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>
41 #include <linux/sched/signal.h>
42 #include <linux/sched/mm.h>
46 int ipoib_max_conn_qp
= 128;
48 module_param_named(max_nonsrq_conn_qp
, ipoib_max_conn_qp
, int, 0444);
49 MODULE_PARM_DESC(max_nonsrq_conn_qp
,
50 "Max number of connected-mode QPs per interface "
51 "(applied only if shared receive queue is not available)");
53 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
54 static int data_debug_level
;
56 module_param_named(cm_data_debug_level
, data_debug_level
, int, 0644);
57 MODULE_PARM_DESC(cm_data_debug_level
,
58 "Enable data path debug tracing for connected mode if > 0");
61 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
63 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
64 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
65 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
66 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
68 #define IPOIB_CM_RX_RESERVE (ALIGN(IPOIB_HARD_LEN, 16) - IPOIB_ENCAP_LEN)
70 static struct ib_qp_attr ipoib_cm_err_attr
= {
71 .qp_state
= IB_QPS_ERR
74 #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
76 static struct ib_send_wr ipoib_cm_rx_drain_wr
= {
80 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
81 const struct ib_cm_event
*event
);
83 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv
*priv
, int frags
,
84 u64 mapping
[IPOIB_CM_RX_SG
])
88 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_CM_HEAD_SIZE
, DMA_FROM_DEVICE
);
90 for (i
= 0; i
< frags
; ++i
)
91 ib_dma_unmap_page(priv
->ca
, mapping
[i
+ 1], PAGE_SIZE
, DMA_FROM_DEVICE
);
94 static int ipoib_cm_post_receive_srq(struct net_device
*dev
, int id
)
96 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
99 priv
->cm
.rx_wr
.wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
101 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
102 priv
->cm
.rx_sge
[i
].addr
= priv
->cm
.srq_ring
[id
].mapping
[i
];
104 ret
= ib_post_srq_recv(priv
->cm
.srq
, &priv
->cm
.rx_wr
, NULL
);
106 ipoib_warn(priv
, "post srq failed for buf %d (%d)\n", id
, ret
);
107 ipoib_cm_dma_unmap_rx(priv
, priv
->cm
.num_frags
- 1,
108 priv
->cm
.srq_ring
[id
].mapping
);
109 dev_kfree_skb_any(priv
->cm
.srq_ring
[id
].skb
);
110 priv
->cm
.srq_ring
[id
].skb
= NULL
;
116 static int ipoib_cm_post_receive_nonsrq(struct net_device
*dev
,
117 struct ipoib_cm_rx
*rx
,
118 struct ib_recv_wr
*wr
,
119 struct ib_sge
*sge
, int id
)
121 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
124 wr
->wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
126 for (i
= 0; i
< IPOIB_CM_RX_SG
; ++i
)
127 sge
[i
].addr
= rx
->rx_ring
[id
].mapping
[i
];
129 ret
= ib_post_recv(rx
->qp
, wr
, NULL
);
131 ipoib_warn(priv
, "post recv failed for buf %d (%d)\n", id
, ret
);
132 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
133 rx
->rx_ring
[id
].mapping
);
134 dev_kfree_skb_any(rx
->rx_ring
[id
].skb
);
135 rx
->rx_ring
[id
].skb
= NULL
;
141 static struct sk_buff
*ipoib_cm_alloc_rx_skb(struct net_device
*dev
,
142 struct ipoib_cm_rx_buf
*rx_ring
,
144 u64 mapping
[IPOIB_CM_RX_SG
],
147 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
151 skb
= dev_alloc_skb(ALIGN(IPOIB_CM_HEAD_SIZE
+ IPOIB_PSEUDO_LEN
, 16));
156 * IPoIB adds a IPOIB_ENCAP_LEN byte header, this will align the
157 * IP header to a multiple of 16.
159 skb_reserve(skb
, IPOIB_CM_RX_RESERVE
);
161 mapping
[0] = ib_dma_map_single(priv
->ca
, skb
->data
, IPOIB_CM_HEAD_SIZE
,
163 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[0]))) {
164 dev_kfree_skb_any(skb
);
168 for (i
= 0; i
< frags
; i
++) {
169 struct page
*page
= alloc_page(gfp
);
173 skb_fill_page_desc(skb
, i
, page
, 0, PAGE_SIZE
);
175 mapping
[i
+ 1] = ib_dma_map_page(priv
->ca
, page
,
176 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
177 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[i
+ 1])))
181 rx_ring
[id
].skb
= skb
;
186 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_CM_HEAD_SIZE
, DMA_FROM_DEVICE
);
189 ib_dma_unmap_page(priv
->ca
, mapping
[i
], PAGE_SIZE
, DMA_FROM_DEVICE
);
191 dev_kfree_skb_any(skb
);
195 static void ipoib_cm_free_rx_ring(struct net_device
*dev
,
196 struct ipoib_cm_rx_buf
*rx_ring
)
198 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
201 for (i
= 0; i
< ipoib_recvq_size
; ++i
)
202 if (rx_ring
[i
].skb
) {
203 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
205 dev_kfree_skb_any(rx_ring
[i
].skb
);
211 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv
*priv
)
213 struct ipoib_cm_rx
*p
;
215 /* We only reserved 1 extra slot in CQ for drain WRs, so
216 * make sure we have at most 1 outstanding WR. */
217 if (list_empty(&priv
->cm
.rx_flush_list
) ||
218 !list_empty(&priv
->cm
.rx_drain_list
))
222 * QPs on flush list are error state. This way, a "flush
223 * error" WC will be immediately generated for each WR we post.
225 p
= list_entry(priv
->cm
.rx_flush_list
.next
, typeof(*p
), list
);
226 ipoib_cm_rx_drain_wr
.wr_id
= IPOIB_CM_RX_DRAIN_WRID
;
227 if (ib_post_send(p
->qp
, &ipoib_cm_rx_drain_wr
, NULL
))
228 ipoib_warn(priv
, "failed to post drain wr\n");
230 list_splice_init(&priv
->cm
.rx_flush_list
, &priv
->cm
.rx_drain_list
);
233 static void ipoib_cm_rx_event_handler(struct ib_event
*event
, void *ctx
)
235 struct ipoib_cm_rx
*p
= ctx
;
236 struct ipoib_dev_priv
*priv
= ipoib_priv(p
->dev
);
239 if (event
->event
!= IB_EVENT_QP_LAST_WQE_REACHED
)
242 spin_lock_irqsave(&priv
->lock
, flags
);
243 list_move(&p
->list
, &priv
->cm
.rx_flush_list
);
244 p
->state
= IPOIB_CM_RX_FLUSH
;
245 ipoib_cm_start_rx_drain(priv
);
246 spin_unlock_irqrestore(&priv
->lock
, flags
);
249 static struct ib_qp
*ipoib_cm_create_rx_qp(struct net_device
*dev
,
250 struct ipoib_cm_rx
*p
)
252 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
253 struct ib_qp_init_attr attr
= {
254 .event_handler
= ipoib_cm_rx_event_handler
,
255 .send_cq
= priv
->recv_cq
, /* For drain WR */
256 .recv_cq
= priv
->recv_cq
,
258 .cap
.max_send_wr
= 1, /* For drain WR */
259 .cap
.max_send_sge
= 1, /* FIXME: 0 Seems not to work */
260 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
261 .qp_type
= IB_QPT_RC
,
265 if (!ipoib_cm_has_srq(dev
)) {
266 attr
.cap
.max_recv_wr
= ipoib_recvq_size
;
267 attr
.cap
.max_recv_sge
= IPOIB_CM_RX_SG
;
270 return ib_create_qp(priv
->pd
, &attr
);
273 static int ipoib_cm_modify_rx_qp(struct net_device
*dev
,
274 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
,
277 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
278 struct ib_qp_attr qp_attr
;
279 int qp_attr_mask
, ret
;
281 qp_attr
.qp_state
= IB_QPS_INIT
;
282 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
284 ipoib_warn(priv
, "failed to init QP attr for INIT: %d\n", ret
);
287 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
289 ipoib_warn(priv
, "failed to modify QP to INIT: %d\n", ret
);
292 qp_attr
.qp_state
= IB_QPS_RTR
;
293 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
295 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
298 qp_attr
.rq_psn
= psn
;
299 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
301 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
306 * Current Mellanox HCA firmware won't generate completions
307 * with error for drain WRs unless the QP has been moved to
308 * RTS first. This work-around leaves a window where a QP has
309 * moved to error asynchronously, but this will eventually get
310 * fixed in firmware, so let's not error out if modify QP
313 qp_attr
.qp_state
= IB_QPS_RTS
;
314 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
316 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
319 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
321 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
328 static void ipoib_cm_init_rx_wr(struct net_device
*dev
,
329 struct ib_recv_wr
*wr
,
332 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
335 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
336 sge
[i
].lkey
= priv
->pd
->local_dma_lkey
;
338 sge
[0].length
= IPOIB_CM_HEAD_SIZE
;
339 for (i
= 1; i
< priv
->cm
.num_frags
; ++i
)
340 sge
[i
].length
= PAGE_SIZE
;
344 wr
->num_sge
= priv
->cm
.num_frags
;
347 static int ipoib_cm_nonsrq_init_rx(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
348 struct ipoib_cm_rx
*rx
)
350 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
352 struct ib_recv_wr wr
;
353 struct ib_sge sge
[IPOIB_CM_RX_SG
];
358 rx
->rx_ring
= vzalloc(array_size(ipoib_recvq_size
,
359 sizeof(*rx
->rx_ring
)));
363 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
369 ipoib_cm_init_rx_wr(dev
, &t
->wr
, t
->sge
);
371 spin_lock_irq(&priv
->lock
);
373 if (priv
->cm
.nonsrq_conn_qp
>= ipoib_max_conn_qp
) {
374 spin_unlock_irq(&priv
->lock
);
375 ib_send_cm_rej(cm_id
, IB_CM_REJ_NO_QP
, NULL
, 0, NULL
, 0);
379 ++priv
->cm
.nonsrq_conn_qp
;
381 spin_unlock_irq(&priv
->lock
);
383 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
384 if (!ipoib_cm_alloc_rx_skb(dev
, rx
->rx_ring
, i
, IPOIB_CM_RX_SG
- 1,
385 rx
->rx_ring
[i
].mapping
,
387 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
391 ret
= ipoib_cm_post_receive_nonsrq(dev
, rx
, &t
->wr
, t
->sge
, i
);
393 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq "
394 "failed for buf %d\n", i
);
400 rx
->recv_count
= ipoib_recvq_size
;
407 spin_lock_irq(&priv
->lock
);
408 --priv
->cm
.nonsrq_conn_qp
;
409 spin_unlock_irq(&priv
->lock
);
415 ipoib_cm_free_rx_ring(dev
, rx
->rx_ring
);
420 static int ipoib_cm_send_rep(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
422 const struct ib_cm_req_event_param
*req
,
425 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
426 struct ipoib_cm_data data
= {};
427 struct ib_cm_rep_param rep
= {};
429 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
430 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
432 rep
.private_data
= &data
;
433 rep
.private_data_len
= sizeof(data
);
434 rep
.flow_control
= 0;
435 rep
.rnr_retry_count
= req
->rnr_retry_count
;
436 rep
.srq
= ipoib_cm_has_srq(dev
);
437 rep
.qp_num
= qp
->qp_num
;
438 rep
.starting_psn
= psn
;
439 return ib_send_cm_rep(cm_id
, &rep
);
442 static int ipoib_cm_req_handler(struct ib_cm_id
*cm_id
,
443 const struct ib_cm_event
*event
)
445 struct net_device
*dev
= cm_id
->context
;
446 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
447 struct ipoib_cm_rx
*p
;
451 ipoib_dbg(priv
, "REQ arrived\n");
452 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
458 p
->state
= IPOIB_CM_RX_LIVE
;
459 p
->jiffies
= jiffies
;
460 INIT_LIST_HEAD(&p
->list
);
462 p
->qp
= ipoib_cm_create_rx_qp(dev
, p
);
464 ret
= PTR_ERR(p
->qp
);
468 psn
= prandom_u32() & 0xffffff;
469 ret
= ipoib_cm_modify_rx_qp(dev
, cm_id
, p
->qp
, psn
);
473 if (!ipoib_cm_has_srq(dev
)) {
474 ret
= ipoib_cm_nonsrq_init_rx(dev
, cm_id
, p
);
479 spin_lock_irq(&priv
->lock
);
480 queue_delayed_work(priv
->wq
,
481 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
482 /* Add this entry to passive ids list head, but do not re-add it
483 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
484 p
->jiffies
= jiffies
;
485 if (p
->state
== IPOIB_CM_RX_LIVE
)
486 list_move(&p
->list
, &priv
->cm
.passive_ids
);
487 spin_unlock_irq(&priv
->lock
);
489 ret
= ipoib_cm_send_rep(dev
, cm_id
, p
->qp
, &event
->param
.req_rcvd
, psn
);
491 ipoib_warn(priv
, "failed to send REP: %d\n", ret
);
492 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
493 ipoib_warn(priv
, "unable to move qp to error state\n");
498 ib_destroy_qp(p
->qp
);
504 static int ipoib_cm_rx_handler(struct ib_cm_id
*cm_id
,
505 const struct ib_cm_event
*event
)
507 struct ipoib_cm_rx
*p
;
508 struct ipoib_dev_priv
*priv
;
510 switch (event
->event
) {
511 case IB_CM_REQ_RECEIVED
:
512 return ipoib_cm_req_handler(cm_id
, event
);
513 case IB_CM_DREQ_RECEIVED
:
514 ib_send_cm_drep(cm_id
, NULL
, 0);
516 case IB_CM_REJ_RECEIVED
:
518 priv
= ipoib_priv(p
->dev
);
519 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
520 ipoib_warn(priv
, "unable to move qp to error state\n");
526 /* Adjust length of skb with fragments to match received data */
527 static void skb_put_frags(struct sk_buff
*skb
, unsigned int hdr_space
,
528 unsigned int length
, struct sk_buff
*toskb
)
533 /* put header into skb */
534 size
= min(length
, hdr_space
);
539 num_frags
= skb_shinfo(skb
)->nr_frags
;
540 for (i
= 0; i
< num_frags
; i
++) {
541 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
544 /* don't need this page */
545 skb_fill_page_desc(toskb
, i
, skb_frag_page(frag
),
547 --skb_shinfo(skb
)->nr_frags
;
549 size
= min_t(unsigned int, length
, PAGE_SIZE
);
551 skb_frag_size_set(frag
, size
);
552 skb
->data_len
+= size
;
553 skb
->truesize
+= size
;
560 void ipoib_cm_handle_rx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
562 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
563 struct ipoib_cm_rx_buf
*rx_ring
;
564 unsigned int wr_id
= wc
->wr_id
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
);
565 struct sk_buff
*skb
, *newskb
;
566 struct ipoib_cm_rx
*p
;
568 u64 mapping
[IPOIB_CM_RX_SG
];
571 struct sk_buff
*small_skb
;
573 ipoib_dbg_data(priv
, "cm recv completion: id %d, status: %d\n",
576 if (unlikely(wr_id
>= ipoib_recvq_size
)) {
577 if (wr_id
== (IPOIB_CM_RX_DRAIN_WRID
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
))) {
578 spin_lock_irqsave(&priv
->lock
, flags
);
579 list_splice_init(&priv
->cm
.rx_drain_list
, &priv
->cm
.rx_reap_list
);
580 ipoib_cm_start_rx_drain(priv
);
581 queue_work(priv
->wq
, &priv
->cm
.rx_reap_task
);
582 spin_unlock_irqrestore(&priv
->lock
, flags
);
584 ipoib_warn(priv
, "cm recv completion event with wrid %d (> %d)\n",
585 wr_id
, ipoib_recvq_size
);
589 p
= wc
->qp
->qp_context
;
591 has_srq
= ipoib_cm_has_srq(dev
);
592 rx_ring
= has_srq
? priv
->cm
.srq_ring
: p
->rx_ring
;
594 skb
= rx_ring
[wr_id
].skb
;
596 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
598 "cm recv error (status=%d, wrid=%d vend_err %#x)\n",
599 wc
->status
, wr_id
, wc
->vendor_err
);
600 ++dev
->stats
.rx_dropped
;
604 if (!--p
->recv_count
) {
605 spin_lock_irqsave(&priv
->lock
, flags
);
606 list_move(&p
->list
, &priv
->cm
.rx_reap_list
);
607 spin_unlock_irqrestore(&priv
->lock
, flags
);
608 queue_work(priv
->wq
, &priv
->cm
.rx_reap_task
);
614 if (unlikely(!(wr_id
& IPOIB_CM_RX_UPDATE_MASK
))) {
615 if (p
&& time_after_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_UPDATE_TIME
)) {
616 spin_lock_irqsave(&priv
->lock
, flags
);
617 p
->jiffies
= jiffies
;
618 /* Move this entry to list head, but do not re-add it
619 * if it has been moved out of list. */
620 if (p
->state
== IPOIB_CM_RX_LIVE
)
621 list_move(&p
->list
, &priv
->cm
.passive_ids
);
622 spin_unlock_irqrestore(&priv
->lock
, flags
);
626 if (wc
->byte_len
< IPOIB_CM_COPYBREAK
) {
627 int dlen
= wc
->byte_len
;
629 small_skb
= dev_alloc_skb(dlen
+ IPOIB_CM_RX_RESERVE
);
631 skb_reserve(small_skb
, IPOIB_CM_RX_RESERVE
);
632 ib_dma_sync_single_for_cpu(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
633 dlen
, DMA_FROM_DEVICE
);
634 skb_copy_from_linear_data(skb
, small_skb
->data
, dlen
);
635 ib_dma_sync_single_for_device(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
636 dlen
, DMA_FROM_DEVICE
);
637 skb_put(small_skb
, dlen
);
643 frags
= PAGE_ALIGN(wc
->byte_len
-
644 min_t(u32
, wc
->byte_len
, IPOIB_CM_HEAD_SIZE
)) /
647 newskb
= ipoib_cm_alloc_rx_skb(dev
, rx_ring
, wr_id
, frags
,
648 mapping
, GFP_ATOMIC
);
649 if (unlikely(!newskb
)) {
651 * If we can't allocate a new RX buffer, dump
652 * this packet and reuse the old buffer.
654 ipoib_dbg(priv
, "failed to allocate receive buffer %d\n", wr_id
);
655 ++dev
->stats
.rx_dropped
;
659 ipoib_cm_dma_unmap_rx(priv
, frags
, rx_ring
[wr_id
].mapping
);
660 memcpy(rx_ring
[wr_id
].mapping
, mapping
, (frags
+ 1) * sizeof(*mapping
));
662 ipoib_dbg_data(priv
, "received %d bytes, SLID 0x%04x\n",
663 wc
->byte_len
, wc
->slid
);
665 skb_put_frags(skb
, IPOIB_CM_HEAD_SIZE
, wc
->byte_len
, newskb
);
668 skb
->protocol
= ((struct ipoib_header
*) skb
->data
)->proto
;
669 skb_add_pseudo_hdr(skb
);
671 ++dev
->stats
.rx_packets
;
672 dev
->stats
.rx_bytes
+= skb
->len
;
675 /* XXX get correct PACKET_ type here */
676 skb
->pkt_type
= PACKET_HOST
;
677 netif_receive_skb(skb
);
681 if (unlikely(ipoib_cm_post_receive_srq(dev
, wr_id
)))
682 ipoib_warn(priv
, "ipoib_cm_post_receive_srq failed "
683 "for buf %d\n", wr_id
);
685 if (unlikely(ipoib_cm_post_receive_nonsrq(dev
, p
,
690 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq failed "
691 "for buf %d\n", wr_id
);
696 static inline int post_send(struct ipoib_dev_priv
*priv
,
697 struct ipoib_cm_tx
*tx
,
699 struct ipoib_tx_buf
*tx_req
)
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
, NULL
);
708 void ipoib_cm_send(struct net_device
*dev
, struct sk_buff
*skb
, struct ipoib_cm_tx
*tx
)
710 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
711 struct ipoib_tx_buf
*tx_req
;
713 unsigned int 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
);
759 if ((priv
->global_tx_head
- priv
->global_tx_tail
) ==
760 ipoib_sendq_size
- 1) {
761 ipoib_dbg(priv
, "TX ring 0x%x full, stopping kernel net queue\n",
763 netif_stop_queue(dev
);
769 if (netif_queue_stopped(dev
)) {
770 rc
= ib_req_notify_cq(priv
->send_cq
, IB_CQ_NEXT_COMP
|
771 IB_CQ_REPORT_MISSED_EVENTS
);
772 if (unlikely(rc
< 0))
773 ipoib_warn(priv
, "IPoIB/CM:request notify on send CQ failed\n");
775 napi_schedule(&priv
->send_napi
);
778 rc
= post_send(priv
, tx
, tx
->tx_head
& (ipoib_sendq_size
- 1), tx_req
);
780 ipoib_warn(priv
, "IPoIB/CM:post_send failed, error %d\n", rc
);
781 ++dev
->stats
.tx_errors
;
782 ipoib_dma_unmap_tx(priv
, tx_req
);
783 dev_kfree_skb_any(skb
);
785 if (netif_queue_stopped(dev
))
786 netif_wake_queue(dev
);
788 netif_trans_update(dev
);
790 ++priv
->global_tx_head
;
794 void ipoib_cm_handle_tx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
796 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
797 struct ipoib_cm_tx
*tx
= wc
->qp
->qp_context
;
798 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_OP_CM
;
799 struct ipoib_tx_buf
*tx_req
;
802 ipoib_dbg_data(priv
, "cm send completion: id %d, status: %d\n",
805 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
806 ipoib_warn(priv
, "cm send completion event with wrid %d (> %d)\n",
807 wr_id
, ipoib_sendq_size
);
811 tx_req
= &tx
->tx_ring
[wr_id
];
813 ipoib_dma_unmap_tx(priv
, tx_req
);
815 /* FIXME: is this right? Shouldn't we only increment on success? */
816 ++dev
->stats
.tx_packets
;
817 dev
->stats
.tx_bytes
+= tx_req
->skb
->len
;
819 dev_kfree_skb_any(tx_req
->skb
);
824 ++priv
->global_tx_tail
;
826 if (unlikely(netif_queue_stopped(dev
) &&
827 ((priv
->global_tx_head
- priv
->global_tx_tail
) <=
828 ipoib_sendq_size
>> 1) &&
829 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
)))
830 netif_wake_queue(dev
);
832 if (wc
->status
!= IB_WC_SUCCESS
&&
833 wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
834 struct ipoib_neigh
*neigh
;
836 /* IB_WC[_RNR]_RETRY_EXC_ERR error is part of the life cycle,
837 * so don't make waves.
839 if (wc
->status
== IB_WC_RNR_RETRY_EXC_ERR
||
840 wc
->status
== IB_WC_RETRY_EXC_ERR
)
842 "%s: failed cm send event (status=%d, wrid=%d vend_err %#x)\n",
843 __func__
, wc
->status
, wr_id
, wc
->vendor_err
);
846 "%s: failed cm send event (status=%d, wrid=%d vend_err %#x)\n",
847 __func__
, wc
->status
, wr_id
, wc
->vendor_err
);
849 spin_lock_irqsave(&priv
->lock
, flags
);
854 ipoib_neigh_free(neigh
);
859 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
860 list_move(&tx
->list
, &priv
->cm
.reap_list
);
861 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
864 clear_bit(IPOIB_FLAG_OPER_UP
, &tx
->flags
);
866 spin_unlock_irqrestore(&priv
->lock
, flags
);
869 netif_tx_unlock(dev
);
872 int ipoib_cm_dev_open(struct net_device
*dev
)
874 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
877 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
880 priv
->cm
.id
= ib_create_cm_id(priv
->ca
, ipoib_cm_rx_handler
, dev
);
881 if (IS_ERR(priv
->cm
.id
)) {
882 pr_warn("%s: failed to create CM ID\n", priv
->ca
->name
);
883 ret
= PTR_ERR(priv
->cm
.id
);
887 ret
= ib_cm_listen(priv
->cm
.id
, cpu_to_be64(IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
),
890 pr_warn("%s: failed to listen on ID 0x%llx\n", priv
->ca
->name
,
891 IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
);
898 ib_destroy_cm_id(priv
->cm
.id
);
904 static void ipoib_cm_free_rx_reap_list(struct net_device
*dev
)
906 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
907 struct ipoib_cm_rx
*rx
, *n
;
910 spin_lock_irq(&priv
->lock
);
911 list_splice_init(&priv
->cm
.rx_reap_list
, &list
);
912 spin_unlock_irq(&priv
->lock
);
914 list_for_each_entry_safe(rx
, n
, &list
, list
) {
915 ib_destroy_cm_id(rx
->id
);
916 ib_destroy_qp(rx
->qp
);
917 if (!ipoib_cm_has_srq(dev
)) {
918 ipoib_cm_free_rx_ring(priv
->dev
, rx
->rx_ring
);
919 spin_lock_irq(&priv
->lock
);
920 --priv
->cm
.nonsrq_conn_qp
;
921 spin_unlock_irq(&priv
->lock
);
927 void ipoib_cm_dev_stop(struct net_device
*dev
)
929 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
930 struct ipoib_cm_rx
*p
;
934 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
) || !priv
->cm
.id
)
937 ib_destroy_cm_id(priv
->cm
.id
);
940 spin_lock_irq(&priv
->lock
);
941 while (!list_empty(&priv
->cm
.passive_ids
)) {
942 p
= list_entry(priv
->cm
.passive_ids
.next
, typeof(*p
), list
);
943 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
944 p
->state
= IPOIB_CM_RX_ERROR
;
945 spin_unlock_irq(&priv
->lock
);
946 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
948 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
949 spin_lock_irq(&priv
->lock
);
952 /* Wait for all RX to be drained */
955 while (!list_empty(&priv
->cm
.rx_error_list
) ||
956 !list_empty(&priv
->cm
.rx_flush_list
) ||
957 !list_empty(&priv
->cm
.rx_drain_list
)) {
958 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
959 ipoib_warn(priv
, "RX drain timing out\n");
962 * assume the HW is wedged and just free up everything.
964 list_splice_init(&priv
->cm
.rx_flush_list
,
965 &priv
->cm
.rx_reap_list
);
966 list_splice_init(&priv
->cm
.rx_error_list
,
967 &priv
->cm
.rx_reap_list
);
968 list_splice_init(&priv
->cm
.rx_drain_list
,
969 &priv
->cm
.rx_reap_list
);
972 spin_unlock_irq(&priv
->lock
);
973 usleep_range(1000, 2000);
975 spin_lock_irq(&priv
->lock
);
978 spin_unlock_irq(&priv
->lock
);
980 ipoib_cm_free_rx_reap_list(dev
);
982 cancel_delayed_work(&priv
->cm
.stale_task
);
985 static int ipoib_cm_rep_handler(struct ib_cm_id
*cm_id
,
986 const struct ib_cm_event
*event
)
988 struct ipoib_cm_tx
*p
= cm_id
->context
;
989 struct ipoib_dev_priv
*priv
= ipoib_priv(p
->dev
);
990 struct ipoib_cm_data
*data
= event
->private_data
;
991 struct sk_buff_head skqueue
;
992 struct ib_qp_attr qp_attr
;
993 int qp_attr_mask
, ret
;
996 p
->mtu
= be32_to_cpu(data
->mtu
);
998 if (p
->mtu
<= IPOIB_ENCAP_LEN
) {
999 ipoib_warn(priv
, "Rejecting connection: mtu %d <= %d\n",
1000 p
->mtu
, IPOIB_ENCAP_LEN
);
1004 qp_attr
.qp_state
= IB_QPS_RTR
;
1005 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
1007 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
1011 qp_attr
.rq_psn
= 0 /* FIXME */;
1012 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
1014 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
1018 qp_attr
.qp_state
= IB_QPS_RTS
;
1019 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
1021 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
1024 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
1026 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
1030 skb_queue_head_init(&skqueue
);
1032 netif_tx_lock_bh(p
->dev
);
1033 spin_lock_irq(&priv
->lock
);
1034 set_bit(IPOIB_FLAG_OPER_UP
, &p
->flags
);
1036 while ((skb
= __skb_dequeue(&p
->neigh
->queue
)))
1037 __skb_queue_tail(&skqueue
, skb
);
1038 spin_unlock_irq(&priv
->lock
);
1039 netif_tx_unlock_bh(p
->dev
);
1041 while ((skb
= __skb_dequeue(&skqueue
))) {
1043 ret
= dev_queue_xmit(skb
);
1045 ipoib_warn(priv
, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
1049 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1051 ipoib_warn(priv
, "failed to send RTU: %d\n", ret
);
1057 static struct ib_qp
*ipoib_cm_create_tx_qp(struct net_device
*dev
, struct ipoib_cm_tx
*tx
)
1059 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1060 struct ib_qp_init_attr attr
= {
1061 .send_cq
= priv
->send_cq
,
1062 .recv_cq
= priv
->recv_cq
,
1063 .srq
= priv
->cm
.srq
,
1064 .cap
.max_send_wr
= ipoib_sendq_size
,
1065 .cap
.max_send_sge
= 1,
1066 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
1067 .qp_type
= IB_QPT_RC
,
1071 struct ib_qp
*tx_qp
;
1073 if (dev
->features
& NETIF_F_SG
)
1074 attr
.cap
.max_send_sge
= min_t(u32
, priv
->ca
->attrs
.max_send_sge
,
1077 tx_qp
= ib_create_qp(priv
->pd
, &attr
);
1078 tx
->max_send_sge
= attr
.cap
.max_send_sge
;
1082 static int ipoib_cm_send_req(struct net_device
*dev
,
1083 struct ib_cm_id
*id
, struct ib_qp
*qp
,
1085 struct sa_path_rec
*pathrec
)
1087 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1088 struct ipoib_cm_data data
= {};
1089 struct ib_cm_req_param req
= {};
1091 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
1092 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
1094 req
.primary_path
= pathrec
;
1095 req
.alternate_path
= NULL
;
1096 req
.service_id
= cpu_to_be64(IPOIB_CM_IETF_ID
| qpn
);
1097 req
.qp_num
= qp
->qp_num
;
1098 req
.qp_type
= qp
->qp_type
;
1099 req
.private_data
= &data
;
1100 req
.private_data_len
= sizeof(data
);
1101 req
.flow_control
= 0;
1103 req
.starting_psn
= 0; /* FIXME */
1106 * Pick some arbitrary defaults here; we could make these
1107 * module parameters if anyone cared about setting them.
1109 req
.responder_resources
= 4;
1110 req
.remote_cm_response_timeout
= 20;
1111 req
.local_cm_response_timeout
= 20;
1112 req
.retry_count
= 0; /* RFC draft warns against retries */
1113 req
.rnr_retry_count
= 0; /* RFC draft warns against retries */
1114 req
.max_cm_retries
= 15;
1115 req
.srq
= ipoib_cm_has_srq(dev
);
1116 return ib_send_cm_req(id
, &req
);
1119 static int ipoib_cm_modify_tx_init(struct net_device
*dev
,
1120 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
)
1122 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1123 struct ib_qp_attr qp_attr
;
1124 int qp_attr_mask
, ret
;
1125 ret
= ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &qp_attr
.pkey_index
);
1127 ipoib_warn(priv
, "pkey 0x%x not found: %d\n", priv
->pkey
, ret
);
1131 qp_attr
.qp_state
= IB_QPS_INIT
;
1132 qp_attr
.qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
1133 qp_attr
.port_num
= priv
->port
;
1134 qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1136 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
1138 ipoib_warn(priv
, "failed to modify tx QP to INIT: %d\n", ret
);
1144 static int ipoib_cm_tx_init(struct ipoib_cm_tx
*p
, u32 qpn
,
1145 struct sa_path_rec
*pathrec
)
1147 struct ipoib_dev_priv
*priv
= ipoib_priv(p
->dev
);
1148 unsigned int noio_flag
;
1151 noio_flag
= memalloc_noio_save();
1152 p
->tx_ring
= vzalloc(array_size(ipoib_sendq_size
, sizeof(*p
->tx_ring
)));
1154 memalloc_noio_restore(noio_flag
);
1159 p
->qp
= ipoib_cm_create_tx_qp(p
->dev
, p
);
1160 memalloc_noio_restore(noio_flag
);
1161 if (IS_ERR(p
->qp
)) {
1162 ret
= PTR_ERR(p
->qp
);
1163 ipoib_warn(priv
, "failed to create tx qp: %d\n", ret
);
1167 p
->id
= ib_create_cm_id(priv
->ca
, ipoib_cm_tx_handler
, p
);
1168 if (IS_ERR(p
->id
)) {
1169 ret
= PTR_ERR(p
->id
);
1170 ipoib_warn(priv
, "failed to create tx cm id: %d\n", ret
);
1174 ret
= ipoib_cm_modify_tx_init(p
->dev
, p
->id
, p
->qp
);
1176 ipoib_warn(priv
, "failed to modify tx qp to rtr: %d\n", ret
);
1177 goto err_modify_send
;
1180 ret
= ipoib_cm_send_req(p
->dev
, p
->id
, p
->qp
, qpn
, pathrec
);
1182 ipoib_warn(priv
, "failed to send cm req: %d\n", ret
);
1183 goto err_modify_send
;
1186 ipoib_dbg(priv
, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1187 p
->qp
->qp_num
, pathrec
->dgid
.raw
, qpn
);
1192 ib_destroy_cm_id(p
->id
);
1195 ib_destroy_qp(p
->qp
);
1203 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx
*p
)
1205 struct ipoib_dev_priv
*priv
= ipoib_priv(p
->dev
);
1206 struct ipoib_tx_buf
*tx_req
;
1207 unsigned long begin
;
1209 ipoib_dbg(priv
, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1210 p
->qp
? p
->qp
->qp_num
: 0, p
->tx_head
, p
->tx_tail
);
1213 ib_destroy_cm_id(p
->id
);
1216 /* Wait for all sends to complete */
1218 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1219 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
1220 ipoib_warn(priv
, "timing out; %d sends not completed\n",
1221 p
->tx_head
- p
->tx_tail
);
1225 usleep_range(1000, 2000);
1231 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1232 tx_req
= &p
->tx_ring
[p
->tx_tail
& (ipoib_sendq_size
- 1)];
1233 ipoib_dma_unmap_tx(priv
, tx_req
);
1234 dev_kfree_skb_any(tx_req
->skb
);
1235 netif_tx_lock_bh(p
->dev
);
1237 ++priv
->global_tx_tail
;
1238 if (unlikely((priv
->global_tx_head
- priv
->global_tx_tail
) <=
1239 ipoib_sendq_size
>> 1) &&
1240 netif_queue_stopped(p
->dev
) &&
1241 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
1242 netif_wake_queue(p
->dev
);
1243 netif_tx_unlock_bh(p
->dev
);
1247 ib_destroy_qp(p
->qp
);
1253 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
1254 const struct ib_cm_event
*event
)
1256 struct ipoib_cm_tx
*tx
= cm_id
->context
;
1257 struct ipoib_dev_priv
*priv
= ipoib_priv(tx
->dev
);
1258 struct net_device
*dev
= priv
->dev
;
1259 struct ipoib_neigh
*neigh
;
1260 unsigned long flags
;
1263 switch (event
->event
) {
1264 case IB_CM_DREQ_RECEIVED
:
1265 ipoib_dbg(priv
, "DREQ received.\n");
1266 ib_send_cm_drep(cm_id
, NULL
, 0);
1268 case IB_CM_REP_RECEIVED
:
1269 ipoib_dbg(priv
, "REP received.\n");
1270 ret
= ipoib_cm_rep_handler(cm_id
, event
);
1272 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
1275 case IB_CM_REQ_ERROR
:
1276 case IB_CM_REJ_RECEIVED
:
1277 case IB_CM_TIMEWAIT_EXIT
:
1278 ipoib_dbg(priv
, "CM error %d.\n", event
->event
);
1279 netif_tx_lock_bh(dev
);
1280 spin_lock_irqsave(&priv
->lock
, flags
);
1285 ipoib_neigh_free(neigh
);
1290 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1291 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1292 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
1295 spin_unlock_irqrestore(&priv
->lock
, flags
);
1296 netif_tx_unlock_bh(dev
);
1305 struct ipoib_cm_tx
*ipoib_cm_create_tx(struct net_device
*dev
, struct ipoib_path
*path
,
1306 struct ipoib_neigh
*neigh
)
1308 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1309 struct ipoib_cm_tx
*tx
;
1311 tx
= kzalloc(sizeof(*tx
), GFP_ATOMIC
);
1318 list_add(&tx
->list
, &priv
->cm
.start_list
);
1319 set_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
);
1320 queue_work(priv
->wq
, &priv
->cm
.start_task
);
1324 void ipoib_cm_destroy_tx(struct ipoib_cm_tx
*tx
)
1326 struct ipoib_dev_priv
*priv
= ipoib_priv(tx
->dev
);
1327 unsigned long flags
;
1328 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1329 spin_lock_irqsave(&priv
->lock
, flags
);
1330 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1331 queue_work(priv
->wq
, &priv
->cm
.reap_task
);
1332 ipoib_dbg(priv
, "Reap connection for gid %pI6\n",
1333 tx
->neigh
->daddr
+ 4);
1335 spin_unlock_irqrestore(&priv
->lock
, flags
);
1339 #define QPN_AND_OPTIONS_OFFSET 4
1341 static void ipoib_cm_tx_start(struct work_struct
*work
)
1343 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1345 struct net_device
*dev
= priv
->dev
;
1346 struct ipoib_neigh
*neigh
;
1347 struct ipoib_cm_tx
*p
;
1348 unsigned long flags
;
1349 struct ipoib_path
*path
;
1352 struct sa_path_rec pathrec
;
1355 netif_tx_lock_bh(dev
);
1356 spin_lock_irqsave(&priv
->lock
, flags
);
1358 while (!list_empty(&priv
->cm
.start_list
)) {
1359 p
= list_entry(priv
->cm
.start_list
.next
, typeof(*p
), list
);
1360 list_del_init(&p
->list
);
1363 qpn
= IPOIB_QPN(neigh
->daddr
);
1365 * As long as the search is with these 2 locks,
1366 * path existence indicates its validity.
1368 path
= __path_find(dev
, neigh
->daddr
+ QPN_AND_OPTIONS_OFFSET
);
1370 pr_info("%s ignore not valid path %pI6\n",
1372 neigh
->daddr
+ QPN_AND_OPTIONS_OFFSET
);
1375 memcpy(&pathrec
, &path
->pathrec
, sizeof(pathrec
));
1377 spin_unlock_irqrestore(&priv
->lock
, flags
);
1378 netif_tx_unlock_bh(dev
);
1380 ret
= ipoib_cm_tx_init(p
, qpn
, &pathrec
);
1382 netif_tx_lock_bh(dev
);
1383 spin_lock_irqsave(&priv
->lock
, flags
);
1390 ipoib_neigh_free(neigh
);
1397 spin_unlock_irqrestore(&priv
->lock
, flags
);
1398 netif_tx_unlock_bh(dev
);
1401 static void ipoib_cm_tx_reap(struct work_struct
*work
)
1403 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1405 struct net_device
*dev
= priv
->dev
;
1406 struct ipoib_cm_tx
*p
;
1407 unsigned long flags
;
1409 netif_tx_lock_bh(dev
);
1410 spin_lock_irqsave(&priv
->lock
, flags
);
1412 while (!list_empty(&priv
->cm
.reap_list
)) {
1413 p
= list_entry(priv
->cm
.reap_list
.next
, typeof(*p
), list
);
1414 list_del_init(&p
->list
);
1415 spin_unlock_irqrestore(&priv
->lock
, flags
);
1416 netif_tx_unlock_bh(dev
);
1417 ipoib_cm_tx_destroy(p
);
1418 netif_tx_lock_bh(dev
);
1419 spin_lock_irqsave(&priv
->lock
, flags
);
1422 spin_unlock_irqrestore(&priv
->lock
, flags
);
1423 netif_tx_unlock_bh(dev
);
1426 static void ipoib_cm_skb_reap(struct work_struct
*work
)
1428 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1430 struct net_device
*dev
= priv
->dev
;
1431 struct sk_buff
*skb
;
1432 unsigned long flags
;
1433 unsigned int mtu
= priv
->mcast_mtu
;
1435 netif_tx_lock_bh(dev
);
1436 spin_lock_irqsave(&priv
->lock
, flags
);
1438 while ((skb
= skb_dequeue(&priv
->cm
.skb_queue
))) {
1439 spin_unlock_irqrestore(&priv
->lock
, flags
);
1440 netif_tx_unlock_bh(dev
);
1442 if (skb
->protocol
== htons(ETH_P_IP
)) {
1443 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
1444 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1446 #if IS_ENABLED(CONFIG_IPV6)
1447 else if (skb
->protocol
== htons(ETH_P_IPV6
)) {
1448 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
1449 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1452 dev_kfree_skb_any(skb
);
1454 netif_tx_lock_bh(dev
);
1455 spin_lock_irqsave(&priv
->lock
, flags
);
1458 spin_unlock_irqrestore(&priv
->lock
, flags
);
1459 netif_tx_unlock_bh(dev
);
1462 void ipoib_cm_skb_too_long(struct net_device
*dev
, struct sk_buff
*skb
,
1465 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1466 int e
= skb_queue_empty(&priv
->cm
.skb_queue
);
1468 skb_dst_update_pmtu(skb
, mtu
);
1470 skb_queue_tail(&priv
->cm
.skb_queue
, skb
);
1472 queue_work(priv
->wq
, &priv
->cm
.skb_task
);
1475 static void ipoib_cm_rx_reap(struct work_struct
*work
)
1477 ipoib_cm_free_rx_reap_list(container_of(work
, struct ipoib_dev_priv
,
1478 cm
.rx_reap_task
)->dev
);
1481 static void ipoib_cm_stale_task(struct work_struct
*work
)
1483 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1484 cm
.stale_task
.work
);
1485 struct ipoib_cm_rx
*p
;
1488 spin_lock_irq(&priv
->lock
);
1489 while (!list_empty(&priv
->cm
.passive_ids
)) {
1490 /* List is sorted by LRU, start from tail,
1491 * stop when we see a recently used entry */
1492 p
= list_entry(priv
->cm
.passive_ids
.prev
, typeof(*p
), list
);
1493 if (time_before_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_TIMEOUT
))
1495 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
1496 p
->state
= IPOIB_CM_RX_ERROR
;
1497 spin_unlock_irq(&priv
->lock
);
1498 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
1500 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
1501 spin_lock_irq(&priv
->lock
);
1504 if (!list_empty(&priv
->cm
.passive_ids
))
1505 queue_delayed_work(priv
->wq
,
1506 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
1507 spin_unlock_irq(&priv
->lock
);
1510 static ssize_t
show_mode(struct device
*d
, struct device_attribute
*attr
,
1513 struct net_device
*dev
= to_net_dev(d
);
1514 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1516 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
1517 return sysfs_emit(buf
, "connected\n");
1519 return sysfs_emit(buf
, "datagram\n");
1522 static ssize_t
set_mode(struct device
*d
, struct device_attribute
*attr
,
1523 const char *buf
, size_t count
)
1525 struct net_device
*dev
= to_net_dev(d
);
1528 if (!rtnl_trylock()) {
1529 return restart_syscall();
1532 if (dev
->reg_state
!= NETREG_REGISTERED
) {
1537 ret
= ipoib_set_mode(dev
, buf
);
1539 /* The assumption is that the function ipoib_set_mode returned
1540 * with the rtnl held by it, if not the value -EBUSY returned,
1541 * then no need to rtnl_unlock
1546 return (!ret
|| ret
== -EBUSY
) ? count
: ret
;
1549 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
, show_mode
, set_mode
);
1551 int ipoib_cm_add_mode_attr(struct net_device
*dev
)
1553 return device_create_file(&dev
->dev
, &dev_attr_mode
);
1556 static void ipoib_cm_create_srq(struct net_device
*dev
, int max_sge
)
1558 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1559 struct ib_srq_init_attr srq_init_attr
= {
1560 .srq_type
= IB_SRQT_BASIC
,
1562 .max_wr
= ipoib_recvq_size
,
1567 priv
->cm
.srq
= ib_create_srq(priv
->pd
, &srq_init_attr
);
1568 if (IS_ERR(priv
->cm
.srq
)) {
1569 if (PTR_ERR(priv
->cm
.srq
) != -EOPNOTSUPP
)
1570 pr_warn("%s: failed to allocate SRQ, error %ld\n",
1571 priv
->ca
->name
, PTR_ERR(priv
->cm
.srq
));
1572 priv
->cm
.srq
= NULL
;
1576 priv
->cm
.srq_ring
= vzalloc(array_size(ipoib_recvq_size
,
1577 sizeof(*priv
->cm
.srq_ring
)));
1578 if (!priv
->cm
.srq_ring
) {
1579 ib_destroy_srq(priv
->cm
.srq
);
1580 priv
->cm
.srq
= NULL
;
1586 int ipoib_cm_dev_init(struct net_device
*dev
)
1588 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1591 INIT_LIST_HEAD(&priv
->cm
.passive_ids
);
1592 INIT_LIST_HEAD(&priv
->cm
.reap_list
);
1593 INIT_LIST_HEAD(&priv
->cm
.start_list
);
1594 INIT_LIST_HEAD(&priv
->cm
.rx_error_list
);
1595 INIT_LIST_HEAD(&priv
->cm
.rx_flush_list
);
1596 INIT_LIST_HEAD(&priv
->cm
.rx_drain_list
);
1597 INIT_LIST_HEAD(&priv
->cm
.rx_reap_list
);
1598 INIT_WORK(&priv
->cm
.start_task
, ipoib_cm_tx_start
);
1599 INIT_WORK(&priv
->cm
.reap_task
, ipoib_cm_tx_reap
);
1600 INIT_WORK(&priv
->cm
.skb_task
, ipoib_cm_skb_reap
);
1601 INIT_WORK(&priv
->cm
.rx_reap_task
, ipoib_cm_rx_reap
);
1602 INIT_DELAYED_WORK(&priv
->cm
.stale_task
, ipoib_cm_stale_task
);
1604 skb_queue_head_init(&priv
->cm
.skb_queue
);
1606 ipoib_dbg(priv
, "max_srq_sge=%d\n", priv
->ca
->attrs
.max_srq_sge
);
1608 max_srq_sge
= min_t(int, IPOIB_CM_RX_SG
, priv
->ca
->attrs
.max_srq_sge
);
1609 ipoib_cm_create_srq(dev
, max_srq_sge
);
1610 if (ipoib_cm_has_srq(dev
)) {
1611 priv
->cm
.max_cm_mtu
= max_srq_sge
* PAGE_SIZE
- 0x10;
1612 priv
->cm
.num_frags
= max_srq_sge
;
1613 ipoib_dbg(priv
, "max_cm_mtu = 0x%x, num_frags=%d\n",
1614 priv
->cm
.max_cm_mtu
, priv
->cm
.num_frags
);
1616 priv
->cm
.max_cm_mtu
= IPOIB_CM_MTU
;
1617 priv
->cm
.num_frags
= IPOIB_CM_RX_SG
;
1620 ipoib_cm_init_rx_wr(dev
, &priv
->cm
.rx_wr
, priv
->cm
.rx_sge
);
1622 if (ipoib_cm_has_srq(dev
)) {
1623 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
1624 if (!ipoib_cm_alloc_rx_skb(dev
, priv
->cm
.srq_ring
, i
,
1625 priv
->cm
.num_frags
- 1,
1626 priv
->cm
.srq_ring
[i
].mapping
,
1628 ipoib_warn(priv
, "failed to allocate "
1629 "receive buffer %d\n", i
);
1630 ipoib_cm_dev_cleanup(dev
);
1634 if (ipoib_cm_post_receive_srq(dev
, i
)) {
1635 ipoib_warn(priv
, "ipoib_cm_post_receive_srq "
1636 "failed for buf %d\n", i
);
1637 ipoib_cm_dev_cleanup(dev
);
1643 priv
->dev
->dev_addr
[0] = IPOIB_FLAGS_RC
;
1647 void ipoib_cm_dev_cleanup(struct net_device
*dev
)
1649 struct ipoib_dev_priv
*priv
= ipoib_priv(dev
);
1654 ipoib_dbg(priv
, "Cleanup ipoib connected mode.\n");
1656 ib_destroy_srq(priv
->cm
.srq
);
1657 priv
->cm
.srq
= NULL
;
1658 if (!priv
->cm
.srq_ring
)
1661 ipoib_cm_free_rx_ring(dev
, priv
->cm
.srq_ring
);
1662 priv
->cm
.srq_ring
= NULL
;