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