Linux 4.19.133
[linux/fpc-iii.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
blob0f2e80f54d33309c26d77d9debe5a118651ee423
1 /*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
36 #include <linux/delay.h>
37 #include <linux/moduleparam.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
41 #include <linux/ip.h>
42 #include <linux/tcp.h>
43 #include <rdma/ib_cache.h>
45 #include "ipoib.h"
47 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
48 static int data_debug_level;
50 module_param(data_debug_level, int, 0644);
51 MODULE_PARM_DESC(data_debug_level,
52 "Enable data path debug tracing if > 0");
53 #endif
55 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
56 struct ib_pd *pd, struct rdma_ah_attr *attr)
58 struct ipoib_ah *ah;
59 struct ib_ah *vah;
61 ah = kmalloc(sizeof(*ah), GFP_KERNEL);
62 if (!ah)
63 return ERR_PTR(-ENOMEM);
65 ah->dev = dev;
66 ah->last_send = 0;
67 kref_init(&ah->ref);
69 vah = rdma_create_ah(pd, attr);
70 if (IS_ERR(vah)) {
71 kfree(ah);
72 ah = (struct ipoib_ah *)vah;
73 } else {
74 ah->ah = vah;
75 ipoib_dbg(ipoib_priv(dev), "Created ah %p\n", ah->ah);
78 return ah;
81 void ipoib_free_ah(struct kref *kref)
83 struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
84 struct ipoib_dev_priv *priv = ipoib_priv(ah->dev);
86 unsigned long flags;
88 spin_lock_irqsave(&priv->lock, flags);
89 list_add_tail(&ah->list, &priv->dead_ahs);
90 spin_unlock_irqrestore(&priv->lock, flags);
93 static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
94 u64 mapping[IPOIB_UD_RX_SG])
96 ib_dma_unmap_single(priv->ca, mapping[0],
97 IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
98 DMA_FROM_DEVICE);
101 static int ipoib_ib_post_receive(struct net_device *dev, int id)
103 struct ipoib_dev_priv *priv = ipoib_priv(dev);
104 int ret;
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];
111 ret = ib_post_recv(priv->qp, &priv->rx_wr, NULL);
112 if (unlikely(ret)) {
113 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
114 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
115 dev_kfree_skb_any(priv->rx_ring[id].skb);
116 priv->rx_ring[id].skb = NULL;
119 return ret;
122 static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
124 struct ipoib_dev_priv *priv = ipoib_priv(dev);
125 struct sk_buff *skb;
126 int buf_size;
127 u64 *mapping;
129 buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
131 skb = dev_alloc_skb(buf_size + IPOIB_HARD_LEN);
132 if (unlikely(!skb))
133 return NULL;
136 * the IP header will be at IPOIP_HARD_LEN + IB_GRH_BYTES, that is
137 * 64 bytes aligned
139 skb_reserve(skb, sizeof(struct ipoib_pseudo_header));
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;
147 priv->rx_ring[id].skb = skb;
148 return skb;
149 error:
150 dev_kfree_skb_any(skb);
151 return NULL;
154 static int ipoib_ib_post_receives(struct net_device *dev)
156 struct ipoib_dev_priv *priv = ipoib_priv(dev);
157 int i;
159 for (i = 0; i < ipoib_recvq_size; ++i) {
160 if (!ipoib_alloc_rx_skb(dev, i)) {
161 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
162 return -ENOMEM;
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;
170 return 0;
173 static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
175 struct ipoib_dev_priv *priv = ipoib_priv(dev);
176 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
177 struct sk_buff *skb;
178 u64 mapping[IPOIB_UD_RX_SG];
179 union ib_gid *dgid;
180 union ib_gid *sgid;
182 ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
183 wr_id, wc->status);
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;
191 skb = priv->rx_ring[wr_id].skb;
193 if (unlikely(wc->status != IB_WC_SUCCESS)) {
194 if (wc->status != IB_WC_WR_FLUSH_ERR)
195 ipoib_warn(priv,
196 "failed recv event (status=%d, wrid=%d vend_err %#x)\n",
197 wc->status, wr_id, wc->vendor_err);
198 ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
199 dev_kfree_skb_any(skb);
200 priv->rx_ring[wr_id].skb = NULL;
201 return;
204 memcpy(mapping, priv->rx_ring[wr_id].mapping,
205 IPOIB_UD_RX_SG * sizeof(*mapping));
208 * If we can't allocate a new RX buffer, dump
209 * this packet and reuse the old buffer.
211 if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
212 ++dev->stats.rx_dropped;
213 goto repost;
216 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
217 wc->byte_len, wc->slid);
219 ipoib_ud_dma_unmap_rx(priv, mapping);
221 skb_put(skb, wc->byte_len);
223 /* First byte of dgid signals multicast when 0xff */
224 dgid = &((struct ib_grh *)skb->data)->dgid;
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;
233 sgid = &((struct ib_grh *)skb->data)->sgid;
236 * Drop packets that this interface sent, ie multicast packets
237 * that the HCA has replicated.
239 if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num) {
240 int need_repost = 1;
242 if ((wc->wc_flags & IB_WC_GRH) &&
243 sgid->global.interface_id != priv->local_gid.global.interface_id)
244 need_repost = 0;
246 if (need_repost) {
247 dev_kfree_skb_any(skb);
248 goto repost;
252 skb_pull(skb, IB_GRH_BYTES);
254 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
255 skb_add_pseudo_hdr(skb);
257 ++dev->stats.rx_packets;
258 dev->stats.rx_bytes += skb->len;
259 if (skb->pkt_type == PACKET_MULTICAST)
260 dev->stats.multicast++;
262 skb->dev = dev;
263 if ((dev->features & NETIF_F_RXCSUM) &&
264 likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
265 skb->ip_summed = CHECKSUM_UNNECESSARY;
267 napi_gro_receive(&priv->recv_napi, skb);
269 repost:
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);
275 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
277 struct sk_buff *skb = tx_req->skb;
278 u64 *mapping = tx_req->mapping;
279 int i;
280 int off;
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;
288 off = 1;
289 } else
290 off = 0;
292 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
293 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
294 mapping[i + off] = ib_dma_map_page(ca,
295 skb_frag_page(frag),
296 frag->page_offset, skb_frag_size(frag),
297 DMA_TO_DEVICE);
298 if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
299 goto partial_error;
301 return 0;
303 partial_error:
304 for (; i > 0; --i) {
305 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
307 ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
310 if (off)
311 ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
313 return -EIO;
316 void ipoib_dma_unmap_tx(struct ipoib_dev_priv *priv,
317 struct ipoib_tx_buf *tx_req)
319 struct sk_buff *skb = tx_req->skb;
320 u64 *mapping = tx_req->mapping;
321 int i;
322 int off;
324 if (skb_headlen(skb)) {
325 ib_dma_unmap_single(priv->ca, mapping[0], skb_headlen(skb),
326 DMA_TO_DEVICE);
327 off = 1;
328 } else
329 off = 0;
331 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
332 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
334 ib_dma_unmap_page(priv->ca, mapping[i + off],
335 skb_frag_size(frag), DMA_TO_DEVICE);
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.
344 static void ipoib_qp_state_validate_work(struct work_struct *work)
346 struct ipoib_qp_state_validate *qp_work =
347 container_of(work, struct ipoib_qp_state_validate, work);
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;
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;
360 pr_info("%s: QP: 0x%x is in state: %d\n",
361 __func__, priv->qp->qp_num, qp_attr.qp_state);
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;
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;
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);
380 free_res:
381 kfree(qp_work);
384 static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
386 struct ipoib_dev_priv *priv = ipoib_priv(dev);
387 unsigned int wr_id = wc->wr_id;
388 struct ipoib_tx_buf *tx_req;
390 ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
391 wr_id, wc->status);
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;
399 tx_req = &priv->tx_ring[wr_id];
401 ipoib_dma_unmap_tx(priv, tx_req);
403 ++dev->stats.tx_packets;
404 dev->stats.tx_bytes += tx_req->skb->len;
406 dev_kfree_skb_any(tx_req->skb);
408 ++priv->tx_tail;
409 ++priv->global_tx_tail;
411 if (unlikely(netif_queue_stopped(dev) &&
412 ((priv->global_tx_head - priv->global_tx_tail) <=
413 ipoib_sendq_size >> 1) &&
414 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)))
415 netif_wake_queue(dev);
417 if (wc->status != IB_WC_SUCCESS &&
418 wc->status != IB_WC_WR_FLUSH_ERR) {
419 struct ipoib_qp_state_validate *qp_work;
420 ipoib_warn(priv,
421 "failed send event (status=%d, wrid=%d vend_err %#x)\n",
422 wc->status, wr_id, wc->vendor_err);
423 qp_work = kzalloc(sizeof(*qp_work), GFP_ATOMIC);
424 if (!qp_work)
425 return;
427 INIT_WORK(&qp_work->work, ipoib_qp_state_validate_work);
428 qp_work->priv = priv;
429 queue_work(priv->wq, &qp_work->work);
433 static int poll_tx(struct ipoib_dev_priv *priv)
435 int n, i;
436 struct ib_wc *wc;
438 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
439 for (i = 0; i < n; ++i) {
440 wc = priv->send_wc + i;
441 if (wc->wr_id & IPOIB_OP_CM)
442 ipoib_cm_handle_tx_wc(priv->dev, priv->send_wc + i);
443 else
444 ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
446 return n == MAX_SEND_CQE;
449 int ipoib_rx_poll(struct napi_struct *napi, int budget)
451 struct ipoib_dev_priv *priv =
452 container_of(napi, struct ipoib_dev_priv, recv_napi);
453 struct net_device *dev = priv->dev;
454 int done;
455 int t;
456 int n, i;
458 done = 0;
460 poll_more:
461 while (done < budget) {
462 int max = (budget - done);
464 t = min(IPOIB_NUM_WC, max);
465 n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
467 for (i = 0; i < n; i++) {
468 struct ib_wc *wc = priv->ibwc + i;
470 if (wc->wr_id & IPOIB_OP_RECV) {
471 ++done;
472 if (wc->wr_id & IPOIB_OP_CM)
473 ipoib_cm_handle_rx_wc(dev, wc);
474 else
475 ipoib_ib_handle_rx_wc(dev, wc);
476 } else {
477 pr_warn("%s: Got unexpected wqe id\n", __func__);
481 if (n != t)
482 break;
485 if (done < budget) {
486 napi_complete(napi);
487 if (unlikely(ib_req_notify_cq(priv->recv_cq,
488 IB_CQ_NEXT_COMP |
489 IB_CQ_REPORT_MISSED_EVENTS)) &&
490 napi_reschedule(napi))
491 goto poll_more;
494 return done;
497 int ipoib_tx_poll(struct napi_struct *napi, int budget)
499 struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv,
500 send_napi);
501 struct net_device *dev = priv->dev;
502 int n, i;
503 struct ib_wc *wc;
505 poll_more:
506 n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
508 for (i = 0; i < n; i++) {
509 wc = priv->send_wc + i;
510 if (wc->wr_id & IPOIB_OP_CM)
511 ipoib_cm_handle_tx_wc(dev, wc);
512 else
513 ipoib_ib_handle_tx_wc(dev, wc);
516 if (n < budget) {
517 napi_complete(napi);
518 if (unlikely(ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
519 IB_CQ_REPORT_MISSED_EVENTS)) &&
520 napi_reschedule(napi))
521 goto poll_more;
523 return n < 0 ? 0 : n;
526 void ipoib_ib_rx_completion(struct ib_cq *cq, void *ctx_ptr)
528 struct ipoib_dev_priv *priv = ctx_ptr;
530 napi_schedule(&priv->recv_napi);
533 void ipoib_ib_tx_completion(struct ib_cq *cq, void *ctx_ptr)
535 struct ipoib_dev_priv *priv = ctx_ptr;
537 napi_schedule(&priv->send_napi);
540 static inline int post_send(struct ipoib_dev_priv *priv,
541 unsigned int wr_id,
542 struct ib_ah *address, u32 dqpn,
543 struct ipoib_tx_buf *tx_req,
544 void *head, int hlen)
546 struct sk_buff *skb = tx_req->skb;
548 ipoib_build_sge(priv, tx_req);
550 priv->tx_wr.wr.wr_id = wr_id;
551 priv->tx_wr.remote_qpn = dqpn;
552 priv->tx_wr.ah = address;
554 if (head) {
555 priv->tx_wr.mss = skb_shinfo(skb)->gso_size;
556 priv->tx_wr.header = head;
557 priv->tx_wr.hlen = hlen;
558 priv->tx_wr.wr.opcode = IB_WR_LSO;
559 } else
560 priv->tx_wr.wr.opcode = IB_WR_SEND;
562 return ib_post_send(priv->qp, &priv->tx_wr.wr, NULL);
565 int ipoib_send(struct net_device *dev, struct sk_buff *skb,
566 struct ib_ah *address, u32 dqpn)
568 struct ipoib_dev_priv *priv = ipoib_priv(dev);
569 struct ipoib_tx_buf *tx_req;
570 int hlen, rc;
571 void *phead;
572 unsigned int usable_sge = priv->max_send_sge - !!skb_headlen(skb);
574 if (skb_is_gso(skb)) {
575 hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
576 phead = skb->data;
577 if (unlikely(!skb_pull(skb, hlen))) {
578 ipoib_warn(priv, "linear data too small\n");
579 ++dev->stats.tx_dropped;
580 ++dev->stats.tx_errors;
581 dev_kfree_skb_any(skb);
582 return -1;
584 } else {
585 if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
586 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
587 skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
588 ++dev->stats.tx_dropped;
589 ++dev->stats.tx_errors;
590 ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
591 return -1;
593 phead = NULL;
594 hlen = 0;
596 if (skb_shinfo(skb)->nr_frags > usable_sge) {
597 if (skb_linearize(skb) < 0) {
598 ipoib_warn(priv, "skb could not be linearized\n");
599 ++dev->stats.tx_dropped;
600 ++dev->stats.tx_errors;
601 dev_kfree_skb_any(skb);
602 return -1;
604 /* Does skb_linearize return ok without reducing nr_frags? */
605 if (skb_shinfo(skb)->nr_frags > usable_sge) {
606 ipoib_warn(priv, "too many frags after skb linearize\n");
607 ++dev->stats.tx_dropped;
608 ++dev->stats.tx_errors;
609 dev_kfree_skb_any(skb);
610 return -1;
614 ipoib_dbg_data(priv,
615 "sending packet, length=%d address=%p dqpn=0x%06x\n",
616 skb->len, address, dqpn);
619 * We put the skb into the tx_ring _before_ we call post_send()
620 * because it's entirely possible that the completion handler will
621 * run before we execute anything after the post_send(). That
622 * means we have to make sure everything is properly recorded and
623 * our state is consistent before we call post_send().
625 tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
626 tx_req->skb = skb;
627 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
628 ++dev->stats.tx_errors;
629 dev_kfree_skb_any(skb);
630 return -1;
633 if (skb->ip_summed == CHECKSUM_PARTIAL)
634 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
635 else
636 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
637 /* increase the tx_head after send success, but use it for queue state */
638 if ((priv->global_tx_head - priv->global_tx_tail) ==
639 ipoib_sendq_size - 1) {
640 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
641 netif_stop_queue(dev);
644 skb_orphan(skb);
645 skb_dst_drop(skb);
647 if (netif_queue_stopped(dev))
648 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
649 IB_CQ_REPORT_MISSED_EVENTS) < 0)
650 ipoib_warn(priv, "request notify on send CQ failed\n");
652 rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
653 address, dqpn, tx_req, phead, hlen);
654 if (unlikely(rc)) {
655 ipoib_warn(priv, "post_send failed, error %d\n", rc);
656 ++dev->stats.tx_errors;
657 ipoib_dma_unmap_tx(priv, tx_req);
658 dev_kfree_skb_any(skb);
659 if (netif_queue_stopped(dev))
660 netif_wake_queue(dev);
661 rc = 0;
662 } else {
663 netif_trans_update(dev);
665 rc = priv->tx_head;
666 ++priv->tx_head;
667 ++priv->global_tx_head;
669 return rc;
672 static void __ipoib_reap_ah(struct net_device *dev)
674 struct ipoib_dev_priv *priv = ipoib_priv(dev);
675 struct ipoib_ah *ah, *tah;
676 LIST_HEAD(remove_list);
677 unsigned long flags;
679 netif_tx_lock_bh(dev);
680 spin_lock_irqsave(&priv->lock, flags);
682 list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
683 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
684 list_del(&ah->list);
685 rdma_destroy_ah(ah->ah);
686 kfree(ah);
689 spin_unlock_irqrestore(&priv->lock, flags);
690 netif_tx_unlock_bh(dev);
693 void ipoib_reap_ah(struct work_struct *work)
695 struct ipoib_dev_priv *priv =
696 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
697 struct net_device *dev = priv->dev;
699 __ipoib_reap_ah(dev);
701 if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
702 queue_delayed_work(priv->wq, &priv->ah_reap_task,
703 round_jiffies_relative(HZ));
706 static void ipoib_flush_ah(struct net_device *dev)
708 struct ipoib_dev_priv *priv = ipoib_priv(dev);
710 cancel_delayed_work(&priv->ah_reap_task);
711 flush_workqueue(priv->wq);
712 ipoib_reap_ah(&priv->ah_reap_task.work);
715 static void ipoib_stop_ah(struct net_device *dev)
717 struct ipoib_dev_priv *priv = ipoib_priv(dev);
719 set_bit(IPOIB_STOP_REAPER, &priv->flags);
720 ipoib_flush_ah(dev);
723 static int recvs_pending(struct net_device *dev)
725 struct ipoib_dev_priv *priv = ipoib_priv(dev);
726 int pending = 0;
727 int i;
729 for (i = 0; i < ipoib_recvq_size; ++i)
730 if (priv->rx_ring[i].skb)
731 ++pending;
733 return pending;
736 static void check_qp_movement_and_print(struct ipoib_dev_priv *priv,
737 struct ib_qp *qp,
738 enum ib_qp_state new_state)
740 struct ib_qp_attr qp_attr;
741 struct ib_qp_init_attr query_init_attr;
742 int ret;
744 ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr);
745 if (ret) {
746 ipoib_warn(priv, "%s: Failed to query QP\n", __func__);
747 return;
749 /* print according to the new-state and the previous state.*/
750 if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET)
751 ipoib_dbg(priv, "Failed modify QP, IB_QPS_RESET to IB_QPS_ERR, acceptable\n");
752 else
753 ipoib_warn(priv, "Failed to modify QP to state: %d from state: %d\n",
754 new_state, qp_attr.qp_state);
757 static void ipoib_napi_enable(struct net_device *dev)
759 struct ipoib_dev_priv *priv = ipoib_priv(dev);
761 napi_enable(&priv->recv_napi);
762 napi_enable(&priv->send_napi);
765 static void ipoib_napi_disable(struct net_device *dev)
767 struct ipoib_dev_priv *priv = ipoib_priv(dev);
769 napi_disable(&priv->recv_napi);
770 napi_disable(&priv->send_napi);
773 int ipoib_ib_dev_stop_default(struct net_device *dev)
775 struct ipoib_dev_priv *priv = ipoib_priv(dev);
776 struct ib_qp_attr qp_attr;
777 unsigned long begin;
778 struct ipoib_tx_buf *tx_req;
779 int i;
781 if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
782 ipoib_napi_disable(dev);
784 ipoib_cm_dev_stop(dev);
787 * Move our QP to the error state and then reinitialize in
788 * when all work requests have completed or have been flushed.
790 qp_attr.qp_state = IB_QPS_ERR;
791 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
792 check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR);
794 /* Wait for all sends and receives to complete */
795 begin = jiffies;
797 while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
798 if (time_after(jiffies, begin + 5 * HZ)) {
799 ipoib_warn(priv,
800 "timing out; %d sends %d receives not completed\n",
801 priv->tx_head - priv->tx_tail,
802 recvs_pending(dev));
805 * assume the HW is wedged and just free up
806 * all our pending work requests.
808 while ((int)priv->tx_tail - (int)priv->tx_head < 0) {
809 tx_req = &priv->tx_ring[priv->tx_tail &
810 (ipoib_sendq_size - 1)];
811 ipoib_dma_unmap_tx(priv, tx_req);
812 dev_kfree_skb_any(tx_req->skb);
813 ++priv->tx_tail;
814 ++priv->global_tx_tail;
817 for (i = 0; i < ipoib_recvq_size; ++i) {
818 struct ipoib_rx_buf *rx_req;
820 rx_req = &priv->rx_ring[i];
821 if (!rx_req->skb)
822 continue;
823 ipoib_ud_dma_unmap_rx(priv,
824 priv->rx_ring[i].mapping);
825 dev_kfree_skb_any(rx_req->skb);
826 rx_req->skb = NULL;
829 goto timeout;
832 ipoib_drain_cq(dev);
834 usleep_range(1000, 2000);
837 ipoib_dbg(priv, "All sends and receives done.\n");
839 timeout:
840 qp_attr.qp_state = IB_QPS_RESET;
841 if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
842 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
844 ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
846 return 0;
849 int ipoib_ib_dev_stop(struct net_device *dev)
851 struct ipoib_dev_priv *priv = ipoib_priv(dev);
853 priv->rn_ops->ndo_stop(dev);
855 clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
856 ipoib_flush_ah(dev);
858 return 0;
861 int ipoib_ib_dev_open_default(struct net_device *dev)
863 struct ipoib_dev_priv *priv = ipoib_priv(dev);
864 int ret;
866 ret = ipoib_init_qp(dev);
867 if (ret) {
868 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
869 return -1;
872 ret = ipoib_ib_post_receives(dev);
873 if (ret) {
874 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
875 goto out;
878 ret = ipoib_cm_dev_open(dev);
879 if (ret) {
880 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
881 goto out;
884 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
885 ipoib_napi_enable(dev);
887 return 0;
888 out:
889 return -1;
892 int ipoib_ib_dev_open(struct net_device *dev)
894 struct ipoib_dev_priv *priv = ipoib_priv(dev);
896 ipoib_pkey_dev_check_presence(dev);
898 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
899 ipoib_warn(priv, "P_Key 0x%04x is %s\n", priv->pkey,
900 (!(priv->pkey & 0x7fff) ? "Invalid" : "not found"));
901 return -1;
904 clear_bit(IPOIB_STOP_REAPER, &priv->flags);
905 queue_delayed_work(priv->wq, &priv->ah_reap_task,
906 round_jiffies_relative(HZ));
908 if (priv->rn_ops->ndo_open(dev)) {
909 pr_warn("%s: Failed to open dev\n", dev->name);
910 goto dev_stop;
913 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
915 return 0;
917 dev_stop:
918 set_bit(IPOIB_STOP_REAPER, &priv->flags);
919 cancel_delayed_work(&priv->ah_reap_task);
920 set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
921 ipoib_ib_dev_stop(dev);
922 return -1;
925 void ipoib_pkey_dev_check_presence(struct net_device *dev)
927 struct ipoib_dev_priv *priv = ipoib_priv(dev);
928 struct rdma_netdev *rn = netdev_priv(dev);
930 if (!(priv->pkey & 0x7fff) ||
931 ib_find_pkey(priv->ca, priv->port, priv->pkey,
932 &priv->pkey_index)) {
933 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
934 } else {
935 if (rn->set_id)
936 rn->set_id(dev, priv->pkey_index);
937 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
941 void ipoib_ib_dev_up(struct net_device *dev)
943 struct ipoib_dev_priv *priv = ipoib_priv(dev);
945 ipoib_pkey_dev_check_presence(dev);
947 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
948 ipoib_dbg(priv, "PKEY is not assigned.\n");
949 return;
952 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
954 ipoib_mcast_start_thread(dev);
957 void ipoib_ib_dev_down(struct net_device *dev)
959 struct ipoib_dev_priv *priv = ipoib_priv(dev);
961 ipoib_dbg(priv, "downing ib_dev\n");
963 clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
964 netif_carrier_off(dev);
966 ipoib_mcast_stop_thread(dev);
967 ipoib_mcast_dev_flush(dev);
969 ipoib_flush_paths(dev);
972 void ipoib_drain_cq(struct net_device *dev)
974 struct ipoib_dev_priv *priv = ipoib_priv(dev);
975 int i, n;
978 * We call completion handling routines that expect to be
979 * called from the BH-disabled NAPI poll context, so disable
980 * BHs here too.
982 local_bh_disable();
984 do {
985 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
986 for (i = 0; i < n; ++i) {
988 * Convert any successful completions to flush
989 * errors to avoid passing packets up the
990 * stack after bringing the device down.
992 if (priv->ibwc[i].status == IB_WC_SUCCESS)
993 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
995 if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
996 if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
997 ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
998 else
999 ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
1000 } else {
1001 pr_warn("%s: Got unexpected wqe id\n", __func__);
1004 } while (n == IPOIB_NUM_WC);
1006 while (poll_tx(priv))
1007 ; /* nothing */
1009 local_bh_enable();
1013 * Takes whatever value which is in pkey index 0 and updates priv->pkey
1014 * returns 0 if the pkey value was changed.
1016 static inline int update_parent_pkey(struct ipoib_dev_priv *priv)
1018 int result;
1019 u16 prev_pkey;
1021 prev_pkey = priv->pkey;
1022 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1023 if (result) {
1024 ipoib_warn(priv, "ib_query_pkey port %d failed (ret = %d)\n",
1025 priv->port, result);
1026 return result;
1029 priv->pkey |= 0x8000;
1031 if (prev_pkey != priv->pkey) {
1032 ipoib_dbg(priv, "pkey changed from 0x%x to 0x%x\n",
1033 prev_pkey, priv->pkey);
1035 * Update the pkey in the broadcast address, while making sure to set
1036 * the full membership bit, so that we join the right broadcast group.
1038 priv->dev->broadcast[8] = priv->pkey >> 8;
1039 priv->dev->broadcast[9] = priv->pkey & 0xff;
1040 return 0;
1043 return 1;
1046 * returns 0 if pkey value was found in a different slot.
1048 static inline int update_child_pkey(struct ipoib_dev_priv *priv)
1050 u16 old_index = priv->pkey_index;
1052 priv->pkey_index = 0;
1053 ipoib_pkey_dev_check_presence(priv->dev);
1055 if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
1056 (old_index == priv->pkey_index))
1057 return 1;
1058 return 0;
1062 * returns true if the device address of the ipoib interface has changed and the
1063 * new address is a valid one (i.e in the gid table), return false otherwise.
1065 static bool ipoib_dev_addr_changed_valid(struct ipoib_dev_priv *priv)
1067 union ib_gid search_gid;
1068 union ib_gid gid0;
1069 union ib_gid *netdev_gid;
1070 int err;
1071 u16 index;
1072 u8 port;
1073 bool ret = false;
1075 netdev_gid = (union ib_gid *)(priv->dev->dev_addr + 4);
1076 if (rdma_query_gid(priv->ca, priv->port, 0, &gid0))
1077 return false;
1079 netif_addr_lock_bh(priv->dev);
1081 /* The subnet prefix may have changed, update it now so we won't have
1082 * to do it later
1084 priv->local_gid.global.subnet_prefix = gid0.global.subnet_prefix;
1085 netdev_gid->global.subnet_prefix = gid0.global.subnet_prefix;
1086 search_gid.global.subnet_prefix = gid0.global.subnet_prefix;
1088 search_gid.global.interface_id = priv->local_gid.global.interface_id;
1090 netif_addr_unlock_bh(priv->dev);
1092 err = ib_find_gid(priv->ca, &search_gid, &port, &index);
1094 netif_addr_lock_bh(priv->dev);
1096 if (search_gid.global.interface_id !=
1097 priv->local_gid.global.interface_id)
1098 /* There was a change while we were looking up the gid, bail
1099 * here and let the next work sort this out
1101 goto out;
1103 /* The next section of code needs some background:
1104 * Per IB spec the port GUID can't change if the HCA is powered on.
1105 * port GUID is the basis for GID at index 0 which is the basis for
1106 * the default device address of a ipoib interface.
1108 * so it seems the flow should be:
1109 * if user_changed_dev_addr && gid in gid tbl
1110 * set bit dev_addr_set
1111 * return true
1112 * else
1113 * return false
1115 * The issue is that there are devices that don't follow the spec,
1116 * they change the port GUID when the HCA is powered, so in order
1117 * not to break userspace applications, We need to check if the
1118 * user wanted to control the device address and we assume that
1119 * if he sets the device address back to be based on GID index 0,
1120 * he no longer wishs to control it.
1122 * If the user doesn't control the the device address,
1123 * IPOIB_FLAG_DEV_ADDR_SET is set and ib_find_gid failed it means
1124 * the port GUID has changed and GID at index 0 has changed
1125 * so we need to change priv->local_gid and priv->dev->dev_addr
1126 * to reflect the new GID.
1128 if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
1129 if (!err && port == priv->port) {
1130 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1131 if (index == 0)
1132 clear_bit(IPOIB_FLAG_DEV_ADDR_CTRL,
1133 &priv->flags);
1134 else
1135 set_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags);
1136 ret = true;
1137 } else {
1138 ret = false;
1140 } else {
1141 if (!err && port == priv->port) {
1142 ret = true;
1143 } else {
1144 if (!test_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags)) {
1145 memcpy(&priv->local_gid, &gid0,
1146 sizeof(priv->local_gid));
1147 memcpy(priv->dev->dev_addr + 4, &gid0,
1148 sizeof(priv->local_gid));
1149 ret = true;
1154 out:
1155 netif_addr_unlock_bh(priv->dev);
1157 return ret;
1160 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
1161 enum ipoib_flush_level level,
1162 int nesting)
1164 struct ipoib_dev_priv *cpriv;
1165 struct net_device *dev = priv->dev;
1166 int result;
1168 down_read_nested(&priv->vlan_rwsem, nesting);
1171 * Flush any child interfaces too -- they might be up even if
1172 * the parent is down.
1174 list_for_each_entry(cpriv, &priv->child_intfs, list)
1175 __ipoib_ib_dev_flush(cpriv, level, nesting + 1);
1177 up_read(&priv->vlan_rwsem);
1179 if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) &&
1180 level != IPOIB_FLUSH_HEAVY) {
1181 /* Make sure the dev_addr is set even if not flushing */
1182 if (level == IPOIB_FLUSH_LIGHT)
1183 ipoib_dev_addr_changed_valid(priv);
1184 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
1185 return;
1188 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
1189 /* interface is down. update pkey and leave. */
1190 if (level == IPOIB_FLUSH_HEAVY) {
1191 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1192 update_parent_pkey(priv);
1193 else
1194 update_child_pkey(priv);
1195 } else if (level == IPOIB_FLUSH_LIGHT)
1196 ipoib_dev_addr_changed_valid(priv);
1197 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
1198 return;
1201 if (level == IPOIB_FLUSH_HEAVY) {
1202 /* child devices chase their origin pkey value, while non-child
1203 * (parent) devices should always takes what present in pkey index 0
1205 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1206 result = update_child_pkey(priv);
1207 if (result) {
1208 /* restart QP only if P_Key index is changed */
1209 ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
1210 return;
1213 } else {
1214 result = update_parent_pkey(priv);
1215 /* restart QP only if P_Key value changed */
1216 if (result) {
1217 ipoib_dbg(priv, "Not flushing - P_Key value not changed.\n");
1218 return;
1223 if (level == IPOIB_FLUSH_LIGHT) {
1224 int oper_up;
1225 ipoib_mark_paths_invalid(dev);
1226 /* Set IPoIB operation as down to prevent races between:
1227 * the flush flow which leaves MCG and on the fly joins
1228 * which can happen during that time. mcast restart task
1229 * should deal with join requests we missed.
1231 oper_up = test_and_clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
1232 ipoib_mcast_dev_flush(dev);
1233 if (oper_up)
1234 set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
1235 ipoib_flush_ah(dev);
1238 if (level >= IPOIB_FLUSH_NORMAL)
1239 ipoib_ib_dev_down(dev);
1241 if (level == IPOIB_FLUSH_HEAVY) {
1242 if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
1243 ipoib_ib_dev_stop(dev);
1245 if (ipoib_ib_dev_open(dev))
1246 return;
1248 if (netif_queue_stopped(dev))
1249 netif_start_queue(dev);
1253 * The device could have been brought down between the start and when
1254 * we get here, don't bring it back up if it's not configured up
1256 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
1257 if (level >= IPOIB_FLUSH_NORMAL)
1258 ipoib_ib_dev_up(dev);
1259 if (ipoib_dev_addr_changed_valid(priv))
1260 ipoib_mcast_restart_task(&priv->restart_task);
1264 void ipoib_ib_dev_flush_light(struct work_struct *work)
1266 struct ipoib_dev_priv *priv =
1267 container_of(work, struct ipoib_dev_priv, flush_light);
1269 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT, 0);
1272 void ipoib_ib_dev_flush_normal(struct work_struct *work)
1274 struct ipoib_dev_priv *priv =
1275 container_of(work, struct ipoib_dev_priv, flush_normal);
1277 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL, 0);
1280 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
1282 struct ipoib_dev_priv *priv =
1283 container_of(work, struct ipoib_dev_priv, flush_heavy);
1285 rtnl_lock();
1286 __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY, 0);
1287 rtnl_unlock();
1290 void ipoib_ib_dev_cleanup(struct net_device *dev)
1292 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1294 ipoib_dbg(priv, "cleaning up ib_dev\n");
1296 * We must make sure there are no more (path) completions
1297 * that may wish to touch priv fields that are no longer valid
1299 ipoib_flush_paths(dev);
1301 ipoib_mcast_stop_thread(dev);
1302 ipoib_mcast_dev_flush(dev);
1305 * All of our ah references aren't free until after
1306 * ipoib_mcast_dev_flush(), ipoib_flush_paths, and
1307 * the neighbor garbage collection is stopped and reaped.
1308 * That should all be done now, so make a final ah flush.
1310 ipoib_stop_ah(dev);
1312 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
1314 priv->rn_ops->ndo_uninit(dev);
1316 if (priv->pd) {
1317 ib_dealloc_pd(priv->pd);
1318 priv->pd = NULL;