blob: 33a15343e8bbbcc5b2d2b8441163b25ac52b244d [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
Borislav Petkov991cb262009-01-02 16:12:52 +010017static 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
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020023/*
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
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +020070/* 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
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200120void 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
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200128/*
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 */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200133static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
134 struct ide_atapi_pc *pc, struct request *rq)
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200135{
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}
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200146
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200147/*
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
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200170int 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
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200181int 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
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200196int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
197{
198 struct ide_atapi_pc pc;
199
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200200 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200201 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
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200211void 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
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200242int 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
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200255/*
256 * This is the usual interrupt handler which will be called during a packet
257 * command. We will transfer some of the data (as requested by the drive)
258 * and will re-point interrupt handler to us.
259 */
260static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200261{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200262 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200263 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200264 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200265 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200266 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200267 ide_expiry_t *expiry;
268 unsigned int timeout, temp;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200269 u16 bcount;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200270 u8 stat, ireason, scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI), dsc = 0;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200271
272 debug_log("Enter %s - interrupt handler\n", __func__);
273
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200274 if (scsi) {
275 timeout = ide_scsi_get_timeout(pc);
276 expiry = ide_scsi_expiry;
277 } else {
278 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
279 : WAIT_TAPE_CMD;
280 expiry = NULL;
281 }
282
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200283 if (pc->flags & PC_FLAG_TIMEDOUT) {
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200284 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200285 return ide_stopped;
286 }
287
288 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200289 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200290
291 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
292 if (hwif->dma_ops->dma_end(drive) ||
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200293 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200294 if (drive->media == ide_floppy && !scsi)
295 printk(KERN_ERR "%s: DMA %s error\n",
296 drive->name, rq_data_dir(pc->rq)
297 ? "write" : "read");
298 pc->flags |= PC_FLAG_DMA_ERROR;
299 } else {
300 pc->xferred = pc->req_xfer;
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200301 if (drive->pc_update_buffers)
302 drive->pc_update_buffers(drive, pc);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200303 }
304 debug_log("%s: DMA finished\n", drive->name);
305 }
306
307 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200308 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200309 debug_log("Packet command completed, %d bytes transferred\n",
310 pc->xferred);
311
312 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
313
314 local_irq_enable_in_hardirq();
315
316 if (drive->media == ide_tape && !scsi &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200317 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
318 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200319
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200320 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200321 /* Error detected */
322 debug_log("%s: I/O error\n", drive->name);
323
324 if (drive->media != ide_tape || scsi) {
325 pc->rq->errors++;
326 if (scsi)
327 goto cmd_finished;
328 }
329
Borislav Petkov8fccf892008-07-23 19:56:01 +0200330 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200331 printk(KERN_ERR "%s: I/O error in request sense"
332 " command\n", drive->name);
333 return ide_do_reset(drive);
334 }
335
Borislav Petkov8fccf892008-07-23 19:56:01 +0200336 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200337
338 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200339 ide_retry_pc(drive, rq->rq_disk);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200340
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200341 /* queued, but not started */
342 return ide_stopped;
343 }
344cmd_finished:
345 pc->error = 0;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200346
347 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
348 dsc = 1;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200349
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200350 /* Command finished - Call the callback function */
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200351 drive->pc_callback(drive, dsc);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200352
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200353 return ide_stopped;
354 }
355
356 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
357 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
358 printk(KERN_ERR "%s: The device wants to issue more interrupts "
359 "in DMA mode\n", drive->name);
360 ide_dma_off(drive);
361 return ide_do_reset(drive);
362 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200363
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200364 /* Get the number of bytes to transfer on this interrupt. */
365 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200366
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200367 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200368 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
369 return ide_do_reset(drive);
370 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200371
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200372 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
373 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200374 /* Hopefully, we will never get here */
375 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
376 "to %s!\n", drive->name,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200377 (ireason & ATAPI_IO) ? "Write" : "Read",
378 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200379 return ide_do_reset(drive);
380 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200381
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200382 if (!(pc->flags & PC_FLAG_WRITING)) {
383 /* Reading - Check that we have enough space */
384 temp = pc->xferred + bcount;
385 if (temp > pc->req_xfer) {
386 if (temp > pc->buf_size) {
387 printk(KERN_ERR "%s: The device wants to send "
388 "us more data than expected - "
389 "discarding data\n",
390 drive->name);
391 if (scsi)
392 temp = pc->buf_size - pc->xferred;
393 else
394 temp = 0;
395 if (temp) {
396 if (pc->sg)
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200397 drive->pc_io_buffers(drive, pc,
398 temp, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200399 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200400 tp_ops->input_data(drive, NULL,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200401 pc->cur_pos, temp);
402 printk(KERN_ERR "%s: transferred %d of "
403 "%d bytes\n",
404 drive->name,
405 temp, bcount);
406 }
407 pc->xferred += temp;
408 pc->cur_pos += temp;
409 ide_pad_transfer(drive, 0, bcount - temp);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200410 goto next_irq;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200411 }
412 debug_log("The device wants to send us more data than "
413 "expected - allowing transfer\n");
414 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200415 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200416 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200417 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200418
419 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
420 (drive->media == ide_tape && !scsi && pc->bh) ||
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200421 (scsi && pc->sg)) {
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200422 int done = drive->pc_io_buffers(drive, pc, bcount,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200423 !!(pc->flags & PC_FLAG_WRITING));
424
425 /* FIXME: don't do partial completions */
426 if (drive->media == ide_floppy && !scsi)
427 ide_end_request(drive, 1, done >> 9);
428 } else
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200429 xferfunc(drive, NULL, pc->cur_pos, bcount);
430
431 /* Update the current position */
432 pc->xferred += bcount;
433 pc->cur_pos += bcount;
434
435 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200436 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200437next_irq:
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200438 /* And set the interrupt handler again */
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200439 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200440 return ide_started;
441}
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200442
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200443static u8 ide_read_ireason(ide_drive_t *drive)
444{
445 ide_task_t task;
446
447 memset(&task, 0, sizeof(task));
448 task.tf_flags = IDE_TFLAG_IN_NSECT;
449
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200450 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200451
452 return task.tf.nsect & 3;
453}
454
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200455static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
456{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200457 int retries = 100;
458
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200459 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
460 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200461 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
462 "a packet command, retrying\n", drive->name);
463 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200464 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200465 if (retries == 0) {
466 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
467 "a packet command, ignoring\n",
468 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200469 ireason |= ATAPI_COD;
470 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200471 }
472 }
473
474 return ireason;
475}
476
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200477static int ide_delayed_transfer_pc(ide_drive_t *drive)
478{
479 /* Send the actual packet */
480 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
481
482 /* Timeout for the packet command */
483 return WAIT_FLOPPY_CMD;
484}
485
486static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200487{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200488 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200489 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200490 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200491 ide_expiry_t *expiry;
492 unsigned int timeout;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200493 ide_startstop_t startstop;
494 u8 ireason;
495
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200496 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200497 printk(KERN_ERR "%s: Strange, packet command initiated yet "
498 "DRQ isn't asserted\n", drive->name);
499 return startstop;
500 }
501
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200502 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200503 if (drive->media == ide_tape &&
504 (drive->dev_flags & IDE_DFLAG_SCSI) == 0)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200505 ireason = ide_wait_ireason(drive, ireason);
506
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200507 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200508 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
509 "a packet command\n", drive->name);
510 return ide_do_reset(drive);
511 }
512
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200513 /*
514 * If necessary schedule the packet transfer to occur 'timeout'
515 * miliseconds later in ide_delayed_transfer_pc() after the device
516 * says it's ready for a packet.
517 */
518 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
519 timeout = drive->pc_delay;
520 expiry = &ide_delayed_transfer_pc;
521 } else {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200522 if (drive->dev_flags & IDE_DFLAG_SCSI) {
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200523 timeout = ide_scsi_get_timeout(pc);
524 expiry = ide_scsi_expiry;
525 } else {
526 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
527 : WAIT_TAPE_CMD;
528 expiry = NULL;
529 }
530 }
531
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200532 /* Set the interrupt routine */
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200533 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200534
535 /* Begin DMA, if necessary */
536 if (pc->flags & PC_FLAG_DMA_OK) {
537 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
538 hwif->dma_ops->dma_start(drive);
539 }
540
541 /* Send the actual packet */
Borislav Petkovea68d272008-07-23 19:56:01 +0200542 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200543 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200544
545 return ide_started;
546}
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200547
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200548ide_startstop_t ide_issue_pc(ide_drive_t *drive, unsigned int timeout,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200549 ide_expiry_t *expiry)
550{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200551 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200552 ide_hwif_t *hwif = drive->hwif;
Borislav Petkovf9476b92008-10-13 21:39:50 +0200553 u32 tf_flags;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200554 u16 bcount;
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200555 u8 scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200556
557 /* We haven't transferred any data yet */
558 pc->xferred = 0;
559 pc->cur_pos = pc->buf;
560
561 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200562 if (drive->media == ide_tape && scsi == 0)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200563 bcount = pc->req_xfer;
564 else
565 bcount = min(pc->req_xfer, 63 * 1024);
566
567 if (pc->flags & PC_FLAG_DMA_ERROR) {
568 pc->flags &= ~PC_FLAG_DMA_ERROR;
569 ide_dma_off(drive);
570 }
571
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200572 if ((pc->flags & PC_FLAG_DMA_OK) &&
573 (drive->dev_flags & IDE_DFLAG_USING_DMA)) {
574 if (scsi)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200575 hwif->sg_mapped = 1;
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200576 drive->dma = !hwif->dma_ops->dma_setup(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200577 if (scsi)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200578 hwif->sg_mapped = 0;
579 }
580
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200581 if (!drive->dma)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200582 pc->flags &= ~PC_FLAG_DMA_OK;
583
Borislav Petkovf9476b92008-10-13 21:39:50 +0200584 if (scsi)
585 tf_flags = 0;
Borislav Petkov991cb262009-01-02 16:12:52 +0100586 else if (dev_is_idecd(drive))
Borislav Petkovf9476b92008-10-13 21:39:50 +0200587 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
588 else
589 tf_flags = IDE_TFLAG_OUT_DEVICE;
590
591 ide_pktcmd_tf_load(drive, tf_flags, bcount, drive->dma);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200592
593 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200594 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200595 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200596 timeout, NULL);
597 return ide_started;
598 } else {
599 ide_execute_pkt_cmd(drive);
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200600 return ide_transfer_pc(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200601 }
602}
603EXPORT_SYMBOL_GPL(ide_issue_pc);