xref: /OK3568_Linux_fs/kernel/Documentation/scsi/ufs.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=======================
4*4882a593SmuzhiyunUniversal Flash Storage
5*4882a593Smuzhiyun=======================
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun.. Contents
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun   1. Overview
11*4882a593Smuzhiyun   2. UFS Architecture Overview
12*4882a593Smuzhiyun     2.1 Application Layer
13*4882a593Smuzhiyun     2.2 UFS Transport Protocol(UTP) layer
14*4882a593Smuzhiyun     2.3 UFS Interconnect(UIC) Layer
15*4882a593Smuzhiyun   3. UFSHCD Overview
16*4882a593Smuzhiyun     3.1 UFS controller initialization
17*4882a593Smuzhiyun     3.2 UTP Transfer requests
18*4882a593Smuzhiyun     3.3 UFS error handling
19*4882a593Smuzhiyun     3.4 SCSI Error handling
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun1. Overview
23*4882a593Smuzhiyun===========
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunUniversal Flash Storage(UFS) is a storage specification for flash devices.
26*4882a593SmuzhiyunIt is aimed to provide a universal storage interface for both
27*4882a593Smuzhiyunembedded and removable flash memory based storage in mobile
28*4882a593Smuzhiyundevices such as smart phones and tablet computers. The specification
29*4882a593Smuzhiyunis defined by JEDEC Solid State Technology Association. UFS is based
30*4882a593Smuzhiyunon MIPI M-PHY physical layer standard. UFS uses MIPI M-PHY as the
31*4882a593Smuzhiyunphysical layer and MIPI Unipro as the link layer.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunThe main goals of UFS is to provide:
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun * Optimized performance:
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun   For UFS version 1.0 and 1.1 the target performance is as follows:
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun   - Support for Gear1 is mandatory (rate A: 1248Mbps, rate B: 1457.6Mbps)
40*4882a593Smuzhiyun   - Support for Gear2 is optional (rate A: 2496Mbps, rate B: 2915.2Mbps)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun   Future version of the standard,
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun   - Gear3 (rate A: 4992Mbps, rate B: 5830.4Mbps)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun * Low power consumption
47*4882a593Smuzhiyun * High random IOPs and low latency
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun2. UFS Architecture Overview
51*4882a593Smuzhiyun============================
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunUFS has a layered communication architecture which is based on SCSI
54*4882a593SmuzhiyunSAM-5 architectural model.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunUFS communication architecture consists of following layers,
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun2.1 Application Layer
59*4882a593Smuzhiyun---------------------
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun  The Application layer is composed of UFS command set layer(UCS),
62*4882a593Smuzhiyun  Task Manager and Device manager. The UFS interface is designed to be
63*4882a593Smuzhiyun  protocol agnostic, however SCSI has been selected as a baseline
64*4882a593Smuzhiyun  protocol for versions 1.0 and 1.1 of UFS protocol  layer.
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun  UFS supports subset of SCSI commands defined by SPC-4 and SBC-3.
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun  * UCS:
69*4882a593Smuzhiyun     It handles SCSI commands supported by UFS specification.
70*4882a593Smuzhiyun  * Task manager:
71*4882a593Smuzhiyun     It handles task management functions defined by the
72*4882a593Smuzhiyun     UFS which are meant for command queue control.
73*4882a593Smuzhiyun  * Device manager:
74*4882a593Smuzhiyun     It handles device level operations and device
75*4882a593Smuzhiyun     configuration operations. Device level operations mainly involve
76*4882a593Smuzhiyun     device power management operations and commands to Interconnect
77*4882a593Smuzhiyun     layers. Device level configurations involve handling of query
78*4882a593Smuzhiyun     requests which are used to modify and retrieve configuration
79*4882a593Smuzhiyun     information of the device.
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun2.2 UFS Transport Protocol(UTP) layer
82*4882a593Smuzhiyun-------------------------------------
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun  UTP layer provides services for
85*4882a593Smuzhiyun  the higher layers through Service Access Points. UTP defines 3
86*4882a593Smuzhiyun  service access points for higher layers.
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun  * UDM_SAP: Device manager service access point is exposed to device
89*4882a593Smuzhiyun    manager for device level operations. These device level operations
90*4882a593Smuzhiyun    are done through query requests.
91*4882a593Smuzhiyun  * UTP_CMD_SAP: Command service access point is exposed to UFS command
92*4882a593Smuzhiyun    set layer(UCS) to transport commands.
93*4882a593Smuzhiyun  * UTP_TM_SAP: Task management service access point is exposed to task
94*4882a593Smuzhiyun    manager to transport task management functions.
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun  UTP transports messages through UFS protocol information unit(UPIU).
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun2.3 UFS Interconnect(UIC) Layer
99*4882a593Smuzhiyun-------------------------------
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun  UIC is the lowest layer of UFS layered architecture. It handles
102*4882a593Smuzhiyun  connection between UFS host and UFS device. UIC consists of
103*4882a593Smuzhiyun  MIPI UniPro and MIPI M-PHY. UIC provides 2 service access points
104*4882a593Smuzhiyun  to upper layer,
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun  * UIC_SAP: To transport UPIU between UFS host and UFS device.
107*4882a593Smuzhiyun  * UIO_SAP: To issue commands to Unipro layers.
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun3. UFSHCD Overview
111*4882a593Smuzhiyun==================
112*4882a593Smuzhiyun
113*4882a593SmuzhiyunThe UFS host controller driver is based on Linux SCSI Framework.
114*4882a593SmuzhiyunUFSHCD is a low level device driver which acts as an interface between
115*4882a593SmuzhiyunSCSI Midlayer and PCIe based UFS host controllers.
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunThe current UFSHCD implementation supports following functionality,
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun3.1 UFS controller initialization
120*4882a593Smuzhiyun---------------------------------
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun  The initialization module brings UFS host controller to active state
123*4882a593Smuzhiyun  and prepares the controller to transfer commands/response between
124*4882a593Smuzhiyun  UFSHCD and UFS device.
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun3.2 UTP Transfer requests
127*4882a593Smuzhiyun-------------------------
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun  Transfer request handling module of UFSHCD receives SCSI commands
130*4882a593Smuzhiyun  from SCSI Midlayer, forms UPIUs and issues the UPIUs to UFS Host
131*4882a593Smuzhiyun  controller. Also, the module decodes, responses received from UFS
132*4882a593Smuzhiyun  host controller in the form of UPIUs and intimates the SCSI Midlayer
133*4882a593Smuzhiyun  of the status of the command.
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun3.3 UFS error handling
136*4882a593Smuzhiyun----------------------
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun  Error handling module handles Host controller fatal errors,
139*4882a593Smuzhiyun  Device fatal errors and UIC interconnect layer related errors.
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun3.4 SCSI Error handling
142*4882a593Smuzhiyun-----------------------
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun  This is done through UFSHCD SCSI error handling routines registered
145*4882a593Smuzhiyun  with SCSI Midlayer. Examples of some of the error handling commands
146*4882a593Smuzhiyun  issues by SCSI Midlayer are Abort task, Lun reset and host reset.
147*4882a593Smuzhiyun  UFSHCD Routines to perform these tasks are registered with
148*4882a593Smuzhiyun  SCSI Midlayer through .eh_abort_handler, .eh_device_reset_handler and
149*4882a593Smuzhiyun  .eh_host_reset_handler.
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunIn this version of UFSHCD Query requests and power management
152*4882a593Smuzhiyunfunctionality are not implemented.
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun4. BSG Support
155*4882a593Smuzhiyun==============
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunThis transport driver supports exchanging UFS protocol information units
158*4882a593Smuzhiyun(UPIUs) with a UFS device. Typically, user space will allocate
159*4882a593Smuzhiyunstruct ufs_bsg_request and struct ufs_bsg_reply (see ufs_bsg.h) as
160*4882a593Smuzhiyunrequest_upiu and reply_upiu respectively.  Filling those UPIUs should
161*4882a593Smuzhiyunbe done in accordance with JEDEC spec UFS2.1 paragraph 10.7.
162*4882a593Smuzhiyun*Caveat emptor*: The driver makes no further input validations and sends the
163*4882a593SmuzhiyunUPIU to the device as it is.  Open the bsg device in /dev/ufs-bsg and
164*4882a593Smuzhiyunsend SG_IO with the applicable sg_io_v4::
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun	io_hdr_v4.guard = 'Q';
167*4882a593Smuzhiyun	io_hdr_v4.protocol = BSG_PROTOCOL_SCSI;
168*4882a593Smuzhiyun	io_hdr_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_TRANSPORT;
169*4882a593Smuzhiyun	io_hdr_v4.response = (__u64)reply_upiu;
170*4882a593Smuzhiyun	io_hdr_v4.max_response_len = reply_len;
171*4882a593Smuzhiyun	io_hdr_v4.request_len = request_len;
172*4882a593Smuzhiyun	io_hdr_v4.request = (__u64)request_upiu;
173*4882a593Smuzhiyun	if (dir == SG_DXFER_TO_DEV) {
174*4882a593Smuzhiyun		io_hdr_v4.dout_xfer_len = (uint32_t)byte_cnt;
175*4882a593Smuzhiyun		io_hdr_v4.dout_xferp = (uintptr_t)(__u64)buff;
176*4882a593Smuzhiyun	} else {
177*4882a593Smuzhiyun		io_hdr_v4.din_xfer_len = (uint32_t)byte_cnt;
178*4882a593Smuzhiyun		io_hdr_v4.din_xferp = (uintptr_t)(__u64)buff;
179*4882a593Smuzhiyun	}
180*4882a593Smuzhiyun
181*4882a593SmuzhiyunIf you wish to read or write a descriptor, use the appropriate xferp of
182*4882a593Smuzhiyunsg_io_v4.
183*4882a593Smuzhiyun
184*4882a593SmuzhiyunThe userspace tool that interacts with the ufs-bsg endpoint and uses its
185*4882a593Smuzhiyunupiu-based protocol is available at:
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun	https://github.com/westerndigitalcorporation/ufs-tool
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunFor more detailed information about the tool and its supported
190*4882a593Smuzhiyunfeatures, please see the tool's README.
191*4882a593Smuzhiyun
192*4882a593SmuzhiyunUFS Specifications can be found at:
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun- UFS - http://www.jedec.org/sites/default/files/docs/JESD220.pdf
195*4882a593Smuzhiyun- UFSHCI - http://www.jedec.org/sites/default/files/docs/JESD223.pdf
196