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