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