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
= {
73 .wr_id
= IPOIB_CM_RX_DRAIN_WRID
,
77 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
78 struct ib_cm_event
*event
);
80 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv
*priv
, int frags
,
81 u64 mapping
[IPOIB_CM_RX_SG
])
85 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_CM_HEAD_SIZE
, DMA_FROM_DEVICE
);
87 for (i
= 0; i
< frags
; ++i
)
88 ib_dma_unmap_page(priv
->ca
, mapping
[i
+ 1], PAGE_SIZE
, DMA_FROM_DEVICE
);
91 static int ipoib_cm_post_receive_srq(struct net_device
*dev
, int id
)
93 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
94 struct ib_recv_wr
*bad_wr
;
97 priv
->cm
.rx_wr
.wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
99 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
100 priv
->cm
.rx_sge
[i
].addr
= priv
->cm
.srq_ring
[id
].mapping
[i
];
102 ret
= ib_post_srq_recv(priv
->cm
.srq
, &priv
->cm
.rx_wr
, &bad_wr
);
104 ipoib_warn(priv
, "post srq failed for buf %d (%d)\n", id
, ret
);
105 ipoib_cm_dma_unmap_rx(priv
, priv
->cm
.num_frags
- 1,
106 priv
->cm
.srq_ring
[id
].mapping
);
107 dev_kfree_skb_any(priv
->cm
.srq_ring
[id
].skb
);
108 priv
->cm
.srq_ring
[id
].skb
= NULL
;
114 static int ipoib_cm_post_receive_nonsrq(struct net_device
*dev
,
115 struct ipoib_cm_rx
*rx
,
116 struct ib_recv_wr
*wr
,
117 struct ib_sge
*sge
, int id
)
119 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
120 struct ib_recv_wr
*bad_wr
;
123 wr
->wr_id
= id
| IPOIB_OP_CM
| IPOIB_OP_RECV
;
125 for (i
= 0; i
< IPOIB_CM_RX_SG
; ++i
)
126 sge
[i
].addr
= rx
->rx_ring
[id
].mapping
[i
];
128 ret
= ib_post_recv(rx
->qp
, wr
, &bad_wr
);
130 ipoib_warn(priv
, "post recv failed for buf %d (%d)\n", id
, ret
);
131 ipoib_cm_dma_unmap_rx(priv
, IPOIB_CM_RX_SG
- 1,
132 rx
->rx_ring
[id
].mapping
);
133 dev_kfree_skb_any(rx
->rx_ring
[id
].skb
);
134 rx
->rx_ring
[id
].skb
= NULL
;
140 static struct sk_buff
*ipoib_cm_alloc_rx_skb(struct net_device
*dev
,
141 struct ipoib_cm_rx_buf
*rx_ring
,
143 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_ATOMIC
);
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 if (ib_post_send(p
->qp
, &ipoib_cm_rx_drain_wr
, &bad_wr
))
226 ipoib_warn(priv
, "failed to post drain wr\n");
228 list_splice_init(&priv
->cm
.rx_flush_list
, &priv
->cm
.rx_drain_list
);
231 static void ipoib_cm_rx_event_handler(struct ib_event
*event
, void *ctx
)
233 struct ipoib_cm_rx
*p
= ctx
;
234 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
237 if (event
->event
!= IB_EVENT_QP_LAST_WQE_REACHED
)
240 spin_lock_irqsave(&priv
->lock
, flags
);
241 list_move(&p
->list
, &priv
->cm
.rx_flush_list
);
242 p
->state
= IPOIB_CM_RX_FLUSH
;
243 ipoib_cm_start_rx_drain(priv
);
244 spin_unlock_irqrestore(&priv
->lock
, flags
);
247 static struct ib_qp
*ipoib_cm_create_rx_qp(struct net_device
*dev
,
248 struct ipoib_cm_rx
*p
)
250 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
251 struct ib_qp_init_attr attr
= {
252 .event_handler
= ipoib_cm_rx_event_handler
,
253 .send_cq
= priv
->recv_cq
, /* For drain WR */
254 .recv_cq
= priv
->recv_cq
,
256 .cap
.max_send_wr
= 1, /* For drain WR */
257 .cap
.max_send_sge
= 1, /* FIXME: 0 Seems not to work */
258 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
259 .qp_type
= IB_QPT_RC
,
263 if (!ipoib_cm_has_srq(dev
)) {
264 attr
.cap
.max_recv_wr
= ipoib_recvq_size
;
265 attr
.cap
.max_recv_sge
= IPOIB_CM_RX_SG
;
268 return ib_create_qp(priv
->pd
, &attr
);
271 static int ipoib_cm_modify_rx_qp(struct net_device
*dev
,
272 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
,
275 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
276 struct ib_qp_attr qp_attr
;
277 int qp_attr_mask
, ret
;
279 qp_attr
.qp_state
= IB_QPS_INIT
;
280 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
282 ipoib_warn(priv
, "failed to init QP attr for INIT: %d\n", ret
);
285 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
287 ipoib_warn(priv
, "failed to modify QP to INIT: %d\n", ret
);
290 qp_attr
.qp_state
= IB_QPS_RTR
;
291 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
293 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
296 qp_attr
.rq_psn
= psn
;
297 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
299 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
304 * Current Mellanox HCA firmware won't generate completions
305 * with error for drain WRs unless the QP has been moved to
306 * RTS first. This work-around leaves a window where a QP has
307 * moved to error asynchronously, but this will eventually get
308 * fixed in firmware, so let's not error out if modify QP
311 qp_attr
.qp_state
= IB_QPS_RTS
;
312 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
314 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
317 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
319 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
326 static void ipoib_cm_init_rx_wr(struct net_device
*dev
,
327 struct ib_recv_wr
*wr
,
330 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
333 for (i
= 0; i
< priv
->cm
.num_frags
; ++i
)
334 sge
[i
].lkey
= priv
->mr
->lkey
;
336 sge
[0].length
= IPOIB_CM_HEAD_SIZE
;
337 for (i
= 1; i
< priv
->cm
.num_frags
; ++i
)
338 sge
[i
].length
= PAGE_SIZE
;
342 wr
->num_sge
= priv
->cm
.num_frags
;
345 static int ipoib_cm_nonsrq_init_rx(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
346 struct ipoib_cm_rx
*rx
)
348 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
350 struct ib_recv_wr wr
;
351 struct ib_sge sge
[IPOIB_CM_RX_SG
];
356 rx
->rx_ring
= vzalloc(ipoib_recvq_size
* sizeof *rx
->rx_ring
);
358 printk(KERN_WARNING
"%s: failed to allocate CM non-SRQ ring (%d entries)\n",
359 priv
->ca
->name
, ipoib_recvq_size
);
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
)) {
386 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
390 ret
= ipoib_cm_post_receive_nonsrq(dev
, rx
, &t
->wr
, t
->sge
, i
);
392 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq "
393 "failed for buf %d\n", i
);
399 rx
->recv_count
= ipoib_recvq_size
;
406 spin_lock_irq(&priv
->lock
);
407 --priv
->cm
.nonsrq_conn_qp
;
408 spin_unlock_irq(&priv
->lock
);
412 ipoib_cm_free_rx_ring(dev
, rx
->rx_ring
);
417 static int ipoib_cm_send_rep(struct net_device
*dev
, struct ib_cm_id
*cm_id
,
418 struct ib_qp
*qp
, struct ib_cm_req_event_param
*req
,
421 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
422 struct ipoib_cm_data data
= {};
423 struct ib_cm_rep_param rep
= {};
425 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
426 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
428 rep
.private_data
= &data
;
429 rep
.private_data_len
= sizeof data
;
430 rep
.flow_control
= 0;
431 rep
.rnr_retry_count
= req
->rnr_retry_count
;
432 rep
.srq
= ipoib_cm_has_srq(dev
);
433 rep
.qp_num
= qp
->qp_num
;
434 rep
.starting_psn
= psn
;
435 return ib_send_cm_rep(cm_id
, &rep
);
438 static int ipoib_cm_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
440 struct net_device
*dev
= cm_id
->context
;
441 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
442 struct ipoib_cm_rx
*p
;
446 ipoib_dbg(priv
, "REQ arrived\n");
447 p
= kzalloc(sizeof *p
, GFP_KERNEL
);
453 p
->state
= IPOIB_CM_RX_LIVE
;
454 p
->jiffies
= jiffies
;
455 INIT_LIST_HEAD(&p
->list
);
457 p
->qp
= ipoib_cm_create_rx_qp(dev
, p
);
459 ret
= PTR_ERR(p
->qp
);
463 psn
= prandom_u32() & 0xffffff;
464 ret
= ipoib_cm_modify_rx_qp(dev
, cm_id
, p
->qp
, psn
);
468 if (!ipoib_cm_has_srq(dev
)) {
469 ret
= ipoib_cm_nonsrq_init_rx(dev
, cm_id
, p
);
474 spin_lock_irq(&priv
->lock
);
475 queue_delayed_work(ipoib_workqueue
,
476 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
477 /* Add this entry to passive ids list head, but do not re-add it
478 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
479 p
->jiffies
= jiffies
;
480 if (p
->state
== IPOIB_CM_RX_LIVE
)
481 list_move(&p
->list
, &priv
->cm
.passive_ids
);
482 spin_unlock_irq(&priv
->lock
);
484 ret
= ipoib_cm_send_rep(dev
, cm_id
, p
->qp
, &event
->param
.req_rcvd
, psn
);
486 ipoib_warn(priv
, "failed to send REP: %d\n", ret
);
487 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
488 ipoib_warn(priv
, "unable to move qp to error state\n");
493 ib_destroy_qp(p
->qp
);
499 static int ipoib_cm_rx_handler(struct ib_cm_id
*cm_id
,
500 struct ib_cm_event
*event
)
502 struct ipoib_cm_rx
*p
;
503 struct ipoib_dev_priv
*priv
;
505 switch (event
->event
) {
506 case IB_CM_REQ_RECEIVED
:
507 return ipoib_cm_req_handler(cm_id
, event
);
508 case IB_CM_DREQ_RECEIVED
:
510 ib_send_cm_drep(cm_id
, NULL
, 0);
512 case IB_CM_REJ_RECEIVED
:
514 priv
= netdev_priv(p
->dev
);
515 if (ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
))
516 ipoib_warn(priv
, "unable to move qp to error state\n");
522 /* Adjust length of skb with fragments to match received data */
523 static void skb_put_frags(struct sk_buff
*skb
, unsigned int hdr_space
,
524 unsigned int length
, struct sk_buff
*toskb
)
529 /* put header into skb */
530 size
= min(length
, hdr_space
);
535 num_frags
= skb_shinfo(skb
)->nr_frags
;
536 for (i
= 0; i
< num_frags
; i
++) {
537 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
540 /* don't need this page */
541 skb_fill_page_desc(toskb
, i
, skb_frag_page(frag
),
543 --skb_shinfo(skb
)->nr_frags
;
545 size
= min(length
, (unsigned) PAGE_SIZE
);
547 skb_frag_size_set(frag
, size
);
548 skb
->data_len
+= size
;
549 skb
->truesize
+= size
;
556 void ipoib_cm_handle_rx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
558 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
559 struct ipoib_cm_rx_buf
*rx_ring
;
560 unsigned int wr_id
= wc
->wr_id
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
);
561 struct sk_buff
*skb
, *newskb
;
562 struct ipoib_cm_rx
*p
;
564 u64 mapping
[IPOIB_CM_RX_SG
];
567 struct sk_buff
*small_skb
;
569 ipoib_dbg_data(priv
, "cm recv completion: id %d, status: %d\n",
572 if (unlikely(wr_id
>= ipoib_recvq_size
)) {
573 if (wr_id
== (IPOIB_CM_RX_DRAIN_WRID
& ~(IPOIB_OP_CM
| IPOIB_OP_RECV
))) {
574 spin_lock_irqsave(&priv
->lock
, flags
);
575 list_splice_init(&priv
->cm
.rx_drain_list
, &priv
->cm
.rx_reap_list
);
576 ipoib_cm_start_rx_drain(priv
);
577 queue_work(ipoib_workqueue
, &priv
->cm
.rx_reap_task
);
578 spin_unlock_irqrestore(&priv
->lock
, flags
);
580 ipoib_warn(priv
, "cm recv completion event with wrid %d (> %d)\n",
581 wr_id
, ipoib_recvq_size
);
585 p
= wc
->qp
->qp_context
;
587 has_srq
= ipoib_cm_has_srq(dev
);
588 rx_ring
= has_srq
? priv
->cm
.srq_ring
: p
->rx_ring
;
590 skb
= rx_ring
[wr_id
].skb
;
592 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
593 ipoib_dbg(priv
, "cm recv error "
594 "(status=%d, wrid=%d vend_err %x)\n",
595 wc
->status
, wr_id
, wc
->vendor_err
);
596 ++dev
->stats
.rx_dropped
;
600 if (!--p
->recv_count
) {
601 spin_lock_irqsave(&priv
->lock
, flags
);
602 list_move(&p
->list
, &priv
->cm
.rx_reap_list
);
603 spin_unlock_irqrestore(&priv
->lock
, flags
);
604 queue_work(ipoib_workqueue
, &priv
->cm
.rx_reap_task
);
610 if (unlikely(!(wr_id
& IPOIB_CM_RX_UPDATE_MASK
))) {
611 if (p
&& time_after_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_UPDATE_TIME
)) {
612 spin_lock_irqsave(&priv
->lock
, flags
);
613 p
->jiffies
= jiffies
;
614 /* Move this entry to list head, but do not re-add it
615 * if it has been moved out of list. */
616 if (p
->state
== IPOIB_CM_RX_LIVE
)
617 list_move(&p
->list
, &priv
->cm
.passive_ids
);
618 spin_unlock_irqrestore(&priv
->lock
, flags
);
622 if (wc
->byte_len
< IPOIB_CM_COPYBREAK
) {
623 int dlen
= wc
->byte_len
;
625 small_skb
= dev_alloc_skb(dlen
+ 12);
627 skb_reserve(small_skb
, 12);
628 ib_dma_sync_single_for_cpu(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
629 dlen
, DMA_FROM_DEVICE
);
630 skb_copy_from_linear_data(skb
, small_skb
->data
, dlen
);
631 ib_dma_sync_single_for_device(priv
->ca
, rx_ring
[wr_id
].mapping
[0],
632 dlen
, DMA_FROM_DEVICE
);
633 skb_put(small_skb
, dlen
);
639 frags
= PAGE_ALIGN(wc
->byte_len
- min(wc
->byte_len
,
640 (unsigned)IPOIB_CM_HEAD_SIZE
)) / PAGE_SIZE
;
642 newskb
= ipoib_cm_alloc_rx_skb(dev
, rx_ring
, wr_id
, frags
, mapping
);
643 if (unlikely(!newskb
)) {
645 * If we can't allocate a new RX buffer, dump
646 * this packet and reuse the old buffer.
648 ipoib_dbg(priv
, "failed to allocate receive buffer %d\n", wr_id
);
649 ++dev
->stats
.rx_dropped
;
653 ipoib_cm_dma_unmap_rx(priv
, frags
, rx_ring
[wr_id
].mapping
);
654 memcpy(rx_ring
[wr_id
].mapping
, mapping
, (frags
+ 1) * sizeof *mapping
);
656 ipoib_dbg_data(priv
, "received %d bytes, SLID 0x%04x\n",
657 wc
->byte_len
, wc
->slid
);
659 skb_put_frags(skb
, IPOIB_CM_HEAD_SIZE
, wc
->byte_len
, newskb
);
662 skb
->protocol
= ((struct ipoib_header
*) skb
->data
)->proto
;
663 skb_reset_mac_header(skb
);
664 skb_pull(skb
, IPOIB_ENCAP_LEN
);
666 ++dev
->stats
.rx_packets
;
667 dev
->stats
.rx_bytes
+= skb
->len
;
670 /* XXX get correct PACKET_ type here */
671 skb
->pkt_type
= PACKET_HOST
;
672 netif_receive_skb(skb
);
676 if (unlikely(ipoib_cm_post_receive_srq(dev
, wr_id
)))
677 ipoib_warn(priv
, "ipoib_cm_post_receive_srq failed "
678 "for buf %d\n", wr_id
);
680 if (unlikely(ipoib_cm_post_receive_nonsrq(dev
, p
,
685 ipoib_warn(priv
, "ipoib_cm_post_receive_nonsrq failed "
686 "for buf %d\n", wr_id
);
691 static inline int post_send(struct ipoib_dev_priv
*priv
,
692 struct ipoib_cm_tx
*tx
,
696 struct ib_send_wr
*bad_wr
;
698 priv
->tx_sge
[0].addr
= addr
;
699 priv
->tx_sge
[0].length
= len
;
701 priv
->tx_wr
.num_sge
= 1;
702 priv
->tx_wr
.wr_id
= wr_id
| IPOIB_OP_CM
;
704 return ib_post_send(tx
->qp
, &priv
->tx_wr
, &bad_wr
);
707 void ipoib_cm_send(struct net_device
*dev
, struct sk_buff
*skb
, struct ipoib_cm_tx
*tx
)
709 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
710 struct ipoib_cm_tx_buf
*tx_req
;
714 if (unlikely(skb
->len
> tx
->mtu
)) {
715 ipoib_warn(priv
, "packet len %d (> %d) too long to send, dropping\n",
717 ++dev
->stats
.tx_dropped
;
718 ++dev
->stats
.tx_errors
;
719 ipoib_cm_skb_too_long(dev
, skb
, tx
->mtu
- IPOIB_ENCAP_LEN
);
723 ipoib_dbg_data(priv
, "sending packet: head 0x%x length %d connection 0x%x\n",
724 tx
->tx_head
, skb
->len
, tx
->qp
->qp_num
);
727 * We put the skb into the tx_ring _before_ we call post_send()
728 * because it's entirely possible that the completion handler will
729 * run before we execute anything after the post_send(). That
730 * means we have to make sure everything is properly recorded and
731 * our state is consistent before we call post_send().
733 tx_req
= &tx
->tx_ring
[tx
->tx_head
& (ipoib_sendq_size
- 1)];
735 addr
= ib_dma_map_single(priv
->ca
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
736 if (unlikely(ib_dma_mapping_error(priv
->ca
, addr
))) {
737 ++dev
->stats
.tx_errors
;
738 dev_kfree_skb_any(skb
);
742 tx_req
->mapping
= addr
;
747 rc
= post_send(priv
, tx
, tx
->tx_head
& (ipoib_sendq_size
- 1),
750 ipoib_warn(priv
, "post_send failed, error %d\n", rc
);
751 ++dev
->stats
.tx_errors
;
752 ib_dma_unmap_single(priv
->ca
, addr
, skb
->len
, DMA_TO_DEVICE
);
753 dev_kfree_skb_any(skb
);
755 dev
->trans_start
= jiffies
;
758 if (++priv
->tx_outstanding
== ipoib_sendq_size
) {
759 ipoib_dbg(priv
, "TX ring 0x%x full, stopping kernel net queue\n",
761 netif_stop_queue(dev
);
762 rc
= ib_req_notify_cq(priv
->send_cq
,
763 IB_CQ_NEXT_COMP
| IB_CQ_REPORT_MISSED_EVENTS
);
765 ipoib_warn(priv
, "request notify on send CQ failed\n");
767 ipoib_send_comp_handler(priv
->send_cq
, dev
);
772 void ipoib_cm_handle_tx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
774 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
775 struct ipoib_cm_tx
*tx
= wc
->qp
->qp_context
;
776 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_OP_CM
;
777 struct ipoib_cm_tx_buf
*tx_req
;
780 ipoib_dbg_data(priv
, "cm send completion: id %d, status: %d\n",
783 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
784 ipoib_warn(priv
, "cm send completion event with wrid %d (> %d)\n",
785 wr_id
, ipoib_sendq_size
);
789 tx_req
= &tx
->tx_ring
[wr_id
];
791 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
, DMA_TO_DEVICE
);
793 /* FIXME: is this right? Shouldn't we only increment on success? */
794 ++dev
->stats
.tx_packets
;
795 dev
->stats
.tx_bytes
+= tx_req
->skb
->len
;
797 dev_kfree_skb_any(tx_req
->skb
);
802 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
803 netif_queue_stopped(dev
) &&
804 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
805 netif_wake_queue(dev
);
807 if (wc
->status
!= IB_WC_SUCCESS
&&
808 wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
809 struct ipoib_neigh
*neigh
;
811 ipoib_dbg(priv
, "failed cm send event "
812 "(status=%d, wrid=%d vend_err %x)\n",
813 wc
->status
, wr_id
, wc
->vendor_err
);
815 spin_lock_irqsave(&priv
->lock
, flags
);
820 ipoib_neigh_free(neigh
);
825 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
826 list_move(&tx
->list
, &priv
->cm
.reap_list
);
827 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
830 clear_bit(IPOIB_FLAG_OPER_UP
, &tx
->flags
);
832 spin_unlock_irqrestore(&priv
->lock
, flags
);
835 netif_tx_unlock(dev
);
838 int ipoib_cm_dev_open(struct net_device
*dev
)
840 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
843 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
))
846 priv
->cm
.id
= ib_create_cm_id(priv
->ca
, ipoib_cm_rx_handler
, dev
);
847 if (IS_ERR(priv
->cm
.id
)) {
848 printk(KERN_WARNING
"%s: failed to create CM ID\n", priv
->ca
->name
);
849 ret
= PTR_ERR(priv
->cm
.id
);
853 ret
= ib_cm_listen(priv
->cm
.id
, cpu_to_be64(IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
),
856 printk(KERN_WARNING
"%s: failed to listen on ID 0x%llx\n", priv
->ca
->name
,
857 IPOIB_CM_IETF_ID
| priv
->qp
->qp_num
);
864 ib_destroy_cm_id(priv
->cm
.id
);
870 static void ipoib_cm_free_rx_reap_list(struct net_device
*dev
)
872 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
873 struct ipoib_cm_rx
*rx
, *n
;
876 spin_lock_irq(&priv
->lock
);
877 list_splice_init(&priv
->cm
.rx_reap_list
, &list
);
878 spin_unlock_irq(&priv
->lock
);
880 list_for_each_entry_safe(rx
, n
, &list
, list
) {
881 ib_destroy_cm_id(rx
->id
);
882 ib_destroy_qp(rx
->qp
);
883 if (!ipoib_cm_has_srq(dev
)) {
884 ipoib_cm_free_rx_ring(priv
->dev
, rx
->rx_ring
);
885 spin_lock_irq(&priv
->lock
);
886 --priv
->cm
.nonsrq_conn_qp
;
887 spin_unlock_irq(&priv
->lock
);
893 void ipoib_cm_dev_stop(struct net_device
*dev
)
895 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
896 struct ipoib_cm_rx
*p
;
900 if (!IPOIB_CM_SUPPORTED(dev
->dev_addr
) || !priv
->cm
.id
)
903 ib_destroy_cm_id(priv
->cm
.id
);
906 spin_lock_irq(&priv
->lock
);
907 while (!list_empty(&priv
->cm
.passive_ids
)) {
908 p
= list_entry(priv
->cm
.passive_ids
.next
, typeof(*p
), list
);
909 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
910 p
->state
= IPOIB_CM_RX_ERROR
;
911 spin_unlock_irq(&priv
->lock
);
912 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
914 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
915 spin_lock_irq(&priv
->lock
);
918 /* Wait for all RX to be drained */
921 while (!list_empty(&priv
->cm
.rx_error_list
) ||
922 !list_empty(&priv
->cm
.rx_flush_list
) ||
923 !list_empty(&priv
->cm
.rx_drain_list
)) {
924 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
925 ipoib_warn(priv
, "RX drain timing out\n");
928 * assume the HW is wedged and just free up everything.
930 list_splice_init(&priv
->cm
.rx_flush_list
,
931 &priv
->cm
.rx_reap_list
);
932 list_splice_init(&priv
->cm
.rx_error_list
,
933 &priv
->cm
.rx_reap_list
);
934 list_splice_init(&priv
->cm
.rx_drain_list
,
935 &priv
->cm
.rx_reap_list
);
938 spin_unlock_irq(&priv
->lock
);
941 spin_lock_irq(&priv
->lock
);
944 spin_unlock_irq(&priv
->lock
);
946 ipoib_cm_free_rx_reap_list(dev
);
948 cancel_delayed_work(&priv
->cm
.stale_task
);
951 static int ipoib_cm_rep_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*event
)
953 struct ipoib_cm_tx
*p
= cm_id
->context
;
954 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
955 struct ipoib_cm_data
*data
= event
->private_data
;
956 struct sk_buff_head skqueue
;
957 struct ib_qp_attr qp_attr
;
958 int qp_attr_mask
, ret
;
961 p
->mtu
= be32_to_cpu(data
->mtu
);
963 if (p
->mtu
<= IPOIB_ENCAP_LEN
) {
964 ipoib_warn(priv
, "Rejecting connection: mtu %d <= %d\n",
965 p
->mtu
, IPOIB_ENCAP_LEN
);
969 qp_attr
.qp_state
= IB_QPS_RTR
;
970 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
972 ipoib_warn(priv
, "failed to init QP attr for RTR: %d\n", ret
);
976 qp_attr
.rq_psn
= 0 /* FIXME */;
977 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
979 ipoib_warn(priv
, "failed to modify QP to RTR: %d\n", ret
);
983 qp_attr
.qp_state
= IB_QPS_RTS
;
984 ret
= ib_cm_init_qp_attr(cm_id
, &qp_attr
, &qp_attr_mask
);
986 ipoib_warn(priv
, "failed to init QP attr for RTS: %d\n", ret
);
989 ret
= ib_modify_qp(p
->qp
, &qp_attr
, qp_attr_mask
);
991 ipoib_warn(priv
, "failed to modify QP to RTS: %d\n", ret
);
995 skb_queue_head_init(&skqueue
);
997 spin_lock_irq(&priv
->lock
);
998 set_bit(IPOIB_FLAG_OPER_UP
, &p
->flags
);
1000 while ((skb
= __skb_dequeue(&p
->neigh
->queue
)))
1001 __skb_queue_tail(&skqueue
, skb
);
1002 spin_unlock_irq(&priv
->lock
);
1004 while ((skb
= __skb_dequeue(&skqueue
))) {
1006 if (dev_queue_xmit(skb
))
1007 ipoib_warn(priv
, "dev_queue_xmit failed "
1008 "to requeue packet\n");
1011 ret
= ib_send_cm_rtu(cm_id
, NULL
, 0);
1013 ipoib_warn(priv
, "failed to send RTU: %d\n", ret
);
1019 static struct ib_qp
*ipoib_cm_create_tx_qp(struct net_device
*dev
, struct ipoib_cm_tx
*tx
)
1021 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1022 struct ib_qp_init_attr attr
= {
1023 .send_cq
= priv
->recv_cq
,
1024 .recv_cq
= priv
->recv_cq
,
1025 .srq
= priv
->cm
.srq
,
1026 .cap
.max_send_wr
= ipoib_sendq_size
,
1027 .cap
.max_send_sge
= 1,
1028 .sq_sig_type
= IB_SIGNAL_ALL_WR
,
1029 .qp_type
= IB_QPT_RC
,
1033 return ib_create_qp(priv
->pd
, &attr
);
1036 static int ipoib_cm_send_req(struct net_device
*dev
,
1037 struct ib_cm_id
*id
, struct ib_qp
*qp
,
1039 struct ib_sa_path_rec
*pathrec
)
1041 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1042 struct ipoib_cm_data data
= {};
1043 struct ib_cm_req_param req
= {};
1045 data
.qpn
= cpu_to_be32(priv
->qp
->qp_num
);
1046 data
.mtu
= cpu_to_be32(IPOIB_CM_BUF_SIZE
);
1048 req
.primary_path
= pathrec
;
1049 req
.alternate_path
= NULL
;
1050 req
.service_id
= cpu_to_be64(IPOIB_CM_IETF_ID
| qpn
);
1051 req
.qp_num
= qp
->qp_num
;
1052 req
.qp_type
= qp
->qp_type
;
1053 req
.private_data
= &data
;
1054 req
.private_data_len
= sizeof data
;
1055 req
.flow_control
= 0;
1057 req
.starting_psn
= 0; /* FIXME */
1060 * Pick some arbitrary defaults here; we could make these
1061 * module parameters if anyone cared about setting them.
1063 req
.responder_resources
= 4;
1064 req
.remote_cm_response_timeout
= 20;
1065 req
.local_cm_response_timeout
= 20;
1066 req
.retry_count
= 0; /* RFC draft warns against retries */
1067 req
.rnr_retry_count
= 0; /* RFC draft warns against retries */
1068 req
.max_cm_retries
= 15;
1069 req
.srq
= ipoib_cm_has_srq(dev
);
1070 return ib_send_cm_req(id
, &req
);
1073 static int ipoib_cm_modify_tx_init(struct net_device
*dev
,
1074 struct ib_cm_id
*cm_id
, struct ib_qp
*qp
)
1076 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1077 struct ib_qp_attr qp_attr
;
1078 int qp_attr_mask
, ret
;
1079 ret
= ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &qp_attr
.pkey_index
);
1081 ipoib_warn(priv
, "pkey 0x%x not found: %d\n", priv
->pkey
, ret
);
1085 qp_attr
.qp_state
= IB_QPS_INIT
;
1086 qp_attr
.qp_access_flags
= IB_ACCESS_LOCAL_WRITE
;
1087 qp_attr
.port_num
= priv
->port
;
1088 qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1090 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
1092 ipoib_warn(priv
, "failed to modify tx QP to INIT: %d\n", ret
);
1098 static int ipoib_cm_tx_init(struct ipoib_cm_tx
*p
, u32 qpn
,
1099 struct ib_sa_path_rec
*pathrec
)
1101 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
1104 p
->tx_ring
= vzalloc(ipoib_sendq_size
* sizeof *p
->tx_ring
);
1106 ipoib_warn(priv
, "failed to allocate tx ring\n");
1111 p
->qp
= ipoib_cm_create_tx_qp(p
->dev
, p
);
1112 if (IS_ERR(p
->qp
)) {
1113 ret
= PTR_ERR(p
->qp
);
1114 ipoib_warn(priv
, "failed to allocate tx qp: %d\n", ret
);
1118 p
->id
= ib_create_cm_id(priv
->ca
, ipoib_cm_tx_handler
, p
);
1119 if (IS_ERR(p
->id
)) {
1120 ret
= PTR_ERR(p
->id
);
1121 ipoib_warn(priv
, "failed to create tx cm id: %d\n", ret
);
1125 ret
= ipoib_cm_modify_tx_init(p
->dev
, p
->id
, p
->qp
);
1127 ipoib_warn(priv
, "failed to modify tx qp to rtr: %d\n", ret
);
1131 ret
= ipoib_cm_send_req(p
->dev
, p
->id
, p
->qp
, qpn
, pathrec
);
1133 ipoib_warn(priv
, "failed to send cm req: %d\n", ret
);
1137 ipoib_dbg(priv
, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1138 p
->qp
->qp_num
, pathrec
->dgid
.raw
, qpn
);
1144 ib_destroy_cm_id(p
->id
);
1147 ib_destroy_qp(p
->qp
);
1155 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx
*p
)
1157 struct ipoib_dev_priv
*priv
= netdev_priv(p
->dev
);
1158 struct ipoib_cm_tx_buf
*tx_req
;
1159 unsigned long begin
;
1161 ipoib_dbg(priv
, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1162 p
->qp
? p
->qp
->qp_num
: 0, p
->tx_head
, p
->tx_tail
);
1165 ib_destroy_cm_id(p
->id
);
1168 /* Wait for all sends to complete */
1170 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1171 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
1172 ipoib_warn(priv
, "timing out; %d sends not completed\n",
1173 p
->tx_head
- p
->tx_tail
);
1183 while ((int) p
->tx_tail
- (int) p
->tx_head
< 0) {
1184 tx_req
= &p
->tx_ring
[p
->tx_tail
& (ipoib_sendq_size
- 1)];
1185 ib_dma_unmap_single(priv
->ca
, tx_req
->mapping
, tx_req
->skb
->len
,
1187 dev_kfree_skb_any(tx_req
->skb
);
1189 netif_tx_lock_bh(p
->dev
);
1190 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
1191 netif_queue_stopped(p
->dev
) &&
1192 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
1193 netif_wake_queue(p
->dev
);
1194 netif_tx_unlock_bh(p
->dev
);
1198 ib_destroy_qp(p
->qp
);
1204 static int ipoib_cm_tx_handler(struct ib_cm_id
*cm_id
,
1205 struct ib_cm_event
*event
)
1207 struct ipoib_cm_tx
*tx
= cm_id
->context
;
1208 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
1209 struct net_device
*dev
= priv
->dev
;
1210 struct ipoib_neigh
*neigh
;
1211 unsigned long flags
;
1214 switch (event
->event
) {
1215 case IB_CM_DREQ_RECEIVED
:
1216 ipoib_dbg(priv
, "DREQ received.\n");
1217 ib_send_cm_drep(cm_id
, NULL
, 0);
1219 case IB_CM_REP_RECEIVED
:
1220 ipoib_dbg(priv
, "REP received.\n");
1221 ret
= ipoib_cm_rep_handler(cm_id
, event
);
1223 ib_send_cm_rej(cm_id
, IB_CM_REJ_CONSUMER_DEFINED
,
1226 case IB_CM_REQ_ERROR
:
1227 case IB_CM_REJ_RECEIVED
:
1228 case IB_CM_TIMEWAIT_EXIT
:
1229 ipoib_dbg(priv
, "CM error %d.\n", event
->event
);
1230 netif_tx_lock_bh(dev
);
1231 spin_lock_irqsave(&priv
->lock
, flags
);
1236 ipoib_neigh_free(neigh
);
1241 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1242 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1243 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
1246 spin_unlock_irqrestore(&priv
->lock
, flags
);
1247 netif_tx_unlock_bh(dev
);
1256 struct ipoib_cm_tx
*ipoib_cm_create_tx(struct net_device
*dev
, struct ipoib_path
*path
,
1257 struct ipoib_neigh
*neigh
)
1259 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1260 struct ipoib_cm_tx
*tx
;
1262 tx
= kzalloc(sizeof *tx
, GFP_ATOMIC
);
1270 list_add(&tx
->list
, &priv
->cm
.start_list
);
1271 set_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
);
1272 queue_work(ipoib_workqueue
, &priv
->cm
.start_task
);
1276 void ipoib_cm_destroy_tx(struct ipoib_cm_tx
*tx
)
1278 struct ipoib_dev_priv
*priv
= netdev_priv(tx
->dev
);
1279 unsigned long flags
;
1280 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED
, &tx
->flags
)) {
1281 spin_lock_irqsave(&priv
->lock
, flags
);
1282 list_move(&tx
->list
, &priv
->cm
.reap_list
);
1283 queue_work(ipoib_workqueue
, &priv
->cm
.reap_task
);
1284 ipoib_dbg(priv
, "Reap connection for gid %pI6\n",
1285 tx
->neigh
->daddr
+ 4);
1287 spin_unlock_irqrestore(&priv
->lock
, flags
);
1291 static void ipoib_cm_tx_start(struct work_struct
*work
)
1293 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1295 struct net_device
*dev
= priv
->dev
;
1296 struct ipoib_neigh
*neigh
;
1297 struct ipoib_cm_tx
*p
;
1298 unsigned long flags
;
1301 struct ib_sa_path_rec pathrec
;
1304 netif_tx_lock_bh(dev
);
1305 spin_lock_irqsave(&priv
->lock
, flags
);
1307 while (!list_empty(&priv
->cm
.start_list
)) {
1308 p
= list_entry(priv
->cm
.start_list
.next
, typeof(*p
), list
);
1309 list_del_init(&p
->list
);
1311 qpn
= IPOIB_QPN(neigh
->daddr
);
1312 memcpy(&pathrec
, &p
->path
->pathrec
, sizeof pathrec
);
1314 spin_unlock_irqrestore(&priv
->lock
, flags
);
1315 netif_tx_unlock_bh(dev
);
1317 ret
= ipoib_cm_tx_init(p
, qpn
, &pathrec
);
1319 netif_tx_lock_bh(dev
);
1320 spin_lock_irqsave(&priv
->lock
, flags
);
1326 ipoib_neigh_free(neigh
);
1333 spin_unlock_irqrestore(&priv
->lock
, flags
);
1334 netif_tx_unlock_bh(dev
);
1337 static void ipoib_cm_tx_reap(struct work_struct
*work
)
1339 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1341 struct net_device
*dev
= priv
->dev
;
1342 struct ipoib_cm_tx
*p
;
1343 unsigned long flags
;
1345 netif_tx_lock_bh(dev
);
1346 spin_lock_irqsave(&priv
->lock
, flags
);
1348 while (!list_empty(&priv
->cm
.reap_list
)) {
1349 p
= list_entry(priv
->cm
.reap_list
.next
, typeof(*p
), list
);
1351 spin_unlock_irqrestore(&priv
->lock
, flags
);
1352 netif_tx_unlock_bh(dev
);
1353 ipoib_cm_tx_destroy(p
);
1354 netif_tx_lock_bh(dev
);
1355 spin_lock_irqsave(&priv
->lock
, flags
);
1358 spin_unlock_irqrestore(&priv
->lock
, flags
);
1359 netif_tx_unlock_bh(dev
);
1362 static void ipoib_cm_skb_reap(struct work_struct
*work
)
1364 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1366 struct net_device
*dev
= priv
->dev
;
1367 struct sk_buff
*skb
;
1368 unsigned long flags
;
1369 unsigned mtu
= priv
->mcast_mtu
;
1371 netif_tx_lock_bh(dev
);
1372 spin_lock_irqsave(&priv
->lock
, flags
);
1374 while ((skb
= skb_dequeue(&priv
->cm
.skb_queue
))) {
1375 spin_unlock_irqrestore(&priv
->lock
, flags
);
1376 netif_tx_unlock_bh(dev
);
1378 if (skb
->protocol
== htons(ETH_P_IP
))
1379 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1380 #if IS_ENABLED(CONFIG_IPV6)
1381 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1382 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1384 dev_kfree_skb_any(skb
);
1386 netif_tx_lock_bh(dev
);
1387 spin_lock_irqsave(&priv
->lock
, flags
);
1390 spin_unlock_irqrestore(&priv
->lock
, flags
);
1391 netif_tx_unlock_bh(dev
);
1394 void ipoib_cm_skb_too_long(struct net_device
*dev
, struct sk_buff
*skb
,
1397 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1398 int e
= skb_queue_empty(&priv
->cm
.skb_queue
);
1401 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
1403 skb_queue_tail(&priv
->cm
.skb_queue
, skb
);
1405 queue_work(ipoib_workqueue
, &priv
->cm
.skb_task
);
1408 static void ipoib_cm_rx_reap(struct work_struct
*work
)
1410 ipoib_cm_free_rx_reap_list(container_of(work
, struct ipoib_dev_priv
,
1411 cm
.rx_reap_task
)->dev
);
1414 static void ipoib_cm_stale_task(struct work_struct
*work
)
1416 struct ipoib_dev_priv
*priv
= container_of(work
, struct ipoib_dev_priv
,
1417 cm
.stale_task
.work
);
1418 struct ipoib_cm_rx
*p
;
1421 spin_lock_irq(&priv
->lock
);
1422 while (!list_empty(&priv
->cm
.passive_ids
)) {
1423 /* List is sorted by LRU, start from tail,
1424 * stop when we see a recently used entry */
1425 p
= list_entry(priv
->cm
.passive_ids
.prev
, typeof(*p
), list
);
1426 if (time_before_eq(jiffies
, p
->jiffies
+ IPOIB_CM_RX_TIMEOUT
))
1428 list_move(&p
->list
, &priv
->cm
.rx_error_list
);
1429 p
->state
= IPOIB_CM_RX_ERROR
;
1430 spin_unlock_irq(&priv
->lock
);
1431 ret
= ib_modify_qp(p
->qp
, &ipoib_cm_err_attr
, IB_QP_STATE
);
1433 ipoib_warn(priv
, "unable to move qp to error state: %d\n", ret
);
1434 spin_lock_irq(&priv
->lock
);
1437 if (!list_empty(&priv
->cm
.passive_ids
))
1438 queue_delayed_work(ipoib_workqueue
,
1439 &priv
->cm
.stale_task
, IPOIB_CM_RX_DELAY
);
1440 spin_unlock_irq(&priv
->lock
);
1444 static ssize_t
show_mode(struct device
*d
, struct device_attribute
*attr
,
1447 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(d
));
1449 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
1450 return sprintf(buf
, "connected\n");
1452 return sprintf(buf
, "datagram\n");
1455 static ssize_t
set_mode(struct device
*d
, struct device_attribute
*attr
,
1456 const char *buf
, size_t count
)
1458 struct net_device
*dev
= to_net_dev(d
);
1461 if (!rtnl_trylock())
1462 return restart_syscall();
1464 ret
= ipoib_set_mode(dev
, buf
);
1474 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
, show_mode
, set_mode
);
1476 int ipoib_cm_add_mode_attr(struct net_device
*dev
)
1478 return device_create_file(&dev
->dev
, &dev_attr_mode
);
1481 static void ipoib_cm_create_srq(struct net_device
*dev
, int max_sge
)
1483 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1484 struct ib_srq_init_attr srq_init_attr
= {
1485 .srq_type
= IB_SRQT_BASIC
,
1487 .max_wr
= ipoib_recvq_size
,
1492 priv
->cm
.srq
= ib_create_srq(priv
->pd
, &srq_init_attr
);
1493 if (IS_ERR(priv
->cm
.srq
)) {
1494 if (PTR_ERR(priv
->cm
.srq
) != -ENOSYS
)
1495 printk(KERN_WARNING
"%s: failed to allocate SRQ, error %ld\n",
1496 priv
->ca
->name
, PTR_ERR(priv
->cm
.srq
));
1497 priv
->cm
.srq
= NULL
;
1501 priv
->cm
.srq_ring
= vzalloc(ipoib_recvq_size
* sizeof *priv
->cm
.srq_ring
);
1502 if (!priv
->cm
.srq_ring
) {
1503 printk(KERN_WARNING
"%s: failed to allocate CM SRQ ring (%d entries)\n",
1504 priv
->ca
->name
, ipoib_recvq_size
);
1505 ib_destroy_srq(priv
->cm
.srq
);
1506 priv
->cm
.srq
= NULL
;
1512 int ipoib_cm_dev_init(struct net_device
*dev
)
1514 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1516 struct ib_device_attr attr
;
1518 INIT_LIST_HEAD(&priv
->cm
.passive_ids
);
1519 INIT_LIST_HEAD(&priv
->cm
.reap_list
);
1520 INIT_LIST_HEAD(&priv
->cm
.start_list
);
1521 INIT_LIST_HEAD(&priv
->cm
.rx_error_list
);
1522 INIT_LIST_HEAD(&priv
->cm
.rx_flush_list
);
1523 INIT_LIST_HEAD(&priv
->cm
.rx_drain_list
);
1524 INIT_LIST_HEAD(&priv
->cm
.rx_reap_list
);
1525 INIT_WORK(&priv
->cm
.start_task
, ipoib_cm_tx_start
);
1526 INIT_WORK(&priv
->cm
.reap_task
, ipoib_cm_tx_reap
);
1527 INIT_WORK(&priv
->cm
.skb_task
, ipoib_cm_skb_reap
);
1528 INIT_WORK(&priv
->cm
.rx_reap_task
, ipoib_cm_rx_reap
);
1529 INIT_DELAYED_WORK(&priv
->cm
.stale_task
, ipoib_cm_stale_task
);
1531 skb_queue_head_init(&priv
->cm
.skb_queue
);
1533 ret
= ib_query_device(priv
->ca
, &attr
);
1535 printk(KERN_WARNING
"ib_query_device() failed with %d\n", ret
);
1539 ipoib_dbg(priv
, "max_srq_sge=%d\n", attr
.max_srq_sge
);
1541 attr
.max_srq_sge
= min_t(int, IPOIB_CM_RX_SG
, attr
.max_srq_sge
);
1542 ipoib_cm_create_srq(dev
, attr
.max_srq_sge
);
1543 if (ipoib_cm_has_srq(dev
)) {
1544 priv
->cm
.max_cm_mtu
= attr
.max_srq_sge
* PAGE_SIZE
- 0x10;
1545 priv
->cm
.num_frags
= attr
.max_srq_sge
;
1546 ipoib_dbg(priv
, "max_cm_mtu = 0x%x, num_frags=%d\n",
1547 priv
->cm
.max_cm_mtu
, priv
->cm
.num_frags
);
1549 priv
->cm
.max_cm_mtu
= IPOIB_CM_MTU
;
1550 priv
->cm
.num_frags
= IPOIB_CM_RX_SG
;
1553 ipoib_cm_init_rx_wr(dev
, &priv
->cm
.rx_wr
, priv
->cm
.rx_sge
);
1555 if (ipoib_cm_has_srq(dev
)) {
1556 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
1557 if (!ipoib_cm_alloc_rx_skb(dev
, priv
->cm
.srq_ring
, i
,
1558 priv
->cm
.num_frags
- 1,
1559 priv
->cm
.srq_ring
[i
].mapping
)) {
1560 ipoib_warn(priv
, "failed to allocate "
1561 "receive buffer %d\n", i
);
1562 ipoib_cm_dev_cleanup(dev
);
1566 if (ipoib_cm_post_receive_srq(dev
, i
)) {
1567 ipoib_warn(priv
, "ipoib_cm_post_receive_srq "
1568 "failed for buf %d\n", i
);
1569 ipoib_cm_dev_cleanup(dev
);
1575 priv
->dev
->dev_addr
[0] = IPOIB_FLAGS_RC
;
1579 void ipoib_cm_dev_cleanup(struct net_device
*dev
)
1581 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1587 ipoib_dbg(priv
, "Cleanup ipoib connected mode.\n");
1589 ret
= ib_destroy_srq(priv
->cm
.srq
);
1591 ipoib_warn(priv
, "ib_destroy_srq failed: %d\n", ret
);
1593 priv
->cm
.srq
= NULL
;
1594 if (!priv
->cm
.srq_ring
)
1597 ipoib_cm_free_rx_ring(dev
, priv
->cm
.srq_ring
);
1598 priv
->cm
.srq_ring
= NULL
;