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