blob: e5b84db0aa037d97bd6df4fb0ee234b3a49ce5ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02008 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011#define KMSG_COMPONENT "dasd"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kmod.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/ctype.h>
18#include <linux/major.h>
19#include <linux/slab.h>
20#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080021#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020022#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/ccwdev.h>
25#include <asm/ebcdic.h>
26#include <asm/idals.h>
27#include <asm/todclk.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010028#include <asm/itcw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* This is ugly... */
31#define PRINTK_HEADER "dasd:"
32
33#include "dasd_int.h"
34/*
35 * SECTION: Constant definitions to be used within this file
36 */
37#define DASD_CHANQ_MAX_SIZE 4
38
39/*
40 * SECTION: exported variables of dasd.c
41 */
42debug_info_t *dasd_debug_area;
43struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010044void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
47MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
48 " Copyright 2000 IBM Corporation");
49MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_LICENSE("GPL");
51
52/*
53 * SECTION: prototypes for static functions of dasd.c
54 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010055static int dasd_alloc_queue(struct dasd_block *);
56static void dasd_setup_queue(struct dasd_block *);
57static void dasd_free_queue(struct dasd_block *);
58static void dasd_flush_request_queue(struct dasd_block *);
59static int dasd_flush_block_queue(struct dasd_block *);
60static void dasd_device_tasklet(struct dasd_device *);
61static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000062static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020063static void do_restore_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010064static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010065static void dasd_device_timeout(unsigned long);
66static void dasd_block_timeout(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * SECTION: Operations on the device structure.
70 */
71static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020072static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020073static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Allocate memory for a new device structure.
77 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010078struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct dasd_device *device;
81
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010082 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
83 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* Get two pages for normal block device operations. */
87 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010088 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 kfree(device);
90 return ERR_PTR(-ENOMEM);
91 }
92 /* Get one page for error recovery. */
93 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010094 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 free_pages((unsigned long) device->ccw_mem, 1);
96 kfree(device);
97 return ERR_PTR(-ENOMEM);
98 }
99
100 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
101 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
102 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200104 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 (unsigned long) device);
107 INIT_LIST_HEAD(&device->ccw_queue);
108 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100109 device->timer.function = dasd_device_timeout;
110 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000111 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200112 INIT_WORK(&device->restore_device, do_restore_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 device->state = DASD_STATE_NEW;
114 device->target = DASD_STATE_NEW;
115
116 return device;
117}
118
119/*
120 * Free memory of a device structure.
121 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100122void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800124 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 free_page((unsigned long) device->erp_mem);
126 free_pages((unsigned long) device->ccw_mem, 1);
127 kfree(device);
128}
129
130/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100131 * Allocate memory for a new device structure.
132 */
133struct dasd_block *dasd_alloc_block(void)
134{
135 struct dasd_block *block;
136
137 block = kzalloc(sizeof(*block), GFP_ATOMIC);
138 if (!block)
139 return ERR_PTR(-ENOMEM);
140 /* open_count = 0 means device online but not in use */
141 atomic_set(&block->open_count, -1);
142
143 spin_lock_init(&block->request_queue_lock);
144 atomic_set(&block->tasklet_scheduled, 0);
145 tasklet_init(&block->tasklet,
146 (void (*)(unsigned long)) dasd_block_tasklet,
147 (unsigned long) block);
148 INIT_LIST_HEAD(&block->ccw_queue);
149 spin_lock_init(&block->queue_lock);
150 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100151 block->timer.function = dasd_block_timeout;
152 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100153
154 return block;
155}
156
157/*
158 * Free memory of a device structure.
159 */
160void dasd_free_block(struct dasd_block *block)
161{
162 kfree(block);
163}
164
165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * Make a new device known to the system.
167 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100168static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 int rc;
171
172 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200173 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * keep the reference count > 0.
175 */
176 dasd_get_device(device);
177
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100178 if (device->block) {
179 rc = dasd_alloc_queue(device->block);
180 if (rc) {
181 dasd_put_device(device);
182 return rc;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 device->state = DASD_STATE_KNOWN;
186 return 0;
187}
188
189/*
190 * Let the system forget about a device.
191 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100192static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800194 /* Disable extended error reporting for this device. */
195 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100197 if (device->discipline) {
198 if (device->discipline->uncheck_device)
199 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800200 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800203 if (device->base_discipline)
204 module_put(device->base_discipline->owner);
205 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 device->state = DASD_STATE_NEW;
207
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100208 if (device->block)
209 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Give up reference we took in dasd_state_new_to_known. */
212 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * Request the irq line for the device.
218 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int rc;
222
223 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100224 if (device->block) {
225 rc = dasd_gendisk_alloc(device->block);
226 if (rc)
227 return rc;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100230 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100231 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200233 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
235
236 device->state = DASD_STATE_BASIC;
237 return 0;
238}
239
240/*
241 * Release the irq line for the device. Terminate any running i/o.
242 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100243static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Horst Hummel8f617012006-08-30 14:33:33 +0200245 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100246 if (device->block) {
247 dasd_gendisk_free(device->block);
248 dasd_block_clear_timer(device->block);
249 }
250 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200251 if (rc)
252 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100253 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
256 if (device->debug_area != NULL) {
257 debug_unregister(device->debug_area);
258 device->debug_area = NULL;
259 }
260 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * Do the initial analysis. The do_analysis function may return
266 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
267 * until the discipline decides to continue the startup sequence
268 * by calling the function dasd_change_state. The eckd disciplines
269 * uses this to start a ccw that detects the format. The completion
270 * interrupt for this detection ccw uses the kernel event daemon to
271 * trigger the call to dasd_change_state. All this is done in the
272 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800273 * After the analysis ccw is done (do_analysis returned 0) the block
274 * device is setup.
275 * In case the analysis returns an error, the device setup is stopped
276 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100278static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100281 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800285 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100286 if (block) {
287 if (block->base->discipline->do_analysis != NULL)
288 rc = block->base->discipline->do_analysis(block);
289 if (rc) {
290 if (rc != -EAGAIN)
291 device->state = DASD_STATE_UNFMT;
292 return rc;
293 }
294 dasd_setup_queue(block);
295 set_capacity(block->gdp,
296 block->blocks << block->s2b_shift);
297 device->state = DASD_STATE_READY;
298 rc = dasd_scan_partitions(block);
299 if (rc)
300 device->state = DASD_STATE_BASIC;
301 } else {
302 device->state = DASD_STATE_READY;
303 }
Horst Hummel90f00942006-03-07 21:55:39 -0800304 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
308 * Remove device from block device layer. Destroy dirty buffers.
309 * Forget format information. Check if the target level is basic
310 * and if it is create fake disk for formatting.
311 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100312static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Horst Hummel8f617012006-08-30 14:33:33 +0200314 int rc;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100317 if (device->block) {
318 struct dasd_block *block = device->block;
319 rc = dasd_flush_block_queue(block);
320 if (rc) {
321 device->state = DASD_STATE_READY;
322 return rc;
323 }
324 dasd_destroy_partitions(block);
325 dasd_flush_request_queue(block);
326 block->blocks = 0;
327 block->bp_block = 0;
328 block->s2b_shift = 0;
329 }
Horst Hummel8f617012006-08-30 14:33:33 +0200330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333/*
Horst Hummel90f00942006-03-07 21:55:39 -0800334 * Back to basic.
335 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800337{
338 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200339 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800340}
341
342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * Make the device online and schedule the bottom half to start
344 * the requeueing of requests from the linux request queue to the
345 * ccw queue.
346 */
Horst Hummel8f617012006-08-30 14:33:33 +0200347static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348dasd_state_ready_to_online(struct dasd_device * device)
349{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100351 struct gendisk *disk;
352 struct disk_part_iter piter;
353 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100354
355 if (device->discipline->ready_to_online) {
356 rc = device->discipline->ready_to_online(device);
357 if (rc)
358 return rc;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100361 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100362 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100363 disk = device->block->bdev->bd_disk;
364 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
365 while ((part = disk_part_iter_next(&piter)))
366 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
367 disk_part_iter_exit(&piter);
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372/*
373 * Stop the requeueing of requests again.
374 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100377 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100378 struct gendisk *disk;
379 struct disk_part_iter piter;
380 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100381
382 if (device->discipline->online_to_ready) {
383 rc = device->discipline->online_to_ready(device);
384 if (rc)
385 return rc;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100388 if (device->block) {
389 disk = device->block->bdev->bd_disk;
390 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
391 while ((part = disk_part_iter_next(&piter)))
392 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
393 disk_part_iter_exit(&piter);
394 }
Horst Hummel8f617012006-08-30 14:33:33 +0200395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
399 * Device startup state changes.
400 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100401static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 int rc;
404
405 rc = 0;
406 if (device->state == DASD_STATE_NEW &&
407 device->target >= DASD_STATE_KNOWN)
408 rc = dasd_state_new_to_known(device);
409
410 if (!rc &&
411 device->state == DASD_STATE_KNOWN &&
412 device->target >= DASD_STATE_BASIC)
413 rc = dasd_state_known_to_basic(device);
414
415 if (!rc &&
416 device->state == DASD_STATE_BASIC &&
417 device->target >= DASD_STATE_READY)
418 rc = dasd_state_basic_to_ready(device);
419
420 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700421 device->state == DASD_STATE_UNFMT &&
422 device->target > DASD_STATE_UNFMT)
423 rc = -EPERM;
424
425 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 device->state == DASD_STATE_READY &&
427 device->target >= DASD_STATE_ONLINE)
428 rc = dasd_state_ready_to_online(device);
429
430 return rc;
431}
432
433/*
434 * Device shutdown state changes.
435 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100436static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Horst Hummel8f617012006-08-30 14:33:33 +0200438 int rc;
439
440 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (device->state == DASD_STATE_ONLINE &&
442 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200443 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200444
Horst Hummel8f617012006-08-30 14:33:33 +0200445 if (!rc &&
446 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200448 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800449
Horst Hummel8f617012006-08-30 14:33:33 +0200450 if (!rc &&
451 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800452 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200453 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800454
Horst Hummel8f617012006-08-30 14:33:33 +0200455 if (!rc &&
456 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200458 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200459
Horst Hummel8f617012006-08-30 14:33:33 +0200460 if (!rc &&
461 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200463 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Horst Hummel8f617012006-08-30 14:33:33 +0200465 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/*
469 * This is the main startup/shutdown routine.
470 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100471static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int rc;
474
475 if (device->state == device->target)
476 /* Already where we want to go today... */
477 return;
478 if (device->state < device->target)
479 rc = dasd_increase_state(device);
480 else
481 rc = dasd_decrease_state(device);
482 if (rc && rc != -EAGAIN)
483 device->target = device->state;
484
Cornelia Huckf3445a12009-04-14 15:36:23 +0200485 if (device->state == device->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 wake_up(&dasd_init_waitq);
Cornelia Huckf3445a12009-04-14 15:36:23 +0200487 dasd_put_device(device);
488 }
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200489
490 /* let user-space know that the device status changed */
491 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * Kick starter for devices that did not complete the startup/shutdown
496 * procedure or were sleeping because of a pending state.
497 * dasd_kick_device will schedule a call do do_kick_device to the kernel
498 * event daemon.
499 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100500static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Al Viro4927b3f2006-12-06 19:18:20 +0000502 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dasd_put_device(device);
506}
507
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100508void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 dasd_get_device(device);
511 /* queue call to dasd_kick_device to the kernel event daemon. */
512 schedule_work(&device->kick_work);
513}
514
515/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200516 * dasd_restore_device will schedule a call do do_restore_device to the kernel
517 * event daemon.
518 */
519static void do_restore_device(struct work_struct *work)
520{
521 struct dasd_device *device = container_of(work, struct dasd_device,
522 restore_device);
523 device->cdev->drv->restore(device->cdev);
524 dasd_put_device(device);
525}
526
527void dasd_restore_device(struct dasd_device *device)
528{
529 dasd_get_device(device);
530 /* queue call to dasd_restore_device to the kernel event daemon. */
531 schedule_work(&device->restore_device);
532}
533
534/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Set the target state for a device and starts the state change.
536 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100537void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200539 dasd_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* If we are in probeonly mode stop at DASD_STATE_READY. */
541 if (dasd_probeonly && target > DASD_STATE_READY)
542 target = DASD_STATE_READY;
543 if (device->target != target) {
Cornelia Huckf3445a12009-04-14 15:36:23 +0200544 if (device->state == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 wake_up(&dasd_init_waitq);
Cornelia Huckf3445a12009-04-14 15:36:23 +0200546 dasd_put_device(device);
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 device->target = target;
549 }
550 if (device->state != device->target)
551 dasd_change_state(device);
552}
553
554/*
555 * Enable devices with device numbers in [from..to].
556 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100557static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 return (device->state == device->target);
560}
561
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100562void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 dasd_set_target_state(device, DASD_STATE_ONLINE);
565 if (device->state <= DASD_STATE_KNOWN)
566 /* No discipline for device found. */
567 dasd_set_target_state(device, DASD_STATE_NEW);
568 /* Now wait for the devices to come up. */
569 wait_event(dasd_init_waitq, _wait_for_device(device));
570}
571
572/*
573 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
574 */
575#ifdef CONFIG_DASD_PROFILE
576
577struct dasd_profile_info_t dasd_global_profile;
578unsigned int dasd_profile_level = DASD_PROFILE_OFF;
579
580/*
581 * Increments counter in global and local profiling structures.
582 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100583#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{ \
585 int index; \
586 for (index = 0; index < 31 && value >> (2+index); index++); \
587 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100588 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
592 * Add profiling information for cqr before execution.
593 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100594static void dasd_profile_start(struct dasd_block *block,
595 struct dasd_ccw_req *cqr,
596 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct list_head *l;
599 unsigned int counter;
600
601 if (dasd_profile_level != DASD_PROFILE_ON)
602 return;
603
604 /* count the length of the chanq for statistics */
605 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100606 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (++counter >= 31)
608 break;
609 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100610 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*
614 * Add profiling information for cqr after execution.
615 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100616static void dasd_profile_end(struct dasd_block *block,
617 struct dasd_ccw_req *cqr,
618 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 long strtime, irqtime, endtime, tottime; /* in microseconds */
621 long tottimeps, sectors;
622
623 if (dasd_profile_level != DASD_PROFILE_ON)
624 return;
625
Tejun Heo83096eb2009-05-07 22:24:39 +0900626 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!cqr->buildclk || !cqr->startclk ||
628 !cqr->stopclk || !cqr->endclk ||
629 !sectors)
630 return;
631
632 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
633 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
634 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
635 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
636 tottimeps = tottime / sectors;
637
638 if (!dasd_global_profile.dasd_io_reqs)
639 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100640 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 dasd_global_profile.dasd_io_reqs++;
642 dasd_global_profile.dasd_io_sects += sectors;
643
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100644 if (!block->profile.dasd_io_reqs)
645 memset(&block->profile, 0,
646 sizeof(struct dasd_profile_info_t));
647 block->profile.dasd_io_reqs++;
648 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100650 dasd_profile_counter(sectors, dasd_io_secs, block);
651 dasd_profile_counter(tottime, dasd_io_times, block);
652 dasd_profile_counter(tottimeps, dasd_io_timps, block);
653 dasd_profile_counter(strtime, dasd_io_time1, block);
654 dasd_profile_counter(irqtime, dasd_io_time2, block);
655 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
656 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100659#define dasd_profile_start(block, cqr, req) do {} while (0)
660#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif /* CONFIG_DASD_PROFILE */
662
663/*
664 * Allocate memory for a channel program with 'cplength' channel
665 * command words and 'datasize' additional space. There are two
666 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
667 * memory and 2) dasd_smalloc_request uses the static ccw memory
668 * that gets allocated for each device.
669 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100670struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
671 int datasize,
672 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct dasd_ccw_req *cqr;
675
676 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100677 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
678 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800680 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (cqr == NULL)
682 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 cqr->cpaddr = NULL;
684 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800685 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 GFP_ATOMIC | GFP_DMA);
687 if (cqr->cpaddr == NULL) {
688 kfree(cqr);
689 return ERR_PTR(-ENOMEM);
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 cqr->data = NULL;
693 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800694 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800696 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(cqr);
698 return ERR_PTR(-ENOMEM);
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 strncpy((char *) &cqr->magic, magic, 4);
702 ASCEBC((char *) &cqr->magic, 4);
703 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
704 dasd_get_device(device);
705 return cqr;
706}
707
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100708struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
709 int datasize,
710 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 unsigned long flags;
713 struct dasd_ccw_req *cqr;
714 char *data;
715 int size;
716
717 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100718 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
719 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
722 if (cplength > 0)
723 size += cplength * sizeof(struct ccw1);
724 if (datasize > 0)
725 size += datasize;
726 spin_lock_irqsave(&device->mem_lock, flags);
727 cqr = (struct dasd_ccw_req *)
728 dasd_alloc_chunk(&device->ccw_chunks, size);
729 spin_unlock_irqrestore(&device->mem_lock, flags);
730 if (cqr == NULL)
731 return ERR_PTR(-ENOMEM);
732 memset(cqr, 0, sizeof(struct dasd_ccw_req));
733 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
734 cqr->cpaddr = NULL;
735 if (cplength > 0) {
736 cqr->cpaddr = (struct ccw1 *) data;
737 data += cplength*sizeof(struct ccw1);
738 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
739 }
740 cqr->data = NULL;
741 if (datasize > 0) {
742 cqr->data = data;
743 memset(cqr->data, 0, datasize);
744 }
745 strncpy((char *) &cqr->magic, magic, 4);
746 ASCEBC((char *) &cqr->magic, 4);
747 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
748 dasd_get_device(device);
749 return cqr;
750}
751
752/*
753 * Free memory of a channel program. This function needs to free all the
754 * idal lists that might have been created by dasd_set_cda and the
755 * struct dasd_ccw_req itself.
756 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100757void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800759#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct ccw1 *ccw;
761
762 /* Clear any idals used for the request. */
763 ccw = cqr->cpaddr;
764 do {
765 clear_normalized_cda(ccw);
766 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
767#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800768 kfree(cqr->cpaddr);
769 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 kfree(cqr);
771 dasd_put_device(device);
772}
773
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100774void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 unsigned long flags;
777
778 spin_lock_irqsave(&device->mem_lock, flags);
779 dasd_free_chunk(&device->ccw_chunks, cqr);
780 spin_unlock_irqrestore(&device->mem_lock, flags);
781 dasd_put_device(device);
782}
783
784/*
785 * Check discipline magic in cqr.
786 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100787static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct dasd_device *device;
790
791 if (cqr == NULL)
792 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100793 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100795 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 " dasd_ccw_req 0x%08x magic doesn't match"
797 " discipline 0x%08x",
798 cqr->magic,
799 *(unsigned int *) device->discipline->name);
800 return -EINVAL;
801 }
802 return 0;
803}
804
805/*
806 * Terminate the current i/o and set the request to clear_pending.
807 * Timer keeps device runnig.
808 * ccw_device_clear can fail if the i/o subsystem
809 * is in a bad mood.
810 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100811int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct dasd_device *device;
814 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100815 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Check the cqr */
818 rc = dasd_check_cqr(cqr);
819 if (rc)
820 return rc;
821 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100822 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
824 rc = ccw_device_clear(device->cdev, (long) cqr);
825 switch (rc) {
826 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800827 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100828 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200830 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 DBF_DEV_EVENT(DBF_DEBUG, device,
832 "terminate cqr %p successful",
833 cqr);
834 break;
835 case -ENODEV:
836 DBF_DEV_EVENT(DBF_ERR, device, "%s",
837 "device gone, retry");
838 break;
839 case -EIO:
840 DBF_DEV_EVENT(DBF_ERR, device, "%s",
841 "I/O error, retry");
842 break;
843 case -EINVAL:
844 case -EBUSY:
845 DBF_DEV_EVENT(DBF_ERR, device, "%s",
846 "device busy, retry later");
847 break;
848 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100849 /* internal error 10 - unknown rc*/
850 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
851 dev_err(&device->cdev->dev, "An error occurred in the "
852 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 BUG();
854 break;
855 }
856 retries++;
857 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100858 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return rc;
860}
861
862/*
863 * Start the i/o. This start_IO can fail if the channel is really busy.
864 * In that case set up a timer to start the request later.
865 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100866int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct dasd_device *device;
869 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100870 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* Check the cqr */
873 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200874 if (rc) {
875 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200877 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100878 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100880 /* internal error 14 - start_IO run out of retries */
881 sprintf(errorstring, "14 %p", cqr);
882 dev_err(&device->cdev->dev, "An error occurred in the DASD "
883 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100884 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EIO;
886 }
887 cqr->startclk = get_clock();
888 cqr->starttime = jiffies;
889 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100890 if (cqr->cpmode == 1) {
891 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
892 (long) cqr, cqr->lpm);
893 } else {
894 rc = ccw_device_start(device->cdev, cqr->cpaddr,
895 (long) cqr, cqr->lpm, 0);
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 switch (rc) {
898 case 0:
899 cqr->status = DASD_CQR_IN_IO;
900 DBF_DEV_EVENT(DBF_DEBUG, device,
901 "start_IO: request %p started successful",
902 cqr);
903 break;
904 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100905 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 "start_IO: device busy, retry later");
907 break;
908 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100909 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 "start_IO: request timeout, retry later");
911 break;
912 case -EACCES:
913 /* -EACCES indicates that the request used only a
914 * subset of the available pathes and all these
915 * pathes are gone.
916 * Do a retry with all available pathes.
917 */
918 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100919 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 "start_IO: selected pathes gone,"
921 " retry on all pathes");
922 break;
923 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100924 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
925 "start_IO: -ENODEV device gone, retry");
926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100928 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100929 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200931 case -EINVAL:
932 /* most likely caused in power management context */
933 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
934 "start_IO: -EINVAL device currently "
935 "not accessible");
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100938 /* internal error 11 - unknown rc */
939 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
940 dev_err(&device->cdev->dev,
941 "An error occurred in the DASD device driver, "
942 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 BUG();
944 break;
945 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200946 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return rc;
948}
949
950/*
951 * Timeout function for dasd devices. This is used for different purposes
952 * 1) missing interrupt handler for normal operation
953 * 2) delayed start of request where start_IO failed with -EBUSY
954 * 3) timeout for missing state change interrupts
955 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
956 * DASD_CQR_QUEUED for 2) and 3).
957 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100958static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 unsigned long flags;
961 struct dasd_device *device;
962
963 device = (struct dasd_device *) ptr;
964 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
965 /* re-activate request queue */
966 device->stopped &= ~DASD_STOPPED_PENDING;
967 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100968 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
972 * Setup timeout for a device in jiffies.
973 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100974void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100976 if (expires == 0)
977 del_timer(&device->timer);
978 else
979 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * Clear timeout for a device.
984 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100985void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100987 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100990static void dasd_handle_killed_request(struct ccw_device *cdev,
991 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct dasd_ccw_req *cqr;
994 struct dasd_device *device;
995
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200996 if (!intparm)
997 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 cqr = (struct dasd_ccw_req *) intparm;
999 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001000 DBF_EVENT(DBF_DEBUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 "invalid status in handle_killed_request: "
1002 "bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001003 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001007 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001009 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001011 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1012 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return;
1014 }
1015
1016 /* Schedule request to be retried. */
1017 cqr->status = DASD_CQR_QUEUED;
1018
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001019 dasd_device_clear_timer(device);
1020 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dasd_put_device(device);
1022}
1023
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001024void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001026 /* First of all start sense subsystem status request. */
1027 dasd_eer_snss(device);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001030 dasd_schedule_device_bh(device);
1031 if (device->block)
1032 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
1036 * Interrupt handler for "normal" ssch-io based dasd devices.
1037 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001038void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1039 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct dasd_ccw_req *cqr, *next;
1042 struct dasd_device *device;
1043 unsigned long long now;
1044 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (IS_ERR(irb)) {
1047 switch (PTR_ERR(irb)) {
1048 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001051 DBF_EVENT(DBF_WARNING, "%s(%s): request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001052 __func__, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001055 DBF_EVENT(DBF_WARNING, "%s(%s): unknown error %ld\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001056 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001058 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return;
1060 }
1061
1062 now = get_clock();
1063
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001064 /* check for unsolicited interrupts */
1065 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001066 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1067 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1068 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001069 if (cqr && cqr->status == DASD_CQR_IN_IO)
1070 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001071 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001073 dasd_device_clear_timer(device);
1074 device->discipline->handle_unsolicited_interrupt(device,
1075 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 dasd_put_device(device);
1077 }
1078 return;
1079 }
1080
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001081 device = (struct dasd_device *) cqr->startdev;
1082 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001084 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1085 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return;
1087 }
1088
1089 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001090 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001091 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001092 cqr->status = DASD_CQR_CLEARED;
1093 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001094 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001095 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return;
1097 }
1098
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001099 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001101 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1102 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001105
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001106 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001108 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1109 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001110 /* request was completed successfully */
1111 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 cqr->stopclk = now;
1113 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001114 if (cqr->devlist.next != &device->ccw_queue) {
1115 next = list_entry(cqr->devlist.next,
1116 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001118 } else { /* error */
1119 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001120 /* log sense for every failed I/O to s390 debugfeature */
1121 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001122 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001123 dasd_log_sense(cqr, irb);
1124 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001125
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001126 /*
1127 * If we don't want complex ERP for this request, then just
1128 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001129 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001130 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001131 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001132 if (cqr->lpm == LPM_ANYPATH)
1133 DBF_DEV_EVENT(DBF_DEBUG, device,
1134 "default ERP in fastpath "
1135 "(%i retries left)",
1136 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001137 cqr->lpm = LPM_ANYPATH;
1138 cqr->status = DASD_CQR_QUEUED;
1139 next = cqr;
1140 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001142 }
1143 if (next && (next->status == DASD_CQR_QUEUED) &&
1144 (!device->stopped)) {
1145 if (device->discipline->start_IO(next) == 0)
1146 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001149 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001151 dasd_device_clear_timer(device);
1152 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001156 * If we have an error on a dasd_block layer request then we cancel
1157 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001159static void __dasd_device_recovery(struct dasd_device *device,
1160 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct list_head *l, *n;
1163 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001166 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001168 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001170
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001171 list_for_each_safe(l, n, &device->ccw_queue) {
1172 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1173 if (cqr->status == DASD_CQR_QUEUED &&
1174 ref_cqr->block == cqr->block) {
1175 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001177 }
1178};
1179
1180/*
1181 * Remove those ccw requests from the queue that need to be returned
1182 * to the upper layer.
1183 */
1184static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1185 struct list_head *final_queue)
1186{
1187 struct list_head *l, *n;
1188 struct dasd_ccw_req *cqr;
1189
1190 /* Process request with final status. */
1191 list_for_each_safe(l, n, &device->ccw_queue) {
1192 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1193
1194 /* Stop list processing at the first non-final request. */
1195 if (cqr->status == DASD_CQR_QUEUED ||
1196 cqr->status == DASD_CQR_IN_IO ||
1197 cqr->status == DASD_CQR_CLEAR_PENDING)
1198 break;
1199 if (cqr->status == DASD_CQR_ERROR) {
1200 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001202 /* Rechain finished requests to final queue */
1203 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205}
1206
1207/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001208 * the cqrs from the final queue are returned to the upper layer
1209 * by setting a dasd_block state and calling the callback function
1210 */
1211static void __dasd_device_process_final_queue(struct dasd_device *device,
1212 struct list_head *final_queue)
1213{
1214 struct list_head *l, *n;
1215 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001216 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001217 void (*callback)(struct dasd_ccw_req *, void *data);
1218 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001219 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001220
1221 list_for_each_safe(l, n, final_queue) {
1222 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1223 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001224 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001225 callback = cqr->callback;
1226 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001227 if (block)
1228 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229 switch (cqr->status) {
1230 case DASD_CQR_SUCCESS:
1231 cqr->status = DASD_CQR_DONE;
1232 break;
1233 case DASD_CQR_ERROR:
1234 cqr->status = DASD_CQR_NEED_ERP;
1235 break;
1236 case DASD_CQR_CLEARED:
1237 cqr->status = DASD_CQR_TERMINATED;
1238 break;
1239 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001240 /* internal error 12 - wrong cqr status*/
1241 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1242 dev_err(&device->cdev->dev,
1243 "An error occurred in the DASD device driver, "
1244 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001245 BUG();
1246 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001247 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001248 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001249 if (block)
1250 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001251 }
1252}
1253
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001254/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * Take a look at the first request on the ccw queue and check
1256 * if it reached its expire time. If so, terminate the IO.
1257 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001258static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct dasd_ccw_req *cqr;
1261
1262 if (list_empty(&device->ccw_queue))
1263 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001264 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001265 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1266 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1267 if (device->discipline->term_IO(cqr) != 0) {
1268 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001269 dev_err(&device->cdev->dev,
1270 "cqr %p timed out (%is) but cannot be "
1271 "ended, retrying in 5 s\n",
1272 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001273 cqr->expires += 5*HZ;
1274 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001275 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001276 dev_err(&device->cdev->dev,
1277 "cqr %p timed out (%is), %i retries "
1278 "remaining\n", cqr, (cqr->expires/HZ),
1279 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 }
1282}
1283
1284/*
1285 * Take a look at the first request on the ccw queue and check
1286 * if it needs to be started.
1287 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001288static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct dasd_ccw_req *cqr;
1291 int rc;
1292
1293 if (list_empty(&device->ccw_queue))
1294 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001295 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001296 if (cqr->status != DASD_CQR_QUEUED)
1297 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001298 /* when device is stopped, return request to previous layer */
1299 if (device->stopped) {
1300 cqr->status = DASD_CQR_CLEARED;
1301 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001302 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001303 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001304
1305 rc = device->discipline->start_IO(cqr);
1306 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001307 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001308 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001309 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001310 } else
1311 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 * Go through all request on the dasd_device request queue,
1317 * terminate them on the cdev if necessary, and return them to the
1318 * submitting layer via callback.
1319 * Note:
1320 * Make sure that all 'submitting layers' still exist when
1321 * this function is called!. In other words, when 'device' is a base
1322 * device then all block layer requests must have been removed before
1323 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001325int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001327 struct dasd_ccw_req *cqr, *n;
1328 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 INIT_LIST_HEAD(&flush_queue);
1332 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001333 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001334 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001335 /* Check status and move request to flush_queue */
1336 switch (cqr->status) {
1337 case DASD_CQR_IN_IO:
1338 rc = device->discipline->term_IO(cqr);
1339 if (rc) {
1340 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001341 dev_err(&device->cdev->dev,
1342 "Flushing the DASD request queue "
1343 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001344 /* stop flush processing */
1345 goto finished;
1346 }
1347 break;
1348 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001350 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001351 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001352 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001353 break;
1354 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001355 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001356 }
Horst Hummel8f617012006-08-30 14:33:33 +02001357finished:
1358 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359 /*
1360 * After this point all requests must be in state CLEAR_PENDING,
1361 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1362 * one of the others.
1363 */
1364 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1365 wait_event(dasd_flush_wq,
1366 (cqr->status != DASD_CQR_CLEAR_PENDING));
1367 /*
1368 * Now set each request back to TERMINATED, DONE or NEED_ERP
1369 * and call the callback function of flushed requests
1370 */
1371 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001372 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375/*
1376 * Acquire the device lock and process queues for the device.
1377 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 atomic_set (&device->tasklet_scheduled, 0);
1383 INIT_LIST_HEAD(&final_queue);
1384 spin_lock_irq(get_ccwdev_lock(device->cdev));
1385 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001386 __dasd_device_check_expire(device);
1387 /* find final requests on ccw queue */
1388 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1390 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 __dasd_device_process_final_queue(device, &final_queue);
1392 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001394 __dasd_device_start_head(device);
1395 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dasd_put_device(device);
1397}
1398
1399/*
1400 * Schedules a call to dasd_tasklet over the device tasklet.
1401 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001402void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001405 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return;
1407 dasd_get_device(device);
1408 tasklet_hi_schedule(&device->tasklet);
1409}
1410
1411/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001412 * Queue a request to the head of the device ccw_queue.
1413 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001415void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct dasd_device *device;
1418 unsigned long flags;
1419
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001420 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001422 cqr->status = DASD_CQR_QUEUED;
1423 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001425 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1427}
1428
1429/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001430 * Queue a request to the tail of the device ccw_queue.
1431 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001433void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct dasd_device *device;
1436 unsigned long flags;
1437
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001438 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001440 cqr->status = DASD_CQR_QUEUED;
1441 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001443 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1445}
1446
1447/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001448 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001450static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 wake_up((wait_queue_head_t *) data);
1453}
1454
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001455static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct dasd_device *device;
1458 int rc;
1459
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001462 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001463 cqr->status == DASD_CQR_NEED_ERP ||
1464 cqr->status == DASD_CQR_TERMINATED) &&
1465 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1467 return rc;
1468}
1469
1470/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001471 * Queue a request to the tail of the device ccw_queue and wait for
1472 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001474int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct dasd_device *device;
1477 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001478
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001479 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001482 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001483 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001484 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001485
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001486 if (cqr->status == DASD_CQR_DONE)
1487 rc = 0;
1488 else if (cqr->intrc)
1489 rc = cqr->intrc;
1490 else
1491 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return rc;
1493}
1494
1495/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001496 * Queue a request to the tail of the device ccw_queue and wait
1497 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001499int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001502 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001504 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001506 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001507 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001508 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001509 if (rc == -ERESTARTSYS) {
1510 dasd_cancel_req(cqr);
1511 /* wait (non-interruptible) for final status */
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001512 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001513 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001515
1516 if (cqr->status == DASD_CQR_DONE)
1517 rc = 0;
1518 else if (cqr->intrc)
1519 rc = cqr->intrc;
1520 else
1521 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return rc;
1523}
1524
1525/*
1526 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1527 * for eckd devices) the currently running request has to be terminated
1528 * and be put back to status queued, before the special request is added
1529 * to the head of the queue. Then the special request is waited on normally.
1530 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001531static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (list_empty(&device->ccw_queue))
1536 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001537 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001538 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001541int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct dasd_device *device;
1544 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001545
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001546 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_lock_irq(get_ccwdev_lock(device->cdev));
1548 rc = _dasd_term_running_cqr(device);
1549 if (rc) {
1550 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1551 return rc;
1552 }
Horst Hummel138c0142006-06-29 14:58:12 +02001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001555 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001557 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001560 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1563
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001564 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001565
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001566 if (cqr->status == DASD_CQR_DONE)
1567 rc = 0;
1568 else if (cqr->intrc)
1569 rc = cqr->intrc;
1570 else
1571 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return rc;
1573}
1574
1575/*
1576 * Cancels a request that was started with dasd_sleep_on_req.
1577 * This is useful to timeout requests. The request will be
1578 * terminated if it is currently in i/o.
1579 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001580 * 0 if there was no need to terminate the request (not started yet)
1581 * negative error code if termination failed
1582 * Cancellation of a request is an asynchronous operation! The calling
1583 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001585int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001587 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 unsigned long flags;
1589 int rc;
1590
1591 rc = 0;
1592 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1593 switch (cqr->status) {
1594 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001595 /* request was not started - just set to cleared */
1596 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 break;
1598 case DASD_CQR_IN_IO:
1599 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001600 rc = device->discipline->term_IO(cqr);
1601 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001602 dev_err(&device->cdev->dev,
1603 "Cancelling request %p failed with rc=%d\n",
1604 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001605 } else {
1606 cqr->stopclk = get_clock();
1607 rc = 1;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001610 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001614 dasd_schedule_device_bh(device);
1615 return rc;
1616}
1617
1618
1619/*
1620 * SECTION: Operations of the dasd_block layer.
1621 */
1622
1623/*
1624 * Timeout function for dasd_block. This is used when the block layer
1625 * is waiting for something that may not come reliably, (e.g. a state
1626 * change interrupt)
1627 */
1628static void dasd_block_timeout(unsigned long ptr)
1629{
1630 unsigned long flags;
1631 struct dasd_block *block;
1632
1633 block = (struct dasd_block *) ptr;
1634 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1635 /* re-activate request queue */
1636 block->base->stopped &= ~DASD_STOPPED_PENDING;
1637 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1638 dasd_schedule_block_bh(block);
1639}
1640
1641/*
1642 * Setup timeout for a dasd_block in jiffies.
1643 */
1644void dasd_block_set_timer(struct dasd_block *block, int expires)
1645{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001646 if (expires == 0)
1647 del_timer(&block->timer);
1648 else
1649 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001650}
1651
1652/*
1653 * Clear timeout for a dasd_block.
1654 */
1655void dasd_block_clear_timer(struct dasd_block *block)
1656{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001657 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001658}
1659
1660/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001661 * Process finished error recovery ccw.
1662 */
1663static inline void __dasd_block_process_erp(struct dasd_block *block,
1664 struct dasd_ccw_req *cqr)
1665{
1666 dasd_erp_fn_t erp_fn;
1667 struct dasd_device *device = block->base;
1668
1669 if (cqr->status == DASD_CQR_DONE)
1670 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1671 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001672 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673 erp_fn = device->discipline->erp_postaction(cqr);
1674 erp_fn(cqr);
1675}
1676
1677/*
1678 * Fetch requests from the block device queue.
1679 */
1680static void __dasd_process_request_queue(struct dasd_block *block)
1681{
1682 struct request_queue *queue;
1683 struct request *req;
1684 struct dasd_ccw_req *cqr;
1685 struct dasd_device *basedev;
1686 unsigned long flags;
1687 queue = block->request_queue;
1688 basedev = block->base;
1689 /* No queue ? Then there is nothing to do. */
1690 if (queue == NULL)
1691 return;
1692
1693 /*
1694 * We requeue request from the block device queue to the ccw
1695 * queue only in two states. In state DASD_STATE_READY the
1696 * partition detection is done and we need to requeue requests
1697 * for that. State DASD_STATE_ONLINE is normal block device
1698 * operation.
1699 */
1700 if (basedev->state < DASD_STATE_READY)
1701 return;
1702 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001703 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001704 if (basedev->features & DASD_FEATURE_READONLY &&
1705 rq_data_dir(req) == WRITE) {
1706 DBF_DEV_EVENT(DBF_ERR, basedev,
1707 "Rejecting write request %p",
1708 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001709 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001710 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001711 continue;
1712 }
1713 cqr = basedev->discipline->build_cp(basedev, block, req);
1714 if (IS_ERR(cqr)) {
1715 if (PTR_ERR(cqr) == -EBUSY)
1716 break; /* normal end condition */
1717 if (PTR_ERR(cqr) == -ENOMEM)
1718 break; /* terminate request queue loop */
1719 if (PTR_ERR(cqr) == -EAGAIN) {
1720 /*
1721 * The current request cannot be build right
1722 * now, we have to try later. If this request
1723 * is the head-of-queue we stop the device
1724 * for 1/2 second.
1725 */
1726 if (!list_empty(&block->ccw_queue))
1727 break;
1728 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1729 basedev->stopped |= DASD_STOPPED_PENDING;
1730 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1731 dasd_block_set_timer(block, HZ/2);
1732 break;
1733 }
1734 DBF_DEV_EVENT(DBF_ERR, basedev,
1735 "CCW creation failed (rc=%ld) "
1736 "on request %p",
1737 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001738 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001739 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 continue;
1741 }
1742 /*
1743 * Note: callback is set to dasd_return_cqr_cb in
1744 * __dasd_block_start_head to cover erp requests as well
1745 */
1746 cqr->callback_data = (void *) req;
1747 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001748 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1750 dasd_profile_start(block, cqr, req);
1751 }
1752}
1753
1754static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1755{
1756 struct request *req;
1757 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001758 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001759
1760 req = (struct request *) cqr->callback_data;
1761 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001762 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001763 if (status <= 0)
1764 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001765 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001766}
1767
1768/*
1769 * Process ccw request queue.
1770 */
1771static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1772 struct list_head *final_queue)
1773{
1774 struct list_head *l, *n;
1775 struct dasd_ccw_req *cqr;
1776 dasd_erp_fn_t erp_fn;
1777 unsigned long flags;
1778 struct dasd_device *base = block->base;
1779
1780restart:
1781 /* Process request with final status. */
1782 list_for_each_safe(l, n, &block->ccw_queue) {
1783 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1784 if (cqr->status != DASD_CQR_DONE &&
1785 cqr->status != DASD_CQR_FAILED &&
1786 cqr->status != DASD_CQR_NEED_ERP &&
1787 cqr->status != DASD_CQR_TERMINATED)
1788 continue;
1789
1790 if (cqr->status == DASD_CQR_TERMINATED) {
1791 base->discipline->handle_terminated_request(cqr);
1792 goto restart;
1793 }
1794
1795 /* Process requests that may be recovered */
1796 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001797 erp_fn = base->discipline->erp_action(cqr);
1798 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001799 goto restart;
1800 }
1801
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001802 /* log sense for fatal error */
1803 if (cqr->status == DASD_CQR_FAILED) {
1804 dasd_log_sense(cqr, &cqr->irb);
1805 }
1806
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001807 /* First of all call extended error reporting. */
1808 if (dasd_eer_enabled(base) &&
1809 cqr->status == DASD_CQR_FAILED) {
1810 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1811
1812 /* restart request */
1813 cqr->status = DASD_CQR_FILLED;
1814 cqr->retries = 255;
1815 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1816 base->stopped |= DASD_STOPPED_QUIESCE;
1817 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1818 flags);
1819 goto restart;
1820 }
1821
1822 /* Process finished ERP request. */
1823 if (cqr->refers) {
1824 __dasd_block_process_erp(block, cqr);
1825 goto restart;
1826 }
1827
1828 /* Rechain finished requests to final queue */
1829 cqr->endclk = get_clock();
1830 list_move_tail(&cqr->blocklist, final_queue);
1831 }
1832}
1833
1834static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1835{
1836 dasd_schedule_block_bh(cqr->block);
1837}
1838
1839static void __dasd_block_start_head(struct dasd_block *block)
1840{
1841 struct dasd_ccw_req *cqr;
1842
1843 if (list_empty(&block->ccw_queue))
1844 return;
1845 /* We allways begin with the first requests on the queue, as some
1846 * of previously started requests have to be enqueued on a
1847 * dasd_device again for error recovery.
1848 */
1849 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1850 if (cqr->status != DASD_CQR_FILLED)
1851 continue;
1852 /* Non-temporary stop condition will trigger fail fast */
1853 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1854 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1855 (!dasd_eer_enabled(block->base))) {
1856 cqr->status = DASD_CQR_FAILED;
1857 dasd_schedule_block_bh(block);
1858 continue;
1859 }
1860 /* Don't try to start requests if device is stopped */
1861 if (block->base->stopped)
1862 return;
1863
1864 /* just a fail safe check, should not happen */
1865 if (!cqr->startdev)
1866 cqr->startdev = block->base;
1867
1868 /* make sure that the requests we submit find their way back */
1869 cqr->callback = dasd_return_cqr_cb;
1870
1871 dasd_add_request_tail(cqr);
1872 }
1873}
1874
1875/*
1876 * Central dasd_block layer routine. Takes requests from the generic
1877 * block layer request queue, creates ccw requests, enqueues them on
1878 * a dasd_device and processes ccw requests that have been returned.
1879 */
1880static void dasd_block_tasklet(struct dasd_block *block)
1881{
1882 struct list_head final_queue;
1883 struct list_head *l, *n;
1884 struct dasd_ccw_req *cqr;
1885
1886 atomic_set(&block->tasklet_scheduled, 0);
1887 INIT_LIST_HEAD(&final_queue);
1888 spin_lock(&block->queue_lock);
1889 /* Finish off requests on ccw queue */
1890 __dasd_process_block_ccw_queue(block, &final_queue);
1891 spin_unlock(&block->queue_lock);
1892 /* Now call the callback function of requests with final status */
1893 spin_lock_irq(&block->request_queue_lock);
1894 list_for_each_safe(l, n, &final_queue) {
1895 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1896 list_del_init(&cqr->blocklist);
1897 __dasd_cleanup_cqr(cqr);
1898 }
1899 spin_lock(&block->queue_lock);
1900 /* Get new request from the block device request queue */
1901 __dasd_process_request_queue(block);
1902 /* Now check if the head of the ccw queue needs to be started. */
1903 __dasd_block_start_head(block);
1904 spin_unlock(&block->queue_lock);
1905 spin_unlock_irq(&block->request_queue_lock);
1906 dasd_put_device(block->base);
1907}
1908
1909static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1910{
1911 wake_up(&dasd_flush_wq);
1912}
1913
1914/*
1915 * Go through all request on the dasd_block request queue, cancel them
1916 * on the respective dasd_device, and return them to the generic
1917 * block layer.
1918 */
1919static int dasd_flush_block_queue(struct dasd_block *block)
1920{
1921 struct dasd_ccw_req *cqr, *n;
1922 int rc, i;
1923 struct list_head flush_queue;
1924
1925 INIT_LIST_HEAD(&flush_queue);
1926 spin_lock_bh(&block->queue_lock);
1927 rc = 0;
1928restart:
1929 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1930 /* if this request currently owned by a dasd_device cancel it */
1931 if (cqr->status >= DASD_CQR_QUEUED)
1932 rc = dasd_cancel_req(cqr);
1933 if (rc < 0)
1934 break;
1935 /* Rechain request (including erp chain) so it won't be
1936 * touched by the dasd_block_tasklet anymore.
1937 * Replace the callback so we notice when the request
1938 * is returned from the dasd_device layer.
1939 */
1940 cqr->callback = _dasd_wake_block_flush_cb;
1941 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1942 list_move_tail(&cqr->blocklist, &flush_queue);
1943 if (i > 1)
1944 /* moved more than one request - need to restart */
1945 goto restart;
1946 }
1947 spin_unlock_bh(&block->queue_lock);
1948 /* Now call the callback function of flushed requests */
1949restart_cb:
1950 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1951 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1952 /* Process finished ERP request. */
1953 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001954 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001955 __dasd_block_process_erp(block, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001956 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001957 /* restart list_for_xx loop since dasd_process_erp
1958 * might remove multiple elements */
1959 goto restart_cb;
1960 }
1961 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001962 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001963 cqr->endclk = get_clock();
1964 list_del_init(&cqr->blocklist);
1965 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001966 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return rc;
1969}
1970
1971/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001972 * Schedules a call to dasd_tasklet over the device tasklet.
1973 */
1974void dasd_schedule_block_bh(struct dasd_block *block)
1975{
1976 /* Protect against rescheduling. */
1977 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1978 return;
1979 /* life cycle of block is bound to it's base device */
1980 dasd_get_device(block->base);
1981 tasklet_hi_schedule(&block->tasklet);
1982}
1983
1984
1985/*
1986 * SECTION: external block device operations
1987 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
1989
1990/*
1991 * Dasd request queue function. Called from ll_rw_blk.c
1992 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001993static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001995 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001997 block = queue->queuedata;
1998 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002000 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002002 __dasd_block_start_head(block);
2003 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
2006/*
2007 * Allocate and initialize request queue and default I/O scheduler.
2008 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002009static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 int rc;
2012
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002013 block->request_queue = blk_init_queue(do_dasd_request,
2014 &block->request_queue_lock);
2015 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return -ENOMEM;
2017
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002018 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002020 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002021 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002022 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002024 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return rc;
2026 }
2027 return 0;
2028}
2029
2030/*
2031 * Allocate and initialize request queue.
2032 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002033static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 int max;
2036
Martin K. Petersene1defc42009-05-22 17:17:49 -04002037 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002038 max = block->base->discipline->max_blocks << block->s2b_shift;
2039 blk_queue_max_sectors(block->request_queue, max);
2040 blk_queue_max_phys_segments(block->request_queue, -1L);
2041 blk_queue_max_hw_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002042 /* with page sized segments we can translate each segement into
2043 * one idaw/tidaw
2044 */
2045 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2046 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002047 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/*
2051 * Deactivate and free request queue.
2052 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002053static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002055 if (block->request_queue) {
2056 blk_cleanup_queue(block->request_queue);
2057 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
2061/*
2062 * Flush request on the request queue.
2063 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct request *req;
2067
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002068 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002070
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002071 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002072 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002073 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Al Viro57a7c0b2008-03-02 10:36:08 -05002077static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Al Viro57a7c0b2008-03-02 10:36:08 -05002079 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002080 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int rc;
2082
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002083 atomic_inc(&block->open_count);
2084 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 rc = -ENODEV;
2086 goto unlock;
2087 }
2088
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002089 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rc = -EINVAL;
2091 goto unlock;
2092 }
2093
2094 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002095 dev_info(&base->cdev->dev,
2096 "Accessing the DASD failed because it is in "
2097 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 rc = -EPERM;
2099 goto out;
2100 }
2101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 if (base->state <= DASD_STATE_BASIC) {
2103 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 " Cannot open unrecognized device");
2105 rc = -ENODEV;
2106 goto out;
2107 }
2108
2109 return 0;
2110
2111out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002112 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002114 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return rc;
2116}
2117
Al Viro57a7c0b2008-03-02 10:36:08 -05002118static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002120 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002122 atomic_dec(&block->open_count);
2123 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125}
2126
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002127/*
2128 * Return disk geometry.
2129 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002130static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002131{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002132 struct dasd_block *block;
2133 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002134
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002135 block = bdev->bd_disk->private_data;
2136 base = block->base;
2137 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002138 return -ENODEV;
2139
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002140 if (!base->discipline ||
2141 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002142 return -EINVAL;
2143
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002144 base->discipline->fill_geometry(block, geo);
2145 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002146 return 0;
2147}
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149struct block_device_operations
2150dasd_device_operations = {
2151 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002152 .open = dasd_open,
2153 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002154 .ioctl = dasd_ioctl,
2155 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002156 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157};
2158
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002159/*******************************************************************************
2160 * end of block device operations
2161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163static void
2164dasd_exit(void)
2165{
2166#ifdef CONFIG_PROC_FS
2167 dasd_proc_exit();
2168#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002169 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002170 if (dasd_page_cache != NULL) {
2171 kmem_cache_destroy(dasd_page_cache);
2172 dasd_page_cache = NULL;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 dasd_gendisk_exit();
2175 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (dasd_debug_area != NULL) {
2177 debug_unregister(dasd_debug_area);
2178 dasd_debug_area = NULL;
2179 }
2180}
2181
2182/*
2183 * SECTION: common functions for ccw_driver use
2184 */
2185
Cornelia Huckf3445a12009-04-14 15:36:23 +02002186static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2187{
2188 struct ccw_device *cdev = data;
2189 int ret;
2190
2191 ret = ccw_device_set_online(cdev);
2192 if (ret)
2193 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2194 dev_name(&cdev->dev), ret);
2195 else {
2196 struct dasd_device *device = dasd_device_from_cdev(cdev);
2197 wait_event(dasd_init_waitq, _wait_for_device(device));
2198 dasd_put_device(device);
2199 }
2200}
2201
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002202/*
2203 * Initial attempt at a probe function. this can be simplified once
2204 * the other detection code is gone.
2205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002206int dasd_generic_probe(struct ccw_device *cdev,
2207 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 int ret;
2210
Horst Hummel40545572006-06-29 15:08:18 +02002211 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2212 if (ret) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002213 DBF_EVENT(DBF_WARNING,
Horst Hummel40545572006-06-29 15:08:18 +02002214 "dasd_generic_probe: could not set ccw-device options "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002215 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002216 return ret;
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 ret = dasd_add_sysfs_files(cdev);
2219 if (ret) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002220 DBF_EVENT(DBF_WARNING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 "dasd_generic_probe: could not add sysfs entries "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002222 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Horst Hummel40545572006-06-29 15:08:18 +02002225 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Horst Hummel40545572006-06-29 15:08:18 +02002227 /*
2228 * Automatically online either all dasd devices (dasd_autodetect)
2229 * or all devices specified with dasd= parameters during
2230 * initial probe.
2231 */
2232 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002233 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002234 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002238/*
2239 * This will one day be called from a global not_oper handler.
2240 * It is also used by driver_unregister during module unload.
2241 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002242void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002245 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Horst Hummel59afda72005-05-16 21:53:39 -07002247 cdev->handler = NULL;
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 dasd_remove_sysfs_files(cdev);
2250 device = dasd_device_from_cdev(cdev);
2251 if (IS_ERR(device))
2252 return;
2253 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2254 /* Already doing offline processing */
2255 dasd_put_device(device);
2256 return;
2257 }
2258 /*
2259 * This device is removed unconditionally. Set offline
2260 * flag to prevent dasd_open from opening it while it is
2261 * no quite down yet.
2262 */
2263 dasd_set_target_state(device, DASD_STATE_NEW);
2264 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002265 block = device->block;
2266 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002268 /*
2269 * life cycle of block is bound to device, so delete it after
2270 * device was safely removed
2271 */
2272 if (block)
2273 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002276/*
2277 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002279 * or the user has started activation through sysfs.
2280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002281int dasd_generic_set_online(struct ccw_device *cdev,
2282 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002284 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002286 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002287
Horst Hummel40545572006-06-29 15:08:18 +02002288 /* first online clears initial online feature flag */
2289 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 device = dasd_create_device(cdev);
2291 if (IS_ERR(device))
2292 return PTR_ERR(device);
2293
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002294 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002295 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002297 pr_warning("%s Setting the DASD online failed because "
2298 "of missing DIAG discipline\n",
2299 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 dasd_delete_device(device);
2301 return -ENODEV;
2302 }
2303 discipline = dasd_diag_discipline_pointer;
2304 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002305 if (!try_module_get(base_discipline->owner)) {
2306 dasd_delete_device(device);
2307 return -EINVAL;
2308 }
2309 if (!try_module_get(discipline->owner)) {
2310 module_put(base_discipline->owner);
2311 dasd_delete_device(device);
2312 return -EINVAL;
2313 }
2314 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 device->discipline = discipline;
2316
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002317 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 rc = discipline->check_device(device);
2319 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002320 pr_warning("%s Setting the DASD online with discipline %s "
2321 "failed with rc=%i\n",
2322 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002323 module_put(discipline->owner);
2324 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 dasd_delete_device(device);
2326 return rc;
2327 }
2328
2329 dasd_set_target_state(device, DASD_STATE_ONLINE);
2330 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002331 pr_warning("%s Setting the DASD online failed because of a "
2332 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 rc = -ENODEV;
2334 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335 if (device->block)
2336 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 dasd_delete_device(device);
2338 } else
2339 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002340 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return rc;
2343}
2344
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002345int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002348 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002349 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 device = dasd_device_from_cdev(cdev);
2352 if (IS_ERR(device))
2353 return PTR_ERR(device);
2354 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2355 /* Already doing offline processing */
2356 dasd_put_device(device);
2357 return 0;
2358 }
2359 /*
2360 * We must make sure that this device is currently not in use.
2361 * The open_count is increased for every opener, that includes
2362 * the blkdev_get in dasd_scan_partitions. We are only interested
2363 * in the other openers.
2364 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002365 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002366 max_count = device->block->bdev ? 0 : -1;
2367 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002368 if (open_count > max_count) {
2369 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002370 pr_warning("%s: The DASD cannot be set offline "
2371 "with open count %i\n",
2372 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002373 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002374 pr_warning("%s: The DASD cannot be set offline "
2375 "while it is in use\n",
2376 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002377 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2378 dasd_put_device(device);
2379 return -EBUSY;
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 dasd_set_target_state(device, DASD_STATE_NEW);
2383 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002384 block = device->block;
2385 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002387 /*
2388 * life cycle of block is bound to device, so delete it after
2389 * device was safely removed
2390 */
2391 if (block)
2392 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return 0;
2394}
2395
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002396int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 struct dasd_device *device;
2399 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 int ret;
2401
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002402 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (IS_ERR(device))
2404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 ret = 0;
2406 switch (event) {
2407 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002408 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002410 /* First of all call extended error reporting. */
2411 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (device->state < DASD_STATE_BASIC)
2414 break;
2415 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002416 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2417 if (cqr->status == DASD_CQR_IN_IO) {
2418 cqr->status = DASD_CQR_QUEUED;
2419 cqr->retries++;
2420 }
2421 device->stopped |= DASD_STOPPED_DC_WAIT;
2422 dasd_device_clear_timer(device);
2423 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 ret = 1;
2425 break;
2426 case CIO_OPER:
2427 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002428 device->stopped &= ~DASD_STOPPED_DC_WAIT;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002429 if (device->stopped & DASD_UNRESUMED_PM) {
2430 device->stopped &= ~DASD_UNRESUMED_PM;
2431 dasd_restore_device(device);
2432 ret = 1;
2433 break;
2434 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002435 dasd_schedule_device_bh(device);
2436 if (device->block)
2437 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 ret = 1;
2439 break;
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 dasd_put_device(device);
2442 return ret;
2443}
2444
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002445int dasd_generic_pm_freeze(struct ccw_device *cdev)
2446{
2447 struct dasd_ccw_req *cqr, *n;
2448 int rc;
2449 struct list_head freeze_queue;
2450 struct dasd_device *device = dasd_device_from_cdev(cdev);
2451
2452 if (IS_ERR(device))
2453 return PTR_ERR(device);
2454 /* disallow new I/O */
2455 device->stopped |= DASD_STOPPED_PM;
2456 /* clear active requests */
2457 INIT_LIST_HEAD(&freeze_queue);
2458 spin_lock_irq(get_ccwdev_lock(cdev));
2459 rc = 0;
2460 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2461 /* Check status and move request to flush_queue */
2462 if (cqr->status == DASD_CQR_IN_IO) {
2463 rc = device->discipline->term_IO(cqr);
2464 if (rc) {
2465 /* unable to terminate requeust */
2466 dev_err(&device->cdev->dev,
2467 "Unable to terminate request %p "
2468 "on suspend\n", cqr);
2469 spin_unlock_irq(get_ccwdev_lock(cdev));
2470 dasd_put_device(device);
2471 return rc;
2472 }
2473 }
2474 list_move_tail(&cqr->devlist, &freeze_queue);
2475 }
2476
2477 spin_unlock_irq(get_ccwdev_lock(cdev));
2478
2479 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2480 wait_event(dasd_flush_wq,
2481 (cqr->status != DASD_CQR_CLEAR_PENDING));
2482 if (cqr->status == DASD_CQR_CLEARED)
2483 cqr->status = DASD_CQR_QUEUED;
2484 }
2485 /* move freeze_queue to start of the ccw_queue */
2486 spin_lock_irq(get_ccwdev_lock(cdev));
2487 list_splice_tail(&freeze_queue, &device->ccw_queue);
2488 spin_unlock_irq(get_ccwdev_lock(cdev));
2489
2490 if (device->discipline->freeze)
2491 rc = device->discipline->freeze(device);
2492
2493 dasd_put_device(device);
2494 return rc;
2495}
2496EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2497
2498int dasd_generic_restore_device(struct ccw_device *cdev)
2499{
2500 struct dasd_device *device = dasd_device_from_cdev(cdev);
2501 int rc = 0;
2502
2503 if (IS_ERR(device))
2504 return PTR_ERR(device);
2505
2506 dasd_schedule_device_bh(device);
2507 if (device->block)
2508 dasd_schedule_block_bh(device->block);
2509
2510 if (device->discipline->restore)
2511 rc = device->discipline->restore(device);
2512
2513 dasd_put_device(device);
2514 return rc;
2515}
2516EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2517
Heiko Carstens763968e2007-05-10 15:45:46 +02002518static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2519 void *rdc_buffer,
2520 int rdc_buffer_size,
2521 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002522{
2523 struct dasd_ccw_req *cqr;
2524 struct ccw1 *ccw;
2525
2526 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2527
2528 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002529 /* internal error 13 - Allocating the RDC request failed*/
2530 dev_err(&device->cdev->dev,
2531 "An error occurred in the DASD device driver, "
2532 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002533 return cqr;
2534 }
2535
2536 ccw = cqr->cpaddr;
2537 ccw->cmd_code = CCW_CMD_RDC;
2538 ccw->cda = (__u32)(addr_t)rdc_buffer;
2539 ccw->count = rdc_buffer_size;
2540
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002541 cqr->startdev = device;
2542 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002543 cqr->expires = 10*HZ;
2544 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2545 cqr->retries = 2;
2546 cqr->buildclk = get_clock();
2547 cqr->status = DASD_CQR_FILLED;
2548 return cqr;
2549}
2550
2551
2552int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002553 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002554{
2555 int ret;
2556 struct dasd_ccw_req *cqr;
2557
Sebastian Ott92636b12009-06-12 10:26:37 +02002558 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002559 magic);
2560 if (IS_ERR(cqr))
2561 return PTR_ERR(cqr);
2562
2563 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002564 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002565 return ret;
2566}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002567EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002568
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002569/*
2570 * In command mode and transport mode we need to look for sense
2571 * data in different places. The sense data itself is allways
2572 * an array of 32 bytes, so we can unify the sense data access
2573 * for both modes.
2574 */
2575char *dasd_get_sense(struct irb *irb)
2576{
2577 struct tsb *tsb = NULL;
2578 char *sense = NULL;
2579
2580 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2581 if (irb->scsw.tm.tcw)
2582 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2583 irb->scsw.tm.tcw);
2584 if (tsb && tsb->length == 64 && tsb->flags)
2585 switch (tsb->flags & 0x07) {
2586 case 1: /* tsa_iostat */
2587 sense = tsb->tsa.iostat.sense;
2588 break;
2589 case 2: /* tsa_ddpc */
2590 sense = tsb->tsa.ddpc.sense;
2591 break;
2592 default:
2593 /* currently we don't use interrogate data */
2594 break;
2595 }
2596 } else if (irb->esw.esw0.erw.cons) {
2597 sense = irb->ecw;
2598 }
2599 return sense;
2600}
2601EXPORT_SYMBOL_GPL(dasd_get_sense);
2602
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002603static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 int rc;
2606
2607 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002608 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002609 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002612 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (dasd_debug_area == NULL) {
2614 rc = -ENOMEM;
2615 goto failed;
2616 }
2617 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002618 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2621
2622 dasd_diag_discipline_pointer = NULL;
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 rc = dasd_devmap_init();
2625 if (rc)
2626 goto failed;
2627 rc = dasd_gendisk_init();
2628 if (rc)
2629 goto failed;
2630 rc = dasd_parse();
2631 if (rc)
2632 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002633 rc = dasd_eer_init();
2634 if (rc)
2635 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636#ifdef CONFIG_PROC_FS
2637 rc = dasd_proc_init();
2638 if (rc)
2639 goto failed;
2640#endif
2641
2642 return 0;
2643failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002644 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 dasd_exit();
2646 return rc;
2647}
2648
2649module_init(dasd_init);
2650module_exit(dasd_exit);
2651
2652EXPORT_SYMBOL(dasd_debug_area);
2653EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2654
2655EXPORT_SYMBOL(dasd_add_request_head);
2656EXPORT_SYMBOL(dasd_add_request_tail);
2657EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002658EXPORT_SYMBOL(dasd_device_clear_timer);
2659EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660EXPORT_SYMBOL(dasd_enable_device);
2661EXPORT_SYMBOL(dasd_int_handler);
2662EXPORT_SYMBOL(dasd_kfree_request);
2663EXPORT_SYMBOL(dasd_kick_device);
2664EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002665EXPORT_SYMBOL(dasd_schedule_device_bh);
2666EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002668EXPORT_SYMBOL(dasd_device_set_timer);
2669EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670EXPORT_SYMBOL(dasd_sfree_request);
2671EXPORT_SYMBOL(dasd_sleep_on);
2672EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2673EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2674EXPORT_SYMBOL(dasd_smalloc_request);
2675EXPORT_SYMBOL(dasd_start_IO);
2676EXPORT_SYMBOL(dasd_term_IO);
2677
2678EXPORT_SYMBOL_GPL(dasd_generic_probe);
2679EXPORT_SYMBOL_GPL(dasd_generic_remove);
2680EXPORT_SYMBOL_GPL(dasd_generic_notify);
2681EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2682EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002683EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2684EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2685EXPORT_SYMBOL_GPL(dasd_alloc_block);
2686EXPORT_SYMBOL_GPL(dasd_free_block);