cw1200: drop useless LIST_HEAD
[linux-2.6-block.git] / drivers / ide / ide-atapi.c
CommitLineData
594c16d8
BZ
1/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
4cad085e 6#include <linux/cdrom.h>
594c16d8 7#include <linux/delay.h>
38789fda 8#include <linux/export.h>
594c16d8 9#include <linux/ide.h>
479edf06 10#include <linux/scatterlist.h>
5a0e3ad6 11#include <linux/gfp.h>
479edf06 12
646c0cb6
BZ
13#include <scsi/scsi.h>
14
103f7033
BP
15#define DRV_NAME "ide-atapi"
16#define PFX DRV_NAME ": "
17
646c0cb6
BZ
18#ifdef DEBUG
19#define debug_log(fmt, args...) \
20 printk(KERN_INFO "ide: " fmt, ## args)
21#else
22#define debug_log(fmt, args...) do {} while (0)
23#endif
24
8c662852
BP
25#define ATAPI_MIN_CDB_BYTES 12
26
991cb26a
BP
27static inline int dev_is_idecd(ide_drive_t *drive)
28{
5317464d 29 return drive->media == ide_cdrom || drive->media == ide_optical;
991cb26a
BP
30}
31
51509eec
BZ
32/*
33 * Check whether we can support a device,
34 * based on the ATAPI IDENTIFY command results.
35 */
36int ide_check_atapi_device(ide_drive_t *drive, const char *s)
37{
38 u16 *id = drive->id;
39 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
40
41 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
42
43 protocol = (gcw[1] & 0xC0) >> 6;
44 device_type = gcw[1] & 0x1F;
45 removable = (gcw[0] & 0x80) >> 7;
46 drq_type = (gcw[0] & 0x60) >> 5;
47 packet_size = gcw[0] & 0x03;
48
49#ifdef CONFIG_PPC
50 /* kludge for Apple PowerBook internal zip */
51 if (drive->media == ide_floppy && device_type == 5 &&
52 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
53 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
54 device_type = 0;
55#endif
56
57 if (protocol != 2)
58 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
59 s, drive->name, protocol);
60 else if ((drive->media == ide_floppy && device_type != 0) ||
61 (drive->media == ide_tape && device_type != 1))
62 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
63 s, drive->name, device_type);
64 else if (removable == 0)
65 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
66 s, drive->name);
67 else if (drive->media == ide_floppy && drq_type == 3)
68 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
69 "supported\n", s, drive->name, drq_type);
70 else if (packet_size != 0)
71 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
72 "bytes\n", s, drive->name, packet_size);
73 else
74 return 1;
75 return 0;
76}
77EXPORT_SYMBOL_GPL(ide_check_atapi_device);
78
7bf7420a
BZ
79void ide_init_pc(struct ide_atapi_pc *pc)
80{
81 memset(pc, 0, sizeof(*pc));
7bf7420a
BZ
82}
83EXPORT_SYMBOL_GPL(ide_init_pc);
84
2ac07d92
BZ
85/*
86 * Add a special packet command request to the tail of the request queue,
87 * and wait for it to be serviced.
88 */
89int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
b13345f3 90 struct ide_atapi_pc *pc, void *buf, unsigned int bufflen)
2ac07d92
BZ
91{
92 struct request *rq;
93 int error;
94
ff005a06 95 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
2f5a8e80 96 ide_req(rq)->type = ATA_PRIV_MISC;
22ce0a7c 97 ide_req(rq)->special = pc;
3eb76c1c 98
b13345f3
BP
99 if (buf && bufflen) {
100 error = blk_rq_map_kern(drive->queue, rq, buf, bufflen,
5c4be572
TH
101 GFP_NOIO);
102 if (error)
103 goto put_req;
3eb76c1c
BP
104 }
105
82ed4db4 106 memcpy(scsi_req(rq)->cmd, pc->c, 12);
2ac07d92 107 if (drive->media == ide_tape)
82ed4db4 108 scsi_req(rq)->cmd[13] = REQ_IDETAPE_PC1;
b7819b92 109 blk_execute_rq(drive->queue, disk, rq, 0);
17d5363b 110 error = scsi_req(rq)->result ? -EIO : 0;
5c4be572 111put_req:
2ac07d92 112 blk_put_request(rq);
2ac07d92
BZ
113 return error;
114}
115EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
116
de699ad5
BZ
117int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
118{
119 struct ide_atapi_pc pc;
120
121 ide_init_pc(&pc);
122 pc.c[0] = TEST_UNIT_READY;
123
b13345f3 124 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
de699ad5
BZ
125}
126EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
127
0c8a6c7a
BZ
128int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
129{
130 struct ide_atapi_pc pc;
131
132 ide_init_pc(&pc);
133 pc.c[0] = START_STOP;
134 pc.c[4] = start;
135
136 if (drive->media == ide_tape)
137 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
138
b13345f3 139 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
0c8a6c7a
BZ
140}
141EXPORT_SYMBOL_GPL(ide_do_start_stop);
142
0578042d
BZ
143int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
144{
145 struct ide_atapi_pc pc;
146
42619d35 147 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
0578042d
BZ
148 return 0;
149
150 ide_init_pc(&pc);
151 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
152 pc.c[4] = on;
153
b13345f3 154 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
0578042d
BZ
155}
156EXPORT_SYMBOL_GPL(ide_set_media_lock);
157
6b0da28b
BZ
158void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
159{
160 ide_init_pc(pc);
161 pc->c[0] = REQUEST_SENSE;
162 if (drive->media == ide_floppy) {
163 pc->c[4] = 255;
164 pc->req_xfer = 18;
165 } else {
166 pc->c[4] = 20;
167 pc->req_xfer = 20;
168 }
169}
170EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
171
a1df5169
BP
172void ide_prep_sense(ide_drive_t *drive, struct request *rq)
173{
174 struct request_sense *sense = &drive->sense_data;
60033520
JA
175 struct request *sense_rq;
176 struct scsi_request *req;
a1df5169 177 unsigned int cmd_len, sense_len;
5c4be572 178 int err;
a1df5169 179
a1df5169
BP
180 switch (drive->media) {
181 case ide_floppy:
182 cmd_len = 255;
183 sense_len = 18;
184 break;
185 case ide_tape:
186 cmd_len = 20;
187 sense_len = 20;
188 break;
189 default:
190 cmd_len = 18;
191 sense_len = 18;
192 }
193
194 BUG_ON(sense_len > sizeof(*sense));
195
2f5a8e80 196 if (ata_sense_request(rq) || drive->sense_rq_armed)
a1df5169
BP
197 return;
198
60033520
JA
199 sense_rq = drive->sense_rq;
200 if (!sense_rq) {
201 sense_rq = blk_mq_alloc_request(drive->queue, REQ_OP_DRV_IN,
202 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
203 drive->sense_rq = sense_rq;
204 }
205 req = scsi_req(sense_rq);
206
a1df5169
BP
207 memset(sense, 0, sizeof(*sense));
208
c8d9cf22 209 scsi_req_init(req);
a1df5169 210
5c4be572
TH
211 err = blk_rq_map_kern(drive->queue, sense_rq, sense, sense_len,
212 GFP_NOIO);
213 if (unlikely(err)) {
214 if (printk_ratelimit())
103f7033
BP
215 printk(KERN_WARNING PFX "%s: failed to map sense "
216 "buffer\n", drive->name);
60033520
JA
217 blk_mq_free_request(sense_rq);
218 drive->sense_rq = NULL;
5c4be572
TH
219 return;
220 }
221
222 sense_rq->rq_disk = rq->rq_disk;
aebf526b 223 sense_rq->cmd_flags = REQ_OP_DRV_IN;
2f5a8e80 224 ide_req(sense_rq)->type = ATA_PRIV_SENSE;
e8064021 225 sense_rq->rq_flags |= RQF_PREEMPT;
a1df5169 226
82ed4db4
CH
227 req->cmd[0] = GPCMD_REQUEST_SENSE;
228 req->cmd[4] = cmd_len;
a1df5169 229 if (drive->media == ide_tape)
82ed4db4 230 req->cmd[13] = REQ_IDETAPE_PC1;
a1df5169
BP
231
232 drive->sense_rq_armed = true;
233}
234EXPORT_SYMBOL_GPL(ide_prep_sense);
235
5c4be572 236int ide_queue_sense_rq(ide_drive_t *drive, void *special)
a1df5169 237{
60033520
JA
238 struct request *sense_rq = drive->sense_rq;
239
5c4be572
TH
240 /* deferred failure from ide_prep_sense() */
241 if (!drive->sense_rq_armed) {
103f7033 242 printk(KERN_WARNING PFX "%s: error queuing a sense request\n",
5c4be572
TH
243 drive->name);
244 return -ENOMEM;
245 }
a1df5169 246
22ce0a7c 247 ide_req(sense_rq)->special = special;
a1df5169
BP
248 drive->sense_rq_armed = false;
249
250 drive->hwif->rq = NULL;
251
60033520 252 ide_insert_request_head(drive, sense_rq);
5c4be572 253 return 0;
a1df5169
BP
254}
255EXPORT_SYMBOL_GPL(ide_queue_sense_rq);
256
6b0da28b
BZ
257/*
258 * Called when an error was detected during the last packet command.
6b544fcc
BP
259 * We queue a request sense packet command at the head of the request
260 * queue.
6b0da28b 261 */
6b544fcc 262void ide_retry_pc(ide_drive_t *drive)
6b0da28b 263{
8f6205cd 264 struct request *failed_rq = drive->hwif->rq;
82ed4db4 265 struct request *sense_rq = drive->sense_rq;
6b0da28b
BZ
266 struct ide_atapi_pc *pc = &drive->request_sense_pc;
267
268 (void)ide_read_error(drive);
6b544fcc
BP
269
270 /* init pc from sense_rq */
271 ide_init_pc(pc);
82ed4db4 272 memcpy(pc->c, scsi_req(sense_rq)->cmd, 12);
6b544fcc 273
6b0da28b 274 if (drive->media == ide_tape)
626542ca 275 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
6b544fcc 276
8f6205cd
TH
277 /*
278 * Push back the failed request and put request sense on top
279 * of it. The failed command will be retried after sense data
280 * is acquired.
281 */
8f6205cd 282 drive->hwif->rq = NULL;
1af18503 283 ide_requeue_and_plug(drive, failed_rq);
60033520 284 if (ide_queue_sense_rq(drive, pc))
2a842aca 285 ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(failed_rq));
6b0da28b
BZ
286}
287EXPORT_SYMBOL_GPL(ide_retry_pc);
288
4cad085e
BP
289int ide_cd_expiry(ide_drive_t *drive)
290{
b65fac32 291 struct request *rq = drive->hwif->rq;
4cad085e
BP
292 unsigned long wait = 0;
293
8dc7a31f 294 debug_log("%s: scsi_req(rq)->cmd[0]: 0x%x\n", __func__, scsi_req(rq)->cmd[0]);
4cad085e
BP
295
296 /*
297 * Some commands are *slow* and normally take a long time to complete.
298 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
299 * commands/drives support that. Let ide_timer_expiry keep polling us
300 * for these.
301 */
82ed4db4 302 switch (scsi_req(rq)->cmd[0]) {
4cad085e
BP
303 case GPCMD_BLANK:
304 case GPCMD_FORMAT_UNIT:
305 case GPCMD_RESERVE_RZONE_TRACK:
306 case GPCMD_CLOSE_TRACK:
307 case GPCMD_FLUSH_CACHE:
308 wait = ATAPI_WAIT_PC;
309 break;
310 default:
e8064021 311 if (!(rq->rq_flags & RQF_QUIET))
103f7033 312 printk(KERN_INFO PFX "cmd 0x%x timed out\n",
82ed4db4 313 scsi_req(rq)->cmd[0]);
4cad085e
BP
314 wait = 0;
315 break;
316 }
317 return wait;
318}
319EXPORT_SYMBOL_GPL(ide_cd_expiry);
320
392de1d5
BP
321int ide_cd_get_xferlen(struct request *rq)
322{
aebf526b
CH
323 switch (req_op(rq)) {
324 default:
392de1d5 325 return 32768;
aebf526b
CH
326 case REQ_OP_SCSI_IN:
327 case REQ_OP_SCSI_OUT:
34b7d2c9 328 return blk_rq_bytes(rq);
aebf526b
CH
329 case REQ_OP_DRV_IN:
330 case REQ_OP_DRV_OUT:
2f5a8e80
CH
331 switch (ide_req(rq)->type) {
332 case ATA_PRIV_PC:
333 case ATA_PRIV_SENSE:
334 return blk_rq_bytes(rq);
aebf526b
CH
335 default:
336 return 0;
2f5a8e80 337 }
33659ebb 338 }
392de1d5
BP
339}
340EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
341
0d6a9754
BZ
342void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
343{
3153c26b 344 struct ide_taskfile tf;
0d6a9754 345
3153c26b
SS
346 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
347 IDE_VALID_LBAM | IDE_VALID_LBAH);
0d6a9754 348
3153c26b
SS
349 *bcount = (tf.lbah << 8) | tf.lbam;
350 *ireason = tf.nsect & 3;
0d6a9754
BZ
351}
352EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
353
103f7033
BP
354/*
355 * Check the contents of the interrupt reason register and attempt to recover if
356 * there are problems.
357 *
358 * Returns:
359 * - 0 if everything's ok
360 * - 1 if the request has to be terminated.
361 */
362int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len,
363 int ireason, int rw)
364{
365 ide_hwif_t *hwif = drive->hwif;
366
367 debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw);
368
369 if (ireason == (!rw << 1))
370 return 0;
371 else if (ireason == (rw << 1)) {
372 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
373 drive->name, __func__);
374
375 if (dev_is_idecd(drive))
376 ide_pad_transfer(drive, rw, len);
377 } else if (!rw && ireason == ATAPI_COD) {
378 if (dev_is_idecd(drive)) {
379 /*
380 * Some drives (ASUS) seem to tell us that status info
381 * is available. Just get it and ignore.
382 */
383 (void)hwif->tp_ops->read_status(hwif);
384 return 0;
385 }
386 } else {
387 if (ireason & ATAPI_COD)
388 printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name,
389 __func__);
390
391 /* drive wants a command packet, or invalid ireason... */
392 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
393 drive->name, __func__, ireason);
394 }
395
2f5a8e80 396 if (dev_is_idecd(drive) && ata_pc_request(rq))
e8064021 397 rq->rq_flags |= RQF_FAILED;
103f7033
BP
398
399 return 1;
400}
401EXPORT_SYMBOL_GPL(ide_check_ireason);
402
aa5d2de7
BZ
403/*
404 * This is the usual interrupt handler which will be called during a packet
405 * command. We will transfer some of the data (as requested by the drive)
406 * and will re-point interrupt handler to us.
407 */
408static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
646c0cb6 409{
2b9efba4 410 struct ide_atapi_pc *pc = drive->pc;
646c0cb6 411 ide_hwif_t *hwif = drive->hwif;
f094d4d8 412 struct ide_cmd *cmd = &hwif->cmd;
b65fac32 413 struct request *rq = hwif->rq;
374e042c 414 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
d93bc452 415 unsigned int timeout, done;
646c0cb6 416 u16 bcount;
5d655a03 417 u8 stat, ireason, dsc = 0;
d93bc452 418 u8 write = !!(pc->flags & PC_FLAG_WRITING);
646c0cb6
BZ
419
420 debug_log("Enter %s - interrupt handler\n", __func__);
421
5d655a03
BP
422 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
423 : WAIT_TAPE_CMD;
844b9468 424
646c0cb6 425 /* Clear the interrupt */
374e042c 426 stat = tp_ops->read_status(hwif);
646c0cb6
BZ
427
428 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
88b4132e 429 int rc;
4453011f 430
88b4132e
BZ
431 drive->waiting_for_dma = 0;
432 rc = hwif->dma_ops->dma_end(drive);
f094d4d8 433 ide_dma_unmap_sg(drive, cmd);
4453011f
BZ
434
435 if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
5d655a03 436 if (drive->media == ide_floppy)
103f7033 437 printk(KERN_ERR PFX "%s: DMA %s error\n",
646c0cb6
BZ
438 drive->name, rq_data_dir(pc->rq)
439 ? "write" : "read");
440 pc->flags |= PC_FLAG_DMA_ERROR;
6d700387 441 } else
82ed4db4 442 scsi_req(rq)->resid_len = 0;
646c0cb6
BZ
443 debug_log("%s: DMA finished\n", drive->name);
444 }
445
446 /* No more interrupts */
3a7d2484 447 if ((stat & ATA_DRQ) == 0) {
2a842aca
CH
448 int uptodate;
449 blk_status_t error;
03a2faae 450
646c0cb6 451 debug_log("Packet command completed, %d bytes transferred\n",
077e6dba 452 blk_rq_bytes(rq));
646c0cb6
BZ
453
454 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
455
456 local_irq_enable_in_hardirq();
457
5d655a03 458 if (drive->media == ide_tape &&
82ed4db4 459 (stat & ATA_ERR) && scsi_req(rq)->cmd[0] == REQUEST_SENSE)
3a7d2484 460 stat &= ~ATA_ERR;
8fccf899 461
3a7d2484 462 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
646c0cb6
BZ
463 /* Error detected */
464 debug_log("%s: I/O error\n", drive->name);
465
5d655a03 466 if (drive->media != ide_tape)
17d5363b 467 scsi_req(pc->rq)->result++;
646c0cb6 468
82ed4db4 469 if (scsi_req(rq)->cmd[0] == REQUEST_SENSE) {
103f7033
BP
470 printk(KERN_ERR PFX "%s: I/O error in request "
471 "sense command\n", drive->name);
646c0cb6
BZ
472 return ide_do_reset(drive);
473 }
474
8dc7a31f 475 debug_log("[cmd %x]: check condition\n", scsi_req(rq)->cmd[0]);
646c0cb6
BZ
476
477 /* Retry operation */
6b544fcc 478 ide_retry_pc(drive);
8fccf899 479
646c0cb6
BZ
480 /* queued, but not started */
481 return ide_stopped;
482 }
646c0cb6 483 pc->error = 0;
b14c7212
BZ
484
485 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
486 dsc = 1;
8fccf899 487
b3071d19
TH
488 /*
489 * ->pc_callback() might change rq->data_len for
490 * residual count, cache total length.
491 */
21d9c5d2 492 done = blk_rq_bytes(rq);
b3071d19 493
646c0cb6 494 /* Command finished - Call the callback function */
03a2faae
BZ
495 uptodate = drive->pc_callback(drive, dsc);
496
497 if (uptodate == 0)
498 drive->failed_pc = NULL;
499
2f5a8e80 500 if (ata_misc_request(rq)) {
17d5363b 501 scsi_req(rq)->result = 0;
2a842aca 502 error = BLK_STS_OK;
89f78b32 503 } else {
349d12a1 504
aebf526b 505 if (blk_rq_is_passthrough(rq) && uptodate <= 0) {
17d5363b
CH
506 if (scsi_req(rq)->result == 0)
507 scsi_req(rq)->result = -EIO;
89f78b32 508 }
349d12a1 509
2a842aca 510 error = uptodate ? BLK_STS_OK : BLK_STS_IOERR;
89f78b32 511 }
8fccf899 512
c3a4d78c 513 ide_complete_rq(drive, error, blk_rq_bytes(rq));
646c0cb6
BZ
514 return ide_stopped;
515 }
516
517 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
518 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
103f7033
BP
519 printk(KERN_ERR PFX "%s: The device wants to issue more "
520 "interrupts in DMA mode\n", drive->name);
646c0cb6
BZ
521 ide_dma_off(drive);
522 return ide_do_reset(drive);
523 }
646c0cb6 524
1823649b
BZ
525 /* Get the number of bytes to transfer on this interrupt. */
526 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
646c0cb6 527
103f7033 528 if (ide_check_ireason(drive, rq, bcount, ireason, write))
646c0cb6 529 return ide_do_reset(drive);
8fccf899 530
6d700387
TH
531 done = min_t(unsigned int, bcount, cmd->nleft);
532 ide_pio_bytes(drive, cmd, write, done);
646c0cb6 533
6d700387 534 /* Update transferred byte count */
82ed4db4 535 scsi_req(rq)->resid_len -= done;
d93bc452
BZ
536
537 bcount -= done;
538
539 if (bcount)
540 ide_pad_transfer(drive, write, bcount);
541
077e6dba 542 debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
8dc7a31f 543 scsi_req(rq)->cmd[0], done, bcount, scsi_req(rq)->resid_len);
646c0cb6 544
646c0cb6 545 /* And set the interrupt handler again */
60c0cd02 546 ide_set_handler(drive, ide_pc_intr, timeout);
646c0cb6
BZ
547 return ide_started;
548}
594c16d8 549
60f85019 550static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
b788ee9c 551 u16 bcount, u8 dma)
7a254df0 552{
60f85019
SS
553 cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
554 cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
555 IDE_VALID_FEATURE | valid_tf;
35b5d0be 556 cmd->tf.command = ATA_CMD_PACKET;
b788ee9c
BZ
557 cmd->tf.feature = dma; /* Use PIO/DMA */
558 cmd->tf.lbam = bcount & 0xff;
559 cmd->tf.lbah = (bcount >> 8) & 0xff;
7a254df0
BZ
560}
561
88a72109
BZ
562static u8 ide_read_ireason(ide_drive_t *drive)
563{
3153c26b 564 struct ide_taskfile tf;
88a72109 565
3153c26b 566 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
88a72109 567
3153c26b 568 return tf.nsect & 3;
88a72109
BZ
569}
570
594c16d8
BZ
571static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
572{
594c16d8
BZ
573 int retries = 100;
574
3a7d2484
BZ
575 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
576 (ireason & ATAPI_IO))) {
103f7033 577 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
594c16d8
BZ
578 "a packet command, retrying\n", drive->name);
579 udelay(100);
88a72109 580 ireason = ide_read_ireason(drive);
594c16d8 581 if (retries == 0) {
103f7033
BP
582 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
583 " a packet command, ignoring\n",
594c16d8 584 drive->name);
3a7d2484
BZ
585 ireason |= ATAPI_COD;
586 ireason &= ~ATAPI_IO;
594c16d8
BZ
587 }
588 }
589
590 return ireason;
591}
592
baf08f0b
BZ
593static int ide_delayed_transfer_pc(ide_drive_t *drive)
594{
595 /* Send the actual packet */
596 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
597
598 /* Timeout for the packet command */
599 return WAIT_FLOPPY_CMD;
600}
601
602static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
594c16d8 603{
b16aabc9 604 struct ide_atapi_pc *uninitialized_var(pc);
594c16d8 605 ide_hwif_t *hwif = drive->hwif;
b65fac32 606 struct request *rq = hwif->rq;
baf08f0b
BZ
607 ide_expiry_t *expiry;
608 unsigned int timeout;
8c662852 609 int cmd_len;
594c16d8
BZ
610 ide_startstop_t startstop;
611 u8 ireason;
612
3a7d2484 613 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
103f7033 614 printk(KERN_ERR PFX "%s: Strange, packet command initiated yet "
594c16d8
BZ
615 "DRQ isn't asserted\n", drive->name);
616 return startstop;
617 }
618
5f25843f
BP
619 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
620 if (drive->dma)
621 drive->waiting_for_dma = 1;
622 }
623
8c662852
BP
624 if (dev_is_idecd(drive)) {
625 /* ATAPI commands get padded out to 12 bytes minimum */
82ed4db4 626 cmd_len = COMMAND_SIZE(scsi_req(rq)->cmd[0]);
8c662852
BP
627 if (cmd_len < ATAPI_MIN_CDB_BYTES)
628 cmd_len = ATAPI_MIN_CDB_BYTES;
8c662852 629
def860d0
BP
630 timeout = rq->timeout;
631 expiry = ide_cd_expiry;
baf08f0b 632 } else {
b16aabc9
BP
633 pc = drive->pc;
634
def860d0
BP
635 cmd_len = ATAPI_MIN_CDB_BYTES;
636
637 /*
638 * If necessary schedule the packet transfer to occur 'timeout'
19af5cdb 639 * milliseconds later in ide_delayed_transfer_pc() after the
def860d0
BP
640 * device says it's ready for a packet.
641 */
642 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
643 timeout = drive->pc_delay;
644 expiry = &ide_delayed_transfer_pc;
645 } else {
646 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
647 : WAIT_TAPE_CMD;
648 expiry = NULL;
649 }
06cc2778
BP
650
651 ireason = ide_read_ireason(drive);
652 if (drive->media == ide_tape)
653 ireason = ide_wait_ireason(drive, ireason);
654
655 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
103f7033
BP
656 printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
657 "issuing a packet command\n", drive->name);
06cc2778
BP
658
659 return ide_do_reset(drive);
660 }
baf08f0b
BZ
661 }
662
60c0cd02
BZ
663 hwif->expiry = expiry;
664
594c16d8 665 /* Set the interrupt routine */
d6251d44
BP
666 ide_set_handler(drive,
667 (dev_is_idecd(drive) ? drive->irq_handler
668 : ide_pc_intr),
60c0cd02 669 timeout);
594c16d8 670
2eba0827
BP
671 /* Send the actual packet */
672 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
82ed4db4 673 hwif->tp_ops->output_data(drive, NULL, scsi_req(rq)->cmd, cmd_len);
2eba0827 674
594c16d8 675 /* Begin DMA, if necessary */
b16aabc9
BP
676 if (dev_is_idecd(drive)) {
677 if (drive->dma)
678 hwif->dma_ops->dma_start(drive);
679 } else {
680 if (pc->flags & PC_FLAG_DMA_OK) {
681 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
682 hwif->dma_ops->dma_start(drive);
683 }
594c16d8
BZ
684 }
685
594c16d8
BZ
686 return ide_started;
687}
6bf1641c 688
b788ee9c 689ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
6bf1641c 690{
d77612ab 691 struct ide_atapi_pc *pc;
6bf1641c 692 ide_hwif_t *hwif = drive->hwif;
4cad085e 693 ide_expiry_t *expiry = NULL;
e6830a86 694 struct request *rq = hwif->rq;
dfb7e621 695 unsigned int timeout, bytes;
392de1d5 696 u16 bcount;
60f85019 697 u8 valid_tf;
35b5d0be 698 u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
6bf1641c 699
ed48554f 700 if (dev_is_idecd(drive)) {
60f85019 701 valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
e6830a86 702 bcount = ide_cd_get_xferlen(rq);
4cad085e 703 expiry = ide_cd_expiry;
28ad91db 704 timeout = ATAPI_WAIT_PC;
d77612ab 705
5ae5412d
BZ
706 if (drive->dma)
707 drive->dma = !ide_dma_prepare(drive, cmd);
ed48554f 708 } else {
d77612ab
BP
709 pc = drive->pc;
710
60f85019 711 valid_tf = IDE_VALID_DEVICE;
dfb7e621 712 bytes = blk_rq_bytes(rq);
dfb7e621
BP
713 bcount = ((drive->media == ide_tape) ? bytes
714 : min_t(unsigned int,
715 bytes, 63 * 1024));
6bf1641c 716
077e6dba 717 /* We haven't transferred any data yet */
82ed4db4 718 scsi_req(rq)->resid_len = bcount;
6bf1641c 719
d77612ab
BP
720 if (pc->flags & PC_FLAG_DMA_ERROR) {
721 pc->flags &= ~PC_FLAG_DMA_ERROR;
722 ide_dma_off(drive);
723 }
6bf1641c 724
5ae5412d
BZ
725 if (pc->flags & PC_FLAG_DMA_OK)
726 drive->dma = !ide_dma_prepare(drive, cmd);
6bf1641c 727
d77612ab
BP
728 if (!drive->dma)
729 pc->flags &= ~PC_FLAG_DMA_OK;
28ad91db
BP
730
731 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
732 : WAIT_TAPE_CMD;
d77612ab 733 }
6bf1641c 734
60f85019 735 ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
b788ee9c
BZ
736
737 (void)do_rw_taskfile(drive, cmd);
6bf1641c 738
35b5d0be 739 if (drq_int) {
5f25843f
BP
740 if (drive->dma)
741 drive->waiting_for_dma = 0;
22117d6e 742 hwif->expiry = expiry;
6bf1641c 743 }
35b5d0be
BZ
744
745 ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
746
747 return drq_int ? ide_started : ide_transfer_pc(drive);
6bf1641c
BZ
748}
749EXPORT_SYMBOL_GPL(ide_issue_pc);