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