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 ietf_mpa_v1 {
163 	u8 key[IETF_MPA_KEY_SIZE];
164 	u8 flags;
165 	u8 rev;
166 	__be16 priv_data_len;
167 	u8 priv_data[];
168 };
169 
170 struct ietf_rtr_msg {
171 	__be16 ctrl_ird;
172 	__be16 ctrl_ord;
173 };
174 
175 struct ietf_mpa_v2 {
176 	u8 key[IETF_MPA_KEY_SIZE];
177 	u8 flags;
178 	u8 rev;
179 	__be16 priv_data_len;
180 	struct ietf_rtr_msg rtr_msg;
181 	u8 priv_data[];
182 };
183 
184 struct option_base {
185 	u8 optionnum;
186 	u8 len;
187 };
188 
189 struct option_mss {
190 	u8 optionnum;
191 	u8 len;
192 	__be16 mss;
193 };
194 
195 struct option_windowscale {
196 	u8 optionnum;
197 	u8 len;
198 	u8 shiftcount;
199 };
200 
201 union all_known_options {
202 	char eol;
203 	struct option_base base;
204 	struct option_mss mss;
205 	struct option_windowscale windowscale;
206 };
207 
208 struct irdma_timer_entry {
209 	struct list_head list;
210 	unsigned long timetosend; /* jiffies */
211 	struct irdma_puda_buf *sqbuf;
212 	u32 type;
213 	u32 retrycount;
214 	u32 retranscount;
215 	u32 context;
216 	u32 send_retrans;
217 	int close_when_complete;
218 };
219 
220 /* CM context params */
221 struct irdma_cm_tcp_context {
222 	u8 client;
223 	u32 loc_seq_num;
224 	u32 loc_ack_num;
225 	u32 rem_ack_num;
226 	u32 rcv_nxt;
227 	u32 loc_id;
228 	u32 rem_id;
229 	u32 snd_wnd;
230 	u32 max_snd_wnd;
231 	u32 rcv_wnd;
232 	u32 mss;
233 	u8 snd_wscale;
234 	u8 rcv_wscale;
235 };
236 
237 struct irdma_apbvt_entry {
238 	struct hlist_node hlist;
239 	u32 use_cnt;
240 	u16 port;
241 };
242 
243 struct irdma_cm_listener {
244 	struct list_head list;
245 	struct iw_cm_id *cm_id;
246 	struct irdma_cm_core *cm_core;
247 	struct irdma_device *iwdev;
248 	struct list_head child_listen_list;
249 	struct irdma_apbvt_entry *apbvt_entry;
250 	enum irdma_cm_listener_state listener_state;
251 	refcount_t refcnt;
252 	atomic_t pend_accepts_cnt;
253 	u32 loc_addr[4];
254 	u32 reused_node;
255 	int backlog;
256 	u16 loc_port;
257 	u16 vlan_id;
258 	u8 loc_mac[ETH_ALEN];
259 	u8 user_pri;
260 	u8 tos;
261 	bool qhash_set:1;
262 	bool ipv4:1;
263 };
264 
265 struct irdma_kmem_info {
266 	void *addr;
267 	u32 size;
268 };
269 
270 struct irdma_mpa_priv_info {
271 	const void *addr;
272 	u32 size;
273 };
274 
275 struct irdma_cm_node {
276 	struct irdma_qp *iwqp;
277 	struct irdma_device *iwdev;
278 	struct irdma_sc_dev *dev;
279 	struct irdma_cm_tcp_context tcp_cntxt;
280 	struct irdma_cm_core *cm_core;
281 	struct irdma_timer_entry *send_entry;
282 	struct irdma_timer_entry *close_entry;
283 	struct irdma_cm_listener *listener;
284 	struct list_head timer_entry;
285 	struct list_head reset_entry;
286 	struct list_head teardown_entry;
287 	struct irdma_apbvt_entry *apbvt_entry;
288 	struct rcu_head rcu_head;
289 	struct irdma_mpa_priv_info pdata;
290 	struct irdma_sc_ah *ah;
291 	union {
292 		struct ietf_mpa_v1 mpa_frame;
293 		struct ietf_mpa_v2 mpa_v2_frame;
294 	};
295 	struct irdma_kmem_info mpa_hdr;
296 	struct iw_cm_id *cm_id;
297 	struct hlist_node list;
298 	struct completion establish_comp;
299 	spinlock_t retrans_list_lock; /* protect CM node rexmit updates*/
300 	atomic_t passive_state;
301 	refcount_t refcnt;
302 	enum irdma_cm_node_state state;
303 	enum send_rdma0 send_rdma0_op;
304 	enum mpa_frame_ver mpa_frame_rev;
305 	u32 loc_addr[4], rem_addr[4];
306 	u16 loc_port, rem_port;
307 	int apbvt_set;
308 	int accept_pend;
309 	u16 vlan_id;
310 	u16 ird_size;
311 	u16 ord_size;
312 	u16 mpav2_ird_ord;
313 	u16 lsmm_size;
314 	u8 pdata_buf[IETF_MAX_PRIV_DATA_LEN];
315 	u8 loc_mac[ETH_ALEN];
316 	u8 rem_mac[ETH_ALEN];
317 	u8 user_pri;
318 	u8 tos;
319 	bool ack_rcvd:1;
320 	bool qhash_set:1;
321 	bool ipv4:1;
322 	bool snd_mark_en:1;
323 	bool rcv_mark_en:1;
324 	bool do_lpb:1;
325 	bool accelerated:1;
326 };
327 
328 /* Used by internal CM APIs to pass CM information*/
329 struct irdma_cm_info {
330 	struct iw_cm_id *cm_id;
331 	u16 loc_port;
332 	u16 rem_port;
333 	u32 loc_addr[4];
334 	u32 rem_addr[4];
335 	u32 qh_qpid;
336 	u16 vlan_id;
337 	int backlog;
338 	u8 user_pri;
339 	u8 tos;
340 	bool ipv4;
341 };
342 
343 struct irdma_cm_event {
344 	enum irdma_cm_event_type type;
345 	struct irdma_cm_info cm_info;
346 	struct work_struct event_work;
347 	struct irdma_cm_node *cm_node;
348 };
349 
350 struct irdma_cm_core {
351 	struct irdma_device *iwdev;
352 	struct irdma_sc_dev *dev;
353 	struct list_head listen_list;
354 	DECLARE_HASHTABLE(cm_hash_tbl, 8);
355 	DECLARE_HASHTABLE(apbvt_hash_tbl, 8);
356 	struct timer_list tcp_timer;
357 	struct workqueue_struct *event_wq;
358 	spinlock_t ht_lock; /* protect CM node (active side) list */
359 	spinlock_t listen_list_lock; /* protect listener list */
360 	spinlock_t apbvt_lock; /*serialize apbvt add/del entries*/
361 	u64 stats_nodes_created;
362 	u64 stats_nodes_destroyed;
363 	u64 stats_listen_created;
364 	u64 stats_listen_destroyed;
365 	u64 stats_listen_nodes_created;
366 	u64 stats_listen_nodes_destroyed;
367 	u64 stats_lpbs;
368 	u64 stats_accepts;
369 	u64 stats_rejects;
370 	u64 stats_connect_errs;
371 	u64 stats_passive_errs;
372 	u64 stats_pkt_retrans;
373 	u64 stats_backlog_drops;
374 	struct irdma_puda_buf *(*form_cm_frame)(struct irdma_cm_node *cm_node,
375 						struct irdma_kmem_info *options,
376 						struct irdma_kmem_info *hdr,
377 						struct irdma_mpa_priv_info *pdata,
378 						u8 flags);
379 	int (*cm_create_ah)(struct irdma_cm_node *cm_node, bool wait);
380 	void (*cm_free_ah)(struct irdma_cm_node *cm_node);
381 };
382 
383 int irdma_schedule_cm_timer(struct irdma_cm_node *cm_node,
384 			    struct irdma_puda_buf *sqbuf,
385 			    enum irdma_timer_type type, int send_retrans,
386 			    int close_when_complete);
387 
irdma_tos2dscp(u8 tos)388 static inline u8 irdma_tos2dscp(u8 tos)
389 {
390 #define IRDMA_DSCP_VAL GENMASK(7, 2)
391 	return (u8)FIELD_GET(IRDMA_DSCP_VAL, tos);
392 }
393 
394 int irdma_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param);
395 int irdma_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len);
396 int irdma_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param);
397 int irdma_create_listen(struct iw_cm_id *cm_id, int backlog);
398 int irdma_destroy_listen(struct iw_cm_id *cm_id);
399 int irdma_add_arp(struct irdma_pci_f *rf, u32 *ip, bool ipv4, const u8 *mac);
400 void irdma_cm_teardown_connections(struct irdma_device *iwdev, u32 *ipaddr,
401 				   struct irdma_cm_info *nfo,
402 				   bool disconnect_all);
403 int irdma_cm_start(struct irdma_device *dev);
404 int irdma_cm_stop(struct irdma_device *dev);
405 bool irdma_ipv4_is_lpb(u32 loc_addr, u32 rem_addr);
406 bool irdma_ipv6_is_lpb(u32 *loc_addr, u32 *rem_addr);
407 int irdma_arp_table(struct irdma_pci_f *rf, u32 *ip_addr, bool ipv4,
408 		    const u8 *mac_addr, u32 action);
409 void irdma_if_notify(struct irdma_device *iwdev, struct net_device *netdev,
410 		     u32 *ipaddr, bool ipv4, bool ifup);
411 bool irdma_port_in_use(struct irdma_cm_core *cm_core, u16 port);
412 void irdma_send_ack(struct irdma_cm_node *cm_node);
413 void irdma_lpb_nop(struct irdma_sc_qp *qp);
414 void irdma_rem_ref_cm_node(struct irdma_cm_node *cm_node);
415 void irdma_add_conn_est_qh(struct irdma_cm_node *cm_node);
416 #endif /* IRDMA_CM_H */
417