Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[deliverable/linux.git] / drivers / s390 / block / dasd.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
10 */
11
fc19f381
SH
12#define KMSG_COMPONENT "dasd"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
1da177e4
LT
15#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>
a885c8c4 22#include <linux/hdreg.h>
1da177e4
LT
23
24#include <asm/ccwdev.h>
25#include <asm/ebcdic.h>
26#include <asm/idals.h>
27#include <asm/todclk.h>
f3eb5384 28#include <asm/itcw.h>
1da177e4
LT
29
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;
2b67fc46 44void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
1da177e4
LT
45
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");
1da177e4
LT
50MODULE_LICENSE("GPL");
51
52/*
53 * SECTION: prototypes for static functions of dasd.c
54 */
8e09f215
SW
55static 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 *);
4927b3f7 62static void do_kick_device(struct work_struct *);
8e09f215 63static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
48cae885
SW
64static void dasd_device_timeout(unsigned long);
65static void dasd_block_timeout(unsigned long);
1da177e4
LT
66
67/*
68 * SECTION: Operations on the device structure.
69 */
70static wait_queue_head_t dasd_init_waitq;
8f61701b 71static wait_queue_head_t dasd_flush_wq;
c80ee724 72static wait_queue_head_t generic_waitq;
1da177e4
LT
73
74/*
75 * Allocate memory for a new device structure.
76 */
8e09f215 77struct dasd_device *dasd_alloc_device(void)
1da177e4
LT
78{
79 struct dasd_device *device;
80
8e09f215
SW
81 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
82 if (!device)
1da177e4 83 return ERR_PTR(-ENOMEM);
1da177e4
LT
84
85 /* Get two pages for normal block device operations. */
86 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
8e09f215 87 if (!device->ccw_mem) {
1da177e4
LT
88 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);
8e09f215 93 if (!device->erp_mem) {
1da177e4
LT
94 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);
8e09f215 102 atomic_set(&device->tasklet_scheduled, 0);
138c014d 103 tasklet_init(&device->tasklet,
8e09f215 104 (void (*)(unsigned long)) dasd_device_tasklet,
1da177e4
LT
105 (unsigned long) device);
106 INIT_LIST_HEAD(&device->ccw_queue);
107 init_timer(&device->timer);
48cae885
SW
108 device->timer.function = dasd_device_timeout;
109 device->timer.data = (unsigned long) device;
4927b3f7 110 INIT_WORK(&device->kick_work, do_kick_device);
1da177e4
LT
111 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 */
8e09f215 120void dasd_free_device(struct dasd_device *device)
1da177e4 121{
17fd682e 122 kfree(device->private);
1da177e4
LT
123 free_page((unsigned long) device->erp_mem);
124 free_pages((unsigned long) device->ccw_mem, 1);
125 kfree(device);
126}
127
8e09f215
SW
128/*
129 * 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);
48cae885
SW
149 block->timer.function = dasd_block_timeout;
150 block->timer.data = (unsigned long) block;
8e09f215
SW
151
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
1da177e4
LT
163/*
164 * Make a new device known to the system.
165 */
8e09f215 166static int dasd_state_new_to_known(struct dasd_device *device)
1da177e4
LT
167{
168 int rc;
169
170 /*
138c014d 171 * As long as the device is not in state DASD_STATE_NEW we want to
1da177e4
LT
172 * keep the reference count > 0.
173 */
174 dasd_get_device(device);
175
8e09f215
SW
176 if (device->block) {
177 rc = dasd_alloc_queue(device->block);
178 if (rc) {
179 dasd_put_device(device);
180 return rc;
181 }
1da177e4 182 }
1da177e4
LT
183 device->state = DASD_STATE_KNOWN;
184 return 0;
185}
186
187/*
188 * Let the system forget about a device.
189 */
8e09f215 190static int dasd_state_known_to_new(struct dasd_device *device)
1da177e4 191{
20c64468
SW
192 /* Disable extended error reporting for this device. */
193 dasd_eer_disable(device);
1da177e4 194 /* Forget the discipline information. */
8e09f215
SW
195 if (device->discipline) {
196 if (device->discipline->uncheck_device)
197 device->discipline->uncheck_device(device);
aa88861f 198 module_put(device->discipline->owner);
8e09f215 199 }
1da177e4 200 device->discipline = NULL;
aa88861f
PO
201 if (device->base_discipline)
202 module_put(device->base_discipline->owner);
203 device->base_discipline = NULL;
1da177e4
LT
204 device->state = DASD_STATE_NEW;
205
8e09f215
SW
206 if (device->block)
207 dasd_free_queue(device->block);
1da177e4
LT
208
209 /* Give up reference we took in dasd_state_new_to_known. */
210 dasd_put_device(device);
8f61701b 211 return 0;
1da177e4
LT
212}
213
214/*
215 * Request the irq line for the device.
216 */
8e09f215 217static int dasd_state_known_to_basic(struct dasd_device *device)
1da177e4
LT
218{
219 int rc;
220
221 /* Allocate and register gendisk structure. */
8e09f215
SW
222 if (device->block) {
223 rc = dasd_gendisk_alloc(device->block);
224 if (rc)
225 return rc;
226 }
1da177e4 227 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
fc19f381 228 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
8e09f215 229 8 * sizeof(long));
1da177e4 230 debug_register_view(device->debug_area, &debug_sprintf_view);
b0035f12 231 debug_set_level(device->debug_area, DBF_WARNING);
1da177e4
LT
232 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 */
8e09f215 241static int dasd_state_basic_to_known(struct dasd_device *device)
1da177e4 242{
8f61701b 243 int rc;
8e09f215
SW
244 if (device->block) {
245 dasd_gendisk_free(device->block);
246 dasd_block_clear_timer(device->block);
247 }
248 rc = dasd_flush_device_queue(device);
8f61701b
HH
249 if (rc)
250 return rc;
8e09f215 251 dasd_device_clear_timer(device);
8f61701b 252
1da177e4
LT
253 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;
8f61701b 259 return 0;
1da177e4
LT
260}
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.
90f0094d
HH
271 * 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).
1da177e4 275 */
8e09f215 276static int dasd_state_basic_to_ready(struct dasd_device *device)
1da177e4
LT
277{
278 int rc;
8e09f215 279 struct dasd_block *block;
1da177e4
LT
280
281 rc = 0;
8e09f215 282 block = device->block;
90f0094d 283 /* make disk known with correct capacity */
8e09f215
SW
284 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 }
90f0094d 302 return rc;
1da177e4
LT
303}
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 */
8e09f215 310static int dasd_state_ready_to_basic(struct dasd_device *device)
1da177e4 311{
8f61701b
HH
312 int rc;
313
1da177e4 314 device->state = DASD_STATE_BASIC;
8e09f215
SW
315 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 }
8f61701b 328 return 0;
1da177e4
LT
329}
330
90f0094d
HH
331/*
332 * Back to basic.
333 */
8e09f215 334static int dasd_state_unfmt_to_basic(struct dasd_device *device)
90f0094d
HH
335{
336 device->state = DASD_STATE_BASIC;
8f61701b 337 return 0;
90f0094d
HH
338}
339
1da177e4
LT
340/*
341 * 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 */
8f61701b 345static int
1da177e4
LT
346dasd_state_ready_to_online(struct dasd_device * device)
347{
8e09f215 348 int rc;
1301809b
SW
349 struct gendisk *disk;
350 struct disk_part_iter piter;
351 struct hd_struct *part;
8e09f215
SW
352
353 if (device->discipline->ready_to_online) {
354 rc = device->discipline->ready_to_online(device);
355 if (rc)
356 return rc;
357 }
1da177e4 358 device->state = DASD_STATE_ONLINE;
1301809b 359 if (device->block) {
8e09f215 360 dasd_schedule_block_bh(device->block);
1301809b
SW
361 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 }
1da177e4
LT
367 return 0;
368}
369
370/*
371 * Stop the requeueing of requests again.
372 */
8e09f215 373static int dasd_state_online_to_ready(struct dasd_device *device)
1da177e4 374{
8e09f215 375 int rc;
1301809b
SW
376 struct gendisk *disk;
377 struct disk_part_iter piter;
378 struct hd_struct *part;
8e09f215
SW
379
380 if (device->discipline->online_to_ready) {
381 rc = device->discipline->online_to_ready(device);
382 if (rc)
383 return rc;
384 }
1da177e4 385 device->state = DASD_STATE_READY;
1301809b
SW
386 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 }
8f61701b 393 return 0;
1da177e4
LT
394}
395
396/*
397 * Device startup state changes.
398 */
8e09f215 399static int dasd_increase_state(struct dasd_device *device)
1da177e4
LT
400{
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
39ccf95e
HH
418 if (!rc &&
419 device->state == DASD_STATE_UNFMT &&
420 device->target > DASD_STATE_UNFMT)
421 rc = -EPERM;
422
1da177e4
LT
423 if (!rc &&
424 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 */
8e09f215 434static int dasd_decrease_state(struct dasd_device *device)
1da177e4 435{
8f61701b
HH
436 int rc;
437
438 rc = 0;
1da177e4
LT
439 if (device->state == DASD_STATE_ONLINE &&
440 device->target <= DASD_STATE_READY)
8f61701b 441 rc = dasd_state_online_to_ready(device);
138c014d 442
8f61701b
HH
443 if (!rc &&
444 device->state == DASD_STATE_READY &&
1da177e4 445 device->target <= DASD_STATE_BASIC)
8f61701b 446 rc = dasd_state_ready_to_basic(device);
90f0094d 447
8f61701b
HH
448 if (!rc &&
449 device->state == DASD_STATE_UNFMT &&
90f0094d 450 device->target <= DASD_STATE_BASIC)
8f61701b 451 rc = dasd_state_unfmt_to_basic(device);
90f0094d 452
8f61701b
HH
453 if (!rc &&
454 device->state == DASD_STATE_BASIC &&
1da177e4 455 device->target <= DASD_STATE_KNOWN)
8f61701b 456 rc = dasd_state_basic_to_known(device);
138c014d 457
8f61701b
HH
458 if (!rc &&
459 device->state == DASD_STATE_KNOWN &&
1da177e4 460 device->target <= DASD_STATE_NEW)
8f61701b 461 rc = dasd_state_known_to_new(device);
1da177e4 462
8f61701b 463 return rc;
1da177e4
LT
464}
465
466/*
467 * This is the main startup/shutdown routine.
468 */
8e09f215 469static void dasd_change_state(struct dasd_device *device)
1da177e4
LT
470{
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);
4dfd5c45
HH
485
486 /* let user-space know that the device status changed */
487 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
1da177e4
LT
488}
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 */
8e09f215 496static void do_kick_device(struct work_struct *work)
1da177e4 497{
4927b3f7 498 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
1da177e4 499 dasd_change_state(device);
8e09f215 500 dasd_schedule_device_bh(device);
1da177e4
LT
501 dasd_put_device(device);
502}
503
8e09f215 504void dasd_kick_device(struct dasd_device *device)
1da177e4
LT
505{
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 */
8e09f215 514void dasd_set_target_state(struct dasd_device *device, int target)
1da177e4
LT
515{
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 */
8e09f215 531static inline int _wait_for_device(struct dasd_device *device)
1da177e4
LT
532{
533 return (device->state == device->target);
534}
535
8e09f215 536void dasd_enable_device(struct dasd_device *device)
1da177e4
LT
537{
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 */
8e09f215 557#define dasd_profile_counter(value, counter, block) \
1da177e4
LT
558{ \
559 int index; \
560 for (index = 0; index < 31 && value >> (2+index); index++); \
561 dasd_global_profile.counter[index]++; \
8e09f215 562 block->profile.counter[index]++; \
1da177e4
LT
563}
564
565/*
566 * Add profiling information for cqr before execution.
567 */
8e09f215
SW
568static void dasd_profile_start(struct dasd_block *block,
569 struct dasd_ccw_req *cqr,
570 struct request *req)
1da177e4
LT
571{
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;
8e09f215 580 list_for_each(l, &block->ccw_queue)
1da177e4
LT
581 if (++counter >= 31)
582 break;
583 dasd_global_profile.dasd_io_nr_req[counter]++;
8e09f215 584 block->profile.dasd_io_nr_req[counter]++;
1da177e4
LT
585}
586
587/*
588 * Add profiling information for cqr after execution.
589 */
8e09f215
SW
590static void dasd_profile_end(struct dasd_block *block,
591 struct dasd_ccw_req *cqr,
592 struct request *req)
1da177e4
LT
593{
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,
8e09f215 614 sizeof(struct dasd_profile_info_t));
1da177e4
LT
615 dasd_global_profile.dasd_io_reqs++;
616 dasd_global_profile.dasd_io_sects += sectors;
617
8e09f215
SW
618 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;
1da177e4 623
8e09f215
SW
624 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);
1da177e4
LT
631}
632#else
8e09f215
SW
633#define dasd_profile_start(block, cqr, req) do {} while (0)
634#define dasd_profile_end(block, cqr, req) do {} while (0)
1da177e4
LT
635#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 */
8e09f215
SW
644struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
645 int datasize,
646 struct dasd_device *device)
1da177e4
LT
647{
648 struct dasd_ccw_req *cqr;
649
650 /* Sanity checks */
7ac1e877
ES
651 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
652 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4 653
88abaab4 654 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1da177e4
LT
655 if (cqr == NULL)
656 return ERR_PTR(-ENOMEM);
1da177e4
LT
657 cqr->cpaddr = NULL;
658 if (cplength > 0) {
88abaab4 659 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1da177e4
LT
660 GFP_ATOMIC | GFP_DMA);
661 if (cqr->cpaddr == NULL) {
662 kfree(cqr);
663 return ERR_PTR(-ENOMEM);
664 }
1da177e4
LT
665 }
666 cqr->data = NULL;
667 if (datasize > 0) {
88abaab4 668 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1da177e4 669 if (cqr->data == NULL) {
17fd682e 670 kfree(cqr->cpaddr);
1da177e4
LT
671 kfree(cqr);
672 return ERR_PTR(-ENOMEM);
673 }
1da177e4
LT
674 }
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
8e09f215
SW
682struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
683 int datasize,
684 struct dasd_device *device)
1da177e4
LT
685{
686 unsigned long flags;
687 struct dasd_ccw_req *cqr;
688 char *data;
689 int size;
690
691 /* Sanity checks */
7ac1e877
ES
692 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
693 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4
LT
694
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 */
8e09f215 731void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4 732{
347a8dc3 733#ifdef CONFIG_64BIT
1da177e4
LT
734 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
17fd682e
JJ
742 kfree(cqr->cpaddr);
743 kfree(cqr->data);
1da177e4
LT
744 kfree(cqr);
745 dasd_put_device(device);
746}
747
8e09f215 748void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4
LT
749{
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 */
8e09f215 761static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1da177e4
LT
762{
763 struct dasd_device *device;
764
765 if (cqr == NULL)
766 return -EINVAL;
8e09f215 767 device = cqr->startdev;
1da177e4 768 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
fc19f381 769 DBF_DEV_EVENT(DBF_WARNING, device,
1da177e4
LT
770 " 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 */
8e09f215 785int dasd_term_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
786{
787 struct dasd_device *device;
788 int retries, rc;
fc19f381 789 char errorstring[ERRORLENGTH];
1da177e4
LT
790
791 /* Check the cqr */
792 rc = dasd_check_cqr(cqr);
793 if (rc)
794 return rc;
795 retries = 0;
8e09f215 796 device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
797 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 */
c2ba444d 801 cqr->retries--;
8e09f215 802 cqr->status = DASD_CQR_CLEAR_PENDING;
1da177e4 803 cqr->stopclk = get_clock();
8f61701b 804 cqr->starttime = 0;
1da177e4
LT
805 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:
fc19f381
SH
823 /* 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);
1da177e4
LT
827 BUG();
828 break;
829 }
830 retries++;
831 }
8e09f215 832 dasd_schedule_device_bh(device);
1da177e4
LT
833 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 */
8e09f215 840int dasd_start_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
841{
842 struct dasd_device *device;
843 int rc;
fc19f381 844 char errorstring[ERRORLENGTH];
1da177e4
LT
845
846 /* Check the cqr */
847 rc = dasd_check_cqr(cqr);
848 if (rc)
849 return rc;
8e09f215 850 device = (struct dasd_device *) cqr->startdev;
1da177e4 851 if (cqr->retries < 0) {
fc19f381
SH
852 /* 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);
8e09f215 856 cqr->status = DASD_CQR_ERROR;
1da177e4
LT
857 return -EIO;
858 }
859 cqr->startclk = get_clock();
860 cqr->starttime = jiffies;
861 cqr->retries--;
f3eb5384
SW
862 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 }
1da177e4
LT
869 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:
fc19f381 877 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1da177e4
LT
878 "start_IO: device busy, retry later");
879 break;
880 case -ETIMEDOUT:
fc19f381 881 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1da177e4
LT
882 "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;
fc19f381 891 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1da177e4
LT
892 "start_IO: selected pathes gone,"
893 " retry on all pathes");
894 break;
895 case -ENODEV:
f3eb5384
SW
896 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
897 "start_IO: -ENODEV device gone, retry");
898 break;
1da177e4 899 case -EIO:
fc19f381 900 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
f3eb5384 901 "start_IO: -EIO device gone, retry");
1da177e4
LT
902 break;
903 default:
fc19f381
SH
904 /* 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);
1da177e4
LT
909 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 */
8e09f215 923static void dasd_device_timeout(unsigned long ptr)
1da177e4
LT
924{
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);
8e09f215 933 dasd_schedule_device_bh(device);
1da177e4
LT
934}
935
936/*
937 * Setup timeout for a device in jiffies.
938 */
8e09f215 939void dasd_device_set_timer(struct dasd_device *device, int expires)
1da177e4 940{
48cae885
SW
941 if (expires == 0)
942 del_timer(&device->timer);
943 else
944 mod_timer(&device->timer, jiffies + expires);
1da177e4
LT
945}
946
947/*
948 * Clear timeout for a device.
949 */
8e09f215 950void dasd_device_clear_timer(struct dasd_device *device)
1da177e4 951{
48cae885 952 del_timer(&device->timer);
1da177e4
LT
953}
954
8e09f215
SW
955static void dasd_handle_killed_request(struct ccw_device *cdev,
956 unsigned long intparm)
1da177e4
LT
957{
958 struct dasd_ccw_req *cqr;
959 struct dasd_device *device;
960
f16f5843
SW
961 if (!intparm)
962 return;
1da177e4
LT
963 cqr = (struct dasd_ccw_req *) intparm;
964 if (cqr->status != DASD_CQR_IN_IO) {
fc19f381 965 DBF_EVENT(DBF_DEBUG,
1da177e4
LT
966 "invalid status in handle_killed_request: "
967 "bus_id %s, status %02x",
2a0217d5 968 dev_name(&cdev->dev), cqr->status);
1da177e4
LT
969 return;
970 }
971
8e09f215 972 device = (struct dasd_device *) cqr->startdev;
1da177e4 973 if (device == NULL ||
a00bfd71 974 device != dasd_device_from_cdev_locked(cdev) ||
1da177e4 975 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
fc19f381
SH
976 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
977 "bus_id %s", dev_name(&cdev->dev));
1da177e4
LT
978 return;
979 }
980
981 /* Schedule request to be retried. */
982 cqr->status = DASD_CQR_QUEUED;
983
8e09f215
SW
984 dasd_device_clear_timer(device);
985 dasd_schedule_device_bh(device);
1da177e4
LT
986 dasd_put_device(device);
987}
988
8e09f215 989void dasd_generic_handle_state_change(struct dasd_device *device)
1da177e4 990{
20c64468
SW
991 /* First of all start sense subsystem status request. */
992 dasd_eer_snss(device);
993
1da177e4 994 device->stopped &= ~DASD_STOPPED_PENDING;
8e09f215
SW
995 dasd_schedule_device_bh(device);
996 if (device->block)
997 dasd_schedule_block_bh(device->block);
1da177e4
LT
998}
999
1000/*
1001 * Interrupt handler for "normal" ssch-io based dasd devices.
1002 */
8e09f215
SW
1003void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1004 struct irb *irb)
1da177e4
LT
1005{
1006 struct dasd_ccw_req *cqr, *next;
1007 struct dasd_device *device;
1008 unsigned long long now;
1009 int expires;
1da177e4
LT
1010
1011 if (IS_ERR(irb)) {
1012 switch (PTR_ERR(irb)) {
1013 case -EIO:
1da177e4
LT
1014 break;
1015 case -ETIMEDOUT:
fc19f381 1016 DBF_EVENT(DBF_WARNING, "%s(%s): request timed out\n",
2a0217d5 1017 __func__, dev_name(&cdev->dev));
1da177e4
LT
1018 break;
1019 default:
fc19f381 1020 DBF_EVENT(DBF_WARNING, "%s(%s): unknown error %ld\n",
2a0217d5 1021 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
1da177e4 1022 }
f16f5843 1023 dasd_handle_killed_request(cdev, intparm);
1da177e4
LT
1024 return;
1025 }
1026
1027 now = get_clock();
1028
8e09f215
SW
1029 /* check for unsolicited interrupts */
1030 cqr = (struct dasd_ccw_req *) intparm;
f3eb5384
SW
1031 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))) {
8e09f215
SW
1034 if (cqr && cqr->status == DASD_CQR_IN_IO)
1035 cqr->status = DASD_CQR_QUEUED;
a00bfd71 1036 device = dasd_device_from_cdev_locked(cdev);
1da177e4 1037 if (!IS_ERR(device)) {
8e09f215
SW
1038 dasd_device_clear_timer(device);
1039 device->discipline->handle_unsolicited_interrupt(device,
1040 irb);
1da177e4
LT
1041 dasd_put_device(device);
1042 }
1043 return;
1044 }
1045
8e09f215
SW
1046 device = (struct dasd_device *) cqr->startdev;
1047 if (!device ||
1da177e4 1048 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
fc19f381
SH
1049 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid device in request: "
1050 "bus_id %s", dev_name(&cdev->dev));
1da177e4
LT
1051 return;
1052 }
1053
1054 /* Check for clear pending */
8e09f215 1055 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
f3eb5384 1056 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
8e09f215
SW
1057 cqr->status = DASD_CQR_CLEARED;
1058 dasd_device_clear_timer(device);
8f61701b 1059 wake_up(&dasd_flush_wq);
8e09f215 1060 dasd_schedule_device_bh(device);
1da177e4
LT
1061 return;
1062 }
1063
f3eb5384 1064 /* check status - the request might have been killed by dyn detach */
1da177e4 1065 if (cqr->status != DASD_CQR_IN_IO) {
fc19f381
SH
1066 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1067 "status %02x", dev_name(&cdev->dev), cqr->status);
1da177e4
LT
1068 return;
1069 }
fc19f381 1070
8e09f215 1071 next = NULL;
1da177e4 1072 expires = 0;
f3eb5384
SW
1073 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1074 scsw_cstat(&irb->scsw) == 0) {
8e09f215
SW
1075 /* request was completed successfully */
1076 cqr->status = DASD_CQR_SUCCESS;
1da177e4
LT
1077 cqr->stopclk = now;
1078 /* Start first request on queue if possible -> fast_io. */
8e09f215
SW
1079 if (cqr->devlist.next != &device->ccw_queue) {
1080 next = list_entry(cqr->devlist.next,
1081 struct dasd_ccw_req, devlist);
1da177e4 1082 }
8e09f215
SW
1083 } else { /* error */
1084 memcpy(&cqr->irb, irb, sizeof(struct irb));
fc19f381
SH
1085 /* log sense for every failed I/O to s390 debugfeature */
1086 dasd_log_sense_dbf(cqr, irb);
9575bf26 1087 if (device->features & DASD_FEATURE_ERPLOG) {
9575bf26
HH
1088 dasd_log_sense(cqr, irb);
1089 }
fc19f381 1090
6c5f57c7
SH
1091 /*
1092 * If we don't want complex ERP for this request, then just
1093 * reset this and retry it in the fastpath
8e09f215 1094 */
6c5f57c7 1095 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
8e09f215 1096 cqr->retries > 0) {
fc19f381
SH
1097 if (cqr->lpm == LPM_ANYPATH)
1098 DBF_DEV_EVENT(DBF_DEBUG, device,
1099 "default ERP in fastpath "
1100 "(%i retries left)",
1101 cqr->retries);
8e09f215
SW
1102 cqr->lpm = LPM_ANYPATH;
1103 cqr->status = DASD_CQR_QUEUED;
1104 next = cqr;
1105 } else
1da177e4 1106 cqr->status = DASD_CQR_ERROR;
8e09f215
SW
1107 }
1108 if (next && (next->status == DASD_CQR_QUEUED) &&
1109 (!device->stopped)) {
1110 if (device->discipline->start_IO(next) == 0)
1111 expires = next->expires;
1da177e4
LT
1112 }
1113 if (expires != 0)
8e09f215 1114 dasd_device_set_timer(device, expires);
1da177e4 1115 else
8e09f215
SW
1116 dasd_device_clear_timer(device);
1117 dasd_schedule_device_bh(device);
1da177e4
LT
1118}
1119
1120/*
8e09f215
SW
1121 * 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.
1da177e4 1123 */
8e09f215
SW
1124static void __dasd_device_recovery(struct dasd_device *device,
1125 struct dasd_ccw_req *ref_cqr)
1da177e4 1126{
8e09f215
SW
1127 struct list_head *l, *n;
1128 struct dasd_ccw_req *cqr;
1da177e4 1129
8e09f215
SW
1130 /*
1131 * only requeue request that came from the dasd_block layer
1132 */
1133 if (!ref_cqr->block)
1134 return;
1da177e4 1135
8e09f215
SW
1136 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;
1141 }
1142 }
1143};
1da177e4
LT
1144
1145/*
8e09f215
SW
1146 * Remove those ccw requests from the queue that need to be returned
1147 * to the upper layer.
1da177e4 1148 */
8e09f215
SW
1149static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1150 struct list_head *final_queue)
1da177e4
LT
1151{
1152 struct list_head *l, *n;
1153 struct dasd_ccw_req *cqr;
1da177e4 1154
1da177e4
LT
1155 /* Process request with final status. */
1156 list_for_each_safe(l, n, &device->ccw_queue) {
8e09f215
SW
1157 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1158
1da177e4 1159 /* Stop list processing at the first non-final request. */
8e09f215
SW
1160 if (cqr->status == DASD_CQR_QUEUED ||
1161 cqr->status == DASD_CQR_IN_IO ||
1162 cqr->status == DASD_CQR_CLEAR_PENDING)
1da177e4 1163 break;
1da177e4 1164 if (cqr->status == DASD_CQR_ERROR) {
8e09f215 1165 __dasd_device_recovery(device, cqr);
20c64468 1166 }
1da177e4 1167 /* Rechain finished requests to final queue */
8e09f215 1168 list_move_tail(&cqr->devlist, final_queue);
1da177e4
LT
1169 }
1170}
1171
1da177e4 1172/*
8e09f215
SW
1173 * the cqrs from the final queue are returned to the upper layer
1174 * by setting a dasd_block state and calling the callback function
1da177e4 1175 */
8e09f215
SW
1176static void __dasd_device_process_final_queue(struct dasd_device *device,
1177 struct list_head *final_queue)
1da177e4 1178{
8e09f215 1179 struct list_head *l, *n;
1da177e4 1180 struct dasd_ccw_req *cqr;
03513bcc 1181 struct dasd_block *block;
c80ee724
SH
1182 void (*callback)(struct dasd_ccw_req *, void *data);
1183 void *callback_data;
fc19f381 1184 char errorstring[ERRORLENGTH];
f24acd45 1185
8e09f215
SW
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);
03513bcc 1189 block = cqr->block;
c80ee724
SH
1190 callback = cqr->callback;
1191 callback_data = cqr->callback_data;
03513bcc
SW
1192 if (block)
1193 spin_lock_bh(&block->queue_lock);
8e09f215
SW
1194 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:
fc19f381
SH
1205 /* 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);
8e09f215 1210 BUG();
1da177e4 1211 }
8e09f215 1212 if (cqr->callback != NULL)
c80ee724 1213 (callback)(cqr, callback_data);
03513bcc
SW
1214 if (block)
1215 spin_unlock_bh(&block->queue_lock);
1da177e4
LT
1216 }
1217}
1218
1219/*
1220 * 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 */
8e09f215 1223static void __dasd_device_check_expire(struct dasd_device *device)
1da177e4
LT
1224{
1225 struct dasd_ccw_req *cqr;
1226
1227 if (list_empty(&device->ccw_queue))
1228 return;
8e09f215 1229 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
29145a6c
HH
1230 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 */
fc19f381
SH
1234 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));
7dc1da9f
SH
1238 cqr->expires += 5*HZ;
1239 dasd_device_set_timer(device, 5*HZ);
29145a6c 1240 } else {
fc19f381
SH
1241 dev_err(&device->cdev->dev,
1242 "cqr %p timed out (%is), %i retries "
1243 "remaining\n", cqr, (cqr->expires/HZ),
1244 cqr->retries);
1da177e4
LT
1245 }
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 */
8e09f215 1253static void __dasd_device_start_head(struct dasd_device *device)
1da177e4
LT
1254{
1255 struct dasd_ccw_req *cqr;
1256 int rc;
1257
1258 if (list_empty(&device->ccw_queue))
1259 return;
8e09f215 1260 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
25ee4cf8
PO
1261 if (cqr->status != DASD_CQR_QUEUED)
1262 return;
8e09f215
SW
1263 /* 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);
25ee4cf8 1267 return;
1c01b8a5 1268 }
25ee4cf8
PO
1269
1270 rc = device->discipline->start_IO(cqr);
1271 if (rc == 0)
8e09f215 1272 dasd_device_set_timer(device, cqr->expires);
25ee4cf8 1273 else if (rc == -EACCES) {
8e09f215 1274 dasd_schedule_device_bh(device);
25ee4cf8
PO
1275 } else
1276 /* Hmpf, try again in 1/2 sec */
8e09f215 1277 dasd_device_set_timer(device, 50);
8f61701b
HH
1278}
1279
1da177e4 1280/*
8e09f215
SW
1281 * 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.
1da177e4 1289 */
8e09f215 1290int dasd_flush_device_queue(struct dasd_device *device)
1da177e4 1291{
8e09f215
SW
1292 struct dasd_ccw_req *cqr, *n;
1293 int rc;
1da177e4 1294 struct list_head flush_queue;
1da177e4
LT
1295
1296 INIT_LIST_HEAD(&flush_queue);
1297 spin_lock_irq(get_ccwdev_lock(device->cdev));
8f61701b 1298 rc = 0;
8e09f215 1299 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
8f61701b
HH
1300 /* 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 */
fc19f381
SH
1306 dev_err(&device->cdev->dev,
1307 "Flushing the DASD request queue "
1308 "failed for request %p\n", cqr);
8f61701b
HH
1309 /* stop flush processing */
1310 goto finished;
1311 }
1312 break;
1313 case DASD_CQR_QUEUED:
1da177e4 1314 cqr->stopclk = get_clock();
8e09f215 1315 cqr->status = DASD_CQR_CLEARED;
8f61701b 1316 break;
8e09f215 1317 default: /* no need to modify the others */
8f61701b
HH
1318 break;
1319 }
8e09f215 1320 list_move_tail(&cqr->devlist, &flush_queue);
8f61701b 1321 }
8f61701b
HH
1322finished:
1323 spin_unlock_irq(get_ccwdev_lock(device->cdev));
8e09f215
SW
1324 /*
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);
8f61701b 1337 return rc;
1da177e4
LT
1338}
1339
1340/*
1341 * Acquire the device lock and process queues for the device.
1342 */
8e09f215 1343static void dasd_device_tasklet(struct dasd_device *device)
1da177e4
LT
1344{
1345 struct list_head final_queue;
1da177e4
LT
1346
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. */
8e09f215
SW
1351 __dasd_device_check_expire(device);
1352 /* find final requests on ccw queue */
1353 __dasd_device_process_ccw_queue(device, &final_queue);
1da177e4
LT
1354 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1355 /* Now call the callback function of requests with final status */
8e09f215
SW
1356 __dasd_device_process_final_queue(device, &final_queue);
1357 spin_lock_irq(get_ccwdev_lock(device->cdev));
1da177e4 1358 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
1359 __dasd_device_start_head(device);
1360 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1da177e4
LT
1361 dasd_put_device(device);
1362}
1363
1364/*
1365 * Schedules a call to dasd_tasklet over the device tasklet.
1366 */
8e09f215 1367void dasd_schedule_device_bh(struct dasd_device *device)
1da177e4
LT
1368{
1369 /* Protect against rescheduling. */
973bd993 1370 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1da177e4
LT
1371 return;
1372 dasd_get_device(device);
1373 tasklet_hi_schedule(&device->tasklet);
1374}
1375
1376/*
8e09f215
SW
1377 * Queue a request to the head of the device ccw_queue.
1378 * Start the I/O if possible.
1da177e4 1379 */
8e09f215 1380void dasd_add_request_head(struct dasd_ccw_req *cqr)
1da177e4
LT
1381{
1382 struct dasd_device *device;
1383 unsigned long flags;
1384
8e09f215 1385 device = cqr->startdev;
1da177e4 1386 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
1387 cqr->status = DASD_CQR_QUEUED;
1388 list_add(&cqr->devlist, &device->ccw_queue);
1da177e4 1389 /* let the bh start the request to keep them in order */
8e09f215 1390 dasd_schedule_device_bh(device);
1da177e4
LT
1391 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1392}
1393
1394/*
8e09f215
SW
1395 * Queue a request to the tail of the device ccw_queue.
1396 * Start the I/O if possible.
1da177e4 1397 */
8e09f215 1398void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1da177e4
LT
1399{
1400 struct dasd_device *device;
1401 unsigned long flags;
1402
8e09f215 1403 device = cqr->startdev;
1da177e4 1404 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
1405 cqr->status = DASD_CQR_QUEUED;
1406 list_add_tail(&cqr->devlist, &device->ccw_queue);
1da177e4 1407 /* let the bh start the request to keep them in order */
8e09f215 1408 dasd_schedule_device_bh(device);
1da177e4
LT
1409 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1410}
1411
1412/*
8e09f215 1413 * Wakeup helper for the 'sleep_on' functions.
1da177e4 1414 */
8e09f215 1415static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1da177e4
LT
1416{
1417 wake_up((wait_queue_head_t *) data);
1418}
1419
8e09f215 1420static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1da177e4
LT
1421{
1422 struct dasd_device *device;
1423 int rc;
1424
8e09f215 1425 device = cqr->startdev;
1da177e4 1426 spin_lock_irq(get_ccwdev_lock(device->cdev));
c2ba444d 1427 rc = ((cqr->status == DASD_CQR_DONE ||
8e09f215
SW
1428 cqr->status == DASD_CQR_NEED_ERP ||
1429 cqr->status == DASD_CQR_TERMINATED) &&
1430 list_empty(&cqr->devlist));
1da177e4
LT
1431 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1432 return rc;
1433}
1434
1435/*
8e09f215
SW
1436 * Queue a request to the tail of the device ccw_queue and wait for
1437 * it's completion.
1da177e4 1438 */
8e09f215 1439int dasd_sleep_on(struct dasd_ccw_req *cqr)
1da177e4 1440{
1da177e4
LT
1441 struct dasd_device *device;
1442 int rc;
138c014d 1443
8e09f215 1444 device = cqr->startdev;
138c014d 1445
1da177e4 1446 cqr->callback = dasd_wakeup_cb;
c80ee724 1447 cqr->callback_data = (void *) &generic_waitq;
8e09f215 1448 dasd_add_request_tail(cqr);
c80ee724 1449 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 1450
1da177e4 1451 /* Request status is either done or failed. */
8e09f215 1452 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1453 return rc;
1454}
1455
1456/*
8e09f215
SW
1457 * Queue a request to the tail of the device ccw_queue and wait
1458 * interruptible for it's completion.
1da177e4 1459 */
8e09f215 1460int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1da177e4 1461{
1da177e4 1462 struct dasd_device *device;
8e09f215 1463 int rc;
1da177e4 1464
8e09f215 1465 device = cqr->startdev;
1da177e4 1466 cqr->callback = dasd_wakeup_cb;
c80ee724 1467 cqr->callback_data = (void *) &generic_waitq;
8e09f215 1468 dasd_add_request_tail(cqr);
c80ee724 1469 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
8e09f215
SW
1470 if (rc == -ERESTARTSYS) {
1471 dasd_cancel_req(cqr);
1472 /* wait (non-interruptible) for final status */
c80ee724 1473 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1da177e4 1474 }
8e09f215 1475 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1476 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 */
8e09f215 1485static inline int _dasd_term_running_cqr(struct dasd_device *device)
1da177e4
LT
1486{
1487 struct dasd_ccw_req *cqr;
1da177e4
LT
1488
1489 if (list_empty(&device->ccw_queue))
1490 return 0;
8e09f215 1491 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
8f61701b 1492 return device->discipline->term_IO(cqr);
1da177e4
LT
1493}
1494
8e09f215 1495int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1da177e4 1496{
1da177e4
LT
1497 struct dasd_device *device;
1498 int rc;
138c014d 1499
8e09f215 1500 device = cqr->startdev;
1da177e4
LT
1501 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 }
138c014d 1507
1da177e4 1508 cqr->callback = dasd_wakeup_cb;
c80ee724 1509 cqr->callback_data = (void *) &generic_waitq;
1da177e4 1510 cqr->status = DASD_CQR_QUEUED;
8e09f215 1511 list_add(&cqr->devlist, &device->ccw_queue);
138c014d 1512
1da177e4 1513 /* let the bh start the request to keep them in order */
8e09f215 1514 dasd_schedule_device_bh(device);
138c014d 1515
1da177e4
LT
1516 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1517
c80ee724 1518 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 1519
1da177e4 1520 /* Request status is either done or failed. */
8e09f215 1521 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1522 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.
8e09f215
SW
1530 * 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.
1da177e4 1534 */
8e09f215 1535int dasd_cancel_req(struct dasd_ccw_req *cqr)
1da177e4 1536{
8e09f215 1537 struct dasd_device *device = cqr->startdev;
1da177e4
LT
1538 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:
8e09f215
SW
1545 /* request was not started - just set to cleared */
1546 cqr->status = DASD_CQR_CLEARED;
1da177e4
LT
1547 break;
1548 case DASD_CQR_IN_IO:
1549 /* request in IO - terminate IO and release again */
8e09f215
SW
1550 rc = device->discipline->term_IO(cqr);
1551 if (rc) {
fc19f381
SH
1552 dev_err(&device->cdev->dev,
1553 "Cancelling request %p failed with rc=%d\n",
1554 cqr, rc);
8e09f215
SW
1555 } else {
1556 cqr->stopclk = get_clock();
1557 rc = 1;
1558 }
1da177e4 1559 break;
8e09f215 1560 default: /* already finished or clear pending - do nothing */
1da177e4 1561 break;
8e09f215
SW
1562 }
1563 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1564 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{
48cae885
SW
1596 if (expires == 0)
1597 del_timer(&block->timer);
1598 else
1599 mod_timer(&block->timer, jiffies + expires);
8e09f215
SW
1600}
1601
1602/*
1603 * Clear timeout for a dasd_block.
1604 */
1605void dasd_block_clear_timer(struct dasd_block *block)
1606{
48cae885 1607 del_timer(&block->timer);
8e09f215
SW
1608}
1609
1610/*
1611 * posts the buffer_cache about a finalized request
1612 */
4c4e2148 1613static inline void dasd_end_request(struct request *req, int error)
8e09f215 1614{
4c4e2148 1615 if (__blk_end_request(req, error, blk_rq_bytes(req)))
1da177e4 1616 BUG();
8e09f215
SW
1617}
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
fc19f381 1631 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
8e09f215
SW
1632 erp_fn = device->discipline->erp_postaction(cqr);
1633 erp_fn(cqr);
1634}
1da177e4 1635
8e09f215
SW
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);
4c4e2148 1673 dasd_end_request(req, -EIO);
8e09f215
SW
1674 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);
4c4e2148 1702 dasd_end_request(req, -EIO);
8e09f215
SW
1703 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;
4c4e2148 1721 int error = 0;
8e09f215
SW
1722
1723 req = (struct request *) cqr->callback_data;
1724 dasd_profile_end(cqr->block, cqr, req);
fe6b8e76 1725 status = cqr->block->base->discipline->free_cp(cqr, req);
4c4e2148
KU
1726 if (status <= 0)
1727 error = status ? status : -EIO;
1728 dasd_end_request(req, error);
8e09f215
SW
1729}
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) {
6c5f57c7
SH
1760 erp_fn = base->discipline->erp_action(cqr);
1761 erp_fn(cqr);
8e09f215
SW
1762 goto restart;
1763 }
1764
a9cffb22
SH
1765 /* log sense for fatal error */
1766 if (cqr->status == DASD_CQR_FAILED) {
1767 dasd_log_sense(cqr, &cqr->irb);
1768 }
1769
8e09f215
SW
1770 /* 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) {
0cd4bd47 1917 spin_lock_bh(&block->queue_lock);
8e09f215 1918 __dasd_block_process_erp(block, cqr);
0cd4bd47 1919 spin_unlock_bh(&block->queue_lock);
8e09f215
SW
1920 /* restart list_for_xx loop since dasd_process_erp
1921 * might remove multiple elements */
1922 goto restart_cb;
1923 }
1924 /* call the callback function */
0cd4bd47 1925 spin_lock_irq(&block->request_queue_lock);
8e09f215
SW
1926 cqr->endclk = get_clock();
1927 list_del_init(&cqr->blocklist);
1928 __dasd_cleanup_cqr(cqr);
0cd4bd47 1929 spin_unlock_irq(&block->request_queue_lock);
1da177e4 1930 }
1da177e4
LT
1931 return rc;
1932}
1933
1934/*
8e09f215
SW
1935 * 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.)
1da177e4
LT
1951 */
1952
1953/*
1954 * Dasd request queue function. Called from ll_rw_blk.c
1955 */
8e09f215 1956static void do_dasd_request(struct request_queue *queue)
1da177e4 1957{
8e09f215 1958 struct dasd_block *block;
1da177e4 1959
8e09f215
SW
1960 block = queue->queuedata;
1961 spin_lock(&block->queue_lock);
1da177e4 1962 /* Get new request from the block device request queue */
8e09f215 1963 __dasd_process_request_queue(block);
1da177e4 1964 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
1965 __dasd_block_start_head(block);
1966 spin_unlock(&block->queue_lock);
1da177e4
LT
1967}
1968
1969/*
1970 * Allocate and initialize request queue and default I/O scheduler.
1971 */
8e09f215 1972static int dasd_alloc_queue(struct dasd_block *block)
1da177e4
LT
1973{
1974 int rc;
1975
8e09f215
SW
1976 block->request_queue = blk_init_queue(do_dasd_request,
1977 &block->request_queue_lock);
1978 if (block->request_queue == NULL)
1da177e4
LT
1979 return -ENOMEM;
1980
8e09f215 1981 block->request_queue->queuedata = block;
1da177e4 1982
8e09f215 1983 elevator_exit(block->request_queue->elevator);
08a8a0c5 1984 block->request_queue->elevator = NULL;
8e09f215 1985 rc = elevator_init(block->request_queue, "deadline");
1da177e4 1986 if (rc) {
8e09f215 1987 blk_cleanup_queue(block->request_queue);
1da177e4
LT
1988 return rc;
1989 }
1990 return 0;
1991}
1992
1993/*
1994 * Allocate and initialize request queue.
1995 */
8e09f215 1996static void dasd_setup_queue(struct dasd_block *block)
1da177e4
LT
1997{
1998 int max;
1999
8e09f215
SW
2000 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);
f3eb5384
SW
2005 /* 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);
8e09f215 2010 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
1da177e4
LT
2011}
2012
2013/*
2014 * Deactivate and free request queue.
2015 */
8e09f215 2016static void dasd_free_queue(struct dasd_block *block)
1da177e4 2017{
8e09f215
SW
2018 if (block->request_queue) {
2019 blk_cleanup_queue(block->request_queue);
2020 block->request_queue = NULL;
1da177e4
LT
2021 }
2022}
2023
2024/*
2025 * Flush request on the request queue.
2026 */
8e09f215 2027static void dasd_flush_request_queue(struct dasd_block *block)
1da177e4
LT
2028{
2029 struct request *req;
2030
8e09f215 2031 if (!block->request_queue)
1da177e4 2032 return;
138c014d 2033
8e09f215
SW
2034 spin_lock_irq(&block->request_queue_lock);
2035 while ((req = elv_next_request(block->request_queue))) {
1da177e4 2036 blkdev_dequeue_request(req);
4c4e2148 2037 dasd_end_request(req, -EIO);
1da177e4 2038 }
8e09f215 2039 spin_unlock_irq(&block->request_queue_lock);
1da177e4
LT
2040}
2041
57a7c0bc 2042static int dasd_open(struct block_device *bdev, fmode_t mode)
1da177e4 2043{
57a7c0bc 2044 struct dasd_block *block = bdev->bd_disk->private_data;
8e09f215 2045 struct dasd_device *base = block->base;
1da177e4
LT
2046 int rc;
2047
8e09f215
SW
2048 atomic_inc(&block->open_count);
2049 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
1da177e4
LT
2050 rc = -ENODEV;
2051 goto unlock;
2052 }
2053
8e09f215 2054 if (!try_module_get(base->discipline->owner)) {
1da177e4
LT
2055 rc = -EINVAL;
2056 goto unlock;
2057 }
2058
2059 if (dasd_probeonly) {
fc19f381
SH
2060 dev_info(&base->cdev->dev,
2061 "Accessing the DASD failed because it is in "
2062 "probeonly mode\n");
1da177e4
LT
2063 rc = -EPERM;
2064 goto out;
2065 }
2066
8e09f215
SW
2067 if (base->state <= DASD_STATE_BASIC) {
2068 DBF_DEV_EVENT(DBF_ERR, base, " %s",
1da177e4
LT
2069 " Cannot open unrecognized device");
2070 rc = -ENODEV;
2071 goto out;
2072 }
2073
2074 return 0;
2075
2076out:
8e09f215 2077 module_put(base->discipline->owner);
1da177e4 2078unlock:
8e09f215 2079 atomic_dec(&block->open_count);
1da177e4
LT
2080 return rc;
2081}
2082
57a7c0bc 2083static int dasd_release(struct gendisk *disk, fmode_t mode)
1da177e4 2084{
8e09f215 2085 struct dasd_block *block = disk->private_data;
1da177e4 2086
8e09f215
SW
2087 atomic_dec(&block->open_count);
2088 module_put(block->base->discipline->owner);
1da177e4
LT
2089 return 0;
2090}
2091
a885c8c4
CH
2092/*
2093 * Return disk geometry.
2094 */
8e09f215 2095static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
a885c8c4 2096{
8e09f215
SW
2097 struct dasd_block *block;
2098 struct dasd_device *base;
a885c8c4 2099
8e09f215
SW
2100 block = bdev->bd_disk->private_data;
2101 base = block->base;
2102 if (!block)
a885c8c4
CH
2103 return -ENODEV;
2104
8e09f215
SW
2105 if (!base->discipline ||
2106 !base->discipline->fill_geometry)
a885c8c4
CH
2107 return -EINVAL;
2108
8e09f215
SW
2109 base->discipline->fill_geometry(block, geo);
2110 geo->start = get_start_sect(bdev) >> block->s2b_shift;
a885c8c4
CH
2111 return 0;
2112}
2113
1da177e4
LT
2114struct block_device_operations
2115dasd_device_operations = {
2116 .owner = THIS_MODULE,
57a7c0bc
AV
2117 .open = dasd_open,
2118 .release = dasd_release,
0000d031
HC
2119 .ioctl = dasd_ioctl,
2120 .compat_ioctl = dasd_ioctl,
a885c8c4 2121 .getgeo = dasd_getgeo,
1da177e4
LT
2122};
2123
8e09f215
SW
2124/*******************************************************************************
2125 * end of block device operations
2126 */
1da177e4
LT
2127
2128static void
2129dasd_exit(void)
2130{
2131#ifdef CONFIG_PROC_FS
2132 dasd_proc_exit();
2133#endif
20c64468 2134 dasd_eer_exit();
6bb0e010
HH
2135 if (dasd_page_cache != NULL) {
2136 kmem_cache_destroy(dasd_page_cache);
2137 dasd_page_cache = NULL;
2138 }
1da177e4
LT
2139 dasd_gendisk_exit();
2140 dasd_devmap_exit();
1da177e4
LT
2141 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
1c01b8a5
HH
2151/*
2152 * Initial attempt at a probe function. this can be simplified once
2153 * the other detection code is gone.
2154 */
8e09f215
SW
2155int dasd_generic_probe(struct ccw_device *cdev,
2156 struct dasd_discipline *discipline)
1da177e4
LT
2157{
2158 int ret;
2159
40545573
HH
2160 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2161 if (ret) {
fc19f381 2162 DBF_EVENT(DBF_WARNING,
40545573 2163 "dasd_generic_probe: could not set ccw-device options "
2a0217d5 2164 "for %s\n", dev_name(&cdev->dev));
40545573
HH
2165 return ret;
2166 }
1da177e4
LT
2167 ret = dasd_add_sysfs_files(cdev);
2168 if (ret) {
fc19f381 2169 DBF_EVENT(DBF_WARNING,
1da177e4 2170 "dasd_generic_probe: could not add sysfs entries "
2a0217d5 2171 "for %s\n", dev_name(&cdev->dev));
40545573 2172 return ret;
1da177e4 2173 }
40545573 2174 cdev->handler = &dasd_int_handler;
1da177e4 2175
40545573
HH
2176 /*
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 ) ||
2a0217d5 2182 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
40545573
HH
2183 ret = ccw_device_set_online(cdev);
2184 if (ret)
fc19f381 2185 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2a0217d5 2186 dev_name(&cdev->dev), ret);
de3e0da1 2187 return 0;
1da177e4
LT
2188}
2189
1c01b8a5
HH
2190/*
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 */
8e09f215 2194void dasd_generic_remove(struct ccw_device *cdev)
1da177e4
LT
2195{
2196 struct dasd_device *device;
8e09f215 2197 struct dasd_block *block;
1da177e4 2198
59afda78
HH
2199 cdev->handler = NULL;
2200
1da177e4
LT
2201 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. */
8e09f215
SW
2217 block = device->block;
2218 device->block = NULL;
1da177e4 2219 dasd_delete_device(device);
8e09f215
SW
2220 /*
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);
1da177e4
LT
2226}
2227
1c01b8a5
HH
2228/*
2229 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1da177e4 2230 * the device is detected for the first time and is supposed to be used
1c01b8a5
HH
2231 * or the user has started activation through sysfs.
2232 */
8e09f215
SW
2233int dasd_generic_set_online(struct ccw_device *cdev,
2234 struct dasd_discipline *base_discipline)
1da177e4 2235{
aa88861f 2236 struct dasd_discipline *discipline;
1da177e4 2237 struct dasd_device *device;
c6eb7b77 2238 int rc;
f24acd45 2239
40545573
HH
2240 /* first online clears initial online feature flag */
2241 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
1da177e4
LT
2242 device = dasd_create_device(cdev);
2243 if (IS_ERR(device))
2244 return PTR_ERR(device);
2245
aa88861f 2246 discipline = base_discipline;
c6eb7b77 2247 if (device->features & DASD_FEATURE_USEDIAG) {
1da177e4 2248 if (!dasd_diag_discipline_pointer) {
fc19f381
SH
2249 pr_warning("%s Setting the DASD online failed because "
2250 "of missing DIAG discipline\n",
2251 dev_name(&cdev->dev));
1da177e4
LT
2252 dasd_delete_device(device);
2253 return -ENODEV;
2254 }
2255 discipline = dasd_diag_discipline_pointer;
2256 }
aa88861f
PO
2257 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;
1da177e4
LT
2267 device->discipline = discipline;
2268
8e09f215 2269 /* check_device will allocate block device if necessary */
1da177e4
LT
2270 rc = discipline->check_device(device);
2271 if (rc) {
fc19f381
SH
2272 pr_warning("%s Setting the DASD online with discipline %s "
2273 "failed with rc=%i\n",
2274 dev_name(&cdev->dev), discipline->name, rc);
aa88861f
PO
2275 module_put(discipline->owner);
2276 module_put(base_discipline->owner);
1da177e4
LT
2277 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) {
fc19f381
SH
2283 pr_warning("%s Setting the DASD online failed because of a "
2284 "missing discipline\n", dev_name(&cdev->dev));
1da177e4
LT
2285 rc = -ENODEV;
2286 dasd_set_target_state(device, DASD_STATE_NEW);
8e09f215
SW
2287 if (device->block)
2288 dasd_free_block(device->block);
1da177e4
LT
2289 dasd_delete_device(device);
2290 } else
2291 pr_debug("dasd_generic device %s found\n",
2a0217d5 2292 dev_name(&cdev->dev));
1da177e4
LT
2293
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
8e09f215 2303int dasd_generic_set_offline(struct ccw_device *cdev)
1da177e4
LT
2304{
2305 struct dasd_device *device;
8e09f215 2306 struct dasd_block *block;
dafd87aa 2307 int max_count, open_count;
1da177e4
LT
2308
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 */
8e09f215 2323 if (device->block) {
a806170e
HC
2324 max_count = device->block->bdev ? 0 : -1;
2325 open_count = atomic_read(&device->block->open_count);
8e09f215
SW
2326 if (open_count > max_count) {
2327 if (open_count > 0)
fc19f381
SH
2328 pr_warning("%s: The DASD cannot be set offline "
2329 "with open count %i\n",
2330 dev_name(&cdev->dev), open_count);
8e09f215 2331 else
fc19f381
SH
2332 pr_warning("%s: The DASD cannot be set offline "
2333 "while it is in use\n",
2334 dev_name(&cdev->dev));
8e09f215
SW
2335 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2336 dasd_put_device(device);
2337 return -EBUSY;
2338 }
1da177e4
LT
2339 }
2340 dasd_set_target_state(device, DASD_STATE_NEW);
2341 /* dasd_delete_device destroys the device reference. */
8e09f215
SW
2342 block = device->block;
2343 device->block = NULL;
1da177e4 2344 dasd_delete_device(device);
8e09f215
SW
2345 /*
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);
1da177e4
LT
2351 return 0;
2352}
2353
8e09f215 2354int dasd_generic_notify(struct ccw_device *cdev, int event)
1da177e4
LT
2355{
2356 struct dasd_device *device;
2357 struct dasd_ccw_req *cqr;
1da177e4
LT
2358 int ret;
2359
91c36919 2360 device = dasd_device_from_cdev_locked(cdev);
1da177e4
LT
2361 if (IS_ERR(device))
2362 return 0;
1da177e4
LT
2363 ret = 0;
2364 switch (event) {
2365 case CIO_GONE:
47593bfa 2366 case CIO_BOXED:
1da177e4 2367 case CIO_NO_PATH:
20c64468
SW
2368 /* First of all call extended error reporting. */
2369 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2370
1da177e4
LT
2371 if (device->state < DASD_STATE_BASIC)
2372 break;
2373 /* Device is active. We want to keep it. */
8e09f215
SW
2374 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);
1da177e4
LT
2382 ret = 1;
2383 break;
2384 case CIO_OPER:
2385 /* FIXME: add a sanity check. */
8e09f215
SW
2386 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2387 dasd_schedule_device_bh(device);
2388 if (device->block)
2389 dasd_schedule_block_bh(device->block);
1da177e4
LT
2390 ret = 1;
2391 break;
2392 }
1da177e4
LT
2393 dasd_put_device(device);
2394 return ret;
2395}
2396
763968e2
HC
2397static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2398 void *rdc_buffer,
2399 int rdc_buffer_size,
2400 char *magic)
17283b56
CH
2401{
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)) {
fc19f381
SH
2408 /* 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");
17283b56
CH
2412 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
8e09f215
SW
2420 cqr->startdev = device;
2421 cqr->memdev = device;
17283b56
CH
2422 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);
8e09f215 2443 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
2444 return ret;
2445}
aaff0f64 2446EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
20c64468 2447
f3eb5384
SW
2448/*
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
8e09f215 2482static int __init dasd_init(void)
1da177e4
LT
2483{
2484 int rc;
2485
2486 init_waitqueue_head(&dasd_init_waitq);
8f61701b 2487 init_waitqueue_head(&dasd_flush_wq);
c80ee724 2488 init_waitqueue_head(&generic_waitq);
1da177e4
LT
2489
2490 /* register 'common' DASD debug area, used for all DBF_XXX calls */
361f494d 2491 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
1da177e4
LT
2492 if (dasd_debug_area == NULL) {
2493 rc = -ENOMEM;
2494 goto failed;
2495 }
2496 debug_register_view(dasd_debug_area, &debug_sprintf_view);
b0035f12 2497 debug_set_level(dasd_debug_area, DBF_WARNING);
1da177e4
LT
2498
2499 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2500
2501 dasd_diag_discipline_pointer = NULL;
2502
1da177e4
LT
2503 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;
20c64468
SW
2512 rc = dasd_eer_init();
2513 if (rc)
2514 goto failed;
1da177e4
LT
2515#ifdef CONFIG_PROC_FS
2516 rc = dasd_proc_init();
2517 if (rc)
2518 goto failed;
2519#endif
2520
2521 return 0;
2522failed:
fc19f381 2523 pr_info("The DASD device driver could not be initialized\n");
1da177e4
LT
2524 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);
8e09f215
SW
2537EXPORT_SYMBOL(dasd_device_clear_timer);
2538EXPORT_SYMBOL(dasd_block_clear_timer);
1da177e4
LT
2539EXPORT_SYMBOL(dasd_enable_device);
2540EXPORT_SYMBOL(dasd_int_handler);
2541EXPORT_SYMBOL(dasd_kfree_request);
2542EXPORT_SYMBOL(dasd_kick_device);
2543EXPORT_SYMBOL(dasd_kmalloc_request);
8e09f215
SW
2544EXPORT_SYMBOL(dasd_schedule_device_bh);
2545EXPORT_SYMBOL(dasd_schedule_block_bh);
1da177e4 2546EXPORT_SYMBOL(dasd_set_target_state);
8e09f215
SW
2547EXPORT_SYMBOL(dasd_device_set_timer);
2548EXPORT_SYMBOL(dasd_block_set_timer);
1da177e4
LT
2549EXPORT_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);
8e09f215
SW
2562EXPORT_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);
This page took 0.725574 seconds and 5 git commands to generate.