Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ATAPI support. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/delay.h> |
| 7 | #include <linux/ide.h> |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 8 | #include <scsi/scsi.h> |
| 9 | |
| 10 | #ifdef DEBUG |
| 11 | #define debug_log(fmt, args...) \ |
| 12 | printk(KERN_INFO "ide: " fmt, ## args) |
| 13 | #else |
| 14 | #define debug_log(fmt, args...) do {} while (0) |
| 15 | #endif |
| 16 | |
| 17 | /* TODO: unify the code thus making some arguments go away */ |
| 18 | ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 19 | ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry, |
| 20 | void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *), |
| 21 | void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *), |
| 22 | void (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int)) |
| 23 | { |
| 24 | ide_hwif_t *hwif = drive->hwif; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 25 | struct request *rq = hwif->hwgroup->rq; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 26 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 27 | xfer_func_t *xferfunc; |
| 28 | unsigned int temp; |
| 29 | u16 bcount; |
| 30 | u8 stat, ireason, scsi = drive->scsi; |
| 31 | |
| 32 | debug_log("Enter %s - interrupt handler\n", __func__); |
| 33 | |
| 34 | if (pc->flags & PC_FLAG_TIMEDOUT) { |
Borislav Petkov | db9d286 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 35 | drive->pc_callback(drive); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 36 | return ide_stopped; |
| 37 | } |
| 38 | |
| 39 | /* Clear the interrupt */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 40 | stat = tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 41 | |
| 42 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 43 | if (hwif->dma_ops->dma_end(drive) || |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 44 | (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 45 | if (drive->media == ide_floppy && !scsi) |
| 46 | printk(KERN_ERR "%s: DMA %s error\n", |
| 47 | drive->name, rq_data_dir(pc->rq) |
| 48 | ? "write" : "read"); |
| 49 | pc->flags |= PC_FLAG_DMA_ERROR; |
| 50 | } else { |
| 51 | pc->xferred = pc->req_xfer; |
| 52 | if (update_buffers) |
| 53 | update_buffers(drive, pc); |
| 54 | } |
| 55 | debug_log("%s: DMA finished\n", drive->name); |
| 56 | } |
| 57 | |
| 58 | /* No more interrupts */ |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 59 | if ((stat & ATA_DRQ) == 0) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 60 | debug_log("Packet command completed, %d bytes transferred\n", |
| 61 | pc->xferred); |
| 62 | |
| 63 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
| 64 | |
| 65 | local_irq_enable_in_hardirq(); |
| 66 | |
| 67 | if (drive->media == ide_tape && !scsi && |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 68 | (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE) |
| 69 | stat &= ~ATA_ERR; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 70 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 71 | if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 72 | /* Error detected */ |
| 73 | debug_log("%s: I/O error\n", drive->name); |
| 74 | |
| 75 | if (drive->media != ide_tape || scsi) { |
| 76 | pc->rq->errors++; |
| 77 | if (scsi) |
| 78 | goto cmd_finished; |
| 79 | } |
| 80 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 81 | if (rq->cmd[0] == REQUEST_SENSE) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 82 | printk(KERN_ERR "%s: I/O error in request sense" |
| 83 | " command\n", drive->name); |
| 84 | return ide_do_reset(drive); |
| 85 | } |
| 86 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 87 | debug_log("[cmd %x]: check condition\n", rq->cmd[0]); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 88 | |
| 89 | /* Retry operation */ |
| 90 | retry_pc(drive); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 91 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 92 | /* queued, but not started */ |
| 93 | return ide_stopped; |
| 94 | } |
| 95 | cmd_finished: |
| 96 | pc->error = 0; |
| 97 | if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 98 | (stat & ATA_DSC) == 0) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 99 | dsc_handle(drive); |
| 100 | return ide_stopped; |
| 101 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 102 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 103 | /* Command finished - Call the callback function */ |
Borislav Petkov | db9d286 | 2008-07-23 19:55:59 +0200 | [diff] [blame] | 104 | drive->pc_callback(drive); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 105 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 106 | return ide_stopped; |
| 107 | } |
| 108 | |
| 109 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 110 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
| 111 | printk(KERN_ERR "%s: The device wants to issue more interrupts " |
| 112 | "in DMA mode\n", drive->name); |
| 113 | ide_dma_off(drive); |
| 114 | return ide_do_reset(drive); |
| 115 | } |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 116 | |
Bartlomiej Zolnierkiewicz | 1823649 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 117 | /* Get the number of bytes to transfer on this interrupt. */ |
| 118 | ide_read_bcount_and_ireason(drive, &bcount, &ireason); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 119 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 120 | if (ireason & ATAPI_COD) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 121 | printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__); |
| 122 | return ide_do_reset(drive); |
| 123 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 124 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 125 | if (((ireason & ATAPI_IO) == ATAPI_IO) == |
| 126 | !!(pc->flags & PC_FLAG_WRITING)) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 127 | /* Hopefully, we will never get here */ |
| 128 | printk(KERN_ERR "%s: We wanted to %s, but the device wants us " |
| 129 | "to %s!\n", drive->name, |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 130 | (ireason & ATAPI_IO) ? "Write" : "Read", |
| 131 | (ireason & ATAPI_IO) ? "Read" : "Write"); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 132 | return ide_do_reset(drive); |
| 133 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 134 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 135 | if (!(pc->flags & PC_FLAG_WRITING)) { |
| 136 | /* Reading - Check that we have enough space */ |
| 137 | temp = pc->xferred + bcount; |
| 138 | if (temp > pc->req_xfer) { |
| 139 | if (temp > pc->buf_size) { |
| 140 | printk(KERN_ERR "%s: The device wants to send " |
| 141 | "us more data than expected - " |
| 142 | "discarding data\n", |
| 143 | drive->name); |
| 144 | if (scsi) |
| 145 | temp = pc->buf_size - pc->xferred; |
| 146 | else |
| 147 | temp = 0; |
| 148 | if (temp) { |
| 149 | if (pc->sg) |
| 150 | io_buffers(drive, pc, temp, 0); |
| 151 | else |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 152 | tp_ops->input_data(drive, NULL, |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 153 | pc->cur_pos, temp); |
| 154 | printk(KERN_ERR "%s: transferred %d of " |
| 155 | "%d bytes\n", |
| 156 | drive->name, |
| 157 | temp, bcount); |
| 158 | } |
| 159 | pc->xferred += temp; |
| 160 | pc->cur_pos += temp; |
| 161 | ide_pad_transfer(drive, 0, bcount - temp); |
| 162 | ide_set_handler(drive, handler, timeout, |
| 163 | expiry); |
| 164 | return ide_started; |
| 165 | } |
| 166 | debug_log("The device wants to send us more data than " |
| 167 | "expected - allowing transfer\n"); |
| 168 | } |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 169 | xferfunc = tp_ops->input_data; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 170 | } else |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 171 | xferfunc = tp_ops->output_data; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 172 | |
| 173 | if ((drive->media == ide_floppy && !scsi && !pc->buf) || |
| 174 | (drive->media == ide_tape && !scsi && pc->bh) || |
| 175 | (scsi && pc->sg)) |
| 176 | io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING)); |
| 177 | else |
| 178 | xferfunc(drive, NULL, pc->cur_pos, bcount); |
| 179 | |
| 180 | /* Update the current position */ |
| 181 | pc->xferred += bcount; |
| 182 | pc->cur_pos += bcount; |
| 183 | |
| 184 | debug_log("[cmd %x] transferred %d bytes on that intr.\n", |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 185 | rq->cmd[0], bcount); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 186 | |
| 187 | /* And set the interrupt handler again */ |
| 188 | ide_set_handler(drive, handler, timeout, expiry); |
| 189 | return ide_started; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(ide_pc_intr); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 192 | |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 193 | static u8 ide_read_ireason(ide_drive_t *drive) |
| 194 | { |
| 195 | ide_task_t task; |
| 196 | |
| 197 | memset(&task, 0, sizeof(task)); |
| 198 | task.tf_flags = IDE_TFLAG_IN_NSECT; |
| 199 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 200 | drive->hwif->tp_ops->tf_read(drive, &task); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 201 | |
| 202 | return task.tf.nsect & 3; |
| 203 | } |
| 204 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 205 | static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason) |
| 206 | { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 207 | int retries = 100; |
| 208 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 209 | while (retries-- && ((ireason & ATAPI_COD) == 0 || |
| 210 | (ireason & ATAPI_IO))) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 211 | printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing " |
| 212 | "a packet command, retrying\n", drive->name); |
| 213 | udelay(100); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 214 | ireason = ide_read_ireason(drive); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 215 | if (retries == 0) { |
| 216 | printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing " |
| 217 | "a packet command, ignoring\n", |
| 218 | drive->name); |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 219 | ireason |= ATAPI_COD; |
| 220 | ireason &= ~ATAPI_IO; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | return ireason; |
| 225 | } |
| 226 | |
| 227 | ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 228 | ide_handler_t *handler, unsigned int timeout, |
| 229 | ide_expiry_t *expiry) |
| 230 | { |
| 231 | ide_hwif_t *hwif = drive->hwif; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 232 | struct request *rq = hwif->hwgroup->rq; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 233 | ide_startstop_t startstop; |
| 234 | u8 ireason; |
| 235 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 236 | if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 237 | printk(KERN_ERR "%s: Strange, packet command initiated yet " |
| 238 | "DRQ isn't asserted\n", drive->name); |
| 239 | return startstop; |
| 240 | } |
| 241 | |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 242 | ireason = ide_read_ireason(drive); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 243 | if (drive->media == ide_tape && !drive->scsi) |
| 244 | ireason = ide_wait_ireason(drive, ireason); |
| 245 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame^] | 246 | if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 247 | printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing " |
| 248 | "a packet command\n", drive->name); |
| 249 | return ide_do_reset(drive); |
| 250 | } |
| 251 | |
| 252 | /* Set the interrupt routine */ |
| 253 | ide_set_handler(drive, handler, timeout, expiry); |
| 254 | |
| 255 | /* Begin DMA, if necessary */ |
| 256 | if (pc->flags & PC_FLAG_DMA_OK) { |
| 257 | pc->flags |= PC_FLAG_DMA_IN_PROGRESS; |
| 258 | hwif->dma_ops->dma_start(drive); |
| 259 | } |
| 260 | |
| 261 | /* Send the actual packet */ |
Borislav Petkov | ea68d27 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 262 | if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0) |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 263 | hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 264 | |
| 265 | return ide_started; |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(ide_transfer_pc); |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 268 | |
| 269 | ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 270 | ide_handler_t *handler, unsigned int timeout, |
| 271 | ide_expiry_t *expiry) |
| 272 | { |
| 273 | ide_hwif_t *hwif = drive->hwif; |
| 274 | u16 bcount; |
| 275 | u8 dma = 0; |
| 276 | |
| 277 | /* We haven't transferred any data yet */ |
| 278 | pc->xferred = 0; |
| 279 | pc->cur_pos = pc->buf; |
| 280 | |
| 281 | /* Request to transfer the entire buffer at once */ |
| 282 | if (drive->media == ide_tape && !drive->scsi) |
| 283 | bcount = pc->req_xfer; |
| 284 | else |
| 285 | bcount = min(pc->req_xfer, 63 * 1024); |
| 286 | |
| 287 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
| 288 | pc->flags &= ~PC_FLAG_DMA_ERROR; |
| 289 | ide_dma_off(drive); |
| 290 | } |
| 291 | |
| 292 | if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) { |
| 293 | if (drive->scsi) |
| 294 | hwif->sg_mapped = 1; |
| 295 | dma = !hwif->dma_ops->dma_setup(drive); |
| 296 | if (drive->scsi) |
| 297 | hwif->sg_mapped = 0; |
| 298 | } |
| 299 | |
| 300 | if (!dma) |
| 301 | pc->flags &= ~PC_FLAG_DMA_OK; |
| 302 | |
| 303 | ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE, |
| 304 | bcount, dma); |
| 305 | |
| 306 | /* Issue the packet command */ |
Borislav Petkov | ac77ef8 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 307 | if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) { |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 308 | ide_execute_command(drive, ATA_CMD_PACKET, handler, |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 309 | timeout, NULL); |
| 310 | return ide_started; |
| 311 | } else { |
| 312 | ide_execute_pkt_cmd(drive); |
| 313 | return (*handler)(drive); |
| 314 | } |
| 315 | } |
| 316 | EXPORT_SYMBOL_GPL(ide_issue_pc); |