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