root/drivers/crypto/bcm/util.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. spu_sg_at_offset
  2. sg_copy_part_to_buf
  3. sg_copy_part_from_buf
  4. spu_sg_count
  5. spu_msg_sg_add
  6. add_to_ctr
  7. do_shash
  8. __dump_sg
  9. spu_alg_name
  10. spu_debugfs_read
  11. spu_setup_debugfs
  12. spu_free_debugfs
  13. format_value_ccm

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright 2016 Broadcom
   4  */
   5 
   6 #include <linux/debugfs.h>
   7 
   8 #include "cipher.h"
   9 #include "util.h"
  10 
  11 /* offset of SPU_OFIFO_CTRL register */
  12 #define SPU_OFIFO_CTRL      0x40
  13 #define SPU_FIFO_WATERMARK  0x1FF
  14 
  15 /**
  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
  22  *                    requested distance.
  23  *
  24  * Return: 0 if entry found at requested distance
  25  *         < 0 otherwise
  26  */
  27 int spu_sg_at_offset(struct scatterlist *sg, unsigned int skip,
  28                      struct scatterlist **sge, unsigned int *sge_offset)
  29 {
  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;
  34 
  35         next_index = sg->length;
  36         while (next_index <= skip) {
  37                 sg = sg_next(sg);
  38                 index = next_index;
  39                 if (!sg)
  40                         return -EINVAL;
  41                 next_index += sg->length;
  42         }
  43 
  44         *sge_offset = skip - index;
  45         *sge = sg;
  46         return 0;
  47 }
  48 
  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)
  52 {
  53         size_t copied;
  54         unsigned int nents = sg_nents(src);
  55 
  56         copied = sg_pcopy_to_buffer(src, nents, dest, len, skip);
  57         if (copied != len) {
  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);
  61         }
  62 }
  63 
  64 /*
  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.
  68  */
  69 void sg_copy_part_from_buf(struct scatterlist *dest, u8 *src,
  70                            unsigned int len, unsigned int skip)
  71 {
  72         size_t copied;
  73         unsigned int nents = sg_nents(dest);
  74 
  75         copied = sg_pcopy_from_buffer(dest, nents, src, len, skip);
  76         if (copied != len) {
  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);
  80         }
  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 
  99         if (!sg_list)
 100                 return 0;
 101 
 102         if (spu_sg_at_offset(sg_list, skip, &sg, &offset) < 0)
 103                 return 0;
 104 
 105         while (sg && (nbytes > 0)) {
 106                 sg_nents++;
 107                 nbytes -= (sg->length - offset);
 108                 offset = 0;
 109                 sg = sg_next(sg);
 110         }
 111         return sg_nents;
 112 }
 113 
 114 /**
 115  * spu_msg_sg_add() - Copy scatterlist entries from one sg to another, up to a
 116  * given length.
 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
 124  *               from_sg.
 125  *
 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.
 128  *
 129  * to_sg, from_sg, skip are all updated to end of copy
 130  *
 131  * Return: Number of bytes copied
 132  */
 133 u32 spu_msg_sg_add(struct scatterlist **to_sg,
 134                    struct scatterlist **from_sg, u32 *from_skip,
 135                    u8 from_nents, u32 length)
 136 {
 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;
 141         u32 offset;
 142         int i;
 143         u32 entry_len = 0;
 144         u32 frag_len = 0;       /* length of entry added to to_sg */
 145         u32 copied = 0;         /* number of bytes copied so far */
 146 
 147         if (length == 0)
 148                 return 0;
 149 
 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;
 155                 if (frag_len)
 156                         sg_set_page(to++, sg_page(sg), frag_len, offset);
 157                 copied += frag_len;
 158                 if (copied == entry_len) {
 159                         /* used up all of from entry */
 160                         skip = 0;       /* start at beginning of next entry */
 161                 }
 162                 if (copied == length)
 163                         break;
 164         }
 165         *to_sg = to;
 166         *from_sg = sg;
 167         if (frag_len < entry_len)
 168                 *from_skip = skip + frag_len;
 169         else
 170                 *from_skip = 0;
 171 
 172         return copied;
 173 }
 174 
 175 void add_to_ctr(u8 *ctr_pos, unsigned int increment)
 176 {
 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;
 181 
 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);
 186 }
 187 
 188 struct sdesc {
 189         struct shash_desc shash;
 190         char ctx[];
 191 };
 192 
 193 /**
 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)
 203  *
 204  * Note that the crypto API will not select this driver's own transform because
 205  * this driver only registers asynchronous algos.
 206  *
 207  * Return: 0 if hash successfully stored in result
 208  *         < 0 otherwise
 209  */
 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)
 214 {
 215         int rc;
 216         unsigned int size;
 217         struct crypto_shash *hash;
 218         struct sdesc *sdesc;
 219 
 220         hash = crypto_alloc_shash(name, 0, 0);
 221         if (IS_ERR(hash)) {
 222                 rc = PTR_ERR(hash);
 223                 pr_err("%s: Crypto %s allocation error %d\n", __func__, name, rc);
 224                 return rc;
 225         }
 226 
 227         size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
 228         sdesc = kmalloc(size, GFP_KERNEL);
 229         if (!sdesc) {
 230                 rc = -ENOMEM;
 231                 goto do_shash_err;
 232         }
 233         sdesc->shash.tfm = hash;
 234 
 235         if (key_len > 0) {
 236                 rc = crypto_shash_setkey(hash, key, key_len);
 237                 if (rc) {
 238                         pr_err("%s: Could not setkey %s shash\n", __func__, name);
 239                         goto do_shash_err;
 240                 }
 241         }
 242 
 243         rc = crypto_shash_init(&sdesc->shash);
 244         if (rc) {
 245                 pr_err("%s: Could not init %s shash\n", __func__, name);
 246                 goto do_shash_err;
 247         }
 248         rc = crypto_shash_update(&sdesc->shash, data1, data1_len);
 249         if (rc) {
 250                 pr_err("%s: Could not update1\n", __func__);
 251                 goto do_shash_err;
 252         }
 253         if (data2 && data2_len) {
 254                 rc = crypto_shash_update(&sdesc->shash, data2, data2_len);
 255                 if (rc) {
 256                         pr_err("%s: Could not update2\n", __func__);
 257                         goto do_shash_err;
 258                 }
 259         }
 260         rc = crypto_shash_final(&sdesc->shash, result);
 261         if (rc)
 262                 pr_err("%s: Could not generate %s hash\n", __func__, name);
 263 
 264 do_shash_err:
 265         crypto_free_shash(hash);
 266         kfree(sdesc);
 267 
 268         return rc;
 269 }
 270 
 271 /* Dump len bytes of a scatterlist starting at skip bytes into the sg */
 272 void __dump_sg(struct scatterlist *sg, unsigned int skip, unsigned int len)
 273 {
 274         u8 dbuf[16];
 275         unsigned int idx = skip;
 276         unsigned int num_out = 0;       /* number of bytes dumped so far */
 277         unsigned int count;
 278 
 279         if (packet_debug_logging) {
 280                 while (num_out < len) {
 281                         count = (len - num_out > 16) ? 16 : len - num_out;
 282                         sg_copy_part_to_buf(sg, dbuf, count, idx);
 283                         num_out += count;
 284                         print_hex_dump(KERN_ALERT, "  sg: ", DUMP_PREFIX_NONE,
 285                                        4, 1, dbuf, count, false);
 286                         idx += 16;
 287                 }
 288         }
 289         if (debug_logging_sleep)
 290                 msleep(debug_logging_sleep);
 291 }
 292 
 293 /* Returns the name for a given cipher alg/mode */
 294 char *spu_alg_name(enum spu_cipher_alg alg, enum spu_cipher_mode mode)
 295 {
 296         switch (alg) {
 297         case CIPHER_ALG_RC4:
 298                 return "rc4";
 299         case CIPHER_ALG_AES:
 300                 switch (mode) {
 301                 case CIPHER_MODE_CBC:
 302                         return "cbc(aes)";
 303                 case CIPHER_MODE_ECB:
 304                         return "ecb(aes)";
 305                 case CIPHER_MODE_OFB:
 306                         return "ofb(aes)";
 307                 case CIPHER_MODE_CFB:
 308                         return "cfb(aes)";
 309                 case CIPHER_MODE_CTR:
 310                         return "ctr(aes)";
 311                 case CIPHER_MODE_XTS:
 312                         return "xts(aes)";
 313                 case CIPHER_MODE_GCM:
 314                         return "gcm(aes)";
 315                 default:
 316                         return "aes";
 317                 }
 318                 break;
 319         case CIPHER_ALG_DES:
 320                 switch (mode) {
 321                 case CIPHER_MODE_CBC:
 322                         return "cbc(des)";
 323                 case CIPHER_MODE_ECB:
 324                         return "ecb(des)";
 325                 case CIPHER_MODE_CTR:
 326                         return "ctr(des)";
 327                 default:
 328                         return "des";
 329                 }
 330                 break;
 331         case CIPHER_ALG_3DES:
 332                 switch (mode) {
 333                 case CIPHER_MODE_CBC:
 334                         return "cbc(des3_ede)";
 335                 case CIPHER_MODE_ECB:
 336                         return "ecb(des3_ede)";
 337                 case CIPHER_MODE_CTR:
 338                         return "ctr(des3_ede)";
 339                 default:
 340                         return "3des";
 341                 }
 342                 break;
 343         default:
 344                 return "other";
 345         }
 346 }
 347 
 348 static ssize_t spu_debugfs_read(struct file *filp, char __user *ubuf,
 349                                 size_t count, loff_t *offp)
 350 {
 351         struct device_private *ipriv;
 352         char *buf;
 353         ssize_t ret, out_offset, out_count;
 354         int i;
 355         u32 fifo_len;
 356         u32 spu_ofifo_ctrl;
 357         u32 alg;
 358         u32 mode;
 359         u32 op_cnt;
 360 
 361         out_count = 2048;
 362 
 363         buf = kmalloc(out_count, GFP_KERNEL);
 364         if (!buf)
 365                 return -ENOMEM;
 366 
 367         ipriv = filp->private_data;
 368         out_offset = 0;
 369         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 370                                "Number of SPUs.........%u\n",
 371                                ipriv->spu.num_spu);
 372         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 373                                "Current sessions.......%u\n",
 374                                atomic_read(&ipriv->session_count));
 375         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 376                                "Session count..........%u\n",
 377                                atomic_read(&ipriv->stream_count));
 378         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 379                                "Cipher setkey..........%u\n",
 380                                atomic_read(&ipriv->setkey_cnt[SPU_OP_CIPHER]));
 381         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 382                                "Cipher Ops.............%u\n",
 383                                atomic_read(&ipriv->op_counts[SPU_OP_CIPHER]));
 384         for (alg = 0; alg < CIPHER_ALG_LAST; alg++) {
 385                 for (mode = 0; mode < CIPHER_MODE_LAST; mode++) {
 386                         op_cnt = atomic_read(&ipriv->cipher_cnt[alg][mode]);
 387                         if (op_cnt) {
 388                                 out_offset += snprintf(buf + out_offset,
 389                                                        out_count - out_offset,
 390                                "  %-13s%11u\n",
 391                                spu_alg_name(alg, mode), op_cnt);
 392                         }
 393                 }
 394         }
 395         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 396                                "Hash Ops...............%u\n",
 397                                atomic_read(&ipriv->op_counts[SPU_OP_HASH]));
 398         for (alg = 0; alg < HASH_ALG_LAST; alg++) {
 399                 op_cnt = atomic_read(&ipriv->hash_cnt[alg]);
 400                 if (op_cnt) {
 401                         out_offset += snprintf(buf + out_offset,
 402                                                out_count - out_offset,
 403                        "  %-13s%11u\n",
 404                        hash_alg_name[alg], op_cnt);
 405                 }
 406         }
 407         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 408                                "HMAC setkey............%u\n",
 409                                atomic_read(&ipriv->setkey_cnt[SPU_OP_HMAC]));
 410         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 411                                "HMAC Ops...............%u\n",
 412                                atomic_read(&ipriv->op_counts[SPU_OP_HMAC]));
 413         for (alg = 0; alg < HASH_ALG_LAST; alg++) {
 414                 op_cnt = atomic_read(&ipriv->hmac_cnt[alg]);
 415                 if (op_cnt) {
 416                         out_offset += snprintf(buf + out_offset,
 417                                                out_count - out_offset,
 418                        "  %-13s%11u\n",
 419                        hash_alg_name[alg], op_cnt);
 420                 }
 421         }
 422         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 423                                "AEAD setkey............%u\n",
 424                                atomic_read(&ipriv->setkey_cnt[SPU_OP_AEAD]));
 425 
 426         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 427                                "AEAD Ops...............%u\n",
 428                                atomic_read(&ipriv->op_counts[SPU_OP_AEAD]));
 429         for (alg = 0; alg < AEAD_TYPE_LAST; alg++) {
 430                 op_cnt = atomic_read(&ipriv->aead_cnt[alg]);
 431                 if (op_cnt) {
 432                         out_offset += snprintf(buf + out_offset,
 433                                                out_count - out_offset,
 434                        "  %-13s%11u\n",
 435                        aead_alg_name[alg], op_cnt);
 436                 }
 437         }
 438         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 439                                "Bytes of req data......%llu\n",
 440                                (u64)atomic64_read(&ipriv->bytes_out));
 441         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 442                                "Bytes of resp data.....%llu\n",
 443                                (u64)atomic64_read(&ipriv->bytes_in));
 444         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 445                                "Mailbox full...........%u\n",
 446                                atomic_read(&ipriv->mb_no_spc));
 447         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 448                                "Mailbox send failures..%u\n",
 449                                atomic_read(&ipriv->mb_send_fail));
 450         out_offset += snprintf(buf + out_offset, out_count - out_offset,
 451                                "Check ICV errors.......%u\n",
 452                                atomic_read(&ipriv->bad_icv));
 453         if (ipriv->spu.spu_type == SPU_TYPE_SPUM)
 454                 for (i = 0; i < ipriv->spu.num_spu; i++) {
 455                         spu_ofifo_ctrl = ioread32(ipriv->spu.reg_vbase[i] +
 456                                                   SPU_OFIFO_CTRL);
 457                         fifo_len = spu_ofifo_ctrl & SPU_FIFO_WATERMARK;
 458                         out_offset += snprintf(buf + out_offset,
 459                                                out_count - out_offset,
 460                                        "SPU %d output FIFO high water.....%u\n",
 461                                        i, fifo_len);
 462                 }
 463 
 464         if (out_offset > out_count)
 465                 out_offset = out_count;
 466 
 467         ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
 468         kfree(buf);
 469         return ret;
 470 }
 471 
 472 static const struct file_operations spu_debugfs_stats = {
 473         .owner = THIS_MODULE,
 474         .open = simple_open,
 475         .read = spu_debugfs_read,
 476 };
 477 
 478 /*
 479  * Create the debug FS directories. If the top-level directory has not yet
 480  * been created, create it now. Create a stats file in this directory for
 481  * a SPU.
 482  */
 483 void spu_setup_debugfs(void)
 484 {
 485         if (!debugfs_initialized())
 486                 return;
 487 
 488         if (!iproc_priv.debugfs_dir)
 489                 iproc_priv.debugfs_dir = debugfs_create_dir(KBUILD_MODNAME,
 490                                                             NULL);
 491 
 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 
 498 void spu_free_debugfs(void)
 499 {
 500         debugfs_remove_recursive(iproc_priv.debugfs_dir);
 501         iproc_priv.debugfs_dir = NULL;
 502 }
 503 
 504 /**
 505  * format_value_ccm() - Format a value into a buffer, using a specified number
 506  *                      of bytes (i.e. maybe writing value X into a 4 byte
 507  *                      buffer, or maybe into a 12 byte buffer), as per the
 508  *                      SPU CCM spec.
 509  *
 510  * @val:                value to write (up to max of unsigned int)
 511  * @buf:                (pointer to) buffer to write the value
 512  * @len:                number of bytes to use (0 to 255)
 513  *
 514  */
 515 void format_value_ccm(unsigned int val, u8 *buf, u8 len)
 516 {
 517         int i;
 518 
 519         /* First clear full output buffer */
 520         memset(buf, 0, len);
 521 
 522         /* Then, starting from right side, fill in with data */
 523         for (i = 0; i < len; i++) {
 524                 buf[len - i - 1] = (val >> (8 * i)) & 0xff;
 525                 if (i >= 3)
 526                         break;  /* Only handle up to 32 bits of 'val' */
 527         }
 528 }

/* [<][>][^][v][top][bottom][index][help] */