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 | |
Bartlomiej Zolnierkiewicz | 51509ee | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 17 | /* |
| 18 | * Check whether we can support a device, |
| 19 | * based on the ATAPI IDENTIFY command results. |
| 20 | */ |
| 21 | int ide_check_atapi_device(ide_drive_t *drive, const char *s) |
| 22 | { |
| 23 | u16 *id = drive->id; |
| 24 | u8 gcw[2], protocol, device_type, removable, drq_type, packet_size; |
| 25 | |
| 26 | *((u16 *)&gcw) = id[ATA_ID_CONFIG]; |
| 27 | |
| 28 | protocol = (gcw[1] & 0xC0) >> 6; |
| 29 | device_type = gcw[1] & 0x1F; |
| 30 | removable = (gcw[0] & 0x80) >> 7; |
| 31 | drq_type = (gcw[0] & 0x60) >> 5; |
| 32 | packet_size = gcw[0] & 0x03; |
| 33 | |
| 34 | #ifdef CONFIG_PPC |
| 35 | /* kludge for Apple PowerBook internal zip */ |
| 36 | if (drive->media == ide_floppy && device_type == 5 && |
| 37 | !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") && |
| 38 | strstr((char *)&id[ATA_ID_PROD], "ZIP")) |
| 39 | device_type = 0; |
| 40 | #endif |
| 41 | |
| 42 | if (protocol != 2) |
| 43 | printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n", |
| 44 | s, drive->name, protocol); |
| 45 | else if ((drive->media == ide_floppy && device_type != 0) || |
| 46 | (drive->media == ide_tape && device_type != 1)) |
| 47 | printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n", |
| 48 | s, drive->name, device_type); |
| 49 | else if (removable == 0) |
| 50 | printk(KERN_ERR "%s: %s: the removable flag is not set\n", |
| 51 | s, drive->name); |
| 52 | else if (drive->media == ide_floppy && drq_type == 3) |
| 53 | printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not " |
| 54 | "supported\n", s, drive->name, drq_type); |
| 55 | else if (packet_size != 0) |
| 56 | printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 " |
| 57 | "bytes\n", s, drive->name, packet_size); |
| 58 | else |
| 59 | return 1; |
| 60 | return 0; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(ide_check_atapi_device); |
| 63 | |
Bartlomiej Zolnierkiewicz | acaa0f5 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 64 | /* PIO data transfer routine using the scatter gather table. */ |
| 65 | int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 66 | unsigned int bcount, int write) |
| 67 | { |
| 68 | ide_hwif_t *hwif = drive->hwif; |
| 69 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
| 70 | xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data; |
| 71 | struct scatterlist *sg = pc->sg; |
| 72 | char *buf; |
| 73 | int count, done = 0; |
| 74 | |
| 75 | while (bcount) { |
| 76 | count = min(sg->length - pc->b_count, bcount); |
| 77 | |
| 78 | if (PageHighMem(sg_page(sg))) { |
| 79 | unsigned long flags; |
| 80 | |
| 81 | local_irq_save(flags); |
| 82 | buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset; |
| 83 | xf(drive, NULL, buf + pc->b_count, count); |
| 84 | kunmap_atomic(buf - sg->offset, KM_IRQ0); |
| 85 | local_irq_restore(flags); |
| 86 | } else { |
| 87 | buf = sg_virt(sg); |
| 88 | xf(drive, NULL, buf + pc->b_count, count); |
| 89 | } |
| 90 | |
| 91 | bcount -= count; |
| 92 | pc->b_count += count; |
| 93 | done += count; |
| 94 | |
| 95 | if (pc->b_count == sg->length) { |
| 96 | if (!--pc->sg_cnt) |
| 97 | break; |
| 98 | pc->sg = sg = sg_next(sg); |
| 99 | pc->b_count = 0; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (bcount) { |
| 104 | printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name, |
| 105 | bcount, write ? "padding with zeros" |
| 106 | : "discarding data"); |
| 107 | ide_pad_transfer(drive, write, bcount); |
| 108 | } |
| 109 | |
| 110 | return done; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(ide_io_buffers); |
| 113 | |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 114 | void ide_init_pc(struct ide_atapi_pc *pc) |
| 115 | { |
| 116 | memset(pc, 0, sizeof(*pc)); |
| 117 | pc->buf = pc->pc_buf; |
| 118 | pc->buf_size = IDE_PC_BUFFER_SIZE; |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(ide_init_pc); |
| 121 | |
Bartlomiej Zolnierkiewicz | 7645c15 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Generate a new packet command request in front of the request queue, before |
| 124 | * the current request, so that it will be processed immediately, on the next |
| 125 | * pass through the driver. |
| 126 | */ |
| 127 | void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk, |
| 128 | struct ide_atapi_pc *pc, struct request *rq) |
| 129 | { |
| 130 | blk_rq_init(NULL, rq); |
| 131 | rq->cmd_type = REQ_TYPE_SPECIAL; |
| 132 | rq->cmd_flags |= REQ_PREEMPT; |
| 133 | rq->buffer = (char *)pc; |
| 134 | rq->rq_disk = disk; |
| 135 | memcpy(rq->cmd, pc->c, 12); |
| 136 | if (drive->media == ide_tape) |
| 137 | rq->cmd[13] = REQ_IDETAPE_PC1; |
| 138 | ide_do_drive_cmd(drive, rq); |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(ide_queue_pc_head); |
| 141 | |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Add a special packet command request to the tail of the request queue, |
| 144 | * and wait for it to be serviced. |
| 145 | */ |
| 146 | int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk, |
| 147 | struct ide_atapi_pc *pc) |
| 148 | { |
| 149 | struct request *rq; |
| 150 | int error; |
| 151 | |
| 152 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 153 | rq->cmd_type = REQ_TYPE_SPECIAL; |
| 154 | rq->buffer = (char *)pc; |
| 155 | memcpy(rq->cmd, pc->c, 12); |
| 156 | if (drive->media == ide_tape) |
| 157 | rq->cmd[13] = REQ_IDETAPE_PC1; |
| 158 | error = blk_execute_rq(drive->queue, disk, rq, 0); |
| 159 | blk_put_request(rq); |
| 160 | |
| 161 | return error; |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(ide_queue_pc_tail); |
| 164 | |
Bartlomiej Zolnierkiewicz | de699ad | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 165 | int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk) |
| 166 | { |
| 167 | struct ide_atapi_pc pc; |
| 168 | |
| 169 | ide_init_pc(&pc); |
| 170 | pc.c[0] = TEST_UNIT_READY; |
| 171 | |
| 172 | return ide_queue_pc_tail(drive, disk, &pc); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(ide_do_test_unit_ready); |
| 175 | |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 176 | int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start) |
| 177 | { |
| 178 | struct ide_atapi_pc pc; |
| 179 | |
| 180 | ide_init_pc(&pc); |
| 181 | pc.c[0] = START_STOP; |
| 182 | pc.c[4] = start; |
| 183 | |
| 184 | if (drive->media == ide_tape) |
| 185 | pc.flags |= PC_FLAG_WAIT_FOR_DSC; |
| 186 | |
| 187 | return ide_queue_pc_tail(drive, disk, &pc); |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(ide_do_start_stop); |
| 190 | |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 191 | int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on) |
| 192 | { |
| 193 | struct ide_atapi_pc pc; |
| 194 | |
| 195 | if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK) |
| 196 | return 0; |
| 197 | |
| 198 | ide_init_pc(&pc); |
| 199 | pc.c[0] = ALLOW_MEDIUM_REMOVAL; |
| 200 | pc.c[4] = on; |
| 201 | |
| 202 | return ide_queue_pc_tail(drive, disk, &pc); |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(ide_set_media_lock); |
| 205 | |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 206 | int ide_scsi_expiry(ide_drive_t *drive) |
| 207 | { |
| 208 | struct ide_atapi_pc *pc = drive->pc; |
| 209 | |
| 210 | debug_log("%s called for %lu at %lu\n", __func__, |
| 211 | pc->scsi_cmd->serial_number, jiffies); |
| 212 | |
| 213 | pc->flags |= PC_FLAG_TIMEDOUT; |
| 214 | |
| 215 | return 0; /* we do not want the IDE subsystem to retry */ |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(ide_scsi_expiry); |
| 218 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 219 | /* TODO: unify the code thus making some arguments go away */ |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 220 | ide_startstop_t ide_pc_intr(ide_drive_t *drive, ide_handler_t *handler, |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 221 | void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *), |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 222 | void (*retry_pc)(ide_drive_t *), |
Bartlomiej Zolnierkiewicz | acaa0f5 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 223 | int (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int)) |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 224 | { |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 225 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 226 | ide_hwif_t *hwif = drive->hwif; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 227 | struct request *rq = hwif->hwgroup->rq; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 228 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 229 | xfer_func_t *xferfunc; |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 230 | ide_expiry_t *expiry; |
| 231 | unsigned int timeout, temp; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 232 | u16 bcount; |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 233 | u8 stat, ireason, scsi = drive->scsi, dsc = 0; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 234 | |
| 235 | debug_log("Enter %s - interrupt handler\n", __func__); |
| 236 | |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 237 | if (scsi) { |
| 238 | timeout = ide_scsi_get_timeout(pc); |
| 239 | expiry = ide_scsi_expiry; |
| 240 | } else { |
| 241 | timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD |
| 242 | : WAIT_TAPE_CMD; |
| 243 | expiry = NULL; |
| 244 | } |
| 245 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 246 | if (pc->flags & PC_FLAG_TIMEDOUT) { |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 247 | drive->pc_callback(drive, 0); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 248 | return ide_stopped; |
| 249 | } |
| 250 | |
| 251 | /* Clear the interrupt */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 252 | stat = tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 253 | |
| 254 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 255 | if (hwif->dma_ops->dma_end(drive) || |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 256 | (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 257 | if (drive->media == ide_floppy && !scsi) |
| 258 | printk(KERN_ERR "%s: DMA %s error\n", |
| 259 | drive->name, rq_data_dir(pc->rq) |
| 260 | ? "write" : "read"); |
| 261 | pc->flags |= PC_FLAG_DMA_ERROR; |
| 262 | } else { |
| 263 | pc->xferred = pc->req_xfer; |
| 264 | if (update_buffers) |
| 265 | update_buffers(drive, pc); |
| 266 | } |
| 267 | debug_log("%s: DMA finished\n", drive->name); |
| 268 | } |
| 269 | |
| 270 | /* No more interrupts */ |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 271 | if ((stat & ATA_DRQ) == 0) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 272 | debug_log("Packet command completed, %d bytes transferred\n", |
| 273 | pc->xferred); |
| 274 | |
| 275 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
| 276 | |
| 277 | local_irq_enable_in_hardirq(); |
| 278 | |
| 279 | if (drive->media == ide_tape && !scsi && |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 280 | (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE) |
| 281 | stat &= ~ATA_ERR; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 282 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 283 | if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 284 | /* Error detected */ |
| 285 | debug_log("%s: I/O error\n", drive->name); |
| 286 | |
| 287 | if (drive->media != ide_tape || scsi) { |
| 288 | pc->rq->errors++; |
| 289 | if (scsi) |
| 290 | goto cmd_finished; |
| 291 | } |
| 292 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 293 | if (rq->cmd[0] == REQUEST_SENSE) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 294 | printk(KERN_ERR "%s: I/O error in request sense" |
| 295 | " command\n", drive->name); |
| 296 | return ide_do_reset(drive); |
| 297 | } |
| 298 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 299 | debug_log("[cmd %x]: check condition\n", rq->cmd[0]); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 300 | |
| 301 | /* Retry operation */ |
| 302 | retry_pc(drive); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 303 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 304 | /* queued, but not started */ |
| 305 | return ide_stopped; |
| 306 | } |
| 307 | cmd_finished: |
| 308 | pc->error = 0; |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 309 | |
| 310 | if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0) |
| 311 | dsc = 1; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 312 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 313 | /* Command finished - Call the callback function */ |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 314 | drive->pc_callback(drive, dsc); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 315 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 316 | return ide_stopped; |
| 317 | } |
| 318 | |
| 319 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 320 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
| 321 | printk(KERN_ERR "%s: The device wants to issue more interrupts " |
| 322 | "in DMA mode\n", drive->name); |
| 323 | ide_dma_off(drive); |
| 324 | return ide_do_reset(drive); |
| 325 | } |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 326 | |
Bartlomiej Zolnierkiewicz | 1823649 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 327 | /* Get the number of bytes to transfer on this interrupt. */ |
| 328 | ide_read_bcount_and_ireason(drive, &bcount, &ireason); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 329 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 330 | if (ireason & ATAPI_COD) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 331 | printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__); |
| 332 | return ide_do_reset(drive); |
| 333 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 334 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 335 | if (((ireason & ATAPI_IO) == ATAPI_IO) == |
| 336 | !!(pc->flags & PC_FLAG_WRITING)) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 337 | /* Hopefully, we will never get here */ |
| 338 | printk(KERN_ERR "%s: We wanted to %s, but the device wants us " |
| 339 | "to %s!\n", drive->name, |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 340 | (ireason & ATAPI_IO) ? "Write" : "Read", |
| 341 | (ireason & ATAPI_IO) ? "Read" : "Write"); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 342 | return ide_do_reset(drive); |
| 343 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 344 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 345 | if (!(pc->flags & PC_FLAG_WRITING)) { |
| 346 | /* Reading - Check that we have enough space */ |
| 347 | temp = pc->xferred + bcount; |
| 348 | if (temp > pc->req_xfer) { |
| 349 | if (temp > pc->buf_size) { |
| 350 | printk(KERN_ERR "%s: The device wants to send " |
| 351 | "us more data than expected - " |
| 352 | "discarding data\n", |
| 353 | drive->name); |
| 354 | if (scsi) |
| 355 | temp = pc->buf_size - pc->xferred; |
| 356 | else |
| 357 | temp = 0; |
| 358 | if (temp) { |
| 359 | if (pc->sg) |
| 360 | io_buffers(drive, pc, temp, 0); |
| 361 | else |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 362 | tp_ops->input_data(drive, NULL, |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 363 | pc->cur_pos, temp); |
| 364 | printk(KERN_ERR "%s: transferred %d of " |
| 365 | "%d bytes\n", |
| 366 | drive->name, |
| 367 | temp, bcount); |
| 368 | } |
| 369 | pc->xferred += temp; |
| 370 | pc->cur_pos += temp; |
| 371 | ide_pad_transfer(drive, 0, bcount - temp); |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 372 | goto next_irq; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 373 | } |
| 374 | debug_log("The device wants to send us more data than " |
| 375 | "expected - allowing transfer\n"); |
| 376 | } |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 377 | xferfunc = tp_ops->input_data; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 378 | } else |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 379 | xferfunc = tp_ops->output_data; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 380 | |
| 381 | if ((drive->media == ide_floppy && !scsi && !pc->buf) || |
| 382 | (drive->media == ide_tape && !scsi && pc->bh) || |
Bartlomiej Zolnierkiewicz | acaa0f5 | 2008-10-10 22:39:36 +0200 | [diff] [blame] | 383 | (scsi && pc->sg)) { |
| 384 | int done = io_buffers(drive, pc, bcount, |
| 385 | !!(pc->flags & PC_FLAG_WRITING)); |
| 386 | |
| 387 | /* FIXME: don't do partial completions */ |
| 388 | if (drive->media == ide_floppy && !scsi) |
| 389 | ide_end_request(drive, 1, done >> 9); |
| 390 | } else |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 391 | xferfunc(drive, NULL, pc->cur_pos, bcount); |
| 392 | |
| 393 | /* Update the current position */ |
| 394 | pc->xferred += bcount; |
| 395 | pc->cur_pos += bcount; |
| 396 | |
| 397 | debug_log("[cmd %x] transferred %d bytes on that intr.\n", |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 398 | rq->cmd[0], bcount); |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 399 | next_irq: |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 400 | /* And set the interrupt handler again */ |
| 401 | ide_set_handler(drive, handler, timeout, expiry); |
| 402 | return ide_started; |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(ide_pc_intr); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 405 | |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 406 | static u8 ide_read_ireason(ide_drive_t *drive) |
| 407 | { |
| 408 | ide_task_t task; |
| 409 | |
| 410 | memset(&task, 0, sizeof(task)); |
| 411 | task.tf_flags = IDE_TFLAG_IN_NSECT; |
| 412 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 413 | drive->hwif->tp_ops->tf_read(drive, &task); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 414 | |
| 415 | return task.tf.nsect & 3; |
| 416 | } |
| 417 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 418 | static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason) |
| 419 | { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 420 | int retries = 100; |
| 421 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 422 | while (retries-- && ((ireason & ATAPI_COD) == 0 || |
| 423 | (ireason & ATAPI_IO))) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 424 | printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing " |
| 425 | "a packet command, retrying\n", drive->name); |
| 426 | udelay(100); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 427 | ireason = ide_read_ireason(drive); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 428 | if (retries == 0) { |
| 429 | printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing " |
| 430 | "a packet command, ignoring\n", |
| 431 | drive->name); |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 432 | ireason |= ATAPI_COD; |
| 433 | ireason &= ~ATAPI_IO; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | return ireason; |
| 438 | } |
| 439 | |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 440 | ide_startstop_t ide_transfer_pc(ide_drive_t *drive, |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 441 | ide_handler_t *handler, unsigned int timeout, |
| 442 | ide_expiry_t *expiry) |
| 443 | { |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 444 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 445 | ide_hwif_t *hwif = drive->hwif; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 446 | struct request *rq = hwif->hwgroup->rq; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 447 | ide_startstop_t startstop; |
| 448 | u8 ireason; |
| 449 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 450 | if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 451 | printk(KERN_ERR "%s: Strange, packet command initiated yet " |
| 452 | "DRQ isn't asserted\n", drive->name); |
| 453 | return startstop; |
| 454 | } |
| 455 | |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 456 | ireason = ide_read_ireason(drive); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 457 | if (drive->media == ide_tape && !drive->scsi) |
| 458 | ireason = ide_wait_ireason(drive, ireason); |
| 459 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 460 | if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 461 | printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing " |
| 462 | "a packet command\n", drive->name); |
| 463 | return ide_do_reset(drive); |
| 464 | } |
| 465 | |
| 466 | /* Set the interrupt routine */ |
| 467 | ide_set_handler(drive, handler, timeout, expiry); |
| 468 | |
| 469 | /* Begin DMA, if necessary */ |
| 470 | if (pc->flags & PC_FLAG_DMA_OK) { |
| 471 | pc->flags |= PC_FLAG_DMA_IN_PROGRESS; |
| 472 | hwif->dma_ops->dma_start(drive); |
| 473 | } |
| 474 | |
| 475 | /* Send the actual packet */ |
Borislav Petkov | ea68d27 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 476 | if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0) |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 477 | hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 478 | |
| 479 | return ide_started; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(ide_transfer_pc); |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 482 | |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 483 | ide_startstop_t ide_issue_pc(ide_drive_t *drive, |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 484 | ide_handler_t *handler, unsigned int timeout, |
| 485 | ide_expiry_t *expiry) |
| 486 | { |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 487 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 488 | ide_hwif_t *hwif = drive->hwif; |
| 489 | u16 bcount; |
| 490 | u8 dma = 0; |
| 491 | |
| 492 | /* We haven't transferred any data yet */ |
| 493 | pc->xferred = 0; |
| 494 | pc->cur_pos = pc->buf; |
| 495 | |
| 496 | /* Request to transfer the entire buffer at once */ |
| 497 | if (drive->media == ide_tape && !drive->scsi) |
| 498 | bcount = pc->req_xfer; |
| 499 | else |
| 500 | bcount = min(pc->req_xfer, 63 * 1024); |
| 501 | |
| 502 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
| 503 | pc->flags &= ~PC_FLAG_DMA_ERROR; |
| 504 | ide_dma_off(drive); |
| 505 | } |
| 506 | |
| 507 | if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) { |
| 508 | if (drive->scsi) |
| 509 | hwif->sg_mapped = 1; |
| 510 | dma = !hwif->dma_ops->dma_setup(drive); |
| 511 | if (drive->scsi) |
| 512 | hwif->sg_mapped = 0; |
| 513 | } |
| 514 | |
| 515 | if (!dma) |
| 516 | pc->flags &= ~PC_FLAG_DMA_OK; |
| 517 | |
| 518 | ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE, |
| 519 | bcount, dma); |
| 520 | |
| 521 | /* Issue the packet command */ |
Borislav Petkov | ac77ef8 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 522 | if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) { |
Bartlomiej Zolnierkiewicz | aaaade3 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 523 | ide_execute_command(drive, ATA_CMD_PACKET, handler, |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 524 | timeout, NULL); |
| 525 | return ide_started; |
| 526 | } else { |
| 527 | ide_execute_pkt_cmd(drive); |
| 528 | return (*handler)(drive); |
| 529 | } |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(ide_issue_pc); |