root/drivers/crypto/chelsio/chtls/chtls_hw.c

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

DEFINITIONS

This source file includes following definitions.
  1. __set_tcb_field_direct
  2. __set_tcb_field
  3. chtls_set_tcb_field
  4. chtls_set_tcb_tflag
  5. chtls_set_tcb_keyid
  6. chtls_set_tcb_seqno
  7. chtls_set_tcb_quiesce
  8. chtls_init_kmap
  9. get_new_keyid
  10. free_tls_keyid
  11. keyid_to_addr
  12. chtls_rxkey_ivauth
  13. chtls_key_info
  14. chtls_set_scmd
  15. chtls_setkey

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (c) 2018 Chelsio Communications, Inc.
   4  *
   5  * Written by: Atul Gupta (atul.gupta@chelsio.com)
   6  */
   7 
   8 #include <linux/module.h>
   9 #include <linux/list.h>
  10 #include <linux/workqueue.h>
  11 #include <linux/skbuff.h>
  12 #include <linux/timer.h>
  13 #include <linux/notifier.h>
  14 #include <linux/inetdevice.h>
  15 #include <linux/ip.h>
  16 #include <linux/tcp.h>
  17 #include <linux/tls.h>
  18 #include <net/tls.h>
  19 
  20 #include "chtls.h"
  21 #include "chtls_cm.h"
  22 
  23 static void __set_tcb_field_direct(struct chtls_sock *csk,
  24                                    struct cpl_set_tcb_field *req, u16 word,
  25                                    u64 mask, u64 val, u8 cookie, int no_reply)
  26 {
  27         struct ulptx_idata *sc;
  28 
  29         INIT_TP_WR_CPL(req, CPL_SET_TCB_FIELD, csk->tid);
  30         req->wr.wr_mid |= htonl(FW_WR_FLOWID_V(csk->tid));
  31         req->reply_ctrl = htons(NO_REPLY_V(no_reply) |
  32                                 QUEUENO_V(csk->rss_qid));
  33         req->word_cookie = htons(TCB_WORD_V(word) | TCB_COOKIE_V(cookie));
  34         req->mask = cpu_to_be64(mask);
  35         req->val = cpu_to_be64(val);
  36         sc = (struct ulptx_idata *)(req + 1);
  37         sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_NOOP));
  38         sc->len = htonl(0);
  39 }
  40 
  41 static void __set_tcb_field(struct sock *sk, struct sk_buff *skb, u16 word,
  42                             u64 mask, u64 val, u8 cookie, int no_reply)
  43 {
  44         struct cpl_set_tcb_field *req;
  45         struct chtls_sock *csk;
  46         struct ulptx_idata *sc;
  47         unsigned int wrlen;
  48 
  49         wrlen = roundup(sizeof(*req) + sizeof(*sc), 16);
  50         csk = rcu_dereference_sk_user_data(sk);
  51 
  52         req = (struct cpl_set_tcb_field *)__skb_put(skb, wrlen);
  53         __set_tcb_field_direct(csk, req, word, mask, val, cookie, no_reply);
  54         set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
  55 }
  56 
  57 /*
  58  * Send control message to HW, message go as immediate data and packet
  59  * is freed immediately.
  60  */
  61 static int chtls_set_tcb_field(struct sock *sk, u16 word, u64 mask, u64 val)
  62 {
  63         struct cpl_set_tcb_field *req;
  64         unsigned int credits_needed;
  65         struct chtls_sock *csk;
  66         struct ulptx_idata *sc;
  67         struct sk_buff *skb;
  68         unsigned int wrlen;
  69         int ret;
  70 
  71         wrlen = roundup(sizeof(*req) + sizeof(*sc), 16);
  72 
  73         skb = alloc_skb(wrlen, GFP_ATOMIC);
  74         if (!skb)
  75                 return -ENOMEM;
  76 
  77         credits_needed = DIV_ROUND_UP(wrlen, 16);
  78         csk = rcu_dereference_sk_user_data(sk);
  79 
  80         __set_tcb_field(sk, skb, word, mask, val, 0, 1);
  81         skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
  82         csk->wr_credits -= credits_needed;
  83         csk->wr_unacked += credits_needed;
  84         enqueue_wr(csk, skb);
  85         ret = cxgb4_ofld_send(csk->egress_dev, skb);
  86         if (ret < 0)
  87                 kfree_skb(skb);
  88         return ret < 0 ? ret : 0;
  89 }
  90 
  91 /*
  92  * Set one of the t_flags bits in the TCB.
  93  */
  94 int chtls_set_tcb_tflag(struct sock *sk, unsigned int bit_pos, int val)
  95 {
  96         return chtls_set_tcb_field(sk, 1, 1ULL << bit_pos,
  97                                    (u64)val << bit_pos);
  98 }
  99 
 100 static int chtls_set_tcb_keyid(struct sock *sk, int keyid)
 101 {
 102         return chtls_set_tcb_field(sk, 31, 0xFFFFFFFFULL, keyid);
 103 }
 104 
 105 static int chtls_set_tcb_seqno(struct sock *sk)
 106 {
 107         return chtls_set_tcb_field(sk, 28, ~0ULL, 0);
 108 }
 109 
 110 static int chtls_set_tcb_quiesce(struct sock *sk, int val)
 111 {
 112         return chtls_set_tcb_field(sk, 1, (1ULL << TF_RX_QUIESCE_S),
 113                                    TF_RX_QUIESCE_V(val));
 114 }
 115 
 116 /* TLS Key bitmap processing */
 117 int chtls_init_kmap(struct chtls_dev *cdev, struct cxgb4_lld_info *lldi)
 118 {
 119         unsigned int num_key_ctx, bsize;
 120         int ksize;
 121 
 122         num_key_ctx = (lldi->vr->key.size / TLS_KEY_CONTEXT_SZ);
 123         bsize = BITS_TO_LONGS(num_key_ctx);
 124 
 125         cdev->kmap.size = num_key_ctx;
 126         cdev->kmap.available = bsize;
 127         ksize = sizeof(*cdev->kmap.addr) * bsize;
 128         cdev->kmap.addr = kvzalloc(ksize, GFP_KERNEL);
 129         if (!cdev->kmap.addr)
 130                 return -ENOMEM;
 131 
 132         cdev->kmap.start = lldi->vr->key.start;
 133         spin_lock_init(&cdev->kmap.lock);
 134         return 0;
 135 }
 136 
 137 static int get_new_keyid(struct chtls_sock *csk, u32 optname)
 138 {
 139         struct net_device *dev = csk->egress_dev;
 140         struct chtls_dev *cdev = csk->cdev;
 141         struct chtls_hws *hws;
 142         struct adapter *adap;
 143         int keyid;
 144 
 145         adap = netdev2adap(dev);
 146         hws = &csk->tlshws;
 147 
 148         spin_lock_bh(&cdev->kmap.lock);
 149         keyid = find_first_zero_bit(cdev->kmap.addr, cdev->kmap.size);
 150         if (keyid < cdev->kmap.size) {
 151                 __set_bit(keyid, cdev->kmap.addr);
 152                 if (optname == TLS_RX)
 153                         hws->rxkey = keyid;
 154                 else
 155                         hws->txkey = keyid;
 156                 atomic_inc(&adap->chcr_stats.tls_key);
 157         } else {
 158                 keyid = -1;
 159         }
 160         spin_unlock_bh(&cdev->kmap.lock);
 161         return keyid;
 162 }
 163 
 164 void free_tls_keyid(struct sock *sk)
 165 {
 166         struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
 167         struct net_device *dev = csk->egress_dev;
 168         struct chtls_dev *cdev = csk->cdev;
 169         struct chtls_hws *hws;
 170         struct adapter *adap;
 171 
 172         if (!cdev->kmap.addr)
 173                 return;
 174 
 175         adap = netdev2adap(dev);
 176         hws = &csk->tlshws;
 177 
 178         spin_lock_bh(&cdev->kmap.lock);
 179         if (hws->rxkey >= 0) {
 180                 __clear_bit(hws->rxkey, cdev->kmap.addr);
 181                 atomic_dec(&adap->chcr_stats.tls_key);
 182                 hws->rxkey = -1;
 183         }
 184         if (hws->txkey >= 0) {
 185                 __clear_bit(hws->txkey, cdev->kmap.addr);
 186                 atomic_dec(&adap->chcr_stats.tls_key);
 187                 hws->txkey = -1;
 188         }
 189         spin_unlock_bh(&cdev->kmap.lock);
 190 }
 191 
 192 unsigned int keyid_to_addr(int start_addr, int keyid)
 193 {
 194         return (start_addr + (keyid * TLS_KEY_CONTEXT_SZ)) >> 5;
 195 }
 196 
 197 static void chtls_rxkey_ivauth(struct _key_ctx *kctx)
 198 {
 199         kctx->iv_to_auth = cpu_to_be64(KEYCTX_TX_WR_IV_V(6ULL) |
 200                                   KEYCTX_TX_WR_AAD_V(1ULL) |
 201                                   KEYCTX_TX_WR_AADST_V(5ULL) |
 202                                   KEYCTX_TX_WR_CIPHER_V(14ULL) |
 203                                   KEYCTX_TX_WR_CIPHERST_V(0ULL) |
 204                                   KEYCTX_TX_WR_AUTH_V(14ULL) |
 205                                   KEYCTX_TX_WR_AUTHST_V(16ULL) |
 206                                   KEYCTX_TX_WR_AUTHIN_V(16ULL));
 207 }
 208 
 209 static int chtls_key_info(struct chtls_sock *csk,
 210                           struct _key_ctx *kctx,
 211                           u32 keylen, u32 optname)
 212 {
 213         unsigned char key[AES_KEYSIZE_128];
 214         struct tls12_crypto_info_aes_gcm_128 *gcm_ctx;
 215         unsigned char ghash_h[AEAD_H_SIZE];
 216         int ck_size, key_ctx_size;
 217         struct crypto_aes_ctx aes;
 218         int ret;
 219 
 220         gcm_ctx = (struct tls12_crypto_info_aes_gcm_128 *)
 221                   &csk->tlshws.crypto_info;
 222 
 223         key_ctx_size = sizeof(struct _key_ctx) +
 224                        roundup(keylen, 16) + AEAD_H_SIZE;
 225 
 226         if (keylen == AES_KEYSIZE_128) {
 227                 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
 228         } else {
 229                 pr_err("GCM: Invalid key length %d\n", keylen);
 230                 return -EINVAL;
 231         }
 232         memcpy(key, gcm_ctx->key, keylen);
 233 
 234         /* Calculate the H = CIPH(K, 0 repeated 16 times).
 235          * It will go in key context
 236          */
 237         ret = aes_expandkey(&aes, key, keylen);
 238         if (ret)
 239                 return ret;
 240 
 241         memset(ghash_h, 0, AEAD_H_SIZE);
 242         aes_encrypt(&aes, ghash_h, ghash_h);
 243         memzero_explicit(&aes, sizeof(aes));
 244         csk->tlshws.keylen = key_ctx_size;
 245 
 246         /* Copy the Key context */
 247         if (optname == TLS_RX) {
 248                 int key_ctx;
 249 
 250                 key_ctx = ((key_ctx_size >> 4) << 3);
 251                 kctx->ctx_hdr = FILL_KEY_CRX_HDR(ck_size,
 252                                                  CHCR_KEYCTX_MAC_KEY_SIZE_128,
 253                                                  0, 0, key_ctx);
 254                 chtls_rxkey_ivauth(kctx);
 255         } else {
 256                 kctx->ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
 257                                                  CHCR_KEYCTX_MAC_KEY_SIZE_128,
 258                                                  0, 0, key_ctx_size >> 4);
 259         }
 260 
 261         memcpy(kctx->salt, gcm_ctx->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
 262         memcpy(kctx->key, gcm_ctx->key, keylen);
 263         memcpy(kctx->key + keylen, ghash_h, AEAD_H_SIZE);
 264         /* erase key info from driver */
 265         memset(gcm_ctx->key, 0, keylen);
 266 
 267         return 0;
 268 }
 269 
 270 static void chtls_set_scmd(struct chtls_sock *csk)
 271 {
 272         struct chtls_hws *hws = &csk->tlshws;
 273 
 274         hws->scmd.seqno_numivs =
 275                 SCMD_SEQ_NO_CTRL_V(3) |
 276                 SCMD_PROTO_VERSION_V(0) |
 277                 SCMD_ENC_DEC_CTRL_V(0) |
 278                 SCMD_CIPH_AUTH_SEQ_CTRL_V(1) |
 279                 SCMD_CIPH_MODE_V(2) |
 280                 SCMD_AUTH_MODE_V(4) |
 281                 SCMD_HMAC_CTRL_V(0) |
 282                 SCMD_IV_SIZE_V(4) |
 283                 SCMD_NUM_IVS_V(1);
 284 
 285         hws->scmd.ivgen_hdrlen =
 286                 SCMD_IV_GEN_CTRL_V(1) |
 287                 SCMD_KEY_CTX_INLINE_V(0) |
 288                 SCMD_TLS_FRAG_ENABLE_V(1);
 289 }
 290 
 291 int chtls_setkey(struct chtls_sock *csk, u32 keylen, u32 optname)
 292 {
 293         struct tls_key_req *kwr;
 294         struct chtls_dev *cdev;
 295         struct _key_ctx *kctx;
 296         int wrlen, klen, len;
 297         struct sk_buff *skb;
 298         struct sock *sk;
 299         int keyid;
 300         int kaddr;
 301         int ret;
 302 
 303         cdev = csk->cdev;
 304         sk = csk->sk;
 305 
 306         klen = roundup((keylen + AEAD_H_SIZE) + sizeof(*kctx), 32);
 307         wrlen = roundup(sizeof(*kwr), 16);
 308         len = klen + wrlen;
 309 
 310         /* Flush out-standing data before new key takes effect */
 311         if (optname == TLS_TX) {
 312                 lock_sock(sk);
 313                 if (skb_queue_len(&csk->txq))
 314                         chtls_push_frames(csk, 0);
 315                 release_sock(sk);
 316         }
 317 
 318         skb = alloc_skb(len, GFP_KERNEL);
 319         if (!skb)
 320                 return -ENOMEM;
 321 
 322         keyid = get_new_keyid(csk, optname);
 323         if (keyid < 0) {
 324                 ret = -ENOSPC;
 325                 goto out_nokey;
 326         }
 327 
 328         kaddr = keyid_to_addr(cdev->kmap.start, keyid);
 329         kwr = (struct tls_key_req *)__skb_put_zero(skb, len);
 330         kwr->wr.op_to_compl =
 331                 cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR) | FW_WR_COMPL_F |
 332                       FW_WR_ATOMIC_V(1U));
 333         kwr->wr.flowid_len16 =
 334                 cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(len, 16) |
 335                             FW_WR_FLOWID_V(csk->tid)));
 336         kwr->wr.protocol = 0;
 337         kwr->wr.mfs = htons(TLS_MFS);
 338         kwr->wr.reneg_to_write_rx = optname;
 339 
 340         /* ulptx command */
 341         kwr->req.cmd = cpu_to_be32(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
 342                             T5_ULP_MEMIO_ORDER_V(1) |
 343                             T5_ULP_MEMIO_IMM_V(1));
 344         kwr->req.len16 = cpu_to_be32((csk->tid << 8) |
 345                               DIV_ROUND_UP(len - sizeof(kwr->wr), 16));
 346         kwr->req.dlen = cpu_to_be32(ULP_MEMIO_DATA_LEN_V(klen >> 5));
 347         kwr->req.lock_addr = cpu_to_be32(ULP_MEMIO_ADDR_V(kaddr));
 348 
 349         /* sub command */
 350         kwr->sc_imm.cmd_more = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_IMM));
 351         kwr->sc_imm.len = cpu_to_be32(klen);
 352 
 353         lock_sock(sk);
 354         /* key info */
 355         kctx = (struct _key_ctx *)(kwr + 1);
 356         ret = chtls_key_info(csk, kctx, keylen, optname);
 357         if (ret)
 358                 goto out_notcb;
 359 
 360         set_wr_txq(skb, CPL_PRIORITY_DATA, csk->tlshws.txqid);
 361         csk->wr_credits -= DIV_ROUND_UP(len, 16);
 362         csk->wr_unacked += DIV_ROUND_UP(len, 16);
 363         enqueue_wr(csk, skb);
 364         cxgb4_ofld_send(csk->egress_dev, skb);
 365 
 366         chtls_set_scmd(csk);
 367         /* Clear quiesce for Rx key */
 368         if (optname == TLS_RX) {
 369                 ret = chtls_set_tcb_keyid(sk, keyid);
 370                 if (ret)
 371                         goto out_notcb;
 372                 ret = chtls_set_tcb_field(sk, 0,
 373                                           TCB_ULP_RAW_V(TCB_ULP_RAW_M),
 374                                           TCB_ULP_RAW_V((TF_TLS_KEY_SIZE_V(1) |
 375                                           TF_TLS_CONTROL_V(1) |
 376                                           TF_TLS_ACTIVE_V(1) |
 377                                           TF_TLS_ENABLE_V(1))));
 378                 if (ret)
 379                         goto out_notcb;
 380                 ret = chtls_set_tcb_seqno(sk);
 381                 if (ret)
 382                         goto out_notcb;
 383                 ret = chtls_set_tcb_quiesce(sk, 0);
 384                 if (ret)
 385                         goto out_notcb;
 386                 csk->tlshws.rxkey = keyid;
 387         } else {
 388                 csk->tlshws.tx_seq_no = 0;
 389                 csk->tlshws.txkey = keyid;
 390         }
 391 
 392         release_sock(sk);
 393         return ret;
 394 out_notcb:
 395         release_sock(sk);
 396         free_tls_keyid(sk);
 397 out_nokey:
 398         kfree_skb(skb);
 399         return ret;
 400 }

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