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