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