blob: 8373ca0de8e0b00484f40d48c5813e600da920c2 [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>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020024#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010029#include <asm/itcw.h>
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010030#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +020041#define DASD_SLEEPON_START_TAG (void *) 1
42#define DASD_SLEEPON_END_TAG (void *) 2
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * SECTION: exported variables of dasd.c
46 */
47debug_info_t *dasd_debug_area;
48struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010049void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
52MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
53 " Copyright 2000 IBM Corporation");
54MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_LICENSE("GPL");
56
57/*
58 * SECTION: prototypes for static functions of dasd.c
59 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010060static int dasd_alloc_queue(struct dasd_block *);
61static void dasd_setup_queue(struct dasd_block *);
62static void dasd_free_queue(struct dasd_block *);
63static void dasd_flush_request_queue(struct dasd_block *);
64static int dasd_flush_block_queue(struct dasd_block *);
65static void dasd_device_tasklet(struct dasd_device *);
66static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000067static void do_kick_device(struct work_struct *);
Stefan Haberlandd41dd122009-06-16 10:30:25 +020068static void do_restore_device(struct work_struct *);
Stefan Haberland501183f2010-05-17 10:00:10 +020069static void do_reload_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010070static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010071static void dasd_device_timeout(unsigned long);
72static void dasd_block_timeout(unsigned long);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +010073static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * SECTION: Operations on the device structure.
77 */
78static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020079static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020080static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * Allocate memory for a new device structure.
84 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010085struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct dasd_device *device;
88
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010089 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
90 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* Get two pages for normal block device operations. */
94 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010095 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kfree(device);
97 return ERR_PTR(-ENOMEM);
98 }
99 /* Get one page for error recovery. */
100 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100101 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 free_pages((unsigned long) device->ccw_mem, 1);
103 kfree(device);
104 return ERR_PTR(-ENOMEM);
105 }
106
107 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
108 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
109 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100110 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200111 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100112 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 (unsigned long) device);
114 INIT_LIST_HEAD(&device->ccw_queue);
115 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100116 device->timer.function = dasd_device_timeout;
117 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000118 INIT_WORK(&device->kick_work, do_kick_device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200119 INIT_WORK(&device->restore_device, do_restore_device);
Stefan Haberland501183f2010-05-17 10:00:10 +0200120 INIT_WORK(&device->reload_device, do_reload_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 device->state = DASD_STATE_NEW;
122 device->target = DASD_STATE_NEW;
Stefan Haberland9eb25122010-02-26 22:37:46 +0100123 mutex_init(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return device;
126}
127
128/*
129 * Free memory of a device structure.
130 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100131void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800133 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 free_page((unsigned long) device->erp_mem);
135 free_pages((unsigned long) device->ccw_mem, 1);
136 kfree(device);
137}
138
139/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100140 * Allocate memory for a new device structure.
141 */
142struct dasd_block *dasd_alloc_block(void)
143{
144 struct dasd_block *block;
145
146 block = kzalloc(sizeof(*block), GFP_ATOMIC);
147 if (!block)
148 return ERR_PTR(-ENOMEM);
149 /* open_count = 0 means device online but not in use */
150 atomic_set(&block->open_count, -1);
151
152 spin_lock_init(&block->request_queue_lock);
153 atomic_set(&block->tasklet_scheduled, 0);
154 tasklet_init(&block->tasklet,
155 (void (*)(unsigned long)) dasd_block_tasklet,
156 (unsigned long) block);
157 INIT_LIST_HEAD(&block->ccw_queue);
158 spin_lock_init(&block->queue_lock);
159 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100160 block->timer.function = dasd_block_timeout;
161 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100162
163 return block;
164}
165
166/*
167 * Free memory of a device structure.
168 */
169void dasd_free_block(struct dasd_block *block)
170{
171 kfree(block);
172}
173
174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Make a new device known to the system.
176 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100177static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int rc;
180
181 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200182 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * keep the reference count > 0.
184 */
185 dasd_get_device(device);
186
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187 if (device->block) {
188 rc = dasd_alloc_queue(device->block);
189 if (rc) {
190 dasd_put_device(device);
191 return rc;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 device->state = DASD_STATE_KNOWN;
195 return 0;
196}
197
198/*
199 * Let the system forget about a device.
200 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100201static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800203 /* Disable extended error reporting for this device. */
204 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206 if (device->discipline) {
207 if (device->discipline->uncheck_device)
208 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800209 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800212 if (device->base_discipline)
213 module_put(device->base_discipline->owner);
214 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 device->state = DASD_STATE_NEW;
216
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 if (device->block)
218 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Give up reference we took in dasd_state_new_to_known. */
221 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
226 * Request the irq line for the device.
227 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100228static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int rc;
231
232 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100233 if (device->block) {
234 rc = dasd_gendisk_alloc(device->block);
235 if (rc)
236 return rc;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100239 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100240 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200242 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
244
245 device->state = DASD_STATE_BASIC;
246 return 0;
247}
248
249/*
250 * Release the irq line for the device. Terminate any running i/o.
251 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100252static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Horst Hummel8f617012006-08-30 14:33:33 +0200254 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100255 if (device->block) {
256 dasd_gendisk_free(device->block);
257 dasd_block_clear_timer(device->block);
258 }
259 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200260 if (rc)
261 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100262 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
265 if (device->debug_area != NULL) {
266 debug_unregister(device->debug_area);
267 device->debug_area = NULL;
268 }
269 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Do the initial analysis. The do_analysis function may return
275 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
276 * until the discipline decides to continue the startup sequence
277 * by calling the function dasd_change_state. The eckd disciplines
278 * uses this to start a ccw that detects the format. The completion
279 * interrupt for this detection ccw uses the kernel event daemon to
280 * trigger the call to dasd_change_state. All this is done in the
281 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800282 * After the analysis ccw is done (do_analysis returned 0) the block
283 * device is setup.
284 * In case the analysis returns an error, the device setup is stopped
285 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100287static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100290 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100293 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800294 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100295 if (block) {
296 if (block->base->discipline->do_analysis != NULL)
297 rc = block->base->discipline->do_analysis(block);
298 if (rc) {
299 if (rc != -EAGAIN)
300 device->state = DASD_STATE_UNFMT;
301 return rc;
302 }
303 dasd_setup_queue(block);
304 set_capacity(block->gdp,
305 block->blocks << block->s2b_shift);
306 device->state = DASD_STATE_READY;
307 rc = dasd_scan_partitions(block);
308 if (rc)
309 device->state = DASD_STATE_BASIC;
310 } else {
311 device->state = DASD_STATE_READY;
312 }
Horst Hummel90f00942006-03-07 21:55:39 -0800313 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/*
317 * Remove device from block device layer. Destroy dirty buffers.
318 * Forget format information. Check if the target level is basic
319 * and if it is create fake disk for formatting.
320 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100321static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Horst Hummel8f617012006-08-30 14:33:33 +0200323 int rc;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100326 if (device->block) {
327 struct dasd_block *block = device->block;
328 rc = dasd_flush_block_queue(block);
329 if (rc) {
330 device->state = DASD_STATE_READY;
331 return rc;
332 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100333 dasd_flush_request_queue(block);
Stefan Haberlandb695adf2010-02-26 22:37:48 +0100334 dasd_destroy_partitions(block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100335 block->blocks = 0;
336 block->bp_block = 0;
337 block->s2b_shift = 0;
338 }
Horst Hummel8f617012006-08-30 14:33:33 +0200339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/*
Horst Hummel90f00942006-03-07 21:55:39 -0800343 * Back to basic.
344 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800346{
347 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200348 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800349}
350
351/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * Make the device online and schedule the bottom half to start
353 * the requeueing of requests from the linux request queue to the
354 * ccw queue.
355 */
Horst Hummel8f617012006-08-30 14:33:33 +0200356static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357dasd_state_ready_to_online(struct dasd_device * device)
358{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100359 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100360 struct gendisk *disk;
361 struct disk_part_iter piter;
362 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100363
364 if (device->discipline->ready_to_online) {
365 rc = device->discipline->ready_to_online(device);
366 if (rc)
367 return rc;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100370 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100371 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100372 disk = device->block->bdev->bd_disk;
373 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
374 while ((part = disk_part_iter_next(&piter)))
375 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
376 disk_part_iter_exit(&piter);
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
381/*
382 * Stop the requeueing of requests again.
383 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100384static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100386 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100387 struct gendisk *disk;
388 struct disk_part_iter piter;
389 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100390
391 if (device->discipline->online_to_ready) {
392 rc = device->discipline->online_to_ready(device);
393 if (rc)
394 return rc;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100397 if (device->block) {
398 disk = device->block->bdev->bd_disk;
399 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
400 while ((part = disk_part_iter_next(&piter)))
401 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
402 disk_part_iter_exit(&piter);
403 }
Horst Hummel8f617012006-08-30 14:33:33 +0200404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/*
408 * Device startup state changes.
409 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100410static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int rc;
413
414 rc = 0;
415 if (device->state == DASD_STATE_NEW &&
416 device->target >= DASD_STATE_KNOWN)
417 rc = dasd_state_new_to_known(device);
418
419 if (!rc &&
420 device->state == DASD_STATE_KNOWN &&
421 device->target >= DASD_STATE_BASIC)
422 rc = dasd_state_known_to_basic(device);
423
424 if (!rc &&
425 device->state == DASD_STATE_BASIC &&
426 device->target >= DASD_STATE_READY)
427 rc = dasd_state_basic_to_ready(device);
428
429 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700430 device->state == DASD_STATE_UNFMT &&
431 device->target > DASD_STATE_UNFMT)
432 rc = -EPERM;
433
434 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 device->state == DASD_STATE_READY &&
436 device->target >= DASD_STATE_ONLINE)
437 rc = dasd_state_ready_to_online(device);
438
439 return rc;
440}
441
442/*
443 * Device shutdown state changes.
444 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100445static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Horst Hummel8f617012006-08-30 14:33:33 +0200447 int rc;
448
449 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (device->state == DASD_STATE_ONLINE &&
451 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200452 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200453
Horst Hummel8f617012006-08-30 14:33:33 +0200454 if (!rc &&
455 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200457 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800458
Horst Hummel8f617012006-08-30 14:33:33 +0200459 if (!rc &&
460 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800461 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200462 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800463
Horst Hummel8f617012006-08-30 14:33:33 +0200464 if (!rc &&
465 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200467 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200468
Horst Hummel8f617012006-08-30 14:33:33 +0200469 if (!rc &&
470 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200472 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Horst Hummel8f617012006-08-30 14:33:33 +0200474 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * This is the main startup/shutdown routine.
479 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100480static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Sebastian Ott181d9522009-06-22 12:08:21 +0200482 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (device->state == device->target)
485 /* Already where we want to go today... */
486 return;
487 if (device->state < device->target)
488 rc = dasd_increase_state(device);
489 else
490 rc = dasd_decrease_state(device);
Sebastian Ott181d9522009-06-22 12:08:21 +0200491 if (rc == -EAGAIN)
492 return;
493 if (rc)
494 device->target = device->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Stefan Haberland9eb25122010-02-26 22:37:46 +0100496 if (device->state == device->target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200498
499 /* let user-space know that the device status changed */
500 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
504 * Kick starter for devices that did not complete the startup/shutdown
505 * procedure or were sleeping because of a pending state.
506 * dasd_kick_device will schedule a call do do_kick_device to the kernel
507 * event daemon.
508 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100509static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Al Viro4927b3f2006-12-06 19:18:20 +0000511 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100512 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100514 mutex_unlock(&device->state_mutex);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100515 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 dasd_put_device(device);
517}
518
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100519void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 dasd_get_device(device);
522 /* queue call to dasd_kick_device to the kernel event daemon. */
523 schedule_work(&device->kick_work);
524}
525
526/*
Stefan Haberland501183f2010-05-17 10:00:10 +0200527 * dasd_reload_device will schedule a call do do_reload_device to the kernel
528 * event daemon.
529 */
530static void do_reload_device(struct work_struct *work)
531{
532 struct dasd_device *device = container_of(work, struct dasd_device,
533 reload_device);
534 device->discipline->reload(device);
535 dasd_put_device(device);
536}
537
538void dasd_reload_device(struct dasd_device *device)
539{
540 dasd_get_device(device);
541 /* queue call to dasd_reload_device to the kernel event daemon. */
542 schedule_work(&device->reload_device);
543}
544EXPORT_SYMBOL(dasd_reload_device);
545
546/*
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200547 * dasd_restore_device will schedule a call do do_restore_device to the kernel
548 * event daemon.
549 */
550static void do_restore_device(struct work_struct *work)
551{
552 struct dasd_device *device = container_of(work, struct dasd_device,
553 restore_device);
554 device->cdev->drv->restore(device->cdev);
555 dasd_put_device(device);
556}
557
558void dasd_restore_device(struct dasd_device *device)
559{
560 dasd_get_device(device);
561 /* queue call to dasd_restore_device to the kernel event daemon. */
562 schedule_work(&device->restore_device);
563}
564
565/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * Set the target state for a device and starts the state change.
567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Cornelia Huckf3445a12009-04-14 15:36:23 +0200570 dasd_get_device(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100571 mutex_lock(&device->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* If we are in probeonly mode stop at DASD_STATE_READY. */
573 if (dasd_probeonly && target > DASD_STATE_READY)
574 target = DASD_STATE_READY;
575 if (device->target != target) {
Stefan Haberland9eb25122010-02-26 22:37:46 +0100576 if (device->state == target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 wake_up(&dasd_init_waitq);
578 device->target = target;
579 }
580 if (device->state != device->target)
581 dasd_change_state(device);
Stefan Haberland9eb25122010-02-26 22:37:46 +0100582 mutex_unlock(&device->state_mutex);
583 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586/*
587 * Enable devices with device numbers in [from..to].
588 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100589static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 return (device->state == device->target);
592}
593
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100594void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 dasd_set_target_state(device, DASD_STATE_ONLINE);
597 if (device->state <= DASD_STATE_KNOWN)
598 /* No discipline for device found. */
599 dasd_set_target_state(device, DASD_STATE_NEW);
600 /* Now wait for the devices to come up. */
601 wait_event(dasd_init_waitq, _wait_for_device(device));
602}
603
604/*
605 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
606 */
607#ifdef CONFIG_DASD_PROFILE
608
609struct dasd_profile_info_t dasd_global_profile;
610unsigned int dasd_profile_level = DASD_PROFILE_OFF;
611
612/*
613 * Increments counter in global and local profiling structures.
614 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100615#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{ \
617 int index; \
618 for (index = 0; index < 31 && value >> (2+index); index++); \
619 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100620 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
624 * Add profiling information for cqr before execution.
625 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100626static void dasd_profile_start(struct dasd_block *block,
627 struct dasd_ccw_req *cqr,
628 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct list_head *l;
631 unsigned int counter;
632
633 if (dasd_profile_level != DASD_PROFILE_ON)
634 return;
635
636 /* count the length of the chanq for statistics */
637 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100638 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (++counter >= 31)
640 break;
641 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100642 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
646 * Add profiling information for cqr after execution.
647 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100648static void dasd_profile_end(struct dasd_block *block,
649 struct dasd_ccw_req *cqr,
650 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 long strtime, irqtime, endtime, tottime; /* in microseconds */
653 long tottimeps, sectors;
654
655 if (dasd_profile_level != DASD_PROFILE_ON)
656 return;
657
Tejun Heo83096eb2009-05-07 22:24:39 +0900658 sectors = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!cqr->buildclk || !cqr->startclk ||
660 !cqr->stopclk || !cqr->endclk ||
661 !sectors)
662 return;
663
664 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
665 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
666 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
667 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
668 tottimeps = tottime / sectors;
669
670 if (!dasd_global_profile.dasd_io_reqs)
671 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100672 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 dasd_global_profile.dasd_io_reqs++;
674 dasd_global_profile.dasd_io_sects += sectors;
675
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100676 if (!block->profile.dasd_io_reqs)
677 memset(&block->profile, 0,
678 sizeof(struct dasd_profile_info_t));
679 block->profile.dasd_io_reqs++;
680 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100682 dasd_profile_counter(sectors, dasd_io_secs, block);
683 dasd_profile_counter(tottime, dasd_io_times, block);
684 dasd_profile_counter(tottimeps, dasd_io_timps, block);
685 dasd_profile_counter(strtime, dasd_io_time1, block);
686 dasd_profile_counter(irqtime, dasd_io_time2, block);
687 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
688 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100691#define dasd_profile_start(block, cqr, req) do {} while (0)
692#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#endif /* CONFIG_DASD_PROFILE */
694
695/*
696 * Allocate memory for a channel program with 'cplength' channel
697 * command words and 'datasize' additional space. There are two
698 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
699 * memory and 2) dasd_smalloc_request uses the static ccw memory
700 * that gets allocated for each device.
701 */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200702struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100703 int datasize,
704 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct dasd_ccw_req *cqr;
707
708 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200709 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100710 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800712 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (cqr == NULL)
714 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cqr->cpaddr = NULL;
716 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800717 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 GFP_ATOMIC | GFP_DMA);
719 if (cqr->cpaddr == NULL) {
720 kfree(cqr);
721 return ERR_PTR(-ENOMEM);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 cqr->data = NULL;
725 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800726 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800728 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(cqr);
730 return ERR_PTR(-ENOMEM);
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200733 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
735 dasd_get_device(device);
736 return cqr;
737}
738
Stefan Haberland68b781f2009-09-11 10:28:29 +0200739struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100740 int datasize,
741 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 unsigned long flags;
744 struct dasd_ccw_req *cqr;
745 char *data;
746 int size;
747
748 /* Sanity checks */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200749 BUG_ON(datasize > PAGE_SIZE ||
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100750 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
753 if (cplength > 0)
754 size += cplength * sizeof(struct ccw1);
755 if (datasize > 0)
756 size += datasize;
757 spin_lock_irqsave(&device->mem_lock, flags);
758 cqr = (struct dasd_ccw_req *)
759 dasd_alloc_chunk(&device->ccw_chunks, size);
760 spin_unlock_irqrestore(&device->mem_lock, flags);
761 if (cqr == NULL)
762 return ERR_PTR(-ENOMEM);
763 memset(cqr, 0, sizeof(struct dasd_ccw_req));
764 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
765 cqr->cpaddr = NULL;
766 if (cplength > 0) {
767 cqr->cpaddr = (struct ccw1 *) data;
768 data += cplength*sizeof(struct ccw1);
769 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
770 }
771 cqr->data = NULL;
772 if (datasize > 0) {
773 cqr->data = data;
774 memset(cqr->data, 0, datasize);
775 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200776 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
778 dasd_get_device(device);
779 return cqr;
780}
781
782/*
783 * Free memory of a channel program. This function needs to free all the
784 * idal lists that might have been created by dasd_set_cda and the
785 * struct dasd_ccw_req itself.
786 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100787void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800789#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct ccw1 *ccw;
791
792 /* Clear any idals used for the request. */
793 ccw = cqr->cpaddr;
794 do {
795 clear_normalized_cda(ccw);
796 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
797#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800798 kfree(cqr->cpaddr);
799 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kfree(cqr);
801 dasd_put_device(device);
802}
803
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100804void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned long flags;
807
808 spin_lock_irqsave(&device->mem_lock, flags);
809 dasd_free_chunk(&device->ccw_chunks, cqr);
810 spin_unlock_irqrestore(&device->mem_lock, flags);
811 dasd_put_device(device);
812}
813
814/*
815 * Check discipline magic in cqr.
816 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100817static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct dasd_device *device;
820
821 if (cqr == NULL)
822 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100823 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100825 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 " dasd_ccw_req 0x%08x magic doesn't match"
827 " discipline 0x%08x",
828 cqr->magic,
829 *(unsigned int *) device->discipline->name);
830 return -EINVAL;
831 }
832 return 0;
833}
834
835/*
836 * Terminate the current i/o and set the request to clear_pending.
837 * Timer keeps device runnig.
838 * ccw_device_clear can fail if the i/o subsystem
839 * is in a bad mood.
840 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100841int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct dasd_device *device;
844 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100845 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* Check the cqr */
848 rc = dasd_check_cqr(cqr);
849 if (rc)
850 return rc;
851 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100852 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
854 rc = ccw_device_clear(device->cdev, (long) cqr);
855 switch (rc) {
856 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800857 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100858 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200860 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 DBF_DEV_EVENT(DBF_DEBUG, device,
862 "terminate cqr %p successful",
863 cqr);
864 break;
865 case -ENODEV:
866 DBF_DEV_EVENT(DBF_ERR, device, "%s",
867 "device gone, retry");
868 break;
869 case -EIO:
870 DBF_DEV_EVENT(DBF_ERR, device, "%s",
871 "I/O error, retry");
872 break;
873 case -EINVAL:
874 case -EBUSY:
875 DBF_DEV_EVENT(DBF_ERR, device, "%s",
876 "device busy, retry later");
877 break;
878 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100879 /* internal error 10 - unknown rc*/
880 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
881 dev_err(&device->cdev->dev, "An error occurred in the "
882 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 BUG();
884 break;
885 }
886 retries++;
887 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100888 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return rc;
890}
891
892/*
893 * Start the i/o. This start_IO can fail if the channel is really busy.
894 * In that case set up a timer to start the request later.
895 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100896int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct dasd_device *device;
899 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100900 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Check the cqr */
903 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200904 if (rc) {
905 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200907 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100908 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100910 /* internal error 14 - start_IO run out of retries */
911 sprintf(errorstring, "14 %p", cqr);
912 dev_err(&device->cdev->dev, "An error occurred in the DASD "
913 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100914 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EIO;
916 }
917 cqr->startclk = get_clock();
918 cqr->starttime = jiffies;
919 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100920 if (cqr->cpmode == 1) {
921 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
922 (long) cqr, cqr->lpm);
923 } else {
924 rc = ccw_device_start(device->cdev, cqr->cpaddr,
925 (long) cqr, cqr->lpm, 0);
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 switch (rc) {
928 case 0:
929 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
931 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100932 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 "start_IO: device busy, retry later");
934 break;
935 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100936 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 "start_IO: request timeout, retry later");
938 break;
939 case -EACCES:
940 /* -EACCES indicates that the request used only a
941 * subset of the available pathes and all these
942 * pathes are gone.
943 * Do a retry with all available pathes.
944 */
945 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100946 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 "start_IO: selected pathes gone,"
948 " retry on all pathes");
949 break;
950 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100951 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
952 "start_IO: -ENODEV device gone, retry");
953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100955 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100956 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200958 case -EINVAL:
959 /* most likely caused in power management context */
960 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
961 "start_IO: -EINVAL device currently "
962 "not accessible");
963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100965 /* internal error 11 - unknown rc */
966 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
967 dev_err(&device->cdev->dev,
968 "An error occurred in the DASD device driver, "
969 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 BUG();
971 break;
972 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200973 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return rc;
975}
976
977/*
978 * Timeout function for dasd devices. This is used for different purposes
979 * 1) missing interrupt handler for normal operation
980 * 2) delayed start of request where start_IO failed with -EBUSY
981 * 3) timeout for missing state change interrupts
982 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
983 * DASD_CQR_QUEUED for 2) and 3).
984 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100985static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 unsigned long flags;
988 struct dasd_device *device;
989
990 device = (struct dasd_device *) ptr;
991 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
992 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100993 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100995 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998/*
999 * Setup timeout for a device in jiffies.
1000 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001001void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001003 if (expires == 0)
1004 del_timer(&device->timer);
1005 else
1006 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*
1010 * Clear timeout for a device.
1011 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001012void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001014 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001017static void dasd_handle_killed_request(struct ccw_device *cdev,
1018 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct dasd_ccw_req *cqr;
1021 struct dasd_device *device;
1022
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001023 if (!intparm)
1024 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 cqr = (struct dasd_ccw_req *) intparm;
1026 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001027 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1028 "invalid status in handle_killed_request: "
1029 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return;
1031 }
1032
Stefan Haberland589c74d2010-02-26 22:37:47 +01001033 device = dasd_device_from_cdev_locked(cdev);
1034 if (IS_ERR(device)) {
1035 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1036 "unable to get device from cdev");
1037 return;
1038 }
1039
1040 if (!cqr->startdev ||
1041 device != cqr->startdev ||
1042 strncmp(cqr->startdev->discipline->ebcname,
1043 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001044 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1045 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001046 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return;
1048 }
1049
1050 /* Schedule request to be retried. */
1051 cqr->status = DASD_CQR_QUEUED;
1052
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001053 dasd_device_clear_timer(device);
1054 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dasd_put_device(device);
1056}
1057
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001058void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001060 /* First of all start sense subsystem status request. */
1061 dasd_eer_snss(device);
1062
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001063 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001064 dasd_schedule_device_bh(device);
1065 if (device->block)
1066 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
1070 * Interrupt handler for "normal" ssch-io based dasd devices.
1071 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001072void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1073 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct dasd_ccw_req *cqr, *next;
1076 struct dasd_device *device;
1077 unsigned long long now;
1078 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (IS_ERR(irb)) {
1081 switch (PTR_ERR(irb)) {
1082 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001085 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1086 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
1088 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001089 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1090 "unknown error %ld\n", __func__,
1091 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001093 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return;
1095 }
1096
1097 now = get_clock();
1098
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001099 /* check for unsolicited interrupts */
1100 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001101 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1102 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1103 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001104 if (cqr && cqr->status == DASD_CQR_IN_IO)
1105 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001106 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001108 dasd_device_clear_timer(device);
1109 device->discipline->handle_unsolicited_interrupt(device,
1110 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dasd_put_device(device);
1112 }
1113 return;
1114 }
1115
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001116 device = (struct dasd_device *) cqr->startdev;
1117 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001119 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1120 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return;
1122 }
1123
1124 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001125 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001126 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001127 cqr->status = DASD_CQR_CLEARED;
1128 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001129 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001130 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return;
1132 }
1133
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001134 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001136 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1137 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return;
1139 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001140
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001141 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001143 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1144 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001145 /* request was completed successfully */
1146 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 cqr->stopclk = now;
1148 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001149 if (cqr->devlist.next != &device->ccw_queue) {
1150 next = list_entry(cqr->devlist.next,
1151 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001153 } else { /* error */
1154 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001155 /* log sense for every failed I/O to s390 debugfeature */
1156 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001157 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001158 dasd_log_sense(cqr, irb);
1159 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001160
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001161 /*
1162 * If we don't want complex ERP for this request, then just
1163 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001164 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001165 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001166 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001167 if (cqr->lpm == LPM_ANYPATH)
1168 DBF_DEV_EVENT(DBF_DEBUG, device,
1169 "default ERP in fastpath "
1170 "(%i retries left)",
1171 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001172 cqr->lpm = LPM_ANYPATH;
1173 cqr->status = DASD_CQR_QUEUED;
1174 next = cqr;
1175 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001177 }
1178 if (next && (next->status == DASD_CQR_QUEUED) &&
1179 (!device->stopped)) {
1180 if (device->discipline->start_IO(next) == 0)
1181 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001184 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001186 dasd_device_clear_timer(device);
1187 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001190enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1191{
1192 struct dasd_device *device;
1193
1194 device = dasd_device_from_cdev_locked(cdev);
1195
1196 if (IS_ERR(device))
1197 goto out;
1198 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1199 device->state != device->target ||
1200 !device->discipline->handle_unsolicited_interrupt){
1201 dasd_put_device(device);
1202 goto out;
1203 }
1204
1205 dasd_device_clear_timer(device);
1206 device->discipline->handle_unsolicited_interrupt(device, irb);
1207 dasd_put_device(device);
1208out:
1209 return UC_TODO_RETRY;
1210}
1211EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001214 * If we have an error on a dasd_block layer request then we cancel
1215 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001217static void __dasd_device_recovery(struct dasd_device *device,
1218 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct list_head *l, *n;
1221 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001224 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001226 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001228
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229 list_for_each_safe(l, n, &device->ccw_queue) {
1230 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1231 if (cqr->status == DASD_CQR_QUEUED &&
1232 ref_cqr->block == cqr->block) {
1233 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001235 }
1236};
1237
1238/*
1239 * Remove those ccw requests from the queue that need to be returned
1240 * to the upper layer.
1241 */
1242static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1243 struct list_head *final_queue)
1244{
1245 struct list_head *l, *n;
1246 struct dasd_ccw_req *cqr;
1247
1248 /* Process request with final status. */
1249 list_for_each_safe(l, n, &device->ccw_queue) {
1250 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1251
1252 /* Stop list processing at the first non-final request. */
1253 if (cqr->status == DASD_CQR_QUEUED ||
1254 cqr->status == DASD_CQR_IN_IO ||
1255 cqr->status == DASD_CQR_CLEAR_PENDING)
1256 break;
1257 if (cqr->status == DASD_CQR_ERROR) {
1258 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001260 /* Rechain finished requests to final queue */
1261 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263}
1264
1265/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001266 * the cqrs from the final queue are returned to the upper layer
1267 * by setting a dasd_block state and calling the callback function
1268 */
1269static void __dasd_device_process_final_queue(struct dasd_device *device,
1270 struct list_head *final_queue)
1271{
1272 struct list_head *l, *n;
1273 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001274 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001275 void (*callback)(struct dasd_ccw_req *, void *data);
1276 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001277 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001278
1279 list_for_each_safe(l, n, final_queue) {
1280 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1281 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001282 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001283 callback = cqr->callback;
1284 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001285 if (block)
1286 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001287 switch (cqr->status) {
1288 case DASD_CQR_SUCCESS:
1289 cqr->status = DASD_CQR_DONE;
1290 break;
1291 case DASD_CQR_ERROR:
1292 cqr->status = DASD_CQR_NEED_ERP;
1293 break;
1294 case DASD_CQR_CLEARED:
1295 cqr->status = DASD_CQR_TERMINATED;
1296 break;
1297 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001298 /* internal error 12 - wrong cqr status*/
1299 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1300 dev_err(&device->cdev->dev,
1301 "An error occurred in the DASD device driver, "
1302 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001303 BUG();
1304 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001305 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001306 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001307 if (block)
1308 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001309 }
1310}
1311
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * Take a look at the first request on the ccw queue and check
1314 * if it reached its expire time. If so, terminate the IO.
1315 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct dasd_ccw_req *cqr;
1319
1320 if (list_empty(&device->ccw_queue))
1321 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001322 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001323 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1324 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1325 if (device->discipline->term_IO(cqr) != 0) {
1326 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001327 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001328 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001329 "ended, retrying in 5 s\n",
1330 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001331 cqr->expires += 5*HZ;
1332 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001333 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001334 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001335 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001336 "remaining\n", cqr, (cqr->expires/HZ),
1337 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 }
1340}
1341
1342/*
1343 * Take a look at the first request on the ccw queue and check
1344 * if it needs to be started.
1345 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001346static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct dasd_ccw_req *cqr;
1349 int rc;
1350
1351 if (list_empty(&device->ccw_queue))
1352 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001353 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001354 if (cqr->status != DASD_CQR_QUEUED)
1355 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356 /* when device is stopped, return request to previous layer */
1357 if (device->stopped) {
1358 cqr->status = DASD_CQR_CLEARED;
1359 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001360 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001361 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001362
1363 rc = device->discipline->start_IO(cqr);
1364 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001365 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001366 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001367 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001368 } else
1369 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001370 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001374 * Go through all request on the dasd_device request queue,
1375 * terminate them on the cdev if necessary, and return them to the
1376 * submitting layer via callback.
1377 * Note:
1378 * Make sure that all 'submitting layers' still exist when
1379 * this function is called!. In other words, when 'device' is a base
1380 * device then all block layer requests must have been removed before
1381 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001383int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001385 struct dasd_ccw_req *cqr, *n;
1386 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 INIT_LIST_HEAD(&flush_queue);
1390 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001391 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001393 /* Check status and move request to flush_queue */
1394 switch (cqr->status) {
1395 case DASD_CQR_IN_IO:
1396 rc = device->discipline->term_IO(cqr);
1397 if (rc) {
1398 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001399 dev_err(&device->cdev->dev,
1400 "Flushing the DASD request queue "
1401 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001402 /* stop flush processing */
1403 goto finished;
1404 }
1405 break;
1406 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001408 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001409 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001410 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001411 break;
1412 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001413 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001414 }
Horst Hummel8f617012006-08-30 14:33:33 +02001415finished:
1416 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001417 /*
1418 * After this point all requests must be in state CLEAR_PENDING,
1419 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1420 * one of the others.
1421 */
1422 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1423 wait_event(dasd_flush_wq,
1424 (cqr->status != DASD_CQR_CLEAR_PENDING));
1425 /*
1426 * Now set each request back to TERMINATED, DONE or NEED_ERP
1427 * and call the callback function of flushed requests
1428 */
1429 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001430 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433/*
1434 * Acquire the device lock and process queues for the device.
1435 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 atomic_set (&device->tasklet_scheduled, 0);
1441 INIT_LIST_HEAD(&final_queue);
1442 spin_lock_irq(get_ccwdev_lock(device->cdev));
1443 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444 __dasd_device_check_expire(device);
1445 /* find final requests on ccw queue */
1446 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1448 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001449 __dasd_device_process_final_queue(device, &final_queue);
1450 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001452 __dasd_device_start_head(device);
1453 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 dasd_put_device(device);
1455}
1456
1457/*
1458 * Schedules a call to dasd_tasklet over the device tasklet.
1459 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001463 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465 dasd_get_device(device);
1466 tasklet_hi_schedule(&device->tasklet);
1467}
1468
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001469void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1470{
1471 device->stopped |= bits;
1472}
1473EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1474
1475void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1476{
1477 device->stopped &= ~bits;
1478 if (!device->stopped)
1479 wake_up(&generic_waitq);
1480}
1481EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001484 * Queue a request to the head of the device ccw_queue.
1485 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001487void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct dasd_device *device;
1490 unsigned long flags;
1491
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001492 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001494 cqr->status = DASD_CQR_QUEUED;
1495 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001497 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1499}
1500
1501/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001502 * Queue a request to the tail of the device ccw_queue.
1503 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001505void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct dasd_device *device;
1508 unsigned long flags;
1509
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001510 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001512 cqr->status = DASD_CQR_QUEUED;
1513 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001515 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1517}
1518
1519/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001520 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001522static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001524 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1525 cqr->callback_data = DASD_SLEEPON_END_TAG;
1526 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1527 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001530static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct dasd_device *device;
1533 int rc;
1534
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001535 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001537 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1539 return rc;
1540}
1541
1542/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001543 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1544 */
1545static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1546{
1547 struct dasd_device *device;
1548 dasd_erp_fn_t erp_fn;
1549
1550 if (cqr->status == DASD_CQR_FILLED)
1551 return 0;
1552 device = cqr->startdev;
1553 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1554 if (cqr->status == DASD_CQR_TERMINATED) {
1555 device->discipline->handle_terminated_request(cqr);
1556 return 1;
1557 }
1558 if (cqr->status == DASD_CQR_NEED_ERP) {
1559 erp_fn = device->discipline->erp_action(cqr);
1560 erp_fn(cqr);
1561 return 1;
1562 }
1563 if (cqr->status == DASD_CQR_FAILED)
1564 dasd_log_sense(cqr, &cqr->irb);
1565 if (cqr->refers) {
1566 __dasd_process_erp(device, cqr);
1567 return 1;
1568 }
1569 }
1570 return 0;
1571}
1572
1573static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1574{
1575 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1576 if (cqr->refers) /* erp is not done yet */
1577 return 1;
1578 return ((cqr->status != DASD_CQR_DONE) &&
1579 (cqr->status != DASD_CQR_FAILED));
1580 } else
1581 return (cqr->status == DASD_CQR_FILLED);
1582}
1583
1584static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1585{
1586 struct dasd_device *device;
1587 int rc;
1588 struct list_head ccw_queue;
1589 struct dasd_ccw_req *cqr;
1590
1591 INIT_LIST_HEAD(&ccw_queue);
1592 maincqr->status = DASD_CQR_FILLED;
1593 device = maincqr->startdev;
1594 list_add(&maincqr->blocklist, &ccw_queue);
1595 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1596 cqr = list_first_entry(&ccw_queue,
1597 struct dasd_ccw_req, blocklist)) {
1598
1599 if (__dasd_sleep_on_erp(cqr))
1600 continue;
1601 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1602 continue;
1603
1604 /* Non-temporary stop condition will trigger fail fast */
1605 if (device->stopped & ~DASD_STOPPED_PENDING &&
1606 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1607 (!dasd_eer_enabled(device))) {
1608 cqr->status = DASD_CQR_FAILED;
1609 continue;
1610 }
1611
1612 /* Don't try to start requests if device is stopped */
1613 if (interruptible) {
1614 rc = wait_event_interruptible(
1615 generic_waitq, !(device->stopped));
1616 if (rc == -ERESTARTSYS) {
1617 cqr->status = DASD_CQR_FAILED;
1618 maincqr->intrc = rc;
1619 continue;
1620 }
1621 } else
1622 wait_event(generic_waitq, !(device->stopped));
1623
1624 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001625 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001626 dasd_add_request_tail(cqr);
1627 if (interruptible) {
1628 rc = wait_event_interruptible(
1629 generic_waitq, _wait_for_wakeup(cqr));
1630 if (rc == -ERESTARTSYS) {
1631 dasd_cancel_req(cqr);
1632 /* wait (non-interruptible) for final status */
1633 wait_event(generic_waitq,
1634 _wait_for_wakeup(cqr));
1635 cqr->status = DASD_CQR_FAILED;
1636 maincqr->intrc = rc;
1637 continue;
1638 }
1639 } else
1640 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1641 }
1642
1643 maincqr->endclk = get_clock();
1644 if ((maincqr->status != DASD_CQR_DONE) &&
1645 (maincqr->intrc != -ERESTARTSYS))
1646 dasd_log_sense(maincqr, &maincqr->irb);
1647 if (maincqr->status == DASD_CQR_DONE)
1648 rc = 0;
1649 else if (maincqr->intrc)
1650 rc = maincqr->intrc;
1651 else
1652 rc = -EIO;
1653 return rc;
1654}
1655
1656/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001657 * Queue a request to the tail of the device ccw_queue and wait for
1658 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001660int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001662 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001666 * Queue a request to the tail of the device ccw_queue and wait
1667 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001669int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001671 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/*
1675 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1676 * for eckd devices) the currently running request has to be terminated
1677 * and be put back to status queued, before the special request is added
1678 * to the head of the queue. Then the special request is waited on normally.
1679 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (list_empty(&device->ccw_queue))
1685 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001686 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001687 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001690int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct dasd_device *device;
1693 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001694
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001695 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 spin_lock_irq(get_ccwdev_lock(device->cdev));
1697 rc = _dasd_term_running_cqr(device);
1698 if (rc) {
1699 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1700 return rc;
1701 }
Horst Hummel138c0142006-06-29 14:58:12 +02001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001704 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001709 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1712
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001713 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001714
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001715 if (cqr->status == DASD_CQR_DONE)
1716 rc = 0;
1717 else if (cqr->intrc)
1718 rc = cqr->intrc;
1719 else
1720 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return rc;
1722}
1723
1724/*
1725 * Cancels a request that was started with dasd_sleep_on_req.
1726 * This is useful to timeout requests. The request will be
1727 * terminated if it is currently in i/o.
1728 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001729 * 0 if there was no need to terminate the request (not started yet)
1730 * negative error code if termination failed
1731 * Cancellation of a request is an asynchronous operation! The calling
1732 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001734int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001736 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 unsigned long flags;
1738 int rc;
1739
1740 rc = 0;
1741 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1742 switch (cqr->status) {
1743 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001744 /* request was not started - just set to cleared */
1745 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 break;
1747 case DASD_CQR_IN_IO:
1748 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001749 rc = device->discipline->term_IO(cqr);
1750 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001751 dev_err(&device->cdev->dev,
1752 "Cancelling request %p failed with rc=%d\n",
1753 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001754 } else {
1755 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001758 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 dasd_schedule_device_bh(device);
1763 return rc;
1764}
1765
1766
1767/*
1768 * SECTION: Operations of the dasd_block layer.
1769 */
1770
1771/*
1772 * Timeout function for dasd_block. This is used when the block layer
1773 * is waiting for something that may not come reliably, (e.g. a state
1774 * change interrupt)
1775 */
1776static void dasd_block_timeout(unsigned long ptr)
1777{
1778 unsigned long flags;
1779 struct dasd_block *block;
1780
1781 block = (struct dasd_block *) ptr;
1782 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1783 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001784 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001785 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1786 dasd_schedule_block_bh(block);
1787}
1788
1789/*
1790 * Setup timeout for a dasd_block in jiffies.
1791 */
1792void dasd_block_set_timer(struct dasd_block *block, int expires)
1793{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001794 if (expires == 0)
1795 del_timer(&block->timer);
1796 else
1797 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001798}
1799
1800/*
1801 * Clear timeout for a dasd_block.
1802 */
1803void dasd_block_clear_timer(struct dasd_block *block)
1804{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001805 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001806}
1807
1808/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001809 * Process finished error recovery ccw.
1810 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001811static void __dasd_process_erp(struct dasd_device *device,
1812 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001813{
1814 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001815
1816 if (cqr->status == DASD_CQR_DONE)
1817 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1818 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001819 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001820 erp_fn = device->discipline->erp_postaction(cqr);
1821 erp_fn(cqr);
1822}
1823
1824/*
1825 * Fetch requests from the block device queue.
1826 */
1827static void __dasd_process_request_queue(struct dasd_block *block)
1828{
1829 struct request_queue *queue;
1830 struct request *req;
1831 struct dasd_ccw_req *cqr;
1832 struct dasd_device *basedev;
1833 unsigned long flags;
1834 queue = block->request_queue;
1835 basedev = block->base;
1836 /* No queue ? Then there is nothing to do. */
1837 if (queue == NULL)
1838 return;
1839
1840 /*
1841 * We requeue request from the block device queue to the ccw
1842 * queue only in two states. In state DASD_STATE_READY the
1843 * partition detection is done and we need to requeue requests
1844 * for that. State DASD_STATE_ONLINE is normal block device
1845 * operation.
1846 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001847 if (basedev->state < DASD_STATE_READY) {
1848 while ((req = blk_fetch_request(block->request_queue)))
1849 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001850 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001851 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001852 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001853 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001854 if (basedev->features & DASD_FEATURE_READONLY &&
1855 rq_data_dir(req) == WRITE) {
1856 DBF_DEV_EVENT(DBF_ERR, basedev,
1857 "Rejecting write request %p",
1858 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001859 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001860 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001861 continue;
1862 }
1863 cqr = basedev->discipline->build_cp(basedev, block, req);
1864 if (IS_ERR(cqr)) {
1865 if (PTR_ERR(cqr) == -EBUSY)
1866 break; /* normal end condition */
1867 if (PTR_ERR(cqr) == -ENOMEM)
1868 break; /* terminate request queue loop */
1869 if (PTR_ERR(cqr) == -EAGAIN) {
1870 /*
1871 * The current request cannot be build right
1872 * now, we have to try later. If this request
1873 * is the head-of-queue we stop the device
1874 * for 1/2 second.
1875 */
1876 if (!list_empty(&block->ccw_queue))
1877 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001878 spin_lock_irqsave(
1879 get_ccwdev_lock(basedev->cdev), flags);
1880 dasd_device_set_stop_bits(basedev,
1881 DASD_STOPPED_PENDING);
1882 spin_unlock_irqrestore(
1883 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001884 dasd_block_set_timer(block, HZ/2);
1885 break;
1886 }
1887 DBF_DEV_EVENT(DBF_ERR, basedev,
1888 "CCW creation failed (rc=%ld) "
1889 "on request %p",
1890 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001891 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001892 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001893 continue;
1894 }
1895 /*
1896 * Note: callback is set to dasd_return_cqr_cb in
1897 * __dasd_block_start_head to cover erp requests as well
1898 */
1899 cqr->callback_data = (void *) req;
1900 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001901 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001902 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1903 dasd_profile_start(block, cqr, req);
1904 }
1905}
1906
1907static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1908{
1909 struct request *req;
1910 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001911 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001912
1913 req = (struct request *) cqr->callback_data;
1914 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001915 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001916 if (status <= 0)
1917 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001918 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001919}
1920
1921/*
1922 * Process ccw request queue.
1923 */
1924static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1925 struct list_head *final_queue)
1926{
1927 struct list_head *l, *n;
1928 struct dasd_ccw_req *cqr;
1929 dasd_erp_fn_t erp_fn;
1930 unsigned long flags;
1931 struct dasd_device *base = block->base;
1932
1933restart:
1934 /* Process request with final status. */
1935 list_for_each_safe(l, n, &block->ccw_queue) {
1936 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1937 if (cqr->status != DASD_CQR_DONE &&
1938 cqr->status != DASD_CQR_FAILED &&
1939 cqr->status != DASD_CQR_NEED_ERP &&
1940 cqr->status != DASD_CQR_TERMINATED)
1941 continue;
1942
1943 if (cqr->status == DASD_CQR_TERMINATED) {
1944 base->discipline->handle_terminated_request(cqr);
1945 goto restart;
1946 }
1947
1948 /* Process requests that may be recovered */
1949 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001950 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02001951 if (IS_ERR(erp_fn(cqr)))
1952 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001953 goto restart;
1954 }
1955
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001956 /* log sense for fatal error */
1957 if (cqr->status == DASD_CQR_FAILED) {
1958 dasd_log_sense(cqr, &cqr->irb);
1959 }
1960
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 /* First of all call extended error reporting. */
1962 if (dasd_eer_enabled(base) &&
1963 cqr->status == DASD_CQR_FAILED) {
1964 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1965
1966 /* restart request */
1967 cqr->status = DASD_CQR_FILLED;
1968 cqr->retries = 255;
1969 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001970 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001971 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1972 flags);
1973 goto restart;
1974 }
1975
1976 /* Process finished ERP request. */
1977 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001978 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001979 goto restart;
1980 }
1981
1982 /* Rechain finished requests to final queue */
1983 cqr->endclk = get_clock();
1984 list_move_tail(&cqr->blocklist, final_queue);
1985 }
1986}
1987
1988static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1989{
1990 dasd_schedule_block_bh(cqr->block);
1991}
1992
1993static void __dasd_block_start_head(struct dasd_block *block)
1994{
1995 struct dasd_ccw_req *cqr;
1996
1997 if (list_empty(&block->ccw_queue))
1998 return;
1999 /* We allways begin with the first requests on the queue, as some
2000 * of previously started requests have to be enqueued on a
2001 * dasd_device again for error recovery.
2002 */
2003 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2004 if (cqr->status != DASD_CQR_FILLED)
2005 continue;
2006 /* Non-temporary stop condition will trigger fail fast */
2007 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2008 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2009 (!dasd_eer_enabled(block->base))) {
2010 cqr->status = DASD_CQR_FAILED;
2011 dasd_schedule_block_bh(block);
2012 continue;
2013 }
2014 /* Don't try to start requests if device is stopped */
2015 if (block->base->stopped)
2016 return;
2017
2018 /* just a fail safe check, should not happen */
2019 if (!cqr->startdev)
2020 cqr->startdev = block->base;
2021
2022 /* make sure that the requests we submit find their way back */
2023 cqr->callback = dasd_return_cqr_cb;
2024
2025 dasd_add_request_tail(cqr);
2026 }
2027}
2028
2029/*
2030 * Central dasd_block layer routine. Takes requests from the generic
2031 * block layer request queue, creates ccw requests, enqueues them on
2032 * a dasd_device and processes ccw requests that have been returned.
2033 */
2034static void dasd_block_tasklet(struct dasd_block *block)
2035{
2036 struct list_head final_queue;
2037 struct list_head *l, *n;
2038 struct dasd_ccw_req *cqr;
2039
2040 atomic_set(&block->tasklet_scheduled, 0);
2041 INIT_LIST_HEAD(&final_queue);
2042 spin_lock(&block->queue_lock);
2043 /* Finish off requests on ccw queue */
2044 __dasd_process_block_ccw_queue(block, &final_queue);
2045 spin_unlock(&block->queue_lock);
2046 /* Now call the callback function of requests with final status */
2047 spin_lock_irq(&block->request_queue_lock);
2048 list_for_each_safe(l, n, &final_queue) {
2049 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2050 list_del_init(&cqr->blocklist);
2051 __dasd_cleanup_cqr(cqr);
2052 }
2053 spin_lock(&block->queue_lock);
2054 /* Get new request from the block device request queue */
2055 __dasd_process_request_queue(block);
2056 /* Now check if the head of the ccw queue needs to be started. */
2057 __dasd_block_start_head(block);
2058 spin_unlock(&block->queue_lock);
2059 spin_unlock_irq(&block->request_queue_lock);
2060 dasd_put_device(block->base);
2061}
2062
2063static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2064{
2065 wake_up(&dasd_flush_wq);
2066}
2067
2068/*
2069 * Go through all request on the dasd_block request queue, cancel them
2070 * on the respective dasd_device, and return them to the generic
2071 * block layer.
2072 */
2073static int dasd_flush_block_queue(struct dasd_block *block)
2074{
2075 struct dasd_ccw_req *cqr, *n;
2076 int rc, i;
2077 struct list_head flush_queue;
2078
2079 INIT_LIST_HEAD(&flush_queue);
2080 spin_lock_bh(&block->queue_lock);
2081 rc = 0;
2082restart:
2083 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2084 /* if this request currently owned by a dasd_device cancel it */
2085 if (cqr->status >= DASD_CQR_QUEUED)
2086 rc = dasd_cancel_req(cqr);
2087 if (rc < 0)
2088 break;
2089 /* Rechain request (including erp chain) so it won't be
2090 * touched by the dasd_block_tasklet anymore.
2091 * Replace the callback so we notice when the request
2092 * is returned from the dasd_device layer.
2093 */
2094 cqr->callback = _dasd_wake_block_flush_cb;
2095 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2096 list_move_tail(&cqr->blocklist, &flush_queue);
2097 if (i > 1)
2098 /* moved more than one request - need to restart */
2099 goto restart;
2100 }
2101 spin_unlock_bh(&block->queue_lock);
2102 /* Now call the callback function of flushed requests */
2103restart_cb:
2104 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2105 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2106 /* Process finished ERP request. */
2107 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002108 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002109 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002110 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002111 /* restart list_for_xx loop since dasd_process_erp
2112 * might remove multiple elements */
2113 goto restart_cb;
2114 }
2115 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002116 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002117 cqr->endclk = get_clock();
2118 list_del_init(&cqr->blocklist);
2119 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002120 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return rc;
2123}
2124
2125/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002126 * Schedules a call to dasd_tasklet over the device tasklet.
2127 */
2128void dasd_schedule_block_bh(struct dasd_block *block)
2129{
2130 /* Protect against rescheduling. */
2131 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2132 return;
2133 /* life cycle of block is bound to it's base device */
2134 dasd_get_device(block->base);
2135 tasklet_hi_schedule(&block->tasklet);
2136}
2137
2138
2139/*
2140 * SECTION: external block device operations
2141 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
2143
2144/*
2145 * Dasd request queue function. Called from ll_rw_blk.c
2146 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002147static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002149 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002151 block = queue->queuedata;
2152 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002154 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002156 __dasd_block_start_head(block);
2157 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160/*
2161 * Allocate and initialize request queue and default I/O scheduler.
2162 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002163static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 int rc;
2166
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002167 block->request_queue = blk_init_queue(do_dasd_request,
2168 &block->request_queue_lock);
2169 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return -ENOMEM;
2171
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002172 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002174 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002175 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002176 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002178 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return rc;
2180 }
2181 return 0;
2182}
2183
2184/*
2185 * Allocate and initialize request queue.
2186 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002187static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 int max;
2190
Martin K. Petersene1defc42009-05-22 17:17:49 -04002191 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002192 max = block->base->discipline->max_blocks << block->s2b_shift;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002193 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002194 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002195 /* with page sized segments we can translate each segement into
2196 * one idaw/tidaw
2197 */
2198 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2199 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
FUJITA Tomonori00fff262010-07-03 17:45:40 +09002200 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203/*
2204 * Deactivate and free request queue.
2205 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002206static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002208 if (block->request_queue) {
2209 blk_cleanup_queue(block->request_queue);
2210 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212}
2213
2214/*
2215 * Flush request on the request queue.
2216 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002217static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct request *req;
2220
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002221 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002223
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002224 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002225 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002226 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002227 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Al Viro57a7c0b2008-03-02 10:36:08 -05002230static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Al Viro57a7c0b2008-03-02 10:36:08 -05002232 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Haberland9eb25122010-02-26 22:37:46 +01002233 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 int rc;
2235
Stefan Haberland9eb25122010-02-26 22:37:46 +01002236 if (!block)
2237 return -ENODEV;
2238
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002239 lock_kernel();
Stefan Haberland9eb25122010-02-26 22:37:46 +01002240 base = block->base;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002241 atomic_inc(&block->open_count);
2242 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 rc = -ENODEV;
2244 goto unlock;
2245 }
2246
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002247 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 rc = -EINVAL;
2249 goto unlock;
2250 }
2251
2252 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002253 dev_info(&base->cdev->dev,
2254 "Accessing the DASD failed because it is in "
2255 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 rc = -EPERM;
2257 goto out;
2258 }
2259
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002260 if (base->state <= DASD_STATE_BASIC) {
2261 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 " Cannot open unrecognized device");
2263 rc = -ENODEV;
2264 goto out;
2265 }
2266
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002267 if ((mode & FMODE_WRITE) &&
2268 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2269 (base->features & DASD_FEATURE_READONLY))) {
2270 rc = -EROFS;
2271 goto out;
2272 }
2273
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002274 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return 0;
2276
2277out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002278 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002280 atomic_dec(&block->open_count);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002281 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return rc;
2283}
2284
Al Viro57a7c0b2008-03-02 10:36:08 -05002285static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002287 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002289 lock_kernel();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002290 atomic_dec(&block->open_count);
2291 module_put(block->base->discipline->owner);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002292 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
2294}
2295
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002296/*
2297 * Return disk geometry.
2298 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002299static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002300{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002301 struct dasd_block *block;
2302 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002303
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002304 block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002305 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002306 return -ENODEV;
Julia Lawallcf05b822009-08-23 18:09:05 +02002307 base = block->base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002308
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002309 if (!base->discipline ||
2310 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002311 return -EINVAL;
2312
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002313 base->discipline->fill_geometry(block, geo);
2314 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002315 return 0;
2316}
2317
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002318const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319dasd_device_operations = {
2320 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002321 .open = dasd_open,
2322 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002323 .ioctl = dasd_ioctl,
2324 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002325 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326};
2327
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002328/*******************************************************************************
2329 * end of block device operations
2330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332static void
2333dasd_exit(void)
2334{
2335#ifdef CONFIG_PROC_FS
2336 dasd_proc_exit();
2337#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002338 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002339 if (dasd_page_cache != NULL) {
2340 kmem_cache_destroy(dasd_page_cache);
2341 dasd_page_cache = NULL;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 dasd_gendisk_exit();
2344 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (dasd_debug_area != NULL) {
2346 debug_unregister(dasd_debug_area);
2347 dasd_debug_area = NULL;
2348 }
2349}
2350
2351/*
2352 * SECTION: common functions for ccw_driver use
2353 */
2354
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002355/*
2356 * Is the device read-only?
2357 * Note that this function does not report the setting of the
2358 * readonly device attribute, but how it is configured in z/VM.
2359 */
2360int dasd_device_is_ro(struct dasd_device *device)
2361{
2362 struct ccw_dev_id dev_id;
2363 struct diag210 diag_data;
2364 int rc;
2365
2366 if (!MACHINE_IS_VM)
2367 return 0;
2368 ccw_device_get_id(device->cdev, &dev_id);
2369 memset(&diag_data, 0, sizeof(diag_data));
2370 diag_data.vrdcdvno = dev_id.devno;
2371 diag_data.vrdclen = sizeof(diag_data);
2372 rc = diag210(&diag_data);
2373 if (rc == 0 || rc == 2) {
2374 return diag_data.vrdcvfla & 0x80;
2375 } else {
2376 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2377 dev_id.devno, rc);
2378 return 0;
2379 }
2380}
2381EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2382
Cornelia Huckf3445a12009-04-14 15:36:23 +02002383static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2384{
2385 struct ccw_device *cdev = data;
2386 int ret;
2387
2388 ret = ccw_device_set_online(cdev);
2389 if (ret)
2390 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2391 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002392}
2393
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002394/*
2395 * Initial attempt at a probe function. this can be simplified once
2396 * the other detection code is gone.
2397 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002398int dasd_generic_probe(struct ccw_device *cdev,
2399 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 int ret;
2402
2403 ret = dasd_add_sysfs_files(cdev);
2404 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002405 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2406 "dasd_generic_probe: could not add "
2407 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Horst Hummel40545572006-06-29 15:08:18 +02002410 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Horst Hummel40545572006-06-29 15:08:18 +02002412 /*
2413 * Automatically online either all dasd devices (dasd_autodetect)
2414 * or all devices specified with dasd= parameters during
2415 * initial probe.
2416 */
2417 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002418 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002419 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002423/*
2424 * This will one day be called from a global not_oper handler.
2425 * It is also used by driver_unregister during module unload.
2426 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002427void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002430 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Horst Hummel59afda72005-05-16 21:53:39 -07002432 cdev->handler = NULL;
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 dasd_remove_sysfs_files(cdev);
2435 device = dasd_device_from_cdev(cdev);
2436 if (IS_ERR(device))
2437 return;
2438 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2439 /* Already doing offline processing */
2440 dasd_put_device(device);
2441 return;
2442 }
2443 /*
2444 * This device is removed unconditionally. Set offline
2445 * flag to prevent dasd_open from opening it while it is
2446 * no quite down yet.
2447 */
2448 dasd_set_target_state(device, DASD_STATE_NEW);
2449 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002450 block = device->block;
2451 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002453 /*
2454 * life cycle of block is bound to device, so delete it after
2455 * device was safely removed
2456 */
2457 if (block)
2458 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002461/*
2462 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002464 * or the user has started activation through sysfs.
2465 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002466int dasd_generic_set_online(struct ccw_device *cdev,
2467 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002469 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002471 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002472
Horst Hummel40545572006-06-29 15:08:18 +02002473 /* first online clears initial online feature flag */
2474 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 device = dasd_create_device(cdev);
2476 if (IS_ERR(device))
2477 return PTR_ERR(device);
2478
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002479 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002480 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002482 pr_warning("%s Setting the DASD online failed because "
2483 "of missing DIAG discipline\n",
2484 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 dasd_delete_device(device);
2486 return -ENODEV;
2487 }
2488 discipline = dasd_diag_discipline_pointer;
2489 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002490 if (!try_module_get(base_discipline->owner)) {
2491 dasd_delete_device(device);
2492 return -EINVAL;
2493 }
2494 if (!try_module_get(discipline->owner)) {
2495 module_put(base_discipline->owner);
2496 dasd_delete_device(device);
2497 return -EINVAL;
2498 }
2499 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 device->discipline = discipline;
2501
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002502 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 rc = discipline->check_device(device);
2504 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002505 pr_warning("%s Setting the DASD online with discipline %s "
2506 "failed with rc=%i\n",
2507 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002508 module_put(discipline->owner);
2509 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 dasd_delete_device(device);
2511 return rc;
2512 }
2513
2514 dasd_set_target_state(device, DASD_STATE_ONLINE);
2515 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002516 pr_warning("%s Setting the DASD online failed because of a "
2517 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 rc = -ENODEV;
2519 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002520 if (device->block)
2521 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 dasd_delete_device(device);
2523 } else
2524 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002525 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01002526
2527 wait_event(dasd_init_waitq, _wait_for_device(device));
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return rc;
2531}
2532
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002533int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002537 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 device = dasd_device_from_cdev(cdev);
2540 if (IS_ERR(device))
2541 return PTR_ERR(device);
2542 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2543 /* Already doing offline processing */
2544 dasd_put_device(device);
2545 return 0;
2546 }
2547 /*
2548 * We must make sure that this device is currently not in use.
2549 * The open_count is increased for every opener, that includes
2550 * the blkdev_get in dasd_scan_partitions. We are only interested
2551 * in the other openers.
2552 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002553 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002554 max_count = device->block->bdev ? 0 : -1;
2555 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002556 if (open_count > max_count) {
2557 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002558 pr_warning("%s: The DASD cannot be set offline "
2559 "with open count %i\n",
2560 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002561 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002562 pr_warning("%s: The DASD cannot be set offline "
2563 "while it is in use\n",
2564 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002565 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2566 dasd_put_device(device);
2567 return -EBUSY;
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570 dasd_set_target_state(device, DASD_STATE_NEW);
2571 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002572 block = device->block;
2573 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002575 /*
2576 * life cycle of block is bound to device, so delete it after
2577 * device was safely removed
2578 */
2579 if (block)
2580 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 0;
2582}
2583
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002584int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 struct dasd_device *device;
2587 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int ret;
2589
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002590 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (IS_ERR(device))
2592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 ret = 0;
2594 switch (event) {
2595 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002596 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002598 /* First of all call extended error reporting. */
2599 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (device->state < DASD_STATE_BASIC)
2602 break;
2603 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002604 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2605 if (cqr->status == DASD_CQR_IN_IO) {
2606 cqr->status = DASD_CQR_QUEUED;
2607 cqr->retries++;
2608 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002609 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002610 dasd_device_clear_timer(device);
2611 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 ret = 1;
2613 break;
2614 case CIO_OPER:
2615 /* FIXME: add a sanity check. */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002616 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002617 if (device->stopped & DASD_UNRESUMED_PM) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002618 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002619 dasd_restore_device(device);
2620 ret = 1;
2621 break;
2622 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002623 dasd_schedule_device_bh(device);
2624 if (device->block)
2625 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 ret = 1;
2627 break;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 dasd_put_device(device);
2630 return ret;
2631}
2632
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002633int dasd_generic_pm_freeze(struct ccw_device *cdev)
2634{
2635 struct dasd_ccw_req *cqr, *n;
2636 int rc;
2637 struct list_head freeze_queue;
2638 struct dasd_device *device = dasd_device_from_cdev(cdev);
2639
2640 if (IS_ERR(device))
2641 return PTR_ERR(device);
2642 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002643 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002644 /* clear active requests */
2645 INIT_LIST_HEAD(&freeze_queue);
2646 spin_lock_irq(get_ccwdev_lock(cdev));
2647 rc = 0;
2648 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2649 /* Check status and move request to flush_queue */
2650 if (cqr->status == DASD_CQR_IN_IO) {
2651 rc = device->discipline->term_IO(cqr);
2652 if (rc) {
2653 /* unable to terminate requeust */
2654 dev_err(&device->cdev->dev,
2655 "Unable to terminate request %p "
2656 "on suspend\n", cqr);
2657 spin_unlock_irq(get_ccwdev_lock(cdev));
2658 dasd_put_device(device);
2659 return rc;
2660 }
2661 }
2662 list_move_tail(&cqr->devlist, &freeze_queue);
2663 }
2664
2665 spin_unlock_irq(get_ccwdev_lock(cdev));
2666
2667 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2668 wait_event(dasd_flush_wq,
2669 (cqr->status != DASD_CQR_CLEAR_PENDING));
2670 if (cqr->status == DASD_CQR_CLEARED)
2671 cqr->status = DASD_CQR_QUEUED;
2672 }
2673 /* move freeze_queue to start of the ccw_queue */
2674 spin_lock_irq(get_ccwdev_lock(cdev));
2675 list_splice_tail(&freeze_queue, &device->ccw_queue);
2676 spin_unlock_irq(get_ccwdev_lock(cdev));
2677
2678 if (device->discipline->freeze)
2679 rc = device->discipline->freeze(device);
2680
2681 dasd_put_device(device);
2682 return rc;
2683}
2684EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2685
2686int dasd_generic_restore_device(struct ccw_device *cdev)
2687{
2688 struct dasd_device *device = dasd_device_from_cdev(cdev);
2689 int rc = 0;
2690
2691 if (IS_ERR(device))
2692 return PTR_ERR(device);
2693
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002694 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002695 dasd_device_remove_stop_bits(device,
2696 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002697
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002698 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002699
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002700 /*
2701 * call discipline restore function
2702 * if device is stopped do nothing e.g. for disconnected devices
2703 */
2704 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002705 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002706 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002707 /*
2708 * if the resume failed for the DASD we put it in
2709 * an UNRESUMED stop state
2710 */
2711 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002712
Stefan Haberland6fca97a2009-10-06 10:34:15 +02002713 if (device->block)
2714 dasd_schedule_block_bh(device->block);
2715
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002716 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002717 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002718}
2719EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2720
Heiko Carstens763968e2007-05-10 15:45:46 +02002721static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2722 void *rdc_buffer,
2723 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02002724 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002725{
2726 struct dasd_ccw_req *cqr;
2727 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002728 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02002729
2730 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2731
2732 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002733 /* internal error 13 - Allocating the RDC request failed*/
2734 dev_err(&device->cdev->dev,
2735 "An error occurred in the DASD device driver, "
2736 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002737 return cqr;
2738 }
2739
2740 ccw = cqr->cpaddr;
2741 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002742 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2743 idaw = (unsigned long *) (cqr->data);
2744 ccw->cda = (__u32)(addr_t) idaw;
2745 ccw->flags = CCW_FLAG_IDA;
2746 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2747 } else {
2748 ccw->cda = (__u32)(addr_t) rdc_buffer;
2749 ccw->flags = 0;
2750 }
Cornelia Huck17283b52007-05-04 18:47:51 +02002751
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002752 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002753 cqr->startdev = device;
2754 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002755 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002756 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02002757 cqr->buildclk = get_clock();
2758 cqr->status = DASD_CQR_FILLED;
2759 return cqr;
2760}
2761
2762
Stefan Haberland68b781f2009-09-11 10:28:29 +02002763int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002764 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002765{
2766 int ret;
2767 struct dasd_ccw_req *cqr;
2768
Sebastian Ott92636b12009-06-12 10:26:37 +02002769 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002770 magic);
2771 if (IS_ERR(cqr))
2772 return PTR_ERR(cqr);
2773
2774 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002775 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002776 return ret;
2777}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002778EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002779
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002780/*
2781 * In command mode and transport mode we need to look for sense
2782 * data in different places. The sense data itself is allways
2783 * an array of 32 bytes, so we can unify the sense data access
2784 * for both modes.
2785 */
2786char *dasd_get_sense(struct irb *irb)
2787{
2788 struct tsb *tsb = NULL;
2789 char *sense = NULL;
2790
2791 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2792 if (irb->scsw.tm.tcw)
2793 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2794 irb->scsw.tm.tcw);
2795 if (tsb && tsb->length == 64 && tsb->flags)
2796 switch (tsb->flags & 0x07) {
2797 case 1: /* tsa_iostat */
2798 sense = tsb->tsa.iostat.sense;
2799 break;
2800 case 2: /* tsa_ddpc */
2801 sense = tsb->tsa.ddpc.sense;
2802 break;
2803 default:
2804 /* currently we don't use interrogate data */
2805 break;
2806 }
2807 } else if (irb->esw.esw0.erw.cons) {
2808 sense = irb->ecw;
2809 }
2810 return sense;
2811}
2812EXPORT_SYMBOL_GPL(dasd_get_sense);
2813
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002814static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
2816 int rc;
2817
2818 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002819 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002820 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002823 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (dasd_debug_area == NULL) {
2825 rc = -ENOMEM;
2826 goto failed;
2827 }
2828 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002829 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2832
2833 dasd_diag_discipline_pointer = NULL;
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 rc = dasd_devmap_init();
2836 if (rc)
2837 goto failed;
2838 rc = dasd_gendisk_init();
2839 if (rc)
2840 goto failed;
2841 rc = dasd_parse();
2842 if (rc)
2843 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002844 rc = dasd_eer_init();
2845 if (rc)
2846 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847#ifdef CONFIG_PROC_FS
2848 rc = dasd_proc_init();
2849 if (rc)
2850 goto failed;
2851#endif
2852
2853 return 0;
2854failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002855 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 dasd_exit();
2857 return rc;
2858}
2859
2860module_init(dasd_init);
2861module_exit(dasd_exit);
2862
2863EXPORT_SYMBOL(dasd_debug_area);
2864EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2865
2866EXPORT_SYMBOL(dasd_add_request_head);
2867EXPORT_SYMBOL(dasd_add_request_tail);
2868EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002869EXPORT_SYMBOL(dasd_device_clear_timer);
2870EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871EXPORT_SYMBOL(dasd_enable_device);
2872EXPORT_SYMBOL(dasd_int_handler);
2873EXPORT_SYMBOL(dasd_kfree_request);
2874EXPORT_SYMBOL(dasd_kick_device);
2875EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002876EXPORT_SYMBOL(dasd_schedule_device_bh);
2877EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002879EXPORT_SYMBOL(dasd_device_set_timer);
2880EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881EXPORT_SYMBOL(dasd_sfree_request);
2882EXPORT_SYMBOL(dasd_sleep_on);
2883EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2884EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2885EXPORT_SYMBOL(dasd_smalloc_request);
2886EXPORT_SYMBOL(dasd_start_IO);
2887EXPORT_SYMBOL(dasd_term_IO);
2888
2889EXPORT_SYMBOL_GPL(dasd_generic_probe);
2890EXPORT_SYMBOL_GPL(dasd_generic_remove);
2891EXPORT_SYMBOL_GPL(dasd_generic_notify);
2892EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2893EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002894EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2895EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2896EXPORT_SYMBOL_GPL(dasd_alloc_block);
2897EXPORT_SYMBOL_GPL(dasd_free_block);