Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1247377
  • 博文数量: 122
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4004
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-20 08:27
文章分类
文章存档

2016年(1)

2015年(21)

2014年(100)

分类: LINUX

2014-08-08 14:46:12

原文地址:atapi programming 作者:soararing

这几天研究了下ATAPI的协议,到现在为止,基本上知道了是怎么回事。 
     ATA也就是IDE,是用来连接硬盘的。接口是29根或者44根,应该还有其它类型的,只是我不知道而已!其实,44根线就是多出了一些的地线,以使信号稳定。最基本的无非就是16根数据线(DD0~DD15),5根地址线(CS0,CS1,AD0~AD2),一根“读”状态信号线,一根“写”状态信号线,一根中断信号线(IRQ)。其中数据的传输过程都被主机(host)用寄存器进行了封装,我们只需要操作主机的寄存器即可!--可不可以依据读写时序,不依赖寄存器把通讯做成一个纯软件的东西?我没有试过,理论上可行的,只是数据传输会很慢。
    通讯的过程是这样的。根据协议,设备(device)提供了9个command block 寄存器。分别是:DATA,Features,sector count, sector number,Cylinder low,cylinder high,device/head,status,command。除了command和status外,其它寄存器用来为command提供参数。主机也会提供相应的寄存器,主要就是addres和data,其它还有一些控制,反馈状态的积存器。一次完整的读过程大致是这样的:先向主机address积存器里填值,选种device的积存器来进行一些设置。然后,分别向sector count,sector number,cylinder low,cylinder high里输入参数,最后,选种command积存器,输入读写命令,device会产生一个中断先,统治它已经准备要传输数据,然后,主机反复读data积存器就可以将数据取去。关于通讯命令的资料,可以在“下载”一栏里面下载。
     上面说的只是PIO传输,还有DMA传输,已经的ATA协议好象不支持。DMA就是,host上面有一个控制器,它来控制总线,从device传过来的数据,直接存放到内存,而不经过主机的CPU。--到现在我还不知道,RAM的首地址如何设置?通讯过程跟PIO也差不多,通过命令设置好device的参数后,写主机的寄存器启动DMA即可!
      以上是ATA,下面讲ATAPI。
      ATAPI是在ATA上的扩展的协议,增加了一种叫做packet的命令。用来连接光驱。其实也没什么,packet类的命令,无非就是把一些常规的操作,打包成了一个命令使操作简单些了,也没什么。可能在ATA里要传好几个命令才完成的工作,在packet类的命令里,一个就搞掂了。举个例子:一个对device的操作来完成一个功能,在ATA协议里面能需要两个步骤,即先后要发命令A和B。在ATAPI协议里面就把这个功能打包成一个命令C,你只要发一个命令C,device就会自动执行以前A+B的动作。有点类似,复杂指令和精简指令的区别。
      关于 ATAPI的overlap功能,我的理解是:当一个主机操作两个设备的时候,做读写某一设备时,可以使该设备释放总线,去操作另一个设备。没有这个功能之前,如果要操作另一个设备的话,有一些寄存器要修改。
      这只是我的一些看法,不一定对。
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kartorz/archive/2008/10/29/3176223.aspx
 
 
SATA System software rules:
1. initialize the HBA
   a.reset HBA controller
    1).set global host control(GHC) bit0(HBA Reset, HR)
    2).enable AHCI by setting GHC bit31(AHCI Enable, AE)
   b.reset SATA port
    1).clear two bits (FIS Receive Enable & Command Start)
    2).detect device presence by testing whether SStatus(offset 28h) bit(3:0)(Device Detection, DET) is 3h or not
   c.check if device is connected by testing whether SStatus(offset 28h) bit(3:0) and bit(11:08)(Interface power Management, IPM)
   d.turn on IRQs by setting PxIE(offset 14h) register.
   e.set PxCMD(offset 18h) register bit4(FIS Receive Enable, FRE) for receiving FIS to put them into the FIS receive area(shall not must).
   f.set PxCMD(offset 18h) register bit0(Start, ST) to 1 for preparing process the cmd list
2. Build command
   a. build cmd FIS (H2D FIS)
   b. build ACMD (if has ATAPI command)
   c. build command header
