linux/drivers/ide/ide-atapi.c
<<
>>
Prefs
   1/*
   2 * ATAPI support.
   3 */
   4
   5#include <linux/kernel.h>
   6#include <linux/cdrom.h>
   7#include <linux/delay.h>
   8#include <linux/export.h>
   9#include <linux/ide.h>
  10#include <linux/scatterlist.h>
  11#include <linux/gfp.h>
  12
  13#include <scsi/scsi.h>
  14
  15#define DRV_NAME "ide-atapi"
  16#define PFX DRV_NAME ": "
  17
  18#ifdef DEBUG
  19#define debug_log(fmt, args...) \
  20        printk(KERN_INFO "ide: " fmt, ## args)
  21#else
  22#define debug_log(fmt, args...) do {} while (0)
  23#endif
  24
  25#define ATAPI_MIN_CDB_BYTES     12
  26
  27static inline int dev_is_idecd(ide_drive_t *drive)
  28{
  29        return drive->media == ide_cdrom || drive->media == ide_optical;
  30}
  31
  32/*
  33 * Check whether we can support a device,
  34 * based on the ATAPI IDENTIFY command results.
  35 */
  36int ide_check_atapi_device(ide_drive_t *drive, const char *s)
  37{
  38        u16 *id = drive->id;
  39        u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
  40
  41        *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  42
  43        protocol    = (gcw[1] & 0xC0) >> 6;
  44        device_type =  gcw[1] & 0x1F;
  45        removable   = (gcw[0] & 0x80) >> 7;
  46        drq_type    = (gcw[0] & 0x60) >> 5;
  47        packet_size =  gcw[0] & 0x03;
  48
  49#ifdef CONFIG_PPC
  50        /* kludge for Apple PowerBook internal zip */
  51        if (drive->media == ide_floppy && device_type == 5 &&
  52            !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
  53            strstr((char *)&id[ATA_ID_PROD], "ZIP"))
  54                device_type = 0;
  55#endif
  56
  57        if (protocol != 2)
  58                printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
  59                        s, drive->name, protocol);
  60        else if ((drive->media == ide_floppy && device_type != 0) ||
  61                 (drive->media == ide_tape && device_type != 1))
  62                printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
  63                        s, drive->name, device_type);
  64        else if (removable == 0)
  65                printk(KERN_ERR "%s: %s: the removable flag is not set\n",
  66                        s, drive->name);
  67        else if (drive->media == ide_floppy && drq_type == 3)
  68                printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
  69                        "supported\n", s, drive->name, drq_type);
  70        else if (packet_size != 0)
  71                printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
  72                        "bytes\n", s, drive->name, packet_size);
  73        else
  74                return 1;
  75        return 0;
  76}
  77EXPORT_SYMBOL_GPL(ide_check_atapi_device);
  78
  79void ide_init_pc(struct ide_atapi_pc *pc)
  80{
  81        memset(pc, 0, sizeof(*pc));
  82}
  83EXPORT_SYMBOL_GPL(ide_init_pc);
  84
  85/*
  86 * Add a special packet command request to the tail of the request queue,
  87 * and wait for it to be serviced.
  88 */
  89int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
  90                      struct ide_atapi_pc *pc, void *buf, unsigned int bufflen)
  91{
  92        struct request *rq;
  93        int error;
  94
  95        rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
  96        ide_req(rq)->type = ATA_PRIV_MISC;
  97        ide_req(rq)->special = pc;
  98
  99        if (buf && bufflen) {
 100                error = blk_rq_map_kern(drive->queue, rq, buf, bufflen,
 101                                        GFP_NOIO);
 102                if (error)
 103                        goto put_req;
 104        }
 105
 106        memcpy(scsi_req(rq)->cmd, pc->c, 12);
 107        if (drive->media == ide_tape)
 108                scsi_req(rq)->cmd[13] = REQ_IDETAPE_PC1;
 109        blk_execute_rq(drive->queue, disk, rq, 0);
 110        error = scsi_req(rq)->result ? -EIO : 0;
 111put_req:
 112        blk_put_request(rq);
 113        return error;
 114}
 115EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
 116
 117int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
 118{
 119        struct ide_atapi_pc pc;
 120
 121        ide_init_pc(&pc);
 122        pc.c[0] = TEST_UNIT_READY;
 123
 124        return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
 125}
 126EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
 127
 128int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
 129{
 130        struct ide_atapi_pc pc;
 131
 132        ide_init_pc(&pc);
 133        pc.c[0] = START_STOP;
 134        pc.c[4] = start;
 135
 136        if (drive->media == ide_tape)
 137                pc.flags |= PC_FLAG_WAIT_FOR_DSC;
 138
 139        return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
 140}
 141EXPORT_SYMBOL_GPL(ide_do_start_stop);
 142
 143int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
 144{
 145        struct ide_atapi_pc pc;
 146
 147        if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
 148                return 0;
 149
 150        ide_init_pc(&pc);
 151        pc.c[0] = ALLOW_MEDIUM_REMOVAL;
 152        pc.c[4] = on;
 153
 154        return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
 155}
 156EXPORT_SYMBOL_GPL(ide_set_media_lock);
 157
 158void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
 159{
 160        ide_init_pc(pc);
 161        pc->c[0] = REQUEST_SENSE;
 162        if (drive->media == ide_floppy) {
 163                pc->c[4] = 255;
 164                pc->req_xfer = 18;
 165        } else {
 166                pc->c[4] = 20;
 167                pc->req_xfer = 20;
 168        }
 169}
 170EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
 171
 172void ide_prep_sense(ide_drive_t *drive, struct request *rq)
 173{
 174        struct request_sense *sense = &drive->sense_data;
 175        struct request *sense_rq;
 176        struct scsi_request *req;
 177        unsigned int cmd_len, sense_len;
 178        int err;
 179
 180        switch (drive->media) {
 181        case ide_floppy:
 182                cmd_len = 255;
 183                sense_len = 18;
 184                break;
 185        case ide_tape:
 186                cmd_len = 20;
 187                sense_len = 20;
 188                break;
 189        default:
 190                cmd_len = 18;
 191                sense_len = 18;
 192        }
 193
 194        BUG_ON(sense_len > sizeof(*sense));
 195
 196        if (ata_sense_request(rq) || drive->sense_rq_armed)
 197                return;
 198
 199        sense_rq = drive->sense_rq;
 200        if (!sense_rq) {
 201                sense_rq = blk_mq_alloc_request(drive->queue, REQ_OP_DRV_IN,
 202                                        BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
 203                drive->sense_rq = sense_rq;
 204        }
 205        req = scsi_req(sense_rq);
 206
 207        memset(sense, 0, sizeof(*sense));
 208
 209        scsi_req_init(req);
 210
 211        err = blk_rq_map_kern(drive->queue, sense_rq, sense, sense_len,
 212                              GFP_NOIO);
 213        if (unlikely(err)) {
 214                if (printk_ratelimit())
 215                        printk(KERN_WARNING PFX "%s: failed to map sense "
 216                                            "buffer\n", drive->name);
 217                blk_mq_free_request(sense_rq);
 218                drive->sense_rq = NULL;
 219                return;
 220        }
 221
 222        sense_rq->rq_disk = rq->rq_disk;
 223        sense_rq->cmd_flags = REQ_OP_DRV_IN;
 224        ide_req(sense_rq)->type = ATA_PRIV_SENSE;
 225        sense_rq->rq_flags |= RQF_PREEMPT;
 226
 227        req->cmd[0] = GPCMD_REQUEST_SENSE;
 228        req->cmd[4] = cmd_len;
 229        if (drive->media == ide_tape)
 230                req->cmd[13] = REQ_IDETAPE_PC1;
 231
 232        drive->sense_rq_armed = true;
 233}
 234EXPORT_SYMBOL_GPL(ide_prep_sense);
 235
 236int ide_queue_sense_rq(ide_drive_t *drive, void *special)
 237{
 238        struct request *sense_rq = drive->sense_rq;
 239
 240        /* deferred failure from ide_prep_sense() */
 241        if (!drive->sense_rq_armed) {
 242                printk(KERN_WARNING PFX "%s: error queuing a sense request\n",
 243                       drive->name);
 244                return -ENOMEM;
 245        }
 246
 247        ide_req(sense_rq)->special = special;
 248        drive->sense_rq_armed = false;
 249
 250        drive->hwif->rq = NULL;
 251
 252        ide_insert_request_head(drive, sense_rq);
 253        return 0;
 254}
 255EXPORT_SYMBOL_GPL(ide_queue_sense_rq);
 256
 257/*
 258 * Called when an error was detected during the last packet command.
 259 * We queue a request sense packet command at the head of the request
 260 * queue.
 261 */
 262void ide_retry_pc(ide_drive_t *drive)
 263{
 264        struct request *failed_rq = drive->hwif->rq;
 265        struct request *sense_rq = drive->sense_rq;
 266        struct ide_atapi_pc *pc = &drive->request_sense_pc;
 267
 268        (void)ide_read_error(drive);
 269
 270        /* init pc from sense_rq */
 271        ide_init_pc(pc);
 272        memcpy(pc->c, scsi_req(sense_rq)->cmd, 12);
 273
 274        if (drive->media == ide_tape)
 275                drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
 276
 277        /*
 278         * Push back the failed request and put request sense on top
 279         * of it.  The failed command will be retried after sense data
 280         * is acquired.
 281         */
 282        drive->hwif->rq = NULL;
 283        ide_requeue_and_plug(drive, failed_rq);
 284        if (ide_queue_sense_rq(drive, pc))
 285                ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(failed_rq));
 286}
 287EXPORT_SYMBOL_GPL(ide_retry_pc);
 288
 289int ide_cd_expiry(ide_drive_t *drive)
 290{
 291        struct request *rq = drive->hwif->rq;
 292        unsigned long wait = 0;
 293
 294        debug_log("%s: scsi_req(rq)->cmd[0]: 0x%x\n", __func__, scsi_req(rq)->cmd[0]);
 295
 296        /*
 297         * Some commands are *slow* and normally take a long time to complete.
 298         * Usually we can use the ATAPI "disconnect" to bypass this, but not all
 299         * commands/drives support that. Let ide_timer_expiry keep polling us
 300         * for these.
 301         */
 302        switch (scsi_req(rq)->cmd[0]) {
 303        case GPCMD_BLANK:
 304        case GPCMD_FORMAT_UNIT:
 305        case GPCMD_RESERVE_RZONE_TRACK:
 306        case GPCMD_CLOSE_TRACK:
 307        case GPCMD_FLUSH_CACHE:
 308                wait = ATAPI_WAIT_PC;
 309                break;
 310        default:
 311                if (!(rq->rq_flags & RQF_QUIET))
 312                        printk(KERN_INFO PFX "cmd 0x%x timed out\n",
 313                                         scsi_req(rq)->cmd[0]);
 314                wait = 0;
 315                break;
 316        }
 317        return wait;
 318}
 319EXPORT_SYMBOL_GPL(ide_cd_expiry);
 320
 321int ide_cd_get_xferlen(struct request *rq)
 322{
 323        switch (req_op(rq)) {
 324        default:
 325                return 32768;
 326        case REQ_OP_SCSI_IN:
 327        case REQ_OP_SCSI_OUT:
 328                return blk_rq_bytes(rq);
 329        case REQ_OP_DRV_IN:
 330        case REQ_OP_DRV_OUT:
 331                switch (ide_req(rq)->type) {
 332                case ATA_PRIV_PC:
 333                case ATA_PRIV_SENSE:
 334                        return blk_rq_bytes(rq);
 335                default:
 336                        return 0;
 337                }
 338        }
 339}
 340EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
 341
 342void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
 343{
 344        struct ide_taskfile tf;
 345
 346        drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
 347                                     IDE_VALID_LBAM | IDE_VALID_LBAH);
 348
 349        *bcount = (tf.lbah << 8) | tf.lbam;
 350        *ireason = tf.nsect & 3;
 351}
 352EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
 353
 354/*
 355 * Check the contents of the interrupt reason register and attempt to recover if
 356 * there are problems.
 357 *
 358 * Returns:
 359 * - 0 if everything's ok
 360 * - 1 if the request has to be terminated.
 361 */
 362int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len,
 363                      int ireason, int rw)
 364{
 365        ide_hwif_t *hwif = drive->hwif;
 366
 367        debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw);
 368
 369        if (ireason == (!rw << 1))
 370                return 0;
 371        else if (ireason == (rw << 1)) {
 372                printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
 373                                drive->name, __func__);
 374
 375                if (dev_is_idecd(drive))
 376                        ide_pad_transfer(drive, rw, len);
 377        } else if (!rw && ireason == ATAPI_COD) {
 378                if (dev_is_idecd(drive)) {
 379                        /*
 380                         * Some drives (ASUS) seem to tell us that status info
 381                         * is available.  Just get it and ignore.
 382                         */
 383                        (void)hwif->tp_ops->read_status(hwif);
 384                        return 0;
 385                }
 386        } else {
 387                if (ireason & ATAPI_COD)
 388                        printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name,
 389                                        __func__);
 390
 391                /* drive wants a command packet, or invalid ireason... */
 392                printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
 393                                drive->name, __func__, ireason);
 394        }
 395
 396        if (dev_is_idecd(drive) && ata_pc_request(rq))
 397                rq->rq_flags |= RQF_FAILED;
 398
 399        return 1;
 400}
 401EXPORT_SYMBOL_GPL(ide_check_ireason);
 402
 403/*
 404 * This is the usual interrupt handler which will be called during a packet
 405 * command.  We will transfer some of the data (as requested by the drive)
 406 * and will re-point interrupt handler to us.
 407 */
 408static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
 409{
 410        struct ide_atapi_pc *pc = drive->pc;
 411        ide_hwif_t *hwif = drive->hwif;
 412        struct ide_cmd *cmd = &hwif->cmd;
 413        struct request *rq = hwif->rq;
 414        const struct ide_tp_ops *tp_ops = hwif->tp_ops;
 415        unsigned int timeout, done;
 416        u16 bcount;
 417        u8 stat, ireason, dsc = 0;
 418        u8 write = !!(pc->flags & PC_FLAG_WRITING);
 419
 420        debug_log("Enter %s - interrupt handler\n", __func__);
 421
 422        timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
 423                                               : WAIT_TAPE_CMD;
 424
 425        /* Clear the interrupt */
 426        stat = tp_ops->read_status(hwif);
 427
 428        if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
 429                int rc;
 430
 431                drive->waiting_for_dma = 0;
 432                rc = hwif->dma_ops->dma_end(drive);
 433                ide_dma_unmap_sg(drive, cmd);
 434
 435                if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
 436                        if (drive->media == ide_floppy)
 437                                printk(KERN_ERR PFX "%s: DMA %s error\n",
 438                                        drive->name, rq_data_dir(pc->rq)
 439                                                     ? "write" : "read");
 440                        pc->flags |= PC_FLAG_DMA_ERROR;
 441                } else
 442                        scsi_req(rq)->resid_len = 0;
 443                debug_log("%s: DMA finished\n", drive->name);
 444        }
 445
 446        /* No more interrupts */
 447        if ((stat & ATA_DRQ) == 0) {
 448                int uptodate;
 449                blk_status_t error;
 450
 451                debug_log("Packet command completed, %d bytes transferred\n",
 452                          blk_rq_bytes(rq));
 453
 454                pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
 455
 456                local_irq_enable_in_hardirq();
 457
 458                if (drive->media == ide_tape &&
 459                    (stat & ATA_ERR) && scsi_req(rq)->cmd[0] == REQUEST_SENSE)
 460                        stat &= ~ATA_ERR;
 461
 462                if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
 463                        /* Error detected */
 464                        debug_log("%s: I/O error\n", drive->name);
 465
 466                        if (drive->media != ide_tape)
 467                                scsi_req(pc->rq)->result++;
 468
 469                        if (scsi_req(rq)->cmd[0] == REQUEST_SENSE) {
 470                                printk(KERN_ERR PFX "%s: I/O error in request "
 471                                                "sense command\n", drive->name);
 472                                return ide_do_reset(drive);
 473                        }
 474
 475                        debug_log("[cmd %x]: check condition\n", scsi_req(rq)->cmd[0]);
 476
 477                        /* Retry operation */
 478                        ide_retry_pc(drive);
 479
 480                        /* queued, but not started */
 481                        return ide_stopped;
 482                }
 483                pc->error = 0;
 484
 485                if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
 486                        dsc = 1;
 487
 488                /*
 489                 * ->pc_callback() might change rq->data_len for
 490                 * residual count, cache total length.
 491                 */
 492                done = blk_rq_bytes(rq);
 493
 494                /* Command finished - Call the callback function */
 495                uptodate = drive->pc_callback(drive, dsc);
 496
 497                if (uptodate == 0)
 498                        drive->failed_pc = NULL;
 499
 500                if (ata_misc_request(rq)) {
 501                        scsi_req(rq)->result = 0;
 502                        error = BLK_STS_OK;
 503                } else {
 504
 505                        if (blk_rq_is_passthrough(rq) && uptodate <= 0) {
 506                                if (scsi_req(rq)->result == 0)
 507                                        scsi_req(rq)->result = -EIO;
 508                        }
 509
 510                        error = uptodate ? BLK_STS_OK : BLK_STS_IOERR;
 511                }
 512
 513                ide_complete_rq(drive, error, blk_rq_bytes(rq));
 514                return ide_stopped;
 515        }
 516
 517        if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
 518                pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
 519                printk(KERN_ERR PFX "%s: The device wants to issue more "
 520                                "interrupts in DMA mode\n", drive->name);
 521                ide_dma_off(drive);
 522                return ide_do_reset(drive);
 523        }
 524
 525        /* Get the number of bytes to transfer on this interrupt. */
 526        ide_read_bcount_and_ireason(drive, &bcount, &ireason);
 527
 528        if (ide_check_ireason(drive, rq, bcount, ireason, write))
 529                return ide_do_reset(drive);
 530
 531        done = min_t(unsigned int, bcount, cmd->nleft);
 532        ide_pio_bytes(drive, cmd, write, done);
 533
 534        /* Update transferred byte count */
 535        scsi_req(rq)->resid_len -= done;
 536
 537        bcount -= done;
 538
 539        if (bcount)
 540                ide_pad_transfer(drive, write, bcount);
 541
 542        debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
 543                  scsi_req(rq)->cmd[0], done, bcount, scsi_req(rq)->resid_len);
 544
 545        /* And set the interrupt handler again */
 546        ide_set_handler(drive, ide_pc_intr, timeout);
 547        return ide_started;
 548}
 549
 550static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
 551                                u16 bcount, u8 dma)
 552{
 553        cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
 554        cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
 555                            IDE_VALID_FEATURE | valid_tf;
 556        cmd->tf.command = ATA_CMD_PACKET;
 557        cmd->tf.feature = dma;          /* Use PIO/DMA */
 558        cmd->tf.lbam    = bcount & 0xff;
 559        cmd->tf.lbah    = (bcount >> 8) & 0xff;
 560}
 561
 562static u8 ide_read_ireason(ide_drive_t *drive)
 563{
 564        struct ide_taskfile tf;
 565
 566        drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
 567
 568        return tf.nsect & 3;
 569}
 570
 571static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
 572{
 573        int retries = 100;
 574
 575        while (retries-- && ((ireason & ATAPI_COD) == 0 ||
 576                (ireason & ATAPI_IO))) {
 577                printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
 578                                "a packet command, retrying\n", drive->name);
 579                udelay(100);
 580                ireason = ide_read_ireason(drive);
 581                if (retries == 0) {
 582                        printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
 583                                        " a packet command, ignoring\n",
 584                                        drive->name);
 585                        ireason |= ATAPI_COD;
 586                        ireason &= ~ATAPI_IO;
 587                }
 588        }
 589
 590        return ireason;
 591}
 592
 593static int ide_delayed_transfer_pc(ide_drive_t *drive)
 594{
 595        /* Send the actual packet */
 596        drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
 597
 598        /* Timeout for the packet command */
 599        return WAIT_FLOPPY_CMD;
 600}
 601
 602static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
 603{
 604        struct ide_atapi_pc *uninitialized_var(pc);
 605        ide_hwif_t *hwif = drive->hwif;
 606        struct request *rq = hwif->rq;
 607        ide_expiry_t *expiry;
 608        unsigned int timeout;
 609        int cmd_len;
 610        ide_startstop_t startstop;
 611        u8 ireason;
 612
 613        if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
 614                printk(KERN_ERR PFX "%s: Strange, packet command initiated yet "
 615                                "DRQ isn't asserted\n", drive->name);
 616                return startstop;
 617        }
 618
 619        if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
 620                if (drive->dma)
 621                        drive->waiting_for_dma = 1;
 622        }
 623
 624        if (dev_is_idecd(drive)) {
 625                /* ATAPI commands get padded out to 12 bytes minimum */
 626                cmd_len = COMMAND_SIZE(scsi_req(rq)->cmd[0]);
 627                if (cmd_len < ATAPI_MIN_CDB_BYTES)
 628                        cmd_len = ATAPI_MIN_CDB_BYTES;
 629
 630                timeout = rq->timeout;
 631                expiry  = ide_cd_expiry;
 632        } else {
 633                pc = drive->pc;
 634
 635                cmd_len = ATAPI_MIN_CDB_BYTES;
 636
 637                /*
 638                 * If necessary schedule the packet transfer to occur 'timeout'
 639                 * milliseconds later in ide_delayed_transfer_pc() after the
 640                 * device says it's ready for a packet.
 641                 */
 642                if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
 643                        timeout = drive->pc_delay;
 644                        expiry = &ide_delayed_transfer_pc;
 645                } else {
 646                        timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
 647                                                               : WAIT_TAPE_CMD;
 648                        expiry = NULL;
 649                }
 650
 651                ireason = ide_read_ireason(drive);
 652                if (drive->media == ide_tape)
 653                        ireason = ide_wait_ireason(drive, ireason);
 654
 655                if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
 656                        printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
 657                                "issuing a packet command\n", drive->name);
 658
 659                        return ide_do_reset(drive);
 660                }
 661        }
 662
 663        hwif->expiry = expiry;
 664
 665        /* Set the interrupt routine */
 666        ide_set_handler(drive,
 667                        (dev_is_idecd(drive) ? drive->irq_handler
 668                                             : ide_pc_intr),
 669                        timeout);
 670
 671        /* Send the actual packet */
 672        if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
 673                hwif->tp_ops->output_data(drive, NULL, scsi_req(rq)->cmd, cmd_len);
 674
 675        /* Begin DMA, if necessary */
 676        if (dev_is_idecd(drive)) {
 677                if (drive->dma)
 678                        hwif->dma_ops->dma_start(drive);
 679        } else {
 680                if (pc->flags & PC_FLAG_DMA_OK) {
 681                        pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
 682                        hwif->dma_ops->dma_start(drive);
 683                }
 684        }
 685
 686        return ide_started;
 687}
 688
 689ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
 690{
 691        struct ide_atapi_pc *pc;
 692        ide_hwif_t *hwif = drive->hwif;
 693        ide_expiry_t *expiry = NULL;
 694        struct request *rq = hwif->rq;
 695        unsigned int timeout, bytes;
 696        u16 bcount;
 697        u8 valid_tf;
 698        u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
 699
 700        if (dev_is_idecd(drive)) {
 701                valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
 702                bcount = ide_cd_get_xferlen(rq);
 703                expiry = ide_cd_expiry;
 704                timeout = ATAPI_WAIT_PC;
 705
 706                if (drive->dma)
 707                        drive->dma = !ide_dma_prepare(drive, cmd);
 708        } else {
 709                pc = drive->pc;
 710
 711                valid_tf = IDE_VALID_DEVICE;
 712                bytes = blk_rq_bytes(rq);
 713                bcount = ((drive->media == ide_tape) ? bytes
 714                                                     : min_t(unsigned int,
 715                                                             bytes, 63 * 1024));
 716
 717                /* We haven't transferred any data yet */
 718                scsi_req(rq)->resid_len = bcount;
 719
 720                if (pc->flags & PC_FLAG_DMA_ERROR) {
 721                        pc->flags &= ~PC_FLAG_DMA_ERROR;
 722                        ide_dma_off(drive);
 723                }
 724
 725                if (pc->flags & PC_FLAG_DMA_OK)
 726                        drive->dma = !ide_dma_prepare(drive, cmd);
 727
 728                if (!drive->dma)
 729                        pc->flags &= ~PC_FLAG_DMA_OK;
 730
 731                timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
 732                                                       : WAIT_TAPE_CMD;
 733        }
 734
 735        ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
 736
 737        (void)do_rw_taskfile(drive, cmd);
 738
 739        if (drq_int) {
 740                if (drive->dma)
 741                        drive->waiting_for_dma = 0;
 742                hwif->expiry = expiry;
 743        }
 744
 745        ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
 746
 747        return drq_int ? ide_started : ide_transfer_pc(drive);
 748}
 749EXPORT_SYMBOL_GPL(ide_issue_pc);
 750