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/dma-mapping.h>
38 #include <linux/slab.h>
41 #include <linux/tcp.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 net_device *dev,
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(netdev_priv(dev), "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 = netdev_priv(ah->dev);
85 spin_lock_irqsave(&priv->lock, flags);
86 list_add_tail(&ah->list, &priv->dead_ahs);
87 spin_unlock_irqrestore(&priv->lock, flags);
90 static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
91 u64 mapping[IPOIB_UD_RX_SG])
93 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
94 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,
96 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
99 ib_dma_unmap_single(priv->ca, mapping[0],
100 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
104 static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
108 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
109 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
112 * There is only two buffers needed for max_payload = 4K,
113 * first buf size is IPOIB_UD_HEAD_SIZE
115 skb->tail += IPOIB_UD_HEAD_SIZE;
118 size = length - IPOIB_UD_HEAD_SIZE;
121 skb->data_len += size;
122 skb->truesize += size;
124 skb_put(skb, length);
128 static int ipoib_ib_post_receive(struct net_device *dev, int id)
130 struct ipoib_dev_priv *priv = netdev_priv(dev);
131 struct ib_recv_wr *bad_wr;
134 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
135 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
136 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
139 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
141 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
142 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
143 dev_kfree_skb_any(priv->rx_ring[id].skb);
144 priv->rx_ring[id].skb = NULL;
150 static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 if (ipoib_ud_need_sg(priv->max_ib_mtu))
158 buf_size = IPOIB_UD_HEAD_SIZE;
160 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
162 skb = dev_alloc_skb(buf_size + 4);
167 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
168 * header. So we need 4 more bytes to get to 48 and align the
169 * IP header to a multiple of 16.
173 mapping = priv->rx_ring[id].mapping;
174 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
176 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
179 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
180 struct page *page = alloc_page(GFP_ATOMIC);
183 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
185 ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,
186 0, PAGE_SIZE, DMA_FROM_DEVICE);
187 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
191 priv->rx_ring[id].skb = skb;
195 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
197 dev_kfree_skb_any(skb);
201 static int ipoib_ib_post_receives(struct net_device *dev)
203 struct ipoib_dev_priv *priv = netdev_priv(dev);
206 for (i = 0; i < ipoib_recvq_size; ++i) {
207 if (!ipoib_alloc_rx_skb(dev, i)) {
208 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
211 if (ipoib_ib_post_receive(dev, i)) {
212 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
220 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
222 struct ipoib_dev_priv *priv = netdev_priv(dev);
223 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
225 u64 mapping[IPOIB_UD_RX_SG];
228 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
231 if (unlikely(wr_id >= ipoib_recvq_size)) {
232 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
233 wr_id, ipoib_recvq_size);
237 skb = priv->rx_ring[wr_id].skb;
239 if (unlikely(wc->status != IB_WC_SUCCESS)) {
240 if (wc->status != IB_WC_WR_FLUSH_ERR)
241 ipoib_warn(priv, "failed recv event "
242 "(status=%d, wrid=%d vend_err %x)\n",
243 wc->status, wr_id, wc->vendor_err);
244 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
245 dev_kfree_skb_any(skb);
246 priv->rx_ring[wr_id].skb = NULL;
251 * Drop packets that this interface sent, ie multicast packets
252 * that the HCA has replicated.
254 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
257 memcpy(mapping, priv->rx_ring[wr_id].mapping,
258 IPOIB_UD_RX_SG * sizeof *mapping);
261 * If we can't allocate a new RX buffer, dump
262 * this packet and reuse the old buffer.
264 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
265 ++dev->stats.rx_dropped;
269 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
270 wc->byte_len, wc->slid);
272 ipoib_ud_dma_unmap_rx(priv, mapping);
273 ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);
275 /* First byte of dgid signals multicast when 0xff */
276 dgid = &((struct ib_grh *)skb->data)->dgid;
278 if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
279 skb->pkt_type = PACKET_HOST;
280 else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
281 skb->pkt_type = PACKET_BROADCAST;
283 skb->pkt_type = PACKET_MULTICAST;
285 skb_pull(skb, IB_GRH_BYTES);
287 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
288 skb_reset_mac_header(skb);
289 skb_pull(skb, IPOIB_ENCAP_LEN);
291 ++dev->stats.rx_packets;
292 dev->stats.rx_bytes += skb->len;
295 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
296 skb->ip_summed = CHECKSUM_UNNECESSARY;
298 if (dev->features & NETIF_F_LRO)
299 lro_receive_skb(&priv->lro.lro_mgr, skb, NULL);
301 netif_receive_skb(skb);
304 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
305 ipoib_warn(priv, "ipoib_ib_post_receive failed "
306 "for buf %d\n", wr_id);
309 static int ipoib_dma_map_tx(struct ib_device *ca,
310 struct ipoib_tx_buf *tx_req)
312 struct sk_buff *skb = tx_req->skb;
313 u64 *mapping = tx_req->mapping;
317 if (skb_headlen(skb)) {
318 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
320 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
327 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
328 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
329 mapping[i + off] = ib_dma_map_page(ca, frag->page,
330 frag->page_offset, frag->size,
332 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
339 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
340 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
344 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
349 static void ipoib_dma_unmap_tx(struct ib_device *ca,
350 struct ipoib_tx_buf *tx_req)
352 struct sk_buff *skb = tx_req->skb;
353 u64 *mapping = tx_req->mapping;
357 if (skb_headlen(skb)) {
358 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
363 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
364 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
365 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
370 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
372 struct ipoib_dev_priv *priv = netdev_priv(dev);
373 unsigned int wr_id = wc->wr_id;
374 struct ipoib_tx_buf *tx_req;
376 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
379 if (unlikely(wr_id >= ipoib_sendq_size)) {
380 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
381 wr_id, ipoib_sendq_size);
385 tx_req = &priv->tx_ring[wr_id];
387 ipoib_dma_unmap_tx(priv->ca, tx_req);
389 ++dev->stats.tx_packets;
390 dev->stats.tx_bytes += tx_req->skb->len;
392 dev_kfree_skb_any(tx_req->skb);
395 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
396 netif_queue_stopped(dev) &&
397 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
398 netif_wake_queue(dev);
400 if (wc->status != IB_WC_SUCCESS &&
401 wc->status != IB_WC_WR_FLUSH_ERR)
402 ipoib_warn(priv, "failed send event "
403 "(status=%d, wrid=%d vend_err %x)\n",
404 wc->status, wr_id, wc->vendor_err);
407 static int poll_tx(struct ipoib_dev_priv *priv)
411 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
412 for (i = 0; i < n; ++i)
413 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
415 return n == MAX_SEND_CQE;
418 int ipoib_poll(struct napi_struct *napi, int budget)
420 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
421 struct net_device *dev = priv->dev;
429 while (done < budget) {
430 int max = (budget - done);
432 t = min(IPOIB_NUM_WC, max);
433 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
435 for (i = 0; i < n; i++) {
436 struct ib_wc *wc = priv->ibwc + i;
438 if (wc->wr_id & IPOIB_OP_RECV) {
440 if (wc->wr_id & IPOIB_OP_CM)
441 ipoib_cm_handle_rx_wc(dev, wc);
443 ipoib_ib_handle_rx_wc(dev, wc);
445 ipoib_cm_handle_tx_wc(priv->dev, wc);
453 if (dev->features & NETIF_F_LRO)
454 lro_flush_all(&priv->lro.lro_mgr);
457 if (unlikely(ib_req_notify_cq(priv->recv_cq,
459 IB_CQ_REPORT_MISSED_EVENTS)) &&
460 napi_reschedule(napi))
467 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
469 struct net_device *dev = dev_ptr;
470 struct ipoib_dev_priv *priv = netdev_priv(dev);
472 napi_schedule(&priv->napi);
475 static void drain_tx_cq(struct net_device *dev)
477 struct ipoib_dev_priv *priv = netdev_priv(dev);
480 while (poll_tx(priv))
483 if (netif_queue_stopped(dev))
484 mod_timer(&priv->poll_timer, jiffies + 1);
486 netif_tx_unlock(dev);
489 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
491 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
493 mod_timer(&priv->poll_timer, jiffies);
496 static inline int post_send(struct ipoib_dev_priv *priv,
498 struct ib_ah *address, u32 qpn,
499 struct ipoib_tx_buf *tx_req,
500 void *head, int hlen)
502 struct ib_send_wr *bad_wr;
504 struct sk_buff *skb = tx_req->skb;
505 skb_frag_t *frags = skb_shinfo(skb)->frags;
506 int nr_frags = skb_shinfo(skb)->nr_frags;
507 u64 *mapping = tx_req->mapping;
509 if (skb_headlen(skb)) {
510 priv->tx_sge[0].addr = mapping[0];
511 priv->tx_sge[0].length = skb_headlen(skb);
516 for (i = 0; i < nr_frags; ++i) {
517 priv->tx_sge[i + off].addr = mapping[i + off];
518 priv->tx_sge[i + off].length = frags[i].size;
520 priv->tx_wr.num_sge = nr_frags + off;
521 priv->tx_wr.wr_id = wr_id;
522 priv->tx_wr.wr.ud.remote_qpn = qpn;
523 priv->tx_wr.wr.ud.ah = address;
526 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
527 priv->tx_wr.wr.ud.header = head;
528 priv->tx_wr.wr.ud.hlen = hlen;
529 priv->tx_wr.opcode = IB_WR_LSO;
531 priv->tx_wr.opcode = IB_WR_SEND;
533 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
536 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
537 struct ipoib_ah *address, u32 qpn)
539 struct ipoib_dev_priv *priv = netdev_priv(dev);
540 struct ipoib_tx_buf *tx_req;
544 if (skb_is_gso(skb)) {
545 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
547 if (unlikely(!skb_pull(skb, hlen))) {
548 ipoib_warn(priv, "linear data too small\n");
549 ++dev->stats.tx_dropped;
550 ++dev->stats.tx_errors;
551 dev_kfree_skb_any(skb);
555 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
556 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
557 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
558 ++dev->stats.tx_dropped;
559 ++dev->stats.tx_errors;
560 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
567 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
568 skb->len, address, qpn);
571 * We put the skb into the tx_ring _before_ we call post_send()
572 * because it's entirely possible that the completion handler will
573 * run before we execute anything after the post_send(). That
574 * means we have to make sure everything is properly recorded and
575 * our state is consistent before we call post_send().
577 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
579 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
580 ++dev->stats.tx_errors;
581 dev_kfree_skb_any(skb);
585 if (skb->ip_summed == CHECKSUM_PARTIAL)
586 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
588 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
590 if (++priv->tx_outstanding == ipoib_sendq_size) {
591 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
592 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
593 ipoib_warn(priv, "request notify on send CQ failed\n");
594 netif_stop_queue(dev);
597 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
598 address->ah, qpn, tx_req, phead, hlen);
600 ipoib_warn(priv, "post_send failed, error %d\n", rc);
601 ++dev->stats.tx_errors;
602 --priv->tx_outstanding;
603 ipoib_dma_unmap_tx(priv->ca, tx_req);
604 dev_kfree_skb_any(skb);
605 if (netif_queue_stopped(dev))
606 netif_wake_queue(dev);
608 dev->trans_start = jiffies;
610 address->last_send = priv->tx_head;
616 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
617 while (poll_tx(priv))
621 static void __ipoib_reap_ah(struct net_device *dev)
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 struct ipoib_ah *ah, *tah;
625 LIST_HEAD(remove_list);
628 netif_tx_lock_bh(dev);
629 spin_lock_irqsave(&priv->lock, flags);
631 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
632 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
634 ib_destroy_ah(ah->ah);
638 spin_unlock_irqrestore(&priv->lock, flags);
639 netif_tx_unlock_bh(dev);
642 void ipoib_reap_ah(struct work_struct *work)
644 struct ipoib_dev_priv *priv =
645 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
646 struct net_device *dev = priv->dev;
648 __ipoib_reap_ah(dev);
650 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
651 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
652 round_jiffies_relative(HZ));
655 static void ipoib_ib_tx_timer_func(unsigned long ctx)
657 drain_tx_cq((struct net_device *)ctx);
660 int ipoib_ib_dev_open(struct net_device *dev)
662 struct ipoib_dev_priv *priv = netdev_priv(dev);
665 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
666 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
667 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
670 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
672 ret = ipoib_init_qp(dev);
674 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
678 ret = ipoib_ib_post_receives(dev);
680 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
681 ipoib_ib_dev_stop(dev, 1);
685 ret = ipoib_cm_dev_open(dev);
687 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
688 ipoib_ib_dev_stop(dev, 1);
692 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
693 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
694 round_jiffies_relative(HZ));
696 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
697 napi_enable(&priv->napi);
702 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
704 struct ipoib_dev_priv *priv = netdev_priv(dev);
707 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
708 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
710 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
713 int ipoib_ib_dev_up(struct net_device *dev)
715 struct ipoib_dev_priv *priv = netdev_priv(dev);
717 ipoib_pkey_dev_check_presence(dev);
719 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
720 ipoib_dbg(priv, "PKEY is not assigned.\n");
724 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
726 return ipoib_mcast_start_thread(dev);
729 int ipoib_ib_dev_down(struct net_device *dev, int flush)
731 struct ipoib_dev_priv *priv = netdev_priv(dev);
733 ipoib_dbg(priv, "downing ib_dev\n");
735 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
736 netif_carrier_off(dev);
738 /* Shutdown the P_Key thread if still active */
739 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
740 mutex_lock(&pkey_mutex);
741 set_bit(IPOIB_PKEY_STOP, &priv->flags);
742 cancel_delayed_work(&priv->pkey_poll_task);
743 mutex_unlock(&pkey_mutex);
745 flush_workqueue(ipoib_workqueue);
748 ipoib_mcast_stop_thread(dev, flush);
749 ipoib_mcast_dev_flush(dev);
751 ipoib_flush_paths(dev);
756 static int recvs_pending(struct net_device *dev)
758 struct ipoib_dev_priv *priv = netdev_priv(dev);
762 for (i = 0; i < ipoib_recvq_size; ++i)
763 if (priv->rx_ring[i].skb)
769 void ipoib_drain_cq(struct net_device *dev)
771 struct ipoib_dev_priv *priv = netdev_priv(dev);
775 * We call completion handling routines that expect to be
776 * called from the BH-disabled NAPI poll context, so disable
782 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
783 for (i = 0; i < n; ++i) {
785 * Convert any successful completions to flush
786 * errors to avoid passing packets up the
787 * stack after bringing the device down.
789 if (priv->ibwc[i].status == IB_WC_SUCCESS)
790 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
792 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
793 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
794 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
796 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
798 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
800 } while (n == IPOIB_NUM_WC);
802 while (poll_tx(priv))
808 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
810 struct ipoib_dev_priv *priv = netdev_priv(dev);
811 struct ib_qp_attr qp_attr;
813 struct ipoib_tx_buf *tx_req;
816 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
817 napi_disable(&priv->napi);
819 ipoib_cm_dev_stop(dev);
822 * Move our QP to the error state and then reinitialize in
823 * when all work requests have completed or have been flushed.
825 qp_attr.qp_state = IB_QPS_ERR;
826 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
827 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
829 /* Wait for all sends and receives to complete */
832 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
833 if (time_after(jiffies, begin + 5 * HZ)) {
834 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
835 priv->tx_head - priv->tx_tail, recvs_pending(dev));
838 * assume the HW is wedged and just free up
839 * all our pending work requests.
841 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
842 tx_req = &priv->tx_ring[priv->tx_tail &
843 (ipoib_sendq_size - 1)];
844 ipoib_dma_unmap_tx(priv->ca, tx_req);
845 dev_kfree_skb_any(tx_req->skb);
847 --priv->tx_outstanding;
850 for (i = 0; i < ipoib_recvq_size; ++i) {
851 struct ipoib_rx_buf *rx_req;
853 rx_req = &priv->rx_ring[i];
856 ipoib_ud_dma_unmap_rx(priv,
857 priv->rx_ring[i].mapping);
858 dev_kfree_skb_any(rx_req->skb);
870 ipoib_dbg(priv, "All sends and receives done.\n");
873 del_timer_sync(&priv->poll_timer);
874 qp_attr.qp_state = IB_QPS_RESET;
875 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
876 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
878 /* Wait for all AHs to be reaped */
879 set_bit(IPOIB_STOP_REAPER, &priv->flags);
880 cancel_delayed_work(&priv->ah_reap_task);
882 flush_workqueue(ipoib_workqueue);
886 while (!list_empty(&priv->dead_ahs)) {
887 __ipoib_reap_ah(dev);
889 if (time_after(jiffies, begin + HZ)) {
890 ipoib_warn(priv, "timing out; will leak address handles\n");
897 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
902 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
904 struct ipoib_dev_priv *priv = netdev_priv(dev);
910 if (ipoib_transport_dev_init(dev, ca)) {
911 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
915 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
916 (unsigned long) dev);
918 if (dev->flags & IFF_UP) {
919 if (ipoib_ib_dev_open(dev)) {
920 ipoib_transport_dev_cleanup(dev);
928 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
929 enum ipoib_flush_level level)
931 struct ipoib_dev_priv *cpriv;
932 struct net_device *dev = priv->dev;
935 mutex_lock(&priv->vlan_mutex);
938 * Flush any child interfaces too -- they might be up even if
939 * the parent is down.
941 list_for_each_entry(cpriv, &priv->child_intfs, list)
942 __ipoib_ib_dev_flush(cpriv, level);
944 mutex_unlock(&priv->vlan_mutex);
946 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
947 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
951 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
952 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
956 if (level == IPOIB_FLUSH_HEAVY) {
957 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
958 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
959 ipoib_ib_dev_down(dev, 0);
960 ipoib_ib_dev_stop(dev, 0);
961 if (ipoib_pkey_dev_delay_open(dev))
965 /* restart QP only if P_Key index is changed */
966 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
967 new_index == priv->pkey_index) {
968 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
971 priv->pkey_index = new_index;
974 if (level == IPOIB_FLUSH_LIGHT) {
975 ipoib_mark_paths_invalid(dev);
976 ipoib_mcast_dev_flush(dev);
979 if (level >= IPOIB_FLUSH_NORMAL)
980 ipoib_ib_dev_down(dev, 0);
982 if (level == IPOIB_FLUSH_HEAVY) {
983 ipoib_ib_dev_stop(dev, 0);
984 ipoib_ib_dev_open(dev);
988 * The device could have been brought down between the start and when
989 * we get here, don't bring it back up if it's not configured up
991 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
992 if (level >= IPOIB_FLUSH_NORMAL)
993 ipoib_ib_dev_up(dev);
994 ipoib_mcast_restart_task(&priv->restart_task);
998 void ipoib_ib_dev_flush_light(struct work_struct *work)
1000 struct ipoib_dev_priv *priv =
1001 container_of(work, struct ipoib_dev_priv, flush_light);
1003 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
1006 void ipoib_ib_dev_flush_normal(struct work_struct *work)
1008 struct ipoib_dev_priv *priv =
1009 container_of(work, struct ipoib_dev_priv, flush_normal);
1011 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1014 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1016 struct ipoib_dev_priv *priv =
1017 container_of(work, struct ipoib_dev_priv, flush_heavy);
1019 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
1022 void ipoib_ib_dev_cleanup(struct net_device *dev)
1024 struct ipoib_dev_priv *priv = netdev_priv(dev);
1026 ipoib_dbg(priv, "cleaning up ib_dev\n");
1028 ipoib_mcast_stop_thread(dev, 1);
1029 ipoib_mcast_dev_flush(dev);
1031 ipoib_transport_dev_cleanup(dev);
1035 * Delayed P_Key Assigment Interim Support
1037 * The following is initial implementation of delayed P_Key assigment
1038 * mechanism. It is using the same approach implemented for the multicast
1039 * group join. The single goal of this implementation is to quickly address
1040 * Bug #2507. This implementation will probably be removed when the P_Key
1041 * change async notification is available.
1044 void ipoib_pkey_poll(struct work_struct *work)
1046 struct ipoib_dev_priv *priv =
1047 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
1048 struct net_device *dev = priv->dev;
1050 ipoib_pkey_dev_check_presence(dev);
1052 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1055 mutex_lock(&pkey_mutex);
1056 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1057 queue_delayed_work(ipoib_workqueue,
1058 &priv->pkey_poll_task,
1060 mutex_unlock(&pkey_mutex);
1064 int ipoib_pkey_dev_delay_open(struct net_device *dev)
1066 struct ipoib_dev_priv *priv = netdev_priv(dev);
1068 /* Look for the interface pkey value in the IB Port P_Key table and */
1069 /* set the interface pkey assigment flag */
1070 ipoib_pkey_dev_check_presence(dev);
1072 /* P_Key value not assigned yet - start polling */
1073 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
1074 mutex_lock(&pkey_mutex);
1075 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1076 queue_delayed_work(ipoib_workqueue,
1077 &priv->pkey_poll_task,
1079 mutex_unlock(&pkey_mutex);