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