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