block: add rq->resid_len
[safe/jmp/linux-2.6] / drivers / ide / ide-cd.c
index 692fd45..8bbe222 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 1994-1996   Scott Snyder <snyder@fnald0.fnal.gov>
  * Copyright (C) 1996-1998   Erik Andersen <andersee@debian.org>
  * Copyright (C) 1998-2000   Jens Axboe <axboe@suse.de>
- * Copyright (C) 2005, 2007  Bartlomiej Zolnierkiewicz
+ * Copyright (C) 2005, 2007-2009  Bartlomiej Zolnierkiewicz
  *
  * May be copied or modified under the terms of the GNU General Public
  * License.  See linux/COPYING for more information.
  * See Documentation/cdrom/ide-cd for usage information.
  *
  * Suggestions are welcome. Patches that work are more welcome though. ;-)
- * For those wishing to work on this driver, please be sure you download
- * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
- * (SFF-8020i rev 2.6) standards. These documents can be obtained by
- * anonymous ftp from:
- * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
- * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
+ *
+ * Documentation:
+ *     Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards.
  *
  * For historical changelog please see:
  *     Documentation/ide/ChangeLog.ide-cd.1994-2004
 
 #include "ide-cd.h"
 
-#define IDECD_DEBUG_LOG                1
-
-#if IDECD_DEBUG_LOG
-#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, args)
-#else
-#define ide_debug_log(lvl, fmt, args...) do {} while (0)
-#endif
-
 static DEFINE_MUTEX(idecd_ref_mutex);
 
-static void ide_cd_release(struct kref *);
+static void ide_cd_release(struct device *);
 
 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
 {
@@ -75,7 +64,7 @@ static struct cdrom_info *ide_cd_get(struct gendisk *disk)
                if (ide_device_get(cd->drive))
                        cd = NULL;
                else
-                       kref_get(&cd->kref);
+                       get_device(&cd->dev);
 
        }
        mutex_unlock(&idecd_ref_mutex);
@@ -87,7 +76,7 @@ static void ide_cd_put(struct cdrom_info *cd)
        ide_drive_t *drive = cd->drive;
 
        mutex_lock(&idecd_ref_mutex);
-       kref_put(&cd->kref, ide_cd_release);
+       put_device(&cd->dev);
        ide_device_put(drive);
        mutex_unlock(&idecd_ref_mutex);
 }
@@ -108,8 +97,7 @@ static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
 {
        int log = 0;
 
-       ide_debug_log(IDE_DBG_SENSE, "Call %s, sense_key: 0x%x\n", __func__,
-                     sense->sense_key);
+       ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key);
 
        if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
                return 0;
@@ -159,13 +147,12 @@ static void cdrom_analyze_sense_data(ide_drive_t *drive,
        unsigned long bio_sectors;
        struct cdrom_info *info = drive->driver_data;
 
-       ide_debug_log(IDE_DBG_SENSE, "Call %s, error_code: 0x%x, "
-                       "sense_key: 0x%x\n", __func__, sense->error_code,
-                       sense->sense_key);
+       ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x",
+                                    sense->error_code, sense->sense_key);
 
        if (failed_command)
-               ide_debug_log(IDE_DBG_SENSE, "%s: failed cmd: 0x%x\n",
-                               __func__, failed_command->cmd[0]);
+               ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x",
+                                            failed_command->cmd[0]);
 
        if (!cdrom_log_sense(drive, failed_command, sense))
                return;
@@ -202,6 +189,14 @@ static void cdrom_analyze_sense_data(ide_drive_t *drive,
                        bio_sectors = max(bio_sectors(failed_command->bio), 4U);
                        sector &= ~(bio_sectors - 1);
 
+                       /*
+                        * The SCSI specification allows for the value
+                        * returned by READ CAPACITY to be up to 75 2K
+                        * sectors past the last readable block.
+                        * Therefore, if we hit a medium error within the
+                        * last 75 2K sectors, we decrease the saved size
+                        * value.
+                        */
                        if (sector < get_capacity(info->disk) &&
                            drive->probed_capacity - sector < 4 * 75)
                                set_capacity(info->disk, sector);
@@ -211,135 +206,92 @@ static void cdrom_analyze_sense_data(ide_drive_t *drive,
        ide_cd_log_error(drive->name, failed_command, sense);
 }
 
-static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
-                                     struct request *failed_command)
+static void ide_cd_complete_failed_rq(ide_drive_t *drive, struct request *rq)
 {
-       struct cdrom_info *info         = drive->driver_data;
-       struct request *rq              = &info->request_sense_request;
-
-       ide_debug_log(IDE_DBG_SENSE, "Call %s\n", __func__);
-
-       if (sense == NULL)
-               sense = &info->sense_data;
+       /*
+        * For REQ_TYPE_SENSE, "rq->special" points to the original
+        * failed request.  Also, the sense data should be read
+        * directly from rq which might be different from the original
+        * sense buffer if it got copied during mapping.
+        */
+       struct request *failed = (struct request *)rq->special;
+       void *sense = bio_data(rq->bio);
 
-       /* stuff the sense request in front of our current request */
-       blk_rq_init(NULL, rq);
-       rq->cmd_type = REQ_TYPE_ATA_PC;
-       rq->rq_disk = info->disk;
+       if (failed) {
+               if (failed->sense) {
+                       /*
+                        * Sense is always read into drive->sense_data.
+                        * Copy back if the failed request has its
+                        * sense pointer set.
+                        */
+                       memcpy(failed->sense, sense, 18);
+                       sense = failed->sense;
+                       failed->sense_len = rq->sense_len;
+               }
+               cdrom_analyze_sense_data(drive, failed, sense);
 
-       rq->data = sense;
-       rq->cmd[0] = GPCMD_REQUEST_SENSE;
-       rq->cmd[4] = 18;
-       rq->data_len = 18;
+               if (ide_end_rq(drive, failed, -EIO, blk_rq_bytes(failed)))
+                       BUG();
+       } else
+               cdrom_analyze_sense_data(drive, NULL, sense);
+}
 
-       rq->cmd_type = REQ_TYPE_SENSE;
-       rq->cmd_flags |= REQ_PREEMPT;
 
-       /* NOTE! Save the failed command in "rq->buffer" */
-       rq->buffer = (void *) failed_command;
+/*
+ * Allow the drive 5 seconds to recover; some devices will return NOT_READY
+ * while flushing data from cache.
+ *
+ * returns: 0 failed (write timeout expired)
+ *         1 success
+ */
+static int ide_cd_breathe(ide_drive_t *drive, struct request *rq)
+{
 
-       if (failed_command)
-               ide_debug_log(IDE_DBG_SENSE, "failed_cmd: 0x%x\n",
-                             failed_command->cmd[0]);
+       struct cdrom_info *info = drive->driver_data;
 
-       ide_do_drive_cmd(drive, rq);
-}
+       if (!rq->errors)
+               info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
 
