ide-tape: add ide_tape_set_media_lock() helper
[linux-2.6-block.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
51509eec
BZ
17/*
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
acaa0f5f
BZ
64/* 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
646c0cb6
BZ
114/* TODO: unify the code thus making some arguments go away */
115ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
116 ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
117 void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
118 void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
acaa0f5f 119 int (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
646c0cb6
BZ
120{
121 ide_hwif_t *hwif = drive->hwif;
8fccf899 122 struct request *rq = hwif->hwgroup->rq;
374e042c 123 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
646c0cb6
BZ
124 xfer_func_t *xferfunc;
125 unsigned int temp;
126 u16 bcount;
127 u8 stat, ireason, scsi = drive->scsi;
128
129 debug_log("Enter %s - interrupt handler\n", __func__);
130
131 if (pc->flags & PC_FLAG_TIMEDOUT) {
db9d2869 132 drive->pc_callback(drive);
646c0cb6
BZ
133 return ide_stopped;
134 }
135
136 /* Clear the interrupt */
374e042c 137 stat = tp_ops->read_status(hwif);
646c0cb6
BZ
138
139 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
140 if (hwif->dma_ops->dma_end(drive) ||
3a7d2484 141 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
646c0cb6
BZ
142 if (drive->media == ide_floppy && !scsi)
143 printk(KERN_ERR "%s: DMA %s error\n",
144 drive->name, rq_data_dir(pc->rq)
145 ? "write" : "read");
146 pc->flags |= PC_FLAG_DMA_ERROR;
147 } else {
148 pc->xferred = pc->req_xfer;
149 if (update_buffers)
150 update_buffers(drive, pc);
151 }
152 debug_log("%s: DMA finished\n", drive->name);
153 }
154
155 /* No more interrupts */
3a7d2484 156 if ((stat & ATA_DRQ) == 0) {
646c0cb6
BZ
157 debug_log("Packet command completed, %d bytes transferred\n",
158 pc->xferred);
159
160 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
161
162 local_irq_enable_in_hardirq();
163
164 if (drive->media == ide_tape && !scsi &&
3a7d2484
BZ
165 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
166 stat &= ~ATA_ERR;
8fccf899 167
3a7d2484 168 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
646c0cb6
BZ
169 /* Error detected */
170 debug_log("%s: I/O error\n", drive->name);
171
172 if (drive->media != ide_tape || scsi) {
173 pc->rq->errors++;
174 if (scsi)
175 goto cmd_finished;
176 }
177
8fccf899 178 if (rq->cmd[0] == REQUEST_SENSE) {
646c0cb6
BZ
179 printk(KERN_ERR "%s: I/O error in request sense"
180 " command\n", drive->name);
181 return ide_do_reset(drive);
182 }
183
8fccf899 184 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
646c0cb6
BZ
185
186 /* Retry operation */
187 retry_pc(drive);
8fccf899 188
646c0cb6
BZ
189 /* queued, but not started */
190 return ide_stopped;
191 }
192cmd_finished:
193 pc->error = 0;
194 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
3a7d2484 195 (stat & ATA_DSC) == 0) {
646c0cb6
BZ
196 dsc_handle(drive);
197 return ide_stopped;
198 }
8fccf899 199
646c0cb6 200 /* Command finished - Call the callback function */
db9d2869 201 drive->pc_callback(drive);
8fccf899 202
646c0cb6
BZ
203 return ide_stopped;
204 }
205
206 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
207 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
208 printk(KERN_ERR "%s: The device wants to issue more interrupts "
209 "in DMA mode\n", drive->name);
210 ide_dma_off(drive);
211 return ide_do_reset(drive);
212 }
646c0cb6 213
1823649b
BZ
214 /* Get the number of bytes to transfer on this interrupt. */
215 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
646c0cb6 216
3a7d2484 217 if (ireason & ATAPI_COD) {
646c0cb6
BZ
218 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
219 return ide_do_reset(drive);
220 }
8fccf899 221
3a7d2484
BZ
222 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
223 !!(pc->flags & PC_FLAG_WRITING)) {
646c0cb6
BZ
224 /* Hopefully, we will never get here */
225 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
226 "to %s!\n", drive->name,
3a7d2484
BZ
227 (ireason & ATAPI_IO) ? "Write" : "Read",
228 (ireason & ATAPI_IO) ? "Read" : "Write");
646c0cb6
BZ
229 return ide_do_reset(drive);
230 }
8fccf899 231
646c0cb6
BZ
232 if (!(pc->flags & PC_FLAG_WRITING)) {
233 /* Reading - Check that we have enough space */
234 temp = pc->xferred + bcount;
235 if (temp > pc->req_xfer) {
236 if (temp > pc->buf_size) {
237 printk(KERN_ERR "%s: The device wants to send "
238 "us more data than expected - "
239 "discarding data\n",
240 drive->name);
241 if (scsi)
242 temp = pc->buf_size - pc->xferred;
243 else
244 temp = 0;
245 if (temp) {
246 if (pc->sg)
247 io_buffers(drive, pc, temp, 0);
248 else
374e042c 249 tp_ops->input_data(drive, NULL,
646c0cb6
BZ
250 pc->cur_pos, temp);
251 printk(KERN_ERR "%s: transferred %d of "
252 "%d bytes\n",
253 drive->name,
254 temp, bcount);
255 }
256 pc->xferred += temp;
257 pc->cur_pos += temp;
258 ide_pad_transfer(drive, 0, bcount - temp);
259 ide_set_handler(drive, handler, timeout,
260 expiry);
261 return ide_started;
262 }
263 debug_log("The device wants to send us more data than "
264 "expected - allowing transfer\n");
265 }
374e042c 266 xferfunc = tp_ops->input_data;
646c0cb6 267 } else
374e042c 268 xferfunc = tp_ops->output_data;
646c0cb6
BZ
269
270 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
271 (drive->media == ide_tape && !scsi && pc->bh) ||
acaa0f5f
BZ
272 (scsi && pc->sg)) {
273 int done = io_buffers(drive, pc, bcount,
274 !!(pc->flags & PC_FLAG_WRITING));
275
276 /* FIXME: don't do partial completions */
277 if (drive->media == ide_floppy && !scsi)
278 ide_end_request(drive, 1, done >> 9);
279 } else
646c0cb6
BZ
280 xferfunc(drive, NULL, pc->cur_pos, bcount);
281
282 /* Update the current position */
283 pc->xferred += bcount;
284 pc->cur_pos += bcount;
285
286 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
8fccf899 287 rq->cmd[0], bcount);
646c0cb6
BZ
288
289 /* And set the interrupt handler again */
290 ide_set_handler(drive, handler, timeout, expiry);
291 return ide_started;
292}
293EXPORT_SYMBOL_GPL(ide_pc_intr);
594c16d8 294
88a72109
BZ
295static u8 ide_read_ireason(ide_drive_t *drive)
296{
297 ide_task_t task;
298
299 memset(&task, 0, sizeof(task));
300 task.tf_flags = IDE_TFLAG_IN_NSECT;
301
374e042c 302 drive->hwif->tp_ops->tf_read(drive, &task);
88a72109
BZ
303
304 return task.tf.nsect & 3;
305}
306
594c16d8
BZ
307static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
308{
594c16d8
BZ
309 int retries = 100;
310
3a7d2484
BZ
311 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
312 (ireason & ATAPI_IO))) {
594c16d8
BZ
313 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
314 "a packet command, retrying\n", drive->name);
315 udelay(100);
88a72109 316 ireason = ide_read_ireason(drive);
594c16d8
BZ
317 if (retries == 0) {
318 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
319 "a packet command, ignoring\n",
320 drive->name);
3a7d2484
BZ
321 ireason |= ATAPI_COD;
322 ireason &= ~ATAPI_IO;
594c16d8
BZ
323 }
324 }
325
326 return ireason;
327}
328
329ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
330 ide_handler_t *handler, unsigned int timeout,
331 ide_expiry_t *expiry)
332{
333 ide_hwif_t *hwif = drive->hwif;
8fccf899 334 struct request *rq = hwif->hwgroup->rq;
594c16d8
BZ
335 ide_startstop_t startstop;
336 u8 ireason;
337
3a7d2484 338 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
594c16d8
BZ
339 printk(KERN_ERR "%s: Strange, packet command initiated yet "
340 "DRQ isn't asserted\n", drive->name);
341 return startstop;
342 }
343
88a72109 344 ireason = ide_read_ireason(drive);
594c16d8
BZ
345 if (drive->media == ide_tape && !drive->scsi)
346 ireason = ide_wait_ireason(drive, ireason);
347
3a7d2484 348 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
594c16d8
BZ
349 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
350 "a packet command\n", drive->name);
351 return ide_do_reset(drive);
352 }
353
354 /* Set the interrupt routine */
355 ide_set_handler(drive, handler, timeout, expiry);
356
357 /* Begin DMA, if necessary */
358 if (pc->flags & PC_FLAG_DMA_OK) {
359 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
360 hwif->dma_ops->dma_start(drive);
361 }
362
363 /* Send the actual packet */
ea68d270 364 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
8fccf899 365 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
594c16d8
BZ
366
367 return ide_started;
368}
369EXPORT_SYMBOL_GPL(ide_transfer_pc);
6bf1641c
BZ
370
371ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
372 ide_handler_t *handler, unsigned int timeout,
373 ide_expiry_t *expiry)
374{
375 ide_hwif_t *hwif = drive->hwif;
376 u16 bcount;
377 u8 dma = 0;
378
379 /* We haven't transferred any data yet */
380 pc->xferred = 0;
381 pc->cur_pos = pc->buf;
382
383 /* Request to transfer the entire buffer at once */
384 if (drive->media == ide_tape && !drive->scsi)
385 bcount = pc->req_xfer;
386 else
387 bcount = min(pc->req_xfer, 63 * 1024);
388
389 if (pc->flags & PC_FLAG_DMA_ERROR) {
390 pc->flags &= ~PC_FLAG_DMA_ERROR;
391 ide_dma_off(drive);
392 }
393
394 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
395 if (drive->scsi)
396 hwif->sg_mapped = 1;
397 dma = !hwif->dma_ops->dma_setup(drive);
398 if (drive->scsi)
399 hwif->sg_mapped = 0;
400 }
401
402 if (!dma)
403 pc->flags &= ~PC_FLAG_DMA_OK;
404
405 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
406 bcount, dma);
407
408 /* Issue the packet command */
ac77ef8b 409 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
aaaade3f 410 ide_execute_command(drive, ATA_CMD_PACKET, handler,
6bf1641c
BZ
411 timeout, NULL);
412 return ide_started;
413 } else {
414 ide_execute_pkt_cmd(drive);
415 return (*handler)(drive);
416 }
417}
418EXPORT_SYMBOL_GPL(ide_issue_pc);