2 * Copyright (c) 2006 Mellanox Technologies. All rights reserved
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_cache.h>
37 #include <linux/icmpv6.h>
38 #include <linux/delay.h>
39 #include <linux/vmalloc.h>
43 int ipoib_max_conn_qp = 128;
45 module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
46 MODULE_PARM_DESC(max_nonsrq_conn_qp,
47 "Max number of connected-mode QPs per interface "
48 "(applied only if shared receive queue is not available)");
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51 static int data_debug_level;
53 module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
54 MODULE_PARM_DESC(cm_data_debug_level,
55 "Enable data path debug tracing for connected mode if > 0");
58 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
60 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
61 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
62 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
63 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
65 static struct ib_qp_attr ipoib_cm_err_attr = {
66 .qp_state = IB_QPS_ERR
69 #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
71 static struct ib_send_wr ipoib_cm_rx_drain_wr = {
72 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
76 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
77 struct ib_cm_event *event);
79 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
80 u64 mapping[IPOIB_CM_RX_SG])
84 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
86 for (i = 0; i < frags; ++i)
87 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
90 static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
93 struct ib_recv_wr *bad_wr;
96 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
98 for (i = 0; i < priv->cm.num_frags; ++i)
99 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
101 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
103 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
104 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
105 priv->cm.srq_ring[id].mapping);
106 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
107 priv->cm.srq_ring[id].skb = NULL;
113 static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
114 struct ipoib_cm_rx *rx,
115 struct ib_recv_wr *wr,
116 struct ib_sge *sge, int id)
118 struct ipoib_dev_priv *priv = netdev_priv(dev);
119 struct ib_recv_wr *bad_wr;
122 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
124 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
125 sge[i].addr = rx->rx_ring[id].mapping[i];
127 ret = ib_post_recv(rx->qp, wr, &bad_wr);
129 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
130 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
131 rx->rx_ring[id].mapping);
132 dev_kfree_skb_any(rx->rx_ring[id].skb);
133 rx->rx_ring[id].skb = NULL;
139 static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
140 struct ipoib_cm_rx_buf *rx_ring,
142 u64 mapping[IPOIB_CM_RX_SG])
144 struct ipoib_dev_priv *priv = netdev_priv(dev);
148 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
153 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
154 * IP header to a multiple of 16.
156 skb_reserve(skb, 12);
158 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
160 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
161 dev_kfree_skb_any(skb);
165 for (i = 0; i < frags; i++) {
166 struct page *page = alloc_page(GFP_ATOMIC);
170 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
172 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
173 0, PAGE_SIZE, DMA_FROM_DEVICE);
174 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
178 rx_ring[id].skb = skb;
183 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
186 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
188 dev_kfree_skb_any(skb);
192 static void ipoib_cm_free_rx_ring(struct net_device *dev,
193 struct ipoib_cm_rx_buf *rx_ring)
195 struct ipoib_dev_priv *priv = netdev_priv(dev);
198 for (i = 0; i < ipoib_recvq_size; ++i)
199 if (rx_ring[i].skb) {
200 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
202 dev_kfree_skb_any(rx_ring[i].skb);
208 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
210 struct ib_send_wr *bad_wr;
211 struct ipoib_cm_rx *p;
213 /* We only reserved 1 extra slot in CQ for drain WRs, so
214 * make sure we have at most 1 outstanding WR. */
215 if (list_empty(&priv->cm.rx_flush_list) ||
216 !list_empty(&priv->cm.rx_drain_list))
220 * QPs on flush list are error state. This way, a "flush
221 * error" WC will be immediately generated for each WR we post.
223 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
224 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
225 ipoib_warn(priv, "failed to post drain wr\n");
227 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
230 static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
232 struct ipoib_cm_rx *p = ctx;
233 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
236 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
239 spin_lock_irqsave(&priv->lock, flags);
240 list_move(&p->list, &priv->cm.rx_flush_list);
241 p->state = IPOIB_CM_RX_FLUSH;
242 ipoib_cm_start_rx_drain(priv);
243 spin_unlock_irqrestore(&priv->lock, flags);
246 static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
247 struct ipoib_cm_rx *p)
249 struct ipoib_dev_priv *priv = netdev_priv(dev);
250 struct ib_qp_init_attr attr = {
251 .event_handler = ipoib_cm_rx_event_handler,
252 .send_cq = priv->recv_cq, /* For drain WR */
253 .recv_cq = priv->recv_cq,
255 .cap.max_send_wr = 1, /* For drain WR */
256 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
257 .sq_sig_type = IB_SIGNAL_ALL_WR,
258 .qp_type = IB_QPT_RC,
262 if (!ipoib_cm_has_srq(dev)) {
263 attr.cap.max_recv_wr = ipoib_recvq_size;
264 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
267 return ib_create_qp(priv->pd, &attr);
270 static int ipoib_cm_modify_rx_qp(struct net_device *dev,
271 struct ib_cm_id *cm_id, struct ib_qp *qp,
274 struct ipoib_dev_priv *priv = netdev_priv(dev);
275 struct ib_qp_attr qp_attr;
276 int qp_attr_mask, ret;
278 qp_attr.qp_state = IB_QPS_INIT;
279 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
281 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
284 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
286 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
289 qp_attr.qp_state = IB_QPS_RTR;
290 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
292 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
295 qp_attr.rq_psn = psn;
296 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
298 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
303 * Current Mellanox HCA firmware won't generate completions
304 * with error for drain WRs unless the QP has been moved to
305 * RTS first. This work-around leaves a window where a QP has
306 * moved to error asynchronously, but this will eventually get
307 * fixed in firmware, so let's not error out if modify QP
310 qp_attr.qp_state = IB_QPS_RTS;
311 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
313 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
316 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
318 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
325 static void ipoib_cm_init_rx_wr(struct net_device *dev,
326 struct ib_recv_wr *wr,
329 struct ipoib_dev_priv *priv = netdev_priv(dev);
332 for (i = 0; i < priv->cm.num_frags; ++i)
333 sge[i].lkey = priv->mr->lkey;
335 sge[0].length = IPOIB_CM_HEAD_SIZE;
336 for (i = 1; i < priv->cm.num_frags; ++i)
337 sge[i].length = PAGE_SIZE;
341 wr->num_sge = priv->cm.num_frags;
344 static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
345 struct ipoib_cm_rx *rx)
347 struct ipoib_dev_priv *priv = netdev_priv(dev);
349 struct ib_recv_wr wr;
350 struct ib_sge sge[IPOIB_CM_RX_SG];
355 rx->rx_ring = vmalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
357 printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
358 priv->ca->name, ipoib_recvq_size);
362 memset(rx->rx_ring, 0, ipoib_recvq_size * sizeof *rx->rx_ring);
364 t = kmalloc(sizeof *t, GFP_KERNEL);
370 ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
372 spin_lock_irq(&priv->lock);
374 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
375 spin_unlock_irq(&priv->lock);
376 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
380 ++priv->cm.nonsrq_conn_qp;
382 spin_unlock_irq(&priv->lock);
384 for (i = 0; i < ipoib_recvq_size; ++i) {
385 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
386 rx->rx_ring[i].mapping)) {
387 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
391 ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
393 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
394 "failed for buf %d\n", i);
400 rx->recv_count = ipoib_recvq_size;
407 spin_lock_irq(&priv->lock);
408 --priv->cm.nonsrq_conn_qp;
409 spin_unlock_irq(&priv->lock);
413 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
418 static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
419 struct ib_qp *qp, struct ib_cm_req_event_param *req,
422 struct ipoib_dev_priv *priv = netdev_priv(dev);
423 struct ipoib_cm_data data = {};
424 struct ib_cm_rep_param rep = {};
426 data.qpn = cpu_to_be32(priv->qp->qp_num);
427 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
429 rep.private_data = &data;
430 rep.private_data_len = sizeof data;
431 rep.flow_control = 0;
432 rep.rnr_retry_count = req->rnr_retry_count;
433 rep.srq = ipoib_cm_has_srq(dev);
434 rep.qp_num = qp->qp_num;
435 rep.starting_psn = psn;
436 return ib_send_cm_rep(cm_id, &rep);
439 static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
441 struct net_device *dev = cm_id->context;
442 struct ipoib_dev_priv *priv = netdev_priv(dev);
443 struct ipoib_cm_rx *p;
447 ipoib_dbg(priv, "REQ arrived\n");
448 p = kzalloc(sizeof *p, GFP_KERNEL);
454 p->state = IPOIB_CM_RX_LIVE;
455 p->jiffies = jiffies;
456 INIT_LIST_HEAD(&p->list);
458 p->qp = ipoib_cm_create_rx_qp(dev, p);
460 ret = PTR_ERR(p->qp);
464 psn = random32() & 0xffffff;
465 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
469 if (!ipoib_cm_has_srq(dev)) {
470 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
475 spin_lock_irq(&priv->lock);
476 queue_delayed_work(ipoib_workqueue,
477 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
478 /* Add this entry to passive ids list head, but do not re-add it
479 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
480 p->jiffies = jiffies;
481 if (p->state == IPOIB_CM_RX_LIVE)
482 list_move(&p->list, &priv->cm.passive_ids);
483 spin_unlock_irq(&priv->lock);
485 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
487 ipoib_warn(priv, "failed to send REP: %d\n", ret);
488 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
489 ipoib_warn(priv, "unable to move qp to error state\n");
494 ib_destroy_qp(p->qp);
500 static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
501 struct ib_cm_event *event)
503 struct ipoib_cm_rx *p;
504 struct ipoib_dev_priv *priv;
506 switch (event->event) {
507 case IB_CM_REQ_RECEIVED:
508 return ipoib_cm_req_handler(cm_id, event);
509 case IB_CM_DREQ_RECEIVED:
511 ib_send_cm_drep(cm_id, NULL, 0);
513 case IB_CM_REJ_RECEIVED:
515 priv = netdev_priv(p->dev);
516 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
517 ipoib_warn(priv, "unable to move qp to error state\n");
523 /* Adjust length of skb with fragments to match received data */
524 static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
525 unsigned int length, struct sk_buff *toskb)
530 /* put header into skb */
531 size = min(length, hdr_space);
536 num_frags = skb_shinfo(skb)->nr_frags;
537 for (i = 0; i < num_frags; i++) {
538 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
541 /* don't need this page */
542 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
543 --skb_shinfo(skb)->nr_frags;
545 size = min(length, (unsigned) PAGE_SIZE);
548 skb->data_len += size;
549 skb->truesize += size;
556 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
558 struct ipoib_dev_priv *priv = netdev_priv(dev);
559 struct ipoib_cm_rx_buf *rx_ring;
560 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
561 struct sk_buff *skb, *newskb;
562 struct ipoib_cm_rx *p;
564 u64 mapping[IPOIB_CM_RX_SG];
567 struct sk_buff *small_skb;
569 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
572 if (unlikely(wr_id >= ipoib_recvq_size)) {
573 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
574 spin_lock_irqsave(&priv->lock, flags);
575 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
576 ipoib_cm_start_rx_drain(priv);
577 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
578 spin_unlock_irqrestore(&priv->lock, flags);
580 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
581 wr_id, ipoib_recvq_size);
585 p = wc->qp->qp_context;
587 has_srq = ipoib_cm_has_srq(dev);
588 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
590 skb = rx_ring[wr_id].skb;
592 if (unlikely(wc->status != IB_WC_SUCCESS)) {
593 ipoib_dbg(priv, "cm recv error "
594 "(status=%d, wrid=%d vend_err %x)\n",
595 wc->status, wr_id, wc->vendor_err);
596 ++dev->stats.rx_dropped;
600 if (!--p->recv_count) {
601 spin_lock_irqsave(&priv->lock, flags);
602 list_move(&p->list, &priv->cm.rx_reap_list);
603 spin_unlock_irqrestore(&priv->lock, flags);
604 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
610 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
611 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
612 spin_lock_irqsave(&priv->lock, flags);
613 p->jiffies = jiffies;
614 /* Move this entry to list head, but do not re-add it
615 * if it has been moved out of list. */
616 if (p->state == IPOIB_CM_RX_LIVE)
617 list_move(&p->list, &priv->cm.passive_ids);
618 spin_unlock_irqrestore(&priv->lock, flags);
622 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
623 int dlen = wc->byte_len;
625 small_skb = dev_alloc_skb(dlen + 12);
627 skb_reserve(small_skb, 12);
628 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
629 dlen, DMA_FROM_DEVICE);
630 skb_copy_from_linear_data(skb, small_skb->data, dlen);
631 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
632 dlen, DMA_FROM_DEVICE);
633 skb_put(small_skb, dlen);
639 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
640 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
642 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
643 if (unlikely(!newskb)) {
645 * If we can't allocate a new RX buffer, dump
646 * this packet and reuse the old buffer.
648 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
649 ++dev->stats.rx_dropped;
653 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
654 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
656 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
657 wc->byte_len, wc->slid);
659 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
662 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
663 skb_reset_mac_header(skb);
664 skb_pull(skb, IPOIB_ENCAP_LEN);
666 dev->last_rx = jiffies;
667 ++dev->stats.rx_packets;
668 dev->stats.rx_bytes += skb->len;
671 /* XXX get correct PACKET_ type here */
672 skb->pkt_type = PACKET_HOST;
673 netif_receive_skb(skb);
677 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
678 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
679 "for buf %d\n", wr_id);
681 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
686 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
687 "for buf %d\n", wr_id);
692 static inline int post_send(struct ipoib_dev_priv *priv,
693 struct ipoib_cm_tx *tx,
697 struct ib_send_wr *bad_wr;
699 priv->tx_sge[0].addr = addr;
700 priv->tx_sge[0].length = len;
702 priv->tx_wr.num_sge = 1;
703 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
705 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
708 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
710 struct ipoib_dev_priv *priv = netdev_priv(dev);
711 struct ipoib_cm_tx_buf *tx_req;
714 if (unlikely(skb->len > tx->mtu)) {
715 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
717 ++dev->stats.tx_dropped;
718 ++dev->stats.tx_errors;
719 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
723 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
724 tx->tx_head, skb->len, tx->qp->qp_num);
727 * We put the skb into the tx_ring _before_ we call post_send()
728 * because it's entirely possible that the completion handler will
729 * run before we execute anything after the post_send(). That
730 * means we have to make sure everything is properly recorded and
731 * our state is consistent before we call post_send().
733 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
735 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
736 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
737 ++dev->stats.tx_errors;
738 dev_kfree_skb_any(skb);
742 tx_req->mapping = addr;
744 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
746 ipoib_warn(priv, "post_send failed\n");
747 ++dev->stats.tx_errors;
748 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
749 dev_kfree_skb_any(skb);
751 dev->trans_start = jiffies;
754 if (++priv->tx_outstanding == ipoib_sendq_size) {
755 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
757 netif_stop_queue(dev);
762 void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
764 struct ipoib_dev_priv *priv = netdev_priv(dev);
765 struct ipoib_cm_tx *tx = wc->qp->qp_context;
766 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
767 struct ipoib_cm_tx_buf *tx_req;
770 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
773 if (unlikely(wr_id >= ipoib_sendq_size)) {
774 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
775 wr_id, ipoib_sendq_size);
779 tx_req = &tx->tx_ring[wr_id];
781 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
783 /* FIXME: is this right? Shouldn't we only increment on success? */
784 ++dev->stats.tx_packets;
785 dev->stats.tx_bytes += tx_req->skb->len;
787 dev_kfree_skb_any(tx_req->skb);
789 spin_lock_irqsave(&priv->tx_lock, flags);
791 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
792 netif_queue_stopped(dev) &&
793 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
794 netif_wake_queue(dev);
796 if (wc->status != IB_WC_SUCCESS &&
797 wc->status != IB_WC_WR_FLUSH_ERR) {
798 struct ipoib_neigh *neigh;
800 ipoib_dbg(priv, "failed cm send event "
801 "(status=%d, wrid=%d vend_err %x)\n",
802 wc->status, wr_id, wc->vendor_err);
804 spin_lock(&priv->lock);
809 list_del(&neigh->list);
811 ipoib_put_ah(neigh->ah);
812 ipoib_neigh_free(dev, neigh);
817 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
818 list_move(&tx->list, &priv->cm.reap_list);
819 queue_work(ipoib_workqueue, &priv->cm.reap_task);
822 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
824 spin_unlock(&priv->lock);
827 spin_unlock_irqrestore(&priv->tx_lock, flags);
830 int ipoib_cm_dev_open(struct net_device *dev)
832 struct ipoib_dev_priv *priv = netdev_priv(dev);
835 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
838 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
839 if (IS_ERR(priv->cm.id)) {
840 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
841 ret = PTR_ERR(priv->cm.id);
845 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
848 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
849 IPOIB_CM_IETF_ID | priv->qp->qp_num);
856 ib_destroy_cm_id(priv->cm.id);
862 static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
864 struct ipoib_dev_priv *priv = netdev_priv(dev);
865 struct ipoib_cm_rx *rx, *n;
868 spin_lock_irq(&priv->lock);
869 list_splice_init(&priv->cm.rx_reap_list, &list);
870 spin_unlock_irq(&priv->lock);
872 list_for_each_entry_safe(rx, n, &list, list) {
873 ib_destroy_cm_id(rx->id);
874 ib_destroy_qp(rx->qp);
875 if (!ipoib_cm_has_srq(dev)) {
876 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
877 spin_lock_irq(&priv->lock);
878 --priv->cm.nonsrq_conn_qp;
879 spin_unlock_irq(&priv->lock);
885 void ipoib_cm_dev_stop(struct net_device *dev)
887 struct ipoib_dev_priv *priv = netdev_priv(dev);
888 struct ipoib_cm_rx *p;
892 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
895 ib_destroy_cm_id(priv->cm.id);
898 spin_lock_irq(&priv->lock);
899 while (!list_empty(&priv->cm.passive_ids)) {
900 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
901 list_move(&p->list, &priv->cm.rx_error_list);
902 p->state = IPOIB_CM_RX_ERROR;
903 spin_unlock_irq(&priv->lock);
904 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
906 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
907 spin_lock_irq(&priv->lock);
910 /* Wait for all RX to be drained */
913 while (!list_empty(&priv->cm.rx_error_list) ||
914 !list_empty(&priv->cm.rx_flush_list) ||
915 !list_empty(&priv->cm.rx_drain_list)) {
916 if (time_after(jiffies, begin + 5 * HZ)) {
917 ipoib_warn(priv, "RX drain timing out\n");
920 * assume the HW is wedged and just free up everything.
922 list_splice_init(&priv->cm.rx_flush_list,
923 &priv->cm.rx_reap_list);
924 list_splice_init(&priv->cm.rx_error_list,
925 &priv->cm.rx_reap_list);
926 list_splice_init(&priv->cm.rx_drain_list,
927 &priv->cm.rx_reap_list);
930 spin_unlock_irq(&priv->lock);
933 spin_lock_irq(&priv->lock);
936 spin_unlock_irq(&priv->lock);
938 ipoib_cm_free_rx_reap_list(dev);
940 cancel_delayed_work(&priv->cm.stale_task);
943 static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
945 struct ipoib_cm_tx *p = cm_id->context;
946 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
947 struct ipoib_cm_data *data = event->private_data;
948 struct sk_buff_head skqueue;
949 struct ib_qp_attr qp_attr;
950 int qp_attr_mask, ret;
953 p->mtu = be32_to_cpu(data->mtu);
955 if (p->mtu <= IPOIB_ENCAP_LEN) {
956 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
957 p->mtu, IPOIB_ENCAP_LEN);
961 qp_attr.qp_state = IB_QPS_RTR;
962 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
964 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
968 qp_attr.rq_psn = 0 /* FIXME */;
969 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
971 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
975 qp_attr.qp_state = IB_QPS_RTS;
976 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
978 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
981 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
983 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
987 skb_queue_head_init(&skqueue);
989 spin_lock_irq(&priv->lock);
990 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
992 while ((skb = __skb_dequeue(&p->neigh->queue)))
993 __skb_queue_tail(&skqueue, skb);
994 spin_unlock_irq(&priv->lock);
996 while ((skb = __skb_dequeue(&skqueue))) {
998 if (dev_queue_xmit(skb))
999 ipoib_warn(priv, "dev_queue_xmit failed "
1000 "to requeue packet\n");
1003 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1005 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1011 static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
1013 struct ipoib_dev_priv *priv = netdev_priv(dev);
1014 struct ib_qp_init_attr attr = {
1015 .send_cq = priv->recv_cq,
1016 .recv_cq = priv->recv_cq,
1017 .srq = priv->cm.srq,
1018 .cap.max_send_wr = ipoib_sendq_size,
1019 .cap.max_send_sge = 1,
1020 .sq_sig_type = IB_SIGNAL_ALL_WR,
1021 .qp_type = IB_QPT_RC,
1025 return ib_create_qp(priv->pd, &attr);
1028 static int ipoib_cm_send_req(struct net_device *dev,
1029 struct ib_cm_id *id, struct ib_qp *qp,
1031 struct ib_sa_path_rec *pathrec)
1033 struct ipoib_dev_priv *priv = netdev_priv(dev);
1034 struct ipoib_cm_data data = {};
1035 struct ib_cm_req_param req = {};
1037 data.qpn = cpu_to_be32(priv->qp->qp_num);
1038 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1040 req.primary_path = pathrec;
1041 req.alternate_path = NULL;
1042 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1043 req.qp_num = qp->qp_num;
1044 req.qp_type = qp->qp_type;
1045 req.private_data = &data;
1046 req.private_data_len = sizeof data;
1047 req.flow_control = 0;
1049 req.starting_psn = 0; /* FIXME */
1052 * Pick some arbitrary defaults here; we could make these
1053 * module parameters if anyone cared about setting them.
1055 req.responder_resources = 4;
1056 req.remote_cm_response_timeout = 20;
1057 req.local_cm_response_timeout = 20;
1058 req.retry_count = 0; /* RFC draft warns against retries */
1059 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1060 req.max_cm_retries = 15;
1061 req.srq = ipoib_cm_has_srq(dev);
1062 return ib_send_cm_req(id, &req);
1065 static int ipoib_cm_modify_tx_init(struct net_device *dev,
1066 struct ib_cm_id *cm_id, struct ib_qp *qp)
1068 struct ipoib_dev_priv *priv = netdev_priv(dev);
1069 struct ib_qp_attr qp_attr;
1070 int qp_attr_mask, ret;
1071 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
1073 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
1077 qp_attr.qp_state = IB_QPS_INIT;
1078 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1079 qp_attr.port_num = priv->port;
1080 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1082 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1084 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1090 static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1091 struct ib_sa_path_rec *pathrec)
1093 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1096 p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring);
1098 ipoib_warn(priv, "failed to allocate tx ring\n");
1102 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
1104 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
1105 if (IS_ERR(p->qp)) {
1106 ret = PTR_ERR(p->qp);
1107 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1111 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1112 if (IS_ERR(p->id)) {
1113 ret = PTR_ERR(p->id);
1114 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1118 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1120 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1124 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1126 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1130 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
1131 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
1137 ib_destroy_cm_id(p->id);
1140 ib_destroy_qp(p->qp);
1148 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1150 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1151 struct ipoib_cm_tx_buf *tx_req;
1152 unsigned long flags;
1153 unsigned long begin;
1155 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1156 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1159 ib_destroy_cm_id(p->id);
1162 /* Wait for all sends to complete */
1164 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1165 if (time_after(jiffies, begin + 5 * HZ)) {
1166 ipoib_warn(priv, "timing out; %d sends not completed\n",
1167 p->tx_head - p->tx_tail);
1177 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1178 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1179 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
1181 dev_kfree_skb_any(tx_req->skb);
1183 spin_lock_irqsave(&priv->tx_lock, flags);
1184 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1185 netif_queue_stopped(p->dev) &&
1186 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1187 netif_wake_queue(p->dev);
1188 spin_unlock_irqrestore(&priv->tx_lock, flags);
1192 ib_destroy_qp(p->qp);
1198 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1199 struct ib_cm_event *event)
1201 struct ipoib_cm_tx *tx = cm_id->context;
1202 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1203 struct net_device *dev = priv->dev;
1204 struct ipoib_neigh *neigh;
1207 switch (event->event) {
1208 case IB_CM_DREQ_RECEIVED:
1209 ipoib_dbg(priv, "DREQ received.\n");
1210 ib_send_cm_drep(cm_id, NULL, 0);
1212 case IB_CM_REP_RECEIVED:
1213 ipoib_dbg(priv, "REP received.\n");
1214 ret = ipoib_cm_rep_handler(cm_id, event);
1216 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1219 case IB_CM_REQ_ERROR:
1220 case IB_CM_REJ_RECEIVED:
1221 case IB_CM_TIMEWAIT_EXIT:
1222 ipoib_dbg(priv, "CM error %d.\n", event->event);
1223 spin_lock_irq(&priv->tx_lock);
1224 spin_lock(&priv->lock);
1229 list_del(&neigh->list);
1231 ipoib_put_ah(neigh->ah);
1232 ipoib_neigh_free(dev, neigh);
1237 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1238 list_move(&tx->list, &priv->cm.reap_list);
1239 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1242 spin_unlock(&priv->lock);
1243 spin_unlock_irq(&priv->tx_lock);
1252 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1253 struct ipoib_neigh *neigh)
1255 struct ipoib_dev_priv *priv = netdev_priv(dev);
1256 struct ipoib_cm_tx *tx;
1258 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1266 list_add(&tx->list, &priv->cm.start_list);
1267 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1268 queue_work(ipoib_workqueue, &priv->cm.start_task);
1272 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1274 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1275 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1276 list_move(&tx->list, &priv->cm.reap_list);
1277 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1278 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1279 IPOIB_GID_ARG(tx->neigh->dgid));
1284 static void ipoib_cm_tx_start(struct work_struct *work)
1286 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1288 struct net_device *dev = priv->dev;
1289 struct ipoib_neigh *neigh;
1290 struct ipoib_cm_tx *p;
1291 unsigned long flags;
1294 struct ib_sa_path_rec pathrec;
1297 spin_lock_irqsave(&priv->tx_lock, flags);
1298 spin_lock(&priv->lock);
1299 while (!list_empty(&priv->cm.start_list)) {
1300 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1301 list_del_init(&p->list);
1303 qpn = IPOIB_QPN(neigh->neighbour->ha);
1304 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1305 spin_unlock(&priv->lock);
1306 spin_unlock_irqrestore(&priv->tx_lock, flags);
1307 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1308 spin_lock_irqsave(&priv->tx_lock, flags);
1309 spin_lock(&priv->lock);
1314 list_del(&neigh->list);
1316 ipoib_put_ah(neigh->ah);
1317 ipoib_neigh_free(dev, neigh);
1323 spin_unlock(&priv->lock);
1324 spin_unlock_irqrestore(&priv->tx_lock, flags);
1327 static void ipoib_cm_tx_reap(struct work_struct *work)
1329 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1331 struct ipoib_cm_tx *p;
1333 spin_lock_irq(&priv->tx_lock);
1334 spin_lock(&priv->lock);
1335 while (!list_empty(&priv->cm.reap_list)) {
1336 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1338 spin_unlock(&priv->lock);
1339 spin_unlock_irq(&priv->tx_lock);
1340 ipoib_cm_tx_destroy(p);
1341 spin_lock_irq(&priv->tx_lock);
1342 spin_lock(&priv->lock);
1344 spin_unlock(&priv->lock);
1345 spin_unlock_irq(&priv->tx_lock);
1348 static void ipoib_cm_skb_reap(struct work_struct *work)
1350 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1352 struct sk_buff *skb;
1354 unsigned mtu = priv->mcast_mtu;
1356 spin_lock_irq(&priv->tx_lock);
1357 spin_lock(&priv->lock);
1358 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1359 spin_unlock(&priv->lock);
1360 spin_unlock_irq(&priv->tx_lock);
1361 if (skb->protocol == htons(ETH_P_IP))
1362 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1363 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1364 else if (skb->protocol == htons(ETH_P_IPV6))
1365 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
1367 dev_kfree_skb_any(skb);
1368 spin_lock_irq(&priv->tx_lock);
1369 spin_lock(&priv->lock);
1371 spin_unlock(&priv->lock);
1372 spin_unlock_irq(&priv->tx_lock);
1375 void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
1378 struct ipoib_dev_priv *priv = netdev_priv(dev);
1379 int e = skb_queue_empty(&priv->cm.skb_queue);
1382 skb->dst->ops->update_pmtu(skb->dst, mtu);
1384 skb_queue_tail(&priv->cm.skb_queue, skb);
1386 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1389 static void ipoib_cm_rx_reap(struct work_struct *work)
1391 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1392 cm.rx_reap_task)->dev);
1395 static void ipoib_cm_stale_task(struct work_struct *work)
1397 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1398 cm.stale_task.work);
1399 struct ipoib_cm_rx *p;
1402 spin_lock_irq(&priv->lock);
1403 while (!list_empty(&priv->cm.passive_ids)) {
1404 /* List is sorted by LRU, start from tail,
1405 * stop when we see a recently used entry */
1406 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1407 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1409 list_move(&p->list, &priv->cm.rx_error_list);
1410 p->state = IPOIB_CM_RX_ERROR;
1411 spin_unlock_irq(&priv->lock);
1412 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1414 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1415 spin_lock_irq(&priv->lock);
1418 if (!list_empty(&priv->cm.passive_ids))
1419 queue_delayed_work(ipoib_workqueue,
1420 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1421 spin_unlock_irq(&priv->lock);
1425 static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1428 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1430 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1431 return sprintf(buf, "connected\n");
1433 return sprintf(buf, "datagram\n");
1436 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1437 const char *buf, size_t count)
1439 struct net_device *dev = to_net_dev(d);
1440 struct ipoib_dev_priv *priv = netdev_priv(dev);
1442 /* flush paths if we switch modes so that connections are restarted */
1443 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1444 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1445 ipoib_warn(priv, "enabling connected mode "
1446 "will cause multicast packet drops\n");
1449 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
1451 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
1453 ipoib_flush_paths(dev);
1457 if (!strcmp(buf, "datagram\n")) {
1458 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1461 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) {
1462 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
1463 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1464 dev->features |= NETIF_F_TSO;
1466 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
1468 ipoib_flush_paths(dev);
1476 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1478 int ipoib_cm_add_mode_attr(struct net_device *dev)
1480 return device_create_file(&dev->dev, &dev_attr_mode);
1483 static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1485 struct ipoib_dev_priv *priv = netdev_priv(dev);
1486 struct ib_srq_init_attr srq_init_attr = {
1488 .max_wr = ipoib_recvq_size,
1493 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1494 if (IS_ERR(priv->cm.srq)) {
1495 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1496 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1497 priv->ca->name, PTR_ERR(priv->cm.srq));
1498 priv->cm.srq = NULL;
1502 priv->cm.srq_ring = vmalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1503 if (!priv->cm.srq_ring) {
1504 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1505 priv->ca->name, ipoib_recvq_size);
1506 ib_destroy_srq(priv->cm.srq);
1507 priv->cm.srq = NULL;
1511 memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1514 int ipoib_cm_dev_init(struct net_device *dev)
1516 struct ipoib_dev_priv *priv = netdev_priv(dev);
1518 struct ib_device_attr attr;
1520 INIT_LIST_HEAD(&priv->cm.passive_ids);
1521 INIT_LIST_HEAD(&priv->cm.reap_list);
1522 INIT_LIST_HEAD(&priv->cm.start_list);
1523 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1524 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1525 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1526 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1527 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1528 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1529 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1530 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1531 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1533 skb_queue_head_init(&priv->cm.skb_queue);
1535 ret = ib_query_device(priv->ca, &attr);
1537 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1541 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1543 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1544 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1545 if (ipoib_cm_has_srq(dev)) {
1546 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1547 priv->cm.num_frags = attr.max_srq_sge;
1548 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1549 priv->cm.max_cm_mtu, priv->cm.num_frags);
1551 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1552 priv->cm.num_frags = IPOIB_CM_RX_SG;
1555 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
1557 if (ipoib_cm_has_srq(dev)) {
1558 for (i = 0; i < ipoib_recvq_size; ++i) {
1559 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
1560 priv->cm.num_frags - 1,
1561 priv->cm.srq_ring[i].mapping)) {
1562 ipoib_warn(priv, "failed to allocate "
1563 "receive buffer %d\n", i);
1564 ipoib_cm_dev_cleanup(dev);
1568 if (ipoib_cm_post_receive_srq(dev, i)) {
1569 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1570 "failed for buf %d\n", i);
1571 ipoib_cm_dev_cleanup(dev);
1577 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1581 void ipoib_cm_dev_cleanup(struct net_device *dev)
1583 struct ipoib_dev_priv *priv = netdev_priv(dev);
1589 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1591 ret = ib_destroy_srq(priv->cm.srq);
1593 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1595 priv->cm.srq = NULL;
1596 if (!priv->cm.srq_ring)
1599 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
1600 priv->cm.srq_ring = NULL;