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