IPoIB: Add LSO support
[linux-2.6-block.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
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.
1da177e4
LT
6 *
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:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
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.
25 *
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.
34 *
35 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
36 */
37
38#include <linux/delay.h>
39#include <linux/dma-mapping.h>
40
a4d61e84 41#include <rdma/ib_cache.h>
40ca1988
EC
42#include <linux/ip.h>
43#include <linux/tcp.h>
1da177e4
LT
44
45#include "ipoib.h"
46
47#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
48static int data_debug_level;
49
50module_param(data_debug_level, int, 0644);
51MODULE_PARM_DESC(data_debug_level,
52 "Enable data path debug tracing if > 0");
53#endif
54
95ed644f 55static DEFINE_MUTEX(pkey_mutex);
1da177e4
LT
56
57struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
58 struct ib_pd *pd, struct ib_ah_attr *attr)
59{
60 struct ipoib_ah *ah;
61
62 ah = kmalloc(sizeof *ah, GFP_KERNEL);
63 if (!ah)
64 return NULL;
65
66 ah->dev = dev;
67 ah->last_send = 0;
68 kref_init(&ah->ref);
69
70 ah->ah = ib_create_ah(pd, attr);
71 if (IS_ERR(ah->ah)) {
72 kfree(ah);
73 ah = NULL;
74 } else
75 ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);
76
77 return ah;
78}
79
80void ipoib_free_ah(struct kref *kref)
81{
82 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
83 struct ipoib_dev_priv *priv = netdev_priv(ah->dev);
84
85 unsigned long flags;
86
31c02e21
RD
87 spin_lock_irqsave(&priv->lock, flags);
88 list_add_tail(&ah->list, &priv->dead_ahs);
89 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
90}
91
1993d683 92static int ipoib_ib_post_receive(struct net_device *dev, int id)
1da177e4 93{
1993d683
RD
94 struct ipoib_dev_priv *priv = netdev_priv(dev);
95 struct ib_sge list;
96 struct ib_recv_wr param;
1da177e4 97 struct ib_recv_wr *bad_wr;
1993d683
RD
98 int ret;
99
100 list.addr = priv->rx_ring[id].mapping;
101 list.length = IPOIB_BUF_SIZE;
102 list.lkey = priv->mr->lkey;
103
104 param.next = NULL;
105 param.wr_id = id | IPOIB_OP_RECV;
106 param.sg_list = &list;
107 param.num_sge = 1;
108
109 ret = ib_post_recv(priv->qp, &param, &bad_wr);
110 if (unlikely(ret)) {
111 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
37ccf9df
RC
112 ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
113 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
1993d683
RD
114 dev_kfree_skb_any(priv->rx_ring[id].skb);
115 priv->rx_ring[id].skb = NULL;
116 }
1da177e4 117
1993d683 118 return ret;
1da177e4
LT
119}
120
1993d683 121static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
1da177e4
LT
122{
123 struct ipoib_dev_priv *priv = netdev_priv(dev);
124 struct sk_buff *skb;
37ccf9df 125 u64 addr;
1da177e4
LT
126
127 skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
1993d683 128 if (!skb)
1da177e4 129 return -ENOMEM;
1993d683
RD
130
131 /*
132 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
133 * header. So we need 4 more bytes to get to 48 and align the
134 * IP header to a multiple of 16.
135 */
136 skb_reserve(skb, 4);
137
37ccf9df
RC
138 addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
139 DMA_FROM_DEVICE);
140 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
1da177e4 141 dev_kfree_skb_any(skb);
1993d683 142 return -EIO;
1da177e4
LT
143 }
144
1993d683
RD
145 priv->rx_ring[id].skb = skb;
146 priv->rx_ring[id].mapping = addr;
147
148 return 0;
1da177e4
LT
149}
150
151static int ipoib_ib_post_receives(struct net_device *dev)
152{
153 struct ipoib_dev_priv *priv = netdev_priv(dev);
154 int i;
155
0f485251 156 for (i = 0; i < ipoib_recvq_size; ++i) {
1993d683
RD
157 if (ipoib_alloc_rx_skb(dev, i)) {
158 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
159 return -ENOMEM;
160 }
1da177e4
LT
161 if (ipoib_ib_post_receive(dev, i)) {
162 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
163 return -EIO;
164 }
165 }
166
167 return 0;
168}
169
2439a6e6 170static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
1da177e4
LT
171{
172 struct ipoib_dev_priv *priv = netdev_priv(dev);
2439a6e6
RD
173 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
174 struct sk_buff *skb;
37ccf9df 175 u64 addr;
1da177e4 176
a89875fc
RD
177 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
178 wr_id, wc->status);
1da177e4 179
2439a6e6
RD
180 if (unlikely(wr_id >= ipoib_recvq_size)) {
181 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
182 wr_id, ipoib_recvq_size);
183 return;
184 }
185
186 skb = priv->rx_ring[wr_id].skb;
187 addr = priv->rx_ring[wr_id].mapping;
188
189 if (unlikely(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);
37ccf9df
RC
194 ib_dma_unmap_single(priv->ca, addr,
195 IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
2439a6e6
RD
196 dev_kfree_skb_any(skb);
197 priv->rx_ring[wr_id].skb = NULL;
198 return;
199 }
1da177e4 200
1b844afe
RD
201 /*
202 * Drop packets that this interface sent, ie multicast packets
203 * that the HCA has replicated.
204 */
205 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
206 goto repost;
207
2439a6e6
RD
208 /*
209 * If we can't allocate a new RX buffer, dump
210 * this packet and reuse the old buffer.
211 */
212 if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
de903512 213 ++dev->stats.rx_dropped;
2439a6e6
RD
214 goto repost;
215 }
216
217 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
218 wc->byte_len, wc->slid);
219
37ccf9df 220 ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
2439a6e6
RD
221
222 skb_put(skb, wc->byte_len);
223 skb_pull(skb, IB_GRH_BYTES);
224
1b844afe
RD
225 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
226 skb_reset_mac_header(skb);
227 skb_pull(skb, IPOIB_ENCAP_LEN);
228
229 dev->last_rx = jiffies;
de903512
RD
230 ++dev->stats.rx_packets;
231 dev->stats.rx_bytes += skb->len;
1b844afe
RD
232
233 skb->dev = dev;
234 /* XXX get correct PACKET_ type here */
235 skb->pkt_type = PACKET_HOST;
6046136c
EC
236
237 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->csum_ok))
238 skb->ip_summed = CHECKSUM_UNNECESSARY;
239
1b844afe 240 netif_receive_skb(skb);
1da177e4 241
2439a6e6
RD
242repost:
243 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
244 ipoib_warn(priv, "ipoib_ib_post_receive failed "
245 "for buf %d\n", wr_id);
246}
1da177e4 247
7143740d
EC
248static int ipoib_dma_map_tx(struct ib_device *ca,
249 struct ipoib_tx_buf *tx_req)
250{
251 struct sk_buff *skb = tx_req->skb;
252 u64 *mapping = tx_req->mapping;
253 int i;
40ca1988 254 int off;
7143740d 255
40ca1988
EC
256 if (skb_headlen(skb)) {
257 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
258 DMA_TO_DEVICE);
259 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
260 return -EIO;
261
262 off = 1;
263 } else
264 off = 0;
7143740d
EC
265
266 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
267 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
40ca1988 268 mapping[i + off] = ib_dma_map_page(ca, frag->page,
7143740d
EC
269 frag->page_offset, frag->size,
270 DMA_TO_DEVICE);
40ca1988 271 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
7143740d
EC
272 goto partial_error;
273 }
274 return 0;
275
276partial_error:
7143740d
EC
277 for (; i > 0; --i) {
278 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
40ca1988 279 ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);
7143740d 280 }
40ca1988
EC
281
282 if (off)
283 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
284
7143740d
EC
285 return -EIO;
286}
287
288static void ipoib_dma_unmap_tx(struct ib_device *ca,
289 struct ipoib_tx_buf *tx_req)
290{
291 struct sk_buff *skb = tx_req->skb;
292 u64 *mapping = tx_req->mapping;
293 int i;
40ca1988 294 int off;
7143740d 295
40ca1988
EC
296 if (skb_headlen(skb)) {
297 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
298 off = 1;
299 } else
300 off = 0;
7143740d
EC
301
302 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
303 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
40ca1988 304 ib_dma_unmap_page(ca, mapping[i + off], frag->size,
7143740d
EC
305 DMA_TO_DEVICE);
306 }
307}
308
2439a6e6
RD
309static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
310{
311 struct ipoib_dev_priv *priv = netdev_priv(dev);
312 unsigned int wr_id = wc->wr_id;
313 struct ipoib_tx_buf *tx_req;
314 unsigned long flags;
1da177e4 315
a89875fc
RD
316 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
317 wr_id, wc->status);
1da177e4 318
2439a6e6
RD
319 if (unlikely(wr_id >= ipoib_sendq_size)) {
320 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
321 wr_id, ipoib_sendq_size);
322 return;
1da177e4 323 }
2439a6e6
RD
324
325 tx_req = &priv->tx_ring[wr_id];
326
7143740d 327 ipoib_dma_unmap_tx(priv->ca, tx_req);
2439a6e6 328
de903512
RD
329 ++dev->stats.tx_packets;
330 dev->stats.tx_bytes += tx_req->skb->len;
2439a6e6
RD
331
332 dev_kfree_skb_any(tx_req->skb);
333
334 spin_lock_irqsave(&priv->tx_lock, flags);
335 ++priv->tx_tail;
1b524963
MT
336 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
337 netif_queue_stopped(dev) &&
338 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
2439a6e6
RD
339 netif_wake_queue(dev);
340 spin_unlock_irqrestore(&priv->tx_lock, flags);
341
342 if (wc->status != IB_WC_SUCCESS &&
343 wc->status != IB_WC_WR_FLUSH_ERR)
344 ipoib_warn(priv, "failed send event "
345 "(status=%d, wrid=%d vend_err %x)\n",
346 wc->status, wr_id, wc->vendor_err);
347}
348
bea3348e 349int ipoib_poll(struct napi_struct *napi, int budget)
2439a6e6 350{
bea3348e
SH
351 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);
352 struct net_device *dev = priv->dev;
8d1cc86a
RD
353 int done;
354 int t;
8d1cc86a
RD
355 int n, i;
356
357 done = 0;
8d1cc86a 358
bea3348e
SH
359poll_more:
360 while (done < budget) {
361 int max = (budget - done);
362
8d1cc86a
RD
363 t = min(IPOIB_NUM_WC, max);
364 n = ib_poll_cq(priv->cq, t, priv->ibwc);
365
bea3348e 366 for (i = 0; i < n; i++) {
8d1cc86a
RD
367 struct ib_wc *wc = priv->ibwc + i;
368
1b524963 369 if (wc->wr_id & IPOIB_OP_RECV) {
8d1cc86a 370 ++done;
1b524963
MT
371 if (wc->wr_id & IPOIB_OP_CM)
372 ipoib_cm_handle_rx_wc(dev, wc);
373 else
374 ipoib_ib_handle_rx_wc(dev, wc);
375 } else {
376 if (wc->wr_id & IPOIB_OP_CM)
377 ipoib_cm_handle_tx_wc(dev, wc);
378 else
379 ipoib_ib_handle_tx_wc(dev, wc);
380 }
8d1cc86a
RD
381 }
382
bea3348e 383 if (n != t)
8d1cc86a 384 break;
8d1cc86a
RD
385 }
386
bea3348e
SH
387 if (done < budget) {
388 netif_rx_complete(dev, napi);
8d1cc86a
RD
389 if (unlikely(ib_req_notify_cq(priv->cq,
390 IB_CQ_NEXT_COMP |
391 IB_CQ_REPORT_MISSED_EVENTS)) &&
bea3348e
SH
392 netif_rx_reschedule(dev, napi))
393 goto poll_more;
8d1cc86a
RD
394 }
395
bea3348e 396 return done;
1da177e4
LT
397}
398
399void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
400{
bea3348e
SH
401 struct net_device *dev = dev_ptr;
402 struct ipoib_dev_priv *priv = netdev_priv(dev);
403
404 netif_rx_schedule(dev, &priv->napi);
1da177e4
LT
405}
406
407static inline int post_send(struct ipoib_dev_priv *priv,
408 unsigned int wr_id,
409 struct ib_ah *address, u32 qpn,
40ca1988
EC
410 struct ipoib_tx_buf *tx_req,
411 void *head, int hlen)
1da177e4
LT
412{
413 struct ib_send_wr *bad_wr;
40ca1988
EC
414 int i, off;
415 struct sk_buff *skb = tx_req->skb;
416 skb_frag_t *frags = skb_shinfo(skb)->frags;
417 int nr_frags = skb_shinfo(skb)->nr_frags;
418 u64 *mapping = tx_req->mapping;
419
420 if (skb_headlen(skb)) {
421 priv->tx_sge[0].addr = mapping[0];
422 priv->tx_sge[0].length = skb_headlen(skb);
423 off = 1;
424 } else
425 off = 0;
1da177e4 426
7143740d 427 for (i = 0; i < nr_frags; ++i) {
40ca1988
EC
428 priv->tx_sge[i + off].addr = mapping[i + off];
429 priv->tx_sge[i + off].length = frags[i].size;
7143740d 430 }
40ca1988 431 priv->tx_wr.num_sge = nr_frags + off;
7143740d
EC
432 priv->tx_wr.wr_id = wr_id;
433 priv->tx_wr.wr.ud.remote_qpn = qpn;
434 priv->tx_wr.wr.ud.ah = address;
1da177e4 435
40ca1988
EC
436 if (head) {
437 priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;
438 priv->tx_wr.wr.ud.header = head;
439 priv->tx_wr.wr.ud.hlen = hlen;
440 priv->tx_wr.opcode = IB_WR_LSO;
441 } else
442 priv->tx_wr.opcode = IB_WR_SEND;
443
1da177e4
LT
444 return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
445}
446
447void ipoib_send(struct net_device *dev, struct sk_buff *skb,
448 struct ipoib_ah *address, u32 qpn)
449{
450 struct ipoib_dev_priv *priv = netdev_priv(dev);
1993d683 451 struct ipoib_tx_buf *tx_req;
40ca1988
EC
452 int hlen;
453 void *phead;
454
455 if (skb_is_gso(skb)) {
456 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
457 phead = skb->data;
458 if (unlikely(!skb_pull(skb, hlen))) {
459 ipoib_warn(priv, "linear data too small\n");
460 ++dev->stats.tx_dropped;
461 ++dev->stats.tx_errors;
462 dev_kfree_skb_any(skb);
463 return;
464 }
465 } else {
466 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
467 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
468 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
469 ++dev->stats.tx_dropped;
470 ++dev->stats.tx_errors;
471 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
472 return;
473 }
474 phead = NULL;
475 hlen = 0;
1da177e4
LT
476 }
477
478 ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
479 skb->len, address, qpn);
480
481 /*
482 * We put the skb into the tx_ring _before_ we call post_send()
483 * because it's entirely possible that the completion handler will
484 * run before we execute anything after the post_send(). That
485 * means we have to make sure everything is properly recorded and
486 * our state is consistent before we call post_send().
487 */
0f485251 488 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
1da177e4 489 tx_req->skb = skb;
7143740d 490 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
de903512 491 ++dev->stats.tx_errors;
73fbe8be
RD
492 dev_kfree_skb_any(skb);
493 return;
494 }
1da177e4 495
6046136c
EC
496 if (skb->ip_summed == CHECKSUM_PARTIAL)
497 priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;
498 else
499 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
500
0f485251 501 if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
40ca1988 502 address->ah, qpn, tx_req, phead, hlen))) {
1da177e4 503 ipoib_warn(priv, "post_send failed\n");
de903512 504 ++dev->stats.tx_errors;
7143740d 505 ipoib_dma_unmap_tx(priv->ca, tx_req);
1da177e4
LT
506 dev_kfree_skb_any(skb);
507 } else {
508 dev->trans_start = jiffies;
509
510 address->last_send = priv->tx_head;
511 ++priv->tx_head;
512
1b524963 513 if (++priv->tx_outstanding == ipoib_sendq_size) {
1da177e4
LT
514 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
515 netif_stop_queue(dev);
516 }
517 }
518}
519
520static void __ipoib_reap_ah(struct net_device *dev)
521{
522 struct ipoib_dev_priv *priv = netdev_priv(dev);
523 struct ipoib_ah *ah, *tah;
524 LIST_HEAD(remove_list);
525
31c02e21
RD
526 spin_lock_irq(&priv->tx_lock);
527 spin_lock(&priv->lock);
1da177e4 528 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
2181858b 529 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
1da177e4 530 list_del(&ah->list);
31c02e21
RD
531 ib_destroy_ah(ah->ah);
532 kfree(ah);
1da177e4 533 }
31c02e21
RD
534 spin_unlock(&priv->lock);
535 spin_unlock_irq(&priv->tx_lock);
1da177e4
LT
536}
537
c4028958 538void ipoib_reap_ah(struct work_struct *work)
1da177e4 539{
c4028958
DH
540 struct ipoib_dev_priv *priv =
541 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
542 struct net_device *dev = priv->dev;
1da177e4
LT
543
544 __ipoib_reap_ah(dev);
545
546 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
69fc507a
AB
547 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
548 round_jiffies_relative(HZ));
1da177e4
LT
549}
550
551int ipoib_ib_dev_open(struct net_device *dev)
552{
553 struct ipoib_dev_priv *priv = netdev_priv(dev);
554 int ret;
555
26bbf13c
YE
556 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
557 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
558 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
559 return -1;
560 }
561 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
562
5b6810e0 563 ret = ipoib_init_qp(dev);
1da177e4 564 if (ret) {
5b6810e0 565 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
1da177e4
LT
566 return -1;
567 }
568
569 ret = ipoib_ib_post_receives(dev);
570 if (ret) {
571 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
26bbf13c 572 ipoib_ib_dev_stop(dev, 1);
1da177e4
LT
573 return -1;
574 }
575
839fcaba
MT
576 ret = ipoib_cm_dev_open(dev);
577 if (ret) {
24bd1e4e 578 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
26bbf13c 579 ipoib_ib_dev_stop(dev, 1);
839fcaba
MT
580 return -1;
581 }
582
1da177e4 583 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
69fc507a
AB
584 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
585 round_jiffies_relative(HZ));
1da177e4 586
7a343d4c
LA
587 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
588
1da177e4
LT
589 return 0;
590}
591
7a343d4c
LA
592static void ipoib_pkey_dev_check_presence(struct net_device *dev)
593{
594 struct ipoib_dev_priv *priv = netdev_priv(dev);
595 u16 pkey_index = 0;
596
597 if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
598 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
599 else
600 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
601}
602
1da177e4
LT
603int ipoib_ib_dev_up(struct net_device *dev)
604{
605 struct ipoib_dev_priv *priv = netdev_priv(dev);
606
7a343d4c
LA
607 ipoib_pkey_dev_check_presence(dev);
608
609 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
610 ipoib_dbg(priv, "PKEY is not assigned.\n");
611 return 0;
612 }
613
1da177e4
LT
614 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
615
616 return ipoib_mcast_start_thread(dev);
617}
618
0b3ea082 619int ipoib_ib_dev_down(struct net_device *dev, int flush)
1da177e4
LT
620{
621 struct ipoib_dev_priv *priv = netdev_priv(dev);
622
623 ipoib_dbg(priv, "downing ib_dev\n");
624
625 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
626 netif_carrier_off(dev);
627
628 /* Shutdown the P_Key thread if still active */
629 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 630 mutex_lock(&pkey_mutex);
1da177e4 631 set_bit(IPOIB_PKEY_STOP, &priv->flags);
26bbf13c 632 cancel_delayed_work(&priv->pkey_poll_task);
95ed644f 633 mutex_unlock(&pkey_mutex);
0b3ea082
JM
634 if (flush)
635 flush_workqueue(ipoib_workqueue);
1da177e4
LT
636 }
637
0b3ea082 638 ipoib_mcast_stop_thread(dev, flush);
1da177e4
LT
639 ipoib_mcast_dev_flush(dev);
640
1da177e4
LT
641 ipoib_flush_paths(dev);
642
643 return 0;
644}
645
646static int recvs_pending(struct net_device *dev)
647{
648 struct ipoib_dev_priv *priv = netdev_priv(dev);
649 int pending = 0;
650 int i;
651
0f485251 652 for (i = 0; i < ipoib_recvq_size; ++i)
1da177e4
LT
653 if (priv->rx_ring[i].skb)
654 ++pending;
655
656 return pending;
657}
658
2dfbfc37
MT
659void ipoib_drain_cq(struct net_device *dev)
660{
661 struct ipoib_dev_priv *priv = netdev_priv(dev);
662 int i, n;
663 do {
664 n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
665 for (i = 0; i < n; ++i) {
ce423ef5
RD
666 /*
667 * Convert any successful completions to flush
668 * errors to avoid passing packets up the
669 * stack after bringing the device down.
670 */
671 if (priv->ibwc[i].status == IB_WC_SUCCESS)
672 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
673
1b524963
MT
674 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
675 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
676 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
677 else
678 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
679 } else {
680 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
681 ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);
682 else
683 ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
684 }
2dfbfc37
MT
685 }
686 } while (n == IPOIB_NUM_WC);
687}
688
26bbf13c 689int ipoib_ib_dev_stop(struct net_device *dev, int flush)
1da177e4
LT
690{
691 struct ipoib_dev_priv *priv = netdev_priv(dev);
692 struct ib_qp_attr qp_attr;
1da177e4 693 unsigned long begin;
1993d683 694 struct ipoib_tx_buf *tx_req;
2dfbfc37 695 int i;
1da177e4 696
7a343d4c
LA
697 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
698
839fcaba
MT
699 ipoib_cm_dev_stop(dev);
700
3bc12e75
RD
701 /*
702 * Move our QP to the error state and then reinitialize in
703 * when all work requests have completed or have been flushed.
704 */
1da177e4 705 qp_attr.qp_state = IB_QPS_ERR;
3bc12e75 706 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
707 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
708
709 /* Wait for all sends and receives to complete */
710 begin = jiffies;
711
712 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
713 if (time_after(jiffies, begin + 5 * HZ)) {
714 ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
715 priv->tx_head - priv->tx_tail, recvs_pending(dev));
716
717 /*
718 * assume the HW is wedged and just free up
719 * all our pending work requests.
720 */
2181858b 721 while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
1da177e4 722 tx_req = &priv->tx_ring[priv->tx_tail &
0f485251 723 (ipoib_sendq_size - 1)];
7143740d 724 ipoib_dma_unmap_tx(priv->ca, tx_req);
1da177e4
LT
725 dev_kfree_skb_any(tx_req->skb);
726 ++priv->tx_tail;
1b524963 727 --priv->tx_outstanding;
1da177e4
LT
728 }
729
37ccf9df
RC
730 for (i = 0; i < ipoib_recvq_size; ++i) {
731 struct ipoib_rx_buf *rx_req;
732
733 rx_req = &priv->rx_ring[i];
734 if (!rx_req->skb)
735 continue;
736 ib_dma_unmap_single(priv->ca,
737 rx_req->mapping,
738 IPOIB_BUF_SIZE,
739 DMA_FROM_DEVICE);
740 dev_kfree_skb_any(rx_req->skb);
741 rx_req->skb = NULL;
742 }
1da177e4
LT
743
744 goto timeout;
745 }
746
2dfbfc37 747 ipoib_drain_cq(dev);
8d1cc86a 748
1da177e4
LT
749 msleep(1);
750 }
751
752 ipoib_dbg(priv, "All sends and receives done.\n");
753
754timeout:
755 qp_attr.qp_state = IB_QPS_RESET;
3bc12e75 756 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
1da177e4
LT
757 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
758
759 /* Wait for all AHs to be reaped */
760 set_bit(IPOIB_STOP_REAPER, &priv->flags);
761 cancel_delayed_work(&priv->ah_reap_task);
26bbf13c
YE
762 if (flush)
763 flush_workqueue(ipoib_workqueue);
1da177e4
LT
764
765 begin = jiffies;
766
767 while (!list_empty(&priv->dead_ahs)) {
768 __ipoib_reap_ah(dev);
769
770 if (time_after(jiffies, begin + HZ)) {
771 ipoib_warn(priv, "timing out; will leak address handles\n");
772 break;
773 }
774
775 msleep(1);
776 }
777
8d1cc86a
RD
778 ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);
779
1da177e4
LT
780 return 0;
781}
782
783int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
784{
785 struct ipoib_dev_priv *priv = netdev_priv(dev);
786
787 priv->ca = ca;
788 priv->port = port;
789 priv->qp = NULL;
790
791 if (ipoib_transport_dev_init(dev, ca)) {
792 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
793 return -ENODEV;
794 }
795
796 if (dev->flags & IFF_UP) {
797 if (ipoib_ib_dev_open(dev)) {
798 ipoib_transport_dev_cleanup(dev);
799 return -ENODEV;
800 }
801 }
802
803 return 0;
804}
805
26bbf13c 806static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
1da177e4 807{
26bbf13c 808 struct ipoib_dev_priv *cpriv;
c4028958 809 struct net_device *dev = priv->dev;
26bbf13c
YE
810 u16 new_index;
811
812 mutex_lock(&priv->vlan_mutex);
1da177e4 813
26bbf13c
YE
814 /*
815 * Flush any child interfaces too -- they might be up even if
816 * the parent is down.
817 */
818 list_for_each_entry(cpriv, &priv->child_intfs, list)
819 __ipoib_ib_dev_flush(cpriv, pkey_event);
820
821 mutex_unlock(&priv->vlan_mutex);
822
823 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
7a343d4c
LA
824 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
825 return;
826 }
827
828 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
829 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
1da177e4 830 return;
7a343d4c 831 }
1da177e4 832
26bbf13c
YE
833 if (pkey_event) {
834 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
835 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
836 ipoib_ib_dev_down(dev, 0);
167c4265 837 ipoib_ib_dev_stop(dev, 0);
26bbf13c
YE
838 ipoib_pkey_dev_delay_open(dev);
839 return;
840 }
841 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
842
843 /* restart QP only if P_Key index is changed */
844 if (new_index == priv->pkey_index) {
845 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
846 return;
847 }
848 priv->pkey_index = new_index;
849 }
850
1da177e4
LT
851 ipoib_dbg(priv, "flushing\n");
852
0b3ea082 853 ipoib_ib_dev_down(dev, 0);
1da177e4 854
26bbf13c
YE
855 if (pkey_event) {
856 ipoib_ib_dev_stop(dev, 0);
857 ipoib_ib_dev_open(dev);
858 }
859
1da177e4
LT
860 /*
861 * The device could have been brought down between the start and when
862 * we get here, don't bring it back up if it's not configured up
863 */
5ccd0255 864 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
1da177e4 865 ipoib_ib_dev_up(dev);
c4028958 866 ipoib_mcast_restart_task(&priv->restart_task);
5ccd0255 867 }
26bbf13c 868}
1da177e4 869
26bbf13c
YE
870void ipoib_ib_dev_flush(struct work_struct *work)
871{
872 struct ipoib_dev_priv *priv =
873 container_of(work, struct ipoib_dev_priv, flush_task);
4f71055a 874
26bbf13c
YE
875 ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
876 __ipoib_ib_dev_flush(priv, 0);
877}
4f71055a 878
26bbf13c
YE
879void ipoib_pkey_event(struct work_struct *work)
880{
881 struct ipoib_dev_priv *priv =
882 container_of(work, struct ipoib_dev_priv, pkey_event_task);
883
884 ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
885 __ipoib_ib_dev_flush(priv, 1);
1da177e4
LT
886}
887
888void ipoib_ib_dev_cleanup(struct net_device *dev)
889{
890 struct ipoib_dev_priv *priv = netdev_priv(dev);
891
892 ipoib_dbg(priv, "cleaning up ib_dev\n");
893
8d2cae06 894 ipoib_mcast_stop_thread(dev, 1);
988bd503 895 ipoib_mcast_dev_flush(dev);
1da177e4
LT
896
897 ipoib_transport_dev_cleanup(dev);
898}
899
900/*
901 * Delayed P_Key Assigment Interim Support
902 *
903 * The following is initial implementation of delayed P_Key assigment
904 * mechanism. It is using the same approach implemented for the multicast
905 * group join. The single goal of this implementation is to quickly address
906 * Bug #2507. This implementation will probably be removed when the P_Key
907 * change async notification is available.
908 */
1da177e4 909
c4028958 910void ipoib_pkey_poll(struct work_struct *work)
1da177e4 911{
c4028958 912 struct ipoib_dev_priv *priv =
26bbf13c 913 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
c4028958 914 struct net_device *dev = priv->dev;
1da177e4
LT
915
916 ipoib_pkey_dev_check_presence(dev);
917
918 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
919 ipoib_open(dev);
920 else {
95ed644f 921 mutex_lock(&pkey_mutex);
1da177e4
LT
922 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
923 queue_delayed_work(ipoib_workqueue,
26bbf13c 924 &priv->pkey_poll_task,
1da177e4 925 HZ);
95ed644f 926 mutex_unlock(&pkey_mutex);
1da177e4
LT
927 }
928}
929
930int ipoib_pkey_dev_delay_open(struct net_device *dev)
931{
932 struct ipoib_dev_priv *priv = netdev_priv(dev);
933
934 /* Look for the interface pkey value in the IB Port P_Key table and */
935 /* set the interface pkey assigment flag */
936 ipoib_pkey_dev_check_presence(dev);
937
938 /* P_Key value not assigned yet - start polling */
939 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
95ed644f 940 mutex_lock(&pkey_mutex);
1da177e4
LT
941 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
942 queue_delayed_work(ipoib_workqueue,
26bbf13c 943 &priv->pkey_poll_task,
1da177e4 944 HZ);
95ed644f 945 mutex_unlock(&pkey_mutex);
1da177e4
LT
946 return 1;
947 }
948
949 return 0;
950}