2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
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.
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:
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
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.
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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include <rdma/ib_cache.h>
45 #include <security/mac/mac_framework.h>
47 #include <linux/delay.h>
48 #include <linux/dma-mapping.h>
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51 static int data_debug_level;
53 module_param(data_debug_level, int, 0644);
54 MODULE_PARM_DESC(data_debug_level,
55 "Enable data path debug tracing if > 0");
58 static DEFINE_MUTEX(pkey_mutex);
60 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *priv,
61 struct ib_pd *pd, struct ib_ah_attr *attr)
65 ah = kmalloc(sizeof *ah, GFP_KERNEL);
73 ah->ah = ib_create_ah(pd, attr);
78 ipoib_dbg(priv, "Created ah %p\n", ah->ah);
83 void ipoib_free_ah(struct kref *kref)
85 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
86 struct ipoib_dev_priv *priv = ah->priv;
90 spin_lock_irqsave(&priv->lock, flags);
91 list_add_tail(&ah->list, &priv->dead_ahs);
92 spin_unlock_irqrestore(&priv->lock, flags);
96 ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req)
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,
107 ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length)
110 m_adj(mb, -(mb->m_pkthdr.len - length));
114 ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req,
121 mb = m_getm2(NULL, size, M_NOWAIT, MT_DATA, M_PKTHDR);
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])))
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,
145 static int ipoib_ib_post_receive(struct ipoib_dev_priv *priv, int id)
147 struct ipoib_rx_buf *rx_req;
148 struct ib_recv_wr *bad_wr;
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;
158 priv->rx_wr.num_sge = i;
159 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
161 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
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;
173 ipoib_alloc_rx_mb(struct ipoib_dev_priv *priv, int id)
176 return ipoib_alloc_map_mb(priv, &priv->rx_ring[id],
177 priv->max_ib_mtu + IB_GRH_BYTES);
180 static int ipoib_ib_post_receives(struct ipoib_dev_priv *priv)
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);
189 if (ipoib_ib_post_receive(priv, i)) {
190 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
199 ipoib_ib_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
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;
207 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
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);
216 mb = priv->rx_ring[wr_id].mb;
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);
226 ipoib_dma_unmap_rx(priv, &priv->rx_ring[wr_id]);
228 priv->rx_ring[wr_id].mb = NULL;
234 * Drop packets that this interface sent, ie multicast packets
235 * that the HCA has replicated.
237 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
240 memcpy(&saverx, &priv->rx_ring[wr_id], sizeof(saverx));
242 * If we can't allocate a new RX buffer, dump
243 * this packet and reuse the old buffer.
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);
251 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
252 wc->byte_len, wc->slid);
254 ipoib_dma_unmap_rx(priv, &saverx);
255 ipoib_dma_mb(priv, mb, wc->byte_len);
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 */
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;
267 dev->if_input(dev, mb);
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);
275 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max)
277 struct mbuf *mb = tx_req->mb;
278 u64 *mapping = tx_req->mapping;
283 for (m = mb, p = NULL, i = 0; m != NULL; p = m, m = m->m_next, i++) {
287 panic("ipoib_dma_map_tx: First mbuf empty\n");
288 p->m_next = m_free(m);
294 tx_req->mb = mb = m_defrag(mb, M_NOWAIT);
297 for (m = mb, i = 0; m != NULL; m = m->m_next, i++);
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]))) {
314 for (m = mb, i = 0; i < end; m = m->m_next, i++)
315 ib_dma_unmap_single(ca, mapping[i], m->m_len,
321 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
323 struct mbuf *mb = tx_req->mb;
324 u64 *mapping = tx_req->mapping;
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);
332 static void ipoib_ib_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
334 struct ifnet *dev = priv->dev;
335 unsigned int wr_id = wc->wr_id;
336 struct ipoib_tx_buf *tx_req;
338 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
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);
347 tx_req = &priv->tx_ring[wr_id];
349 ipoib_dma_unmap_tx(priv->ca, tx_req);
351 if_inc_counter(dev, IFCOUNTER_OPACKETS, 1);
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;
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);
369 ipoib_poll_tx(struct ipoib_dev_priv *priv)
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);
379 ipoib_ib_handle_tx_wc(priv, wc);
382 return n == MAX_SEND_CQE;
386 ipoib_poll(struct ipoib_dev_priv *priv)
391 spin_lock(&priv->drain_lock);
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;
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);
403 ipoib_ib_handle_rx_wc(priv, wc);
406 if (n != IPOIB_NUM_WC)
409 spin_unlock(&priv->drain_lock);
411 if (ib_req_notify_cq(priv->recv_cq,
412 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS))
416 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
418 struct ipoib_dev_priv *priv = dev_ptr;
423 static void drain_tx_cq(struct ipoib_dev_priv *priv)
425 struct ifnet *dev = priv->dev;
427 spin_lock(&priv->lock);
428 while (ipoib_poll_tx(priv))
431 if (dev->if_drv_flags & IFF_DRV_OACTIVE)
432 mod_timer(&priv->poll_timer, jiffies + 1);
434 spin_unlock(&priv->lock);
437 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
439 struct ipoib_dev_priv *priv = dev_ptr;
441 mod_timer(&priv->poll_timer, jiffies);
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,
449 struct ib_send_wr *bad_wr;
450 struct mbuf *mb = tx_req->mb;
451 u64 *mapping = tx_req->mapping;
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;
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;
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;
470 priv->tx_wr.wr.opcode = IB_WR_SEND;
472 return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr);
476 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb,
477 struct ipoib_ah *address, u32 qpn)
479 struct ifnet *dev = priv->dev;
480 struct ipoib_tx_buf *tx_req;
484 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
485 while (ipoib_poll_tx(priv))
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);
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);
511 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
512 mb->m_pkthdr.len, address, qpn);
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().
521 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
523 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) {
524 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
530 if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP))
531 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
533 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
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;
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);
550 if (dev->if_drv_flags & IFF_DRV_OACTIVE)
551 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
553 address->last_send = priv->tx_head;
558 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv)
560 struct ipoib_ah *ah, *tah;
561 LIST_HEAD(remove_list);
564 spin_lock_irqsave(&priv->lock, flags);
566 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
567 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
569 ib_destroy_ah(ah->ah);
573 spin_unlock_irqrestore(&priv->lock, flags);
576 void ipoib_reap_ah(struct work_struct *work)
578 struct ipoib_dev_priv *priv =
579 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
581 __ipoib_reap_ah(priv);
583 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
584 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
588 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv)
594 while (!list_empty(&priv->dead_ahs)) {
595 __ipoib_reap_ah(priv);
597 if (time_after(jiffies, begin + HZ)) {
598 ipoib_warn(priv, "timing out; will leak address handles\n");
606 static void ipoib_ib_tx_timer_func(unsigned long ctx)
608 drain_tx_cq((struct ipoib_dev_priv *)ctx);
611 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv)
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);
620 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
622 ret = ipoib_init_qp(priv);
624 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
628 ret = ipoib_ib_post_receives(priv);
630 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
631 ipoib_ib_dev_stop(priv, 1);
635 ret = ipoib_cm_dev_open(priv);
637 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
638 ipoib_ib_dev_stop(priv, 1);
642 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
643 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
645 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
650 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv)
654 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
655 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
657 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
660 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv)
663 ipoib_pkey_dev_check_presence(priv);
665 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
666 ipoib_dbg(priv, "PKEY is not assigned.\n");
670 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
672 return ipoib_mcast_start_thread(priv);
675 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush)
678 ipoib_dbg(priv, "downing ib_dev\n");
680 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
681 if_link_state_change(priv->dev, LINK_STATE_DOWN);
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);
690 flush_workqueue(ipoib_workqueue);
693 ipoib_mcast_stop_thread(priv, flush);
694 ipoib_mcast_dev_flush(priv);
696 ipoib_flush_paths(priv);
701 static int recvs_pending(struct ipoib_dev_priv *priv)
706 for (i = 0; i < ipoib_recvq_size; ++i)
707 if (priv->rx_ring[i].mb)
713 void ipoib_drain_cq(struct ipoib_dev_priv *priv)
717 spin_lock(&priv->drain_lock);
719 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
720 for (i = 0; i < n; ++i) {
722 * Convert any successful completions to flush
723 * errors to avoid passing packets up the
724 * stack after bringing the device down.
726 if (priv->ibwc[i].status == IB_WC_SUCCESS)
727 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
729 if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0)
730 panic("ipoib_drain_cq: Bad wrid 0x%jX\n",
731 (intmax_t)priv->ibwc[i].wr_id);
732 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
733 ipoib_cm_handle_rx_wc(priv, priv->ibwc + i);
735 ipoib_ib_handle_rx_wc(priv, priv->ibwc + i);
737 } while (n == IPOIB_NUM_WC);
738 spin_unlock(&priv->drain_lock);
740 spin_lock(&priv->lock);
741 while (ipoib_poll_tx(priv))
744 spin_unlock(&priv->lock);
747 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush)
749 struct ib_qp_attr qp_attr;
751 struct ipoib_tx_buf *tx_req;
754 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
756 ipoib_cm_dev_stop(priv);
759 * Move our QP to the error state and then reinitialize in
760 * when all work requests have completed or have been flushed.
762 qp_attr.qp_state = IB_QPS_ERR;
763 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
764 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
766 /* Wait for all sends and receives to complete */
769 while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) {
770 if (time_after(jiffies, begin + 5 * HZ)) {
771 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
772 priv->tx_head - priv->tx_tail, recvs_pending(priv));
775 * assume the HW is wedged and just free up
776 * all our pending work requests.
778 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
779 tx_req = &priv->tx_ring[priv->tx_tail &
780 (ipoib_sendq_size - 1)];
781 ipoib_dma_unmap_tx(priv->ca, tx_req);
784 --priv->tx_outstanding;
787 for (i = 0; i < ipoib_recvq_size; ++i) {
788 struct ipoib_rx_buf *rx_req;
790 rx_req = &priv->rx_ring[i];
793 ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]);
801 ipoib_drain_cq(priv);
806 ipoib_dbg(priv, "All sends and receives done.\n");
809 del_timer_sync(&priv->poll_timer);
810 qp_attr.qp_state = IB_QPS_RESET;
811 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
812 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
814 /* Wait for all AHs to be reaped */
815 set_bit(IPOIB_STOP_REAPER, &priv->flags);
816 cancel_delayed_work(&priv->ah_reap_task);
818 flush_workqueue(ipoib_workqueue);
820 ipoib_ah_dev_cleanup(priv);
822 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
827 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
829 struct ifnet *dev = priv->dev;
835 if (ipoib_transport_dev_init(priv, ca)) {
836 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
840 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
841 (unsigned long) priv);
843 if (dev->if_flags & IFF_UP) {
844 if (ipoib_ib_dev_open(priv)) {
845 ipoib_transport_dev_cleanup(priv);
853 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
854 enum ipoib_flush_level level)
856 struct ipoib_dev_priv *cpriv;
859 mutex_lock(&priv->vlan_mutex);
862 * Flush any child interfaces too -- they might be up even if
863 * the parent is down.
865 list_for_each_entry(cpriv, &priv->child_intfs, list)
866 __ipoib_ib_dev_flush(cpriv, level);
868 mutex_unlock(&priv->vlan_mutex);
870 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
871 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
875 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
876 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
880 if (level == IPOIB_FLUSH_HEAVY) {
881 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
882 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
883 ipoib_ib_dev_down(priv, 0);
884 ipoib_ib_dev_stop(priv, 0);
885 if (ipoib_pkey_dev_delay_open(priv))
889 /* restart QP only if P_Key index is changed */
890 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
891 new_index == priv->pkey_index) {
892 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
895 priv->pkey_index = new_index;
898 if (level == IPOIB_FLUSH_LIGHT) {
899 ipoib_mark_paths_invalid(priv);
900 ipoib_mcast_dev_flush(priv);
903 if (level >= IPOIB_FLUSH_NORMAL)
904 ipoib_ib_dev_down(priv, 0);
906 if (level == IPOIB_FLUSH_HEAVY) {
907 ipoib_ib_dev_stop(priv, 0);
908 ipoib_ib_dev_open(priv);
912 * The device could have been brought down between the start and when
913 * we get here, don't bring it back up if it's not configured up
915 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
916 if (level >= IPOIB_FLUSH_NORMAL)
917 ipoib_ib_dev_up(priv);
918 ipoib_mcast_restart_task(&priv->restart_task);
922 void ipoib_ib_dev_flush_light(struct work_struct *work)
924 struct ipoib_dev_priv *priv =
925 container_of(work, struct ipoib_dev_priv, flush_light);
927 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
930 void ipoib_ib_dev_flush_normal(struct work_struct *work)
932 struct ipoib_dev_priv *priv =
933 container_of(work, struct ipoib_dev_priv, flush_normal);
935 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
938 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
940 struct ipoib_dev_priv *priv =
941 container_of(work, struct ipoib_dev_priv, flush_heavy);
943 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
946 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv)
949 ipoib_dbg(priv, "cleaning up ib_dev\n");
951 ipoib_mcast_stop_thread(priv, 1);
952 ipoib_mcast_dev_flush(priv);
954 ipoib_ah_dev_cleanup(priv);
955 ipoib_transport_dev_cleanup(priv);
959 * Delayed P_Key Assigment Interim Support
961 * The following is initial implementation of delayed P_Key assigment
962 * mechanism. It is using the same approach implemented for the multicast
963 * group join. The single goal of this implementation is to quickly address
964 * Bug #2507. This implementation will probably be removed when the P_Key
965 * change async notification is available.
968 void ipoib_pkey_poll(struct work_struct *work)
970 struct ipoib_dev_priv *priv =
971 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
973 ipoib_pkey_dev_check_presence(priv);
975 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
978 mutex_lock(&pkey_mutex);
979 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
980 queue_delayed_work(ipoib_workqueue,
981 &priv->pkey_poll_task,
983 mutex_unlock(&pkey_mutex);
987 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv)
990 /* Look for the interface pkey value in the IB Port P_Key table and */
991 /* set the interface pkey assigment flag */
992 ipoib_pkey_dev_check_presence(priv);
994 /* P_Key value not assigned yet - start polling */
995 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
996 mutex_lock(&pkey_mutex);
997 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
998 queue_delayed_work(ipoib_workqueue,
999 &priv->pkey_poll_task,
1001 mutex_unlock(&pkey_mutex);