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