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