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
35 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
38 #include <linux/delay.h>
39 #include <linux/dma-mapping.h>
41 #include <rdma/ib_cache.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 int ipoib_ib_post_receive(struct net_device *dev, int id)
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
94 struct ib_recv_wr param;
95 struct ib_recv_wr *bad_wr;
98 list.addr = priv->rx_ring[id].mapping;
99 list.length = IPOIB_BUF_SIZE;
100 list.lkey = priv->mr->lkey;
103 param.wr_id = id | IPOIB_OP_RECV;
104 param.sg_list = &list;
107 ret = ib_post_recv(priv->qp, ¶m, &bad_wr);
109 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
110 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
111 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
112 dev_kfree_skb_any(priv->rx_ring[id].skb);
113 priv->rx_ring[id].skb = NULL;
119 static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
121 struct ipoib_dev_priv *priv = netdev_priv(dev);
125 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
130 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
131 * header. So we need 4 more bytes to get to 48 and align the
132 * IP header to a multiple of 16.
136 addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
138 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
139 dev_kfree_skb_any(skb);
143 priv->rx_ring[id].skb = skb;
144 priv->rx_ring[id].mapping = addr;
149 static int ipoib_ib_post_receives(struct net_device *dev)
151 struct ipoib_dev_priv *priv = netdev_priv(dev);
154 for (i = 0; i < ipoib_recvq_size; ++i) {
155 if (ipoib_alloc_rx_skb(dev, i)) {
156 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
159 if (ipoib_ib_post_receive(dev, i)) {
160 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
168 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
170 struct ipoib_dev_priv *priv = netdev_priv(dev);
171 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
175 ipoib_dbg_data(priv, "recv completion: id %d, op %d, status: %d\n",
176 wr_id, wc->opcode, wc->status);
178 if (unlikely(wr_id >= ipoib_recvq_size)) {
179 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
180 wr_id, ipoib_recvq_size);
184 skb = priv->rx_ring[wr_id].skb;
185 addr = priv->rx_ring[wr_id].mapping;
187 if (unlikely(wc->status != IB_WC_SUCCESS)) {
188 if (wc->status != IB_WC_WR_FLUSH_ERR)
189 ipoib_warn(priv, "failed recv event "
190 "(status=%d, wrid=%d vend_err %x)\n",
191 wc->status, wr_id, wc->vendor_err);
192 ib_dma_unmap_single(priv->ca, addr,
193 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
194 dev_kfree_skb_any(skb);
195 priv->rx_ring[wr_id].skb = NULL;
200 * If we can't allocate a new RX buffer, dump
201 * this packet and reuse the old buffer.
203 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
204 ++priv->stats.rx_dropped;
208 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
209 wc->byte_len, wc->slid);
211 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
213 skb_put(skb, wc->byte_len);
214 skb_pull(skb, IB_GRH_BYTES);
216 if (wc->slid != priv->local_lid ||
217 wc->src_qp != priv->qp->qp_num) {
218 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
219 skb->mac.raw = skb->data;
220 skb_pull(skb, IPOIB_ENCAP_LEN);
222 dev->last_rx = jiffies;
223 ++priv->stats.rx_packets;
224 priv->stats.rx_bytes += skb->len;
227 /* XXX get correct PACKET_ type here */
228 skb->pkt_type = PACKET_HOST;
231 ipoib_dbg_data(priv, "dropping loopback packet\n");
232 dev_kfree_skb_any(skb);
236 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
237 ipoib_warn(priv, "ipoib_ib_post_receive failed "
238 "for buf %d\n", wr_id);
241 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
243 struct ipoib_dev_priv *priv = netdev_priv(dev);
244 unsigned int wr_id = wc->wr_id;
245 struct ipoib_tx_buf *tx_req;
248 ipoib_dbg_data(priv, "send completion: id %d, op %d, status: %d\n",
249 wr_id, wc->opcode, wc->status);
251 if (unlikely(wr_id >= ipoib_sendq_size)) {
252 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
253 wr_id, ipoib_sendq_size);
257 tx_req = &priv->tx_ring[wr_id];
259 ib_dma_unmap_single(priv->ca, tx_req->mapping,
260 tx_req->skb->len, DMA_TO_DEVICE);
262 ++priv->stats.tx_packets;
263 priv->stats.tx_bytes += tx_req->skb->len;
265 dev_kfree_skb_any(tx_req->skb);
267 spin_lock_irqsave(&priv->tx_lock, flags);
269 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
270 priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
271 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
272 netif_wake_queue(dev);
274 spin_unlock_irqrestore(&priv->tx_lock, flags);
276 if (wc->status != IB_WC_SUCCESS &&
277 wc->status != IB_WC_WR_FLUSH_ERR)
278 ipoib_warn(priv, "failed send event "
279 "(status=%d, wrid=%d vend_err %x)\n",
280 wc->status, wr_id, wc->vendor_err);
283 static void ipoib_ib_handle_wc(struct net_device *dev, struct ib_wc *wc)
285 if (wc->wr_id & IPOIB_CM_OP_SRQ)
286 ipoib_cm_handle_rx_wc(dev, wc);
287 else if (wc->wr_id & IPOIB_OP_RECV)
288 ipoib_ib_handle_rx_wc(dev, wc);
290 ipoib_ib_handle_tx_wc(dev, wc);
293 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
295 struct net_device *dev = (struct net_device *) dev_ptr;
296 struct ipoib_dev_priv *priv = netdev_priv(dev);
299 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
301 n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
302 for (i = 0; i < n; ++i)
303 ipoib_ib_handle_wc(dev, priv->ibwc + i);
304 } while (n == IPOIB_NUM_WC);
307 static inline int post_send(struct ipoib_dev_priv *priv,
309 struct ib_ah *address, u32 qpn,
312 struct ib_send_wr *bad_wr;
314 priv->tx_sge.addr = addr;
315 priv->tx_sge.length = len;
317 priv->tx_wr.wr_id = wr_id;
318 priv->tx_wr.wr.ud.remote_qpn = qpn;
319 priv->tx_wr.wr.ud.ah = address;
321 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
324 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
325 struct ipoib_ah *address, u32 qpn)
327 struct ipoib_dev_priv *priv = netdev_priv(dev);
328 struct ipoib_tx_buf *tx_req;
331 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
332 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
333 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
334 ++priv->stats.tx_dropped;
335 ++priv->stats.tx_errors;
336 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
340 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
341 skb->len, address, qpn);
344 * We put the skb into the tx_ring _before_ we call post_send()
345 * because it's entirely possible that the completion handler will
346 * run before we execute anything after the post_send(). That
347 * means we have to make sure everything is properly recorded and
348 * our state is consistent before we call post_send().
350 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
352 addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
354 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
355 ++priv->stats.tx_errors;
356 dev_kfree_skb_any(skb);
359 tx_req->mapping = addr;
361 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
362 address->ah, qpn, addr, skb->len))) {
363 ipoib_warn(priv, "post_send failed\n");
364 ++priv->stats.tx_errors;
365 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
366 dev_kfree_skb_any(skb);
368 dev->trans_start = jiffies;
370 address->last_send = priv->tx_head;
373 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
374 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
375 netif_stop_queue(dev);
376 set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
381 static void __ipoib_reap_ah(struct net_device *dev)
383 struct ipoib_dev_priv *priv = netdev_priv(dev);
384 struct ipoib_ah *ah, *tah;
385 LIST_HEAD(remove_list);
387 spin_lock_irq(&priv->tx_lock);
388 spin_lock(&priv->lock);
389 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
390 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
392 ib_destroy_ah(ah->ah);
395 spin_unlock(&priv->lock);
396 spin_unlock_irq(&priv->tx_lock);
399 void ipoib_reap_ah(struct work_struct *work)
401 struct ipoib_dev_priv *priv =
402 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
403 struct net_device *dev = priv->dev;
405 __ipoib_reap_ah(dev);
407 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
408 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
411 int ipoib_ib_dev_open(struct net_device *dev)
413 struct ipoib_dev_priv *priv = netdev_priv(dev);
416 ret = ipoib_init_qp(dev);
418 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
422 ret = ipoib_ib_post_receives(dev);
424 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
425 ipoib_ib_dev_stop(dev);
429 ret = ipoib_cm_dev_open(dev);
431 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
432 ipoib_ib_dev_stop(dev);
436 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
437 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
439 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
444 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
446 struct ipoib_dev_priv *priv = netdev_priv(dev);
449 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
450 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
452 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
455 int ipoib_ib_dev_up(struct net_device *dev)
457 struct ipoib_dev_priv *priv = netdev_priv(dev);
459 ipoib_pkey_dev_check_presence(dev);
461 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
462 ipoib_dbg(priv, "PKEY is not assigned.\n");
466 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
468 return ipoib_mcast_start_thread(dev);
471 int ipoib_ib_dev_down(struct net_device *dev, int flush)
473 struct ipoib_dev_priv *priv = netdev_priv(dev);
475 ipoib_dbg(priv, "downing ib_dev\n");
477 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
478 netif_carrier_off(dev);
480 /* Shutdown the P_Key thread if still active */
481 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
482 mutex_lock(&pkey_mutex);
483 set_bit(IPOIB_PKEY_STOP, &priv->flags);
484 cancel_delayed_work(&priv->pkey_task);
485 mutex_unlock(&pkey_mutex);
487 flush_workqueue(ipoib_workqueue);
490 ipoib_mcast_stop_thread(dev, flush);
491 ipoib_mcast_dev_flush(dev);
493 ipoib_flush_paths(dev);
498 static int recvs_pending(struct net_device *dev)
500 struct ipoib_dev_priv *priv = netdev_priv(dev);
504 for (i = 0; i < ipoib_recvq_size; ++i)
505 if (priv->rx_ring[i].skb)
511 int ipoib_ib_dev_stop(struct net_device *dev)
513 struct ipoib_dev_priv *priv = netdev_priv(dev);
514 struct ib_qp_attr qp_attr;
516 struct ipoib_tx_buf *tx_req;
519 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
521 ipoib_cm_dev_stop(dev);
524 * Move our QP to the error state and then reinitialize in
525 * when all work requests have completed or have been flushed.
527 qp_attr.qp_state = IB_QPS_ERR;
528 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
529 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
531 /* Wait for all sends and receives to complete */
534 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
535 if (time_after(jiffies, begin + 5 * HZ)) {
536 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
537 priv->tx_head - priv->tx_tail, recvs_pending(dev));
540 * assume the HW is wedged and just free up
541 * all our pending work requests.
543 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
544 tx_req = &priv->tx_ring[priv->tx_tail &
545 (ipoib_sendq_size - 1)];
546 ib_dma_unmap_single(priv->ca,
550 dev_kfree_skb_any(tx_req->skb);
554 for (i = 0; i < ipoib_recvq_size; ++i) {
555 struct ipoib_rx_buf *rx_req;
557 rx_req = &priv->rx_ring[i];
560 ib_dma_unmap_single(priv->ca,
564 dev_kfree_skb_any(rx_req->skb);
574 ipoib_dbg(priv, "All sends and receives done.\n");
577 qp_attr.qp_state = IB_QPS_RESET;
578 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
579 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
581 /* Wait for all AHs to be reaped */
582 set_bit(IPOIB_STOP_REAPER, &priv->flags);
583 cancel_delayed_work(&priv->ah_reap_task);
584 flush_workqueue(ipoib_workqueue);
588 while (!list_empty(&priv->dead_ahs)) {
589 __ipoib_reap_ah(dev);
591 if (time_after(jiffies, begin + HZ)) {
592 ipoib_warn(priv, "timing out; will leak address handles\n");
602 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
604 struct ipoib_dev_priv *priv = netdev_priv(dev);
610 if (ipoib_transport_dev_init(dev, ca)) {
611 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
615 if (dev->flags & IFF_UP) {
616 if (ipoib_ib_dev_open(dev)) {
617 ipoib_transport_dev_cleanup(dev);
625 void ipoib_ib_dev_flush(struct work_struct *work)
627 struct ipoib_dev_priv *cpriv, *priv =
628 container_of(work, struct ipoib_dev_priv, flush_task);
629 struct net_device *dev = priv->dev;
631 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
632 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
636 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
637 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
641 ipoib_dbg(priv, "flushing\n");
643 ipoib_ib_dev_down(dev, 0);
646 * The device could have been brought down between the start and when
647 * we get here, don't bring it back up if it's not configured up
649 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
650 ipoib_ib_dev_up(dev);
651 ipoib_mcast_restart_task(&priv->restart_task);
654 mutex_lock(&priv->vlan_mutex);
656 /* Flush any child interfaces too */
657 list_for_each_entry(cpriv, &priv->child_intfs, list)
658 ipoib_ib_dev_flush(&cpriv->flush_task);
660 mutex_unlock(&priv->vlan_mutex);
663 void ipoib_ib_dev_cleanup(struct net_device *dev)
665 struct ipoib_dev_priv *priv = netdev_priv(dev);
667 ipoib_dbg(priv, "cleaning up ib_dev\n");
669 ipoib_mcast_stop_thread(dev, 1);
670 ipoib_mcast_dev_flush(dev);
672 ipoib_transport_dev_cleanup(dev);
676 * Delayed P_Key Assigment Interim Support
678 * The following is initial implementation of delayed P_Key assigment
679 * mechanism. It is using the same approach implemented for the multicast
680 * group join. The single goal of this implementation is to quickly address
681 * Bug #2507. This implementation will probably be removed when the P_Key
682 * change async notification is available.
685 void ipoib_pkey_poll(struct work_struct *work)
687 struct ipoib_dev_priv *priv =
688 container_of(work, struct ipoib_dev_priv, pkey_task.work);
689 struct net_device *dev = priv->dev;
691 ipoib_pkey_dev_check_presence(dev);
693 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
696 mutex_lock(&pkey_mutex);
697 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
698 queue_delayed_work(ipoib_workqueue,
701 mutex_unlock(&pkey_mutex);
705 int ipoib_pkey_dev_delay_open(struct net_device *dev)
707 struct ipoib_dev_priv *priv = netdev_priv(dev);
709 /* Look for the interface pkey value in the IB Port P_Key table and */
710 /* set the interface pkey assigment flag */
711 ipoib_pkey_dev_check_presence(dev);
713 /* P_Key value not assigned yet - start polling */
714 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
715 mutex_lock(&pkey_mutex);
716 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
717 queue_delayed_work(ipoib_workqueue,
720 mutex_unlock(&pkey_mutex);