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. if (len) /* avoid sparse warning: memcpy with byte count of 0 */
  141. memcpy(offset, data, len);
  142. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
  143. (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ);
  144. }
  145. static inline void append_cmd(u32 * const desc, u32 command)
  146. {
  147. u32 *cmd = desc_end(desc);
  148. *cmd = cpu_to_caam32(command);
  149. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1);
  150. }
  151. #define append_u32 append_cmd
  152. static inline void append_u64(u32 * const desc, u64 data)
  153. {
  154. u32 *offset = desc_end(desc);
  155. /* Only 32-bit alignment is guaranteed in descriptor buffer */
  156. if (caam_little_end) {
  157. *offset = cpu_to_caam32(lower_32_bits(data));
  158. *(++offset) = cpu_to_caam32(upper_32_bits(data));
  159. } else {
  160. *offset = cpu_to_caam32(upper_32_bits(data));
  161. *(++offset) = cpu_to_caam32(lower_32_bits(data));
  162. }
  163. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2);
  164. }
  165. /* Write command without affecting header, and return pointer to next word */
  166. static inline u32 *write_cmd(u32 * const desc, u32 command)
  167. {
  168. *desc = cpu_to_caam32(command);
  169. return desc + 1;
  170. }
  171. static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len,
  172. u32 command)
  173. {
  174. append_cmd(desc, command | len);
  175. append_ptr(desc, ptr);
  176. }
  177. /* Write length after pointer, rather than inside command */
  178. static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr,
  179. unsigned int len, u32 command)
  180. {
  181. append_cmd(desc, command);
  182. if (!(command & (SQIN_RTO | SQIN_PRE)))
  183. append_ptr(desc, ptr);
  184. append_cmd(desc, len);
  185. }
  186. static inline void append_cmd_data(u32 * const desc, const void *data, int len,
  187. u32 command)
  188. {
  189. append_cmd(desc, command | IMMEDIATE | len);
  190. append_data(desc, data, len);
  191. }
  192. #define APPEND_CMD_RET(cmd, op) \
  193. static inline u32 *append_##cmd(u32 * const desc, u32 options) \
  194. { \
  195. u32 *cmd = desc_end(desc); \
  196. PRINT_POS; \
  197. append_cmd(desc, CMD_##op | options); \
  198. return cmd; \
  199. }
  200. APPEND_CMD_RET(jump, JUMP)
  201. APPEND_CMD_RET(move, MOVE)
  202. APPEND_CMD_RET(move_len, MOVE_LEN)
  203. static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
  204. {
  205. *jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) |
  206. (desc_len(desc) - (jump_cmd - desc)));
  207. }
  208. static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd)
  209. {
  210. u32 val = caam32_to_cpu(*move_cmd);
  211. val &= ~MOVE_OFFSET_MASK;
  212. val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK;
  213. *move_cmd = cpu_to_caam32(val);
  214. }
  215. #define APPEND_CMD(cmd, op) \
  216. static inline void append_##cmd(u32 * const desc, u32 options) \
  217. { \
  218. PRINT_POS; \
  219. append_cmd(desc, CMD_##op | options); \
  220. }
  221. APPEND_CMD(operation, OPERATION)
  222. #define APPEND_CMD_LEN(cmd, op) \
  223. static inline void append_##cmd(u32 * const desc, unsigned int len, \
  224. u32 options) \
  225. { \
  226. PRINT_POS; \
  227. append_cmd(desc, CMD_##op | len | options); \
  228. }
  229. APPEND_CMD_LEN(seq_load, SEQ_LOAD)
  230. APPEND_CMD_LEN(seq_store, SEQ_STORE)
  231. APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
  232. APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
  233. #define APPEND_CMD_PTR(cmd, op) \
  234. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  235. unsigned int len, u32 options) \
  236. { \
  237. PRINT_POS; \
  238. append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
  239. }
  240. APPEND_CMD_PTR(key, KEY)
  241. APPEND_CMD_PTR(load, LOAD)
  242. APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
  243. APPEND_CMD_PTR(fifo_store, FIFO_STORE)
  244. static inline void append_store(u32 * const desc, dma_addr_t ptr,
  245. unsigned int len, u32 options)
  246. {
  247. u32 cmd_src;
  248. cmd_src = options & LDST_SRCDST_MASK;
  249. append_cmd(desc, CMD_STORE | options | len);
  250. /* The following options do not require pointer */
  251. if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
  252. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
  253. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
  254. cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
  255. append_ptr(desc, ptr);
  256. }
  257. #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
  258. static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \
  259. dma_addr_t ptr, \
  260. unsigned int len, \
  261. u32 options) \
  262. { \
  263. PRINT_POS; \
  264. if (options & (SQIN_RTO | SQIN_PRE)) \
  265. append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
  266. else \
  267. append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
  268. }
  269. APPEND_SEQ_PTR_INTLEN(in, IN)
  270. APPEND_SEQ_PTR_INTLEN(out, OUT)
  271. #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
  272. static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
  273. unsigned int len, u32 options) \
  274. { \
  275. PRINT_POS; \
  276. append_cmd_data(desc, data, len, CMD_##op | options); \
  277. }
  278. APPEND_CMD_PTR_TO_IMM(load, LOAD);
  279. APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
  280. #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
  281. static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \
  282. unsigned int len, u32 options) \
  283. { \
  284. PRINT_POS; \
  285. append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
  286. }
  287. APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
  288. APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
  289. /*
  290. * Determine whether to store length internally or externally depending on
  291. * the size of its type
  292. */
  293. #define APPEND_CMD_PTR_LEN(cmd, op, type) \
  294. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  295. type len, u32 options) \
  296. { \
  297. PRINT_POS; \
  298. if (sizeof(type) > sizeof(u16)) \
  299. append_##cmd##_extlen(desc, ptr, len, options); \
  300. else \
  301. append_##cmd##_intlen(desc, ptr, len, options); \
  302. }
  303. APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
  304. APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
  305. /*
  306. * 2nd variant for commands whose specified immediate length differs
  307. * from length of immediate data provided, e.g., split keys
  308. */
  309. #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
  310. static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
  311. unsigned int data_len, \
  312. unsigned int len, u32 options) \
  313. { \
  314. PRINT_POS; \
  315. append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
  316. append_data(desc, data, data_len); \
  317. }
  318. APPEND_CMD_PTR_TO_IMM2(key, KEY);
  319. #define APPEND_CMD_RAW_IMM(cmd, op, type) \
  320. static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \
  321. u32 options) \
  322. { \
  323. PRINT_POS; \
  324. if (options & LDST_LEN_MASK) \
  325. append_cmd(desc, CMD_##op | IMMEDIATE | options); \
  326. else \
  327. append_cmd(desc, CMD_##op | IMMEDIATE | options | \
  328. sizeof(type)); \
  329. append_cmd(desc, immediate); \
  330. }
  331. APPEND_CMD_RAW_IMM(load, LOAD, u32);
  332. /*
  333. * ee - endianness
  334. * size - size of immediate type in bytes
  335. */
  336. #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \
  337. static inline void append_##cmd##_imm_##ee##size(u32 *desc, \
  338. u##size immediate, \
  339. u32 options) \
  340. { \
  341. __##ee##size data = cpu_to_##ee##size(immediate); \
  342. PRINT_POS; \
  343. append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \
  344. append_data(desc, &data, sizeof(data)); \
  345. }
  346. APPEND_CMD_RAW_IMM2(load, LOAD, be, 32);
  347. /*
  348. * Append math command. Only the last part of destination and source need to
  349. * be specified
  350. */
  351. #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
  352. append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
  353. MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
  354. #define append_math_add(desc, dest, src0, src1, len) \
  355. APPEND_MATH(ADD, desc, dest, src0, src1, len)
  356. #define append_math_sub(desc, dest, src0, src1, len) \
  357. APPEND_MATH(SUB, desc, dest, src0, src1, len)
  358. #define append_math_add_c(desc, dest, src0, src1, len) \
  359. APPEND_MATH(ADDC, desc, dest, src0, src1, len)
  360. #define append_math_sub_b(desc, dest, src0, src1, len) \
  361. APPEND_MATH(SUBB, desc, dest, src0, src1, len)
  362. #define append_math_and(desc, dest, src0, src1, len) \
  363. APPEND_MATH(AND, desc, dest, src0, src1, len)
  364. #define append_math_or(desc, dest, src0, src1, len) \
  365. APPEND_MATH(OR, desc, dest, src0, src1, len)
  366. #define append_math_xor(desc, dest, src0, src1, len) \
  367. APPEND_MATH(XOR, desc, dest, src0, src1, len)
  368. #define append_math_lshift(desc, dest, src0, src1, len) \
  369. APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
  370. #define append_math_rshift(desc, dest, src0, src1, len) \
  371. APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
  372. #define append_math_ldshift(desc, dest, src0, src1, len) \
  373. APPEND_MATH(SHLD, desc, dest, src0, src1, len)
  374. /* Exactly one source is IMM. Data is passed in as u32 value */
  375. #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
  376. do { \
  377. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
  378. append_cmd(desc, data); \
  379. } while (0)
  380. #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
  381. APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
  382. #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
  383. APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
  384. #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
  385. APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
  386. #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
  387. APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
  388. #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
  389. APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
  390. #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
  391. APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
  392. #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
  393. APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
  394. #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
  395. APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
  396. #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
  397. APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
  398. /* Exactly one source is IMM. Data is passed in as u64 value */
  399. #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
  400. do { \
  401. u32 upper = (data >> 16) >> 16; \
  402. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
  403. (upper ? 0 : MATH_IFB)); \
  404. if (upper) \
  405. append_u64(desc, data); \
  406. else \
  407. append_u32(desc, lower_32_bits(data)); \
  408. } while (0)
  409. #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
  410. APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
  411. #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
  412. APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
  413. #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
  414. APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
  415. #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
  416. APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
  417. #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
  418. APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
  419. #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
  420. APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
  421. #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
  422. APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
  423. #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
  424. APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
  425. #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
  426. APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
  427. /**
  428. * struct alginfo - Container for algorithm details
  429. * @algtype: algorithm selector; for valid values, see documentation of the
  430. * functions where it is used.
  431. * @keylen: length of the provided algorithm key, in bytes
  432. * @keylen_pad: padded length of the provided algorithm key, in bytes
  433. * @key_dma: dma (bus) address where algorithm key resides
  434. * @key_virt: virtual address where algorithm key resides
  435. * @key_inline: true - key can be inlined in the descriptor; false - key is
  436. * referenced by the descriptor
  437. */
  438. struct alginfo {
  439. u32 algtype;
  440. unsigned int keylen;
  441. unsigned int keylen_pad;
  442. dma_addr_t key_dma;
  443. const void *key_virt;
  444. bool key_inline;
  445. };
  446. /**
  447. * desc_inline_query() - Provide indications on which data items can be inlined
  448. * and which shall be referenced in a shared descriptor.
  449. * @sd_base_len: Shared descriptor base length - bytes consumed by the commands,
  450. * excluding the data items to be inlined (or corresponding
  451. * pointer if an item is not inlined). Each cnstr_* function that
  452. * generates descriptors should have a define mentioning
  453. * corresponding length.
  454. * @jd_len: Maximum length of the job descriptor(s) that will be used
  455. * together with the shared descriptor.
  456. * @data_len: Array of lengths of the data items trying to be inlined
  457. * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0
  458. * otherwise.
  459. * @count: Number of data items (size of @data_len array); must be <= 32
  460. *
  461. * Return: 0 if data can be inlined / referenced, negative value if not. If 0,
  462. * check @inl_mask for details.
  463. */
  464. static inline int desc_inline_query(unsigned int sd_base_len,
  465. unsigned int jd_len, unsigned int *data_len,
  466. u32 *inl_mask, unsigned int count)
  467. {
  468. int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len);
  469. unsigned int i;
  470. *inl_mask = 0;
  471. for (i = 0; (i < count) && (rem_bytes > 0); i++) {
  472. if (rem_bytes - (int)(data_len[i] +
  473. (count - i - 1) * CAAM_PTR_SZ) >= 0) {
  474. rem_bytes -= data_len[i];
  475. *inl_mask |= (1 << i);
  476. } else {
  477. rem_bytes -= CAAM_PTR_SZ;
  478. }
  479. }
  480. return (rem_bytes >= 0) ? 0 : -1;
  481. }
  482. /**
  483. * append_proto_dkp - Derived Key Protocol (DKP): key -> split key
  484. * @desc: pointer to buffer used for descriptor construction
  485. * @adata: pointer to authentication transform definitions.
  486. * keylen should be the length of initial key, while keylen_pad
  487. * the length of the derived (split) key.
  488. * Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
  489. * SHA256, SHA384, SHA512}.
  490. */
  491. static inline void append_proto_dkp(u32 * const desc, struct alginfo *adata)
  492. {
  493. u32 protid;
  494. /*
  495. * Quick & dirty translation from OP_ALG_ALGSEL_{MD5, SHA*}
  496. * to OP_PCLID_DKP_{MD5, SHA*}
  497. */
  498. protid = (adata->algtype & OP_ALG_ALGSEL_SUBMASK) |
  499. (0x20 << OP_ALG_ALGSEL_SHIFT);
  500. if (adata->key_inline) {
  501. int words;
  502. if (adata->keylen > adata->keylen_pad) {
  503. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  504. OP_PCL_DKP_SRC_PTR |
  505. OP_PCL_DKP_DST_IMM | adata->keylen);
  506. append_ptr(desc, adata->key_dma);
  507. words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
  508. CAAM_PTR_SZ) / CAAM_CMD_SZ;
  509. } else {
  510. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  511. OP_PCL_DKP_SRC_IMM |
  512. OP_PCL_DKP_DST_IMM | adata->keylen);
  513. append_data(desc, adata->key_virt, adata->keylen);
  514. words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
  515. ALIGN(adata->keylen, CAAM_CMD_SZ)) /
  516. CAAM_CMD_SZ;
  517. }
  518. /* Reserve space in descriptor buffer for the derived key */
  519. if (words)
  520. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + words);
  521. } else {
  522. append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
  523. OP_PCL_DKP_SRC_PTR | OP_PCL_DKP_DST_PTR |
  524. adata->keylen);
  525. append_ptr(desc, adata->key_dma);
  526. }
  527. }
  528. #endif /* DESC_CONSTR_H */