blob: 6758292311f416b823f90b2d65e1b1e9c1be80f8 [file] [log] [blame]
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001/*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 *
32 */
33
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030034#include <net/busy_poll.h>
Brenden Blanco47a38e12016-07-19 12:16:50 -070035#include <linux/bpf.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070036#include <linux/mlx4/cq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070038#include <linux/mlx4/qp.h>
39#include <linux/skbuff.h>
Sasha Levinb67bfe02013-02-27 17:06:00 -080040#include <linux/rculist.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070041#include <linux/if_ether.h>
42#include <linux/if_vlan.h>
43#include <linux/vmalloc.h>
Amir Vadai35f6f452014-06-29 11:54:55 +030044#include <linux/irq.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070045
Shani Michaelif8c64552014-11-09 13:51:53 +020046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ip6_checksum.h>
48#endif
49
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070050#include "mlx4_en.h"
51
Eric Dumazet51151a12013-06-23 08:17:56 -070052static int mlx4_alloc_pages(struct mlx4_en_priv *priv,
53 struct mlx4_en_rx_alloc *page_alloc,
54 const struct mlx4_en_frag_info *frag_info,
55 gfp_t _gfp)
56{
57 int order;
58 struct page *page;
59 dma_addr_t dma;
60
Brenden Blancod576acf2016-07-19 12:16:52 -070061 for (order = frag_info->order; ;) {
Eric Dumazet51151a12013-06-23 08:17:56 -070062 gfp_t gfp = _gfp;
63
64 if (order)
Konstantin Khlebnikov04aeb562016-04-18 14:33:54 +030065 gfp |= __GFP_COMP | __GFP_NOWARN | __GFP_NOMEMALLOC;
Eric Dumazet51151a12013-06-23 08:17:56 -070066 page = alloc_pages(gfp, order);
67 if (likely(page))
68 break;
69 if (--order < 0 ||
70 ((PAGE_SIZE << order) < frag_info->frag_size))
71 return -ENOMEM;
72 }
73 dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE << order,
Brenden Blancod576acf2016-07-19 12:16:52 -070074 frag_info->dma_dir);
Eric Dumazet51151a12013-06-23 08:17:56 -070075 if (dma_mapping_error(priv->ddev, dma)) {
76 put_page(page);
77 return -ENOMEM;
78 }
Amir Vadai70fbe072013-10-07 13:38:12 +020079 page_alloc->page_size = PAGE_SIZE << order;
Eric Dumazet51151a12013-06-23 08:17:56 -070080 page_alloc->page = page;
81 page_alloc->dma = dma;
Ido Shamay5f6e9802014-11-02 16:26:15 +020082 page_alloc->page_offset = 0;
Eric Dumazet51151a12013-06-23 08:17:56 -070083 /* Not doing get_page() for each frag is a big win
Eric Dumazet98226202014-10-10 04:48:17 -070084 * on asymetric workloads. Note we can not use atomic_set().
Eric Dumazet51151a12013-06-23 08:17:56 -070085 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -070086 page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);
Eric Dumazet51151a12013-06-23 08:17:56 -070087 return 0;
88}
89
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000090static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
91 struct mlx4_en_rx_desc *rx_desc,
92 struct mlx4_en_rx_alloc *frags,
Eric Dumazet51151a12013-06-23 08:17:56 -070093 struct mlx4_en_rx_alloc *ring_alloc,
94 gfp_t gfp)
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000095{
96 struct mlx4_en_rx_alloc page_alloc[MLX4_EN_MAX_RX_FRAGS];
Eric Dumazet51151a12013-06-23 08:17:56 -070097 const struct mlx4_en_frag_info *frag_info;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000098 struct page *page;
99 dma_addr_t dma;
100 int i;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700101
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000102 for (i = 0; i < priv->num_frags; i++) {
103 frag_info = &priv->frag_info[i];
Eric Dumazet51151a12013-06-23 08:17:56 -0700104 page_alloc[i] = ring_alloc[i];
Amir Vadai70fbe072013-10-07 13:38:12 +0200105 page_alloc[i].page_offset += frag_info->frag_stride;
106
107 if (page_alloc[i].page_offset + frag_info->frag_stride <=
108 ring_alloc[i].page_size)
Eric Dumazet51151a12013-06-23 08:17:56 -0700109 continue;
Amir Vadai70fbe072013-10-07 13:38:12 +0200110
Eric Dumazet51151a12013-06-23 08:17:56 -0700111 if (mlx4_alloc_pages(priv, &page_alloc[i], frag_info, gfp))
112 goto out;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000113 }
114
115 for (i = 0; i < priv->num_frags; i++) {
116 frags[i] = ring_alloc[i];
Amir Vadai70fbe072013-10-07 13:38:12 +0200117 dma = ring_alloc[i].dma + ring_alloc[i].page_offset;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000118 ring_alloc[i] = page_alloc[i];
119 rx_desc->data[i].addr = cpu_to_be64(dma);
120 }
121
122 return 0;
123
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000124out:
125 while (i--) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700126 if (page_alloc[i].page != ring_alloc[i].page) {
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000127 dma_unmap_page(priv->ddev, page_alloc[i].dma,
Brenden Blancod576acf2016-07-19 12:16:52 -0700128 page_alloc[i].page_size,
129 priv->frag_info[i].dma_dir);
Eric Dumazet51151a12013-06-23 08:17:56 -0700130 page = page_alloc[i].page;
Konstantin Khlebnikov851b10d2016-04-18 14:34:05 +0300131 /* Revert changes done by mlx4_alloc_pages */
132 page_ref_sub(page, page_alloc[i].page_size /
133 priv->frag_info[i].frag_stride - 1);
Eric Dumazet51151a12013-06-23 08:17:56 -0700134 put_page(page);
135 }
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000136 }
137 return -ENOMEM;
138}
139
140static void mlx4_en_free_frag(struct mlx4_en_priv *priv,
141 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700142 int i)
143{
Eric Dumazet51151a12013-06-23 08:17:56 -0700144 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Amir Vadai021f1102013-10-07 13:38:13 +0200145 u32 next_frag_end = frags[i].page_offset + 2 * frag_info->frag_stride;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700146
Amir Vadai021f1102013-10-07 13:38:13 +0200147
148 if (next_frag_end > frags[i].page_size)
Amir Vadai70fbe072013-10-07 13:38:12 +0200149 dma_unmap_page(priv->ddev, frags[i].dma, frags[i].page_size,
Brenden Blancod576acf2016-07-19 12:16:52 -0700150 frag_info->dma_dir);
Eric Dumazet51151a12013-06-23 08:17:56 -0700151
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000152 if (frags[i].page)
153 put_page(frags[i].page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700154}
155
156static int mlx4_en_init_allocator(struct mlx4_en_priv *priv,
157 struct mlx4_en_rx_ring *ring)
158{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700159 int i;
Eric Dumazet51151a12013-06-23 08:17:56 -0700160 struct mlx4_en_rx_alloc *page_alloc;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700161
162 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700163 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700164
Eric Dumazet51151a12013-06-23 08:17:56 -0700165 if (mlx4_alloc_pages(priv, &ring->page_alloc[i],
Ido Shamay1ab25f82014-11-02 16:26:16 +0200166 frag_info, GFP_KERNEL | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000167 goto out;
Ido Shamayb110d2c2015-02-03 17:57:19 +0200168
169 en_dbg(DRV, priv, " frag %d allocator: - size:%d frags:%d\n",
170 i, ring->page_alloc[i].page_size,
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700171 page_ref_count(ring->page_alloc[i].page));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700172 }
173 return 0;
174
175out:
176 while (i--) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700177 struct page *page;
178
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700179 page_alloc = &ring->page_alloc[i];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000180 dma_unmap_page(priv->ddev, page_alloc->dma,
Brenden Blancod576acf2016-07-19 12:16:52 -0700181 page_alloc->page_size,
182 priv->frag_info[i].dma_dir);
Eric Dumazet51151a12013-06-23 08:17:56 -0700183 page = page_alloc->page;
Konstantin Khlebnikov851b10d2016-04-18 14:34:05 +0300184 /* Revert changes done by mlx4_alloc_pages */
185 page_ref_sub(page, page_alloc->page_size /
186 priv->frag_info[i].frag_stride - 1);
Eric Dumazet51151a12013-06-23 08:17:56 -0700187 put_page(page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700188 page_alloc->page = NULL;
189 }
190 return -ENOMEM;
191}
192
193static void mlx4_en_destroy_allocator(struct mlx4_en_priv *priv,
194 struct mlx4_en_rx_ring *ring)
195{
196 struct mlx4_en_rx_alloc *page_alloc;
197 int i;
198
199 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700200 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
201
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700202 page_alloc = &ring->page_alloc[i];
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000203 en_dbg(DRV, priv, "Freeing allocator:%d count:%d\n",
204 i, page_count(page_alloc->page));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700205
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000206 dma_unmap_page(priv->ddev, page_alloc->dma,
Brenden Blancod576acf2016-07-19 12:16:52 -0700207 page_alloc->page_size, frag_info->dma_dir);
Amir Vadai70fbe072013-10-07 13:38:12 +0200208 while (page_alloc->page_offset + frag_info->frag_stride <
209 page_alloc->page_size) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700210 put_page(page_alloc->page);
Amir Vadai70fbe072013-10-07 13:38:12 +0200211 page_alloc->page_offset += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700212 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700213 page_alloc->page = NULL;
214 }
215}
216
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700217static void mlx4_en_init_rx_desc(struct mlx4_en_priv *priv,
218 struct mlx4_en_rx_ring *ring, int index)
219{
220 struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700221 int possible_frags;
222 int i;
223
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700224 /* Set size and memtype fields */
225 for (i = 0; i < priv->num_frags; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700226 rx_desc->data[i].byte_count =
227 cpu_to_be32(priv->frag_info[i].frag_size);
228 rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
229 }
230
231 /* If the number of used fragments does not fill up the ring stride,
232 * remaining (unused) fragments must be padded with null address/size
233 * and a special memory key */
234 possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
235 for (i = priv->num_frags; i < possible_frags; i++) {
236 rx_desc->data[i].byte_count = 0;
237 rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
238 rx_desc->data[i].addr = 0;
239 }
240}
241
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700242static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
Eric Dumazet51151a12013-06-23 08:17:56 -0700243 struct mlx4_en_rx_ring *ring, int index,
244 gfp_t gfp)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700245{
246 struct mlx4_en_rx_desc *rx_desc = ring->buf + (index * ring->stride);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000247 struct mlx4_en_rx_alloc *frags = ring->rx_info +
248 (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700249
Brenden Blancod576acf2016-07-19 12:16:52 -0700250 if (ring->page_cache.index > 0) {
251 frags[0] = ring->page_cache.buf[--ring->page_cache.index];
252 rx_desc->data[0].addr = cpu_to_be64(frags[0].dma);
253 return 0;
254 }
255
Eric Dumazet51151a12013-06-23 08:17:56 -0700256 return mlx4_en_alloc_frags(priv, rx_desc, frags, ring->page_alloc, gfp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700257}
258
Ido Shamay07841f92015-04-30 17:32:46 +0300259static inline bool mlx4_en_is_ring_empty(struct mlx4_en_rx_ring *ring)
260{
Ido Shamay07841f92015-04-30 17:32:46 +0300261 return ring->prod == ring->cons;
262}
263
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700264static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
265{
266 *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
267}
268
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000269static void mlx4_en_free_rx_desc(struct mlx4_en_priv *priv,
270 struct mlx4_en_rx_ring *ring,
271 int index)
272{
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000273 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000274 int nr;
275
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000276 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000277 for (nr = 0; nr < priv->num_frags; nr++) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000278 en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000279 mlx4_en_free_frag(priv, frags, nr);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000280 }
281}
282
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700283static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
284{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700285 struct mlx4_en_rx_ring *ring;
286 int ring_ind;
287 int buf_ind;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000288 int new_size;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700289
290 for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
291 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200292 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700293
294 if (mlx4_en_prepare_rx_desc(priv, ring,
Eric Dumazet51151a12013-06-23 08:17:56 -0700295 ring->actual_size,
Ido Shamay1ab25f82014-11-02 16:26:16 +0200296 GFP_KERNEL | __GFP_COLD)) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700297 if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
Joe Perches1a91de22014-05-07 12:52:57 -0700298 en_err(priv, "Failed to allocate enough rx buffers\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700299 return -ENOMEM;
300 } else {
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000301 new_size = rounddown_pow_of_two(ring->actual_size);
Joe Perches1a91de22014-05-07 12:52:57 -0700302 en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000303 ring->actual_size, new_size);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000304 goto reduce_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700305 }
306 }
307 ring->actual_size++;
308 ring->prod++;
309 }
310 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000311 return 0;
312
313reduce_rings:
314 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200315 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000316 while (ring->actual_size > new_size) {
317 ring->actual_size--;
318 ring->prod--;
319 mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
320 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000321 }
322
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700323 return 0;
324}
325
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700326static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
327 struct mlx4_en_rx_ring *ring)
328{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700329 int index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700330
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000331 en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
332 ring->cons, ring->prod);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700333
334 /* Unmap and free Rx buffers */
Ido Shamay07841f92015-04-30 17:32:46 +0300335 while (!mlx4_en_is_ring_empty(ring)) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700336 index = ring->cons & ring->size_mask;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000337 en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000338 mlx4_en_free_rx_desc(priv, ring, index);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700339 ++ring->cons;
340 }
341}
342
Ido Shamay02512482014-02-21 12:39:17 +0200343void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
344{
345 int i;
346 int num_of_eqs;
Ido Shamaybb2146b2014-02-21 12:39:18 +0200347 int num_rx_rings;
Ido Shamay02512482014-02-21 12:39:17 +0200348 struct mlx4_dev *dev = mdev->dev;
349
350 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
Matan Barakc66fa192015-05-31 09:30:16 +0300351 num_of_eqs = max_t(int, MIN_RX_RINGS,
352 min_t(int,
353 mlx4_get_eqs_per_port(mdev->dev, i),
354 DEF_RX_RINGS));
Ido Shamay02512482014-02-21 12:39:17 +0200355
Amir Vadaiea1c1af2014-07-22 15:44:12 +0300356 num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
357 min_t(int, num_of_eqs,
358 netif_get_num_default_rss_queues());
Ido Shamay02512482014-02-21 12:39:17 +0200359 mdev->profile.prof[i].rx_ring_num =
Ido Shamaybb2146b2014-02-21 12:39:18 +0200360 rounddown_pow_of_two(num_rx_rings);
Ido Shamay02512482014-02-21 12:39:17 +0200361 }
362}
363
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700364int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200365 struct mlx4_en_rx_ring **pring,
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200366 u32 size, u16 stride, int node)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700367{
368 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200369 struct mlx4_en_rx_ring *ring;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000370 int err = -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700371 int tmp;
372
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200373 ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200374 if (!ring) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200375 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
376 if (!ring) {
377 en_err(priv, "Failed to allocate RX ring structure\n");
378 return -ENOMEM;
379 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200380 }
381
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700382 ring->prod = 0;
383 ring->cons = 0;
384 ring->size = size;
385 ring->size_mask = size - 1;
386 ring->stride = stride;
387 ring->log_stride = ffs(ring->stride) - 1;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700388 ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700389
390 tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000391 sizeof(struct mlx4_en_rx_alloc));
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200392 ring->rx_info = vmalloc_node(tmp, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200393 if (!ring->rx_info) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200394 ring->rx_info = vmalloc(tmp);
395 if (!ring->rx_info) {
396 err = -ENOMEM;
397 goto err_ring;
398 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200399 }
Joe Perchese404dec2012-01-29 12:56:23 +0000400
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000401 en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700402 ring->rx_info, tmp);
403
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200404 /* Allocate HW buffers on provided NUMA node */
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200405 set_dev_node(&mdev->dev->persist->pdev->dev, node);
Haggai Abramovsky73898db2016-05-04 14:50:15 +0300406 err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200407 set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700408 if (err)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200409 goto err_info;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700410
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700411 ring->buf = ring->wqres.buf.direct.buf;
412
Amir Vadaiec693d42013-04-23 06:06:49 +0000413 ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
414
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200415 *pring = ring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700416 return 0;
417
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200418err_info:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700419 vfree(ring->rx_info);
420 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200421err_ring:
422 kfree(ring);
423 *pring = NULL;
424
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700425 return err;
426}
427
428int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
429{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700430 struct mlx4_en_rx_ring *ring;
431 int i;
432 int ring_ind;
433 int err;
434 int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
435 DS_SIZE * priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700436
437 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200438 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700439
440 ring->prod = 0;
441 ring->cons = 0;
442 ring->actual_size = 0;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200443 ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700444
445 ring->stride = stride;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700446 if (ring->stride <= TXBB_SIZE)
447 ring->buf += TXBB_SIZE;
448
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700449 ring->log_stride = ffs(ring->stride) - 1;
450 ring->buf_size = ring->size * ring->stride;
451
452 memset(ring->buf, 0, ring->buf_size);
453 mlx4_en_update_rx_prod_db(ring);
454
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000455 /* Initialize all descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700456 for (i = 0; i < ring->size; i++)
457 mlx4_en_init_rx_desc(priv, ring, i);
458
459 /* Initialize page allocators */
460 err = mlx4_en_init_allocator(priv, ring);
461 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000462 en_err(priv, "Failed initializing ring allocator\n");
Yevgeny Petrilin60b18092011-04-06 23:25:45 +0000463 if (ring->stride <= TXBB_SIZE)
464 ring->buf -= TXBB_SIZE;
Yevgeny Petrilin9a4f92a2009-04-20 04:24:28 +0000465 ring_ind--;
466 goto err_allocator;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700467 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700468 }
Ingo Molnarb58515b2008-11-25 16:53:32 -0800469 err = mlx4_en_fill_rx_buffers(priv);
470 if (err)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700471 goto err_buffers;
472
473 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200474 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700475
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +0000476 ring->size_mask = ring->actual_size - 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700477 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700478 }
479
480 return 0;
481
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700482err_buffers:
483 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200484 mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700485
486 ring_ind = priv->rx_ring_num - 1;
487err_allocator:
488 while (ring_ind >= 0) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200489 if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
490 priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
491 mlx4_en_destroy_allocator(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700492 ring_ind--;
493 }
494 return err;
495}
496
Ido Shamay07841f92015-04-30 17:32:46 +0300497/* We recover from out of memory by scheduling our napi poll
498 * function (mlx4_en_process_cq), which tries to allocate
499 * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
500 */
501void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
502{
503 int ring;
504
505 if (!priv->port_up)
506 return;
507
508 for (ring = 0; ring < priv->rx_ring_num; ring++) {
509 if (mlx4_en_is_ring_empty(priv->rx_ring[ring]))
510 napi_reschedule(&priv->rx_cq[ring]->napi);
511 }
512}
513
Brenden Blancod576acf2016-07-19 12:16:52 -0700514/* When the rx ring is running in page-per-packet mode, a released frame can go
515 * directly into a small cache, to avoid unmapping or touching the page
516 * allocator. In bpf prog performance scenarios, buffers are either forwarded
517 * or dropped, never converted to skbs, so every page can come directly from
518 * this cache when it is sized to be a multiple of the napi budget.
519 */
520bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
521 struct mlx4_en_rx_alloc *frame)
522{
523 struct mlx4_en_page_cache *cache = &ring->page_cache;
524
525 if (cache->index >= MLX4_EN_CACHE_SIZE)
526 return false;
527
528 cache->buf[cache->index++] = *frame;
529 return true;
530}
531
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700532void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200533 struct mlx4_en_rx_ring **pring,
534 u32 size, u16 stride)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700535{
536 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200537 struct mlx4_en_rx_ring *ring = *pring;
Brenden Blancocb7386d2016-07-20 17:22:33 -0700538 struct bpf_prog *old_prog;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700539
Brenden Blanco326fe022016-09-03 21:29:58 -0700540 old_prog = rcu_dereference_protected(
541 ring->xdp_prog,
542 lockdep_is_held(&mdev->state_lock));
Brenden Blancocb7386d2016-07-20 17:22:33 -0700543 if (old_prog)
544 bpf_prog_put(old_prog);
Thadeu Lima de Souza Cascardo68355f72012-02-06 08:39:49 +0000545 mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700546 vfree(ring->rx_info);
547 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200548 kfree(ring);
549 *pring = NULL;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700550}
551
552void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
553 struct mlx4_en_rx_ring *ring)
554{
Brenden Blancod576acf2016-07-19 12:16:52 -0700555 int i;
556
557 for (i = 0; i < ring->page_cache.index; i++) {
558 struct mlx4_en_rx_alloc *frame = &ring->page_cache.buf[i];
559
560 dma_unmap_page(priv->ddev, frame->dma, frame->page_size,
561 priv->frag_info[0].dma_dir);
562 put_page(frame->page);
563 }
564 ring->page_cache.index = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700565 mlx4_en_free_rx_buf(priv, ring);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700566 if (ring->stride <= TXBB_SIZE)
567 ring->buf -= TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700568 mlx4_en_destroy_allocator(priv, ring);
569}
570
571
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700572static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
573 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000574 struct mlx4_en_rx_alloc *frags,
Eric Dumazet90278c92011-10-19 18:49:52 +0000575 struct sk_buff *skb,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700576 int length)
577{
Eric Dumazet90278c92011-10-19 18:49:52 +0000578 struct skb_frag_struct *skb_frags_rx = skb_shinfo(skb)->frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700579 struct mlx4_en_frag_info *frag_info;
580 int nr;
581 dma_addr_t dma;
582
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000583 /* Collect used fragments while replacing them in the HW descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700584 for (nr = 0; nr < priv->num_frags; nr++) {
585 frag_info = &priv->frag_info[nr];
586 if (length <= frag_info->frag_prefix_size)
587 break;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000588 if (!frags[nr].page)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700589 goto fail;
590
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000591 dma = be64_to_cpu(rx_desc->data[nr].addr);
592 dma_sync_single_for_cpu(priv->ddev, dma, frag_info->frag_size,
593 DMA_FROM_DEVICE);
594
595 /* Save page reference in skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000596 __skb_frag_set_page(&skb_frags_rx[nr], frags[nr].page);
597 skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size);
Amir Vadai70fbe072013-10-07 13:38:12 +0200598 skb_frags_rx[nr].page_offset = frags[nr].page_offset;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000599 skb->truesize += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700600 frags[nr].page = NULL;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700601 }
602 /* Adjust size of last fragment to match actual length */
roel kluin973507c2009-08-08 23:54:21 +0000603 if (nr > 0)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000604 skb_frag_size_set(&skb_frags_rx[nr - 1],
605 length - priv->frag_info[nr - 1].frag_prefix_size);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700606 return nr;
607
608fail:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700609 while (nr > 0) {
610 nr--;
Ian Campbell311761c2011-10-19 23:01:45 +0000611 __skb_frag_unref(&skb_frags_rx[nr]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700612 }
613 return 0;
614}
615
616
617static struct sk_buff *mlx4_en_rx_skb(struct mlx4_en_priv *priv,
618 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000619 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700620 unsigned int length)
621{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700622 struct sk_buff *skb;
623 void *va;
624 int used_frags;
625 dma_addr_t dma;
626
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000627 skb = netdev_alloc_skb(priv->dev, SMALL_PACKET_SIZE + NET_IP_ALIGN);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700628 if (!skb) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000629 en_dbg(RX_ERR, priv, "Failed allocating skb\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700630 return NULL;
631 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700632 skb_reserve(skb, NET_IP_ALIGN);
633 skb->len = length;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700634
635 /* Get pointer to first fragment so we could copy the headers into the
636 * (linear part of the) skb */
Amir Vadai70fbe072013-10-07 13:38:12 +0200637 va = page_address(frags[0].page) + frags[0].page_offset;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700638
639 if (length <= SMALL_PACKET_SIZE) {
640 /* We are copying all relevant data to the skb - temporarily
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000641 * sync buffers for the copy */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700642 dma = be64_to_cpu(rx_desc->data[0].addr);
Yevgeny Petrilinebf8c9a2012-03-06 04:03:34 +0000643 dma_sync_single_for_cpu(priv->ddev, dma, length,
FUJITA Tomonorie4fc8562010-02-04 18:57:42 +0000644 DMA_FROM_DEVICE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700645 skb_copy_to_linear_data(skb, va, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700646 skb->tail += length;
647 } else {
Eric Dumazetcfecec52014-09-05 18:29:45 -0700648 unsigned int pull_len;
649
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700650 /* Move relevant fragments to skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000651 used_frags = mlx4_en_complete_rx_desc(priv, rx_desc, frags,
652 skb, length);
Yevgeny Petrilin785a09822009-04-26 20:42:57 +0000653 if (unlikely(!used_frags)) {
654 kfree_skb(skb);
655 return NULL;
656 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700657 skb_shinfo(skb)->nr_frags = used_frags;
658
Eric Dumazetcfecec52014-09-05 18:29:45 -0700659 pull_len = eth_get_headlen(va, SMALL_PACKET_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700660 /* Copy headers into the skb linear buffer */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700661 memcpy(skb->data, va, pull_len);
662 skb->tail += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700663
664 /* Skip headers in first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700665 skb_shinfo(skb)->frags[0].page_offset += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700666
667 /* Adjust size of first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700668 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], pull_len);
669 skb->data_len = length - pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700670 }
671 return skb;
672}
673
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000674static void validate_loopback(struct mlx4_en_priv *priv, struct sk_buff *skb)
675{
676 int i;
677 int offset = ETH_HLEN;
678
679 for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++, offset++) {
680 if (*(skb->data + offset) != (unsigned char) (i & 0xff))
681 goto out_loopback;
682 }
683 /* Loopback found */
684 priv->loopback_ok = 1;
685
686out_loopback:
687 dev_kfree_skb_any(skb);
688}
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700689
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000690static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
691 struct mlx4_en_rx_ring *ring)
692{
693 int index = ring->prod & ring->size_mask;
694
695 while ((u32) (ring->prod - ring->cons) < ring->actual_size) {
Ido Shamay1ab25f82014-11-02 16:26:16 +0200696 if (mlx4_en_prepare_rx_desc(priv, ring, index,
697 GFP_ATOMIC | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000698 break;
699 ring->prod++;
700 index = ring->prod & ring->size_mask;
701 }
702}
703
Shani Michaelif8c64552014-11-09 13:51:53 +0200704/* When hardware doesn't strip the vlan, we need to calculate the checksum
705 * over it and add it to the hardware's checksum calculation
706 */
707static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
708 struct vlan_hdr *vlanh)
709{
710 return csum_add(hw_checksum, *(__wsum *)vlanh);
711}
712
713/* Although the stack expects checksum which doesn't include the pseudo
714 * header, the HW adds it. To address that, we are subtracting the pseudo
715 * header checksum from the checksum value provided by the HW.
716 */
717static void get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
718 struct iphdr *iph)
719{
720 __u16 length_for_csum = 0;
721 __wsum csum_pseudo_header = 0;
722
723 length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
724 csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
725 length_for_csum, iph->protocol, 0);
726 skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
727}
728
729#if IS_ENABLED(CONFIG_IPV6)
730/* In IPv6 packets, besides subtracting the pseudo header checksum,
731 * we also compute/add the IP header checksum which
732 * is not added by the HW.
733 */
734static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
735 struct ipv6hdr *ipv6h)
736{
737 __wsum csum_pseudo_hdr = 0;
738
739 if (ipv6h->nexthdr == IPPROTO_FRAGMENT || ipv6h->nexthdr == IPPROTO_HOPOPTS)
740 return -1;
Daniel Jurgens82d69202016-05-04 15:00:33 +0300741 hw_checksum = csum_add(hw_checksum, (__force __wsum)htons(ipv6h->nexthdr));
Shani Michaelif8c64552014-11-09 13:51:53 +0200742
743 csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
744 sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
745 csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
746 csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ntohs(ipv6h->nexthdr));
747
748 skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
749 skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
750 return 0;
751}
752#endif
753static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
Ido Shamay79a25852015-06-25 11:29:43 +0300754 netdev_features_t dev_features)
Shani Michaelif8c64552014-11-09 13:51:53 +0200755{
756 __wsum hw_checksum = 0;
757
758 void *hdr = (u8 *)va + sizeof(struct ethhdr);
759
760 hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
761
Hadar Hen Zione802f8e2015-07-27 14:46:33 +0300762 if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
Ido Shamay79a25852015-06-25 11:29:43 +0300763 !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
Shani Michaelif8c64552014-11-09 13:51:53 +0200764 hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
765 hdr += sizeof(struct vlan_hdr);
766 }
767
768 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
769 get_fixed_ipv4_csum(hw_checksum, skb, hdr);
770#if IS_ENABLED(CONFIG_IPV6)
771 else if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
772 if (get_fixed_ipv6_csum(hw_checksum, skb, hdr))
773 return -1;
774#endif
775 return 0;
776}
777
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700778int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
779{
780 struct mlx4_en_priv *priv = netdev_priv(dev);
Amir Vadaiec693d42013-04-23 06:06:49 +0000781 struct mlx4_en_dev *mdev = priv->mdev;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700782 struct mlx4_cqe *cqe;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200783 struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000784 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700785 struct mlx4_en_rx_desc *rx_desc;
Brenden Blanco47a38e12016-07-19 12:16:50 -0700786 struct bpf_prog *xdp_prog;
Brenden Blanco9ecc2d82016-07-19 12:16:55 -0700787 int doorbell_pending;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700788 struct sk_buff *skb;
Brenden Blanco9ecc2d82016-07-19 12:16:55 -0700789 int tx_index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700790 int index;
791 int nr;
792 unsigned int length;
793 int polled = 0;
794 int ip_summed;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000795 int factor = priv->cqe_factor;
Amir Vadaiec693d42013-04-23 06:06:49 +0000796 u64 timestamp;
Or Gerlitz837052d2013-12-23 16:09:44 +0200797 bool l2_tunnel;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700798
799 if (!priv->port_up)
800 return 0;
801
Eric W. Biederman38be0a32014-03-14 18:05:58 -0700802 if (budget <= 0)
803 return polled;
804
Brenden Blanco326fe022016-09-03 21:29:58 -0700805 /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
806 rcu_read_lock();
807 xdp_prog = rcu_dereference(ring->xdp_prog);
Brenden Blanco9ecc2d82016-07-19 12:16:55 -0700808 doorbell_pending = 0;
809 tx_index = (priv->tx_ring_num - priv->xdp_ring_num) + cq->ring;
Brenden Blanco47a38e12016-07-19 12:16:50 -0700810
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700811 /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
812 * descriptor offset can be deduced from the CQE index instead of
813 * reading 'cqe->index' */
814 index = cq->mcq.cons_index & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +0300815 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700816
817 /* Process all completed CQEs */
818 while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
819 cq->mcq.cons_index & cq->size)) {
820
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000821 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700822 rx_desc = ring->buf + (index << ring->log_stride);
823
824 /*
825 * make sure we read the CQE after we read the ownership bit
826 */
Alexander Duyck12b33752015-04-08 18:49:36 -0700827 dma_rmb();
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700828
829 /* Drop packet on bad receive or bad checksum */
830 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
831 MLX4_CQE_OPCODE_ERROR)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700832 en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
833 ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
834 ((struct mlx4_err_cqe *)cqe)->syndrome);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700835 goto next;
836 }
837 if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000838 en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700839 goto next;
840 }
841
Yan Burman79aeacc2013-02-07 02:25:19 +0000842 /* Check if we need to drop the packet if SRIOV is not enabled
843 * and not performing the selftest or flb disabled
844 */
845 if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
846 struct ethhdr *ethh;
847 dma_addr_t dma;
Yan Burman79aeacc2013-02-07 02:25:19 +0000848 /* Get pointer to first fragment since we haven't
849 * skb yet and cast it to ethhdr struct
850 */
851 dma = be64_to_cpu(rx_desc->data[0].addr);
852 dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
853 DMA_FROM_DEVICE);
854 ethh = (struct ethhdr *)(page_address(frags[0].page) +
Amir Vadai70fbe072013-10-07 13:38:12 +0200855 frags[0].page_offset);
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000856
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000857 if (is_multicast_ether_addr(ethh->h_dest)) {
858 struct mlx4_mac_entry *entry;
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000859 struct hlist_head *bucket;
860 unsigned int mac_hash;
861
862 /* Drop the packet, since HW loopback-ed it */
863 mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
864 bucket = &priv->mac_hash[mac_hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800865 hlist_for_each_entry_rcu(entry, bucket, hlist) {
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000866 if (ether_addr_equal_64bits(entry->mac,
Brenden Blanco326fe022016-09-03 21:29:58 -0700867 ethh->h_source))
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000868 goto next;
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000869 }
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000870 }
Yan Burman79aeacc2013-02-07 02:25:19 +0000871 }
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000872
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700873 /*
874 * Packet is OK - process it.
875 */
876 length = be32_to_cpu(cqe->byte_cnt);
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -0500877 length -= ring->fcs_del;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700878 ring->bytes += length;
879 ring->packets++;
Or Gerlitz837052d2013-12-23 16:09:44 +0200880 l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
881 (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700882
Brenden Blanco47a38e12016-07-19 12:16:50 -0700883 /* A bpf program gets first chance to drop the packet. It may
884 * read bytes but not past the end of the frag.
885 */
886 if (xdp_prog) {
887 struct xdp_buff xdp;
888 dma_addr_t dma;
889 u32 act;
890
891 dma = be64_to_cpu(rx_desc->data[0].addr);
892 dma_sync_single_for_cpu(priv->ddev, dma,
893 priv->frag_info[0].frag_size,
894 DMA_FROM_DEVICE);
895
896 xdp.data = page_address(frags[0].page) +
897 frags[0].page_offset;
898 xdp.data_end = xdp.data + length;
899
900 act = bpf_prog_run_xdp(xdp_prog, &xdp);
901 switch (act) {
902 case XDP_PASS:
903 break;
Brenden Blanco9ecc2d82016-07-19 12:16:55 -0700904 case XDP_TX:
905 if (!mlx4_en_xmit_frame(frags, dev,
906 length, tx_index,
907 &doorbell_pending))
908 goto consumed;
909 break;
Brenden Blanco47a38e12016-07-19 12:16:50 -0700910 default:
911 bpf_warn_invalid_xdp_action(act);
912 case XDP_ABORTED:
913 case XDP_DROP:
Brenden Blancod576acf2016-07-19 12:16:52 -0700914 if (mlx4_en_rx_recycle(ring, frags))
915 goto consumed;
Brenden Blanco47a38e12016-07-19 12:16:50 -0700916 goto next;
917 }
918 }
919
Michał Mirosławc8c64cf2011-04-15 04:50:49 +0000920 if (likely(dev->features & NETIF_F_RXCSUM)) {
Shani Michaelif8c64552014-11-09 13:51:53 +0200921 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
922 MLX4_CQE_STATUS_UDP)) {
923 if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
924 cqe->checksum == cpu_to_be16(0xffff)) {
925 ip_summed = CHECKSUM_UNNECESSARY;
926 ring->csum_ok++;
927 } else {
928 ip_summed = CHECKSUM_NONE;
929 ring->csum_none++;
930 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700931 } else {
Shani Michaelif8c64552014-11-09 13:51:53 +0200932 if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
933 (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
934 MLX4_CQE_STATUS_IPV6))) {
935 ip_summed = CHECKSUM_COMPLETE;
936 ring->csum_complete++;
937 } else {
938 ip_summed = CHECKSUM_NONE;
939 ring->csum_none++;
940 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700941 }
942 } else {
943 ip_summed = CHECKSUM_NONE;
Yevgeny Petrilinad043782011-10-18 01:50:56 +0000944 ring->csum_none++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700945 }
946
Shani Michaelidd65bea2014-11-09 13:51:52 +0200947 /* This packet is eligible for GRO if it is:
948 * - DIX Ethernet (type interpretation)
949 * - TCP/IP (v4)
950 * - without IP options
951 * - not an IP fragment
Shani Michaelidd65bea2014-11-09 13:51:52 +0200952 */
Eric Dumazet868fdb02015-11-18 06:30:58 -0800953 if (dev->features & NETIF_F_GRO) {
Shani Michaelidd65bea2014-11-09 13:51:52 +0200954 struct sk_buff *gro_skb = napi_get_frags(&cq->napi);
955 if (!gro_skb)
956 goto next;
957
958 nr = mlx4_en_complete_rx_desc(priv,
959 rx_desc, frags, gro_skb,
960 length);
961 if (!nr)
962 goto next;
963
Shani Michaelif8c64552014-11-09 13:51:53 +0200964 if (ip_summed == CHECKSUM_COMPLETE) {
965 void *va = skb_frag_address(skb_shinfo(gro_skb)->frags);
Ido Shamay79a25852015-06-25 11:29:43 +0300966 if (check_csum(cqe, gro_skb, va,
967 dev->features)) {
Shani Michaelif8c64552014-11-09 13:51:53 +0200968 ip_summed = CHECKSUM_NONE;
969 ring->csum_none++;
970 ring->csum_complete--;
971 }
972 }
973
Shani Michaelidd65bea2014-11-09 13:51:52 +0200974 skb_shinfo(gro_skb)->nr_frags = nr;
975 gro_skb->len = length;
976 gro_skb->data_len = length;
977 gro_skb->ip_summed = ip_summed;
978
979 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
Or Gerlitzc58942f2014-12-11 10:57:51 +0200980 gro_skb->csum_level = 1;
981
Shani Michaelidd65bea2014-11-09 13:51:52 +0200982 if ((cqe->vlan_my_qpn &
Hadar Hen Zione802f8e2015-07-27 14:46:33 +0300983 cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
Shani Michaelidd65bea2014-11-09 13:51:52 +0200984 (dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
985 u16 vid = be16_to_cpu(cqe->sl_vid);
986
987 __vlan_hwaccel_put_tag(gro_skb, htons(ETH_P_8021Q), vid);
Hadar Hen Zione38af4f2015-07-27 14:46:34 +0300988 } else if ((be32_to_cpu(cqe->vlan_my_qpn) &
989 MLX4_CQE_SVLAN_PRESENT_MASK) &&
990 (dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
991 __vlan_hwaccel_put_tag(gro_skb,
992 htons(ETH_P_8021AD),
993 be16_to_cpu(cqe->sl_vid));
Shani Michaelidd65bea2014-11-09 13:51:52 +0200994 }
995
996 if (dev->features & NETIF_F_RXHASH)
997 skb_set_hash(gro_skb,
998 be32_to_cpu(cqe->immed_rss_invalid),
Eric Dumazet0a6d4242015-07-02 13:24:44 +0200999 (ip_summed == CHECKSUM_UNNECESSARY) ?
1000 PKT_HASH_TYPE_L4 :
1001 PKT_HASH_TYPE_L3);
Shani Michaelidd65bea2014-11-09 13:51:52 +02001002
1003 skb_record_rx_queue(gro_skb, cq->ring);
Shani Michaelidd65bea2014-11-09 13:51:52 +02001004
1005 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
1006 timestamp = mlx4_en_get_cqe_ts(cqe);
1007 mlx4_en_fill_hwtstamps(mdev,
1008 skb_hwtstamps(gro_skb),
1009 timestamp);
1010 }
1011
1012 napi_gro_frags(&cq->napi);
1013 goto next;
1014 }
1015
1016 /* GRO not possible, complete processing here */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +00001017 skb = mlx4_en_rx_skb(priv, rx_desc, frags, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001018 if (!skb) {
Eran Ben Elishad21ed3a2016-04-20 16:01:18 +03001019 ring->dropped++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001020 goto next;
1021 }
1022
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +00001023 if (unlikely(priv->validate_loopback)) {
1024 validate_loopback(priv, skb);
1025 goto next;
1026 }
1027
Shani Michaelif8c64552014-11-09 13:51:53 +02001028 if (ip_summed == CHECKSUM_COMPLETE) {
Ido Shamay79a25852015-06-25 11:29:43 +03001029 if (check_csum(cqe, skb, skb->data, dev->features)) {
Shani Michaelif8c64552014-11-09 13:51:53 +02001030 ip_summed = CHECKSUM_NONE;
1031 ring->csum_complete--;
1032 ring->csum_none++;
1033 }
1034 }
1035
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001036 skb->ip_summed = ip_summed;
1037 skb->protocol = eth_type_trans(skb, dev);
David S. Miller0c8dfc82009-01-27 16:22:32 -08001038 skb_record_rx_queue(skb, cq->ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001039
Tom Herbert9ca86002014-08-27 21:27:53 -07001040 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
1041 skb->csum_level = 1;
Or Gerlitz837052d2013-12-23 16:09:44 +02001042
Yevgeny Petrilinad861072011-10-18 01:51:24 +00001043 if (dev->features & NETIF_F_RXHASH)
Tom Herbert69174412013-12-17 23:31:23 -08001044 skb_set_hash(skb,
1045 be32_to_cpu(cqe->immed_rss_invalid),
Eric Dumazet0a6d4242015-07-02 13:24:44 +02001046 (ip_summed == CHECKSUM_UNNECESSARY) ?
1047 PKT_HASH_TYPE_L4 :
1048 PKT_HASH_TYPE_L3);
Yevgeny Petrilinad861072011-10-18 01:51:24 +00001049
Amir Vadaiec693d42013-04-23 06:06:49 +00001050 if ((be32_to_cpu(cqe->vlan_my_qpn) &
Hadar Hen Zione802f8e2015-07-27 14:46:33 +03001051 MLX4_CQE_CVLAN_PRESENT_MASK) &&
Amir Vadaiec693d42013-04-23 06:06:49 +00001052 (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001053 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(cqe->sl_vid));
Hadar Hen Zione38af4f2015-07-27 14:46:34 +03001054 else if ((be32_to_cpu(cqe->vlan_my_qpn) &
1055 MLX4_CQE_SVLAN_PRESENT_MASK) &&
1056 (dev->features & NETIF_F_HW_VLAN_STAG_RX))
1057 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
1058 be16_to_cpu(cqe->sl_vid));
Jiri Pirkof1b553f2011-07-20 04:54:22 +00001059
Amir Vadaiec693d42013-04-23 06:06:49 +00001060 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
1061 timestamp = mlx4_en_get_cqe_ts(cqe);
1062 mlx4_en_fill_hwtstamps(mdev, skb_hwtstamps(skb),
1063 timestamp);
1064 }
1065
Eric Dumazet868fdb02015-11-18 06:30:58 -08001066 napi_gro_receive(&cq->napi, skb);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001067next:
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +00001068 for (nr = 0; nr < priv->num_frags; nr++)
1069 mlx4_en_free_frag(priv, frags, nr);
1070
Brenden Blancod576acf2016-07-19 12:16:52 -07001071consumed:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001072 ++cq->mcq.cons_index;
1073 index = (cq->mcq.cons_index) & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +03001074 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Ben Hutchingsf1d29a32012-11-16 12:44:56 +00001075 if (++polled == budget)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001076 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001077 }
1078
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001079out:
Brenden Blanco326fe022016-09-03 21:29:58 -07001080 rcu_read_unlock();
Brenden Blanco9ecc2d82016-07-19 12:16:55 -07001081 if (doorbell_pending)
1082 mlx4_en_xmit_doorbell(priv->tx_ring[tx_index]);
1083
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001084 AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
1085 mlx4_cq_set_ci(&cq->mcq);
1086 wmb(); /* ensure HW sees CQ consumer before we post new buffers */
1087 ring->cons = cq->mcq.cons_index;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +00001088 mlx4_en_refill_rx_buffers(priv, ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001089 mlx4_en_update_rx_prod_db(ring);
1090 return polled;
1091}
1092
1093
1094void mlx4_en_rx_irq(struct mlx4_cq *mcq)
1095{
1096 struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
1097 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
1098
Eric Dumazet477b35b2014-10-29 16:54:45 -07001099 if (likely(priv->port_up))
1100 napi_schedule_irqoff(&cq->napi);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001101 else
1102 mlx4_en_arm_cq(priv, cq);
1103}
1104
1105/* Rx CQ polling - called by NAPI */
1106int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
1107{
1108 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
1109 struct net_device *dev = cq->dev;
1110 struct mlx4_en_priv *priv = netdev_priv(dev);
1111 int done;
1112
1113 done = mlx4_en_process_rx_cq(dev, cq, budget);
1114
1115 /* If we used up all the quota - we're probably not done yet... */
Yuval Atias2eacc232014-05-14 12:15:10 +03001116 if (done == budget) {
Amir Vadai35f6f452014-06-29 11:54:55 +03001117 const struct cpumask *aff;
Thomas Gleixnerdc2ec622015-09-15 13:34:05 +02001118 struct irq_data *idata;
1119 int cpu_curr;
Amir Vadai35f6f452014-06-29 11:54:55 +03001120
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001121 INC_PERF_COUNTER(priv->pstats.napi_quota);
Amir Vadai35f6f452014-06-29 11:54:55 +03001122
1123 cpu_curr = smp_processor_id();
Thomas Gleixnerdc2ec622015-09-15 13:34:05 +02001124 idata = irq_desc_get_irq_data(cq->irq_desc);
1125 aff = irq_data_get_affinity_mask(idata);
Amir Vadai35f6f452014-06-29 11:54:55 +03001126
Eric Dumazet2e1af7d2014-11-10 14:07:20 -08001127 if (likely(cpumask_test_cpu(cpu_curr, aff)))
1128 return budget;
1129
1130 /* Current cpu is not according to smp_irq_affinity -
1131 * probably affinity changed. need to stop this NAPI
1132 * poll, and restart it on the right CPU
1133 */
1134 done = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001135 }
Eric Dumazet1a288172014-11-06 21:10:11 -08001136 /* Done for now */
1137 napi_complete_done(napi, done);
1138 mlx4_en_arm_cq(priv, cq);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001139 return done;
1140}
1141
Eric Dumazet51151a12013-06-23 08:17:56 -07001142static const int frag_sizes[] = {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001143 FRAG_SZ0,
1144 FRAG_SZ1,
1145 FRAG_SZ2,
1146 FRAG_SZ3
1147};
1148
1149void mlx4_en_calc_rx_buf(struct net_device *dev)
1150{
Brenden Blancod576acf2016-07-19 12:16:52 -07001151 enum dma_data_direction dma_dir = PCI_DMA_FROMDEVICE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001152 struct mlx4_en_priv *priv = netdev_priv(dev);
Brenden Blanco47a38e12016-07-19 12:16:50 -07001153 int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
Brenden Blancod576acf2016-07-19 12:16:52 -07001154 int order = MLX4_EN_ALLOC_PREFER_ORDER;
1155 u32 align = SMP_CACHE_BYTES;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001156 int buf_size = 0;
1157 int i = 0;
1158
Brenden Blancod576acf2016-07-19 12:16:52 -07001159 /* bpf requires buffers to be set up as 1 packet per page.
1160 * This only works when num_frags == 1.
1161 */
1162 if (priv->xdp_ring_num) {
Brenden Blanco9ecc2d82016-07-19 12:16:55 -07001163 dma_dir = PCI_DMA_BIDIRECTIONAL;
Brenden Blancod576acf2016-07-19 12:16:52 -07001164 /* This will gain efficient xdp frame recycling at the expense
1165 * of more costly truesize accounting
1166 */
1167 align = PAGE_SIZE;
1168 order = 0;
1169 }
1170
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001171 while (buf_size < eff_mtu) {
Brenden Blancod576acf2016-07-19 12:16:52 -07001172 priv->frag_info[i].order = order;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001173 priv->frag_info[i].frag_size =
1174 (eff_mtu > buf_size + frag_sizes[i]) ?
1175 frag_sizes[i] : eff_mtu - buf_size;
1176 priv->frag_info[i].frag_prefix_size = buf_size;
Ido Shamaye8e7f012015-02-03 17:57:20 +02001177 priv->frag_info[i].frag_stride =
Brenden Blancod576acf2016-07-19 12:16:52 -07001178 ALIGN(priv->frag_info[i].frag_size, align);
1179 priv->frag_info[i].dma_dir = dma_dir;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001180 buf_size += priv->frag_info[i].frag_size;
1181 i++;
1182 }
1183
1184 priv->num_frags = i;
1185 priv->rx_skb_size = eff_mtu;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +00001186 priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001187
Joe Perches1a91de22014-05-07 12:52:57 -07001188 en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
1189 eff_mtu, priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001190 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -07001191 en_err(priv,
Ido Shamay5f6e9802014-11-02 16:26:15 +02001192 " frag:%d - size:%d prefix:%d stride:%d\n",
Eric Dumazet51151a12013-06-23 08:17:56 -07001193 i,
1194 priv->frag_info[i].frag_size,
1195 priv->frag_info[i].frag_prefix_size,
Eric Dumazet51151a12013-06-23 08:17:56 -07001196 priv->frag_info[i].frag_stride);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001197 }
1198}
1199
1200/* RSS related functions */
1201
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001202static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
1203 struct mlx4_en_rx_ring *ring,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001204 enum mlx4_qp_state *state,
1205 struct mlx4_qp *qp)
1206{
1207 struct mlx4_en_dev *mdev = priv->mdev;
1208 struct mlx4_qp_context *context;
1209 int err = 0;
1210
Joe Perches14f8dc42013-02-07 11:46:27 +00001211 context = kmalloc(sizeof(*context), GFP_KERNEL);
1212 if (!context)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001213 return -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001214
Jiri Kosina40f22872014-05-11 15:15:12 +03001215 err = mlx4_qp_alloc(mdev->dev, qpn, qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001216 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001217 en_err(priv, "Failed to allocate qp #%x\n", qpn);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001218 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001219 }
1220 qp->event = mlx4_en_sqp_event;
1221
1222 memset(context, 0, sizeof *context);
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +00001223 mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
Amir Vadai0e98b522012-04-04 21:33:24 +00001224 qpn, ring->cqn, -1, context);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001225 context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001226
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001227 /* Cancel FCS removal if FW allows */
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -05001228 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001229 context->param3 |= cpu_to_be32(1 << 29);
Muhammad Mahajnaf0df3502015-04-02 16:31:21 +03001230 if (priv->dev->features & NETIF_F_RXFCS)
1231 ring->fcs_del = 0;
1232 else
1233 ring->fcs_del = ETH_FCS_LEN;
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -05001234 } else
1235 ring->fcs_del = 0;
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001236
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001237 err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001238 if (err) {
1239 mlx4_qp_remove(mdev->dev, qp);
1240 mlx4_qp_free(mdev->dev, qp);
1241 }
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001242 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001243out:
1244 kfree(context);
1245 return err;
1246}
1247
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001248int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
1249{
1250 int err;
1251 u32 qpn;
1252
Matan Barakd57febe2014-12-11 10:57:57 +02001253 err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
1254 MLX4_RESERVE_A0_QP);
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001255 if (err) {
1256 en_err(priv, "Failed reserving drop qpn\n");
1257 return err;
1258 }
Jiri Kosina40f22872014-05-11 15:15:12 +03001259 err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp, GFP_KERNEL);
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001260 if (err) {
1261 en_err(priv, "Failed allocating drop qp\n");
1262 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1263 return err;
1264 }
1265
1266 return 0;
1267}
1268
1269void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
1270{
1271 u32 qpn;
1272
1273 qpn = priv->drop_qp.qpn;
1274 mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
1275 mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
1276 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1277}
1278
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001279/* Allocate rx qp's and configure them according to rss map */
1280int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
1281{
1282 struct mlx4_en_dev *mdev = priv->mdev;
1283 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1284 struct mlx4_qp_context context;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001285 struct mlx4_rss_context *rss_context;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001286 int rss_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001287 void *ptr;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001288 u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
Or Gerlitz1202d462011-11-26 19:55:02 +00001289 MLX4_RSS_TCP_IPV6);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001290 int i, qpn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001291 int err = 0;
1292 int good_qps = 0;
1293
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001294 en_dbg(DRV, priv, "Configuring rss steering\n");
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001295 err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
1296 priv->rx_ring_num,
Eugenia Emantayevddae0342014-12-11 10:57:54 +02001297 &rss_map->base_qpn, 0);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001298 if (err) {
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001299 en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001300 return err;
1301 }
1302
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001303 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001304 qpn = rss_map->base_qpn + i;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001305 err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001306 &rss_map->state[i],
1307 &rss_map->qps[i]);
1308 if (err)
1309 goto rss_err;
1310
1311 ++good_qps;
1312 }
1313
1314 /* Configure RSS indirection qp */
Jiri Kosina40f22872014-05-11 15:15:12 +03001315 err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, &rss_map->indir_qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001316 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001317 en_err(priv, "Failed to allocate RSS indirection QP\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001318 goto rss_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001319 }
1320 rss_map->indir_qp.event = mlx4_en_sqp_event;
1321 mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001322 priv->rx_ring[0]->cqn, -1, &context);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001323
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001324 if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
1325 rss_rings = priv->rx_ring_num;
1326 else
1327 rss_rings = priv->prof->rss_rings;
1328
Or Gerlitz876f6e62011-11-26 19:54:58 +00001329 ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
1330 + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
Joe Perches43d620c2011-06-16 19:08:06 +00001331 rss_context = ptr;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001332 rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001333 (rss_map->base_qpn));
Yevgeny Petrilin89efea22011-12-19 21:53:38 +00001334 rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
Or Gerlitz1202d462011-11-26 19:55:02 +00001335 if (priv->mdev->profile.udp_rss) {
1336 rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
1337 rss_context->base_qpn_udp = rss_context->default_qpn;
1338 }
Or Gerlitz837052d2013-12-23 16:09:44 +02001339
1340 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1341 en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
1342 rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
1343 }
1344
Yevgeny Petrilin05339432010-08-24 03:46:42 +00001345 rss_context->flags = rss_mask;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001346 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
Eyal Perry947cbb02014-12-02 18:12:11 +02001347 if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
1348 rss_context->hash_fn = MLX4_RSS_HASH_XOR;
1349 } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
1350 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1351 memcpy(rss_context->rss_key, priv->rss_key,
1352 MLX4_EN_RSS_KEY_SIZE);
Eyal Perry947cbb02014-12-02 18:12:11 +02001353 } else {
1354 en_err(priv, "Unknown RSS hash function requested\n");
1355 err = -EINVAL;
1356 goto indir_err;
1357 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001358 err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
1359 &rss_map->indir_qp, &rss_map->indir_state);
1360 if (err)
1361 goto indir_err;
1362
1363 return 0;
1364
1365indir_err:
1366 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1367 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1368 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1369 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001370rss_err:
1371 for (i = 0; i < good_qps; i++) {
1372 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1373 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1374 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1375 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1376 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001377 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001378 return err;
1379}
1380
1381void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
1382{
1383 struct mlx4_en_dev *mdev = priv->mdev;
1384 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1385 int i;
1386
1387 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1388 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1389 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1390 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001391
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001392 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001393 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1394 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1395 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1396 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1397 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001398 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001399}