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