blob: 605f96f154a5db38e2a3b58f6502e2daa2f702a7 [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
Heiko Carstens32839422011-01-05 12:47:30 +010014#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kmod.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/major.h>
20#include <linux/slab.h>
21#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080022#include <linux/hdreg.h>
Cornelia Huckf3445a12009-04-14 15:36:23 +020023#include <linux/async.h>
Stefan Haberland9eb25122010-02-26 22:37:46 +010024#include <linux/mutex.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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
749 if (cplength > 0)
750 size += cplength * sizeof(struct ccw1);
751 if (datasize > 0)
752 size += datasize;
753 spin_lock_irqsave(&device->mem_lock, flags);
754 cqr = (struct dasd_ccw_req *)
755 dasd_alloc_chunk(&device->ccw_chunks, size);
756 spin_unlock_irqrestore(&device->mem_lock, flags);
757 if (cqr == NULL)
758 return ERR_PTR(-ENOMEM);
759 memset(cqr, 0, sizeof(struct dasd_ccw_req));
760 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
761 cqr->cpaddr = NULL;
762 if (cplength > 0) {
763 cqr->cpaddr = (struct ccw1 *) data;
764 data += cplength*sizeof(struct ccw1);
765 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
766 }
767 cqr->data = NULL;
768 if (datasize > 0) {
769 cqr->data = data;
770 memset(cqr->data, 0, datasize);
771 }
Stefan Haberland68b781f2009-09-11 10:28:29 +0200772 cqr->magic = magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
774 dasd_get_device(device);
775 return cqr;
776}
777
778/*
779 * Free memory of a channel program. This function needs to free all the
780 * idal lists that might have been created by dasd_set_cda and the
781 * struct dasd_ccw_req itself.
782 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100783void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800785#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct ccw1 *ccw;
787
788 /* Clear any idals used for the request. */
789 ccw = cqr->cpaddr;
790 do {
791 clear_normalized_cda(ccw);
792 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
793#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800794 kfree(cqr->cpaddr);
795 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 kfree(cqr);
797 dasd_put_device(device);
798}
799
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100800void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 unsigned long flags;
803
804 spin_lock_irqsave(&device->mem_lock, flags);
805 dasd_free_chunk(&device->ccw_chunks, cqr);
806 spin_unlock_irqrestore(&device->mem_lock, flags);
807 dasd_put_device(device);
808}
809
810/*
811 * Check discipline magic in cqr.
812 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100813static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct dasd_device *device;
816
817 if (cqr == NULL)
818 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100819 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100821 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 " dasd_ccw_req 0x%08x magic doesn't match"
823 " discipline 0x%08x",
824 cqr->magic,
825 *(unsigned int *) device->discipline->name);
826 return -EINVAL;
827 }
828 return 0;
829}
830
831/*
832 * Terminate the current i/o and set the request to clear_pending.
833 * Timer keeps device runnig.
834 * ccw_device_clear can fail if the i/o subsystem
835 * is in a bad mood.
836 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100837int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct dasd_device *device;
840 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100841 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Check the cqr */
844 rc = dasd_check_cqr(cqr);
845 if (rc)
846 return rc;
847 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100848 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
850 rc = ccw_device_clear(device->cdev, (long) cqr);
851 switch (rc) {
852 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800853 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100854 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200856 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 DBF_DEV_EVENT(DBF_DEBUG, device,
858 "terminate cqr %p successful",
859 cqr);
860 break;
861 case -ENODEV:
862 DBF_DEV_EVENT(DBF_ERR, device, "%s",
863 "device gone, retry");
864 break;
865 case -EIO:
866 DBF_DEV_EVENT(DBF_ERR, device, "%s",
867 "I/O error, retry");
868 break;
869 case -EINVAL:
870 case -EBUSY:
871 DBF_DEV_EVENT(DBF_ERR, device, "%s",
872 "device busy, retry later");
873 break;
874 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100875 /* internal error 10 - unknown rc*/
876 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
877 dev_err(&device->cdev->dev, "An error occurred in the "
878 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 BUG();
880 break;
881 }
882 retries++;
883 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100884 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return rc;
886}
887
888/*
889 * Start the i/o. This start_IO can fail if the channel is really busy.
890 * In that case set up a timer to start the request later.
891 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100892int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct dasd_device *device;
895 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100896 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* Check the cqr */
899 rc = dasd_check_cqr(cqr);
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200900 if (rc) {
901 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return rc;
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200903 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100904 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100906 /* internal error 14 - start_IO run out of retries */
907 sprintf(errorstring, "14 %p", cqr);
908 dev_err(&device->cdev->dev, "An error occurred in the DASD "
909 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100910 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -EIO;
912 }
913 cqr->startclk = get_clock();
914 cqr->starttime = jiffies;
915 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100916 if (cqr->cpmode == 1) {
917 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
918 (long) cqr, cqr->lpm);
919 } else {
920 rc = ccw_device_start(device->cdev, cqr->cpaddr,
921 (long) cqr, cqr->lpm, 0);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 switch (rc) {
924 case 0:
925 cqr->status = DASD_CQR_IN_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100928 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 "start_IO: device busy, retry later");
930 break;
931 case -ETIMEDOUT:
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: request timeout, retry later");
934 break;
935 case -EACCES:
936 /* -EACCES indicates that the request used only a
937 * subset of the available pathes and all these
938 * pathes are gone.
939 * Do a retry with all available pathes.
940 */
941 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100942 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 "start_IO: selected pathes gone,"
944 " retry on all pathes");
945 break;
946 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100947 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
948 "start_IO: -ENODEV device gone, retry");
949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100951 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100952 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
Stefan Haberlandd41dd122009-06-16 10:30:25 +0200954 case -EINVAL:
955 /* most likely caused in power management context */
956 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
957 "start_IO: -EINVAL device currently "
958 "not accessible");
959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100961 /* internal error 11 - unknown rc */
962 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
963 dev_err(&device->cdev->dev,
964 "An error occurred in the DASD device driver, "
965 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 BUG();
967 break;
968 }
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +0200969 cqr->intrc = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return rc;
971}
972
973/*
974 * Timeout function for dasd devices. This is used for different purposes
975 * 1) missing interrupt handler for normal operation
976 * 2) delayed start of request where start_IO failed with -EBUSY
977 * 3) timeout for missing state change interrupts
978 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
979 * DASD_CQR_QUEUED for 2) and 3).
980 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100981static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 unsigned long flags;
984 struct dasd_device *device;
985
986 device = (struct dasd_device *) ptr;
987 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
988 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100989 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100991 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994/*
995 * Setup timeout for a device in jiffies.
996 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100997void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100999 if (expires == 0)
1000 del_timer(&device->timer);
1001 else
1002 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005/*
1006 * Clear timeout for a device.
1007 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001008void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001010 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001013static void dasd_handle_killed_request(struct ccw_device *cdev,
1014 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct dasd_ccw_req *cqr;
1017 struct dasd_device *device;
1018
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001019 if (!intparm)
1020 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 cqr = (struct dasd_ccw_req *) intparm;
1022 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001023 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1024 "invalid status in handle_killed_request: "
1025 "%02x", cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return;
1027 }
1028
Stefan Haberland589c74d2010-02-26 22:37:47 +01001029 device = dasd_device_from_cdev_locked(cdev);
1030 if (IS_ERR(device)) {
1031 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1032 "unable to get device from cdev");
1033 return;
1034 }
1035
1036 if (!cqr->startdev ||
1037 device != cqr->startdev ||
1038 strncmp(cqr->startdev->discipline->ebcname,
1039 (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001040 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1041 "invalid device in request");
Stefan Haberland589c74d2010-02-26 22:37:47 +01001042 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return;
1044 }
1045
1046 /* Schedule request to be retried. */
1047 cqr->status = DASD_CQR_QUEUED;
1048
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001049 dasd_device_clear_timer(device);
1050 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dasd_put_device(device);
1052}
1053
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001054void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001056 /* First of all start sense subsystem status request. */
1057 dasd_eer_snss(device);
1058
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001059 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001060 dasd_schedule_device_bh(device);
1061 if (device->block)
1062 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
1066 * Interrupt handler for "normal" ssch-io based dasd devices.
1067 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001068void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1069 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct dasd_ccw_req *cqr, *next;
1072 struct dasd_device *device;
1073 unsigned long long now;
1074 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Heiko Carstens32839422011-01-05 12:47:30 +01001076 kstat_cpu(smp_processor_id()).irqs[IOINT_DAS]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (IS_ERR(irb)) {
1078 switch (PTR_ERR(irb)) {
1079 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 case -ETIMEDOUT:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001082 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1083 "request timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 default:
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01001086 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1087 "unknown error %ld\n", __func__,
1088 PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001090 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return;
1092 }
1093
1094 now = get_clock();
1095
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001096 /* check for unsolicited interrupts */
1097 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001098 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1099 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001100 ((scsw_stctl(&irb->scsw) == SCSW_STCTL_STATUS_PEND) ||
1101 (scsw_stctl(&irb->scsw) == (SCSW_STCTL_STATUS_PEND |
1102 SCSW_STCTL_ALERT_STATUS))))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001103 if (cqr && cqr->status == DASD_CQR_IN_IO)
1104 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhubera5a00612010-10-25 16:10:47 +02001105 if (cqr)
1106 memcpy(&cqr->irb, irb, sizeof(*irb));
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001107 device = dasd_device_from_cdev_locked(cdev);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001108 if (IS_ERR(device))
1109 return;
1110 /* ignore unsolicited interrupts for DIAG discipline */
1111 if (device->discipline == dasd_diag_discipline_pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 dasd_put_device(device);
Stefan Haberland56b86b62010-10-25 16:10:49 +02001113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Stefan Haberland56b86b62010-10-25 16:10:49 +02001115 device->discipline->dump_sense_dbf(device, irb,
1116 "unsolicited");
1117 if ((device->features & DASD_FEATURE_ERPLOG))
1118 device->discipline->dump_sense(device, cqr,
1119 irb);
1120 dasd_device_clear_timer(device);
1121 device->discipline->handle_unsolicited_interrupt(device,
1122 irb);
1123 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return;
1125 }
1126
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001127 device = (struct dasd_device *) cqr->startdev;
1128 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberland294001a2010-01-27 10:12:35 +01001130 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1131 "invalid device in request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return;
1133 }
1134
1135 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001136 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001137 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001138 cqr->status = DASD_CQR_CLEARED;
1139 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001140 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001141 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return;
1143 }
1144
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001145 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001147 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1148 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return;
1150 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001151
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001152 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001154 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1155 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001156 /* request was completed successfully */
1157 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 cqr->stopclk = now;
1159 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001160 if (cqr->devlist.next != &device->ccw_queue) {
1161 next = list_entry(cqr->devlist.next,
1162 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001164 } else { /* error */
1165 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001166 /* log sense for every failed I/O to s390 debugfeature */
1167 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001168 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001169 dasd_log_sense(cqr, irb);
1170 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001171
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001172 /*
1173 * If we don't want complex ERP for this request, then just
1174 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001175 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001176 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001177 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001178 if (cqr->lpm == LPM_ANYPATH)
1179 DBF_DEV_EVENT(DBF_DEBUG, device,
1180 "default ERP in fastpath "
1181 "(%i retries left)",
1182 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001183 cqr->lpm = LPM_ANYPATH;
1184 cqr->status = DASD_CQR_QUEUED;
1185 next = cqr;
1186 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001188 }
1189 if (next && (next->status == DASD_CQR_QUEUED) &&
1190 (!device->stopped)) {
1191 if (device->discipline->start_IO(next) == 0)
1192 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001195 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001197 dasd_device_clear_timer(device);
1198 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Stefan Haberlanda23ed002010-05-26 23:27:09 +02001201enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1202{
1203 struct dasd_device *device;
1204
1205 device = dasd_device_from_cdev_locked(cdev);
1206
1207 if (IS_ERR(device))
1208 goto out;
1209 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1210 device->state != device->target ||
1211 !device->discipline->handle_unsolicited_interrupt){
1212 dasd_put_device(device);
1213 goto out;
1214 }
1215
1216 dasd_device_clear_timer(device);
1217 device->discipline->handle_unsolicited_interrupt(device, irb);
1218 dasd_put_device(device);
1219out:
1220 return UC_TODO_RETRY;
1221}
1222EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001225 * If we have an error on a dasd_block layer request then we cancel
1226 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001228static void __dasd_device_recovery(struct dasd_device *device,
1229 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct list_head *l, *n;
1232 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001235 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001237 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001239
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001240 list_for_each_safe(l, n, &device->ccw_queue) {
1241 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1242 if (cqr->status == DASD_CQR_QUEUED &&
1243 ref_cqr->block == cqr->block) {
1244 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001246 }
1247};
1248
1249/*
1250 * Remove those ccw requests from the queue that need to be returned
1251 * to the upper layer.
1252 */
1253static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1254 struct list_head *final_queue)
1255{
1256 struct list_head *l, *n;
1257 struct dasd_ccw_req *cqr;
1258
1259 /* Process request with final status. */
1260 list_for_each_safe(l, n, &device->ccw_queue) {
1261 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1262
1263 /* Stop list processing at the first non-final request. */
1264 if (cqr->status == DASD_CQR_QUEUED ||
1265 cqr->status == DASD_CQR_IN_IO ||
1266 cqr->status == DASD_CQR_CLEAR_PENDING)
1267 break;
1268 if (cqr->status == DASD_CQR_ERROR) {
1269 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001271 /* Rechain finished requests to final queue */
1272 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274}
1275
1276/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277 * the cqrs from the final queue are returned to the upper layer
1278 * by setting a dasd_block state and calling the callback function
1279 */
1280static void __dasd_device_process_final_queue(struct dasd_device *device,
1281 struct list_head *final_queue)
1282{
1283 struct list_head *l, *n;
1284 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001285 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001286 void (*callback)(struct dasd_ccw_req *, void *data);
1287 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001288 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001289
1290 list_for_each_safe(l, n, final_queue) {
1291 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1292 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001293 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001294 callback = cqr->callback;
1295 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001296 if (block)
1297 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001298 switch (cqr->status) {
1299 case DASD_CQR_SUCCESS:
1300 cqr->status = DASD_CQR_DONE;
1301 break;
1302 case DASD_CQR_ERROR:
1303 cqr->status = DASD_CQR_NEED_ERP;
1304 break;
1305 case DASD_CQR_CLEARED:
1306 cqr->status = DASD_CQR_TERMINATED;
1307 break;
1308 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001309 /* internal error 12 - wrong cqr status*/
1310 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1311 dev_err(&device->cdev->dev,
1312 "An error occurred in the DASD device driver, "
1313 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001314 BUG();
1315 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001316 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001317 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001318 if (block)
1319 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001320 }
1321}
1322
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * Take a look at the first request on the ccw queue and check
1325 * if it reached its expire time. If so, terminate the IO.
1326 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001327static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct dasd_ccw_req *cqr;
1330
1331 if (list_empty(&device->ccw_queue))
1332 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001333 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001334 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1335 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1336 if (device->discipline->term_IO(cqr) != 0) {
1337 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001338 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001339 "cqr %p timed out (%lus) but cannot be "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001340 "ended, retrying in 5 s\n",
1341 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001342 cqr->expires += 5*HZ;
1343 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001344 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001345 dev_err(&device->cdev->dev,
Heiko Carstens625c94d2010-08-13 10:06:38 +02001346 "cqr %p timed out (%lus), %i retries "
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001347 "remaining\n", cqr, (cqr->expires/HZ),
1348 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
1351}
1352
1353/*
1354 * Take a look at the first request on the ccw queue and check
1355 * if it needs to be started.
1356 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001357static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct dasd_ccw_req *cqr;
1360 int rc;
1361
1362 if (list_empty(&device->ccw_queue))
1363 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001365 if (cqr->status != DASD_CQR_QUEUED)
1366 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001367 /* when device is stopped, return request to previous layer */
1368 if (device->stopped) {
1369 cqr->status = DASD_CQR_CLEARED;
1370 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001371 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001372 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001373
1374 rc = device->discipline->start_IO(cqr);
1375 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001376 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001377 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001378 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001379 } else
1380 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001381 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001385 * Go through all request on the dasd_device request queue,
1386 * terminate them on the cdev if necessary, and return them to the
1387 * submitting layer via callback.
1388 * Note:
1389 * Make sure that all 'submitting layers' still exist when
1390 * this function is called!. In other words, when 'device' is a base
1391 * device then all block layer requests must have been removed before
1392 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001394int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001396 struct dasd_ccw_req *cqr, *n;
1397 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 INIT_LIST_HEAD(&flush_queue);
1401 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001402 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001404 /* Check status and move request to flush_queue */
1405 switch (cqr->status) {
1406 case DASD_CQR_IN_IO:
1407 rc = device->discipline->term_IO(cqr);
1408 if (rc) {
1409 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001410 dev_err(&device->cdev->dev,
1411 "Flushing the DASD request queue "
1412 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001413 /* stop flush processing */
1414 goto finished;
1415 }
1416 break;
1417 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001419 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001420 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001421 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001422 break;
1423 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001424 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001425 }
Horst Hummel8f617012006-08-30 14:33:33 +02001426finished:
1427 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001428 /*
1429 * After this point all requests must be in state CLEAR_PENDING,
1430 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1431 * one of the others.
1432 */
1433 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1434 wait_event(dasd_flush_wq,
1435 (cqr->status != DASD_CQR_CLEAR_PENDING));
1436 /*
1437 * Now set each request back to TERMINATED, DONE or NEED_ERP
1438 * and call the callback function of flushed requests
1439 */
1440 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001441 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444/*
1445 * Acquire the device lock and process queues for the device.
1446 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001447static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 atomic_set (&device->tasklet_scheduled, 0);
1452 INIT_LIST_HEAD(&final_queue);
1453 spin_lock_irq(get_ccwdev_lock(device->cdev));
1454 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001455 __dasd_device_check_expire(device);
1456 /* find final requests on ccw queue */
1457 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1459 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460 __dasd_device_process_final_queue(device, &final_queue);
1461 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001463 __dasd_device_start_head(device);
1464 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 dasd_put_device(device);
1466}
1467
1468/*
1469 * Schedules a call to dasd_tasklet over the device tasklet.
1470 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001471void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001474 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return;
1476 dasd_get_device(device);
1477 tasklet_hi_schedule(&device->tasklet);
1478}
1479
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001480void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1481{
1482 device->stopped |= bits;
1483}
1484EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1485
1486void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1487{
1488 device->stopped &= ~bits;
1489 if (!device->stopped)
1490 wake_up(&generic_waitq);
1491}
1492EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001495 * Queue a request to the head of the device ccw_queue.
1496 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001498void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct dasd_device *device;
1501 unsigned long flags;
1502
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001503 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001505 cqr->status = DASD_CQR_QUEUED;
1506 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001508 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1510}
1511
1512/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001513 * Queue a request to the tail of the device ccw_queue.
1514 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001516void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct dasd_device *device;
1519 unsigned long flags;
1520
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001521 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001523 cqr->status = DASD_CQR_QUEUED;
1524 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001526 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1528}
1529
1530/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001531 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001533static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001535 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1536 cqr->callback_data = DASD_SLEEPON_END_TAG;
1537 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1538 wake_up(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001541static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct dasd_device *device;
1544 int rc;
1545
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001546 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001548 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1550 return rc;
1551}
1552
1553/*
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001554 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1555 */
1556static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1557{
1558 struct dasd_device *device;
1559 dasd_erp_fn_t erp_fn;
1560
1561 if (cqr->status == DASD_CQR_FILLED)
1562 return 0;
1563 device = cqr->startdev;
1564 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1565 if (cqr->status == DASD_CQR_TERMINATED) {
1566 device->discipline->handle_terminated_request(cqr);
1567 return 1;
1568 }
1569 if (cqr->status == DASD_CQR_NEED_ERP) {
1570 erp_fn = device->discipline->erp_action(cqr);
1571 erp_fn(cqr);
1572 return 1;
1573 }
1574 if (cqr->status == DASD_CQR_FAILED)
1575 dasd_log_sense(cqr, &cqr->irb);
1576 if (cqr->refers) {
1577 __dasd_process_erp(device, cqr);
1578 return 1;
1579 }
1580 }
1581 return 0;
1582}
1583
1584static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1585{
1586 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1587 if (cqr->refers) /* erp is not done yet */
1588 return 1;
1589 return ((cqr->status != DASD_CQR_DONE) &&
1590 (cqr->status != DASD_CQR_FAILED));
1591 } else
1592 return (cqr->status == DASD_CQR_FILLED);
1593}
1594
1595static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1596{
1597 struct dasd_device *device;
1598 int rc;
1599 struct list_head ccw_queue;
1600 struct dasd_ccw_req *cqr;
1601
1602 INIT_LIST_HEAD(&ccw_queue);
1603 maincqr->status = DASD_CQR_FILLED;
1604 device = maincqr->startdev;
1605 list_add(&maincqr->blocklist, &ccw_queue);
1606 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1607 cqr = list_first_entry(&ccw_queue,
1608 struct dasd_ccw_req, blocklist)) {
1609
1610 if (__dasd_sleep_on_erp(cqr))
1611 continue;
1612 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1613 continue;
1614
1615 /* Non-temporary stop condition will trigger fail fast */
1616 if (device->stopped & ~DASD_STOPPED_PENDING &&
1617 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1618 (!dasd_eer_enabled(device))) {
1619 cqr->status = DASD_CQR_FAILED;
1620 continue;
1621 }
1622
1623 /* Don't try to start requests if device is stopped */
1624 if (interruptible) {
1625 rc = wait_event_interruptible(
1626 generic_waitq, !(device->stopped));
1627 if (rc == -ERESTARTSYS) {
1628 cqr->status = DASD_CQR_FAILED;
1629 maincqr->intrc = rc;
1630 continue;
1631 }
1632 } else
1633 wait_event(generic_waitq, !(device->stopped));
1634
1635 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001636 cqr->callback_data = DASD_SLEEPON_START_TAG;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001637 dasd_add_request_tail(cqr);
1638 if (interruptible) {
1639 rc = wait_event_interruptible(
1640 generic_waitq, _wait_for_wakeup(cqr));
1641 if (rc == -ERESTARTSYS) {
1642 dasd_cancel_req(cqr);
1643 /* wait (non-interruptible) for final status */
1644 wait_event(generic_waitq,
1645 _wait_for_wakeup(cqr));
1646 cqr->status = DASD_CQR_FAILED;
1647 maincqr->intrc = rc;
1648 continue;
1649 }
1650 } else
1651 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1652 }
1653
1654 maincqr->endclk = get_clock();
1655 if ((maincqr->status != DASD_CQR_DONE) &&
1656 (maincqr->intrc != -ERESTARTSYS))
1657 dasd_log_sense(maincqr, &maincqr->irb);
1658 if (maincqr->status == DASD_CQR_DONE)
1659 rc = 0;
1660 else if (maincqr->intrc)
1661 rc = maincqr->intrc;
1662 else
1663 rc = -EIO;
1664 return rc;
1665}
1666
1667/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001668 * Queue a request to the tail of the device ccw_queue and wait for
1669 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001671int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001673 return _dasd_sleep_on(cqr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001677 * Queue a request to the tail of the device ccw_queue and wait
1678 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001680int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001682 return _dasd_sleep_on(cqr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685/*
1686 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1687 * for eckd devices) the currently running request has to be terminated
1688 * and be put back to status queued, before the special request is added
1689 * to the head of the queue. Then the special request is waited on normally.
1690 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001691static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (list_empty(&device->ccw_queue))
1696 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001697 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001698 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001701int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct dasd_device *device;
1704 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001705
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001706 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 spin_lock_irq(get_ccwdev_lock(device->cdev));
1708 rc = _dasd_term_running_cqr(device);
1709 if (rc) {
1710 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1711 return rc;
1712 }
Horst Hummel138c0142006-06-29 14:58:12 +02001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 cqr->callback = dasd_wakeup_cb;
Stefan Weinhuber1c1e0932010-05-12 09:32:11 +02001715 cqr->callback_data = DASD_SLEEPON_START_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001717 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001720 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1723
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001724 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001725
Stefan Weinhuber6cc7f162009-06-12 10:26:39 +02001726 if (cqr->status == DASD_CQR_DONE)
1727 rc = 0;
1728 else if (cqr->intrc)
1729 rc = cqr->intrc;
1730 else
1731 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return rc;
1733}
1734
1735/*
1736 * Cancels a request that was started with dasd_sleep_on_req.
1737 * This is useful to timeout requests. The request will be
1738 * terminated if it is currently in i/o.
1739 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001740 * 0 if there was no need to terminate the request (not started yet)
1741 * negative error code if termination failed
1742 * Cancellation of a request is an asynchronous operation! The calling
1743 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001745int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001747 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 unsigned long flags;
1749 int rc;
1750
1751 rc = 0;
1752 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1753 switch (cqr->status) {
1754 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001755 /* request was not started - just set to cleared */
1756 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
1758 case DASD_CQR_IN_IO:
1759 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 rc = device->discipline->term_IO(cqr);
1761 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001762 dev_err(&device->cdev->dev,
1763 "Cancelling request %p failed with rc=%d\n",
1764 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001765 } else {
1766 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001769 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001773 dasd_schedule_device_bh(device);
1774 return rc;
1775}
1776
1777
1778/*
1779 * SECTION: Operations of the dasd_block layer.
1780 */
1781
1782/*
1783 * Timeout function for dasd_block. This is used when the block layer
1784 * is waiting for something that may not come reliably, (e.g. a state
1785 * change interrupt)
1786 */
1787static void dasd_block_timeout(unsigned long ptr)
1788{
1789 unsigned long flags;
1790 struct dasd_block *block;
1791
1792 block = (struct dasd_block *) ptr;
1793 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1794 /* re-activate request queue */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001795 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001796 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1797 dasd_schedule_block_bh(block);
1798}
1799
1800/*
1801 * Setup timeout for a dasd_block in jiffies.
1802 */
1803void dasd_block_set_timer(struct dasd_block *block, int expires)
1804{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001805 if (expires == 0)
1806 del_timer(&block->timer);
1807 else
1808 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001809}
1810
1811/*
1812 * Clear timeout for a dasd_block.
1813 */
1814void dasd_block_clear_timer(struct dasd_block *block)
1815{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001816 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001817}
1818
1819/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001820 * Process finished error recovery ccw.
1821 */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001822static void __dasd_process_erp(struct dasd_device *device,
1823 struct dasd_ccw_req *cqr)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001824{
1825 dasd_erp_fn_t erp_fn;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001826
1827 if (cqr->status == DASD_CQR_DONE)
1828 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1829 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001830 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001831 erp_fn = device->discipline->erp_postaction(cqr);
1832 erp_fn(cqr);
1833}
1834
1835/*
1836 * Fetch requests from the block device queue.
1837 */
1838static void __dasd_process_request_queue(struct dasd_block *block)
1839{
1840 struct request_queue *queue;
1841 struct request *req;
1842 struct dasd_ccw_req *cqr;
1843 struct dasd_device *basedev;
1844 unsigned long flags;
1845 queue = block->request_queue;
1846 basedev = block->base;
1847 /* No queue ? Then there is nothing to do. */
1848 if (queue == NULL)
1849 return;
1850
1851 /*
1852 * We requeue request from the block device queue to the ccw
1853 * queue only in two states. In state DASD_STATE_READY the
1854 * partition detection is done and we need to requeue requests
1855 * for that. State DASD_STATE_ONLINE is normal block device
1856 * operation.
1857 */
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001858 if (basedev->state < DASD_STATE_READY) {
1859 while ((req = blk_fetch_request(block->request_queue)))
1860 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001861 return;
Stefan Weinhuber97f604b2009-09-11 10:28:28 +02001862 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001863 /* Now we try to fetch requests from the request queue */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001864 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001865 if (basedev->features & DASD_FEATURE_READONLY &&
1866 rq_data_dir(req) == WRITE) {
1867 DBF_DEV_EVENT(DBF_ERR, basedev,
1868 "Rejecting write request %p",
1869 req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001870 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001871 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001872 continue;
1873 }
1874 cqr = basedev->discipline->build_cp(basedev, block, req);
1875 if (IS_ERR(cqr)) {
1876 if (PTR_ERR(cqr) == -EBUSY)
1877 break; /* normal end condition */
1878 if (PTR_ERR(cqr) == -ENOMEM)
1879 break; /* terminate request queue loop */
1880 if (PTR_ERR(cqr) == -EAGAIN) {
1881 /*
1882 * The current request cannot be build right
1883 * now, we have to try later. If this request
1884 * is the head-of-queue we stop the device
1885 * for 1/2 second.
1886 */
1887 if (!list_empty(&block->ccw_queue))
1888 break;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001889 spin_lock_irqsave(
1890 get_ccwdev_lock(basedev->cdev), flags);
1891 dasd_device_set_stop_bits(basedev,
1892 DASD_STOPPED_PENDING);
1893 spin_unlock_irqrestore(
1894 get_ccwdev_lock(basedev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001895 dasd_block_set_timer(block, HZ/2);
1896 break;
1897 }
1898 DBF_DEV_EVENT(DBF_ERR, basedev,
1899 "CCW creation failed (rc=%ld) "
1900 "on request %p",
1901 PTR_ERR(cqr), req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001902 blk_start_request(req);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001903 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001904 continue;
1905 }
1906 /*
1907 * Note: callback is set to dasd_return_cqr_cb in
1908 * __dasd_block_start_head to cover erp requests as well
1909 */
1910 cqr->callback_data = (void *) req;
1911 cqr->status = DASD_CQR_FILLED;
Tejun Heo9934c8c2009-05-08 11:54:16 +09001912 blk_start_request(req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001913 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1914 dasd_profile_start(block, cqr, req);
1915 }
1916}
1917
1918static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1919{
1920 struct request *req;
1921 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001922 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001923
1924 req = (struct request *) cqr->callback_data;
1925 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001926 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001927 if (status <= 0)
1928 error = status ? status : -EIO;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001929 __blk_end_request_all(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001930}
1931
1932/*
1933 * Process ccw request queue.
1934 */
1935static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1936 struct list_head *final_queue)
1937{
1938 struct list_head *l, *n;
1939 struct dasd_ccw_req *cqr;
1940 dasd_erp_fn_t erp_fn;
1941 unsigned long flags;
1942 struct dasd_device *base = block->base;
1943
1944restart:
1945 /* Process request with final status. */
1946 list_for_each_safe(l, n, &block->ccw_queue) {
1947 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1948 if (cqr->status != DASD_CQR_DONE &&
1949 cqr->status != DASD_CQR_FAILED &&
1950 cqr->status != DASD_CQR_NEED_ERP &&
1951 cqr->status != DASD_CQR_TERMINATED)
1952 continue;
1953
1954 if (cqr->status == DASD_CQR_TERMINATED) {
1955 base->discipline->handle_terminated_request(cqr);
1956 goto restart;
1957 }
1958
1959 /* Process requests that may be recovered */
1960 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001961 erp_fn = base->discipline->erp_action(cqr);
Stefan Haberland6a5176c2010-04-22 17:17:02 +02001962 if (IS_ERR(erp_fn(cqr)))
1963 continue;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001964 goto restart;
1965 }
1966
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001967 /* log sense for fatal error */
1968 if (cqr->status == DASD_CQR_FAILED) {
1969 dasd_log_sense(cqr, &cqr->irb);
1970 }
1971
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001972 /* First of all call extended error reporting. */
1973 if (dasd_eer_enabled(base) &&
1974 cqr->status == DASD_CQR_FAILED) {
1975 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1976
1977 /* restart request */
1978 cqr->status = DASD_CQR_FILLED;
1979 cqr->retries = 255;
1980 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001981 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001982 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1983 flags);
1984 goto restart;
1985 }
1986
1987 /* Process finished ERP request. */
1988 if (cqr->refers) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01001989 __dasd_process_erp(base, cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001990 goto restart;
1991 }
1992
1993 /* Rechain finished requests to final queue */
1994 cqr->endclk = get_clock();
1995 list_move_tail(&cqr->blocklist, final_queue);
1996 }
1997}
1998
1999static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2000{
2001 dasd_schedule_block_bh(cqr->block);
2002}
2003
2004static void __dasd_block_start_head(struct dasd_block *block)
2005{
2006 struct dasd_ccw_req *cqr;
2007
2008 if (list_empty(&block->ccw_queue))
2009 return;
2010 /* We allways begin with the first requests on the queue, as some
2011 * of previously started requests have to be enqueued on a
2012 * dasd_device again for error recovery.
2013 */
2014 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2015 if (cqr->status != DASD_CQR_FILLED)
2016 continue;
2017 /* Non-temporary stop condition will trigger fail fast */
2018 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2019 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2020 (!dasd_eer_enabled(block->base))) {
2021 cqr->status = DASD_CQR_FAILED;
2022 dasd_schedule_block_bh(block);
2023 continue;
2024 }
2025 /* Don't try to start requests if device is stopped */
2026 if (block->base->stopped)
2027 return;
2028
2029 /* just a fail safe check, should not happen */
2030 if (!cqr->startdev)
2031 cqr->startdev = block->base;
2032
2033 /* make sure that the requests we submit find their way back */
2034 cqr->callback = dasd_return_cqr_cb;
2035
2036 dasd_add_request_tail(cqr);
2037 }
2038}
2039
2040/*
2041 * Central dasd_block layer routine. Takes requests from the generic
2042 * block layer request queue, creates ccw requests, enqueues them on
2043 * a dasd_device and processes ccw requests that have been returned.
2044 */
2045static void dasd_block_tasklet(struct dasd_block *block)
2046{
2047 struct list_head final_queue;
2048 struct list_head *l, *n;
2049 struct dasd_ccw_req *cqr;
2050
2051 atomic_set(&block->tasklet_scheduled, 0);
2052 INIT_LIST_HEAD(&final_queue);
2053 spin_lock(&block->queue_lock);
2054 /* Finish off requests on ccw queue */
2055 __dasd_process_block_ccw_queue(block, &final_queue);
2056 spin_unlock(&block->queue_lock);
2057 /* Now call the callback function of requests with final status */
2058 spin_lock_irq(&block->request_queue_lock);
2059 list_for_each_safe(l, n, &final_queue) {
2060 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2061 list_del_init(&cqr->blocklist);
2062 __dasd_cleanup_cqr(cqr);
2063 }
2064 spin_lock(&block->queue_lock);
2065 /* Get new request from the block device request queue */
2066 __dasd_process_request_queue(block);
2067 /* Now check if the head of the ccw queue needs to be started. */
2068 __dasd_block_start_head(block);
2069 spin_unlock(&block->queue_lock);
2070 spin_unlock_irq(&block->request_queue_lock);
2071 dasd_put_device(block->base);
2072}
2073
2074static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2075{
2076 wake_up(&dasd_flush_wq);
2077}
2078
2079/*
2080 * Go through all request on the dasd_block request queue, cancel them
2081 * on the respective dasd_device, and return them to the generic
2082 * block layer.
2083 */
2084static int dasd_flush_block_queue(struct dasd_block *block)
2085{
2086 struct dasd_ccw_req *cqr, *n;
2087 int rc, i;
2088 struct list_head flush_queue;
2089
2090 INIT_LIST_HEAD(&flush_queue);
2091 spin_lock_bh(&block->queue_lock);
2092 rc = 0;
2093restart:
2094 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2095 /* if this request currently owned by a dasd_device cancel it */
2096 if (cqr->status >= DASD_CQR_QUEUED)
2097 rc = dasd_cancel_req(cqr);
2098 if (rc < 0)
2099 break;
2100 /* Rechain request (including erp chain) so it won't be
2101 * touched by the dasd_block_tasklet anymore.
2102 * Replace the callback so we notice when the request
2103 * is returned from the dasd_device layer.
2104 */
2105 cqr->callback = _dasd_wake_block_flush_cb;
2106 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2107 list_move_tail(&cqr->blocklist, &flush_queue);
2108 if (i > 1)
2109 /* moved more than one request - need to restart */
2110 goto restart;
2111 }
2112 spin_unlock_bh(&block->queue_lock);
2113 /* Now call the callback function of flushed requests */
2114restart_cb:
2115 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2116 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2117 /* Process finished ERP request. */
2118 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002119 spin_lock_bh(&block->queue_lock);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002120 __dasd_process_erp(block->base, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002121 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002122 /* restart list_for_xx loop since dasd_process_erp
2123 * might remove multiple elements */
2124 goto restart_cb;
2125 }
2126 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002127 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002128 cqr->endclk = get_clock();
2129 list_del_init(&cqr->blocklist);
2130 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01002131 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return rc;
2134}
2135
2136/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002137 * Schedules a call to dasd_tasklet over the device tasklet.
2138 */
2139void dasd_schedule_block_bh(struct dasd_block *block)
2140{
2141 /* Protect against rescheduling. */
2142 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2143 return;
2144 /* life cycle of block is bound to it's base device */
2145 dasd_get_device(block->base);
2146 tasklet_hi_schedule(&block->tasklet);
2147}
2148
2149
2150/*
2151 * SECTION: external block device operations
2152 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 */
2154
2155/*
2156 * Dasd request queue function. Called from ll_rw_blk.c
2157 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002158static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002160 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002162 block = queue->queuedata;
2163 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002165 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002167 __dasd_block_start_head(block);
2168 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/*
2172 * Allocate and initialize request queue and default I/O scheduler.
2173 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002174static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 int rc;
2177
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002178 block->request_queue = blk_init_queue(do_dasd_request,
2179 &block->request_queue_lock);
2180 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -ENOMEM;
2182
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002183 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002185 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02002186 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002187 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002189 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return rc;
2191 }
2192 return 0;
2193}
2194
2195/*
2196 * Allocate and initialize request queue.
2197 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002198static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 int max;
2201
Martin K. Petersene1defc42009-05-22 17:17:49 -04002202 blk_queue_logical_block_size(block->request_queue, block->bp_block);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002203 max = block->base->discipline->max_blocks << block->s2b_shift;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002204 blk_queue_max_hw_sectors(block->request_queue, max);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002205 blk_queue_max_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002206 /* with page sized segments we can translate each segement into
2207 * one idaw/tidaw
2208 */
2209 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2210 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
2213/*
2214 * Deactivate and free request queue.
2215 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002216static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002218 if (block->request_queue) {
2219 blk_cleanup_queue(block->request_queue);
2220 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222}
2223
2224/*
2225 * Flush request on the request queue.
2226 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002227static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 struct request *req;
2230
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002231 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002233
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002234 spin_lock_irq(&block->request_queue_lock);
Tejun Heo9934c8c2009-05-08 11:54:16 +09002235 while ((req = blk_fetch_request(block->request_queue)))
Tejun Heo40cbbb72009-04-23 11:05:19 +09002236 __blk_end_request_all(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002237 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Al Viro57a7c0b2008-03-02 10:36:08 -05002240static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Al Viro57a7c0b2008-03-02 10:36:08 -05002242 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Haberland9eb25122010-02-26 22:37:46 +01002243 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int rc;
2245
Stefan Haberland9eb25122010-02-26 22:37:46 +01002246 if (!block)
2247 return -ENODEV;
2248
2249 base = block->base;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002250 atomic_inc(&block->open_count);
2251 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 rc = -ENODEV;
2253 goto unlock;
2254 }
2255
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002256 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 rc = -EINVAL;
2258 goto unlock;
2259 }
2260
2261 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002262 dev_info(&base->cdev->dev,
2263 "Accessing the DASD failed because it is in "
2264 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 rc = -EPERM;
2266 goto out;
2267 }
2268
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002269 if (base->state <= DASD_STATE_BASIC) {
2270 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 " Cannot open unrecognized device");
2272 rc = -ENODEV;
2273 goto out;
2274 }
2275
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002276 if ((mode & FMODE_WRITE) &&
2277 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2278 (base->features & DASD_FEATURE_READONLY))) {
2279 rc = -EROFS;
2280 goto out;
2281 }
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return 0;
2284
2285out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002286 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002288 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return rc;
2290}
2291
Al Viro57a7c0b2008-03-02 10:36:08 -05002292static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002294 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002296 atomic_dec(&block->open_count);
2297 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002301/*
2302 * Return disk geometry.
2303 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002304static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002305{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002306 struct dasd_block *block;
2307 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002308
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002309 block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002310 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002311 return -ENODEV;
Julia Lawallcf05b822009-08-23 18:09:05 +02002312 base = block->base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002313
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002314 if (!base->discipline ||
2315 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002316 return -EINVAL;
2317
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002318 base->discipline->fill_geometry(block, geo);
2319 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002320 return 0;
2321}
2322
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002323const struct block_device_operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324dasd_device_operations = {
2325 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002326 .open = dasd_open,
2327 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002328 .ioctl = dasd_ioctl,
2329 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002330 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331};
2332
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002333/*******************************************************************************
2334 * end of block device operations
2335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337static void
2338dasd_exit(void)
2339{
2340#ifdef CONFIG_PROC_FS
2341 dasd_proc_exit();
2342#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002343 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002344 if (dasd_page_cache != NULL) {
2345 kmem_cache_destroy(dasd_page_cache);
2346 dasd_page_cache = NULL;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 dasd_gendisk_exit();
2349 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (dasd_debug_area != NULL) {
2351 debug_unregister(dasd_debug_area);
2352 dasd_debug_area = NULL;
2353 }
2354}
2355
2356/*
2357 * SECTION: common functions for ccw_driver use
2358 */
2359
Stefan Weinhuber33b62a32010-03-08 12:26:24 +01002360/*
2361 * Is the device read-only?
2362 * Note that this function does not report the setting of the
2363 * readonly device attribute, but how it is configured in z/VM.
2364 */
2365int dasd_device_is_ro(struct dasd_device *device)
2366{
2367 struct ccw_dev_id dev_id;
2368 struct diag210 diag_data;
2369 int rc;
2370
2371 if (!MACHINE_IS_VM)
2372 return 0;
2373 ccw_device_get_id(device->cdev, &dev_id);
2374 memset(&diag_data, 0, sizeof(diag_data));
2375 diag_data.vrdcdvno = dev_id.devno;
2376 diag_data.vrdclen = sizeof(diag_data);
2377 rc = diag210(&diag_data);
2378 if (rc == 0 || rc == 2) {
2379 return diag_data.vrdcvfla & 0x80;
2380 } else {
2381 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2382 dev_id.devno, rc);
2383 return 0;
2384 }
2385}
2386EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2387
Cornelia Huckf3445a12009-04-14 15:36:23 +02002388static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2389{
2390 struct ccw_device *cdev = data;
2391 int ret;
2392
2393 ret = ccw_device_set_online(cdev);
2394 if (ret)
2395 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2396 dev_name(&cdev->dev), ret);
Cornelia Huckf3445a12009-04-14 15:36:23 +02002397}
2398
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002399/*
2400 * Initial attempt at a probe function. this can be simplified once
2401 * the other detection code is gone.
2402 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002403int dasd_generic_probe(struct ccw_device *cdev,
2404 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 int ret;
2407
2408 ret = dasd_add_sysfs_files(cdev);
2409 if (ret) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +01002410 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2411 "dasd_generic_probe: could not add "
2412 "sysfs entries");
Horst Hummel40545572006-06-29 15:08:18 +02002413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Horst Hummel40545572006-06-29 15:08:18 +02002415 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Horst Hummel40545572006-06-29 15:08:18 +02002417 /*
2418 * Automatically online either all dasd devices (dasd_autodetect)
2419 * or all devices specified with dasd= parameters during
2420 * initial probe.
2421 */
2422 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002423 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Cornelia Huckf3445a12009-04-14 15:36:23 +02002424 async_schedule(dasd_generic_auto_online, cdev);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002428/*
2429 * This will one day be called from a global not_oper handler.
2430 * It is also used by driver_unregister during module unload.
2431 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002432void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002435 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Horst Hummel59afda72005-05-16 21:53:39 -07002437 cdev->handler = NULL;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 dasd_remove_sysfs_files(cdev);
2440 device = dasd_device_from_cdev(cdev);
2441 if (IS_ERR(device))
2442 return;
2443 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2444 /* Already doing offline processing */
2445 dasd_put_device(device);
2446 return;
2447 }
2448 /*
2449 * This device is removed unconditionally. Set offline
2450 * flag to prevent dasd_open from opening it while it is
2451 * no quite down yet.
2452 */
2453 dasd_set_target_state(device, DASD_STATE_NEW);
2454 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002455 block = device->block;
2456 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002458 /*
2459 * life cycle of block is bound to device, so delete it after
2460 * device was safely removed
2461 */
2462 if (block)
2463 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002466/*
2467 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002469 * or the user has started activation through sysfs.
2470 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002471int dasd_generic_set_online(struct ccw_device *cdev,
2472 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002474 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002476 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002477
Horst Hummel40545572006-06-29 15:08:18 +02002478 /* first online clears initial online feature flag */
2479 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 device = dasd_create_device(cdev);
2481 if (IS_ERR(device))
2482 return PTR_ERR(device);
2483
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002484 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002485 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002487 pr_warning("%s Setting the DASD online failed because "
2488 "of missing DIAG discipline\n",
2489 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 dasd_delete_device(device);
2491 return -ENODEV;
2492 }
2493 discipline = dasd_diag_discipline_pointer;
2494 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002495 if (!try_module_get(base_discipline->owner)) {
2496 dasd_delete_device(device);
2497 return -EINVAL;
2498 }
2499 if (!try_module_get(discipline->owner)) {
2500 module_put(base_discipline->owner);
2501 dasd_delete_device(device);
2502 return -EINVAL;
2503 }
2504 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 device->discipline = discipline;
2506
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002507 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 rc = discipline->check_device(device);
2509 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002510 pr_warning("%s Setting the DASD online with discipline %s "
2511 "failed with rc=%i\n",
2512 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002513 module_put(discipline->owner);
2514 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 dasd_delete_device(device);
2516 return rc;
2517 }
2518
2519 dasd_set_target_state(device, DASD_STATE_ONLINE);
2520 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002521 pr_warning("%s Setting the DASD online failed because of a "
2522 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 rc = -ENODEV;
2524 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002525 if (device->block)
2526 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dasd_delete_device(device);
2528 } else
2529 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002530 dev_name(&cdev->dev));
Stefan Haberland589c74d2010-02-26 22:37:47 +01002531
2532 wait_event(dasd_init_waitq, _wait_for_device(device));
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 dasd_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return rc;
2536}
2537
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002538int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002541 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002542 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 device = dasd_device_from_cdev(cdev);
2545 if (IS_ERR(device))
2546 return PTR_ERR(device);
2547 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2548 /* Already doing offline processing */
2549 dasd_put_device(device);
2550 return 0;
2551 }
2552 /*
2553 * We must make sure that this device is currently not in use.
2554 * The open_count is increased for every opener, that includes
2555 * the blkdev_get in dasd_scan_partitions. We are only interested
2556 * in the other openers.
2557 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002558 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002559 max_count = device->block->bdev ? 0 : -1;
2560 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002561 if (open_count > max_count) {
2562 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002563 pr_warning("%s: The DASD cannot be set offline "
2564 "with open count %i\n",
2565 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002566 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002567 pr_warning("%s: The DASD cannot be set offline "
2568 "while it is in use\n",
2569 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002570 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2571 dasd_put_device(device);
2572 return -EBUSY;
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575 dasd_set_target_state(device, DASD_STATE_NEW);
2576 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002577 block = device->block;
2578 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002580 /*
2581 * life cycle of block is bound to device, so delete it after
2582 * device was safely removed
2583 */
2584 if (block)
2585 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return 0;
2587}
2588
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002589int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct dasd_device *device;
2592 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 int ret;
2594
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002595 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (IS_ERR(device))
2597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 ret = 0;
2599 switch (event) {
2600 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002601 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002603 /* First of all call extended error reporting. */
2604 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (device->state < DASD_STATE_BASIC)
2607 break;
2608 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002609 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2610 if (cqr->status == DASD_CQR_IN_IO) {
2611 cqr->status = DASD_CQR_QUEUED;
2612 cqr->retries++;
2613 }
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002614 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002615 dasd_device_clear_timer(device);
2616 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ret = 1;
2618 break;
2619 case CIO_OPER:
2620 /* FIXME: add a sanity check. */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002621 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002622 if (device->stopped & DASD_UNRESUMED_PM) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002623 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002624 dasd_restore_device(device);
2625 ret = 1;
2626 break;
2627 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002628 dasd_schedule_device_bh(device);
2629 if (device->block)
2630 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 ret = 1;
2632 break;
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 dasd_put_device(device);
2635 return ret;
2636}
2637
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002638int dasd_generic_pm_freeze(struct ccw_device *cdev)
2639{
2640 struct dasd_ccw_req *cqr, *n;
2641 int rc;
2642 struct list_head freeze_queue;
2643 struct dasd_device *device = dasd_device_from_cdev(cdev);
2644
2645 if (IS_ERR(device))
2646 return PTR_ERR(device);
2647 /* disallow new I/O */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002648 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002649 /* clear active requests */
2650 INIT_LIST_HEAD(&freeze_queue);
2651 spin_lock_irq(get_ccwdev_lock(cdev));
2652 rc = 0;
2653 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2654 /* Check status and move request to flush_queue */
2655 if (cqr->status == DASD_CQR_IN_IO) {
2656 rc = device->discipline->term_IO(cqr);
2657 if (rc) {
2658 /* unable to terminate requeust */
2659 dev_err(&device->cdev->dev,
2660 "Unable to terminate request %p "
2661 "on suspend\n", cqr);
2662 spin_unlock_irq(get_ccwdev_lock(cdev));
2663 dasd_put_device(device);
2664 return rc;
2665 }
2666 }
2667 list_move_tail(&cqr->devlist, &freeze_queue);
2668 }
2669
2670 spin_unlock_irq(get_ccwdev_lock(cdev));
2671
2672 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2673 wait_event(dasd_flush_wq,
2674 (cqr->status != DASD_CQR_CLEAR_PENDING));
2675 if (cqr->status == DASD_CQR_CLEARED)
2676 cqr->status = DASD_CQR_QUEUED;
2677 }
2678 /* move freeze_queue to start of the ccw_queue */
2679 spin_lock_irq(get_ccwdev_lock(cdev));
2680 list_splice_tail(&freeze_queue, &device->ccw_queue);
2681 spin_unlock_irq(get_ccwdev_lock(cdev));
2682
2683 if (device->discipline->freeze)
2684 rc = device->discipline->freeze(device);
2685
2686 dasd_put_device(device);
2687 return rc;
2688}
2689EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2690
2691int dasd_generic_restore_device(struct ccw_device *cdev)
2692{
2693 struct dasd_device *device = dasd_device_from_cdev(cdev);
2694 int rc = 0;
2695
2696 if (IS_ERR(device))
2697 return PTR_ERR(device);
2698
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002699 /* allow new IO again */
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002700 dasd_device_remove_stop_bits(device,
2701 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002702
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002703 dasd_schedule_device_bh(device);
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002704
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002705 /*
2706 * call discipline restore function
2707 * if device is stopped do nothing e.g. for disconnected devices
2708 */
2709 if (device->discipline->restore && !(device->stopped))
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002710 rc = device->discipline->restore(device);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002711 if (rc || device->stopped)
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002712 /*
2713 * if the resume failed for the DASD we put it in
2714 * an UNRESUMED stop state
2715 */
2716 device->stopped |= DASD_UNRESUMED_PM;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002717
Stefan Haberland6fca97a2009-10-06 10:34:15 +02002718 if (device->block)
2719 dasd_schedule_block_bh(device->block);
2720
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002721 dasd_put_device(device);
Stefan Haberlande6125fb2009-06-22 12:08:17 +02002722 return 0;
Stefan Haberlandd41dd122009-06-16 10:30:25 +02002723}
2724EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2725
Heiko Carstens763968e2007-05-10 15:45:46 +02002726static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2727 void *rdc_buffer,
2728 int rdc_buffer_size,
Stefan Haberland68b781f2009-09-11 10:28:29 +02002729 int magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002730{
2731 struct dasd_ccw_req *cqr;
2732 struct ccw1 *ccw;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002733 unsigned long *idaw;
Cornelia Huck17283b52007-05-04 18:47:51 +02002734
2735 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2736
2737 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002738 /* internal error 13 - Allocating the RDC request failed*/
2739 dev_err(&device->cdev->dev,
2740 "An error occurred in the DASD device driver, "
2741 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002742 return cqr;
2743 }
2744
2745 ccw = cqr->cpaddr;
2746 ccw->cmd_code = CCW_CMD_RDC;
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002747 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2748 idaw = (unsigned long *) (cqr->data);
2749 ccw->cda = (__u32)(addr_t) idaw;
2750 ccw->flags = CCW_FLAG_IDA;
2751 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2752 } else {
2753 ccw->cda = (__u32)(addr_t) rdc_buffer;
2754 ccw->flags = 0;
2755 }
Cornelia Huck17283b52007-05-04 18:47:51 +02002756
Stefan Haberlandd9fa9442009-10-14 12:43:48 +02002757 ccw->count = rdc_buffer_size;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002758 cqr->startdev = device;
2759 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002760 cqr->expires = 10*HZ;
Stefan Weinhubereb6e1992009-12-07 12:51:51 +01002761 cqr->retries = 256;
Cornelia Huck17283b52007-05-04 18:47:51 +02002762 cqr->buildclk = get_clock();
2763 cqr->status = DASD_CQR_FILLED;
2764 return cqr;
2765}
2766
2767
Stefan Haberland68b781f2009-09-11 10:28:29 +02002768int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
Sebastian Ott92636b12009-06-12 10:26:37 +02002769 void *rdc_buffer, int rdc_buffer_size)
Cornelia Huck17283b52007-05-04 18:47:51 +02002770{
2771 int ret;
2772 struct dasd_ccw_req *cqr;
2773
Sebastian Ott92636b12009-06-12 10:26:37 +02002774 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
Cornelia Huck17283b52007-05-04 18:47:51 +02002775 magic);
2776 if (IS_ERR(cqr))
2777 return PTR_ERR(cqr);
2778
2779 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002780 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002781 return ret;
2782}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002783EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002784
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002785/*
2786 * In command mode and transport mode we need to look for sense
2787 * data in different places. The sense data itself is allways
2788 * an array of 32 bytes, so we can unify the sense data access
2789 * for both modes.
2790 */
2791char *dasd_get_sense(struct irb *irb)
2792{
2793 struct tsb *tsb = NULL;
2794 char *sense = NULL;
2795
2796 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2797 if (irb->scsw.tm.tcw)
2798 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2799 irb->scsw.tm.tcw);
2800 if (tsb && tsb->length == 64 && tsb->flags)
2801 switch (tsb->flags & 0x07) {
2802 case 1: /* tsa_iostat */
2803 sense = tsb->tsa.iostat.sense;
2804 break;
2805 case 2: /* tsa_ddpc */
2806 sense = tsb->tsa.ddpc.sense;
2807 break;
2808 default:
2809 /* currently we don't use interrogate data */
2810 break;
2811 }
2812 } else if (irb->esw.esw0.erw.cons) {
2813 sense = irb->ecw;
2814 }
2815 return sense;
2816}
2817EXPORT_SYMBOL_GPL(dasd_get_sense);
2818
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002819static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 int rc;
2822
2823 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002824 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002825 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002828 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (dasd_debug_area == NULL) {
2830 rc = -ENOMEM;
2831 goto failed;
2832 }
2833 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002834 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2837
2838 dasd_diag_discipline_pointer = NULL;
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 rc = dasd_devmap_init();
2841 if (rc)
2842 goto failed;
2843 rc = dasd_gendisk_init();
2844 if (rc)
2845 goto failed;
2846 rc = dasd_parse();
2847 if (rc)
2848 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002849 rc = dasd_eer_init();
2850 if (rc)
2851 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852#ifdef CONFIG_PROC_FS
2853 rc = dasd_proc_init();
2854 if (rc)
2855 goto failed;
2856#endif
2857
2858 return 0;
2859failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002860 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 dasd_exit();
2862 return rc;
2863}
2864
2865module_init(dasd_init);
2866module_exit(dasd_exit);
2867
2868EXPORT_SYMBOL(dasd_debug_area);
2869EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2870
2871EXPORT_SYMBOL(dasd_add_request_head);
2872EXPORT_SYMBOL(dasd_add_request_tail);
2873EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002874EXPORT_SYMBOL(dasd_device_clear_timer);
2875EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876EXPORT_SYMBOL(dasd_enable_device);
2877EXPORT_SYMBOL(dasd_int_handler);
2878EXPORT_SYMBOL(dasd_kfree_request);
2879EXPORT_SYMBOL(dasd_kick_device);
2880EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002881EXPORT_SYMBOL(dasd_schedule_device_bh);
2882EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002884EXPORT_SYMBOL(dasd_device_set_timer);
2885EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886EXPORT_SYMBOL(dasd_sfree_request);
2887EXPORT_SYMBOL(dasd_sleep_on);
2888EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2889EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2890EXPORT_SYMBOL(dasd_smalloc_request);
2891EXPORT_SYMBOL(dasd_start_IO);
2892EXPORT_SYMBOL(dasd_term_IO);
2893
2894EXPORT_SYMBOL_GPL(dasd_generic_probe);
2895EXPORT_SYMBOL_GPL(dasd_generic_remove);
2896EXPORT_SYMBOL_GPL(dasd_generic_notify);
2897EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2898EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002899EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2900EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2901EXPORT_SYMBOL_GPL(dasd_alloc_block);
2902EXPORT_SYMBOL_GPL(dasd_free_block);