3. command to be issued by turning on the command slot bit in PxCI(offset 38h) register.
4. waiting for the D2H FIS received, just verify whether the PxIS(offset 10h) register bit0 is 1 or not.
5. get data from the PRD
----------------------------------
Software related is very simple. For most work has been done by the HBA. Such as (ATAPI Packet commands with DMA data in):
1. Software isssued cmd by writting the shadow register
2. HBA sets BSY in Shadow register and transmit a H2D FIS to device.
3. If has ACMD(namely it is ATAPI command packet), Device transmits a PIO setup D2H FIS to the HBA
4. HBA receive PIO setup D2H FIS then transfer the contents into the Shadow register(BSY->0, DRQ->1)
5. HBA writes the command packet to the Shadow register Block data register.
6. HBA transmits the data to the device as a Data-H2D FIS (DRQ->0, BSY->1), completing the transmission of the command packet to the device.
7. Device processes the command,then transmits the read data to the HBA as a single Data FIS
8. HBA and DMA controller forwards the incoming data to the appropriate host memory locations.
9. Upon completion of the transfer, device transmits a Register-D2H FIS to indicating ending status for the command(BSY->0)
-------------------------------------------------
Port Multiplier discovery:
1. HBA determine if communication is established on HBA port by checking the SStatus
register.
2. If device is present, HBA issues a software reset with the PM port field set to the
control port(0Fh).
3. PM recieves a software reset to the control port, then the PM shall issue a Register
FIS to the HBA
3. HBA checks the signature value returned and if it corresponds to the PM Signature,
HBA knows that a PM is present.
4. If the signature value does not correspond to a PM, HBA may proceed with the normal
initialization sequencce for that device type.
-----------------------
Native Hot Plug Support:
1. Hot plug removal is detected by a change in the state of the HBA’s internal PhyRdy
signal.
2. When the communication negotiation sequence is complete and PhyRdy is true the
PxSSTS.DET field shall be set to 3h.
3. Hot plug removals are detected via the PxIS.PRCS bit that directly reflects the
PxSERR.DIAG.N bit.
4. Note that PxSERR.DIAG.N is also set to ‘1’ on insertions and during interface power
management entry/exit.
5. When PhyRdy transitions from ‘1’ to ‘0’, the HBA may update PxSSTS.DET to 1h since
the reason for the loss of communications is not fully known.
 
 

 SATA system software rules
 
用I/O命令访问PCI总线设备配置空间

SATA programming : Software

PCI bus info and code
to
Hello World - ATAPI Music CD Programming Interface in Linux

An Introduction To Programming With ATA And ATAPI
 
 ATAPI programming
 
The serialized interface uses the command set from the
ATA/ATAPI-6 standard, augmented with Native Command Queuing commands optimized for the
serialized interface. The serialized ATA interface is defined in a register-compatible manner with
parallel ATA to enable backward compatibility with parallel ATA drivers.

Task File
Interface registers used for delivering commands to the device or posting status from the device. In
Serial ATA, these registers are communicated as part of FIS payload fields. In later revisions of the ATA
specification, these registers may be referred to as the Command and Control Block registers.
Communication between a device and software moves from the task file via byte-wide accesses to a
command FIS located in system memory that is fetched by the HBA. This reduces command setup time
significantly, allowing for many more devices to be added to a single host controller. Software no longer
communicates directly to a device via the task file.

frame
A frame is an indivisible unit of information exchanged between a host and device. A frame
consists of an SOFP primitive, a Frame Information Structure, a CRC calculated over the contents
of the FIS, and an EOFP primitive.
 
FIS (Frame Information Structure)
The user payload of a frame, does not include the SOFP, CRC, and EOFP delimiters.

AHCI is a PCI class device that acts as a data movement engine between system memory and Serial
ATA devices.
AHCI host devices (referred to as host bus adapters, or HBA) support from 1 to 32 ports. An HBA must
support ATA and ATAPI devices, and must support both the PIO and DMA protocols.
 AHCI

HBA (Host Bus Adapter)
HBA is a component that connects to the host system’s expansion bus to provide connectivity for
devices. HBAs are also often referred to as controller cards or merely controllers.

SATA Architecture
There are four layers in the Serial ATA architecture: Application, Transport, Link, and Phy. The
Applicaton layer is responsible for overall ATA command execution, including controlling
Command Block Register accesses. The Transport layer is responsible for placing control
information and data to be transferred between the host and device in a packet/frame, known as
a Frame Information Structure (FIS). The Link layer is responsible for taking data from the
constructed frames, encoding or decoding each byte using 8b/10b, and inserting control
characters such that the 10-bit stream of data may be decoded correctly. The Phy layer is
responsible for transmitting and receiving the encoded information as a serial data stream on the
wire. 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liluvu/archive/2009/09/19/4568649.aspx
 
 
 
 
 

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.)

