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