cesa.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __MARVELL_CESA_H__
  3. #define __MARVELL_CESA_H__
  4. #include <crypto/algapi.h>
  5. #include <crypto/hash.h>
  6. #include <crypto/internal/hash.h>
  7. #include <crypto/internal/skcipher.h>
  8. #include <linux/crypto.h>
  9. #include <linux/dmapool.h>
  10. #define CESA_ENGINE_OFF(i) (((i) * 0x2000))
  11. #define CESA_TDMA_BYTE_CNT 0x800
  12. #define CESA_TDMA_SRC_ADDR 0x810
  13. #define CESA_TDMA_DST_ADDR 0x820
  14. #define CESA_TDMA_NEXT_ADDR 0x830
  15. #define CESA_TDMA_CONTROL 0x840
  16. #define CESA_TDMA_DST_BURST GENMASK(2, 0)
  17. #define CESA_TDMA_DST_BURST_32B 3
  18. #define CESA_TDMA_DST_BURST_128B 4
  19. #define CESA_TDMA_OUT_RD_EN BIT(4)
  20. #define CESA_TDMA_SRC_BURST GENMASK(8, 6)
  21. #define CESA_TDMA_SRC_BURST_32B (3 << 6)
  22. #define CESA_TDMA_SRC_BURST_128B (4 << 6)
  23. #define CESA_TDMA_CHAIN BIT(9)
  24. #define CESA_TDMA_BYTE_SWAP BIT(11)
  25. #define CESA_TDMA_NO_BYTE_SWAP BIT(11)
  26. #define CESA_TDMA_EN BIT(12)
  27. #define CESA_TDMA_FETCH_ND BIT(13)
  28. #define CESA_TDMA_ACT BIT(14)
  29. #define CESA_TDMA_CUR 0x870
  30. #define CESA_TDMA_ERROR_CAUSE 0x8c8
  31. #define CESA_TDMA_ERROR_MSK 0x8cc
  32. #define CESA_TDMA_WINDOW_BASE(x) (((x) * 0x8) + 0xa00)
  33. #define CESA_TDMA_WINDOW_CTRL(x) (((x) * 0x8) + 0xa04)
  34. #define CESA_IVDIG(x) (0xdd00 + ((x) * 4) + \
  35. (((x) < 5) ? 0 : 0x14))
  36. #define CESA_SA_CMD 0xde00
  37. #define CESA_SA_CMD_EN_CESA_SA_ACCL0 BIT(0)
  38. #define CESA_SA_CMD_EN_CESA_SA_ACCL1 BIT(1)
  39. #define CESA_SA_CMD_DISABLE_SEC BIT(2)
  40. #define CESA_SA_DESC_P0 0xde04
  41. #define CESA_SA_DESC_P1 0xde14
  42. #define CESA_SA_CFG 0xde08
  43. #define CESA_SA_CFG_STOP_DIG_ERR GENMASK(1, 0)
  44. #define CESA_SA_CFG_DIG_ERR_CONT 0
  45. #define CESA_SA_CFG_DIG_ERR_SKIP 1
  46. #define CESA_SA_CFG_DIG_ERR_STOP 3
  47. #define CESA_SA_CFG_CH0_W_IDMA BIT(7)
  48. #define CESA_SA_CFG_CH1_W_IDMA BIT(8)
  49. #define CESA_SA_CFG_ACT_CH0_IDMA BIT(9)
  50. #define CESA_SA_CFG_ACT_CH1_IDMA BIT(10)
  51. #define CESA_SA_CFG_MULTI_PKT BIT(11)
  52. #define CESA_SA_CFG_PARA_DIS BIT(13)
  53. #define CESA_SA_ACCEL_STATUS 0xde0c
  54. #define CESA_SA_ST_ACT_0 BIT(0)
  55. #define CESA_SA_ST_ACT_1 BIT(1)
  56. /*
  57. * CESA_SA_FPGA_INT_STATUS looks like a FPGA leftover and is documented only
  58. * in Errata 4.12. It looks like that it was part of an IRQ-controller in FPGA
  59. * and someone forgot to remove it while switching to the core and moving to
  60. * CESA_SA_INT_STATUS.
  61. */
  62. #define CESA_SA_FPGA_INT_STATUS 0xdd68
  63. #define CESA_SA_INT_STATUS 0xde20
  64. #define CESA_SA_INT_AUTH_DONE BIT(0)
  65. #define CESA_SA_INT_DES_E_DONE BIT(1)
  66. #define CESA_SA_INT_AES_E_DONE BIT(2)
  67. #define CESA_SA_INT_AES_D_DONE BIT(3)
  68. #define CESA_SA_INT_ENC_DONE BIT(4)
  69. #define CESA_SA_INT_ACCEL0_DONE BIT(5)
  70. #define CESA_SA_INT_ACCEL1_DONE BIT(6)
  71. #define CESA_SA_INT_ACC0_IDMA_DONE BIT(7)
  72. #define CESA_SA_INT_ACC1_IDMA_DONE BIT(8)
  73. #define CESA_SA_INT_IDMA_DONE BIT(9)
  74. #define CESA_SA_INT_IDMA_OWN_ERR BIT(10)
  75. #define CESA_SA_INT_MSK 0xde24
  76. #define CESA_SA_DESC_CFG_OP_MAC_ONLY 0
  77. #define CESA_SA_DESC_CFG_OP_CRYPT_ONLY 1
  78. #define CESA_SA_DESC_CFG_OP_MAC_CRYPT 2
  79. #define CESA_SA_DESC_CFG_OP_CRYPT_MAC 3
  80. #define CESA_SA_DESC_CFG_OP_MSK GENMASK(1, 0)
  81. #define CESA_SA_DESC_CFG_MACM_SHA256 (1 << 4)
  82. #define CESA_SA_DESC_CFG_MACM_HMAC_SHA256 (3 << 4)
  83. #define CESA_SA_DESC_CFG_MACM_MD5 (4 << 4)
  84. #define CESA_SA_DESC_CFG_MACM_SHA1 (5 << 4)
  85. #define CESA_SA_DESC_CFG_MACM_HMAC_MD5 (6 << 4)
  86. #define CESA_SA_DESC_CFG_MACM_HMAC_SHA1 (7 << 4)
  87. #define CESA_SA_DESC_CFG_MACM_MSK GENMASK(6, 4)
  88. #define CESA_SA_DESC_CFG_CRYPTM_DES (1 << 8)
  89. #define CESA_SA_DESC_CFG_CRYPTM_3DES (2 << 8)
  90. #define CESA_SA_DESC_CFG_CRYPTM_AES (3 << 8)
  91. #define CESA_SA_DESC_CFG_CRYPTM_MSK GENMASK(9, 8)
  92. #define CESA_SA_DESC_CFG_DIR_ENC (0 << 12)
  93. #define CESA_SA_DESC_CFG_DIR_DEC (1 << 12)
  94. #define CESA_SA_DESC_CFG_CRYPTCM_ECB (0 << 16)
  95. #define CESA_SA_DESC_CFG_CRYPTCM_CBC (1 << 16)
  96. #define CESA_SA_DESC_CFG_CRYPTCM_MSK BIT(16)
  97. #define CESA_SA_DESC_CFG_3DES_EEE (0 << 20)
  98. #define CESA_SA_DESC_CFG_3DES_EDE (1 << 20)
  99. #define CESA_SA_DESC_CFG_AES_LEN_128 (0 << 24)
  100. #define CESA_SA_DESC_CFG_AES_LEN_192 (1 << 24)
  101. #define CESA_SA_DESC_CFG_AES_LEN_256 (2 << 24)
  102. #define CESA_SA_DESC_CFG_AES_LEN_MSK GENMASK(25, 24)
  103. #define CESA_SA_DESC_CFG_NOT_FRAG (0 << 30)
  104. #define CESA_SA_DESC_CFG_FIRST_FRAG (1 << 30)
  105. #define CESA_SA_DESC_CFG_LAST_FRAG (2 << 30)
  106. #define CESA_SA_DESC_CFG_MID_FRAG (3 << 30)
  107. #define CESA_SA_DESC_CFG_FRAG_MSK GENMASK(31, 30)
  108. /*
  109. * /-----------\ 0
  110. * | ACCEL CFG | 4 * 8
  111. * |-----------| 0x20
  112. * | CRYPT KEY | 8 * 4
  113. * |-----------| 0x40
  114. * | IV IN | 4 * 4
  115. * |-----------| 0x40 (inplace)
  116. * | IV BUF | 4 * 4
  117. * |-----------| 0x80
  118. * | DATA IN | 16 * x (max ->max_req_size)
  119. * |-----------| 0x80 (inplace operation)
  120. * | DATA OUT | 16 * x (max ->max_req_size)
  121. * \-----------/ SRAM size
  122. */
  123. /*
  124. * Hashing memory map:
  125. * /-----------\ 0
  126. * | ACCEL CFG | 4 * 8
  127. * |-----------| 0x20
  128. * | Inner IV | 8 * 4
  129. * |-----------| 0x40
  130. * | Outer IV | 8 * 4
  131. * |-----------| 0x60
  132. * | Output BUF| 8 * 4
  133. * |-----------| 0x80
  134. * | DATA IN | 64 * x (max ->max_req_size)
  135. * \-----------/ SRAM size
  136. */
  137. #define CESA_SA_CFG_SRAM_OFFSET 0x00
  138. #define CESA_SA_DATA_SRAM_OFFSET 0x80
  139. #define CESA_SA_CRYPT_KEY_SRAM_OFFSET 0x20
  140. #define CESA_SA_CRYPT_IV_SRAM_OFFSET 0x40
  141. #define CESA_SA_MAC_IIV_SRAM_OFFSET 0x20
  142. #define CESA_SA_MAC_OIV_SRAM_OFFSET 0x40
  143. #define CESA_SA_MAC_DIG_SRAM_OFFSET 0x60
  144. #define CESA_SA_DESC_CRYPT_DATA(offset) \
  145. cpu_to_le32((CESA_SA_DATA_SRAM_OFFSET + (offset)) | \
  146. ((CESA_SA_DATA_SRAM_OFFSET + (offset)) << 16))
  147. #define CESA_SA_DESC_CRYPT_IV(offset) \
  148. cpu_to_le32((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) | \
  149. ((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) << 16))
  150. #define CESA_SA_DESC_CRYPT_KEY(offset) \
  151. cpu_to_le32(CESA_SA_CRYPT_KEY_SRAM_OFFSET + (offset))
  152. #define CESA_SA_DESC_MAC_DATA(offset) \
  153. cpu_to_le32(CESA_SA_DATA_SRAM_OFFSET + (offset))
  154. #define CESA_SA_DESC_MAC_DATA_MSK cpu_to_le32(GENMASK(15, 0))
  155. #define CESA_SA_DESC_MAC_TOTAL_LEN(total_len) cpu_to_le32((total_len) << 16)
  156. #define CESA_SA_DESC_MAC_TOTAL_LEN_MSK cpu_to_le32(GENMASK(31, 16))
  157. #define CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX 0xffff
  158. #define CESA_SA_DESC_MAC_DIGEST(offset) \
  159. cpu_to_le32(CESA_SA_MAC_DIG_SRAM_OFFSET + (offset))
  160. #define CESA_SA_DESC_MAC_DIGEST_MSK cpu_to_le32(GENMASK(15, 0))
  161. #define CESA_SA_DESC_MAC_FRAG_LEN(frag_len) cpu_to_le32((frag_len) << 16)
  162. #define CESA_SA_DESC_MAC_FRAG_LEN_MSK cpu_to_le32(GENMASK(31, 16))
  163. #define CESA_SA_DESC_MAC_IV(offset) \
  164. cpu_to_le32((CESA_SA_MAC_IIV_SRAM_OFFSET + (offset)) | \
  165. ((CESA_SA_MAC_OIV_SRAM_OFFSET + (offset)) << 16))
  166. #define CESA_SA_SRAM_SIZE 2048
  167. #define CESA_SA_SRAM_PAYLOAD_SIZE (cesa_dev->sram_size - \
  168. CESA_SA_DATA_SRAM_OFFSET)
  169. #define CESA_SA_DEFAULT_SRAM_SIZE 2048
  170. #define CESA_SA_MIN_SRAM_SIZE 1024
  171. #define CESA_SA_SRAM_MSK (2048 - 1)
  172. #define CESA_MAX_HASH_BLOCK_SIZE 64
  173. #define CESA_HASH_BLOCK_SIZE_MSK (CESA_MAX_HASH_BLOCK_SIZE - 1)
  174. /**
  175. * struct mv_cesa_sec_accel_desc - security accelerator descriptor
  176. * @config: engine config
  177. * @enc_p: input and output data pointers for a cipher operation
  178. * @enc_len: cipher operation length
  179. * @enc_key_p: cipher key pointer
  180. * @enc_iv: cipher IV pointers
  181. * @mac_src_p: input pointer and total hash length
  182. * @mac_digest: digest pointer and hash operation length
  183. * @mac_iv: hmac IV pointers
  184. *
  185. * Structure passed to the CESA engine to describe the crypto operation
  186. * to be executed.
  187. */
  188. struct mv_cesa_sec_accel_desc {
  189. __le32 config;
  190. __le32 enc_p;
  191. __le32 enc_len;
  192. __le32 enc_key_p;
  193. __le32 enc_iv;
  194. __le32 mac_src_p;
  195. __le32 mac_digest;
  196. __le32 mac_iv;
  197. };
  198. /**
  199. * struct mv_cesa_blkcipher_op_ctx - cipher operation context
  200. * @key: cipher key
  201. * @iv: cipher IV
  202. *
  203. * Context associated to a cipher operation.
  204. */
  205. struct mv_cesa_blkcipher_op_ctx {
  206. u32 key[8];
  207. u32 iv[4];
  208. };
  209. /**
  210. * struct mv_cesa_hash_op_ctx - hash or hmac operation context
  211. * @key: cipher key
  212. * @iv: cipher IV
  213. *
  214. * Context associated to an hash or hmac operation.
  215. */
  216. struct mv_cesa_hash_op_ctx {
  217. u32 iv[16];
  218. u32 hash[8];
  219. };
  220. /**
  221. * struct mv_cesa_op_ctx - crypto operation context
  222. * @desc: CESA descriptor
  223. * @ctx: context associated to the crypto operation
  224. *
  225. * Context associated to a crypto operation.
  226. */
  227. struct mv_cesa_op_ctx {
  228. struct mv_cesa_sec_accel_desc desc;
  229. union {
  230. struct mv_cesa_blkcipher_op_ctx blkcipher;
  231. struct mv_cesa_hash_op_ctx hash;
  232. } ctx;
  233. };
  234. /* TDMA descriptor flags */
  235. #define CESA_TDMA_DST_IN_SRAM BIT(31)
  236. #define CESA_TDMA_SRC_IN_SRAM BIT(30)
  237. #define CESA_TDMA_END_OF_REQ BIT(29)
  238. #define CESA_TDMA_BREAK_CHAIN BIT(28)
  239. #define CESA_TDMA_SET_STATE BIT(27)
  240. #define CESA_TDMA_TYPE_MSK GENMASK(26, 0)
  241. #define CESA_TDMA_DUMMY 0
  242. #define CESA_TDMA_DATA 1
  243. #define CESA_TDMA_OP 2
  244. #define CESA_TDMA_RESULT 3
  245. /**
  246. * struct mv_cesa_tdma_desc - TDMA descriptor
  247. * @byte_cnt: number of bytes to transfer
  248. * @src: DMA address of the source
  249. * @dst: DMA address of the destination
  250. * @next_dma: DMA address of the next TDMA descriptor
  251. * @cur_dma: DMA address of this TDMA descriptor
  252. * @next: pointer to the next TDMA descriptor
  253. * @op: CESA operation attached to this TDMA descriptor
  254. * @data: raw data attached to this TDMA descriptor
  255. * @flags: flags describing the TDMA transfer. See the
  256. * "TDMA descriptor flags" section above
  257. *
  258. * TDMA descriptor used to create a transfer chain describing a crypto
  259. * operation.
  260. */
  261. struct mv_cesa_tdma_desc {
  262. __le32 byte_cnt;
  263. __le32 src;
  264. __le32 dst;
  265. __le32 next_dma;
  266. /* Software state */
  267. dma_addr_t cur_dma;
  268. struct mv_cesa_tdma_desc *next;
  269. union {
  270. struct mv_cesa_op_ctx *op;
  271. void *data;
  272. };
  273. u32 flags;
  274. };
  275. /**
  276. * struct mv_cesa_sg_dma_iter - scatter-gather iterator
  277. * @dir: transfer direction
  278. * @sg: scatter list
  279. * @offset: current position in the scatter list
  280. * @op_offset: current position in the crypto operation
  281. *
  282. * Iterator used to iterate over a scatterlist while creating a TDMA chain for
  283. * a crypto operation.
  284. */
  285. struct mv_cesa_sg_dma_iter {
  286. enum dma_data_direction dir;
  287. struct scatterlist *sg;
  288. unsigned int offset;
  289. unsigned int op_offset;
  290. };
  291. /**
  292. * struct mv_cesa_dma_iter - crypto operation iterator
  293. * @len: the crypto operation length
  294. * @offset: current position in the crypto operation
  295. * @op_len: sub-operation length (the crypto engine can only act on 2kb
  296. * chunks)
  297. *
  298. * Iterator used to create a TDMA chain for a given crypto operation.
  299. */
  300. struct mv_cesa_dma_iter {
  301. unsigned int len;
  302. unsigned int offset;
  303. unsigned int op_len;
  304. };
  305. /**
  306. * struct mv_cesa_tdma_chain - TDMA chain
  307. * @first: first entry in the TDMA chain
  308. * @last: last entry in the TDMA chain
  309. *
  310. * Stores a TDMA chain for a specific crypto operation.
  311. */
  312. struct mv_cesa_tdma_chain {
  313. struct mv_cesa_tdma_desc *first;
  314. struct mv_cesa_tdma_desc *last;
  315. };
  316. struct mv_cesa_engine;
  317. /**
  318. * struct mv_cesa_caps - CESA device capabilities
  319. * @engines: number of engines
  320. * @has_tdma: whether this device has a TDMA block
  321. * @cipher_algs: supported cipher algorithms
  322. * @ncipher_algs: number of supported cipher algorithms
  323. * @ahash_algs: supported hash algorithms
  324. * @nahash_algs: number of supported hash algorithms
  325. *
  326. * Structure used to describe CESA device capabilities.
  327. */
  328. struct mv_cesa_caps {
  329. int nengines;
  330. bool has_tdma;
  331. struct skcipher_alg **cipher_algs;
  332. int ncipher_algs;
  333. struct ahash_alg **ahash_algs;
  334. int nahash_algs;
  335. };
  336. /**
  337. * struct mv_cesa_dev_dma - DMA pools
  338. * @tdma_desc_pool: TDMA desc pool
  339. * @op_pool: crypto operation pool
  340. * @cache_pool: data cache pool (used by hash implementation when the
  341. * hash request is smaller than the hash block size)
  342. * @padding_pool: padding pool (used by hash implementation when hardware
  343. * padding cannot be used)
  344. *
  345. * Structure containing the different DMA pools used by this driver.
  346. */
  347. struct mv_cesa_dev_dma {
  348. struct dma_pool *tdma_desc_pool;
  349. struct dma_pool *op_pool;
  350. struct dma_pool *cache_pool;
  351. struct dma_pool *padding_pool;
  352. };
  353. /**
  354. * struct mv_cesa_dev - CESA device
  355. * @caps: device capabilities
  356. * @regs: device registers
  357. * @sram_size: usable SRAM size
  358. * @lock: device lock
  359. * @engines: array of engines
  360. * @dma: dma pools
  361. *
  362. * Structure storing CESA device information.
  363. */
  364. struct mv_cesa_dev {
  365. const struct mv_cesa_caps *caps;
  366. void __iomem *regs;
  367. struct device *dev;
  368. unsigned int sram_size;
  369. spinlock_t lock;
  370. struct mv_cesa_engine *engines;
  371. struct mv_cesa_dev_dma *dma;
  372. };
  373. /**
  374. * struct mv_cesa_engine - CESA engine
  375. * @id: engine id
  376. * @regs: engine registers
  377. * @sram: SRAM memory region
  378. * @sram_dma: DMA address of the SRAM memory region
  379. * @lock: engine lock
  380. * @req: current crypto request
  381. * @clk: engine clk
  382. * @zclk: engine zclk
  383. * @max_req_len: maximum chunk length (useful to create the TDMA chain)
  384. * @int_mask: interrupt mask cache
  385. * @pool: memory pool pointing to the memory region reserved in
  386. * SRAM
  387. * @queue: fifo of the pending crypto requests
  388. * @load: engine load counter, useful for load balancing
  389. * @chain: list of the current tdma descriptors being processed
  390. * by this engine.
  391. * @complete_queue: fifo of the processed requests by the engine
  392. *
  393. * Structure storing CESA engine information.
  394. */
  395. struct mv_cesa_engine {
  396. int id;
  397. void __iomem *regs;
  398. void __iomem *sram;
  399. dma_addr_t sram_dma;
  400. spinlock_t lock;
  401. struct crypto_async_request *req;
  402. struct clk *clk;
  403. struct clk *zclk;
  404. size_t max_req_len;
  405. u32 int_mask;
  406. struct gen_pool *pool;
  407. struct crypto_queue queue;
  408. atomic_t load;
  409. struct mv_cesa_tdma_chain chain;
  410. struct list_head complete_queue;
  411. };
  412. /**
  413. * struct mv_cesa_req_ops - CESA request operations
  414. * @process: process a request chunk result (should return 0 if the
  415. * operation, -EINPROGRESS if it needs more steps or an error
  416. * code)
  417. * @step: launch the crypto operation on the next chunk
  418. * @cleanup: cleanup the crypto request (release associated data)
  419. * @complete: complete the request, i.e copy result or context from sram when
  420. * needed.
  421. */
  422. struct mv_cesa_req_ops {
  423. int (*process)(struct crypto_async_request *req, u32 status);
  424. void (*step)(struct crypto_async_request *req);
  425. void (*cleanup)(struct crypto_async_request *req);
  426. void (*complete)(struct crypto_async_request *req);
  427. };
  428. /**
  429. * struct mv_cesa_ctx - CESA operation context
  430. * @ops: crypto operations
  431. *
  432. * Base context structure inherited by operation specific ones.
  433. */
  434. struct mv_cesa_ctx {
  435. const struct mv_cesa_req_ops *ops;
  436. };
  437. /**
  438. * struct mv_cesa_hash_ctx - CESA hash operation context
  439. * @base: base context structure
  440. *
  441. * Hash context structure.
  442. */
  443. struct mv_cesa_hash_ctx {
  444. struct mv_cesa_ctx base;
  445. };
  446. /**
  447. * struct mv_cesa_hash_ctx - CESA hmac operation context
  448. * @base: base context structure
  449. * @iv: initialization vectors
  450. *
  451. * HMAC context structure.
  452. */
  453. struct mv_cesa_hmac_ctx {
  454. struct mv_cesa_ctx base;
  455. u32 iv[16];
  456. };
  457. /**
  458. * enum mv_cesa_req_type - request type definitions
  459. * @CESA_STD_REQ: standard request
  460. * @CESA_DMA_REQ: DMA request
  461. */
  462. enum mv_cesa_req_type {
  463. CESA_STD_REQ,
  464. CESA_DMA_REQ,
  465. };
  466. /**
  467. * struct mv_cesa_req - CESA request
  468. * @engine: engine associated with this request
  469. * @chain: list of tdma descriptors associated with this request
  470. */
  471. struct mv_cesa_req {
  472. struct mv_cesa_engine *engine;
  473. struct mv_cesa_tdma_chain chain;
  474. };
  475. /**
  476. * struct mv_cesa_sg_std_iter - CESA scatter-gather iterator for standard
  477. * requests
  478. * @iter: sg mapping iterator
  479. * @offset: current offset in the SG entry mapped in memory
  480. */
  481. struct mv_cesa_sg_std_iter {
  482. struct sg_mapping_iter iter;
  483. unsigned int offset;
  484. };
  485. /**
  486. * struct mv_cesa_skcipher_std_req - cipher standard request
  487. * @op: operation context
  488. * @offset: current operation offset
  489. * @size: size of the crypto operation
  490. */
  491. struct mv_cesa_skcipher_std_req {
  492. struct mv_cesa_op_ctx op;
  493. unsigned int offset;
  494. unsigned int size;
  495. bool skip_ctx;
  496. };
  497. /**
  498. * struct mv_cesa_skcipher_req - cipher request
  499. * @req: type specific request information
  500. * @src_nents: number of entries in the src sg list
  501. * @dst_nents: number of entries in the dest sg list
  502. */
  503. struct mv_cesa_skcipher_req {
  504. struct mv_cesa_req base;
  505. struct mv_cesa_skcipher_std_req std;
  506. int src_nents;
  507. int dst_nents;
  508. };
  509. /**
  510. * struct mv_cesa_ahash_std_req - standard hash request
  511. * @offset: current operation offset
  512. */
  513. struct mv_cesa_ahash_std_req {
  514. unsigned int offset;
  515. };
  516. /**
  517. * struct mv_cesa_ahash_dma_req - DMA hash request
  518. * @padding: padding buffer
  519. * @padding_dma: DMA address of the padding buffer
  520. * @cache_dma: DMA address of the cache buffer
  521. */
  522. struct mv_cesa_ahash_dma_req {
  523. u8 *padding;
  524. dma_addr_t padding_dma;
  525. u8 *cache;
  526. dma_addr_t cache_dma;
  527. };
  528. /**
  529. * struct mv_cesa_ahash_req - hash request
  530. * @req: type specific request information
  531. * @cache: cache buffer
  532. * @cache_ptr: write pointer in the cache buffer
  533. * @len: hash total length
  534. * @src_nents: number of entries in the scatterlist
  535. * @last_req: define whether the current operation is the last one
  536. * or not
  537. * @state: hash state
  538. */
  539. struct mv_cesa_ahash_req {
  540. struct mv_cesa_req base;
  541. union {
  542. struct mv_cesa_ahash_dma_req dma;
  543. struct mv_cesa_ahash_std_req std;
  544. } req;
  545. struct mv_cesa_op_ctx op_tmpl;
  546. u8 cache[CESA_MAX_HASH_BLOCK_SIZE];
  547. unsigned int cache_ptr;
  548. u64 len;
  549. int src_nents;
  550. bool last_req;
  551. bool algo_le;
  552. u32 state[8];
  553. };
  554. /* CESA functions */
  555. extern struct mv_cesa_dev *cesa_dev;
  556. static inline void
  557. mv_cesa_engine_enqueue_complete_request(struct mv_cesa_engine *engine,
  558. struct crypto_async_request *req)
  559. {
  560. list_add_tail(&req->list, &engine->complete_queue);
  561. }
  562. static inline struct crypto_async_request *
  563. mv_cesa_engine_dequeue_complete_request(struct mv_cesa_engine *engine)
  564. {
  565. struct crypto_async_request *req;
  566. req = list_first_entry_or_null(&engine->complete_queue,
  567. struct crypto_async_request,
  568. list);
  569. if (req)
  570. list_del(&req->list);
  571. return req;
  572. }
  573. static inline enum mv_cesa_req_type
  574. mv_cesa_req_get_type(struct mv_cesa_req *req)
  575. {
  576. return req->chain.first ? CESA_DMA_REQ : CESA_STD_REQ;
  577. }
  578. static inline void mv_cesa_update_op_cfg(struct mv_cesa_op_ctx *op,
  579. u32 cfg, u32 mask)
  580. {
  581. op->desc.config &= cpu_to_le32(~mask);
  582. op->desc.config |= cpu_to_le32(cfg);
  583. }
  584. static inline u32 mv_cesa_get_op_cfg(const struct mv_cesa_op_ctx *op)
  585. {
  586. return le32_to_cpu(op->desc.config);
  587. }
  588. static inline void mv_cesa_set_op_cfg(struct mv_cesa_op_ctx *op, u32 cfg)
  589. {
  590. op->desc.config = cpu_to_le32(cfg);
  591. }
  592. static inline void mv_cesa_adjust_op(struct mv_cesa_engine *engine,
  593. struct mv_cesa_op_ctx *op)
  594. {
  595. u32 offset = engine->sram_dma & CESA_SA_SRAM_MSK;
  596. op->desc.enc_p = CESA_SA_DESC_CRYPT_DATA(offset);
  597. op->desc.enc_key_p = CESA_SA_DESC_CRYPT_KEY(offset);
  598. op->desc.enc_iv = CESA_SA_DESC_CRYPT_IV(offset);
  599. op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_DATA_MSK;
  600. op->desc.mac_src_p |= CESA_SA_DESC_MAC_DATA(offset);
  601. op->desc.mac_digest &= ~CESA_SA_DESC_MAC_DIGEST_MSK;
  602. op->desc.mac_digest |= CESA_SA_DESC_MAC_DIGEST(offset);
  603. op->desc.mac_iv = CESA_SA_DESC_MAC_IV(offset);
  604. }
  605. static inline void mv_cesa_set_crypt_op_len(struct mv_cesa_op_ctx *op, int len)
  606. {
  607. op->desc.enc_len = cpu_to_le32(len);
  608. }
  609. static inline void mv_cesa_set_mac_op_total_len(struct mv_cesa_op_ctx *op,
  610. int len)
  611. {
  612. op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_TOTAL_LEN_MSK;
  613. op->desc.mac_src_p |= CESA_SA_DESC_MAC_TOTAL_LEN(len);
  614. }
  615. static inline void mv_cesa_set_mac_op_frag_len(struct mv_cesa_op_ctx *op,
  616. int len)
  617. {
  618. op->desc.mac_digest &= ~CESA_SA_DESC_MAC_FRAG_LEN_MSK;
  619. op->desc.mac_digest |= CESA_SA_DESC_MAC_FRAG_LEN(len);
  620. }
  621. static inline void mv_cesa_set_int_mask(struct mv_cesa_engine *engine,
  622. u32 int_mask)
  623. {
  624. if (int_mask == engine->int_mask)
  625. return;
  626. writel_relaxed(int_mask, engine->regs + CESA_SA_INT_MSK);
  627. engine->int_mask = int_mask;
  628. }
  629. static inline u32 mv_cesa_get_int_mask(struct mv_cesa_engine *engine)
  630. {
  631. return engine->int_mask;
  632. }
  633. static inline bool mv_cesa_mac_op_is_first_frag(const struct mv_cesa_op_ctx *op)
  634. {
  635. return (mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK) ==
  636. CESA_SA_DESC_CFG_FIRST_FRAG;
  637. }
  638. int mv_cesa_queue_req(struct crypto_async_request *req,
  639. struct mv_cesa_req *creq);
  640. struct crypto_async_request *
  641. mv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine,
  642. struct crypto_async_request **backlog);
  643. static inline struct mv_cesa_engine *mv_cesa_select_engine(int weight)
  644. {
  645. int i;
  646. u32 min_load = U32_MAX;
  647. struct mv_cesa_engine *selected = NULL;
  648. for (i = 0; i < cesa_dev->caps->nengines; i++) {
  649. struct mv_cesa_engine *engine = cesa_dev->engines + i;
  650. u32 load = atomic_read(&engine->load);
  651. if (load < min_load) {
  652. min_load = load;
  653. selected = engine;
  654. }
  655. }
  656. atomic_add(weight, &selected->load);
  657. return selected;
  658. }
  659. /*
  660. * Helper function that indicates whether a crypto request needs to be
  661. * cleaned up or not after being enqueued using mv_cesa_queue_req().
  662. */
  663. static inline int mv_cesa_req_needs_cleanup(struct crypto_async_request *req,
  664. int ret)
  665. {
  666. /*
  667. * The queue still had some space, the request was queued
  668. * normally, so there's no need to clean it up.
  669. */
  670. if (ret == -EINPROGRESS)
  671. return false;
  672. /*
  673. * The queue had not space left, but since the request is
  674. * flagged with CRYPTO_TFM_REQ_MAY_BACKLOG, it was added to
  675. * the backlog and will be processed later. There's no need to
  676. * clean it up.
  677. */
  678. if (ret == -EBUSY)
  679. return false;
  680. /* Request wasn't queued, we need to clean it up */
  681. return true;
  682. }
  683. /* TDMA functions */
  684. static inline void mv_cesa_req_dma_iter_init(struct mv_cesa_dma_iter *iter,
  685. unsigned int len)
  686. {
  687. iter->len = len;
  688. iter->op_len = min(len, CESA_SA_SRAM_PAYLOAD_SIZE);
  689. iter->offset = 0;
  690. }
  691. static inline void mv_cesa_sg_dma_iter_init(struct mv_cesa_sg_dma_iter *iter,
  692. struct scatterlist *sg,
  693. enum dma_data_direction dir)
  694. {
  695. iter->op_offset = 0;
  696. iter->offset = 0;
  697. iter->sg = sg;
  698. iter->dir = dir;
  699. }
  700. static inline unsigned int
  701. mv_cesa_req_dma_iter_transfer_len(struct mv_cesa_dma_iter *iter,
  702. struct mv_cesa_sg_dma_iter *sgiter)
  703. {
  704. return min(iter->op_len - sgiter->op_offset,
  705. sg_dma_len(sgiter->sg) - sgiter->offset);
  706. }
  707. bool mv_cesa_req_dma_iter_next_transfer(struct mv_cesa_dma_iter *chain,
  708. struct mv_cesa_sg_dma_iter *sgiter,
  709. unsigned int len);
  710. static inline bool mv_cesa_req_dma_iter_next_op(struct mv_cesa_dma_iter *iter)
  711. {
  712. iter->offset += iter->op_len;
  713. iter->op_len = min(iter->len - iter->offset,
  714. CESA_SA_SRAM_PAYLOAD_SIZE);
  715. return iter->op_len;
  716. }
  717. void mv_cesa_dma_step(struct mv_cesa_req *dreq);
  718. static inline int mv_cesa_dma_process(struct mv_cesa_req *dreq,
  719. u32 status)
  720. {
  721. if (!(status & CESA_SA_INT_ACC0_IDMA_DONE))
  722. return -EINPROGRESS;
  723. if (status & CESA_SA_INT_IDMA_OWN_ERR)
  724. return -EINVAL;
  725. return 0;
  726. }
  727. void mv_cesa_dma_prepare(struct mv_cesa_req *dreq,
  728. struct mv_cesa_engine *engine);
  729. void mv_cesa_dma_cleanup(struct mv_cesa_req *dreq);
  730. void mv_cesa_tdma_chain(struct mv_cesa_engine *engine,
  731. struct mv_cesa_req *dreq);
  732. int mv_cesa_tdma_process(struct mv_cesa_engine *engine, u32 status);
  733. static inline void
  734. mv_cesa_tdma_desc_iter_init(struct mv_cesa_tdma_chain *chain)
  735. {
  736. memset(chain, 0, sizeof(*chain));
  737. }
  738. int mv_cesa_dma_add_result_op(struct mv_cesa_tdma_chain *chain, dma_addr_t src,
  739. u32 size, u32 flags, gfp_t gfp_flags);
  740. struct mv_cesa_op_ctx *mv_cesa_dma_add_op(struct mv_cesa_tdma_chain *chain,
  741. const struct mv_cesa_op_ctx *op_templ,
  742. bool skip_ctx,
  743. gfp_t flags);
  744. int mv_cesa_dma_add_data_transfer(struct mv_cesa_tdma_chain *chain,
  745. dma_addr_t dst, dma_addr_t src, u32 size,
  746. u32 flags, gfp_t gfp_flags);
  747. int mv_cesa_dma_add_dummy_launch(struct mv_cesa_tdma_chain *chain, gfp_t flags);
  748. int mv_cesa_dma_add_dummy_end(struct mv_cesa_tdma_chain *chain, gfp_t flags);
  749. int mv_cesa_dma_add_op_transfers(struct mv_cesa_tdma_chain *chain,
  750. struct mv_cesa_dma_iter *dma_iter,
  751. struct mv_cesa_sg_dma_iter *sgiter,
  752. gfp_t gfp_flags);
  753. /* Algorithm definitions */
  754. extern struct ahash_alg mv_md5_alg;
  755. extern struct ahash_alg mv_sha1_alg;
  756. extern struct ahash_alg mv_sha256_alg;
  757. extern struct ahash_alg mv_ahmac_md5_alg;
  758. extern struct ahash_alg mv_ahmac_sha1_alg;
  759. extern struct ahash_alg mv_ahmac_sha256_alg;
  760. extern struct skcipher_alg mv_cesa_ecb_des_alg;
  761. extern struct skcipher_alg mv_cesa_cbc_des_alg;
  762. extern struct skcipher_alg mv_cesa_ecb_des3_ede_alg;
  763. extern struct skcipher_alg mv_cesa_cbc_des3_ede_alg;
  764. extern struct skcipher_alg mv_cesa_ecb_aes_alg;
  765. extern struct skcipher_alg mv_cesa_cbc_aes_alg;
  766. #endif /* __MARVELL_CESA_H__ */