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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
38 #include <rdma/ib_cache.h>
40 #include <security/mac/mac_framework.h>
42 #include <linux/delay.h>
43 #include <linux/dma-mapping.h>
45 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
46 static int data_debug_level;
48 module_param(data_debug_level, int, 0644);
49 MODULE_PARM_DESC(data_debug_level,
50 "Enable data path debug tracing if > 0");
53 static DEFINE_MUTEX(pkey_mutex);
55 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *priv,
56 struct ib_pd *pd, struct ib_ah_attr *attr)
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
68 ah->ah = ib_create_ah(pd, attr);
73 ipoib_dbg(priv, "Created ah %p\n", ah->ah);
78 void ipoib_free_ah(struct kref *kref)
80 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
81 struct ipoib_dev_priv *priv = ah->priv;
85 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
91 ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req)
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,
102 ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length)
105 m_adj(mb, -(mb->m_pkthdr.len - length));
109 ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req,
116 mb = m_getm2(NULL, size, M_NOWAIT, MT_DATA, M_PKTHDR);
119 for (i = 0, m = mb; m != NULL; m = m->m_next, i++) {
120 m->m_len = M_SIZE(m);
121 mb->m_pkthdr.len += m->m_len;
122 rx_req->mapping[i] = ib_dma_map_single(priv->ca,
123 mtod(m, void *), m->m_len, DMA_FROM_DEVICE);
124 if (unlikely(ib_dma_mapping_error(priv->ca,
125 rx_req->mapping[i])))
132 for (j = 0, m = mb; j < i; m = m->m_next, j++)
133 ib_dma_unmap_single(priv->ca, rx_req->mapping[j], m->m_len,
140 static int ipoib_ib_post_receive(struct ipoib_dev_priv *priv, int id)
142 struct ipoib_rx_buf *rx_req;
143 struct ib_recv_wr *bad_wr;
148 rx_req = &priv->rx_ring[id];
149 for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) {
150 priv->rx_sge[i].addr = rx_req->mapping[i];
151 priv->rx_sge[i].length = m->m_len;
153 priv->rx_wr.num_sge = i;
154 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
156 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
158 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
159 ipoib_dma_unmap_rx(priv, &priv->rx_ring[id]);
160 m_freem(priv->rx_ring[id].mb);
161 priv->rx_ring[id].mb = NULL;
168 ipoib_alloc_rx_mb(struct ipoib_dev_priv *priv, int id)
171 return ipoib_alloc_map_mb(priv, &priv->rx_ring[id],
172 priv->max_ib_mtu + IB_GRH_BYTES);
175 static int ipoib_ib_post_receives(struct ipoib_dev_priv *priv)
179 for (i = 0; i < ipoib_recvq_size; ++i) {
180 if (!ipoib_alloc_rx_mb(priv, i)) {
181 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
184 if (ipoib_ib_post_receive(priv, i)) {
185 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
194 ipoib_ib_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
196 struct ipoib_rx_buf saverx;
197 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
198 struct ifnet *dev = priv->dev;
199 struct ipoib_header *eh;
202 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
205 if (unlikely(wr_id >= ipoib_recvq_size)) {
206 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
207 wr_id, ipoib_recvq_size);
211 mb = priv->rx_ring[wr_id].mb;
213 if (unlikely(wc->status != IB_WC_SUCCESS)) {
214 if (wc->status != IB_WC_WR_FLUSH_ERR) {
215 ipoib_warn(priv, "failed recv event "
216 "(status=%d, wrid=%d vend_err %x)\n",
217 wc->status, wr_id, wc->vendor_err);
221 ipoib_dma_unmap_rx(priv, &priv->rx_ring[wr_id]);
223 priv->rx_ring[wr_id].mb = NULL;
229 * Drop packets that this interface sent, ie multicast packets
230 * that the HCA has replicated.
232 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
235 memcpy(&saverx, &priv->rx_ring[wr_id], sizeof(saverx));
237 * If we can't allocate a new RX buffer, dump
238 * this packet and reuse the old buffer.
240 if (unlikely(!ipoib_alloc_rx_mb(priv, wr_id))) {
241 memcpy(&priv->rx_ring[wr_id], &saverx, sizeof(saverx));
242 if_inc_counter(dev, IFCOUNTER_IQDROPS, 1);
246 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
247 wc->byte_len, wc->slid);
249 ipoib_dma_unmap_rx(priv, &saverx);
250 ipoib_dma_mb(priv, mb, wc->byte_len);
252 if_inc_counter(dev, IFCOUNTER_IPACKETS, 1);
253 if_inc_counter(dev, IFCOUNTER_IBYTES, mb->m_pkthdr.len);
254 mb->m_pkthdr.rcvif = dev;
255 m_adj(mb, sizeof(struct ib_grh) - INFINIBAND_ALEN);
256 eh = mtod(mb, struct ipoib_header *);
257 bzero(eh->hwaddr, 4); /* Zero the queue pair, only dgid is in grh */
259 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
260 mb->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
262 dev->if_input(dev, mb);
265 if (unlikely(ipoib_ib_post_receive(priv, wr_id)))
266 ipoib_warn(priv, "ipoib_ib_post_receive failed "
267 "for buf %d\n", wr_id);
270 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max)
272 struct mbuf *mb = tx_req->mb;
273 u64 *mapping = tx_req->mapping;
278 for (m = mb, p = NULL, i = 0; m != NULL; p = m, m = m->m_next, i++) {
282 panic("ipoib_dma_map_tx: First mbuf empty\n");
283 p->m_next = m_free(m);
289 tx_req->mb = mb = m_defrag(mb, M_NOWAIT);
292 for (m = mb, i = 0; m != NULL; m = m->m_next, i++);
297 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
298 mapping[i] = ib_dma_map_single(ca, mtod(m, void *),
299 m->m_len, DMA_TO_DEVICE);
300 if (unlikely(ib_dma_mapping_error(ca, mapping[i]))) {
309 for (m = mb, i = 0; i < end; m = m->m_next, i++)
310 ib_dma_unmap_single(ca, mapping[i], m->m_len,
316 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
318 struct mbuf *mb = tx_req->mb;
319 u64 *mapping = tx_req->mapping;
323 for (m = mb, i = 0; m != NULL; m = m->m_next, i++)
324 ib_dma_unmap_single(ca, mapping[i], m->m_len, DMA_TO_DEVICE);
327 static void ipoib_ib_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
329 struct ifnet *dev = priv->dev;
330 unsigned int wr_id = wc->wr_id;
331 struct ipoib_tx_buf *tx_req;
333 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
336 if (unlikely(wr_id >= ipoib_sendq_size)) {
337 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
338 wr_id, ipoib_sendq_size);
342 tx_req = &priv->tx_ring[wr_id];
344 ipoib_dma_unmap_tx(priv->ca, tx_req);
346 if_inc_counter(dev, IFCOUNTER_OPACKETS, 1);
351 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
352 (dev->if_drv_flags & IFF_DRV_OACTIVE) &&
353 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
354 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
356 if (wc->status != IB_WC_SUCCESS &&
357 wc->status != IB_WC_WR_FLUSH_ERR)
358 ipoib_warn(priv, "failed send event "
359 "(status=%d, wrid=%d vend_err %x)\n",
360 wc->status, wr_id, wc->vendor_err);
364 ipoib_poll_tx(struct ipoib_dev_priv *priv)
368 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
369 for (i = 0; i < n; ++i) {
370 struct ib_wc *wc = priv->send_wc + i;
371 if (wc->wr_id & IPOIB_OP_CM)
372 ipoib_cm_handle_tx_wc(priv, wc);
374 ipoib_ib_handle_tx_wc(priv, wc);
377 return n == MAX_SEND_CQE;
381 ipoib_poll(struct ipoib_dev_priv *priv)
386 spin_lock(&priv->drain_lock);
388 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
389 for (i = 0; i < n; i++) {
390 struct ib_wc *wc = priv->ibwc + i;
392 if ((wc->wr_id & IPOIB_OP_RECV) == 0)
393 panic("ipoib_poll: Bad wr_id 0x%jX\n",
394 (intmax_t)wc->wr_id);
395 if (wc->wr_id & IPOIB_OP_CM)
396 ipoib_cm_handle_rx_wc(priv, wc);
398 ipoib_ib_handle_rx_wc(priv, wc);
401 if (n != IPOIB_NUM_WC)
404 spin_unlock(&priv->drain_lock);
406 if (ib_req_notify_cq(priv->recv_cq,
407 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS))
411 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
413 struct ipoib_dev_priv *priv = dev_ptr;
418 static void drain_tx_cq(struct ipoib_dev_priv *priv)
420 struct ifnet *dev = priv->dev;
422 spin_lock(&priv->lock);
423 while (ipoib_poll_tx(priv))
426 if (dev->if_drv_flags & IFF_DRV_OACTIVE)
427 mod_timer(&priv->poll_timer, jiffies + 1);
429 spin_unlock(&priv->lock);
432 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
434 struct ipoib_dev_priv *priv = dev_ptr;
436 mod_timer(&priv->poll_timer, jiffies);
440 post_send(struct ipoib_dev_priv *priv, unsigned int wr_id,
441 struct ib_ah *address, u32 qpn, struct ipoib_tx_buf *tx_req, void *head,
444 struct ib_send_wr *bad_wr;
445 struct mbuf *mb = tx_req->mb;
446 u64 *mapping = tx_req->mapping;
450 for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
451 priv->tx_sge[i].addr = mapping[i];
452 priv->tx_sge[i].length = m->m_len;
454 priv->tx_wr.wr.num_sge = i;
455 priv->tx_wr.wr.wr_id = wr_id;
456 priv->tx_wr.remote_qpn = qpn;
457 priv->tx_wr.ah = address;
460 priv->tx_wr.mss = 0; /* XXX mb_shinfo(mb)->gso_size; */
461 priv->tx_wr.header = head;
462 priv->tx_wr.hlen = hlen;
463 priv->tx_wr.wr.opcode = IB_WR_LSO;
465 priv->tx_wr.wr.opcode = IB_WR_SEND;
467 return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr);
471 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb,
472 struct ipoib_ah *address, u32 qpn)
474 struct ifnet *dev = priv->dev;
475 struct ipoib_tx_buf *tx_req;
479 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
480 while (ipoib_poll_tx(priv))
483 m_adj(mb, sizeof (struct ipoib_pseudoheader));
484 if (0 /* XXX segment offload mb_is_gso(mb) */) {
485 /* XXX hlen = mb_transport_offset(mb) + tcp_hdrlen(mb); */
486 phead = mtod(mb, void *);
487 if (mb->m_len < hlen) {
488 ipoib_warn(priv, "linear data too small\n");
489 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
495 if (unlikely(mb->m_pkthdr.len - IPOIB_ENCAP_LEN > priv->mcast_mtu)) {
496 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
497 mb->m_pkthdr.len, priv->mcast_mtu);
498 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
499 ipoib_cm_mb_too_long(priv, mb, priv->mcast_mtu);
506 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
507 mb->m_pkthdr.len, address, qpn);
510 * We put the mb into the tx_ring _before_ we call post_send()
511 * because it's entirely possible that the completion handler will
512 * run before we execute anything after the post_send(). That
513 * means we have to make sure everything is properly recorded and
514 * our state is consistent before we call post_send().
516 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
518 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) {
519 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
525 if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP))
526 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
528 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
530 if (++priv->tx_outstanding == ipoib_sendq_size) {
531 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
532 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
533 ipoib_warn(priv, "request notify on send CQ failed\n");
534 dev->if_drv_flags |= IFF_DRV_OACTIVE;
537 if (unlikely(post_send(priv,
538 priv->tx_head & (ipoib_sendq_size - 1), address->ah, qpn,
539 tx_req, phead, hlen))) {
540 ipoib_warn(priv, "post_send failed\n");
541 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
542 --priv->tx_outstanding;
543 ipoib_dma_unmap_tx(priv->ca, tx_req);
545 if (dev->if_drv_flags & IFF_DRV_OACTIVE)
546 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
548 address->last_send = priv->tx_head;
553 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv)
555 struct ipoib_ah *ah, *tah;
556 LIST_HEAD(remove_list);
559 spin_lock_irqsave(&priv->lock, flags);
561 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
562 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
564 ib_destroy_ah(ah->ah);
568 spin_unlock_irqrestore(&priv->lock, flags);
571 void ipoib_reap_ah(struct work_struct *work)
573 struct ipoib_dev_priv *priv =
574 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
576 __ipoib_reap_ah(priv);
578 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
579 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
583 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv)
589 while (!list_empty(&priv->dead_ahs)) {
590 __ipoib_reap_ah(priv);
592 if (time_after(jiffies, begin + HZ)) {
593 ipoib_warn(priv, "timing out; will leak address handles\n");
601 static void ipoib_ib_tx_timer_func(unsigned long ctx)
603 drain_tx_cq((struct ipoib_dev_priv *)ctx);
606 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv)
610 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
611 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
612 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
615 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
617 ret = ipoib_init_qp(priv);
619 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
623 ret = ipoib_ib_post_receives(priv);
625 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
626 ipoib_ib_dev_stop(priv, 1);
630 ret = ipoib_cm_dev_open(priv);
632 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
633 ipoib_ib_dev_stop(priv, 1);
637 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
638 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
640 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
645 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv)
649 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
650 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
652 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
655 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv)
658 ipoib_pkey_dev_check_presence(priv);
660 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
661 ipoib_dbg(priv, "PKEY is not assigned.\n");
665 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
667 return ipoib_mcast_start_thread(priv);
670 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush)
673 ipoib_dbg(priv, "downing ib_dev\n");
675 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
676 if_link_state_change(priv->dev, LINK_STATE_DOWN);
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);
685 flush_workqueue(ipoib_workqueue);
688 ipoib_mcast_stop_thread(priv, flush);
689 ipoib_mcast_dev_flush(priv);
691 ipoib_flush_paths(priv);
696 static int recvs_pending(struct ipoib_dev_priv *priv)
701 for (i = 0; i < ipoib_recvq_size; ++i)
702 if (priv->rx_ring[i].mb)
708 void ipoib_drain_cq(struct ipoib_dev_priv *priv)
712 spin_lock(&priv->drain_lock);
714 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
715 for (i = 0; i < n; ++i) {
717 * Convert any successful completions to flush
718 * errors to avoid passing packets up the
719 * stack after bringing the device down.
721 if (priv->ibwc[i].status == IB_WC_SUCCESS)
722 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
724 if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0)
725 panic("ipoib_drain_cq: Bad wrid 0x%jX\n",
726 (intmax_t)priv->ibwc[i].wr_id);
727 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
728 ipoib_cm_handle_rx_wc(priv, priv->ibwc + i);
730 ipoib_ib_handle_rx_wc(priv, priv->ibwc + i);
732 } while (n == IPOIB_NUM_WC);
733 spin_unlock(&priv->drain_lock);
735 spin_lock(&priv->lock);
736 while (ipoib_poll_tx(priv))
739 spin_unlock(&priv->lock);
742 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush)
744 struct ib_qp_attr qp_attr;
746 struct ipoib_tx_buf *tx_req;
749 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
751 ipoib_cm_dev_stop(priv);
754 * Move our QP to the error state and then reinitialize in
755 * when all work requests have completed or have been flushed.
757 qp_attr.qp_state = IB_QPS_ERR;
758 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
759 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
761 /* Wait for all sends and receives to complete */
764 while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) {
765 if (time_after(jiffies, begin + 5 * HZ)) {
766 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
767 priv->tx_head - priv->tx_tail, recvs_pending(priv));
770 * assume the HW is wedged and just free up
771 * all our pending work requests.
773 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
774 tx_req = &priv->tx_ring[priv->tx_tail &
775 (ipoib_sendq_size - 1)];
776 ipoib_dma_unmap_tx(priv->ca, tx_req);
779 --priv->tx_outstanding;
782 for (i = 0; i < ipoib_recvq_size; ++i) {
783 struct ipoib_rx_buf *rx_req;
785 rx_req = &priv->rx_ring[i];
788 ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]);
796 ipoib_drain_cq(priv);
801 ipoib_dbg(priv, "All sends and receives done.\n");
804 del_timer_sync(&priv->poll_timer);
805 qp_attr.qp_state = IB_QPS_RESET;
806 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
807 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
809 /* Wait for all AHs to be reaped */
810 set_bit(IPOIB_STOP_REAPER, &priv->flags);
811 cancel_delayed_work(&priv->ah_reap_task);
813 flush_workqueue(ipoib_workqueue);
815 ipoib_ah_dev_cleanup(priv);
817 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
822 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
824 struct ifnet *dev = priv->dev;
830 if (ipoib_transport_dev_init(priv, ca)) {
831 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
835 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
836 (unsigned long) priv);
838 if (dev->if_flags & IFF_UP) {
839 if (ipoib_ib_dev_open(priv)) {
840 ipoib_transport_dev_cleanup(priv);
848 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
849 enum ipoib_flush_level level)
851 struct ipoib_dev_priv *cpriv;
854 mutex_lock(&priv->vlan_mutex);
857 * Flush any child interfaces too -- they might be up even if
858 * the parent is down.
860 list_for_each_entry(cpriv, &priv->child_intfs, list)
861 __ipoib_ib_dev_flush(cpriv, level);
863 mutex_unlock(&priv->vlan_mutex);
865 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
866 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
870 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
871 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
875 if (level == IPOIB_FLUSH_HEAVY) {
876 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
877 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
878 ipoib_ib_dev_down(priv, 0);
879 ipoib_ib_dev_stop(priv, 0);
880 if (ipoib_pkey_dev_delay_open(priv))
884 /* restart QP only if P_Key index is changed */
885 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
886 new_index == priv->pkey_index) {
887 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
890 priv->pkey_index = new_index;
893 if (level == IPOIB_FLUSH_LIGHT) {
894 ipoib_mark_paths_invalid(priv);
895 ipoib_mcast_dev_flush(priv);
898 if (level >= IPOIB_FLUSH_NORMAL)
899 ipoib_ib_dev_down(priv, 0);
901 if (level == IPOIB_FLUSH_HEAVY) {
902 ipoib_ib_dev_stop(priv, 0);
903 ipoib_ib_dev_open(priv);
907 * The device could have been brought down between the start and when
908 * we get here, don't bring it back up if it's not configured up
910 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
911 if (level >= IPOIB_FLUSH_NORMAL)
912 ipoib_ib_dev_up(priv);
913 ipoib_mcast_restart_task(&priv->restart_task);
917 void ipoib_ib_dev_flush_light(struct work_struct *work)
919 struct ipoib_dev_priv *priv =
920 container_of(work, struct ipoib_dev_priv, flush_light);
922 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
925 void ipoib_ib_dev_flush_normal(struct work_struct *work)
927 struct ipoib_dev_priv *priv =
928 container_of(work, struct ipoib_dev_priv, flush_normal);
930 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
933 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
935 struct ipoib_dev_priv *priv =
936 container_of(work, struct ipoib_dev_priv, flush_heavy);
938 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
941 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv)
944 ipoib_dbg(priv, "cleaning up ib_dev\n");
946 ipoib_mcast_stop_thread(priv, 1);
947 ipoib_mcast_dev_flush(priv);
949 ipoib_ah_dev_cleanup(priv);
950 ipoib_transport_dev_cleanup(priv);
954 * Delayed P_Key Assigment Interim Support
956 * The following is initial implementation of delayed P_Key assigment
957 * mechanism. It is using the same approach implemented for the multicast
958 * group join. The single goal of this implementation is to quickly address
959 * Bug #2507. This implementation will probably be removed when the P_Key
960 * change async notification is available.
963 void ipoib_pkey_poll(struct work_struct *work)
965 struct ipoib_dev_priv *priv =
966 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
968 ipoib_pkey_dev_check_presence(priv);
970 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
973 mutex_lock(&pkey_mutex);
974 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
975 queue_delayed_work(ipoib_workqueue,
976 &priv->pkey_poll_task,
978 mutex_unlock(&pkey_mutex);
982 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv)
985 /* Look for the interface pkey value in the IB Port P_Key table and */
986 /* set the interface pkey assigment flag */
987 ipoib_pkey_dev_check_presence(priv);
989 /* P_Key value not assigned yet - start polling */
990 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
991 mutex_lock(&pkey_mutex);
992 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
993 queue_delayed_work(ipoib_workqueue,
994 &priv->pkey_poll_task,
996 mutex_unlock(&pkey_mutex);