blob: fb613d70c2cbb32e381bba6951568bc217ac8d5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02008 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kmod.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/ctype.h>
18#include <linux/major.h>
19#include <linux/slab.h>
20#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080021#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020022#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/ccwdev.h>
26#include <asm/ebcdic.h>
27#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010028#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010029#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* This is ugly... */
32#define PRINTK_HEADER "dasd:"
33
34#include "dasd_int.h"
35/*
36 * SECTION: Constant definitions to be used within this file
37 */
38#define DASD_CHANQ_MAX_SIZE 4
39
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +020040#define DASD_SLEEPON_START_TAG (void *) 1
41#define DASD_SLEEPON_END_TAG (void *) 2
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * SECTION: exported variables of dasd.c
45 */
46debug_info_t *dasd_debug_area;
47struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010048void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
51MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
52 " Copyright 2000 IBM Corporation");
53MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_LICENSE("GPL");
55
56/*
57 * SECTION: prototypes for static functions of dasd.c
58 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010059static int dasd_alloc_queue(struct dasd_block *);
60static void dasd_setup_queue(struct dasd_block *);
61static void dasd_free_queue(struct dasd_block *);
62static void dasd_flush_request_queue(struct dasd_block *);
63static int dasd_flush_block_queue(struct dasd_block *);
64static void dasd_device_tasklet(struct dasd_device *);
65static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000066static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020067static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020068static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010069static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010070static void dasd_device_timeout(unsigned long);
71static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010072static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * SECTION: Operations on the device structure.
76 */
77static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020078static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020079static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Allocate memory for a new device structure.
83 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010084struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct dasd_device *device;
87
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010088 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
89 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Get two pages for normal block device operations. */
93 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010094 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(device);
96 return ERR_PTR(-ENOMEM);
97 }
98 /* Get one page for error recovery. */
99 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100100 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 free_pages((unsigned long) device->ccw_mem, 1);
102 kfree(device);
103 return ERR_PTR(-ENOMEM);
104 }
105
106 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
107 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
108 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100109 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200110 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100111 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 (unsigned long) device);
113 INIT_LIST_HEAD(&device->ccw_queue);
114 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100115 device->timer.function = dasd_device_timeout;
116 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000117 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200118 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200119 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 device->state = DASD_STATE_NEW;
121 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100122 mutex_init(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return device;
125}
126
127/*
128 * Free memory of a device structure.
129 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100130void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800132 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 free_page((unsigned long) device->erp_mem);
134 free_pages((unsigned long) device->ccw_mem, 1);
135 kfree(device);
136}
137
138/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100139 * Allocate memory for a new device structure.
140 */
141struct dasd_block *dasd_alloc_block(void)
142{
143 struct dasd_block *block;
144
145 block = kzalloc(sizeof(*block), GFP_ATOMIC);
146 if (!block)
147 return ERR_PTR(-ENOMEM);
148 /* open_count = 0 means device online but not in use */
149 atomic_set(&block->open_count, -1);
150
151 spin_lock_init(&block->request_queue_lock);
152 atomic_set(&block->tasklet_scheduled, 0);
153 tasklet_init(&block->tasklet,
154 (void (*)(unsigned long)) dasd_block_tasklet,
155 (unsigned long) block);
156 INIT_LIST_HEAD(&block->ccw_queue);
157 spin_lock_init(&block->queue_lock);
158 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100159 block->timer.function = dasd_block_timeout;
160 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100161
162 return block;
163}
164
165/*
166 * Free memory of a device structure.
167 */
168void dasd_free_block(struct dasd_block *block)
169{
170 kfree(block);
171}
172
173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Make a new device known to the system.
175 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int rc;
179
180 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200181 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * keep the reference count > 0.
183 */
184 dasd_get_device(device);
185
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100186 if (device->block) {
187 rc = dasd_alloc_queue(device->block);
188 if (rc) {
189 dasd_put_device(device);
190 return rc;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 device->state = DASD_STATE_KNOWN;
194 return 0;
195}
196
197/*
198 * Let the system forget about a device.
199 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100200static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800202 /* Disable extended error reporting for this device. */
203 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100205 if (device->discipline) {
206 if (device->discipline->uncheck_device)
207 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800208 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800211 if (device->base_discipline)
212 module_put(device->base_discipline->owner);
213 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 device->state = DASD_STATE_NEW;
215
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100216 if (device->block)
217 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Give up reference we took in dasd_state_new_to_known. */
220 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * Request the irq line for the device.
226 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100227static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int rc;
230
231 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100232 if (device->block) {
233 rc = dasd_gendisk_alloc(device->block);
234 if (rc)
235 return rc;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100238 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100239 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200241 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
243
244 device->state = DASD_STATE_BASIC;
245 return 0;
246}
247
248/*
249 * Release the irq line for the device. Terminate any running i/o.
250 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Horst Hummel8f617012006-08-30 14:33:33 +0200253 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100254 if (device->block) {
255 dasd_gendisk_free(device->block);
256 dasd_block_clear_timer(device->block);
257 }
258 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200259 if (rc)
260 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100261 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
264 if (device->debug_area != NULL) {
265 debug_unregister(device->debug_area);
266 device->debug_area = NULL;
267 }
268 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * Do the initial analysis. The do_analysis function may return
274 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
275 * until the discipline decides to continue the startup sequence
276 * by calling the function dasd_change_state. The eckd disciplines
277 * uses this to start a ccw that detects the format. The completion
278 * interrupt for this detection ccw uses the kernel event daemon to
279 * trigger the call to dasd_change_state. All this is done in the
280 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800281 * After the analysis ccw is done (do_analysis returned 0) the block
282 * device is setup.
283 * In case the analysis returns an error, the device setup is stopped
284 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100286static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100292 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800293 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294 if (block) {
295 if (block->base->discipline->do_analysis != NULL)
296 rc = block->base->discipline->do_analysis(block);
297 if (rc) {
298 if (rc != -EAGAIN)
299 device->state = DASD_STATE_UNFMT;
300 return rc;
301 }
302 dasd_setup_queue(block);
303 set_capacity(block->gdp,
304 block->blocks << block->s2b_shift);
305 device->state = DASD_STATE_READY;
306 rc = dasd_scan_partitions(block);
307 if (rc)
308 device->state = DASD_STATE_BASIC;
309 } else {
310 device->state = DASD_STATE_READY;
311 }
Horst Hummel90f00942006-03-07 21:55:39 -0800312 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * Remove device from block device layer. Destroy dirty buffers.
317 * Forget format information. Check if the target level is basic
318 * and if it is create fake disk for formatting.
319 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100320static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Horst Hummel8f617012006-08-30 14:33:33 +0200322 int rc;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325 if (device->block) {
326 struct dasd_block *block = device->block;
327 rc = dasd_flush_block_queue(block);
328 if (rc) {
329 device->state = DASD_STATE_READY;
330 return rc;
331 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100333 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 block->blocks = 0;
335 block->bp_block = 0;
336 block->s2b_shift = 0;
337 }
Horst Hummel8f617012006-08-30 14:33:33 +0200338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
Horst Hummel90f00942006-03-07 21:55:39 -0800342 * Back to basic.
343 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100344static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800345{
346 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200347 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800348}
349
350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * Make the device online and schedule the bottom half to start
352 * the requeueing of requests from the linux request queue to the
353 * ccw queue.
354 */
Horst Hummel8f617012006-08-30 14:33:33 +0200355static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356dasd_state_ready_to_online(struct dasd_device * device)
357{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100358 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100359 struct gendisk *disk;
360 struct disk_part_iter piter;
361 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100362
363 if (device->discipline->ready_to_online) {
364 rc = device->discipline->ready_to_online(device);
365 if (rc)
366 return rc;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100369 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100371 disk = device->block->bdev->bd_disk;
372 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
373 while ((part = disk_part_iter_next(&piter)))
374 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
375 disk_part_iter_exit(&piter);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378}
379
380/*
381 * Stop the requeueing of requests again.
382 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100383static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100386 struct gendisk *disk;
387 struct disk_part_iter piter;
388 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100389
390 if (device->discipline->online_to_ready) {
391 rc = device->discipline->online_to_ready(device);
392 if (rc)
393 return rc;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100396 if (device->block) {
397 disk = device->block->bdev->bd_disk;
398 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
399 while ((part = disk_part_iter_next(&piter)))
400 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
401 disk_part_iter_exit(&piter);
402 }
Horst Hummel8f617012006-08-30 14:33:33 +0200403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/*
407 * Device startup state changes.
408 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100409static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 int rc;
412
413 rc = 0;
414 if (device->state == DASD_STATE_NEW &&
415 device->target >= DASD_STATE_KNOWN)
416 rc = dasd_state_new_to_known(device);
417
418 if (!rc &&
419 device->state == DASD_STATE_KNOWN &&
420 device->target >= DASD_STATE_BASIC)
421 rc = dasd_state_known_to_basic(device);
422
423 if (!rc &&
424 device->state == DASD_STATE_BASIC &&
425 device->target >= DASD_STATE_READY)
426 rc = dasd_state_basic_to_ready(device);
427
428 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700429 device->state == DASD_STATE_UNFMT &&
430 device->target > DASD_STATE_UNFMT)
431 rc = -EPERM;
432
433 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 device->state == DASD_STATE_READY &&
435 device->target >= DASD_STATE_ONLINE)
436 rc = dasd_state_ready_to_online(device);
437
438 return rc;
439}
440
441/*
442 * Device shutdown state changes.
443 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100444static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Horst Hummel8f617012006-08-30 14:33:33 +0200446 int rc;
447
448 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (device->state == DASD_STATE_ONLINE &&
450 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200451 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200452
Horst Hummel8f617012006-08-30 14:33:33 +0200453 if (!rc &&
454 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200456 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800457
Horst Hummel8f617012006-08-30 14:33:33 +0200458 if (!rc &&
459 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800460 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200461 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800462
Horst Hummel8f617012006-08-30 14:33:33 +0200463 if (!rc &&
464 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200466 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200467
Horst Hummel8f617012006-08-30 14:33:33 +0200468 if (!rc &&
469 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200471 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Horst Hummel8f617012006-08-30 14:33:33 +0200473 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
477 * This is the main startup/shutdown routine.
478 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100479static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Sebastian Ott181d9522009-06-22 12:08:21 +0200481 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (device->state == device->target)
484 /* Already where we want to go today... */
485 return;
486 if (device->state < device->target)
487 rc = dasd_increase_state(device);
488 else
489 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200490 if (rc == -EAGAIN)
491 return;
492 if (rc)
493 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Stefan Haberland9eb25122010-02-26 22:37:46 +0100495 if (device->state == device->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200497
498 /* let user-space know that the device status changed */
499 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/*
503 * Kick starter for devices that did not complete the startup/shutdown
504 * procedure or were sleeping because of a pending state.
505 * dasd_kick_device will schedule a call do do_kick_device to the kernel
506 * event daemon.
507 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100508static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Al Viro4927b3f2006-12-06 19:18:20 +0000510 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100511 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100513 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100514 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dasd_put_device(device);
516}
517
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100518void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 dasd_get_device(device);
521 /* queue call to dasd_kick_device to the kernel event daemon. */
522 schedule_work(&device->kick_work);
523}
524
525/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200526 * dasd_reload_device will schedule a call do do_reload_device to the kernel
527 * event daemon.
528 */
529static void do_reload_device(struct work_struct *work)
530{
531 struct dasd_device *device = container_of(work, struct dasd_device,
532 reload_device);
533 device->discipline->reload(device);
534 dasd_put_device(device);
535}
536
537void dasd_reload_device(struct dasd_device *device)
538{
539 dasd_get_device(device);
540 /* queue call to dasd_reload_device to the kernel event daemon. */
541 schedule_work(&device->reload_device);
542}
543EXPORT_SYMBOL(dasd_reload_device);
544
545/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200546 * dasd_restore_device will schedule a call do do_restore_device to the kernel
547 * event daemon.
548 */
549static void do_restore_device(struct work_struct *work)
550{
551 struct dasd_device *device = container_of(work, struct dasd_device,
552 restore_device);
553 device->cdev->drv->restore(device->cdev);
554 dasd_put_device(device);
555}
556
557void dasd_restore_device(struct dasd_device *device)
558{
559 dasd_get_device(device);
560 /* queue call to dasd_restore_device to the kernel event daemon. */
561 schedule_work(&device->restore_device);
562}
563
564/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * Set the target state for a device and starts the state change.
566 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100567void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200569 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100570 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* If we are in probeonly mode stop at DASD_STATE_READY. */
572 if (dasd_probeonly && target > DASD_STATE_READY)
573 target = DASD_STATE_READY;
574 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100575 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 wake_up(&dasd_init_waitq);
577 device->target = target;
578 }
579 if (device->state != device->target)
580 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100581 mutex_unlock(&device->state_mutex);
582 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/*
586 * Enable devices with device numbers in [from..to].
587 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100588static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 return (device->state == device->target);
591}
592
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100593void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 dasd_set_target_state(device, DASD_STATE_ONLINE);
596 if (device->state <= DASD_STATE_KNOWN)
597 /* No discipline for device found. */
598 dasd_set_target_state(device, DASD_STATE_NEW);
599 /* Now wait for the devices to come up. */
600 wait_event(dasd_init_waitq, _wait_for_device(device));
601}
602
603/*
604 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
605 */
606#ifdef CONFIG_DASD_PROFILE
607
608struct dasd_profile_info_t dasd_global_profile;
609unsigned int dasd_profile_level = DASD_PROFILE_OFF;
610
611/*
612 * Increments counter in global and local profiling structures.
613 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100614#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{ \
616 int index; \
617 for (index = 0; index < 31 && value >> (2+index); index++); \
618 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100619 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/*
623 * Add profiling information for cqr before execution.
624 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100625static void dasd_profile_start(struct dasd_block *block,
626 struct dasd_ccw_req *cqr,
627 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct list_head *l;
630 unsigned int counter;
631
632 if (dasd_profile_level != DASD_PROFILE_ON)
633 return;
634
635 /* count the length of the chanq for statistics */
636 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100637 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (++counter >= 31)
639 break;
640 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100641 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
645 * Add profiling information for cqr after execution.
646 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100647static void dasd_profile_end(struct dasd_block *block,
648 struct dasd_ccw_req *cqr,
649 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 long strtime, irqtime, endtime, tottime; /* in microseconds */
652 long tottimeps, sectors;
653
654 if (dasd_profile_level != DASD_PROFILE_ON)
655 return;
656
Tejun Heo83096eb2009-05-07 22:24:39 +0900657 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!cqr->buildclk || !cqr->startclk ||
659 !cqr->stopclk || !cqr->endclk ||
660 !sectors)
661 return;
662
663 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
664 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
665 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
666 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
667 tottimeps = tottime / sectors;
668
669 if (!dasd_global_profile.dasd_io_reqs)
670 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100671 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 dasd_global_profile.dasd_io_reqs++;
673 dasd_global_profile.dasd_io_sects += sectors;
674
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100675 if (!block->profile.dasd_io_reqs)
676 memset(&block->profile, 0,
677 sizeof(struct dasd_profile_info_t));
678 block->profile.dasd_io_reqs++;
679 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100681 dasd_profile_counter(sectors, dasd_io_secs, block);
682 dasd_profile_counter(tottime, dasd_io_times, block);
683 dasd_profile_counter(tottimeps, dasd_io_timps, block);
684 dasd_profile_counter(strtime, dasd_io_time1, block);
685 dasd_profile_counter(irqtime, dasd_io_time2, block);
686 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
687 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100690#define dasd_profile_start(block, cqr, req) do {} while (0)
691#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#endif /* CONFIG_DASD_PROFILE */
693
694/*
695 * Allocate memory for a channel program with 'cplength' channel
696 * command words and 'datasize' additional space. There are two
697 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
698 * memory and 2) dasd_smalloc_request uses the static ccw memory
699 * that gets allocated for each device.
700 */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200701struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100702 int datasize,
703 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct dasd_ccw_req *cqr;
706
707 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200708 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100709 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800711 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (cqr == NULL)
713 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 cqr->cpaddr = NULL;
715 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800716 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 GFP_ATOMIC | GFP_DMA);
718 if (cqr->cpaddr == NULL) {
719 kfree(cqr);
720 return ERR_PTR(-ENOMEM);
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 cqr->data = NULL;
724 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800725 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800727 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 kfree(cqr);
729 return ERR_PTR(-ENOMEM);
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200732 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
734 dasd_get_device(device);
735 return cqr;
736}
737
Stefan Haberland68b781f2009-09-11 10:28:29 +0200738struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100739 int datasize,
740 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 unsigned long flags;
743 struct dasd_ccw_req *cqr;
744 char *data;
745 int size;
746
747 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200748 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100749 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
752 if (cplength > 0)
753 size += cplength * sizeof(struct ccw1);
754 if (datasize > 0)
755 size += datasize;
756 spin_lock_irqsave(&device->mem_lock, flags);
757 cqr = (struct dasd_ccw_req *)
758 dasd_alloc_chunk(&device->ccw_chunks, size);
759 spin_unlock_irqrestore(&device->mem_lock, flags);
760 if (cqr == NULL)
761 return ERR_PTR(-ENOMEM);
762 memset(cqr, 0, sizeof(struct dasd_ccw_req));
763 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
764 cqr->cpaddr = NULL;
765 if (cplength > 0) {
766 cqr->cpaddr = (struct ccw1 *) data;
767 data += cplength*sizeof(struct ccw1);
768 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
769 }
770 cqr->data = NULL;
771 if (datasize > 0) {
772 cqr->data = data;
773 memset(cqr->data, 0, datasize);
774 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200775 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
777 dasd_get_device(device);
778 return cqr;
779}
780
781/*
782 * Free memory of a channel program. This function needs to free all the
783 * idal lists that might have been created by dasd_set_cda and the
784 * struct dasd_ccw_req itself.
785 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100786void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800788#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct ccw1 *ccw;
790
791 /* Clear any idals used for the request. */
792 ccw = cqr->cpaddr;
793 do {
794 clear_normalized_cda(ccw);
795 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
796#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800797 kfree(cqr->cpaddr);
798 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 kfree(cqr);
800 dasd_put_device(device);
801}
802
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100803void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 unsigned long flags;
806
807 spin_lock_irqsave(&device->mem_lock, flags);
808 dasd_free_chunk(&device->ccw_chunks, cqr);
809 spin_unlock_irqrestore(&device->mem_lock, flags);
810 dasd_put_device(device);
811}
812
813/*
814 * Check discipline magic in cqr.
815 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100816static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct dasd_device *device;
819
820 if (cqr == NULL)
821 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100822 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100824 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 " dasd_ccw_req 0x%08x magic doesn't match"
826 " discipline 0x%08x",
827 cqr->magic,
828 *(unsigned int *) device->discipline->name);
829 return -EINVAL;
830 }
831 return 0;
832}
833
834/*
835 * Terminate the current i/o and set the request to clear_pending.
836 * Timer keeps device runnig.
837 * ccw_device_clear can fail if the i/o subsystem
838 * is in a bad mood.
839 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100840int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct dasd_device *device;
843 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100844 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Check the cqr */
847 rc = dasd_check_cqr(cqr);
848 if (rc)
849 return rc;
850 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100851 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
853 rc = ccw_device_clear(device->cdev, (long) cqr);
854 switch (rc) {
855 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800856 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100857 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200859 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 DBF_DEV_EVENT(DBF_DEBUG, device,
861 "terminate cqr %p successful",
862 cqr);
863 break;
864 case -ENODEV:
865 DBF_DEV_EVENT(DBF_ERR, device, "%s",
866 "device gone, retry");
867 break;
868 case -EIO:
869 DBF_DEV_EVENT(DBF_ERR, device, "%s",
870 "I/O error, retry");
871 break;
872 case -EINVAL:
873 case -EBUSY:
874 DBF_DEV_EVENT(DBF_ERR, device, "%s",
875 "device busy, retry later");
876 break;
877 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100878 /* internal error 10 - unknown rc*/
879 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
880 dev_err(&device->cdev->dev, "An error occurred in the "
881 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 BUG();
883 break;
884 }
885 retries++;
886 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100887 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return rc;
889}
890
891/*
892 * Start the i/o. This start_IO can fail if the channel is really busy.
893 * In that case set up a timer to start the request later.
894 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100895int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct dasd_device *device;
898 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100899 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Check the cqr */
902 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200903 if (rc) {
904 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200906 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100907 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100909 /* internal error 14 - start_IO run out of retries */
910 sprintf(errorstring, "14 %p", cqr);
911 dev_err(&device->cdev->dev, "An error occurred in the DASD "
912 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100913 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EIO;
915 }
916 cqr->startclk = get_clock();
917 cqr->starttime = jiffies;
918 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100919 if (cqr->cpmode == 1) {
920 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
921 (long) cqr, cqr->lpm);
922 } else {
923 rc = ccw_device_start(device->cdev, cqr->cpaddr,
924 (long) cqr, cqr->lpm, 0);
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 switch (rc) {
927 case 0:
928 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100931 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 "start_IO: device busy, retry later");
933 break;
934 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100935 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 "start_IO: request timeout, retry later");
937 break;
938 case -EACCES:
939 /* -EACCES indicates that the request used only a
940 * subset of the available pathes and all these
941 * pathes are gone.
942 * Do a retry with all available pathes.
943 */
944 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100945 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 "start_IO: selected pathes gone,"
947 " retry on all pathes");
948 break;
949 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100950 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
951 "start_IO: -ENODEV device gone, retry");
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100954 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100955 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200957 case -EINVAL:
958 /* most likely caused in power management context */
959 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
960 "start_IO: -EINVAL device currently "
961 "not accessible");
962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100964 /* internal error 11 - unknown rc */
965 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
966 dev_err(&device->cdev->dev,
967 "An error occurred in the DASD device driver, "
968 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 BUG();
970 break;
971 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200972 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return rc;
974}
975
976/*
977 * Timeout function for dasd devices. This is used for different purposes
978 * 1) missing interrupt handler for normal operation
979 * 2) delayed start of request where start_IO failed with -EBUSY
980 * 3) timeout for missing state change interrupts
981 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
982 * DASD_CQR_QUEUED for 2) and 3).
983 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100984static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 unsigned long flags;
987 struct dasd_device *device;
988
989 device = (struct dasd_device *) ptr;
990 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
991 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100992 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100994 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997/*
998 * Setup timeout for a device in jiffies.
999 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001000void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001002 if (expires == 0)
1003 del_timer(&device->timer);
1004 else
1005 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/*
1009 * Clear timeout for a device.
1010 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001011void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001013 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001016static void dasd_handle_killed_request(struct ccw_device *cdev,
1017 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct dasd_ccw_req *cqr;
1020 struct dasd_device *device;
1021
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001022 if (!intparm)
1023 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 cqr = (struct dasd_ccw_req *) intparm;
1025 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001026 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1027 "invalid status in handle_killed_request: "
1028 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return;
1030 }
1031
Stefan Haberland589c74d2010-02-26 22:37:47 +01001032 device = dasd_device_from_cdev_locked(cdev);
1033 if (IS_ERR(device)) {
1034 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1035 "unable to get device from cdev");
1036 return;
1037 }
1038
1039 if (!cqr->startdev ||
1040 device != cqr->startdev ||
1041 strncmp(cqr->startdev->discipline->ebcname,
1042 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001043 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1044 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001045 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return;
1047 }
1048
1049 /* Schedule request to be retried. */
1050 cqr->status = DASD_CQR_QUEUED;
1051
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001052 dasd_device_clear_timer(device);
1053 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dasd_put_device(device);
1055}
1056
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001057void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001059 /* First of all start sense subsystem status request. */
1060 dasd_eer_snss(device);
1061
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001062 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001063 dasd_schedule_device_bh(device);
1064 if (device->block)
1065 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068/*
1069 * Interrupt handler for "normal" ssch-io based dasd devices.
1070 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001071void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1072 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct dasd_ccw_req *cqr, *next;
1075 struct dasd_device *device;
1076 unsigned long long now;
1077 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (IS_ERR(irb)) {
1080 switch (PTR_ERR(irb)) {
1081 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
1083 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001084 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1085 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 break;
1087 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001088 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1089 "unknown error %ld\n", __func__,
1090 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001092 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094 }
1095
1096 now = get_clock();
1097
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001098 /* check for unsolicited interrupts */
1099 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001100 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1101 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001102 ((scsw_stctl(&irb->scsw) == SCSW_STCTL_STATUS_PEND) ||
1103 (scsw_stctl(&irb->scsw) == (SCSW_STCTL_STATUS_PEND |
1104 SCSW_STCTL_ALERT_STATUS))))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001105 if (cqr && cqr->status == DASD_CQR_IN_IO)
1106 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001107 if (cqr)
1108 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001109 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001110 if (IS_ERR(device))
1111 return;
1112 /* ignore unsolicited interrupts for DIAG discipline */
1113 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Stefan Haberland56b86b62010-10-25 16:10:49 +02001117 device->discipline->dump_sense_dbf(device, irb,
1118 "unsolicited");
1119 if ((device->features & DASD_FEATURE_ERPLOG))
1120 device->discipline->dump_sense(device, cqr,
1121 irb);
1122 dasd_device_clear_timer(device);
1123 device->discipline->handle_unsolicited_interrupt(device,
1124 irb);
1125 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return;
1127 }
1128
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001129 device = (struct dasd_device *) cqr->startdev;
1130 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001132 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1133 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
1135 }
1136
1137 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001138 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001139 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001140 cqr->status = DASD_CQR_CLEARED;
1141 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001142 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001143 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return;
1145 }
1146
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001147 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001149 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1150 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return;
1152 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001153
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001154 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001156 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1157 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001158 /* request was completed successfully */
1159 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 cqr->stopclk = now;
1161 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001162 if (cqr->devlist.next != &device->ccw_queue) {
1163 next = list_entry(cqr->devlist.next,
1164 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001166 } else { /* error */
1167 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001168 /* log sense for every failed I/O to s390 debugfeature */
1169 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001170 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001171 dasd_log_sense(cqr, irb);
1172 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001173
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001174 /*
1175 * If we don't want complex ERP for this request, then just
1176 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001177 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001178 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001179 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001180 if (cqr->lpm == LPM_ANYPATH)
1181 DBF_DEV_EVENT(DBF_DEBUG, device,
1182 "default ERP in fastpath "
1183 "(%i retries left)",
1184 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001185 cqr->lpm = LPM_ANYPATH;
1186 cqr->status = DASD_CQR_QUEUED;
1187 next = cqr;
1188 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001190 }
1191 if (next && (next->status == DASD_CQR_QUEUED) &&
1192 (!device->stopped)) {
1193 if (device->discipline->start_IO(next) == 0)
1194 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001197 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001199 dasd_device_clear_timer(device);
1200 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001203enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1204{
1205 struct dasd_device *device;
1206
1207 device = dasd_device_from_cdev_locked(cdev);
1208
1209 if (IS_ERR(device))
1210 goto out;
1211 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1212 device->state != device->target ||
1213 !device->discipline->handle_unsolicited_interrupt){
1214 dasd_put_device(device);
1215 goto out;
1216 }
1217
1218 dasd_device_clear_timer(device);
1219 device->discipline->handle_unsolicited_interrupt(device, irb);
1220 dasd_put_device(device);
1221out:
1222 return UC_TODO_RETRY;
1223}
1224EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001227 * If we have an error on a dasd_block layer request then we cancel
1228 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001230static void __dasd_device_recovery(struct dasd_device *device,
1231 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct list_head *l, *n;
1234 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001237 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001239 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001241
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001242 list_for_each_safe(l, n, &device->ccw_queue) {
1243 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1244 if (cqr->status == DASD_CQR_QUEUED &&
1245 ref_cqr->block == cqr->block) {
1246 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001248 }
1249};
1250
1251/*
1252 * Remove those ccw requests from the queue that need to be returned
1253 * to the upper layer.
1254 */
1255static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1256 struct list_head *final_queue)
1257{
1258 struct list_head *l, *n;
1259 struct dasd_ccw_req *cqr;
1260
1261 /* Process request with final status. */
1262 list_for_each_safe(l, n, &device->ccw_queue) {
1263 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1264
1265 /* Stop list processing at the first non-final request. */
1266 if (cqr->status == DASD_CQR_QUEUED ||
1267 cqr->status == DASD_CQR_IN_IO ||
1268 cqr->status == DASD_CQR_CLEAR_PENDING)
1269 break;
1270 if (cqr->status == DASD_CQR_ERROR) {
1271 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001273 /* Rechain finished requests to final queue */
1274 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276}
1277
1278/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001279 * the cqrs from the final queue are returned to the upper layer
1280 * by setting a dasd_block state and calling the callback function
1281 */
1282static void __dasd_device_process_final_queue(struct dasd_device *device,
1283 struct list_head *final_queue)
1284{
1285 struct list_head *l, *n;
1286 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001287 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001288 void (*callback)(struct dasd_ccw_req *, void *data);
1289 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001290 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001291
1292 list_for_each_safe(l, n, final_queue) {
1293 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1294 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001295 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001296 callback = cqr->callback;
1297 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001298 if (block)
1299 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001300 switch (cqr->status) {
1301 case DASD_CQR_SUCCESS:
1302 cqr->status = DASD_CQR_DONE;
1303 break;
1304 case DASD_CQR_ERROR:
1305 cqr->status = DASD_CQR_NEED_ERP;
1306 break;
1307 case DASD_CQR_CLEARED:
1308 cqr->status = DASD_CQR_TERMINATED;
1309 break;
1310 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001311 /* internal error 12 - wrong cqr status*/
1312 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1313 dev_err(&device->cdev->dev,
1314 "An error occurred in the DASD device driver, "
1315 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 BUG();
1317 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001318 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001319 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001320 if (block)
1321 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001322 }
1323}
1324
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * Take a look at the first request on the ccw queue and check
1327 * if it reached its expire time. If so, terminate the IO.
1328 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001329static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct dasd_ccw_req *cqr;
1332
1333 if (list_empty(&device->ccw_queue))
1334 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001335 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001336 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1337 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1338 if (device->discipline->term_IO(cqr) != 0) {
1339 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001340 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001341 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001342 "ended, retrying in 5 s\n",
1343 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001344 cqr->expires += 5*HZ;
1345 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001346 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001347 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001348 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001349 "remaining\n", cqr, (cqr->expires/HZ),
1350 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 }
1353}
1354
1355/*
1356 * Take a look at the first request on the ccw queue and check
1357 * if it needs to be started.
1358 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct dasd_ccw_req *cqr;
1362 int rc;
1363
1364 if (list_empty(&device->ccw_queue))
1365 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001366 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001367 if (cqr->status != DASD_CQR_QUEUED)
1368 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001369 /* when device is stopped, return request to previous layer */
1370 if (device->stopped) {
1371 cqr->status = DASD_CQR_CLEARED;
1372 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001373 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001374 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001375
1376 rc = device->discipline->start_IO(cqr);
1377 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001379 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001380 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001381 } else
1382 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001383 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387 * Go through all request on the dasd_device request queue,
1388 * terminate them on the cdev if necessary, and return them to the
1389 * submitting layer via callback.
1390 * Note:
1391 * Make sure that all 'submitting layers' still exist when
1392 * this function is called!. In other words, when 'device' is a base
1393 * device then all block layer requests must have been removed before
1394 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001396int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001398 struct dasd_ccw_req *cqr, *n;
1399 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 INIT_LIST_HEAD(&flush_queue);
1403 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001404 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001405 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001406 /* Check status and move request to flush_queue */
1407 switch (cqr->status) {
1408 case DASD_CQR_IN_IO:
1409 rc = device->discipline->term_IO(cqr);
1410 if (rc) {
1411 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001412 dev_err(&device->cdev->dev,
1413 "Flushing the DASD request queue "
1414 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001415 /* stop flush processing */
1416 goto finished;
1417 }
1418 break;
1419 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001421 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001422 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001423 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001424 break;
1425 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001426 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001427 }
Horst Hummel8f617012006-08-30 14:33:33 +02001428finished:
1429 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001430 /*
1431 * After this point all requests must be in state CLEAR_PENDING,
1432 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1433 * one of the others.
1434 */
1435 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1436 wait_event(dasd_flush_wq,
1437 (cqr->status != DASD_CQR_CLEAR_PENDING));
1438 /*
1439 * Now set each request back to TERMINATED, DONE or NEED_ERP
1440 * and call the callback function of flushed requests
1441 */
1442 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001443 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446/*
1447 * Acquire the device lock and process queues for the device.
1448 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001449static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 atomic_set (&device->tasklet_scheduled, 0);
1454 INIT_LIST_HEAD(&final_queue);
1455 spin_lock_irq(get_ccwdev_lock(device->cdev));
1456 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001457 __dasd_device_check_expire(device);
1458 /* find final requests on ccw queue */
1459 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1461 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001462 __dasd_device_process_final_queue(device, &final_queue);
1463 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001465 __dasd_device_start_head(device);
1466 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dasd_put_device(device);
1468}
1469
1470/*
1471 * Schedules a call to dasd_tasklet over the device tasklet.
1472 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001473void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001476 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return;
1478 dasd_get_device(device);
1479 tasklet_hi_schedule(&device->tasklet);
1480}
1481
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001482void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1483{
1484 device->stopped |= bits;
1485}
1486EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1487
1488void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1489{
1490 device->stopped &= ~bits;
1491 if (!device->stopped)
1492 wake_up(&generic_waitq);
1493}
1494EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001497 * Queue a request to the head of the device ccw_queue.
1498 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001500void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 struct dasd_device *device;
1503 unsigned long flags;
1504
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001505 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001507 cqr->status = DASD_CQR_QUEUED;
1508 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001510 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1512}
1513
1514/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001515 * Queue a request to the tail of the device ccw_queue.
1516 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct dasd_device *device;
1521 unsigned long flags;
1522
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001523 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001525 cqr->status = DASD_CQR_QUEUED;
1526 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001528 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1530}
1531
1532/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001533 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001535static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001537 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1538 cqr->callback_data = DASD_SLEEPON_END_TAG;
1539 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1540 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001543static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 struct dasd_device *device;
1546 int rc;
1547
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001548 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001550 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1552 return rc;
1553}
1554
1555/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001556 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1557 */
1558static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1559{
1560 struct dasd_device *device;
1561 dasd_erp_fn_t erp_fn;
1562
1563 if (cqr->status == DASD_CQR_FILLED)
1564 return 0;
1565 device = cqr->startdev;
1566 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1567 if (cqr->status == DASD_CQR_TERMINATED) {
1568 device->discipline->handle_terminated_request(cqr);
1569 return 1;
1570 }
1571 if (cqr->status == DASD_CQR_NEED_ERP) {
1572 erp_fn = device->discipline->erp_action(cqr);
1573 erp_fn(cqr);
1574 return 1;
1575 }
1576 if (cqr->status == DASD_CQR_FAILED)
1577 dasd_log_sense(cqr, &cqr->irb);
1578 if (cqr->refers) {
1579 __dasd_process_erp(device, cqr);
1580 return 1;
1581 }
1582 }
1583 return 0;
1584}
1585
1586static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1587{
1588 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1589 if (cqr->refers) /* erp is not done yet */
1590 return 1;
1591 return ((cqr->status != DASD_CQR_DONE) &&
1592 (cqr->status != DASD_CQR_FAILED));
1593 } else
1594 return (cqr->status == DASD_CQR_FILLED);
1595}
1596
1597static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1598{
1599 struct dasd_device *device;
1600 int rc;
1601 struct list_head ccw_queue;
1602 struct dasd_ccw_req *cqr;
1603
1604 INIT_LIST_HEAD(&ccw_queue);
1605 maincqr->status = DASD_CQR_FILLED;
1606 device = maincqr->startdev;
1607 list_add(&maincqr->blocklist, &ccw_queue);
1608 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1609 cqr = list_first_entry(&ccw_queue,
1610 struct dasd_ccw_req, blocklist)) {
1611
1612 if (__dasd_sleep_on_erp(cqr))
1613 continue;
1614 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1615 continue;
1616
1617 /* Non-temporary stop condition will trigger fail fast */
1618 if (device->stopped & ~DASD_STOPPED_PENDING &&
1619 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1620 (!dasd_eer_enabled(device))) {
1621 cqr->status = DASD_CQR_FAILED;
1622 continue;
1623 }
1624
1625 /* Don't try to start requests if device is stopped */
1626 if (interruptible) {
1627 rc = wait_event_interruptible(
1628 generic_waitq, !(device->stopped));
1629 if (rc == -ERESTARTSYS) {
1630 cqr->status = DASD_CQR_FAILED;
1631 maincqr->intrc = rc;
1632 continue;
1633 }
1634 } else
1635 wait_event(generic_waitq, !(device->stopped));
1636
1637 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001638 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001639 dasd_add_request_tail(cqr);
1640 if (interruptible) {
1641 rc = wait_event_interruptible(
1642 generic_waitq, _wait_for_wakeup(cqr));
1643 if (rc == -ERESTARTSYS) {
1644 dasd_cancel_req(cqr);
1645 /* wait (non-interruptible) for final status */
1646 wait_event(generic_waitq,
1647 _wait_for_wakeup(cqr));
1648 cqr->status = DASD_CQR_FAILED;
1649 maincqr->intrc = rc;
1650 continue;
1651 }
1652 } else
1653 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1654 }
1655
1656 maincqr->endclk = get_clock();
1657 if ((maincqr->status != DASD_CQR_DONE) &&
1658 (maincqr->intrc != -ERESTARTSYS))
1659 dasd_log_sense(maincqr, &maincqr->irb);
1660 if (maincqr->status == DASD_CQR_DONE)
1661 rc = 0;
1662 else if (maincqr->intrc)
1663 rc = maincqr->intrc;
1664 else
1665 rc = -EIO;
1666 return rc;
1667}
1668
1669/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001670 * Queue a request to the tail of the device ccw_queue and wait for
1671 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001675 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001679 * Queue a request to the tail of the device ccw_queue and wait
1680 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001682int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001684 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687/*
1688 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1689 * for eckd devices) the currently running request has to be terminated
1690 * and be put back to status queued, before the special request is added
1691 * to the head of the queue. Then the special request is waited on normally.
1692 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001693static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 if (list_empty(&device->ccw_queue))
1698 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001699 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001700 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001703int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct dasd_device *device;
1706 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001707
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001708 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_lock_irq(get_ccwdev_lock(device->cdev));
1710 rc = _dasd_term_running_cqr(device);
1711 if (rc) {
1712 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1713 return rc;
1714 }
Horst Hummel138c0142006-06-29 14:58:12 +02001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001717 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001719 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001722 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1725
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001726 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001727
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001728 if (cqr->status == DASD_CQR_DONE)
1729 rc = 0;
1730 else if (cqr->intrc)
1731 rc = cqr->intrc;
1732 else
1733 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return rc;
1735}
1736
1737/*
1738 * Cancels a request that was started with dasd_sleep_on_req.
1739 * This is useful to timeout requests. The request will be
1740 * terminated if it is currently in i/o.
1741 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001742 * 0 if there was no need to terminate the request (not started yet)
1743 * negative error code if termination failed
1744 * Cancellation of a request is an asynchronous operation! The calling
1745 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001747int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 unsigned long flags;
1751 int rc;
1752
1753 rc = 0;
1754 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1755 switch (cqr->status) {
1756 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001757 /* request was not started - just set to cleared */
1758 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 break;
1760 case DASD_CQR_IN_IO:
1761 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 rc = device->discipline->term_IO(cqr);
1763 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001764 dev_err(&device->cdev->dev,
1765 "Cancelling request %p failed with rc=%d\n",
1766 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001767 } else {
1768 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001775 dasd_schedule_device_bh(device);
1776 return rc;
1777}
1778
1779
1780/*
1781 * SECTION: Operations of the dasd_block layer.
1782 */
1783
1784/*
1785 * Timeout function for dasd_block. This is used when the block layer
1786 * is waiting for something that may not come reliably, (e.g. a state
1787 * change interrupt)
1788 */
1789static void dasd_block_timeout(unsigned long ptr)
1790{
1791 unsigned long flags;
1792 struct dasd_block *block;
1793
1794 block = (struct dasd_block *) ptr;
1795 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1796 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001797 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001798 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1799 dasd_schedule_block_bh(block);
1800}
1801
1802/*
1803 * Setup timeout for a dasd_block in jiffies.
1804 */
1805void dasd_block_set_timer(struct dasd_block *block, int expires)
1806{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001807 if (expires == 0)
1808 del_timer(&block->timer);
1809 else
1810 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001811}
1812
1813/*
1814 * Clear timeout for a dasd_block.
1815 */
1816void dasd_block_clear_timer(struct dasd_block *block)
1817{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001818 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001819}
1820
1821/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001822 * Process finished error recovery ccw.
1823 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001824static void __dasd_process_erp(struct dasd_device *device,
1825 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001826{
1827 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001828
1829 if (cqr->status == DASD_CQR_DONE)
1830 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1831 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001832 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001833 erp_fn = device->discipline->erp_postaction(cqr);
1834 erp_fn(cqr);
1835}
1836
1837/*
1838 * Fetch requests from the block device queue.
1839 */
1840static void __dasd_process_request_queue(struct dasd_block *block)
1841{
1842 struct request_queue *queue;
1843 struct request *req;
1844 struct dasd_ccw_req *cqr;
1845 struct dasd_device *basedev;
1846 unsigned long flags;
1847 queue = block->request_queue;
1848 basedev = block->base;
1849 /* No queue ? Then there is nothing to do. */
1850 if (queue == NULL)
1851 return;
1852
1853 /*
1854 * We requeue request from the block device queue to the ccw
1855 * queue only in two states. In state DASD_STATE_READY the
1856 * partition detection is done and we need to requeue requests
1857 * for that. State DASD_STATE_ONLINE is normal block device
1858 * operation.
1859 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001860 if (basedev->state < DASD_STATE_READY) {
1861 while ((req = blk_fetch_request(block->request_queue)))
1862 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001863 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001864 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001865 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001866 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001867 if (basedev->features & DASD_FEATURE_READONLY &&
1868 rq_data_dir(req) == WRITE) {
1869 DBF_DEV_EVENT(DBF_ERR, basedev,
1870 "Rejecting write request %p",
1871 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001872 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001873 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001874 continue;
1875 }
1876 cqr = basedev->discipline->build_cp(basedev, block, req);
1877 if (IS_ERR(cqr)) {
1878 if (PTR_ERR(cqr) == -EBUSY)
1879 break; /* normal end condition */
1880 if (PTR_ERR(cqr) == -ENOMEM)
1881 break; /* terminate request queue loop */
1882 if (PTR_ERR(cqr) == -EAGAIN) {
1883 /*
1884 * The current request cannot be build right
1885 * now, we have to try later. If this request
1886 * is the head-of-queue we stop the device
1887 * for 1/2 second.
1888 */
1889 if (!list_empty(&block->ccw_queue))
1890 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001891 spin_lock_irqsave(
1892 get_ccwdev_lock(basedev->cdev), flags);
1893 dasd_device_set_stop_bits(basedev,
1894 DASD_STOPPED_PENDING);
1895 spin_unlock_irqrestore(
1896 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001897 dasd_block_set_timer(block, HZ/2);
1898 break;
1899 }
1900 DBF_DEV_EVENT(DBF_ERR, basedev,
1901 "CCW creation failed (rc=%ld) "
1902 "on request %p",
1903 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001904 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001905 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001906 continue;
1907 }
1908 /*
1909 * Note: callback is set to dasd_return_cqr_cb in
1910 * __dasd_block_start_head to cover erp requests as well
1911 */
1912 cqr->callback_data = (void *) req;
1913 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001914 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001915 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1916 dasd_profile_start(block, cqr, req);
1917 }
1918}
1919
1920static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1921{
1922 struct request *req;
1923 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001924 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001925
1926 req = (struct request *) cqr->callback_data;
1927 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001928 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001929 if (status <= 0)
1930 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001931 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001932}
1933
1934/*
1935 * Process ccw request queue.
1936 */
1937static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1938 struct list_head *final_queue)
1939{
1940 struct list_head *l, *n;
1941 struct dasd_ccw_req *cqr;
1942 dasd_erp_fn_t erp_fn;
1943 unsigned long flags;
1944 struct dasd_device *base = block->base;
1945
1946restart:
1947 /* Process request with final status. */
1948 list_for_each_safe(l, n, &block->ccw_queue) {
1949 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1950 if (cqr->status != DASD_CQR_DONE &&
1951 cqr->status != DASD_CQR_FAILED &&
1952 cqr->status != DASD_CQR_NEED_ERP &&
1953 cqr->status != DASD_CQR_TERMINATED)
1954 continue;
1955
1956 if (cqr->status == DASD_CQR_TERMINATED) {
1957 base->discipline->handle_terminated_request(cqr);
1958 goto restart;
1959 }
1960
1961 /* Process requests that may be recovered */
1962 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001963 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02001964 if (IS_ERR(erp_fn(cqr)))
1965 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001966 goto restart;
1967 }
1968
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001969 /* log sense for fatal error */
1970 if (cqr->status == DASD_CQR_FAILED) {
1971 dasd_log_sense(cqr, &cqr->irb);
1972 }
1973
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001974 /* First of all call extended error reporting. */
1975 if (dasd_eer_enabled(base) &&
1976 cqr->status == DASD_CQR_FAILED) {
1977 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1978
1979 /* restart request */
1980 cqr->status = DASD_CQR_FILLED;
1981 cqr->retries = 255;
1982 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001983 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001984 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1985 flags);
1986 goto restart;
1987 }
1988
1989 /* Process finished ERP request. */
1990 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001991 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001992 goto restart;
1993 }
1994
1995 /* Rechain finished requests to final queue */
1996 cqr->endclk = get_clock();
1997 list_move_tail(&cqr->blocklist, final_queue);
1998 }
1999}
2000
2001static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2002{
2003 dasd_schedule_block_bh(cqr->block);
2004}
2005
2006static void __dasd_block_start_head(struct dasd_block *block)
2007{
2008 struct dasd_ccw_req *cqr;
2009
2010 if (list_empty(&block->ccw_queue))
2011 return;
2012 /* We allways begin with the first requests on the queue, as some
2013 * of previously started requests have to be enqueued on a
2014 * dasd_device again for error recovery.
2015 */
2016 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2017 if (cqr->status != DASD_CQR_FILLED)
2018 continue;
2019 /* Non-temporary stop condition will trigger fail fast */
2020 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2021 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2022 (!dasd_eer_enabled(block->base))) {
2023 cqr->status = DASD_CQR_FAILED;
2024 dasd_schedule_block_bh(block);
2025 continue;
2026 }
2027 /* Don't try to start requests if device is stopped */
2028 if (block->base->stopped)
2029 return;
2030
2031 /* just a fail safe check, should not happen */
2032 if (!cqr->startdev)
2033 cqr->startdev = block->base;
2034
2035 /* make sure that the requests we submit find their way back */
2036 cqr->callback = dasd_return_cqr_cb;
2037
2038 dasd_add_request_tail(cqr);
2039 }
2040}
2041
2042/*
2043 * Central dasd_block layer routine. Takes requests from the generic
2044 * block layer request queue, creates ccw requests, enqueues them on
2045 * a dasd_device and processes ccw requests that have been returned.
2046 */
2047static void dasd_block_tasklet(struct dasd_block *block)
2048{
2049 struct list_head final_queue;
2050 struct list_head *l, *n;
2051 struct dasd_ccw_req *cqr;
2052
2053 atomic_set(&block->tasklet_scheduled, 0);
2054 INIT_LIST_HEAD(&final_queue);
2055 spin_lock(&block->queue_lock);
2056 /* Finish off requests on ccw queue */
2057 __dasd_process_block_ccw_queue(block, &final_queue);
2058 spin_unlock(&block->queue_lock);
2059 /* Now call the callback function of requests with final status */
2060 spin_lock_irq(&block->request_queue_lock);
2061 list_for_each_safe(l, n, &final_queue) {
2062 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2063 list_del_init(&cqr->blocklist);
2064 __dasd_cleanup_cqr(cqr);
2065 }
2066 spin_lock(&block->queue_lock);
2067 /* Get new request from the block device request queue */
2068 __dasd_process_request_queue(block);
2069 /* Now check if the head of the ccw queue needs to be started. */
2070 __dasd_block_start_head(block);
2071 spin_unlock(&block->queue_lock);
2072 spin_unlock_irq(&block->request_queue_lock);
2073 dasd_put_device(block->base);
2074}
2075
2076static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2077{
2078 wake_up(&dasd_flush_wq);
2079}
2080
2081/*
2082 * Go through all request on the dasd_block request queue, cancel them
2083 * on the respective dasd_device, and return them to the generic
2084 * block layer.
2085 */
2086static int dasd_flush_block_queue(struct dasd_block *block)
2087{
2088 struct dasd_ccw_req *cqr, *n;
2089 int rc, i;
2090 struct list_head flush_queue;
2091
2092 INIT_LIST_HEAD(&flush_queue);
2093 spin_lock_bh(&block->queue_lock);
2094 rc = 0;
2095restart:
2096 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2097 /* if this request currently owned by a dasd_device cancel it */
2098 if (cqr->status >= DASD_CQR_QUEUED)
2099 rc = dasd_cancel_req(cqr);
2100 if (rc < 0)
2101 break;
2102 /* Rechain request (including erp chain) so it won't be
2103 * touched by the dasd_block_tasklet anymore.
2104 * Replace the callback so we notice when the request
2105 * is returned from the dasd_device layer.
2106 */
2107 cqr->callback = _dasd_wake_block_flush_cb;
2108 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2109 list_move_tail(&cqr->blocklist, &flush_queue);
2110 if (i > 1)
2111 /* moved more than one request - need to restart */
2112 goto restart;
2113 }
2114 spin_unlock_bh(&block->queue_lock);
2115 /* Now call the callback function of flushed requests */
2116restart_cb:
2117 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2118 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2119 /* Process finished ERP request. */
2120 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002121 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002122 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002123 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002124 /* restart list_for_xx loop since dasd_process_erp
2125 * might remove multiple elements */
2126 goto restart_cb;
2127 }
2128 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002129 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002130 cqr->endclk = get_clock();
2131 list_del_init(&cqr->blocklist);
2132 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002133 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return rc;
2136}
2137
2138/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002139 * Schedules a call to dasd_tasklet over the device tasklet.
2140 */
2141void dasd_schedule_block_bh(struct dasd_block *block)
2142{
2143 /* Protect against rescheduling. */
2144 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2145 return;
2146 /* life cycle of block is bound to it's base device */
2147 dasd_get_device(block->base);
2148 tasklet_hi_schedule(&block->tasklet);
2149}
2150
2151
2152/*
2153 * SECTION: external block device operations
2154 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
2156
2157/*
2158 * Dasd request queue function. Called from ll_rw_blk.c
2159 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002160static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002162 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002164 block = queue->queuedata;
2165 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002167 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002169 __dasd_block_start_head(block);
2170 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173/*
2174 * Allocate and initialize request queue and default I/O scheduler.
2175 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002176static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 int rc;
2179
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002180 block->request_queue = blk_init_queue(do_dasd_request,
2181 &block->request_queue_lock);
2182 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -ENOMEM;
2184
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002185 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002187 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002188 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002189 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return rc;
2193 }
2194 return 0;
2195}
2196
2197/*
2198 * Allocate and initialize request queue.
2199 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002200static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 int max;
2203
Martin K. Petersene1defc42009-05-22 17:17:49 -04002204 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002205 max = block->base->discipline->max_blocks << block->s2b_shift;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002206 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002207 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002208 /* with page sized segments we can translate each segement into
2209 * one idaw/tidaw
2210 */
2211 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2212 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215/*
2216 * Deactivate and free request queue.
2217 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002218static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002220 if (block->request_queue) {
2221 blk_cleanup_queue(block->request_queue);
2222 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224}
2225
2226/*
2227 * Flush request on the request queue.
2228 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002229static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct request *req;
2232
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002233 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002235
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002236 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002237 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002238 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002239 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Al Viro57a7c0b2008-03-02 10:36:08 -05002242static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Al Viro57a7c0b2008-03-02 10:36:08 -05002244 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Haberland9eb25122010-02-26 22:37:46 +01002245 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 int rc;
2247
Stefan Haberland9eb25122010-02-26 22:37:46 +01002248 if (!block)
2249 return -ENODEV;
2250
2251 base = block->base;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002252 atomic_inc(&block->open_count);
2253 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 rc = -ENODEV;
2255 goto unlock;
2256 }
2257
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002258 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 rc = -EINVAL;
2260 goto unlock;
2261 }
2262
2263 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002264 dev_info(&base->cdev->dev,
2265 "Accessing the DASD failed because it is in "
2266 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 rc = -EPERM;
2268 goto out;
2269 }
2270
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002271 if (base->state <= DASD_STATE_BASIC) {
2272 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 " Cannot open unrecognized device");
2274 rc = -ENODEV;
2275 goto out;
2276 }
2277
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002278 if ((mode & FMODE_WRITE) &&
2279 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2280 (base->features & DASD_FEATURE_READONLY))) {
2281 rc = -EROFS;
2282 goto out;
2283 }
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return 0;
2286
2287out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002288 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002290 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return rc;
2292}
2293
Al Viro57a7c0b2008-03-02 10:36:08 -05002294static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002298 atomic_dec(&block->open_count);
2299 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
2301}
2302
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002303/*
2304 * Return disk geometry.
2305 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002306static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002307{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002308 struct dasd_block *block;
2309 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002310
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002311 block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002312 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002313 return -ENODEV;
Julia Lawallcf05b822009-08-23 18:09:05 +02002314 base = block->base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002315
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002316 if (!base->discipline ||
2317 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002318 return -EINVAL;
2319
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002320 base->discipline->fill_geometry(block, geo);
2321 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002322 return 0;
2323}
2324
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002325const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326dasd_device_operations = {
2327 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002328 .open = dasd_open,
2329 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002330 .ioctl = dasd_ioctl,
2331 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002332 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333};
2334
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335/*******************************************************************************
2336 * end of block device operations
2337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339static void
2340dasd_exit(void)
2341{
2342#ifdef CONFIG_PROC_FS
2343 dasd_proc_exit();
2344#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002345 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002346 if (dasd_page_cache != NULL) {
2347 kmem_cache_destroy(dasd_page_cache);
2348 dasd_page_cache = NULL;
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 dasd_gendisk_exit();
2351 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (dasd_debug_area != NULL) {
2353 debug_unregister(dasd_debug_area);
2354 dasd_debug_area = NULL;
2355 }
2356}
2357
2358/*
2359 * SECTION: common functions for ccw_driver use
2360 */
2361
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002362/*
2363 * Is the device read-only?
2364 * Note that this function does not report the setting of the
2365 * readonly device attribute, but how it is configured in z/VM.
2366 */
2367int dasd_device_is_ro(struct dasd_device *device)
2368{
2369 struct ccw_dev_id dev_id;
2370 struct diag210 diag_data;
2371 int rc;
2372
2373 if (!MACHINE_IS_VM)
2374 return 0;
2375 ccw_device_get_id(device->cdev, &dev_id);
2376 memset(&diag_data, 0, sizeof(diag_data));
2377 diag_data.vrdcdvno = dev_id.devno;
2378 diag_data.vrdclen = sizeof(diag_data);
2379 rc = diag210(&diag_data);
2380 if (rc == 0 || rc == 2) {
2381 return diag_data.vrdcvfla & 0x80;
2382 } else {
2383 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2384 dev_id.devno, rc);
2385 return 0;
2386 }
2387}
2388EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2389
Cornelia Huckf3445a12009-04-14 15:36:23 +02002390static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2391{
2392 struct ccw_device *cdev = data;
2393 int ret;
2394
2395 ret = ccw_device_set_online(cdev);
2396 if (ret)
2397 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2398 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002399}
2400
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002401/*
2402 * Initial attempt at a probe function. this can be simplified once
2403 * the other detection code is gone.
2404 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002405int dasd_generic_probe(struct ccw_device *cdev,
2406 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
2408 int ret;
2409
2410 ret = dasd_add_sysfs_files(cdev);
2411 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002412 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2413 "dasd_generic_probe: could not add "
2414 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
Horst Hummel40545572006-06-29 15:08:18 +02002417 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Horst Hummel40545572006-06-29 15:08:18 +02002419 /*
2420 * Automatically online either all dasd devices (dasd_autodetect)
2421 * or all devices specified with dasd= parameters during
2422 * initial probe.
2423 */
2424 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002425 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002426 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002430/*
2431 * This will one day be called from a global not_oper handler.
2432 * It is also used by driver_unregister during module unload.
2433 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002434void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002437 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Horst Hummel59afda72005-05-16 21:53:39 -07002439 cdev->handler = NULL;
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 dasd_remove_sysfs_files(cdev);
2442 device = dasd_device_from_cdev(cdev);
2443 if (IS_ERR(device))
2444 return;
2445 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2446 /* Already doing offline processing */
2447 dasd_put_device(device);
2448 return;
2449 }
2450 /*
2451 * This device is removed unconditionally. Set offline
2452 * flag to prevent dasd_open from opening it while it is
2453 * no quite down yet.
2454 */
2455 dasd_set_target_state(device, DASD_STATE_NEW);
2456 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002457 block = device->block;
2458 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002460 /*
2461 * life cycle of block is bound to device, so delete it after
2462 * device was safely removed
2463 */
2464 if (block)
2465 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002468/*
2469 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002471 * or the user has started activation through sysfs.
2472 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002473int dasd_generic_set_online(struct ccw_device *cdev,
2474 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002476 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002478 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002479
Horst Hummel40545572006-06-29 15:08:18 +02002480 /* first online clears initial online feature flag */
2481 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 device = dasd_create_device(cdev);
2483 if (IS_ERR(device))
2484 return PTR_ERR(device);
2485
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002486 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002487 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002489 pr_warning("%s Setting the DASD online failed because "
2490 "of missing DIAG discipline\n",
2491 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 dasd_delete_device(device);
2493 return -ENODEV;
2494 }
2495 discipline = dasd_diag_discipline_pointer;
2496 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002497 if (!try_module_get(base_discipline->owner)) {
2498 dasd_delete_device(device);
2499 return -EINVAL;
2500 }
2501 if (!try_module_get(discipline->owner)) {
2502 module_put(base_discipline->owner);
2503 dasd_delete_device(device);
2504 return -EINVAL;
2505 }
2506 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 device->discipline = discipline;
2508
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002509 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 rc = discipline->check_device(device);
2511 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002512 pr_warning("%s Setting the DASD online with discipline %s "
2513 "failed with rc=%i\n",
2514 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002515 module_put(discipline->owner);
2516 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 dasd_delete_device(device);
2518 return rc;
2519 }
2520
2521 dasd_set_target_state(device, DASD_STATE_ONLINE);
2522 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002523 pr_warning("%s Setting the DASD online failed because of a "
2524 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 rc = -ENODEV;
2526 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002527 if (device->block)
2528 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dasd_delete_device(device);
2530 } else
2531 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002532 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01002533
2534 wait_event(dasd_init_waitq, _wait_for_device(device));
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return rc;
2538}
2539
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002540int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002543 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002544 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 device = dasd_device_from_cdev(cdev);
2547 if (IS_ERR(device))
2548 return PTR_ERR(device);
2549 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2550 /* Already doing offline processing */
2551 dasd_put_device(device);
2552 return 0;
2553 }
2554 /*
2555 * We must make sure that this device is currently not in use.
2556 * The open_count is increased for every opener, that includes
2557 * the blkdev_get in dasd_scan_partitions. We are only interested
2558 * in the other openers.
2559 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002560 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002561 max_count = device->block->bdev ? 0 : -1;
2562 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002563 if (open_count > max_count) {
2564 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002565 pr_warning("%s: The DASD cannot be set offline "
2566 "with open count %i\n",
2567 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002568 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002569 pr_warning("%s: The DASD cannot be set offline "
2570 "while it is in use\n",
2571 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002572 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2573 dasd_put_device(device);
2574 return -EBUSY;
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 dasd_set_target_state(device, DASD_STATE_NEW);
2578 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002579 block = device->block;
2580 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002582 /*
2583 * life cycle of block is bound to device, so delete it after
2584 * device was safely removed
2585 */
2586 if (block)
2587 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 0;
2589}
2590
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002591int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct dasd_device *device;
2594 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int ret;
2596
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002597 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (IS_ERR(device))
2599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 ret = 0;
2601 switch (event) {
2602 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002603 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002605 /* First of all call extended error reporting. */
2606 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (device->state < DASD_STATE_BASIC)
2609 break;
2610 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002611 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2612 if (cqr->status == DASD_CQR_IN_IO) {
2613 cqr->status = DASD_CQR_QUEUED;
2614 cqr->retries++;
2615 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002616 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002617 dasd_device_clear_timer(device);
2618 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ret = 1;
2620 break;
2621 case CIO_OPER:
2622 /* FIXME: add a sanity check. */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002623 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002624 if (device->stopped & DASD_UNRESUMED_PM) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002625 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002626 dasd_restore_device(device);
2627 ret = 1;
2628 break;
2629 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002630 dasd_schedule_device_bh(device);
2631 if (device->block)
2632 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 ret = 1;
2634 break;
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 dasd_put_device(device);
2637 return ret;
2638}
2639
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002640int dasd_generic_pm_freeze(struct ccw_device *cdev)
2641{
2642 struct dasd_ccw_req *cqr, *n;
2643 int rc;
2644 struct list_head freeze_queue;
2645 struct dasd_device *device = dasd_device_from_cdev(cdev);
2646
2647 if (IS_ERR(device))
2648 return PTR_ERR(device);
2649 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002650 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002651 /* clear active requests */
2652 INIT_LIST_HEAD(&freeze_queue);
2653 spin_lock_irq(get_ccwdev_lock(cdev));
2654 rc = 0;
2655 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2656 /* Check status and move request to flush_queue */
2657 if (cqr->status == DASD_CQR_IN_IO) {
2658 rc = device->discipline->term_IO(cqr);
2659 if (rc) {
2660 /* unable to terminate requeust */
2661 dev_err(&device->cdev->dev,
2662 "Unable to terminate request %p "
2663 "on suspend\n", cqr);
2664 spin_unlock_irq(get_ccwdev_lock(cdev));
2665 dasd_put_device(device);
2666 return rc;
2667 }
2668 }
2669 list_move_tail(&cqr->devlist, &freeze_queue);
2670 }
2671
2672 spin_unlock_irq(get_ccwdev_lock(cdev));
2673
2674 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2675 wait_event(dasd_flush_wq,
2676 (cqr->status != DASD_CQR_CLEAR_PENDING));
2677 if (cqr->status == DASD_CQR_CLEARED)
2678 cqr->status = DASD_CQR_QUEUED;
2679 }
2680 /* move freeze_queue to start of the ccw_queue */
2681 spin_lock_irq(get_ccwdev_lock(cdev));
2682 list_splice_tail(&freeze_queue, &device->ccw_queue);
2683 spin_unlock_irq(get_ccwdev_lock(cdev));
2684
2685 if (device->discipline->freeze)
2686 rc = device->discipline->freeze(device);
2687
2688 dasd_put_device(device);
2689 return rc;
2690}
2691EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2692
2693int dasd_generic_restore_device(struct ccw_device *cdev)
2694{
2695 struct dasd_device *device = dasd_device_from_cdev(cdev);
2696 int rc = 0;
2697
2698 if (IS_ERR(device))
2699 return PTR_ERR(device);
2700
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002701 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002702 dasd_device_remove_stop_bits(device,
2703 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002704
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002705 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002706
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002707 /*
2708 * call discipline restore function
2709 * if device is stopped do nothing e.g. for disconnected devices
2710 */
2711 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002712 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002713 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002714 /*
2715 * if the resume failed for the DASD we put it in
2716 * an UNRESUMED stop state
2717 */
2718 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002719
Stefan Haberland6fca97a2009-10-06 10:34:15 +02002720 if (device->block)
2721 dasd_schedule_block_bh(device->block);
2722
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002723 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002724 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002725}
2726EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2727
Heiko Carstens763968e2007-05-10 15:45:46 +02002728static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2729 void *rdc_buffer,
2730 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02002731 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002732{
2733 struct dasd_ccw_req *cqr;
2734 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002735 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02002736
2737 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2738
2739 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002740 /* internal error 13 - Allocating the RDC request failed*/
2741 dev_err(&device->cdev->dev,
2742 "An error occurred in the DASD device driver, "
2743 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002744 return cqr;
2745 }
2746
2747 ccw = cqr->cpaddr;
2748 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002749 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2750 idaw = (unsigned long *) (cqr->data);
2751 ccw->cda = (__u32)(addr_t) idaw;
2752 ccw->flags = CCW_FLAG_IDA;
2753 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2754 } else {
2755 ccw->cda = (__u32)(addr_t) rdc_buffer;
2756 ccw->flags = 0;
2757 }
Cornelia Huck17283b52007-05-04 18:47:51 +02002758
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002759 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002760 cqr->startdev = device;
2761 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002762 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002763 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02002764 cqr->buildclk = get_clock();
2765 cqr->status = DASD_CQR_FILLED;
2766 return cqr;
2767}
2768
2769
Stefan Haberland68b781f2009-09-11 10:28:29 +02002770int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002771 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002772{
2773 int ret;
2774 struct dasd_ccw_req *cqr;
2775
Sebastian Ott92636b12009-06-12 10:26:37 +02002776 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002777 magic);
2778 if (IS_ERR(cqr))
2779 return PTR_ERR(cqr);
2780
2781 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002782 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002783 return ret;
2784}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002785EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002786
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002787/*
2788 * In command mode and transport mode we need to look for sense
2789 * data in different places. The sense data itself is allways
2790 * an array of 32 bytes, so we can unify the sense data access
2791 * for both modes.
2792 */
2793char *dasd_get_sense(struct irb *irb)
2794{
2795 struct tsb *tsb = NULL;
2796 char *sense = NULL;
2797
2798 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2799 if (irb->scsw.tm.tcw)
2800 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2801 irb->scsw.tm.tcw);
2802 if (tsb && tsb->length == 64 && tsb->flags)
2803 switch (tsb->flags & 0x07) {
2804 case 1: /* tsa_iostat */
2805 sense = tsb->tsa.iostat.sense;
2806 break;
2807 case 2: /* tsa_ddpc */
2808 sense = tsb->tsa.ddpc.sense;
2809 break;
2810 default:
2811 /* currently we don't use interrogate data */
2812 break;
2813 }
2814 } else if (irb->esw.esw0.erw.cons) {
2815 sense = irb->ecw;
2816 }
2817 return sense;
2818}
2819EXPORT_SYMBOL_GPL(dasd_get_sense);
2820
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002821static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 int rc;
2824
2825 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002826 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002827 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002830 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (dasd_debug_area == NULL) {
2832 rc = -ENOMEM;
2833 goto failed;
2834 }
2835 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002836 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2839
2840 dasd_diag_discipline_pointer = NULL;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 rc = dasd_devmap_init();
2843 if (rc)
2844 goto failed;
2845 rc = dasd_gendisk_init();
2846 if (rc)
2847 goto failed;
2848 rc = dasd_parse();
2849 if (rc)
2850 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002851 rc = dasd_eer_init();
2852 if (rc)
2853 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854#ifdef CONFIG_PROC_FS
2855 rc = dasd_proc_init();
2856 if (rc)
2857 goto failed;
2858#endif
2859
2860 return 0;
2861failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002862 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 dasd_exit();
2864 return rc;
2865}
2866
2867module_init(dasd_init);
2868module_exit(dasd_exit);
2869
2870EXPORT_SYMBOL(dasd_debug_area);
2871EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2872
2873EXPORT_SYMBOL(dasd_add_request_head);
2874EXPORT_SYMBOL(dasd_add_request_tail);
2875EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002876EXPORT_SYMBOL(dasd_device_clear_timer);
2877EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878EXPORT_SYMBOL(dasd_enable_device);
2879EXPORT_SYMBOL(dasd_int_handler);
2880EXPORT_SYMBOL(dasd_kfree_request);
2881EXPORT_SYMBOL(dasd_kick_device);
2882EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002883EXPORT_SYMBOL(dasd_schedule_device_bh);
2884EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002886EXPORT_SYMBOL(dasd_device_set_timer);
2887EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888EXPORT_SYMBOL(dasd_sfree_request);
2889EXPORT_SYMBOL(dasd_sleep_on);
2890EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2891EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2892EXPORT_SYMBOL(dasd_smalloc_request);
2893EXPORT_SYMBOL(dasd_start_IO);
2894EXPORT_SYMBOL(dasd_term_IO);
2895
2896EXPORT_SYMBOL_GPL(dasd_generic_probe);
2897EXPORT_SYMBOL_GPL(dasd_generic_remove);
2898EXPORT_SYMBOL_GPL(dasd_generic_notify);
2899EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2900EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002901EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2902EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2903EXPORT_SYMBOL_GPL(dasd_alloc_block);
2904EXPORT_SYMBOL_GPL(dasd_free_block);