util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2016 Broadcom
  4. */
  5. #include <linux/debugfs.h>
  6. #include "cipher.h"
  7. #include "util.h"
  8. /* offset of SPU_OFIFO_CTRL register */
  9. #define SPU_OFIFO_CTRL 0x40
  10. #define SPU_FIFO_WATERMARK 0x1FF
  11. /**
  12. * spu_sg_at_offset() - Find the scatterlist entry at a given distance from the
  13. * start of a scatterlist.
  14. * @sg: [in] Start of a scatterlist
  15. * @skip: [in] Distance from the start of the scatterlist, in bytes
  16. * @sge: [out] Scatterlist entry at skip bytes from start
  17. * @sge_offset: [out] Number of bytes from start of sge buffer to get to
  18. * requested distance.
  19. *
  20. * Return: 0 if entry found at requested distance
  21. * < 0 otherwise
  22. */
  23. int spu_sg_at_offset(struct scatterlist *sg, unsigned int skip,
  24. struct scatterlist **sge, unsigned int *sge_offset)
  25. {
  26. /* byte index from start of sg to the end of the previous entry */
  27. unsigned int index = 0;
  28. /* byte index from start of sg to the end of the current entry */
  29. unsigned int next_index;
  30. next_index = sg->length;
  31. while (next_index <= skip) {
  32. sg = sg_next(sg);
  33. index = next_index;
  34. if (!sg)
  35. return -EINVAL;
  36. next_index += sg->length;
  37. }
  38. *sge_offset = skip - index;
  39. *sge = sg;
  40. return 0;
  41. }
  42. /* Copy len bytes of sg data, starting at offset skip, to a dest buffer */
  43. void sg_copy_part_to_buf(struct scatterlist *src, u8 *dest,
  44. unsigned int len, unsigned int skip)
  45. {
  46. size_t copied;
  47. unsigned int nents = sg_nents(src);
  48. copied = sg_pcopy_to_buffer(src, nents, dest, len, skip);
  49. if (copied != len) {
  50. flow_log("%s copied %u bytes of %u requested. ",
  51. __func__, (u32)copied, len);
  52. flow_log("sg with %u entries and skip %u\n", nents, skip);
  53. }
  54. }
  55. /*
  56. * Copy data into a scatterlist starting at a specified offset in the
  57. * scatterlist. Specifically, copy len bytes of data in the buffer src
  58. * into the scatterlist dest, starting skip bytes into the scatterlist.
  59. */
  60. void sg_copy_part_from_buf(struct scatterlist *dest, u8 *src,
  61. unsigned int len, unsigned int skip)
  62. {
  63. size_t copied;
  64. unsigned int nents = sg_nents(dest);
  65. copied = sg_pcopy_from_buffer(dest, nents, src, len, skip);
  66. if (copied != len) {
  67. flow_log("%s copied %u bytes of %u requested. ",
  68. __func__, (u32)copied, len);
  69. flow_log("sg with %u entries and skip %u\n", nents, skip);
  70. }
  71. }
  72. /**
  73. * spu_sg_count() - Determine number of elements in scatterlist to provide a
  74. * specified number of bytes.
  75. * @sg_list: scatterlist to examine
  76. * @skip: index of starting point
  77. * @nbytes: consider elements of scatterlist until reaching this number of
  78. * bytes
  79. *
  80. * Return: the number of sg entries contributing to nbytes of data
  81. */
  82. int spu_sg_count(struct scatterlist *sg_list, unsigned int skip, int nbytes)
  83. {
  84. struct scatterlist *sg;
  85. int sg_nents = 0;
  86. unsigned int offset;
  87. if (!sg_list)
  88. return 0;
  89. if (spu_sg_at_offset(sg_list, skip, &sg, &offset) < 0)
  90. return 0;
  91. while (sg && (nbytes > 0)) {
  92. sg_nents++;
  93. nbytes -= (sg->length - offset);
  94. offset = 0;
  95. sg = sg_next(sg);
  96. }
  97. return sg_nents;
  98. }
  99. /**
  100. * spu_msg_sg_add() - Copy scatterlist entries from one sg to another, up to a
  101. * given length.
  102. * @to_sg: scatterlist to copy to
  103. * @from_sg: scatterlist to copy from
  104. * @from_skip: number of bytes to skip in from_sg. Non-zero when previous
  105. * request included part of the buffer in entry in from_sg.
  106. * Assumes from_skip < from_sg->length.
  107. * @from_nents: number of entries in from_sg
  108. * @length: number of bytes to copy. may reach this limit before exhausting
  109. * from_sg.
  110. *
  111. * Copies the entries themselves, not the data in the entries. Assumes to_sg has
  112. * enough entries. Does not limit the size of an individual buffer in to_sg.
  113. *
  114. * to_sg, from_sg, skip are all updated to end of copy
  115. *
  116. * Return: Number of bytes copied
  117. */
  118. u32 spu_msg_sg_add(struct scatterlist **to_sg,
  119. struct scatterlist **from_sg, u32 *from_skip,
  120. u8 from_nents, u32 length)
  121. {
  122. struct scatterlist *sg; /* an entry in from_sg */
  123. struct scatterlist *to = *to_sg;
  124. struct scatterlist *from = *from_sg;
  125. u32 skip = *from_skip;
  126. u32 offset;
  127. int i;
  128. u32 entry_len = 0;
  129. u32 frag_len = 0; /* length of entry added to to_sg */
  130. u32 copied = 0; /* number of bytes copied so far */
  131. if (length == 0)
  132. return 0;
  133. for_each_sg(from, sg, from_nents, i) {
  134. /* number of bytes in this from entry not yet used */
  135. entry_len = sg->length - skip;
  136. frag_len = min(entry_len, length - copied);
  137. offset = sg->offset + skip;
  138. if (frag_len)
  139. sg_set_page(to++, sg_page(sg), frag_len, offset);
  140. copied += frag_len;
  141. if (copied == entry_len) {
  142. /* used up all of from entry */
  143. skip = 0; /* start at beginning of next entry */
  144. }
  145. if (copied == length)
  146. break;
  147. }
  148. *to_sg = to;
  149. *from_sg = sg;
  150. if (frag_len < entry_len)
  151. *from_skip = skip + frag_len;
  152. else
  153. *from_skip = 0;
  154. return copied;
  155. }
  156. void add_to_ctr(u8 *ctr_pos, unsigned int increment)
  157. {
  158. __be64 *high_be = (__be64 *)ctr_pos;
  159. __be64 *low_be = high_be + 1;
  160. u64 orig_low = __be64_to_cpu(*low_be);
  161. u64 new_low = orig_low + (u64)increment;
  162. *low_be = __cpu_to_be64(new_low);
  163. if (new_low < orig_low)
  164. /* there was a carry from the low 8 bytes */
  165. *high_be = __cpu_to_be64(__be64_to_cpu(*high_be) + 1);
  166. }
  167. struct sdesc {
  168. struct shash_desc shash;
  169. char ctx[];
  170. };
  171. /**
  172. * do_shash() - Do a synchronous hash operation in software
  173. * @name: The name of the hash algorithm
  174. * @result: Buffer where digest is to be written
  175. * @data1: First part of data to hash. May be NULL.
  176. * @data1_len: Length of data1, in bytes
  177. * @data2: Second part of data to hash. May be NULL.
  178. * @data2_len: Length of data2, in bytes
  179. * @key: Key (if keyed hash)
  180. * @key_len: Length of key, in bytes (or 0 if non-keyed hash)
  181. *
  182. * Note that the crypto API will not select this driver's own transform because
  183. * this driver only registers asynchronous algos.
  184. *
  185. * Return: 0 if hash successfully stored in result
  186. * < 0 otherwise
  187. */
  188. int do_shash(unsigned char *name, unsigned char *result,
  189. const u8 *data1, unsigned int data1_len,
  190. const u8 *data2, unsigned int data2_len,
  191. const u8 *key, unsigned int key_len)
  192. {
  193. int rc;
  194. unsigned int size;
  195. struct crypto_shash *hash;
  196. struct sdesc *sdesc;
  197. hash = crypto_alloc_shash(name, 0, 0);
  198. if (IS_ERR(hash)) {
  199. rc = PTR_ERR(hash);
  200. pr_err("%s: Crypto %s allocation error %d\n", __func__, name, rc);
  201. return rc;
  202. }
  203. size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
  204. sdesc = kmalloc(size, GFP_KERNEL);
  205. if (!sdesc) {
  206. rc = -ENOMEM;
  207. goto do_shash_err;
  208. }
  209. sdesc->shash.tfm = hash;
  210. if (key_len > 0) {
  211. rc = crypto_shash_setkey(hash, key, key_len);
  212. if (rc) {
  213. pr_err("%s: Could not setkey %s shash\n", __func__, name);
  214. goto do_shash_err;
  215. }
  216. }
  217. rc = crypto_shash_init(&sdesc->shash);
  218. if (rc) {
  219. pr_err("%s: Could not init %s shash\n", __func__, name);
  220. goto do_shash_err;
  221. }
  222. rc = crypto_shash_update(&sdesc->shash, data1, data1_len);
  223. if (rc) {
  224. pr_err("%s: Could not update1\n", __func__);
  225. goto do_shash_err;
  226. }
  227. if (data2 && data2_len) {
  228. rc = crypto_shash_update(&sdesc->shash, data2, data2_len);
  229. if (rc) {
  230. pr_err("%s: Could not update2\n", __func__);
  231. goto do_shash_err;
  232. }
  233. }
  234. rc = crypto_shash_final(&sdesc->shash, result);
  235. if (rc)
  236. pr_err("%s: Could not generate %s hash\n", __func__, name);
  237. do_shash_err:
  238. crypto_free_shash(hash);
  239. kfree(sdesc);
  240. return rc;
  241. }
  242. #ifdef DEBUG
  243. /* Dump len bytes of a scatterlist starting at skip bytes into the sg */
  244. void __dump_sg(struct scatterlist *sg, unsigned int skip, unsigned int len)
  245. {
  246. u8 dbuf[16];
  247. unsigned int idx = skip;
  248. unsigned int num_out = 0; /* number of bytes dumped so far */
  249. unsigned int count;
  250. if (packet_debug_logging) {
  251. while (num_out < len) {
  252. count = (len - num_out > 16) ? 16 : len - num_out;
  253. sg_copy_part_to_buf(sg, dbuf, count, idx);
  254. num_out += count;
  255. print_hex_dump(KERN_ALERT, " sg: ", DUMP_PREFIX_NONE,
  256. 4, 1, dbuf, count, false);
  257. idx += 16;
  258. }
  259. }
  260. if (debug_logging_sleep)
  261. msleep(debug_logging_sleep);
  262. }
  263. #endif
  264. /* Returns the name for a given cipher alg/mode */
  265. char *spu_alg_name(enum spu_cipher_alg alg, enum spu_cipher_mode mode)
  266. {
  267. switch (alg) {
  268. case CIPHER_ALG_RC4:
  269. return "rc4";
  270. case CIPHER_ALG_AES:
  271. switch (mode) {
  272. case CIPHER_MODE_CBC:
  273. return "cbc(aes)";
  274. case CIPHER_MODE_ECB:
  275. return "ecb(aes)";
  276. case CIPHER_MODE_OFB:
  277. return "ofb(aes)";
  278. case CIPHER_MODE_CFB:
  279. return "cfb(aes)";
  280. case CIPHER_MODE_CTR:
  281. return "ctr(aes)";
  282. case CIPHER_MODE_XTS:
  283. return "xts(aes)";
  284. case CIPHER_MODE_GCM:
  285. return "gcm(aes)";
  286. default:
  287. return "aes";
  288. }
  289. break;
  290. case CIPHER_ALG_DES:
  291. switch (mode) {
  292. case CIPHER_MODE_CBC:
  293. return "cbc(des)";
  294. case CIPHER_MODE_ECB:
  295. return "ecb(des)";
  296. case CIPHER_MODE_CTR:
  297. return "ctr(des)";
  298. default:
  299. return "des";
  300. }
  301. break;
  302. case CIPHER_ALG_3DES:
  303. switch (mode) {
  304. case CIPHER_MODE_CBC:
  305. return "cbc(des3_ede)";
  306. case CIPHER_MODE_ECB:
  307. return "ecb(des3_ede)";
  308. case CIPHER_MODE_CTR:
  309. return "ctr(des3_ede)";
  310. default:
  311. return "3des";
  312. }
  313. break;
  314. default:
  315. return "other";
  316. }
  317. }
  318. static ssize_t spu_debugfs_read(struct file *filp, char __user *ubuf,
  319. size_t count, loff_t *offp)
  320. {
  321. struct bcm_device_private *ipriv;
  322. char *buf;
  323. ssize_t ret, out_offset, out_count;
  324. int i;
  325. u32 fifo_len;
  326. u32 spu_ofifo_ctrl;
  327. u32 alg;
  328. u32 mode;
  329. u32 op_cnt;
  330. out_count = 2048;
  331. buf = kmalloc(out_count, GFP_KERNEL);
  332. if (!buf)
  333. return -ENOMEM;
  334. ipriv = filp->private_data;
  335. out_offset = 0;
  336. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  337. "Number of SPUs.........%u\n",
  338. ipriv->spu.num_spu);
  339. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  340. "Current sessions.......%u\n",
  341. atomic_read(&ipriv->session_count));
  342. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  343. "Session count..........%u\n",
  344. atomic_read(&ipriv->stream_count));
  345. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  346. "Cipher setkey..........%u\n",
  347. atomic_read(&ipriv->setkey_cnt[SPU_OP_CIPHER]));
  348. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  349. "Cipher Ops.............%u\n",
  350. atomic_read(&ipriv->op_counts[SPU_OP_CIPHER]));
  351. for (alg = 0; alg < CIPHER_ALG_LAST; alg++) {
  352. for (mode = 0; mode < CIPHER_MODE_LAST; mode++) {
  353. op_cnt = atomic_read(&ipriv->cipher_cnt[alg][mode]);
  354. if (op_cnt) {
  355. out_offset += scnprintf(buf + out_offset,
  356. out_count - out_offset,
  357. " %-13s%11u\n",
  358. spu_alg_name(alg, mode), op_cnt);
  359. }
  360. }
  361. }
  362. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  363. "Hash Ops...............%u\n",
  364. atomic_read(&ipriv->op_counts[SPU_OP_HASH]));
  365. for (alg = 0; alg < HASH_ALG_LAST; alg++) {
  366. op_cnt = atomic_read(&ipriv->hash_cnt[alg]);
  367. if (op_cnt) {
  368. out_offset += scnprintf(buf + out_offset,
  369. out_count - out_offset,
  370. " %-13s%11u\n",
  371. hash_alg_name[alg], op_cnt);
  372. }
  373. }
  374. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  375. "HMAC setkey............%u\n",
  376. atomic_read(&ipriv->setkey_cnt[SPU_OP_HMAC]));
  377. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  378. "HMAC Ops...............%u\n",
  379. atomic_read(&ipriv->op_counts[SPU_OP_HMAC]));
  380. for (alg = 0; alg < HASH_ALG_LAST; alg++) {
  381. op_cnt = atomic_read(&ipriv->hmac_cnt[alg]);
  382. if (op_cnt) {
  383. out_offset += scnprintf(buf + out_offset,
  384. out_count - out_offset,
  385. " %-13s%11u\n",
  386. hash_alg_name[alg], op_cnt);
  387. }
  388. }
  389. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  390. "AEAD setkey............%u\n",
  391. atomic_read(&ipriv->setkey_cnt[SPU_OP_AEAD]));
  392. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  393. "AEAD Ops...............%u\n",
  394. atomic_read(&ipriv->op_counts[SPU_OP_AEAD]));
  395. for (alg = 0; alg < AEAD_TYPE_LAST; alg++) {
  396. op_cnt = atomic_read(&ipriv->aead_cnt[alg]);
  397. if (op_cnt) {
  398. out_offset += scnprintf(buf + out_offset,
  399. out_count - out_offset,
  400. " %-13s%11u\n",
  401. aead_alg_name[alg], op_cnt);
  402. }
  403. }
  404. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  405. "Bytes of req data......%llu\n",
  406. (u64)atomic64_read(&ipriv->bytes_out));
  407. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  408. "Bytes of resp data.....%llu\n",
  409. (u64)atomic64_read(&ipriv->bytes_in));
  410. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  411. "Mailbox full...........%u\n",
  412. atomic_read(&ipriv->mb_no_spc));
  413. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  414. "Mailbox send failures..%u\n",
  415. atomic_read(&ipriv->mb_send_fail));
  416. out_offset += scnprintf(buf + out_offset, out_count - out_offset,
  417. "Check ICV errors.......%u\n",
  418. atomic_read(&ipriv->bad_icv));
  419. if (ipriv->spu.spu_type == SPU_TYPE_SPUM)
  420. for (i = 0; i < ipriv->spu.num_spu; i++) {
  421. spu_ofifo_ctrl = ioread32(ipriv->spu.reg_vbase[i] +
  422. SPU_OFIFO_CTRL);
  423. fifo_len = spu_ofifo_ctrl & SPU_FIFO_WATERMARK;
  424. out_offset += scnprintf(buf + out_offset,
  425. out_count - out_offset,
  426. "SPU %d output FIFO high water.....%u\n",
  427. i, fifo_len);
  428. }
  429. if (out_offset > out_count)
  430. out_offset = out_count;
  431. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  432. kfree(buf);
  433. return ret;
  434. }
  435. static const struct file_operations spu_debugfs_stats = {
  436. .owner = THIS_MODULE,
  437. .open = simple_open,
  438. .read = spu_debugfs_read,
  439. };
  440. /*
  441. * Create the debug FS directories. If the top-level directory has not yet
  442. * been created, create it now. Create a stats file in this directory for
  443. * a SPU.
  444. */
  445. void spu_setup_debugfs(void)
  446. {
  447. if (!debugfs_initialized())
  448. return;
  449. if (!iproc_priv.debugfs_dir)
  450. iproc_priv.debugfs_dir = debugfs_create_dir(KBUILD_MODNAME,
  451. NULL);
  452. if (!iproc_priv.debugfs_stats)
  453. /* Create file with permissions S_IRUSR */
  454. debugfs_create_file("stats", 0400, iproc_priv.debugfs_dir,
  455. &iproc_priv, &spu_debugfs_stats);
  456. }
  457. void spu_free_debugfs(void)
  458. {
  459. debugfs_remove_recursive(iproc_priv.debugfs_dir);
  460. iproc_priv.debugfs_dir = NULL;
  461. }
  462. /**
  463. * format_value_ccm() - Format a value into a buffer, using a specified number
  464. * of bytes (i.e. maybe writing value X into a 4 byte
  465. * buffer, or maybe into a 12 byte buffer), as per the
  466. * SPU CCM spec.
  467. *
  468. * @val: value to write (up to max of unsigned int)
  469. * @buf: (pointer to) buffer to write the value
  470. * @len: number of bytes to use (0 to 255)
  471. *
  472. */
  473. void format_value_ccm(unsigned int val, u8 *buf, u8 len)
  474. {
  475. int i;
  476. /* First clear full output buffer */
  477. memset(buf, 0, len);
  478. /* Then, starting from right side, fill in with data */
  479. for (i = 0; i < len; i++) {
  480. buf[len - i - 1] = (val >> (8 * i)) & 0xff;
  481. if (i >= 3)
  482. break; /* Only handle up to 32 bits of 'val' */
  483. }
  484. }