]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/s390/block/dasd.c
[S390] dasd: add High Performance FICON support
[mirror_ubuntu-hirsute-kernel.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
1da177e4
LT
12#include <linux/kmod.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ctype.h>
16#include <linux/major.h>
17#include <linux/slab.h>
18#include <linux/buffer_head.h>
a885c8c4 19#include <linux/hdreg.h>
1da177e4
LT
20
21#include <asm/ccwdev.h>
22#include <asm/ebcdic.h>
23#include <asm/idals.h>
24#include <asm/todclk.h>
f3eb5384 25#include <asm/itcw.h>
1da177e4
LT
26
27/* This is ugly... */
28#define PRINTK_HEADER "dasd:"
29
30#include "dasd_int.h"
31/*
32 * SECTION: Constant definitions to be used within this file
33 */
34#define DASD_CHANQ_MAX_SIZE 4
35
36/*
37 * SECTION: exported variables of dasd.c
38 */
39debug_info_t *dasd_debug_area;
40struct dasd_discipline *dasd_diag_discipline_pointer;
2b67fc46 41void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
1da177e4
LT
42
43MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
44MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
45 " Copyright 2000 IBM Corporation");
46MODULE_SUPPORTED_DEVICE("dasd");
1da177e4
LT
47MODULE_LICENSE("GPL");
48
49/*
50 * SECTION: prototypes for static functions of dasd.c
51 */
8e09f215
SW
52static int dasd_alloc_queue(struct dasd_block *);
53static void dasd_setup_queue(struct dasd_block *);
54static void dasd_free_queue(struct dasd_block *);
55static void dasd_flush_request_queue(struct dasd_block *);
56static int dasd_flush_block_queue(struct dasd_block *);
57static void dasd_device_tasklet(struct dasd_device *);
58static void dasd_block_tasklet(struct dasd_block *);
4927b3f7 59static void do_kick_device(struct work_struct *);
8e09f215 60static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
48cae885
SW
61static void dasd_device_timeout(unsigned long);
62static void dasd_block_timeout(unsigned long);
1da177e4
LT
63
64/*
65 * SECTION: Operations on the device structure.
66 */
67static wait_queue_head_t dasd_init_waitq;
8f61701b 68static wait_queue_head_t dasd_flush_wq;
c80ee724 69static wait_queue_head_t generic_waitq;
1da177e4
LT
70
71/*
72 * Allocate memory for a new device structure.
73 */
8e09f215 74struct dasd_device *dasd_alloc_device(void)
1da177e4
LT
75{
76 struct dasd_device *device;
77
8e09f215
SW
78 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
79 if (!device)
1da177e4 80 return ERR_PTR(-ENOMEM);
1da177e4
LT
81
82 /* Get two pages for normal block device operations. */
83 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
8e09f215 84 if (!device->ccw_mem) {
1da177e4
LT
85 kfree(device);
86 return ERR_PTR(-ENOMEM);
87 }
88 /* Get one page for error recovery. */
89 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
8e09f215 90 if (!device->erp_mem) {
1da177e4
LT
91 free_pages((unsigned long) device->ccw_mem, 1);
92 kfree(device);
93 return ERR_PTR(-ENOMEM);
94 }
95
96 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
97 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
98 spin_lock_init(&device->mem_lock);
8e09f215 99 atomic_set(&device->tasklet_scheduled, 0);
138c014d 100 tasklet_init(&device->tasklet,
8e09f215 101 (void (*)(unsigned long)) dasd_device_tasklet,
1da177e4
LT
102 (unsigned long) device);
103 INIT_LIST_HEAD(&device->ccw_queue);
104 init_timer(&device->timer);
48cae885
SW
105 device->timer.function = dasd_device_timeout;
106 device->timer.data = (unsigned long) device;
4927b3f7 107 INIT_WORK(&device->kick_work, do_kick_device);
1da177e4
LT
108 device->state = DASD_STATE_NEW;
109 device->target = DASD_STATE_NEW;
110
111 return device;
112}
113
114/*
115 * Free memory of a device structure.
116 */
8e09f215 117void dasd_free_device(struct dasd_device *device)
1da177e4 118{
17fd682e 119 kfree(device->private);
1da177e4
LT
120 free_page((unsigned long) device->erp_mem);
121 free_pages((unsigned long) device->ccw_mem, 1);
122 kfree(device);
123}
124
8e09f215
SW
125/*
126 * Allocate memory for a new device structure.
127 */
128struct dasd_block *dasd_alloc_block(void)
129{
130 struct dasd_block *block;
131
132 block = kzalloc(sizeof(*block), GFP_ATOMIC);
133 if (!block)
134 return ERR_PTR(-ENOMEM);
135 /* open_count = 0 means device online but not in use */
136 atomic_set(&block->open_count, -1);
137
138 spin_lock_init(&block->request_queue_lock);
139 atomic_set(&block->tasklet_scheduled, 0);
140 tasklet_init(&block->tasklet,
141 (void (*)(unsigned long)) dasd_block_tasklet,
142 (unsigned long) block);
143 INIT_LIST_HEAD(&block->ccw_queue);
144 spin_lock_init(&block->queue_lock);
145 init_timer(&block->timer);
48cae885
SW
146 block->timer.function = dasd_block_timeout;
147 block->timer.data = (unsigned long) block;
8e09f215
SW
148
149 return block;
150}
151
152/*
153 * Free memory of a device structure.
154 */
155void dasd_free_block(struct dasd_block *block)
156{
157 kfree(block);
158}
159
1da177e4
LT
160/*
161 * Make a new device known to the system.
162 */
8e09f215 163static int dasd_state_new_to_known(struct dasd_device *device)
1da177e4
LT
164{
165 int rc;
166
167 /*
138c014d 168 * As long as the device is not in state DASD_STATE_NEW we want to
1da177e4
LT
169 * keep the reference count > 0.
170 */
171 dasd_get_device(device);
172
8e09f215
SW
173 if (device->block) {
174 rc = dasd_alloc_queue(device->block);
175 if (rc) {
176 dasd_put_device(device);
177 return rc;
178 }
1da177e4 179 }
1da177e4
LT
180 device->state = DASD_STATE_KNOWN;
181 return 0;
182}
183
184/*
185 * Let the system forget about a device.
186 */
8e09f215 187static int dasd_state_known_to_new(struct dasd_device *device)
1da177e4 188{
20c64468
SW
189 /* Disable extended error reporting for this device. */
190 dasd_eer_disable(device);
1da177e4 191 /* Forget the discipline information. */
8e09f215
SW
192 if (device->discipline) {
193 if (device->discipline->uncheck_device)
194 device->discipline->uncheck_device(device);
aa88861f 195 module_put(device->discipline->owner);
8e09f215 196 }
1da177e4 197 device->discipline = NULL;
aa88861f
PO
198 if (device->base_discipline)
199 module_put(device->base_discipline->owner);
200 device->base_discipline = NULL;
1da177e4
LT
201 device->state = DASD_STATE_NEW;
202
8e09f215
SW
203 if (device->block)
204 dasd_free_queue(device->block);
1da177e4
LT
205
206 /* Give up reference we took in dasd_state_new_to_known. */
207 dasd_put_device(device);
8f61701b 208 return 0;
1da177e4
LT
209}
210
211/*
212 * Request the irq line for the device.
213 */
8e09f215 214static int dasd_state_known_to_basic(struct dasd_device *device)
1da177e4
LT
215{
216 int rc;
217
218 /* Allocate and register gendisk structure. */
8e09f215
SW
219 if (device->block) {
220 rc = dasd_gendisk_alloc(device->block);
221 if (rc)
222 return rc;
223 }
1da177e4 224 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
2a0217d5 225 device->debug_area = debug_register(dev_name(&device->cdev->dev), 1, 1,
8e09f215 226 8 * sizeof(long));
1da177e4 227 debug_register_view(device->debug_area, &debug_sprintf_view);
b0035f12 228 debug_set_level(device->debug_area, DBF_WARNING);
1da177e4
LT
229 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
230
231 device->state = DASD_STATE_BASIC;
232 return 0;
233}
234
235/*
236 * Release the irq line for the device. Terminate any running i/o.
237 */
8e09f215 238static int dasd_state_basic_to_known(struct dasd_device *device)
1da177e4 239{
8f61701b 240 int rc;
8e09f215
SW
241 if (device->block) {
242 dasd_gendisk_free(device->block);
243 dasd_block_clear_timer(device->block);
244 }
245 rc = dasd_flush_device_queue(device);
8f61701b
HH
246 if (rc)
247 return rc;
8e09f215 248 dasd_device_clear_timer(device);
8f61701b 249
1da177e4
LT
250 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
251 if (device->debug_area != NULL) {
252 debug_unregister(device->debug_area);
253 device->debug_area = NULL;
254 }
255 device->state = DASD_STATE_KNOWN;
8f61701b 256 return 0;
1da177e4
LT
257}
258
259/*
260 * Do the initial analysis. The do_analysis function may return
261 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
262 * until the discipline decides to continue the startup sequence
263 * by calling the function dasd_change_state. The eckd disciplines
264 * uses this to start a ccw that detects the format. The completion
265 * interrupt for this detection ccw uses the kernel event daemon to
266 * trigger the call to dasd_change_state. All this is done in the
267 * discipline code, see dasd_eckd.c.
90f0094d
HH
268 * After the analysis ccw is done (do_analysis returned 0) the block
269 * device is setup.
270 * In case the analysis returns an error, the device setup is stopped
271 * (a fake disk was already added to allow formatting).
1da177e4 272 */
8e09f215 273static int dasd_state_basic_to_ready(struct dasd_device *device)
1da177e4
LT
274{
275 int rc;
8e09f215 276 struct dasd_block *block;
1da177e4
LT
277
278 rc = 0;
8e09f215 279 block = device->block;
90f0094d 280 /* make disk known with correct capacity */
8e09f215
SW
281 if (block) {
282 if (block->base->discipline->do_analysis != NULL)
283 rc = block->base->discipline->do_analysis(block);
284 if (rc) {
285 if (rc != -EAGAIN)
286 device->state = DASD_STATE_UNFMT;
287 return rc;
288 }
289 dasd_setup_queue(block);
290 set_capacity(block->gdp,
291 block->blocks << block->s2b_shift);
292 device->state = DASD_STATE_READY;
293 rc = dasd_scan_partitions(block);
294 if (rc)
295 device->state = DASD_STATE_BASIC;
296 } else {
297 device->state = DASD_STATE_READY;
298 }
90f0094d 299 return rc;
1da177e4
LT
300}
301
302/*
303 * Remove device from block device layer. Destroy dirty buffers.
304 * Forget format information. Check if the target level is basic
305 * and if it is create fake disk for formatting.
306 */
8e09f215 307static int dasd_state_ready_to_basic(struct dasd_device *device)
1da177e4 308{
8f61701b
HH
309 int rc;
310
1da177e4 311 device->state = DASD_STATE_BASIC;
8e09f215
SW
312 if (device->block) {
313 struct dasd_block *block = device->block;
314 rc = dasd_flush_block_queue(block);
315 if (rc) {
316 device->state = DASD_STATE_READY;
317 return rc;
318 }
319 dasd_destroy_partitions(block);
320 dasd_flush_request_queue(block);
321 block->blocks = 0;
322 block->bp_block = 0;
323 block->s2b_shift = 0;
324 }
8f61701b 325 return 0;
1da177e4
LT
326}
327
90f0094d
HH
328/*
329 * Back to basic.
330 */
8e09f215 331static int dasd_state_unfmt_to_basic(struct dasd_device *device)
90f0094d
HH
332{
333 device->state = DASD_STATE_BASIC;
8f61701b 334 return 0;
90f0094d
HH
335}
336
1da177e4
LT
337/*
338 * Make the device online and schedule the bottom half to start
339 * the requeueing of requests from the linux request queue to the
340 * ccw queue.
341 */
8f61701b 342static int
1da177e4
LT
343dasd_state_ready_to_online(struct dasd_device * device)
344{
8e09f215 345 int rc;
1301809b
SW
346 struct gendisk *disk;
347 struct disk_part_iter piter;
348 struct hd_struct *part;
8e09f215
SW
349
350 if (device->discipline->ready_to_online) {
351 rc = device->discipline->ready_to_online(device);
352 if (rc)
353 return rc;
354 }
1da177e4 355 device->state = DASD_STATE_ONLINE;
1301809b 356 if (device->block) {
8e09f215 357 dasd_schedule_block_bh(device->block);
1301809b
SW
358 disk = device->block->bdev->bd_disk;
359 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
360 while ((part = disk_part_iter_next(&piter)))
361 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
362 disk_part_iter_exit(&piter);
363 }
1da177e4
LT
364 return 0;
365}
366
367/*
368 * Stop the requeueing of requests again.
369 */
8e09f215 370static int dasd_state_online_to_ready(struct dasd_device *device)
1da177e4 371{
8e09f215 372 int rc;
1301809b
SW
373 struct gendisk *disk;
374 struct disk_part_iter piter;
375 struct hd_struct *part;
8e09f215
SW
376
377 if (device->discipline->online_to_ready) {
378 rc = device->discipline->online_to_ready(device);
379 if (rc)
380 return rc;
381 }
1da177e4 382 device->state = DASD_STATE_READY;
1301809b
SW
383 if (device->block) {
384 disk = device->block->bdev->bd_disk;
385 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
386 while ((part = disk_part_iter_next(&piter)))
387 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
388 disk_part_iter_exit(&piter);
389 }
8f61701b 390 return 0;
1da177e4
LT
391}
392
393/*
394 * Device startup state changes.
395 */
8e09f215 396static int dasd_increase_state(struct dasd_device *device)
1da177e4
LT
397{
398 int rc;
399
400 rc = 0;
401 if (device->state == DASD_STATE_NEW &&
402 device->target >= DASD_STATE_KNOWN)
403 rc = dasd_state_new_to_known(device);
404
405 if (!rc &&
406 device->state == DASD_STATE_KNOWN &&
407 device->target >= DASD_STATE_BASIC)
408 rc = dasd_state_known_to_basic(device);
409
410 if (!rc &&
411 device->state == DASD_STATE_BASIC &&
412 device->target >= DASD_STATE_READY)
413 rc = dasd_state_basic_to_ready(device);
414
39ccf95e
HH
415 if (!rc &&
416 device->state == DASD_STATE_UNFMT &&
417 device->target > DASD_STATE_UNFMT)
418 rc = -EPERM;
419
1da177e4
LT
420 if (!rc &&
421 device->state == DASD_STATE_READY &&
422 device->target >= DASD_STATE_ONLINE)
423 rc = dasd_state_ready_to_online(device);
424
425 return rc;
426}
427
428/*
429 * Device shutdown state changes.
430 */
8e09f215 431static int dasd_decrease_state(struct dasd_device *device)
1da177e4 432{
8f61701b
HH
433 int rc;
434
435 rc = 0;
1da177e4
LT
436 if (device->state == DASD_STATE_ONLINE &&
437 device->target <= DASD_STATE_READY)
8f61701b 438 rc = dasd_state_online_to_ready(device);
138c014d 439
8f61701b
HH
440 if (!rc &&
441 device->state == DASD_STATE_READY &&
1da177e4 442 device->target <= DASD_STATE_BASIC)
8f61701b 443 rc = dasd_state_ready_to_basic(device);
90f0094d 444
8f61701b
HH
445 if (!rc &&
446 device->state == DASD_STATE_UNFMT &&
90f0094d 447 device->target <= DASD_STATE_BASIC)
8f61701b 448 rc = dasd_state_unfmt_to_basic(device);
90f0094d 449
8f61701b
HH
450 if (!rc &&
451 device->state == DASD_STATE_BASIC &&
1da177e4 452 device->target <= DASD_STATE_KNOWN)
8f61701b 453 rc = dasd_state_basic_to_known(device);
138c014d 454
8f61701b
HH
455 if (!rc &&
456 device->state == DASD_STATE_KNOWN &&
1da177e4 457 device->target <= DASD_STATE_NEW)
8f61701b 458 rc = dasd_state_known_to_new(device);
1da177e4 459
8f61701b 460 return rc;
1da177e4
LT
461}
462
463/*
464 * This is the main startup/shutdown routine.
465 */
8e09f215 466static void dasd_change_state(struct dasd_device *device)
1da177e4
LT
467{
468 int rc;
469
470 if (device->state == device->target)
471 /* Already where we want to go today... */
472 return;
473 if (device->state < device->target)
474 rc = dasd_increase_state(device);
475 else
476 rc = dasd_decrease_state(device);
477 if (rc && rc != -EAGAIN)
478 device->target = device->state;
479
480 if (device->state == device->target)
481 wake_up(&dasd_init_waitq);
4dfd5c45
HH
482
483 /* let user-space know that the device status changed */
484 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
1da177e4
LT
485}
486
487/*
488 * Kick starter for devices that did not complete the startup/shutdown
489 * procedure or were sleeping because of a pending state.
490 * dasd_kick_device will schedule a call do do_kick_device to the kernel
491 * event daemon.
492 */
8e09f215 493static void do_kick_device(struct work_struct *work)
1da177e4 494{
4927b3f7 495 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
1da177e4 496 dasd_change_state(device);
8e09f215 497 dasd_schedule_device_bh(device);
1da177e4
LT
498 dasd_put_device(device);
499}
500
8e09f215 501void dasd_kick_device(struct dasd_device *device)
1da177e4
LT
502{
503 dasd_get_device(device);
504 /* queue call to dasd_kick_device to the kernel event daemon. */
505 schedule_work(&device->kick_work);
506}
507
508/*
509 * Set the target state for a device and starts the state change.
510 */
8e09f215 511void dasd_set_target_state(struct dasd_device *device, int target)
1da177e4
LT
512{
513 /* If we are in probeonly mode stop at DASD_STATE_READY. */
514 if (dasd_probeonly && target > DASD_STATE_READY)
515 target = DASD_STATE_READY;
516 if (device->target != target) {
517 if (device->state == target)
518 wake_up(&dasd_init_waitq);
519 device->target = target;
520 }
521 if (device->state != device->target)
522 dasd_change_state(device);
523}
524
525/*
526 * Enable devices with device numbers in [from..to].
527 */
8e09f215 528static inline int _wait_for_device(struct dasd_device *device)
1da177e4
LT
529{
530 return (device->state == device->target);
531}
532
8e09f215 533void dasd_enable_device(struct dasd_device *device)
1da177e4
LT
534{
535 dasd_set_target_state(device, DASD_STATE_ONLINE);
536 if (device->state <= DASD_STATE_KNOWN)
537 /* No discipline for device found. */
538 dasd_set_target_state(device, DASD_STATE_NEW);
539 /* Now wait for the devices to come up. */
540 wait_event(dasd_init_waitq, _wait_for_device(device));
541}
542
543/*
544 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
545 */
546#ifdef CONFIG_DASD_PROFILE
547
548struct dasd_profile_info_t dasd_global_profile;
549unsigned int dasd_profile_level = DASD_PROFILE_OFF;
550
551/*
552 * Increments counter in global and local profiling structures.
553 */
8e09f215 554#define dasd_profile_counter(value, counter, block) \
1da177e4
LT
555{ \
556 int index; \
557 for (index = 0; index < 31 && value >> (2+index); index++); \
558 dasd_global_profile.counter[index]++; \
8e09f215 559 block->profile.counter[index]++; \
1da177e4
LT
560}
561
562/*
563 * Add profiling information for cqr before execution.
564 */
8e09f215
SW
565static void dasd_profile_start(struct dasd_block *block,
566 struct dasd_ccw_req *cqr,
567 struct request *req)
1da177e4
LT
568{
569 struct list_head *l;
570 unsigned int counter;
571
572 if (dasd_profile_level != DASD_PROFILE_ON)
573 return;
574
575 /* count the length of the chanq for statistics */
576 counter = 0;
8e09f215 577 list_for_each(l, &block->ccw_queue)
1da177e4
LT
578 if (++counter >= 31)
579 break;
580 dasd_global_profile.dasd_io_nr_req[counter]++;
8e09f215 581 block->profile.dasd_io_nr_req[counter]++;
1da177e4
LT
582}
583
584/*
585 * Add profiling information for cqr after execution.
586 */
8e09f215
SW
587static void dasd_profile_end(struct dasd_block *block,
588 struct dasd_ccw_req *cqr,
589 struct request *req)
1da177e4
LT
590{
591 long strtime, irqtime, endtime, tottime; /* in microseconds */
592 long tottimeps, sectors;
593
594 if (dasd_profile_level != DASD_PROFILE_ON)
595 return;
596
597 sectors = req->nr_sectors;
598 if (!cqr->buildclk || !cqr->startclk ||
599 !cqr->stopclk || !cqr->endclk ||
600 !sectors)
601 return;
602
603 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
604 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
605 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
606 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
607 tottimeps = tottime / sectors;
608
609 if (!dasd_global_profile.dasd_io_reqs)
610 memset(&dasd_global_profile, 0,
8e09f215 611 sizeof(struct dasd_profile_info_t));
1da177e4
LT
612 dasd_global_profile.dasd_io_reqs++;
613 dasd_global_profile.dasd_io_sects += sectors;
614
8e09f215
SW
615 if (!block->profile.dasd_io_reqs)
616 memset(&block->profile, 0,
617 sizeof(struct dasd_profile_info_t));
618 block->profile.dasd_io_reqs++;
619 block->profile.dasd_io_sects += sectors;
1da177e4 620
8e09f215
SW
621 dasd_profile_counter(sectors, dasd_io_secs, block);
622 dasd_profile_counter(tottime, dasd_io_times, block);
623 dasd_profile_counter(tottimeps, dasd_io_timps, block);
624 dasd_profile_counter(strtime, dasd_io_time1, block);
625 dasd_profile_counter(irqtime, dasd_io_time2, block);
626 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
627 dasd_profile_counter(endtime, dasd_io_time3, block);
1da177e4
LT
628}
629#else
8e09f215
SW
630#define dasd_profile_start(block, cqr, req) do {} while (0)
631#define dasd_profile_end(block, cqr, req) do {} while (0)
1da177e4
LT
632#endif /* CONFIG_DASD_PROFILE */
633
634/*
635 * Allocate memory for a channel program with 'cplength' channel
636 * command words and 'datasize' additional space. There are two
637 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
638 * memory and 2) dasd_smalloc_request uses the static ccw memory
639 * that gets allocated for each device.
640 */
8e09f215
SW
641struct dasd_ccw_req *dasd_kmalloc_request(char *magic, int cplength,
642 int datasize,
643 struct dasd_device *device)
1da177e4
LT
644{
645 struct dasd_ccw_req *cqr;
646
647 /* Sanity checks */
7ac1e877
ES
648 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
649 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4 650
88abaab4 651 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1da177e4
LT
652 if (cqr == NULL)
653 return ERR_PTR(-ENOMEM);
1da177e4
LT
654 cqr->cpaddr = NULL;
655 if (cplength > 0) {
88abaab4 656 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1da177e4
LT
657 GFP_ATOMIC | GFP_DMA);
658 if (cqr->cpaddr == NULL) {
659 kfree(cqr);
660 return ERR_PTR(-ENOMEM);
661 }
1da177e4
LT
662 }
663 cqr->data = NULL;
664 if (datasize > 0) {
88abaab4 665 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1da177e4 666 if (cqr->data == NULL) {
17fd682e 667 kfree(cqr->cpaddr);
1da177e4
LT
668 kfree(cqr);
669 return ERR_PTR(-ENOMEM);
670 }
1da177e4
LT
671 }
672 strncpy((char *) &cqr->magic, magic, 4);
673 ASCEBC((char *) &cqr->magic, 4);
674 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
675 dasd_get_device(device);
676 return cqr;
677}
678
8e09f215
SW
679struct dasd_ccw_req *dasd_smalloc_request(char *magic, int cplength,
680 int datasize,
681 struct dasd_device *device)
1da177e4
LT
682{
683 unsigned long flags;
684 struct dasd_ccw_req *cqr;
685 char *data;
686 int size;
687
688 /* Sanity checks */
7ac1e877
ES
689 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
690 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4
LT
691
692 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
693 if (cplength > 0)
694 size += cplength * sizeof(struct ccw1);
695 if (datasize > 0)
696 size += datasize;
697 spin_lock_irqsave(&device->mem_lock, flags);
698 cqr = (struct dasd_ccw_req *)
699 dasd_alloc_chunk(&device->ccw_chunks, size);
700 spin_unlock_irqrestore(&device->mem_lock, flags);
701 if (cqr == NULL)
702 return ERR_PTR(-ENOMEM);
703 memset(cqr, 0, sizeof(struct dasd_ccw_req));
704 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
705 cqr->cpaddr = NULL;
706 if (cplength > 0) {
707 cqr->cpaddr = (struct ccw1 *) data;
708 data += cplength*sizeof(struct ccw1);
709 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
710 }
711 cqr->data = NULL;
712 if (datasize > 0) {
713 cqr->data = data;
714 memset(cqr->data, 0, datasize);
715 }
716 strncpy((char *) &cqr->magic, magic, 4);
717 ASCEBC((char *) &cqr->magic, 4);
718 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
719 dasd_get_device(device);
720 return cqr;
721}
722
723/*
724 * Free memory of a channel program. This function needs to free all the
725 * idal lists that might have been created by dasd_set_cda and the
726 * struct dasd_ccw_req itself.
727 */
8e09f215 728void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4 729{
347a8dc3 730#ifdef CONFIG_64BIT
1da177e4
LT
731 struct ccw1 *ccw;
732
733 /* Clear any idals used for the request. */
734 ccw = cqr->cpaddr;
735 do {
736 clear_normalized_cda(ccw);
737 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
738#endif
17fd682e
JJ
739 kfree(cqr->cpaddr);
740 kfree(cqr->data);
1da177e4
LT
741 kfree(cqr);
742 dasd_put_device(device);
743}
744
8e09f215 745void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4
LT
746{
747 unsigned long flags;
748
749 spin_lock_irqsave(&device->mem_lock, flags);
750 dasd_free_chunk(&device->ccw_chunks, cqr);
751 spin_unlock_irqrestore(&device->mem_lock, flags);
752 dasd_put_device(device);
753}
754
755/*
756 * Check discipline magic in cqr.
757 */
8e09f215 758static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1da177e4
LT
759{
760 struct dasd_device *device;
761
762 if (cqr == NULL)
763 return -EINVAL;
8e09f215 764 device = cqr->startdev;
1da177e4
LT
765 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
766 DEV_MESSAGE(KERN_WARNING, device,
767 " dasd_ccw_req 0x%08x magic doesn't match"
768 " discipline 0x%08x",
769 cqr->magic,
770 *(unsigned int *) device->discipline->name);
771 return -EINVAL;
772 }
773 return 0;
774}
775
776/*
777 * Terminate the current i/o and set the request to clear_pending.
778 * Timer keeps device runnig.
779 * ccw_device_clear can fail if the i/o subsystem
780 * is in a bad mood.
781 */
8e09f215 782int dasd_term_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
783{
784 struct dasd_device *device;
785 int retries, rc;
786
787 /* Check the cqr */
788 rc = dasd_check_cqr(cqr);
789 if (rc)
790 return rc;
791 retries = 0;
8e09f215 792 device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
793 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
794 rc = ccw_device_clear(device->cdev, (long) cqr);
795 switch (rc) {
796 case 0: /* termination successful */
c2ba444d 797 cqr->retries--;
8e09f215 798 cqr->status = DASD_CQR_CLEAR_PENDING;
1da177e4 799 cqr->stopclk = get_clock();
8f61701b 800 cqr->starttime = 0;
1da177e4
LT
801 DBF_DEV_EVENT(DBF_DEBUG, device,
802 "terminate cqr %p successful",
803 cqr);
804 break;
805 case -ENODEV:
806 DBF_DEV_EVENT(DBF_ERR, device, "%s",
807 "device gone, retry");
808 break;
809 case -EIO:
810 DBF_DEV_EVENT(DBF_ERR, device, "%s",
811 "I/O error, retry");
812 break;
813 case -EINVAL:
814 case -EBUSY:
815 DBF_DEV_EVENT(DBF_ERR, device, "%s",
816 "device busy, retry later");
817 break;
818 default:
819 DEV_MESSAGE(KERN_ERR, device,
820 "line %d unknown RC=%d, please "
821 "report to linux390@de.ibm.com",
822 __LINE__, rc);
823 BUG();
824 break;
825 }
826 retries++;
827 }
8e09f215 828 dasd_schedule_device_bh(device);
1da177e4
LT
829 return rc;
830}
831
832/*
833 * Start the i/o. This start_IO can fail if the channel is really busy.
834 * In that case set up a timer to start the request later.
835 */
8e09f215 836int dasd_start_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
837{
838 struct dasd_device *device;
839 int rc;
840
841 /* Check the cqr */
842 rc = dasd_check_cqr(cqr);
843 if (rc)
844 return rc;
8e09f215 845 device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
846 if (cqr->retries < 0) {
847 DEV_MESSAGE(KERN_DEBUG, device,
848 "start_IO: request %p (%02x/%i) - no retry left.",
849 cqr, cqr->status, cqr->retries);
8e09f215 850 cqr->status = DASD_CQR_ERROR;
1da177e4
LT
851 return -EIO;
852 }
853 cqr->startclk = get_clock();
854 cqr->starttime = jiffies;
855 cqr->retries--;
f3eb5384
SW
856 if (cqr->cpmode == 1) {
857 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
858 (long) cqr, cqr->lpm);
859 } else {
860 rc = ccw_device_start(device->cdev, cqr->cpaddr,
861 (long) cqr, cqr->lpm, 0);
862 }
1da177e4
LT
863 switch (rc) {
864 case 0:
865 cqr->status = DASD_CQR_IN_IO;
866 DBF_DEV_EVENT(DBF_DEBUG, device,
867 "start_IO: request %p started successful",
868 cqr);
869 break;
870 case -EBUSY:
871 DBF_DEV_EVENT(DBF_ERR, device, "%s",
872 "start_IO: device busy, retry later");
873 break;
874 case -ETIMEDOUT:
875 DBF_DEV_EVENT(DBF_ERR, device, "%s",
876 "start_IO: request timeout, retry later");
877 break;
878 case -EACCES:
879 /* -EACCES indicates that the request used only a
880 * subset of the available pathes and all these
881 * pathes are gone.
882 * Do a retry with all available pathes.
883 */
884 cqr->lpm = LPM_ANYPATH;
885 DBF_DEV_EVENT(DBF_ERR, device, "%s",
886 "start_IO: selected pathes gone,"
887 " retry on all pathes");
888 break;
889 case -ENODEV:
f3eb5384
SW
890 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
891 "start_IO: -ENODEV device gone, retry");
892 break;
1da177e4
LT
893 case -EIO:
894 DBF_DEV_EVENT(DBF_ERR, device, "%s",
f3eb5384 895 "start_IO: -EIO device gone, retry");
1da177e4
LT
896 break;
897 default:
898 DEV_MESSAGE(KERN_ERR, device,
899 "line %d unknown RC=%d, please report"
900 " to linux390@de.ibm.com", __LINE__, rc);
901 BUG();
902 break;
903 }
904 return rc;
905}
906
907/*
908 * Timeout function for dasd devices. This is used for different purposes
909 * 1) missing interrupt handler for normal operation
910 * 2) delayed start of request where start_IO failed with -EBUSY
911 * 3) timeout for missing state change interrupts
912 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
913 * DASD_CQR_QUEUED for 2) and 3).
914 */
8e09f215 915static void dasd_device_timeout(unsigned long ptr)
1da177e4
LT
916{
917 unsigned long flags;
918 struct dasd_device *device;
919
920 device = (struct dasd_device *) ptr;
921 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
922 /* re-activate request queue */
923 device->stopped &= ~DASD_STOPPED_PENDING;
924 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215 925 dasd_schedule_device_bh(device);
1da177e4
LT
926}
927
928/*
929 * Setup timeout for a device in jiffies.
930 */
8e09f215 931void dasd_device_set_timer(struct dasd_device *device, int expires)
1da177e4 932{
48cae885
SW
933 if (expires == 0)
934 del_timer(&device->timer);
935 else
936 mod_timer(&device->timer, jiffies + expires);
1da177e4
LT
937}
938
939/*
940 * Clear timeout for a device.
941 */
8e09f215 942void dasd_device_clear_timer(struct dasd_device *device)
1da177e4 943{
48cae885 944 del_timer(&device->timer);
1da177e4
LT
945}
946
8e09f215
SW
947static void dasd_handle_killed_request(struct ccw_device *cdev,
948 unsigned long intparm)
1da177e4
LT
949{
950 struct dasd_ccw_req *cqr;
951 struct dasd_device *device;
952
f16f5843
SW
953 if (!intparm)
954 return;
1da177e4
LT
955 cqr = (struct dasd_ccw_req *) intparm;
956 if (cqr->status != DASD_CQR_IN_IO) {
957 MESSAGE(KERN_DEBUG,
958 "invalid status in handle_killed_request: "
959 "bus_id %s, status %02x",
2a0217d5 960 dev_name(&cdev->dev), cqr->status);
1da177e4
LT
961 return;
962 }
963
8e09f215 964 device = (struct dasd_device *) cqr->startdev;
1da177e4 965 if (device == NULL ||
a00bfd71 966 device != dasd_device_from_cdev_locked(cdev) ||
1da177e4
LT
967 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
968 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
2a0217d5 969 dev_name(&cdev->dev));
1da177e4
LT
970 return;
971 }
972
973 /* Schedule request to be retried. */
974 cqr->status = DASD_CQR_QUEUED;
975
8e09f215
SW
976 dasd_device_clear_timer(device);
977 dasd_schedule_device_bh(device);
1da177e4
LT
978 dasd_put_device(device);
979}
980
8e09f215 981void dasd_generic_handle_state_change(struct dasd_device *device)
1da177e4 982{
20c64468
SW
983 /* First of all start sense subsystem status request. */
984 dasd_eer_snss(device);
985
1da177e4 986 device->stopped &= ~DASD_STOPPED_PENDING;
8e09f215
SW
987 dasd_schedule_device_bh(device);
988 if (device->block)
989 dasd_schedule_block_bh(device->block);
1da177e4
LT
990}
991
992/*
993 * Interrupt handler for "normal" ssch-io based dasd devices.
994 */
8e09f215
SW
995void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
996 struct irb *irb)
1da177e4
LT
997{
998 struct dasd_ccw_req *cqr, *next;
999 struct dasd_device *device;
1000 unsigned long long now;
1001 int expires;
1da177e4
LT
1002
1003 if (IS_ERR(irb)) {
1004 switch (PTR_ERR(irb)) {
1005 case -EIO:
1da177e4
LT
1006 break;
1007 case -ETIMEDOUT:
1008 printk(KERN_WARNING"%s(%s): request timed out\n",
2a0217d5 1009 __func__, dev_name(&cdev->dev));
1da177e4
LT
1010 break;
1011 default:
1012 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
2a0217d5 1013 __func__, dev_name(&cdev->dev), PTR_ERR(irb));
1da177e4 1014 }
f16f5843 1015 dasd_handle_killed_request(cdev, intparm);
1da177e4
LT
1016 return;
1017 }
1018
1019 now = get_clock();
1020
1021 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
2a0217d5 1022 dev_name(&cdev->dev), ((irb->scsw.cmd.cstat << 8) |
23d805b6 1023 irb->scsw.cmd.dstat), (unsigned int) intparm);
1da177e4 1024
8e09f215
SW
1025 /* check for unsolicited interrupts */
1026 cqr = (struct dasd_ccw_req *) intparm;
f3eb5384
SW
1027 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1028 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1029 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
8e09f215
SW
1030 if (cqr && cqr->status == DASD_CQR_IN_IO)
1031 cqr->status = DASD_CQR_QUEUED;
a00bfd71 1032 device = dasd_device_from_cdev_locked(cdev);
1da177e4 1033 if (!IS_ERR(device)) {
8e09f215
SW
1034 dasd_device_clear_timer(device);
1035 device->discipline->handle_unsolicited_interrupt(device,
1036 irb);
1da177e4
LT
1037 dasd_put_device(device);
1038 }
1039 return;
1040 }
1041
8e09f215
SW
1042 device = (struct dasd_device *) cqr->startdev;
1043 if (!device ||
1da177e4
LT
1044 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1045 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
2a0217d5 1046 dev_name(&cdev->dev));
1da177e4
LT
1047 return;
1048 }
1049
1050 /* Check for clear pending */
8e09f215 1051 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
f3eb5384 1052 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
8e09f215
SW
1053 cqr->status = DASD_CQR_CLEARED;
1054 dasd_device_clear_timer(device);
8f61701b 1055 wake_up(&dasd_flush_wq);
8e09f215 1056 dasd_schedule_device_bh(device);
1da177e4
LT
1057 return;
1058 }
1059
f3eb5384 1060 /* check status - the request might have been killed by dyn detach */
1da177e4
LT
1061 if (cqr->status != DASD_CQR_IN_IO) {
1062 MESSAGE(KERN_DEBUG,
1063 "invalid status: bus_id %s, status %02x",
2a0217d5 1064 dev_name(&cdev->dev), cqr->status);
1da177e4
LT
1065 return;
1066 }
1067 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
23d805b6 1068 ((irb->scsw.cmd.cstat << 8) | irb->scsw.cmd.dstat), cqr);
8e09f215 1069 next = NULL;
1da177e4 1070 expires = 0;
f3eb5384
SW
1071 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1072 scsw_cstat(&irb->scsw) == 0) {
8e09f215
SW
1073 /* request was completed successfully */
1074 cqr->status = DASD_CQR_SUCCESS;
1da177e4
LT
1075 cqr->stopclk = now;
1076 /* Start first request on queue if possible -> fast_io. */
8e09f215
SW
1077 if (cqr->devlist.next != &device->ccw_queue) {
1078 next = list_entry(cqr->devlist.next,
1079 struct dasd_ccw_req, devlist);
1da177e4 1080 }
8e09f215
SW
1081 } else { /* error */
1082 memcpy(&cqr->irb, irb, sizeof(struct irb));
9575bf26 1083 if (device->features & DASD_FEATURE_ERPLOG) {
9575bf26
HH
1084 dasd_log_sense(cqr, irb);
1085 }
6c5f57c7
SH
1086 /*
1087 * If we don't want complex ERP for this request, then just
1088 * reset this and retry it in the fastpath
8e09f215 1089 */
6c5f57c7 1090 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
8e09f215
SW
1091 cqr->retries > 0) {
1092 DEV_MESSAGE(KERN_DEBUG, device,
1093 "default ERP in fastpath (%i retries left)",
1094 cqr->retries);
1095 cqr->lpm = LPM_ANYPATH;
1096 cqr->status = DASD_CQR_QUEUED;
1097 next = cqr;
1098 } else
1da177e4 1099 cqr->status = DASD_CQR_ERROR;
8e09f215
SW
1100 }
1101 if (next && (next->status == DASD_CQR_QUEUED) &&
1102 (!device->stopped)) {
1103 if (device->discipline->start_IO(next) == 0)
1104 expires = next->expires;
1105 else
1106 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1107 "Interrupt fastpath "
1108 "failed!");
1da177e4
LT
1109 }
1110 if (expires != 0)
8e09f215 1111 dasd_device_set_timer(device, expires);
1da177e4 1112 else
8e09f215
SW
1113 dasd_device_clear_timer(device);
1114 dasd_schedule_device_bh(device);
1da177e4
LT
1115}
1116
1117/*
8e09f215
SW
1118 * If we have an error on a dasd_block layer request then we cancel
1119 * and return all further requests from the same dasd_block as well.
1da177e4 1120 */
8e09f215
SW
1121static void __dasd_device_recovery(struct dasd_device *device,
1122 struct dasd_ccw_req *ref_cqr)
1da177e4 1123{
8e09f215
SW
1124 struct list_head *l, *n;
1125 struct dasd_ccw_req *cqr;
1da177e4 1126
8e09f215
SW
1127 /*
1128 * only requeue request that came from the dasd_block layer
1129 */
1130 if (!ref_cqr->block)
1131 return;
1da177e4 1132
8e09f215
SW
1133 list_for_each_safe(l, n, &device->ccw_queue) {
1134 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1135 if (cqr->status == DASD_CQR_QUEUED &&
1136 ref_cqr->block == cqr->block) {
1137 cqr->status = DASD_CQR_CLEARED;
1138 }
1139 }
1140};
1da177e4
LT
1141
1142/*
8e09f215
SW
1143 * Remove those ccw requests from the queue that need to be returned
1144 * to the upper layer.
1da177e4 1145 */
8e09f215
SW
1146static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1147 struct list_head *final_queue)
1da177e4
LT
1148{
1149 struct list_head *l, *n;
1150 struct dasd_ccw_req *cqr;
1da177e4 1151
1da177e4
LT
1152 /* Process request with final status. */
1153 list_for_each_safe(l, n, &device->ccw_queue) {
8e09f215
SW
1154 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1155
1da177e4 1156 /* Stop list processing at the first non-final request. */
8e09f215
SW
1157 if (cqr->status == DASD_CQR_QUEUED ||
1158 cqr->status == DASD_CQR_IN_IO ||
1159 cqr->status == DASD_CQR_CLEAR_PENDING)
1da177e4 1160 break;
1da177e4 1161 if (cqr->status == DASD_CQR_ERROR) {
8e09f215 1162 __dasd_device_recovery(device, cqr);
20c64468 1163 }
1da177e4 1164 /* Rechain finished requests to final queue */
8e09f215 1165 list_move_tail(&cqr->devlist, final_queue);
1da177e4
LT
1166 }
1167}
1168
1da177e4 1169/*
8e09f215
SW
1170 * the cqrs from the final queue are returned to the upper layer
1171 * by setting a dasd_block state and calling the callback function
1da177e4 1172 */
8e09f215
SW
1173static void __dasd_device_process_final_queue(struct dasd_device *device,
1174 struct list_head *final_queue)
1da177e4 1175{
8e09f215 1176 struct list_head *l, *n;
1da177e4 1177 struct dasd_ccw_req *cqr;
03513bcc 1178 struct dasd_block *block;
c80ee724
SH
1179 void (*callback)(struct dasd_ccw_req *, void *data);
1180 void *callback_data;
f24acd45 1181
8e09f215
SW
1182 list_for_each_safe(l, n, final_queue) {
1183 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1184 list_del_init(&cqr->devlist);
03513bcc 1185 block = cqr->block;
c80ee724
SH
1186 callback = cqr->callback;
1187 callback_data = cqr->callback_data;
03513bcc
SW
1188 if (block)
1189 spin_lock_bh(&block->queue_lock);
8e09f215
SW
1190 switch (cqr->status) {
1191 case DASD_CQR_SUCCESS:
1192 cqr->status = DASD_CQR_DONE;
1193 break;
1194 case DASD_CQR_ERROR:
1195 cqr->status = DASD_CQR_NEED_ERP;
1196 break;
1197 case DASD_CQR_CLEARED:
1198 cqr->status = DASD_CQR_TERMINATED;
1199 break;
1200 default:
1201 DEV_MESSAGE(KERN_ERR, device,
1202 "wrong cqr status in __dasd_process_final_queue "
1203 "for cqr %p, status %x",
1204 cqr, cqr->status);
1205 BUG();
1da177e4 1206 }
8e09f215 1207 if (cqr->callback != NULL)
c80ee724 1208 (callback)(cqr, callback_data);
03513bcc
SW
1209 if (block)
1210 spin_unlock_bh(&block->queue_lock);
1da177e4
LT
1211 }
1212}
1213
1214/*
1215 * Take a look at the first request on the ccw queue and check
1216 * if it reached its expire time. If so, terminate the IO.
1217 */
8e09f215 1218static void __dasd_device_check_expire(struct dasd_device *device)
1da177e4
LT
1219{
1220 struct dasd_ccw_req *cqr;
1221
1222 if (list_empty(&device->ccw_queue))
1223 return;
8e09f215 1224 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
29145a6c
HH
1225 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1226 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1227 if (device->discipline->term_IO(cqr) != 0) {
1228 /* Hmpf, try again in 5 sec */
29145a6c
HH
1229 DEV_MESSAGE(KERN_ERR, device,
1230 "internal error - timeout (%is) expired "
1231 "for cqr %p, termination failed, "
1232 "retrying in 5s",
1233 (cqr->expires/HZ), cqr);
7dc1da9f
SH
1234 cqr->expires += 5*HZ;
1235 dasd_device_set_timer(device, 5*HZ);
29145a6c 1236 } else {
8f61701b
HH
1237 DEV_MESSAGE(KERN_ERR, device,
1238 "internal error - timeout (%is) expired "
1239 "for cqr %p (%i retries left)",
1240 (cqr->expires/HZ), cqr, cqr->retries);
1da177e4
LT
1241 }
1242 }
1243}
1244
1245/*
1246 * Take a look at the first request on the ccw queue and check
1247 * if it needs to be started.
1248 */
8e09f215 1249static void __dasd_device_start_head(struct dasd_device *device)
1da177e4
LT
1250{
1251 struct dasd_ccw_req *cqr;
1252 int rc;
1253
1254 if (list_empty(&device->ccw_queue))
1255 return;
8e09f215 1256 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
25ee4cf8
PO
1257 if (cqr->status != DASD_CQR_QUEUED)
1258 return;
8e09f215
SW
1259 /* when device is stopped, return request to previous layer */
1260 if (device->stopped) {
1261 cqr->status = DASD_CQR_CLEARED;
1262 dasd_schedule_device_bh(device);
25ee4cf8 1263 return;
1c01b8a5 1264 }
25ee4cf8
PO
1265
1266 rc = device->discipline->start_IO(cqr);
1267 if (rc == 0)
8e09f215 1268 dasd_device_set_timer(device, cqr->expires);
25ee4cf8 1269 else if (rc == -EACCES) {
8e09f215 1270 dasd_schedule_device_bh(device);
25ee4cf8
PO
1271 } else
1272 /* Hmpf, try again in 1/2 sec */
8e09f215 1273 dasd_device_set_timer(device, 50);
8f61701b
HH
1274}
1275
1da177e4 1276/*
8e09f215
SW
1277 * Go through all request on the dasd_device request queue,
1278 * terminate them on the cdev if necessary, and return them to the
1279 * submitting layer via callback.
1280 * Note:
1281 * Make sure that all 'submitting layers' still exist when
1282 * this function is called!. In other words, when 'device' is a base
1283 * device then all block layer requests must have been removed before
1284 * via dasd_flush_block_queue.
1da177e4 1285 */
8e09f215 1286int dasd_flush_device_queue(struct dasd_device *device)
1da177e4 1287{
8e09f215
SW
1288 struct dasd_ccw_req *cqr, *n;
1289 int rc;
1da177e4 1290 struct list_head flush_queue;
1da177e4
LT
1291
1292 INIT_LIST_HEAD(&flush_queue);
1293 spin_lock_irq(get_ccwdev_lock(device->cdev));
8f61701b 1294 rc = 0;
8e09f215 1295 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
8f61701b
HH
1296 /* Check status and move request to flush_queue */
1297 switch (cqr->status) {
1298 case DASD_CQR_IN_IO:
1299 rc = device->discipline->term_IO(cqr);
1300 if (rc) {
1301 /* unable to terminate requeust */
1302 DEV_MESSAGE(KERN_ERR, device,
1303 "dasd flush ccw_queue is unable "
1304 " to terminate request %p",
1305 cqr);
1306 /* stop flush processing */
1307 goto finished;
1308 }
1309 break;
1310 case DASD_CQR_QUEUED:
1da177e4 1311 cqr->stopclk = get_clock();
8e09f215 1312 cqr->status = DASD_CQR_CLEARED;
8f61701b 1313 break;
8e09f215 1314 default: /* no need to modify the others */
8f61701b
HH
1315 break;
1316 }
8e09f215 1317 list_move_tail(&cqr->devlist, &flush_queue);
8f61701b 1318 }
8f61701b
HH
1319finished:
1320 spin_unlock_irq(get_ccwdev_lock(device->cdev));
8e09f215
SW
1321 /*
1322 * After this point all requests must be in state CLEAR_PENDING,
1323 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1324 * one of the others.
1325 */
1326 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1327 wait_event(dasd_flush_wq,
1328 (cqr->status != DASD_CQR_CLEAR_PENDING));
1329 /*
1330 * Now set each request back to TERMINATED, DONE or NEED_ERP
1331 * and call the callback function of flushed requests
1332 */
1333 __dasd_device_process_final_queue(device, &flush_queue);
8f61701b 1334 return rc;
1da177e4
LT
1335}
1336
1337/*
1338 * Acquire the device lock and process queues for the device.
1339 */
8e09f215 1340static void dasd_device_tasklet(struct dasd_device *device)
1da177e4
LT
1341{
1342 struct list_head final_queue;
1da177e4
LT
1343
1344 atomic_set (&device->tasklet_scheduled, 0);
1345 INIT_LIST_HEAD(&final_queue);
1346 spin_lock_irq(get_ccwdev_lock(device->cdev));
1347 /* Check expire time of first request on the ccw queue. */
8e09f215
SW
1348 __dasd_device_check_expire(device);
1349 /* find final requests on ccw queue */
1350 __dasd_device_process_ccw_queue(device, &final_queue);
1da177e4
LT
1351 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1352 /* Now call the callback function of requests with final status */
8e09f215
SW
1353 __dasd_device_process_final_queue(device, &final_queue);
1354 spin_lock_irq(get_ccwdev_lock(device->cdev));
1da177e4 1355 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
1356 __dasd_device_start_head(device);
1357 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1da177e4
LT
1358 dasd_put_device(device);
1359}
1360
1361/*
1362 * Schedules a call to dasd_tasklet over the device tasklet.
1363 */
8e09f215 1364void dasd_schedule_device_bh(struct dasd_device *device)
1da177e4
LT
1365{
1366 /* Protect against rescheduling. */
973bd993 1367 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1da177e4
LT
1368 return;
1369 dasd_get_device(device);
1370 tasklet_hi_schedule(&device->tasklet);
1371}
1372
1373/*
8e09f215
SW
1374 * Queue a request to the head of the device ccw_queue.
1375 * Start the I/O if possible.
1da177e4 1376 */
8e09f215 1377void dasd_add_request_head(struct dasd_ccw_req *cqr)
1da177e4
LT
1378{
1379 struct dasd_device *device;
1380 unsigned long flags;
1381
8e09f215 1382 device = cqr->startdev;
1da177e4 1383 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
1384 cqr->status = DASD_CQR_QUEUED;
1385 list_add(&cqr->devlist, &device->ccw_queue);
1da177e4 1386 /* let the bh start the request to keep them in order */
8e09f215 1387 dasd_schedule_device_bh(device);
1da177e4
LT
1388 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1389}
1390
1391/*
8e09f215
SW
1392 * Queue a request to the tail of the device ccw_queue.
1393 * Start the I/O if possible.
1da177e4 1394 */
8e09f215 1395void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1da177e4
LT
1396{
1397 struct dasd_device *device;
1398 unsigned long flags;
1399
8e09f215 1400 device = cqr->startdev;
1da177e4 1401 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
1402 cqr->status = DASD_CQR_QUEUED;
1403 list_add_tail(&cqr->devlist, &device->ccw_queue);
1da177e4 1404 /* let the bh start the request to keep them in order */
8e09f215 1405 dasd_schedule_device_bh(device);
1da177e4
LT
1406 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1407}
1408
1409/*
8e09f215 1410 * Wakeup helper for the 'sleep_on' functions.
1da177e4 1411 */
8e09f215 1412static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1da177e4
LT
1413{
1414 wake_up((wait_queue_head_t *) data);
1415}
1416
8e09f215 1417static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1da177e4
LT
1418{
1419 struct dasd_device *device;
1420 int rc;
1421
8e09f215 1422 device = cqr->startdev;
1da177e4 1423 spin_lock_irq(get_ccwdev_lock(device->cdev));
c2ba444d 1424 rc = ((cqr->status == DASD_CQR_DONE ||
8e09f215
SW
1425 cqr->status == DASD_CQR_NEED_ERP ||
1426 cqr->status == DASD_CQR_TERMINATED) &&
1427 list_empty(&cqr->devlist));
1da177e4
LT
1428 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1429 return rc;
1430}
1431
1432/*
8e09f215
SW
1433 * Queue a request to the tail of the device ccw_queue and wait for
1434 * it's completion.
1da177e4 1435 */
8e09f215 1436int dasd_sleep_on(struct dasd_ccw_req *cqr)
1da177e4 1437{
1da177e4
LT
1438 struct dasd_device *device;
1439 int rc;
138c014d 1440
8e09f215 1441 device = cqr->startdev;
138c014d 1442
1da177e4 1443 cqr->callback = dasd_wakeup_cb;
c80ee724 1444 cqr->callback_data = (void *) &generic_waitq;
8e09f215 1445 dasd_add_request_tail(cqr);
c80ee724 1446 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 1447
1da177e4 1448 /* Request status is either done or failed. */
8e09f215 1449 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1450 return rc;
1451}
1452
1453/*
8e09f215
SW
1454 * Queue a request to the tail of the device ccw_queue and wait
1455 * interruptible for it's completion.
1da177e4 1456 */
8e09f215 1457int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1da177e4 1458{
1da177e4 1459 struct dasd_device *device;
8e09f215 1460 int rc;
1da177e4 1461
8e09f215 1462 device = cqr->startdev;
1da177e4 1463 cqr->callback = dasd_wakeup_cb;
c80ee724 1464 cqr->callback_data = (void *) &generic_waitq;
8e09f215 1465 dasd_add_request_tail(cqr);
c80ee724 1466 rc = wait_event_interruptible(generic_waitq, _wait_for_wakeup(cqr));
8e09f215
SW
1467 if (rc == -ERESTARTSYS) {
1468 dasd_cancel_req(cqr);
1469 /* wait (non-interruptible) for final status */
c80ee724 1470 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1da177e4 1471 }
8e09f215 1472 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1473 return rc;
1474}
1475
1476/*
1477 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1478 * for eckd devices) the currently running request has to be terminated
1479 * and be put back to status queued, before the special request is added
1480 * to the head of the queue. Then the special request is waited on normally.
1481 */
8e09f215 1482static inline int _dasd_term_running_cqr(struct dasd_device *device)
1da177e4
LT
1483{
1484 struct dasd_ccw_req *cqr;
1da177e4
LT
1485
1486 if (list_empty(&device->ccw_queue))
1487 return 0;
8e09f215 1488 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
8f61701b 1489 return device->discipline->term_IO(cqr);
1da177e4
LT
1490}
1491
8e09f215 1492int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1da177e4 1493{
1da177e4
LT
1494 struct dasd_device *device;
1495 int rc;
138c014d 1496
8e09f215 1497 device = cqr->startdev;
1da177e4
LT
1498 spin_lock_irq(get_ccwdev_lock(device->cdev));
1499 rc = _dasd_term_running_cqr(device);
1500 if (rc) {
1501 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1502 return rc;
1503 }
138c014d 1504
1da177e4 1505 cqr->callback = dasd_wakeup_cb;
c80ee724 1506 cqr->callback_data = (void *) &generic_waitq;
1da177e4 1507 cqr->status = DASD_CQR_QUEUED;
8e09f215 1508 list_add(&cqr->devlist, &device->ccw_queue);
138c014d 1509
1da177e4 1510 /* let the bh start the request to keep them in order */
8e09f215 1511 dasd_schedule_device_bh(device);
138c014d 1512
1da177e4
LT
1513 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1514
c80ee724 1515 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 1516
1da177e4 1517 /* Request status is either done or failed. */
8e09f215 1518 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1da177e4
LT
1519 return rc;
1520}
1521
1522/*
1523 * Cancels a request that was started with dasd_sleep_on_req.
1524 * This is useful to timeout requests. The request will be
1525 * terminated if it is currently in i/o.
1526 * Returns 1 if the request has been terminated.
8e09f215
SW
1527 * 0 if there was no need to terminate the request (not started yet)
1528 * negative error code if termination failed
1529 * Cancellation of a request is an asynchronous operation! The calling
1530 * function has to wait until the request is properly returned via callback.
1da177e4 1531 */
8e09f215 1532int dasd_cancel_req(struct dasd_ccw_req *cqr)
1da177e4 1533{
8e09f215 1534 struct dasd_device *device = cqr->startdev;
1da177e4
LT
1535 unsigned long flags;
1536 int rc;
1537
1538 rc = 0;
1539 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1540 switch (cqr->status) {
1541 case DASD_CQR_QUEUED:
8e09f215
SW
1542 /* request was not started - just set to cleared */
1543 cqr->status = DASD_CQR_CLEARED;
1da177e4
LT
1544 break;
1545 case DASD_CQR_IN_IO:
1546 /* request in IO - terminate IO and release again */
8e09f215
SW
1547 rc = device->discipline->term_IO(cqr);
1548 if (rc) {
1549 DEV_MESSAGE(KERN_ERR, device,
1550 "dasd_cancel_req is unable "
1551 " to terminate request %p, rc = %d",
1552 cqr, rc);
1553 } else {
1554 cqr->stopclk = get_clock();
1555 rc = 1;
1556 }
1da177e4 1557 break;
8e09f215 1558 default: /* already finished or clear pending - do nothing */
1da177e4 1559 break;
8e09f215
SW
1560 }
1561 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1562 dasd_schedule_device_bh(device);
1563 return rc;
1564}
1565
1566
1567/*
1568 * SECTION: Operations of the dasd_block layer.
1569 */
1570
1571/*
1572 * Timeout function for dasd_block. This is used when the block layer
1573 * is waiting for something that may not come reliably, (e.g. a state
1574 * change interrupt)
1575 */
1576static void dasd_block_timeout(unsigned long ptr)
1577{
1578 unsigned long flags;
1579 struct dasd_block *block;
1580
1581 block = (struct dasd_block *) ptr;
1582 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1583 /* re-activate request queue */
1584 block->base->stopped &= ~DASD_STOPPED_PENDING;
1585 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1586 dasd_schedule_block_bh(block);
1587}
1588
1589/*
1590 * Setup timeout for a dasd_block in jiffies.
1591 */
1592void dasd_block_set_timer(struct dasd_block *block, int expires)
1593{
48cae885
SW
1594 if (expires == 0)
1595 del_timer(&block->timer);
1596 else
1597 mod_timer(&block->timer, jiffies + expires);
8e09f215
SW
1598}
1599
1600/*
1601 * Clear timeout for a dasd_block.
1602 */
1603void dasd_block_clear_timer(struct dasd_block *block)
1604{
48cae885 1605 del_timer(&block->timer);
8e09f215
SW
1606}
1607
1608/*
1609 * posts the buffer_cache about a finalized request
1610 */
4c4e2148 1611static inline void dasd_end_request(struct request *req, int error)
8e09f215 1612{
4c4e2148 1613 if (__blk_end_request(req, error, blk_rq_bytes(req)))
1da177e4 1614 BUG();
8e09f215
SW
1615}
1616
1617/*
1618 * Process finished error recovery ccw.
1619 */
1620static inline void __dasd_block_process_erp(struct dasd_block *block,
1621 struct dasd_ccw_req *cqr)
1622{
1623 dasd_erp_fn_t erp_fn;
1624 struct dasd_device *device = block->base;
1625
1626 if (cqr->status == DASD_CQR_DONE)
1627 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1628 else
1629 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1630 erp_fn = device->discipline->erp_postaction(cqr);
1631 erp_fn(cqr);
1632}
1da177e4 1633
8e09f215
SW
1634/*
1635 * Fetch requests from the block device queue.
1636 */
1637static void __dasd_process_request_queue(struct dasd_block *block)
1638{
1639 struct request_queue *queue;
1640 struct request *req;
1641 struct dasd_ccw_req *cqr;
1642 struct dasd_device *basedev;
1643 unsigned long flags;
1644 queue = block->request_queue;
1645 basedev = block->base;
1646 /* No queue ? Then there is nothing to do. */
1647 if (queue == NULL)
1648 return;
1649
1650 /*
1651 * We requeue request from the block device queue to the ccw
1652 * queue only in two states. In state DASD_STATE_READY the
1653 * partition detection is done and we need to requeue requests
1654 * for that. State DASD_STATE_ONLINE is normal block device
1655 * operation.
1656 */
1657 if (basedev->state < DASD_STATE_READY)
1658 return;
1659 /* Now we try to fetch requests from the request queue */
1660 while (!blk_queue_plugged(queue) &&
1661 elv_next_request(queue)) {
1662
1663 req = elv_next_request(queue);
1664
1665 if (basedev->features & DASD_FEATURE_READONLY &&
1666 rq_data_dir(req) == WRITE) {
1667 DBF_DEV_EVENT(DBF_ERR, basedev,
1668 "Rejecting write request %p",
1669 req);
1670 blkdev_dequeue_request(req);
4c4e2148 1671 dasd_end_request(req, -EIO);
8e09f215
SW
1672 continue;
1673 }
1674 cqr = basedev->discipline->build_cp(basedev, block, req);
1675 if (IS_ERR(cqr)) {
1676 if (PTR_ERR(cqr) == -EBUSY)
1677 break; /* normal end condition */
1678 if (PTR_ERR(cqr) == -ENOMEM)
1679 break; /* terminate request queue loop */
1680 if (PTR_ERR(cqr) == -EAGAIN) {
1681 /*
1682 * The current request cannot be build right
1683 * now, we have to try later. If this request
1684 * is the head-of-queue we stop the device
1685 * for 1/2 second.
1686 */
1687 if (!list_empty(&block->ccw_queue))
1688 break;
1689 spin_lock_irqsave(get_ccwdev_lock(basedev->cdev), flags);
1690 basedev->stopped |= DASD_STOPPED_PENDING;
1691 spin_unlock_irqrestore(get_ccwdev_lock(basedev->cdev), flags);
1692 dasd_block_set_timer(block, HZ/2);
1693 break;
1694 }
1695 DBF_DEV_EVENT(DBF_ERR, basedev,
1696 "CCW creation failed (rc=%ld) "
1697 "on request %p",
1698 PTR_ERR(cqr), req);
1699 blkdev_dequeue_request(req);
4c4e2148 1700 dasd_end_request(req, -EIO);
8e09f215
SW
1701 continue;
1702 }
1703 /*
1704 * Note: callback is set to dasd_return_cqr_cb in
1705 * __dasd_block_start_head to cover erp requests as well
1706 */
1707 cqr->callback_data = (void *) req;
1708 cqr->status = DASD_CQR_FILLED;
1709 blkdev_dequeue_request(req);
1710 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1711 dasd_profile_start(block, cqr, req);
1712 }
1713}
1714
1715static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1716{
1717 struct request *req;
1718 int status;
4c4e2148 1719 int error = 0;
8e09f215
SW
1720
1721 req = (struct request *) cqr->callback_data;
1722 dasd_profile_end(cqr->block, cqr, req);
fe6b8e76 1723 status = cqr->block->base->discipline->free_cp(cqr, req);
4c4e2148
KU
1724 if (status <= 0)
1725 error = status ? status : -EIO;
1726 dasd_end_request(req, error);
8e09f215
SW
1727}
1728
1729/*
1730 * Process ccw request queue.
1731 */
1732static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1733 struct list_head *final_queue)
1734{
1735 struct list_head *l, *n;
1736 struct dasd_ccw_req *cqr;
1737 dasd_erp_fn_t erp_fn;
1738 unsigned long flags;
1739 struct dasd_device *base = block->base;
1740
1741restart:
1742 /* Process request with final status. */
1743 list_for_each_safe(l, n, &block->ccw_queue) {
1744 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1745 if (cqr->status != DASD_CQR_DONE &&
1746 cqr->status != DASD_CQR_FAILED &&
1747 cqr->status != DASD_CQR_NEED_ERP &&
1748 cqr->status != DASD_CQR_TERMINATED)
1749 continue;
1750
1751 if (cqr->status == DASD_CQR_TERMINATED) {
1752 base->discipline->handle_terminated_request(cqr);
1753 goto restart;
1754 }
1755
1756 /* Process requests that may be recovered */
1757 if (cqr->status == DASD_CQR_NEED_ERP) {
6c5f57c7
SH
1758 erp_fn = base->discipline->erp_action(cqr);
1759 erp_fn(cqr);
8e09f215
SW
1760 goto restart;
1761 }
1762
a9cffb22
SH
1763 /* log sense for fatal error */
1764 if (cqr->status == DASD_CQR_FAILED) {
1765 dasd_log_sense(cqr, &cqr->irb);
1766 }
1767
8e09f215
SW
1768 /* First of all call extended error reporting. */
1769 if (dasd_eer_enabled(base) &&
1770 cqr->status == DASD_CQR_FAILED) {
1771 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1772
1773 /* restart request */
1774 cqr->status = DASD_CQR_FILLED;
1775 cqr->retries = 255;
1776 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1777 base->stopped |= DASD_STOPPED_QUIESCE;
1778 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1779 flags);
1780 goto restart;
1781 }
1782
1783 /* Process finished ERP request. */
1784 if (cqr->refers) {
1785 __dasd_block_process_erp(block, cqr);
1786 goto restart;
1787 }
1788
1789 /* Rechain finished requests to final queue */
1790 cqr->endclk = get_clock();
1791 list_move_tail(&cqr->blocklist, final_queue);
1792 }
1793}
1794
1795static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1796{
1797 dasd_schedule_block_bh(cqr->block);
1798}
1799
1800static void __dasd_block_start_head(struct dasd_block *block)
1801{
1802 struct dasd_ccw_req *cqr;
1803
1804 if (list_empty(&block->ccw_queue))
1805 return;
1806 /* We allways begin with the first requests on the queue, as some
1807 * of previously started requests have to be enqueued on a
1808 * dasd_device again for error recovery.
1809 */
1810 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1811 if (cqr->status != DASD_CQR_FILLED)
1812 continue;
1813 /* Non-temporary stop condition will trigger fail fast */
1814 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1815 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1816 (!dasd_eer_enabled(block->base))) {
1817 cqr->status = DASD_CQR_FAILED;
1818 dasd_schedule_block_bh(block);
1819 continue;
1820 }
1821 /* Don't try to start requests if device is stopped */
1822 if (block->base->stopped)
1823 return;
1824
1825 /* just a fail safe check, should not happen */
1826 if (!cqr->startdev)
1827 cqr->startdev = block->base;
1828
1829 /* make sure that the requests we submit find their way back */
1830 cqr->callback = dasd_return_cqr_cb;
1831
1832 dasd_add_request_tail(cqr);
1833 }
1834}
1835
1836/*
1837 * Central dasd_block layer routine. Takes requests from the generic
1838 * block layer request queue, creates ccw requests, enqueues them on
1839 * a dasd_device and processes ccw requests that have been returned.
1840 */
1841static void dasd_block_tasklet(struct dasd_block *block)
1842{
1843 struct list_head final_queue;
1844 struct list_head *l, *n;
1845 struct dasd_ccw_req *cqr;
1846
1847 atomic_set(&block->tasklet_scheduled, 0);
1848 INIT_LIST_HEAD(&final_queue);
1849 spin_lock(&block->queue_lock);
1850 /* Finish off requests on ccw queue */
1851 __dasd_process_block_ccw_queue(block, &final_queue);
1852 spin_unlock(&block->queue_lock);
1853 /* Now call the callback function of requests with final status */
1854 spin_lock_irq(&block->request_queue_lock);
1855 list_for_each_safe(l, n, &final_queue) {
1856 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1857 list_del_init(&cqr->blocklist);
1858 __dasd_cleanup_cqr(cqr);
1859 }
1860 spin_lock(&block->queue_lock);
1861 /* Get new request from the block device request queue */
1862 __dasd_process_request_queue(block);
1863 /* Now check if the head of the ccw queue needs to be started. */
1864 __dasd_block_start_head(block);
1865 spin_unlock(&block->queue_lock);
1866 spin_unlock_irq(&block->request_queue_lock);
1867 dasd_put_device(block->base);
1868}
1869
1870static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
1871{
1872 wake_up(&dasd_flush_wq);
1873}
1874
1875/*
1876 * Go through all request on the dasd_block request queue, cancel them
1877 * on the respective dasd_device, and return them to the generic
1878 * block layer.
1879 */
1880static int dasd_flush_block_queue(struct dasd_block *block)
1881{
1882 struct dasd_ccw_req *cqr, *n;
1883 int rc, i;
1884 struct list_head flush_queue;
1885
1886 INIT_LIST_HEAD(&flush_queue);
1887 spin_lock_bh(&block->queue_lock);
1888 rc = 0;
1889restart:
1890 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
1891 /* if this request currently owned by a dasd_device cancel it */
1892 if (cqr->status >= DASD_CQR_QUEUED)
1893 rc = dasd_cancel_req(cqr);
1894 if (rc < 0)
1895 break;
1896 /* Rechain request (including erp chain) so it won't be
1897 * touched by the dasd_block_tasklet anymore.
1898 * Replace the callback so we notice when the request
1899 * is returned from the dasd_device layer.
1900 */
1901 cqr->callback = _dasd_wake_block_flush_cb;
1902 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
1903 list_move_tail(&cqr->blocklist, &flush_queue);
1904 if (i > 1)
1905 /* moved more than one request - need to restart */
1906 goto restart;
1907 }
1908 spin_unlock_bh(&block->queue_lock);
1909 /* Now call the callback function of flushed requests */
1910restart_cb:
1911 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
1912 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
1913 /* Process finished ERP request. */
1914 if (cqr->refers) {
0cd4bd47 1915 spin_lock_bh(&block->queue_lock);
8e09f215 1916 __dasd_block_process_erp(block, cqr);
0cd4bd47 1917 spin_unlock_bh(&block->queue_lock);
8e09f215
SW
1918 /* restart list_for_xx loop since dasd_process_erp
1919 * might remove multiple elements */
1920 goto restart_cb;
1921 }
1922 /* call the callback function */
0cd4bd47 1923 spin_lock_irq(&block->request_queue_lock);
8e09f215
SW
1924 cqr->endclk = get_clock();
1925 list_del_init(&cqr->blocklist);
1926 __dasd_cleanup_cqr(cqr);
0cd4bd47 1927 spin_unlock_irq(&block->request_queue_lock);
1da177e4 1928 }
1da177e4
LT
1929 return rc;
1930}
1931
1932/*
8e09f215
SW
1933 * Schedules a call to dasd_tasklet over the device tasklet.
1934 */
1935void dasd_schedule_block_bh(struct dasd_block *block)
1936{
1937 /* Protect against rescheduling. */
1938 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
1939 return;
1940 /* life cycle of block is bound to it's base device */
1941 dasd_get_device(block->base);
1942 tasklet_hi_schedule(&block->tasklet);
1943}
1944
1945
1946/*
1947 * SECTION: external block device operations
1948 * (request queue handling, open, release, etc.)
1da177e4
LT
1949 */
1950
1951/*
1952 * Dasd request queue function. Called from ll_rw_blk.c
1953 */
8e09f215 1954static void do_dasd_request(struct request_queue *queue)
1da177e4 1955{
8e09f215 1956 struct dasd_block *block;
1da177e4 1957
8e09f215
SW
1958 block = queue->queuedata;
1959 spin_lock(&block->queue_lock);
1da177e4 1960 /* Get new request from the block device request queue */
8e09f215 1961 __dasd_process_request_queue(block);
1da177e4 1962 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
1963 __dasd_block_start_head(block);
1964 spin_unlock(&block->queue_lock);
1da177e4
LT
1965}
1966
1967/*
1968 * Allocate and initialize request queue and default I/O scheduler.
1969 */
8e09f215 1970static int dasd_alloc_queue(struct dasd_block *block)
1da177e4
LT
1971{
1972 int rc;
1973
8e09f215
SW
1974 block->request_queue = blk_init_queue(do_dasd_request,
1975 &block->request_queue_lock);
1976 if (block->request_queue == NULL)
1da177e4
LT
1977 return -ENOMEM;
1978
8e09f215 1979 block->request_queue->queuedata = block;
1da177e4 1980
8e09f215 1981 elevator_exit(block->request_queue->elevator);
08a8a0c5 1982 block->request_queue->elevator = NULL;
8e09f215 1983 rc = elevator_init(block->request_queue, "deadline");
1da177e4 1984 if (rc) {
8e09f215 1985 blk_cleanup_queue(block->request_queue);
1da177e4
LT
1986 return rc;
1987 }
1988 return 0;
1989}
1990
1991/*
1992 * Allocate and initialize request queue.
1993 */
8e09f215 1994static void dasd_setup_queue(struct dasd_block *block)
1da177e4
LT
1995{
1996 int max;
1997
8e09f215
SW
1998 blk_queue_hardsect_size(block->request_queue, block->bp_block);
1999 max = block->base->discipline->max_blocks << block->s2b_shift;
2000 blk_queue_max_sectors(block->request_queue, max);
2001 blk_queue_max_phys_segments(block->request_queue, -1L);
2002 blk_queue_max_hw_segments(block->request_queue, -1L);
f3eb5384
SW
2003 /* with page sized segments we can translate each segement into
2004 * one idaw/tidaw
2005 */
2006 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2007 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
8e09f215 2008 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
1da177e4
LT
2009}
2010
2011/*
2012 * Deactivate and free request queue.
2013 */
8e09f215 2014static void dasd_free_queue(struct dasd_block *block)
1da177e4 2015{
8e09f215
SW
2016 if (block->request_queue) {
2017 blk_cleanup_queue(block->request_queue);
2018 block->request_queue = NULL;
1da177e4
LT
2019 }
2020}
2021
2022/*
2023 * Flush request on the request queue.
2024 */
8e09f215 2025static void dasd_flush_request_queue(struct dasd_block *block)
1da177e4
LT
2026{
2027 struct request *req;
2028
8e09f215 2029 if (!block->request_queue)
1da177e4 2030 return;
138c014d 2031
8e09f215
SW
2032 spin_lock_irq(&block->request_queue_lock);
2033 while ((req = elv_next_request(block->request_queue))) {
1da177e4 2034 blkdev_dequeue_request(req);
4c4e2148 2035 dasd_end_request(req, -EIO);
1da177e4 2036 }
8e09f215 2037 spin_unlock_irq(&block->request_queue_lock);
1da177e4
LT
2038}
2039
57a7c0bc 2040static int dasd_open(struct block_device *bdev, fmode_t mode)
1da177e4 2041{
57a7c0bc 2042 struct dasd_block *block = bdev->bd_disk->private_data;
8e09f215 2043 struct dasd_device *base = block->base;
1da177e4
LT
2044 int rc;
2045
8e09f215
SW
2046 atomic_inc(&block->open_count);
2047 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
1da177e4
LT
2048 rc = -ENODEV;
2049 goto unlock;
2050 }
2051
8e09f215 2052 if (!try_module_get(base->discipline->owner)) {
1da177e4
LT
2053 rc = -EINVAL;
2054 goto unlock;
2055 }
2056
2057 if (dasd_probeonly) {
8e09f215 2058 DEV_MESSAGE(KERN_INFO, base, "%s",
1da177e4
LT
2059 "No access to device due to probeonly mode");
2060 rc = -EPERM;
2061 goto out;
2062 }
2063
8e09f215
SW
2064 if (base->state <= DASD_STATE_BASIC) {
2065 DBF_DEV_EVENT(DBF_ERR, base, " %s",
1da177e4
LT
2066 " Cannot open unrecognized device");
2067 rc = -ENODEV;
2068 goto out;
2069 }
2070
2071 return 0;
2072
2073out:
8e09f215 2074 module_put(base->discipline->owner);
1da177e4 2075unlock:
8e09f215 2076 atomic_dec(&block->open_count);
1da177e4
LT
2077 return rc;
2078}
2079
57a7c0bc 2080static int dasd_release(struct gendisk *disk, fmode_t mode)
1da177e4 2081{
8e09f215 2082 struct dasd_block *block = disk->private_data;
1da177e4 2083
8e09f215
SW
2084 atomic_dec(&block->open_count);
2085 module_put(block->base->discipline->owner);
1da177e4
LT
2086 return 0;
2087}
2088
a885c8c4
CH
2089/*
2090 * Return disk geometry.
2091 */
8e09f215 2092static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
a885c8c4 2093{
8e09f215
SW
2094 struct dasd_block *block;
2095 struct dasd_device *base;
a885c8c4 2096
8e09f215
SW
2097 block = bdev->bd_disk->private_data;
2098 base = block->base;
2099 if (!block)
a885c8c4
CH
2100 return -ENODEV;
2101
8e09f215
SW
2102 if (!base->discipline ||
2103 !base->discipline->fill_geometry)
a885c8c4
CH
2104 return -EINVAL;
2105
8e09f215
SW
2106 base->discipline->fill_geometry(block, geo);
2107 geo->start = get_start_sect(bdev) >> block->s2b_shift;
a885c8c4
CH
2108 return 0;
2109}
2110
1da177e4
LT
2111struct block_device_operations
2112dasd_device_operations = {
2113 .owner = THIS_MODULE,
57a7c0bc
AV
2114 .open = dasd_open,
2115 .release = dasd_release,
0000d031
HC
2116 .ioctl = dasd_ioctl,
2117 .compat_ioctl = dasd_ioctl,
a885c8c4 2118 .getgeo = dasd_getgeo,
1da177e4
LT
2119};
2120
8e09f215
SW
2121/*******************************************************************************
2122 * end of block device operations
2123 */
1da177e4
LT
2124
2125static void
2126dasd_exit(void)
2127{
2128#ifdef CONFIG_PROC_FS
2129 dasd_proc_exit();
2130#endif
20c64468 2131 dasd_eer_exit();
6bb0e010
HH
2132 if (dasd_page_cache != NULL) {
2133 kmem_cache_destroy(dasd_page_cache);
2134 dasd_page_cache = NULL;
2135 }
1da177e4
LT
2136 dasd_gendisk_exit();
2137 dasd_devmap_exit();
1da177e4
LT
2138 if (dasd_debug_area != NULL) {
2139 debug_unregister(dasd_debug_area);
2140 dasd_debug_area = NULL;
2141 }
2142}
2143
2144/*
2145 * SECTION: common functions for ccw_driver use
2146 */
2147
1c01b8a5
HH
2148/*
2149 * Initial attempt at a probe function. this can be simplified once
2150 * the other detection code is gone.
2151 */
8e09f215
SW
2152int dasd_generic_probe(struct ccw_device *cdev,
2153 struct dasd_discipline *discipline)
1da177e4
LT
2154{
2155 int ret;
2156
40545573
HH
2157 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
2158 if (ret) {
2159 printk(KERN_WARNING
2160 "dasd_generic_probe: could not set ccw-device options "
2a0217d5 2161 "for %s\n", dev_name(&cdev->dev));
40545573
HH
2162 return ret;
2163 }
1da177e4
LT
2164 ret = dasd_add_sysfs_files(cdev);
2165 if (ret) {
2166 printk(KERN_WARNING
2167 "dasd_generic_probe: could not add sysfs entries "
2a0217d5 2168 "for %s\n", dev_name(&cdev->dev));
40545573 2169 return ret;
1da177e4 2170 }
40545573 2171 cdev->handler = &dasd_int_handler;
1da177e4 2172
40545573
HH
2173 /*
2174 * Automatically online either all dasd devices (dasd_autodetect)
2175 * or all devices specified with dasd= parameters during
2176 * initial probe.
2177 */
2178 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2a0217d5 2179 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
40545573
HH
2180 ret = ccw_device_set_online(cdev);
2181 if (ret)
2182 printk(KERN_WARNING
de3e0da1
SH
2183 "dasd_generic_probe: could not initially "
2184 "online ccw-device %s; return code: %d\n",
2a0217d5 2185 dev_name(&cdev->dev), ret);
de3e0da1 2186 return 0;
1da177e4
LT
2187}
2188
1c01b8a5
HH
2189/*
2190 * This will one day be called from a global not_oper handler.
2191 * It is also used by driver_unregister during module unload.
2192 */
8e09f215 2193void dasd_generic_remove(struct ccw_device *cdev)
1da177e4
LT
2194{
2195 struct dasd_device *device;
8e09f215 2196 struct dasd_block *block;
1da177e4 2197
59afda78
HH
2198 cdev->handler = NULL;
2199
1da177e4
LT
2200 dasd_remove_sysfs_files(cdev);
2201 device = dasd_device_from_cdev(cdev);
2202 if (IS_ERR(device))
2203 return;
2204 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2205 /* Already doing offline processing */
2206 dasd_put_device(device);
2207 return;
2208 }
2209 /*
2210 * This device is removed unconditionally. Set offline
2211 * flag to prevent dasd_open from opening it while it is
2212 * no quite down yet.
2213 */
2214 dasd_set_target_state(device, DASD_STATE_NEW);
2215 /* dasd_delete_device destroys the device reference. */
8e09f215
SW
2216 block = device->block;
2217 device->block = NULL;
1da177e4 2218 dasd_delete_device(device);
8e09f215
SW
2219 /*
2220 * life cycle of block is bound to device, so delete it after
2221 * device was safely removed
2222 */
2223 if (block)
2224 dasd_free_block(block);
1da177e4
LT
2225}
2226
1c01b8a5
HH
2227/*
2228 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1da177e4 2229 * the device is detected for the first time and is supposed to be used
1c01b8a5
HH
2230 * or the user has started activation through sysfs.
2231 */
8e09f215
SW
2232int dasd_generic_set_online(struct ccw_device *cdev,
2233 struct dasd_discipline *base_discipline)
1da177e4 2234{
aa88861f 2235 struct dasd_discipline *discipline;
1da177e4 2236 struct dasd_device *device;
c6eb7b77 2237 int rc;
f24acd45 2238
40545573
HH
2239 /* first online clears initial online feature flag */
2240 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
1da177e4
LT
2241 device = dasd_create_device(cdev);
2242 if (IS_ERR(device))
2243 return PTR_ERR(device);
2244
aa88861f 2245 discipline = base_discipline;
c6eb7b77 2246 if (device->features & DASD_FEATURE_USEDIAG) {
1da177e4
LT
2247 if (!dasd_diag_discipline_pointer) {
2248 printk (KERN_WARNING
2249 "dasd_generic couldn't online device %s "
2250 "- discipline DIAG not available\n",
2a0217d5 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) {
2272 printk (KERN_WARNING
2273 "dasd_generic couldn't online device %s "
2274 "with discipline %s rc=%i\n",
2a0217d5 2275 dev_name(&cdev->dev), discipline->name, rc);
aa88861f
PO
2276 module_put(discipline->owner);
2277 module_put(base_discipline->owner);
1da177e4
LT
2278 dasd_delete_device(device);
2279 return rc;
2280 }
2281
2282 dasd_set_target_state(device, DASD_STATE_ONLINE);
2283 if (device->state <= DASD_STATE_KNOWN) {
2284 printk (KERN_WARNING
2285 "dasd_generic discipline not found for %s\n",
2a0217d5 2286 dev_name(&cdev->dev));
1da177e4
LT
2287 rc = -ENODEV;
2288 dasd_set_target_state(device, DASD_STATE_NEW);
8e09f215
SW
2289 if (device->block)
2290 dasd_free_block(device->block);
1da177e4
LT
2291 dasd_delete_device(device);
2292 } else
2293 pr_debug("dasd_generic device %s found\n",
2a0217d5 2294 dev_name(&cdev->dev));
1da177e4
LT
2295
2296 /* FIXME: we have to wait for the root device but we don't want
2297 * to wait for each single device but for all at once. */
2298 wait_event(dasd_init_waitq, _wait_for_device(device));
2299
2300 dasd_put_device(device);
2301
2302 return rc;
2303}
2304
8e09f215 2305int dasd_generic_set_offline(struct ccw_device *cdev)
1da177e4
LT
2306{
2307 struct dasd_device *device;
8e09f215 2308 struct dasd_block *block;
dafd87aa 2309 int max_count, open_count;
1da177e4
LT
2310
2311 device = dasd_device_from_cdev(cdev);
2312 if (IS_ERR(device))
2313 return PTR_ERR(device);
2314 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2315 /* Already doing offline processing */
2316 dasd_put_device(device);
2317 return 0;
2318 }
2319 /*
2320 * We must make sure that this device is currently not in use.
2321 * The open_count is increased for every opener, that includes
2322 * the blkdev_get in dasd_scan_partitions. We are only interested
2323 * in the other openers.
2324 */
8e09f215 2325 if (device->block) {
a806170e
HC
2326 max_count = device->block->bdev ? 0 : -1;
2327 open_count = atomic_read(&device->block->open_count);
8e09f215
SW
2328 if (open_count > max_count) {
2329 if (open_count > 0)
2330 printk(KERN_WARNING "Can't offline dasd "
2331 "device with open count = %i.\n",
2332 open_count);
2333 else
2334 printk(KERN_WARNING "%s",
2335 "Can't offline dasd device due "
2336 "to internal use\n");
2337 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2338 dasd_put_device(device);
2339 return -EBUSY;
2340 }
1da177e4
LT
2341 }
2342 dasd_set_target_state(device, DASD_STATE_NEW);
2343 /* dasd_delete_device destroys the device reference. */
8e09f215
SW
2344 block = device->block;
2345 device->block = NULL;
1da177e4 2346 dasd_delete_device(device);
8e09f215
SW
2347 /*
2348 * life cycle of block is bound to device, so delete it after
2349 * device was safely removed
2350 */
2351 if (block)
2352 dasd_free_block(block);
1da177e4
LT
2353 return 0;
2354}
2355
8e09f215 2356int dasd_generic_notify(struct ccw_device *cdev, int event)
1da177e4
LT
2357{
2358 struct dasd_device *device;
2359 struct dasd_ccw_req *cqr;
1da177e4
LT
2360 int ret;
2361
91c36919 2362 device = dasd_device_from_cdev_locked(cdev);
1da177e4
LT
2363 if (IS_ERR(device))
2364 return 0;
1da177e4
LT
2365 ret = 0;
2366 switch (event) {
2367 case CIO_GONE:
2368 case CIO_NO_PATH:
20c64468
SW
2369 /* First of all call extended error reporting. */
2370 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2371
1da177e4
LT
2372 if (device->state < DASD_STATE_BASIC)
2373 break;
2374 /* Device is active. We want to keep it. */
8e09f215
SW
2375 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2376 if (cqr->status == DASD_CQR_IN_IO) {
2377 cqr->status = DASD_CQR_QUEUED;
2378 cqr->retries++;
2379 }
2380 device->stopped |= DASD_STOPPED_DC_WAIT;
2381 dasd_device_clear_timer(device);
2382 dasd_schedule_device_bh(device);
1da177e4
LT
2383 ret = 1;
2384 break;
2385 case CIO_OPER:
2386 /* FIXME: add a sanity check. */
8e09f215
SW
2387 device->stopped &= ~DASD_STOPPED_DC_WAIT;
2388 dasd_schedule_device_bh(device);
2389 if (device->block)
2390 dasd_schedule_block_bh(device->block);
1da177e4
LT
2391 ret = 1;
2392 break;
2393 }
1da177e4
LT
2394 dasd_put_device(device);
2395 return ret;
2396}
2397
763968e2
HC
2398static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2399 void *rdc_buffer,
2400 int rdc_buffer_size,
2401 char *magic)
17283b56
CH
2402{
2403 struct dasd_ccw_req *cqr;
2404 struct ccw1 *ccw;
2405
2406 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2407
2408 if (IS_ERR(cqr)) {
2409 DEV_MESSAGE(KERN_WARNING, device, "%s",
2410 "Could not allocate RDC request");
2411 return cqr;
2412 }
2413
2414 ccw = cqr->cpaddr;
2415 ccw->cmd_code = CCW_CMD_RDC;
2416 ccw->cda = (__u32)(addr_t)rdc_buffer;
2417 ccw->count = rdc_buffer_size;
2418
8e09f215
SW
2419 cqr->startdev = device;
2420 cqr->memdev = device;
17283b56
CH
2421 cqr->expires = 10*HZ;
2422 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
2423 cqr->retries = 2;
2424 cqr->buildclk = get_clock();
2425 cqr->status = DASD_CQR_FILLED;
2426 return cqr;
2427}
2428
2429
2430int dasd_generic_read_dev_chars(struct dasd_device *device, char *magic,
2431 void **rdc_buffer, int rdc_buffer_size)
2432{
2433 int ret;
2434 struct dasd_ccw_req *cqr;
2435
2436 cqr = dasd_generic_build_rdc(device, *rdc_buffer, rdc_buffer_size,
2437 magic);
2438 if (IS_ERR(cqr))
2439 return PTR_ERR(cqr);
2440
2441 ret = dasd_sleep_on(cqr);
8e09f215 2442 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
2443 return ret;
2444}
aaff0f64 2445EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
20c64468 2446
f3eb5384
SW
2447/*
2448 * In command mode and transport mode we need to look for sense
2449 * data in different places. The sense data itself is allways
2450 * an array of 32 bytes, so we can unify the sense data access
2451 * for both modes.
2452 */
2453char *dasd_get_sense(struct irb *irb)
2454{
2455 struct tsb *tsb = NULL;
2456 char *sense = NULL;
2457
2458 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2459 if (irb->scsw.tm.tcw)
2460 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2461 irb->scsw.tm.tcw);
2462 if (tsb && tsb->length == 64 && tsb->flags)
2463 switch (tsb->flags & 0x07) {
2464 case 1: /* tsa_iostat */
2465 sense = tsb->tsa.iostat.sense;
2466 break;
2467 case 2: /* tsa_ddpc */
2468 sense = tsb->tsa.ddpc.sense;
2469 break;
2470 default:
2471 /* currently we don't use interrogate data */
2472 break;
2473 }
2474 } else if (irb->esw.esw0.erw.cons) {
2475 sense = irb->ecw;
2476 }
2477 return sense;
2478}
2479EXPORT_SYMBOL_GPL(dasd_get_sense);
2480
8e09f215 2481static int __init dasd_init(void)
1da177e4
LT
2482{
2483 int rc;
2484
2485 init_waitqueue_head(&dasd_init_waitq);
8f61701b 2486 init_waitqueue_head(&dasd_flush_wq);
c80ee724 2487 init_waitqueue_head(&generic_waitq);
1da177e4
LT
2488
2489 /* register 'common' DASD debug area, used for all DBF_XXX calls */
361f494d 2490 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
1da177e4
LT
2491 if (dasd_debug_area == NULL) {
2492 rc = -ENOMEM;
2493 goto failed;
2494 }
2495 debug_register_view(dasd_debug_area, &debug_sprintf_view);
b0035f12 2496 debug_set_level(dasd_debug_area, DBF_WARNING);
1da177e4
LT
2497
2498 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2499
2500 dasd_diag_discipline_pointer = NULL;
2501
1da177e4
LT
2502 rc = dasd_devmap_init();
2503 if (rc)
2504 goto failed;
2505 rc = dasd_gendisk_init();
2506 if (rc)
2507 goto failed;
2508 rc = dasd_parse();
2509 if (rc)
2510 goto failed;
20c64468
SW
2511 rc = dasd_eer_init();
2512 if (rc)
2513 goto failed;
1da177e4
LT
2514#ifdef CONFIG_PROC_FS
2515 rc = dasd_proc_init();
2516 if (rc)
2517 goto failed;
2518#endif
2519
2520 return 0;
2521failed:
2522 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2523 dasd_exit();
2524 return rc;
2525}
2526
2527module_init(dasd_init);
2528module_exit(dasd_exit);
2529
2530EXPORT_SYMBOL(dasd_debug_area);
2531EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2532
2533EXPORT_SYMBOL(dasd_add_request_head);
2534EXPORT_SYMBOL(dasd_add_request_tail);
2535EXPORT_SYMBOL(dasd_cancel_req);
8e09f215
SW
2536EXPORT_SYMBOL(dasd_device_clear_timer);
2537EXPORT_SYMBOL(dasd_block_clear_timer);
1da177e4
LT
2538EXPORT_SYMBOL(dasd_enable_device);
2539EXPORT_SYMBOL(dasd_int_handler);
2540EXPORT_SYMBOL(dasd_kfree_request);
2541EXPORT_SYMBOL(dasd_kick_device);
2542EXPORT_SYMBOL(dasd_kmalloc_request);
8e09f215
SW
2543EXPORT_SYMBOL(dasd_schedule_device_bh);
2544EXPORT_SYMBOL(dasd_schedule_block_bh);
1da177e4 2545EXPORT_SYMBOL(dasd_set_target_state);
8e09f215
SW
2546EXPORT_SYMBOL(dasd_device_set_timer);
2547EXPORT_SYMBOL(dasd_block_set_timer);
1da177e4
LT
2548EXPORT_SYMBOL(dasd_sfree_request);
2549EXPORT_SYMBOL(dasd_sleep_on);
2550EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2551EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2552EXPORT_SYMBOL(dasd_smalloc_request);
2553EXPORT_SYMBOL(dasd_start_IO);
2554EXPORT_SYMBOL(dasd_term_IO);
2555
2556EXPORT_SYMBOL_GPL(dasd_generic_probe);
2557EXPORT_SYMBOL_GPL(dasd_generic_remove);
2558EXPORT_SYMBOL_GPL(dasd_generic_notify);
2559EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2560EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
8e09f215
SW
2561EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2562EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2563EXPORT_SYMBOL_GPL(dasd_alloc_block);
2564EXPORT_SYMBOL_GPL(dasd_free_block);