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