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
35 #include <rdma/ib_cm.h>
36 #include <rdma/ib_cache.h>
39 #include <linux/icmpv6.h>
40 #include <linux/delay.h>
44 int ipoib_max_conn_qp = 128;
46 module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
47 MODULE_PARM_DESC(max_nonsrq_conn_qp,
48 "Max number of connected-mode QPs per interface "
49 "(applied only if shared receive queue is not available)");
51 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
52 static int data_debug_level;
54 module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
55 MODULE_PARM_DESC(cm_data_debug_level,
56 "Enable data path debug tracing for connected mode if > 0");
59 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
61 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
62 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
63 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
64 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
66 static struct ib_qp_attr ipoib_cm_err_attr = {
67 .qp_state = IB_QPS_ERR
70 #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
72 static struct ib_send_wr ipoib_cm_rx_drain_wr = {
73 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
77 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
78 struct ib_cm_event *event);
80 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
81 u64 mapping[IPOIB_CM_RX_SG])
85 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
87 for (i = 0; i < frags; ++i)
88 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
91 static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
93 struct ipoib_dev_priv *priv = netdev_priv(dev);
94 struct ib_recv_wr *bad_wr;
97 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
99 for (i = 0; i < priv->cm.num_frags; ++i)
100 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
102 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
104 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
105 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
106 priv->cm.srq_ring[id].mapping);
107 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
108 priv->cm.srq_ring[id].skb = NULL;
114 static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
115 struct ipoib_cm_rx *rx, int id)
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct ib_recv_wr *bad_wr;
121 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
123 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
124 priv->cm.rx_sge[i].addr = rx->rx_ring[id].mapping[i];
126 ret = ib_post_recv(rx->qp, &priv->cm.rx_wr, &bad_wr);
128 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
129 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
130 rx->rx_ring[id].mapping);
131 dev_kfree_skb_any(rx->rx_ring[id].skb);
132 rx->rx_ring[id].skb = NULL;
138 static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
139 struct ipoib_cm_rx_buf *rx_ring,
141 u64 mapping[IPOIB_CM_RX_SG])
143 struct ipoib_dev_priv *priv = netdev_priv(dev);
147 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
152 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
153 * IP header to a multiple of 16.
155 skb_reserve(skb, 12);
157 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
159 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
160 dev_kfree_skb_any(skb);
164 for (i = 0; i < frags; i++) {
165 struct page *page = alloc_page(GFP_ATOMIC);
169 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
171 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
172 0, PAGE_SIZE, DMA_FROM_DEVICE);
173 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
177 rx_ring[id].skb = skb;
182 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
185 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
187 dev_kfree_skb_any(skb);
191 static void ipoib_cm_free_rx_ring(struct net_device *dev,
192 struct ipoib_cm_rx_buf *rx_ring)
194 struct ipoib_dev_priv *priv = netdev_priv(dev);
197 for (i = 0; i < ipoib_recvq_size; ++i)
198 if (rx_ring[i].skb) {
199 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
201 dev_kfree_skb_any(rx_ring[i].skb);
207 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
209 struct ib_send_wr *bad_wr;
210 struct ipoib_cm_rx *p;
212 /* We only reserved 1 extra slot in CQ for drain WRs, so
213 * make sure we have at most 1 outstanding WR. */
214 if (list_empty(&priv->cm.rx_flush_list) ||
215 !list_empty(&priv->cm.rx_drain_list))
219 * QPs on flush list are error state. This way, a "flush
220 * error" WC will be immediately generated for each WR we post.
222 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
223 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
224 ipoib_warn(priv, "failed to post drain wr\n");
226 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
229 static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
231 struct ipoib_cm_rx *p = ctx;
232 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
235 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
238 spin_lock_irqsave(&priv->lock, flags);
239 list_move(&p->list, &priv->cm.rx_flush_list);
240 p->state = IPOIB_CM_RX_FLUSH;
241 ipoib_cm_start_rx_drain(priv);
242 spin_unlock_irqrestore(&priv->lock, flags);
245 static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
246 struct ipoib_cm_rx *p)
248 struct ipoib_dev_priv *priv = netdev_priv(dev);
249 struct ib_qp_init_attr attr = {
250 .event_handler = ipoib_cm_rx_event_handler,
251 .send_cq = priv->cq, /* For drain WR */
254 .cap.max_send_wr = 1, /* For drain WR */
255 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
256 .sq_sig_type = IB_SIGNAL_ALL_WR,
257 .qp_type = IB_QPT_RC,
261 if (!ipoib_cm_has_srq(dev)) {
262 attr.cap.max_recv_wr = ipoib_recvq_size;
263 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
266 return ib_create_qp(priv->pd, &attr);
269 static int ipoib_cm_modify_rx_qp(struct net_device *dev,
270 struct ib_cm_id *cm_id, struct ib_qp *qp,
273 struct ipoib_dev_priv *priv = netdev_priv(dev);
274 struct ib_qp_attr qp_attr;
275 int qp_attr_mask, ret;
277 qp_attr.qp_state = IB_QPS_INIT;
278 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
280 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
283 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
285 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
288 qp_attr.qp_state = IB_QPS_RTR;
289 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
291 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
294 qp_attr.rq_psn = psn;
295 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
297 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
302 * Current Mellanox HCA firmware won't generate completions
303 * with error for drain WRs unless the QP has been moved to
304 * RTS first. This work-around leaves a window where a QP has
305 * moved to error asynchronously, but this will eventually get
306 * fixed in firmware, so let's not error out if modify QP
309 qp_attr.qp_state = IB_QPS_RTS;
310 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
312 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
315 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
317 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
324 static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
325 struct ipoib_cm_rx *rx)
327 struct ipoib_dev_priv *priv = netdev_priv(dev);
331 rx->rx_ring = kcalloc(ipoib_recvq_size, sizeof *rx->rx_ring, GFP_KERNEL);
335 spin_lock_irq(&priv->lock);
337 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
338 spin_unlock_irq(&priv->lock);
339 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
343 ++priv->cm.nonsrq_conn_qp;
345 spin_unlock_irq(&priv->lock);
347 for (i = 0; i < ipoib_recvq_size; ++i) {
348 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
349 rx->rx_ring[i].mapping)) {
350 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
354 ret = ipoib_cm_post_receive_nonsrq(dev, rx, i);
356 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
357 "failed for buf %d\n", i);
363 rx->recv_count = ipoib_recvq_size;
368 spin_lock_irq(&priv->lock);
369 --priv->cm.nonsrq_conn_qp;
370 spin_unlock_irq(&priv->lock);
373 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
378 static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
379 struct ib_qp *qp, struct ib_cm_req_event_param *req,
382 struct ipoib_dev_priv *priv = netdev_priv(dev);
383 struct ipoib_cm_data data = {};
384 struct ib_cm_rep_param rep = {};
386 data.qpn = cpu_to_be32(priv->qp->qp_num);
387 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
389 rep.private_data = &data;
390 rep.private_data_len = sizeof data;
391 rep.flow_control = 0;
392 rep.rnr_retry_count = req->rnr_retry_count;
393 rep.srq = ipoib_cm_has_srq(dev);
394 rep.qp_num = qp->qp_num;
395 rep.starting_psn = psn;
396 return ib_send_cm_rep(cm_id, &rep);
399 static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
401 struct net_device *dev = cm_id->context;
402 struct ipoib_dev_priv *priv = netdev_priv(dev);
403 struct ipoib_cm_rx *p;
407 ipoib_dbg(priv, "REQ arrived\n");
408 p = kzalloc(sizeof *p, GFP_KERNEL);
414 p->state = IPOIB_CM_RX_LIVE;
415 p->jiffies = jiffies;
416 INIT_LIST_HEAD(&p->list);
418 p->qp = ipoib_cm_create_rx_qp(dev, p);
420 ret = PTR_ERR(p->qp);
424 psn = random32() & 0xffffff;
425 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
429 if (!ipoib_cm_has_srq(dev)) {
430 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
435 spin_lock_irq(&priv->lock);
436 queue_delayed_work(ipoib_workqueue,
437 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
438 /* Add this entry to passive ids list head, but do not re-add it
439 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
440 p->jiffies = jiffies;
441 if (p->state == IPOIB_CM_RX_LIVE)
442 list_move(&p->list, &priv->cm.passive_ids);
443 spin_unlock_irq(&priv->lock);
445 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
447 ipoib_warn(priv, "failed to send REP: %d\n", ret);
448 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
449 ipoib_warn(priv, "unable to move qp to error state\n");
454 ib_destroy_qp(p->qp);
460 static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
461 struct ib_cm_event *event)
463 struct ipoib_cm_rx *p;
464 struct ipoib_dev_priv *priv;
466 switch (event->event) {
467 case IB_CM_REQ_RECEIVED:
468 return ipoib_cm_req_handler(cm_id, event);
469 case IB_CM_DREQ_RECEIVED:
471 ib_send_cm_drep(cm_id, NULL, 0);
473 case IB_CM_REJ_RECEIVED:
475 priv = netdev_priv(p->dev);
476 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
477 ipoib_warn(priv, "unable to move qp to error state\n");
483 /* Adjust length of skb with fragments to match received data */
484 static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
485 unsigned int length, struct sk_buff *toskb)
490 /* put header into skb */
491 size = min(length, hdr_space);
496 num_frags = skb_shinfo(skb)->nr_frags;
497 for (i = 0; i < num_frags; i++) {
498 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
501 /* don't need this page */
502 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
503 --skb_shinfo(skb)->nr_frags;
505 size = min(length, (unsigned) PAGE_SIZE);
508 skb->data_len += size;
509 skb->truesize += size;
516 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
518 struct ipoib_dev_priv *priv = netdev_priv(dev);
519 struct ipoib_cm_rx_buf *rx_ring;
520 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
521 struct sk_buff *skb, *newskb;
522 struct ipoib_cm_rx *p;
524 u64 mapping[IPOIB_CM_RX_SG];
528 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
531 if (unlikely(wr_id >= ipoib_recvq_size)) {
532 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
533 spin_lock_irqsave(&priv->lock, flags);
534 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
535 ipoib_cm_start_rx_drain(priv);
536 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
537 spin_unlock_irqrestore(&priv->lock, flags);
539 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
540 wr_id, ipoib_recvq_size);
544 p = wc->qp->qp_context;
546 has_srq = ipoib_cm_has_srq(dev);
547 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
549 skb = rx_ring[wr_id].skb;
551 if (unlikely(wc->status != IB_WC_SUCCESS)) {
552 ipoib_dbg(priv, "cm recv error "
553 "(status=%d, wrid=%d vend_err %x)\n",
554 wc->status, wr_id, wc->vendor_err);
555 ++dev->stats.rx_dropped;
559 if (!--p->recv_count) {
560 spin_lock_irqsave(&priv->lock, flags);
561 list_move(&p->list, &priv->cm.rx_reap_list);
562 spin_unlock_irqrestore(&priv->lock, flags);
563 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
569 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
570 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
571 spin_lock_irqsave(&priv->lock, flags);
572 p->jiffies = jiffies;
573 /* Move this entry to list head, but do not re-add it
574 * if it has been moved out of list. */
575 if (p->state == IPOIB_CM_RX_LIVE)
576 list_move(&p->list, &priv->cm.passive_ids);
577 spin_unlock_irqrestore(&priv->lock, flags);
581 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
582 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
584 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
585 if (unlikely(!newskb)) {
587 * If we can't allocate a new RX buffer, dump
588 * this packet and reuse the old buffer.
590 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
591 ++dev->stats.rx_dropped;
595 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
596 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
598 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
599 wc->byte_len, wc->slid);
601 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
603 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
604 skb_reset_mac_header(skb);
605 skb_pull(skb, IPOIB_ENCAP_LEN);
607 dev->last_rx = jiffies;
608 ++dev->stats.rx_packets;
609 dev->stats.rx_bytes += skb->len;
612 /* XXX get correct PACKET_ type here */
613 skb->pkt_type = PACKET_HOST;
614 netif_receive_skb(skb);
618 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
619 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
620 "for buf %d\n", wr_id);
622 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, wr_id))) {
624 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
625 "for buf %d\n", wr_id);
630 static inline int post_send(struct ipoib_dev_priv *priv,
631 struct ipoib_cm_tx *tx,
635 struct ib_send_wr *bad_wr;
637 priv->tx_sge[0].addr = addr;
638 priv->tx_sge[0].length = len;
640 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
642 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
645 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
647 struct ipoib_dev_priv *priv = netdev_priv(dev);
648 struct ipoib_tx_buf *tx_req;
651 if (unlikely(skb->len > tx->mtu)) {
652 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
654 ++dev->stats.tx_dropped;
655 ++dev->stats.tx_errors;
656 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
660 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
661 tx->tx_head, skb->len, tx->qp->qp_num);
664 * We put the skb into the tx_ring _before_ we call post_send()
665 * because it's entirely possible that the completion handler will
666 * run before we execute anything after the post_send(). That
667 * means we have to make sure everything is properly recorded and
668 * our state is consistent before we call post_send().
670 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
672 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
673 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
674 ++dev->stats.tx_errors;
675 dev_kfree_skb_any(skb);
679 tx_req->mapping[0] = addr;
681 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
683 ipoib_warn(priv, "post_send failed\n");
684 ++dev->stats.tx_errors;
685 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
686 dev_kfree_skb_any(skb);
688 dev->trans_start = jiffies;
691 if (++priv->tx_outstanding == ipoib_sendq_size) {
692 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
694 netif_stop_queue(dev);
699 void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
701 struct ipoib_dev_priv *priv = netdev_priv(dev);
702 struct ipoib_cm_tx *tx = wc->qp->qp_context;
703 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
704 struct ipoib_tx_buf *tx_req;
707 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
710 if (unlikely(wr_id >= ipoib_sendq_size)) {
711 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
712 wr_id, ipoib_sendq_size);
716 tx_req = &tx->tx_ring[wr_id];
718 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len, DMA_TO_DEVICE);
720 /* FIXME: is this right? Shouldn't we only increment on success? */
721 ++dev->stats.tx_packets;
722 dev->stats.tx_bytes += tx_req->skb->len;
724 dev_kfree_skb_any(tx_req->skb);
726 spin_lock_irqsave(&priv->tx_lock, flags);
728 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
729 netif_queue_stopped(dev) &&
730 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
731 netif_wake_queue(dev);
733 if (wc->status != IB_WC_SUCCESS &&
734 wc->status != IB_WC_WR_FLUSH_ERR) {
735 struct ipoib_neigh *neigh;
737 ipoib_dbg(priv, "failed cm send event "
738 "(status=%d, wrid=%d vend_err %x)\n",
739 wc->status, wr_id, wc->vendor_err);
741 spin_lock(&priv->lock);
746 list_del(&neigh->list);
748 ipoib_put_ah(neigh->ah);
749 ipoib_neigh_free(dev, neigh);
754 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
755 list_move(&tx->list, &priv->cm.reap_list);
756 queue_work(ipoib_workqueue, &priv->cm.reap_task);
759 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
761 spin_unlock(&priv->lock);
764 spin_unlock_irqrestore(&priv->tx_lock, flags);
767 int ipoib_cm_dev_open(struct net_device *dev)
769 struct ipoib_dev_priv *priv = netdev_priv(dev);
772 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
775 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
776 if (IS_ERR(priv->cm.id)) {
777 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
778 ret = PTR_ERR(priv->cm.id);
782 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
785 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
786 IPOIB_CM_IETF_ID | priv->qp->qp_num);
793 ib_destroy_cm_id(priv->cm.id);
799 static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
801 struct ipoib_dev_priv *priv = netdev_priv(dev);
802 struct ipoib_cm_rx *rx, *n;
805 spin_lock_irq(&priv->lock);
806 list_splice_init(&priv->cm.rx_reap_list, &list);
807 spin_unlock_irq(&priv->lock);
809 list_for_each_entry_safe(rx, n, &list, list) {
810 ib_destroy_cm_id(rx->id);
811 ib_destroy_qp(rx->qp);
812 if (!ipoib_cm_has_srq(dev)) {
813 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
814 spin_lock_irq(&priv->lock);
815 --priv->cm.nonsrq_conn_qp;
816 spin_unlock_irq(&priv->lock);
822 void ipoib_cm_dev_stop(struct net_device *dev)
824 struct ipoib_dev_priv *priv = netdev_priv(dev);
825 struct ipoib_cm_rx *p;
830 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
833 ib_destroy_cm_id(priv->cm.id);
836 spin_lock_irq(&priv->lock);
837 while (!list_empty(&priv->cm.passive_ids)) {
838 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
839 list_move(&p->list, &priv->cm.rx_error_list);
840 p->state = IPOIB_CM_RX_ERROR;
841 spin_unlock_irq(&priv->lock);
842 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
844 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
845 spin_lock_irq(&priv->lock);
848 /* Wait for all RX to be drained */
851 while (!list_empty(&priv->cm.rx_error_list) ||
852 !list_empty(&priv->cm.rx_flush_list) ||
853 !list_empty(&priv->cm.rx_drain_list)) {
854 if (time_after(jiffies, begin + 5 * HZ)) {
855 ipoib_warn(priv, "RX drain timing out\n");
858 * assume the HW is wedged and just free up everything.
860 list_splice_init(&priv->cm.rx_flush_list, &list);
861 list_splice_init(&priv->cm.rx_error_list, &list);
862 list_splice_init(&priv->cm.rx_drain_list, &list);
865 spin_unlock_irq(&priv->lock);
868 spin_lock_irq(&priv->lock);
871 spin_unlock_irq(&priv->lock);
873 ipoib_cm_free_rx_reap_list(dev);
875 cancel_delayed_work(&priv->cm.stale_task);
878 static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
880 struct ipoib_cm_tx *p = cm_id->context;
881 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
882 struct ipoib_cm_data *data = event->private_data;
883 struct sk_buff_head skqueue;
884 struct ib_qp_attr qp_attr;
885 int qp_attr_mask, ret;
888 p->mtu = be32_to_cpu(data->mtu);
890 if (p->mtu <= IPOIB_ENCAP_LEN) {
891 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
892 p->mtu, IPOIB_ENCAP_LEN);
896 qp_attr.qp_state = IB_QPS_RTR;
897 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
899 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
903 qp_attr.rq_psn = 0 /* FIXME */;
904 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
906 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
910 qp_attr.qp_state = IB_QPS_RTS;
911 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
913 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
916 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
918 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
922 skb_queue_head_init(&skqueue);
924 spin_lock_irq(&priv->lock);
925 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
927 while ((skb = __skb_dequeue(&p->neigh->queue)))
928 __skb_queue_tail(&skqueue, skb);
929 spin_unlock_irq(&priv->lock);
931 while ((skb = __skb_dequeue(&skqueue))) {
933 if (dev_queue_xmit(skb))
934 ipoib_warn(priv, "dev_queue_xmit failed "
935 "to requeue packet\n");
938 ret = ib_send_cm_rtu(cm_id, NULL, 0);
940 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
946 static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
948 struct ipoib_dev_priv *priv = netdev_priv(dev);
949 struct ib_qp_init_attr attr = {
953 .cap.max_send_wr = ipoib_sendq_size,
954 .cap.max_send_sge = 1,
955 .sq_sig_type = IB_SIGNAL_ALL_WR,
956 .qp_type = IB_QPT_RC,
960 return ib_create_qp(priv->pd, &attr);
963 static int ipoib_cm_send_req(struct net_device *dev,
964 struct ib_cm_id *id, struct ib_qp *qp,
966 struct ib_sa_path_rec *pathrec)
968 struct ipoib_dev_priv *priv = netdev_priv(dev);
969 struct ipoib_cm_data data = {};
970 struct ib_cm_req_param req = {};
972 data.qpn = cpu_to_be32(priv->qp->qp_num);
973 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
975 req.primary_path = pathrec;
976 req.alternate_path = NULL;
977 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
978 req.qp_num = qp->qp_num;
979 req.qp_type = qp->qp_type;
980 req.private_data = &data;
981 req.private_data_len = sizeof data;
982 req.flow_control = 0;
984 req.starting_psn = 0; /* FIXME */
987 * Pick some arbitrary defaults here; we could make these
988 * module parameters if anyone cared about setting them.
990 req.responder_resources = 4;
991 req.remote_cm_response_timeout = 20;
992 req.local_cm_response_timeout = 20;
993 req.retry_count = 0; /* RFC draft warns against retries */
994 req.rnr_retry_count = 0; /* RFC draft warns against retries */
995 req.max_cm_retries = 15;
996 req.srq = ipoib_cm_has_srq(dev);
997 return ib_send_cm_req(id, &req);
1000 static int ipoib_cm_modify_tx_init(struct net_device *dev,
1001 struct ib_cm_id *cm_id, struct ib_qp *qp)
1003 struct ipoib_dev_priv *priv = netdev_priv(dev);
1004 struct ib_qp_attr qp_attr;
1005 int qp_attr_mask, ret;
1006 ret = ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
1008 ipoib_warn(priv, "pkey 0x%x not in cache: %d\n", priv->pkey, ret);
1012 qp_attr.qp_state = IB_QPS_INIT;
1013 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1014 qp_attr.port_num = priv->port;
1015 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1017 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1019 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1025 static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1026 struct ib_sa_path_rec *pathrec)
1028 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1031 p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1034 ipoib_warn(priv, "failed to allocate tx ring\n");
1039 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
1040 if (IS_ERR(p->qp)) {
1041 ret = PTR_ERR(p->qp);
1042 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1046 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1047 if (IS_ERR(p->id)) {
1048 ret = PTR_ERR(p->id);
1049 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1053 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1055 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1059 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1061 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1065 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
1066 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
1072 ib_destroy_cm_id(p->id);
1075 ib_destroy_qp(p->qp);
1082 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1084 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1085 struct ipoib_tx_buf *tx_req;
1086 unsigned long flags;
1087 unsigned long begin;
1089 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1090 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1093 ib_destroy_cm_id(p->id);
1096 /* Wait for all sends to complete */
1098 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1099 if (time_after(jiffies, begin + 5 * HZ)) {
1100 ipoib_warn(priv, "timing out; %d sends not completed\n",
1101 p->tx_head - p->tx_tail);
1111 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1112 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1113 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len,
1115 dev_kfree_skb_any(tx_req->skb);
1117 spin_lock_irqsave(&priv->tx_lock, flags);
1118 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1119 netif_queue_stopped(p->dev) &&
1120 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1121 netif_wake_queue(p->dev);
1122 spin_unlock_irqrestore(&priv->tx_lock, flags);
1126 ib_destroy_qp(p->qp);
1132 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1133 struct ib_cm_event *event)
1135 struct ipoib_cm_tx *tx = cm_id->context;
1136 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1137 struct net_device *dev = priv->dev;
1138 struct ipoib_neigh *neigh;
1141 switch (event->event) {
1142 case IB_CM_DREQ_RECEIVED:
1143 ipoib_dbg(priv, "DREQ received.\n");
1144 ib_send_cm_drep(cm_id, NULL, 0);
1146 case IB_CM_REP_RECEIVED:
1147 ipoib_dbg(priv, "REP received.\n");
1148 ret = ipoib_cm_rep_handler(cm_id, event);
1150 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1153 case IB_CM_REQ_ERROR:
1154 case IB_CM_REJ_RECEIVED:
1155 case IB_CM_TIMEWAIT_EXIT:
1156 ipoib_dbg(priv, "CM error %d.\n", event->event);
1157 spin_lock_irq(&priv->tx_lock);
1158 spin_lock(&priv->lock);
1163 list_del(&neigh->list);
1165 ipoib_put_ah(neigh->ah);
1166 ipoib_neigh_free(dev, neigh);
1171 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1172 list_move(&tx->list, &priv->cm.reap_list);
1173 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1176 spin_unlock(&priv->lock);
1177 spin_unlock_irq(&priv->tx_lock);
1186 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1187 struct ipoib_neigh *neigh)
1189 struct ipoib_dev_priv *priv = netdev_priv(dev);
1190 struct ipoib_cm_tx *tx;
1192 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1200 list_add(&tx->list, &priv->cm.start_list);
1201 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1202 queue_work(ipoib_workqueue, &priv->cm.start_task);
1206 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1208 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1209 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1210 list_move(&tx->list, &priv->cm.reap_list);
1211 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1212 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1213 IPOIB_GID_ARG(tx->neigh->dgid));
1218 static void ipoib_cm_tx_start(struct work_struct *work)
1220 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1222 struct net_device *dev = priv->dev;
1223 struct ipoib_neigh *neigh;
1224 struct ipoib_cm_tx *p;
1225 unsigned long flags;
1228 struct ib_sa_path_rec pathrec;
1231 spin_lock_irqsave(&priv->tx_lock, flags);
1232 spin_lock(&priv->lock);
1233 while (!list_empty(&priv->cm.start_list)) {
1234 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1235 list_del_init(&p->list);
1237 qpn = IPOIB_QPN(neigh->neighbour->ha);
1238 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1239 spin_unlock(&priv->lock);
1240 spin_unlock_irqrestore(&priv->tx_lock, flags);
1241 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1242 spin_lock_irqsave(&priv->tx_lock, flags);
1243 spin_lock(&priv->lock);
1248 list_del(&neigh->list);
1250 ipoib_put_ah(neigh->ah);
1251 ipoib_neigh_free(dev, neigh);
1257 spin_unlock(&priv->lock);
1258 spin_unlock_irqrestore(&priv->tx_lock, flags);
1261 static void ipoib_cm_tx_reap(struct work_struct *work)
1263 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1265 struct ipoib_cm_tx *p;
1267 spin_lock_irq(&priv->tx_lock);
1268 spin_lock(&priv->lock);
1269 while (!list_empty(&priv->cm.reap_list)) {
1270 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1272 spin_unlock(&priv->lock);
1273 spin_unlock_irq(&priv->tx_lock);
1274 ipoib_cm_tx_destroy(p);
1275 spin_lock_irq(&priv->tx_lock);
1276 spin_lock(&priv->lock);
1278 spin_unlock(&priv->lock);
1279 spin_unlock_irq(&priv->tx_lock);
1282 static void ipoib_cm_skb_reap(struct work_struct *work)
1284 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1286 struct sk_buff *skb;
1288 unsigned mtu = priv->mcast_mtu;
1290 spin_lock_irq(&priv->tx_lock);
1291 spin_lock(&priv->lock);
1292 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1293 spin_unlock(&priv->lock);
1294 spin_unlock_irq(&priv->tx_lock);
1295 if (skb->protocol == htons(ETH_P_IP))
1296 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1297 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1298 else if (skb->protocol == htons(ETH_P_IPV6))
1299 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
1301 dev_kfree_skb_any(skb);
1302 spin_lock_irq(&priv->tx_lock);
1303 spin_lock(&priv->lock);
1305 spin_unlock(&priv->lock);
1306 spin_unlock_irq(&priv->tx_lock);
1309 void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
1312 struct ipoib_dev_priv *priv = netdev_priv(dev);
1313 int e = skb_queue_empty(&priv->cm.skb_queue);
1316 skb->dst->ops->update_pmtu(skb->dst, mtu);
1318 skb_queue_tail(&priv->cm.skb_queue, skb);
1320 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1323 static void ipoib_cm_rx_reap(struct work_struct *work)
1325 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1326 cm.rx_reap_task)->dev);
1329 static void ipoib_cm_stale_task(struct work_struct *work)
1331 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1332 cm.stale_task.work);
1333 struct ipoib_cm_rx *p;
1336 spin_lock_irq(&priv->lock);
1337 while (!list_empty(&priv->cm.passive_ids)) {
1338 /* List is sorted by LRU, start from tail,
1339 * stop when we see a recently used entry */
1340 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1341 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1343 list_move(&p->list, &priv->cm.rx_error_list);
1344 p->state = IPOIB_CM_RX_ERROR;
1345 spin_unlock_irq(&priv->lock);
1346 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1348 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1349 spin_lock_irq(&priv->lock);
1352 if (!list_empty(&priv->cm.passive_ids))
1353 queue_delayed_work(ipoib_workqueue,
1354 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1355 spin_unlock_irq(&priv->lock);
1359 static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1362 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1364 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1365 return sprintf(buf, "connected\n");
1367 return sprintf(buf, "datagram\n");
1370 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1371 const char *buf, size_t count)
1373 struct net_device *dev = to_net_dev(d);
1374 struct ipoib_dev_priv *priv = netdev_priv(dev);
1376 /* flush paths if we switch modes so that connections are restarted */
1377 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1378 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1379 ipoib_warn(priv, "enabling connected mode "
1380 "will cause multicast packet drops\n");
1381 ipoib_flush_paths(dev);
1385 if (!strcmp(buf, "datagram\n")) {
1386 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1387 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1388 ipoib_flush_paths(dev);
1395 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1397 int ipoib_cm_add_mode_attr(struct net_device *dev)
1399 return device_create_file(&dev->dev, &dev_attr_mode);
1402 static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1404 struct ipoib_dev_priv *priv = netdev_priv(dev);
1405 struct ib_srq_init_attr srq_init_attr = {
1407 .max_wr = ipoib_recvq_size,
1412 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1413 if (IS_ERR(priv->cm.srq)) {
1414 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1415 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1416 priv->ca->name, PTR_ERR(priv->cm.srq));
1417 priv->cm.srq = NULL;
1421 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1423 if (!priv->cm.srq_ring) {
1424 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1425 priv->ca->name, ipoib_recvq_size);
1426 ib_destroy_srq(priv->cm.srq);
1427 priv->cm.srq = NULL;
1431 int ipoib_cm_dev_init(struct net_device *dev)
1433 struct ipoib_dev_priv *priv = netdev_priv(dev);
1435 struct ib_device_attr attr;
1437 INIT_LIST_HEAD(&priv->cm.passive_ids);
1438 INIT_LIST_HEAD(&priv->cm.reap_list);
1439 INIT_LIST_HEAD(&priv->cm.start_list);
1440 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1441 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1442 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1443 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1444 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1445 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1446 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1447 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1448 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1450 skb_queue_head_init(&priv->cm.skb_queue);
1452 ret = ib_query_device(priv->ca, &attr);
1454 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1458 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1460 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1461 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1462 if (ipoib_cm_has_srq(dev)) {
1463 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1464 priv->cm.num_frags = attr.max_srq_sge;
1465 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1466 priv->cm.max_cm_mtu, priv->cm.num_frags);
1468 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1469 priv->cm.num_frags = IPOIB_CM_RX_SG;
1472 for (i = 0; i < priv->cm.num_frags; ++i)
1473 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1475 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
1476 for (i = 1; i < priv->cm.num_frags; ++i)
1477 priv->cm.rx_sge[i].length = PAGE_SIZE;
1478 priv->cm.rx_wr.next = NULL;
1479 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
1480 priv->cm.rx_wr.num_sge = priv->cm.num_frags;
1482 if (ipoib_cm_has_srq(dev)) {
1483 for (i = 0; i < ipoib_recvq_size; ++i) {
1484 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
1485 priv->cm.num_frags - 1,
1486 priv->cm.srq_ring[i].mapping)) {
1487 ipoib_warn(priv, "failed to allocate "
1488 "receive buffer %d\n", i);
1489 ipoib_cm_dev_cleanup(dev);
1493 if (ipoib_cm_post_receive_srq(dev, i)) {
1494 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1495 "failed for buf %d\n", i);
1496 ipoib_cm_dev_cleanup(dev);
1502 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1506 void ipoib_cm_dev_cleanup(struct net_device *dev)
1508 struct ipoib_dev_priv *priv = netdev_priv(dev);
1514 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1516 ret = ib_destroy_srq(priv->cm.srq);
1518 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1520 priv->cm.srq = NULL;
1521 if (!priv->cm.srq_ring)
1524 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
1525 priv->cm.srq_ring = NULL;