scsi_bsg_ufs.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  17. /*
  18. * UFS Transport SGIO v4 BSG Message Support
  19. *
  20. * Copyright (C) 2011-2013 Samsung India Software Operations
  21. * Copyright (C) 2018 Western Digital Corporation
  22. */
  23. #ifndef SCSI_BSG_UFS_H
  24. #define SCSI_BSG_UFS_H
  25. #include <asm/byteorder.h>
  26. #include <linux/types.h>
  27. /*
  28. * This file intended to be included by both kernel and user space
  29. */
  30. #define UFS_CDB_SIZE 16
  31. /* uic commands are 4DW long, per UFSHCI V2.1 paragraph 5.6.1 */
  32. #define UIC_CMD_SIZE (sizeof(__u32) * 4)
  33. enum ufs_bsg_msg_code {
  34. UPIU_TRANSACTION_UIC_CMD = 0x1F,
  35. UPIU_TRANSACTION_ARPMB_CMD,
  36. };
  37. /* UFS RPMB Request Message Types */
  38. enum ufs_rpmb_op_type {
  39. UFS_RPMB_WRITE_KEY = 0x01,
  40. UFS_RPMB_READ_CNT = 0x02,
  41. UFS_RPMB_WRITE = 0x03,
  42. UFS_RPMB_READ = 0x04,
  43. UFS_RPMB_READ_RESP = 0x05,
  44. UFS_RPMB_SEC_CONF_WRITE = 0x06,
  45. UFS_RPMB_SEC_CONF_READ = 0x07,
  46. UFS_RPMB_PURGE_ENABLE = 0x08,
  47. UFS_RPMB_PURGE_STATUS_READ = 0x09,
  48. };
  49. /**
  50. * struct utp_upiu_header - UPIU header structure
  51. * @dword_0: UPIU header DW-0
  52. * @dword_1: UPIU header DW-1
  53. * @dword_2: UPIU header DW-2
  54. *
  55. * @transaction_code: Type of request or response. See also enum
  56. * upiu_request_transaction and enum upiu_response_transaction.
  57. * @flags: UPIU flags. The meaning of individual flags depends on the
  58. * transaction code.
  59. * @lun: Logical unit number.
  60. * @task_tag: Task tag.
  61. * @iid: Initiator ID.
  62. * @command_set_type: 0 for SCSI command set; 1 for UFS specific.
  63. * @tm_function: Task management function in case of a task management request
  64. * UPIU.
  65. * @query_function: Query function in case of a query request UPIU.
  66. * @response: 0 for success; 1 for failure.
  67. * @status: SCSI status if this is the header of a response to a SCSI command.
  68. * @ehs_length: EHS length in units of 32 bytes.
  69. * @device_information:
  70. * @data_segment_length: data segment length.
  71. */
  72. struct utp_upiu_header {
  73. union {
  74. struct {
  75. __be32 dword_0;
  76. __be32 dword_1;
  77. __be32 dword_2;
  78. };
  79. struct {
  80. __u8 transaction_code;
  81. __u8 flags;
  82. __u8 lun;
  83. __u8 task_tag;
  84. #if defined(__BIG_ENDIAN)
  85. __u8 iid: 4;
  86. __u8 command_set_type: 4;
  87. #elif defined(__LITTLE_ENDIAN)
  88. __u8 command_set_type: 4;
  89. __u8 iid: 4;
  90. #else
  91. #error
  92. #endif
  93. union {
  94. __u8 tm_function;
  95. __u8 query_function;
  96. } __attribute__((packed));
  97. __u8 response;
  98. __u8 status;
  99. __u8 ehs_length;
  100. __u8 device_information;
  101. __be16 data_segment_length;
  102. };
  103. };
  104. };
  105. /**
  106. * struct utp_upiu_query - QUERY REQUEST UPIU structure.
  107. * @opcode: query function to perform B-0
  108. * @idn: descriptor or attribute identification number B-1
  109. * @index: Index that further identifies which data to access B-2
  110. * @selector: Index that further identifies which data to access B-3
  111. * @reserved_osf: spec reserved field B-4,5
  112. * @length: number of descriptor bytes to read or write B-6,7
  113. * @value: if @opcode == UPIU_QUERY_OPCODE_WRITE_ATTR, the value to be written B-6,7
  114. * @reserved: reserved for future use DW-6,7
  115. */
  116. struct utp_upiu_query {
  117. __u8 opcode;
  118. __u8 idn;
  119. __u8 index;
  120. __u8 selector;
  121. __be16 reserved_osf;
  122. __be16 length;
  123. __be32 value;
  124. __be32 reserved[2];
  125. };
  126. /**
  127. * struct utp_upiu_query_v4_0 - upiu request buffer structure for
  128. * query request >= UFS 4.0 spec.
  129. * @opcode: command to perform B-0
  130. * @idn: a value that indicates the particular type of data B-1
  131. * @index: Index to further identify data B-2
  132. * @selector: Index to further identify data B-3
  133. * @osf3: spec field B-4
  134. * @osf4: spec field B-5
  135. * @osf5: spec field B 6,7
  136. * @osf6: spec field DW 8,9
  137. * @osf7: spec field DW 10,11
  138. */
  139. struct utp_upiu_query_v4_0 {
  140. __u8 opcode;
  141. __u8 idn;
  142. __u8 index;
  143. __u8 selector;
  144. __u8 osf3;
  145. __u8 osf4;
  146. __be16 osf5;
  147. __be32 osf6;
  148. __be32 osf7;
  149. /* private: */
  150. __be32 reserved;
  151. };
  152. /**
  153. * struct utp_upiu_cmd - Command UPIU structure
  154. * @exp_data_transfer_len: Data Transfer Length DW-3
  155. * @cdb: Command Descriptor Block CDB DW-4 to DW-7
  156. */
  157. struct utp_upiu_cmd {
  158. __be32 exp_data_transfer_len;
  159. __u8 cdb[UFS_CDB_SIZE];
  160. };
  161. /**
  162. * struct utp_upiu_req - general upiu request structure
  163. * @header:UPIU header structure DW-0 to DW-2
  164. * @sc: fields structure for scsi command DW-3 to DW-7
  165. * @qr: fields structure for query request DW-3 to DW-7
  166. * @uc: use utp_upiu_query to host the 4 dwords of uic command
  167. */
  168. struct utp_upiu_req {
  169. struct utp_upiu_header header;
  170. union {
  171. struct utp_upiu_cmd sc;
  172. struct utp_upiu_query qr;
  173. struct utp_upiu_query uc;
  174. };
  175. };
  176. struct ufs_arpmb_meta {
  177. __be16 req_resp_type;
  178. __u8 nonce[16];
  179. __be32 write_counter;
  180. __be16 addr_lun;
  181. __be16 block_count;
  182. __be16 result;
  183. } __attribute__((__packed__));
  184. struct ufs_ehs {
  185. __u8 length;
  186. __u8 ehs_type;
  187. __be16 ehssub_type;
  188. struct ufs_arpmb_meta meta;
  189. __u8 mac_key[32];
  190. } __attribute__((__packed__));
  191. /* request (CDB) structure of the sg_io_v4 */
  192. struct ufs_bsg_request {
  193. __u32 msgcode;
  194. struct utp_upiu_req upiu_req;
  195. };
  196. /* response (request sense data) structure of the sg_io_v4 */
  197. struct ufs_bsg_reply {
  198. /*
  199. * The completion result. Result exists in two forms:
  200. * if negative, it is an -Exxx system errno value. There will
  201. * be no further reply information supplied.
  202. * else, it's the 4-byte scsi error result, with driver, host,
  203. * msg and status fields. The per-msgcode reply structure
  204. * will contain valid data.
  205. */
  206. int result;
  207. /* If there was reply_payload, how much was received? */
  208. __u32 reply_payload_rcv_len;
  209. struct utp_upiu_req upiu_rsp;
  210. };
  211. struct ufs_rpmb_request {
  212. struct ufs_bsg_request bsg_request;
  213. struct ufs_ehs ehs_req;
  214. };
  215. struct ufs_rpmb_reply {
  216. struct ufs_bsg_reply bsg_reply;
  217. struct ufs_ehs ehs_rsp;
  218. };
  219. #endif /* UFS_BSG_H */