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