cesa.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #ifndef __MARVELL_CESA_H__
  2. #define __MARVELL_CESA_H__
  3. #include <crypto/algapi.h>
  4. #include <crypto/hash.h>
  5. #include <crypto/internal/hash.h>
  6. #include <linux/crypto.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 a 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_blkcipher_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_blkcipher_op_ctx {
  204. u32 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. u32 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_blkcipher_op_ctx blkcipher;
  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_TYPE_MSK GENMASK(29, 0)
  236. #define CESA_TDMA_DUMMY 0
  237. #define CESA_TDMA_DATA 1
  238. #define CESA_TDMA_OP 2
  239. /**
  240. * struct mv_cesa_tdma_desc - TDMA descriptor
  241. * @byte_cnt: number of bytes to transfer
  242. * @src: DMA address of the source
  243. * @dst: DMA address of the destination
  244. * @next_dma: DMA address of the next TDMA descriptor
  245. * @cur_dma: DMA address of this TDMA descriptor
  246. * @next: pointer to the next TDMA descriptor
  247. * @op: CESA operation attached to this TDMA descriptor
  248. * @data: raw data attached to this TDMA descriptor
  249. * @flags: flags describing the TDMA transfer. See the
  250. * "TDMA descriptor flags" section above
  251. *
  252. * TDMA descriptor used to create a transfer chain describing a crypto
  253. * operation.
  254. */
  255. struct mv_cesa_tdma_desc {
  256. __le32 byte_cnt;
  257. __le32 src;
  258. __le32 dst;
  259. __le32 next_dma;
  260. /* Software state */
  261. dma_addr_t cur_dma;
  262. struct mv_cesa_tdma_desc *next;
  263. union {
  264. struct mv_cesa_op_ctx *op;
  265. void *data;
  266. };
  267. u32 flags;
  268. };
  269. /**
  270. * struct mv_cesa_sg_dma_iter - scatter-gather iterator
  271. * @dir: transfer direction
  272. * @sg: scatter list
  273. * @offset: current position in the scatter list
  274. * @op_offset: current position in the crypto operation
  275. *
  276. * Iterator used to iterate over a scatterlist while creating a TDMA chain for
  277. * a crypto operation.
  278. */
  279. struct mv_cesa_sg_dma_iter {
  280. enum dma_data_direction dir;
  281. struct scatterlist *sg;
  282. unsigned int offset;
  283. unsigned int op_offset;
  284. };
  285. /**
  286. * struct mv_cesa_dma_iter - crypto operation iterator
  287. * @len: the crypto operation length
  288. * @offset: current position in the crypto operation
  289. * @op_len: sub-operation length (the crypto engine can only act on 2kb
  290. * chunks)
  291. *
  292. * Iterator used to create a TDMA chain for a given crypto operation.
  293. */
  294. struct mv_cesa_dma_iter {
  295. unsigned int len;
  296. unsigned int offset;
  297. unsigned int op_len;
  298. };
  299. /**
  300. * struct mv_cesa_tdma_chain - TDMA chain
  301. * @first: first entry in the TDMA chain
  302. * @last: last entry in the TDMA chain
  303. *
  304. * Stores a TDMA chain for a specific crypto operation.
  305. */
  306. struct mv_cesa_tdma_chain {
  307. struct mv_cesa_tdma_desc *first;
  308. struct mv_cesa_tdma_desc *last;
  309. };
  310. struct mv_cesa_engine;
  311. /**
  312. * struct mv_cesa_caps - CESA device capabilities
  313. * @engines: number of engines
  314. * @has_tdma: whether this device has a TDMA block
  315. * @cipher_algs: supported cipher algorithms
  316. * @ncipher_algs: number of supported cipher algorithms
  317. * @ahash_algs: supported hash algorithms
  318. * @nahash_algs: number of supported hash algorithms
  319. *
  320. * Structure used to describe CESA device capabilities.
  321. */
  322. struct mv_cesa_caps {
  323. int nengines;
  324. bool has_tdma;
  325. struct crypto_alg **cipher_algs;
  326. int ncipher_algs;
  327. struct ahash_alg **ahash_algs;
  328. int nahash_algs;
  329. };
  330. /**
  331. * struct mv_cesa_dev_dma - DMA pools
  332. * @tdma_desc_pool: TDMA desc pool
  333. * @op_pool: crypto operation pool
  334. * @cache_pool: data cache pool (used by hash implementation when the
  335. * hash request is smaller than the hash block size)
  336. * @padding_pool: padding pool (used by hash implementation when hardware
  337. * padding cannot be used)
  338. *
  339. * Structure containing the different DMA pools used by this driver.
  340. */
  341. struct mv_cesa_dev_dma {
  342. struct dma_pool *tdma_desc_pool;
  343. struct dma_pool *op_pool;
  344. struct dma_pool *cache_pool;
  345. struct dma_pool *padding_pool;
  346. };
  347. /**
  348. * struct mv_cesa_dev - CESA device
  349. * @caps: device capabilities
  350. * @regs: device registers
  351. * @sram_size: usable SRAM size
  352. * @lock: device lock
  353. * @queue: crypto request queue
  354. * @engines: array of engines
  355. * @dma: dma pools
  356. *
  357. * Structure storing CESA device information.
  358. */
  359. struct mv_cesa_dev {
  360. const struct mv_cesa_caps *caps;
  361. void __iomem *regs;
  362. struct device *dev;
  363. unsigned int sram_size;
  364. spinlock_t lock;
  365. struct crypto_queue queue;
  366. struct mv_cesa_engine *engines;
  367. struct mv_cesa_dev_dma *dma;
  368. };
  369. /**
  370. * struct mv_cesa_engine - CESA engine
  371. * @id: engine id
  372. * @regs: engine registers
  373. * @sram: SRAM memory region
  374. * @sram_dma: DMA address of the SRAM memory region
  375. * @lock: engine lock
  376. * @req: current crypto request
  377. * @clk: engine clk
  378. * @zclk: engine zclk
  379. * @max_req_len: maximum chunk length (useful to create the TDMA chain)
  380. * @int_mask: interrupt mask cache
  381. * @pool: memory pool pointing to the memory region reserved in
  382. * SRAM
  383. *
  384. * Structure storing CESA engine information.
  385. */
  386. struct mv_cesa_engine {
  387. int id;
  388. void __iomem *regs;
  389. void __iomem *sram;
  390. dma_addr_t sram_dma;
  391. spinlock_t lock;
  392. struct crypto_async_request *req;
  393. struct clk *clk;
  394. struct clk *zclk;
  395. size_t max_req_len;
  396. u32 int_mask;
  397. struct gen_pool *pool;
  398. };
  399. /**
  400. * struct mv_cesa_req_ops - CESA request operations
  401. * @prepare: prepare a request to be executed on the specified engine
  402. * @process: process a request chunk result (should return 0 if the
  403. * operation, -EINPROGRESS if it needs more steps or an error
  404. * code)
  405. * @step: launch the crypto operation on the next chunk
  406. * @cleanup: cleanup the crypto request (release associated data)
  407. */
  408. struct mv_cesa_req_ops {
  409. void (*prepare)(struct crypto_async_request *req,
  410. struct mv_cesa_engine *engine);
  411. int (*process)(struct crypto_async_request *req, u32 status);
  412. void (*step)(struct crypto_async_request *req);
  413. void (*cleanup)(struct crypto_async_request *req);
  414. };
  415. /**
  416. * struct mv_cesa_ctx - CESA operation context
  417. * @ops: crypto operations
  418. *
  419. * Base context structure inherited by operation specific ones.
  420. */
  421. struct mv_cesa_ctx {
  422. const struct mv_cesa_req_ops *ops;
  423. };
  424. /**
  425. * struct mv_cesa_hash_ctx - CESA hash operation context
  426. * @base: base context structure
  427. *
  428. * Hash context structure.
  429. */
  430. struct mv_cesa_hash_ctx {
  431. struct mv_cesa_ctx base;
  432. };
  433. /**
  434. * struct mv_cesa_hash_ctx - CESA hmac operation context
  435. * @base: base context structure
  436. * @iv: initialization vectors
  437. *
  438. * HMAC context structure.
  439. */
  440. struct mv_cesa_hmac_ctx {
  441. struct mv_cesa_ctx base;
  442. u32 iv[16];
  443. };
  444. /**
  445. * enum mv_cesa_req_type - request type definitions
  446. * @CESA_STD_REQ: standard request
  447. * @CESA_DMA_REQ: DMA request
  448. */
  449. enum mv_cesa_req_type {
  450. CESA_STD_REQ,
  451. CESA_DMA_REQ,
  452. };
  453. /**
  454. * struct mv_cesa_req - CESA request
  455. * @type: request type
  456. * @engine: engine associated with this request
  457. */
  458. struct mv_cesa_req {
  459. enum mv_cesa_req_type type;
  460. struct mv_cesa_engine *engine;
  461. };
  462. /**
  463. * struct mv_cesa_tdma_req - CESA TDMA request
  464. * @base: base information
  465. * @chain: TDMA chain
  466. */
  467. struct mv_cesa_tdma_req {
  468. struct mv_cesa_req base;
  469. struct mv_cesa_tdma_chain chain;
  470. };
  471. /**
  472. * struct mv_cesa_sg_std_iter - CESA scatter-gather iterator for standard
  473. * requests
  474. * @iter: sg mapping iterator
  475. * @offset: current offset in the SG entry mapped in memory
  476. */
  477. struct mv_cesa_sg_std_iter {
  478. struct sg_mapping_iter iter;
  479. unsigned int offset;
  480. };
  481. /**
  482. * struct mv_cesa_ablkcipher_std_req - cipher standard request
  483. * @base: base information
  484. * @op: operation context
  485. * @offset: current operation offset
  486. * @size: size of the crypto operation
  487. */
  488. struct mv_cesa_ablkcipher_std_req {
  489. struct mv_cesa_req base;
  490. struct mv_cesa_op_ctx op;
  491. unsigned int offset;
  492. unsigned int size;
  493. bool skip_ctx;
  494. };
  495. /**
  496. * struct mv_cesa_ablkcipher_req - cipher request
  497. * @req: type specific request information
  498. * @src_nents: number of entries in the src sg list
  499. * @dst_nents: number of entries in the dest sg list
  500. */
  501. struct mv_cesa_ablkcipher_req {
  502. union {
  503. struct mv_cesa_req base;
  504. struct mv_cesa_tdma_req dma;
  505. struct mv_cesa_ablkcipher_std_req std;
  506. } req;
  507. int src_nents;
  508. int dst_nents;
  509. };
  510. /**
  511. * struct mv_cesa_ahash_std_req - standard hash request
  512. * @base: base information
  513. * @offset: current operation offset
  514. */
  515. struct mv_cesa_ahash_std_req {
  516. struct mv_cesa_req base;
  517. unsigned int offset;
  518. };
  519. /**
  520. * struct mv_cesa_ahash_dma_req - DMA hash request
  521. * @base: base information
  522. * @padding: padding buffer
  523. * @padding_dma: DMA address of the padding buffer
  524. * @cache_dma: DMA address of the cache buffer
  525. */
  526. struct mv_cesa_ahash_dma_req {
  527. struct mv_cesa_tdma_req base;
  528. u8 *padding;
  529. dma_addr_t padding_dma;
  530. u8 *cache;
  531. dma_addr_t cache_dma;
  532. };
  533. /**
  534. * struct mv_cesa_ahash_req - hash request
  535. * @req: type specific request information
  536. * @cache: cache buffer
  537. * @cache_ptr: write pointer in the cache buffer
  538. * @len: hash total length
  539. * @src_nents: number of entries in the scatterlist
  540. * @last_req: define whether the current operation is the last one
  541. * or not
  542. * @state: hash state
  543. */
  544. struct mv_cesa_ahash_req {
  545. union {
  546. struct mv_cesa_req base;
  547. struct mv_cesa_ahash_dma_req dma;
  548. struct mv_cesa_ahash_std_req std;
  549. } req;
  550. struct mv_cesa_op_ctx op_tmpl;
  551. u8 cache[CESA_MAX_HASH_BLOCK_SIZE];
  552. unsigned int cache_ptr;
  553. u64 len;
  554. int src_nents;
  555. bool last_req;
  556. bool algo_le;
  557. u32 state[8];
  558. };
  559. /* CESA functions */
  560. extern struct mv_cesa_dev *cesa_dev;
  561. static inline void mv_cesa_update_op_cfg(struct mv_cesa_op_ctx *op,
  562. u32 cfg, u32 mask)
  563. {
  564. op->desc.config &= cpu_to_le32(~mask);
  565. op->desc.config |= cpu_to_le32(cfg);
  566. }
  567. static inline u32 mv_cesa_get_op_cfg(const struct mv_cesa_op_ctx *op)
  568. {
  569. return le32_to_cpu(op->desc.config);
  570. }
  571. static inline void mv_cesa_set_op_cfg(struct mv_cesa_op_ctx *op, u32 cfg)
  572. {
  573. op->desc.config = cpu_to_le32(cfg);
  574. }
  575. static inline void mv_cesa_adjust_op(struct mv_cesa_engine *engine,
  576. struct mv_cesa_op_ctx *op)
  577. {
  578. u32 offset = engine->sram_dma & CESA_SA_SRAM_MSK;
  579. op->desc.enc_p = CESA_SA_DESC_CRYPT_DATA(offset);
  580. op->desc.enc_key_p = CESA_SA_DESC_CRYPT_KEY(offset);
  581. op->desc.enc_iv = CESA_SA_DESC_CRYPT_IV(offset);
  582. op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_DATA_MSK;
  583. op->desc.mac_src_p |= CESA_SA_DESC_MAC_DATA(offset);
  584. op->desc.mac_digest &= ~CESA_SA_DESC_MAC_DIGEST_MSK;
  585. op->desc.mac_digest |= CESA_SA_DESC_MAC_DIGEST(offset);
  586. op->desc.mac_iv = CESA_SA_DESC_MAC_IV(offset);
  587. }
  588. static inline void mv_cesa_set_crypt_op_len(struct mv_cesa_op_ctx *op, int len)
  589. {
  590. op->desc.enc_len = cpu_to_le32(len);
  591. }
  592. static inline void mv_cesa_set_mac_op_total_len(struct mv_cesa_op_ctx *op,
  593. int len)
  594. {
  595. op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_TOTAL_LEN_MSK;
  596. op->desc.mac_src_p |= CESA_SA_DESC_MAC_TOTAL_LEN(len);
  597. }
  598. static inline void mv_cesa_set_mac_op_frag_len(struct mv_cesa_op_ctx *op,
  599. int len)
  600. {
  601. op->desc.mac_digest &= ~CESA_SA_DESC_MAC_FRAG_LEN_MSK;
  602. op->desc.mac_digest |= CESA_SA_DESC_MAC_FRAG_LEN(len);
  603. }
  604. static inline void mv_cesa_set_int_mask(struct mv_cesa_engine *engine,
  605. u32 int_mask)
  606. {
  607. if (int_mask == engine->int_mask)
  608. return;
  609. writel_relaxed(int_mask, engine->regs + CESA_SA_INT_MSK);
  610. engine->int_mask = int_mask;
  611. }
  612. static inline u32 mv_cesa_get_int_mask(struct mv_cesa_engine *engine)
  613. {
  614. return engine->int_mask;
  615. }
  616. static inline bool mv_cesa_mac_op_is_first_frag(const struct mv_cesa_op_ctx *op)
  617. {
  618. return (mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK) ==
  619. CESA_SA_DESC_CFG_FIRST_FRAG;
  620. }
  621. int mv_cesa_queue_req(struct crypto_async_request *req);
  622. /*
  623. * Helper function that indicates whether a crypto request needs to be
  624. * cleaned up or not after being enqueued using mv_cesa_queue_req().
  625. */
  626. static inline int mv_cesa_req_needs_cleanup(struct crypto_async_request *req,
  627. int ret)
  628. {
  629. /*
  630. * The queue still had some space, the request was queued
  631. * normally, so there's no need to clean it up.
  632. */
  633. if (ret == -EINPROGRESS)
  634. return false;
  635. /*
  636. * The queue had not space left, but since the request is
  637. * flagged with CRYPTO_TFM_REQ_MAY_BACKLOG, it was added to
  638. * the backlog and will be processed later. There's no need to
  639. * clean it up.
  640. */
  641. if (ret == -EBUSY && req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
  642. return false;
  643. /* Request wasn't queued, we need to clean it up */
  644. return true;
  645. }
  646. /* TDMA functions */
  647. static inline void mv_cesa_req_dma_iter_init(struct mv_cesa_dma_iter *iter,
  648. unsigned int len)
  649. {
  650. iter->len = len;
  651. iter->op_len = min(len, CESA_SA_SRAM_PAYLOAD_SIZE);
  652. iter->offset = 0;
  653. }
  654. static inline void mv_cesa_sg_dma_iter_init(struct mv_cesa_sg_dma_iter *iter,
  655. struct scatterlist *sg,
  656. enum dma_data_direction dir)
  657. {
  658. iter->op_offset = 0;
  659. iter->offset = 0;
  660. iter->sg = sg;
  661. iter->dir = dir;
  662. }
  663. static inline unsigned int
  664. mv_cesa_req_dma_iter_transfer_len(struct mv_cesa_dma_iter *iter,
  665. struct mv_cesa_sg_dma_iter *sgiter)
  666. {
  667. return min(iter->op_len - sgiter->op_offset,
  668. sg_dma_len(sgiter->sg) - sgiter->offset);
  669. }
  670. bool mv_cesa_req_dma_iter_next_transfer(struct mv_cesa_dma_iter *chain,
  671. struct mv_cesa_sg_dma_iter *sgiter,
  672. unsigned int len);
  673. static inline bool mv_cesa_req_dma_iter_next_op(struct mv_cesa_dma_iter *iter)
  674. {
  675. iter->offset += iter->op_len;
  676. iter->op_len = min(iter->len - iter->offset,
  677. CESA_SA_SRAM_PAYLOAD_SIZE);
  678. return iter->op_len;
  679. }
  680. void mv_cesa_dma_step(struct mv_cesa_tdma_req *dreq);
  681. static inline int mv_cesa_dma_process(struct mv_cesa_tdma_req *dreq,
  682. u32 status)
  683. {
  684. if (!(status & CESA_SA_INT_ACC0_IDMA_DONE))
  685. return -EINPROGRESS;
  686. if (status & CESA_SA_INT_IDMA_OWN_ERR)
  687. return -EINVAL;
  688. return 0;
  689. }
  690. void mv_cesa_dma_prepare(struct mv_cesa_tdma_req *dreq,
  691. struct mv_cesa_engine *engine);
  692. void mv_cesa_dma_cleanup(struct mv_cesa_tdma_req *dreq);
  693. static inline void
  694. mv_cesa_tdma_desc_iter_init(struct mv_cesa_tdma_chain *chain)
  695. {
  696. memset(chain, 0, sizeof(*chain));
  697. }
  698. struct mv_cesa_op_ctx *mv_cesa_dma_add_op(struct mv_cesa_tdma_chain *chain,
  699. const struct mv_cesa_op_ctx *op_templ,
  700. bool skip_ctx,
  701. gfp_t flags);
  702. int mv_cesa_dma_add_data_transfer(struct mv_cesa_tdma_chain *chain,
  703. dma_addr_t dst, dma_addr_t src, u32 size,
  704. u32 flags, gfp_t gfp_flags);
  705. int mv_cesa_dma_add_dummy_launch(struct mv_cesa_tdma_chain *chain, gfp_t flags);
  706. int mv_cesa_dma_add_dummy_end(struct mv_cesa_tdma_chain *chain, gfp_t flags);
  707. int mv_cesa_dma_add_op_transfers(struct mv_cesa_tdma_chain *chain,
  708. struct mv_cesa_dma_iter *dma_iter,
  709. struct mv_cesa_sg_dma_iter *sgiter,
  710. gfp_t gfp_flags);
  711. /* Algorithm definitions */
  712. extern struct ahash_alg mv_md5_alg;
  713. extern struct ahash_alg mv_sha1_alg;
  714. extern struct ahash_alg mv_sha256_alg;
  715. extern struct ahash_alg mv_ahmac_md5_alg;
  716. extern struct ahash_alg mv_ahmac_sha1_alg;
  717. extern struct ahash_alg mv_ahmac_sha256_alg;
  718. extern struct crypto_alg mv_cesa_ecb_des_alg;
  719. extern struct crypto_alg mv_cesa_cbc_des_alg;
  720. extern struct crypto_alg mv_cesa_ecb_des3_ede_alg;
  721. extern struct crypto_alg mv_cesa_cbc_des3_ede_alg;
  722. extern struct crypto_alg mv_cesa_ecb_aes_alg;
  723. extern struct crypto_alg mv_cesa_cbc_aes_alg;
  724. #endif /* __MARVELL_CESA_H__ */