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