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