-static void cdrom_end_request(ide_drive_t *drive, int uptodate)
-{
-       struct request *rq = HWGROUP(drive)->rq;
-       int nsectors = rq->hard_cur_sectors;
+       rq->errors = 1;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s, cmd: 0x%x, uptodate: 0x%x, "
-                     "nsectors: %d\n", __func__, rq->cmd[0], uptodate,
-                     nsectors);
+       if (time_after(jiffies, info->write_timeout))
+               return 0;
+       else {
+               struct request_queue *q = drive->queue;
+               unsigned long flags;
 
-       if (blk_sense_request(rq) && uptodate) {
                /*
-                * For REQ_TYPE_SENSE, "rq->buffer" points to the original
-                * failed request
+                * take a breather relying on the unplug timer to kick us again
                 */
-               struct request *failed = (struct request *) rq->buffer;
-               struct cdrom_info *info = drive->driver_data;
-               void *sense = &info->sense_data;
-
-               if (failed) {
-                       if (failed->sense) {
-                               sense = failed->sense;
-                               failed->sense_len = rq->sense_len;
-                       }
-                       cdrom_analyze_sense_data(drive, failed, sense);
-                       /*
-                        * now end the failed request
-                        */
-                       if (blk_fs_request(failed)) {
-                               if (ide_end_dequeued_request(drive, failed, 0,
-                                               failed->hard_nr_sectors))
-                                       BUG();
-                       } else {
-                               if (blk_end_request(failed, -EIO,
-                                                   failed->data_len))
-                                       BUG();
-                       }
-               } else
-                       cdrom_analyze_sense_data(drive, NULL, sense);
-       }
 
-       if (!rq->current_nr_sectors && blk_fs_request(rq))
-               uptodate = 1;
-       /* make sure it's fully ended */
-       if (blk_pc_request(rq))
-               nsectors = (rq->data_len + 511) >> 9;
-       if (!nsectors)
-               nsectors = 1;
-
-       ide_debug_log(IDE_DBG_FUNC, "Exit %s, uptodate: 0x%x, nsectors: %d\n",
-                     __func__, uptodate, nsectors);
-
-       ide_end_request(drive, uptodate, nsectors);
-}
+               spin_lock_irqsave(q->queue_lock, flags);
+               blk_plug_device(q);
+               spin_unlock_irqrestore(q->queue_lock, flags);
 
-static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 st)
-{
-       if (st & 0x80)
-               return;
-       ide_dump_status(drive, msg, st);
+               return 1;
+       }
 }
 
-/*
+/**
  * Returns:
  * 0: if the request should be continued.
- * 1: if the request was ended.
+ * 1: if the request will be going through error recovery.
+ * 2: if the request should be ended.
  */
-static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
+static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
 {
        ide_hwif_t *hwif = drive->hwif;
-       ide_hwgroup_t *hwgroup = hwif->hwgroup;
-       struct request *rq = hwgroup->rq;
-       int stat, err, sense_key;
-
-       /* check for errors */
-       stat = hwif->tp_ops->read_status(hwif);
-
-       if (stat_ret)
-               *stat_ret = stat;
-
-       if (OK_STAT(stat, good_stat, BAD_R_STAT))
-               return 0;
+       struct request *rq = hwif->rq;
+       int err, sense_key, do_end_request = 0;
+       u8 quiet = rq->cmd_flags & REQ_QUIET;
 
        /* get the IDE error register */
        err = ide_read_error(drive);
        sense_key = err >> 4;
 
-       if (rq == NULL) {
-               printk(KERN_ERR PFX "%s: missing rq in %s\n",
-                               drive->name, __func__);
-               return 1;
-       }
-
-       ide_debug_log(IDE_DBG_RQ, "%s: stat: 0x%x, good_stat: 0x%x, "
-                     "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x, err: 0x%x\n",
-                     __func__, stat, good_stat, rq->cmd[0], rq->cmd_type, err);
+       ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, rq->cmd_type: 0x%x, err: 0x%x, "
+                                 "stat 0x%x",
+                                 rq->cmd[0], rq->cmd_type, err, stat);
 
        if (blk_sense_request(rq)) {
                /*
@@ -348,157 +300,107 @@ static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
                 * Just give up.
                 */
                rq->cmd_flags |= REQ_FAILED;
-               cdrom_end_request(drive, 0);
-               ide_error(drive, "request sense failure", stat);
-               return 1;
+               return 2;
+       }
 
-       } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
-               /* All other functions, except for READ. */
+       /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */
+       if (blk_pc_request(rq) && !rq->errors)
+               rq->errors = SAM_STAT_CHECK_CONDITION;
 
-               /*
-                * if we have an error, pass back CHECK_CONDITION as the
-                * scsi status byte
-                */
-               if (blk_pc_request(rq) && !rq->errors)
-                       rq->errors = SAM_STAT_CHECK_CONDITION;
+       if (blk_noretry_request(rq))
+               do_end_request = 1;
 
