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 #define IPOIB_OP_RECV (1ul << 31)
55 static DECLARE_MUTEX(pkey_sem);
57 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
58 struct ib_pd *pd, struct ib_ah_attr *attr)
62 ah = kmalloc(sizeof *ah, GFP_KERNEL);
70 ah->ah = ib_create_ah(pd, attr);
75 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
80 void ipoib_free_ah(struct kref *kref)
82 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
83 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
87 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
88 ipoib_dbg(priv, "Freeing ah %p\n", ah->ah);
89 ib_destroy_ah(ah->ah);
92 spin_lock_irqsave(&priv->lock, flags);
93 list_add_tail(&ah->list, &priv->dead_ahs);
94 spin_unlock_irqrestore(&priv->lock, flags);
98 static inline int ipoib_ib_receive(struct ipoib_dev_priv *priv,
102 struct ib_sge list = {
104 .length = IPOIB_BUF_SIZE,
105 .lkey = priv->mr->lkey,
107 struct ib_recv_wr param = {
108 .wr_id = wr_id | IPOIB_OP_RECV,
112 struct ib_recv_wr *bad_wr;
114 return ib_post_recv(priv->qp, ¶m, &bad_wr);
117 static int ipoib_ib_post_receive(struct net_device *dev, int id)
119 struct ipoib_dev_priv *priv = netdev_priv(dev);
124 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
126 ipoib_warn(priv, "failed to allocate receive buffer\n");
128 priv->rx_ring[id].skb = NULL;
131 skb_reserve(skb, 4); /* 16 byte align IP header */
132 priv->rx_ring[id].skb = skb;
133 addr = dma_map_single(priv->ca->dma_device,
134 skb->data, IPOIB_BUF_SIZE,
136 pci_unmap_addr_set(&priv->rx_ring[id], mapping, addr);
138 ret = ipoib_ib_receive(priv, id, addr);
140 ipoib_warn(priv, "ipoib_ib_receive failed for buf %d (%d)\n",
142 dma_unmap_single(priv->ca->dma_device, addr,
143 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
144 dev_kfree_skb_any(skb);
145 priv->rx_ring[id].skb = NULL;
151 static int ipoib_ib_post_receives(struct net_device *dev)
153 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) {
157 if (ipoib_ib_post_receive(dev, i)) {
158 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
166 static void ipoib_ib_handle_wc(struct net_device *dev,
169 struct ipoib_dev_priv *priv = netdev_priv(dev);
170 unsigned int wr_id = wc->wr_id;
172 ipoib_dbg_data(priv, "called: id %d, op %d, status: %d\n",
173 wr_id, wc->opcode, wc->status);
175 if (wr_id & IPOIB_OP_RECV) {
176 wr_id &= ~IPOIB_OP_RECV;
178 if (wr_id < IPOIB_RX_RING_SIZE) {
179 struct sk_buff *skb = priv->rx_ring[wr_id].skb;
181 priv->rx_ring[wr_id].skb = NULL;
183 dma_unmap_single(priv->ca->dma_device,
184 pci_unmap_addr(&priv->rx_ring[wr_id],
189 if (wc->status != IB_WC_SUCCESS) {
190 if (wc->status != IB_WC_WR_FLUSH_ERR)
191 ipoib_warn(priv, "failed recv event "
192 "(status=%d, wrid=%d vend_err %x)\n",
193 wc->status, wr_id, wc->vendor_err);
194 dev_kfree_skb_any(skb);
198 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
199 wc->byte_len, wc->slid);
201 skb_put(skb, wc->byte_len);
202 skb_pull(skb, IB_GRH_BYTES);
204 if (wc->slid != priv->local_lid ||
205 wc->src_qp != priv->qp->qp_num) {
206 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
207 skb->mac.raw = skb->data;
208 skb_pull(skb, IPOIB_ENCAP_LEN);
210 dev->last_rx = jiffies;
211 ++priv->stats.rx_packets;
212 priv->stats.rx_bytes += skb->len;
215 /* XXX get correct PACKET_ type here */
216 skb->pkt_type = PACKET_HOST;
219 ipoib_dbg_data(priv, "dropping loopback packet\n");
220 dev_kfree_skb_any(skb);
224 if (ipoib_ib_post_receive(dev, wr_id))
225 ipoib_warn(priv, "ipoib_ib_post_receive failed "
226 "for buf %d\n", wr_id);
228 ipoib_warn(priv, "completion event with wrid %d\n",
232 struct ipoib_buf *tx_req;
235 if (wr_id >= IPOIB_TX_RING_SIZE) {
236 ipoib_warn(priv, "completion event with wrid %d (> %d)\n",
237 wr_id, IPOIB_TX_RING_SIZE);
241 ipoib_dbg_data(priv, "send complete, wrid %d\n", wr_id);
243 tx_req = &priv->tx_ring[wr_id];
245 dma_unmap_single(priv->ca->dma_device,
246 pci_unmap_addr(tx_req, mapping),
250 ++priv->stats.tx_packets;
251 priv->stats.tx_bytes += tx_req->skb->len;
253 dev_kfree_skb_any(tx_req->skb);
255 spin_lock_irqsave(&priv->tx_lock, flags);
257 if (netif_queue_stopped(dev) &&
258 priv->tx_head - priv->tx_tail <= IPOIB_TX_RING_SIZE / 2)
259 netif_wake_queue(dev);
260 spin_unlock_irqrestore(&priv->tx_lock, flags);
262 if (wc->status != IB_WC_SUCCESS &&
263 wc->status != IB_WC_WR_FLUSH_ERR)
264 ipoib_warn(priv, "failed send event "
265 "(status=%d, wrid=%d vend_err %x)\n",
266 wc->status, wr_id, wc->vendor_err);
270 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
272 struct net_device *dev = (struct net_device *) dev_ptr;
273 struct ipoib_dev_priv *priv = netdev_priv(dev);
276 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
278 n = ib_poll_cq(cq, IPOIB_NUM_WC, priv->ibwc);
279 for (i = 0; i < n; ++i)
280 ipoib_ib_handle_wc(dev, priv->ibwc + i);
281 } while (n == IPOIB_NUM_WC);
284 static inline int post_send(struct ipoib_dev_priv *priv,
286 struct ib_ah *address, u32 qpn,
287 dma_addr_t addr, int len)
289 struct ib_send_wr *bad_wr;
291 priv->tx_sge.addr = addr;
292 priv->tx_sge.length = len;
294 priv->tx_wr.wr_id = wr_id;
295 priv->tx_wr.wr.ud.remote_qpn = qpn;
296 priv->tx_wr.wr.ud.ah = address;
298 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
301 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
302 struct ipoib_ah *address, u32 qpn)
304 struct ipoib_dev_priv *priv = netdev_priv(dev);
305 struct ipoib_buf *tx_req;
308 if (skb->len > dev->mtu + INFINIBAND_ALEN) {
309 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
310 skb->len, dev->mtu + INFINIBAND_ALEN);
311 ++priv->stats.tx_dropped;
312 ++priv->stats.tx_errors;
313 dev_kfree_skb_any(skb);
317 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
318 skb->len, address, qpn);
321 * We put the skb into the tx_ring _before_ we call post_send()
322 * because it's entirely possible that the completion handler will
323 * run before we execute anything after the post_send(). That
324 * means we have to make sure everything is properly recorded and
325 * our state is consistent before we call post_send().
327 tx_req = &priv->tx_ring[priv->tx_head & (IPOIB_TX_RING_SIZE - 1)];
329 addr = dma_map_single(priv->ca->dma_device, skb->data, skb->len,
331 pci_unmap_addr_set(tx_req, mapping, addr);
333 if (unlikely(post_send(priv, priv->tx_head & (IPOIB_TX_RING_SIZE - 1),
334 address->ah, qpn, addr, skb->len))) {
335 ipoib_warn(priv, "post_send failed\n");
336 ++priv->stats.tx_errors;
337 dma_unmap_single(priv->ca->dma_device, addr, skb->len,
339 dev_kfree_skb_any(skb);
341 dev->trans_start = jiffies;
343 address->last_send = priv->tx_head;
346 if (priv->tx_head - priv->tx_tail == IPOIB_TX_RING_SIZE) {
347 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
348 netif_stop_queue(dev);
353 static void __ipoib_reap_ah(struct net_device *dev)
355 struct ipoib_dev_priv *priv = netdev_priv(dev);
356 struct ipoib_ah *ah, *tah;
357 LIST_HEAD(remove_list);
359 spin_lock_irq(&priv->lock);
360 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
361 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
363 list_add_tail(&ah->list, &remove_list);
365 spin_unlock_irq(&priv->lock);
367 list_for_each_entry_safe(ah, tah, &remove_list, list) {
368 ipoib_dbg(priv, "Reaping ah %p\n", ah->ah);
369 ib_destroy_ah(ah->ah);
374 void ipoib_reap_ah(void *dev_ptr)
376 struct net_device *dev = dev_ptr;
377 struct ipoib_dev_priv *priv = netdev_priv(dev);
379 __ipoib_reap_ah(dev);
381 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
382 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
385 int ipoib_ib_dev_open(struct net_device *dev)
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
390 ret = ipoib_qp_create(dev);
392 ipoib_warn(priv, "ipoib_qp_create returned %d\n", ret);
396 ret = ipoib_ib_post_receives(dev);
398 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
402 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
403 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
408 int ipoib_ib_dev_up(struct net_device *dev)
410 struct ipoib_dev_priv *priv = netdev_priv(dev);
412 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
414 return ipoib_mcast_start_thread(dev);
417 int ipoib_ib_dev_down(struct net_device *dev)
419 struct ipoib_dev_priv *priv = netdev_priv(dev);
421 ipoib_dbg(priv, "downing ib_dev\n");
423 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
424 netif_carrier_off(dev);
426 /* Shutdown the P_Key thread if still active */
427 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
429 set_bit(IPOIB_PKEY_STOP, &priv->flags);
430 cancel_delayed_work(&priv->pkey_task);
432 flush_workqueue(ipoib_workqueue);
435 ipoib_mcast_stop_thread(dev);
438 * Flush the multicast groups first so we stop any multicast joins. The
439 * completion thread may have already died and we may deadlock waiting
440 * for the completion thread to finish some multicast joins.
442 ipoib_mcast_dev_flush(dev);
444 /* Delete broadcast and local addresses since they will be recreated */
445 ipoib_mcast_dev_down(dev);
447 ipoib_flush_paths(dev);
452 static int recvs_pending(struct net_device *dev)
454 struct ipoib_dev_priv *priv = netdev_priv(dev);
458 for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
459 if (priv->rx_ring[i].skb)
465 int ipoib_ib_dev_stop(struct net_device *dev)
467 struct ipoib_dev_priv *priv = netdev_priv(dev);
468 struct ib_qp_attr qp_attr;
471 struct ipoib_buf *tx_req;
474 /* Kill the existing QP and allocate a new one */
475 qp_attr.qp_state = IB_QPS_ERR;
476 attr_mask = IB_QP_STATE;
477 if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
478 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
480 /* Wait for all sends and receives to complete */
483 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
484 if (time_after(jiffies, begin + 5 * HZ)) {
485 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
486 priv->tx_head - priv->tx_tail, recvs_pending(dev));
489 * assume the HW is wedged and just free up
490 * all our pending work requests.
492 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
493 tx_req = &priv->tx_ring[priv->tx_tail &
494 (IPOIB_TX_RING_SIZE - 1)];
495 dma_unmap_single(priv->ca->dma_device,
496 pci_unmap_addr(tx_req, mapping),
499 dev_kfree_skb_any(tx_req->skb);
503 for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
504 if (priv->rx_ring[i].skb) {
505 dma_unmap_single(priv->ca->dma_device,
506 pci_unmap_addr(&priv->rx_ring[i],
510 dev_kfree_skb_any(priv->rx_ring[i].skb);
511 priv->rx_ring[i].skb = NULL;
520 ipoib_dbg(priv, "All sends and receives done.\n");
523 qp_attr.qp_state = IB_QPS_RESET;
524 attr_mask = IB_QP_STATE;
525 if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
526 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
528 /* Wait for all AHs to be reaped */
529 set_bit(IPOIB_STOP_REAPER, &priv->flags);
530 cancel_delayed_work(&priv->ah_reap_task);
531 flush_workqueue(ipoib_workqueue);
535 while (!list_empty(&priv->dead_ahs)) {
536 __ipoib_reap_ah(dev);
538 if (time_after(jiffies, begin + HZ)) {
539 ipoib_warn(priv, "timing out; will leak address handles\n");
549 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
551 struct ipoib_dev_priv *priv = netdev_priv(dev);
557 if (ipoib_transport_dev_init(dev, ca)) {
558 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
562 if (dev->flags & IFF_UP) {
563 if (ipoib_ib_dev_open(dev)) {
564 ipoib_transport_dev_cleanup(dev);
572 void ipoib_ib_dev_flush(void *_dev)
574 struct net_device *dev = (struct net_device *)_dev;
575 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv;
577 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
580 ipoib_dbg(priv, "flushing\n");
582 ipoib_ib_dev_down(dev);
585 * The device could have been brought down between the start and when
586 * we get here, don't bring it back up if it's not configured up
588 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
589 ipoib_ib_dev_up(dev);
591 /* Flush any child interfaces too */
592 list_for_each_entry(cpriv, &priv->child_intfs, list)
593 ipoib_ib_dev_flush(&cpriv->dev);
596 void ipoib_ib_dev_cleanup(struct net_device *dev)
598 struct ipoib_dev_priv *priv = netdev_priv(dev);
600 ipoib_dbg(priv, "cleaning up ib_dev\n");
602 ipoib_mcast_stop_thread(dev);
604 /* Delete the broadcast address and the local address */
605 ipoib_mcast_dev_down(dev);
607 ipoib_transport_dev_cleanup(dev);
611 * Delayed P_Key Assigment Interim Support
613 * The following is initial implementation of delayed P_Key assigment
614 * mechanism. It is using the same approach implemented for the multicast
615 * group join. The single goal of this implementation is to quickly address
616 * Bug #2507. This implementation will probably be removed when the P_Key
617 * change async notification is available.
619 int ipoib_open(struct net_device *dev);
621 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
626 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
627 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
629 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
632 void ipoib_pkey_poll(void *dev_ptr)
634 struct net_device *dev = dev_ptr;
635 struct ipoib_dev_priv *priv = netdev_priv(dev);
637 ipoib_pkey_dev_check_presence(dev);
639 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
643 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
644 queue_delayed_work(ipoib_workqueue,
651 int ipoib_pkey_dev_delay_open(struct net_device *dev)
653 struct ipoib_dev_priv *priv = netdev_priv(dev);
655 /* Look for the interface pkey value in the IB Port P_Key table and */
656 /* set the interface pkey assigment flag */
657 ipoib_pkey_dev_check_presence(dev);
659 /* P_Key value not assigned yet - start polling */
660 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
662 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
663 queue_delayed_work(ipoib_workqueue,