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> |
Geert Uytterhoeven | 479edf0 | 2009-03-31 20:14:57 +0200 | [diff] [blame] | 9 | #include <linux/scatterlist.h> |
| 10 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 11 | #include <scsi/scsi.h> |
| 12 | |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 13 | #define DRV_NAME "ide-atapi" |
| 14 | #define PFX DRV_NAME ": " |
| 15 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 16 | #ifdef DEBUG |
| 17 | #define debug_log(fmt, args...) \ |
| 18 | printk(KERN_INFO "ide: " fmt, ## args) |
| 19 | #else |
| 20 | #define debug_log(fmt, args...) do {} while (0) |
| 21 | #endif |
| 22 | |
Borislav Petkov | 8c66285 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 23 | #define ATAPI_MIN_CDB_BYTES 12 |
| 24 | |
Borislav Petkov | 991cb26 | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 25 | static inline int dev_is_idecd(ide_drive_t *drive) |
| 26 | { |
Borislav Petkov | 5317464 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 27 | return drive->media == ide_cdrom || drive->media == ide_optical; |
Borislav Petkov | 991cb26 | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Bartlomiej Zolnierkiewicz | 51509ee | 2008-10-10 22:39:34 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Check whether we can support a device, |
| 32 | * based on the ATAPI IDENTIFY command results. |
| 33 | */ |
| 34 | int ide_check_atapi_device(ide_drive_t *drive, const char *s) |
| 35 | { |
| 36 | u16 *id = drive->id; |
| 37 | u8 gcw[2], protocol, device_type, removable, drq_type, packet_size; |
| 38 | |
| 39 | *((u16 *)&gcw) = id[ATA_ID_CONFIG]; |
| 40 | |
| 41 | protocol = (gcw[1] & 0xC0) >> 6; |
| 42 | device_type = gcw[1] & 0x1F; |
| 43 | removable = (gcw[0] & 0x80) >> 7; |
| 44 | drq_type = (gcw[0] & 0x60) >> 5; |
| 45 | packet_size = gcw[0] & 0x03; |
| 46 | |
| 47 | #ifdef CONFIG_PPC |
| 48 | /* kludge for Apple PowerBook internal zip */ |
| 49 | if (drive->media == ide_floppy && device_type == 5 && |
| 50 | !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") && |
| 51 | strstr((char *)&id[ATA_ID_PROD], "ZIP")) |
| 52 | device_type = 0; |
| 53 | #endif |
| 54 | |
| 55 | if (protocol != 2) |
| 56 | printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n", |
| 57 | s, drive->name, protocol); |
| 58 | else if ((drive->media == ide_floppy && device_type != 0) || |
| 59 | (drive->media == ide_tape && device_type != 1)) |
| 60 | printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n", |
| 61 | s, drive->name, device_type); |
| 62 | else if (removable == 0) |
| 63 | printk(KERN_ERR "%s: %s: the removable flag is not set\n", |
| 64 | s, drive->name); |
| 65 | else if (drive->media == ide_floppy && drq_type == 3) |
| 66 | printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not " |
| 67 | "supported\n", s, drive->name, drq_type); |
| 68 | else if (packet_size != 0) |
| 69 | printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 " |
| 70 | "bytes\n", s, drive->name, packet_size); |
| 71 | else |
| 72 | return 1; |
| 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(ide_check_atapi_device); |
| 76 | |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 77 | void ide_init_pc(struct ide_atapi_pc *pc) |
| 78 | { |
| 79 | memset(pc, 0, sizeof(*pc)); |
Bartlomiej Zolnierkiewicz | 7bf7420 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 80 | } |
| 81 | EXPORT_SYMBOL_GPL(ide_init_pc); |
| 82 | |
Bartlomiej Zolnierkiewicz | 7645c15 | 2008-10-10 22:39:37 +0200 | [diff] [blame] | 83 | /* |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 84 | * Add a special packet command request to the tail of the request queue, |
| 85 | * and wait for it to be serviced. |
| 86 | */ |
| 87 | int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk, |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 88 | struct ide_atapi_pc *pc, void *buf, unsigned int bufflen) |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 89 | { |
| 90 | struct request *rq; |
| 91 | int error; |
| 92 | |
| 93 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 94 | rq->cmd_type = REQ_TYPE_SPECIAL; |
Tejun Heo | c267cc1 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 95 | rq->special = (char *)pc; |
Borislav Petkov | 3eb76c1 | 2009-03-13 21:16:12 +0100 | [diff] [blame] | 96 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 97 | if (buf && bufflen) { |
| 98 | error = blk_rq_map_kern(drive->queue, rq, buf, bufflen, |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 99 | GFP_NOIO); |
| 100 | if (error) |
| 101 | goto put_req; |
Borislav Petkov | 3eb76c1 | 2009-03-13 21:16:12 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 104 | memcpy(rq->cmd, pc->c, 12); |
| 105 | if (drive->media == ide_tape) |
| 106 | rq->cmd[13] = REQ_IDETAPE_PC1; |
| 107 | error = blk_execute_rq(drive->queue, disk, rq, 0); |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 108 | put_req: |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 109 | blk_put_request(rq); |
Bartlomiej Zolnierkiewicz | 2ac07d9 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 110 | return error; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(ide_queue_pc_tail); |
| 113 | |
Bartlomiej Zolnierkiewicz | de699ad | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 114 | int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk) |
| 115 | { |
| 116 | struct ide_atapi_pc pc; |
| 117 | |
| 118 | ide_init_pc(&pc); |
| 119 | pc.c[0] = TEST_UNIT_READY; |
| 120 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 121 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Bartlomiej Zolnierkiewicz | de699ad | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL_GPL(ide_do_test_unit_ready); |
| 124 | |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 125 | int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start) |
| 126 | { |
| 127 | struct ide_atapi_pc pc; |
| 128 | |
| 129 | ide_init_pc(&pc); |
| 130 | pc.c[0] = START_STOP; |
| 131 | pc.c[4] = start; |
| 132 | |
| 133 | if (drive->media == ide_tape) |
| 134 | pc.flags |= PC_FLAG_WAIT_FOR_DSC; |
| 135 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 136 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Bartlomiej Zolnierkiewicz | 0c8a6c7 | 2008-10-10 22:39:39 +0200 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL_GPL(ide_do_start_stop); |
| 139 | |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 140 | int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on) |
| 141 | { |
| 142 | struct ide_atapi_pc pc; |
| 143 | |
Bartlomiej Zolnierkiewicz | 42619d3 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 144 | if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | |
| 147 | ide_init_pc(&pc); |
| 148 | pc.c[0] = ALLOW_MEDIUM_REMOVAL; |
| 149 | pc.c[4] = on; |
| 150 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 151 | return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); |
Bartlomiej Zolnierkiewicz | 0578042 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL_GPL(ide_set_media_lock); |
| 154 | |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 155 | void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc) |
| 156 | { |
| 157 | ide_init_pc(pc); |
| 158 | pc->c[0] = REQUEST_SENSE; |
| 159 | if (drive->media == ide_floppy) { |
| 160 | pc->c[4] = 255; |
| 161 | pc->req_xfer = 18; |
| 162 | } else { |
| 163 | pc->c[4] = 20; |
| 164 | pc->req_xfer = 20; |
| 165 | } |
| 166 | } |
| 167 | EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd); |
| 168 | |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 169 | void ide_prep_sense(ide_drive_t *drive, struct request *rq) |
| 170 | { |
| 171 | struct request_sense *sense = &drive->sense_data; |
| 172 | struct request *sense_rq = &drive->sense_rq; |
| 173 | unsigned int cmd_len, sense_len; |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 174 | int err; |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 175 | |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 176 | switch (drive->media) { |
| 177 | case ide_floppy: |
| 178 | cmd_len = 255; |
| 179 | sense_len = 18; |
| 180 | break; |
| 181 | case ide_tape: |
| 182 | cmd_len = 20; |
| 183 | sense_len = 20; |
| 184 | break; |
| 185 | default: |
| 186 | cmd_len = 18; |
| 187 | sense_len = 18; |
| 188 | } |
| 189 | |
| 190 | BUG_ON(sense_len > sizeof(*sense)); |
| 191 | |
| 192 | if (blk_sense_request(rq) || drive->sense_rq_armed) |
| 193 | return; |
| 194 | |
| 195 | memset(sense, 0, sizeof(*sense)); |
| 196 | |
| 197 | blk_rq_init(rq->q, sense_rq); |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 198 | |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 199 | err = blk_rq_map_kern(drive->queue, sense_rq, sense, sense_len, |
| 200 | GFP_NOIO); |
| 201 | if (unlikely(err)) { |
| 202 | if (printk_ratelimit()) |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 203 | printk(KERN_WARNING PFX "%s: failed to map sense " |
| 204 | "buffer\n", drive->name); |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | sense_rq->rq_disk = rq->rq_disk; |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 209 | sense_rq->cmd[0] = GPCMD_REQUEST_SENSE; |
| 210 | sense_rq->cmd[4] = cmd_len; |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 211 | sense_rq->cmd_type = REQ_TYPE_SENSE; |
| 212 | sense_rq->cmd_flags |= REQ_PREEMPT; |
| 213 | |
| 214 | if (drive->media == ide_tape) |
| 215 | sense_rq->cmd[13] = REQ_IDETAPE_PC1; |
| 216 | |
| 217 | drive->sense_rq_armed = true; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(ide_prep_sense); |
| 220 | |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 221 | int ide_queue_sense_rq(ide_drive_t *drive, void *special) |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 222 | { |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 223 | /* deferred failure from ide_prep_sense() */ |
| 224 | if (!drive->sense_rq_armed) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 225 | printk(KERN_WARNING PFX "%s: error queuing a sense request\n", |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 226 | drive->name); |
| 227 | return -ENOMEM; |
| 228 | } |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 229 | |
| 230 | drive->sense_rq.special = special; |
| 231 | drive->sense_rq_armed = false; |
| 232 | |
| 233 | drive->hwif->rq = NULL; |
| 234 | |
| 235 | elv_add_request(drive->queue, &drive->sense_rq, |
| 236 | ELEVATOR_INSERT_FRONT, 0); |
Tejun Heo | 5c4be57 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 237 | return 0; |
Borislav Petkov | a1df516 | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(ide_queue_sense_rq); |
| 240 | |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 241 | /* |
| 242 | * Called when an error was detected during the last packet command. |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 243 | * We queue a request sense packet command at the head of the request |
| 244 | * queue. |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 245 | */ |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 246 | void ide_retry_pc(ide_drive_t *drive) |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 247 | { |
Tejun Heo | 8f6205c | 2009-05-08 11:53:59 +0900 | [diff] [blame] | 248 | struct request *failed_rq = drive->hwif->rq; |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 249 | struct request *sense_rq = &drive->sense_rq; |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 250 | struct ide_atapi_pc *pc = &drive->request_sense_pc; |
| 251 | |
| 252 | (void)ide_read_error(drive); |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 253 | |
| 254 | /* init pc from sense_rq */ |
| 255 | ide_init_pc(pc); |
| 256 | memcpy(pc->c, sense_rq->cmd, 12); |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 257 | |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 258 | if (drive->media == ide_tape) |
Borislav Petkov | 626542c | 2009-06-07 15:37:05 +0200 | [diff] [blame] | 259 | drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC; |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 260 | |
Tejun Heo | 8f6205c | 2009-05-08 11:53:59 +0900 | [diff] [blame] | 261 | /* |
| 262 | * Push back the failed request and put request sense on top |
| 263 | * of it. The failed command will be retried after sense data |
| 264 | * is acquired. |
| 265 | */ |
| 266 | blk_requeue_request(failed_rq->q, failed_rq); |
| 267 | drive->hwif->rq = NULL; |
| 268 | if (ide_queue_sense_rq(drive, pc)) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 269 | blk_start_request(failed_rq); |
Tejun Heo | 8f6205c | 2009-05-08 11:53:59 +0900 | [diff] [blame] | 270 | ide_complete_rq(drive, -EIO, blk_rq_bytes(failed_rq)); |
| 271 | } |
Bartlomiej Zolnierkiewicz | 6b0da28 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 272 | } |
| 273 | EXPORT_SYMBOL_GPL(ide_retry_pc); |
| 274 | |
Borislav Petkov | 4cad085 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 275 | int ide_cd_expiry(ide_drive_t *drive) |
| 276 | { |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 277 | struct request *rq = drive->hwif->rq; |
Borislav Petkov | 4cad085 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 278 | unsigned long wait = 0; |
| 279 | |
| 280 | debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]); |
| 281 | |
| 282 | /* |
| 283 | * Some commands are *slow* and normally take a long time to complete. |
| 284 | * Usually we can use the ATAPI "disconnect" to bypass this, but not all |
| 285 | * commands/drives support that. Let ide_timer_expiry keep polling us |
| 286 | * for these. |
| 287 | */ |
| 288 | switch (rq->cmd[0]) { |
| 289 | case GPCMD_BLANK: |
| 290 | case GPCMD_FORMAT_UNIT: |
| 291 | case GPCMD_RESERVE_RZONE_TRACK: |
| 292 | case GPCMD_CLOSE_TRACK: |
| 293 | case GPCMD_FLUSH_CACHE: |
| 294 | wait = ATAPI_WAIT_PC; |
| 295 | break; |
| 296 | default: |
| 297 | if (!(rq->cmd_flags & REQ_QUIET)) |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 298 | printk(KERN_INFO PFX "cmd 0x%x timed out\n", |
Borislav Petkov | 4cad085 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 299 | rq->cmd[0]); |
| 300 | wait = 0; |
| 301 | break; |
| 302 | } |
| 303 | return wait; |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(ide_cd_expiry); |
| 306 | |
Borislav Petkov | 392de1d | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 307 | int ide_cd_get_xferlen(struct request *rq) |
| 308 | { |
| 309 | if (blk_fs_request(rq)) |
| 310 | return 32768; |
| 311 | else if (blk_sense_request(rq) || blk_pc_request(rq) || |
| 312 | rq->cmd_type == REQ_TYPE_ATA_PC) |
Tejun Heo | 34b7d2c | 2009-05-07 22:24:43 +0900 | [diff] [blame] | 313 | return blk_rq_bytes(rq); |
Borislav Petkov | 392de1d | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 314 | else |
| 315 | return 0; |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(ide_cd_get_xferlen); |
| 318 | |
Bartlomiej Zolnierkiewicz | 0d6a975 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 319 | void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason) |
| 320 | { |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 321 | struct ide_taskfile tf; |
Bartlomiej Zolnierkiewicz | 0d6a975 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 322 | |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 323 | drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT | |
| 324 | IDE_VALID_LBAM | IDE_VALID_LBAH); |
Bartlomiej Zolnierkiewicz | 0d6a975 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 325 | |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 326 | *bcount = (tf.lbah << 8) | tf.lbam; |
| 327 | *ireason = tf.nsect & 3; |
Bartlomiej Zolnierkiewicz | 0d6a975 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 328 | } |
| 329 | EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason); |
| 330 | |
Bartlomiej Zolnierkiewicz | aa5d2de7 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 331 | /* |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 332 | * Check the contents of the interrupt reason register and attempt to recover if |
| 333 | * there are problems. |
| 334 | * |
| 335 | * Returns: |
| 336 | * - 0 if everything's ok |
| 337 | * - 1 if the request has to be terminated. |
| 338 | */ |
| 339 | int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len, |
| 340 | int ireason, int rw) |
| 341 | { |
| 342 | ide_hwif_t *hwif = drive->hwif; |
| 343 | |
| 344 | debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw); |
| 345 | |
| 346 | if (ireason == (!rw << 1)) |
| 347 | return 0; |
| 348 | else if (ireason == (rw << 1)) { |
| 349 | printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n", |
| 350 | drive->name, __func__); |
| 351 | |
| 352 | if (dev_is_idecd(drive)) |
| 353 | ide_pad_transfer(drive, rw, len); |
| 354 | } else if (!rw && ireason == ATAPI_COD) { |
| 355 | if (dev_is_idecd(drive)) { |
| 356 | /* |
| 357 | * Some drives (ASUS) seem to tell us that status info |
| 358 | * is available. Just get it and ignore. |
| 359 | */ |
| 360 | (void)hwif->tp_ops->read_status(hwif); |
| 361 | return 0; |
| 362 | } |
| 363 | } else { |
| 364 | if (ireason & ATAPI_COD) |
| 365 | printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name, |
| 366 | __func__); |
| 367 | |
| 368 | /* drive wants a command packet, or invalid ireason... */ |
| 369 | printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n", |
| 370 | drive->name, __func__, ireason); |
| 371 | } |
| 372 | |
| 373 | if (dev_is_idecd(drive) && rq->cmd_type == REQ_TYPE_ATA_PC) |
| 374 | rq->cmd_flags |= REQ_FAILED; |
| 375 | |
| 376 | return 1; |
| 377 | } |
| 378 | EXPORT_SYMBOL_GPL(ide_check_ireason); |
| 379 | |
| 380 | /* |
Bartlomiej Zolnierkiewicz | aa5d2de7 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 381 | * This is the usual interrupt handler which will be called during a packet |
| 382 | * command. We will transfer some of the data (as requested by the drive) |
| 383 | * and will re-point interrupt handler to us. |
| 384 | */ |
| 385 | static ide_startstop_t ide_pc_intr(ide_drive_t *drive) |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 386 | { |
Bartlomiej Zolnierkiewicz | 2b9efba | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 387 | struct ide_atapi_pc *pc = drive->pc; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 388 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | f094d4d8 | 2009-03-31 20:15:24 +0200 | [diff] [blame] | 389 | struct ide_cmd *cmd = &hwif->cmd; |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 390 | struct request *rq = hwif->rq; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 391 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
Bartlomiej Zolnierkiewicz | d93bc45 | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 392 | unsigned int timeout, done; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 393 | u16 bcount; |
Borislav Petkov | 5d655a0 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 394 | u8 stat, ireason, dsc = 0; |
Bartlomiej Zolnierkiewicz | d93bc45 | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 395 | u8 write = !!(pc->flags & PC_FLAG_WRITING); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 396 | |
| 397 | debug_log("Enter %s - interrupt handler\n", __func__); |
| 398 | |
Borislav Petkov | 5d655a0 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 399 | timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD |
| 400 | : WAIT_TAPE_CMD; |
Bartlomiej Zolnierkiewicz | 844b946 | 2008-10-13 21:39:31 +0200 | [diff] [blame] | 401 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 402 | /* Clear the interrupt */ |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame] | 403 | stat = tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 404 | |
| 405 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
Bartlomiej Zolnierkiewicz | 88b4132 | 2009-03-31 20:15:22 +0200 | [diff] [blame] | 406 | int rc; |
Bartlomiej Zolnierkiewicz | 4453011 | 2009-03-31 20:15:20 +0200 | [diff] [blame] | 407 | |
Bartlomiej Zolnierkiewicz | 88b4132 | 2009-03-31 20:15:22 +0200 | [diff] [blame] | 408 | drive->waiting_for_dma = 0; |
| 409 | rc = hwif->dma_ops->dma_end(drive); |
Bartlomiej Zolnierkiewicz | f094d4d8 | 2009-03-31 20:15:24 +0200 | [diff] [blame] | 410 | ide_dma_unmap_sg(drive, cmd); |
Bartlomiej Zolnierkiewicz | 4453011 | 2009-03-31 20:15:20 +0200 | [diff] [blame] | 411 | |
| 412 | if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) { |
Borislav Petkov | 5d655a0 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 413 | if (drive->media == ide_floppy) |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 414 | printk(KERN_ERR PFX "%s: DMA %s error\n", |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 415 | drive->name, rq_data_dir(pc->rq) |
| 416 | ? "write" : "read"); |
| 417 | pc->flags |= PC_FLAG_DMA_ERROR; |
Tejun Heo | 6d70038 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 418 | } else |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 419 | rq->resid_len = 0; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 420 | debug_log("%s: DMA finished\n", drive->name); |
| 421 | } |
| 422 | |
| 423 | /* No more interrupts */ |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 424 | if ((stat & ATA_DRQ) == 0) { |
Borislav Petkov | 5b6c942 | 2009-03-31 20:15:34 +0200 | [diff] [blame] | 425 | int uptodate, error; |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 426 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 427 | debug_log("Packet command completed, %d bytes transferred\n", |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 428 | blk_rq_bytes(rq)); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 429 | |
| 430 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
| 431 | |
| 432 | local_irq_enable_in_hardirq(); |
| 433 | |
Borislav Petkov | 5d655a0 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 434 | if (drive->media == ide_tape && |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 435 | (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE) |
| 436 | stat &= ~ATA_ERR; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 437 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 438 | if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) { |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 439 | /* Error detected */ |
| 440 | debug_log("%s: I/O error\n", drive->name); |
| 441 | |
Borislav Petkov | 5d655a0 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 442 | if (drive->media != ide_tape) |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 443 | pc->rq->errors++; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 444 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 445 | if (rq->cmd[0] == REQUEST_SENSE) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 446 | printk(KERN_ERR PFX "%s: I/O error in request " |
| 447 | "sense command\n", drive->name); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 448 | return ide_do_reset(drive); |
| 449 | } |
| 450 | |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 451 | debug_log("[cmd %x]: check condition\n", rq->cmd[0]); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 452 | |
| 453 | /* Retry operation */ |
Borislav Petkov | 6b544fc | 2009-04-19 07:00:42 +0900 | [diff] [blame] | 454 | ide_retry_pc(drive); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 455 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 456 | /* queued, but not started */ |
| 457 | return ide_stopped; |
| 458 | } |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 459 | pc->error = 0; |
Bartlomiej Zolnierkiewicz | b14c721 | 2008-10-13 21:39:30 +0200 | [diff] [blame] | 460 | |
| 461 | if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0) |
| 462 | dsc = 1; |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 463 | |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 464 | /* |
| 465 | * ->pc_callback() might change rq->data_len for |
| 466 | * residual count, cache total length. |
| 467 | */ |
Tejun Heo | 21d9c5d | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 468 | done = blk_rq_bytes(rq); |
Tejun Heo | b3071d1 | 2009-04-19 08:46:02 +0900 | [diff] [blame] | 469 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 470 | /* Command finished - Call the callback function */ |
Bartlomiej Zolnierkiewicz | 03a2faa | 2009-03-27 12:46:36 +0100 | [diff] [blame] | 471 | uptodate = drive->pc_callback(drive, dsc); |
| 472 | |
| 473 | if (uptodate == 0) |
| 474 | drive->failed_pc = NULL; |
| 475 | |
Bartlomiej Zolnierkiewicz | 6902a53 | 2009-03-27 12:46:43 +0100 | [diff] [blame] | 476 | if (blk_special_request(rq)) { |
| 477 | rq->errors = 0; |
Borislav Petkov | 5b6c942 | 2009-03-31 20:15:34 +0200 | [diff] [blame] | 478 | error = 0; |
Bartlomiej Zolnierkiewicz | 89f78b3 | 2009-03-27 12:46:43 +0100 | [diff] [blame] | 479 | } else { |
Bartlomiej Zolnierkiewicz | 349d12a | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 480 | |
Bartlomiej Zolnierkiewicz | 89f78b3 | 2009-03-27 12:46:43 +0100 | [diff] [blame] | 481 | if (blk_fs_request(rq) == 0 && uptodate <= 0) { |
| 482 | if (rq->errors == 0) |
| 483 | rq->errors = -EIO; |
| 484 | } |
Bartlomiej Zolnierkiewicz | 349d12a | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 485 | |
Borislav Petkov | 5b6c942 | 2009-03-31 20:15:34 +0200 | [diff] [blame] | 486 | error = uptodate ? 0 : -EIO; |
Bartlomiej Zolnierkiewicz | 89f78b3 | 2009-03-27 12:46:43 +0100 | [diff] [blame] | 487 | } |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 488 | |
Tejun Heo | c3a4d78 | 2009-05-07 22:24:37 +0900 | [diff] [blame] | 489 | ide_complete_rq(drive, error, blk_rq_bytes(rq)); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 490 | return ide_stopped; |
| 491 | } |
| 492 | |
| 493 | if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) { |
| 494 | pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS; |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 495 | printk(KERN_ERR PFX "%s: The device wants to issue more " |
| 496 | "interrupts in DMA mode\n", drive->name); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 497 | ide_dma_off(drive); |
| 498 | return ide_do_reset(drive); |
| 499 | } |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 500 | |
Bartlomiej Zolnierkiewicz | 1823649 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 501 | /* Get the number of bytes to transfer on this interrupt. */ |
| 502 | ide_read_bcount_and_ireason(drive, &bcount, &ireason); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 503 | |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 504 | if (ide_check_ireason(drive, rq, bcount, ireason, write)) |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 505 | return ide_do_reset(drive); |
Borislav Petkov | 8fccf89 | 2008-07-23 19:56:01 +0200 | [diff] [blame] | 506 | |
Tejun Heo | 6d70038 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 507 | done = min_t(unsigned int, bcount, cmd->nleft); |
| 508 | ide_pio_bytes(drive, cmd, write, done); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 509 | |
Tejun Heo | 6d70038 | 2009-04-19 08:46:03 +0900 | [diff] [blame] | 510 | /* Update transferred byte count */ |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 511 | rq->resid_len -= done; |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 512 | |
Bartlomiej Zolnierkiewicz | d93bc45 | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 513 | bcount -= done; |
| 514 | |
| 515 | if (bcount) |
| 516 | ide_pad_transfer(drive, write, bcount); |
| 517 | |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 518 | debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n", |
| 519 | rq->cmd[0], done, bcount, rq->resid_len); |
Bartlomiej Zolnierkiewicz | d93bc45 | 2009-03-31 20:15:26 +0200 | [diff] [blame] | 520 | |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 521 | /* And set the interrupt handler again */ |
Bartlomiej Zolnierkiewicz | 60c0cd0 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 522 | ide_set_handler(drive, ide_pc_intr, timeout); |
Bartlomiej Zolnierkiewicz | 646c0cb | 2008-07-15 21:22:03 +0200 | [diff] [blame] | 523 | return ide_started; |
| 524 | } |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 525 | |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 526 | static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf, |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 527 | u16 bcount, u8 dma) |
Bartlomiej Zolnierkiewicz | 7a254df | 2009-03-24 23:22:39 +0100 | [diff] [blame] | 528 | { |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 529 | cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO; |
| 530 | cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM | |
| 531 | IDE_VALID_FEATURE | valid_tf; |
Bartlomiej Zolnierkiewicz | 35b5d0b | 2009-03-27 12:46:47 +0100 | [diff] [blame] | 532 | cmd->tf.command = ATA_CMD_PACKET; |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 533 | cmd->tf.feature = dma; /* Use PIO/DMA */ |
| 534 | cmd->tf.lbam = bcount & 0xff; |
| 535 | cmd->tf.lbah = (bcount >> 8) & 0xff; |
Bartlomiej Zolnierkiewicz | 7a254df | 2009-03-24 23:22:39 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 538 | static u8 ide_read_ireason(ide_drive_t *drive) |
| 539 | { |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 540 | struct ide_taskfile tf; |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 541 | |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 542 | drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 543 | |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 544 | return tf.nsect & 3; |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 547 | static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason) |
| 548 | { |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 549 | int retries = 100; |
| 550 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 551 | while (retries-- && ((ireason & ATAPI_COD) == 0 || |
| 552 | (ireason & ATAPI_IO))) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 553 | printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing " |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 554 | "a packet command, retrying\n", drive->name); |
| 555 | udelay(100); |
Bartlomiej Zolnierkiewicz | 88a7210 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 556 | ireason = ide_read_ireason(drive); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 557 | if (retries == 0) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 558 | printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing" |
| 559 | " a packet command, ignoring\n", |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 560 | drive->name); |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 561 | ireason |= ATAPI_COD; |
| 562 | ireason &= ~ATAPI_IO; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | return ireason; |
| 567 | } |
| 568 | |
Bartlomiej Zolnierkiewicz | baf08f0 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 569 | static int ide_delayed_transfer_pc(ide_drive_t *drive) |
| 570 | { |
| 571 | /* Send the actual packet */ |
| 572 | drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12); |
| 573 | |
| 574 | /* Timeout for the packet command */ |
| 575 | return WAIT_FLOPPY_CMD; |
| 576 | } |
| 577 | |
| 578 | static ide_startstop_t ide_transfer_pc(ide_drive_t *drive) |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 579 | { |
Borislav Petkov | b16aabc | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 580 | struct ide_atapi_pc *uninitialized_var(pc); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 581 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 582 | struct request *rq = hwif->rq; |
Bartlomiej Zolnierkiewicz | baf08f0 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 583 | ide_expiry_t *expiry; |
| 584 | unsigned int timeout; |
Borislav Petkov | 8c66285 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 585 | int cmd_len; |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 586 | ide_startstop_t startstop; |
| 587 | u8 ireason; |
| 588 | |
Bartlomiej Zolnierkiewicz | 3a7d248 | 2008-10-10 22:39:21 +0200 | [diff] [blame] | 589 | if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 590 | printk(KERN_ERR PFX "%s: Strange, packet command initiated yet " |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 591 | "DRQ isn't asserted\n", drive->name); |
| 592 | return startstop; |
| 593 | } |
| 594 | |
Borislav Petkov | 5f25843 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 595 | if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) { |
| 596 | if (drive->dma) |
| 597 | drive->waiting_for_dma = 1; |
| 598 | } |
| 599 | |
Borislav Petkov | 8c66285 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 600 | if (dev_is_idecd(drive)) { |
| 601 | /* ATAPI commands get padded out to 12 bytes minimum */ |
| 602 | cmd_len = COMMAND_SIZE(rq->cmd[0]); |
| 603 | if (cmd_len < ATAPI_MIN_CDB_BYTES) |
| 604 | cmd_len = ATAPI_MIN_CDB_BYTES; |
Borislav Petkov | def860d | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 605 | |
| 606 | timeout = rq->timeout; |
| 607 | expiry = ide_cd_expiry; |
| 608 | } else { |
Borislav Petkov | b16aabc | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 609 | pc = drive->pc; |
| 610 | |
Borislav Petkov | 8c66285 | 2009-01-02 16:12:54 +0100 | [diff] [blame] | 611 | cmd_len = ATAPI_MIN_CDB_BYTES; |
| 612 | |
Borislav Petkov | def860d | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 613 | /* |
| 614 | * If necessary schedule the packet transfer to occur 'timeout' |
Martin Olsson | 19af5cd | 2009-04-23 11:37:37 +0200 | [diff] [blame] | 615 | * milliseconds later in ide_delayed_transfer_pc() after the |
Borislav Petkov | def860d | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 616 | * device says it's ready for a packet. |
| 617 | */ |
| 618 | if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) { |
| 619 | timeout = drive->pc_delay; |
| 620 | expiry = &ide_delayed_transfer_pc; |
| 621 | } else { |
| 622 | timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD |
| 623 | : WAIT_TAPE_CMD; |
| 624 | expiry = NULL; |
| 625 | } |
Borislav Petkov | 06cc277 | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 626 | |
| 627 | ireason = ide_read_ireason(drive); |
| 628 | if (drive->media == ide_tape) |
| 629 | ireason = ide_wait_ireason(drive, ireason); |
| 630 | |
| 631 | if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) { |
Borislav Petkov | 103f703 | 2009-04-26 10:39:07 +0200 | [diff] [blame] | 632 | printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while " |
| 633 | "issuing a packet command\n", drive->name); |
Borislav Petkov | 06cc277 | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 634 | |
| 635 | return ide_do_reset(drive); |
| 636 | } |
Bartlomiej Zolnierkiewicz | baf08f0 | 2008-10-13 21:39:32 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Bartlomiej Zolnierkiewicz | 60c0cd0 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 639 | hwif->expiry = expiry; |
| 640 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 641 | /* Set the interrupt routine */ |
Borislav Petkov | d6251d4 | 2009-01-06 17:20:58 +0100 | [diff] [blame] | 642 | ide_set_handler(drive, |
| 643 | (dev_is_idecd(drive) ? drive->irq_handler |
| 644 | : ide_pc_intr), |
Bartlomiej Zolnierkiewicz | 60c0cd0 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 645 | timeout); |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 646 | |
Borislav Petkov | 2eba082 | 2009-03-31 20:14:58 +0200 | [diff] [blame] | 647 | /* Send the actual packet */ |
| 648 | if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0) |
| 649 | hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len); |
| 650 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 651 | /* Begin DMA, if necessary */ |
Borislav Petkov | b16aabc | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 652 | if (dev_is_idecd(drive)) { |
| 653 | if (drive->dma) |
| 654 | hwif->dma_ops->dma_start(drive); |
| 655 | } else { |
| 656 | if (pc->flags & PC_FLAG_DMA_OK) { |
| 657 | pc->flags |= PC_FLAG_DMA_IN_PROGRESS; |
| 658 | hwif->dma_ops->dma_start(drive); |
| 659 | } |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Bartlomiej Zolnierkiewicz | 594c16d | 2008-07-15 21:21:58 +0200 | [diff] [blame] | 662 | return ide_started; |
| 663 | } |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 664 | |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 665 | ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd) |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 666 | { |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 667 | struct ide_atapi_pc *pc; |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 668 | ide_hwif_t *hwif = drive->hwif; |
Borislav Petkov | 4cad085 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 669 | ide_expiry_t *expiry = NULL; |
Bartlomiej Zolnierkiewicz | e6830a8 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 670 | struct request *rq = hwif->rq; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 671 | unsigned int timeout, bytes; |
Borislav Petkov | 392de1d | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 672 | u16 bcount; |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 673 | u8 valid_tf; |
Bartlomiej Zolnierkiewicz | 35b5d0b | 2009-03-27 12:46:47 +0100 | [diff] [blame] | 674 | u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT); |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 675 | |
Borislav Petkov | ed48554 | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 676 | if (dev_is_idecd(drive)) { |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 677 | valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL; |
Bartlomiej Zolnierkiewicz | e6830a8 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 678 | bcount = ide_cd_get_xferlen(rq); |
Borislav Petkov | 4cad085 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 679 | expiry = ide_cd_expiry; |
Borislav Petkov | 28ad91d | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 680 | timeout = ATAPI_WAIT_PC; |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 681 | |
Bartlomiej Zolnierkiewicz | 5ae5412 | 2009-03-31 20:15:20 +0200 | [diff] [blame] | 682 | if (drive->dma) |
| 683 | drive->dma = !ide_dma_prepare(drive, cmd); |
Borislav Petkov | ed48554 | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 684 | } else { |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 685 | pc = drive->pc; |
| 686 | |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 687 | valid_tf = IDE_VALID_DEVICE; |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 688 | bytes = blk_rq_bytes(rq); |
Borislav Petkov | dfb7e62 | 2009-05-01 20:35:21 +0200 | [diff] [blame] | 689 | bcount = ((drive->media == ide_tape) ? bytes |
| 690 | : min_t(unsigned int, |
| 691 | bytes, 63 * 1024)); |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 692 | |
Borislav Petkov | 077e6db | 2009-05-01 21:21:02 +0200 | [diff] [blame] | 693 | /* We haven't transferred any data yet */ |
| 694 | rq->resid_len = bcount; |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 695 | |
| 696 | if (pc->flags & PC_FLAG_DMA_ERROR) { |
| 697 | pc->flags &= ~PC_FLAG_DMA_ERROR; |
| 698 | ide_dma_off(drive); |
| 699 | } |
| 700 | |
Bartlomiej Zolnierkiewicz | 5ae5412 | 2009-03-31 20:15:20 +0200 | [diff] [blame] | 701 | if (pc->flags & PC_FLAG_DMA_OK) |
| 702 | drive->dma = !ide_dma_prepare(drive, cmd); |
Borislav Petkov | d77612a | 2009-01-02 16:12:55 +0100 | [diff] [blame] | 703 | |
| 704 | if (!drive->dma) |
| 705 | pc->flags &= ~PC_FLAG_DMA_OK; |
Borislav Petkov | 28ad91d | 2009-01-02 16:12:56 +0100 | [diff] [blame] | 706 | |
| 707 | timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD |
| 708 | : WAIT_TAPE_CMD; |
Borislav Petkov | ed48554 | 2009-01-02 16:12:52 +0100 | [diff] [blame] | 709 | } |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 710 | |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 711 | ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma); |
Bartlomiej Zolnierkiewicz | b788ee9 | 2009-03-27 12:46:46 +0100 | [diff] [blame] | 712 | |
| 713 | (void)do_rw_taskfile(drive, cmd); |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 714 | |
Bartlomiej Zolnierkiewicz | 35b5d0b | 2009-03-27 12:46:47 +0100 | [diff] [blame] | 715 | if (drq_int) { |
Borislav Petkov | 5f25843 | 2009-01-02 16:12:53 +0100 | [diff] [blame] | 716 | if (drive->dma) |
| 717 | drive->waiting_for_dma = 0; |
Bartlomiej Zolnierkiewicz | 22117d6 | 2009-03-27 12:46:47 +0100 | [diff] [blame] | 718 | hwif->expiry = expiry; |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 719 | } |
Bartlomiej Zolnierkiewicz | 35b5d0b | 2009-03-27 12:46:47 +0100 | [diff] [blame] | 720 | |
| 721 | ide_execute_command(drive, cmd, ide_transfer_pc, timeout); |
| 722 | |
| 723 | return drq_int ? ide_started : ide_transfer_pc(drive); |
Bartlomiej Zolnierkiewicz | 6bf1641 | 2008-07-15 21:22:00 +0200 | [diff] [blame] | 724 | } |
| 725 | EXPORT_SYMBOL_GPL(ide_issue_pc); |