1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2018 Chelsio Communications, Inc.
4  */
5 
6 #ifndef __CHTLS_CM_H__
7 #define __CHTLS_CM_H__
8 
9 /*
10  * TCB settings
11  */
12 /* 3:0 */
13 #define TCB_ULP_TYPE_W    0
14 #define TCB_ULP_TYPE_S    0
15 #define TCB_ULP_TYPE_M    0xfULL
16 #define TCB_ULP_TYPE_V(x) ((x) << TCB_ULP_TYPE_S)
17 
18 /* 11:4 */
19 #define TCB_ULP_RAW_W    0
20 #define TCB_ULP_RAW_S    4
21 #define TCB_ULP_RAW_M    0xffULL
22 #define TCB_ULP_RAW_V(x) ((x) << TCB_ULP_RAW_S)
23 
24 #define TF_TLS_KEY_SIZE_S    7
25 #define TF_TLS_KEY_SIZE_V(x) ((x) << TF_TLS_KEY_SIZE_S)
26 
27 #define TF_TLS_CONTROL_S     2
28 #define TF_TLS_CONTROL_V(x) ((x) << TF_TLS_CONTROL_S)
29 
30 #define TF_TLS_ACTIVE_S      1
31 #define TF_TLS_ACTIVE_V(x) ((x) << TF_TLS_ACTIVE_S)
32 
33 #define TF_TLS_ENABLE_S      0
34 #define TF_TLS_ENABLE_V(x) ((x) << TF_TLS_ENABLE_S)
35 
36 #define TF_RX_QUIESCE_S    15
37 #define TF_RX_QUIESCE_V(x) ((x) << TF_RX_QUIESCE_S)
38 
39 /*
40  * Max receive window supported by HW in bytes.  Only a small part of it can
41  * be set through option0, the rest needs to be set through RX_DATA_ACK.
42  */
43 #define MAX_RCV_WND ((1U << 27) - 1)
44 #define MAX_MSS     65536
45 
46 /*
47  * Min receive window.  We want it to be large enough to accommodate receive
48  * coalescing, handle jumbo frames, and not trigger sender SWS avoidance.
49  */
50 #define MIN_RCV_WND (24 * 1024U)
51 #define LOOPBACK(x)     (((x) & htonl(0xff000000)) == htonl(0x7f000000))
52 
53 /* ulp_mem_io + ulptx_idata + payload + padding */
54 #define MAX_IMM_ULPTX_WR_LEN (32 + 8 + 256 + 8)
55 
56 /* for TX: a skb must have a headroom of at least TX_HEADER_LEN bytes */
57 #define TX_HEADER_LEN \
58 	(sizeof(struct fw_ofld_tx_data_wr) + sizeof(struct sge_opaque_hdr))
59 #define TX_TLSHDR_LEN \
60 	(sizeof(struct fw_tlstx_data_wr) + sizeof(struct cpl_tx_tls_sfo) + \
61 	 sizeof(struct sge_opaque_hdr))
62 #define TXDATA_SKB_LEN 128
63 
64 enum {
65 	CPL_TX_TLS_SFO_TYPE_CCS,
66 	CPL_TX_TLS_SFO_TYPE_ALERT,
67 	CPL_TX_TLS_SFO_TYPE_HANDSHAKE,
68 	CPL_TX_TLS_SFO_TYPE_DATA,
69 	CPL_TX_TLS_SFO_TYPE_HEARTBEAT,
70 };
71 
72 enum {
73 	TLS_HDR_TYPE_CCS = 20,
74 	TLS_HDR_TYPE_ALERT,
75 	TLS_HDR_TYPE_HANDSHAKE,
76 	TLS_HDR_TYPE_RECORD,
77 	TLS_HDR_TYPE_HEARTBEAT,
78 };
79 
80 typedef void (*defer_handler_t)(struct chtls_dev *dev, struct sk_buff *skb);
81 extern struct request_sock_ops chtls_rsk_ops;
82 extern struct request_sock_ops chtls_rsk_opsv6;
83 
84 struct deferred_skb_cb {
85 	defer_handler_t handler;
86 	struct chtls_dev *dev;
87 };
88 
89 #define DEFERRED_SKB_CB(skb) ((struct deferred_skb_cb *)(skb)->cb)
90 #define failover_flowc_wr_len offsetof(struct fw_flowc_wr, mnemval[3])
91 #define WR_SKB_CB(skb) ((struct wr_skb_cb *)(skb)->cb)
92 #define ACCEPT_QUEUE(sk) (&inet_csk(sk)->icsk_accept_queue.rskq_accept_head)
93 
94 #define SND_WSCALE(tp) ((tp)->rx_opt.snd_wscale)
95 #define RCV_WSCALE(tp) ((tp)->rx_opt.rcv_wscale)
96 #define USER_MSS(tp) ((tp)->rx_opt.user_mss)
97 #define TS_RECENT_STAMP(tp) ((tp)->rx_opt.ts_recent_stamp)
98 #define WSCALE_OK(tp) ((tp)->rx_opt.wscale_ok)
99 #define TSTAMP_OK(tp) ((tp)->rx_opt.tstamp_ok)
100 #define SACK_OK(tp) ((tp)->rx_opt.sack_ok)
101 #define INC_ORPHAN_COUNT(sk) percpu_counter_inc((sk)->sk_prot->orphan_count)
102 
103 /* TLS SKB */
104 #define skb_ulp_tls_inline(skb)      (ULP_SKB_CB(skb)->ulp.tls.ofld)
105 #define skb_ulp_tls_iv_imm(skb)      (ULP_SKB_CB(skb)->ulp.tls.iv)
106 
107 void chtls_defer_reply(struct sk_buff *skb, struct chtls_dev *dev,
108 		       defer_handler_t handler);
109 
110 /*
111  * Returns true if the socket is in one of the supplied states.
112  */
113 static inline unsigned int sk_in_state(const struct sock *sk,
114 				       unsigned int states)
115 {
116 	return states & (1 << sk->sk_state);
117 }
118 
119 static void chtls_rsk_destructor(struct request_sock *req)
120 {
121 	/* do nothing */
122 }
123 
124 static inline void chtls_init_rsk_ops(struct proto *chtls_tcp_prot,
125 				      struct request_sock_ops *chtls_tcp_ops,
126 				      struct proto *tcp_prot, int family)
127 {
128 	memset(chtls_tcp_ops, 0, sizeof(*chtls_tcp_ops));
129 	chtls_tcp_ops->family = family;
130 	chtls_tcp_ops->obj_size = sizeof(struct tcp_request_sock);
131 	chtls_tcp_ops->destructor = chtls_rsk_destructor;
132 	chtls_tcp_ops->slab = tcp_prot->rsk_prot->slab;
133 	chtls_tcp_prot->rsk_prot = chtls_tcp_ops;
134 }
135 
136 static inline void chtls_reqsk_free(struct request_sock *req)
137 {
138 	if (req->rsk_listener)
139 		sock_put(req->rsk_listener);
140 	kmem_cache_free(req->rsk_ops->slab, req);
141 }
142 
143 #define DECLARE_TASK_FUNC(task, task_param) \
144 		static void task(struct work_struct *task_param)
145 
146 static inline void sk_wakeup_sleepers(struct sock *sk, bool interruptable)
147 {
148 	struct socket_wq *wq;
149 
150 	rcu_read_lock();
151 	wq = rcu_dereference(sk->sk_wq);
152 	if (skwq_has_sleeper(wq)) {
153 		if (interruptable)
154 			wake_up_interruptible(sk_sleep(sk));
155 		else
156 			wake_up_all(sk_sleep(sk));
157 	}
158 	rcu_read_unlock();
159 }
160 
161 static inline void chtls_set_req_port(struct request_sock *oreq,
162 				      __be16 source, __be16 dest)
163 {
164 	inet_rsk(oreq)->ir_rmt_port = source;
165 	inet_rsk(oreq)->ir_num = ntohs(dest);
166 }
167 
168 static inline void chtls_set_req_addr(struct request_sock *oreq,
169 				      __be32 local_ip, __be32 peer_ip)
170 {
171 	inet_rsk(oreq)->ir_loc_addr = local_ip;
172 	inet_rsk(oreq)->ir_rmt_addr = peer_ip;
173 }
174 
175 static inline void chtls_free_skb(struct sock *sk, struct sk_buff *skb)
176 {
177 	skb_dst_set(skb, NULL);
178 	__skb_unlink(skb, &sk->sk_receive_queue);
179 	__kfree_skb(skb);
180 }
181 
182 static inline void chtls_kfree_skb(struct sock *sk, struct sk_buff *skb)
183 {
184 	skb_dst_set(skb, NULL);
185 	__skb_unlink(skb, &sk->sk_receive_queue);
186 	kfree_skb(skb);
187 }
188 
189 static inline void chtls_reset_wr_list(struct chtls_sock *csk)
190 {
191 	csk->wr_skb_head = NULL;
192 	csk->wr_skb_tail = NULL;
193 }
194 
195 static inline void enqueue_wr(struct chtls_sock *csk, struct sk_buff *skb)
196 {
197 	WR_SKB_CB(skb)->next_wr = NULL;
198 
199 	skb_get(skb);
200 
201 	if (!csk->wr_skb_head)
202 		csk->wr_skb_head = skb;
203 	else
204 		WR_SKB_CB(csk->wr_skb_tail)->next_wr = skb;
205 	csk->wr_skb_tail = skb;
206 }
207 
208 static inline struct sk_buff *dequeue_wr(struct sock *sk)
209 {
210 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
211 	struct sk_buff *skb = NULL;
212 
213 	skb = csk->wr_skb_head;
214 
215 	if (likely(skb)) {
216 	 /* Don't bother clearing the tail */
217 		csk->wr_skb_head = WR_SKB_CB(skb)->next_wr;
218 		WR_SKB_CB(skb)->next_wr = NULL;
219 	}
220 	return skb;
221 }
222 #endif
223