]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/ufs/ufs.h
Merge remote-tracking branch 'scsi/for-next'
[karo-tx-linux.git] / drivers / scsi / ufs / ufs.h
1 /*
2  * Universal Flash Storage Host controller driver
3  *
4  * This code is based on drivers/scsi/ufs/ufs.h
5  * Copyright (C) 2011-2013 Samsung India Software Operations
6  *
7  * Authors:
8  *      Santosh Yaraganavi <santosh.sy@samsung.com>
9  *      Vinayak Holikatti <h.vinayak@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * See the COPYING file in the top-level directory or visit
16  * <http://www.gnu.org/licenses/gpl-2.0.html>
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * This program is provided "AS IS" and "WITH ALL FAULTS" and
24  * without warranty of any kind. You are solely responsible for
25  * determining the appropriateness of using and distributing
26  * the program and assume all risks associated with your exercise
27  * of rights with respect to the program, including but not limited
28  * to infringement of third party rights, the risks and costs of
29  * program errors, damage to or loss of data, programs or equipment,
30  * and unavailability or interruption of operations. Under no
31  * circumstances will the contributor of this Program be liable for
32  * any damages of any kind arising from your use or distribution of
33  * this program.
34  */
35
36 #ifndef _UFS_H
37 #define _UFS_H
38
39 #include <linux/mutex.h>
40 #include <linux/types.h>
41 #include <scsi/ufs/ufs.h>
42
43 #define MAX_CDB_SIZE    16
44 #define GENERAL_UPIU_REQUEST_SIZE 32
45 #define QUERY_DESC_MAX_SIZE       255
46 #define QUERY_DESC_MIN_SIZE       2
47 #define QUERY_OSF_SIZE            (GENERAL_UPIU_REQUEST_SIZE - \
48                                         (sizeof(struct utp_upiu_header)))
49
50 #define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
51                         cpu_to_be32((byte3 << 24) | (byte2 << 16) |\
52                          (byte1 << 8) | (byte0))
53 /*
54  * UFS device may have standard LUs and LUN id could be from 0x00 to
55  * 0x7F. Standard LUs use "Peripheral Device Addressing Format".
56  * UFS device may also have the Well Known LUs (also referred as W-LU)
57  * which again could be from 0x00 to 0x7F. For W-LUs, device only use
58  * the "Extended Addressing Format" which means the W-LUNs would be
59  * from 0xc100 (SCSI_W_LUN_BASE) onwards.
60  * This means max. LUN number reported from UFS device could be 0xC17F.
61  */
62 #define UFS_UPIU_MAX_UNIT_NUM_ID        0x7F
63 #define UFS_MAX_LUNS            (SCSI_W_LUN_BASE + UFS_UPIU_MAX_UNIT_NUM_ID)
64 #define UFS_UPIU_WLUN_ID        (1 << 7)
65 #define UFS_UPIU_MAX_GENERAL_LUN        8
66
67 /* Well known logical unit id in LUN field of UPIU */
68 enum {
69         UFS_UPIU_REPORT_LUNS_WLUN       = 0x81,
70         UFS_UPIU_UFS_DEVICE_WLUN        = 0xD0,
71         UFS_UPIU_BOOT_WLUN              = 0xB0,
72         UFS_UPIU_RPMB_WLUN              = 0xC4,
73 };
74
75 /**
76  * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor
77  * @lun: LU number to check
78  * @return: true if the lun has a matching unit descriptor, false otherwise
79  */
80 static inline bool ufs_is_valid_unit_desc_lun(u8 lun)
81 {
82         return (lun == UFS_UPIU_RPMB_WLUN || (lun < UFS_UPIU_MAX_GENERAL_LUN));
83 }
84
85 /*
86  * UFS Protocol Information Unit related definitions
87  */
88
89 /* Task management functions */
90 enum {
91         UFS_ABORT_TASK          = 0x01,
92         UFS_ABORT_TASK_SET      = 0x02,
93         UFS_CLEAR_TASK_SET      = 0x04,
94         UFS_LOGICAL_RESET       = 0x08,
95         UFS_QUERY_TASK          = 0x80,
96         UFS_QUERY_TASK_SET      = 0x81,
97 };
98
99 /* UTP UPIU Transaction Codes Initiator to Target */
100 enum {
101         UPIU_TRANSACTION_NOP_OUT        = 0x00,
102         UPIU_TRANSACTION_COMMAND        = 0x01,
103         UPIU_TRANSACTION_DATA_OUT       = 0x02,
104         UPIU_TRANSACTION_TASK_REQ       = 0x04,
105         UPIU_TRANSACTION_QUERY_REQ      = 0x16,
106 };
107
108 /* UTP UPIU Transaction Codes Target to Initiator */
109 enum {
110         UPIU_TRANSACTION_NOP_IN         = 0x20,
111         UPIU_TRANSACTION_RESPONSE       = 0x21,
112         UPIU_TRANSACTION_DATA_IN        = 0x22,
113         UPIU_TRANSACTION_TASK_RSP       = 0x24,
114         UPIU_TRANSACTION_READY_XFER     = 0x31,
115         UPIU_TRANSACTION_QUERY_RSP      = 0x36,
116         UPIU_TRANSACTION_REJECT_UPIU    = 0x3F,
117 };
118
119 /* UPIU Read/Write flags */
120 enum {
121         UPIU_CMD_FLAGS_NONE     = 0x00,
122         UPIU_CMD_FLAGS_WRITE    = 0x20,
123         UPIU_CMD_FLAGS_READ     = 0x40,
124 };
125
126 /* UPIU Task Attributes */
127 enum {
128         UPIU_TASK_ATTR_SIMPLE   = 0x00,
129         UPIU_TASK_ATTR_ORDERED  = 0x01,
130         UPIU_TASK_ATTR_HEADQ    = 0x02,
131         UPIU_TASK_ATTR_ACA      = 0x03,
132 };
133
134 /* UPIU Query request function */
135 enum {
136         UPIU_QUERY_FUNC_STANDARD_READ_REQUEST           = 0x01,
137         UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST          = 0x81,
138 };
139
140 enum desc_header_offset {
141         QUERY_DESC_LENGTH_OFFSET        = 0x00,
142         QUERY_DESC_DESC_TYPE_OFFSET     = 0x01,
143 };
144
145 enum ufs_desc_max_size {
146         QUERY_DESC_DEVICE_MAX_SIZE              = 0x1F,
147         QUERY_DESC_CONFIGURAION_MAX_SIZE        = 0x90,
148         QUERY_DESC_UNIT_MAX_SIZE                = 0x23,
149         QUERY_DESC_INTERCONNECT_MAX_SIZE        = 0x06,
150         /*
151          * Max. 126 UNICODE characters (2 bytes per character) plus 2 bytes
152          * of descriptor header.
153          */
154         QUERY_DESC_STRING_MAX_SIZE              = 0xFE,
155         QUERY_DESC_GEOMETRY_MAZ_SIZE            = 0x44,
156         QUERY_DESC_POWER_MAX_SIZE               = 0x62,
157         QUERY_DESC_RFU_MAX_SIZE                 = 0x00,
158 };
159
160 /* Unit descriptor parameters offsets in bytes*/
161 enum unit_desc_param {
162         UNIT_DESC_PARAM_LEN                     = 0x0,
163         UNIT_DESC_PARAM_TYPE                    = 0x1,
164         UNIT_DESC_PARAM_UNIT_INDEX              = 0x2,
165         UNIT_DESC_PARAM_LU_ENABLE               = 0x3,
166         UNIT_DESC_PARAM_BOOT_LUN_ID             = 0x4,
167         UNIT_DESC_PARAM_LU_WR_PROTECT           = 0x5,
168         UNIT_DESC_PARAM_LU_Q_DEPTH              = 0x6,
169         UNIT_DESC_PARAM_MEM_TYPE                = 0x8,
170         UNIT_DESC_PARAM_DATA_RELIABILITY        = 0x9,
171         UNIT_DESC_PARAM_LOGICAL_BLK_SIZE        = 0xA,
172         UNIT_DESC_PARAM_LOGICAL_BLK_COUNT       = 0xB,
173         UNIT_DESC_PARAM_ERASE_BLK_SIZE          = 0x13,
174         UNIT_DESC_PARAM_PROVISIONING_TYPE       = 0x17,
175         UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT        = 0x18,
176         UNIT_DESC_PARAM_CTX_CAPABILITIES        = 0x20,
177         UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1      = 0x22,
178 };
179
180 /*
181  * Logical Unit Write Protect
182  * 00h: LU not write protected
183  * 01h: LU write protected when fPowerOnWPEn =1
184  * 02h: LU permanently write protected when fPermanentWPEn =1
185  */
186 enum ufs_lu_wp_type {
187         UFS_LU_NO_WP            = 0x00,
188         UFS_LU_POWER_ON_WP      = 0x01,
189         UFS_LU_PERM_WP          = 0x02,
190 };
191
192 /* bActiveICCLevel parameter current units */
193 enum {
194         UFSHCD_NANO_AMP         = 0,
195         UFSHCD_MICRO_AMP        = 1,
196         UFSHCD_MILI_AMP         = 2,
197         UFSHCD_AMP              = 3,
198 };
199
200 #define POWER_DESC_MAX_SIZE                     0x62
201 #define POWER_DESC_MAX_ACTV_ICC_LVLS            16
202
203 /* Attribute  bActiveICCLevel parameter bit masks definitions */
204 #define ATTR_ICC_LVL_UNIT_OFFSET        14
205 #define ATTR_ICC_LVL_UNIT_MASK          (0x3 << ATTR_ICC_LVL_UNIT_OFFSET)
206 #define ATTR_ICC_LVL_VALUE_MASK         0x3FF
207
208 /* Power descriptor parameters offsets in bytes */
209 enum power_desc_param_offset {
210         PWR_DESC_LEN                    = 0x0,
211         PWR_DESC_TYPE                   = 0x1,
212         PWR_DESC_ACTIVE_LVLS_VCC_0      = 0x2,
213         PWR_DESC_ACTIVE_LVLS_VCCQ_0     = 0x22,
214         PWR_DESC_ACTIVE_LVLS_VCCQ2_0    = 0x42,
215 };
216
217 /* Exception event mask values */
218 enum {
219         MASK_EE_STATUS          = 0xFFFF,
220         MASK_EE_URGENT_BKOPS    = (1 << 2),
221 };
222
223 /* Background operation status */
224 enum bkops_status {
225         BKOPS_STATUS_NO_OP               = 0x0,
226         BKOPS_STATUS_NON_CRITICAL        = 0x1,
227         BKOPS_STATUS_PERF_IMPACT         = 0x2,
228         BKOPS_STATUS_CRITICAL            = 0x3,
229         BKOPS_STATUS_MAX                 = BKOPS_STATUS_CRITICAL,
230 };
231
232 /* Query response result code */
233 enum {
234         QUERY_RESULT_SUCCESS                    = 0x00,
235         QUERY_RESULT_NOT_READABLE               = 0xF6,
236         QUERY_RESULT_NOT_WRITEABLE              = 0xF7,
237         QUERY_RESULT_ALREADY_WRITTEN            = 0xF8,
238         QUERY_RESULT_INVALID_LENGTH             = 0xF9,
239         QUERY_RESULT_INVALID_VALUE              = 0xFA,
240         QUERY_RESULT_INVALID_SELECTOR           = 0xFB,
241         QUERY_RESULT_INVALID_INDEX              = 0xFC,
242         QUERY_RESULT_INVALID_IDN                = 0xFD,
243         QUERY_RESULT_INVALID_OPCODE             = 0xFE,
244         QUERY_RESULT_GENERAL_FAILURE            = 0xFF,
245 };
246
247 /* UTP Transfer Request Command Type (CT) */
248 enum {
249         UPIU_COMMAND_SET_TYPE_SCSI      = 0x0,
250         UPIU_COMMAND_SET_TYPE_UFS       = 0x1,
251         UPIU_COMMAND_SET_TYPE_QUERY     = 0x2,
252 };
253
254 /* UTP Transfer Request Command Offset */
255 #define UPIU_COMMAND_TYPE_OFFSET        28
256
257 /* Offset of the response code in the UPIU header */
258 #define UPIU_RSP_CODE_OFFSET            8
259
260 enum {
261         MASK_SCSI_STATUS                = 0xFF,
262         MASK_TASK_RESPONSE              = 0xFF00,
263         MASK_RSP_UPIU_RESULT            = 0xFFFF,
264         MASK_QUERY_DATA_SEG_LEN         = 0xFFFF,
265         MASK_RSP_UPIU_DATA_SEG_LEN      = 0xFFFF,
266         MASK_RSP_EXCEPTION_EVENT        = 0x10000,
267 };
268
269 /* Task management service response */
270 enum {
271         UPIU_TASK_MANAGEMENT_FUNC_COMPL         = 0x00,
272         UPIU_TASK_MANAGEMENT_FUNC_NOT_SUPPORTED = 0x04,
273         UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED     = 0x08,
274         UPIU_TASK_MANAGEMENT_FUNC_FAILED        = 0x05,
275         UPIU_INCORRECT_LOGICAL_UNIT_NO          = 0x09,
276 };
277
278 /* UFS device power modes */
279 enum ufs_dev_pwr_mode {
280         UFS_ACTIVE_PWR_MODE     = 1,
281         UFS_SLEEP_PWR_MODE      = 2,
282         UFS_POWERDOWN_PWR_MODE  = 3,
283 };
284
285 /**
286  * struct utp_upiu_header - UPIU header structure
287  * @dword_0: UPIU header DW-0
288  * @dword_1: UPIU header DW-1
289  * @dword_2: UPIU header DW-2
290  */
291 struct utp_upiu_header {
292         __be32 dword_0;
293         __be32 dword_1;
294         __be32 dword_2;
295 };
296
297 /**
298  * struct utp_upiu_cmd - Command UPIU structure
299  * @data_transfer_len: Data Transfer Length DW-3
300  * @cdb: Command Descriptor Block CDB DW-4 to DW-7
301  */
302 struct utp_upiu_cmd {
303         __be32 exp_data_transfer_len;
304         u8 cdb[MAX_CDB_SIZE];
305 };
306
307 /**
308  * struct utp_upiu_query - upiu request buffer structure for
309  * query request.
310  * @opcode: command to perform B-0
311  * @idn: a value that indicates the particular type of data B-1
312  * @index: Index to further identify data B-2
313  * @selector: Index to further identify data B-3
314  * @reserved_osf: spec reserved field B-4,5
315  * @length: number of descriptor bytes to read/write B-6,7
316  * @value: Attribute value to be written DW-5
317  * @reserved: spec reserved DW-6,7
318  */
319 struct utp_upiu_query {
320         u8 opcode;
321         u8 idn;
322         u8 index;
323         u8 selector;
324         __be16 reserved_osf;
325         __be16 length;
326         __be32 value;
327         __be32 reserved[2];
328 };
329
330 /**
331  * struct utp_upiu_req - general upiu request structure
332  * @header:UPIU header structure DW-0 to DW-2
333  * @sc: fields structure for scsi command DW-3 to DW-7
334  * @qr: fields structure for query request DW-3 to DW-7
335  */
336 struct utp_upiu_req {
337         struct utp_upiu_header header;
338         union {
339                 struct utp_upiu_cmd sc;
340                 struct utp_upiu_query qr;
341         };
342 };
343
344 /**
345  * struct utp_cmd_rsp - Response UPIU structure
346  * @residual_transfer_count: Residual transfer count DW-3
347  * @reserved: Reserved double words DW-4 to DW-7
348  * @sense_data_len: Sense data length DW-8 U16
349  * @sense_data: Sense data field DW-8 to DW-12
350  */
351 struct utp_cmd_rsp {
352         __be32 residual_transfer_count;
353         __be32 reserved[4];
354         __be16 sense_data_len;
355         u8 sense_data[18];
356 };
357
358 /**
359  * struct utp_upiu_rsp - general upiu response structure
360  * @header: UPIU header structure DW-0 to DW-2
361  * @sr: fields structure for scsi command DW-3 to DW-12
362  * @qr: fields structure for query request DW-3 to DW-7
363  */
364 struct utp_upiu_rsp {
365         struct utp_upiu_header header;
366         union {
367                 struct utp_cmd_rsp sr;
368                 struct utp_upiu_query qr;
369         };
370 };
371
372 /**
373  * struct utp_upiu_task_req - Task request UPIU structure
374  * @header - UPIU header structure DW0 to DW-2
375  * @input_param1: Input parameter 1 DW-3
376  * @input_param2: Input parameter 2 DW-4
377  * @input_param3: Input parameter 3 DW-5
378  * @reserved: Reserved double words DW-6 to DW-7
379  */
380 struct utp_upiu_task_req {
381         struct utp_upiu_header header;
382         __be32 input_param1;
383         __be32 input_param2;
384         __be32 input_param3;
385         __be32 reserved[2];
386 };
387
388 /**
389  * struct utp_upiu_task_rsp - Task Management Response UPIU structure
390  * @header: UPIU header structure DW0-DW-2
391  * @output_param1: Ouput parameter 1 DW3
392  * @output_param2: Output parameter 2 DW4
393  * @reserved: Reserved double words DW-5 to DW-7
394  */
395 struct utp_upiu_task_rsp {
396         struct utp_upiu_header header;
397         __be32 output_param1;
398         __be32 output_param2;
399         __be32 reserved[3];
400 };
401
402 /**
403  * struct ufs_query_req - parameters for building a query request
404  * @query_func: UPIU header query function
405  * @upiu_req: the query request data
406  */
407 struct ufs_query_req {
408         u8 query_func;
409         struct utp_upiu_query upiu_req;
410 };
411
412 /**
413  * struct ufs_query_resp - UPIU QUERY
414  * @response: device response code
415  * @upiu_res: query response data
416  */
417 struct ufs_query_res {
418         u8 response;
419         struct utp_upiu_query upiu_res;
420 };
421
422 #define UFS_VREG_VCC_MIN_UV        2700000 /* uV */
423 #define UFS_VREG_VCC_MAX_UV        3600000 /* uV */
424 #define UFS_VREG_VCC_1P8_MIN_UV    1700000 /* uV */
425 #define UFS_VREG_VCC_1P8_MAX_UV    1950000 /* uV */
426 #define UFS_VREG_VCCQ_MIN_UV       1100000 /* uV */
427 #define UFS_VREG_VCCQ_MAX_UV       1300000 /* uV */
428 #define UFS_VREG_VCCQ2_MIN_UV      1650000 /* uV */
429 #define UFS_VREG_VCCQ2_MAX_UV      1950000 /* uV */
430
431 /*
432  * VCCQ & VCCQ2 current requirement when UFS device is in sleep state
433  * and link is in Hibern8 state.
434  */
435 #define UFS_VREG_LPM_LOAD_UA    1000 /* uA */
436
437 struct ufs_vreg {
438         struct regulator *reg;
439         const char *name;
440         bool enabled;
441         int min_uV;
442         int max_uV;
443         int min_uA;
444         int max_uA;
445 };
446
447 struct ufs_vreg_info {
448         struct ufs_vreg *vcc;
449         struct ufs_vreg *vccq;
450         struct ufs_vreg *vccq2;
451         struct ufs_vreg *vdd_hba;
452 };
453
454 struct ufs_dev_info {
455         bool f_power_on_wp_en;
456         /* Keeps information if any of the LU is power on write protected */
457         bool is_lu_power_on_wp;
458 };
459
460 #endif /* End of Header */