]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/block/dasd.c
block: introduce new block status code type
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / block / dasd.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
d41dd122 7 * Copyright IBM Corp. 1999, 2009
1da177e4
LT
8 */
9
fc19f381
SH
10#define KMSG_COMPONENT "dasd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
1da177e4
LT
13#include <linux/kmod.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/ctype.h>
17#include <linux/major.h>
18#include <linux/slab.h>
a885c8c4 19#include <linux/hdreg.h>
f3445a1a 20#include <linux/async.h>
9eb25122 21#include <linux/mutex.h>
4fa52aa7
SW
22#include <linux/debugfs.h>
23#include <linux/seq_file.h>
e4258d55 24#include <linux/vmalloc.h>
1da177e4
LT
25
26#include <asm/ccwdev.h>
27#include <asm/ebcdic.h>
28#include <asm/idals.h>
f3eb5384 29#include <asm/itcw.h>
33b62a30 30#include <asm/diag.h>
1da177e4
LT
31
32/* This is ugly... */
33#define PRINTK_HEADER "dasd:"
34
35#include "dasd_int.h"
36/*
37 * SECTION: Constant definitions to be used within this file
38 */
39#define DASD_CHANQ_MAX_SIZE 4
40
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 *);
a521b048 72static void do_requeue_requests(struct work_struct *);
8e09f215 73static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
48cae885
SW
74static void dasd_device_timeout(unsigned long);
75static void dasd_block_timeout(unsigned long);
eb6e199b 76static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
4fa52aa7
SW
77static void dasd_profile_init(struct dasd_profile *, struct dentry *);
78static void dasd_profile_exit(struct dasd_profile *);
5a3b7b11
SH
79static void dasd_hosts_init(struct dentry *, struct dasd_device *);
80static void dasd_hosts_exit(struct dasd_device *);
1da177e4
LT
81
82/*
83 * SECTION: Operations on the device structure.
84 */
85static wait_queue_head_t dasd_init_waitq;
8f61701b 86static wait_queue_head_t dasd_flush_wq;
c80ee724 87static wait_queue_head_t generic_waitq;
4679e893 88static wait_queue_head_t shutdown_waitq;
1da177e4
LT
89
90/*
91 * Allocate memory for a new device structure.
92 */
8e09f215 93struct dasd_device *dasd_alloc_device(void)
1da177e4
LT
94{
95 struct dasd_device *device;
96
8e09f215
SW
97 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
98 if (!device)
1da177e4 99 return ERR_PTR(-ENOMEM);
1da177e4
LT
100
101 /* Get two pages for normal block device operations. */
102 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
8e09f215 103 if (!device->ccw_mem) {
1da177e4
LT
104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107 /* Get one page for error recovery. */
108 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
8e09f215 109 if (!device->erp_mem) {
1da177e4
LT
110 free_pages((unsigned long) device->ccw_mem, 1);
111 kfree(device);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
116 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
117 spin_lock_init(&device->mem_lock);
8e09f215 118 atomic_set(&device->tasklet_scheduled, 0);
138c014d 119 tasklet_init(&device->tasklet,
8e09f215 120 (void (*)(unsigned long)) dasd_device_tasklet,
1da177e4
LT
121 (unsigned long) device);
122 INIT_LIST_HEAD(&device->ccw_queue);
123 init_timer(&device->timer);
48cae885
SW
124 device->timer.function = dasd_device_timeout;
125 device->timer.data = (unsigned long) device;
4927b3f7 126 INIT_WORK(&device->kick_work, do_kick_device);
d41dd122 127 INIT_WORK(&device->restore_device, do_restore_device);
501183f2 128 INIT_WORK(&device->reload_device, do_reload_device);
a521b048 129 INIT_WORK(&device->requeue_requests, do_requeue_requests);
1da177e4
LT
130 device->state = DASD_STATE_NEW;
131 device->target = DASD_STATE_NEW;
9eb25122 132 mutex_init(&device->state_mutex);
4fa52aa7 133 spin_lock_init(&device->profile.lock);
1da177e4
LT
134 return device;
135}
136
137/*
138 * Free memory of a device structure.
139 */
8e09f215 140void dasd_free_device(struct dasd_device *device)
1da177e4 141{
17fd682e 142 kfree(device->private);
1da177e4
LT
143 free_page((unsigned long) device->erp_mem);
144 free_pages((unsigned long) device->ccw_mem, 1);
145 kfree(device);
146}
147
8e09f215
SW
148/*
149 * Allocate memory for a new device structure.
150 */
151struct dasd_block *dasd_alloc_block(void)
152{
153 struct dasd_block *block;
154
155 block = kzalloc(sizeof(*block), GFP_ATOMIC);
156 if (!block)
157 return ERR_PTR(-ENOMEM);
158 /* open_count = 0 means device online but not in use */
159 atomic_set(&block->open_count, -1);
160
161 spin_lock_init(&block->request_queue_lock);
162 atomic_set(&block->tasklet_scheduled, 0);
163 tasklet_init(&block->tasklet,
164 (void (*)(unsigned long)) dasd_block_tasklet,
165 (unsigned long) block);
166 INIT_LIST_HEAD(&block->ccw_queue);
167 spin_lock_init(&block->queue_lock);
168 init_timer(&block->timer);
48cae885
SW
169 block->timer.function = dasd_block_timeout;
170 block->timer.data = (unsigned long) block;
4fa52aa7 171 spin_lock_init(&block->profile.lock);
8e09f215
SW
172
173 return block;
174}
7048a2b5 175EXPORT_SYMBOL_GPL(dasd_alloc_block);
8e09f215
SW
176
177/*
178 * Free memory of a device structure.
179 */
180void dasd_free_block(struct dasd_block *block)
181{
182 kfree(block);
183}
7048a2b5 184EXPORT_SYMBOL_GPL(dasd_free_block);
8e09f215 185
1da177e4
LT
186/*
187 * Make a new device known to the system.
188 */
8e09f215 189static int dasd_state_new_to_known(struct dasd_device *device)
1da177e4
LT
190{
191 int rc;
192
193 /*
138c014d 194 * As long as the device is not in state DASD_STATE_NEW we want to
1da177e4
LT
195 * keep the reference count > 0.
196 */
197 dasd_get_device(device);
198
8e09f215
SW
199 if (device->block) {
200 rc = dasd_alloc_queue(device->block);
201 if (rc) {
202 dasd_put_device(device);
203 return rc;
204 }
1da177e4 205 }
1da177e4
LT
206 device->state = DASD_STATE_KNOWN;
207 return 0;
208}
209
210/*
211 * Let the system forget about a device.
212 */
8e09f215 213static int dasd_state_known_to_new(struct dasd_device *device)
1da177e4 214{
20c64468
SW
215 /* Disable extended error reporting for this device. */
216 dasd_eer_disable(device);
1da177e4
LT
217 device->state = DASD_STATE_NEW;
218
8e09f215
SW
219 if (device->block)
220 dasd_free_queue(device->block);
1da177e4
LT
221
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
8f61701b 224 return 0;
1da177e4
LT
225}
226
4fa52aa7
SW
227static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
229{
230 struct dentry *pde;
231
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
238}
239
1da177e4
LT
240/*
241 * Request the irq line for the device.
242 */
8e09f215 243static int dasd_state_known_to_basic(struct dasd_device *device)
1da177e4 244{
4fa52aa7 245 struct dasd_block *block = device->block;
d42e1712 246 int rc = 0;
1da177e4
LT
247
248 /* Allocate and register gendisk structure. */
4fa52aa7
SW
249 if (block) {
250 rc = dasd_gendisk_alloc(block);
8e09f215
SW
251 if (rc)
252 return rc;
4fa52aa7
SW
253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
259 }
260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
5a3b7b11 264 dasd_hosts_init(device->debugfs_dentry, device);
4fa52aa7 265
1da177e4 266 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
fc19f381 267 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
8e09f215 268 8 * sizeof(long));
1da177e4 269 debug_register_view(device->debug_area, &debug_sprintf_view);
b0035f12 270 debug_set_level(device->debug_area, DBF_WARNING);
1da177e4
LT
271 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
272
273 device->state = DASD_STATE_BASIC;
d42e1712
SH
274
275 return rc;
1da177e4
LT
276}
277
278/*
279 * Release the irq line for the device. Terminate any running i/o.
280 */
8e09f215 281static int dasd_state_basic_to_known(struct dasd_device *device)
1da177e4 282{
8f61701b 283 int rc;
d42e1712 284
daa991bf
SH
285 if (device->discipline->basic_to_known) {
286 rc = device->discipline->basic_to_known(device);
287 if (rc)
288 return rc;
289 }
290
8e09f215 291 if (device->block) {
4fa52aa7 292 dasd_profile_exit(&device->block->profile);
d7309aaa 293 debugfs_remove(device->block->debugfs_dentry);
8e09f215
SW
294 dasd_gendisk_free(device->block);
295 dasd_block_clear_timer(device->block);
296 }
297 rc = dasd_flush_device_queue(device);
8f61701b
HH
298 if (rc)
299 return rc;
8e09f215 300 dasd_device_clear_timer(device);
4fa52aa7 301 dasd_profile_exit(&device->profile);
5a3b7b11 302 dasd_hosts_exit(device);
d7309aaa 303 debugfs_remove(device->debugfs_dentry);
1da177e4
LT
304 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
305 if (device->debug_area != NULL) {
306 debug_unregister(device->debug_area);
307 device->debug_area = NULL;
308 }
309 device->state = DASD_STATE_KNOWN;
8f61701b 310 return 0;
1da177e4
LT
311}
312
313/*
314 * Do the initial analysis. The do_analysis function may return
315 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
316 * until the discipline decides to continue the startup sequence
317 * by calling the function dasd_change_state. The eckd disciplines
318 * uses this to start a ccw that detects the format. The completion
319 * interrupt for this detection ccw uses the kernel event daemon to
320 * trigger the call to dasd_change_state. All this is done in the
321 * discipline code, see dasd_eckd.c.
90f0094d
HH
322 * After the analysis ccw is done (do_analysis returned 0) the block
323 * device is setup.
324 * In case the analysis returns an error, the device setup is stopped
325 * (a fake disk was already added to allow formatting).
1da177e4 326 */
8e09f215 327static int dasd_state_basic_to_ready(struct dasd_device *device)
1da177e4
LT
328{
329 int rc;
8e09f215 330 struct dasd_block *block;
eed5c4b1 331 struct gendisk *disk;
1da177e4
LT
332
333 rc = 0;
8e09f215 334 block = device->block;
90f0094d 335 /* make disk known with correct capacity */
8e09f215
SW
336 if (block) {
337 if (block->base->discipline->do_analysis != NULL)
338 rc = block->base->discipline->do_analysis(block);
339 if (rc) {
d42e1712 340 if (rc != -EAGAIN) {
8e09f215 341 device->state = DASD_STATE_UNFMT;
eed5c4b1
SH
342 disk = device->block->gdp;
343 kobject_uevent(&disk_to_dev(disk)->kobj,
344 KOBJ_CHANGE);
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
5a3b7b11
SH
1153static int dasd_hosts_show(struct seq_file *m, void *v)
1154{
1155 struct dasd_device *device;
1156 int rc = -EOPNOTSUPP;
1157
1158 device = m->private;
1159 dasd_get_device(device);
1160
1161 if (device->discipline->hosts_print)
1162 rc = device->discipline->hosts_print(device, m);
1163
1164 dasd_put_device(device);
1165 return rc;
1166}
1167
1168static int dasd_hosts_open(struct inode *inode, struct file *file)
1169{
1170 struct dasd_device *device = inode->i_private;
1171
1172 return single_open(file, dasd_hosts_show, device);
1173}
1174
1175static const struct file_operations dasd_hosts_fops = {
1176 .owner = THIS_MODULE,
1177 .open = dasd_hosts_open,
1178 .read = seq_read,
1179 .llseek = seq_lseek,
1180 .release = single_release,
1181};
1182
1183static void dasd_hosts_exit(struct dasd_device *device)
1184{
1185 debugfs_remove(device->hosts_dentry);
1186 device->hosts_dentry = NULL;
1187}
1188
1189static void dasd_hosts_init(struct dentry *base_dentry,
1190 struct dasd_device *device)
1191{
1192 struct dentry *pde;
1193 umode_t mode;
1194
1195 if (!base_dentry)
1196 return;
1197
1198 mode = S_IRUSR | S_IFREG;
1199 pde = debugfs_create_file("host_access_list", mode, base_dentry,
1200 device, &dasd_hosts_fops);
1201 if (pde && !IS_ERR(pde))
1202 device->hosts_dentry = pde;
1203}
1204
1da177e4
LT
1205/*
1206 * Allocate memory for a channel program with 'cplength' channel
1207 * command words and 'datasize' additional space. There are two
1208 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1209 * memory and 2) dasd_smalloc_request uses the static ccw memory
1210 * that gets allocated for each device.
1211 */
68b781fe 1212struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
8e09f215
SW
1213 int datasize,
1214 struct dasd_device *device)
1da177e4
LT
1215{
1216 struct dasd_ccw_req *cqr;
1217
1218 /* Sanity checks */
68b781fe 1219 BUG_ON(datasize > PAGE_SIZE ||
7ac1e877 1220 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1da177e4 1221
88abaab4 1222 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1da177e4
LT
1223 if (cqr == NULL)
1224 return ERR_PTR(-ENOMEM);
1da177e4
LT
1225 cqr->cpaddr = NULL;
1226 if (cplength > 0) {
88abaab4 1227 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1da177e4
LT
1228 GFP_ATOMIC | GFP_DMA);
1229 if (cqr->cpaddr == NULL) {
1230 kfree(cqr);
1231 return ERR_PTR(-ENOMEM);
1232 }
1da177e4
LT
1233 }
1234 cqr->data = NULL;
1235 if (datasize > 0) {
88abaab4 1236 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1da177e4 1237 if (cqr->data == NULL) {
17fd682e 1238 kfree(cqr->cpaddr);
1da177e4
LT
1239 kfree(cqr);
1240 return ERR_PTR(-ENOMEM);
1241 }
1da177e4 1242 }
68b781fe 1243 cqr->magic = magic;
1da177e4
LT
1244 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1245 dasd_get_device(device);
1246 return cqr;
1247}
7048a2b5 1248EXPORT_SYMBOL(dasd_kmalloc_request);
1da177e4 1249
68b781fe 1250struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
8e09f215
SW
1251 int datasize,
1252 struct dasd_device *device)
1da177e4
LT
1253{
1254 unsigned long flags;
1255 struct dasd_ccw_req *cqr;
1256 char *data;
1257 int size;
1258
1da177e4
LT
1259 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1260 if (cplength > 0)
1261 size += cplength * sizeof(struct ccw1);
1262 if (datasize > 0)
1263 size += datasize;
1264 spin_lock_irqsave(&device->mem_lock, flags);
1265 cqr = (struct dasd_ccw_req *)
1266 dasd_alloc_chunk(&device->ccw_chunks, size);
1267 spin_unlock_irqrestore(&device->mem_lock, flags);
1268 if (cqr == NULL)
1269 return ERR_PTR(-ENOMEM);
1270 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1271 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1272 cqr->cpaddr = NULL;
1273 if (cplength > 0) {
1274 cqr->cpaddr = (struct ccw1 *) data;
1275 data += cplength*sizeof(struct ccw1);
1276 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1277 }
1278 cqr->data = NULL;
1279 if (datasize > 0) {
1280 cqr->data = data;
1281 memset(cqr->data, 0, datasize);
1282 }
68b781fe 1283 cqr->magic = magic;
1da177e4
LT
1284 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1285 dasd_get_device(device);
1286 return cqr;
1287}
7048a2b5 1288EXPORT_SYMBOL(dasd_smalloc_request);
1da177e4
LT
1289
1290/*
1291 * Free memory of a channel program. This function needs to free all the
1292 * idal lists that might have been created by dasd_set_cda and the
1293 * struct dasd_ccw_req itself.
1294 */
8e09f215 1295void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4 1296{
1da177e4
LT
1297 struct ccw1 *ccw;
1298
1299 /* Clear any idals used for the request. */
1300 ccw = cqr->cpaddr;
1301 do {
1302 clear_normalized_cda(ccw);
1303 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
17fd682e
JJ
1304 kfree(cqr->cpaddr);
1305 kfree(cqr->data);
1da177e4
LT
1306 kfree(cqr);
1307 dasd_put_device(device);
1308}
7048a2b5 1309EXPORT_SYMBOL(dasd_kfree_request);
1da177e4 1310
8e09f215 1311void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1da177e4
LT
1312{
1313 unsigned long flags;
1314
1315 spin_lock_irqsave(&device->mem_lock, flags);
1316 dasd_free_chunk(&device->ccw_chunks, cqr);
1317 spin_unlock_irqrestore(&device->mem_lock, flags);
1318 dasd_put_device(device);
1319}
7048a2b5 1320EXPORT_SYMBOL(dasd_sfree_request);
1da177e4
LT
1321
1322/*
1323 * Check discipline magic in cqr.
1324 */
8e09f215 1325static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1da177e4
LT
1326{
1327 struct dasd_device *device;
1328
1329 if (cqr == NULL)
1330 return -EINVAL;
8e09f215 1331 device = cqr->startdev;
1da177e4 1332 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
fc19f381 1333 DBF_DEV_EVENT(DBF_WARNING, device,
1da177e4
LT
1334 " dasd_ccw_req 0x%08x magic doesn't match"
1335 " discipline 0x%08x",
1336 cqr->magic,
1337 *(unsigned int *) device->discipline->name);
1338 return -EINVAL;
1339 }
1340 return 0;
1341}
1342
1343/*
1344 * Terminate the current i/o and set the request to clear_pending.
1345 * Timer keeps device runnig.
1346 * ccw_device_clear can fail if the i/o subsystem
1347 * is in a bad mood.
1348 */
8e09f215 1349int dasd_term_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
1350{
1351 struct dasd_device *device;
1352 int retries, rc;
fc19f381 1353 char errorstring[ERRORLENGTH];
1da177e4
LT
1354
1355 /* Check the cqr */
1356 rc = dasd_check_cqr(cqr);
1357 if (rc)
1358 return rc;
1359 retries = 0;
8e09f215 1360 device = (struct dasd_device *) cqr->startdev;
1da177e4
LT
1361 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1362 rc = ccw_device_clear(device->cdev, (long) cqr);
1363 switch (rc) {
1364 case 0: /* termination successful */
8e09f215 1365 cqr->status = DASD_CQR_CLEAR_PENDING;
1aae0560 1366 cqr->stopclk = get_tod_clock();
8f61701b 1367 cqr->starttime = 0;
1da177e4
LT
1368 DBF_DEV_EVENT(DBF_DEBUG, device,
1369 "terminate cqr %p successful",
1370 cqr);
1371 break;
1372 case -ENODEV:
1373 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1374 "device gone, retry");
1375 break;
1376 case -EIO:
1377 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1378 "I/O error, retry");
1379 break;
1380 case -EINVAL:
2c17124b
SH
1381 /*
1382 * device not valid so no I/O could be running
1383 * handle CQR as termination successful
1384 */
1385 cqr->status = DASD_CQR_CLEARED;
1386 cqr->stopclk = get_tod_clock();
1387 cqr->starttime = 0;
1388 /* no retries for invalid devices */
1389 cqr->retries = -1;
1390 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1391 "EINVAL, handle as terminated");
1392 /* fake rc to success */
1393 rc = 0;
1394 break;
1da177e4
LT
1395 case -EBUSY:
1396 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1397 "device busy, retry later");
1398 break;
1399 default:
fc19f381
SH
1400 /* internal error 10 - unknown rc*/
1401 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1402 dev_err(&device->cdev->dev, "An error occurred in the "
1403 "DASD device driver, reason=%s\n", errorstring);
1da177e4
LT
1404 BUG();
1405 break;
1406 }
1407 retries++;
1408 }
8e09f215 1409 dasd_schedule_device_bh(device);
1da177e4
LT
1410 return rc;
1411}
7048a2b5 1412EXPORT_SYMBOL(dasd_term_IO);
1da177e4
LT
1413
1414/*
1415 * Start the i/o. This start_IO can fail if the channel is really busy.
1416 * In that case set up a timer to start the request later.
1417 */
8e09f215 1418int dasd_start_IO(struct dasd_ccw_req *cqr)
1da177e4
LT
1419{
1420 struct dasd_device *device;
1421 int rc;
fc19f381 1422 char errorstring[ERRORLENGTH];
1da177e4
LT
1423
1424 /* Check the cqr */
1425 rc = dasd_check_cqr(cqr);
6cc7f168
SW
1426 if (rc) {
1427 cqr->intrc = rc;
1da177e4 1428 return rc;
6cc7f168 1429 }
8e09f215 1430 device = (struct dasd_device *) cqr->startdev;
5a27e60d
SW
1431 if (((cqr->block &&
1432 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1433 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1434 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1435 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1436 "because of stolen lock", cqr);
1437 cqr->status = DASD_CQR_ERROR;
1438 cqr->intrc = -EPERM;
1439 return -EPERM;
1440 }
1da177e4 1441 if (cqr->retries < 0) {
fc19f381
SH
1442 /* internal error 14 - start_IO run out of retries */
1443 sprintf(errorstring, "14 %p", cqr);
1444 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1445 "device driver, reason=%s\n", errorstring);
8e09f215 1446 cqr->status = DASD_CQR_ERROR;
1da177e4
LT
1447 return -EIO;
1448 }
1aae0560 1449 cqr->startclk = get_tod_clock();
1da177e4
LT
1450 cqr->starttime = jiffies;
1451 cqr->retries--;
a4d26c6a 1452 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
c9346151 1453 cqr->lpm &= dasd_path_get_opm(device);
a4d26c6a 1454 if (!cqr->lpm)
c9346151 1455 cqr->lpm = dasd_path_get_opm(device);
a4d26c6a 1456 }
f3eb5384
SW
1457 if (cqr->cpmode == 1) {
1458 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1459 (long) cqr, cqr->lpm);
1460 } else {
1461 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1462 (long) cqr, cqr->lpm, 0);
1463 }
1da177e4
LT
1464 switch (rc) {
1465 case 0:
1466 cqr->status = DASD_CQR_IN_IO;
1da177e4
LT
1467 break;
1468 case -EBUSY:
a4d26c6a 1469 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
1470 "start_IO: device busy, retry later");
1471 break;
1472 case -ETIMEDOUT:
a4d26c6a 1473 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1da177e4
LT
1474 "start_IO: request timeout, retry later");
1475 break;
1476 case -EACCES:
a4d26c6a
SW
1477 /* -EACCES indicates that the request used only a subset of the
1478 * available paths and all these paths are gone. If the lpm of
1479 * this request was only a subset of the opm (e.g. the ppm) then
1480 * we just do a retry with all available paths.
1481 * If we already use the full opm, something is amiss, and we
1482 * need a full path verification.
1da177e4 1483 */
a4d26c6a
SW
1484 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1485 DBF_DEV_EVENT(DBF_WARNING, device,
1486 "start_IO: selected paths gone (%x)",
1487 cqr->lpm);
c9346151
SH
1488 } else if (cqr->lpm != dasd_path_get_opm(device)) {
1489 cqr->lpm = dasd_path_get_opm(device);
a4d26c6a
SW
1490 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1491 "start_IO: selected paths gone,"
1492 " retry on all paths");
1493 } else {
1494 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1495 "start_IO: all paths in opm gone,"
1496 " do path verification");
1497 dasd_generic_last_path_gone(device);
c9346151
SH
1498 dasd_path_no_path(device);
1499 dasd_path_set_tbvpm(device,
1500 ccw_device_get_path_mask(
1501 device->cdev));
a4d26c6a 1502 }
1da177e4
LT
1503 break;
1504 case -ENODEV:
a4d26c6a 1505 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
f3eb5384
SW
1506 "start_IO: -ENODEV device gone, retry");
1507 break;
1da177e4 1508 case -EIO:
a4d26c6a 1509 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
f3eb5384 1510 "start_IO: -EIO device gone, retry");
1da177e4 1511 break;
d41dd122
SH
1512 case -EINVAL:
1513 /* most likely caused in power management context */
a4d26c6a 1514 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
d41dd122
SH
1515 "start_IO: -EINVAL device currently "
1516 "not accessible");
1517 break;
1da177e4 1518 default:
fc19f381
SH
1519 /* internal error 11 - unknown rc */
1520 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1521 dev_err(&device->cdev->dev,
1522 "An error occurred in the DASD device driver, "
1523 "reason=%s\n", errorstring);
1da177e4
LT
1524 BUG();
1525 break;
1526 }
6cc7f168 1527 cqr->intrc = rc;
1da177e4
LT
1528 return rc;
1529}
7048a2b5 1530EXPORT_SYMBOL(dasd_start_IO);
1da177e4
LT
1531
1532/*
1533 * Timeout function for dasd devices. This is used for different purposes
1534 * 1) missing interrupt handler for normal operation
1535 * 2) delayed start of request where start_IO failed with -EBUSY
1536 * 3) timeout for missing state change interrupts
1537 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1538 * DASD_CQR_QUEUED for 2) and 3).
1539 */
8e09f215 1540static void dasd_device_timeout(unsigned long ptr)
1da177e4
LT
1541{
1542 unsigned long flags;
1543 struct dasd_device *device;
1544
1545 device = (struct dasd_device *) ptr;
1546 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1547 /* re-activate request queue */
eb6e199b 1548 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1da177e4 1549 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215 1550 dasd_schedule_device_bh(device);
1da177e4
LT
1551}
1552
1553/*
1554 * Setup timeout for a device in jiffies.
1555 */
8e09f215 1556void dasd_device_set_timer(struct dasd_device *device, int expires)
1da177e4 1557{
48cae885
SW
1558 if (expires == 0)
1559 del_timer(&device->timer);
1560 else
1561 mod_timer(&device->timer, jiffies + expires);
1da177e4 1562}
7048a2b5 1563EXPORT_SYMBOL(dasd_device_set_timer);
1da177e4
LT
1564
1565/*
1566 * Clear timeout for a device.
1567 */
8e09f215 1568void dasd_device_clear_timer(struct dasd_device *device)
1da177e4 1569{
48cae885 1570 del_timer(&device->timer);
1da177e4 1571}
7048a2b5 1572EXPORT_SYMBOL(dasd_device_clear_timer);
1da177e4 1573
8e09f215
SW
1574static void dasd_handle_killed_request(struct ccw_device *cdev,
1575 unsigned long intparm)
1da177e4
LT
1576{
1577 struct dasd_ccw_req *cqr;
1578 struct dasd_device *device;
1579
f16f5843
SW
1580 if (!intparm)
1581 return;
1da177e4
LT
1582 cqr = (struct dasd_ccw_req *) intparm;
1583 if (cqr->status != DASD_CQR_IN_IO) {
b8ed5dd5
SH
1584 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1585 "invalid status in handle_killed_request: "
1586 "%02x", cqr->status);
1da177e4
LT
1587 return;
1588 }
1589
589c74d5
SH
1590 device = dasd_device_from_cdev_locked(cdev);
1591 if (IS_ERR(device)) {
1592 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1593 "unable to get device from cdev");
1594 return;
1595 }
1596
1597 if (!cqr->startdev ||
1598 device != cqr->startdev ||
1599 strncmp(cqr->startdev->discipline->ebcname,
1600 (char *) &cqr->magic, 4)) {
294001a8
SH
1601 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1602 "invalid device in request");
589c74d5 1603 dasd_put_device(device);
1da177e4
LT
1604 return;
1605 }
1606
1607 /* Schedule request to be retried. */
1608 cqr->status = DASD_CQR_QUEUED;
1609
8e09f215
SW
1610 dasd_device_clear_timer(device);
1611 dasd_schedule_device_bh(device);
1da177e4
LT
1612 dasd_put_device(device);
1613}
1614
8e09f215 1615void dasd_generic_handle_state_change(struct dasd_device *device)
1da177e4 1616{
20c64468
SW
1617 /* First of all start sense subsystem status request. */
1618 dasd_eer_snss(device);
1619
eb6e199b 1620 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
8e09f215
SW
1621 dasd_schedule_device_bh(device);
1622 if (device->block)
1623 dasd_schedule_block_bh(device->block);
1da177e4 1624}
7048a2b5 1625EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
1da177e4 1626
a521b048
SH
1627static int dasd_check_hpf_error(struct irb *irb)
1628{
1629 return (scsw_tm_is_valid_schxs(&irb->scsw) &&
1630 (irb->scsw.tm.sesq == SCSW_SESQ_DEV_NOFCX ||
1631 irb->scsw.tm.sesq == SCSW_SESQ_PATH_NOFCX));
1632}
1633
1da177e4
LT
1634/*
1635 * Interrupt handler for "normal" ssch-io based dasd devices.
1636 */
8e09f215
SW
1637void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1638 struct irb *irb)
1da177e4
LT
1639{
1640 struct dasd_ccw_req *cqr, *next;
1641 struct dasd_device *device;
1642 unsigned long long now;
8fd57520
JH
1643 int nrf_suppressed = 0;
1644 int fp_suppressed = 0;
1645 u8 *sense = NULL;
1da177e4 1646 int expires;
1da177e4 1647
9ba333dc 1648 cqr = (struct dasd_ccw_req *) intparm;
1da177e4
LT
1649 if (IS_ERR(irb)) {
1650 switch (PTR_ERR(irb)) {
1651 case -EIO:
9ba333dc 1652 if (cqr && cqr->status == DASD_CQR_CLEAR_PENDING) {
c9346151 1653 device = cqr->startdev;
9ba333dc
SH
1654 cqr->status = DASD_CQR_CLEARED;
1655 dasd_device_clear_timer(device);
1656 wake_up(&dasd_flush_wq);
1657 dasd_schedule_device_bh(device);
1658 return;
1659 }
1da177e4
LT
1660 break;
1661 case -ETIMEDOUT:
b8ed5dd5
SH
1662 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1663 "request timed out\n", __func__);
1da177e4
LT
1664 break;
1665 default:
b8ed5dd5
SH
1666 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1667 "unknown error %ld\n", __func__,
1668 PTR_ERR(irb));
1da177e4 1669 }
f16f5843 1670 dasd_handle_killed_request(cdev, intparm);
1da177e4
LT
1671 return;
1672 }
1673
1aae0560 1674 now = get_tod_clock();
5a27e60d
SW
1675 /* check for conditions that should be handled immediately */
1676 if (!cqr ||
1677 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1678 scsw_cstat(&irb->scsw) == 0)) {
a5a0061f
SW
1679 if (cqr)
1680 memcpy(&cqr->irb, irb, sizeof(*irb));
a00bfd71 1681 device = dasd_device_from_cdev_locked(cdev);
56b86b61
SH
1682 if (IS_ERR(device))
1683 return;
1684 /* ignore unsolicited interrupts for DIAG discipline */
1685 if (device->discipline == dasd_diag_discipline_pointer) {
1da177e4 1686 dasd_put_device(device);
56b86b61 1687 return;
1da177e4 1688 }
8fd57520
JH
1689
1690 /*
1691 * In some cases 'File Protected' or 'No Record Found' errors
1692 * might be expected and debug log messages for the
1693 * corresponding interrupts shouldn't be written then.
1694 * Check if either of the according suppress bits is set.
1695 */
1696 sense = dasd_get_sense(irb);
1697 if (sense) {
1698 fp_suppressed = (sense[1] & SNS1_FILE_PROTECTED) &&
1699 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags);
1700 nrf_suppressed = (sense[1] & SNS1_NO_REC_FOUND) &&
1701 test_bit(DASD_CQR_SUPPRESS_NRF, &cqr->flags);
1702 }
1703 if (!(fp_suppressed || nrf_suppressed))
1704 device->discipline->dump_sense_dbf(device, irb, "int");
1705
5a27e60d
SW
1706 if (device->features & DASD_FEATURE_ERPLOG)
1707 device->discipline->dump_sense(device, cqr, irb);
1708 device->discipline->check_for_device_change(device, cqr, irb);
56b86b61 1709 dasd_put_device(device);
1da177e4 1710 }
5db8440c
SH
1711
1712 /* check for for attention message */
1713 if (scsw_dstat(&irb->scsw) & DEV_STAT_ATTENTION) {
1714 device = dasd_device_from_cdev_locked(cdev);
2202134e
SH
1715 if (!IS_ERR(device)) {
1716 device->discipline->check_attention(device,
1717 irb->esw.esw1.lpum);
1718 dasd_put_device(device);
1719 }
5db8440c
SH
1720 }
1721
5a27e60d
SW
1722 if (!cqr)
1723 return;
1da177e4 1724
8e09f215
SW
1725 device = (struct dasd_device *) cqr->startdev;
1726 if (!device ||
1da177e4 1727 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
294001a8
SH
1728 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1729 "invalid device in request");
1da177e4
LT
1730 return;
1731 }
1732
1733 /* Check for clear pending */
8e09f215 1734 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
f3eb5384 1735 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
8e09f215
SW
1736 cqr->status = DASD_CQR_CLEARED;
1737 dasd_device_clear_timer(device);
8f61701b 1738 wake_up(&dasd_flush_wq);
8e09f215 1739 dasd_schedule_device_bh(device);
1da177e4
LT
1740 return;
1741 }
1742
f3eb5384 1743 /* check status - the request might have been killed by dyn detach */
1da177e4 1744 if (cqr->status != DASD_CQR_IN_IO) {
fc19f381
SH
1745 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1746 "status %02x", dev_name(&cdev->dev), cqr->status);
1da177e4
LT
1747 return;
1748 }
fc19f381 1749
8e09f215 1750 next = NULL;
1da177e4 1751 expires = 0;
f3eb5384
SW
1752 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1753 scsw_cstat(&irb->scsw) == 0) {
8e09f215
SW
1754 /* request was completed successfully */
1755 cqr->status = DASD_CQR_SUCCESS;
1da177e4
LT
1756 cqr->stopclk = now;
1757 /* Start first request on queue if possible -> fast_io. */
8e09f215
SW
1758 if (cqr->devlist.next != &device->ccw_queue) {
1759 next = list_entry(cqr->devlist.next,
1760 struct dasd_ccw_req, devlist);
1da177e4 1761 }
8e09f215 1762 } else { /* error */
a521b048
SH
1763 /* check for HPF error
1764 * call discipline function to requeue all requests
1765 * and disable HPF accordingly
1766 */
1767 if (cqr->cpmode && dasd_check_hpf_error(irb) &&
1768 device->discipline->handle_hpf_error)
1769 device->discipline->handle_hpf_error(device, irb);
6c5f57c7
SH
1770 /*
1771 * If we don't want complex ERP for this request, then just
1772 * reset this and retry it in the fastpath
8e09f215 1773 */
6c5f57c7 1774 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
8e09f215 1775 cqr->retries > 0) {
c9346151 1776 if (cqr->lpm == dasd_path_get_opm(device))
fc19f381
SH
1777 DBF_DEV_EVENT(DBF_DEBUG, device,
1778 "default ERP in fastpath "
1779 "(%i retries left)",
1780 cqr->retries);
a4d26c6a 1781 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
c9346151 1782 cqr->lpm = dasd_path_get_opm(device);
8e09f215
SW
1783 cqr->status = DASD_CQR_QUEUED;
1784 next = cqr;
1785 } else
1da177e4 1786 cqr->status = DASD_CQR_ERROR;
8e09f215
SW
1787 }
1788 if (next && (next->status == DASD_CQR_QUEUED) &&
1789 (!device->stopped)) {
1790 if (device->discipline->start_IO(next) == 0)
1791 expires = next->expires;
1da177e4
LT
1792 }
1793 if (expires != 0)
8e09f215 1794 dasd_device_set_timer(device, expires);
1da177e4 1795 else
8e09f215
SW
1796 dasd_device_clear_timer(device);
1797 dasd_schedule_device_bh(device);
1da177e4 1798}
7048a2b5 1799EXPORT_SYMBOL(dasd_int_handler);
1da177e4 1800
a23ed009
SH
1801enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1802{
1803 struct dasd_device *device;
1804
1805 device = dasd_device_from_cdev_locked(cdev);
1806
1807 if (IS_ERR(device))
1808 goto out;
1809 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1810 device->state != device->target ||
5a27e60d 1811 !device->discipline->check_for_device_change){
a23ed009
SH
1812 dasd_put_device(device);
1813 goto out;
1814 }
5a27e60d
SW
1815 if (device->discipline->dump_sense_dbf)
1816 device->discipline->dump_sense_dbf(device, irb, "uc");
1817 device->discipline->check_for_device_change(device, NULL, irb);
a23ed009
SH
1818 dasd_put_device(device);
1819out:
1820 return UC_TODO_RETRY;
1821}
1822EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1823
1da177e4 1824/*
8e09f215
SW
1825 * If we have an error on a dasd_block layer request then we cancel
1826 * and return all further requests from the same dasd_block as well.
1da177e4 1827 */
8e09f215
SW
1828static void __dasd_device_recovery(struct dasd_device *device,
1829 struct dasd_ccw_req *ref_cqr)
1da177e4 1830{
8e09f215
SW
1831 struct list_head *l, *n;
1832 struct dasd_ccw_req *cqr;
1da177e4 1833
8e09f215
SW
1834 /*
1835 * only requeue request that came from the dasd_block layer
1836 */
1837 if (!ref_cqr->block)
1838 return;
1da177e4 1839
8e09f215
SW
1840 list_for_each_safe(l, n, &device->ccw_queue) {
1841 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1842 if (cqr->status == DASD_CQR_QUEUED &&
1843 ref_cqr->block == cqr->block) {
1844 cqr->status = DASD_CQR_CLEARED;
1845 }
1846 }
1847};
1da177e4
LT
1848
1849/*
8e09f215
SW
1850 * Remove those ccw requests from the queue that need to be returned
1851 * to the upper layer.
1da177e4 1852 */
8e09f215
SW
1853static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1854 struct list_head *final_queue)
1da177e4
LT
1855{
1856 struct list_head *l, *n;
1857 struct dasd_ccw_req *cqr;
1da177e4 1858
1da177e4
LT
1859 /* Process request with final status. */
1860 list_for_each_safe(l, n, &device->ccw_queue) {
8e09f215
SW
1861 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1862
1fbdb8be 1863 /* Skip any non-final request. */
8e09f215
SW
1864 if (cqr->status == DASD_CQR_QUEUED ||
1865 cqr->status == DASD_CQR_IN_IO ||
1866 cqr->status == DASD_CQR_CLEAR_PENDING)
1fbdb8be 1867 continue;
1da177e4 1868 if (cqr->status == DASD_CQR_ERROR) {
8e09f215 1869 __dasd_device_recovery(device, cqr);
20c64468 1870 }
1da177e4 1871 /* Rechain finished requests to final queue */
8e09f215 1872 list_move_tail(&cqr->devlist, final_queue);
1da177e4
LT
1873 }
1874}
1875
1da177e4 1876/*
8e09f215
SW
1877 * the cqrs from the final queue are returned to the upper layer
1878 * by setting a dasd_block state and calling the callback function
1da177e4 1879 */
8e09f215
SW
1880static void __dasd_device_process_final_queue(struct dasd_device *device,
1881 struct list_head *final_queue)
1da177e4 1882{
8e09f215 1883 struct list_head *l, *n;
1da177e4 1884 struct dasd_ccw_req *cqr;
03513bcc 1885 struct dasd_block *block;
c80ee724
SH
1886 void (*callback)(struct dasd_ccw_req *, void *data);
1887 void *callback_data;
fc19f381 1888 char errorstring[ERRORLENGTH];
f24acd45 1889
8e09f215
SW
1890 list_for_each_safe(l, n, final_queue) {
1891 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1892 list_del_init(&cqr->devlist);
03513bcc 1893 block = cqr->block;
c80ee724
SH
1894 callback = cqr->callback;
1895 callback_data = cqr->callback_data;
03513bcc
SW
1896 if (block)
1897 spin_lock_bh(&block->queue_lock);
8e09f215
SW
1898 switch (cqr->status) {
1899 case DASD_CQR_SUCCESS:
1900 cqr->status = DASD_CQR_DONE;
1901 break;
1902 case DASD_CQR_ERROR:
1903 cqr->status = DASD_CQR_NEED_ERP;
1904 break;
1905 case DASD_CQR_CLEARED:
1906 cqr->status = DASD_CQR_TERMINATED;
1907 break;
1908 default:
fc19f381
SH
1909 /* internal error 12 - wrong cqr status*/
1910 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1911 dev_err(&device->cdev->dev,
1912 "An error occurred in the DASD device driver, "
1913 "reason=%s\n", errorstring);
8e09f215 1914 BUG();
1da177e4 1915 }
8e09f215 1916 if (cqr->callback != NULL)
c80ee724 1917 (callback)(cqr, callback_data);
03513bcc
SW
1918 if (block)
1919 spin_unlock_bh(&block->queue_lock);
1da177e4
LT
1920 }
1921}
1922
1923/*
1924 * Take a look at the first request on the ccw queue and check
1925 * if it reached its expire time. If so, terminate the IO.
1926 */
8e09f215 1927static void __dasd_device_check_expire(struct dasd_device *device)
1da177e4
LT
1928{
1929 struct dasd_ccw_req *cqr;
1930
1931 if (list_empty(&device->ccw_queue))
1932 return;
8e09f215 1933 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
29145a6c
HH
1934 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1935 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
d07dc5d8
SH
1936 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1937 /*
1938 * IO in safe offline processing should not
1939 * run out of retries
1940 */
1941 cqr->retries++;
1942 }
29145a6c
HH
1943 if (device->discipline->term_IO(cqr) != 0) {
1944 /* Hmpf, try again in 5 sec */
fc19f381 1945 dev_err(&device->cdev->dev,
625c94df 1946 "cqr %p timed out (%lus) but cannot be "
fc19f381
SH
1947 "ended, retrying in 5 s\n",
1948 cqr, (cqr->expires/HZ));
7dc1da9f
SH
1949 cqr->expires += 5*HZ;
1950 dasd_device_set_timer(device, 5*HZ);
29145a6c 1951 } else {
fc19f381 1952 dev_err(&device->cdev->dev,
625c94df 1953 "cqr %p timed out (%lus), %i retries "
fc19f381
SH
1954 "remaining\n", cqr, (cqr->expires/HZ),
1955 cqr->retries);
1da177e4
LT
1956 }
1957 }
1958}
1959
f81a49d1
SH
1960/*
1961 * return 1 when device is not eligible for IO
1962 */
1963static int __dasd_device_is_unusable(struct dasd_device *device,
1964 struct dasd_ccw_req *cqr)
1965{
1966 int mask = ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM);
1967
1968 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1969 /* dasd is being set offline. */
1970 return 1;
1971 }
1972 if (device->stopped) {
1973 if (device->stopped & mask) {
1974 /* stopped and CQR will not change that. */
1975 return 1;
1976 }
1977 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1978 /* CQR is not able to change device to
1979 * operational. */
1980 return 1;
1981 }
1982 /* CQR required to get device operational. */
1983 }
1984 return 0;
1985}
1986
1da177e4
LT
1987/*
1988 * Take a look at the first request on the ccw queue and check
1989 * if it needs to be started.
1990 */
8e09f215 1991static void __dasd_device_start_head(struct dasd_device *device)
1da177e4
LT
1992{
1993 struct dasd_ccw_req *cqr;
1994 int rc;
1995
1996 if (list_empty(&device->ccw_queue))
1997 return;
8e09f215 1998 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
25ee4cf8
PO
1999 if (cqr->status != DASD_CQR_QUEUED)
2000 return;
f81a49d1
SH
2001 /* if device is not usable return request to upper layer */
2002 if (__dasd_device_is_unusable(device, cqr)) {
a4d26c6a 2003 cqr->intrc = -EAGAIN;
8e09f215
SW
2004 cqr->status = DASD_CQR_CLEARED;
2005 dasd_schedule_device_bh(device);
25ee4cf8 2006 return;
1c01b8a5 2007 }
25ee4cf8
PO
2008
2009 rc = device->discipline->start_IO(cqr);
2010 if (rc == 0)
8e09f215 2011 dasd_device_set_timer(device, cqr->expires);
25ee4cf8 2012 else if (rc == -EACCES) {
8e09f215 2013 dasd_schedule_device_bh(device);
25ee4cf8
PO
2014 } else
2015 /* Hmpf, try again in 1/2 sec */
8e09f215 2016 dasd_device_set_timer(device, 50);
8f61701b
HH
2017}
2018
a4d26c6a
SW
2019static void __dasd_device_check_path_events(struct dasd_device *device)
2020{
2021 int rc;
2022
c9346151
SH
2023 if (!dasd_path_get_tbvpm(device))
2024 return;
2025
2026 if (device->stopped &
2027 ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
2028 return;
2029 rc = device->discipline->verify_path(device,
2030 dasd_path_get_tbvpm(device));
2031 if (rc)
2032 dasd_device_set_timer(device, 50);
2033 else
2034 dasd_path_clear_all_verify(device);
a4d26c6a
SW
2035};
2036
1da177e4 2037/*
8e09f215
SW
2038 * Go through all request on the dasd_device request queue,
2039 * terminate them on the cdev if necessary, and return them to the
2040 * submitting layer via callback.
2041 * Note:
2042 * Make sure that all 'submitting layers' still exist when
2043 * this function is called!. In other words, when 'device' is a base
2044 * device then all block layer requests must have been removed before
2045 * via dasd_flush_block_queue.
1da177e4 2046 */
8e09f215 2047int dasd_flush_device_queue(struct dasd_device *device)
1da177e4 2048{
8e09f215
SW
2049 struct dasd_ccw_req *cqr, *n;
2050 int rc;
1da177e4 2051 struct list_head flush_queue;
1da177e4
LT
2052
2053 INIT_LIST_HEAD(&flush_queue);
2054 spin_lock_irq(get_ccwdev_lock(device->cdev));
8f61701b 2055 rc = 0;
8e09f215 2056 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
8f61701b
HH
2057 /* Check status and move request to flush_queue */
2058 switch (cqr->status) {
2059 case DASD_CQR_IN_IO:
2060 rc = device->discipline->term_IO(cqr);
2061 if (rc) {
2062 /* unable to terminate requeust */
fc19f381
SH
2063 dev_err(&device->cdev->dev,
2064 "Flushing the DASD request queue "
2065 "failed for request %p\n", cqr);
8f61701b
HH
2066 /* stop flush processing */
2067 goto finished;
2068 }
2069 break;
2070 case DASD_CQR_QUEUED:
1aae0560 2071 cqr->stopclk = get_tod_clock();
8e09f215 2072 cqr->status = DASD_CQR_CLEARED;
8f61701b 2073 break;
8e09f215 2074 default: /* no need to modify the others */
8f61701b
HH
2075 break;
2076 }
8e09f215 2077 list_move_tail(&cqr->devlist, &flush_queue);
8f61701b 2078 }
8f61701b
HH
2079finished:
2080 spin_unlock_irq(get_ccwdev_lock(device->cdev));
8e09f215
SW
2081 /*
2082 * After this point all requests must be in state CLEAR_PENDING,
2083 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
2084 * one of the others.
2085 */
2086 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
2087 wait_event(dasd_flush_wq,
2088 (cqr->status != DASD_CQR_CLEAR_PENDING));
2089 /*
2090 * Now set each request back to TERMINATED, DONE or NEED_ERP
2091 * and call the callback function of flushed requests
2092 */
2093 __dasd_device_process_final_queue(device, &flush_queue);
8f61701b 2094 return rc;
1da177e4 2095}
7048a2b5 2096EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
1da177e4
LT
2097
2098/*
2099 * Acquire the device lock and process queues for the device.
2100 */
8e09f215 2101static void dasd_device_tasklet(struct dasd_device *device)
1da177e4
LT
2102{
2103 struct list_head final_queue;
1da177e4
LT
2104
2105 atomic_set (&device->tasklet_scheduled, 0);
2106 INIT_LIST_HEAD(&final_queue);
2107 spin_lock_irq(get_ccwdev_lock(device->cdev));
2108 /* Check expire time of first request on the ccw queue. */
8e09f215
SW
2109 __dasd_device_check_expire(device);
2110 /* find final requests on ccw queue */
2111 __dasd_device_process_ccw_queue(device, &final_queue);
a4d26c6a 2112 __dasd_device_check_path_events(device);
1da177e4
LT
2113 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2114 /* Now call the callback function of requests with final status */
8e09f215
SW
2115 __dasd_device_process_final_queue(device, &final_queue);
2116 spin_lock_irq(get_ccwdev_lock(device->cdev));
1da177e4 2117 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
2118 __dasd_device_start_head(device);
2119 spin_unlock_irq(get_ccwdev_lock(device->cdev));
4679e893
SH
2120 if (waitqueue_active(&shutdown_waitq))
2121 wake_up(&shutdown_waitq);
1da177e4
LT
2122 dasd_put_device(device);
2123}
2124
2125/*
2126 * Schedules a call to dasd_tasklet over the device tasklet.
2127 */
8e09f215 2128void dasd_schedule_device_bh(struct dasd_device *device)
1da177e4
LT
2129{
2130 /* Protect against rescheduling. */
973bd993 2131 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1da177e4
LT
2132 return;
2133 dasd_get_device(device);
2134 tasklet_hi_schedule(&device->tasklet);
2135}
7048a2b5 2136EXPORT_SYMBOL(dasd_schedule_device_bh);
1da177e4 2137
eb6e199b
SW
2138void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2139{
2140 device->stopped |= bits;
2141}
2142EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2143
2144void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2145{
2146 device->stopped &= ~bits;
2147 if (!device->stopped)
2148 wake_up(&generic_waitq);
2149}
2150EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2151
1da177e4 2152/*
8e09f215
SW
2153 * Queue a request to the head of the device ccw_queue.
2154 * Start the I/O if possible.
1da177e4 2155 */
8e09f215 2156void dasd_add_request_head(struct dasd_ccw_req *cqr)
1da177e4
LT
2157{
2158 struct dasd_device *device;
2159 unsigned long flags;
2160
8e09f215 2161 device = cqr->startdev;
1da177e4 2162 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
2163 cqr->status = DASD_CQR_QUEUED;
2164 list_add(&cqr->devlist, &device->ccw_queue);
1da177e4 2165 /* let the bh start the request to keep them in order */
8e09f215 2166 dasd_schedule_device_bh(device);
1da177e4
LT
2167 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2168}
7048a2b5 2169EXPORT_SYMBOL(dasd_add_request_head);
1da177e4
LT
2170
2171/*
8e09f215
SW
2172 * Queue a request to the tail of the device ccw_queue.
2173 * Start the I/O if possible.
1da177e4 2174 */
8e09f215 2175void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1da177e4
LT
2176{
2177 struct dasd_device *device;
2178 unsigned long flags;
2179
8e09f215 2180 device = cqr->startdev;
1da177e4 2181 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
2182 cqr->status = DASD_CQR_QUEUED;
2183 list_add_tail(&cqr->devlist, &device->ccw_queue);
1da177e4 2184 /* let the bh start the request to keep them in order */
8e09f215 2185 dasd_schedule_device_bh(device);
1da177e4
LT
2186 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2187}
7048a2b5 2188EXPORT_SYMBOL(dasd_add_request_tail);
1da177e4
LT
2189
2190/*
8e09f215 2191 * Wakeup helper for the 'sleep_on' functions.
1da177e4 2192 */
5915a873 2193void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1da177e4 2194{
1c1e093c
SW
2195 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2196 cqr->callback_data = DASD_SLEEPON_END_TAG;
2197 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2198 wake_up(&generic_waitq);
1da177e4 2199}
5915a873 2200EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
1da177e4 2201
8e09f215 2202static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1da177e4
LT
2203{
2204 struct dasd_device *device;
2205 int rc;
2206
8e09f215 2207 device = cqr->startdev;
1da177e4 2208 spin_lock_irq(get_ccwdev_lock(device->cdev));
1c1e093c 2209 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
1da177e4
LT
2210 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2211 return rc;
2212}
2213
2214/*
eb6e199b 2215 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1da177e4 2216 */
eb6e199b 2217static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1da177e4 2218{
1da177e4 2219 struct dasd_device *device;
eb6e199b 2220 dasd_erp_fn_t erp_fn;
138c014d 2221
eb6e199b
SW
2222 if (cqr->status == DASD_CQR_FILLED)
2223 return 0;
8e09f215 2224 device = cqr->startdev;
eb6e199b
SW
2225 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2226 if (cqr->status == DASD_CQR_TERMINATED) {
2227 device->discipline->handle_terminated_request(cqr);
2228 return 1;
2229 }
2230 if (cqr->status == DASD_CQR_NEED_ERP) {
2231 erp_fn = device->discipline->erp_action(cqr);
2232 erp_fn(cqr);
2233 return 1;
2234 }
2235 if (cqr->status == DASD_CQR_FAILED)
2236 dasd_log_sense(cqr, &cqr->irb);
2237 if (cqr->refers) {
2238 __dasd_process_erp(device, cqr);
2239 return 1;
2240 }
2241 }
2242 return 0;
2243}
138c014d 2244
eb6e199b
SW
2245static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2246{
2247 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2248 if (cqr->refers) /* erp is not done yet */
2249 return 1;
2250 return ((cqr->status != DASD_CQR_DONE) &&
2251 (cqr->status != DASD_CQR_FAILED));
2252 } else
2253 return (cqr->status == DASD_CQR_FILLED);
2254}
138c014d 2255
eb6e199b
SW
2256static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2257{
2258 struct dasd_device *device;
2259 int rc;
2260 struct list_head ccw_queue;
2261 struct dasd_ccw_req *cqr;
2262
2263 INIT_LIST_HEAD(&ccw_queue);
2264 maincqr->status = DASD_CQR_FILLED;
2265 device = maincqr->startdev;
2266 list_add(&maincqr->blocklist, &ccw_queue);
2267 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2268 cqr = list_first_entry(&ccw_queue,
2269 struct dasd_ccw_req, blocklist)) {
2270
2271 if (__dasd_sleep_on_erp(cqr))
2272 continue;
2273 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2274 continue;
5a27e60d
SW
2275 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2276 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2277 cqr->status = DASD_CQR_FAILED;
2278 cqr->intrc = -EPERM;
2279 continue;
2280 }
eb6e199b
SW
2281 /* Non-temporary stop condition will trigger fail fast */
2282 if (device->stopped & ~DASD_STOPPED_PENDING &&
2283 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2284 (!dasd_eer_enabled(device))) {
2285 cqr->status = DASD_CQR_FAILED;
d1ffc1f8 2286 cqr->intrc = -ENOLINK;
eb6e199b
SW
2287 continue;
2288 }
a9f6273f
SH
2289 /*
2290 * Don't try to start requests if device is in
2291 * offline processing, it might wait forever
2292 */
2293 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2294 cqr->status = DASD_CQR_FAILED;
2295 cqr->intrc = -ENODEV;
2296 continue;
2297 }
df3044f1
SH
2298 /*
2299 * Don't try to start requests if device is stopped
2300 * except path verification requests
2301 */
2302 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
2303 if (interruptible) {
2304 rc = wait_event_interruptible(
2305 generic_waitq, !(device->stopped));
2306 if (rc == -ERESTARTSYS) {
2307 cqr->status = DASD_CQR_FAILED;
2308 maincqr->intrc = rc;
2309 continue;
2310 }
2311 } else
2312 wait_event(generic_waitq, !(device->stopped));
2313 }
5915a873
SH
2314 if (!cqr->callback)
2315 cqr->callback = dasd_wakeup_cb;
2316
1c1e093c 2317 cqr->callback_data = DASD_SLEEPON_START_TAG;
eb6e199b
SW
2318 dasd_add_request_tail(cqr);
2319 if (interruptible) {
2320 rc = wait_event_interruptible(
2321 generic_waitq, _wait_for_wakeup(cqr));
2322 if (rc == -ERESTARTSYS) {
2323 dasd_cancel_req(cqr);
2324 /* wait (non-interruptible) for final status */
2325 wait_event(generic_waitq,
2326 _wait_for_wakeup(cqr));
2327 cqr->status = DASD_CQR_FAILED;
2328 maincqr->intrc = rc;
2329 continue;
2330 }
2331 } else
2332 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2333 }
2334
1aae0560 2335 maincqr->endclk = get_tod_clock();
eb6e199b
SW
2336 if ((maincqr->status != DASD_CQR_DONE) &&
2337 (maincqr->intrc != -ERESTARTSYS))
2338 dasd_log_sense(maincqr, &maincqr->irb);
2339 if (maincqr->status == DASD_CQR_DONE)
6cc7f168 2340 rc = 0;
eb6e199b
SW
2341 else if (maincqr->intrc)
2342 rc = maincqr->intrc;
6cc7f168
SW
2343 else
2344 rc = -EIO;
1da177e4
LT
2345 return rc;
2346}
2347
d42e1712
SH
2348static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2349{
2350 struct dasd_ccw_req *cqr;
2351
2352 list_for_each_entry(cqr, ccw_queue, blocklist) {
2353 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2354 return 0;
2355 }
2356
2357 return 1;
2358}
2359
2360static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2361{
2362 struct dasd_device *device;
d42e1712 2363 struct dasd_ccw_req *cqr, *n;
8fd57520 2364 u8 *sense = NULL;
362ce84f 2365 int rc;
d42e1712
SH
2366
2367retry:
2368 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2369 device = cqr->startdev;
2370 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2371 continue;
2372
2373 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2374 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2375 cqr->status = DASD_CQR_FAILED;
2376 cqr->intrc = -EPERM;
2377 continue;
2378 }
2379 /*Non-temporary stop condition will trigger fail fast*/
2380 if (device->stopped & ~DASD_STOPPED_PENDING &&
2381 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2382 !dasd_eer_enabled(device)) {
2383 cqr->status = DASD_CQR_FAILED;
2384 cqr->intrc = -EAGAIN;
2385 continue;
2386 }
2387
2388 /*Don't try to start requests if device is stopped*/
2389 if (interruptible) {
2390 rc = wait_event_interruptible(
2391 generic_waitq, !device->stopped);
2392 if (rc == -ERESTARTSYS) {
2393 cqr->status = DASD_CQR_FAILED;
2394 cqr->intrc = rc;
2395 continue;
2396 }
2397 } else
2398 wait_event(generic_waitq, !(device->stopped));
2399
2400 if (!cqr->callback)
2401 cqr->callback = dasd_wakeup_cb;
2402 cqr->callback_data = DASD_SLEEPON_START_TAG;
2403 dasd_add_request_tail(cqr);
2404 }
2405
2406 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2407
2408 rc = 0;
2409 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
8fd57520
JH
2410 /*
2411 * In some cases the 'File Protected' or 'Incorrect Length'
2412 * error might be expected and error recovery would be
2413 * unnecessary in these cases. Check if the according suppress
2414 * bit is set.
2415 */
2416 sense = dasd_get_sense(&cqr->irb);
2417 if (sense && sense[1] & SNS1_FILE_PROTECTED &&
2418 test_bit(DASD_CQR_SUPPRESS_FP, &cqr->flags))
2419 continue;
2420 if (scsw_cstat(&cqr->irb.scsw) == 0x40 &&
2421 test_bit(DASD_CQR_SUPPRESS_IL, &cqr->flags))
2422 continue;
2423
29b8dd9d
SH
2424 /*
2425 * for alias devices simplify error recovery and
2426 * return to upper layer
362ce84f 2427 * do not skip ERP requests
29b8dd9d 2428 */
362ce84f 2429 if (cqr->startdev != cqr->basedev && !cqr->refers &&
29b8dd9d
SH
2430 (cqr->status == DASD_CQR_TERMINATED ||
2431 cqr->status == DASD_CQR_NEED_ERP))
2432 return -EAGAIN;
362ce84f
SH
2433
2434 /* normal recovery for basedev IO */
590aeedd
SH
2435 if (__dasd_sleep_on_erp(cqr))
2436 /* handle erp first */
362ce84f 2437 goto retry;
d42e1712 2438 }
362ce84f 2439
d42e1712
SH
2440 return 0;
2441}
2442
eb6e199b
SW
2443/*
2444 * Queue a request to the tail of the device ccw_queue and wait for
2445 * it's completion.
2446 */
2447int dasd_sleep_on(struct dasd_ccw_req *cqr)
2448{
2449 return _dasd_sleep_on(cqr, 0);
2450}
7048a2b5 2451EXPORT_SYMBOL(dasd_sleep_on);
eb6e199b 2452
d42e1712
SH
2453/*
2454 * Start requests from a ccw_queue and wait for their completion.
2455 */
2456int dasd_sleep_on_queue(struct list_head *ccw_queue)
2457{
2458 return _dasd_sleep_on_queue(ccw_queue, 0);
2459}
2460EXPORT_SYMBOL(dasd_sleep_on_queue);
2461
1da177e4 2462/*
8e09f215
SW
2463 * Queue a request to the tail of the device ccw_queue and wait
2464 * interruptible for it's completion.
1da177e4 2465 */
8e09f215 2466int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1da177e4 2467{
eb6e199b 2468 return _dasd_sleep_on(cqr, 1);
1da177e4 2469}
7048a2b5 2470EXPORT_SYMBOL(dasd_sleep_on_interruptible);
1da177e4
LT
2471
2472/*
2473 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2474 * for eckd devices) the currently running request has to be terminated
2475 * and be put back to status queued, before the special request is added
2476 * to the head of the queue. Then the special request is waited on normally.
2477 */
8e09f215 2478static inline int _dasd_term_running_cqr(struct dasd_device *device)
1da177e4
LT
2479{
2480 struct dasd_ccw_req *cqr;
aade6c0d 2481 int rc;
1da177e4
LT
2482
2483 if (list_empty(&device->ccw_queue))
2484 return 0;
8e09f215 2485 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
aade6c0d
SH
2486 rc = device->discipline->term_IO(cqr);
2487 if (!rc)
2488 /*
2489 * CQR terminated because a more important request is pending.
2490 * Undo decreasing of retry counter because this is
2491 * not an error case.
2492 */
2493 cqr->retries++;
2494 return rc;
1da177e4
LT
2495}
2496
8e09f215 2497int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1da177e4 2498{
1da177e4
LT
2499 struct dasd_device *device;
2500 int rc;
138c014d 2501
8e09f215 2502 device = cqr->startdev;
5a27e60d
SW
2503 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2504 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2505 cqr->status = DASD_CQR_FAILED;
2506 cqr->intrc = -EPERM;
2507 return -EIO;
2508 }
1da177e4
LT
2509 spin_lock_irq(get_ccwdev_lock(device->cdev));
2510 rc = _dasd_term_running_cqr(device);
2511 if (rc) {
2512 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2513 return rc;
2514 }
1da177e4 2515 cqr->callback = dasd_wakeup_cb;
1c1e093c 2516 cqr->callback_data = DASD_SLEEPON_START_TAG;
1da177e4 2517 cqr->status = DASD_CQR_QUEUED;
214b8ffc
SH
2518 /*
2519 * add new request as second
2520 * first the terminated cqr needs to be finished
2521 */
2522 list_add(&cqr->devlist, device->ccw_queue.next);
138c014d 2523
1da177e4 2524 /* let the bh start the request to keep them in order */
8e09f215 2525 dasd_schedule_device_bh(device);
138c014d 2526
1da177e4
LT
2527 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2528
c80ee724 2529 wait_event(generic_waitq, _wait_for_wakeup(cqr));
138c014d 2530
6cc7f168
SW
2531 if (cqr->status == DASD_CQR_DONE)
2532 rc = 0;
2533 else if (cqr->intrc)
2534 rc = cqr->intrc;
2535 else
2536 rc = -EIO;
0e003b70
SH
2537
2538 /* kick tasklets */
2539 dasd_schedule_device_bh(device);
2540 if (device->block)
2541 dasd_schedule_block_bh(device->block);
2542
1da177e4
LT
2543 return rc;
2544}
7048a2b5 2545EXPORT_SYMBOL(dasd_sleep_on_immediatly);
1da177e4
LT
2546
2547/*
2548 * Cancels a request that was started with dasd_sleep_on_req.
2549 * This is useful to timeout requests. The request will be
2550 * terminated if it is currently in i/o.
b99a946d 2551 * Returns 0 if request termination was successful
8e09f215
SW
2552 * negative error code if termination failed
2553 * Cancellation of a request is an asynchronous operation! The calling
2554 * function has to wait until the request is properly returned via callback.
1da177e4 2555 */
8e09f215 2556int dasd_cancel_req(struct dasd_ccw_req *cqr)
1da177e4 2557{
8e09f215 2558 struct dasd_device *device = cqr->startdev;
1da177e4
LT
2559 unsigned long flags;
2560 int rc;
2561
2562 rc = 0;
2563 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2564 switch (cqr->status) {
2565 case DASD_CQR_QUEUED:
8e09f215
SW
2566 /* request was not started - just set to cleared */
2567 cqr->status = DASD_CQR_CLEARED;
931a3dce
SH
2568 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
2569 cqr->callback_data = DASD_SLEEPON_END_TAG;
1da177e4
LT
2570 break;
2571 case DASD_CQR_IN_IO:
2572 /* request in IO - terminate IO and release again */
8e09f215
SW
2573 rc = device->discipline->term_IO(cqr);
2574 if (rc) {
fc19f381
SH
2575 dev_err(&device->cdev->dev,
2576 "Cancelling request %p failed with rc=%d\n",
2577 cqr, rc);
8e09f215 2578 } else {
1aae0560 2579 cqr->stopclk = get_tod_clock();
8e09f215 2580 }
1da177e4 2581 break;
8e09f215 2582 default: /* already finished or clear pending - do nothing */
1da177e4 2583 break;
8e09f215
SW
2584 }
2585 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2586 dasd_schedule_device_bh(device);
2587 return rc;
2588}
7048a2b5 2589EXPORT_SYMBOL(dasd_cancel_req);
8e09f215 2590
8e09f215
SW
2591/*
2592 * SECTION: Operations of the dasd_block layer.
2593 */
2594
2595/*
2596 * Timeout function for dasd_block. This is used when the block layer
2597 * is waiting for something that may not come reliably, (e.g. a state
2598 * change interrupt)
2599 */
2600static void dasd_block_timeout(unsigned long ptr)
2601{
2602 unsigned long flags;
2603 struct dasd_block *block;
2604
2605 block = (struct dasd_block *) ptr;
2606 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2607 /* re-activate request queue */
eb6e199b 2608 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
8e09f215
SW
2609 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2610 dasd_schedule_block_bh(block);
2611}
2612
2613/*
2614 * Setup timeout for a dasd_block in jiffies.
2615 */
2616void dasd_block_set_timer(struct dasd_block *block, int expires)
2617{
48cae885
SW
2618 if (expires == 0)
2619 del_timer(&block->timer);
2620 else
2621 mod_timer(&block->timer, jiffies + expires);
8e09f215 2622}
7048a2b5 2623EXPORT_SYMBOL(dasd_block_set_timer);
8e09f215
SW
2624
2625/*
2626 * Clear timeout for a dasd_block.
2627 */
2628void dasd_block_clear_timer(struct dasd_block *block)
2629{
48cae885 2630 del_timer(&block->timer);
8e09f215 2631}
7048a2b5 2632EXPORT_SYMBOL(dasd_block_clear_timer);
8e09f215 2633
8e09f215
SW
2634/*
2635 * Process finished error recovery ccw.
2636 */
eb6e199b
SW
2637static void __dasd_process_erp(struct dasd_device *device,
2638 struct dasd_ccw_req *cqr)
8e09f215
SW
2639{
2640 dasd_erp_fn_t erp_fn;
8e09f215
SW
2641
2642 if (cqr->status == DASD_CQR_DONE)
2643 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2644 else
fc19f381 2645 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
8e09f215
SW
2646 erp_fn = device->discipline->erp_postaction(cqr);
2647 erp_fn(cqr);
2648}
1da177e4 2649
8e09f215
SW
2650/*
2651 * Fetch requests from the block device queue.
2652 */
2653static void __dasd_process_request_queue(struct dasd_block *block)
2654{
2655 struct request_queue *queue;
2656 struct request *req;
2657 struct dasd_ccw_req *cqr;
2658 struct dasd_device *basedev;
2659 unsigned long flags;
2660 queue = block->request_queue;
2661 basedev = block->base;
2662 /* No queue ? Then there is nothing to do. */
2663 if (queue == NULL)
2664 return;
2665
2666 /*
2667 * We requeue request from the block device queue to the ccw
2668 * queue only in two states. In state DASD_STATE_READY the
2669 * partition detection is done and we need to requeue requests
2670 * for that. State DASD_STATE_ONLINE is normal block device
2671 * operation.
2672 */
97f604b0
SW
2673 if (basedev->state < DASD_STATE_READY) {
2674 while ((req = blk_fetch_request(block->request_queue)))
2a842aca 2675 __blk_end_request_all(req, BLK_STS_IOERR);
8e09f215 2676 return;
97f604b0 2677 }
a3147a7b 2678
c6fc7b6f
SH
2679 /*
2680 * if device is stopped do not fetch new requests
2681 * except failfast is active which will let requests fail
2682 * immediately in __dasd_block_start_head()
2683 */
2684 if (basedev->stopped && !(basedev->features & DASD_FEATURE_FAILFAST))
a3147a7b
SH
2685 return;
2686
8e09f215 2687 /* Now we try to fetch requests from the request queue */
7eaceacc 2688 while ((req = blk_peek_request(queue))) {
8e09f215
SW
2689 if (basedev->features & DASD_FEATURE_READONLY &&
2690 rq_data_dir(req) == WRITE) {
2691 DBF_DEV_EVENT(DBF_ERR, basedev,
2692 "Rejecting write request %p",
2693 req);
9934c8c0 2694 blk_start_request(req);
2a842aca 2695 __blk_end_request_all(req, BLK_STS_IOERR);
8e09f215
SW
2696 continue;
2697 }
5ea34a01
HR
2698 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2699 (basedev->features & DASD_FEATURE_FAILFAST ||
2700 blk_noretry_request(req))) {
2701 DBF_DEV_EVENT(DBF_ERR, basedev,
2702 "Rejecting failfast request %p",
2703 req);
2704 blk_start_request(req);
2a842aca 2705 __blk_end_request_all(req, BLK_STS_TIMEOUT);
5ea34a01
HR
2706 continue;
2707 }
8e09f215
SW
2708 cqr = basedev->discipline->build_cp(basedev, block, req);
2709 if (IS_ERR(cqr)) {
2710 if (PTR_ERR(cqr) == -EBUSY)
2711 break; /* normal end condition */
2712 if (PTR_ERR(cqr) == -ENOMEM)
2713 break; /* terminate request queue loop */
2714 if (PTR_ERR(cqr) == -EAGAIN) {
2715 /*
2716 * The current request cannot be build right
2717 * now, we have to try later. If this request
2718 * is the head-of-queue we stop the device
2719 * for 1/2 second.
2720 */
2721 if (!list_empty(&block->ccw_queue))
2722 break;
eb6e199b
SW
2723 spin_lock_irqsave(
2724 get_ccwdev_lock(basedev->cdev), flags);
2725 dasd_device_set_stop_bits(basedev,
2726 DASD_STOPPED_PENDING);
2727 spin_unlock_irqrestore(
2728 get_ccwdev_lock(basedev->cdev), flags);
8e09f215
SW
2729 dasd_block_set_timer(block, HZ/2);
2730 break;
2731 }
2732 DBF_DEV_EVENT(DBF_ERR, basedev,
2733 "CCW creation failed (rc=%ld) "
2734 "on request %p",
2735 PTR_ERR(cqr), req);
9934c8c0 2736 blk_start_request(req);
2a842aca 2737 __blk_end_request_all(req, BLK_STS_IOERR);
8e09f215
SW
2738 continue;
2739 }
2740 /*
2741 * Note: callback is set to dasd_return_cqr_cb in
2742 * __dasd_block_start_head to cover erp requests as well
2743 */
2744 cqr->callback_data = (void *) req;
2745 cqr->status = DASD_CQR_FILLED;
a2ace466 2746 req->completion_data = cqr;
9934c8c0 2747 blk_start_request(req);
8e09f215 2748 list_add_tail(&cqr->blocklist, &block->ccw_queue);
a2ace466 2749 INIT_LIST_HEAD(&cqr->devlist);
8e09f215
SW
2750 dasd_profile_start(block, cqr, req);
2751 }
2752}
2753
2754static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2755{
2756 struct request *req;
2757 int status;
2a842aca 2758 blk_status_t error = BLK_STS_OK;
8e09f215
SW
2759
2760 req = (struct request *) cqr->callback_data;
2761 dasd_profile_end(cqr->block, cqr, req);
2a842aca 2762
fe6b8e76 2763 status = cqr->block->base->discipline->free_cp(cqr, req);
d1ffc1f8 2764 if (status < 0)
2a842aca 2765 error = errno_to_blk_status(status);
d1ffc1f8 2766 else if (status == 0) {
2a842aca
CH
2767 switch (cqr->intrc) {
2768 case -EPERM:
2769 error = BLK_STS_NEXUS;
2770 break;
2771 case -ENOLINK:
2772 error = BLK_STS_TRANSPORT;
2773 break;
2774 case -ETIMEDOUT:
2775 error = BLK_STS_TIMEOUT;
2776 break;
2777 default:
2778 error = BLK_STS_IOERR;
2779 break;
2780 }
d1ffc1f8 2781 }
40cbbb78 2782 __blk_end_request_all(req, error);
8e09f215
SW
2783}
2784
2785/*
2786 * Process ccw request queue.
2787 */
2788static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2789 struct list_head *final_queue)
2790{
2791 struct list_head *l, *n;
2792 struct dasd_ccw_req *cqr;
2793 dasd_erp_fn_t erp_fn;
2794 unsigned long flags;
2795 struct dasd_device *base = block->base;
2796
2797restart:
2798 /* Process request with final status. */
2799 list_for_each_safe(l, n, &block->ccw_queue) {
2800 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2801 if (cqr->status != DASD_CQR_DONE &&
2802 cqr->status != DASD_CQR_FAILED &&
2803 cqr->status != DASD_CQR_NEED_ERP &&
2804 cqr->status != DASD_CQR_TERMINATED)
2805 continue;
2806
2807 if (cqr->status == DASD_CQR_TERMINATED) {
2808 base->discipline->handle_terminated_request(cqr);
2809 goto restart;
2810 }
2811
2812 /* Process requests that may be recovered */
2813 if (cqr->status == DASD_CQR_NEED_ERP) {
6c5f57c7 2814 erp_fn = base->discipline->erp_action(cqr);
6a5176c4
SH
2815 if (IS_ERR(erp_fn(cqr)))
2816 continue;
8e09f215
SW
2817 goto restart;
2818 }
2819
a9cffb22
SH
2820 /* log sense for fatal error */
2821 if (cqr->status == DASD_CQR_FAILED) {
2822 dasd_log_sense(cqr, &cqr->irb);
2823 }
2824
8e09f215
SW
2825 /* First of all call extended error reporting. */
2826 if (dasd_eer_enabled(base) &&
2827 cqr->status == DASD_CQR_FAILED) {
2828 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2829
2830 /* restart request */
2831 cqr->status = DASD_CQR_FILLED;
2832 cqr->retries = 255;
2833 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
eb6e199b 2834 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
8e09f215
SW
2835 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2836 flags);
2837 goto restart;
2838 }
2839
2840 /* Process finished ERP request. */
2841 if (cqr->refers) {
eb6e199b 2842 __dasd_process_erp(base, cqr);
8e09f215
SW
2843 goto restart;
2844 }
2845
2846 /* Rechain finished requests to final queue */
1aae0560 2847 cqr->endclk = get_tod_clock();
8e09f215
SW
2848 list_move_tail(&cqr->blocklist, final_queue);
2849 }
2850}
2851
2852static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2853{
2854 dasd_schedule_block_bh(cqr->block);
2855}
2856
2857static void __dasd_block_start_head(struct dasd_block *block)
2858{
2859 struct dasd_ccw_req *cqr;
2860
2861 if (list_empty(&block->ccw_queue))
2862 return;
2863 /* We allways begin with the first requests on the queue, as some
2864 * of previously started requests have to be enqueued on a
2865 * dasd_device again for error recovery.
2866 */
2867 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2868 if (cqr->status != DASD_CQR_FILLED)
2869 continue;
5a27e60d
SW
2870 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2871 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2872 cqr->status = DASD_CQR_FAILED;
2873 cqr->intrc = -EPERM;
2874 dasd_schedule_block_bh(block);
2875 continue;
2876 }
8e09f215
SW
2877 /* Non-temporary stop condition will trigger fail fast */
2878 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2879 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2880 (!dasd_eer_enabled(block->base))) {
2881 cqr->status = DASD_CQR_FAILED;
d1ffc1f8 2882 cqr->intrc = -ENOLINK;
8e09f215
SW
2883 dasd_schedule_block_bh(block);
2884 continue;
2885 }
2886 /* Don't try to start requests if device is stopped */
2887 if (block->base->stopped)
2888 return;
2889
2890 /* just a fail safe check, should not happen */
2891 if (!cqr->startdev)
2892 cqr->startdev = block->base;
2893
2894 /* make sure that the requests we submit find their way back */
2895 cqr->callback = dasd_return_cqr_cb;
2896
2897 dasd_add_request_tail(cqr);
2898 }
2899}
2900
2901/*
2902 * Central dasd_block layer routine. Takes requests from the generic
2903 * block layer request queue, creates ccw requests, enqueues them on
2904 * a dasd_device and processes ccw requests that have been returned.
2905 */
2906static void dasd_block_tasklet(struct dasd_block *block)
2907{
2908 struct list_head final_queue;
2909 struct list_head *l, *n;
2910 struct dasd_ccw_req *cqr;
2911
2912 atomic_set(&block->tasklet_scheduled, 0);
2913 INIT_LIST_HEAD(&final_queue);
2914 spin_lock(&block->queue_lock);
2915 /* Finish off requests on ccw queue */
2916 __dasd_process_block_ccw_queue(block, &final_queue);
2917 spin_unlock(&block->queue_lock);
2918 /* Now call the callback function of requests with final status */
2919 spin_lock_irq(&block->request_queue_lock);
2920 list_for_each_safe(l, n, &final_queue) {
2921 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2922 list_del_init(&cqr->blocklist);
2923 __dasd_cleanup_cqr(cqr);
2924 }
2925 spin_lock(&block->queue_lock);
2926 /* Get new request from the block device request queue */
2927 __dasd_process_request_queue(block);
2928 /* Now check if the head of the ccw queue needs to be started. */
2929 __dasd_block_start_head(block);
2930 spin_unlock(&block->queue_lock);
2931 spin_unlock_irq(&block->request_queue_lock);
4679e893
SH
2932 if (waitqueue_active(&shutdown_waitq))
2933 wake_up(&shutdown_waitq);
8e09f215
SW
2934 dasd_put_device(block->base);
2935}
2936
2937static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2938{
2939 wake_up(&dasd_flush_wq);
2940}
2941
c5576876
SH
2942/*
2943 * Requeue a request back to the block request queue
2944 * only works for block requests
2945 */
2946static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2947{
2948 struct dasd_block *block = cqr->block;
2949 struct request *req;
2950 unsigned long flags;
2951
2952 if (!block)
2953 return -EINVAL;
a521b048 2954 spin_lock_irqsave(&block->request_queue_lock, flags);
c5576876
SH
2955 req = (struct request *) cqr->callback_data;
2956 blk_requeue_request(block->request_queue, req);
a521b048 2957 spin_unlock_irqrestore(&block->request_queue_lock, flags);
c5576876
SH
2958
2959 return 0;
2960}
2961
8e09f215
SW
2962/*
2963 * Go through all request on the dasd_block request queue, cancel them
2964 * on the respective dasd_device, and return them to the generic
2965 * block layer.
2966 */
2967static int dasd_flush_block_queue(struct dasd_block *block)
2968{
2969 struct dasd_ccw_req *cqr, *n;
2970 int rc, i;
2971 struct list_head flush_queue;
2972
2973 INIT_LIST_HEAD(&flush_queue);
2974 spin_lock_bh(&block->queue_lock);
2975 rc = 0;
2976restart:
2977 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2978 /* if this request currently owned by a dasd_device cancel it */
2979 if (cqr->status >= DASD_CQR_QUEUED)
2980 rc = dasd_cancel_req(cqr);
2981 if (rc < 0)
2982 break;
2983 /* Rechain request (including erp chain) so it won't be
2984 * touched by the dasd_block_tasklet anymore.
2985 * Replace the callback so we notice when the request
2986 * is returned from the dasd_device layer.
2987 */
2988 cqr->callback = _dasd_wake_block_flush_cb;
2989 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2990 list_move_tail(&cqr->blocklist, &flush_queue);
2991 if (i > 1)
2992 /* moved more than one request - need to restart */
2993 goto restart;
2994 }
2995 spin_unlock_bh(&block->queue_lock);
2996 /* Now call the callback function of flushed requests */
2997restart_cb:
2998 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2999 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
3000 /* Process finished ERP request. */
3001 if (cqr->refers) {
0cd4bd47 3002 spin_lock_bh(&block->queue_lock);
eb6e199b 3003 __dasd_process_erp(block->base, cqr);
0cd4bd47 3004 spin_unlock_bh(&block->queue_lock);
8e09f215
SW
3005 /* restart list_for_xx loop since dasd_process_erp
3006 * might remove multiple elements */
3007 goto restart_cb;
3008 }
3009 /* call the callback function */
0cd4bd47 3010 spin_lock_irq(&block->request_queue_lock);
1aae0560 3011 cqr->endclk = get_tod_clock();
8e09f215
SW
3012 list_del_init(&cqr->blocklist);
3013 __dasd_cleanup_cqr(cqr);
0cd4bd47 3014 spin_unlock_irq(&block->request_queue_lock);
1da177e4 3015 }
1da177e4
LT
3016 return rc;
3017}
3018
3019/*
8e09f215
SW
3020 * Schedules a call to dasd_tasklet over the device tasklet.
3021 */
3022void dasd_schedule_block_bh(struct dasd_block *block)
3023{
3024 /* Protect against rescheduling. */
3025 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
3026 return;
3027 /* life cycle of block is bound to it's base device */
3028 dasd_get_device(block->base);
3029 tasklet_hi_schedule(&block->tasklet);
3030}
7048a2b5 3031EXPORT_SYMBOL(dasd_schedule_block_bh);
8e09f215
SW
3032
3033
3034/*
3035 * SECTION: external block device operations
3036 * (request queue handling, open, release, etc.)
1da177e4
LT
3037 */
3038
3039/*
3040 * Dasd request queue function. Called from ll_rw_blk.c
3041 */
8e09f215 3042static void do_dasd_request(struct request_queue *queue)
1da177e4 3043{
8e09f215 3044 struct dasd_block *block;
1da177e4 3045
8e09f215
SW
3046 block = queue->queuedata;
3047 spin_lock(&block->queue_lock);
1da177e4 3048 /* Get new request from the block device request queue */
8e09f215 3049 __dasd_process_request_queue(block);
1da177e4 3050 /* Now check if the head of the ccw queue needs to be started. */
8e09f215
SW
3051 __dasd_block_start_head(block);
3052 spin_unlock(&block->queue_lock);
1da177e4
LT
3053}
3054
a2ace466
HR
3055/*
3056 * Block timeout callback, called from the block layer
3057 *
3058 * request_queue lock is held on entry.
3059 *
3060 * Return values:
3061 * BLK_EH_RESET_TIMER if the request should be left running
3062 * BLK_EH_NOT_HANDLED if the request is handled or terminated
3063 * by the driver.
3064 */
3065enum blk_eh_timer_return dasd_times_out(struct request *req)
3066{
3067 struct dasd_ccw_req *cqr = req->completion_data;
3068 struct dasd_block *block = req->q->queuedata;
3069 struct dasd_device *device;
3070 int rc = 0;
3071
3072 if (!cqr)
3073 return BLK_EH_NOT_HANDLED;
3074
3075 device = cqr->startdev ? cqr->startdev : block->base;
3d71ad32
HR
3076 if (!device->blk_timeout)
3077 return BLK_EH_RESET_TIMER;
a2ace466
HR
3078 DBF_DEV_EVENT(DBF_WARNING, device,
3079 " dasd_times_out cqr %p status %x",
3080 cqr, cqr->status);
3081
3082 spin_lock(&block->queue_lock);
3083 spin_lock(get_ccwdev_lock(device->cdev));
3084 cqr->retries = -1;
3085 cqr->intrc = -ETIMEDOUT;
3086 if (cqr->status >= DASD_CQR_QUEUED) {
3087 spin_unlock(get_ccwdev_lock(device->cdev));
3088 rc = dasd_cancel_req(cqr);
3089 } else if (cqr->status == DASD_CQR_FILLED ||
3090 cqr->status == DASD_CQR_NEED_ERP) {
3091 cqr->status = DASD_CQR_TERMINATED;
3092 spin_unlock(get_ccwdev_lock(device->cdev));
3093 } else if (cqr->status == DASD_CQR_IN_ERP) {
3094 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
3095
3096 list_for_each_entry_safe(searchcqr, nextcqr,
3097 &block->ccw_queue, blocklist) {
3098 tmpcqr = searchcqr;
3099 while (tmpcqr->refers)
3100 tmpcqr = tmpcqr->refers;
3101 if (tmpcqr != cqr)
3102 continue;
3103 /* searchcqr is an ERP request for cqr */
3104 searchcqr->retries = -1;
3105 searchcqr->intrc = -ETIMEDOUT;
3106 if (searchcqr->status >= DASD_CQR_QUEUED) {
3107 spin_unlock(get_ccwdev_lock(device->cdev));
3108 rc = dasd_cancel_req(searchcqr);
3109 spin_lock(get_ccwdev_lock(device->cdev));
3110 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
3111 (searchcqr->status == DASD_CQR_NEED_ERP)) {
3112 searchcqr->status = DASD_CQR_TERMINATED;
3113 rc = 0;
3114 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
3115 /*
3116 * Shouldn't happen; most recent ERP
3117 * request is at the front of queue
3118 */
3119 continue;
3120 }
3121 break;
3122 }
3123 spin_unlock(get_ccwdev_lock(device->cdev));
3124 }
3125 dasd_schedule_block_bh(block);
3126 spin_unlock(&block->queue_lock);
3127
3128 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
3129}
3130
1da177e4
LT
3131/*
3132 * Allocate and initialize request queue and default I/O scheduler.
3133 */
8e09f215 3134static int dasd_alloc_queue(struct dasd_block *block)
1da177e4 3135{
8e09f215
SW
3136 block->request_queue = blk_init_queue(do_dasd_request,
3137 &block->request_queue_lock);
3138 if (block->request_queue == NULL)
1da177e4
LT
3139 return -ENOMEM;
3140
8e09f215 3141 block->request_queue->queuedata = block;
1da177e4 3142
a5fd8ddc 3143 return 0;
1da177e4
LT
3144}
3145
3146/*
3147 * Allocate and initialize request queue.
3148 */
8e09f215 3149static void dasd_setup_queue(struct dasd_block *block)
1da177e4 3150{
62ba6f85 3151 struct request_queue *q = block->request_queue;
1da177e4
LT
3152 int max;
3153
e4dbb0f2
SH
3154 if (block->base->features & DASD_FEATURE_USERAW) {
3155 /*
3156 * the max_blocks value for raw_track access is 256
3157 * it is higher than the native ECKD value because we
3158 * only need one ccw per track
3159 * so the max_hw_sectors are
3160 * 2048 x 512B = 1024kB = 16 tracks
3161 */
3162 max = 2048;
3163 } else {
3164 max = block->base->discipline->max_blocks << block->s2b_shift;
3165 }
62ba6f85
JH
3166 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
3167 q->limits.max_dev_sectors = max;
3168 blk_queue_logical_block_size(q, block->bp_block);
3169 blk_queue_max_hw_sectors(q, max);
3170 blk_queue_max_segments(q, USHRT_MAX);
f3eb5384
SW
3171 /* with page sized segments we can translate each segement into
3172 * one idaw/tidaw
3173 */
62ba6f85
JH
3174 blk_queue_max_segment_size(q, PAGE_SIZE);
3175 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
1da177e4
LT
3176}
3177
3178/*
3179 * Deactivate and free request queue.
3180 */
8e09f215 3181static void dasd_free_queue(struct dasd_block *block)
1da177e4 3182{
8e09f215
SW
3183 if (block->request_queue) {
3184 blk_cleanup_queue(block->request_queue);
3185 block->request_queue = NULL;
1da177e4
LT
3186 }
3187}
3188
3189/*
3190 * Flush request on the request queue.
3191 */
8e09f215 3192static void dasd_flush_request_queue(struct dasd_block *block)
1da177e4
LT
3193{
3194 struct request *req;
3195
8e09f215 3196 if (!block->request_queue)
1da177e4 3197 return;
138c014d 3198
8e09f215 3199 spin_lock_irq(&block->request_queue_lock);
9934c8c0 3200 while ((req = blk_fetch_request(block->request_queue)))
2a842aca 3201 __blk_end_request_all(req, BLK_STS_IOERR);
8e09f215 3202 spin_unlock_irq(&block->request_queue_lock);
1da177e4
LT
3203}
3204
57a7c0bc 3205static int dasd_open(struct block_device *bdev, fmode_t mode)
1da177e4 3206{
9eb25122 3207 struct dasd_device *base;
1da177e4
LT
3208 int rc;
3209
65f8da47
SW
3210 base = dasd_device_from_gendisk(bdev->bd_disk);
3211 if (!base)
9eb25122
SH
3212 return -ENODEV;
3213
65f8da47 3214 atomic_inc(&base->block->open_count);
8e09f215 3215 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
1da177e4
LT
3216 rc = -ENODEV;
3217 goto unlock;
3218 }
3219
8e09f215 3220 if (!try_module_get(base->discipline->owner)) {
1da177e4
LT
3221 rc = -EINVAL;
3222 goto unlock;
3223 }
3224
3225 if (dasd_probeonly) {
fc19f381
SH
3226 dev_info(&base->cdev->dev,
3227 "Accessing the DASD failed because it is in "
3228 "probeonly mode\n");
1da177e4
LT
3229 rc = -EPERM;
3230 goto out;
3231 }
3232
8e09f215
SW
3233 if (base->state <= DASD_STATE_BASIC) {
3234 DBF_DEV_EVENT(DBF_ERR, base, " %s",
1da177e4
LT
3235 " Cannot open unrecognized device");
3236 rc = -ENODEV;
3237 goto out;
3238 }
3239
33b62a30
SW
3240 if ((mode & FMODE_WRITE) &&
3241 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3242 (base->features & DASD_FEATURE_READONLY))) {
3243 rc = -EROFS;
3244 goto out;
3245 }
3246
65f8da47 3247 dasd_put_device(base);
1da177e4
LT
3248 return 0;
3249
3250out:
8e09f215 3251 module_put(base->discipline->owner);
1da177e4 3252unlock:
65f8da47
SW
3253 atomic_dec(&base->block->open_count);
3254 dasd_put_device(base);
1da177e4
LT
3255 return rc;
3256}
3257
db2a144b 3258static void dasd_release(struct gendisk *disk, fmode_t mode)
1da177e4 3259{
db2a144b
AV
3260 struct dasd_device *base = dasd_device_from_gendisk(disk);
3261 if (base) {
3262 atomic_dec(&base->block->open_count);
3263 module_put(base->discipline->owner);
3264 dasd_put_device(base);
3265 }
1da177e4
LT
3266}
3267
a885c8c4
CH
3268/*
3269 * Return disk geometry.
3270 */
8e09f215 3271static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
a885c8c4 3272{
8e09f215 3273 struct dasd_device *base;
a885c8c4 3274
65f8da47
SW
3275 base = dasd_device_from_gendisk(bdev->bd_disk);
3276 if (!base)
a885c8c4
CH
3277 return -ENODEV;
3278
8e09f215 3279 if (!base->discipline ||
65f8da47
SW
3280 !base->discipline->fill_geometry) {
3281 dasd_put_device(base);
a885c8c4 3282 return -EINVAL;
65f8da47
SW
3283 }
3284 base->discipline->fill_geometry(base->block, geo);
3285 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3286 dasd_put_device(base);
a885c8c4
CH
3287 return 0;
3288}
3289
83d5cde4 3290const struct block_device_operations
1da177e4
LT
3291dasd_device_operations = {
3292 .owner = THIS_MODULE,
57a7c0bc
AV
3293 .open = dasd_open,
3294 .release = dasd_release,
0000d031
HC
3295 .ioctl = dasd_ioctl,
3296 .compat_ioctl = dasd_ioctl,
a885c8c4 3297 .getgeo = dasd_getgeo,
1da177e4
LT
3298};
3299
8e09f215
SW
3300/*******************************************************************************
3301 * end of block device operations
3302 */
1da177e4
LT
3303
3304static void
3305dasd_exit(void)
3306{
3307#ifdef CONFIG_PROC_FS
3308 dasd_proc_exit();
3309#endif
20c64468 3310 dasd_eer_exit();
6bb0e010
HH
3311 if (dasd_page_cache != NULL) {
3312 kmem_cache_destroy(dasd_page_cache);
3313 dasd_page_cache = NULL;
3314 }
1da177e4
LT
3315 dasd_gendisk_exit();
3316 dasd_devmap_exit();
1da177e4
LT
3317 if (dasd_debug_area != NULL) {
3318 debug_unregister(dasd_debug_area);
3319 dasd_debug_area = NULL;
3320 }
4fa52aa7 3321 dasd_statistics_removeroot();
1da177e4
LT
3322}
3323
3324/*
3325 * SECTION: common functions for ccw_driver use
3326 */
3327
33b62a30
SW
3328/*
3329 * Is the device read-only?
3330 * Note that this function does not report the setting of the
3331 * readonly device attribute, but how it is configured in z/VM.
3332 */
3333int dasd_device_is_ro(struct dasd_device *device)
3334{
3335 struct ccw_dev_id dev_id;
3336 struct diag210 diag_data;
3337 int rc;
3338
3339 if (!MACHINE_IS_VM)
3340 return 0;
3341 ccw_device_get_id(device->cdev, &dev_id);
3342 memset(&diag_data, 0, sizeof(diag_data));
3343 diag_data.vrdcdvno = dev_id.devno;
3344 diag_data.vrdclen = sizeof(diag_data);
3345 rc = diag210(&diag_data);
3346 if (rc == 0 || rc == 2) {
3347 return diag_data.vrdcvfla & 0x80;
3348 } else {
3349 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3350 dev_id.devno, rc);
3351 return 0;
3352 }
3353}
3354EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3355
f3445a1a
CH
3356static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3357{
3358 struct ccw_device *cdev = data;
3359 int ret;
3360
3361 ret = ccw_device_set_online(cdev);
3362 if (ret)
4ce25966
FF
3363 pr_warn("%s: Setting the DASD online failed with rc=%d\n",
3364 dev_name(&cdev->dev), ret);
f3445a1a
CH
3365}
3366
1c01b8a5
HH
3367/*
3368 * Initial attempt at a probe function. this can be simplified once
3369 * the other detection code is gone.
3370 */
8e09f215
SW
3371int dasd_generic_probe(struct ccw_device *cdev,
3372 struct dasd_discipline *discipline)
1da177e4
LT
3373{
3374 int ret;
3375
3376 ret = dasd_add_sysfs_files(cdev);
3377 if (ret) {
b8ed5dd5
SH
3378 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3379 "dasd_generic_probe: could not add "
3380 "sysfs entries");
40545573 3381 return ret;
1da177e4 3382 }
40545573 3383 cdev->handler = &dasd_int_handler;
1da177e4 3384
40545573
HH
3385 /*
3386 * Automatically online either all dasd devices (dasd_autodetect)
3387 * or all devices specified with dasd= parameters during
3388 * initial probe.
3389 */
3390 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2a0217d5 3391 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
f3445a1a 3392 async_schedule(dasd_generic_auto_online, cdev);
de3e0da1 3393 return 0;
1da177e4 3394}
7048a2b5 3395EXPORT_SYMBOL_GPL(dasd_generic_probe);
1da177e4 3396
c020d722
SH
3397void dasd_generic_free_discipline(struct dasd_device *device)
3398{
3399 /* Forget the discipline information. */
3400 if (device->discipline) {
3401 if (device->discipline->uncheck_device)
3402 device->discipline->uncheck_device(device);
3403 module_put(device->discipline->owner);
3404 device->discipline = NULL;
3405 }
3406 if (device->base_discipline) {
3407 module_put(device->base_discipline->owner);
3408 device->base_discipline = NULL;
3409 }
3410}
3411EXPORT_SYMBOL_GPL(dasd_generic_free_discipline);
3412
1c01b8a5
HH
3413/*
3414 * This will one day be called from a global not_oper handler.
3415 * It is also used by driver_unregister during module unload.
3416 */
8e09f215 3417void dasd_generic_remove(struct ccw_device *cdev)
1da177e4
LT
3418{
3419 struct dasd_device *device;
8e09f215 3420 struct dasd_block *block;
1da177e4 3421
59afda78
HH
3422 cdev->handler = NULL;
3423
1da177e4 3424 device = dasd_device_from_cdev(cdev);
be4904e5
SW
3425 if (IS_ERR(device)) {
3426 dasd_remove_sysfs_files(cdev);
1da177e4 3427 return;
be4904e5 3428 }
d07dc5d8
SH
3429 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3430 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1da177e4
LT
3431 /* Already doing offline processing */
3432 dasd_put_device(device);
be4904e5 3433 dasd_remove_sysfs_files(cdev);
1da177e4
LT
3434 return;
3435 }
3436 /*
3437 * This device is removed unconditionally. Set offline
3438 * flag to prevent dasd_open from opening it while it is
3439 * no quite down yet.
3440 */
3441 dasd_set_target_state(device, DASD_STATE_NEW);
3442 /* dasd_delete_device destroys the device reference. */
8e09f215 3443 block = device->block;
1da177e4 3444 dasd_delete_device(device);
8e09f215
SW
3445 /*
3446 * life cycle of block is bound to device, so delete it after
3447 * device was safely removed
3448 */
3449 if (block)
3450 dasd_free_block(block);
d07dc5d8
SH
3451
3452 dasd_remove_sysfs_files(cdev);
1da177e4 3453}
7048a2b5 3454EXPORT_SYMBOL_GPL(dasd_generic_remove);
1da177e4 3455
1c01b8a5
HH
3456/*
3457 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
1da177e4 3458 * the device is detected for the first time and is supposed to be used
1c01b8a5
HH
3459 * or the user has started activation through sysfs.
3460 */
8e09f215
SW
3461int dasd_generic_set_online(struct ccw_device *cdev,
3462 struct dasd_discipline *base_discipline)
1da177e4 3463{
aa88861f 3464 struct dasd_discipline *discipline;
1da177e4 3465 struct dasd_device *device;
c6eb7b77 3466 int rc;
f24acd45 3467
40545573
HH
3468 /* first online clears initial online feature flag */
3469 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
1da177e4
LT
3470 device = dasd_create_device(cdev);
3471 if (IS_ERR(device))
3472 return PTR_ERR(device);
3473
aa88861f 3474 discipline = base_discipline;
c6eb7b77 3475 if (device->features & DASD_FEATURE_USEDIAG) {
1da177e4 3476 if (!dasd_diag_discipline_pointer) {
7c53fcb3
PO
3477 /* Try to load the required module. */
3478 rc = request_module(DASD_DIAG_MOD);
3479 if (rc) {
3480 pr_warn("%s Setting the DASD online failed "
3481 "because the required module %s "
3482 "could not be loaded (rc=%d)\n",
3483 dev_name(&cdev->dev), DASD_DIAG_MOD,
3484 rc);
3485 dasd_delete_device(device);
3486 return -ENODEV;
3487 }
3488 }
3489 /* Module init could have failed, so check again here after
3490 * request_module(). */
3491 if (!dasd_diag_discipline_pointer) {
4ce25966
FF
3492 pr_warn("%s Setting the DASD online failed because of missing DIAG discipline\n",
3493 dev_name(&cdev->dev));
1da177e4
LT
3494 dasd_delete_device(device);
3495 return -ENODEV;
3496 }
3497 discipline = dasd_diag_discipline_pointer;
3498 }
aa88861f
PO
3499 if (!try_module_get(base_discipline->owner)) {
3500 dasd_delete_device(device);
3501 return -EINVAL;
3502 }
3503 if (!try_module_get(discipline->owner)) {
3504 module_put(base_discipline->owner);
3505 dasd_delete_device(device);
3506 return -EINVAL;
3507 }
3508 device->base_discipline = base_discipline;
1da177e4
LT
3509 device->discipline = discipline;
3510
8e09f215 3511 /* check_device will allocate block device if necessary */
1da177e4
LT
3512 rc = discipline->check_device(device);
3513 if (rc) {
4ce25966
FF
3514 pr_warn("%s Setting the DASD online with discipline %s failed with rc=%i\n",
3515 dev_name(&cdev->dev), discipline->name, rc);
aa88861f
PO
3516 module_put(discipline->owner);
3517 module_put(base_discipline->owner);
1da177e4
LT
3518 dasd_delete_device(device);
3519 return rc;
3520 }
3521
3522 dasd_set_target_state(device, DASD_STATE_ONLINE);
3523 if (device->state <= DASD_STATE_KNOWN) {
4ce25966
FF
3524 pr_warn("%s Setting the DASD online failed because of a missing discipline\n",
3525 dev_name(&cdev->dev));
1da177e4
LT
3526 rc = -ENODEV;
3527 dasd_set_target_state(device, DASD_STATE_NEW);
8e09f215
SW
3528 if (device->block)
3529 dasd_free_block(device->block);
1da177e4
LT
3530 dasd_delete_device(device);
3531 } else
3532 pr_debug("dasd_generic device %s found\n",
2a0217d5 3533 dev_name(&cdev->dev));
589c74d5
SH
3534
3535 wait_event(dasd_init_waitq, _wait_for_device(device));
3536
1da177e4 3537 dasd_put_device(device);
1da177e4
LT
3538 return rc;
3539}
7048a2b5 3540EXPORT_SYMBOL_GPL(dasd_generic_set_online);
1da177e4 3541
8e09f215 3542int dasd_generic_set_offline(struct ccw_device *cdev)
1da177e4
LT
3543{
3544 struct dasd_device *device;
8e09f215 3545 struct dasd_block *block;
d07dc5d8 3546 int max_count, open_count, rc;
0f57c97f 3547 unsigned long flags;
1da177e4 3548
d07dc5d8 3549 rc = 0;
0f57c97f
JH
3550 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
3551 device = dasd_device_from_cdev_locked(cdev);
3552 if (IS_ERR(device)) {
3553 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1da177e4 3554 return PTR_ERR(device);
0f57c97f 3555 }
d07dc5d8 3556
1da177e4
LT
3557 /*
3558 * We must make sure that this device is currently not in use.
3559 * The open_count is increased for every opener, that includes
3560 * the blkdev_get in dasd_scan_partitions. We are only interested
3561 * in the other openers.
3562 */
8e09f215 3563 if (device->block) {
a806170e
HC
3564 max_count = device->block->bdev ? 0 : -1;
3565 open_count = atomic_read(&device->block->open_count);
8e09f215
SW
3566 if (open_count > max_count) {
3567 if (open_count > 0)
4ce25966
FF
3568 pr_warn("%s: The DASD cannot be set offline with open count %i\n",
3569 dev_name(&cdev->dev), open_count);
8e09f215 3570 else
4ce25966
FF
3571 pr_warn("%s: The DASD cannot be set offline while it is in use\n",
3572 dev_name(&cdev->dev));
8e09f215 3573 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
0f57c97f 3574 goto out_busy;
8e09f215 3575 }
1da177e4 3576 }
d07dc5d8
SH
3577
3578 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3579 /*
b4a96015 3580 * safe offline already running
d07dc5d8
SH
3581 * could only be called by normal offline so safe_offline flag
3582 * needs to be removed to run normal offline and kill all I/O
3583 */
0f57c97f 3584 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags))
d07dc5d8 3585 /* Already doing normal offline processing */
0f57c97f
JH
3586 goto out_busy;
3587 else
d07dc5d8 3588 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
0f57c97f
JH
3589 } else {
3590 if (test_bit(DASD_FLAG_OFFLINE, &device->flags))
d07dc5d8 3591 /* Already doing offline processing */
0f57c97f
JH
3592 goto out_busy;
3593 }
3594
3595 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3596 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
d07dc5d8
SH
3597
3598 /*
3599 * if safe_offline called set safe_offline_running flag and
3600 * clear safe_offline so that a call to normal offline
3601 * can overrun safe_offline processing
3602 */
3603 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3604 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3605 /*
3606 * If we want to set the device safe offline all IO operations
3607 * should be finished before continuing the offline process
3608 * so sync bdev first and then wait for our queues to become
3609 * empty
3610 */
3611 /* sync blockdev and partitions */
ca732e11
SH
3612 if (device->block) {
3613 rc = fsync_bdev(device->block->bdev);
3614 if (rc != 0)
3615 goto interrupted;
3616 }
d07dc5d8
SH
3617 /* schedule device tasklet and wait for completion */
3618 dasd_schedule_device_bh(device);
3619 rc = wait_event_interruptible(shutdown_waitq,
3620 _wait_for_empty_queues(device));
3621 if (rc != 0)
3622 goto interrupted;
3623 }
3624
1da177e4
LT
3625 dasd_set_target_state(device, DASD_STATE_NEW);
3626 /* dasd_delete_device destroys the device reference. */
8e09f215 3627 block = device->block;
1da177e4 3628 dasd_delete_device(device);
8e09f215
SW
3629 /*
3630 * life cycle of block is bound to device, so delete it after
3631 * device was safely removed
3632 */
3633 if (block)
3634 dasd_free_block(block);
1da177e4 3635 return 0;
d07dc5d8
SH
3636
3637interrupted:
3638 /* interrupted by signal */
3639 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3640 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3641 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3642 dasd_put_device(device);
0f57c97f 3643
d07dc5d8 3644 return rc;
0f57c97f
JH
3645
3646out_busy:
3647 dasd_put_device(device);
3648 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
3649
3650 return -EBUSY;
1da177e4 3651}
7048a2b5 3652EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
1da177e4 3653
a4d26c6a
SW
3654int dasd_generic_last_path_gone(struct dasd_device *device)
3655{
3656 struct dasd_ccw_req *cqr;
3657
3658 dev_warn(&device->cdev->dev, "No operational channel path is left "
3659 "for the device\n");
3660 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3661 /* First of all call extended error reporting. */
3662 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3663
3664 if (device->state < DASD_STATE_BASIC)
3665 return 0;
3666 /* Device is active. We want to keep it. */
3667 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3668 if ((cqr->status == DASD_CQR_IN_IO) ||
3669 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3670 cqr->status = DASD_CQR_QUEUED;
3671 cqr->retries++;
3672 }
3673 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3674 dasd_device_clear_timer(device);
3675 dasd_schedule_device_bh(device);
3676 return 1;
3677}
3678EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3679
3680int dasd_generic_path_operational(struct dasd_device *device)
3681{
3682 dev_info(&device->cdev->dev, "A channel path to the device has become "
3683 "operational\n");
3684 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3685 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3686 if (device->stopped & DASD_UNRESUMED_PM) {
3687 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3688 dasd_restore_device(device);
3689 return 1;
3690 }
3691 dasd_schedule_device_bh(device);
3692 if (device->block)
3693 dasd_schedule_block_bh(device->block);
931a3dce
SH
3694
3695 if (!device->stopped)
3696 wake_up(&generic_waitq);
3697
a4d26c6a
SW
3698 return 1;
3699}
3700EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3701
8e09f215 3702int dasd_generic_notify(struct ccw_device *cdev, int event)
1da177e4
LT
3703{
3704 struct dasd_device *device;
1da177e4
LT
3705 int ret;
3706
91c36919 3707 device = dasd_device_from_cdev_locked(cdev);
1da177e4
LT
3708 if (IS_ERR(device))
3709 return 0;
1da177e4
LT
3710 ret = 0;
3711 switch (event) {
3712 case CIO_GONE:
47593bfa 3713 case CIO_BOXED:
1da177e4 3714 case CIO_NO_PATH:
c9346151 3715 dasd_path_no_path(device);
a4d26c6a 3716 ret = dasd_generic_last_path_gone(device);
1da177e4
LT
3717 break;
3718 case CIO_OPER:
1da177e4 3719 ret = 1;
c9346151 3720 if (dasd_path_get_opm(device))
a4d26c6a 3721 ret = dasd_generic_path_operational(device);
1da177e4
LT
3722 break;
3723 }
1da177e4
LT
3724 dasd_put_device(device);
3725 return ret;
3726}
7048a2b5 3727EXPORT_SYMBOL_GPL(dasd_generic_notify);
1da177e4 3728
a4d26c6a
SW
3729void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3730{
a4d26c6a 3731 struct dasd_device *device;
a521b048 3732 int chp, oldopm, hpfpm, ifccpm;
a4d26c6a
SW
3733
3734 device = dasd_device_from_cdev_locked(cdev);
3735 if (IS_ERR(device))
3736 return;
c9346151
SH
3737
3738 oldopm = dasd_path_get_opm(device);
a4d26c6a 3739 for (chp = 0; chp < 8; chp++) {
a4d26c6a 3740 if (path_event[chp] & PE_PATH_GONE) {
c9346151 3741 dasd_path_notoper(device, chp);
a4d26c6a
SW
3742 }
3743 if (path_event[chp] & PE_PATH_AVAILABLE) {
c9346151 3744 dasd_path_available(device, chp);
a4d26c6a
SW
3745 dasd_schedule_device_bh(device);
3746 }
f1633031 3747 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
c9346151
SH
3748 if (!dasd_path_is_operational(device, chp) &&
3749 !dasd_path_need_verify(device, chp)) {
12d7b107
SH
3750 /*
3751 * we can not establish a pathgroup on an
3752 * unavailable path, so trigger a path
3753 * verification first
3754 */
c9346151
SH
3755 dasd_path_available(device, chp);
3756 dasd_schedule_device_bh(device);
12d7b107 3757 }
f1633031
SH
3758 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3759 "Pathgroup re-established\n");
3760 if (device->discipline->kick_validate)
3761 device->discipline->kick_validate(device);
3762 }
a4d26c6a 3763 }
a521b048
SH
3764 hpfpm = dasd_path_get_hpfpm(device);
3765 ifccpm = dasd_path_get_ifccpm(device);
3766 if (!dasd_path_get_opm(device) && hpfpm) {
3767 /*
3768 * device has no operational paths but at least one path is
3769 * disabled due to HPF errors
3770 * disable HPF at all and use the path(s) again
3771 */
3772 if (device->discipline->disable_hpf)
3773 device->discipline->disable_hpf(device);
3774 dasd_device_set_stop_bits(device, DASD_STOPPED_NOT_ACC);
3775 dasd_path_set_tbvpm(device, hpfpm);
3776 dasd_schedule_device_bh(device);
3777 dasd_schedule_requeue(device);
3778 } else if (!dasd_path_get_opm(device) && ifccpm) {
3779 /*
3780 * device has no operational paths but at least one path is
3781 * disabled due to IFCC errors
3782 * trigger path verification on paths with IFCC errors
3783 */
3784 dasd_path_set_tbvpm(device, ifccpm);
3785 dasd_schedule_device_bh(device);
3786 }
3787 if (oldopm && !dasd_path_get_opm(device) && !hpfpm && !ifccpm) {
c9346151
SH
3788 dev_warn(&device->cdev->dev,
3789 "No verified channel paths remain for the device\n");
3790 DBF_DEV_EVENT(DBF_WARNING, device,
3791 "%s", "last verified path gone");
3792 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3793 dasd_device_set_stop_bits(device,
3794 DASD_STOPPED_DC_WAIT);
3795 }
a4d26c6a
SW
3796 dasd_put_device(device);
3797}
3798EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3799
3800int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3801{
c9346151
SH
3802 if (!dasd_path_get_opm(device) && lpm) {
3803 dasd_path_set_opm(device, lpm);
a4d26c6a
SW
3804 dasd_generic_path_operational(device);
3805 } else
c9346151 3806 dasd_path_add_opm(device, lpm);
a4d26c6a
SW
3807 return 0;
3808}
3809EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3810
a521b048
SH
3811/*
3812 * clear active requests and requeue them to block layer if possible
3813 */
3814static int dasd_generic_requeue_all_requests(struct dasd_device *device)
d41dd122 3815{
a521b048 3816 struct list_head requeue_queue;
d41dd122 3817 struct dasd_ccw_req *cqr, *n;
c5576876 3818 struct dasd_ccw_req *refers;
d41dd122 3819 int rc;
d41dd122 3820
a521b048
SH
3821 INIT_LIST_HEAD(&requeue_queue);
3822 spin_lock_irq(get_ccwdev_lock(device->cdev));
d41dd122
SH
3823 rc = 0;
3824 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3825 /* Check status and move request to flush_queue */
3826 if (cqr->status == DASD_CQR_IN_IO) {
3827 rc = device->discipline->term_IO(cqr);
3828 if (rc) {
3829 /* unable to terminate requeust */
3830 dev_err(&device->cdev->dev,
3831 "Unable to terminate request %p "
3832 "on suspend\n", cqr);
a521b048 3833 spin_unlock_irq(get_ccwdev_lock(device->cdev));
d41dd122
SH
3834 dasd_put_device(device);
3835 return rc;
3836 }
3837 }
a521b048 3838 list_move_tail(&cqr->devlist, &requeue_queue);
d41dd122 3839 }
a521b048 3840 spin_unlock_irq(get_ccwdev_lock(device->cdev));
d41dd122 3841
a521b048 3842 list_for_each_entry_safe(cqr, n, &requeue_queue, devlist) {
d41dd122
SH
3843 wait_event(dasd_flush_wq,
3844 (cqr->status != DASD_CQR_CLEAR_PENDING));
c5576876 3845
a521b048
SH
3846 /* mark sleepon requests as ended */
3847 if (cqr->callback_data == DASD_SLEEPON_START_TAG)
3848 cqr->callback_data = DASD_SLEEPON_END_TAG;
c5576876
SH
3849
3850 /* remove requests from device and block queue */
3851 list_del_init(&cqr->devlist);
3852 while (cqr->refers != NULL) {
3853 refers = cqr->refers;
3854 /* remove the request from the block queue */
3855 list_del(&cqr->blocklist);
3856 /* free the finished erp request */
3857 dasd_free_erp_request(cqr, cqr->memdev);
3858 cqr = refers;
3859 }
a521b048
SH
3860
3861 /*
3862 * requeue requests to blocklayer will only work
3863 * for block device requests
3864 */
3865 if (_dasd_requeue_request(cqr))
3866 continue;
3867
c5576876
SH
3868 if (cqr->block)
3869 list_del_init(&cqr->blocklist);
3870 cqr->block->base->discipline->free_cp(
3871 cqr, (struct request *) cqr->callback_data);
d41dd122 3872 }
d41dd122 3873
c5576876
SH
3874 /*
3875 * if requests remain then they are internal request
3876 * and go back to the device queue
3877 */
a521b048 3878 if (!list_empty(&requeue_queue)) {
c5576876 3879 /* move freeze_queue to start of the ccw_queue */
a521b048
SH
3880 spin_lock_irq(get_ccwdev_lock(device->cdev));
3881 list_splice_tail(&requeue_queue, &device->ccw_queue);
3882 spin_unlock_irq(get_ccwdev_lock(device->cdev));
c5576876 3883 }
a521b048
SH
3884 /* wake up generic waitqueue for eventually ended sleepon requests */
3885 wake_up(&generic_waitq);
d41dd122
SH
3886 return rc;
3887}
a521b048
SH
3888
3889static void do_requeue_requests(struct work_struct *work)
3890{
3891 struct dasd_device *device = container_of(work, struct dasd_device,
3892 requeue_requests);
3893 dasd_generic_requeue_all_requests(device);
3894 dasd_device_remove_stop_bits(device, DASD_STOPPED_NOT_ACC);
3895 if (device->block)
3896 dasd_schedule_block_bh(device->block);
3897 dasd_put_device(device);
3898}
3899
3900void dasd_schedule_requeue(struct dasd_device *device)
3901{
3902 dasd_get_device(device);
3903 /* queue call to dasd_reload_device to the kernel event daemon. */
3904 if (!schedule_work(&device->requeue_requests))
3905 dasd_put_device(device);
3906}
3907EXPORT_SYMBOL(dasd_schedule_requeue);
3908
3909int dasd_generic_pm_freeze(struct ccw_device *cdev)
3910{
3911 struct dasd_device *device = dasd_device_from_cdev(cdev);
3912 int rc;
3913
3914 if (IS_ERR(device))
3915 return PTR_ERR(device);
3916
3917 /* mark device as suspended */
3918 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3919
3920 if (device->discipline->freeze)
3921 rc = device->discipline->freeze(device);
3922
3923 /* disallow new I/O */
3924 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3925
3926 return dasd_generic_requeue_all_requests(device);
3927}
d41dd122
SH
3928EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3929
3930int dasd_generic_restore_device(struct ccw_device *cdev)
3931{
3932 struct dasd_device *device = dasd_device_from_cdev(cdev);
3933 int rc = 0;
3934
3935 if (IS_ERR(device))
3936 return PTR_ERR(device);
3937
e6125fba 3938 /* allow new IO again */
eb6e199b
SW
3939 dasd_device_remove_stop_bits(device,
3940 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
e6125fba 3941
d41dd122 3942 dasd_schedule_device_bh(device);
d41dd122 3943
eb6e199b
SW
3944 /*
3945 * call discipline restore function
3946 * if device is stopped do nothing e.g. for disconnected devices
3947 */
3948 if (device->discipline->restore && !(device->stopped))
d41dd122 3949 rc = device->discipline->restore(device);
eb6e199b 3950 if (rc || device->stopped)
e6125fba
SH
3951 /*
3952 * if the resume failed for the DASD we put it in
3953 * an UNRESUMED stop state
3954 */
3955 device->stopped |= DASD_UNRESUMED_PM;
d41dd122 3956
6fca97a9
SH
3957 if (device->block)
3958 dasd_schedule_block_bh(device->block);
3959
c8d1c0ff 3960 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
d41dd122 3961 dasd_put_device(device);
e6125fba 3962 return 0;
d41dd122
SH
3963}
3964EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3965
763968e2
HC
3966static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3967 void *rdc_buffer,
3968 int rdc_buffer_size,
68b781fe 3969 int magic)
17283b56
CH
3970{
3971 struct dasd_ccw_req *cqr;
3972 struct ccw1 *ccw;
d9fa9441 3973 unsigned long *idaw;
17283b56
CH
3974
3975 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3976
3977 if (IS_ERR(cqr)) {
fc19f381
SH
3978 /* internal error 13 - Allocating the RDC request failed*/
3979 dev_err(&device->cdev->dev,
3980 "An error occurred in the DASD device driver, "
3981 "reason=%s\n", "13");
17283b56
CH
3982 return cqr;
3983 }
3984
3985 ccw = cqr->cpaddr;
3986 ccw->cmd_code = CCW_CMD_RDC;
d9fa9441
SH
3987 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3988 idaw = (unsigned long *) (cqr->data);
3989 ccw->cda = (__u32)(addr_t) idaw;
3990 ccw->flags = CCW_FLAG_IDA;
3991 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3992 } else {
3993 ccw->cda = (__u32)(addr_t) rdc_buffer;
3994 ccw->flags = 0;
3995 }
17283b56 3996
d9fa9441 3997 ccw->count = rdc_buffer_size;
8e09f215
SW
3998 cqr->startdev = device;
3999 cqr->memdev = device;
17283b56 4000 cqr->expires = 10*HZ;
eb6e199b 4001 cqr->retries = 256;
1aae0560 4002 cqr->buildclk = get_tod_clock();
17283b56
CH
4003 cqr->status = DASD_CQR_FILLED;
4004 return cqr;
4005}
4006
4007
68b781fe 4008int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
92636b15 4009 void *rdc_buffer, int rdc_buffer_size)
17283b56
CH
4010{
4011 int ret;
4012 struct dasd_ccw_req *cqr;
4013
92636b15 4014 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
17283b56
CH
4015 magic);
4016 if (IS_ERR(cqr))
4017 return PTR_ERR(cqr);
4018
4019 ret = dasd_sleep_on(cqr);
8e09f215 4020 dasd_sfree_request(cqr, cqr->memdev);
17283b56
CH
4021 return ret;
4022}
aaff0f64 4023EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
20c64468 4024
f3eb5384
SW
4025/*
4026 * In command mode and transport mode we need to look for sense
4027 * data in different places. The sense data itself is allways
4028 * an array of 32 bytes, so we can unify the sense data access
4029 * for both modes.
4030 */
4031char *dasd_get_sense(struct irb *irb)
4032{
4033 struct tsb *tsb = NULL;
4034 char *sense = NULL;
4035
4036 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
4037 if (irb->scsw.tm.tcw)
4038 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
4039 irb->scsw.tm.tcw);
4040 if (tsb && tsb->length == 64 && tsb->flags)
4041 switch (tsb->flags & 0x07) {
4042 case 1: /* tsa_iostat */
4043 sense = tsb->tsa.iostat.sense;
4044 break;
4045 case 2: /* tsa_ddpc */
4046 sense = tsb->tsa.ddpc.sense;
4047 break;
4048 default:
4049 /* currently we don't use interrogate data */
4050 break;
4051 }
4052 } else if (irb->esw.esw0.erw.cons) {
4053 sense = irb->ecw;
4054 }
4055 return sense;
4056}
4057EXPORT_SYMBOL_GPL(dasd_get_sense);
4058
4679e893
SH
4059void dasd_generic_shutdown(struct ccw_device *cdev)
4060{
4061 struct dasd_device *device;
4062
4063 device = dasd_device_from_cdev(cdev);
4064 if (IS_ERR(device))
4065 return;
4066
4067 if (device->block)
4068 dasd_schedule_block_bh(device->block);
4069
4070 dasd_schedule_device_bh(device);
4071
4072 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
4073}
4074EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
4075
8e09f215 4076static int __init dasd_init(void)
1da177e4
LT
4077{
4078 int rc;
4079
4080 init_waitqueue_head(&dasd_init_waitq);
8f61701b 4081 init_waitqueue_head(&dasd_flush_wq);
c80ee724 4082 init_waitqueue_head(&generic_waitq);
4679e893 4083 init_waitqueue_head(&shutdown_waitq);
1da177e4
LT
4084
4085 /* register 'common' DASD debug area, used for all DBF_XXX calls */
361f494d 4086 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
1da177e4
LT
4087 if (dasd_debug_area == NULL) {
4088 rc = -ENOMEM;
4089 goto failed;
4090 }
4091 debug_register_view(dasd_debug_area, &debug_sprintf_view);
b0035f12 4092 debug_set_level(dasd_debug_area, DBF_WARNING);
1da177e4
LT
4093
4094 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
4095
4096 dasd_diag_discipline_pointer = NULL;
4097
4fa52aa7
SW
4098 dasd_statistics_createroot();
4099
1da177e4
LT
4100 rc = dasd_devmap_init();
4101 if (rc)
4102 goto failed;
4103 rc = dasd_gendisk_init();
4104 if (rc)
4105 goto failed;
4106 rc = dasd_parse();
4107 if (rc)
4108 goto failed;
20c64468
SW
4109 rc = dasd_eer_init();
4110 if (rc)
4111 goto failed;
1da177e4
LT
4112#ifdef CONFIG_PROC_FS
4113 rc = dasd_proc_init();
4114 if (rc)
4115 goto failed;
4116#endif
4117
4118 return 0;
4119failed:
fc19f381 4120 pr_info("The DASD device driver could not be initialized\n");
1da177e4
LT
4121 dasd_exit();
4122 return rc;
4123}
4124
4125module_init(dasd_init);
4126module_exit(dasd_exit);