2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
38 #include <linux/delay.h>
39 #include <linux/dma-mapping.h>
41 #include <rdma/ib_cache.h>
43 #include <linux/tcp.h>
47 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
48 static int data_debug_level
;
50 module_param(data_debug_level
, int, 0644);
51 MODULE_PARM_DESC(data_debug_level
,
52 "Enable data path debug tracing if > 0");
55 static DEFINE_MUTEX(pkey_mutex
);
57 struct ipoib_ah
*ipoib_create_ah(struct net_device
*dev
,
58 struct ib_pd
*pd
, struct ib_ah_attr
*attr
)
62 ah
= kmalloc(sizeof *ah
, GFP_KERNEL
);
70 ah
->ah
= ib_create_ah(pd
, attr
);
75 ipoib_dbg(netdev_priv(dev
), "Created ah %p\n", ah
->ah
);
80 void ipoib_free_ah(struct kref
*kref
)
82 struct ipoib_ah
*ah
= container_of(kref
, struct ipoib_ah
, ref
);
83 struct ipoib_dev_priv
*priv
= netdev_priv(ah
->dev
);
87 spin_lock_irqsave(&priv
->lock
, flags
);
88 list_add_tail(&ah
->list
, &priv
->dead_ahs
);
89 spin_unlock_irqrestore(&priv
->lock
, flags
);
92 static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv
*priv
,
93 u64 mapping
[IPOIB_UD_RX_SG
])
95 if (ipoib_ud_need_sg(priv
->max_ib_mtu
)) {
96 ib_dma_unmap_single(priv
->ca
, mapping
[0], IPOIB_UD_HEAD_SIZE
,
98 ib_dma_unmap_page(priv
->ca
, mapping
[1], PAGE_SIZE
,
101 ib_dma_unmap_single(priv
->ca
, mapping
[0],
102 IPOIB_UD_BUF_SIZE(priv
->max_ib_mtu
),
106 static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv
*priv
,
110 if (ipoib_ud_need_sg(priv
->max_ib_mtu
)) {
111 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[0];
114 * There is only two buffers needed for max_payload = 4K,
115 * first buf size is IPOIB_UD_HEAD_SIZE
117 skb
->tail
+= IPOIB_UD_HEAD_SIZE
;
120 size
= length
- IPOIB_UD_HEAD_SIZE
;
123 skb
->data_len
+= size
;
124 skb
->truesize
+= size
;
126 skb_put(skb
, length
);
130 static int ipoib_ib_post_receive(struct net_device
*dev
, int id
)
132 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
133 struct ib_recv_wr
*bad_wr
;
136 priv
->rx_wr
.wr_id
= id
| IPOIB_OP_RECV
;
137 priv
->rx_sge
[0].addr
= priv
->rx_ring
[id
].mapping
[0];
138 priv
->rx_sge
[1].addr
= priv
->rx_ring
[id
].mapping
[1];
141 ret
= ib_post_recv(priv
->qp
, &priv
->rx_wr
, &bad_wr
);
143 ipoib_warn(priv
, "receive failed for buf %d (%d)\n", id
, ret
);
144 ipoib_ud_dma_unmap_rx(priv
, priv
->rx_ring
[id
].mapping
);
145 dev_kfree_skb_any(priv
->rx_ring
[id
].skb
);
146 priv
->rx_ring
[id
].skb
= NULL
;
152 static struct sk_buff
*ipoib_alloc_rx_skb(struct net_device
*dev
, int id
)
154 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
159 if (ipoib_ud_need_sg(priv
->max_ib_mtu
))
160 buf_size
= IPOIB_UD_HEAD_SIZE
;
162 buf_size
= IPOIB_UD_BUF_SIZE(priv
->max_ib_mtu
);
164 skb
= dev_alloc_skb(buf_size
+ 4);
169 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
170 * header. So we need 4 more bytes to get to 48 and align the
171 * IP header to a multiple of 16.
175 mapping
= priv
->rx_ring
[id
].mapping
;
176 mapping
[0] = ib_dma_map_single(priv
->ca
, skb
->data
, buf_size
,
178 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[0])))
181 if (ipoib_ud_need_sg(priv
->max_ib_mtu
)) {
182 struct page
*page
= alloc_page(GFP_ATOMIC
);
185 skb_fill_page_desc(skb
, 0, page
, 0, PAGE_SIZE
);
187 ib_dma_map_page(priv
->ca
, skb_shinfo(skb
)->frags
[0].page
,
188 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
189 if (unlikely(ib_dma_mapping_error(priv
->ca
, mapping
[1])))
193 priv
->rx_ring
[id
].skb
= skb
;
197 ib_dma_unmap_single(priv
->ca
, mapping
[0], buf_size
, DMA_FROM_DEVICE
);
199 dev_kfree_skb_any(skb
);
203 static int ipoib_ib_post_receives(struct net_device
*dev
)
205 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
208 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
209 if (!ipoib_alloc_rx_skb(dev
, i
)) {
210 ipoib_warn(priv
, "failed to allocate receive buffer %d\n", i
);
213 if (ipoib_ib_post_receive(dev
, i
)) {
214 ipoib_warn(priv
, "ipoib_ib_post_receive failed for buf %d\n", i
);
222 static void ipoib_ib_handle_rx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
224 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
225 unsigned int wr_id
= wc
->wr_id
& ~IPOIB_OP_RECV
;
227 u64 mapping
[IPOIB_UD_RX_SG
];
229 ipoib_dbg_data(priv
, "recv completion: id %d, status: %d\n",
232 if (unlikely(wr_id
>= ipoib_recvq_size
)) {
233 ipoib_warn(priv
, "recv completion event with wrid %d (> %d)\n",
234 wr_id
, ipoib_recvq_size
);
238 skb
= priv
->rx_ring
[wr_id
].skb
;
240 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
241 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
242 ipoib_warn(priv
, "failed recv event "
243 "(status=%d, wrid=%d vend_err %x)\n",
244 wc
->status
, wr_id
, wc
->vendor_err
);
245 ipoib_ud_dma_unmap_rx(priv
, priv
->rx_ring
[wr_id
].mapping
);
246 dev_kfree_skb_any(skb
);
247 priv
->rx_ring
[wr_id
].skb
= NULL
;
252 * Drop packets that this interface sent, ie multicast packets
253 * that the HCA has replicated.
255 if (wc
->slid
== priv
->local_lid
&& wc
->src_qp
== priv
->qp
->qp_num
)
258 memcpy(mapping
, priv
->rx_ring
[wr_id
].mapping
,
259 IPOIB_UD_RX_SG
* sizeof *mapping
);
262 * If we can't allocate a new RX buffer, dump
263 * this packet and reuse the old buffer.
265 if (unlikely(!ipoib_alloc_rx_skb(dev
, wr_id
))) {
266 ++dev
->stats
.rx_dropped
;
270 ipoib_dbg_data(priv
, "received %d bytes, SLID 0x%04x\n",
271 wc
->byte_len
, wc
->slid
);
273 ipoib_ud_dma_unmap_rx(priv
, mapping
);
274 ipoib_ud_skb_put_frags(priv
, skb
, wc
->byte_len
);
276 skb_pull(skb
, IB_GRH_BYTES
);
278 skb
->protocol
= ((struct ipoib_header
*) skb
->data
)->proto
;
279 skb_reset_mac_header(skb
);
280 skb_pull(skb
, IPOIB_ENCAP_LEN
);
282 dev
->last_rx
= jiffies
;
283 ++dev
->stats
.rx_packets
;
284 dev
->stats
.rx_bytes
+= skb
->len
;
287 /* XXX get correct PACKET_ type here */
288 skb
->pkt_type
= PACKET_HOST
;
290 if (test_bit(IPOIB_FLAG_CSUM
, &priv
->flags
) && likely(wc
->csum_ok
))
291 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
293 netif_receive_skb(skb
);
296 if (unlikely(ipoib_ib_post_receive(dev
, wr_id
)))
297 ipoib_warn(priv
, "ipoib_ib_post_receive failed "
298 "for buf %d\n", wr_id
);
301 static int ipoib_dma_map_tx(struct ib_device
*ca
,
302 struct ipoib_tx_buf
*tx_req
)
304 struct sk_buff
*skb
= tx_req
->skb
;
305 u64
*mapping
= tx_req
->mapping
;
309 if (skb_headlen(skb
)) {
310 mapping
[0] = ib_dma_map_single(ca
, skb
->data
, skb_headlen(skb
),
312 if (unlikely(ib_dma_mapping_error(ca
, mapping
[0])))
319 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; ++i
) {
320 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
321 mapping
[i
+ off
] = ib_dma_map_page(ca
, frag
->page
,
322 frag
->page_offset
, frag
->size
,
324 if (unlikely(ib_dma_mapping_error(ca
, mapping
[i
+ off
])))
331 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
- 1];
332 ib_dma_unmap_page(ca
, mapping
[i
- !off
], frag
->size
, DMA_TO_DEVICE
);
336 ib_dma_unmap_single(ca
, mapping
[0], skb_headlen(skb
), DMA_TO_DEVICE
);
341 static void ipoib_dma_unmap_tx(struct ib_device
*ca
,
342 struct ipoib_tx_buf
*tx_req
)
344 struct sk_buff
*skb
= tx_req
->skb
;
345 u64
*mapping
= tx_req
->mapping
;
349 if (skb_headlen(skb
)) {
350 ib_dma_unmap_single(ca
, mapping
[0], skb_headlen(skb
), DMA_TO_DEVICE
);
355 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; ++i
) {
356 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
357 ib_dma_unmap_page(ca
, mapping
[i
+ off
], frag
->size
,
362 static void ipoib_ib_handle_tx_wc(struct net_device
*dev
, struct ib_wc
*wc
)
364 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
365 unsigned int wr_id
= wc
->wr_id
;
366 struct ipoib_tx_buf
*tx_req
;
368 ipoib_dbg_data(priv
, "send completion: id %d, status: %d\n",
371 if (unlikely(wr_id
>= ipoib_sendq_size
)) {
372 ipoib_warn(priv
, "send completion event with wrid %d (> %d)\n",
373 wr_id
, ipoib_sendq_size
);
377 tx_req
= &priv
->tx_ring
[wr_id
];
379 ipoib_dma_unmap_tx(priv
->ca
, tx_req
);
381 ++dev
->stats
.tx_packets
;
382 dev
->stats
.tx_bytes
+= tx_req
->skb
->len
;
384 dev_kfree_skb_any(tx_req
->skb
);
387 if (unlikely(--priv
->tx_outstanding
== ipoib_sendq_size
>> 1) &&
388 netif_queue_stopped(dev
) &&
389 test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
))
390 netif_wake_queue(dev
);
392 if (wc
->status
!= IB_WC_SUCCESS
&&
393 wc
->status
!= IB_WC_WR_FLUSH_ERR
)
394 ipoib_warn(priv
, "failed send event "
395 "(status=%d, wrid=%d vend_err %x)\n",
396 wc
->status
, wr_id
, wc
->vendor_err
);
399 static int poll_tx(struct ipoib_dev_priv
*priv
)
403 n
= ib_poll_cq(priv
->send_cq
, MAX_SEND_CQE
, priv
->send_wc
);
404 for (i
= 0; i
< n
; ++i
)
405 ipoib_ib_handle_tx_wc(priv
->dev
, priv
->send_wc
+ i
);
407 return n
== MAX_SEND_CQE
;
410 int ipoib_poll(struct napi_struct
*napi
, int budget
)
412 struct ipoib_dev_priv
*priv
= container_of(napi
, struct ipoib_dev_priv
, napi
);
413 struct net_device
*dev
= priv
->dev
;
421 while (done
< budget
) {
422 int max
= (budget
- done
);
424 t
= min(IPOIB_NUM_WC
, max
);
425 n
= ib_poll_cq(priv
->recv_cq
, t
, priv
->ibwc
);
427 for (i
= 0; i
< n
; i
++) {
428 struct ib_wc
*wc
= priv
->ibwc
+ i
;
430 if (wc
->wr_id
& IPOIB_OP_RECV
) {
432 if (wc
->wr_id
& IPOIB_OP_CM
)
433 ipoib_cm_handle_rx_wc(dev
, wc
);
435 ipoib_ib_handle_rx_wc(dev
, wc
);
437 ipoib_cm_handle_tx_wc(priv
->dev
, wc
);
445 netif_rx_complete(dev
, napi
);
446 if (unlikely(ib_req_notify_cq(priv
->recv_cq
,
448 IB_CQ_REPORT_MISSED_EVENTS
)) &&
449 netif_rx_reschedule(dev
, napi
))
456 void ipoib_ib_completion(struct ib_cq
*cq
, void *dev_ptr
)
458 struct net_device
*dev
= dev_ptr
;
459 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
461 netif_rx_schedule(dev
, &priv
->napi
);
464 static inline int post_send(struct ipoib_dev_priv
*priv
,
466 struct ib_ah
*address
, u32 qpn
,
467 struct ipoib_tx_buf
*tx_req
,
468 void *head
, int hlen
)
470 struct ib_send_wr
*bad_wr
;
472 struct sk_buff
*skb
= tx_req
->skb
;
473 skb_frag_t
*frags
= skb_shinfo(skb
)->frags
;
474 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
475 u64
*mapping
= tx_req
->mapping
;
477 if (skb_headlen(skb
)) {
478 priv
->tx_sge
[0].addr
= mapping
[0];
479 priv
->tx_sge
[0].length
= skb_headlen(skb
);
484 for (i
= 0; i
< nr_frags
; ++i
) {
485 priv
->tx_sge
[i
+ off
].addr
= mapping
[i
+ off
];
486 priv
->tx_sge
[i
+ off
].length
= frags
[i
].size
;
488 priv
->tx_wr
.num_sge
= nr_frags
+ off
;
489 priv
->tx_wr
.wr_id
= wr_id
;
490 priv
->tx_wr
.wr
.ud
.remote_qpn
= qpn
;
491 priv
->tx_wr
.wr
.ud
.ah
= address
;
494 priv
->tx_wr
.wr
.ud
.mss
= skb_shinfo(skb
)->gso_size
;
495 priv
->tx_wr
.wr
.ud
.header
= head
;
496 priv
->tx_wr
.wr
.ud
.hlen
= hlen
;
497 priv
->tx_wr
.opcode
= IB_WR_LSO
;
499 priv
->tx_wr
.opcode
= IB_WR_SEND
;
501 return ib_post_send(priv
->qp
, &priv
->tx_wr
, &bad_wr
);
504 void ipoib_send(struct net_device
*dev
, struct sk_buff
*skb
,
505 struct ipoib_ah
*address
, u32 qpn
)
507 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
508 struct ipoib_tx_buf
*tx_req
;
512 if (skb_is_gso(skb
)) {
513 hlen
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
515 if (unlikely(!skb_pull(skb
, hlen
))) {
516 ipoib_warn(priv
, "linear data too small\n");
517 ++dev
->stats
.tx_dropped
;
518 ++dev
->stats
.tx_errors
;
519 dev_kfree_skb_any(skb
);
523 if (unlikely(skb
->len
> priv
->mcast_mtu
+ IPOIB_ENCAP_LEN
)) {
524 ipoib_warn(priv
, "packet len %d (> %d) too long to send, dropping\n",
525 skb
->len
, priv
->mcast_mtu
+ IPOIB_ENCAP_LEN
);
526 ++dev
->stats
.tx_dropped
;
527 ++dev
->stats
.tx_errors
;
528 ipoib_cm_skb_too_long(dev
, skb
, priv
->mcast_mtu
);
535 ipoib_dbg_data(priv
, "sending packet, length=%d address=%p qpn=0x%06x\n",
536 skb
->len
, address
, qpn
);
539 * We put the skb into the tx_ring _before_ we call post_send()
540 * because it's entirely possible that the completion handler will
541 * run before we execute anything after the post_send(). That
542 * means we have to make sure everything is properly recorded and
543 * our state is consistent before we call post_send().
545 tx_req
= &priv
->tx_ring
[priv
->tx_head
& (ipoib_sendq_size
- 1)];
547 if (unlikely(ipoib_dma_map_tx(priv
->ca
, tx_req
))) {
548 ++dev
->stats
.tx_errors
;
549 dev_kfree_skb_any(skb
);
553 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
554 priv
->tx_wr
.send_flags
|= IB_SEND_IP_CSUM
;
556 priv
->tx_wr
.send_flags
&= ~IB_SEND_IP_CSUM
;
558 if (unlikely(post_send(priv
, priv
->tx_head
& (ipoib_sendq_size
- 1),
559 address
->ah
, qpn
, tx_req
, phead
, hlen
))) {
560 ipoib_warn(priv
, "post_send failed\n");
561 ++dev
->stats
.tx_errors
;
562 ipoib_dma_unmap_tx(priv
->ca
, tx_req
);
563 dev_kfree_skb_any(skb
);
565 dev
->trans_start
= jiffies
;
567 address
->last_send
= priv
->tx_head
;
571 if (++priv
->tx_outstanding
== ipoib_sendq_size
) {
572 ipoib_dbg(priv
, "TX ring full, stopping kernel net queue\n");
573 netif_stop_queue(dev
);
577 if (unlikely(priv
->tx_outstanding
> MAX_SEND_CQE
))
581 static void __ipoib_reap_ah(struct net_device
*dev
)
583 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
584 struct ipoib_ah
*ah
, *tah
;
585 LIST_HEAD(remove_list
);
587 spin_lock_irq(&priv
->tx_lock
);
588 spin_lock(&priv
->lock
);
589 list_for_each_entry_safe(ah
, tah
, &priv
->dead_ahs
, list
)
590 if ((int) priv
->tx_tail
- (int) ah
->last_send
>= 0) {
592 ib_destroy_ah(ah
->ah
);
595 spin_unlock(&priv
->lock
);
596 spin_unlock_irq(&priv
->tx_lock
);
599 void ipoib_reap_ah(struct work_struct
*work
)
601 struct ipoib_dev_priv
*priv
=
602 container_of(work
, struct ipoib_dev_priv
, ah_reap_task
.work
);
603 struct net_device
*dev
= priv
->dev
;
605 __ipoib_reap_ah(dev
);
607 if (!test_bit(IPOIB_STOP_REAPER
, &priv
->flags
))
608 queue_delayed_work(ipoib_workqueue
, &priv
->ah_reap_task
,
609 round_jiffies_relative(HZ
));
612 int ipoib_ib_dev_open(struct net_device
*dev
)
614 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
617 if (ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &priv
->pkey_index
)) {
618 ipoib_warn(priv
, "P_Key 0x%04x not found\n", priv
->pkey
);
619 clear_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
);
622 set_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
);
624 ret
= ipoib_init_qp(dev
);
626 ipoib_warn(priv
, "ipoib_init_qp returned %d\n", ret
);
630 ret
= ipoib_ib_post_receives(dev
);
632 ipoib_warn(priv
, "ipoib_ib_post_receives returned %d\n", ret
);
633 ipoib_ib_dev_stop(dev
, 1);
637 ret
= ipoib_cm_dev_open(dev
);
639 ipoib_warn(priv
, "ipoib_cm_dev_open returned %d\n", ret
);
640 ipoib_ib_dev_stop(dev
, 1);
644 clear_bit(IPOIB_STOP_REAPER
, &priv
->flags
);
645 queue_delayed_work(ipoib_workqueue
, &priv
->ah_reap_task
,
646 round_jiffies_relative(HZ
));
648 set_bit(IPOIB_FLAG_INITIALIZED
, &priv
->flags
);
653 static void ipoib_pkey_dev_check_presence(struct net_device
*dev
)
655 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
658 if (ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &pkey_index
))
659 clear_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
);
661 set_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
);
664 int ipoib_ib_dev_up(struct net_device
*dev
)
666 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
668 ipoib_pkey_dev_check_presence(dev
);
670 if (!test_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
)) {
671 ipoib_dbg(priv
, "PKEY is not assigned.\n");
675 set_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
);
677 return ipoib_mcast_start_thread(dev
);
680 int ipoib_ib_dev_down(struct net_device
*dev
, int flush
)
682 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
684 ipoib_dbg(priv
, "downing ib_dev\n");
686 clear_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
);
687 netif_carrier_off(dev
);
689 /* Shutdown the P_Key thread if still active */
690 if (!test_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
)) {
691 mutex_lock(&pkey_mutex
);
692 set_bit(IPOIB_PKEY_STOP
, &priv
->flags
);
693 cancel_delayed_work(&priv
->pkey_poll_task
);
694 mutex_unlock(&pkey_mutex
);
696 flush_workqueue(ipoib_workqueue
);
699 ipoib_mcast_stop_thread(dev
, flush
);
700 ipoib_mcast_dev_flush(dev
);
702 ipoib_flush_paths(dev
);
707 static int recvs_pending(struct net_device
*dev
)
709 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
713 for (i
= 0; i
< ipoib_recvq_size
; ++i
)
714 if (priv
->rx_ring
[i
].skb
)
720 void ipoib_drain_cq(struct net_device
*dev
)
722 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
725 n
= ib_poll_cq(priv
->recv_cq
, IPOIB_NUM_WC
, priv
->ibwc
);
726 for (i
= 0; i
< n
; ++i
) {
728 * Convert any successful completions to flush
729 * errors to avoid passing packets up the
730 * stack after bringing the device down.
732 if (priv
->ibwc
[i
].status
== IB_WC_SUCCESS
)
733 priv
->ibwc
[i
].status
= IB_WC_WR_FLUSH_ERR
;
735 if (priv
->ibwc
[i
].wr_id
& IPOIB_OP_RECV
) {
736 if (priv
->ibwc
[i
].wr_id
& IPOIB_OP_CM
)
737 ipoib_cm_handle_rx_wc(dev
, priv
->ibwc
+ i
);
739 ipoib_ib_handle_rx_wc(dev
, priv
->ibwc
+ i
);
741 ipoib_cm_handle_tx_wc(dev
, priv
->ibwc
+ i
);
743 } while (n
== IPOIB_NUM_WC
);
745 while (poll_tx(priv
))
749 int ipoib_ib_dev_stop(struct net_device
*dev
, int flush
)
751 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
752 struct ib_qp_attr qp_attr
;
754 struct ipoib_tx_buf
*tx_req
;
757 clear_bit(IPOIB_FLAG_INITIALIZED
, &priv
->flags
);
759 ipoib_cm_dev_stop(dev
);
762 * Move our QP to the error state and then reinitialize in
763 * when all work requests have completed or have been flushed.
765 qp_attr
.qp_state
= IB_QPS_ERR
;
766 if (ib_modify_qp(priv
->qp
, &qp_attr
, IB_QP_STATE
))
767 ipoib_warn(priv
, "Failed to modify QP to ERROR state\n");
769 /* Wait for all sends and receives to complete */
772 while (priv
->tx_head
!= priv
->tx_tail
|| recvs_pending(dev
)) {
773 if (time_after(jiffies
, begin
+ 5 * HZ
)) {
774 ipoib_warn(priv
, "timing out; %d sends %d receives not completed\n",
775 priv
->tx_head
- priv
->tx_tail
, recvs_pending(dev
));
778 * assume the HW is wedged and just free up
779 * all our pending work requests.
781 while ((int) priv
->tx_tail
- (int) priv
->tx_head
< 0) {
782 tx_req
= &priv
->tx_ring
[priv
->tx_tail
&
783 (ipoib_sendq_size
- 1)];
784 ipoib_dma_unmap_tx(priv
->ca
, tx_req
);
785 dev_kfree_skb_any(tx_req
->skb
);
787 --priv
->tx_outstanding
;
790 for (i
= 0; i
< ipoib_recvq_size
; ++i
) {
791 struct ipoib_rx_buf
*rx_req
;
793 rx_req
= &priv
->rx_ring
[i
];
796 ipoib_ud_dma_unmap_rx(priv
,
797 priv
->rx_ring
[i
].mapping
);
798 dev_kfree_skb_any(rx_req
->skb
);
810 ipoib_dbg(priv
, "All sends and receives done.\n");
813 qp_attr
.qp_state
= IB_QPS_RESET
;
814 if (ib_modify_qp(priv
->qp
, &qp_attr
, IB_QP_STATE
))
815 ipoib_warn(priv
, "Failed to modify QP to RESET state\n");
817 /* Wait for all AHs to be reaped */
818 set_bit(IPOIB_STOP_REAPER
, &priv
->flags
);
819 cancel_delayed_work(&priv
->ah_reap_task
);
821 flush_workqueue(ipoib_workqueue
);
825 while (!list_empty(&priv
->dead_ahs
)) {
826 __ipoib_reap_ah(dev
);
828 if (time_after(jiffies
, begin
+ HZ
)) {
829 ipoib_warn(priv
, "timing out; will leak address handles\n");
836 ib_req_notify_cq(priv
->recv_cq
, IB_CQ_NEXT_COMP
);
841 int ipoib_ib_dev_init(struct net_device
*dev
, struct ib_device
*ca
, int port
)
843 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
849 if (ipoib_transport_dev_init(dev
, ca
)) {
850 printk(KERN_WARNING
"%s: ipoib_transport_dev_init failed\n", ca
->name
);
854 if (dev
->flags
& IFF_UP
) {
855 if (ipoib_ib_dev_open(dev
)) {
856 ipoib_transport_dev_cleanup(dev
);
864 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv
*priv
, int pkey_event
)
866 struct ipoib_dev_priv
*cpriv
;
867 struct net_device
*dev
= priv
->dev
;
870 mutex_lock(&priv
->vlan_mutex
);
873 * Flush any child interfaces too -- they might be up even if
874 * the parent is down.
876 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
)
877 __ipoib_ib_dev_flush(cpriv
, pkey_event
);
879 mutex_unlock(&priv
->vlan_mutex
);
881 if (!test_bit(IPOIB_FLAG_INITIALIZED
, &priv
->flags
)) {
882 ipoib_dbg(priv
, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
886 if (!test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
)) {
887 ipoib_dbg(priv
, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
892 if (ib_find_pkey(priv
->ca
, priv
->port
, priv
->pkey
, &new_index
)) {
893 clear_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
);
894 ipoib_ib_dev_down(dev
, 0);
895 ipoib_ib_dev_stop(dev
, 0);
896 if (ipoib_pkey_dev_delay_open(dev
))
900 /* restart QP only if P_Key index is changed */
901 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
) &&
902 new_index
== priv
->pkey_index
) {
903 ipoib_dbg(priv
, "Not flushing - P_Key index not changed.\n");
906 priv
->pkey_index
= new_index
;
909 ipoib_dbg(priv
, "flushing\n");
911 ipoib_ib_dev_down(dev
, 0);
914 ipoib_ib_dev_stop(dev
, 0);
915 ipoib_ib_dev_open(dev
);
919 * The device could have been brought down between the start and when
920 * we get here, don't bring it back up if it's not configured up
922 if (test_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
)) {
923 ipoib_ib_dev_up(dev
);
924 ipoib_mcast_restart_task(&priv
->restart_task
);
928 void ipoib_ib_dev_flush(struct work_struct
*work
)
930 struct ipoib_dev_priv
*priv
=
931 container_of(work
, struct ipoib_dev_priv
, flush_task
);
933 ipoib_dbg(priv
, "Flushing %s\n", priv
->dev
->name
);
934 __ipoib_ib_dev_flush(priv
, 0);
937 void ipoib_pkey_event(struct work_struct
*work
)
939 struct ipoib_dev_priv
*priv
=
940 container_of(work
, struct ipoib_dev_priv
, pkey_event_task
);
942 ipoib_dbg(priv
, "Flushing %s and restarting its QP\n", priv
->dev
->name
);
943 __ipoib_ib_dev_flush(priv
, 1);
946 void ipoib_ib_dev_cleanup(struct net_device
*dev
)
948 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
950 ipoib_dbg(priv
, "cleaning up ib_dev\n");
952 ipoib_mcast_stop_thread(dev
, 1);
953 ipoib_mcast_dev_flush(dev
);
955 ipoib_transport_dev_cleanup(dev
);
959 * Delayed P_Key Assigment Interim Support
961 * The following is initial implementation of delayed P_Key assigment
962 * mechanism. It is using the same approach implemented for the multicast
963 * group join. The single goal of this implementation is to quickly address
964 * Bug #2507. This implementation will probably be removed when the P_Key
965 * change async notification is available.
968 void ipoib_pkey_poll(struct work_struct
*work
)
970 struct ipoib_dev_priv
*priv
=
971 container_of(work
, struct ipoib_dev_priv
, pkey_poll_task
.work
);
972 struct net_device
*dev
= priv
->dev
;
974 ipoib_pkey_dev_check_presence(dev
);
976 if (test_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
))
979 mutex_lock(&pkey_mutex
);
980 if (!test_bit(IPOIB_PKEY_STOP
, &priv
->flags
))
981 queue_delayed_work(ipoib_workqueue
,
982 &priv
->pkey_poll_task
,
984 mutex_unlock(&pkey_mutex
);
988 int ipoib_pkey_dev_delay_open(struct net_device
*dev
)
990 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
992 /* Look for the interface pkey value in the IB Port P_Key table and */
993 /* set the interface pkey assigment flag */
994 ipoib_pkey_dev_check_presence(dev
);
996 /* P_Key value not assigned yet - start polling */
997 if (!test_bit(IPOIB_PKEY_ASSIGNED
, &priv
->flags
)) {
998 mutex_lock(&pkey_mutex
);
999 clear_bit(IPOIB_PKEY_STOP
, &priv
->flags
);
1000 queue_delayed_work(ipoib_workqueue
,
1001 &priv
->pkey_poll_task
,
1003 mutex_unlock(&pkey_mutex
);