blob: 58411591edf390a9da31a859e15be37e9c37df56 [file] [log] [blame]
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02001/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/ide.h>
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +02008#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
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020017/*
18 * Check whether we can support a device,
19 * based on the ATAPI IDENTIFY command results.
20 */
21int ide_check_atapi_device(ide_drive_t *drive, const char *s)
22{
23 u16 *id = drive->id;
24 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
25
26 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
27
28 protocol = (gcw[1] & 0xC0) >> 6;
29 device_type = gcw[1] & 0x1F;
30 removable = (gcw[0] & 0x80) >> 7;
31 drq_type = (gcw[0] & 0x60) >> 5;
32 packet_size = gcw[0] & 0x03;
33
34#ifdef CONFIG_PPC
35 /* kludge for Apple PowerBook internal zip */
36 if (drive->media == ide_floppy && device_type == 5 &&
37 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
38 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
39 device_type = 0;
40#endif
41
42 if (protocol != 2)
43 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
44 s, drive->name, protocol);
45 else if ((drive->media == ide_floppy && device_type != 0) ||
46 (drive->media == ide_tape && device_type != 1))
47 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
48 s, drive->name, device_type);
49 else if (removable == 0)
50 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
51 s, drive->name);
52 else if (drive->media == ide_floppy && drq_type == 3)
53 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
54 "supported\n", s, drive->name, drq_type);
55 else if (packet_size != 0)
56 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
57 "bytes\n", s, drive->name, packet_size);
58 else
59 return 1;
60 return 0;
61}
62EXPORT_SYMBOL_GPL(ide_check_atapi_device);
63
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +020064/* PIO data transfer routine using the scatter gather table. */
65int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
66 unsigned int bcount, int write)
67{
68 ide_hwif_t *hwif = drive->hwif;
69 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
70 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
71 struct scatterlist *sg = pc->sg;
72 char *buf;
73 int count, done = 0;
74
75 while (bcount) {
76 count = min(sg->length - pc->b_count, bcount);
77
78 if (PageHighMem(sg_page(sg))) {
79 unsigned long flags;
80
81 local_irq_save(flags);
82 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
83 xf(drive, NULL, buf + pc->b_count, count);
84 kunmap_atomic(buf - sg->offset, KM_IRQ0);
85 local_irq_restore(flags);
86 } else {
87 buf = sg_virt(sg);
88 xf(drive, NULL, buf + pc->b_count, count);
89 }
90
91 bcount -= count;
92 pc->b_count += count;
93 done += count;
94
95 if (pc->b_count == sg->length) {
96 if (!--pc->sg_cnt)
97 break;
98 pc->sg = sg = sg_next(sg);
99 pc->b_count = 0;
100 }
101 }
102
103 if (bcount) {
104 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
105 bcount, write ? "padding with zeros"
106 : "discarding data");
107 ide_pad_transfer(drive, write, bcount);
108 }
109
110 return done;
111}
112EXPORT_SYMBOL_GPL(ide_io_buffers);
113
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200114void ide_init_pc(struct ide_atapi_pc *pc)
115{
116 memset(pc, 0, sizeof(*pc));
117 pc->buf = pc->pc_buf;
118 pc->buf_size = IDE_PC_BUFFER_SIZE;
119}
120EXPORT_SYMBOL_GPL(ide_init_pc);
121
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200122/*
123 * Generate a new packet command request in front of the request queue, before
124 * the current request, so that it will be processed immediately, on the next
125 * pass through the driver.
126 */
127void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
128 struct ide_atapi_pc *pc, struct request *rq)
129{
130 blk_rq_init(NULL, rq);
131 rq->cmd_type = REQ_TYPE_SPECIAL;
132 rq->cmd_flags |= REQ_PREEMPT;
133 rq->buffer = (char *)pc;
134 rq->rq_disk = disk;
135 memcpy(rq->cmd, pc->c, 12);
136 if (drive->media == ide_tape)
137 rq->cmd[13] = REQ_IDETAPE_PC1;
138 ide_do_drive_cmd(drive, rq);
139}
140EXPORT_SYMBOL_GPL(ide_queue_pc_head);
141
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200142/*
143 * Add a special packet command request to the tail of the request queue,
144 * and wait for it to be serviced.
145 */
146int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
147 struct ide_atapi_pc *pc)
148{
149 struct request *rq;
150 int error;
151
152 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
153 rq->cmd_type = REQ_TYPE_SPECIAL;
154 rq->buffer = (char *)pc;
155 memcpy(rq->cmd, pc->c, 12);
156 if (drive->media == ide_tape)
157 rq->cmd[13] = REQ_IDETAPE_PC1;
158 error = blk_execute_rq(drive->queue, disk, rq, 0);
159 blk_put_request(rq);
160
161 return error;
162}
163EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
164
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200165int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
166{
167 struct ide_atapi_pc pc;
168
169 ide_init_pc(&pc);
170 pc.c[0] = START_STOP;
171 pc.c[4] = start;
172
173 if (drive->media == ide_tape)
174 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
175
176 return ide_queue_pc_tail(drive, disk, &pc);
177}
178EXPORT_SYMBOL_GPL(ide_do_start_stop);
179
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200180int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
181{
182 struct ide_atapi_pc pc;
183
184 if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK)
185 return 0;
186
187 ide_init_pc(&pc);
188 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
189 pc.c[4] = on;
190
191 return ide_queue_pc_tail(drive, disk, &pc);
192}
193EXPORT_SYMBOL_GPL(ide_set_media_lock);
194
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200195/* TODO: unify the code thus making some arguments go away */
196ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
197 ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
198 void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
199 void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200200 int (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200201{
202 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200203 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200204 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200205 xfer_func_t *xferfunc;
206 unsigned int temp;
207 u16 bcount;
208 u8 stat, ireason, scsi = drive->scsi;
209
210 debug_log("Enter %s - interrupt handler\n", __func__);
211
212 if (pc->flags & PC_FLAG_TIMEDOUT) {
Borislav Petkovdb9d2862008-07-23 19:55:59 +0200213 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200214 return ide_stopped;
215 }
216
217 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200218 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200219
220 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
221 if (hwif->dma_ops->dma_end(drive) ||
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200222 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200223 if (drive->media == ide_floppy && !scsi)
224 printk(KERN_ERR "%s: DMA %s error\n",
225 drive->name, rq_data_dir(pc->rq)
226 ? "write" : "read");
227 pc->flags |= PC_FLAG_DMA_ERROR;
228 } else {
229 pc->xferred = pc->req_xfer;
230 if (update_buffers)
231 update_buffers(drive, pc);
232 }
233 debug_log("%s: DMA finished\n", drive->name);
234 }
235
236 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200237 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200238 debug_log("Packet command completed, %d bytes transferred\n",
239 pc->xferred);
240
241 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
242
243 local_irq_enable_in_hardirq();
244
245 if (drive->media == ide_tape && !scsi &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200246 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
247 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200248
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200249 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200250 /* Error detected */
251 debug_log("%s: I/O error\n", drive->name);
252
253 if (drive->media != ide_tape || scsi) {
254 pc->rq->errors++;
255 if (scsi)
256 goto cmd_finished;
257 }
258
Borislav Petkov8fccf892008-07-23 19:56:01 +0200259 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200260 printk(KERN_ERR "%s: I/O error in request sense"
261 " command\n", drive->name);
262 return ide_do_reset(drive);
263 }
264
Borislav Petkov8fccf892008-07-23 19:56:01 +0200265 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200266
267 /* Retry operation */
268 retry_pc(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200269
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200270 /* queued, but not started */
271 return ide_stopped;
272 }
273cmd_finished:
274 pc->error = 0;
275 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200276 (stat & ATA_DSC) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200277 dsc_handle(drive);
278 return ide_stopped;
279 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200280
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200281 /* Command finished - Call the callback function */
Borislav Petkovdb9d2862008-07-23 19:55:59 +0200282 drive->pc_callback(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200283
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200284 return ide_stopped;
285 }
286
287 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
288 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
289 printk(KERN_ERR "%s: The device wants to issue more interrupts "
290 "in DMA mode\n", drive->name);
291 ide_dma_off(drive);
292 return ide_do_reset(drive);
293 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200294
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200295 /* Get the number of bytes to transfer on this interrupt. */
296 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200297
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200298 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200299 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
300 return ide_do_reset(drive);
301 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200302
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200303 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
304 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200305 /* Hopefully, we will never get here */
306 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
307 "to %s!\n", drive->name,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200308 (ireason & ATAPI_IO) ? "Write" : "Read",
309 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200310 return ide_do_reset(drive);
311 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200312
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200313 if (!(pc->flags & PC_FLAG_WRITING)) {
314 /* Reading - Check that we have enough space */
315 temp = pc->xferred + bcount;
316 if (temp > pc->req_xfer) {
317 if (temp > pc->buf_size) {
318 printk(KERN_ERR "%s: The device wants to send "
319 "us more data than expected - "
320 "discarding data\n",
321 drive->name);
322 if (scsi)
323 temp = pc->buf_size - pc->xferred;
324 else
325 temp = 0;
326 if (temp) {
327 if (pc->sg)
328 io_buffers(drive, pc, temp, 0);
329 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200330 tp_ops->input_data(drive, NULL,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200331 pc->cur_pos, temp);
332 printk(KERN_ERR "%s: transferred %d of "
333 "%d bytes\n",
334 drive->name,
335 temp, bcount);
336 }
337 pc->xferred += temp;
338 pc->cur_pos += temp;
339 ide_pad_transfer(drive, 0, bcount - temp);
340 ide_set_handler(drive, handler, timeout,
341 expiry);
342 return ide_started;
343 }
344 debug_log("The device wants to send us more data than "
345 "expected - allowing transfer\n");
346 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200347 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200348 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200349 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200350
351 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
352 (drive->media == ide_tape && !scsi && pc->bh) ||
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200353 (scsi && pc->sg)) {
354 int done = io_buffers(drive, pc, bcount,
355 !!(pc->flags & PC_FLAG_WRITING));
356
357 /* FIXME: don't do partial completions */
358 if (drive->media == ide_floppy && !scsi)
359 ide_end_request(drive, 1, done >> 9);
360 } else
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200361 xferfunc(drive, NULL, pc->cur_pos, bcount);
362
363 /* Update the current position */
364 pc->xferred += bcount;
365 pc->cur_pos += bcount;
366
367 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200368 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200369
370 /* And set the interrupt handler again */
371 ide_set_handler(drive, handler, timeout, expiry);
372 return ide_started;
373}
374EXPORT_SYMBOL_GPL(ide_pc_intr);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200375
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200376static u8 ide_read_ireason(ide_drive_t *drive)
377{
378 ide_task_t task;
379
380 memset(&task, 0, sizeof(task));
381 task.tf_flags = IDE_TFLAG_IN_NSECT;
382
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200383 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200384
385 return task.tf.nsect & 3;
386}
387
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200388static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
389{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200390 int retries = 100;
391
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200392 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
393 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200394 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
395 "a packet command, retrying\n", drive->name);
396 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200397 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200398 if (retries == 0) {
399 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
400 "a packet command, ignoring\n",
401 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200402 ireason |= ATAPI_COD;
403 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200404 }
405 }
406
407 return ireason;
408}
409
410ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
411 ide_handler_t *handler, unsigned int timeout,
412 ide_expiry_t *expiry)
413{
414 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200415 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200416 ide_startstop_t startstop;
417 u8 ireason;
418
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200419 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200420 printk(KERN_ERR "%s: Strange, packet command initiated yet "
421 "DRQ isn't asserted\n", drive->name);
422 return startstop;
423 }
424
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200425 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200426 if (drive->media == ide_tape && !drive->scsi)
427 ireason = ide_wait_ireason(drive, ireason);
428
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200429 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200430 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
431 "a packet command\n", drive->name);
432 return ide_do_reset(drive);
433 }
434
435 /* Set the interrupt routine */
436 ide_set_handler(drive, handler, timeout, expiry);
437
438 /* Begin DMA, if necessary */
439 if (pc->flags & PC_FLAG_DMA_OK) {
440 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
441 hwif->dma_ops->dma_start(drive);
442 }
443
444 /* Send the actual packet */
Borislav Petkovea68d272008-07-23 19:56:01 +0200445 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200446 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200447
448 return ide_started;
449}
450EXPORT_SYMBOL_GPL(ide_transfer_pc);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200451
452ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
453 ide_handler_t *handler, unsigned int timeout,
454 ide_expiry_t *expiry)
455{
456 ide_hwif_t *hwif = drive->hwif;
457 u16 bcount;
458 u8 dma = 0;
459
460 /* We haven't transferred any data yet */
461 pc->xferred = 0;
462 pc->cur_pos = pc->buf;
463
464 /* Request to transfer the entire buffer at once */
465 if (drive->media == ide_tape && !drive->scsi)
466 bcount = pc->req_xfer;
467 else
468 bcount = min(pc->req_xfer, 63 * 1024);
469
470 if (pc->flags & PC_FLAG_DMA_ERROR) {
471 pc->flags &= ~PC_FLAG_DMA_ERROR;
472 ide_dma_off(drive);
473 }
474
475 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
476 if (drive->scsi)
477 hwif->sg_mapped = 1;
478 dma = !hwif->dma_ops->dma_setup(drive);
479 if (drive->scsi)
480 hwif->sg_mapped = 0;
481 }
482
483 if (!dma)
484 pc->flags &= ~PC_FLAG_DMA_OK;
485
486 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
487 bcount, dma);
488
489 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200490 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200491 ide_execute_command(drive, ATA_CMD_PACKET, handler,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200492 timeout, NULL);
493 return ide_started;
494 } else {
495 ide_execute_pkt_cmd(drive);
496 return (*handler)(drive);
497 }
498}
499EXPORT_SYMBOL_GPL(ide_issue_pc);