]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/s390/block/dasd.c
s390/dasd: fix infinite loop during format
[mirror_ubuntu-artful-kernel.git] / drivers / s390 / block / dasd.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
d41dd122 7 * Copyright IBM Corp. 1999, 2009
1da177e4
LT
8 */
9
fc19f381
SH
10#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
1da177e4
LT
13#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
a885c8c4 19#include <linux/hdreg.h>
f3445a1a 20#include <linux/async.h>
9eb25122 21#include <linux/mutex.h>
4fa52aa7
SW
22#include <linux/debugfs.h>
23#include <linux/seq_file.h>
e4258d55 24#include <linux/vmalloc.h>
1da177e4
LT
25
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
f3eb5384 29#include <asm/itcw.h>
33b62a30 30#include <asm/diag.h>
1da177e4
LT
31
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
41/*
42 * SECTION: exported variables of dasd.c
43 */
44debug_info_t *dasd_debug_area;
7048a2b5 45EXPORT_SYMBOL(dasd_debug_area);
4fa52aa7 46static struct dentry *dasd_debugfs_root_entry;
1da177e4 47struct dasd_discipline *dasd_diag_discipline_pointer;
7048a2b5 48EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2b67fc46 49void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
1da177e4
LT
50
51MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
52MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
a53c8fab 53 " Copyright IBM Corp. 2000");
1da177e4 54MODULE_SUPPORTED_DEVICE("dasd");
1da177e4
LT
55MODULE_LICENSE("GPL");
56
57/*
58 * SECTION: prototypes for static functions of dasd.c
59 */
8e09f215
SW
60static int dasd_alloc_queue(struct dasd_block *);
61static void dasd_setup_queue(struct dasd_block *);
62static void dasd_free_queue(struct dasd_block *);
63static void dasd_flush_request_queue(struct dasd_block *);
64static int dasd_flush_block_queue(struct dasd_block *);
65static void dasd_device_tasklet(struct dasd_device *);
66static void dasd_block_tasklet(struct dasd_block *);
4927b3f7 67static void do_kick_device(struct work_struct *);
d41dd122 68static void do_restore_device(struct work_struct *);
501183f2 69static void do_reload_device(struct work_struct *);
8e09f215 70static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
48cae885
SW
71static void dasd_device_timeout(unsigned long);
72static void dasd_block_timeout(unsigned long);
eb6e199b 73static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
4fa52aa7
SW
74static void dasd_profile_init(struct dasd_profile *, struct dentry *);
75static void dasd_profile_exit(struct dasd_profile *);
1da177e4
LT
76
77/*
78 * SECTION: Operations on the device structure.
79 */
80static wait_queue_head_t dasd_init_waitq;
8f61701b 81static wait_queue_head_t dasd_flush_wq;
c80ee724 82static wait_queue_head_t generic_waitq;
4679e893 83static wait_queue_head_t shutdown_waitq;
1da177e4
LT
84
85/*
86 * Allocate memory for a new device structure.
87 */
8e09f215 88struct dasd_device *dasd_alloc_device(void)
1da177e4
LT
89{
90 struct dasd_device *device;
91
8e09f215
SW
92 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
93 if (!device)
1da177e4 94 return ERR_PTR(-ENOMEM);
1da177e4
LT
95
96 /* Get two pages for normal block device operations. */
97 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
8e09f215 98 if (!device->ccw_mem) {
1da177e4
LT
99 kfree(device);
100 return ERR_PTR(-ENOMEM);
101 }
102 /* Get one page for error recovery. */
103 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
8e09f215 104 if (!device->erp_mem) {
1da177e4
LT
105 free_pages((unsigned long) device->ccw_mem, 1);
106 kfree(device);
107 return ERR_PTR(-ENOMEM);
108 }
109
110 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
111 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
112 spin_lock_init(&device->mem_lock);
8e09f215 113 atomic_set(&device->tasklet_scheduled, 0);
138c014d 114 tasklet_init(&device->tasklet,
8e09f215 115 (void (*)(unsigned long)) dasd_device_tasklet,
1da177e4
LT
116 (unsigned long) device);
117 INIT_LIST_HEAD(&device->ccw_queue);
118 init_timer(&device->timer);
48cae885
SW
119 device->timer.function = dasd_device_timeout;
120 device->timer.data = (unsigned long) device;
4927b3f7 121 INIT_WORK(&device->kick_work, do_kick_device);
d41dd122 122 INIT_WORK(&device->restore_device, do_restore_device);
501183f2 123 INIT_WORK(&device->reload_device, do_reload_device);
1da177e4
LT
124 device->state = DASD_STATE_NEW;
125 device->target = DASD_STATE_NEW;
9eb25122 126 mutex_init(&device->state_mutex);
4fa52aa7 127 spin_lock_init(&device->profile.lock);
1da177e4
LT
128 return device;
129}
130
131/*
132 * Free memory of a device structure.
133 */
8e09f215 134void dasd_free_device(struct dasd_device *device)
1da177e4 135{
17fd682e 136 kfree(device->private);
1da177e4
LT
137 free_page((unsigned long) device->erp_mem);
138 free_pages((unsigned long) device->ccw_mem, 1);
139 kfree(device);
140}
141
8e09f215
SW
142/*
143 * Allocate memory for a new device structure.
144 */
145struct dasd_block *dasd_alloc_block(void)
146{
147 struct dasd_block *block;
148
149 block = kzalloc(sizeof(*block), GFP_ATOMIC);
150 if (!block)
151 return ERR_PTR(-ENOMEM);
152 /* open_count = 0 means device online but not in use */
153 atomic_set(&block->open_count, -1);
154
155 spin_lock_init(&block->request_queue_lock);
156 atomic_set(&block->tasklet_scheduled, 0);
157 tasklet_init(&block->tasklet,
158 (void (*)(unsigned long)) dasd_block_tasklet,
159 (unsigned long) block);
160 INIT_LIST_HEAD(&block->ccw_queue);
161 spin_lock_init(&block->queue_lock);
162 init_timer(&block->timer);
48cae885
SW
163 block->timer.function = dasd_block_timeout;
164 block->timer.data = (unsigned long) block;
4fa52aa7 165 spin_lock_init(&block->profile.lock);
8e09f215
SW
166
167 return block;
168}
7048a2b5 169EXPORT_SYMBOL_GPL(dasd_alloc_block);
8e09f215
SW
170
171/*
172 * Free memory of a device structure.
173 */
174void dasd_free_block(struct dasd_block *block)
175{
176 kfree(block);
177}
7048a2b5 178EXPORT_SYMBOL_GPL(dasd_free_block);
8e09f215 179
1da177e4
LT
180/*
181 * Make a new device known to the system.
182 */
8e09f215 183static int dasd_state_new_to_known(struct dasd_device *device)
1da177e4
LT
184{
185 int rc;
186
187 /*
138c014d 188 * As long as the device is not in state DASD_STATE_NEW we want to
1da177e4
LT
189 * keep the reference count > 0.
190 */
191 dasd_get_device(device);
192
8e09f215
SW
193 if (device->block) {
194 rc = dasd_alloc_queue(device->block);
195 if (rc) {
196 dasd_put_device(device);
197 return rc;
198 }
1da177e4 199 }
1da177e4
LT
200 device->state = DASD_STATE_KNOWN;
201 return 0;
202}
203
204/*
205 * Let the system forget about a device.
206 */
8e09f215 207static int dasd_state_known_to_new(struct dasd_device *device)
1da177e4 208{
20c64468
SW
209 /* Disable extended error reporting for this device. */
210 dasd_eer_disable(device);
1da177e4 211 /* Forget the discipline information. */
8e09f215
SW
212 if (device->discipline) {
213 if (device->discipline->uncheck_device)
214 device->discipline->uncheck_device(device);
aa88861f 215 module_put(device->discipline->owner);
8e09f215 216 }
1da177e4 217 device->discipline = NULL;
aa88861f
PO
218 if (device->base_discipline)
219 module_put(device->base_discipline->owner);
220 device->base_discipline = NULL;
1da177e4
LT
221 device->state = DASD_STATE_NEW;
222
8e09f215
SW
223 if (device->block)
224 dasd_free_queue(device->block);
1da177e4
LT
225
226 /* Give up reference we took in dasd_state_new_to_known. */
227 dasd_put_device(device);
8f61701b 228 return 0;
1da177e4
LT
229}
230
4fa52aa7
SW
231static struct dentry *dasd_debugfs_setup(const char *name,
232 struct dentry *base_dentry)
233{
234 struct dentry *pde;
235
236 if (!base_dentry)
237 return NULL;
238 pde = debugfs_create_dir(name, base_dentry);
239 if (!pde || IS_ERR(pde))
240 return NULL;
241 return pde;
242}
243
1da177e4
LT
244/*
245 * Request the irq line for the device.
246 */
8e09f215 247static int dasd_state_known_to_basic(struct dasd_device *device)
1da177e4 248{
4fa52aa7 249 struct dasd_block *block = device->block;
d42e1712 250 int rc = 0;
1da177e4
LT
251
252 /* Allocate and register gendisk structure. */
4fa52aa7
SW
253 if (block) {
254 rc = dasd_gendisk_alloc(block);
8e09f215
SW
255 if (rc)
256 return rc;
4fa52aa7
SW
257 block->debugfs_dentry =
258 dasd_debugfs_setup(block->gdp->disk_name,
259 dasd_debugfs_root_entry);
260 dasd_profile_init(&block->profile, block->debugfs_dentry);
261 if (dasd_global_profile_level == DASD_PROFILE_ON)
262 dasd_profile_on(&device->block->profile);
263 }
264 device->debugfs_dentry =
265 dasd_debugfs_setup(dev_name(&device->cdev->dev),
266 dasd_debugfs_root_entry);
267 dasd_profile_init(&device->profile, device->debugfs_dentry);
268
1da177e4 269 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
fc19f381 270 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
8e09f215 271 8 * sizeof(long));
1da177e4 272 debug_register_view(device->debug_area, &debug_sprintf_view);
b0035f12 273 debug_set_level(device->debug_area, DBF_WARNING);
1da177e4
LT
274 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
275
276 device->state = DASD_STATE_BASIC;
d42e1712
SH
277
278 return rc;
1da177e4
LT
279}
280
281/*
282 * Release the irq line for the device. Terminate any running i/o.
283 */
8e09f215 284static int dasd_state_basic_to_known(struct dasd_device *device)
1da177e4 285{
8f61701b 286 int rc;
d42e1712 287
daa991bf
SH
288 if (device->discipline->basic_to_known) {
289 rc = device->discipline->basic_to_known(device);
290 if (rc)
291 return rc;
292 }
293
8e09f215 294 if (device->block) {
4fa52aa7 295 dasd_profile_exit(&device->block->profile);
d7309aaa 296 debugfs_remove(device->block->debugfs_dentry);
8e09f215
SW
297 dasd_gendisk_free(device->block);
298 dasd_block_clear_timer(device->block);
299 }
300 rc = dasd_flush_device_queue(device);
8f61701b
HH
301 if (rc)
302 return rc;
8e09f215 303 dasd_device_clear_timer(device);
4fa52aa7 304 dasd_profile_exit(&device->profile);
d7309aaa 305 debugfs_remove(device->debugfs_dentry);
1da177e4
LT
306 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
307 if (device->debug_area != NULL) {
308 debug_unregister(device->debug_area);
309 device->debug_area = NULL;
310 }
311 device->state = DASD_STATE_KNOWN;
8f61701b 312 return 0;
1da177e4
LT
313}
314
315/*
316 * Do the initial analysis. The do_analysis function may return
317 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
318 * until the discipline decides to continue the startup sequence
319 * by calling the function dasd_change_state. The eckd disciplines
320 * uses this to start a ccw that detects the format. The completion
321 * interrupt for this detection ccw uses the kernel event daemon to
322 * trigger the call to dasd_change_state. All this is done in the
323 * discipline code, see dasd_eckd.c.
90f0094d
HH
324 * After the analysis ccw is done (do_analysis returned 0) the block
325 * device is setup.
326 * In case the analysis returns an error, the device setup is stopped
327 * (a fake disk was already added to allow formatting).
1da177e4 328 */
8e09f215 329static int dasd_state_basic_to_ready(struct dasd_device *device)
1da177e4
LT
330{
331 int rc;
8e09f215 332 struct dasd_block *block;
1da177e4
LT
333
334 rc = 0;
8e09f215 335 block = device->block;
90f0094d 336 /* make disk known with correct capacity */
8e09f215
SW
337 if (block) {
338 if (block->base->discipline->do_analysis != NULL)
339 rc = block->base->discipline->do_analysis(block);
340 if (rc) {
d42e1712 341 if (rc != -EAGAIN) {
8e09f215 342 device->state = DASD_STATE_UNFMT;
d42e1712
SH
343 goto out;
344 }
8e09f215
SW
345 return rc;
346 }
347 dasd_setup_queue(block);
348 set_capacity(block->gdp,
349 block->blocks << block->s2b_shift);
350 device->state = DASD_STATE_READY;
351 rc = dasd_scan_partitions(block);
d42e1712 352 if (rc) {
8e09f215 353 device->state = DASD_STATE_BASIC;
d42e1712
SH
354 return rc;
355 }
8e09f215
SW
356 } else {
357 device->state = DASD_STATE_READY;
358 }
d42e1712
SH
359out:
360 if (device->discipline->basic_to_ready)
361 rc = device->discipline->basic_to_ready(device);
90f0094d 362 return rc;
1da177e4
LT
363}
364
d07dc5d8
SH
365static inline
366int _wait_for_empty_queues(struct dasd_device *device)
367{
368 if (device->block)
369 return list_empty(&device->ccw_queue) &&
370 list_empty(&device->block->ccw_queue);
371 else
372 return list_empty(&device->ccw_queue);
373}
374
1da177e4
LT
375/*
376 * Remove device from block device layer. Destroy dirty buffers.
377 * Forget format information. Check if the target level is basic
378 * and if it is create fake disk for formatting.
379 */
8e09f215 380static int dasd_state_ready_to_basic(struct dasd_device *device)
1da177e4 381{
8f61701b
HH
382 int rc;
383
1da177e4 384 device->state = DASD_STATE_BASIC;
8e09f215
SW
385 if (device->block) {
386 struct dasd_block *block = device->block;
387 rc = dasd_flush_block_queue(block);
388 if (rc) {
389 device->state = DASD_STATE_READY;
390 return rc;
391 }
8e09f215 392 dasd_flush_request_queue(block);
b695adfa 393 dasd_destroy_partitions(block);
8e09f215
SW
394 block->blocks = 0;
395 block->bp_block = 0;
396 block->s2b_shift = 0;
397 }
8f61701b 398 return 0;
1da177e4
LT
399}
400
90f0094d
HH
401/*
402 * Back to basic.
403 */
8e09f215 404static int dasd_state_unfmt_to_basic(struct dasd_device *device)
90f0094d
HH
405{
406 device->state = DASD_STATE_BASIC;
8f61701b 407 return 0;
90f0094d
HH
408}
409
1da177e4
LT
410/*
411 * Make the device online and schedule the bottom half to start
412 * the requeueing of requests from the linux request queue to the
413 * ccw queue.
414 */
8f61701b 415static int
1da177e4
LT
416dasd_state_ready_to_online(struct dasd_device * device)
417{
1301809b
SW
418 struct gendisk *disk;
419 struct disk_part_iter piter;
420 struct hd_struct *part;
8e09f215 421
1da177e4 422 device->state = DASD_STATE_ONLINE;
1301809b 423 if (device->block) {
8e09f215 424 dasd_schedule_block_bh(device->block);
e4dbb0f2
SH
425 if ((device->features & DASD_FEATURE_USERAW)) {
426 disk = device->block->gdp;
427 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
428 return 0;
429 }
1301809b
SW
430 disk = device->block->bdev->bd_disk;
431 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
432 while ((part = disk_part_iter_next(&piter)))
433 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
434 disk_part_iter_exit(&piter);
435 }
1da177e4
LT
436 return 0;
437}
438
439/*
440 * Stop the requeueing of requests again.
441 */
8e09f215 442static int dasd_state_online_to_ready(struct dasd_device *device)
1da177e4 443{
8e09f215 444 int rc;
1301809b
SW
445 struct gendisk *disk;
446 struct disk_part_iter piter;
447 struct hd_struct *part;
8e09f215
SW
448
449 if (device->discipline->online_to_ready) {
450 rc = device->discipline->online_to_ready(device);
451 if (rc)
452 return rc;
453 }
d42e1712 454
1da177e4 455 device->state = DASD_STATE_READY;
e4dbb0f2 456 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
1301809b
SW
457 disk = device->block->bdev->bd_disk;
458 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
459 while ((part = disk_part_iter_next(&piter)))
460 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
461 disk_part_iter_exit(&piter);
462 }
8f61701b 463 return 0;
1da177e4
LT
464}
465
466/*
467 * Device startup state changes.
468 */
8e09f215 469static int dasd_increase_state(struct dasd_device *device)
1da177e4
LT
470{
471 int rc;
472
473 rc = 0;
474 if (device->state == DASD_STATE_NEW &&
475 device->target >= DASD_STATE_KNOWN)
476 rc = dasd_state_new_to_known(device);
477
478 if (!rc &&
479 device->state == DASD_STATE_KNOWN &&
480 device->target >= DASD_STATE_BASIC)
481 rc = dasd_state_known_to_basic(device);
482
483 if (!rc &&
484 device->state == DASD_STATE_BASIC &&
485 device->target >= DASD_STATE_READY)
486 rc = dasd_state_basic_to_ready(device);
487
39ccf95e
HH
488 if (!rc &&
489 device->state == DASD_STATE_UNFMT &&
490 device->target > DASD_STATE_UNFMT)
491 rc = -EPERM;
492
1da177e4
LT
493 if (!rc &&
494 device->state == DASD_STATE_READY &&
495 device->target >= DASD_STATE_ONLINE)
496 rc = dasd_state_ready_to_online(device);
497
498 return rc;
499}
500
501/*
502 * Device shutdown state changes.
503 */
8e09f215 504static int dasd_decrease_state(struct dasd_device *device)
1da177e4 505{
8f61701b
HH
506 int rc;
507
508 rc = 0;
1da177e4
LT
509 if (device->state == DASD_STATE_ONLINE &&
510 device->target <= DASD_STATE_READY)
8f61701b 511 rc = dasd_state_online_to_ready(device);
138c014d 512
8f61701b
HH
513 if (!rc &&
514 device->state == DASD_STATE_READY &&
1da177e4 515 device->target <= DASD_STATE_BASIC)
8f61701b 516 rc = dasd_state_ready_to_basic(device);
90f0094d 517
8f61701b
HH
518 if (!rc &&
519 device->state == DASD_STATE_UNFMT &&
90f0094d 520 device->target <= DASD_STATE_BASIC)
8f61701b 521 rc = dasd_state_unfmt_to_basic(device);
90f0094d 522
8f61701b
HH
523 if (!rc &&
524 device->state == DASD_STATE_BASIC &&
1da177e4 525 device->target <= DASD_STATE_KNOWN)
8f61701b 526 rc = dasd_state_basic_to_known(device);
138c014d 527
8f61701b
HH
528 if (!rc &&
529 device->state == DASD_STATE_KNOWN &&
1da177e4 530 device->target <= DASD_STATE_NEW)
8f61701b 531 rc = dasd_state_known_to_new(device);
1da177e4 532
8f61701b 533 return rc;
1da177e4
LT
534}
535
536/*
537 * This is the main startup/shutdown routine.
538 */
8e09f215 539static void dasd_change_state(struct dasd_device *device)
1da177e4 540{
181d9522 541 int rc;
1da177e4
LT
542
543 if (device->state == device->target)
544 /* Already where we want to go today... */
545 return;
546 if (device->state < device->target)
547 rc = dasd_increase_state(device);
548 else
549 rc = dasd_decrease_state(device);
181d9522
SO
550 if (rc == -EAGAIN)
551 return;
552 if (rc)
553 device->target = device->state;
1da177e4 554
4dfd5c45
HH
555 /* let user-space know that the device status changed */
556 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
1f08be80
SO
557
558 if (device->state == device->target)
559 wake_up(&dasd_init_waitq);
1da177e4
LT
560}
561
562/*
563 * Kick starter for devices that did not complete the startup/shutdown
564 * procedure or were sleeping because of a pending state.
565 * dasd_kick_device will schedule a call do do_kick_device to the kernel
566 * event daemon.
567 */
8e09f215 568static void do_kick_device(struct work_struct *work)
1da177e4 569{
4927b3f7 570 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
9eb25122 571 mutex_lock(&device->state_mutex);
1da177e4 572 dasd_change_state(device);
9eb25122 573 mutex_unlock(&device->state_mutex);
8e09f215 574 dasd_schedule_device_bh(device);
1da177e4
LT
575 dasd_put_device(device);
576}
577
8e09f215 578void dasd_kick_device(struct dasd_device *device)
1da177e4
LT
579{
580 dasd_get_device(device);
581 /* queue call to dasd_kick_device to the kernel event daemon. */
582 schedule_work(&device->kick_work);
583}
7048a2b5 584EXPORT_SYMBOL(dasd_kick_device);
1da177e4 585
501183f2
SH
586/*
587 * dasd_reload_device will schedule a call do do_reload_device to the kernel
588 * event daemon.
589 */
590static void do_reload_device(struct work_struct *work)
591{
592 struct dasd_device *device = container_of(work, struct dasd_device,
593 reload_device);
594 device->discipline->reload(device);
595 dasd_put_device(device);
596}
597
598void dasd_reload_device(struct dasd_device *device)
599{
600 dasd_get_device(device);
601 /* queue call to dasd_reload_device to the kernel event daemon. */
602 schedule_work(&device->reload_device);
603}
604EXPORT_SYMBOL(dasd_reload_device);
605
d41dd122
SH
606/*
607 * dasd_restore_device will schedule a call do do_restore_device to the kernel
608 * event daemon.
609 */
610static void do_restore_device(struct work_struct *work)
611{
612 struct dasd_device *device = container_of(work, struct dasd_device,
613 restore_device);
614 device->cdev->drv->restore(device->cdev);
615 dasd_put_device(device);
616}
617
618void dasd_restore_device(struct dasd_device *device)
619{
620 dasd_get_device(device);
621 /* queue call to dasd_restore_device to the kernel event daemon. */
622 schedule_work(&device->restore_device);
623}
624
1da177e4
LT
625/*
626 * Set the target state for a device and starts the state change.
627 */
8e09f215 628void dasd_set_target_state(struct dasd_device *device, int target)
1da177e4 629{
f3445a1a 630 dasd_get_device(device);
9eb25122 631 mutex_lock(&device->state_mutex);
1da177e4
LT
632 /* If we are in probeonly mode stop at DASD_STATE_READY. */
633 if (dasd_probeonly && target > DASD_STATE_READY)
634 target = DASD_STATE_READY;
635 if (device->target != target) {
9eb25122 636 if (device->state == target)
1da177e4
LT
637 wake_up(&dasd_init_waitq);
638 device->target = target;
639 }
640 if (device->state != device->target)
641 dasd_change_state(device);
9eb25122
SH
642 mutex_unlock(&device->state_mutex);
643 dasd_put_device(device);
1da177e4 644}
7048a2b5 645EXPORT_SYMBOL(dasd_set_target_state);
1da177e4
LT
646
647/*
648 * Enable devices with device numbers in [from..to].
649 */
8e09f215 650static inline int _wait_for_device(struct dasd_device *device)
1da177e4
LT
651{
652 return (device->state == device->target);
653}
654
8e09f215 655void dasd_enable_device(struct dasd_device *device)
1da177e4
LT
656{
657 dasd_set_target_state(device, DASD_STATE_ONLINE);
658 if (device->state <= DASD_STATE_KNOWN)
659 /* No discipline for device found. */
660 dasd_set_target_state(device, DASD_STATE_NEW);
661 /* Now wait for the devices to come up. */
662 wait_event(dasd_init_waitq, _wait_for_device(device));
25e2cf1c
SH
663
664 dasd_reload_device(device);
665 if (device->discipline->kick_validate)
666 device->discipline->kick_validate(device);
1da177e4 667}
7048a2b5 668EXPORT_SYMBOL(dasd_enable_device);
1da177e4
LT
669
670/*
671 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
672 */
1da177e4 673
4fa52aa7 674unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
1da177e4 675
4fa52aa7
SW
676#ifdef CONFIG_DASD_PROFILE
677struct dasd_profile_info dasd_global_profile_data;
678static struct dentry *dasd_global_profile_dentry;
679static struct dentry *dasd_debugfs_global_entry;
1da177e4
LT
680
681/*
682 * Add profiling information for cqr before execution.
683 */
8e09f215
SW
684static void dasd_profile_start(struct dasd_block *block,
685 struct dasd_ccw_req *cqr,
686 struct request *req)
1da177e4
LT
687{
688 struct list_head *l;
689 unsigned int counter;
4fa52aa7 690 struct dasd_device *device;
1da177e4
LT
691
692 /* count the length of the chanq for statistics */
693 counter = 0;
4fa52aa7
SW
694 if (dasd_global_profile_level || block->profile.data)
695 list_for_each(l, &block->ccw_queue)
696 if (++counter >= 31)
697 break;
698
699 if (dasd_global_profile_level) {
700 dasd_global_profile_data.dasd_io_nr_req[counter]++;
701 if (rq_data_dir(req) == READ)
702 dasd_global_profile_data.dasd_read_nr_req[counter]++;
703 }
704
705 spin_lock(&block->profile.lock);
c81a90c8 706 if (block->profile.data) {
4fa52aa7
SW
707 block->profile.data->dasd_io_nr_req[counter]++;
708 if (rq_data_dir(req) == READ)
709 block->profile.data->dasd_read_nr_req[counter]++;
c81a90c8 710 }
4fa52aa7
SW
711 spin_unlock(&block->profile.lock);
712
713 /*
714 * We count the request for the start device, even though it may run on
715 * some other device due to error recovery. This way we make sure that
716 * we count each request only once.
717 */
718 device = cqr->startdev;
719 if (device->profile.data) {
720 counter = 1; /* request is not yet queued on the start device */
721 list_for_each(l, &device->ccw_queue)
722 if (++counter >= 31)
723 break;
724 }
725 spin_lock(&device->profile.lock);
726 if (device->profile.data) {
727 device->profile.data->dasd_io_nr_req[counter]++;
728 if (rq_data_dir(req) == READ)
729 device->profile.data->dasd_read_nr_req[counter]++;
730 }
731 spin_unlock(&device->profile.lock);
1da177e4
LT
732}
733
734/*
735 * Add profiling information for cqr after execution.
736 */
4fa52aa7
SW
737
738#define dasd_profile_counter(value, index) \
739{ \
740 for (index = 0; index < 31 && value >> (2+index); index++) \
741 ; \
742}
743
744static void dasd_profile_end_add_data(struct dasd_profile_info *data,
745 int is_alias,
746 int is_tpm,
747 int is_read,
748 long sectors,
749 int sectors_ind,
750 int tottime_ind,
751 int tottimeps_ind,
752 int strtime_ind,
753 int irqtime_ind,
754 int irqtimeps_ind,
755 int endtime_ind)
756{
757 /* in case of an overflow, reset the whole profile */
758 if (data->dasd_io_reqs == UINT_MAX) {
759 memset(data, 0, sizeof(*data));
760 getnstimeofday(&data->starttod);
761 }
762 data->dasd_io_reqs++;
763 data->dasd_io_sects += sectors;
764 if (is_alias)
765 data->dasd_io_alias++;
766 if (is_tpm)
767 data->dasd_io_tpm++;
768
769 data->dasd_io_secs[sectors_ind]++;
770 data->dasd_io_times[tottime_ind]++;
771 data->dasd_io_timps[tottimeps_ind]++;
772 data->dasd_io_time1[strtime_ind]++;
773 data->dasd_io_time2[irqtime_ind]++;
774 data->dasd_io_time2ps[irqtimeps_ind]++;
775 data->dasd_io_time3[endtime_ind]++;
776
777 if (is_read) {
778 data->dasd_read_reqs++;
779 data->dasd_read_sects += sectors;
780 if (is_alias)
781 data->dasd_read_alias++;
782 if (is_tpm)
783 data->dasd_read_tpm++;
784 data->dasd_read_secs[sectors_ind]++;
785 data->dasd_read_times[tottime_ind]++;
786 data->dasd_read_time1[strtime_ind]++;
787 data->dasd_read_time2[irqtime_ind]++;
788 data->dasd_read_time3[endtime_ind]++;
789 }
790}
791
8e09f215
SW
792static void dasd_profile_end(struct dasd_block *block,
793 struct dasd_ccw_req *cqr,
794 struct request *req)
1da177e4
LT
795{
796 long strtime, irqtime, endtime, tottime; /* in microseconds */
797 long tottimeps, sectors;
4fa52aa7
SW
798 struct dasd_device *device;
799 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
800 int irqtime_ind, irqtimeps_ind, endtime_ind;
1da177e4 801
4fa52aa7
SW
802 device = cqr->startdev;
803 if (!(dasd_global_profile_level ||
804 block->profile.data ||
805 device->profile.data))
1da177e4
LT
806 return;
807
83096ebf 808 sectors = blk_rq_sectors(req);
1da177e4
LT
809 if (!cqr->buildclk || !cqr->startclk ||
810 !cqr->stopclk || !cqr->endclk ||
811 !sectors)
812 return;
813
814 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
815 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
816 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
817 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
818 tottimeps = tottime / sectors;
819
4fa52aa7
SW
820 dasd_profile_counter(sectors, sectors_ind);
821 dasd_profile_counter(tottime, tottime_ind);
822 dasd_profile_counter(tottimeps, tottimeps_ind);
823 dasd_profile_counter(strtime, strtime_ind);
824 dasd_profile_counter(irqtime, irqtime_ind);
825 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
826 dasd_profile_counter(endtime, endtime_ind);
827
828 if (dasd_global_profile_level) {
829 dasd_profile_end_add_data(&dasd_global_profile_data,
830 cqr->startdev != block->base,
831 cqr->cpmode == 1,
832 rq_data_dir(req) == READ,
833 sectors, sectors_ind, tottime_ind,
834 tottimeps_ind, strtime_ind,
835 irqtime_ind, irqtimeps_ind,
836 endtime_ind);
837 }
838
839 spin_lock(&block->profile.lock);
840 if (block->profile.data)
841 dasd_profile_end_add_data(block->profile.data,
842 cqr->startdev != block->base,
843 cqr->cpmode == 1,
844 rq_data_dir(req) == READ,
845 sectors, sectors_ind, tottime_ind,
846 tottimeps_ind, strtime_ind,
847 irqtime_ind, irqtimeps_ind,
848 endtime_ind);
849 spin_unlock(&block->profile.lock);
850
851 spin_lock(&device->profile.lock);
852 if (device->profile.data)
853 dasd_profile_end_add_data(device->profile.data,
854 cqr->startdev != block->base,
855 cqr->cpmode == 1,
856 rq_data_dir(req) == READ,
857 sectors, sectors_ind, tottime_ind,
858 tottimeps_ind, strtime_ind,
859 irqtime_ind, irqtimeps_ind,
860 endtime_ind);
861 spin_unlock(&device->profile.lock);
862}
863
864void dasd_profile_reset(struct dasd_profile *profile)
865{
866 struct dasd_profile_info *data;
867
868 spin_lock_bh(&profile->lock);
869 data = profile->data;
870 if (!data) {
871 spin_unlock_bh(&profile->lock);
872 return;
873 }
874 memset(data, 0, sizeof(*data));
875 getnstimeofday(&data->starttod);
876 spin_unlock_bh(&profile->lock);
877}
878
879void dasd_global_profile_reset(void)
880{
881 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
882 getnstimeofday(&dasd_global_profile_data.starttod);
883}
884
885int dasd_profile_on(struct dasd_profile *profile)
886{
887 struct dasd_profile_info *data;
888
889 data = kzalloc(sizeof(*data), GFP_KERNEL);
890 if (!data)
891 return -ENOMEM;
892 spin_lock_bh(&profile->lock);
893 if (profile->data) {
894 spin_unlock_bh(&profile->lock);
895 kfree(data);
896 return 0;
897 }
898 getnstimeofday(&data->starttod);
899 profile->data = data;
900 spin_unlock_bh(&profile->lock);
901 return 0;
902}
903
904void dasd_profile_off(struct dasd_profile *profile)
905{
906 spin_lock_bh(&profile->lock);
907 kfree(profile->data);
908 profile->data = NULL;
909 spin_unlock_bh(&profile->lock);
910}
911
912char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
913{
914 char *buffer;
915
e4258d55 916 buffer = vmalloc(user_len + 1);
4fa52aa7
SW
917 if (buffer == NULL)
918 return ERR_PTR(-ENOMEM);
919 if (copy_from_user(buffer, user_buf, user_len) != 0) {
e4258d55 920 vfree(buffer);
4fa52aa7
SW
921 return ERR_PTR(-EFAULT);
922 }
923 /* got the string, now strip linefeed. */
924 if (buffer[user_len - 1] == '\n')
925 buffer[user_len - 1] = 0;
926 else
927 buffer[user_len] = 0;
928 return buffer;
1da177e4 929}
4fa52aa7
SW
930
931static ssize_t dasd_stats_write(struct file *file,
932 const char __user *user_buf,
933 size_t user_len, loff_t *pos)
934{
935 char *buffer, *str;
936 int rc;
937 struct seq_file *m = (struct seq_file *)file->private_data;
938 struct dasd_profile *prof = m->private;
939
940 if (user_len > 65536)
941 user_len = 65536;
942 buffer = dasd_get_user_string(user_buf, user_len);
943 if (IS_ERR(buffer))
944 return PTR_ERR(buffer);
945
946 str = skip_spaces(buffer);
947 rc = user_len;
948 if (strncmp(str, "reset", 5) == 0) {
949 dasd_profile_reset(prof);
950 } else if (strncmp(str, "on", 2) == 0) {
951 rc = dasd_profile_on(prof);
952 if (!rc)
953 rc = user_len;
954 } else if (strncmp(str, "off", 3) == 0) {
955 dasd_profile_off(prof);
956 } else
957 rc = -EINVAL;
e4258d55 958 vfree(buffer);
4fa52aa7
SW
959 return rc;
960}
961
962static void dasd_stats_array(struct seq_file *m, unsigned int *array)
963{
964 int i;
965
966 for (i = 0; i < 32; i++)
967 seq_printf(m, "%u ", array[i]);
968 seq_putc(m, '\n');
969}
970
971static void dasd_stats_seq_print(struct seq_file *m,
972 struct dasd_profile_info *data)
973{
974 seq_printf(m, "start_time %ld.%09ld\n",
975 data->starttod.tv_sec, data->starttod.tv_nsec);
976 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
977 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
978 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
979 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
c794caf9 980 seq_puts(m, "histogram_sectors ");
4fa52aa7 981 dasd_stats_array(m, data->dasd_io_secs);
c794caf9 982 seq_puts(m, "histogram_io_times ");
4fa52aa7 983 dasd_stats_array(m, data->dasd_io_times);
c794caf9 984 seq_puts(m, "histogram_io_times_weighted ");
4fa52aa7 985 dasd_stats_array(m, data->dasd_io_timps);
c794caf9 986 seq_puts(m, "histogram_time_build_to_ssch ");
4fa52aa7 987 dasd_stats_array(m, data->dasd_io_time1);
c794caf9 988 seq_puts(m, "histogram_time_ssch_to_irq ");
4fa52aa7 989 dasd_stats_array(m, data->dasd_io_time2);
c794caf9 990 seq_puts(m, "histogram_time_ssch_to_irq_weighted ");
4fa52aa7 991 dasd_stats_array(m, data->dasd_io_time2ps);
c794caf9 992 seq_puts(m, "histogram_time_irq_to_end ");
4fa52aa7 993 dasd_stats_array(m, data->dasd_io_time3);
c794caf9 994 seq_puts(m, "histogram_ccw_queue_length ");
4fa52aa7
SW
995 dasd_stats_array(m, data->dasd_io_nr_req);
996 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
997 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
998 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
999 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
c794caf9 1000 seq_puts(m, "histogram_read_sectors ");
4fa52aa7 1001 dasd_stats_array(m, data->dasd_read_secs);
c794caf9 1002 seq_puts(m, "histogram_read_times ");
4fa52aa7 1003 dasd_stats_array(m, data->dasd_read_times);
c794caf9 1004 seq_puts(m, "histogram_read_time_build_to_ssch ");
4fa52aa7 1005 dasd_stats_array(m, data->dasd_read_time1);
c794caf9 1006 seq_puts(m, "histogram_read_time_ssch_to_irq ");
4fa52aa7 1007 dasd_stats_array(m, data->dasd_read_time2);
c794caf9 1008 seq_puts(m, "histogram_read_time_irq_to_end ");
4fa52aa7 1009 dasd_stats_array(m, data->dasd_read_time3);
c794caf9 1010 seq_puts(m, "histogram_read_ccw_queue_length ");
4fa52aa7
SW
1011 dasd_stats_array(m, data->dasd_read_nr_req);
1012}
1013
1014static int dasd_stats_show(struct seq_file *m, void *v)
1015{
1016 struct dasd_profile *profile;
1017 struct dasd_profile_info *data;
1018
1019 profile = m->private;
1020 spin_lock_bh(&profile->lock);
1021 data = profile->data;
1022 if (!data) {
1023 spin_unlock_bh(&profile->lock);
c794caf9 1024 seq_puts(m, "disabled\n");
4fa52aa7
SW
1025 return 0;
1026 }
1027 dasd_stats_seq_print(m, data);
1028 spin_unlock_bh(&profile->lock);
1029 return 0;
1030}
1031
1032static int dasd_stats_open(struct inode *inode, struct file *file)
1033{
1034 struct dasd_profile *profile = inode->i_private;
1035 return single_open(file, dasd_stats_show, profile);
1036}
1037
1038static const struct file_operations dasd_stats_raw_fops = {
1039 .owner = THIS_MODULE,
1040 .open = dasd_stats_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1044 .write = dasd_stats_write,
1045};
1046
1047static ssize_t dasd_stats_global_write(struct file *file,
1048 const char __user *user_buf,
1049 size_t user_len, loff_t *pos)
1050{
1051 char *buffer, *str;
1052 ssize_t rc;
1053
1054 if (user_len > 65536)
1055 user_len = 65536;
1056 buffer = dasd_get_user_string(user_buf, user_len);
1057 if (IS_ERR(buffer))
1058 return PTR_ERR(buffer);
1059 str = skip_spaces(buffer);
1060 rc = user_len;
1061 if (strncmp(str, "reset", 5) == 0) {
1062 dasd_global_profile_reset();
1063 } else if (strncmp(str, "on", 2) == 0) {
1064 dasd_global_profile_reset();
1065 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1066 } else if (strncmp(str, "off", 3) == 0) {
1067 dasd_global_profile_level = DASD_PROFILE_OFF;
1068 } else
1069 rc = -EINVAL;
e4258d55 1070 vfree(buffer);
4fa52aa7
SW
1071 return rc;
1072}
1073
1074static int dasd_stats_global_show(struct seq_file *m, void *v)
1075{
1076 if (!dasd_global_profile_level) {
c794caf9 1077 seq_puts(m, "disabled\n");
4fa52aa7
SW
1078 return 0;
1079 }
1080 dasd_stats_seq_print(m, &dasd_global_profile_data);
1081 return 0;
1082}
1083
1084static int dasd_stats_global_open(struct inode *inode, struct file *file)
1085{
1086 return single_open(file, dasd_stats_global_show, NULL);
1087}
1088
1089static const struct file_operations dasd_stats_global_fops = {
1090 .owner = THIS_MODULE,
1091 .open = dasd_stats_global_open,
1092 .read = seq_read,
1093 .llseek = seq_lseek,
1094 .release = single_release,
1095 .write = dasd_stats_global_write,
1096};
1097
1098static void dasd_profile_init(struct dasd_profile *profile,
1099 struct dentry *base_dentry)
1100{
f4ae40a6 1101 umode_t mode;
4fa52aa7
SW
1102 struct dentry *pde;
1103
1104 if (!base_dentry)
1105 return;
1106 profile->dentry = NULL;
1107 profile->data = NULL;
1108 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1109 pde = debugfs_create_file("statistics", mode, base_dentry,
1110 profile, &dasd_stats_raw_fops);
1111 if (pde && !IS_ERR(pde))
1112 profile->dentry = pde;
1113 return;
1114}
1115
1116static void dasd_profile_exit(struct dasd_profile *profile)
1117{
1118 dasd_profile_off(profile);
d7309aaa
FF
1119 debugfs_remove(profile->dentry);
1120 profile->dentry = NULL;
4fa52aa7
SW
1121}
1122
1123static void dasd_statistics_removeroot(void)
1124{
1125 dasd_global_profile_level = DASD_PROFILE_OFF;
d7309aaa
FF
1126 debugfs_remove(dasd_global_profile_dentry);
1127 dasd_global_profile_dentry = NULL;
1128 debugfs_remove(dasd_debugfs_global_entry);
1129 debugfs_remove(dasd_debugfs_root_entry);
4fa52aa7
SW
1130}
1131
1132static void dasd_statistics_createroot(void)
1133{
f4ae40a6 1134 umode_t mode;
4fa52aa7
SW
1135 struct dentry *pde;
1136
1137 dasd_debugfs_root_entry = NULL;
1138 dasd_debugfs_global_entry = NULL;
1139 dasd_global_profile_dentry = NULL;
1140 pde = debugfs_create_dir("dasd", NULL);
1141 if (!pde || IS_ERR(pde))
1142 goto error;
1143 dasd_debugfs_root_entry = pde;
1144 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1145 if (!pde || IS_ERR(pde))
1146 goto error;
1147 dasd_debugfs_global_entry = pde;
1148
1149 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1150 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1151 NULL, &dasd_stats_global_fops);
1152 if (!pde || IS_ERR(pde))
1153 goto error;
1154 dasd_global_profile_dentry = pde;
1155 return;
1156
1157error:
1158 DBF_EVENT(DBF_ERR, "%s",
1159 "Creation of the dasd debugfs interface failed");
1160 dasd_statistics_removeroot();
1161 return;
1162}
1163
1da177e4 1164#else
8e09f215
SW
1165#define dasd_profile_start(block, cqr, req) do {} while (0)
1166#define dasd_profile_end(block, cqr, req) do {} while (0)
4fa52aa7
SW
1167
1168static void dasd_statistics_createroot(void)
1169{
1170 return;
1171}
1172
1173static void dasd_statistics_removeroot(void)
1174{
1175 return;
1176}
1177
1178int dasd_stats_generic_show(struct seq_file *m, void *v)
1179{
c794caf9 1180 seq_puts(m, "Statistics are not activated in this kernel\n");
4fa52aa7
SW
1181 return 0;
1182}
1183
1184static void dasd_profile_init(struct dasd_profile *profile,
1185 struct dentry *base_dentry)
1186{
1187 return;
1188}
1189
1190static void dasd_profile_exit(struct dasd_profile *profile)
1191{
1192 return;
1193}
1194
1195int dasd_profile_on(struct dasd_profile *profile)
1196{
1197 return 0;
1198}
1199
1da177e4
LT
1200#endif /* CONFIG_DASD_PROFILE */
1201
1202/*
1203 * Allocate memory for a channel program with 'cplength' channel
1204 * command words and 'datasize' additional space. There are two
1205 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1206 * memory and 2) dasd_smalloc_request uses the static ccw memory
1207 * that gets allocated for each device.
1208 */
68b781fe 1209struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
8e09f215
SW
1210 int datasize,
1211 struct dasd_device *device)
1da177e4
LT
1212{
1213 struct dasd_ccw_req *cqr;
1214
1215 /* Sanity checks */
68b781fe 1216 BUG_ON(datasize > PAGE_SIZE ||
7ac1e877 1217 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4 1218
88abaab4 1219 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1da177e4
LT
1220 if (cqr == NULL)
1221 return ERR_PTR(-ENOMEM);
1da177e4
LT
1222 cqr->cpaddr = NULL;
1223 if (cplength > 0) {
88abaab4 1224 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1da177e4
LT
1225 GFP_ATOMIC | GFP_DMA);
1226 if (cqr->cpaddr == NULL) {
1227 kfree(cqr);
1228 return ERR_PTR(-ENOMEM);
1229 }
1da177e4
LT
1230 }
1231 cqr->data = NULL;
1232 if (datasize > 0) {
88abaab4 1233 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1da177e4 1234 if (cqr->data == NULL) {
17fd682e 1235 kfree(cqr->cpaddr);
1da177e4
LT
1236 kfree(cqr);
1237 return ERR_PTR(-ENOMEM);
1238 }
1da177e4 1239 }
68b781fe 1240 cqr->magic = magic;
1da177e4
LT
1241 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1242 dasd_get_device(device);
1243 return cqr;
1244}
7048a2b5 1245EXPORT_SYMBOL(dasd_kmalloc_request);
1da177e4 1246
68b781fe 1247struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
8e09f215
SW
1248 int datasize,
1249 struct dasd_device *device)
1da177e4
LT
1250{
1251 unsigned long flags;
1252 struct dasd_ccw_req *cqr;
1253 char *data;
1254 int size;
1255
1da177e4
LT
1256 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1257 if (cplength > 0)
1258 size += cplength * sizeof(struct ccw1);
1259 if (datasize > 0)
1260 size += datasize;
1261 spin_lock_irqsave(&device->mem_lock, flags);
1262 cqr = (struct dasd_ccw_req *)
1263 dasd_alloc_chunk(&device->ccw_chunks, size);
1264 spin_unlock_irqrestore(&device->mem_lock, flags);
1265 if (cqr == NULL)
1266 return ERR_PTR(-ENOMEM);
1267 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1268 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1269 cqr->cpaddr = NULL;
1270 if (cplength > 0) {
1271 cqr->cpaddr = (struct ccw1 *) data;
1272 data += cplength*sizeof(struct ccw1);
1273 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1274 }
1275 cqr->data = NULL;
1276 if (datasize > 0) {
1277 cqr->data = data;
1278 memset(cqr->data, 0, datasize);
1279 }
68b781fe 1280 cqr->magic = magic;
1da177e4
LT
1281 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1282 dasd_get_device(device);
1283 return cqr;
1284}
7048a2b5 1285EXPORT_SYMBOL(dasd_smalloc_request);
1da177e4
LT
1286
1287/*
1288 * Free memory of a channel program. This function needs to free all the
1289 * idal lists that might have been created by dasd_set_cda and the
1290 * struct dasd_ccw_req itself.
1291 */
8e09f215 1292void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4 1293{
347a8dc3 1294#ifdef CONFIG_64BIT
1da177e4
LT
1295 struct ccw1 *ccw;
1296
1297 /* Clear any idals used for the request. */
1298 ccw = cqr->cpaddr;
1299 do {
1300 clear_normalized_cda(ccw);
1301 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1302#endif
17fd682e
JJ
1303 kfree(cqr->cpaddr);
1304 kfree(cqr->data);
1da177e4
LT
1305 kfree(cqr);
1306 dasd_put_device(device);
1307}
7048a2b5 1308EXPORT_SYMBOL(dasd_kfree_request);
1da177e4 1309
8e09f215 1310void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4
LT
1311{
1312 unsigned long flags;
1313
1314 spin_lock_irqsave(&device->mem_lock, flags);
1315 dasd_free_chunk(&device->ccw_chunks, cqr);
1316 spin_unlock_irqrestore(&device->mem_lock, flags);
1317 dasd_put_device(device);
1318}
7048a2b5 1319EXPORT_SYMBOL(dasd_sfree_request);
1da177e4
LT
1320
1321/*
1322 * Check discipline magic in cqr.
1323 */
8e09f215 1324static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1da177e4
LT
1325{
1326 struct dasd_device *device;
1327
1328 if (cqr == NULL)
1329 return -EINVAL;
8e09f215 1330 device = cqr->startdev;
1da177e4 1331 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
fc19f381 1332 DBF_DEV_EVENT(DBF_WARNING, device,
1da177e4
LT
1333 " dasd_ccw_req 0x%08x magic doesn't match"
1334 " discipline 0x%08x",
1335 cqr->magic,
1336 *(unsigned int *) device->discipline->name);
1337 return -EINVAL;
1338 }
1339 return 0;
1340}
1341
1342/*
1343 * Terminate the current i/o and set the request to clear_pending.
1344 * Timer keeps device runnig.
1345 * ccw_device_clear can fail if the i/o subsystem
1346 * is in a bad mood.
1347 */
8e09f215 1348int dasd_term_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
1349{
1350 struct dasd_device *device;
1351 int retries, rc;
fc19f381 1352 char errorstring[ERRORLENGTH];
1da177e4
LT
1353
1354 /* Check the cqr */
1355 rc = dasd_check_cqr(cqr);
1356 if (rc)
1357 return rc;
1358 retries = 0;
8e09f215 1359 device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
1360 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1361 rc = ccw_device_clear(device->cdev, (long) cqr);
1362 switch (rc) {
1363 case 0: /* termination successful */
8e09f215 1364 cqr->status = DASD_CQR_CLEAR_PENDING;
1aae0560 1365 cqr->stopclk = get_tod_clock();
8f61701b 1366 cqr->starttime = 0;
1da177e4
LT
1367 DBF_DEV_EVENT(DBF_DEBUG, device,
1368 "terminate cqr %p successful",
1369 cqr);
1370 break;
1371 case -ENODEV:
1372 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1373 "device gone, retry");
1374 break;
1375 case -EIO:
1376 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1377 "I/O error, retry");
1378 break;
1379 case -EINVAL:
1380 case -EBUSY:
1381 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1382 "device busy, retry later");
1383 break;
1384 default:
fc19f381
SH
1385 /* internal error 10 - unknown rc*/
1386 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1387 dev_err(&device->cdev->dev, "An error occurred in the "
1388 "DASD device driver, reason=%s\n", errorstring);
1da177e4
LT
1389 BUG();
1390 break;
1391 }
1392 retries++;
1393 }
8e09f215 1394 dasd_schedule_device_bh(device);
1da177e4
LT
1395 return rc;
1396}
7048a2b5 1397EXPORT_SYMBOL(dasd_term_IO);
1da177e4
LT
1398
1399/*
1400 * Start the i/o. This start_IO can fail if the channel is really busy.
1401 * In that case set up a timer to start the request later.
1402 */
8e09f215 1403int dasd_start_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
1404{
1405 struct dasd_device *device;
1406 int rc;
fc19f381 1407 char errorstring[ERRORLENGTH];
1da177e4
LT
1408
1409 /* Check the cqr */
1410 rc = dasd_check_cqr(cqr);
6cc7f168
SW
1411 if (rc) {
1412 cqr->intrc = rc;
1da177e4 1413 return rc;
6cc7f168 1414 }
8e09f215 1415 device = (struct dasd_device *) cqr->startdev;
5a27e60d
SW
1416 if (((cqr->block &&
1417 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1418 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1419 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1420 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1421 "because of stolen lock", cqr);
1422 cqr->status = DASD_CQR_ERROR;
1423 cqr->intrc = -EPERM;
1424 return -EPERM;
1425 }
1da177e4 1426 if (cqr->retries < 0) {
fc19f381
SH
1427 /* internal error 14 - start_IO run out of retries */
1428 sprintf(errorstring, "14 %p", cqr);
1429 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1430 "device driver, reason=%s\n", errorstring);
8e09f215 1431 cqr->status = DASD_CQR_ERROR;
1da177e4
LT
1432 return -EIO;
1433 }
1aae0560 1434 cqr->startclk = get_tod_clock();
1da177e4
LT
1435 cqr->starttime = jiffies;
1436 cqr->retries--;
a4d26c6a
SW
1437 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1438 cqr->lpm &= device->path_data.opm;
1439 if (!cqr->lpm)
1440 cqr->lpm = device->path_data.opm;
1441 }
f3eb5384
SW
1442 if (cqr->cpmode == 1) {
1443 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1444 (long) cqr, cqr->lpm);
1445 } else {
1446 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1447 (long) cqr, cqr->lpm, 0);
1448 }
1da177e4
LT
1449 switch (rc) {
1450 case 0:
1451 cqr->status = DASD_CQR_IN_IO;
1da177e4
LT
1452 break;
1453 case -EBUSY:
a4d26c6a 1454 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
1455 "start_IO: device busy, retry later");
1456 break;
1457 case -ETIMEDOUT:
a4d26c6a 1458 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
1459 "start_IO: request timeout, retry later");
1460 break;
1461 case -EACCES:
a4d26c6a
SW
1462 /* -EACCES indicates that the request used only a subset of the
1463 * available paths and all these paths are gone. If the lpm of
1464 * this request was only a subset of the opm (e.g. the ppm) then
1465 * we just do a retry with all available paths.
1466 * If we already use the full opm, something is amiss, and we
1467 * need a full path verification.
1da177e4 1468 */
a4d26c6a
SW
1469 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1470 DBF_DEV_EVENT(DBF_WARNING, device,
1471 "start_IO: selected paths gone (%x)",
1472 cqr->lpm);
1473 } else if (cqr->lpm != device->path_data.opm) {
1474 cqr->lpm = device->path_data.opm;
1475 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1476 "start_IO: selected paths gone,"
1477 " retry on all paths");
1478 } else {
1479 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1480 "start_IO: all paths in opm gone,"
1481 " do path verification");
1482 dasd_generic_last_path_gone(device);
1483 device->path_data.opm = 0;
1484 device->path_data.ppm = 0;
1485 device->path_data.npm = 0;
1486 device->path_data.tbvpm =
1487 ccw_device_get_path_mask(device->cdev);
1488 }
1da177e4
LT
1489 break;
1490 case -ENODEV:
a4d26c6a 1491 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
f3eb5384
SW
1492 "start_IO: -ENODEV device gone, retry");
1493 break;
1da177e4 1494 case -EIO:
a4d26c6a 1495 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
f3eb5384 1496 "start_IO: -EIO device gone, retry");
1da177e4 1497 break;
d41dd122
SH
1498 case -EINVAL:
1499 /* most likely caused in power management context */
a4d26c6a 1500 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
d41dd122
SH
1501 "start_IO: -EINVAL device currently "
1502 "not accessible");
1503 break;
1da177e4 1504 default:
fc19f381
SH
1505 /* internal error 11 - unknown rc */
1506 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1507 dev_err(&device->cdev->dev,
1508 "An error occurred in the DASD device driver, "
1509 "reason=%s\n", errorstring);
1da177e4
LT
1510 BUG();
1511 break;
1512 }
6cc7f168 1513 cqr->intrc = rc;
1da177e4
LT
1514 return rc;
1515}
7048a2b5 1516EXPORT_SYMBOL(dasd_start_IO);
1da177e4
LT
1517
1518/*
1519 * Timeout function for dasd devices. This is used for different purposes
1520 * 1) missing interrupt handler for normal operation
1521 * 2) delayed start of request where start_IO failed with -EBUSY
1522 * 3) timeout for missing state change interrupts
1523 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1524 * DASD_CQR_QUEUED for 2) and 3).
1525 */
8e09f215 1526static void dasd_device_timeout(unsigned long ptr)
1da177e4
LT
1527{
1528 unsigned long flags;
1529 struct dasd_device *device;
1530
1531 device = (struct dasd_device *) ptr;
1532 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1533 /* re-activate request queue */
eb6e199b 1534 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1da177e4 1535 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215 1536 dasd_schedule_device_bh(device);
1da177e4
LT
1537}
1538
1539/*
1540 * Setup timeout for a device in jiffies.
1541 */
8e09f215 1542void dasd_device_set_timer(struct dasd_device *device, int expires)
1da177e4 1543{
48cae885
SW
1544 if (expires == 0)
1545 del_timer(&device->timer);
1546 else
1547 mod_timer(&device->timer, jiffies + expires);
1da177e4 1548}
7048a2b5 1549EXPORT_SYMBOL(dasd_device_set_timer);
1da177e4
LT
1550
1551/*
1552 * Clear timeout for a device.
1553 */
8e09f215 1554void dasd_device_clear_timer(struct dasd_device *device)
1da177e4 1555{
48cae885 1556 del_timer(&device->timer);
1da177e4 1557}
7048a2b5 1558EXPORT_SYMBOL(dasd_device_clear_timer);
1da177e4 1559
8e09f215
SW
1560static void dasd_handle_killed_request(struct ccw_device *cdev,
1561 unsigned long intparm)
1da177e4
LT
1562{
1563 struct dasd_ccw_req *cqr;
1564 struct dasd_device *device;
1565
f16f5843
SW
1566 if (!intparm)
1567 return;
1da177e4
LT
1568 cqr = (struct dasd_ccw_req *) intparm;
1569 if (cqr->status != DASD_CQR_IN_IO) {
b8ed5dd5
SH
1570 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1571 "invalid status in handle_killed_request: "
1572 "%02x", cqr->status);
1da177e4
LT
1573 return;
1574 }
1575
589c74d5
SH
1576 device = dasd_device_from_cdev_locked(cdev);
1577 if (IS_ERR(device)) {
1578 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1579 "unable to get device from cdev");
1580 return;
1581 }
1582
1583 if (!cqr->startdev ||
1584 device != cqr->startdev ||
1585 strncmp(cqr->startdev->discipline->ebcname,
1586 (char *) &cqr->magic, 4)) {
294001a8
SH
1587 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1588 "invalid device in request");
589c74d5 1589 dasd_put_device(device);
1da177e4
LT
1590 return;
1591 }
1592
1593 /* Schedule request to be retried. */
1594 cqr->status = DASD_CQR_QUEUED;
1595
8e09f215
SW
1596 dasd_device_clear_timer(device);
1597 dasd_schedule_device_bh(device);
1da177e4
LT
1598 dasd_put_device(device);
1599}
1600
8e09f215 1601void dasd_generic_handle_state_change(struct dasd_device *device)
1da177e4 1602{
20c64468
SW
1603 /* First of all start sense subsystem status request. */
1604 dasd_eer_snss(device);
1605
eb6e199b 1606 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
8e09f215
SW
1607 dasd_schedule_device_bh(device);
1608 if (device->block)
1609 dasd_schedule_block_bh(device->block);
1da177e4 1610}
7048a2b5 1611EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1da177e4
LT
1612
1613/*
1614 * Interrupt handler for "normal" ssch-io based dasd devices.
1615 */
8e09f215
SW
1616void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1617 struct irb *irb)
1da177e4
LT
1618{
1619 struct dasd_ccw_req *cqr, *next;
1620 struct dasd_device *device;
1621 unsigned long long now;
1622 int expires;
1da177e4
LT
1623
1624 if (IS_ERR(irb)) {
1625 switch (PTR_ERR(irb)) {
1626 case -EIO:
1da177e4
LT
1627 break;
1628 case -ETIMEDOUT:
b8ed5dd5
SH
1629 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1630 "request timed out\n", __func__);
1da177e4
LT
1631 break;
1632 default:
b8ed5dd5
SH
1633 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1634 "unknown error %ld\n", __func__,
1635 PTR_ERR(irb));
1da177e4 1636 }
f16f5843 1637 dasd_handle_killed_request(cdev, intparm);
1da177e4
LT
1638 return;
1639 }
1640
1aae0560 1641 now = get_tod_clock();
8e09f215 1642 cqr = (struct dasd_ccw_req *) intparm;
5a27e60d
SW
1643 /* check for conditions that should be handled immediately */
1644 if (!cqr ||
1645 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1646 scsw_cstat(&irb->scsw) == 0)) {
a5a0061f
SW
1647 if (cqr)
1648 memcpy(&cqr->irb, irb, sizeof(*irb));
a00bfd71 1649 device = dasd_device_from_cdev_locked(cdev);
56b86b61
SH
1650 if (IS_ERR(device))
1651 return;
1652 /* ignore unsolicited interrupts for DIAG discipline */
1653 if (device->discipline == dasd_diag_discipline_pointer) {
1da177e4 1654 dasd_put_device(device);
56b86b61 1655 return;
1da177e4 1656 }
5a27e60d
SW
1657 device->discipline->dump_sense_dbf(device, irb, "int");
1658 if (device->features & DASD_FEATURE_ERPLOG)
1659 device->discipline->dump_sense(device, cqr, irb);
1660 device->discipline->check_for_device_change(device, cqr, irb);
56b86b61 1661 dasd_put_device(device);
1da177e4 1662 }
5a27e60d
SW
1663 if (!cqr)
1664 return;
1da177e4 1665
8e09f215
SW
1666 device = (struct dasd_device *) cqr->startdev;
1667 if (!device ||
1da177e4 1668 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
294001a8
SH
1669 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1670 "invalid device in request");
1da177e4
LT
1671 return;
1672 }
1673
1674 /* Check for clear pending */
8e09f215 1675 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
f3eb5384 1676 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
8e09f215 1677 cqr->status = DASD_CQR_CLEARED;
931a3dce
SH
1678 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
1679 cqr->callback_data = DASD_SLEEPON_END_TAG;
8e09f215 1680 dasd_device_clear_timer(device);
8f61701b 1681 wake_up(&dasd_flush_wq);
931a3dce 1682 wake_up(&generic_waitq);
8e09f215 1683 dasd_schedule_device_bh(device);
1da177e4
LT
1684 return;
1685 }
1686
f3eb5384 1687 /* check status - the request might have been killed by dyn detach */
1da177e4 1688 if (cqr->status != DASD_CQR_IN_IO) {
fc19f381
SH
1689 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1690 "status %02x", dev_name(&cdev->dev), cqr->status);
1da177e4
LT
1691 return;
1692 }
fc19f381 1693
8e09f215 1694 next = NULL;
1da177e4 1695 expires = 0;
f3eb5384
SW
1696 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1697 scsw_cstat(&irb->scsw) == 0) {
8e09f215
SW
1698 /* request was completed successfully */
1699 cqr->status = DASD_CQR_SUCCESS;
1da177e4
LT
1700 cqr->stopclk = now;
1701 /* Start first request on queue if possible -> fast_io. */
8e09f215
SW
1702 if (cqr->devlist.next != &device->ccw_queue) {
1703 next = list_entry(cqr->devlist.next,
1704 struct dasd_ccw_req, devlist);
1da177e4 1705 }
8e09f215 1706 } else { /* error */
6c5f57c7
SH
1707 /*
1708 * If we don't want complex ERP for this request, then just
1709 * reset this and retry it in the fastpath
8e09f215 1710 */
6c5f57c7 1711 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
8e09f215 1712 cqr->retries > 0) {
a4d26c6a 1713 if (cqr->lpm == device->path_data.opm)
fc19f381
SH
1714 DBF_DEV_EVENT(DBF_DEBUG, device,
1715 "default ERP in fastpath "
1716 "(%i retries left)",
1717 cqr->retries);
a4d26c6a
SW
1718 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1719 cqr->lpm = device->path_data.opm;
8e09f215
SW
1720 cqr->status = DASD_CQR_QUEUED;
1721 next = cqr;
1722 } else
1da177e4 1723 cqr->status = DASD_CQR_ERROR;
8e09f215
SW
1724 }
1725 if (next && (next->status == DASD_CQR_QUEUED) &&
1726 (!device->stopped)) {
1727 if (device->discipline->start_IO(next) == 0)
1728 expires = next->expires;
1da177e4
LT
1729 }
1730 if (expires != 0)
8e09f215 1731 dasd_device_set_timer(device, expires);
1da177e4 1732 else
8e09f215
SW
1733 dasd_device_clear_timer(device);
1734 dasd_schedule_device_bh(device);
1da177e4 1735}
7048a2b5 1736EXPORT_SYMBOL(dasd_int_handler);
1da177e4 1737
a23ed009
SH
1738enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1739{
1740 struct dasd_device *device;
1741
1742 device = dasd_device_from_cdev_locked(cdev);
1743
1744 if (IS_ERR(device))
1745 goto out;
1746 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1747 device->state != device->target ||
5a27e60d 1748 !device->discipline->check_for_device_change){
a23ed009
SH
1749 dasd_put_device(device);
1750 goto out;
1751 }
5a27e60d
SW
1752 if (device->discipline->dump_sense_dbf)
1753 device->discipline->dump_sense_dbf(device, irb, "uc");
1754 device->discipline->check_for_device_change(device, NULL, irb);
a23ed009
SH
1755 dasd_put_device(device);
1756out:
1757 return UC_TODO_RETRY;
1758}
1759EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1760
1da177e4 1761/*
8e09f215
SW
1762 * If we have an error on a dasd_block layer request then we cancel
1763 * and return all further requests from the same dasd_block as well.
1da177e4 1764 */
8e09f215
SW
1765static void __dasd_device_recovery(struct dasd_device *device,
1766 struct dasd_ccw_req *ref_cqr)
1da177e4 1767{
8e09f215
SW
1768 struct list_head *l, *n;
1769 struct dasd_ccw_req *cqr;
1da177e4 1770
8e09f215
SW
1771 /*
1772 * only requeue request that came from the dasd_block layer
1773 */
1774 if (!ref_cqr->block)
1775 return;
1da177e4 1776
8e09f215
SW
1777 list_for_each_safe(l, n, &device->ccw_queue) {
1778 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1779 if (cqr->status == DASD_CQR_QUEUED &&
1780 ref_cqr->block == cqr->block) {
1781 cqr->status = DASD_CQR_CLEARED;
1782 }
1783 }
1784};
1da177e4
LT
1785
1786/*
8e09f215
SW
1787 * Remove those ccw requests from the queue that need to be returned
1788 * to the upper layer.
1da177e4 1789 */
8e09f215
SW
1790static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1791 struct list_head *final_queue)
1da177e4
LT
1792{
1793 struct list_head *l, *n;
1794 struct dasd_ccw_req *cqr;
1da177e4 1795
1da177e4
LT
1796 /* Process request with final status. */
1797 list_for_each_safe(l, n, &device->ccw_queue) {
8e09f215
SW
1798 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1799
1fbdb8be 1800 /* Skip any non-final request. */
8e09f215
SW
1801 if (cqr->status == DASD_CQR_QUEUED ||
1802 cqr->status == DASD_CQR_IN_IO ||
1803 cqr->status == DASD_CQR_CLEAR_PENDING)
1fbdb8be 1804 continue;
1da177e4 1805 if (cqr->status == DASD_CQR_ERROR) {
8e09f215 1806 __dasd_device_recovery(device, cqr);
20c64468 1807 }
1da177e4 1808 /* Rechain finished requests to final queue */
8e09f215 1809 list_move_tail(&cqr->devlist, final_queue);
1da177e4
LT
1810 }
1811}
1812
1da177e4 1813/*
8e09f215
SW
1814 * the cqrs from the final queue are returned to the upper layer
1815 * by setting a dasd_block state and calling the callback function
1da177e4 1816 */
8e09f215
SW
1817static void __dasd_device_process_final_queue(struct dasd_device *device,
1818 struct list_head *final_queue)
1da177e4 1819{
8e09f215 1820 struct list_head *l, *n;
1da177e4 1821 struct dasd_ccw_req *cqr;
03513bcc 1822 struct dasd_block *block;
c80ee724
SH
1823 void (*callback)(struct dasd_ccw_req *, void *data);
1824 void *callback_data;
fc19f381 1825 char errorstring[ERRORLENGTH];
f24acd45 1826
8e09f215
SW
1827 list_for_each_safe(l, n, final_queue) {
1828 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1829 list_del_init(&cqr->devlist);
03513bcc 1830 block = cqr->block;
c80ee724
SH
1831 callback = cqr->callback;
1832 callback_data = cqr->callback_data;
03513bcc
SW
1833 if (block)
1834 spin_lock_bh(&block->queue_lock);
8e09f215
SW
1835 switch (cqr->status) {
1836 case DASD_CQR_SUCCESS:
1837 cqr->status = DASD_CQR_DONE;
1838 break;
1839 case DASD_CQR_ERROR:
1840 cqr->status = DASD_CQR_NEED_ERP;
1841 break;
1842 case DASD_CQR_CLEARED:
1843 cqr->status = DASD_CQR_TERMINATED;
1844 break;
1845 default:
fc19f381
SH
1846 /* internal error 12 - wrong cqr status*/
1847 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1848 dev_err(&device->cdev->dev,
1849 "An error occurred in the DASD device driver, "
1850 "reason=%s\n", errorstring);
8e09f215 1851 BUG();
1da177e4 1852 }
8e09f215 1853 if (cqr->callback != NULL)
c80ee724 1854 (callback)(cqr, callback_data);
03513bcc
SW
1855 if (block)
1856 spin_unlock_bh(&block->queue_lock);
1da177e4
LT
1857 }
1858}
1859
1860/*
1861 * Take a look at the first request on the ccw queue and check
1862 * if it reached its expire time. If so, terminate the IO.
1863 */
8e09f215 1864static void __dasd_device_check_expire(struct dasd_device *device)
1da177e4
LT
1865{
1866 struct dasd_ccw_req *cqr;
1867
1868 if (list_empty(&device->ccw_queue))
1869 return;
8e09f215 1870 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
29145a6c
HH
1871 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1872 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
d07dc5d8
SH
1873 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1874 /*
1875 * IO in safe offline processing should not
1876 * run out of retries
1877 */
1878 cqr->retries++;
1879 }
29145a6c
HH
1880 if (device->discipline->term_IO(cqr) != 0) {
1881 /* Hmpf, try again in 5 sec */
fc19f381 1882 dev_err(&device->cdev->dev,
625c94df 1883 "cqr %p timed out (%lus) but cannot be "
fc19f381
SH
1884 "ended, retrying in 5 s\n",
1885 cqr, (cqr->expires/HZ));
7dc1da9f
SH
1886 cqr->expires += 5*HZ;
1887 dasd_device_set_timer(device, 5*HZ);
29145a6c 1888 } else {
fc19f381 1889 dev_err(&device->cdev->dev,
625c94df 1890 "cqr %p timed out (%lus), %i retries "
fc19f381
SH
1891 "remaining\n", cqr, (cqr->expires/HZ),
1892 cqr->retries);
1da177e4
LT
1893 }
1894 }
1895}
1896
1897/*
1898 * Take a look at the first request on the ccw queue and check
1899 * if it needs to be started.
1900 */
8e09f215 1901static void __dasd_device_start_head(struct dasd_device *device)
1da177e4
LT
1902{
1903 struct dasd_ccw_req *cqr;
1904 int rc;
1905
1906 if (list_empty(&device->ccw_queue))
1907 return;
8e09f215 1908 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
25ee4cf8
PO
1909 if (cqr->status != DASD_CQR_QUEUED)
1910 return;
a4d26c6a
SW
1911 /* when device is stopped, return request to previous layer
1912 * exception: only the disconnect or unresumed bits are set and the
1913 * cqr is a path verification request
1914 */
1915 if (device->stopped &&
1916 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1917 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1918 cqr->intrc = -EAGAIN;
8e09f215
SW
1919 cqr->status = DASD_CQR_CLEARED;
1920 dasd_schedule_device_bh(device);
25ee4cf8 1921 return;
1c01b8a5 1922 }
25ee4cf8
PO
1923
1924 rc = device->discipline->start_IO(cqr);
1925 if (rc == 0)
8e09f215 1926 dasd_device_set_timer(device, cqr->expires);
25ee4cf8 1927 else if (rc == -EACCES) {
8e09f215 1928 dasd_schedule_device_bh(device);
25ee4cf8
PO
1929 } else
1930 /* Hmpf, try again in 1/2 sec */
8e09f215 1931 dasd_device_set_timer(device, 50);
8f61701b
HH
1932}
1933
a4d26c6a
SW
1934static void __dasd_device_check_path_events(struct dasd_device *device)
1935{
1936 int rc;
1937
1938 if (device->path_data.tbvpm) {
1939 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1940 DASD_UNRESUMED_PM))
1941 return;
1942 rc = device->discipline->verify_path(
1943 device, device->path_data.tbvpm);
1944 if (rc)
1945 dasd_device_set_timer(device, 50);
1946 else
1947 device->path_data.tbvpm = 0;
1948 }
1949};
1950
1da177e4 1951/*
8e09f215
SW
1952 * Go through all request on the dasd_device request queue,
1953 * terminate them on the cdev if necessary, and return them to the
1954 * submitting layer via callback.
1955 * Note:
1956 * Make sure that all 'submitting layers' still exist when
1957 * this function is called!. In other words, when 'device' is a base
1958 * device then all block layer requests must have been removed before
1959 * via dasd_flush_block_queue.
1da177e4 1960 */
8e09f215 1961int dasd_flush_device_queue(struct dasd_device *device)
1da177e4 1962{
8e09f215
SW
1963 struct dasd_ccw_req *cqr, *n;
1964 int rc;
1da177e4 1965 struct list_head flush_queue;
1da177e4
LT
1966
1967 INIT_LIST_HEAD(&flush_queue);
1968 spin_lock_irq(get_ccwdev_lock(device->cdev));
8f61701b 1969 rc = 0;
8e09f215 1970 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
8f61701b
HH
1971 /* Check status and move request to flush_queue */
1972 switch (cqr->status) {
1973 case DASD_CQR_IN_IO:
1974 rc = device->discipline->term_IO(cqr);
1975 if (rc) {
1976 /* unable to terminate requeust */
fc19f381
SH
1977 dev_err(&device->cdev->dev,
1978 "Flushing the DASD request queue "
1979 "failed for request %p\n", cqr);
8f61701b
HH
1980 /* stop flush processing */
1981 goto finished;
1982 }
1983 break;
1984 case DASD_CQR_QUEUED:
1aae0560 1985 cqr->stopclk = get_tod_clock();
8e09f215 1986 cqr->status = DASD_CQR_CLEARED;
8f61701b 1987 break;
8e09f215 1988 default: /* no need to modify the others */
8f61701b
HH
1989 break;
1990 }
8e09f215 1991 list_move_tail(&cqr->devlist, &flush_queue);
8f61701b 1992 }
8f61701b
HH
1993finished:
1994 spin_unlock_irq(get_ccwdev_lock(device->cdev));
8e09f215
SW
1995 /*
1996 * After this point all requests must be in state CLEAR_PENDING,
1997 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1998 * one of the others.
1999 */
2000 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2001 wait_event(dasd_flush_wq,
2002 (cqr->status != DASD_CQR_CLEAR_PENDING));
2003 /*
2004 * Now set each request back to TERMINATED, DONE or NEED_ERP
2005 * and call the callback function of flushed requests
2006 */
2007 __dasd_device_process_final_queue(device, &flush_queue);
8f61701b 2008 return rc;
1da177e4 2009}
7048a2b5 2010EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
1da177e4
LT
2011
2012/*
2013 * Acquire the device lock and process queues for the device.
2014 */
8e09f215 2015static void dasd_device_tasklet(struct dasd_device *device)
1da177e4
LT
2016{
2017 struct list_head final_queue;
1da177e4
LT
2018
2019 atomic_set (&device->tasklet_scheduled, 0);
2020 INIT_LIST_HEAD(&final_queue);
2021 spin_lock_irq(get_ccwdev_lock(device->cdev));
2022 /* Check expire time of first request on the ccw queue. */
8e09f215
SW
2023 __dasd_device_check_expire(device);
2024 /* find final requests on ccw queue */
2025 __dasd_device_process_ccw_queue(device, &final_queue);
a4d26c6a 2026 __dasd_device_check_path_events(device);
1da177e4
LT
2027 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2028 /* Now call the callback function of requests with final status */
8e09f215
SW
2029 __dasd_device_process_final_queue(device, &final_queue);
2030 spin_lock_irq(get_ccwdev_lock(device->cdev));
1da177e4 2031 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
2032 __dasd_device_start_head(device);
2033 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4679e893
SH
2034 if (waitqueue_active(&shutdown_waitq))
2035 wake_up(&shutdown_waitq);
1da177e4
LT
2036 dasd_put_device(device);
2037}
2038
2039/*
2040 * Schedules a call to dasd_tasklet over the device tasklet.
2041 */
8e09f215 2042void dasd_schedule_device_bh(struct dasd_device *device)
1da177e4
LT
2043{
2044 /* Protect against rescheduling. */
973bd993 2045 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1da177e4
LT
2046 return;
2047 dasd_get_device(device);
2048 tasklet_hi_schedule(&device->tasklet);
2049}
7048a2b5 2050EXPORT_SYMBOL(dasd_schedule_device_bh);
1da177e4 2051
eb6e199b
SW
2052void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2053{
2054 device->stopped |= bits;
2055}
2056EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2057
2058void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2059{
2060 device->stopped &= ~bits;
2061 if (!device->stopped)
2062 wake_up(&generic_waitq);
2063}
2064EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2065
1da177e4 2066/*
8e09f215
SW
2067 * Queue a request to the head of the device ccw_queue.
2068 * Start the I/O if possible.
1da177e4 2069 */
8e09f215 2070void dasd_add_request_head(struct dasd_ccw_req *cqr)
1da177e4
LT
2071{
2072 struct dasd_device *device;
2073 unsigned long flags;
2074
8e09f215 2075 device = cqr->startdev;
1da177e4 2076 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
2077 cqr->status = DASD_CQR_QUEUED;
2078 list_add(&cqr->devlist, &device->ccw_queue);
1da177e4 2079 /* let the bh start the request to keep them in order */
8e09f215 2080 dasd_schedule_device_bh(device);
1da177e4
LT
2081 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2082}
7048a2b5 2083EXPORT_SYMBOL(dasd_add_request_head);
1da177e4
LT
2084
2085/*
8e09f215
SW
2086 * Queue a request to the tail of the device ccw_queue.
2087 * Start the I/O if possible.
1da177e4 2088 */
8e09f215 2089void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1da177e4
LT
2090{
2091 struct dasd_device *device;
2092 unsigned long flags;
2093
8e09f215 2094 device = cqr->startdev;
1da177e4 2095 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
2096 cqr->status = DASD_CQR_QUEUED;
2097 list_add_tail(&cqr->devlist, &device->ccw_queue);
1da177e4 2098 /* let the bh start the request to keep them in order */
8e09f215 2099 dasd_schedule_device_bh(device);
1da177e4
LT
2100 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2101}
7048a2b5 2102EXPORT_SYMBOL(dasd_add_request_tail);
1da177e4
LT
2103
2104/*
8e09f215 2105 * Wakeup helper for the 'sleep_on' functions.
1da177e4 2106 */
5915a873 2107void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1da177e4 2108{
1c1e093c
SW
2109 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2110 cqr->callback_data = DASD_SLEEPON_END_TAG;
2111 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2112 wake_up(&generic_waitq);
1da177e4 2113}
5915a873 2114EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
1da177e4 2115
8e09f215 2116static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1da177e4
LT
2117{
2118 struct dasd_device *device;
2119 int rc;
2120
8e09f215 2121 device = cqr->startdev;
1da177e4 2122 spin_lock_irq(get_ccwdev_lock(device->cdev));
1c1e093c 2123 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
1da177e4
LT
2124 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2125 return rc;
2126}
2127
2128/*
eb6e199b 2129 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1da177e4 2130 */
eb6e199b 2131static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1da177e4 2132{
1da177e4 2133 struct dasd_device *device;
eb6e199b 2134 dasd_erp_fn_t erp_fn;
138c014d 2135
eb6e199b
SW
2136 if (cqr->status == DASD_CQR_FILLED)
2137 return 0;
8e09f215 2138 device = cqr->startdev;
eb6e199b
SW
2139 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2140 if (cqr->status == DASD_CQR_TERMINATED) {
2141 device->discipline->handle_terminated_request(cqr);
2142 return 1;
2143 }
2144 if (cqr->status == DASD_CQR_NEED_ERP) {
2145 erp_fn = device->discipline->erp_action(cqr);
2146 erp_fn(cqr);
2147 return 1;
2148 }
2149 if (cqr->status == DASD_CQR_FAILED)
2150 dasd_log_sense(cqr, &cqr->irb);
2151 if (cqr->refers) {
2152 __dasd_process_erp(device, cqr);
2153 return 1;
2154 }
2155 }
2156 return 0;
2157}
138c014d 2158
eb6e199b
SW
2159static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2160{
2161 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2162 if (cqr->refers) /* erp is not done yet */
2163 return 1;
2164 return ((cqr->status != DASD_CQR_DONE) &&
2165 (cqr->status != DASD_CQR_FAILED));
2166 } else
2167 return (cqr->status == DASD_CQR_FILLED);
2168}
138c014d 2169
eb6e199b
SW
2170static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2171{
2172 struct dasd_device *device;
2173 int rc;
2174 struct list_head ccw_queue;
2175 struct dasd_ccw_req *cqr;
2176
2177 INIT_LIST_HEAD(&ccw_queue);
2178 maincqr->status = DASD_CQR_FILLED;
2179 device = maincqr->startdev;
2180 list_add(&maincqr->blocklist, &ccw_queue);
2181 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2182 cqr = list_first_entry(&ccw_queue,
2183 struct dasd_ccw_req, blocklist)) {
2184
2185 if (__dasd_sleep_on_erp(cqr))
2186 continue;
2187 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2188 continue;
5a27e60d
SW
2189 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2190 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2191 cqr->status = DASD_CQR_FAILED;
2192 cqr->intrc = -EPERM;
2193 continue;
2194 }
eb6e199b
SW
2195 /* Non-temporary stop condition will trigger fail fast */
2196 if (device->stopped & ~DASD_STOPPED_PENDING &&
2197 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2198 (!dasd_eer_enabled(device))) {
2199 cqr->status = DASD_CQR_FAILED;
d1ffc1f8 2200 cqr->intrc = -ENOLINK;
eb6e199b
SW
2201 continue;
2202 }
eb6e199b
SW
2203 /* Don't try to start requests if device is stopped */
2204 if (interruptible) {
2205 rc = wait_event_interruptible(
2206 generic_waitq, !(device->stopped));
2207 if (rc == -ERESTARTSYS) {
2208 cqr->status = DASD_CQR_FAILED;
2209 maincqr->intrc = rc;
2210 continue;
2211 }
2212 } else
2213 wait_event(generic_waitq, !(device->stopped));
2214
5915a873
SH
2215 if (!cqr->callback)
2216 cqr->callback = dasd_wakeup_cb;
2217
1c1e093c 2218 cqr->callback_data = DASD_SLEEPON_START_TAG;
eb6e199b
SW
2219 dasd_add_request_tail(cqr);
2220 if (interruptible) {
2221 rc = wait_event_interruptible(
2222 generic_waitq, _wait_for_wakeup(cqr));
2223 if (rc == -ERESTARTSYS) {
2224 dasd_cancel_req(cqr);
2225 /* wait (non-interruptible) for final status */
2226 wait_event(generic_waitq,
2227 _wait_for_wakeup(cqr));
2228 cqr->status = DASD_CQR_FAILED;
2229 maincqr->intrc = rc;
2230 continue;
2231 }
2232 } else
2233 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2234 }
2235
1aae0560 2236 maincqr->endclk = get_tod_clock();
eb6e199b
SW
2237 if ((maincqr->status != DASD_CQR_DONE) &&
2238 (maincqr->intrc != -ERESTARTSYS))
2239 dasd_log_sense(maincqr, &maincqr->irb);
2240 if (maincqr->status == DASD_CQR_DONE)
6cc7f168 2241 rc = 0;
eb6e199b
SW
2242 else if (maincqr->intrc)
2243 rc = maincqr->intrc;
6cc7f168
SW
2244 else
2245 rc = -EIO;
1da177e4
LT
2246 return rc;
2247}
2248
d42e1712
SH
2249static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2250{
2251 struct dasd_ccw_req *cqr;
2252
2253 list_for_each_entry(cqr, ccw_queue, blocklist) {
2254 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2255 return 0;
2256 }
2257
2258 return 1;
2259}
2260
2261static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2262{
2263 struct dasd_device *device;
d42e1712 2264 struct dasd_ccw_req *cqr, *n;
362ce84f 2265 int rc;
d42e1712
SH
2266
2267retry:
2268 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2269 device = cqr->startdev;
2270 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2271 continue;
2272
2273 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2274 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2275 cqr->status = DASD_CQR_FAILED;
2276 cqr->intrc = -EPERM;
2277 continue;
2278 }
2279 /*Non-temporary stop condition will trigger fail fast*/
2280 if (device->stopped & ~DASD_STOPPED_PENDING &&
2281 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2282 !dasd_eer_enabled(device)) {
2283 cqr->status = DASD_CQR_FAILED;
2284 cqr->intrc = -EAGAIN;
2285 continue;
2286 }
2287
2288 /*Don't try to start requests if device is stopped*/
2289 if (interruptible) {
2290 rc = wait_event_interruptible(
2291 generic_waitq, !device->stopped);
2292 if (rc == -ERESTARTSYS) {
2293 cqr->status = DASD_CQR_FAILED;
2294 cqr->intrc = rc;
2295 continue;
2296 }
2297 } else
2298 wait_event(generic_waitq, !(device->stopped));
2299
2300 if (!cqr->callback)
2301 cqr->callback = dasd_wakeup_cb;
2302 cqr->callback_data = DASD_SLEEPON_START_TAG;
2303 dasd_add_request_tail(cqr);
2304 }
2305
2306 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2307
2308 rc = 0;
2309 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
29b8dd9d
SH
2310 /*
2311 * for alias devices simplify error recovery and
2312 * return to upper layer
362ce84f 2313 * do not skip ERP requests
29b8dd9d 2314 */
362ce84f 2315 if (cqr->startdev != cqr->basedev && !cqr->refers &&
29b8dd9d
SH
2316 (cqr->status == DASD_CQR_TERMINATED ||
2317 cqr->status == DASD_CQR_NEED_ERP))
2318 return -EAGAIN;
362ce84f
SH
2319
2320 /* normal recovery for basedev IO */
2321 if (__dasd_sleep_on_erp(cqr)) {
2322 goto retry;
2323 /* remember that ERP was needed */
2324 rc = 1;
2325 /* skip processing for active cqr */
2326 if (cqr->status != DASD_CQR_TERMINATED &&
2327 cqr->status != DASD_CQR_NEED_ERP)
2328 break;
8fa56aed 2329 }
d42e1712 2330 }
362ce84f
SH
2331
2332 /* start ERP requests in upper loop */
d42e1712
SH
2333 if (rc)
2334 goto retry;
2335
d42e1712
SH
2336 return 0;
2337}
2338
eb6e199b
SW
2339/*
2340 * Queue a request to the tail of the device ccw_queue and wait for
2341 * it's completion.
2342 */
2343int dasd_sleep_on(struct dasd_ccw_req *cqr)
2344{
2345 return _dasd_sleep_on(cqr, 0);
2346}
7048a2b5 2347EXPORT_SYMBOL(dasd_sleep_on);
eb6e199b 2348
d42e1712
SH
2349/*
2350 * Start requests from a ccw_queue and wait for their completion.
2351 */
2352int dasd_sleep_on_queue(struct list_head *ccw_queue)
2353{
2354 return _dasd_sleep_on_queue(ccw_queue, 0);
2355}
2356EXPORT_SYMBOL(dasd_sleep_on_queue);
2357
1da177e4 2358/*
8e09f215
SW
2359 * Queue a request to the tail of the device ccw_queue and wait
2360 * interruptible for it's completion.
1da177e4 2361 */
8e09f215 2362int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1da177e4 2363{
eb6e199b 2364 return _dasd_sleep_on(cqr, 1);
1da177e4 2365}
7048a2b5 2366EXPORT_SYMBOL(dasd_sleep_on_interruptible);
1da177e4
LT
2367
2368/*
2369 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2370 * for eckd devices) the currently running request has to be terminated
2371 * and be put back to status queued, before the special request is added
2372 * to the head of the queue. Then the special request is waited on normally.
2373 */
8e09f215 2374static inline int _dasd_term_running_cqr(struct dasd_device *device)
1da177e4
LT
2375{
2376 struct dasd_ccw_req *cqr;
aade6c0d 2377 int rc;
1da177e4
LT
2378
2379 if (list_empty(&device->ccw_queue))
2380 return 0;
8e09f215 2381 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
aade6c0d
SH
2382 rc = device->discipline->term_IO(cqr);
2383 if (!rc)
2384 /*
2385 * CQR terminated because a more important request is pending.
2386 * Undo decreasing of retry counter because this is
2387 * not an error case.
2388 */
2389 cqr->retries++;
2390 return rc;
1da177e4
LT
2391}
2392
8e09f215 2393int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1da177e4 2394{
1da177e4
LT
2395 struct dasd_device *device;
2396 int rc;
138c014d 2397
8e09f215 2398 device = cqr->startdev;
5a27e60d
SW
2399 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2400 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2401 cqr->status = DASD_CQR_FAILED;
2402 cqr->intrc = -EPERM;
2403 return -EIO;
2404 }
1da177e4
LT
2405 spin_lock_irq(get_ccwdev_lock(device->cdev));
2406 rc = _dasd_term_running_cqr(device);
2407 if (rc) {
2408 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2409 return rc;
2410 }
1da177e4 2411 cqr->callback = dasd_wakeup_cb;
1c1e093c 2412 cqr->callback_data = DASD_SLEEPON_START_TAG;
1da177e4 2413 cqr->status = DASD_CQR_QUEUED;
214b8ffc
SH
2414 /*
2415 * add new request as second
2416 * first the terminated cqr needs to be finished
2417 */
2418 list_add(&cqr->devlist, device->ccw_queue.next);
138c014d 2419
1da177e4 2420 /* let the bh start the request to keep them in order */
8e09f215 2421 dasd_schedule_device_bh(device);
138c014d 2422
1da177e4
LT
2423 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2424
c80ee724 2425 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 2426
6cc7f168
SW
2427 if (cqr->status == DASD_CQR_DONE)
2428 rc = 0;
2429 else if (cqr->intrc)
2430 rc = cqr->intrc;
2431 else
2432 rc = -EIO;
0e003b70
SH
2433
2434 /* kick tasklets */
2435 dasd_schedule_device_bh(device);
2436 if (device->block)
2437 dasd_schedule_block_bh(device->block);
2438
1da177e4
LT
2439 return rc;
2440}
7048a2b5 2441EXPORT_SYMBOL(dasd_sleep_on_immediatly);
1da177e4
LT
2442
2443/*
2444 * Cancels a request that was started with dasd_sleep_on_req.
2445 * This is useful to timeout requests. The request will be
2446 * terminated if it is currently in i/o.
b99a946d 2447 * Returns 0 if request termination was successful
8e09f215
SW
2448 * negative error code if termination failed
2449 * Cancellation of a request is an asynchronous operation! The calling
2450 * function has to wait until the request is properly returned via callback.
1da177e4 2451 */
8e09f215 2452int dasd_cancel_req(struct dasd_ccw_req *cqr)
1da177e4 2453{
8e09f215 2454 struct dasd_device *device = cqr->startdev;
1da177e4
LT
2455 unsigned long flags;
2456 int rc;
2457
2458 rc = 0;
2459 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2460 switch (cqr->status) {
2461 case DASD_CQR_QUEUED:
8e09f215
SW
2462 /* request was not started - just set to cleared */
2463 cqr->status = DASD_CQR_CLEARED;
931a3dce
SH
2464 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2465 cqr->callback_data = DASD_SLEEPON_END_TAG;
1da177e4
LT
2466 break;
2467 case DASD_CQR_IN_IO:
2468 /* request in IO - terminate IO and release again */
8e09f215
SW
2469 rc = device->discipline->term_IO(cqr);
2470 if (rc) {
fc19f381
SH
2471 dev_err(&device->cdev->dev,
2472 "Cancelling request %p failed with rc=%d\n",
2473 cqr, rc);
8e09f215 2474 } else {
1aae0560 2475 cqr->stopclk = get_tod_clock();
8e09f215 2476 }
1da177e4 2477 break;
8e09f215 2478 default: /* already finished or clear pending - do nothing */
1da177e4 2479 break;
8e09f215
SW
2480 }
2481 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2482 dasd_schedule_device_bh(device);
2483 return rc;
2484}
7048a2b5 2485EXPORT_SYMBOL(dasd_cancel_req);
8e09f215 2486
8e09f215
SW
2487/*
2488 * SECTION: Operations of the dasd_block layer.
2489 */
2490
2491/*
2492 * Timeout function for dasd_block. This is used when the block layer
2493 * is waiting for something that may not come reliably, (e.g. a state
2494 * change interrupt)
2495 */
2496static void dasd_block_timeout(unsigned long ptr)
2497{
2498 unsigned long flags;
2499 struct dasd_block *block;
2500
2501 block = (struct dasd_block *) ptr;
2502 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2503 /* re-activate request queue */
eb6e199b 2504 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
8e09f215
SW
2505 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2506 dasd_schedule_block_bh(block);
2507}
2508
2509/*
2510 * Setup timeout for a dasd_block in jiffies.
2511 */
2512void dasd_block_set_timer(struct dasd_block *block, int expires)
2513{
48cae885
SW
2514 if (expires == 0)
2515 del_timer(&block->timer);
2516 else
2517 mod_timer(&block->timer, jiffies + expires);
8e09f215 2518}
7048a2b5 2519EXPORT_SYMBOL(dasd_block_set_timer);
8e09f215
SW
2520
2521/*
2522 * Clear timeout for a dasd_block.
2523 */
2524void dasd_block_clear_timer(struct dasd_block *block)
2525{
48cae885 2526 del_timer(&block->timer);
8e09f215 2527}
7048a2b5 2528EXPORT_SYMBOL(dasd_block_clear_timer);
8e09f215 2529
8e09f215
SW
2530/*
2531 * Process finished error recovery ccw.
2532 */
eb6e199b
SW
2533static void __dasd_process_erp(struct dasd_device *device,
2534 struct dasd_ccw_req *cqr)
8e09f215
SW
2535{
2536 dasd_erp_fn_t erp_fn;
8e09f215
SW
2537
2538 if (cqr->status == DASD_CQR_DONE)
2539 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2540 else
fc19f381 2541 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
8e09f215
SW
2542 erp_fn = device->discipline->erp_postaction(cqr);
2543 erp_fn(cqr);
2544}
1da177e4 2545
8e09f215
SW
2546/*
2547 * Fetch requests from the block device queue.
2548 */
2549static void __dasd_process_request_queue(struct dasd_block *block)
2550{
2551 struct request_queue *queue;
2552 struct request *req;
2553 struct dasd_ccw_req *cqr;
2554 struct dasd_device *basedev;
2555 unsigned long flags;
2556 queue = block->request_queue;
2557 basedev = block->base;
2558 /* No queue ? Then there is nothing to do. */
2559 if (queue == NULL)
2560 return;
2561
2562 /*
2563 * We requeue request from the block device queue to the ccw
2564 * queue only in two states. In state DASD_STATE_READY the
2565 * partition detection is done and we need to requeue requests
2566 * for that. State DASD_STATE_ONLINE is normal block device
2567 * operation.
2568 */
97f604b0
SW
2569 if (basedev->state < DASD_STATE_READY) {
2570 while ((req = blk_fetch_request(block->request_queue)))
2571 __blk_end_request_all(req, -EIO);
8e09f215 2572 return;
97f604b0 2573 }
8e09f215 2574 /* Now we try to fetch requests from the request queue */
7eaceacc 2575 while ((req = blk_peek_request(queue))) {
8e09f215
SW
2576 if (basedev->features & DASD_FEATURE_READONLY &&
2577 rq_data_dir(req) == WRITE) {
2578 DBF_DEV_EVENT(DBF_ERR, basedev,
2579 "Rejecting write request %p",
2580 req);
9934c8c0 2581 blk_start_request(req);
40cbbb78 2582 __blk_end_request_all(req, -EIO);
8e09f215
SW
2583 continue;
2584 }
5ea34a01
HR
2585 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2586 (basedev->features & DASD_FEATURE_FAILFAST ||
2587 blk_noretry_request(req))) {
2588 DBF_DEV_EVENT(DBF_ERR, basedev,
2589 "Rejecting failfast request %p",
2590 req);
2591 blk_start_request(req);
2592 __blk_end_request_all(req, -ETIMEDOUT);
2593 continue;
2594 }
8e09f215
SW
2595 cqr = basedev->discipline->build_cp(basedev, block, req);
2596 if (IS_ERR(cqr)) {
2597 if (PTR_ERR(cqr) == -EBUSY)
2598 break; /* normal end condition */
2599 if (PTR_ERR(cqr) == -ENOMEM)
2600 break; /* terminate request queue loop */
2601 if (PTR_ERR(cqr) == -EAGAIN) {
2602 /*
2603 * The current request cannot be build right
2604 * now, we have to try later. If this request
2605 * is the head-of-queue we stop the device
2606 * for 1/2 second.
2607 */
2608 if (!list_empty(&block->ccw_queue))
2609 break;
eb6e199b
SW
2610 spin_lock_irqsave(
2611 get_ccwdev_lock(basedev->cdev), flags);
2612 dasd_device_set_stop_bits(basedev,
2613 DASD_STOPPED_PENDING);
2614 spin_unlock_irqrestore(
2615 get_ccwdev_lock(basedev->cdev), flags);
8e09f215
SW
2616 dasd_block_set_timer(block, HZ/2);
2617 break;
2618 }
2619 DBF_DEV_EVENT(DBF_ERR, basedev,
2620 "CCW creation failed (rc=%ld) "
2621 "on request %p",
2622 PTR_ERR(cqr), req);
9934c8c0 2623 blk_start_request(req);
40cbbb78 2624 __blk_end_request_all(req, -EIO);
8e09f215
SW
2625 continue;
2626 }
2627 /*
2628 * Note: callback is set to dasd_return_cqr_cb in
2629 * __dasd_block_start_head to cover erp requests as well
2630 */
2631 cqr->callback_data = (void *) req;
2632 cqr->status = DASD_CQR_FILLED;
a2ace466 2633 req->completion_data = cqr;
9934c8c0 2634 blk_start_request(req);
8e09f215 2635 list_add_tail(&cqr->blocklist, &block->ccw_queue);
a2ace466 2636 INIT_LIST_HEAD(&cqr->devlist);
8e09f215
SW
2637 dasd_profile_start(block, cqr, req);
2638 }
2639}
2640
2641static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2642{
2643 struct request *req;
2644 int status;
4c4e2148 2645 int error = 0;
8e09f215
SW
2646
2647 req = (struct request *) cqr->callback_data;
2648 dasd_profile_end(cqr->block, cqr, req);
fe6b8e76 2649 status = cqr->block->base->discipline->free_cp(cqr, req);
d1ffc1f8
HR
2650 if (status < 0)
2651 error = status;
2652 else if (status == 0) {
2653 if (cqr->intrc == -EPERM)
2654 error = -EBADE;
2655 else if (cqr->intrc == -ENOLINK ||
2656 cqr->intrc == -ETIMEDOUT)
2657 error = cqr->intrc;
2658 else
2659 error = -EIO;
2660 }
40cbbb78 2661 __blk_end_request_all(req, error);
8e09f215
SW
2662}
2663
2664/*
2665 * Process ccw request queue.
2666 */
2667static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2668 struct list_head *final_queue)
2669{
2670 struct list_head *l, *n;
2671 struct dasd_ccw_req *cqr;
2672 dasd_erp_fn_t erp_fn;
2673 unsigned long flags;
2674 struct dasd_device *base = block->base;
2675
2676restart:
2677 /* Process request with final status. */
2678 list_for_each_safe(l, n, &block->ccw_queue) {
2679 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2680 if (cqr->status != DASD_CQR_DONE &&
2681 cqr->status != DASD_CQR_FAILED &&
2682 cqr->status != DASD_CQR_NEED_ERP &&
2683 cqr->status != DASD_CQR_TERMINATED)
2684 continue;
2685
2686 if (cqr->status == DASD_CQR_TERMINATED) {
2687 base->discipline->handle_terminated_request(cqr);
2688 goto restart;
2689 }
2690
2691 /* Process requests that may be recovered */
2692 if (cqr->status == DASD_CQR_NEED_ERP) {
6c5f57c7 2693 erp_fn = base->discipline->erp_action(cqr);
6a5176c4
SH
2694 if (IS_ERR(erp_fn(cqr)))
2695 continue;
8e09f215
SW
2696 goto restart;
2697 }
2698
a9cffb22
SH
2699 /* log sense for fatal error */
2700 if (cqr->status == DASD_CQR_FAILED) {
2701 dasd_log_sense(cqr, &cqr->irb);
2702 }
2703
8e09f215
SW
2704 /* First of all call extended error reporting. */
2705 if (dasd_eer_enabled(base) &&
2706 cqr->status == DASD_CQR_FAILED) {
2707 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2708
2709 /* restart request */
2710 cqr->status = DASD_CQR_FILLED;
2711 cqr->retries = 255;
2712 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
eb6e199b 2713 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
8e09f215
SW
2714 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2715 flags);
2716 goto restart;
2717 }
2718
2719 /* Process finished ERP request. */
2720 if (cqr->refers) {
eb6e199b 2721 __dasd_process_erp(base, cqr);
8e09f215
SW
2722 goto restart;
2723 }
2724
2725 /* Rechain finished requests to final queue */
1aae0560 2726 cqr->endclk = get_tod_clock();
8e09f215
SW
2727 list_move_tail(&cqr->blocklist, final_queue);
2728 }
2729}
2730
2731static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2732{
2733 dasd_schedule_block_bh(cqr->block);
2734}
2735
2736static void __dasd_block_start_head(struct dasd_block *block)
2737{
2738 struct dasd_ccw_req *cqr;
2739
2740 if (list_empty(&block->ccw_queue))
2741 return;
2742 /* We allways begin with the first requests on the queue, as some
2743 * of previously started requests have to be enqueued on a
2744 * dasd_device again for error recovery.
2745 */
2746 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2747 if (cqr->status != DASD_CQR_FILLED)
2748 continue;
5a27e60d
SW
2749 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2750 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2751 cqr->status = DASD_CQR_FAILED;
2752 cqr->intrc = -EPERM;
2753 dasd_schedule_block_bh(block);
2754 continue;
2755 }
8e09f215
SW
2756 /* Non-temporary stop condition will trigger fail fast */
2757 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2758 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2759 (!dasd_eer_enabled(block->base))) {
2760 cqr->status = DASD_CQR_FAILED;
d1ffc1f8 2761 cqr->intrc = -ENOLINK;
8e09f215
SW
2762 dasd_schedule_block_bh(block);
2763 continue;
2764 }
2765 /* Don't try to start requests if device is stopped */
2766 if (block->base->stopped)
2767 return;
2768
2769 /* just a fail safe check, should not happen */
2770 if (!cqr->startdev)
2771 cqr->startdev = block->base;
2772
2773 /* make sure that the requests we submit find their way back */
2774 cqr->callback = dasd_return_cqr_cb;
2775
2776 dasd_add_request_tail(cqr);
2777 }
2778}
2779
2780/*
2781 * Central dasd_block layer routine. Takes requests from the generic
2782 * block layer request queue, creates ccw requests, enqueues them on
2783 * a dasd_device and processes ccw requests that have been returned.
2784 */
2785static void dasd_block_tasklet(struct dasd_block *block)
2786{
2787 struct list_head final_queue;
2788 struct list_head *l, *n;
2789 struct dasd_ccw_req *cqr;
2790
2791 atomic_set(&block->tasklet_scheduled, 0);
2792 INIT_LIST_HEAD(&final_queue);
2793 spin_lock(&block->queue_lock);
2794 /* Finish off requests on ccw queue */
2795 __dasd_process_block_ccw_queue(block, &final_queue);
2796 spin_unlock(&block->queue_lock);
2797 /* Now call the callback function of requests with final status */
2798 spin_lock_irq(&block->request_queue_lock);
2799 list_for_each_safe(l, n, &final_queue) {
2800 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2801 list_del_init(&cqr->blocklist);
2802 __dasd_cleanup_cqr(cqr);
2803 }
2804 spin_lock(&block->queue_lock);
2805 /* Get new request from the block device request queue */
2806 __dasd_process_request_queue(block);
2807 /* Now check if the head of the ccw queue needs to be started. */
2808 __dasd_block_start_head(block);
2809 spin_unlock(&block->queue_lock);
2810 spin_unlock_irq(&block->request_queue_lock);
4679e893
SH
2811 if (waitqueue_active(&shutdown_waitq))
2812 wake_up(&shutdown_waitq);
8e09f215
SW
2813 dasd_put_device(block->base);
2814}
2815
2816static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2817{
2818 wake_up(&dasd_flush_wq);
2819}
2820
c5576876
SH
2821/*
2822 * Requeue a request back to the block request queue
2823 * only works for block requests
2824 */
2825static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2826{
2827 struct dasd_block *block = cqr->block;
2828 struct request *req;
2829 unsigned long flags;
2830
2831 if (!block)
2832 return -EINVAL;
2833 spin_lock_irqsave(&block->queue_lock, flags);
2834 req = (struct request *) cqr->callback_data;
2835 blk_requeue_request(block->request_queue, req);
2836 spin_unlock_irqrestore(&block->queue_lock, flags);
2837
2838 return 0;
2839}
2840
8e09f215
SW
2841/*
2842 * Go through all request on the dasd_block request queue, cancel them
2843 * on the respective dasd_device, and return them to the generic
2844 * block layer.
2845 */
2846static int dasd_flush_block_queue(struct dasd_block *block)
2847{
2848 struct dasd_ccw_req *cqr, *n;
2849 int rc, i;
2850 struct list_head flush_queue;
2851
2852 INIT_LIST_HEAD(&flush_queue);
2853 spin_lock_bh(&block->queue_lock);
2854 rc = 0;
2855restart:
2856 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2857 /* if this request currently owned by a dasd_device cancel it */
2858 if (cqr->status >= DASD_CQR_QUEUED)
2859 rc = dasd_cancel_req(cqr);
2860 if (rc < 0)
2861 break;
2862 /* Rechain request (including erp chain) so it won't be
2863 * touched by the dasd_block_tasklet anymore.
2864 * Replace the callback so we notice when the request
2865 * is returned from the dasd_device layer.
2866 */
2867 cqr->callback = _dasd_wake_block_flush_cb;
2868 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2869 list_move_tail(&cqr->blocklist, &flush_queue);
2870 if (i > 1)
2871 /* moved more than one request - need to restart */
2872 goto restart;
2873 }
2874 spin_unlock_bh(&block->queue_lock);
2875 /* Now call the callback function of flushed requests */
2876restart_cb:
2877 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2878 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2879 /* Process finished ERP request. */
2880 if (cqr->refers) {
0cd4bd47 2881 spin_lock_bh(&block->queue_lock);
eb6e199b 2882 __dasd_process_erp(block->base, cqr);
0cd4bd47 2883 spin_unlock_bh(&block->queue_lock);
8e09f215
SW
2884 /* restart list_for_xx loop since dasd_process_erp
2885 * might remove multiple elements */
2886 goto restart_cb;
2887 }
2888 /* call the callback function */
0cd4bd47 2889 spin_lock_irq(&block->request_queue_lock);
1aae0560 2890 cqr->endclk = get_tod_clock();
8e09f215
SW
2891 list_del_init(&cqr->blocklist);
2892 __dasd_cleanup_cqr(cqr);
0cd4bd47 2893 spin_unlock_irq(&block->request_queue_lock);
1da177e4 2894 }
1da177e4
LT
2895 return rc;
2896}
2897
2898/*
8e09f215
SW
2899 * Schedules a call to dasd_tasklet over the device tasklet.
2900 */
2901void dasd_schedule_block_bh(struct dasd_block *block)
2902{
2903 /* Protect against rescheduling. */
2904 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2905 return;
2906 /* life cycle of block is bound to it's base device */
2907 dasd_get_device(block->base);
2908 tasklet_hi_schedule(&block->tasklet);
2909}
7048a2b5 2910EXPORT_SYMBOL(dasd_schedule_block_bh);
8e09f215
SW
2911
2912
2913/*
2914 * SECTION: external block device operations
2915 * (request queue handling, open, release, etc.)
1da177e4
LT
2916 */
2917
2918/*
2919 * Dasd request queue function. Called from ll_rw_blk.c
2920 */
8e09f215 2921static void do_dasd_request(struct request_queue *queue)
1da177e4 2922{
8e09f215 2923 struct dasd_block *block;
1da177e4 2924
8e09f215
SW
2925 block = queue->queuedata;
2926 spin_lock(&block->queue_lock);
1da177e4 2927 /* Get new request from the block device request queue */
8e09f215 2928 __dasd_process_request_queue(block);
1da177e4 2929 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
2930 __dasd_block_start_head(block);
2931 spin_unlock(&block->queue_lock);
1da177e4
LT
2932}
2933
a2ace466
HR
2934/*
2935 * Block timeout callback, called from the block layer
2936 *
2937 * request_queue lock is held on entry.
2938 *
2939 * Return values:
2940 * BLK_EH_RESET_TIMER if the request should be left running
2941 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2942 * by the driver.
2943 */
2944enum blk_eh_timer_return dasd_times_out(struct request *req)
2945{
2946 struct dasd_ccw_req *cqr = req->completion_data;
2947 struct dasd_block *block = req->q->queuedata;
2948 struct dasd_device *device;
2949 int rc = 0;
2950
2951 if (!cqr)
2952 return BLK_EH_NOT_HANDLED;
2953
2954 device = cqr->startdev ? cqr->startdev : block->base;
3d71ad32
HR
2955 if (!device->blk_timeout)
2956 return BLK_EH_RESET_TIMER;
a2ace466
HR
2957 DBF_DEV_EVENT(DBF_WARNING, device,
2958 " dasd_times_out cqr %p status %x",
2959 cqr, cqr->status);
2960
2961 spin_lock(&block->queue_lock);
2962 spin_lock(get_ccwdev_lock(device->cdev));
2963 cqr->retries = -1;
2964 cqr->intrc = -ETIMEDOUT;
2965 if (cqr->status >= DASD_CQR_QUEUED) {
2966 spin_unlock(get_ccwdev_lock(device->cdev));
2967 rc = dasd_cancel_req(cqr);
2968 } else if (cqr->status == DASD_CQR_FILLED ||
2969 cqr->status == DASD_CQR_NEED_ERP) {
2970 cqr->status = DASD_CQR_TERMINATED;
2971 spin_unlock(get_ccwdev_lock(device->cdev));
2972 } else if (cqr->status == DASD_CQR_IN_ERP) {
2973 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2974
2975 list_for_each_entry_safe(searchcqr, nextcqr,
2976 &block->ccw_queue, blocklist) {
2977 tmpcqr = searchcqr;
2978 while (tmpcqr->refers)
2979 tmpcqr = tmpcqr->refers;
2980 if (tmpcqr != cqr)
2981 continue;
2982 /* searchcqr is an ERP request for cqr */
2983 searchcqr->retries = -1;
2984 searchcqr->intrc = -ETIMEDOUT;
2985 if (searchcqr->status >= DASD_CQR_QUEUED) {
2986 spin_unlock(get_ccwdev_lock(device->cdev));
2987 rc = dasd_cancel_req(searchcqr);
2988 spin_lock(get_ccwdev_lock(device->cdev));
2989 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2990 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2991 searchcqr->status = DASD_CQR_TERMINATED;
2992 rc = 0;
2993 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2994 /*
2995 * Shouldn't happen; most recent ERP
2996 * request is at the front of queue
2997 */
2998 continue;
2999 }
3000 break;
3001 }
3002 spin_unlock(get_ccwdev_lock(device->cdev));
3003 }
3004 dasd_schedule_block_bh(block);
3005 spin_unlock(&block->queue_lock);
3006
3007 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3008}
3009
1da177e4
LT
3010/*
3011 * Allocate and initialize request queue and default I/O scheduler.
3012 */
8e09f215 3013static int dasd_alloc_queue(struct dasd_block *block)
1da177e4
LT
3014{
3015 int rc;
3016
8e09f215
SW
3017 block->request_queue = blk_init_queue(do_dasd_request,
3018 &block->request_queue_lock);
3019 if (block->request_queue == NULL)
1da177e4
LT
3020 return -ENOMEM;
3021
8e09f215 3022 block->request_queue->queuedata = block;
1da177e4 3023
8e09f215 3024 elevator_exit(block->request_queue->elevator);
08a8a0c5 3025 block->request_queue->elevator = NULL;
ef089941 3026 mutex_lock(&block->request_queue->sysfs_lock);
8e09f215 3027 rc = elevator_init(block->request_queue, "deadline");
ef089941 3028 if (rc)
8e09f215 3029 blk_cleanup_queue(block->request_queue);
ef089941
HC
3030 mutex_unlock(&block->request_queue->sysfs_lock);
3031 return rc;
1da177e4
LT
3032}
3033
3034/*
3035 * Allocate and initialize request queue.
3036 */
8e09f215 3037static void dasd_setup_queue(struct dasd_block *block)
1da177e4
LT
3038{
3039 int max;
3040
e4dbb0f2
SH
3041 if (block->base->features & DASD_FEATURE_USERAW) {
3042 /*
3043 * the max_blocks value for raw_track access is 256
3044 * it is higher than the native ECKD value because we
3045 * only need one ccw per track
3046 * so the max_hw_sectors are
3047 * 2048 x 512B = 1024kB = 16 tracks
3048 */
3049 max = 2048;
3050 } else {
3051 max = block->base->discipline->max_blocks << block->s2b_shift;
3052 }
3053 blk_queue_logical_block_size(block->request_queue,
3054 block->bp_block);
086fa5ff 3055 blk_queue_max_hw_sectors(block->request_queue, max);
8a78362c 3056 blk_queue_max_segments(block->request_queue, -1L);
f3eb5384
SW
3057 /* with page sized segments we can translate each segement into
3058 * one idaw/tidaw
3059 */
3060 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3061 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
1da177e4
LT
3062}
3063
3064/*
3065 * Deactivate and free request queue.
3066 */
8e09f215 3067static void dasd_free_queue(struct dasd_block *block)
1da177e4 3068{
8e09f215
SW
3069 if (block->request_queue) {
3070 blk_cleanup_queue(block->request_queue);
3071 block->request_queue = NULL;
1da177e4
LT
3072 }
3073}
3074
3075/*
3076 * Flush request on the request queue.
3077 */
8e09f215 3078static void dasd_flush_request_queue(struct dasd_block *block)
1da177e4
LT
3079{
3080 struct request *req;
3081
8e09f215 3082 if (!block->request_queue)
1da177e4 3083 return;
138c014d 3084
8e09f215 3085 spin_lock_irq(&block->request_queue_lock);
9934c8c0 3086 while ((req = blk_fetch_request(block->request_queue)))
40cbbb78 3087 __blk_end_request_all(req, -EIO);
8e09f215 3088 spin_unlock_irq(&block->request_queue_lock);
1da177e4
LT
3089}
3090
57a7c0bc 3091static int dasd_open(struct block_device *bdev, fmode_t mode)
1da177e4 3092{
9eb25122 3093 struct dasd_device *base;
1da177e4
LT
3094 int rc;
3095
65f8da47
SW
3096 base = dasd_device_from_gendisk(bdev->bd_disk);
3097 if (!base)
9eb25122
SH
3098 return -ENODEV;
3099
65f8da47 3100 atomic_inc(&base->block->open_count);
8e09f215 3101 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
1da177e4
LT
3102 rc = -ENODEV;
3103 goto unlock;
3104 }
3105
8e09f215 3106 if (!try_module_get(base->discipline->owner)) {
1da177e4
LT
3107 rc = -EINVAL;
3108 goto unlock;
3109 }
3110
3111 if (dasd_probeonly) {
fc19f381
SH
3112 dev_info(&base->cdev->dev,
3113 "Accessing the DASD failed because it is in "
3114 "probeonly mode\n");
1da177e4
LT
3115 rc = -EPERM;
3116 goto out;
3117 }
3118
8e09f215
SW
3119 if (base->state <= DASD_STATE_BASIC) {
3120 DBF_DEV_EVENT(DBF_ERR, base, " %s",
1da177e4
LT
3121 " Cannot open unrecognized device");
3122 rc = -ENODEV;
3123 goto out;
3124 }
3125
33b62a30
SW
3126 if ((mode & FMODE_WRITE) &&
3127 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3128 (base->features & DASD_FEATURE_READONLY))) {
3129 rc = -EROFS;
3130 goto out;
3131 }
3132
65f8da47 3133 dasd_put_device(base);
1da177e4
LT
3134 return 0;
3135
3136out:
8e09f215 3137 module_put(base->discipline->owner);
1da177e4 3138unlock:
65f8da47
SW
3139 atomic_dec(&base->block->open_count);
3140 dasd_put_device(base);
1da177e4
LT
3141 return rc;
3142}
3143
db2a144b 3144static void dasd_release(struct gendisk *disk, fmode_t mode)
1da177e4 3145{
db2a144b
AV
3146 struct dasd_device *base = dasd_device_from_gendisk(disk);
3147 if (base) {
3148 atomic_dec(&base->block->open_count);
3149 module_put(base->discipline->owner);
3150 dasd_put_device(base);
3151 }
1da177e4
LT
3152}
3153
a885c8c4
CH
3154/*
3155 * Return disk geometry.
3156 */
8e09f215 3157static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
a885c8c4 3158{
8e09f215 3159 struct dasd_device *base;
a885c8c4 3160
65f8da47
SW
3161 base = dasd_device_from_gendisk(bdev->bd_disk);
3162 if (!base)
a885c8c4
CH
3163 return -ENODEV;
3164
8e09f215 3165 if (!base->discipline ||
65f8da47
SW
3166 !base->discipline->fill_geometry) {
3167 dasd_put_device(base);
a885c8c4 3168 return -EINVAL;
65f8da47
SW
3169 }
3170 base->discipline->fill_geometry(base->block, geo);
3171 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3172 dasd_put_device(base);
a885c8c4
CH
3173 return 0;
3174}
3175
83d5cde4 3176const struct block_device_operations
1da177e4
LT
3177dasd_device_operations = {
3178 .owner = THIS_MODULE,
57a7c0bc
AV
3179 .open = dasd_open,
3180 .release = dasd_release,
0000d031
HC
3181 .ioctl = dasd_ioctl,
3182 .compat_ioctl = dasd_ioctl,
a885c8c4 3183 .getgeo = dasd_getgeo,
1da177e4
LT
3184};
3185
8e09f215
SW
3186/*******************************************************************************
3187 * end of block device operations
3188 */
1da177e4
LT
3189
3190static void
3191dasd_exit(void)
3192{
3193#ifdef CONFIG_PROC_FS
3194 dasd_proc_exit();
3195#endif
20c64468 3196 dasd_eer_exit();
6bb0e010
HH
3197 if (dasd_page_cache != NULL) {
3198 kmem_cache_destroy(dasd_page_cache);
3199 dasd_page_cache = NULL;
3200 }
1da177e4
LT
3201 dasd_gendisk_exit();
3202 dasd_devmap_exit();
1da177e4
LT
3203 if (dasd_debug_area != NULL) {
3204 debug_unregister(dasd_debug_area);
3205 dasd_debug_area = NULL;
3206 }
4fa52aa7 3207 dasd_statistics_removeroot();
1da177e4
LT
3208}
3209
3210/*
3211 * SECTION: common functions for ccw_driver use
3212 */
3213
33b62a30
SW
3214/*
3215 * Is the device read-only?
3216 * Note that this function does not report the setting of the
3217 * readonly device attribute, but how it is configured in z/VM.
3218 */
3219int dasd_device_is_ro(struct dasd_device *device)
3220{
3221 struct ccw_dev_id dev_id;
3222 struct diag210 diag_data;
3223 int rc;
3224
3225 if (!MACHINE_IS_VM)
3226 return 0;
3227 ccw_device_get_id(device->cdev, &dev_id);
3228 memset(&diag_data, 0, sizeof(diag_data));
3229 diag_data.vrdcdvno = dev_id.devno;
3230 diag_data.vrdclen = sizeof(diag_data);
3231 rc = diag210(&diag_data);
3232 if (rc == 0 || rc == 2) {
3233 return diag_data.vrdcvfla & 0x80;
3234 } else {
3235 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3236 dev_id.devno, rc);
3237 return 0;
3238 }
3239}
3240EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3241
f3445a1a
CH
3242static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3243{
3244 struct ccw_device *cdev = data;
3245 int ret;
3246
3247 ret = ccw_device_set_online(cdev);
3248 if (ret)
4ce25966
FF
3249 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3250 dev_name(&cdev->dev), ret);
f3445a1a
CH
3251}
3252
1c01b8a5
HH
3253/*
3254 * Initial attempt at a probe function. this can be simplified once
3255 * the other detection code is gone.
3256 */
8e09f215
SW
3257int dasd_generic_probe(struct ccw_device *cdev,
3258 struct dasd_discipline *discipline)
1da177e4
LT
3259{
3260 int ret;
3261
3262 ret = dasd_add_sysfs_files(cdev);
3263 if (ret) {
b8ed5dd5
SH
3264 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3265 "dasd_generic_probe: could not add "
3266 "sysfs entries");
40545573 3267 return ret;
1da177e4 3268 }
40545573 3269 cdev->handler = &dasd_int_handler;
1da177e4 3270
40545573
HH
3271 /*
3272 * Automatically online either all dasd devices (dasd_autodetect)
3273 * or all devices specified with dasd= parameters during
3274 * initial probe.
3275 */
3276 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2a0217d5 3277 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
f3445a1a 3278 async_schedule(dasd_generic_auto_online, cdev);
de3e0da1 3279 return 0;
1da177e4 3280}
7048a2b5 3281EXPORT_SYMBOL_GPL(dasd_generic_probe);
1da177e4 3282
1c01b8a5
HH
3283/*
3284 * This will one day be called from a global not_oper handler.
3285 * It is also used by driver_unregister during module unload.
3286 */
8e09f215 3287void dasd_generic_remove(struct ccw_device *cdev)
1da177e4
LT
3288{
3289 struct dasd_device *device;
8e09f215 3290 struct dasd_block *block;
1da177e4 3291
59afda78
HH
3292 cdev->handler = NULL;
3293
1da177e4 3294 device = dasd_device_from_cdev(cdev);
be4904e5
SW
3295 if (IS_ERR(device)) {
3296 dasd_remove_sysfs_files(cdev);
1da177e4 3297 return;
be4904e5 3298 }
d07dc5d8
SH
3299 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3300 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1da177e4
LT
3301 /* Already doing offline processing */
3302 dasd_put_device(device);
be4904e5 3303 dasd_remove_sysfs_files(cdev);
1da177e4
LT
3304 return;
3305 }
3306 /*
3307 * This device is removed unconditionally. Set offline
3308 * flag to prevent dasd_open from opening it while it is
3309 * no quite down yet.
3310 */
3311 dasd_set_target_state(device, DASD_STATE_NEW);
3312 /* dasd_delete_device destroys the device reference. */
8e09f215 3313 block = device->block;
1da177e4 3314 dasd_delete_device(device);
8e09f215
SW
3315 /*
3316 * life cycle of block is bound to device, so delete it after
3317 * device was safely removed
3318 */
3319 if (block)
3320 dasd_free_block(block);
d07dc5d8
SH
3321
3322 dasd_remove_sysfs_files(cdev);
1da177e4 3323}
7048a2b5 3324EXPORT_SYMBOL_GPL(dasd_generic_remove);
1da177e4 3325
1c01b8a5
HH
3326/*
3327 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1da177e4 3328 * the device is detected for the first time and is supposed to be used
1c01b8a5
HH
3329 * or the user has started activation through sysfs.
3330 */
8e09f215
SW
3331int dasd_generic_set_online(struct ccw_device *cdev,
3332 struct dasd_discipline *base_discipline)
1da177e4 3333{
aa88861f 3334 struct dasd_discipline *discipline;
1da177e4 3335 struct dasd_device *device;
c6eb7b77 3336 int rc;
f24acd45 3337
40545573
HH
3338 /* first online clears initial online feature flag */
3339 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
1da177e4
LT
3340 device = dasd_create_device(cdev);
3341 if (IS_ERR(device))
3342 return PTR_ERR(device);
3343
aa88861f 3344 discipline = base_discipline;
c6eb7b77 3345 if (device->features & DASD_FEATURE_USEDIAG) {
1da177e4 3346 if (!dasd_diag_discipline_pointer) {
4ce25966
FF
3347 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3348 dev_name(&cdev->dev));
1da177e4
LT
3349 dasd_delete_device(device);
3350 return -ENODEV;
3351 }
3352 discipline = dasd_diag_discipline_pointer;
3353 }
aa88861f
PO
3354 if (!try_module_get(base_discipline->owner)) {
3355 dasd_delete_device(device);
3356 return -EINVAL;
3357 }
3358 if (!try_module_get(discipline->owner)) {
3359 module_put(base_discipline->owner);
3360 dasd_delete_device(device);
3361 return -EINVAL;
3362 }
3363 device->base_discipline = base_discipline;
1da177e4
LT
3364 device->discipline = discipline;
3365
8e09f215 3366 /* check_device will allocate block device if necessary */
1da177e4
LT
3367 rc = discipline->check_device(device);
3368 if (rc) {
4ce25966
FF
3369 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3370 dev_name(&cdev->dev), discipline->name, rc);
aa88861f
PO
3371 module_put(discipline->owner);
3372 module_put(base_discipline->owner);
1da177e4
LT
3373 dasd_delete_device(device);
3374 return rc;
3375 }
3376
3377 dasd_set_target_state(device, DASD_STATE_ONLINE);
3378 if (device->state <= DASD_STATE_KNOWN) {
4ce25966
FF
3379 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3380 dev_name(&cdev->dev));
1da177e4
LT
3381 rc = -ENODEV;
3382 dasd_set_target_state(device, DASD_STATE_NEW);
8e09f215
SW
3383 if (device->block)
3384 dasd_free_block(device->block);
1da177e4
LT
3385 dasd_delete_device(device);
3386 } else
3387 pr_debug("dasd_generic device %s found\n",
2a0217d5 3388 dev_name(&cdev->dev));
589c74d5
SH
3389
3390 wait_event(dasd_init_waitq, _wait_for_device(device));
3391
1da177e4 3392 dasd_put_device(device);
1da177e4
LT
3393 return rc;
3394}
7048a2b5 3395EXPORT_SYMBOL_GPL(dasd_generic_set_online);
1da177e4 3396
8e09f215 3397int dasd_generic_set_offline(struct ccw_device *cdev)
1da177e4
LT
3398{
3399 struct dasd_device *device;
8e09f215 3400 struct dasd_block *block;
d07dc5d8 3401 int max_count, open_count, rc;
1da177e4 3402
d07dc5d8 3403 rc = 0;
1da177e4
LT
3404 device = dasd_device_from_cdev(cdev);
3405 if (IS_ERR(device))
3406 return PTR_ERR(device);
d07dc5d8 3407
1da177e4
LT
3408 /*
3409 * We must make sure that this device is currently not in use.
3410 * The open_count is increased for every opener, that includes
3411 * the blkdev_get in dasd_scan_partitions. We are only interested
3412 * in the other openers.
3413 */
8e09f215 3414 if (device->block) {
a806170e
HC
3415 max_count = device->block->bdev ? 0 : -1;
3416 open_count = atomic_read(&device->block->open_count);
8e09f215
SW
3417 if (open_count > max_count) {
3418 if (open_count > 0)
4ce25966
FF
3419 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3420 dev_name(&cdev->dev), open_count);
8e09f215 3421 else
4ce25966
FF
3422 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3423 dev_name(&cdev->dev));
8e09f215
SW
3424 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3425 dasd_put_device(device);
3426 return -EBUSY;
3427 }
1da177e4 3428 }
d07dc5d8
SH
3429
3430 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3431 /*
b4a96015 3432 * safe offline already running
d07dc5d8
SH
3433 * could only be called by normal offline so safe_offline flag
3434 * needs to be removed to run normal offline and kill all I/O
3435 */
3436 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3437 /* Already doing normal offline processing */
3438 dasd_put_device(device);
3439 return -EBUSY;
3440 } else
3441 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3442
3443 } else
3444 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3445 /* Already doing offline processing */
3446 dasd_put_device(device);
3447 return -EBUSY;
3448 }
3449
3450 /*
3451 * if safe_offline called set safe_offline_running flag and
3452 * clear safe_offline so that a call to normal offline
3453 * can overrun safe_offline processing
3454 */
3455 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3456 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3457 /*
3458 * If we want to set the device safe offline all IO operations
3459 * should be finished before continuing the offline process
3460 * so sync bdev first and then wait for our queues to become
3461 * empty
3462 */
3463 /* sync blockdev and partitions */
3464 rc = fsync_bdev(device->block->bdev);
3465 if (rc != 0)
3466 goto interrupted;
3467
3468 /* schedule device tasklet and wait for completion */
3469 dasd_schedule_device_bh(device);
3470 rc = wait_event_interruptible(shutdown_waitq,
3471 _wait_for_empty_queues(device));
3472 if (rc != 0)
3473 goto interrupted;
3474 }
3475
3476 set_bit(DASD_FLAG_OFFLINE, &device->flags);
1da177e4
LT
3477 dasd_set_target_state(device, DASD_STATE_NEW);
3478 /* dasd_delete_device destroys the device reference. */
8e09f215 3479 block = device->block;
1da177e4 3480 dasd_delete_device(device);
8e09f215
SW
3481 /*
3482 * life cycle of block is bound to device, so delete it after
3483 * device was safely removed
3484 */
3485 if (block)
3486 dasd_free_block(block);
1da177e4 3487 return 0;
d07dc5d8
SH
3488
3489interrupted:
3490 /* interrupted by signal */
3491 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3492 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3493 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3494 dasd_put_device(device);
3495 return rc;
1da177e4 3496}
7048a2b5 3497EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
1da177e4 3498
a4d26c6a
SW
3499int dasd_generic_last_path_gone(struct dasd_device *device)
3500{
3501 struct dasd_ccw_req *cqr;
3502
3503 dev_warn(&device->cdev->dev, "No operational channel path is left "
3504 "for the device\n");
3505 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3506 /* First of all call extended error reporting. */
3507 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3508
3509 if (device->state < DASD_STATE_BASIC)
3510 return 0;
3511 /* Device is active. We want to keep it. */
3512 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3513 if ((cqr->status == DASD_CQR_IN_IO) ||
3514 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3515 cqr->status = DASD_CQR_QUEUED;
3516 cqr->retries++;
3517 }
3518 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3519 dasd_device_clear_timer(device);
3520 dasd_schedule_device_bh(device);
3521 return 1;
3522}
3523EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3524
3525int dasd_generic_path_operational(struct dasd_device *device)
3526{
3527 dev_info(&device->cdev->dev, "A channel path to the device has become "
3528 "operational\n");
3529 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3530 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3531 if (device->stopped & DASD_UNRESUMED_PM) {
3532 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3533 dasd_restore_device(device);
3534 return 1;
3535 }
3536 dasd_schedule_device_bh(device);
3537 if (device->block)
3538 dasd_schedule_block_bh(device->block);
931a3dce
SH
3539
3540 if (!device->stopped)
3541 wake_up(&generic_waitq);
3542
a4d26c6a
SW
3543 return 1;
3544}
3545EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3546
8e09f215 3547int dasd_generic_notify(struct ccw_device *cdev, int event)
1da177e4
LT
3548{
3549 struct dasd_device *device;
1da177e4
LT
3550 int ret;
3551
91c36919 3552 device = dasd_device_from_cdev_locked(cdev);
1da177e4
LT
3553 if (IS_ERR(device))
3554 return 0;
1da177e4
LT
3555 ret = 0;
3556 switch (event) {
3557 case CIO_GONE:
47593bfa 3558 case CIO_BOXED:
1da177e4 3559 case CIO_NO_PATH:
a4d26c6a
SW
3560 device->path_data.opm = 0;
3561 device->path_data.ppm = 0;
3562 device->path_data.npm = 0;
3563 ret = dasd_generic_last_path_gone(device);
1da177e4
LT
3564 break;
3565 case CIO_OPER:
1da177e4 3566 ret = 1;
a4d26c6a
SW
3567 if (device->path_data.opm)
3568 ret = dasd_generic_path_operational(device);
1da177e4
LT
3569 break;
3570 }
1da177e4
LT
3571 dasd_put_device(device);
3572 return ret;
3573}
7048a2b5 3574EXPORT_SYMBOL_GPL(dasd_generic_notify);
1da177e4 3575
a4d26c6a
SW
3576void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3577{
3578 int chp;
3579 __u8 oldopm, eventlpm;
3580 struct dasd_device *device;
3581
3582 device = dasd_device_from_cdev_locked(cdev);
3583 if (IS_ERR(device))
3584 return;
3585 for (chp = 0; chp < 8; chp++) {
3586 eventlpm = 0x80 >> chp;
3587 if (path_event[chp] & PE_PATH_GONE) {
3588 oldopm = device->path_data.opm;
3589 device->path_data.opm &= ~eventlpm;
3590 device->path_data.ppm &= ~eventlpm;
3591 device->path_data.npm &= ~eventlpm;
8b811bae
SW
3592 if (oldopm && !device->path_data.opm) {
3593 dev_warn(&device->cdev->dev,
3594 "No verified channel paths remain "
3595 "for the device\n");
3596 DBF_DEV_EVENT(DBF_WARNING, device,
3597 "%s", "last verified path gone");
3598 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3599 dasd_device_set_stop_bits(device,
3600 DASD_STOPPED_DC_WAIT);
3601 }
a4d26c6a
SW
3602 }
3603 if (path_event[chp] & PE_PATH_AVAILABLE) {
3604 device->path_data.opm &= ~eventlpm;
3605 device->path_data.ppm &= ~eventlpm;
3606 device->path_data.npm &= ~eventlpm;
3607 device->path_data.tbvpm |= eventlpm;
3608 dasd_schedule_device_bh(device);
3609 }
f1633031 3610 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
12d7b107
SH
3611 if (!(device->path_data.opm & eventlpm) &&
3612 !(device->path_data.tbvpm & eventlpm)) {
3613 /*
3614 * we can not establish a pathgroup on an
3615 * unavailable path, so trigger a path
3616 * verification first
3617 */
3618 device->path_data.tbvpm |= eventlpm;
3619 dasd_schedule_device_bh(device);
3620 }
f1633031
SH
3621 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3622 "Pathgroup re-established\n");
3623 if (device->discipline->kick_validate)
3624 device->discipline->kick_validate(device);
3625 }
a4d26c6a
SW
3626 }
3627 dasd_put_device(device);
3628}
3629EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3630
3631int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3632{
3633 if (!device->path_data.opm && lpm) {
3634 device->path_data.opm = lpm;
3635 dasd_generic_path_operational(device);
3636 } else
3637 device->path_data.opm |= lpm;
3638 return 0;
3639}
3640EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3641
3642
d41dd122
SH
3643int dasd_generic_pm_freeze(struct ccw_device *cdev)
3644{
c5576876
SH
3645 struct dasd_device *device = dasd_device_from_cdev(cdev);
3646 struct list_head freeze_queue;
d41dd122 3647 struct dasd_ccw_req *cqr, *n;
c5576876 3648 struct dasd_ccw_req *refers;
d41dd122 3649 int rc;
d41dd122
SH
3650
3651 if (IS_ERR(device))
3652 return PTR_ERR(device);
6f272b9c 3653
c8d1c0ff
SH
3654 /* mark device as suspended */
3655 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3656
6f272b9c
SH
3657 if (device->discipline->freeze)
3658 rc = device->discipline->freeze(device);
3659
d41dd122 3660 /* disallow new I/O */
eb6e199b 3661 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
c5576876
SH
3662
3663 /* clear active requests and requeue them to block layer if possible */
d41dd122
SH
3664 INIT_LIST_HEAD(&freeze_queue);
3665 spin_lock_irq(get_ccwdev_lock(cdev));
3666 rc = 0;
3667 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3668 /* Check status and move request to flush_queue */
3669 if (cqr->status == DASD_CQR_IN_IO) {
3670 rc = device->discipline->term_IO(cqr);
3671 if (rc) {
3672 /* unable to terminate requeust */
3673 dev_err(&device->cdev->dev,
3674 "Unable to terminate request %p "
3675 "on suspend\n", cqr);
3676 spin_unlock_irq(get_ccwdev_lock(cdev));
3677 dasd_put_device(device);
3678 return rc;
3679 }
3680 }
3681 list_move_tail(&cqr->devlist, &freeze_queue);
3682 }
d41dd122
SH
3683 spin_unlock_irq(get_ccwdev_lock(cdev));
3684
3685 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3686 wait_event(dasd_flush_wq,
3687 (cqr->status != DASD_CQR_CLEAR_PENDING));
3688 if (cqr->status == DASD_CQR_CLEARED)
3689 cqr->status = DASD_CQR_QUEUED;
c5576876
SH
3690
3691 /* requeue requests to blocklayer will only work for
3692 block device requests */
3693 if (_dasd_requeue_request(cqr))
3694 continue;
3695
3696 /* remove requests from device and block queue */
3697 list_del_init(&cqr->devlist);
3698 while (cqr->refers != NULL) {
3699 refers = cqr->refers;
3700 /* remove the request from the block queue */
3701 list_del(&cqr->blocklist);
3702 /* free the finished erp request */
3703 dasd_free_erp_request(cqr, cqr->memdev);
3704 cqr = refers;
3705 }
3706 if (cqr->block)
3707 list_del_init(&cqr->blocklist);
3708 cqr->block->base->discipline->free_cp(
3709 cqr, (struct request *) cqr->callback_data);
d41dd122 3710 }
d41dd122 3711
c5576876
SH
3712 /*
3713 * if requests remain then they are internal request
3714 * and go back to the device queue
3715 */
3716 if (!list_empty(&freeze_queue)) {
3717 /* move freeze_queue to start of the ccw_queue */
3718 spin_lock_irq(get_ccwdev_lock(cdev));
3719 list_splice_tail(&freeze_queue, &device->ccw_queue);
3720 spin_unlock_irq(get_ccwdev_lock(cdev));
3721 }
d41dd122
SH
3722 dasd_put_device(device);
3723 return rc;
3724}
3725EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3726
3727int dasd_generic_restore_device(struct ccw_device *cdev)
3728{
3729 struct dasd_device *device = dasd_device_from_cdev(cdev);
3730 int rc = 0;
3731
3732 if (IS_ERR(device))
3733 return PTR_ERR(device);
3734
e6125fba 3735 /* allow new IO again */
eb6e199b
SW
3736 dasd_device_remove_stop_bits(device,
3737 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
e6125fba 3738
d41dd122 3739 dasd_schedule_device_bh(device);
d41dd122 3740
eb6e199b
SW
3741 /*
3742 * call discipline restore function
3743 * if device is stopped do nothing e.g. for disconnected devices
3744 */
3745 if (device->discipline->restore && !(device->stopped))
d41dd122 3746 rc = device->discipline->restore(device);
eb6e199b 3747 if (rc || device->stopped)
e6125fba
SH
3748 /*
3749 * if the resume failed for the DASD we put it in
3750 * an UNRESUMED stop state
3751 */
3752 device->stopped |= DASD_UNRESUMED_PM;
d41dd122 3753
6fca97a9
SH
3754 if (device->block)
3755 dasd_schedule_block_bh(device->block);
3756
c8d1c0ff 3757 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
d41dd122 3758 dasd_put_device(device);
e6125fba 3759 return 0;
d41dd122
SH
3760}
3761EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3762
763968e2
HC
3763static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3764 void *rdc_buffer,
3765 int rdc_buffer_size,
68b781fe 3766 int magic)
17283b56
CH
3767{
3768 struct dasd_ccw_req *cqr;
3769 struct ccw1 *ccw;
d9fa9441 3770 unsigned long *idaw;
17283b56
CH
3771
3772 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3773
3774 if (IS_ERR(cqr)) {
fc19f381
SH
3775 /* internal error 13 - Allocating the RDC request failed*/
3776 dev_err(&device->cdev->dev,
3777 "An error occurred in the DASD device driver, "
3778 "reason=%s\n", "13");
17283b56
CH
3779 return cqr;
3780 }
3781
3782 ccw = cqr->cpaddr;
3783 ccw->cmd_code = CCW_CMD_RDC;
d9fa9441
SH
3784 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3785 idaw = (unsigned long *) (cqr->data);
3786 ccw->cda = (__u32)(addr_t) idaw;
3787 ccw->flags = CCW_FLAG_IDA;
3788 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3789 } else {
3790 ccw->cda = (__u32)(addr_t) rdc_buffer;
3791 ccw->flags = 0;
3792 }
17283b56 3793
d9fa9441 3794 ccw->count = rdc_buffer_size;
8e09f215
SW
3795 cqr->startdev = device;
3796 cqr->memdev = device;
17283b56 3797 cqr->expires = 10*HZ;
eb6e199b 3798 cqr->retries = 256;
1aae0560 3799 cqr->buildclk = get_tod_clock();
17283b56
CH
3800 cqr->status = DASD_CQR_FILLED;
3801 return cqr;
3802}
3803
3804
68b781fe 3805int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
92636b15 3806 void *rdc_buffer, int rdc_buffer_size)
17283b56
CH
3807{
3808 int ret;
3809 struct dasd_ccw_req *cqr;
3810
92636b15 3811 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
17283b56
CH
3812 magic);
3813 if (IS_ERR(cqr))
3814 return PTR_ERR(cqr);
3815
3816 ret = dasd_sleep_on(cqr);
8e09f215 3817 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
3818 return ret;
3819}
aaff0f64 3820EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
20c64468 3821
f3eb5384
SW
3822/*
3823 * In command mode and transport mode we need to look for sense
3824 * data in different places. The sense data itself is allways
3825 * an array of 32 bytes, so we can unify the sense data access
3826 * for both modes.
3827 */
3828char *dasd_get_sense(struct irb *irb)
3829{
3830 struct tsb *tsb = NULL;
3831 char *sense = NULL;
3832
3833 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3834 if (irb->scsw.tm.tcw)
3835 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3836 irb->scsw.tm.tcw);
3837 if (tsb && tsb->length == 64 && tsb->flags)
3838 switch (tsb->flags & 0x07) {
3839 case 1: /* tsa_iostat */
3840 sense = tsb->tsa.iostat.sense;
3841 break;
3842 case 2: /* tsa_ddpc */
3843 sense = tsb->tsa.ddpc.sense;
3844 break;
3845 default:
3846 /* currently we don't use interrogate data */
3847 break;
3848 }
3849 } else if (irb->esw.esw0.erw.cons) {
3850 sense = irb->ecw;
3851 }
3852 return sense;
3853}
3854EXPORT_SYMBOL_GPL(dasd_get_sense);
3855
4679e893
SH
3856void dasd_generic_shutdown(struct ccw_device *cdev)
3857{
3858 struct dasd_device *device;
3859
3860 device = dasd_device_from_cdev(cdev);
3861 if (IS_ERR(device))
3862 return;
3863
3864 if (device->block)
3865 dasd_schedule_block_bh(device->block);
3866
3867 dasd_schedule_device_bh(device);
3868
3869 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3870}
3871EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3872
8e09f215 3873static int __init dasd_init(void)
1da177e4
LT
3874{
3875 int rc;
3876
3877 init_waitqueue_head(&dasd_init_waitq);
8f61701b 3878 init_waitqueue_head(&dasd_flush_wq);
c80ee724 3879 init_waitqueue_head(&generic_waitq);
4679e893 3880 init_waitqueue_head(&shutdown_waitq);
1da177e4
LT
3881
3882 /* register 'common' DASD debug area, used for all DBF_XXX calls */
361f494d 3883 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
1da177e4
LT
3884 if (dasd_debug_area == NULL) {
3885 rc = -ENOMEM;
3886 goto failed;
3887 }
3888 debug_register_view(dasd_debug_area, &debug_sprintf_view);
b0035f12 3889 debug_set_level(dasd_debug_area, DBF_WARNING);
1da177e4
LT
3890
3891 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3892
3893 dasd_diag_discipline_pointer = NULL;
3894
4fa52aa7
SW
3895 dasd_statistics_createroot();
3896
1da177e4
LT
3897 rc = dasd_devmap_init();
3898 if (rc)
3899 goto failed;
3900 rc = dasd_gendisk_init();
3901 if (rc)
3902 goto failed;
3903 rc = dasd_parse();
3904 if (rc)
3905 goto failed;
20c64468
SW
3906 rc = dasd_eer_init();
3907 if (rc)
3908 goto failed;
1da177e4
LT
3909#ifdef CONFIG_PROC_FS
3910 rc = dasd_proc_init();
3911 if (rc)
3912 goto failed;
3913#endif
3914
3915 return 0;
3916failed:
fc19f381 3917 pr_info("The DASD device driver could not be initialized\n");
1da177e4
LT
3918 dasd_exit();
3919 return rc;
3920}
3921
3922module_init(dasd_init);
3923module_exit(dasd_exit);