-               /* check for tray open */
-               if (sense_key == NOT_READY) {
-                       cdrom_saw_media_change(drive);
-               } else if (sense_key == UNIT_ATTENTION) {
-                       /* check for media change */
+       switch (sense_key) {
+       case NOT_READY:
+               if (blk_fs_request(rq) && rq_data_dir(rq) == WRITE) {
+                       if (ide_cd_breathe(drive, rq))
+                               return 1;
+               } else {
                        cdrom_saw_media_change(drive);
-                       return 0;
-               } else if (sense_key == ILLEGAL_REQUEST &&
-                          rq->cmd[0] == GPCMD_START_STOP_UNIT) {
-                       /*
-                        * Don't print error message for this condition--
-                        * SFF8090i indicates that 5/24/00 is the correct
-                        * response to a request to close the tray if the
-                        * drive doesn't have that capability.
-                        * cdrom_log_sense() knows this!
-                        */
-               } else if (!(rq->cmd_flags & REQ_QUIET)) {
-                       /* otherwise, print an error */
-                       ide_dump_status(drive, "packet command error", stat);
+
+                       if (blk_fs_request(rq) && !quiet)
+                               printk(KERN_ERR PFX "%s: tray open\n",
+                                       drive->name);
                }
+               do_end_request = 1;
+               break;
+       case UNIT_ATTENTION:
+               cdrom_saw_media_change(drive);
 
-               rq->cmd_flags |= REQ_FAILED;
+               if (blk_fs_request(rq) == 0)
+                       return 0;
 
                /*
-                * instead of playing games with moving completions around,
-                * remove failed request completely and end it when the
-                * request sense has completed
+                * Arrange to retry the request but be sure to give up if we've
+                * retried too many times.
                 */
-               goto end_request;
-
-       } else if (blk_fs_request(rq)) {
-               int do_end_request = 0;
-
-               /* handle errors from READ and WRITE requests */
-
-               if (blk_noretry_request(rq))
+               if (++rq->errors > ERROR_MAX)
                        do_end_request = 1;
-
-               if (sense_key == NOT_READY) {
-                       /* tray open */
-                       if (rq_data_dir(rq) == READ) {
-                               cdrom_saw_media_change(drive);
-
-                               /* fail the request */
-                               printk(KERN_ERR PFX "%s: tray open\n",
-                                               drive->name);
-                               do_end_request = 1;
-                       } else {
-                               struct cdrom_info *info = drive->driver_data;
-
-                               /*
-                                * Allow the drive 5 seconds to recover, some
-                                * devices will return this error while flushing
-                                * data from cache.
-                                */
-                               if (!rq->errors)
-                                       info->write_timeout = jiffies +
-                                                       ATAPI_WAIT_WRITE_BUSY;
-                               rq->errors = 1;
-                               if (time_after(jiffies, info->write_timeout))
-                                       do_end_request = 1;
-                               else {
-                                       struct request_queue *q = drive->queue;
-                                       unsigned long flags;
-
-                                       /*
-                                        * take a breather relying on the unplug
-                                        * timer to kick us again
-                                        */
-                                       spin_lock_irqsave(q->queue_lock, flags);
-                                       blk_plug_device(q);
-                                       spin_unlock_irqrestore(q->queue_lock, flags);
-
-                                       return 1;
-                               }
-                       }
-               } else if (sense_key == UNIT_ATTENTION) {
-                       /* media change */
-                       cdrom_saw_media_change(drive);
-
-                       /*
-                        * Arrange to retry the request but be sure to give up
-                        * if we've retried too many times.
-                        */
-                       if (++rq->errors > ERROR_MAX)
-                               do_end_request = 1;
-               } else if (sense_key == ILLEGAL_REQUEST ||
-                          sense_key == DATA_PROTECT) {
-                       /*
-                        * No point in retrying after an illegal request or data
-                        * protect error.
-                        */
-                       ide_dump_status_no_sense(drive, "command error", stat);
-                       do_end_request = 1;
-               } else if (sense_key == MEDIUM_ERROR) {
-                       /*
-                        * No point in re-trying a zillion times on a bad
-                        * sector. If we got here the error is not correctable.
-                        */
-                       ide_dump_status_no_sense(drive,
-                                                "media error (bad sector)",
-                                                stat);
-                       do_end_request = 1;
-               } else if (sense_key == BLANK_CHECK) {
-                       /* disk appears blank ?? */
-                       ide_dump_status_no_sense(drive, "media error (blank)",
-                                                stat);
-                       do_end_request = 1;
-               } else if ((err & ~ATA_ABORTED) != 0) {
+               break;
+       case ILLEGAL_REQUEST:
+               /*
+                * Don't print error message for this condition -- SFF8090i
+                * indicates that 5/24/00 is the correct response to a request
+                * to close the tray if the drive doesn't have that capability.
+                *
+                * cdrom_log_sense() knows this!
+                */
+               if (rq->cmd[0] == GPCMD_START_STOP_UNIT)
+                       break;
+               /* fall-through */
+       case DATA_PROTECT:
+               /*
+                * No point in retrying after an illegal request or data
+                * protect error.
+                */
+               if (!quiet)
+                       ide_dump_status(drive, "command error", stat);
+               do_end_request = 1;
+               break;
+       case MEDIUM_ERROR:
+               /*
+                * No point in re-trying a zillion times on a bad sector.
+                * If we got here the error is not correctable.
+                */
+               if (!quiet)
+                       ide_dump_status(drive, "media error "
+                                       "(bad sector)", stat);
+               do_end_request = 1;
+               break;
+       case BLANK_CHECK:
+               /* disk appears blank? */
+               if (!quiet)
+                       ide_dump_status(drive, "media error (blank)",
+                                       stat);
+               do_end_request = 1;
+               break;
+       default:
+               if (blk_fs_request(rq) == 0)
+                       break;
+               if (err & ~ATA_ABORTED) {
                        /* go to the default handler for other errors */
                        ide_error(drive, "cdrom_decode_status", stat);
                        return 1;
-               } else if ((++rq->errors > ERROR_MAX)) {
+               } else if (++rq->errors > ERROR_MAX)
                        /* we've racked up too many retries, abort */
                        do_end_request = 1;
-               }
-
-               /*
-                * End a request through request sense analysis when we have
-                * sense data. We need this in order to perform end of media
-                * processing.
-                */
-               if (do_end_request)
-                       goto end_request;
+       }
 
-               /*
-                * If we got a CHECK_CONDITION status, queue
-                * a request sense command.
-                */
-               if (stat & ATA_ERR)
-                       cdrom_queue_request_sense(drive, NULL, NULL);
-       } else {
-               blk_dump_rq_flags(rq, PFX "bad rq");
-               cdrom_end_request(drive, 0);
+       if (blk_fs_request(rq) == 0) {
+               rq->cmd_flags |= REQ_FAILED;
+               do_end_request = 1;
        }
 
-       /* retry, or handle the next request */
+       /*
+        * End a request through request sense analysis when we have sense data.
+        * We need this in order to perform end of media processing.
+        */
+       if (do_end_request)
+               goto end_request;
+
+       /* if we got a CHECK_CONDITION status, queue a request sense command */
+       if (stat & ATA_ERR)
+               return ide_queue_sense_rq(drive, NULL) ? 2 : 1;
        return 1;
 
 end_request:
@@ -510,140 +412,11 @@ end_request:
                blkdev_dequeue_request(rq);
                spin_unlock_irqrestore(q->queue_lock, flags);
 
-               hwgroup->rq = NULL;
+               hwif->rq = NULL;
 
-               cdrom_queue_request_sense(drive, rq->sense, rq);
+               return ide_queue_sense_rq(drive, rq) ? 2 : 1;
        } else
