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_OP_CM
| IPOIB_OP_RECV
;
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_OP_CM
| IPOIB_OP_RECV
);
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_OP_CM
| IPOIB_OP_RECV
))) {
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 (unlikely(!(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
| IPOIB_OP_CM
;
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 (++priv
->tx_outstanding
== ipoib_sendq_size
) {
553 ipoib_dbg(priv
, "TX ring 0x%x full, stopping kernel net queue\n",
555 netif_stop_queue(dev
);
560 void ipoib_cm_handle_tx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
562 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
563 struct ipoib_cm_tx
*tx
= wc
->qp
->qp_context
;
564 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_OP_CM
;
565 struct ipoib_tx_buf
*tx_req
;
568 ipoib_dbg_data(priv
, "cm send completion: id %d, status: %d\n",
571 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
572 ipoib_warn(priv
, "cm send completion event with wrid %d (> %d)\n",
573 wr_id
, ipoib_sendq_size
);
577 tx_req
= &tx
->tx_ring
[wr_id
];
579 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
, DMA_TO_DEVICE
);
581 /* FIXME: is this right? Shouldn't we only increment on success? */
582 ++dev
->stats
.tx_packets
;
583 dev
->stats
.tx_bytes
+= tx_req
->skb
->len
;
585 dev_kfree_skb_any(tx_req
->skb
);
587 spin_lock_irqsave(&priv
->tx_lock
, flags
);
589 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
590 netif_queue_stopped(dev
) &&
591 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
592 netif_wake_queue(dev
);
594 if (wc
->status
!= IB_WC_SUCCESS
&&
595 wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
596 struct ipoib_neigh
*neigh
;
598 ipoib_dbg(priv
, "failed cm send event "
599 "(status=%d, wrid=%d vend_err %x)\n",
600 wc
->status
, wr_id
, wc
->vendor_err
);
602 spin_lock(&priv
->lock
);
607 list_del(&neigh
->list
);
609 ipoib_put_ah(neigh
->ah
);
610 ipoib_neigh_free(dev
, neigh
);
615 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
616 list_move(&tx
->list
, &priv
->cm
.reap_list
);
617 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
620 clear_bit(IPOIB_FLAG_OPER_UP
, &tx
->flags
);
622 spin_unlock(&priv
->lock
);
625 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
628 int ipoib_cm_dev_open(struct net_device
*dev
)
630 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
633 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
636 priv
->cm
.id
= ib_create_cm_id(priv
->ca
, ipoib_cm_rx_handler
, dev
);
637 if (IS_ERR(priv
->cm
.id
)) {
638 printk(KERN_WARNING
"%s: failed to create CM ID\n", priv
->ca
->name
);
639 ret
= PTR_ERR(priv
->cm
.id
);
643 ret
= ib_cm_listen(priv
->cm
.id
, cpu_to_be64(IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
),
646 printk(KERN_WARNING
"%s: failed to listen on ID 0x%llx\n", priv
->ca
->name
,
647 IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
);
654 ib_destroy_cm_id(priv
->cm
.id
);
660 void ipoib_cm_dev_stop(struct net_device
*dev
)
662 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
663 struct ipoib_cm_rx
*p
, *n
;
668 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
) || !priv
->cm
.id
)
671 ib_destroy_cm_id(priv
->cm
.id
);
674 spin_lock_irq(&priv
->lock
);
675 while (!list_empty(&priv
->cm
.passive_ids
)) {
676 p
= list_entry(priv
->cm
.passive_ids
.next
, typeof(*p
), list
);
677 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
678 p
->state
= IPOIB_CM_RX_ERROR
;
679 spin_unlock_irq(&priv
->lock
);
680 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
682 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
683 spin_lock_irq(&priv
->lock
);
686 /* Wait for all RX to be drained */
689 while (!list_empty(&priv
->cm
.rx_error_list
) ||
690 !list_empty(&priv
->cm
.rx_flush_list
) ||
691 !list_empty(&priv
->cm
.rx_drain_list
)) {
692 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
693 ipoib_warn(priv
, "RX drain timing out\n");
696 * assume the HW is wedged and just free up everything.
698 list_splice_init(&priv
->cm
.rx_flush_list
, &list
);
699 list_splice_init(&priv
->cm
.rx_error_list
, &list
);
700 list_splice_init(&priv
->cm
.rx_drain_list
, &list
);
703 spin_unlock_irq(&priv
->lock
);
706 spin_lock_irq(&priv
->lock
);
709 list_splice_init(&priv
->cm
.rx_reap_list
, &list
);
711 spin_unlock_irq(&priv
->lock
);
713 list_for_each_entry_safe(p
, n
, &list
, list
) {
714 ib_destroy_cm_id(p
->id
);
715 ib_destroy_qp(p
->qp
);
719 cancel_delayed_work(&priv
->cm
.stale_task
);
722 static int ipoib_cm_rep_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
724 struct ipoib_cm_tx
*p
= cm_id
->context
;
725 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
726 struct ipoib_cm_data
*data
= event
->private_data
;
727 struct sk_buff_head skqueue
;
728 struct ib_qp_attr qp_attr
;
729 int qp_attr_mask
, ret
;
732 p
->mtu
= be32_to_cpu(data
->mtu
);
734 if (p
->mtu
<= IPOIB_ENCAP_LEN
) {
735 ipoib_warn(priv
, "Rejecting connection: mtu %d <= %d\n",
736 p
->mtu
, IPOIB_ENCAP_LEN
);
740 qp_attr
.qp_state
= IB_QPS_RTR
;
741 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
743 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
747 qp_attr
.rq_psn
= 0 /* FIXME */;
748 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
750 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
754 qp_attr
.qp_state
= IB_QPS_RTS
;
755 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
757 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
760 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
762 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
766 skb_queue_head_init(&skqueue
);
768 spin_lock_irq(&priv
->lock
);
769 set_bit(IPOIB_FLAG_OPER_UP
, &p
->flags
);
771 while ((skb
= __skb_dequeue(&p
->neigh
->queue
)))
772 __skb_queue_tail(&skqueue
, skb
);
773 spin_unlock_irq(&priv
->lock
);
775 while ((skb
= __skb_dequeue(&skqueue
))) {
777 if (dev_queue_xmit(skb
))
778 ipoib_warn(priv
, "dev_queue_xmit failed "
779 "to requeue packet\n");
782 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
784 ipoib_warn(priv
, "failed to send RTU: %d\n", ret
);
790 static struct ib_qp
*ipoib_cm_create_tx_qp(struct net_device
*dev
, struct ipoib_cm_tx
*tx
)
792 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
793 struct ib_qp_init_attr attr
= {
797 .cap
.max_send_wr
= ipoib_sendq_size
,
798 .cap
.max_send_sge
= 1,
799 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
800 .qp_type
= IB_QPT_RC
,
804 return ib_create_qp(priv
->pd
, &attr
);
807 static int ipoib_cm_send_req(struct net_device
*dev
,
808 struct ib_cm_id
*id
, struct ib_qp
*qp
,
810 struct ib_sa_path_rec
*pathrec
)
812 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
813 struct ipoib_cm_data data
= {};
814 struct ib_cm_req_param req
= {};
816 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
817 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
819 req
.primary_path
= pathrec
;
820 req
.alternate_path
= NULL
;
821 req
.service_id
= cpu_to_be64(IPOIB_CM_IETF_ID
| qpn
);
822 req
.qp_num
= qp
->qp_num
;
823 req
.qp_type
= qp
->qp_type
;
824 req
.private_data
= &data
;
825 req
.private_data_len
= sizeof data
;
826 req
.flow_control
= 0;
828 req
.starting_psn
= 0; /* FIXME */
831 * Pick some arbitrary defaults here; we could make these
832 * module parameters if anyone cared about setting them.
834 req
.responder_resources
= 4;
835 req
.remote_cm_response_timeout
= 20;
836 req
.local_cm_response_timeout
= 20;
837 req
.retry_count
= 0; /* RFC draft warns against retries */
838 req
.rnr_retry_count
= 0; /* RFC draft warns against retries */
839 req
.max_cm_retries
= 15;
841 return ib_send_cm_req(id
, &req
);
844 static int ipoib_cm_modify_tx_init(struct net_device
*dev
,
845 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
)
847 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
848 struct ib_qp_attr qp_attr
;
849 int qp_attr_mask
, ret
;
850 ret
= ib_find_cached_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &qp_attr
.pkey_index
);
852 ipoib_warn(priv
, "pkey 0x%x not in cache: %d\n", priv
->pkey
, ret
);
856 qp_attr
.qp_state
= IB_QPS_INIT
;
857 qp_attr
.qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
858 qp_attr
.port_num
= priv
->port
;
859 qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
861 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
863 ipoib_warn(priv
, "failed to modify tx QP to INIT: %d\n", ret
);
869 static int ipoib_cm_tx_init(struct ipoib_cm_tx
*p
, u32 qpn
,
870 struct ib_sa_path_rec
*pathrec
)
872 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
875 p
->tx_ring
= kzalloc(ipoib_sendq_size
* sizeof *p
->tx_ring
,
878 ipoib_warn(priv
, "failed to allocate tx ring\n");
883 p
->qp
= ipoib_cm_create_tx_qp(p
->dev
, p
);
885 ret
= PTR_ERR(p
->qp
);
886 ipoib_warn(priv
, "failed to allocate tx qp: %d\n", ret
);
890 p
->id
= ib_create_cm_id(priv
->ca
, ipoib_cm_tx_handler
, p
);
892 ret
= PTR_ERR(p
->id
);
893 ipoib_warn(priv
, "failed to create tx cm id: %d\n", ret
);
897 ret
= ipoib_cm_modify_tx_init(p
->dev
, p
->id
, p
->qp
);
899 ipoib_warn(priv
, "failed to modify tx qp to rtr: %d\n", ret
);
903 ret
= ipoib_cm_send_req(p
->dev
, p
->id
, p
->qp
, qpn
, pathrec
);
905 ipoib_warn(priv
, "failed to send cm req: %d\n", ret
);
909 ipoib_dbg(priv
, "Request connection 0x%x for gid " IPOIB_GID_FMT
" qpn 0x%x\n",
910 p
->qp
->qp_num
, IPOIB_GID_ARG(pathrec
->dgid
), qpn
);
916 ib_destroy_cm_id(p
->id
);
919 ib_destroy_qp(p
->qp
);
926 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx
*p
)
928 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
929 struct ipoib_tx_buf
*tx_req
;
933 ipoib_dbg(priv
, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
934 p
->qp
? p
->qp
->qp_num
: 0, p
->tx_head
, p
->tx_tail
);
937 ib_destroy_cm_id(p
->id
);
940 /* Wait for all sends to complete */
942 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
943 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
944 ipoib_warn(priv
, "timing out; %d sends not completed\n",
945 p
->tx_head
- p
->tx_tail
);
955 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
956 tx_req
= &p
->tx_ring
[p
->tx_tail
& (ipoib_sendq_size
- 1)];
957 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
,
959 dev_kfree_skb_any(tx_req
->skb
);
961 spin_lock_irqsave(&priv
->tx_lock
, flags
);
962 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
963 netif_queue_stopped(p
->dev
) &&
964 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
965 netif_wake_queue(p
->dev
);
966 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
970 ib_destroy_qp(p
->qp
);
976 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
977 struct ib_cm_event
*event
)
979 struct ipoib_cm_tx
*tx
= cm_id
->context
;
980 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
981 struct net_device
*dev
= priv
->dev
;
982 struct ipoib_neigh
*neigh
;
985 switch (event
->event
) {
986 case IB_CM_DREQ_RECEIVED
:
987 ipoib_dbg(priv
, "DREQ received.\n");
988 ib_send_cm_drep(cm_id
, NULL
, 0);
990 case IB_CM_REP_RECEIVED
:
991 ipoib_dbg(priv
, "REP received.\n");
992 ret
= ipoib_cm_rep_handler(cm_id
, event
);
994 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
997 case IB_CM_REQ_ERROR
:
998 case IB_CM_REJ_RECEIVED
:
999 case IB_CM_TIMEWAIT_EXIT
:
1000 ipoib_dbg(priv
, "CM error %d.\n", event
->event
);
1001 spin_lock_irq(&priv
->tx_lock
);
1002 spin_lock(&priv
->lock
);
1007 list_del(&neigh
->list
);
1009 ipoib_put_ah(neigh
->ah
);
1010 ipoib_neigh_free(dev
, neigh
);
1015 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1016 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1017 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
1020 spin_unlock(&priv
->lock
);
1021 spin_unlock_irq(&priv
->tx_lock
);
1030 struct ipoib_cm_tx
*ipoib_cm_create_tx(struct net_device
*dev
, struct ipoib_path
*path
,
1031 struct ipoib_neigh
*neigh
)
1033 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1034 struct ipoib_cm_tx
*tx
;
1036 tx
= kzalloc(sizeof *tx
, GFP_ATOMIC
);
1044 list_add(&tx
->list
, &priv
->cm
.start_list
);
1045 set_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
);
1046 queue_work(ipoib_workqueue
, &priv
->cm
.start_task
);
1050 void ipoib_cm_destroy_tx(struct ipoib_cm_tx
*tx
)
1052 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
1053 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1054 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1055 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
1056 ipoib_dbg(priv
, "Reap connection for gid " IPOIB_GID_FMT
"\n",
1057 IPOIB_GID_ARG(tx
->neigh
->dgid
));
1062 static void ipoib_cm_tx_start(struct work_struct
*work
)
1064 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1066 struct net_device
*dev
= priv
->dev
;
1067 struct ipoib_neigh
*neigh
;
1068 struct ipoib_cm_tx
*p
;
1069 unsigned long flags
;
1072 struct ib_sa_path_rec pathrec
;
1075 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1076 spin_lock(&priv
->lock
);
1077 while (!list_empty(&priv
->cm
.start_list
)) {
1078 p
= list_entry(priv
->cm
.start_list
.next
, typeof(*p
), list
);
1079 list_del_init(&p
->list
);
1081 qpn
= IPOIB_QPN(neigh
->neighbour
->ha
);
1082 memcpy(&pathrec
, &p
->path
->pathrec
, sizeof pathrec
);
1083 spin_unlock(&priv
->lock
);
1084 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1085 ret
= ipoib_cm_tx_init(p
, qpn
, &pathrec
);
1086 spin_lock_irqsave(&priv
->tx_lock
, flags
);
1087 spin_lock(&priv
->lock
);
1092 list_del(&neigh
->list
);
1094 ipoib_put_ah(neigh
->ah
);
1095 ipoib_neigh_free(dev
, neigh
);
1101 spin_unlock(&priv
->lock
);
1102 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
1105 static void ipoib_cm_tx_reap(struct work_struct
*work
)
1107 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1109 struct ipoib_cm_tx
*p
;
1111 spin_lock_irq(&priv
->tx_lock
);
1112 spin_lock(&priv
->lock
);
1113 while (!list_empty(&priv
->cm
.reap_list
)) {
1114 p
= list_entry(priv
->cm
.reap_list
.next
, typeof(*p
), list
);
1116 spin_unlock(&priv
->lock
);
1117 spin_unlock_irq(&priv
->tx_lock
);
1118 ipoib_cm_tx_destroy(p
);
1119 spin_lock_irq(&priv
->tx_lock
);
1120 spin_lock(&priv
->lock
);
1122 spin_unlock(&priv
->lock
);
1123 spin_unlock_irq(&priv
->tx_lock
);
1126 static void ipoib_cm_skb_reap(struct work_struct
*work
)
1128 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1130 struct sk_buff
*skb
;
1132 unsigned mtu
= priv
->mcast_mtu
;
1134 spin_lock_irq(&priv
->tx_lock
);
1135 spin_lock(&priv
->lock
);
1136 while ((skb
= skb_dequeue(&priv
->cm
.skb_queue
))) {
1137 spin_unlock(&priv
->lock
);
1138 spin_unlock_irq(&priv
->tx_lock
);
1139 if (skb
->protocol
== htons(ETH_P_IP
))
1140 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1141 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1142 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1143 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
, priv
->dev
);
1145 dev_kfree_skb_any(skb
);
1146 spin_lock_irq(&priv
->tx_lock
);
1147 spin_lock(&priv
->lock
);
1149 spin_unlock(&priv
->lock
);
1150 spin_unlock_irq(&priv
->tx_lock
);
1153 void ipoib_cm_skb_too_long(struct net_device
* dev
, struct sk_buff
*skb
,
1156 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1157 int e
= skb_queue_empty(&priv
->cm
.skb_queue
);
1160 skb
->dst
->ops
->update_pmtu(skb
->dst
, mtu
);
1162 skb_queue_tail(&priv
->cm
.skb_queue
, skb
);
1164 queue_work(ipoib_workqueue
, &priv
->cm
.skb_task
);
1167 static void ipoib_cm_rx_reap(struct work_struct
*work
)
1169 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1171 struct ipoib_cm_rx
*p
, *n
;
1174 spin_lock_irq(&priv
->lock
);
1175 list_splice_init(&priv
->cm
.rx_reap_list
, &list
);
1176 spin_unlock_irq(&priv
->lock
);
1178 list_for_each_entry_safe(p
, n
, &list
, list
) {
1179 ib_destroy_cm_id(p
->id
);
1180 ib_destroy_qp(p
->qp
);
1185 static void ipoib_cm_stale_task(struct work_struct
*work
)
1187 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1188 cm
.stale_task
.work
);
1189 struct ipoib_cm_rx
*p
;
1192 spin_lock_irq(&priv
->lock
);
1193 while (!list_empty(&priv
->cm
.passive_ids
)) {
1194 /* List is sorted by LRU, start from tail,
1195 * stop when we see a recently used entry */
1196 p
= list_entry(priv
->cm
.passive_ids
.prev
, typeof(*p
), list
);
1197 if (time_before_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_TIMEOUT
))
1199 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
1200 p
->state
= IPOIB_CM_RX_ERROR
;
1201 spin_unlock_irq(&priv
->lock
);
1202 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
1204 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
1205 spin_lock_irq(&priv
->lock
);
1208 if (!list_empty(&priv
->cm
.passive_ids
))
1209 queue_delayed_work(ipoib_workqueue
,
1210 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
1211 spin_unlock_irq(&priv
->lock
);
1215 static ssize_t
show_mode(struct device
*d
, struct device_attribute
*attr
,
1218 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(d
));
1220 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
1221 return sprintf(buf
, "connected\n");
1223 return sprintf(buf
, "datagram\n");
1226 static ssize_t
set_mode(struct device
*d
, struct device_attribute
*attr
,
1227 const char *buf
, size_t count
)
1229 struct net_device
*dev
= to_net_dev(d
);
1230 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1232 /* flush paths if we switch modes so that connections are restarted */
1233 if (IPOIB_CM_SUPPORTED(dev
->dev_addr
) && !strcmp(buf
, "connected\n")) {
1234 set_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
1235 ipoib_warn(priv
, "enabling connected mode "
1236 "will cause multicast packet drops\n");
1237 ipoib_flush_paths(dev
);
1241 if (!strcmp(buf
, "datagram\n")) {
1242 clear_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
1243 dev
->mtu
= min(priv
->mcast_mtu
, dev
->mtu
);
1244 ipoib_flush_paths(dev
);
1251 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
, show_mode
, set_mode
);
1253 int ipoib_cm_add_mode_attr(struct net_device
*dev
)
1255 return device_create_file(&dev
->dev
, &dev_attr_mode
);
1258 int ipoib_cm_dev_init(struct net_device
*dev
)
1260 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1261 struct ib_srq_init_attr srq_init_attr
= {
1263 .max_wr
= ipoib_recvq_size
,
1264 .max_sge
= IPOIB_CM_RX_SG
1269 INIT_LIST_HEAD(&priv
->cm
.passive_ids
);
1270 INIT_LIST_HEAD(&priv
->cm
.reap_list
);
1271 INIT_LIST_HEAD(&priv
->cm
.start_list
);
1272 INIT_LIST_HEAD(&priv
->cm
.rx_error_list
);
1273 INIT_LIST_HEAD(&priv
->cm
.rx_flush_list
);
1274 INIT_LIST_HEAD(&priv
->cm
.rx_drain_list
);
1275 INIT_LIST_HEAD(&priv
->cm
.rx_reap_list
);
1276 INIT_WORK(&priv
->cm
.start_task
, ipoib_cm_tx_start
);
1277 INIT_WORK(&priv
->cm
.reap_task
, ipoib_cm_tx_reap
);
1278 INIT_WORK(&priv
->cm
.skb_task
, ipoib_cm_skb_reap
);
1279 INIT_WORK(&priv
->cm
.rx_reap_task
, ipoib_cm_rx_reap
);
1280 INIT_DELAYED_WORK(&priv
->cm
.stale_task
, ipoib_cm_stale_task
);
1282 skb_queue_head_init(&priv
->cm
.skb_queue
);
1284 priv
->cm
.srq
= ib_create_srq(priv
->pd
, &srq_init_attr
);
1285 if (IS_ERR(priv
->cm
.srq
)) {
1286 ret
= PTR_ERR(priv
->cm
.srq
);
1287 priv
->cm
.srq
= NULL
;
1291 priv
->cm
.srq_ring
= kzalloc(ipoib_recvq_size
* sizeof *priv
->cm
.srq_ring
,
1293 if (!priv
->cm
.srq_ring
) {
1294 printk(KERN_WARNING
"%s: failed to allocate CM ring (%d entries)\n",
1295 priv
->ca
->name
, ipoib_recvq_size
);
1296 ipoib_cm_dev_cleanup(dev
);
1300 for (i
= 0; i
< IPOIB_CM_RX_SG
; ++i
)
1301 priv
->cm
.rx_sge
[i
].lkey
= priv
->mr
->lkey
;
1303 priv
->cm
.rx_sge
[0].length
= IPOIB_CM_HEAD_SIZE
;
1304 for (i
= 1; i
< IPOIB_CM_RX_SG
; ++i
)
1305 priv
->cm
.rx_sge
[i
].length
= PAGE_SIZE
;
1306 priv
->cm
.rx_wr
.next
= NULL
;
1307 priv
->cm
.rx_wr
.sg_list
= priv
->cm
.rx_sge
;
1308 priv
->cm
.rx_wr
.num_sge
= IPOIB_CM_RX_SG
;
1310 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
1311 if (!ipoib_cm_alloc_rx_skb(dev
, i
, IPOIB_CM_RX_SG
- 1,
1312 priv
->cm
.srq_ring
[i
].mapping
)) {
1313 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
1314 ipoib_cm_dev_cleanup(dev
);
1317 if (ipoib_cm_post_receive(dev
, i
)) {
1318 ipoib_warn(priv
, "ipoib_ib_post_receive failed for buf %d\n", i
);
1319 ipoib_cm_dev_cleanup(dev
);
1324 priv
->dev
->dev_addr
[0] = IPOIB_FLAGS_RC
;
1328 void ipoib_cm_dev_cleanup(struct net_device
*dev
)
1330 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1336 ipoib_dbg(priv
, "Cleanup ipoib connected mode.\n");
1338 ret
= ib_destroy_srq(priv
->cm
.srq
);
1340 ipoib_warn(priv
, "ib_destroy_srq failed: %d\n", ret
);
1342 priv
->cm
.srq
= NULL
;
1343 if (!priv
->cm
.srq_ring
)
1345 for (i
= 0; i
< ipoib_recvq_size
; ++i
)
1346 if (priv
->cm
.srq_ring
[i
].skb
) {
1347 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
1348 priv
->cm
.srq_ring
[i
].mapping
);
1349 dev_kfree_skb_any(priv
->cm
.srq_ring
[i
].skb
);
1350 priv
->cm
.srq_ring
[i
].skb
= NULL
;
1352 kfree(priv
->cm
.srq_ring
);
1353 priv
->cm
.srq_ring
= NULL
;