blob: acf222f91f5a97ac0564c25a5bea9386466d045b [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>
Stefan Haberland9eb25122010-02-26 22:37:46 +010023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/ccwdev.h>
26#include <asm/ebcdic.h>
27#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010028#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010029#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* This is ugly... */
32#define PRINTK_HEADER "dasd:"
33
34#include "dasd_int.h"
35/*
36 * SECTION: Constant definitions to be used within this file
37 */
38#define DASD_CHANQ_MAX_SIZE 4
39
40/*
41 * SECTION: exported variables of dasd.c
42 */
43debug_info_t *dasd_debug_area;
44struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010045void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
48MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
49 " Copyright 2000 IBM Corporation");
50MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_LICENSE("GPL");
52
53/*
54 * SECTION: prototypes for static functions of dasd.c
55 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010056static int dasd_alloc_queue(struct dasd_block *);
57static void dasd_setup_queue(struct dasd_block *);
58static void dasd_free_queue(struct dasd_block *);
59static void dasd_flush_request_queue(struct dasd_block *);
60static int dasd_flush_block_queue(struct dasd_block *);
61static void dasd_device_tasklet(struct dasd_device *);
62static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000063static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020064static void do_restore_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010065static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010066static void dasd_device_timeout(unsigned long);
67static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010068static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * SECTION: Operations on the device structure.
72 */
73static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020074static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020075static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Allocate memory for a new device structure.
79 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010080struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct dasd_device *device;
83
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010084 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
85 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* Get two pages for normal block device operations. */
89 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 kfree(device);
92 return ERR_PTR(-ENOMEM);
93 }
94 /* Get one page for error recovery. */
95 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010096 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 free_pages((unsigned long) device->ccw_mem, 1);
98 kfree(device);
99 return ERR_PTR(-ENOMEM);
100 }
101
102 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
103 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
104 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200106 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100107 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 (unsigned long) device);
109 INIT_LIST_HEAD(&device->ccw_queue);
110 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100111 device->timer.function = dasd_device_timeout;
112 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000113 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200114 INIT_WORK(&device->restore_device, do_restore_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 device->state = DASD_STATE_NEW;
116 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100117 mutex_init(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return device;
120}
121
122/*
123 * Free memory of a device structure.
124 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100125void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800127 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 free_page((unsigned long) device->erp_mem);
129 free_pages((unsigned long) device->ccw_mem, 1);
130 kfree(device);
131}
132
133/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100134 * Allocate memory for a new device structure.
135 */
136struct dasd_block *dasd_alloc_block(void)
137{
138 struct dasd_block *block;
139
140 block = kzalloc(sizeof(*block), GFP_ATOMIC);
141 if (!block)
142 return ERR_PTR(-ENOMEM);
143 /* open_count = 0 means device online but not in use */
144 atomic_set(&block->open_count, -1);
145
146 spin_lock_init(&block->request_queue_lock);
147 atomic_set(&block->tasklet_scheduled, 0);
148 tasklet_init(&block->tasklet,
149 (void (*)(unsigned long)) dasd_block_tasklet,
150 (unsigned long) block);
151 INIT_LIST_HEAD(&block->ccw_queue);
152 spin_lock_init(&block->queue_lock);
153 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100154 block->timer.function = dasd_block_timeout;
155 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100156
157 return block;
158}
159
160/*
161 * Free memory of a device structure.
162 */
163void dasd_free_block(struct dasd_block *block)
164{
165 kfree(block);
166}
167
168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Make a new device known to the system.
170 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100171static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int rc;
174
175 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200176 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * keep the reference count > 0.
178 */
179 dasd_get_device(device);
180
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100181 if (device->block) {
182 rc = dasd_alloc_queue(device->block);
183 if (rc) {
184 dasd_put_device(device);
185 return rc;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 device->state = DASD_STATE_KNOWN;
189 return 0;
190}
191
192/*
193 * Let the system forget about a device.
194 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800197 /* Disable extended error reporting for this device. */
198 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100200 if (device->discipline) {
201 if (device->discipline->uncheck_device)
202 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800203 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800206 if (device->base_discipline)
207 module_put(device->base_discipline->owner);
208 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 device->state = DASD_STATE_NEW;
210
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100211 if (device->block)
212 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Give up reference we took in dasd_state_new_to_known. */
215 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/*
220 * Request the irq line for the device.
221 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100222static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 int rc;
225
226 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100227 if (device->block) {
228 rc = dasd_gendisk_alloc(device->block);
229 if (rc)
230 return rc;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100233 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100234 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200236 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
238
239 device->state = DASD_STATE_BASIC;
240 return 0;
241}
242
243/*
244 * Release the irq line for the device. Terminate any running i/o.
245 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100246static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Horst Hummel8f617012006-08-30 14:33:33 +0200248 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100249 if (device->block) {
250 dasd_gendisk_free(device->block);
251 dasd_block_clear_timer(device->block);
252 }
253 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200254 if (rc)
255 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100256 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
259 if (device->debug_area != NULL) {
260 debug_unregister(device->debug_area);
261 device->debug_area = NULL;
262 }
263 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Do the initial analysis. The do_analysis function may return
269 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
270 * until the discipline decides to continue the startup sequence
271 * by calling the function dasd_change_state. The eckd disciplines
272 * uses this to start a ccw that detects the format. The completion
273 * interrupt for this detection ccw uses the kernel event daemon to
274 * trigger the call to dasd_change_state. All this is done in the
275 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800276 * After the analysis ccw is done (do_analysis returned 0) the block
277 * device is setup.
278 * In case the analysis returns an error, the device setup is stopped
279 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100281static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100287 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800288 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289 if (block) {
290 if (block->base->discipline->do_analysis != NULL)
291 rc = block->base->discipline->do_analysis(block);
292 if (rc) {
293 if (rc != -EAGAIN)
294 device->state = DASD_STATE_UNFMT;
295 return rc;
296 }
297 dasd_setup_queue(block);
298 set_capacity(block->gdp,
299 block->blocks << block->s2b_shift);
300 device->state = DASD_STATE_READY;
301 rc = dasd_scan_partitions(block);
302 if (rc)
303 device->state = DASD_STATE_BASIC;
304 } else {
305 device->state = DASD_STATE_READY;
306 }
Horst Hummel90f00942006-03-07 21:55:39 -0800307 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
311 * Remove device from block device layer. Destroy dirty buffers.
312 * Forget format information. Check if the target level is basic
313 * and if it is create fake disk for formatting.
314 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100315static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Horst Hummel8f617012006-08-30 14:33:33 +0200317 int rc;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100320 if (device->block) {
321 struct dasd_block *block = device->block;
322 rc = dasd_flush_block_queue(block);
323 if (rc) {
324 device->state = DASD_STATE_READY;
325 return rc;
326 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100328 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329 block->blocks = 0;
330 block->bp_block = 0;
331 block->s2b_shift = 0;
332 }
Horst Hummel8f617012006-08-30 14:33:33 +0200333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336/*
Horst Hummel90f00942006-03-07 21:55:39 -0800337 * Back to basic.
338 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100339static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800340{
341 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200342 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800343}
344
345/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * Make the device online and schedule the bottom half to start
347 * the requeueing of requests from the linux request queue to the
348 * ccw queue.
349 */
Horst Hummel8f617012006-08-30 14:33:33 +0200350static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351dasd_state_ready_to_online(struct dasd_device * device)
352{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100354 struct gendisk *disk;
355 struct disk_part_iter piter;
356 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100357
358 if (device->discipline->ready_to_online) {
359 rc = device->discipline->ready_to_online(device);
360 if (rc)
361 return rc;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100364 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100365 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100366 disk = device->block->bdev->bd_disk;
367 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
368 while ((part = disk_part_iter_next(&piter)))
369 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
370 disk_part_iter_exit(&piter);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373}
374
375/*
376 * Stop the requeueing of requests again.
377 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100378static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100381 struct gendisk *disk;
382 struct disk_part_iter piter;
383 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100384
385 if (device->discipline->online_to_ready) {
386 rc = device->discipline->online_to_ready(device);
387 if (rc)
388 return rc;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100391 if (device->block) {
392 disk = device->block->bdev->bd_disk;
393 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
394 while ((part = disk_part_iter_next(&piter)))
395 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
396 disk_part_iter_exit(&piter);
397 }
Horst Hummel8f617012006-08-30 14:33:33 +0200398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Device startup state changes.
403 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 int rc;
407
408 rc = 0;
409 if (device->state == DASD_STATE_NEW &&
410 device->target >= DASD_STATE_KNOWN)
411 rc = dasd_state_new_to_known(device);
412
413 if (!rc &&
414 device->state == DASD_STATE_KNOWN &&
415 device->target >= DASD_STATE_BASIC)
416 rc = dasd_state_known_to_basic(device);
417
418 if (!rc &&
419 device->state == DASD_STATE_BASIC &&
420 device->target >= DASD_STATE_READY)
421 rc = dasd_state_basic_to_ready(device);
422
423 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700424 device->state == DASD_STATE_UNFMT &&
425 device->target > DASD_STATE_UNFMT)
426 rc = -EPERM;
427
428 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 device->state == DASD_STATE_READY &&
430 device->target >= DASD_STATE_ONLINE)
431 rc = dasd_state_ready_to_online(device);
432
433 return rc;
434}
435
436/*
437 * Device shutdown state changes.
438 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100439static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Horst Hummel8f617012006-08-30 14:33:33 +0200441 int rc;
442
443 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (device->state == DASD_STATE_ONLINE &&
445 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200446 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200447
Horst Hummel8f617012006-08-30 14:33:33 +0200448 if (!rc &&
449 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200451 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800452
Horst Hummel8f617012006-08-30 14:33:33 +0200453 if (!rc &&
454 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800455 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200456 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800457
Horst Hummel8f617012006-08-30 14:33:33 +0200458 if (!rc &&
459 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200461 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200462
Horst Hummel8f617012006-08-30 14:33:33 +0200463 if (!rc &&
464 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200466 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Horst Hummel8f617012006-08-30 14:33:33 +0200468 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/*
472 * This is the main startup/shutdown routine.
473 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100474static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Sebastian Ott181d9522009-06-22 12:08:21 +0200476 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (device->state == device->target)
479 /* Already where we want to go today... */
480 return;
481 if (device->state < device->target)
482 rc = dasd_increase_state(device);
483 else
484 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200485 if (rc == -EAGAIN)
486 return;
487 if (rc)
488 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Stefan Haberland9eb25122010-02-26 22:37:46 +0100490 if (device->state == device->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200492
493 /* let user-space know that the device status changed */
494 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/*
498 * Kick starter for devices that did not complete the startup/shutdown
499 * procedure or were sleeping because of a pending state.
500 * dasd_kick_device will schedule a call do do_kick_device to the kernel
501 * event daemon.
502 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100503static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Al Viro4927b3f2006-12-06 19:18:20 +0000505 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100506 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100508 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100509 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 dasd_put_device(device);
511}
512
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100513void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 dasd_get_device(device);
516 /* queue call to dasd_kick_device to the kernel event daemon. */
517 schedule_work(&device->kick_work);
518}
519
520/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200521 * dasd_restore_device will schedule a call do do_restore_device to the kernel
522 * event daemon.
523 */
524static void do_restore_device(struct work_struct *work)
525{
526 struct dasd_device *device = container_of(work, struct dasd_device,
527 restore_device);
528 device->cdev->drv->restore(device->cdev);
529 dasd_put_device(device);
530}
531
532void dasd_restore_device(struct dasd_device *device)
533{
534 dasd_get_device(device);
535 /* queue call to dasd_restore_device to the kernel event daemon. */
536 schedule_work(&device->restore_device);
537}
538
539/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * Set the target state for a device and starts the state change.
541 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100542void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200544 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100545 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* If we are in probeonly mode stop at DASD_STATE_READY. */
547 if (dasd_probeonly && target > DASD_STATE_READY)
548 target = DASD_STATE_READY;
549 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100550 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 wake_up(&dasd_init_waitq);
552 device->target = target;
553 }
554 if (device->state != device->target)
555 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100556 mutex_unlock(&device->state_mutex);
557 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
561 * Enable devices with device numbers in [from..to].
562 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100563static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 return (device->state == device->target);
566}
567
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 dasd_set_target_state(device, DASD_STATE_ONLINE);
571 if (device->state <= DASD_STATE_KNOWN)
572 /* No discipline for device found. */
573 dasd_set_target_state(device, DASD_STATE_NEW);
574 /* Now wait for the devices to come up. */
575 wait_event(dasd_init_waitq, _wait_for_device(device));
576}
577
578/*
579 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
580 */
581#ifdef CONFIG_DASD_PROFILE
582
583struct dasd_profile_info_t dasd_global_profile;
584unsigned int dasd_profile_level = DASD_PROFILE_OFF;
585
586/*
587 * Increments counter in global and local profiling structures.
588 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100589#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{ \
591 int index; \
592 for (index = 0; index < 31 && value >> (2+index); index++); \
593 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100594 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Add profiling information for cqr before execution.
599 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100600static void dasd_profile_start(struct dasd_block *block,
601 struct dasd_ccw_req *cqr,
602 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct list_head *l;
605 unsigned int counter;
606
607 if (dasd_profile_level != DASD_PROFILE_ON)
608 return;
609
610 /* count the length of the chanq for statistics */
611 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100612 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (++counter >= 31)
614 break;
615 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100616 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
620 * Add profiling information for cqr after execution.
621 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100622static void dasd_profile_end(struct dasd_block *block,
623 struct dasd_ccw_req *cqr,
624 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 long strtime, irqtime, endtime, tottime; /* in microseconds */
627 long tottimeps, sectors;
628
629 if (dasd_profile_level != DASD_PROFILE_ON)
630 return;
631
Tejun Heo83096eb2009-05-07 22:24:39 +0900632 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!cqr->buildclk || !cqr->startclk ||
634 !cqr->stopclk || !cqr->endclk ||
635 !sectors)
636 return;
637
638 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
639 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
640 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
641 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
642 tottimeps = tottime / sectors;
643
644 if (!dasd_global_profile.dasd_io_reqs)
645 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100646 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dasd_global_profile.dasd_io_reqs++;
648 dasd_global_profile.dasd_io_sects += sectors;
649
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100650 if (!block->profile.dasd_io_reqs)
651 memset(&block->profile, 0,
652 sizeof(struct dasd_profile_info_t));
653 block->profile.dasd_io_reqs++;
654 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100656 dasd_profile_counter(sectors, dasd_io_secs, block);
657 dasd_profile_counter(tottime, dasd_io_times, block);
658 dasd_profile_counter(tottimeps, dasd_io_timps, block);
659 dasd_profile_counter(strtime, dasd_io_time1, block);
660 dasd_profile_counter(irqtime, dasd_io_time2, block);
661 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
662 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100665#define dasd_profile_start(block, cqr, req) do {} while (0)
666#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif /* CONFIG_DASD_PROFILE */
668
669/*
670 * Allocate memory for a channel program with 'cplength' channel
671 * command words and 'datasize' additional space. There are two
672 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
673 * memory and 2) dasd_smalloc_request uses the static ccw memory
674 * that gets allocated for each device.
675 */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200676struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100677 int datasize,
678 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct dasd_ccw_req *cqr;
681
682 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200683 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100684 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800686 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (cqr == NULL)
688 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 cqr->cpaddr = NULL;
690 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800691 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 GFP_ATOMIC | GFP_DMA);
693 if (cqr->cpaddr == NULL) {
694 kfree(cqr);
695 return ERR_PTR(-ENOMEM);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 cqr->data = NULL;
699 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800700 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800702 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 kfree(cqr);
704 return ERR_PTR(-ENOMEM);
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200707 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
709 dasd_get_device(device);
710 return cqr;
711}
712
Stefan Haberland68b781f2009-09-11 10:28:29 +0200713struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100714 int datasize,
715 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 unsigned long flags;
718 struct dasd_ccw_req *cqr;
719 char *data;
720 int size;
721
722 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200723 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100724 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
727 if (cplength > 0)
728 size += cplength * sizeof(struct ccw1);
729 if (datasize > 0)
730 size += datasize;
731 spin_lock_irqsave(&device->mem_lock, flags);
732 cqr = (struct dasd_ccw_req *)
733 dasd_alloc_chunk(&device->ccw_chunks, size);
734 spin_unlock_irqrestore(&device->mem_lock, flags);
735 if (cqr == NULL)
736 return ERR_PTR(-ENOMEM);
737 memset(cqr, 0, sizeof(struct dasd_ccw_req));
738 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
739 cqr->cpaddr = NULL;
740 if (cplength > 0) {
741 cqr->cpaddr = (struct ccw1 *) data;
742 data += cplength*sizeof(struct ccw1);
743 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
744 }
745 cqr->data = NULL;
746 if (datasize > 0) {
747 cqr->data = data;
748 memset(cqr->data, 0, datasize);
749 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200750 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
752 dasd_get_device(device);
753 return cqr;
754}
755
756/*
757 * Free memory of a channel program. This function needs to free all the
758 * idal lists that might have been created by dasd_set_cda and the
759 * struct dasd_ccw_req itself.
760 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100761void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800763#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct ccw1 *ccw;
765
766 /* Clear any idals used for the request. */
767 ccw = cqr->cpaddr;
768 do {
769 clear_normalized_cda(ccw);
770 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
771#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800772 kfree(cqr->cpaddr);
773 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 kfree(cqr);
775 dasd_put_device(device);
776}
777
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100778void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 unsigned long flags;
781
782 spin_lock_irqsave(&device->mem_lock, flags);
783 dasd_free_chunk(&device->ccw_chunks, cqr);
784 spin_unlock_irqrestore(&device->mem_lock, flags);
785 dasd_put_device(device);
786}
787
788/*
789 * Check discipline magic in cqr.
790 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100791static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct dasd_device *device;
794
795 if (cqr == NULL)
796 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100797 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100799 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 " dasd_ccw_req 0x%08x magic doesn't match"
801 " discipline 0x%08x",
802 cqr->magic,
803 *(unsigned int *) device->discipline->name);
804 return -EINVAL;
805 }
806 return 0;
807}
808
809/*
810 * Terminate the current i/o and set the request to clear_pending.
811 * Timer keeps device runnig.
812 * ccw_device_clear can fail if the i/o subsystem
813 * is in a bad mood.
814 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100815int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct dasd_device *device;
818 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100819 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Check the cqr */
822 rc = dasd_check_cqr(cqr);
823 if (rc)
824 return rc;
825 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100826 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
828 rc = ccw_device_clear(device->cdev, (long) cqr);
829 switch (rc) {
830 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800831 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100832 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200834 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 DBF_DEV_EVENT(DBF_DEBUG, device,
836 "terminate cqr %p successful",
837 cqr);
838 break;
839 case -ENODEV:
840 DBF_DEV_EVENT(DBF_ERR, device, "%s",
841 "device gone, retry");
842 break;
843 case -EIO:
844 DBF_DEV_EVENT(DBF_ERR, device, "%s",
845 "I/O error, retry");
846 break;
847 case -EINVAL:
848 case -EBUSY:
849 DBF_DEV_EVENT(DBF_ERR, device, "%s",
850 "device busy, retry later");
851 break;
852 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100853 /* internal error 10 - unknown rc*/
854 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
855 dev_err(&device->cdev->dev, "An error occurred in the "
856 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 BUG();
858 break;
859 }
860 retries++;
861 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100862 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return rc;
864}
865
866/*
867 * Start the i/o. This start_IO can fail if the channel is really busy.
868 * In that case set up a timer to start the request later.
869 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100870int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct dasd_device *device;
873 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100874 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Check the cqr */
877 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200878 if (rc) {
879 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200881 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100882 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100884 /* internal error 14 - start_IO run out of retries */
885 sprintf(errorstring, "14 %p", cqr);
886 dev_err(&device->cdev->dev, "An error occurred in the DASD "
887 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100888 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EIO;
890 }
891 cqr->startclk = get_clock();
892 cqr->starttime = jiffies;
893 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100894 if (cqr->cpmode == 1) {
895 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
896 (long) cqr, cqr->lpm);
897 } else {
898 rc = ccw_device_start(device->cdev, cqr->cpaddr,
899 (long) cqr, cqr->lpm, 0);
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 switch (rc) {
902 case 0:
903 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
905 case -EBUSY:
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: device busy, retry later");
908 break;
909 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100910 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 "start_IO: request timeout, retry later");
912 break;
913 case -EACCES:
914 /* -EACCES indicates that the request used only a
915 * subset of the available pathes and all these
916 * pathes are gone.
917 * Do a retry with all available pathes.
918 */
919 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100920 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 "start_IO: selected pathes gone,"
922 " retry on all pathes");
923 break;
924 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100925 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
926 "start_IO: -ENODEV device gone, retry");
927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100929 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100930 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200932 case -EINVAL:
933 /* most likely caused in power management context */
934 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
935 "start_IO: -EINVAL device currently "
936 "not accessible");
937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100939 /* internal error 11 - unknown rc */
940 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
941 dev_err(&device->cdev->dev,
942 "An error occurred in the DASD device driver, "
943 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 BUG();
945 break;
946 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200947 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return rc;
949}
950
951/*
952 * Timeout function for dasd devices. This is used for different purposes
953 * 1) missing interrupt handler for normal operation
954 * 2) delayed start of request where start_IO failed with -EBUSY
955 * 3) timeout for missing state change interrupts
956 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
957 * DASD_CQR_QUEUED for 2) and 3).
958 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100959static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 unsigned long flags;
962 struct dasd_device *device;
963
964 device = (struct dasd_device *) ptr;
965 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
966 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100967 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100969 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
973 * Setup timeout for a device in jiffies.
974 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100975void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100977 if (expires == 0)
978 del_timer(&device->timer);
979 else
980 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Clear timeout for a device.
985 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100986void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100988 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100991static void dasd_handle_killed_request(struct ccw_device *cdev,
992 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct dasd_ccw_req *cqr;
995 struct dasd_device *device;
996
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200997 if (!intparm)
998 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 cqr = (struct dasd_ccw_req *) intparm;
1000 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001001 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1002 "invalid status in handle_killed_request: "
1003 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006
Stefan Haberland589c74d2010-02-26 22:37:47 +01001007 device = dasd_device_from_cdev_locked(cdev);
1008 if (IS_ERR(device)) {
1009 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1010 "unable to get device from cdev");
1011 return;
1012 }
1013
1014 if (!cqr->startdev ||
1015 device != cqr->startdev ||
1016 strncmp(cqr->startdev->discipline->ebcname,
1017 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001018 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1019 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001020 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return;
1022 }
1023
1024 /* Schedule request to be retried. */
1025 cqr->status = DASD_CQR_QUEUED;
1026
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001027 dasd_device_clear_timer(device);
1028 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 dasd_put_device(device);
1030}
1031
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001032void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001034 /* First of all start sense subsystem status request. */
1035 dasd_eer_snss(device);
1036
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001037 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001038 dasd_schedule_device_bh(device);
1039 if (device->block)
1040 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/*
1044 * Interrupt handler for "normal" ssch-io based dasd devices.
1045 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001046void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1047 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct dasd_ccw_req *cqr, *next;
1050 struct dasd_device *device;
1051 unsigned long long now;
1052 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (IS_ERR(irb)) {
1055 switch (PTR_ERR(irb)) {
1056 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001059 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1060 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001063 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1064 "unknown error %ld\n", __func__,
1065 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001067 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
1069 }
1070
1071 now = get_clock();
1072
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001073 /* check for unsolicited interrupts */
1074 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001075 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1076 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1077 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001078 if (cqr && cqr->status == DASD_CQR_IN_IO)
1079 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001080 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001082 dasd_device_clear_timer(device);
1083 device->discipline->handle_unsolicited_interrupt(device,
1084 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 dasd_put_device(device);
1086 }
1087 return;
1088 }
1089
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001090 device = (struct dasd_device *) cqr->startdev;
1091 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001093 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1094 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return;
1096 }
1097
1098 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001099 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001100 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001101 cqr->status = DASD_CQR_CLEARED;
1102 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001103 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001104 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return;
1106 }
1107
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001108 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001110 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1111 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return;
1113 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001114
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001115 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001117 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1118 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001119 /* request was completed successfully */
1120 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 cqr->stopclk = now;
1122 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001123 if (cqr->devlist.next != &device->ccw_queue) {
1124 next = list_entry(cqr->devlist.next,
1125 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001127 } else { /* error */
1128 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001129 /* log sense for every failed I/O to s390 debugfeature */
1130 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001131 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001132 dasd_log_sense(cqr, irb);
1133 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001134
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001135 /*
1136 * If we don't want complex ERP for this request, then just
1137 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001138 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001139 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001140 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001141 if (cqr->lpm == LPM_ANYPATH)
1142 DBF_DEV_EVENT(DBF_DEBUG, device,
1143 "default ERP in fastpath "
1144 "(%i retries left)",
1145 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001146 cqr->lpm = LPM_ANYPATH;
1147 cqr->status = DASD_CQR_QUEUED;
1148 next = cqr;
1149 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001151 }
1152 if (next && (next->status == DASD_CQR_QUEUED) &&
1153 (!device->stopped)) {
1154 if (device->discipline->start_IO(next) == 0)
1155 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001158 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001160 dasd_device_clear_timer(device);
1161 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001165 * If we have an error on a dasd_block layer request then we cancel
1166 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001168static void __dasd_device_recovery(struct dasd_device *device,
1169 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct list_head *l, *n;
1172 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001175 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001177 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001179
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001180 list_for_each_safe(l, n, &device->ccw_queue) {
1181 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1182 if (cqr->status == DASD_CQR_QUEUED &&
1183 ref_cqr->block == cqr->block) {
1184 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001186 }
1187};
1188
1189/*
1190 * Remove those ccw requests from the queue that need to be returned
1191 * to the upper layer.
1192 */
1193static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1194 struct list_head *final_queue)
1195{
1196 struct list_head *l, *n;
1197 struct dasd_ccw_req *cqr;
1198
1199 /* Process request with final status. */
1200 list_for_each_safe(l, n, &device->ccw_queue) {
1201 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1202
1203 /* Stop list processing at the first non-final request. */
1204 if (cqr->status == DASD_CQR_QUEUED ||
1205 cqr->status == DASD_CQR_IN_IO ||
1206 cqr->status == DASD_CQR_CLEAR_PENDING)
1207 break;
1208 if (cqr->status == DASD_CQR_ERROR) {
1209 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001211 /* Rechain finished requests to final queue */
1212 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
1216/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001217 * the cqrs from the final queue are returned to the upper layer
1218 * by setting a dasd_block state and calling the callback function
1219 */
1220static void __dasd_device_process_final_queue(struct dasd_device *device,
1221 struct list_head *final_queue)
1222{
1223 struct list_head *l, *n;
1224 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001225 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001226 void (*callback)(struct dasd_ccw_req *, void *data);
1227 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001228 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229
1230 list_for_each_safe(l, n, final_queue) {
1231 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1232 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001233 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001234 callback = cqr->callback;
1235 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001236 if (block)
1237 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001238 switch (cqr->status) {
1239 case DASD_CQR_SUCCESS:
1240 cqr->status = DASD_CQR_DONE;
1241 break;
1242 case DASD_CQR_ERROR:
1243 cqr->status = DASD_CQR_NEED_ERP;
1244 break;
1245 case DASD_CQR_CLEARED:
1246 cqr->status = DASD_CQR_TERMINATED;
1247 break;
1248 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001249 /* internal error 12 - wrong cqr status*/
1250 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1251 dev_err(&device->cdev->dev,
1252 "An error occurred in the DASD device driver, "
1253 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001254 BUG();
1255 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001256 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001257 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001258 if (block)
1259 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001260 }
1261}
1262
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001263/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * Take a look at the first request on the ccw queue and check
1265 * if it reached its expire time. If so, terminate the IO.
1266 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001267static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct dasd_ccw_req *cqr;
1270
1271 if (list_empty(&device->ccw_queue))
1272 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001273 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001274 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1275 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1276 if (device->discipline->term_IO(cqr) != 0) {
1277 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001278 dev_err(&device->cdev->dev,
1279 "cqr %p timed out (%is) but cannot be "
1280 "ended, retrying in 5 s\n",
1281 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001282 cqr->expires += 5*HZ;
1283 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001284 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001285 dev_err(&device->cdev->dev,
1286 "cqr %p timed out (%is), %i retries "
1287 "remaining\n", cqr, (cqr->expires/HZ),
1288 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 }
1291}
1292
1293/*
1294 * Take a look at the first request on the ccw queue and check
1295 * if it needs to be started.
1296 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001297static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct dasd_ccw_req *cqr;
1300 int rc;
1301
1302 if (list_empty(&device->ccw_queue))
1303 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001304 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001305 if (cqr->status != DASD_CQR_QUEUED)
1306 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001307 /* when device is stopped, return request to previous layer */
1308 if (device->stopped) {
1309 cqr->status = DASD_CQR_CLEARED;
1310 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001311 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001312 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001313
1314 rc = device->discipline->start_IO(cqr);
1315 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001317 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001318 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001319 } else
1320 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001321 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001325 * Go through all request on the dasd_device request queue,
1326 * terminate them on the cdev if necessary, and return them to the
1327 * submitting layer via callback.
1328 * Note:
1329 * Make sure that all 'submitting layers' still exist when
1330 * this function is called!. In other words, when 'device' is a base
1331 * device then all block layer requests must have been removed before
1332 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001334int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001336 struct dasd_ccw_req *cqr, *n;
1337 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 INIT_LIST_HEAD(&flush_queue);
1341 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001342 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001343 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001344 /* Check status and move request to flush_queue */
1345 switch (cqr->status) {
1346 case DASD_CQR_IN_IO:
1347 rc = device->discipline->term_IO(cqr);
1348 if (rc) {
1349 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001350 dev_err(&device->cdev->dev,
1351 "Flushing the DASD request queue "
1352 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001353 /* stop flush processing */
1354 goto finished;
1355 }
1356 break;
1357 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001360 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001361 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001362 break;
1363 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001365 }
Horst Hummel8f617012006-08-30 14:33:33 +02001366finished:
1367 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001368 /*
1369 * After this point all requests must be in state CLEAR_PENDING,
1370 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1371 * one of the others.
1372 */
1373 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1374 wait_event(dasd_flush_wq,
1375 (cqr->status != DASD_CQR_CLEAR_PENDING));
1376 /*
1377 * Now set each request back to TERMINATED, DONE or NEED_ERP
1378 * and call the callback function of flushed requests
1379 */
1380 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001381 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384/*
1385 * Acquire the device lock and process queues for the device.
1386 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 atomic_set (&device->tasklet_scheduled, 0);
1392 INIT_LIST_HEAD(&final_queue);
1393 spin_lock_irq(get_ccwdev_lock(device->cdev));
1394 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001395 __dasd_device_check_expire(device);
1396 /* find final requests on ccw queue */
1397 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1399 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001400 __dasd_device_process_final_queue(device, &final_queue);
1401 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403 __dasd_device_start_head(device);
1404 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 dasd_put_device(device);
1406}
1407
1408/*
1409 * Schedules a call to dasd_tasklet over the device tasklet.
1410 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001411void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001414 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return;
1416 dasd_get_device(device);
1417 tasklet_hi_schedule(&device->tasklet);
1418}
1419
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001420void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1421{
1422 device->stopped |= bits;
1423}
1424EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1425
1426void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1427{
1428 device->stopped &= ~bits;
1429 if (!device->stopped)
1430 wake_up(&generic_waitq);
1431}
1432EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001435 * Queue a request to the head of the device ccw_queue.
1436 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001438void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct dasd_device *device;
1441 unsigned long flags;
1442
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001443 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001445 cqr->status = DASD_CQR_QUEUED;
1446 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001448 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1450}
1451
1452/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001453 * Queue a request to the tail of the device ccw_queue.
1454 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001456void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct dasd_device *device;
1459 unsigned long flags;
1460
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001461 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001463 cqr->status = DASD_CQR_QUEUED;
1464 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001466 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1468}
1469
1470/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001471 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001473static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 wake_up((wait_queue_head_t *) data);
1476}
1477
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001478static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct dasd_device *device;
1481 int rc;
1482
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001483 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001485 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001486 cqr->status == DASD_CQR_NEED_ERP ||
1487 cqr->status == DASD_CQR_TERMINATED) &&
1488 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1490 return rc;
1491}
1492
1493/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001494 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1495 */
1496static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1497{
1498 struct dasd_device *device;
1499 dasd_erp_fn_t erp_fn;
1500
1501 if (cqr->status == DASD_CQR_FILLED)
1502 return 0;
1503 device = cqr->startdev;
1504 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1505 if (cqr->status == DASD_CQR_TERMINATED) {
1506 device->discipline->handle_terminated_request(cqr);
1507 return 1;
1508 }
1509 if (cqr->status == DASD_CQR_NEED_ERP) {
1510 erp_fn = device->discipline->erp_action(cqr);
1511 erp_fn(cqr);
1512 return 1;
1513 }
1514 if (cqr->status == DASD_CQR_FAILED)
1515 dasd_log_sense(cqr, &cqr->irb);
1516 if (cqr->refers) {
1517 __dasd_process_erp(device, cqr);
1518 return 1;
1519 }
1520 }
1521 return 0;
1522}
1523
1524static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1525{
1526 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1527 if (cqr->refers) /* erp is not done yet */
1528 return 1;
1529 return ((cqr->status != DASD_CQR_DONE) &&
1530 (cqr->status != DASD_CQR_FAILED));
1531 } else
1532 return (cqr->status == DASD_CQR_FILLED);
1533}
1534
1535static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1536{
1537 struct dasd_device *device;
1538 int rc;
1539 struct list_head ccw_queue;
1540 struct dasd_ccw_req *cqr;
1541
1542 INIT_LIST_HEAD(&ccw_queue);
1543 maincqr->status = DASD_CQR_FILLED;
1544 device = maincqr->startdev;
1545 list_add(&maincqr->blocklist, &ccw_queue);
1546 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1547 cqr = list_first_entry(&ccw_queue,
1548 struct dasd_ccw_req, blocklist)) {
1549
1550 if (__dasd_sleep_on_erp(cqr))
1551 continue;
1552 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1553 continue;
1554
1555 /* Non-temporary stop condition will trigger fail fast */
1556 if (device->stopped & ~DASD_STOPPED_PENDING &&
1557 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1558 (!dasd_eer_enabled(device))) {
1559 cqr->status = DASD_CQR_FAILED;
1560 continue;
1561 }
1562
1563 /* Don't try to start requests if device is stopped */
1564 if (interruptible) {
1565 rc = wait_event_interruptible(
1566 generic_waitq, !(device->stopped));
1567 if (rc == -ERESTARTSYS) {
1568 cqr->status = DASD_CQR_FAILED;
1569 maincqr->intrc = rc;
1570 continue;
1571 }
1572 } else
1573 wait_event(generic_waitq, !(device->stopped));
1574
1575 cqr->callback = dasd_wakeup_cb;
1576 cqr->callback_data = (void *) &generic_waitq;
1577 dasd_add_request_tail(cqr);
1578 if (interruptible) {
1579 rc = wait_event_interruptible(
1580 generic_waitq, _wait_for_wakeup(cqr));
1581 if (rc == -ERESTARTSYS) {
1582 dasd_cancel_req(cqr);
1583 /* wait (non-interruptible) for final status */
1584 wait_event(generic_waitq,
1585 _wait_for_wakeup(cqr));
1586 cqr->status = DASD_CQR_FAILED;
1587 maincqr->intrc = rc;
1588 continue;
1589 }
1590 } else
1591 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1592 }
1593
1594 maincqr->endclk = get_clock();
1595 if ((maincqr->status != DASD_CQR_DONE) &&
1596 (maincqr->intrc != -ERESTARTSYS))
1597 dasd_log_sense(maincqr, &maincqr->irb);
1598 if (maincqr->status == DASD_CQR_DONE)
1599 rc = 0;
1600 else if (maincqr->intrc)
1601 rc = maincqr->intrc;
1602 else
1603 rc = -EIO;
1604 return rc;
1605}
1606
1607/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001608 * Queue a request to the tail of the device ccw_queue and wait for
1609 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001611int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001613 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001617 * Queue a request to the tail of the device ccw_queue and wait
1618 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001620int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001622 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1627 * for eckd devices) the currently running request has to be terminated
1628 * and be put back to status queued, before the special request is added
1629 * to the head of the queue. Then the special request is waited on normally.
1630 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001631static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (list_empty(&device->ccw_queue))
1636 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001637 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001638 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001641int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct dasd_device *device;
1644 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001645
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001646 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 spin_lock_irq(get_ccwdev_lock(device->cdev));
1648 rc = _dasd_term_running_cqr(device);
1649 if (rc) {
1650 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1651 return rc;
1652 }
Horst Hummel138c0142006-06-29 14:58:12 +02001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001655 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001657 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001660 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1663
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001664 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001665
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001666 if (cqr->status == DASD_CQR_DONE)
1667 rc = 0;
1668 else if (cqr->intrc)
1669 rc = cqr->intrc;
1670 else
1671 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return rc;
1673}
1674
1675/*
1676 * Cancels a request that was started with dasd_sleep_on_req.
1677 * This is useful to timeout requests. The request will be
1678 * terminated if it is currently in i/o.
1679 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680 * 0 if there was no need to terminate the request (not started yet)
1681 * negative error code if termination failed
1682 * Cancellation of a request is an asynchronous operation! The calling
1683 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001685int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001687 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 unsigned long flags;
1689 int rc;
1690
1691 rc = 0;
1692 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1693 switch (cqr->status) {
1694 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001695 /* request was not started - just set to cleared */
1696 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
1698 case DASD_CQR_IN_IO:
1699 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001700 rc = device->discipline->term_IO(cqr);
1701 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001702 dev_err(&device->cdev->dev,
1703 "Cancelling request %p failed with rc=%d\n",
1704 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001705 } else {
1706 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001709 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001713 dasd_schedule_device_bh(device);
1714 return rc;
1715}
1716
1717
1718/*
1719 * SECTION: Operations of the dasd_block layer.
1720 */
1721
1722/*
1723 * Timeout function for dasd_block. This is used when the block layer
1724 * is waiting for something that may not come reliably, (e.g. a state
1725 * change interrupt)
1726 */
1727static void dasd_block_timeout(unsigned long ptr)
1728{
1729 unsigned long flags;
1730 struct dasd_block *block;
1731
1732 block = (struct dasd_block *) ptr;
1733 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1734 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001735 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001736 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1737 dasd_schedule_block_bh(block);
1738}
1739
1740/*
1741 * Setup timeout for a dasd_block in jiffies.
1742 */
1743void dasd_block_set_timer(struct dasd_block *block, int expires)
1744{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001745 if (expires == 0)
1746 del_timer(&block->timer);
1747 else
1748 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749}
1750
1751/*
1752 * Clear timeout for a dasd_block.
1753 */
1754void dasd_block_clear_timer(struct dasd_block *block)
1755{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001756 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001757}
1758
1759/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 * Process finished error recovery ccw.
1761 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001762static void __dasd_process_erp(struct dasd_device *device,
1763 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001764{
1765 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001766
1767 if (cqr->status == DASD_CQR_DONE)
1768 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1769 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001770 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001771 erp_fn = device->discipline->erp_postaction(cqr);
1772 erp_fn(cqr);
1773}
1774
1775/*
1776 * Fetch requests from the block device queue.
1777 */
1778static void __dasd_process_request_queue(struct dasd_block *block)
1779{
1780 struct request_queue *queue;
1781 struct request *req;
1782 struct dasd_ccw_req *cqr;
1783 struct dasd_device *basedev;
1784 unsigned long flags;
1785 queue = block->request_queue;
1786 basedev = block->base;
1787 /* No queue ? Then there is nothing to do. */
1788 if (queue == NULL)
1789 return;
1790
1791 /*
1792 * We requeue request from the block device queue to the ccw
1793 * queue only in two states. In state DASD_STATE_READY the
1794 * partition detection is done and we need to requeue requests
1795 * for that. State DASD_STATE_ONLINE is normal block device
1796 * operation.
1797 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001798 if (basedev->state < DASD_STATE_READY) {
1799 while ((req = blk_fetch_request(block->request_queue)))
1800 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001801 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001802 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001803 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001804 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001805 if (basedev->features & DASD_FEATURE_READONLY &&
1806 rq_data_dir(req) == WRITE) {
1807 DBF_DEV_EVENT(DBF_ERR, basedev,
1808 "Rejecting write request %p",
1809 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001810 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001811 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001812 continue;
1813 }
1814 cqr = basedev->discipline->build_cp(basedev, block, req);
1815 if (IS_ERR(cqr)) {
1816 if (PTR_ERR(cqr) == -EBUSY)
1817 break; /* normal end condition */
1818 if (PTR_ERR(cqr) == -ENOMEM)
1819 break; /* terminate request queue loop */
1820 if (PTR_ERR(cqr) == -EAGAIN) {
1821 /*
1822 * The current request cannot be build right
1823 * now, we have to try later. If this request
1824 * is the head-of-queue we stop the device
1825 * for 1/2 second.
1826 */
1827 if (!list_empty(&block->ccw_queue))
1828 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001829 spin_lock_irqsave(
1830 get_ccwdev_lock(basedev->cdev), flags);
1831 dasd_device_set_stop_bits(basedev,
1832 DASD_STOPPED_PENDING);
1833 spin_unlock_irqrestore(
1834 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001835 dasd_block_set_timer(block, HZ/2);
1836 break;
1837 }
1838 DBF_DEV_EVENT(DBF_ERR, basedev,
1839 "CCW creation failed (rc=%ld) "
1840 "on request %p",
1841 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001842 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001843 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001844 continue;
1845 }
1846 /*
1847 * Note: callback is set to dasd_return_cqr_cb in
1848 * __dasd_block_start_head to cover erp requests as well
1849 */
1850 cqr->callback_data = (void *) req;
1851 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001852 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001853 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1854 dasd_profile_start(block, cqr, req);
1855 }
1856}
1857
1858static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1859{
1860 struct request *req;
1861 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001862 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001863
1864 req = (struct request *) cqr->callback_data;
1865 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001866 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001867 if (status <= 0)
1868 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001869 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001870}
1871
1872/*
1873 * Process ccw request queue.
1874 */
1875static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1876 struct list_head *final_queue)
1877{
1878 struct list_head *l, *n;
1879 struct dasd_ccw_req *cqr;
1880 dasd_erp_fn_t erp_fn;
1881 unsigned long flags;
1882 struct dasd_device *base = block->base;
1883
1884restart:
1885 /* Process request with final status. */
1886 list_for_each_safe(l, n, &block->ccw_queue) {
1887 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1888 if (cqr->status != DASD_CQR_DONE &&
1889 cqr->status != DASD_CQR_FAILED &&
1890 cqr->status != DASD_CQR_NEED_ERP &&
1891 cqr->status != DASD_CQR_TERMINATED)
1892 continue;
1893
1894 if (cqr->status == DASD_CQR_TERMINATED) {
1895 base->discipline->handle_terminated_request(cqr);
1896 goto restart;
1897 }
1898
1899 /* Process requests that may be recovered */
1900 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001901 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02001902 if (IS_ERR(erp_fn(cqr)))
1903 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001904 goto restart;
1905 }
1906
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001907 /* log sense for fatal error */
1908 if (cqr->status == DASD_CQR_FAILED) {
1909 dasd_log_sense(cqr, &cqr->irb);
1910 }
1911
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001912 /* First of all call extended error reporting. */
1913 if (dasd_eer_enabled(base) &&
1914 cqr->status == DASD_CQR_FAILED) {
1915 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1916
1917 /* restart request */
1918 cqr->status = DASD_CQR_FILLED;
1919 cqr->retries = 255;
1920 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001921 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001922 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1923 flags);
1924 goto restart;
1925 }
1926
1927 /* Process finished ERP request. */
1928 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001929 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001930 goto restart;
1931 }
1932
1933 /* Rechain finished requests to final queue */
1934 cqr->endclk = get_clock();
1935 list_move_tail(&cqr->blocklist, final_queue);
1936 }
1937}
1938
1939static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1940{
1941 dasd_schedule_block_bh(cqr->block);
1942}
1943
1944static void __dasd_block_start_head(struct dasd_block *block)
1945{
1946 struct dasd_ccw_req *cqr;
1947
1948 if (list_empty(&block->ccw_queue))
1949 return;
1950 /* We allways begin with the first requests on the queue, as some
1951 * of previously started requests have to be enqueued on a
1952 * dasd_device again for error recovery.
1953 */
1954 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1955 if (cqr->status != DASD_CQR_FILLED)
1956 continue;
1957 /* Non-temporary stop condition will trigger fail fast */
1958 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1959 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1960 (!dasd_eer_enabled(block->base))) {
1961 cqr->status = DASD_CQR_FAILED;
1962 dasd_schedule_block_bh(block);
1963 continue;
1964 }
1965 /* Don't try to start requests if device is stopped */
1966 if (block->base->stopped)
1967 return;
1968
1969 /* just a fail safe check, should not happen */
1970 if (!cqr->startdev)
1971 cqr->startdev = block->base;
1972
1973 /* make sure that the requests we submit find their way back */
1974 cqr->callback = dasd_return_cqr_cb;
1975
1976 dasd_add_request_tail(cqr);
1977 }
1978}
1979
1980/*
1981 * Central dasd_block layer routine. Takes requests from the generic
1982 * block layer request queue, creates ccw requests, enqueues them on
1983 * a dasd_device and processes ccw requests that have been returned.
1984 */
1985static void dasd_block_tasklet(struct dasd_block *block)
1986{
1987 struct list_head final_queue;
1988 struct list_head *l, *n;
1989 struct dasd_ccw_req *cqr;
1990
1991 atomic_set(&block->tasklet_scheduled, 0);
1992 INIT_LIST_HEAD(&final_queue);
1993 spin_lock(&block->queue_lock);
1994 /* Finish off requests on ccw queue */
1995 __dasd_process_block_ccw_queue(block, &final_queue);
1996 spin_unlock(&block->queue_lock);
1997 /* Now call the callback function of requests with final status */
1998 spin_lock_irq(&block->request_queue_lock);
1999 list_for_each_safe(l, n, &final_queue) {
2000 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2001 list_del_init(&cqr->blocklist);
2002 __dasd_cleanup_cqr(cqr);
2003 }
2004 spin_lock(&block->queue_lock);
2005 /* Get new request from the block device request queue */
2006 __dasd_process_request_queue(block);
2007 /* Now check if the head of the ccw queue needs to be started. */
2008 __dasd_block_start_head(block);
2009 spin_unlock(&block->queue_lock);
2010 spin_unlock_irq(&block->request_queue_lock);
2011 dasd_put_device(block->base);
2012}
2013
2014static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2015{
2016 wake_up(&dasd_flush_wq);
2017}
2018
2019/*
2020 * Go through all request on the dasd_block request queue, cancel them
2021 * on the respective dasd_device, and return them to the generic
2022 * block layer.
2023 */
2024static int dasd_flush_block_queue(struct dasd_block *block)
2025{
2026 struct dasd_ccw_req *cqr, *n;
2027 int rc, i;
2028 struct list_head flush_queue;
2029
2030 INIT_LIST_HEAD(&flush_queue);
2031 spin_lock_bh(&block->queue_lock);
2032 rc = 0;
2033restart:
2034 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2035 /* if this request currently owned by a dasd_device cancel it */
2036 if (cqr->status >= DASD_CQR_QUEUED)
2037 rc = dasd_cancel_req(cqr);
2038 if (rc < 0)
2039 break;
2040 /* Rechain request (including erp chain) so it won't be
2041 * touched by the dasd_block_tasklet anymore.
2042 * Replace the callback so we notice when the request
2043 * is returned from the dasd_device layer.
2044 */
2045 cqr->callback = _dasd_wake_block_flush_cb;
2046 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2047 list_move_tail(&cqr->blocklist, &flush_queue);
2048 if (i > 1)
2049 /* moved more than one request - need to restart */
2050 goto restart;
2051 }
2052 spin_unlock_bh(&block->queue_lock);
2053 /* Now call the callback function of flushed requests */
2054restart_cb:
2055 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2056 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2057 /* Process finished ERP request. */
2058 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002059 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002060 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002061 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002062 /* restart list_for_xx loop since dasd_process_erp
2063 * might remove multiple elements */
2064 goto restart_cb;
2065 }
2066 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002067 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002068 cqr->endclk = get_clock();
2069 list_del_init(&cqr->blocklist);
2070 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002071 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return rc;
2074}
2075
2076/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077 * Schedules a call to dasd_tasklet over the device tasklet.
2078 */
2079void dasd_schedule_block_bh(struct dasd_block *block)
2080{
2081 /* Protect against rescheduling. */
2082 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2083 return;
2084 /* life cycle of block is bound to it's base device */
2085 dasd_get_device(block->base);
2086 tasklet_hi_schedule(&block->tasklet);
2087}
2088
2089
2090/*
2091 * SECTION: external block device operations
2092 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
2094
2095/*
2096 * Dasd request queue function. Called from ll_rw_blk.c
2097 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002098static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002100 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 block = queue->queuedata;
2103 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002105 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002107 __dasd_block_start_head(block);
2108 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111/*
2112 * Allocate and initialize request queue and default I/O scheduler.
2113 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002114static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 int rc;
2117
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002118 block->request_queue = blk_init_queue(do_dasd_request,
2119 &block->request_queue_lock);
2120 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -ENOMEM;
2122
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002123 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002125 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002126 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002127 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002129 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return rc;
2131 }
2132 return 0;
2133}
2134
2135/*
2136 * Allocate and initialize request queue.
2137 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002138static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 int max;
2141
Martin K. Petersene1defc42009-05-22 17:17:49 -04002142 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002143 max = block->base->discipline->max_blocks << block->s2b_shift;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002144 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002145 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002146 /* with page sized segments we can translate each segement into
2147 * one idaw/tidaw
2148 */
2149 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2150 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002151 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154/*
2155 * Deactivate and free request queue.
2156 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002157static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002159 if (block->request_queue) {
2160 blk_cleanup_queue(block->request_queue);
2161 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163}
2164
2165/*
2166 * Flush request on the request queue.
2167 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002168static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct request *req;
2171
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002172 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002174
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002175 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002176 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002177 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002178 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Al Viro57a7c0b2008-03-02 10:36:08 -05002181static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Al Viro57a7c0b2008-03-02 10:36:08 -05002183 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Haberland9eb25122010-02-26 22:37:46 +01002184 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int rc;
2186
Stefan Haberland9eb25122010-02-26 22:37:46 +01002187 if (!block)
2188 return -ENODEV;
2189
2190 base = block->base;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002191 atomic_inc(&block->open_count);
2192 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 rc = -ENODEV;
2194 goto unlock;
2195 }
2196
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002197 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 rc = -EINVAL;
2199 goto unlock;
2200 }
2201
2202 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002203 dev_info(&base->cdev->dev,
2204 "Accessing the DASD failed because it is in "
2205 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 rc = -EPERM;
2207 goto out;
2208 }
2209
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002210 if (base->state <= DASD_STATE_BASIC) {
2211 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 " Cannot open unrecognized device");
2213 rc = -ENODEV;
2214 goto out;
2215 }
2216
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002217 if ((mode & FMODE_WRITE) &&
2218 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2219 (base->features & DASD_FEATURE_READONLY))) {
2220 rc = -EROFS;
2221 goto out;
2222 }
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return 0;
2225
2226out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002227 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002229 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return rc;
2231}
2232
Al Viro57a7c0b2008-03-02 10:36:08 -05002233static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002235 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002237 atomic_dec(&block->open_count);
2238 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 0;
2240}
2241
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002242/*
2243 * Return disk geometry.
2244 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002245static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002246{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002247 struct dasd_block *block;
2248 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002249
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002250 block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002251 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002252 return -ENODEV;
Julia Lawallcf05b822009-08-23 18:09:05 +02002253 base = block->base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002254
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002255 if (!base->discipline ||
2256 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002257 return -EINVAL;
2258
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002259 base->discipline->fill_geometry(block, geo);
2260 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002261 return 0;
2262}
2263
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002264const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265dasd_device_operations = {
2266 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002267 .open = dasd_open,
2268 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002269 .ioctl = dasd_ioctl,
2270 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002271 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272};
2273
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002274/*******************************************************************************
2275 * end of block device operations
2276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278static void
2279dasd_exit(void)
2280{
2281#ifdef CONFIG_PROC_FS
2282 dasd_proc_exit();
2283#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002284 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002285 if (dasd_page_cache != NULL) {
2286 kmem_cache_destroy(dasd_page_cache);
2287 dasd_page_cache = NULL;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 dasd_gendisk_exit();
2290 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (dasd_debug_area != NULL) {
2292 debug_unregister(dasd_debug_area);
2293 dasd_debug_area = NULL;
2294 }
2295}
2296
2297/*
2298 * SECTION: common functions for ccw_driver use
2299 */
2300
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002301/*
2302 * Is the device read-only?
2303 * Note that this function does not report the setting of the
2304 * readonly device attribute, but how it is configured in z/VM.
2305 */
2306int dasd_device_is_ro(struct dasd_device *device)
2307{
2308 struct ccw_dev_id dev_id;
2309 struct diag210 diag_data;
2310 int rc;
2311
2312 if (!MACHINE_IS_VM)
2313 return 0;
2314 ccw_device_get_id(device->cdev, &dev_id);
2315 memset(&diag_data, 0, sizeof(diag_data));
2316 diag_data.vrdcdvno = dev_id.devno;
2317 diag_data.vrdclen = sizeof(diag_data);
2318 rc = diag210(&diag_data);
2319 if (rc == 0 || rc == 2) {
2320 return diag_data.vrdcvfla & 0x80;
2321 } else {
2322 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2323 dev_id.devno, rc);
2324 return 0;
2325 }
2326}
2327EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2328
Cornelia Huckf3445a12009-04-14 15:36:23 +02002329static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2330{
2331 struct ccw_device *cdev = data;
2332 int ret;
2333
2334 ret = ccw_device_set_online(cdev);
2335 if (ret)
2336 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2337 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002338}
2339
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002340/*
2341 * Initial attempt at a probe function. this can be simplified once
2342 * the other detection code is gone.
2343 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002344int dasd_generic_probe(struct ccw_device *cdev,
2345 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int ret;
2348
2349 ret = dasd_add_sysfs_files(cdev);
2350 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002351 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2352 "dasd_generic_probe: could not add "
2353 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002354 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Horst Hummel40545572006-06-29 15:08:18 +02002356 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Horst Hummel40545572006-06-29 15:08:18 +02002358 /*
2359 * Automatically online either all dasd devices (dasd_autodetect)
2360 * or all devices specified with dasd= parameters during
2361 * initial probe.
2362 */
2363 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002364 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002365 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002369/*
2370 * This will one day be called from a global not_oper handler.
2371 * It is also used by driver_unregister during module unload.
2372 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002373void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002376 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Horst Hummel59afda72005-05-16 21:53:39 -07002378 cdev->handler = NULL;
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 dasd_remove_sysfs_files(cdev);
2381 device = dasd_device_from_cdev(cdev);
2382 if (IS_ERR(device))
2383 return;
2384 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2385 /* Already doing offline processing */
2386 dasd_put_device(device);
2387 return;
2388 }
2389 /*
2390 * This device is removed unconditionally. Set offline
2391 * flag to prevent dasd_open from opening it while it is
2392 * no quite down yet.
2393 */
2394 dasd_set_target_state(device, DASD_STATE_NEW);
2395 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002396 block = device->block;
2397 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002399 /*
2400 * life cycle of block is bound to device, so delete it after
2401 * device was safely removed
2402 */
2403 if (block)
2404 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002407/*
2408 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002410 * or the user has started activation through sysfs.
2411 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002412int dasd_generic_set_online(struct ccw_device *cdev,
2413 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002415 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002417 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002418
Horst Hummel40545572006-06-29 15:08:18 +02002419 /* first online clears initial online feature flag */
2420 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 device = dasd_create_device(cdev);
2422 if (IS_ERR(device))
2423 return PTR_ERR(device);
2424
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002425 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002426 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002428 pr_warning("%s Setting the DASD online failed because "
2429 "of missing DIAG discipline\n",
2430 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 dasd_delete_device(device);
2432 return -ENODEV;
2433 }
2434 discipline = dasd_diag_discipline_pointer;
2435 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002436 if (!try_module_get(base_discipline->owner)) {
2437 dasd_delete_device(device);
2438 return -EINVAL;
2439 }
2440 if (!try_module_get(discipline->owner)) {
2441 module_put(base_discipline->owner);
2442 dasd_delete_device(device);
2443 return -EINVAL;
2444 }
2445 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 device->discipline = discipline;
2447
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002448 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 rc = discipline->check_device(device);
2450 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002451 pr_warning("%s Setting the DASD online with discipline %s "
2452 "failed with rc=%i\n",
2453 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002454 module_put(discipline->owner);
2455 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 dasd_delete_device(device);
2457 return rc;
2458 }
2459
2460 dasd_set_target_state(device, DASD_STATE_ONLINE);
2461 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002462 pr_warning("%s Setting the DASD online failed because of a "
2463 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 rc = -ENODEV;
2465 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002466 if (device->block)
2467 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 dasd_delete_device(device);
2469 } else
2470 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002471 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01002472
2473 wait_event(dasd_init_waitq, _wait_for_device(device));
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return rc;
2477}
2478
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002479int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002483 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 device = dasd_device_from_cdev(cdev);
2486 if (IS_ERR(device))
2487 return PTR_ERR(device);
2488 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2489 /* Already doing offline processing */
2490 dasd_put_device(device);
2491 return 0;
2492 }
2493 /*
2494 * We must make sure that this device is currently not in use.
2495 * The open_count is increased for every opener, that includes
2496 * the blkdev_get in dasd_scan_partitions. We are only interested
2497 * in the other openers.
2498 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002499 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002500 max_count = device->block->bdev ? 0 : -1;
2501 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002502 if (open_count > max_count) {
2503 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002504 pr_warning("%s: The DASD cannot be set offline "
2505 "with open count %i\n",
2506 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002507 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002508 pr_warning("%s: The DASD cannot be set offline "
2509 "while it is in use\n",
2510 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002511 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2512 dasd_put_device(device);
2513 return -EBUSY;
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 dasd_set_target_state(device, DASD_STATE_NEW);
2517 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002518 block = device->block;
2519 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002521 /*
2522 * life cycle of block is bound to device, so delete it after
2523 * device was safely removed
2524 */
2525 if (block)
2526 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return 0;
2528}
2529
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002530int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 struct dasd_device *device;
2533 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int ret;
2535
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002536 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (IS_ERR(device))
2538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 ret = 0;
2540 switch (event) {
2541 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002542 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002544 /* First of all call extended error reporting. */
2545 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (device->state < DASD_STATE_BASIC)
2548 break;
2549 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002550 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2551 if (cqr->status == DASD_CQR_IN_IO) {
2552 cqr->status = DASD_CQR_QUEUED;
2553 cqr->retries++;
2554 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002555 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002556 dasd_device_clear_timer(device);
2557 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 ret = 1;
2559 break;
2560 case CIO_OPER:
2561 /* FIXME: add a sanity check. */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002562 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002563 if (device->stopped & DASD_UNRESUMED_PM) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002564 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002565 dasd_restore_device(device);
2566 ret = 1;
2567 break;
2568 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002569 dasd_schedule_device_bh(device);
2570 if (device->block)
2571 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 ret = 1;
2573 break;
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 dasd_put_device(device);
2576 return ret;
2577}
2578
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002579int dasd_generic_pm_freeze(struct ccw_device *cdev)
2580{
2581 struct dasd_ccw_req *cqr, *n;
2582 int rc;
2583 struct list_head freeze_queue;
2584 struct dasd_device *device = dasd_device_from_cdev(cdev);
2585
2586 if (IS_ERR(device))
2587 return PTR_ERR(device);
2588 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002589 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002590 /* clear active requests */
2591 INIT_LIST_HEAD(&freeze_queue);
2592 spin_lock_irq(get_ccwdev_lock(cdev));
2593 rc = 0;
2594 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2595 /* Check status and move request to flush_queue */
2596 if (cqr->status == DASD_CQR_IN_IO) {
2597 rc = device->discipline->term_IO(cqr);
2598 if (rc) {
2599 /* unable to terminate requeust */
2600 dev_err(&device->cdev->dev,
2601 "Unable to terminate request %p "
2602 "on suspend\n", cqr);
2603 spin_unlock_irq(get_ccwdev_lock(cdev));
2604 dasd_put_device(device);
2605 return rc;
2606 }
2607 }
2608 list_move_tail(&cqr->devlist, &freeze_queue);
2609 }
2610
2611 spin_unlock_irq(get_ccwdev_lock(cdev));
2612
2613 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2614 wait_event(dasd_flush_wq,
2615 (cqr->status != DASD_CQR_CLEAR_PENDING));
2616 if (cqr->status == DASD_CQR_CLEARED)
2617 cqr->status = DASD_CQR_QUEUED;
2618 }
2619 /* move freeze_queue to start of the ccw_queue */
2620 spin_lock_irq(get_ccwdev_lock(cdev));
2621 list_splice_tail(&freeze_queue, &device->ccw_queue);
2622 spin_unlock_irq(get_ccwdev_lock(cdev));
2623
2624 if (device->discipline->freeze)
2625 rc = device->discipline->freeze(device);
2626
2627 dasd_put_device(device);
2628 return rc;
2629}
2630EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2631
2632int dasd_generic_restore_device(struct ccw_device *cdev)
2633{
2634 struct dasd_device *device = dasd_device_from_cdev(cdev);
2635 int rc = 0;
2636
2637 if (IS_ERR(device))
2638 return PTR_ERR(device);
2639
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002640 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002641 dasd_device_remove_stop_bits(device,
2642 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002643
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002644 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002645
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002646 /*
2647 * call discipline restore function
2648 * if device is stopped do nothing e.g. for disconnected devices
2649 */
2650 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002651 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002652 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002653 /*
2654 * if the resume failed for the DASD we put it in
2655 * an UNRESUMED stop state
2656 */
2657 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002658
Stefan Haberland6fca97a2009-10-06 10:34:15 +02002659 if (device->block)
2660 dasd_schedule_block_bh(device->block);
2661
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002662 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002663 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002664}
2665EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2666
Heiko Carstens763968e2007-05-10 15:45:46 +02002667static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2668 void *rdc_buffer,
2669 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02002670 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002671{
2672 struct dasd_ccw_req *cqr;
2673 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002674 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02002675
2676 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2677
2678 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002679 /* internal error 13 - Allocating the RDC request failed*/
2680 dev_err(&device->cdev->dev,
2681 "An error occurred in the DASD device driver, "
2682 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002683 return cqr;
2684 }
2685
2686 ccw = cqr->cpaddr;
2687 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002688 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2689 idaw = (unsigned long *) (cqr->data);
2690 ccw->cda = (__u32)(addr_t) idaw;
2691 ccw->flags = CCW_FLAG_IDA;
2692 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2693 } else {
2694 ccw->cda = (__u32)(addr_t) rdc_buffer;
2695 ccw->flags = 0;
2696 }
Cornelia Huck17283b52007-05-04 18:47:51 +02002697
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002698 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002699 cqr->startdev = device;
2700 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002701 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002702 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02002703 cqr->buildclk = get_clock();
2704 cqr->status = DASD_CQR_FILLED;
2705 return cqr;
2706}
2707
2708
Stefan Haberland68b781f2009-09-11 10:28:29 +02002709int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002710 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002711{
2712 int ret;
2713 struct dasd_ccw_req *cqr;
2714
Sebastian Ott92636b12009-06-12 10:26:37 +02002715 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002716 magic);
2717 if (IS_ERR(cqr))
2718 return PTR_ERR(cqr);
2719
2720 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002721 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002722 return ret;
2723}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002724EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002725
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002726/*
2727 * In command mode and transport mode we need to look for sense
2728 * data in different places. The sense data itself is allways
2729 * an array of 32 bytes, so we can unify the sense data access
2730 * for both modes.
2731 */
2732char *dasd_get_sense(struct irb *irb)
2733{
2734 struct tsb *tsb = NULL;
2735 char *sense = NULL;
2736
2737 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2738 if (irb->scsw.tm.tcw)
2739 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2740 irb->scsw.tm.tcw);
2741 if (tsb && tsb->length == 64 && tsb->flags)
2742 switch (tsb->flags & 0x07) {
2743 case 1: /* tsa_iostat */
2744 sense = tsb->tsa.iostat.sense;
2745 break;
2746 case 2: /* tsa_ddpc */
2747 sense = tsb->tsa.ddpc.sense;
2748 break;
2749 default:
2750 /* currently we don't use interrogate data */
2751 break;
2752 }
2753 } else if (irb->esw.esw0.erw.cons) {
2754 sense = irb->ecw;
2755 }
2756 return sense;
2757}
2758EXPORT_SYMBOL_GPL(dasd_get_sense);
2759
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002760static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 int rc;
2763
2764 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002765 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002766 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002769 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (dasd_debug_area == NULL) {
2771 rc = -ENOMEM;
2772 goto failed;
2773 }
2774 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002775 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2778
2779 dasd_diag_discipline_pointer = NULL;
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 rc = dasd_devmap_init();
2782 if (rc)
2783 goto failed;
2784 rc = dasd_gendisk_init();
2785 if (rc)
2786 goto failed;
2787 rc = dasd_parse();
2788 if (rc)
2789 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002790 rc = dasd_eer_init();
2791 if (rc)
2792 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793#ifdef CONFIG_PROC_FS
2794 rc = dasd_proc_init();
2795 if (rc)
2796 goto failed;
2797#endif
2798
2799 return 0;
2800failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002801 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 dasd_exit();
2803 return rc;
2804}
2805
2806module_init(dasd_init);
2807module_exit(dasd_exit);
2808
2809EXPORT_SYMBOL(dasd_debug_area);
2810EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2811
2812EXPORT_SYMBOL(dasd_add_request_head);
2813EXPORT_SYMBOL(dasd_add_request_tail);
2814EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002815EXPORT_SYMBOL(dasd_device_clear_timer);
2816EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817EXPORT_SYMBOL(dasd_enable_device);
2818EXPORT_SYMBOL(dasd_int_handler);
2819EXPORT_SYMBOL(dasd_kfree_request);
2820EXPORT_SYMBOL(dasd_kick_device);
2821EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002822EXPORT_SYMBOL(dasd_schedule_device_bh);
2823EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002825EXPORT_SYMBOL(dasd_device_set_timer);
2826EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827EXPORT_SYMBOL(dasd_sfree_request);
2828EXPORT_SYMBOL(dasd_sleep_on);
2829EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2830EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2831EXPORT_SYMBOL(dasd_smalloc_request);
2832EXPORT_SYMBOL(dasd_start_IO);
2833EXPORT_SYMBOL(dasd_term_IO);
2834
2835EXPORT_SYMBOL_GPL(dasd_generic_probe);
2836EXPORT_SYMBOL_GPL(dasd_generic_remove);
2837EXPORT_SYMBOL_GPL(dasd_generic_notify);
2838EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2839EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002840EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2841EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2842EXPORT_SYMBOL_GPL(dasd_alloc_block);
2843EXPORT_SYMBOL_GPL(dasd_free_block);