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