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
33 #include <rdma/ib_cm.h>
36 #include <linux/icmpv6.h>
37 #include <linux/delay.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/moduleparam.h>
44 int ipoib_max_conn_qp
= 128;
46 module_param_named(max_nonsrq_conn_qp
, ipoib_max_conn_qp
, int, 0444);
47 MODULE_PARM_DESC(max_nonsrq_conn_qp
,
48 "Max number of connected-mode QPs per interface "
49 "(applied only if shared receive queue is not available)");
51 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
52 static int data_debug_level
;
54 module_param_named(cm_data_debug_level
, data_debug_level
, int, 0644);
55 MODULE_PARM_DESC(cm_data_debug_level
,
56 "Enable data path debug tracing for connected mode if > 0");
59 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
61 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
62 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
63 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
64 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
66 static struct ib_qp_attr ipoib_cm_err_attr
= {
67 .qp_state
= IB_QPS_ERR
70 #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
72 static struct ib_send_wr ipoib_cm_rx_drain_wr
= {
76 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
77 struct ib_cm_event
*event
);
79 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv
*priv
, int frags
,
80 u64 mapping
[IPOIB_CM_RX_SG
])
84 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_CM_HEAD_SIZE
, DMA_FROM_DEVICE
);
86 for (i
= 0; i
< frags
; ++i
)
87 ib_dma_unmap_page(priv
->ca
, mapping
[i
+ 1], PAGE_SIZE
, DMA_FROM_DEVICE
);
90 static int ipoib_cm_post_receive_srq(struct net_device
*dev
, int id
)
92 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
93 struct ib_recv_wr
*bad_wr
;
96 priv
->cm
.rx_wr
.wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
98 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
99 priv
->cm
.rx_sge
[i
].addr
= priv
->cm
.srq_ring
[id
].mapping
[i
];
101 ret
= ib_post_srq_recv(priv
->cm
.srq
, &priv
->cm
.rx_wr
, &bad_wr
);
103 ipoib_warn(priv
, "post srq failed for buf %d (%d)\n", id
, ret
);
104 ipoib_cm_dma_unmap_rx(priv
, priv
->cm
.num_frags
- 1,
105 priv
->cm
.srq_ring
[id
].mapping
);
106 dev_kfree_skb_any(priv
->cm
.srq_ring
[id
].skb
);
107 priv
->cm
.srq_ring
[id
].skb
= NULL
;
113 static int ipoib_cm_post_receive_nonsrq(struct net_device
*dev
,
114 struct ipoib_cm_rx
*rx
,
115 struct ib_recv_wr
*wr
,
116 struct ib_sge
*sge
, int id
)
118 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
119 struct ib_recv_wr
*bad_wr
;
122 wr
->wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
124 for (i
= 0; i
< IPOIB_CM_RX_SG
; ++i
)
125 sge
[i
].addr
= rx
->rx_ring
[id
].mapping
[i
];
127 ret
= ib_post_recv(rx
->qp
, wr
, &bad_wr
);
129 ipoib_warn(priv
, "post recv failed for buf %d (%d)\n", id
, ret
);
130 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
131 rx
->rx_ring
[id
].mapping
);
132 dev_kfree_skb_any(rx
->rx_ring
[id
].skb
);
133 rx
->rx_ring
[id
].skb
= NULL
;
139 static struct sk_buff
*ipoib_cm_alloc_rx_skb(struct net_device
*dev
,
140 struct ipoib_cm_rx_buf
*rx_ring
,
142 u64 mapping
[IPOIB_CM_RX_SG
],
145 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
149 skb
= dev_alloc_skb(IPOIB_CM_HEAD_SIZE
+ 12);
154 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
155 * IP header to a multiple of 16.
157 skb_reserve(skb
, 12);
159 mapping
[0] = ib_dma_map_single(priv
->ca
, skb
->data
, IPOIB_CM_HEAD_SIZE
,
161 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[0]))) {
162 dev_kfree_skb_any(skb
);
166 for (i
= 0; i
< frags
; i
++) {
167 struct page
*page
= alloc_page(gfp
);
171 skb_fill_page_desc(skb
, i
, page
, 0, PAGE_SIZE
);
173 mapping
[i
+ 1] = ib_dma_map_page(priv
->ca
, page
,
174 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
175 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[i
+ 1])))
179 rx_ring
[id
].skb
= skb
;
184 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_CM_HEAD_SIZE
, DMA_FROM_DEVICE
);
187 ib_dma_unmap_page(priv
->ca
, mapping
[i
], PAGE_SIZE
, DMA_FROM_DEVICE
);
189 dev_kfree_skb_any(skb
);
193 static void ipoib_cm_free_rx_ring(struct net_device
*dev
,
194 struct ipoib_cm_rx_buf
*rx_ring
)
196 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
199 for (i
= 0; i
< ipoib_recvq_size
; ++i
)
200 if (rx_ring
[i
].skb
) {
201 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
203 dev_kfree_skb_any(rx_ring
[i
].skb
);
209 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv
*priv
)
211 struct ib_send_wr
*bad_wr
;
212 struct ipoib_cm_rx
*p
;
214 /* We only reserved 1 extra slot in CQ for drain WRs, so
215 * make sure we have at most 1 outstanding WR. */
216 if (list_empty(&priv
->cm
.rx_flush_list
) ||
217 !list_empty(&priv
->cm
.rx_drain_list
))
221 * QPs on flush list are error state. This way, a "flush
222 * error" WC will be immediately generated for each WR we post.
224 p
= list_entry(priv
->cm
.rx_flush_list
.next
, typeof(*p
), list
);
225 ipoib_cm_rx_drain_wr
.wr_id
= IPOIB_CM_RX_DRAIN_WRID
;
226 if (ib_post_send(p
->qp
, &ipoib_cm_rx_drain_wr
, &bad_wr
))
227 ipoib_warn(priv
, "failed to post drain wr\n");
229 list_splice_init(&priv
->cm
.rx_flush_list
, &priv
->cm
.rx_drain_list
);
232 static void ipoib_cm_rx_event_handler(struct ib_event
*event
, void *ctx
)
234 struct ipoib_cm_rx
*p
= ctx
;
235 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
238 if (event
->event
!= IB_EVENT_QP_LAST_WQE_REACHED
)
241 spin_lock_irqsave(&priv
->lock
, flags
);
242 list_move(&p
->list
, &priv
->cm
.rx_flush_list
);
243 p
->state
= IPOIB_CM_RX_FLUSH
;
244 ipoib_cm_start_rx_drain(priv
);
245 spin_unlock_irqrestore(&priv
->lock
, flags
);
248 static struct ib_qp
*ipoib_cm_create_rx_qp(struct net_device
*dev
,
249 struct ipoib_cm_rx
*p
)
251 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
252 struct ib_qp_init_attr attr
= {
253 .event_handler
= ipoib_cm_rx_event_handler
,
254 .send_cq
= priv
->recv_cq
, /* For drain WR */
255 .recv_cq
= priv
->recv_cq
,
257 .cap
.max_send_wr
= 1, /* For drain WR */
258 .cap
.max_send_sge
= 1, /* FIXME: 0 Seems not to work */
259 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
260 .qp_type
= IB_QPT_RC
,
264 if (!ipoib_cm_has_srq(dev
)) {
265 attr
.cap
.max_recv_wr
= ipoib_recvq_size
;
266 attr
.cap
.max_recv_sge
= IPOIB_CM_RX_SG
;
269 return ib_create_qp(priv
->pd
, &attr
);
272 static int ipoib_cm_modify_rx_qp(struct net_device
*dev
,
273 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
,
276 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
277 struct ib_qp_attr qp_attr
;
278 int qp_attr_mask
, ret
;
280 qp_attr
.qp_state
= IB_QPS_INIT
;
281 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
283 ipoib_warn(priv
, "failed to init QP attr for INIT: %d\n", ret
);
286 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
288 ipoib_warn(priv
, "failed to modify QP to INIT: %d\n", ret
);
291 qp_attr
.qp_state
= IB_QPS_RTR
;
292 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
294 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
297 qp_attr
.rq_psn
= psn
;
298 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
300 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
305 * Current Mellanox HCA firmware won't generate completions
306 * with error for drain WRs unless the QP has been moved to
307 * RTS first. This work-around leaves a window where a QP has
308 * moved to error asynchronously, but this will eventually get
309 * fixed in firmware, so let's not error out if modify QP
312 qp_attr
.qp_state
= IB_QPS_RTS
;
313 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
315 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
318 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
320 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
327 static void ipoib_cm_init_rx_wr(struct net_device
*dev
,
328 struct ib_recv_wr
*wr
,
331 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
334 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
335 sge
[i
].lkey
= priv
->pd
->local_dma_lkey
;
337 sge
[0].length
= IPOIB_CM_HEAD_SIZE
;
338 for (i
= 1; i
< priv
->cm
.num_frags
; ++i
)
339 sge
[i
].length
= PAGE_SIZE
;
343 wr
->num_sge
= priv
->cm
.num_frags
;
346 static int ipoib_cm_nonsrq_init_rx(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
347 struct ipoib_cm_rx
*rx
)
349 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
351 struct ib_recv_wr wr
;
352 struct ib_sge sge
[IPOIB_CM_RX_SG
];
357 rx
->rx_ring
= vzalloc(ipoib_recvq_size
* sizeof *rx
->rx_ring
);
359 printk(KERN_WARNING
"%s: failed to allocate CM non-SRQ ring (%d entries)\n",
360 priv
->ca
->name
, ipoib_recvq_size
);
364 t
= kmalloc(sizeof *t
, GFP_KERNEL
);
370 ipoib_cm_init_rx_wr(dev
, &t
->wr
, t
->sge
);
372 spin_lock_irq(&priv
->lock
);
374 if (priv
->cm
.nonsrq_conn_qp
>= ipoib_max_conn_qp
) {
375 spin_unlock_irq(&priv
->lock
);
376 ib_send_cm_rej(cm_id
, IB_CM_REJ_NO_QP
, NULL
, 0, NULL
, 0);
380 ++priv
->cm
.nonsrq_conn_qp
;
382 spin_unlock_irq(&priv
->lock
);
384 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
385 if (!ipoib_cm_alloc_rx_skb(dev
, rx
->rx_ring
, i
, IPOIB_CM_RX_SG
- 1,
386 rx
->rx_ring
[i
].mapping
,
388 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
392 ret
= ipoib_cm_post_receive_nonsrq(dev
, rx
, &t
->wr
, t
->sge
, i
);
394 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq "
395 "failed for buf %d\n", i
);
401 rx
->recv_count
= ipoib_recvq_size
;
408 spin_lock_irq(&priv
->lock
);
409 --priv
->cm
.nonsrq_conn_qp
;
410 spin_unlock_irq(&priv
->lock
);
414 ipoib_cm_free_rx_ring(dev
, rx
->rx_ring
);
419 static int ipoib_cm_send_rep(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
420 struct ib_qp
*qp
, struct ib_cm_req_event_param
*req
,
423 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
424 struct ipoib_cm_data data
= {};
425 struct ib_cm_rep_param rep
= {};
427 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
428 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
430 rep
.private_data
= &data
;
431 rep
.private_data_len
= sizeof data
;
432 rep
.flow_control
= 0;
433 rep
.rnr_retry_count
= req
->rnr_retry_count
;
434 rep
.srq
= ipoib_cm_has_srq(dev
);
435 rep
.qp_num
= qp
->qp_num
;
436 rep
.starting_psn
= psn
;
437 return ib_send_cm_rep(cm_id
, &rep
);
440 static int ipoib_cm_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
442 struct net_device
*dev
= cm_id
->context
;
443 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
444 struct ipoib_cm_rx
*p
;
448 ipoib_dbg(priv
, "REQ arrived\n");
449 p
= kzalloc(sizeof *p
, GFP_KERNEL
);
455 p
->state
= IPOIB_CM_RX_LIVE
;
456 p
->jiffies
= jiffies
;
457 INIT_LIST_HEAD(&p
->list
);
459 p
->qp
= ipoib_cm_create_rx_qp(dev
, p
);
461 ret
= PTR_ERR(p
->qp
);
465 psn
= prandom_u32() & 0xffffff;
466 ret
= ipoib_cm_modify_rx_qp(dev
, cm_id
, p
->qp
, psn
);
470 if (!ipoib_cm_has_srq(dev
)) {
471 ret
= ipoib_cm_nonsrq_init_rx(dev
, cm_id
, p
);
476 spin_lock_irq(&priv
->lock
);
477 queue_delayed_work(priv
->wq
,
478 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
479 /* Add this entry to passive ids list head, but do not re-add it
480 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
481 p
->jiffies
= jiffies
;
482 if (p
->state
== IPOIB_CM_RX_LIVE
)
483 list_move(&p
->list
, &priv
->cm
.passive_ids
);
484 spin_unlock_irq(&priv
->lock
);
486 ret
= ipoib_cm_send_rep(dev
, cm_id
, p
->qp
, &event
->param
.req_rcvd
, psn
);
488 ipoib_warn(priv
, "failed to send REP: %d\n", ret
);
489 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
490 ipoib_warn(priv
, "unable to move qp to error state\n");
495 ib_destroy_qp(p
->qp
);
501 static int ipoib_cm_rx_handler(struct ib_cm_id
*cm_id
,
502 struct ib_cm_event
*event
)
504 struct ipoib_cm_rx
*p
;
505 struct ipoib_dev_priv
*priv
;
507 switch (event
->event
) {
508 case IB_CM_REQ_RECEIVED
:
509 return ipoib_cm_req_handler(cm_id
, event
);
510 case IB_CM_DREQ_RECEIVED
:
512 ib_send_cm_drep(cm_id
, NULL
, 0);
514 case IB_CM_REJ_RECEIVED
:
516 priv
= netdev_priv(p
->dev
);
517 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
518 ipoib_warn(priv
, "unable to move qp to error state\n");
524 /* Adjust length of skb with fragments to match received data */
525 static void skb_put_frags(struct sk_buff
*skb
, unsigned int hdr_space
,
526 unsigned int length
, struct sk_buff
*toskb
)
531 /* put header into skb */
532 size
= min(length
, hdr_space
);
537 num_frags
= skb_shinfo(skb
)->nr_frags
;
538 for (i
= 0; i
< num_frags
; i
++) {
539 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
542 /* don't need this page */
543 skb_fill_page_desc(toskb
, i
, skb_frag_page(frag
),
545 --skb_shinfo(skb
)->nr_frags
;
547 size
= min(length
, (unsigned) PAGE_SIZE
);
549 skb_frag_size_set(frag
, size
);
550 skb
->data_len
+= size
;
551 skb
->truesize
+= size
;
558 void ipoib_cm_handle_rx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
560 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
561 struct ipoib_cm_rx_buf
*rx_ring
;
562 unsigned int wr_id
= wc
->wr_id
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
);
563 struct sk_buff
*skb
, *newskb
;
564 struct ipoib_cm_rx
*p
;
566 u64 mapping
[IPOIB_CM_RX_SG
];
569 struct sk_buff
*small_skb
;
571 ipoib_dbg_data(priv
, "cm recv completion: id %d, status: %d\n",
574 if (unlikely(wr_id
>= ipoib_recvq_size
)) {
575 if (wr_id
== (IPOIB_CM_RX_DRAIN_WRID
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
))) {
576 spin_lock_irqsave(&priv
->lock
, flags
);
577 list_splice_init(&priv
->cm
.rx_drain_list
, &priv
->cm
.rx_reap_list
);
578 ipoib_cm_start_rx_drain(priv
);
579 queue_work(priv
->wq
, &priv
->cm
.rx_reap_task
);
580 spin_unlock_irqrestore(&priv
->lock
, flags
);
582 ipoib_warn(priv
, "cm recv completion event with wrid %d (> %d)\n",
583 wr_id
, ipoib_recvq_size
);
587 p
= wc
->qp
->qp_context
;
589 has_srq
= ipoib_cm_has_srq(dev
);
590 rx_ring
= has_srq
? priv
->cm
.srq_ring
: p
->rx_ring
;
592 skb
= rx_ring
[wr_id
].skb
;
594 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
595 ipoib_dbg(priv
, "cm recv error "
596 "(status=%d, wrid=%d vend_err %x)\n",
597 wc
->status
, wr_id
, wc
->vendor_err
);
598 ++dev
->stats
.rx_dropped
;
602 if (!--p
->recv_count
) {
603 spin_lock_irqsave(&priv
->lock
, flags
);
604 list_move(&p
->list
, &priv
->cm
.rx_reap_list
);
605 spin_unlock_irqrestore(&priv
->lock
, flags
);
606 queue_work(priv
->wq
, &priv
->cm
.rx_reap_task
);
612 if (unlikely(!(wr_id
& IPOIB_CM_RX_UPDATE_MASK
))) {
613 if (p
&& time_after_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_UPDATE_TIME
)) {
614 spin_lock_irqsave(&priv
->lock
, flags
);
615 p
->jiffies
= jiffies
;
616 /* Move this entry to list head, but do not re-add it
617 * if it has been moved out of list. */
618 if (p
->state
== IPOIB_CM_RX_LIVE
)
619 list_move(&p
->list
, &priv
->cm
.passive_ids
);
620 spin_unlock_irqrestore(&priv
->lock
, flags
);
624 if (wc
->byte_len
< IPOIB_CM_COPYBREAK
) {
625 int dlen
= wc
->byte_len
;
627 small_skb
= dev_alloc_skb(dlen
+ 12);
629 skb_reserve(small_skb
, 12);
630 ib_dma_sync_single_for_cpu(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
631 dlen
, DMA_FROM_DEVICE
);
632 skb_copy_from_linear_data(skb
, small_skb
->data
, dlen
);
633 ib_dma_sync_single_for_device(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
634 dlen
, DMA_FROM_DEVICE
);
635 skb_put(small_skb
, dlen
);
641 frags
= PAGE_ALIGN(wc
->byte_len
- min(wc
->byte_len
,
642 (unsigned)IPOIB_CM_HEAD_SIZE
)) / PAGE_SIZE
;
644 newskb
= ipoib_cm_alloc_rx_skb(dev
, rx_ring
, wr_id
, frags
,
645 mapping
, GFP_ATOMIC
);
646 if (unlikely(!newskb
)) {
648 * If we can't allocate a new RX buffer, dump
649 * this packet and reuse the old buffer.
651 ipoib_dbg(priv
, "failed to allocate receive buffer %d\n", wr_id
);
652 ++dev
->stats
.rx_dropped
;
656 ipoib_cm_dma_unmap_rx(priv
, frags
, rx_ring
[wr_id
].mapping
);
657 memcpy(rx_ring
[wr_id
].mapping
, mapping
, (frags
+ 1) * sizeof *mapping
);
659 ipoib_dbg_data(priv
, "received %d bytes, SLID 0x%04x\n",
660 wc
->byte_len
, wc
->slid
);
662 skb_put_frags(skb
, IPOIB_CM_HEAD_SIZE
, wc
->byte_len
, newskb
);
665 skb
->protocol
= ((struct ipoib_header
*) skb
->data
)->proto
;
666 skb_reset_mac_header(skb
);
667 skb_pull(skb
, IPOIB_ENCAP_LEN
);
669 ++dev
->stats
.rx_packets
;
670 dev
->stats
.rx_bytes
+= skb
->len
;
673 /* XXX get correct PACKET_ type here */
674 skb
->pkt_type
= PACKET_HOST
;
675 netif_receive_skb(skb
);
679 if (unlikely(ipoib_cm_post_receive_srq(dev
, wr_id
)))
680 ipoib_warn(priv
, "ipoib_cm_post_receive_srq failed "
681 "for buf %d\n", wr_id
);
683 if (unlikely(ipoib_cm_post_receive_nonsrq(dev
, p
,
688 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq failed "
689 "for buf %d\n", wr_id
);
694 static inline int post_send(struct ipoib_dev_priv
*priv
,
695 struct ipoib_cm_tx
*tx
,
697 struct ipoib_tx_buf
*tx_req
)
699 struct ib_send_wr
*bad_wr
;
701 ipoib_build_sge(priv
, tx_req
);
703 priv
->tx_wr
.wr
.wr_id
= wr_id
| IPOIB_OP_CM
;
705 return ib_post_send(tx
->qp
, &priv
->tx_wr
.wr
, &bad_wr
);
708 void ipoib_cm_send(struct net_device
*dev
, struct sk_buff
*skb
, struct ipoib_cm_tx
*tx
)
710 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
711 struct ipoib_tx_buf
*tx_req
;
713 unsigned usable_sge
= tx
->max_send_sge
- !!skb_headlen(skb
);
715 if (unlikely(skb
->len
> tx
->mtu
)) {
716 ipoib_warn(priv
, "packet len %d (> %d) too long to send, dropping\n",
718 ++dev
->stats
.tx_dropped
;
719 ++dev
->stats
.tx_errors
;
720 ipoib_cm_skb_too_long(dev
, skb
, tx
->mtu
- IPOIB_ENCAP_LEN
);
723 if (skb_shinfo(skb
)->nr_frags
> usable_sge
) {
724 if (skb_linearize(skb
) < 0) {
725 ipoib_warn(priv
, "skb could not be linearized\n");
726 ++dev
->stats
.tx_dropped
;
727 ++dev
->stats
.tx_errors
;
728 dev_kfree_skb_any(skb
);
731 /* Does skb_linearize return ok without reducing nr_frags? */
732 if (skb_shinfo(skb
)->nr_frags
> usable_sge
) {
733 ipoib_warn(priv
, "too many frags after skb linearize\n");
734 ++dev
->stats
.tx_dropped
;
735 ++dev
->stats
.tx_errors
;
736 dev_kfree_skb_any(skb
);
740 ipoib_dbg_data(priv
, "sending packet: head 0x%x length %d connection 0x%x\n",
741 tx
->tx_head
, skb
->len
, tx
->qp
->qp_num
);
744 * We put the skb into the tx_ring _before_ we call post_send()
745 * because it's entirely possible that the completion handler will
746 * run before we execute anything after the post_send(). That
747 * means we have to make sure everything is properly recorded and
748 * our state is consistent before we call post_send().
750 tx_req
= &tx
->tx_ring
[tx
->tx_head
& (ipoib_sendq_size
- 1)];
753 if (unlikely(ipoib_dma_map_tx(priv
->ca
, tx_req
))) {
754 ++dev
->stats
.tx_errors
;
755 dev_kfree_skb_any(skb
);
762 rc
= post_send(priv
, tx
, tx
->tx_head
& (ipoib_sendq_size
- 1), tx_req
);
764 ipoib_warn(priv
, "post_send failed, error %d\n", rc
);
765 ++dev
->stats
.tx_errors
;
766 ipoib_dma_unmap_tx(priv
, tx_req
);
767 dev_kfree_skb_any(skb
);
769 dev
->trans_start
= jiffies
;
772 if (++priv
->tx_outstanding
== ipoib_sendq_size
) {
773 ipoib_dbg(priv
, "TX ring 0x%x full, stopping kernel net queue\n",
775 netif_stop_queue(dev
);
776 rc
= ib_req_notify_cq(priv
->send_cq
,
777 IB_CQ_NEXT_COMP
| IB_CQ_REPORT_MISSED_EVENTS
);
779 ipoib_warn(priv
, "request notify on send CQ failed\n");
781 ipoib_send_comp_handler(priv
->send_cq
, dev
);
786 void ipoib_cm_handle_tx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
788 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
789 struct ipoib_cm_tx
*tx
= wc
->qp
->qp_context
;
790 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_OP_CM
;
791 struct ipoib_tx_buf
*tx_req
;
794 ipoib_dbg_data(priv
, "cm send completion: id %d, status: %d\n",
797 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
798 ipoib_warn(priv
, "cm send completion event with wrid %d (> %d)\n",
799 wr_id
, ipoib_sendq_size
);
803 tx_req
= &tx
->tx_ring
[wr_id
];
805 ipoib_dma_unmap_tx(priv
, tx_req
);
807 /* FIXME: is this right? Shouldn't we only increment on success? */
808 ++dev
->stats
.tx_packets
;
809 dev
->stats
.tx_bytes
+= tx_req
->skb
->len
;
811 dev_kfree_skb_any(tx_req
->skb
);
816 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
817 netif_queue_stopped(dev
) &&
818 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
819 netif_wake_queue(dev
);
821 if (wc
->status
!= IB_WC_SUCCESS
&&
822 wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
823 struct ipoib_neigh
*neigh
;
825 ipoib_dbg(priv
, "failed cm send event "
826 "(status=%d, wrid=%d vend_err %x)\n",
827 wc
->status
, wr_id
, wc
->vendor_err
);
829 spin_lock_irqsave(&priv
->lock
, flags
);
834 ipoib_neigh_free(neigh
);
839 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
840 list_move(&tx
->list
, &priv
->cm
.reap_list
);
841 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
844 clear_bit(IPOIB_FLAG_OPER_UP
, &tx
->flags
);
846 spin_unlock_irqrestore(&priv
->lock
, flags
);
849 netif_tx_unlock(dev
);
852 int ipoib_cm_dev_open(struct net_device
*dev
)
854 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
857 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
860 priv
->cm
.id
= ib_create_cm_id(priv
->ca
, ipoib_cm_rx_handler
, dev
);
861 if (IS_ERR(priv
->cm
.id
)) {
862 printk(KERN_WARNING
"%s: failed to create CM ID\n", priv
->ca
->name
);
863 ret
= PTR_ERR(priv
->cm
.id
);
867 ret
= ib_cm_listen(priv
->cm
.id
, cpu_to_be64(IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
),
870 printk(KERN_WARNING
"%s: failed to listen on ID 0x%llx\n", priv
->ca
->name
,
871 IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
);
878 ib_destroy_cm_id(priv
->cm
.id
);
884 static void ipoib_cm_free_rx_reap_list(struct net_device
*dev
)
886 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
887 struct ipoib_cm_rx
*rx
, *n
;
890 spin_lock_irq(&priv
->lock
);
891 list_splice_init(&priv
->cm
.rx_reap_list
, &list
);
892 spin_unlock_irq(&priv
->lock
);
894 list_for_each_entry_safe(rx
, n
, &list
, list
) {
895 ib_destroy_cm_id(rx
->id
);
896 ib_destroy_qp(rx
->qp
);
897 if (!ipoib_cm_has_srq(dev
)) {
898 ipoib_cm_free_rx_ring(priv
->dev
, rx
->rx_ring
);
899 spin_lock_irq(&priv
->lock
);
900 --priv
->cm
.nonsrq_conn_qp
;
901 spin_unlock_irq(&priv
->lock
);
907 void ipoib_cm_dev_stop(struct net_device
*dev
)
909 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
910 struct ipoib_cm_rx
*p
;
914 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
) || !priv
->cm
.id
)
917 ib_destroy_cm_id(priv
->cm
.id
);
920 spin_lock_irq(&priv
->lock
);
921 while (!list_empty(&priv
->cm
.passive_ids
)) {
922 p
= list_entry(priv
->cm
.passive_ids
.next
, typeof(*p
), list
);
923 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
924 p
->state
= IPOIB_CM_RX_ERROR
;
925 spin_unlock_irq(&priv
->lock
);
926 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
928 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
929 spin_lock_irq(&priv
->lock
);
932 /* Wait for all RX to be drained */
935 while (!list_empty(&priv
->cm
.rx_error_list
) ||
936 !list_empty(&priv
->cm
.rx_flush_list
) ||
937 !list_empty(&priv
->cm
.rx_drain_list
)) {
938 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
939 ipoib_warn(priv
, "RX drain timing out\n");
942 * assume the HW is wedged and just free up everything.
944 list_splice_init(&priv
->cm
.rx_flush_list
,
945 &priv
->cm
.rx_reap_list
);
946 list_splice_init(&priv
->cm
.rx_error_list
,
947 &priv
->cm
.rx_reap_list
);
948 list_splice_init(&priv
->cm
.rx_drain_list
,
949 &priv
->cm
.rx_reap_list
);
952 spin_unlock_irq(&priv
->lock
);
955 spin_lock_irq(&priv
->lock
);
958 spin_unlock_irq(&priv
->lock
);
960 ipoib_cm_free_rx_reap_list(dev
);
962 cancel_delayed_work(&priv
->cm
.stale_task
);
965 static int ipoib_cm_rep_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
967 struct ipoib_cm_tx
*p
= cm_id
->context
;
968 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
969 struct ipoib_cm_data
*data
= event
->private_data
;
970 struct sk_buff_head skqueue
;
971 struct ib_qp_attr qp_attr
;
972 int qp_attr_mask
, ret
;
975 p
->mtu
= be32_to_cpu(data
->mtu
);
977 if (p
->mtu
<= IPOIB_ENCAP_LEN
) {
978 ipoib_warn(priv
, "Rejecting connection: mtu %d <= %d\n",
979 p
->mtu
, IPOIB_ENCAP_LEN
);
983 qp_attr
.qp_state
= IB_QPS_RTR
;
984 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
986 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
990 qp_attr
.rq_psn
= 0 /* FIXME */;
991 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
993 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
997 qp_attr
.qp_state
= IB_QPS_RTS
;
998 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
1000 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
1003 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
1005 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
1009 skb_queue_head_init(&skqueue
);
1011 spin_lock_irq(&priv
->lock
);
1012 set_bit(IPOIB_FLAG_OPER_UP
, &p
->flags
);
1014 while ((skb
= __skb_dequeue(&p
->neigh
->queue
)))
1015 __skb_queue_tail(&skqueue
, skb
);
1016 spin_unlock_irq(&priv
->lock
);
1018 while ((skb
= __skb_dequeue(&skqueue
))) {
1020 if (dev_queue_xmit(skb
))
1021 ipoib_warn(priv
, "dev_queue_xmit failed "
1022 "to requeue packet\n");
1025 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1027 ipoib_warn(priv
, "failed to send RTU: %d\n", ret
);
1033 static struct ib_qp
*ipoib_cm_create_tx_qp(struct net_device
*dev
, struct ipoib_cm_tx
*tx
)
1035 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1036 struct ib_qp_init_attr attr
= {
1037 .send_cq
= priv
->recv_cq
,
1038 .recv_cq
= priv
->recv_cq
,
1039 .srq
= priv
->cm
.srq
,
1040 .cap
.max_send_wr
= ipoib_sendq_size
,
1041 .cap
.max_send_sge
= 1,
1042 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
1043 .qp_type
= IB_QPT_RC
,
1045 .create_flags
= IB_QP_CREATE_USE_GFP_NOIO
1048 struct ib_qp
*tx_qp
;
1050 if (dev
->features
& NETIF_F_SG
)
1051 attr
.cap
.max_send_sge
=
1052 min_t(u32
, priv
->ca
->attrs
.max_sge
, MAX_SKB_FRAGS
+ 1);
1054 tx_qp
= ib_create_qp(priv
->pd
, &attr
);
1055 if (PTR_ERR(tx_qp
) == -EINVAL
) {
1056 ipoib_warn(priv
, "can't use GFP_NOIO for QPs on device %s, using GFP_KERNEL\n",
1058 attr
.create_flags
&= ~IB_QP_CREATE_USE_GFP_NOIO
;
1059 tx_qp
= ib_create_qp(priv
->pd
, &attr
);
1061 tx
->max_send_sge
= attr
.cap
.max_send_sge
;
1065 static int ipoib_cm_send_req(struct net_device
*dev
,
1066 struct ib_cm_id
*id
, struct ib_qp
*qp
,
1068 struct ib_sa_path_rec
*pathrec
)
1070 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1071 struct ipoib_cm_data data
= {};
1072 struct ib_cm_req_param req
= {};
1074 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
1075 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
1077 req
.primary_path
= pathrec
;
1078 req
.alternate_path
= NULL
;
1079 req
.service_id
= cpu_to_be64(IPOIB_CM_IETF_ID
| qpn
);
1080 req
.qp_num
= qp
->qp_num
;
1081 req
.qp_type
= qp
->qp_type
;
1082 req
.private_data
= &data
;
1083 req
.private_data_len
= sizeof data
;
1084 req
.flow_control
= 0;
1086 req
.starting_psn
= 0; /* FIXME */
1089 * Pick some arbitrary defaults here; we could make these
1090 * module parameters if anyone cared about setting them.
1092 req
.responder_resources
= 4;
1093 req
.remote_cm_response_timeout
= 20;
1094 req
.local_cm_response_timeout
= 20;
1095 req
.retry_count
= 0; /* RFC draft warns against retries */
1096 req
.rnr_retry_count
= 0; /* RFC draft warns against retries */
1097 req
.max_cm_retries
= 15;
1098 req
.srq
= ipoib_cm_has_srq(dev
);
1099 return ib_send_cm_req(id
, &req
);
1102 static int ipoib_cm_modify_tx_init(struct net_device
*dev
,
1103 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
)
1105 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1106 struct ib_qp_attr qp_attr
;
1107 int qp_attr_mask
, ret
;
1108 ret
= ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &qp_attr
.pkey_index
);
1110 ipoib_warn(priv
, "pkey 0x%x not found: %d\n", priv
->pkey
, ret
);
1114 qp_attr
.qp_state
= IB_QPS_INIT
;
1115 qp_attr
.qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
1116 qp_attr
.port_num
= priv
->port
;
1117 qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1119 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
1121 ipoib_warn(priv
, "failed to modify tx QP to INIT: %d\n", ret
);
1127 static int ipoib_cm_tx_init(struct ipoib_cm_tx
*p
, u32 qpn
,
1128 struct ib_sa_path_rec
*pathrec
)
1130 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
1133 p
->tx_ring
= __vmalloc(ipoib_sendq_size
* sizeof *p
->tx_ring
,
1134 GFP_NOIO
, PAGE_KERNEL
);
1136 ipoib_warn(priv
, "failed to allocate tx ring\n");
1140 memset(p
->tx_ring
, 0, ipoib_sendq_size
* sizeof *p
->tx_ring
);
1142 p
->qp
= ipoib_cm_create_tx_qp(p
->dev
, p
);
1143 if (IS_ERR(p
->qp
)) {
1144 ret
= PTR_ERR(p
->qp
);
1145 ipoib_warn(priv
, "failed to allocate tx qp: %d\n", ret
);
1149 p
->id
= ib_create_cm_id(priv
->ca
, ipoib_cm_tx_handler
, p
);
1150 if (IS_ERR(p
->id
)) {
1151 ret
= PTR_ERR(p
->id
);
1152 ipoib_warn(priv
, "failed to create tx cm id: %d\n", ret
);
1156 ret
= ipoib_cm_modify_tx_init(p
->dev
, p
->id
, p
->qp
);
1158 ipoib_warn(priv
, "failed to modify tx qp to rtr: %d\n", ret
);
1162 ret
= ipoib_cm_send_req(p
->dev
, p
->id
, p
->qp
, qpn
, pathrec
);
1164 ipoib_warn(priv
, "failed to send cm req: %d\n", ret
);
1168 ipoib_dbg(priv
, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1169 p
->qp
->qp_num
, pathrec
->dgid
.raw
, qpn
);
1175 ib_destroy_cm_id(p
->id
);
1178 ib_destroy_qp(p
->qp
);
1186 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx
*p
)
1188 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
1189 struct ipoib_tx_buf
*tx_req
;
1190 unsigned long begin
;
1192 ipoib_dbg(priv
, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1193 p
->qp
? p
->qp
->qp_num
: 0, p
->tx_head
, p
->tx_tail
);
1196 ib_destroy_cm_id(p
->id
);
1199 /* Wait for all sends to complete */
1201 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1202 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
1203 ipoib_warn(priv
, "timing out; %d sends not completed\n",
1204 p
->tx_head
- p
->tx_tail
);
1214 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1215 tx_req
= &p
->tx_ring
[p
->tx_tail
& (ipoib_sendq_size
- 1)];
1216 ipoib_dma_unmap_tx(priv
, tx_req
);
1217 dev_kfree_skb_any(tx_req
->skb
);
1219 netif_tx_lock_bh(p
->dev
);
1220 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
1221 netif_queue_stopped(p
->dev
) &&
1222 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
1223 netif_wake_queue(p
->dev
);
1224 netif_tx_unlock_bh(p
->dev
);
1228 ib_destroy_qp(p
->qp
);
1234 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
1235 struct ib_cm_event
*event
)
1237 struct ipoib_cm_tx
*tx
= cm_id
->context
;
1238 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
1239 struct net_device
*dev
= priv
->dev
;
1240 struct ipoib_neigh
*neigh
;
1241 unsigned long flags
;
1244 switch (event
->event
) {
1245 case IB_CM_DREQ_RECEIVED
:
1246 ipoib_dbg(priv
, "DREQ received.\n");
1247 ib_send_cm_drep(cm_id
, NULL
, 0);
1249 case IB_CM_REP_RECEIVED
:
1250 ipoib_dbg(priv
, "REP received.\n");
1251 ret
= ipoib_cm_rep_handler(cm_id
, event
);
1253 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
1256 case IB_CM_REQ_ERROR
:
1257 case IB_CM_REJ_RECEIVED
:
1258 case IB_CM_TIMEWAIT_EXIT
:
1259 ipoib_dbg(priv
, "CM error %d.\n", event
->event
);
1260 netif_tx_lock_bh(dev
);
1261 spin_lock_irqsave(&priv
->lock
, flags
);
1266 ipoib_neigh_free(neigh
);
1271 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1272 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1273 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
1276 spin_unlock_irqrestore(&priv
->lock
, flags
);
1277 netif_tx_unlock_bh(dev
);
1286 struct ipoib_cm_tx
*ipoib_cm_create_tx(struct net_device
*dev
, struct ipoib_path
*path
,
1287 struct ipoib_neigh
*neigh
)
1289 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1290 struct ipoib_cm_tx
*tx
;
1292 tx
= kzalloc(sizeof *tx
, GFP_ATOMIC
);
1300 list_add(&tx
->list
, &priv
->cm
.start_list
);
1301 set_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
);
1302 queue_work(priv
->wq
, &priv
->cm
.start_task
);
1306 void ipoib_cm_destroy_tx(struct ipoib_cm_tx
*tx
)
1308 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
1309 unsigned long flags
;
1310 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1311 spin_lock_irqsave(&priv
->lock
, flags
);
1312 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1313 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
1314 ipoib_dbg(priv
, "Reap connection for gid %pI6\n",
1315 tx
->neigh
->daddr
+ 4);
1317 spin_unlock_irqrestore(&priv
->lock
, flags
);
1321 static void ipoib_cm_tx_start(struct work_struct
*work
)
1323 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1325 struct net_device
*dev
= priv
->dev
;
1326 struct ipoib_neigh
*neigh
;
1327 struct ipoib_cm_tx
*p
;
1328 unsigned long flags
;
1331 struct ib_sa_path_rec pathrec
;
1334 netif_tx_lock_bh(dev
);
1335 spin_lock_irqsave(&priv
->lock
, flags
);
1337 while (!list_empty(&priv
->cm
.start_list
)) {
1338 p
= list_entry(priv
->cm
.start_list
.next
, typeof(*p
), list
);
1339 list_del_init(&p
->list
);
1341 qpn
= IPOIB_QPN(neigh
->daddr
);
1342 memcpy(&pathrec
, &p
->path
->pathrec
, sizeof pathrec
);
1344 spin_unlock_irqrestore(&priv
->lock
, flags
);
1345 netif_tx_unlock_bh(dev
);
1347 ret
= ipoib_cm_tx_init(p
, qpn
, &pathrec
);
1349 netif_tx_lock_bh(dev
);
1350 spin_lock_irqsave(&priv
->lock
, flags
);
1356 ipoib_neigh_free(neigh
);
1363 spin_unlock_irqrestore(&priv
->lock
, flags
);
1364 netif_tx_unlock_bh(dev
);
1367 static void ipoib_cm_tx_reap(struct work_struct
*work
)
1369 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1371 struct net_device
*dev
= priv
->dev
;
1372 struct ipoib_cm_tx
*p
;
1373 unsigned long flags
;
1375 netif_tx_lock_bh(dev
);
1376 spin_lock_irqsave(&priv
->lock
, flags
);
1378 while (!list_empty(&priv
->cm
.reap_list
)) {
1379 p
= list_entry(priv
->cm
.reap_list
.next
, typeof(*p
), list
);
1381 spin_unlock_irqrestore(&priv
->lock
, flags
);
1382 netif_tx_unlock_bh(dev
);
1383 ipoib_cm_tx_destroy(p
);
1384 netif_tx_lock_bh(dev
);
1385 spin_lock_irqsave(&priv
->lock
, flags
);
1388 spin_unlock_irqrestore(&priv
->lock
, flags
);
1389 netif_tx_unlock_bh(dev
);
1392 static void ipoib_cm_skb_reap(struct work_struct
*work
)
1394 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1396 struct net_device
*dev
= priv
->dev
;
1397 struct sk_buff
*skb
;
1398 unsigned long flags
;
1399 unsigned mtu
= priv
->mcast_mtu
;
1401 netif_tx_lock_bh(dev
);
1402 spin_lock_irqsave(&priv
->lock
, flags
);
1404 while ((skb
= skb_dequeue(&priv
->cm
.skb_queue
))) {
1405 spin_unlock_irqrestore(&priv
->lock
, flags
);
1406 netif_tx_unlock_bh(dev
);
1408 if (skb
->protocol
== htons(ETH_P_IP
))
1409 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1410 #if IS_ENABLED(CONFIG_IPV6)
1411 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1412 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1414 dev_kfree_skb_any(skb
);
1416 netif_tx_lock_bh(dev
);
1417 spin_lock_irqsave(&priv
->lock
, flags
);
1420 spin_unlock_irqrestore(&priv
->lock
, flags
);
1421 netif_tx_unlock_bh(dev
);
1424 void ipoib_cm_skb_too_long(struct net_device
*dev
, struct sk_buff
*skb
,
1427 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1428 int e
= skb_queue_empty(&priv
->cm
.skb_queue
);
1431 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
1433 skb_queue_tail(&priv
->cm
.skb_queue
, skb
);
1435 queue_work(priv
->wq
, &priv
->cm
.skb_task
);
1438 static void ipoib_cm_rx_reap(struct work_struct
*work
)
1440 ipoib_cm_free_rx_reap_list(container_of(work
, struct ipoib_dev_priv
,
1441 cm
.rx_reap_task
)->dev
);
1444 static void ipoib_cm_stale_task(struct work_struct
*work
)
1446 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1447 cm
.stale_task
.work
);
1448 struct ipoib_cm_rx
*p
;
1451 spin_lock_irq(&priv
->lock
);
1452 while (!list_empty(&priv
->cm
.passive_ids
)) {
1453 /* List is sorted by LRU, start from tail,
1454 * stop when we see a recently used entry */
1455 p
= list_entry(priv
->cm
.passive_ids
.prev
, typeof(*p
), list
);
1456 if (time_before_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_TIMEOUT
))
1458 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
1459 p
->state
= IPOIB_CM_RX_ERROR
;
1460 spin_unlock_irq(&priv
->lock
);
1461 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
1463 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
1464 spin_lock_irq(&priv
->lock
);
1467 if (!list_empty(&priv
->cm
.passive_ids
))
1468 queue_delayed_work(priv
->wq
,
1469 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
1470 spin_unlock_irq(&priv
->lock
);
1473 static ssize_t
show_mode(struct device
*d
, struct device_attribute
*attr
,
1476 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(d
));
1478 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
1479 return sprintf(buf
, "connected\n");
1481 return sprintf(buf
, "datagram\n");
1484 static ssize_t
set_mode(struct device
*d
, struct device_attribute
*attr
,
1485 const char *buf
, size_t count
)
1487 struct net_device
*dev
= to_net_dev(d
);
1490 if (!rtnl_trylock())
1491 return restart_syscall();
1493 ret
= ipoib_set_mode(dev
, buf
);
1503 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
, show_mode
, set_mode
);
1505 int ipoib_cm_add_mode_attr(struct net_device
*dev
)
1507 return device_create_file(&dev
->dev
, &dev_attr_mode
);
1510 static void ipoib_cm_create_srq(struct net_device
*dev
, int max_sge
)
1512 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1513 struct ib_srq_init_attr srq_init_attr
= {
1514 .srq_type
= IB_SRQT_BASIC
,
1516 .max_wr
= ipoib_recvq_size
,
1521 priv
->cm
.srq
= ib_create_srq(priv
->pd
, &srq_init_attr
);
1522 if (IS_ERR(priv
->cm
.srq
)) {
1523 if (PTR_ERR(priv
->cm
.srq
) != -ENOSYS
)
1524 printk(KERN_WARNING
"%s: failed to allocate SRQ, error %ld\n",
1525 priv
->ca
->name
, PTR_ERR(priv
->cm
.srq
));
1526 priv
->cm
.srq
= NULL
;
1530 priv
->cm
.srq_ring
= vzalloc(ipoib_recvq_size
* sizeof *priv
->cm
.srq_ring
);
1531 if (!priv
->cm
.srq_ring
) {
1532 printk(KERN_WARNING
"%s: failed to allocate CM SRQ ring (%d entries)\n",
1533 priv
->ca
->name
, ipoib_recvq_size
);
1534 ib_destroy_srq(priv
->cm
.srq
);
1535 priv
->cm
.srq
= NULL
;
1541 int ipoib_cm_dev_init(struct net_device
*dev
)
1543 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1546 INIT_LIST_HEAD(&priv
->cm
.passive_ids
);
1547 INIT_LIST_HEAD(&priv
->cm
.reap_list
);
1548 INIT_LIST_HEAD(&priv
->cm
.start_list
);
1549 INIT_LIST_HEAD(&priv
->cm
.rx_error_list
);
1550 INIT_LIST_HEAD(&priv
->cm
.rx_flush_list
);
1551 INIT_LIST_HEAD(&priv
->cm
.rx_drain_list
);
1552 INIT_LIST_HEAD(&priv
->cm
.rx_reap_list
);
1553 INIT_WORK(&priv
->cm
.start_task
, ipoib_cm_tx_start
);
1554 INIT_WORK(&priv
->cm
.reap_task
, ipoib_cm_tx_reap
);
1555 INIT_WORK(&priv
->cm
.skb_task
, ipoib_cm_skb_reap
);
1556 INIT_WORK(&priv
->cm
.rx_reap_task
, ipoib_cm_rx_reap
);
1557 INIT_DELAYED_WORK(&priv
->cm
.stale_task
, ipoib_cm_stale_task
);
1559 skb_queue_head_init(&priv
->cm
.skb_queue
);
1561 ipoib_dbg(priv
, "max_srq_sge=%d\n", priv
->ca
->attrs
.max_srq_sge
);
1563 max_srq_sge
= min_t(int, IPOIB_CM_RX_SG
, priv
->ca
->attrs
.max_srq_sge
);
1564 ipoib_cm_create_srq(dev
, max_srq_sge
);
1565 if (ipoib_cm_has_srq(dev
)) {
1566 priv
->cm
.max_cm_mtu
= max_srq_sge
* PAGE_SIZE
- 0x10;
1567 priv
->cm
.num_frags
= max_srq_sge
;
1568 ipoib_dbg(priv
, "max_cm_mtu = 0x%x, num_frags=%d\n",
1569 priv
->cm
.max_cm_mtu
, priv
->cm
.num_frags
);
1571 priv
->cm
.max_cm_mtu
= IPOIB_CM_MTU
;
1572 priv
->cm
.num_frags
= IPOIB_CM_RX_SG
;
1575 ipoib_cm_init_rx_wr(dev
, &priv
->cm
.rx_wr
, priv
->cm
.rx_sge
);
1577 if (ipoib_cm_has_srq(dev
)) {
1578 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
1579 if (!ipoib_cm_alloc_rx_skb(dev
, priv
->cm
.srq_ring
, i
,
1580 priv
->cm
.num_frags
- 1,
1581 priv
->cm
.srq_ring
[i
].mapping
,
1583 ipoib_warn(priv
, "failed to allocate "
1584 "receive buffer %d\n", i
);
1585 ipoib_cm_dev_cleanup(dev
);
1589 if (ipoib_cm_post_receive_srq(dev
, i
)) {
1590 ipoib_warn(priv
, "ipoib_cm_post_receive_srq "
1591 "failed for buf %d\n", i
);
1592 ipoib_cm_dev_cleanup(dev
);
1598 priv
->dev
->dev_addr
[0] = IPOIB_FLAGS_RC
;
1602 void ipoib_cm_dev_cleanup(struct net_device
*dev
)
1604 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1610 ipoib_dbg(priv
, "Cleanup ipoib connected mode.\n");
1612 ret
= ib_destroy_srq(priv
->cm
.srq
);
1614 ipoib_warn(priv
, "ib_destroy_srq failed: %d\n", ret
);
1616 priv
->cm
.srq
= NULL
;
1617 if (!priv
->cm
.srq_ring
)
1620 ipoib_cm_free_rx_ring(dev
, priv
->cm
.srq_ring
);
1621 priv
->cm
.srq_ring
= NULL
;