blob: 4fdd3c37e47bf7c7862b9edf569be6f7f38e8dae [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>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070035#include <linux/mlx4/cq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070037#include <linux/mlx4/qp.h>
38#include <linux/skbuff.h>
Sasha Levinb67bfe02013-02-27 17:06:00 -080039#include <linux/rculist.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070040#include <linux/if_ether.h>
41#include <linux/if_vlan.h>
42#include <linux/vmalloc.h>
Amir Vadai35f6f452014-06-29 11:54:55 +030043#include <linux/irq.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070044
Shani Michaelif8c64552014-11-09 13:51:53 +020045#if IS_ENABLED(CONFIG_IPV6)
46#include <net/ip6_checksum.h>
47#endif
48
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070049#include "mlx4_en.h"
50
Eric Dumazet51151a12013-06-23 08:17:56 -070051static int mlx4_alloc_pages(struct mlx4_en_priv *priv,
52 struct mlx4_en_rx_alloc *page_alloc,
53 const struct mlx4_en_frag_info *frag_info,
54 gfp_t _gfp)
55{
56 int order;
57 struct page *page;
58 dma_addr_t dma;
59
60 for (order = MLX4_EN_ALLOC_PREFER_ORDER; ;) {
61 gfp_t gfp = _gfp;
62
63 if (order)
64 gfp |= __GFP_COMP | __GFP_NOWARN;
65 page = alloc_pages(gfp, order);
66 if (likely(page))
67 break;
68 if (--order < 0 ||
69 ((PAGE_SIZE << order) < frag_info->frag_size))
70 return -ENOMEM;
71 }
72 dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE << order,
73 PCI_DMA_FROMDEVICE);
74 if (dma_mapping_error(priv->ddev, dma)) {
75 put_page(page);
76 return -ENOMEM;
77 }
Amir Vadai70fbe072013-10-07 13:38:12 +020078 page_alloc->page_size = PAGE_SIZE << order;
Eric Dumazet51151a12013-06-23 08:17:56 -070079 page_alloc->page = page;
80 page_alloc->dma = dma;
Ido Shamay5f6e9802014-11-02 16:26:15 +020081 page_alloc->page_offset = 0;
Eric Dumazet51151a12013-06-23 08:17:56 -070082 /* Not doing get_page() for each frag is a big win
Eric Dumazet98226202014-10-10 04:48:17 -070083 * on asymetric workloads. Note we can not use atomic_set().
Eric Dumazet51151a12013-06-23 08:17:56 -070084 */
Eric Dumazet98226202014-10-10 04:48:17 -070085 atomic_add(page_alloc->page_size / frag_info->frag_stride - 1,
86 &page->_count);
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,
Amir Vadai70fbe072013-10-07 13:38:12 +0200128 page_alloc[i].page_size, PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700129 page = page_alloc[i].page;
130 atomic_set(&page->_count, 1);
131 put_page(page);
132 }
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000133 }
134 return -ENOMEM;
135}
136
137static void mlx4_en_free_frag(struct mlx4_en_priv *priv,
138 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700139 int i)
140{
Eric Dumazet51151a12013-06-23 08:17:56 -0700141 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Amir Vadai021f1102013-10-07 13:38:13 +0200142 u32 next_frag_end = frags[i].page_offset + 2 * frag_info->frag_stride;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700143
Amir Vadai021f1102013-10-07 13:38:13 +0200144
145 if (next_frag_end > frags[i].page_size)
Amir Vadai70fbe072013-10-07 13:38:12 +0200146 dma_unmap_page(priv->ddev, frags[i].dma, frags[i].page_size,
147 PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700148
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000149 if (frags[i].page)
150 put_page(frags[i].page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700151}
152
153static int mlx4_en_init_allocator(struct mlx4_en_priv *priv,
154 struct mlx4_en_rx_ring *ring)
155{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700156 int i;
Eric Dumazet51151a12013-06-23 08:17:56 -0700157 struct mlx4_en_rx_alloc *page_alloc;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700158
159 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700160 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700161
Eric Dumazet51151a12013-06-23 08:17:56 -0700162 if (mlx4_alloc_pages(priv, &ring->page_alloc[i],
Ido Shamay1ab25f82014-11-02 16:26:16 +0200163 frag_info, GFP_KERNEL | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000164 goto out;
Ido Shamayb110d2c2015-02-03 17:57:19 +0200165
166 en_dbg(DRV, priv, " frag %d allocator: - size:%d frags:%d\n",
167 i, ring->page_alloc[i].page_size,
168 atomic_read(&ring->page_alloc[i].page->_count));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700169 }
170 return 0;
171
172out:
173 while (i--) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700174 struct page *page;
175
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700176 page_alloc = &ring->page_alloc[i];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000177 dma_unmap_page(priv->ddev, page_alloc->dma,
Amir Vadai70fbe072013-10-07 13:38:12 +0200178 page_alloc->page_size, PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700179 page = page_alloc->page;
180 atomic_set(&page->_count, 1);
181 put_page(page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700182 page_alloc->page = NULL;
183 }
184 return -ENOMEM;
185}
186
187static void mlx4_en_destroy_allocator(struct mlx4_en_priv *priv,
188 struct mlx4_en_rx_ring *ring)
189{
190 struct mlx4_en_rx_alloc *page_alloc;
191 int i;
192
193 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700194 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
195
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700196 page_alloc = &ring->page_alloc[i];
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000197 en_dbg(DRV, priv, "Freeing allocator:%d count:%d\n",
198 i, page_count(page_alloc->page));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700199
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000200 dma_unmap_page(priv->ddev, page_alloc->dma,
Amir Vadai70fbe072013-10-07 13:38:12 +0200201 page_alloc->page_size, PCI_DMA_FROMDEVICE);
202 while (page_alloc->page_offset + frag_info->frag_stride <
203 page_alloc->page_size) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700204 put_page(page_alloc->page);
Amir Vadai70fbe072013-10-07 13:38:12 +0200205 page_alloc->page_offset += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700206 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700207 page_alloc->page = NULL;
208 }
209}
210
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700211static void mlx4_en_init_rx_desc(struct mlx4_en_priv *priv,
212 struct mlx4_en_rx_ring *ring, int index)
213{
214 struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700215 int possible_frags;
216 int i;
217
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700218 /* Set size and memtype fields */
219 for (i = 0; i < priv->num_frags; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700220 rx_desc->data[i].byte_count =
221 cpu_to_be32(priv->frag_info[i].frag_size);
222 rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
223 }
224
225 /* If the number of used fragments does not fill up the ring stride,
226 * remaining (unused) fragments must be padded with null address/size
227 * and a special memory key */
228 possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
229 for (i = priv->num_frags; i < possible_frags; i++) {
230 rx_desc->data[i].byte_count = 0;
231 rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
232 rx_desc->data[i].addr = 0;
233 }
234}
235
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700236static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
Eric Dumazet51151a12013-06-23 08:17:56 -0700237 struct mlx4_en_rx_ring *ring, int index,
238 gfp_t gfp)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700239{
240 struct mlx4_en_rx_desc *rx_desc = ring->buf + (index * ring->stride);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000241 struct mlx4_en_rx_alloc *frags = ring->rx_info +
242 (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700243
Eric Dumazet51151a12013-06-23 08:17:56 -0700244 return mlx4_en_alloc_frags(priv, rx_desc, frags, ring->page_alloc, gfp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700245}
246
247static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
248{
249 *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
250}
251
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000252static void mlx4_en_free_rx_desc(struct mlx4_en_priv *priv,
253 struct mlx4_en_rx_ring *ring,
254 int index)
255{
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000256 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000257 int nr;
258
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000259 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000260 for (nr = 0; nr < priv->num_frags; nr++) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000261 en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000262 mlx4_en_free_frag(priv, frags, nr);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000263 }
264}
265
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700266static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
267{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700268 struct mlx4_en_rx_ring *ring;
269 int ring_ind;
270 int buf_ind;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000271 int new_size;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700272
273 for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
274 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200275 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700276
277 if (mlx4_en_prepare_rx_desc(priv, ring,
Eric Dumazet51151a12013-06-23 08:17:56 -0700278 ring->actual_size,
Ido Shamay1ab25f82014-11-02 16:26:16 +0200279 GFP_KERNEL | __GFP_COLD)) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700280 if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
Joe Perches1a91de22014-05-07 12:52:57 -0700281 en_err(priv, "Failed to allocate enough rx buffers\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700282 return -ENOMEM;
283 } else {
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000284 new_size = rounddown_pow_of_two(ring->actual_size);
Joe Perches1a91de22014-05-07 12:52:57 -0700285 en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000286 ring->actual_size, new_size);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000287 goto reduce_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700288 }
289 }
290 ring->actual_size++;
291 ring->prod++;
292 }
293 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000294 return 0;
295
296reduce_rings:
297 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200298 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000299 while (ring->actual_size > new_size) {
300 ring->actual_size--;
301 ring->prod--;
302 mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
303 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000304 }
305
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700306 return 0;
307}
308
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700309static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
310 struct mlx4_en_rx_ring *ring)
311{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700312 int index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700313
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000314 en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
315 ring->cons, ring->prod);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700316
317 /* Unmap and free Rx buffers */
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000318 BUG_ON((u32) (ring->prod - ring->cons) > ring->actual_size);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700319 while (ring->cons != ring->prod) {
320 index = ring->cons & ring->size_mask;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000321 en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000322 mlx4_en_free_rx_desc(priv, ring, index);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700323 ++ring->cons;
324 }
325}
326
Ido Shamay02512482014-02-21 12:39:17 +0200327void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
328{
329 int i;
330 int num_of_eqs;
Ido Shamaybb2146b2014-02-21 12:39:18 +0200331 int num_rx_rings;
Ido Shamay02512482014-02-21 12:39:17 +0200332 struct mlx4_dev *dev = mdev->dev;
333
334 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
335 if (!dev->caps.comp_pool)
336 num_of_eqs = max_t(int, MIN_RX_RINGS,
337 min_t(int,
338 dev->caps.num_comp_vectors,
339 DEF_RX_RINGS));
340 else
341 num_of_eqs = min_t(int, MAX_MSIX_P_PORT,
342 dev->caps.comp_pool/
343 dev->caps.num_ports) - 1;
344
Amir Vadaiea1c1af2014-07-22 15:44:12 +0300345 num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
346 min_t(int, num_of_eqs,
347 netif_get_num_default_rss_queues());
Ido Shamay02512482014-02-21 12:39:17 +0200348 mdev->profile.prof[i].rx_ring_num =
Ido Shamaybb2146b2014-02-21 12:39:18 +0200349 rounddown_pow_of_two(num_rx_rings);
Ido Shamay02512482014-02-21 12:39:17 +0200350 }
351}
352
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700353int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200354 struct mlx4_en_rx_ring **pring,
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200355 u32 size, u16 stride, int node)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700356{
357 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200358 struct mlx4_en_rx_ring *ring;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000359 int err = -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700360 int tmp;
361
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200362 ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200363 if (!ring) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200364 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
365 if (!ring) {
366 en_err(priv, "Failed to allocate RX ring structure\n");
367 return -ENOMEM;
368 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200369 }
370
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700371 ring->prod = 0;
372 ring->cons = 0;
373 ring->size = size;
374 ring->size_mask = size - 1;
375 ring->stride = stride;
376 ring->log_stride = ffs(ring->stride) - 1;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700377 ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700378
379 tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000380 sizeof(struct mlx4_en_rx_alloc));
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200381 ring->rx_info = vmalloc_node(tmp, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200382 if (!ring->rx_info) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200383 ring->rx_info = vmalloc(tmp);
384 if (!ring->rx_info) {
385 err = -ENOMEM;
386 goto err_ring;
387 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200388 }
Joe Perchese404dec2012-01-29 12:56:23 +0000389
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000390 en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700391 ring->rx_info, tmp);
392
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200393 /* Allocate HW buffers on provided NUMA node */
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200394 set_dev_node(&mdev->dev->persist->pdev->dev, node);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700395 err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres,
396 ring->buf_size, 2 * PAGE_SIZE);
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200397 set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700398 if (err)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200399 goto err_info;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700400
401 err = mlx4_en_map_buffer(&ring->wqres.buf);
402 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000403 en_err(priv, "Failed to map RX buffer\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700404 goto err_hwq;
405 }
406 ring->buf = ring->wqres.buf.direct.buf;
407
Amir Vadaiec693d42013-04-23 06:06:49 +0000408 ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
409
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200410 *pring = ring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700411 return 0;
412
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700413err_hwq:
414 mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200415err_info:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700416 vfree(ring->rx_info);
417 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200418err_ring:
419 kfree(ring);
420 *pring = NULL;
421
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700422 return err;
423}
424
425int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
426{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700427 struct mlx4_en_rx_ring *ring;
428 int i;
429 int ring_ind;
430 int err;
431 int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
432 DS_SIZE * priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700433
434 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200435 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700436
437 ring->prod = 0;
438 ring->cons = 0;
439 ring->actual_size = 0;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200440 ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700441
442 ring->stride = stride;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700443 if (ring->stride <= TXBB_SIZE)
444 ring->buf += TXBB_SIZE;
445
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700446 ring->log_stride = ffs(ring->stride) - 1;
447 ring->buf_size = ring->size * ring->stride;
448
449 memset(ring->buf, 0, ring->buf_size);
450 mlx4_en_update_rx_prod_db(ring);
451
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000452 /* Initialize all descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700453 for (i = 0; i < ring->size; i++)
454 mlx4_en_init_rx_desc(priv, ring, i);
455
456 /* Initialize page allocators */
457 err = mlx4_en_init_allocator(priv, ring);
458 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000459 en_err(priv, "Failed initializing ring allocator\n");
Yevgeny Petrilin60b18092011-04-06 23:25:45 +0000460 if (ring->stride <= TXBB_SIZE)
461 ring->buf -= TXBB_SIZE;
Yevgeny Petrilin9a4f92a2009-04-20 04:24:28 +0000462 ring_ind--;
463 goto err_allocator;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700464 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700465 }
Ingo Molnarb58515b2008-11-25 16:53:32 -0800466 err = mlx4_en_fill_rx_buffers(priv);
467 if (err)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700468 goto err_buffers;
469
470 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200471 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700472
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +0000473 ring->size_mask = ring->actual_size - 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700474 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700475 }
476
477 return 0;
478
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700479err_buffers:
480 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200481 mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700482
483 ring_ind = priv->rx_ring_num - 1;
484err_allocator:
485 while (ring_ind >= 0) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200486 if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
487 priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
488 mlx4_en_destroy_allocator(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700489 ring_ind--;
490 }
491 return err;
492}
493
494void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200495 struct mlx4_en_rx_ring **pring,
496 u32 size, u16 stride)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700497{
498 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200499 struct mlx4_en_rx_ring *ring = *pring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700500
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700501 mlx4_en_unmap_buffer(&ring->wqres.buf);
Thadeu Lima de Souza Cascardo68355f72012-02-06 08:39:49 +0000502 mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700503 vfree(ring->rx_info);
504 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200505 kfree(ring);
506 *pring = NULL;
Amir Vadai1eb8c692012-07-18 22:33:52 +0000507#ifdef CONFIG_RFS_ACCEL
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200508 mlx4_en_cleanup_filters(priv);
Amir Vadai1eb8c692012-07-18 22:33:52 +0000509#endif
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700510}
511
512void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
513 struct mlx4_en_rx_ring *ring)
514{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700515 mlx4_en_free_rx_buf(priv, ring);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700516 if (ring->stride <= TXBB_SIZE)
517 ring->buf -= TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700518 mlx4_en_destroy_allocator(priv, ring);
519}
520
521
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700522static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
523 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000524 struct mlx4_en_rx_alloc *frags,
Eric Dumazet90278c92011-10-19 18:49:52 +0000525 struct sk_buff *skb,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700526 int length)
527{
Eric Dumazet90278c92011-10-19 18:49:52 +0000528 struct skb_frag_struct *skb_frags_rx = skb_shinfo(skb)->frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700529 struct mlx4_en_frag_info *frag_info;
530 int nr;
531 dma_addr_t dma;
532
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000533 /* Collect used fragments while replacing them in the HW descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700534 for (nr = 0; nr < priv->num_frags; nr++) {
535 frag_info = &priv->frag_info[nr];
536 if (length <= frag_info->frag_prefix_size)
537 break;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000538 if (!frags[nr].page)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700539 goto fail;
540
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000541 dma = be64_to_cpu(rx_desc->data[nr].addr);
542 dma_sync_single_for_cpu(priv->ddev, dma, frag_info->frag_size,
543 DMA_FROM_DEVICE);
544
545 /* Save page reference in skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000546 __skb_frag_set_page(&skb_frags_rx[nr], frags[nr].page);
547 skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size);
Amir Vadai70fbe072013-10-07 13:38:12 +0200548 skb_frags_rx[nr].page_offset = frags[nr].page_offset;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000549 skb->truesize += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700550 frags[nr].page = NULL;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700551 }
552 /* Adjust size of last fragment to match actual length */
roel kluin973507c2009-08-08 23:54:21 +0000553 if (nr > 0)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000554 skb_frag_size_set(&skb_frags_rx[nr - 1],
555 length - priv->frag_info[nr - 1].frag_prefix_size);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700556 return nr;
557
558fail:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700559 while (nr > 0) {
560 nr--;
Ian Campbell311761c2011-10-19 23:01:45 +0000561 __skb_frag_unref(&skb_frags_rx[nr]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700562 }
563 return 0;
564}
565
566
567static struct sk_buff *mlx4_en_rx_skb(struct mlx4_en_priv *priv,
568 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000569 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700570 unsigned int length)
571{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700572 struct sk_buff *skb;
573 void *va;
574 int used_frags;
575 dma_addr_t dma;
576
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000577 skb = netdev_alloc_skb(priv->dev, SMALL_PACKET_SIZE + NET_IP_ALIGN);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700578 if (!skb) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000579 en_dbg(RX_ERR, priv, "Failed allocating skb\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700580 return NULL;
581 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700582 skb_reserve(skb, NET_IP_ALIGN);
583 skb->len = length;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700584
585 /* Get pointer to first fragment so we could copy the headers into the
586 * (linear part of the) skb */
Amir Vadai70fbe072013-10-07 13:38:12 +0200587 va = page_address(frags[0].page) + frags[0].page_offset;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700588
589 if (length <= SMALL_PACKET_SIZE) {
590 /* We are copying all relevant data to the skb - temporarily
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000591 * sync buffers for the copy */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700592 dma = be64_to_cpu(rx_desc->data[0].addr);
Yevgeny Petrilinebf8c9a2012-03-06 04:03:34 +0000593 dma_sync_single_for_cpu(priv->ddev, dma, length,
FUJITA Tomonorie4fc8562010-02-04 18:57:42 +0000594 DMA_FROM_DEVICE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700595 skb_copy_to_linear_data(skb, va, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700596 skb->tail += length;
597 } else {
Eric Dumazetcfecec52014-09-05 18:29:45 -0700598 unsigned int pull_len;
599
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700600 /* Move relevant fragments to skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000601 used_frags = mlx4_en_complete_rx_desc(priv, rx_desc, frags,
602 skb, length);
Yevgeny Petrilin785a09822009-04-26 20:42:57 +0000603 if (unlikely(!used_frags)) {
604 kfree_skb(skb);
605 return NULL;
606 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700607 skb_shinfo(skb)->nr_frags = used_frags;
608
Eric Dumazetcfecec52014-09-05 18:29:45 -0700609 pull_len = eth_get_headlen(va, SMALL_PACKET_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700610 /* Copy headers into the skb linear buffer */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700611 memcpy(skb->data, va, pull_len);
612 skb->tail += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700613
614 /* Skip headers in first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700615 skb_shinfo(skb)->frags[0].page_offset += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700616
617 /* Adjust size of first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700618 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], pull_len);
619 skb->data_len = length - pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700620 }
621 return skb;
622}
623
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000624static void validate_loopback(struct mlx4_en_priv *priv, struct sk_buff *skb)
625{
626 int i;
627 int offset = ETH_HLEN;
628
629 for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++, offset++) {
630 if (*(skb->data + offset) != (unsigned char) (i & 0xff))
631 goto out_loopback;
632 }
633 /* Loopback found */
634 priv->loopback_ok = 1;
635
636out_loopback:
637 dev_kfree_skb_any(skb);
638}
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700639
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000640static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
641 struct mlx4_en_rx_ring *ring)
642{
643 int index = ring->prod & ring->size_mask;
644
645 while ((u32) (ring->prod - ring->cons) < ring->actual_size) {
Ido Shamay1ab25f82014-11-02 16:26:16 +0200646 if (mlx4_en_prepare_rx_desc(priv, ring, index,
647 GFP_ATOMIC | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000648 break;
649 ring->prod++;
650 index = ring->prod & ring->size_mask;
651 }
652}
653
Shani Michaelif8c64552014-11-09 13:51:53 +0200654/* When hardware doesn't strip the vlan, we need to calculate the checksum
655 * over it and add it to the hardware's checksum calculation
656 */
657static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
658 struct vlan_hdr *vlanh)
659{
660 return csum_add(hw_checksum, *(__wsum *)vlanh);
661}
662
663/* Although the stack expects checksum which doesn't include the pseudo
664 * header, the HW adds it. To address that, we are subtracting the pseudo
665 * header checksum from the checksum value provided by the HW.
666 */
667static void get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
668 struct iphdr *iph)
669{
670 __u16 length_for_csum = 0;
671 __wsum csum_pseudo_header = 0;
672
673 length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
674 csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
675 length_for_csum, iph->protocol, 0);
676 skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
677}
678
679#if IS_ENABLED(CONFIG_IPV6)
680/* In IPv6 packets, besides subtracting the pseudo header checksum,
681 * we also compute/add the IP header checksum which
682 * is not added by the HW.
683 */
684static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
685 struct ipv6hdr *ipv6h)
686{
687 __wsum csum_pseudo_hdr = 0;
688
689 if (ipv6h->nexthdr == IPPROTO_FRAGMENT || ipv6h->nexthdr == IPPROTO_HOPOPTS)
690 return -1;
691 hw_checksum = csum_add(hw_checksum, (__force __wsum)(ipv6h->nexthdr << 8));
692
693 csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
694 sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
695 csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
696 csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ntohs(ipv6h->nexthdr));
697
698 skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
699 skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
700 return 0;
701}
702#endif
703static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
704 int hwtstamp_rx_filter)
705{
706 __wsum hw_checksum = 0;
707
708 void *hdr = (u8 *)va + sizeof(struct ethhdr);
709
710 hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
711
712 if (((struct ethhdr *)va)->h_proto == htons(ETH_P_8021Q) &&
713 hwtstamp_rx_filter != HWTSTAMP_FILTER_NONE) {
714 /* next protocol non IPv4 or IPv6 */
715 if (((struct vlan_hdr *)hdr)->h_vlan_encapsulated_proto
716 != htons(ETH_P_IP) &&
717 ((struct vlan_hdr *)hdr)->h_vlan_encapsulated_proto
718 != htons(ETH_P_IPV6))
719 return -1;
720 hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
721 hdr += sizeof(struct vlan_hdr);
722 }
723
724 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
725 get_fixed_ipv4_csum(hw_checksum, skb, hdr);
726#if IS_ENABLED(CONFIG_IPV6)
727 else if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
728 if (get_fixed_ipv6_csum(hw_checksum, skb, hdr))
729 return -1;
730#endif
731 return 0;
732}
733
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700734int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
735{
736 struct mlx4_en_priv *priv = netdev_priv(dev);
Amir Vadaiec693d42013-04-23 06:06:49 +0000737 struct mlx4_en_dev *mdev = priv->mdev;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700738 struct mlx4_cqe *cqe;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200739 struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000740 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700741 struct mlx4_en_rx_desc *rx_desc;
742 struct sk_buff *skb;
743 int index;
744 int nr;
745 unsigned int length;
746 int polled = 0;
747 int ip_summed;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000748 int factor = priv->cqe_factor;
Amir Vadaiec693d42013-04-23 06:06:49 +0000749 u64 timestamp;
Or Gerlitz837052d2013-12-23 16:09:44 +0200750 bool l2_tunnel;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700751
752 if (!priv->port_up)
753 return 0;
754
Eric W. Biederman38be0a32014-03-14 18:05:58 -0700755 if (budget <= 0)
756 return polled;
757
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700758 /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
759 * descriptor offset can be deduced from the CQE index instead of
760 * reading 'cqe->index' */
761 index = cq->mcq.cons_index & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +0300762 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700763
764 /* Process all completed CQEs */
765 while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
766 cq->mcq.cons_index & cq->size)) {
767
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000768 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700769 rx_desc = ring->buf + (index << ring->log_stride);
770
771 /*
772 * make sure we read the CQE after we read the ownership bit
773 */
Alexander Duyck12b33752015-04-08 18:49:36 -0700774 dma_rmb();
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700775
776 /* Drop packet on bad receive or bad checksum */
777 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
778 MLX4_CQE_OPCODE_ERROR)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700779 en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
780 ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
781 ((struct mlx4_err_cqe *)cqe)->syndrome);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700782 goto next;
783 }
784 if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000785 en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700786 goto next;
787 }
788
Yan Burman79aeacc2013-02-07 02:25:19 +0000789 /* Check if we need to drop the packet if SRIOV is not enabled
790 * and not performing the selftest or flb disabled
791 */
792 if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
793 struct ethhdr *ethh;
794 dma_addr_t dma;
Yan Burman79aeacc2013-02-07 02:25:19 +0000795 /* Get pointer to first fragment since we haven't
796 * skb yet and cast it to ethhdr struct
797 */
798 dma = be64_to_cpu(rx_desc->data[0].addr);
799 dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
800 DMA_FROM_DEVICE);
801 ethh = (struct ethhdr *)(page_address(frags[0].page) +
Amir Vadai70fbe072013-10-07 13:38:12 +0200802 frags[0].page_offset);
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000803
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000804 if (is_multicast_ether_addr(ethh->h_dest)) {
805 struct mlx4_mac_entry *entry;
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000806 struct hlist_head *bucket;
807 unsigned int mac_hash;
808
809 /* Drop the packet, since HW loopback-ed it */
810 mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
811 bucket = &priv->mac_hash[mac_hash];
812 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800813 hlist_for_each_entry_rcu(entry, bucket, hlist) {
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000814 if (ether_addr_equal_64bits(entry->mac,
815 ethh->h_source)) {
816 rcu_read_unlock();
817 goto next;
818 }
819 }
820 rcu_read_unlock();
821 }
Yan Burman79aeacc2013-02-07 02:25:19 +0000822 }
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000823
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700824 /*
825 * Packet is OK - process it.
826 */
827 length = be32_to_cpu(cqe->byte_cnt);
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -0500828 length -= ring->fcs_del;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700829 ring->bytes += length;
830 ring->packets++;
Or Gerlitz837052d2013-12-23 16:09:44 +0200831 l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
832 (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700833
Michał Mirosławc8c64cf2011-04-15 04:50:49 +0000834 if (likely(dev->features & NETIF_F_RXCSUM)) {
Shani Michaelif8c64552014-11-09 13:51:53 +0200835 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
836 MLX4_CQE_STATUS_UDP)) {
837 if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
838 cqe->checksum == cpu_to_be16(0xffff)) {
839 ip_summed = CHECKSUM_UNNECESSARY;
840 ring->csum_ok++;
841 } else {
842 ip_summed = CHECKSUM_NONE;
843 ring->csum_none++;
844 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700845 } else {
Shani Michaelif8c64552014-11-09 13:51:53 +0200846 if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
847 (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
848 MLX4_CQE_STATUS_IPV6))) {
849 ip_summed = CHECKSUM_COMPLETE;
850 ring->csum_complete++;
851 } else {
852 ip_summed = CHECKSUM_NONE;
853 ring->csum_none++;
854 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700855 }
856 } else {
857 ip_summed = CHECKSUM_NONE;
Yevgeny Petrilinad043782011-10-18 01:50:56 +0000858 ring->csum_none++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700859 }
860
Shani Michaelidd65bea2014-11-09 13:51:52 +0200861 /* This packet is eligible for GRO if it is:
862 * - DIX Ethernet (type interpretation)
863 * - TCP/IP (v4)
864 * - without IP options
865 * - not an IP fragment
866 * - no LLS polling in progress
867 */
868 if (!mlx4_en_cq_busy_polling(cq) &&
869 (dev->features & NETIF_F_GRO)) {
870 struct sk_buff *gro_skb = napi_get_frags(&cq->napi);
871 if (!gro_skb)
872 goto next;
873
874 nr = mlx4_en_complete_rx_desc(priv,
875 rx_desc, frags, gro_skb,
876 length);
877 if (!nr)
878 goto next;
879
Shani Michaelif8c64552014-11-09 13:51:53 +0200880 if (ip_summed == CHECKSUM_COMPLETE) {
881 void *va = skb_frag_address(skb_shinfo(gro_skb)->frags);
882 if (check_csum(cqe, gro_skb, va, ring->hwtstamp_rx_filter)) {
883 ip_summed = CHECKSUM_NONE;
884 ring->csum_none++;
885 ring->csum_complete--;
886 }
887 }
888
Shani Michaelidd65bea2014-11-09 13:51:52 +0200889 skb_shinfo(gro_skb)->nr_frags = nr;
890 gro_skb->len = length;
891 gro_skb->data_len = length;
892 gro_skb->ip_summed = ip_summed;
893
894 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
Or Gerlitzc58942f2014-12-11 10:57:51 +0200895 gro_skb->csum_level = 1;
896
Shani Michaelidd65bea2014-11-09 13:51:52 +0200897 if ((cqe->vlan_my_qpn &
898 cpu_to_be32(MLX4_CQE_VLAN_PRESENT_MASK)) &&
899 (dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
900 u16 vid = be16_to_cpu(cqe->sl_vid);
901
902 __vlan_hwaccel_put_tag(gro_skb, htons(ETH_P_8021Q), vid);
903 }
904
905 if (dev->features & NETIF_F_RXHASH)
906 skb_set_hash(gro_skb,
907 be32_to_cpu(cqe->immed_rss_invalid),
908 PKT_HASH_TYPE_L3);
909
910 skb_record_rx_queue(gro_skb, cq->ring);
911 skb_mark_napi_id(gro_skb, &cq->napi);
912
913 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
914 timestamp = mlx4_en_get_cqe_ts(cqe);
915 mlx4_en_fill_hwtstamps(mdev,
916 skb_hwtstamps(gro_skb),
917 timestamp);
918 }
919
920 napi_gro_frags(&cq->napi);
921 goto next;
922 }
923
924 /* GRO not possible, complete processing here */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000925 skb = mlx4_en_rx_skb(priv, rx_desc, frags, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700926 if (!skb) {
927 priv->stats.rx_dropped++;
928 goto next;
929 }
930
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000931 if (unlikely(priv->validate_loopback)) {
932 validate_loopback(priv, skb);
933 goto next;
934 }
935
Shani Michaelif8c64552014-11-09 13:51:53 +0200936 if (ip_summed == CHECKSUM_COMPLETE) {
937 if (check_csum(cqe, skb, skb->data, ring->hwtstamp_rx_filter)) {
938 ip_summed = CHECKSUM_NONE;
939 ring->csum_complete--;
940 ring->csum_none++;
941 }
942 }
943
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700944 skb->ip_summed = ip_summed;
945 skb->protocol = eth_type_trans(skb, dev);
David S. Miller0c8dfc82009-01-27 16:22:32 -0800946 skb_record_rx_queue(skb, cq->ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700947
Tom Herbert9ca86002014-08-27 21:27:53 -0700948 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
949 skb->csum_level = 1;
Or Gerlitz837052d2013-12-23 16:09:44 +0200950
Yevgeny Petrilinad861072011-10-18 01:51:24 +0000951 if (dev->features & NETIF_F_RXHASH)
Tom Herbert69174412013-12-17 23:31:23 -0800952 skb_set_hash(skb,
953 be32_to_cpu(cqe->immed_rss_invalid),
954 PKT_HASH_TYPE_L3);
Yevgeny Petrilinad861072011-10-18 01:51:24 +0000955
Amir Vadaiec693d42013-04-23 06:06:49 +0000956 if ((be32_to_cpu(cqe->vlan_my_qpn) &
957 MLX4_CQE_VLAN_PRESENT_MASK) &&
958 (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000959 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(cqe->sl_vid));
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000960
Amir Vadaiec693d42013-04-23 06:06:49 +0000961 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
962 timestamp = mlx4_en_get_cqe_ts(cqe);
963 mlx4_en_fill_hwtstamps(mdev, skb_hwtstamps(skb),
964 timestamp);
965 }
966
Eliezer Tamir8b80cda2013-07-10 17:13:26 +0300967 skb_mark_napi_id(skb, &cq->napi);
Amir Vadai9e77a2b2013-06-18 16:18:27 +0300968
Eric Dumazete6a76752014-01-09 10:30:13 -0800969 if (!mlx4_en_cq_busy_polling(cq))
970 napi_gro_receive(&cq->napi, skb);
971 else
972 netif_receive_skb(skb);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700973
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700974next:
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000975 for (nr = 0; nr < priv->num_frags; nr++)
976 mlx4_en_free_frag(priv, frags, nr);
977
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700978 ++cq->mcq.cons_index;
979 index = (cq->mcq.cons_index) & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +0300980 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Ben Hutchingsf1d29a32012-11-16 12:44:56 +0000981 if (++polled == budget)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700982 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700983 }
984
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700985out:
986 AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
987 mlx4_cq_set_ci(&cq->mcq);
988 wmb(); /* ensure HW sees CQ consumer before we post new buffers */
989 ring->cons = cq->mcq.cons_index;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000990 mlx4_en_refill_rx_buffers(priv, ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700991 mlx4_en_update_rx_prod_db(ring);
992 return polled;
993}
994
995
996void mlx4_en_rx_irq(struct mlx4_cq *mcq)
997{
998 struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
999 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
1000
Eric Dumazet477b35b2014-10-29 16:54:45 -07001001 if (likely(priv->port_up))
1002 napi_schedule_irqoff(&cq->napi);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001003 else
1004 mlx4_en_arm_cq(priv, cq);
1005}
1006
1007/* Rx CQ polling - called by NAPI */
1008int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
1009{
1010 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
1011 struct net_device *dev = cq->dev;
1012 struct mlx4_en_priv *priv = netdev_priv(dev);
1013 int done;
1014
Amir Vadai9e77a2b2013-06-18 16:18:27 +03001015 if (!mlx4_en_cq_lock_napi(cq))
1016 return budget;
1017
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001018 done = mlx4_en_process_rx_cq(dev, cq, budget);
1019
Amir Vadai9e77a2b2013-06-18 16:18:27 +03001020 mlx4_en_cq_unlock_napi(cq);
1021
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001022 /* If we used up all the quota - we're probably not done yet... */
Yuval Atias2eacc232014-05-14 12:15:10 +03001023 if (done == budget) {
Amir Vadai35f6f452014-06-29 11:54:55 +03001024 int cpu_curr;
1025 const struct cpumask *aff;
1026
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001027 INC_PERF_COUNTER(priv->pstats.napi_quota);
Amir Vadai35f6f452014-06-29 11:54:55 +03001028
1029 cpu_curr = smp_processor_id();
1030 aff = irq_desc_get_irq_data(cq->irq_desc)->affinity;
1031
Eric Dumazet2e1af7d2014-11-10 14:07:20 -08001032 if (likely(cpumask_test_cpu(cpu_curr, aff)))
1033 return budget;
1034
1035 /* Current cpu is not according to smp_irq_affinity -
1036 * probably affinity changed. need to stop this NAPI
1037 * poll, and restart it on the right CPU
1038 */
1039 done = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001040 }
Eric Dumazet1a288172014-11-06 21:10:11 -08001041 /* Done for now */
1042 napi_complete_done(napi, done);
1043 mlx4_en_arm_cq(priv, cq);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001044 return done;
1045}
1046
Eric Dumazet51151a12013-06-23 08:17:56 -07001047static const int frag_sizes[] = {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001048 FRAG_SZ0,
1049 FRAG_SZ1,
1050 FRAG_SZ2,
1051 FRAG_SZ3
1052};
1053
1054void mlx4_en_calc_rx_buf(struct net_device *dev)
1055{
1056 struct mlx4_en_priv *priv = netdev_priv(dev);
Yishai Hadasd5b8dff2014-07-08 11:25:23 +03001057 int eff_mtu = dev->mtu + ETH_HLEN + VLAN_HLEN;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001058 int buf_size = 0;
1059 int i = 0;
1060
1061 while (buf_size < eff_mtu) {
1062 priv->frag_info[i].frag_size =
1063 (eff_mtu > buf_size + frag_sizes[i]) ?
1064 frag_sizes[i] : eff_mtu - buf_size;
1065 priv->frag_info[i].frag_prefix_size = buf_size;
Ido Shamaye8e7f012015-02-03 17:57:20 +02001066 priv->frag_info[i].frag_stride =
1067 ALIGN(priv->frag_info[i].frag_size,
1068 SMP_CACHE_BYTES);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001069 buf_size += priv->frag_info[i].frag_size;
1070 i++;
1071 }
1072
1073 priv->num_frags = i;
1074 priv->rx_skb_size = eff_mtu;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +00001075 priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001076
Joe Perches1a91de22014-05-07 12:52:57 -07001077 en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
1078 eff_mtu, priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001079 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -07001080 en_err(priv,
Ido Shamay5f6e9802014-11-02 16:26:15 +02001081 " frag:%d - size:%d prefix:%d stride:%d\n",
Eric Dumazet51151a12013-06-23 08:17:56 -07001082 i,
1083 priv->frag_info[i].frag_size,
1084 priv->frag_info[i].frag_prefix_size,
Eric Dumazet51151a12013-06-23 08:17:56 -07001085 priv->frag_info[i].frag_stride);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001086 }
1087}
1088
1089/* RSS related functions */
1090
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001091static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
1092 struct mlx4_en_rx_ring *ring,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001093 enum mlx4_qp_state *state,
1094 struct mlx4_qp *qp)
1095{
1096 struct mlx4_en_dev *mdev = priv->mdev;
1097 struct mlx4_qp_context *context;
1098 int err = 0;
1099
Joe Perches14f8dc42013-02-07 11:46:27 +00001100 context = kmalloc(sizeof(*context), GFP_KERNEL);
1101 if (!context)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001102 return -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001103
Jiri Kosina40f22872014-05-11 15:15:12 +03001104 err = mlx4_qp_alloc(mdev->dev, qpn, qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001105 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001106 en_err(priv, "Failed to allocate qp #%x\n", qpn);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001107 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001108 }
1109 qp->event = mlx4_en_sqp_event;
1110
1111 memset(context, 0, sizeof *context);
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +00001112 mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
Amir Vadai0e98b522012-04-04 21:33:24 +00001113 qpn, ring->cqn, -1, context);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001114 context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001115
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001116 /* Cancel FCS removal if FW allows */
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -05001117 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001118 context->param3 |= cpu_to_be32(1 << 29);
Muhammad Mahajnaf0df3502015-04-02 16:31:21 +03001119 if (priv->dev->features & NETIF_F_RXFCS)
1120 ring->fcs_del = 0;
1121 else
1122 ring->fcs_del = ETH_FCS_LEN;
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -05001123 } else
1124 ring->fcs_del = 0;
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001125
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001126 err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001127 if (err) {
1128 mlx4_qp_remove(mdev->dev, qp);
1129 mlx4_qp_free(mdev->dev, qp);
1130 }
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001131 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001132out:
1133 kfree(context);
1134 return err;
1135}
1136
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001137int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
1138{
1139 int err;
1140 u32 qpn;
1141
Matan Barakd57febe2014-12-11 10:57:57 +02001142 err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
1143 MLX4_RESERVE_A0_QP);
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001144 if (err) {
1145 en_err(priv, "Failed reserving drop qpn\n");
1146 return err;
1147 }
Jiri Kosina40f22872014-05-11 15:15:12 +03001148 err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp, GFP_KERNEL);
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001149 if (err) {
1150 en_err(priv, "Failed allocating drop qp\n");
1151 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1152 return err;
1153 }
1154
1155 return 0;
1156}
1157
1158void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
1159{
1160 u32 qpn;
1161
1162 qpn = priv->drop_qp.qpn;
1163 mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
1164 mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
1165 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1166}
1167
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001168/* Allocate rx qp's and configure them according to rss map */
1169int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
1170{
1171 struct mlx4_en_dev *mdev = priv->mdev;
1172 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1173 struct mlx4_qp_context context;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001174 struct mlx4_rss_context *rss_context;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001175 int rss_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001176 void *ptr;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001177 u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
Or Gerlitz1202d462011-11-26 19:55:02 +00001178 MLX4_RSS_TCP_IPV6);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001179 int i, qpn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001180 int err = 0;
1181 int good_qps = 0;
1182
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001183 en_dbg(DRV, priv, "Configuring rss steering\n");
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001184 err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
1185 priv->rx_ring_num,
Eugenia Emantayevddae0342014-12-11 10:57:54 +02001186 &rss_map->base_qpn, 0);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001187 if (err) {
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001188 en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001189 return err;
1190 }
1191
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001192 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001193 qpn = rss_map->base_qpn + i;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001194 err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001195 &rss_map->state[i],
1196 &rss_map->qps[i]);
1197 if (err)
1198 goto rss_err;
1199
1200 ++good_qps;
1201 }
1202
1203 /* Configure RSS indirection qp */
Jiri Kosina40f22872014-05-11 15:15:12 +03001204 err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, &rss_map->indir_qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001205 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001206 en_err(priv, "Failed to allocate RSS indirection QP\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001207 goto rss_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001208 }
1209 rss_map->indir_qp.event = mlx4_en_sqp_event;
1210 mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001211 priv->rx_ring[0]->cqn, -1, &context);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001212
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001213 if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
1214 rss_rings = priv->rx_ring_num;
1215 else
1216 rss_rings = priv->prof->rss_rings;
1217
Or Gerlitz876f6e62011-11-26 19:54:58 +00001218 ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
1219 + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
Joe Perches43d620c2011-06-16 19:08:06 +00001220 rss_context = ptr;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001221 rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001222 (rss_map->base_qpn));
Yevgeny Petrilin89efea22011-12-19 21:53:38 +00001223 rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
Or Gerlitz1202d462011-11-26 19:55:02 +00001224 if (priv->mdev->profile.udp_rss) {
1225 rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
1226 rss_context->base_qpn_udp = rss_context->default_qpn;
1227 }
Or Gerlitz837052d2013-12-23 16:09:44 +02001228
1229 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1230 en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
1231 rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
1232 }
1233
Yevgeny Petrilin05339432010-08-24 03:46:42 +00001234 rss_context->flags = rss_mask;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001235 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
Eyal Perry947cbb02014-12-02 18:12:11 +02001236 if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
1237 rss_context->hash_fn = MLX4_RSS_HASH_XOR;
1238 } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
1239 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1240 memcpy(rss_context->rss_key, priv->rss_key,
1241 MLX4_EN_RSS_KEY_SIZE);
1242 netdev_rss_key_fill(rss_context->rss_key,
1243 MLX4_EN_RSS_KEY_SIZE);
1244 } else {
1245 en_err(priv, "Unknown RSS hash function requested\n");
1246 err = -EINVAL;
1247 goto indir_err;
1248 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001249 err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
1250 &rss_map->indir_qp, &rss_map->indir_state);
1251 if (err)
1252 goto indir_err;
1253
1254 return 0;
1255
1256indir_err:
1257 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1258 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1259 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1260 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001261rss_err:
1262 for (i = 0; i < good_qps; i++) {
1263 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1264 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1265 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1266 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1267 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001268 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001269 return err;
1270}
1271
1272void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
1273{
1274 struct mlx4_en_dev *mdev = priv->mdev;
1275 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1276 int i;
1277
1278 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1279 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1280 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1281 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001282
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001283 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001284 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1285 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1286 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1287 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1288 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001289 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001290}