1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0 3 * 4 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 6 * Copyright (c) 2005 Mellanox Technologies. All rights reserved. 7 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved. 8 * 9 * This software is available to you under a choice of one of two 10 * licenses. You may choose to be licensed under the terms of the GNU 11 * General Public License (GPL) Version 2, available from the file 12 * COPYING in the main directory of this source tree, or the 13 * OpenIB.org BSD license below: 14 * 15 * Redistribution and use in source and binary forms, with or 16 * without modification, are permitted provided that the following 17 * conditions are met: 18 * 19 * - Redistributions of source code must retain the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer. 22 * 23 * - Redistributions in binary form must reproduce the above 24 * copyright notice, this list of conditions and the following 25 * disclaimer in the documentation and/or other materials 26 * provided with the distribution. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 35 * SOFTWARE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "ipoib.h" 42 43 #include <rdma/ib_cache.h> 44 45 #include <security/mac/mac_framework.h> 46 47 #include <linux/delay.h> 48 #include <linux/dma-mapping.h> 49 50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA 51 static int data_debug_level; 52 53 module_param(data_debug_level, int, 0644); 54 MODULE_PARM_DESC(data_debug_level, 55 "Enable data path debug tracing if > 0"); 56 #endif 57 58 static DEFINE_MUTEX(pkey_mutex); 59 60 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *priv, 61 struct ib_pd *pd, struct ib_ah_attr *attr) 62 { 63 struct ipoib_ah *ah; 64 65 ah = kmalloc(sizeof *ah, GFP_KERNEL); 66 if (!ah) 67 return NULL; 68 69 ah->priv = priv; 70 ah->last_send = 0; 71 kref_init(&ah->ref); 72 73 ah->ah = ib_create_ah(pd, attr); 74 if (IS_ERR(ah->ah)) { 75 kfree(ah); 76 ah = NULL; 77 } else 78 ipoib_dbg(priv, "Created ah %p\n", ah->ah); 79 80 return ah; 81 } 82 83 void ipoib_free_ah(struct kref *kref) 84 { 85 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref); 86 struct ipoib_dev_priv *priv = ah->priv; 87 88 unsigned long flags; 89 90 spin_lock_irqsave(&priv->lock, flags); 91 list_add_tail(&ah->list, &priv->dead_ahs); 92 spin_unlock_irqrestore(&priv->lock, flags); 93 } 94 95 void 96 ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req) 97 { 98 struct mbuf *m; 99 int i; 100 101 for (i = 0, m = rx_req->mb; m != NULL; m = m->m_next, i++) 102 ib_dma_unmap_single(priv->ca, rx_req->mapping[i], m->m_len, 103 DMA_FROM_DEVICE); 104 } 105 106 void 107 ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length) 108 { 109 110 m_adj(mb, -(mb->m_pkthdr.len - length)); 111 } 112 113 struct mbuf * 114 ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req, 115 int size) 116 { 117 struct mbuf *mb, *m; 118 int i, j; 119 120 rx_req->mb = NULL; 121 mb = m_getm2(NULL, size, M_NOWAIT, MT_DATA, M_PKTHDR); 122 if (mb == NULL) 123 return (NULL); 124 for (i = 0, m = mb; m != NULL; m = m->m_next, i++) { 125 m->m_len = M_SIZE(m); 126 mb->m_pkthdr.len += m->m_len; 127 rx_req->mapping[i] = ib_dma_map_single(priv->ca, 128 mtod(m, void *), m->m_len, DMA_FROM_DEVICE); 129 if (unlikely(ib_dma_mapping_error(priv->ca, 130 rx_req->mapping[i]))) 131 goto error; 132 133 } 134 rx_req->mb = mb; 135 return (mb); 136 error: 137 for (j = 0, m = mb; j < i; m = m->m_next, j++) 138 ib_dma_unmap_single(priv->ca, rx_req->mapping[j], m->m_len, 139 DMA_FROM_DEVICE); 140 m_freem(mb); 141 return (NULL); 142 143 } 144 145 static int ipoib_ib_post_receive(struct ipoib_dev_priv *priv, int id) 146 { 147 struct ipoib_rx_buf *rx_req; 148 struct ib_recv_wr *bad_wr; 149 struct mbuf *m; 150 int ret; 151 int i; 152 153 rx_req = &priv->rx_ring[id]; 154 for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) { 155 priv->rx_sge[i].addr = rx_req->mapping[i]; 156 priv->rx_sge[i].length = m->m_len; 157 } 158 priv->rx_wr.num_sge = i; 159 priv->rx_wr.wr_id = id | IPOIB_OP_RECV; 160 161 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr); 162 if (unlikely(ret)) { 163 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret); 164 ipoib_dma_unmap_rx(priv, &priv->rx_ring[id]); 165 m_freem(priv->rx_ring[id].mb); 166 priv->rx_ring[id].mb = NULL; 167 } 168 169 return ret; 170 } 171 172 static struct mbuf * 173 ipoib_alloc_rx_mb(struct ipoib_dev_priv *priv, int id) 174 { 175 176 return ipoib_alloc_map_mb(priv, &priv->rx_ring[id], 177 priv->max_ib_mtu + IB_GRH_BYTES); 178 } 179 180 static int ipoib_ib_post_receives(struct ipoib_dev_priv *priv) 181 { 182 int i; 183 184 for (i = 0; i < ipoib_recvq_size; ++i) { 185 if (!ipoib_alloc_rx_mb(priv, i)) { 186 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i); 187 return -ENOMEM; 188 } 189 if (ipoib_ib_post_receive(priv, i)) { 190 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i); 191 return -EIO; 192 } 193 } 194 195 return 0; 196 } 197 198 static void 199 ipoib_ib_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc) 200 { 201 struct ipoib_rx_buf saverx; 202 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV; 203 struct ifnet *dev = priv->dev; 204 struct ipoib_header *eh; 205 struct mbuf *mb; 206 207 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n", 208 wr_id, wc->status); 209 210 if (unlikely(wr_id >= ipoib_recvq_size)) { 211 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n", 212 wr_id, ipoib_recvq_size); 213 return; 214 } 215 216 mb = priv->rx_ring[wr_id].mb; 217 218 if (unlikely(wc->status != IB_WC_SUCCESS)) { 219 if (wc->status != IB_WC_WR_FLUSH_ERR) { 220 ipoib_warn(priv, "failed recv event " 221 "(status=%d, wrid=%d vend_err %x)\n", 222 wc->status, wr_id, wc->vendor_err); 223 goto repost; 224 } 225 if (mb) { 226 ipoib_dma_unmap_rx(priv, &priv->rx_ring[wr_id]); 227 m_freem(mb); 228 priv->rx_ring[wr_id].mb = NULL; 229 } 230 return; 231 } 232 233 /* 234 * Drop packets that this interface sent, ie multicast packets 235 * that the HCA has replicated. 236 */ 237 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num) 238 goto repost; 239 240 memcpy(&saverx, &priv->rx_ring[wr_id], sizeof(saverx)); 241 /* 242 * If we can't allocate a new RX buffer, dump 243 * this packet and reuse the old buffer. 244 */ 245 if (unlikely(!ipoib_alloc_rx_mb(priv, wr_id))) { 246 memcpy(&priv->rx_ring[wr_id], &saverx, sizeof(saverx)); 247 if_inc_counter(dev, IFCOUNTER_IQDROPS, 1); 248 goto repost; 249 } 250 251 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n", 252 wc->byte_len, wc->slid); 253 254 ipoib_dma_unmap_rx(priv, &saverx); 255 ipoib_dma_mb(priv, mb, wc->byte_len); 256 257 if_inc_counter(dev, IFCOUNTER_IPACKETS, 1); 258 if_inc_counter(dev, IFCOUNTER_IBYTES, mb->m_pkthdr.len); 259 mb->m_pkthdr.rcvif = dev; 260 m_adj(mb, sizeof(struct ib_grh) - INFINIBAND_ALEN); 261 eh = mtod(mb, struct ipoib_header *); 262 bzero(eh->hwaddr, 4); /* Zero the queue pair, only dgid is in grh */ 263 264 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->wc_flags & IB_WC_IP_CSUM_OK)) 265 mb->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID; 266 267 dev->if_input(dev, mb); 268 269 repost: 270 if (unlikely(ipoib_ib_post_receive(priv, wr_id))) 271 ipoib_warn(priv, "ipoib_ib_post_receive failed " 272 "for buf %d\n", wr_id); 273 } 274 275 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max) 276 { 277 struct mbuf *mb = tx_req->mb; 278 u64 *mapping = tx_req->mapping; 279 struct mbuf *m, *p; 280 int error; 281 int i; 282 283 for (m = mb, p = NULL, i = 0; m != NULL; p = m, m = m->m_next, i++) { 284 if (m->m_len != 0) 285 continue; 286 if (p == NULL) 287 panic("ipoib_dma_map_tx: First mbuf empty\n"); 288 p->m_next = m_free(m); 289 m = p; 290 i--; 291 } 292 i--; 293 if (i >= max) { 294 tx_req->mb = mb = m_defrag(mb, M_NOWAIT); 295 if (mb == NULL) 296 return -EIO; 297 for (m = mb, i = 0; m != NULL; m = m->m_next, i++); 298 if (i >= max) 299 return -EIO; 300 } 301 error = 0; 302 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) { 303 mapping[i] = ib_dma_map_single(ca, mtod(m, void *), 304 m->m_len, DMA_TO_DEVICE); 305 if (unlikely(ib_dma_mapping_error(ca, mapping[i]))) { 306 error = -EIO; 307 break; 308 } 309 } 310 if (error) { 311 int end; 312 313 end = i; 314 for (m = mb, i = 0; i < end; m = m->m_next, i++) 315 ib_dma_unmap_single(ca, mapping[i], m->m_len, 316 DMA_TO_DEVICE); 317 } 318 return error; 319 } 320 321 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req) 322 { 323 struct mbuf *mb = tx_req->mb; 324 u64 *mapping = tx_req->mapping; 325 struct mbuf *m; 326 int i; 327 328 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) 329 ib_dma_unmap_single(ca, mapping[i], m->m_len, DMA_TO_DEVICE); 330 } 331 332 static void ipoib_ib_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc) 333 { 334 struct ifnet *dev = priv->dev; 335 unsigned int wr_id = wc->wr_id; 336 struct ipoib_tx_buf *tx_req; 337 338 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n", 339 wr_id, wc->status); 340 341 if (unlikely(wr_id >= ipoib_sendq_size)) { 342 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n", 343 wr_id, ipoib_sendq_size); 344 return; 345 } 346 347 tx_req = &priv->tx_ring[wr_id]; 348 349 ipoib_dma_unmap_tx(priv->ca, tx_req); 350 351 if_inc_counter(dev, IFCOUNTER_OPACKETS, 1); 352 353 m_freem(tx_req->mb); 354 355 ++priv->tx_tail; 356 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && 357 (dev->if_drv_flags & IFF_DRV_OACTIVE) && 358 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) 359 dev->if_drv_flags &= ~IFF_DRV_OACTIVE; 360 361 if (wc->status != IB_WC_SUCCESS && 362 wc->status != IB_WC_WR_FLUSH_ERR) 363 ipoib_warn(priv, "failed send event " 364 "(status=%d, wrid=%d vend_err %x)\n", 365 wc->status, wr_id, wc->vendor_err); 366 } 367 368 int 369 ipoib_poll_tx(struct ipoib_dev_priv *priv, bool do_start) 370 { 371 int n, i; 372 373 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc); 374 for (i = 0; i < n; ++i) { 375 struct ib_wc *wc = priv->send_wc + i; 376 if (wc->wr_id & IPOIB_OP_CM) 377 ipoib_cm_handle_tx_wc(priv, wc); 378 else 379 ipoib_ib_handle_tx_wc(priv, wc); 380 } 381 382 if (do_start && n != 0) 383 ipoib_start_locked(priv->dev, priv); 384 385 return n == MAX_SEND_CQE; 386 } 387 388 static void 389 ipoib_poll(struct ipoib_dev_priv *priv) 390 { 391 int n, i; 392 393 poll_more: 394 spin_lock(&priv->drain_lock); 395 for (;;) { 396 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc); 397 for (i = 0; i < n; i++) { 398 struct ib_wc *wc = priv->ibwc + i; 399 400 if ((wc->wr_id & IPOIB_OP_RECV) == 0) 401 panic("ipoib_poll: Bad wr_id 0x%jX\n", 402 (intmax_t)wc->wr_id); 403 if (wc->wr_id & IPOIB_OP_CM) 404 ipoib_cm_handle_rx_wc(priv, wc); 405 else 406 ipoib_ib_handle_rx_wc(priv, wc); 407 } 408 409 if (n != IPOIB_NUM_WC) 410 break; 411 } 412 spin_unlock(&priv->drain_lock); 413 414 if (ib_req_notify_cq(priv->recv_cq, 415 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS) > 0) 416 goto poll_more; 417 } 418 419 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr) 420 { 421 struct ipoib_dev_priv *priv = dev_ptr; 422 423 ipoib_poll(priv); 424 } 425 426 static void drain_tx_cq(struct ipoib_dev_priv *priv) 427 { 428 struct ifnet *dev = priv->dev; 429 430 spin_lock(&priv->lock); 431 while (ipoib_poll_tx(priv, true)) 432 ; /* nothing */ 433 434 if (dev->if_drv_flags & IFF_DRV_OACTIVE) 435 mod_timer(&priv->poll_timer, jiffies + 1); 436 437 spin_unlock(&priv->lock); 438 } 439 440 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr) 441 { 442 struct ipoib_dev_priv *priv = dev_ptr; 443 444 mod_timer(&priv->poll_timer, jiffies); 445 } 446 447 static inline int 448 post_send(struct ipoib_dev_priv *priv, unsigned int wr_id, 449 struct ib_ah *address, u32 qpn, struct ipoib_tx_buf *tx_req, void *head, 450 int hlen) 451 { 452 struct ib_send_wr *bad_wr; 453 struct mbuf *mb = tx_req->mb; 454 u64 *mapping = tx_req->mapping; 455 struct mbuf *m; 456 int i; 457 458 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) { 459 priv->tx_sge[i].addr = mapping[i]; 460 priv->tx_sge[i].length = m->m_len; 461 } 462 priv->tx_wr.wr.num_sge = i; 463 priv->tx_wr.wr.wr_id = wr_id; 464 priv->tx_wr.remote_qpn = qpn; 465 priv->tx_wr.ah = address; 466 467 if (head) { 468 priv->tx_wr.mss = 0; /* XXX mb_shinfo(mb)->gso_size; */ 469 priv->tx_wr.header = head; 470 priv->tx_wr.hlen = hlen; 471 priv->tx_wr.wr.opcode = IB_WR_LSO; 472 } else 473 priv->tx_wr.wr.opcode = IB_WR_SEND; 474 475 return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr); 476 } 477 478 void 479 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb, 480 struct ipoib_ah *address, u32 qpn) 481 { 482 struct ifnet *dev = priv->dev; 483 struct ipoib_tx_buf *tx_req; 484 int hlen; 485 void *phead; 486 487 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE)) 488 while (ipoib_poll_tx(priv, false)) 489 ; /* nothing */ 490 491 m_adj(mb, sizeof (struct ipoib_pseudoheader)); 492 if (0 /* XXX segment offload mb_is_gso(mb) */) { 493 /* XXX hlen = mb_transport_offset(mb) + tcp_hdrlen(mb); */ 494 phead = mtod(mb, void *); 495 if (mb->m_len < hlen) { 496 ipoib_warn(priv, "linear data too small\n"); 497 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 498 m_freem(mb); 499 return; 500 } 501 m_adj(mb, hlen); 502 } else { 503 if (unlikely(mb->m_pkthdr.len - IPOIB_ENCAP_LEN > priv->mcast_mtu)) { 504 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n", 505 mb->m_pkthdr.len, priv->mcast_mtu); 506 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 507 ipoib_cm_mb_too_long(priv, mb, priv->mcast_mtu); 508 return; 509 } 510 phead = NULL; 511 hlen = 0; 512 } 513 514 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n", 515 mb->m_pkthdr.len, address, qpn); 516 517 /* 518 * We put the mb into the tx_ring _before_ we call post_send() 519 * because it's entirely possible that the completion handler will 520 * run before we execute anything after the post_send(). That 521 * means we have to make sure everything is properly recorded and 522 * our state is consistent before we call post_send(). 523 */ 524 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)]; 525 tx_req->mb = mb; 526 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) { 527 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 528 if (tx_req->mb) 529 m_freem(tx_req->mb); 530 return; 531 } 532 533 if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP)) 534 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM; 535 else 536 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM; 537 538 if (++priv->tx_outstanding == ipoib_sendq_size) { 539 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n"); 540 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP)) 541 ipoib_warn(priv, "request notify on send CQ failed\n"); 542 dev->if_drv_flags |= IFF_DRV_OACTIVE; 543 } 544 545 if (unlikely(post_send(priv, 546 priv->tx_head & (ipoib_sendq_size - 1), address->ah, qpn, 547 tx_req, phead, hlen))) { 548 ipoib_warn(priv, "post_send failed\n"); 549 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 550 --priv->tx_outstanding; 551 ipoib_dma_unmap_tx(priv->ca, tx_req); 552 m_freem(mb); 553 if (dev->if_drv_flags & IFF_DRV_OACTIVE) 554 dev->if_drv_flags &= ~IFF_DRV_OACTIVE; 555 } else { 556 address->last_send = priv->tx_head; 557 ++priv->tx_head; 558 } 559 } 560 561 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv) 562 { 563 struct ipoib_ah *ah, *tah; 564 LIST_HEAD(remove_list); 565 unsigned long flags; 566 567 spin_lock_irqsave(&priv->lock, flags); 568 569 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list) 570 if ((int) priv->tx_tail - (int) ah->last_send >= 0) { 571 list_del(&ah->list); 572 ib_destroy_ah(ah->ah); 573 kfree(ah); 574 } 575 576 spin_unlock_irqrestore(&priv->lock, flags); 577 } 578 579 void ipoib_reap_ah(struct work_struct *work) 580 { 581 struct ipoib_dev_priv *priv = 582 container_of(work, struct ipoib_dev_priv, ah_reap_task.work); 583 584 __ipoib_reap_ah(priv); 585 586 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags)) 587 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, 588 HZ); 589 } 590 591 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv) 592 { 593 unsigned long begin; 594 595 begin = jiffies; 596 597 while (!list_empty(&priv->dead_ahs)) { 598 __ipoib_reap_ah(priv); 599 600 if (time_after(jiffies, begin + HZ)) { 601 ipoib_warn(priv, "timing out; will leak address handles\n"); 602 break; 603 } 604 605 msleep(1); 606 } 607 } 608 609 static void ipoib_ib_tx_timer_func(unsigned long ctx) 610 { 611 drain_tx_cq((struct ipoib_dev_priv *)ctx); 612 } 613 614 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv) 615 { 616 int ret; 617 618 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) { 619 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey); 620 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 621 return -1; 622 } 623 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 624 625 ret = ipoib_init_qp(priv); 626 if (ret) { 627 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret); 628 return -1; 629 } 630 631 ret = ipoib_ib_post_receives(priv); 632 if (ret) { 633 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret); 634 ipoib_ib_dev_stop(priv, 1); 635 return -1; 636 } 637 638 ret = ipoib_cm_dev_open(priv); 639 if (ret) { 640 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret); 641 ipoib_ib_dev_stop(priv, 1); 642 return -1; 643 } 644 645 clear_bit(IPOIB_STOP_REAPER, &priv->flags); 646 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ); 647 648 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); 649 650 return 0; 651 } 652 653 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv) 654 { 655 u16 pkey_index = 0; 656 657 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) 658 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 659 else 660 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 661 } 662 663 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv) 664 { 665 666 ipoib_pkey_dev_check_presence(priv); 667 668 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 669 ipoib_dbg(priv, "PKEY is not assigned.\n"); 670 return 0; 671 } 672 673 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags); 674 675 return ipoib_mcast_start_thread(priv); 676 } 677 678 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush) 679 { 680 681 ipoib_dbg(priv, "downing ib_dev\n"); 682 683 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags); 684 if_link_state_change(priv->dev, LINK_STATE_DOWN); 685 686 /* Shutdown the P_Key thread if still active */ 687 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 688 mutex_lock(&pkey_mutex); 689 set_bit(IPOIB_PKEY_STOP, &priv->flags); 690 cancel_delayed_work(&priv->pkey_poll_task); 691 mutex_unlock(&pkey_mutex); 692 if (flush) 693 flush_workqueue(ipoib_workqueue); 694 } 695 696 ipoib_mcast_stop_thread(priv, flush); 697 ipoib_mcast_dev_flush(priv); 698 699 ipoib_flush_paths(priv); 700 701 return 0; 702 } 703 704 static int recvs_pending(struct ipoib_dev_priv *priv) 705 { 706 int pending = 0; 707 int i; 708 709 for (i = 0; i < ipoib_recvq_size; ++i) 710 if (priv->rx_ring[i].mb) 711 ++pending; 712 713 return pending; 714 } 715 716 static void check_qp_movement_and_print(struct ipoib_dev_priv *priv, 717 struct ib_qp *qp, 718 enum ib_qp_state new_state) 719 { 720 struct ib_qp_attr qp_attr; 721 struct ib_qp_init_attr query_init_attr; 722 int ret; 723 724 ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr); 725 if (ret) { 726 ipoib_warn(priv, "%s: Failed to query QP (%d)\n", __func__, ret); 727 return; 728 } 729 730 /* print according to the new-state and the previous state */ 731 if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET) { 732 ipoib_dbg(priv, "Failed to modify QP %d->%d, acceptable\n", 733 qp_attr.qp_state, new_state); 734 } else { 735 ipoib_warn(priv, "Failed to modify QP %d->%d\n", 736 qp_attr.qp_state, new_state); 737 } 738 } 739 740 void ipoib_drain_cq(struct ipoib_dev_priv *priv) 741 { 742 int i, n; 743 744 spin_lock(&priv->drain_lock); 745 do { 746 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc); 747 for (i = 0; i < n; ++i) { 748 /* 749 * Convert any successful completions to flush 750 * errors to avoid passing packets up the 751 * stack after bringing the device down. 752 */ 753 if (priv->ibwc[i].status == IB_WC_SUCCESS) 754 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR; 755 756 if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0) 757 panic("ipoib_drain_cq: Bad wrid 0x%jX\n", 758 (intmax_t)priv->ibwc[i].wr_id); 759 if (priv->ibwc[i].wr_id & IPOIB_OP_CM) 760 ipoib_cm_handle_rx_wc(priv, priv->ibwc + i); 761 else 762 ipoib_ib_handle_rx_wc(priv, priv->ibwc + i); 763 } 764 } while (n == IPOIB_NUM_WC); 765 spin_unlock(&priv->drain_lock); 766 767 spin_lock(&priv->lock); 768 while (ipoib_poll_tx(priv, true)) 769 ; /* nothing */ 770 771 spin_unlock(&priv->lock); 772 } 773 774 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush) 775 { 776 struct ib_qp_attr qp_attr; 777 unsigned long begin; 778 struct ipoib_tx_buf *tx_req; 779 int i; 780 781 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); 782 783 ipoib_cm_dev_stop(priv); 784 785 /* 786 * Move our QP to the error state and then reinitialize in 787 * when all work requests have completed or have been flushed. 788 */ 789 qp_attr.qp_state = IB_QPS_ERR; 790 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) 791 check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR); 792 793 /* Wait for all sends and receives to complete */ 794 begin = jiffies; 795 796 while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) { 797 if (time_after(jiffies, begin + 5 * HZ)) { 798 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n", 799 priv->tx_head - priv->tx_tail, recvs_pending(priv)); 800 801 /* 802 * assume the HW is wedged and just free up 803 * all our pending work requests. 804 */ 805 while ((int) priv->tx_tail - (int) priv->tx_head < 0) { 806 tx_req = &priv->tx_ring[priv->tx_tail & 807 (ipoib_sendq_size - 1)]; 808 ipoib_dma_unmap_tx(priv->ca, tx_req); 809 m_freem(tx_req->mb); 810 ++priv->tx_tail; 811 --priv->tx_outstanding; 812 } 813 814 for (i = 0; i < ipoib_recvq_size; ++i) { 815 struct ipoib_rx_buf *rx_req; 816 817 rx_req = &priv->rx_ring[i]; 818 if (!rx_req->mb) 819 continue; 820 ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]); 821 m_freem(rx_req->mb); 822 rx_req->mb = NULL; 823 } 824 825 goto timeout; 826 } 827 828 ipoib_drain_cq(priv); 829 830 msleep(1); 831 } 832 833 ipoib_dbg(priv, "All sends and receives done.\n"); 834 835 timeout: 836 del_timer_sync(&priv->poll_timer); 837 qp_attr.qp_state = IB_QPS_RESET; 838 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) 839 ipoib_warn(priv, "Failed to modify QP to RESET state\n"); 840 841 /* Wait for all AHs to be reaped */ 842 set_bit(IPOIB_STOP_REAPER, &priv->flags); 843 cancel_delayed_work(&priv->ah_reap_task); 844 if (flush) 845 flush_workqueue(ipoib_workqueue); 846 847 ipoib_ah_dev_cleanup(priv); 848 849 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP); 850 851 return 0; 852 } 853 854 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port) 855 { 856 struct ifnet *dev = priv->dev; 857 858 priv->ca = ca; 859 priv->port = port; 860 priv->qp = NULL; 861 862 if (ipoib_transport_dev_init(priv, ca)) { 863 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name); 864 return -ENODEV; 865 } 866 867 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func, 868 (unsigned long) priv); 869 870 if (dev->if_flags & IFF_UP) { 871 if (ipoib_ib_dev_open(priv)) { 872 ipoib_transport_dev_cleanup(priv); 873 return -ENODEV; 874 } 875 } 876 877 return 0; 878 } 879 880 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, 881 enum ipoib_flush_level level) 882 { 883 struct ipoib_dev_priv *cpriv; 884 u16 new_index; 885 886 mutex_lock(&priv->vlan_mutex); 887 888 /* 889 * Flush any child interfaces too -- they might be up even if 890 * the parent is down. 891 */ 892 list_for_each_entry(cpriv, &priv->child_intfs, list) 893 __ipoib_ib_dev_flush(cpriv, level); 894 895 mutex_unlock(&priv->vlan_mutex); 896 897 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) { 898 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n"); 899 return; 900 } 901 902 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { 903 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n"); 904 return; 905 } 906 907 if (level == IPOIB_FLUSH_HEAVY) { 908 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) { 909 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 910 ipoib_ib_dev_down(priv, 0); 911 ipoib_ib_dev_stop(priv, 0); 912 if (ipoib_pkey_dev_delay_open(priv)) 913 return; 914 } 915 916 /* restart QP only if P_Key index is changed */ 917 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) && 918 new_index == priv->pkey_index) { 919 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n"); 920 return; 921 } 922 priv->pkey_index = new_index; 923 } 924 925 if (level == IPOIB_FLUSH_LIGHT) { 926 ipoib_mark_paths_invalid(priv); 927 ipoib_mcast_dev_flush(priv); 928 } 929 930 if (level >= IPOIB_FLUSH_NORMAL) 931 ipoib_ib_dev_down(priv, 0); 932 933 if (level == IPOIB_FLUSH_HEAVY) { 934 ipoib_ib_dev_stop(priv, 0); 935 ipoib_ib_dev_open(priv); 936 } 937 938 /* 939 * The device could have been brought down between the start and when 940 * we get here, don't bring it back up if it's not configured up 941 */ 942 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { 943 if (level >= IPOIB_FLUSH_NORMAL) 944 ipoib_ib_dev_up(priv); 945 ipoib_mcast_restart_task(&priv->restart_task); 946 } 947 } 948 949 void ipoib_ib_dev_flush_light(struct work_struct *work) 950 { 951 struct ipoib_dev_priv *priv = 952 container_of(work, struct ipoib_dev_priv, flush_light); 953 954 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT); 955 } 956 957 void ipoib_ib_dev_flush_normal(struct work_struct *work) 958 { 959 struct ipoib_dev_priv *priv = 960 container_of(work, struct ipoib_dev_priv, flush_normal); 961 962 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL); 963 } 964 965 void ipoib_ib_dev_flush_heavy(struct work_struct *work) 966 { 967 struct ipoib_dev_priv *priv = 968 container_of(work, struct ipoib_dev_priv, flush_heavy); 969 970 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY); 971 } 972 973 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv) 974 { 975 976 ipoib_dbg(priv, "cleaning up ib_dev\n"); 977 978 ipoib_mcast_stop_thread(priv, 1); 979 ipoib_mcast_dev_flush(priv); 980 981 ipoib_ah_dev_cleanup(priv); 982 ipoib_transport_dev_cleanup(priv); 983 } 984 985 /* 986 * Delayed P_Key Assigment Interim Support 987 * 988 * The following is initial implementation of delayed P_Key assigment 989 * mechanism. It is using the same approach implemented for the multicast 990 * group join. The single goal of this implementation is to quickly address 991 * Bug #2507. This implementation will probably be removed when the P_Key 992 * change async notification is available. 993 */ 994 995 void ipoib_pkey_poll(struct work_struct *work) 996 { 997 struct ipoib_dev_priv *priv = 998 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work); 999 1000 ipoib_pkey_dev_check_presence(priv); 1001 1002 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) 1003 ipoib_open(priv); 1004 else { 1005 mutex_lock(&pkey_mutex); 1006 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags)) 1007 queue_delayed_work(ipoib_workqueue, 1008 &priv->pkey_poll_task, 1009 HZ); 1010 mutex_unlock(&pkey_mutex); 1011 } 1012 } 1013 1014 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv) 1015 { 1016 1017 /* Look for the interface pkey value in the IB Port P_Key table and */ 1018 /* set the interface pkey assigment flag */ 1019 ipoib_pkey_dev_check_presence(priv); 1020 1021 /* P_Key value not assigned yet - start polling */ 1022 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 1023 mutex_lock(&pkey_mutex); 1024 clear_bit(IPOIB_PKEY_STOP, &priv->flags); 1025 queue_delayed_work(ipoib_workqueue, 1026 &priv->pkey_poll_task, 1027 HZ); 1028 mutex_unlock(&pkey_mutex); 1029 return 1; 1030 } 1031 1032 return 0; 1033 } 1034