1 /*
2  * Copyright (c) 2006 - 2011 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 
34 #ifndef NES_CM_H
35 #define NES_CM_H
36 
37 #define QUEUE_EVENTS
38 
39 #define NES_MANAGE_APBVT_DEL 0
40 #define NES_MANAGE_APBVT_ADD 1
41 
42 #define NES_MPA_REQUEST_ACCEPT  1
43 #define NES_MPA_REQUEST_REJECT  2
44 
45 /* IETF MPA -- defines, enums, structs */
46 #define IEFT_MPA_KEY_REQ  "MPA ID Req Frame"
47 #define IEFT_MPA_KEY_REP  "MPA ID Rep Frame"
48 #define IETF_MPA_KEY_SIZE 16
49 #define IETF_MPA_VERSION  1
50 #define IETF_MAX_PRIV_DATA_LEN 512
51 #define IETF_MPA_FRAME_SIZE    20
52 #define IETF_RTR_MSG_SIZE      4
53 #define IETF_MPA_V2_FLAG       0x10
54 
55 /* IETF RTR MSG Fields               */
56 #define IETF_PEER_TO_PEER       0x8000
57 #define IETF_FLPDU_ZERO_LEN     0x4000
58 #define IETF_RDMA0_WRITE        0x8000
59 #define IETF_RDMA0_READ         0x4000
60 #define IETF_NO_IRD_ORD         0x3FFF
61 
62 enum ietf_mpa_flags {
63 	IETF_MPA_FLAGS_MARKERS = 0x80,	/* receive Markers */
64 	IETF_MPA_FLAGS_CRC     = 0x40,	/* receive Markers */
65 	IETF_MPA_FLAGS_REJECT  = 0x20,	/* Reject */
66 };
67 
68 struct ietf_mpa_v1 {
69 	u8 key[IETF_MPA_KEY_SIZE];
70 	u8 flags;
71 	u8 rev;
72 	__be16 priv_data_len;
73 	u8 priv_data[0];
74 };
75 
76 #define ietf_mpa_req_resp_frame ietf_mpa_frame
77 
78 struct ietf_rtr_msg {
79 	__be16 ctrl_ird;
80 	__be16 ctrl_ord;
81 };
82 
83 struct ietf_mpa_v2 {
84 	u8 key[IETF_MPA_KEY_SIZE];
85 	u8 flags;
86 	u8 rev;
87 	 __be16 priv_data_len;
88 	struct ietf_rtr_msg rtr_msg;
89 	u8 priv_data[0];
90 };
91 
92 struct nes_v4_quad {
93 	u32 rsvd0;
94 	__le32 DstIpAdrIndex;	/* Only most significant 5 bits are valid */
95 	__be32 SrcIpadr;
96 	__be16 TcpPorts[2];		/* src is low, dest is high */
97 };
98 
99 struct nes_cm_node;
100 enum nes_timer_type {
101 	NES_TIMER_TYPE_SEND,
102 	NES_TIMER_TYPE_RECV,
103 	NES_TIMER_NODE_CLEANUP,
104 	NES_TIMER_TYPE_CLOSE,
105 };
106 
107 #define NES_PASSIVE_STATE_INDICATED	0
108 #define NES_DO_NOT_SEND_RESET_EVENT	1
109 #define NES_SEND_RESET_EVENT		2
110 
111 #define MAX_NES_IFS 4
112 
113 #define SET_ACK 1
114 #define SET_SYN 2
115 #define SET_FIN 4
116 #define SET_RST 8
117 
118 #define TCP_OPTIONS_PADDING	3
119 
120 struct option_base {
121 	u8 optionnum;
122 	u8 length;
123 };
124 
125 enum option_numbers {
126 	OPTION_NUMBER_END,
127 	OPTION_NUMBER_NONE,
128 	OPTION_NUMBER_MSS,
129 	OPTION_NUMBER_WINDOW_SCALE,
130 	OPTION_NUMBER_SACK_PERM,
131 	OPTION_NUMBER_SACK,
132 	OPTION_NUMBER_WRITE0 = 0xbc
133 };
134 
135 struct option_mss {
136 	u8 optionnum;
137 	u8 length;
138 	__be16 mss;
139 };
140 
141 struct option_windowscale {
142 	u8 optionnum;
143 	u8 length;
144 	u8 shiftcount;
145 };
146 
147 union all_known_options {
148 	char as_end;
149 	struct option_base as_base;
150 	struct option_mss as_mss;
151 	struct option_windowscale as_windowscale;
152 };
153 
154 struct nes_timer_entry {
155 	struct list_head list;
156 	unsigned long timetosend;	/* jiffies */
157 	struct sk_buff *skb;
158 	u32 type;
159 	u32 retrycount;
160 	u32 retranscount;
161 	u32 context;
162 	u32 seq_num;
163 	u32 send_retrans;
164 	int close_when_complete;
165 	struct net_device *netdev;
166 };
167 
168 #define NES_DEFAULT_RETRYS  64
169 #define NES_DEFAULT_RETRANS 8
170 #ifdef CONFIG_INFINIBAND_NES_DEBUG
171 #define NES_RETRY_TIMEOUT   (1000*HZ/1000)
172 #else
173 #define NES_RETRY_TIMEOUT   (3000*HZ/1000)
174 #endif
175 #define NES_SHORT_TIME      (10)
176 #define NES_LONG_TIME       (2000*HZ/1000)
177 #define NES_MAX_TIMEOUT     ((unsigned long) (12*HZ))
178 
179 #define NES_CM_HASHTABLE_SIZE         1024
180 #define NES_CM_TCP_TIMER_INTERVAL     3000
181 #define NES_CM_DEFAULT_MTU            1540
182 #define NES_CM_DEFAULT_FRAME_CNT      10
183 #define NES_CM_THREAD_STACK_SIZE      256
184 #define NES_CM_DEFAULT_RCV_WND        64240	// before we know that window scaling is allowed
185 #define NES_CM_DEFAULT_RCV_WND_SCALED 256960  // after we know that window scaling is allowed
186 #define NES_CM_DEFAULT_RCV_WND_SCALE  2
187 #define NES_CM_DEFAULT_FREE_PKTS      0x000A
188 #define NES_CM_FREE_PKT_LO_WATERMARK  2
189 
190 #define NES_CM_DEFAULT_MSS   536
191 
192 #define NES_CM_DEF_SEQ       0x159bf75f
193 #define NES_CM_DEF_LOCAL_ID  0x3b47
194 
195 #define NES_CM_DEF_SEQ2      0x18ed5740
196 #define NES_CM_DEF_LOCAL_ID2 0xb807
197 #define	MAX_CM_BUFFER	(IETF_MPA_FRAME_SIZE + IETF_RTR_MSG_SIZE + IETF_MAX_PRIV_DATA_LEN)
198 
199 typedef u32 nes_addr_t;
200 
201 #define nes_cm_tsa_context nes_qp_context
202 
203 struct nes_qp;
204 
205 /* cm node transition states */
206 enum nes_cm_node_state {
207 	NES_CM_STATE_UNKNOWN,
208 	NES_CM_STATE_INITED,
209 	NES_CM_STATE_LISTENING,
210 	NES_CM_STATE_SYN_RCVD,
211 	NES_CM_STATE_SYN_SENT,
212 	NES_CM_STATE_ONE_SIDE_ESTABLISHED,
213 	NES_CM_STATE_ESTABLISHED,
214 	NES_CM_STATE_ACCEPTING,
215 	NES_CM_STATE_MPAREQ_SENT,
216 	NES_CM_STATE_MPAREQ_RCVD,
217 	NES_CM_STATE_MPAREJ_RCVD,
218 	NES_CM_STATE_TSA,
219 	NES_CM_STATE_FIN_WAIT1,
220 	NES_CM_STATE_FIN_WAIT2,
221 	NES_CM_STATE_CLOSE_WAIT,
222 	NES_CM_STATE_TIME_WAIT,
223 	NES_CM_STATE_LAST_ACK,
224 	NES_CM_STATE_CLOSING,
225 	NES_CM_STATE_LISTENER_DESTROYED,
226 	NES_CM_STATE_CLOSED
227 };
228 
229 enum mpa_frame_version {
230 	IETF_MPA_V1 = 1,
231 	IETF_MPA_V2 = 2
232 };
233 
234 enum mpa_frame_key {
235 	MPA_KEY_REQUEST,
236 	MPA_KEY_REPLY
237 };
238 
239 enum send_rdma0 {
240 	SEND_RDMA_READ_ZERO = 1,
241 	SEND_RDMA_WRITE_ZERO = 2
242 };
243 
244 enum nes_tcpip_pkt_type {
245 	NES_PKT_TYPE_UNKNOWN,
246 	NES_PKT_TYPE_SYN,
247 	NES_PKT_TYPE_SYNACK,
248 	NES_PKT_TYPE_ACK,
249 	NES_PKT_TYPE_FIN,
250 	NES_PKT_TYPE_RST
251 };
252 
253 
254 /* type of nes connection */
255 enum nes_cm_conn_type {
256 	NES_CM_IWARP_CONN_TYPE,
257 };
258 
259 /* CM context params */
260 struct nes_cm_tcp_context {
261 	u8  client;
262 
263 	u32 loc_seq_num;
264 	u32 loc_ack_num;
265 	u32 rem_ack_num;
266 	u32 rcv_nxt;
267 
268 	u32 loc_id;
269 	u32 rem_id;
270 
271 	u32 snd_wnd;
272 	u32 max_snd_wnd;
273 
274 	u32 rcv_wnd;
275 	u32 mss;
276 	u8  snd_wscale;
277 	u8  rcv_wscale;
278 
279 	struct nes_cm_tsa_context tsa_cntxt;
280 	struct timeval            sent_ts;
281 };
282 
283 
284 enum nes_cm_listener_state {
285 	NES_CM_LISTENER_PASSIVE_STATE = 1,
286 	NES_CM_LISTENER_ACTIVE_STATE = 2,
287 	NES_CM_LISTENER_EITHER_STATE = 3
288 };
289 
290 struct nes_cm_listener {
291 	struct list_head           list;
292 	struct nes_cm_core         *cm_core;
293 	u8                         loc_mac[ETH_ALEN];
294 	nes_addr_t                 loc_addr;
295 	u16                        loc_port;
296 	struct iw_cm_id            *cm_id;
297 	enum nes_cm_conn_type      conn_type;
298 	atomic_t                   ref_count;
299 	struct nes_vnic            *nesvnic;
300 	atomic_t                   pend_accepts_cnt;
301 	int                        backlog;
302 	enum nes_cm_listener_state listener_state;
303 	u32                        reused_node;
304 };
305 
306 /* per connection node and node state information */
307 struct nes_cm_node {
308 	nes_addr_t                loc_addr, rem_addr;
309 	u16                       loc_port, rem_port;
310 
311 	u8                        loc_mac[ETH_ALEN];
312 	u8                        rem_mac[ETH_ALEN];
313 
314 	enum nes_cm_node_state    state;
315 	struct nes_cm_tcp_context tcp_cntxt;
316 	struct nes_cm_core        *cm_core;
317 	struct sk_buff_head       resend_list;
318 	atomic_t                  ref_count;
319 	struct net_device         *netdev;
320 
321 	struct nes_cm_node        *loopbackpartner;
322 
323 	struct nes_timer_entry	  *send_entry;
324 	struct nes_timer_entry    *recv_entry;
325 	spinlock_t                retrans_list_lock;
326 	enum send_rdma0           send_rdma0_op;
327 
328 	union {
329 		struct ietf_mpa_v1 mpa_frame;
330 		struct ietf_mpa_v2 mpa_v2_frame;
331 		u8                 mpa_frame_buf[MAX_CM_BUFFER];
332 	};
333 	enum mpa_frame_version    mpa_frame_rev;
334 	u16			  ird_size;
335 	u16                       ord_size;
336 
337 	u16                       mpa_frame_size;
338 	struct iw_cm_id           *cm_id;
339 	struct list_head          list;
340 	int                       accelerated;
341 	struct nes_cm_listener    *listener;
342 	enum nes_cm_conn_type     conn_type;
343 	struct nes_vnic           *nesvnic;
344 	int                       apbvt_set;
345 	int                       accept_pend;
346 	struct list_head	timer_entry;
347 	struct list_head	reset_entry;
348 	struct nes_qp		*nesqp;
349 	atomic_t 		passive_state;
350 };
351 
352 /* structure for client or CM to fill when making CM api calls. */
353 /*	- only need to set relevant data, based on op. */
354 struct nes_cm_info {
355 	union {
356 		struct iw_cm_id   *cm_id;
357 		struct net_device *netdev;
358 	};
359 
360 	u16 loc_port;
361 	u16 rem_port;
362 	nes_addr_t loc_addr;
363 	nes_addr_t rem_addr;
364 
365 	enum nes_cm_conn_type  conn_type;
366 	int backlog;
367 };
368 
369 /* CM event codes */
370 enum  nes_cm_event_type {
371 	NES_CM_EVENT_UNKNOWN,
372 	NES_CM_EVENT_ESTABLISHED,
373 	NES_CM_EVENT_MPA_REQ,
374 	NES_CM_EVENT_MPA_CONNECT,
375 	NES_CM_EVENT_MPA_ACCEPT,
376 	NES_CM_EVENT_MPA_REJECT,
377 	NES_CM_EVENT_MPA_ESTABLISHED,
378 	NES_CM_EVENT_CONNECTED,
379 	NES_CM_EVENT_CLOSED,
380 	NES_CM_EVENT_RESET,
381 	NES_CM_EVENT_DROPPED_PKT,
382 	NES_CM_EVENT_CLOSE_IMMED,
383 	NES_CM_EVENT_CLOSE_HARD,
384 	NES_CM_EVENT_CLOSE_CLEAN,
385 	NES_CM_EVENT_ABORTED,
386 	NES_CM_EVENT_SEND_FIRST
387 };
388 
389 /* event to post to CM event handler */
390 struct nes_cm_event {
391 	enum nes_cm_event_type type;
392 
393 	struct nes_cm_info cm_info;
394 	struct work_struct event_work;
395 	struct nes_cm_node *cm_node;
396 };
397 
398 struct nes_cm_core {
399 	enum nes_cm_node_state  state;
400 
401 	atomic_t                listen_node_cnt;
402 	struct nes_cm_node      listen_list;
403 	spinlock_t              listen_list_lock;
404 
405 	u32                     mtu;
406 	u32                     free_tx_pkt_max;
407 	u32                     rx_pkt_posted;
408 	atomic_t                ht_node_cnt;
409 	struct list_head        connected_nodes;
410 	/* struct list_head hashtable[NES_CM_HASHTABLE_SIZE]; */
411 	spinlock_t              ht_lock;
412 
413 	struct timer_list       tcp_timer;
414 
415 	struct nes_cm_ops       *api;
416 
417 	int (*post_event)(struct nes_cm_event *event);
418 	atomic_t                events_posted;
419 	struct workqueue_struct *event_wq;
420 	struct workqueue_struct *disconn_wq;
421 
422 	atomic_t                node_cnt;
423 	u64                     aborted_connects;
424 	u32                     options;
425 
426 	struct nes_cm_node      *current_listen_node;
427 };
428 
429 
430 #define NES_CM_SET_PKT_SIZE        (1 << 1)
431 #define NES_CM_SET_FREE_PKT_Q_SIZE (1 << 2)
432 
433 /* CM ops/API for client interface */
434 struct nes_cm_ops {
435 	int (*accelerated)(struct nes_cm_core *, struct nes_cm_node *);
436 	struct nes_cm_listener * (*listen)(struct nes_cm_core *, struct nes_vnic *,
437 			struct nes_cm_info *);
438 	int (*stop_listener)(struct nes_cm_core *, struct nes_cm_listener *);
439 	struct nes_cm_node * (*connect)(struct nes_cm_core *,
440 			struct nes_vnic *, u16, void *,
441 			struct nes_cm_info *);
442 	int (*close)(struct nes_cm_core *, struct nes_cm_node *);
443 	int (*accept)(struct nes_cm_core *, struct nes_cm_node *);
444 	int (*reject)(struct nes_cm_core *, struct nes_cm_node *);
445 	int (*recv_pkt)(struct nes_cm_core *, struct nes_vnic *,
446 			struct sk_buff *);
447 	int (*destroy_cm_core)(struct nes_cm_core *);
448 	int (*get)(struct nes_cm_core *);
449 	int (*set)(struct nes_cm_core *, u32, u32);
450 };
451 
452 int schedule_nes_timer(struct nes_cm_node *, struct sk_buff *,
453 		enum nes_timer_type, int, int);
454 
455 int nes_accept(struct iw_cm_id *, struct iw_cm_conn_param *);
456 int nes_reject(struct iw_cm_id *, const void *, u8);
457 int nes_connect(struct iw_cm_id *, struct iw_cm_conn_param *);
458 int nes_create_listen(struct iw_cm_id *, int);
459 int nes_destroy_listen(struct iw_cm_id *);
460 
461 int nes_cm_recv(struct sk_buff *, struct net_device *);
462 int nes_cm_start(void);
463 int nes_cm_stop(void);
464 int nes_add_ref_cm_node(struct nes_cm_node *cm_node);
465 int nes_rem_ref_cm_node(struct nes_cm_node *cm_node);
466 
467 #endif			/* NES_CM_H */
468