-               cdrom_end_request(drive, 0);
-
-       return 1;
-}
-
-static int cdrom_timer_expiry(ide_drive_t *drive)
-{
-       struct request *rq = HWGROUP(drive)->rq;
-       unsigned long wait = 0;
-
-       ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd[0]: 0x%x\n", __func__,
-                     rq->cmd[0]);
-
-       /*
-        * Some commands are *slow* and normally take a long time to complete.
-        * Usually we can use the ATAPI "disconnect" to bypass this, but not all
-        * commands/drives support that. Let ide_timer_expiry keep polling us
-        * for these.
-        */
-       switch (rq->cmd[0]) {
-       case GPCMD_BLANK:
-       case GPCMD_FORMAT_UNIT:
-       case GPCMD_RESERVE_RZONE_TRACK:
-       case GPCMD_CLOSE_TRACK:
-       case GPCMD_FLUSH_CACHE:
-               wait = ATAPI_WAIT_PC;
-               break;
-       default:
-               if (!(rq->cmd_flags & REQ_QUIET))
-                       printk(KERN_INFO PFX "cmd 0x%x timed out\n",
-                                        rq->cmd[0]);
-               wait = 0;
-               break;
-       }
-       return wait;
-}
-
-/*
- * Set up the device registers for transferring a packet command on DEV,
- * expecting to later transfer XFERLEN bytes.  HANDLER is the routine
- * which actually transfers the command to the drive.  If this is a
- * drq_interrupt device, this routine will arrange for HANDLER to be
- * called when the interrupt from the drive arrives.  Otherwise, HANDLER
- * will be called immediately after the drive is prepared for the transfer.
- */
-static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
-                                                 int xferlen,
-                                                 ide_handler_t *handler)
-{
-       ide_hwif_t *hwif = drive->hwif;
-
-       ide_debug_log(IDE_DBG_PC, "Call %s, xferlen: %d\n", __func__, xferlen);
-
-       /* FIXME: for Virtual DMA we must check harder */
-       if (drive->dma)
-               drive->dma = !hwif->dma_ops->dma_setup(drive);
-
-       /* set up the controller registers */
-       ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL,
-                          xferlen, drive->dma);
-
-       if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
-               /* waiting for CDB interrupt, not DMA yet. */
-               if (drive->dma)
-                       drive->waiting_for_dma = 0;
-
-               /* packet command */
-               ide_execute_command(drive, ATA_CMD_PACKET, handler,
-                                   ATAPI_WAIT_PC, cdrom_timer_expiry);
-               return ide_started;
-       } else {
-               ide_execute_pkt_cmd(drive);
-
-               return (*handler) (drive);
-       }
-}
-
-/*
- * Send a packet command to DRIVE described by CMD_BUF and CMD_LEN. The device
- * registers must have already been prepared by cdrom_start_packet_command.
- * HANDLER is the interrupt handler to call when the command completes or
- * there's data ready.
- */
-#define ATAPI_MIN_CDB_BYTES 12
-static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *drive,
-                                         struct request *rq,
-                                         ide_handler_t *handler)
-{
-       ide_hwif_t *hwif = drive->hwif;
-       int cmd_len;
-       ide_startstop_t startstop;
-
-       ide_debug_log(IDE_DBG_PC, "Call %s\n", __func__);
-
-       if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
-               /*
-                * Here we should have been called after receiving an interrupt
-                * from the device.  DRQ should how be set.
-                */
-
-               /* check for errors */
-               if (cdrom_decode_status(drive, ATA_DRQ, NULL))
-                       return ide_stopped;
-
-               /* ok, next interrupt will be DMA interrupt */
-               if (drive->dma)
-                       drive->waiting_for_dma = 1;
-       } else {
-               /* otherwise, we must wait for DRQ to get set */
-               if (ide_wait_stat(&startstop, drive, ATA_DRQ,
-                                 ATA_BUSY, WAIT_READY))
-                       return startstop;
-       }
-
-       /* arm the interrupt handler */
-       ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
-
-       /* ATAPI commands get padded out to 12 bytes minimum */
-       cmd_len = COMMAND_SIZE(rq->cmd[0]);
-       if (cmd_len < ATAPI_MIN_CDB_BYTES)
-               cmd_len = ATAPI_MIN_CDB_BYTES;
-
-       /* send the command to the device */
-       hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
-
-       /* start the DMA if need be */
-       if (drive->dma)
-               hwif->dma_ops->dma_start(drive);
-
-       return ide_started;
+               return 2;
 }
 
 /*
@@ -656,8 +429,7 @@ static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
 {
        ide_hwif_t *hwif = drive->hwif;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s, ireason: 0x%x, rw: 0x%x\n",
-                     __func__, ireason, rw);
+       ide_debug_log(IDE_DBG_FUNC, "ireason: 0x%x, rw: 0x%x", ireason, rw);
 
        /*
         * ireason == 0: the drive wants to receive data from us
@@ -688,177 +460,22 @@ static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
        if (rq->cmd_type == REQ_TYPE_ATA_PC)
                rq->cmd_flags |= REQ_FAILED;
 
-       cdrom_end_request(drive, 0);
        return -1;
 }
 
-/*
- * Assume that the drive will always provide data in multiples of at least
- * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
- */
-static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
+static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct ide_cmd *cmd)
 {
-       ide_debug_log(IDE_DBG_FUNC, "Call %s, len: %d\n", __func__, len);
-
-       if ((len % SECTOR_SIZE) == 0)
-               return 0;
-
-       printk(KERN_ERR PFX "%s: %s: Bad transfer size %d\n", drive->name,
-                       __func__, len);
+       struct request *rq = cmd->rq;
 
-       if (drive->atapi_flags & IDE_AFLAG_LIMIT_NFRAMES)
-               printk(KERN_ERR PFX "This drive is not supported by this "
-                               "version of the driver\n");
-       else {
-               printk(KERN_ERR PFX "Trying to limit transfer sizes\n");
-               drive->atapi_flags |= IDE_AFLAG_LIMIT_NFRAMES;
-       }
-
-       return 1;
-}
-
-static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
-
-static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
-                                                struct request *rq)
-{
-       ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd_flags: 0x%x\n", __func__,
-                     rq->cmd_flags);
-
-       if (rq_data_dir(rq) == READ) {
-               unsigned short sectors_per_frame =
-                       queue_hardsect_size(drive->queue) >> SECTOR_BITS;
-               int nskip = rq->sector & (sectors_per_frame - 1);
-
-               /*
-                * If the requested sector doesn't start on a frame boundary,
-                * we must adjust the start of the transfer so that it does,
-                * and remember to skip the first few sectors.
-                *
-                * If the rq->current_nr_sectors field is larger than the size
-                * of the buffer, it will mean that we're to skip a number of
-                * sectors equal to the amount by which rq->current_nr_sectors
-                * is larger than the buffer size.
-                */
-               if (nskip > 0) {
-                       /* sanity check... */
-                       if (rq->current_nr_sectors !=
-                           bio_cur_sectors(rq->bio)) {
-                               printk(KERN_ERR PFX "%s: %s: buffer botch (%u)\n",
-                                               drive->name, __func__,
-                                               rq->current_nr_sectors);
-                               cdrom_end_request(drive, 0);
-                               return ide_stopped;
-                       }
-                       rq->current_nr_sectors += nskip;
-               }
-       }
-
-       /* set up the command */
-       rq->timeout = ATAPI_WAIT_PC;
-
-       return ide_started;
-}
-
-/*
- * Routine to send a read/write packet command to the drive. This is usually
- * called directly from cdrom_start_{read,write}(). However, for drq_interrupt
- * devices, it is called from an interrupt when the drive is ready to accept
- * the command.
- */
-static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
-{
-       struct request *rq = drive->hwif->hwgroup->rq;
-
-       /* send the command to the drive and return */
-       return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
-}
-
-#define IDECD_SEEK_THRESHOLD   (1000)                  /* 1000 blocks */
-#define IDECD_SEEK_TIMER       (5 * WAIT_MIN_SLEEP)    /* 100 ms */
-#define IDECD_SEEK_TIMEOUT     (2 * WAIT_CMD)          /* 20 sec */
-
-static ide_startstop_t cdrom_seek_intr(ide_drive_t *drive)
-{
-       struct cdrom_info *info = drive->driver_data;
-       int stat;
-       static int retry = 10;
-
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
-
-       if (cdrom_decode_status(drive, 0, &stat))
-               return ide_stopped;
-
-       drive->atapi_flags |= IDE_AFLAG_SEEKING;
-
-       if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
-               if (--retry == 0)
-                       drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
-       }
-       return ide_stopped;
-}
-
-static void ide_cd_prepare_seek_request(ide_drive_t *drive, struct request *rq)
-{
-       sector_t frame = rq->sector;
-
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
-
-       sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
-
-       memset(rq->cmd, 0, BLK_MAX_CDB);
-       rq->cmd[0] = GPCMD_SEEK;
-       put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
-
-       rq->timeout = ATAPI_WAIT_PC;
-}
-
-static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive)
-{
-       struct request *rq = drive->hwif->hwgroup->rq;
-
-       return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
-}
-
-/*
- * Fix up a possibly partially-processed request so that we can start it over
- * entirely, or even put it back on the request queue.
- */
-static void ide_cd_restore_request(ide_drive_t *drive, struct request *rq)
-{
-
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
-
-       if (rq->buffer != bio_data(rq->bio)) {
-               sector_t n =
-                       (rq->buffer - (char *)bio_data(rq->bio)) / SECTOR_SIZE;
-
-               rq->buffer = bio_data(rq->bio);
-               rq->nr_sectors += n;
-               rq->sector -= n;
-       }
-       rq->current_nr_sectors = bio_cur_sectors(rq->bio);
-       rq->hard_cur_sectors = rq->current_nr_sectors;
-       rq->hard_nr_sectors = rq->nr_sectors;
-       rq->hard_sector = rq->sector;
-       rq->q->prep_rq_fn(rq->q, rq);
-}
-
-static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct request *rq)
-{
-       ide_debug_log(IDE_DBG_FUNC, "Call %s, rq->cmd[0]: 0x%x\n",
-                     __func__, rq->cmd[0]);
+       ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]);
 
        /*
         * Some of the trailing request sense fields are optional,
         * and some drives don't send them.  Sigh.
         */
        if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
