blob: 7a04509bf962a00c1699f70201549bf0d047e84e [file] [log] [blame]
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02001/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
Borislav Petkov4cad0852009-01-02 16:12:53 +01006#include <linux/cdrom.h>
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02007#include <linux/delay.h>
8#include <linux/ide.h>
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +02009#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
Borislav Petkov991cb262009-01-02 16:12:52 +010018static 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
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020024/*
25 * Check whether we can support a device,
26 * based on the ATAPI IDENTIFY command results.
27 */
28int 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}
69EXPORT_SYMBOL_GPL(ide_check_atapi_device);
70
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +020071/* PIO data transfer routine using the scatter gather table. */
72int 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}
119EXPORT_SYMBOL_GPL(ide_io_buffers);
120
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200121void 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}
127EXPORT_SYMBOL_GPL(ide_init_pc);
128
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200129/*
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 */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200134static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
135 struct ide_atapi_pc *pc, struct request *rq)
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200136{
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}
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200147
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200148/*
149 * Add a special packet command request to the tail of the request queue,
150 * and wait for it to be serviced.
151 */
152int 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}
169EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
170
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200171int 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}
180EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
181
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200182int 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}
195EXPORT_SYMBOL_GPL(ide_do_start_stop);
196
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200197int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
198{
199 struct ide_atapi_pc pc;
200
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200201 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200202 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}
210EXPORT_SYMBOL_GPL(ide_set_media_lock);
211
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200212void 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}
224EXPORT_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 */
230void 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}
241EXPORT_SYMBOL_GPL(ide_retry_pc);
242
Borislav Petkov4cad0852009-01-02 16:12:53 +0100243int 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}
273EXPORT_SYMBOL_GPL(ide_cd_expiry);
274
Borislav Petkov392de1d2009-01-02 16:12:52 +0100275int 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}
285EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
286
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200287/*
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 */
292static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200293{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200294 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200295 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200296 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200297 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200298 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200299 unsigned int timeout, temp;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200300 u16 bcount;
Borislav Petkov5d655a02009-01-02 16:12:54 +0100301 u8 stat, ireason, dsc = 0;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200302
303 debug_log("Enter %s - interrupt handler\n", __func__);
304
Borislav Petkov5d655a02009-01-02 16:12:54 +0100305 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
306 : WAIT_TAPE_CMD;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200307
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200308 if (pc->flags & PC_FLAG_TIMEDOUT) {
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200309 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200310 return ide_stopped;
311 }
312
313 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200314 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200315
316 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
317 if (hwif->dma_ops->dma_end(drive) ||
Borislav Petkov5d655a02009-01-02 16:12:54 +0100318 (drive->media == ide_tape && (stat & ATA_ERR))) {
319 if (drive->media == ide_floppy)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200320 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;
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200326 if (drive->pc_update_buffers)
327 drive->pc_update_buffers(drive, pc);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200328 }
329 debug_log("%s: DMA finished\n", drive->name);
330 }
331
332 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200333 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200334 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
Borislav Petkov5d655a02009-01-02 16:12:54 +0100341 if (drive->media == ide_tape &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200342 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
343 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200344
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200345 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200346 /* Error detected */
347 debug_log("%s: I/O error\n", drive->name);
348
Borislav Petkov5d655a02009-01-02 16:12:54 +0100349 if (drive->media != ide_tape)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200350 pc->rq->errors++;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200351
Borislav Petkov8fccf892008-07-23 19:56:01 +0200352 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200353 printk(KERN_ERR "%s: I/O error in request sense"
354 " command\n", drive->name);
355 return ide_do_reset(drive);
356 }
357
Borislav Petkov8fccf892008-07-23 19:56:01 +0200358 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200359
360 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200361 ide_retry_pc(drive, rq->rq_disk);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200362
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200363 /* queued, but not started */
364 return ide_stopped;
365 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200366 pc->error = 0;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200367
368 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
369 dsc = 1;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200370
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200371 /* Command finished - Call the callback function */
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200372 drive->pc_callback(drive, dsc);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200373
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200374 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 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200384
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200385 /* Get the number of bytes to transfer on this interrupt. */
386 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200387
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200388 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200389 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
390 return ide_do_reset(drive);
391 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200392
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200393 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
394 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200395 /* 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,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200398 (ireason & ATAPI_IO) ? "Write" : "Read",
399 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200400 return ide_do_reset(drive);
401 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200402
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200403 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);
Borislav Petkov5d655a02009-01-02 16:12:54 +0100412
413 ide_pad_transfer(drive, 0, bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200414 goto next_irq;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200415 }
416 debug_log("The device wants to send us more data than "
417 "expected - allowing transfer\n");
418 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200419 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200420 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200421 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200422
Borislav Petkov5d655a02009-01-02 16:12:54 +0100423 if ((drive->media == ide_floppy && !pc->buf) ||
424 (drive->media == ide_tape && pc->bh)) {
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200425 int done = drive->pc_io_buffers(drive, pc, bcount,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200426 !!(pc->flags & PC_FLAG_WRITING));
427
428 /* FIXME: don't do partial completions */
Borislav Petkov5d655a02009-01-02 16:12:54 +0100429 if (drive->media == ide_floppy)
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200430 ide_end_request(drive, 1, done >> 9);
431 } else
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200432 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",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200439 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200440next_irq:
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200441 /* And set the interrupt handler again */
Borislav Petkov5d655a02009-01-02 16:12:54 +0100442 ide_set_handler(drive, ide_pc_intr, timeout, NULL);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200443 return ide_started;
444}
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200445
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200446static 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
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200453 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200454
455 return task.tf.nsect & 3;
456}
457
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200458static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
459{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200460 int retries = 100;
461
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200462 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
463 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200464 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
465 "a packet command, retrying\n", drive->name);
466 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200467 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200468 if (retries == 0) {
469 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
470 "a packet command, ignoring\n",
471 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200472 ireason |= ATAPI_COD;
473 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200474 }
475 }
476
477 return ireason;
478}
479
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200480static 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
489static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200490{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200491 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200492 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200493 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200494 ide_expiry_t *expiry;
495 unsigned int timeout;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200496 ide_startstop_t startstop;
497 u8 ireason;
498
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200499 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200500 printk(KERN_ERR "%s: Strange, packet command initiated yet "
501 "DRQ isn't asserted\n", drive->name);
502 return startstop;
503 }
504
Borislav Petkov5f258432009-01-02 16:12:53 +0100505 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
506 if (drive->dma)
507 drive->waiting_for_dma = 1;
508 }
509
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200510 ireason = ide_read_ireason(drive);
Borislav Petkov5fe31102009-01-02 16:12:53 +0100511 if (drive->media == ide_tape)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200512 ireason = ide_wait_ireason(drive, ireason);
513
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200514 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200515 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
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200520 /*
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 {
Borislav Petkov5fe31102009-01-02 16:12:53 +0100529 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
530 : WAIT_TAPE_CMD;
531 expiry = NULL;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200532 }
533
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200534 /* Set the interrupt routine */
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200535 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200536
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 */
Borislav Petkovea68d272008-07-23 19:56:01 +0200544 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200545 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200546
547 return ide_started;
548}
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200549
Borislav Petkov4cad0852009-01-02 16:12:53 +0100550ide_startstop_t ide_issue_pc(ide_drive_t *drive, unsigned int timeout)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200551{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200552 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200553 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100554 ide_expiry_t *expiry = NULL;
Borislav Petkovf9476b92008-10-13 21:39:50 +0200555 u32 tf_flags;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100556 u16 bcount;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200557
558 /* We haven't transferred any data yet */
559 pc->xferred = 0;
560 pc->cur_pos = pc->buf;
561
Borislav Petkoved485542009-01-02 16:12:52 +0100562 if (dev_is_idecd(drive)) {
563 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100564 bcount = ide_cd_get_xferlen(hwif->hwgroup->rq);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100565 expiry = ide_cd_expiry;
Borislav Petkoved485542009-01-02 16:12:52 +0100566 } 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 }
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200572
573 if (pc->flags & PC_FLAG_DMA_ERROR) {
574 pc->flags &= ~PC_FLAG_DMA_ERROR;
575 ide_dma_off(drive);
576 }
577
Borislav Petkov4f02ff02009-01-02 16:12:52 +0100578 if (((pc->flags & PC_FLAG_DMA_OK) &&
579 (drive->dev_flags & IDE_DFLAG_USING_DMA)) ||
Borislav Petkov152fe1c2009-01-02 16:12:53 +0100580 drive->dma)
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200581 drive->dma = !hwif->dma_ops->dma_setup(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200582
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200583 if (!drive->dma)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200584 pc->flags &= ~PC_FLAG_DMA_OK;
585
Borislav Petkovf9476b92008-10-13 21:39:50 +0200586 ide_pktcmd_tf_load(drive, tf_flags, bcount, drive->dma);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200587
588 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200589 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov5f258432009-01-02 16:12:53 +0100590 if (drive->dma)
591 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200592 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
Borislav Petkov4cad0852009-01-02 16:12:53 +0100593 timeout, expiry);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200594 return ide_started;
595 } else {
596 ide_execute_pkt_cmd(drive);
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200597 return ide_transfer_pc(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200598 }
599}
600EXPORT_SYMBOL_GPL(ide_issue_pc);