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