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