-           rq->data_len > 0 && rq->data_len <= 5)
-               while (rq->data_len > 0) {
-                       *(u8 *)rq->data++ = 0;
-                       --rq->data_len;
-               }
+           cmd->nleft > 0 && cmd->nleft <= 5)
+               cmd->nleft = 0;
 }
 
 int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
@@ -874,9 +491,9 @@ int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
        if (!sense)
                sense = &local_sense;
 
-       ide_debug_log(IDE_DBG_PC, "Call %s, cmd[0]: 0x%x, write: 0x%x, "
-                     "timeout: %d, cmd_flags: 0x%x\n", __func__, cmd[0], write,
-                     timeout, cmd_flags);
+       ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, "
+                                 "cmd_flags: 0x%x",
+                                 cmd[0], write, timeout, cmd_flags);
 
        /* start of retry loop */
        do {
@@ -891,14 +508,18 @@ int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
                rq->cmd_flags |= cmd_flags;
                rq->timeout = timeout;
                if (buffer) {
-                       rq->data = buffer;
-                       rq->data_len = *bufflen;
+                       error = blk_rq_map_kern(drive->queue, rq, buffer,
+                                               *bufflen, GFP_NOIO);
+                       if (error) {
+                               blk_put_request(rq);
+                               return error;
+                       }
                }
 
                error = blk_execute_rq(drive->queue, info->disk, rq, 0);
 
                if (buffer)
-                       *bufflen = rq->data_len;
+                       *bufflen = rq->resid_len;
 
                flags = rq->cmd_flags;
                blk_put_request(rq);
@@ -938,37 +559,39 @@ int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
        return (flags & REQ_FAILED) ? -EIO : 0;
 }
 
-/*
- * Called from blk_end_request_callback() after the data of the request is
- * completed and before the request itself is completed. By returning value '1',
- * blk_end_request_callback() returns immediately without completing it.
- */
-static int cdrom_newpc_intr_dummy_cb(struct request *rq)
+static void ide_cd_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
 {
-       return 1;
+       unsigned int nr_bytes = cmd->nbytes - cmd->nleft;
+
+       if (cmd->tf_flags & IDE_TFLAG_WRITE)
+               nr_bytes -= cmd->last_xfer_len;
+
+       if (nr_bytes > 0)
+               ide_complete_rq(drive, 0, nr_bytes);
 }
 
 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
 {
        ide_hwif_t *hwif = drive->hwif;
-       ide_hwgroup_t *hwgroup = hwif->hwgroup;
-       struct request *rq = hwgroup->rq;
-       xfer_func_t *xferfunc;
+       struct ide_cmd *cmd = &hwif->cmd;
+       struct request *rq = hwif->rq;
        ide_expiry_t *expiry = NULL;
-       int dma_error = 0, dma, stat, thislen, uptodate = 0;
-       int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
+       int dma_error = 0, dma, thislen, uptodate = 0;
+       int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0, nsectors;
+       int sense = blk_sense_request(rq);
        unsigned int timeout;
        u16 len;
-       u8 ireason;
+       u8 ireason, stat;
 
-       ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x\n",
-                     __func__, rq->cmd[0], write);
+       ide_debug_log(IDE_DBG_PC, "cmd: 0x%x, write: 0x%x", rq->cmd[0], write);
 
        /* check for errors */
        dma = drive->dma;
        if (dma) {
                drive->dma = 0;
+               drive->waiting_for_dma = 0;
                dma_error = hwif->dma_ops->dma_end(drive);
+               ide_dma_unmap_sg(drive, cmd);
                if (dma_error) {
                        printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
                                        write ? "write" : "read");
@@ -976,28 +599,34 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
                }
        }
 
-       if (cdrom_decode_status(drive, 0, &stat))
-               return ide_stopped;
+       /* check status */
+       stat = hwif->tp_ops->read_status(hwif);
+
+       if (!OK_STAT(stat, 0, BAD_R_STAT)) {
+               rc = cdrom_decode_status(drive, stat);
+               if (rc) {
+                       if (rc == 2)
+                               goto out_end;
+                       return ide_stopped;
+               }
+       }
 
        /* using dma, transfer is complete now */
        if (dma) {
                if (dma_error)
                        return ide_error(drive, "dma error", stat);
-               if (blk_fs_request(rq)) {
-                       ide_end_request(drive, 1, rq->nr_sectors);
-                       return ide_stopped;
-               }
-               goto end_request;
+               uptodate = 1;
+               goto out_end;
        }
 
        ide_read_bcount_and_ireason(drive, &len, &ireason);
 
-       thislen = blk_fs_request(rq) ? len : rq->data_len;
+       thislen = blk_fs_request(rq) ? len : cmd->nleft;
        if (thislen > len)
                thislen = len;
 
-       ide_debug_log(IDE_DBG_PC, "%s: DRQ: stat: 0x%x, thislen: %d\n",
-                     __func__, stat, thislen);
+       ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
+                                 stat, thislen);
 
        /* If DRQ is clear, the command has completed. */
        if ((stat & ATA_DRQ) == 0) {
@@ -1007,162 +636,116 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
                         * Otherwise, complete the command normally.
                         */
                        uptodate = 1;
-                       if (rq->current_nr_sectors > 0) {
+                       if (cmd->nleft > 0) {
                                printk(KERN_ERR PFX "%s: %s: data underrun "
-                                               "(%d blocks)\n",
-                                               drive->name, __func__,
-                                               rq->current_nr_sectors);
+                                       "(%u bytes)\n", drive->name, __func__,
+                                       cmd->nleft);
                                if (!write)
                                        rq->cmd_flags |= REQ_FAILED;
                                uptodate = 0;
                        }
-                       cdrom_end_request(drive, uptodate);
-                       return ide_stopped;
                } else if (!blk_pc_request(rq)) {
-                       ide_cd_request_sense_fixup(drive, rq);
+                       ide_cd_request_sense_fixup(drive, cmd);
                        /* complain if we still have data left to transfer */
-                       uptodate = rq->data_len ? 0 : 1;
+                       uptodate = cmd->nleft ? 0 : 1;
+                       if (uptodate == 0)
+                               rq->cmd_flags |= REQ_FAILED;
                }
-               goto end_request;
+               goto out_end;
        }
 
        /* check which way to transfer data */
-       if (ide_cd_check_ireason(drive, rq, len, ireason, write))
-               return ide_stopped;
+       rc = ide_cd_check_ireason(drive, rq, len, ireason, write);
+       if (rc)
+               goto out_end;
 
-       if (blk_fs_request(rq)) {
-               if (write == 0) {
-                       int nskip;
-
-                       if (ide_cd_check_transfer_size(drive, len)) {
-                               cdrom_end_request(drive, 0);
-                               return ide_stopped;
-                       }
-
-                       /*
-                        * First, figure out if we need to bit-bucket
-                        * any of the leading sectors.
-                        */
-                       nskip = min_t(int, rq->current_nr_sectors
-                                          - bio_cur_sectors(rq->bio),
-                                          thislen >> 9);
-                       if (nskip > 0) {
-                               ide_pad_transfer(drive, write, nskip << 9);
-                               rq->current_nr_sectors -= nskip;
-                               thislen -= (nskip << 9);
-                       }
-               }
-       }
-
-       if (ireason == 0) {
-               write = 1;
-               xferfunc = hwif->tp_ops->output_data;
-       } else {
-               write = 0;
-               xferfunc = hwif->tp_ops->input_data;
-       }
+       cmd->last_xfer_len = 0;
 
