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