1b64f6822SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0 2b64f6822SMauro Carvalho Chehab 3b64f6822SMauro Carvalho Chehab======================= 4b64f6822SMauro Carvalho ChehabUniversal Flash Storage 5b64f6822SMauro Carvalho Chehab======================= 6b64f6822SMauro Carvalho Chehab 7b64f6822SMauro Carvalho Chehab 8b64f6822SMauro Carvalho Chehab.. Contents 9b64f6822SMauro Carvalho Chehab 10b64f6822SMauro Carvalho Chehab 1. Overview 11b64f6822SMauro Carvalho Chehab 2. UFS Architecture Overview 12b64f6822SMauro Carvalho Chehab 2.1 Application Layer 13b64f6822SMauro Carvalho Chehab 2.2 UFS Transport Protocol (UTP) layer 14b64f6822SMauro Carvalho Chehab 2.3 UFS Interconnect (UIC) Layer 15b64f6822SMauro Carvalho Chehab 3. UFSHCD Overview 16b64f6822SMauro Carvalho Chehab 3.1 UFS controller initialization 17b64f6822SMauro Carvalho Chehab 3.2 UTP Transfer requests 18b64f6822SMauro Carvalho Chehab 3.3 UFS error handling 19b64f6822SMauro Carvalho Chehab 3.4 SCSI Error handling 20ca452621SDaniil Lunev 4. BSG Support 21ca452621SDaniil Lunev 5. UFS Reference Clock Frequency configuration 22b64f6822SMauro Carvalho Chehab 23b64f6822SMauro Carvalho Chehab 24b64f6822SMauro Carvalho Chehab1. Overview 25b64f6822SMauro Carvalho Chehab=========== 26b64f6822SMauro Carvalho Chehab 27b64f6822SMauro Carvalho ChehabUniversal Flash Storage (UFS) is a storage specification for flash devices. 28296559d4SRandy DunlapIt aims to provide a universal storage interface for both 29296559d4SRandy Dunlapembedded and removable flash memory-based storage in mobile 30b64f6822SMauro Carvalho Chehabdevices such as smart phones and tablet computers. The specification 31b64f6822SMauro Carvalho Chehabis defined by JEDEC Solid State Technology Association. UFS is based 32296559d4SRandy Dunlapon the MIPI M-PHY physical layer standard. UFS uses MIPI M-PHY as the 33b64f6822SMauro Carvalho Chehabphysical layer and MIPI Unipro as the link layer. 34b64f6822SMauro Carvalho Chehab 35296559d4SRandy DunlapThe main goals of UFS are to provide: 36b64f6822SMauro Carvalho Chehab 37b64f6822SMauro Carvalho Chehab * Optimized performance: 38b64f6822SMauro Carvalho Chehab 39b64f6822SMauro Carvalho Chehab For UFS version 1.0 and 1.1 the target performance is as follows: 40b64f6822SMauro Carvalho Chehab 41b64f6822SMauro Carvalho Chehab - Support for Gear1 is mandatory (rate A: 1248Mbps, rate B: 1457.6Mbps) 42b64f6822SMauro Carvalho Chehab - Support for Gear2 is optional (rate A: 2496Mbps, rate B: 2915.2Mbps) 43b64f6822SMauro Carvalho Chehab 44b64f6822SMauro Carvalho Chehab Future version of the standard, 45b64f6822SMauro Carvalho Chehab 46b64f6822SMauro Carvalho Chehab - Gear3 (rate A: 4992Mbps, rate B: 5830.4Mbps) 47b64f6822SMauro Carvalho Chehab 48b64f6822SMauro Carvalho Chehab * Low power consumption 49b64f6822SMauro Carvalho Chehab * High random IOPs and low latency 50b64f6822SMauro Carvalho Chehab 51b64f6822SMauro Carvalho Chehab 52b64f6822SMauro Carvalho Chehab2. UFS Architecture Overview 53b64f6822SMauro Carvalho Chehab============================ 54b64f6822SMauro Carvalho Chehab 55b64f6822SMauro Carvalho ChehabUFS has a layered communication architecture which is based on SCSI 56b64f6822SMauro Carvalho ChehabSAM-5 architectural model. 57b64f6822SMauro Carvalho Chehab 58296559d4SRandy DunlapUFS communication architecture consists of the following layers. 59b64f6822SMauro Carvalho Chehab 60b64f6822SMauro Carvalho Chehab2.1 Application Layer 61b64f6822SMauro Carvalho Chehab--------------------- 62b64f6822SMauro Carvalho Chehab 63296559d4SRandy Dunlap The Application layer is composed of the UFS command set layer (UCS), 64b64f6822SMauro Carvalho Chehab Task Manager and Device manager. The UFS interface is designed to be 65b64f6822SMauro Carvalho Chehab protocol agnostic, however SCSI has been selected as a baseline 66296559d4SRandy Dunlap protocol for versions 1.0 and 1.1 of the UFS protocol layer. 67b64f6822SMauro Carvalho Chehab 68296559d4SRandy Dunlap UFS supports a subset of SCSI commands defined by SPC-4 and SBC-3. 69b64f6822SMauro Carvalho Chehab 70b64f6822SMauro Carvalho Chehab * UCS: 71b64f6822SMauro Carvalho Chehab It handles SCSI commands supported by UFS specification. 72b64f6822SMauro Carvalho Chehab * Task manager: 73b64f6822SMauro Carvalho Chehab It handles task management functions defined by the 74b64f6822SMauro Carvalho Chehab UFS which are meant for command queue control. 75b64f6822SMauro Carvalho Chehab * Device manager: 76b64f6822SMauro Carvalho Chehab It handles device level operations and device 77b64f6822SMauro Carvalho Chehab configuration operations. Device level operations mainly involve 78b64f6822SMauro Carvalho Chehab device power management operations and commands to Interconnect 79b64f6822SMauro Carvalho Chehab layers. Device level configurations involve handling of query 80b64f6822SMauro Carvalho Chehab requests which are used to modify and retrieve configuration 81b64f6822SMauro Carvalho Chehab information of the device. 82b64f6822SMauro Carvalho Chehab 83b64f6822SMauro Carvalho Chehab2.2 UFS Transport Protocol (UTP) layer 84296559d4SRandy Dunlap-------------------------------------- 85b64f6822SMauro Carvalho Chehab 86296559d4SRandy Dunlap The UTP layer provides services for 87b64f6822SMauro Carvalho Chehab the higher layers through Service Access Points. UTP defines 3 88b64f6822SMauro Carvalho Chehab service access points for higher layers. 89b64f6822SMauro Carvalho Chehab 90b64f6822SMauro Carvalho Chehab * UDM_SAP: Device manager service access point is exposed to device 91b64f6822SMauro Carvalho Chehab manager for device level operations. These device level operations 92b64f6822SMauro Carvalho Chehab are done through query requests. 93b64f6822SMauro Carvalho Chehab * UTP_CMD_SAP: Command service access point is exposed to UFS command 94b64f6822SMauro Carvalho Chehab set layer (UCS) to transport commands. 95b64f6822SMauro Carvalho Chehab * UTP_TM_SAP: Task management service access point is exposed to task 96b64f6822SMauro Carvalho Chehab manager to transport task management functions. 97b64f6822SMauro Carvalho Chehab 98b64f6822SMauro Carvalho Chehab UTP transports messages through UFS protocol information unit (UPIU). 99b64f6822SMauro Carvalho Chehab 100b64f6822SMauro Carvalho Chehab2.3 UFS Interconnect (UIC) Layer 101296559d4SRandy Dunlap-------------------------------- 102b64f6822SMauro Carvalho Chehab 103296559d4SRandy Dunlap UIC is the lowest layer of the UFS layered architecture. It handles 104296559d4SRandy Dunlap the connection between UFS host and UFS device. UIC consists of 105b64f6822SMauro Carvalho Chehab MIPI UniPro and MIPI M-PHY. UIC provides 2 service access points 106296559d4SRandy Dunlap to upper layer: 107b64f6822SMauro Carvalho Chehab 108b64f6822SMauro Carvalho Chehab * UIC_SAP: To transport UPIU between UFS host and UFS device. 109b64f6822SMauro Carvalho Chehab * UIO_SAP: To issue commands to Unipro layers. 110b64f6822SMauro Carvalho Chehab 111b64f6822SMauro Carvalho Chehab 112b64f6822SMauro Carvalho Chehab3. UFSHCD Overview 113b64f6822SMauro Carvalho Chehab================== 114b64f6822SMauro Carvalho Chehab 115296559d4SRandy DunlapThe UFS host controller driver is based on the Linux SCSI Framework. 116296559d4SRandy DunlapUFSHCD is a low-level device driver which acts as an interface between 117296559d4SRandy Dunlapthe SCSI Midlayer and PCIe-based UFS host controllers. 118b64f6822SMauro Carvalho Chehab 119296559d4SRandy DunlapThe current UFSHCD implementation supports the following functionality: 120b64f6822SMauro Carvalho Chehab 121b64f6822SMauro Carvalho Chehab3.1 UFS controller initialization 122b64f6822SMauro Carvalho Chehab--------------------------------- 123b64f6822SMauro Carvalho Chehab 124296559d4SRandy Dunlap The initialization module brings the UFS host controller to active state 125296559d4SRandy Dunlap and prepares the controller to transfer commands/responses between 126b64f6822SMauro Carvalho Chehab UFSHCD and UFS device. 127b64f6822SMauro Carvalho Chehab 128b64f6822SMauro Carvalho Chehab3.2 UTP Transfer requests 129b64f6822SMauro Carvalho Chehab------------------------- 130b64f6822SMauro Carvalho Chehab 131b64f6822SMauro Carvalho Chehab Transfer request handling module of UFSHCD receives SCSI commands 132296559d4SRandy Dunlap from the SCSI Midlayer, forms UPIUs and issues the UPIUs to the UFS Host 133296559d4SRandy Dunlap controller. Also, the module decodes responses received from the UFS 134b64f6822SMauro Carvalho Chehab host controller in the form of UPIUs and intimates the SCSI Midlayer 135b64f6822SMauro Carvalho Chehab of the status of the command. 136b64f6822SMauro Carvalho Chehab 137b64f6822SMauro Carvalho Chehab3.3 UFS error handling 138b64f6822SMauro Carvalho Chehab---------------------- 139b64f6822SMauro Carvalho Chehab 140b64f6822SMauro Carvalho Chehab Error handling module handles Host controller fatal errors, 141296559d4SRandy Dunlap Device fatal errors and UIC interconnect layer-related errors. 142b64f6822SMauro Carvalho Chehab 143b64f6822SMauro Carvalho Chehab3.4 SCSI Error handling 144b64f6822SMauro Carvalho Chehab----------------------- 145b64f6822SMauro Carvalho Chehab 146b64f6822SMauro Carvalho Chehab This is done through UFSHCD SCSI error handling routines registered 147296559d4SRandy Dunlap with the SCSI Midlayer. Examples of some of the error handling commands 148296559d4SRandy Dunlap issues by the SCSI Midlayer are Abort task, LUN reset and host reset. 149b64f6822SMauro Carvalho Chehab UFSHCD Routines to perform these tasks are registered with 150b64f6822SMauro Carvalho Chehab SCSI Midlayer through .eh_abort_handler, .eh_device_reset_handler and 151b64f6822SMauro Carvalho Chehab .eh_host_reset_handler. 152b64f6822SMauro Carvalho Chehab 153296559d4SRandy DunlapIn this version of UFSHCD, Query requests and power management 154b64f6822SMauro Carvalho Chehabfunctionality are not implemented. 155b64f6822SMauro Carvalho Chehab 156b64f6822SMauro Carvalho Chehab4. BSG Support 157b64f6822SMauro Carvalho Chehab============== 158b64f6822SMauro Carvalho Chehab 159b64f6822SMauro Carvalho ChehabThis transport driver supports exchanging UFS protocol information units 160b64f6822SMauro Carvalho Chehab(UPIUs) with a UFS device. Typically, user space will allocate 161b64f6822SMauro Carvalho Chehabstruct ufs_bsg_request and struct ufs_bsg_reply (see ufs_bsg.h) as 162b64f6822SMauro Carvalho Chehabrequest_upiu and reply_upiu respectively. Filling those UPIUs should 163b64f6822SMauro Carvalho Chehabbe done in accordance with JEDEC spec UFS2.1 paragraph 10.7. 164b64f6822SMauro Carvalho Chehab*Caveat emptor*: The driver makes no further input validations and sends the 165b64f6822SMauro Carvalho ChehabUPIU to the device as it is. Open the bsg device in /dev/ufs-bsg and 166b64f6822SMauro Carvalho Chehabsend SG_IO with the applicable sg_io_v4:: 167b64f6822SMauro Carvalho Chehab 168b64f6822SMauro Carvalho Chehab io_hdr_v4.guard = 'Q'; 169b64f6822SMauro Carvalho Chehab io_hdr_v4.protocol = BSG_PROTOCOL_SCSI; 170b64f6822SMauro Carvalho Chehab io_hdr_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_TRANSPORT; 171b64f6822SMauro Carvalho Chehab io_hdr_v4.response = (__u64)reply_upiu; 172b64f6822SMauro Carvalho Chehab io_hdr_v4.max_response_len = reply_len; 173b64f6822SMauro Carvalho Chehab io_hdr_v4.request_len = request_len; 174b64f6822SMauro Carvalho Chehab io_hdr_v4.request = (__u64)request_upiu; 175b64f6822SMauro Carvalho Chehab if (dir == SG_DXFER_TO_DEV) { 176b64f6822SMauro Carvalho Chehab io_hdr_v4.dout_xfer_len = (uint32_t)byte_cnt; 177b64f6822SMauro Carvalho Chehab io_hdr_v4.dout_xferp = (uintptr_t)(__u64)buff; 178b64f6822SMauro Carvalho Chehab } else { 179b64f6822SMauro Carvalho Chehab io_hdr_v4.din_xfer_len = (uint32_t)byte_cnt; 180b64f6822SMauro Carvalho Chehab io_hdr_v4.din_xferp = (uintptr_t)(__u64)buff; 181b64f6822SMauro Carvalho Chehab } 182b64f6822SMauro Carvalho Chehab 183b64f6822SMauro Carvalho ChehabIf you wish to read or write a descriptor, use the appropriate xferp of 184b64f6822SMauro Carvalho Chehabsg_io_v4. 185b64f6822SMauro Carvalho Chehab 186b64f6822SMauro Carvalho ChehabThe userspace tool that interacts with the ufs-bsg endpoint and uses its 187296559d4SRandy DunlapUPIU-based protocol is available at: 188b64f6822SMauro Carvalho Chehab 189b64f6822SMauro Carvalho Chehab https://github.com/westerndigitalcorporation/ufs-tool 190b64f6822SMauro Carvalho Chehab 191b64f6822SMauro Carvalho ChehabFor more detailed information about the tool and its supported 192b64f6822SMauro Carvalho Chehabfeatures, please see the tool's README. 193b64f6822SMauro Carvalho Chehab 194296559d4SRandy DunlapUFS specifications can be found at: 195b64f6822SMauro Carvalho Chehab 196b64f6822SMauro Carvalho Chehab- UFS - http://www.jedec.org/sites/default/files/docs/JESD220.pdf 197b64f6822SMauro Carvalho Chehab- UFSHCI - http://www.jedec.org/sites/default/files/docs/JESD223.pdf 198ca452621SDaniil Lunev 199ca452621SDaniil Lunev5. UFS Reference Clock Frequency configuration 200ca452621SDaniil Lunev============================================== 201ca452621SDaniil Lunev 202ca452621SDaniil LunevDevicetree can define a clock named "ref_clk" under the UFS controller node 203ca452621SDaniil Lunevto specify the intended reference clock frequency for the UFS storage 204ca452621SDaniil Lunevparts. ACPI-based system can specify the frequency using ACPI 205ca452621SDaniil LunevDevice-Specific Data property named "ref-clk-freq". In both ways the value 206ca452621SDaniil Lunevis interpreted as frequency in Hz and must match one of the values given in 207ca452621SDaniil Lunevthe UFS specification. UFS subsystem will attempt to read the value when 208ca452621SDaniil Lunevexecuting common controller initialization. If the value is available, UFS 209*cf065a7dSRandy Dunlapsubsystem will ensure the bRefClkFreq attribute of the UFS storage device is 210ca452621SDaniil Lunevset accordingly and will modify it if there is a mismatch. 211