blob: c110329ccb138fd059f85927b1e744bad4c700f9 [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
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200243int ide_scsi_expiry(ide_drive_t *drive)
244{
245 struct ide_atapi_pc *pc = drive->pc;
246
247 debug_log("%s called for %lu at %lu\n", __func__,
248 pc->scsi_cmd->serial_number, jiffies);
249
250 pc->flags |= PC_FLAG_TIMEDOUT;
251
252 return 0; /* we do not want the IDE subsystem to retry */
253}
254EXPORT_SYMBOL_GPL(ide_scsi_expiry);
255
Borislav Petkov4cad0852009-01-02 16:12:53 +0100256int ide_cd_expiry(ide_drive_t *drive)
257{
258 struct request *rq = HWGROUP(drive)->rq;
259 unsigned long wait = 0;
260
261 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
262
263 /*
264 * Some commands are *slow* and normally take a long time to complete.
265 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
266 * commands/drives support that. Let ide_timer_expiry keep polling us
267 * for these.
268 */
269 switch (rq->cmd[0]) {
270 case GPCMD_BLANK:
271 case GPCMD_FORMAT_UNIT:
272 case GPCMD_RESERVE_RZONE_TRACK:
273 case GPCMD_CLOSE_TRACK:
274 case GPCMD_FLUSH_CACHE:
275 wait = ATAPI_WAIT_PC;
276 break;
277 default:
278 if (!(rq->cmd_flags & REQ_QUIET))
279 printk(KERN_INFO "cmd 0x%x timed out\n",
280 rq->cmd[0]);
281 wait = 0;
282 break;
283 }
284 return wait;
285}
286EXPORT_SYMBOL_GPL(ide_cd_expiry);
287
Borislav Petkov392de1d2009-01-02 16:12:52 +0100288int ide_cd_get_xferlen(struct request *rq)
289{
290 if (blk_fs_request(rq))
291 return 32768;
292 else if (blk_sense_request(rq) || blk_pc_request(rq) ||
293 rq->cmd_type == REQ_TYPE_ATA_PC)
294 return rq->data_len;
295 else
296 return 0;
297}
298EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
299
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200300/*
301 * This is the usual interrupt handler which will be called during a packet
302 * command. We will transfer some of the data (as requested by the drive)
303 * and will re-point interrupt handler to us.
304 */
305static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200306{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200307 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200308 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200309 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200310 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200311 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200312 ide_expiry_t *expiry;
313 unsigned int timeout, temp;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200314 u16 bcount;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200315 u8 stat, ireason, scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI), dsc = 0;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200316
317 debug_log("Enter %s - interrupt handler\n", __func__);
318
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200319 if (scsi) {
320 timeout = ide_scsi_get_timeout(pc);
321 expiry = ide_scsi_expiry;
322 } else {
323 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
324 : WAIT_TAPE_CMD;
325 expiry = NULL;
326 }
327
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200328 if (pc->flags & PC_FLAG_TIMEDOUT) {
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200329 drive->pc_callback(drive, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200330 return ide_stopped;
331 }
332
333 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200334 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200335
336 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
337 if (hwif->dma_ops->dma_end(drive) ||
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200338 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200339 if (drive->media == ide_floppy && !scsi)
340 printk(KERN_ERR "%s: DMA %s error\n",
341 drive->name, rq_data_dir(pc->rq)
342 ? "write" : "read");
343 pc->flags |= PC_FLAG_DMA_ERROR;
344 } else {
345 pc->xferred = pc->req_xfer;
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200346 if (drive->pc_update_buffers)
347 drive->pc_update_buffers(drive, pc);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200348 }
349 debug_log("%s: DMA finished\n", drive->name);
350 }
351
352 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200353 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200354 debug_log("Packet command completed, %d bytes transferred\n",
355 pc->xferred);
356
357 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
358
359 local_irq_enable_in_hardirq();
360
361 if (drive->media == ide_tape && !scsi &&
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200362 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
363 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200364
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200365 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200366 /* Error detected */
367 debug_log("%s: I/O error\n", drive->name);
368
369 if (drive->media != ide_tape || scsi) {
370 pc->rq->errors++;
371 if (scsi)
372 goto cmd_finished;
373 }
374
Borislav Petkov8fccf892008-07-23 19:56:01 +0200375 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200376 printk(KERN_ERR "%s: I/O error in request sense"
377 " command\n", drive->name);
378 return ide_do_reset(drive);
379 }
380
Borislav Petkov8fccf892008-07-23 19:56:01 +0200381 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200382
383 /* Retry operation */
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200384 ide_retry_pc(drive, rq->rq_disk);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200385
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200386 /* queued, but not started */
387 return ide_stopped;
388 }
389cmd_finished:
390 pc->error = 0;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200391
392 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
393 dsc = 1;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200394
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200395 /* Command finished - Call the callback function */
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200396 drive->pc_callback(drive, dsc);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200397
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200398 return ide_stopped;
399 }
400
401 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
402 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
403 printk(KERN_ERR "%s: The device wants to issue more interrupts "
404 "in DMA mode\n", drive->name);
405 ide_dma_off(drive);
406 return ide_do_reset(drive);
407 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200408
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200409 /* Get the number of bytes to transfer on this interrupt. */
410 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200411
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200412 if (ireason & ATAPI_COD) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200413 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
414 return ide_do_reset(drive);
415 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200416
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200417 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
418 !!(pc->flags & PC_FLAG_WRITING)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200419 /* Hopefully, we will never get here */
420 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
421 "to %s!\n", drive->name,
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200422 (ireason & ATAPI_IO) ? "Write" : "Read",
423 (ireason & ATAPI_IO) ? "Read" : "Write");
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200424 return ide_do_reset(drive);
425 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200426
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200427 if (!(pc->flags & PC_FLAG_WRITING)) {
428 /* Reading - Check that we have enough space */
429 temp = pc->xferred + bcount;
430 if (temp > pc->req_xfer) {
431 if (temp > pc->buf_size) {
432 printk(KERN_ERR "%s: The device wants to send "
433 "us more data than expected - "
434 "discarding data\n",
435 drive->name);
436 if (scsi)
437 temp = pc->buf_size - pc->xferred;
438 else
439 temp = 0;
440 if (temp) {
441 if (pc->sg)
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200442 drive->pc_io_buffers(drive, pc,
443 temp, 0);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200444 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200445 tp_ops->input_data(drive, NULL,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200446 pc->cur_pos, temp);
447 printk(KERN_ERR "%s: transferred %d of "
448 "%d bytes\n",
449 drive->name,
450 temp, bcount);
451 }
452 pc->xferred += temp;
453 pc->cur_pos += temp;
454 ide_pad_transfer(drive, 0, bcount - temp);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200455 goto next_irq;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200456 }
457 debug_log("The device wants to send us more data than "
458 "expected - allowing transfer\n");
459 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200460 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200461 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200462 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200463
464 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
465 (drive->media == ide_tape && !scsi && pc->bh) ||
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200466 (scsi && pc->sg)) {
Bartlomiej Zolnierkiewicz85e39032008-10-13 21:39:32 +0200467 int done = drive->pc_io_buffers(drive, pc, bcount,
Bartlomiej Zolnierkiewiczacaa0f52008-10-10 22:39:36 +0200468 !!(pc->flags & PC_FLAG_WRITING));
469
470 /* FIXME: don't do partial completions */
471 if (drive->media == ide_floppy && !scsi)
472 ide_end_request(drive, 1, done >> 9);
473 } else
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200474 xferfunc(drive, NULL, pc->cur_pos, bcount);
475
476 /* Update the current position */
477 pc->xferred += bcount;
478 pc->cur_pos += bcount;
479
480 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200481 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200482next_irq:
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200483 /* And set the interrupt handler again */
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200484 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200485 return ide_started;
486}
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200487
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200488static u8 ide_read_ireason(ide_drive_t *drive)
489{
490 ide_task_t task;
491
492 memset(&task, 0, sizeof(task));
493 task.tf_flags = IDE_TFLAG_IN_NSECT;
494
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200495 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200496
497 return task.tf.nsect & 3;
498}
499
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200500static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
501{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200502 int retries = 100;
503
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200504 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
505 (ireason & ATAPI_IO))) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200506 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
507 "a packet command, retrying\n", drive->name);
508 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200509 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200510 if (retries == 0) {
511 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
512 "a packet command, ignoring\n",
513 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200514 ireason |= ATAPI_COD;
515 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200516 }
517 }
518
519 return ireason;
520}
521
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200522static int ide_delayed_transfer_pc(ide_drive_t *drive)
523{
524 /* Send the actual packet */
525 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
526
527 /* Timeout for the packet command */
528 return WAIT_FLOPPY_CMD;
529}
530
531static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200532{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200533 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200534 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200535 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200536 ide_expiry_t *expiry;
537 unsigned int timeout;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200538 ide_startstop_t startstop;
539 u8 ireason;
540
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200541 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200542 printk(KERN_ERR "%s: Strange, packet command initiated yet "
543 "DRQ isn't asserted\n", drive->name);
544 return startstop;
545 }
546
Borislav Petkov5f258432009-01-02 16:12:53 +0100547 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
548 if (drive->dma)
549 drive->waiting_for_dma = 1;
550 }
551
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200552 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200553 if (drive->media == ide_tape &&
554 (drive->dev_flags & IDE_DFLAG_SCSI) == 0)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200555 ireason = ide_wait_ireason(drive, ireason);
556
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200557 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200558 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
559 "a packet command\n", drive->name);
560 return ide_do_reset(drive);
561 }
562
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200563 /*
564 * If necessary schedule the packet transfer to occur 'timeout'
565 * miliseconds later in ide_delayed_transfer_pc() after the device
566 * says it's ready for a packet.
567 */
568 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
569 timeout = drive->pc_delay;
570 expiry = &ide_delayed_transfer_pc;
571 } else {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200572 if (drive->dev_flags & IDE_DFLAG_SCSI) {
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200573 timeout = ide_scsi_get_timeout(pc);
574 expiry = ide_scsi_expiry;
575 } else {
576 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
577 : WAIT_TAPE_CMD;
578 expiry = NULL;
579 }
580 }
581
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200582 /* Set the interrupt routine */
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200583 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200584
585 /* Begin DMA, if necessary */
586 if (pc->flags & PC_FLAG_DMA_OK) {
587 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
588 hwif->dma_ops->dma_start(drive);
589 }
590
591 /* Send the actual packet */
Borislav Petkovea68d272008-07-23 19:56:01 +0200592 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200593 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200594
595 return ide_started;
596}
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200597
Borislav Petkov4cad0852009-01-02 16:12:53 +0100598ide_startstop_t ide_issue_pc(ide_drive_t *drive, unsigned int timeout)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200599{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200600 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200601 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100602 ide_expiry_t *expiry = NULL;
Borislav Petkovf9476b92008-10-13 21:39:50 +0200603 u32 tf_flags;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100604 u16 bcount;
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200605 u8 scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200606
607 /* We haven't transferred any data yet */
608 pc->xferred = 0;
609 pc->cur_pos = pc->buf;
610
Borislav Petkoved485542009-01-02 16:12:52 +0100611 if (dev_is_idecd(drive)) {
612 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100613 bcount = ide_cd_get_xferlen(hwif->hwgroup->rq);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100614 expiry = ide_cd_expiry;
Borislav Petkoved485542009-01-02 16:12:52 +0100615 } else if (scsi) {
616 tf_flags = 0;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200617 bcount = min(pc->req_xfer, 63 * 1024);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100618 expiry = ide_scsi_expiry;
Borislav Petkoved485542009-01-02 16:12:52 +0100619 } else {
620 tf_flags = IDE_TFLAG_OUT_DEVICE;
621 bcount = ((drive->media == ide_tape) ?
622 pc->req_xfer :
623 min(pc->req_xfer, 63 * 1024));
624 }
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200625
626 if (pc->flags & PC_FLAG_DMA_ERROR) {
627 pc->flags &= ~PC_FLAG_DMA_ERROR;
628 ide_dma_off(drive);
629 }
630
Borislav Petkov4f02ff02009-01-02 16:12:52 +0100631 if (((pc->flags & PC_FLAG_DMA_OK) &&
632 (drive->dev_flags & IDE_DFLAG_USING_DMA)) ||
633 drive->dma) {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200634 if (scsi)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200635 hwif->sg_mapped = 1;
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200636 drive->dma = !hwif->dma_ops->dma_setup(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200637 if (scsi)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200638 hwif->sg_mapped = 0;
639 }
640
Borislav Petkov0a9b6f82008-10-13 21:39:49 +0200641 if (!drive->dma)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200642 pc->flags &= ~PC_FLAG_DMA_OK;
643
Borislav Petkovf9476b92008-10-13 21:39:50 +0200644 ide_pktcmd_tf_load(drive, tf_flags, bcount, drive->dma);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200645
646 /* Issue the packet command */
Borislav Petkovac77ef82008-07-23 19:56:01 +0200647 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov5f258432009-01-02 16:12:53 +0100648 if (drive->dma)
649 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200650 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
Borislav Petkov4cad0852009-01-02 16:12:53 +0100651 timeout, expiry);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200652 return ide_started;
653 } else {
654 ide_execute_pkt_cmd(drive);
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200655 return ide_transfer_pc(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200656 }
657}
658EXPORT_SYMBOL_GPL(ide_issue_pc);