ATAPI
From OSDev Wiki
ATAPI refers to devices that use the Packet Interface of the ATA6 (or higher) standard command set. It is basically a way to issue SCSI commands to a CDROM, CD-RW, DVD, or tape drive, attached to the ATA bus.
ATAPI uses a very small number of ATA commands. The most important are the PACKET command (0xA0), and IDENTIFY PACKET DEVICE (0xA1).
Contents |
PACKET command (0xA0)
Each ATAPI command packet is made of a 'command byte' (from the SCSI command set -- see below for a partial list), followed by 11 'data' bytes. For instance, reading the table of contents is achieved by sending the following byte string to the device, as a "command".
unsigned char atapi_readtoc[]= { 0x43 /* ATAPI_READTOC */, 0, 1, 0, 0, 0, 0, 0, 12, 0x40, 0, 0};
The ATA PACKET command works in three phases, in PIO mode.
Phase 1) Set up the standard ATA IO port registers with ATAPI specific values. Then Send the ATA PACKET command to the device exactly as you would with any other ATA command: outb (ATA Command Register, 0xA0)
Phase 2) Prepare to do a PIO data transfer, the normal way, to the device. When the device is ready (BSY clear, DRQ set) you send the ATAPI command string (like the one above) as a 6 word PIO transfer to the device.
Phase 3) Wait for an IRQ. When it arrives, you must read the LBA Mid and LBA High IO port registers. They tell you the packet byte count that the ATAPI drive will send to you, or must receive from you. In a loop, you transmit that number of bytes, then wait for the next IRQ.
In DMA mode, only the first two phases happen. The device handles the details of phase 3 itself, with the PCI drive controller.
IDENTIFY PACKET DEVICE command (0xA1)
This command is a "normal" ATA PIO mode command, used during initialization. It is an exact mirror of the ATA IDENTIFY command, except that it only returns information about ATAPI devices. Use it in exactly the same way as you use IDENTIFY, including the meanings of all the bits in all the 256 words of data returned.
Disc Filesystem
A data CDROM stores data in an ISO 9660 filesystem. A DVD may store data in a UDF or RockRidge filesystem. An AudioCD (CD-DA) stores data in a Joliet filesystem. To support these devices, you need to support the filesystems in your VFS code. That is the hard part. Making the driver work is relatively easy.
x86 Directions
Important note: on the Primary bus, the standard set of ATA IO ports is 0x1F0 through 0x1F7. In much or all of the ATAPI documentation, you will see this set of IO ports called the "Task File". The term seems very confusing.
First, you need to have a buffer. If it is going to be a DMA buffer, it needs to follow the PRD rules (see the ATA/ATAPI using DMA article). If it is going to be a PIO buffer, then you need to know the size of the buffer. Call this size "maxByteCount". It must be an unsigned word, and 0 is illegal for PIO mode. A value of zero is mandatory for DMA mode, no matter what size the PRD buffers are.
Assume that the command is in words Command1 through Command6. Device is the Primary slave. Select the target device by setting the master/slave bit in the Device Select Register. There are no other bits needed.
outb (0x1F6, slavebit<<4)
If the command is going to use DMA, set the Features Register to 1, otherwise 0 for PIO.
outb (0x1F1, isDMA)
The Sectorcount Register and LBA Low Register are unused currently. Send maxByteCount in LBA Mid and LBA High Registers.
outb (0x1F4, (maxByteCount & 0xff))
outb (0x1F5, (maxByteCount >> 8))
Send the ATAPI PACKET command to the Command Register
outb (0x1F7, 0xA0)
Wait for an IRQ, or poll for BSY to clear and DRQ to set.
Then send the ATAPI command as 6 words, to the data port.
outw (0x1F0, Command1)
outw (0x1F0, Command2)
outw (0x1F0, Command3)
outw (0x1F0, Command4)
outw (0x1F0, Command5)
outw (0x1F0, Command6)
Then wait for another IRQ. You cannot poll.
If this was a DMA command (isDMA == 1), then you are done. When the IRQ arrives, the transfer is complete.
If it was a PIO command, when the IRQ arrives, read the LBA Mid and LBA High Registers. This is vital. You told the drive the maximum amount of data to transfer at one time. Now the drive has to tell you the actual transfer size.
Once you have the transfer size (bytecount = LBA High << 8 | LBA Mid), do the PIO transfer.
wordcount = bytecount/2;
loop on inw(0x1F0) or outw(0x1f0) wordcount times.
If the transfer is complete, BSY and DRQ will clear. Otherwise, wait for the next IRQ, and read or write the same number of words again.
Notes: there is a possible future change planned to increase the length of ATAPI command strings to 8 words. Check the two bottom bits of ATAPI Identify word 0 to verify 6 or 8 word command size.
Once again, if you use polling to check BSY, DRQ, and ERR after sending the PACKET command, then you should probably ignore the ERR bit for the first four loops. (ATAPI calls this the "CHECK" bit, instead of ERR, but it means the same thing.)
Partial Command Set
"SCSI" command name command byte value INQUIRY 0x12 LOAD/UNLOAD CD 0xA6 MECHANISM STATUS 0xBD MODE SELECT (10) 0x55 MODE SENSE (10) 0x5A PAUSE/RESUME 0x4B PLAY AUDIO (10) 0x45 PLAY AUDIO MSF 0x47 PLAY CD 0xBC PREVENT/ALLOW MEDIUM REMOVAL 0x1E READ (10) 0x28 READ (12) 0xA8 READ CD-ROM CAPACITY 0x25 READ CD 0xBE READ CD MSF 0xB9 READ HEADER 0x44 READ SUB-CHANNEL 0x42 READ TOC 0x43 REQUEST SENSE 0x03 SCAN 0xBA SEEK 0x2B SET CD SPEED 0xBB STOP PLAY / SCAN 0x4E START STOP UNIT 0x1B TEST UNIT READY 0
x86 Examples
(coming soon)
Operating Theory
Quoting The Guide to ATA/ATAPI documentation (stanford.edu)
Hosts control ATAPI devices using SCSI command packets. The SCSI command packets are transported over the ATA interface, instead of the parallel SCSI bus. This cool hack is described in ATA/ATAPI-6. The set of SCSI command packets applicable to all SCSI devices is described in the SCSI-3 Primary Commands PDF. Again, ATAPI devices don't implement all of these, so it's best to consult the associated ATAPI spec for a device. CD-ROM command packets were originally described in INF-8020 PDF. This document contains many inaccuracies in its description of the ATA bus interface, so please double check any statements against ATA/ATAPI-6. (Unfortunately, some of those inaccuracies were implemented!)
Comments
See Also
External Links
- http://www.t10.org/ -- T10, the group that creates the SCSI (and therefore ATAPI) command set.
- http://www.ata-atapi.com -- Public Domain C driver sourcecode, including SATA, Busmatering DMA, ATAPI -- not perfect, but good.
- -- The Guide to ATA/ATAPI documentation
- Beta release of original ATAPI spec (PDF) Has partial list of CD-ROM ATAPI commands. MANY errors. If something in this document looks wrong to you, it IS wrong. It is still very informative.
- http://www.osta.org/specs/pdf/udf201.pdf -- UDF filesystem format PDF
- http://www.osdever.net/downloads/docs/iso9660.zip -- ISO 9660 filesystem format
- http://bmrc.berkeley.edu/people/chaffee/jolspec.html -- Joliet filesystem specification
- http://www.osdever.net/downloads/docs/susp112.zip -- Rock Ridge Filesystem Sharing Protocol (POSIX)
- http://www.osdever.net/downloads/docs/rrip112.zip -- Rock Ridge Interchange specification (POSIX)
|
This page or section is a stub. Please help by expanding it |
Categories: Stubs | ATA | Storage
