2 * Copyright (c) 2007 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
34 #include <linux/mlx4/cq.h>
35 #include <linux/mlx4/qp.h>
36 #include <linux/skbuff.h>
37 #include <linux/if_ether.h>
38 #include <linux/if_vlan.h>
39 #include <linux/vmalloc.h>
44 static int mlx4_en_get_frag_header(struct skb_frag_struct
*frags
, void **mac_hdr
,
45 void **ip_hdr
, void **tcpudp_hdr
,
46 u64
*hdr_flags
, void *priv
)
48 *mac_hdr
= page_address(frags
->page
) + frags
->page_offset
;
49 *ip_hdr
= *mac_hdr
+ ETH_HLEN
;
50 *tcpudp_hdr
= (struct tcphdr
*)(*ip_hdr
+ sizeof(struct iphdr
));
51 *hdr_flags
= LRO_IPV4
| LRO_TCP
;
56 static int mlx4_en_alloc_frag(struct mlx4_en_priv
*priv
,
57 struct mlx4_en_rx_desc
*rx_desc
,
58 struct skb_frag_struct
*skb_frags
,
59 struct mlx4_en_rx_alloc
*ring_alloc
,
62 struct mlx4_en_dev
*mdev
= priv
->mdev
;
63 struct mlx4_en_frag_info
*frag_info
= &priv
->frag_info
[i
];
64 struct mlx4_en_rx_alloc
*page_alloc
= &ring_alloc
[i
];
68 if (page_alloc
->offset
== frag_info
->last_offset
) {
69 /* Allocate new page */
70 page
= alloc_pages(GFP_ATOMIC
| __GFP_COMP
, MLX4_EN_ALLOC_ORDER
);
74 skb_frags
[i
].page
= page_alloc
->page
;
75 skb_frags
[i
].page_offset
= page_alloc
->offset
;
76 page_alloc
->page
= page
;
77 page_alloc
->offset
= frag_info
->frag_align
;
79 page
= page_alloc
->page
;
82 skb_frags
[i
].page
= page
;
83 skb_frags
[i
].page_offset
= page_alloc
->offset
;
84 page_alloc
->offset
+= frag_info
->frag_stride
;
86 dma
= pci_map_single(mdev
->pdev
, page_address(skb_frags
[i
].page
) +
87 skb_frags
[i
].page_offset
, frag_info
->frag_size
,
89 rx_desc
->data
[i
].addr
= cpu_to_be64(dma
);
93 static int mlx4_en_init_allocator(struct mlx4_en_priv
*priv
,
94 struct mlx4_en_rx_ring
*ring
)
96 struct mlx4_en_rx_alloc
*page_alloc
;
99 for (i
= 0; i
< priv
->num_frags
; i
++) {
100 page_alloc
= &ring
->page_alloc
[i
];
101 page_alloc
->page
= alloc_pages(GFP_ATOMIC
| __GFP_COMP
,
102 MLX4_EN_ALLOC_ORDER
);
103 if (!page_alloc
->page
)
106 page_alloc
->offset
= priv
->frag_info
[i
].frag_align
;
107 en_dbg(DRV
, priv
, "Initialized allocator:%d with page:%p\n",
108 i
, page_alloc
->page
);
114 page_alloc
= &ring
->page_alloc
[i
];
115 put_page(page_alloc
->page
);
116 page_alloc
->page
= NULL
;
121 static void mlx4_en_destroy_allocator(struct mlx4_en_priv
*priv
,
122 struct mlx4_en_rx_ring
*ring
)
124 struct mlx4_en_rx_alloc
*page_alloc
;
127 for (i
= 0; i
< priv
->num_frags
; i
++) {
128 page_alloc
= &ring
->page_alloc
[i
];
129 en_dbg(DRV
, priv
, "Freeing allocator:%d count:%d\n",
130 i
, page_count(page_alloc
->page
));
132 put_page(page_alloc
->page
);
133 page_alloc
->page
= NULL
;
138 static void mlx4_en_init_rx_desc(struct mlx4_en_priv
*priv
,
139 struct mlx4_en_rx_ring
*ring
, int index
)
141 struct mlx4_en_rx_desc
*rx_desc
= ring
->buf
+ ring
->stride
* index
;
142 struct skb_frag_struct
*skb_frags
= ring
->rx_info
+
143 (index
<< priv
->log_rx_info
);
147 /* Set size and memtype fields */
148 for (i
= 0; i
< priv
->num_frags
; i
++) {
149 skb_frags
[i
].size
= priv
->frag_info
[i
].frag_size
;
150 rx_desc
->data
[i
].byte_count
=
151 cpu_to_be32(priv
->frag_info
[i
].frag_size
);
152 rx_desc
->data
[i
].lkey
= cpu_to_be32(priv
->mdev
->mr
.key
);
155 /* If the number of used fragments does not fill up the ring stride,
156 * remaining (unused) fragments must be padded with null address/size
157 * and a special memory key */
158 possible_frags
= (ring
->stride
- sizeof(struct mlx4_en_rx_desc
)) / DS_SIZE
;
159 for (i
= priv
->num_frags
; i
< possible_frags
; i
++) {
160 rx_desc
->data
[i
].byte_count
= 0;
161 rx_desc
->data
[i
].lkey
= cpu_to_be32(MLX4_EN_MEMTYPE_PAD
);
162 rx_desc
->data
[i
].addr
= 0;
167 static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv
*priv
,
168 struct mlx4_en_rx_ring
*ring
, int index
)
170 struct mlx4_en_rx_desc
*rx_desc
= ring
->buf
+ (index
* ring
->stride
);
171 struct skb_frag_struct
*skb_frags
= ring
->rx_info
+
172 (index
<< priv
->log_rx_info
);
175 for (i
= 0; i
< priv
->num_frags
; i
++)
176 if (mlx4_en_alloc_frag(priv
, rx_desc
, skb_frags
, ring
->page_alloc
, i
))
183 put_page(skb_frags
[i
].page
);
187 static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring
*ring
)
189 *ring
->wqres
.db
.db
= cpu_to_be32(ring
->prod
& 0xffff);
192 static void mlx4_en_free_rx_desc(struct mlx4_en_priv
*priv
,
193 struct mlx4_en_rx_ring
*ring
,
196 struct mlx4_en_dev
*mdev
= priv
->mdev
;
197 struct skb_frag_struct
*skb_frags
;
198 struct mlx4_en_rx_desc
*rx_desc
= ring
->buf
+ (index
<< ring
->log_stride
);
202 skb_frags
= ring
->rx_info
+ (index
<< priv
->log_rx_info
);
203 for (nr
= 0; nr
< priv
->num_frags
; nr
++) {
204 en_dbg(DRV
, priv
, "Freeing fragment:%d\n", nr
);
205 dma
= be64_to_cpu(rx_desc
->data
[nr
].addr
);
207 en_dbg(DRV
, priv
, "Unmaping buffer at dma:0x%llx\n", (u64
) dma
);
208 pci_unmap_single(mdev
->pdev
, dma
, skb_frags
[nr
].size
,
210 put_page(skb_frags
[nr
].page
);
214 static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv
*priv
)
216 struct mlx4_en_rx_ring
*ring
;
221 for (buf_ind
= 0; buf_ind
< priv
->prof
->rx_ring_size
; buf_ind
++) {
222 for (ring_ind
= 0; ring_ind
< priv
->rx_ring_num
; ring_ind
++) {
223 ring
= &priv
->rx_ring
[ring_ind
];
225 if (mlx4_en_prepare_rx_desc(priv
, ring
,
226 ring
->actual_size
)) {
227 if (ring
->actual_size
< MLX4_EN_MIN_RX_SIZE
) {
228 en_err(priv
, "Failed to allocate "
229 "enough rx buffers\n");
232 new_size
= rounddown_pow_of_two(ring
->actual_size
);
233 en_warn(priv
, "Only %d buffers allocated "
234 "reducing ring size to %d",
235 ring
->actual_size
, new_size
);
246 for (ring_ind
= 0; ring_ind
< priv
->rx_ring_num
; ring_ind
++) {
247 ring
= &priv
->rx_ring
[ring_ind
];
248 while (ring
->actual_size
> new_size
) {
251 mlx4_en_free_rx_desc(priv
, ring
, ring
->actual_size
);
253 ring
->size_mask
= ring
->actual_size
- 1;
259 static void mlx4_en_free_rx_buf(struct mlx4_en_priv
*priv
,
260 struct mlx4_en_rx_ring
*ring
)
264 en_dbg(DRV
, priv
, "Freeing Rx buf - cons:%d prod:%d\n",
265 ring
->cons
, ring
->prod
);
267 /* Unmap and free Rx buffers */
268 BUG_ON((u32
) (ring
->prod
- ring
->cons
) > ring
->actual_size
);
269 while (ring
->cons
!= ring
->prod
) {
270 index
= ring
->cons
& ring
->size_mask
;
271 en_dbg(DRV
, priv
, "Processing descriptor:%d\n", index
);
272 mlx4_en_free_rx_desc(priv
, ring
, index
);
277 int mlx4_en_create_rx_ring(struct mlx4_en_priv
*priv
,
278 struct mlx4_en_rx_ring
*ring
, u32 size
, u16 stride
)
280 struct mlx4_en_dev
*mdev
= priv
->mdev
;
288 ring
->size_mask
= size
- 1;
289 ring
->stride
= stride
;
290 ring
->log_stride
= ffs(ring
->stride
) - 1;
291 ring
->buf_size
= ring
->size
* ring
->stride
+ TXBB_SIZE
;
293 tmp
= size
* roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS
*
294 sizeof(struct skb_frag_struct
));
295 ring
->rx_info
= vmalloc(tmp
);
296 if (!ring
->rx_info
) {
297 en_err(priv
, "Failed allocating rx_info ring\n");
300 en_dbg(DRV
, priv
, "Allocated rx_info ring at addr:%p size:%d\n",
303 err
= mlx4_alloc_hwq_res(mdev
->dev
, &ring
->wqres
,
304 ring
->buf_size
, 2 * PAGE_SIZE
);
308 err
= mlx4_en_map_buffer(&ring
->wqres
.buf
);
310 en_err(priv
, "Failed to map RX buffer\n");
313 ring
->buf
= ring
->wqres
.buf
.direct
.buf
;
315 /* Configure lro mngr */
316 memset(&ring
->lro
, 0, sizeof(struct net_lro_mgr
));
317 ring
->lro
.dev
= priv
->dev
;
318 ring
->lro
.features
= LRO_F_NAPI
;
319 ring
->lro
.frag_align_pad
= NET_IP_ALIGN
;
320 ring
->lro
.ip_summed
= CHECKSUM_UNNECESSARY
;
321 ring
->lro
.ip_summed_aggr
= CHECKSUM_UNNECESSARY
;
322 ring
->lro
.max_desc
= mdev
->profile
.num_lro
;
323 ring
->lro
.max_aggr
= MAX_SKB_FRAGS
;
324 ring
->lro
.lro_arr
= kzalloc(mdev
->profile
.num_lro
*
325 sizeof(struct net_lro_desc
),
327 if (!ring
->lro
.lro_arr
) {
328 en_err(priv
, "Failed to allocate lro array\n");
331 ring
->lro
.get_frag_header
= mlx4_en_get_frag_header
;
336 mlx4_en_unmap_buffer(&ring
->wqres
.buf
);
338 mlx4_free_hwq_res(mdev
->dev
, &ring
->wqres
, ring
->buf_size
);
340 vfree(ring
->rx_info
);
341 ring
->rx_info
= NULL
;
345 int mlx4_en_activate_rx_rings(struct mlx4_en_priv
*priv
)
347 struct mlx4_en_rx_ring
*ring
;
351 int stride
= roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc
) +
352 DS_SIZE
* priv
->num_frags
);
354 for (ring_ind
= 0; ring_ind
< priv
->rx_ring_num
; ring_ind
++) {
355 ring
= &priv
->rx_ring
[ring_ind
];
359 ring
->actual_size
= 0;
360 ring
->cqn
= priv
->rx_cq
[ring_ind
].mcq
.cqn
;
362 ring
->stride
= stride
;
363 if (ring
->stride
<= TXBB_SIZE
)
364 ring
->buf
+= TXBB_SIZE
;
366 ring
->log_stride
= ffs(ring
->stride
) - 1;
367 ring
->buf_size
= ring
->size
* ring
->stride
;
369 memset(ring
->buf
, 0, ring
->buf_size
);
370 mlx4_en_update_rx_prod_db(ring
);
372 /* Initailize all descriptors */
373 for (i
= 0; i
< ring
->size
; i
++)
374 mlx4_en_init_rx_desc(priv
, ring
, i
);
376 /* Initialize page allocators */
377 err
= mlx4_en_init_allocator(priv
, ring
);
379 en_err(priv
, "Failed initializing ring allocator\n");
384 err
= mlx4_en_fill_rx_buffers(priv
);
388 for (ring_ind
= 0; ring_ind
< priv
->rx_ring_num
; ring_ind
++) {
389 ring
= &priv
->rx_ring
[ring_ind
];
391 mlx4_en_update_rx_prod_db(ring
);
397 for (ring_ind
= 0; ring_ind
< priv
->rx_ring_num
; ring_ind
++)
398 mlx4_en_free_rx_buf(priv
, &priv
->rx_ring
[ring_ind
]);
400 ring_ind
= priv
->rx_ring_num
- 1;
402 while (ring_ind
>= 0) {
403 mlx4_en_destroy_allocator(priv
, &priv
->rx_ring
[ring_ind
]);
409 void mlx4_en_destroy_rx_ring(struct mlx4_en_priv
*priv
,
410 struct mlx4_en_rx_ring
*ring
)
412 struct mlx4_en_dev
*mdev
= priv
->mdev
;
414 kfree(ring
->lro
.lro_arr
);
415 mlx4_en_unmap_buffer(&ring
->wqres
.buf
);
416 mlx4_free_hwq_res(mdev
->dev
, &ring
->wqres
, ring
->buf_size
+ TXBB_SIZE
);
417 vfree(ring
->rx_info
);
418 ring
->rx_info
= NULL
;
421 void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv
*priv
,
422 struct mlx4_en_rx_ring
*ring
)
424 mlx4_en_free_rx_buf(priv
, ring
);
425 if (ring
->stride
<= TXBB_SIZE
)
426 ring
->buf
-= TXBB_SIZE
;
427 mlx4_en_destroy_allocator(priv
, ring
);
431 /* Unmap a completed descriptor and free unused pages */
432 static int mlx4_en_complete_rx_desc(struct mlx4_en_priv
*priv
,
433 struct mlx4_en_rx_desc
*rx_desc
,
434 struct skb_frag_struct
*skb_frags
,
435 struct skb_frag_struct
*skb_frags_rx
,
436 struct mlx4_en_rx_alloc
*page_alloc
,
439 struct mlx4_en_dev
*mdev
= priv
->mdev
;
440 struct mlx4_en_frag_info
*frag_info
;
444 /* Collect used fragments while replacing them in the HW descirptors */
445 for (nr
= 0; nr
< priv
->num_frags
; nr
++) {
446 frag_info
= &priv
->frag_info
[nr
];
447 if (length
<= frag_info
->frag_prefix_size
)
450 /* Save page reference in skb */
451 skb_frags_rx
[nr
].page
= skb_frags
[nr
].page
;
452 skb_frags_rx
[nr
].size
= skb_frags
[nr
].size
;
453 skb_frags_rx
[nr
].page_offset
= skb_frags
[nr
].page_offset
;
454 dma
= be64_to_cpu(rx_desc
->data
[nr
].addr
);
456 /* Allocate a replacement page */
457 if (mlx4_en_alloc_frag(priv
, rx_desc
, skb_frags
, page_alloc
, nr
))
461 pci_unmap_single(mdev
->pdev
, dma
, skb_frags
[nr
].size
,
464 /* Adjust size of last fragment to match actual length */
466 skb_frags_rx
[nr
- 1].size
= length
-
467 priv
->frag_info
[nr
- 1].frag_prefix_size
;
471 /* Drop all accumulated fragments (which have already been replaced in
472 * the descriptor) of this packet; remaining fragments are reused... */
475 put_page(skb_frags_rx
[nr
].page
);
481 static struct sk_buff
*mlx4_en_rx_skb(struct mlx4_en_priv
*priv
,
482 struct mlx4_en_rx_desc
*rx_desc
,
483 struct skb_frag_struct
*skb_frags
,
484 struct mlx4_en_rx_alloc
*page_alloc
,
487 struct mlx4_en_dev
*mdev
= priv
->mdev
;
493 skb
= dev_alloc_skb(SMALL_PACKET_SIZE
+ NET_IP_ALIGN
);
495 en_dbg(RX_ERR
, priv
, "Failed allocating skb\n");
498 skb
->dev
= priv
->dev
;
499 skb_reserve(skb
, NET_IP_ALIGN
);
501 skb
->truesize
= length
+ sizeof(struct sk_buff
);
503 /* Get pointer to first fragment so we could copy the headers into the
504 * (linear part of the) skb */
505 va
= page_address(skb_frags
[0].page
) + skb_frags
[0].page_offset
;
507 if (length
<= SMALL_PACKET_SIZE
) {
508 /* We are copying all relevant data to the skb - temporarily
509 * synch buffers for the copy */
510 dma
= be64_to_cpu(rx_desc
->data
[0].addr
);
511 dma_sync_single_range_for_cpu(&mdev
->pdev
->dev
, dma
, 0,
512 length
, DMA_FROM_DEVICE
);
513 skb_copy_to_linear_data(skb
, va
, length
);
514 dma_sync_single_range_for_device(&mdev
->pdev
->dev
, dma
, 0,
515 length
, DMA_FROM_DEVICE
);
519 /* Move relevant fragments to skb */
520 used_frags
= mlx4_en_complete_rx_desc(priv
, rx_desc
, skb_frags
,
521 skb_shinfo(skb
)->frags
,
523 if (unlikely(!used_frags
)) {
527 skb_shinfo(skb
)->nr_frags
= used_frags
;
529 /* Copy headers into the skb linear buffer */
530 memcpy(skb
->data
, va
, HEADER_COPY_SIZE
);
531 skb
->tail
+= HEADER_COPY_SIZE
;
533 /* Skip headers in first fragment */
534 skb_shinfo(skb
)->frags
[0].page_offset
+= HEADER_COPY_SIZE
;
536 /* Adjust size of first fragment */
537 skb_shinfo(skb
)->frags
[0].size
-= HEADER_COPY_SIZE
;
538 skb
->data_len
= length
- HEADER_COPY_SIZE
;
544 int mlx4_en_process_rx_cq(struct net_device
*dev
, struct mlx4_en_cq
*cq
, int budget
)
546 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
547 struct mlx4_cqe
*cqe
;
548 struct mlx4_en_rx_ring
*ring
= &priv
->rx_ring
[cq
->ring
];
549 struct skb_frag_struct
*skb_frags
;
550 struct skb_frag_struct lro_frags
[MLX4_EN_MAX_RX_FRAGS
];
551 struct mlx4_en_rx_desc
*rx_desc
;
562 /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
563 * descriptor offset can be deduced from the CQE index instead of
564 * reading 'cqe->index' */
565 index
= cq
->mcq
.cons_index
& ring
->size_mask
;
566 cqe
= &cq
->buf
[index
];
568 /* Process all completed CQEs */
569 while (XNOR(cqe
->owner_sr_opcode
& MLX4_CQE_OWNER_MASK
,
570 cq
->mcq
.cons_index
& cq
->size
)) {
572 skb_frags
= ring
->rx_info
+ (index
<< priv
->log_rx_info
);
573 rx_desc
= ring
->buf
+ (index
<< ring
->log_stride
);
576 * make sure we read the CQE after we read the ownership bit
580 /* Drop packet on bad receive or bad checksum */
581 if (unlikely((cqe
->owner_sr_opcode
& MLX4_CQE_OPCODE_MASK
) ==
582 MLX4_CQE_OPCODE_ERROR
)) {
583 en_err(priv
, "CQE completed in error - vendor "
584 "syndrom:%d syndrom:%d\n",
585 ((struct mlx4_err_cqe
*) cqe
)->vendor_err_syndrome
,
586 ((struct mlx4_err_cqe
*) cqe
)->syndrome
);
589 if (unlikely(cqe
->badfcs_enc
& MLX4_CQE_BAD_FCS
)) {
590 en_dbg(RX_ERR
, priv
, "Accepted frame with bad FCS\n");
595 * Packet is OK - process it.
597 length
= be32_to_cpu(cqe
->byte_cnt
);
598 ring
->bytes
+= length
;
601 if (likely(priv
->rx_csum
)) {
602 if ((cqe
->status
& cpu_to_be16(MLX4_CQE_STATUS_IPOK
)) &&
603 (cqe
->checksum
== cpu_to_be16(0xffff))) {
604 priv
->port_stats
.rx_chksum_good
++;
605 /* This packet is eligible for LRO if it is:
606 * - DIX Ethernet (type interpretation)
608 * - without IP options
609 * - not an IP fragment */
610 if (mlx4_en_can_lro(cqe
->status
) &&
611 dev
->features
& NETIF_F_LRO
) {
613 nr
= mlx4_en_complete_rx_desc(
615 skb_frags
, lro_frags
,
616 ring
->page_alloc
, length
);
620 if (priv
->vlgrp
&& (cqe
->vlan_my_qpn
&
621 cpu_to_be32(MLX4_CQE_VLAN_PRESENT_MASK
))) {
622 lro_vlan_hwaccel_receive_frags(
623 &ring
->lro
, lro_frags
,
626 be16_to_cpu(cqe
->sl_vid
),
629 lro_receive_frags(&ring
->lro
,
638 /* LRO not possible, complete processing here */
639 ip_summed
= CHECKSUM_UNNECESSARY
;
640 INC_PERF_COUNTER(priv
->pstats
.lro_misses
);
642 ip_summed
= CHECKSUM_NONE
;
643 priv
->port_stats
.rx_chksum_none
++;
646 ip_summed
= CHECKSUM_NONE
;
647 priv
->port_stats
.rx_chksum_none
++;
650 skb
= mlx4_en_rx_skb(priv
, rx_desc
, skb_frags
,
651 ring
->page_alloc
, length
);
653 priv
->stats
.rx_dropped
++;
657 skb
->ip_summed
= ip_summed
;
658 skb
->protocol
= eth_type_trans(skb
, dev
);
659 skb_record_rx_queue(skb
, cq
->ring
);
661 /* Push it up the stack */
662 if (priv
->vlgrp
&& (be32_to_cpu(cqe
->vlan_my_qpn
) &
663 MLX4_CQE_VLAN_PRESENT_MASK
)) {
664 vlan_hwaccel_receive_skb(skb
, priv
->vlgrp
,
665 be16_to_cpu(cqe
->sl_vid
));
667 netif_receive_skb(skb
);
670 ++cq
->mcq
.cons_index
;
671 index
= (cq
->mcq
.cons_index
) & ring
->size_mask
;
672 cqe
= &cq
->buf
[index
];
673 if (++polled
== budget
) {
674 /* We are here because we reached the NAPI budget -
675 * flush only pending LRO sessions */
676 lro_flush_all(&ring
->lro
);
681 /* If CQ is empty flush all LRO sessions unconditionally */
682 lro_flush_all(&ring
->lro
);
685 AVG_PERF_COUNTER(priv
->pstats
.rx_coal_avg
, polled
);
686 mlx4_cq_set_ci(&cq
->mcq
);
687 wmb(); /* ensure HW sees CQ consumer before we post new buffers */
688 ring
->cons
= cq
->mcq
.cons_index
;
689 ring
->prod
+= polled
; /* Polled descriptors were realocated in place */
690 mlx4_en_update_rx_prod_db(ring
);
695 void mlx4_en_rx_irq(struct mlx4_cq
*mcq
)
697 struct mlx4_en_cq
*cq
= container_of(mcq
, struct mlx4_en_cq
, mcq
);
698 struct mlx4_en_priv
*priv
= netdev_priv(cq
->dev
);
701 napi_schedule(&cq
->napi
);
703 mlx4_en_arm_cq(priv
, cq
);
706 /* Rx CQ polling - called by NAPI */
707 int mlx4_en_poll_rx_cq(struct napi_struct
*napi
, int budget
)
709 struct mlx4_en_cq
*cq
= container_of(napi
, struct mlx4_en_cq
, napi
);
710 struct net_device
*dev
= cq
->dev
;
711 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
714 done
= mlx4_en_process_rx_cq(dev
, cq
, budget
);
716 /* If we used up all the quota - we're probably not done yet... */
718 INC_PERF_COUNTER(priv
->pstats
.napi_quota
);
722 mlx4_en_arm_cq(priv
, cq
);
728 /* Calculate the last offset position that accomodates a full fragment
729 * (assuming fagment size = stride-align) */
730 static int mlx4_en_last_alloc_offset(struct mlx4_en_priv
*priv
, u16 stride
, u16 align
)
732 u16 res
= MLX4_EN_ALLOC_SIZE
% stride
;
733 u16 offset
= MLX4_EN_ALLOC_SIZE
- stride
- res
+ align
;
735 en_dbg(DRV
, priv
, "Calculated last offset for stride:%d align:%d "
736 "res:%d offset:%d\n", stride
, align
, res
, offset
);
741 static int frag_sizes
[] = {
748 void mlx4_en_calc_rx_buf(struct net_device
*dev
)
750 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
751 int eff_mtu
= dev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_LLC_SNAP_SIZE
;
755 while (buf_size
< eff_mtu
) {
756 priv
->frag_info
[i
].frag_size
=
757 (eff_mtu
> buf_size
+ frag_sizes
[i
]) ?
758 frag_sizes
[i
] : eff_mtu
- buf_size
;
759 priv
->frag_info
[i
].frag_prefix_size
= buf_size
;
761 priv
->frag_info
[i
].frag_align
= NET_IP_ALIGN
;
762 priv
->frag_info
[i
].frag_stride
=
763 ALIGN(frag_sizes
[i
] + NET_IP_ALIGN
, SMP_CACHE_BYTES
);
765 priv
->frag_info
[i
].frag_align
= 0;
766 priv
->frag_info
[i
].frag_stride
=
767 ALIGN(frag_sizes
[i
], SMP_CACHE_BYTES
);
769 priv
->frag_info
[i
].last_offset
= mlx4_en_last_alloc_offset(
770 priv
, priv
->frag_info
[i
].frag_stride
,
771 priv
->frag_info
[i
].frag_align
);
772 buf_size
+= priv
->frag_info
[i
].frag_size
;
777 priv
->rx_skb_size
= eff_mtu
;
778 priv
->log_rx_info
= ROUNDUP_LOG2(i
* sizeof(struct skb_frag_struct
));
780 en_dbg(DRV
, priv
, "Rx buffer scatter-list (effective-mtu:%d "
781 "num_frags:%d):\n", eff_mtu
, priv
->num_frags
);
782 for (i
= 0; i
< priv
->num_frags
; i
++) {
783 en_dbg(DRV
, priv
, " frag:%d - size:%d prefix:%d align:%d "
784 "stride:%d last_offset:%d\n", i
,
785 priv
->frag_info
[i
].frag_size
,
786 priv
->frag_info
[i
].frag_prefix_size
,
787 priv
->frag_info
[i
].frag_align
,
788 priv
->frag_info
[i
].frag_stride
,
789 priv
->frag_info
[i
].last_offset
);
793 /* RSS related functions */
795 static int mlx4_en_config_rss_qp(struct mlx4_en_priv
*priv
, int qpn
,
796 struct mlx4_en_rx_ring
*ring
,
797 enum mlx4_qp_state
*state
,
800 struct mlx4_en_dev
*mdev
= priv
->mdev
;
801 struct mlx4_qp_context
*context
;
804 context
= kmalloc(sizeof *context
, GFP_KERNEL
);
806 en_err(priv
, "Failed to allocate qp context\n");
810 err
= mlx4_qp_alloc(mdev
->dev
, qpn
, qp
);
812 en_err(priv
, "Failed to allocate qp #%x\n", qpn
);
815 qp
->event
= mlx4_en_sqp_event
;
817 memset(context
, 0, sizeof *context
);
818 mlx4_en_fill_qp_context(priv
, ring
->size
, ring
->stride
, 0, 0,
819 qpn
, ring
->cqn
, context
);
820 context
->db_rec_addr
= cpu_to_be64(ring
->wqres
.db
.dma
);
822 err
= mlx4_qp_to_ready(mdev
->dev
, &ring
->wqres
.mtt
, context
, qp
, state
);
824 mlx4_qp_remove(mdev
->dev
, qp
);
825 mlx4_qp_free(mdev
->dev
, qp
);
827 mlx4_en_update_rx_prod_db(ring
);
833 /* Allocate rx qp's and configure them according to rss map */
834 int mlx4_en_config_rss_steer(struct mlx4_en_priv
*priv
)
836 struct mlx4_en_dev
*mdev
= priv
->mdev
;
837 struct mlx4_en_rss_map
*rss_map
= &priv
->rss_map
;
838 struct mlx4_qp_context context
;
839 struct mlx4_en_rss_context
*rss_context
;
841 int rss_xor
= mdev
->profile
.rss_xor
;
842 u8 rss_mask
= mdev
->profile
.rss_mask
;
847 en_dbg(DRV
, priv
, "Configuring rss steering\n");
848 err
= mlx4_qp_reserve_range(mdev
->dev
, priv
->rx_ring_num
,
852 en_err(priv
, "Failed reserving %d qps\n", priv
->rx_ring_num
);
856 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
857 qpn
= rss_map
->base_qpn
+ i
;
858 err
= mlx4_en_config_rss_qp(priv
, qpn
, &priv
->rx_ring
[i
],
867 /* Configure RSS indirection qp */
868 err
= mlx4_qp_reserve_range(mdev
->dev
, 1, 1, &priv
->base_qpn
);
870 en_err(priv
, "Failed to reserve range for RSS "
874 err
= mlx4_qp_alloc(mdev
->dev
, priv
->base_qpn
, &rss_map
->indir_qp
);
876 en_err(priv
, "Failed to allocate RSS indirection QP\n");
879 rss_map
->indir_qp
.event
= mlx4_en_sqp_event
;
880 mlx4_en_fill_qp_context(priv
, 0, 0, 0, 1, priv
->base_qpn
,
881 priv
->rx_ring
[0].cqn
, &context
);
883 ptr
= ((void *) &context
) + 0x3c;
884 rss_context
= (struct mlx4_en_rss_context
*) ptr
;
885 rss_context
->base_qpn
= cpu_to_be32(ilog2(priv
->rx_ring_num
) << 24 |
886 (rss_map
->base_qpn
));
887 rss_context
->default_qpn
= cpu_to_be32(rss_map
->base_qpn
);
888 rss_context
->hash_fn
= rss_xor
& 0x3;
889 rss_context
->flags
= rss_mask
<< 2;
891 err
= mlx4_qp_to_ready(mdev
->dev
, &priv
->res
.mtt
, &context
,
892 &rss_map
->indir_qp
, &rss_map
->indir_state
);
899 mlx4_qp_modify(mdev
->dev
, NULL
, rss_map
->indir_state
,
900 MLX4_QP_STATE_RST
, NULL
, 0, 0, &rss_map
->indir_qp
);
901 mlx4_qp_remove(mdev
->dev
, &rss_map
->indir_qp
);
902 mlx4_qp_free(mdev
->dev
, &rss_map
->indir_qp
);
904 mlx4_qp_release_range(mdev
->dev
, priv
->base_qpn
, 1);
906 for (i
= 0; i
< good_qps
; i
++) {
907 mlx4_qp_modify(mdev
->dev
, NULL
, rss_map
->state
[i
],
908 MLX4_QP_STATE_RST
, NULL
, 0, 0, &rss_map
->qps
[i
]);
909 mlx4_qp_remove(mdev
->dev
, &rss_map
->qps
[i
]);
910 mlx4_qp_free(mdev
->dev
, &rss_map
->qps
[i
]);
912 mlx4_qp_release_range(mdev
->dev
, rss_map
->base_qpn
, priv
->rx_ring_num
);
916 void mlx4_en_release_rss_steer(struct mlx4_en_priv
*priv
)
918 struct mlx4_en_dev
*mdev
= priv
->mdev
;
919 struct mlx4_en_rss_map
*rss_map
= &priv
->rss_map
;
922 mlx4_qp_modify(mdev
->dev
, NULL
, rss_map
->indir_state
,
923 MLX4_QP_STATE_RST
, NULL
, 0, 0, &rss_map
->indir_qp
);
924 mlx4_qp_remove(mdev
->dev
, &rss_map
->indir_qp
);
925 mlx4_qp_free(mdev
->dev
, &rss_map
->indir_qp
);
926 mlx4_qp_release_range(mdev
->dev
, priv
->base_qpn
, 1);
928 for (i
= 0; i
< priv
->rx_ring_num
; i
++) {
929 mlx4_qp_modify(mdev
->dev
, NULL
, rss_map
->state
[i
],
930 MLX4_QP_STATE_RST
, NULL
, 0, 0, &rss_map
->qps
[i
]);
931 mlx4_qp_remove(mdev
->dev
, &rss_map
->qps
[i
]);
932 mlx4_qp_free(mdev
->dev
, &rss_map
->qps
[i
]);
934 mlx4_qp_release_range(mdev
->dev
, rss_map
->base_qpn
, priv
->rx_ring_num
);