Complete Command Set

SCSI Command Name Command Byte (OpCode)
TEST UNIT READY 0x00
REQUEST SENSE 0x03
FORMAT UNIT 0x04
INQUIRY 0x12
START STOP UNIT 0x1B
PREVENT ALLOW MEDIUM REMOVAL 0x1E
READ FORMAT CAPACITIES 0x23
READ CAPACITY 0x25
READ (10) 0x28
WRITE (10) 0x2A
SEEK (10) 0x2B
WRITE AND VERIFY (10) 0x2E
VERIFY (10) 0x2F
SYNCHRONIZE CACHE 0x35
WRITE BUFFER 0x3B
READ BUFFER 0x3C
READ TOC/PMA/ATIP 0x43
GET CONFIGURATION 0x46
GET EVENT STATUS NOTIFICATION 0x4A
READ DISC INFORMATION 0x51
READ TRACK INFORMATION 0x52
RESERVE TRACK 0x53
SEND OPC INFORMATION 0x54
MODE SELECT (10) 0x55
REPAIR TRACK 0x58
MODE SENSE (10) 0x5A
CLOSE TRACK SESSION 0x5B
READ BUFFER CAPACITY 0x5C
SEND CUE SHEET 0x5D
REPORT LUNS 0xA0
BLANK 0xA1
SECURITY PROTOCOL IN 0xA2
SEND KEY 0xA3
REPORT KEY 0xA4
LOAD/UNLOAD MEDIUM 0xA6
SET READ AHEAD 0xA7
READ (12) 0xA8
WRITE (12) 0xAA
READ MEDIA SERIAL NUMBER / SERVICE ACTION IN (12) 0xAB / 0x01
GET PERFORMANCE 0xAC
READ DISC STRUCTURE 0xAD
SECURITY PROTOCOL OUT 0xB5
SET STREAMING 0xB6
READ CD MSF 0xB9
SET CD SPEED 0xBB
MECHANISM STATUS 0xBD
READ CD 0xBE
SEND DISC STRUCTURE 0xBF

x86 Examples

