• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006 - 2014 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 
35 #define TCPOPT_TIMESTAMP 8
36 
37 #include <linux/atomic.h>
38 #include <linux/skbuff.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_vlan.h>
44 #include <linux/notifier.h>
45 #include <linux/net.h>
46 #include <linux/types.h>
47 #include <linux/timer.h>
48 #include <linux/time.h>
49 #include <linux/delay.h>
50 #include <linux/etherdevice.h>
51 #include <linux/netdevice.h>
52 #include <linux/random.h>
53 #include <linux/list.h>
54 #include <linux/threads.h>
55 #include <linux/highmem.h>
56 #include <linux/slab.h>
57 #include <net/arp.h>
58 #include <net/neighbour.h>
59 #include <net/route.h>
60 #include <net/ip_fib.h>
61 #include <net/tcp.h>
62 #include <linux/fcntl.h>
63 
64 #include "nes.h"
65 
66 u32 cm_packets_sent;
67 u32 cm_packets_bounced;
68 u32 cm_packets_dropped;
69 u32 cm_packets_retrans;
70 u32 cm_packets_created;
71 u32 cm_packets_received;
72 atomic_t cm_listens_created;
73 atomic_t cm_listens_destroyed;
74 u32 cm_backlog_drops;
75 atomic_t cm_loopbacks;
76 atomic_t cm_nodes_created;
77 atomic_t cm_nodes_destroyed;
78 atomic_t cm_accel_dropped_pkts;
79 atomic_t cm_resets_recvd;
80 
81 static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
82 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
83 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
84 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
85 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
86 static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
87 static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
88 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
89 static int mini_cm_dealloc_core(struct nes_cm_core *);
90 static int mini_cm_get(struct nes_cm_core *);
91 static int mini_cm_set(struct nes_cm_core *, u32, u32);
92 
93 static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
94 static int add_ref_cm_node(struct nes_cm_node *);
95 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
96 
97 static int nes_cm_disconn_true(struct nes_qp *);
98 static int nes_cm_post_event(struct nes_cm_event *event);
99 static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
100 static void nes_disconnect_worker(struct work_struct *work);
101 
102 static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
103 static int send_mpa_reject(struct nes_cm_node *);
104 static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
105 static int send_reset(struct nes_cm_node *, struct sk_buff *);
106 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
107 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
108 static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
109 
110 static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
111 static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
112 static void cleanup_retrans_entry(struct nes_cm_node *);
113 static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
114 static void free_retrans_entry(struct nes_cm_node *cm_node);
115 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
116 
117 /* CM event handler functions */
118 static void cm_event_connected(struct nes_cm_event *);
119 static void cm_event_connect_error(struct nes_cm_event *);
120 static void cm_event_reset(struct nes_cm_event *);
121 static void cm_event_mpa_req(struct nes_cm_event *);
122 static void cm_event_mpa_reject(struct nes_cm_event *);
123 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
124 
125 /* MPA build functions */
126 static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
127 static void build_mpa_v2(struct nes_cm_node *, void *, u8);
128 static void build_mpa_v1(struct nes_cm_node *, void *, u8);
129 static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
130 
131 static void print_core(struct nes_cm_core *core);
132 static void record_ird_ord(struct nes_cm_node *, u16, u16);
133 
134 /* External CM API Interface */
135 /* instance of function pointers for client API */
136 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
137 static struct nes_cm_ops nes_cm_api = {
138 	mini_cm_accelerated,
139 	mini_cm_listen,
140 	mini_cm_del_listen,
141 	mini_cm_connect,
142 	mini_cm_close,
143 	mini_cm_accept,
144 	mini_cm_reject,
145 	mini_cm_recv_pkt,
146 	mini_cm_dealloc_core,
147 	mini_cm_get,
148 	mini_cm_set
149 };
150 
151 static struct nes_cm_core *g_cm_core;
152 
153 atomic_t cm_connects;
154 atomic_t cm_accepts;
155 atomic_t cm_disconnects;
156 atomic_t cm_closes;
157 atomic_t cm_connecteds;
158 atomic_t cm_connect_reqs;
159 atomic_t cm_rejects;
160 
nes_add_ref_cm_node(struct nes_cm_node * cm_node)161 int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
162 {
163 	return add_ref_cm_node(cm_node);
164 }
165 
nes_rem_ref_cm_node(struct nes_cm_node * cm_node)166 int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
167 {
168 	return rem_ref_cm_node(cm_node->cm_core, cm_node);
169 }
170 /**
171  * create_event
172  */
create_event(struct nes_cm_node * cm_node,enum nes_cm_event_type type)173 static struct nes_cm_event *create_event(struct nes_cm_node *	cm_node,
174 					 enum nes_cm_event_type type)
175 {
176 	struct nes_cm_event *event;
177 
178 	if (!cm_node->cm_id)
179 		return NULL;
180 
181 	/* allocate an empty event */
182 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
183 
184 	if (!event)
185 		return NULL;
186 
187 	event->type = type;
188 	event->cm_node = cm_node;
189 	event->cm_info.rem_addr = cm_node->rem_addr;
190 	event->cm_info.loc_addr = cm_node->loc_addr;
191 	event->cm_info.rem_port = cm_node->rem_port;
192 	event->cm_info.loc_port = cm_node->loc_port;
193 	event->cm_info.cm_id = cm_node->cm_id;
194 
195 	nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
196 		  "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
197 		  cm_node, event, type, event->cm_info.loc_addr,
198 		  event->cm_info.loc_port, event->cm_info.rem_addr,
199 		  event->cm_info.rem_port);
200 
201 	nes_cm_post_event(event);
202 	return event;
203 }
204 
205 
206 /**
207  * send_mpa_request
208  */
send_mpa_request(struct nes_cm_node * cm_node,struct sk_buff * skb)209 static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
210 {
211 	u8 start_addr = 0;
212 	u8 *start_ptr = &start_addr;
213 	u8 **start_buff = &start_ptr;
214 	u16 buff_len = 0;
215 
216 	if (!skb) {
217 		nes_debug(NES_DBG_CM, "skb set to NULL\n");
218 		return -1;
219 	}
220 
221 	/* send an MPA Request frame */
222 	cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
223 	form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
224 
225 	return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
226 }
227 
228 
229 
send_mpa_reject(struct nes_cm_node * cm_node)230 static int send_mpa_reject(struct nes_cm_node *cm_node)
231 {
232 	struct sk_buff *skb = NULL;
233 	u8 start_addr = 0;
234 	u8 *start_ptr = &start_addr;
235 	u8 **start_buff = &start_ptr;
236 	u16 buff_len = 0;
237 	struct ietf_mpa_v1 *mpa_frame;
238 
239 	skb = dev_alloc_skb(MAX_CM_BUFFER);
240 	if (!skb) {
241 		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
242 		return -ENOMEM;
243 	}
244 
245 	/* send an MPA reject frame */
246 	cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
247 	mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
248 	mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
249 	form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
250 
251 	cm_node->state = NES_CM_STATE_FIN_WAIT1;
252 	return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
253 }
254 
255 
256 /**
257  * recv_mpa - process a received TCP pkt, we are expecting an
258  * IETF MPA frame
259  */
parse_mpa(struct nes_cm_node * cm_node,u8 * buffer,u32 * type,u32 len)260 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
261 		     u32 len)
262 {
263 	struct ietf_mpa_v1 *mpa_frame;
264 	struct ietf_mpa_v2 *mpa_v2_frame;
265 	struct ietf_rtr_msg *rtr_msg;
266 	int mpa_hdr_len;
267 	int priv_data_len;
268 
269 	*type = NES_MPA_REQUEST_ACCEPT;
270 
271 	/* assume req frame is in tcp data payload */
272 	if (len < sizeof(struct ietf_mpa_v1)) {
273 		nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
274 		return -EINVAL;
275 	}
276 
277 	/* points to the beginning of the frame, which could be MPA V1 or V2 */
278 	mpa_frame = (struct ietf_mpa_v1 *)buffer;
279 	mpa_hdr_len = sizeof(struct ietf_mpa_v1);
280 	priv_data_len = ntohs(mpa_frame->priv_data_len);
281 
282 	/* make sure mpa private data len is less than 512 bytes */
283 	if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
284 		nes_debug(NES_DBG_CM, "The received Length of Private"
285 			  " Data field exceeds 512 octets\n");
286 		return -EINVAL;
287 	}
288 	/*
289 	 * make sure MPA receiver interoperate with the
290 	 * received MPA version and MPA key information
291 	 *
292 	 */
293 	if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
294 		nes_debug(NES_DBG_CM, "The received mpa version"
295 			  " is not supported\n");
296 		return -EINVAL;
297 	}
298 	/*
299 	* backwards compatibility only
300 	*/
301 	if (mpa_frame->rev > cm_node->mpa_frame_rev) {
302 		nes_debug(NES_DBG_CM, "The received mpa version"
303 			" can not be interoperated\n");
304 		return -EINVAL;
305 	} else {
306 		cm_node->mpa_frame_rev = mpa_frame->rev;
307 	}
308 
309 	if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
310 		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
311 			nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
312 			return -EINVAL;
313 		}
314 	} else {
315 		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
316 			nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
317 			return -EINVAL;
318 		}
319 	}
320 
321 	if (priv_data_len + mpa_hdr_len != len) {
322 		nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
323 			" complete (%x + %x != %x)\n",
324 			priv_data_len, mpa_hdr_len, len);
325 		return -EINVAL;
326 	}
327 	/* make sure it does not exceed the max size */
328 	if (len > MAX_CM_BUFFER) {
329 		nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
330 			" (%x + %x != %x)\n",
331 			priv_data_len, mpa_hdr_len, len);
332 		return -EINVAL;
333 	}
334 
335 	cm_node->mpa_frame_size = priv_data_len;
336 
337 	switch (mpa_frame->rev) {
338 	case IETF_MPA_V2: {
339 		u16 ird_size;
340 		u16 ord_size;
341 		u16 rtr_ctrl_ird;
342 		u16 rtr_ctrl_ord;
343 
344 		mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
345 		mpa_hdr_len += IETF_RTR_MSG_SIZE;
346 		cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
347 		rtr_msg = &mpa_v2_frame->rtr_msg;
348 
349 		/* parse rtr message */
350 		rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
351 		rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
352 		ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
353 		ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
354 
355 		if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
356 			/* send reset */
357 			return -EINVAL;
358 		}
359 		if (ird_size == IETF_NO_IRD_ORD || ord_size == IETF_NO_IRD_ORD)
360 			cm_node->mpav2_ird_ord = IETF_NO_IRD_ORD;
361 
362 		if (cm_node->mpav2_ird_ord != IETF_NO_IRD_ORD) {
363 			/* responder */
364 			if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
365 				/* we are still negotiating */
366 				if (ord_size > NES_MAX_IRD) {
367 					cm_node->ird_size = NES_MAX_IRD;
368 				} else {
369 					cm_node->ird_size = ord_size;
370 					if (ord_size == 0 &&
371 					(rtr_ctrl_ord & IETF_RDMA0_READ)) {
372 						cm_node->ird_size = 1;
373 						nes_debug(NES_DBG_CM,
374 						"%s: Remote peer doesn't support RDMA0_READ (ord=%u)\n",
375 							__func__, ord_size);
376 					}
377 				}
378 				if (ird_size > NES_MAX_ORD)
379 					cm_node->ord_size = NES_MAX_ORD;
380 				else
381 					cm_node->ord_size = ird_size;
382 			} else { /* initiator */
383 				if (ord_size > NES_MAX_IRD) {
384 					nes_debug(NES_DBG_CM,
385 					"%s: Unable to support the requested (ord =%u)\n",
386 							__func__, ord_size);
387 					return -EINVAL;
388 				}
389 				cm_node->ird_size = ord_size;
390 
391 				if (ird_size > NES_MAX_ORD) {
392 					cm_node->ord_size = NES_MAX_ORD;
393 				} else {
394 					if (ird_size == 0 &&
395 					(rtr_ctrl_ord & IETF_RDMA0_READ)) {
396 						nes_debug(NES_DBG_CM,
397 						"%s: Remote peer doesn't support RDMA0_READ (ird=%u)\n",
398 							__func__, ird_size);
399 						return -EINVAL;
400 					} else {
401 						cm_node->ord_size = ird_size;
402 					}
403 				}
404 			}
405 		}
406 
407 		if (rtr_ctrl_ord & IETF_RDMA0_READ) {
408 			cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
409 
410 		} else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
411 			cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
412 		} else {        /* Not supported RDMA0 operation */
413 			return -EINVAL;
414 		}
415 		break;
416 	}
417 	case IETF_MPA_V1:
418 	default:
419 		break;
420 	}
421 
422 	/* copy entire MPA frame to our cm_node's frame */
423 	memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
424 
425 	if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
426 		*type = NES_MPA_REQUEST_REJECT;
427 	return 0;
428 }
429 
430 
431 /**
432  * form_cm_frame - get a free packet and build empty frame Use
433  * node info to build.
434  */
form_cm_frame(struct sk_buff * skb,struct nes_cm_node * cm_node,void * options,u32 optionsize,void * data,u32 datasize,u8 flags)435 static void form_cm_frame(struct sk_buff *skb,
436 			  struct nes_cm_node *cm_node, void *options, u32 optionsize,
437 			  void *data, u32 datasize, u8 flags)
438 {
439 	struct tcphdr *tcph;
440 	struct iphdr *iph;
441 	struct ethhdr *ethh;
442 	u8 *buf;
443 	u16 packetsize = sizeof(*iph);
444 
445 	packetsize += sizeof(*tcph);
446 	packetsize += optionsize + datasize;
447 
448 	skb_trim(skb, 0);
449 	memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
450 
451 	buf = skb_put(skb, packetsize + ETH_HLEN);
452 
453 	ethh = (struct ethhdr *)buf;
454 	buf += ETH_HLEN;
455 
456 	iph = (struct iphdr *)buf;
457 	buf += sizeof(*iph);
458 	tcph = (struct tcphdr *)buf;
459 	skb_reset_mac_header(skb);
460 	skb_set_network_header(skb, ETH_HLEN);
461 	skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
462 	buf += sizeof(*tcph);
463 
464 	skb->ip_summed = CHECKSUM_PARTIAL;
465 	if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
466 		skb->ip_summed = CHECKSUM_NONE;
467 	skb->protocol = htons(0x800);
468 	skb->data_len = 0;
469 	skb->mac_len = ETH_HLEN;
470 
471 	memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
472 	memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
473 	ethh->h_proto = htons(0x0800);
474 
475 	iph->version = IPVERSION;
476 	iph->ihl = 5;           /* 5 * 4Byte words, IP headr len */
477 	iph->tos = 0;
478 	iph->tot_len = htons(packetsize);
479 	iph->id = htons(++cm_node->tcp_cntxt.loc_id);
480 
481 	iph->frag_off = htons(0x4000);
482 	iph->ttl = 0x40;
483 	iph->protocol = 0x06;   /* IPPROTO_TCP */
484 
485 	iph->saddr = htonl(cm_node->mapped_loc_addr);
486 	iph->daddr = htonl(cm_node->mapped_rem_addr);
487 
488 	tcph->source = htons(cm_node->mapped_loc_port);
489 	tcph->dest = htons(cm_node->mapped_rem_port);
490 	tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
491 
492 	if (flags & SET_ACK) {
493 		cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
494 		tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
495 		tcph->ack = 1;
496 	} else {
497 		tcph->ack_seq = 0;
498 	}
499 
500 	if (flags & SET_SYN) {
501 		cm_node->tcp_cntxt.loc_seq_num++;
502 		tcph->syn = 1;
503 	} else {
504 		cm_node->tcp_cntxt.loc_seq_num += datasize;
505 	}
506 
507 	if (flags & SET_FIN) {
508 		cm_node->tcp_cntxt.loc_seq_num++;
509 		tcph->fin = 1;
510 	}
511 
512 	if (flags & SET_RST)
513 		tcph->rst = 1;
514 
515 	tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
516 	tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
517 	tcph->urg_ptr = 0;
518 	if (optionsize)
519 		memcpy(buf, options, optionsize);
520 	buf += optionsize;
521 	if (datasize)
522 		memcpy(buf, data, datasize);
523 
524 	skb_shinfo(skb)->nr_frags = 0;
525 	cm_packets_created++;
526 }
527 
528 /*
529  * nes_create_sockaddr - Record ip addr and tcp port in a sockaddr struct
530  */
nes_create_sockaddr(__be32 ip_addr,__be16 port,struct sockaddr_storage * addr)531 static void nes_create_sockaddr(__be32 ip_addr, __be16 port,
532 				struct sockaddr_storage *addr)
533 {
534 	struct sockaddr_in *nes_sockaddr = (struct sockaddr_in *)addr;
535 	nes_sockaddr->sin_family = AF_INET;
536 	memcpy(&nes_sockaddr->sin_addr.s_addr, &ip_addr, sizeof(__be32));
537 	nes_sockaddr->sin_port = port;
538 }
539 
540 /*
541  * nes_create_mapinfo - Create a mapinfo object in the port mapper data base
542  */
nes_create_mapinfo(struct nes_cm_info * cm_info)543 static int nes_create_mapinfo(struct nes_cm_info *cm_info)
544 {
545 	struct sockaddr_storage local_sockaddr;
546 	struct sockaddr_storage mapped_sockaddr;
547 
548 	nes_create_sockaddr(htonl(cm_info->loc_addr), htons(cm_info->loc_port),
549 				&local_sockaddr);
550 	nes_create_sockaddr(htonl(cm_info->mapped_loc_addr),
551 			htons(cm_info->mapped_loc_port), &mapped_sockaddr);
552 
553 	return iwpm_create_mapinfo(&local_sockaddr,
554 				&mapped_sockaddr, RDMA_NL_NES);
555 }
556 
557 /*
558  * nes_remove_mapinfo - Remove a mapinfo object from the port mapper data base
559  *                      and send a remove mapping op message to
560  *                      the userspace port mapper
561  */
nes_remove_mapinfo(u32 loc_addr,u16 loc_port,u32 mapped_loc_addr,u16 mapped_loc_port)562 static int nes_remove_mapinfo(u32 loc_addr, u16 loc_port,
563 			u32 mapped_loc_addr, u16 mapped_loc_port)
564 {
565 	struct sockaddr_storage local_sockaddr;
566 	struct sockaddr_storage mapped_sockaddr;
567 
568 	nes_create_sockaddr(htonl(loc_addr), htons(loc_port), &local_sockaddr);
569 	nes_create_sockaddr(htonl(mapped_loc_addr), htons(mapped_loc_port),
570 				&mapped_sockaddr);
571 
572 	iwpm_remove_mapinfo(&local_sockaddr, &mapped_sockaddr);
573 	return iwpm_remove_mapping(&local_sockaddr, RDMA_NL_NES);
574 }
575 
576 /*
577  * nes_form_pm_msg - Form a port mapper message with mapping info
578  */
nes_form_pm_msg(struct nes_cm_info * cm_info,struct iwpm_sa_data * pm_msg)579 static void nes_form_pm_msg(struct nes_cm_info *cm_info,
580 				struct iwpm_sa_data *pm_msg)
581 {
582 	nes_create_sockaddr(htonl(cm_info->loc_addr), htons(cm_info->loc_port),
583 				&pm_msg->loc_addr);
584 	nes_create_sockaddr(htonl(cm_info->rem_addr), htons(cm_info->rem_port),
585 				&pm_msg->rem_addr);
586 }
587 
588 /*
589  * nes_form_reg_msg - Form a port mapper message with dev info
590  */
nes_form_reg_msg(struct nes_vnic * nesvnic,struct iwpm_dev_data * pm_msg)591 static void nes_form_reg_msg(struct nes_vnic *nesvnic,
592 			struct iwpm_dev_data *pm_msg)
593 {
594 	memcpy(pm_msg->dev_name, nesvnic->nesibdev->ibdev.name,
595 				IWPM_DEVNAME_SIZE);
596 	memcpy(pm_msg->if_name, nesvnic->netdev->name, IWPM_IFNAME_SIZE);
597 }
598 
599 /*
600  * nes_record_pm_msg - Save the received mapping info
601  */
nes_record_pm_msg(struct nes_cm_info * cm_info,struct iwpm_sa_data * pm_msg)602 static void nes_record_pm_msg(struct nes_cm_info *cm_info,
603 			struct iwpm_sa_data *pm_msg)
604 {
605 	struct sockaddr_in *mapped_loc_addr =
606 			(struct sockaddr_in *)&pm_msg->mapped_loc_addr;
607 	struct sockaddr_in *mapped_rem_addr =
608 			(struct sockaddr_in *)&pm_msg->mapped_rem_addr;
609 
610 	if (mapped_loc_addr->sin_family == AF_INET) {
611 		cm_info->mapped_loc_addr =
612 			ntohl(mapped_loc_addr->sin_addr.s_addr);
613 		cm_info->mapped_loc_port = ntohs(mapped_loc_addr->sin_port);
614 	}
615 	if (mapped_rem_addr->sin_family == AF_INET) {
616 		cm_info->mapped_rem_addr =
617 			ntohl(mapped_rem_addr->sin_addr.s_addr);
618 		cm_info->mapped_rem_port = ntohs(mapped_rem_addr->sin_port);
619 	}
620 }
621 
622 /**
623  * print_core - dump a cm core
624  */
print_core(struct nes_cm_core * core)625 static void print_core(struct nes_cm_core *core)
626 {
627 	nes_debug(NES_DBG_CM, "---------------------------------------------\n");
628 	nes_debug(NES_DBG_CM, "CM Core  -- (core = %p )\n", core);
629 	if (!core)
630 		return;
631 	nes_debug(NES_DBG_CM, "---------------------------------------------\n");
632 
633 	nes_debug(NES_DBG_CM, "State         : %u \n", core->state);
634 
635 	nes_debug(NES_DBG_CM, "Listen Nodes  : %u \n", atomic_read(&core->listen_node_cnt));
636 	nes_debug(NES_DBG_CM, "Active Nodes  : %u \n", atomic_read(&core->node_cnt));
637 
638 	nes_debug(NES_DBG_CM, "core          : %p \n", core);
639 
640 	nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
641 }
642 
record_ird_ord(struct nes_cm_node * cm_node,u16 conn_ird,u16 conn_ord)643 static void record_ird_ord(struct nes_cm_node *cm_node,
644 					u16 conn_ird, u16 conn_ord)
645 {
646 	if (conn_ird > NES_MAX_IRD)
647 		conn_ird = NES_MAX_IRD;
648 
649 	if (conn_ord > NES_MAX_ORD)
650 		conn_ord = NES_MAX_ORD;
651 
652 	cm_node->ird_size = conn_ird;
653 	cm_node->ord_size = conn_ord;
654 }
655 
656 /**
657  * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
658  */
cm_build_mpa_frame(struct nes_cm_node * cm_node,u8 ** start_buff,u16 * buff_len,u8 * pci_mem,u8 mpa_key)659 static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
660 			      u16 *buff_len, u8 *pci_mem, u8 mpa_key)
661 {
662 	int ret = 0;
663 
664 	*start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
665 
666 	switch (cm_node->mpa_frame_rev) {
667 	case IETF_MPA_V1:
668 		*start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
669 		*buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
670 		build_mpa_v1(cm_node, *start_buff, mpa_key);
671 		break;
672 	case IETF_MPA_V2:
673 		*buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
674 		build_mpa_v2(cm_node, *start_buff, mpa_key);
675 		break;
676 	default:
677 		ret = -EINVAL;
678 	}
679 	return ret;
680 }
681 
682 /**
683  * build_mpa_v2 - build a MPA V2 frame
684  */
build_mpa_v2(struct nes_cm_node * cm_node,void * start_addr,u8 mpa_key)685 static void build_mpa_v2(struct nes_cm_node *cm_node,
686 			 void *start_addr, u8 mpa_key)
687 {
688 	struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
689 	struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
690 	u16 ctrl_ird;
691 	u16 ctrl_ord;
692 
693 	/* initialize the upper 5 bytes of the frame */
694 	build_mpa_v1(cm_node, start_addr, mpa_key);
695 	mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
696 	mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
697 
698 	/* initialize RTR msg */
699 	if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
700 		ctrl_ird = IETF_NO_IRD_ORD;
701 		ctrl_ord = IETF_NO_IRD_ORD;
702 	} else {
703 		ctrl_ird = cm_node->ird_size & IETF_NO_IRD_ORD;
704 		ctrl_ord = cm_node->ord_size & IETF_NO_IRD_ORD;
705 	}
706 	ctrl_ird |= IETF_PEER_TO_PEER;
707 	ctrl_ird |= IETF_FLPDU_ZERO_LEN;
708 
709 	switch (mpa_key) {
710 	case MPA_KEY_REQUEST:
711 		ctrl_ord |= IETF_RDMA0_WRITE;
712 		ctrl_ord |= IETF_RDMA0_READ;
713 		break;
714 	case MPA_KEY_REPLY:
715 		switch (cm_node->send_rdma0_op) {
716 		case SEND_RDMA_WRITE_ZERO:
717 			ctrl_ord |= IETF_RDMA0_WRITE;
718 			break;
719 		case SEND_RDMA_READ_ZERO:
720 			ctrl_ord |= IETF_RDMA0_READ;
721 			break;
722 		}
723 	}
724 	rtr_msg->ctrl_ird = htons(ctrl_ird);
725 	rtr_msg->ctrl_ord = htons(ctrl_ord);
726 }
727 
728 /**
729  * build_mpa_v1 - build a MPA V1 frame
730  */
build_mpa_v1(struct nes_cm_node * cm_node,void * start_addr,u8 mpa_key)731 static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
732 {
733 	struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
734 
735 	switch (mpa_key) {
736 	case MPA_KEY_REQUEST:
737 		memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
738 		break;
739 	case MPA_KEY_REPLY:
740 		memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
741 		break;
742 	}
743 	mpa_frame->flags = IETF_MPA_FLAGS_CRC;
744 	mpa_frame->rev = cm_node->mpa_frame_rev;
745 	mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
746 }
747 
build_rdma0_msg(struct nes_cm_node * cm_node,struct nes_qp ** nesqp_addr)748 static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
749 {
750 	u64 u64temp;
751 	struct nes_qp *nesqp = *nesqp_addr;
752 	struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
753 
754 	u64temp = (unsigned long)nesqp->nesuqp_addr;
755 	u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
756 	set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
757 
758 	wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
759 	wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
760 
761 	switch (cm_node->send_rdma0_op) {
762 	case SEND_RDMA_WRITE_ZERO:
763 		nes_debug(NES_DBG_CM, "Sending first write.\n");
764 		wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
765 			cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
766 		wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
767 		wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
768 		wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
769 		break;
770 
771 	case SEND_RDMA_READ_ZERO:
772 	default:
773 		if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
774 			WARN(1, "Unsupported RDMA0 len operation=%u\n",
775 			     cm_node->send_rdma0_op);
776 		nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
777 		wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
778 			cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
779 		wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
780 		wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
781 		wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
782 		wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
783 		wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
784 		break;
785 	}
786 
787 	if (nesqp->sq_kmapped) {
788 		nesqp->sq_kmapped = 0;
789 		kunmap(nesqp->page);
790 	}
791 
792 	/*use the reserved spot on the WQ for the extra first WQE*/
793 	nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
794 							     NES_QPCONTEXT_ORDIRD_WRPDU |
795 							     NES_QPCONTEXT_ORDIRD_ALSMM));
796 	nesqp->skip_lsmm = 1;
797 	nesqp->hwqp.sq_tail = 0;
798 }
799 
800 /**
801  * schedule_nes_timer
802  * note - cm_node needs to be protected before calling this. Encase in:
803  *			rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
804  */
schedule_nes_timer(struct nes_cm_node * cm_node,struct sk_buff * skb,enum nes_timer_type type,int send_retrans,int close_when_complete)805 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
806 		       enum nes_timer_type type, int send_retrans,
807 		       int close_when_complete)
808 {
809 	unsigned long flags;
810 	struct nes_cm_core *cm_core = cm_node->cm_core;
811 	struct nes_timer_entry *new_send;
812 	int ret = 0;
813 
814 	new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
815 	if (!new_send)
816 		return -ENOMEM;
817 
818 	/* new_send->timetosend = currenttime */
819 	new_send->retrycount = NES_DEFAULT_RETRYS;
820 	new_send->retranscount = NES_DEFAULT_RETRANS;
821 	new_send->skb = skb;
822 	new_send->timetosend = jiffies;
823 	new_send->type = type;
824 	new_send->netdev = cm_node->netdev;
825 	new_send->send_retrans = send_retrans;
826 	new_send->close_when_complete = close_when_complete;
827 
828 	if (type == NES_TIMER_TYPE_CLOSE) {
829 		new_send->timetosend += (HZ / 10);
830 		if (cm_node->recv_entry) {
831 			kfree(new_send);
832 			WARN_ON(1);
833 			return -EINVAL;
834 		}
835 		cm_node->recv_entry = new_send;
836 	}
837 
838 	if (type == NES_TIMER_TYPE_SEND) {
839 		new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
840 		atomic_inc(&new_send->skb->users);
841 		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
842 		cm_node->send_entry = new_send;
843 		add_ref_cm_node(cm_node);
844 		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
845 		new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
846 
847 		ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
848 		if (ret != NETDEV_TX_OK) {
849 			nes_debug(NES_DBG_CM, "Error sending packet %p "
850 				  "(jiffies = %lu)\n", new_send, jiffies);
851 			new_send->timetosend = jiffies;
852 			ret = NETDEV_TX_OK;
853 		} else {
854 			cm_packets_sent++;
855 			if (!send_retrans) {
856 				cleanup_retrans_entry(cm_node);
857 				if (close_when_complete)
858 					rem_ref_cm_node(cm_core, cm_node);
859 				return ret;
860 			}
861 		}
862 	}
863 
864 	if (!timer_pending(&cm_core->tcp_timer))
865 		mod_timer(&cm_core->tcp_timer, new_send->timetosend);
866 
867 	return ret;
868 }
869 
nes_retrans_expired(struct nes_cm_node * cm_node)870 static void nes_retrans_expired(struct nes_cm_node *cm_node)
871 {
872 	struct iw_cm_id *cm_id = cm_node->cm_id;
873 	enum nes_cm_node_state state = cm_node->state;
874 	cm_node->state = NES_CM_STATE_CLOSED;
875 
876 	switch (state) {
877 	case NES_CM_STATE_SYN_RCVD:
878 	case NES_CM_STATE_CLOSING:
879 		rem_ref_cm_node(cm_node->cm_core, cm_node);
880 		break;
881 	case NES_CM_STATE_LAST_ACK:
882 	case NES_CM_STATE_FIN_WAIT1:
883 		if (cm_node->cm_id)
884 			cm_id->rem_ref(cm_id);
885 		send_reset(cm_node, NULL);
886 		break;
887 	default:
888 		add_ref_cm_node(cm_node);
889 		send_reset(cm_node, NULL);
890 		create_event(cm_node, NES_CM_EVENT_ABORTED);
891 	}
892 }
893 
handle_recv_entry(struct nes_cm_node * cm_node,u32 rem_node)894 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
895 {
896 	struct nes_timer_entry *recv_entry = cm_node->recv_entry;
897 	struct iw_cm_id *cm_id = cm_node->cm_id;
898 	struct nes_qp *nesqp;
899 	unsigned long qplockflags;
900 
901 	if (!recv_entry)
902 		return;
903 	nesqp = (struct nes_qp *)recv_entry->skb;
904 	if (nesqp) {
905 		spin_lock_irqsave(&nesqp->lock, qplockflags);
906 		if (nesqp->cm_id) {
907 			nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
908 				  "refcount = %d: HIT A "
909 				  "NES_TIMER_TYPE_CLOSE with something "
910 				  "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
911 				  atomic_read(&nesqp->refcount));
912 			nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
913 			nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
914 			nesqp->ibqp_state = IB_QPS_ERR;
915 			spin_unlock_irqrestore(&nesqp->lock, qplockflags);
916 			nes_cm_disconn(nesqp);
917 		} else {
918 			spin_unlock_irqrestore(&nesqp->lock, qplockflags);
919 			nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
920 				  "refcount = %d: HIT A "
921 				  "NES_TIMER_TYPE_CLOSE with nothing "
922 				  "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
923 				  atomic_read(&nesqp->refcount));
924 		}
925 	} else if (rem_node) {
926 		/* TIME_WAIT state */
927 		rem_ref_cm_node(cm_node->cm_core, cm_node);
928 	}
929 	if (cm_node->cm_id)
930 		cm_id->rem_ref(cm_id);
931 	kfree(recv_entry);
932 	cm_node->recv_entry = NULL;
933 }
934 
935 /**
936  * nes_cm_timer_tick
937  */
nes_cm_timer_tick(unsigned long pass)938 static void nes_cm_timer_tick(unsigned long pass)
939 {
940 	unsigned long flags;
941 	unsigned long nexttimeout = jiffies + NES_LONG_TIME;
942 	struct nes_cm_node *cm_node;
943 	struct nes_timer_entry *send_entry, *recv_entry;
944 	struct list_head *list_core_temp;
945 	struct list_head *list_node;
946 	struct nes_cm_core *cm_core = g_cm_core;
947 	u32 settimer = 0;
948 	unsigned long timetosend;
949 	int ret = NETDEV_TX_OK;
950 
951 	struct list_head timer_list;
952 
953 	INIT_LIST_HEAD(&timer_list);
954 	spin_lock_irqsave(&cm_core->ht_lock, flags);
955 
956 	list_for_each_safe(list_node, list_core_temp,
957 			   &cm_core->connected_nodes) {
958 		cm_node = container_of(list_node, struct nes_cm_node, list);
959 		if ((cm_node->recv_entry) || (cm_node->send_entry)) {
960 			add_ref_cm_node(cm_node);
961 			list_add(&cm_node->timer_entry, &timer_list);
962 		}
963 	}
964 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
965 
966 	list_for_each_safe(list_node, list_core_temp, &timer_list) {
967 		cm_node = container_of(list_node, struct nes_cm_node,
968 				       timer_entry);
969 		recv_entry = cm_node->recv_entry;
970 
971 		if (recv_entry) {
972 			if (time_after(recv_entry->timetosend, jiffies)) {
973 				if (nexttimeout > recv_entry->timetosend ||
974 				    !settimer) {
975 					nexttimeout = recv_entry->timetosend;
976 					settimer = 1;
977 				}
978 			} else {
979 				handle_recv_entry(cm_node, 1);
980 			}
981 		}
982 
983 		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
984 		do {
985 			send_entry = cm_node->send_entry;
986 			if (!send_entry)
987 				break;
988 			if (time_after(send_entry->timetosend, jiffies)) {
989 				if (cm_node->state != NES_CM_STATE_TSA) {
990 					if ((nexttimeout >
991 					     send_entry->timetosend) ||
992 					    !settimer) {
993 						nexttimeout =
994 							send_entry->timetosend;
995 						settimer = 1;
996 					}
997 				} else {
998 					free_retrans_entry(cm_node);
999 				}
1000 				break;
1001 			}
1002 
1003 			if ((cm_node->state == NES_CM_STATE_TSA) ||
1004 			    (cm_node->state == NES_CM_STATE_CLOSED)) {
1005 				free_retrans_entry(cm_node);
1006 				break;
1007 			}
1008 
1009 			if (!send_entry->retranscount ||
1010 			    !send_entry->retrycount) {
1011 				cm_packets_dropped++;
1012 				free_retrans_entry(cm_node);
1013 
1014 				spin_unlock_irqrestore(
1015 					&cm_node->retrans_list_lock, flags);
1016 				nes_retrans_expired(cm_node);
1017 				cm_node->state = NES_CM_STATE_CLOSED;
1018 				spin_lock_irqsave(&cm_node->retrans_list_lock,
1019 						  flags);
1020 				break;
1021 			}
1022 			atomic_inc(&send_entry->skb->users);
1023 			cm_packets_retrans++;
1024 			nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
1025 				  "for node %p, jiffies = %lu, time to send = "
1026 				  "%lu, retranscount = %u, send_entry->seq_num = "
1027 				  "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
1028 				  "0x%08X\n", send_entry, cm_node, jiffies,
1029 				  send_entry->timetosend,
1030 				  send_entry->retranscount,
1031 				  send_entry->seq_num,
1032 				  cm_node->tcp_cntxt.rem_ack_num);
1033 
1034 			spin_unlock_irqrestore(&cm_node->retrans_list_lock,
1035 					       flags);
1036 			ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
1037 			spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1038 			if (ret != NETDEV_TX_OK) {
1039 				nes_debug(NES_DBG_CM, "rexmit failed for "
1040 					  "node=%p\n", cm_node);
1041 				cm_packets_bounced++;
1042 				send_entry->retrycount--;
1043 				nexttimeout = jiffies + NES_SHORT_TIME;
1044 				settimer = 1;
1045 				break;
1046 			} else {
1047 				cm_packets_sent++;
1048 			}
1049 			nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
1050 				  "%u, retry count = %u.\n",
1051 				  send_entry->retranscount,
1052 				  send_entry->retrycount);
1053 			if (send_entry->send_retrans) {
1054 				send_entry->retranscount--;
1055 				timetosend = (NES_RETRY_TIMEOUT <<
1056 					      (NES_DEFAULT_RETRANS - send_entry->retranscount));
1057 
1058 				send_entry->timetosend = jiffies +
1059 							 min(timetosend, NES_MAX_TIMEOUT);
1060 				if (nexttimeout > send_entry->timetosend ||
1061 				    !settimer) {
1062 					nexttimeout = send_entry->timetosend;
1063 					settimer = 1;
1064 				}
1065 			} else {
1066 				int close_when_complete;
1067 				close_when_complete =
1068 					send_entry->close_when_complete;
1069 				nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
1070 					  cm_node, cm_node->state);
1071 				free_retrans_entry(cm_node);
1072 				if (close_when_complete)
1073 					rem_ref_cm_node(cm_node->cm_core,
1074 							cm_node);
1075 			}
1076 		} while (0);
1077 
1078 		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1079 		rem_ref_cm_node(cm_node->cm_core, cm_node);
1080 	}
1081 
1082 	if (settimer) {
1083 		if (!timer_pending(&cm_core->tcp_timer))
1084 			mod_timer(&cm_core->tcp_timer, nexttimeout);
1085 	}
1086 }
1087 
1088 
1089 /**
1090  * send_syn
1091  */
send_syn(struct nes_cm_node * cm_node,u32 sendack,struct sk_buff * skb)1092 static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
1093 		    struct sk_buff *skb)
1094 {
1095 	int ret;
1096 	int flags = SET_SYN;
1097 	char optionsbuffer[sizeof(struct option_mss) +
1098 			   sizeof(struct option_windowscale) + sizeof(struct option_base) +
1099 			   TCP_OPTIONS_PADDING];
1100 
1101 	int optionssize = 0;
1102 	/* Sending MSS option */
1103 	union all_known_options *options;
1104 
1105 	if (!cm_node)
1106 		return -EINVAL;
1107 
1108 	options = (union all_known_options *)&optionsbuffer[optionssize];
1109 	options->as_mss.optionnum = OPTION_NUMBER_MSS;
1110 	options->as_mss.length = sizeof(struct option_mss);
1111 	options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1112 	optionssize += sizeof(struct option_mss);
1113 
1114 	options = (union all_known_options *)&optionsbuffer[optionssize];
1115 	options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1116 	options->as_windowscale.length = sizeof(struct option_windowscale);
1117 	options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1118 	optionssize += sizeof(struct option_windowscale);
1119 
1120 	if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
1121 		options = (union all_known_options *)&optionsbuffer[optionssize];
1122 		options->as_base.optionnum = OPTION_NUMBER_WRITE0;
1123 		options->as_base.length = sizeof(struct option_base);
1124 		optionssize += sizeof(struct option_base);
1125 		/* we need the size to be a multiple of 4 */
1126 		options = (union all_known_options *)&optionsbuffer[optionssize];
1127 		options->as_end = 1;
1128 		optionssize += 1;
1129 		options = (union all_known_options *)&optionsbuffer[optionssize];
1130 		options->as_end = 1;
1131 		optionssize += 1;
1132 	}
1133 
1134 	options = (union all_known_options *)&optionsbuffer[optionssize];
1135 	options->as_end = OPTION_NUMBER_END;
1136 	optionssize += 1;
1137 
1138 	if (!skb)
1139 		skb = dev_alloc_skb(MAX_CM_BUFFER);
1140 	if (!skb) {
1141 		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1142 		return -1;
1143 	}
1144 
1145 	if (sendack)
1146 		flags |= SET_ACK;
1147 
1148 	form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1149 	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1150 
1151 	return ret;
1152 }
1153 
1154 
1155 /**
1156  * send_reset
1157  */
send_reset(struct nes_cm_node * cm_node,struct sk_buff * skb)1158 static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
1159 {
1160 	int ret;
1161 	int flags = SET_RST | SET_ACK;
1162 
1163 	if (!skb)
1164 		skb = dev_alloc_skb(MAX_CM_BUFFER);
1165 	if (!skb) {
1166 		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1167 		return -ENOMEM;
1168 	}
1169 
1170 	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1171 	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1172 
1173 	return ret;
1174 }
1175 
1176 
1177 /**
1178  * send_ack
1179  */
send_ack(struct nes_cm_node * cm_node,struct sk_buff * skb)1180 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
1181 {
1182 	int ret;
1183 
1184 	if (!skb)
1185 		skb = dev_alloc_skb(MAX_CM_BUFFER);
1186 
1187 	if (!skb) {
1188 		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1189 		return -1;
1190 	}
1191 
1192 	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1193 	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1194 
1195 	return ret;
1196 }
1197 
1198 
1199 /**
1200  * send_fin
1201  */
send_fin(struct nes_cm_node * cm_node,struct sk_buff * skb)1202 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
1203 {
1204 	int ret;
1205 
1206 	/* if we didn't get a frame get one */
1207 	if (!skb)
1208 		skb = dev_alloc_skb(MAX_CM_BUFFER);
1209 
1210 	if (!skb) {
1211 		nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1212 		return -1;
1213 	}
1214 
1215 	form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1216 	ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1217 
1218 	return ret;
1219 }
1220 
1221 
1222 /**
1223  * find_node - find a cm node that matches the reference cm node
1224  */
find_node(struct nes_cm_core * cm_core,u16 rem_port,nes_addr_t rem_addr,u16 loc_port,nes_addr_t loc_addr)1225 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
1226 				     u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
1227 {
1228 	unsigned long flags;
1229 	struct list_head *hte;
1230 	struct nes_cm_node *cm_node;
1231 
1232 	/* get a handle on the hte */
1233 	hte = &cm_core->connected_nodes;
1234 
1235 	/* walk list and find cm_node associated with this session ID */
1236 	spin_lock_irqsave(&cm_core->ht_lock, flags);
1237 	list_for_each_entry(cm_node, hte, list) {
1238 		/* compare quad, return node handle if a match */
1239 		nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
1240 			  cm_node->loc_addr, cm_node->loc_port,
1241 			  loc_addr, loc_port,
1242 			  cm_node->rem_addr, cm_node->rem_port,
1243 			  rem_addr, rem_port);
1244 		if ((cm_node->mapped_loc_addr == loc_addr) &&
1245 			(cm_node->mapped_loc_port == loc_port) &&
1246 			(cm_node->mapped_rem_addr == rem_addr) &&
1247 			(cm_node->mapped_rem_port == rem_port)) {
1248 
1249 			add_ref_cm_node(cm_node);
1250 			spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1251 			return cm_node;
1252 		}
1253 	}
1254 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1255 
1256 	/* no owner node */
1257 	return NULL;
1258 }
1259 
1260 
1261 /**
1262  * find_listener - find a cm node listening on this addr-port pair
1263  */
find_listener(struct nes_cm_core * cm_core,nes_addr_t dst_addr,u16 dst_port,enum nes_cm_listener_state listener_state,int local)1264 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
1265 					nes_addr_t dst_addr, u16 dst_port,
1266 					enum nes_cm_listener_state listener_state, int local)
1267 {
1268 	unsigned long flags;
1269 	struct nes_cm_listener *listen_node;
1270 	nes_addr_t listen_addr;
1271 	u16 listen_port;
1272 
1273 	/* walk list and find cm_node associated with this session ID */
1274 	spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1275 	list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
1276 		if (local) {
1277 			listen_addr = listen_node->loc_addr;
1278 			listen_port = listen_node->loc_port;
1279 		} else {
1280 			listen_addr = listen_node->mapped_loc_addr;
1281 			listen_port = listen_node->mapped_loc_port;
1282 		}
1283 		/* compare node pair, return node handle if a match */
1284 		if (((listen_addr == dst_addr) ||
1285 		     listen_addr == 0x00000000) &&
1286 		    (listen_port == dst_port) &&
1287 		    (listener_state & listen_node->listener_state)) {
1288 			atomic_inc(&listen_node->ref_count);
1289 			spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1290 			return listen_node;
1291 		}
1292 	}
1293 	spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1294 
1295 	/* no listener */
1296 	return NULL;
1297 }
1298 
1299 /**
1300  * add_hte_node - add a cm node to the hash table
1301  */
add_hte_node(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)1302 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1303 {
1304 	unsigned long flags;
1305 	struct list_head *hte;
1306 
1307 	if (!cm_node || !cm_core)
1308 		return -EINVAL;
1309 
1310 	nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
1311 		  cm_node);
1312 
1313 	spin_lock_irqsave(&cm_core->ht_lock, flags);
1314 
1315 	/* get a handle on the hash table element (list head for this slot) */
1316 	hte = &cm_core->connected_nodes;
1317 	list_add_tail(&cm_node->list, hte);
1318 	atomic_inc(&cm_core->ht_node_cnt);
1319 
1320 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1321 
1322 	return 0;
1323 }
1324 
1325 
1326 /**
1327  * mini_cm_dec_refcnt_listen
1328  */
mini_cm_dec_refcnt_listen(struct nes_cm_core * cm_core,struct nes_cm_listener * listener,int free_hanging_nodes)1329 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
1330 				     struct nes_cm_listener *listener, int free_hanging_nodes)
1331 {
1332 	int ret = -EINVAL;
1333 	int err = 0;
1334 	unsigned long flags;
1335 	struct list_head *list_pos = NULL;
1336 	struct list_head *list_temp = NULL;
1337 	struct nes_cm_node *cm_node = NULL;
1338 	struct list_head reset_list;
1339 
1340 	nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
1341 		  "refcnt=%d\n", listener, free_hanging_nodes,
1342 		  atomic_read(&listener->ref_count));
1343 	/* free non-accelerated child nodes for this listener */
1344 	INIT_LIST_HEAD(&reset_list);
1345 	if (free_hanging_nodes) {
1346 		spin_lock_irqsave(&cm_core->ht_lock, flags);
1347 		list_for_each_safe(list_pos, list_temp,
1348 				   &g_cm_core->connected_nodes) {
1349 			cm_node = container_of(list_pos, struct nes_cm_node,
1350 					       list);
1351 			if ((cm_node->listener == listener) &&
1352 			    (!cm_node->accelerated)) {
1353 				add_ref_cm_node(cm_node);
1354 				list_add(&cm_node->reset_entry, &reset_list);
1355 			}
1356 		}
1357 		spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1358 	}
1359 
1360 	list_for_each_safe(list_pos, list_temp, &reset_list) {
1361 		cm_node = container_of(list_pos, struct nes_cm_node,
1362 				       reset_entry);
1363 		{
1364 			struct nes_cm_node *loopback = cm_node->loopbackpartner;
1365 			enum nes_cm_node_state old_state;
1366 			if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1367 				rem_ref_cm_node(cm_node->cm_core, cm_node);
1368 			} else {
1369 				if (!loopback) {
1370 					cleanup_retrans_entry(cm_node);
1371 					err = send_reset(cm_node, NULL);
1372 					if (err) {
1373 						cm_node->state =
1374 							NES_CM_STATE_CLOSED;
1375 						WARN_ON(1);
1376 					} else {
1377 						old_state = cm_node->state;
1378 						cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1379 						if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1380 							rem_ref_cm_node(
1381 								cm_node->cm_core,
1382 								cm_node);
1383 					}
1384 				} else {
1385 					struct nes_cm_event event;
1386 
1387 					event.cm_node = loopback;
1388 					event.cm_info.rem_addr =
1389 							loopback->rem_addr;
1390 					event.cm_info.loc_addr =
1391 							loopback->loc_addr;
1392 					event.cm_info.rem_port =
1393 							loopback->rem_port;
1394 					event.cm_info.loc_port =
1395 							 loopback->loc_port;
1396 					event.cm_info.cm_id = loopback->cm_id;
1397 					add_ref_cm_node(loopback);
1398 					loopback->state = NES_CM_STATE_CLOSED;
1399 					cm_event_connect_error(&event);
1400 					cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1401 
1402 					rem_ref_cm_node(cm_node->cm_core,
1403 							 cm_node);
1404 
1405 				}
1406 			}
1407 		}
1408 	}
1409 
1410 	spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1411 	if (!atomic_dec_return(&listener->ref_count)) {
1412 		list_del(&listener->list);
1413 
1414 		/* decrement our listen node count */
1415 		atomic_dec(&cm_core->listen_node_cnt);
1416 
1417 		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1418 
1419 		if (listener->nesvnic) {
1420 			nes_manage_apbvt(listener->nesvnic,
1421 				listener->mapped_loc_port,
1422 				PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn),
1423 				NES_MANAGE_APBVT_DEL);
1424 
1425 			nes_remove_mapinfo(listener->loc_addr,
1426 					listener->loc_port,
1427 					listener->mapped_loc_addr,
1428 					listener->mapped_loc_port);
1429 			nes_debug(NES_DBG_NLMSG,
1430 					"Delete APBVT mapped_loc_port = %04X\n",
1431 					listener->mapped_loc_port);
1432 		}
1433 
1434 		nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1435 
1436 		kfree(listener);
1437 		listener = NULL;
1438 		ret = 0;
1439 		atomic_inc(&cm_listens_destroyed);
1440 	} else {
1441 		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1442 	}
1443 	if (listener) {
1444 		if (atomic_read(&listener->pend_accepts_cnt) > 0)
1445 			nes_debug(NES_DBG_CM, "destroying listener (%p)"
1446 				  " with non-zero pending accepts=%u\n",
1447 				  listener, atomic_read(&listener->pend_accepts_cnt));
1448 	}
1449 
1450 	return ret;
1451 }
1452 
1453 
1454 /**
1455  * mini_cm_del_listen
1456  */
mini_cm_del_listen(struct nes_cm_core * cm_core,struct nes_cm_listener * listener)1457 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1458 			      struct nes_cm_listener *listener)
1459 {
1460 	listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1461 	listener->cm_id = NULL; /* going to be destroyed pretty soon */
1462 	return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1463 }
1464 
1465 
1466 /**
1467  * mini_cm_accelerated
1468  */
mini_cm_accelerated(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)1469 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1470 				      struct nes_cm_node *cm_node)
1471 {
1472 	cm_node->accelerated = 1;
1473 
1474 	if (cm_node->accept_pend) {
1475 		BUG_ON(!cm_node->listener);
1476 		atomic_dec(&cm_node->listener->pend_accepts_cnt);
1477 		cm_node->accept_pend = 0;
1478 		BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1479 	}
1480 
1481 	if (!timer_pending(&cm_core->tcp_timer))
1482 		mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
1483 
1484 	return 0;
1485 }
1486 
1487 
1488 /**
1489  * nes_addr_resolve_neigh
1490  */
nes_addr_resolve_neigh(struct nes_vnic * nesvnic,u32 dst_ip,int arpindex)1491 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
1492 {
1493 	struct rtable *rt;
1494 	struct neighbour *neigh;
1495 	int rc = arpindex;
1496 	struct net_device *netdev;
1497 	struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1498 
1499 	rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
1500 	if (IS_ERR(rt)) {
1501 		printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1502 		       __func__, dst_ip);
1503 		return rc;
1504 	}
1505 
1506 	if (netif_is_bond_slave(nesvnic->netdev))
1507 		netdev = netdev_master_upper_dev_get(nesvnic->netdev);
1508 	else
1509 		netdev = nesvnic->netdev;
1510 
1511 	neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
1512 
1513 	rcu_read_lock();
1514 	if (neigh) {
1515 		if (neigh->nud_state & NUD_VALID) {
1516 			nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1517 				  " is %pM, Gateway is 0x%08X \n", dst_ip,
1518 				  neigh->ha, ntohl(rt->rt_gateway));
1519 
1520 			if (arpindex >= 0) {
1521 				if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
1522 					/* Mac address same as in nes_arp_table */
1523 					goto out;
1524 				}
1525 
1526 				nes_manage_arp_cache(nesvnic->netdev,
1527 						     nesadapter->arp_table[arpindex].mac_addr,
1528 						     dst_ip, NES_ARP_DELETE);
1529 			}
1530 
1531 			nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1532 					     dst_ip, NES_ARP_ADD);
1533 			rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1534 					   NES_ARP_RESOLVE);
1535 		} else {
1536 			neigh_event_send(neigh, NULL);
1537 		}
1538 	}
1539 out:
1540 	rcu_read_unlock();
1541 
1542 	if (neigh)
1543 		neigh_release(neigh);
1544 
1545 	ip_rt_put(rt);
1546 	return rc;
1547 }
1548 
1549 /**
1550  * make_cm_node - create a new instance of a cm node
1551  */
make_cm_node(struct nes_cm_core * cm_core,struct nes_vnic * nesvnic,struct nes_cm_info * cm_info,struct nes_cm_listener * listener)1552 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1553 					struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1554 					struct nes_cm_listener *listener)
1555 {
1556 	struct nes_cm_node *cm_node;
1557 	struct timespec ts;
1558 	int oldarpindex = 0;
1559 	int arpindex = 0;
1560 	struct nes_device *nesdev;
1561 	struct nes_adapter *nesadapter;
1562 
1563 	/* create an hte and cm_node for this instance */
1564 	cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1565 	if (!cm_node)
1566 		return NULL;
1567 
1568 	/* set our node specific transport info */
1569 	cm_node->loc_addr = cm_info->loc_addr;
1570 	cm_node->rem_addr = cm_info->rem_addr;
1571 	cm_node->loc_port = cm_info->loc_port;
1572 	cm_node->rem_port = cm_info->rem_port;
1573 
1574 	cm_node->mapped_loc_addr = cm_info->mapped_loc_addr;
1575 	cm_node->mapped_rem_addr = cm_info->mapped_rem_addr;
1576 	cm_node->mapped_loc_port = cm_info->mapped_loc_port;
1577 	cm_node->mapped_rem_port = cm_info->mapped_rem_port;
1578 
1579 	cm_node->mpa_frame_rev = mpa_version;
1580 	cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1581 	cm_node->mpav2_ird_ord = 0;
1582 	cm_node->ird_size = 0;
1583 	cm_node->ord_size = 0;
1584 
1585 	nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1586 		  &cm_node->loc_addr, cm_node->loc_port,
1587 		  &cm_node->rem_addr, cm_node->rem_port);
1588 	cm_node->listener = listener;
1589 	cm_node->netdev = nesvnic->netdev;
1590 	cm_node->cm_id = cm_info->cm_id;
1591 	memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1592 
1593 	nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1594 		  cm_node->cm_id);
1595 
1596 	spin_lock_init(&cm_node->retrans_list_lock);
1597 
1598 	cm_node->loopbackpartner = NULL;
1599 	atomic_set(&cm_node->ref_count, 1);
1600 	/* associate our parent CM core */
1601 	cm_node->cm_core = cm_core;
1602 	cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1603 	cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1604 	cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1605 				     NES_CM_DEFAULT_RCV_WND_SCALE;
1606 	ts = current_kernel_time();
1607 	cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1608 	cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1609 				 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1610 	cm_node->tcp_cntxt.rcv_nxt = 0;
1611 	/* get a unique session ID , add thread_id to an upcounter to handle race */
1612 	atomic_inc(&cm_core->node_cnt);
1613 	cm_node->conn_type = cm_info->conn_type;
1614 	cm_node->apbvt_set = 0;
1615 	cm_node->accept_pend = 0;
1616 
1617 	cm_node->nesvnic = nesvnic;
1618 	/* get some device handles, for arp lookup */
1619 	nesdev = nesvnic->nesdev;
1620 	nesadapter = nesdev->nesadapter;
1621 
1622 	cm_node->loopbackpartner = NULL;
1623 
1624 	/* get the mac addr for the remote node */
1625 	oldarpindex = nes_arp_table(nesdev, cm_node->mapped_rem_addr,
1626 				NULL, NES_ARP_RESOLVE);
1627 	arpindex = nes_addr_resolve_neigh(nesvnic,
1628 				cm_node->mapped_rem_addr, oldarpindex);
1629 	if (arpindex < 0) {
1630 		kfree(cm_node);
1631 		return NULL;
1632 	}
1633 
1634 	/* copy the mac addr to node context */
1635 	memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1636 	nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1637 		  cm_node->rem_mac);
1638 
1639 	add_hte_node(cm_core, cm_node);
1640 	atomic_inc(&cm_nodes_created);
1641 
1642 	return cm_node;
1643 }
1644 
1645 
1646 /**
1647  * add_ref_cm_node - destroy an instance of a cm node
1648  */
add_ref_cm_node(struct nes_cm_node * cm_node)1649 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1650 {
1651 	atomic_inc(&cm_node->ref_count);
1652 	return 0;
1653 }
1654 
1655 
1656 /**
1657  * rem_ref_cm_node - destroy an instance of a cm node
1658  */
rem_ref_cm_node(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)1659 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1660 			   struct nes_cm_node *cm_node)
1661 {
1662 	unsigned long flags;
1663 	struct nes_qp *nesqp;
1664 
1665 	if (!cm_node)
1666 		return -EINVAL;
1667 
1668 	spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1669 	if (atomic_dec_return(&cm_node->ref_count)) {
1670 		spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1671 		return 0;
1672 	}
1673 	list_del(&cm_node->list);
1674 	atomic_dec(&cm_core->ht_node_cnt);
1675 	spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1676 
1677 	/* if the node is destroyed before connection was accelerated */
1678 	if (!cm_node->accelerated && cm_node->accept_pend) {
1679 		BUG_ON(!cm_node->listener);
1680 		atomic_dec(&cm_node->listener->pend_accepts_cnt);
1681 		BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1682 	}
1683 	WARN_ON(cm_node->send_entry);
1684 	if (cm_node->recv_entry)
1685 		handle_recv_entry(cm_node, 0);
1686 	if (cm_node->listener) {
1687 		mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1688 	} else {
1689 		if (cm_node->apbvt_set && cm_node->nesvnic) {
1690 			nes_manage_apbvt(cm_node->nesvnic, cm_node->mapped_loc_port,
1691 					 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
1692 					 NES_MANAGE_APBVT_DEL);
1693 		}
1694 		nes_debug(NES_DBG_NLMSG, "Delete APBVT mapped_loc_port = %04X\n",
1695 					cm_node->mapped_loc_port);
1696 		nes_remove_mapinfo(cm_node->loc_addr, cm_node->loc_port,
1697 			cm_node->mapped_loc_addr, cm_node->mapped_loc_port);
1698 	}
1699 
1700 	atomic_dec(&cm_core->node_cnt);
1701 	atomic_inc(&cm_nodes_destroyed);
1702 	nesqp = cm_node->nesqp;
1703 	if (nesqp) {
1704 		nesqp->cm_node = NULL;
1705 		nes_rem_ref(&nesqp->ibqp);
1706 		cm_node->nesqp = NULL;
1707 	}
1708 
1709 	kfree(cm_node);
1710 	return 0;
1711 }
1712 
1713 /**
1714  * process_options
1715  */
process_options(struct nes_cm_node * cm_node,u8 * optionsloc,u32 optionsize,u32 syn_packet)1716 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1717 			   u32 optionsize, u32 syn_packet)
1718 {
1719 	u32 tmp;
1720 	u32 offset = 0;
1721 	union all_known_options *all_options;
1722 	char got_mss_option = 0;
1723 
1724 	while (offset < optionsize) {
1725 		all_options = (union all_known_options *)(optionsloc + offset);
1726 		switch (all_options->as_base.optionnum) {
1727 		case OPTION_NUMBER_END:
1728 			offset = optionsize;
1729 			break;
1730 		case OPTION_NUMBER_NONE:
1731 			offset += 1;
1732 			continue;
1733 		case OPTION_NUMBER_MSS:
1734 			nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1735 				  "Size: %d\n", __func__,
1736 				  all_options->as_mss.length, offset, optionsize);
1737 			got_mss_option = 1;
1738 			if (all_options->as_mss.length != 4) {
1739 				return 1;
1740 			} else {
1741 				tmp = ntohs(all_options->as_mss.mss);
1742 				if (tmp > 0 && tmp <
1743 				    cm_node->tcp_cntxt.mss)
1744 					cm_node->tcp_cntxt.mss = tmp;
1745 			}
1746 			break;
1747 		case OPTION_NUMBER_WINDOW_SCALE:
1748 			cm_node->tcp_cntxt.snd_wscale =
1749 				all_options->as_windowscale.shiftcount;
1750 			break;
1751 		default:
1752 			nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1753 				  all_options->as_base.optionnum);
1754 			break;
1755 		}
1756 		offset += all_options->as_base.length;
1757 	}
1758 	if ((!got_mss_option) && (syn_packet))
1759 		cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1760 	return 0;
1761 }
1762 
drop_packet(struct sk_buff * skb)1763 static void drop_packet(struct sk_buff *skb)
1764 {
1765 	atomic_inc(&cm_accel_dropped_pkts);
1766 	dev_kfree_skb_any(skb);
1767 }
1768 
handle_fin_pkt(struct nes_cm_node * cm_node)1769 static void handle_fin_pkt(struct nes_cm_node *cm_node)
1770 {
1771 	nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1772 		  "refcnt=%d\n", cm_node, cm_node->state,
1773 		  atomic_read(&cm_node->ref_count));
1774 	switch (cm_node->state) {
1775 	case NES_CM_STATE_SYN_RCVD:
1776 	case NES_CM_STATE_SYN_SENT:
1777 	case NES_CM_STATE_ESTABLISHED:
1778 	case NES_CM_STATE_MPAREJ_RCVD:
1779 		cm_node->tcp_cntxt.rcv_nxt++;
1780 		cleanup_retrans_entry(cm_node);
1781 		cm_node->state = NES_CM_STATE_LAST_ACK;
1782 		send_fin(cm_node, NULL);
1783 		break;
1784 	case NES_CM_STATE_MPAREQ_SENT:
1785 		create_event(cm_node, NES_CM_EVENT_ABORTED);
1786 		cm_node->tcp_cntxt.rcv_nxt++;
1787 		cleanup_retrans_entry(cm_node);
1788 		cm_node->state = NES_CM_STATE_CLOSED;
1789 		add_ref_cm_node(cm_node);
1790 		send_reset(cm_node, NULL);
1791 		break;
1792 	case NES_CM_STATE_FIN_WAIT1:
1793 		cm_node->tcp_cntxt.rcv_nxt++;
1794 		cleanup_retrans_entry(cm_node);
1795 		cm_node->state = NES_CM_STATE_CLOSING;
1796 		send_ack(cm_node, NULL);
1797 		/* Wait for ACK as this is simultaneous close..
1798 		* After we receive ACK, do not send anything..
1799 		* Just rm the node.. Done.. */
1800 		break;
1801 	case NES_CM_STATE_FIN_WAIT2:
1802 		cm_node->tcp_cntxt.rcv_nxt++;
1803 		cleanup_retrans_entry(cm_node);
1804 		cm_node->state = NES_CM_STATE_TIME_WAIT;
1805 		send_ack(cm_node, NULL);
1806 		schedule_nes_timer(cm_node, NULL,  NES_TIMER_TYPE_CLOSE, 1, 0);
1807 		break;
1808 	case NES_CM_STATE_TIME_WAIT:
1809 		cm_node->tcp_cntxt.rcv_nxt++;
1810 		cleanup_retrans_entry(cm_node);
1811 		cm_node->state = NES_CM_STATE_CLOSED;
1812 		rem_ref_cm_node(cm_node->cm_core, cm_node);
1813 		break;
1814 	case NES_CM_STATE_TSA:
1815 	default:
1816 		nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1817 			cm_node, cm_node->state);
1818 		break;
1819 	}
1820 }
1821 
1822 
handle_rst_pkt(struct nes_cm_node * cm_node,struct sk_buff * skb,struct tcphdr * tcph)1823 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1824 	struct tcphdr *tcph)
1825 {
1826 
1827 	int	reset = 0;	/* whether to send reset in case of err.. */
1828 	atomic_inc(&cm_resets_recvd);
1829 	nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1830 			" refcnt=%d\n", cm_node, cm_node->state,
1831 			atomic_read(&cm_node->ref_count));
1832 	cleanup_retrans_entry(cm_node);
1833 	switch (cm_node->state) {
1834 	case NES_CM_STATE_SYN_SENT:
1835 	case NES_CM_STATE_MPAREQ_SENT:
1836 		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1837 			"listener=%p state=%d\n", __func__, __LINE__, cm_node,
1838 			cm_node->listener, cm_node->state);
1839 		switch (cm_node->mpa_frame_rev) {
1840 		case IETF_MPA_V2:
1841 			cm_node->mpa_frame_rev = IETF_MPA_V1;
1842 			/* send a syn and goto syn sent state */
1843 			cm_node->state = NES_CM_STATE_SYN_SENT;
1844 			if (send_syn(cm_node, 0, NULL)) {
1845 				active_open_err(cm_node, skb, reset);
1846 			}
1847 			break;
1848 		case IETF_MPA_V1:
1849 		default:
1850 			active_open_err(cm_node, skb, reset);
1851 			break;
1852 		}
1853 		break;
1854 	case NES_CM_STATE_MPAREQ_RCVD:
1855 		atomic_inc(&cm_node->passive_state);
1856 		dev_kfree_skb_any(skb);
1857 		break;
1858 	case NES_CM_STATE_ESTABLISHED:
1859 	case NES_CM_STATE_SYN_RCVD:
1860 	case NES_CM_STATE_LISTENING:
1861 		nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1862 		passive_open_err(cm_node, skb, reset);
1863 		break;
1864 	case NES_CM_STATE_TSA:
1865 		active_open_err(cm_node, skb, reset);
1866 		break;
1867 	case NES_CM_STATE_CLOSED:
1868 		drop_packet(skb);
1869 		break;
1870 	case NES_CM_STATE_FIN_WAIT2:
1871 	case NES_CM_STATE_FIN_WAIT1:
1872 	case NES_CM_STATE_LAST_ACK:
1873 		cm_node->cm_id->rem_ref(cm_node->cm_id);
1874 	case NES_CM_STATE_TIME_WAIT:
1875 		cm_node->state = NES_CM_STATE_CLOSED;
1876 		rem_ref_cm_node(cm_node->cm_core, cm_node);
1877 		drop_packet(skb);
1878 		break;
1879 	default:
1880 		drop_packet(skb);
1881 		break;
1882 	}
1883 }
1884 
1885 
handle_rcv_mpa(struct nes_cm_node * cm_node,struct sk_buff * skb)1886 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1887 {
1888 	int ret = 0;
1889 	int datasize = skb->len;
1890 	u8 *dataloc = skb->data;
1891 
1892 	enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1893 	u32 res_type;
1894 
1895 	ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1896 	if (ret) {
1897 		nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1898 		if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1899 			nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1900 				  "cm_node=%p listener=%p state=%d\n", __func__,
1901 				  __LINE__, cm_node, cm_node->listener,
1902 				  cm_node->state);
1903 			active_open_err(cm_node, skb, 1);
1904 		} else {
1905 			passive_open_err(cm_node, skb, 1);
1906 		}
1907 		return;
1908 	}
1909 
1910 	switch (cm_node->state) {
1911 	case NES_CM_STATE_ESTABLISHED:
1912 		if (res_type == NES_MPA_REQUEST_REJECT)
1913 			/*BIG problem as we are receiving the MPA.. So should
1914 			 * not be REJECT.. This is Passive Open.. We can
1915 			 * only receive it Reject for Active Open...*/
1916 			WARN_ON(1);
1917 		cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1918 		type = NES_CM_EVENT_MPA_REQ;
1919 		atomic_set(&cm_node->passive_state,
1920 			   NES_PASSIVE_STATE_INDICATED);
1921 		break;
1922 	case NES_CM_STATE_MPAREQ_SENT:
1923 		cleanup_retrans_entry(cm_node);
1924 		if (res_type == NES_MPA_REQUEST_REJECT) {
1925 			type = NES_CM_EVENT_MPA_REJECT;
1926 			cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1927 		} else {
1928 			type = NES_CM_EVENT_CONNECTED;
1929 			cm_node->state = NES_CM_STATE_TSA;
1930 		}
1931 
1932 		break;
1933 	default:
1934 		WARN_ON(1);
1935 		break;
1936 	}
1937 	dev_kfree_skb_any(skb);
1938 	create_event(cm_node, type);
1939 }
1940 
indicate_pkt_err(struct nes_cm_node * cm_node,struct sk_buff * skb)1941 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1942 {
1943 	switch (cm_node->state) {
1944 	case NES_CM_STATE_SYN_SENT:
1945 	case NES_CM_STATE_MPAREQ_SENT:
1946 		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1947 			  "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1948 			  cm_node->listener, cm_node->state);
1949 		active_open_err(cm_node, skb, 1);
1950 		break;
1951 	case NES_CM_STATE_ESTABLISHED:
1952 	case NES_CM_STATE_SYN_RCVD:
1953 		passive_open_err(cm_node, skb, 1);
1954 		break;
1955 	case NES_CM_STATE_TSA:
1956 	default:
1957 		drop_packet(skb);
1958 	}
1959 }
1960 
check_syn(struct nes_cm_node * cm_node,struct tcphdr * tcph,struct sk_buff * skb)1961 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1962 		     struct sk_buff *skb)
1963 {
1964 	int err;
1965 
1966 	err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
1967 	if (err)
1968 		active_open_err(cm_node, skb, 1);
1969 
1970 	return err;
1971 }
1972 
check_seq(struct nes_cm_node * cm_node,struct tcphdr * tcph,struct sk_buff * skb)1973 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1974 		     struct sk_buff *skb)
1975 {
1976 	int err = 0;
1977 	u32 seq;
1978 	u32 ack_seq;
1979 	u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1980 	u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1981 	u32 rcv_wnd;
1982 
1983 	seq = ntohl(tcph->seq);
1984 	ack_seq = ntohl(tcph->ack_seq);
1985 	rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1986 	if (ack_seq != loc_seq_num)
1987 		err = 1;
1988 	else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
1989 		err = 1;
1990 	if (err) {
1991 		nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1992 			  "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1993 			  cm_node->listener, cm_node->state);
1994 		indicate_pkt_err(cm_node, skb);
1995 		nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1996 			  "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1997 			  rcv_wnd);
1998 	}
1999 	return err;
2000 }
2001 
2002 /*
2003  * handle_syn_pkt() is for Passive node. The syn packet is received when a node
2004  * is created with a listener or it may comein as rexmitted packet which in
2005  * that case will be just dropped.
2006  */
handle_syn_pkt(struct nes_cm_node * cm_node,struct sk_buff * skb,struct tcphdr * tcph)2007 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
2008 			   struct tcphdr *tcph)
2009 {
2010 	int ret;
2011 	u32 inc_sequence;
2012 	int optionsize;
2013 
2014 	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2015 	skb_trim(skb, 0);
2016 	inc_sequence = ntohl(tcph->seq);
2017 
2018 	switch (cm_node->state) {
2019 	case NES_CM_STATE_SYN_SENT:
2020 	case NES_CM_STATE_MPAREQ_SENT:
2021 		/* Rcvd syn on active open connection*/
2022 		active_open_err(cm_node, skb, 1);
2023 		break;
2024 	case NES_CM_STATE_LISTENING:
2025 		/* Passive OPEN */
2026 		if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
2027 		    cm_node->listener->backlog) {
2028 			nes_debug(NES_DBG_CM, "drop syn due to backlog "
2029 				  "pressure \n");
2030 			cm_backlog_drops++;
2031 			passive_open_err(cm_node, skb, 0);
2032 			break;
2033 		}
2034 		ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
2035 					 1);
2036 		if (ret) {
2037 			passive_open_err(cm_node, skb, 0);
2038 			/* drop pkt */
2039 			break;
2040 		}
2041 		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2042 		BUG_ON(cm_node->send_entry);
2043 		cm_node->accept_pend = 1;
2044 		atomic_inc(&cm_node->listener->pend_accepts_cnt);
2045 
2046 		cm_node->state = NES_CM_STATE_SYN_RCVD;
2047 		send_syn(cm_node, 1, skb);
2048 		break;
2049 	case NES_CM_STATE_CLOSED:
2050 		cleanup_retrans_entry(cm_node);
2051 		add_ref_cm_node(cm_node);
2052 		send_reset(cm_node, skb);
2053 		break;
2054 	case NES_CM_STATE_TSA:
2055 	case NES_CM_STATE_ESTABLISHED:
2056 	case NES_CM_STATE_FIN_WAIT1:
2057 	case NES_CM_STATE_FIN_WAIT2:
2058 	case NES_CM_STATE_MPAREQ_RCVD:
2059 	case NES_CM_STATE_LAST_ACK:
2060 	case NES_CM_STATE_CLOSING:
2061 	case NES_CM_STATE_UNKNOWN:
2062 	default:
2063 		drop_packet(skb);
2064 		break;
2065 	}
2066 }
2067 
handle_synack_pkt(struct nes_cm_node * cm_node,struct sk_buff * skb,struct tcphdr * tcph)2068 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
2069 			      struct tcphdr *tcph)
2070 {
2071 	int ret;
2072 	u32 inc_sequence;
2073 	int optionsize;
2074 
2075 	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2076 	skb_trim(skb, 0);
2077 	inc_sequence = ntohl(tcph->seq);
2078 	switch (cm_node->state) {
2079 	case NES_CM_STATE_SYN_SENT:
2080 		cleanup_retrans_entry(cm_node);
2081 		/* active open */
2082 		if (check_syn(cm_node, tcph, skb))
2083 			return;
2084 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2085 		/* setup options */
2086 		ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
2087 		if (ret) {
2088 			nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
2089 				  cm_node);
2090 			break;
2091 		}
2092 		cleanup_retrans_entry(cm_node);
2093 		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2094 		send_mpa_request(cm_node, skb);
2095 		cm_node->state = NES_CM_STATE_MPAREQ_SENT;
2096 		break;
2097 	case NES_CM_STATE_MPAREQ_RCVD:
2098 		/* passive open, so should not be here */
2099 		passive_open_err(cm_node, skb, 1);
2100 		break;
2101 	case NES_CM_STATE_LISTENING:
2102 		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2103 		cleanup_retrans_entry(cm_node);
2104 		cm_node->state = NES_CM_STATE_CLOSED;
2105 		send_reset(cm_node, skb);
2106 		break;
2107 	case NES_CM_STATE_CLOSED:
2108 		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2109 		cleanup_retrans_entry(cm_node);
2110 		add_ref_cm_node(cm_node);
2111 		send_reset(cm_node, skb);
2112 		break;
2113 	case NES_CM_STATE_ESTABLISHED:
2114 	case NES_CM_STATE_FIN_WAIT1:
2115 	case NES_CM_STATE_FIN_WAIT2:
2116 	case NES_CM_STATE_LAST_ACK:
2117 	case NES_CM_STATE_TSA:
2118 	case NES_CM_STATE_CLOSING:
2119 	case NES_CM_STATE_UNKNOWN:
2120 	case NES_CM_STATE_MPAREQ_SENT:
2121 	default:
2122 		drop_packet(skb);
2123 		break;
2124 	}
2125 }
2126 
handle_ack_pkt(struct nes_cm_node * cm_node,struct sk_buff * skb,struct tcphdr * tcph)2127 static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
2128 			  struct tcphdr *tcph)
2129 {
2130 	int datasize = 0;
2131 	u32 inc_sequence;
2132 	int ret = 0;
2133 	int optionsize;
2134 
2135 	optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2136 
2137 	if (check_seq(cm_node, tcph, skb))
2138 		return -EINVAL;
2139 
2140 	skb_pull(skb, tcph->doff << 2);
2141 	inc_sequence = ntohl(tcph->seq);
2142 	datasize = skb->len;
2143 	switch (cm_node->state) {
2144 	case NES_CM_STATE_SYN_RCVD:
2145 		/* Passive OPEN */
2146 		cleanup_retrans_entry(cm_node);
2147 		ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
2148 		if (ret)
2149 			break;
2150 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2151 		cm_node->state = NES_CM_STATE_ESTABLISHED;
2152 		if (datasize) {
2153 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2154 			handle_rcv_mpa(cm_node, skb);
2155 		} else { /* rcvd ACK only */
2156 			dev_kfree_skb_any(skb);
2157 		}
2158 		break;
2159 	case NES_CM_STATE_ESTABLISHED:
2160 		/* Passive OPEN */
2161 		cleanup_retrans_entry(cm_node);
2162 		if (datasize) {
2163 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2164 			handle_rcv_mpa(cm_node, skb);
2165 		} else {
2166 			drop_packet(skb);
2167 		}
2168 		break;
2169 	case NES_CM_STATE_MPAREQ_SENT:
2170 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2171 		if (datasize) {
2172 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2173 			handle_rcv_mpa(cm_node, skb);
2174 		} else { /* Could be just an ack pkt.. */
2175 			dev_kfree_skb_any(skb);
2176 		}
2177 		break;
2178 	case NES_CM_STATE_LISTENING:
2179 		cleanup_retrans_entry(cm_node);
2180 		cm_node->state = NES_CM_STATE_CLOSED;
2181 		send_reset(cm_node, skb);
2182 		break;
2183 	case NES_CM_STATE_CLOSED:
2184 		cleanup_retrans_entry(cm_node);
2185 		add_ref_cm_node(cm_node);
2186 		send_reset(cm_node, skb);
2187 		break;
2188 	case NES_CM_STATE_LAST_ACK:
2189 	case NES_CM_STATE_CLOSING:
2190 		cleanup_retrans_entry(cm_node);
2191 		cm_node->state = NES_CM_STATE_CLOSED;
2192 		cm_node->cm_id->rem_ref(cm_node->cm_id);
2193 		rem_ref_cm_node(cm_node->cm_core, cm_node);
2194 		drop_packet(skb);
2195 		break;
2196 	case NES_CM_STATE_FIN_WAIT1:
2197 		cleanup_retrans_entry(cm_node);
2198 		drop_packet(skb);
2199 		cm_node->state = NES_CM_STATE_FIN_WAIT2;
2200 		break;
2201 	case NES_CM_STATE_SYN_SENT:
2202 	case NES_CM_STATE_FIN_WAIT2:
2203 	case NES_CM_STATE_TSA:
2204 	case NES_CM_STATE_MPAREQ_RCVD:
2205 	case NES_CM_STATE_UNKNOWN:
2206 	default:
2207 		cleanup_retrans_entry(cm_node);
2208 		drop_packet(skb);
2209 		break;
2210 	}
2211 	return ret;
2212 }
2213 
2214 
2215 
handle_tcp_options(struct nes_cm_node * cm_node,struct tcphdr * tcph,struct sk_buff * skb,int optionsize,int passive)2216 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
2217 			      struct sk_buff *skb, int optionsize, int passive)
2218 {
2219 	u8 *optionsloc = (u8 *)&tcph[1];
2220 
2221 	if (optionsize) {
2222 		if (process_options(cm_node, optionsloc, optionsize,
2223 				    (u32)tcph->syn)) {
2224 			nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
2225 				  __func__, cm_node);
2226 			if (passive)
2227 				passive_open_err(cm_node, skb, 1);
2228 			else
2229 				active_open_err(cm_node, skb, 1);
2230 			return 1;
2231 		}
2232 	}
2233 
2234 	cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
2235 				     cm_node->tcp_cntxt.snd_wscale;
2236 
2237 	if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
2238 		cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
2239 	return 0;
2240 }
2241 
2242 /*
2243  * active_open_err() will send reset() if flag set..
2244  * It will also send ABORT event.
2245  */
active_open_err(struct nes_cm_node * cm_node,struct sk_buff * skb,int reset)2246 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2247 			    int reset)
2248 {
2249 	cleanup_retrans_entry(cm_node);
2250 	if (reset) {
2251 		nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
2252 			  "state=%d\n", cm_node, cm_node->state);
2253 		add_ref_cm_node(cm_node);
2254 		send_reset(cm_node, skb);
2255 	} else {
2256 		dev_kfree_skb_any(skb);
2257 	}
2258 
2259 	cm_node->state = NES_CM_STATE_CLOSED;
2260 	create_event(cm_node, NES_CM_EVENT_ABORTED);
2261 }
2262 
2263 /*
2264  * passive_open_err() will either do a reset() or will free up the skb and
2265  * remove the cm_node.
2266  */
passive_open_err(struct nes_cm_node * cm_node,struct sk_buff * skb,int reset)2267 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2268 			     int reset)
2269 {
2270 	cleanup_retrans_entry(cm_node);
2271 	cm_node->state = NES_CM_STATE_CLOSED;
2272 	if (reset) {
2273 		nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
2274 			  "cm_node=%p state =%d\n", cm_node, cm_node->state);
2275 		send_reset(cm_node, skb);
2276 	} else {
2277 		dev_kfree_skb_any(skb);
2278 		rem_ref_cm_node(cm_node->cm_core, cm_node);
2279 	}
2280 }
2281 
2282 /*
2283  * free_retrans_entry() routines assumes that the retrans_list_lock has
2284  * been acquired before calling.
2285  */
free_retrans_entry(struct nes_cm_node * cm_node)2286 static void free_retrans_entry(struct nes_cm_node *cm_node)
2287 {
2288 	struct nes_timer_entry *send_entry;
2289 
2290 	send_entry = cm_node->send_entry;
2291 	if (send_entry) {
2292 		cm_node->send_entry = NULL;
2293 		dev_kfree_skb_any(send_entry->skb);
2294 		kfree(send_entry);
2295 		rem_ref_cm_node(cm_node->cm_core, cm_node);
2296 	}
2297 }
2298 
cleanup_retrans_entry(struct nes_cm_node * cm_node)2299 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2300 {
2301 	unsigned long flags;
2302 
2303 	spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2304 	free_retrans_entry(cm_node);
2305 	spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2306 }
2307 
2308 /**
2309  * process_packet
2310  * Returns skb if to be freed, else it will return NULL if already used..
2311  */
process_packet(struct nes_cm_node * cm_node,struct sk_buff * skb,struct nes_cm_core * cm_core)2312 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
2313 			   struct nes_cm_core *cm_core)
2314 {
2315 	enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
2316 	struct tcphdr *tcph = tcp_hdr(skb);
2317 	u32 fin_set = 0;
2318 	int ret = 0;
2319 
2320 	skb_pull(skb, ip_hdr(skb)->ihl << 2);
2321 
2322 	nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
2323 		  "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2324 		  tcph->ack, tcph->rst, tcph->fin);
2325 
2326 	if (tcph->rst) {
2327 		pkt_type = NES_PKT_TYPE_RST;
2328 	} else if (tcph->syn) {
2329 		pkt_type = NES_PKT_TYPE_SYN;
2330 		if (tcph->ack)
2331 			pkt_type = NES_PKT_TYPE_SYNACK;
2332 	} else if (tcph->ack) {
2333 		pkt_type = NES_PKT_TYPE_ACK;
2334 	}
2335 	if (tcph->fin)
2336 		fin_set = 1;
2337 
2338 	switch (pkt_type) {
2339 	case NES_PKT_TYPE_SYN:
2340 		handle_syn_pkt(cm_node, skb, tcph);
2341 		break;
2342 	case NES_PKT_TYPE_SYNACK:
2343 		handle_synack_pkt(cm_node, skb, tcph);
2344 		break;
2345 	case NES_PKT_TYPE_ACK:
2346 		ret = handle_ack_pkt(cm_node, skb, tcph);
2347 		if (fin_set && !ret)
2348 			handle_fin_pkt(cm_node);
2349 		break;
2350 	case NES_PKT_TYPE_RST:
2351 		handle_rst_pkt(cm_node, skb, tcph);
2352 		break;
2353 	default:
2354 		if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
2355 			handle_fin_pkt(cm_node);
2356 		drop_packet(skb);
2357 		break;
2358 	}
2359 }
2360 
2361 /**
2362  * mini_cm_listen - create a listen node with params
2363  */
mini_cm_listen(struct nes_cm_core * cm_core,struct nes_vnic * nesvnic,struct nes_cm_info * cm_info)2364 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
2365 			struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
2366 {
2367 	struct nes_cm_listener *listener;
2368 	struct iwpm_dev_data pm_reg_msg;
2369 	struct iwpm_sa_data pm_msg;
2370 	unsigned long flags;
2371 	int iwpm_err = 0;
2372 
2373 	nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
2374 		  cm_info->loc_addr, cm_info->loc_port);
2375 
2376 	/* cannot have multiple matching listeners */
2377 	listener = find_listener(cm_core, cm_info->loc_addr, cm_info->loc_port,
2378 				NES_CM_LISTENER_EITHER_STATE, 1);
2379 
2380 	if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2381 		/* find automatically incs ref count ??? */
2382 		atomic_dec(&listener->ref_count);
2383 		nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2384 		return NULL;
2385 	}
2386 
2387 	if (!listener) {
2388 		nes_form_reg_msg(nesvnic, &pm_reg_msg);
2389 		iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_NES);
2390 		if (iwpm_err) {
2391 			nes_debug(NES_DBG_NLMSG,
2392 			"Port Mapper reg pid fail (err = %d).\n", iwpm_err);
2393 		}
2394 		if (iwpm_valid_pid() && !iwpm_err) {
2395 			nes_form_pm_msg(cm_info, &pm_msg);
2396 			iwpm_err = iwpm_add_mapping(&pm_msg, RDMA_NL_NES);
2397 			if (iwpm_err)
2398 				nes_debug(NES_DBG_NLMSG,
2399 				"Port Mapper query fail (err = %d).\n", iwpm_err);
2400 			else
2401 				nes_record_pm_msg(cm_info, &pm_msg);
2402 		}
2403 
2404 		/* create a CM listen node (1/2 node to compare incoming traffic to) */
2405 		listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2406 		if (!listener) {
2407 			nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2408 			return NULL;
2409 		}
2410 
2411 		listener->loc_addr = cm_info->loc_addr;
2412 		listener->loc_port = cm_info->loc_port;
2413 		listener->mapped_loc_addr = cm_info->mapped_loc_addr;
2414 		listener->mapped_loc_port = cm_info->mapped_loc_port;
2415 		listener->reused_node = 0;
2416 
2417 		atomic_set(&listener->ref_count, 1);
2418 	}
2419 	/* pasive case */
2420 	/* find already inc'ed the ref count */
2421 	else {
2422 		listener->reused_node = 1;
2423 	}
2424 
2425 	listener->cm_id = cm_info->cm_id;
2426 	atomic_set(&listener->pend_accepts_cnt, 0);
2427 	listener->cm_core = cm_core;
2428 	listener->nesvnic = nesvnic;
2429 	atomic_inc(&cm_core->node_cnt);
2430 
2431 	listener->conn_type = cm_info->conn_type;
2432 	listener->backlog = cm_info->backlog;
2433 	listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2434 
2435 	if (!listener->reused_node) {
2436 		spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2437 		list_add(&listener->list, &cm_core->listen_list.list);
2438 		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2439 		atomic_inc(&cm_core->listen_node_cnt);
2440 	}
2441 
2442 	nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2443 		  " listener = %p, backlog = %d, cm_id = %p.\n",
2444 		  cm_info->loc_addr, cm_info->loc_port,
2445 		  listener, listener->backlog, listener->cm_id);
2446 
2447 	return listener;
2448 }
2449 
2450 
2451 /**
2452  * mini_cm_connect - make a connection node with params
2453  */
mini_cm_connect(struct nes_cm_core * cm_core,struct nes_vnic * nesvnic,u16 private_data_len,void * private_data,struct nes_cm_info * cm_info)2454 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
2455 					   struct nes_vnic *nesvnic, u16 private_data_len,
2456 					   void *private_data, struct nes_cm_info *cm_info)
2457 {
2458 	int ret = 0;
2459 	struct nes_cm_node *cm_node;
2460 	struct nes_cm_listener *loopbackremotelistener;
2461 	struct nes_cm_node *loopbackremotenode;
2462 	struct nes_cm_info loopback_cm_info;
2463 	u8 *start_buff;
2464 
2465 	/* create a CM connection node */
2466 	cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2467 	if (!cm_node)
2468 		return NULL;
2469 
2470 	/* set our node side to client (active) side */
2471 	cm_node->tcp_cntxt.client = 1;
2472 	cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2473 
2474 	if (cm_info->loc_addr == cm_info->rem_addr) {
2475 		loopbackremotelistener = find_listener(cm_core,
2476 			cm_node->mapped_loc_addr, cm_node->mapped_rem_port,
2477 			NES_CM_LISTENER_ACTIVE_STATE, 0);
2478 		if (loopbackremotelistener == NULL) {
2479 			create_event(cm_node, NES_CM_EVENT_ABORTED);
2480 		} else {
2481 			loopback_cm_info = *cm_info;
2482 			loopback_cm_info.loc_port = cm_info->rem_port;
2483 			loopback_cm_info.rem_port = cm_info->loc_port;
2484 			loopback_cm_info.mapped_loc_port =
2485 				cm_info->mapped_rem_port;
2486 			loopback_cm_info.mapped_rem_port =
2487 				cm_info->mapped_loc_port;
2488 			loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
2489 			loopbackremotenode = make_cm_node(cm_core, nesvnic,
2490 							  &loopback_cm_info, loopbackremotelistener);
2491 			if (!loopbackremotenode) {
2492 				rem_ref_cm_node(cm_node->cm_core, cm_node);
2493 				return NULL;
2494 			}
2495 			atomic_inc(&cm_loopbacks);
2496 			loopbackremotenode->loopbackpartner = cm_node;
2497 			loopbackremotenode->tcp_cntxt.rcv_wscale =
2498 				NES_CM_DEFAULT_RCV_WND_SCALE;
2499 			cm_node->loopbackpartner = loopbackremotenode;
2500 			memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2501 			       private_data_len);
2502 			loopbackremotenode->mpa_frame_size = private_data_len;
2503 
2504 			/* we are done handling this state. */
2505 			/* set node to a TSA state */
2506 			cm_node->state = NES_CM_STATE_TSA;
2507 			cm_node->tcp_cntxt.rcv_nxt =
2508 				loopbackremotenode->tcp_cntxt.loc_seq_num;
2509 			loopbackremotenode->tcp_cntxt.rcv_nxt =
2510 				cm_node->tcp_cntxt.loc_seq_num;
2511 			cm_node->tcp_cntxt.max_snd_wnd =
2512 				loopbackremotenode->tcp_cntxt.rcv_wnd;
2513 			loopbackremotenode->tcp_cntxt.max_snd_wnd =
2514 				cm_node->tcp_cntxt.rcv_wnd;
2515 			cm_node->tcp_cntxt.snd_wnd =
2516 				loopbackremotenode->tcp_cntxt.rcv_wnd;
2517 			loopbackremotenode->tcp_cntxt.snd_wnd =
2518 				cm_node->tcp_cntxt.rcv_wnd;
2519 			cm_node->tcp_cntxt.snd_wscale =
2520 				loopbackremotenode->tcp_cntxt.rcv_wscale;
2521 			loopbackremotenode->tcp_cntxt.snd_wscale =
2522 				cm_node->tcp_cntxt.rcv_wscale;
2523 			loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2524 			create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2525 		}
2526 		return cm_node;
2527 	}
2528 
2529 	start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2530 	cm_node->mpa_frame_size = private_data_len;
2531 
2532 	memcpy(start_buff, private_data, private_data_len);
2533 
2534 	/* send a syn and goto syn sent state */
2535 	cm_node->state = NES_CM_STATE_SYN_SENT;
2536 	ret = send_syn(cm_node, 0, NULL);
2537 
2538 	if (ret) {
2539 		/* error in sending the syn free up the cm_node struct */
2540 		nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2541 			  "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2542 			  cm_node->rem_addr, cm_node->rem_port, cm_node,
2543 			  cm_node->cm_id);
2544 		rem_ref_cm_node(cm_node->cm_core, cm_node);
2545 		cm_node = NULL;
2546 	}
2547 
2548 	if (cm_node) {
2549 		nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2550 			  "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2551 			  cm_node->rem_addr, cm_node->rem_port, cm_node,
2552 			  cm_node->cm_id);
2553 	}
2554 
2555 	return cm_node;
2556 }
2557 
2558 
2559 /**
2560  * mini_cm_accept - accept a connection
2561  * This function is never called
2562  */
mini_cm_accept(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)2563 static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2564 {
2565 	return 0;
2566 }
2567 
2568 
2569 /**
2570  * mini_cm_reject - reject and teardown a connection
2571  */
mini_cm_reject(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)2572 static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2573 {
2574 	int ret = 0;
2575 	int err = 0;
2576 	int passive_state;
2577 	struct nes_cm_event event;
2578 	struct iw_cm_id *cm_id = cm_node->cm_id;
2579 	struct nes_cm_node *loopback = cm_node->loopbackpartner;
2580 
2581 	nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2582 		  __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2583 
2584 	if (cm_node->tcp_cntxt.client)
2585 		return ret;
2586 	cleanup_retrans_entry(cm_node);
2587 
2588 	if (!loopback) {
2589 		passive_state = atomic_add_return(1, &cm_node->passive_state);
2590 		if (passive_state == NES_SEND_RESET_EVENT) {
2591 			cm_node->state = NES_CM_STATE_CLOSED;
2592 			rem_ref_cm_node(cm_core, cm_node);
2593 		} else {
2594 			if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2595 				rem_ref_cm_node(cm_core, cm_node);
2596 			} else {
2597 				ret = send_mpa_reject(cm_node);
2598 				if (ret) {
2599 					cm_node->state = NES_CM_STATE_CLOSED;
2600 					err = send_reset(cm_node, NULL);
2601 					if (err)
2602 						WARN_ON(1);
2603 				} else {
2604 					cm_id->add_ref(cm_id);
2605 				}
2606 			}
2607 		}
2608 	} else {
2609 		cm_node->cm_id = NULL;
2610 		if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2611 			rem_ref_cm_node(cm_core, cm_node);
2612 			rem_ref_cm_node(cm_core, loopback);
2613 		} else {
2614 			event.cm_node = loopback;
2615 			event.cm_info.rem_addr = loopback->rem_addr;
2616 			event.cm_info.loc_addr = loopback->loc_addr;
2617 			event.cm_info.rem_port = loopback->rem_port;
2618 			event.cm_info.loc_port = loopback->loc_port;
2619 			event.cm_info.cm_id = loopback->cm_id;
2620 			cm_event_mpa_reject(&event);
2621 			rem_ref_cm_node(cm_core, cm_node);
2622 			loopback->state = NES_CM_STATE_CLOSING;
2623 
2624 			cm_id = loopback->cm_id;
2625 			rem_ref_cm_node(cm_core, loopback);
2626 			cm_id->rem_ref(cm_id);
2627 		}
2628 	}
2629 
2630 	return ret;
2631 }
2632 
2633 
2634 /**
2635  * mini_cm_close
2636  */
mini_cm_close(struct nes_cm_core * cm_core,struct nes_cm_node * cm_node)2637 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2638 {
2639 	int ret = 0;
2640 
2641 	if (!cm_core || !cm_node)
2642 		return -EINVAL;
2643 
2644 	switch (cm_node->state) {
2645 	case NES_CM_STATE_SYN_RCVD:
2646 	case NES_CM_STATE_SYN_SENT:
2647 	case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2648 	case NES_CM_STATE_ESTABLISHED:
2649 	case NES_CM_STATE_ACCEPTING:
2650 	case NES_CM_STATE_MPAREQ_SENT:
2651 	case NES_CM_STATE_MPAREQ_RCVD:
2652 		cleanup_retrans_entry(cm_node);
2653 		send_reset(cm_node, NULL);
2654 		break;
2655 	case NES_CM_STATE_CLOSE_WAIT:
2656 		cm_node->state = NES_CM_STATE_LAST_ACK;
2657 		send_fin(cm_node, NULL);
2658 		break;
2659 	case NES_CM_STATE_FIN_WAIT1:
2660 	case NES_CM_STATE_FIN_WAIT2:
2661 	case NES_CM_STATE_LAST_ACK:
2662 	case NES_CM_STATE_TIME_WAIT:
2663 	case NES_CM_STATE_CLOSING:
2664 		ret = -1;
2665 		break;
2666 	case NES_CM_STATE_LISTENING:
2667 		cleanup_retrans_entry(cm_node);
2668 		send_reset(cm_node, NULL);
2669 		break;
2670 	case NES_CM_STATE_MPAREJ_RCVD:
2671 	case NES_CM_STATE_UNKNOWN:
2672 	case NES_CM_STATE_INITED:
2673 	case NES_CM_STATE_CLOSED:
2674 	case NES_CM_STATE_LISTENER_DESTROYED:
2675 		ret = rem_ref_cm_node(cm_core, cm_node);
2676 		break;
2677 	case NES_CM_STATE_TSA:
2678 		if (cm_node->send_entry)
2679 			printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2680 			       "send_entry=%p\n", cm_node->send_entry);
2681 		ret = rem_ref_cm_node(cm_core, cm_node);
2682 		break;
2683 	}
2684 	return ret;
2685 }
2686 
2687 
2688 /**
2689  * recv_pkt - recv an ETHERNET packet, and process it through CM
2690  * node state machine
2691  */
mini_cm_recv_pkt(struct nes_cm_core * cm_core,struct nes_vnic * nesvnic,struct sk_buff * skb)2692 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2693 			    struct nes_vnic *nesvnic, struct sk_buff *skb)
2694 {
2695 	struct nes_cm_node *cm_node = NULL;
2696 	struct nes_cm_listener *listener = NULL;
2697 	struct iphdr *iph;
2698 	struct tcphdr *tcph;
2699 	struct nes_cm_info nfo;
2700 	int skb_handled = 1;
2701 	__be32 tmp_daddr, tmp_saddr;
2702 
2703 	if (!skb)
2704 		return 0;
2705 	if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
2706 		return 0;
2707 
2708 	iph = (struct iphdr *)skb->data;
2709 	tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2710 
2711 	nfo.loc_addr = ntohl(iph->daddr);
2712 	nfo.loc_port = ntohs(tcph->dest);
2713 	nfo.rem_addr = ntohl(iph->saddr);
2714 	nfo.rem_port = ntohs(tcph->source);
2715 
2716 	/* If port mapper is available these should be mapped address info */
2717 	nfo.mapped_loc_addr = ntohl(iph->daddr);
2718 	nfo.mapped_loc_port = ntohs(tcph->dest);
2719 	nfo.mapped_rem_addr = ntohl(iph->saddr);
2720 	nfo.mapped_rem_port = ntohs(tcph->source);
2721 
2722 	tmp_daddr = cpu_to_be32(iph->daddr);
2723 	tmp_saddr = cpu_to_be32(iph->saddr);
2724 
2725 	nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2726 		  &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2727 
2728 	do {
2729 		cm_node = find_node(cm_core,
2730 				    nfo.mapped_rem_port, nfo.mapped_rem_addr,
2731 				    nfo.mapped_loc_port, nfo.mapped_loc_addr);
2732 
2733 		if (!cm_node) {
2734 			/* Only type of packet accepted are for */
2735 			/* the PASSIVE open (syn only) */
2736 			if ((!tcph->syn) || (tcph->ack)) {
2737 				skb_handled = 0;
2738 				break;
2739 			}
2740 			listener = find_listener(cm_core, nfo.mapped_loc_addr,
2741 					nfo.mapped_loc_port,
2742 					NES_CM_LISTENER_ACTIVE_STATE, 0);
2743 			if (!listener) {
2744 				nfo.cm_id = NULL;
2745 				nfo.conn_type = 0;
2746 				nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2747 				skb_handled = 0;
2748 				break;
2749 			}
2750 			nfo.cm_id = listener->cm_id;
2751 			nfo.conn_type = listener->conn_type;
2752 			cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2753 					       listener);
2754 			if (!cm_node) {
2755 				nes_debug(NES_DBG_CM, "Unable to allocate "
2756 					  "node\n");
2757 				cm_packets_dropped++;
2758 				atomic_dec(&listener->ref_count);
2759 				dev_kfree_skb_any(skb);
2760 				break;
2761 			}
2762 			if (!tcph->rst && !tcph->fin) {
2763 				cm_node->state = NES_CM_STATE_LISTENING;
2764 			} else {
2765 				cm_packets_dropped++;
2766 				rem_ref_cm_node(cm_core, cm_node);
2767 				dev_kfree_skb_any(skb);
2768 				break;
2769 			}
2770 			add_ref_cm_node(cm_node);
2771 		} else if (cm_node->state == NES_CM_STATE_TSA) {
2772 			if (cm_node->nesqp->pau_mode)
2773 				nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2774 			else {
2775 				rem_ref_cm_node(cm_core, cm_node);
2776 				atomic_inc(&cm_accel_dropped_pkts);
2777 				dev_kfree_skb_any(skb);
2778 			}
2779 			break;
2780 		}
2781 		skb_reset_network_header(skb);
2782 		skb_set_transport_header(skb, sizeof(*tcph));
2783 		skb->len = ntohs(iph->tot_len);
2784 		process_packet(cm_node, skb, cm_core);
2785 		rem_ref_cm_node(cm_core, cm_node);
2786 	} while (0);
2787 	return skb_handled;
2788 }
2789 
2790 
2791 /**
2792  * nes_cm_alloc_core - allocate a top level instance of a cm core
2793  */
nes_cm_alloc_core(void)2794 static struct nes_cm_core *nes_cm_alloc_core(void)
2795 {
2796 	struct nes_cm_core *cm_core;
2797 
2798 	/* setup the CM core */
2799 	/* alloc top level core control structure */
2800 	cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2801 	if (!cm_core)
2802 		return NULL;
2803 
2804 	INIT_LIST_HEAD(&cm_core->connected_nodes);
2805 	init_timer(&cm_core->tcp_timer);
2806 	cm_core->tcp_timer.function = nes_cm_timer_tick;
2807 
2808 	cm_core->mtu = NES_CM_DEFAULT_MTU;
2809 	cm_core->state = NES_CM_STATE_INITED;
2810 	cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2811 
2812 	atomic_set(&cm_core->events_posted, 0);
2813 
2814 	cm_core->api = &nes_cm_api;
2815 
2816 	spin_lock_init(&cm_core->ht_lock);
2817 	spin_lock_init(&cm_core->listen_list_lock);
2818 
2819 	INIT_LIST_HEAD(&cm_core->listen_list.list);
2820 
2821 	nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2822 
2823 	nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2824 	cm_core->event_wq = create_singlethread_workqueue("nesewq");
2825 	cm_core->post_event = nes_cm_post_event;
2826 	nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2827 	cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2828 
2829 	print_core(cm_core);
2830 	return cm_core;
2831 }
2832 
2833 
2834 /**
2835  * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2836  */
mini_cm_dealloc_core(struct nes_cm_core * cm_core)2837 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2838 {
2839 	nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2840 
2841 	if (!cm_core)
2842 		return -EINVAL;
2843 
2844 	barrier();
2845 
2846 	if (timer_pending(&cm_core->tcp_timer))
2847 		del_timer(&cm_core->tcp_timer);
2848 
2849 	destroy_workqueue(cm_core->event_wq);
2850 	destroy_workqueue(cm_core->disconn_wq);
2851 	nes_debug(NES_DBG_CM, "\n");
2852 	kfree(cm_core);
2853 
2854 	return 0;
2855 }
2856 
2857 
2858 /**
2859  * mini_cm_get
2860  */
mini_cm_get(struct nes_cm_core * cm_core)2861 static int mini_cm_get(struct nes_cm_core *cm_core)
2862 {
2863 	return cm_core->state;
2864 }
2865 
2866 
2867 /**
2868  * mini_cm_set
2869  */
mini_cm_set(struct nes_cm_core * cm_core,u32 type,u32 value)2870 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2871 {
2872 	int ret = 0;
2873 
2874 	switch (type) {
2875 	case NES_CM_SET_PKT_SIZE:
2876 		cm_core->mtu = value;
2877 		break;
2878 	case NES_CM_SET_FREE_PKT_Q_SIZE:
2879 		cm_core->free_tx_pkt_max = value;
2880 		break;
2881 	default:
2882 		/* unknown set option */
2883 		ret = -EINVAL;
2884 	}
2885 
2886 	return ret;
2887 }
2888 
2889 
2890 /**
2891  * nes_cm_init_tsa_conn setup HW; MPA frames must be
2892  * successfully exchanged when this is called
2893  */
nes_cm_init_tsa_conn(struct nes_qp * nesqp,struct nes_cm_node * cm_node)2894 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2895 {
2896 	int ret = 0;
2897 
2898 	if (!nesqp)
2899 		return -EINVAL;
2900 
2901 	nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2902 						  NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2903 						  NES_QPCONTEXT_MISC_DROS);
2904 
2905 	if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2906 		nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2907 
2908 	nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2909 
2910 	nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2911 
2912 	nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2913 		(u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2914 
2915 	nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2916 		(cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2917 		NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2918 
2919 	nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2920 		(cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2921 		NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2922 
2923 	nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2924 	nesqp->nesqp_context->ts_recent = 0;
2925 	nesqp->nesqp_context->ts_age = 0;
2926 	nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2927 	nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2928 	nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2929 	nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2930 						    cm_node->tcp_cntxt.rcv_wscale);
2931 	nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2932 	nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2933 	nesqp->nesqp_context->srtt = 0;
2934 	nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2935 	nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2936 	nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
2937 	nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2938 	nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2939 	nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2940 
2941 	nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2942 		  " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2943 		  nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2944 		  le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2945 		  cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2946 		  le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2947 		  le32_to_cpu(nesqp->nesqp_context->misc));
2948 	nes_debug(NES_DBG_CM, "  snd_wnd  = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2949 	nes_debug(NES_DBG_CM, "  snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2950 	nes_debug(NES_DBG_CM, "  max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2951 
2952 	nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2953 	cm_node->state = NES_CM_STATE_TSA;
2954 
2955 	return ret;
2956 }
2957 
2958 
2959 /**
2960  * nes_cm_disconn
2961  */
nes_cm_disconn(struct nes_qp * nesqp)2962 int nes_cm_disconn(struct nes_qp *nesqp)
2963 {
2964 	struct disconn_work *work;
2965 
2966 	work = kzalloc(sizeof *work, GFP_ATOMIC);
2967 	if (!work)
2968 		return -ENOMEM;  /* Timer will clean up */
2969 
2970 	nes_add_ref(&nesqp->ibqp);
2971 	work->nesqp = nesqp;
2972 	INIT_WORK(&work->work, nes_disconnect_worker);
2973 	queue_work(g_cm_core->disconn_wq, &work->work);
2974 	return 0;
2975 }
2976 
2977 
2978 /**
2979  * nes_disconnect_worker
2980  */
nes_disconnect_worker(struct work_struct * work)2981 static void nes_disconnect_worker(struct work_struct *work)
2982 {
2983 	struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2984 	struct nes_qp *nesqp = dwork->nesqp;
2985 
2986 	kfree(dwork);
2987 	nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2988 		  nesqp->last_aeq, nesqp->hwqp.qp_id);
2989 	nes_cm_disconn_true(nesqp);
2990 	nes_rem_ref(&nesqp->ibqp);
2991 }
2992 
2993 
2994 /**
2995  * nes_cm_disconn_true
2996  */
nes_cm_disconn_true(struct nes_qp * nesqp)2997 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2998 {
2999 	unsigned long flags;
3000 	int ret = 0;
3001 	struct iw_cm_id *cm_id;
3002 	struct iw_cm_event cm_event;
3003 	struct nes_vnic *nesvnic;
3004 	u16 last_ae;
3005 	u8 original_hw_tcp_state;
3006 	u8 original_ibqp_state;
3007 	int disconn_status = 0;
3008 	int issue_disconn = 0;
3009 	int issue_close = 0;
3010 	int issue_flush = 0;
3011 	u32 flush_q = NES_CQP_FLUSH_RQ;
3012 	struct ib_event ibevent;
3013 
3014 	if (!nesqp) {
3015 		nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
3016 		return -1;
3017 	}
3018 
3019 	spin_lock_irqsave(&nesqp->lock, flags);
3020 	cm_id = nesqp->cm_id;
3021 	/* make sure we havent already closed this connection */
3022 	if (!cm_id) {
3023 		nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
3024 			  nesqp->hwqp.qp_id);
3025 		spin_unlock_irqrestore(&nesqp->lock, flags);
3026 		return -1;
3027 	}
3028 
3029 	nesvnic = to_nesvnic(nesqp->ibqp.device);
3030 	nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
3031 
3032 	original_hw_tcp_state = nesqp->hw_tcp_state;
3033 	original_ibqp_state = nesqp->ibqp_state;
3034 	last_ae = nesqp->last_aeq;
3035 
3036 	if (nesqp->term_flags) {
3037 		issue_disconn = 1;
3038 		issue_close = 1;
3039 		nesqp->cm_id = NULL;
3040 		del_timer(&nesqp->terminate_timer);
3041 		if (nesqp->flush_issued == 0) {
3042 			nesqp->flush_issued = 1;
3043 			issue_flush = 1;
3044 		}
3045 	} else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
3046 			((original_ibqp_state == IB_QPS_RTS) &&
3047 			(last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
3048 		issue_disconn = 1;
3049 		if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
3050 			disconn_status = -ECONNRESET;
3051 	}
3052 
3053 	if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
3054 		 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
3055 		 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
3056 		 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
3057 		issue_close = 1;
3058 		nesqp->cm_id = NULL;
3059 		if (nesqp->flush_issued == 0) {
3060 			nesqp->flush_issued = 1;
3061 			issue_flush = 1;
3062 		}
3063 	}
3064 
3065 	spin_unlock_irqrestore(&nesqp->lock, flags);
3066 
3067 	if ((issue_flush) && (nesqp->destroyed == 0)) {
3068 		/* Flush the queue(s) */
3069 		if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
3070 			flush_q |= NES_CQP_FLUSH_SQ;
3071 		flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
3072 
3073 		if (nesqp->term_flags) {
3074 			ibevent.device = nesqp->ibqp.device;
3075 			ibevent.event = nesqp->terminate_eventtype;
3076 			ibevent.element.qp = &nesqp->ibqp;
3077 			if (nesqp->ibqp.event_handler)
3078 				nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
3079 		}
3080 	}
3081 
3082 	if ((cm_id) && (cm_id->event_handler)) {
3083 		if (issue_disconn) {
3084 			atomic_inc(&cm_disconnects);
3085 			cm_event.event = IW_CM_EVENT_DISCONNECT;
3086 			cm_event.status = disconn_status;
3087 			cm_event.local_addr = cm_id->local_addr;
3088 			cm_event.remote_addr = cm_id->remote_addr;
3089 			cm_event.private_data = NULL;
3090 			cm_event.private_data_len = 0;
3091 
3092 			nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
3093 				  " for  QP%u, SQ Head = %u, SQ Tail = %u. "
3094 				  "cm_id = %p, refcount = %u.\n",
3095 				  nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
3096 				  nesqp->hwqp.sq_tail, cm_id,
3097 				  atomic_read(&nesqp->refcount));
3098 
3099 			ret = cm_id->event_handler(cm_id, &cm_event);
3100 			if (ret)
3101 				nes_debug(NES_DBG_CM, "OFA CM event_handler "
3102 					  "returned, ret=%d\n", ret);
3103 		}
3104 
3105 		if (issue_close) {
3106 			atomic_inc(&cm_closes);
3107 			nes_disconnect(nesqp, 1);
3108 
3109 			cm_id->provider_data = nesqp;
3110 			/* Send up the close complete event */
3111 			cm_event.event = IW_CM_EVENT_CLOSE;
3112 			cm_event.status = 0;
3113 			cm_event.provider_data = cm_id->provider_data;
3114 			cm_event.local_addr = cm_id->local_addr;
3115 			cm_event.remote_addr = cm_id->remote_addr;
3116 			cm_event.private_data = NULL;
3117 			cm_event.private_data_len = 0;
3118 
3119 			ret = cm_id->event_handler(cm_id, &cm_event);
3120 			if (ret)
3121 				nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3122 
3123 			cm_id->rem_ref(cm_id);
3124 		}
3125 	}
3126 
3127 	return 0;
3128 }
3129 
3130 
3131 /**
3132  * nes_disconnect
3133  */
nes_disconnect(struct nes_qp * nesqp,int abrupt)3134 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
3135 {
3136 	int ret = 0;
3137 	struct nes_vnic *nesvnic;
3138 	struct nes_device *nesdev;
3139 	struct nes_ib_device *nesibdev;
3140 
3141 	nesvnic = to_nesvnic(nesqp->ibqp.device);
3142 	if (!nesvnic)
3143 		return -EINVAL;
3144 
3145 	nesdev = nesvnic->nesdev;
3146 	nesibdev = nesvnic->nesibdev;
3147 
3148 	nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
3149 			netdev_refcnt_read(nesvnic->netdev));
3150 
3151 	if (nesqp->active_conn) {
3152 
3153 		/* indicate this connection is NOT active */
3154 		nesqp->active_conn = 0;
3155 	} else {
3156 		/* Need to free the Last Streaming Mode Message */
3157 		if (nesqp->ietf_frame) {
3158 			if (nesqp->lsmm_mr)
3159 				nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
3160 			pci_free_consistent(nesdev->pcidev,
3161 					    nesqp->private_data_len + nesqp->ietf_frame_size,
3162 					    nesqp->ietf_frame, nesqp->ietf_frame_pbase);
3163 		}
3164 	}
3165 
3166 	/* close the CM node down if it is still active */
3167 	if (nesqp->cm_node) {
3168 		nes_debug(NES_DBG_CM, "Call close API\n");
3169 
3170 		g_cm_core->api->close(g_cm_core, nesqp->cm_node);
3171 	}
3172 
3173 	return ret;
3174 }
3175 
3176 
3177 /**
3178  * nes_accept
3179  */
nes_accept(struct iw_cm_id * cm_id,struct iw_cm_conn_param * conn_param)3180 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3181 {
3182 	u64 u64temp;
3183 	struct ib_qp *ibqp;
3184 	struct nes_qp *nesqp;
3185 	struct nes_vnic *nesvnic;
3186 	struct nes_device *nesdev;
3187 	struct nes_cm_node *cm_node;
3188 	struct nes_adapter *adapter;
3189 	struct ib_qp_attr attr;
3190 	struct iw_cm_event cm_event;
3191 	struct nes_hw_qp_wqe *wqe;
3192 	struct nes_v4_quad nes_quad;
3193 	u32 crc_value;
3194 	int ret;
3195 	int passive_state;
3196 	struct nes_ib_device *nesibdev;
3197 	struct ib_mr *ibmr = NULL;
3198 	struct ib_phys_buf ibphysbuf;
3199 	struct nes_pd *nespd;
3200 	u64 tagged_offset;
3201 	u8 mpa_frame_offset = 0;
3202 	struct ietf_mpa_v2 *mpa_v2_frame;
3203 	u8 start_addr = 0;
3204 	u8 *start_ptr = &start_addr;
3205 	u8 **start_buff = &start_ptr;
3206 	u16 buff_len = 0;
3207 	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3208 	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3209 
3210 	ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3211 	if (!ibqp)
3212 		return -EINVAL;
3213 
3214 	/* get all our handles */
3215 	nesqp = to_nesqp(ibqp);
3216 	nesvnic = to_nesvnic(nesqp->ibqp.device);
3217 	nesdev = nesvnic->nesdev;
3218 	adapter = nesdev->nesadapter;
3219 
3220 	cm_node = (struct nes_cm_node *)cm_id->provider_data;
3221 	nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3222 		"%s\n", cm_node, nesvnic, nesvnic->netdev,
3223 		nesvnic->netdev->name);
3224 
3225 	if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3226 		if (cm_node->loopbackpartner)
3227 			rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3228 		rem_ref_cm_node(cm_node->cm_core, cm_node);
3229 		return -EINVAL;
3230 	}
3231 
3232 	passive_state = atomic_add_return(1, &cm_node->passive_state);
3233 	if (passive_state == NES_SEND_RESET_EVENT) {
3234 		rem_ref_cm_node(cm_node->cm_core, cm_node);
3235 		return -ECONNRESET;
3236 	}
3237 	/* associate the node with the QP */
3238 	nesqp->cm_node = (void *)cm_node;
3239 	cm_node->nesqp = nesqp;
3240 
3241 
3242 	nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3243 		nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
3244 	atomic_inc(&cm_accepts);
3245 
3246 	nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
3247 			netdev_refcnt_read(nesvnic->netdev));
3248 
3249 	nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
3250 	/* allocate the ietf frame and space for private data */
3251 	nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
3252 						 nesqp->ietf_frame_size + conn_param->private_data_len,
3253 						 &nesqp->ietf_frame_pbase);
3254 
3255 	if (!nesqp->ietf_frame) {
3256 		nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
3257 		return -ENOMEM;
3258 	}
3259 	mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
3260 
3261 	if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3262 		mpa_frame_offset = 4;
3263 
3264 	if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
3265 			cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
3266 		record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3267 	}
3268 
3269 	memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3270 	       conn_param->private_data_len);
3271 
3272 	cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
3273 	nesqp->private_data_len = conn_param->private_data_len;
3274 
3275 	/* setup our first outgoing iWarp send WQE (the IETF frame response) */
3276 	wqe = &nesqp->hwqp.sq_vbase[0];
3277 
3278 	if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3279 		u64temp = (unsigned long)nesqp;
3280 		nesibdev = nesvnic->nesibdev;
3281 		nespd = nesqp->nespd;
3282 		ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3283 		ibphysbuf.size = buff_len;
3284 		tagged_offset = (u64)(unsigned long)*start_buff;
3285 		ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
3286 						   &ibphysbuf, 1,
3287 						   IB_ACCESS_LOCAL_WRITE,
3288 						   &tagged_offset);
3289 		if (!ibmr) {
3290 			nes_debug(NES_DBG_CM, "Unable to register memory region"
3291 				  "for lSMM for cm_node = %p \n",
3292 				  cm_node);
3293 			pci_free_consistent(nesdev->pcidev,
3294 					    nesqp->private_data_len + nesqp->ietf_frame_size,
3295 					    nesqp->ietf_frame, nesqp->ietf_frame_pbase);
3296 			return -ENOMEM;
3297 		}
3298 
3299 		ibmr->pd = &nespd->ibpd;
3300 		ibmr->device = nespd->ibpd.device;
3301 		nesqp->lsmm_mr = ibmr;
3302 
3303 		u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
3304 		set_wqe_64bit_value(wqe->wqe_words,
3305 				    NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3306 				    u64temp);
3307 		wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3308 			cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
3309 				    NES_IWARP_SQ_WQE_WRPDU);
3310 		wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
3311 			cpu_to_le32(buff_len);
3312 		set_wqe_64bit_value(wqe->wqe_words,
3313 				    NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3314 				    (u64)(unsigned long)(*start_buff));
3315 		wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
3316 			cpu_to_le32(buff_len);
3317 		wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
3318 		if (nesqp->sq_kmapped) {
3319 			nesqp->sq_kmapped = 0;
3320 			kunmap(nesqp->page);
3321 		}
3322 
3323 		nesqp->nesqp_context->ird_ord_sizes |=
3324 			cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3325 				    NES_QPCONTEXT_ORDIRD_WRPDU);
3326 	} else {
3327 		nesqp->nesqp_context->ird_ord_sizes |=
3328 			cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
3329 	}
3330 	nesqp->skip_lsmm = 1;
3331 
3332 	/* Cache the cm_id in the qp */
3333 	nesqp->cm_id = cm_id;
3334 	cm_node->cm_id = cm_id;
3335 
3336 	/*  nesqp->cm_node = (void *)cm_id->provider_data; */
3337 	cm_id->provider_data = nesqp;
3338 	nesqp->active_conn = 0;
3339 
3340 	if (cm_node->state == NES_CM_STATE_TSA)
3341 		nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
3342 			  cm_node);
3343 
3344 	nes_cm_init_tsa_conn(nesqp, cm_node);
3345 
3346 	nesqp->nesqp_context->tcpPorts[0] =
3347 				cpu_to_le16(cm_node->mapped_loc_port);
3348 	nesqp->nesqp_context->tcpPorts[1] =
3349 				cpu_to_le16(cm_node->mapped_rem_port);
3350 
3351 	nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->mapped_rem_addr);
3352 
3353 	nesqp->nesqp_context->misc2 |= cpu_to_le32(
3354 		(u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3355 		NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3356 
3357 	nesqp->nesqp_context->arp_index_vlan |=
3358 		cpu_to_le32(nes_arp_table(nesdev,
3359 					  le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3360 					  NES_ARP_RESOLVE) << 16);
3361 
3362 	nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3363 		jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3364 
3365 	nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3366 
3367 	nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
3368 		((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3369 	nesqp->nesqp_context->ird_ord_sizes |=
3370 		cpu_to_le32((u32)cm_node->ord_size);
3371 
3372 	memset(&nes_quad, 0, sizeof(nes_quad));
3373 	nes_quad.DstIpAdrIndex =
3374 		cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3375 	nes_quad.SrcIpadr = htonl(cm_node->mapped_rem_addr);
3376 	nes_quad.TcpPorts[0] = htons(cm_node->mapped_rem_port);
3377 	nes_quad.TcpPorts[1] = htons(cm_node->mapped_loc_port);
3378 
3379 	/* Produce hash key */
3380 	crc_value = get_crc_value(&nes_quad);
3381 	nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3382 	nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
3383 		  nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
3384 
3385 	nesqp->hte_index &= adapter->hte_index_mask;
3386 	nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3387 
3388 	cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3389 
3390 	nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
3391 		  "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3392 		  "private data length=%u.\n", nesqp->hwqp.qp_id,
3393 		  ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3394 		  ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
3395 		  le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3396 		  le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3397 		  buff_len);
3398 
3399 	/* notify OF layer that accept event was successful */
3400 	cm_id->add_ref(cm_id);
3401 	nes_add_ref(&nesqp->ibqp);
3402 
3403 	cm_event.event = IW_CM_EVENT_ESTABLISHED;
3404 	cm_event.status = 0;
3405 	cm_event.provider_data = (void *)nesqp;
3406 	cm_event.local_addr = cm_id->local_addr;
3407 	cm_event.remote_addr = cm_id->remote_addr;
3408 	cm_event.private_data = NULL;
3409 	cm_event.private_data_len = 0;
3410 	cm_event.ird = cm_node->ird_size;
3411 	cm_event.ord = cm_node->ord_size;
3412 
3413 	ret = cm_id->event_handler(cm_id, &cm_event);
3414 	attr.qp_state = IB_QPS_RTS;
3415 	nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3416 	if (cm_node->loopbackpartner) {
3417 		cm_node->loopbackpartner->mpa_frame_size =
3418 			nesqp->private_data_len;
3419 		/* copy entire MPA frame to our cm_node's frame */
3420 		memcpy(cm_node->loopbackpartner->mpa_frame_buf,
3421 		       conn_param->private_data, conn_param->private_data_len);
3422 		create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3423 	}
3424 	if (ret)
3425 		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3426 		       "ret=%d\n", __func__, __LINE__, ret);
3427 
3428 	return 0;
3429 }
3430 
3431 
3432 /**
3433  * nes_reject
3434  */
nes_reject(struct iw_cm_id * cm_id,const void * pdata,u8 pdata_len)3435 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3436 {
3437 	struct nes_cm_node *cm_node;
3438 	struct nes_cm_node *loopback;
3439 	struct nes_cm_core *cm_core;
3440 	u8 *start_buff;
3441 
3442 	atomic_inc(&cm_rejects);
3443 	cm_node = (struct nes_cm_node *)cm_id->provider_data;
3444 	loopback = cm_node->loopbackpartner;
3445 	cm_core = cm_node->cm_core;
3446 	cm_node->cm_id = cm_id;
3447 
3448 	if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
3449 		return -EINVAL;
3450 
3451 	if (loopback) {
3452 		memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3453 		loopback->mpa_frame.priv_data_len = pdata_len;
3454 		loopback->mpa_frame_size = pdata_len;
3455 	} else {
3456 		start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3457 		cm_node->mpa_frame_size = pdata_len;
3458 		memcpy(start_buff, pdata, pdata_len);
3459 	}
3460 	return cm_core->api->reject(cm_core, cm_node);
3461 }
3462 
3463 
3464 /**
3465  * nes_connect
3466  * setup and launch cm connect node
3467  */
nes_connect(struct iw_cm_id * cm_id,struct iw_cm_conn_param * conn_param)3468 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3469 {
3470 	struct ib_qp *ibqp;
3471 	struct nes_qp *nesqp;
3472 	struct nes_vnic *nesvnic;
3473 	struct nes_device *nesdev;
3474 	struct nes_cm_node *cm_node;
3475 	struct nes_cm_info cm_info;
3476 	int apbvt_set = 0;
3477 	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3478 	struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3479 	struct iwpm_dev_data pm_reg_msg;
3480 	struct iwpm_sa_data pm_msg;
3481 	int iwpm_err = 0;
3482 
3483 	if (cm_id->remote_addr.ss_family != AF_INET)
3484 		return -ENOSYS;
3485 	ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3486 	if (!ibqp)
3487 		return -EINVAL;
3488 	nesqp = to_nesqp(ibqp);
3489 	if (!nesqp)
3490 		return -EINVAL;
3491 	nesvnic = to_nesvnic(nesqp->ibqp.device);
3492 	if (!nesvnic)
3493 		return -EINVAL;
3494 	nesdev = nesvnic->nesdev;
3495 	if (!nesdev)
3496 		return -EINVAL;
3497 
3498 	if (!laddr->sin_port || !raddr->sin_port)
3499 		return -EINVAL;
3500 
3501 	nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3502 		  "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3503 		  ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3504 		  ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3505 		  ntohs(laddr->sin_port));
3506 
3507 	atomic_inc(&cm_connects);
3508 	nesqp->active_conn = 1;
3509 
3510 	/* cache the cm_id in the qp */
3511 	nesqp->cm_id = cm_id;
3512 	cm_id->provider_data = nesqp;
3513 	nesqp->private_data_len = conn_param->private_data_len;
3514 
3515 	nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
3516 	nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3517 		  conn_param->private_data_len);
3518 
3519 	/* set up the connection params for the node */
3520 	cm_info.loc_addr = ntohl(laddr->sin_addr.s_addr);
3521 	cm_info.loc_port = ntohs(laddr->sin_port);
3522 	cm_info.rem_addr = ntohl(raddr->sin_addr.s_addr);
3523 	cm_info.rem_port = ntohs(raddr->sin_port);
3524 	cm_info.cm_id = cm_id;
3525 	cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3526 
3527 	/* No port mapper available, go with the specified peer information */
3528 	cm_info.mapped_loc_addr = cm_info.loc_addr;
3529 	cm_info.mapped_loc_port = cm_info.loc_port;
3530 	cm_info.mapped_rem_addr = cm_info.rem_addr;
3531 	cm_info.mapped_rem_port = cm_info.rem_port;
3532 
3533 	nes_form_reg_msg(nesvnic, &pm_reg_msg);
3534 	iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_NES);
3535 	if (iwpm_err) {
3536 		nes_debug(NES_DBG_NLMSG,
3537 			"Port Mapper reg pid fail (err = %d).\n", iwpm_err);
3538 	}
3539 	if (iwpm_valid_pid() && !iwpm_err) {
3540 		nes_form_pm_msg(&cm_info, &pm_msg);
3541 		iwpm_err = iwpm_add_and_query_mapping(&pm_msg, RDMA_NL_NES);
3542 		if (iwpm_err)
3543 			nes_debug(NES_DBG_NLMSG,
3544 			"Port Mapper query fail (err = %d).\n", iwpm_err);
3545 		else
3546 			nes_record_pm_msg(&cm_info, &pm_msg);
3547 	}
3548 
3549 	if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
3550 		nes_manage_apbvt(nesvnic, cm_info.mapped_loc_port,
3551 			PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
3552 		apbvt_set = 1;
3553 	}
3554 
3555 	if (nes_create_mapinfo(&cm_info))
3556 		return -ENOMEM;
3557 
3558 	cm_id->add_ref(cm_id);
3559 
3560 	/* create a connect CM node connection */
3561 	cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3562 					  conn_param->private_data_len, (void *)conn_param->private_data,
3563 					  &cm_info);
3564 	if (!cm_node) {
3565 		if (apbvt_set)
3566 			nes_manage_apbvt(nesvnic, cm_info.mapped_loc_port,
3567 					 PCI_FUNC(nesdev->pcidev->devfn),
3568 					 NES_MANAGE_APBVT_DEL);
3569 
3570 		nes_debug(NES_DBG_NLMSG, "Delete mapped_loc_port = %04X\n",
3571 				cm_info.mapped_loc_port);
3572 		nes_remove_mapinfo(cm_info.loc_addr, cm_info.loc_port,
3573 			cm_info.mapped_loc_addr, cm_info.mapped_loc_port);
3574 		cm_id->rem_ref(cm_id);
3575 		return -ENOMEM;
3576 	}
3577 
3578 	record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3579 	if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3580 				cm_node->ord_size == 0)
3581 		cm_node->ord_size = 1;
3582 
3583 	cm_node->apbvt_set = apbvt_set;
3584 	nesqp->cm_node = cm_node;
3585 	cm_node->nesqp = nesqp;
3586 	nes_add_ref(&nesqp->ibqp);
3587 
3588 	return 0;
3589 }
3590 
3591 
3592 /**
3593  * nes_create_listen
3594  */
nes_create_listen(struct iw_cm_id * cm_id,int backlog)3595 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3596 {
3597 	struct nes_vnic *nesvnic;
3598 	struct nes_cm_listener *cm_node;
3599 	struct nes_cm_info cm_info;
3600 	int err;
3601 	struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3602 
3603 	nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3604 		  cm_id, ntohs(laddr->sin_port));
3605 
3606 	if (cm_id->local_addr.ss_family != AF_INET)
3607 		return -ENOSYS;
3608 	nesvnic = to_nesvnic(cm_id->device);
3609 	if (!nesvnic)
3610 		return -EINVAL;
3611 
3612 	nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3613 			nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3614 
3615 	nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3616 			nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
3617 
3618 	/* setup listen params in our api call struct */
3619 	cm_info.loc_addr = ntohl(nesvnic->local_ipaddr);
3620 	cm_info.loc_port = ntohs(laddr->sin_port);
3621 	cm_info.backlog = backlog;
3622 	cm_info.cm_id = cm_id;
3623 
3624 	cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3625 
3626 	/* No port mapper available, go with the specified info */
3627 	cm_info.mapped_loc_addr = cm_info.loc_addr;
3628 	cm_info.mapped_loc_port = cm_info.loc_port;
3629 
3630 	cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3631 	if (!cm_node) {
3632 		printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
3633 		       __func__, __LINE__);
3634 		return -ENOMEM;
3635 	}
3636 
3637 	cm_id->provider_data = cm_node;
3638 
3639 	if (!cm_node->reused_node) {
3640 		if (nes_create_mapinfo(&cm_info))
3641 			return -ENOMEM;
3642 
3643 		err = nes_manage_apbvt(nesvnic, cm_node->mapped_loc_port,
3644 				       PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3645 				       NES_MANAGE_APBVT_ADD);
3646 		if (err) {
3647 			printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3648 			       err);
3649 			g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3650 			return err;
3651 		}
3652 		atomic_inc(&cm_listens_created);
3653 	}
3654 
3655 	cm_id->add_ref(cm_id);
3656 	cm_id->provider_data = (void *)cm_node;
3657 
3658 
3659 	return 0;
3660 }
3661 
3662 
3663 /**
3664  * nes_destroy_listen
3665  */
nes_destroy_listen(struct iw_cm_id * cm_id)3666 int nes_destroy_listen(struct iw_cm_id *cm_id)
3667 {
3668 	if (cm_id->provider_data)
3669 		g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3670 	else
3671 		nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3672 
3673 	cm_id->rem_ref(cm_id);
3674 
3675 	return 0;
3676 }
3677 
3678 
3679 /**
3680  * nes_cm_recv
3681  */
nes_cm_recv(struct sk_buff * skb,struct net_device * netdevice)3682 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3683 {
3684 	int rc = 0;
3685 
3686 	cm_packets_received++;
3687 	if ((g_cm_core) && (g_cm_core->api))
3688 		rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
3689 	else
3690 		nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3691 			  " cm is not setup properly.\n");
3692 
3693 	return rc;
3694 }
3695 
3696 
3697 /**
3698  * nes_cm_start
3699  * Start and init a cm core module
3700  */
nes_cm_start(void)3701 int nes_cm_start(void)
3702 {
3703 	nes_debug(NES_DBG_CM, "\n");
3704 	/* create the primary CM core, pass this handle to subsequent core inits */
3705 	g_cm_core = nes_cm_alloc_core();
3706 	if (g_cm_core)
3707 		return 0;
3708 	else
3709 		return -ENOMEM;
3710 }
3711 
3712 
3713 /**
3714  * nes_cm_stop
3715  * stop and dealloc all cm core instances
3716  */
nes_cm_stop(void)3717 int nes_cm_stop(void)
3718 {
3719 	g_cm_core->api->destroy_cm_core(g_cm_core);
3720 	return 0;
3721 }
3722 
3723 
3724 /**
3725  * cm_event_connected
3726  * handle a connected event, setup QPs and HW
3727  */
cm_event_connected(struct nes_cm_event * event)3728 static void cm_event_connected(struct nes_cm_event *event)
3729 {
3730 	struct nes_qp *nesqp;
3731 	struct nes_vnic *nesvnic;
3732 	struct nes_device *nesdev;
3733 	struct nes_cm_node *cm_node;
3734 	struct nes_adapter *nesadapter;
3735 	struct ib_qp_attr attr;
3736 	struct iw_cm_id *cm_id;
3737 	struct iw_cm_event cm_event;
3738 	struct nes_v4_quad nes_quad;
3739 	u32 crc_value;
3740 	int ret;
3741 	struct sockaddr_in *laddr;
3742 	struct sockaddr_in *raddr;
3743 	struct sockaddr_in *cm_event_laddr;
3744 
3745 	/* get all our handles */
3746 	cm_node = event->cm_node;
3747 	cm_id = cm_node->cm_id;
3748 	nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3749 	nesqp = (struct nes_qp *)cm_id->provider_data;
3750 	nesvnic = to_nesvnic(nesqp->ibqp.device);
3751 	nesdev = nesvnic->nesdev;
3752 	nesadapter = nesdev->nesadapter;
3753 	laddr = (struct sockaddr_in *)&cm_id->local_addr;
3754 	raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3755 	cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
3756 
3757 	if (nesqp->destroyed)
3758 		return;
3759 	atomic_inc(&cm_connecteds);
3760 	nes_debug(NES_DBG_CM, "QP%u attempting to connect to  0x%08X:0x%04X on"
3761 		  " local port 0x%04X. jiffies = %lu.\n",
3762 		  nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3763 		  ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
3764 
3765 	nes_cm_init_tsa_conn(nesqp, cm_node);
3766 
3767 	/* set the QP tsa context */
3768 	nesqp->nesqp_context->tcpPorts[0] =
3769 			cpu_to_le16(cm_node->mapped_loc_port);
3770 	nesqp->nesqp_context->tcpPorts[1] =
3771 			cpu_to_le16(cm_node->mapped_rem_port);
3772 	nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->mapped_rem_addr);
3773 
3774 	nesqp->nesqp_context->misc2 |= cpu_to_le32(
3775 			(u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3776 			NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3777 	nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3778 			nes_arp_table(nesdev,
3779 			le32_to_cpu(nesqp->nesqp_context->ip0),
3780 			NULL, NES_ARP_RESOLVE) << 16);
3781 	nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3782 			jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3783 	nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3784 	nesqp->nesqp_context->ird_ord_sizes |=
3785 			cpu_to_le32((u32)1 <<
3786 			NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
3787 	nesqp->nesqp_context->ird_ord_sizes |=
3788 			cpu_to_le32((u32)cm_node->ord_size);
3789 
3790 	/* Adjust tail for not having a LSMM */
3791 	/*nesqp->hwqp.sq_tail = 1;*/
3792 
3793 	build_rdma0_msg(cm_node, &nesqp);
3794 
3795 	nes_write32(nesdev->regs + NES_WQE_ALLOC,
3796 		    (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3797 
3798 	memset(&nes_quad, 0, sizeof(nes_quad));
3799 
3800 	nes_quad.DstIpAdrIndex =
3801 		cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3802 	nes_quad.SrcIpadr = htonl(cm_node->mapped_rem_addr);
3803 	nes_quad.TcpPorts[0] = htons(cm_node->mapped_rem_port);
3804 	nes_quad.TcpPorts[1] = htons(cm_node->mapped_loc_port);
3805 
3806 	/* Produce hash key */
3807 	crc_value = get_crc_value(&nes_quad);
3808 	nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3809 	nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3810 		  nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3811 
3812 	nesqp->hte_index &= nesadapter->hte_index_mask;
3813 	nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3814 
3815 	nesqp->ietf_frame = &cm_node->mpa_frame;
3816 	nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
3817 	cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3818 
3819 	/* notify OF layer we successfully created the requested connection */
3820 	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3821 	cm_event.status = 0;
3822 	cm_event.provider_data = cm_id->provider_data;
3823 	cm_event_laddr->sin_family = AF_INET;
3824 	cm_event_laddr->sin_port = laddr->sin_port;
3825 	cm_event.remote_addr = cm_id->remote_addr;
3826 
3827 	cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3828 	cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3829 	cm_event.ird = cm_node->ird_size;
3830 	cm_event.ord = cm_node->ord_size;
3831 
3832 	cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3833 	ret = cm_id->event_handler(cm_id, &cm_event);
3834 	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3835 
3836 	if (ret)
3837 		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3838 		       "ret=%d\n", __func__, __LINE__, ret);
3839 	attr.qp_state = IB_QPS_RTS;
3840 	nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3841 
3842 	nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3843 		  "%lu\n", nesqp->hwqp.qp_id, jiffies);
3844 
3845 	return;
3846 }
3847 
3848 
3849 /**
3850  * cm_event_connect_error
3851  */
cm_event_connect_error(struct nes_cm_event * event)3852 static void cm_event_connect_error(struct nes_cm_event *event)
3853 {
3854 	struct nes_qp *nesqp;
3855 	struct iw_cm_id *cm_id;
3856 	struct iw_cm_event cm_event;
3857 	/* struct nes_cm_info cm_info; */
3858 	int ret;
3859 
3860 	if (!event->cm_node)
3861 		return;
3862 
3863 	cm_id = event->cm_node->cm_id;
3864 	if (!cm_id)
3865 		return;
3866 
3867 	nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3868 	nesqp = cm_id->provider_data;
3869 
3870 	if (!nesqp)
3871 		return;
3872 
3873 	/* notify OF layer about this connection error event */
3874 	/* cm_id->rem_ref(cm_id); */
3875 	nesqp->cm_id = NULL;
3876 	cm_id->provider_data = NULL;
3877 	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3878 	cm_event.status = -ECONNRESET;
3879 	cm_event.provider_data = cm_id->provider_data;
3880 	cm_event.local_addr = cm_id->local_addr;
3881 	cm_event.remote_addr = cm_id->remote_addr;
3882 	cm_event.private_data = NULL;
3883 	cm_event.private_data_len = 0;
3884 
3885 #ifdef CONFIG_INFINIBAND_NES_DEBUG
3886 	{
3887 		struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3888 						     &cm_event.local_addr;
3889 		struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3890 						     &cm_event.remote_addr;
3891 		nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3892 			  cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3893 	}
3894 #endif
3895 
3896 	ret = cm_id->event_handler(cm_id, &cm_event);
3897 	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3898 	if (ret)
3899 		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3900 		       "ret=%d\n", __func__, __LINE__, ret);
3901 	cm_id->rem_ref(cm_id);
3902 
3903 	rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3904 	return;
3905 }
3906 
3907 
3908 /**
3909  * cm_event_reset
3910  */
cm_event_reset(struct nes_cm_event * event)3911 static void cm_event_reset(struct nes_cm_event *event)
3912 {
3913 	struct nes_qp *nesqp;
3914 	struct iw_cm_id *cm_id;
3915 	struct iw_cm_event cm_event;
3916 	/* struct nes_cm_info cm_info; */
3917 	int ret;
3918 
3919 	if (!event->cm_node)
3920 		return;
3921 
3922 	if (!event->cm_node->cm_id)
3923 		return;
3924 
3925 	cm_id = event->cm_node->cm_id;
3926 
3927 	nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3928 	nesqp = cm_id->provider_data;
3929 	if (!nesqp)
3930 		return;
3931 
3932 	nesqp->cm_id = NULL;
3933 	/* cm_id->provider_data = NULL; */
3934 	cm_event.event = IW_CM_EVENT_DISCONNECT;
3935 	cm_event.status = -ECONNRESET;
3936 	cm_event.provider_data = cm_id->provider_data;
3937 	cm_event.local_addr = cm_id->local_addr;
3938 	cm_event.remote_addr = cm_id->remote_addr;
3939 	cm_event.private_data = NULL;
3940 	cm_event.private_data_len = 0;
3941 
3942 	cm_id->add_ref(cm_id);
3943 	ret = cm_id->event_handler(cm_id, &cm_event);
3944 	atomic_inc(&cm_closes);
3945 	cm_event.event = IW_CM_EVENT_CLOSE;
3946 	cm_event.status = 0;
3947 	cm_event.provider_data = cm_id->provider_data;
3948 	cm_event.local_addr = cm_id->local_addr;
3949 	cm_event.remote_addr = cm_id->remote_addr;
3950 	cm_event.private_data = NULL;
3951 	cm_event.private_data_len = 0;
3952 	nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3953 	ret = cm_id->event_handler(cm_id, &cm_event);
3954 
3955 	nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3956 
3957 
3958 	/* notify OF layer about this connection error event */
3959 	cm_id->rem_ref(cm_id);
3960 
3961 	return;
3962 }
3963 
3964 
3965 /**
3966  * cm_event_mpa_req
3967  */
cm_event_mpa_req(struct nes_cm_event * event)3968 static void cm_event_mpa_req(struct nes_cm_event *event)
3969 {
3970 	struct iw_cm_id *cm_id;
3971 	struct iw_cm_event cm_event;
3972 	int ret;
3973 	struct nes_cm_node *cm_node;
3974 	struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3975 					     &cm_event.local_addr;
3976 	struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3977 					     &cm_event.remote_addr;
3978 
3979 	cm_node = event->cm_node;
3980 	if (!cm_node)
3981 		return;
3982 	cm_id = cm_node->cm_id;
3983 
3984 	atomic_inc(&cm_connect_reqs);
3985 	nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3986 		  cm_node, cm_id, jiffies);
3987 
3988 	cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3989 	cm_event.status = 0;
3990 	cm_event.provider_data = (void *)cm_node;
3991 
3992 	cm_event_laddr->sin_family = AF_INET;
3993 	cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3994 	cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3995 
3996 	cm_event_raddr->sin_family = AF_INET;
3997 	cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3998 	cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3999 	cm_event.private_data = cm_node->mpa_frame_buf;
4000 	cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
4001 	if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
4002 		cm_event.ird = NES_MAX_IRD;
4003 		cm_event.ord = NES_MAX_ORD;
4004 	} else {
4005 	cm_event.ird = cm_node->ird_size;
4006 	cm_event.ord = cm_node->ord_size;
4007 	}
4008 
4009 	ret = cm_id->event_handler(cm_id, &cm_event);
4010 	if (ret)
4011 		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
4012 		       __func__, __LINE__, ret);
4013 	return;
4014 }
4015 
4016 
cm_event_mpa_reject(struct nes_cm_event * event)4017 static void cm_event_mpa_reject(struct nes_cm_event *event)
4018 {
4019 	struct iw_cm_id *cm_id;
4020 	struct iw_cm_event cm_event;
4021 	struct nes_cm_node *cm_node;
4022 	int ret;
4023 	struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
4024 					     &cm_event.local_addr;
4025 	struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
4026 					     &cm_event.remote_addr;
4027 
4028 	cm_node = event->cm_node;
4029 	if (!cm_node)
4030 		return;
4031 	cm_id = cm_node->cm_id;
4032 
4033 	atomic_inc(&cm_connect_reqs);
4034 	nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
4035 		  cm_node, cm_id, jiffies);
4036 
4037 	cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
4038 	cm_event.status = -ECONNREFUSED;
4039 	cm_event.provider_data = cm_id->provider_data;
4040 
4041 	cm_event_laddr->sin_family = AF_INET;
4042 	cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
4043 	cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
4044 
4045 	cm_event_raddr->sin_family = AF_INET;
4046 	cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
4047 	cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
4048 
4049 	cm_event.private_data = cm_node->mpa_frame_buf;
4050 	cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
4051 
4052 	nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
4053 		  "remove_addr=%08x\n",
4054 		  cm_event_laddr->sin_addr.s_addr,
4055 		  cm_event_raddr->sin_addr.s_addr);
4056 
4057 	ret = cm_id->event_handler(cm_id, &cm_event);
4058 	if (ret)
4059 		printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
4060 		       __func__, __LINE__, ret);
4061 
4062 	return;
4063 }
4064 
4065 
4066 static void nes_cm_event_handler(struct work_struct *);
4067 
4068 /**
4069  * nes_cm_post_event
4070  * post an event to the cm event handler
4071  */
nes_cm_post_event(struct nes_cm_event * event)4072 static int nes_cm_post_event(struct nes_cm_event *event)
4073 {
4074 	atomic_inc(&event->cm_node->cm_core->events_posted);
4075 	add_ref_cm_node(event->cm_node);
4076 	event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
4077 	INIT_WORK(&event->event_work, nes_cm_event_handler);
4078 	nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
4079 		  event->cm_node, event);
4080 
4081 	queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
4082 
4083 	nes_debug(NES_DBG_CM, "Exit\n");
4084 	return 0;
4085 }
4086 
4087 
4088 /**
4089  * nes_cm_event_handler
4090  * worker function to handle cm events
4091  * will free instance of nes_cm_event
4092  */
nes_cm_event_handler(struct work_struct * work)4093 static void nes_cm_event_handler(struct work_struct *work)
4094 {
4095 	struct nes_cm_event *event = container_of(work, struct nes_cm_event,
4096 						  event_work);
4097 	struct nes_cm_core *cm_core;
4098 
4099 	if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
4100 		return;
4101 
4102 	cm_core = event->cm_node->cm_core;
4103 	nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
4104 		  event, event->type, atomic_read(&cm_core->events_posted));
4105 
4106 	switch (event->type) {
4107 	case NES_CM_EVENT_MPA_REQ:
4108 		cm_event_mpa_req(event);
4109 		nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
4110 			  event->cm_node);
4111 		break;
4112 	case NES_CM_EVENT_RESET:
4113 		nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
4114 			  event->cm_node);
4115 		cm_event_reset(event);
4116 		break;
4117 	case NES_CM_EVENT_CONNECTED:
4118 		if ((!event->cm_node->cm_id) ||
4119 		    (event->cm_node->state != NES_CM_STATE_TSA))
4120 			break;
4121 		cm_event_connected(event);
4122 		nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
4123 		break;
4124 	case NES_CM_EVENT_MPA_REJECT:
4125 		if ((!event->cm_node->cm_id) ||
4126 		    (event->cm_node->state == NES_CM_STATE_TSA))
4127 			break;
4128 		cm_event_mpa_reject(event);
4129 		nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
4130 		break;
4131 
4132 	case NES_CM_EVENT_ABORTED:
4133 		if ((!event->cm_node->cm_id) ||
4134 		    (event->cm_node->state == NES_CM_STATE_TSA))
4135 			break;
4136 		cm_event_connect_error(event);
4137 		nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
4138 		break;
4139 	case NES_CM_EVENT_DROPPED_PKT:
4140 		nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
4141 		break;
4142 	default:
4143 		nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
4144 		break;
4145 	}
4146 
4147 	atomic_dec(&cm_core->events_posted);
4148 	event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
4149 	rem_ref_cm_node(cm_core, event->cm_node);
4150 	kfree(event);
4151 
4152 	return;
4153 }
4154