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
36 #include <linux/delay.h>
37 #include <linux/moduleparam.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
42 #include <linux/tcp.h>
46 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
47 static int data_debug_level;
49 module_param(data_debug_level, int, 0644);
50 MODULE_PARM_DESC(data_debug_level,
51 "Enable data path debug tracing if > 0");
54 static DEFINE_MUTEX(pkey_mutex);
56 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
57 struct ib_pd *pd, struct ib_ah_attr *attr)
62 ah = kmalloc(sizeof *ah, GFP_KERNEL);
64 return ERR_PTR(-ENOMEM);
70 vah = ib_create_ah(pd, attr);
73 ah = (struct ipoib_ah *)vah;
76 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
82 void ipoib_free_ah(struct kref *kref)
84 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
85 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
89 spin_lock_irqsave(&priv->lock, flags);
90 list_add_tail(&ah->list, &priv->dead_ahs);
91 spin_unlock_irqrestore(&priv->lock, flags);
94 static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
95 u64 mapping[IPOIB_UD_RX_SG])
97 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
98 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
100 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
103 ib_dma_unmap_single(priv->ca, mapping[0],
104 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
108 static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
112 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
113 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
116 * There is only two buffers needed for max_payload = 4K,
117 * first buf size is IPOIB_UD_HEAD_SIZE
119 skb->tail += IPOIB_UD_HEAD_SIZE;
122 size = length - IPOIB_UD_HEAD_SIZE;
124 skb_frag_size_set(frag, size);
125 skb->data_len += size;
126 skb->truesize += size;
128 skb_put(skb, length);
132 static int ipoib_ib_post_receive(struct net_device *dev, int id)
134 struct ipoib_dev_priv *priv = netdev_priv(dev);
135 struct ib_recv_wr *bad_wr;
138 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
139 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
140 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
143 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
145 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
146 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
147 dev_kfree_skb_any(priv->rx_ring[id].skb);
148 priv->rx_ring[id].skb = NULL;
154 static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
161 if (ipoib_ud_need_sg(priv->max_ib_mtu))
162 buf_size = IPOIB_UD_HEAD_SIZE;
164 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
166 skb = dev_alloc_skb(buf_size + 4);
171 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
172 * header. So we need 4 more bytes to get to 48 and align the
173 * IP header to a multiple of 16.
177 mapping = priv->rx_ring[id].mapping;
178 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
180 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
183 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
184 struct page *page = alloc_page(GFP_ATOMIC);
187 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
189 ib_dma_map_page(priv->ca, page,
190 0, PAGE_SIZE, DMA_FROM_DEVICE);
191 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
195 priv->rx_ring[id].skb = skb;
199 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
201 dev_kfree_skb_any(skb);
205 static int ipoib_ib_post_receives(struct net_device *dev)
207 struct ipoib_dev_priv *priv = netdev_priv(dev);
210 for (i = 0; i < ipoib_recvq_size; ++i) {
211 if (!ipoib_alloc_rx_skb(dev, i)) {
212 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
215 if (ipoib_ib_post_receive(dev, i)) {
216 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
224 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
226 struct ipoib_dev_priv *priv = netdev_priv(dev);
227 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
229 u64 mapping[IPOIB_UD_RX_SG];
232 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
235 if (unlikely(wr_id >= ipoib_recvq_size)) {
236 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
237 wr_id, ipoib_recvq_size);
241 skb = priv->rx_ring[wr_id].skb;
243 if (unlikely(wc->status != IB_WC_SUCCESS)) {
244 if (wc->status != IB_WC_WR_FLUSH_ERR)
245 ipoib_warn(priv, "failed recv event "
246 "(status=%d, wrid=%d vend_err %x)\n",
247 wc->status, wr_id, wc->vendor_err);
248 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
249 dev_kfree_skb_any(skb);
250 priv->rx_ring[wr_id].skb = NULL;
255 * Drop packets that this interface sent, ie multicast packets
256 * that the HCA has replicated.
258 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
261 memcpy(mapping, priv->rx_ring[wr_id].mapping,
262 IPOIB_UD_RX_SG * sizeof *mapping);
265 * If we can't allocate a new RX buffer, dump
266 * this packet and reuse the old buffer.
268 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
269 ++dev->stats.rx_dropped;
273 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
274 wc->byte_len, wc->slid);
276 ipoib_ud_dma_unmap_rx(priv, mapping);
277 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
279 /* First byte of dgid signals multicast when 0xff */
280 dgid = &((struct ib_grh *)skb->data)->dgid;
282 if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
283 skb->pkt_type = PACKET_HOST;
284 else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
285 skb->pkt_type = PACKET_BROADCAST;
287 skb->pkt_type = PACKET_MULTICAST;
289 skb_pull(skb, IB_GRH_BYTES);
291 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
292 skb_reset_mac_header(skb);
293 skb_pull(skb, IPOIB_ENCAP_LEN);
295 ++dev->stats.rx_packets;
296 dev->stats.rx_bytes += skb->len;
299 if ((dev->features & NETIF_F_RXCSUM) && likely(wc->csum_ok))
300 skb->ip_summed = CHECKSUM_UNNECESSARY;
302 napi_gro_receive(&priv->napi, skb);
305 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
306 ipoib_warn(priv, "ipoib_ib_post_receive failed "
307 "for buf %d\n", wr_id);
310 static int ipoib_dma_map_tx(struct ib_device *ca,
311 struct ipoib_tx_buf *tx_req)
313 struct sk_buff *skb = tx_req->skb;
314 u64 *mapping = tx_req->mapping;
318 if (skb_headlen(skb)) {
319 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
321 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
328 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
329 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
330 mapping[i + off] = ib_dma_map_page(ca,
332 frag->page_offset, skb_frag_size(frag),
334 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
341 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
343 ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
347 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
352 static void ipoib_dma_unmap_tx(struct ib_device *ca,
353 struct ipoib_tx_buf *tx_req)
355 struct sk_buff *skb = tx_req->skb;
356 u64 *mapping = tx_req->mapping;
360 if (skb_headlen(skb)) {
361 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
366 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
367 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
369 ib_dma_unmap_page(ca, mapping[i + off], skb_frag_size(frag),
374 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
376 struct ipoib_dev_priv *priv = netdev_priv(dev);
377 unsigned int wr_id = wc->wr_id;
378 struct ipoib_tx_buf *tx_req;
380 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
383 if (unlikely(wr_id >= ipoib_sendq_size)) {
384 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
385 wr_id, ipoib_sendq_size);
389 tx_req = &priv->tx_ring[wr_id];
391 ipoib_dma_unmap_tx(priv->ca, tx_req);
393 ++dev->stats.tx_packets;
394 dev->stats.tx_bytes += tx_req->skb->len;
396 dev_kfree_skb_any(tx_req->skb);
399 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
400 netif_queue_stopped(dev) &&
401 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
402 netif_wake_queue(dev);
404 if (wc->status != IB_WC_SUCCESS &&
405 wc->status != IB_WC_WR_FLUSH_ERR)
406 ipoib_warn(priv, "failed send event "
407 "(status=%d, wrid=%d vend_err %x)\n",
408 wc->status, wr_id, wc->vendor_err);
411 static int poll_tx(struct ipoib_dev_priv *priv)
415 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
416 for (i = 0; i < n; ++i)
417 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
419 return n == MAX_SEND_CQE;
422 int ipoib_poll(struct napi_struct *napi, int budget)
424 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
425 struct net_device *dev = priv->dev;
433 while (done < budget) {
434 int max = (budget - done);
436 t = min(IPOIB_NUM_WC, max);
437 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
439 for (i = 0; i < n; i++) {
440 struct ib_wc *wc = priv->ibwc + i;
442 if (wc->wr_id & IPOIB_OP_RECV) {
444 if (wc->wr_id & IPOIB_OP_CM)
445 ipoib_cm_handle_rx_wc(dev, wc);
447 ipoib_ib_handle_rx_wc(dev, wc);
449 ipoib_cm_handle_tx_wc(priv->dev, wc);
458 if (unlikely(ib_req_notify_cq(priv->recv_cq,
460 IB_CQ_REPORT_MISSED_EVENTS)) &&
461 napi_reschedule(napi))
468 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
470 struct net_device *dev = dev_ptr;
471 struct ipoib_dev_priv *priv = netdev_priv(dev);
473 napi_schedule(&priv->napi);
476 static void drain_tx_cq(struct net_device *dev)
478 struct ipoib_dev_priv *priv = netdev_priv(dev);
481 while (poll_tx(priv))
484 if (netif_queue_stopped(dev))
485 mod_timer(&priv->poll_timer, jiffies + 1);
487 netif_tx_unlock(dev);
490 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
492 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
494 mod_timer(&priv->poll_timer, jiffies);
497 static inline int post_send(struct ipoib_dev_priv *priv,
499 struct ib_ah *address, u32 qpn,
500 struct ipoib_tx_buf *tx_req,
501 void *head, int hlen)
503 struct ib_send_wr *bad_wr;
505 struct sk_buff *skb = tx_req->skb;
506 skb_frag_t *frags = skb_shinfo(skb)->frags;
507 int nr_frags = skb_shinfo(skb)->nr_frags;
508 u64 *mapping = tx_req->mapping;
510 if (skb_headlen(skb)) {
511 priv->tx_sge[0].addr = mapping[0];
512 priv->tx_sge[0].length = skb_headlen(skb);
517 for (i = 0; i < nr_frags; ++i) {
518 priv->tx_sge[i + off].addr = mapping[i + off];
519 priv->tx_sge[i + off].length = skb_frag_size(&frags[i]);
521 priv->tx_wr.num_sge = nr_frags + off;
522 priv->tx_wr.wr_id = wr_id;
523 priv->tx_wr.wr.ud.remote_qpn = qpn;
524 priv->tx_wr.wr.ud.ah = address;
527 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
528 priv->tx_wr.wr.ud.header = head;
529 priv->tx_wr.wr.ud.hlen = hlen;
530 priv->tx_wr.opcode = IB_WR_LSO;
532 priv->tx_wr.opcode = IB_WR_SEND;
534 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
537 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
538 struct ipoib_ah *address, u32 qpn)
540 struct ipoib_dev_priv *priv = netdev_priv(dev);
541 struct ipoib_tx_buf *tx_req;
545 if (skb_is_gso(skb)) {
546 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
548 if (unlikely(!skb_pull(skb, hlen))) {
549 ipoib_warn(priv, "linear data too small\n");
550 ++dev->stats.tx_dropped;
551 ++dev->stats.tx_errors;
552 dev_kfree_skb_any(skb);
556 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
557 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
558 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
559 ++dev->stats.tx_dropped;
560 ++dev->stats.tx_errors;
561 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
568 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
569 skb->len, address, qpn);
572 * We put the skb into the tx_ring _before_ we call post_send()
573 * because it's entirely possible that the completion handler will
574 * run before we execute anything after the post_send(). That
575 * means we have to make sure everything is properly recorded and
576 * our state is consistent before we call post_send().
578 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
580 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
581 ++dev->stats.tx_errors;
582 dev_kfree_skb_any(skb);
586 if (skb->ip_summed == CHECKSUM_PARTIAL)
587 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
589 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
591 if (++priv->tx_outstanding == ipoib_sendq_size) {
592 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
593 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
594 ipoib_warn(priv, "request notify on send CQ failed\n");
595 netif_stop_queue(dev);
598 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
599 address->ah, qpn, tx_req, phead, hlen);
601 ipoib_warn(priv, "post_send failed, error %d\n", rc);
602 ++dev->stats.tx_errors;
603 --priv->tx_outstanding;
604 ipoib_dma_unmap_tx(priv->ca, tx_req);
605 dev_kfree_skb_any(skb);
606 if (netif_queue_stopped(dev))
607 netif_wake_queue(dev);
609 dev->trans_start = jiffies;
611 address->last_send = priv->tx_head;
617 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
618 while (poll_tx(priv))
622 static void __ipoib_reap_ah(struct net_device *dev)
624 struct ipoib_dev_priv *priv = netdev_priv(dev);
625 struct ipoib_ah *ah, *tah;
626 LIST_HEAD(remove_list);
629 netif_tx_lock_bh(dev);
630 spin_lock_irqsave(&priv->lock, flags);
632 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
633 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
635 ib_destroy_ah(ah->ah);
639 spin_unlock_irqrestore(&priv->lock, flags);
640 netif_tx_unlock_bh(dev);
643 void ipoib_reap_ah(struct work_struct *work)
645 struct ipoib_dev_priv *priv =
646 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
647 struct net_device *dev = priv->dev;
649 __ipoib_reap_ah(dev);
651 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
652 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
653 round_jiffies_relative(HZ));
656 static void ipoib_ib_tx_timer_func(unsigned long ctx)
658 drain_tx_cq((struct net_device *)ctx);
661 int ipoib_ib_dev_open(struct net_device *dev)
663 struct ipoib_dev_priv *priv = netdev_priv(dev);
666 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
667 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
668 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
671 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
673 ret = ipoib_init_qp(dev);
675 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
679 ret = ipoib_ib_post_receives(dev);
681 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
682 ipoib_ib_dev_stop(dev, 1);
686 ret = ipoib_cm_dev_open(dev);
688 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
689 ipoib_ib_dev_stop(dev, 1);
693 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
694 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
695 round_jiffies_relative(HZ));
697 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
698 napi_enable(&priv->napi);
703 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
705 struct ipoib_dev_priv *priv = netdev_priv(dev);
708 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
709 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
711 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
714 int ipoib_ib_dev_up(struct net_device *dev)
716 struct ipoib_dev_priv *priv = netdev_priv(dev);
718 ipoib_pkey_dev_check_presence(dev);
720 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
721 ipoib_dbg(priv, "PKEY is not assigned.\n");
725 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
727 return ipoib_mcast_start_thread(dev);
730 int ipoib_ib_dev_down(struct net_device *dev, int flush)
732 struct ipoib_dev_priv *priv = netdev_priv(dev);
734 ipoib_dbg(priv, "downing ib_dev\n");
736 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
737 netif_carrier_off(dev);
739 /* Shutdown the P_Key thread if still active */
740 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
741 mutex_lock(&pkey_mutex);
742 set_bit(IPOIB_PKEY_STOP, &priv->flags);
743 cancel_delayed_work(&priv->pkey_poll_task);
744 mutex_unlock(&pkey_mutex);
746 flush_workqueue(ipoib_workqueue);
749 ipoib_mcast_stop_thread(dev, flush);
750 ipoib_mcast_dev_flush(dev);
752 ipoib_flush_paths(dev);
757 static int recvs_pending(struct net_device *dev)
759 struct ipoib_dev_priv *priv = netdev_priv(dev);
763 for (i = 0; i < ipoib_recvq_size; ++i)
764 if (priv->rx_ring[i].skb)
770 void ipoib_drain_cq(struct net_device *dev)
772 struct ipoib_dev_priv *priv = netdev_priv(dev);
776 * We call completion handling routines that expect to be
777 * called from the BH-disabled NAPI poll context, so disable
783 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
784 for (i = 0; i < n; ++i) {
786 * Convert any successful completions to flush
787 * errors to avoid passing packets up the
788 * stack after bringing the device down.
790 if (priv->ibwc[i].status == IB_WC_SUCCESS)
791 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
793 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
794 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
795 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
797 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
799 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
801 } while (n == IPOIB_NUM_WC);
803 while (poll_tx(priv))
809 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
811 struct ipoib_dev_priv *priv = netdev_priv(dev);
812 struct ib_qp_attr qp_attr;
814 struct ipoib_tx_buf *tx_req;
817 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
818 napi_disable(&priv->napi);
820 ipoib_cm_dev_stop(dev);
823 * Move our QP to the error state and then reinitialize in
824 * when all work requests have completed or have been flushed.
826 qp_attr.qp_state = IB_QPS_ERR;
827 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
828 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
830 /* Wait for all sends and receives to complete */
833 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
834 if (time_after(jiffies, begin + 5 * HZ)) {
835 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
836 priv->tx_head - priv->tx_tail, recvs_pending(dev));
839 * assume the HW is wedged and just free up
840 * all our pending work requests.
842 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
843 tx_req = &priv->tx_ring[priv->tx_tail &
844 (ipoib_sendq_size - 1)];
845 ipoib_dma_unmap_tx(priv->ca, tx_req);
846 dev_kfree_skb_any(tx_req->skb);
848 --priv->tx_outstanding;
851 for (i = 0; i < ipoib_recvq_size; ++i) {
852 struct ipoib_rx_buf *rx_req;
854 rx_req = &priv->rx_ring[i];
857 ipoib_ud_dma_unmap_rx(priv,
858 priv->rx_ring[i].mapping);
859 dev_kfree_skb_any(rx_req->skb);
871 ipoib_dbg(priv, "All sends and receives done.\n");
874 del_timer_sync(&priv->poll_timer);
875 qp_attr.qp_state = IB_QPS_RESET;
876 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
877 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
879 /* Wait for all AHs to be reaped */
880 set_bit(IPOIB_STOP_REAPER, &priv->flags);
881 cancel_delayed_work(&priv->ah_reap_task);
883 flush_workqueue(ipoib_workqueue);
887 while (!list_empty(&priv->dead_ahs)) {
888 __ipoib_reap_ah(dev);
890 if (time_after(jiffies, begin + HZ)) {
891 ipoib_warn(priv, "timing out; will leak address handles\n");
898 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
903 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
905 struct ipoib_dev_priv *priv = netdev_priv(dev);
911 if (ipoib_transport_dev_init(dev, ca)) {
912 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
916 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
917 (unsigned long) dev);
919 if (dev->flags & IFF_UP) {
920 if (ipoib_ib_dev_open(dev)) {
921 ipoib_transport_dev_cleanup(dev);
929 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
930 enum ipoib_flush_level level)
932 struct ipoib_dev_priv *cpriv;
933 struct net_device *dev = priv->dev;
936 mutex_lock(&priv->vlan_mutex);
939 * Flush any child interfaces too -- they might be up even if
940 * the parent is down.
942 list_for_each_entry(cpriv, &priv->child_intfs, list)
943 __ipoib_ib_dev_flush(cpriv, level);
945 mutex_unlock(&priv->vlan_mutex);
947 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
948 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
952 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
953 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
957 if (level == IPOIB_FLUSH_HEAVY) {
958 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
959 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
960 ipoib_ib_dev_down(dev, 0);
961 ipoib_ib_dev_stop(dev, 0);
962 if (ipoib_pkey_dev_delay_open(dev))
966 /* restart QP only if P_Key index is changed */
967 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
968 new_index == priv->pkey_index) {
969 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
972 priv->pkey_index = new_index;
975 if (level == IPOIB_FLUSH_LIGHT) {
976 ipoib_mark_paths_invalid(dev);
977 ipoib_mcast_dev_flush(dev);
980 if (level >= IPOIB_FLUSH_NORMAL)
981 ipoib_ib_dev_down(dev, 0);
983 if (level == IPOIB_FLUSH_HEAVY) {
984 ipoib_ib_dev_stop(dev, 0);
985 ipoib_ib_dev_open(dev);
989 * The device could have been brought down between the start and when
990 * we get here, don't bring it back up if it's not configured up
992 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
993 if (level >= IPOIB_FLUSH_NORMAL)
994 ipoib_ib_dev_up(dev);
995 ipoib_mcast_restart_task(&priv->restart_task);
999 void ipoib_ib_dev_flush_light(struct work_struct *work)
1001 struct ipoib_dev_priv *priv =
1002 container_of(work, struct ipoib_dev_priv, flush_light);
1004 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
1007 void ipoib_ib_dev_flush_normal(struct work_struct *work)
1009 struct ipoib_dev_priv *priv =
1010 container_of(work, struct ipoib_dev_priv, flush_normal);
1012 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1015 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1017 struct ipoib_dev_priv *priv =
1018 container_of(work, struct ipoib_dev_priv, flush_heavy);
1020 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
1023 void ipoib_ib_dev_cleanup(struct net_device *dev)
1025 struct ipoib_dev_priv *priv = netdev_priv(dev);
1027 ipoib_dbg(priv, "cleaning up ib_dev\n");
1029 ipoib_mcast_stop_thread(dev, 1);
1030 ipoib_mcast_dev_flush(dev);
1032 ipoib_transport_dev_cleanup(dev);
1036 * Delayed P_Key Assigment Interim Support
1038 * The following is initial implementation of delayed P_Key assigment
1039 * mechanism. It is using the same approach implemented for the multicast
1040 * group join. The single goal of this implementation is to quickly address
1041 * Bug #2507. This implementation will probably be removed when the P_Key
1042 * change async notification is available.
1045 void ipoib_pkey_poll(struct work_struct *work)
1047 struct ipoib_dev_priv *priv =
1048 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
1049 struct net_device *dev = priv->dev;
1051 ipoib_pkey_dev_check_presence(dev);
1053 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1056 mutex_lock(&pkey_mutex);
1057 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1058 queue_delayed_work(ipoib_workqueue,
1059 &priv->pkey_poll_task,
1061 mutex_unlock(&pkey_mutex);
1065 int ipoib_pkey_dev_delay_open(struct net_device *dev)
1067 struct ipoib_dev_priv *priv = netdev_priv(dev);
1069 /* Look for the interface pkey value in the IB Port P_Key table and */
1070 /* set the interface pkey assigment flag */
1071 ipoib_pkey_dev_check_presence(dev);
1073 /* P_Key value not assigned yet - start polling */
1074 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
1075 mutex_lock(&pkey_mutex);
1076 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1077 queue_delayed_work(ipoib_workqueue,
1078 &priv->pkey_poll_task,
1080 mutex_unlock(&pkey_mutex);