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>
41 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
42 static int data_debug_level;
44 module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
45 MODULE_PARM_DESC(cm_data_debug_level,
46 "Enable data path debug tracing for connected mode if > 0");
51 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
53 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
54 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
55 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
56 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
65 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
66 struct ib_cm_event *event);
68 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
69 u64 mapping[IPOIB_CM_RX_SG])
73 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
75 for (i = 0; i < frags; ++i)
76 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
79 static int ipoib_cm_post_receive(struct net_device *dev, int id)
81 struct ipoib_dev_priv *priv = netdev_priv(dev);
82 struct ib_recv_wr *bad_wr;
85 priv->cm.rx_wr.wr_id = id | IPOIB_CM_OP_SRQ;
87 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
88 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
90 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
92 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
93 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
94 priv->cm.srq_ring[id].mapping);
95 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
96 priv->cm.srq_ring[id].skb = NULL;
102 static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, int id, int frags,
103 u64 mapping[IPOIB_CM_RX_SG])
105 struct ipoib_dev_priv *priv = netdev_priv(dev);
109 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
114 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
115 * IP header to a multiple of 16.
117 skb_reserve(skb, 12);
119 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
121 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
122 dev_kfree_skb_any(skb);
126 for (i = 0; i < frags; i++) {
127 struct page *page = alloc_page(GFP_ATOMIC);
131 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
133 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
134 0, PAGE_SIZE, DMA_FROM_DEVICE);
135 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
139 priv->cm.srq_ring[id].skb = skb;
144 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
147 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
149 dev_kfree_skb_any(skb);
153 static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
154 struct ipoib_cm_rx *p)
156 struct ipoib_dev_priv *priv = netdev_priv(dev);
157 struct ib_qp_init_attr attr = {
158 .send_cq = priv->cq, /* does not matter, we never send anything */
161 .cap.max_send_wr = 1, /* FIXME: 0 Seems not to work */
162 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
163 .sq_sig_type = IB_SIGNAL_ALL_WR,
164 .qp_type = IB_QPT_RC,
167 return ib_create_qp(priv->pd, &attr);
170 static int ipoib_cm_modify_rx_qp(struct net_device *dev,
171 struct ib_cm_id *cm_id, struct ib_qp *qp,
174 struct ipoib_dev_priv *priv = netdev_priv(dev);
175 struct ib_qp_attr qp_attr;
176 int qp_attr_mask, ret;
178 qp_attr.qp_state = IB_QPS_INIT;
179 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
181 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
184 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
186 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
189 qp_attr.qp_state = IB_QPS_RTR;
190 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
192 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
195 qp_attr.rq_psn = psn;
196 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
198 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
204 static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
205 struct ib_qp *qp, struct ib_cm_req_event_param *req,
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
209 struct ipoib_cm_data data = {};
210 struct ib_cm_rep_param rep = {};
212 data.qpn = cpu_to_be32(priv->qp->qp_num);
213 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
215 rep.private_data = &data;
216 rep.private_data_len = sizeof data;
217 rep.flow_control = 0;
218 rep.rnr_retry_count = req->rnr_retry_count;
219 rep.target_ack_delay = 20; /* FIXME */
221 rep.qp_num = qp->qp_num;
222 rep.starting_psn = psn;
223 return ib_send_cm_rep(cm_id, &rep);
226 static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
228 struct net_device *dev = cm_id->context;
229 struct ipoib_dev_priv *priv = netdev_priv(dev);
230 struct ipoib_cm_rx *p;
234 ipoib_dbg(priv, "REQ arrived\n");
235 p = kzalloc(sizeof *p, GFP_KERNEL);
240 p->qp = ipoib_cm_create_rx_qp(dev, p);
242 ret = PTR_ERR(p->qp);
246 psn = random32() & 0xffffff;
247 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
251 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
253 ipoib_warn(priv, "failed to send REP: %d\n", ret);
258 p->jiffies = jiffies;
259 spin_lock_irq(&priv->lock);
260 list_add(&p->list, &priv->cm.passive_ids);
261 spin_unlock_irq(&priv->lock);
262 queue_delayed_work(ipoib_workqueue,
263 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
268 ib_destroy_qp(p->qp);
274 static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
275 struct ib_cm_event *event)
277 struct ipoib_cm_rx *p;
278 struct ipoib_dev_priv *priv;
281 switch (event->event) {
282 case IB_CM_REQ_RECEIVED:
283 return ipoib_cm_req_handler(cm_id, event);
284 case IB_CM_DREQ_RECEIVED:
286 ib_send_cm_drep(cm_id, NULL, 0);
288 case IB_CM_REJ_RECEIVED:
290 priv = netdev_priv(p->dev);
291 spin_lock_irq(&priv->lock);
292 if (list_empty(&p->list))
293 ret = 0; /* Connection is going away already. */
295 list_del_init(&p->list);
298 spin_unlock_irq(&priv->lock);
300 ib_destroy_qp(p->qp);
309 /* Adjust length of skb with fragments to match received data */
310 static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
311 unsigned int length, struct sk_buff *toskb)
316 /* put header into skb */
317 size = min(length, hdr_space);
322 num_frags = skb_shinfo(skb)->nr_frags;
323 for (i = 0; i < num_frags; i++) {
324 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
327 /* don't need this page */
328 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
329 --skb_shinfo(skb)->nr_frags;
331 size = min(length, (unsigned) PAGE_SIZE);
334 skb->data_len += size;
335 skb->truesize += size;
342 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
344 struct ipoib_dev_priv *priv = netdev_priv(dev);
345 unsigned int wr_id = wc->wr_id & ~IPOIB_CM_OP_SRQ;
346 struct sk_buff *skb, *newskb;
347 struct ipoib_cm_rx *p;
349 u64 mapping[IPOIB_CM_RX_SG];
352 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
355 if (unlikely(wr_id >= ipoib_recvq_size)) {
356 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
357 wr_id, ipoib_recvq_size);
361 skb = priv->cm.srq_ring[wr_id].skb;
363 if (unlikely(wc->status != IB_WC_SUCCESS)) {
364 ipoib_dbg(priv, "cm recv error "
365 "(status=%d, wrid=%d vend_err %x)\n",
366 wc->status, wr_id, wc->vendor_err);
367 ++priv->stats.rx_dropped;
371 if (!likely(wr_id & IPOIB_CM_RX_UPDATE_MASK)) {
372 p = wc->qp->qp_context;
373 if (time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
374 spin_lock_irqsave(&priv->lock, flags);
375 p->jiffies = jiffies;
376 /* Move this entry to list head, but do
377 * not re-add it if it has been removed. */
378 if (!list_empty(&p->list))
379 list_move(&p->list, &priv->cm.passive_ids);
380 spin_unlock_irqrestore(&priv->lock, flags);
381 queue_delayed_work(ipoib_workqueue,
382 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
386 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
387 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
389 newskb = ipoib_cm_alloc_rx_skb(dev, wr_id, frags, mapping);
390 if (unlikely(!newskb)) {
392 * If we can't allocate a new RX buffer, dump
393 * this packet and reuse the old buffer.
395 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
396 ++priv->stats.rx_dropped;
400 ipoib_cm_dma_unmap_rx(priv, frags, priv->cm.srq_ring[wr_id].mapping);
401 memcpy(priv->cm.srq_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
403 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
404 wc->byte_len, wc->slid);
406 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
408 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
409 skb_reset_mac_header(skb);
410 skb_pull(skb, IPOIB_ENCAP_LEN);
412 dev->last_rx = jiffies;
413 ++priv->stats.rx_packets;
414 priv->stats.rx_bytes += skb->len;
417 /* XXX get correct PACKET_ type here */
418 skb->pkt_type = PACKET_HOST;
422 if (unlikely(ipoib_cm_post_receive(dev, wr_id)))
423 ipoib_warn(priv, "ipoib_cm_post_receive failed "
424 "for buf %d\n", wr_id);
427 static inline int post_send(struct ipoib_dev_priv *priv,
428 struct ipoib_cm_tx *tx,
432 struct ib_send_wr *bad_wr;
434 priv->tx_sge.addr = addr;
435 priv->tx_sge.length = len;
437 priv->tx_wr.wr_id = wr_id;
439 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
442 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
444 struct ipoib_dev_priv *priv = netdev_priv(dev);
445 struct ipoib_tx_buf *tx_req;
448 if (unlikely(skb->len > tx->mtu)) {
449 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
451 ++priv->stats.tx_dropped;
452 ++priv->stats.tx_errors;
453 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
457 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
458 tx->tx_head, skb->len, tx->qp->qp_num);
461 * We put the skb into the tx_ring _before_ we call post_send()
462 * because it's entirely possible that the completion handler will
463 * run before we execute anything after the post_send(). That
464 * means we have to make sure everything is properly recorded and
465 * our state is consistent before we call post_send().
467 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
469 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
470 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
471 ++priv->stats.tx_errors;
472 dev_kfree_skb_any(skb);
476 tx_req->mapping = addr;
478 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
480 ipoib_warn(priv, "post_send failed\n");
481 ++priv->stats.tx_errors;
482 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
483 dev_kfree_skb_any(skb);
485 dev->trans_start = jiffies;
488 if (tx->tx_head - tx->tx_tail == ipoib_sendq_size) {
489 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
491 netif_stop_queue(dev);
492 set_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
497 static void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ipoib_cm_tx *tx,
500 struct ipoib_dev_priv *priv = netdev_priv(dev);
501 unsigned int wr_id = wc->wr_id;
502 struct ipoib_tx_buf *tx_req;
505 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
508 if (unlikely(wr_id >= ipoib_sendq_size)) {
509 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
510 wr_id, ipoib_sendq_size);
514 tx_req = &tx->tx_ring[wr_id];
516 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
518 /* FIXME: is this right? Shouldn't we only increment on success? */
519 ++priv->stats.tx_packets;
520 priv->stats.tx_bytes += tx_req->skb->len;
522 dev_kfree_skb_any(tx_req->skb);
524 spin_lock_irqsave(&priv->tx_lock, flags);
526 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags)) &&
527 tx->tx_head - tx->tx_tail <= ipoib_sendq_size >> 1) {
528 clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags);
529 netif_wake_queue(dev);
532 if (wc->status != IB_WC_SUCCESS &&
533 wc->status != IB_WC_WR_FLUSH_ERR) {
534 struct ipoib_neigh *neigh;
536 ipoib_dbg(priv, "failed cm send event "
537 "(status=%d, wrid=%d vend_err %x)\n",
538 wc->status, wr_id, wc->vendor_err);
540 spin_lock(&priv->lock);
545 list_del(&neigh->list);
547 ipoib_put_ah(neigh->ah);
548 ipoib_neigh_free(dev, neigh);
553 /* queue would be re-started anyway when TX is destroyed,
554 * but it makes sense to do it ASAP here. */
555 if (test_and_clear_bit(IPOIB_FLAG_NETIF_STOPPED, &tx->flags))
556 netif_wake_queue(dev);
558 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
559 list_move(&tx->list, &priv->cm.reap_list);
560 queue_work(ipoib_workqueue, &priv->cm.reap_task);
563 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
565 spin_unlock(&priv->lock);
568 spin_unlock_irqrestore(&priv->tx_lock, flags);
571 static void ipoib_cm_tx_completion(struct ib_cq *cq, void *tx_ptr)
573 struct ipoib_cm_tx *tx = tx_ptr;
576 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
578 n = ib_poll_cq(cq, IPOIB_NUM_WC, tx->ibwc);
579 for (i = 0; i < n; ++i)
580 ipoib_cm_handle_tx_wc(tx->dev, tx, tx->ibwc + i);
581 } while (n == IPOIB_NUM_WC);
584 int ipoib_cm_dev_open(struct net_device *dev)
586 struct ipoib_dev_priv *priv = netdev_priv(dev);
589 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
592 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
593 if (IS_ERR(priv->cm.id)) {
594 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
595 return IS_ERR(priv->cm.id);
598 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
601 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
602 IPOIB_CM_IETF_ID | priv->qp->qp_num);
603 ib_destroy_cm_id(priv->cm.id);
609 void ipoib_cm_dev_stop(struct net_device *dev)
611 struct ipoib_dev_priv *priv = netdev_priv(dev);
612 struct ipoib_cm_rx *p;
614 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
617 ib_destroy_cm_id(priv->cm.id);
618 spin_lock_irq(&priv->lock);
619 while (!list_empty(&priv->cm.passive_ids)) {
620 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
621 list_del_init(&p->list);
622 spin_unlock_irq(&priv->lock);
623 ib_destroy_cm_id(p->id);
624 ib_destroy_qp(p->qp);
626 spin_lock_irq(&priv->lock);
628 spin_unlock_irq(&priv->lock);
630 cancel_delayed_work(&priv->cm.stale_task);
633 static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
635 struct ipoib_cm_tx *p = cm_id->context;
636 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
637 struct ipoib_cm_data *data = event->private_data;
638 struct sk_buff_head skqueue;
639 struct ib_qp_attr qp_attr;
640 int qp_attr_mask, ret;
643 p->mtu = be32_to_cpu(data->mtu);
645 if (p->mtu < priv->dev->mtu + IPOIB_ENCAP_LEN) {
646 ipoib_warn(priv, "Rejecting connection: mtu %d < device mtu %d + 4\n",
647 p->mtu, priv->dev->mtu);
651 qp_attr.qp_state = IB_QPS_RTR;
652 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
654 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
658 qp_attr.rq_psn = 0 /* FIXME */;
659 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
661 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
665 qp_attr.qp_state = IB_QPS_RTS;
666 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
668 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
671 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
673 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
677 skb_queue_head_init(&skqueue);
679 spin_lock_irq(&priv->lock);
680 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
682 while ((skb = __skb_dequeue(&p->neigh->queue)))
683 __skb_queue_tail(&skqueue, skb);
684 spin_unlock_irq(&priv->lock);
686 while ((skb = __skb_dequeue(&skqueue))) {
688 if (dev_queue_xmit(skb))
689 ipoib_warn(priv, "dev_queue_xmit failed "
690 "to requeue packet\n");
693 ret = ib_send_cm_rtu(cm_id, NULL, 0);
695 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
701 static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ib_cq *cq)
703 struct ipoib_dev_priv *priv = netdev_priv(dev);
704 struct ib_qp_init_attr attr = {};
705 attr.recv_cq = priv->cq;
706 attr.srq = priv->cm.srq;
707 attr.cap.max_send_wr = ipoib_sendq_size;
708 attr.cap.max_send_sge = 1;
709 attr.sq_sig_type = IB_SIGNAL_ALL_WR;
710 attr.qp_type = IB_QPT_RC;
712 return ib_create_qp(priv->pd, &attr);
715 static int ipoib_cm_send_req(struct net_device *dev,
716 struct ib_cm_id *id, struct ib_qp *qp,
718 struct ib_sa_path_rec *pathrec)
720 struct ipoib_dev_priv *priv = netdev_priv(dev);
721 struct ipoib_cm_data data = {};
722 struct ib_cm_req_param req = {};
724 data.qpn = cpu_to_be32(priv->qp->qp_num);
725 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
727 req.primary_path = pathrec;
728 req.alternate_path = NULL;
729 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
730 req.qp_num = qp->qp_num;
731 req.qp_type = qp->qp_type;
732 req.private_data = &data;
733 req.private_data_len = sizeof data;
734 req.flow_control = 0;
736 req.starting_psn = 0; /* FIXME */
739 * Pick some arbitrary defaults here; we could make these
740 * module parameters if anyone cared about setting them.
742 req.responder_resources = 4;
743 req.remote_cm_response_timeout = 20;
744 req.local_cm_response_timeout = 20;
745 req.retry_count = 0; /* RFC draft warns against retries */
746 req.rnr_retry_count = 0; /* RFC draft warns against retries */
747 req.max_cm_retries = 15;
749 return ib_send_cm_req(id, &req);
752 static int ipoib_cm_modify_tx_init(struct net_device *dev,
753 struct ib_cm_id *cm_id, struct ib_qp *qp)
755 struct ipoib_dev_priv *priv = netdev_priv(dev);
756 struct ib_qp_attr qp_attr;
757 int qp_attr_mask, ret;
758 ret = ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
760 ipoib_warn(priv, "pkey 0x%x not in cache: %d\n", priv->pkey, ret);
764 qp_attr.qp_state = IB_QPS_INIT;
765 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
766 qp_attr.port_num = priv->port;
767 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
769 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
771 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
777 static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
778 struct ib_sa_path_rec *pathrec)
780 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
783 p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring,
786 ipoib_warn(priv, "failed to allocate tx ring\n");
791 p->cq = ib_create_cq(priv->ca, ipoib_cm_tx_completion, NULL, p,
792 ipoib_sendq_size + 1);
794 ret = PTR_ERR(p->cq);
795 ipoib_warn(priv, "failed to allocate tx cq: %d\n", ret);
799 ret = ib_req_notify_cq(p->cq, IB_CQ_NEXT_COMP);
801 ipoib_warn(priv, "failed to request completion notification: %d\n", ret);
805 p->qp = ipoib_cm_create_tx_qp(p->dev, p->cq);
807 ret = PTR_ERR(p->qp);
808 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
812 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
814 ret = PTR_ERR(p->id);
815 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
819 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
821 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
825 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
827 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
831 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
832 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
838 ib_destroy_cm_id(p->id);
841 ib_destroy_qp(p->qp);
845 ib_destroy_cq(p->cq);
852 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
854 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
855 struct ipoib_tx_buf *tx_req;
857 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
858 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
861 ib_destroy_cm_id(p->id);
864 ib_destroy_qp(p->qp);
867 ib_destroy_cq(p->cq);
869 if (test_bit(IPOIB_FLAG_NETIF_STOPPED, &p->flags))
870 netif_wake_queue(p->dev);
873 while ((int) p->tx_tail - (int) p->tx_head < 0) {
874 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
875 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
877 dev_kfree_skb_any(tx_req->skb);
887 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
888 struct ib_cm_event *event)
890 struct ipoib_cm_tx *tx = cm_id->context;
891 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
892 struct net_device *dev = priv->dev;
893 struct ipoib_neigh *neigh;
896 switch (event->event) {
897 case IB_CM_DREQ_RECEIVED:
898 ipoib_dbg(priv, "DREQ received.\n");
899 ib_send_cm_drep(cm_id, NULL, 0);
901 case IB_CM_REP_RECEIVED:
902 ipoib_dbg(priv, "REP received.\n");
903 ret = ipoib_cm_rep_handler(cm_id, event);
905 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
908 case IB_CM_REQ_ERROR:
909 case IB_CM_REJ_RECEIVED:
910 case IB_CM_TIMEWAIT_EXIT:
911 ipoib_dbg(priv, "CM error %d.\n", event->event);
912 spin_lock_irq(&priv->tx_lock);
913 spin_lock(&priv->lock);
918 list_del(&neigh->list);
920 ipoib_put_ah(neigh->ah);
921 ipoib_neigh_free(dev, neigh);
926 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
927 list_move(&tx->list, &priv->cm.reap_list);
928 queue_work(ipoib_workqueue, &priv->cm.reap_task);
931 spin_unlock(&priv->lock);
932 spin_unlock_irq(&priv->tx_lock);
941 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
942 struct ipoib_neigh *neigh)
944 struct ipoib_dev_priv *priv = netdev_priv(dev);
945 struct ipoib_cm_tx *tx;
947 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
955 list_add(&tx->list, &priv->cm.start_list);
956 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
957 queue_work(ipoib_workqueue, &priv->cm.start_task);
961 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
963 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
964 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
965 list_move(&tx->list, &priv->cm.reap_list);
966 queue_work(ipoib_workqueue, &priv->cm.reap_task);
967 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
968 IPOIB_GID_ARG(tx->neigh->dgid));
973 static void ipoib_cm_tx_start(struct work_struct *work)
975 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
977 struct net_device *dev = priv->dev;
978 struct ipoib_neigh *neigh;
979 struct ipoib_cm_tx *p;
983 struct ib_sa_path_rec pathrec;
986 spin_lock_irqsave(&priv->tx_lock, flags);
987 spin_lock(&priv->lock);
988 while (!list_empty(&priv->cm.start_list)) {
989 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
990 list_del_init(&p->list);
992 qpn = IPOIB_QPN(neigh->neighbour->ha);
993 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
994 spin_unlock(&priv->lock);
995 spin_unlock_irqrestore(&priv->tx_lock, flags);
996 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
997 spin_lock_irqsave(&priv->tx_lock, flags);
998 spin_lock(&priv->lock);
1003 list_del(&neigh->list);
1005 ipoib_put_ah(neigh->ah);
1006 ipoib_neigh_free(dev, neigh);
1012 spin_unlock(&priv->lock);
1013 spin_unlock_irqrestore(&priv->tx_lock, flags);
1016 static void ipoib_cm_tx_reap(struct work_struct *work)
1018 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1020 struct ipoib_cm_tx *p;
1022 spin_lock_irq(&priv->tx_lock);
1023 spin_lock(&priv->lock);
1024 while (!list_empty(&priv->cm.reap_list)) {
1025 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1027 spin_unlock(&priv->lock);
1028 spin_unlock_irq(&priv->tx_lock);
1029 ipoib_cm_tx_destroy(p);
1030 spin_lock_irq(&priv->tx_lock);
1031 spin_lock(&priv->lock);
1033 spin_unlock(&priv->lock);
1034 spin_unlock_irq(&priv->tx_lock);
1037 static void ipoib_cm_skb_reap(struct work_struct *work)
1039 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1041 struct net_device *dev = priv->dev;
1042 struct sk_buff *skb;
1044 unsigned mtu = priv->mcast_mtu;
1046 spin_lock_irq(&priv->tx_lock);
1047 spin_lock(&priv->lock);
1048 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1049 spin_unlock(&priv->lock);
1050 spin_unlock_irq(&priv->tx_lock);
1051 if (skb->protocol == htons(ETH_P_IP))
1052 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1053 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1054 else if (skb->protocol == htons(ETH_P_IPV6))
1055 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1057 dev_kfree_skb_any(skb);
1058 spin_lock_irq(&priv->tx_lock);
1059 spin_lock(&priv->lock);
1061 spin_unlock(&priv->lock);
1062 spin_unlock_irq(&priv->tx_lock);
1065 void ipoib_cm_skb_too_long(struct net_device* dev, struct sk_buff *skb,
1068 struct ipoib_dev_priv *priv = netdev_priv(dev);
1069 int e = skb_queue_empty(&priv->cm.skb_queue);
1072 skb->dst->ops->update_pmtu(skb->dst, mtu);
1074 skb_queue_tail(&priv->cm.skb_queue, skb);
1076 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1079 static void ipoib_cm_stale_task(struct work_struct *work)
1081 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1082 cm.stale_task.work);
1083 struct ipoib_cm_rx *p;
1085 spin_lock_irq(&priv->lock);
1086 while (!list_empty(&priv->cm.passive_ids)) {
1087 /* List if sorted by LRU, start from tail,
1088 * stop when we see a recently used entry */
1089 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1090 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1092 list_del_init(&p->list);
1093 spin_unlock_irq(&priv->lock);
1094 ib_destroy_cm_id(p->id);
1095 ib_destroy_qp(p->qp);
1097 spin_lock_irq(&priv->lock);
1099 spin_unlock_irq(&priv->lock);
1103 static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1106 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1108 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1109 return sprintf(buf, "connected\n");
1111 return sprintf(buf, "datagram\n");
1114 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1115 const char *buf, size_t count)
1117 struct net_device *dev = to_net_dev(d);
1118 struct ipoib_dev_priv *priv = netdev_priv(dev);
1120 /* flush paths if we switch modes so that connections are restarted */
1121 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1122 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1123 ipoib_warn(priv, "enabling connected mode "
1124 "will cause multicast packet drops\n");
1125 ipoib_flush_paths(dev);
1129 if (!strcmp(buf, "datagram\n")) {
1130 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1131 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1132 ipoib_flush_paths(dev);
1139 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1141 int ipoib_cm_add_mode_attr(struct net_device *dev)
1143 return device_create_file(&dev->dev, &dev_attr_mode);
1146 int ipoib_cm_dev_init(struct net_device *dev)
1148 struct ipoib_dev_priv *priv = netdev_priv(dev);
1149 struct ib_srq_init_attr srq_init_attr = {
1151 .max_wr = ipoib_recvq_size,
1152 .max_sge = IPOIB_CM_RX_SG
1157 INIT_LIST_HEAD(&priv->cm.passive_ids);
1158 INIT_LIST_HEAD(&priv->cm.reap_list);
1159 INIT_LIST_HEAD(&priv->cm.start_list);
1160 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1161 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1162 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1163 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1165 skb_queue_head_init(&priv->cm.skb_queue);
1167 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1168 if (IS_ERR(priv->cm.srq)) {
1169 ret = PTR_ERR(priv->cm.srq);
1170 priv->cm.srq = NULL;
1174 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1176 if (!priv->cm.srq_ring) {
1177 printk(KERN_WARNING "%s: failed to allocate CM ring (%d entries)\n",
1178 priv->ca->name, ipoib_recvq_size);
1179 ipoib_cm_dev_cleanup(dev);
1183 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
1184 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1186 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
1187 for (i = 1; i < IPOIB_CM_RX_SG; ++i)
1188 priv->cm.rx_sge[i].length = PAGE_SIZE;
1189 priv->cm.rx_wr.next = NULL;
1190 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
1191 priv->cm.rx_wr.num_sge = IPOIB_CM_RX_SG;
1193 for (i = 0; i < ipoib_recvq_size; ++i) {
1194 if (!ipoib_cm_alloc_rx_skb(dev, i, IPOIB_CM_RX_SG - 1,
1195 priv->cm.srq_ring[i].mapping)) {
1196 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
1197 ipoib_cm_dev_cleanup(dev);
1200 if (ipoib_cm_post_receive(dev, i)) {
1201 ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
1202 ipoib_cm_dev_cleanup(dev);
1207 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1211 void ipoib_cm_dev_cleanup(struct net_device *dev)
1213 struct ipoib_dev_priv *priv = netdev_priv(dev);
1219 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1221 ret = ib_destroy_srq(priv->cm.srq);
1223 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1225 priv->cm.srq = NULL;
1226 if (!priv->cm.srq_ring)
1228 for (i = 0; i < ipoib_recvq_size; ++i)
1229 if (priv->cm.srq_ring[i].skb) {
1230 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
1231 priv->cm.srq_ring[i].mapping);
1232 dev_kfree_skb_any(priv->cm.srq_ring[i].skb);
1233 priv->cm.srq_ring[i].skb = NULL;
1235 kfree(priv->cm.srq_ring);
1236 priv->cm.srq_ring = NULL;