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