]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/infiniband/ulp/ipoib/ipoib_ib.c
Merge tag 'powerpc-4.15-6' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-bionic-kernel.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.
1da177e4
LT
34 */
35
36#include <linux/delay.h>
fec14d2f 37#include <linux/moduleparam.h>
1da177e4 38#include <linux/dma-mapping.h>
5a0e3ad6 39#include <linux/slab.h>
1da177e4 40
40ca1988
EC
41#include <linux/ip.h>
42#include <linux/tcp.h>
1da177e4
LT
43
44#include "ipoib.h"
45
46#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
47static int data_debug_level;
48
49module_param(data_debug_level, int, 0644);
50MODULE_PARM_DESC(data_debug_level,
51 "Enable data path debug tracing if > 0");
52#endif
53
1da177e4 54struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
90898850 55 struct ib_pd *pd, struct rdma_ah_attr *attr)
1da177e4
LT
56{
57 struct ipoib_ah *ah;
3874397c 58 struct ib_ah *vah;
1da177e4
LT
59
60 ah = kmalloc(sizeof *ah, GFP_KERNEL);
61 if (!ah)
3874397c 62 return ERR_PTR(-ENOMEM);
1da177e4
LT
63
64 ah->dev = dev;
65 ah->last_send = 0;
66 kref_init(&ah->ref);
67
0a18cfe4 68 vah = rdma_create_ah(pd, attr);
3874397c 69 if (IS_ERR(vah)) {
1da177e4 70 kfree(ah);
3874397c
MM
71 ah = (struct ipoib_ah *)vah;
72 } else {
73 ah->ah = vah;
c1048aff 74 ipoib_dbg(ipoib_priv(dev), "Created ah %p\n", ah->ah);
3874397c 75 }
1da177e4
LT
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);
c1048aff 83 struct ipoib_dev_priv *priv = ipoib_priv(ah->dev);
1da177e4
LT
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{
a44878d1
ES
95 ib_dma_unmap_single(priv->ca, mapping[0],
96 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
97 DMA_FROM_DEVICE);
bc7b3a36
SM
98}
99
1993d683 100static int ipoib_ib_post_receive(struct net_device *dev, int id)
1da177e4 101{
c1048aff 102 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 103 struct ib_recv_wr *bad_wr;
1993d683
RD
104 int ret;
105
bc7b3a36
SM
106 priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
107 priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
108 priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
1993d683 109
1993d683 110
bc7b3a36 111 ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
1993d683
RD
112 if (unlikely(ret)) {
113 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
bc7b3a36 114 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
1993d683
RD
115 dev_kfree_skb_any(priv->rx_ring[id].skb);
116 priv->rx_ring[id].skb = NULL;
117 }
1da177e4 118
1993d683 119 return ret;
1da177e4
LT
120}
121
bc7b3a36 122static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
1da177e4 123{
c1048aff 124 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 125 struct sk_buff *skb;
bc7b3a36
SM
126 int buf_size;
127 u64 *mapping;
1da177e4 128
a44878d1 129 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
bc7b3a36 130
fc791b63 131 skb = dev_alloc_skb(buf_size + IPOIB_HARD_LEN);
bc7b3a36
SM
132 if (unlikely(!skb))
133 return NULL;
1993d683
RD
134
135 /*
fc791b63
PA
136 * the IP header will be at IPOIP_HARD_LEN + IB_GRH_BYTES, that is
137 * 64 bytes aligned
1993d683 138 */
fc791b63 139 skb_reserve(skb, sizeof(struct ipoib_pseudo_header));
1993d683 140
bc7b3a36
SM
141 mapping = priv->rx_ring[id].mapping;
142 mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
143 DMA_FROM_DEVICE);
144 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
145 goto error;
146
bc7b3a36
SM
147 priv->rx_ring[id].skb = skb;
148 return skb;
bc7b3a36
SM
149error:
150 dev_kfree_skb_any(skb);
151 return NULL;
1da177e4
LT
152}
153
154static int ipoib_ib_post_receives(struct net_device *dev)
155{
c1048aff 156 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
157 int i;
158
0f485251 159 for (i = 0; i < ipoib_recvq_size; ++i) {
bc7b3a36 160 if (!ipoib_alloc_rx_skb(dev, i)) {
1993d683
RD
161 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
162 return -ENOMEM;
163 }
1da177e4
LT
164 if (ipoib_ib_post_receive(dev, i)) {
165 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
166 return -EIO;
167 }
168 }
169
170 return 0;
171}
172
2439a6e6 173static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
1da177e4 174{
c1048aff 175 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2439a6e6
RD
176 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
177 struct sk_buff *skb;
bc7b3a36 178 u64 mapping[IPOIB_UD_RX_SG];
fed1db33 179 union ib_gid *dgid;
68996a6e 180 union ib_gid *sgid;
1da177e4 181
a89875fc
RD
182 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
183 wr_id, wc->status);
1da177e4 184
2439a6e6
RD
185 if (unlikely(wr_id >= ipoib_recvq_size)) {
186 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
187 wr_id, ipoib_recvq_size);
188 return;
189 }
190
191 skb = priv->rx_ring[wr_id].skb;
2439a6e6
RD
192
193 if (unlikely(wc->status != IB_WC_SUCCESS)) {
194 if (wc->status != IB_WC_WR_FLUSH_ERR)
b04dc199
AH
195 ipoib_warn(priv,
196 "failed recv event (status=%d, wrid=%d vend_err %#x)\n",
2439a6e6 197 wc->status, wr_id, wc->vendor_err);
bc7b3a36 198 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
2439a6e6
RD
199 dev_kfree_skb_any(skb);
200 priv->rx_ring[wr_id].skb = NULL;
201 return;
202 }
1da177e4 203
bc7b3a36
SM
204 memcpy(mapping, priv->rx_ring[wr_id].mapping,
205 IPOIB_UD_RX_SG * sizeof *mapping);
206
2439a6e6
RD
207 /*
208 * If we can't allocate a new RX buffer, dump
209 * this packet and reuse the old buffer.
210 */
bc7b3a36 211 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
de903512 212 ++dev->stats.rx_dropped;
2439a6e6
RD
213 goto repost;
214 }
215
216 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
217 wc->byte_len, wc->slid);
218
bc7b3a36 219 ipoib_ud_dma_unmap_rx(priv, mapping);
a44878d1
ES
220
221 skb_put(skb, wc->byte_len);
2439a6e6 222
fed1db33
CL
223 /* First byte of dgid signals multicast when 0xff */
224 dgid = &((struct ib_grh *)skb->data)->dgid;
225
226 if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
227 skb->pkt_type = PACKET_HOST;
228 else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
229 skb->pkt_type = PACKET_BROADCAST;
230 else
231 skb->pkt_type = PACKET_MULTICAST;
232
68996a6e
EC
233 sgid = &((struct ib_grh *)skb->data)->sgid;
234
235 /*
236 * Drop packets that this interface sent, ie multicast packets
237 * that the HCA has replicated.
238 */
239 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num) {
240 int need_repost = 1;
241
242 if ((wc->wc_flags & IB_WC_GRH) &&
243 sgid->global.interface_id != priv->local_gid.global.interface_id)
244 need_repost = 0;
245
246 if (need_repost) {
247 dev_kfree_skb_any(skb);
248 goto repost;
249 }
250 }
251
2439a6e6
RD
252 skb_pull(skb, IB_GRH_BYTES);
253
1b844afe 254 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
fc791b63 255 skb_add_pseudo_hdr(skb);
1b844afe 256
de903512
RD
257 ++dev->stats.rx_packets;
258 dev->stats.rx_bytes += skb->len;
4829d964
AV
259 if (skb->pkt_type == PACKET_MULTICAST)
260 dev->stats.multicast++;
1b844afe
RD
261
262 skb->dev = dev;
d927d505
OG
263 if ((dev->features & NETIF_F_RXCSUM) &&
264 likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
6046136c
EC
265 skb->ip_summed = CHECKSUM_UNNECESSARY;
266
8966e28d 267 napi_gro_receive(&priv->recv_napi, skb);
1da177e4 268
2439a6e6
RD
269repost:
270 if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
271 ipoib_warn(priv, "ipoib_ib_post_receive failed "
272 "for buf %d\n", wr_id);
273}
1da177e4 274
c4268778 275int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
7143740d
EC
276{
277 struct sk_buff *skb = tx_req->skb;
278 u64 *mapping = tx_req->mapping;
279 int i;
40ca1988 280 int off;
7143740d 281
40ca1988
EC
282 if (skb_headlen(skb)) {
283 mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
284 DMA_TO_DEVICE);
285 if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
286 return -EIO;
287
288 off = 1;
289 } else
290 off = 0;
7143740d
EC
291
292 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
9e903e08 293 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5581be3b
IC
294 mapping[i + off] = ib_dma_map_page(ca,
295 skb_frag_page(frag),
9e903e08 296 frag->page_offset, skb_frag_size(frag),
7143740d 297 DMA_TO_DEVICE);
40ca1988 298 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
7143740d
EC
299 goto partial_error;
300 }
301 return 0;
302
303partial_error:
7143740d 304 for (; i > 0; --i) {
9e903e08
ED
305 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
306
307 ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
7143740d 308 }
40ca1988
EC
309
310 if (off)
311 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
312
7143740d
EC
313 return -EIO;
314}
315
c4268778
YS
316void ipoib_dma_unmap_tx(struct ipoib_dev_priv *priv,
317 struct ipoib_tx_buf *tx_req)
7143740d
EC
318{
319 struct sk_buff *skb = tx_req->skb;
320 u64 *mapping = tx_req->mapping;
321 int i;
40ca1988 322 int off;
7143740d 323
40ca1988 324 if (skb_headlen(skb)) {
c4268778
YS
325 ib_dma_unmap_single(priv->ca, mapping[0], skb_headlen(skb),
326 DMA_TO_DEVICE);
40ca1988
EC
327 off = 1;
328 } else
329 off = 0;
7143740d
EC
330
331 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
9e903e08
ED
332 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
333
c4268778
YS
334 ib_dma_unmap_page(priv->ca, mapping[i + off],
335 skb_frag_size(frag), DMA_TO_DEVICE);
7143740d
EC
336 }
337}
338
2c010730
ES
339/*
340 * As the result of a completion error the QP Can be transferred to SQE states.
341 * The function checks if the (send)QP is in SQE state and
342 * moves it back to RTS state, that in order to have it functional again.
343 */
344static void ipoib_qp_state_validate_work(struct work_struct *work)
345{
346 struct ipoib_qp_state_validate *qp_work =
347 container_of(work, struct ipoib_qp_state_validate, work);
348
349 struct ipoib_dev_priv *priv = qp_work->priv;
350 struct ib_qp_attr qp_attr;
351 struct ib_qp_init_attr query_init_attr;
352 int ret;
353
354 ret = ib_query_qp(priv->qp, &qp_attr, IB_QP_STATE, &query_init_attr);
355 if (ret) {
356 ipoib_warn(priv, "%s: Failed to query QP ret: %d\n",
357 __func__, ret);
358 goto free_res;
359 }
360 pr_info("%s: QP: 0x%x is in state: %d\n",
361 __func__, priv->qp->qp_num, qp_attr.qp_state);
362
363 /* currently support only in SQE->RTS transition*/
364 if (qp_attr.qp_state == IB_QPS_SQE) {
365 qp_attr.qp_state = IB_QPS_RTS;
366
367 ret = ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE);
368 if (ret) {
369 pr_warn("failed(%d) modify QP:0x%x SQE->RTS\n",
370 ret, priv->qp->qp_num);
371 goto free_res;
372 }
373 pr_info("%s: QP: 0x%x moved from IB_QPS_SQE to IB_QPS_RTS\n",
374 __func__, priv->qp->qp_num);
375 } else {
376 pr_warn("QP (%d) will stay in state: %d\n",
377 priv->qp->qp_num, qp_attr.qp_state);
378 }
379
380free_res:
381 kfree(qp_work);
382}
383
2439a6e6
RD
384static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
385{
c1048aff 386 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2439a6e6
RD
387 unsigned int wr_id = wc->wr_id;
388 struct ipoib_tx_buf *tx_req;
1da177e4 389
a89875fc
RD
390 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
391 wr_id, wc->status);
1da177e4 392
2439a6e6
RD
393 if (unlikely(wr_id >= ipoib_sendq_size)) {
394 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
395 wr_id, ipoib_sendq_size);
396 return;
1da177e4 397 }
2439a6e6
RD
398
399 tx_req = &priv->tx_ring[wr_id];
400
c4268778 401 ipoib_dma_unmap_tx(priv, tx_req);
2439a6e6 402
de903512
RD
403 ++dev->stats.tx_packets;
404 dev->stats.tx_bytes += tx_req->skb->len;
2439a6e6
RD
405
406 dev_kfree_skb_any(tx_req->skb);
407
2439a6e6 408 ++priv->tx_tail;
8966e28d
ES
409
410 if (unlikely(netif_queue_stopped(dev) &&
411 ((priv->tx_head - priv->tx_tail) <= ipoib_sendq_size >> 1) &&
412 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)))
2439a6e6 413 netif_wake_queue(dev);
2439a6e6
RD
414
415 if (wc->status != IB_WC_SUCCESS &&
2c010730
ES
416 wc->status != IB_WC_WR_FLUSH_ERR) {
417 struct ipoib_qp_state_validate *qp_work;
b04dc199
AH
418 ipoib_warn(priv,
419 "failed send event (status=%d, wrid=%d vend_err %#x)\n",
2439a6e6 420 wc->status, wr_id, wc->vendor_err);
2c010730 421 qp_work = kzalloc(sizeof(*qp_work), GFP_ATOMIC);
74226649 422 if (!qp_work)
2c010730 423 return;
2c010730
ES
424
425 INIT_WORK(&qp_work->work, ipoib_qp_state_validate_work);
426 qp_work->priv = priv;
427 queue_work(priv->wq, &qp_work->work);
428 }
2439a6e6
RD
429}
430
f56bcd80
EC
431static int poll_tx(struct ipoib_dev_priv *priv)
432{
433 int n, i;
8966e28d 434 struct ib_wc *wc;
f56bcd80
EC
435
436 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
8966e28d
ES
437 for (i = 0; i < n; ++i) {
438 wc = priv->send_wc + i;
439 if (wc->wr_id & IPOIB_OP_CM)
440 ipoib_cm_handle_tx_wc(priv->dev, priv->send_wc + i);
441 else
442 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
443 }
f56bcd80
EC
444 return n == MAX_SEND_CQE;
445}
446
8966e28d 447int ipoib_rx_poll(struct napi_struct *napi, int budget)
2439a6e6 448{
8966e28d
ES
449 struct ipoib_dev_priv *priv =
450 container_of(napi, struct ipoib_dev_priv, recv_napi);
bea3348e 451 struct net_device *dev = priv->dev;
8d1cc86a
RD
452 int done;
453 int t;
8d1cc86a
RD
454 int n, i;
455
456 done = 0;
8d1cc86a 457
bea3348e
SH
458poll_more:
459 while (done < budget) {
460 int max = (budget - done);
461
8d1cc86a 462 t = min(IPOIB_NUM_WC, max);
f56bcd80 463 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
8d1cc86a 464
bea3348e 465 for (i = 0; i < n; i++) {
8d1cc86a
RD
466 struct ib_wc *wc = priv->ibwc + i;
467
1b524963 468 if (wc->wr_id & IPOIB_OP_RECV) {
8d1cc86a 469 ++done;
1b524963
MT
470 if (wc->wr_id & IPOIB_OP_CM)
471 ipoib_cm_handle_rx_wc(dev, wc);
472 else
473 ipoib_ib_handle_rx_wc(dev, wc);
8966e28d
ES
474 } else {
475 pr_warn("%s: Got unexpected wqe id\n", __func__);
476 }
8d1cc86a
RD
477 }
478
bea3348e 479 if (n != t)
8d1cc86a 480 break;
8d1cc86a
RD
481 }
482
bea3348e 483 if (done < budget) {
288379f0 484 napi_complete(napi);
f56bcd80 485 if (unlikely(ib_req_notify_cq(priv->recv_cq,
8d1cc86a
RD
486 IB_CQ_NEXT_COMP |
487 IB_CQ_REPORT_MISSED_EVENTS)) &&
288379f0 488 napi_reschedule(napi))
bea3348e 489 goto poll_more;
8d1cc86a
RD
490 }
491
bea3348e 492 return done;
1da177e4
LT
493}
494
8966e28d 495int ipoib_tx_poll(struct napi_struct *napi, int budget)
1da177e4 496{
8966e28d
ES
497 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv,
498 send_napi);
499 struct net_device *dev = priv->dev;
500 int n, i;
501 struct ib_wc *wc;
bea3348e 502
8966e28d
ES
503poll_more:
504 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
1da177e4 505
8966e28d
ES
506 for (i = 0; i < n; i++) {
507 wc = priv->send_wc + i;
508 if (wc->wr_id & IPOIB_OP_CM)
509 ipoib_cm_handle_tx_wc(dev, wc);
510 else
511 ipoib_ib_handle_tx_wc(dev, wc);
512 }
57ce41d1 513
8966e28d
ES
514 if (n < budget) {
515 napi_complete(napi);
516 if (unlikely(ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
517 IB_CQ_REPORT_MISSED_EVENTS)) &&
518 napi_reschedule(napi))
519 goto poll_more;
520 }
521 return n < 0 ? 0 : n;
522}
57ce41d1 523
8966e28d
ES
524void ipoib_ib_rx_completion(struct ib_cq *cq, void *ctx_ptr)
525{
526 struct ipoib_dev_priv *priv = ctx_ptr;
57ce41d1 527
8966e28d 528 napi_schedule(&priv->recv_napi);
57ce41d1
EC
529}
530
8966e28d 531void ipoib_ib_tx_completion(struct ib_cq *cq, void *ctx_ptr)
57ce41d1 532{
8966e28d 533 struct ipoib_dev_priv *priv = ctx_ptr;
943c246e 534
8966e28d 535 napi_schedule(&priv->send_napi);
57ce41d1
EC
536}
537
1da177e4
LT
538static inline int post_send(struct ipoib_dev_priv *priv,
539 unsigned int wr_id,
10adcbd2 540 struct ib_ah *address, u32 dqpn,
40ca1988
EC
541 struct ipoib_tx_buf *tx_req,
542 void *head, int hlen)
1da177e4
LT
543{
544 struct ib_send_wr *bad_wr;
40ca1988 545 struct sk_buff *skb = tx_req->skb;
40ca1988 546
c4268778 547 ipoib_build_sge(priv, tx_req);
1da177e4 548
e622f2f4 549 priv->tx_wr.wr.wr_id = wr_id;
10adcbd2 550 priv->tx_wr.remote_qpn = dqpn;
e622f2f4 551 priv->tx_wr.ah = address;
1da177e4 552
40ca1988 553 if (head) {
e622f2f4
CH
554 priv->tx_wr.mss = skb_shinfo(skb)->gso_size;
555 priv->tx_wr.header = head;
556 priv->tx_wr.hlen = hlen;
557 priv->tx_wr.wr.opcode = IB_WR_LSO;
40ca1988 558 } else
e622f2f4 559 priv->tx_wr.wr.opcode = IB_WR_SEND;
40ca1988 560
e622f2f4 561 return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr);
1da177e4
LT
562}
563
cd565b4b
ES
564int ipoib_send(struct net_device *dev, struct sk_buff *skb,
565 struct ib_ah *address, u32 dqpn)
1da177e4 566{
c1048aff 567 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1993d683 568 struct ipoib_tx_buf *tx_req;
a48f509b 569 int hlen, rc;
40ca1988 570 void *phead;
78a50a5e 571 unsigned usable_sge = priv->max_send_sge - !!skb_headlen(skb);
40ca1988
EC
572
573 if (skb_is_gso(skb)) {
574 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
575 phead = skb->data;
576 if (unlikely(!skb_pull(skb, hlen))) {
577 ipoib_warn(priv, "linear data too small\n");
578 ++dev->stats.tx_dropped;
579 ++dev->stats.tx_errors;
580 dev_kfree_skb_any(skb);
cd565b4b 581 return -1;
40ca1988
EC
582 }
583 } else {
584 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
585 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
586 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
587 ++dev->stats.tx_dropped;
588 ++dev->stats.tx_errors;
589 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
cd565b4b 590 return -1;
40ca1988
EC
591 }
592 phead = NULL;
593 hlen = 0;
1da177e4 594 }
78a50a5e
HWR
595 if (skb_shinfo(skb)->nr_frags > usable_sge) {
596 if (skb_linearize(skb) < 0) {
597 ipoib_warn(priv, "skb could not be linearized\n");
598 ++dev->stats.tx_dropped;
599 ++dev->stats.tx_errors;
600 dev_kfree_skb_any(skb);
cd565b4b 601 return -1;
78a50a5e
HWR
602 }
603 /* Does skb_linearize return ok without reducing nr_frags? */
604 if (skb_shinfo(skb)->nr_frags > usable_sge) {
605 ipoib_warn(priv, "too many frags after skb linearize\n");
606 ++dev->stats.tx_dropped;
607 ++dev->stats.tx_errors;
608 dev_kfree_skb_any(skb);
cd565b4b 609 return -1;
78a50a5e
HWR
610 }
611 }
1da177e4 612
10adcbd2
ES
613 ipoib_dbg_data(priv,
614 "sending packet, length=%d address=%p dqpn=0x%06x\n",
615 skb->len, address, dqpn);
1da177e4
LT
616
617 /*
618 * We put the skb into the tx_ring _before_ we call post_send()
619 * because it's entirely possible that the completion handler will
620 * run before we execute anything after the post_send(). That
621 * means we have to make sure everything is properly recorded and
622 * our state is consistent before we call post_send().
623 */
0f485251 624 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
1da177e4 625 tx_req->skb = skb;
7143740d 626 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
de903512 627 ++dev->stats.tx_errors;
73fbe8be 628 dev_kfree_skb_any(skb);
cd565b4b 629 return -1;
73fbe8be 630 }
1da177e4 631
6046136c 632 if (skb->ip_summed == CHECKSUM_PARTIAL)
e622f2f4 633 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
6046136c 634 else
e622f2f4 635 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
2c104ea6
ES
636 /* increase the tx_head after send success, but use it for queue state */
637 if (priv->tx_head - priv->tx_tail == ipoib_sendq_size - 1) {
57ce41d1 638 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
57ce41d1
EC
639 netif_stop_queue(dev);
640 }
641
7e5a90c2
SP
642 skb_orphan(skb);
643 skb_dst_drop(skb);
644
8966e28d
ES
645 if (netif_queue_stopped(dev))
646 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
647 IB_CQ_REPORT_MISSED_EVENTS))
648 ipoib_warn(priv, "request notify on send CQ failed\n");
649
a48f509b 650 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
cd565b4b 651 address, dqpn, tx_req, phead, hlen);
a48f509b
OG
652 if (unlikely(rc)) {
653 ipoib_warn(priv, "post_send failed, error %d\n", rc);
de903512 654 ++dev->stats.tx_errors;
c4268778 655 ipoib_dma_unmap_tx(priv, tx_req);
1da177e4 656 dev_kfree_skb_any(skb);
57ce41d1
EC
657 if (netif_queue_stopped(dev))
658 netif_wake_queue(dev);
cd565b4b 659 rc = 0;
1da177e4 660 } else {
860e9538 661 netif_trans_update(dev);
1da177e4 662
cd565b4b 663 rc = priv->tx_head;
1da177e4 664 ++priv->tx_head;
1da177e4 665 }
cd565b4b 666 return rc;
1da177e4
LT
667}
668
669static void __ipoib_reap_ah(struct net_device *dev)
670{
c1048aff 671 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
672 struct ipoib_ah *ah, *tah;
673 LIST_HEAD(remove_list);
943c246e
RD
674 unsigned long flags;
675
676 netif_tx_lock_bh(dev);
677 spin_lock_irqsave(&priv->lock, flags);
1da177e4 678
1da177e4 679 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
2181858b 680 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
1da177e4 681 list_del(&ah->list);
36523159 682 rdma_destroy_ah(ah->ah);
31c02e21 683 kfree(ah);
1da177e4 684 }
943c246e
RD
685
686 spin_unlock_irqrestore(&priv->lock, flags);
687 netif_tx_unlock_bh(dev);
1da177e4
LT
688}
689
c4028958 690void ipoib_reap_ah(struct work_struct *work)
1da177e4 691{
c4028958
DH
692 struct ipoib_dev_priv *priv =
693 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
694 struct net_device *dev = priv->dev;
1da177e4
LT
695
696 __ipoib_reap_ah(dev);
697
698 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
0b39578b 699 queue_delayed_work(priv->wq, &priv->ah_reap_task,
69fc507a 700 round_jiffies_relative(HZ));
1da177e4
LT
701}
702
efc82eee 703static void ipoib_flush_ah(struct net_device *dev)
e135106f 704{
c1048aff 705 struct ipoib_dev_priv *priv = ipoib_priv(dev);
e135106f
DL
706
707 cancel_delayed_work(&priv->ah_reap_task);
efc82eee 708 flush_workqueue(priv->wq);
e135106f
DL
709 ipoib_reap_ah(&priv->ah_reap_task.work);
710}
711
efc82eee 712static void ipoib_stop_ah(struct net_device *dev)
e135106f 713{
c1048aff 714 struct ipoib_dev_priv *priv = ipoib_priv(dev);
e135106f
DL
715
716 set_bit(IPOIB_STOP_REAPER, &priv->flags);
efc82eee 717 ipoib_flush_ah(dev);
e135106f
DL
718}
719
7ce1a3ee 720static int recvs_pending(struct net_device *dev)
57ce41d1 721{
c1048aff 722 struct ipoib_dev_priv *priv = ipoib_priv(dev);
7ce1a3ee
ES
723 int pending = 0;
724 int i;
725
726 for (i = 0; i < ipoib_recvq_size; ++i)
727 if (priv->rx_ring[i].skb)
728 ++pending;
729
730 return pending;
57ce41d1
EC
731}
732
5dc78ad1
ES
733static void check_qp_movement_and_print(struct ipoib_dev_priv *priv,
734 struct ib_qp *qp,
735 enum ib_qp_state new_state)
736{
737 struct ib_qp_attr qp_attr;
738 struct ib_qp_init_attr query_init_attr;
739 int ret;
740
741 ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr);
742 if (ret) {
743 ipoib_warn(priv, "%s: Failed to query QP\n", __func__);
744 return;
745 }
746 /* print according to the new-state and the previous state.*/
747 if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET)
748 ipoib_dbg(priv, "Failed modify QP, IB_QPS_RESET to IB_QPS_ERR, acceptable\n");
749 else
750 ipoib_warn(priv, "Failed to modify QP to state: %d from state: %d\n",
751 new_state, qp_attr.qp_state);
752}
753
8966e28d
ES
754static void ipoib_napi_enable(struct net_device *dev)
755{
756 struct ipoib_dev_priv *priv = ipoib_priv(dev);
757
758 napi_enable(&priv->recv_napi);
759 napi_enable(&priv->send_napi);
760}
761
762static void ipoib_napi_disable(struct net_device *dev)
763{
764 struct ipoib_dev_priv *priv = ipoib_priv(dev);
765
766 napi_disable(&priv->recv_napi);
767 napi_disable(&priv->send_napi);
768}
769
7ce1a3ee 770int ipoib_ib_dev_stop_default(struct net_device *dev)
1da177e4 771{
c1048aff 772 struct ipoib_dev_priv *priv = ipoib_priv(dev);
7ce1a3ee
ES
773 struct ib_qp_attr qp_attr;
774 unsigned long begin;
775 struct ipoib_tx_buf *tx_req;
776 int i;
1da177e4 777
cd565b4b 778 if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
8966e28d 779 ipoib_napi_disable(dev);
dd57c930 780
7ce1a3ee
ES
781 ipoib_cm_dev_stop(dev);
782
783 /*
784 * Move our QP to the error state and then reinitialize in
785 * when all work requests have completed or have been flushed.
786 */
787 qp_attr.qp_state = IB_QPS_ERR;
788 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
5dc78ad1 789 check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR);
7ce1a3ee
ES
790
791 /* Wait for all sends and receives to complete */
792 begin = jiffies;
793
794 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
795 if (time_after(jiffies, begin + 5 * HZ)) {
796 ipoib_warn(priv,
797 "timing out; %d sends %d receives not completed\n",
798 priv->tx_head - priv->tx_tail,
799 recvs_pending(dev));
800
801 /*
802 * assume the HW is wedged and just free up
803 * all our pending work requests.
804 */
805 while ((int)priv->tx_tail - (int)priv->tx_head < 0) {
806 tx_req = &priv->tx_ring[priv->tx_tail &
807 (ipoib_sendq_size - 1)];
808 ipoib_dma_unmap_tx(priv, tx_req);
809 dev_kfree_skb_any(tx_req->skb);
810 ++priv->tx_tail;
7ce1a3ee
ES
811 }
812
813 for (i = 0; i < ipoib_recvq_size; ++i) {
814 struct ipoib_rx_buf *rx_req;
815
816 rx_req = &priv->rx_ring[i];
817 if (!rx_req->skb)
818 continue;
819 ipoib_ud_dma_unmap_rx(priv,
820 priv->rx_ring[i].mapping);
821 dev_kfree_skb_any(rx_req->skb);
822 rx_req->skb = NULL;
823 }
824
825 goto timeout;
826 }
827
828 ipoib_drain_cq(dev);
829
98e77d9f 830 usleep_range(1000, 2000);
26bbf13c 831 }
26bbf13c 832
7ce1a3ee
ES
833 ipoib_dbg(priv, "All sends and receives done.\n");
834
835timeout:
7ce1a3ee
ES
836 qp_attr.qp_state = IB_QPS_RESET;
837 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
838 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
839
840 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
841
842 return 0;
843}
844
845int ipoib_ib_dev_stop(struct net_device *dev)
846{
cd565b4b
ES
847 struct ipoib_dev_priv *priv = ipoib_priv(dev);
848
849 priv->rn_ops->ndo_stop(dev);
7ce1a3ee 850
cd565b4b 851 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
7ce1a3ee
ES
852 ipoib_flush_ah(dev);
853
854 return 0;
855}
856
7ce1a3ee
ES
857int ipoib_ib_dev_open_default(struct net_device *dev)
858{
c1048aff 859 struct ipoib_dev_priv *priv = ipoib_priv(dev);
7ce1a3ee
ES
860 int ret;
861
5b6810e0 862 ret = ipoib_init_qp(dev);
1da177e4 863 if (ret) {
5b6810e0 864 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
1da177e4
LT
865 return -1;
866 }
867
868 ret = ipoib_ib_post_receives(dev);
869 if (ret) {
870 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
cd565b4b 871 goto out;
1da177e4
LT
872 }
873
839fcaba
MT
874 ret = ipoib_cm_dev_open(dev);
875 if (ret) {
24bd1e4e 876 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
cd565b4b 877 goto out;
839fcaba
MT
878 }
879
cd565b4b 880 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
8966e28d 881 ipoib_napi_enable(dev);
7a343d4c 882
1da177e4 883 return 0;
cd565b4b 884out:
c2bb5628 885 return -1;
1da177e4
LT
886}
887
7ce1a3ee
ES
888int ipoib_ib_dev_open(struct net_device *dev)
889{
c1048aff 890 struct ipoib_dev_priv *priv = ipoib_priv(dev);
7ce1a3ee
ES
891
892 ipoib_pkey_dev_check_presence(dev);
893
894 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
895 ipoib_warn(priv, "P_Key 0x%04x is %s\n", priv->pkey,
896 (!(priv->pkey & 0x7fff) ? "Invalid" : "not found"));
897 return -1;
898 }
899
900 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
901 queue_delayed_work(priv->wq, &priv->ah_reap_task,
902 round_jiffies_relative(HZ));
903
cd565b4b 904 if (priv->rn_ops->ndo_open(dev)) {
7ce1a3ee 905 pr_warn("%s: Failed to open dev\n", dev->name);
cd565b4b 906 goto dev_stop;
7ce1a3ee
ES
907 }
908
cd565b4b
ES
909 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
910
7ce1a3ee
ES
911 return 0;
912
cd565b4b 913dev_stop:
7ce1a3ee
ES
914 set_bit(IPOIB_STOP_REAPER, &priv->flags);
915 cancel_delayed_work(&priv->ah_reap_task);
cd565b4b 916 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
cd565b4b 917 ipoib_ib_dev_stop(dev);
7ce1a3ee
ES
918 return -1;
919}
920
db84f880 921void ipoib_pkey_dev_check_presence(struct net_device *dev)
7a343d4c 922{
c1048aff 923 struct ipoib_dev_priv *priv = ipoib_priv(dev);
980f91c3 924 struct rdma_netdev *rn = netdev_priv(dev);
7a343d4c 925
dd57c930
AE
926 if (!(priv->pkey & 0x7fff) ||
927 ib_find_pkey(priv->ca, priv->port, priv->pkey,
980f91c3 928 &priv->pkey_index)) {
7a343d4c 929 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
980f91c3
AV
930 } else {
931 if (rn->set_id)
932 rn->set_id(dev, priv->pkey_index);
7a343d4c 933 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
980f91c3 934 }
7a343d4c
LA
935}
936
5c37077f 937void ipoib_ib_dev_up(struct net_device *dev)
1da177e4 938{
c1048aff 939 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 940
7a343d4c
LA
941 ipoib_pkey_dev_check_presence(dev);
942
943 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
944 ipoib_dbg(priv, "PKEY is not assigned.\n");
5c37077f 945 return;
7a343d4c
LA
946 }
947
1da177e4
LT
948 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
949
5c37077f 950 ipoib_mcast_start_thread(dev);
1da177e4
LT
951}
952
dfc0e555 953void ipoib_ib_dev_down(struct net_device *dev)
1da177e4 954{
c1048aff 955 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
956
957 ipoib_dbg(priv, "downing ib_dev\n");
958
959 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
960 netif_carrier_off(dev);
961
efc82eee 962 ipoib_mcast_stop_thread(dev);
1da177e4
LT
963 ipoib_mcast_dev_flush(dev);
964
1da177e4 965 ipoib_flush_paths(dev);
1da177e4
LT
966}
967
2dfbfc37
MT
968void ipoib_drain_cq(struct net_device *dev)
969{
c1048aff 970 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2dfbfc37 971 int i, n;
943c246e
RD
972
973 /*
974 * We call completion handling routines that expect to be
975 * called from the BH-disabled NAPI poll context, so disable
976 * BHs here too.
977 */
978 local_bh_disable();
979
2dfbfc37 980 do {
f56bcd80 981 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
2dfbfc37 982 for (i = 0; i < n; ++i) {
ce423ef5
RD
983 /*
984 * Convert any successful completions to flush
985 * errors to avoid passing packets up the
986 * stack after bringing the device down.
987 */
988 if (priv->ibwc[i].status == IB_WC_SUCCESS)
989 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
990
1b524963
MT
991 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
992 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
993 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
994 else
995 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
8966e28d
ES
996 } else {
997 pr_warn("%s: Got unexpected wqe id\n", __func__);
998 }
2dfbfc37
MT
999 }
1000 } while (n == IPOIB_NUM_WC);
f56bcd80
EC
1001
1002 while (poll_tx(priv))
1003 ; /* nothing */
943c246e
RD
1004
1005 local_bh_enable();
2dfbfc37
MT
1006}
1007
c2904141
ES
1008/*
1009 * Takes whatever value which is in pkey index 0 and updates priv->pkey
1010 * returns 0 if the pkey value was changed.
1011 */
1012static inline int update_parent_pkey(struct ipoib_dev_priv *priv)
1013{
1014 int result;
1015 u16 prev_pkey;
1016
1017 prev_pkey = priv->pkey;
1018 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1019 if (result) {
1020 ipoib_warn(priv, "ib_query_pkey port %d failed (ret = %d)\n",
1021 priv->port, result);
1022 return result;
1023 }
1024
1025 priv->pkey |= 0x8000;
1026
1027 if (prev_pkey != priv->pkey) {
1028 ipoib_dbg(priv, "pkey changed from 0x%x to 0x%x\n",
1029 prev_pkey, priv->pkey);
1030 /*
1031 * Update the pkey in the broadcast address, while making sure to set
1032 * the full membership bit, so that we join the right broadcast group.
1033 */
1034 priv->dev->broadcast[8] = priv->pkey >> 8;
1035 priv->dev->broadcast[9] = priv->pkey & 0xff;
1036 return 0;
1037 }
1038
1039 return 1;
1040}
dd57c930
AE
1041/*
1042 * returns 0 if pkey value was found in a different slot.
1043 */
1044static inline int update_child_pkey(struct ipoib_dev_priv *priv)
1045{
1046 u16 old_index = priv->pkey_index;
1047
1048 priv->pkey_index = 0;
1049 ipoib_pkey_dev_check_presence(priv->dev);
1050
1051 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
1052 (old_index == priv->pkey_index))
1053 return 1;
1054 return 0;
1055}
c2904141 1056
492a7e67
MB
1057/*
1058 * returns true if the device address of the ipoib interface has changed and the
1059 * new address is a valid one (i.e in the gid table), return false otherwise.
1060 */
1061static bool ipoib_dev_addr_changed_valid(struct ipoib_dev_priv *priv)
1062{
1063 union ib_gid search_gid;
1064 union ib_gid gid0;
1065 union ib_gid *netdev_gid;
1066 int err;
1067 u16 index;
1068 u8 port;
1069 bool ret = false;
1070
1071 netdev_gid = (union ib_gid *)(priv->dev->dev_addr + 4);
1072 if (ib_query_gid(priv->ca, priv->port, 0, &gid0, NULL))
1073 return false;
1074
9b29953b 1075 netif_addr_lock_bh(priv->dev);
492a7e67
MB
1076
1077 /* The subnet prefix may have changed, update it now so we won't have
1078 * to do it later
1079 */
1080 priv->local_gid.global.subnet_prefix = gid0.global.subnet_prefix;
1081 netdev_gid->global.subnet_prefix = gid0.global.subnet_prefix;
1082 search_gid.global.subnet_prefix = gid0.global.subnet_prefix;
1083
1084 search_gid.global.interface_id = priv->local_gid.global.interface_id;
1085
9b29953b 1086 netif_addr_unlock_bh(priv->dev);
492a7e67
MB
1087
1088 err = ib_find_gid(priv->ca, &search_gid, IB_GID_TYPE_IB,
1089 priv->dev, &port, &index);
1090
9b29953b 1091 netif_addr_lock_bh(priv->dev);
492a7e67
MB
1092
1093 if (search_gid.global.interface_id !=
1094 priv->local_gid.global.interface_id)
1095 /* There was a change while we were looking up the gid, bail
1096 * here and let the next work sort this out
1097 */
1098 goto out;
1099
1100 /* The next section of code needs some background:
1101 * Per IB spec the port GUID can't change if the HCA is powered on.
1102 * port GUID is the basis for GID at index 0 which is the basis for
1103 * the default device address of a ipoib interface.
1104 *
1105 * so it seems the flow should be:
1106 * if user_changed_dev_addr && gid in gid tbl
1107 * set bit dev_addr_set
1108 * return true
1109 * else
1110 * return false
1111 *
1112 * The issue is that there are devices that don't follow the spec,
1113 * they change the port GUID when the HCA is powered, so in order
1114 * not to break userspace applications, We need to check if the
1115 * user wanted to control the device address and we assume that
1116 * if he sets the device address back to be based on GID index 0,
1117 * he no longer wishs to control it.
1118 *
1119 * If the user doesn't control the the device address,
1120 * IPOIB_FLAG_DEV_ADDR_SET is set and ib_find_gid failed it means
1121 * the port GUID has changed and GID at index 0 has changed
1122 * so we need to change priv->local_gid and priv->dev->dev_addr
1123 * to reflect the new GID.
1124 */
1125 if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
1126 if (!err && port == priv->port) {
1127 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1128 if (index == 0)
1129 clear_bit(IPOIB_FLAG_DEV_ADDR_CTRL,
1130 &priv->flags);
1131 else
1132 set_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags);
1133 ret = true;
1134 } else {
1135 ret = false;
1136 }
1137 } else {
1138 if (!err && port == priv->port) {
1139 ret = true;
1140 } else {
1141 if (!test_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags)) {
1142 memcpy(&priv->local_gid, &gid0,
1143 sizeof(priv->local_gid));
1144 memcpy(priv->dev->dev_addr + 4, &gid0,
1145 sizeof(priv->local_gid));
1146 ret = true;
1147 }
1148 }
1149 }
1150
1151out:
9b29953b 1152 netif_addr_unlock_bh(priv->dev);
492a7e67
MB
1153
1154 return ret;
1155}
1156
ee1e2c82 1157static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
8b7cce0d
HE
1158 enum ipoib_flush_level level,
1159 int nesting)
1da177e4 1160{
26bbf13c 1161 struct ipoib_dev_priv *cpriv;
c4028958 1162 struct net_device *dev = priv->dev;
c2904141 1163 int result;
26bbf13c 1164
8b7cce0d 1165 down_read_nested(&priv->vlan_rwsem, nesting);
1da177e4 1166
26bbf13c
YE
1167 /*
1168 * Flush any child interfaces too -- they might be up even if
1169 * the parent is down.
1170 */
1171 list_for_each_entry(cpriv, &priv->child_intfs, list)
8b7cce0d 1172 __ipoib_ib_dev_flush(cpriv, level, nesting + 1);
26bbf13c 1173
f47944cc 1174 up_read(&priv->vlan_rwsem);
26bbf13c 1175
dd57c930
AE
1176 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) &&
1177 level != IPOIB_FLUSH_HEAVY) {
492a7e67
MB
1178 /* Make sure the dev_addr is set even if not flushing */
1179 if (level == IPOIB_FLUSH_LIGHT)
1180 ipoib_dev_addr_changed_valid(priv);
7a343d4c
LA
1181 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
1182 return;
1183 }
1184
1185 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
dd57c930
AE
1186 /* interface is down. update pkey and leave. */
1187 if (level == IPOIB_FLUSH_HEAVY) {
1188 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1189 update_parent_pkey(priv);
1190 else
1191 update_child_pkey(priv);
492a7e67
MB
1192 } else if (level == IPOIB_FLUSH_LIGHT)
1193 ipoib_dev_addr_changed_valid(priv);
7a343d4c 1194 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
1da177e4 1195 return;
7a343d4c 1196 }
1da177e4 1197
ee1e2c82 1198 if (level == IPOIB_FLUSH_HEAVY) {
c2904141
ES
1199 /* child devices chase their origin pkey value, while non-child
1200 * (parent) devices should always takes what present in pkey index 0
1201 */
1202 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
dd57c930
AE
1203 result = update_child_pkey(priv);
1204 if (result) {
1205 /* restart QP only if P_Key index is changed */
c2904141 1206 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
9fdd5e5b 1207 return;
c2904141 1208 }
dd57c930 1209
c2904141
ES
1210 } else {
1211 result = update_parent_pkey(priv);
1212 /* restart QP only if P_Key value changed */
1213 if (result) {
1214 ipoib_dbg(priv, "Not flushing - P_Key value not changed.\n");
1215 return;
1216 }
26bbf13c 1217 }
26bbf13c
YE
1218 }
1219
ee1e2c82 1220 if (level == IPOIB_FLUSH_LIGHT) {
344bacca 1221 int oper_up;
ee1e2c82 1222 ipoib_mark_paths_invalid(dev);
344bacca
AV
1223 /* Set IPoIB operation as down to prevent races between:
1224 * the flush flow which leaves MCG and on the fly joins
1225 * which can happen during that time. mcast restart task
1226 * should deal with join requests we missed.
1227 */
1228 oper_up = test_and_clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
ee1e2c82 1229 ipoib_mcast_dev_flush(dev);
344bacca
AV
1230 if (oper_up)
1231 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
efc82eee 1232 ipoib_flush_ah(dev);
ee1e2c82 1233 }
1da177e4 1234
ee1e2c82 1235 if (level >= IPOIB_FLUSH_NORMAL)
efc82eee 1236 ipoib_ib_dev_down(dev);
1da177e4 1237
ee1e2c82 1238 if (level == IPOIB_FLUSH_HEAVY) {
dd57c930 1239 if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
efc82eee 1240 ipoib_ib_dev_stop(dev);
b4b678b0 1241
1f80bd6a 1242 if (ipoib_ib_dev_open(dev))
dd57c930 1243 return;
b4b678b0 1244
dd57c930
AE
1245 if (netif_queue_stopped(dev))
1246 netif_start_queue(dev);
26bbf13c
YE
1247 }
1248
1da177e4
LT
1249 /*
1250 * The device could have been brought down between the start and when
1251 * we get here, don't bring it back up if it's not configured up
1252 */
5ccd0255 1253 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
ee1e2c82
MS
1254 if (level >= IPOIB_FLUSH_NORMAL)
1255 ipoib_ib_dev_up(dev);
492a7e67
MB
1256 if (ipoib_dev_addr_changed_valid(priv))
1257 ipoib_mcast_restart_task(&priv->restart_task);
5ccd0255 1258 }
26bbf13c 1259}
1da177e4 1260
ee1e2c82
MS
1261void ipoib_ib_dev_flush_light(struct work_struct *work)
1262{
1263 struct ipoib_dev_priv *priv =
1264 container_of(work, struct ipoib_dev_priv, flush_light);
1265
8b7cce0d 1266 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT, 0);
ee1e2c82
MS
1267}
1268
1269void ipoib_ib_dev_flush_normal(struct work_struct *work)
26bbf13c
YE
1270{
1271 struct ipoib_dev_priv *priv =
ee1e2c82 1272 container_of(work, struct ipoib_dev_priv, flush_normal);
4f71055a 1273
8b7cce0d 1274 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL, 0);
26bbf13c 1275}
4f71055a 1276
ee1e2c82 1277void ipoib_ib_dev_flush_heavy(struct work_struct *work)
26bbf13c
YE
1278{
1279 struct ipoib_dev_priv *priv =
ee1e2c82 1280 container_of(work, struct ipoib_dev_priv, flush_heavy);
26bbf13c 1281
1f80bd6a 1282 rtnl_lock();
8b7cce0d 1283 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY, 0);
1f80bd6a 1284 rtnl_unlock();
1da177e4
LT
1285}
1286
1287void ipoib_ib_dev_cleanup(struct net_device *dev)
1288{
c1048aff 1289 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
1290
1291 ipoib_dbg(priv, "cleaning up ib_dev\n");
a39c52ab
ES
1292 /*
1293 * We must make sure there are no more (path) completions
1294 * that may wish to touch priv fields that are no longer valid
1295 */
1296 ipoib_flush_paths(dev);
1da177e4 1297
efc82eee 1298 ipoib_mcast_stop_thread(dev);
988bd503 1299 ipoib_mcast_dev_flush(dev);
1da177e4 1300
e135106f
DL
1301 /*
1302 * All of our ah references aren't free until after
1303 * ipoib_mcast_dev_flush(), ipoib_flush_paths, and
1304 * the neighbor garbage collection is stopped and reaped.
1305 * That should all be done now, so make a final ah flush.
1306 */
efc82eee 1307 ipoib_stop_ah(dev);
e135106f 1308
515ed4f3
ES
1309 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
1310
cd565b4b 1311 priv->rn_ops->ndo_uninit(dev);
1da177e4 1312
515ed4f3
ES
1313 if (priv->pd) {
1314 ib_dealloc_pd(priv->pd);
1315 priv->pd = NULL;
1316 }
1317}
1da177e4 1318