blob: 3044c51c06a559cacb8776638ae3acb69d200337 [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 Petkov8c662852009-01-02 16:12:54 +010018#define ATAPI_MIN_CDB_BYTES 12
19
Borislav Petkov991cb262009-01-02 16:12:52 +010020static inline int dev_is_idecd(ide_drive_t *drive)
21{
Borislav Petkov53174642009-01-02 16:12:54 +010022 return drive->media == ide_cdrom || drive->media == ide_optical;
Borislav Petkov991cb262009-01-02 16:12:52 +010023}
24
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020025/*
26 * Check whether we can support a device,
27 * based on the ATAPI IDENTIFY command results.
28 */
29int ide_check_atapi_device(ide_drive_t *drive, const char *s)
30{
31 u16 *id = drive->id;
32 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
33
34 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
35
36 protocol = (gcw[1] & 0xC0) >> 6;
37 device_type = gcw[1] & 0x1F;
38 removable = (gcw[0] & 0x80) >> 7;
39 drq_type = (gcw[0] & 0x60) >> 5;
40 packet_size = gcw[0] & 0x03;
41
42#ifdef CONFIG_PPC
43 /* kludge for Apple PowerBook internal zip */
44 if (drive->media == ide_floppy && device_type == 5 &&
45 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
46 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
47 device_type = 0;
48#endif
49
50 if (protocol != 2)
51 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
52 s, drive->name, protocol);
53 else if ((drive->media == ide_floppy && device_type != 0) ||
54 (drive->media == ide_tape && device_type != 1))
55 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
56 s, drive->name, device_type);
57 else if (removable == 0)
58 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
59 s, drive->name);
60 else if (drive->media == ide_floppy && drq_type == 3)
61 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
62 "supported\n", s, drive->name, drq_type);
63 else if (packet_size != 0)
64 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
65 "bytes\n", s, drive->name, packet_size);
66 else
67 return 1;
68 return 0;
69}
70EXPORT_SYMBOL_GPL(ide_check_atapi_device);
71
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +020072/* PIO data transfer routine using the scatter gather table. */
73int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
74 unsigned int bcount, int write)
75{
76 ide_hwif_t *hwif = drive->hwif;
77 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
78 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
79 struct scatterlist *sg = pc->sg;
80 char *buf;
81 int count, done = 0;
82
83 while (bcount) {
84 count = min(sg->length - pc->b_count, bcount);
85
86 if (PageHighMem(sg_page(sg))) {
87 unsigned long flags;
88
89 local_irq_save(flags);
90 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
91 xf(drive, NULL, buf + pc->b_count, count);
92 kunmap_atomic(buf - sg->offset, KM_IRQ0);
93 local_irq_restore(flags);
94 } else {
95 buf = sg_virt(sg);
96 xf(drive, NULL, buf + pc->b_count, count);
97 }
98
99 bcount -= count;
100 pc->b_count += count;
101 done += count;
102
103 if (pc->b_count == sg->length) {
104 if (!--pc->sg_cnt)
105 break;
106 pc->sg = sg = sg_next(sg);
107 pc->b_count = 0;
108 }
109 }
110
111 if (bcount) {
112 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
113 bcount, write ? "padding with zeros"
114 : "discarding data");
115 ide_pad_transfer(drive, write, bcount);
116 }
117
118 return done;
119}
120EXPORT_SYMBOL_GPL(ide_io_buffers);
121
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +0200122void ide_init_pc(struct ide_atapi_pc *pc)
123{
124 memset(pc, 0, sizeof(*pc));
125 pc->buf = pc->pc_buf;
126 pc->buf_size = IDE_PC_BUFFER_SIZE;
127}
128EXPORT_SYMBOL_GPL(ide_init_pc);
129
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200130/*
131 * Generate a new packet command request in front of the request queue, before
132 * the current request, so that it will be processed immediately, on the next
133 * pass through the driver.
134 */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200135static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
136 struct ide_atapi_pc *pc, struct request *rq)
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200137{
138 blk_rq_init(NULL, rq);
139 rq->cmd_type = REQ_TYPE_SPECIAL;
140 rq->cmd_flags |= REQ_PREEMPT;
141 rq->buffer = (char *)pc;
142 rq->rq_disk = disk;
Borislav Petkov3eb76c12009-03-13 21:16:12 +0100143
144 if (pc->req_xfer) {
145 rq->data = pc->buf;
146 rq->data_len = pc->req_xfer;
147 }
148
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200149 memcpy(rq->cmd, pc->c, 12);
150 if (drive->media == ide_tape)
151 rq->cmd[13] = REQ_IDETAPE_PC1;
Bartlomiej Zolnierkiewicz18660822009-03-24 23:22:44 +0100152
153 drive->hwif->rq = NULL;
154
155 elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200156}
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +0200157
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200158/*
159 * Add a special packet command request to the tail of the request queue,
160 * and wait for it to be serviced.
161 */
162int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
163 struct ide_atapi_pc *pc)
164{
165 struct request *rq;
166 int error;
167
168 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
169 rq->cmd_type = REQ_TYPE_SPECIAL;
170 rq->buffer = (char *)pc;
Borislav Petkov3eb76c12009-03-13 21:16:12 +0100171
172 if (pc->req_xfer) {
173 rq->data = pc->buf;
174 rq->data_len = pc->req_xfer;
175 }
176
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200177 memcpy(rq->cmd, pc->c, 12);
178 if (drive->media == ide_tape)
179 rq->cmd[13] = REQ_IDETAPE_PC1;
180 error = blk_execute_rq(drive->queue, disk, rq, 0);
181 blk_put_request(rq);
182
183 return error;
184}
185EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
186
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200187int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
188{
189 struct ide_atapi_pc pc;
190
191 ide_init_pc(&pc);
192 pc.c[0] = TEST_UNIT_READY;
193
194 return ide_queue_pc_tail(drive, disk, &pc);
195}
196EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
197
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200198int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
199{
200 struct ide_atapi_pc pc;
201
202 ide_init_pc(&pc);
203 pc.c[0] = START_STOP;
204 pc.c[4] = start;
205
206 if (drive->media == ide_tape)
207 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
208
209 return ide_queue_pc_tail(drive, disk, &pc);
210}
211EXPORT_SYMBOL_GPL(ide_do_start_stop);
212
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200213int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
214{
215 struct ide_atapi_pc pc;
216
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200217 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200218 return 0;
219
220 ide_init_pc(&pc);
221 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
222 pc.c[4] = on;
223
224 return ide_queue_pc_tail(drive, disk, &pc);
225}
226EXPORT_SYMBOL_GPL(ide_set_media_lock);
227
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200228void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
229{
230 ide_init_pc(pc);
231 pc->c[0] = REQUEST_SENSE;
232 if (drive->media == ide_floppy) {
233 pc->c[4] = 255;
234 pc->req_xfer = 18;
235 } else {
236 pc->c[4] = 20;
237 pc->req_xfer = 20;
238 }
239}
240EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
241
242/*
243 * Called when an error was detected during the last packet command.
244 * We queue a request sense packet command in the head of the request list.
245 */
246void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
247{
248 struct request *rq = &drive->request_sense_rq;
249 struct ide_atapi_pc *pc = &drive->request_sense_pc;
250
251 (void)ide_read_error(drive);
252 ide_create_request_sense_cmd(drive, pc);
253 if (drive->media == ide_tape)
254 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
255 ide_queue_pc_head(drive, disk, pc, rq);
256}
257EXPORT_SYMBOL_GPL(ide_retry_pc);
258
Borislav Petkov4cad0852009-01-02 16:12:53 +0100259int ide_cd_expiry(ide_drive_t *drive)
260{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100261 struct request *rq = drive->hwif->rq;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100262 unsigned long wait = 0;
263
264 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
265
266 /*
267 * Some commands are *slow* and normally take a long time to complete.
268 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
269 * commands/drives support that. Let ide_timer_expiry keep polling us
270 * for these.
271 */
272 switch (rq->cmd[0]) {
273 case GPCMD_BLANK:
274 case GPCMD_FORMAT_UNIT:
275 case GPCMD_RESERVE_RZONE_TRACK:
276 case GPCMD_CLOSE_TRACK:
277 case GPCMD_FLUSH_CACHE:
278 wait = ATAPI_WAIT_PC;
279 break;
280 default:
281 if (!(rq->cmd_flags & REQ_QUIET))
282 printk(KERN_INFO "cmd 0x%x timed out\n",
283 rq->cmd[0]);
284 wait = 0;
285 break;
286 }
287 return wait;
288}
289EXPORT_SYMBOL_GPL(ide_cd_expiry);
290
Borislav Petkov392de1d2009-01-02 16:12:52 +0100291int ide_cd_get_xferlen(struct request *rq)
292{
293 if (blk_fs_request(rq))
294 return 32768;
295 else if (blk_sense_request(rq) || blk_pc_request(rq) ||
296 rq->cmd_type == REQ_TYPE_ATA_PC)
297 return rq->data_len;
298 else
299 return 0;
300}
301EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
302
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200303/*
304 * This is the usual interrupt handler which will be called during a packet
305 * command. We will transfer some of the data (as requested by the drive)
306 * and will re-point interrupt handler to us.
307 */
308static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200309{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200310 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200311 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100312 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200313 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200314 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200315 unsigned int timeout, temp;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200316 u16 bcount;
Borislav Petkov5d655a02009-01-02 16:12:54 +0100317 u8 stat, ireason, dsc = 0;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200318
319 debug_log("Enter %s - interrupt handler\n", __func__);
320
Borislav Petkov5d655a02009-01-02 16:12:54 +0100321 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
322 : WAIT_TAPE_CMD;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200323
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200324 if (pc->flags & PC_FLAG_TIMEDOUT) {
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200325 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200326 return ide_stopped;
327 }
328
329 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200330 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200331
332 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
333 if (hwif->dma_ops->dma_end(drive) ||
Borislav Petkov5d655a02009-01-02 16:12:54 +0100334 (drive->media == ide_tape && (stat & ATA_ERR))) {
335 if (drive->media == ide_floppy)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200336 printk(KERN_ERR "%s: DMA %s error\n",
337 drive->name, rq_data_dir(pc->rq)
338 ? "write" : "read");
339 pc->flags |= PC_FLAG_DMA_ERROR;
340 } else {
341 pc->xferred = pc->req_xfer;
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200342 if (drive->pc_update_buffers)
343 drive->pc_update_buffers(drive, pc);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200344 }
345 debug_log("%s: DMA finished\n", drive->name);
346 }
347
348 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200349 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200350 debug_log("Packet command completed, %d bytes transferred\n",
351 pc->xferred);
352
353 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
354
355 local_irq_enable_in_hardirq();
356
Borislav Petkov5d655a02009-01-02 16:12:54 +0100357 if (drive->media == ide_tape &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200358 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
359 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200360
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200361 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200362 /* Error detected */
363 debug_log("%s: I/O error\n", drive->name);
364
Borislav Petkov5d655a02009-01-02 16:12:54 +0100365 if (drive->media != ide_tape)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200366 pc->rq->errors++;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200367
Borislav Petkov8fccf892008-07-23 19:56:01 +0200368 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200369 printk(KERN_ERR "%s: I/O error in request sense"
370 " command\n", drive->name);
371 return ide_do_reset(drive);
372 }
373
Borislav Petkov8fccf892008-07-23 19:56:01 +0200374 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200375
376 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200377 ide_retry_pc(drive, rq->rq_disk);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200378
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200379 /* queued, but not started */
380 return ide_stopped;
381 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200382 pc->error = 0;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200383
384 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
385 dsc = 1;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200386
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200387 /* Command finished - Call the callback function */
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200388 drive->pc_callback(drive, dsc);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200389
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200390 return ide_stopped;
391 }
392
393 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
394 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
395 printk(KERN_ERR "%s: The device wants to issue more interrupts "
396 "in DMA mode\n", drive->name);
397 ide_dma_off(drive);
398 return ide_do_reset(drive);
399 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200400
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200401 /* Get the number of bytes to transfer on this interrupt. */
402 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200403
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200404 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200405 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
406 return ide_do_reset(drive);
407 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200408
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200409 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
410 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200411 /* Hopefully, we will never get here */
412 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
413 "to %s!\n", drive->name,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200414 (ireason & ATAPI_IO) ? "Write" : "Read",
415 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200416 return ide_do_reset(drive);
417 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200418
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200419 if (!(pc->flags & PC_FLAG_WRITING)) {
420 /* Reading - Check that we have enough space */
421 temp = pc->xferred + bcount;
422 if (temp > pc->req_xfer) {
423 if (temp > pc->buf_size) {
424 printk(KERN_ERR "%s: The device wants to send "
425 "us more data than expected - "
426 "discarding data\n",
427 drive->name);
Borislav Petkov5d655a02009-01-02 16:12:54 +0100428
429 ide_pad_transfer(drive, 0, bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200430 goto next_irq;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200431 }
432 debug_log("The device wants to send us more data than "
433 "expected - allowing transfer\n");
434 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200435 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200436 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200437 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200438
Borislav Petkov5d655a02009-01-02 16:12:54 +0100439 if ((drive->media == ide_floppy && !pc->buf) ||
440 (drive->media == ide_tape && pc->bh)) {
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200441 int done = drive->pc_io_buffers(drive, pc, bcount,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200442 !!(pc->flags & PC_FLAG_WRITING));
443
444 /* FIXME: don't do partial completions */
Borislav Petkov5d655a02009-01-02 16:12:54 +0100445 if (drive->media == ide_floppy)
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200446 ide_end_request(drive, 1, done >> 9);
447 } else
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200448 xferfunc(drive, NULL, pc->cur_pos, bcount);
449
450 /* Update the current position */
451 pc->xferred += bcount;
452 pc->cur_pos += bcount;
453
454 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200455 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200456next_irq:
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200457 /* And set the interrupt handler again */
Borislav Petkov5d655a02009-01-02 16:12:54 +0100458 ide_set_handler(drive, ide_pc_intr, timeout, NULL);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200459 return ide_started;
460}
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200461
Bartlomiej Zolnierkiewicz7a254df2009-03-24 23:22:39 +0100462static void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount)
463{
464 ide_hwif_t *hwif = drive->hwif;
465 ide_task_t task;
466 u8 dma = drive->dma;
467
468 memset(&task, 0, sizeof(task));
469 task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
470 IDE_TFLAG_OUT_FEATURE | tf_flags;
471 task.tf.feature = dma; /* Use PIO/DMA */
472 task.tf.lbam = bcount & 0xff;
473 task.tf.lbah = (bcount >> 8) & 0xff;
474
475 ide_tf_dump(drive->name, &task.tf);
476 hwif->tp_ops->set_irq(hwif, 1);
477 SELECT_MASK(drive, 0);
478 hwif->tp_ops->tf_load(drive, &task);
479}
480
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200481static u8 ide_read_ireason(ide_drive_t *drive)
482{
483 ide_task_t task;
484
485 memset(&task, 0, sizeof(task));
486 task.tf_flags = IDE_TFLAG_IN_NSECT;
487
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200488 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200489
490 return task.tf.nsect & 3;
491}
492
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200493static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
494{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200495 int retries = 100;
496
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200497 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
498 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200499 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
500 "a packet command, retrying\n", drive->name);
501 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200502 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200503 if (retries == 0) {
504 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
505 "a packet command, ignoring\n",
506 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200507 ireason |= ATAPI_COD;
508 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200509 }
510 }
511
512 return ireason;
513}
514
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200515static int ide_delayed_transfer_pc(ide_drive_t *drive)
516{
517 /* Send the actual packet */
518 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
519
520 /* Timeout for the packet command */
521 return WAIT_FLOPPY_CMD;
522}
523
524static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200525{
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100526 struct ide_atapi_pc *uninitialized_var(pc);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200527 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100528 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200529 ide_expiry_t *expiry;
530 unsigned int timeout;
Borislav Petkov8c662852009-01-02 16:12:54 +0100531 int cmd_len;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200532 ide_startstop_t startstop;
533 u8 ireason;
534
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200535 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200536 printk(KERN_ERR "%s: Strange, packet command initiated yet "
537 "DRQ isn't asserted\n", drive->name);
538 return startstop;
539 }
540
Borislav Petkov5f258432009-01-02 16:12:53 +0100541 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
542 if (drive->dma)
543 drive->waiting_for_dma = 1;
544 }
545
Borislav Petkov8c662852009-01-02 16:12:54 +0100546 if (dev_is_idecd(drive)) {
547 /* ATAPI commands get padded out to 12 bytes minimum */
548 cmd_len = COMMAND_SIZE(rq->cmd[0]);
549 if (cmd_len < ATAPI_MIN_CDB_BYTES)
550 cmd_len = ATAPI_MIN_CDB_BYTES;
Borislav Petkovdef860d2009-01-02 16:12:55 +0100551
552 timeout = rq->timeout;
553 expiry = ide_cd_expiry;
554 } else {
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100555 pc = drive->pc;
556
Borislav Petkov8c662852009-01-02 16:12:54 +0100557 cmd_len = ATAPI_MIN_CDB_BYTES;
558
Borislav Petkovdef860d2009-01-02 16:12:55 +0100559 /*
560 * If necessary schedule the packet transfer to occur 'timeout'
561 * miliseconds later in ide_delayed_transfer_pc() after the
562 * device says it's ready for a packet.
563 */
564 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
565 timeout = drive->pc_delay;
566 expiry = &ide_delayed_transfer_pc;
567 } else {
568 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
569 : WAIT_TAPE_CMD;
570 expiry = NULL;
571 }
Borislav Petkov06cc2772009-01-02 16:12:56 +0100572
573 ireason = ide_read_ireason(drive);
574 if (drive->media == ide_tape)
575 ireason = ide_wait_ireason(drive, ireason);
576
577 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
578 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
579 "a packet command\n", drive->name);
580
581 return ide_do_reset(drive);
582 }
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200583 }
584
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200585 /* Set the interrupt routine */
Borislav Petkovd6251d42009-01-06 17:20:58 +0100586 ide_set_handler(drive,
587 (dev_is_idecd(drive) ? drive->irq_handler
588 : ide_pc_intr),
589 timeout, expiry);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200590
591 /* Begin DMA, if necessary */
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100592 if (dev_is_idecd(drive)) {
593 if (drive->dma)
594 hwif->dma_ops->dma_start(drive);
595 } else {
596 if (pc->flags & PC_FLAG_DMA_OK) {
597 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
598 hwif->dma_ops->dma_start(drive);
599 }
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200600 }
601
602 /* Send the actual packet */
Borislav Petkovea68d272008-07-23 19:56:01 +0200603 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8c662852009-01-02 16:12:54 +0100604 hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200605
606 return ide_started;
607}
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200608
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100609ide_startstop_t ide_issue_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200610{
Borislav Petkovd77612a2009-01-02 16:12:55 +0100611 struct ide_atapi_pc *pc;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200612 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100613 ide_expiry_t *expiry = NULL;
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100614 unsigned int timeout;
Borislav Petkovf9476b92008-10-13 21:39:50 +0200615 u32 tf_flags;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100616 u16 bcount;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200617
Borislav Petkoved485542009-01-02 16:12:52 +0100618 if (dev_is_idecd(drive)) {
619 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100620 bcount = ide_cd_get_xferlen(hwif->rq);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100621 expiry = ide_cd_expiry;
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100622 timeout = ATAPI_WAIT_PC;
Borislav Petkovd77612a2009-01-02 16:12:55 +0100623
624 if (drive->dma)
625 drive->dma = !hwif->dma_ops->dma_setup(drive);
Borislav Petkoved485542009-01-02 16:12:52 +0100626 } else {
Borislav Petkovd77612a2009-01-02 16:12:55 +0100627 pc = drive->pc;
628
629 /* We haven't transferred any data yet */
630 pc->xferred = 0;
631 pc->cur_pos = pc->buf;
632
Borislav Petkoved485542009-01-02 16:12:52 +0100633 tf_flags = IDE_TFLAG_OUT_DEVICE;
634 bcount = ((drive->media == ide_tape) ?
635 pc->req_xfer :
636 min(pc->req_xfer, 63 * 1024));
Borislav Petkovd77612a2009-01-02 16:12:55 +0100637
638 if (pc->flags & PC_FLAG_DMA_ERROR) {
639 pc->flags &= ~PC_FLAG_DMA_ERROR;
640 ide_dma_off(drive);
641 }
642
643 if ((pc->flags & PC_FLAG_DMA_OK) &&
644 (drive->dev_flags & IDE_DFLAG_USING_DMA))
645 drive->dma = !hwif->dma_ops->dma_setup(drive);
646
647 if (!drive->dma)
648 pc->flags &= ~PC_FLAG_DMA_OK;
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100649
650 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
651 : WAIT_TAPE_CMD;
Borislav Petkoved485542009-01-02 16:12:52 +0100652 }
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200653
Bartlomiej Zolnierkiewicz7a254df2009-03-24 23:22:39 +0100654 ide_pktcmd_tf_load(drive, tf_flags, bcount);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200655
656 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200657 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov5f258432009-01-02 16:12:53 +0100658 if (drive->dma)
659 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200660 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
Borislav Petkov4cad0852009-01-02 16:12:53 +0100661 timeout, expiry);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200662 return ide_started;
663 } else {
664 ide_execute_pkt_cmd(drive);
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200665 return ide_transfer_pc(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200666 }
667}
668EXPORT_SYMBOL_GPL(ide_issue_pc);