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) 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 return n == MAX_SEND_CQE; 383 } 384 385 static void 386 ipoib_poll(struct ipoib_dev_priv *priv) 387 { 388 int n, i; 389 390 poll_more: 391 spin_lock(&priv->drain_lock); 392 for (;;) { 393 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc); 394 for (i = 0; i < n; i++) { 395 struct ib_wc *wc = priv->ibwc + i; 396 397 if ((wc->wr_id & IPOIB_OP_RECV) == 0) 398 panic("ipoib_poll: Bad wr_id 0x%jX\n", 399 (intmax_t)wc->wr_id); 400 if (wc->wr_id & IPOIB_OP_CM) 401 ipoib_cm_handle_rx_wc(priv, wc); 402 else 403 ipoib_ib_handle_rx_wc(priv, wc); 404 } 405 406 if (n != IPOIB_NUM_WC) 407 break; 408 } 409 spin_unlock(&priv->drain_lock); 410 411 if (ib_req_notify_cq(priv->recv_cq, 412 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS) > 0) 413 goto poll_more; 414 } 415 416 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr) 417 { 418 struct ipoib_dev_priv *priv = dev_ptr; 419 420 ipoib_poll(priv); 421 } 422 423 static void drain_tx_cq(struct ipoib_dev_priv *priv) 424 { 425 struct ifnet *dev = priv->dev; 426 427 spin_lock(&priv->lock); 428 while (ipoib_poll_tx(priv)) 429 ; /* nothing */ 430 431 if (dev->if_drv_flags & IFF_DRV_OACTIVE) 432 mod_timer(&priv->poll_timer, jiffies + 1); 433 434 spin_unlock(&priv->lock); 435 } 436 437 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr) 438 { 439 struct ipoib_dev_priv *priv = dev_ptr; 440 441 mod_timer(&priv->poll_timer, jiffies); 442 } 443 444 static inline int 445 post_send(struct ipoib_dev_priv *priv, unsigned int wr_id, 446 struct ib_ah *address, u32 qpn, struct ipoib_tx_buf *tx_req, void *head, 447 int hlen) 448 { 449 struct ib_send_wr *bad_wr; 450 struct mbuf *mb = tx_req->mb; 451 u64 *mapping = tx_req->mapping; 452 struct mbuf *m; 453 int i; 454 455 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) { 456 priv->tx_sge[i].addr = mapping[i]; 457 priv->tx_sge[i].length = m->m_len; 458 } 459 priv->tx_wr.wr.num_sge = i; 460 priv->tx_wr.wr.wr_id = wr_id; 461 priv->tx_wr.remote_qpn = qpn; 462 priv->tx_wr.ah = address; 463 464 if (head) { 465 priv->tx_wr.mss = 0; /* XXX mb_shinfo(mb)->gso_size; */ 466 priv->tx_wr.header = head; 467 priv->tx_wr.hlen = hlen; 468 priv->tx_wr.wr.opcode = IB_WR_LSO; 469 } else 470 priv->tx_wr.wr.opcode = IB_WR_SEND; 471 472 return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr); 473 } 474 475 void 476 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb, 477 struct ipoib_ah *address, u32 qpn) 478 { 479 struct ifnet *dev = priv->dev; 480 struct ipoib_tx_buf *tx_req; 481 int hlen; 482 void *phead; 483 484 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE)) 485 while (ipoib_poll_tx(priv)) 486 ; /* nothing */ 487 488 m_adj(mb, sizeof (struct ipoib_pseudoheader)); 489 if (0 /* XXX segment offload mb_is_gso(mb) */) { 490 /* XXX hlen = mb_transport_offset(mb) + tcp_hdrlen(mb); */ 491 phead = mtod(mb, void *); 492 if (mb->m_len < hlen) { 493 ipoib_warn(priv, "linear data too small\n"); 494 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 495 m_freem(mb); 496 return; 497 } 498 m_adj(mb, hlen); 499 } else { 500 if (unlikely(mb->m_pkthdr.len - IPOIB_ENCAP_LEN > priv->mcast_mtu)) { 501 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n", 502 mb->m_pkthdr.len, priv->mcast_mtu); 503 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 504 ipoib_cm_mb_too_long(priv, mb, priv->mcast_mtu); 505 return; 506 } 507 phead = NULL; 508 hlen = 0; 509 } 510 511 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n", 512 mb->m_pkthdr.len, address, qpn); 513 514 /* 515 * We put the mb into the tx_ring _before_ we call post_send() 516 * because it's entirely possible that the completion handler will 517 * run before we execute anything after the post_send(). That 518 * means we have to make sure everything is properly recorded and 519 * our state is consistent before we call post_send(). 520 */ 521 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)]; 522 tx_req->mb = mb; 523 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) { 524 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 525 if (tx_req->mb) 526 m_freem(tx_req->mb); 527 return; 528 } 529 530 if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP)) 531 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM; 532 else 533 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM; 534 535 if (++priv->tx_outstanding == ipoib_sendq_size) { 536 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n"); 537 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP)) 538 ipoib_warn(priv, "request notify on send CQ failed\n"); 539 dev->if_drv_flags |= IFF_DRV_OACTIVE; 540 } 541 542 if (unlikely(post_send(priv, 543 priv->tx_head & (ipoib_sendq_size - 1), address->ah, qpn, 544 tx_req, phead, hlen))) { 545 ipoib_warn(priv, "post_send failed\n"); 546 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 547 --priv->tx_outstanding; 548 ipoib_dma_unmap_tx(priv->ca, tx_req); 549 m_freem(mb); 550 if (dev->if_drv_flags & IFF_DRV_OACTIVE) 551 dev->if_drv_flags &= ~IFF_DRV_OACTIVE; 552 } else { 553 address->last_send = priv->tx_head; 554 ++priv->tx_head; 555 } 556 } 557 558 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv) 559 { 560 struct ipoib_ah *ah, *tah; 561 LIST_HEAD(remove_list); 562 unsigned long flags; 563 564 spin_lock_irqsave(&priv->lock, flags); 565 566 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list) 567 if ((int) priv->tx_tail - (int) ah->last_send >= 0) { 568 list_del(&ah->list); 569 ib_destroy_ah(ah->ah); 570 kfree(ah); 571 } 572 573 spin_unlock_irqrestore(&priv->lock, flags); 574 } 575 576 void ipoib_reap_ah(struct work_struct *work) 577 { 578 struct ipoib_dev_priv *priv = 579 container_of(work, struct ipoib_dev_priv, ah_reap_task.work); 580 581 __ipoib_reap_ah(priv); 582 583 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags)) 584 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, 585 HZ); 586 } 587 588 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv) 589 { 590 unsigned long begin; 591 592 begin = jiffies; 593 594 while (!list_empty(&priv->dead_ahs)) { 595 __ipoib_reap_ah(priv); 596 597 if (time_after(jiffies, begin + HZ)) { 598 ipoib_warn(priv, "timing out; will leak address handles\n"); 599 break; 600 } 601 602 msleep(1); 603 } 604 } 605 606 static void ipoib_ib_tx_timer_func(unsigned long ctx) 607 { 608 drain_tx_cq((struct ipoib_dev_priv *)ctx); 609 } 610 611 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv) 612 { 613 int ret; 614 615 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) { 616 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey); 617 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 618 return -1; 619 } 620 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 621 622 ret = ipoib_init_qp(priv); 623 if (ret) { 624 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret); 625 return -1; 626 } 627 628 ret = ipoib_ib_post_receives(priv); 629 if (ret) { 630 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret); 631 ipoib_ib_dev_stop(priv, 1); 632 return -1; 633 } 634 635 ret = ipoib_cm_dev_open(priv); 636 if (ret) { 637 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret); 638 ipoib_ib_dev_stop(priv, 1); 639 return -1; 640 } 641 642 clear_bit(IPOIB_STOP_REAPER, &priv->flags); 643 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ); 644 645 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); 646 647 return 0; 648 } 649 650 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv) 651 { 652 u16 pkey_index = 0; 653 654 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) 655 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 656 else 657 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 658 } 659 660 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv) 661 { 662 663 ipoib_pkey_dev_check_presence(priv); 664 665 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 666 ipoib_dbg(priv, "PKEY is not assigned.\n"); 667 return 0; 668 } 669 670 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags); 671 672 return ipoib_mcast_start_thread(priv); 673 } 674 675 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush) 676 { 677 678 ipoib_dbg(priv, "downing ib_dev\n"); 679 680 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags); 681 if_link_state_change(priv->dev, LINK_STATE_DOWN); 682 683 /* Shutdown the P_Key thread if still active */ 684 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 685 mutex_lock(&pkey_mutex); 686 set_bit(IPOIB_PKEY_STOP, &priv->flags); 687 cancel_delayed_work(&priv->pkey_poll_task); 688 mutex_unlock(&pkey_mutex); 689 if (flush) 690 flush_workqueue(ipoib_workqueue); 691 } 692 693 ipoib_mcast_stop_thread(priv, flush); 694 ipoib_mcast_dev_flush(priv); 695 696 ipoib_flush_paths(priv); 697 698 return 0; 699 } 700 701 static int recvs_pending(struct ipoib_dev_priv *priv) 702 { 703 int pending = 0; 704 int i; 705 706 for (i = 0; i < ipoib_recvq_size; ++i) 707 if (priv->rx_ring[i].mb) 708 ++pending; 709 710 return pending; 711 } 712 713 static void check_qp_movement_and_print(struct ipoib_dev_priv *priv, 714 struct ib_qp *qp, 715 enum ib_qp_state new_state) 716 { 717 struct ib_qp_attr qp_attr; 718 struct ib_qp_init_attr query_init_attr; 719 int ret; 720 721 ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr); 722 if (ret) { 723 ipoib_warn(priv, "%s: Failed to query QP (%d)\n", __func__, ret); 724 return; 725 } 726 727 /* print according to the new-state and the previous state */ 728 if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET) { 729 ipoib_dbg(priv, "Failed to modify QP %d->%d, acceptable\n", 730 qp_attr.qp_state, new_state); 731 } else { 732 ipoib_warn(priv, "Failed to modify QP %d->%d\n", 733 qp_attr.qp_state, new_state); 734 } 735 } 736 737 void ipoib_drain_cq(struct ipoib_dev_priv *priv) 738 { 739 int i, n; 740 741 spin_lock(&priv->drain_lock); 742 do { 743 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc); 744 for (i = 0; i < n; ++i) { 745 /* 746 * Convert any successful completions to flush 747 * errors to avoid passing packets up the 748 * stack after bringing the device down. 749 */ 750 if (priv->ibwc[i].status == IB_WC_SUCCESS) 751 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR; 752 753 if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0) 754 panic("ipoib_drain_cq: Bad wrid 0x%jX\n", 755 (intmax_t)priv->ibwc[i].wr_id); 756 if (priv->ibwc[i].wr_id & IPOIB_OP_CM) 757 ipoib_cm_handle_rx_wc(priv, priv->ibwc + i); 758 else 759 ipoib_ib_handle_rx_wc(priv, priv->ibwc + i); 760 } 761 } while (n == IPOIB_NUM_WC); 762 spin_unlock(&priv->drain_lock); 763 764 spin_lock(&priv->lock); 765 while (ipoib_poll_tx(priv)) 766 ; /* nothing */ 767 768 spin_unlock(&priv->lock); 769 } 770 771 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush) 772 { 773 struct ib_qp_attr qp_attr; 774 unsigned long begin; 775 struct ipoib_tx_buf *tx_req; 776 int i; 777 778 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags); 779 780 ipoib_cm_dev_stop(priv); 781 782 /* 783 * Move our QP to the error state and then reinitialize in 784 * when all work requests have completed or have been flushed. 785 */ 786 qp_attr.qp_state = IB_QPS_ERR; 787 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) 788 check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR); 789 790 /* Wait for all sends and receives to complete */ 791 begin = jiffies; 792 793 while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) { 794 if (time_after(jiffies, begin + 5 * HZ)) { 795 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n", 796 priv->tx_head - priv->tx_tail, recvs_pending(priv)); 797 798 /* 799 * assume the HW is wedged and just free up 800 * all our pending work requests. 801 */ 802 while ((int) priv->tx_tail - (int) priv->tx_head < 0) { 803 tx_req = &priv->tx_ring[priv->tx_tail & 804 (ipoib_sendq_size - 1)]; 805 ipoib_dma_unmap_tx(priv->ca, tx_req); 806 m_freem(tx_req->mb); 807 ++priv->tx_tail; 808 --priv->tx_outstanding; 809 } 810 811 for (i = 0; i < ipoib_recvq_size; ++i) { 812 struct ipoib_rx_buf *rx_req; 813 814 rx_req = &priv->rx_ring[i]; 815 if (!rx_req->mb) 816 continue; 817 ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]); 818 m_freem(rx_req->mb); 819 rx_req->mb = NULL; 820 } 821 822 goto timeout; 823 } 824 825 ipoib_drain_cq(priv); 826 827 msleep(1); 828 } 829 830 ipoib_dbg(priv, "All sends and receives done.\n"); 831 832 timeout: 833 del_timer_sync(&priv->poll_timer); 834 qp_attr.qp_state = IB_QPS_RESET; 835 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE)) 836 ipoib_warn(priv, "Failed to modify QP to RESET state\n"); 837 838 /* Wait for all AHs to be reaped */ 839 set_bit(IPOIB_STOP_REAPER, &priv->flags); 840 cancel_delayed_work(&priv->ah_reap_task); 841 if (flush) 842 flush_workqueue(ipoib_workqueue); 843 844 ipoib_ah_dev_cleanup(priv); 845 846 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP); 847 848 return 0; 849 } 850 851 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port) 852 { 853 struct ifnet *dev = priv->dev; 854 855 priv->ca = ca; 856 priv->port = port; 857 priv->qp = NULL; 858 859 if (ipoib_transport_dev_init(priv, ca)) { 860 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name); 861 return -ENODEV; 862 } 863 864 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func, 865 (unsigned long) priv); 866 867 if (dev->if_flags & IFF_UP) { 868 if (ipoib_ib_dev_open(priv)) { 869 ipoib_transport_dev_cleanup(priv); 870 return -ENODEV; 871 } 872 } 873 874 return 0; 875 } 876 877 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, 878 enum ipoib_flush_level level) 879 { 880 struct ipoib_dev_priv *cpriv; 881 u16 new_index; 882 883 mutex_lock(&priv->vlan_mutex); 884 885 /* 886 * Flush any child interfaces too -- they might be up even if 887 * the parent is down. 888 */ 889 list_for_each_entry(cpriv, &priv->child_intfs, list) 890 __ipoib_ib_dev_flush(cpriv, level); 891 892 mutex_unlock(&priv->vlan_mutex); 893 894 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) { 895 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n"); 896 return; 897 } 898 899 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { 900 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n"); 901 return; 902 } 903 904 if (level == IPOIB_FLUSH_HEAVY) { 905 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) { 906 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags); 907 ipoib_ib_dev_down(priv, 0); 908 ipoib_ib_dev_stop(priv, 0); 909 if (ipoib_pkey_dev_delay_open(priv)) 910 return; 911 } 912 913 /* restart QP only if P_Key index is changed */ 914 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) && 915 new_index == priv->pkey_index) { 916 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n"); 917 return; 918 } 919 priv->pkey_index = new_index; 920 } 921 922 if (level == IPOIB_FLUSH_LIGHT) { 923 ipoib_mark_paths_invalid(priv); 924 ipoib_mcast_dev_flush(priv); 925 } 926 927 if (level >= IPOIB_FLUSH_NORMAL) 928 ipoib_ib_dev_down(priv, 0); 929 930 if (level == IPOIB_FLUSH_HEAVY) { 931 ipoib_ib_dev_stop(priv, 0); 932 ipoib_ib_dev_open(priv); 933 } 934 935 /* 936 * The device could have been brought down between the start and when 937 * we get here, don't bring it back up if it's not configured up 938 */ 939 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) { 940 if (level >= IPOIB_FLUSH_NORMAL) 941 ipoib_ib_dev_up(priv); 942 ipoib_mcast_restart_task(&priv->restart_task); 943 } 944 } 945 946 void ipoib_ib_dev_flush_light(struct work_struct *work) 947 { 948 struct ipoib_dev_priv *priv = 949 container_of(work, struct ipoib_dev_priv, flush_light); 950 951 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT); 952 } 953 954 void ipoib_ib_dev_flush_normal(struct work_struct *work) 955 { 956 struct ipoib_dev_priv *priv = 957 container_of(work, struct ipoib_dev_priv, flush_normal); 958 959 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL); 960 } 961 962 void ipoib_ib_dev_flush_heavy(struct work_struct *work) 963 { 964 struct ipoib_dev_priv *priv = 965 container_of(work, struct ipoib_dev_priv, flush_heavy); 966 967 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY); 968 } 969 970 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv) 971 { 972 973 ipoib_dbg(priv, "cleaning up ib_dev\n"); 974 975 ipoib_mcast_stop_thread(priv, 1); 976 ipoib_mcast_dev_flush(priv); 977 978 ipoib_ah_dev_cleanup(priv); 979 ipoib_transport_dev_cleanup(priv); 980 } 981 982 /* 983 * Delayed P_Key Assigment Interim Support 984 * 985 * The following is initial implementation of delayed P_Key assigment 986 * mechanism. It is using the same approach implemented for the multicast 987 * group join. The single goal of this implementation is to quickly address 988 * Bug #2507. This implementation will probably be removed when the P_Key 989 * change async notification is available. 990 */ 991 992 void ipoib_pkey_poll(struct work_struct *work) 993 { 994 struct ipoib_dev_priv *priv = 995 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work); 996 997 ipoib_pkey_dev_check_presence(priv); 998 999 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) 1000 ipoib_open(priv); 1001 else { 1002 mutex_lock(&pkey_mutex); 1003 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags)) 1004 queue_delayed_work(ipoib_workqueue, 1005 &priv->pkey_poll_task, 1006 HZ); 1007 mutex_unlock(&pkey_mutex); 1008 } 1009 } 1010 1011 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv) 1012 { 1013 1014 /* Look for the interface pkey value in the IB Port P_Key table and */ 1015 /* set the interface pkey assigment flag */ 1016 ipoib_pkey_dev_check_presence(priv); 1017 1018 /* P_Key value not assigned yet - start polling */ 1019 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) { 1020 mutex_lock(&pkey_mutex); 1021 clear_bit(IPOIB_PKEY_STOP, &priv->flags); 1022 queue_delayed_work(ipoib_workqueue, 1023 &priv->pkey_poll_task, 1024 HZ); 1025 mutex_unlock(&pkey_mutex); 1026 return 1; 1027 } 1028 1029 return 0; 1030 } 1031