util.c 16 KB

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