desc_constr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * caam descriptor construction helper functions
  4. *
  5. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  6. * Copyright 2019 NXP
  7. */
  8. #ifndef DESC_CONSTR_H
  9. #define DESC_CONSTR_H
  10. #include "desc.h"
  11. #include "regs.h"
  12. #define IMMEDIATE (1 << 23)
  13. #define CAAM_CMD_SZ sizeof(u32)
  14. #define CAAM_PTR_SZ caam_ptr_sz
  15. #define CAAM_PTR_SZ_MAX sizeof(dma_addr_t)
  16. #define CAAM_PTR_SZ_MIN sizeof(u32)
  17. #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
  18. #define __DESC_JOB_IO_LEN(n) (CAAM_CMD_SZ * 5 + (n) * 3)
  19. #define DESC_JOB_IO_LEN __DESC_JOB_IO_LEN(CAAM_PTR_SZ)
  20. #define DESC_JOB_IO_LEN_MAX __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MAX)
  21. #define DESC_JOB_IO_LEN_MIN __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MIN)
  22. /*
  23. * The CAAM QI hardware constructs a job descriptor which points
  24. * to shared descriptor (as pointed by context_a of FQ to CAAM).
  25. * When the job descriptor is executed by deco, the whole job
  26. * descriptor together with shared descriptor gets loaded in
  27. * deco buffer which is 64 words long (each 32-bit).
  28. *
  29. * The job descriptor constructed by QI hardware has layout:
  30. *
  31. * HEADER (1 word)
  32. * Shdesc ptr (1 or 2 words)
  33. * SEQ_OUT_PTR (1 word)
  34. * Out ptr (1 or 2 words)
  35. * Out length (1 word)
  36. * SEQ_IN_PTR (1 word)
  37. * In ptr (1 or 2 words)
  38. * In length (1 word)
  39. *
  40. * The shdesc ptr is used to fetch shared descriptor contents
  41. * into deco buffer.
  42. *
  43. * Apart from shdesc contents, the total number of words that
  44. * get loaded in deco buffer are '8' or '11'. The remaining words
  45. * in deco buffer can be used for storing shared descriptor.
  46. */
  47. #define MAX_SDLEN ((CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN_MIN) / CAAM_CMD_SZ)
  48. #ifdef DEBUG
  49. #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
  50. &__func__[sizeof("append")]); } while (0)
  51. #else
  52. #define PRINT_POS
  53. #endif
  54. #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
  55. LDST_SRCDST_WORD_DECOCTRL | \
  56. (LDOFF_CHG_SHARE_OK_NO_PROP << \
  57. LDST_OFFSET_SHIFT))
  58. #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
  59. LDST_SRCDST_WORD_DECOCTRL | \
  60. (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
  61. #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
  62. LDST_SRCDST_WORD_DECOCTRL | \
  63. (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
  64. extern bool caam_little_end;
  65. extern size_t caam_ptr_sz;
  66. /*
  67. * HW fetches 4 S/G table entries at a time, irrespective of how many entries
  68. * are in the table. It's SW's responsibility to make sure these accesses
  69. * do not have side effects.
  70. */
  71. static inline int pad_sg_nents(int sg_nents)
  72. {
  73. return ALIGN(sg_nents, 4);
  74. }
  75. static inline int desc_len(u32 * const desc)
  76. {
  77. return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK;
  78. }
  79. static inline int desc_bytes(void * const desc)
  80. {
  81. return desc_len(desc) * CAAM_CMD_SZ;
  82. }
  83. static inline u32 *desc_end(u32 * const desc)
  84. {
  85. return desc + desc_len(desc);
  86. }
  87. static inline void *sh_desc_pdb(u32 * const desc)
  88. {
  89. return desc + 1;
  90. }
  91. static inline void init_desc(u32 * const desc, u32 options)
  92. {
  93. *desc = cpu_to_caam32((options | HDR_ONE) + 1);
  94. }
  95. static inline void init_sh_desc(u32 * const desc, u32 options)
  96. {
  97. PRINT_POS;
  98. init_desc(desc, CMD_SHARED_DESC_HDR | options);
  99. }
  100. static inline void init_sh_desc_pdb(u32 * const desc, u32 options,
  101. size_t pdb_bytes)
  102. {
  103. u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
  104. init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
  105. options);
  106. }
  107. static inline void init_job_desc(u32 * const desc, u32 options)
  108. {
  109. init_desc(desc, CMD_DESC_HDR | options);
  110. }
  111. static inline void init_job_desc_pdb(u32 * const desc, u32 options,
  112. size_t pdb_bytes)
  113. {
  114. u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
  115. init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options);
  116. }
  117. static inline void append_ptr(u32 * const desc, dma_addr_t ptr)
  118. {
  119. if (caam_ptr_sz == sizeof(dma_addr_t)) {
  120. dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
  121. *offset = cpu_to_caam_dma(ptr);
  122. } else {
  123. u32 *offset = (u32 *)desc_end(desc);
  124. *offset = cpu_to_caam_dma(ptr);
  125. }
  126. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
  127. CAAM_PTR_SZ / CAAM_CMD_SZ);
  128. }
  129. static inline void init_job_desc_shared(u32 * const desc, dma_addr_t ptr,
  130. int len, u32 options)
  131. {
  132. PRINT_POS;
  133. init_job_desc(desc, HDR_SHARED | options |
  134. (len << HDR_START_IDX_SHIFT));
  135. append_ptr(desc, ptr);
  136. }
  137. static inline void append_data(u32 * const desc, const void *data, int len)
  138. {
  139. u32 *offset = desc_end(desc);
  140. /* Avoid gcc warning: memcpy with data == NULL */
  141. if (!IS_ENABLED(CONFIG_CRYPTO_DEV_FSL_CAAM_DEBUG) || data)
  142. memcpy(offset, data, len);
  143. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
  144. (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ);
  145. }
  146. static inline void append_cmd(u32 * const desc, u32 command)
  147. {
  148. u32 *cmd = desc_end(desc);
  149. *cmd = cpu_to_caam32(command);
  150. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1);
  151. }
  152. #define append_u32 append_cmd
  153. static inline void append_u64(u32 * const desc, u64 data)
  154. {
  155. u32 *offset = desc_end(desc);
  156. /* Only 32-bit alignment is guaranteed in descriptor buffer */
  157. if (caam_little_end) {
  158. *offset = cpu_to_caam32(lower_32_bits(data));
  159. *(++offset) = cpu_to_caam32(upper_32_bits(data));
  160. } else {
  161. *offset = cpu_to_caam32(upper_32_bits(data));
  162. *(++offset) = cpu_to_caam32(lower_32_bits(data));
  163. }
  164. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2);
  165. }
  166. /* Write command without affecting header, and return pointer to next word */
  167. static inline u32 *write_cmd(u32 * const desc, u32 command)
  168. {
  169. *desc = cpu_to_caam32(command);
  170. return desc + 1;
  171. }
  172. static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len,
  173. u32 command)
  174. {
  175. append_cmd(desc, command | len);
  176. append_ptr(desc, ptr);
  177. }
  178. /* Write length after pointer, rather than inside command */
  179. static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr,
  180. unsigned int len, u32 command)
  181. {
  182. append_cmd(desc, command);
  183. if (!(command & (SQIN_RTO | SQIN_PRE)))
  184. append_ptr(desc, ptr);
  185. append_cmd(desc, len);
  186. }
  187. static inline void append_cmd_data(u32 * const desc, const void *data, int len,
  188. u32 command)
  189. {
  190. append_cmd(desc, command | IMMEDIATE | len);
  191. append_data(desc, data, len);
  192. }
  193. #define APPEND_CMD_RET(cmd, op) \
  194. static inline u32 *append_##cmd(u32 * const desc, u32 options) \
  195. { \
  196. u32 *cmd = desc_end(desc); \
  197. PRINT_POS; \
  198. append_cmd(desc, CMD_##op | options); \
  199. return cmd; \
  200. }
  201. APPEND_CMD_RET(jump, JUMP)
  202. APPEND_CMD_RET(move, MOVE)
  203. APPEND_CMD_RET(move_len, MOVE_LEN)
  204. static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
  205. {
  206. *jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) |
  207. (desc_len(desc) - (jump_cmd - desc)));
  208. }
  209. static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd)
  210. {
  211. u32 val = caam32_to_cpu(*move_cmd);
  212. val &= ~MOVE_OFFSET_MASK;
  213. val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK;
  214. *move_cmd = cpu_to_caam32(val);
  215. }
  216. #define APPEND_CMD(cmd, op) \
  217. static inline void append_##cmd(u32 * const desc, u32 options) \
  218. { \
  219. PRINT_POS; \
  220. append_cmd(desc, CMD_##op | options); \
  221. }
  222. APPEND_CMD(operation, OPERATION)
  223. #define APPEND_CMD_LEN(cmd, op) \
  224. static inline void append_##cmd(u32 * const desc, unsigned int len, \
  225. u32 options) \
  226. { \
  227. PRINT_POS; \
  228. append_cmd(desc, CMD_##op | len | options); \
  229. }
  230. APPEND_CMD_LEN(seq_load, SEQ_LOAD)
  231. APPEND_CMD_LEN(seq_store, SEQ_STORE)
  232. APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
  233. APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
  234. #define APPEND_CMD_PTR(cmd, op) \
  235. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  236. unsigned int len, u32 options) \
  237. { \
  238. PRINT_POS; \
  239. append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
  240. }
  241. APPEND_CMD_PTR(key, KEY)
  242. APPEND_CMD_PTR(load, LOAD)
  243. APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
  244. APPEND_CMD_PTR(fifo_store, FIFO_STORE)
  245. static inline void append_store(u32 * const desc, dma_addr_t ptr,
  246. unsigned int len, u32 options)
  247. {
  248. u32 cmd_src;
  249. cmd_src = options & LDST_SRCDST_MASK;
  250. append_cmd(desc, CMD_STORE | options | len);
  251. /* The following options do not require pointer */
  252. if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
  253. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
  254. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
  255. cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
  256. append_ptr(desc, ptr);
  257. }
  258. #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
  259. static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \
  260. dma_addr_t ptr, \
  261. unsigned int len, \
  262. u32 options) \
  263. { \
  264. PRINT_POS; \
  265. if (options & (SQIN_RTO | SQIN_PRE)) \
  266. append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
  267. else \
  268. append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
  269. }
  270. APPEND_SEQ_PTR_INTLEN(in, IN)
  271. APPEND_SEQ_PTR_INTLEN(out, OUT)
  272. #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
  273. static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
  274. unsigned int len, u32 options) \
  275. { \
  276. PRINT_POS; \
  277. append_cmd_data(desc, data, len, CMD_##op | options); \
  278. }
  279. APPEND_CMD_PTR_TO_IMM(load, LOAD);
  280. APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
  281. #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
  282. static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \
  283. unsigned int len, u32 options) \
  284. { \
  285. PRINT_POS; \
  286. append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
  287. }
  288. APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
  289. APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
  290. /*
  291. * Determine whether to store length internally or externally depending on
  292. * the size of its type
  293. */
  294. #define APPEND_CMD_PTR_LEN(cmd, op, type) \
  295. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  296. type len, u32 options) \
  297. { \
  298. PRINT_POS; \
  299. if (sizeof(type) > sizeof(u16)) \
  300. append_##cmd##_extlen(desc, ptr, len, options); \
  301. else \
  302. append_##cmd##_intlen(desc, ptr, len, options); \
  303. }
  304. APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
  305. APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
  306. /*
  307. * 2nd variant for commands whose specified immediate length differs
  308. * from length of immediate data provided, e.g., split keys
  309. */
  310. #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
  311. static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
  312. unsigned int data_len, \
  313. unsigned int len, u32 options) \
  314. { \
  315. PRINT_POS; \
  316. append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
  317. append_data(desc, data, data_len); \
  318. }
  319. APPEND_CMD_PTR_TO_IMM2(key, KEY);
  320. #define APPEND_CMD_RAW_IMM(cmd, op, type) \
  321. static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \
  322. u32 options) \
  323. { \
  324. PRINT_POS; \
  325. if (options & LDST_LEN_MASK) \
  326. append_cmd(desc, CMD_##op | IMMEDIATE | options); \
  327. else \
  328. append_cmd(desc, CMD_##op | IMMEDIATE | options | \
  329. sizeof(type)); \
  330. append_cmd(desc, immediate); \
  331. }
  332. APPEND_CMD_RAW_IMM(load, LOAD, u32);
  333. /*
  334. * ee - endianness
  335. * size - size of immediate type in bytes
  336. */
  337. #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \
  338. static inline void append_##cmd##_imm_##ee##size(u32 *desc, \
  339. u##size immediate, \
  340. u32 options) \
  341. { \
  342. __##ee##size data = cpu_to_##ee##size(immediate); \
  343. PRINT_POS; \
  344. append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \
  345. append_data(desc, &data, sizeof(data)); \
  346. }
  347. APPEND_CMD_RAW_IMM2(load, LOAD, be, 32);
  348. /*
  349. * Append math command. Only the last part of destination and source need to
  350. * be specified
  351. */
  352. #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
  353. append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
  354. MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
  355. #define append_math_add(desc, dest, src0, src1, len) \
  356. APPEND_MATH(ADD, desc, dest, src0, src1, len)
  357. #define append_math_sub(desc, dest, src0, src1, len) \
  358. APPEND_MATH(SUB, desc, dest, src0, src1, len)
  359. #define append_math_add_c(desc, dest, src0, src1, len) \
  360. APPEND_MATH(ADDC, desc, dest, src0, src1, len)
  361. #define append_math_sub_b(desc, dest, src0, src1, len) \
  362. APPEND_MATH(SUBB, desc, dest, src0, src1, len)
  363. #define append_math_and(desc, dest, src0, src1, len) \
  364. APPEND_MATH(AND, desc, dest, src0, src1, len)
  365. #define append_math_or(desc, dest, src0, src1, len) \
  366. APPEND_MATH(OR, desc, dest, src0, src1, len)
  367. #define append_math_xor(desc, dest, src0, src1, len) \
  368. APPEND_MATH(XOR, desc, dest, src0, src1, len)
  369. #define append_math_lshift(desc, dest, src0, src1, len) \
  370. APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
  371. #define append_math_rshift(desc, dest, src0, src1, len) \
  372. APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
  373. #define append_math_ldshift(desc, dest, src0, src1, len) \
  374. APPEND_MATH(SHLD, desc, dest, src0, src1, len)
  375. /* Exactly one source is IMM. Data is passed in as u32 value */
  376. #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
  377. do { \
  378. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
  379. append_cmd(desc, data); \
  380. } while (0)
  381. #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
  382. APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
  383. #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
  384. APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
  385. #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
  386. APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
  387. #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
  388. APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
  389. #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
  390. APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
  391. #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
  392. APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
  393. #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
  394. APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
  395. #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
  396. APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
  397. #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
  398. APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
  399. /* Exactly one source is IMM. Data is passed in as u64 value */
  400. #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
  401. do { \
  402. u32 upper = (data >> 16) >> 16; \
  403. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
  404. (upper ? 0 : MATH_IFB)); \
  405. if (upper) \
  406. append_u64(desc, data); \
  407. else \
  408. append_u32(desc, lower_32_bits(data)); \
  409. } while (0)
  410. #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
  411. APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
  412. #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
  413. APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
  414. #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
  415. APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
  416. #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
  417. APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
  418. #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
  419. APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
  420. #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
  421. APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
  422. #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
  423. APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
  424. #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
  425. APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
  426. #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
  427. APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
  428. /**
  429. * struct alginfo - Container for algorithm details
  430. * @algtype: algorithm selector; for valid values, see documentation of the
  431. * functions where it is used.
  432. * @keylen: length of the provided algorithm key, in bytes
  433. * @keylen_pad: padded length of the provided algorithm key, in bytes
  434. * @key_dma: dma (bus) address where algorithm key resides
  435. * @key_virt: virtual address where algorithm key resides
  436. * @key_inline: true - key can be inlined in the descriptor; false - key is
  437. * referenced by the descriptor
  438. */
  439. struct alginfo {
  440. u32 algtype;
  441. unsigned int keylen;
  442. unsigned int keylen_pad;
  443. dma_addr_t key_dma;
  444. const void *key_virt;
  445. bool key_inline;
  446. };
  447. /**
  448. * desc_inline_query() - Provide indications on which data items can be inlined
  449. * and which shall be referenced in a shared descriptor.
  450. * @sd_base_len: Shared descriptor base length - bytes consumed by the commands,
  451. * excluding the data items to be inlined (or corresponding
  452. * pointer if an item is not inlined). Each cnstr_* function that
  453. * generates descriptors should have a define mentioning
  454. * corresponding length.
  455. * @jd_len: Maximum length of the job descriptor(s) that will be used
  456. * together with the shared descriptor.
  457. * @data_len: Array of lengths of the data items trying to be inlined
  458. * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0
  459. * otherwise.
  460. * @count: Number of data items (size of @data_len array); must be <= 32
  461. *
  462. * Return: 0 if data can be inlined / referenced, negative value if not. If 0,
  463. * check @inl_mask for details.
  464. */
  465. static inline int desc_inline_query(unsigned int sd_base_len,
  466. unsigned int jd_len, unsigned int *data_len,
  467. u32 *inl_mask, unsigned int count)
  468. {
  469. int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len);
  470. unsigned int i;
  471. *inl_mask = 0;
  472. for (i = 0; (i < count) && (rem_bytes > 0); i++) {
  473. if (rem_bytes - (int)(data_len[i] +
  474. (count - i - 1) * CAAM_PTR_SZ) >= 0) {
  475. rem_bytes -= data_len[i];
  476. *inl_mask |= (1 << i);
  477. } else {
  478. rem_bytes -= CAAM_PTR_SZ;
  479. }
  480. }
  481. return (rem_bytes >= 0) ? 0 : -1;
  482. }
  483. /**
  484. * append_proto_dkp - Derived Key Protocol (DKP): key -> split key
  485. * @desc: pointer to buffer used for descriptor construction
  486. * @adata: pointer to authentication transform definitions.
  487. * keylen should be the length of initial key, while keylen_pad
  488. * the length of the derived (split) key.
  489. * Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
  490. * SHA256, SHA384, SHA512}.
  491. */
  492. static inline void append_proto_dkp(u32 * const desc, struct alginfo *adata)
  493. {
  494. u32 protid;
  495. /*
  496. * Quick & dirty translation from OP_ALG_ALGSEL_{MD5, SHA*}
  497. * to OP_PCLID_DKP_{MD5, SHA*}
  498. */
  499. protid = (adata->algtype & OP_ALG_ALGSEL_SUBMASK) |
  500. (0x20 << OP_ALG_ALGSEL_SHIFT);
  501. if (adata->key_inline) {
  502. int words;
  503. if (adata->keylen > adata->keylen_pad) {
  504. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  505. OP_PCL_DKP_SRC_PTR |
  506. OP_PCL_DKP_DST_IMM | adata->keylen);
  507. append_ptr(desc, adata->key_dma);
  508. words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
  509. CAAM_PTR_SZ) / CAAM_CMD_SZ;
  510. } else {
  511. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  512. OP_PCL_DKP_SRC_IMM |
  513. OP_PCL_DKP_DST_IMM | adata->keylen);
  514. append_data(desc, adata->key_virt, adata->keylen);
  515. words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
  516. ALIGN(adata->keylen, CAAM_CMD_SZ)) /
  517. CAAM_CMD_SZ;
  518. }
  519. /* Reserve space in descriptor buffer for the derived key */
  520. if (words)
  521. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + words);
  522. } else {
  523. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  524. OP_PCL_DKP_SRC_PTR | OP_PCL_DKP_DST_PTR |
  525. adata->keylen);
  526. append_ptr(desc, adata->key_dma);
  527. }
  528. }
  529. #endif /* DESC_CONSTR_H */