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