xref: /linux/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_common.h (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2020 Chelsio Communications.  All rights reserved. */
3 
4 #ifndef __CHCR_COMMON_H__
5 #define __CHCR_COMMON_H__
6 
7 #include "cxgb4.h"
8 
9 #define CHCR_MAX_SALT                      4
10 #define CHCR_KEYCTX_MAC_KEY_SIZE_128       0
11 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128    0
12 #define CHCR_SCMD_CIPHER_MODE_AES_GCM      2
13 #define CHCR_SCMD_CIPHER_MODE_AES_CTR      3
14 #define CHCR_CPL_TX_SEC_PDU_LEN_64BIT      2
15 #define CHCR_SCMD_SEQ_NO_CTRL_64BIT        3
16 #define CHCR_SCMD_PROTO_VERSION_TLS        0
17 #define CHCR_SCMD_PROTO_VERSION_GENERIC    4
18 #define CHCR_SCMD_AUTH_MODE_GHASH          4
19 #define AES_BLOCK_LEN                      16
20 
21 struct ktls_key_ctx {
22 	__be32 ctx_hdr;
23 	u8 salt[CHCR_MAX_SALT];
24 	__be64 iv_to_auth;
25 	unsigned char key[TLS_CIPHER_AES_GCM_128_KEY_SIZE +
26 			  TLS_CIPHER_AES_GCM_256_TAG_SIZE];
27 };
28 
29 /* Crypto key context */
30 #define KEY_CONTEXT_CTX_LEN_S           24
31 #define KEY_CONTEXT_CTX_LEN_V(x)        ((x) << KEY_CONTEXT_CTX_LEN_S)
32 
33 #define KEY_CONTEXT_SALT_PRESENT_S      10
34 #define KEY_CONTEXT_SALT_PRESENT_V(x)   ((x) << KEY_CONTEXT_SALT_PRESENT_S)
35 #define KEY_CONTEXT_SALT_PRESENT_F      KEY_CONTEXT_SALT_PRESENT_V(1U)
36 
37 #define KEY_CONTEXT_VALID_S     0
38 #define KEY_CONTEXT_VALID_V(x)  ((x) << KEY_CONTEXT_VALID_S)
39 #define KEY_CONTEXT_VALID_F     KEY_CONTEXT_VALID_V(1U)
40 
41 #define KEY_CONTEXT_CK_SIZE_S           6
42 #define KEY_CONTEXT_CK_SIZE_V(x)        ((x) << KEY_CONTEXT_CK_SIZE_S)
43 
44 #define KEY_CONTEXT_MK_SIZE_S           2
45 #define KEY_CONTEXT_MK_SIZE_V(x)        ((x) << KEY_CONTEXT_MK_SIZE_S)
46 
47 #define KEY_CONTEXT_OPAD_PRESENT_S      11
48 #define KEY_CONTEXT_OPAD_PRESENT_V(x)   ((x) << KEY_CONTEXT_OPAD_PRESENT_S)
49 #define KEY_CONTEXT_OPAD_PRESENT_F      KEY_CONTEXT_OPAD_PRESENT_V(1U)
50 
51 #define FILL_KEY_CTX_HDR(ck_size, mk_size, ctx_len) \
52 		htonl(KEY_CONTEXT_MK_SIZE_V(mk_size) | \
53 		      KEY_CONTEXT_CK_SIZE_V(ck_size) | \
54 		      KEY_CONTEXT_VALID_F | \
55 		      KEY_CONTEXT_SALT_PRESENT_F | \
56 		      KEY_CONTEXT_CTX_LEN_V((ctx_len)))
57 
chcr_copy_to_txd(const void * src,const struct sge_txq * q,void * pos,int length)58 static inline void *chcr_copy_to_txd(const void *src, const struct sge_txq *q,
59 				     void *pos, int length)
60 {
61 	int left = (void *)q->stat - pos;
62 	u64 *p;
63 
64 	if (likely(length <= left)) {
65 		memcpy(pos, src, length);
66 		pos += length;
67 	} else {
68 		memcpy(pos, src, left);
69 		memcpy(q->desc, src + left, length - left);
70 		pos = (void *)q->desc + (length - left);
71 	}
72 	/* 0-pad to multiple of 16 */
73 	p = PTR_ALIGN(pos, 8);
74 	if ((uintptr_t)p & 8) {
75 		*p = 0;
76 		return p + 1;
77 	}
78 	return p;
79 }
80 
chcr_txq_avail(const struct sge_txq * q)81 static inline unsigned int chcr_txq_avail(const struct sge_txq *q)
82 {
83 	return q->size - 1 - q->in_use;
84 }
85 
chcr_txq_advance(struct sge_txq * q,unsigned int n)86 static inline void chcr_txq_advance(struct sge_txq *q, unsigned int n)
87 {
88 	q->in_use += n;
89 	q->pidx += n;
90 	if (q->pidx >= q->size)
91 		q->pidx -= q->size;
92 }
93 
chcr_eth_txq_stop(struct sge_eth_txq * q)94 static inline void chcr_eth_txq_stop(struct sge_eth_txq *q)
95 {
96 	netif_tx_stop_queue(q->txq);
97 	q->q.stops++;
98 }
99 
chcr_sgl_len(unsigned int n)100 static inline unsigned int chcr_sgl_len(unsigned int n)
101 {
102 	n--;
103 	return (3 * n) / 2 + (n & 1) + 2;
104 }
105 
chcr_flits_to_desc(unsigned int n)106 static inline unsigned int chcr_flits_to_desc(unsigned int n)
107 {
108 	WARN_ON(n > SGE_MAX_WR_LEN / 8);
109 	return DIV_ROUND_UP(n, 8);
110 }
111 #endif /* __CHCR_COMMON_H__ */
112