1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2016 Broadcom
6 #include <linux/debugfs.h>
11 /* offset of SPU_OFIFO_CTRL register */
12 #define SPU_OFIFO_CTRL 0x40
13 #define SPU_FIFO_WATERMARK 0x1FF
16 * spu_sg_at_offset() - Find the scatterlist entry at a given distance from the
17 * start of a scatterlist.
18 * @sg: [in] Start of a scatterlist
19 * @skip: [in] Distance from the start of the scatterlist, in bytes
20 * @sge: [out] Scatterlist entry at skip bytes from start
21 * @sge_offset: [out] Number of bytes from start of sge buffer to get to
24 * Return: 0 if entry found at requested distance
27 int spu_sg_at_offset(struct scatterlist *sg, unsigned int skip,
28 struct scatterlist **sge, unsigned int *sge_offset)
30 /* byte index from start of sg to the end of the previous entry */
31 unsigned int index = 0;
32 /* byte index from start of sg to the end of the current entry */
33 unsigned int next_index;
35 next_index = sg->length;
36 while (next_index <= skip) {
41 next_index += sg->length;
44 *sge_offset = skip - index;
49 /* Copy len bytes of sg data, starting at offset skip, to a dest buffer */
50 void sg_copy_part_to_buf(struct scatterlist *src, u8 *dest,
51 unsigned int len, unsigned int skip)
54 unsigned int nents = sg_nents(src);
56 copied = sg_pcopy_to_buffer(src, nents, dest, len, skip);
58 flow_log("%s copied %u bytes of %u requested. ",
59 __func__, (u32)copied, len);
60 flow_log("sg with %u entries and skip %u\n", nents, skip);
65 * Copy data into a scatterlist starting at a specified offset in the
66 * scatterlist. Specifically, copy len bytes of data in the buffer src
67 * into the scatterlist dest, starting skip bytes into the scatterlist.
69 void sg_copy_part_from_buf(struct scatterlist *dest, u8 *src,
70 unsigned int len, unsigned int skip)
73 unsigned int nents = sg_nents(dest);
75 copied = sg_pcopy_from_buffer(dest, nents, src, len, skip);
77 flow_log("%s copied %u bytes of %u requested. ",
78 __func__, (u32)copied, len);
79 flow_log("sg with %u entries and skip %u\n", nents, skip);
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
91 * Return: the number of sg entries contributing to nbytes of data
93 int spu_sg_count(struct scatterlist *sg_list, unsigned int skip, int nbytes)
95 struct scatterlist *sg;
102 if (spu_sg_at_offset(sg_list, skip, &sg, &offset) < 0)
105 while (sg && (nbytes > 0)) {
107 nbytes -= (sg->length - offset);
115 * spu_msg_sg_add() - Copy scatterlist entries from one sg to another, up to a
117 * @to_sg: scatterlist to copy to
118 * @from_sg: scatterlist to copy from
119 * @from_skip: number of bytes to skip in from_sg. Non-zero when previous
120 * request included part of the buffer in entry in from_sg.
121 * Assumes from_skip < from_sg->length.
122 * @from_nents: number of entries in from_sg
123 * @length: number of bytes to copy. may reach this limit before exhausting
126 * Copies the entries themselves, not the data in the entries. Assumes to_sg has
127 * enough entries. Does not limit the size of an individual buffer in to_sg.
129 * to_sg, from_sg, skip are all updated to end of copy
131 * Return: Number of bytes copied
133 u32 spu_msg_sg_add(struct scatterlist **to_sg,
134 struct scatterlist **from_sg, u32 *from_skip,
135 u8 from_nents, u32 length)
137 struct scatterlist *sg; /* an entry in from_sg */
138 struct scatterlist *to = *to_sg;
139 struct scatterlist *from = *from_sg;
140 u32 skip = *from_skip;
144 u32 frag_len = 0; /* length of entry added to to_sg */
145 u32 copied = 0; /* number of bytes copied so far */
150 for_each_sg(from, sg, from_nents, i) {
151 /* number of bytes in this from entry not yet used */
152 entry_len = sg->length - skip;
153 frag_len = min(entry_len, length - copied);
154 offset = sg->offset + skip;
156 sg_set_page(to++, sg_page(sg), frag_len, offset);
158 if (copied == entry_len) {
159 /* used up all of from entry */
160 skip = 0; /* start at beginning of next entry */
162 if (copied == length)
167 if (frag_len < entry_len)
168 *from_skip = skip + frag_len;
175 void add_to_ctr(u8 *ctr_pos, unsigned int increment)
177 __be64 *high_be = (__be64 *)ctr_pos;
178 __be64 *low_be = high_be + 1;
179 u64 orig_low = __be64_to_cpu(*low_be);
180 u64 new_low = orig_low + (u64)increment;
182 *low_be = __cpu_to_be64(new_low);
183 if (new_low < orig_low)
184 /* there was a carry from the low 8 bytes */
185 *high_be = __cpu_to_be64(__be64_to_cpu(*high_be) + 1);
189 struct shash_desc shash;
194 * do_shash() - Do a synchronous hash operation in software
195 * @name: The name of the hash algorithm
196 * @result: Buffer where digest is to be written
197 * @data1: First part of data to hash. May be NULL.
198 * @data1_len: Length of data1, in bytes
199 * @data2: Second part of data to hash. May be NULL.
200 * @data2_len: Length of data2, in bytes
201 * @key: Key (if keyed hash)
202 * @key_len: Length of key, in bytes (or 0 if non-keyed hash)
204 * Note that the crypto API will not select this driver's own transform because
205 * this driver only registers asynchronous algos.
207 * Return: 0 if hash successfully stored in result
210 int do_shash(unsigned char *name, unsigned char *result,
211 const u8 *data1, unsigned int data1_len,
212 const u8 *data2, unsigned int data2_len,
213 const u8 *key, unsigned int key_len)
217 struct crypto_shash *hash;
220 hash = crypto_alloc_shash(name, 0, 0);
223 pr_err("%s: Crypto %s allocation error %d\n", __func__, name, rc);
227 size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
228 sdesc = kmalloc(size, GFP_KERNEL);
233 sdesc->shash.tfm = hash;
236 rc = crypto_shash_setkey(hash, key, key_len);
238 pr_err("%s: Could not setkey %s shash\n", __func__, name);
243 rc = crypto_shash_init(&sdesc->shash);
245 pr_err("%s: Could not init %s shash\n", __func__, name);
248 rc = crypto_shash_update(&sdesc->shash, data1, data1_len);
250 pr_err("%s: Could not update1\n", __func__);
253 if (data2 && data2_len) {
254 rc = crypto_shash_update(&sdesc->shash, data2, data2_len);
256 pr_err("%s: Could not update2\n", __func__);
260 rc = crypto_shash_final(&sdesc->shash, result);
262 pr_err("%s: Could not generate %s hash\n", __func__, name);
265 crypto_free_shash(hash);
272 /* Dump len bytes of a scatterlist starting at skip bytes into the sg */
273 void __dump_sg(struct scatterlist *sg, unsigned int skip, unsigned int len)
276 unsigned int idx = skip;
277 unsigned int num_out = 0; /* number of bytes dumped so far */
280 if (packet_debug_logging) {
281 while (num_out < len) {
282 count = (len - num_out > 16) ? 16 : len - num_out;
283 sg_copy_part_to_buf(sg, dbuf, count, idx);
285 print_hex_dump(KERN_ALERT, " sg: ", DUMP_PREFIX_NONE,
286 4, 1, dbuf, count, false);
290 if (debug_logging_sleep)
291 msleep(debug_logging_sleep);
295 /* Returns the name for a given cipher alg/mode */
296 char *spu_alg_name(enum spu_cipher_alg alg, enum spu_cipher_mode mode)
303 case CIPHER_MODE_CBC:
305 case CIPHER_MODE_ECB:
307 case CIPHER_MODE_OFB:
309 case CIPHER_MODE_CFB:
311 case CIPHER_MODE_CTR:
313 case CIPHER_MODE_XTS:
315 case CIPHER_MODE_GCM:
323 case CIPHER_MODE_CBC:
325 case CIPHER_MODE_ECB:
327 case CIPHER_MODE_CTR:
333 case CIPHER_ALG_3DES:
335 case CIPHER_MODE_CBC:
336 return "cbc(des3_ede)";
337 case CIPHER_MODE_ECB:
338 return "ecb(des3_ede)";
339 case CIPHER_MODE_CTR:
340 return "ctr(des3_ede)";
350 static ssize_t spu_debugfs_read(struct file *filp, char __user *ubuf,
351 size_t count, loff_t *offp)
353 struct bcm_device_private *ipriv;
355 ssize_t ret, out_offset, out_count;
365 buf = kmalloc(out_count, GFP_KERNEL);
369 ipriv = filp->private_data;
371 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
372 "Number of SPUs.........%u\n",
374 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
375 "Current sessions.......%u\n",
376 atomic_read(&ipriv->session_count));
377 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
378 "Session count..........%u\n",
379 atomic_read(&ipriv->stream_count));
380 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
381 "Cipher setkey..........%u\n",
382 atomic_read(&ipriv->setkey_cnt[SPU_OP_CIPHER]));
383 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
384 "Cipher Ops.............%u\n",
385 atomic_read(&ipriv->op_counts[SPU_OP_CIPHER]));
386 for (alg = 0; alg < CIPHER_ALG_LAST; alg++) {
387 for (mode = 0; mode < CIPHER_MODE_LAST; mode++) {
388 op_cnt = atomic_read(&ipriv->cipher_cnt[alg][mode]);
390 out_offset += scnprintf(buf + out_offset,
391 out_count - out_offset,
393 spu_alg_name(alg, mode), op_cnt);
397 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
398 "Hash Ops...............%u\n",
399 atomic_read(&ipriv->op_counts[SPU_OP_HASH]));
400 for (alg = 0; alg < HASH_ALG_LAST; alg++) {
401 op_cnt = atomic_read(&ipriv->hash_cnt[alg]);
403 out_offset += scnprintf(buf + out_offset,
404 out_count - out_offset,
406 hash_alg_name[alg], op_cnt);
409 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
410 "HMAC setkey............%u\n",
411 atomic_read(&ipriv->setkey_cnt[SPU_OP_HMAC]));
412 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
413 "HMAC Ops...............%u\n",
414 atomic_read(&ipriv->op_counts[SPU_OP_HMAC]));
415 for (alg = 0; alg < HASH_ALG_LAST; alg++) {
416 op_cnt = atomic_read(&ipriv->hmac_cnt[alg]);
418 out_offset += scnprintf(buf + out_offset,
419 out_count - out_offset,
421 hash_alg_name[alg], op_cnt);
424 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
425 "AEAD setkey............%u\n",
426 atomic_read(&ipriv->setkey_cnt[SPU_OP_AEAD]));
428 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
429 "AEAD Ops...............%u\n",
430 atomic_read(&ipriv->op_counts[SPU_OP_AEAD]));
431 for (alg = 0; alg < AEAD_TYPE_LAST; alg++) {
432 op_cnt = atomic_read(&ipriv->aead_cnt[alg]);
434 out_offset += scnprintf(buf + out_offset,
435 out_count - out_offset,
437 aead_alg_name[alg], op_cnt);
440 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
441 "Bytes of req data......%llu\n",
442 (u64)atomic64_read(&ipriv->bytes_out));
443 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
444 "Bytes of resp data.....%llu\n",
445 (u64)atomic64_read(&ipriv->bytes_in));
446 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
447 "Mailbox full...........%u\n",
448 atomic_read(&ipriv->mb_no_spc));
449 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
450 "Mailbox send failures..%u\n",
451 atomic_read(&ipriv->mb_send_fail));
452 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
453 "Check ICV errors.......%u\n",
454 atomic_read(&ipriv->bad_icv));
455 if (ipriv->spu.spu_type == SPU_TYPE_SPUM)
456 for (i = 0; i < ipriv->spu.num_spu; i++) {
457 spu_ofifo_ctrl = ioread32(ipriv->spu.reg_vbase[i] +
459 fifo_len = spu_ofifo_ctrl & SPU_FIFO_WATERMARK;
460 out_offset += scnprintf(buf + out_offset,
461 out_count - out_offset,
462 "SPU %d output FIFO high water.....%u\n",
466 if (out_offset > out_count)
467 out_offset = out_count;
469 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
474 static const struct file_operations spu_debugfs_stats = {
475 .owner = THIS_MODULE,
477 .read = spu_debugfs_read,
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
485 void spu_setup_debugfs(void)
487 if (!debugfs_initialized())
490 if (!iproc_priv.debugfs_dir)
491 iproc_priv.debugfs_dir = debugfs_create_dir(KBUILD_MODNAME,
494 if (!iproc_priv.debugfs_stats)
495 /* Create file with permissions S_IRUSR */
496 debugfs_create_file("stats", 0400, iproc_priv.debugfs_dir,
497 &iproc_priv, &spu_debugfs_stats);
500 void spu_free_debugfs(void)
502 debugfs_remove_recursive(iproc_priv.debugfs_dir);
503 iproc_priv.debugfs_dir = NULL;
507 * format_value_ccm() - Format a value into a buffer, using a specified number
508 * of bytes (i.e. maybe writing value X into a 4 byte
509 * buffer, or maybe into a 12 byte buffer), as per the
512 * @val: value to write (up to max of unsigned int)
513 * @buf: (pointer to) buffer to write the value
514 * @len: number of bytes to use (0 to 255)
517 void format_value_ccm(unsigned int val, u8 *buf, u8 len)
521 /* First clear full output buffer */
524 /* Then, starting from right side, fill in with data */
525 for (i = 0; i < len; i++) {
526 buf[len - i - 1] = (val >> (8 * i)) & 0xff;
528 break; /* Only handle up to 32 bits of 'val' */