blob: db9193d389868732af63a64be54b9c6d90f81777 [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>
8 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kmod.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ctype.h>
16#include <linux/major.h>
17#include <linux/slab.h>
18#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/ebcdic.h>
23#include <asm/idals.h>
24#include <asm/todclk.h>
25
26/* This is ugly... */
27#define PRINTK_HEADER "dasd:"
28
29#include "dasd_int.h"
30/*
31 * SECTION: Constant definitions to be used within this file
32 */
33#define DASD_CHANQ_MAX_SIZE 4
34
35/*
36 * SECTION: exported variables of dasd.c
37 */
38debug_info_t *dasd_debug_area;
39struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010040void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
43MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
44 " Copyright 2000 IBM Corporation");
45MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_LICENSE("GPL");
47
48/*
49 * SECTION: prototypes for static functions of dasd.c
50 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010051static int dasd_alloc_queue(struct dasd_block *);
52static void dasd_setup_queue(struct dasd_block *);
53static void dasd_free_queue(struct dasd_block *);
54static void dasd_flush_request_queue(struct dasd_block *);
55static int dasd_flush_block_queue(struct dasd_block *);
56static void dasd_device_tasklet(struct dasd_device *);
57static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000058static void do_kick_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010059static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * SECTION: Operations on the device structure.
63 */
64static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020065static wait_queue_head_t dasd_flush_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * Allocate memory for a new device structure.
69 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010070struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct dasd_device *device;
73
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010074 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
75 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* Get two pages for normal block device operations. */
79 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010080 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 kfree(device);
82 return ERR_PTR(-ENOMEM);
83 }
84 /* Get one page for error recovery. */
85 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010086 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 free_pages((unsigned long) device->ccw_mem, 1);
88 kfree(device);
89 return ERR_PTR(-ENOMEM);
90 }
91
92 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
93 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
94 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010095 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +020096 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010097 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (unsigned long) device);
99 INIT_LIST_HEAD(&device->ccw_queue);
100 init_timer(&device->timer);
Al Viro4927b3f2006-12-06 19:18:20 +0000101 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 device->state = DASD_STATE_NEW;
103 device->target = DASD_STATE_NEW;
104
105 return device;
106}
107
108/*
109 * Free memory of a device structure.
110 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100111void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800113 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 free_page((unsigned long) device->erp_mem);
115 free_pages((unsigned long) device->ccw_mem, 1);
116 kfree(device);
117}
118
119/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100120 * Allocate memory for a new device structure.
121 */
122struct dasd_block *dasd_alloc_block(void)
123{
124 struct dasd_block *block;
125
126 block = kzalloc(sizeof(*block), GFP_ATOMIC);
127 if (!block)
128 return ERR_PTR(-ENOMEM);
129 /* open_count = 0 means device online but not in use */
130 atomic_set(&block->open_count, -1);
131
132 spin_lock_init(&block->request_queue_lock);
133 atomic_set(&block->tasklet_scheduled, 0);
134 tasklet_init(&block->tasklet,
135 (void (*)(unsigned long)) dasd_block_tasklet,
136 (unsigned long) block);
137 INIT_LIST_HEAD(&block->ccw_queue);
138 spin_lock_init(&block->queue_lock);
139 init_timer(&block->timer);
140
141 return block;
142}
143
144/*
145 * Free memory of a device structure.
146 */
147void dasd_free_block(struct dasd_block *block)
148{
149 kfree(block);
150}
151
152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Make a new device known to the system.
154 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100155static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int rc;
158
159 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200160 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * keep the reference count > 0.
162 */
163 dasd_get_device(device);
164
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100165 if (device->block) {
166 rc = dasd_alloc_queue(device->block);
167 if (rc) {
168 dasd_put_device(device);
169 return rc;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 device->state = DASD_STATE_KNOWN;
173 return 0;
174}
175
176/*
177 * Let the system forget about a device.
178 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100179static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800181 /* Disable extended error reporting for this device. */
182 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100184 if (device->discipline) {
185 if (device->discipline->uncheck_device)
186 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800187 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800190 if (device->base_discipline)
191 module_put(device->base_discipline->owner);
192 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 device->state = DASD_STATE_NEW;
194
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195 if (device->block)
196 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Give up reference we took in dasd_state_new_to_known. */
199 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * Request the irq line for the device.
205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int rc;
209
210 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100211 if (device->block) {
212 rc = dasd_gendisk_alloc(device->block);
213 if (rc)
214 return rc;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700217 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100218 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200220 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
222
223 device->state = DASD_STATE_BASIC;
224 return 0;
225}
226
227/*
228 * Release the irq line for the device. Terminate any running i/o.
229 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100230static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Horst Hummel8f617012006-08-30 14:33:33 +0200232 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100233 if (device->block) {
234 dasd_gendisk_free(device->block);
235 dasd_block_clear_timer(device->block);
236 }
237 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200238 if (rc)
239 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100240 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
243 if (device->debug_area != NULL) {
244 debug_unregister(device->debug_area);
245 device->debug_area = NULL;
246 }
247 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * Do the initial analysis. The do_analysis function may return
253 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
254 * until the discipline decides to continue the startup sequence
255 * by calling the function dasd_change_state. The eckd disciplines
256 * uses this to start a ccw that detects the format. The completion
257 * interrupt for this detection ccw uses the kernel event daemon to
258 * trigger the call to dasd_change_state. All this is done in the
259 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800260 * After the analysis ccw is done (do_analysis returned 0) the block
261 * device is setup.
262 * In case the analysis returns an error, the device setup is stopped
263 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100265static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100268 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100271 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800272 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100273 if (block) {
274 if (block->base->discipline->do_analysis != NULL)
275 rc = block->base->discipline->do_analysis(block);
276 if (rc) {
277 if (rc != -EAGAIN)
278 device->state = DASD_STATE_UNFMT;
279 return rc;
280 }
281 dasd_setup_queue(block);
282 set_capacity(block->gdp,
283 block->blocks << block->s2b_shift);
284 device->state = DASD_STATE_READY;
285 rc = dasd_scan_partitions(block);
286 if (rc)
287 device->state = DASD_STATE_BASIC;
288 } else {
289 device->state = DASD_STATE_READY;
290 }
Horst Hummel90f00942006-03-07 21:55:39 -0800291 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Remove device from block device layer. Destroy dirty buffers.
296 * Forget format information. Check if the target level is basic
297 * and if it is create fake disk for formatting.
298 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100299static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Horst Hummel8f617012006-08-30 14:33:33 +0200301 int rc;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100304 if (device->block) {
305 struct dasd_block *block = device->block;
306 rc = dasd_flush_block_queue(block);
307 if (rc) {
308 device->state = DASD_STATE_READY;
309 return rc;
310 }
311 dasd_destroy_partitions(block);
312 dasd_flush_request_queue(block);
313 block->blocks = 0;
314 block->bp_block = 0;
315 block->s2b_shift = 0;
316 }
Horst Hummel8f617012006-08-30 14:33:33 +0200317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/*
Horst Hummel90f00942006-03-07 21:55:39 -0800321 * Back to basic.
322 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100323static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800324{
325 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200326 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800327}
328
329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * Make the device online and schedule the bottom half to start
331 * the requeueing of requests from the linux request queue to the
332 * ccw queue.
333 */
Horst Hummel8f617012006-08-30 14:33:33 +0200334static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335dasd_state_ready_to_online(struct dasd_device * device)
336{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100337 int rc;
338
339 if (device->discipline->ready_to_online) {
340 rc = device->discipline->ready_to_online(device);
341 if (rc)
342 return rc;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 if (device->block)
346 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
350/*
351 * Stop the requeueing of requests again.
352 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100355 int rc;
356
357 if (device->discipline->online_to_ready) {
358 rc = device->discipline->online_to_ready(device);
359 if (rc)
360 return rc;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 device->state = DASD_STATE_READY;
Horst Hummel8f617012006-08-30 14:33:33 +0200363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
367 * Device startup state changes.
368 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100369static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 int rc;
372
373 rc = 0;
374 if (device->state == DASD_STATE_NEW &&
375 device->target >= DASD_STATE_KNOWN)
376 rc = dasd_state_new_to_known(device);
377
378 if (!rc &&
379 device->state == DASD_STATE_KNOWN &&
380 device->target >= DASD_STATE_BASIC)
381 rc = dasd_state_known_to_basic(device);
382
383 if (!rc &&
384 device->state == DASD_STATE_BASIC &&
385 device->target >= DASD_STATE_READY)
386 rc = dasd_state_basic_to_ready(device);
387
388 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700389 device->state == DASD_STATE_UNFMT &&
390 device->target > DASD_STATE_UNFMT)
391 rc = -EPERM;
392
393 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 device->state == DASD_STATE_READY &&
395 device->target >= DASD_STATE_ONLINE)
396 rc = dasd_state_ready_to_online(device);
397
398 return rc;
399}
400
401/*
402 * Device shutdown state changes.
403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Horst Hummel8f617012006-08-30 14:33:33 +0200406 int rc;
407
408 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (device->state == DASD_STATE_ONLINE &&
410 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200411 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200412
Horst Hummel8f617012006-08-30 14:33:33 +0200413 if (!rc &&
414 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200416 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800417
Horst Hummel8f617012006-08-30 14:33:33 +0200418 if (!rc &&
419 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800420 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200421 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800422
Horst Hummel8f617012006-08-30 14:33:33 +0200423 if (!rc &&
424 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200426 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200427
Horst Hummel8f617012006-08-30 14:33:33 +0200428 if (!rc &&
429 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200431 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Horst Hummel8f617012006-08-30 14:33:33 +0200433 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * This is the main startup/shutdown routine.
438 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100439static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 int rc;
442
443 if (device->state == device->target)
444 /* Already where we want to go today... */
445 return;
446 if (device->state < device->target)
447 rc = dasd_increase_state(device);
448 else
449 rc = dasd_decrease_state(device);
450 if (rc && rc != -EAGAIN)
451 device->target = device->state;
452
453 if (device->state == device->target)
454 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200455
456 /* let user-space know that the device status changed */
457 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Kick starter for devices that did not complete the startup/shutdown
462 * procedure or were sleeping because of a pending state.
463 * dasd_kick_device will schedule a call do do_kick_device to the kernel
464 * event daemon.
465 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100466static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Al Viro4927b3f2006-12-06 19:18:20 +0000468 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100470 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dasd_put_device(device);
472}
473
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100474void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 dasd_get_device(device);
477 /* queue call to dasd_kick_device to the kernel event daemon. */
478 schedule_work(&device->kick_work);
479}
480
481/*
482 * Set the target state for a device and starts the state change.
483 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100484void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 /* If we are in probeonly mode stop at DASD_STATE_READY. */
487 if (dasd_probeonly && target > DASD_STATE_READY)
488 target = DASD_STATE_READY;
489 if (device->target != target) {
490 if (device->state == target)
491 wake_up(&dasd_init_waitq);
492 device->target = target;
493 }
494 if (device->state != device->target)
495 dasd_change_state(device);
496}
497
498/*
499 * Enable devices with device numbers in [from..to].
500 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100501static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 return (device->state == device->target);
504}
505
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100506void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 dasd_set_target_state(device, DASD_STATE_ONLINE);
509 if (device->state <= DASD_STATE_KNOWN)
510 /* No discipline for device found. */
511 dasd_set_target_state(device, DASD_STATE_NEW);
512 /* Now wait for the devices to come up. */
513 wait_event(dasd_init_waitq, _wait_for_device(device));
514}
515
516/*
517 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
518 */
519#ifdef CONFIG_DASD_PROFILE
520
521struct dasd_profile_info_t dasd_global_profile;
522unsigned int dasd_profile_level = DASD_PROFILE_OFF;
523
524/*
525 * Increments counter in global and local profiling structures.
526 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100527#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{ \
529 int index; \
530 for (index = 0; index < 31 && value >> (2+index); index++); \
531 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100532 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * Add profiling information for cqr before execution.
537 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100538static void dasd_profile_start(struct dasd_block *block,
539 struct dasd_ccw_req *cqr,
540 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct list_head *l;
543 unsigned int counter;
544
545 if (dasd_profile_level != DASD_PROFILE_ON)
546 return;
547
548 /* count the length of the chanq for statistics */
549 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100550 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (++counter >= 31)
552 break;
553 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100554 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
558 * Add profiling information for cqr after execution.
559 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100560static void dasd_profile_end(struct dasd_block *block,
561 struct dasd_ccw_req *cqr,
562 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 long strtime, irqtime, endtime, tottime; /* in microseconds */
565 long tottimeps, sectors;
566
567 if (dasd_profile_level != DASD_PROFILE_ON)
568 return;
569
570 sectors = req->nr_sectors;
571 if (!cqr->buildclk || !cqr->startclk ||
572 !cqr->stopclk || !cqr->endclk ||
573 !sectors)
574 return;
575
576 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
577 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
578 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
579 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
580 tottimeps = tottime / sectors;
581
582 if (!dasd_global_profile.dasd_io_reqs)
583 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100584 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 dasd_global_profile.dasd_io_reqs++;
586 dasd_global_profile.dasd_io_sects += sectors;
587
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100588 if (!block->profile.dasd_io_reqs)
589 memset(&block->profile, 0,
590 sizeof(struct dasd_profile_info_t));
591 block->profile.dasd_io_reqs++;
592 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100594 dasd_profile_counter(sectors, dasd_io_secs, block);
595 dasd_profile_counter(tottime, dasd_io_times, block);
596 dasd_profile_counter(tottimeps, dasd_io_timps, block);
597 dasd_profile_counter(strtime, dasd_io_time1, block);
598 dasd_profile_counter(irqtime, dasd_io_time2, block);
599 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
600 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100603#define dasd_profile_start(block, cqr, req) do {} while (0)
604#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#endif /* CONFIG_DASD_PROFILE */
606
607/*
608 * Allocate memory for a channel program with 'cplength' channel
609 * command words and 'datasize' additional space. There are two
610 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
611 * memory and 2) dasd_smalloc_request uses the static ccw memory
612 * that gets allocated for each device.
613 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100614struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
615 int datasize,
616 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct dasd_ccw_req *cqr;
619
620 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100621 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
622 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800624 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (cqr == NULL)
626 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 cqr->cpaddr = NULL;
628 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800629 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 GFP_ATOMIC | GFP_DMA);
631 if (cqr->cpaddr == NULL) {
632 kfree(cqr);
633 return ERR_PTR(-ENOMEM);
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 cqr->data = NULL;
637 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800638 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800640 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree(cqr);
642 return ERR_PTR(-ENOMEM);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 strncpy((char *) &cqr->magic, magic, 4);
646 ASCEBC((char *) &cqr->magic, 4);
647 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
648 dasd_get_device(device);
649 return cqr;
650}
651
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100652struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
653 int datasize,
654 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 unsigned long flags;
657 struct dasd_ccw_req *cqr;
658 char *data;
659 int size;
660
661 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100662 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
663 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
666 if (cplength > 0)
667 size += cplength * sizeof(struct ccw1);
668 if (datasize > 0)
669 size += datasize;
670 spin_lock_irqsave(&device->mem_lock, flags);
671 cqr = (struct dasd_ccw_req *)
672 dasd_alloc_chunk(&device->ccw_chunks, size);
673 spin_unlock_irqrestore(&device->mem_lock, flags);
674 if (cqr == NULL)
675 return ERR_PTR(-ENOMEM);
676 memset(cqr, 0, sizeof(struct dasd_ccw_req));
677 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
678 cqr->cpaddr = NULL;
679 if (cplength > 0) {
680 cqr->cpaddr = (struct ccw1 *) data;
681 data += cplength*sizeof(struct ccw1);
682 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
683 }
684 cqr->data = NULL;
685 if (datasize > 0) {
686 cqr->data = data;
687 memset(cqr->data, 0, datasize);
688 }
689 strncpy((char *) &cqr->magic, magic, 4);
690 ASCEBC((char *) &cqr->magic, 4);
691 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
692 dasd_get_device(device);
693 return cqr;
694}
695
696/*
697 * Free memory of a channel program. This function needs to free all the
698 * idal lists that might have been created by dasd_set_cda and the
699 * struct dasd_ccw_req itself.
700 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100701void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800703#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct ccw1 *ccw;
705
706 /* Clear any idals used for the request. */
707 ccw = cqr->cpaddr;
708 do {
709 clear_normalized_cda(ccw);
710 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
711#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800712 kfree(cqr->cpaddr);
713 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(cqr);
715 dasd_put_device(device);
716}
717
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100718void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 unsigned long flags;
721
722 spin_lock_irqsave(&device->mem_lock, flags);
723 dasd_free_chunk(&device->ccw_chunks, cqr);
724 spin_unlock_irqrestore(&device->mem_lock, flags);
725 dasd_put_device(device);
726}
727
728/*
729 * Check discipline magic in cqr.
730 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100731static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct dasd_device *device;
734
735 if (cqr == NULL)
736 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100737 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
739 DEV_MESSAGE(KERN_WARNING, device,
740 " dasd_ccw_req 0x%08x magic doesn't match"
741 " discipline 0x%08x",
742 cqr->magic,
743 *(unsigned int *) device->discipline->name);
744 return -EINVAL;
745 }
746 return 0;
747}
748
749/*
750 * Terminate the current i/o and set the request to clear_pending.
751 * Timer keeps device runnig.
752 * ccw_device_clear can fail if the i/o subsystem
753 * is in a bad mood.
754 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100755int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct dasd_device *device;
758 int retries, rc;
759
760 /* Check the cqr */
761 rc = dasd_check_cqr(cqr);
762 if (rc)
763 return rc;
764 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100765 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
767 rc = ccw_device_clear(device->cdev, (long) cqr);
768 switch (rc) {
769 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800770 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100771 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200773 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 DBF_DEV_EVENT(DBF_DEBUG, device,
775 "terminate cqr %p successful",
776 cqr);
777 break;
778 case -ENODEV:
779 DBF_DEV_EVENT(DBF_ERR, device, "%s",
780 "device gone, retry");
781 break;
782 case -EIO:
783 DBF_DEV_EVENT(DBF_ERR, device, "%s",
784 "I/O error, retry");
785 break;
786 case -EINVAL:
787 case -EBUSY:
788 DBF_DEV_EVENT(DBF_ERR, device, "%s",
789 "device busy, retry later");
790 break;
791 default:
792 DEV_MESSAGE(KERN_ERR, device,
793 "line %d unknown RC=%d, please "
794 "report to linux390@de.ibm.com",
795 __LINE__, rc);
796 BUG();
797 break;
798 }
799 retries++;
800 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100801 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return rc;
803}
804
805/*
806 * Start the i/o. This start_IO can fail if the channel is really busy.
807 * In that case set up a timer to start the request later.
808 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100809int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct dasd_device *device;
812 int rc;
813
814 /* Check the cqr */
815 rc = dasd_check_cqr(cqr);
816 if (rc)
817 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100818 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (cqr->retries < 0) {
820 DEV_MESSAGE(KERN_DEBUG, device,
821 "start_IO: request %p (%02x/%i) - no retry left.",
822 cqr, cqr->status, cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100823 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EIO;
825 }
826 cqr->startclk = get_clock();
827 cqr->starttime = jiffies;
828 cqr->retries--;
829 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
830 cqr->lpm, 0);
831 switch (rc) {
832 case 0:
833 cqr->status = DASD_CQR_IN_IO;
834 DBF_DEV_EVENT(DBF_DEBUG, device,
835 "start_IO: request %p started successful",
836 cqr);
837 break;
838 case -EBUSY:
839 DBF_DEV_EVENT(DBF_ERR, device, "%s",
840 "start_IO: device busy, retry later");
841 break;
842 case -ETIMEDOUT:
843 DBF_DEV_EVENT(DBF_ERR, device, "%s",
844 "start_IO: request timeout, retry later");
845 break;
846 case -EACCES:
847 /* -EACCES indicates that the request used only a
848 * subset of the available pathes and all these
849 * pathes are gone.
850 * Do a retry with all available pathes.
851 */
852 cqr->lpm = LPM_ANYPATH;
853 DBF_DEV_EVENT(DBF_ERR, device, "%s",
854 "start_IO: selected pathes gone,"
855 " retry on all pathes");
856 break;
857 case -ENODEV:
858 case -EIO:
859 DBF_DEV_EVENT(DBF_ERR, device, "%s",
860 "start_IO: device gone, retry");
861 break;
862 default:
863 DEV_MESSAGE(KERN_ERR, device,
864 "line %d unknown RC=%d, please report"
865 " to linux390@de.ibm.com", __LINE__, rc);
866 BUG();
867 break;
868 }
869 return rc;
870}
871
872/*
873 * Timeout function for dasd devices. This is used for different purposes
874 * 1) missing interrupt handler for normal operation
875 * 2) delayed start of request where start_IO failed with -EBUSY
876 * 3) timeout for missing state change interrupts
877 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
878 * DASD_CQR_QUEUED for 2) and 3).
879 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100880static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 unsigned long flags;
883 struct dasd_device *device;
884
885 device = (struct dasd_device *) ptr;
886 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
887 /* re-activate request queue */
888 device->stopped &= ~DASD_STOPPED_PENDING;
889 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100890 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * Setup timeout for a device in jiffies.
895 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100896void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 if (expires == 0) {
899 if (timer_pending(&device->timer))
900 del_timer(&device->timer);
901 return;
902 }
903 if (timer_pending(&device->timer)) {
904 if (mod_timer(&device->timer, jiffies + expires))
905 return;
906 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100907 device->timer.function = dasd_device_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 device->timer.data = (unsigned long) device;
909 device->timer.expires = jiffies + expires;
910 add_timer(&device->timer);
911}
912
913/*
914 * Clear timeout for a device.
915 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100916void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (timer_pending(&device->timer))
919 del_timer(&device->timer);
920}
921
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100922static void dasd_handle_killed_request(struct ccw_device *cdev,
923 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct dasd_ccw_req *cqr;
926 struct dasd_device *device;
927
928 cqr = (struct dasd_ccw_req *) intparm;
929 if (cqr->status != DASD_CQR_IN_IO) {
930 MESSAGE(KERN_DEBUG,
931 "invalid status in handle_killed_request: "
932 "bus_id %s, status %02x",
933 cdev->dev.bus_id, cqr->status);
934 return;
935 }
936
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100937 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200939 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
941 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
942 cdev->dev.bus_id);
943 return;
944 }
945
946 /* Schedule request to be retried. */
947 cqr->status = DASD_CQR_QUEUED;
948
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100949 dasd_device_clear_timer(device);
950 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 dasd_put_device(device);
952}
953
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100954void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800956 /* First of all start sense subsystem status request. */
957 dasd_eer_snss(device);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100960 dasd_schedule_device_bh(device);
961 if (device->block)
962 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*
966 * Interrupt handler for "normal" ssch-io based dasd devices.
967 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100968void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
969 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct dasd_ccw_req *cqr, *next;
972 struct dasd_device *device;
973 unsigned long long now;
974 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (IS_ERR(irb)) {
977 switch (PTR_ERR(irb)) {
978 case -EIO:
979 dasd_handle_killed_request(cdev, intparm);
980 break;
981 case -ETIMEDOUT:
982 printk(KERN_WARNING"%s(%s): request timed out\n",
983 __FUNCTION__, cdev->dev.bus_id);
984 //FIXME - dasd uses own timeout interface...
985 break;
986 default:
987 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
988 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
989 }
990 return;
991 }
992
993 now = get_clock();
994
995 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
996 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
997 (unsigned int) intparm);
998
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100999 /* check for unsolicited interrupts */
1000 cqr = (struct dasd_ccw_req *) intparm;
1001 if (!cqr || ((irb->scsw.cc == 1) &&
1002 (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1003 (irb->scsw.stctl & SCSW_STCTL_STATUS_PEND)) ) {
1004 if (cqr && cqr->status == DASD_CQR_IN_IO)
1005 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001006 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001008 dasd_device_clear_timer(device);
1009 device->discipline->handle_unsolicited_interrupt(device,
1010 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dasd_put_device(device);
1012 }
1013 return;
1014 }
1015
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001016 device = (struct dasd_device *) cqr->startdev;
1017 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1019 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
1020 cdev->dev.bus_id);
1021 return;
1022 }
1023
1024 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001025 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001027 cqr->status = DASD_CQR_CLEARED;
1028 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001029 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001030 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032 }
1033
1034 /* check status - the request might have been killed by dyn detach */
1035 if (cqr->status != DASD_CQR_IN_IO) {
1036 MESSAGE(KERN_DEBUG,
1037 "invalid status: bus_id %s, status %02x",
1038 cdev->dev.bus_id, cqr->status);
1039 return;
1040 }
1041 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
1042 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001043 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 expires = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001045 if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1046 irb->scsw.cstat == 0 && !irb->esw.esw0.erw.cons) {
1047 /* request was completed successfully */
1048 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 cqr->stopclk = now;
1050 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001051 if (cqr->devlist.next != &device->ccw_queue) {
1052 next = list_entry(cqr->devlist.next,
1053 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001055 } else { /* error */
1056 memcpy(&cqr->irb, irb, sizeof(struct irb));
Horst Hummel9575bf22006-12-08 15:54:15 +01001057 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001058 dasd_log_sense(cqr, irb);
1059 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001060 /* If we have no sense data, or we just don't want complex ERP
1061 * for this request, but if we have retries left, then just
1062 * reset this request and retry it in the fastpath
1063 */
1064 if (!(cqr->irb.esw.esw0.erw.cons &&
1065 test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) &&
1066 cqr->retries > 0) {
1067 DEV_MESSAGE(KERN_DEBUG, device,
1068 "default ERP in fastpath (%i retries left)",
1069 cqr->retries);
1070 cqr->lpm = LPM_ANYPATH;
1071 cqr->status = DASD_CQR_QUEUED;
1072 next = cqr;
1073 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001075 }
1076 if (next && (next->status == DASD_CQR_QUEUED) &&
1077 (!device->stopped)) {
1078 if (device->discipline->start_IO(next) == 0)
1079 expires = next->expires;
1080 else
1081 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1082 "Interrupt fastpath "
1083 "failed!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001086 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001088 dasd_device_clear_timer(device);
1089 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001093 * If we have an error on a dasd_block layer request then we cancel
1094 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001096static void __dasd_device_recovery(struct dasd_device *device,
1097 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 struct list_head *l, *n;
1100 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001103 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001105 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001107
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001108 list_for_each_safe(l, n, &device->ccw_queue) {
1109 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1110 if (cqr->status == DASD_CQR_QUEUED &&
1111 ref_cqr->block == cqr->block) {
1112 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001114 }
1115};
1116
1117/*
1118 * Remove those ccw requests from the queue that need to be returned
1119 * to the upper layer.
1120 */
1121static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1122 struct list_head *final_queue)
1123{
1124 struct list_head *l, *n;
1125 struct dasd_ccw_req *cqr;
1126
1127 /* Process request with final status. */
1128 list_for_each_safe(l, n, &device->ccw_queue) {
1129 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1130
1131 /* Stop list processing at the first non-final request. */
1132 if (cqr->status == DASD_CQR_QUEUED ||
1133 cqr->status == DASD_CQR_IN_IO ||
1134 cqr->status == DASD_CQR_CLEAR_PENDING)
1135 break;
1136 if (cqr->status == DASD_CQR_ERROR) {
1137 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001139 /* Rechain finished requests to final queue */
1140 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142}
1143
1144/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001145 * the cqrs from the final queue are returned to the upper layer
1146 * by setting a dasd_block state and calling the callback function
1147 */
1148static void __dasd_device_process_final_queue(struct dasd_device *device,
1149 struct list_head *final_queue)
1150{
1151 struct list_head *l, *n;
1152 struct dasd_ccw_req *cqr;
1153
1154 list_for_each_safe(l, n, final_queue) {
1155 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1156 list_del_init(&cqr->devlist);
1157 if (cqr->block)
1158 spin_lock_bh(&cqr->block->queue_lock);
1159 switch (cqr->status) {
1160 case DASD_CQR_SUCCESS:
1161 cqr->status = DASD_CQR_DONE;
1162 break;
1163 case DASD_CQR_ERROR:
1164 cqr->status = DASD_CQR_NEED_ERP;
1165 break;
1166 case DASD_CQR_CLEARED:
1167 cqr->status = DASD_CQR_TERMINATED;
1168 break;
1169 default:
1170 DEV_MESSAGE(KERN_ERR, device,
1171 "wrong cqr status in __dasd_process_final_queue "
1172 "for cqr %p, status %x",
1173 cqr, cqr->status);
1174 BUG();
1175 }
1176 if (cqr->block)
1177 spin_unlock_bh(&cqr->block->queue_lock);
1178 if (cqr->callback != NULL)
1179 (cqr->callback)(cqr, cqr->callback_data);
1180 }
1181}
1182
1183
1184
1185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * Take a look at the first request on the ccw queue and check
1187 * if it reached its expire time. If so, terminate the IO.
1188 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001189static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct dasd_ccw_req *cqr;
1192
1193 if (list_empty(&device->ccw_queue))
1194 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001195 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001196 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1197 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1198 if (device->discipline->term_IO(cqr) != 0) {
1199 /* Hmpf, try again in 5 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001200 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001201 DEV_MESSAGE(KERN_ERR, device,
1202 "internal error - timeout (%is) expired "
1203 "for cqr %p, termination failed, "
1204 "retrying in 5s",
1205 (cqr->expires/HZ), cqr);
1206 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001207 DEV_MESSAGE(KERN_ERR, device,
1208 "internal error - timeout (%is) expired "
1209 "for cqr %p (%i retries left)",
1210 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 }
1213}
1214
1215/*
1216 * Take a look at the first request on the ccw queue and check
1217 * if it needs to be started.
1218 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001219static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct dasd_ccw_req *cqr;
1222 int rc;
1223
1224 if (list_empty(&device->ccw_queue))
1225 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001226 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001227 if (cqr->status != DASD_CQR_QUEUED)
1228 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229 /* when device is stopped, return request to previous layer */
1230 if (device->stopped) {
1231 cqr->status = DASD_CQR_CLEARED;
1232 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001233 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001234 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001235
1236 rc = device->discipline->start_IO(cqr);
1237 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001238 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001239 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001240 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001241 } else
1242 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001243 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001247 * Go through all request on the dasd_device request queue,
1248 * terminate them on the cdev if necessary, and return them to the
1249 * submitting layer via callback.
1250 * Note:
1251 * Make sure that all 'submitting layers' still exist when
1252 * this function is called!. In other words, when 'device' is a base
1253 * device then all block layer requests must have been removed before
1254 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001256int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001258 struct dasd_ccw_req *cqr, *n;
1259 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 INIT_LIST_HEAD(&flush_queue);
1263 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001264 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001265 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001266 /* Check status and move request to flush_queue */
1267 switch (cqr->status) {
1268 case DASD_CQR_IN_IO:
1269 rc = device->discipline->term_IO(cqr);
1270 if (rc) {
1271 /* unable to terminate requeust */
1272 DEV_MESSAGE(KERN_ERR, device,
1273 "dasd flush ccw_queue is unable "
1274 " to terminate request %p",
1275 cqr);
1276 /* stop flush processing */
1277 goto finished;
1278 }
1279 break;
1280 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001282 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001283 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001284 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001285 break;
1286 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001287 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001288 }
Horst Hummel8f617012006-08-30 14:33:33 +02001289finished:
1290 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001291 /*
1292 * After this point all requests must be in state CLEAR_PENDING,
1293 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1294 * one of the others.
1295 */
1296 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1297 wait_event(dasd_flush_wq,
1298 (cqr->status != DASD_CQR_CLEAR_PENDING));
1299 /*
1300 * Now set each request back to TERMINATED, DONE or NEED_ERP
1301 * and call the callback function of flushed requests
1302 */
1303 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001304 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307/*
1308 * Acquire the device lock and process queues for the device.
1309 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001310static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 atomic_set (&device->tasklet_scheduled, 0);
1315 INIT_LIST_HEAD(&final_queue);
1316 spin_lock_irq(get_ccwdev_lock(device->cdev));
1317 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001318 __dasd_device_check_expire(device);
1319 /* find final requests on ccw queue */
1320 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1322 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001323 __dasd_device_process_final_queue(device, &final_queue);
1324 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001326 __dasd_device_start_head(device);
1327 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 dasd_put_device(device);
1329}
1330
1331/*
1332 * Schedules a call to dasd_tasklet over the device tasklet.
1333 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001334void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001337 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return;
1339 dasd_get_device(device);
1340 tasklet_hi_schedule(&device->tasklet);
1341}
1342
1343/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001344 * Queue a request to the head of the device ccw_queue.
1345 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001347void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct dasd_device *device;
1350 unsigned long flags;
1351
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001352 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001354 cqr->status = DASD_CQR_QUEUED;
1355 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001357 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1359}
1360
1361/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001362 * Queue a request to the tail of the device ccw_queue.
1363 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001365void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct dasd_device *device;
1368 unsigned long flags;
1369
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001370 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001372 cqr->status = DASD_CQR_QUEUED;
1373 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001375 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1377}
1378
1379/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001380 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001382static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 wake_up((wait_queue_head_t *) data);
1385}
1386
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 struct dasd_device *device;
1390 int rc;
1391
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001394 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001395 cqr->status == DASD_CQR_NEED_ERP ||
1396 cqr->status == DASD_CQR_TERMINATED) &&
1397 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1399 return rc;
1400}
1401
1402/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403 * Queue a request to the tail of the device ccw_queue and wait for
1404 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001406int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 wait_queue_head_t wait_q;
1409 struct dasd_device *device;
1410 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001411
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001412 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 init_waitqueue_head (&wait_q);
1415 cqr->callback = dasd_wakeup_cb;
1416 cqr->callback_data = (void *) &wait_q;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001417 dasd_add_request_tail(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001421 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return rc;
1423}
1424
1425/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001426 * Queue a request to the tail of the device ccw_queue and wait
1427 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001429int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 wait_queue_head_t wait_q;
1432 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001433 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001435 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 init_waitqueue_head (&wait_q);
1437 cqr->callback = dasd_wakeup_cb;
1438 cqr->callback_data = (void *) &wait_q;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001439 dasd_add_request_tail(cqr);
1440 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1441 if (rc == -ERESTARTSYS) {
1442 dasd_cancel_req(cqr);
1443 /* wait (non-interruptible) for final status */
1444 wait_event(wait_q, _wait_for_wakeup(cqr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001446 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return rc;
1448}
1449
1450/*
1451 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1452 * for eckd devices) the currently running request has to be terminated
1453 * and be put back to status queued, before the special request is added
1454 * to the head of the queue. Then the special request is waited on normally.
1455 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001456static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (list_empty(&device->ccw_queue))
1461 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001462 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001463 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001466int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 wait_queue_head_t wait_q;
1469 struct dasd_device *device;
1470 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001471
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001472 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 spin_lock_irq(get_ccwdev_lock(device->cdev));
1474 rc = _dasd_term_running_cqr(device);
1475 if (rc) {
1476 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1477 return rc;
1478 }
Horst Hummel138c0142006-06-29 14:58:12 +02001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 init_waitqueue_head (&wait_q);
1481 cqr->callback = dasd_wakeup_cb;
1482 cqr->callback_data = (void *) &wait_q;
1483 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001484 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001487 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1490
1491 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001494 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return rc;
1496}
1497
1498/*
1499 * Cancels a request that was started with dasd_sleep_on_req.
1500 * This is useful to timeout requests. The request will be
1501 * terminated if it is currently in i/o.
1502 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001503 * 0 if there was no need to terminate the request (not started yet)
1504 * negative error code if termination failed
1505 * Cancellation of a request is an asynchronous operation! The calling
1506 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001508int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001510 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned long flags;
1512 int rc;
1513
1514 rc = 0;
1515 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1516 switch (cqr->status) {
1517 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518 /* request was not started - just set to cleared */
1519 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case DASD_CQR_IN_IO:
1522 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001523 rc = device->discipline->term_IO(cqr);
1524 if (rc) {
1525 DEV_MESSAGE(KERN_ERR, device,
1526 "dasd_cancel_req is unable "
1527 " to terminate request %p, rc = %d",
1528 cqr, rc);
1529 } else {
1530 cqr->stopclk = get_clock();
1531 rc = 1;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001534 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001538 dasd_schedule_device_bh(device);
1539 return rc;
1540}
1541
1542
1543/*
1544 * SECTION: Operations of the dasd_block layer.
1545 */
1546
1547/*
1548 * Timeout function for dasd_block. This is used when the block layer
1549 * is waiting for something that may not come reliably, (e.g. a state
1550 * change interrupt)
1551 */
1552static void dasd_block_timeout(unsigned long ptr)
1553{
1554 unsigned long flags;
1555 struct dasd_block *block;
1556
1557 block = (struct dasd_block *) ptr;
1558 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1559 /* re-activate request queue */
1560 block->base->stopped &= ~DASD_STOPPED_PENDING;
1561 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1562 dasd_schedule_block_bh(block);
1563}
1564
1565/*
1566 * Setup timeout for a dasd_block in jiffies.
1567 */
1568void dasd_block_set_timer(struct dasd_block *block, int expires)
1569{
1570 if (expires == 0) {
1571 if (timer_pending(&block->timer))
1572 del_timer(&block->timer);
1573 return;
1574 }
1575 if (timer_pending(&block->timer)) {
1576 if (mod_timer(&block->timer, jiffies + expires))
1577 return;
1578 }
1579 block->timer.function = dasd_block_timeout;
1580 block->timer.data = (unsigned long) block;
1581 block->timer.expires = jiffies + expires;
1582 add_timer(&block->timer);
1583}
1584
1585/*
1586 * Clear timeout for a dasd_block.
1587 */
1588void dasd_block_clear_timer(struct dasd_block *block)
1589{
1590 if (timer_pending(&block->timer))
1591 del_timer(&block->timer);
1592}
1593
1594/*
1595 * posts the buffer_cache about a finalized request
1596 */
1597static inline void dasd_end_request(struct request *req, int uptodate)
1598{
1599 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1600 BUG();
1601 add_disk_randomness(req->rq_disk);
1602 end_that_request_last(req, uptodate);
1603}
1604
1605/*
1606 * Process finished error recovery ccw.
1607 */
1608static inline void __dasd_block_process_erp(struct dasd_block *block,
1609 struct dasd_ccw_req *cqr)
1610{
1611 dasd_erp_fn_t erp_fn;
1612 struct dasd_device *device = block->base;
1613
1614 if (cqr->status == DASD_CQR_DONE)
1615 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1616 else
1617 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1618 erp_fn = device->discipline->erp_postaction(cqr);
1619 erp_fn(cqr);
1620}
1621
1622/*
1623 * Fetch requests from the block device queue.
1624 */
1625static void __dasd_process_request_queue(struct dasd_block *block)
1626{
1627 struct request_queue *queue;
1628 struct request *req;
1629 struct dasd_ccw_req *cqr;
1630 struct dasd_device *basedev;
1631 unsigned long flags;
1632 queue = block->request_queue;
1633 basedev = block->base;
1634 /* No queue ? Then there is nothing to do. */
1635 if (queue == NULL)
1636 return;
1637
1638 /*
1639 * We requeue request from the block device queue to the ccw
1640 * queue only in two states. In state DASD_STATE_READY the
1641 * partition detection is done and we need to requeue requests
1642 * for that. State DASD_STATE_ONLINE is normal block device
1643 * operation.
1644 */
1645 if (basedev->state < DASD_STATE_READY)
1646 return;
1647 /* Now we try to fetch requests from the request queue */
1648 while (!blk_queue_plugged(queue) &&
1649 elv_next_request(queue)) {
1650
1651 req = elv_next_request(queue);
1652
1653 if (basedev->features & DASD_FEATURE_READONLY &&
1654 rq_data_dir(req) == WRITE) {
1655 DBF_DEV_EVENT(DBF_ERR, basedev,
1656 "Rejecting write request %p",
1657 req);
1658 blkdev_dequeue_request(req);
1659 dasd_end_request(req, 0);
1660 continue;
1661 }
1662 cqr = basedev->discipline->build_cp(basedev, block, req);
1663 if (IS_ERR(cqr)) {
1664 if (PTR_ERR(cqr) == -EBUSY)
1665 break; /* normal end condition */
1666 if (PTR_ERR(cqr) == -ENOMEM)
1667 break; /* terminate request queue loop */
1668 if (PTR_ERR(cqr) == -EAGAIN) {
1669 /*
1670 * The current request cannot be build right
1671 * now, we have to try later. If this request
1672 * is the head-of-queue we stop the device
1673 * for 1/2 second.
1674 */
1675 if (!list_empty(&block->ccw_queue))
1676 break;
1677 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1678 basedev->stopped |= DASD_STOPPED_PENDING;
1679 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1680 dasd_block_set_timer(block, HZ/2);
1681 break;
1682 }
1683 DBF_DEV_EVENT(DBF_ERR, basedev,
1684 "CCW creation failed (rc=%ld) "
1685 "on request %p",
1686 PTR_ERR(cqr), req);
1687 blkdev_dequeue_request(req);
1688 dasd_end_request(req, 0);
1689 continue;
1690 }
1691 /*
1692 * Note: callback is set to dasd_return_cqr_cb in
1693 * __dasd_block_start_head to cover erp requests as well
1694 */
1695 cqr->callback_data = (void *) req;
1696 cqr->status = DASD_CQR_FILLED;
1697 blkdev_dequeue_request(req);
1698 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1699 dasd_profile_start(block, cqr, req);
1700 }
1701}
1702
1703static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1704{
1705 struct request *req;
1706 int status;
1707
1708 req = (struct request *) cqr->callback_data;
1709 dasd_profile_end(cqr->block, cqr, req);
1710 status = cqr->memdev->discipline->free_cp(cqr, req);
1711 dasd_end_request(req, status);
1712}
1713
1714/*
1715 * Process ccw request queue.
1716 */
1717static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1718 struct list_head *final_queue)
1719{
1720 struct list_head *l, *n;
1721 struct dasd_ccw_req *cqr;
1722 dasd_erp_fn_t erp_fn;
1723 unsigned long flags;
1724 struct dasd_device *base = block->base;
1725
1726restart:
1727 /* Process request with final status. */
1728 list_for_each_safe(l, n, &block->ccw_queue) {
1729 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1730 if (cqr->status != DASD_CQR_DONE &&
1731 cqr->status != DASD_CQR_FAILED &&
1732 cqr->status != DASD_CQR_NEED_ERP &&
1733 cqr->status != DASD_CQR_TERMINATED)
1734 continue;
1735
1736 if (cqr->status == DASD_CQR_TERMINATED) {
1737 base->discipline->handle_terminated_request(cqr);
1738 goto restart;
1739 }
1740
1741 /* Process requests that may be recovered */
1742 if (cqr->status == DASD_CQR_NEED_ERP) {
1743 if (cqr->irb.esw.esw0.erw.cons &&
1744 test_bit(DASD_CQR_FLAGS_USE_ERP,
1745 &cqr->flags)) {
1746 erp_fn = base->discipline->erp_action(cqr);
1747 erp_fn(cqr);
1748 }
1749 goto restart;
1750 }
1751
1752 /* First of all call extended error reporting. */
1753 if (dasd_eer_enabled(base) &&
1754 cqr->status == DASD_CQR_FAILED) {
1755 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1756
1757 /* restart request */
1758 cqr->status = DASD_CQR_FILLED;
1759 cqr->retries = 255;
1760 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1761 base->stopped |= DASD_STOPPED_QUIESCE;
1762 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1763 flags);
1764 goto restart;
1765 }
1766
1767 /* Process finished ERP request. */
1768 if (cqr->refers) {
1769 __dasd_block_process_erp(block, cqr);
1770 goto restart;
1771 }
1772
1773 /* Rechain finished requests to final queue */
1774 cqr->endclk = get_clock();
1775 list_move_tail(&cqr->blocklist, final_queue);
1776 }
1777}
1778
1779static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1780{
1781 dasd_schedule_block_bh(cqr->block);
1782}
1783
1784static void __dasd_block_start_head(struct dasd_block *block)
1785{
1786 struct dasd_ccw_req *cqr;
1787
1788 if (list_empty(&block->ccw_queue))
1789 return;
1790 /* We allways begin with the first requests on the queue, as some
1791 * of previously started requests have to be enqueued on a
1792 * dasd_device again for error recovery.
1793 */
1794 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1795 if (cqr->status != DASD_CQR_FILLED)
1796 continue;
1797 /* Non-temporary stop condition will trigger fail fast */
1798 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1799 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1800 (!dasd_eer_enabled(block->base))) {
1801 cqr->status = DASD_CQR_FAILED;
1802 dasd_schedule_block_bh(block);
1803 continue;
1804 }
1805 /* Don't try to start requests if device is stopped */
1806 if (block->base->stopped)
1807 return;
1808
1809 /* just a fail safe check, should not happen */
1810 if (!cqr->startdev)
1811 cqr->startdev = block->base;
1812
1813 /* make sure that the requests we submit find their way back */
1814 cqr->callback = dasd_return_cqr_cb;
1815
1816 dasd_add_request_tail(cqr);
1817 }
1818}
1819
1820/*
1821 * Central dasd_block layer routine. Takes requests from the generic
1822 * block layer request queue, creates ccw requests, enqueues them on
1823 * a dasd_device and processes ccw requests that have been returned.
1824 */
1825static void dasd_block_tasklet(struct dasd_block *block)
1826{
1827 struct list_head final_queue;
1828 struct list_head *l, *n;
1829 struct dasd_ccw_req *cqr;
1830
1831 atomic_set(&block->tasklet_scheduled, 0);
1832 INIT_LIST_HEAD(&final_queue);
1833 spin_lock(&block->queue_lock);
1834 /* Finish off requests on ccw queue */
1835 __dasd_process_block_ccw_queue(block, &final_queue);
1836 spin_unlock(&block->queue_lock);
1837 /* Now call the callback function of requests with final status */
1838 spin_lock_irq(&block->request_queue_lock);
1839 list_for_each_safe(l, n, &final_queue) {
1840 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1841 list_del_init(&cqr->blocklist);
1842 __dasd_cleanup_cqr(cqr);
1843 }
1844 spin_lock(&block->queue_lock);
1845 /* Get new request from the block device request queue */
1846 __dasd_process_request_queue(block);
1847 /* Now check if the head of the ccw queue needs to be started. */
1848 __dasd_block_start_head(block);
1849 spin_unlock(&block->queue_lock);
1850 spin_unlock_irq(&block->request_queue_lock);
1851 dasd_put_device(block->base);
1852}
1853
1854static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1855{
1856 wake_up(&dasd_flush_wq);
1857}
1858
1859/*
1860 * Go through all request on the dasd_block request queue, cancel them
1861 * on the respective dasd_device, and return them to the generic
1862 * block layer.
1863 */
1864static int dasd_flush_block_queue(struct dasd_block *block)
1865{
1866 struct dasd_ccw_req *cqr, *n;
1867 int rc, i;
1868 struct list_head flush_queue;
1869
1870 INIT_LIST_HEAD(&flush_queue);
1871 spin_lock_bh(&block->queue_lock);
1872 rc = 0;
1873restart:
1874 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1875 /* if this request currently owned by a dasd_device cancel it */
1876 if (cqr->status >= DASD_CQR_QUEUED)
1877 rc = dasd_cancel_req(cqr);
1878 if (rc < 0)
1879 break;
1880 /* Rechain request (including erp chain) so it won't be
1881 * touched by the dasd_block_tasklet anymore.
1882 * Replace the callback so we notice when the request
1883 * is returned from the dasd_device layer.
1884 */
1885 cqr->callback = _dasd_wake_block_flush_cb;
1886 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1887 list_move_tail(&cqr->blocklist, &flush_queue);
1888 if (i > 1)
1889 /* moved more than one request - need to restart */
1890 goto restart;
1891 }
1892 spin_unlock_bh(&block->queue_lock);
1893 /* Now call the callback function of flushed requests */
1894restart_cb:
1895 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1896 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1897 /* Process finished ERP request. */
1898 if (cqr->refers) {
1899 __dasd_block_process_erp(block, cqr);
1900 /* restart list_for_xx loop since dasd_process_erp
1901 * might remove multiple elements */
1902 goto restart_cb;
1903 }
1904 /* call the callback function */
1905 cqr->endclk = get_clock();
1906 list_del_init(&cqr->blocklist);
1907 __dasd_cleanup_cqr(cqr);
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return rc;
1910}
1911
1912/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001913 * Schedules a call to dasd_tasklet over the device tasklet.
1914 */
1915void dasd_schedule_block_bh(struct dasd_block *block)
1916{
1917 /* Protect against rescheduling. */
1918 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1919 return;
1920 /* life cycle of block is bound to it's base device */
1921 dasd_get_device(block->base);
1922 tasklet_hi_schedule(&block->tasklet);
1923}
1924
1925
1926/*
1927 * SECTION: external block device operations
1928 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
1930
1931/*
1932 * Dasd request queue function. Called from ll_rw_blk.c
1933 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001934static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001936 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001938 block = queue->queuedata;
1939 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001941 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001943 __dasd_block_start_head(block);
1944 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947/*
1948 * Allocate and initialize request queue and default I/O scheduler.
1949 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001950static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 int rc;
1953
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 block->request_queue = blk_init_queue(do_dasd_request,
1955 &block->request_queue_lock);
1956 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -ENOMEM;
1958
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001959 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 elevator_exit(block->request_queue->elevator);
1962 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001964 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return rc;
1966 }
1967 return 0;
1968}
1969
1970/*
1971 * Allocate and initialize request queue.
1972 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001973static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int max;
1976
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001977 blk_queue_hardsect_size(block->request_queue, block->bp_block);
1978 max = block->base->discipline->max_blocks << block->s2b_shift;
1979 blk_queue_max_sectors(block->request_queue, max);
1980 blk_queue_max_phys_segments(block->request_queue, -1L);
1981 blk_queue_max_hw_segments(block->request_queue, -1L);
1982 blk_queue_max_segment_size(block->request_queue, -1L);
1983 blk_queue_segment_boundary(block->request_queue, -1L);
1984 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987/*
1988 * Deactivate and free request queue.
1989 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001990static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001992 if (block->request_queue) {
1993 blk_cleanup_queue(block->request_queue);
1994 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996}
1997
1998/*
1999 * Flush request on the request queue.
2000 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002001static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct request *req;
2004
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002005 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002007
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002008 spin_lock_irq(&block->request_queue_lock);
2009 while ((req = elv_next_request(block->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02002011 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016static int dasd_open(struct inode *inp, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct gendisk *disk = inp->i_bdev->bd_disk;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002019 struct dasd_block *block = disk->private_data;
2020 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 int rc;
2022
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002023 atomic_inc(&block->open_count);
2024 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 rc = -ENODEV;
2026 goto unlock;
2027 }
2028
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rc = -EINVAL;
2031 goto unlock;
2032 }
2033
2034 if (dasd_probeonly) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002035 DEV_MESSAGE(KERN_INFO, base, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 "No access to device due to probeonly mode");
2037 rc = -EPERM;
2038 goto out;
2039 }
2040
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002041 if (base->state <= DASD_STATE_BASIC) {
2042 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 " Cannot open unrecognized device");
2044 rc = -ENODEV;
2045 goto out;
2046 }
2047
2048 return 0;
2049
2050out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002051 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002053 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return rc;
2055}
2056
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002057static int dasd_release(struct inode *inp, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct gendisk *disk = inp->i_bdev->bd_disk;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002060 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 atomic_dec(&block->open_count);
2063 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
2065}
2066
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002067/*
2068 * Return disk geometry.
2069 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002070static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002071{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 struct dasd_block *block;
2073 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002074
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002075 block = bdev->bd_disk->private_data;
2076 base = block->base;
2077 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002078 return -ENODEV;
2079
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002080 if (!base->discipline ||
2081 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002082 return -EINVAL;
2083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084 base->discipline->fill_geometry(block, geo);
2085 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002086 return 0;
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089struct block_device_operations
2090dasd_device_operations = {
2091 .owner = THIS_MODULE,
2092 .open = dasd_open,
2093 .release = dasd_release,
2094 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08002095 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002096 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097};
2098
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002099/*******************************************************************************
2100 * end of block device operations
2101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103static void
2104dasd_exit(void)
2105{
2106#ifdef CONFIG_PROC_FS
2107 dasd_proc_exit();
2108#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002109 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002110 if (dasd_page_cache != NULL) {
2111 kmem_cache_destroy(dasd_page_cache);
2112 dasd_page_cache = NULL;
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 dasd_gendisk_exit();
2115 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (dasd_debug_area != NULL) {
2117 debug_unregister(dasd_debug_area);
2118 dasd_debug_area = NULL;
2119 }
2120}
2121
2122/*
2123 * SECTION: common functions for ccw_driver use
2124 */
2125
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002126/*
2127 * Initial attempt at a probe function. this can be simplified once
2128 * the other detection code is gone.
2129 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002130int dasd_generic_probe(struct ccw_device *cdev,
2131 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 int ret;
2134
Horst Hummel40545572006-06-29 15:08:18 +02002135 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2136 if (ret) {
2137 printk(KERN_WARNING
2138 "dasd_generic_probe: could not set ccw-device options "
2139 "for %s\n", cdev->dev.bus_id);
2140 return ret;
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ret = dasd_add_sysfs_files(cdev);
2143 if (ret) {
2144 printk(KERN_WARNING
2145 "dasd_generic_probe: could not add sysfs entries "
2146 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02002147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
Horst Hummel40545572006-06-29 15:08:18 +02002149 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Horst Hummel40545572006-06-29 15:08:18 +02002151 /*
2152 * Automatically online either all dasd devices (dasd_autodetect)
2153 * or all devices specified with dasd= parameters during
2154 * initial probe.
2155 */
2156 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2157 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
2158 ret = ccw_device_set_online(cdev);
2159 if (ret)
2160 printk(KERN_WARNING
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002161 "dasd_generic_probe: could not initially "
2162 "online ccw-device %s; return code: %d\n",
2163 cdev->dev.bus_id, ret);
2164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002167/*
2168 * This will one day be called from a global not_oper handler.
2169 * It is also used by driver_unregister during module unload.
2170 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002171void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002174 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Horst Hummel59afda72005-05-16 21:53:39 -07002176 cdev->handler = NULL;
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 dasd_remove_sysfs_files(cdev);
2179 device = dasd_device_from_cdev(cdev);
2180 if (IS_ERR(device))
2181 return;
2182 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2183 /* Already doing offline processing */
2184 dasd_put_device(device);
2185 return;
2186 }
2187 /*
2188 * This device is removed unconditionally. Set offline
2189 * flag to prevent dasd_open from opening it while it is
2190 * no quite down yet.
2191 */
2192 dasd_set_target_state(device, DASD_STATE_NEW);
2193 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002194 block = device->block;
2195 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002197 /*
2198 * life cycle of block is bound to device, so delete it after
2199 * device was safely removed
2200 */
2201 if (block)
2202 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002205/*
2206 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002208 * or the user has started activation through sysfs.
2209 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002210int dasd_generic_set_online(struct ccw_device *cdev,
2211 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002213 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002215 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002216
Horst Hummel40545572006-06-29 15:08:18 +02002217 /* first online clears initial online feature flag */
2218 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 device = dasd_create_device(cdev);
2220 if (IS_ERR(device))
2221 return PTR_ERR(device);
2222
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002223 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002224 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!dasd_diag_discipline_pointer) {
2226 printk (KERN_WARNING
2227 "dasd_generic couldn't online device %s "
2228 "- discipline DIAG not available\n",
2229 cdev->dev.bus_id);
2230 dasd_delete_device(device);
2231 return -ENODEV;
2232 }
2233 discipline = dasd_diag_discipline_pointer;
2234 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002235 if (!try_module_get(base_discipline->owner)) {
2236 dasd_delete_device(device);
2237 return -EINVAL;
2238 }
2239 if (!try_module_get(discipline->owner)) {
2240 module_put(base_discipline->owner);
2241 dasd_delete_device(device);
2242 return -EINVAL;
2243 }
2244 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 device->discipline = discipline;
2246
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002247 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 rc = discipline->check_device(device);
2249 if (rc) {
2250 printk (KERN_WARNING
2251 "dasd_generic couldn't online device %s "
2252 "with discipline %s rc=%i\n",
2253 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002254 module_put(discipline->owner);
2255 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 dasd_delete_device(device);
2257 return rc;
2258 }
2259
2260 dasd_set_target_state(device, DASD_STATE_ONLINE);
2261 if (device->state <= DASD_STATE_KNOWN) {
2262 printk (KERN_WARNING
2263 "dasd_generic discipline not found for %s\n",
2264 cdev->dev.bus_id);
2265 rc = -ENODEV;
2266 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002267 if (device->block)
2268 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 dasd_delete_device(device);
2270 } else
2271 pr_debug("dasd_generic device %s found\n",
2272 cdev->dev.bus_id);
2273
2274 /* FIXME: we have to wait for the root device but we don't want
2275 * to wait for each single device but for all at once. */
2276 wait_event(dasd_init_waitq, _wait_for_device(device));
2277
2278 dasd_put_device(device);
2279
2280 return rc;
2281}
2282
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002283int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002286 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002287 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 device = dasd_device_from_cdev(cdev);
2290 if (IS_ERR(device))
2291 return PTR_ERR(device);
2292 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2293 /* Already doing offline processing */
2294 dasd_put_device(device);
2295 return 0;
2296 }
2297 /*
2298 * We must make sure that this device is currently not in use.
2299 * The open_count is increased for every opener, that includes
2300 * the blkdev_get in dasd_scan_partitions. We are only interested
2301 * in the other openers.
2302 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002303 if (device->block) {
2304 struct dasd_block *block = device->block;
2305 max_count = block->bdev ? 0 : -1;
2306 open_count = (int) atomic_read(&block->open_count);
2307 if (open_count > max_count) {
2308 if (open_count > 0)
2309 printk(KERN_WARNING "Can't offline dasd "
2310 "device with open count = %i.\n",
2311 open_count);
2312 else
2313 printk(KERN_WARNING "%s",
2314 "Can't offline dasd device due "
2315 "to internal use\n");
2316 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2317 dasd_put_device(device);
2318 return -EBUSY;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 dasd_set_target_state(device, DASD_STATE_NEW);
2322 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002323 block = device->block;
2324 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002326 /*
2327 * life cycle of block is bound to device, so delete it after
2328 * device was safely removed
2329 */
2330 if (block)
2331 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333}
2334
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 struct dasd_device *device;
2338 struct dasd_ccw_req *cqr;
2339 unsigned long flags;
2340 int ret;
2341
2342 device = dasd_device_from_cdev(cdev);
2343 if (IS_ERR(device))
2344 return 0;
2345 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2346 ret = 0;
2347 switch (event) {
2348 case CIO_GONE:
2349 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002350 /* First of all call extended error reporting. */
2351 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (device->state < DASD_STATE_BASIC)
2354 break;
2355 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002356 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2357 if (cqr->status == DASD_CQR_IN_IO) {
2358 cqr->status = DASD_CQR_QUEUED;
2359 cqr->retries++;
2360 }
2361 device->stopped |= DASD_STOPPED_DC_WAIT;
2362 dasd_device_clear_timer(device);
2363 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 ret = 1;
2365 break;
2366 case CIO_OPER:
2367 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002368 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2369 dasd_schedule_device_bh(device);
2370 if (device->block)
2371 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ret = 1;
2373 break;
2374 }
2375 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2376 dasd_put_device(device);
2377 return ret;
2378}
2379
Heiko Carstens763968e2007-05-10 15:45:46 +02002380static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2381 void *rdc_buffer,
2382 int rdc_buffer_size,
2383 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002384{
2385 struct dasd_ccw_req *cqr;
2386 struct ccw1 *ccw;
2387
2388 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2389
2390 if (IS_ERR(cqr)) {
2391 DEV_MESSAGE(KERN_WARNING, device, "%s",
2392 "Could not allocate RDC request");
2393 return cqr;
2394 }
2395
2396 ccw = cqr->cpaddr;
2397 ccw->cmd_code = CCW_CMD_RDC;
2398 ccw->cda = (__u32)(addr_t)rdc_buffer;
2399 ccw->count = rdc_buffer_size;
2400
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002401 cqr->startdev = device;
2402 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002403 cqr->expires = 10*HZ;
2404 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2405 cqr->retries = 2;
2406 cqr->buildclk = get_clock();
2407 cqr->status = DASD_CQR_FILLED;
2408 return cqr;
2409}
2410
2411
2412int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2413 void **rdc_buffer, int rdc_buffer_size)
2414{
2415 int ret;
2416 struct dasd_ccw_req *cqr;
2417
2418 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2419 magic);
2420 if (IS_ERR(cqr))
2421 return PTR_ERR(cqr);
2422
2423 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002424 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002425 return ret;
2426}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002427EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002428
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002429static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 int rc;
2432
2433 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002434 init_waitqueue_head(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002437 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (dasd_debug_area == NULL) {
2439 rc = -ENOMEM;
2440 goto failed;
2441 }
2442 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002443 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2446
2447 dasd_diag_discipline_pointer = NULL;
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 rc = dasd_devmap_init();
2450 if (rc)
2451 goto failed;
2452 rc = dasd_gendisk_init();
2453 if (rc)
2454 goto failed;
2455 rc = dasd_parse();
2456 if (rc)
2457 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002458 rc = dasd_eer_init();
2459 if (rc)
2460 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461#ifdef CONFIG_PROC_FS
2462 rc = dasd_proc_init();
2463 if (rc)
2464 goto failed;
2465#endif
2466
2467 return 0;
2468failed:
2469 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2470 dasd_exit();
2471 return rc;
2472}
2473
2474module_init(dasd_init);
2475module_exit(dasd_exit);
2476
2477EXPORT_SYMBOL(dasd_debug_area);
2478EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2479
2480EXPORT_SYMBOL(dasd_add_request_head);
2481EXPORT_SYMBOL(dasd_add_request_tail);
2482EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002483EXPORT_SYMBOL(dasd_device_clear_timer);
2484EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485EXPORT_SYMBOL(dasd_enable_device);
2486EXPORT_SYMBOL(dasd_int_handler);
2487EXPORT_SYMBOL(dasd_kfree_request);
2488EXPORT_SYMBOL(dasd_kick_device);
2489EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490EXPORT_SYMBOL(dasd_schedule_device_bh);
2491EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002493EXPORT_SYMBOL(dasd_device_set_timer);
2494EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495EXPORT_SYMBOL(dasd_sfree_request);
2496EXPORT_SYMBOL(dasd_sleep_on);
2497EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2498EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2499EXPORT_SYMBOL(dasd_smalloc_request);
2500EXPORT_SYMBOL(dasd_start_IO);
2501EXPORT_SYMBOL(dasd_term_IO);
2502
2503EXPORT_SYMBOL_GPL(dasd_generic_probe);
2504EXPORT_SYMBOL_GPL(dasd_generic_remove);
2505EXPORT_SYMBOL_GPL(dasd_generic_notify);
2506EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2507EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002508EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2509EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2510EXPORT_SYMBOL_GPL(dasd_alloc_block);
2511EXPORT_SYMBOL_GPL(dasd_free_block);