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>
42 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
43 static int data_debug_level;
45 module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
46 MODULE_PARM_DESC(cm_data_debug_level,
47 "Enable data path debug tracing for connected mode if > 0");
52 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
54 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
55 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
56 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
57 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
59 static struct ib_qp_attr ipoib_cm_err_attr = {
60 .qp_state = IB_QPS_ERR
63 #define IPOIB_CM_RX_DRAIN_WRID 0x7fffffff
65 static struct ib_send_wr ipoib_cm_rx_drain_wr = {
66 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
70 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
71 struct ib_cm_event *event);
73 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
74 u64 mapping[IPOIB_CM_RX_SG])
78 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
80 for (i = 0; i < frags; ++i)
81 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
84 static int ipoib_cm_post_receive(struct net_device *dev, int id)
86 struct ipoib_dev_priv *priv = netdev_priv(dev);
87 struct ib_recv_wr *bad_wr;
90 priv->cm.rx_wr.wr_id = id | IPOIB_CM_OP_SRQ;
92 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
93 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
95 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
97 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
98 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
99 priv->cm.srq_ring[id].mapping);
100 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
101 priv->cm.srq_ring[id].skb = NULL;
107 static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, int id, int frags,
108 u64 mapping[IPOIB_CM_RX_SG])
110 struct ipoib_dev_priv *priv = netdev_priv(dev);
114 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
119 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
120 * IP header to a multiple of 16.
122 skb_reserve(skb, 12);
124 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
126 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
127 dev_kfree_skb_any(skb);
131 for (i = 0; i < frags; i++) {
132 struct page *page = alloc_page(GFP_ATOMIC);
136 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
138 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
139 0, PAGE_SIZE, DMA_FROM_DEVICE);
140 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
144 priv->cm.srq_ring[id].skb = skb;
149 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
152 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
154 dev_kfree_skb_any(skb);
158 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv* priv)
160 struct ib_send_wr *bad_wr;
161 struct ipoib_cm_rx *p;
163 /* We only reserved 1 extra slot in CQ for drain WRs, so
164 * make sure we have at most 1 outstanding WR. */
165 if (list_empty(&priv->cm.rx_flush_list) ||
166 !list_empty(&priv->cm.rx_drain_list))
170 * QPs on flush list are error state. This way, a "flush
171 * error" WC will be immediately generated for each WR we post.
173 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
174 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
175 ipoib_warn(priv, "failed to post drain wr\n");
177 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
180 static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
182 struct ipoib_cm_rx *p = ctx;
183 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
186 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
189 spin_lock_irqsave(&priv->lock, flags);
190 list_move(&p->list, &priv->cm.rx_flush_list);
191 p->state = IPOIB_CM_RX_FLUSH;
192 ipoib_cm_start_rx_drain(priv);
193 spin_unlock_irqrestore(&priv->lock, flags);
196 static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
197 struct ipoib_cm_rx *p)
199 struct ipoib_dev_priv *priv = netdev_priv(dev);
200 struct ib_qp_init_attr attr = {
201 .event_handler = ipoib_cm_rx_event_handler,
202 .send_cq = priv->cq, /* For drain WR */
205 .cap.max_send_wr = 1, /* For drain WR */
206 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
207 .sq_sig_type = IB_SIGNAL_ALL_WR,
208 .qp_type = IB_QPT_RC,
211 return ib_create_qp(priv->pd, &attr);
214 static int ipoib_cm_modify_rx_qp(struct net_device *dev,
215 struct ib_cm_id *cm_id, struct ib_qp *qp,
218 struct ipoib_dev_priv *priv = netdev_priv(dev);
219 struct ib_qp_attr qp_attr;
220 int qp_attr_mask, ret;
222 qp_attr.qp_state = IB_QPS_INIT;
223 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
225 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
228 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
230 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
233 qp_attr.qp_state = IB_QPS_RTR;
234 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
236 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
239 qp_attr.rq_psn = psn;
240 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
242 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
247 * Current Mellanox HCA firmware won't generate completions
248 * with error for drain WRs unless the QP has been moved to
249 * RTS first. This work-around leaves a window where a QP has
250 * moved to error asynchronously, but this will eventually get
251 * fixed in firmware, so let's not error out if modify QP
254 qp_attr.qp_state = IB_QPS_RTS;
255 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
257 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
260 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
262 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
269 static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
270 struct ib_qp *qp, struct ib_cm_req_event_param *req,
273 struct ipoib_dev_priv *priv = netdev_priv(dev);
274 struct ipoib_cm_data data = {};
275 struct ib_cm_rep_param rep = {};
277 data.qpn = cpu_to_be32(priv->qp->qp_num);
278 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
280 rep.private_data = &data;
281 rep.private_data_len = sizeof data;
282 rep.flow_control = 0;
283 rep.rnr_retry_count = req->rnr_retry_count;
285 rep.qp_num = qp->qp_num;
286 rep.starting_psn = psn;
287 return ib_send_cm_rep(cm_id, &rep);
290 static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
292 struct net_device *dev = cm_id->context;
293 struct ipoib_dev_priv *priv = netdev_priv(dev);
294 struct ipoib_cm_rx *p;
298 ipoib_dbg(priv, "REQ arrived\n");
299 p = kzalloc(sizeof *p, GFP_KERNEL);
305 p->state = IPOIB_CM_RX_LIVE;
306 p->jiffies = jiffies;
307 INIT_LIST_HEAD(&p->list);
309 p->qp = ipoib_cm_create_rx_qp(dev, p);
311 ret = PTR_ERR(p->qp);
315 psn = random32() & 0xffffff;
316 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
320 spin_lock_irq(&priv->lock);
321 queue_delayed_work(ipoib_workqueue,
322 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
323 /* Add this entry to passive ids list head, but do not re-add it
324 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
325 p->jiffies = jiffies;
326 if (p->state == IPOIB_CM_RX_LIVE)
327 list_move(&p->list, &priv->cm.passive_ids);
328 spin_unlock_irq(&priv->lock);
330 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
332 ipoib_warn(priv, "failed to send REP: %d\n", ret);
333 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
334 ipoib_warn(priv, "unable to move qp to error state\n");
339 ib_destroy_qp(p->qp);
345 static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
346 struct ib_cm_event *event)
348 struct ipoib_cm_rx *p;
349 struct ipoib_dev_priv *priv;
351 switch (event->event) {
352 case IB_CM_REQ_RECEIVED:
353 return ipoib_cm_req_handler(cm_id, event);
354 case IB_CM_DREQ_RECEIVED:
356 ib_send_cm_drep(cm_id, NULL, 0);
358 case IB_CM_REJ_RECEIVED:
360 priv = netdev_priv(p->dev);
361 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
362 ipoib_warn(priv, "unable to move qp to error state\n");
368 /* Adjust length of skb with fragments to match received data */
369 static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
370 unsigned int length, struct sk_buff *toskb)
375 /* put header into skb */
376 size = min(length, hdr_space);
381 num_frags = skb_shinfo(skb)->nr_frags;
382 for (i = 0; i < num_frags; i++) {
383 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
386 /* don't need this page */
387 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
388 --skb_shinfo(skb)->nr_frags;
390 size = min(length, (unsigned) PAGE_SIZE);
393 skb->data_len += size;
394 skb->truesize += size;
401 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
403 struct ipoib_dev_priv *priv = netdev_priv(dev);
404 unsigned int wr_id = wc->wr_id & ~IPOIB_CM_OP_SRQ;
405 struct sk_buff *skb, *newskb;
406 struct ipoib_cm_rx *p;
408 u64 mapping[IPOIB_CM_RX_SG];
411 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
414 if (unlikely(wr_id >= ipoib_recvq_size)) {
415 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~IPOIB_CM_OP_SRQ)) {
416 spin_lock_irqsave(&priv->lock, flags);
417 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
418 ipoib_cm_start_rx_drain(priv);
419 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
420 spin_unlock_irqrestore(&priv->lock, flags);
422 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
423 wr_id, ipoib_recvq_size);
427 skb = priv->cm.srq_ring[wr_id].skb;
429 if (unlikely(wc->status != IB_WC_SUCCESS)) {
430 ipoib_dbg(priv, "cm recv error "
431 "(status=%d, wrid=%d vend_err %x)\n",
432 wc->status, wr_id, wc->vendor_err);
433 ++dev->stats.rx_dropped;
437 if (!likely(wr_id & IPOIB_CM_RX_UPDATE_MASK)) {
438 p = wc->qp->qp_context;
439 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
440 spin_lock_irqsave(&priv->lock, flags);
441 p->jiffies = jiffies;
442 /* Move this entry to list head, but do not re-add it
443 * if it has been moved out of list. */
444 if (p->state == IPOIB_CM_RX_LIVE)
445 list_move(&p->list, &priv->cm.passive_ids);
446 spin_unlock_irqrestore(&priv->lock, flags);
450 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
451 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
453 newskb = ipoib_cm_alloc_rx_skb(dev, wr_id, frags, mapping);
454 if (unlikely(!newskb)) {
456 * If we can't allocate a new RX buffer, dump
457 * this packet and reuse the old buffer.
459 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
460 ++dev->stats.rx_dropped;
464 ipoib_cm_dma_unmap_rx(priv, frags, priv->cm.srq_ring[wr_id].mapping);
465 memcpy(priv->cm.srq_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
467 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
468 wc->byte_len, wc->slid);
470 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
472 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
473 skb_reset_mac_header(skb);
474 skb_pull(skb, IPOIB_ENCAP_LEN);
476 dev->last_rx = jiffies;
477 ++dev->stats.rx_packets;
478 dev->stats.rx_bytes += skb->len;
481 /* XXX get correct PACKET_ type here */
482 skb->pkt_type = PACKET_HOST;
483 netif_receive_skb(skb);
486 if (unlikely(ipoib_cm_post_receive(dev, wr_id)))
487 ipoib_warn(priv, "ipoib_cm_post_receive failed "
488 "for buf %d\n", wr_id);
491 static inline int post_send(struct ipoib_dev_priv *priv,
492 struct ipoib_cm_tx *tx,
496 struct ib_send_wr *bad_wr;
498 priv->tx_sge.addr = addr;
499 priv->tx_sge.length = len;
501 priv->tx_wr.wr_id = wr_id;
503 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
506 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
508 struct ipoib_dev_priv *priv = netdev_priv(dev);
509 struct ipoib_tx_buf *tx_req;
512 if (unlikely(skb->len > tx->mtu)) {
513 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
515 ++dev->stats.tx_dropped;
516 ++dev->stats.tx_errors;
517 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
521 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
522 tx->tx_head, skb->len, tx->qp->qp_num);
525 * We put the skb into the tx_ring _before_ we call post_send()
526 * because it's entirely possible that the completion handler will
527 * run before we execute anything after the post_send(). That
528 * means we have to make sure everything is properly recorded and
529 * our state is consistent before we call post_send().
531 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
533 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
534 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
535 ++dev->stats.tx_errors;
536 dev_kfree_skb_any(skb);
540 tx_req->mapping = addr;
542 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
544 ipoib_warn(priv, "post_send failed\n");
545 ++dev->stats.tx_errors;
546 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
547 dev_kfree_skb_any(skb);
549 dev->trans_start = jiffies;
552 if (tx->tx_head - tx->tx_tail == ipoib_sendq_size) {
553 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
555 netif_stop_queue(dev);
556 set_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
561 static void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ipoib_cm_tx *tx,
564 struct ipoib_dev_priv *priv = netdev_priv(dev);
565 unsigned int wr_id = wc->wr_id;
566 struct ipoib_tx_buf *tx_req;
569 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
572 if (unlikely(wr_id >= ipoib_sendq_size)) {
573 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
574 wr_id, ipoib_sendq_size);
578 tx_req = &tx->tx_ring[wr_id];
580 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
582 /* FIXME: is this right? Shouldn't we only increment on success? */
583 ++dev->stats.tx_packets;
584 dev->stats.tx_bytes += tx_req->skb->len;
586 dev_kfree_skb_any(tx_req->skb);
588 spin_lock_irqsave(&priv->tx_lock, flags);
590 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags)) &&
591 tx->tx_head - tx->tx_tail <= ipoib_sendq_size >> 1) {
592 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
593 netif_wake_queue(dev);
596 if (wc->status != IB_WC_SUCCESS &&
597 wc->status != IB_WC_WR_FLUSH_ERR) {
598 struct ipoib_neigh *neigh;
600 ipoib_dbg(priv, "failed cm send event "
601 "(status=%d, wrid=%d vend_err %x)\n",
602 wc->status, wr_id, wc->vendor_err);
604 spin_lock(&priv->lock);
609 list_del(&neigh->list);
611 ipoib_put_ah(neigh->ah);
612 ipoib_neigh_free(dev, neigh);
617 /* queue would be re-started anyway when TX is destroyed,
618 * but it makes sense to do it ASAP here. */
619 if (test_and_clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags))
620 netif_wake_queue(dev);
622 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
623 list_move(&tx->list, &priv->cm.reap_list);
624 queue_work(ipoib_workqueue, &priv->cm.reap_task);
627 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
629 spin_unlock(&priv->lock);
632 spin_unlock_irqrestore(&priv->tx_lock, flags);
635 static void ipoib_cm_tx_completion(struct ib_cq *cq, void *tx_ptr)
637 struct ipoib_cm_tx *tx = tx_ptr;
640 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
642 n = ib_poll_cq(cq, IPOIB_NUM_WC, tx->ibwc);
643 for (i = 0; i < n; ++i)
644 ipoib_cm_handle_tx_wc(tx->dev, tx, tx->ibwc + i);
645 } while (n == IPOIB_NUM_WC);
648 int ipoib_cm_dev_open(struct net_device *dev)
650 struct ipoib_dev_priv *priv = netdev_priv(dev);
653 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
656 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
657 if (IS_ERR(priv->cm.id)) {
658 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
659 ret = PTR_ERR(priv->cm.id);
663 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
666 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
667 IPOIB_CM_IETF_ID | priv->qp->qp_num);
674 ib_destroy_cm_id(priv->cm.id);
680 void ipoib_cm_dev_stop(struct net_device *dev)
682 struct ipoib_dev_priv *priv = netdev_priv(dev);
683 struct ipoib_cm_rx *p, *n;
688 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
691 ib_destroy_cm_id(priv->cm.id);
694 spin_lock_irq(&priv->lock);
695 while (!list_empty(&priv->cm.passive_ids)) {
696 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
697 list_move(&p->list, &priv->cm.rx_error_list);
698 p->state = IPOIB_CM_RX_ERROR;
699 spin_unlock_irq(&priv->lock);
700 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
702 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
703 spin_lock_irq(&priv->lock);
706 /* Wait for all RX to be drained */
709 while (!list_empty(&priv->cm.rx_error_list) ||
710 !list_empty(&priv->cm.rx_flush_list) ||
711 !list_empty(&priv->cm.rx_drain_list)) {
712 if (time_after(jiffies, begin + 5 * HZ)) {
713 ipoib_warn(priv, "RX drain timing out\n");
716 * assume the HW is wedged and just free up everything.
718 list_splice_init(&priv->cm.rx_flush_list, &list);
719 list_splice_init(&priv->cm.rx_error_list, &list);
720 list_splice_init(&priv->cm.rx_drain_list, &list);
723 spin_unlock_irq(&priv->lock);
726 spin_lock_irq(&priv->lock);
729 list_splice_init(&priv->cm.rx_reap_list, &list);
731 spin_unlock_irq(&priv->lock);
733 list_for_each_entry_safe(p, n, &list, list) {
734 ib_destroy_cm_id(p->id);
735 ib_destroy_qp(p->qp);
739 cancel_delayed_work(&priv->cm.stale_task);
742 static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
744 struct ipoib_cm_tx *p = cm_id->context;
745 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
746 struct ipoib_cm_data *data = event->private_data;
747 struct sk_buff_head skqueue;
748 struct ib_qp_attr qp_attr;
749 int qp_attr_mask, ret;
752 p->mtu = be32_to_cpu(data->mtu);
754 if (p->mtu <= IPOIB_ENCAP_LEN) {
755 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
756 p->mtu, IPOIB_ENCAP_LEN);
760 qp_attr.qp_state = IB_QPS_RTR;
761 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
763 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
767 qp_attr.rq_psn = 0 /* FIXME */;
768 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
770 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
774 qp_attr.qp_state = IB_QPS_RTS;
775 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
777 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
780 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
782 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
786 skb_queue_head_init(&skqueue);
788 spin_lock_irq(&priv->lock);
789 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
791 while ((skb = __skb_dequeue(&p->neigh->queue)))
792 __skb_queue_tail(&skqueue, skb);
793 spin_unlock_irq(&priv->lock);
795 while ((skb = __skb_dequeue(&skqueue))) {
797 if (dev_queue_xmit(skb))
798 ipoib_warn(priv, "dev_queue_xmit failed "
799 "to requeue packet\n");
802 ret = ib_send_cm_rtu(cm_id, NULL, 0);
804 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
810 static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ib_cq *cq)
812 struct ipoib_dev_priv *priv = netdev_priv(dev);
813 struct ib_qp_init_attr attr = {
817 .cap.max_send_wr = ipoib_sendq_size,
818 .cap.max_send_sge = 1,
819 .sq_sig_type = IB_SIGNAL_ALL_WR,
820 .qp_type = IB_QPT_RC,
823 return ib_create_qp(priv->pd, &attr);
826 static int ipoib_cm_send_req(struct net_device *dev,
827 struct ib_cm_id *id, struct ib_qp *qp,
829 struct ib_sa_path_rec *pathrec)
831 struct ipoib_dev_priv *priv = netdev_priv(dev);
832 struct ipoib_cm_data data = {};
833 struct ib_cm_req_param req = {};
835 data.qpn = cpu_to_be32(priv->qp->qp_num);
836 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
838 req.primary_path = pathrec;
839 req.alternate_path = NULL;
840 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
841 req.qp_num = qp->qp_num;
842 req.qp_type = qp->qp_type;
843 req.private_data = &data;
844 req.private_data_len = sizeof data;
845 req.flow_control = 0;
847 req.starting_psn = 0; /* FIXME */
850 * Pick some arbitrary defaults here; we could make these
851 * module parameters if anyone cared about setting them.
853 req.responder_resources = 4;
854 req.remote_cm_response_timeout = 20;
855 req.local_cm_response_timeout = 20;
856 req.retry_count = 0; /* RFC draft warns against retries */
857 req.rnr_retry_count = 0; /* RFC draft warns against retries */
858 req.max_cm_retries = 15;
860 return ib_send_cm_req(id, &req);
863 static int ipoib_cm_modify_tx_init(struct net_device *dev,
864 struct ib_cm_id *cm_id, struct ib_qp *qp)
866 struct ipoib_dev_priv *priv = netdev_priv(dev);
867 struct ib_qp_attr qp_attr;
868 int qp_attr_mask, ret;
869 ret = ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
871 ipoib_warn(priv, "pkey 0x%x not in cache: %d\n", priv->pkey, ret);
875 qp_attr.qp_state = IB_QPS_INIT;
876 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
877 qp_attr.port_num = priv->port;
878 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
880 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
882 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
888 static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
889 struct ib_sa_path_rec *pathrec)
891 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
894 p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring,
897 ipoib_warn(priv, "failed to allocate tx ring\n");
902 p->cq = ib_create_cq(priv->ca, ipoib_cm_tx_completion, NULL, p,
903 ipoib_sendq_size + 1, 0);
905 ret = PTR_ERR(p->cq);
906 ipoib_warn(priv, "failed to allocate tx cq: %d\n", ret);
910 ret = ib_req_notify_cq(p->cq, IB_CQ_NEXT_COMP);
912 ipoib_warn(priv, "failed to request completion notification: %d\n", ret);
916 p->qp = ipoib_cm_create_tx_qp(p->dev, p->cq);
918 ret = PTR_ERR(p->qp);
919 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
923 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
925 ret = PTR_ERR(p->id);
926 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
930 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
932 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
936 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
938 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
942 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
943 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
949 ib_destroy_cm_id(p->id);
952 ib_destroy_qp(p->qp);
956 ib_destroy_cq(p->cq);
963 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
965 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
966 struct ipoib_tx_buf *tx_req;
968 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
969 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
972 ib_destroy_cm_id(p->id);
975 ib_destroy_qp(p->qp);
978 ib_destroy_cq(p->cq);
980 if (test_bit(IPOIB_FLAG_NETIF_STOPPED, &p->flags))
981 netif_wake_queue(p->dev);
984 while ((int) p->tx_tail - (int) p->tx_head < 0) {
985 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
986 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
988 dev_kfree_skb_any(tx_req->skb);
998 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
999 struct ib_cm_event *event)
1001 struct ipoib_cm_tx *tx = cm_id->context;
1002 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1003 struct net_device *dev = priv->dev;
1004 struct ipoib_neigh *neigh;
1007 switch (event->event) {
1008 case IB_CM_DREQ_RECEIVED:
1009 ipoib_dbg(priv, "DREQ received.\n");
1010 ib_send_cm_drep(cm_id, NULL, 0);
1012 case IB_CM_REP_RECEIVED:
1013 ipoib_dbg(priv, "REP received.\n");
1014 ret = ipoib_cm_rep_handler(cm_id, event);
1016 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1019 case IB_CM_REQ_ERROR:
1020 case IB_CM_REJ_RECEIVED:
1021 case IB_CM_TIMEWAIT_EXIT:
1022 ipoib_dbg(priv, "CM error %d.\n", event->event);
1023 spin_lock_irq(&priv->tx_lock);
1024 spin_lock(&priv->lock);
1029 list_del(&neigh->list);
1031 ipoib_put_ah(neigh->ah);
1032 ipoib_neigh_free(dev, neigh);
1037 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1038 list_move(&tx->list, &priv->cm.reap_list);
1039 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1042 spin_unlock(&priv->lock);
1043 spin_unlock_irq(&priv->tx_lock);
1052 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1053 struct ipoib_neigh *neigh)
1055 struct ipoib_dev_priv *priv = netdev_priv(dev);
1056 struct ipoib_cm_tx *tx;
1058 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1066 list_add(&tx->list, &priv->cm.start_list);
1067 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1068 queue_work(ipoib_workqueue, &priv->cm.start_task);
1072 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1074 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1075 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1076 list_move(&tx->list, &priv->cm.reap_list);
1077 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1078 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1079 IPOIB_GID_ARG(tx->neigh->dgid));
1084 static void ipoib_cm_tx_start(struct work_struct *work)
1086 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1088 struct net_device *dev = priv->dev;
1089 struct ipoib_neigh *neigh;
1090 struct ipoib_cm_tx *p;
1091 unsigned long flags;
1094 struct ib_sa_path_rec pathrec;
1097 spin_lock_irqsave(&priv->tx_lock, flags);
1098 spin_lock(&priv->lock);
1099 while (!list_empty(&priv->cm.start_list)) {
1100 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1101 list_del_init(&p->list);
1103 qpn = IPOIB_QPN(neigh->neighbour->ha);
1104 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1105 spin_unlock(&priv->lock);
1106 spin_unlock_irqrestore(&priv->tx_lock, flags);
1107 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1108 spin_lock_irqsave(&priv->tx_lock, flags);
1109 spin_lock(&priv->lock);
1114 list_del(&neigh->list);
1116 ipoib_put_ah(neigh->ah);
1117 ipoib_neigh_free(dev, neigh);
1123 spin_unlock(&priv->lock);
1124 spin_unlock_irqrestore(&priv->tx_lock, flags);
1127 static void ipoib_cm_tx_reap(struct work_struct *work)
1129 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1131 struct ipoib_cm_tx *p;
1133 spin_lock_irq(&priv->tx_lock);
1134 spin_lock(&priv->lock);
1135 while (!list_empty(&priv->cm.reap_list)) {
1136 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1138 spin_unlock(&priv->lock);
1139 spin_unlock_irq(&priv->tx_lock);
1140 ipoib_cm_tx_destroy(p);
1141 spin_lock_irq(&priv->tx_lock);
1142 spin_lock(&priv->lock);
1144 spin_unlock(&priv->lock);
1145 spin_unlock_irq(&priv->tx_lock);
1148 static void ipoib_cm_skb_reap(struct work_struct *work)
1150 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1152 struct sk_buff *skb;
1154 unsigned mtu = priv->mcast_mtu;
1156 spin_lock_irq(&priv->tx_lock);
1157 spin_lock(&priv->lock);
1158 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1159 spin_unlock(&priv->lock);
1160 spin_unlock_irq(&priv->tx_lock);
1161 if (skb->protocol == htons(ETH_P_IP))
1162 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1163 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1164 else if (skb->protocol == htons(ETH_P_IPV6))
1165 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
1167 dev_kfree_skb_any(skb);
1168 spin_lock_irq(&priv->tx_lock);
1169 spin_lock(&priv->lock);
1171 spin_unlock(&priv->lock);
1172 spin_unlock_irq(&priv->tx_lock);
1175 void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb,
1178 struct ipoib_dev_priv *priv = netdev_priv(dev);
1179 int e = skb_queue_empty(&priv->cm.skb_queue);
1182 skb->dst->ops->update_pmtu(skb->dst, mtu);
1184 skb_queue_tail(&priv->cm.skb_queue, skb);
1186 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1189 static void ipoib_cm_rx_reap(struct work_struct *work)
1191 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1193 struct ipoib_cm_rx *p, *n;
1196 spin_lock_irq(&priv->lock);
1197 list_splice_init(&priv->cm.rx_reap_list, &list);
1198 spin_unlock_irq(&priv->lock);
1200 list_for_each_entry_safe(p, n, &list, list) {
1201 ib_destroy_cm_id(p->id);
1202 ib_destroy_qp(p->qp);
1207 static void ipoib_cm_stale_task(struct work_struct *work)
1209 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1210 cm.stale_task.work);
1211 struct ipoib_cm_rx *p;
1214 spin_lock_irq(&priv->lock);
1215 while (!list_empty(&priv->cm.passive_ids)) {
1216 /* List is sorted by LRU, start from tail,
1217 * stop when we see a recently used entry */
1218 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1219 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1221 list_move(&p->list, &priv->cm.rx_error_list);
1222 p->state = IPOIB_CM_RX_ERROR;
1223 spin_unlock_irq(&priv->lock);
1224 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1226 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1227 spin_lock_irq(&priv->lock);
1230 if (!list_empty(&priv->cm.passive_ids))
1231 queue_delayed_work(ipoib_workqueue,
1232 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1233 spin_unlock_irq(&priv->lock);
1237 static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1240 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1242 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1243 return sprintf(buf, "connected\n");
1245 return sprintf(buf, "datagram\n");
1248 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1249 const char *buf, size_t count)
1251 struct net_device *dev = to_net_dev(d);
1252 struct ipoib_dev_priv *priv = netdev_priv(dev);
1254 /* flush paths if we switch modes so that connections are restarted */
1255 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1256 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1257 ipoib_warn(priv, "enabling connected mode "
1258 "will cause multicast packet drops\n");
1259 ipoib_flush_paths(dev);
1263 if (!strcmp(buf, "datagram\n")) {
1264 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1265 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1266 ipoib_flush_paths(dev);
1273 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1275 int ipoib_cm_add_mode_attr(struct net_device *dev)
1277 return device_create_file(&dev->dev, &dev_attr_mode);
1280 int ipoib_cm_dev_init(struct net_device *dev)
1282 struct ipoib_dev_priv *priv = netdev_priv(dev);
1283 struct ib_srq_init_attr srq_init_attr = {
1285 .max_wr = ipoib_recvq_size,
1286 .max_sge = IPOIB_CM_RX_SG
1291 INIT_LIST_HEAD(&priv->cm.passive_ids);
1292 INIT_LIST_HEAD(&priv->cm.reap_list);
1293 INIT_LIST_HEAD(&priv->cm.start_list);
1294 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1295 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1296 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1297 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1298 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1299 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1300 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1301 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1302 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1304 skb_queue_head_init(&priv->cm.skb_queue);
1306 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1307 if (IS_ERR(priv->cm.srq)) {
1308 ret = PTR_ERR(priv->cm.srq);
1309 priv->cm.srq = NULL;
1313 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1315 if (!priv->cm.srq_ring) {
1316 printk(KERN_WARNING "%s: failed to allocate CM ring (%d entries)\n",
1317 priv->ca->name, ipoib_recvq_size);
1318 ipoib_cm_dev_cleanup(dev);
1322 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
1323 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1325 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
1326 for (i = 1; i < IPOIB_CM_RX_SG; ++i)
1327 priv->cm.rx_sge[i].length = PAGE_SIZE;
1328 priv->cm.rx_wr.next = NULL;
1329 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
1330 priv->cm.rx_wr.num_sge = IPOIB_CM_RX_SG;
1332 for (i = 0; i < ipoib_recvq_size; ++i) {
1333 if (!ipoib_cm_alloc_rx_skb(dev, i, IPOIB_CM_RX_SG - 1,
1334 priv->cm.srq_ring[i].mapping)) {
1335 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
1336 ipoib_cm_dev_cleanup(dev);
1339 if (ipoib_cm_post_receive(dev, i)) {
1340 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
1341 ipoib_cm_dev_cleanup(dev);
1346 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1350 void ipoib_cm_dev_cleanup(struct net_device *dev)
1352 struct ipoib_dev_priv *priv = netdev_priv(dev);
1358 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1360 ret = ib_destroy_srq(priv->cm.srq);
1362 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1364 priv->cm.srq = NULL;
1365 if (!priv->cm.srq_ring)
1367 for (i = 0; i < ipoib_recvq_size; ++i)
1368 if (priv->cm.srq_ring[i].skb) {
1369 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
1370 priv->cm.srq_ring[i].mapping);
1371 dev_kfree_skb_any(priv->cm.srq_ring[i].skb);
1372 priv->cm.srq_ring[i].skb = NULL;
1374 kfree(priv->cm.srq_ring);
1375 priv->cm.srq_ring = NULL;