Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
blob00435be4a44b00a395d2cf77a0154b99cd02b36c
1 /*
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
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
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
33 * SOFTWARE.
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/slab.h>
40 #include <linux/ip.h>
41 #include <linux/tcp.h>
43 #include "ipoib.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");
51 #endif
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)
58 struct ipoib_ah *ah;
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
61 if (!ah)
62 return NULL;
64 ah->dev = dev;
65 ah->last_send = 0;
66 kref_init(&ah->ref);
68 ah->ah = ib_create_ah(pd, attr);
69 if (IS_ERR(ah->ah)) {
70 kfree(ah);
71 ah = NULL;
72 } else
73 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
75 return 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);
83 unsigned long flags;
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,
95 DMA_FROM_DEVICE);
96 ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,
97 DMA_FROM_DEVICE);
98 } else
99 ib_dma_unmap_single(priv->ca, mapping[0],
100 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
101 DMA_FROM_DEVICE);
104 static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,
105 struct sk_buff *skb,
106 unsigned int length)
108 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
109 skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
110 unsigned int size;
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;
116 skb->len += length;
118 size = length - IPOIB_UD_HEAD_SIZE;
120 frag->size = size;
121 skb->data_len += size;
122 skb->truesize += size;
123 } else
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;
132 int ret;
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);
140 if (unlikely(ret)) {
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;
147 return ret;
150 static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
153 struct sk_buff *skb;
154 int buf_size;
155 u64 *mapping;
157 if (ipoib_ud_need_sg(priv->max_ib_mtu))
158 buf_size = IPOIB_UD_HEAD_SIZE;
159 else
160 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
162 skb = dev_alloc_skb(buf_size + 4);
163 if (unlikely(!skb))
164 return NULL;
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.
171 skb_reserve(skb, 4);
173 mapping = priv->rx_ring[id].mapping;
174 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
175 DMA_FROM_DEVICE);
176 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
177 goto error;
179 if (ipoib_ud_need_sg(priv->max_ib_mtu)) {
180 struct page *page = alloc_page(GFP_ATOMIC);
181 if (!page)
182 goto partial_error;
183 skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);
184 mapping[1] =
185 ib_dma_map_page(priv->ca, page,
186 0, PAGE_SIZE, DMA_FROM_DEVICE);
187 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))
188 goto partial_error;
191 priv->rx_ring[id].skb = skb;
192 return skb;
194 partial_error:
195 ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);
196 error:
197 dev_kfree_skb_any(skb);
198 return NULL;
201 static int ipoib_ib_post_receives(struct net_device *dev)
203 struct ipoib_dev_priv *priv = netdev_priv(dev);
204 int i;
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);
209 return -ENOMEM;
211 if (ipoib_ib_post_receive(dev, i)) {
212 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
213 return -EIO;
217 return 0;
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;
224 struct sk_buff *skb;
225 u64 mapping[IPOIB_UD_RX_SG];
226 union ib_gid *dgid;
228 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
229 wr_id, wc->status);
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);
234 return;
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;
247 return;
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)
255 goto repost;
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;
266 goto repost;
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;
282 else
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;
294 skb->dev = dev;
295 if ((dev->features & NETIF_F_RXCSUM) && likely(wc->csum_ok))
296 skb->ip_summed = CHECKSUM_UNNECESSARY;
298 napi_gro_receive(&priv->napi, skb);
300 repost:
301 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
302 ipoib_warn(priv, "ipoib_ib_post_receive failed "
303 "for buf %d\n", wr_id);
306 static int ipoib_dma_map_tx(struct ib_device *ca,
307 struct ipoib_tx_buf *tx_req)
309 struct sk_buff *skb = tx_req->skb;
310 u64 *mapping = tx_req->mapping;
311 int i;
312 int off;
314 if (skb_headlen(skb)) {
315 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
316 DMA_TO_DEVICE);
317 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
318 return -EIO;
320 off = 1;
321 } else
322 off = 0;
324 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
325 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
326 mapping[i + off] = ib_dma_map_page(ca,
327 skb_frag_page(frag),
328 frag->page_offset, frag->size,
329 DMA_TO_DEVICE);
330 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
331 goto partial_error;
333 return 0;
335 partial_error:
336 for (; i > 0; --i) {
337 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
338 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
341 if (off)
342 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
344 return -EIO;
347 static void ipoib_dma_unmap_tx(struct ib_device *ca,
348 struct ipoib_tx_buf *tx_req)
350 struct sk_buff *skb = tx_req->skb;
351 u64 *mapping = tx_req->mapping;
352 int i;
353 int off;
355 if (skb_headlen(skb)) {
356 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
357 off = 1;
358 } else
359 off = 0;
361 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
362 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
363 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
364 DMA_TO_DEVICE);
368 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
370 struct ipoib_dev_priv *priv = netdev_priv(dev);
371 unsigned int wr_id = wc->wr_id;
372 struct ipoib_tx_buf *tx_req;
374 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
375 wr_id, wc->status);
377 if (unlikely(wr_id >= ipoib_sendq_size)) {
378 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
379 wr_id, ipoib_sendq_size);
380 return;
383 tx_req = &priv->tx_ring[wr_id];
385 ipoib_dma_unmap_tx(priv->ca, tx_req);
387 ++dev->stats.tx_packets;
388 dev->stats.tx_bytes += tx_req->skb->len;
390 dev_kfree_skb_any(tx_req->skb);
392 ++priv->tx_tail;
393 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
394 netif_queue_stopped(dev) &&
395 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
396 netif_wake_queue(dev);
398 if (wc->status != IB_WC_SUCCESS &&
399 wc->status != IB_WC_WR_FLUSH_ERR)
400 ipoib_warn(priv, "failed send event "
401 "(status=%d, wrid=%d vend_err %x)\n",
402 wc->status, wr_id, wc->vendor_err);
405 static int poll_tx(struct ipoib_dev_priv *priv)
407 int n, i;
409 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
410 for (i = 0; i < n; ++i)
411 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
413 return n == MAX_SEND_CQE;
416 int ipoib_poll(struct napi_struct *napi, int budget)
418 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
419 struct net_device *dev = priv->dev;
420 int done;
421 int t;
422 int n, i;
424 done = 0;
426 poll_more:
427 while (done < budget) {
428 int max = (budget - done);
430 t = min(IPOIB_NUM_WC, max);
431 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
433 for (i = 0; i < n; i++) {
434 struct ib_wc *wc = priv->ibwc + i;
436 if (wc->wr_id & IPOIB_OP_RECV) {
437 ++done;
438 if (wc->wr_id & IPOIB_OP_CM)
439 ipoib_cm_handle_rx_wc(dev, wc);
440 else
441 ipoib_ib_handle_rx_wc(dev, wc);
442 } else
443 ipoib_cm_handle_tx_wc(priv->dev, wc);
446 if (n != t)
447 break;
450 if (done < budget) {
451 napi_complete(napi);
452 if (unlikely(ib_req_notify_cq(priv->recv_cq,
453 IB_CQ_NEXT_COMP |
454 IB_CQ_REPORT_MISSED_EVENTS)) &&
455 napi_reschedule(napi))
456 goto poll_more;
459 return done;
462 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
464 struct net_device *dev = dev_ptr;
465 struct ipoib_dev_priv *priv = netdev_priv(dev);
467 napi_schedule(&priv->napi);
470 static void drain_tx_cq(struct net_device *dev)
472 struct ipoib_dev_priv *priv = netdev_priv(dev);
474 netif_tx_lock(dev);
475 while (poll_tx(priv))
476 ; /* nothing */
478 if (netif_queue_stopped(dev))
479 mod_timer(&priv->poll_timer, jiffies + 1);
481 netif_tx_unlock(dev);
484 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
486 struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);
488 mod_timer(&priv->poll_timer, jiffies);
491 static inline int post_send(struct ipoib_dev_priv *priv,
492 unsigned int wr_id,
493 struct ib_ah *address, u32 qpn,
494 struct ipoib_tx_buf *tx_req,
495 void *head, int hlen)
497 struct ib_send_wr *bad_wr;
498 int i, off;
499 struct sk_buff *skb = tx_req->skb;
500 skb_frag_t *frags = skb_shinfo(skb)->frags;
501 int nr_frags = skb_shinfo(skb)->nr_frags;
502 u64 *mapping = tx_req->mapping;
504 if (skb_headlen(skb)) {
505 priv->tx_sge[0].addr = mapping[0];
506 priv->tx_sge[0].length = skb_headlen(skb);
507 off = 1;
508 } else
509 off = 0;
511 for (i = 0; i < nr_frags; ++i) {
512 priv->tx_sge[i + off].addr = mapping[i + off];
513 priv->tx_sge[i + off].length = frags[i].size;
515 priv->tx_wr.num_sge = nr_frags + off;
516 priv->tx_wr.wr_id = wr_id;
517 priv->tx_wr.wr.ud.remote_qpn = qpn;
518 priv->tx_wr.wr.ud.ah = address;
520 if (head) {
521 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
522 priv->tx_wr.wr.ud.header = head;
523 priv->tx_wr.wr.ud.hlen = hlen;
524 priv->tx_wr.opcode = IB_WR_LSO;
525 } else
526 priv->tx_wr.opcode = IB_WR_SEND;
528 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
531 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
532 struct ipoib_ah *address, u32 qpn)
534 struct ipoib_dev_priv *priv = netdev_priv(dev);
535 struct ipoib_tx_buf *tx_req;
536 int hlen, rc;
537 void *phead;
539 if (skb_is_gso(skb)) {
540 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
541 phead = skb->data;
542 if (unlikely(!skb_pull(skb, hlen))) {
543 ipoib_warn(priv, "linear data too small\n");
544 ++dev->stats.tx_dropped;
545 ++dev->stats.tx_errors;
546 dev_kfree_skb_any(skb);
547 return;
549 } else {
550 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
551 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
552 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
553 ++dev->stats.tx_dropped;
554 ++dev->stats.tx_errors;
555 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
556 return;
558 phead = NULL;
559 hlen = 0;
562 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
563 skb->len, address, qpn);
566 * We put the skb into the tx_ring _before_ we call post_send()
567 * because it's entirely possible that the completion handler will
568 * run before we execute anything after the post_send(). That
569 * means we have to make sure everything is properly recorded and
570 * our state is consistent before we call post_send().
572 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
573 tx_req->skb = skb;
574 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
575 ++dev->stats.tx_errors;
576 dev_kfree_skb_any(skb);
577 return;
580 if (skb->ip_summed == CHECKSUM_PARTIAL)
581 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
582 else
583 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
585 if (++priv->tx_outstanding == ipoib_sendq_size) {
586 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
587 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
588 ipoib_warn(priv, "request notify on send CQ failed\n");
589 netif_stop_queue(dev);
592 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
593 address->ah, qpn, tx_req, phead, hlen);
594 if (unlikely(rc)) {
595 ipoib_warn(priv, "post_send failed, error %d\n", rc);
596 ++dev->stats.tx_errors;
597 --priv->tx_outstanding;
598 ipoib_dma_unmap_tx(priv->ca, tx_req);
599 dev_kfree_skb_any(skb);
600 if (netif_queue_stopped(dev))
601 netif_wake_queue(dev);
602 } else {
603 dev->trans_start = jiffies;
605 address->last_send = priv->tx_head;
606 ++priv->tx_head;
607 skb_orphan(skb);
611 if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
612 while (poll_tx(priv))
613 ; /* nothing */
616 static void __ipoib_reap_ah(struct net_device *dev)
618 struct ipoib_dev_priv *priv = netdev_priv(dev);
619 struct ipoib_ah *ah, *tah;
620 LIST_HEAD(remove_list);
621 unsigned long flags;
623 netif_tx_lock_bh(dev);
624 spin_lock_irqsave(&priv->lock, flags);
626 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
627 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
628 list_del(&ah->list);
629 ib_destroy_ah(ah->ah);
630 kfree(ah);
633 spin_unlock_irqrestore(&priv->lock, flags);
634 netif_tx_unlock_bh(dev);
637 void ipoib_reap_ah(struct work_struct *work)
639 struct ipoib_dev_priv *priv =
640 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
641 struct net_device *dev = priv->dev;
643 __ipoib_reap_ah(dev);
645 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
646 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
647 round_jiffies_relative(HZ));
650 static void ipoib_ib_tx_timer_func(unsigned long ctx)
652 drain_tx_cq((struct net_device *)ctx);
655 int ipoib_ib_dev_open(struct net_device *dev)
657 struct ipoib_dev_priv *priv = netdev_priv(dev);
658 int ret;
660 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
661 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
662 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
663 return -1;
665 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
667 ret = ipoib_init_qp(dev);
668 if (ret) {
669 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
670 return -1;
673 ret = ipoib_ib_post_receives(dev);
674 if (ret) {
675 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
676 ipoib_ib_dev_stop(dev, 1);
677 return -1;
680 ret = ipoib_cm_dev_open(dev);
681 if (ret) {
682 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
683 ipoib_ib_dev_stop(dev, 1);
684 return -1;
687 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
688 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
689 round_jiffies_relative(HZ));
691 if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
692 napi_enable(&priv->napi);
694 return 0;
697 static void ipoib_pkey_dev_check_presence(struct net_device *dev)
699 struct ipoib_dev_priv *priv = netdev_priv(dev);
700 u16 pkey_index = 0;
702 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
703 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
704 else
705 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
708 int ipoib_ib_dev_up(struct net_device *dev)
710 struct ipoib_dev_priv *priv = netdev_priv(dev);
712 ipoib_pkey_dev_check_presence(dev);
714 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
715 ipoib_dbg(priv, "PKEY is not assigned.\n");
716 return 0;
719 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
721 return ipoib_mcast_start_thread(dev);
724 int ipoib_ib_dev_down(struct net_device *dev, int flush)
726 struct ipoib_dev_priv *priv = netdev_priv(dev);
728 ipoib_dbg(priv, "downing ib_dev\n");
730 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
731 netif_carrier_off(dev);
733 /* Shutdown the P_Key thread if still active */
734 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
735 mutex_lock(&pkey_mutex);
736 set_bit(IPOIB_PKEY_STOP, &priv->flags);
737 cancel_delayed_work(&priv->pkey_poll_task);
738 mutex_unlock(&pkey_mutex);
739 if (flush)
740 flush_workqueue(ipoib_workqueue);
743 ipoib_mcast_stop_thread(dev, flush);
744 ipoib_mcast_dev_flush(dev);
746 ipoib_flush_paths(dev);
748 return 0;
751 static int recvs_pending(struct net_device *dev)
753 struct ipoib_dev_priv *priv = netdev_priv(dev);
754 int pending = 0;
755 int i;
757 for (i = 0; i < ipoib_recvq_size; ++i)
758 if (priv->rx_ring[i].skb)
759 ++pending;
761 return pending;
764 void ipoib_drain_cq(struct net_device *dev)
766 struct ipoib_dev_priv *priv = netdev_priv(dev);
767 int i, n;
770 * We call completion handling routines that expect to be
771 * called from the BH-disabled NAPI poll context, so disable
772 * BHs here too.
774 local_bh_disable();
776 do {
777 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
778 for (i = 0; i < n; ++i) {
780 * Convert any successful completions to flush
781 * errors to avoid passing packets up the
782 * stack after bringing the device down.
784 if (priv->ibwc[i].status == IB_WC_SUCCESS)
785 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
787 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
788 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
789 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
790 else
791 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
792 } else
793 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
795 } while (n == IPOIB_NUM_WC);
797 while (poll_tx(priv))
798 ; /* nothing */
800 local_bh_enable();
803 int ipoib_ib_dev_stop(struct net_device *dev, int flush)
805 struct ipoib_dev_priv *priv = netdev_priv(dev);
806 struct ib_qp_attr qp_attr;
807 unsigned long begin;
808 struct ipoib_tx_buf *tx_req;
809 int i;
811 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
812 napi_disable(&priv->napi);
814 ipoib_cm_dev_stop(dev);
817 * Move our QP to the error state and then reinitialize in
818 * when all work requests have completed or have been flushed.
820 qp_attr.qp_state = IB_QPS_ERR;
821 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
822 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
824 /* Wait for all sends and receives to complete */
825 begin = jiffies;
827 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
828 if (time_after(jiffies, begin + 5 * HZ)) {
829 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
830 priv->tx_head - priv->tx_tail, recvs_pending(dev));
833 * assume the HW is wedged and just free up
834 * all our pending work requests.
836 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
837 tx_req = &priv->tx_ring[priv->tx_tail &
838 (ipoib_sendq_size - 1)];
839 ipoib_dma_unmap_tx(priv->ca, tx_req);
840 dev_kfree_skb_any(tx_req->skb);
841 ++priv->tx_tail;
842 --priv->tx_outstanding;
845 for (i = 0; i < ipoib_recvq_size; ++i) {
846 struct ipoib_rx_buf *rx_req;
848 rx_req = &priv->rx_ring[i];
849 if (!rx_req->skb)
850 continue;
851 ipoib_ud_dma_unmap_rx(priv,
852 priv->rx_ring[i].mapping);
853 dev_kfree_skb_any(rx_req->skb);
854 rx_req->skb = NULL;
857 goto timeout;
860 ipoib_drain_cq(dev);
862 msleep(1);
865 ipoib_dbg(priv, "All sends and receives done.\n");
867 timeout:
868 del_timer_sync(&priv->poll_timer);
869 qp_attr.qp_state = IB_QPS_RESET;
870 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
871 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
873 /* Wait for all AHs to be reaped */
874 set_bit(IPOIB_STOP_REAPER, &priv->flags);
875 cancel_delayed_work(&priv->ah_reap_task);
876 if (flush)
877 flush_workqueue(ipoib_workqueue);
879 begin = jiffies;
881 while (!list_empty(&priv->dead_ahs)) {
882 __ipoib_reap_ah(dev);
884 if (time_after(jiffies, begin + HZ)) {
885 ipoib_warn(priv, "timing out; will leak address handles\n");
886 break;
889 msleep(1);
892 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
894 return 0;
897 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
899 struct ipoib_dev_priv *priv = netdev_priv(dev);
901 priv->ca = ca;
902 priv->port = port;
903 priv->qp = NULL;
905 if (ipoib_transport_dev_init(dev, ca)) {
906 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
907 return -ENODEV;
910 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
911 (unsigned long) dev);
913 if (dev->flags & IFF_UP) {
914 if (ipoib_ib_dev_open(dev)) {
915 ipoib_transport_dev_cleanup(dev);
916 return -ENODEV;
920 return 0;
923 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
924 enum ipoib_flush_level level)
926 struct ipoib_dev_priv *cpriv;
927 struct net_device *dev = priv->dev;
928 u16 new_index;
930 mutex_lock(&priv->vlan_mutex);
933 * Flush any child interfaces too -- they might be up even if
934 * the parent is down.
936 list_for_each_entry(cpriv, &priv->child_intfs, list)
937 __ipoib_ib_dev_flush(cpriv, level);
939 mutex_unlock(&priv->vlan_mutex);
941 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
942 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
943 return;
946 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
947 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
948 return;
951 if (level == IPOIB_FLUSH_HEAVY) {
952 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
953 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
954 ipoib_ib_dev_down(dev, 0);
955 ipoib_ib_dev_stop(dev, 0);
956 if (ipoib_pkey_dev_delay_open(dev))
957 return;
960 /* restart QP only if P_Key index is changed */
961 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
962 new_index == priv->pkey_index) {
963 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
964 return;
966 priv->pkey_index = new_index;
969 if (level == IPOIB_FLUSH_LIGHT) {
970 ipoib_mark_paths_invalid(dev);
971 ipoib_mcast_dev_flush(dev);
974 if (level >= IPOIB_FLUSH_NORMAL)
975 ipoib_ib_dev_down(dev, 0);
977 if (level == IPOIB_FLUSH_HEAVY) {
978 ipoib_ib_dev_stop(dev, 0);
979 ipoib_ib_dev_open(dev);
983 * The device could have been brought down between the start and when
984 * we get here, don't bring it back up if it's not configured up
986 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
987 if (level >= IPOIB_FLUSH_NORMAL)
988 ipoib_ib_dev_up(dev);
989 ipoib_mcast_restart_task(&priv->restart_task);
993 void ipoib_ib_dev_flush_light(struct work_struct *work)
995 struct ipoib_dev_priv *priv =
996 container_of(work, struct ipoib_dev_priv, flush_light);
998 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
1001 void ipoib_ib_dev_flush_normal(struct work_struct *work)
1003 struct ipoib_dev_priv *priv =
1004 container_of(work, struct ipoib_dev_priv, flush_normal);
1006 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
1009 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1011 struct ipoib_dev_priv *priv =
1012 container_of(work, struct ipoib_dev_priv, flush_heavy);
1014 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
1017 void ipoib_ib_dev_cleanup(struct net_device *dev)
1019 struct ipoib_dev_priv *priv = netdev_priv(dev);
1021 ipoib_dbg(priv, "cleaning up ib_dev\n");
1023 ipoib_mcast_stop_thread(dev, 1);
1024 ipoib_mcast_dev_flush(dev);
1026 ipoib_transport_dev_cleanup(dev);
1030 * Delayed P_Key Assigment Interim Support
1032 * The following is initial implementation of delayed P_Key assigment
1033 * mechanism. It is using the same approach implemented for the multicast
1034 * group join. The single goal of this implementation is to quickly address
1035 * Bug #2507. This implementation will probably be removed when the P_Key
1036 * change async notification is available.
1039 void ipoib_pkey_poll(struct work_struct *work)
1041 struct ipoib_dev_priv *priv =
1042 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
1043 struct net_device *dev = priv->dev;
1045 ipoib_pkey_dev_check_presence(dev);
1047 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1048 ipoib_open(dev);
1049 else {
1050 mutex_lock(&pkey_mutex);
1051 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1052 queue_delayed_work(ipoib_workqueue,
1053 &priv->pkey_poll_task,
1054 HZ);
1055 mutex_unlock(&pkey_mutex);
1059 int ipoib_pkey_dev_delay_open(struct net_device *dev)
1061 struct ipoib_dev_priv *priv = netdev_priv(dev);
1063 /* Look for the interface pkey value in the IB Port P_Key table and */
1064 /* set the interface pkey assigment flag */
1065 ipoib_pkey_dev_check_presence(dev);
1067 /* P_Key value not assigned yet - start polling */
1068 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
1069 mutex_lock(&pkey_mutex);
1070 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1071 queue_delayed_work(ipoib_workqueue,
1072 &priv->pkey_poll_task,
1073 HZ);
1074 mutex_unlock(&pkey_mutex);
1075 return 1;
1078 return 0;