-       ide_debug_log(IDE_DBG_PC, "%s: data transfer, rq->cmd_type: 0x%x, "
-                     "ireason: 0x%x\n", __func__, rq->cmd_type, ireason);
+       ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
+                                 "ireason: 0x%x",
+                                 rq->cmd_type, ireason);
 
        /* transfer data */
        while (thislen > 0) {
-               u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
-               int blen = rq->data_len;
-
-               /* bio backed? */
-               if (rq->bio) {
-                       if (blk_fs_request(rq)) {
-                               ptr = rq->buffer;
-                               blen = rq->current_nr_sectors << 9;
-                       } else {
-                               ptr = bio_data(rq->bio);
-                               blen = bio_iovec(rq->bio)->bv_len;
-                       }
-               }
+               int blen = min_t(int, thislen, cmd->nleft);
 
-               if (!ptr) {
-                       if (blk_fs_request(rq) && !write)
-                               /*
-                                * If the buffers are full, pipe the rest into
-                                * oblivion.
-                                */
-                               ide_pad_transfer(drive, 0, thislen);
-                       else {
-                               printk(KERN_ERR PFX "%s: confused, missing data\n",
-                                               drive->name);
-                               blk_dump_rq_flags(rq, rq_data_dir(rq)
-                                                 ? "cdrom_newpc_intr, write"
-                                                 : "cdrom_newpc_intr, read");
-                       }
+               if (cmd->nleft == 0)
                        break;
-               }
-
-               if (blen > thislen)
-                       blen = thislen;
 
-               xferfunc(drive, NULL, ptr, blen);
+               ide_pio_bytes(drive, cmd, write, blen);
+               cmd->last_xfer_len += blen;
 
                thislen -= blen;
                len -= blen;
 
-               if (blk_fs_request(rq)) {
-                       rq->buffer += blen;
-                       rq->nr_sectors -= (blen >> 9);
-                       rq->current_nr_sectors -= (blen >> 9);
-                       rq->sector += (blen >> 9);
-
-                       if (rq->current_nr_sectors == 0 && rq->nr_sectors)
-                               cdrom_end_request(drive, 1);
-               } else {
-                       rq->data_len -= blen;
-
-                       /*
-                        * The request can't be completed until DRQ is cleared.
-                        * So complete the data, but don't complete the request
-                        * using the dummy function for the callback feature
-                        * of blk_end_request_callback().
-                        */
-                       if (rq->bio)
-                               blk_end_request_callback(rq, 0, blen,
-                                                cdrom_newpc_intr_dummy_cb);
-                       else
-                               rq->data += blen;
-               }
-               if (!write && blk_sense_request(rq))
+               if (sense && write == 0)
                        rq->sense_len += blen;
        }
 
        /* pad, if necessary */
-       if (!blk_fs_request(rq) && len > 0)
-               ide_pad_transfer(drive, write, len);
+       if (len > 0) {
+               if (blk_fs_request(rq) == 0 || write == 0)
+                       ide_pad_transfer(drive, write, len);
+               else {
+                       printk(KERN_ERR PFX "%s: confused, missing data\n",
+                               drive->name);
+                       blk_dump_rq_flags(rq, "cdrom_newpc_intr");
+               }
+       }
 
        if (blk_pc_request(rq)) {
                timeout = rq->timeout;
        } else {
                timeout = ATAPI_WAIT_PC;
                if (!blk_fs_request(rq))
-                       expiry = cdrom_timer_expiry;
+                       expiry = ide_cd_expiry;
        }
 
-       ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
+       hwif->expiry = expiry;
+       ide_set_handler(drive, cdrom_newpc_intr, timeout);
        return ide_started;
 
-end_request:
-       if (blk_pc_request(rq)) {
-               unsigned int dlen = rq->data_len;
-
-               if (dma)
-                       rq->data_len = 0;
-
-               if (blk_end_request(rq, 0, dlen))
+out_end:
+       if (blk_pc_request(rq) && rc == 0) {
+               if (blk_end_request(rq, 0, rq->data_len))
                        BUG();
 
-               hwgroup->rq = NULL;
+               hwif->rq = NULL;
        } else {
-               if (!uptodate)
-                       rq->cmd_flags |= REQ_FAILED;
-               cdrom_end_request(drive, uptodate);
+               if (sense && uptodate)
+                       ide_cd_complete_failed_rq(drive, rq);
+
+               if (blk_fs_request(rq)) {
+                       if (cmd->nleft == 0)
+                               uptodate = 1;
+               } else {
+                       if (uptodate <= 0 && rq->errors == 0)
+                               rq->errors = -EIO;
+               }
+
+               if (uptodate == 0)
+                       ide_cd_error_cmd(drive, cmd);
+
+               /* make sure it's fully ended */
+               if (blk_pc_request(rq))
+                       nsectors = (rq->data_len + 511) >> 9;
+               else
+                       nsectors = rq->hard_nr_sectors;
+
+               if (nsectors == 0)
+                       nsectors = 1;
+
+               if (blk_fs_request(rq) == 0) {
+                       rq->resid_len = rq->data_len -
+                               (cmd->nbytes - cmd->nleft);
+                       if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
+                               rq->resid_len += cmd->last_xfer_len;
+               }
+
+               ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
+
+               if (sense && rc == 2)
+                       ide_error(drive, "request sense failure", stat);
        }
        return ide_stopped;
 }
@@ -1170,58 +753,48 @@ end_request:
 static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
 {
        struct cdrom_info *cd = drive->driver_data;
+       struct request_queue *q = drive->queue;
        int write = rq_data_dir(rq) == WRITE;
        unsigned short sectors_per_frame =
-               queue_hardsect_size(drive->queue) >> SECTOR_BITS;
+               queue_hardsect_size(q) >> SECTOR_BITS;
 
-       ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x, "
-                     "secs_per_frame: %u\n",
-                     __func__, rq->cmd[0], write, sectors_per_frame);
+       ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, "
+                                 "secs_per_frame: %u",
+                                 rq->cmd[0], rq->cmd_flags, sectors_per_frame);
 
        if (write) {
                /* disk has become write protected */
-               if (get_disk_ro(cd->disk)) {
-                       cdrom_end_request(drive, 0);
+               if (get_disk_ro(cd->disk))
                        return ide_stopped;
-               }
        } else {
                /*
                 * We may be retrying this request after an error.  Fix up any
                 * weirdness which might be present in the request packet.
                 */
-               ide_cd_restore_request(drive, rq);
+               q->prep_rq_fn(q, rq);
        }
 
-       /* use DMA, if possible / writes *must* be hardware frame aligned */
+       /* fs requests *must* be hardware frame aligned */
        if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
-           (rq->sector & (sectors_per_frame - 1))) {
-               if (write) {
-                       cdrom_end_request(drive, 0);
-                       return ide_stopped;
-               }
-               drive->dma = 0;
-       } else
-               drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
+           (rq->sector & (sectors_per_frame - 1)))
+               return ide_stopped;
+
+       /* use DMA, if possible */
+       drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
 
        if (write)
                cd->devinfo.media_written = 1;
 
