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