blob: 0570794ccf1c8f8d26c89f4d6825abd9d047738e [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
Stefan Haberlandfc19f382009-03-26 15:23:49 +010012#define KMSG_COMPONENT "dasd"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kmod.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/major.h>
20#include <linux/slab.h>
21#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080022#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/ccwdev.h>
25#include <asm/ebcdic.h>
26#include <asm/idals.h>
27#include <asm/todclk.h>
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +010028#include <asm/itcw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* This is ugly... */
31#define PRINTK_HEADER "dasd:"
32
33#include "dasd_int.h"
34/*
35 * SECTION: Constant definitions to be used within this file
36 */
37#define DASD_CHANQ_MAX_SIZE 4
38
39/*
40 * SECTION: exported variables of dasd.c
41 */
42debug_info_t *dasd_debug_area;
43struct dasd_discipline *dasd_diag_discipline_pointer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010044void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
47MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
48 " Copyright 2000 IBM Corporation");
49MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_LICENSE("GPL");
51
52/*
53 * SECTION: prototypes for static functions of dasd.c
54 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010055static int dasd_alloc_queue(struct dasd_block *);
56static void dasd_setup_queue(struct dasd_block *);
57static void dasd_free_queue(struct dasd_block *);
58static void dasd_flush_request_queue(struct dasd_block *);
59static int dasd_flush_block_queue(struct dasd_block *);
60static void dasd_device_tasklet(struct dasd_device *);
61static void dasd_block_tasklet(struct dasd_block *);
Al Viro4927b3f2006-12-06 19:18:20 +000062static void do_kick_device(struct work_struct *);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
Stefan Weinhuber48cae882009-02-11 10:37:31 +010064static void dasd_device_timeout(unsigned long);
65static void dasd_block_timeout(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * SECTION: Operations on the device structure.
69 */
70static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020071static wait_queue_head_t dasd_flush_wq;
Stefan Haberlandc80ee722008-05-30 10:03:31 +020072static wait_queue_head_t generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Allocate memory for a new device structure.
76 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010077struct dasd_device *dasd_alloc_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct dasd_device *device;
80
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010081 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
82 if (!device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* Get two pages for normal block device operations. */
86 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010087 if (!device->ccw_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 kfree(device);
89 return ERR_PTR(-ENOMEM);
90 }
91 /* Get one page for error recovery. */
92 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010093 if (!device->erp_mem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 free_pages((unsigned long) device->ccw_mem, 1);
95 kfree(device);
96 return ERR_PTR(-ENOMEM);
97 }
98
99 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
100 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
101 spin_lock_init(&device->mem_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100102 atomic_set(&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +0200103 tasklet_init(&device->tasklet,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100104 (void (*)(unsigned long)) dasd_device_tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 (unsigned long) device);
106 INIT_LIST_HEAD(&device->ccw_queue);
107 init_timer(&device->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100108 device->timer.function = dasd_device_timeout;
109 device->timer.data = (unsigned long) device;
Al Viro4927b3f2006-12-06 19:18:20 +0000110 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 device->state = DASD_STATE_NEW;
112 device->target = DASD_STATE_NEW;
113
114 return device;
115}
116
117/*
118 * Free memory of a device structure.
119 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100120void dasd_free_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800122 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 free_page((unsigned long) device->erp_mem);
124 free_pages((unsigned long) device->ccw_mem, 1);
125 kfree(device);
126}
127
128/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100129 * Allocate memory for a new device structure.
130 */
131struct dasd_block *dasd_alloc_block(void)
132{
133 struct dasd_block *block;
134
135 block = kzalloc(sizeof(*block), GFP_ATOMIC);
136 if (!block)
137 return ERR_PTR(-ENOMEM);
138 /* open_count = 0 means device online but not in use */
139 atomic_set(&block->open_count, -1);
140
141 spin_lock_init(&block->request_queue_lock);
142 atomic_set(&block->tasklet_scheduled, 0);
143 tasklet_init(&block->tasklet,
144 (void (*)(unsigned long)) dasd_block_tasklet,
145 (unsigned long) block);
146 INIT_LIST_HEAD(&block->ccw_queue);
147 spin_lock_init(&block->queue_lock);
148 init_timer(&block->timer);
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100149 block->timer.function = dasd_block_timeout;
150 block->timer.data = (unsigned long) block;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100151
152 return block;
153}
154
155/*
156 * Free memory of a device structure.
157 */
158void dasd_free_block(struct dasd_block *block)
159{
160 kfree(block);
161}
162
163/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * Make a new device known to the system.
165 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100166static int dasd_state_new_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int rc;
169
170 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200171 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * keep the reference count > 0.
173 */
174 dasd_get_device(device);
175
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176 if (device->block) {
177 rc = dasd_alloc_queue(device->block);
178 if (rc) {
179 dasd_put_device(device);
180 return rc;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 device->state = DASD_STATE_KNOWN;
184 return 0;
185}
186
187/*
188 * Let the system forget about a device.
189 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100190static int dasd_state_known_to_new(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800192 /* Disable extended error reporting for this device. */
193 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Forget the discipline information. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195 if (device->discipline) {
196 if (device->discipline->uncheck_device)
197 device->discipline->uncheck_device(device);
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800198 module_put(device->discipline->owner);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800201 if (device->base_discipline)
202 module_put(device->base_discipline->owner);
203 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 device->state = DASD_STATE_NEW;
205
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206 if (device->block)
207 dasd_free_queue(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Give up reference we took in dasd_state_new_to_known. */
210 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * Request the irq line for the device.
216 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217static int dasd_state_known_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int rc;
220
221 /* Allocate and register gendisk structure. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100222 if (device->block) {
223 rc = dasd_gendisk_alloc(device->block);
224 if (rc)
225 return rc;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100228 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100229 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200231 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
233
234 device->state = DASD_STATE_BASIC;
235 return 0;
236}
237
238/*
239 * Release the irq line for the device. Terminate any running i/o.
240 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241static int dasd_state_basic_to_known(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Horst Hummel8f617012006-08-30 14:33:33 +0200243 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100244 if (device->block) {
245 dasd_gendisk_free(device->block);
246 dasd_block_clear_timer(device->block);
247 }
248 rc = dasd_flush_device_queue(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200249 if (rc)
250 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
254 if (device->debug_area != NULL) {
255 debug_unregister(device->debug_area);
256 device->debug_area = NULL;
257 }
258 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * Do the initial analysis. The do_analysis function may return
264 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
265 * until the discipline decides to continue the startup sequence
266 * by calling the function dasd_change_state. The eckd disciplines
267 * uses this to start a ccw that detects the format. The completion
268 * interrupt for this detection ccw uses the kernel event daemon to
269 * trigger the call to dasd_change_state. All this is done in the
270 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800271 * After the analysis ccw is done (do_analysis returned 0) the block
272 * device is setup.
273 * In case the analysis returns an error, the device setup is stopped
274 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100276static int dasd_state_basic_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100282 block = device->block;
Horst Hummel90f00942006-03-07 21:55:39 -0800283 /* make disk known with correct capacity */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100284 if (block) {
285 if (block->base->discipline->do_analysis != NULL)
286 rc = block->base->discipline->do_analysis(block);
287 if (rc) {
288 if (rc != -EAGAIN)
289 device->state = DASD_STATE_UNFMT;
290 return rc;
291 }
292 dasd_setup_queue(block);
293 set_capacity(block->gdp,
294 block->blocks << block->s2b_shift);
295 device->state = DASD_STATE_READY;
296 rc = dasd_scan_partitions(block);
297 if (rc)
298 device->state = DASD_STATE_BASIC;
299 } else {
300 device->state = DASD_STATE_READY;
301 }
Horst Hummel90f00942006-03-07 21:55:39 -0800302 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * Remove device from block device layer. Destroy dirty buffers.
307 * Forget format information. Check if the target level is basic
308 * and if it is create fake disk for formatting.
309 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100310static int dasd_state_ready_to_basic(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Horst Hummel8f617012006-08-30 14:33:33 +0200312 int rc;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 device->state = DASD_STATE_BASIC;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100315 if (device->block) {
316 struct dasd_block *block = device->block;
317 rc = dasd_flush_block_queue(block);
318 if (rc) {
319 device->state = DASD_STATE_READY;
320 return rc;
321 }
322 dasd_destroy_partitions(block);
323 dasd_flush_request_queue(block);
324 block->blocks = 0;
325 block->bp_block = 0;
326 block->s2b_shift = 0;
327 }
Horst Hummel8f617012006-08-30 14:33:33 +0200328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/*
Horst Hummel90f00942006-03-07 21:55:39 -0800332 * Back to basic.
333 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334static int dasd_state_unfmt_to_basic(struct dasd_device *device)
Horst Hummel90f00942006-03-07 21:55:39 -0800335{
336 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200337 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800338}
339
340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * Make the device online and schedule the bottom half to start
342 * the requeueing of requests from the linux request queue to the
343 * ccw queue.
344 */
Horst Hummel8f617012006-08-30 14:33:33 +0200345static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346dasd_state_ready_to_online(struct dasd_device * device)
347{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100348 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100349 struct gendisk *disk;
350 struct disk_part_iter piter;
351 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100352
353 if (device->discipline->ready_to_online) {
354 rc = device->discipline->ready_to_online(device);
355 if (rc)
356 return rc;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 device->state = DASD_STATE_ONLINE;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100359 if (device->block) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100360 dasd_schedule_block_bh(device->block);
Stefan Weinhuber13018092009-01-09 12:14:50 +0100361 disk = device->block->bdev->bd_disk;
362 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
363 while ((part = disk_part_iter_next(&piter)))
364 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
365 disk_part_iter_exit(&piter);
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
370/*
371 * Stop the requeueing of requests again.
372 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100373static int dasd_state_online_to_ready(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375 int rc;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100376 struct gendisk *disk;
377 struct disk_part_iter piter;
378 struct hd_struct *part;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100379
380 if (device->discipline->online_to_ready) {
381 rc = device->discipline->online_to_ready(device);
382 if (rc)
383 return rc;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 device->state = DASD_STATE_READY;
Stefan Weinhuber13018092009-01-09 12:14:50 +0100386 if (device->block) {
387 disk = device->block->bdev->bd_disk;
388 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
389 while ((part = disk_part_iter_next(&piter)))
390 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
391 disk_part_iter_exit(&piter);
392 }
Horst Hummel8f617012006-08-30 14:33:33 +0200393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * Device startup state changes.
398 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100399static int dasd_increase_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int rc;
402
403 rc = 0;
404 if (device->state == DASD_STATE_NEW &&
405 device->target >= DASD_STATE_KNOWN)
406 rc = dasd_state_new_to_known(device);
407
408 if (!rc &&
409 device->state == DASD_STATE_KNOWN &&
410 device->target >= DASD_STATE_BASIC)
411 rc = dasd_state_known_to_basic(device);
412
413 if (!rc &&
414 device->state == DASD_STATE_BASIC &&
415 device->target >= DASD_STATE_READY)
416 rc = dasd_state_basic_to_ready(device);
417
418 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700419 device->state == DASD_STATE_UNFMT &&
420 device->target > DASD_STATE_UNFMT)
421 rc = -EPERM;
422
423 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 device->state == DASD_STATE_READY &&
425 device->target >= DASD_STATE_ONLINE)
426 rc = dasd_state_ready_to_online(device);
427
428 return rc;
429}
430
431/*
432 * Device shutdown state changes.
433 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100434static int dasd_decrease_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Horst Hummel8f617012006-08-30 14:33:33 +0200436 int rc;
437
438 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (device->state == DASD_STATE_ONLINE &&
440 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200441 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200442
Horst Hummel8f617012006-08-30 14:33:33 +0200443 if (!rc &&
444 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200446 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800447
Horst Hummel8f617012006-08-30 14:33:33 +0200448 if (!rc &&
449 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800450 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200451 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800452
Horst Hummel8f617012006-08-30 14:33:33 +0200453 if (!rc &&
454 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200456 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200457
Horst Hummel8f617012006-08-30 14:33:33 +0200458 if (!rc &&
459 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200461 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Horst Hummel8f617012006-08-30 14:33:33 +0200463 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * This is the main startup/shutdown routine.
468 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100469static void dasd_change_state(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int rc;
472
473 if (device->state == device->target)
474 /* Already where we want to go today... */
475 return;
476 if (device->state < device->target)
477 rc = dasd_increase_state(device);
478 else
479 rc = dasd_decrease_state(device);
480 if (rc && rc != -EAGAIN)
481 device->target = device->state;
482
483 if (device->state == device->target)
484 wake_up(&dasd_init_waitq);
Horst Hummel4dfd5c42007-04-27 16:01:47 +0200485
486 /* let user-space know that the device status changed */
487 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/*
491 * Kick starter for devices that did not complete the startup/shutdown
492 * procedure or were sleeping because of a pending state.
493 * dasd_kick_device will schedule a call do do_kick_device to the kernel
494 * event daemon.
495 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100496static void do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Al Viro4927b3f2006-12-06 19:18:20 +0000498 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dasd_change_state(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100500 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dasd_put_device(device);
502}
503
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100504void dasd_kick_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 dasd_get_device(device);
507 /* queue call to dasd_kick_device to the kernel event daemon. */
508 schedule_work(&device->kick_work);
509}
510
511/*
512 * Set the target state for a device and starts the state change.
513 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100514void dasd_set_target_state(struct dasd_device *device, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 /* If we are in probeonly mode stop at DASD_STATE_READY. */
517 if (dasd_probeonly && target > DASD_STATE_READY)
518 target = DASD_STATE_READY;
519 if (device->target != target) {
520 if (device->state == target)
521 wake_up(&dasd_init_waitq);
522 device->target = target;
523 }
524 if (device->state != device->target)
525 dasd_change_state(device);
526}
527
528/*
529 * Enable devices with device numbers in [from..to].
530 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100531static inline int _wait_for_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 return (device->state == device->target);
534}
535
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100536void dasd_enable_device(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 dasd_set_target_state(device, DASD_STATE_ONLINE);
539 if (device->state <= DASD_STATE_KNOWN)
540 /* No discipline for device found. */
541 dasd_set_target_state(device, DASD_STATE_NEW);
542 /* Now wait for the devices to come up. */
543 wait_event(dasd_init_waitq, _wait_for_device(device));
544}
545
546/*
547 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
548 */
549#ifdef CONFIG_DASD_PROFILE
550
551struct dasd_profile_info_t dasd_global_profile;
552unsigned int dasd_profile_level = DASD_PROFILE_OFF;
553
554/*
555 * Increments counter in global and local profiling structures.
556 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100557#define dasd_profile_counter(value, counter, block) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{ \
559 int index; \
560 for (index = 0; index < 31 && value >> (2+index); index++); \
561 dasd_global_profile.counter[index]++; \
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100562 block->profile.counter[index]++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * Add profiling information for cqr before execution.
567 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100568static void dasd_profile_start(struct dasd_block *block,
569 struct dasd_ccw_req *cqr,
570 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct list_head *l;
573 unsigned int counter;
574
575 if (dasd_profile_level != DASD_PROFILE_ON)
576 return;
577
578 /* count the length of the chanq for statistics */
579 counter = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100580 list_for_each(l, &block->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (++counter >= 31)
582 break;
583 dasd_global_profile.dasd_io_nr_req[counter]++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100584 block->profile.dasd_io_nr_req[counter]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*
588 * Add profiling information for cqr after execution.
589 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100590static void dasd_profile_end(struct dasd_block *block,
591 struct dasd_ccw_req *cqr,
592 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 long strtime, irqtime, endtime, tottime; /* in microseconds */
595 long tottimeps, sectors;
596
597 if (dasd_profile_level != DASD_PROFILE_ON)
598 return;
599
600 sectors = req->nr_sectors;
601 if (!cqr->buildclk || !cqr->startclk ||
602 !cqr->stopclk || !cqr->endclk ||
603 !sectors)
604 return;
605
606 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
607 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
608 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
609 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
610 tottimeps = tottime / sectors;
611
612 if (!dasd_global_profile.dasd_io_reqs)
613 memset(&dasd_global_profile, 0,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100614 sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 dasd_global_profile.dasd_io_reqs++;
616 dasd_global_profile.dasd_io_sects += sectors;
617
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100618 if (!block->profile.dasd_io_reqs)
619 memset(&block->profile, 0,
620 sizeof(struct dasd_profile_info_t));
621 block->profile.dasd_io_reqs++;
622 block->profile.dasd_io_sects += sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100624 dasd_profile_counter(sectors, dasd_io_secs, block);
625 dasd_profile_counter(tottime, dasd_io_times, block);
626 dasd_profile_counter(tottimeps, dasd_io_timps, block);
627 dasd_profile_counter(strtime, dasd_io_time1, block);
628 dasd_profile_counter(irqtime, dasd_io_time2, block);
629 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
630 dasd_profile_counter(endtime, dasd_io_time3, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100633#define dasd_profile_start(block, cqr, req) do {} while (0)
634#define dasd_profile_end(block, cqr, req) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#endif /* CONFIG_DASD_PROFILE */
636
637/*
638 * Allocate memory for a channel program with 'cplength' channel
639 * command words and 'datasize' additional space. There are two
640 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
641 * memory and 2) dasd_smalloc_request uses the static ccw memory
642 * that gets allocated for each device.
643 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100644struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
645 int datasize,
646 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct dasd_ccw_req *cqr;
649
650 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100651 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
652 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800654 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (cqr == NULL)
656 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 cqr->cpaddr = NULL;
658 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800659 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 GFP_ATOMIC | GFP_DMA);
661 if (cqr->cpaddr == NULL) {
662 kfree(cqr);
663 return ERR_PTR(-ENOMEM);
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 cqr->data = NULL;
667 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800668 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800670 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 kfree(cqr);
672 return ERR_PTR(-ENOMEM);
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 strncpy((char *) &cqr->magic, magic, 4);
676 ASCEBC((char *) &cqr->magic, 4);
677 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
678 dasd_get_device(device);
679 return cqr;
680}
681
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100682struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
683 int datasize,
684 struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 unsigned long flags;
687 struct dasd_ccw_req *cqr;
688 char *data;
689 int size;
690
691 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100692 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
693 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
696 if (cplength > 0)
697 size += cplength * sizeof(struct ccw1);
698 if (datasize > 0)
699 size += datasize;
700 spin_lock_irqsave(&device->mem_lock, flags);
701 cqr = (struct dasd_ccw_req *)
702 dasd_alloc_chunk(&device->ccw_chunks, size);
703 spin_unlock_irqrestore(&device->mem_lock, flags);
704 if (cqr == NULL)
705 return ERR_PTR(-ENOMEM);
706 memset(cqr, 0, sizeof(struct dasd_ccw_req));
707 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
708 cqr->cpaddr = NULL;
709 if (cplength > 0) {
710 cqr->cpaddr = (struct ccw1 *) data;
711 data += cplength*sizeof(struct ccw1);
712 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
713 }
714 cqr->data = NULL;
715 if (datasize > 0) {
716 cqr->data = data;
717 memset(cqr->data, 0, datasize);
718 }
719 strncpy((char *) &cqr->magic, magic, 4);
720 ASCEBC((char *) &cqr->magic, 4);
721 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
722 dasd_get_device(device);
723 return cqr;
724}
725
726/*
727 * Free memory of a channel program. This function needs to free all the
728 * idal lists that might have been created by dasd_set_cda and the
729 * struct dasd_ccw_req itself.
730 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100731void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800733#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct ccw1 *ccw;
735
736 /* Clear any idals used for the request. */
737 ccw = cqr->cpaddr;
738 do {
739 clear_normalized_cda(ccw);
740 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
741#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800742 kfree(cqr->cpaddr);
743 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 kfree(cqr);
745 dasd_put_device(device);
746}
747
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100748void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 unsigned long flags;
751
752 spin_lock_irqsave(&device->mem_lock, flags);
753 dasd_free_chunk(&device->ccw_chunks, cqr);
754 spin_unlock_irqrestore(&device->mem_lock, flags);
755 dasd_put_device(device);
756}
757
758/*
759 * Check discipline magic in cqr.
760 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100761static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct dasd_device *device;
764
765 if (cqr == NULL)
766 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100767 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100769 DBF_DEV_EVENT(DBF_WARNING, device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 " dasd_ccw_req 0x%08x magic doesn't match"
771 " discipline 0x%08x",
772 cqr->magic,
773 *(unsigned int *) device->discipline->name);
774 return -EINVAL;
775 }
776 return 0;
777}
778
779/*
780 * Terminate the current i/o and set the request to clear_pending.
781 * Timer keeps device runnig.
782 * ccw_device_clear can fail if the i/o subsystem
783 * is in a bad mood.
784 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100785int dasd_term_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct dasd_device *device;
788 int retries, rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100789 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Check the cqr */
792 rc = dasd_check_cqr(cqr);
793 if (rc)
794 return rc;
795 retries = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100796 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
798 rc = ccw_device_clear(device->cdev, (long) cqr);
799 switch (rc) {
800 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800801 cqr->retries--;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100802 cqr->status = DASD_CQR_CLEAR_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200804 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 DBF_DEV_EVENT(DBF_DEBUG, device,
806 "terminate cqr %p successful",
807 cqr);
808 break;
809 case -ENODEV:
810 DBF_DEV_EVENT(DBF_ERR, device, "%s",
811 "device gone, retry");
812 break;
813 case -EIO:
814 DBF_DEV_EVENT(DBF_ERR, device, "%s",
815 "I/O error, retry");
816 break;
817 case -EINVAL:
818 case -EBUSY:
819 DBF_DEV_EVENT(DBF_ERR, device, "%s",
820 "device busy, retry later");
821 break;
822 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100823 /* internal error 10 - unknown rc*/
824 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
825 dev_err(&device->cdev->dev, "An error occurred in the "
826 "DASD device driver, reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 BUG();
828 break;
829 }
830 retries++;
831 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100832 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return rc;
834}
835
836/*
837 * Start the i/o. This start_IO can fail if the channel is really busy.
838 * In that case set up a timer to start the request later.
839 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100840int dasd_start_IO(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct dasd_device *device;
843 int rc;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100844 char errorstring[ERRORLENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Check the cqr */
847 rc = dasd_check_cqr(cqr);
848 if (rc)
849 return rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100850 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (cqr->retries < 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100852 /* internal error 14 - start_IO run out of retries */
853 sprintf(errorstring, "14 %p", cqr);
854 dev_err(&device->cdev->dev, "An error occurred in the DASD "
855 "device driver, reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100856 cqr->status = DASD_CQR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EIO;
858 }
859 cqr->startclk = get_clock();
860 cqr->starttime = jiffies;
861 cqr->retries--;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100862 if (cqr->cpmode == 1) {
863 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
864 (long) cqr, cqr->lpm);
865 } else {
866 rc = ccw_device_start(device->cdev, cqr->cpaddr,
867 (long) cqr, cqr->lpm, 0);
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 switch (rc) {
870 case 0:
871 cqr->status = DASD_CQR_IN_IO;
872 DBF_DEV_EVENT(DBF_DEBUG, device,
873 "start_IO: request %p started successful",
874 cqr);
875 break;
876 case -EBUSY:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100877 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 "start_IO: device busy, retry later");
879 break;
880 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100881 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 "start_IO: request timeout, retry later");
883 break;
884 case -EACCES:
885 /* -EACCES indicates that the request used only a
886 * subset of the available pathes and all these
887 * pathes are gone.
888 * Do a retry with all available pathes.
889 */
890 cqr->lpm = LPM_ANYPATH;
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100891 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 "start_IO: selected pathes gone,"
893 " retry on all pathes");
894 break;
895 case -ENODEV:
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100896 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
897 "start_IO: -ENODEV device gone, retry");
898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case -EIO:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100900 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100901 "start_IO: -EIO device gone, retry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100904 /* internal error 11 - unknown rc */
905 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
906 dev_err(&device->cdev->dev,
907 "An error occurred in the DASD device driver, "
908 "reason=%s\n", errorstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 BUG();
910 break;
911 }
912 return rc;
913}
914
915/*
916 * Timeout function for dasd devices. This is used for different purposes
917 * 1) missing interrupt handler for normal operation
918 * 2) delayed start of request where start_IO failed with -EBUSY
919 * 3) timeout for missing state change interrupts
920 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
921 * DASD_CQR_QUEUED for 2) and 3).
922 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100923static void dasd_device_timeout(unsigned long ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 unsigned long flags;
926 struct dasd_device *device;
927
928 device = (struct dasd_device *) ptr;
929 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
930 /* re-activate request queue */
931 device->stopped &= ~DASD_STOPPED_PENDING;
932 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100933 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937 * Setup timeout for a device in jiffies.
938 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100939void dasd_device_set_timer(struct dasd_device *device, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100941 if (expires == 0)
942 del_timer(&device->timer);
943 else
944 mod_timer(&device->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948 * Clear timeout for a device.
949 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100950void dasd_device_clear_timer(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Stefan Weinhuber48cae882009-02-11 10:37:31 +0100952 del_timer(&device->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100955static void dasd_handle_killed_request(struct ccw_device *cdev,
956 unsigned long intparm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct dasd_ccw_req *cqr;
959 struct dasd_device *device;
960
Stefan Weinhuberf16f5842008-05-15 16:52:36 +0200961 if (!intparm)
962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 cqr = (struct dasd_ccw_req *) intparm;
964 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100965 DBF_EVENT(DBF_DEBUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 "invalid status in handle_killed_request: "
967 "bus_id %s, status %02x",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200968 dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
970 }
971
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100972 device = (struct dasd_device *) cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200974 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100976 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
977 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 }
980
981 /* Schedule request to be retried. */
982 cqr->status = DASD_CQR_QUEUED;
983
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100984 dasd_device_clear_timer(device);
985 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 dasd_put_device(device);
987}
988
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100989void dasd_generic_handle_state_change(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800991 /* First of all start sense subsystem status request. */
992 dasd_eer_snss(device);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 device->stopped &= ~DASD_STOPPED_PENDING;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100995 dasd_schedule_device_bh(device);
996 if (device->block)
997 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
1001 * Interrupt handler for "normal" ssch-io based dasd devices.
1002 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001003void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1004 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct dasd_ccw_req *cqr, *next;
1007 struct dasd_device *device;
1008 unsigned long long now;
1009 int expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (IS_ERR(irb)) {
1012 switch (PTR_ERR(irb)) {
1013 case -EIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 break;
1015 case -ETIMEDOUT:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001016 DBF_EVENT(DBF_WARNING, "%s(%s): request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001017 __func__, dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001020 DBF_EVENT(DBF_WARNING, "%s(%s): unknown error %ld\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001021 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Stefan Weinhuberf16f5842008-05-15 16:52:36 +02001023 dasd_handle_killed_request(cdev, intparm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026
1027 now = get_clock();
1028
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001029 /* check for unsolicited interrupts */
1030 cqr = (struct dasd_ccw_req *) intparm;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001031 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1032 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1033 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001034 if (cqr && cqr->status == DASD_CQR_IN_IO)
1035 cqr->status = DASD_CQR_QUEUED;
Martin Schwidefskya00bfd72006-09-20 15:59:05 +02001036 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!IS_ERR(device)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001038 dasd_device_clear_timer(device);
1039 device->discipline->handle_unsolicited_interrupt(device,
1040 irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 dasd_put_device(device);
1042 }
1043 return;
1044 }
1045
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001046 device = (struct dasd_device *) cqr->startdev;
1047 if (!device ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001049 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1050 "bus_id %s", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return;
1052 }
1053
1054 /* Check for clear pending */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001055 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001056 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001057 cqr->status = DASD_CQR_CLEARED;
1058 dasd_device_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001059 wake_up(&dasd_flush_wq);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001060 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return;
1062 }
1063
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001064 /* check status - the request might have been killed by dyn detach */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (cqr->status != DASD_CQR_IN_IO) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001066 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1067 "status %02x", dev_name(&cdev->dev), cqr->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
1069 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001070
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001071 next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 expires = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01001073 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1074 scsw_cstat(&irb->scsw) == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001075 /* request was completed successfully */
1076 cqr->status = DASD_CQR_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 cqr->stopclk = now;
1078 /* Start first request on queue if possible -> fast_io. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001079 if (cqr->devlist.next != &device->ccw_queue) {
1080 next = list_entry(cqr->devlist.next,
1081 struct dasd_ccw_req, devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001083 } else { /* error */
1084 memcpy(&cqr->irb, irb, sizeof(struct irb));
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001085 /* log sense for every failed I/O to s390 debugfeature */
1086 dasd_log_sense_dbf(cqr, irb);
Horst Hummel9575bf22006-12-08 15:54:15 +01001087 if (device->features & DASD_FEATURE_ERPLOG) {
Horst Hummel9575bf22006-12-08 15:54:15 +01001088 dasd_log_sense(cqr, irb);
1089 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001090
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001091 /*
1092 * If we don't want complex ERP for this request, then just
1093 * reset this and retry it in the fastpath
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001094 */
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001095 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001096 cqr->retries > 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001097 if (cqr->lpm == LPM_ANYPATH)
1098 DBF_DEV_EVENT(DBF_DEBUG, device,
1099 "default ERP in fastpath "
1100 "(%i retries left)",
1101 cqr->retries);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001102 cqr->lpm = LPM_ANYPATH;
1103 cqr->status = DASD_CQR_QUEUED;
1104 next = cqr;
1105 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 cqr->status = DASD_CQR_ERROR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001107 }
1108 if (next && (next->status == DASD_CQR_QUEUED) &&
1109 (!device->stopped)) {
1110 if (device->discipline->start_IO(next) == 0)
1111 expires = next->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 if (expires != 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001114 dasd_device_set_timer(device, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001116 dasd_device_clear_timer(device);
1117 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001121 * If we have an error on a dasd_block layer request then we cancel
1122 * and return all further requests from the same dasd_block as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001124static void __dasd_device_recovery(struct dasd_device *device,
1125 struct dasd_ccw_req *ref_cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct list_head *l, *n;
1128 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001131 * only requeue request that came from the dasd_block layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001133 if (!ref_cqr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
Horst Hummelf24acd42005-05-01 08:58:59 -07001135
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001136 list_for_each_safe(l, n, &device->ccw_queue) {
1137 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1138 if (cqr->status == DASD_CQR_QUEUED &&
1139 ref_cqr->block == cqr->block) {
1140 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001142 }
1143};
1144
1145/*
1146 * Remove those ccw requests from the queue that need to be returned
1147 * to the upper layer.
1148 */
1149static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1150 struct list_head *final_queue)
1151{
1152 struct list_head *l, *n;
1153 struct dasd_ccw_req *cqr;
1154
1155 /* Process request with final status. */
1156 list_for_each_safe(l, n, &device->ccw_queue) {
1157 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1158
1159 /* Stop list processing at the first non-final request. */
1160 if (cqr->status == DASD_CQR_QUEUED ||
1161 cqr->status == DASD_CQR_IN_IO ||
1162 cqr->status == DASD_CQR_CLEAR_PENDING)
1163 break;
1164 if (cqr->status == DASD_CQR_ERROR) {
1165 __dasd_device_recovery(device, cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001167 /* Rechain finished requests to final queue */
1168 list_move_tail(&cqr->devlist, final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170}
1171
1172/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001173 * the cqrs from the final queue are returned to the upper layer
1174 * by setting a dasd_block state and calling the callback function
1175 */
1176static void __dasd_device_process_final_queue(struct dasd_device *device,
1177 struct list_head *final_queue)
1178{
1179 struct list_head *l, *n;
1180 struct dasd_ccw_req *cqr;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001181 struct dasd_block *block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001182 void (*callback)(struct dasd_ccw_req *, void *data);
1183 void *callback_data;
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001184 char errorstring[ERRORLENGTH];
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001185
1186 list_for_each_safe(l, n, final_queue) {
1187 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1188 list_del_init(&cqr->devlist);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001189 block = cqr->block;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001190 callback = cqr->callback;
1191 callback_data = cqr->callback_data;
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001192 if (block)
1193 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001194 switch (cqr->status) {
1195 case DASD_CQR_SUCCESS:
1196 cqr->status = DASD_CQR_DONE;
1197 break;
1198 case DASD_CQR_ERROR:
1199 cqr->status = DASD_CQR_NEED_ERP;
1200 break;
1201 case DASD_CQR_CLEARED:
1202 cqr->status = DASD_CQR_TERMINATED;
1203 break;
1204 default:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001205 /* internal error 12 - wrong cqr status*/
1206 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1207 dev_err(&device->cdev->dev,
1208 "An error occurred in the DASD device driver, "
1209 "reason=%s\n", errorstring);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001210 BUG();
1211 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001212 if (cqr->callback != NULL)
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001213 (callback)(cqr, callback_data);
Stefan Weinhuber03513bc2008-02-19 15:29:27 +01001214 if (block)
1215 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001216 }
1217}
1218
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * Take a look at the first request on the ccw queue and check
1221 * if it reached its expire time. If so, terminate the IO.
1222 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001223static void __dasd_device_check_expire(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct dasd_ccw_req *cqr;
1226
1227 if (list_empty(&device->ccw_queue))
1228 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001229 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel29145a62006-12-04 15:40:15 +01001230 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1231 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1232 if (device->discipline->term_IO(cqr) != 0) {
1233 /* Hmpf, try again in 5 sec */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001234 dev_err(&device->cdev->dev,
1235 "cqr %p timed out (%is) but cannot be "
1236 "ended, retrying in 5 s\n",
1237 cqr, (cqr->expires/HZ));
Stefan Haberland7dc1da92008-01-26 14:11:26 +01001238 cqr->expires += 5*HZ;
1239 dasd_device_set_timer(device, 5*HZ);
Horst Hummel29145a62006-12-04 15:40:15 +01001240 } else {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001241 dev_err(&device->cdev->dev,
1242 "cqr %p timed out (%is), %i retries "
1243 "remaining\n", cqr, (cqr->expires/HZ),
1244 cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 }
1247}
1248
1249/*
1250 * Take a look at the first request on the ccw queue and check
1251 * if it needs to be started.
1252 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001253static void __dasd_device_start_head(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 struct dasd_ccw_req *cqr;
1256 int rc;
1257
1258 if (list_empty(&device->ccw_queue))
1259 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001260 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001261 if (cqr->status != DASD_CQR_QUEUED)
1262 return;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001263 /* when device is stopped, return request to previous layer */
1264 if (device->stopped) {
1265 cqr->status = DASD_CQR_CLEARED;
1266 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001267 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001268 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001269
1270 rc = device->discipline->start_IO(cqr);
1271 if (rc == 0)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001272 dasd_device_set_timer(device, cqr->expires);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001273 else if (rc == -EACCES) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001274 dasd_schedule_device_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001275 } else
1276 /* Hmpf, try again in 1/2 sec */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001277 dasd_device_set_timer(device, 50);
Horst Hummel8f617012006-08-30 14:33:33 +02001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001281 * Go through all request on the dasd_device request queue,
1282 * terminate them on the cdev if necessary, and return them to the
1283 * submitting layer via callback.
1284 * Note:
1285 * Make sure that all 'submitting layers' still exist when
1286 * this function is called!. In other words, when 'device' is a base
1287 * device then all block layer requests must have been removed before
1288 * via dasd_flush_block_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001290int dasd_flush_device_queue(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001292 struct dasd_ccw_req *cqr, *n;
1293 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 INIT_LIST_HEAD(&flush_queue);
1297 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001298 rc = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001299 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
Horst Hummel8f617012006-08-30 14:33:33 +02001300 /* Check status and move request to flush_queue */
1301 switch (cqr->status) {
1302 case DASD_CQR_IN_IO:
1303 rc = device->discipline->term_IO(cqr);
1304 if (rc) {
1305 /* unable to terminate requeust */
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001306 dev_err(&device->cdev->dev,
1307 "Flushing the DASD request queue "
1308 "failed for request %p\n", cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001309 /* stop flush processing */
1310 goto finished;
1311 }
1312 break;
1313 case DASD_CQR_QUEUED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 cqr->stopclk = get_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001315 cqr->status = DASD_CQR_CLEARED;
Horst Hummel8f617012006-08-30 14:33:33 +02001316 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001317 default: /* no need to modify the others */
Horst Hummel8f617012006-08-30 14:33:33 +02001318 break;
1319 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001320 list_move_tail(&cqr->devlist, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001321 }
Horst Hummel8f617012006-08-30 14:33:33 +02001322finished:
1323 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001324 /*
1325 * After this point all requests must be in state CLEAR_PENDING,
1326 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1327 * one of the others.
1328 */
1329 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1330 wait_event(dasd_flush_wq,
1331 (cqr->status != DASD_CQR_CLEAR_PENDING));
1332 /*
1333 * Now set each request back to TERMINATED, DONE or NEED_ERP
1334 * and call the callback function of flushed requests
1335 */
1336 __dasd_device_process_final_queue(device, &flush_queue);
Horst Hummel8f617012006-08-30 14:33:33 +02001337 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340/*
1341 * Acquire the device lock and process queues for the device.
1342 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001343static void dasd_device_tasklet(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 struct list_head final_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 atomic_set (&device->tasklet_scheduled, 0);
1348 INIT_LIST_HEAD(&final_queue);
1349 spin_lock_irq(get_ccwdev_lock(device->cdev));
1350 /* Check expire time of first request on the ccw queue. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001351 __dasd_device_check_expire(device);
1352 /* find final requests on ccw queue */
1353 __dasd_device_process_ccw_queue(device, &final_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1355 /* Now call the callback function of requests with final status */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001356 __dasd_device_process_final_queue(device, &final_queue);
1357 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001359 __dasd_device_start_head(device);
1360 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 dasd_put_device(device);
1362}
1363
1364/*
1365 * Schedules a call to dasd_tasklet over the device tasklet.
1366 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001367void dasd_schedule_device_bh(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001370 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return;
1372 dasd_get_device(device);
1373 tasklet_hi_schedule(&device->tasklet);
1374}
1375
1376/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001377 * Queue a request to the head of the device ccw_queue.
1378 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001380void dasd_add_request_head(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct dasd_device *device;
1383 unsigned long flags;
1384
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001385 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001387 cqr->status = DASD_CQR_QUEUED;
1388 list_add(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001390 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1392}
1393
1394/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001395 * Queue a request to the tail of the device ccw_queue.
1396 * Start the I/O if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001398void dasd_add_request_tail(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct dasd_device *device;
1401 unsigned long flags;
1402
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001403 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001405 cqr->status = DASD_CQR_QUEUED;
1406 list_add_tail(&cqr->devlist, &device->ccw_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001408 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1410}
1411
1412/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001413 * Wakeup helper for the 'sleep_on' functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001415static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 wake_up((wait_queue_head_t *) data);
1418}
1419
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001420static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct dasd_device *device;
1423 int rc;
1424
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001425 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001427 rc = ((cqr->status == DASD_CQR_DONE ||
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001428 cqr->status == DASD_CQR_NEED_ERP ||
1429 cqr->status == DASD_CQR_TERMINATED) &&
1430 list_empty(&cqr->devlist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1432 return rc;
1433}
1434
1435/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001436 * Queue a request to the tail of the device ccw_queue and wait for
1437 * it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001439int dasd_sleep_on(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct dasd_device *device;
1442 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001443
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001444 device = cqr->startdev;
Horst Hummel138c0142006-06-29 14:58:12 +02001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001447 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001448 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001449 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001452 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return rc;
1454}
1455
1456/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001457 * Queue a request to the tail of the device ccw_queue and wait
1458 * interruptible for it's completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001460int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001463 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001465 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001467 cqr->callback_data = (void *) &generic_waitq;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001468 dasd_add_request_tail(cqr);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001469 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001470 if (rc == -ERESTARTSYS) {
1471 dasd_cancel_req(cqr);
1472 /* wait (non-interruptible) for final status */
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001473 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001475 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return rc;
1477}
1478
1479/*
1480 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1481 * for eckd devices) the currently running request has to be terminated
1482 * and be put back to status queued, before the special request is added
1483 * to the head of the queue. Then the special request is waited on normally.
1484 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001485static inline int _dasd_term_running_cqr(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (list_empty(&device->ccw_queue))
1490 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001491 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
Horst Hummel8f617012006-08-30 14:33:33 +02001492 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001495int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct dasd_device *device;
1498 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001499
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001500 device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 spin_lock_irq(get_ccwdev_lock(device->cdev));
1502 rc = _dasd_term_running_cqr(device);
1503 if (rc) {
1504 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1505 return rc;
1506 }
Horst Hummel138c0142006-06-29 14:58:12 +02001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 cqr->callback = dasd_wakeup_cb;
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001509 cqr->callback_data = (void *) &generic_waitq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cqr->status = DASD_CQR_QUEUED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001511 list_add(&cqr->devlist, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* let the bh start the request to keep them in order */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001514 dasd_schedule_device_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1517
Stefan Haberlandc80ee722008-05-30 10:03:31 +02001518 wait_event(generic_waitq, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* Request status is either done or failed. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001521 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return rc;
1523}
1524
1525/*
1526 * Cancels a request that was started with dasd_sleep_on_req.
1527 * This is useful to timeout requests. The request will be
1528 * terminated if it is currently in i/o.
1529 * Returns 1 if the request has been terminated.
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001530 * 0 if there was no need to terminate the request (not started yet)
1531 * negative error code if termination failed
1532 * Cancellation of a request is an asynchronous operation! The calling
1533 * function has to wait until the request is properly returned via callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001535int dasd_cancel_req(struct dasd_ccw_req *cqr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001537 struct dasd_device *device = cqr->startdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned long flags;
1539 int rc;
1540
1541 rc = 0;
1542 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1543 switch (cqr->status) {
1544 case DASD_CQR_QUEUED:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001545 /* request was not started - just set to cleared */
1546 cqr->status = DASD_CQR_CLEARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
1548 case DASD_CQR_IN_IO:
1549 /* request in IO - terminate IO and release again */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001550 rc = device->discipline->term_IO(cqr);
1551 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001552 dev_err(&device->cdev->dev,
1553 "Cancelling request %p failed with rc=%d\n",
1554 cqr, rc);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001555 } else {
1556 cqr->stopclk = get_clock();
1557 rc = 1;
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001560 default: /* already finished or clear pending - do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001564 dasd_schedule_device_bh(device);
1565 return rc;
1566}
1567
1568
1569/*
1570 * SECTION: Operations of the dasd_block layer.
1571 */
1572
1573/*
1574 * Timeout function for dasd_block. This is used when the block layer
1575 * is waiting for something that may not come reliably, (e.g. a state
1576 * change interrupt)
1577 */
1578static void dasd_block_timeout(unsigned long ptr)
1579{
1580 unsigned long flags;
1581 struct dasd_block *block;
1582
1583 block = (struct dasd_block *) ptr;
1584 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1585 /* re-activate request queue */
1586 block->base->stopped &= ~DASD_STOPPED_PENDING;
1587 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1588 dasd_schedule_block_bh(block);
1589}
1590
1591/*
1592 * Setup timeout for a dasd_block in jiffies.
1593 */
1594void dasd_block_set_timer(struct dasd_block *block, int expires)
1595{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001596 if (expires == 0)
1597 del_timer(&block->timer);
1598 else
1599 mod_timer(&block->timer, jiffies + expires);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001600}
1601
1602/*
1603 * Clear timeout for a dasd_block.
1604 */
1605void dasd_block_clear_timer(struct dasd_block *block)
1606{
Stefan Weinhuber48cae882009-02-11 10:37:31 +01001607 del_timer(&block->timer);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001608}
1609
1610/*
1611 * posts the buffer_cache about a finalized request
1612 */
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001613static inline void dasd_end_request(struct request *req, int error)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001614{
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001615 if (__blk_end_request(req, error, blk_rq_bytes(req)))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001616 BUG();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001617}
1618
1619/*
1620 * Process finished error recovery ccw.
1621 */
1622static inline void __dasd_block_process_erp(struct dasd_block *block,
1623 struct dasd_ccw_req *cqr)
1624{
1625 dasd_erp_fn_t erp_fn;
1626 struct dasd_device *device = block->base;
1627
1628 if (cqr->status == DASD_CQR_DONE)
1629 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1630 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01001631 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001632 erp_fn = device->discipline->erp_postaction(cqr);
1633 erp_fn(cqr);
1634}
1635
1636/*
1637 * Fetch requests from the block device queue.
1638 */
1639static void __dasd_process_request_queue(struct dasd_block *block)
1640{
1641 struct request_queue *queue;
1642 struct request *req;
1643 struct dasd_ccw_req *cqr;
1644 struct dasd_device *basedev;
1645 unsigned long flags;
1646 queue = block->request_queue;
1647 basedev = block->base;
1648 /* No queue ? Then there is nothing to do. */
1649 if (queue == NULL)
1650 return;
1651
1652 /*
1653 * We requeue request from the block device queue to the ccw
1654 * queue only in two states. In state DASD_STATE_READY the
1655 * partition detection is done and we need to requeue requests
1656 * for that. State DASD_STATE_ONLINE is normal block device
1657 * operation.
1658 */
1659 if (basedev->state < DASD_STATE_READY)
1660 return;
1661 /* Now we try to fetch requests from the request queue */
1662 while (!blk_queue_plugged(queue) &&
1663 elv_next_request(queue)) {
1664
1665 req = elv_next_request(queue);
1666
1667 if (basedev->features & DASD_FEATURE_READONLY &&
1668 rq_data_dir(req) == WRITE) {
1669 DBF_DEV_EVENT(DBF_ERR, basedev,
1670 "Rejecting write request %p",
1671 req);
1672 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001673 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001674 continue;
1675 }
1676 cqr = basedev->discipline->build_cp(basedev, block, req);
1677 if (IS_ERR(cqr)) {
1678 if (PTR_ERR(cqr) == -EBUSY)
1679 break; /* normal end condition */
1680 if (PTR_ERR(cqr) == -ENOMEM)
1681 break; /* terminate request queue loop */
1682 if (PTR_ERR(cqr) == -EAGAIN) {
1683 /*
1684 * The current request cannot be build right
1685 * now, we have to try later. If this request
1686 * is the head-of-queue we stop the device
1687 * for 1/2 second.
1688 */
1689 if (!list_empty(&block->ccw_queue))
1690 break;
1691 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1692 basedev->stopped |= DASD_STOPPED_PENDING;
1693 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1694 dasd_block_set_timer(block, HZ/2);
1695 break;
1696 }
1697 DBF_DEV_EVENT(DBF_ERR, basedev,
1698 "CCW creation failed (rc=%ld) "
1699 "on request %p",
1700 PTR_ERR(cqr), req);
1701 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001702 dasd_end_request(req, -EIO);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001703 continue;
1704 }
1705 /*
1706 * Note: callback is set to dasd_return_cqr_cb in
1707 * __dasd_block_start_head to cover erp requests as well
1708 */
1709 cqr->callback_data = (void *) req;
1710 cqr->status = DASD_CQR_FILLED;
1711 blkdev_dequeue_request(req);
1712 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1713 dasd_profile_start(block, cqr, req);
1714 }
1715}
1716
1717static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1718{
1719 struct request *req;
1720 int status;
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001721 int error = 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001722
1723 req = (struct request *) cqr->callback_data;
1724 dasd_profile_end(cqr->block, cqr, req);
Stefan Weinhuberfe6b8e72008-02-05 16:50:47 +01001725 status = cqr->block->base->discipline->free_cp(cqr, req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01001726 if (status <= 0)
1727 error = status ? status : -EIO;
1728 dasd_end_request(req, error);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001729}
1730
1731/*
1732 * Process ccw request queue.
1733 */
1734static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1735 struct list_head *final_queue)
1736{
1737 struct list_head *l, *n;
1738 struct dasd_ccw_req *cqr;
1739 dasd_erp_fn_t erp_fn;
1740 unsigned long flags;
1741 struct dasd_device *base = block->base;
1742
1743restart:
1744 /* Process request with final status. */
1745 list_for_each_safe(l, n, &block->ccw_queue) {
1746 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1747 if (cqr->status != DASD_CQR_DONE &&
1748 cqr->status != DASD_CQR_FAILED &&
1749 cqr->status != DASD_CQR_NEED_ERP &&
1750 cqr->status != DASD_CQR_TERMINATED)
1751 continue;
1752
1753 if (cqr->status == DASD_CQR_TERMINATED) {
1754 base->discipline->handle_terminated_request(cqr);
1755 goto restart;
1756 }
1757
1758 /* Process requests that may be recovered */
1759 if (cqr->status == DASD_CQR_NEED_ERP) {
Stefan Haberland6c5f57c2008-02-05 16:50:46 +01001760 erp_fn = base->discipline->erp_action(cqr);
1761 erp_fn(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001762 goto restart;
1763 }
1764
Stefan Haberlanda9cffb22008-11-14 18:18:08 +01001765 /* log sense for fatal error */
1766 if (cqr->status == DASD_CQR_FAILED) {
1767 dasd_log_sense(cqr, &cqr->irb);
1768 }
1769
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001770 /* First of all call extended error reporting. */
1771 if (dasd_eer_enabled(base) &&
1772 cqr->status == DASD_CQR_FAILED) {
1773 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1774
1775 /* restart request */
1776 cqr->status = DASD_CQR_FILLED;
1777 cqr->retries = 255;
1778 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1779 base->stopped |= DASD_STOPPED_QUIESCE;
1780 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1781 flags);
1782 goto restart;
1783 }
1784
1785 /* Process finished ERP request. */
1786 if (cqr->refers) {
1787 __dasd_block_process_erp(block, cqr);
1788 goto restart;
1789 }
1790
1791 /* Rechain finished requests to final queue */
1792 cqr->endclk = get_clock();
1793 list_move_tail(&cqr->blocklist, final_queue);
1794 }
1795}
1796
1797static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1798{
1799 dasd_schedule_block_bh(cqr->block);
1800}
1801
1802static void __dasd_block_start_head(struct dasd_block *block)
1803{
1804 struct dasd_ccw_req *cqr;
1805
1806 if (list_empty(&block->ccw_queue))
1807 return;
1808 /* We allways begin with the first requests on the queue, as some
1809 * of previously started requests have to be enqueued on a
1810 * dasd_device again for error recovery.
1811 */
1812 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1813 if (cqr->status != DASD_CQR_FILLED)
1814 continue;
1815 /* Non-temporary stop condition will trigger fail fast */
1816 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1817 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1818 (!dasd_eer_enabled(block->base))) {
1819 cqr->status = DASD_CQR_FAILED;
1820 dasd_schedule_block_bh(block);
1821 continue;
1822 }
1823 /* Don't try to start requests if device is stopped */
1824 if (block->base->stopped)
1825 return;
1826
1827 /* just a fail safe check, should not happen */
1828 if (!cqr->startdev)
1829 cqr->startdev = block->base;
1830
1831 /* make sure that the requests we submit find their way back */
1832 cqr->callback = dasd_return_cqr_cb;
1833
1834 dasd_add_request_tail(cqr);
1835 }
1836}
1837
1838/*
1839 * Central dasd_block layer routine. Takes requests from the generic
1840 * block layer request queue, creates ccw requests, enqueues them on
1841 * a dasd_device and processes ccw requests that have been returned.
1842 */
1843static void dasd_block_tasklet(struct dasd_block *block)
1844{
1845 struct list_head final_queue;
1846 struct list_head *l, *n;
1847 struct dasd_ccw_req *cqr;
1848
1849 atomic_set(&block->tasklet_scheduled, 0);
1850 INIT_LIST_HEAD(&final_queue);
1851 spin_lock(&block->queue_lock);
1852 /* Finish off requests on ccw queue */
1853 __dasd_process_block_ccw_queue(block, &final_queue);
1854 spin_unlock(&block->queue_lock);
1855 /* Now call the callback function of requests with final status */
1856 spin_lock_irq(&block->request_queue_lock);
1857 list_for_each_safe(l, n, &final_queue) {
1858 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1859 list_del_init(&cqr->blocklist);
1860 __dasd_cleanup_cqr(cqr);
1861 }
1862 spin_lock(&block->queue_lock);
1863 /* Get new request from the block device request queue */
1864 __dasd_process_request_queue(block);
1865 /* Now check if the head of the ccw queue needs to be started. */
1866 __dasd_block_start_head(block);
1867 spin_unlock(&block->queue_lock);
1868 spin_unlock_irq(&block->request_queue_lock);
1869 dasd_put_device(block->base);
1870}
1871
1872static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1873{
1874 wake_up(&dasd_flush_wq);
1875}
1876
1877/*
1878 * Go through all request on the dasd_block request queue, cancel them
1879 * on the respective dasd_device, and return them to the generic
1880 * block layer.
1881 */
1882static int dasd_flush_block_queue(struct dasd_block *block)
1883{
1884 struct dasd_ccw_req *cqr, *n;
1885 int rc, i;
1886 struct list_head flush_queue;
1887
1888 INIT_LIST_HEAD(&flush_queue);
1889 spin_lock_bh(&block->queue_lock);
1890 rc = 0;
1891restart:
1892 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1893 /* if this request currently owned by a dasd_device cancel it */
1894 if (cqr->status >= DASD_CQR_QUEUED)
1895 rc = dasd_cancel_req(cqr);
1896 if (rc < 0)
1897 break;
1898 /* Rechain request (including erp chain) so it won't be
1899 * touched by the dasd_block_tasklet anymore.
1900 * Replace the callback so we notice when the request
1901 * is returned from the dasd_device layer.
1902 */
1903 cqr->callback = _dasd_wake_block_flush_cb;
1904 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1905 list_move_tail(&cqr->blocklist, &flush_queue);
1906 if (i > 1)
1907 /* moved more than one request - need to restart */
1908 goto restart;
1909 }
1910 spin_unlock_bh(&block->queue_lock);
1911 /* Now call the callback function of flushed requests */
1912restart_cb:
1913 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1914 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1915 /* Process finished ERP request. */
1916 if (cqr->refers) {
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001917 spin_lock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001918 __dasd_block_process_erp(block, cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001919 spin_unlock_bh(&block->queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001920 /* restart list_for_xx loop since dasd_process_erp
1921 * might remove multiple elements */
1922 goto restart_cb;
1923 }
1924 /* call the callback function */
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001925 spin_lock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001926 cqr->endclk = get_clock();
1927 list_del_init(&cqr->blocklist);
1928 __dasd_cleanup_cqr(cqr);
Stefan Haberland0cd4bd42008-12-25 13:38:54 +01001929 spin_unlock_irq(&block->request_queue_lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return rc;
1932}
1933
1934/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001935 * Schedules a call to dasd_tasklet over the device tasklet.
1936 */
1937void dasd_schedule_block_bh(struct dasd_block *block)
1938{
1939 /* Protect against rescheduling. */
1940 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1941 return;
1942 /* life cycle of block is bound to it's base device */
1943 dasd_get_device(block->base);
1944 tasklet_hi_schedule(&block->tasklet);
1945}
1946
1947
1948/*
1949 * SECTION: external block device operations
1950 * (request queue handling, open, release, etc.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
1952
1953/*
1954 * Dasd request queue function. Called from ll_rw_blk.c
1955 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001956static void do_dasd_request(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001958 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001960 block = queue->queuedata;
1961 spin_lock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* Get new request from the block device request queue */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001963 __dasd_process_request_queue(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* Now check if the head of the ccw queue needs to be started. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001965 __dasd_block_start_head(block);
1966 spin_unlock(&block->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969/*
1970 * Allocate and initialize request queue and default I/O scheduler.
1971 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001972static int dasd_alloc_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 int rc;
1975
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001976 block->request_queue = blk_init_queue(do_dasd_request,
1977 &block->request_queue_lock);
1978 if (block->request_queue == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return -ENOMEM;
1980
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001981 block->request_queue->queuedata = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001983 elevator_exit(block->request_queue->elevator);
Josef 'Jeff' Sipek08a8a0c2008-04-17 07:45:56 +02001984 block->request_queue->elevator = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001985 rc = elevator_init(block->request_queue, "deadline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (rc) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001987 blk_cleanup_queue(block->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return rc;
1989 }
1990 return 0;
1991}
1992
1993/*
1994 * Allocate and initialize request queue.
1995 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001996static void dasd_setup_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 int max;
1999
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002000 blk_queue_hardsect_size(block->request_queue, block->bp_block);
2001 max = block->base->discipline->max_blocks << block->s2b_shift;
2002 blk_queue_max_sectors(block->request_queue, max);
2003 blk_queue_max_phys_segments(block->request_queue, -1L);
2004 blk_queue_max_hw_segments(block->request_queue, -1L);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002005 /* with page sized segments we can translate each segement into
2006 * one idaw/tidaw
2007 */
2008 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2009 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002010 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013/*
2014 * Deactivate and free request queue.
2015 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002016static void dasd_free_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002018 if (block->request_queue) {
2019 blk_cleanup_queue(block->request_queue);
2020 block->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022}
2023
2024/*
2025 * Flush request on the request queue.
2026 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002027static void dasd_flush_request_queue(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct request *req;
2030
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002031 if (!block->request_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return;
Horst Hummel138c0142006-06-29 14:58:12 +02002033
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002034 spin_lock_irq(&block->request_queue_lock);
2035 while ((req = elv_next_request(block->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 blkdev_dequeue_request(req);
Kiyoshi Ueda4c4e2142008-01-28 10:29:42 +01002037 dasd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002039 spin_unlock_irq(&block->request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Al Viro57a7c0b2008-03-02 10:36:08 -05002042static int dasd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Al Viro57a7c0b2008-03-02 10:36:08 -05002044 struct dasd_block *block = bdev->bd_disk->private_data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002045 struct dasd_device *base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int rc;
2047
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002048 atomic_inc(&block->open_count);
2049 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rc = -ENODEV;
2051 goto unlock;
2052 }
2053
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002054 if (!try_module_get(base->discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rc = -EINVAL;
2056 goto unlock;
2057 }
2058
2059 if (dasd_probeonly) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002060 dev_info(&base->cdev->dev,
2061 "Accessing the DASD failed because it is in "
2062 "probeonly mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 rc = -EPERM;
2064 goto out;
2065 }
2066
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002067 if (base->state <= DASD_STATE_BASIC) {
2068 DBF_DEV_EVENT(DBF_ERR, base, " %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 " Cannot open unrecognized device");
2070 rc = -ENODEV;
2071 goto out;
2072 }
2073
2074 return 0;
2075
2076out:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002077 module_put(base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078unlock:
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002079 atomic_dec(&block->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return rc;
2081}
2082
Al Viro57a7c0b2008-03-02 10:36:08 -05002083static int dasd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002085 struct dasd_block *block = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002087 atomic_dec(&block->open_count);
2088 module_put(block->base->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 0;
2090}
2091
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002092/*
2093 * Return disk geometry.
2094 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002095static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002096{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002097 struct dasd_block *block;
2098 struct dasd_device *base;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002099
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002100 block = bdev->bd_disk->private_data;
2101 base = block->base;
2102 if (!block)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002103 return -ENODEV;
2104
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002105 if (!base->discipline ||
2106 !base->discipline->fill_geometry)
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002107 return -EINVAL;
2108
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002109 base->discipline->fill_geometry(block, geo);
2110 geo->start = get_start_sect(bdev) >> block->s2b_shift;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002111 return 0;
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114struct block_device_operations
2115dasd_device_operations = {
2116 .owner = THIS_MODULE,
Al Viro57a7c0b2008-03-02 10:36:08 -05002117 .open = dasd_open,
2118 .release = dasd_release,
Heiko Carstens0000d032009-03-26 15:23:45 +01002119 .ioctl = dasd_ioctl,
2120 .compat_ioctl = dasd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08002121 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122};
2123
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002124/*******************************************************************************
2125 * end of block device operations
2126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128static void
2129dasd_exit(void)
2130{
2131#ifdef CONFIG_PROC_FS
2132 dasd_proc_exit();
2133#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002134 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07002135 if (dasd_page_cache != NULL) {
2136 kmem_cache_destroy(dasd_page_cache);
2137 dasd_page_cache = NULL;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 dasd_gendisk_exit();
2140 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (dasd_debug_area != NULL) {
2142 debug_unregister(dasd_debug_area);
2143 dasd_debug_area = NULL;
2144 }
2145}
2146
2147/*
2148 * SECTION: common functions for ccw_driver use
2149 */
2150
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002151/*
2152 * Initial attempt at a probe function. this can be simplified once
2153 * the other detection code is gone.
2154 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002155int dasd_generic_probe(struct ccw_device *cdev,
2156 struct dasd_discipline *discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 int ret;
2159
Horst Hummel40545572006-06-29 15:08:18 +02002160 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2161 if (ret) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002162 DBF_EVENT(DBF_WARNING,
Horst Hummel40545572006-06-29 15:08:18 +02002163 "dasd_generic_probe: could not set ccw-device options "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002164 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002165 return ret;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 ret = dasd_add_sysfs_files(cdev);
2168 if (ret) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002169 DBF_EVENT(DBF_WARNING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 "dasd_generic_probe: could not add sysfs entries "
Kay Sievers2a0217d2008-10-10 21:33:09 +02002171 "for %s\n", dev_name(&cdev->dev));
Horst Hummel40545572006-06-29 15:08:18 +02002172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Horst Hummel40545572006-06-29 15:08:18 +02002174 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Horst Hummel40545572006-06-29 15:08:18 +02002176 /*
2177 * Automatically online either all dasd devices (dasd_autodetect)
2178 * or all devices specified with dasd= parameters during
2179 * initial probe.
2180 */
2181 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
Kay Sievers2a0217d2008-10-10 21:33:09 +02002182 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
Horst Hummel40545572006-06-29 15:08:18 +02002183 ret = ccw_device_set_online(cdev);
2184 if (ret)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002185 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002186 dev_name(&cdev->dev), ret);
Stefan Haberlandde3e0da2008-01-26 14:11:08 +01002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002190/*
2191 * This will one day be called from a global not_oper handler.
2192 * It is also used by driver_unregister during module unload.
2193 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002194void dasd_generic_remove(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002197 struct dasd_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Horst Hummel59afda72005-05-16 21:53:39 -07002199 cdev->handler = NULL;
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 dasd_remove_sysfs_files(cdev);
2202 device = dasd_device_from_cdev(cdev);
2203 if (IS_ERR(device))
2204 return;
2205 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2206 /* Already doing offline processing */
2207 dasd_put_device(device);
2208 return;
2209 }
2210 /*
2211 * This device is removed unconditionally. Set offline
2212 * flag to prevent dasd_open from opening it while it is
2213 * no quite down yet.
2214 */
2215 dasd_set_target_state(device, DASD_STATE_NEW);
2216 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002217 block = device->block;
2218 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002220 /*
2221 * life cycle of block is bound to device, so delete it after
2222 * device was safely removed
2223 */
2224 if (block)
2225 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002228/*
2229 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002231 * or the user has started activation through sysfs.
2232 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002233int dasd_generic_set_online(struct ccw_device *cdev,
2234 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002236 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002238 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002239
Horst Hummel40545572006-06-29 15:08:18 +02002240 /* first online clears initial online feature flag */
2241 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 device = dasd_create_device(cdev);
2243 if (IS_ERR(device))
2244 return PTR_ERR(device);
2245
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002246 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002247 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!dasd_diag_discipline_pointer) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002249 pr_warning("%s Setting the DASD online failed because "
2250 "of missing DIAG discipline\n",
2251 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) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002272 pr_warning("%s Setting the DASD online with discipline %s "
2273 "failed with rc=%i\n",
2274 dev_name(&cdev->dev), discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002275 module_put(discipline->owner);
2276 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 dasd_delete_device(device);
2278 return rc;
2279 }
2280
2281 dasd_set_target_state(device, DASD_STATE_ONLINE);
2282 if (device->state <= DASD_STATE_KNOWN) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002283 pr_warning("%s Setting the DASD online failed because of a "
2284 "missing discipline\n", dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 rc = -ENODEV;
2286 dasd_set_target_state(device, DASD_STATE_NEW);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002287 if (device->block)
2288 dasd_free_block(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 dasd_delete_device(device);
2290 } else
2291 pr_debug("dasd_generic device %s found\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02002292 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* FIXME: we have to wait for the root device but we don't want
2295 * to wait for each single device but for all at once. */
2296 wait_event(dasd_init_waitq, _wait_for_device(device));
2297
2298 dasd_put_device(device);
2299
2300 return rc;
2301}
2302
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002303int dasd_generic_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct dasd_device *device;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002306 struct dasd_block *block;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002307 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 device = dasd_device_from_cdev(cdev);
2310 if (IS_ERR(device))
2311 return PTR_ERR(device);
2312 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2313 /* Already doing offline processing */
2314 dasd_put_device(device);
2315 return 0;
2316 }
2317 /*
2318 * We must make sure that this device is currently not in use.
2319 * The open_count is increased for every opener, that includes
2320 * the blkdev_get in dasd_scan_partitions. We are only interested
2321 * in the other openers.
2322 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002323 if (device->block) {
Heiko Carstensa8061702008-04-17 07:46:26 +02002324 max_count = device->block->bdev ? 0 : -1;
2325 open_count = atomic_read(&device->block->open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002326 if (open_count > max_count) {
2327 if (open_count > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002328 pr_warning("%s: The DASD cannot be set offline "
2329 "with open count %i\n",
2330 dev_name(&cdev->dev), open_count);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002331 else
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002332 pr_warning("%s: The DASD cannot be set offline "
2333 "while it is in use\n",
2334 dev_name(&cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002335 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2336 dasd_put_device(device);
2337 return -EBUSY;
2338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 dasd_set_target_state(device, DASD_STATE_NEW);
2341 /* dasd_delete_device destroys the device reference. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002342 block = device->block;
2343 device->block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 dasd_delete_device(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002345 /*
2346 * life cycle of block is bound to device, so delete it after
2347 * device was safely removed
2348 */
2349 if (block)
2350 dasd_free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 0;
2352}
2353
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002354int dasd_generic_notify(struct ccw_device *cdev, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 struct dasd_device *device;
2357 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 int ret;
2359
Peter Oberparleiter91c36912008-08-21 19:46:39 +02002360 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (IS_ERR(device))
2362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 ret = 0;
2364 switch (event) {
2365 case CIO_GONE:
Sebastian Ott47593bf2009-03-31 19:16:05 +02002366 case CIO_BOXED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002368 /* First of all call extended error reporting. */
2369 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (device->state < DASD_STATE_BASIC)
2372 break;
2373 /* Device is active. We want to keep it. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002374 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2375 if (cqr->status == DASD_CQR_IN_IO) {
2376 cqr->status = DASD_CQR_QUEUED;
2377 cqr->retries++;
2378 }
2379 device->stopped |= DASD_STOPPED_DC_WAIT;
2380 dasd_device_clear_timer(device);
2381 dasd_schedule_device_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 ret = 1;
2383 break;
2384 case CIO_OPER:
2385 /* FIXME: add a sanity check. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002386 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2387 dasd_schedule_device_bh(device);
2388 if (device->block)
2389 dasd_schedule_block_bh(device->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ret = 1;
2391 break;
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 dasd_put_device(device);
2394 return ret;
2395}
2396
Heiko Carstens763968e2007-05-10 15:45:46 +02002397static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2398 void *rdc_buffer,
2399 int rdc_buffer_size,
2400 char *magic)
Cornelia Huck17283b52007-05-04 18:47:51 +02002401{
2402 struct dasd_ccw_req *cqr;
2403 struct ccw1 *ccw;
2404
2405 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2406
2407 if (IS_ERR(cqr)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002408 /* internal error 13 - Allocating the RDC request failed*/
2409 dev_err(&device->cdev->dev,
2410 "An error occurred in the DASD device driver, "
2411 "reason=%s\n", "13");
Cornelia Huck17283b52007-05-04 18:47:51 +02002412 return cqr;
2413 }
2414
2415 ccw = cqr->cpaddr;
2416 ccw->cmd_code = CCW_CMD_RDC;
2417 ccw->cda = (__u32)(addr_t)rdc_buffer;
2418 ccw->count = rdc_buffer_size;
2419
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002420 cqr->startdev = device;
2421 cqr->memdev = device;
Cornelia Huck17283b52007-05-04 18:47:51 +02002422 cqr->expires = 10*HZ;
2423 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2424 cqr->retries = 2;
2425 cqr->buildclk = get_clock();
2426 cqr->status = DASD_CQR_FILLED;
2427 return cqr;
2428}
2429
2430
2431int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2432 void **rdc_buffer, int rdc_buffer_size)
2433{
2434 int ret;
2435 struct dasd_ccw_req *cqr;
2436
2437 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2438 magic);
2439 if (IS_ERR(cqr))
2440 return PTR_ERR(cqr);
2441
2442 ret = dasd_sleep_on(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002443 dasd_sfree_request(cqr, cqr->memdev);
Cornelia Huck17283b52007-05-04 18:47:51 +02002444 return ret;
2445}
Cornelia Huckaaff0f62007-05-10 15:45:45 +02002446EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002447
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +01002448/*
2449 * In command mode and transport mode we need to look for sense
2450 * data in different places. The sense data itself is allways
2451 * an array of 32 bytes, so we can unify the sense data access
2452 * for both modes.
2453 */
2454char *dasd_get_sense(struct irb *irb)
2455{
2456 struct tsb *tsb = NULL;
2457 char *sense = NULL;
2458
2459 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2460 if (irb->scsw.tm.tcw)
2461 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2462 irb->scsw.tm.tcw);
2463 if (tsb && tsb->length == 64 && tsb->flags)
2464 switch (tsb->flags & 0x07) {
2465 case 1: /* tsa_iostat */
2466 sense = tsb->tsa.iostat.sense;
2467 break;
2468 case 2: /* tsa_ddpc */
2469 sense = tsb->tsa.ddpc.sense;
2470 break;
2471 default:
2472 /* currently we don't use interrogate data */
2473 break;
2474 }
2475 } else if (irb->esw.esw0.erw.cons) {
2476 sense = irb->ecw;
2477 }
2478 return sense;
2479}
2480EXPORT_SYMBOL_GPL(dasd_get_sense);
2481
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002482static int __init dasd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 int rc;
2485
2486 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002487 init_waitqueue_head(&dasd_flush_wq);
Stefan Haberlandc80ee722008-05-30 10:03:31 +02002488 init_waitqueue_head(&generic_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Peter Tiedemann361f4942008-01-26 14:11:30 +01002491 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (dasd_debug_area == NULL) {
2493 rc = -ENOMEM;
2494 goto failed;
2495 }
2496 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002497 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2500
2501 dasd_diag_discipline_pointer = NULL;
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 rc = dasd_devmap_init();
2504 if (rc)
2505 goto failed;
2506 rc = dasd_gendisk_init();
2507 if (rc)
2508 goto failed;
2509 rc = dasd_parse();
2510 if (rc)
2511 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002512 rc = dasd_eer_init();
2513 if (rc)
2514 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515#ifdef CONFIG_PROC_FS
2516 rc = dasd_proc_init();
2517 if (rc)
2518 goto failed;
2519#endif
2520
2521 return 0;
2522failed:
Stefan Haberlandfc19f382009-03-26 15:23:49 +01002523 pr_info("The DASD device driver could not be initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 dasd_exit();
2525 return rc;
2526}
2527
2528module_init(dasd_init);
2529module_exit(dasd_exit);
2530
2531EXPORT_SYMBOL(dasd_debug_area);
2532EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2533
2534EXPORT_SYMBOL(dasd_add_request_head);
2535EXPORT_SYMBOL(dasd_add_request_tail);
2536EXPORT_SYMBOL(dasd_cancel_req);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002537EXPORT_SYMBOL(dasd_device_clear_timer);
2538EXPORT_SYMBOL(dasd_block_clear_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539EXPORT_SYMBOL(dasd_enable_device);
2540EXPORT_SYMBOL(dasd_int_handler);
2541EXPORT_SYMBOL(dasd_kfree_request);
2542EXPORT_SYMBOL(dasd_kick_device);
2543EXPORT_SYMBOL(dasd_kmalloc_request);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002544EXPORT_SYMBOL(dasd_schedule_device_bh);
2545EXPORT_SYMBOL(dasd_schedule_block_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546EXPORT_SYMBOL(dasd_set_target_state);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002547EXPORT_SYMBOL(dasd_device_set_timer);
2548EXPORT_SYMBOL(dasd_block_set_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549EXPORT_SYMBOL(dasd_sfree_request);
2550EXPORT_SYMBOL(dasd_sleep_on);
2551EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2552EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2553EXPORT_SYMBOL(dasd_smalloc_request);
2554EXPORT_SYMBOL(dasd_start_IO);
2555EXPORT_SYMBOL(dasd_term_IO);
2556
2557EXPORT_SYMBOL_GPL(dasd_generic_probe);
2558EXPORT_SYMBOL_GPL(dasd_generic_remove);
2559EXPORT_SYMBOL_GPL(dasd_generic_notify);
2560EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2561EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01002562EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2563EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2564EXPORT_SYMBOL_GPL(dasd_alloc_block);
2565EXPORT_SYMBOL_GPL(dasd_free_block);