blob: bd5914994142707556b36b95fcb8b3e7042899fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kmod.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ctype.h>
16#include <linux/major.h>
17#include <linux/slab.h>
18#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/ebcdic.h>
23#include <asm/idals.h>
24#include <asm/todclk.h>
25
26/* This is ugly... */
27#define PRINTK_HEADER "dasd:"
28
29#include "dasd_int.h"
30/*
31 * SECTION: Constant definitions to be used within this file
32 */
33#define DASD_CHANQ_MAX_SIZE 4
34
35/*
36 * SECTION: exported variables of dasd.c
37 */
38debug_info_t *dasd_debug_area;
39struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010040void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
43MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
44 " Copyright 2000 IBM Corporation");
45MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_LICENSE("GPL");
47
48/*
49 * SECTION: prototypes for static functions of dasd.c
50 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010051static int dasd_alloc_queue(struct dasd_block *);
52static void dasd_setup_queue(struct dasd_block *);
53static void dasd_free_queue(struct dasd_block *);
54static void dasd_flush_request_queue(struct dasd_block *);
55static int dasd_flush_block_queue(struct dasd_block *);
56static void dasd_device_tasklet(struct dasd_device *);
57static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000058static void do_kick_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010059static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * SECTION: Operations on the device structure.
63 */
64static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020065static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020066static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * Allocate memory for a new device structure.
70 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010071struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct dasd_device *device;
74
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010075 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
76 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* Get two pages for normal block device operations. */
80 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010081 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 kfree(device);
83 return ERR_PTR(-ENOMEM);
84 }
85 /* Get one page for error recovery. */
86 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010087 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 free_pages((unsigned long) device->ccw_mem, 1);
89 kfree(device);
90 return ERR_PTR(-ENOMEM);
91 }
92
93 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
94 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
95 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010096 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +020097 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010098 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 (unsigned long) device);
100 INIT_LIST_HEAD(&device->ccw_queue);
101 init_timer(&device->timer);
Al Viro4927b3f2006-12-06 19:18:20 +0000102 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 device->state = DASD_STATE_NEW;
104 device->target = DASD_STATE_NEW;
105
106 return device;
107}
108
109/*
110 * Free memory of a device structure.
111 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100112void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800114 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 free_page((unsigned long) device->erp_mem);
116 free_pages((unsigned long) device->ccw_mem, 1);
117 kfree(device);
118}
119
120/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100121 * Allocate memory for a new device structure.
122 */
123struct dasd_block *dasd_alloc_block(void)
124{
125 struct dasd_block *block;
126
127 block = kzalloc(sizeof(*block), GFP_ATOMIC);
128 if (!block)
129 return ERR_PTR(-ENOMEM);
130 /* open_count = 0 means device online but not in use */
131 atomic_set(&block->open_count, -1);
132
133 spin_lock_init(&block->request_queue_lock);
134 atomic_set(&block->tasklet_scheduled, 0);
135 tasklet_init(&block->tasklet,
136 (void (*)(unsigned long)) dasd_block_tasklet,
137 (unsigned long) block);
138 INIT_LIST_HEAD(&block->ccw_queue);
139 spin_lock_init(&block->queue_lock);
140 init_timer(&block->timer);
141
142 return block;
143}
144
145/*
146 * Free memory of a device structure.
147 */
148void dasd_free_block(struct dasd_block *block)
149{
150 kfree(block);
151}
152
153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Make a new device known to the system.
155 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100156static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int rc;
159
160 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200161 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * keep the reference count > 0.
163 */
164 dasd_get_device(device);
165
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100166 if (device->block) {
167 rc = dasd_alloc_queue(device->block);
168 if (rc) {
169 dasd_put_device(device);
170 return rc;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 device->state = DASD_STATE_KNOWN;
174 return 0;
175}
176
177/*
178 * Let the system forget about a device.
179 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100180static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800182 /* Disable extended error reporting for this device. */
183 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100185 if (device->discipline) {
186 if (device->discipline->uncheck_device)
187 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800188 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800191 if (device->base_discipline)
192 module_put(device->base_discipline->owner);
193 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 device->state = DASD_STATE_NEW;
195
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100196 if (device->block)
197 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Give up reference we took in dasd_state_new_to_known. */
200 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
205 * Request the irq line for the device.
206 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100207static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int rc;
210
211 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (device->block) {
213 rc = dasd_gendisk_alloc(device->block);
214 if (rc)
215 return rc;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Kay Sievers2a0217d2008-10-10 21:33:09 +0200218 device->debug_area = debug_register(dev_name(&device->cdev->dev), 1, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200221 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
223
224 device->state = DASD_STATE_BASIC;
225 return 0;
226}
227
228/*
229 * Release the irq line for the device. Terminate any running i/o.
230 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100231static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Horst Hummel8f617012006-08-30 14:33:33 +0200233 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100234 if (device->block) {
235 dasd_gendisk_free(device->block);
236 dasd_block_clear_timer(device->block);
237 }
238 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200239 if (rc)
240 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
244 if (device->debug_area != NULL) {
245 debug_unregister(device->debug_area);
246 device->debug_area = NULL;
247 }
248 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Do the initial analysis. The do_analysis function may return
254 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
255 * until the discipline decides to continue the startup sequence
256 * by calling the function dasd_change_state. The eckd disciplines
257 * uses this to start a ccw that detects the format. The completion
258 * interrupt for this detection ccw uses the kernel event daemon to
259 * trigger the call to dasd_change_state. All this is done in the
260 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800261 * After the analysis ccw is done (do_analysis returned 0) the block
262 * device is setup.
263 * In case the analysis returns an error, the device setup is stopped
264 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100266static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100269 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100272 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800273 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100274 if (block) {
275 if (block->base->discipline->do_analysis != NULL)
276 rc = block->base->discipline->do_analysis(block);
277 if (rc) {
278 if (rc != -EAGAIN)
279 device->state = DASD_STATE_UNFMT;
280 return rc;
281 }
282 dasd_setup_queue(block);
283 set_capacity(block->gdp,
284 block->blocks << block->s2b_shift);
285 device->state = DASD_STATE_READY;
286 rc = dasd_scan_partitions(block);
287 if (rc)
288 device->state = DASD_STATE_BASIC;
289 } else {
290 device->state = DASD_STATE_READY;
291 }
Horst Hummel90f00942006-03-07 21:55:39 -0800292 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * Remove device from block device layer. Destroy dirty buffers.
297 * Forget format information. Check if the target level is basic
298 * and if it is create fake disk for formatting.
299 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100300static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Horst Hummel8f617012006-08-30 14:33:33 +0200302 int rc;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100305 if (device->block) {
306 struct dasd_block *block = device->block;
307 rc = dasd_flush_block_queue(block);
308 if (rc) {
309 device->state = DASD_STATE_READY;
310 return rc;
311 }
312 dasd_destroy_partitions(block);
313 dasd_flush_request_queue(block);
314 block->blocks = 0;
315 block->bp_block = 0;
316 block->s2b_shift = 0;
317 }
Horst Hummel8f617012006-08-30 14:33:33 +0200318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
Horst Hummel90f00942006-03-07 21:55:39 -0800322 * Back to basic.
323 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100324static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800325{
326 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200327 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800328}
329
330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Make the device online and schedule the bottom half to start
332 * the requeueing of requests from the linux request queue to the
333 * ccw queue.
334 */
Horst Hummel8f617012006-08-30 14:33:33 +0200335static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336dasd_state_ready_to_online(struct dasd_device * device)
337{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100338 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100339 struct gendisk *disk;
340 struct disk_part_iter piter;
341 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342
343 if (device->discipline->ready_to_online) {
344 rc = device->discipline->ready_to_online(device);
345 if (rc)
346 return rc;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100349 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100351 disk = device->block->bdev->bd_disk;
352 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
353 while ((part = disk_part_iter_next(&piter)))
354 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
355 disk_part_iter_exit(&piter);
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360/*
361 * Stop the requeueing of requests again.
362 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100363static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100365 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100366 struct gendisk *disk;
367 struct disk_part_iter piter;
368 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100369
370 if (device->discipline->online_to_ready) {
371 rc = device->discipline->online_to_ready(device);
372 if (rc)
373 return rc;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100376 if (device->block) {
377 disk = device->block->bdev->bd_disk;
378 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
379 while ((part = disk_part_iter_next(&piter)))
380 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
381 disk_part_iter_exit(&piter);
382 }
Horst Hummel8f617012006-08-30 14:33:33 +0200383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/*
387 * Device startup state changes.
388 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100389static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int rc;
392
393 rc = 0;
394 if (device->state == DASD_STATE_NEW &&
395 device->target >= DASD_STATE_KNOWN)
396 rc = dasd_state_new_to_known(device);
397
398 if (!rc &&
399 device->state == DASD_STATE_KNOWN &&
400 device->target >= DASD_STATE_BASIC)
401 rc = dasd_state_known_to_basic(device);
402
403 if (!rc &&
404 device->state == DASD_STATE_BASIC &&
405 device->target >= DASD_STATE_READY)
406 rc = dasd_state_basic_to_ready(device);
407
408 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700409 device->state == DASD_STATE_UNFMT &&
410 device->target > DASD_STATE_UNFMT)
411 rc = -EPERM;
412
413 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 device->state == DASD_STATE_READY &&
415 device->target >= DASD_STATE_ONLINE)
416 rc = dasd_state_ready_to_online(device);
417
418 return rc;
419}
420
421/*
422 * Device shutdown state changes.
423 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100424static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Horst Hummel8f617012006-08-30 14:33:33 +0200426 int rc;
427
428 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (device->state == DASD_STATE_ONLINE &&
430 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200431 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200432
Horst Hummel8f617012006-08-30 14:33:33 +0200433 if (!rc &&
434 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200436 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800437
Horst Hummel8f617012006-08-30 14:33:33 +0200438 if (!rc &&
439 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800440 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200441 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800442
Horst Hummel8f617012006-08-30 14:33:33 +0200443 if (!rc &&
444 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200446 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200447
Horst Hummel8f617012006-08-30 14:33:33 +0200448 if (!rc &&
449 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200451 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Horst Hummel8f617012006-08-30 14:33:33 +0200453 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456/*
457 * This is the main startup/shutdown routine.
458 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100459static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 int rc;
462
463 if (device->state == device->target)
464 /* Already where we want to go today... */
465 return;
466 if (device->state < device->target)
467 rc = dasd_increase_state(device);
468 else
469 rc = dasd_decrease_state(device);
470 if (rc && rc != -EAGAIN)
471 device->target = device->state;
472
473 if (device->state == device->target)
474 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200475
476 /* let user-space know that the device status changed */
477 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/*
481 * Kick starter for devices that did not complete the startup/shutdown
482 * procedure or were sleeping because of a pending state.
483 * dasd_kick_device will schedule a call do do_kick_device to the kernel
484 * event daemon.
485 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100486static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Al Viro4927b3f2006-12-06 19:18:20 +0000488 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100490 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dasd_put_device(device);
492}
493
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100494void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 dasd_get_device(device);
497 /* queue call to dasd_kick_device to the kernel event daemon. */
498 schedule_work(&device->kick_work);
499}
500
501/*
502 * Set the target state for a device and starts the state change.
503 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 /* If we are in probeonly mode stop at DASD_STATE_READY. */
507 if (dasd_probeonly && target > DASD_STATE_READY)
508 target = DASD_STATE_READY;
509 if (device->target != target) {
510 if (device->state == target)
511 wake_up(&dasd_init_waitq);
512 device->target = target;
513 }
514 if (device->state != device->target)
515 dasd_change_state(device);
516}
517
518/*
519 * Enable devices with device numbers in [from..to].
520 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100521static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 return (device->state == device->target);
524}
525
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100526void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 dasd_set_target_state(device, DASD_STATE_ONLINE);
529 if (device->state <= DASD_STATE_KNOWN)
530 /* No discipline for device found. */
531 dasd_set_target_state(device, DASD_STATE_NEW);
532 /* Now wait for the devices to come up. */
533 wait_event(dasd_init_waitq, _wait_for_device(device));
534}
535
536/*
537 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
538 */
539#ifdef CONFIG_DASD_PROFILE
540
541struct dasd_profile_info_t dasd_global_profile;
542unsigned int dasd_profile_level = DASD_PROFILE_OFF;
543
544/*
545 * Increments counter in global and local profiling structures.
546 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100547#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{ \
549 int index; \
550 for (index = 0; index < 31 && value >> (2+index); index++); \
551 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100552 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
556 * Add profiling information for cqr before execution.
557 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100558static void dasd_profile_start(struct dasd_block *block,
559 struct dasd_ccw_req *cqr,
560 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct list_head *l;
563 unsigned int counter;
564
565 if (dasd_profile_level != DASD_PROFILE_ON)
566 return;
567
568 /* count the length of the chanq for statistics */
569 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100570 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (++counter >= 31)
572 break;
573 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100574 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * Add profiling information for cqr after execution.
579 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100580static void dasd_profile_end(struct dasd_block *block,
581 struct dasd_ccw_req *cqr,
582 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 long strtime, irqtime, endtime, tottime; /* in microseconds */
585 long tottimeps, sectors;
586
587 if (dasd_profile_level != DASD_PROFILE_ON)
588 return;
589
590 sectors = req->nr_sectors;
591 if (!cqr->buildclk || !cqr->startclk ||
592 !cqr->stopclk || !cqr->endclk ||
593 !sectors)
594 return;
595
596 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
597 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
598 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
599 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
600 tottimeps = tottime / sectors;
601
602 if (!dasd_global_profile.dasd_io_reqs)
603 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100604 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dasd_global_profile.dasd_io_reqs++;
606 dasd_global_profile.dasd_io_sects += sectors;
607
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100608 if (!block->profile.dasd_io_reqs)
609 memset(&block->profile, 0,
610 sizeof(struct dasd_profile_info_t));
611 block->profile.dasd_io_reqs++;
612 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100614 dasd_profile_counter(sectors, dasd_io_secs, block);
615 dasd_profile_counter(tottime, dasd_io_times, block);
616 dasd_profile_counter(tottimeps, dasd_io_timps, block);
617 dasd_profile_counter(strtime, dasd_io_time1, block);
618 dasd_profile_counter(irqtime, dasd_io_time2, block);
619 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
620 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100623#define dasd_profile_start(block, cqr, req) do {} while (0)
624#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif /* CONFIG_DASD_PROFILE */
626
627/*
628 * Allocate memory for a channel program with 'cplength' channel
629 * command words and 'datasize' additional space. There are two
630 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
631 * memory and 2) dasd_smalloc_request uses the static ccw memory
632 * that gets allocated for each device.
633 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100634struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
635 int datasize,
636 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct dasd_ccw_req *cqr;
639
640 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100641 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
642 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800644 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (cqr == NULL)
646 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 cqr->cpaddr = NULL;
648 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800649 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 GFP_ATOMIC | GFP_DMA);
651 if (cqr->cpaddr == NULL) {
652 kfree(cqr);
653 return ERR_PTR(-ENOMEM);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 cqr->data = NULL;
657 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800658 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800660 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 kfree(cqr);
662 return ERR_PTR(-ENOMEM);
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 strncpy((char *) &cqr->magic, magic, 4);
666 ASCEBC((char *) &cqr->magic, 4);
667 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
668 dasd_get_device(device);
669 return cqr;
670}
671
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100672struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
673 int datasize,
674 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 unsigned long flags;
677 struct dasd_ccw_req *cqr;
678 char *data;
679 int size;
680
681 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100682 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
683 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
686 if (cplength > 0)
687 size += cplength * sizeof(struct ccw1);
688 if (datasize > 0)
689 size += datasize;
690 spin_lock_irqsave(&device->mem_lock, flags);
691 cqr = (struct dasd_ccw_req *)
692 dasd_alloc_chunk(&device->ccw_chunks, size);
693 spin_unlock_irqrestore(&device->mem_lock, flags);
694 if (cqr == NULL)
695 return ERR_PTR(-ENOMEM);
696 memset(cqr, 0, sizeof(struct dasd_ccw_req));
697 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
698 cqr->cpaddr = NULL;
699 if (cplength > 0) {
700 cqr->cpaddr = (struct ccw1 *) data;
701 data += cplength*sizeof(struct ccw1);
702 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
703 }
704 cqr->data = NULL;
705 if (datasize > 0) {
706 cqr->data = data;
707 memset(cqr->data, 0, datasize);
708 }
709 strncpy((char *) &cqr->magic, magic, 4);
710 ASCEBC((char *) &cqr->magic, 4);
711 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
712 dasd_get_device(device);
713 return cqr;
714}
715
716/*
717 * Free memory of a channel program. This function needs to free all the
718 * idal lists that might have been created by dasd_set_cda and the
719 * struct dasd_ccw_req itself.
720 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100721void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800723#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct ccw1 *ccw;
725
726 /* Clear any idals used for the request. */
727 ccw = cqr->cpaddr;
728 do {
729 clear_normalized_cda(ccw);
730 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
731#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800732 kfree(cqr->cpaddr);
733 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 kfree(cqr);
735 dasd_put_device(device);
736}
737
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100738void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned long flags;
741
742 spin_lock_irqsave(&device->mem_lock, flags);
743 dasd_free_chunk(&device->ccw_chunks, cqr);
744 spin_unlock_irqrestore(&device->mem_lock, flags);
745 dasd_put_device(device);
746}
747
748/*
749 * Check discipline magic in cqr.
750 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100751static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct dasd_device *device;
754
755 if (cqr == NULL)
756 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100757 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
759 DEV_MESSAGE(KERN_WARNING, device,
760 " dasd_ccw_req 0x%08x magic doesn't match"
761 " discipline 0x%08x",
762 cqr->magic,
763 *(unsigned int *) device->discipline->name);
764 return -EINVAL;
765 }
766 return 0;
767}
768
769/*
770 * Terminate the current i/o and set the request to clear_pending.
771 * Timer keeps device runnig.
772 * ccw_device_clear can fail if the i/o subsystem
773 * is in a bad mood.
774 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100775int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct dasd_device *device;
778 int retries, rc;
779
780 /* Check the cqr */
781 rc = dasd_check_cqr(cqr);
782 if (rc)
783 return rc;
784 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100785 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
787 rc = ccw_device_clear(device->cdev, (long) cqr);
788 switch (rc) {
789 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800790 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100791 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200793 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 DBF_DEV_EVENT(DBF_DEBUG, device,
795 "terminate cqr %p successful",
796 cqr);
797 break;
798 case -ENODEV:
799 DBF_DEV_EVENT(DBF_ERR, device, "%s",
800 "device gone, retry");
801 break;
802 case -EIO:
803 DBF_DEV_EVENT(DBF_ERR, device, "%s",
804 "I/O error, retry");
805 break;
806 case -EINVAL:
807 case -EBUSY:
808 DBF_DEV_EVENT(DBF_ERR, device, "%s",
809 "device busy, retry later");
810 break;
811 default:
812 DEV_MESSAGE(KERN_ERR, device,
813 "line %d unknown RC=%d, please "
814 "report to linux390@de.ibm.com",
815 __LINE__, rc);
816 BUG();
817 break;
818 }
819 retries++;
820 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100821 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return rc;
823}
824
825/*
826 * Start the i/o. This start_IO can fail if the channel is really busy.
827 * In that case set up a timer to start the request later.
828 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100829int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct dasd_device *device;
832 int rc;
833
834 /* Check the cqr */
835 rc = dasd_check_cqr(cqr);
836 if (rc)
837 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100838 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (cqr->retries < 0) {
840 DEV_MESSAGE(KERN_DEBUG, device,
841 "start_IO: request %p (%02x/%i) - no retry left.",
842 cqr, cqr->status, cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100843 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EIO;
845 }
846 cqr->startclk = get_clock();
847 cqr->starttime = jiffies;
848 cqr->retries--;
849 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
850 cqr->lpm, 0);
851 switch (rc) {
852 case 0:
853 cqr->status = DASD_CQR_IN_IO;
854 DBF_DEV_EVENT(DBF_DEBUG, device,
855 "start_IO: request %p started successful",
856 cqr);
857 break;
858 case -EBUSY:
859 DBF_DEV_EVENT(DBF_ERR, device, "%s",
860 "start_IO: device busy, retry later");
861 break;
862 case -ETIMEDOUT:
863 DBF_DEV_EVENT(DBF_ERR, device, "%s",
864 "start_IO: request timeout, retry later");
865 break;
866 case -EACCES:
867 /* -EACCES indicates that the request used only a
868 * subset of the available pathes and all these
869 * pathes are gone.
870 * Do a retry with all available pathes.
871 */
872 cqr->lpm = LPM_ANYPATH;
873 DBF_DEV_EVENT(DBF_ERR, device, "%s",
874 "start_IO: selected pathes gone,"
875 " retry on all pathes");
876 break;
877 case -ENODEV:
878 case -EIO:
879 DBF_DEV_EVENT(DBF_ERR, device, "%s",
880 "start_IO: device gone, retry");
881 break;
882 default:
883 DEV_MESSAGE(KERN_ERR, device,
884 "line %d unknown RC=%d, please report"
885 " to linux390@de.ibm.com", __LINE__, rc);
886 BUG();
887 break;
888 }
889 return rc;
890}
891
892/*
893 * Timeout function for dasd devices. This is used for different purposes
894 * 1) missing interrupt handler for normal operation
895 * 2) delayed start of request where start_IO failed with -EBUSY
896 * 3) timeout for missing state change interrupts
897 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
898 * DASD_CQR_QUEUED for 2) and 3).
899 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100900static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 unsigned long flags;
903 struct dasd_device *device;
904
905 device = (struct dasd_device *) ptr;
906 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
907 /* re-activate request queue */
908 device->stopped &= ~DASD_STOPPED_PENDING;
909 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100910 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * Setup timeout for a device in jiffies.
915 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100916void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (expires == 0) {
919 if (timer_pending(&device->timer))
920 del_timer(&device->timer);
921 return;
922 }
923 if (timer_pending(&device->timer)) {
924 if (mod_timer(&device->timer, jiffies + expires))
925 return;
926 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100927 device->timer.function = dasd_device_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 device->timer.data = (unsigned long) device;
929 device->timer.expires = jiffies + expires;
930 add_timer(&device->timer);
931}
932
933/*
934 * Clear timeout for a device.
935 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100936void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 if (timer_pending(&device->timer))
939 del_timer(&device->timer);
940}
941
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100942static void dasd_handle_killed_request(struct ccw_device *cdev,
943 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct dasd_ccw_req *cqr;
946 struct dasd_device *device;
947
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200948 if (!intparm)
949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 cqr = (struct dasd_ccw_req *) intparm;
951 if (cqr->status != DASD_CQR_IN_IO) {
952 MESSAGE(KERN_DEBUG,
953 "invalid status in handle_killed_request: "
954 "bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200955 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return;
957 }
958
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100959 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200961 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
963 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200964 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return;
966 }
967
968 /* Schedule request to be retried. */
969 cqr->status = DASD_CQR_QUEUED;
970
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100971 dasd_device_clear_timer(device);
972 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dasd_put_device(device);
974}
975
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100976void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800978 /* First of all start sense subsystem status request. */
979 dasd_eer_snss(device);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100982 dasd_schedule_device_bh(device);
983 if (device->block)
984 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987/*
988 * Interrupt handler for "normal" ssch-io based dasd devices.
989 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100990void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
991 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct dasd_ccw_req *cqr, *next;
994 struct dasd_device *device;
995 unsigned long long now;
996 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 if (IS_ERR(irb)) {
999 switch (PTR_ERR(irb)) {
1000 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 case -ETIMEDOUT:
1003 printk(KERN_WARNING"%s(%s): request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001004 __func__, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006 default:
1007 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001008 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001010 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return;
1012 }
1013
1014 now = get_clock();
1015
1016 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001017 dev_name(&cdev->dev), ((irb->scsw.cmd.cstat << 8) |
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001018 irb->scsw.cmd.dstat), (unsigned int) intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001020 /* check for unsolicited interrupts */
1021 cqr = (struct dasd_ccw_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001022 if (!cqr || ((irb->scsw.cmd.cc == 1) &&
1023 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1024 (irb->scsw.cmd.stctl & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001025 if (cqr && cqr->status == DASD_CQR_IN_IO)
1026 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001027 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001029 dasd_device_clear_timer(device);
1030 device->discipline->handle_unsolicited_interrupt(device,
1031 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dasd_put_device(device);
1033 }
1034 return;
1035 }
1036
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001037 device = (struct dasd_device *) cqr->startdev;
1038 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1040 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001041 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return;
1043 }
1044
1045 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001046 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001047 irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001048 cqr->status = DASD_CQR_CLEARED;
1049 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001050 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001051 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053 }
1054
1055 /* check status - the request might have been killed by dyn detach */
1056 if (cqr->status != DASD_CQR_IN_IO) {
1057 MESSAGE(KERN_DEBUG,
1058 "invalid status: bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001059 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return;
1061 }
1062 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001063 ((irb->scsw.cmd.cstat << 8) | irb->scsw.cmd.dstat), cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001064 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 expires = 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001066 if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1067 irb->scsw.cmd.cstat == 0 && !irb->esw.esw0.erw.cons) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001068 /* request was completed successfully */
1069 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 cqr->stopclk = now;
1071 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001072 if (cqr->devlist.next != &device->ccw_queue) {
1073 next = list_entry(cqr->devlist.next,
1074 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001076 } else { /* error */
1077 memcpy(&cqr->irb, irb, sizeof(struct irb));
Horst Hummel9575bf22006-12-08 15:54:15 +01001078 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001079 dasd_log_sense(cqr, irb);
1080 }
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001081 /*
1082 * If we don't want complex ERP for this request, then just
1083 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001084 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001085 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001086 cqr->retries > 0) {
1087 DEV_MESSAGE(KERN_DEBUG, device,
1088 "default ERP in fastpath (%i retries left)",
1089 cqr->retries);
1090 cqr->lpm = LPM_ANYPATH;
1091 cqr->status = DASD_CQR_QUEUED;
1092 next = cqr;
1093 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001095 }
1096 if (next && (next->status == DASD_CQR_QUEUED) &&
1097 (!device->stopped)) {
1098 if (device->discipline->start_IO(next) == 0)
1099 expires = next->expires;
1100 else
1101 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1102 "Interrupt fastpath "
1103 "failed!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001106 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001108 dasd_device_clear_timer(device);
1109 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001113 * If we have an error on a dasd_block layer request then we cancel
1114 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001116static void __dasd_device_recovery(struct dasd_device *device,
1117 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct list_head *l, *n;
1120 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001123 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001125 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001127
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001128 list_for_each_safe(l, n, &device->ccw_queue) {
1129 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1130 if (cqr->status == DASD_CQR_QUEUED &&
1131 ref_cqr->block == cqr->block) {
1132 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001134 }
1135};
1136
1137/*
1138 * Remove those ccw requests from the queue that need to be returned
1139 * to the upper layer.
1140 */
1141static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1142 struct list_head *final_queue)
1143{
1144 struct list_head *l, *n;
1145 struct dasd_ccw_req *cqr;
1146
1147 /* Process request with final status. */
1148 list_for_each_safe(l, n, &device->ccw_queue) {
1149 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1150
1151 /* Stop list processing at the first non-final request. */
1152 if (cqr->status == DASD_CQR_QUEUED ||
1153 cqr->status == DASD_CQR_IN_IO ||
1154 cqr->status == DASD_CQR_CLEAR_PENDING)
1155 break;
1156 if (cqr->status == DASD_CQR_ERROR) {
1157 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001159 /* Rechain finished requests to final queue */
1160 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162}
1163
1164/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001165 * the cqrs from the final queue are returned to the upper layer
1166 * by setting a dasd_block state and calling the callback function
1167 */
1168static void __dasd_device_process_final_queue(struct dasd_device *device,
1169 struct list_head *final_queue)
1170{
1171 struct list_head *l, *n;
1172 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001173 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001174 void (*callback)(struct dasd_ccw_req *, void *data);
1175 void *callback_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001176
1177 list_for_each_safe(l, n, final_queue) {
1178 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1179 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001180 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001181 callback = cqr->callback;
1182 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001183 if (block)
1184 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001185 switch (cqr->status) {
1186 case DASD_CQR_SUCCESS:
1187 cqr->status = DASD_CQR_DONE;
1188 break;
1189 case DASD_CQR_ERROR:
1190 cqr->status = DASD_CQR_NEED_ERP;
1191 break;
1192 case DASD_CQR_CLEARED:
1193 cqr->status = DASD_CQR_TERMINATED;
1194 break;
1195 default:
1196 DEV_MESSAGE(KERN_ERR, device,
1197 "wrong cqr status in __dasd_process_final_queue "
1198 "for cqr %p, status %x",
1199 cqr, cqr->status);
1200 BUG();
1201 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001202 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001203 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001204 if (block)
1205 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001206 }
1207}
1208
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * Take a look at the first request on the ccw queue and check
1211 * if it reached its expire time. If so, terminate the IO.
1212 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001213static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct dasd_ccw_req *cqr;
1216
1217 if (list_empty(&device->ccw_queue))
1218 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001219 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001220 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1221 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1222 if (device->discipline->term_IO(cqr) != 0) {
1223 /* Hmpf, try again in 5 sec */
Horst Hummel29145a62006-12-04 15:40:15 +01001224 DEV_MESSAGE(KERN_ERR, device,
1225 "internal error - timeout (%is) expired "
1226 "for cqr %p, termination failed, "
1227 "retrying in 5s",
1228 (cqr->expires/HZ), cqr);
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001229 cqr->expires += 5*HZ;
1230 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001231 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001232 DEV_MESSAGE(KERN_ERR, device,
1233 "internal error - timeout (%is) expired "
1234 "for cqr %p (%i retries left)",
1235 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 }
1238}
1239
1240/*
1241 * Take a look at the first request on the ccw queue and check
1242 * if it needs to be started.
1243 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001244static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct dasd_ccw_req *cqr;
1247 int rc;
1248
1249 if (list_empty(&device->ccw_queue))
1250 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001251 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001252 if (cqr->status != DASD_CQR_QUEUED)
1253 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001254 /* when device is stopped, return request to previous layer */
1255 if (device->stopped) {
1256 cqr->status = DASD_CQR_CLEARED;
1257 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001258 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001259 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001260
1261 rc = device->discipline->start_IO(cqr);
1262 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001263 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001264 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001265 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001266 } else
1267 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001268 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001272 * Go through all request on the dasd_device request queue,
1273 * terminate them on the cdev if necessary, and return them to the
1274 * submitting layer via callback.
1275 * Note:
1276 * Make sure that all 'submitting layers' still exist when
1277 * this function is called!. In other words, when 'device' is a base
1278 * device then all block layer requests must have been removed before
1279 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001281int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001283 struct dasd_ccw_req *cqr, *n;
1284 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 INIT_LIST_HEAD(&flush_queue);
1288 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001289 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001290 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001291 /* Check status and move request to flush_queue */
1292 switch (cqr->status) {
1293 case DASD_CQR_IN_IO:
1294 rc = device->discipline->term_IO(cqr);
1295 if (rc) {
1296 /* unable to terminate requeust */
1297 DEV_MESSAGE(KERN_ERR, device,
1298 "dasd flush ccw_queue is unable "
1299 " to terminate request %p",
1300 cqr);
1301 /* stop flush processing */
1302 goto finished;
1303 }
1304 break;
1305 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001307 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001308 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001309 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001310 break;
1311 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001313 }
Horst Hummel8f617012006-08-30 14:33:33 +02001314finished:
1315 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 /*
1317 * After this point all requests must be in state CLEAR_PENDING,
1318 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1319 * one of the others.
1320 */
1321 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1322 wait_event(dasd_flush_wq,
1323 (cqr->status != DASD_CQR_CLEAR_PENDING));
1324 /*
1325 * Now set each request back to TERMINATED, DONE or NEED_ERP
1326 * and call the callback function of flushed requests
1327 */
1328 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001329 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332/*
1333 * Acquire the device lock and process queues for the device.
1334 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001335static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 atomic_set (&device->tasklet_scheduled, 0);
1340 INIT_LIST_HEAD(&final_queue);
1341 spin_lock_irq(get_ccwdev_lock(device->cdev));
1342 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001343 __dasd_device_check_expire(device);
1344 /* find final requests on ccw queue */
1345 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1347 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001348 __dasd_device_process_final_queue(device, &final_queue);
1349 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001351 __dasd_device_start_head(device);
1352 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 dasd_put_device(device);
1354}
1355
1356/*
1357 * Schedules a call to dasd_tasklet over the device tasklet.
1358 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001362 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return;
1364 dasd_get_device(device);
1365 tasklet_hi_schedule(&device->tasklet);
1366}
1367
1368/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001369 * Queue a request to the head of the device ccw_queue.
1370 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001372void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct dasd_device *device;
1375 unsigned long flags;
1376
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001377 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001379 cqr->status = DASD_CQR_QUEUED;
1380 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001382 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1384}
1385
1386/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387 * Queue a request to the tail of the device ccw_queue.
1388 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001390void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct dasd_device *device;
1393 unsigned long flags;
1394
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001395 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001397 cqr->status = DASD_CQR_QUEUED;
1398 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001400 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1402}
1403
1404/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001405 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001407static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 wake_up((wait_queue_head_t *) data);
1410}
1411
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001412static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct dasd_device *device;
1415 int rc;
1416
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001417 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001419 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001420 cqr->status == DASD_CQR_NEED_ERP ||
1421 cqr->status == DASD_CQR_TERMINATED) &&
1422 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1424 return rc;
1425}
1426
1427/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001428 * Queue a request to the tail of the device ccw_queue and wait for
1429 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001431int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct dasd_device *device;
1434 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001435
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001439 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001440 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001441 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return rc;
1446}
1447
1448/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001449 * Queue a request to the tail of the device ccw_queue and wait
1450 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001452int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001455 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001457 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001459 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001461 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001462 if (rc == -ERESTARTSYS) {
1463 dasd_cancel_req(cqr);
1464 /* wait (non-interruptible) for final status */
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001465 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001467 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return rc;
1469}
1470
1471/*
1472 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1473 * for eckd devices) the currently running request has to be terminated
1474 * and be put back to status queued, before the special request is added
1475 * to the head of the queue. Then the special request is waited on normally.
1476 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001477static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (list_empty(&device->ccw_queue))
1482 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001483 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001484 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001487int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct dasd_device *device;
1490 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001491
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001492 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 spin_lock_irq(get_ccwdev_lock(device->cdev));
1494 rc = _dasd_term_running_cqr(device);
1495 if (rc) {
1496 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1497 return rc;
1498 }
Horst Hummel138c0142006-06-29 14:58:12 +02001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001501 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001503 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001506 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1509
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001510 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001513 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return rc;
1515}
1516
1517/*
1518 * Cancels a request that was started with dasd_sleep_on_req.
1519 * This is useful to timeout requests. The request will be
1520 * terminated if it is currently in i/o.
1521 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001522 * 0 if there was no need to terminate the request (not started yet)
1523 * negative error code if termination failed
1524 * Cancellation of a request is an asynchronous operation! The calling
1525 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001527int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001529 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 unsigned long flags;
1531 int rc;
1532
1533 rc = 0;
1534 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1535 switch (cqr->status) {
1536 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001537 /* request was not started - just set to cleared */
1538 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540 case DASD_CQR_IN_IO:
1541 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001542 rc = device->discipline->term_IO(cqr);
1543 if (rc) {
1544 DEV_MESSAGE(KERN_ERR, device,
1545 "dasd_cancel_req is unable "
1546 " to terminate request %p, rc = %d",
1547 cqr, rc);
1548 } else {
1549 cqr->stopclk = get_clock();
1550 rc = 1;
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001553 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001557 dasd_schedule_device_bh(device);
1558 return rc;
1559}
1560
1561
1562/*
1563 * SECTION: Operations of the dasd_block layer.
1564 */
1565
1566/*
1567 * Timeout function for dasd_block. This is used when the block layer
1568 * is waiting for something that may not come reliably, (e.g. a state
1569 * change interrupt)
1570 */
1571static void dasd_block_timeout(unsigned long ptr)
1572{
1573 unsigned long flags;
1574 struct dasd_block *block;
1575
1576 block = (struct dasd_block *) ptr;
1577 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1578 /* re-activate request queue */
1579 block->base->stopped &= ~DASD_STOPPED_PENDING;
1580 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1581 dasd_schedule_block_bh(block);
1582}
1583
1584/*
1585 * Setup timeout for a dasd_block in jiffies.
1586 */
1587void dasd_block_set_timer(struct dasd_block *block, int expires)
1588{
1589 if (expires == 0) {
1590 if (timer_pending(&block->timer))
1591 del_timer(&block->timer);
1592 return;
1593 }
1594 if (timer_pending(&block->timer)) {
1595 if (mod_timer(&block->timer, jiffies + expires))
1596 return;
1597 }
1598 block->timer.function = dasd_block_timeout;
1599 block->timer.data = (unsigned long) block;
1600 block->timer.expires = jiffies + expires;
1601 add_timer(&block->timer);
1602}
1603
1604/*
1605 * Clear timeout for a dasd_block.
1606 */
1607void dasd_block_clear_timer(struct dasd_block *block)
1608{
1609 if (timer_pending(&block->timer))
1610 del_timer(&block->timer);
1611}
1612
1613/*
1614 * posts the buffer_cache about a finalized request
1615 */
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001616static inline void dasd_end_request(struct request *req, int error)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001617{
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001618 if (__blk_end_request(req, error, blk_rq_bytes(req)))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001619 BUG();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001620}
1621
1622/*
1623 * Process finished error recovery ccw.
1624 */
1625static inline void __dasd_block_process_erp(struct dasd_block *block,
1626 struct dasd_ccw_req *cqr)
1627{
1628 dasd_erp_fn_t erp_fn;
1629 struct dasd_device *device = block->base;
1630
1631 if (cqr->status == DASD_CQR_DONE)
1632 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1633 else
1634 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1635 erp_fn = device->discipline->erp_postaction(cqr);
1636 erp_fn(cqr);
1637}
1638
1639/*
1640 * Fetch requests from the block device queue.
1641 */
1642static void __dasd_process_request_queue(struct dasd_block *block)
1643{
1644 struct request_queue *queue;
1645 struct request *req;
1646 struct dasd_ccw_req *cqr;
1647 struct dasd_device *basedev;
1648 unsigned long flags;
1649 queue = block->request_queue;
1650 basedev = block->base;
1651 /* No queue ? Then there is nothing to do. */
1652 if (queue == NULL)
1653 return;
1654
1655 /*
1656 * We requeue request from the block device queue to the ccw
1657 * queue only in two states. In state DASD_STATE_READY the
1658 * partition detection is done and we need to requeue requests
1659 * for that. State DASD_STATE_ONLINE is normal block device
1660 * operation.
1661 */
1662 if (basedev->state < DASD_STATE_READY)
1663 return;
1664 /* Now we try to fetch requests from the request queue */
1665 while (!blk_queue_plugged(queue) &&
1666 elv_next_request(queue)) {
1667
1668 req = elv_next_request(queue);
1669
1670 if (basedev->features & DASD_FEATURE_READONLY &&
1671 rq_data_dir(req) == WRITE) {
1672 DBF_DEV_EVENT(DBF_ERR, basedev,
1673 "Rejecting write request %p",
1674 req);
1675 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001676 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001677 continue;
1678 }
1679 cqr = basedev->discipline->build_cp(basedev, block, req);
1680 if (IS_ERR(cqr)) {
1681 if (PTR_ERR(cqr) == -EBUSY)
1682 break; /* normal end condition */
1683 if (PTR_ERR(cqr) == -ENOMEM)
1684 break; /* terminate request queue loop */
1685 if (PTR_ERR(cqr) == -EAGAIN) {
1686 /*
1687 * The current request cannot be build right
1688 * now, we have to try later. If this request
1689 * is the head-of-queue we stop the device
1690 * for 1/2 second.
1691 */
1692 if (!list_empty(&block->ccw_queue))
1693 break;
1694 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1695 basedev->stopped |= DASD_STOPPED_PENDING;
1696 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1697 dasd_block_set_timer(block, HZ/2);
1698 break;
1699 }
1700 DBF_DEV_EVENT(DBF_ERR, basedev,
1701 "CCW creation failed (rc=%ld) "
1702 "on request %p",
1703 PTR_ERR(cqr), req);
1704 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001705 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 continue;
1707 }
1708 /*
1709 * Note: callback is set to dasd_return_cqr_cb in
1710 * __dasd_block_start_head to cover erp requests as well
1711 */
1712 cqr->callback_data = (void *) req;
1713 cqr->status = DASD_CQR_FILLED;
1714 blkdev_dequeue_request(req);
1715 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1716 dasd_profile_start(block, cqr, req);
1717 }
1718}
1719
1720static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1721{
1722 struct request *req;
1723 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001724 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001725
1726 req = (struct request *) cqr->callback_data;
1727 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001728 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001729 if (status <= 0)
1730 error = status ? status : -EIO;
1731 dasd_end_request(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001732}
1733
1734/*
1735 * Process ccw request queue.
1736 */
1737static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1738 struct list_head *final_queue)
1739{
1740 struct list_head *l, *n;
1741 struct dasd_ccw_req *cqr;
1742 dasd_erp_fn_t erp_fn;
1743 unsigned long flags;
1744 struct dasd_device *base = block->base;
1745
1746restart:
1747 /* Process request with final status. */
1748 list_for_each_safe(l, n, &block->ccw_queue) {
1749 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1750 if (cqr->status != DASD_CQR_DONE &&
1751 cqr->status != DASD_CQR_FAILED &&
1752 cqr->status != DASD_CQR_NEED_ERP &&
1753 cqr->status != DASD_CQR_TERMINATED)
1754 continue;
1755
1756 if (cqr->status == DASD_CQR_TERMINATED) {
1757 base->discipline->handle_terminated_request(cqr);
1758 goto restart;
1759 }
1760
1761 /* Process requests that may be recovered */
1762 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001763 erp_fn = base->discipline->erp_action(cqr);
1764 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001765 goto restart;
1766 }
1767
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001768 /* log sense for fatal error */
1769 if (cqr->status == DASD_CQR_FAILED) {
1770 dasd_log_sense(cqr, &cqr->irb);
1771 }
1772
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773 /* First of all call extended error reporting. */
1774 if (dasd_eer_enabled(base) &&
1775 cqr->status == DASD_CQR_FAILED) {
1776 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1777
1778 /* restart request */
1779 cqr->status = DASD_CQR_FILLED;
1780 cqr->retries = 255;
1781 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1782 base->stopped |= DASD_STOPPED_QUIESCE;
1783 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1784 flags);
1785 goto restart;
1786 }
1787
1788 /* Process finished ERP request. */
1789 if (cqr->refers) {
1790 __dasd_block_process_erp(block, cqr);
1791 goto restart;
1792 }
1793
1794 /* Rechain finished requests to final queue */
1795 cqr->endclk = get_clock();
1796 list_move_tail(&cqr->blocklist, final_queue);
1797 }
1798}
1799
1800static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1801{
1802 dasd_schedule_block_bh(cqr->block);
1803}
1804
1805static void __dasd_block_start_head(struct dasd_block *block)
1806{
1807 struct dasd_ccw_req *cqr;
1808
1809 if (list_empty(&block->ccw_queue))
1810 return;
1811 /* We allways begin with the first requests on the queue, as some
1812 * of previously started requests have to be enqueued on a
1813 * dasd_device again for error recovery.
1814 */
1815 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1816 if (cqr->status != DASD_CQR_FILLED)
1817 continue;
1818 /* Non-temporary stop condition will trigger fail fast */
1819 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1820 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1821 (!dasd_eer_enabled(block->base))) {
1822 cqr->status = DASD_CQR_FAILED;
1823 dasd_schedule_block_bh(block);
1824 continue;
1825 }
1826 /* Don't try to start requests if device is stopped */
1827 if (block->base->stopped)
1828 return;
1829
1830 /* just a fail safe check, should not happen */
1831 if (!cqr->startdev)
1832 cqr->startdev = block->base;
1833
1834 /* make sure that the requests we submit find their way back */
1835 cqr->callback = dasd_return_cqr_cb;
1836
1837 dasd_add_request_tail(cqr);
1838 }
1839}
1840
1841/*
1842 * Central dasd_block layer routine. Takes requests from the generic
1843 * block layer request queue, creates ccw requests, enqueues them on
1844 * a dasd_device and processes ccw requests that have been returned.
1845 */
1846static void dasd_block_tasklet(struct dasd_block *block)
1847{
1848 struct list_head final_queue;
1849 struct list_head *l, *n;
1850 struct dasd_ccw_req *cqr;
1851
1852 atomic_set(&block->tasklet_scheduled, 0);
1853 INIT_LIST_HEAD(&final_queue);
1854 spin_lock(&block->queue_lock);
1855 /* Finish off requests on ccw queue */
1856 __dasd_process_block_ccw_queue(block, &final_queue);
1857 spin_unlock(&block->queue_lock);
1858 /* Now call the callback function of requests with final status */
1859 spin_lock_irq(&block->request_queue_lock);
1860 list_for_each_safe(l, n, &final_queue) {
1861 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1862 list_del_init(&cqr->blocklist);
1863 __dasd_cleanup_cqr(cqr);
1864 }
1865 spin_lock(&block->queue_lock);
1866 /* Get new request from the block device request queue */
1867 __dasd_process_request_queue(block);
1868 /* Now check if the head of the ccw queue needs to be started. */
1869 __dasd_block_start_head(block);
1870 spin_unlock(&block->queue_lock);
1871 spin_unlock_irq(&block->request_queue_lock);
1872 dasd_put_device(block->base);
1873}
1874
1875static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1876{
1877 wake_up(&dasd_flush_wq);
1878}
1879
1880/*
1881 * Go through all request on the dasd_block request queue, cancel them
1882 * on the respective dasd_device, and return them to the generic
1883 * block layer.
1884 */
1885static int dasd_flush_block_queue(struct dasd_block *block)
1886{
1887 struct dasd_ccw_req *cqr, *n;
1888 int rc, i;
1889 struct list_head flush_queue;
1890
1891 INIT_LIST_HEAD(&flush_queue);
1892 spin_lock_bh(&block->queue_lock);
1893 rc = 0;
1894restart:
1895 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1896 /* if this request currently owned by a dasd_device cancel it */
1897 if (cqr->status >= DASD_CQR_QUEUED)
1898 rc = dasd_cancel_req(cqr);
1899 if (rc < 0)
1900 break;
1901 /* Rechain request (including erp chain) so it won't be
1902 * touched by the dasd_block_tasklet anymore.
1903 * Replace the callback so we notice when the request
1904 * is returned from the dasd_device layer.
1905 */
1906 cqr->callback = _dasd_wake_block_flush_cb;
1907 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1908 list_move_tail(&cqr->blocklist, &flush_queue);
1909 if (i > 1)
1910 /* moved more than one request - need to restart */
1911 goto restart;
1912 }
1913 spin_unlock_bh(&block->queue_lock);
1914 /* Now call the callback function of flushed requests */
1915restart_cb:
1916 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1917 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1918 /* Process finished ERP request. */
1919 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001920 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001921 __dasd_block_process_erp(block, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001922 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001923 /* restart list_for_xx loop since dasd_process_erp
1924 * might remove multiple elements */
1925 goto restart_cb;
1926 }
1927 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001928 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001929 cqr->endclk = get_clock();
1930 list_del_init(&cqr->blocklist);
1931 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001932 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return rc;
1935}
1936
1937/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001938 * Schedules a call to dasd_tasklet over the device tasklet.
1939 */
1940void dasd_schedule_block_bh(struct dasd_block *block)
1941{
1942 /* Protect against rescheduling. */
1943 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1944 return;
1945 /* life cycle of block is bound to it's base device */
1946 dasd_get_device(block->base);
1947 tasklet_hi_schedule(&block->tasklet);
1948}
1949
1950
1951/*
1952 * SECTION: external block device operations
1953 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
1955
1956/*
1957 * Dasd request queue function. Called from ll_rw_blk.c
1958 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001959static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001963 block = queue->queuedata;
1964 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001966 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001968 __dasd_block_start_head(block);
1969 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*
1973 * Allocate and initialize request queue and default I/O scheduler.
1974 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001975static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 int rc;
1978
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001979 block->request_queue = blk_init_queue(do_dasd_request,
1980 &block->request_queue_lock);
1981 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return -ENOMEM;
1983
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001984 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001986 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02001987 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001988 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001990 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return rc;
1992 }
1993 return 0;
1994}
1995
1996/*
1997 * Allocate and initialize request queue.
1998 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001999static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 int max;
2002
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002003 blk_queue_hardsect_size(block->request_queue, block->bp_block);
2004 max = block->base->discipline->max_blocks << block->s2b_shift;
2005 blk_queue_max_sectors(block->request_queue, max);
2006 blk_queue_max_phys_segments(block->request_queue, -1L);
2007 blk_queue_max_hw_segments(block->request_queue, -1L);
2008 blk_queue_max_segment_size(block->request_queue, -1L);
2009 blk_queue_segment_boundary(block->request_queue, -1L);
2010 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013/*
2014 * Deactivate and free request queue.
2015 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002018 if (block->request_queue) {
2019 blk_cleanup_queue(block->request_queue);
2020 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022}
2023
2024/*
2025 * Flush request on the request queue.
2026 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002027static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct request *req;
2030
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002033
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 spin_lock_irq(&block->request_queue_lock);
2035 while ((req = elv_next_request(block->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002037 dasd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002039 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Al Viro57a7c0b2008-03-02 10:36:08 -05002042static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Al Viro57a7c0b2008-03-02 10:36:08 -05002044 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002045 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int rc;
2047
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002048 atomic_inc(&block->open_count);
2049 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rc = -ENODEV;
2051 goto unlock;
2052 }
2053
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rc = -EINVAL;
2056 goto unlock;
2057 }
2058
2059 if (dasd_probeonly) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002060 DEV_MESSAGE(KERN_INFO, base, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 "No access to device due to probeonly mode");
2062 rc = -EPERM;
2063 goto out;
2064 }
2065
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002066 if (base->state <= DASD_STATE_BASIC) {
2067 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 " Cannot open unrecognized device");
2069 rc = -ENODEV;
2070 goto out;
2071 }
2072
2073 return 0;
2074
2075out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002076 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002078 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return rc;
2080}
2081
Al Viro57a7c0b2008-03-02 10:36:08 -05002082static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002086 atomic_dec(&block->open_count);
2087 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 0;
2089}
2090
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002091/*
2092 * Return disk geometry.
2093 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002095{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002096 struct dasd_block *block;
2097 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002098
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002099 block = bdev->bd_disk->private_data;
2100 base = block->base;
2101 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002102 return -ENODEV;
2103
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002104 if (!base->discipline ||
2105 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002106 return -EINVAL;
2107
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002108 base->discipline->fill_geometry(block, geo);
2109 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002110 return 0;
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113struct block_device_operations
2114dasd_device_operations = {
2115 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002116 .open = dasd_open,
2117 .release = dasd_release,
2118 .locked_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002119 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120};
2121
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002122/*******************************************************************************
2123 * end of block device operations
2124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126static void
2127dasd_exit(void)
2128{
2129#ifdef CONFIG_PROC_FS
2130 dasd_proc_exit();
2131#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002132 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002133 if (dasd_page_cache != NULL) {
2134 kmem_cache_destroy(dasd_page_cache);
2135 dasd_page_cache = NULL;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 dasd_gendisk_exit();
2138 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (dasd_debug_area != NULL) {
2140 debug_unregister(dasd_debug_area);
2141 dasd_debug_area = NULL;
2142 }
2143}
2144
2145/*
2146 * SECTION: common functions for ccw_driver use
2147 */
2148
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002149/*
2150 * Initial attempt at a probe function. this can be simplified once
2151 * the other detection code is gone.
2152 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002153int dasd_generic_probe(struct ccw_device *cdev,
2154 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 int ret;
2157
Horst Hummel40545572006-06-29 15:08:18 +02002158 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2159 if (ret) {
2160 printk(KERN_WARNING
2161 "dasd_generic_probe: could not set ccw-device options "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002162 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002163 return ret;
2164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 ret = dasd_add_sysfs_files(cdev);
2166 if (ret) {
2167 printk(KERN_WARNING
2168 "dasd_generic_probe: could not add sysfs entries "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002169 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Horst Hummel40545572006-06-29 15:08:18 +02002172 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Horst Hummel40545572006-06-29 15:08:18 +02002174 /*
2175 * Automatically online either all dasd devices (dasd_autodetect)
2176 * or all devices specified with dasd= parameters during
2177 * initial probe.
2178 */
2179 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002180 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Horst Hummel40545572006-06-29 15:08:18 +02002181 ret = ccw_device_set_online(cdev);
2182 if (ret)
2183 printk(KERN_WARNING
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002184 "dasd_generic_probe: could not initially "
2185 "online ccw-device %s; return code: %d\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002186 dev_name(&cdev->dev), ret);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002190/*
2191 * This will one day be called from a global not_oper handler.
2192 * It is also used by driver_unregister during module unload.
2193 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002194void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002197 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Horst Hummel59afda72005-05-16 21:53:39 -07002199 cdev->handler = NULL;
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 dasd_remove_sysfs_files(cdev);
2202 device = dasd_device_from_cdev(cdev);
2203 if (IS_ERR(device))
2204 return;
2205 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2206 /* Already doing offline processing */
2207 dasd_put_device(device);
2208 return;
2209 }
2210 /*
2211 * This device is removed unconditionally. Set offline
2212 * flag to prevent dasd_open from opening it while it is
2213 * no quite down yet.
2214 */
2215 dasd_set_target_state(device, DASD_STATE_NEW);
2216 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002217 block = device->block;
2218 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002220 /*
2221 * life cycle of block is bound to device, so delete it after
2222 * device was safely removed
2223 */
2224 if (block)
2225 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002228/*
2229 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002231 * or the user has started activation through sysfs.
2232 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002233int dasd_generic_set_online(struct ccw_device *cdev,
2234 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002236 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002238 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002239
Horst Hummel40545572006-06-29 15:08:18 +02002240 /* first online clears initial online feature flag */
2241 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 device = dasd_create_device(cdev);
2243 if (IS_ERR(device))
2244 return PTR_ERR(device);
2245
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002246 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002247 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!dasd_diag_discipline_pointer) {
2249 printk (KERN_WARNING
2250 "dasd_generic couldn't online device %s "
2251 "- discipline DIAG not available\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002252 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 dasd_delete_device(device);
2254 return -ENODEV;
2255 }
2256 discipline = dasd_diag_discipline_pointer;
2257 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002258 if (!try_module_get(base_discipline->owner)) {
2259 dasd_delete_device(device);
2260 return -EINVAL;
2261 }
2262 if (!try_module_get(discipline->owner)) {
2263 module_put(base_discipline->owner);
2264 dasd_delete_device(device);
2265 return -EINVAL;
2266 }
2267 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 device->discipline = discipline;
2269
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002270 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 rc = discipline->check_device(device);
2272 if (rc) {
2273 printk (KERN_WARNING
2274 "dasd_generic couldn't online device %s "
2275 "with discipline %s rc=%i\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002276 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002277 module_put(discipline->owner);
2278 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 dasd_delete_device(device);
2280 return rc;
2281 }
2282
2283 dasd_set_target_state(device, DASD_STATE_ONLINE);
2284 if (device->state <= DASD_STATE_KNOWN) {
2285 printk (KERN_WARNING
2286 "dasd_generic discipline not found for %s\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002287 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 rc = -ENODEV;
2289 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002290 if (device->block)
2291 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 dasd_delete_device(device);
2293 } else
2294 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002295 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /* FIXME: we have to wait for the root device but we don't want
2298 * to wait for each single device but for all at once. */
2299 wait_event(dasd_init_waitq, _wait_for_device(device));
2300
2301 dasd_put_device(device);
2302
2303 return rc;
2304}
2305
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002306int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002309 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002310 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 device = dasd_device_from_cdev(cdev);
2313 if (IS_ERR(device))
2314 return PTR_ERR(device);
2315 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2316 /* Already doing offline processing */
2317 dasd_put_device(device);
2318 return 0;
2319 }
2320 /*
2321 * We must make sure that this device is currently not in use.
2322 * The open_count is increased for every opener, that includes
2323 * the blkdev_get in dasd_scan_partitions. We are only interested
2324 * in the other openers.
2325 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002326 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002327 max_count = device->block->bdev ? 0 : -1;
2328 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002329 if (open_count > max_count) {
2330 if (open_count > 0)
2331 printk(KERN_WARNING "Can't offline dasd "
2332 "device with open count = %i.\n",
2333 open_count);
2334 else
2335 printk(KERN_WARNING "%s",
2336 "Can't offline dasd device due "
2337 "to internal use\n");
2338 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2339 dasd_put_device(device);
2340 return -EBUSY;
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 dasd_set_target_state(device, DASD_STATE_NEW);
2344 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002345 block = device->block;
2346 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002348 /*
2349 * life cycle of block is bound to device, so delete it after
2350 * device was safely removed
2351 */
2352 if (block)
2353 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return 0;
2355}
2356
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002357int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 struct dasd_device *device;
2360 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 int ret;
2362
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002363 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (IS_ERR(device))
2365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 ret = 0;
2367 switch (event) {
2368 case CIO_GONE:
2369 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002370 /* First of all call extended error reporting. */
2371 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (device->state < DASD_STATE_BASIC)
2374 break;
2375 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002376 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2377 if (cqr->status == DASD_CQR_IN_IO) {
2378 cqr->status = DASD_CQR_QUEUED;
2379 cqr->retries++;
2380 }
2381 device->stopped |= DASD_STOPPED_DC_WAIT;
2382 dasd_device_clear_timer(device);
2383 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ret = 1;
2385 break;
2386 case CIO_OPER:
2387 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002388 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2389 dasd_schedule_device_bh(device);
2390 if (device->block)
2391 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 ret = 1;
2393 break;
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 dasd_put_device(device);
2396 return ret;
2397}
2398
Heiko Carstens763968e2007-05-10 15:45:46 +02002399static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2400 void *rdc_buffer,
2401 int rdc_buffer_size,
2402 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002403{
2404 struct dasd_ccw_req *cqr;
2405 struct ccw1 *ccw;
2406
2407 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2408
2409 if (IS_ERR(cqr)) {
2410 DEV_MESSAGE(KERN_WARNING, device, "%s",
2411 "Could not allocate RDC request");
2412 return cqr;
2413 }
2414
2415 ccw = cqr->cpaddr;
2416 ccw->cmd_code = CCW_CMD_RDC;
2417 ccw->cda = (__u32)(addr_t)rdc_buffer;
2418 ccw->count = rdc_buffer_size;
2419
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002420 cqr->startdev = device;
2421 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002422 cqr->expires = 10*HZ;
2423 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2424 cqr->retries = 2;
2425 cqr->buildclk = get_clock();
2426 cqr->status = DASD_CQR_FILLED;
2427 return cqr;
2428}
2429
2430
2431int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2432 void **rdc_buffer, int rdc_buffer_size)
2433{
2434 int ret;
2435 struct dasd_ccw_req *cqr;
2436
2437 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2438 magic);
2439 if (IS_ERR(cqr))
2440 return PTR_ERR(cqr);
2441
2442 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002443 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002444 return ret;
2445}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002446EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002447
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002448static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 int rc;
2451
2452 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002453 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002454 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002457 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (dasd_debug_area == NULL) {
2459 rc = -ENOMEM;
2460 goto failed;
2461 }
2462 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002463 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2466
2467 dasd_diag_discipline_pointer = NULL;
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 rc = dasd_devmap_init();
2470 if (rc)
2471 goto failed;
2472 rc = dasd_gendisk_init();
2473 if (rc)
2474 goto failed;
2475 rc = dasd_parse();
2476 if (rc)
2477 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002478 rc = dasd_eer_init();
2479 if (rc)
2480 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481#ifdef CONFIG_PROC_FS
2482 rc = dasd_proc_init();
2483 if (rc)
2484 goto failed;
2485#endif
2486
2487 return 0;
2488failed:
2489 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2490 dasd_exit();
2491 return rc;
2492}
2493
2494module_init(dasd_init);
2495module_exit(dasd_exit);
2496
2497EXPORT_SYMBOL(dasd_debug_area);
2498EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2499
2500EXPORT_SYMBOL(dasd_add_request_head);
2501EXPORT_SYMBOL(dasd_add_request_tail);
2502EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002503EXPORT_SYMBOL(dasd_device_clear_timer);
2504EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505EXPORT_SYMBOL(dasd_enable_device);
2506EXPORT_SYMBOL(dasd_int_handler);
2507EXPORT_SYMBOL(dasd_kfree_request);
2508EXPORT_SYMBOL(dasd_kick_device);
2509EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002510EXPORT_SYMBOL(dasd_schedule_device_bh);
2511EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002513EXPORT_SYMBOL(dasd_device_set_timer);
2514EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515EXPORT_SYMBOL(dasd_sfree_request);
2516EXPORT_SYMBOL(dasd_sleep_on);
2517EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2518EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2519EXPORT_SYMBOL(dasd_smalloc_request);
2520EXPORT_SYMBOL(dasd_start_IO);
2521EXPORT_SYMBOL(dasd_term_IO);
2522
2523EXPORT_SYMBOL_GPL(dasd_generic_probe);
2524EXPORT_SYMBOL_GPL(dasd_generic_remove);
2525EXPORT_SYMBOL_GPL(dasd_generic_notify);
2526EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2527EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002528EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2529EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2530EXPORT_SYMBOL_GPL(dasd_alloc_block);
2531EXPORT_SYMBOL_GPL(dasd_free_block);