2016年12月8日 星期四

About AT Attachment (ATA)

Advanced Technology Attachment,簡稱「ATA」
ATA是一個控制器技術,而IDE是一個符合它的磁碟機技術,但是兩個術語經常可以互用。
SATA(Serial ATA)於2002年推出後,原有的ATA改名為PATA(並列高技術配置,Parallel ATA)。
ATAPI (ATA Packet Interface)是一款ATA協定,允許使用ATA連接到硬碟以外的周邊裝置。

從架構面來看,ATA分為以下三層
ATA application Layer:
依照各個Command的不同,做出相對應的Flow。舉例來說,送出讀取SMART資訊的Command後,返回的資料該怎麼從Raw dataParse出對應的資訊。
ATA transport protocol layer:
維持Protocol的基本流程,除了基本Protocol外,上層比較會接觸到的Command Protocol基本上分為Non-data / PIO Data-In / PIO Data-Out  / DMA / DMA Queued / PACKET Command protocol,每一種都有自己的Flow。
ATA interconnect layer:
最接近物理層面的一層,會依據PATA/SATA而有相對應的處理。


所有的Command Set都是建構在Client-Server的觀念去完成的,硬碟是Server /  主機是Client

Command Protocol下的各種類型非常重要,所有的ATA Command後面一定會寫明該Command是哪一種類型,這代表著該Command送出後,Host與Device該採用什麼樣的方式來做溝通。

ATA protocol types are defined
  • Host Power-on protocol
  • Device Power-on protocol
  • Nexus Loss protocol
  • Device Management protocol
  • Command protocol
    • Non-data Command protocol
    • PIO Data-In Command protocol
    • PIO Data-Out Command protocol
    • DMA Command protocol 
    • DMA Queued Command protocol
    • PACKET Command protocol


以Non-data Command protocol來說,流程如下:
1.由Application Client Send Commandhost Transport protocol layer。
2.Device Transport protocol layer收到Command後,發送給Device server,然後Device server開始處理此Command。
3.Devicecommand處理完後,device server發出Send Command Function Complete responsedevice port。
4.Host port收到response後,送出Command Function Complete Received confirmationApplication Client



其它類型的Protocol因為基本上就是看圖說故事,就不在此描述了。下次再來介紹ATA中各種Field的定義及目的。

關於ATA Trim Command

Trim 指令讓OS能夠通知SSD哪個Block的data已經不被需要了,讓SSD內部可以對此Block進行清除。在固態硬碟出現後,Trim即開始被作業系統採用,其原因在於SSD的設計原理與傳統硬碟有極大的不同,如果用傳統操作方法,當作業系統對SSD執行刪除跟格式化操作時,會發生預料之外的效能衰退。 在啟用Trim後,SSD能夠使用更有效率的方法去做garbage collection,來加速未來的寫入操作的時間。

大部份SSD其內部的背景garbage collection機制完全獨立於Trim,就算在作業系統不支援Trim的情況下,SSD依然可以將本身的效能維持在良好的狀態。
Trim指令是不可逆的,沒有回復的可能性。

傳統Trim command定義無法被排入Queue裡,這使得它不容易跟read/write queue command混用,於是SATA 3.1協議針對這點推出了QUEUE TRIM COMMAND來補救。

TRIM is implemented under the DATA SET MANAGEMENT command (opcode 06h)

跟Trim相關支持的Bit如下:
Word 69 bit 14 Deterministic data in trimmed LBA range(s) is supported
Word 69 bit 5 Trimmed LBA range(s) returning zeroed data is supported
Word 169 bit 0 0 the TRIM bit in the DATA SET MANAGEMENT command is supported

TRIM SUPPORTED Bit
Device是否支援Trim command。
DRAT SUPPORTED Bit (Deterministic read after TRIM is supported)
此Bit為1時,每次Trim後Read LBA取得的資料都是固定的。
RZAT SUPPORTED Bit (Trimmed LBA range(s) returning zeroed data is supported)
此Bit為1時,每次Trim後Read LBA取得的資料都是固定的,且拿到的資料都為0。



TRIM SUPPORTEDDRAT SUPPORTEDRZAT SUPPORTEDDescription
0Should be 0Should be 0不支援Trim功能。
10Should be 0Device不保證每次Read Trimmed LBA會回一樣的數據。
110Device保證每次Read Trimmed LBA的到的結果都是固定的,但FW可以選擇要寫什麼值進去。(換句話說,只要你能夠保證資料不變,讓它維持在原先的值也是可以的。)
111Device保證每次Read Trimmed LBA的結果都是固定且為零。