1 /* SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB */
2 /* Copyright (c) 2015 - 2021 Intel Corporation */
3 #ifndef IRDMA_CM_H
4 #define IRDMA_CM_H
5 
6 #define IRDMA_MPA_REQUEST_ACCEPT	1
7 #define IRDMA_MPA_REQUEST_REJECT	2
8 
9 /* IETF MPA -- defines */
10 #define IEFT_MPA_KEY_REQ	"MPA ID Req Frame"
11 #define IEFT_MPA_KEY_REP	"MPA ID Rep Frame"
12 #define IETF_MPA_KEY_SIZE	16
13 #define IETF_MPA_VER		1
14 #define IETF_MAX_PRIV_DATA_LEN	512
15 #define IETF_MPA_FRAME_SIZE	20
16 #define IETF_RTR_MSG_SIZE	4
17 #define IETF_MPA_V2_FLAG	0x10
18 #define SNDMARKER_SEQNMASK	0x000001ff
19 #define IRDMA_MAX_IETF_SIZE	32
20 
21 /* IETF RTR MSG Fields */
22 #define IETF_PEER_TO_PEER	0x8000
23 #define IETF_FLPDU_ZERO_LEN	0x4000
24 #define IETF_RDMA0_WRITE	0x8000
25 #define IETF_RDMA0_READ		0x4000
26 #define IETF_NO_IRD_ORD		0x3fff
27 
28 #define MAX_PORTS	65536
29 
30 #define IRDMA_PASSIVE_STATE_INDICATED	0
31 #define IRDMA_DO_NOT_SEND_RESET_EVENT	1
32 #define IRDMA_SEND_RESET_EVENT		2
33 
34 #define MAX_IRDMA_IFS	4
35 
36 #define SET_ACK		1
37 #define SET_SYN		2
38 #define SET_FIN		4
39 #define SET_RST		8
40 
41 #define TCP_OPTIONS_PADDING	3
42 
43 #define IRDMA_DEFAULT_RETRYS	64
44 #define IRDMA_DEFAULT_RETRANS	8
45 #define IRDMA_DEFAULT_TTL		0x40
46 #define IRDMA_DEFAULT_RTT_VAR		6
47 #define IRDMA_DEFAULT_SS_THRESH		0x3fffffff
48 #define IRDMA_DEFAULT_REXMIT_THRESH	8
49 
50 #define IRDMA_RETRY_TIMEOUT	HZ
51 #define IRDMA_SHORT_TIME	10
52 #define IRDMA_LONG_TIME		(2 * HZ)
53 #define IRDMA_MAX_TIMEOUT	((unsigned long)(12 * HZ))
54 
55 #define IRDMA_CM_HASHTABLE_SIZE		1024
56 #define IRDMA_CM_TCP_TIMER_INTERVAL	3000
57 #define IRDMA_CM_DEFAULT_MTU		1540
58 #define IRDMA_CM_DEFAULT_FRAME_CNT	10
59 #define IRDMA_CM_THREAD_STACK_SIZE	256
60 #define IRDMA_CM_DEFAULT_RCV_WND	64240
61 #define IRDMA_CM_DEFAULT_RCV_WND_SCALED	0x3FFFC
62 #define IRDMA_CM_DEFAULT_RCV_WND_SCALE	2
63 #define IRDMA_CM_DEFAULT_FREE_PKTS	10
64 #define IRDMA_CM_FREE_PKT_LO_WATERMARK	2
65 #define IRDMA_CM_DEFAULT_MSS		536
66 #define IRDMA_CM_DEFAULT_MPA_VER	2
67 #define IRDMA_CM_DEFAULT_SEQ		0x159bf75f
68 #define IRDMA_CM_DEFAULT_LOCAL_ID	0x3b47
69 #define IRDMA_CM_DEFAULT_SEQ2		0x18ed5740
70 #define IRDMA_CM_DEFAULT_LOCAL_ID2	0xb807
71 #define IRDMA_MAX_CM_BUF		(IRDMA_MAX_IETF_SIZE + IETF_MAX_PRIV_DATA_LEN)
72 
73 enum ietf_mpa_flags {
74 	IETF_MPA_FLAGS_REJECT  = 0x20,
75 	IETF_MPA_FLAGS_CRC     = 0x40,
76 	IETF_MPA_FLAGS_MARKERS = 0x80,
77 };
78 
79 enum irdma_timer_type {
80 	IRDMA_TIMER_TYPE_SEND,
81 	IRDMA_TIMER_TYPE_CLOSE,
82 };
83 
84 enum option_nums {
85 	OPTION_NUM_EOL,
86 	OPTION_NUM_NONE,
87 	OPTION_NUM_MSS,
88 	OPTION_NUM_WINDOW_SCALE,
89 	OPTION_NUM_SACK_PERM,
90 	OPTION_NUM_SACK,
91 	OPTION_NUM_WRITE0 = 0xbc,
92 };
93 
94 /* cm node transition states */
95 enum irdma_cm_node_state {
96 	IRDMA_CM_STATE_UNKNOWN,
97 	IRDMA_CM_STATE_INITED,
98 	IRDMA_CM_STATE_LISTENING,
99 	IRDMA_CM_STATE_SYN_RCVD,
100 	IRDMA_CM_STATE_SYN_SENT,
101 	IRDMA_CM_STATE_ONE_SIDE_ESTABLISHED,
102 	IRDMA_CM_STATE_ESTABLISHED,
103 	IRDMA_CM_STATE_ACCEPTING,
104 	IRDMA_CM_STATE_MPAREQ_SENT,
105 	IRDMA_CM_STATE_MPAREQ_RCVD,
106 	IRDMA_CM_STATE_MPAREJ_RCVD,
107 	IRDMA_CM_STATE_OFFLOADED,
108 	IRDMA_CM_STATE_FIN_WAIT1,
109 	IRDMA_CM_STATE_FIN_WAIT2,
110 	IRDMA_CM_STATE_CLOSE_WAIT,
111 	IRDMA_CM_STATE_TIME_WAIT,
112 	IRDMA_CM_STATE_LAST_ACK,
113 	IRDMA_CM_STATE_CLOSING,
114 	IRDMA_CM_STATE_LISTENER_DESTROYED,
115 	IRDMA_CM_STATE_CLOSED,
116 };
117 
118 enum mpa_frame_ver {
119 	IETF_MPA_V1 = 1,
120 	IETF_MPA_V2 = 2,
121 };
122 
123 enum mpa_frame_key {
124 	MPA_KEY_REQUEST,
125 	MPA_KEY_REPLY,
126 };
127 
128 enum send_rdma0 {
129 	SEND_RDMA_READ_ZERO  = 1,
130 	SEND_RDMA_WRITE_ZERO = 2,
131 };
132 
133 enum irdma_tcpip_pkt_type {
134 	IRDMA_PKT_TYPE_UNKNOWN,
135 	IRDMA_PKT_TYPE_SYN,
136 	IRDMA_PKT_TYPE_SYNACK,
137 	IRDMA_PKT_TYPE_ACK,
138 	IRDMA_PKT_TYPE_FIN,
139 	IRDMA_PKT_TYPE_RST,
140 };
141 
142 enum irdma_cm_listener_state {
143 	IRDMA_CM_LISTENER_PASSIVE_STATE = 1,
144 	IRDMA_CM_LISTENER_ACTIVE_STATE  = 2,
145 	IRDMA_CM_LISTENER_EITHER_STATE  = 3,
146 };
147 
148 /* CM event codes */
149 enum irdma_cm_event_type {
150 	IRDMA_CM_EVENT_UNKNOWN,
151 	IRDMA_CM_EVENT_ESTABLISHED,
152 	IRDMA_CM_EVENT_MPA_REQ,
153 	IRDMA_CM_EVENT_MPA_CONNECT,
154 	IRDMA_CM_EVENT_MPA_ACCEPT,
155 	IRDMA_CM_EVENT_MPA_REJECT,
156 	IRDMA_CM_EVENT_MPA_ESTABLISHED,
157 	IRDMA_CM_EVENT_CONNECTED,
158 	IRDMA_CM_EVENT_RESET,
159 	IRDMA_CM_EVENT_ABORTED,
160 };
161 
162 struct irdma_bth { /* Base Trasnport Header */
163 	u8 opcode;
164 	u8 flags;
165 	__be16 pkey;
166 	__be32 qpn;
167 	__be32 apsn;
168 };
169 
170 struct ietf_mpa_v1 {
171 	u8 key[IETF_MPA_KEY_SIZE];
172 	u8 flags;
173 	u8 rev;
174 	__be16 priv_data_len;
175 	u8 priv_data[];
176 };
177 
178 struct ietf_rtr_msg {
179 	__be16 ctrl_ird;
180 	__be16 ctrl_ord;
181 };
182 
183 struct ietf_mpa_v2 {
184 	u8 key[IETF_MPA_KEY_SIZE];
185 	u8 flags;
186 	u8 rev;
187 	__be16 priv_data_len;
188 	struct ietf_rtr_msg rtr_msg;
189 	u8 priv_data[];
190 };
191 
192 struct option_base {
193 	u8 optionnum;
194 	u8 len;
195 };
196 
197 struct option_mss {
198 	u8 optionnum;
199 	u8 len;
200 	__be16 mss;
201 };
202 
203 struct option_windowscale {
204 	u8 optionnum;
205 	u8 len;
206 	u8 shiftcount;
207 };
208 
209 union all_known_options {
210 	char eol;
211 	struct option_base base;
212 	struct option_mss mss;
213 	struct option_windowscale windowscale;
214 };
215 
216 struct irdma_timer_entry {
217 	struct list_head list;
218 	unsigned long timetosend; /* jiffies */
219 	struct irdma_puda_buf *sqbuf;
220 	u32 type;
221 	u32 retrycount;
222 	u32 retranscount;
223 	u32 context;
224 	u32 send_retrans;
225 	int close_when_complete;
226 };
227 
228 /* CM context params */
229 struct irdma_cm_tcp_context {
230 	u8 client;
231 	u32 loc_seq_num;
232 	u32 loc_ack_num;
233 	u32 rem_ack_num;
234 	u32 rcv_nxt;
235 	u32 loc_id;
236 	u32 rem_id;
237 	u32 snd_wnd;
238 	u32 max_snd_wnd;
239 	u32 rcv_wnd;
240 	u32 mss;
241 	u8 snd_wscale;
242 	u8 rcv_wscale;
243 };
244 
245 struct irdma_apbvt_entry {
246 	struct hlist_node hlist;
247 	u32 use_cnt;
248 	u16 port;
249 };
250 
251 struct irdma_cm_listener {
252 	struct list_head list;
253 	struct iw_cm_id *cm_id;
254 	struct irdma_cm_core *cm_core;
255 	struct irdma_device *iwdev;
256 	struct list_head child_listen_list;
257 	struct irdma_apbvt_entry *apbvt_entry;
258 	enum irdma_cm_listener_state listener_state;
259 	refcount_t refcnt;
260 	atomic_t pend_accepts_cnt;
261 	u32 loc_addr[4];
262 	u32 reused_node;
263 	int backlog;
264 	u16 loc_port;
265 	u16 vlan_id;
266 	u8 loc_mac[ETH_ALEN];
267 	u8 user_pri;
268 	u8 tos;
269 	bool qhash_set:1;
270 	bool ipv4:1;
271 };
272 
273 struct irdma_kmem_info {
274 	void *addr;
275 	u32 size;
276 };
277 
278 struct irdma_mpa_priv_info {
279 	const void *addr;
280 	u32 size;
281 };
282 
283 struct irdma_cm_node {
284 	struct irdma_qp *iwqp;
285 	struct irdma_device *iwdev;
286 	struct irdma_sc_dev *dev;
287 	struct irdma_cm_tcp_context tcp_cntxt;
288 	struct irdma_cm_core *cm_core;
289 	struct irdma_timer_entry *send_entry;
290 	struct irdma_timer_entry *close_entry;
291 	struct irdma_cm_listener *listener;
292 	struct list_head timer_entry;
293 	struct list_head reset_entry;
294 	struct list_head teardown_entry;
295 	struct irdma_apbvt_entry *apbvt_entry;
296 	struct rcu_head rcu_head;
297 	struct irdma_mpa_priv_info pdata;
298 	struct irdma_sc_ah *ah;
299 	union {
300 		struct ietf_mpa_v1 mpa_frame;
301 		struct ietf_mpa_v2 mpa_v2_frame;
302 	};
303 	struct irdma_kmem_info mpa_hdr;
304 	struct iw_cm_id *cm_id;
305 	struct hlist_node list;
306 	struct completion establish_comp;
307 	spinlock_t retrans_list_lock; /* protect CM node rexmit updates*/
308 	atomic_t passive_state;
309 	refcount_t refcnt;
310 	enum irdma_cm_node_state state;
311 	enum send_rdma0 send_rdma0_op;
312 	enum mpa_frame_ver mpa_frame_rev;
313 	u32 loc_addr[4], rem_addr[4];
314 	u16 loc_port, rem_port;
315 	int apbvt_set;
316 	int accept_pend;
317 	u16 vlan_id;
318 	u16 ird_size;
319 	u16 ord_size;
320 	u16 mpav2_ird_ord;
321 	u16 lsmm_size;
322 	u8 pdata_buf[IETF_MAX_PRIV_DATA_LEN];
323 	u8 loc_mac[ETH_ALEN];
324 	u8 rem_mac[ETH_ALEN];
325 	u8 user_pri;
326 	u8 tos;
327 	bool ack_rcvd:1;
328 	bool qhash_set:1;
329 	bool ipv4:1;
330 	bool snd_mark_en:1;
331 	bool rcv_mark_en:1;
332 	bool do_lpb:1;
333 	bool accelerated:1;
334 };
335 
336 /* Used by internal CM APIs to pass CM information*/
337 struct irdma_cm_info {
338 	struct iw_cm_id *cm_id;
339 	u16 loc_port;
340 	u16 rem_port;
341 	u32 loc_addr[4];
342 	u32 rem_addr[4];
343 	u32 qh_qpid;
344 	u16 vlan_id;
345 	int backlog;
346 	u8 user_pri;
347 	u8 tos;
348 	bool ipv4;
349 };
350 
351 struct irdma_cm_event {
352 	enum irdma_cm_event_type type;
353 	struct irdma_cm_info cm_info;
354 	struct work_struct event_work;
355 	struct irdma_cm_node *cm_node;
356 };
357 
358 struct irdma_cm_core {
359 	struct irdma_device *iwdev;
360 	struct irdma_sc_dev *dev;
361 	struct list_head listen_list;
362 	DECLARE_HASHTABLE(cm_hash_tbl, 8);
363 	DECLARE_HASHTABLE(apbvt_hash_tbl, 8);
364 	struct timer_list tcp_timer;
365 	struct workqueue_struct *event_wq;
366 	spinlock_t ht_lock; /* protect CM node (active side) list */
367 	spinlock_t listen_list_lock; /* protect listener list */
368 	spinlock_t apbvt_lock; /*serialize apbvt add/del entries*/
369 	u64 stats_nodes_created;
370 	u64 stats_nodes_destroyed;
371 	u64 stats_listen_created;
372 	u64 stats_listen_destroyed;
373 	u64 stats_listen_nodes_created;
374 	u64 stats_listen_nodes_destroyed;
375 	u64 stats_lpbs;
376 	u64 stats_accepts;
377 	u64 stats_rejects;
378 	u64 stats_connect_errs;
379 	u64 stats_passive_errs;
380 	u64 stats_pkt_retrans;
381 	u64 stats_backlog_drops;
382 	struct irdma_puda_buf *(*form_cm_frame)(struct irdma_cm_node *cm_node,
383 						struct irdma_kmem_info *options,
384 						struct irdma_kmem_info *hdr,
385 						struct irdma_mpa_priv_info *pdata,
386 						u8 flags);
387 	int (*cm_create_ah)(struct irdma_cm_node *cm_node, bool wait);
388 	void (*cm_free_ah)(struct irdma_cm_node *cm_node);
389 };
390 
391 int irdma_schedule_cm_timer(struct irdma_cm_node *cm_node,
392 			    struct irdma_puda_buf *sqbuf,
393 			    enum irdma_timer_type type, int send_retrans,
394 			    int close_when_complete);
395 int irdma_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param);
396 int irdma_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len);
397 int irdma_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param);
398 int irdma_create_listen(struct iw_cm_id *cm_id, int backlog);
399 int irdma_destroy_listen(struct iw_cm_id *cm_id);
400 int irdma_add_arp(struct irdma_pci_f *rf, u32 *ip, bool ipv4, u8 *mac);
401 void irdma_cm_teardown_connections(struct irdma_device *iwdev, u32 *ipaddr,
402 				   struct irdma_cm_info *nfo,
403 				   bool disconnect_all);
404 int irdma_cm_start(struct irdma_device *dev);
405 int irdma_cm_stop(struct irdma_device *dev);
406 bool irdma_ipv4_is_lpb(u32 loc_addr, u32 rem_addr);
407 bool irdma_ipv6_is_lpb(u32 *loc_addr, u32 *rem_addr);
408 int irdma_arp_table(struct irdma_pci_f *rf, u32 *ip_addr, bool ipv4,
409 		    u8 *mac_addr, u32 action);
410 void irdma_if_notify(struct irdma_device *iwdev, struct net_device *netdev,
411 		     u32 *ipaddr, bool ipv4, bool ifup);
412 bool irdma_port_in_use(struct irdma_cm_core *cm_core, u16 port);
413 void irdma_send_ack(struct irdma_cm_node *cm_node);
414 void irdma_lpb_nop(struct irdma_sc_qp *qp);
415 void irdma_rem_ref_cm_node(struct irdma_cm_node *cm_node);
416 void irdma_add_conn_est_qh(struct irdma_cm_node *cm_node);
417 #endif /* IRDMA_CM_H */
418