-       return ide_started;
-}
-
-static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
-{
-       struct request *rq = HWGROUP(drive)->rq;
+       rq->timeout = ATAPI_WAIT_PC;
 
-       return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
+       return ide_started;
 }
 
 static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
 {
 
-       ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, "
-                     "rq->cmd_type: 0x%x\n", __func__, rq->cmd[0],
-                     rq->cmd_type);
+       ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
+                                 rq->cmd[0], rq->cmd_type);
 
        if (blk_pc_request(rq))
                rq->cmd_flags |= REQ_QUIET;
@@ -1231,15 +804,10 @@ static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
        drive->dma = 0;
 
        /* sg request */
-       if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
+       if (rq->bio) {
                struct request_queue *q = drive->queue;
+               char *buf = bio_data(rq->bio);
                unsigned int alignment;
-               char *buf;
-
-               if (rq->bio)
-                       buf = bio_data(rq->bio);
-               else
-                       buf = rq->data;
 
                drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
 
@@ -1260,74 +828,61 @@ static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
 static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
                                        sector_t block)
 {
-       struct cdrom_info *info = drive->driver_data;
-       ide_handler_t *fn;
-       int xferlen;
-
-       ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, "
-                     "rq->cmd_type: 0x%x, block: %llu\n",
-                     __func__, rq->cmd[0], rq->cmd_type,
-                     (unsigned long long)block);
+       struct ide_cmd cmd;
+       int uptodate = 0, nsectors;
 
-       if (blk_fs_request(rq)) {
-               if (drive->atapi_flags & IDE_AFLAG_SEEKING) {
-                       ide_hwif_t *hwif = drive->hwif;
-                       unsigned long elapsed = jiffies - info->start_seek;
-                       int stat = hwif->tp_ops->read_status(hwif);
-
-                       if ((stat & ATA_DSC) != ATA_DSC) {
-                               if (elapsed < IDECD_SEEK_TIMEOUT) {
-                                       ide_stall_queue(drive,
-                                                       IDECD_SEEK_TIMER);
-                                       return ide_stopped;
-                               }
-                               printk(KERN_ERR PFX "%s: DSC timeout\n",
-                                               drive->name);
-                       }
-                       drive->atapi_flags &= ~IDE_AFLAG_SEEKING;
-               }
-               if (rq_data_dir(rq) == READ &&
-                   IDE_LARGE_SEEK(info->last_block, block,
-                           IDECD_SEEK_THRESHOLD) &&
-                   (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)) {
-                       xferlen = 0;
-                       fn = cdrom_start_seek_continuation;
+       ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
+                                 rq->cmd[0], (unsigned long long)block);
 
-                       drive->dma = 0;
-                       info->start_seek = jiffies;
-
-                       ide_cd_prepare_seek_request(drive, rq);
-               } else {
-                       xferlen = 32768;
-                       fn = cdrom_start_rw_cont;
+       if (drive->debug_mask & IDE_DBG_RQ)
+               blk_dump_rq_flags(rq, "ide_cd_do_request");
 
-                       if (cdrom_start_rw(drive, rq) == ide_stopped)
-                               return ide_stopped;
-
-                       if (ide_cd_prepare_rw_request(drive, rq) == ide_stopped)
-                               return ide_stopped;
-               }
-               info->last_block = block;
+       if (blk_fs_request(rq)) {
+               if (cdrom_start_rw(drive, rq) == ide_stopped)
+                       goto out_end;
        } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
                   rq->cmd_type == REQ_TYPE_ATA_PC) {
-               xferlen = rq->data_len;
-               fn = cdrom_do_newpc_cont;
-
                if (!rq->timeout)
                        rq->timeout = ATAPI_WAIT_PC;
 
                cdrom_do_block_pc(drive, rq);
        } else if (blk_special_request(rq)) {
                /* right now this can only be a reset... */
-               cdrom_end_request(drive, 1);
-               return ide_stopped;
+               uptodate = 1;
+               goto out_end;
        } else {
                blk_dump_rq_flags(rq, DRV_NAME " bad flags");
-               cdrom_end_request(drive, 0);
-               return ide_stopped;
+               if (rq->errors == 0)
+                       rq->errors = -EIO;
+               goto out_end;
+       }
+
+       /* prepare sense request for this command */
+       ide_prep_sense(drive, rq);
+
+       memset(&cmd, 0, sizeof(cmd));
+
+       if (rq_data_dir(rq))
+               cmd.tf_flags |= IDE_TFLAG_WRITE;
+
+       cmd.rq = rq;
+
+       if (blk_fs_request(rq) || rq->data_len) {
+               ide_init_sg_cmd(&cmd, blk_fs_request(rq) ? (rq->nr_sectors << 9)
+                                                        : rq->data_len);
+               ide_map_sg(drive, &cmd);
        }
 
-       return cdrom_start_packet_command(drive, xferlen, fn);
+       return ide_issue_pc(drive, &cmd);
+out_end:
+       nsectors = rq->hard_nr_sectors;
+
+       if (nsectors == 0)
+               nsectors = 1;
+
+       ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
+
+       return ide_stopped;
 }
 
 /*
@@ -1352,7 +907,7 @@ int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
        struct cdrom_device_info *cdi = &info->devinfo;
        unsigned char cmd[BLK_MAX_CDB];
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        memset(cmd, 0, BLK_MAX_CDB);
        cmd[0] = GPCMD_TEST_UNIT_READY;
@@ -1380,7 +935,7 @@ static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
        unsigned len = sizeof(capbuf);
        u32 blocklen;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        memset(cmd, 0, BLK_MAX_CDB);
        cmd[0] = GPCMD_READ_CDVD_CAPACITY;
@@ -1412,8 +967,8 @@ static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
        *capacity = 1 + be32_to_cpu(capbuf.lba);
        *sectors_per_frame = blocklen >> SECTOR_BITS;
 
-       ide_debug_log(IDE_DBG_PROBE, "%s: cap: %lu, sectors_per_frame: %lu\n",
-                     __func__, *capacity, *sectors_per_frame);
+       ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
+                                    *capacity, *sectors_per_frame);
 
        return 0;
 }
@@ -1424,7 +979,7 @@ static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
 {
        unsigned char cmd[BLK_MAX_CDB];
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        memset(cmd, 0, BLK_MAX_CDB);
 
@@ -1454,7 +1009,7 @@ int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
        long last_written;
        unsigned long sectors_per_frame = SECTORS_PER_FRAME;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        if (toc == NULL) {
                /* try to allocate space */
@@ -1616,7 +1171,7 @@ int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
        struct packet_command cgc;
        int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
                size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
@@ -1636,7 +1191,7 @@ void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
        struct cdrom_info *cd = drive->driver_data;
        u16 curspeed, maxspeed;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
                curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
@@ -1646,8 +1201,8 @@ void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
                maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
        }
 
-       ide_debug_log(IDE_DBG_PROBE, "%s: curspeed: %u, maxspeed: %u\n",
-                     __func__, curspeed, maxspeed);
+       ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
+                                    curspeed, maxspeed);
 
        cd->current_speed = (curspeed + (176/2)) / 176;
        cd->max_speed = (maxspeed + (176/2)) / 176;
@@ -1681,7 +1236,7 @@ static int ide_cdrom_register(ide_drive_t *drive, int nslots)
        struct cdrom_info *info = drive->driver_data;
        struct cdrom_device_info *devinfo = &info->devinfo;
 
-       ide_debug_log(IDE_DBG_PROBE, "Call %s, nslots: %d\n", __func__, nslots);
+       ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
 
        devinfo->ops = &ide_cdrom_dops;
        devinfo->speed = info->current_speed;
