Introduction to UFS Subsystem
Mohammad Faiz Abbas Rizvi
ELC 2019
[email protected] 1
About Me
• Software Engineer at Texas Instruments India since 2017.
• Part of the Linux Team that works on supporting various TI SoCs in mainline
kernel and u-boot.
• I work on supporting peripheral drivers on TI SoCs, mainly MMC, CAN and now
UFS.
• This presentation is a result of my experience adding the UFS subsystem to U-
boot.
2
What is Universal Flash Storage?
It is primarily a Managed Flash.
3
Managed Flash
• A non-volatile flash array combined with a
memory controller
Flash
• The controller manages the memory – bad
block management, ECC, wear leveling etc.
• The controller communicates with the outside Flash
Host
world with a defined communication protocol Communication controller
like MMC, Sata, SPI, Hyperflash etc. Protocol
Device
Generic Managed Flash
4
UFS Features
• It’s a high performance serial interface designed for
low power devices.
• Marketed as a replacement for SD card and eMMC
for smartphones, ultra portable PCs and other
embedded devices.
• Theoretical bidirectional full duplex transfer speeds
upto 1.45 GBps
• Can be a removable card or embedded on the board
Image courtesy: dpreview.com
5
How is UFS better?
Property eMMC 5.1 UFS
I/O speed Upto 400 MBps Upto 1.45 GBps
I/O Voltage 1.2V-3.3V 0.2V-0.4V differential
Random Read 13000 IOPS[1] 68000 IOPS[1]
Protocol Half duplex Full duplex
Theoretical Device Capacity 128 GB 16 TB
[1] androidcentral.com 6
UFS System Overview
Application
LU-0
UFS Application Layer SCSI Driver
Configuration
LU-N Structures
UFS Transport Layer UFS Driver
MIPI Unipro MIPI Unipro
UFS Interconnect Layer
MIPI M-PHY MIPI M-PHY
UFS Host UFS Device 7
UFS System Overview
Application
LU-0
UFS Application Layer SCSI Driver
Configuration
LU-N Structures
UFS Transport Layer UFS Driver
MIPI Unipro MIPI Unipro
UFS Interconnect Layer
MIPI M-PHY MIPI M-PHY
UFS Host UFS Device 8
UFS Application Layer
• Consists of the UFS SCSI command set based on SCSI Architecture Model (SAM)
• Transactions take place with fixed length Command Descriptor Blocks (CBD)
• Each Transaction follows the I_T_L_Q nexus meaning the CDB needs to identify an
Initiator communicating with a specific LUN in a Target with a specific Query.
Example 6 byte CDB format
9
UFS SCSI Commands
• A command descriptor block can be of 6, 10 or 16 bytes of length
• Some important commands :
– READ
– WRITE
– READ CAPACITY - get the size of a logical unit
– REPORT LUNS - get list of all logical units
– TEST UNIT READY - check if a logical unit is ready for accepting requests
– START STOP UNIT – switch Power mode of the device
– INQUIRY – More information about a logical unit
10
What is a Logical unit?
0x0
• Externally addressable Boot Logical unit 0
• Storage entity Logical unit 1
• Internal Task Queue
• LUs inside a UFS device can be configured in variety of ways: Logical unit 3
• Amount of physical memory allocated to each LU
• Write Protection Logical unit 4
• Boot
• Memory Type (default, system code, non-persistent, enhanced)
• Priority access Logical unit 7
• RPMB
Device Capacity
11
Logical unit (continued)
• A UFS device can have a maximum of 32 logical units in it.
• In addition, there might be 4 Well Known Logical Units (W-LUNS).
• REPORT LUNS
– Target for REPORT LUNS SCSI Request Well known logical unit LUN
– Returns number and configuration of all the LUNs on the device
• UFS Device REPORT LUNS 0x81
– Target for INQUIRY SCSI Request
– Configuration, Power Control, Formatting, flags, attributes UFS Device D0h
• Boot Boot B0h
– Used for boot operation
– Can be 1 or 2 boot partitions but only one active one RPMB C4h
• RPMB –
– Replay Protected memory block Well-known LUNS
– Need security protocol to access data
12
Application Layer Transaction View
Report LUNS
LU-0 /dev/sda
Response
LU-1 /dev/sdb
SCSI Driver
Read Capacity
LU-N /dev/sdX
Response
x Device
N
13
UFS System Overview
Application
LU-0
UFS Application Layer SCSI Driver
Configuration
LU-N Structures
UFS Transport Layer UFS Driver
MIPI Unipro MIPI Unipro
UFS Interconnect Layer
MIPI M-PHY MIPI M-PHY
UFS Host UFS Device 14
UFS Interconnect Layer • MIPI-PHY standard defines the physical layer
implementation.
• MIPI-Unipro standard defines the data link layer
implementation.
RST_N
HS-GEAR Maximum Datarate REF_CLK
UFS
HS-GEAR1 182 MBps
Controller
DIN_t/_c
Device
HS-GEAR2 364 MBps
DOUT_t/_c
HS-GEAR3 728 MBps
HS-GEAR4 1457 MBps
UFS data rates UFS signals
15
Simplified UFS Power Modes
ActiveICCLevel
0x0
Sleep
…
0x15
Active Idle
Each of these functions implements a
START STOP UNIT Command
Power
Down
16
UFS System Overview
Application
LU-0
UFS Application Layer SCSI Driver
Configuration
LU-N Structures
UFS Transport Layer UFS Driver
MIPI Unipro MIPI Unipro
UFS Interconnect Layer
MIPI M-PHY MIPI M-PHY
UFS Host UFS Device 17
UFS Transport Layer
• Transactions consist of packets called UFS Protocol Information Units (UPIU).
• The host is the Initiator and the device is the Target.
• There are different types of UPIUs for handling SCSI commands, data operations,
task management operations, query operations etc.
• Each transaction consists of:
– One COMMAND UPIU
– Zero or more DATA IN or DATA OUT UPIU
– RESPONSE UPIU
• Each UPIU contains a 12 bytes header followed by optional fields depending on the
type of UPIU.
UFS Transport Layer
UPIU HEADER
General UPIU Structure
19
Transaction Type
UPIU Structure
Flags
LUN
Initiator Target
Task Tag NOP OUT NOP IN
Initiator ID Command Response
Command Set Type Data Out Data In
Query/Task Manag. Fn Task Manag. Req Task Mang. Resp.
Response - Ready to Transfer
Status Query Request Query Response
Total length - Reject
Device Info Types of UPIUs
Data Length
UPIU Header
20
UTP Read Transaction
UAP Initiator Target
Command
UPIU
READ CDB
Data In
UPIU
Data In
UPIU
Response
UPIU
21
UTP Write Transaction
UAP Initiator Target
Command
UPIU
WRITE CDB
Ready to Transfer
UPIU
Data Out
UPIU
Ready to Transfer
UPIU
Data Out
UPIU
Response
UPIU 22
UTP Host Memory Configuration
UTP Transfer Command UPIU
Request
Descriptor Response UPIU
PRDT Data Buffer
UTP Transfer Command Descriptor
23
Transaction Type
UFS Host Memory Configuration
Flags 0
UTP Transfer Command UPIU
LUN Request
Descriptor Response UPIU
Task Tag 1
UTP Transfer PRDT Data Buffer
Initiator ID Request
Descriptor
Command Set Type Command UPIU
Query/Task Manag. Fn
. Response UPIU
Response . PRDT
.
Data Buffer
Status . .
Total length . Command UPIU
Device Info 31
UTP Transfer Response UPIU
Data Length Request
PRDT Data Buffer
Descriptor
UPIU Header
24
Transaction Type
UFS Task Management
0
Flags Task Management
Request UPIU
LUN
Task Management
Task Tag Response UPIU
Initiator ID 1
Task Management
Command Set Type Request UPIU
Query/Task Manag. Fn Task Management
Response UPIU
Response
Status
.
Total length
7
.
Task Management
Device Info
Request UPIU
Data Length Task Management
Response UPIU
UPIU Header
25
UTP Host Memory Configuration
0
Command UPIU
UTP Transfer Request
Descriptor
Response UPIU
1
PRDT Data Buffer
UTP Transfer Request
Descriptor
Command UPIU
0
Task Magement . Response UPIU
Request UPIU
.
PRDT Data Buffer
Task Magement .
Response UPIU
.
1
Task Magement
Request UPIU Command UPIU
31
Response UPIU
Task Magement UTP Transfer Request
Response UPIU Descriptor PRDT Data Buffer
.
.
7
Task Magement
Request UPIU
Task Magement
Response UPIU
26
UFS Host Controller Interface
• Host Capabilities Registers: Version, Vendor ID, 64 bit support, number of slots etc.
• Operation and Runtime Registers: Interrupt handling (including aggregator), error status at each layer,
host controller status etc.
• UTP Transfer Registers: Base addresses of UTPTRD table and UTPCMD table, doorbell registers,
complete notifier etc.
• Task Management Registers: Base address of UTPTM table, doorbell, complete notifiers etc.
• UIC Command Registers: Interconnect layer command register and arguments.
27
UFS System Overview
Application
LU-0
UFS Application Layer SCSI Driver
Configuration
LU-N Structures
UFS Transport Layer UFS Driver
MIPI Unipro MIPI Unipro
UFS Interconnect Layer
MIPI M-PHY MIPI M-PHY
UFS Host UFS Device 28
Transaction Type Query Request
Flags
LUN • Query request UPIU is used to access information about
Task Tag
configuration, enumeration, device descriptors, flags,
attributes etc.
Initiator ID Query Function Query Operation
OPCODE
Command Set Type Any value 00h NOP
Std. Read 01h Read
Query/Task Manag. Fn Descriptor
Std. Write 02h Write
Response Descriptor
Std. Read 03h Read
Status Attribute
Std. Write 04h Write
Total length Attribute
Std. Read 05h Read Flag
Device Info Std. Write 06h Set Flag
Std. Write 07h Clear Flag
Data Length
Std. Write 08h Toggle Flag
UPIU Header Query functions
29
UFS Descriptors, Flags and Attributes
Descriptor IDN Descriptor Type
UFS Descriptor
• Independently addressable data structure 00h Device
describing something about the device 01h Configuration
02h Unit
• Mostly read only except Configuration 03h -
Descriptor 04h Interconnect
05h String
Query Function Query Operation
OPCODE 06 -
Any value 00h NOP
07h Geometry
Std. Read 01h Read
Descriptor 08h Power
Std. Write 02h Write
09h Device Health
Descriptor
Query UPIU opcode Types of Descriptors
30
UFS Descriptors, Flags and Attributes
Flag IDN Name
UFS Flags
• A single Boolean value that can be set On or 01h fDeviceInit
Off. Can be set, cleared or toggled. 02h fPermanentWPEn
03h fPowerOnWPEn
04h fBackgroundOpsEn
05h fDeviceLifeSpanModeEn
Query Function Query Operation
OPCODE 06h fPurgeEnable
Std. Read 05h Read Flag
07h fRefreshEnable
Std. Write 06h Set Flag
08h fPhyResourceRemoval
Std. Write 07h Clear Flag
Std. Write 08h Toggle Flag 09h fBusyRTC
Query UPIU opcode 0Bh fPermanentlyDisableFwUpdate
Types of Descriptors
31
UFS Descriptors, Flags and Attributes
Attribute IDN Name
UFS Attributes
• A single parameter that represents a specific 00h bBootLunEn
range of numerical values that can be set or 02h bCurrentPowerMode
read. 03h bActiveICCLevel
04h bOutOfOrderDataEn
05h bBackgroundOpStatus
Query Function Query Operation 06h bPurgeStatus
OPCODE
07h bMaxDataInSize
Std. Read 03h Read
Attribute
08h bMaxDataOutSize
Std. Write 04h Write
Attribute 09h dDynCapNeeded
Query UPIU opcode 0Ah bRefClkFreq
Types of Descriptors
32
Kernel Implementation
• All the drivers can be found under drivers/scsi/ufs/
• Documentation/scsi/ufs.txt – for information about device enumeration steps
• Documentation/devicetree/bindings/ufs/ - for device tree node implementation
• ufshcd_pltrfm_init() : Call this from your probe()
33
Kernel Implementation
34
Configuring the device from user space
• UFS appears as a SCSI generic block device “/dev/bsg/ufs-bsg” that can accept
an ioctl system call.
• User space can allocate struct sg_io_v4 and send in the ioctl system call with
request code SG_IO.
int ioctl(int fd, SG_IO, struct sg_io_v4 *)
35
ufs-utils
https://github.com/westerndigitalcorporation/ufs-utils
ufs-utils < desc | attr | fl | err_hist | uic >
• Examples:
– Read Configuration descriptor
ufs-utils desc –t 1 –p /dev/bsg/ufs-bsg
– Set Background operations flag
ufs-utils fl –t 4 –e –p /dev/bsg/ufs-bsg
36
U-boot Implementation
• Available in 2020.01 release (got merged this Thursday!).
• drivers/ufs/ufs.c
• cmd/ufs.c
– Contains:
• basic command to initialize the ufs device, detect all LUNs and register them as scsi
devices.
– Does not contain:
• commands to access/configure descriptors/flags/attributes
Patches Welcome
37
References
• Universal Flash Storage 3.0
• UFS Host Controller Interface (UFSHCI) version 3.0
• https://git.kernel.org
• J721E Technical Reference Manual: http://www.ti.com/lit/ug/spruil1/spruil1.pdf
38
Thank You
39