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