Here is an example adapted from a working driver implementation.

  /* The default and seemingly universal sector size for CD-ROMs. */
  #define ATAPI_SECTOR_SIZE 2048
 
  /* The default ISA IRQ numbers of the ATA controllers. */
  #define ATA_IRQ_PRIMARY     0x0E
  #define ATA_IRQ_SECONDARY   0x0F
 
  /* The necessary I/O ports, indexed by "bus". */
  #define ATA_DATA(x)         (x)
  #define ATA_FEATURES(x)     (x+1)
  #define ATA_SECTOR_COUNT(x) (x+2)
  #define ATA_ADDRESS1(x)     (x+3)
  #define ATA_ADDRESS2(x)     (x+4)
  #define ATA_ADDRESS3(x)     (x+5)
  #define ATA_DRIVE_SELECT(x) (x+6)
  #define ATA_COMMAND(x)      (x+7)
  #define ATA_DCR(x)          (x+0x206)   /* device control register */
 
  /* valid values for "bus" */
  #define ATA_BUS_PRIMARY     0x1F0
  #define ATA_BUS_SECONDARY   0x170
  /* valid values for "drive" */
  #define ATA_DRIVE_MASTER    0xA0
  #define ATA_DRIVE_SLAVE     0xB0
 
  /* ATA specifies a 400ns delay after drive switching -- often
   * implemented as 4 Alternative Status queries. */
  #define ATA_SELECT_DELAY(bus) \
    {inb(ATA_DCR(bus));inb(ATA_DCR(bus));inb(ATA_DCR(bus));inb(ATA_DCR(bus));}
 
  /* Use the ATAPI protocol to read a single sector from the given
   * bus/drive into the buffer using logical block address lba. */
  int
  atapi_drive_read_sector (uint32 bus, uint32 drive, uint32 lba, uint8 *buffer)
  {
    /* 0xA8 is READ SECTORS command byte. */
    uint8 read_cmd[12] = { 0xA8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    uint8 status;
    int size;
    /* Tell the scheduler that this process is using the ATA subsystem. */
    ata_grab ();
    /* Select drive (only the slave-bit is set) */
    outb (drive & (1 << 4), ATA_DRIVE_SELECT (bus));      
    ATA_SELECT_DELAY (bus);       /* 400ns delay */
    outb (0x0, ATA_FEATURES (bus));       /* PIO mode */
    outb (ATAPI_SECTOR_SIZE & 0xFF, ATA_ADDRESS2 (bus));
    outb (ATAPI_SECTOR_SIZE >> 8, ATA_ADDRESS3 (bus));
    outb (0xA0, ATA_COMMAND (bus));       /* ATA PACKET command */
    while ((status = inb (ATA_COMMAND (bus))) & 0x80)     /* BUSY */
      asm volatile ("pause");
    while (!((status = inb (ATA_COMMAND (bus))) & 0x8) && !(status & 0x1))
      asm volatile ("pause");
    /* DRQ or ERROR set */
    if (status & 0x1) {
      size = -1;
      goto cleanup;
    }
    read_cmd[9] = 1;              /* 1 sector */
    read_cmd[2] = (lba >> 0x18) & 0xFF;   /* most sig. byte of LBA */
    read_cmd[3] = (lba >> 0x10) & 0xFF;
    read_cmd[4] = (lba >> 0x08) & 0xFF;
    read_cmd[5] = (lba >> 0x00) & 0xFF;   /* least sig. byte of LBA */
    /* Send ATAPI/SCSI command */
    outsw (ATA_DATA (bus), (uint16 *) read_cmd, 6);
    /* Wait for IRQ that says the data is ready. */
    schedule ();
    /* Read actual size */
    size =
      (((int) inb (ATA_ADDRESS3 (bus))) << 8) |
      (int) (inb (ATA_ADDRESS2 (bus)));
    /* This example code only supports the case where the data transfer
     * of one sector is done in one step. */
    ASSERT (size == ATAPI_SECTOR_SIZE);
    /* Read data. */
    insw (ATA_DATA (bus), buffer, size / 2);
    /* The controller will send another IRQ even though we've read all
     * the data we want.  Wait for it -- so it doesn't interfere with
     * subsequent operations: */
    schedule ();
    /* Wait for BSY and DRQ to clear, indicating Command Finished */
    while ((status = inb (ATA_COMMAND (bus))) & 0x88) 
      asm volatile ("pause");
   cleanup:
    /* Exit the ATA subsystem */
    ata_release ();
    return size;
  }

Detecting a Medium's Capacity

A medium is any media inserted in the ATAPI Drive, like a CD or a DVD. By using the 'SCSI Read Capacity' command, you can read the last LBA of the medium, then you calculate the medium's capacity using this relationship:

Capacity = (Last LBA + 1) * Block Size;

Last LBA and Block Size are returned after processing the command. Almost all CDs and DVDs use blocks with size of 2KB each.

Processing this command goes in the following algorithm:

  • Selecting the Drive [Master/Slave].
  • Waiting 400ns for select to complete.
  • Setting FEATURES Register to 0 [PIO Mode].
  • Setting LBA1 and LBA2 Registers to 0x0008 [Number of Bytes will be returned].
  • Sending Packet Command, then Polling.
  • Sending the ATAPI Packet, then polling.
  • If there isn't an error, reading 4 Words [8 bytes] from the DATA Register.

The ATAPI packet should be in this formulation:


bit→
↓byte
7 6 5 4 3 2 1 0
0 Operation code = 25h
1 LUN Reserved RelAdr
2 LBA (MSB)



LBA (LSB)
3
4
5
6 Reserved
7 Reserved
8 Reserved PMI
9 Control

The special control fields in the CDB have the following meaning:

  • RelAdr - indicates that the logical block address (LBA) value is relative (only used with linked commands).
  • PMI - partial medium indicator:
    • 0 - return value for the last LBA
    • 1 - return value for the last LBA after which a substantial delay in data transfer will be encountered (e.g., the current track or cylinder)

The target will return capacity data structured as follows:

bit→
↓byte
7 6 5 4 3 2 1 0
0–3 Returned LBA
4–7 Block length in bytes

If there is an error after polling, there may be no medium inserted, so this command may be also used to detect whether there is a medium or not, and if there is a medium, its capacity is read.

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 . 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 . 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

Articles

External Links

阅读(2842) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~