blob: 93972ed7f2df0eb792a978c6c9552a6e99ead831 [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 *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010060static void dasd_device_timeout(unsigned long);
61static void dasd_block_timeout(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * SECTION: Operations on the device structure.
65 */
66static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020067static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020068static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Allocate memory for a new device structure.
72 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 struct dasd_device *device;
76
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010077 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
78 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* Get two pages for normal block device operations. */
82 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010083 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 kfree(device);
85 return ERR_PTR(-ENOMEM);
86 }
87 /* Get one page for error recovery. */
88 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010089 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 free_pages((unsigned long) device->ccw_mem, 1);
91 kfree(device);
92 return ERR_PTR(-ENOMEM);
93 }
94
95 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
96 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
97 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010098 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +020099 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100100 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 (unsigned long) device);
102 INIT_LIST_HEAD(&device->ccw_queue);
103 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100104 device->timer.function = dasd_device_timeout;
105 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000106 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 device->state = DASD_STATE_NEW;
108 device->target = DASD_STATE_NEW;
109
110 return device;
111}
112
113/*
114 * Free memory of a device structure.
115 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100116void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800118 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 free_page((unsigned long) device->erp_mem);
120 free_pages((unsigned long) device->ccw_mem, 1);
121 kfree(device);
122}
123
124/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100125 * Allocate memory for a new device structure.
126 */
127struct dasd_block *dasd_alloc_block(void)
128{
129 struct dasd_block *block;
130
131 block = kzalloc(sizeof(*block), GFP_ATOMIC);
132 if (!block)
133 return ERR_PTR(-ENOMEM);
134 /* open_count = 0 means device online but not in use */
135 atomic_set(&block->open_count, -1);
136
137 spin_lock_init(&block->request_queue_lock);
138 atomic_set(&block->tasklet_scheduled, 0);
139 tasklet_init(&block->tasklet,
140 (void (*)(unsigned long)) dasd_block_tasklet,
141 (unsigned long) block);
142 INIT_LIST_HEAD(&block->ccw_queue);
143 spin_lock_init(&block->queue_lock);
144 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100145 block->timer.function = dasd_block_timeout;
146 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100147
148 return block;
149}
150
151/*
152 * Free memory of a device structure.
153 */
154void dasd_free_block(struct dasd_block *block)
155{
156 kfree(block);
157}
158
159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Make a new device known to the system.
161 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100162static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int rc;
165
166 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200167 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * keep the reference count > 0.
169 */
170 dasd_get_device(device);
171
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100172 if (device->block) {
173 rc = dasd_alloc_queue(device->block);
174 if (rc) {
175 dasd_put_device(device);
176 return rc;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 device->state = DASD_STATE_KNOWN;
180 return 0;
181}
182
183/*
184 * Let the system forget about a device.
185 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100186static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800188 /* Disable extended error reporting for this device. */
189 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100191 if (device->discipline) {
192 if (device->discipline->uncheck_device)
193 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800194 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800197 if (device->base_discipline)
198 module_put(device->base_discipline->owner);
199 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 device->state = DASD_STATE_NEW;
201
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100202 if (device->block)
203 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* Give up reference we took in dasd_state_new_to_known. */
206 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/*
211 * Request the irq line for the device.
212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100213static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int rc;
216
217 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100218 if (device->block) {
219 rc = dasd_gendisk_alloc(device->block);
220 if (rc)
221 return rc;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Kay Sievers2a0217d2008-10-10 21:33:09 +0200224 device->debug_area = debug_register(dev_name(&device->cdev->dev), 1, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100225 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200227 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
229
230 device->state = DASD_STATE_BASIC;
231 return 0;
232}
233
234/*
235 * Release the irq line for the device. Terminate any running i/o.
236 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100237static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Horst Hummel8f617012006-08-30 14:33:33 +0200239 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100240 if (device->block) {
241 dasd_gendisk_free(device->block);
242 dasd_block_clear_timer(device->block);
243 }
244 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200245 if (rc)
246 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100247 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
250 if (device->debug_area != NULL) {
251 debug_unregister(device->debug_area);
252 device->debug_area = NULL;
253 }
254 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * Do the initial analysis. The do_analysis function may return
260 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
261 * until the discipline decides to continue the startup sequence
262 * by calling the function dasd_change_state. The eckd disciplines
263 * uses this to start a ccw that detects the format. The completion
264 * interrupt for this detection ccw uses the kernel event daemon to
265 * trigger the call to dasd_change_state. All this is done in the
266 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800267 * After the analysis ccw is done (do_analysis returned 0) the block
268 * device is setup.
269 * In case the analysis returns an error, the device setup is stopped
270 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100272static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100275 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100278 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800279 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100280 if (block) {
281 if (block->base->discipline->do_analysis != NULL)
282 rc = block->base->discipline->do_analysis(block);
283 if (rc) {
284 if (rc != -EAGAIN)
285 device->state = DASD_STATE_UNFMT;
286 return rc;
287 }
288 dasd_setup_queue(block);
289 set_capacity(block->gdp,
290 block->blocks << block->s2b_shift);
291 device->state = DASD_STATE_READY;
292 rc = dasd_scan_partitions(block);
293 if (rc)
294 device->state = DASD_STATE_BASIC;
295 } else {
296 device->state = DASD_STATE_READY;
297 }
Horst Hummel90f00942006-03-07 21:55:39 -0800298 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
302 * Remove device from block device layer. Destroy dirty buffers.
303 * Forget format information. Check if the target level is basic
304 * and if it is create fake disk for formatting.
305 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100306static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Horst Hummel8f617012006-08-30 14:33:33 +0200308 int rc;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100311 if (device->block) {
312 struct dasd_block *block = device->block;
313 rc = dasd_flush_block_queue(block);
314 if (rc) {
315 device->state = DASD_STATE_READY;
316 return rc;
317 }
318 dasd_destroy_partitions(block);
319 dasd_flush_request_queue(block);
320 block->blocks = 0;
321 block->bp_block = 0;
322 block->s2b_shift = 0;
323 }
Horst Hummel8f617012006-08-30 14:33:33 +0200324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/*
Horst Hummel90f00942006-03-07 21:55:39 -0800328 * Back to basic.
329 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100330static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800331{
332 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200333 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800334}
335
336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Make the device online and schedule the bottom half to start
338 * the requeueing of requests from the linux request queue to the
339 * ccw queue.
340 */
Horst Hummel8f617012006-08-30 14:33:33 +0200341static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342dasd_state_ready_to_online(struct dasd_device * device)
343{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100344 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100345 struct gendisk *disk;
346 struct disk_part_iter piter;
347 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100348
349 if (device->discipline->ready_to_online) {
350 rc = device->discipline->ready_to_online(device);
351 if (rc)
352 return rc;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100355 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100356 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100357 disk = device->block->bdev->bd_disk;
358 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
359 while ((part = disk_part_iter_next(&piter)))
360 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
361 disk_part_iter_exit(&piter);
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364}
365
366/*
367 * Stop the requeueing of requests again.
368 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100369static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100371 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100372 struct gendisk *disk;
373 struct disk_part_iter piter;
374 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375
376 if (device->discipline->online_to_ready) {
377 rc = device->discipline->online_to_ready(device);
378 if (rc)
379 return rc;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100382 if (device->block) {
383 disk = device->block->bdev->bd_disk;
384 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
385 while ((part = disk_part_iter_next(&piter)))
386 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
387 disk_part_iter_exit(&piter);
388 }
Horst Hummel8f617012006-08-30 14:33:33 +0200389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
393 * Device startup state changes.
394 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100395static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int rc;
398
399 rc = 0;
400 if (device->state == DASD_STATE_NEW &&
401 device->target >= DASD_STATE_KNOWN)
402 rc = dasd_state_new_to_known(device);
403
404 if (!rc &&
405 device->state == DASD_STATE_KNOWN &&
406 device->target >= DASD_STATE_BASIC)
407 rc = dasd_state_known_to_basic(device);
408
409 if (!rc &&
410 device->state == DASD_STATE_BASIC &&
411 device->target >= DASD_STATE_READY)
412 rc = dasd_state_basic_to_ready(device);
413
414 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700415 device->state == DASD_STATE_UNFMT &&
416 device->target > DASD_STATE_UNFMT)
417 rc = -EPERM;
418
419 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 device->state == DASD_STATE_READY &&
421 device->target >= DASD_STATE_ONLINE)
422 rc = dasd_state_ready_to_online(device);
423
424 return rc;
425}
426
427/*
428 * Device shutdown state changes.
429 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100430static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Horst Hummel8f617012006-08-30 14:33:33 +0200432 int rc;
433
434 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (device->state == DASD_STATE_ONLINE &&
436 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200437 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200438
Horst Hummel8f617012006-08-30 14:33:33 +0200439 if (!rc &&
440 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200442 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800443
Horst Hummel8f617012006-08-30 14:33:33 +0200444 if (!rc &&
445 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800446 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200447 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800448
Horst Hummel8f617012006-08-30 14:33:33 +0200449 if (!rc &&
450 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200452 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200453
Horst Hummel8f617012006-08-30 14:33:33 +0200454 if (!rc &&
455 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200457 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Horst Hummel8f617012006-08-30 14:33:33 +0200459 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * This is the main startup/shutdown routine.
464 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100465static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int rc;
468
469 if (device->state == device->target)
470 /* Already where we want to go today... */
471 return;
472 if (device->state < device->target)
473 rc = dasd_increase_state(device);
474 else
475 rc = dasd_decrease_state(device);
476 if (rc && rc != -EAGAIN)
477 device->target = device->state;
478
479 if (device->state == device->target)
480 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200481
482 /* let user-space know that the device status changed */
483 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/*
487 * Kick starter for devices that did not complete the startup/shutdown
488 * procedure or were sleeping because of a pending state.
489 * dasd_kick_device will schedule a call do do_kick_device to the kernel
490 * event daemon.
491 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100492static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Al Viro4927b3f2006-12-06 19:18:20 +0000494 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100496 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 dasd_put_device(device);
498}
499
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100500void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 dasd_get_device(device);
503 /* queue call to dasd_kick_device to the kernel event daemon. */
504 schedule_work(&device->kick_work);
505}
506
507/*
508 * Set the target state for a device and starts the state change.
509 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100510void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 /* If we are in probeonly mode stop at DASD_STATE_READY. */
513 if (dasd_probeonly && target > DASD_STATE_READY)
514 target = DASD_STATE_READY;
515 if (device->target != target) {
516 if (device->state == target)
517 wake_up(&dasd_init_waitq);
518 device->target = target;
519 }
520 if (device->state != device->target)
521 dasd_change_state(device);
522}
523
524/*
525 * Enable devices with device numbers in [from..to].
526 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100527static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 return (device->state == device->target);
530}
531
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100532void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 dasd_set_target_state(device, DASD_STATE_ONLINE);
535 if (device->state <= DASD_STATE_KNOWN)
536 /* No discipline for device found. */
537 dasd_set_target_state(device, DASD_STATE_NEW);
538 /* Now wait for the devices to come up. */
539 wait_event(dasd_init_waitq, _wait_for_device(device));
540}
541
542/*
543 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
544 */
545#ifdef CONFIG_DASD_PROFILE
546
547struct dasd_profile_info_t dasd_global_profile;
548unsigned int dasd_profile_level = DASD_PROFILE_OFF;
549
550/*
551 * Increments counter in global and local profiling structures.
552 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100553#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{ \
555 int index; \
556 for (index = 0; index < 31 && value >> (2+index); index++); \
557 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100558 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
562 * Add profiling information for cqr before execution.
563 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100564static void dasd_profile_start(struct dasd_block *block,
565 struct dasd_ccw_req *cqr,
566 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 struct list_head *l;
569 unsigned int counter;
570
571 if (dasd_profile_level != DASD_PROFILE_ON)
572 return;
573
574 /* count the length of the chanq for statistics */
575 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100576 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (++counter >= 31)
578 break;
579 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100580 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583/*
584 * Add profiling information for cqr after execution.
585 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100586static void dasd_profile_end(struct dasd_block *block,
587 struct dasd_ccw_req *cqr,
588 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 long strtime, irqtime, endtime, tottime; /* in microseconds */
591 long tottimeps, sectors;
592
593 if (dasd_profile_level != DASD_PROFILE_ON)
594 return;
595
596 sectors = req->nr_sectors;
597 if (!cqr->buildclk || !cqr->startclk ||
598 !cqr->stopclk || !cqr->endclk ||
599 !sectors)
600 return;
601
602 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
603 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
604 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
605 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
606 tottimeps = tottime / sectors;
607
608 if (!dasd_global_profile.dasd_io_reqs)
609 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100610 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dasd_global_profile.dasd_io_reqs++;
612 dasd_global_profile.dasd_io_sects += sectors;
613
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100614 if (!block->profile.dasd_io_reqs)
615 memset(&block->profile, 0,
616 sizeof(struct dasd_profile_info_t));
617 block->profile.dasd_io_reqs++;
618 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100620 dasd_profile_counter(sectors, dasd_io_secs, block);
621 dasd_profile_counter(tottime, dasd_io_times, block);
622 dasd_profile_counter(tottimeps, dasd_io_timps, block);
623 dasd_profile_counter(strtime, dasd_io_time1, block);
624 dasd_profile_counter(irqtime, dasd_io_time2, block);
625 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
626 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100629#define dasd_profile_start(block, cqr, req) do {} while (0)
630#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#endif /* CONFIG_DASD_PROFILE */
632
633/*
634 * Allocate memory for a channel program with 'cplength' channel
635 * command words and 'datasize' additional space. There are two
636 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
637 * memory and 2) dasd_smalloc_request uses the static ccw memory
638 * that gets allocated for each device.
639 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100640struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
641 int datasize,
642 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct dasd_ccw_req *cqr;
645
646 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100647 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
648 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800650 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (cqr == NULL)
652 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 cqr->cpaddr = NULL;
654 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800655 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 GFP_ATOMIC | GFP_DMA);
657 if (cqr->cpaddr == NULL) {
658 kfree(cqr);
659 return ERR_PTR(-ENOMEM);
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 cqr->data = NULL;
663 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800664 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800666 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 kfree(cqr);
668 return ERR_PTR(-ENOMEM);
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 strncpy((char *) &cqr->magic, magic, 4);
672 ASCEBC((char *) &cqr->magic, 4);
673 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
674 dasd_get_device(device);
675 return cqr;
676}
677
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100678struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
679 int datasize,
680 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 unsigned long flags;
683 struct dasd_ccw_req *cqr;
684 char *data;
685 int size;
686
687 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100688 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
689 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
692 if (cplength > 0)
693 size += cplength * sizeof(struct ccw1);
694 if (datasize > 0)
695 size += datasize;
696 spin_lock_irqsave(&device->mem_lock, flags);
697 cqr = (struct dasd_ccw_req *)
698 dasd_alloc_chunk(&device->ccw_chunks, size);
699 spin_unlock_irqrestore(&device->mem_lock, flags);
700 if (cqr == NULL)
701 return ERR_PTR(-ENOMEM);
702 memset(cqr, 0, sizeof(struct dasd_ccw_req));
703 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
704 cqr->cpaddr = NULL;
705 if (cplength > 0) {
706 cqr->cpaddr = (struct ccw1 *) data;
707 data += cplength*sizeof(struct ccw1);
708 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
709 }
710 cqr->data = NULL;
711 if (datasize > 0) {
712 cqr->data = data;
713 memset(cqr->data, 0, datasize);
714 }
715 strncpy((char *) &cqr->magic, magic, 4);
716 ASCEBC((char *) &cqr->magic, 4);
717 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
718 dasd_get_device(device);
719 return cqr;
720}
721
722/*
723 * Free memory of a channel program. This function needs to free all the
724 * idal lists that might have been created by dasd_set_cda and the
725 * struct dasd_ccw_req itself.
726 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100727void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800729#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct ccw1 *ccw;
731
732 /* Clear any idals used for the request. */
733 ccw = cqr->cpaddr;
734 do {
735 clear_normalized_cda(ccw);
736 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
737#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800738 kfree(cqr->cpaddr);
739 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 kfree(cqr);
741 dasd_put_device(device);
742}
743
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100744void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 unsigned long flags;
747
748 spin_lock_irqsave(&device->mem_lock, flags);
749 dasd_free_chunk(&device->ccw_chunks, cqr);
750 spin_unlock_irqrestore(&device->mem_lock, flags);
751 dasd_put_device(device);
752}
753
754/*
755 * Check discipline magic in cqr.
756 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100757static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct dasd_device *device;
760
761 if (cqr == NULL)
762 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100763 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
765 DEV_MESSAGE(KERN_WARNING, device,
766 " dasd_ccw_req 0x%08x magic doesn't match"
767 " discipline 0x%08x",
768 cqr->magic,
769 *(unsigned int *) device->discipline->name);
770 return -EINVAL;
771 }
772 return 0;
773}
774
775/*
776 * Terminate the current i/o and set the request to clear_pending.
777 * Timer keeps device runnig.
778 * ccw_device_clear can fail if the i/o subsystem
779 * is in a bad mood.
780 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100781int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct dasd_device *device;
784 int retries, rc;
785
786 /* Check the cqr */
787 rc = dasd_check_cqr(cqr);
788 if (rc)
789 return rc;
790 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100791 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
793 rc = ccw_device_clear(device->cdev, (long) cqr);
794 switch (rc) {
795 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800796 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100797 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200799 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 DBF_DEV_EVENT(DBF_DEBUG, device,
801 "terminate cqr %p successful",
802 cqr);
803 break;
804 case -ENODEV:
805 DBF_DEV_EVENT(DBF_ERR, device, "%s",
806 "device gone, retry");
807 break;
808 case -EIO:
809 DBF_DEV_EVENT(DBF_ERR, device, "%s",
810 "I/O error, retry");
811 break;
812 case -EINVAL:
813 case -EBUSY:
814 DBF_DEV_EVENT(DBF_ERR, device, "%s",
815 "device busy, retry later");
816 break;
817 default:
818 DEV_MESSAGE(KERN_ERR, device,
819 "line %d unknown RC=%d, please "
820 "report to linux390@de.ibm.com",
821 __LINE__, rc);
822 BUG();
823 break;
824 }
825 retries++;
826 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100827 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return rc;
829}
830
831/*
832 * Start the i/o. This start_IO can fail if the channel is really busy.
833 * In that case set up a timer to start the request later.
834 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100835int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct dasd_device *device;
838 int rc;
839
840 /* Check the cqr */
841 rc = dasd_check_cqr(cqr);
842 if (rc)
843 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100844 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (cqr->retries < 0) {
846 DEV_MESSAGE(KERN_DEBUG, device,
847 "start_IO: request %p (%02x/%i) - no retry left.",
848 cqr, cqr->status, cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100849 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -EIO;
851 }
852 cqr->startclk = get_clock();
853 cqr->starttime = jiffies;
854 cqr->retries--;
855 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
856 cqr->lpm, 0);
857 switch (rc) {
858 case 0:
859 cqr->status = DASD_CQR_IN_IO;
860 DBF_DEV_EVENT(DBF_DEBUG, device,
861 "start_IO: request %p started successful",
862 cqr);
863 break;
864 case -EBUSY:
865 DBF_DEV_EVENT(DBF_ERR, device, "%s",
866 "start_IO: device busy, retry later");
867 break;
868 case -ETIMEDOUT:
869 DBF_DEV_EVENT(DBF_ERR, device, "%s",
870 "start_IO: request timeout, retry later");
871 break;
872 case -EACCES:
873 /* -EACCES indicates that the request used only a
874 * subset of the available pathes and all these
875 * pathes are gone.
876 * Do a retry with all available pathes.
877 */
878 cqr->lpm = LPM_ANYPATH;
879 DBF_DEV_EVENT(DBF_ERR, device, "%s",
880 "start_IO: selected pathes gone,"
881 " retry on all pathes");
882 break;
883 case -ENODEV:
884 case -EIO:
885 DBF_DEV_EVENT(DBF_ERR, device, "%s",
886 "start_IO: device gone, retry");
887 break;
888 default:
889 DEV_MESSAGE(KERN_ERR, device,
890 "line %d unknown RC=%d, please report"
891 " to linux390@de.ibm.com", __LINE__, rc);
892 BUG();
893 break;
894 }
895 return rc;
896}
897
898/*
899 * Timeout function for dasd devices. This is used for different purposes
900 * 1) missing interrupt handler for normal operation
901 * 2) delayed start of request where start_IO failed with -EBUSY
902 * 3) timeout for missing state change interrupts
903 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
904 * DASD_CQR_QUEUED for 2) and 3).
905 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100906static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 unsigned long flags;
909 struct dasd_device *device;
910
911 device = (struct dasd_device *) ptr;
912 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
913 /* re-activate request queue */
914 device->stopped &= ~DASD_STOPPED_PENDING;
915 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100916 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919/*
920 * Setup timeout for a device in jiffies.
921 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100922void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100924 if (expires == 0)
925 del_timer(&device->timer);
926 else
927 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
931 * Clear timeout for a device.
932 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100933void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100935 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100938static void dasd_handle_killed_request(struct ccw_device *cdev,
939 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct dasd_ccw_req *cqr;
942 struct dasd_device *device;
943
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200944 if (!intparm)
945 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 cqr = (struct dasd_ccw_req *) intparm;
947 if (cqr->status != DASD_CQR_IN_IO) {
948 MESSAGE(KERN_DEBUG,
949 "invalid status in handle_killed_request: "
950 "bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200951 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 }
954
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100955 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200957 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
959 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200960 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return;
962 }
963
964 /* Schedule request to be retried. */
965 cqr->status = DASD_CQR_QUEUED;
966
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100967 dasd_device_clear_timer(device);
968 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dasd_put_device(device);
970}
971
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100972void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800974 /* First of all start sense subsystem status request. */
975 dasd_eer_snss(device);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100978 dasd_schedule_device_bh(device);
979 if (device->block)
980 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Interrupt handler for "normal" ssch-io based dasd devices.
985 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100986void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
987 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct dasd_ccw_req *cqr, *next;
990 struct dasd_device *device;
991 unsigned long long now;
992 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (IS_ERR(irb)) {
995 switch (PTR_ERR(irb)) {
996 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998 case -ETIMEDOUT:
999 printk(KERN_WARNING"%s(%s): request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001000 __func__, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 default:
1003 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001004 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001006 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
1008 }
1009
1010 now = get_clock();
1011
1012 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001013 dev_name(&cdev->dev), ((irb->scsw.cmd.cstat << 8) |
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001014 irb->scsw.cmd.dstat), (unsigned int) intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001016 /* check for unsolicited interrupts */
1017 cqr = (struct dasd_ccw_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001018 if (!cqr || ((irb->scsw.cmd.cc == 1) &&
1019 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1020 (irb->scsw.cmd.stctl & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001021 if (cqr && cqr->status == DASD_CQR_IN_IO)
1022 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001023 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001025 dasd_device_clear_timer(device);
1026 device->discipline->handle_unsolicited_interrupt(device,
1027 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 dasd_put_device(device);
1029 }
1030 return;
1031 }
1032
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001033 device = (struct dasd_device *) cqr->startdev;
1034 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1036 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001037 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039 }
1040
1041 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001042 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001043 irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001044 cqr->status = DASD_CQR_CLEARED;
1045 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001046 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001047 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return;
1049 }
1050
1051 /* check status - the request might have been killed by dyn detach */
1052 if (cqr->status != DASD_CQR_IN_IO) {
1053 MESSAGE(KERN_DEBUG,
1054 "invalid status: bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001055 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return;
1057 }
1058 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001059 ((irb->scsw.cmd.cstat << 8) | irb->scsw.cmd.dstat), cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001060 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 expires = 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001062 if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1063 irb->scsw.cmd.cstat == 0 && !irb->esw.esw0.erw.cons) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001064 /* request was completed successfully */
1065 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 cqr->stopclk = now;
1067 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001068 if (cqr->devlist.next != &device->ccw_queue) {
1069 next = list_entry(cqr->devlist.next,
1070 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001072 } else { /* error */
1073 memcpy(&cqr->irb, irb, sizeof(struct irb));
Horst Hummel9575bf22006-12-08 15:54:15 +01001074 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001075 dasd_log_sense(cqr, irb);
1076 }
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001077 /*
1078 * If we don't want complex ERP for this request, then just
1079 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001080 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001081 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001082 cqr->retries > 0) {
1083 DEV_MESSAGE(KERN_DEBUG, device,
1084 "default ERP in fastpath (%i retries left)",
1085 cqr->retries);
1086 cqr->lpm = LPM_ANYPATH;
1087 cqr->status = DASD_CQR_QUEUED;
1088 next = cqr;
1089 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001091 }
1092 if (next && (next->status == DASD_CQR_QUEUED) &&
1093 (!device->stopped)) {
1094 if (device->discipline->start_IO(next) == 0)
1095 expires = next->expires;
1096 else
1097 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1098 "Interrupt fastpath "
1099 "failed!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001102 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001104 dasd_device_clear_timer(device);
1105 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001109 * If we have an error on a dasd_block layer request then we cancel
1110 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001112static void __dasd_device_recovery(struct dasd_device *device,
1113 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct list_head *l, *n;
1116 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001119 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001121 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001123
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001124 list_for_each_safe(l, n, &device->ccw_queue) {
1125 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1126 if (cqr->status == DASD_CQR_QUEUED &&
1127 ref_cqr->block == cqr->block) {
1128 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001130 }
1131};
1132
1133/*
1134 * Remove those ccw requests from the queue that need to be returned
1135 * to the upper layer.
1136 */
1137static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1138 struct list_head *final_queue)
1139{
1140 struct list_head *l, *n;
1141 struct dasd_ccw_req *cqr;
1142
1143 /* Process request with final status. */
1144 list_for_each_safe(l, n, &device->ccw_queue) {
1145 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1146
1147 /* Stop list processing at the first non-final request. */
1148 if (cqr->status == DASD_CQR_QUEUED ||
1149 cqr->status == DASD_CQR_IN_IO ||
1150 cqr->status == DASD_CQR_CLEAR_PENDING)
1151 break;
1152 if (cqr->status == DASD_CQR_ERROR) {
1153 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001155 /* Rechain finished requests to final queue */
1156 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158}
1159
1160/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001161 * the cqrs from the final queue are returned to the upper layer
1162 * by setting a dasd_block state and calling the callback function
1163 */
1164static void __dasd_device_process_final_queue(struct dasd_device *device,
1165 struct list_head *final_queue)
1166{
1167 struct list_head *l, *n;
1168 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001169 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001170 void (*callback)(struct dasd_ccw_req *, void *data);
1171 void *callback_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001172
1173 list_for_each_safe(l, n, final_queue) {
1174 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1175 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001176 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001177 callback = cqr->callback;
1178 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001179 if (block)
1180 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001181 switch (cqr->status) {
1182 case DASD_CQR_SUCCESS:
1183 cqr->status = DASD_CQR_DONE;
1184 break;
1185 case DASD_CQR_ERROR:
1186 cqr->status = DASD_CQR_NEED_ERP;
1187 break;
1188 case DASD_CQR_CLEARED:
1189 cqr->status = DASD_CQR_TERMINATED;
1190 break;
1191 default:
1192 DEV_MESSAGE(KERN_ERR, device,
1193 "wrong cqr status in __dasd_process_final_queue "
1194 "for cqr %p, status %x",
1195 cqr, cqr->status);
1196 BUG();
1197 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001198 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001199 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001200 if (block)
1201 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001202 }
1203}
1204
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * Take a look at the first request on the ccw queue and check
1207 * if it reached its expire time. If so, terminate the IO.
1208 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001209static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct dasd_ccw_req *cqr;
1212
1213 if (list_empty(&device->ccw_queue))
1214 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001215 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001216 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1217 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1218 if (device->discipline->term_IO(cqr) != 0) {
1219 /* Hmpf, try again in 5 sec */
Horst Hummel29145a62006-12-04 15:40:15 +01001220 DEV_MESSAGE(KERN_ERR, device,
1221 "internal error - timeout (%is) expired "
1222 "for cqr %p, termination failed, "
1223 "retrying in 5s",
1224 (cqr->expires/HZ), cqr);
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001225 cqr->expires += 5*HZ;
1226 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001227 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001228 DEV_MESSAGE(KERN_ERR, device,
1229 "internal error - timeout (%is) expired "
1230 "for cqr %p (%i retries left)",
1231 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 }
1234}
1235
1236/*
1237 * Take a look at the first request on the ccw queue and check
1238 * if it needs to be started.
1239 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001240static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct dasd_ccw_req *cqr;
1243 int rc;
1244
1245 if (list_empty(&device->ccw_queue))
1246 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001247 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001248 if (cqr->status != DASD_CQR_QUEUED)
1249 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001250 /* when device is stopped, return request to previous layer */
1251 if (device->stopped) {
1252 cqr->status = DASD_CQR_CLEARED;
1253 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001254 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001255 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001256
1257 rc = device->discipline->start_IO(cqr);
1258 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001259 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001260 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001261 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001262 } else
1263 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001264 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001268 * Go through all request on the dasd_device request queue,
1269 * terminate them on the cdev if necessary, and return them to the
1270 * submitting layer via callback.
1271 * Note:
1272 * Make sure that all 'submitting layers' still exist when
1273 * this function is called!. In other words, when 'device' is a base
1274 * device then all block layer requests must have been removed before
1275 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001279 struct dasd_ccw_req *cqr, *n;
1280 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 INIT_LIST_HEAD(&flush_queue);
1284 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001285 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001286 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001287 /* Check status and move request to flush_queue */
1288 switch (cqr->status) {
1289 case DASD_CQR_IN_IO:
1290 rc = device->discipline->term_IO(cqr);
1291 if (rc) {
1292 /* unable to terminate requeust */
1293 DEV_MESSAGE(KERN_ERR, device,
1294 "dasd flush ccw_queue is unable "
1295 " to terminate request %p",
1296 cqr);
1297 /* stop flush processing */
1298 goto finished;
1299 }
1300 break;
1301 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001303 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001304 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001305 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001306 break;
1307 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001308 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001309 }
Horst Hummel8f617012006-08-30 14:33:33 +02001310finished:
1311 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312 /*
1313 * After this point all requests must be in state CLEAR_PENDING,
1314 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1315 * one of the others.
1316 */
1317 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1318 wait_event(dasd_flush_wq,
1319 (cqr->status != DASD_CQR_CLEAR_PENDING));
1320 /*
1321 * Now set each request back to TERMINATED, DONE or NEED_ERP
1322 * and call the callback function of flushed requests
1323 */
1324 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001325 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328/*
1329 * Acquire the device lock and process queues for the device.
1330 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001331static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 atomic_set (&device->tasklet_scheduled, 0);
1336 INIT_LIST_HEAD(&final_queue);
1337 spin_lock_irq(get_ccwdev_lock(device->cdev));
1338 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001339 __dasd_device_check_expire(device);
1340 /* find final requests on ccw queue */
1341 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1343 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001344 __dasd_device_process_final_queue(device, &final_queue);
1345 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001347 __dasd_device_start_head(device);
1348 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 dasd_put_device(device);
1350}
1351
1352/*
1353 * Schedules a call to dasd_tasklet over the device tasklet.
1354 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001355void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001358 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return;
1360 dasd_get_device(device);
1361 tasklet_hi_schedule(&device->tasklet);
1362}
1363
1364/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001365 * Queue a request to the head of the device ccw_queue.
1366 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001368void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct dasd_device *device;
1371 unsigned long flags;
1372
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001373 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001375 cqr->status = DASD_CQR_QUEUED;
1376 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1380}
1381
1382/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001383 * Queue a request to the tail of the device ccw_queue.
1384 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001386void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct dasd_device *device;
1389 unsigned long flags;
1390
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001393 cqr->status = DASD_CQR_QUEUED;
1394 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001396 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1398}
1399
1400/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001401 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 wake_up((wait_queue_head_t *) data);
1406}
1407
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001408static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct dasd_device *device;
1411 int rc;
1412
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001413 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001415 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001416 cqr->status == DASD_CQR_NEED_ERP ||
1417 cqr->status == DASD_CQR_TERMINATED) &&
1418 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1420 return rc;
1421}
1422
1423/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001424 * Queue a request to the tail of the device ccw_queue and wait for
1425 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001427int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct dasd_device *device;
1430 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001431
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001432 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001435 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001437 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001440 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return rc;
1442}
1443
1444/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001445 * Queue a request to the tail of the device ccw_queue and wait
1446 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001448int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001451 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001453 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001455 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001456 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001457 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001458 if (rc == -ERESTARTSYS) {
1459 dasd_cancel_req(cqr);
1460 /* wait (non-interruptible) for final status */
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001461 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001463 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return rc;
1465}
1466
1467/*
1468 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1469 * for eckd devices) the currently running request has to be terminated
1470 * and be put back to status queued, before the special request is added
1471 * to the head of the queue. Then the special request is waited on normally.
1472 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001473static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (list_empty(&device->ccw_queue))
1478 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001479 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001480 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001483int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct dasd_device *device;
1486 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001487
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001488 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 spin_lock_irq(get_ccwdev_lock(device->cdev));
1490 rc = _dasd_term_running_cqr(device);
1491 if (rc) {
1492 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1493 return rc;
1494 }
Horst Hummel138c0142006-06-29 14:58:12 +02001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001497 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001499 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001502 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1505
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001506 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001509 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return rc;
1511}
1512
1513/*
1514 * Cancels a request that was started with dasd_sleep_on_req.
1515 * This is useful to timeout requests. The request will be
1516 * terminated if it is currently in i/o.
1517 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518 * 0 if there was no need to terminate the request (not started yet)
1519 * negative error code if termination failed
1520 * Cancellation of a request is an asynchronous operation! The calling
1521 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001523int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001525 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 unsigned long flags;
1527 int rc;
1528
1529 rc = 0;
1530 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1531 switch (cqr->status) {
1532 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001533 /* request was not started - just set to cleared */
1534 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 case DASD_CQR_IN_IO:
1537 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001538 rc = device->discipline->term_IO(cqr);
1539 if (rc) {
1540 DEV_MESSAGE(KERN_ERR, device,
1541 "dasd_cancel_req is unable "
1542 " to terminate request %p, rc = %d",
1543 cqr, rc);
1544 } else {
1545 cqr->stopclk = get_clock();
1546 rc = 1;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001549 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001553 dasd_schedule_device_bh(device);
1554 return rc;
1555}
1556
1557
1558/*
1559 * SECTION: Operations of the dasd_block layer.
1560 */
1561
1562/*
1563 * Timeout function for dasd_block. This is used when the block layer
1564 * is waiting for something that may not come reliably, (e.g. a state
1565 * change interrupt)
1566 */
1567static void dasd_block_timeout(unsigned long ptr)
1568{
1569 unsigned long flags;
1570 struct dasd_block *block;
1571
1572 block = (struct dasd_block *) ptr;
1573 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1574 /* re-activate request queue */
1575 block->base->stopped &= ~DASD_STOPPED_PENDING;
1576 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1577 dasd_schedule_block_bh(block);
1578}
1579
1580/*
1581 * Setup timeout for a dasd_block in jiffies.
1582 */
1583void dasd_block_set_timer(struct dasd_block *block, int expires)
1584{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001585 if (expires == 0)
1586 del_timer(&block->timer);
1587 else
1588 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001589}
1590
1591/*
1592 * Clear timeout for a dasd_block.
1593 */
1594void dasd_block_clear_timer(struct dasd_block *block)
1595{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001596 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001597}
1598
1599/*
1600 * posts the buffer_cache about a finalized request
1601 */
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001602static inline void dasd_end_request(struct request *req, int error)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001603{
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001604 if (__blk_end_request(req, error, blk_rq_bytes(req)))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001605 BUG();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001606}
1607
1608/*
1609 * Process finished error recovery ccw.
1610 */
1611static inline void __dasd_block_process_erp(struct dasd_block *block,
1612 struct dasd_ccw_req *cqr)
1613{
1614 dasd_erp_fn_t erp_fn;
1615 struct dasd_device *device = block->base;
1616
1617 if (cqr->status == DASD_CQR_DONE)
1618 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1619 else
1620 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1621 erp_fn = device->discipline->erp_postaction(cqr);
1622 erp_fn(cqr);
1623}
1624
1625/*
1626 * Fetch requests from the block device queue.
1627 */
1628static void __dasd_process_request_queue(struct dasd_block *block)
1629{
1630 struct request_queue *queue;
1631 struct request *req;
1632 struct dasd_ccw_req *cqr;
1633 struct dasd_device *basedev;
1634 unsigned long flags;
1635 queue = block->request_queue;
1636 basedev = block->base;
1637 /* No queue ? Then there is nothing to do. */
1638 if (queue == NULL)
1639 return;
1640
1641 /*
1642 * We requeue request from the block device queue to the ccw
1643 * queue only in two states. In state DASD_STATE_READY the
1644 * partition detection is done and we need to requeue requests
1645 * for that. State DASD_STATE_ONLINE is normal block device
1646 * operation.
1647 */
1648 if (basedev->state < DASD_STATE_READY)
1649 return;
1650 /* Now we try to fetch requests from the request queue */
1651 while (!blk_queue_plugged(queue) &&
1652 elv_next_request(queue)) {
1653
1654 req = elv_next_request(queue);
1655
1656 if (basedev->features & DASD_FEATURE_READONLY &&
1657 rq_data_dir(req) == WRITE) {
1658 DBF_DEV_EVENT(DBF_ERR, basedev,
1659 "Rejecting write request %p",
1660 req);
1661 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001662 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001663 continue;
1664 }
1665 cqr = basedev->discipline->build_cp(basedev, block, req);
1666 if (IS_ERR(cqr)) {
1667 if (PTR_ERR(cqr) == -EBUSY)
1668 break; /* normal end condition */
1669 if (PTR_ERR(cqr) == -ENOMEM)
1670 break; /* terminate request queue loop */
1671 if (PTR_ERR(cqr) == -EAGAIN) {
1672 /*
1673 * The current request cannot be build right
1674 * now, we have to try later. If this request
1675 * is the head-of-queue we stop the device
1676 * for 1/2 second.
1677 */
1678 if (!list_empty(&block->ccw_queue))
1679 break;
1680 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1681 basedev->stopped |= DASD_STOPPED_PENDING;
1682 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1683 dasd_block_set_timer(block, HZ/2);
1684 break;
1685 }
1686 DBF_DEV_EVENT(DBF_ERR, basedev,
1687 "CCW creation failed (rc=%ld) "
1688 "on request %p",
1689 PTR_ERR(cqr), req);
1690 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001691 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001692 continue;
1693 }
1694 /*
1695 * Note: callback is set to dasd_return_cqr_cb in
1696 * __dasd_block_start_head to cover erp requests as well
1697 */
1698 cqr->callback_data = (void *) req;
1699 cqr->status = DASD_CQR_FILLED;
1700 blkdev_dequeue_request(req);
1701 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1702 dasd_profile_start(block, cqr, req);
1703 }
1704}
1705
1706static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1707{
1708 struct request *req;
1709 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001710 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001711
1712 req = (struct request *) cqr->callback_data;
1713 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001714 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001715 if (status <= 0)
1716 error = status ? status : -EIO;
1717 dasd_end_request(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001718}
1719
1720/*
1721 * Process ccw request queue.
1722 */
1723static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1724 struct list_head *final_queue)
1725{
1726 struct list_head *l, *n;
1727 struct dasd_ccw_req *cqr;
1728 dasd_erp_fn_t erp_fn;
1729 unsigned long flags;
1730 struct dasd_device *base = block->base;
1731
1732restart:
1733 /* Process request with final status. */
1734 list_for_each_safe(l, n, &block->ccw_queue) {
1735 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1736 if (cqr->status != DASD_CQR_DONE &&
1737 cqr->status != DASD_CQR_FAILED &&
1738 cqr->status != DASD_CQR_NEED_ERP &&
1739 cqr->status != DASD_CQR_TERMINATED)
1740 continue;
1741
1742 if (cqr->status == DASD_CQR_TERMINATED) {
1743 base->discipline->handle_terminated_request(cqr);
1744 goto restart;
1745 }
1746
1747 /* Process requests that may be recovered */
1748 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001749 erp_fn = base->discipline->erp_action(cqr);
1750 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001751 goto restart;
1752 }
1753
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001754 /* log sense for fatal error */
1755 if (cqr->status == DASD_CQR_FAILED) {
1756 dasd_log_sense(cqr, &cqr->irb);
1757 }
1758
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001759 /* First of all call extended error reporting. */
1760 if (dasd_eer_enabled(base) &&
1761 cqr->status == DASD_CQR_FAILED) {
1762 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1763
1764 /* restart request */
1765 cqr->status = DASD_CQR_FILLED;
1766 cqr->retries = 255;
1767 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1768 base->stopped |= DASD_STOPPED_QUIESCE;
1769 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1770 flags);
1771 goto restart;
1772 }
1773
1774 /* Process finished ERP request. */
1775 if (cqr->refers) {
1776 __dasd_block_process_erp(block, cqr);
1777 goto restart;
1778 }
1779
1780 /* Rechain finished requests to final queue */
1781 cqr->endclk = get_clock();
1782 list_move_tail(&cqr->blocklist, final_queue);
1783 }
1784}
1785
1786static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1787{
1788 dasd_schedule_block_bh(cqr->block);
1789}
1790
1791static void __dasd_block_start_head(struct dasd_block *block)
1792{
1793 struct dasd_ccw_req *cqr;
1794
1795 if (list_empty(&block->ccw_queue))
1796 return;
1797 /* We allways begin with the first requests on the queue, as some
1798 * of previously started requests have to be enqueued on a
1799 * dasd_device again for error recovery.
1800 */
1801 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1802 if (cqr->status != DASD_CQR_FILLED)
1803 continue;
1804 /* Non-temporary stop condition will trigger fail fast */
1805 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1806 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1807 (!dasd_eer_enabled(block->base))) {
1808 cqr->status = DASD_CQR_FAILED;
1809 dasd_schedule_block_bh(block);
1810 continue;
1811 }
1812 /* Don't try to start requests if device is stopped */
1813 if (block->base->stopped)
1814 return;
1815
1816 /* just a fail safe check, should not happen */
1817 if (!cqr->startdev)
1818 cqr->startdev = block->base;
1819
1820 /* make sure that the requests we submit find their way back */
1821 cqr->callback = dasd_return_cqr_cb;
1822
1823 dasd_add_request_tail(cqr);
1824 }
1825}
1826
1827/*
1828 * Central dasd_block layer routine. Takes requests from the generic
1829 * block layer request queue, creates ccw requests, enqueues them on
1830 * a dasd_device and processes ccw requests that have been returned.
1831 */
1832static void dasd_block_tasklet(struct dasd_block *block)
1833{
1834 struct list_head final_queue;
1835 struct list_head *l, *n;
1836 struct dasd_ccw_req *cqr;
1837
1838 atomic_set(&block->tasklet_scheduled, 0);
1839 INIT_LIST_HEAD(&final_queue);
1840 spin_lock(&block->queue_lock);
1841 /* Finish off requests on ccw queue */
1842 __dasd_process_block_ccw_queue(block, &final_queue);
1843 spin_unlock(&block->queue_lock);
1844 /* Now call the callback function of requests with final status */
1845 spin_lock_irq(&block->request_queue_lock);
1846 list_for_each_safe(l, n, &final_queue) {
1847 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1848 list_del_init(&cqr->blocklist);
1849 __dasd_cleanup_cqr(cqr);
1850 }
1851 spin_lock(&block->queue_lock);
1852 /* Get new request from the block device request queue */
1853 __dasd_process_request_queue(block);
1854 /* Now check if the head of the ccw queue needs to be started. */
1855 __dasd_block_start_head(block);
1856 spin_unlock(&block->queue_lock);
1857 spin_unlock_irq(&block->request_queue_lock);
1858 dasd_put_device(block->base);
1859}
1860
1861static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1862{
1863 wake_up(&dasd_flush_wq);
1864}
1865
1866/*
1867 * Go through all request on the dasd_block request queue, cancel them
1868 * on the respective dasd_device, and return them to the generic
1869 * block layer.
1870 */
1871static int dasd_flush_block_queue(struct dasd_block *block)
1872{
1873 struct dasd_ccw_req *cqr, *n;
1874 int rc, i;
1875 struct list_head flush_queue;
1876
1877 INIT_LIST_HEAD(&flush_queue);
1878 spin_lock_bh(&block->queue_lock);
1879 rc = 0;
1880restart:
1881 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1882 /* if this request currently owned by a dasd_device cancel it */
1883 if (cqr->status >= DASD_CQR_QUEUED)
1884 rc = dasd_cancel_req(cqr);
1885 if (rc < 0)
1886 break;
1887 /* Rechain request (including erp chain) so it won't be
1888 * touched by the dasd_block_tasklet anymore.
1889 * Replace the callback so we notice when the request
1890 * is returned from the dasd_device layer.
1891 */
1892 cqr->callback = _dasd_wake_block_flush_cb;
1893 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1894 list_move_tail(&cqr->blocklist, &flush_queue);
1895 if (i > 1)
1896 /* moved more than one request - need to restart */
1897 goto restart;
1898 }
1899 spin_unlock_bh(&block->queue_lock);
1900 /* Now call the callback function of flushed requests */
1901restart_cb:
1902 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1903 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1904 /* Process finished ERP request. */
1905 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001906 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001907 __dasd_block_process_erp(block, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001908 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001909 /* restart list_for_xx loop since dasd_process_erp
1910 * might remove multiple elements */
1911 goto restart_cb;
1912 }
1913 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001914 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001915 cqr->endclk = get_clock();
1916 list_del_init(&cqr->blocklist);
1917 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001918 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return rc;
1921}
1922
1923/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924 * Schedules a call to dasd_tasklet over the device tasklet.
1925 */
1926void dasd_schedule_block_bh(struct dasd_block *block)
1927{
1928 /* Protect against rescheduling. */
1929 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1930 return;
1931 /* life cycle of block is bound to it's base device */
1932 dasd_get_device(block->base);
1933 tasklet_hi_schedule(&block->tasklet);
1934}
1935
1936
1937/*
1938 * SECTION: external block device operations
1939 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
1941
1942/*
1943 * Dasd request queue function. Called from ll_rw_blk.c
1944 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001945static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001947 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001949 block = queue->queuedata;
1950 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001952 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954 __dasd_block_start_head(block);
1955 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958/*
1959 * Allocate and initialize request queue and default I/O scheduler.
1960 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 int rc;
1964
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001965 block->request_queue = blk_init_queue(do_dasd_request,
1966 &block->request_queue_lock);
1967 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -ENOMEM;
1969
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001970 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001972 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02001973 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001974 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return rc;
1978 }
1979 return 0;
1980}
1981
1982/*
1983 * Allocate and initialize request queue.
1984 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001985static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 int max;
1988
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001989 blk_queue_hardsect_size(block->request_queue, block->bp_block);
1990 max = block->base->discipline->max_blocks << block->s2b_shift;
1991 blk_queue_max_sectors(block->request_queue, max);
1992 blk_queue_max_phys_segments(block->request_queue, -1L);
1993 blk_queue_max_hw_segments(block->request_queue, -1L);
1994 blk_queue_max_segment_size(block->request_queue, -1L);
1995 blk_queue_segment_boundary(block->request_queue, -1L);
1996 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999/*
2000 * Deactivate and free request queue.
2001 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002002static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002004 if (block->request_queue) {
2005 blk_cleanup_queue(block->request_queue);
2006 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008}
2009
2010/*
2011 * Flush request on the request queue.
2012 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct request *req;
2016
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002017 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002019
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002020 spin_lock_irq(&block->request_queue_lock);
2021 while ((req = elv_next_request(block->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002023 dasd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002025 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Al Viro57a7c0b2008-03-02 10:36:08 -05002028static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Al Viro57a7c0b2008-03-02 10:36:08 -05002030 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int rc;
2033
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 atomic_inc(&block->open_count);
2035 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 rc = -ENODEV;
2037 goto unlock;
2038 }
2039
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002040 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 rc = -EINVAL;
2042 goto unlock;
2043 }
2044
2045 if (dasd_probeonly) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002046 DEV_MESSAGE(KERN_INFO, base, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 "No access to device due to probeonly mode");
2048 rc = -EPERM;
2049 goto out;
2050 }
2051
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 if (base->state <= DASD_STATE_BASIC) {
2053 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 " Cannot open unrecognized device");
2055 rc = -ENODEV;
2056 goto out;
2057 }
2058
2059 return 0;
2060
2061out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return rc;
2066}
2067
Al Viro57a7c0b2008-03-02 10:36:08 -05002068static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002070 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 atomic_dec(&block->open_count);
2073 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075}
2076
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002077/*
2078 * Return disk geometry.
2079 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002080static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002081{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002082 struct dasd_block *block;
2083 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002084
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085 block = bdev->bd_disk->private_data;
2086 base = block->base;
2087 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002088 return -ENODEV;
2089
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002090 if (!base->discipline ||
2091 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002092 return -EINVAL;
2093
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094 base->discipline->fill_geometry(block, geo);
2095 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002096 return 0;
2097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099struct block_device_operations
2100dasd_device_operations = {
2101 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002102 .open = dasd_open,
2103 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002104 .ioctl = dasd_ioctl,
2105 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002106 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107};
2108
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002109/*******************************************************************************
2110 * end of block device operations
2111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113static void
2114dasd_exit(void)
2115{
2116#ifdef CONFIG_PROC_FS
2117 dasd_proc_exit();
2118#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002119 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002120 if (dasd_page_cache != NULL) {
2121 kmem_cache_destroy(dasd_page_cache);
2122 dasd_page_cache = NULL;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 dasd_gendisk_exit();
2125 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (dasd_debug_area != NULL) {
2127 debug_unregister(dasd_debug_area);
2128 dasd_debug_area = NULL;
2129 }
2130}
2131
2132/*
2133 * SECTION: common functions for ccw_driver use
2134 */
2135
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002136/*
2137 * Initial attempt at a probe function. this can be simplified once
2138 * the other detection code is gone.
2139 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002140int dasd_generic_probe(struct ccw_device *cdev,
2141 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int ret;
2144
Horst Hummel40545572006-06-29 15:08:18 +02002145 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2146 if (ret) {
2147 printk(KERN_WARNING
2148 "dasd_generic_probe: could not set ccw-device options "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002149 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002150 return ret;
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 ret = dasd_add_sysfs_files(cdev);
2153 if (ret) {
2154 printk(KERN_WARNING
2155 "dasd_generic_probe: could not add sysfs entries "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002156 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
Horst Hummel40545572006-06-29 15:08:18 +02002159 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Horst Hummel40545572006-06-29 15:08:18 +02002161 /*
2162 * Automatically online either all dasd devices (dasd_autodetect)
2163 * or all devices specified with dasd= parameters during
2164 * initial probe.
2165 */
2166 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002167 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Horst Hummel40545572006-06-29 15:08:18 +02002168 ret = ccw_device_set_online(cdev);
2169 if (ret)
2170 printk(KERN_WARNING
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002171 "dasd_generic_probe: could not initially "
2172 "online ccw-device %s; return code: %d\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002173 dev_name(&cdev->dev), ret);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002177/*
2178 * This will one day be called from a global not_oper handler.
2179 * It is also used by driver_unregister during module unload.
2180 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002181void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002184 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Horst Hummel59afda72005-05-16 21:53:39 -07002186 cdev->handler = NULL;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 dasd_remove_sysfs_files(cdev);
2189 device = dasd_device_from_cdev(cdev);
2190 if (IS_ERR(device))
2191 return;
2192 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2193 /* Already doing offline processing */
2194 dasd_put_device(device);
2195 return;
2196 }
2197 /*
2198 * This device is removed unconditionally. Set offline
2199 * flag to prevent dasd_open from opening it while it is
2200 * no quite down yet.
2201 */
2202 dasd_set_target_state(device, DASD_STATE_NEW);
2203 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002204 block = device->block;
2205 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002207 /*
2208 * life cycle of block is bound to device, so delete it after
2209 * device was safely removed
2210 */
2211 if (block)
2212 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002215/*
2216 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002218 * or the user has started activation through sysfs.
2219 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002220int dasd_generic_set_online(struct ccw_device *cdev,
2221 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002223 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002225 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002226
Horst Hummel40545572006-06-29 15:08:18 +02002227 /* first online clears initial online feature flag */
2228 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 device = dasd_create_device(cdev);
2230 if (IS_ERR(device))
2231 return PTR_ERR(device);
2232
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002233 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002234 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (!dasd_diag_discipline_pointer) {
2236 printk (KERN_WARNING
2237 "dasd_generic couldn't online device %s "
2238 "- discipline DIAG not available\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002239 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 dasd_delete_device(device);
2241 return -ENODEV;
2242 }
2243 discipline = dasd_diag_discipline_pointer;
2244 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002245 if (!try_module_get(base_discipline->owner)) {
2246 dasd_delete_device(device);
2247 return -EINVAL;
2248 }
2249 if (!try_module_get(discipline->owner)) {
2250 module_put(base_discipline->owner);
2251 dasd_delete_device(device);
2252 return -EINVAL;
2253 }
2254 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 device->discipline = discipline;
2256
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002257 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rc = discipline->check_device(device);
2259 if (rc) {
2260 printk (KERN_WARNING
2261 "dasd_generic couldn't online device %s "
2262 "with discipline %s rc=%i\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002263 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002264 module_put(discipline->owner);
2265 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 dasd_delete_device(device);
2267 return rc;
2268 }
2269
2270 dasd_set_target_state(device, DASD_STATE_ONLINE);
2271 if (device->state <= DASD_STATE_KNOWN) {
2272 printk (KERN_WARNING
2273 "dasd_generic discipline not found for %s\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002274 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 rc = -ENODEV;
2276 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002277 if (device->block)
2278 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 dasd_delete_device(device);
2280 } else
2281 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002282 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 /* FIXME: we have to wait for the root device but we don't want
2285 * to wait for each single device but for all at once. */
2286 wait_event(dasd_init_waitq, _wait_for_device(device));
2287
2288 dasd_put_device(device);
2289
2290 return rc;
2291}
2292
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002293int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002297 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 device = dasd_device_from_cdev(cdev);
2300 if (IS_ERR(device))
2301 return PTR_ERR(device);
2302 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2303 /* Already doing offline processing */
2304 dasd_put_device(device);
2305 return 0;
2306 }
2307 /*
2308 * We must make sure that this device is currently not in use.
2309 * The open_count is increased for every opener, that includes
2310 * the blkdev_get in dasd_scan_partitions. We are only interested
2311 * in the other openers.
2312 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002313 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002314 max_count = device->block->bdev ? 0 : -1;
2315 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002316 if (open_count > max_count) {
2317 if (open_count > 0)
2318 printk(KERN_WARNING "Can't offline dasd "
2319 "device with open count = %i.\n",
2320 open_count);
2321 else
2322 printk(KERN_WARNING "%s",
2323 "Can't offline dasd device due "
2324 "to internal use\n");
2325 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2326 dasd_put_device(device);
2327 return -EBUSY;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 dasd_set_target_state(device, DASD_STATE_NEW);
2331 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002332 block = device->block;
2333 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335 /*
2336 * life cycle of block is bound to device, so delete it after
2337 * device was safely removed
2338 */
2339 if (block)
2340 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
2343
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002344int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct dasd_device *device;
2347 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int ret;
2349
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002350 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (IS_ERR(device))
2352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 ret = 0;
2354 switch (event) {
2355 case CIO_GONE:
2356 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002357 /* First of all call extended error reporting. */
2358 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (device->state < DASD_STATE_BASIC)
2361 break;
2362 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002363 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2364 if (cqr->status == DASD_CQR_IN_IO) {
2365 cqr->status = DASD_CQR_QUEUED;
2366 cqr->retries++;
2367 }
2368 device->stopped |= DASD_STOPPED_DC_WAIT;
2369 dasd_device_clear_timer(device);
2370 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 ret = 1;
2372 break;
2373 case CIO_OPER:
2374 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002375 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2376 dasd_schedule_device_bh(device);
2377 if (device->block)
2378 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 ret = 1;
2380 break;
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 dasd_put_device(device);
2383 return ret;
2384}
2385
Heiko Carstens763968e2007-05-10 15:45:46 +02002386static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2387 void *rdc_buffer,
2388 int rdc_buffer_size,
2389 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002390{
2391 struct dasd_ccw_req *cqr;
2392 struct ccw1 *ccw;
2393
2394 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2395
2396 if (IS_ERR(cqr)) {
2397 DEV_MESSAGE(KERN_WARNING, device, "%s",
2398 "Could not allocate RDC request");
2399 return cqr;
2400 }
2401
2402 ccw = cqr->cpaddr;
2403 ccw->cmd_code = CCW_CMD_RDC;
2404 ccw->cda = (__u32)(addr_t)rdc_buffer;
2405 ccw->count = rdc_buffer_size;
2406
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002407 cqr->startdev = device;
2408 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002409 cqr->expires = 10*HZ;
2410 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2411 cqr->retries = 2;
2412 cqr->buildclk = get_clock();
2413 cqr->status = DASD_CQR_FILLED;
2414 return cqr;
2415}
2416
2417
2418int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2419 void **rdc_buffer, int rdc_buffer_size)
2420{
2421 int ret;
2422 struct dasd_ccw_req *cqr;
2423
2424 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2425 magic);
2426 if (IS_ERR(cqr))
2427 return PTR_ERR(cqr);
2428
2429 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002430 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002431 return ret;
2432}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002433EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002434
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002435static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 int rc;
2438
2439 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002440 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002441 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002444 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (dasd_debug_area == NULL) {
2446 rc = -ENOMEM;
2447 goto failed;
2448 }
2449 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002450 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2453
2454 dasd_diag_discipline_pointer = NULL;
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 rc = dasd_devmap_init();
2457 if (rc)
2458 goto failed;
2459 rc = dasd_gendisk_init();
2460 if (rc)
2461 goto failed;
2462 rc = dasd_parse();
2463 if (rc)
2464 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002465 rc = dasd_eer_init();
2466 if (rc)
2467 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468#ifdef CONFIG_PROC_FS
2469 rc = dasd_proc_init();
2470 if (rc)
2471 goto failed;
2472#endif
2473
2474 return 0;
2475failed:
2476 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2477 dasd_exit();
2478 return rc;
2479}
2480
2481module_init(dasd_init);
2482module_exit(dasd_exit);
2483
2484EXPORT_SYMBOL(dasd_debug_area);
2485EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2486
2487EXPORT_SYMBOL(dasd_add_request_head);
2488EXPORT_SYMBOL(dasd_add_request_tail);
2489EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002490EXPORT_SYMBOL(dasd_device_clear_timer);
2491EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492EXPORT_SYMBOL(dasd_enable_device);
2493EXPORT_SYMBOL(dasd_int_handler);
2494EXPORT_SYMBOL(dasd_kfree_request);
2495EXPORT_SYMBOL(dasd_kick_device);
2496EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002497EXPORT_SYMBOL(dasd_schedule_device_bh);
2498EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002500EXPORT_SYMBOL(dasd_device_set_timer);
2501EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502EXPORT_SYMBOL(dasd_sfree_request);
2503EXPORT_SYMBOL(dasd_sleep_on);
2504EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2505EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2506EXPORT_SYMBOL(dasd_smalloc_request);
2507EXPORT_SYMBOL(dasd_start_IO);
2508EXPORT_SYMBOL(dasd_term_IO);
2509
2510EXPORT_SYMBOL_GPL(dasd_generic_probe);
2511EXPORT_SYMBOL_GPL(dasd_generic_remove);
2512EXPORT_SYMBOL_GPL(dasd_generic_notify);
2513EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2514EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002515EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2516EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2517EXPORT_SYMBOL_GPL(dasd_alloc_block);
2518EXPORT_SYMBOL_GPL(dasd_free_block);