cesa.h 25 KB

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