ocs-hcu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel Keem Bay OCS HCU Crypto Driver.
  4. *
  5. * Copyright (C) 2018-2020 Intel Corporation
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/irq.h>
  11. #include <linux/module.h>
  12. #include <crypto/sha2.h>
  13. #include "ocs-hcu.h"
  14. /* Registers. */
  15. #define OCS_HCU_MODE 0x00
  16. #define OCS_HCU_CHAIN 0x04
  17. #define OCS_HCU_OPERATION 0x08
  18. #define OCS_HCU_KEY_0 0x0C
  19. #define OCS_HCU_ISR 0x50
  20. #define OCS_HCU_IER 0x54
  21. #define OCS_HCU_STATUS 0x58
  22. #define OCS_HCU_MSG_LEN_LO 0x60
  23. #define OCS_HCU_MSG_LEN_HI 0x64
  24. #define OCS_HCU_KEY_BYTE_ORDER_CFG 0x80
  25. #define OCS_HCU_DMA_SRC_ADDR 0x400
  26. #define OCS_HCU_DMA_SRC_SIZE 0x408
  27. #define OCS_HCU_DMA_DST_SIZE 0x40C
  28. #define OCS_HCU_DMA_DMA_MODE 0x410
  29. #define OCS_HCU_DMA_NEXT_SRC_DESCR 0x418
  30. #define OCS_HCU_DMA_MSI_ISR 0x480
  31. #define OCS_HCU_DMA_MSI_IER 0x484
  32. #define OCS_HCU_DMA_MSI_MASK 0x488
  33. /* Register bit definitions. */
  34. #define HCU_MODE_ALGO_SHIFT 16
  35. #define HCU_MODE_HMAC_SHIFT 22
  36. #define HCU_STATUS_BUSY BIT(0)
  37. #define HCU_BYTE_ORDER_SWAP BIT(0)
  38. #define HCU_IRQ_HASH_DONE BIT(2)
  39. #define HCU_IRQ_HASH_ERR_MASK (BIT(3) | BIT(1) | BIT(0))
  40. #define HCU_DMA_IRQ_SRC_DONE BIT(0)
  41. #define HCU_DMA_IRQ_SAI_ERR BIT(2)
  42. #define HCU_DMA_IRQ_BAD_COMP_ERR BIT(3)
  43. #define HCU_DMA_IRQ_INBUF_RD_ERR BIT(4)
  44. #define HCU_DMA_IRQ_INBUF_WD_ERR BIT(5)
  45. #define HCU_DMA_IRQ_OUTBUF_WR_ERR BIT(6)
  46. #define HCU_DMA_IRQ_OUTBUF_RD_ERR BIT(7)
  47. #define HCU_DMA_IRQ_CRD_ERR BIT(8)
  48. #define HCU_DMA_IRQ_ERR_MASK (HCU_DMA_IRQ_SAI_ERR | \
  49. HCU_DMA_IRQ_BAD_COMP_ERR | \
  50. HCU_DMA_IRQ_INBUF_RD_ERR | \
  51. HCU_DMA_IRQ_INBUF_WD_ERR | \
  52. HCU_DMA_IRQ_OUTBUF_WR_ERR | \
  53. HCU_DMA_IRQ_OUTBUF_RD_ERR | \
  54. HCU_DMA_IRQ_CRD_ERR)
  55. #define HCU_DMA_SNOOP_MASK (0x7 << 28)
  56. #define HCU_DMA_SRC_LL_EN BIT(25)
  57. #define HCU_DMA_EN BIT(31)
  58. #define OCS_HCU_ENDIANNESS_VALUE 0x2A
  59. #define HCU_DMA_MSI_UNMASK BIT(0)
  60. #define HCU_DMA_MSI_DISABLE 0
  61. #define HCU_IRQ_DISABLE 0
  62. #define OCS_HCU_START BIT(0)
  63. #define OCS_HCU_TERMINATE BIT(1)
  64. #define OCS_LL_DMA_FLAG_TERMINATE BIT(31)
  65. #define OCS_HCU_HW_KEY_LEN_U32 (OCS_HCU_HW_KEY_LEN / sizeof(u32))
  66. #define HCU_DATA_WRITE_ENDIANNESS_OFFSET 26
  67. #define OCS_HCU_NUM_CHAINS_SHA256_224_SM3 (SHA256_DIGEST_SIZE / sizeof(u32))
  68. #define OCS_HCU_NUM_CHAINS_SHA384_512 (SHA512_DIGEST_SIZE / sizeof(u32))
  69. /*
  70. * While polling on a busy HCU, wait maximum 200us between one check and the
  71. * other.
  72. */
  73. #define OCS_HCU_WAIT_BUSY_RETRY_DELAY_US 200
  74. /* Wait on a busy HCU for maximum 1 second. */
  75. #define OCS_HCU_WAIT_BUSY_TIMEOUT_US 1000000
  76. /**
  77. * struct ocs_hcu_dma_entry - An entry in an OCS DMA linked list.
  78. * @src_addr: Source address of the data.
  79. * @src_len: Length of data to be fetched.
  80. * @nxt_desc: Next descriptor to fetch.
  81. * @ll_flags: Flags (Freeze @ terminate) for the DMA engine.
  82. */
  83. struct ocs_hcu_dma_entry {
  84. u32 src_addr;
  85. u32 src_len;
  86. u32 nxt_desc;
  87. u32 ll_flags;
  88. };
  89. /**
  90. * struct ocs_hcu_dma_list - OCS-specific DMA linked list.
  91. * @head: The head of the list (points to the array backing the list).
  92. * @tail: The current tail of the list; NULL if the list is empty.
  93. * @dma_addr: The DMA address of @head (i.e., the DMA address of the backing
  94. * array).
  95. * @max_nents: Maximum number of entries in the list (i.e., number of elements
  96. * in the backing array).
  97. *
  98. * The OCS DMA list is an array-backed list of OCS DMA descriptors. The array
  99. * backing the list is allocated with dma_alloc_coherent() and pointed by
  100. * @head.
  101. */
  102. struct ocs_hcu_dma_list {
  103. struct ocs_hcu_dma_entry *head;
  104. struct ocs_hcu_dma_entry *tail;
  105. dma_addr_t dma_addr;
  106. size_t max_nents;
  107. };
  108. static inline u32 ocs_hcu_num_chains(enum ocs_hcu_algo algo)
  109. {
  110. switch (algo) {
  111. case OCS_HCU_ALGO_SHA224:
  112. case OCS_HCU_ALGO_SHA256:
  113. case OCS_HCU_ALGO_SM3:
  114. return OCS_HCU_NUM_CHAINS_SHA256_224_SM3;
  115. case OCS_HCU_ALGO_SHA384:
  116. case OCS_HCU_ALGO_SHA512:
  117. return OCS_HCU_NUM_CHAINS_SHA384_512;
  118. default:
  119. return 0;
  120. };
  121. }
  122. static inline u32 ocs_hcu_digest_size(enum ocs_hcu_algo algo)
  123. {
  124. switch (algo) {
  125. case OCS_HCU_ALGO_SHA224:
  126. return SHA224_DIGEST_SIZE;
  127. case OCS_HCU_ALGO_SHA256:
  128. case OCS_HCU_ALGO_SM3:
  129. /* SM3 shares the same block size. */
  130. return SHA256_DIGEST_SIZE;
  131. case OCS_HCU_ALGO_SHA384:
  132. return SHA384_DIGEST_SIZE;
  133. case OCS_HCU_ALGO_SHA512:
  134. return SHA512_DIGEST_SIZE;
  135. default:
  136. return 0;
  137. }
  138. }
  139. /**
  140. * ocs_hcu_wait_busy() - Wait for HCU OCS hardware to became usable.
  141. * @hcu_dev: OCS HCU device to wait for.
  142. *
  143. * Return: 0 if device free, -ETIMEOUT if device busy and internal timeout has
  144. * expired.
  145. */
  146. static int ocs_hcu_wait_busy(struct ocs_hcu_dev *hcu_dev)
  147. {
  148. long val;
  149. return readl_poll_timeout(hcu_dev->io_base + OCS_HCU_STATUS, val,
  150. !(val & HCU_STATUS_BUSY),
  151. OCS_HCU_WAIT_BUSY_RETRY_DELAY_US,
  152. OCS_HCU_WAIT_BUSY_TIMEOUT_US);
  153. }
  154. static void ocs_hcu_done_irq_en(struct ocs_hcu_dev *hcu_dev)
  155. {
  156. /* Clear any pending interrupts. */
  157. writel(0xFFFFFFFF, hcu_dev->io_base + OCS_HCU_ISR);
  158. hcu_dev->irq_err = false;
  159. /* Enable error and HCU done interrupts. */
  160. writel(HCU_IRQ_HASH_DONE | HCU_IRQ_HASH_ERR_MASK,
  161. hcu_dev->io_base + OCS_HCU_IER);
  162. }
  163. static void ocs_hcu_dma_irq_en(struct ocs_hcu_dev *hcu_dev)
  164. {
  165. /* Clear any pending interrupts. */
  166. writel(0xFFFFFFFF, hcu_dev->io_base + OCS_HCU_DMA_MSI_ISR);
  167. hcu_dev->irq_err = false;
  168. /* Only operating on DMA source completion and error interrupts. */
  169. writel(HCU_DMA_IRQ_ERR_MASK | HCU_DMA_IRQ_SRC_DONE,
  170. hcu_dev->io_base + OCS_HCU_DMA_MSI_IER);
  171. /* Unmask */
  172. writel(HCU_DMA_MSI_UNMASK, hcu_dev->io_base + OCS_HCU_DMA_MSI_MASK);
  173. }
  174. static void ocs_hcu_irq_dis(struct ocs_hcu_dev *hcu_dev)
  175. {
  176. writel(HCU_IRQ_DISABLE, hcu_dev->io_base + OCS_HCU_IER);
  177. writel(HCU_DMA_MSI_DISABLE, hcu_dev->io_base + OCS_HCU_DMA_MSI_IER);
  178. }
  179. static int ocs_hcu_wait_and_disable_irq(struct ocs_hcu_dev *hcu_dev)
  180. {
  181. int rc;
  182. rc = wait_for_completion_interruptible(&hcu_dev->irq_done);
  183. if (rc)
  184. goto exit;
  185. if (hcu_dev->irq_err) {
  186. /* Unset flag and return error. */
  187. hcu_dev->irq_err = false;
  188. rc = -EIO;
  189. goto exit;
  190. }
  191. exit:
  192. ocs_hcu_irq_dis(hcu_dev);
  193. return rc;
  194. }
  195. /**
  196. * ocs_hcu_get_intermediate_data() - Get intermediate data.
  197. * @hcu_dev: The target HCU device.
  198. * @data: Where to store the intermediate.
  199. * @algo: The algorithm being used.
  200. *
  201. * This function is used to save the current hashing process state in order to
  202. * continue it in the future.
  203. *
  204. * Note: once all data has been processed, the intermediate data actually
  205. * contains the hashing result. So this function is also used to retrieve the
  206. * final result of a hashing process.
  207. *
  208. * Return: 0 on success, negative error code otherwise.
  209. */
  210. static int ocs_hcu_get_intermediate_data(struct ocs_hcu_dev *hcu_dev,
  211. struct ocs_hcu_idata *data,
  212. enum ocs_hcu_algo algo)
  213. {
  214. const int n = ocs_hcu_num_chains(algo);
  215. u32 *chain;
  216. int rc;
  217. int i;
  218. /* Data not requested. */
  219. if (!data)
  220. return -EINVAL;
  221. chain = (u32 *)data->digest;
  222. /* Ensure that the OCS is no longer busy before reading the chains. */
  223. rc = ocs_hcu_wait_busy(hcu_dev);
  224. if (rc)
  225. return rc;
  226. /*
  227. * This loops is safe because data->digest is an array of
  228. * SHA512_DIGEST_SIZE bytes and the maximum value returned by
  229. * ocs_hcu_num_chains() is OCS_HCU_NUM_CHAINS_SHA384_512 which is equal
  230. * to SHA512_DIGEST_SIZE / sizeof(u32).
  231. */
  232. for (i = 0; i < n; i++)
  233. chain[i] = readl(hcu_dev->io_base + OCS_HCU_CHAIN);
  234. data->msg_len_lo = readl(hcu_dev->io_base + OCS_HCU_MSG_LEN_LO);
  235. data->msg_len_hi = readl(hcu_dev->io_base + OCS_HCU_MSG_LEN_HI);
  236. return 0;
  237. }
  238. /**
  239. * ocs_hcu_set_intermediate_data() - Set intermediate data.
  240. * @hcu_dev: The target HCU device.
  241. * @data: The intermediate data to be set.
  242. * @algo: The algorithm being used.
  243. *
  244. * This function is used to continue a previous hashing process.
  245. */
  246. static void ocs_hcu_set_intermediate_data(struct ocs_hcu_dev *hcu_dev,
  247. const struct ocs_hcu_idata *data,
  248. enum ocs_hcu_algo algo)
  249. {
  250. const int n = ocs_hcu_num_chains(algo);
  251. u32 *chain = (u32 *)data->digest;
  252. int i;
  253. /*
  254. * This loops is safe because data->digest is an array of
  255. * SHA512_DIGEST_SIZE bytes and the maximum value returned by
  256. * ocs_hcu_num_chains() is OCS_HCU_NUM_CHAINS_SHA384_512 which is equal
  257. * to SHA512_DIGEST_SIZE / sizeof(u32).
  258. */
  259. for (i = 0; i < n; i++)
  260. writel(chain[i], hcu_dev->io_base + OCS_HCU_CHAIN);
  261. writel(data->msg_len_lo, hcu_dev->io_base + OCS_HCU_MSG_LEN_LO);
  262. writel(data->msg_len_hi, hcu_dev->io_base + OCS_HCU_MSG_LEN_HI);
  263. }
  264. static int ocs_hcu_get_digest(struct ocs_hcu_dev *hcu_dev,
  265. enum ocs_hcu_algo algo, u8 *dgst, size_t dgst_len)
  266. {
  267. u32 *chain;
  268. int rc;
  269. int i;
  270. if (!dgst)
  271. return -EINVAL;
  272. /* Length of the output buffer must match the algo digest size. */
  273. if (dgst_len != ocs_hcu_digest_size(algo))
  274. return -EINVAL;
  275. /* Ensure that the OCS is no longer busy before reading the chains. */
  276. rc = ocs_hcu_wait_busy(hcu_dev);
  277. if (rc)
  278. return rc;
  279. chain = (u32 *)dgst;
  280. for (i = 0; i < dgst_len / sizeof(u32); i++)
  281. chain[i] = readl(hcu_dev->io_base + OCS_HCU_CHAIN);
  282. return 0;
  283. }
  284. /**
  285. * ocs_hcu_hw_cfg() - Configure the HCU hardware.
  286. * @hcu_dev: The HCU device to configure.
  287. * @algo: The algorithm to be used by the HCU device.
  288. * @use_hmac: Whether or not HW HMAC should be used.
  289. *
  290. * Return: 0 on success, negative error code otherwise.
  291. */
  292. static int ocs_hcu_hw_cfg(struct ocs_hcu_dev *hcu_dev, enum ocs_hcu_algo algo,
  293. bool use_hmac)
  294. {
  295. u32 cfg;
  296. int rc;
  297. if (algo != OCS_HCU_ALGO_SHA256 && algo != OCS_HCU_ALGO_SHA224 &&
  298. algo != OCS_HCU_ALGO_SHA384 && algo != OCS_HCU_ALGO_SHA512 &&
  299. algo != OCS_HCU_ALGO_SM3)
  300. return -EINVAL;
  301. rc = ocs_hcu_wait_busy(hcu_dev);
  302. if (rc)
  303. return rc;
  304. /* Ensure interrupts are disabled. */
  305. ocs_hcu_irq_dis(hcu_dev);
  306. /* Configure endianness, hashing algorithm and HW HMAC (if needed) */
  307. cfg = OCS_HCU_ENDIANNESS_VALUE << HCU_DATA_WRITE_ENDIANNESS_OFFSET;
  308. cfg |= algo << HCU_MODE_ALGO_SHIFT;
  309. if (use_hmac)
  310. cfg |= BIT(HCU_MODE_HMAC_SHIFT);
  311. writel(cfg, hcu_dev->io_base + OCS_HCU_MODE);
  312. return 0;
  313. }
  314. /**
  315. * ocs_hcu_clear_key() - Clear key stored in OCS HMAC KEY registers.
  316. * @hcu_dev: The OCS HCU device whose key registers should be cleared.
  317. */
  318. static void ocs_hcu_clear_key(struct ocs_hcu_dev *hcu_dev)
  319. {
  320. int reg_off;
  321. /* Clear OCS_HCU_KEY_[0..15] */
  322. for (reg_off = 0; reg_off < OCS_HCU_HW_KEY_LEN; reg_off += sizeof(u32))
  323. writel(0, hcu_dev->io_base + OCS_HCU_KEY_0 + reg_off);
  324. }
  325. /**
  326. * ocs_hcu_write_key() - Write key to OCS HMAC KEY registers.
  327. * @hcu_dev: The OCS HCU device the key should be written to.
  328. * @key: The key to be written.
  329. * @len: The size of the key to write. It must be OCS_HCU_HW_KEY_LEN.
  330. *
  331. * Return: 0 on success, negative error code otherwise.
  332. */
  333. static int ocs_hcu_write_key(struct ocs_hcu_dev *hcu_dev, const u8 *key, size_t len)
  334. {
  335. u32 key_u32[OCS_HCU_HW_KEY_LEN_U32];
  336. int i;
  337. if (len > OCS_HCU_HW_KEY_LEN)
  338. return -EINVAL;
  339. /* Copy key into temporary u32 array. */
  340. memcpy(key_u32, key, len);
  341. /*
  342. * Hardware requires all the bytes of the HW Key vector to be
  343. * written. So pad with zero until we reach OCS_HCU_HW_KEY_LEN.
  344. */
  345. memzero_explicit((u8 *)key_u32 + len, OCS_HCU_HW_KEY_LEN - len);
  346. /*
  347. * OCS hardware expects the MSB of the key to be written at the highest
  348. * address of the HCU Key vector; in other word, the key must be
  349. * written in reverse order.
  350. *
  351. * Therefore, we first enable byte swapping for the HCU key vector;
  352. * so that bytes of 32-bit word written to OCS_HCU_KEY_[0..15] will be
  353. * swapped:
  354. * 3 <---> 0, 2 <---> 1.
  355. */
  356. writel(HCU_BYTE_ORDER_SWAP,
  357. hcu_dev->io_base + OCS_HCU_KEY_BYTE_ORDER_CFG);
  358. /*
  359. * And then we write the 32-bit words composing the key starting from
  360. * the end of the key.
  361. */
  362. for (i = 0; i < OCS_HCU_HW_KEY_LEN_U32; i++)
  363. writel(key_u32[OCS_HCU_HW_KEY_LEN_U32 - 1 - i],
  364. hcu_dev->io_base + OCS_HCU_KEY_0 + (sizeof(u32) * i));
  365. memzero_explicit(key_u32, OCS_HCU_HW_KEY_LEN);
  366. return 0;
  367. }
  368. /**
  369. * ocs_hcu_ll_dma_start() - Start OCS HCU hashing via DMA
  370. * @hcu_dev: The OCS HCU device to use.
  371. * @dma_list: The OCS DMA list mapping the data to hash.
  372. * @finalize: Whether or not this is the last hashing operation and therefore
  373. * the final hash should be compute even if data is not
  374. * block-aligned.
  375. *
  376. * Return: 0 on success, negative error code otherwise.
  377. */
  378. static int ocs_hcu_ll_dma_start(struct ocs_hcu_dev *hcu_dev,
  379. const struct ocs_hcu_dma_list *dma_list,
  380. bool finalize)
  381. {
  382. u32 cfg = HCU_DMA_SNOOP_MASK | HCU_DMA_SRC_LL_EN | HCU_DMA_EN;
  383. int rc;
  384. if (!dma_list)
  385. return -EINVAL;
  386. /*
  387. * For final requests we use HCU_DONE IRQ to be notified when all input
  388. * data has been processed by the HCU; however, we cannot do so for
  389. * non-final requests, because we don't get a HCU_DONE IRQ when we
  390. * don't terminate the operation.
  391. *
  392. * Therefore, for non-final requests, we use the DMA IRQ, which
  393. * triggers when DMA has finishing feeding all the input data to the
  394. * HCU, but the HCU may still be processing it. This is fine, since we
  395. * will wait for the HCU processing to be completed when we try to read
  396. * intermediate results, in ocs_hcu_get_intermediate_data().
  397. */
  398. if (finalize)
  399. ocs_hcu_done_irq_en(hcu_dev);
  400. else
  401. ocs_hcu_dma_irq_en(hcu_dev);
  402. reinit_completion(&hcu_dev->irq_done);
  403. writel(dma_list->dma_addr, hcu_dev->io_base + OCS_HCU_DMA_NEXT_SRC_DESCR);
  404. writel(0, hcu_dev->io_base + OCS_HCU_DMA_SRC_SIZE);
  405. writel(0, hcu_dev->io_base + OCS_HCU_DMA_DST_SIZE);
  406. writel(OCS_HCU_START, hcu_dev->io_base + OCS_HCU_OPERATION);
  407. writel(cfg, hcu_dev->io_base + OCS_HCU_DMA_DMA_MODE);
  408. if (finalize)
  409. writel(OCS_HCU_TERMINATE, hcu_dev->io_base + OCS_HCU_OPERATION);
  410. rc = ocs_hcu_wait_and_disable_irq(hcu_dev);
  411. if (rc)
  412. return rc;
  413. return 0;
  414. }
  415. struct ocs_hcu_dma_list *ocs_hcu_dma_list_alloc(struct ocs_hcu_dev *hcu_dev,
  416. int max_nents)
  417. {
  418. struct ocs_hcu_dma_list *dma_list;
  419. dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
  420. if (!dma_list)
  421. return NULL;
  422. /* Total size of the DMA list to allocate. */
  423. dma_list->head = dma_alloc_coherent(hcu_dev->dev,
  424. sizeof(*dma_list->head) * max_nents,
  425. &dma_list->dma_addr, GFP_KERNEL);
  426. if (!dma_list->head) {
  427. kfree(dma_list);
  428. return NULL;
  429. }
  430. dma_list->max_nents = max_nents;
  431. dma_list->tail = NULL;
  432. return dma_list;
  433. }
  434. void ocs_hcu_dma_list_free(struct ocs_hcu_dev *hcu_dev,
  435. struct ocs_hcu_dma_list *dma_list)
  436. {
  437. if (!dma_list)
  438. return;
  439. dma_free_coherent(hcu_dev->dev,
  440. sizeof(*dma_list->head) * dma_list->max_nents,
  441. dma_list->head, dma_list->dma_addr);
  442. kfree(dma_list);
  443. }
  444. /* Add a new DMA entry at the end of the OCS DMA list. */
  445. int ocs_hcu_dma_list_add_tail(struct ocs_hcu_dev *hcu_dev,
  446. struct ocs_hcu_dma_list *dma_list,
  447. dma_addr_t addr, u32 len)
  448. {
  449. struct device *dev = hcu_dev->dev;
  450. struct ocs_hcu_dma_entry *old_tail;
  451. struct ocs_hcu_dma_entry *new_tail;
  452. if (!len)
  453. return 0;
  454. if (!dma_list)
  455. return -EINVAL;
  456. if (addr & ~OCS_HCU_DMA_BIT_MASK) {
  457. dev_err(dev,
  458. "Unexpected error: Invalid DMA address for OCS HCU\n");
  459. return -EINVAL;
  460. }
  461. old_tail = dma_list->tail;
  462. new_tail = old_tail ? old_tail + 1 : dma_list->head;
  463. /* Check if list is full. */
  464. if (new_tail - dma_list->head >= dma_list->max_nents)
  465. return -ENOMEM;
  466. /*
  467. * If there was an old tail (i.e., this is not the first element we are
  468. * adding), un-terminate the old tail and make it point to the new one.
  469. */
  470. if (old_tail) {
  471. old_tail->ll_flags &= ~OCS_LL_DMA_FLAG_TERMINATE;
  472. /*
  473. * The old tail 'nxt_desc' must point to the DMA address of the
  474. * new tail.
  475. */
  476. old_tail->nxt_desc = dma_list->dma_addr +
  477. sizeof(*dma_list->tail) * (new_tail -
  478. dma_list->head);
  479. }
  480. new_tail->src_addr = (u32)addr;
  481. new_tail->src_len = (u32)len;
  482. new_tail->ll_flags = OCS_LL_DMA_FLAG_TERMINATE;
  483. new_tail->nxt_desc = 0;
  484. /* Update list tail with new tail. */
  485. dma_list->tail = new_tail;
  486. return 0;
  487. }
  488. /**
  489. * ocs_hcu_hash_init() - Initialize hash operation context.
  490. * @ctx: The context to initialize.
  491. * @algo: The hashing algorithm to use.
  492. *
  493. * Return: 0 on success, negative error code otherwise.
  494. */
  495. int ocs_hcu_hash_init(struct ocs_hcu_hash_ctx *ctx, enum ocs_hcu_algo algo)
  496. {
  497. if (!ctx)
  498. return -EINVAL;
  499. ctx->algo = algo;
  500. ctx->idata.msg_len_lo = 0;
  501. ctx->idata.msg_len_hi = 0;
  502. /* No need to set idata.digest to 0. */
  503. return 0;
  504. }
  505. /**
  506. * ocs_hcu_hash_update() - Perform a hashing iteration.
  507. * @hcu_dev: The OCS HCU device to use.
  508. * @ctx: The OCS HCU hashing context.
  509. * @dma_list: The OCS DMA list mapping the input data to process.
  510. *
  511. * Return: 0 on success; negative error code otherwise.
  512. */
  513. int ocs_hcu_hash_update(struct ocs_hcu_dev *hcu_dev,
  514. struct ocs_hcu_hash_ctx *ctx,
  515. const struct ocs_hcu_dma_list *dma_list)
  516. {
  517. int rc;
  518. if (!hcu_dev || !ctx)
  519. return -EINVAL;
  520. /* Configure the hardware for the current request. */
  521. rc = ocs_hcu_hw_cfg(hcu_dev, ctx->algo, false);
  522. if (rc)
  523. return rc;
  524. /* If we already processed some data, idata needs to be set. */
  525. if (ctx->idata.msg_len_lo || ctx->idata.msg_len_hi)
  526. ocs_hcu_set_intermediate_data(hcu_dev, &ctx->idata, ctx->algo);
  527. /* Start linked-list DMA hashing. */
  528. rc = ocs_hcu_ll_dma_start(hcu_dev, dma_list, false);
  529. if (rc)
  530. return rc;
  531. /* Update idata and return. */
  532. return ocs_hcu_get_intermediate_data(hcu_dev, &ctx->idata, ctx->algo);
  533. }
  534. /**
  535. * ocs_hcu_hash_finup() - Update and finalize hash computation.
  536. * @hcu_dev: The OCS HCU device to use.
  537. * @ctx: The OCS HCU hashing context.
  538. * @dma_list: The OCS DMA list mapping the input data to process.
  539. * @dgst: The buffer where to save the computed digest.
  540. * @dgst_len: The length of @dgst.
  541. *
  542. * Return: 0 on success; negative error code otherwise.
  543. */
  544. int ocs_hcu_hash_finup(struct ocs_hcu_dev *hcu_dev,
  545. const struct ocs_hcu_hash_ctx *ctx,
  546. const struct ocs_hcu_dma_list *dma_list,
  547. u8 *dgst, size_t dgst_len)
  548. {
  549. int rc;
  550. if (!hcu_dev || !ctx)
  551. return -EINVAL;
  552. /* Configure the hardware for the current request. */
  553. rc = ocs_hcu_hw_cfg(hcu_dev, ctx->algo, false);
  554. if (rc)
  555. return rc;
  556. /* If we already processed some data, idata needs to be set. */
  557. if (ctx->idata.msg_len_lo || ctx->idata.msg_len_hi)
  558. ocs_hcu_set_intermediate_data(hcu_dev, &ctx->idata, ctx->algo);
  559. /* Start linked-list DMA hashing. */
  560. rc = ocs_hcu_ll_dma_start(hcu_dev, dma_list, true);
  561. if (rc)
  562. return rc;
  563. /* Get digest and return. */
  564. return ocs_hcu_get_digest(hcu_dev, ctx->algo, dgst, dgst_len);
  565. }
  566. /**
  567. * ocs_hcu_hash_final() - Finalize hash computation.
  568. * @hcu_dev: The OCS HCU device to use.
  569. * @ctx: The OCS HCU hashing context.
  570. * @dgst: The buffer where to save the computed digest.
  571. * @dgst_len: The length of @dgst.
  572. *
  573. * Return: 0 on success; negative error code otherwise.
  574. */
  575. int ocs_hcu_hash_final(struct ocs_hcu_dev *hcu_dev,
  576. const struct ocs_hcu_hash_ctx *ctx, u8 *dgst,
  577. size_t dgst_len)
  578. {
  579. int rc;
  580. if (!hcu_dev || !ctx)
  581. return -EINVAL;
  582. /* Configure the hardware for the current request. */
  583. rc = ocs_hcu_hw_cfg(hcu_dev, ctx->algo, false);
  584. if (rc)
  585. return rc;
  586. /* If we already processed some data, idata needs to be set. */
  587. if (ctx->idata.msg_len_lo || ctx->idata.msg_len_hi)
  588. ocs_hcu_set_intermediate_data(hcu_dev, &ctx->idata, ctx->algo);
  589. /*
  590. * Enable HCU interrupts, so that HCU_DONE will be triggered once the
  591. * final hash is computed.
  592. */
  593. ocs_hcu_done_irq_en(hcu_dev);
  594. reinit_completion(&hcu_dev->irq_done);
  595. writel(OCS_HCU_TERMINATE, hcu_dev->io_base + OCS_HCU_OPERATION);
  596. rc = ocs_hcu_wait_and_disable_irq(hcu_dev);
  597. if (rc)
  598. return rc;
  599. /* Get digest and return. */
  600. return ocs_hcu_get_digest(hcu_dev, ctx->algo, dgst, dgst_len);
  601. }
  602. /**
  603. * ocs_hcu_digest() - Compute hash digest.
  604. * @hcu_dev: The OCS HCU device to use.
  605. * @algo: The hash algorithm to use.
  606. * @data: The input data to process.
  607. * @data_len: The length of @data.
  608. * @dgst: The buffer where to save the computed digest.
  609. * @dgst_len: The length of @dgst.
  610. *
  611. * Return: 0 on success; negative error code otherwise.
  612. */
  613. int ocs_hcu_digest(struct ocs_hcu_dev *hcu_dev, enum ocs_hcu_algo algo,
  614. void *data, size_t data_len, u8 *dgst, size_t dgst_len)
  615. {
  616. struct device *dev = hcu_dev->dev;
  617. dma_addr_t dma_handle;
  618. u32 reg;
  619. int rc;
  620. /* Configure the hardware for the current request. */
  621. rc = ocs_hcu_hw_cfg(hcu_dev, algo, false);
  622. if (rc)
  623. return rc;
  624. dma_handle = dma_map_single(dev, data, data_len, DMA_TO_DEVICE);
  625. if (dma_mapping_error(dev, dma_handle))
  626. return -EIO;
  627. reg = HCU_DMA_SNOOP_MASK | HCU_DMA_EN;
  628. ocs_hcu_done_irq_en(hcu_dev);
  629. reinit_completion(&hcu_dev->irq_done);
  630. writel(dma_handle, hcu_dev->io_base + OCS_HCU_DMA_SRC_ADDR);
  631. writel(data_len, hcu_dev->io_base + OCS_HCU_DMA_SRC_SIZE);
  632. writel(OCS_HCU_START, hcu_dev->io_base + OCS_HCU_OPERATION);
  633. writel(reg, hcu_dev->io_base + OCS_HCU_DMA_DMA_MODE);
  634. writel(OCS_HCU_TERMINATE, hcu_dev->io_base + OCS_HCU_OPERATION);
  635. rc = ocs_hcu_wait_and_disable_irq(hcu_dev);
  636. if (rc)
  637. return rc;
  638. dma_unmap_single(dev, dma_handle, data_len, DMA_TO_DEVICE);
  639. return ocs_hcu_get_digest(hcu_dev, algo, dgst, dgst_len);
  640. }
  641. /**
  642. * ocs_hcu_hmac() - Compute HMAC.
  643. * @hcu_dev: The OCS HCU device to use.
  644. * @algo: The hash algorithm to use with HMAC.
  645. * @key: The key to use.
  646. * @dma_list: The OCS DMA list mapping the input data to process.
  647. * @key_len: The length of @key.
  648. * @dgst: The buffer where to save the computed HMAC.
  649. * @dgst_len: The length of @dgst.
  650. *
  651. * Return: 0 on success; negative error code otherwise.
  652. */
  653. int ocs_hcu_hmac(struct ocs_hcu_dev *hcu_dev, enum ocs_hcu_algo algo,
  654. const u8 *key, size_t key_len,
  655. const struct ocs_hcu_dma_list *dma_list,
  656. u8 *dgst, size_t dgst_len)
  657. {
  658. int rc;
  659. /* Ensure 'key' is not NULL. */
  660. if (!key || key_len == 0)
  661. return -EINVAL;
  662. /* Configure the hardware for the current request. */
  663. rc = ocs_hcu_hw_cfg(hcu_dev, algo, true);
  664. if (rc)
  665. return rc;
  666. rc = ocs_hcu_write_key(hcu_dev, key, key_len);
  667. if (rc)
  668. return rc;
  669. rc = ocs_hcu_ll_dma_start(hcu_dev, dma_list, true);
  670. /* Clear HW key before processing return code. */
  671. ocs_hcu_clear_key(hcu_dev);
  672. if (rc)
  673. return rc;
  674. return ocs_hcu_get_digest(hcu_dev, algo, dgst, dgst_len);
  675. }
  676. irqreturn_t ocs_hcu_irq_handler(int irq, void *dev_id)
  677. {
  678. struct ocs_hcu_dev *hcu_dev = dev_id;
  679. u32 hcu_irq;
  680. u32 dma_irq;
  681. /* Read and clear the HCU interrupt. */
  682. hcu_irq = readl(hcu_dev->io_base + OCS_HCU_ISR);
  683. writel(hcu_irq, hcu_dev->io_base + OCS_HCU_ISR);
  684. /* Read and clear the HCU DMA interrupt. */
  685. dma_irq = readl(hcu_dev->io_base + OCS_HCU_DMA_MSI_ISR);
  686. writel(dma_irq, hcu_dev->io_base + OCS_HCU_DMA_MSI_ISR);
  687. /* Check for errors. */
  688. if (hcu_irq & HCU_IRQ_HASH_ERR_MASK || dma_irq & HCU_DMA_IRQ_ERR_MASK) {
  689. hcu_dev->irq_err = true;
  690. goto complete;
  691. }
  692. /* Check for DONE IRQs. */
  693. if (hcu_irq & HCU_IRQ_HASH_DONE || dma_irq & HCU_DMA_IRQ_SRC_DONE)
  694. goto complete;
  695. return IRQ_NONE;
  696. complete:
  697. complete(&hcu_dev->irq_done);
  698. return IRQ_HANDLED;
  699. }
  700. MODULE_LICENSE("GPL");