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