blob: fdb2e7c14506297cab6e39f09aa52aceb5c8274b [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>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010027#include <asm/itcw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/* This is ugly... */
30#define PRINTK_HEADER "dasd:"
31
32#include "dasd_int.h"
33/*
34 * SECTION: Constant definitions to be used within this file
35 */
36#define DASD_CHANQ_MAX_SIZE 4
37
38/*
39 * SECTION: exported variables of dasd.c
40 */
41debug_info_t *dasd_debug_area;
42struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010043void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
46MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
47 " Copyright 2000 IBM Corporation");
48MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_LICENSE("GPL");
50
51/*
52 * SECTION: prototypes for static functions of dasd.c
53 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010054static int dasd_alloc_queue(struct dasd_block *);
55static void dasd_setup_queue(struct dasd_block *);
56static void dasd_free_queue(struct dasd_block *);
57static void dasd_flush_request_queue(struct dasd_block *);
58static int dasd_flush_block_queue(struct dasd_block *);
59static void dasd_device_tasklet(struct dasd_device *);
60static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000061static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020062static void do_restore_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010064static void dasd_device_timeout(unsigned long);
65static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010066static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
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{
Sebastian Ott181d9522009-06-22 12:08:21 +0200473 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
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);
Sebastian Ott181d9522009-06-22 12:08:21 +0200482 if (rc == -EAGAIN)
483 return;
484 if (rc)
485 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Cornelia Huckf3445a12009-04-14 15:36:23 +0200487 if (device->state == device->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 wake_up(&dasd_init_waitq);
Cornelia Huckf3445a12009-04-14 15:36:23 +0200489 dasd_put_device(device);
490 }
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200491
492 /* let user-space know that the device status changed */
493 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/*
497 * Kick starter for devices that did not complete the startup/shutdown
498 * procedure or were sleeping because of a pending state.
499 * dasd_kick_device will schedule a call do do_kick_device to the kernel
500 * event daemon.
501 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100502static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Al Viro4927b3f2006-12-06 19:18:20 +0000504 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100506 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dasd_put_device(device);
508}
509
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100510void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 dasd_get_device(device);
513 /* queue call to dasd_kick_device to the kernel event daemon. */
514 schedule_work(&device->kick_work);
515}
516
517/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200518 * dasd_restore_device will schedule a call do do_restore_device to the kernel
519 * event daemon.
520 */
521static void do_restore_device(struct work_struct *work)
522{
523 struct dasd_device *device = container_of(work, struct dasd_device,
524 restore_device);
525 device->cdev->drv->restore(device->cdev);
526 dasd_put_device(device);
527}
528
529void dasd_restore_device(struct dasd_device *device)
530{
531 dasd_get_device(device);
532 /* queue call to dasd_restore_device to the kernel event daemon. */
533 schedule_work(&device->restore_device);
534}
535
536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Set the target state for a device and starts the state change.
538 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100539void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200541 dasd_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* If we are in probeonly mode stop at DASD_STATE_READY. */
543 if (dasd_probeonly && target > DASD_STATE_READY)
544 target = DASD_STATE_READY;
545 if (device->target != target) {
Cornelia Huckf3445a12009-04-14 15:36:23 +0200546 if (device->state == target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 wake_up(&dasd_init_waitq);
Cornelia Huckf3445a12009-04-14 15:36:23 +0200548 dasd_put_device(device);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 device->target = target;
551 }
552 if (device->state != device->target)
553 dasd_change_state(device);
554}
555
556/*
557 * Enable devices with device numbers in [from..to].
558 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100559static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 return (device->state == device->target);
562}
563
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100564void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 dasd_set_target_state(device, DASD_STATE_ONLINE);
567 if (device->state <= DASD_STATE_KNOWN)
568 /* No discipline for device found. */
569 dasd_set_target_state(device, DASD_STATE_NEW);
570 /* Now wait for the devices to come up. */
571 wait_event(dasd_init_waitq, _wait_for_device(device));
572}
573
574/*
575 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
576 */
577#ifdef CONFIG_DASD_PROFILE
578
579struct dasd_profile_info_t dasd_global_profile;
580unsigned int dasd_profile_level = DASD_PROFILE_OFF;
581
582/*
583 * Increments counter in global and local profiling structures.
584 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100585#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{ \
587 int index; \
588 for (index = 0; index < 31 && value >> (2+index); index++); \
589 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100590 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*
594 * Add profiling information for cqr before execution.
595 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100596static void dasd_profile_start(struct dasd_block *block,
597 struct dasd_ccw_req *cqr,
598 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct list_head *l;
601 unsigned int counter;
602
603 if (dasd_profile_level != DASD_PROFILE_ON)
604 return;
605
606 /* count the length of the chanq for statistics */
607 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100608 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (++counter >= 31)
610 break;
611 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100612 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/*
616 * Add profiling information for cqr after execution.
617 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100618static void dasd_profile_end(struct dasd_block *block,
619 struct dasd_ccw_req *cqr,
620 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 long strtime, irqtime, endtime, tottime; /* in microseconds */
623 long tottimeps, sectors;
624
625 if (dasd_profile_level != DASD_PROFILE_ON)
626 return;
627
Tejun Heo83096eb2009-05-07 22:24:39 +0900628 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!cqr->buildclk || !cqr->startclk ||
630 !cqr->stopclk || !cqr->endclk ||
631 !sectors)
632 return;
633
634 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
635 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
636 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
637 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
638 tottimeps = tottime / sectors;
639
640 if (!dasd_global_profile.dasd_io_reqs)
641 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100642 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 dasd_global_profile.dasd_io_reqs++;
644 dasd_global_profile.dasd_io_sects += sectors;
645
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100646 if (!block->profile.dasd_io_reqs)
647 memset(&block->profile, 0,
648 sizeof(struct dasd_profile_info_t));
649 block->profile.dasd_io_reqs++;
650 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100652 dasd_profile_counter(sectors, dasd_io_secs, block);
653 dasd_profile_counter(tottime, dasd_io_times, block);
654 dasd_profile_counter(tottimeps, dasd_io_timps, block);
655 dasd_profile_counter(strtime, dasd_io_time1, block);
656 dasd_profile_counter(irqtime, dasd_io_time2, block);
657 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
658 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100661#define dasd_profile_start(block, cqr, req) do {} while (0)
662#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#endif /* CONFIG_DASD_PROFILE */
664
665/*
666 * Allocate memory for a channel program with 'cplength' channel
667 * command words and 'datasize' additional space. There are two
668 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
669 * memory and 2) dasd_smalloc_request uses the static ccw memory
670 * that gets allocated for each device.
671 */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200672struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100673 int datasize,
674 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct dasd_ccw_req *cqr;
677
678 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200679 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100680 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800682 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (cqr == NULL)
684 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 cqr->cpaddr = NULL;
686 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800687 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 GFP_ATOMIC | GFP_DMA);
689 if (cqr->cpaddr == NULL) {
690 kfree(cqr);
691 return ERR_PTR(-ENOMEM);
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 cqr->data = NULL;
695 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800696 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800698 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(cqr);
700 return ERR_PTR(-ENOMEM);
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200703 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
705 dasd_get_device(device);
706 return cqr;
707}
708
Stefan Haberland68b781f2009-09-11 10:28:29 +0200709struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100710 int datasize,
711 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 unsigned long flags;
714 struct dasd_ccw_req *cqr;
715 char *data;
716 int size;
717
718 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200719 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100720 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
723 if (cplength > 0)
724 size += cplength * sizeof(struct ccw1);
725 if (datasize > 0)
726 size += datasize;
727 spin_lock_irqsave(&device->mem_lock, flags);
728 cqr = (struct dasd_ccw_req *)
729 dasd_alloc_chunk(&device->ccw_chunks, size);
730 spin_unlock_irqrestore(&device->mem_lock, flags);
731 if (cqr == NULL)
732 return ERR_PTR(-ENOMEM);
733 memset(cqr, 0, sizeof(struct dasd_ccw_req));
734 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
735 cqr->cpaddr = NULL;
736 if (cplength > 0) {
737 cqr->cpaddr = (struct ccw1 *) data;
738 data += cplength*sizeof(struct ccw1);
739 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
740 }
741 cqr->data = NULL;
742 if (datasize > 0) {
743 cqr->data = data;
744 memset(cqr->data, 0, datasize);
745 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200746 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100902 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 "start_IO: device busy, retry later");
904 break;
905 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100906 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 "start_IO: request timeout, retry later");
908 break;
909 case -EACCES:
910 /* -EACCES indicates that the request used only a
911 * subset of the available pathes and all these
912 * pathes are gone.
913 * Do a retry with all available pathes.
914 */
915 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100916 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 "start_IO: selected pathes gone,"
918 " retry on all pathes");
919 break;
920 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100921 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
922 "start_IO: -ENODEV device gone, retry");
923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100925 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100926 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200928 case -EINVAL:
929 /* most likely caused in power management context */
930 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
931 "start_IO: -EINVAL device currently "
932 "not accessible");
933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100935 /* internal error 11 - unknown rc */
936 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
937 dev_err(&device->cdev->dev,
938 "An error occurred in the DASD device driver, "
939 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 BUG();
941 break;
942 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200943 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return rc;
945}
946
947/*
948 * Timeout function for dasd devices. This is used for different purposes
949 * 1) missing interrupt handler for normal operation
950 * 2) delayed start of request where start_IO failed with -EBUSY
951 * 3) timeout for missing state change interrupts
952 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
953 * DASD_CQR_QUEUED for 2) and 3).
954 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100955static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 unsigned long flags;
958 struct dasd_device *device;
959
960 device = (struct dasd_device *) ptr;
961 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
962 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100963 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100965 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * Setup timeout for a device in jiffies.
970 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100971void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100973 if (expires == 0)
974 del_timer(&device->timer);
975 else
976 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
980 * Clear timeout for a device.
981 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100982void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100984 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100987static void dasd_handle_killed_request(struct ccw_device *cdev,
988 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 struct dasd_ccw_req *cqr;
991 struct dasd_device *device;
992
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200993 if (!intparm)
994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 cqr = (struct dasd_ccw_req *) intparm;
996 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +0100997 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
998 "invalid status in handle_killed_request: "
999 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return;
1001 }
1002
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001003 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001005 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001007 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1008 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
1010 }
1011
1012 /* Schedule request to be retried. */
1013 cqr->status = DASD_CQR_QUEUED;
1014
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001015 dasd_device_clear_timer(device);
1016 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 dasd_put_device(device);
1018}
1019
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001020void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001022 /* First of all start sense subsystem status request. */
1023 dasd_eer_snss(device);
1024
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001025 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001026 dasd_schedule_device_bh(device);
1027 if (device->block)
1028 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
1032 * Interrupt handler for "normal" ssch-io based dasd devices.
1033 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001034void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1035 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct dasd_ccw_req *cqr, *next;
1038 struct dasd_device *device;
1039 unsigned long long now;
1040 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (IS_ERR(irb)) {
1043 switch (PTR_ERR(irb)) {
1044 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 break;
1046 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001047 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1048 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001051 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1052 "unknown error %ld\n", __func__,
1053 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001055 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return;
1057 }
1058
1059 now = get_clock();
1060
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001061 /* check for unsolicited interrupts */
1062 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001063 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1064 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1065 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001066 if (cqr && cqr->status == DASD_CQR_IN_IO)
1067 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001068 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001070 dasd_device_clear_timer(device);
1071 device->discipline->handle_unsolicited_interrupt(device,
1072 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 dasd_put_device(device);
1074 }
1075 return;
1076 }
1077
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001078 device = (struct dasd_device *) cqr->startdev;
1079 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001081 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1082 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return;
1084 }
1085
1086 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001087 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001088 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001089 cqr->status = DASD_CQR_CLEARED;
1090 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001091 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001092 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094 }
1095
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001096 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001098 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1099 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001102
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001103 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001105 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1106 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001107 /* request was completed successfully */
1108 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 cqr->stopclk = now;
1110 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001111 if (cqr->devlist.next != &device->ccw_queue) {
1112 next = list_entry(cqr->devlist.next,
1113 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001115 } else { /* error */
1116 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001117 /* log sense for every failed I/O to s390 debugfeature */
1118 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001119 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001120 dasd_log_sense(cqr, irb);
1121 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001122
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001123 /*
1124 * If we don't want complex ERP for this request, then just
1125 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001126 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001127 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001128 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001129 if (cqr->lpm == LPM_ANYPATH)
1130 DBF_DEV_EVENT(DBF_DEBUG, device,
1131 "default ERP in fastpath "
1132 "(%i retries left)",
1133 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001134 cqr->lpm = LPM_ANYPATH;
1135 cqr->status = DASD_CQR_QUEUED;
1136 next = cqr;
1137 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001139 }
1140 if (next && (next->status == DASD_CQR_QUEUED) &&
1141 (!device->stopped)) {
1142 if (device->discipline->start_IO(next) == 0)
1143 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001146 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001148 dasd_device_clear_timer(device);
1149 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001153 * If we have an error on a dasd_block layer request then we cancel
1154 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001156static void __dasd_device_recovery(struct dasd_device *device,
1157 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct list_head *l, *n;
1160 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001163 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001165 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001167
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001168 list_for_each_safe(l, n, &device->ccw_queue) {
1169 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1170 if (cqr->status == DASD_CQR_QUEUED &&
1171 ref_cqr->block == cqr->block) {
1172 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001174 }
1175};
1176
1177/*
1178 * Remove those ccw requests from the queue that need to be returned
1179 * to the upper layer.
1180 */
1181static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1182 struct list_head *final_queue)
1183{
1184 struct list_head *l, *n;
1185 struct dasd_ccw_req *cqr;
1186
1187 /* Process request with final status. */
1188 list_for_each_safe(l, n, &device->ccw_queue) {
1189 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1190
1191 /* Stop list processing at the first non-final request. */
1192 if (cqr->status == DASD_CQR_QUEUED ||
1193 cqr->status == DASD_CQR_IN_IO ||
1194 cqr->status == DASD_CQR_CLEAR_PENDING)
1195 break;
1196 if (cqr->status == DASD_CQR_ERROR) {
1197 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001199 /* Rechain finished requests to final queue */
1200 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202}
1203
1204/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001205 * the cqrs from the final queue are returned to the upper layer
1206 * by setting a dasd_block state and calling the callback function
1207 */
1208static void __dasd_device_process_final_queue(struct dasd_device *device,
1209 struct list_head *final_queue)
1210{
1211 struct list_head *l, *n;
1212 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001213 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001214 void (*callback)(struct dasd_ccw_req *, void *data);
1215 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001216 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001217
1218 list_for_each_safe(l, n, final_queue) {
1219 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1220 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001221 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001222 callback = cqr->callback;
1223 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001224 if (block)
1225 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001226 switch (cqr->status) {
1227 case DASD_CQR_SUCCESS:
1228 cqr->status = DASD_CQR_DONE;
1229 break;
1230 case DASD_CQR_ERROR:
1231 cqr->status = DASD_CQR_NEED_ERP;
1232 break;
1233 case DASD_CQR_CLEARED:
1234 cqr->status = DASD_CQR_TERMINATED;
1235 break;
1236 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001237 /* internal error 12 - wrong cqr status*/
1238 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1239 dev_err(&device->cdev->dev,
1240 "An error occurred in the DASD device driver, "
1241 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001242 BUG();
1243 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001244 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001245 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001246 if (block)
1247 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001248 }
1249}
1250
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 * Take a look at the first request on the ccw queue and check
1253 * if it reached its expire time. If so, terminate the IO.
1254 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001255static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct dasd_ccw_req *cqr;
1258
1259 if (list_empty(&device->ccw_queue))
1260 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001261 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001262 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1263 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1264 if (device->discipline->term_IO(cqr) != 0) {
1265 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001266 dev_err(&device->cdev->dev,
1267 "cqr %p timed out (%is) but cannot be "
1268 "ended, retrying in 5 s\n",
1269 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001270 cqr->expires += 5*HZ;
1271 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001272 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001273 dev_err(&device->cdev->dev,
1274 "cqr %p timed out (%is), %i retries "
1275 "remaining\n", cqr, (cqr->expires/HZ),
1276 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
1279}
1280
1281/*
1282 * Take a look at the first request on the ccw queue and check
1283 * if it needs to be started.
1284 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001285static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct dasd_ccw_req *cqr;
1288 int rc;
1289
1290 if (list_empty(&device->ccw_queue))
1291 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001292 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001293 if (cqr->status != DASD_CQR_QUEUED)
1294 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001295 /* when device is stopped, return request to previous layer */
1296 if (device->stopped) {
1297 cqr->status = DASD_CQR_CLEARED;
1298 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001299 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001300 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001301
1302 rc = device->discipline->start_IO(cqr);
1303 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001304 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001305 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001306 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001307 } else
1308 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001309 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001313 * Go through all request on the dasd_device request queue,
1314 * terminate them on the cdev if necessary, and return them to the
1315 * submitting layer via callback.
1316 * Note:
1317 * Make sure that all 'submitting layers' still exist when
1318 * this function is called!. In other words, when 'device' is a base
1319 * device then all block layer requests must have been removed before
1320 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001322int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001324 struct dasd_ccw_req *cqr, *n;
1325 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 INIT_LIST_HEAD(&flush_queue);
1329 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001330 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001331 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001332 /* Check status and move request to flush_queue */
1333 switch (cqr->status) {
1334 case DASD_CQR_IN_IO:
1335 rc = device->discipline->term_IO(cqr);
1336 if (rc) {
1337 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001338 dev_err(&device->cdev->dev,
1339 "Flushing the DASD request queue "
1340 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001341 /* stop flush processing */
1342 goto finished;
1343 }
1344 break;
1345 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001347 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001348 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001349 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001350 break;
1351 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001352 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001353 }
Horst Hummel8f617012006-08-30 14:33:33 +02001354finished:
1355 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356 /*
1357 * After this point all requests must be in state CLEAR_PENDING,
1358 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1359 * one of the others.
1360 */
1361 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1362 wait_event(dasd_flush_wq,
1363 (cqr->status != DASD_CQR_CLEAR_PENDING));
1364 /*
1365 * Now set each request back to TERMINATED, DONE or NEED_ERP
1366 * and call the callback function of flushed requests
1367 */
1368 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001369 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372/*
1373 * Acquire the device lock and process queues for the device.
1374 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001375static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 atomic_set (&device->tasklet_scheduled, 0);
1380 INIT_LIST_HEAD(&final_queue);
1381 spin_lock_irq(get_ccwdev_lock(device->cdev));
1382 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001383 __dasd_device_check_expire(device);
1384 /* find final requests on ccw queue */
1385 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1387 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001388 __dasd_device_process_final_queue(device, &final_queue);
1389 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001391 __dasd_device_start_head(device);
1392 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 dasd_put_device(device);
1394}
1395
1396/*
1397 * Schedules a call to dasd_tasklet over the device tasklet.
1398 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001399void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001402 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return;
1404 dasd_get_device(device);
1405 tasklet_hi_schedule(&device->tasklet);
1406}
1407
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001408void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1409{
1410 device->stopped |= bits;
1411}
1412EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1413
1414void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1415{
1416 device->stopped &= ~bits;
1417 if (!device->stopped)
1418 wake_up(&generic_waitq);
1419}
1420EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001423 * Queue a request to the head of the device ccw_queue.
1424 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001426void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct dasd_device *device;
1429 unsigned long flags;
1430
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001431 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001433 cqr->status = DASD_CQR_QUEUED;
1434 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1438}
1439
1440/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001441 * Queue a request to the tail of the device ccw_queue.
1442 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct dasd_device *device;
1447 unsigned long flags;
1448
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001449 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001451 cqr->status = DASD_CQR_QUEUED;
1452 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001454 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1456}
1457
1458/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001459 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001461static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 wake_up((wait_queue_head_t *) data);
1464}
1465
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001466static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct dasd_device *device;
1469 int rc;
1470
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001471 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001473 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001474 cqr->status == DASD_CQR_NEED_ERP ||
1475 cqr->status == DASD_CQR_TERMINATED) &&
1476 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1478 return rc;
1479}
1480
1481/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001482 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1483 */
1484static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1485{
1486 struct dasd_device *device;
1487 dasd_erp_fn_t erp_fn;
1488
1489 if (cqr->status == DASD_CQR_FILLED)
1490 return 0;
1491 device = cqr->startdev;
1492 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1493 if (cqr->status == DASD_CQR_TERMINATED) {
1494 device->discipline->handle_terminated_request(cqr);
1495 return 1;
1496 }
1497 if (cqr->status == DASD_CQR_NEED_ERP) {
1498 erp_fn = device->discipline->erp_action(cqr);
1499 erp_fn(cqr);
1500 return 1;
1501 }
1502 if (cqr->status == DASD_CQR_FAILED)
1503 dasd_log_sense(cqr, &cqr->irb);
1504 if (cqr->refers) {
1505 __dasd_process_erp(device, cqr);
1506 return 1;
1507 }
1508 }
1509 return 0;
1510}
1511
1512static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1513{
1514 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1515 if (cqr->refers) /* erp is not done yet */
1516 return 1;
1517 return ((cqr->status != DASD_CQR_DONE) &&
1518 (cqr->status != DASD_CQR_FAILED));
1519 } else
1520 return (cqr->status == DASD_CQR_FILLED);
1521}
1522
1523static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1524{
1525 struct dasd_device *device;
1526 int rc;
1527 struct list_head ccw_queue;
1528 struct dasd_ccw_req *cqr;
1529
1530 INIT_LIST_HEAD(&ccw_queue);
1531 maincqr->status = DASD_CQR_FILLED;
1532 device = maincqr->startdev;
1533 list_add(&maincqr->blocklist, &ccw_queue);
1534 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1535 cqr = list_first_entry(&ccw_queue,
1536 struct dasd_ccw_req, blocklist)) {
1537
1538 if (__dasd_sleep_on_erp(cqr))
1539 continue;
1540 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1541 continue;
1542
1543 /* Non-temporary stop condition will trigger fail fast */
1544 if (device->stopped & ~DASD_STOPPED_PENDING &&
1545 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1546 (!dasd_eer_enabled(device))) {
1547 cqr->status = DASD_CQR_FAILED;
1548 continue;
1549 }
1550
1551 /* Don't try to start requests if device is stopped */
1552 if (interruptible) {
1553 rc = wait_event_interruptible(
1554 generic_waitq, !(device->stopped));
1555 if (rc == -ERESTARTSYS) {
1556 cqr->status = DASD_CQR_FAILED;
1557 maincqr->intrc = rc;
1558 continue;
1559 }
1560 } else
1561 wait_event(generic_waitq, !(device->stopped));
1562
1563 cqr->callback = dasd_wakeup_cb;
1564 cqr->callback_data = (void *) &generic_waitq;
1565 dasd_add_request_tail(cqr);
1566 if (interruptible) {
1567 rc = wait_event_interruptible(
1568 generic_waitq, _wait_for_wakeup(cqr));
1569 if (rc == -ERESTARTSYS) {
1570 dasd_cancel_req(cqr);
1571 /* wait (non-interruptible) for final status */
1572 wait_event(generic_waitq,
1573 _wait_for_wakeup(cqr));
1574 cqr->status = DASD_CQR_FAILED;
1575 maincqr->intrc = rc;
1576 continue;
1577 }
1578 } else
1579 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1580 }
1581
1582 maincqr->endclk = get_clock();
1583 if ((maincqr->status != DASD_CQR_DONE) &&
1584 (maincqr->intrc != -ERESTARTSYS))
1585 dasd_log_sense(maincqr, &maincqr->irb);
1586 if (maincqr->status == DASD_CQR_DONE)
1587 rc = 0;
1588 else if (maincqr->intrc)
1589 rc = maincqr->intrc;
1590 else
1591 rc = -EIO;
1592 return rc;
1593}
1594
1595/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001596 * Queue a request to the tail of the device ccw_queue and wait for
1597 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001599int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001601 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001605 * Queue a request to the tail of the device ccw_queue and wait
1606 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001608int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001610 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
1614 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1615 * for eckd devices) the currently running request has to be terminated
1616 * and be put back to status queued, before the special request is added
1617 * to the head of the queue. Then the special request is waited on normally.
1618 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001619static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (list_empty(&device->ccw_queue))
1624 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001625 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001626 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001629int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct dasd_device *device;
1632 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001633
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001634 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_lock_irq(get_ccwdev_lock(device->cdev));
1636 rc = _dasd_term_running_cqr(device);
1637 if (rc) {
1638 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1639 return rc;
1640 }
Horst Hummel138c0142006-06-29 14:58:12 +02001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001643 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001645 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001648 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1651
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001652 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001653
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001654 if (cqr->status == DASD_CQR_DONE)
1655 rc = 0;
1656 else if (cqr->intrc)
1657 rc = cqr->intrc;
1658 else
1659 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return rc;
1661}
1662
1663/*
1664 * Cancels a request that was started with dasd_sleep_on_req.
1665 * This is useful to timeout requests. The request will be
1666 * terminated if it is currently in i/o.
1667 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001668 * 0 if there was no need to terminate the request (not started yet)
1669 * negative error code if termination failed
1670 * Cancellation of a request is an asynchronous operation! The calling
1671 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001673int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001675 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 unsigned long flags;
1677 int rc;
1678
1679 rc = 0;
1680 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1681 switch (cqr->status) {
1682 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001683 /* request was not started - just set to cleared */
1684 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686 case DASD_CQR_IN_IO:
1687 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001688 rc = device->discipline->term_IO(cqr);
1689 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001690 dev_err(&device->cdev->dev,
1691 "Cancelling request %p failed with rc=%d\n",
1692 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001693 } else {
1694 cqr->stopclk = get_clock();
1695 rc = 1;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001698 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001702 dasd_schedule_device_bh(device);
1703 return rc;
1704}
1705
1706
1707/*
1708 * SECTION: Operations of the dasd_block layer.
1709 */
1710
1711/*
1712 * Timeout function for dasd_block. This is used when the block layer
1713 * is waiting for something that may not come reliably, (e.g. a state
1714 * change interrupt)
1715 */
1716static void dasd_block_timeout(unsigned long ptr)
1717{
1718 unsigned long flags;
1719 struct dasd_block *block;
1720
1721 block = (struct dasd_block *) ptr;
1722 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1723 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001724 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001725 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1726 dasd_schedule_block_bh(block);
1727}
1728
1729/*
1730 * Setup timeout for a dasd_block in jiffies.
1731 */
1732void dasd_block_set_timer(struct dasd_block *block, int expires)
1733{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001734 if (expires == 0)
1735 del_timer(&block->timer);
1736 else
1737 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001738}
1739
1740/*
1741 * Clear timeout for a dasd_block.
1742 */
1743void dasd_block_clear_timer(struct dasd_block *block)
1744{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001745 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001746}
1747
1748/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749 * Process finished error recovery ccw.
1750 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001751static void __dasd_process_erp(struct dasd_device *device,
1752 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001753{
1754 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001755
1756 if (cqr->status == DASD_CQR_DONE)
1757 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1758 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001759 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 erp_fn = device->discipline->erp_postaction(cqr);
1761 erp_fn(cqr);
1762}
1763
1764/*
1765 * Fetch requests from the block device queue.
1766 */
1767static void __dasd_process_request_queue(struct dasd_block *block)
1768{
1769 struct request_queue *queue;
1770 struct request *req;
1771 struct dasd_ccw_req *cqr;
1772 struct dasd_device *basedev;
1773 unsigned long flags;
1774 queue = block->request_queue;
1775 basedev = block->base;
1776 /* No queue ? Then there is nothing to do. */
1777 if (queue == NULL)
1778 return;
1779
1780 /*
1781 * We requeue request from the block device queue to the ccw
1782 * queue only in two states. In state DASD_STATE_READY the
1783 * partition detection is done and we need to requeue requests
1784 * for that. State DASD_STATE_ONLINE is normal block device
1785 * operation.
1786 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001787 if (basedev->state < DASD_STATE_READY) {
1788 while ((req = blk_fetch_request(block->request_queue)))
1789 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001790 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001791 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001792 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001793 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001794 if (basedev->features & DASD_FEATURE_READONLY &&
1795 rq_data_dir(req) == WRITE) {
1796 DBF_DEV_EVENT(DBF_ERR, basedev,
1797 "Rejecting write request %p",
1798 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001799 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001800 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001801 continue;
1802 }
1803 cqr = basedev->discipline->build_cp(basedev, block, req);
1804 if (IS_ERR(cqr)) {
1805 if (PTR_ERR(cqr) == -EBUSY)
1806 break; /* normal end condition */
1807 if (PTR_ERR(cqr) == -ENOMEM)
1808 break; /* terminate request queue loop */
1809 if (PTR_ERR(cqr) == -EAGAIN) {
1810 /*
1811 * The current request cannot be build right
1812 * now, we have to try later. If this request
1813 * is the head-of-queue we stop the device
1814 * for 1/2 second.
1815 */
1816 if (!list_empty(&block->ccw_queue))
1817 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001818 spin_lock_irqsave(
1819 get_ccwdev_lock(basedev->cdev), flags);
1820 dasd_device_set_stop_bits(basedev,
1821 DASD_STOPPED_PENDING);
1822 spin_unlock_irqrestore(
1823 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001824 dasd_block_set_timer(block, HZ/2);
1825 break;
1826 }
1827 DBF_DEV_EVENT(DBF_ERR, basedev,
1828 "CCW creation failed (rc=%ld) "
1829 "on request %p",
1830 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001831 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001832 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001833 continue;
1834 }
1835 /*
1836 * Note: callback is set to dasd_return_cqr_cb in
1837 * __dasd_block_start_head to cover erp requests as well
1838 */
1839 cqr->callback_data = (void *) req;
1840 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001841 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001842 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1843 dasd_profile_start(block, cqr, req);
1844 }
1845}
1846
1847static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1848{
1849 struct request *req;
1850 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001851 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001852
1853 req = (struct request *) cqr->callback_data;
1854 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001855 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001856 if (status <= 0)
1857 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001858 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001859}
1860
1861/*
1862 * Process ccw request queue.
1863 */
1864static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1865 struct list_head *final_queue)
1866{
1867 struct list_head *l, *n;
1868 struct dasd_ccw_req *cqr;
1869 dasd_erp_fn_t erp_fn;
1870 unsigned long flags;
1871 struct dasd_device *base = block->base;
1872
1873restart:
1874 /* Process request with final status. */
1875 list_for_each_safe(l, n, &block->ccw_queue) {
1876 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1877 if (cqr->status != DASD_CQR_DONE &&
1878 cqr->status != DASD_CQR_FAILED &&
1879 cqr->status != DASD_CQR_NEED_ERP &&
1880 cqr->status != DASD_CQR_TERMINATED)
1881 continue;
1882
1883 if (cqr->status == DASD_CQR_TERMINATED) {
1884 base->discipline->handle_terminated_request(cqr);
1885 goto restart;
1886 }
1887
1888 /* Process requests that may be recovered */
1889 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001890 erp_fn = base->discipline->erp_action(cqr);
1891 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001892 goto restart;
1893 }
1894
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001895 /* log sense for fatal error */
1896 if (cqr->status == DASD_CQR_FAILED) {
1897 dasd_log_sense(cqr, &cqr->irb);
1898 }
1899
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001900 /* First of all call extended error reporting. */
1901 if (dasd_eer_enabled(base) &&
1902 cqr->status == DASD_CQR_FAILED) {
1903 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1904
1905 /* restart request */
1906 cqr->status = DASD_CQR_FILLED;
1907 cqr->retries = 255;
1908 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001909 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001910 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1911 flags);
1912 goto restart;
1913 }
1914
1915 /* Process finished ERP request. */
1916 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001917 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 goto restart;
1919 }
1920
1921 /* Rechain finished requests to final queue */
1922 cqr->endclk = get_clock();
1923 list_move_tail(&cqr->blocklist, final_queue);
1924 }
1925}
1926
1927static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1928{
1929 dasd_schedule_block_bh(cqr->block);
1930}
1931
1932static void __dasd_block_start_head(struct dasd_block *block)
1933{
1934 struct dasd_ccw_req *cqr;
1935
1936 if (list_empty(&block->ccw_queue))
1937 return;
1938 /* We allways begin with the first requests on the queue, as some
1939 * of previously started requests have to be enqueued on a
1940 * dasd_device again for error recovery.
1941 */
1942 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1943 if (cqr->status != DASD_CQR_FILLED)
1944 continue;
1945 /* Non-temporary stop condition will trigger fail fast */
1946 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1947 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1948 (!dasd_eer_enabled(block->base))) {
1949 cqr->status = DASD_CQR_FAILED;
1950 dasd_schedule_block_bh(block);
1951 continue;
1952 }
1953 /* Don't try to start requests if device is stopped */
1954 if (block->base->stopped)
1955 return;
1956
1957 /* just a fail safe check, should not happen */
1958 if (!cqr->startdev)
1959 cqr->startdev = block->base;
1960
1961 /* make sure that the requests we submit find their way back */
1962 cqr->callback = dasd_return_cqr_cb;
1963
1964 dasd_add_request_tail(cqr);
1965 }
1966}
1967
1968/*
1969 * Central dasd_block layer routine. Takes requests from the generic
1970 * block layer request queue, creates ccw requests, enqueues them on
1971 * a dasd_device and processes ccw requests that have been returned.
1972 */
1973static void dasd_block_tasklet(struct dasd_block *block)
1974{
1975 struct list_head final_queue;
1976 struct list_head *l, *n;
1977 struct dasd_ccw_req *cqr;
1978
1979 atomic_set(&block->tasklet_scheduled, 0);
1980 INIT_LIST_HEAD(&final_queue);
1981 spin_lock(&block->queue_lock);
1982 /* Finish off requests on ccw queue */
1983 __dasd_process_block_ccw_queue(block, &final_queue);
1984 spin_unlock(&block->queue_lock);
1985 /* Now call the callback function of requests with final status */
1986 spin_lock_irq(&block->request_queue_lock);
1987 list_for_each_safe(l, n, &final_queue) {
1988 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1989 list_del_init(&cqr->blocklist);
1990 __dasd_cleanup_cqr(cqr);
1991 }
1992 spin_lock(&block->queue_lock);
1993 /* Get new request from the block device request queue */
1994 __dasd_process_request_queue(block);
1995 /* Now check if the head of the ccw queue needs to be started. */
1996 __dasd_block_start_head(block);
1997 spin_unlock(&block->queue_lock);
1998 spin_unlock_irq(&block->request_queue_lock);
1999 dasd_put_device(block->base);
2000}
2001
2002static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2003{
2004 wake_up(&dasd_flush_wq);
2005}
2006
2007/*
2008 * Go through all request on the dasd_block request queue, cancel them
2009 * on the respective dasd_device, and return them to the generic
2010 * block layer.
2011 */
2012static int dasd_flush_block_queue(struct dasd_block *block)
2013{
2014 struct dasd_ccw_req *cqr, *n;
2015 int rc, i;
2016 struct list_head flush_queue;
2017
2018 INIT_LIST_HEAD(&flush_queue);
2019 spin_lock_bh(&block->queue_lock);
2020 rc = 0;
2021restart:
2022 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2023 /* if this request currently owned by a dasd_device cancel it */
2024 if (cqr->status >= DASD_CQR_QUEUED)
2025 rc = dasd_cancel_req(cqr);
2026 if (rc < 0)
2027 break;
2028 /* Rechain request (including erp chain) so it won't be
2029 * touched by the dasd_block_tasklet anymore.
2030 * Replace the callback so we notice when the request
2031 * is returned from the dasd_device layer.
2032 */
2033 cqr->callback = _dasd_wake_block_flush_cb;
2034 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2035 list_move_tail(&cqr->blocklist, &flush_queue);
2036 if (i > 1)
2037 /* moved more than one request - need to restart */
2038 goto restart;
2039 }
2040 spin_unlock_bh(&block->queue_lock);
2041 /* Now call the callback function of flushed requests */
2042restart_cb:
2043 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2044 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2045 /* Process finished ERP request. */
2046 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002047 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002048 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002049 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002050 /* restart list_for_xx loop since dasd_process_erp
2051 * might remove multiple elements */
2052 goto restart_cb;
2053 }
2054 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002055 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002056 cqr->endclk = get_clock();
2057 list_del_init(&cqr->blocklist);
2058 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002059 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return rc;
2062}
2063
2064/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002065 * Schedules a call to dasd_tasklet over the device tasklet.
2066 */
2067void dasd_schedule_block_bh(struct dasd_block *block)
2068{
2069 /* Protect against rescheduling. */
2070 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2071 return;
2072 /* life cycle of block is bound to it's base device */
2073 dasd_get_device(block->base);
2074 tasklet_hi_schedule(&block->tasklet);
2075}
2076
2077
2078/*
2079 * SECTION: external block device operations
2080 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
2082
2083/*
2084 * Dasd request queue function. Called from ll_rw_blk.c
2085 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002086static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002088 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002090 block = queue->queuedata;
2091 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002093 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002095 __dasd_block_start_head(block);
2096 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099/*
2100 * Allocate and initialize request queue and default I/O scheduler.
2101 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int rc;
2105
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002106 block->request_queue = blk_init_queue(do_dasd_request,
2107 &block->request_queue_lock);
2108 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return -ENOMEM;
2110
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002111 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002113 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002114 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002115 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002117 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return rc;
2119 }
2120 return 0;
2121}
2122
2123/*
2124 * Allocate and initialize request queue.
2125 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002126static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int max;
2129
Martin K. Petersene1defc42009-05-22 17:17:49 -04002130 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002131 max = block->base->discipline->max_blocks << block->s2b_shift;
2132 blk_queue_max_sectors(block->request_queue, max);
2133 blk_queue_max_phys_segments(block->request_queue, -1L);
2134 blk_queue_max_hw_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002135 /* with page sized segments we can translate each segement into
2136 * one idaw/tidaw
2137 */
2138 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2139 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002140 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143/*
2144 * Deactivate and free request queue.
2145 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002146static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002148 if (block->request_queue) {
2149 blk_cleanup_queue(block->request_queue);
2150 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152}
2153
2154/*
2155 * Flush request on the request queue.
2156 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002157static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct request *req;
2160
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002161 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002163
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002164 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002165 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002166 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002167 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Al Viro57a7c0b2008-03-02 10:36:08 -05002170static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Al Viro57a7c0b2008-03-02 10:36:08 -05002172 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002173 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int rc;
2175
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002176 atomic_inc(&block->open_count);
2177 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 rc = -ENODEV;
2179 goto unlock;
2180 }
2181
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002182 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rc = -EINVAL;
2184 goto unlock;
2185 }
2186
2187 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002188 dev_info(&base->cdev->dev,
2189 "Accessing the DASD failed because it is in "
2190 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 rc = -EPERM;
2192 goto out;
2193 }
2194
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002195 if (base->state <= DASD_STATE_BASIC) {
2196 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 " Cannot open unrecognized device");
2198 rc = -ENODEV;
2199 goto out;
2200 }
2201
2202 return 0;
2203
2204out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002205 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002207 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return rc;
2209}
2210
Al Viro57a7c0b2008-03-02 10:36:08 -05002211static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002213 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002215 atomic_dec(&block->open_count);
2216 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218}
2219
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002220/*
2221 * Return disk geometry.
2222 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002223static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002224{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002225 struct dasd_block *block;
2226 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002227
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002228 block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002229 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002230 return -ENODEV;
Julia Lawallcf05b822009-08-23 18:09:05 +02002231 base = block->base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002232
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002233 if (!base->discipline ||
2234 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002235 return -EINVAL;
2236
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002237 base->discipline->fill_geometry(block, geo);
2238 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002239 return 0;
2240}
2241
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002242const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243dasd_device_operations = {
2244 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002245 .open = dasd_open,
2246 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002247 .ioctl = dasd_ioctl,
2248 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002249 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250};
2251
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002252/*******************************************************************************
2253 * end of block device operations
2254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256static void
2257dasd_exit(void)
2258{
2259#ifdef CONFIG_PROC_FS
2260 dasd_proc_exit();
2261#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002262 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002263 if (dasd_page_cache != NULL) {
2264 kmem_cache_destroy(dasd_page_cache);
2265 dasd_page_cache = NULL;
2266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 dasd_gendisk_exit();
2268 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (dasd_debug_area != NULL) {
2270 debug_unregister(dasd_debug_area);
2271 dasd_debug_area = NULL;
2272 }
2273}
2274
2275/*
2276 * SECTION: common functions for ccw_driver use
2277 */
2278
Cornelia Huckf3445a12009-04-14 15:36:23 +02002279static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2280{
2281 struct ccw_device *cdev = data;
2282 int ret;
2283
2284 ret = ccw_device_set_online(cdev);
2285 if (ret)
2286 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2287 dev_name(&cdev->dev), ret);
2288 else {
2289 struct dasd_device *device = dasd_device_from_cdev(cdev);
2290 wait_event(dasd_init_waitq, _wait_for_device(device));
2291 dasd_put_device(device);
2292 }
2293}
2294
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002295/*
2296 * Initial attempt at a probe function. this can be simplified once
2297 * the other detection code is gone.
2298 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002299int dasd_generic_probe(struct ccw_device *cdev,
2300 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 int ret;
2303
2304 ret = dasd_add_sysfs_files(cdev);
2305 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002306 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2307 "dasd_generic_probe: could not add "
2308 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Horst Hummel40545572006-06-29 15:08:18 +02002311 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Horst Hummel40545572006-06-29 15:08:18 +02002313 /*
2314 * Automatically online either all dasd devices (dasd_autodetect)
2315 * or all devices specified with dasd= parameters during
2316 * initial probe.
2317 */
2318 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002319 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002320 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002324/*
2325 * This will one day be called from a global not_oper handler.
2326 * It is also used by driver_unregister during module unload.
2327 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002328void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002331 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Horst Hummel59afda72005-05-16 21:53:39 -07002333 cdev->handler = NULL;
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 dasd_remove_sysfs_files(cdev);
2336 device = dasd_device_from_cdev(cdev);
2337 if (IS_ERR(device))
2338 return;
2339 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2340 /* Already doing offline processing */
2341 dasd_put_device(device);
2342 return;
2343 }
2344 /*
2345 * This device is removed unconditionally. Set offline
2346 * flag to prevent dasd_open from opening it while it is
2347 * no quite down yet.
2348 */
2349 dasd_set_target_state(device, DASD_STATE_NEW);
2350 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002351 block = device->block;
2352 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002354 /*
2355 * life cycle of block is bound to device, so delete it after
2356 * device was safely removed
2357 */
2358 if (block)
2359 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002362/*
2363 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002365 * or the user has started activation through sysfs.
2366 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002367int dasd_generic_set_online(struct ccw_device *cdev,
2368 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002370 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002372 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002373
Horst Hummel40545572006-06-29 15:08:18 +02002374 /* first online clears initial online feature flag */
2375 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 device = dasd_create_device(cdev);
2377 if (IS_ERR(device))
2378 return PTR_ERR(device);
2379
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002380 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002381 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002383 pr_warning("%s Setting the DASD online failed because "
2384 "of missing DIAG discipline\n",
2385 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 dasd_delete_device(device);
2387 return -ENODEV;
2388 }
2389 discipline = dasd_diag_discipline_pointer;
2390 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002391 if (!try_module_get(base_discipline->owner)) {
2392 dasd_delete_device(device);
2393 return -EINVAL;
2394 }
2395 if (!try_module_get(discipline->owner)) {
2396 module_put(base_discipline->owner);
2397 dasd_delete_device(device);
2398 return -EINVAL;
2399 }
2400 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 device->discipline = discipline;
2402
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002403 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 rc = discipline->check_device(device);
2405 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002406 pr_warning("%s Setting the DASD online with discipline %s "
2407 "failed with rc=%i\n",
2408 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002409 module_put(discipline->owner);
2410 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 dasd_delete_device(device);
2412 return rc;
2413 }
2414
2415 dasd_set_target_state(device, DASD_STATE_ONLINE);
2416 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002417 pr_warning("%s Setting the DASD online failed because of a "
2418 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 rc = -ENODEV;
2420 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002421 if (device->block)
2422 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 dasd_delete_device(device);
2424 } else
2425 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002426 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return rc;
2429}
2430
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002431int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002434 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002435 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 device = dasd_device_from_cdev(cdev);
2438 if (IS_ERR(device))
2439 return PTR_ERR(device);
2440 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2441 /* Already doing offline processing */
2442 dasd_put_device(device);
2443 return 0;
2444 }
2445 /*
2446 * We must make sure that this device is currently not in use.
2447 * The open_count is increased for every opener, that includes
2448 * the blkdev_get in dasd_scan_partitions. We are only interested
2449 * in the other openers.
2450 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002451 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002452 max_count = device->block->bdev ? 0 : -1;
2453 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002454 if (open_count > max_count) {
2455 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002456 pr_warning("%s: The DASD cannot be set offline "
2457 "with open count %i\n",
2458 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002459 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002460 pr_warning("%s: The DASD cannot be set offline "
2461 "while it is in use\n",
2462 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002463 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2464 dasd_put_device(device);
2465 return -EBUSY;
2466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468 dasd_set_target_state(device, DASD_STATE_NEW);
2469 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002470 block = device->block;
2471 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002473 /*
2474 * life cycle of block is bound to device, so delete it after
2475 * device was safely removed
2476 */
2477 if (block)
2478 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return 0;
2480}
2481
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 struct dasd_device *device;
2485 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 int ret;
2487
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002488 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (IS_ERR(device))
2490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 ret = 0;
2492 switch (event) {
2493 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002494 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002496 /* First of all call extended error reporting. */
2497 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (device->state < DASD_STATE_BASIC)
2500 break;
2501 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002502 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2503 if (cqr->status == DASD_CQR_IN_IO) {
2504 cqr->status = DASD_CQR_QUEUED;
2505 cqr->retries++;
2506 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002507 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002508 dasd_device_clear_timer(device);
2509 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 ret = 1;
2511 break;
2512 case CIO_OPER:
2513 /* FIXME: add a sanity check. */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002514 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002515 if (device->stopped & DASD_UNRESUMED_PM) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002516 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002517 dasd_restore_device(device);
2518 ret = 1;
2519 break;
2520 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002521 dasd_schedule_device_bh(device);
2522 if (device->block)
2523 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 ret = 1;
2525 break;
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dasd_put_device(device);
2528 return ret;
2529}
2530
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002531int dasd_generic_pm_freeze(struct ccw_device *cdev)
2532{
2533 struct dasd_ccw_req *cqr, *n;
2534 int rc;
2535 struct list_head freeze_queue;
2536 struct dasd_device *device = dasd_device_from_cdev(cdev);
2537
2538 if (IS_ERR(device))
2539 return PTR_ERR(device);
2540 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002541 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002542 /* clear active requests */
2543 INIT_LIST_HEAD(&freeze_queue);
2544 spin_lock_irq(get_ccwdev_lock(cdev));
2545 rc = 0;
2546 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2547 /* Check status and move request to flush_queue */
2548 if (cqr->status == DASD_CQR_IN_IO) {
2549 rc = device->discipline->term_IO(cqr);
2550 if (rc) {
2551 /* unable to terminate requeust */
2552 dev_err(&device->cdev->dev,
2553 "Unable to terminate request %p "
2554 "on suspend\n", cqr);
2555 spin_unlock_irq(get_ccwdev_lock(cdev));
2556 dasd_put_device(device);
2557 return rc;
2558 }
2559 }
2560 list_move_tail(&cqr->devlist, &freeze_queue);
2561 }
2562
2563 spin_unlock_irq(get_ccwdev_lock(cdev));
2564
2565 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2566 wait_event(dasd_flush_wq,
2567 (cqr->status != DASD_CQR_CLEAR_PENDING));
2568 if (cqr->status == DASD_CQR_CLEARED)
2569 cqr->status = DASD_CQR_QUEUED;
2570 }
2571 /* move freeze_queue to start of the ccw_queue */
2572 spin_lock_irq(get_ccwdev_lock(cdev));
2573 list_splice_tail(&freeze_queue, &device->ccw_queue);
2574 spin_unlock_irq(get_ccwdev_lock(cdev));
2575
2576 if (device->discipline->freeze)
2577 rc = device->discipline->freeze(device);
2578
2579 dasd_put_device(device);
2580 return rc;
2581}
2582EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2583
2584int dasd_generic_restore_device(struct ccw_device *cdev)
2585{
2586 struct dasd_device *device = dasd_device_from_cdev(cdev);
2587 int rc = 0;
2588
2589 if (IS_ERR(device))
2590 return PTR_ERR(device);
2591
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002592 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002593 dasd_device_remove_stop_bits(device,
2594 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002595
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002596 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002597
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002598 /*
2599 * call discipline restore function
2600 * if device is stopped do nothing e.g. for disconnected devices
2601 */
2602 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002603 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002604 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002605 /*
2606 * if the resume failed for the DASD we put it in
2607 * an UNRESUMED stop state
2608 */
2609 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002610
Stefan Haberland6fca97a2009-10-06 10:34:15 +02002611 if (device->block)
2612 dasd_schedule_block_bh(device->block);
2613
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002614 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002615 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002616}
2617EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2618
Heiko Carstens763968e2007-05-10 15:45:46 +02002619static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2620 void *rdc_buffer,
2621 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02002622 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002623{
2624 struct dasd_ccw_req *cqr;
2625 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002626 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02002627
2628 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2629
2630 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002631 /* internal error 13 - Allocating the RDC request failed*/
2632 dev_err(&device->cdev->dev,
2633 "An error occurred in the DASD device driver, "
2634 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002635 return cqr;
2636 }
2637
2638 ccw = cqr->cpaddr;
2639 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002640 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2641 idaw = (unsigned long *) (cqr->data);
2642 ccw->cda = (__u32)(addr_t) idaw;
2643 ccw->flags = CCW_FLAG_IDA;
2644 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2645 } else {
2646 ccw->cda = (__u32)(addr_t) rdc_buffer;
2647 ccw->flags = 0;
2648 }
Cornelia Huck17283b52007-05-04 18:47:51 +02002649
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002650 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002651 cqr->startdev = device;
2652 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002653 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002654 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02002655 cqr->buildclk = get_clock();
2656 cqr->status = DASD_CQR_FILLED;
2657 return cqr;
2658}
2659
2660
Stefan Haberland68b781f2009-09-11 10:28:29 +02002661int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002662 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002663{
2664 int ret;
2665 struct dasd_ccw_req *cqr;
2666
Sebastian Ott92636b12009-06-12 10:26:37 +02002667 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002668 magic);
2669 if (IS_ERR(cqr))
2670 return PTR_ERR(cqr);
2671
2672 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002673 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002674 return ret;
2675}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002676EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002677
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002678/*
2679 * In command mode and transport mode we need to look for sense
2680 * data in different places. The sense data itself is allways
2681 * an array of 32 bytes, so we can unify the sense data access
2682 * for both modes.
2683 */
2684char *dasd_get_sense(struct irb *irb)
2685{
2686 struct tsb *tsb = NULL;
2687 char *sense = NULL;
2688
2689 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2690 if (irb->scsw.tm.tcw)
2691 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2692 irb->scsw.tm.tcw);
2693 if (tsb && tsb->length == 64 && tsb->flags)
2694 switch (tsb->flags & 0x07) {
2695 case 1: /* tsa_iostat */
2696 sense = tsb->tsa.iostat.sense;
2697 break;
2698 case 2: /* tsa_ddpc */
2699 sense = tsb->tsa.ddpc.sense;
2700 break;
2701 default:
2702 /* currently we don't use interrogate data */
2703 break;
2704 }
2705 } else if (irb->esw.esw0.erw.cons) {
2706 sense = irb->ecw;
2707 }
2708 return sense;
2709}
2710EXPORT_SYMBOL_GPL(dasd_get_sense);
2711
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002712static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 int rc;
2715
2716 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002717 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002718 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002721 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (dasd_debug_area == NULL) {
2723 rc = -ENOMEM;
2724 goto failed;
2725 }
2726 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002727 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2730
2731 dasd_diag_discipline_pointer = NULL;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 rc = dasd_devmap_init();
2734 if (rc)
2735 goto failed;
2736 rc = dasd_gendisk_init();
2737 if (rc)
2738 goto failed;
2739 rc = dasd_parse();
2740 if (rc)
2741 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002742 rc = dasd_eer_init();
2743 if (rc)
2744 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745#ifdef CONFIG_PROC_FS
2746 rc = dasd_proc_init();
2747 if (rc)
2748 goto failed;
2749#endif
2750
2751 return 0;
2752failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002753 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 dasd_exit();
2755 return rc;
2756}
2757
2758module_init(dasd_init);
2759module_exit(dasd_exit);
2760
2761EXPORT_SYMBOL(dasd_debug_area);
2762EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2763
2764EXPORT_SYMBOL(dasd_add_request_head);
2765EXPORT_SYMBOL(dasd_add_request_tail);
2766EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002767EXPORT_SYMBOL(dasd_device_clear_timer);
2768EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769EXPORT_SYMBOL(dasd_enable_device);
2770EXPORT_SYMBOL(dasd_int_handler);
2771EXPORT_SYMBOL(dasd_kfree_request);
2772EXPORT_SYMBOL(dasd_kick_device);
2773EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002774EXPORT_SYMBOL(dasd_schedule_device_bh);
2775EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002777EXPORT_SYMBOL(dasd_device_set_timer);
2778EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779EXPORT_SYMBOL(dasd_sfree_request);
2780EXPORT_SYMBOL(dasd_sleep_on);
2781EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2782EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2783EXPORT_SYMBOL(dasd_smalloc_request);
2784EXPORT_SYMBOL(dasd_start_IO);
2785EXPORT_SYMBOL(dasd_term_IO);
2786
2787EXPORT_SYMBOL_GPL(dasd_generic_probe);
2788EXPORT_SYMBOL_GPL(dasd_generic_remove);
2789EXPORT_SYMBOL_GPL(dasd_generic_notify);
2790EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2791EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002792EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2793EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2794EXPORT_SYMBOL_GPL(dasd_alloc_block);
2795EXPORT_SYMBOL_GPL(dasd_free_block);