@@ -1704,9 +1259,8 @@ static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
        mechtype_t mechtype;
        int nslots = 1;
 
-       ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->media: 0x%x, "
-                     "drive->atapi_flags: 0x%lx\n", __func__, drive->media,
-                     drive->atapi_flags);
+       ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
+                                    drive->media, drive->atapi_flags);
 
        cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
                     CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
@@ -1908,13 +1462,6 @@ static ide_proc_entry_t idecd_proc[] = {
        { NULL, 0, NULL, NULL }
 };
 
-ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
-
-static const struct ide_proc_devset idecd_settings[] = {
-       IDE_PROC_DEVSET(dsc_overlap, 0, 1),
-       { 0 },
-};
-
 static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
 {
        return idecd_proc;
@@ -1922,14 +1469,11 @@ static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
 
 static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
 {
-       return idecd_settings;
+       return NULL;
 }
 #endif
 
 static const struct cd_list_entry ide_cd_quirks_list[] = {
-       /* Limit transfer size per interrupt. */
-       { "SAMSUNG CD-ROM SCR-2430", NULL,   IDE_AFLAG_LIMIT_NFRAMES         },
-       { "SAMSUNG CD-ROM SCR-2432", NULL,   IDE_AFLAG_LIMIT_NFRAMES         },
        /* SCR-3231 doesn't support the SET_CD_SPEED command. */
        { "SAMSUNG CD-ROM SCR-3231", NULL,   IDE_AFLAG_NO_SPEED_SELECT       },
        /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
@@ -1990,18 +1534,18 @@ static int ide_cdrom_setup(ide_drive_t *drive)
 {
        struct cdrom_info *cd = drive->driver_data;
        struct cdrom_device_info *cdi = &cd->devinfo;
+       struct request_queue *q = drive->queue;
        u16 *id = drive->id;
        char *fw_rev = (char *)&id[ATA_ID_FW_REV];
        int nslots;
 
-       ide_debug_log(IDE_DBG_PROBE, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_PROBE, "enter");
+
+       blk_queue_prep_rq(q, ide_cdrom_prep_fn);
+       blk_queue_dma_alignment(q, 31);
+       blk_queue_update_dma_pad(q, 15);
 
-       blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
-       blk_queue_dma_alignment(drive->queue, 31);
-       blk_queue_update_dma_pad(drive->queue, 15);
-       drive->queue->unplug_delay = (1 * HZ) / 1000;
-       if (!drive->queue->unplug_delay)
-               drive->queue->unplug_delay = 1;
+       q->unplug_delay = max((1 * HZ) / 1000, 1);
 
        drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
        drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
@@ -2019,13 +1563,7 @@ static int ide_cdrom_setup(ide_drive_t *drive)
 
        nslots = ide_cdrom_probe_capabilities(drive);
 
-       /* set correct block size */
-       blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
-
-       if (drive->next != drive)
-               drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
-       else
-               drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
+       blk_queue_hardsect_size(q, CD_FRAMESIZE);
 
        if (ide_cdrom_register(drive, nslots)) {
                printk(KERN_ERR PFX "%s: %s failed to register device with the"
@@ -2042,28 +1580,29 @@ static void ide_cd_remove(ide_drive_t *drive)
 {
        struct cdrom_info *info = drive->driver_data;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        ide_proc_unregister_driver(drive, info->driver);
-
+       device_del(&info->dev);
        del_gendisk(info->disk);
 
-       ide_cd_put(info);
+       mutex_lock(&idecd_ref_mutex);
+       put_device(&info->dev);
+       mutex_unlock(&idecd_ref_mutex);
 }
 
-static void ide_cd_release(struct kref *kref)
+static void ide_cd_release(struct device *dev)
 {
-       struct cdrom_info *info = to_ide_drv(kref, cdrom_info);
+       struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
        struct cdrom_device_info *devinfo = &info->devinfo;
        ide_drive_t *drive = info->drive;
        struct gendisk *g = info->disk;
 
-       ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
+       ide_debug_log(IDE_DBG_FUNC, "enter");
 
        kfree(info->toc);
        if (devinfo->handle == drive)
                unregister_cdrom(devinfo);
-       drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
        drive->driver_data = NULL;
        blk_queue_prep_rq(drive->queue, NULL);
        g->private_data = NULL;
@@ -2073,7 +1612,7 @@ static void ide_cd_release(struct kref *kref)
 
 static int ide_cd_probe(ide_drive_t *);
 
-static ide_driver_t ide_cdrom_driver = {
+static struct ide_driver ide_cdrom_driver = {
        .gen_driver = {
                .owner          = THIS_MODULE,
                .name           = "ide-cdrom",
@@ -2083,8 +1622,6 @@ static ide_driver_t ide_cdrom_driver = {
        .remove                 = ide_cd_remove,
        .version                = IDECD_VERSION,
        .do_request             = ide_cd_do_request,
-       .end_request            = ide_end_request,
-       .error                  = __ide_error,
 #ifdef CONFIG_IDE_PROC_FS
        .proc_entries           = ide_cd_proc_entries,
        .proc_devsets           = ide_cd_proc_devsets,
@@ -2205,9 +1742,6 @@ static struct block_device_operations idecd_ops = {
 };
 
 /* module options */
-static char *ignore;
-module_param(ignore, charp, 0400);
-
 static unsigned long debug_mask;
 module_param(debug_mask, ulong, 0644);
 
@@ -2219,9 +1753,8 @@ static int ide_cd_probe(ide_drive_t *drive)
        struct gendisk *g;
        struct request_sense sense;
 
-       ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->driver_req: %s, "
-                     "drive->media: 0x%x\n", __func__, drive->driver_req,
-                     drive->media);
+       ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
+                                    drive->driver_req, drive->media);
 
        if (!strstr("ide-cdrom", drive->driver_req))
                goto failed;
@@ -2229,16 +1762,8 @@ static int ide_cd_probe(ide_drive_t *drive)
        if (drive->media != ide_cdrom && drive->media != ide_optical)
                goto failed;
 
-       /* skip drives that we were told to ignore */
-       if (ignore != NULL) {
-               if (strstr(ignore, drive->name)) {
-                       printk(KERN_INFO PFX "ignoring drive %s\n",
-                                        drive->name);
-                       goto failed;
-               }
-       }
-
        drive->debug_mask = debug_mask;
+       drive->irq_handler = cdrom_newpc_intr;
 
        info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
        if (info == NULL) {
@@ -2253,7 +1778,12 @@ static int ide_cd_probe(ide_drive_t *drive)
 
        ide_init_disk(g, drive);
 
-       kref_init(&info->kref);
+       info->dev.parent = &drive->gendev;
+       info->dev.release = ide_cd_release;
+       dev_set_name(&info->dev, dev_name(&drive->gendev));
+
+       if (device_register(&info->dev))
+               goto out_free_disk;
 
        info->drive = drive;
        info->driver = &ide_cdrom_driver;
@@ -2267,7 +1797,7 @@ static int ide_cd_probe(ide_drive_t *drive)
        g->driverfs_dev = &drive->gendev;
        g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
        if (ide_cdrom_setup(drive)) {
-               ide_cd_release(&info->kref);
+               put_device(&info->dev);
                goto failed;
        }
 
@@ -2277,6 +1807,8 @@ static int ide_cd_probe(ide_drive_t *drive)
        add_disk(g);
        return 0;
 
+out_free_disk:
+       put_disk(g);
 out_free_cd:
        kfree(info);
 failed: