1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Universal Flash Storage Host controller driver
4 * Copyright (C) 2011-2013 Samsung India Software Operations
5 *
6 * Authors:
7 * Santosh Yaraganavi <santosh.sy@samsung.com>
8 * Vinayak Holikatti <h.vinayak@samsung.com>
9 */
10
11 #ifndef _UFS_H
12 #define _UFS_H
13
14 #include <linux/mutex.h>
15 #include <linux/types.h>
16 #include <linux/android_kabi.h>
17 #include <linux/android_vendor.h>
18 #include <uapi/scsi/scsi_bsg_ufs.h>
19
20 #define GENERAL_UPIU_REQUEST_SIZE (sizeof(struct utp_upiu_req))
21 #define QUERY_DESC_MAX_SIZE 255
22 #define QUERY_DESC_MIN_SIZE 2
23 #define QUERY_DESC_HDR_SIZE 2
24 #define QUERY_OSF_SIZE (GENERAL_UPIU_REQUEST_SIZE - \
25 (sizeof(struct utp_upiu_header)))
26 #define UFS_SENSE_SIZE 18
27
28 #define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
29 cpu_to_be32((byte3 << 24) | (byte2 << 16) |\
30 (byte1 << 8) | (byte0))
31 /*
32 * UFS device may have standard LUs and LUN id could be from 0x00 to
33 * 0x7F. Standard LUs use "Peripheral Device Addressing Format".
34 * UFS device may also have the Well Known LUs (also referred as W-LU)
35 * which again could be from 0x00 to 0x7F. For W-LUs, device only use
36 * the "Extended Addressing Format" which means the W-LUNs would be
37 * from 0xc100 (SCSI_W_LUN_BASE) onwards.
38 * This means max. LUN number reported from UFS device could be 0xC17F.
39 */
40 #define UFS_UPIU_MAX_UNIT_NUM_ID 0x7F
41 #define UFS_MAX_LUNS (SCSI_W_LUN_BASE + UFS_UPIU_MAX_UNIT_NUM_ID)
42 #define UFS_UPIU_WLUN_ID (1 << 7)
43 #define UFS_RPMB_UNIT 0xC4
44
45 /* WriteBooster buffer is available only for the logical unit from 0 to 7 */
46 #define UFS_UPIU_MAX_WB_LUN_ID 8
47
48 /* Well known logical unit id in LUN field of UPIU */
49 enum {
50 UFS_UPIU_REPORT_LUNS_WLUN = 0x81,
51 UFS_UPIU_UFS_DEVICE_WLUN = 0xD0,
52 UFS_UPIU_BOOT_WLUN = 0xB0,
53 UFS_UPIU_RPMB_WLUN = 0xC4,
54 };
55
56 /*
57 * UFS Protocol Information Unit related definitions
58 */
59
60 /* Task management functions */
61 enum {
62 UFS_ABORT_TASK = 0x01,
63 UFS_ABORT_TASK_SET = 0x02,
64 UFS_CLEAR_TASK_SET = 0x04,
65 UFS_LOGICAL_RESET = 0x08,
66 UFS_QUERY_TASK = 0x80,
67 UFS_QUERY_TASK_SET = 0x81,
68 };
69
70 /* UTP UPIU Transaction Codes Initiator to Target */
71 enum {
72 UPIU_TRANSACTION_NOP_OUT = 0x00,
73 UPIU_TRANSACTION_COMMAND = 0x01,
74 UPIU_TRANSACTION_DATA_OUT = 0x02,
75 UPIU_TRANSACTION_TASK_REQ = 0x04,
76 UPIU_TRANSACTION_QUERY_REQ = 0x16,
77 };
78
79 /* UTP UPIU Transaction Codes Target to Initiator */
80 enum {
81 UPIU_TRANSACTION_NOP_IN = 0x20,
82 UPIU_TRANSACTION_RESPONSE = 0x21,
83 UPIU_TRANSACTION_DATA_IN = 0x22,
84 UPIU_TRANSACTION_TASK_RSP = 0x24,
85 UPIU_TRANSACTION_READY_XFER = 0x31,
86 UPIU_TRANSACTION_QUERY_RSP = 0x36,
87 UPIU_TRANSACTION_REJECT_UPIU = 0x3F,
88 };
89
90 /* UPIU Read/Write flags */
91 enum {
92 UPIU_CMD_FLAGS_NONE = 0x00,
93 UPIU_CMD_FLAGS_WRITE = 0x20,
94 UPIU_CMD_FLAGS_READ = 0x40,
95 };
96
97 /* UPIU Task Attributes */
98 enum {
99 UPIU_TASK_ATTR_SIMPLE = 0x00,
100 UPIU_TASK_ATTR_ORDERED = 0x01,
101 UPIU_TASK_ATTR_HEADQ = 0x02,
102 UPIU_TASK_ATTR_ACA = 0x03,
103 };
104
105 /* UPIU Query request function */
106 enum {
107 UPIU_QUERY_FUNC_STANDARD_READ_REQUEST = 0x01,
108 UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST = 0x81,
109 };
110
111 /* Flag idn for Query Requests*/
112 enum flag_idn {
113 QUERY_FLAG_IDN_FDEVICEINIT = 0x01,
114 QUERY_FLAG_IDN_PERMANENT_WPE = 0x02,
115 QUERY_FLAG_IDN_PWR_ON_WPE = 0x03,
116 QUERY_FLAG_IDN_BKOPS_EN = 0x04,
117 QUERY_FLAG_IDN_LIFE_SPAN_MODE_ENABLE = 0x05,
118 QUERY_FLAG_IDN_PURGE_ENABLE = 0x06,
119 QUERY_FLAG_IDN_RESERVED2 = 0x07,
120 QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL = 0x08,
121 QUERY_FLAG_IDN_BUSY_RTC = 0x09,
122 QUERY_FLAG_IDN_RESERVED3 = 0x0A,
123 QUERY_FLAG_IDN_PERMANENTLY_DISABLE_FW_UPDATE = 0x0B,
124 QUERY_FLAG_IDN_WB_EN = 0x0E,
125 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN = 0x0F,
126 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8 = 0x10,
127 QUERY_FLAG_IDN_HPB_RESET = 0x11,
128 QUERY_FLAG_IDN_HPB_EN = 0x12,
129 };
130
131 /* Attribute idn for Query requests */
132 enum attr_idn {
133 QUERY_ATTR_IDN_BOOT_LU_EN = 0x00,
134 QUERY_ATTR_IDN_MAX_HPB_SINGLE_CMD = 0x01,
135 QUERY_ATTR_IDN_POWER_MODE = 0x02,
136 QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03,
137 QUERY_ATTR_IDN_OOO_DATA_EN = 0x04,
138 QUERY_ATTR_IDN_BKOPS_STATUS = 0x05,
139 QUERY_ATTR_IDN_PURGE_STATUS = 0x06,
140 QUERY_ATTR_IDN_MAX_DATA_IN = 0x07,
141 QUERY_ATTR_IDN_MAX_DATA_OUT = 0x08,
142 QUERY_ATTR_IDN_DYN_CAP_NEEDED = 0x09,
143 QUERY_ATTR_IDN_REF_CLK_FREQ = 0x0A,
144 QUERY_ATTR_IDN_CONF_DESC_LOCK = 0x0B,
145 QUERY_ATTR_IDN_MAX_NUM_OF_RTT = 0x0C,
146 QUERY_ATTR_IDN_EE_CONTROL = 0x0D,
147 QUERY_ATTR_IDN_EE_STATUS = 0x0E,
148 QUERY_ATTR_IDN_SECONDS_PASSED = 0x0F,
149 QUERY_ATTR_IDN_CNTX_CONF = 0x10,
150 QUERY_ATTR_IDN_CORR_PRG_BLK_NUM = 0x11,
151 QUERY_ATTR_IDN_RESERVED2 = 0x12,
152 QUERY_ATTR_IDN_RESERVED3 = 0x13,
153 QUERY_ATTR_IDN_FFU_STATUS = 0x14,
154 QUERY_ATTR_IDN_PSA_STATE = 0x15,
155 QUERY_ATTR_IDN_PSA_DATA_SIZE = 0x16,
156 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME = 0x17,
157 QUERY_ATTR_IDN_CASE_ROUGH_TEMP = 0x18,
158 QUERY_ATTR_IDN_HIGH_TEMP_BOUND = 0x19,
159 QUERY_ATTR_IDN_LOW_TEMP_BOUND = 0x1A,
160 QUERY_ATTR_IDN_WB_FLUSH_STATUS = 0x1C,
161 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE = 0x1D,
162 QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST = 0x1E,
163 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE = 0x1F,
164 };
165
166 /* Descriptor idn for Query requests */
167 enum desc_idn {
168 QUERY_DESC_IDN_DEVICE = 0x0,
169 QUERY_DESC_IDN_CONFIGURATION = 0x1,
170 QUERY_DESC_IDN_UNIT = 0x2,
171 QUERY_DESC_IDN_RFU_0 = 0x3,
172 QUERY_DESC_IDN_INTERCONNECT = 0x4,
173 QUERY_DESC_IDN_STRING = 0x5,
174 QUERY_DESC_IDN_RFU_1 = 0x6,
175 QUERY_DESC_IDN_GEOMETRY = 0x7,
176 QUERY_DESC_IDN_POWER = 0x8,
177 QUERY_DESC_IDN_HEALTH = 0x9,
178 QUERY_DESC_IDN_MAX,
179 };
180
181 enum desc_header_offset {
182 QUERY_DESC_LENGTH_OFFSET = 0x00,
183 QUERY_DESC_DESC_TYPE_OFFSET = 0x01,
184 };
185
186 /* Unit descriptor parameters offsets in bytes*/
187 enum unit_desc_param {
188 UNIT_DESC_PARAM_LEN = 0x0,
189 UNIT_DESC_PARAM_TYPE = 0x1,
190 UNIT_DESC_PARAM_UNIT_INDEX = 0x2,
191 UNIT_DESC_PARAM_LU_ENABLE = 0x3,
192 UNIT_DESC_PARAM_BOOT_LUN_ID = 0x4,
193 UNIT_DESC_PARAM_LU_WR_PROTECT = 0x5,
194 UNIT_DESC_PARAM_LU_Q_DEPTH = 0x6,
195 UNIT_DESC_PARAM_PSA_SENSITIVE = 0x7,
196 UNIT_DESC_PARAM_MEM_TYPE = 0x8,
197 UNIT_DESC_PARAM_DATA_RELIABILITY = 0x9,
198 UNIT_DESC_PARAM_LOGICAL_BLK_SIZE = 0xA,
199 UNIT_DESC_PARAM_LOGICAL_BLK_COUNT = 0xB,
200 UNIT_DESC_PARAM_ERASE_BLK_SIZE = 0x13,
201 UNIT_DESC_PARAM_PROVISIONING_TYPE = 0x17,
202 UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT = 0x18,
203 UNIT_DESC_PARAM_CTX_CAPABILITIES = 0x20,
204 UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1 = 0x22,
205 UNIT_DESC_PARAM_HPB_LU_MAX_ACTIVE_RGNS = 0x23,
206 UNIT_DESC_PARAM_HPB_PIN_RGN_START_OFF = 0x25,
207 UNIT_DESC_PARAM_HPB_NUM_PIN_RGNS = 0x27,
208 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS = 0x29,
209 };
210
211 /* Device descriptor parameters offsets in bytes*/
212 enum device_desc_param {
213 DEVICE_DESC_PARAM_LEN = 0x0,
214 DEVICE_DESC_PARAM_TYPE = 0x1,
215 DEVICE_DESC_PARAM_DEVICE_TYPE = 0x2,
216 DEVICE_DESC_PARAM_DEVICE_CLASS = 0x3,
217 DEVICE_DESC_PARAM_DEVICE_SUB_CLASS = 0x4,
218 DEVICE_DESC_PARAM_PRTCL = 0x5,
219 DEVICE_DESC_PARAM_NUM_LU = 0x6,
220 DEVICE_DESC_PARAM_NUM_WLU = 0x7,
221 DEVICE_DESC_PARAM_BOOT_ENBL = 0x8,
222 DEVICE_DESC_PARAM_DESC_ACCSS_ENBL = 0x9,
223 DEVICE_DESC_PARAM_INIT_PWR_MODE = 0xA,
224 DEVICE_DESC_PARAM_HIGH_PR_LUN = 0xB,
225 DEVICE_DESC_PARAM_SEC_RMV_TYPE = 0xC,
226 DEVICE_DESC_PARAM_SEC_LU = 0xD,
227 DEVICE_DESC_PARAM_BKOP_TERM_LT = 0xE,
228 DEVICE_DESC_PARAM_ACTVE_ICC_LVL = 0xF,
229 DEVICE_DESC_PARAM_SPEC_VER = 0x10,
230 DEVICE_DESC_PARAM_MANF_DATE = 0x12,
231 DEVICE_DESC_PARAM_MANF_NAME = 0x14,
232 DEVICE_DESC_PARAM_PRDCT_NAME = 0x15,
233 DEVICE_DESC_PARAM_SN = 0x16,
234 DEVICE_DESC_PARAM_OEM_ID = 0x17,
235 DEVICE_DESC_PARAM_MANF_ID = 0x18,
236 DEVICE_DESC_PARAM_UD_OFFSET = 0x1A,
237 DEVICE_DESC_PARAM_UD_LEN = 0x1B,
238 DEVICE_DESC_PARAM_RTT_CAP = 0x1C,
239 DEVICE_DESC_PARAM_FRQ_RTC = 0x1D,
240 DEVICE_DESC_PARAM_UFS_FEAT = 0x1F,
241 DEVICE_DESC_PARAM_FFU_TMT = 0x20,
242 DEVICE_DESC_PARAM_Q_DPTH = 0x21,
243 DEVICE_DESC_PARAM_DEV_VER = 0x22,
244 DEVICE_DESC_PARAM_NUM_SEC_WPA = 0x24,
245 DEVICE_DESC_PARAM_PSA_MAX_DATA = 0x25,
246 DEVICE_DESC_PARAM_PSA_TMT = 0x29,
247 DEVICE_DESC_PARAM_PRDCT_REV = 0x2A,
248 DEVICE_DESC_PARAM_HPB_VER = 0x40,
249 DEVICE_DESC_PARAM_HPB_CONTROL = 0x42,
250 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP = 0x4F,
251 DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN = 0x53,
252 DEVICE_DESC_PARAM_WB_TYPE = 0x54,
253 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS = 0x55,
254 };
255
256 /* Interconnect descriptor parameters offsets in bytes*/
257 enum interconnect_desc_param {
258 INTERCONNECT_DESC_PARAM_LEN = 0x0,
259 INTERCONNECT_DESC_PARAM_TYPE = 0x1,
260 INTERCONNECT_DESC_PARAM_UNIPRO_VER = 0x2,
261 INTERCONNECT_DESC_PARAM_MPHY_VER = 0x4,
262 };
263
264 /* Geometry descriptor parameters offsets in bytes*/
265 enum geometry_desc_param {
266 GEOMETRY_DESC_PARAM_LEN = 0x0,
267 GEOMETRY_DESC_PARAM_TYPE = 0x1,
268 GEOMETRY_DESC_PARAM_DEV_CAP = 0x4,
269 GEOMETRY_DESC_PARAM_MAX_NUM_LUN = 0xC,
270 GEOMETRY_DESC_PARAM_SEG_SIZE = 0xD,
271 GEOMETRY_DESC_PARAM_ALLOC_UNIT_SIZE = 0x11,
272 GEOMETRY_DESC_PARAM_MIN_BLK_SIZE = 0x12,
273 GEOMETRY_DESC_PARAM_OPT_RD_BLK_SIZE = 0x13,
274 GEOMETRY_DESC_PARAM_OPT_WR_BLK_SIZE = 0x14,
275 GEOMETRY_DESC_PARAM_MAX_IN_BUF_SIZE = 0x15,
276 GEOMETRY_DESC_PARAM_MAX_OUT_BUF_SIZE = 0x16,
277 GEOMETRY_DESC_PARAM_RPMB_RW_SIZE = 0x17,
278 GEOMETRY_DESC_PARAM_DYN_CAP_RSRC_PLC = 0x18,
279 GEOMETRY_DESC_PARAM_DATA_ORDER = 0x19,
280 GEOMETRY_DESC_PARAM_MAX_NUM_CTX = 0x1A,
281 GEOMETRY_DESC_PARAM_TAG_UNIT_SIZE = 0x1B,
282 GEOMETRY_DESC_PARAM_TAG_RSRC_SIZE = 0x1C,
283 GEOMETRY_DESC_PARAM_SEC_RM_TYPES = 0x1D,
284 GEOMETRY_DESC_PARAM_MEM_TYPES = 0x1E,
285 GEOMETRY_DESC_PARAM_SCM_MAX_NUM_UNITS = 0x20,
286 GEOMETRY_DESC_PARAM_SCM_CAP_ADJ_FCTR = 0x24,
287 GEOMETRY_DESC_PARAM_NPM_MAX_NUM_UNITS = 0x26,
288 GEOMETRY_DESC_PARAM_NPM_CAP_ADJ_FCTR = 0x2A,
289 GEOMETRY_DESC_PARAM_ENM1_MAX_NUM_UNITS = 0x2C,
290 GEOMETRY_DESC_PARAM_ENM1_CAP_ADJ_FCTR = 0x30,
291 GEOMETRY_DESC_PARAM_ENM2_MAX_NUM_UNITS = 0x32,
292 GEOMETRY_DESC_PARAM_ENM2_CAP_ADJ_FCTR = 0x36,
293 GEOMETRY_DESC_PARAM_ENM3_MAX_NUM_UNITS = 0x38,
294 GEOMETRY_DESC_PARAM_ENM3_CAP_ADJ_FCTR = 0x3C,
295 GEOMETRY_DESC_PARAM_ENM4_MAX_NUM_UNITS = 0x3E,
296 GEOMETRY_DESC_PARAM_ENM4_CAP_ADJ_FCTR = 0x42,
297 GEOMETRY_DESC_PARAM_OPT_LOG_BLK_SIZE = 0x44,
298 GEOMETRY_DESC_PARAM_HPB_REGION_SIZE = 0x48,
299 GEOMETRY_DESC_PARAM_HPB_NUMBER_LU = 0x49,
300 GEOMETRY_DESC_PARAM_HPB_SUBREGION_SIZE = 0x4A,
301 GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGS = 0x4B,
302 GEOMETRY_DESC_PARAM_WB_MAX_ALLOC_UNITS = 0x4F,
303 GEOMETRY_DESC_PARAM_WB_MAX_WB_LUNS = 0x53,
304 GEOMETRY_DESC_PARAM_WB_BUFF_CAP_ADJ = 0x54,
305 GEOMETRY_DESC_PARAM_WB_SUP_RED_TYPE = 0x55,
306 GEOMETRY_DESC_PARAM_WB_SUP_WB_TYPE = 0x56,
307 };
308
309 /* Health descriptor parameters offsets in bytes*/
310 enum health_desc_param {
311 HEALTH_DESC_PARAM_LEN = 0x0,
312 HEALTH_DESC_PARAM_TYPE = 0x1,
313 HEALTH_DESC_PARAM_EOL_INFO = 0x2,
314 HEALTH_DESC_PARAM_LIFE_TIME_EST_A = 0x3,
315 HEALTH_DESC_PARAM_LIFE_TIME_EST_B = 0x4,
316 };
317
318 /* WriteBooster buffer mode */
319 enum {
320 WB_BUF_MODE_LU_DEDICATED = 0x0,
321 WB_BUF_MODE_SHARED = 0x1,
322 };
323
324 /*
325 * Logical Unit Write Protect
326 * 00h: LU not write protected
327 * 01h: LU write protected when fPowerOnWPEn =1
328 * 02h: LU permanently write protected when fPermanentWPEn =1
329 */
330 enum ufs_lu_wp_type {
331 UFS_LU_NO_WP = 0x00,
332 UFS_LU_POWER_ON_WP = 0x01,
333 UFS_LU_PERM_WP = 0x02,
334 };
335
336 /* bActiveICCLevel parameter current units */
337 enum {
338 UFSHCD_NANO_AMP = 0,
339 UFSHCD_MICRO_AMP = 1,
340 UFSHCD_MILI_AMP = 2,
341 UFSHCD_AMP = 3,
342 };
343
344 /* Possible values for dExtendedUFSFeaturesSupport */
345 enum {
346 UFS_DEV_LOW_TEMP_NOTIF = BIT(4),
347 UFS_DEV_HIGH_TEMP_NOTIF = BIT(5),
348 UFS_DEV_EXT_TEMP_NOTIF = BIT(6),
349 UFS_DEV_HPB_SUPPORT = BIT(7),
350 UFS_DEV_WRITE_BOOSTER_SUP = BIT(8),
351 };
352 #define UFS_DEV_HPB_SUPPORT_VERSION 0x310
353
354 #define POWER_DESC_MAX_ACTV_ICC_LVLS 16
355
356 /* Attribute bActiveICCLevel parameter bit masks definitions */
357 #define ATTR_ICC_LVL_UNIT_OFFSET 14
358 #define ATTR_ICC_LVL_UNIT_MASK (0x3 << ATTR_ICC_LVL_UNIT_OFFSET)
359 #define ATTR_ICC_LVL_VALUE_MASK 0x3FF
360
361 /* Power descriptor parameters offsets in bytes */
362 enum power_desc_param_offset {
363 PWR_DESC_LEN = 0x0,
364 PWR_DESC_TYPE = 0x1,
365 PWR_DESC_ACTIVE_LVLS_VCC_0 = 0x2,
366 PWR_DESC_ACTIVE_LVLS_VCCQ_0 = 0x22,
367 PWR_DESC_ACTIVE_LVLS_VCCQ2_0 = 0x42,
368 };
369
370 /* Exception event mask values */
371 enum {
372 MASK_EE_STATUS = 0xFFFF,
373 MASK_EE_DYNCAP_EVENT = BIT(0),
374 MASK_EE_SYSPOOL_EVENT = BIT(1),
375 MASK_EE_URGENT_BKOPS = BIT(2),
376 MASK_EE_TOO_HIGH_TEMP = BIT(3),
377 MASK_EE_TOO_LOW_TEMP = BIT(4),
378 MASK_EE_WRITEBOOSTER_EVENT = BIT(5),
379 MASK_EE_PERFORMANCE_THROTTLING = BIT(6),
380 };
381 #define MASK_EE_URGENT_TEMP (MASK_EE_TOO_HIGH_TEMP | MASK_EE_TOO_LOW_TEMP)
382
383 /* Background operation status */
384 enum bkops_status {
385 BKOPS_STATUS_NO_OP = 0x0,
386 BKOPS_STATUS_NON_CRITICAL = 0x1,
387 BKOPS_STATUS_PERF_IMPACT = 0x2,
388 BKOPS_STATUS_CRITICAL = 0x3,
389 BKOPS_STATUS_MAX = BKOPS_STATUS_CRITICAL,
390 };
391
392 /* UTP QUERY Transaction Specific Fields OpCode */
393 enum query_opcode {
394 UPIU_QUERY_OPCODE_NOP = 0x0,
395 UPIU_QUERY_OPCODE_READ_DESC = 0x1,
396 UPIU_QUERY_OPCODE_WRITE_DESC = 0x2,
397 UPIU_QUERY_OPCODE_READ_ATTR = 0x3,
398 UPIU_QUERY_OPCODE_WRITE_ATTR = 0x4,
399 UPIU_QUERY_OPCODE_READ_FLAG = 0x5,
400 UPIU_QUERY_OPCODE_SET_FLAG = 0x6,
401 UPIU_QUERY_OPCODE_CLEAR_FLAG = 0x7,
402 UPIU_QUERY_OPCODE_TOGGLE_FLAG = 0x8,
403 };
404
405 /* bRefClkFreq attribute values */
406 enum ufs_ref_clk_freq {
407 REF_CLK_FREQ_19_2_MHZ = 0,
408 REF_CLK_FREQ_26_MHZ = 1,
409 REF_CLK_FREQ_38_4_MHZ = 2,
410 REF_CLK_FREQ_52_MHZ = 3,
411 REF_CLK_FREQ_INVAL = -1,
412 };
413
414 struct ufs_ref_clk {
415 unsigned long freq_hz;
416 enum ufs_ref_clk_freq val;
417 };
418
419 /* Query response result code */
420 enum {
421 QUERY_RESULT_SUCCESS = 0x00,
422 QUERY_RESULT_NOT_READABLE = 0xF6,
423 QUERY_RESULT_NOT_WRITEABLE = 0xF7,
424 QUERY_RESULT_ALREADY_WRITTEN = 0xF8,
425 QUERY_RESULT_INVALID_LENGTH = 0xF9,
426 QUERY_RESULT_INVALID_VALUE = 0xFA,
427 QUERY_RESULT_INVALID_SELECTOR = 0xFB,
428 QUERY_RESULT_INVALID_INDEX = 0xFC,
429 QUERY_RESULT_INVALID_IDN = 0xFD,
430 QUERY_RESULT_INVALID_OPCODE = 0xFE,
431 QUERY_RESULT_GENERAL_FAILURE = 0xFF,
432 };
433
434 /* UTP Transfer Request Command Type (CT) */
435 enum {
436 UPIU_COMMAND_SET_TYPE_SCSI = 0x0,
437 UPIU_COMMAND_SET_TYPE_UFS = 0x1,
438 UPIU_COMMAND_SET_TYPE_QUERY = 0x2,
439 };
440
441 /* UTP Transfer Request Command Offset */
442 #define UPIU_COMMAND_TYPE_OFFSET 28
443
444 /* Offset of the response code in the UPIU header */
445 #define UPIU_RSP_CODE_OFFSET 8
446
447 enum {
448 MASK_SCSI_STATUS = 0xFF,
449 MASK_TASK_RESPONSE = 0xFF00,
450 MASK_RSP_UPIU_RESULT = 0xFFFF,
451 MASK_QUERY_DATA_SEG_LEN = 0xFFFF,
452 MASK_RSP_UPIU_DATA_SEG_LEN = 0xFFFF,
453 MASK_RSP_EXCEPTION_EVENT = 0x10000,
454 MASK_TM_SERVICE_RESP = 0xFF,
455 MASK_TM_FUNC = 0xFF,
456 };
457
458 /* Task management service response */
459 enum {
460 UPIU_TASK_MANAGEMENT_FUNC_COMPL = 0x00,
461 UPIU_TASK_MANAGEMENT_FUNC_NOT_SUPPORTED = 0x04,
462 UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED = 0x08,
463 UPIU_TASK_MANAGEMENT_FUNC_FAILED = 0x05,
464 UPIU_INCORRECT_LOGICAL_UNIT_NO = 0x09,
465 };
466
467 /* UFS device power modes */
468 enum ufs_dev_pwr_mode {
469 UFS_ACTIVE_PWR_MODE = 1,
470 UFS_SLEEP_PWR_MODE = 2,
471 UFS_POWERDOWN_PWR_MODE = 3,
472 UFS_DEEPSLEEP_PWR_MODE = 4,
473 };
474
475 #define UFS_WB_BUF_REMAIN_PERCENT(val) ((val) / 10)
476
477 /**
478 * struct utp_cmd_rsp - Response UPIU structure
479 * @residual_transfer_count: Residual transfer count DW-3
480 * @reserved: Reserved double words DW-4 to DW-7
481 * @sense_data_len: Sense data length DW-8 U16
482 * @sense_data: Sense data field DW-8 to DW-12
483 */
484 struct utp_cmd_rsp {
485 __be32 residual_transfer_count;
486 __be32 reserved[4];
487 __be16 sense_data_len;
488 u8 sense_data[UFS_SENSE_SIZE];
489 };
490
491 struct ufshpb_active_field {
492 __be16 active_rgn;
493 __be16 active_srgn;
494 };
495 #define HPB_ACT_FIELD_SIZE 4
496
497 /**
498 * struct utp_hpb_rsp - Response UPIU structure
499 * @residual_transfer_count: Residual transfer count DW-3
500 * @reserved1: Reserved double words DW-4 to DW-7
501 * @sense_data_len: Sense data length DW-8 U16
502 * @desc_type: Descriptor type of sense data
503 * @additional_len: Additional length of sense data
504 * @hpb_op: HPB operation type
505 * @lun: LUN of response UPIU
506 * @active_rgn_cnt: Active region count
507 * @inactive_rgn_cnt: Inactive region count
508 * @hpb_active_field: Recommended to read HPB region and subregion
509 * @hpb_inactive_field: To be inactivated HPB region and subregion
510 */
511 struct utp_hpb_rsp {
512 __be32 residual_transfer_count;
513 __be32 reserved1[4];
514 __be16 sense_data_len;
515 u8 desc_type;
516 u8 additional_len;
517 u8 hpb_op;
518 u8 lun;
519 u8 active_rgn_cnt;
520 u8 inactive_rgn_cnt;
521 struct ufshpb_active_field hpb_active_field[2];
522 __be16 hpb_inactive_field[2];
523 };
524 #define UTP_HPB_RSP_SIZE 40
525
526 /**
527 * struct utp_upiu_rsp - general upiu response structure
528 * @header: UPIU header structure DW-0 to DW-2
529 * @sr: fields structure for scsi command DW-3 to DW-12
530 * @qr: fields structure for query request DW-3 to DW-7
531 */
532 struct utp_upiu_rsp {
533 struct utp_upiu_header header;
534 union {
535 struct utp_cmd_rsp sr;
536 struct utp_hpb_rsp hr;
537 struct utp_upiu_query qr;
538 };
539 };
540
541 /**
542 * struct ufs_query_req - parameters for building a query request
543 * @query_func: UPIU header query function
544 * @upiu_req: the query request data
545 */
546 struct ufs_query_req {
547 u8 query_func;
548 struct utp_upiu_query upiu_req;
549 };
550
551 /**
552 * struct ufs_query_resp - UPIU QUERY
553 * @response: device response code
554 * @upiu_res: query response data
555 */
556 struct ufs_query_res {
557 u8 response;
558 struct utp_upiu_query upiu_res;
559 };
560
561 #define UFS_VREG_VCC_MIN_UV 2700000 /* uV */
562 #define UFS_VREG_VCC_MAX_UV 3600000 /* uV */
563 #define UFS_VREG_VCC_1P8_MIN_UV 1700000 /* uV */
564 #define UFS_VREG_VCC_1P8_MAX_UV 1950000 /* uV */
565 #define UFS_VREG_VCCQ_MIN_UV 1140000 /* uV */
566 #define UFS_VREG_VCCQ_MAX_UV 1260000 /* uV */
567 #define UFS_VREG_VCCQ2_MIN_UV 1700000 /* uV */
568 #define UFS_VREG_VCCQ2_MAX_UV 1950000 /* uV */
569
570 /*
571 * VCCQ & VCCQ2 current requirement when UFS device is in sleep state
572 * and link is in Hibern8 state.
573 */
574 #define UFS_VREG_LPM_LOAD_UA 1000 /* uA */
575
576 struct ufs_vreg {
577 struct regulator *reg;
578 const char *name;
579 bool always_on;
580 bool enabled;
581 int min_uV;
582 int max_uV;
583 int max_uA;
584 };
585
586 struct ufs_vreg_info {
587 struct ufs_vreg *vcc;
588 struct ufs_vreg *vccq;
589 struct ufs_vreg *vccq2;
590 struct ufs_vreg *vdd_hba;
591 };
592
593 struct ufs_dev_info {
594 bool f_power_on_wp_en;
595 /* Keeps information if any of the LU is power on write protected */
596 bool is_lu_power_on_wp;
597 /* Maximum number of general LU supported by the UFS device */
598 u8 max_lu_supported;
599 u16 wmanufacturerid;
600 /*UFS device Product Name */
601 u8 *model;
602 u16 wspecversion;
603 u32 clk_gating_wait_us;
604
605 /* UFS HPB related flag */
606 bool hpb_enabled;
607
608 /* UFS WB related flags */
609 bool wb_enabled;
610 bool wb_buf_flush_enabled;
611 u8 wb_dedicated_lu;
612 u8 wb_buffer_type;
613
614 bool b_rpm_dev_flush_capable;
615 u8 b_presrv_uspc_en;
616 ANDROID_KABI_RESERVE(1);
617
618 ANDROID_OEM_DATA(1);
619 };
620
621 /*
622 * This enum is used in string mapping in include/trace/events/ufs.h.
623 */
624 enum ufs_trace_str_t {
625 UFS_CMD_SEND, UFS_CMD_COMP, UFS_DEV_COMP,
626 UFS_QUERY_SEND, UFS_QUERY_COMP, UFS_QUERY_ERR,
627 UFS_TM_SEND, UFS_TM_COMP, UFS_TM_ERR
628 };
629
630 /*
631 * Transaction Specific Fields (TSF) type in the UPIU package, this enum is
632 * used in include/trace/events/ufs.h for UFS command trace.
633 */
634 enum ufs_trace_tsf_t {
635 UFS_TSF_CDB, UFS_TSF_OSF, UFS_TSF_TM_INPUT, UFS_TSF_TM_OUTPUT
636 };
637
638 /**
639 * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor
640 * @dev_info: pointer of instance of struct ufs_dev_info
641 * @lun: LU number to check
642 * @return: true if the lun has a matching unit descriptor, false otherwise
643 */
ufs_is_valid_unit_desc_lun(struct ufs_dev_info * dev_info,u8 lun,u8 param_offset)644 static inline bool ufs_is_valid_unit_desc_lun(struct ufs_dev_info *dev_info,
645 u8 lun, u8 param_offset)
646 {
647 if (!dev_info || !dev_info->max_lu_supported) {
648 pr_err("Max General LU supported by UFS isn't initialized\n");
649 return false;
650 }
651 /* WB is available only for the logical unit from 0 to 7 */
652 if (param_offset == UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS)
653 return lun < UFS_UPIU_MAX_WB_LUN_ID;
654 return lun == UFS_UPIU_RPMB_WLUN || (lun < dev_info->max_lu_supported);
655 }
656
657 #endif /* End of Header */
658