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\n", __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. goto do_shash_err;
  249. }
  250. sdesc->shash.tfm = hash;
  251. sdesc->shash.flags = 0x0;
  252. if (key_len > 0) {
  253. rc = crypto_shash_setkey(hash, key, key_len);
  254. if (rc) {
  255. pr_err("%s: Could not setkey %s shash\n", __func__, name);
  256. goto do_shash_err;
  257. }
  258. }
  259. rc = crypto_shash_init(&sdesc->shash);
  260. if (rc) {
  261. pr_err("%s: Could not init %s shash\n", __func__, name);
  262. goto do_shash_err;
  263. }
  264. rc = crypto_shash_update(&sdesc->shash, data1, data1_len);
  265. if (rc) {
  266. pr_err("%s: Could not update1\n", __func__);
  267. goto do_shash_err;
  268. }
  269. if (data2 && data2_len) {
  270. rc = crypto_shash_update(&sdesc->shash, data2, data2_len);
  271. if (rc) {
  272. pr_err("%s: Could not update2\n", __func__);
  273. goto do_shash_err;
  274. }
  275. }
  276. rc = crypto_shash_final(&sdesc->shash, result);
  277. if (rc)
  278. pr_err("%s: Could not generate %s hash\n", __func__, name);
  279. do_shash_err:
  280. crypto_free_shash(hash);
  281. kfree(sdesc);
  282. return rc;
  283. }
  284. /* Dump len bytes of a scatterlist starting at skip bytes into the sg */
  285. void __dump_sg(struct scatterlist *sg, unsigned int skip, unsigned int len)
  286. {
  287. u8 dbuf[16];
  288. unsigned int idx = skip;
  289. unsigned int num_out = 0; /* number of bytes dumped so far */
  290. unsigned int count;
  291. if (packet_debug_logging) {
  292. while (num_out < len) {
  293. count = (len - num_out > 16) ? 16 : len - num_out;
  294. sg_copy_part_to_buf(sg, dbuf, count, idx);
  295. num_out += count;
  296. print_hex_dump(KERN_ALERT, " sg: ", DUMP_PREFIX_NONE,
  297. 4, 1, dbuf, count, false);
  298. idx += 16;
  299. }
  300. }
  301. if (debug_logging_sleep)
  302. msleep(debug_logging_sleep);
  303. }
  304. /* Returns the name for a given cipher alg/mode */
  305. char *spu_alg_name(enum spu_cipher_alg alg, enum spu_cipher_mode mode)
  306. {
  307. switch (alg) {
  308. case CIPHER_ALG_RC4:
  309. return "rc4";
  310. case CIPHER_ALG_AES:
  311. switch (mode) {
  312. case CIPHER_MODE_CBC:
  313. return "cbc(aes)";
  314. case CIPHER_MODE_ECB:
  315. return "ecb(aes)";
  316. case CIPHER_MODE_OFB:
  317. return "ofb(aes)";
  318. case CIPHER_MODE_CFB:
  319. return "cfb(aes)";
  320. case CIPHER_MODE_CTR:
  321. return "ctr(aes)";
  322. case CIPHER_MODE_XTS:
  323. return "xts(aes)";
  324. case CIPHER_MODE_GCM:
  325. return "gcm(aes)";
  326. default:
  327. return "aes";
  328. }
  329. break;
  330. case CIPHER_ALG_DES:
  331. switch (mode) {
  332. case CIPHER_MODE_CBC:
  333. return "cbc(des)";
  334. case CIPHER_MODE_ECB:
  335. return "ecb(des)";
  336. case CIPHER_MODE_CTR:
  337. return "ctr(des)";
  338. default:
  339. return "des";
  340. }
  341. break;
  342. case CIPHER_ALG_3DES:
  343. switch (mode) {
  344. case CIPHER_MODE_CBC:
  345. return "cbc(des3_ede)";
  346. case CIPHER_MODE_ECB:
  347. return "ecb(des3_ede)";
  348. case CIPHER_MODE_CTR:
  349. return "ctr(des3_ede)";
  350. default:
  351. return "3des";
  352. }
  353. break;
  354. default:
  355. return "other";
  356. }
  357. }
  358. static ssize_t spu_debugfs_read(struct file *filp, char __user *ubuf,
  359. size_t count, loff_t *offp)
  360. {
  361. struct device_private *ipriv;
  362. char *buf;
  363. ssize_t ret, out_offset, out_count;
  364. int i;
  365. u32 fifo_len;
  366. u32 spu_ofifo_ctrl;
  367. u32 alg;
  368. u32 mode;
  369. u32 op_cnt;
  370. out_count = 2048;
  371. buf = kmalloc(out_count, GFP_KERNEL);
  372. if (!buf)
  373. return -ENOMEM;
  374. ipriv = filp->private_data;
  375. out_offset = 0;
  376. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  377. "Number of SPUs.........%u\n",
  378. ipriv->spu.num_spu);
  379. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  380. "Current sessions.......%u\n",
  381. atomic_read(&ipriv->session_count));
  382. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  383. "Session count..........%u\n",
  384. atomic_read(&ipriv->stream_count));
  385. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  386. "Cipher setkey..........%u\n",
  387. atomic_read(&ipriv->setkey_cnt[SPU_OP_CIPHER]));
  388. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  389. "Cipher Ops.............%u\n",
  390. atomic_read(&ipriv->op_counts[SPU_OP_CIPHER]));
  391. for (alg = 0; alg < CIPHER_ALG_LAST; alg++) {
  392. for (mode = 0; mode < CIPHER_MODE_LAST; mode++) {
  393. op_cnt = atomic_read(&ipriv->cipher_cnt[alg][mode]);
  394. if (op_cnt) {
  395. out_offset += snprintf(buf + out_offset,
  396. out_count - out_offset,
  397. " %-13s%11u\n",
  398. spu_alg_name(alg, mode), op_cnt);
  399. }
  400. }
  401. }
  402. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  403. "Hash Ops...............%u\n",
  404. atomic_read(&ipriv->op_counts[SPU_OP_HASH]));
  405. for (alg = 0; alg < HASH_ALG_LAST; alg++) {
  406. op_cnt = atomic_read(&ipriv->hash_cnt[alg]);
  407. if (op_cnt) {
  408. out_offset += snprintf(buf + out_offset,
  409. out_count - out_offset,
  410. " %-13s%11u\n",
  411. hash_alg_name[alg], op_cnt);
  412. }
  413. }
  414. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  415. "HMAC setkey............%u\n",
  416. atomic_read(&ipriv->setkey_cnt[SPU_OP_HMAC]));
  417. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  418. "HMAC Ops...............%u\n",
  419. atomic_read(&ipriv->op_counts[SPU_OP_HMAC]));
  420. for (alg = 0; alg < HASH_ALG_LAST; alg++) {
  421. op_cnt = atomic_read(&ipriv->hmac_cnt[alg]);
  422. if (op_cnt) {
  423. out_offset += snprintf(buf + out_offset,
  424. out_count - out_offset,
  425. " %-13s%11u\n",
  426. hash_alg_name[alg], op_cnt);
  427. }
  428. }
  429. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  430. "AEAD setkey............%u\n",
  431. atomic_read(&ipriv->setkey_cnt[SPU_OP_AEAD]));
  432. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  433. "AEAD Ops...............%u\n",
  434. atomic_read(&ipriv->op_counts[SPU_OP_AEAD]));
  435. for (alg = 0; alg < AEAD_TYPE_LAST; alg++) {
  436. op_cnt = atomic_read(&ipriv->aead_cnt[alg]);
  437. if (op_cnt) {
  438. out_offset += snprintf(buf + out_offset,
  439. out_count - out_offset,
  440. " %-13s%11u\n",
  441. aead_alg_name[alg], op_cnt);
  442. }
  443. }
  444. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  445. "Bytes of req data......%llu\n",
  446. (u64)atomic64_read(&ipriv->bytes_out));
  447. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  448. "Bytes of resp data.....%llu\n",
  449. (u64)atomic64_read(&ipriv->bytes_in));
  450. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  451. "Mailbox full...........%u\n",
  452. atomic_read(&ipriv->mb_no_spc));
  453. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  454. "Mailbox send failures..%u\n",
  455. atomic_read(&ipriv->mb_send_fail));
  456. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  457. "Check ICV errors.......%u\n",
  458. atomic_read(&ipriv->bad_icv));
  459. if (ipriv->spu.spu_type == SPU_TYPE_SPUM)
  460. for (i = 0; i < ipriv->spu.num_spu; i++) {
  461. spu_ofifo_ctrl = ioread32(ipriv->spu.reg_vbase[i] +
  462. SPU_OFIFO_CTRL);
  463. fifo_len = spu_ofifo_ctrl & SPU_FIFO_WATERMARK;
  464. out_offset += snprintf(buf + out_offset,
  465. out_count - out_offset,
  466. "SPU %d output FIFO high water.....%u\n",
  467. i, fifo_len);
  468. }
  469. if (out_offset > out_count)
  470. out_offset = out_count;
  471. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  472. kfree(buf);
  473. return ret;
  474. }
  475. static const struct file_operations spu_debugfs_stats = {
  476. .owner = THIS_MODULE,
  477. .open = simple_open,
  478. .read = spu_debugfs_read,
  479. };
  480. /*
  481. * Create the debug FS directories. If the top-level directory has not yet
  482. * been created, create it now. Create a stats file in this directory for
  483. * a SPU.
  484. */
  485. void spu_setup_debugfs(void)
  486. {
  487. if (!debugfs_initialized())
  488. return;
  489. if (!iproc_priv.debugfs_dir)
  490. iproc_priv.debugfs_dir = debugfs_create_dir(KBUILD_MODNAME,
  491. NULL);
  492. if (!iproc_priv.debugfs_stats)
  493. /* Create file with permissions S_IRUSR */
  494. debugfs_create_file("stats", 0400, iproc_priv.debugfs_dir,
  495. &iproc_priv, &spu_debugfs_stats);
  496. }
  497. void spu_free_debugfs(void)
  498. {
  499. debugfs_remove_recursive(iproc_priv.debugfs_dir);
  500. iproc_priv.debugfs_dir = NULL;
  501. }
  502. /**
  503. * format_value_ccm() - Format a value into a buffer, using a specified number
  504. * of bytes (i.e. maybe writing value X into a 4 byte
  505. * buffer, or maybe into a 12 byte buffer), as per the
  506. * SPU CCM spec.
  507. *
  508. * @val: value to write (up to max of unsigned int)
  509. * @buf: (pointer to) buffer to write the value
  510. * @len: number of bytes to use (0 to 255)
  511. *
  512. */
  513. void format_value_ccm(unsigned int val, u8 *buf, u8 len)
  514. {
  515. int i;
  516. /* First clear full output buffer */
  517. memset(buf, 0, len);
  518. /* Then, starting from right side, fill in with data */
  519. for (i = 0; i < len; i++) {
  520. buf[len - i - 1] = (val >> (8 * i)) & 0xff;
  521. if (i >= 3)
  522. break; /* Only handle up to 32 bits of 'val' */
  523. }
  524. }