1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/kernel.h>
7 #include <linux/cdrom.h>
8 #include <linux/delay.h>
9 #include <linux/export.h>
10 #include <linux/ide.h>
11 #include <linux/scatterlist.h>
12 #include <linux/gfp.h>
14 #include <scsi/scsi.h>
16 #define DRV_NAME "ide-atapi"
17 #define PFX DRV_NAME ": "
20 #define debug_log(fmt, args...) \
21 printk(KERN_INFO "ide: " fmt, ## args)
23 #define debug_log(fmt, args...) do {} while (0)
26 #define ATAPI_MIN_CDB_BYTES 12
28 static inline int dev_is_idecd(ide_drive_t *drive)
30 return drive->media == ide_cdrom || drive->media == ide_optical;
34 * Check whether we can support a device,
35 * based on the ATAPI IDENTIFY command results.
37 int ide_check_atapi_device(ide_drive_t *drive, const char *s)
40 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
42 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
44 protocol = (gcw[1] & 0xC0) >> 6;
45 device_type = gcw[1] & 0x1F;
46 removable = (gcw[0] & 0x80) >> 7;
47 drq_type = (gcw[0] & 0x60) >> 5;
48 packet_size = gcw[0] & 0x03;
51 /* kludge for Apple PowerBook internal zip */
52 if (drive->media == ide_floppy && device_type == 5 &&
53 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
54 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
59 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
60 s, drive->name, protocol);
61 else if ((drive->media == ide_floppy && device_type != 0) ||
62 (drive->media == ide_tape && device_type != 1))
63 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
64 s, drive->name, device_type);
65 else if (removable == 0)
66 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
68 else if (drive->media == ide_floppy && drq_type == 3)
69 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
70 "supported\n", s, drive->name, drq_type);
71 else if (packet_size != 0)
72 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
73 "bytes\n", s, drive->name, packet_size);
78 EXPORT_SYMBOL_GPL(ide_check_atapi_device);
80 void ide_init_pc(struct ide_atapi_pc *pc)
82 memset(pc, 0, sizeof(*pc));
84 EXPORT_SYMBOL_GPL(ide_init_pc);
87 * Add a special packet command request to the tail of the request queue,
88 * and wait for it to be serviced.
90 int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
91 struct ide_atapi_pc *pc, void *buf, unsigned int bufflen)
96 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
97 ide_req(rq)->type = ATA_PRIV_MISC;
98 ide_req(rq)->special = pc;
100 if (buf && bufflen) {
101 error = blk_rq_map_kern(drive->queue, rq, buf, bufflen,
107 memcpy(scsi_req(rq)->cmd, pc->c, 12);
108 if (drive->media == ide_tape)
109 scsi_req(rq)->cmd[13] = REQ_IDETAPE_PC1;
110 blk_execute_rq(drive->queue, disk, rq, 0);
111 error = scsi_req(rq)->result ? -EIO : 0;
116 EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
118 int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
120 struct ide_atapi_pc pc;
123 pc.c[0] = TEST_UNIT_READY;
125 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
127 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
129 int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
131 struct ide_atapi_pc pc;
134 pc.c[0] = START_STOP;
137 if (drive->media == ide_tape)
138 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
140 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
142 EXPORT_SYMBOL_GPL(ide_do_start_stop);
144 int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
146 struct ide_atapi_pc pc;
148 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
152 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
155 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
157 EXPORT_SYMBOL_GPL(ide_set_media_lock);
159 void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
162 pc->c[0] = REQUEST_SENSE;
163 if (drive->media == ide_floppy) {
171 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
173 void ide_prep_sense(ide_drive_t *drive, struct request *rq)
175 struct request_sense *sense = &drive->sense_data;
176 struct request *sense_rq;
177 struct scsi_request *req;
178 unsigned int cmd_len, sense_len;
181 switch (drive->media) {
195 BUG_ON(sense_len > sizeof(*sense));
197 if (ata_sense_request(rq) || drive->sense_rq_armed)
200 sense_rq = drive->sense_rq;
202 sense_rq = blk_mq_alloc_request(drive->queue, REQ_OP_DRV_IN,
203 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
204 drive->sense_rq = sense_rq;
206 req = scsi_req(sense_rq);
208 memset(sense, 0, sizeof(*sense));
212 err = blk_rq_map_kern(drive->queue, sense_rq, sense, sense_len,
215 if (printk_ratelimit())
216 printk(KERN_WARNING PFX "%s: failed to map sense "
217 "buffer\n", drive->name);
218 blk_mq_free_request(sense_rq);
219 drive->sense_rq = NULL;
223 sense_rq->rq_disk = rq->rq_disk;
224 sense_rq->cmd_flags = REQ_OP_DRV_IN;
225 ide_req(sense_rq)->type = ATA_PRIV_SENSE;
227 req->cmd[0] = GPCMD_REQUEST_SENSE;
228 req->cmd[4] = cmd_len;
229 if (drive->media == ide_tape)
230 req->cmd[13] = REQ_IDETAPE_PC1;
232 drive->sense_rq_armed = true;
234 EXPORT_SYMBOL_GPL(ide_prep_sense);
236 int ide_queue_sense_rq(ide_drive_t *drive, void *special)
238 ide_hwif_t *hwif = drive->hwif;
239 struct request *sense_rq;
242 spin_lock_irqsave(&hwif->lock, flags);
244 /* deferred failure from ide_prep_sense() */
245 if (!drive->sense_rq_armed) {
246 printk(KERN_WARNING PFX "%s: error queuing a sense request\n",
248 spin_unlock_irqrestore(&hwif->lock, flags);
252 sense_rq = drive->sense_rq;
253 ide_req(sense_rq)->special = special;
254 drive->sense_rq_armed = false;
256 drive->hwif->rq = NULL;
258 ide_insert_request_head(drive, sense_rq);
259 spin_unlock_irqrestore(&hwif->lock, flags);
262 EXPORT_SYMBOL_GPL(ide_queue_sense_rq);
265 * Called when an error was detected during the last packet command.
266 * We queue a request sense packet command at the head of the request
269 void ide_retry_pc(ide_drive_t *drive)
271 struct request *failed_rq = drive->hwif->rq;
272 struct request *sense_rq = drive->sense_rq;
273 struct ide_atapi_pc *pc = &drive->request_sense_pc;
275 (void)ide_read_error(drive);
277 /* init pc from sense_rq */
279 memcpy(pc->c, scsi_req(sense_rq)->cmd, 12);
281 if (drive->media == ide_tape)
282 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
285 * Push back the failed request and put request sense on top
286 * of it. The failed command will be retried after sense data
289 drive->hwif->rq = NULL;
290 ide_requeue_and_plug(drive, failed_rq);
291 if (ide_queue_sense_rq(drive, pc))
292 ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(failed_rq));
294 EXPORT_SYMBOL_GPL(ide_retry_pc);
296 int ide_cd_expiry(ide_drive_t *drive)
298 struct request *rq = drive->hwif->rq;
299 unsigned long wait = 0;
301 debug_log("%s: scsi_req(rq)->cmd[0]: 0x%x\n", __func__, scsi_req(rq)->cmd[0]);
304 * Some commands are *slow* and normally take a long time to complete.
305 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
306 * commands/drives support that. Let ide_timer_expiry keep polling us
309 switch (scsi_req(rq)->cmd[0]) {
311 case GPCMD_FORMAT_UNIT:
312 case GPCMD_RESERVE_RZONE_TRACK:
313 case GPCMD_CLOSE_TRACK:
314 case GPCMD_FLUSH_CACHE:
315 wait = ATAPI_WAIT_PC;
318 if (!(rq->rq_flags & RQF_QUIET))
319 printk(KERN_INFO PFX "cmd 0x%x timed out\n",
320 scsi_req(rq)->cmd[0]);
326 EXPORT_SYMBOL_GPL(ide_cd_expiry);
328 int ide_cd_get_xferlen(struct request *rq)
330 switch (req_op(rq)) {
334 case REQ_OP_SCSI_OUT:
335 return blk_rq_bytes(rq);
338 switch (ide_req(rq)->type) {
341 return blk_rq_bytes(rq);
347 EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
349 void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
351 struct ide_taskfile tf;
353 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
354 IDE_VALID_LBAM | IDE_VALID_LBAH);
356 *bcount = (tf.lbah << 8) | tf.lbam;
357 *ireason = tf.nsect & 3;
359 EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
362 * Check the contents of the interrupt reason register and attempt to recover if
363 * there are problems.
366 * - 0 if everything's ok
367 * - 1 if the request has to be terminated.
369 int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len,
372 ide_hwif_t *hwif = drive->hwif;
374 debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw);
376 if (ireason == (!rw << 1))
378 else if (ireason == (rw << 1)) {
379 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
380 drive->name, __func__);
382 if (dev_is_idecd(drive))
383 ide_pad_transfer(drive, rw, len);
384 } else if (!rw && ireason == ATAPI_COD) {
385 if (dev_is_idecd(drive)) {
387 * Some drives (ASUS) seem to tell us that status info
388 * is available. Just get it and ignore.
390 (void)hwif->tp_ops->read_status(hwif);
394 if (ireason & ATAPI_COD)
395 printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name,
398 /* drive wants a command packet, or invalid ireason... */
399 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
400 drive->name, __func__, ireason);
403 if (dev_is_idecd(drive) && ata_pc_request(rq))
404 rq->rq_flags |= RQF_FAILED;
408 EXPORT_SYMBOL_GPL(ide_check_ireason);
411 * This is the usual interrupt handler which will be called during a packet
412 * command. We will transfer some of the data (as requested by the drive)
413 * and will re-point interrupt handler to us.
415 static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
417 struct ide_atapi_pc *pc = drive->pc;
418 ide_hwif_t *hwif = drive->hwif;
419 struct ide_cmd *cmd = &hwif->cmd;
420 struct request *rq = hwif->rq;
421 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
422 unsigned int timeout, done;
424 u8 stat, ireason, dsc = 0;
425 u8 write = !!(pc->flags & PC_FLAG_WRITING);
427 debug_log("Enter %s - interrupt handler\n", __func__);
429 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
432 /* Clear the interrupt */
433 stat = tp_ops->read_status(hwif);
435 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
438 drive->waiting_for_dma = 0;
439 rc = hwif->dma_ops->dma_end(drive);
440 ide_dma_unmap_sg(drive, cmd);
442 if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
443 if (drive->media == ide_floppy)
444 printk(KERN_ERR PFX "%s: DMA %s error\n",
445 drive->name, rq_data_dir(pc->rq)
447 pc->flags |= PC_FLAG_DMA_ERROR;
449 scsi_req(rq)->resid_len = 0;
450 debug_log("%s: DMA finished\n", drive->name);
453 /* No more interrupts */
454 if ((stat & ATA_DRQ) == 0) {
458 debug_log("Packet command completed, %d bytes transferred\n",
461 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
463 local_irq_enable_in_hardirq();
465 if (drive->media == ide_tape &&
466 (stat & ATA_ERR) && scsi_req(rq)->cmd[0] == REQUEST_SENSE)
469 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
471 debug_log("%s: I/O error\n", drive->name);
473 if (drive->media != ide_tape)
474 scsi_req(pc->rq)->result++;
476 if (scsi_req(rq)->cmd[0] == REQUEST_SENSE) {
477 printk(KERN_ERR PFX "%s: I/O error in request "
478 "sense command\n", drive->name);
479 return ide_do_reset(drive);
482 debug_log("[cmd %x]: check condition\n", scsi_req(rq)->cmd[0]);
484 /* Retry operation */
487 /* queued, but not started */
492 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
496 * ->pc_callback() might change rq->data_len for
497 * residual count, cache total length.
499 done = blk_rq_bytes(rq);
501 /* Command finished - Call the callback function */
502 uptodate = drive->pc_callback(drive, dsc);
505 drive->failed_pc = NULL;
507 if (ata_misc_request(rq)) {
508 scsi_req(rq)->result = 0;
512 if (blk_rq_is_passthrough(rq) && uptodate <= 0) {
513 if (scsi_req(rq)->result == 0)
514 scsi_req(rq)->result = -EIO;
517 error = uptodate ? BLK_STS_OK : BLK_STS_IOERR;
520 ide_complete_rq(drive, error, blk_rq_bytes(rq));
524 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
525 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
526 printk(KERN_ERR PFX "%s: The device wants to issue more "
527 "interrupts in DMA mode\n", drive->name);
529 return ide_do_reset(drive);
532 /* Get the number of bytes to transfer on this interrupt. */
533 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
535 if (ide_check_ireason(drive, rq, bcount, ireason, write))
536 return ide_do_reset(drive);
538 done = min_t(unsigned int, bcount, cmd->nleft);
539 ide_pio_bytes(drive, cmd, write, done);
541 /* Update transferred byte count */
542 scsi_req(rq)->resid_len -= done;
547 ide_pad_transfer(drive, write, bcount);
549 debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
550 scsi_req(rq)->cmd[0], done, bcount, scsi_req(rq)->resid_len);
552 /* And set the interrupt handler again */
553 ide_set_handler(drive, ide_pc_intr, timeout);
557 static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
560 cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
561 cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
562 IDE_VALID_FEATURE | valid_tf;
563 cmd->tf.command = ATA_CMD_PACKET;
564 cmd->tf.feature = dma; /* Use PIO/DMA */
565 cmd->tf.lbam = bcount & 0xff;
566 cmd->tf.lbah = (bcount >> 8) & 0xff;
569 static u8 ide_read_ireason(ide_drive_t *drive)
571 struct ide_taskfile tf;
573 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
578 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
582 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
583 (ireason & ATAPI_IO))) {
584 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
585 "a packet command, retrying\n", drive->name);
587 ireason = ide_read_ireason(drive);
589 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
590 " a packet command, ignoring\n",
592 ireason |= ATAPI_COD;
593 ireason &= ~ATAPI_IO;
600 static int ide_delayed_transfer_pc(ide_drive_t *drive)
602 /* Send the actual packet */
603 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
605 /* Timeout for the packet command */
606 return WAIT_FLOPPY_CMD;
609 static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
611 struct ide_atapi_pc *pc;
612 ide_hwif_t *hwif = drive->hwif;
613 struct request *rq = hwif->rq;
614 ide_expiry_t *expiry;
615 unsigned int timeout;
617 ide_startstop_t startstop;
620 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
621 printk(KERN_ERR PFX "%s: Strange, packet command initiated yet "
622 "DRQ isn't asserted\n", drive->name);
626 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
628 drive->waiting_for_dma = 1;
631 if (dev_is_idecd(drive)) {
632 /* ATAPI commands get padded out to 12 bytes minimum */
633 cmd_len = COMMAND_SIZE(scsi_req(rq)->cmd[0]);
634 if (cmd_len < ATAPI_MIN_CDB_BYTES)
635 cmd_len = ATAPI_MIN_CDB_BYTES;
637 timeout = rq->timeout;
638 expiry = ide_cd_expiry;
642 cmd_len = ATAPI_MIN_CDB_BYTES;
645 * If necessary schedule the packet transfer to occur 'timeout'
646 * milliseconds later in ide_delayed_transfer_pc() after the
647 * device says it's ready for a packet.
649 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
650 timeout = drive->pc_delay;
651 expiry = &ide_delayed_transfer_pc;
653 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
658 ireason = ide_read_ireason(drive);
659 if (drive->media == ide_tape)
660 ireason = ide_wait_ireason(drive, ireason);
662 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
663 printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
664 "issuing a packet command\n", drive->name);
666 return ide_do_reset(drive);
670 hwif->expiry = expiry;
672 /* Set the interrupt routine */
673 ide_set_handler(drive,
674 (dev_is_idecd(drive) ? drive->irq_handler
678 /* Send the actual packet */
679 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
680 hwif->tp_ops->output_data(drive, NULL, scsi_req(rq)->cmd, cmd_len);
682 /* Begin DMA, if necessary */
683 if (dev_is_idecd(drive)) {
685 hwif->dma_ops->dma_start(drive);
687 if (pc->flags & PC_FLAG_DMA_OK) {
688 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
689 hwif->dma_ops->dma_start(drive);
696 ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
698 struct ide_atapi_pc *pc;
699 ide_hwif_t *hwif = drive->hwif;
700 ide_expiry_t *expiry = NULL;
701 struct request *rq = hwif->rq;
702 unsigned int timeout, bytes;
705 u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
707 if (dev_is_idecd(drive)) {
708 valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
709 bcount = ide_cd_get_xferlen(rq);
710 expiry = ide_cd_expiry;
711 timeout = ATAPI_WAIT_PC;
714 drive->dma = !ide_dma_prepare(drive, cmd);
718 valid_tf = IDE_VALID_DEVICE;
719 bytes = blk_rq_bytes(rq);
720 bcount = ((drive->media == ide_tape) ? bytes
721 : min_t(unsigned int,
724 /* We haven't transferred any data yet */
725 scsi_req(rq)->resid_len = bcount;
727 if (pc->flags & PC_FLAG_DMA_ERROR) {
728 pc->flags &= ~PC_FLAG_DMA_ERROR;
732 if (pc->flags & PC_FLAG_DMA_OK)
733 drive->dma = !ide_dma_prepare(drive, cmd);
736 pc->flags &= ~PC_FLAG_DMA_OK;
738 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
742 ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
744 (void)do_rw_taskfile(drive, cmd);
748 drive->waiting_for_dma = 0;
749 hwif->expiry = expiry;
752 ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
754 return drq_int ? ide_started : ide_transfer_pc(drive);
756 EXPORT_SYMBOL_GPL(ide_issue_pc);