blob: 00f7d24b337a92d8d284e379fd9f7254509b2c6b [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>
8 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kmod.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ctype.h>
16#include <linux/major.h>
17#include <linux/slab.h>
18#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/ebcdic.h>
23#include <asm/idals.h>
24#include <asm/todclk.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010025#include <asm/itcw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* This is ugly... */
28#define PRINTK_HEADER "dasd:"
29
30#include "dasd_int.h"
31/*
32 * SECTION: Constant definitions to be used within this file
33 */
34#define DASD_CHANQ_MAX_SIZE 4
35
36/*
37 * SECTION: exported variables of dasd.c
38 */
39debug_info_t *dasd_debug_area;
40struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010041void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
44MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
45 " Copyright 2000 IBM Corporation");
46MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047MODULE_LICENSE("GPL");
48
49/*
50 * SECTION: prototypes for static functions of dasd.c
51 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010052static int dasd_alloc_queue(struct dasd_block *);
53static void dasd_setup_queue(struct dasd_block *);
54static void dasd_free_queue(struct dasd_block *);
55static void dasd_flush_request_queue(struct dasd_block *);
56static int dasd_flush_block_queue(struct dasd_block *);
57static void dasd_device_tasklet(struct dasd_device *);
58static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000059static void do_kick_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010060static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010061static void dasd_device_timeout(unsigned long);
62static void dasd_block_timeout(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * SECTION: Operations on the device structure.
66 */
67static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020068static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020069static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Allocate memory for a new device structure.
73 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010074struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct dasd_device *device;
77
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010078 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
79 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* Get two pages for normal block device operations. */
83 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010084 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 kfree(device);
86 return ERR_PTR(-ENOMEM);
87 }
88 /* Get one page for error recovery. */
89 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 free_pages((unsigned long) device->ccw_mem, 1);
92 kfree(device);
93 return ERR_PTR(-ENOMEM);
94 }
95
96 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
97 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
98 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010099 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200100 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100101 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 (unsigned long) device);
103 INIT_LIST_HEAD(&device->ccw_queue);
104 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100105 device->timer.function = dasd_device_timeout;
106 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000107 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 device->state = DASD_STATE_NEW;
109 device->target = DASD_STATE_NEW;
110
111 return device;
112}
113
114/*
115 * Free memory of a device structure.
116 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100117void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800119 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 free_page((unsigned long) device->erp_mem);
121 free_pages((unsigned long) device->ccw_mem, 1);
122 kfree(device);
123}
124
125/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100126 * Allocate memory for a new device structure.
127 */
128struct dasd_block *dasd_alloc_block(void)
129{
130 struct dasd_block *block;
131
132 block = kzalloc(sizeof(*block), GFP_ATOMIC);
133 if (!block)
134 return ERR_PTR(-ENOMEM);
135 /* open_count = 0 means device online but not in use */
136 atomic_set(&block->open_count, -1);
137
138 spin_lock_init(&block->request_queue_lock);
139 atomic_set(&block->tasklet_scheduled, 0);
140 tasklet_init(&block->tasklet,
141 (void (*)(unsigned long)) dasd_block_tasklet,
142 (unsigned long) block);
143 INIT_LIST_HEAD(&block->ccw_queue);
144 spin_lock_init(&block->queue_lock);
145 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100146 block->timer.function = dasd_block_timeout;
147 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100148
149 return block;
150}
151
152/*
153 * Free memory of a device structure.
154 */
155void dasd_free_block(struct dasd_block *block)
156{
157 kfree(block);
158}
159
160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * Make a new device known to the system.
162 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100163static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int rc;
166
167 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200168 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * keep the reference count > 0.
170 */
171 dasd_get_device(device);
172
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100173 if (device->block) {
174 rc = dasd_alloc_queue(device->block);
175 if (rc) {
176 dasd_put_device(device);
177 return rc;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 device->state = DASD_STATE_KNOWN;
181 return 0;
182}
183
184/*
185 * Let the system forget about a device.
186 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800189 /* Disable extended error reporting for this device. */
190 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100192 if (device->discipline) {
193 if (device->discipline->uncheck_device)
194 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800195 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800198 if (device->base_discipline)
199 module_put(device->base_discipline->owner);
200 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 device->state = DASD_STATE_NEW;
202
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100203 if (device->block)
204 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Give up reference we took in dasd_state_new_to_known. */
207 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/*
212 * Request the irq line for the device.
213 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100214static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int rc;
217
218 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 if (device->block) {
220 rc = dasd_gendisk_alloc(device->block);
221 if (rc)
222 return rc;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Kay Sievers2a0217d2008-10-10 21:33:09 +0200225 device->debug_area = debug_register(dev_name(&device->cdev->dev), 1, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100226 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200228 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
230
231 device->state = DASD_STATE_BASIC;
232 return 0;
233}
234
235/*
236 * Release the irq line for the device. Terminate any running i/o.
237 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100238static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Horst Hummel8f617012006-08-30 14:33:33 +0200240 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241 if (device->block) {
242 dasd_gendisk_free(device->block);
243 dasd_block_clear_timer(device->block);
244 }
245 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200246 if (rc)
247 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100248 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
251 if (device->debug_area != NULL) {
252 debug_unregister(device->debug_area);
253 device->debug_area = NULL;
254 }
255 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/*
260 * Do the initial analysis. The do_analysis function may return
261 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
262 * until the discipline decides to continue the startup sequence
263 * by calling the function dasd_change_state. The eckd disciplines
264 * uses this to start a ccw that detects the format. The completion
265 * interrupt for this detection ccw uses the kernel event daemon to
266 * trigger the call to dasd_change_state. All this is done in the
267 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800268 * After the analysis ccw is done (do_analysis returned 0) the block
269 * device is setup.
270 * In case the analysis returns an error, the device setup is stopped
271 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100273static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100276 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800280 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100281 if (block) {
282 if (block->base->discipline->do_analysis != NULL)
283 rc = block->base->discipline->do_analysis(block);
284 if (rc) {
285 if (rc != -EAGAIN)
286 device->state = DASD_STATE_UNFMT;
287 return rc;
288 }
289 dasd_setup_queue(block);
290 set_capacity(block->gdp,
291 block->blocks << block->s2b_shift);
292 device->state = DASD_STATE_READY;
293 rc = dasd_scan_partitions(block);
294 if (rc)
295 device->state = DASD_STATE_BASIC;
296 } else {
297 device->state = DASD_STATE_READY;
298 }
Horst Hummel90f00942006-03-07 21:55:39 -0800299 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
303 * Remove device from block device layer. Destroy dirty buffers.
304 * Forget format information. Check if the target level is basic
305 * and if it is create fake disk for formatting.
306 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100307static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Horst Hummel8f617012006-08-30 14:33:33 +0200309 int rc;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100312 if (device->block) {
313 struct dasd_block *block = device->block;
314 rc = dasd_flush_block_queue(block);
315 if (rc) {
316 device->state = DASD_STATE_READY;
317 return rc;
318 }
319 dasd_destroy_partitions(block);
320 dasd_flush_request_queue(block);
321 block->blocks = 0;
322 block->bp_block = 0;
323 block->s2b_shift = 0;
324 }
Horst Hummel8f617012006-08-30 14:33:33 +0200325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
Horst Hummel90f00942006-03-07 21:55:39 -0800329 * Back to basic.
330 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800332{
333 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200334 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800335}
336
337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * Make the device online and schedule the bottom half to start
339 * the requeueing of requests from the linux request queue to the
340 * ccw queue.
341 */
Horst Hummel8f617012006-08-30 14:33:33 +0200342static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343dasd_state_ready_to_online(struct dasd_device * device)
344{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100345 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100346 struct gendisk *disk;
347 struct disk_part_iter piter;
348 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100349
350 if (device->discipline->ready_to_online) {
351 rc = device->discipline->ready_to_online(device);
352 if (rc)
353 return rc;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100356 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100357 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100358 disk = device->block->bdev->bd_disk;
359 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
360 while ((part = disk_part_iter_next(&piter)))
361 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
362 disk_part_iter_exit(&piter);
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
367/*
368 * Stop the requeueing of requests again.
369 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100372 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100373 struct gendisk *disk;
374 struct disk_part_iter piter;
375 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100376
377 if (device->discipline->online_to_ready) {
378 rc = device->discipline->online_to_ready(device);
379 if (rc)
380 return rc;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100383 if (device->block) {
384 disk = device->block->bdev->bd_disk;
385 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
386 while ((part = disk_part_iter_next(&piter)))
387 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
388 disk_part_iter_exit(&piter);
389 }
Horst Hummel8f617012006-08-30 14:33:33 +0200390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
394 * Device startup state changes.
395 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100396static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 int rc;
399
400 rc = 0;
401 if (device->state == DASD_STATE_NEW &&
402 device->target >= DASD_STATE_KNOWN)
403 rc = dasd_state_new_to_known(device);
404
405 if (!rc &&
406 device->state == DASD_STATE_KNOWN &&
407 device->target >= DASD_STATE_BASIC)
408 rc = dasd_state_known_to_basic(device);
409
410 if (!rc &&
411 device->state == DASD_STATE_BASIC &&
412 device->target >= DASD_STATE_READY)
413 rc = dasd_state_basic_to_ready(device);
414
415 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700416 device->state == DASD_STATE_UNFMT &&
417 device->target > DASD_STATE_UNFMT)
418 rc = -EPERM;
419
420 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 device->state == DASD_STATE_READY &&
422 device->target >= DASD_STATE_ONLINE)
423 rc = dasd_state_ready_to_online(device);
424
425 return rc;
426}
427
428/*
429 * Device shutdown state changes.
430 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100431static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Horst Hummel8f617012006-08-30 14:33:33 +0200433 int rc;
434
435 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (device->state == DASD_STATE_ONLINE &&
437 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200438 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200439
Horst Hummel8f617012006-08-30 14:33:33 +0200440 if (!rc &&
441 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200443 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800444
Horst Hummel8f617012006-08-30 14:33:33 +0200445 if (!rc &&
446 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800447 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200448 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800449
Horst Hummel8f617012006-08-30 14:33:33 +0200450 if (!rc &&
451 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200453 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200454
Horst Hummel8f617012006-08-30 14:33:33 +0200455 if (!rc &&
456 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200458 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Horst Hummel8f617012006-08-30 14:33:33 +0200460 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * This is the main startup/shutdown routine.
465 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100466static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int rc;
469
470 if (device->state == device->target)
471 /* Already where we want to go today... */
472 return;
473 if (device->state < device->target)
474 rc = dasd_increase_state(device);
475 else
476 rc = dasd_decrease_state(device);
477 if (rc && rc != -EAGAIN)
478 device->target = device->state;
479
480 if (device->state == device->target)
481 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200482
483 /* let user-space know that the device status changed */
484 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487/*
488 * Kick starter for devices that did not complete the startup/shutdown
489 * procedure or were sleeping because of a pending state.
490 * dasd_kick_device will schedule a call do do_kick_device to the kernel
491 * event daemon.
492 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100493static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Al Viro4927b3f2006-12-06 19:18:20 +0000495 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100497 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dasd_put_device(device);
499}
500
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100501void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 dasd_get_device(device);
504 /* queue call to dasd_kick_device to the kernel event daemon. */
505 schedule_work(&device->kick_work);
506}
507
508/*
509 * Set the target state for a device and starts the state change.
510 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100511void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 /* If we are in probeonly mode stop at DASD_STATE_READY. */
514 if (dasd_probeonly && target > DASD_STATE_READY)
515 target = DASD_STATE_READY;
516 if (device->target != target) {
517 if (device->state == target)
518 wake_up(&dasd_init_waitq);
519 device->target = target;
520 }
521 if (device->state != device->target)
522 dasd_change_state(device);
523}
524
525/*
526 * Enable devices with device numbers in [from..to].
527 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100528static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 return (device->state == device->target);
531}
532
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100533void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 dasd_set_target_state(device, DASD_STATE_ONLINE);
536 if (device->state <= DASD_STATE_KNOWN)
537 /* No discipline for device found. */
538 dasd_set_target_state(device, DASD_STATE_NEW);
539 /* Now wait for the devices to come up. */
540 wait_event(dasd_init_waitq, _wait_for_device(device));
541}
542
543/*
544 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
545 */
546#ifdef CONFIG_DASD_PROFILE
547
548struct dasd_profile_info_t dasd_global_profile;
549unsigned int dasd_profile_level = DASD_PROFILE_OFF;
550
551/*
552 * Increments counter in global and local profiling structures.
553 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100554#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{ \
556 int index; \
557 for (index = 0; index < 31 && value >> (2+index); index++); \
558 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100559 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Add profiling information for cqr before execution.
564 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100565static void dasd_profile_start(struct dasd_block *block,
566 struct dasd_ccw_req *cqr,
567 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct list_head *l;
570 unsigned int counter;
571
572 if (dasd_profile_level != DASD_PROFILE_ON)
573 return;
574
575 /* count the length of the chanq for statistics */
576 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100577 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (++counter >= 31)
579 break;
580 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100581 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/*
585 * Add profiling information for cqr after execution.
586 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100587static void dasd_profile_end(struct dasd_block *block,
588 struct dasd_ccw_req *cqr,
589 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 long strtime, irqtime, endtime, tottime; /* in microseconds */
592 long tottimeps, sectors;
593
594 if (dasd_profile_level != DASD_PROFILE_ON)
595 return;
596
597 sectors = req->nr_sectors;
598 if (!cqr->buildclk || !cqr->startclk ||
599 !cqr->stopclk || !cqr->endclk ||
600 !sectors)
601 return;
602
603 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
604 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
605 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
606 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
607 tottimeps = tottime / sectors;
608
609 if (!dasd_global_profile.dasd_io_reqs)
610 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100611 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 dasd_global_profile.dasd_io_reqs++;
613 dasd_global_profile.dasd_io_sects += sectors;
614
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100615 if (!block->profile.dasd_io_reqs)
616 memset(&block->profile, 0,
617 sizeof(struct dasd_profile_info_t));
618 block->profile.dasd_io_reqs++;
619 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100621 dasd_profile_counter(sectors, dasd_io_secs, block);
622 dasd_profile_counter(tottime, dasd_io_times, block);
623 dasd_profile_counter(tottimeps, dasd_io_timps, block);
624 dasd_profile_counter(strtime, dasd_io_time1, block);
625 dasd_profile_counter(irqtime, dasd_io_time2, block);
626 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
627 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100630#define dasd_profile_start(block, cqr, req) do {} while (0)
631#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#endif /* CONFIG_DASD_PROFILE */
633
634/*
635 * Allocate memory for a channel program with 'cplength' channel
636 * command words and 'datasize' additional space. There are two
637 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
638 * memory and 2) dasd_smalloc_request uses the static ccw memory
639 * that gets allocated for each device.
640 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100641struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
642 int datasize,
643 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 struct dasd_ccw_req *cqr;
646
647 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100648 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
649 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800651 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (cqr == NULL)
653 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 cqr->cpaddr = NULL;
655 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800656 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 GFP_ATOMIC | GFP_DMA);
658 if (cqr->cpaddr == NULL) {
659 kfree(cqr);
660 return ERR_PTR(-ENOMEM);
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 cqr->data = NULL;
664 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800665 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800667 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 kfree(cqr);
669 return ERR_PTR(-ENOMEM);
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 strncpy((char *) &cqr->magic, magic, 4);
673 ASCEBC((char *) &cqr->magic, 4);
674 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
675 dasd_get_device(device);
676 return cqr;
677}
678
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100679struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
680 int datasize,
681 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 unsigned long flags;
684 struct dasd_ccw_req *cqr;
685 char *data;
686 int size;
687
688 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100689 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
690 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
693 if (cplength > 0)
694 size += cplength * sizeof(struct ccw1);
695 if (datasize > 0)
696 size += datasize;
697 spin_lock_irqsave(&device->mem_lock, flags);
698 cqr = (struct dasd_ccw_req *)
699 dasd_alloc_chunk(&device->ccw_chunks, size);
700 spin_unlock_irqrestore(&device->mem_lock, flags);
701 if (cqr == NULL)
702 return ERR_PTR(-ENOMEM);
703 memset(cqr, 0, sizeof(struct dasd_ccw_req));
704 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
705 cqr->cpaddr = NULL;
706 if (cplength > 0) {
707 cqr->cpaddr = (struct ccw1 *) data;
708 data += cplength*sizeof(struct ccw1);
709 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
710 }
711 cqr->data = NULL;
712 if (datasize > 0) {
713 cqr->data = data;
714 memset(cqr->data, 0, datasize);
715 }
716 strncpy((char *) &cqr->magic, magic, 4);
717 ASCEBC((char *) &cqr->magic, 4);
718 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
719 dasd_get_device(device);
720 return cqr;
721}
722
723/*
724 * Free memory of a channel program. This function needs to free all the
725 * idal lists that might have been created by dasd_set_cda and the
726 * struct dasd_ccw_req itself.
727 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100728void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800730#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct ccw1 *ccw;
732
733 /* Clear any idals used for the request. */
734 ccw = cqr->cpaddr;
735 do {
736 clear_normalized_cda(ccw);
737 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
738#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800739 kfree(cqr->cpaddr);
740 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 kfree(cqr);
742 dasd_put_device(device);
743}
744
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100745void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 unsigned long flags;
748
749 spin_lock_irqsave(&device->mem_lock, flags);
750 dasd_free_chunk(&device->ccw_chunks, cqr);
751 spin_unlock_irqrestore(&device->mem_lock, flags);
752 dasd_put_device(device);
753}
754
755/*
756 * Check discipline magic in cqr.
757 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100758static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct dasd_device *device;
761
762 if (cqr == NULL)
763 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100764 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
766 DEV_MESSAGE(KERN_WARNING, device,
767 " dasd_ccw_req 0x%08x magic doesn't match"
768 " discipline 0x%08x",
769 cqr->magic,
770 *(unsigned int *) device->discipline->name);
771 return -EINVAL;
772 }
773 return 0;
774}
775
776/*
777 * Terminate the current i/o and set the request to clear_pending.
778 * Timer keeps device runnig.
779 * ccw_device_clear can fail if the i/o subsystem
780 * is in a bad mood.
781 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100782int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct dasd_device *device;
785 int retries, rc;
786
787 /* Check the cqr */
788 rc = dasd_check_cqr(cqr);
789 if (rc)
790 return rc;
791 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100792 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
794 rc = ccw_device_clear(device->cdev, (long) cqr);
795 switch (rc) {
796 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800797 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100798 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200800 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 DBF_DEV_EVENT(DBF_DEBUG, device,
802 "terminate cqr %p successful",
803 cqr);
804 break;
805 case -ENODEV:
806 DBF_DEV_EVENT(DBF_ERR, device, "%s",
807 "device gone, retry");
808 break;
809 case -EIO:
810 DBF_DEV_EVENT(DBF_ERR, device, "%s",
811 "I/O error, retry");
812 break;
813 case -EINVAL:
814 case -EBUSY:
815 DBF_DEV_EVENT(DBF_ERR, device, "%s",
816 "device busy, retry later");
817 break;
818 default:
819 DEV_MESSAGE(KERN_ERR, device,
820 "line %d unknown RC=%d, please "
821 "report to linux390@de.ibm.com",
822 __LINE__, rc);
823 BUG();
824 break;
825 }
826 retries++;
827 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100828 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return rc;
830}
831
832/*
833 * Start the i/o. This start_IO can fail if the channel is really busy.
834 * In that case set up a timer to start the request later.
835 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100836int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct dasd_device *device;
839 int rc;
840
841 /* Check the cqr */
842 rc = dasd_check_cqr(cqr);
843 if (rc)
844 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100845 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (cqr->retries < 0) {
847 DEV_MESSAGE(KERN_DEBUG, device,
848 "start_IO: request %p (%02x/%i) - no retry left.",
849 cqr, cqr->status, cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100850 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EIO;
852 }
853 cqr->startclk = get_clock();
854 cqr->starttime = jiffies;
855 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100856 if (cqr->cpmode == 1) {
857 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
858 (long) cqr, cqr->lpm);
859 } else {
860 rc = ccw_device_start(device->cdev, cqr->cpaddr,
861 (long) cqr, cqr->lpm, 0);
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 switch (rc) {
864 case 0:
865 cqr->status = DASD_CQR_IN_IO;
866 DBF_DEV_EVENT(DBF_DEBUG, device,
867 "start_IO: request %p started successful",
868 cqr);
869 break;
870 case -EBUSY:
871 DBF_DEV_EVENT(DBF_ERR, device, "%s",
872 "start_IO: device busy, retry later");
873 break;
874 case -ETIMEDOUT:
875 DBF_DEV_EVENT(DBF_ERR, device, "%s",
876 "start_IO: request timeout, retry later");
877 break;
878 case -EACCES:
879 /* -EACCES indicates that the request used only a
880 * subset of the available pathes and all these
881 * pathes are gone.
882 * Do a retry with all available pathes.
883 */
884 cqr->lpm = LPM_ANYPATH;
885 DBF_DEV_EVENT(DBF_ERR, device, "%s",
886 "start_IO: selected pathes gone,"
887 " retry on all pathes");
888 break;
889 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100890 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
891 "start_IO: -ENODEV device gone, retry");
892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 case -EIO:
894 DBF_DEV_EVENT(DBF_ERR, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100895 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 default:
898 DEV_MESSAGE(KERN_ERR, device,
899 "line %d unknown RC=%d, please report"
900 " to linux390@de.ibm.com", __LINE__, rc);
901 BUG();
902 break;
903 }
904 return rc;
905}
906
907/*
908 * Timeout function for dasd devices. This is used for different purposes
909 * 1) missing interrupt handler for normal operation
910 * 2) delayed start of request where start_IO failed with -EBUSY
911 * 3) timeout for missing state change interrupts
912 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
913 * DASD_CQR_QUEUED for 2) and 3).
914 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100915static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 unsigned long flags;
918 struct dasd_device *device;
919
920 device = (struct dasd_device *) ptr;
921 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
922 /* re-activate request queue */
923 device->stopped &= ~DASD_STOPPED_PENDING;
924 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100925 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/*
929 * Setup timeout for a device in jiffies.
930 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100931void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100933 if (expires == 0)
934 del_timer(&device->timer);
935 else
936 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*
940 * Clear timeout for a device.
941 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100942void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100944 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100947static void dasd_handle_killed_request(struct ccw_device *cdev,
948 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct dasd_ccw_req *cqr;
951 struct dasd_device *device;
952
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200953 if (!intparm)
954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 cqr = (struct dasd_ccw_req *) intparm;
956 if (cqr->status != DASD_CQR_IN_IO) {
957 MESSAGE(KERN_DEBUG,
958 "invalid status in handle_killed_request: "
959 "bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200960 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return;
962 }
963
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100964 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200966 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
968 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200969 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971 }
972
973 /* Schedule request to be retried. */
974 cqr->status = DASD_CQR_QUEUED;
975
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100976 dasd_device_clear_timer(device);
977 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 dasd_put_device(device);
979}
980
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100981void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800983 /* First of all start sense subsystem status request. */
984 dasd_eer_snss(device);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100987 dasd_schedule_device_bh(device);
988 if (device->block)
989 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
993 * Interrupt handler for "normal" ssch-io based dasd devices.
994 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100995void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
996 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct dasd_ccw_req *cqr, *next;
999 struct dasd_device *device;
1000 unsigned long long now;
1001 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (IS_ERR(irb)) {
1004 switch (PTR_ERR(irb)) {
1005 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 case -ETIMEDOUT:
1008 printk(KERN_WARNING"%s(%s): request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001009 __func__, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
1011 default:
1012 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001013 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001015 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return;
1017 }
1018
1019 now = get_clock();
1020
1021 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001022 dev_name(&cdev->dev), ((irb->scsw.cmd.cstat << 8) |
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001023 irb->scsw.cmd.dstat), (unsigned int) intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001025 /* check for unsolicited interrupts */
1026 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001027 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1028 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1029 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001030 if (cqr && cqr->status == DASD_CQR_IN_IO)
1031 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001032 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001034 dasd_device_clear_timer(device);
1035 device->discipline->handle_unsolicited_interrupt(device,
1036 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dasd_put_device(device);
1038 }
1039 return;
1040 }
1041
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001042 device = (struct dasd_device *) cqr->startdev;
1043 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1045 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001046 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return;
1048 }
1049
1050 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001051 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001052 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001053 cqr->status = DASD_CQR_CLEARED;
1054 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001055 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001056 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return;
1058 }
1059
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001060 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (cqr->status != DASD_CQR_IN_IO) {
1062 MESSAGE(KERN_DEBUG,
1063 "invalid status: bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001064 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return;
1066 }
1067 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001068 ((irb->scsw.cmd.cstat << 8) | irb->scsw.cmd.dstat), cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001069 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001071 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1072 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001073 /* request was completed successfully */
1074 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 cqr->stopclk = now;
1076 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001077 if (cqr->devlist.next != &device->ccw_queue) {
1078 next = list_entry(cqr->devlist.next,
1079 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001081 } else { /* error */
1082 memcpy(&cqr->irb, irb, sizeof(struct irb));
Horst Hummel9575bf22006-12-08 15:54:15 +01001083 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001084 dasd_log_sense(cqr, irb);
1085 }
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001086 /*
1087 * If we don't want complex ERP for this request, then just
1088 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001089 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001090 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001091 cqr->retries > 0) {
1092 DEV_MESSAGE(KERN_DEBUG, device,
1093 "default ERP in fastpath (%i retries left)",
1094 cqr->retries);
1095 cqr->lpm = LPM_ANYPATH;
1096 cqr->status = DASD_CQR_QUEUED;
1097 next = cqr;
1098 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001100 }
1101 if (next && (next->status == DASD_CQR_QUEUED) &&
1102 (!device->stopped)) {
1103 if (device->discipline->start_IO(next) == 0)
1104 expires = next->expires;
1105 else
1106 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1107 "Interrupt fastpath "
1108 "failed!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001111 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001113 dasd_device_clear_timer(device);
1114 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001118 * If we have an error on a dasd_block layer request then we cancel
1119 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001121static void __dasd_device_recovery(struct dasd_device *device,
1122 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct list_head *l, *n;
1125 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001128 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001130 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001132
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001133 list_for_each_safe(l, n, &device->ccw_queue) {
1134 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1135 if (cqr->status == DASD_CQR_QUEUED &&
1136 ref_cqr->block == cqr->block) {
1137 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001139 }
1140};
1141
1142/*
1143 * Remove those ccw requests from the queue that need to be returned
1144 * to the upper layer.
1145 */
1146static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1147 struct list_head *final_queue)
1148{
1149 struct list_head *l, *n;
1150 struct dasd_ccw_req *cqr;
1151
1152 /* Process request with final status. */
1153 list_for_each_safe(l, n, &device->ccw_queue) {
1154 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1155
1156 /* Stop list processing at the first non-final request. */
1157 if (cqr->status == DASD_CQR_QUEUED ||
1158 cqr->status == DASD_CQR_IN_IO ||
1159 cqr->status == DASD_CQR_CLEAR_PENDING)
1160 break;
1161 if (cqr->status == DASD_CQR_ERROR) {
1162 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001164 /* Rechain finished requests to final queue */
1165 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167}
1168
1169/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001170 * the cqrs from the final queue are returned to the upper layer
1171 * by setting a dasd_block state and calling the callback function
1172 */
1173static void __dasd_device_process_final_queue(struct dasd_device *device,
1174 struct list_head *final_queue)
1175{
1176 struct list_head *l, *n;
1177 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001178 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001179 void (*callback)(struct dasd_ccw_req *, void *data);
1180 void *callback_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001181
1182 list_for_each_safe(l, n, final_queue) {
1183 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1184 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001185 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001186 callback = cqr->callback;
1187 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001188 if (block)
1189 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001190 switch (cqr->status) {
1191 case DASD_CQR_SUCCESS:
1192 cqr->status = DASD_CQR_DONE;
1193 break;
1194 case DASD_CQR_ERROR:
1195 cqr->status = DASD_CQR_NEED_ERP;
1196 break;
1197 case DASD_CQR_CLEARED:
1198 cqr->status = DASD_CQR_TERMINATED;
1199 break;
1200 default:
1201 DEV_MESSAGE(KERN_ERR, device,
1202 "wrong cqr status in __dasd_process_final_queue "
1203 "for cqr %p, status %x",
1204 cqr, cqr->status);
1205 BUG();
1206 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001207 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001208 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001209 if (block)
1210 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001211 }
1212}
1213
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * Take a look at the first request on the ccw queue and check
1216 * if it reached its expire time. If so, terminate the IO.
1217 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001218static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct dasd_ccw_req *cqr;
1221
1222 if (list_empty(&device->ccw_queue))
1223 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001224 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001225 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1226 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1227 if (device->discipline->term_IO(cqr) != 0) {
1228 /* Hmpf, try again in 5 sec */
Horst Hummel29145a62006-12-04 15:40:15 +01001229 DEV_MESSAGE(KERN_ERR, device,
1230 "internal error - timeout (%is) expired "
1231 "for cqr %p, termination failed, "
1232 "retrying in 5s",
1233 (cqr->expires/HZ), cqr);
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001234 cqr->expires += 5*HZ;
1235 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001236 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001237 DEV_MESSAGE(KERN_ERR, device,
1238 "internal error - timeout (%is) expired "
1239 "for cqr %p (%i retries left)",
1240 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 }
1243}
1244
1245/*
1246 * Take a look at the first request on the ccw queue and check
1247 * if it needs to be started.
1248 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001249static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct dasd_ccw_req *cqr;
1252 int rc;
1253
1254 if (list_empty(&device->ccw_queue))
1255 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001256 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001257 if (cqr->status != DASD_CQR_QUEUED)
1258 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001259 /* when device is stopped, return request to previous layer */
1260 if (device->stopped) {
1261 cqr->status = DASD_CQR_CLEARED;
1262 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001263 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001264 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001265
1266 rc = device->discipline->start_IO(cqr);
1267 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001268 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001269 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001270 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001271 } else
1272 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001273 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277 * Go through all request on the dasd_device request queue,
1278 * terminate them on the cdev if necessary, and return them to the
1279 * submitting layer via callback.
1280 * Note:
1281 * Make sure that all 'submitting layers' still exist when
1282 * this function is called!. In other words, when 'device' is a base
1283 * device then all block layer requests must have been removed before
1284 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001286int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001288 struct dasd_ccw_req *cqr, *n;
1289 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 INIT_LIST_HEAD(&flush_queue);
1293 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001294 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001295 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001296 /* Check status and move request to flush_queue */
1297 switch (cqr->status) {
1298 case DASD_CQR_IN_IO:
1299 rc = device->discipline->term_IO(cqr);
1300 if (rc) {
1301 /* unable to terminate requeust */
1302 DEV_MESSAGE(KERN_ERR, device,
1303 "dasd flush ccw_queue is unable "
1304 " to terminate request %p",
1305 cqr);
1306 /* stop flush processing */
1307 goto finished;
1308 }
1309 break;
1310 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001312 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001313 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001314 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001315 break;
1316 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001317 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001318 }
Horst Hummel8f617012006-08-30 14:33:33 +02001319finished:
1320 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001321 /*
1322 * After this point all requests must be in state CLEAR_PENDING,
1323 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1324 * one of the others.
1325 */
1326 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1327 wait_event(dasd_flush_wq,
1328 (cqr->status != DASD_CQR_CLEAR_PENDING));
1329 /*
1330 * Now set each request back to TERMINATED, DONE or NEED_ERP
1331 * and call the callback function of flushed requests
1332 */
1333 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001334 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337/*
1338 * Acquire the device lock and process queues for the device.
1339 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001340static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 atomic_set (&device->tasklet_scheduled, 0);
1345 INIT_LIST_HEAD(&final_queue);
1346 spin_lock_irq(get_ccwdev_lock(device->cdev));
1347 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001348 __dasd_device_check_expire(device);
1349 /* find final requests on ccw queue */
1350 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1352 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001353 __dasd_device_process_final_queue(device, &final_queue);
1354 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356 __dasd_device_start_head(device);
1357 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 dasd_put_device(device);
1359}
1360
1361/*
1362 * Schedules a call to dasd_tasklet over the device tasklet.
1363 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001364void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001367 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return;
1369 dasd_get_device(device);
1370 tasklet_hi_schedule(&device->tasklet);
1371}
1372
1373/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001374 * Queue a request to the head of the device ccw_queue.
1375 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001377void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct dasd_device *device;
1380 unsigned long flags;
1381
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001382 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001384 cqr->status = DASD_CQR_QUEUED;
1385 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1389}
1390
1391/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001392 * Queue a request to the tail of the device ccw_queue.
1393 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001395void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct dasd_device *device;
1398 unsigned long flags;
1399
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001400 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001402 cqr->status = DASD_CQR_QUEUED;
1403 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001405 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1407}
1408
1409/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001410 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001412static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 wake_up((wait_queue_head_t *) data);
1415}
1416
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001417static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct dasd_device *device;
1420 int rc;
1421
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001422 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001424 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001425 cqr->status == DASD_CQR_NEED_ERP ||
1426 cqr->status == DASD_CQR_TERMINATED) &&
1427 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1429 return rc;
1430}
1431
1432/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001433 * Queue a request to the tail of the device ccw_queue and wait for
1434 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct dasd_device *device;
1439 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001440
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001441 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001444 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001445 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001446 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001449 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return rc;
1451}
1452
1453/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001454 * Queue a request to the tail of the device ccw_queue and wait
1455 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001457int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001462 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001464 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001465 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001466 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001467 if (rc == -ERESTARTSYS) {
1468 dasd_cancel_req(cqr);
1469 /* wait (non-interruptible) for final status */
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001470 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001472 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return rc;
1474}
1475
1476/*
1477 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1478 * for eckd devices) the currently running request has to be terminated
1479 * and be put back to status queued, before the special request is added
1480 * to the head of the queue. Then the special request is waited on normally.
1481 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001482static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (list_empty(&device->ccw_queue))
1487 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001488 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001489 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001492int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct dasd_device *device;
1495 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001496
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001497 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 spin_lock_irq(get_ccwdev_lock(device->cdev));
1499 rc = _dasd_term_running_cqr(device);
1500 if (rc) {
1501 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1502 return rc;
1503 }
Horst Hummel138c0142006-06-29 14:58:12 +02001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001506 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001508 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001511 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1514
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001515 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001518 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return rc;
1520}
1521
1522/*
1523 * Cancels a request that was started with dasd_sleep_on_req.
1524 * This is useful to timeout requests. The request will be
1525 * terminated if it is currently in i/o.
1526 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001527 * 0 if there was no need to terminate the request (not started yet)
1528 * negative error code if termination failed
1529 * Cancellation of a request is an asynchronous operation! The calling
1530 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001532int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001534 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unsigned long flags;
1536 int rc;
1537
1538 rc = 0;
1539 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1540 switch (cqr->status) {
1541 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001542 /* request was not started - just set to cleared */
1543 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545 case DASD_CQR_IN_IO:
1546 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001547 rc = device->discipline->term_IO(cqr);
1548 if (rc) {
1549 DEV_MESSAGE(KERN_ERR, device,
1550 "dasd_cancel_req is unable "
1551 " to terminate request %p, rc = %d",
1552 cqr, rc);
1553 } else {
1554 cqr->stopclk = get_clock();
1555 rc = 1;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001558 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001562 dasd_schedule_device_bh(device);
1563 return rc;
1564}
1565
1566
1567/*
1568 * SECTION: Operations of the dasd_block layer.
1569 */
1570
1571/*
1572 * Timeout function for dasd_block. This is used when the block layer
1573 * is waiting for something that may not come reliably, (e.g. a state
1574 * change interrupt)
1575 */
1576static void dasd_block_timeout(unsigned long ptr)
1577{
1578 unsigned long flags;
1579 struct dasd_block *block;
1580
1581 block = (struct dasd_block *) ptr;
1582 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1583 /* re-activate request queue */
1584 block->base->stopped &= ~DASD_STOPPED_PENDING;
1585 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1586 dasd_schedule_block_bh(block);
1587}
1588
1589/*
1590 * Setup timeout for a dasd_block in jiffies.
1591 */
1592void dasd_block_set_timer(struct dasd_block *block, int expires)
1593{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001594 if (expires == 0)
1595 del_timer(&block->timer);
1596 else
1597 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001598}
1599
1600/*
1601 * Clear timeout for a dasd_block.
1602 */
1603void dasd_block_clear_timer(struct dasd_block *block)
1604{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001605 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001606}
1607
1608/*
1609 * posts the buffer_cache about a finalized request
1610 */
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001611static inline void dasd_end_request(struct request *req, int error)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001612{
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001613 if (__blk_end_request(req, error, blk_rq_bytes(req)))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001614 BUG();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001615}
1616
1617/*
1618 * Process finished error recovery ccw.
1619 */
1620static inline void __dasd_block_process_erp(struct dasd_block *block,
1621 struct dasd_ccw_req *cqr)
1622{
1623 dasd_erp_fn_t erp_fn;
1624 struct dasd_device *device = block->base;
1625
1626 if (cqr->status == DASD_CQR_DONE)
1627 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1628 else
1629 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1630 erp_fn = device->discipline->erp_postaction(cqr);
1631 erp_fn(cqr);
1632}
1633
1634/*
1635 * Fetch requests from the block device queue.
1636 */
1637static void __dasd_process_request_queue(struct dasd_block *block)
1638{
1639 struct request_queue *queue;
1640 struct request *req;
1641 struct dasd_ccw_req *cqr;
1642 struct dasd_device *basedev;
1643 unsigned long flags;
1644 queue = block->request_queue;
1645 basedev = block->base;
1646 /* No queue ? Then there is nothing to do. */
1647 if (queue == NULL)
1648 return;
1649
1650 /*
1651 * We requeue request from the block device queue to the ccw
1652 * queue only in two states. In state DASD_STATE_READY the
1653 * partition detection is done and we need to requeue requests
1654 * for that. State DASD_STATE_ONLINE is normal block device
1655 * operation.
1656 */
1657 if (basedev->state < DASD_STATE_READY)
1658 return;
1659 /* Now we try to fetch requests from the request queue */
1660 while (!blk_queue_plugged(queue) &&
1661 elv_next_request(queue)) {
1662
1663 req = elv_next_request(queue);
1664
1665 if (basedev->features & DASD_FEATURE_READONLY &&
1666 rq_data_dir(req) == WRITE) {
1667 DBF_DEV_EVENT(DBF_ERR, basedev,
1668 "Rejecting write request %p",
1669 req);
1670 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001671 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001672 continue;
1673 }
1674 cqr = basedev->discipline->build_cp(basedev, block, req);
1675 if (IS_ERR(cqr)) {
1676 if (PTR_ERR(cqr) == -EBUSY)
1677 break; /* normal end condition */
1678 if (PTR_ERR(cqr) == -ENOMEM)
1679 break; /* terminate request queue loop */
1680 if (PTR_ERR(cqr) == -EAGAIN) {
1681 /*
1682 * The current request cannot be build right
1683 * now, we have to try later. If this request
1684 * is the head-of-queue we stop the device
1685 * for 1/2 second.
1686 */
1687 if (!list_empty(&block->ccw_queue))
1688 break;
1689 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1690 basedev->stopped |= DASD_STOPPED_PENDING;
1691 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1692 dasd_block_set_timer(block, HZ/2);
1693 break;
1694 }
1695 DBF_DEV_EVENT(DBF_ERR, basedev,
1696 "CCW creation failed (rc=%ld) "
1697 "on request %p",
1698 PTR_ERR(cqr), req);
1699 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001700 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001701 continue;
1702 }
1703 /*
1704 * Note: callback is set to dasd_return_cqr_cb in
1705 * __dasd_block_start_head to cover erp requests as well
1706 */
1707 cqr->callback_data = (void *) req;
1708 cqr->status = DASD_CQR_FILLED;
1709 blkdev_dequeue_request(req);
1710 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1711 dasd_profile_start(block, cqr, req);
1712 }
1713}
1714
1715static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1716{
1717 struct request *req;
1718 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001719 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001720
1721 req = (struct request *) cqr->callback_data;
1722 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001723 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001724 if (status <= 0)
1725 error = status ? status : -EIO;
1726 dasd_end_request(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001727}
1728
1729/*
1730 * Process ccw request queue.
1731 */
1732static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1733 struct list_head *final_queue)
1734{
1735 struct list_head *l, *n;
1736 struct dasd_ccw_req *cqr;
1737 dasd_erp_fn_t erp_fn;
1738 unsigned long flags;
1739 struct dasd_device *base = block->base;
1740
1741restart:
1742 /* Process request with final status. */
1743 list_for_each_safe(l, n, &block->ccw_queue) {
1744 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1745 if (cqr->status != DASD_CQR_DONE &&
1746 cqr->status != DASD_CQR_FAILED &&
1747 cqr->status != DASD_CQR_NEED_ERP &&
1748 cqr->status != DASD_CQR_TERMINATED)
1749 continue;
1750
1751 if (cqr->status == DASD_CQR_TERMINATED) {
1752 base->discipline->handle_terminated_request(cqr);
1753 goto restart;
1754 }
1755
1756 /* Process requests that may be recovered */
1757 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001758 erp_fn = base->discipline->erp_action(cqr);
1759 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001760 goto restart;
1761 }
1762
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001763 /* log sense for fatal error */
1764 if (cqr->status == DASD_CQR_FAILED) {
1765 dasd_log_sense(cqr, &cqr->irb);
1766 }
1767
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001768 /* First of all call extended error reporting. */
1769 if (dasd_eer_enabled(base) &&
1770 cqr->status == DASD_CQR_FAILED) {
1771 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1772
1773 /* restart request */
1774 cqr->status = DASD_CQR_FILLED;
1775 cqr->retries = 255;
1776 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1777 base->stopped |= DASD_STOPPED_QUIESCE;
1778 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1779 flags);
1780 goto restart;
1781 }
1782
1783 /* Process finished ERP request. */
1784 if (cqr->refers) {
1785 __dasd_block_process_erp(block, cqr);
1786 goto restart;
1787 }
1788
1789 /* Rechain finished requests to final queue */
1790 cqr->endclk = get_clock();
1791 list_move_tail(&cqr->blocklist, final_queue);
1792 }
1793}
1794
1795static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1796{
1797 dasd_schedule_block_bh(cqr->block);
1798}
1799
1800static void __dasd_block_start_head(struct dasd_block *block)
1801{
1802 struct dasd_ccw_req *cqr;
1803
1804 if (list_empty(&block->ccw_queue))
1805 return;
1806 /* We allways begin with the first requests on the queue, as some
1807 * of previously started requests have to be enqueued on a
1808 * dasd_device again for error recovery.
1809 */
1810 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1811 if (cqr->status != DASD_CQR_FILLED)
1812 continue;
1813 /* Non-temporary stop condition will trigger fail fast */
1814 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1815 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1816 (!dasd_eer_enabled(block->base))) {
1817 cqr->status = DASD_CQR_FAILED;
1818 dasd_schedule_block_bh(block);
1819 continue;
1820 }
1821 /* Don't try to start requests if device is stopped */
1822 if (block->base->stopped)
1823 return;
1824
1825 /* just a fail safe check, should not happen */
1826 if (!cqr->startdev)
1827 cqr->startdev = block->base;
1828
1829 /* make sure that the requests we submit find their way back */
1830 cqr->callback = dasd_return_cqr_cb;
1831
1832 dasd_add_request_tail(cqr);
1833 }
1834}
1835
1836/*
1837 * Central dasd_block layer routine. Takes requests from the generic
1838 * block layer request queue, creates ccw requests, enqueues them on
1839 * a dasd_device and processes ccw requests that have been returned.
1840 */
1841static void dasd_block_tasklet(struct dasd_block *block)
1842{
1843 struct list_head final_queue;
1844 struct list_head *l, *n;
1845 struct dasd_ccw_req *cqr;
1846
1847 atomic_set(&block->tasklet_scheduled, 0);
1848 INIT_LIST_HEAD(&final_queue);
1849 spin_lock(&block->queue_lock);
1850 /* Finish off requests on ccw queue */
1851 __dasd_process_block_ccw_queue(block, &final_queue);
1852 spin_unlock(&block->queue_lock);
1853 /* Now call the callback function of requests with final status */
1854 spin_lock_irq(&block->request_queue_lock);
1855 list_for_each_safe(l, n, &final_queue) {
1856 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1857 list_del_init(&cqr->blocklist);
1858 __dasd_cleanup_cqr(cqr);
1859 }
1860 spin_lock(&block->queue_lock);
1861 /* Get new request from the block device request queue */
1862 __dasd_process_request_queue(block);
1863 /* Now check if the head of the ccw queue needs to be started. */
1864 __dasd_block_start_head(block);
1865 spin_unlock(&block->queue_lock);
1866 spin_unlock_irq(&block->request_queue_lock);
1867 dasd_put_device(block->base);
1868}
1869
1870static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1871{
1872 wake_up(&dasd_flush_wq);
1873}
1874
1875/*
1876 * Go through all request on the dasd_block request queue, cancel them
1877 * on the respective dasd_device, and return them to the generic
1878 * block layer.
1879 */
1880static int dasd_flush_block_queue(struct dasd_block *block)
1881{
1882 struct dasd_ccw_req *cqr, *n;
1883 int rc, i;
1884 struct list_head flush_queue;
1885
1886 INIT_LIST_HEAD(&flush_queue);
1887 spin_lock_bh(&block->queue_lock);
1888 rc = 0;
1889restart:
1890 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1891 /* if this request currently owned by a dasd_device cancel it */
1892 if (cqr->status >= DASD_CQR_QUEUED)
1893 rc = dasd_cancel_req(cqr);
1894 if (rc < 0)
1895 break;
1896 /* Rechain request (including erp chain) so it won't be
1897 * touched by the dasd_block_tasklet anymore.
1898 * Replace the callback so we notice when the request
1899 * is returned from the dasd_device layer.
1900 */
1901 cqr->callback = _dasd_wake_block_flush_cb;
1902 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1903 list_move_tail(&cqr->blocklist, &flush_queue);
1904 if (i > 1)
1905 /* moved more than one request - need to restart */
1906 goto restart;
1907 }
1908 spin_unlock_bh(&block->queue_lock);
1909 /* Now call the callback function of flushed requests */
1910restart_cb:
1911 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1912 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1913 /* Process finished ERP request. */
1914 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001915 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001916 __dasd_block_process_erp(block, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001917 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 /* restart list_for_xx loop since dasd_process_erp
1919 * might remove multiple elements */
1920 goto restart_cb;
1921 }
1922 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001923 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001924 cqr->endclk = get_clock();
1925 list_del_init(&cqr->blocklist);
1926 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001927 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return rc;
1930}
1931
1932/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001933 * Schedules a call to dasd_tasklet over the device tasklet.
1934 */
1935void dasd_schedule_block_bh(struct dasd_block *block)
1936{
1937 /* Protect against rescheduling. */
1938 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1939 return;
1940 /* life cycle of block is bound to it's base device */
1941 dasd_get_device(block->base);
1942 tasklet_hi_schedule(&block->tasklet);
1943}
1944
1945
1946/*
1947 * SECTION: external block device operations
1948 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 */
1950
1951/*
1952 * Dasd request queue function. Called from ll_rw_blk.c
1953 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001954static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001956 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 block = queue->queuedata;
1959 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001961 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001963 __dasd_block_start_head(block);
1964 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967/*
1968 * Allocate and initialize request queue and default I/O scheduler.
1969 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001970static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 int rc;
1973
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001974 block->request_queue = blk_init_queue(do_dasd_request,
1975 &block->request_queue_lock);
1976 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -ENOMEM;
1978
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001979 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001981 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02001982 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001985 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return rc;
1987 }
1988 return 0;
1989}
1990
1991/*
1992 * Allocate and initialize request queue.
1993 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001994static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 int max;
1997
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001998 blk_queue_hardsect_size(block->request_queue, block->bp_block);
1999 max = block->base->discipline->max_blocks << block->s2b_shift;
2000 blk_queue_max_sectors(block->request_queue, max);
2001 blk_queue_max_phys_segments(block->request_queue, -1L);
2002 blk_queue_max_hw_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002003 /* with page sized segments we can translate each segement into
2004 * one idaw/tidaw
2005 */
2006 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2007 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002008 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011/*
2012 * Deactivate and free request queue.
2013 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002014static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016 if (block->request_queue) {
2017 blk_cleanup_queue(block->request_queue);
2018 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020}
2021
2022/*
2023 * Flush request on the request queue.
2024 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002025static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct request *req;
2028
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002029 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002031
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002032 spin_lock_irq(&block->request_queue_lock);
2033 while ((req = elv_next_request(block->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002035 dasd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002037 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Al Viro57a7c0b2008-03-02 10:36:08 -05002040static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Al Viro57a7c0b2008-03-02 10:36:08 -05002042 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002043 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int rc;
2045
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002046 atomic_inc(&block->open_count);
2047 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 rc = -ENODEV;
2049 goto unlock;
2050 }
2051
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002052 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rc = -EINVAL;
2054 goto unlock;
2055 }
2056
2057 if (dasd_probeonly) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002058 DEV_MESSAGE(KERN_INFO, base, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 "No access to device due to probeonly mode");
2060 rc = -EPERM;
2061 goto out;
2062 }
2063
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002064 if (base->state <= DASD_STATE_BASIC) {
2065 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 " Cannot open unrecognized device");
2067 rc = -ENODEV;
2068 goto out;
2069 }
2070
2071 return 0;
2072
2073out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002074 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002076 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return rc;
2078}
2079
Al Viro57a7c0b2008-03-02 10:36:08 -05002080static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002082 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002084 atomic_dec(&block->open_count);
2085 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 0;
2087}
2088
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002089/*
2090 * Return disk geometry.
2091 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002092static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002093{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002094 struct dasd_block *block;
2095 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002096
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002097 block = bdev->bd_disk->private_data;
2098 base = block->base;
2099 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002100 return -ENODEV;
2101
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002102 if (!base->discipline ||
2103 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002104 return -EINVAL;
2105
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002106 base->discipline->fill_geometry(block, geo);
2107 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002108 return 0;
2109}
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111struct block_device_operations
2112dasd_device_operations = {
2113 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002114 .open = dasd_open,
2115 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002116 .ioctl = dasd_ioctl,
2117 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002118 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119};
2120
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002121/*******************************************************************************
2122 * end of block device operations
2123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125static void
2126dasd_exit(void)
2127{
2128#ifdef CONFIG_PROC_FS
2129 dasd_proc_exit();
2130#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002131 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002132 if (dasd_page_cache != NULL) {
2133 kmem_cache_destroy(dasd_page_cache);
2134 dasd_page_cache = NULL;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 dasd_gendisk_exit();
2137 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (dasd_debug_area != NULL) {
2139 debug_unregister(dasd_debug_area);
2140 dasd_debug_area = NULL;
2141 }
2142}
2143
2144/*
2145 * SECTION: common functions for ccw_driver use
2146 */
2147
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002148/*
2149 * Initial attempt at a probe function. this can be simplified once
2150 * the other detection code is gone.
2151 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002152int dasd_generic_probe(struct ccw_device *cdev,
2153 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 int ret;
2156
Horst Hummel40545572006-06-29 15:08:18 +02002157 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2158 if (ret) {
2159 printk(KERN_WARNING
2160 "dasd_generic_probe: could not set ccw-device options "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002161 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002162 return ret;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 ret = dasd_add_sysfs_files(cdev);
2165 if (ret) {
2166 printk(KERN_WARNING
2167 "dasd_generic_probe: could not add sysfs entries "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002168 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
Horst Hummel40545572006-06-29 15:08:18 +02002171 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Horst Hummel40545572006-06-29 15:08:18 +02002173 /*
2174 * Automatically online either all dasd devices (dasd_autodetect)
2175 * or all devices specified with dasd= parameters during
2176 * initial probe.
2177 */
2178 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002179 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Horst Hummel40545572006-06-29 15:08:18 +02002180 ret = ccw_device_set_online(cdev);
2181 if (ret)
2182 printk(KERN_WARNING
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002183 "dasd_generic_probe: could not initially "
2184 "online ccw-device %s; return code: %d\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002185 dev_name(&cdev->dev), ret);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002189/*
2190 * This will one day be called from a global not_oper handler.
2191 * It is also used by driver_unregister during module unload.
2192 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002193void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002196 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Horst Hummel59afda72005-05-16 21:53:39 -07002198 cdev->handler = NULL;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 dasd_remove_sysfs_files(cdev);
2201 device = dasd_device_from_cdev(cdev);
2202 if (IS_ERR(device))
2203 return;
2204 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2205 /* Already doing offline processing */
2206 dasd_put_device(device);
2207 return;
2208 }
2209 /*
2210 * This device is removed unconditionally. Set offline
2211 * flag to prevent dasd_open from opening it while it is
2212 * no quite down yet.
2213 */
2214 dasd_set_target_state(device, DASD_STATE_NEW);
2215 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002216 block = device->block;
2217 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002219 /*
2220 * life cycle of block is bound to device, so delete it after
2221 * device was safely removed
2222 */
2223 if (block)
2224 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002227/*
2228 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002230 * or the user has started activation through sysfs.
2231 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002232int dasd_generic_set_online(struct ccw_device *cdev,
2233 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002235 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002237 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002238
Horst Hummel40545572006-06-29 15:08:18 +02002239 /* first online clears initial online feature flag */
2240 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 device = dasd_create_device(cdev);
2242 if (IS_ERR(device))
2243 return PTR_ERR(device);
2244
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002245 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002246 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!dasd_diag_discipline_pointer) {
2248 printk (KERN_WARNING
2249 "dasd_generic couldn't online device %s "
2250 "- discipline DIAG not available\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002251 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 dasd_delete_device(device);
2253 return -ENODEV;
2254 }
2255 discipline = dasd_diag_discipline_pointer;
2256 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002257 if (!try_module_get(base_discipline->owner)) {
2258 dasd_delete_device(device);
2259 return -EINVAL;
2260 }
2261 if (!try_module_get(discipline->owner)) {
2262 module_put(base_discipline->owner);
2263 dasd_delete_device(device);
2264 return -EINVAL;
2265 }
2266 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 device->discipline = discipline;
2268
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002269 /* check_device will allocate block device if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rc = discipline->check_device(device);
2271 if (rc) {
2272 printk (KERN_WARNING
2273 "dasd_generic couldn't online device %s "
2274 "with discipline %s rc=%i\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002275 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002276 module_put(discipline->owner);
2277 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 dasd_delete_device(device);
2279 return rc;
2280 }
2281
2282 dasd_set_target_state(device, DASD_STATE_ONLINE);
2283 if (device->state <= DASD_STATE_KNOWN) {
2284 printk (KERN_WARNING
2285 "dasd_generic discipline not found for %s\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002286 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 rc = -ENODEV;
2288 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002289 if (device->block)
2290 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 dasd_delete_device(device);
2292 } else
2293 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002294 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /* FIXME: we have to wait for the root device but we don't want
2297 * to wait for each single device but for all at once. */
2298 wait_event(dasd_init_waitq, _wait_for_device(device));
2299
2300 dasd_put_device(device);
2301
2302 return rc;
2303}
2304
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002305int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002308 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002309 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 device = dasd_device_from_cdev(cdev);
2312 if (IS_ERR(device))
2313 return PTR_ERR(device);
2314 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2315 /* Already doing offline processing */
2316 dasd_put_device(device);
2317 return 0;
2318 }
2319 /*
2320 * We must make sure that this device is currently not in use.
2321 * The open_count is increased for every opener, that includes
2322 * the blkdev_get in dasd_scan_partitions. We are only interested
2323 * in the other openers.
2324 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002325 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002326 max_count = device->block->bdev ? 0 : -1;
2327 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002328 if (open_count > max_count) {
2329 if (open_count > 0)
2330 printk(KERN_WARNING "Can't offline dasd "
2331 "device with open count = %i.\n",
2332 open_count);
2333 else
2334 printk(KERN_WARNING "%s",
2335 "Can't offline dasd device due "
2336 "to internal use\n");
2337 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2338 dasd_put_device(device);
2339 return -EBUSY;
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342 dasd_set_target_state(device, DASD_STATE_NEW);
2343 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002344 block = device->block;
2345 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002347 /*
2348 * life cycle of block is bound to device, so delete it after
2349 * device was safely removed
2350 */
2351 if (block)
2352 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354}
2355
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002356int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 struct dasd_device *device;
2359 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 int ret;
2361
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002362 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (IS_ERR(device))
2364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ret = 0;
2366 switch (event) {
2367 case CIO_GONE:
2368 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002369 /* First of all call extended error reporting. */
2370 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (device->state < DASD_STATE_BASIC)
2373 break;
2374 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002375 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2376 if (cqr->status == DASD_CQR_IN_IO) {
2377 cqr->status = DASD_CQR_QUEUED;
2378 cqr->retries++;
2379 }
2380 device->stopped |= DASD_STOPPED_DC_WAIT;
2381 dasd_device_clear_timer(device);
2382 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 ret = 1;
2384 break;
2385 case CIO_OPER:
2386 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002387 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2388 dasd_schedule_device_bh(device);
2389 if (device->block)
2390 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 ret = 1;
2392 break;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dasd_put_device(device);
2395 return ret;
2396}
2397
Heiko Carstens763968e2007-05-10 15:45:46 +02002398static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2399 void *rdc_buffer,
2400 int rdc_buffer_size,
2401 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002402{
2403 struct dasd_ccw_req *cqr;
2404 struct ccw1 *ccw;
2405
2406 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2407
2408 if (IS_ERR(cqr)) {
2409 DEV_MESSAGE(KERN_WARNING, device, "%s",
2410 "Could not allocate RDC request");
2411 return cqr;
2412 }
2413
2414 ccw = cqr->cpaddr;
2415 ccw->cmd_code = CCW_CMD_RDC;
2416 ccw->cda = (__u32)(addr_t)rdc_buffer;
2417 ccw->count = rdc_buffer_size;
2418
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002419 cqr->startdev = device;
2420 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002421 cqr->expires = 10*HZ;
2422 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2423 cqr->retries = 2;
2424 cqr->buildclk = get_clock();
2425 cqr->status = DASD_CQR_FILLED;
2426 return cqr;
2427}
2428
2429
2430int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2431 void **rdc_buffer, int rdc_buffer_size)
2432{
2433 int ret;
2434 struct dasd_ccw_req *cqr;
2435
2436 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2437 magic);
2438 if (IS_ERR(cqr))
2439 return PTR_ERR(cqr);
2440
2441 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002442 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002443 return ret;
2444}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002445EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002446
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002447/*
2448 * In command mode and transport mode we need to look for sense
2449 * data in different places. The sense data itself is allways
2450 * an array of 32 bytes, so we can unify the sense data access
2451 * for both modes.
2452 */
2453char *dasd_get_sense(struct irb *irb)
2454{
2455 struct tsb *tsb = NULL;
2456 char *sense = NULL;
2457
2458 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2459 if (irb->scsw.tm.tcw)
2460 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2461 irb->scsw.tm.tcw);
2462 if (tsb && tsb->length == 64 && tsb->flags)
2463 switch (tsb->flags & 0x07) {
2464 case 1: /* tsa_iostat */
2465 sense = tsb->tsa.iostat.sense;
2466 break;
2467 case 2: /* tsa_ddpc */
2468 sense = tsb->tsa.ddpc.sense;
2469 break;
2470 default:
2471 /* currently we don't use interrogate data */
2472 break;
2473 }
2474 } else if (irb->esw.esw0.erw.cons) {
2475 sense = irb->ecw;
2476 }
2477 return sense;
2478}
2479EXPORT_SYMBOL_GPL(dasd_get_sense);
2480
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002481static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 int rc;
2484
2485 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002486 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002487 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002490 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (dasd_debug_area == NULL) {
2492 rc = -ENOMEM;
2493 goto failed;
2494 }
2495 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002496 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2499
2500 dasd_diag_discipline_pointer = NULL;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 rc = dasd_devmap_init();
2503 if (rc)
2504 goto failed;
2505 rc = dasd_gendisk_init();
2506 if (rc)
2507 goto failed;
2508 rc = dasd_parse();
2509 if (rc)
2510 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002511 rc = dasd_eer_init();
2512 if (rc)
2513 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514#ifdef CONFIG_PROC_FS
2515 rc = dasd_proc_init();
2516 if (rc)
2517 goto failed;
2518#endif
2519
2520 return 0;
2521failed:
2522 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2523 dasd_exit();
2524 return rc;
2525}
2526
2527module_init(dasd_init);
2528module_exit(dasd_exit);
2529
2530EXPORT_SYMBOL(dasd_debug_area);
2531EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2532
2533EXPORT_SYMBOL(dasd_add_request_head);
2534EXPORT_SYMBOL(dasd_add_request_tail);
2535EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002536EXPORT_SYMBOL(dasd_device_clear_timer);
2537EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538EXPORT_SYMBOL(dasd_enable_device);
2539EXPORT_SYMBOL(dasd_int_handler);
2540EXPORT_SYMBOL(dasd_kfree_request);
2541EXPORT_SYMBOL(dasd_kick_device);
2542EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002543EXPORT_SYMBOL(dasd_schedule_device_bh);
2544EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002546EXPORT_SYMBOL(dasd_device_set_timer);
2547EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548EXPORT_SYMBOL(dasd_sfree_request);
2549EXPORT_SYMBOL(dasd_sleep_on);
2550EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2551EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2552EXPORT_SYMBOL(dasd_smalloc_request);
2553EXPORT_SYMBOL(dasd_start_IO);
2554EXPORT_SYMBOL(dasd_term_IO);
2555
2556EXPORT_SYMBOL_GPL(dasd_generic_probe);
2557EXPORT_SYMBOL_GPL(dasd_generic_remove);
2558EXPORT_SYMBOL_GPL(dasd_generic_notify);
2559EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2560EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002561EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2562EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2563EXPORT_SYMBOL_GPL(dasd_alloc_block);
2564EXPORT_SYMBOL_GPL(dasd_free_block);