blob: 61c52fb665ca7922fab57f1459f8f18f789db811 [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 Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020064/* TODO: unify the code thus making some arguments go away */
65ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
66 ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
67 void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
68 void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
69 void (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
70{
71 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +020072 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020073 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020074 xfer_func_t *xferfunc;
75 unsigned int temp;
76 u16 bcount;
77 u8 stat, ireason, scsi = drive->scsi;
78
79 debug_log("Enter %s - interrupt handler\n", __func__);
80
81 if (pc->flags & PC_FLAG_TIMEDOUT) {
Borislav Petkovdb9d2862008-07-23 19:55:59 +020082 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020083 return ide_stopped;
84 }
85
86 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020087 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020088
89 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
90 if (hwif->dma_ops->dma_end(drive) ||
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020091 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020092 if (drive->media == ide_floppy && !scsi)
93 printk(KERN_ERR "%s: DMA %s error\n",
94 drive->name, rq_data_dir(pc->rq)
95 ? "write" : "read");
96 pc->flags |= PC_FLAG_DMA_ERROR;
97 } else {
98 pc->xferred = pc->req_xfer;
99 if (update_buffers)
100 update_buffers(drive, pc);
101 }
102 debug_log("%s: DMA finished\n", drive->name);
103 }
104
105 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200106 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200107 debug_log("Packet command completed, %d bytes transferred\n",
108 pc->xferred);
109
110 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
111
112 local_irq_enable_in_hardirq();
113
114 if (drive->media == ide_tape && !scsi &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200115 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
116 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200117
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200118 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200119 /* Error detected */
120 debug_log("%s: I/O error\n", drive->name);
121
122 if (drive->media != ide_tape || scsi) {
123 pc->rq->errors++;
124 if (scsi)
125 goto cmd_finished;
126 }
127
Borislav Petkov8fccf892008-07-23 19:56:01 +0200128 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200129 printk(KERN_ERR "%s: I/O error in request sense"
130 " command\n", drive->name);
131 return ide_do_reset(drive);
132 }
133
Borislav Petkov8fccf892008-07-23 19:56:01 +0200134 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200135
136 /* Retry operation */
137 retry_pc(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200138
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200139 /* queued, but not started */
140 return ide_stopped;
141 }
142cmd_finished:
143 pc->error = 0;
144 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200145 (stat & ATA_DSC) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200146 dsc_handle(drive);
147 return ide_stopped;
148 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200149
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200150 /* Command finished - Call the callback function */
Borislav Petkovdb9d2862008-07-23 19:55:59 +0200151 drive->pc_callback(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200152
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200153 return ide_stopped;
154 }
155
156 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
157 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
158 printk(KERN_ERR "%s: The device wants to issue more interrupts "
159 "in DMA mode\n", drive->name);
160 ide_dma_off(drive);
161 return ide_do_reset(drive);
162 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200163
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200164 /* Get the number of bytes to transfer on this interrupt. */
165 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200166
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200167 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200168 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
169 return ide_do_reset(drive);
170 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200171
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200172 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
173 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200174 /* Hopefully, we will never get here */
175 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
176 "to %s!\n", drive->name,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200177 (ireason & ATAPI_IO) ? "Write" : "Read",
178 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200179 return ide_do_reset(drive);
180 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200181
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200182 if (!(pc->flags & PC_FLAG_WRITING)) {
183 /* Reading - Check that we have enough space */
184 temp = pc->xferred + bcount;
185 if (temp > pc->req_xfer) {
186 if (temp > pc->buf_size) {
187 printk(KERN_ERR "%s: The device wants to send "
188 "us more data than expected - "
189 "discarding data\n",
190 drive->name);
191 if (scsi)
192 temp = pc->buf_size - pc->xferred;
193 else
194 temp = 0;
195 if (temp) {
196 if (pc->sg)
197 io_buffers(drive, pc, temp, 0);
198 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200199 tp_ops->input_data(drive, NULL,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200200 pc->cur_pos, temp);
201 printk(KERN_ERR "%s: transferred %d of "
202 "%d bytes\n",
203 drive->name,
204 temp, bcount);
205 }
206 pc->xferred += temp;
207 pc->cur_pos += temp;
208 ide_pad_transfer(drive, 0, bcount - temp);
209 ide_set_handler(drive, handler, timeout,
210 expiry);
211 return ide_started;
212 }
213 debug_log("The device wants to send us more data than "
214 "expected - allowing transfer\n");
215 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200216 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200217 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200218 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200219
220 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
221 (drive->media == ide_tape && !scsi && pc->bh) ||
222 (scsi && pc->sg))
223 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
224 else
225 xferfunc(drive, NULL, pc->cur_pos, bcount);
226
227 /* Update the current position */
228 pc->xferred += bcount;
229 pc->cur_pos += bcount;
230
231 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200232 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200233
234 /* And set the interrupt handler again */
235 ide_set_handler(drive, handler, timeout, expiry);
236 return ide_started;
237}
238EXPORT_SYMBOL_GPL(ide_pc_intr);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200239
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200240static u8 ide_read_ireason(ide_drive_t *drive)
241{
242 ide_task_t task;
243
244 memset(&task, 0, sizeof(task));
245 task.tf_flags = IDE_TFLAG_IN_NSECT;
246
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200247 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200248
249 return task.tf.nsect & 3;
250}
251
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200252static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
253{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200254 int retries = 100;
255
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200256 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
257 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200258 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
259 "a packet command, retrying\n", drive->name);
260 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200261 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200262 if (retries == 0) {
263 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
264 "a packet command, ignoring\n",
265 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200266 ireason |= ATAPI_COD;
267 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200268 }
269 }
270
271 return ireason;
272}
273
274ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
275 ide_handler_t *handler, unsigned int timeout,
276 ide_expiry_t *expiry)
277{
278 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200279 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200280 ide_startstop_t startstop;
281 u8 ireason;
282
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200283 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200284 printk(KERN_ERR "%s: Strange, packet command initiated yet "
285 "DRQ isn't asserted\n", drive->name);
286 return startstop;
287 }
288
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200289 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200290 if (drive->media == ide_tape && !drive->scsi)
291 ireason = ide_wait_ireason(drive, ireason);
292
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200293 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200294 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
295 "a packet command\n", drive->name);
296 return ide_do_reset(drive);
297 }
298
299 /* Set the interrupt routine */
300 ide_set_handler(drive, handler, timeout, expiry);
301
302 /* Begin DMA, if necessary */
303 if (pc->flags & PC_FLAG_DMA_OK) {
304 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
305 hwif->dma_ops->dma_start(drive);
306 }
307
308 /* Send the actual packet */
Borislav Petkovea68d272008-07-23 19:56:01 +0200309 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200310 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200311
312 return ide_started;
313}
314EXPORT_SYMBOL_GPL(ide_transfer_pc);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200315
316ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
317 ide_handler_t *handler, unsigned int timeout,
318 ide_expiry_t *expiry)
319{
320 ide_hwif_t *hwif = drive->hwif;
321 u16 bcount;
322 u8 dma = 0;
323
324 /* We haven't transferred any data yet */
325 pc->xferred = 0;
326 pc->cur_pos = pc->buf;
327
328 /* Request to transfer the entire buffer at once */
329 if (drive->media == ide_tape && !drive->scsi)
330 bcount = pc->req_xfer;
331 else
332 bcount = min(pc->req_xfer, 63 * 1024);
333
334 if (pc->flags & PC_FLAG_DMA_ERROR) {
335 pc->flags &= ~PC_FLAG_DMA_ERROR;
336 ide_dma_off(drive);
337 }
338
339 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
340 if (drive->scsi)
341 hwif->sg_mapped = 1;
342 dma = !hwif->dma_ops->dma_setup(drive);
343 if (drive->scsi)
344 hwif->sg_mapped = 0;
345 }
346
347 if (!dma)
348 pc->flags &= ~PC_FLAG_DMA_OK;
349
350 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
351 bcount, dma);
352
353 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200354 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200355 ide_execute_command(drive, ATA_CMD_PACKET, handler,
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200356 timeout, NULL);
357 return ide_started;
358 } else {
359 ide_execute_pkt_cmd(drive);
360 return (*handler)(drive);
361 }
362}
363EXPORT_SYMBOL_GPL(ide_issue_pc);