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