xref: /freebsd/sys/dev/irdma/irdma_cm.c (revision 6c92544d7c9722a3fe6263134938d1f864c158c5)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2015 - 2022 Intel Corporation
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenFabrics.org BSD license below:
11  *
12  *   Redistribution and use in source and binary forms, with or
13  *   without modification, are permitted provided that the following
14  *   conditions are met:
15  *
16  *    - Redistributions of source code must retain the above
17  *	copyright notice, this list of conditions and the following
18  *	disclaimer.
19  *
20  *    - Redistributions in binary form must reproduce the above
21  *	copyright notice, this list of conditions and the following
22  *	disclaimer in the documentation and/or other materials
23  *	provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 /*$FreeBSD$*/
35 
36 #include "irdma_main.h"
37 
38 static void irdma_cm_post_event(struct irdma_cm_event *event);
39 static void irdma_disconnect_worker(struct work_struct *work);
40 
41 /**
42  * irdma_free_sqbuf - put back puda buffer if refcount is 0
43  * @vsi: The VSI structure of the device
44  * @bufp: puda buffer to free
45  */
46 void
47 irdma_free_sqbuf(struct irdma_sc_vsi *vsi, void *bufp)
48 {
49 	struct irdma_puda_buf *buf = bufp;
50 	struct irdma_puda_rsrc *ilq = vsi->ilq;
51 
52 	if (atomic_dec_and_test(&buf->refcount))
53 		irdma_puda_ret_bufpool(ilq, buf);
54 }
55 
56 /**
57  * irdma_record_ird_ord - Record IRD/ORD passed in
58  * @cm_node: connection's node
59  * @conn_ird: connection IRD
60  * @conn_ord: connection ORD
61  */
62 static void
63 irdma_record_ird_ord(struct irdma_cm_node *cm_node, u32 conn_ird,
64 		     u32 conn_ord)
65 {
66 	if (conn_ird > cm_node->dev->hw_attrs.max_hw_ird)
67 		conn_ird = cm_node->dev->hw_attrs.max_hw_ird;
68 
69 	if (conn_ord > cm_node->dev->hw_attrs.max_hw_ord)
70 		conn_ord = cm_node->dev->hw_attrs.max_hw_ord;
71 	else if (!conn_ord && cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO)
72 		conn_ord = 1;
73 	cm_node->ird_size = conn_ird;
74 	cm_node->ord_size = conn_ord;
75 }
76 
77 /**
78  * irdma_copy_ip_ntohl - copy IP address from  network to host
79  * @dst: IP address in host order
80  * @src: IP address in network order (big endian)
81  */
82 void
83 irdma_copy_ip_ntohl(u32 *dst, __be32 *src)
84 {
85 	*dst++ = ntohl(*src++);
86 	*dst++ = ntohl(*src++);
87 	*dst++ = ntohl(*src++);
88 	*dst = ntohl(*src);
89 }
90 
91 /**
92  * irdma_copy_ip_htonl - copy IP address from host to network order
93  * @dst: IP address in network order (big endian)
94  * @src: IP address in host order
95  */
96 void
97 irdma_copy_ip_htonl(__be32 *dst, u32 *src)
98 {
99 	*dst++ = htonl(*src++);
100 	*dst++ = htonl(*src++);
101 	*dst++ = htonl(*src++);
102 	*dst = htonl(*src);
103 }
104 
105 /**
106  * irdma_get_addr_info
107  * @cm_node: contains ip/tcp info
108  * @cm_info: to get a copy of the cm_node ip/tcp info
109  */
110 static void
111 irdma_get_addr_info(struct irdma_cm_node *cm_node,
112 		    struct irdma_cm_info *cm_info)
113 {
114 	memset(cm_info, 0, sizeof(*cm_info));
115 	cm_info->ipv4 = cm_node->ipv4;
116 	cm_info->vlan_id = cm_node->vlan_id;
117 	memcpy(cm_info->loc_addr, cm_node->loc_addr, sizeof(cm_info->loc_addr));
118 	memcpy(cm_info->rem_addr, cm_node->rem_addr, sizeof(cm_info->rem_addr));
119 	cm_info->loc_port = cm_node->loc_port;
120 	cm_info->rem_port = cm_node->rem_port;
121 }
122 
123 /**
124  * irdma_fill_sockaddr4 - fill in addr info for IPv4 connection
125  * @cm_node: connection's node
126  * @event: upper layer's cm event
127  */
128 static inline void
129 irdma_fill_sockaddr4(struct irdma_cm_node *cm_node,
130 		     struct iw_cm_event *event)
131 {
132 	struct sockaddr_in *laddr = (struct sockaddr_in *)&event->local_addr;
133 	struct sockaddr_in *raddr = (struct sockaddr_in *)&event->remote_addr;
134 
135 	laddr->sin_family = AF_INET;
136 	raddr->sin_family = AF_INET;
137 
138 	laddr->sin_port = htons(cm_node->loc_port);
139 	raddr->sin_port = htons(cm_node->rem_port);
140 
141 	laddr->sin_addr.s_addr = htonl(cm_node->loc_addr[0]);
142 	raddr->sin_addr.s_addr = htonl(cm_node->rem_addr[0]);
143 }
144 
145 /**
146  * irdma_fill_sockaddr6 - fill in addr info for IPv6 connection
147  * @cm_node: connection's node
148  * @event: upper layer's cm event
149  */
150 static inline void
151 irdma_fill_sockaddr6(struct irdma_cm_node *cm_node,
152 		     struct iw_cm_event *event)
153 {
154 	struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)&event->local_addr;
155 	struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)&event->remote_addr;
156 
157 	laddr6->sin6_family = AF_INET6;
158 	raddr6->sin6_family = AF_INET6;
159 
160 	laddr6->sin6_port = htons(cm_node->loc_port);
161 	raddr6->sin6_port = htons(cm_node->rem_port);
162 
163 	irdma_copy_ip_htonl(laddr6->sin6_addr.__u6_addr.__u6_addr32,
164 			    cm_node->loc_addr);
165 	irdma_copy_ip_htonl(raddr6->sin6_addr.__u6_addr.__u6_addr32,
166 			    cm_node->rem_addr);
167 }
168 
169 /**
170  * irdma_get_cmevent_info - for cm event upcall
171  * @cm_node: connection's node
172  * @cm_id: upper layers cm struct for the event
173  * @event: upper layer's cm event
174  */
175 static inline void
176 irdma_get_cmevent_info(struct irdma_cm_node *cm_node,
177 		       struct iw_cm_id *cm_id,
178 		       struct iw_cm_event *event)
179 {
180 	memcpy(&event->local_addr, &cm_id->m_local_addr,
181 	       sizeof(event->local_addr));
182 	memcpy(&event->remote_addr, &cm_id->m_remote_addr,
183 	       sizeof(event->remote_addr));
184 	if (cm_node) {
185 		event->private_data = cm_node->pdata_buf;
186 		event->private_data_len = (u8)cm_node->pdata.size;
187 		event->ird = cm_node->ird_size;
188 		event->ord = cm_node->ord_size;
189 	}
190 }
191 
192 /**
193  * irdma_send_cm_event - upcall cm's event handler
194  * @cm_node: connection's node
195  * @cm_id: upper layer's cm info struct
196  * @type: Event type to indicate
197  * @status: status for the event type
198  */
199 static int
200 irdma_send_cm_event(struct irdma_cm_node *cm_node,
201 		    struct iw_cm_id *cm_id,
202 		    enum iw_cm_event_type type, int status)
203 {
204 	struct iw_cm_event event = {0};
205 
206 	event.event = type;
207 	event.status = status;
208 
209 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
210 		    "cm_node %p cm_id=%p state=%d accel=%d event_type=%d status=%d\n",
211 		    cm_node, cm_id, cm_node->accelerated, cm_node->state, type,
212 		    status);
213 
214 	switch (type) {
215 	case IW_CM_EVENT_CONNECT_REQUEST:
216 		if (cm_node->ipv4)
217 			irdma_fill_sockaddr4(cm_node, &event);
218 		else
219 			irdma_fill_sockaddr6(cm_node, &event);
220 		event.provider_data = cm_node;
221 		event.private_data = cm_node->pdata_buf;
222 		event.private_data_len = (u8)cm_node->pdata.size;
223 		event.ird = cm_node->ird_size;
224 		break;
225 	case IW_CM_EVENT_CONNECT_REPLY:
226 		irdma_get_cmevent_info(cm_node, cm_id, &event);
227 		break;
228 	case IW_CM_EVENT_ESTABLISHED:
229 		event.ird = cm_node->ird_size;
230 		event.ord = cm_node->ord_size;
231 		break;
232 	case IW_CM_EVENT_DISCONNECT:
233 	case IW_CM_EVENT_CLOSE:
234 		/* Wait if we are in RTS but havent issued the iwcm event upcall */
235 		if (!cm_node->accelerated)
236 			wait_for_completion(&cm_node->establish_comp);
237 		break;
238 	default:
239 		return -EINVAL;
240 	}
241 
242 	return cm_id->event_handler(cm_id, &event);
243 }
244 
245 /**
246  * irdma_timer_list_prep - add connection nodes to a list to perform timer tasks
247  * @cm_core: cm's core
248  * @timer_list: a timer list to which cm_node will be selected
249  */
250 static void
251 irdma_timer_list_prep(struct irdma_cm_core *cm_core,
252 		      struct list_head *timer_list)
253 {
254 	struct irdma_cm_node *cm_node;
255 	int bkt;
256 
257 	HASH_FOR_EACH_RCU(cm_core->cm_hash_tbl, bkt, cm_node, list) {
258 		if ((cm_node->close_entry || cm_node->send_entry) &&
259 		    atomic_inc_not_zero(&cm_node->refcnt))
260 			list_add(&cm_node->timer_entry, timer_list);
261 	}
262 }
263 
264 /**
265  * irdma_create_event - create cm event
266  * @cm_node: connection's node
267  * @type: Event type to generate
268  */
269 static struct irdma_cm_event *
270 irdma_create_event(struct irdma_cm_node *cm_node,
271 		   enum irdma_cm_event_type type)
272 {
273 	struct irdma_cm_event *event;
274 
275 	if (!cm_node->cm_id)
276 		return NULL;
277 
278 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
279 
280 	if (!event)
281 		return NULL;
282 
283 	event->type = type;
284 	event->cm_node = cm_node;
285 	memcpy(event->cm_info.rem_addr, cm_node->rem_addr,
286 	       sizeof(event->cm_info.rem_addr));
287 	memcpy(event->cm_info.loc_addr, cm_node->loc_addr,
288 	       sizeof(event->cm_info.loc_addr));
289 	event->cm_info.rem_port = cm_node->rem_port;
290 	event->cm_info.loc_port = cm_node->loc_port;
291 	event->cm_info.cm_id = cm_node->cm_id;
292 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
293 		    "node=%p event=%p type=%u dst=%pI4 src=%pI4\n",
294 		    cm_node,
295 		    event, type, event->cm_info.loc_addr,
296 		    event->cm_info.rem_addr);
297 	irdma_cm_post_event(event);
298 
299 	return event;
300 }
301 
302 /**
303  * irdma_free_retrans_entry - free send entry
304  * @cm_node: connection's node
305  */
306 static void
307 irdma_free_retrans_entry(struct irdma_cm_node *cm_node)
308 {
309 	struct irdma_device *iwdev = cm_node->iwdev;
310 	struct irdma_timer_entry *send_entry;
311 
312 	send_entry = cm_node->send_entry;
313 	if (!send_entry)
314 		return;
315 
316 	cm_node->send_entry = NULL;
317 	irdma_free_sqbuf(&iwdev->vsi, send_entry->sqbuf);
318 	kfree(send_entry);
319 	atomic_dec(&cm_node->refcnt);
320 }
321 
322 /**
323  * irdma_cleanup_retrans_entry - free send entry with lock
324  * @cm_node: connection's node
325  */
326 static void
327 irdma_cleanup_retrans_entry(struct irdma_cm_node *cm_node)
328 {
329 	unsigned long flags;
330 
331 	spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
332 	irdma_free_retrans_entry(cm_node);
333 	spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
334 }
335 
336 /**
337  * irdma_form_ah_cm_frame - get a free packet and build frame with address handle
338  * @cm_node: connection's node ionfo to use in frame
339  * @options: pointer to options info
340  * @hdr: pointer mpa header
341  * @pdata: pointer to private data
342  * @flags:  indicates FIN or ACK
343  */
344 static struct irdma_puda_buf *
345 irdma_form_ah_cm_frame(struct irdma_cm_node *cm_node,
346 		       struct irdma_kmem_info *options,
347 		       struct irdma_kmem_info *hdr,
348 		       struct irdma_mpa_priv_info *pdata,
349 		       u8 flags)
350 {
351 	struct irdma_puda_buf *sqbuf;
352 	struct irdma_sc_vsi *vsi = &cm_node->iwdev->vsi;
353 	u8 *buf;
354 	struct tcphdr *tcph;
355 	u16 pktsize;
356 	u32 opts_len = 0;
357 	u32 pd_len = 0;
358 	u32 hdr_len = 0;
359 
360 	if (!cm_node->ah || !cm_node->ah->ah_info.ah_valid) {
361 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "AH invalid\n");
362 		return NULL;
363 	}
364 
365 	sqbuf = irdma_puda_get_bufpool(vsi->ilq);
366 	if (!sqbuf) {
367 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "SQ buf NULL\n");
368 		return NULL;
369 	}
370 
371 	sqbuf->ah_id = cm_node->ah->ah_info.ah_idx;
372 	buf = sqbuf->mem.va;
373 	if (options)
374 		opts_len = (u32)options->size;
375 
376 	if (hdr)
377 		hdr_len = hdr->size;
378 
379 	if (pdata)
380 		pd_len = pdata->size;
381 
382 	pktsize = sizeof(*tcph) + opts_len + hdr_len + pd_len;
383 
384 	memset(buf, 0, pktsize);
385 
386 	sqbuf->totallen = pktsize;
387 	sqbuf->tcphlen = sizeof(*tcph) + opts_len;
388 	sqbuf->scratch = cm_node;
389 
390 	tcph = (struct tcphdr *)buf;
391 	buf += sizeof(*tcph);
392 
393 	tcph->th_sport = htons(cm_node->loc_port);
394 	tcph->th_dport = htons(cm_node->rem_port);
395 	tcph->th_seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
396 
397 	if (flags & SET_ACK) {
398 		cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
399 		tcph->th_ack = htonl(cm_node->tcp_cntxt.loc_ack_num);
400 		tcph->th_flags |= TH_ACK;
401 	} else {
402 		tcph->th_ack = 0;
403 	}
404 
405 	if (flags & SET_SYN) {
406 		cm_node->tcp_cntxt.loc_seq_num++;
407 		tcph->th_flags |= TH_SYN;
408 	} else {
409 		cm_node->tcp_cntxt.loc_seq_num += hdr_len + pd_len;
410 	}
411 
412 	if (flags & SET_FIN) {
413 		cm_node->tcp_cntxt.loc_seq_num++;
414 		tcph->th_flags |= TH_FIN;
415 	}
416 
417 	if (flags & SET_RST)
418 		tcph->th_flags |= TH_RST;
419 
420 	tcph->th_off = (u16)((sizeof(*tcph) + opts_len + 3) >> 2);
421 	sqbuf->tcphlen = tcph->th_off << 2;
422 	tcph->th_win = htons(cm_node->tcp_cntxt.rcv_wnd);
423 	tcph->th_urp = 0;
424 
425 	if (opts_len) {
426 		memcpy(buf, options->addr, opts_len);
427 		buf += opts_len;
428 	}
429 
430 	if (hdr_len) {
431 		memcpy(buf, hdr->addr, hdr_len);
432 		buf += hdr_len;
433 	}
434 
435 	if (pdata && pdata->addr)
436 		memcpy(buf, pdata->addr, pdata->size);
437 
438 	atomic_set(&sqbuf->refcount, 1);
439 
440 	irdma_debug_buf(vsi->dev, IRDMA_DEBUG_ILQ, "TRANSMIT ILQ BUFFER",
441 			sqbuf->mem.va, sqbuf->totallen);
442 
443 	return sqbuf;
444 }
445 
446 /**
447  * irdma_form_uda_cm_frame - get a free packet and build frame full tcpip packet
448  * @cm_node: connection's node ionfo to use in frame
449  * @options: pointer to options info
450  * @hdr: pointer mpa header
451  * @pdata: pointer to private data
452  * @flags:  indicates FIN or ACK
453  */
454 static struct irdma_puda_buf *
455 irdma_form_uda_cm_frame(struct irdma_cm_node *cm_node,
456 			struct irdma_kmem_info *options,
457 			struct irdma_kmem_info *hdr,
458 			struct irdma_mpa_priv_info *pdata,
459 			u8 flags)
460 {
461 	struct irdma_puda_buf *sqbuf;
462 	struct irdma_sc_vsi *vsi = &cm_node->iwdev->vsi;
463 	u8 *buf;
464 
465 	struct tcphdr *tcph;
466 	struct ip *iph;
467 	struct ip6_hdr *ip6h;
468 	struct ether_header *ethh;
469 	u16 pktsize;
470 	u16 eth_hlen = ETH_HLEN;
471 	u32 opts_len = 0;
472 	u32 pd_len = 0;
473 	u32 hdr_len = 0;
474 
475 	u16 vtag;
476 
477 	sqbuf = irdma_puda_get_bufpool(vsi->ilq);
478 	if (!sqbuf)
479 		return NULL;
480 
481 	buf = sqbuf->mem.va;
482 
483 	if (options)
484 		opts_len = (u32)options->size;
485 
486 	if (hdr)
487 		hdr_len = hdr->size;
488 
489 	if (pdata)
490 		pd_len = pdata->size;
491 
492 	if (cm_node->vlan_id < VLAN_N_VID)
493 		eth_hlen += 4;
494 
495 	if (cm_node->ipv4)
496 		pktsize = sizeof(*iph) + sizeof(*tcph);
497 	else
498 		pktsize = sizeof(*ip6h) + sizeof(*tcph);
499 	pktsize += opts_len + hdr_len + pd_len;
500 
501 	memset(buf, 0, eth_hlen + pktsize);
502 
503 	sqbuf->totallen = pktsize + eth_hlen;
504 	sqbuf->maclen = eth_hlen;
505 	sqbuf->tcphlen = sizeof(*tcph) + opts_len;
506 	sqbuf->scratch = cm_node;
507 
508 	ethh = (struct ether_header *)buf;
509 	buf += eth_hlen;
510 
511 	if (cm_node->do_lpb)
512 		sqbuf->do_lpb = true;
513 
514 	if (cm_node->ipv4) {
515 		sqbuf->ipv4 = true;
516 
517 		iph = (struct ip *)buf;
518 		buf += sizeof(*iph);
519 		tcph = (struct tcphdr *)buf;
520 		buf += sizeof(*tcph);
521 
522 		ether_addr_copy(ethh->ether_dhost, cm_node->rem_mac);
523 		ether_addr_copy(ethh->ether_shost, cm_node->loc_mac);
524 		if (cm_node->vlan_id < VLAN_N_VID) {
525 			((struct ether_vlan_header *)ethh)->evl_proto =
526 			    htons(ETH_P_8021Q);
527 			vtag = (cm_node->user_pri << VLAN_PRIO_SHIFT) |
528 			    cm_node->vlan_id;
529 			((struct ether_vlan_header *)ethh)->evl_tag = htons(vtag);
530 
531 			((struct ether_vlan_header *)ethh)->evl_encap_proto =
532 			    htons(ETH_P_IP);
533 		} else {
534 			ethh->ether_type = htons(ETH_P_IP);
535 		}
536 
537 		iph->ip_v = IPVERSION;
538 		iph->ip_hl = 5;	/* 5 * 4Byte words, IP headr len */
539 		iph->ip_tos = cm_node->tos;
540 		iph->ip_len = htons(pktsize);
541 		iph->ip_id = htons(++cm_node->tcp_cntxt.loc_id);
542 
543 		iph->ip_off = htons(0x4000);
544 		iph->ip_ttl = 0x40;
545 		iph->ip_p = IPPROTO_TCP;
546 		iph->ip_src.s_addr = htonl(cm_node->loc_addr[0]);
547 		iph->ip_dst.s_addr = htonl(cm_node->rem_addr[0]);
548 	} else {
549 		sqbuf->ipv4 = false;
550 		ip6h = (struct ip6_hdr *)buf;
551 		buf += sizeof(*ip6h);
552 		tcph = (struct tcphdr *)buf;
553 		buf += sizeof(*tcph);
554 
555 		ether_addr_copy(ethh->ether_dhost, cm_node->rem_mac);
556 		ether_addr_copy(ethh->ether_shost, cm_node->loc_mac);
557 		if (cm_node->vlan_id < VLAN_N_VID) {
558 			((struct ether_vlan_header *)ethh)->evl_proto =
559 			    htons(ETH_P_8021Q);
560 			vtag = (cm_node->user_pri << VLAN_PRIO_SHIFT) |
561 			    cm_node->vlan_id;
562 			((struct ether_vlan_header *)ethh)->evl_tag = htons(vtag);
563 			((struct ether_vlan_header *)ethh)->evl_encap_proto =
564 			    htons(ETH_P_IPV6);
565 		} else {
566 			ethh->ether_type = htons(ETH_P_IPV6);
567 		}
568 		ip6h->ip6_vfc = 6 << 4;
569 		ip6h->ip6_vfc |= cm_node->tos >> 4;
570 		ip6h->ip6_flow = cm_node->tos << 20;
571 		ip6h->ip6_plen = htons(pktsize - sizeof(*ip6h));
572 		ip6h->ip6_nxt = 6;
573 		ip6h->ip6_hops = 128;
574 		irdma_copy_ip_htonl(ip6h->ip6_src.__u6_addr.__u6_addr32,
575 				    cm_node->loc_addr);
576 		irdma_copy_ip_htonl(ip6h->ip6_dst.__u6_addr.__u6_addr32,
577 				    cm_node->rem_addr);
578 	}
579 
580 	tcph->th_sport = htons(cm_node->loc_port);
581 	tcph->th_dport = htons(cm_node->rem_port);
582 	tcph->th_seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
583 
584 	if (flags & SET_ACK) {
585 		cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
586 		tcph->th_ack = htonl(cm_node->tcp_cntxt.loc_ack_num);
587 		tcph->th_flags |= TH_ACK;
588 	} else {
589 		tcph->th_ack = 0;
590 	}
591 
592 	if (flags & SET_SYN) {
593 		cm_node->tcp_cntxt.loc_seq_num++;
594 		tcph->th_flags |= TH_SYN;
595 	} else {
596 		cm_node->tcp_cntxt.loc_seq_num += hdr_len + pd_len;
597 	}
598 
599 	if (flags & SET_FIN) {
600 		cm_node->tcp_cntxt.loc_seq_num++;
601 		tcph->th_flags |= TH_FIN;
602 	}
603 
604 	if (flags & SET_RST)
605 		tcph->th_flags |= TH_RST;
606 
607 	tcph->th_off = (u16)((sizeof(*tcph) + opts_len + 3) >> 2);
608 	sqbuf->tcphlen = tcph->th_off << 2;
609 	tcph->th_win = htons(cm_node->tcp_cntxt.rcv_wnd);
610 	tcph->th_urp = 0;
611 
612 	if (opts_len) {
613 		memcpy(buf, options->addr, opts_len);
614 		buf += opts_len;
615 	}
616 
617 	if (hdr_len) {
618 		memcpy(buf, hdr->addr, hdr_len);
619 		buf += hdr_len;
620 	}
621 
622 	if (pdata && pdata->addr)
623 		memcpy(buf, pdata->addr, pdata->size);
624 
625 	atomic_set(&sqbuf->refcount, 1);
626 
627 	irdma_debug_buf(vsi->dev, IRDMA_DEBUG_ILQ, "TRANSMIT ILQ BUFFER",
628 			sqbuf->mem.va, sqbuf->totallen);
629 
630 	return sqbuf;
631 }
632 
633 /**
634  * irdma_send_reset - Send RST packet
635  * @cm_node: connection's node
636  */
637 int
638 irdma_send_reset(struct irdma_cm_node *cm_node)
639 {
640 	struct irdma_puda_buf *sqbuf;
641 	int flags = SET_RST | SET_ACK;
642 
643 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, NULL, NULL, NULL,
644 						flags);
645 	if (!sqbuf)
646 		return -ENOMEM;
647 
648 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
649 		    "caller: %pS cm_node %p cm_id=%p accel=%d state=%d rem_port=0x%04x, loc_port=0x%04x rem_addr=%pI4 loc_addr=%pI4\n",
650 		    __builtin_return_address(0), cm_node, cm_node->cm_id,
651 		    cm_node->accelerated, cm_node->state, cm_node->rem_port,
652 		    cm_node->loc_port, cm_node->rem_addr, cm_node->loc_addr);
653 
654 	return irdma_schedule_cm_timer(cm_node, sqbuf, IRDMA_TIMER_TYPE_SEND, 0,
655 				       1);
656 }
657 
658 /**
659  * irdma_active_open_err - send event for active side cm error
660  * @cm_node: connection's node
661  * @reset: Flag to send reset or not
662  */
663 static void
664 irdma_active_open_err(struct irdma_cm_node *cm_node, bool reset)
665 {
666 	irdma_cleanup_retrans_entry(cm_node);
667 	cm_node->cm_core->stats_connect_errs++;
668 	if (reset) {
669 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
670 			    IRDMA_DEBUG_CM, "cm_node=%p state=%d\n", cm_node,
671 			    cm_node->state);
672 		atomic_inc(&cm_node->refcnt);
673 		irdma_send_reset(cm_node);
674 	}
675 
676 	cm_node->state = IRDMA_CM_STATE_CLOSED;
677 	irdma_create_event(cm_node, IRDMA_CM_EVENT_ABORTED);
678 }
679 
680 /**
681  * irdma_passive_open_err - handle passive side cm error
682  * @cm_node: connection's node
683  * @reset: send reset or just free cm_node
684  */
685 static void
686 irdma_passive_open_err(struct irdma_cm_node *cm_node, bool reset)
687 {
688 	irdma_cleanup_retrans_entry(cm_node);
689 	cm_node->cm_core->stats_passive_errs++;
690 	cm_node->state = IRDMA_CM_STATE_CLOSED;
691 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
692 		    "cm_node=%p state =%d\n",
693 		    cm_node, cm_node->state);
694 	if (reset)
695 		irdma_send_reset(cm_node);
696 	else
697 		irdma_rem_ref_cm_node(cm_node);
698 }
699 
700 /**
701  * irdma_event_connect_error - to create connect error event
702  * @event: cm information for connect event
703  */
704 static void
705 irdma_event_connect_error(struct irdma_cm_event *event)
706 {
707 	struct irdma_qp *iwqp;
708 	struct iw_cm_id *cm_id;
709 
710 	cm_id = event->cm_node->cm_id;
711 	if (!cm_id)
712 		return;
713 
714 	iwqp = cm_id->provider_data;
715 
716 	if (!iwqp || !iwqp->iwdev)
717 		return;
718 
719 	iwqp->cm_id = NULL;
720 	cm_id->provider_data = NULL;
721 	irdma_send_cm_event(event->cm_node, cm_id, IW_CM_EVENT_CONNECT_REPLY,
722 			    -ECONNRESET);
723 	irdma_rem_ref_cm_node(event->cm_node);
724 }
725 
726 /**
727  * irdma_process_options - process options from TCP header
728  * @cm_node: connection's node
729  * @optionsloc: point to start of options
730  * @optionsize: size of all options
731  * @syn_pkt: flag if syn packet
732  */
733 static int
734 irdma_process_options(struct irdma_cm_node *cm_node, u8 *optionsloc,
735 		      u32 optionsize, u32 syn_pkt)
736 {
737 	u32 tmp;
738 	u32 offset = 0;
739 	union all_known_options *all_options;
740 	char got_mss_option = 0;
741 
742 	while (offset < optionsize) {
743 		all_options = (union all_known_options *)(optionsloc + offset);
744 		switch (all_options->base.optionnum) {
745 		case OPTION_NUM_EOL:
746 			offset = optionsize;
747 			break;
748 		case OPTION_NUM_NONE:
749 			offset += 1;
750 			continue;
751 		case OPTION_NUM_MSS:
752 			irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
753 				    "MSS Length: %d Offset: %d Size: %d\n",
754 				    all_options->mss.len, offset, optionsize);
755 			got_mss_option = 1;
756 			if (all_options->mss.len != 4)
757 				return -EINVAL;
758 			tmp = ntohs(all_options->mss.mss);
759 			if ((cm_node->ipv4 &&
760 			     (tmp + IRDMA_MTU_TO_MSS_IPV4) < IRDMA_MIN_MTU_IPV4) ||
761 			    (!cm_node->ipv4 &&
762 			     (tmp + IRDMA_MTU_TO_MSS_IPV6) < IRDMA_MIN_MTU_IPV6))
763 				return -EINVAL;
764 			if (tmp < cm_node->tcp_cntxt.mss)
765 				cm_node->tcp_cntxt.mss = tmp;
766 			break;
767 		case OPTION_NUM_WINDOW_SCALE:
768 			cm_node->tcp_cntxt.snd_wscale =
769 			    all_options->windowscale.shiftcount;
770 			break;
771 		default:
772 			irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
773 				    "Unsupported TCP Option: %x\n",
774 				    all_options->base.optionnum);
775 			break;
776 		}
777 		offset += all_options->base.len;
778 	}
779 	if (!got_mss_option && syn_pkt)
780 		cm_node->tcp_cntxt.mss = IRDMA_CM_DEFAULT_MSS;
781 
782 	return 0;
783 }
784 
785 /**
786  * irdma_handle_tcp_options - setup TCP context info after parsing TCP options
787  * @cm_node: connection's node
788  * @tcph: pointer tcp header
789  * @optionsize: size of options rcvd
790  * @passive: active or passive flag
791  */
792 static int
793 irdma_handle_tcp_options(struct irdma_cm_node *cm_node,
794 			 struct tcphdr *tcph, int optionsize,
795 			 int passive)
796 {
797 	u8 *optionsloc = (u8 *)&tcph[1];
798 	int ret;
799 
800 	if (optionsize) {
801 		ret = irdma_process_options(cm_node, optionsloc, optionsize,
802 					    (u32)tcph->th_flags & TH_SYN);
803 		if (ret) {
804 			irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
805 				    "Node %p, Sending Reset\n",
806 				    cm_node);
807 			if (passive)
808 				irdma_passive_open_err(cm_node, true);
809 			else
810 				irdma_active_open_err(cm_node, true);
811 			return ret;
812 		}
813 	}
814 
815 	cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->th_win)
816 	    << cm_node->tcp_cntxt.snd_wscale;
817 
818 	if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
819 		cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
820 
821 	return 0;
822 }
823 
824 /**
825  * irdma_build_mpa_v1 - build a MPA V1 frame
826  * @cm_node: connection's node
827  * @start_addr: address where to build frame
828  * @mpa_key: to do read0 or write0
829  */
830 static void
831 irdma_build_mpa_v1(struct irdma_cm_node *cm_node, void *start_addr,
832 		   u8 mpa_key)
833 {
834 	struct ietf_mpa_v1 *mpa_frame = start_addr;
835 
836 	switch (mpa_key) {
837 	case MPA_KEY_REQUEST:
838 		memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
839 		break;
840 	case MPA_KEY_REPLY:
841 		memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
842 		break;
843 	default:
844 		break;
845 	}
846 	mpa_frame->flags = IETF_MPA_FLAGS_CRC;
847 	mpa_frame->rev = cm_node->mpa_frame_rev;
848 	mpa_frame->priv_data_len = htons(cm_node->pdata.size);
849 }
850 
851 /**
852  * irdma_build_mpa_v2 - build a MPA V2 frame
853  * @cm_node: connection's node
854  * @start_addr: buffer start address
855  * @mpa_key: to do read0 or write0
856  */
857 static void
858 irdma_build_mpa_v2(struct irdma_cm_node *cm_node, void *start_addr,
859 		   u8 mpa_key)
860 {
861 	struct ietf_mpa_v2 *mpa_frame = start_addr;
862 	struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
863 	u16 ctrl_ird, ctrl_ord;
864 
865 	/* initialize the upper 5 bytes of the frame */
866 	irdma_build_mpa_v1(cm_node, start_addr, mpa_key);
867 	mpa_frame->flags |= IETF_MPA_V2_FLAG;
868 	if (cm_node->iwdev->iw_ooo) {
869 		mpa_frame->flags |= IETF_MPA_FLAGS_MARKERS;
870 		cm_node->rcv_mark_en = true;
871 	}
872 	mpa_frame->priv_data_len = cpu_to_be16(be16_to_cpu(mpa_frame->priv_data_len) +
873 					       IETF_RTR_MSG_SIZE);
874 
875 	/* initialize RTR msg */
876 	if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
877 		ctrl_ird = IETF_NO_IRD_ORD;
878 		ctrl_ord = IETF_NO_IRD_ORD;
879 	} else {
880 		ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
881 		    IETF_NO_IRD_ORD :
882 		    cm_node->ird_size;
883 		ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
884 		    IETF_NO_IRD_ORD :
885 		    cm_node->ord_size;
886 	}
887 	ctrl_ird |= IETF_PEER_TO_PEER;
888 
889 	switch (mpa_key) {
890 	case MPA_KEY_REQUEST:
891 		ctrl_ord |= IETF_RDMA0_WRITE;
892 		ctrl_ord |= IETF_RDMA0_READ;
893 		break;
894 	case MPA_KEY_REPLY:
895 		switch (cm_node->send_rdma0_op) {
896 		case SEND_RDMA_WRITE_ZERO:
897 			ctrl_ord |= IETF_RDMA0_WRITE;
898 			break;
899 		case SEND_RDMA_READ_ZERO:
900 			ctrl_ord |= IETF_RDMA0_READ;
901 			break;
902 		}
903 		break;
904 	default:
905 		break;
906 	}
907 	rtr_msg->ctrl_ird = htons(ctrl_ird);
908 	rtr_msg->ctrl_ord = htons(ctrl_ord);
909 }
910 
911 /**
912  * irdma_cm_build_mpa_frame - build mpa frame for mpa version 1 or version 2
913  * @cm_node: connection's node
914  * @mpa: mpa: data buffer
915  * @mpa_key: to do read0 or write0
916  */
917 static int
918 irdma_cm_build_mpa_frame(struct irdma_cm_node *cm_node,
919 			 struct irdma_kmem_info *mpa, u8 mpa_key)
920 {
921 	int hdr_len = 0;
922 
923 	switch (cm_node->mpa_frame_rev) {
924 	case IETF_MPA_V1:
925 		hdr_len = sizeof(struct ietf_mpa_v1);
926 		irdma_build_mpa_v1(cm_node, mpa->addr, mpa_key);
927 		break;
928 	case IETF_MPA_V2:
929 		hdr_len = sizeof(struct ietf_mpa_v2);
930 		irdma_build_mpa_v2(cm_node, mpa->addr, mpa_key);
931 		break;
932 	default:
933 		break;
934 	}
935 
936 	return hdr_len;
937 }
938 
939 /**
940  * irdma_send_mpa_request - active node send mpa request to passive node
941  * @cm_node: connection's node
942  */
943 static int
944 irdma_send_mpa_request(struct irdma_cm_node *cm_node)
945 {
946 	struct irdma_puda_buf *sqbuf;
947 
948 	cm_node->mpa_hdr.addr = &cm_node->mpa_v2_frame;
949 	cm_node->mpa_hdr.size = irdma_cm_build_mpa_frame(cm_node,
950 							 &cm_node->mpa_hdr,
951 							 MPA_KEY_REQUEST);
952 	if (!cm_node->mpa_hdr.size) {
953 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
954 			    "mpa size = %d\n",
955 			    cm_node->mpa_hdr.size);
956 		return -EINVAL;
957 	}
958 
959 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, NULL,
960 						&cm_node->mpa_hdr,
961 						&cm_node->pdata, SET_ACK);
962 	if (!sqbuf)
963 		return -ENOMEM;
964 
965 	return irdma_schedule_cm_timer(cm_node, sqbuf, IRDMA_TIMER_TYPE_SEND, 1,
966 				       0);
967 }
968 
969 /**
970  * irdma_send_mpa_reject -
971  * @cm_node: connection's node
972  * @pdata: reject data for connection
973  * @plen: length of reject data
974  */
975 static int
976 irdma_send_mpa_reject(struct irdma_cm_node *cm_node,
977 		      const void *pdata, u8 plen)
978 {
979 	struct irdma_puda_buf *sqbuf;
980 	struct irdma_mpa_priv_info priv_info;
981 
982 	cm_node->mpa_hdr.addr = &cm_node->mpa_v2_frame;
983 	cm_node->mpa_hdr.size = irdma_cm_build_mpa_frame(cm_node,
984 							 &cm_node->mpa_hdr,
985 							 MPA_KEY_REPLY);
986 
987 	cm_node->mpa_v2_frame.flags |= IETF_MPA_FLAGS_REJECT;
988 	priv_info.addr = pdata;
989 	priv_info.size = plen;
990 
991 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, NULL,
992 						&cm_node->mpa_hdr, &priv_info,
993 						SET_ACK | SET_FIN);
994 	if (!sqbuf)
995 		return -ENOMEM;
996 
997 	cm_node->state = IRDMA_CM_STATE_FIN_WAIT1;
998 
999 	return irdma_schedule_cm_timer(cm_node, sqbuf, IRDMA_TIMER_TYPE_SEND, 1,
1000 				       0);
1001 }
1002 
1003 /**
1004  * irdma_negotiate_mpa_v2_ird_ord - negotiate MPAv2 IRD/ORD
1005  * @cm_node: connection's node
1006  * @buf: Data pointer
1007  */
1008 static int
1009 irdma_negotiate_mpa_v2_ird_ord(struct irdma_cm_node *cm_node,
1010 			       u8 *buf)
1011 {
1012 	struct ietf_mpa_v2 *mpa_v2_frame;
1013 	struct ietf_rtr_msg *rtr_msg;
1014 	u16 ird_size;
1015 	u16 ord_size;
1016 	u16 ctrl_ord;
1017 	u16 ctrl_ird;
1018 
1019 	mpa_v2_frame = (struct ietf_mpa_v2 *)buf;
1020 	rtr_msg = &mpa_v2_frame->rtr_msg;
1021 
1022 	/* parse rtr message */
1023 	ctrl_ord = ntohs(rtr_msg->ctrl_ord);
1024 	ctrl_ird = ntohs(rtr_msg->ctrl_ird);
1025 	ird_size = ctrl_ird & IETF_NO_IRD_ORD;
1026 	ord_size = ctrl_ord & IETF_NO_IRD_ORD;
1027 
1028 	if (!(ctrl_ird & IETF_PEER_TO_PEER))
1029 		return -EOPNOTSUPP;
1030 
1031 	if (ird_size == IETF_NO_IRD_ORD || ord_size == IETF_NO_IRD_ORD) {
1032 		cm_node->mpav2_ird_ord = IETF_NO_IRD_ORD;
1033 		goto negotiate_done;
1034 	}
1035 
1036 	if (cm_node->state != IRDMA_CM_STATE_MPAREQ_SENT) {
1037 		/* responder */
1038 		if (!ord_size && (ctrl_ord & IETF_RDMA0_READ))
1039 			cm_node->ird_size = 1;
1040 		if (cm_node->ord_size > ird_size)
1041 			cm_node->ord_size = ird_size;
1042 	} else {
1043 		/* initiator */
1044 		if (!ird_size && (ctrl_ord & IETF_RDMA0_READ))
1045 			/* Remote peer doesn't support RDMA0_READ */
1046 			return -EOPNOTSUPP;
1047 
1048 		if (cm_node->ord_size > ird_size)
1049 			cm_node->ord_size = ird_size;
1050 
1051 		if (cm_node->ird_size < ord_size)
1052 			/* no resources available */
1053 			return -EINVAL;
1054 	}
1055 
1056 negotiate_done:
1057 	if (ctrl_ord & IETF_RDMA0_READ)
1058 		cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1059 	else if (ctrl_ord & IETF_RDMA0_WRITE)
1060 		cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
1061 	else
1062 		/* Not supported RDMA0 operation */
1063 		return -EOPNOTSUPP;
1064 
1065 	irdma_debug(&cm_node->iwdev->rf->sc_dev,
1066 		    IRDMA_DEBUG_CM, "MPAV2 Negotiated ORD: %d, IRD: %d\n",
1067 		    cm_node->ord_size, cm_node->ird_size);
1068 	return 0;
1069 }
1070 
1071 /**
1072  * irdma_parse_mpa - process an IETF MPA frame
1073  * @cm_node: connection's node
1074  * @buf: Data pointer
1075  * @type: to return accept or reject
1076  * @len: Len of mpa buffer
1077  */
1078 static int
1079 irdma_parse_mpa(struct irdma_cm_node *cm_node, u8 *buf, u32 *type,
1080 		u32 len)
1081 {
1082 	struct ietf_mpa_v1 *mpa_frame;
1083 	int mpa_hdr_len, priv_data_len, ret;
1084 
1085 	*type = IRDMA_MPA_REQUEST_ACCEPT;
1086 
1087 	if (len < sizeof(struct ietf_mpa_v1)) {
1088 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
1089 			    IRDMA_DEBUG_CM, "ietf buffer small (%x)\n", len);
1090 		return -EINVAL;
1091 	}
1092 
1093 	mpa_frame = (struct ietf_mpa_v1 *)buf;
1094 	mpa_hdr_len = sizeof(struct ietf_mpa_v1);
1095 	priv_data_len = ntohs(mpa_frame->priv_data_len);
1096 
1097 	if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
1098 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1099 			    "private_data too big %d\n",
1100 			    priv_data_len);
1101 		return -EOVERFLOW;
1102 	}
1103 
1104 	if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
1105 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1106 			    "unsupported mpa rev = %d\n",
1107 			    mpa_frame->rev);
1108 		return -EINVAL;
1109 	}
1110 
1111 	if (mpa_frame->rev > cm_node->mpa_frame_rev) {
1112 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1113 			    "rev %d\n",
1114 			    mpa_frame->rev);
1115 		return -EINVAL;
1116 	}
1117 
1118 	cm_node->mpa_frame_rev = mpa_frame->rev;
1119 	if (cm_node->state != IRDMA_CM_STATE_MPAREQ_SENT) {
1120 		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ,
1121 			   IETF_MPA_KEY_SIZE)) {
1122 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
1123 				    IRDMA_DEBUG_CM, "Unexpected MPA Key received\n");
1124 			return -EINVAL;
1125 		}
1126 	} else {
1127 		if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP,
1128 			   IETF_MPA_KEY_SIZE)) {
1129 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
1130 				    IRDMA_DEBUG_CM, "Unexpected MPA Key received\n");
1131 			return -EINVAL;
1132 		}
1133 	}
1134 
1135 	if (priv_data_len + mpa_hdr_len > len) {
1136 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
1137 			    IRDMA_DEBUG_CM, "ietf buffer len(%x + %x != %x)\n",
1138 			    priv_data_len, mpa_hdr_len, len);
1139 		return -EOVERFLOW;
1140 	}
1141 
1142 	if (len > IRDMA_MAX_CM_BUF) {
1143 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
1144 			    IRDMA_DEBUG_CM, "ietf buffer large len = %d\n", len);
1145 		return -EOVERFLOW;
1146 	}
1147 
1148 	switch (mpa_frame->rev) {
1149 	case IETF_MPA_V2:
1150 		mpa_hdr_len += IETF_RTR_MSG_SIZE;
1151 		ret = irdma_negotiate_mpa_v2_ird_ord(cm_node, buf);
1152 		if (ret)
1153 			return ret;
1154 		break;
1155 	case IETF_MPA_V1:
1156 	default:
1157 		break;
1158 	}
1159 
1160 	memcpy(cm_node->pdata_buf, buf + mpa_hdr_len, priv_data_len);
1161 	cm_node->pdata.size = priv_data_len;
1162 
1163 	if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
1164 		*type = IRDMA_MPA_REQUEST_REJECT;
1165 
1166 	if (mpa_frame->flags & IETF_MPA_FLAGS_MARKERS)
1167 		cm_node->snd_mark_en = true;
1168 
1169 	return 0;
1170 }
1171 
1172 /**
1173  * irdma_schedule_cm_timer
1174  * @cm_node: connection's node
1175  * @sqbuf: buffer to send
1176  * @type: if it is send or close
1177  * @send_retrans: if rexmits to be done
1178  * @close_when_complete: is cm_node to be removed
1179  *
1180  * note - cm_node needs to be protected before calling this. Encase in:
1181  *		irdma_rem_ref_cm_node(cm_core, cm_node);
1182  *		irdma_schedule_cm_timer(...)
1183  *		atomic_inc(&cm_node->refcnt);
1184  */
1185 int
1186 irdma_schedule_cm_timer(struct irdma_cm_node *cm_node,
1187 			struct irdma_puda_buf *sqbuf,
1188 			enum irdma_timer_type type, int send_retrans,
1189 			int close_when_complete)
1190 {
1191 	struct irdma_sc_vsi *vsi = &cm_node->iwdev->vsi;
1192 	struct irdma_cm_core *cm_core = cm_node->cm_core;
1193 	struct irdma_timer_entry *new_send;
1194 	u32 was_timer_set;
1195 	unsigned long flags;
1196 
1197 	new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
1198 	if (!new_send) {
1199 		if (type != IRDMA_TIMER_TYPE_CLOSE)
1200 			irdma_free_sqbuf(vsi, sqbuf);
1201 		return -ENOMEM;
1202 	}
1203 
1204 	new_send->retrycount = IRDMA_DEFAULT_RETRYS;
1205 	new_send->retranscount = IRDMA_DEFAULT_RETRANS;
1206 	new_send->sqbuf = sqbuf;
1207 	new_send->timetosend = jiffies;
1208 	new_send->type = type;
1209 	new_send->send_retrans = send_retrans;
1210 	new_send->close_when_complete = close_when_complete;
1211 
1212 	if (type == IRDMA_TIMER_TYPE_CLOSE) {
1213 		new_send->timetosend += (HZ / 10);
1214 		if (cm_node->close_entry) {
1215 			kfree(new_send);
1216 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
1217 				    IRDMA_DEBUG_CM, "already close entry\n");
1218 			return -EINVAL;
1219 		}
1220 
1221 		cm_node->close_entry = new_send;
1222 	} else {		/* type == IRDMA_TIMER_TYPE_SEND */
1223 		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1224 		cm_node->send_entry = new_send;
1225 		atomic_inc(&cm_node->refcnt);
1226 		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1227 		new_send->timetosend = jiffies + IRDMA_RETRY_TIMEOUT;
1228 
1229 		atomic_inc(&sqbuf->refcount);
1230 		irdma_puda_send_buf(vsi->ilq, sqbuf);
1231 		if (!send_retrans) {
1232 			irdma_cleanup_retrans_entry(cm_node);
1233 			if (close_when_complete)
1234 				irdma_rem_ref_cm_node(cm_node);
1235 			return 0;
1236 		}
1237 	}
1238 
1239 	spin_lock_irqsave(&cm_core->ht_lock, flags);
1240 	was_timer_set = timer_pending(&cm_core->tcp_timer);
1241 
1242 	if (!was_timer_set) {
1243 		cm_core->tcp_timer.expires = new_send->timetosend;
1244 		add_timer(&cm_core->tcp_timer);
1245 	}
1246 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1247 
1248 	return 0;
1249 }
1250 
1251 /**
1252  * irdma_retrans_expired - Could not rexmit the packet
1253  * @cm_node: connection's node
1254  */
1255 static void
1256 irdma_retrans_expired(struct irdma_cm_node *cm_node)
1257 {
1258 	enum irdma_cm_node_state state = cm_node->state;
1259 
1260 	cm_node->state = IRDMA_CM_STATE_CLOSED;
1261 	switch (state) {
1262 	case IRDMA_CM_STATE_SYN_RCVD:
1263 	case IRDMA_CM_STATE_CLOSING:
1264 		irdma_rem_ref_cm_node(cm_node);
1265 		break;
1266 	case IRDMA_CM_STATE_FIN_WAIT1:
1267 	case IRDMA_CM_STATE_LAST_ACK:
1268 		irdma_send_reset(cm_node);
1269 		break;
1270 	default:
1271 		atomic_inc(&cm_node->refcnt);
1272 		irdma_send_reset(cm_node);
1273 		irdma_create_event(cm_node, IRDMA_CM_EVENT_ABORTED);
1274 		break;
1275 	}
1276 }
1277 
1278 /**
1279  * irdma_handle_close_entry - for handling retry/timeouts
1280  * @cm_node: connection's node
1281  * @rem_node: flag for remove cm_node
1282  */
1283 static void
1284 irdma_handle_close_entry(struct irdma_cm_node *cm_node,
1285 			 u32 rem_node)
1286 {
1287 	struct irdma_timer_entry *close_entry = cm_node->close_entry;
1288 	struct irdma_qp *iwqp;
1289 	unsigned long flags;
1290 
1291 	if (!close_entry)
1292 		return;
1293 	iwqp = (struct irdma_qp *)close_entry->sqbuf;
1294 	if (iwqp) {
1295 		spin_lock_irqsave(&iwqp->lock, flags);
1296 		if (iwqp->cm_id) {
1297 			iwqp->hw_tcp_state = IRDMA_TCP_STATE_CLOSED;
1298 			iwqp->hw_iwarp_state = IRDMA_QP_STATE_ERROR;
1299 			iwqp->last_aeq = IRDMA_AE_RESET_SENT;
1300 			iwqp->ibqp_state = IB_QPS_ERR;
1301 			spin_unlock_irqrestore(&iwqp->lock, flags);
1302 			irdma_cm_disconn(iwqp);
1303 		} else {
1304 			spin_unlock_irqrestore(&iwqp->lock, flags);
1305 		}
1306 	} else if (rem_node) {
1307 		/* TIME_WAIT state */
1308 		irdma_rem_ref_cm_node(cm_node);
1309 	}
1310 
1311 	kfree(close_entry);
1312 	cm_node->close_entry = NULL;
1313 }
1314 
1315 /**
1316  * irdma_cm_timer_tick - system's timer expired callback
1317  * @t: Pointer to timer_list
1318  */
1319 static void
1320 irdma_cm_timer_tick(struct timer_list *t)
1321 {
1322 	unsigned long nexttimeout = jiffies + IRDMA_LONG_TIME;
1323 	struct irdma_cm_node *cm_node;
1324 	struct irdma_timer_entry *send_entry, *close_entry;
1325 	struct list_head *list_core_temp;
1326 	struct list_head *list_node;
1327 	struct irdma_cm_core *cm_core = from_timer(cm_core, t, tcp_timer);
1328 	struct irdma_sc_vsi *vsi;
1329 	u32 settimer = 0;
1330 	unsigned long timetosend;
1331 	unsigned long flags;
1332 	struct list_head timer_list;
1333 
1334 	INIT_LIST_HEAD(&timer_list);
1335 
1336 	rcu_read_lock();
1337 	irdma_timer_list_prep(cm_core, &timer_list);
1338 	rcu_read_unlock();
1339 
1340 	list_for_each_safe(list_node, list_core_temp, &timer_list) {
1341 		cm_node = container_of(list_node, struct irdma_cm_node,
1342 				       timer_entry);
1343 		close_entry = cm_node->close_entry;
1344 
1345 		if (close_entry) {
1346 			if (time_after(close_entry->timetosend, jiffies)) {
1347 				if (nexttimeout > close_entry->timetosend ||
1348 				    !settimer) {
1349 					nexttimeout = close_entry->timetosend;
1350 					settimer = 1;
1351 				}
1352 			} else {
1353 				irdma_handle_close_entry(cm_node, 1);
1354 			}
1355 		}
1356 
1357 		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1358 
1359 		send_entry = cm_node->send_entry;
1360 		if (!send_entry)
1361 			goto done;
1362 		if (time_after(send_entry->timetosend, jiffies)) {
1363 			if (cm_node->state != IRDMA_CM_STATE_OFFLOADED) {
1364 				if (nexttimeout > send_entry->timetosend ||
1365 				    !settimer) {
1366 					nexttimeout = send_entry->timetosend;
1367 					settimer = 1;
1368 				}
1369 			} else {
1370 				irdma_free_retrans_entry(cm_node);
1371 			}
1372 			goto done;
1373 		}
1374 
1375 		if (cm_node->state == IRDMA_CM_STATE_OFFLOADED ||
1376 		    cm_node->state == IRDMA_CM_STATE_CLOSED) {
1377 			irdma_free_retrans_entry(cm_node);
1378 			goto done;
1379 		}
1380 
1381 		if (!send_entry->retranscount || !send_entry->retrycount) {
1382 			irdma_free_retrans_entry(cm_node);
1383 
1384 			spin_unlock_irqrestore(&cm_node->retrans_list_lock,
1385 					       flags);
1386 			irdma_retrans_expired(cm_node);
1387 			cm_node->state = IRDMA_CM_STATE_CLOSED;
1388 			spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1389 			goto done;
1390 		}
1391 		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1392 
1393 		vsi = &cm_node->iwdev->vsi;
1394 		if (!cm_node->ack_rcvd) {
1395 			atomic_inc(&send_entry->sqbuf->refcount);
1396 			irdma_puda_send_buf(vsi->ilq, send_entry->sqbuf);
1397 			cm_node->cm_core->stats_pkt_retrans++;
1398 		}
1399 
1400 		spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1401 		if (send_entry->send_retrans) {
1402 			send_entry->retranscount--;
1403 			timetosend = (IRDMA_RETRY_TIMEOUT <<
1404 				      (IRDMA_DEFAULT_RETRANS -
1405 				       send_entry->retranscount));
1406 
1407 			send_entry->timetosend = jiffies +
1408 			    min(timetosend, IRDMA_MAX_TIMEOUT);
1409 			if (nexttimeout > send_entry->timetosend || !settimer) {
1410 				nexttimeout = send_entry->timetosend;
1411 				settimer = 1;
1412 			}
1413 		} else {
1414 			int close_when_complete;
1415 
1416 			close_when_complete = send_entry->close_when_complete;
1417 			irdma_free_retrans_entry(cm_node);
1418 			if (close_when_complete)
1419 				irdma_rem_ref_cm_node(cm_node);
1420 		}
1421 done:
1422 		spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1423 		irdma_rem_ref_cm_node(cm_node);
1424 	}
1425 
1426 	if (settimer) {
1427 		spin_lock_irqsave(&cm_core->ht_lock, flags);
1428 		if (!timer_pending(&cm_core->tcp_timer)) {
1429 			cm_core->tcp_timer.expires = nexttimeout;
1430 			add_timer(&cm_core->tcp_timer);
1431 		}
1432 		spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1433 	}
1434 }
1435 
1436 /**
1437  * irdma_send_syn - send SYN packet
1438  * @cm_node: connection's node
1439  * @sendack: flag to set ACK bit or not
1440  */
1441 int
1442 irdma_send_syn(struct irdma_cm_node *cm_node, u32 sendack)
1443 {
1444 	struct irdma_puda_buf *sqbuf;
1445 	int flags = SET_SYN;
1446 	char optionsbuf[sizeof(struct option_mss) +
1447 			sizeof(struct option_windowscale) +
1448 			sizeof(struct option_base) + TCP_OPTIONS_PADDING];
1449 	struct irdma_kmem_info opts;
1450 	int optionssize = 0;
1451 	/* Sending MSS option */
1452 	union all_known_options *options;
1453 
1454 	opts.addr = optionsbuf;
1455 	if (!cm_node)
1456 		return -EINVAL;
1457 
1458 	options = (union all_known_options *)&optionsbuf[optionssize];
1459 	options->mss.optionnum = OPTION_NUM_MSS;
1460 	options->mss.len = sizeof(struct option_mss);
1461 	options->mss.mss = htons(cm_node->tcp_cntxt.mss);
1462 	optionssize += sizeof(struct option_mss);
1463 
1464 	options = (union all_known_options *)&optionsbuf[optionssize];
1465 	options->windowscale.optionnum = OPTION_NUM_WINDOW_SCALE;
1466 	options->windowscale.len = sizeof(struct option_windowscale);
1467 	options->windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1468 	optionssize += sizeof(struct option_windowscale);
1469 	options = (union all_known_options *)&optionsbuf[optionssize];
1470 	options->eol = OPTION_NUM_EOL;
1471 	optionssize += 1;
1472 
1473 	if (sendack)
1474 		flags |= SET_ACK;
1475 
1476 	opts.size = optionssize;
1477 
1478 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, &opts, NULL, NULL,
1479 						flags);
1480 	if (!sqbuf)
1481 		return -ENOMEM;
1482 
1483 	return irdma_schedule_cm_timer(cm_node, sqbuf, IRDMA_TIMER_TYPE_SEND, 1,
1484 				       0);
1485 }
1486 
1487 /**
1488  * irdma_send_ack - Send ACK packet
1489  * @cm_node: connection's node
1490  */
1491 void
1492 irdma_send_ack(struct irdma_cm_node *cm_node)
1493 {
1494 	struct irdma_puda_buf *sqbuf;
1495 	struct irdma_sc_vsi *vsi = &cm_node->iwdev->vsi;
1496 
1497 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, NULL, NULL, NULL,
1498 						SET_ACK);
1499 	if (sqbuf)
1500 		irdma_puda_send_buf(vsi->ilq, sqbuf);
1501 }
1502 
1503 /**
1504  * irdma_send_fin - Send FIN pkt
1505  * @cm_node: connection's node
1506  */
1507 static int
1508 irdma_send_fin(struct irdma_cm_node *cm_node)
1509 {
1510 	struct irdma_puda_buf *sqbuf;
1511 
1512 	sqbuf = cm_node->cm_core->form_cm_frame(cm_node, NULL, NULL, NULL,
1513 						SET_ACK | SET_FIN);
1514 	if (!sqbuf)
1515 		return -ENOMEM;
1516 
1517 	return irdma_schedule_cm_timer(cm_node, sqbuf, IRDMA_TIMER_TYPE_SEND, 1,
1518 				       0);
1519 }
1520 
1521 /**
1522  * irdma_find_listener - find a cm node listening on this addr-port pair
1523  * @cm_core: cm's core
1524  * @dst_addr: listener ip addr
1525  * @ipv4: flag indicating IPv4 when true
1526  * @dst_port: listener tcp port num
1527  * @vlan_id: virtual LAN ID
1528  * @listener_state: state to match with listen node's
1529  */
1530 static struct irdma_cm_listener *
1531 irdma_find_listener(struct irdma_cm_core *cm_core, u32 *dst_addr, bool ipv4, u16 dst_port,
1532 		    u16 vlan_id, enum irdma_cm_listener_state listener_state)
1533 {
1534 	struct irdma_cm_listener *listen_node;
1535 	static const u32 ip_zero[4] = {0, 0, 0, 0};
1536 	u32 listen_addr[4];
1537 	u16 listen_port;
1538 	unsigned long flags;
1539 
1540 	/* walk list and find cm_node associated with this session ID */
1541 	spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1542 	list_for_each_entry(listen_node, &cm_core->listen_list, list) {
1543 		memcpy(listen_addr, listen_node->loc_addr, sizeof(listen_addr));
1544 		listen_port = listen_node->loc_port;
1545 		if (listen_node->ipv4 != ipv4 || listen_port != dst_port ||
1546 		    !(listener_state & listen_node->listener_state))
1547 			continue;
1548 		/* compare node pair, return node handle if a match */
1549 		if (!memcmp(listen_addr, ip_zero, sizeof(listen_addr)) ||
1550 		    (!memcmp(listen_addr, dst_addr, sizeof(listen_addr)) &&
1551 		     vlan_id == listen_node->vlan_id)) {
1552 			atomic_inc(&listen_node->refcnt);
1553 			spin_unlock_irqrestore(&cm_core->listen_list_lock,
1554 					       flags);
1555 			return listen_node;
1556 		}
1557 	}
1558 	spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1559 
1560 	return NULL;
1561 }
1562 
1563 /**
1564  * irdma_del_multiple_qhash - Remove qhash and child listens
1565  * @iwdev: iWarp device
1566  * @cm_info: CM info for parent listen node
1567  * @cm_parent_listen_node: The parent listen node
1568  */
1569 static int
1570 irdma_del_multiple_qhash(struct irdma_device *iwdev,
1571 			 struct irdma_cm_info *cm_info,
1572 			 struct irdma_cm_listener *cm_parent_listen_node)
1573 {
1574 	struct irdma_cm_listener *child_listen_node;
1575 	struct list_head *pos, *tpos;
1576 	unsigned long flags;
1577 	int ret = -EINVAL;
1578 
1579 	spin_lock_irqsave(&iwdev->cm_core.listen_list_lock, flags);
1580 	list_for_each_safe(pos, tpos,
1581 			   &cm_parent_listen_node->child_listen_list) {
1582 		child_listen_node = list_entry(pos, struct irdma_cm_listener,
1583 					       child_listen_list);
1584 		if (child_listen_node->ipv4)
1585 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1586 				    "removing child listen for IP=%pI4, port=%d, vlan=%d\n",
1587 				    child_listen_node->loc_addr,
1588 				    child_listen_node->loc_port,
1589 				    child_listen_node->vlan_id);
1590 		else
1591 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1592 				    "removing child listen for IP=%pI6, port=%d, vlan=%d\n",
1593 				    child_listen_node->loc_addr,
1594 				    child_listen_node->loc_port,
1595 				    child_listen_node->vlan_id);
1596 		list_del(pos);
1597 		memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1598 		       sizeof(cm_info->loc_addr));
1599 		cm_info->vlan_id = child_listen_node->vlan_id;
1600 		if (child_listen_node->qhash_set) {
1601 			ret = irdma_manage_qhash(iwdev, cm_info,
1602 						 IRDMA_QHASH_TYPE_TCP_SYN,
1603 						 IRDMA_QHASH_MANAGE_TYPE_DELETE,
1604 						 NULL, false);
1605 			child_listen_node->qhash_set = false;
1606 		} else {
1607 			ret = 0;
1608 		}
1609 		irdma_debug(&iwdev->rf->sc_dev,
1610 			    IRDMA_DEBUG_CM, "Child listen node freed = %p\n",
1611 			    child_listen_node);
1612 		kfree(child_listen_node);
1613 		cm_parent_listen_node->cm_core->stats_listen_nodes_destroyed++;
1614 	}
1615 	spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1616 
1617 	return ret;
1618 }
1619 
1620 static u8 irdma_get_egress_vlan_prio(u32 *loc_addr, u8 prio, bool ipv4){
1621 	return prio;
1622 }
1623 
1624 /**
1625  * irdma_netdev_vlan_ipv6 - Gets the netdev and mac
1626  * @addr: local IPv6 address
1627  * @vlan_id: vlan id for the given IPv6 address
1628  * @mac: mac address for the given IPv6 address
1629  *
1630  * Returns the net_device of the IPv6 address and also sets the
1631  * vlan id and mac for that address.
1632  */
1633 struct ifnet *
1634 irdma_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
1635 {
1636 	struct ifnet *ip_dev = NULL;
1637 	struct in6_addr laddr6;
1638 	u16 scope_id = 0;
1639 
1640 	irdma_copy_ip_htonl(laddr6.__u6_addr.__u6_addr32, addr);
1641 	if (vlan_id)
1642 		*vlan_id = 0xFFFF;	/* Match rdma_vlan_dev_vlan_id() */
1643 	if (mac)
1644 		eth_zero_addr(mac);
1645 
1646 	if (IN6_IS_SCOPE_LINKLOCAL(&laddr6) ||
1647 	    IN6_IS_ADDR_MC_INTFACELOCAL(&laddr6))
1648 		scope_id = ntohs(laddr6.__u6_addr.__u6_addr16[1]);
1649 
1650 	ip_dev = ip6_ifp_find(&init_net, laddr6, scope_id);
1651 	if (ip_dev) {
1652 		if (vlan_id)
1653 			*vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
1654 		if (ip_dev->if_addr && ip_dev->if_addr->ifa_addr && mac)
1655 			ether_addr_copy(mac, IF_LLADDR(ip_dev));
1656 	}
1657 
1658 	return ip_dev;
1659 }
1660 
1661 /**
1662  * irdma_get_vlan_ipv4 - Returns the vlan_id for IPv4 address
1663  * @addr: local IPv4 address
1664  */
1665 u16
1666 irdma_get_vlan_ipv4(u32 *addr)
1667 {
1668 	struct ifnet *netdev;
1669 	u16 vlan_id = 0xFFFF;
1670 
1671 	netdev = ip_ifp_find(&init_net, htonl(addr[0]));
1672 	if (netdev) {
1673 		vlan_id = rdma_vlan_dev_vlan_id(netdev);
1674 		dev_put(netdev);
1675 	}
1676 
1677 	return vlan_id;
1678 }
1679 
1680 /**
1681  * irdma_add_mqh_6 - Adds multiple qhashes for IPv6
1682  * @iwdev: iWarp device
1683  * @cm_info: CM info for parent listen node
1684  * @cm_parent_listen_node: The parent listen node
1685  *
1686  * Adds a qhash and a child listen node for every IPv6 address
1687  * on the adapter and adds the associated qhash filter
1688  */
1689 static int
1690 irdma_add_mqh_6(struct irdma_device *iwdev,
1691 		struct irdma_cm_info *cm_info,
1692 		struct irdma_cm_listener *cm_parent_listen_node)
1693 {
1694 	struct ifnet *ip_dev;
1695 	struct ifaddr *ifp;
1696 	struct irdma_cm_listener *child_listen_node;
1697 	unsigned long flags;
1698 	int ret = 0;
1699 
1700 	IFNET_RLOCK();
1701 	IRDMA_TAILQ_FOREACH((ip_dev), &V_ifnet, if_link) {
1702 		if (!(ip_dev->if_flags & IFF_UP))
1703 			continue;
1704 
1705 		if (((rdma_vlan_dev_vlan_id(ip_dev) >= VLAN_N_VID) ||
1706 		     (rdma_vlan_dev_real_dev(ip_dev) != iwdev->netdev)) &&
1707 		    ip_dev != iwdev->netdev)
1708 			continue;
1709 
1710 		if_addr_rlock(ip_dev);
1711 		IRDMA_TAILQ_FOREACH(ifp, &ip_dev->if_addrhead, ifa_link) {
1712 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1713 				    "IP=%pI6, vlan_id=%d, MAC=%pM\n",
1714 				    &((struct sockaddr_in6 *)ifp->ifa_addr)->sin6_addr, rdma_vlan_dev_vlan_id(ip_dev),
1715 				    IF_LLADDR(ip_dev));
1716 			if (((struct sockaddr_in6 *)ifp->ifa_addr)->sin6_family != AF_INET6)
1717 				continue;
1718 			child_listen_node = kzalloc(sizeof(*child_listen_node), GFP_KERNEL);
1719 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1720 				    "Allocating child listener %p\n",
1721 				    child_listen_node);
1722 			if (!child_listen_node) {
1723 				irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "listener memory allocation\n");
1724 				ret = -ENOMEM;
1725 				if_addr_runlock(ip_dev);
1726 				goto exit;
1727 			}
1728 
1729 			memcpy(child_listen_node, cm_parent_listen_node,
1730 			       sizeof(*child_listen_node));
1731 			cm_info->vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
1732 			child_listen_node->vlan_id = cm_info->vlan_id;
1733 			irdma_copy_ip_ntohl(child_listen_node->loc_addr,
1734 					    ((struct sockaddr_in6 *)ifp->ifa_addr)->sin6_addr.__u6_addr.__u6_addr32);
1735 			memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1736 			       sizeof(cm_info->loc_addr));
1737 			if (!iwdev->vsi.dscp_mode)
1738 				cm_info->user_pri =
1739 				    irdma_get_egress_vlan_prio(child_listen_node->loc_addr,
1740 							       cm_info->user_pri,
1741 							       false);
1742 			ret = irdma_manage_qhash(iwdev, cm_info,
1743 						 IRDMA_QHASH_TYPE_TCP_SYN,
1744 						 IRDMA_QHASH_MANAGE_TYPE_ADD,
1745 						 NULL, true);
1746 			if (ret) {
1747 				kfree(child_listen_node);
1748 				continue;
1749 			}
1750 
1751 			child_listen_node->qhash_set = true;
1752 			spin_lock_irqsave(&iwdev->cm_core.listen_list_lock, flags);
1753 			list_add(&child_listen_node->child_listen_list,
1754 				 &cm_parent_listen_node->child_listen_list);
1755 			spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1756 			cm_parent_listen_node->cm_core->stats_listen_nodes_created++;
1757 		}
1758 		if_addr_runlock(ip_dev);
1759 	}
1760 exit:
1761 	IFNET_RUNLOCK();
1762 
1763 	return ret;
1764 }
1765 
1766 /**
1767  * irdma_add_mqh_4 - Adds multiple qhashes for IPv4
1768  * @iwdev: iWarp device
1769  * @cm_info: CM info for parent listen node
1770  * @cm_parent_listen_node: The parent listen node
1771  *
1772  * Adds a qhash and a child listen node for every IPv4 address
1773  * on the adapter and adds the associated qhash filter
1774  */
1775 static int
1776 irdma_add_mqh_4(struct irdma_device *iwdev,
1777 		struct irdma_cm_info *cm_info,
1778 		struct irdma_cm_listener *cm_parent_listen_node)
1779 {
1780 	struct ifnet *ip_dev;
1781 	struct irdma_cm_listener *child_listen_node;
1782 	unsigned long flags;
1783 	struct ifaddr *ifa;
1784 	int ret = 0;
1785 
1786 	IFNET_RLOCK();
1787 	IRDMA_TAILQ_FOREACH((ip_dev), &V_ifnet, if_link) {
1788 		if (!(ip_dev->if_flags & IFF_UP))
1789 			continue;
1790 
1791 		if (((rdma_vlan_dev_vlan_id(ip_dev) >= VLAN_N_VID) ||
1792 		     (rdma_vlan_dev_real_dev(ip_dev) != iwdev->netdev)) &&
1793 		    ip_dev != iwdev->netdev)
1794 			continue;
1795 
1796 		if_addr_rlock(ip_dev);
1797 		IRDMA_TAILQ_FOREACH(ifa, &ip_dev->if_addrhead, ifa_link) {
1798 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1799 				    "Allocating child CM Listener forIP=%pI4, vlan_id=%d, MAC=%pM\n",
1800 				    &ifa->ifa_addr, rdma_vlan_dev_vlan_id(ip_dev),
1801 				    IF_LLADDR(ip_dev));
1802 			if (((struct sockaddr_in *)ifa->ifa_addr)->sin_family != AF_INET)
1803 				continue;
1804 			child_listen_node = kzalloc(sizeof(*child_listen_node), GFP_KERNEL);
1805 			cm_parent_listen_node->cm_core->stats_listen_nodes_created++;
1806 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1807 				    "Allocating child listener %p\n",
1808 				    child_listen_node);
1809 			if (!child_listen_node) {
1810 				irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "listener memory allocation\n");
1811 				if_addr_runlock(ip_dev);
1812 				ret = -ENOMEM;
1813 				goto exit;
1814 			}
1815 
1816 			memcpy(child_listen_node, cm_parent_listen_node,
1817 			       sizeof(*child_listen_node));
1818 			child_listen_node->vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
1819 			cm_info->vlan_id = child_listen_node->vlan_id;
1820 			child_listen_node->loc_addr[0] =
1821 			    ntohl(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr);
1822 			memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1823 			       sizeof(cm_info->loc_addr));
1824 			if (!iwdev->vsi.dscp_mode)
1825 				cm_info->user_pri =
1826 				    irdma_get_egress_vlan_prio(child_listen_node->loc_addr,
1827 							       cm_info->user_pri,
1828 							       true);
1829 			ret = irdma_manage_qhash(iwdev, cm_info,
1830 						 IRDMA_QHASH_TYPE_TCP_SYN,
1831 						 IRDMA_QHASH_MANAGE_TYPE_ADD,
1832 						 NULL, true);
1833 			if (ret) {
1834 				kfree(child_listen_node);
1835 				cm_parent_listen_node->cm_core
1836 				    ->stats_listen_nodes_created--;
1837 				continue;
1838 			}
1839 
1840 			child_listen_node->qhash_set = true;
1841 			spin_lock_irqsave(&iwdev->cm_core.listen_list_lock,
1842 					  flags);
1843 			list_add(&child_listen_node->child_listen_list,
1844 				 &cm_parent_listen_node->child_listen_list);
1845 			spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1846 		}
1847 		if_addr_runlock(ip_dev);
1848 	}
1849 exit:
1850 	IFNET_RUNLOCK();
1851 
1852 	return ret;
1853 }
1854 
1855 /**
1856  * irdma_add_mqh - Adds multiple qhashes
1857  * @iwdev: iWarp device
1858  * @cm_info: CM info for parent listen node
1859  * @cm_listen_node: The parent listen node
1860  */
1861 static int
1862 irdma_add_mqh(struct irdma_device *iwdev,
1863 	      struct irdma_cm_info *cm_info,
1864 	      struct irdma_cm_listener *cm_listen_node)
1865 {
1866 	int err;
1867 	VNET_ITERATOR_DECL(vnet_iter);
1868 
1869 	VNET_LIST_RLOCK();
1870 	VNET_FOREACH(vnet_iter) {
1871 		IFNET_RLOCK();
1872 		CURVNET_SET_QUIET(vnet_iter);
1873 
1874 		if (cm_info->ipv4)
1875 			err = irdma_add_mqh_4(iwdev, cm_info, cm_listen_node);
1876 		else
1877 			err = irdma_add_mqh_6(iwdev, cm_info, cm_listen_node);
1878 		CURVNET_RESTORE();
1879 		IFNET_RUNLOCK();
1880 	}
1881 	VNET_LIST_RUNLOCK();
1882 
1883 	return err;
1884 }
1885 
1886 /**
1887  * irdma_reset_list_prep - add connection nodes slated for reset to list
1888  * @cm_core: cm's core
1889  * @listener: pointer to listener node
1890  * @reset_list: a list to which cm_node will be selected
1891  */
1892 static void
1893 irdma_reset_list_prep(struct irdma_cm_core *cm_core,
1894 		      struct irdma_cm_listener *listener,
1895 		      struct list_head *reset_list)
1896 {
1897 	struct irdma_cm_node *cm_node;
1898 	int bkt;
1899 
1900 	HASH_FOR_EACH_RCU(cm_core->cm_hash_tbl, bkt, cm_node, list) {
1901 		if (cm_node->listener == listener &&
1902 		    !cm_node->accelerated &&
1903 		    atomic_inc_not_zero(&cm_node->refcnt))
1904 			list_add(&cm_node->reset_entry, reset_list);
1905 	}
1906 }
1907 
1908 /**
1909  * irdma_dec_refcnt_listen - delete listener and associated cm nodes
1910  * @cm_core: cm's core
1911  * @listener: pointer to listener node
1912  * @free_hanging_nodes: to free associated cm_nodes
1913  * @apbvt_del: flag to delete the apbvt
1914  */
1915 static int
1916 irdma_dec_refcnt_listen(struct irdma_cm_core *cm_core,
1917 			struct irdma_cm_listener *listener,
1918 			int free_hanging_nodes, bool apbvt_del)
1919 {
1920 	struct list_head *list_pos;
1921 	struct list_head *list_temp;
1922 	struct irdma_cm_node *cm_node;
1923 	struct list_head reset_list;
1924 	struct irdma_cm_info nfo;
1925 	enum irdma_cm_node_state old_state;
1926 	unsigned long flags;
1927 	int err;
1928 
1929 	/* free non-accelerated child nodes for this listener */
1930 	INIT_LIST_HEAD(&reset_list);
1931 	if (free_hanging_nodes) {
1932 		rcu_read_lock();
1933 		irdma_reset_list_prep(cm_core, listener, &reset_list);
1934 		rcu_read_unlock();
1935 	}
1936 
1937 	list_for_each_safe(list_pos, list_temp, &reset_list) {
1938 		cm_node = container_of(list_pos, struct irdma_cm_node,
1939 				       reset_entry);
1940 		if (cm_node->state >= IRDMA_CM_STATE_FIN_WAIT1) {
1941 			irdma_rem_ref_cm_node(cm_node);
1942 			continue;
1943 		}
1944 
1945 		irdma_cleanup_retrans_entry(cm_node);
1946 		err = irdma_send_reset(cm_node);
1947 		if (err) {
1948 			cm_node->state = IRDMA_CM_STATE_CLOSED;
1949 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
1950 				    IRDMA_DEBUG_CM, "send reset failed\n");
1951 		} else {
1952 			old_state = cm_node->state;
1953 			cm_node->state = IRDMA_CM_STATE_LISTENER_DESTROYED;
1954 			if (old_state != IRDMA_CM_STATE_MPAREQ_RCVD)
1955 				irdma_rem_ref_cm_node(cm_node);
1956 		}
1957 	}
1958 
1959 	if (atomic_dec_and_test(&listener->refcnt)) {
1960 		spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1961 		list_del(&listener->list);
1962 		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1963 
1964 		if (apbvt_del)
1965 			irdma_del_apbvt(listener->iwdev,
1966 					listener->apbvt_entry);
1967 		memcpy(nfo.loc_addr, listener->loc_addr, sizeof(nfo.loc_addr));
1968 		nfo.loc_port = listener->loc_port;
1969 		nfo.ipv4 = listener->ipv4;
1970 		nfo.vlan_id = listener->vlan_id;
1971 		nfo.user_pri = listener->user_pri;
1972 		nfo.qh_qpid = listener->iwdev->vsi.ilq->qp_id;
1973 
1974 		if (!list_empty(&listener->child_listen_list)) {
1975 			irdma_del_multiple_qhash(listener->iwdev, &nfo,
1976 						 listener);
1977 		} else {
1978 			if (listener->qhash_set)
1979 				irdma_manage_qhash(listener->iwdev,
1980 						   &nfo,
1981 						   IRDMA_QHASH_TYPE_TCP_SYN,
1982 						   IRDMA_QHASH_MANAGE_TYPE_DELETE,
1983 						   NULL, false);
1984 		}
1985 
1986 		cm_core->stats_listen_destroyed++;
1987 		cm_core->stats_listen_nodes_destroyed++;
1988 		irdma_debug(&listener->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
1989 			    "loc_port=0x%04x loc_addr=%pI4 cm_listen_node=%p cm_id=%p qhash_set=%d vlan_id=%d apbvt_del=%d\n",
1990 			    listener->loc_port, listener->loc_addr, listener,
1991 			    listener->cm_id, listener->qhash_set,
1992 			    listener->vlan_id, apbvt_del);
1993 		kfree(listener);
1994 		listener = NULL;
1995 		return 0;
1996 	}
1997 
1998 	return -EINVAL;
1999 }
2000 
2001 /**
2002  * irdma_cm_del_listen - delete a listener
2003  * @cm_core: cm's core
2004  * @listener: passive connection's listener
2005  * @apbvt_del: flag to delete apbvt
2006  */
2007 static int
2008 irdma_cm_del_listen(struct irdma_cm_core *cm_core,
2009 		    struct irdma_cm_listener *listener,
2010 		    bool apbvt_del)
2011 {
2012 	listener->listener_state = IRDMA_CM_LISTENER_PASSIVE_STATE;
2013 	listener->cm_id = NULL;
2014 
2015 	return irdma_dec_refcnt_listen(cm_core, listener, 1, apbvt_del);
2016 }
2017 
2018 /**
2019  * irdma_find_node - find a cm node that matches the reference cm node
2020  * @cm_core: cm's core
2021  * @rem_port: remote tcp port num
2022  * @rem_addr: remote ip addr
2023  * @loc_port: local tcp port num
2024  * @loc_addr: local ip addr
2025  * @vlan_id: local VLAN ID
2026  */
2027 struct irdma_cm_node *
2028 irdma_find_node(struct irdma_cm_core *cm_core,
2029 		u16 rem_port, u32 *rem_addr, u16 loc_port,
2030 		u32 *loc_addr, u16 vlan_id)
2031 {
2032 	struct irdma_cm_node *cm_node;
2033 	u32 key = (rem_port << 16) | loc_port;
2034 
2035 	rcu_read_lock();
2036 	HASH_FOR_EACH_POSSIBLE_RCU(cm_core->cm_hash_tbl, cm_node, list, key) {
2037 		if (cm_node->vlan_id == vlan_id &&
2038 		    cm_node->loc_port == loc_port && cm_node->rem_port == rem_port &&
2039 		    !memcmp(cm_node->loc_addr, loc_addr, sizeof(cm_node->loc_addr)) &&
2040 		    !memcmp(cm_node->rem_addr, rem_addr, sizeof(cm_node->rem_addr))) {
2041 			if (!atomic_inc_not_zero(&cm_node->refcnt))
2042 				goto exit;
2043 			rcu_read_unlock();
2044 			return cm_node;
2045 		}
2046 	}
2047 
2048 exit:
2049 	rcu_read_unlock();
2050 
2051 	/* no owner node */
2052 	return NULL;
2053 }
2054 
2055 /**
2056  * irdma_add_hte_node - add a cm node to the hash table
2057  * @cm_core: cm's core
2058  * @cm_node: connection's node
2059  */
2060 static void
2061 irdma_add_hte_node(struct irdma_cm_core *cm_core,
2062 		   struct irdma_cm_node *cm_node)
2063 {
2064 	unsigned long flags;
2065 	u32 key = (cm_node->rem_port << 16) | cm_node->loc_port;
2066 
2067 	spin_lock_irqsave(&cm_core->ht_lock, flags);
2068 	HASH_ADD_RCU(cm_core->cm_hash_tbl, &cm_node->list, key);
2069 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
2070 }
2071 
2072 /**
2073  * irdma_ipv4_is_lpb - check if loopback
2074  * @loc_addr: local addr to compare
2075  * @rem_addr: remote address
2076  */
2077 bool
2078 irdma_ipv4_is_lpb(u32 loc_addr, u32 rem_addr)
2079 {
2080 	return ipv4_is_loopback(htonl(rem_addr)) || (loc_addr == rem_addr);
2081 }
2082 
2083 /**
2084  * irdma_ipv6_is_lpb - check if loopback
2085  * @loc_addr: local addr to compare
2086  * @rem_addr: remote address
2087  */
2088 bool
2089 irdma_ipv6_is_lpb(u32 *loc_addr, u32 *rem_addr)
2090 {
2091 	struct in6_addr raddr6;
2092 
2093 	irdma_copy_ip_htonl(raddr6.__u6_addr.__u6_addr32, rem_addr);
2094 
2095 	return !memcmp(loc_addr, rem_addr, 16) || ipv6_addr_loopback(&raddr6);
2096 }
2097 
2098 /**
2099  * irdma_cm_create_ah - create a cm address handle
2100  * @cm_node: The connection manager node to create AH for
2101  * @wait: Provides option to wait for ah creation or not
2102  */
2103 static int
2104 irdma_cm_create_ah(struct irdma_cm_node *cm_node, bool wait)
2105 {
2106 	struct irdma_ah_info ah_info = {0};
2107 	struct irdma_device *iwdev = cm_node->iwdev;
2108 #ifdef VIMAGE
2109 	struct rdma_cm_id *rdma_id = (struct rdma_cm_id *)cm_node->cm_id->context;
2110 	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
2111 #endif
2112 
2113 	ether_addr_copy(ah_info.mac_addr, IF_LLADDR(iwdev->netdev));
2114 
2115 	ah_info.hop_ttl = 0x40;
2116 	ah_info.tc_tos = cm_node->tos;
2117 	ah_info.vsi = &iwdev->vsi;
2118 
2119 	if (cm_node->ipv4) {
2120 		ah_info.ipv4_valid = true;
2121 		ah_info.dest_ip_addr[0] = cm_node->rem_addr[0];
2122 		ah_info.src_ip_addr[0] = cm_node->loc_addr[0];
2123 		CURVNET_SET_QUIET(vnet);
2124 		ah_info.do_lpbk = irdma_ipv4_is_lpb(ah_info.src_ip_addr[0],
2125 						    ah_info.dest_ip_addr[0]);
2126 		CURVNET_RESTORE();
2127 	} else {
2128 		memcpy(ah_info.dest_ip_addr, cm_node->rem_addr,
2129 		       sizeof(ah_info.dest_ip_addr));
2130 		memcpy(ah_info.src_ip_addr, cm_node->loc_addr,
2131 		       sizeof(ah_info.src_ip_addr));
2132 		ah_info.do_lpbk = irdma_ipv6_is_lpb(ah_info.src_ip_addr,
2133 						    ah_info.dest_ip_addr);
2134 	}
2135 
2136 	ah_info.vlan_tag = cm_node->vlan_id;
2137 	if (cm_node->vlan_id < VLAN_N_VID) {
2138 		ah_info.insert_vlan_tag = 1;
2139 		ah_info.vlan_tag |= cm_node->user_pri << VLAN_PRIO_SHIFT;
2140 	}
2141 
2142 	ah_info.dst_arpindex =
2143 	    irdma_arp_table(iwdev->rf, ah_info.dest_ip_addr,
2144 			    NULL, IRDMA_ARP_RESOLVE);
2145 
2146 	if (irdma_puda_create_ah(&iwdev->rf->sc_dev, &ah_info, wait,
2147 				 IRDMA_PUDA_RSRC_TYPE_ILQ, cm_node,
2148 				 &cm_node->ah))
2149 		return -ENOMEM;
2150 
2151 	return 0;
2152 }
2153 
2154 /**
2155  * irdma_cm_free_ah - free a cm address handle
2156  * @cm_node: The connection manager node to create AH for
2157  */
2158 static void
2159 irdma_cm_free_ah(struct irdma_cm_node *cm_node)
2160 {
2161 	struct irdma_device *iwdev = cm_node->iwdev;
2162 
2163 	irdma_puda_free_ah(&iwdev->rf->sc_dev, cm_node->ah);
2164 	cm_node->ah = NULL;
2165 }
2166 
2167 /**
2168  * irdma_make_cm_node - create a new instance of a cm node
2169  * @cm_core: cm's core
2170  * @iwdev: iwarp device structure
2171  * @cm_info: quad info for connection
2172  * @listener: passive connection's listener
2173  */
2174 static struct irdma_cm_node *
2175 irdma_make_cm_node(struct irdma_cm_core *cm_core, struct irdma_device *iwdev,
2176 		   struct irdma_cm_info *cm_info,
2177 		   struct irdma_cm_listener *listener)
2178 {
2179 	struct irdma_cm_node *cm_node;
2180 	int arpindex;
2181 	struct ifnet *netdev = iwdev->netdev;
2182 
2183 	/* create an hte and cm_node for this instance */
2184 	cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
2185 	if (!cm_node)
2186 		return NULL;
2187 
2188 	/* set our node specific transport info */
2189 	cm_node->ipv4 = cm_info->ipv4;
2190 	cm_node->vlan_id = cm_info->vlan_id;
2191 	if (cm_node->vlan_id >= VLAN_N_VID && iwdev->dcb_vlan_mode)
2192 		cm_node->vlan_id = 0;
2193 	cm_node->tos = cm_info->tos;
2194 	cm_node->user_pri = cm_info->user_pri;
2195 	if (listener) {
2196 		if (listener->tos != cm_info->tos)
2197 			irdma_dev_warn(&iwdev->ibdev,
2198 				       "application TOS[%d] and remote client TOS[%d] mismatch\n",
2199 				       listener->tos, cm_info->tos);
2200 		if (iwdev->vsi.dscp_mode) {
2201 			cm_node->user_pri = listener->user_pri;
2202 		} else {
2203 			cm_node->tos = max(listener->tos, cm_info->tos);
2204 			cm_node->user_pri = rt_tos2priority(cm_node->tos);
2205 			cm_node->user_pri =
2206 			    irdma_get_egress_vlan_prio(cm_info->loc_addr,
2207 						       cm_node->user_pri,
2208 						       cm_info->ipv4);
2209 		}
2210 		irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_DCB,
2211 			    "listener: TOS:[%d] UP:[%d]\n",
2212 			    cm_node->tos,
2213 			    cm_node->user_pri);
2214 	}
2215 	memcpy(cm_node->loc_addr, cm_info->loc_addr, sizeof(cm_node->loc_addr));
2216 	memcpy(cm_node->rem_addr, cm_info->rem_addr, sizeof(cm_node->rem_addr));
2217 	cm_node->loc_port = cm_info->loc_port;
2218 	cm_node->rem_port = cm_info->rem_port;
2219 
2220 	cm_node->mpa_frame_rev = IRDMA_CM_DEFAULT_MPA_VER;
2221 	cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
2222 	cm_node->iwdev = iwdev;
2223 	cm_node->dev = &iwdev->rf->sc_dev;
2224 
2225 	cm_node->ird_size = cm_node->dev->hw_attrs.max_hw_ird;
2226 	cm_node->ord_size = cm_node->dev->hw_attrs.max_hw_ord;
2227 
2228 	cm_node->listener = listener;
2229 	cm_node->cm_id = cm_info->cm_id;
2230 	ether_addr_copy(cm_node->loc_mac, IF_LLADDR(netdev));
2231 	spin_lock_init(&cm_node->retrans_list_lock);
2232 	cm_node->ack_rcvd = false;
2233 
2234 	init_completion(&cm_node->establish_comp);
2235 	atomic_set(&cm_node->refcnt, 1);
2236 	/* associate our parent CM core */
2237 	cm_node->cm_core = cm_core;
2238 	cm_node->tcp_cntxt.loc_id = IRDMA_CM_DEFAULT_LOCAL_ID;
2239 	cm_node->tcp_cntxt.rcv_wscale = iwdev->rcv_wscale;
2240 	cm_node->tcp_cntxt.rcv_wnd = iwdev->rcv_wnd >> cm_node->tcp_cntxt.rcv_wscale;
2241 	kc_set_loc_seq_num_mss(cm_node);
2242 
2243 	arpindex = irdma_resolve_neigh_lpb_chk(iwdev, cm_node, cm_info);
2244 	if (arpindex < 0)
2245 		goto err;
2246 
2247 	ether_addr_copy(cm_node->rem_mac, iwdev->rf->arp_table[arpindex].mac_addr);
2248 	irdma_add_hte_node(cm_core, cm_node);
2249 	cm_core->stats_nodes_created++;
2250 	return cm_node;
2251 
2252 err:
2253 	kfree(cm_node);
2254 
2255 	return NULL;
2256 }
2257 
2258 static void
2259 irdma_destroy_connection(struct irdma_cm_node *cm_node)
2260 {
2261 	struct irdma_cm_core *cm_core = cm_node->cm_core;
2262 	struct irdma_qp *iwqp;
2263 	struct irdma_cm_info nfo;
2264 
2265 	/* if the node is destroyed before connection was accelerated */
2266 	if (!cm_node->accelerated && cm_node->accept_pend) {
2267 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
2268 			    IRDMA_DEBUG_CM, "node destroyed before established\n");
2269 		atomic_dec(&cm_node->listener->pend_accepts_cnt);
2270 	}
2271 	if (cm_node->close_entry)
2272 		irdma_handle_close_entry(cm_node, 0);
2273 	if (cm_node->listener) {
2274 		irdma_dec_refcnt_listen(cm_core, cm_node->listener, 0, true);
2275 	} else {
2276 		if (cm_node->apbvt_set) {
2277 			irdma_del_apbvt(cm_node->iwdev, cm_node->apbvt_entry);
2278 			cm_node->apbvt_set = 0;
2279 		}
2280 		irdma_get_addr_info(cm_node, &nfo);
2281 		if (cm_node->qhash_set) {
2282 			nfo.qh_qpid = cm_node->iwdev->vsi.ilq->qp_id;
2283 			irdma_manage_qhash(cm_node->iwdev, &nfo,
2284 					   IRDMA_QHASH_TYPE_TCP_ESTABLISHED,
2285 					   IRDMA_QHASH_MANAGE_TYPE_DELETE, NULL,
2286 					   false);
2287 			cm_node->qhash_set = 0;
2288 		}
2289 	}
2290 
2291 	iwqp = cm_node->iwqp;
2292 	if (iwqp) {
2293 		cm_node->cm_id->rem_ref(cm_node->cm_id);
2294 		cm_node->cm_id = NULL;
2295 		iwqp->cm_id = NULL;
2296 		irdma_qp_rem_ref(&iwqp->ibqp);
2297 		cm_node->iwqp = NULL;
2298 	} else if (cm_node->qhash_set) {
2299 		irdma_get_addr_info(cm_node, &nfo);
2300 		nfo.qh_qpid = cm_node->iwdev->vsi.ilq->qp_id;
2301 		irdma_manage_qhash(cm_node->iwdev, &nfo,
2302 				   IRDMA_QHASH_TYPE_TCP_ESTABLISHED,
2303 				   IRDMA_QHASH_MANAGE_TYPE_DELETE, NULL, false);
2304 		cm_node->qhash_set = 0;
2305 	}
2306 
2307 	cm_core->cm_free_ah(cm_node);
2308 }
2309 
2310 /**
2311  * irdma_rem_ref_cm_node - destroy an instance of a cm node
2312  * @cm_node: connection's node
2313  */
2314 void
2315 irdma_rem_ref_cm_node(struct irdma_cm_node *cm_node)
2316 {
2317 	struct irdma_cm_core *cm_core = cm_node->cm_core;
2318 	unsigned long flags;
2319 
2320 	spin_lock_irqsave(&cm_core->ht_lock, flags);
2321 
2322 	if (!atomic_dec_and_test(&cm_node->refcnt)) {
2323 		spin_unlock_irqrestore(&cm_core->ht_lock, flags);
2324 		return;
2325 	}
2326 	if (cm_node->iwqp) {
2327 		cm_node->iwqp->cm_node = NULL;
2328 		cm_node->iwqp->cm_id = NULL;
2329 	}
2330 	HASH_DEL_RCU(cm_core->cm_hash_tbl, &cm_node->list);
2331 	cm_node->cm_core->stats_nodes_destroyed++;
2332 
2333 	spin_unlock_irqrestore(&cm_core->ht_lock, flags);
2334 
2335 	irdma_destroy_connection(cm_node);
2336 
2337 	kfree_rcu(cm_node, rcu_head);
2338 }
2339 
2340 /**
2341  * irdma_handle_fin_pkt - FIN packet received
2342  * @cm_node: connection's node
2343  */
2344 static void
2345 irdma_handle_fin_pkt(struct irdma_cm_node *cm_node)
2346 {
2347 	switch (cm_node->state) {
2348 	case IRDMA_CM_STATE_SYN_RCVD:
2349 	case IRDMA_CM_STATE_SYN_SENT:
2350 	case IRDMA_CM_STATE_ESTABLISHED:
2351 	case IRDMA_CM_STATE_MPAREJ_RCVD:
2352 		cm_node->tcp_cntxt.rcv_nxt++;
2353 		irdma_cleanup_retrans_entry(cm_node);
2354 		cm_node->state = IRDMA_CM_STATE_LAST_ACK;
2355 		irdma_send_fin(cm_node);
2356 		break;
2357 	case IRDMA_CM_STATE_MPAREQ_SENT:
2358 		irdma_create_event(cm_node, IRDMA_CM_EVENT_ABORTED);
2359 		cm_node->tcp_cntxt.rcv_nxt++;
2360 		irdma_cleanup_retrans_entry(cm_node);
2361 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2362 		atomic_inc(&cm_node->refcnt);
2363 		irdma_send_reset(cm_node);
2364 		break;
2365 	case IRDMA_CM_STATE_FIN_WAIT1:
2366 		cm_node->tcp_cntxt.rcv_nxt++;
2367 		irdma_cleanup_retrans_entry(cm_node);
2368 		cm_node->state = IRDMA_CM_STATE_CLOSING;
2369 		irdma_send_ack(cm_node);
2370 		/*
2371 		 * Wait for ACK as this is simultaneous close. After we receive ACK, do not send anything. Just rm the
2372 		 * node.
2373 		 */
2374 		break;
2375 	case IRDMA_CM_STATE_FIN_WAIT2:
2376 		cm_node->tcp_cntxt.rcv_nxt++;
2377 		irdma_cleanup_retrans_entry(cm_node);
2378 		cm_node->state = IRDMA_CM_STATE_TIME_WAIT;
2379 		irdma_send_ack(cm_node);
2380 		irdma_schedule_cm_timer(cm_node, NULL, IRDMA_TIMER_TYPE_CLOSE,
2381 					1, 0);
2382 		break;
2383 	case IRDMA_CM_STATE_TIME_WAIT:
2384 		cm_node->tcp_cntxt.rcv_nxt++;
2385 		irdma_cleanup_retrans_entry(cm_node);
2386 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2387 		irdma_rem_ref_cm_node(cm_node);
2388 		break;
2389 	case IRDMA_CM_STATE_OFFLOADED:
2390 	default:
2391 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
2392 			    "bad state node state = %d\n",
2393 			    cm_node->state);
2394 		break;
2395 	}
2396 }
2397 
2398 /**
2399  * irdma_handle_rst_pkt - process received RST packet
2400  * @cm_node: connection's node
2401  * @rbuf: receive buffer
2402  */
2403 static void
2404 irdma_handle_rst_pkt(struct irdma_cm_node *cm_node,
2405 		     struct irdma_puda_buf *rbuf)
2406 {
2407 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
2408 		    "caller: %pS cm_node=%p state=%d rem_port=0x%04x loc_port=0x%04x rem_addr=%pI4 loc_addr=%pI4\n",
2409 		    __builtin_return_address(0), cm_node, cm_node->state,
2410 		    cm_node->rem_port, cm_node->loc_port, cm_node->rem_addr,
2411 		    cm_node->loc_addr);
2412 
2413 	irdma_cleanup_retrans_entry(cm_node);
2414 	switch (cm_node->state) {
2415 	case IRDMA_CM_STATE_SYN_SENT:
2416 	case IRDMA_CM_STATE_MPAREQ_SENT:
2417 		switch (cm_node->mpa_frame_rev) {
2418 		case IETF_MPA_V2:
2419 			/* Drop down to MPA_V1 */
2420 			cm_node->mpa_frame_rev = IETF_MPA_V1;
2421 			/* send a syn and goto syn sent state */
2422 			cm_node->state = IRDMA_CM_STATE_SYN_SENT;
2423 			if (irdma_send_syn(cm_node, 0))
2424 				irdma_active_open_err(cm_node, false);
2425 			break;
2426 		case IETF_MPA_V1:
2427 		default:
2428 			irdma_active_open_err(cm_node, false);
2429 			break;
2430 		}
2431 		break;
2432 	case IRDMA_CM_STATE_MPAREQ_RCVD:
2433 		atomic_inc(&cm_node->passive_state);
2434 		break;
2435 	case IRDMA_CM_STATE_ESTABLISHED:
2436 	case IRDMA_CM_STATE_SYN_RCVD:
2437 	case IRDMA_CM_STATE_LISTENING:
2438 		irdma_passive_open_err(cm_node, false);
2439 		break;
2440 	case IRDMA_CM_STATE_OFFLOADED:
2441 		irdma_active_open_err(cm_node, false);
2442 		break;
2443 	case IRDMA_CM_STATE_CLOSED:
2444 		break;
2445 	case IRDMA_CM_STATE_FIN_WAIT2:
2446 	case IRDMA_CM_STATE_FIN_WAIT1:
2447 	case IRDMA_CM_STATE_LAST_ACK:
2448 	case IRDMA_CM_STATE_TIME_WAIT:
2449 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2450 		irdma_rem_ref_cm_node(cm_node);
2451 		break;
2452 	default:
2453 		break;
2454 	}
2455 }
2456 
2457 /**
2458  * irdma_handle_rcv_mpa - Process a recv'd mpa buffer
2459  * @cm_node: connection's node
2460  * @rbuf: receive buffer
2461  */
2462 static void
2463 irdma_handle_rcv_mpa(struct irdma_cm_node *cm_node,
2464 		     struct irdma_puda_buf *rbuf)
2465 {
2466 	int err;
2467 	int datasize = rbuf->datalen;
2468 	u8 *dataloc = rbuf->data;
2469 
2470 	enum irdma_cm_event_type type = IRDMA_CM_EVENT_UNKNOWN;
2471 	u32 res_type;
2472 
2473 	err = irdma_parse_mpa(cm_node, dataloc, &res_type, datasize);
2474 	if (err) {
2475 		if (cm_node->state == IRDMA_CM_STATE_MPAREQ_SENT)
2476 			irdma_active_open_err(cm_node, true);
2477 		else
2478 			irdma_passive_open_err(cm_node, true);
2479 		return;
2480 	}
2481 
2482 	switch (cm_node->state) {
2483 	case IRDMA_CM_STATE_ESTABLISHED:
2484 		if (res_type == IRDMA_MPA_REQUEST_REJECT)
2485 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
2486 				    IRDMA_DEBUG_CM, "state for reject\n");
2487 		cm_node->state = IRDMA_CM_STATE_MPAREQ_RCVD;
2488 		type = IRDMA_CM_EVENT_MPA_REQ;
2489 		irdma_send_ack(cm_node);	/* ACK received MPA request */
2490 		atomic_set(&cm_node->passive_state,
2491 			   IRDMA_PASSIVE_STATE_INDICATED);
2492 		break;
2493 	case IRDMA_CM_STATE_MPAREQ_SENT:
2494 		irdma_cleanup_retrans_entry(cm_node);
2495 		if (res_type == IRDMA_MPA_REQUEST_REJECT) {
2496 			type = IRDMA_CM_EVENT_MPA_REJECT;
2497 			cm_node->state = IRDMA_CM_STATE_MPAREJ_RCVD;
2498 		} else {
2499 			type = IRDMA_CM_EVENT_CONNECTED;
2500 			cm_node->state = IRDMA_CM_STATE_OFFLOADED;
2501 		}
2502 		irdma_send_ack(cm_node);
2503 		break;
2504 	default:
2505 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
2506 			    "wrong cm_node state =%d\n",
2507 			    cm_node->state);
2508 		break;
2509 	}
2510 	irdma_create_event(cm_node, type);
2511 }
2512 
2513 /**
2514  * irdma_check_syn - Check for error on received syn ack
2515  * @cm_node: connection's node
2516  * @tcph: pointer tcp header
2517  */
2518 static int
2519 irdma_check_syn(struct irdma_cm_node *cm_node, struct tcphdr *tcph)
2520 {
2521 	if (ntohl(tcph->th_ack) != cm_node->tcp_cntxt.loc_seq_num) {
2522 		irdma_active_open_err(cm_node, true);
2523 		return 1;
2524 	}
2525 
2526 	return 0;
2527 }
2528 
2529 /**
2530  * irdma_check_seq - check seq numbers if OK
2531  * @cm_node: connection's node
2532  * @tcph: pointer tcp header
2533  */
2534 static int
2535 irdma_check_seq(struct irdma_cm_node *cm_node, struct tcphdr *tcph)
2536 {
2537 	u32 seq;
2538 	u32 ack_seq;
2539 	u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
2540 	u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
2541 	u32 rcv_wnd;
2542 	int err = 0;
2543 
2544 	seq = ntohl(tcph->th_seq);
2545 	ack_seq = ntohl(tcph->th_ack);
2546 	rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
2547 	if (ack_seq != loc_seq_num ||
2548 	    !between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
2549 		err = -1;
2550 	if (err)
2551 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
2552 			    IRDMA_DEBUG_CM, "seq number err\n");
2553 
2554 	return err;
2555 }
2556 
2557 void
2558 irdma_add_conn_est_qh(struct irdma_cm_node *cm_node)
2559 {
2560 	struct irdma_cm_info nfo;
2561 
2562 	irdma_get_addr_info(cm_node, &nfo);
2563 	nfo.qh_qpid = cm_node->iwdev->vsi.ilq->qp_id;
2564 	irdma_manage_qhash(cm_node->iwdev, &nfo,
2565 			   IRDMA_QHASH_TYPE_TCP_ESTABLISHED,
2566 			   IRDMA_QHASH_MANAGE_TYPE_ADD,
2567 			   cm_node, false);
2568 	cm_node->qhash_set = true;
2569 }
2570 
2571 /**
2572  * irdma_handle_syn_pkt - is for Passive node
2573  * @cm_node: connection's node
2574  * @rbuf: receive buffer
2575  */
2576 static void
2577 irdma_handle_syn_pkt(struct irdma_cm_node *cm_node,
2578 		     struct irdma_puda_buf *rbuf)
2579 {
2580 	struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2581 	int err;
2582 	u32 inc_sequence;
2583 	int optionsize;
2584 
2585 	optionsize = (tcph->th_off << 2) - sizeof(struct tcphdr);
2586 	inc_sequence = ntohl(tcph->th_seq);
2587 
2588 	switch (cm_node->state) {
2589 	case IRDMA_CM_STATE_SYN_SENT:
2590 	case IRDMA_CM_STATE_MPAREQ_SENT:
2591 		/* Rcvd syn on active open connection */
2592 		irdma_active_open_err(cm_node, 1);
2593 		break;
2594 	case IRDMA_CM_STATE_LISTENING:
2595 		/* Passive OPEN */
2596 		if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
2597 		    cm_node->listener->backlog) {
2598 			cm_node->cm_core->stats_backlog_drops++;
2599 			irdma_passive_open_err(cm_node, false);
2600 			break;
2601 		}
2602 		err = irdma_handle_tcp_options(cm_node, tcph, optionsize, 1);
2603 		if (err) {
2604 			irdma_passive_open_err(cm_node, false);
2605 			/* drop pkt */
2606 			break;
2607 		}
2608 		err = cm_node->cm_core->cm_create_ah(cm_node, false);
2609 		if (err) {
2610 			irdma_passive_open_err(cm_node, false);
2611 			/* drop pkt */
2612 			break;
2613 		}
2614 		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2615 		cm_node->accept_pend = 1;
2616 		atomic_inc(&cm_node->listener->pend_accepts_cnt);
2617 
2618 		cm_node->state = IRDMA_CM_STATE_SYN_RCVD;
2619 		break;
2620 	case IRDMA_CM_STATE_CLOSED:
2621 		irdma_cleanup_retrans_entry(cm_node);
2622 		atomic_inc(&cm_node->refcnt);
2623 		irdma_send_reset(cm_node);
2624 		break;
2625 	case IRDMA_CM_STATE_OFFLOADED:
2626 	case IRDMA_CM_STATE_ESTABLISHED:
2627 	case IRDMA_CM_STATE_FIN_WAIT1:
2628 	case IRDMA_CM_STATE_FIN_WAIT2:
2629 	case IRDMA_CM_STATE_MPAREQ_RCVD:
2630 	case IRDMA_CM_STATE_LAST_ACK:
2631 	case IRDMA_CM_STATE_CLOSING:
2632 	case IRDMA_CM_STATE_UNKNOWN:
2633 	default:
2634 		break;
2635 	}
2636 }
2637 
2638 /**
2639  * irdma_handle_synack_pkt - Process SYN+ACK packet (active side)
2640  * @cm_node: connection's node
2641  * @rbuf: receive buffer
2642  */
2643 static void
2644 irdma_handle_synack_pkt(struct irdma_cm_node *cm_node,
2645 			struct irdma_puda_buf *rbuf)
2646 {
2647 	struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2648 	int err;
2649 	u32 inc_sequence;
2650 	int optionsize;
2651 
2652 	optionsize = (tcph->th_off << 2) - sizeof(struct tcphdr);
2653 	inc_sequence = ntohl(tcph->th_seq);
2654 	switch (cm_node->state) {
2655 	case IRDMA_CM_STATE_SYN_SENT:
2656 		irdma_cleanup_retrans_entry(cm_node);
2657 		/* active open */
2658 		if (irdma_check_syn(cm_node, tcph)) {
2659 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
2660 				    IRDMA_DEBUG_CM, "check syn fail\n");
2661 			return;
2662 		}
2663 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->th_ack);
2664 		/* setup options */
2665 		err = irdma_handle_tcp_options(cm_node, tcph, optionsize, 0);
2666 		if (err) {
2667 			irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
2668 				    "cm_node=%p tcp_options failed\n",
2669 				    cm_node);
2670 			break;
2671 		}
2672 		irdma_cleanup_retrans_entry(cm_node);
2673 		cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2674 		irdma_send_ack(cm_node);	/* ACK  for the syn_ack */
2675 		err = irdma_send_mpa_request(cm_node);
2676 		if (err) {
2677 			irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
2678 				    "cm_node=%p irdma_send_mpa_request failed\n",
2679 				    cm_node);
2680 			break;
2681 		}
2682 		cm_node->state = IRDMA_CM_STATE_MPAREQ_SENT;
2683 		break;
2684 	case IRDMA_CM_STATE_MPAREQ_RCVD:
2685 		irdma_passive_open_err(cm_node, true);
2686 		break;
2687 	case IRDMA_CM_STATE_LISTENING:
2688 		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->th_ack);
2689 		irdma_cleanup_retrans_entry(cm_node);
2690 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2691 		irdma_send_reset(cm_node);
2692 		break;
2693 	case IRDMA_CM_STATE_CLOSED:
2694 		cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->th_ack);
2695 		irdma_cleanup_retrans_entry(cm_node);
2696 		atomic_inc(&cm_node->refcnt);
2697 		irdma_send_reset(cm_node);
2698 		break;
2699 	case IRDMA_CM_STATE_ESTABLISHED:
2700 	case IRDMA_CM_STATE_FIN_WAIT1:
2701 	case IRDMA_CM_STATE_FIN_WAIT2:
2702 	case IRDMA_CM_STATE_LAST_ACK:
2703 	case IRDMA_CM_STATE_OFFLOADED:
2704 	case IRDMA_CM_STATE_CLOSING:
2705 	case IRDMA_CM_STATE_UNKNOWN:
2706 	case IRDMA_CM_STATE_MPAREQ_SENT:
2707 	default:
2708 		break;
2709 	}
2710 }
2711 
2712 /**
2713  * irdma_handle_ack_pkt - process packet with ACK
2714  * @cm_node: connection's node
2715  * @rbuf: receive buffer
2716  */
2717 static int
2718 irdma_handle_ack_pkt(struct irdma_cm_node *cm_node,
2719 		     struct irdma_puda_buf *rbuf)
2720 {
2721 	struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2722 	u32 inc_sequence;
2723 	int ret;
2724 	int optionsize;
2725 	u32 datasize = rbuf->datalen;
2726 
2727 	optionsize = (tcph->th_off << 2) - sizeof(struct tcphdr);
2728 
2729 	if (irdma_check_seq(cm_node, tcph))
2730 		return -EINVAL;
2731 
2732 	inc_sequence = ntohl(tcph->th_seq);
2733 	switch (cm_node->state) {
2734 	case IRDMA_CM_STATE_SYN_RCVD:
2735 		irdma_cleanup_retrans_entry(cm_node);
2736 		ret = irdma_handle_tcp_options(cm_node, tcph, optionsize, 1);
2737 		if (ret)
2738 			return ret;
2739 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->th_ack);
2740 		cm_node->state = IRDMA_CM_STATE_ESTABLISHED;
2741 		if (datasize) {
2742 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2743 			irdma_handle_rcv_mpa(cm_node, rbuf);
2744 		}
2745 		break;
2746 	case IRDMA_CM_STATE_ESTABLISHED:
2747 		irdma_cleanup_retrans_entry(cm_node);
2748 		if (datasize) {
2749 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2750 			irdma_handle_rcv_mpa(cm_node, rbuf);
2751 		}
2752 		break;
2753 	case IRDMA_CM_STATE_MPAREQ_SENT:
2754 		cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->th_ack);
2755 		if (datasize) {
2756 			cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2757 			cm_node->ack_rcvd = false;
2758 			irdma_handle_rcv_mpa(cm_node, rbuf);
2759 		} else {
2760 			cm_node->ack_rcvd = true;
2761 		}
2762 		break;
2763 	case IRDMA_CM_STATE_LISTENING:
2764 		irdma_cleanup_retrans_entry(cm_node);
2765 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2766 		irdma_send_reset(cm_node);
2767 		break;
2768 	case IRDMA_CM_STATE_CLOSED:
2769 		irdma_cleanup_retrans_entry(cm_node);
2770 		atomic_inc(&cm_node->refcnt);
2771 		irdma_send_reset(cm_node);
2772 		break;
2773 	case IRDMA_CM_STATE_LAST_ACK:
2774 	case IRDMA_CM_STATE_CLOSING:
2775 		irdma_cleanup_retrans_entry(cm_node);
2776 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2777 		irdma_rem_ref_cm_node(cm_node);
2778 		break;
2779 	case IRDMA_CM_STATE_FIN_WAIT1:
2780 		irdma_cleanup_retrans_entry(cm_node);
2781 		cm_node->state = IRDMA_CM_STATE_FIN_WAIT2;
2782 		break;
2783 	case IRDMA_CM_STATE_SYN_SENT:
2784 	case IRDMA_CM_STATE_FIN_WAIT2:
2785 	case IRDMA_CM_STATE_OFFLOADED:
2786 	case IRDMA_CM_STATE_MPAREQ_RCVD:
2787 	case IRDMA_CM_STATE_UNKNOWN:
2788 	default:
2789 		irdma_cleanup_retrans_entry(cm_node);
2790 		break;
2791 	}
2792 
2793 	return 0;
2794 }
2795 
2796 /**
2797  * irdma_process_pkt - process cm packet
2798  * @cm_node: connection's node
2799  * @rbuf: receive buffer
2800  */
2801 static void
2802 irdma_process_pkt(struct irdma_cm_node *cm_node,
2803 		  struct irdma_puda_buf *rbuf)
2804 {
2805 	enum irdma_tcpip_pkt_type pkt_type = IRDMA_PKT_TYPE_UNKNOWN;
2806 	struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2807 	u32 fin_set = 0;
2808 	int err;
2809 
2810 	if (tcph->th_flags & TH_RST) {
2811 		pkt_type = IRDMA_PKT_TYPE_RST;
2812 	} else if (tcph->th_flags & TH_SYN) {
2813 		pkt_type = IRDMA_PKT_TYPE_SYN;
2814 		if (tcph->th_flags & TH_ACK)
2815 			pkt_type = IRDMA_PKT_TYPE_SYNACK;
2816 	} else if (tcph->th_flags & TH_ACK) {
2817 		pkt_type = IRDMA_PKT_TYPE_ACK;
2818 	}
2819 	if (tcph->th_flags & TH_FIN)
2820 		fin_set = 1;
2821 
2822 	switch (pkt_type) {
2823 	case IRDMA_PKT_TYPE_SYN:
2824 		irdma_handle_syn_pkt(cm_node, rbuf);
2825 		break;
2826 	case IRDMA_PKT_TYPE_SYNACK:
2827 		irdma_handle_synack_pkt(cm_node, rbuf);
2828 		break;
2829 	case IRDMA_PKT_TYPE_ACK:
2830 		err = irdma_handle_ack_pkt(cm_node, rbuf);
2831 		if (fin_set && !err)
2832 			irdma_handle_fin_pkt(cm_node);
2833 		break;
2834 	case IRDMA_PKT_TYPE_RST:
2835 		irdma_handle_rst_pkt(cm_node, rbuf);
2836 		break;
2837 	default:
2838 		if (fin_set &&
2839 		    (!irdma_check_seq(cm_node, (struct tcphdr *)rbuf->tcph)))
2840 			irdma_handle_fin_pkt(cm_node);
2841 		break;
2842 	}
2843 }
2844 
2845 /**
2846  * irdma_make_listen_node - create a listen node with params
2847  * @cm_core: cm's core
2848  * @iwdev: iwarp device structure
2849  * @cm_info: quad info for connection
2850  */
2851 static struct irdma_cm_listener *
2852 irdma_make_listen_node(struct irdma_cm_core *cm_core,
2853 		       struct irdma_device *iwdev,
2854 		       struct irdma_cm_info *cm_info)
2855 {
2856 	struct irdma_cm_listener *listener;
2857 	unsigned long flags;
2858 
2859 	/* cannot have multiple matching listeners */
2860 	listener = irdma_find_listener(cm_core, cm_info->loc_addr, cm_info->ipv4,
2861 				       cm_info->loc_port, cm_info->vlan_id,
2862 				       IRDMA_CM_LISTENER_EITHER_STATE);
2863 	if (listener &&
2864 	    listener->listener_state == IRDMA_CM_LISTENER_ACTIVE_STATE) {
2865 		atomic_dec(&listener->refcnt);
2866 		return NULL;
2867 	}
2868 
2869 	if (!listener) {
2870 		/*
2871 		 * create a CM listen node 1/2 node to compare incoming traffic to
2872 		 */
2873 		listener = kzalloc(sizeof(*listener), GFP_KERNEL);
2874 		if (!listener)
2875 			return NULL;
2876 		cm_core->stats_listen_nodes_created++;
2877 		memcpy(listener->loc_addr, cm_info->loc_addr,
2878 		       sizeof(listener->loc_addr));
2879 		listener->loc_port = cm_info->loc_port;
2880 
2881 		INIT_LIST_HEAD(&listener->child_listen_list);
2882 
2883 		atomic_set(&listener->refcnt, 1);
2884 	} else {
2885 		listener->reused_node = 1;
2886 	}
2887 
2888 	listener->cm_id = cm_info->cm_id;
2889 	listener->ipv4 = cm_info->ipv4;
2890 	listener->vlan_id = cm_info->vlan_id;
2891 	atomic_set(&listener->pend_accepts_cnt, 0);
2892 	listener->cm_core = cm_core;
2893 	listener->iwdev = iwdev;
2894 
2895 	listener->backlog = cm_info->backlog;
2896 	listener->listener_state = IRDMA_CM_LISTENER_ACTIVE_STATE;
2897 
2898 	if (!listener->reused_node) {
2899 		spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2900 		list_add(&listener->list, &cm_core->listen_list);
2901 		spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2902 	}
2903 
2904 	return listener;
2905 }
2906 
2907 /**
2908  * irdma_create_cm_node - make a connection node with params
2909  * @cm_core: cm's core
2910  * @iwdev: iwarp device structure
2911  * @conn_param: connection parameters
2912  * @cm_info: quad info for connection
2913  * @caller_cm_node: pointer to cm_node structure to return
2914  */
2915 static int
2916 irdma_create_cm_node(struct irdma_cm_core *cm_core,
2917 		     struct irdma_device *iwdev,
2918 		     struct iw_cm_conn_param *conn_param,
2919 		     struct irdma_cm_info *cm_info,
2920 		     struct irdma_cm_node **caller_cm_node)
2921 {
2922 	struct irdma_cm_node *cm_node;
2923 	u16 private_data_len = conn_param->private_data_len;
2924 	const void *private_data = conn_param->private_data;
2925 
2926 	/* create a CM connection node */
2927 	cm_node = irdma_make_cm_node(cm_core, iwdev, cm_info, NULL);
2928 	if (!cm_node)
2929 		return -ENOMEM;
2930 
2931 	/* set our node side to client (active) side */
2932 	cm_node->tcp_cntxt.client = 1;
2933 	cm_node->tcp_cntxt.rcv_wscale = IRDMA_CM_DEFAULT_RCV_WND_SCALE;
2934 
2935 	irdma_record_ird_ord(cm_node, conn_param->ird, conn_param->ord);
2936 
2937 	cm_node->pdata.size = private_data_len;
2938 	cm_node->pdata.addr = cm_node->pdata_buf;
2939 
2940 	memcpy(cm_node->pdata_buf, private_data, private_data_len);
2941 	*caller_cm_node = cm_node;
2942 
2943 	return 0;
2944 }
2945 
2946 /**
2947  * irdma_cm_reject - reject and teardown a connection
2948  * @cm_node: connection's node
2949  * @pdata: ptr to private data for reject
2950  * @plen: size of private data
2951  */
2952 static int
2953 irdma_cm_reject(struct irdma_cm_node *cm_node, const void *pdata,
2954 		u8 plen)
2955 {
2956 	int ret;
2957 	int passive_state;
2958 
2959 	if (cm_node->tcp_cntxt.client)
2960 		return 0;
2961 
2962 	irdma_cleanup_retrans_entry(cm_node);
2963 
2964 	passive_state = atomic_add_return(1, &cm_node->passive_state);
2965 	if (passive_state == IRDMA_SEND_RESET_EVENT) {
2966 		cm_node->state = IRDMA_CM_STATE_CLOSED;
2967 		irdma_rem_ref_cm_node(cm_node);
2968 		return 0;
2969 	}
2970 
2971 	if (cm_node->state == IRDMA_CM_STATE_LISTENER_DESTROYED) {
2972 		irdma_rem_ref_cm_node(cm_node);
2973 		return 0;
2974 	}
2975 
2976 	ret = irdma_send_mpa_reject(cm_node, pdata, plen);
2977 	if (!ret)
2978 		return 0;
2979 
2980 	cm_node->state = IRDMA_CM_STATE_CLOSED;
2981 	if (irdma_send_reset(cm_node))
2982 		irdma_debug(&cm_node->iwdev->rf->sc_dev,
2983 			    IRDMA_DEBUG_CM, "send reset failed\n");
2984 
2985 	return ret;
2986 }
2987 
2988 /**
2989  * irdma_cm_close - close of cm connection
2990  * @cm_node: connection's node
2991  */
2992 static int
2993 irdma_cm_close(struct irdma_cm_node *cm_node)
2994 {
2995 	switch (cm_node->state) {
2996 	case IRDMA_CM_STATE_SYN_RCVD:
2997 	case IRDMA_CM_STATE_SYN_SENT:
2998 	case IRDMA_CM_STATE_ONE_SIDE_ESTABLISHED:
2999 	case IRDMA_CM_STATE_ESTABLISHED:
3000 	case IRDMA_CM_STATE_ACCEPTING:
3001 	case IRDMA_CM_STATE_MPAREQ_SENT:
3002 	case IRDMA_CM_STATE_MPAREQ_RCVD:
3003 		irdma_cleanup_retrans_entry(cm_node);
3004 		irdma_send_reset(cm_node);
3005 		break;
3006 	case IRDMA_CM_STATE_CLOSE_WAIT:
3007 		cm_node->state = IRDMA_CM_STATE_LAST_ACK;
3008 		irdma_send_fin(cm_node);
3009 		break;
3010 	case IRDMA_CM_STATE_FIN_WAIT1:
3011 	case IRDMA_CM_STATE_FIN_WAIT2:
3012 	case IRDMA_CM_STATE_LAST_ACK:
3013 	case IRDMA_CM_STATE_TIME_WAIT:
3014 	case IRDMA_CM_STATE_CLOSING:
3015 		return -EINVAL;
3016 	case IRDMA_CM_STATE_LISTENING:
3017 		irdma_cleanup_retrans_entry(cm_node);
3018 		irdma_send_reset(cm_node);
3019 		break;
3020 	case IRDMA_CM_STATE_MPAREJ_RCVD:
3021 	case IRDMA_CM_STATE_UNKNOWN:
3022 	case IRDMA_CM_STATE_INITED:
3023 	case IRDMA_CM_STATE_CLOSED:
3024 	case IRDMA_CM_STATE_LISTENER_DESTROYED:
3025 		irdma_rem_ref_cm_node(cm_node);
3026 		break;
3027 	case IRDMA_CM_STATE_OFFLOADED:
3028 		if (cm_node->send_entry)
3029 			irdma_debug(&cm_node->iwdev->rf->sc_dev,
3030 				    IRDMA_DEBUG_CM, "CM send_entry in OFFLOADED state\n");
3031 		irdma_rem_ref_cm_node(cm_node);
3032 		break;
3033 	}
3034 
3035 	return 0;
3036 }
3037 
3038 /**
3039  * irdma_receive_ilq - recv an ETHERNET packet, and process it
3040  * through CM
3041  * @vsi: VSI structure of dev
3042  * @rbuf: receive buffer
3043  */
3044 void
3045 irdma_receive_ilq(struct irdma_sc_vsi *vsi, struct irdma_puda_buf *rbuf)
3046 {
3047 	struct irdma_cm_node *cm_node;
3048 	struct irdma_cm_listener *listener;
3049 	struct ip *iph;
3050 	struct ip6_hdr *ip6h;
3051 	struct tcphdr *tcph;
3052 	struct irdma_cm_info cm_info = {0};
3053 	struct irdma_device *iwdev = vsi->back_vsi;
3054 	struct irdma_cm_core *cm_core = &iwdev->cm_core;
3055 	struct ether_vlan_header *ethh;
3056 	u16 vtag;
3057 
3058 	/* if vlan, then maclen = 18 else 14 */
3059 	iph = (struct ip *)rbuf->iph;
3060 	irdma_debug_buf(vsi->dev, IRDMA_DEBUG_ILQ, "RECEIVE ILQ BUFFER",
3061 			rbuf->mem.va, rbuf->totallen);
3062 	if (iwdev->rf->sc_dev.hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_2) {
3063 		if (rbuf->vlan_valid) {
3064 			vtag = rbuf->vlan_id;
3065 			cm_info.user_pri = (vtag & EVL_PRI_MASK) >>
3066 			    VLAN_PRIO_SHIFT;
3067 			cm_info.vlan_id = vtag & EVL_VLID_MASK;
3068 		} else {
3069 			cm_info.vlan_id = 0xFFFF;
3070 		}
3071 	} else {
3072 		ethh = rbuf->mem.va;
3073 
3074 		if (ethh->evl_proto == htons(ETH_P_8021Q)) {
3075 			vtag = ntohs(ethh->evl_tag);
3076 			cm_info.user_pri = (vtag & EVL_PRI_MASK) >>
3077 			    VLAN_PRIO_SHIFT;
3078 			cm_info.vlan_id = vtag & EVL_VLID_MASK;
3079 			irdma_debug(&cm_core->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3080 				    "vlan_id=%d\n",
3081 				    cm_info.vlan_id);
3082 		} else {
3083 			cm_info.vlan_id = 0xFFFF;
3084 		}
3085 	}
3086 	tcph = (struct tcphdr *)rbuf->tcph;
3087 
3088 	if (rbuf->ipv4) {
3089 		cm_info.loc_addr[0] = ntohl(iph->ip_dst.s_addr);
3090 		cm_info.rem_addr[0] = ntohl(iph->ip_src.s_addr);
3091 		cm_info.ipv4 = true;
3092 		cm_info.tos = iph->ip_tos;
3093 	} else {
3094 		ip6h = (struct ip6_hdr *)rbuf->iph;
3095 		irdma_copy_ip_ntohl(cm_info.loc_addr,
3096 				    ip6h->ip6_dst.__u6_addr.__u6_addr32);
3097 		irdma_copy_ip_ntohl(cm_info.rem_addr,
3098 				    ip6h->ip6_src.__u6_addr.__u6_addr32);
3099 		cm_info.ipv4 = false;
3100 		cm_info.tos = (ip6h->ip6_vfc << 4) | ip6h->ip6_flow;
3101 	}
3102 	cm_info.loc_port = ntohs(tcph->th_dport);
3103 	cm_info.rem_port = ntohs(tcph->th_sport);
3104 	cm_node = irdma_find_node(cm_core, cm_info.rem_port, cm_info.rem_addr,
3105 				  cm_info.loc_port, cm_info.loc_addr, cm_info.vlan_id);
3106 
3107 	if (!cm_node) {
3108 		/*
3109 		 * Only type of packet accepted are for the PASSIVE open (syn only)
3110 		 */
3111 		if (!(tcph->th_flags & TH_SYN) || tcph->th_flags & TH_ACK)
3112 			return;
3113 
3114 		listener = irdma_find_listener(cm_core,
3115 					       cm_info.loc_addr,
3116 					       cm_info.ipv4,
3117 					       cm_info.loc_port,
3118 					       cm_info.vlan_id,
3119 					       IRDMA_CM_LISTENER_ACTIVE_STATE);
3120 		if (!listener) {
3121 			cm_info.cm_id = NULL;
3122 			irdma_debug(&cm_core->iwdev->rf->sc_dev,
3123 				    IRDMA_DEBUG_CM, "no listener found\n");
3124 			return;
3125 		}
3126 
3127 		cm_info.cm_id = listener->cm_id;
3128 		cm_node = irdma_make_cm_node(cm_core, iwdev, &cm_info,
3129 					     listener);
3130 		if (!cm_node) {
3131 			irdma_debug(&cm_core->iwdev->rf->sc_dev,
3132 				    IRDMA_DEBUG_CM, "allocate node failed\n");
3133 			atomic_dec(&listener->refcnt);
3134 			return;
3135 		}
3136 
3137 		if (!(tcph->th_flags & (TH_RST | TH_FIN))) {
3138 			cm_node->state = IRDMA_CM_STATE_LISTENING;
3139 		} else {
3140 			irdma_rem_ref_cm_node(cm_node);
3141 			return;
3142 		}
3143 
3144 		atomic_inc(&cm_node->refcnt);
3145 	} else if (cm_node->state == IRDMA_CM_STATE_OFFLOADED) {
3146 		irdma_rem_ref_cm_node(cm_node);
3147 		return;
3148 	}
3149 
3150 	irdma_process_pkt(cm_node, rbuf);
3151 	irdma_rem_ref_cm_node(cm_node);
3152 }
3153 
3154 static int
3155 irdma_add_qh(struct irdma_cm_node *cm_node, bool active)
3156 {
3157 	if (!active)
3158 		irdma_add_conn_est_qh(cm_node);
3159 	return 0;
3160 }
3161 
3162 static void
3163 irdma_cm_free_ah_nop(struct irdma_cm_node *cm_node)
3164 {
3165 }
3166 
3167 /**
3168  * irdma_setup_cm_core - setup top level instance of a cm core
3169  * @iwdev: iwarp device structure
3170  * @rdma_ver: HW version
3171  */
3172 int
3173 irdma_setup_cm_core(struct irdma_device *iwdev, u8 rdma_ver)
3174 {
3175 	struct irdma_cm_core *cm_core = &iwdev->cm_core;
3176 
3177 	cm_core->iwdev = iwdev;
3178 	cm_core->dev = &iwdev->rf->sc_dev;
3179 
3180 	/* Handles CM event work items send to Iwarp core */
3181 	cm_core->event_wq = alloc_ordered_workqueue("iwarp-event-wq", 0);
3182 	if (!cm_core->event_wq)
3183 		return -ENOMEM;
3184 
3185 	INIT_LIST_HEAD(&cm_core->listen_list);
3186 
3187 	timer_setup(&cm_core->tcp_timer, irdma_cm_timer_tick, 0);
3188 
3189 	spin_lock_init(&cm_core->ht_lock);
3190 	spin_lock_init(&cm_core->listen_list_lock);
3191 	spin_lock_init(&cm_core->apbvt_lock);
3192 	switch (rdma_ver) {
3193 	case IRDMA_GEN_1:
3194 		cm_core->form_cm_frame = irdma_form_uda_cm_frame;
3195 		cm_core->cm_create_ah = irdma_add_qh;
3196 		cm_core->cm_free_ah = irdma_cm_free_ah_nop;
3197 		break;
3198 	case IRDMA_GEN_2:
3199 	default:
3200 		cm_core->form_cm_frame = irdma_form_ah_cm_frame;
3201 		cm_core->cm_create_ah = irdma_cm_create_ah;
3202 		cm_core->cm_free_ah = irdma_cm_free_ah;
3203 	}
3204 
3205 	return 0;
3206 }
3207 
3208 /**
3209  * irdma_cleanup_cm_core - deallocate a top level instance of a
3210  * cm core
3211  * @cm_core: cm's core
3212  */
3213 void
3214 irdma_cleanup_cm_core(struct irdma_cm_core *cm_core)
3215 {
3216 	if (!cm_core)
3217 		return;
3218 
3219 	del_timer_sync(&cm_core->tcp_timer);
3220 
3221 	destroy_workqueue(cm_core->event_wq);
3222 	cm_core->dev->ws_reset(&cm_core->iwdev->vsi);
3223 }
3224 
3225 /**
3226  * irdma_init_tcp_ctx - setup qp context
3227  * @cm_node: connection's node
3228  * @tcp_info: offload info for tcp
3229  * @iwqp: associate qp for the connection
3230  */
3231 static void
3232 irdma_init_tcp_ctx(struct irdma_cm_node *cm_node,
3233 		   struct irdma_tcp_offload_info *tcp_info,
3234 		   struct irdma_qp *iwqp)
3235 {
3236 	tcp_info->ipv4 = cm_node->ipv4;
3237 	tcp_info->drop_ooo_seg = !iwqp->iwdev->iw_ooo;
3238 	tcp_info->wscale = true;
3239 	tcp_info->ignore_tcp_opt = true;
3240 	tcp_info->ignore_tcp_uns_opt = true;
3241 	tcp_info->no_nagle = false;
3242 
3243 	tcp_info->ttl = IRDMA_DEFAULT_TTL;
3244 	tcp_info->rtt_var = IRDMA_DEFAULT_RTT_VAR;
3245 	tcp_info->ss_thresh = IRDMA_DEFAULT_SS_THRESH;
3246 	tcp_info->rexmit_thresh = IRDMA_DEFAULT_REXMIT_THRESH;
3247 
3248 	tcp_info->tcp_state = IRDMA_TCP_STATE_ESTABLISHED;
3249 	tcp_info->snd_wscale = cm_node->tcp_cntxt.snd_wscale;
3250 	tcp_info->rcv_wscale = cm_node->tcp_cntxt.rcv_wscale;
3251 
3252 	tcp_info->snd_nxt = cm_node->tcp_cntxt.loc_seq_num;
3253 	tcp_info->snd_wnd = cm_node->tcp_cntxt.snd_wnd;
3254 	tcp_info->rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
3255 	tcp_info->snd_max = cm_node->tcp_cntxt.loc_seq_num;
3256 
3257 	tcp_info->snd_una = cm_node->tcp_cntxt.loc_seq_num;
3258 	tcp_info->cwnd = 2 * cm_node->tcp_cntxt.mss;
3259 	tcp_info->snd_wl1 = cm_node->tcp_cntxt.rcv_nxt;
3260 	tcp_info->snd_wl2 = cm_node->tcp_cntxt.loc_seq_num;
3261 	tcp_info->max_snd_window = cm_node->tcp_cntxt.max_snd_wnd;
3262 	tcp_info->rcv_wnd = cm_node->tcp_cntxt.rcv_wnd
3263 	    << cm_node->tcp_cntxt.rcv_wscale;
3264 
3265 	tcp_info->flow_label = 0;
3266 	tcp_info->snd_mss = (u32)cm_node->tcp_cntxt.mss;
3267 	tcp_info->tos = cm_node->tos;
3268 	if (cm_node->vlan_id < VLAN_N_VID) {
3269 		tcp_info->insert_vlan_tag = true;
3270 		tcp_info->vlan_tag = cm_node->vlan_id;
3271 		tcp_info->vlan_tag |= cm_node->user_pri << VLAN_PRIO_SHIFT;
3272 	}
3273 	tcp_info->src_port = cm_node->loc_port;
3274 	tcp_info->dst_port = cm_node->rem_port;
3275 	tcp_info->arp_idx = (u16)irdma_arp_table(iwqp->iwdev->rf,
3276 						  cm_node->rem_addr, NULL,
3277 						  IRDMA_ARP_RESOLVE);
3278 	if (cm_node->ipv4) {
3279 		tcp_info->dest_ip_addr[3] = cm_node->rem_addr[0];
3280 		tcp_info->local_ipaddr[3] = cm_node->loc_addr[0];
3281 	} else {
3282 		memcpy(tcp_info->dest_ip_addr, cm_node->rem_addr,
3283 		       sizeof(tcp_info->dest_ip_addr));
3284 		memcpy(tcp_info->local_ipaddr, cm_node->loc_addr,
3285 		       sizeof(tcp_info->local_ipaddr));
3286 	}
3287 }
3288 
3289 /**
3290  * irdma_cm_init_tsa_conn - setup qp for RTS
3291  * @iwqp: associate qp for the connection
3292  * @cm_node: connection's node
3293  */
3294 static void
3295 irdma_cm_init_tsa_conn(struct irdma_qp *iwqp,
3296 		       struct irdma_cm_node *cm_node)
3297 {
3298 	struct irdma_iwarp_offload_info *iwarp_info;
3299 	struct irdma_qp_host_ctx_info *ctx_info;
3300 
3301 	iwarp_info = &iwqp->iwarp_info;
3302 	ctx_info = &iwqp->ctx_info;
3303 
3304 	ctx_info->tcp_info = &iwqp->tcp_info;
3305 	ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
3306 	ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
3307 
3308 	iwarp_info->ord_size = cm_node->ord_size;
3309 	iwarp_info->ird_size = cm_node->ird_size;
3310 	iwarp_info->rd_en = true;
3311 	iwarp_info->rdmap_ver = 1;
3312 	iwarp_info->ddp_ver = 1;
3313 	iwarp_info->pd_id = iwqp->iwpd->sc_pd.pd_id;
3314 
3315 	ctx_info->tcp_info_valid = true;
3316 	ctx_info->iwarp_info_valid = true;
3317 	ctx_info->user_pri = cm_node->user_pri;
3318 
3319 	irdma_init_tcp_ctx(cm_node, &iwqp->tcp_info, iwqp);
3320 	if (cm_node->snd_mark_en) {
3321 		iwarp_info->snd_mark_en = true;
3322 		iwarp_info->snd_mark_offset = (iwqp->tcp_info.snd_nxt & SNDMARKER_SEQNMASK) +
3323 		    cm_node->lsmm_size;
3324 	}
3325 
3326 	cm_node->state = IRDMA_CM_STATE_OFFLOADED;
3327 	iwqp->tcp_info.tcp_state = IRDMA_TCP_STATE_ESTABLISHED;
3328 	iwqp->tcp_info.src_mac_addr_idx = iwqp->iwdev->mac_ip_table_idx;
3329 
3330 	if (cm_node->rcv_mark_en) {
3331 		iwarp_info->rcv_mark_en = true;
3332 		iwarp_info->align_hdrs = true;
3333 	}
3334 
3335 	irdma_sc_qp_setctx(&iwqp->sc_qp, iwqp->host_ctx.va, ctx_info);
3336 
3337 	/* once tcp_info is set, no need to do it again */
3338 	ctx_info->tcp_info_valid = false;
3339 	ctx_info->iwarp_info_valid = false;
3340 }
3341 
3342 /**
3343  * irdma_cm_disconn - when a connection is being closed
3344  * @iwqp: associated qp for the connection
3345  */
3346 void
3347 irdma_cm_disconn(struct irdma_qp *iwqp)
3348 {
3349 	struct irdma_device *iwdev = iwqp->iwdev;
3350 	struct disconn_work *work;
3351 	unsigned long flags;
3352 
3353 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
3354 	if (!work)
3355 		return;
3356 
3357 	spin_lock_irqsave(&iwdev->rf->qptable_lock, flags);
3358 	if (!iwdev->rf->qp_table[iwqp->ibqp.qp_num]) {
3359 		spin_unlock_irqrestore(&iwdev->rf->qptable_lock, flags);
3360 		irdma_debug(&iwdev->rf->sc_dev,
3361 			    IRDMA_DEBUG_CM, "qp_id %d is already freed\n",
3362 			    iwqp->ibqp.qp_num);
3363 		kfree(work);
3364 		return;
3365 	}
3366 	irdma_qp_add_ref(&iwqp->ibqp);
3367 	spin_unlock_irqrestore(&iwdev->rf->qptable_lock, flags);
3368 
3369 	work->iwqp = iwqp;
3370 	INIT_WORK(&work->work, irdma_disconnect_worker);
3371 	queue_work(iwdev->cleanup_wq, &work->work);
3372 }
3373 
3374 /**
3375  * irdma_qp_disconnect - free qp and close cm
3376  * @iwqp: associate qp for the connection
3377  */
3378 static void
3379 irdma_qp_disconnect(struct irdma_qp *iwqp)
3380 {
3381 	struct irdma_device *iwdev = iwqp->iwdev;
3382 
3383 	iwqp->active_conn = 0;
3384 	/* close the CM node down if it is still active */
3385 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "Call close API\n");
3386 	irdma_cm_close(iwqp->cm_node);
3387 }
3388 
3389 /**
3390  * irdma_cm_disconn_true - called by worker thread to disconnect qp
3391  * @iwqp: associate qp for the connection
3392  */
3393 static void
3394 irdma_cm_disconn_true(struct irdma_qp *iwqp)
3395 {
3396 	struct iw_cm_id *cm_id;
3397 	struct irdma_device *iwdev;
3398 	struct irdma_sc_qp *qp = &iwqp->sc_qp;
3399 	u16 last_ae;
3400 	u8 original_hw_tcp_state;
3401 	u8 original_ibqp_state;
3402 	int disconn_status = 0;
3403 	int issue_disconn = 0;
3404 	int issue_close = 0;
3405 	int issue_flush = 0;
3406 	unsigned long flags;
3407 	int err;
3408 
3409 	iwdev = iwqp->iwdev;
3410 	spin_lock_irqsave(&iwqp->lock, flags);
3411 	if (rdma_protocol_roce(&iwdev->ibdev, 1)) {
3412 		struct ib_qp_attr attr;
3413 
3414 		if (iwqp->flush_issued || iwqp->sc_qp.qp_uk.destroy_pending) {
3415 			spin_unlock_irqrestore(&iwqp->lock, flags);
3416 			return;
3417 		}
3418 
3419 		spin_unlock_irqrestore(&iwqp->lock, flags);
3420 
3421 		attr.qp_state = IB_QPS_ERR;
3422 		irdma_modify_qp_roce(&iwqp->ibqp, &attr, IB_QP_STATE, NULL);
3423 		irdma_ib_qp_event(iwqp, qp->event_type);
3424 		return;
3425 	}
3426 
3427 	cm_id = iwqp->cm_id;
3428 	original_hw_tcp_state = iwqp->hw_tcp_state;
3429 	original_ibqp_state = iwqp->ibqp_state;
3430 	last_ae = iwqp->last_aeq;
3431 
3432 	if (qp->term_flags) {
3433 		issue_disconn = 1;
3434 		issue_close = 1;
3435 		iwqp->cm_id = NULL;
3436 		irdma_terminate_del_timer(qp);
3437 		if (!iwqp->flush_issued) {
3438 			iwqp->flush_issued = 1;
3439 			issue_flush = 1;
3440 		}
3441 	} else if ((original_hw_tcp_state == IRDMA_TCP_STATE_CLOSE_WAIT) ||
3442 		   ((original_ibqp_state == IB_QPS_RTS) &&
3443 		    (last_ae == IRDMA_AE_LLP_CONNECTION_RESET))) {
3444 		issue_disconn = 1;
3445 		if (last_ae == IRDMA_AE_LLP_CONNECTION_RESET)
3446 			disconn_status = -ECONNRESET;
3447 	}
3448 
3449 	if (original_hw_tcp_state == IRDMA_TCP_STATE_CLOSED ||
3450 	    original_hw_tcp_state == IRDMA_TCP_STATE_TIME_WAIT ||
3451 	    last_ae == IRDMA_AE_RDMAP_ROE_BAD_LLP_CLOSE ||
3452 	    last_ae == IRDMA_AE_BAD_CLOSE ||
3453 	    last_ae == IRDMA_AE_LLP_CONNECTION_RESET || iwdev->rf->reset || !cm_id) {
3454 		issue_close = 1;
3455 		iwqp->cm_id = NULL;
3456 		qp->term_flags = 0;
3457 		if (!iwqp->flush_issued) {
3458 			iwqp->flush_issued = 1;
3459 			issue_flush = 1;
3460 		}
3461 	}
3462 
3463 	spin_unlock_irqrestore(&iwqp->lock, flags);
3464 	if (issue_flush && !iwqp->sc_qp.qp_uk.destroy_pending) {
3465 		irdma_flush_wqes(iwqp, IRDMA_FLUSH_SQ | IRDMA_FLUSH_RQ |
3466 				 IRDMA_FLUSH_WAIT);
3467 
3468 		if (qp->term_flags)
3469 			irdma_ib_qp_event(iwqp, qp->event_type);
3470 	}
3471 
3472 	if (!cm_id || !cm_id->event_handler)
3473 		return;
3474 
3475 	spin_lock_irqsave(&iwdev->cm_core.ht_lock, flags);
3476 	if (!iwqp->cm_node) {
3477 		spin_unlock_irqrestore(&iwdev->cm_core.ht_lock, flags);
3478 		return;
3479 	}
3480 	atomic_inc(&iwqp->cm_node->refcnt);
3481 
3482 	spin_unlock_irqrestore(&iwdev->cm_core.ht_lock, flags);
3483 
3484 	if (issue_disconn) {
3485 		err = irdma_send_cm_event(iwqp->cm_node, cm_id,
3486 					  IW_CM_EVENT_DISCONNECT,
3487 					  disconn_status);
3488 		if (err)
3489 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3490 				    "disconnect event failed: - cm_id = %p\n",
3491 				    cm_id);
3492 	}
3493 	if (issue_close) {
3494 		cm_id->provider_data = iwqp;
3495 		err = irdma_send_cm_event(iwqp->cm_node, cm_id,
3496 					  IW_CM_EVENT_CLOSE, 0);
3497 		if (err)
3498 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3499 				    "close event failed: - cm_id = %p\n",
3500 				    cm_id);
3501 		irdma_qp_disconnect(iwqp);
3502 	}
3503 	irdma_rem_ref_cm_node(iwqp->cm_node);
3504 }
3505 
3506 /**
3507  * irdma_disconnect_worker - worker for connection close
3508  * @work: points or disconn structure
3509  */
3510 static void
3511 irdma_disconnect_worker(struct work_struct *work)
3512 {
3513 	struct disconn_work *dwork = container_of(work, struct disconn_work, work);
3514 	struct irdma_qp *iwqp = dwork->iwqp;
3515 
3516 	kfree(dwork);
3517 	irdma_cm_disconn_true(iwqp);
3518 	irdma_qp_rem_ref(&iwqp->ibqp);
3519 }
3520 
3521 /**
3522  * irdma_free_lsmm_rsrc - free lsmm memory and deregister
3523  * @iwqp: associate qp for the connection
3524  */
3525 void
3526 irdma_free_lsmm_rsrc(struct irdma_qp *iwqp)
3527 {
3528 	struct irdma_device *iwdev;
3529 
3530 	iwdev = iwqp->iwdev;
3531 
3532 	if (iwqp->ietf_mem.va) {
3533 		if (iwqp->lsmm_mr)
3534 			kc_free_lsmm_dereg_mr(iwdev, iwqp);
3535 		irdma_free_dma_mem(iwdev->rf->sc_dev.hw,
3536 				   &iwqp->ietf_mem);
3537 		iwqp->ietf_mem.va = NULL;
3538 	}
3539 }
3540 
3541 /**
3542  * irdma_accept - registered call for connection to be accepted
3543  * @cm_id: cm information for passive connection
3544  * @conn_param: accpet parameters
3545  */
3546 int
3547 irdma_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3548 {
3549 	struct ib_qp *ibqp;
3550 	struct irdma_qp *iwqp;
3551 	struct irdma_device *iwdev;
3552 	struct irdma_sc_dev *dev;
3553 	struct irdma_cm_node *cm_node;
3554 	struct ib_qp_attr attr = {0};
3555 	int passive_state;
3556 	struct ib_mr *ibmr;
3557 	struct irdma_pd *iwpd;
3558 	u16 buf_len = 0;
3559 	struct irdma_kmem_info accept;
3560 	u64 tagged_offset;
3561 	int wait_ret;
3562 	int ret = 0;
3563 
3564 	ibqp = irdma_get_qp(cm_id->device, conn_param->qpn);
3565 	if (!ibqp)
3566 		return -EINVAL;
3567 
3568 	iwqp = to_iwqp(ibqp);
3569 	iwdev = iwqp->iwdev;
3570 	dev = &iwdev->rf->sc_dev;
3571 	cm_node = cm_id->provider_data;
3572 
3573 	if (((struct sockaddr_in *)&cm_id->local_addr)->sin_family == AF_INET) {
3574 		cm_node->ipv4 = true;
3575 		cm_node->vlan_id = irdma_get_vlan_ipv4(cm_node->loc_addr);
3576 	} else {
3577 		cm_node->ipv4 = false;
3578 		irdma_netdev_vlan_ipv6(cm_node->loc_addr, &cm_node->vlan_id,
3579 				       NULL);
3580 	}
3581 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "Accept vlan_id=%d\n",
3582 		    cm_node->vlan_id);
3583 
3584 	if (cm_node->state == IRDMA_CM_STATE_LISTENER_DESTROYED) {
3585 		ret = -EINVAL;
3586 		goto error;
3587 	}
3588 
3589 	passive_state = atomic_add_return(1, &cm_node->passive_state);
3590 	if (passive_state == IRDMA_SEND_RESET_EVENT) {
3591 		ret = -ECONNRESET;
3592 		goto error;
3593 	}
3594 
3595 	buf_len = conn_param->private_data_len + IRDMA_MAX_IETF_SIZE;
3596 	iwqp->ietf_mem.size = buf_len;
3597 	iwqp->ietf_mem.va = irdma_allocate_dma_mem(dev->hw, &iwqp->ietf_mem,
3598 						   iwqp->ietf_mem.size, 1);
3599 	if (!iwqp->ietf_mem.va) {
3600 		ret = -ENOMEM;
3601 		goto error;
3602 	}
3603 
3604 	cm_node->pdata.size = conn_param->private_data_len;
3605 	accept.addr = iwqp->ietf_mem.va;
3606 	accept.size = irdma_cm_build_mpa_frame(cm_node, &accept, MPA_KEY_REPLY);
3607 	memcpy((u8 *)accept.addr + accept.size, conn_param->private_data,
3608 	       conn_param->private_data_len);
3609 
3610 	if (cm_node->dev->ws_add(iwqp->sc_qp.vsi, cm_node->user_pri)) {
3611 		ret = -ENOMEM;
3612 		goto error;
3613 	}
3614 	iwqp->sc_qp.user_pri = cm_node->user_pri;
3615 	irdma_qp_add_qos(&iwqp->sc_qp);
3616 	if (cm_node->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_2)
3617 		iwdev->rf->check_fc(&iwdev->vsi, &iwqp->sc_qp);
3618 	/* setup our first outgoing iWarp send WQE (the IETF frame response) */
3619 	iwpd = iwqp->iwpd;
3620 	tagged_offset = (uintptr_t)iwqp->ietf_mem.va;
3621 	ibmr = irdma_reg_phys_mr(&iwpd->ibpd, iwqp->ietf_mem.pa, buf_len,
3622 				 IB_ACCESS_LOCAL_WRITE, &tagged_offset);
3623 	if (IS_ERR(ibmr)) {
3624 		ret = -ENOMEM;
3625 		goto error;
3626 	}
3627 
3628 	ibmr->pd = &iwpd->ibpd;
3629 	ibmr->device = iwpd->ibpd.device;
3630 	iwqp->lsmm_mr = ibmr;
3631 	if (iwqp->page)
3632 		iwqp->sc_qp.qp_uk.sq_base = kmap_local_page(iwqp->page);
3633 
3634 	cm_node->lsmm_size = accept.size + conn_param->private_data_len;
3635 	irdma_sc_send_lsmm(&iwqp->sc_qp, iwqp->ietf_mem.va, cm_node->lsmm_size,
3636 			   ibmr->lkey);
3637 
3638 	if (iwqp->page)
3639 		kunmap_local(iwqp->sc_qp.qp_uk.sq_base);
3640 
3641 	iwqp->cm_id = cm_id;
3642 	cm_node->cm_id = cm_id;
3643 
3644 	cm_id->provider_data = iwqp;
3645 	iwqp->active_conn = 0;
3646 	iwqp->cm_node = cm_node;
3647 	cm_node->iwqp = iwqp;
3648 	irdma_cm_init_tsa_conn(iwqp, cm_node);
3649 	irdma_qp_add_ref(&iwqp->ibqp);
3650 	cm_id->add_ref(cm_id);
3651 
3652 	attr.qp_state = IB_QPS_RTS;
3653 	cm_node->qhash_set = false;
3654 	cm_node->cm_core->cm_free_ah(cm_node);
3655 
3656 	irdma_modify_qp(&iwqp->ibqp, &attr, IB_QP_STATE, NULL);
3657 	if (dev->hw_attrs.uk_attrs.feature_flags & IRDMA_FEATURE_RTS_AE) {
3658 		wait_ret = wait_event_interruptible_timeout(iwqp->waitq,
3659 							    iwqp->rts_ae_rcvd,
3660 							    IRDMA_MAX_TIMEOUT);
3661 		if (!wait_ret) {
3662 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3663 				    "Slow Connection: cm_node=%p, loc_port=%d, rem_port=%d, cm_id=%p\n",
3664 				    cm_node, cm_node->loc_port,
3665 				    cm_node->rem_port, cm_node->cm_id);
3666 			ret = -ECONNRESET;
3667 			goto error;
3668 		}
3669 	}
3670 
3671 	irdma_send_cm_event(cm_node, cm_id, IW_CM_EVENT_ESTABLISHED, 0);
3672 	cm_node->accelerated = true;
3673 	complete(&cm_node->establish_comp);
3674 
3675 	if (cm_node->accept_pend) {
3676 		atomic_dec(&cm_node->listener->pend_accepts_cnt);
3677 		cm_node->accept_pend = 0;
3678 	}
3679 
3680 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3681 		    "rem_port=0x%04x, loc_port=0x%04x rem_addr=%pI4 loc_addr=%pI4 cm_node=%p cm_id=%p qp_id = %d\n\n",
3682 		    cm_node->rem_port, cm_node->loc_port, cm_node->rem_addr,
3683 		    cm_node->loc_addr, cm_node, cm_id, ibqp->qp_num);
3684 	cm_node->cm_core->stats_accepts++;
3685 
3686 	return 0;
3687 error:
3688 	irdma_free_lsmm_rsrc(iwqp);
3689 	irdma_rem_ref_cm_node(cm_node);
3690 
3691 	return ret;
3692 }
3693 
3694 /**
3695  * irdma_reject - registered call for connection to be rejected
3696  * @cm_id: cm information for passive connection
3697  * @pdata: private data to be sent
3698  * @pdata_len: private data length
3699  */
3700 int
3701 irdma_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3702 {
3703 	struct irdma_device *iwdev;
3704 	struct irdma_cm_node *cm_node;
3705 
3706 	cm_node = cm_id->provider_data;
3707 	cm_node->pdata.size = pdata_len;
3708 
3709 	iwdev = to_iwdev(cm_id->device);
3710 	if (!iwdev)
3711 		return -EINVAL;
3712 
3713 	cm_node->cm_core->stats_rejects++;
3714 
3715 	if (pdata_len + sizeof(struct ietf_mpa_v2) > IRDMA_MAX_CM_BUF)
3716 		return -EINVAL;
3717 
3718 	return irdma_cm_reject(cm_node, pdata, pdata_len);
3719 }
3720 
3721 /**
3722  * irdma_connect - registered call for connection to be established
3723  * @cm_id: cm information for passive connection
3724  * @conn_param: Information about the connection
3725  */
3726 int
3727 irdma_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3728 {
3729 	struct ib_qp *ibqp;
3730 	struct irdma_qp *iwqp;
3731 	struct irdma_device *iwdev;
3732 	struct irdma_cm_node *cm_node;
3733 	struct irdma_cm_info cm_info;
3734 	struct sockaddr_in *laddr;
3735 	struct sockaddr_in *raddr;
3736 	struct sockaddr_in6 *laddr6;
3737 	struct sockaddr_in6 *raddr6;
3738 	int ret = 0;
3739 
3740 	ibqp = irdma_get_qp(cm_id->device, conn_param->qpn);
3741 	if (!ibqp)
3742 		return -EINVAL;
3743 	iwqp = to_iwqp(ibqp);
3744 	if (!iwqp)
3745 		return -EINVAL;
3746 	iwdev = iwqp->iwdev;
3747 	if (!iwdev)
3748 		return -EINVAL;
3749 
3750 	laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3751 	raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3752 	laddr6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3753 	raddr6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3754 
3755 	if (!(laddr->sin_port) || !(raddr->sin_port))
3756 		return -EINVAL;
3757 
3758 	iwqp->active_conn = 1;
3759 	iwqp->cm_id = NULL;
3760 	cm_id->provider_data = iwqp;
3761 
3762 	/* set up the connection params for the node */
3763 	if (cm_id->remote_addr.ss_family == AF_INET) {
3764 		if (iwdev->vsi.mtu < IRDMA_MIN_MTU_IPV4)
3765 			return -EINVAL;
3766 
3767 		cm_info.ipv4 = true;
3768 		memset(cm_info.loc_addr, 0, sizeof(cm_info.loc_addr));
3769 		memset(cm_info.rem_addr, 0, sizeof(cm_info.rem_addr));
3770 		cm_info.loc_addr[0] = ntohl(laddr->sin_addr.s_addr);
3771 		cm_info.rem_addr[0] = ntohl(raddr->sin_addr.s_addr);
3772 		cm_info.loc_port = ntohs(laddr->sin_port);
3773 		cm_info.rem_port = ntohs(raddr->sin_port);
3774 		cm_info.vlan_id = irdma_get_vlan_ipv4(cm_info.loc_addr);
3775 	} else {
3776 		if (iwdev->vsi.mtu < IRDMA_MIN_MTU_IPV6)
3777 			return -EINVAL;
3778 
3779 		cm_info.ipv4 = false;
3780 		irdma_copy_ip_ntohl(cm_info.loc_addr,
3781 				    laddr6->sin6_addr.__u6_addr.__u6_addr32);
3782 		irdma_copy_ip_ntohl(cm_info.rem_addr,
3783 				    raddr6->sin6_addr.__u6_addr.__u6_addr32);
3784 		cm_info.loc_port = ntohs(laddr6->sin6_port);
3785 		cm_info.rem_port = ntohs(raddr6->sin6_port);
3786 		irdma_netdev_vlan_ipv6(cm_info.loc_addr, &cm_info.vlan_id, NULL);
3787 	}
3788 	cm_info.cm_id = cm_id;
3789 	cm_info.qh_qpid = iwdev->vsi.ilq->qp_id;
3790 	cm_info.tos = cm_id->tos;
3791 	if (iwdev->vsi.dscp_mode) {
3792 		cm_info.user_pri =
3793 		    iwqp->sc_qp.vsi->dscp_map[irdma_tos2dscp(cm_info.tos)];
3794 	} else {
3795 		cm_info.user_pri = rt_tos2priority(cm_id->tos);
3796 		cm_info.user_pri = irdma_get_egress_vlan_prio(cm_info.loc_addr,
3797 							      cm_info.user_pri,
3798 							      cm_info.ipv4);
3799 	}
3800 
3801 	if (iwqp->sc_qp.dev->ws_add(iwqp->sc_qp.vsi, cm_info.user_pri))
3802 		return -ENOMEM;
3803 	iwqp->sc_qp.user_pri = cm_info.user_pri;
3804 	irdma_qp_add_qos(&iwqp->sc_qp);
3805 	if (iwdev->rf->sc_dev.hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_2)
3806 		iwdev->rf->check_fc(&iwdev->vsi, &iwqp->sc_qp);
3807 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_DCB,
3808 		    "TOS:[%d] UP:[%d]\n", cm_id->tos,
3809 		    cm_info.user_pri);
3810 
3811 	ret = irdma_create_cm_node(&iwdev->cm_core, iwdev, conn_param, &cm_info,
3812 				   &cm_node);
3813 	if (ret)
3814 		return ret;
3815 	ret = cm_node->cm_core->cm_create_ah(cm_node, true);
3816 	if (ret)
3817 		goto err;
3818 	if (irdma_manage_qhash(iwdev, &cm_info,
3819 			       IRDMA_QHASH_TYPE_TCP_ESTABLISHED,
3820 			       IRDMA_QHASH_MANAGE_TYPE_ADD, NULL, true)) {
3821 		ret = -EINVAL;
3822 		goto err;
3823 	}
3824 	cm_node->qhash_set = true;
3825 
3826 	cm_node->apbvt_entry = irdma_add_apbvt(iwdev, cm_info.loc_port);
3827 	if (!cm_node->apbvt_entry) {
3828 		ret = -EINVAL;
3829 		goto err;
3830 	}
3831 
3832 	cm_node->apbvt_set = true;
3833 	iwqp->cm_node = cm_node;
3834 	cm_node->iwqp = iwqp;
3835 	iwqp->cm_id = cm_id;
3836 	irdma_qp_add_ref(&iwqp->ibqp);
3837 	cm_id->add_ref(cm_id);
3838 
3839 	if (cm_node->state != IRDMA_CM_STATE_OFFLOADED) {
3840 		cm_node->state = IRDMA_CM_STATE_SYN_SENT;
3841 		ret = irdma_send_syn(cm_node, 0);
3842 		if (ret)
3843 			goto err;
3844 	}
3845 
3846 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3847 		    "rem_port=0x%04x, loc_port=0x%04x rem_addr=%pI4 loc_addr=%pI4 cm_node=%p cm_id=%p qp_id = %d\n\n",
3848 		    cm_node->rem_port, cm_node->loc_port, cm_node->rem_addr,
3849 		    cm_node->loc_addr, cm_node, cm_id, ibqp->qp_num);
3850 
3851 	return 0;
3852 
3853 err:
3854 	if (cm_info.ipv4)
3855 		irdma_debug(&iwdev->rf->sc_dev,
3856 			    IRDMA_DEBUG_CM, "connect() FAILED: dest addr=%pI4",
3857 			    cm_info.rem_addr);
3858 	else
3859 		irdma_debug(&iwdev->rf->sc_dev,
3860 			    IRDMA_DEBUG_CM, "connect() FAILED: dest addr=%pI6",
3861 			    cm_info.rem_addr);
3862 	irdma_rem_ref_cm_node(cm_node);
3863 	iwdev->cm_core.stats_connect_errs++;
3864 
3865 	return ret;
3866 }
3867 
3868 /**
3869  * irdma_create_listen - registered call creating listener
3870  * @cm_id: cm information for passive connection
3871  * @backlog: to max accept pending count
3872  */
3873 int
3874 irdma_create_listen(struct iw_cm_id *cm_id, int backlog)
3875 {
3876 	struct irdma_device *iwdev;
3877 	struct irdma_cm_listener *cm_listen_node;
3878 	struct irdma_cm_info cm_info = {0};
3879 	struct sockaddr_in *laddr;
3880 	struct sockaddr_in6 *laddr6;
3881 	bool wildcard = false;
3882 	int err;
3883 
3884 	iwdev = to_iwdev(cm_id->device);
3885 	if (!iwdev)
3886 		return -EINVAL;
3887 
3888 	laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3889 	laddr6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3890 	cm_info.qh_qpid = iwdev->vsi.ilq->qp_id;
3891 
3892 	if (laddr->sin_family == AF_INET) {
3893 		if (iwdev->vsi.mtu < IRDMA_MIN_MTU_IPV4)
3894 			return -EINVAL;
3895 
3896 		cm_info.ipv4 = true;
3897 		cm_info.loc_addr[0] = ntohl(laddr->sin_addr.s_addr);
3898 		cm_info.loc_port = ntohs(laddr->sin_port);
3899 
3900 		if (laddr->sin_addr.s_addr != htonl(INADDR_ANY)) {
3901 			cm_info.vlan_id = irdma_get_vlan_ipv4(cm_info.loc_addr);
3902 		} else {
3903 			cm_info.vlan_id = 0xFFFF;
3904 			wildcard = true;
3905 		}
3906 	} else {
3907 		if (iwdev->vsi.mtu < IRDMA_MIN_MTU_IPV6)
3908 			return -EINVAL;
3909 
3910 		cm_info.ipv4 = false;
3911 		irdma_copy_ip_ntohl(cm_info.loc_addr,
3912 				    laddr6->sin6_addr.__u6_addr.__u6_addr32);
3913 		cm_info.loc_port = ntohs(laddr6->sin6_port);
3914 		if (!IN6_IS_ADDR_UNSPECIFIED(&laddr6->sin6_addr)) {
3915 			irdma_netdev_vlan_ipv6(cm_info.loc_addr,
3916 					       &cm_info.vlan_id, NULL);
3917 		} else {
3918 			cm_info.vlan_id = 0xFFFF;
3919 			wildcard = true;
3920 		}
3921 	}
3922 
3923 	if (cm_info.vlan_id >= VLAN_N_VID && iwdev->dcb_vlan_mode)
3924 		cm_info.vlan_id = 0;
3925 	cm_info.backlog = backlog;
3926 	cm_info.cm_id = cm_id;
3927 
3928 	cm_listen_node = irdma_make_listen_node(&iwdev->cm_core, iwdev,
3929 						&cm_info);
3930 	if (!cm_listen_node) {
3931 		irdma_debug(&iwdev->rf->sc_dev,
3932 			    IRDMA_DEBUG_CM, "cm_listen_node == NULL\n");
3933 		return -ENOMEM;
3934 	}
3935 
3936 	cm_id->provider_data = cm_listen_node;
3937 
3938 	cm_listen_node->tos = cm_id->tos;
3939 	if (iwdev->vsi.dscp_mode)
3940 		cm_listen_node->user_pri =
3941 		    iwdev->vsi.dscp_map[irdma_tos2dscp(cm_id->tos)];
3942 	else
3943 		cm_listen_node->user_pri = rt_tos2priority(cm_id->tos);
3944 	cm_info.user_pri = cm_listen_node->user_pri;
3945 	if (!cm_listen_node->reused_node) {
3946 		if (wildcard) {
3947 			err = irdma_add_mqh(iwdev, &cm_info, cm_listen_node);
3948 			if (err)
3949 				goto error;
3950 		} else {
3951 			if (!iwdev->vsi.dscp_mode)
3952 				cm_info.user_pri = cm_listen_node->user_pri =
3953 				    irdma_get_egress_vlan_prio(cm_info.loc_addr,
3954 							       cm_info.user_pri,
3955 							       cm_info.ipv4);
3956 			err = irdma_manage_qhash(iwdev, &cm_info,
3957 						 IRDMA_QHASH_TYPE_TCP_SYN,
3958 						 IRDMA_QHASH_MANAGE_TYPE_ADD,
3959 						 NULL, true);
3960 			if (err)
3961 				goto error;
3962 
3963 			cm_listen_node->qhash_set = true;
3964 		}
3965 
3966 		cm_listen_node->apbvt_entry = irdma_add_apbvt(iwdev,
3967 							      cm_info.loc_port);
3968 		if (!cm_listen_node->apbvt_entry)
3969 			goto error;
3970 	}
3971 	cm_id->add_ref(cm_id);
3972 	cm_listen_node->cm_core->stats_listen_created++;
3973 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
3974 		    "loc_port=0x%04x loc_addr=%pI4 cm_listen_node=%p cm_id=%p qhash_set=%d vlan_id=%d\n",
3975 		    cm_listen_node->loc_port, cm_listen_node->loc_addr,
3976 		    cm_listen_node, cm_listen_node->cm_id,
3977 		    cm_listen_node->qhash_set, cm_listen_node->vlan_id);
3978 
3979 	return 0;
3980 
3981 error:
3982 
3983 	irdma_cm_del_listen(&iwdev->cm_core, cm_listen_node, false);
3984 
3985 	return -EINVAL;
3986 }
3987 
3988 /**
3989  * irdma_destroy_listen - registered call to destroy listener
3990  * @cm_id: cm information for passive connection
3991  */
3992 int
3993 irdma_destroy_listen(struct iw_cm_id *cm_id)
3994 {
3995 	struct irdma_device *iwdev;
3996 
3997 	iwdev = to_iwdev(cm_id->device);
3998 	if (cm_id->provider_data)
3999 		irdma_cm_del_listen(&iwdev->cm_core, cm_id->provider_data,
4000 				    true);
4001 	else
4002 		irdma_debug(&iwdev->rf->sc_dev,
4003 			    IRDMA_DEBUG_CM, "cm_id->provider_data was NULL\n");
4004 
4005 	cm_id->rem_ref(cm_id);
4006 
4007 	return 0;
4008 }
4009 
4010 /**
4011  * irdma_iw_teardown_list_prep - add conn nodes slated for tear
4012  * down to list
4013  * @cm_core: cm's core
4014  * @teardown_list: a list to which cm_node will be selected
4015  * @ipaddr: pointer to ip address
4016  * @nfo: pointer to cm_info structure instance
4017  * @disconnect_all: flag indicating disconnect all QPs
4018  */
4019 static void
4020 irdma_iw_teardown_list_prep(struct irdma_cm_core *cm_core,
4021 			    struct list_head *teardown_list,
4022 			    u32 *ipaddr,
4023 			    struct irdma_cm_info *nfo,
4024 			    bool disconnect_all)
4025 {
4026 	struct irdma_cm_node *cm_node;
4027 	int bkt;
4028 
4029 	HASH_FOR_EACH_RCU(cm_core->cm_hash_tbl, bkt, cm_node, list) {
4030 		if ((disconnect_all ||
4031 		     (nfo->vlan_id == cm_node->vlan_id &&
4032 		      !memcmp(cm_node->loc_addr, ipaddr, nfo->ipv4 ? 4 : 16))) &&
4033 		    atomic_inc_not_zero(&cm_node->refcnt))
4034 			list_add(&cm_node->teardown_entry, teardown_list);
4035 	}
4036 }
4037 
4038 static inline bool
4039 irdma_ip_vlan_match(u32 *ip1, u16 vlan_id1,
4040 		    bool check_vlan, u32 *ip2,
4041 		    u16 vlan_id2, bool ipv4)
4042 {
4043 	return (!check_vlan || vlan_id1 == vlan_id2) &&
4044 	    !memcmp(ip1, ip2, ipv4 ? 4 : 16);
4045 }
4046 
4047 /**
4048  * irdma_roce_teardown_list_prep - add conn nodes slated for
4049  * tear down to list
4050  * @iwdev: RDMA device
4051  * @teardown_list: a list to which cm_node will be selected
4052  * @ipaddr: pointer to ip address
4053  * @nfo: pointer to cm_info structure instance
4054  * @disconnect_all: flag indicating disconnect all QPs
4055  */
4056 static void
4057 irdma_roce_teardown_list_prep(struct irdma_device *iwdev,
4058 			      struct list_head *teardown_list,
4059 			      u32 *ipaddr,
4060 			      struct irdma_cm_info *nfo,
4061 			      bool disconnect_all)
4062 {
4063 	struct irdma_sc_vsi *vsi = &iwdev->vsi;
4064 	struct irdma_sc_qp *sc_qp;
4065 	struct list_head *list_node;
4066 	struct irdma_qp *qp;
4067 	unsigned long flags;
4068 	int i;
4069 
4070 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++) {
4071 		mutex_lock(&vsi->qos[i].qos_mutex);
4072 		list_for_each(list_node, &vsi->qos[i].qplist) {
4073 			u32 qp_ip[4];
4074 
4075 			sc_qp = container_of(list_node, struct irdma_sc_qp,
4076 					     list);
4077 			if (sc_qp->qp_uk.qp_type != IRDMA_QP_TYPE_ROCE_RC)
4078 				continue;
4079 
4080 			qp = sc_qp->qp_uk.back_qp;
4081 			if (!disconnect_all) {
4082 				if (nfo->ipv4)
4083 					qp_ip[0] = qp->udp_info.local_ipaddr[3];
4084 				else
4085 					memcpy(qp_ip,
4086 					       &qp->udp_info.local_ipaddr[0],
4087 					       sizeof(qp_ip));
4088 			}
4089 
4090 			if (disconnect_all ||
4091 			    irdma_ip_vlan_match(qp_ip,
4092 						qp->udp_info.vlan_tag & EVL_VLID_MASK,
4093 						qp->udp_info.insert_vlan_tag,
4094 						ipaddr, nfo->vlan_id, nfo->ipv4)) {
4095 				spin_lock_irqsave(&iwdev->rf->qptable_lock, flags);
4096 				if (iwdev->rf->qp_table[sc_qp->qp_uk.qp_id]) {
4097 					irdma_qp_add_ref(&qp->ibqp);
4098 					list_add(&qp->teardown_entry, teardown_list);
4099 				}
4100 				spin_unlock_irqrestore(&iwdev->rf->qptable_lock, flags);
4101 			}
4102 		}
4103 		mutex_unlock(&vsi->qos[i].qos_mutex);
4104 	}
4105 }
4106 
4107 /**
4108  * irdma_cm_event_connected - handle connected active node
4109  * @event: the info for cm_node of connection
4110  */
4111 static void
4112 irdma_cm_event_connected(struct irdma_cm_event *event)
4113 {
4114 	struct irdma_qp *iwqp;
4115 	struct irdma_device *iwdev;
4116 	struct irdma_cm_node *cm_node;
4117 	struct irdma_sc_dev *dev;
4118 	struct ib_qp_attr attr = {0};
4119 	struct iw_cm_id *cm_id;
4120 	int status;
4121 	bool read0;
4122 	int wait_ret = 0;
4123 
4124 	cm_node = event->cm_node;
4125 	cm_id = cm_node->cm_id;
4126 	iwqp = cm_id->provider_data;
4127 	iwdev = iwqp->iwdev;
4128 	dev = &iwdev->rf->sc_dev;
4129 	if (iwqp->sc_qp.qp_uk.destroy_pending) {
4130 		status = -ETIMEDOUT;
4131 		goto error;
4132 	}
4133 
4134 	irdma_cm_init_tsa_conn(iwqp, cm_node);
4135 	read0 = (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO);
4136 	if (iwqp->page)
4137 		iwqp->sc_qp.qp_uk.sq_base = kmap_local_page(iwqp->page);
4138 	irdma_sc_send_rtt(&iwqp->sc_qp, read0);
4139 	if (iwqp->page)
4140 		kunmap_local(iwqp->sc_qp.qp_uk.sq_base);
4141 
4142 	attr.qp_state = IB_QPS_RTS;
4143 	cm_node->qhash_set = false;
4144 	irdma_modify_qp(&iwqp->ibqp, &attr, IB_QP_STATE, NULL);
4145 	if (dev->hw_attrs.uk_attrs.feature_flags & IRDMA_FEATURE_RTS_AE) {
4146 		wait_ret = wait_event_interruptible_timeout(iwqp->waitq,
4147 							    iwqp->rts_ae_rcvd,
4148 							    IRDMA_MAX_TIMEOUT);
4149 		if (!wait_ret)
4150 			irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
4151 				    "Slow Connection: cm_node=%p, loc_port=%d, rem_port=%d, cm_id=%p\n",
4152 				    cm_node, cm_node->loc_port,
4153 				    cm_node->rem_port, cm_node->cm_id);
4154 	}
4155 
4156 	irdma_send_cm_event(cm_node, cm_id, IW_CM_EVENT_CONNECT_REPLY, 0);
4157 	cm_node->accelerated = true;
4158 	complete(&cm_node->establish_comp);
4159 	cm_node->cm_core->cm_free_ah(cm_node);
4160 	return;
4161 
4162 error:
4163 	iwqp->cm_id = NULL;
4164 	cm_id->provider_data = NULL;
4165 	irdma_send_cm_event(event->cm_node, cm_id, IW_CM_EVENT_CONNECT_REPLY,
4166 			    status);
4167 	irdma_rem_ref_cm_node(event->cm_node);
4168 }
4169 
4170 /**
4171  * irdma_cm_event_reset - handle reset
4172  * @event: the info for cm_node of connection
4173  */
4174 static void
4175 irdma_cm_event_reset(struct irdma_cm_event *event)
4176 {
4177 	struct irdma_cm_node *cm_node = event->cm_node;
4178 	struct iw_cm_id *cm_id = cm_node->cm_id;
4179 	struct irdma_qp *iwqp;
4180 
4181 	if (!cm_id)
4182 		return;
4183 
4184 	iwqp = cm_id->provider_data;
4185 	if (!iwqp)
4186 		return;
4187 
4188 	irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
4189 		    "reset event %p - cm_id = %p\n",
4190 		    event->cm_node, cm_id);
4191 	iwqp->cm_id = NULL;
4192 
4193 	irdma_send_cm_event(cm_node, cm_node->cm_id, IW_CM_EVENT_DISCONNECT,
4194 			    -ECONNRESET);
4195 	irdma_send_cm_event(cm_node, cm_node->cm_id, IW_CM_EVENT_CLOSE, 0);
4196 }
4197 
4198 /**
4199  * irdma_cm_event_handler - send event to cm upper layer
4200  * @work: pointer of cm event info.
4201  */
4202 static void
4203 irdma_cm_event_handler(struct work_struct *work)
4204 {
4205 	struct irdma_cm_event *event = container_of(work, struct irdma_cm_event, event_work);
4206 	struct irdma_cm_node *cm_node;
4207 
4208 	if (!event || !event->cm_node || !event->cm_node->cm_core)
4209 		return;
4210 
4211 	cm_node = event->cm_node;
4212 
4213 	switch (event->type) {
4214 	case IRDMA_CM_EVENT_MPA_REQ:
4215 		irdma_send_cm_event(cm_node, cm_node->cm_id,
4216 				    IW_CM_EVENT_CONNECT_REQUEST, 0);
4217 		break;
4218 	case IRDMA_CM_EVENT_RESET:
4219 		irdma_cm_event_reset(event);
4220 		break;
4221 	case IRDMA_CM_EVENT_CONNECTED:
4222 		if (!event->cm_node->cm_id ||
4223 		    event->cm_node->state != IRDMA_CM_STATE_OFFLOADED)
4224 			break;
4225 		irdma_cm_event_connected(event);
4226 		break;
4227 	case IRDMA_CM_EVENT_MPA_REJECT:
4228 		if (!event->cm_node->cm_id ||
4229 		    cm_node->state == IRDMA_CM_STATE_OFFLOADED)
4230 			break;
4231 		irdma_send_cm_event(cm_node, cm_node->cm_id,
4232 				    IW_CM_EVENT_CONNECT_REPLY, -ECONNREFUSED);
4233 		break;
4234 	case IRDMA_CM_EVENT_ABORTED:
4235 		if (!event->cm_node->cm_id ||
4236 		    event->cm_node->state == IRDMA_CM_STATE_OFFLOADED)
4237 			break;
4238 		irdma_event_connect_error(event);
4239 		break;
4240 	default:
4241 		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
4242 			    "bad event type = %d\n",
4243 			    event->type);
4244 		break;
4245 	}
4246 
4247 	irdma_rem_ref_cm_node(event->cm_node);
4248 	kfree(event);
4249 }
4250 
4251 /**
4252  * irdma_cm_post_event - queue event request for worker thread
4253  * @event: cm node's info for up event call
4254  */
4255 static void
4256 irdma_cm_post_event(struct irdma_cm_event *event)
4257 {
4258 	atomic_inc(&event->cm_node->refcnt);
4259 	INIT_WORK(&event->event_work, irdma_cm_event_handler);
4260 	queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
4261 }
4262 
4263 /**
4264  * irdma_cm_teardown_connections - teardown QPs
4265  * @iwdev: device pointer
4266  * @ipaddr: Pointer to IPv4 or IPv6 address
4267  * @nfo: Connection info
4268  * @disconnect_all: flag indicating disconnect all QPs
4269  *
4270  * teardown QPs where source or destination addr matches ip addr
4271  */
4272 static void
4273 irdma_cm_teardown_connections(struct irdma_device *iwdev,
4274 			      u32 *ipaddr,
4275 			      struct irdma_cm_info *nfo,
4276 			      bool disconnect_all)
4277 {
4278 	struct irdma_cm_core *cm_core = &iwdev->cm_core;
4279 	struct list_head *list_core_temp;
4280 	struct list_head *list_node;
4281 	struct irdma_cm_node *cm_node;
4282 	struct list_head teardown_list;
4283 	struct ib_qp_attr attr;
4284 	struct irdma_qp *qp;
4285 
4286 	INIT_LIST_HEAD(&teardown_list);
4287 
4288 	rcu_read_lock();
4289 	irdma_iw_teardown_list_prep(cm_core, &teardown_list, ipaddr, nfo, disconnect_all);
4290 	rcu_read_unlock();
4291 
4292 	attr.qp_state = IB_QPS_ERR;
4293 	list_for_each_safe(list_node, list_core_temp, &teardown_list) {
4294 		cm_node = container_of(list_node, struct irdma_cm_node,
4295 				       teardown_entry);
4296 		irdma_modify_qp(&cm_node->iwqp->ibqp, &attr, IB_QP_STATE, NULL);
4297 		if (iwdev->rf->reset)
4298 			irdma_cm_disconn(cm_node->iwqp);
4299 		irdma_rem_ref_cm_node(cm_node);
4300 	}
4301 
4302 	if (!rdma_protocol_roce(&iwdev->ibdev, 1))
4303 		return;
4304 
4305 	INIT_LIST_HEAD(&teardown_list);
4306 	irdma_roce_teardown_list_prep(iwdev, &teardown_list, ipaddr, nfo, disconnect_all);
4307 
4308 	list_for_each_safe(list_node, list_core_temp, &teardown_list) {
4309 		qp = container_of(list_node, struct irdma_qp, teardown_entry);
4310 		irdma_modify_qp_roce(&qp->ibqp, &attr, IB_QP_STATE, NULL);
4311 		irdma_ib_qp_event(qp, IRDMA_QP_EVENT_CATASTROPHIC);
4312 		irdma_qp_rem_ref(&qp->ibqp);
4313 	}
4314 }
4315