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
;
235 ipoib_dbg(priv
, "REQ arrived\n");
236 p
= kzalloc(sizeof *p
, GFP_KERNEL
);
241 p
->qp
= ipoib_cm_create_rx_qp(dev
, p
);
243 ret
= PTR_ERR(p
->qp
);
247 psn
= random32() & 0xffffff;
248 ret
= ipoib_cm_modify_rx_qp(dev
, cm_id
, p
->qp
, psn
);
252 ret
= ipoib_cm_send_rep(dev
, cm_id
, p
->qp
, &event
->param
.req_rcvd
, psn
);
254 ipoib_warn(priv
, "failed to send REP: %d\n", ret
);
259 p
->jiffies
= jiffies
;
260 spin_lock_irqsave(&priv
->lock
, flags
);
261 list_add(&p
->list
, &priv
->cm
.passive_ids
);
262 spin_unlock_irqrestore(&priv
->lock
, flags
);
263 queue_delayed_work(ipoib_workqueue
,
264 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
269 ib_destroy_qp(p
->qp
);
275 static int ipoib_cm_rx_handler(struct ib_cm_id
*cm_id
,
276 struct ib_cm_event
*event
)
278 struct ipoib_cm_rx
*p
;
279 struct ipoib_dev_priv
*priv
;
283 switch (event
->event
) {
284 case IB_CM_REQ_RECEIVED
:
285 return ipoib_cm_req_handler(cm_id
, event
);
286 case IB_CM_DREQ_RECEIVED
:
288 ib_send_cm_drep(cm_id
, NULL
, 0);
290 case IB_CM_REJ_RECEIVED
:
292 priv
= netdev_priv(p
->dev
);
293 spin_lock_irqsave(&priv
->lock
, flags
);
294 if (list_empty(&p
->list
))
295 ret
= 0; /* Connection is going away already. */
297 list_del_init(&p
->list
);
300 spin_unlock_irqrestore(&priv
->lock
, flags
);
302 ib_destroy_qp(p
->qp
);
311 /* Adjust length of skb with fragments to match received data */
312 static void skb_put_frags(struct sk_buff
*skb
, unsigned int hdr_space
,
313 unsigned int length
, struct sk_buff
*toskb
)
318 /* put header into skb */
319 size
= min(length
, hdr_space
);
324 num_frags
= skb_shinfo(skb
)->nr_frags
;
325 for (i
= 0; i
< num_frags
; i
++) {
326 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
329 /* don't need this page */
330 skb_fill_page_desc(toskb
, i
, frag
->page
, 0, PAGE_SIZE
);
331 --skb_shinfo(skb
)->nr_frags
;
333 size
= min(length
, (unsigned) PAGE_SIZE
);
336 skb
->data_len
+= size
;
337 skb
->truesize
+= size
;
344 void ipoib_cm_handle_rx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
346 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
347 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_CM_OP_SRQ
;
348 struct sk_buff
*skb
, *newskb
;
349 struct ipoib_cm_rx
*p
;
351 u64 mapping
[IPOIB_CM_RX_SG
];
354 ipoib_dbg_data(priv
, "cm recv completion: id %d, op %d, status: %d\n",
355 wr_id
, wc
->opcode
, wc
->status
);
357 if (unlikely(wr_id
>= ipoib_recvq_size
)) {
358 ipoib_warn(priv
, "cm recv completion event with wrid %d (> %d)\n",
359 wr_id
, ipoib_recvq_size
);
363 skb
= priv
->cm
.srq_ring
[wr_id
].skb
;
365 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
366 ipoib_dbg(priv
, "cm recv error "
367 "(status=%d, wrid=%d vend_err %x)\n",
368 wc
->status
, wr_id
, wc
->vendor_err
);
369 ++priv
->stats
.rx_dropped
;
373 if (!likely(wr_id
& IPOIB_CM_RX_UPDATE_MASK
)) {
374 p
= wc
->qp
->qp_context
;
375 if (time_after_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_UPDATE_TIME
)) {
376 spin_lock_irqsave(&priv
->lock
, flags
);
377 p
->jiffies
= jiffies
;
378 /* Move this entry to list head, but do
379 * not re-add it if it has been removed. */
380 if (!list_empty(&p
->list
))
381 list_move(&p
->list
, &priv
->cm
.passive_ids
);
382 spin_unlock_irqrestore(&priv
->lock
, flags
);
383 queue_delayed_work(ipoib_workqueue
,
384 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
388 frags
= PAGE_ALIGN(wc
->byte_len
- min(wc
->byte_len
,
389 (unsigned)IPOIB_CM_HEAD_SIZE
)) / PAGE_SIZE
;
391 newskb
= ipoib_cm_alloc_rx_skb(dev
, wr_id
, frags
, mapping
);
392 if (unlikely(!newskb
)) {
394 * If we can't allocate a new RX buffer, dump
395 * this packet and reuse the old buffer.
397 ipoib_dbg(priv
, "failed to allocate receive buffer %d\n", wr_id
);
398 ++priv
->stats
.rx_dropped
;
402 ipoib_cm_dma_unmap_rx(priv
, frags
, priv
->cm
.srq_ring
[wr_id
].mapping
);
403 memcpy(priv
->cm
.srq_ring
[wr_id
].mapping
, mapping
, (frags
+ 1) * sizeof *mapping
);
405 ipoib_dbg_data(priv
, "received %d bytes, SLID 0x%04x\n",
406 wc
->byte_len
, wc
->slid
);
408 skb_put_frags(skb
, IPOIB_CM_HEAD_SIZE
, wc
->byte_len
, newskb
);
410 skb
->protocol
= ((struct ipoib_header
*) skb
->data
)->proto
;
411 skb
->mac
.raw
= skb
->data
;
412 skb_pull(skb
, IPOIB_ENCAP_LEN
);
414 dev
->last_rx
= jiffies
;
415 ++priv
->stats
.rx_packets
;
416 priv
->stats
.rx_bytes
+= skb
->len
;
419 /* XXX get correct PACKET_ type here */
420 skb
->pkt_type
= PACKET_HOST
;
424 if (unlikely(ipoib_cm_post_receive(dev
, wr_id
)))
425 ipoib_warn(priv
, "ipoib_cm_post_receive failed "
426 "for buf %d\n", wr_id
);
429 static inline int post_send(struct ipoib_dev_priv
*priv
,
430 struct ipoib_cm_tx
*tx
,
434 struct ib_send_wr
*bad_wr
;
436 priv
->tx_sge
.addr
= addr
;
437 priv
->tx_sge
.length
= len
;
439 priv
->tx_wr
.wr_id
= wr_id
;
441 return ib_post_send(tx
->qp
, &priv
->tx_wr
, &bad_wr
);
444 void ipoib_cm_send(struct net_device
*dev
, struct sk_buff
*skb
, struct ipoib_cm_tx
*tx
)
446 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
447 struct ipoib_tx_buf
*tx_req
;
450 if (unlikely(skb
->len
> tx
->mtu
)) {
451 ipoib_warn(priv
, "packet len %d (> %d) too long to send, dropping\n",
453 ++priv
->stats
.tx_dropped
;
454 ++priv
->stats
.tx_errors
;
455 ipoib_cm_skb_too_long(dev
, skb
, tx
->mtu
- IPOIB_ENCAP_LEN
);
459 ipoib_dbg_data(priv
, "sending packet: head 0x%x length %d connection 0x%x\n",
460 tx
->tx_head
, skb
->len
, tx
->qp
->qp_num
);
463 * We put the skb into the tx_ring _before_ we call post_send()
464 * because it's entirely possible that the completion handler will
465 * run before we execute anything after the post_send(). That
466 * means we have to make sure everything is properly recorded and
467 * our state is consistent before we call post_send().
469 tx_req
= &tx
->tx_ring
[tx
->tx_head
& (ipoib_sendq_size
- 1)];
471 addr
= ib_dma_map_single(priv
->ca
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
472 if (unlikely(ib_dma_mapping_error(priv
->ca
, addr
))) {
473 ++priv
->stats
.tx_errors
;
474 dev_kfree_skb_any(skb
);
478 tx_req
->mapping
= addr
;
480 if (unlikely(post_send(priv
, tx
, tx
->tx_head
& (ipoib_sendq_size
- 1),
482 ipoib_warn(priv
, "post_send failed\n");
483 ++priv
->stats
.tx_errors
;
484 ib_dma_unmap_single(priv
->ca
, addr
, skb
->len
, DMA_TO_DEVICE
);
485 dev_kfree_skb_any(skb
);
487 dev
->trans_start
= jiffies
;
490 if (tx
->tx_head
- tx
->tx_tail
== ipoib_sendq_size
) {
491 ipoib_dbg(priv
, "TX ring 0x%x full, stopping kernel net queue\n",
493 netif_stop_queue(dev
);
494 set_bit(IPOIB_FLAG_NETIF_STOPPED
, &tx
->flags
);
499 static void ipoib_cm_handle_tx_wc(struct net_device
*dev
, struct ipoib_cm_tx
*tx
,
502 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
503 unsigned int wr_id
= wc
->wr_id
;
504 struct ipoib_tx_buf
*tx_req
;
507 ipoib_dbg_data(priv
, "cm send completion: id %d, op %d, status: %d\n",
508 wr_id
, wc
->opcode
, wc
->status
);
510 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
511 ipoib_warn(priv
, "cm send completion event with wrid %d (> %d)\n",
512 wr_id
, ipoib_sendq_size
);
516 tx_req
= &tx
->tx_ring
[wr_id
];
518 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
, DMA_TO_DEVICE
);
520 /* FIXME: is this right? Shouldn't we only increment on success? */
521 ++priv
->stats
.tx_packets
;
522 priv
->stats
.tx_bytes
+= tx_req
->skb
->len
;
524 dev_kfree_skb_any(tx_req
->skb
);
526 spin_lock_irqsave(&priv
->tx_lock
, flags
);
528 if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED
, &tx
->flags
)) &&
529 tx
->tx_head
- tx
->tx_tail
<= ipoib_sendq_size
>> 1) {
530 clear_bit(IPOIB_FLAG_NETIF_STOPPED
, &tx
->flags
);
531 netif_wake_queue(dev
);
534 if (wc
->status
!= IB_WC_SUCCESS
&&
535 wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
536 struct ipoib_neigh
*neigh
;
538 ipoib_dbg(priv
, "failed cm send event "
539 "(status=%d, wrid=%d vend_err %x)\n",
540 wc
->status
, wr_id
, wc
->vendor_err
);
542 spin_lock(&priv
->lock
);
547 list_del(&neigh
->list
);
549 ipoib_put_ah(neigh
->ah
);
550 ipoib_neigh_free(dev
, neigh
);
555 /* queue would be re-started anyway when TX is destroyed,
556 * but it makes sense to do it ASAP here. */
557 if (test_and_clear_bit(IPOIB_FLAG_NETIF_STOPPED
, &tx
->flags
))
558 netif_wake_queue(dev
);
560 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
561 list_move(&tx
->list
, &priv
->cm
.reap_list
);
562 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
565 clear_bit(IPOIB_FLAG_OPER_UP
, &tx
->flags
);
567 spin_unlock(&priv
->lock
);
570 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
573 static void ipoib_cm_tx_completion(struct ib_cq
*cq
, void *tx_ptr
)
575 struct ipoib_cm_tx
*tx
= tx_ptr
;
578 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
580 n
= ib_poll_cq(cq
, IPOIB_NUM_WC
, tx
->ibwc
);
581 for (i
= 0; i
< n
; ++i
)
582 ipoib_cm_handle_tx_wc(tx
->dev
, tx
, tx
->ibwc
+ i
);
583 } while (n
== IPOIB_NUM_WC
);
586 int ipoib_cm_dev_open(struct net_device
*dev
)
588 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
591 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
594 priv
->cm
.id
= ib_create_cm_id(priv
->ca
, ipoib_cm_rx_handler
, dev
);
595 if (IS_ERR(priv
->cm
.id
)) {
596 printk(KERN_WARNING
"%s: failed to create CM ID\n", priv
->ca
->name
);
597 return IS_ERR(priv
->cm
.id
);
600 ret
= ib_cm_listen(priv
->cm
.id
, cpu_to_be64(IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
),
603 printk(KERN_WARNING
"%s: failed to listen on ID 0x%llx\n", priv
->ca
->name
,
604 IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
);
605 ib_destroy_cm_id(priv
->cm
.id
);
611 void ipoib_cm_dev_stop(struct net_device
*dev
)
613 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
614 struct ipoib_cm_rx
*p
;
617 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
620 ib_destroy_cm_id(priv
->cm
.id
);
621 spin_lock_irqsave(&priv
->lock
, flags
);
622 while (!list_empty(&priv
->cm
.passive_ids
)) {
623 p
= list_entry(priv
->cm
.passive_ids
.next
, typeof(*p
), list
);
624 list_del_init(&p
->list
);
625 spin_unlock_irqrestore(&priv
->lock
, flags
);
626 ib_destroy_cm_id(p
->id
);
627 ib_destroy_qp(p
->qp
);
629 spin_lock_irqsave(&priv
->lock
, flags
);
631 spin_unlock_irqrestore(&priv
->lock
, flags
);
633 cancel_delayed_work(&priv
->cm
.stale_task
);
636 static int ipoib_cm_rep_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
638 struct ipoib_cm_tx
*p
= cm_id
->context
;
639 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
640 struct ipoib_cm_data
*data
= event
->private_data
;
641 struct sk_buff_head skqueue
;
642 struct ib_qp_attr qp_attr
;
643 int qp_attr_mask
, ret
;
647 p
->mtu
= be32_to_cpu(data
->mtu
);
649 if (p
->mtu
< priv
->dev
->mtu
+ IPOIB_ENCAP_LEN
) {
650 ipoib_warn(priv
, "Rejecting connection: mtu %d < device mtu %d + 4\n",
651 p
->mtu
, priv
->dev
->mtu
);
655 qp_attr
.qp_state
= IB_QPS_RTR
;
656 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
658 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
662 qp_attr
.rq_psn
= 0 /* FIXME */;
663 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
665 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
669 qp_attr
.qp_state
= IB_QPS_RTS
;
670 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
672 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
675 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
677 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
681 skb_queue_head_init(&skqueue
);
683 spin_lock_irqsave(&priv
->lock
, flags
);
684 set_bit(IPOIB_FLAG_OPER_UP
, &p
->flags
);
686 while ((skb
= __skb_dequeue(&p
->neigh
->queue
)))
687 __skb_queue_tail(&skqueue
, skb
);
688 spin_unlock_irqrestore(&priv
->lock
, flags
);
690 while ((skb
= __skb_dequeue(&skqueue
))) {
692 if (dev_queue_xmit(skb
))
693 ipoib_warn(priv
, "dev_queue_xmit failed "
694 "to requeue packet\n");
697 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
699 ipoib_warn(priv
, "failed to send RTU: %d\n", ret
);
705 static struct ib_qp
*ipoib_cm_create_tx_qp(struct net_device
*dev
, struct ib_cq
*cq
)
707 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
708 struct ib_qp_init_attr attr
= {};
709 attr
.recv_cq
= priv
->cq
;
710 attr
.srq
= priv
->cm
.srq
;
711 attr
.cap
.max_send_wr
= ipoib_sendq_size
;
712 attr
.cap
.max_send_sge
= 1;
713 attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
714 attr
.qp_type
= IB_QPT_RC
;
716 return ib_create_qp(priv
->pd
, &attr
);
719 static int ipoib_cm_send_req(struct net_device
*dev
,
720 struct ib_cm_id
*id
, struct ib_qp
*qp
,
722 struct ib_sa_path_rec
*pathrec
)
724 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
725 struct ipoib_cm_data data
= {};
726 struct ib_cm_req_param req
= {};
728 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
729 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
731 req
.primary_path
= pathrec
;
732 req
.alternate_path
= NULL
;
733 req
.service_id
= cpu_to_be64(IPOIB_CM_IETF_ID
| qpn
);
734 req
.qp_num
= qp
->qp_num
;
735 req
.qp_type
= qp
->qp_type
;
736 req
.private_data
= &data
;
737 req
.private_data_len
= sizeof data
;
738 req
.flow_control
= 0;
740 req
.starting_psn
= 0; /* FIXME */
743 * Pick some arbitrary defaults here; we could make these
744 * module parameters if anyone cared about setting them.
746 req
.responder_resources
= 4;
747 req
.remote_cm_response_timeout
= 20;
748 req
.local_cm_response_timeout
= 20;
749 req
.retry_count
= 0; /* RFC draft warns against retries */
750 req
.rnr_retry_count
= 0; /* RFC draft warns against retries */
751 req
.max_cm_retries
= 15;
753 return ib_send_cm_req(id
, &req
);
756 static int ipoib_cm_modify_tx_init(struct net_device
*dev
,
757 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
)
759 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
760 struct ib_qp_attr qp_attr
;
761 int qp_attr_mask
, ret
;
762 ret
= ib_find_cached_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &qp_attr
.pkey_index
);
764 ipoib_warn(priv
, "pkey 0x%x not in cache: %d\n", priv
->pkey
, ret
);
768 qp_attr
.qp_state
= IB_QPS_INIT
;
769 qp_attr
.qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
770 qp_attr
.port_num
= priv
->port
;
771 qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
773 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
775 ipoib_warn(priv
, "failed to modify tx QP to INIT: %d\n", ret
);
781 static int ipoib_cm_tx_init(struct ipoib_cm_tx
*p
, u32 qpn
,
782 struct ib_sa_path_rec
*pathrec
)
784 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
787 p
->tx_ring
= kzalloc(ipoib_sendq_size
* sizeof *p
->tx_ring
,
790 ipoib_warn(priv
, "failed to allocate tx ring\n");
795 p
->cq
= ib_create_cq(priv
->ca
, ipoib_cm_tx_completion
, NULL
, p
,
796 ipoib_sendq_size
+ 1);
798 ret
= PTR_ERR(p
->cq
);
799 ipoib_warn(priv
, "failed to allocate tx cq: %d\n", ret
);
803 ret
= ib_req_notify_cq(p
->cq
, IB_CQ_NEXT_COMP
);
805 ipoib_warn(priv
, "failed to request completion notification: %d\n", ret
);
809 p
->qp
= ipoib_cm_create_tx_qp(p
->dev
, p
->cq
);
811 ret
= PTR_ERR(p
->qp
);
812 ipoib_warn(priv
, "failed to allocate tx qp: %d\n", ret
);
816 p
->id
= ib_create_cm_id(priv
->ca
, ipoib_cm_tx_handler
, p
);
818 ret
= PTR_ERR(p
->id
);
819 ipoib_warn(priv
, "failed to create tx cm id: %d\n", ret
);
823 ret
= ipoib_cm_modify_tx_init(p
->dev
, p
->id
, p
->qp
);
825 ipoib_warn(priv
, "failed to modify tx qp to rtr: %d\n", ret
);
829 ret
= ipoib_cm_send_req(p
->dev
, p
->id
, p
->qp
, qpn
, pathrec
);
831 ipoib_warn(priv
, "failed to send cm req: %d\n", ret
);
835 ipoib_dbg(priv
, "Request connection 0x%x for gid " IPOIB_GID_FMT
" qpn 0x%x\n",
836 p
->qp
->qp_num
, IPOIB_GID_ARG(pathrec
->dgid
), qpn
);
842 ib_destroy_cm_id(p
->id
);
845 ib_destroy_qp(p
->qp
);
849 ib_destroy_cq(p
->cq
);
856 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx
*p
)
858 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
859 struct ipoib_tx_buf
*tx_req
;
861 ipoib_dbg(priv
, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
862 p
->qp
? p
->qp
->qp_num
: 0, p
->tx_head
, p
->tx_tail
);
865 ib_destroy_cm_id(p
->id
);
868 ib_destroy_qp(p
->qp
);
871 ib_destroy_cq(p
->cq
);
873 if (test_bit(IPOIB_FLAG_NETIF_STOPPED
, &p
->flags
))
874 netif_wake_queue(p
->dev
);
877 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
878 tx_req
= &p
->tx_ring
[p
->tx_tail
& (ipoib_sendq_size
- 1)];
879 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
,
881 dev_kfree_skb_any(tx_req
->skb
);
891 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
892 struct ib_cm_event
*event
)
894 struct ipoib_cm_tx
*tx
= cm_id
->context
;
895 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
896 struct net_device
*dev
= priv
->dev
;
897 struct ipoib_neigh
*neigh
;
901 switch (event
->event
) {
902 case IB_CM_DREQ_RECEIVED
:
903 ipoib_dbg(priv
, "DREQ received.\n");
904 ib_send_cm_drep(cm_id
, NULL
, 0);
906 case IB_CM_REP_RECEIVED
:
907 ipoib_dbg(priv
, "REP received.\n");
908 ret
= ipoib_cm_rep_handler(cm_id
, event
);
910 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
913 case IB_CM_REQ_ERROR
:
914 case IB_CM_REJ_RECEIVED
:
915 case IB_CM_TIMEWAIT_EXIT
:
916 ipoib_dbg(priv
, "CM error %d.\n", event
->event
);
917 spin_lock_irqsave(&priv
->tx_lock
, flags
);
918 spin_lock(&priv
->lock
);
923 list_del(&neigh
->list
);
925 ipoib_put_ah(neigh
->ah
);
926 ipoib_neigh_free(dev
, neigh
);
931 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
932 list_move(&tx
->list
, &priv
->cm
.reap_list
);
933 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
936 spin_unlock(&priv
->lock
);
937 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
946 struct ipoib_cm_tx
*ipoib_cm_create_tx(struct net_device
*dev
, struct ipoib_path
*path
,
947 struct ipoib_neigh
*neigh
)
949 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
950 struct ipoib_cm_tx
*tx
;
952 tx
= kzalloc(sizeof *tx
, GFP_ATOMIC
);
960 list_add(&tx
->list
, &priv
->cm
.start_list
);
961 set_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
);
962 queue_work(ipoib_workqueue
, &priv
->cm
.start_task
);
966 void ipoib_cm_destroy_tx(struct ipoib_cm_tx
*tx
)
968 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
969 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
970 list_move(&tx
->list
, &priv
->cm
.reap_list
);
971 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
972 ipoib_dbg(priv
, "Reap connection for gid " IPOIB_GID_FMT
"\n",
973 IPOIB_GID_ARG(tx
->neigh
->dgid
));
978 static void ipoib_cm_tx_start(struct work_struct
*work
)
980 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
982 struct net_device
*dev
= priv
->dev
;
983 struct ipoib_neigh
*neigh
;
984 struct ipoib_cm_tx
*p
;
988 struct ib_sa_path_rec pathrec
;
991 spin_lock_irqsave(&priv
->tx_lock
, flags
);
992 spin_lock(&priv
->lock
);
993 while (!list_empty(&priv
->cm
.start_list
)) {
994 p
= list_entry(priv
->cm
.start_list
.next
, typeof(*p
), list
);
995 list_del_init(&p
->list
);
997 qpn
= IPOIB_QPN(neigh
->neighbour
->ha
);
998 memcpy(&pathrec
, &p
->path
->pathrec
, sizeof pathrec
);
999 spin_unlock(&priv
->lock
);
1000 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1001 ret
= ipoib_cm_tx_init(p
, qpn
, &pathrec
);
1002 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1003 spin_lock(&priv
->lock
);
1008 list_del(&neigh
->list
);
1010 ipoib_put_ah(neigh
->ah
);
1011 ipoib_neigh_free(dev
, neigh
);
1017 spin_unlock(&priv
->lock
);
1018 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1021 static void ipoib_cm_tx_reap(struct work_struct
*work
)
1023 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1025 struct ipoib_cm_tx
*p
;
1026 unsigned long flags
;
1028 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1029 spin_lock(&priv
->lock
);
1030 while (!list_empty(&priv
->cm
.reap_list
)) {
1031 p
= list_entry(priv
->cm
.reap_list
.next
, typeof(*p
), list
);
1033 spin_unlock(&priv
->lock
);
1034 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1035 ipoib_cm_tx_destroy(p
);
1036 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1037 spin_lock(&priv
->lock
);
1039 spin_unlock(&priv
->lock
);
1040 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1043 static void ipoib_cm_skb_reap(struct work_struct
*work
)
1045 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1047 struct net_device
*dev
= priv
->dev
;
1048 struct sk_buff
*skb
;
1049 unsigned long flags
;
1051 unsigned mtu
= priv
->mcast_mtu
;
1053 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1054 spin_lock(&priv
->lock
);
1055 while ((skb
= skb_dequeue(&priv
->cm
.skb_queue
))) {
1056 spin_unlock(&priv
->lock
);
1057 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1058 if (skb
->protocol
== htons(ETH_P_IP
))
1059 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1060 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1061 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1062 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
, dev
);
1064 dev_kfree_skb_any(skb
);
1065 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1066 spin_lock(&priv
->lock
);
1068 spin_unlock(&priv
->lock
);
1069 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1072 void ipoib_cm_skb_too_long(struct net_device
* dev
, struct sk_buff
*skb
,
1075 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1076 int e
= skb_queue_empty(&priv
->cm
.skb_queue
);
1079 skb
->dst
->ops
->update_pmtu(skb
->dst
, mtu
);
1081 skb_queue_tail(&priv
->cm
.skb_queue
, skb
);
1083 queue_work(ipoib_workqueue
, &priv
->cm
.skb_task
);
1086 static void ipoib_cm_stale_task(struct work_struct
*work
)
1088 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1089 cm
.stale_task
.work
);
1090 struct ipoib_cm_rx
*p
;
1091 unsigned long flags
;
1093 spin_lock_irqsave(&priv
->lock
, flags
);
1094 while (!list_empty(&priv
->cm
.passive_ids
)) {
1095 /* List if sorted by LRU, start from tail,
1096 * stop when we see a recently used entry */
1097 p
= list_entry(priv
->cm
.passive_ids
.prev
, typeof(*p
), list
);
1098 if (time_before_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_TIMEOUT
))
1100 list_del_init(&p
->list
);
1101 spin_unlock_irqrestore(&priv
->lock
, flags
);
1102 ib_destroy_cm_id(p
->id
);
1103 ib_destroy_qp(p
->qp
);
1105 spin_lock_irqsave(&priv
->lock
, flags
);
1107 spin_unlock_irqrestore(&priv
->lock
, flags
);
1111 static ssize_t
show_mode(struct device
*d
, struct device_attribute
*attr
,
1114 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(d
));
1116 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
1117 return sprintf(buf
, "connected\n");
1119 return sprintf(buf
, "datagram\n");
1122 static ssize_t
set_mode(struct device
*d
, struct device_attribute
*attr
,
1123 const char *buf
, size_t count
)
1125 struct net_device
*dev
= to_net_dev(d
);
1126 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1128 /* flush paths if we switch modes so that connections are restarted */
1129 if (IPOIB_CM_SUPPORTED(dev
->dev_addr
) && !strcmp(buf
, "connected\n")) {
1130 set_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
1131 ipoib_warn(priv
, "enabling connected mode "
1132 "will cause multicast packet drops\n");
1133 ipoib_flush_paths(dev
);
1137 if (!strcmp(buf
, "datagram\n")) {
1138 clear_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
1139 dev
->mtu
= min(priv
->mcast_mtu
, dev
->mtu
);
1140 ipoib_flush_paths(dev
);
1147 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
, show_mode
, set_mode
);
1149 int ipoib_cm_add_mode_attr(struct net_device
*dev
)
1151 return device_create_file(&dev
->dev
, &dev_attr_mode
);
1154 int ipoib_cm_dev_init(struct net_device
*dev
)
1156 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1157 struct ib_srq_init_attr srq_init_attr
= {
1159 .max_wr
= ipoib_recvq_size
,
1160 .max_sge
= IPOIB_CM_RX_SG
1165 INIT_LIST_HEAD(&priv
->cm
.passive_ids
);
1166 INIT_LIST_HEAD(&priv
->cm
.reap_list
);
1167 INIT_LIST_HEAD(&priv
->cm
.start_list
);
1168 INIT_WORK(&priv
->cm
.start_task
, ipoib_cm_tx_start
);
1169 INIT_WORK(&priv
->cm
.reap_task
, ipoib_cm_tx_reap
);
1170 INIT_WORK(&priv
->cm
.skb_task
, ipoib_cm_skb_reap
);
1171 INIT_DELAYED_WORK(&priv
->cm
.stale_task
, ipoib_cm_stale_task
);
1173 skb_queue_head_init(&priv
->cm
.skb_queue
);
1175 priv
->cm
.srq
= ib_create_srq(priv
->pd
, &srq_init_attr
);
1176 if (IS_ERR(priv
->cm
.srq
)) {
1177 ret
= PTR_ERR(priv
->cm
.srq
);
1178 priv
->cm
.srq
= NULL
;
1182 priv
->cm
.srq_ring
= kzalloc(ipoib_recvq_size
* sizeof *priv
->cm
.srq_ring
,
1184 if (!priv
->cm
.srq_ring
) {
1185 printk(KERN_WARNING
"%s: failed to allocate CM ring (%d entries)\n",
1186 priv
->ca
->name
, ipoib_recvq_size
);
1187 ipoib_cm_dev_cleanup(dev
);
1191 for (i
= 0; i
< IPOIB_CM_RX_SG
; ++i
)
1192 priv
->cm
.rx_sge
[i
].lkey
= priv
->mr
->lkey
;
1194 priv
->cm
.rx_sge
[0].length
= IPOIB_CM_HEAD_SIZE
;
1195 for (i
= 1; i
< IPOIB_CM_RX_SG
; ++i
)
1196 priv
->cm
.rx_sge
[i
].length
= PAGE_SIZE
;
1197 priv
->cm
.rx_wr
.next
= NULL
;
1198 priv
->cm
.rx_wr
.sg_list
= priv
->cm
.rx_sge
;
1199 priv
->cm
.rx_wr
.num_sge
= IPOIB_CM_RX_SG
;
1201 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
1202 if (!ipoib_cm_alloc_rx_skb(dev
, i
, IPOIB_CM_RX_SG
- 1,
1203 priv
->cm
.srq_ring
[i
].mapping
)) {
1204 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
1205 ipoib_cm_dev_cleanup(dev
);
1208 if (ipoib_cm_post_receive(dev
, i
)) {
1209 ipoib_warn(priv
, "ipoib_ib_post_receive failed for buf %d\n", i
);
1210 ipoib_cm_dev_cleanup(dev
);
1215 priv
->dev
->dev_addr
[0] = IPOIB_FLAGS_RC
;
1219 void ipoib_cm_dev_cleanup(struct net_device
*dev
)
1221 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1227 ipoib_dbg(priv
, "Cleanup ipoib connected mode.\n");
1229 ret
= ib_destroy_srq(priv
->cm
.srq
);
1231 ipoib_warn(priv
, "ib_destroy_srq failed: %d\n", ret
);
1233 priv
->cm
.srq
= NULL
;
1234 if (!priv
->cm
.srq_ring
)
1236 for (i
= 0; i
< ipoib_recvq_size
; ++i
)
1237 if (priv
->cm
.srq_ring
[i
].skb
) {
1238 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
1239 priv
->cm
.srq_ring
[i
].mapping
);
1240 dev_kfree_skb_any(priv
->cm
.srq_ring
[i
].skb
);
1241 priv
->cm
.srq_ring
[i
].skb
= NULL
;
1243 kfree(priv
->cm
.srq_ring
);
1244 priv
->cm
.srq_ring
= NULL
;