blob: df6d352c4e5414450dedc39574a301363eb9c56e [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
45#include "mlx4_en.h"
46
Eric Dumazet51151a12013-06-23 08:17:56 -070047static int mlx4_alloc_pages(struct mlx4_en_priv *priv,
48 struct mlx4_en_rx_alloc *page_alloc,
49 const struct mlx4_en_frag_info *frag_info,
50 gfp_t _gfp)
51{
52 int order;
53 struct page *page;
54 dma_addr_t dma;
55
56 for (order = MLX4_EN_ALLOC_PREFER_ORDER; ;) {
57 gfp_t gfp = _gfp;
58
59 if (order)
60 gfp |= __GFP_COMP | __GFP_NOWARN;
61 page = alloc_pages(gfp, order);
62 if (likely(page))
63 break;
64 if (--order < 0 ||
65 ((PAGE_SIZE << order) < frag_info->frag_size))
66 return -ENOMEM;
67 }
68 dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE << order,
69 PCI_DMA_FROMDEVICE);
70 if (dma_mapping_error(priv->ddev, dma)) {
71 put_page(page);
72 return -ENOMEM;
73 }
Amir Vadai70fbe072013-10-07 13:38:12 +020074 page_alloc->page_size = PAGE_SIZE << order;
Eric Dumazet51151a12013-06-23 08:17:56 -070075 page_alloc->page = page;
76 page_alloc->dma = dma;
Ido Shamay5f6e9802014-11-02 16:26:15 +020077 page_alloc->page_offset = 0;
Eric Dumazet51151a12013-06-23 08:17:56 -070078 /* Not doing get_page() for each frag is a big win
Eric Dumazet98226202014-10-10 04:48:17 -070079 * on asymetric workloads. Note we can not use atomic_set().
Eric Dumazet51151a12013-06-23 08:17:56 -070080 */
Eric Dumazet98226202014-10-10 04:48:17 -070081 atomic_add(page_alloc->page_size / frag_info->frag_stride - 1,
82 &page->_count);
Eric Dumazet51151a12013-06-23 08:17:56 -070083 return 0;
84}
85
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000086static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
87 struct mlx4_en_rx_desc *rx_desc,
88 struct mlx4_en_rx_alloc *frags,
Eric Dumazet51151a12013-06-23 08:17:56 -070089 struct mlx4_en_rx_alloc *ring_alloc,
90 gfp_t gfp)
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000091{
92 struct mlx4_en_rx_alloc page_alloc[MLX4_EN_MAX_RX_FRAGS];
Eric Dumazet51151a12013-06-23 08:17:56 -070093 const struct mlx4_en_frag_info *frag_info;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000094 struct page *page;
95 dma_addr_t dma;
96 int i;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070097
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +000098 for (i = 0; i < priv->num_frags; i++) {
99 frag_info = &priv->frag_info[i];
Eric Dumazet51151a12013-06-23 08:17:56 -0700100 page_alloc[i] = ring_alloc[i];
Amir Vadai70fbe072013-10-07 13:38:12 +0200101 page_alloc[i].page_offset += frag_info->frag_stride;
102
103 if (page_alloc[i].page_offset + frag_info->frag_stride <=
104 ring_alloc[i].page_size)
Eric Dumazet51151a12013-06-23 08:17:56 -0700105 continue;
Amir Vadai70fbe072013-10-07 13:38:12 +0200106
Eric Dumazet51151a12013-06-23 08:17:56 -0700107 if (mlx4_alloc_pages(priv, &page_alloc[i], frag_info, gfp))
108 goto out;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000109 }
110
111 for (i = 0; i < priv->num_frags; i++) {
112 frags[i] = ring_alloc[i];
Amir Vadai70fbe072013-10-07 13:38:12 +0200113 dma = ring_alloc[i].dma + ring_alloc[i].page_offset;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000114 ring_alloc[i] = page_alloc[i];
115 rx_desc->data[i].addr = cpu_to_be64(dma);
116 }
117
118 return 0;
119
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000120out:
121 while (i--) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700122 if (page_alloc[i].page != ring_alloc[i].page) {
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000123 dma_unmap_page(priv->ddev, page_alloc[i].dma,
Amir Vadai70fbe072013-10-07 13:38:12 +0200124 page_alloc[i].page_size, PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700125 page = page_alloc[i].page;
126 atomic_set(&page->_count, 1);
127 put_page(page);
128 }
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000129 }
130 return -ENOMEM;
131}
132
133static void mlx4_en_free_frag(struct mlx4_en_priv *priv,
134 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700135 int i)
136{
Eric Dumazet51151a12013-06-23 08:17:56 -0700137 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Amir Vadai021f1102013-10-07 13:38:13 +0200138 u32 next_frag_end = frags[i].page_offset + 2 * frag_info->frag_stride;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700139
Amir Vadai021f1102013-10-07 13:38:13 +0200140
141 if (next_frag_end > frags[i].page_size)
Amir Vadai70fbe072013-10-07 13:38:12 +0200142 dma_unmap_page(priv->ddev, frags[i].dma, frags[i].page_size,
143 PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700144
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000145 if (frags[i].page)
146 put_page(frags[i].page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700147}
148
149static int mlx4_en_init_allocator(struct mlx4_en_priv *priv,
150 struct mlx4_en_rx_ring *ring)
151{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700152 int i;
Eric Dumazet51151a12013-06-23 08:17:56 -0700153 struct mlx4_en_rx_alloc *page_alloc;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700154
155 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700156 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700157
Eric Dumazet51151a12013-06-23 08:17:56 -0700158 if (mlx4_alloc_pages(priv, &ring->page_alloc[i],
Ido Shamay1ab25f82014-11-02 16:26:16 +0200159 frag_info, GFP_KERNEL | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000160 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700161 }
162 return 0;
163
164out:
165 while (i--) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700166 struct page *page;
167
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700168 page_alloc = &ring->page_alloc[i];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000169 dma_unmap_page(priv->ddev, page_alloc->dma,
Amir Vadai70fbe072013-10-07 13:38:12 +0200170 page_alloc->page_size, PCI_DMA_FROMDEVICE);
Eric Dumazet51151a12013-06-23 08:17:56 -0700171 page = page_alloc->page;
172 atomic_set(&page->_count, 1);
173 put_page(page);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700174 page_alloc->page = NULL;
175 }
176 return -ENOMEM;
177}
178
179static void mlx4_en_destroy_allocator(struct mlx4_en_priv *priv,
180 struct mlx4_en_rx_ring *ring)
181{
182 struct mlx4_en_rx_alloc *page_alloc;
183 int i;
184
185 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700186 const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
187
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700188 page_alloc = &ring->page_alloc[i];
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000189 en_dbg(DRV, priv, "Freeing allocator:%d count:%d\n",
190 i, page_count(page_alloc->page));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700191
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000192 dma_unmap_page(priv->ddev, page_alloc->dma,
Amir Vadai70fbe072013-10-07 13:38:12 +0200193 page_alloc->page_size, PCI_DMA_FROMDEVICE);
194 while (page_alloc->page_offset + frag_info->frag_stride <
195 page_alloc->page_size) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700196 put_page(page_alloc->page);
Amir Vadai70fbe072013-10-07 13:38:12 +0200197 page_alloc->page_offset += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700198 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700199 page_alloc->page = NULL;
200 }
201}
202
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700203static void mlx4_en_init_rx_desc(struct mlx4_en_priv *priv,
204 struct mlx4_en_rx_ring *ring, int index)
205{
206 struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700207 int possible_frags;
208 int i;
209
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700210 /* Set size and memtype fields */
211 for (i = 0; i < priv->num_frags; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700212 rx_desc->data[i].byte_count =
213 cpu_to_be32(priv->frag_info[i].frag_size);
214 rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
215 }
216
217 /* If the number of used fragments does not fill up the ring stride,
218 * remaining (unused) fragments must be padded with null address/size
219 * and a special memory key */
220 possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
221 for (i = priv->num_frags; i < possible_frags; i++) {
222 rx_desc->data[i].byte_count = 0;
223 rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
224 rx_desc->data[i].addr = 0;
225 }
226}
227
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700228static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
Eric Dumazet51151a12013-06-23 08:17:56 -0700229 struct mlx4_en_rx_ring *ring, int index,
230 gfp_t gfp)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700231{
232 struct mlx4_en_rx_desc *rx_desc = ring->buf + (index * ring->stride);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000233 struct mlx4_en_rx_alloc *frags = ring->rx_info +
234 (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700235
Eric Dumazet51151a12013-06-23 08:17:56 -0700236 return mlx4_en_alloc_frags(priv, rx_desc, frags, ring->page_alloc, gfp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700237}
238
239static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
240{
241 *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
242}
243
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000244static void mlx4_en_free_rx_desc(struct mlx4_en_priv *priv,
245 struct mlx4_en_rx_ring *ring,
246 int index)
247{
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000248 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000249 int nr;
250
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000251 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000252 for (nr = 0; nr < priv->num_frags; nr++) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000253 en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000254 mlx4_en_free_frag(priv, frags, nr);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000255 }
256}
257
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700258static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
259{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700260 struct mlx4_en_rx_ring *ring;
261 int ring_ind;
262 int buf_ind;
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000263 int new_size;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700264
265 for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
266 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200267 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700268
269 if (mlx4_en_prepare_rx_desc(priv, ring,
Eric Dumazet51151a12013-06-23 08:17:56 -0700270 ring->actual_size,
Ido Shamay1ab25f82014-11-02 16:26:16 +0200271 GFP_KERNEL | __GFP_COLD)) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700272 if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
Joe Perches1a91de22014-05-07 12:52:57 -0700273 en_err(priv, "Failed to allocate enough rx buffers\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700274 return -ENOMEM;
275 } else {
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000276 new_size = rounddown_pow_of_two(ring->actual_size);
Joe Perches1a91de22014-05-07 12:52:57 -0700277 en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000278 ring->actual_size, new_size);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000279 goto reduce_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700280 }
281 }
282 ring->actual_size++;
283 ring->prod++;
284 }
285 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000286 return 0;
287
288reduce_rings:
289 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200290 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000291 while (ring->actual_size > new_size) {
292 ring->actual_size--;
293 ring->prod--;
294 mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
295 }
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000296 }
297
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700298 return 0;
299}
300
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700301static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
302 struct mlx4_en_rx_ring *ring)
303{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700304 int index;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700305
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000306 en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
307 ring->cons, ring->prod);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700308
309 /* Unmap and free Rx buffers */
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000310 BUG_ON((u32) (ring->prod - ring->cons) > ring->actual_size);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700311 while (ring->cons != ring->prod) {
312 index = ring->cons & ring->size_mask;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000313 en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000314 mlx4_en_free_rx_desc(priv, ring, index);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700315 ++ring->cons;
316 }
317}
318
Ido Shamay02512482014-02-21 12:39:17 +0200319void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
320{
321 int i;
322 int num_of_eqs;
Ido Shamaybb2146b2014-02-21 12:39:18 +0200323 int num_rx_rings;
Ido Shamay02512482014-02-21 12:39:17 +0200324 struct mlx4_dev *dev = mdev->dev;
325
326 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
327 if (!dev->caps.comp_pool)
328 num_of_eqs = max_t(int, MIN_RX_RINGS,
329 min_t(int,
330 dev->caps.num_comp_vectors,
331 DEF_RX_RINGS));
332 else
333 num_of_eqs = min_t(int, MAX_MSIX_P_PORT,
334 dev->caps.comp_pool/
335 dev->caps.num_ports) - 1;
336
Amir Vadaiea1c1af2014-07-22 15:44:12 +0300337 num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
338 min_t(int, num_of_eqs,
339 netif_get_num_default_rss_queues());
Ido Shamay02512482014-02-21 12:39:17 +0200340 mdev->profile.prof[i].rx_ring_num =
Ido Shamaybb2146b2014-02-21 12:39:18 +0200341 rounddown_pow_of_two(num_rx_rings);
Ido Shamay02512482014-02-21 12:39:17 +0200342 }
343}
344
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700345int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200346 struct mlx4_en_rx_ring **pring,
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200347 u32 size, u16 stride, int node)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700348{
349 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200350 struct mlx4_en_rx_ring *ring;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000351 int err = -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700352 int tmp;
353
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200354 ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200355 if (!ring) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200356 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
357 if (!ring) {
358 en_err(priv, "Failed to allocate RX ring structure\n");
359 return -ENOMEM;
360 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200361 }
362
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700363 ring->prod = 0;
364 ring->cons = 0;
365 ring->size = size;
366 ring->size_mask = size - 1;
367 ring->stride = stride;
368 ring->log_stride = ffs(ring->stride) - 1;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700369 ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700370
371 tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000372 sizeof(struct mlx4_en_rx_alloc));
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200373 ring->rx_info = vmalloc_node(tmp, node);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200374 if (!ring->rx_info) {
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200375 ring->rx_info = vmalloc(tmp);
376 if (!ring->rx_info) {
377 err = -ENOMEM;
378 goto err_ring;
379 }
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200380 }
Joe Perchese404dec2012-01-29 12:56:23 +0000381
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000382 en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700383 ring->rx_info, tmp);
384
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200385 /* Allocate HW buffers on provided NUMA node */
386 set_dev_node(&mdev->dev->pdev->dev, node);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700387 err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres,
388 ring->buf_size, 2 * PAGE_SIZE);
Eugenia Emantayev163561a2013-11-07 12:19:54 +0200389 set_dev_node(&mdev->dev->pdev->dev, mdev->dev->numa_node);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700390 if (err)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200391 goto err_info;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700392
393 err = mlx4_en_map_buffer(&ring->wqres.buf);
394 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000395 en_err(priv, "Failed to map RX buffer\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700396 goto err_hwq;
397 }
398 ring->buf = ring->wqres.buf.direct.buf;
399
Amir Vadaiec693d42013-04-23 06:06:49 +0000400 ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
401
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200402 *pring = ring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700403 return 0;
404
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700405err_hwq:
406 mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200407err_info:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700408 vfree(ring->rx_info);
409 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200410err_ring:
411 kfree(ring);
412 *pring = NULL;
413
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700414 return err;
415}
416
417int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
418{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700419 struct mlx4_en_rx_ring *ring;
420 int i;
421 int ring_ind;
422 int err;
423 int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
424 DS_SIZE * priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700425
426 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200427 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700428
429 ring->prod = 0;
430 ring->cons = 0;
431 ring->actual_size = 0;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200432 ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700433
434 ring->stride = stride;
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700435 if (ring->stride <= TXBB_SIZE)
436 ring->buf += TXBB_SIZE;
437
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700438 ring->log_stride = ffs(ring->stride) - 1;
439 ring->buf_size = ring->size * ring->stride;
440
441 memset(ring->buf, 0, ring->buf_size);
442 mlx4_en_update_rx_prod_db(ring);
443
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000444 /* Initialize all descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700445 for (i = 0; i < ring->size; i++)
446 mlx4_en_init_rx_desc(priv, ring, i);
447
448 /* Initialize page allocators */
449 err = mlx4_en_init_allocator(priv, ring);
450 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000451 en_err(priv, "Failed initializing ring allocator\n");
Yevgeny Petrilin60b18092011-04-06 23:25:45 +0000452 if (ring->stride <= TXBB_SIZE)
453 ring->buf -= TXBB_SIZE;
Yevgeny Petrilin9a4f92a2009-04-20 04:24:28 +0000454 ring_ind--;
455 goto err_allocator;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700456 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700457 }
Ingo Molnarb58515b2008-11-25 16:53:32 -0800458 err = mlx4_en_fill_rx_buffers(priv);
459 if (err)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700460 goto err_buffers;
461
462 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200463 ring = priv->rx_ring[ring_ind];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700464
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +0000465 ring->size_mask = ring->actual_size - 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700466 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700467 }
468
469 return 0;
470
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700471err_buffers:
472 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200473 mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700474
475 ring_ind = priv->rx_ring_num - 1;
476err_allocator:
477 while (ring_ind >= 0) {
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200478 if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
479 priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
480 mlx4_en_destroy_allocator(priv, priv->rx_ring[ring_ind]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700481 ring_ind--;
482 }
483 return err;
484}
485
486void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200487 struct mlx4_en_rx_ring **pring,
488 u32 size, u16 stride)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700489{
490 struct mlx4_en_dev *mdev = priv->mdev;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200491 struct mlx4_en_rx_ring *ring = *pring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700492
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700493 mlx4_en_unmap_buffer(&ring->wqres.buf);
Thadeu Lima de Souza Cascardo68355f72012-02-06 08:39:49 +0000494 mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700495 vfree(ring->rx_info);
496 ring->rx_info = NULL;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200497 kfree(ring);
498 *pring = NULL;
Amir Vadai1eb8c692012-07-18 22:33:52 +0000499#ifdef CONFIG_RFS_ACCEL
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200500 mlx4_en_cleanup_filters(priv);
Amir Vadai1eb8c692012-07-18 22:33:52 +0000501#endif
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700502}
503
504void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
505 struct mlx4_en_rx_ring *ring)
506{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700507 mlx4_en_free_rx_buf(priv, ring);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700508 if (ring->stride <= TXBB_SIZE)
509 ring->buf -= TXBB_SIZE;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700510 mlx4_en_destroy_allocator(priv, ring);
511}
512
513
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700514static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
515 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000516 struct mlx4_en_rx_alloc *frags,
Eric Dumazet90278c92011-10-19 18:49:52 +0000517 struct sk_buff *skb,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700518 int length)
519{
Eric Dumazet90278c92011-10-19 18:49:52 +0000520 struct skb_frag_struct *skb_frags_rx = skb_shinfo(skb)->frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700521 struct mlx4_en_frag_info *frag_info;
522 int nr;
523 dma_addr_t dma;
524
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000525 /* Collect used fragments while replacing them in the HW descriptors */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700526 for (nr = 0; nr < priv->num_frags; nr++) {
527 frag_info = &priv->frag_info[nr];
528 if (length <= frag_info->frag_prefix_size)
529 break;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000530 if (!frags[nr].page)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700531 goto fail;
532
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000533 dma = be64_to_cpu(rx_desc->data[nr].addr);
534 dma_sync_single_for_cpu(priv->ddev, dma, frag_info->frag_size,
535 DMA_FROM_DEVICE);
536
537 /* Save page reference in skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000538 __skb_frag_set_page(&skb_frags_rx[nr], frags[nr].page);
539 skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size);
Amir Vadai70fbe072013-10-07 13:38:12 +0200540 skb_frags_rx[nr].page_offset = frags[nr].page_offset;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000541 skb->truesize += frag_info->frag_stride;
Eric Dumazet51151a12013-06-23 08:17:56 -0700542 frags[nr].page = NULL;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700543 }
544 /* Adjust size of last fragment to match actual length */
roel kluin973507c2009-08-08 23:54:21 +0000545 if (nr > 0)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000546 skb_frag_size_set(&skb_frags_rx[nr - 1],
547 length - priv->frag_info[nr - 1].frag_prefix_size);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700548 return nr;
549
550fail:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700551 while (nr > 0) {
552 nr--;
Ian Campbell311761c2011-10-19 23:01:45 +0000553 __skb_frag_unref(&skb_frags_rx[nr]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700554 }
555 return 0;
556}
557
558
559static struct sk_buff *mlx4_en_rx_skb(struct mlx4_en_priv *priv,
560 struct mlx4_en_rx_desc *rx_desc,
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000561 struct mlx4_en_rx_alloc *frags,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700562 unsigned int length)
563{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700564 struct sk_buff *skb;
565 void *va;
566 int used_frags;
567 dma_addr_t dma;
568
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000569 skb = netdev_alloc_skb(priv->dev, SMALL_PACKET_SIZE + NET_IP_ALIGN);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700570 if (!skb) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000571 en_dbg(RX_ERR, priv, "Failed allocating skb\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700572 return NULL;
573 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700574 skb_reserve(skb, NET_IP_ALIGN);
575 skb->len = length;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700576
577 /* Get pointer to first fragment so we could copy the headers into the
578 * (linear part of the) skb */
Amir Vadai70fbe072013-10-07 13:38:12 +0200579 va = page_address(frags[0].page) + frags[0].page_offset;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700580
581 if (length <= SMALL_PACKET_SIZE) {
582 /* We are copying all relevant data to the skb - temporarily
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000583 * sync buffers for the copy */
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700584 dma = be64_to_cpu(rx_desc->data[0].addr);
Yevgeny Petrilinebf8c9a2012-03-06 04:03:34 +0000585 dma_sync_single_for_cpu(priv->ddev, dma, length,
FUJITA Tomonorie4fc8562010-02-04 18:57:42 +0000586 DMA_FROM_DEVICE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700587 skb_copy_to_linear_data(skb, va, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700588 skb->tail += length;
589 } else {
Eric Dumazetcfecec52014-09-05 18:29:45 -0700590 unsigned int pull_len;
591
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700592 /* Move relevant fragments to skb */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000593 used_frags = mlx4_en_complete_rx_desc(priv, rx_desc, frags,
594 skb, length);
Yevgeny Petrilin785a09822009-04-26 20:42:57 +0000595 if (unlikely(!used_frags)) {
596 kfree_skb(skb);
597 return NULL;
598 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700599 skb_shinfo(skb)->nr_frags = used_frags;
600
Eric Dumazetcfecec52014-09-05 18:29:45 -0700601 pull_len = eth_get_headlen(va, SMALL_PACKET_SIZE);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700602 /* Copy headers into the skb linear buffer */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700603 memcpy(skb->data, va, pull_len);
604 skb->tail += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700605
606 /* Skip headers in first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700607 skb_shinfo(skb)->frags[0].page_offset += pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700608
609 /* Adjust size of first fragment */
Eric Dumazetcfecec52014-09-05 18:29:45 -0700610 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], pull_len);
611 skb->data_len = length - pull_len;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700612 }
613 return skb;
614}
615
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000616static void validate_loopback(struct mlx4_en_priv *priv, struct sk_buff *skb)
617{
618 int i;
619 int offset = ETH_HLEN;
620
621 for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++, offset++) {
622 if (*(skb->data + offset) != (unsigned char) (i & 0xff))
623 goto out_loopback;
624 }
625 /* Loopback found */
626 priv->loopback_ok = 1;
627
628out_loopback:
629 dev_kfree_skb_any(skb);
630}
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700631
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000632static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
633 struct mlx4_en_rx_ring *ring)
634{
635 int index = ring->prod & ring->size_mask;
636
637 while ((u32) (ring->prod - ring->cons) < ring->actual_size) {
Ido Shamay1ab25f82014-11-02 16:26:16 +0200638 if (mlx4_en_prepare_rx_desc(priv, ring, index,
639 GFP_ATOMIC | __GFP_COLD))
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000640 break;
641 ring->prod++;
642 index = ring->prod & ring->size_mask;
643 }
644}
645
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700646int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
647{
648 struct mlx4_en_priv *priv = netdev_priv(dev);
Amir Vadaiec693d42013-04-23 06:06:49 +0000649 struct mlx4_en_dev *mdev = priv->mdev;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700650 struct mlx4_cqe *cqe;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +0200651 struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000652 struct mlx4_en_rx_alloc *frags;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700653 struct mlx4_en_rx_desc *rx_desc;
654 struct sk_buff *skb;
655 int index;
656 int nr;
657 unsigned int length;
658 int polled = 0;
659 int ip_summed;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000660 int factor = priv->cqe_factor;
Amir Vadaiec693d42013-04-23 06:06:49 +0000661 u64 timestamp;
Or Gerlitz837052d2013-12-23 16:09:44 +0200662 bool l2_tunnel;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700663
664 if (!priv->port_up)
665 return 0;
666
Eric W. Biederman38be0a32014-03-14 18:05:58 -0700667 if (budget <= 0)
668 return polled;
669
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700670 /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
671 * descriptor offset can be deduced from the CQE index instead of
672 * reading 'cqe->index' */
673 index = cq->mcq.cons_index & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +0300674 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700675
676 /* Process all completed CQEs */
677 while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
678 cq->mcq.cons_index & cq->size)) {
679
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000680 frags = ring->rx_info + (index << priv->log_rx_info);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700681 rx_desc = ring->buf + (index << ring->log_stride);
682
683 /*
684 * make sure we read the CQE after we read the ownership bit
685 */
686 rmb();
687
688 /* Drop packet on bad receive or bad checksum */
689 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
690 MLX4_CQE_OPCODE_ERROR)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700691 en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
692 ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
693 ((struct mlx4_err_cqe *)cqe)->syndrome);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700694 goto next;
695 }
696 if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000697 en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700698 goto next;
699 }
700
Yan Burman79aeacc2013-02-07 02:25:19 +0000701 /* Check if we need to drop the packet if SRIOV is not enabled
702 * and not performing the selftest or flb disabled
703 */
704 if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
705 struct ethhdr *ethh;
706 dma_addr_t dma;
Yan Burman79aeacc2013-02-07 02:25:19 +0000707 /* Get pointer to first fragment since we haven't
708 * skb yet and cast it to ethhdr struct
709 */
710 dma = be64_to_cpu(rx_desc->data[0].addr);
711 dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
712 DMA_FROM_DEVICE);
713 ethh = (struct ethhdr *)(page_address(frags[0].page) +
Amir Vadai70fbe072013-10-07 13:38:12 +0200714 frags[0].page_offset);
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000715
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000716 if (is_multicast_ether_addr(ethh->h_dest)) {
717 struct mlx4_mac_entry *entry;
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000718 struct hlist_head *bucket;
719 unsigned int mac_hash;
720
721 /* Drop the packet, since HW loopback-ed it */
722 mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
723 bucket = &priv->mac_hash[mac_hash];
724 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800725 hlist_for_each_entry_rcu(entry, bucket, hlist) {
Yan Burmanc07cb4b2013-02-07 02:25:25 +0000726 if (ether_addr_equal_64bits(entry->mac,
727 ethh->h_source)) {
728 rcu_read_unlock();
729 goto next;
730 }
731 }
732 rcu_read_unlock();
733 }
Yan Burman79aeacc2013-02-07 02:25:19 +0000734 }
Eugenia Emantayev5b4c4d32011-12-13 04:16:38 +0000735
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700736 /*
737 * Packet is OK - process it.
738 */
739 length = be32_to_cpu(cqe->byte_cnt);
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -0500740 length -= ring->fcs_del;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700741 ring->bytes += length;
742 ring->packets++;
Or Gerlitz837052d2013-12-23 16:09:44 +0200743 l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
744 (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700745
Michał Mirosławc8c64cf2011-04-15 04:50:49 +0000746 if (likely(dev->features & NETIF_F_RXCSUM)) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700747 if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
748 (cqe->checksum == cpu_to_be16(0xffff))) {
Yevgeny Petrilinad043782011-10-18 01:50:56 +0000749 ring->csum_ok++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700750 ip_summed = CHECKSUM_UNNECESSARY;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700751 } else {
752 ip_summed = CHECKSUM_NONE;
Yevgeny Petrilinad043782011-10-18 01:50:56 +0000753 ring->csum_none++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700754 }
755 } else {
756 ip_summed = CHECKSUM_NONE;
Yevgeny Petrilinad043782011-10-18 01:50:56 +0000757 ring->csum_none++;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700758 }
759
Shani Michaelidd65bea2014-11-09 13:51:52 +0200760 /* This packet is eligible for GRO if it is:
761 * - DIX Ethernet (type interpretation)
762 * - TCP/IP (v4)
763 * - without IP options
764 * - not an IP fragment
765 * - no LLS polling in progress
766 */
767 if (!mlx4_en_cq_busy_polling(cq) &&
768 (dev->features & NETIF_F_GRO)) {
769 struct sk_buff *gro_skb = napi_get_frags(&cq->napi);
770 if (!gro_skb)
771 goto next;
772
773 nr = mlx4_en_complete_rx_desc(priv,
774 rx_desc, frags, gro_skb,
775 length);
776 if (!nr)
777 goto next;
778
779 skb_shinfo(gro_skb)->nr_frags = nr;
780 gro_skb->len = length;
781 gro_skb->data_len = length;
782 gro_skb->ip_summed = ip_summed;
783
784 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
785 gro_skb->encapsulation = 1;
786 if ((cqe->vlan_my_qpn &
787 cpu_to_be32(MLX4_CQE_VLAN_PRESENT_MASK)) &&
788 (dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
789 u16 vid = be16_to_cpu(cqe->sl_vid);
790
791 __vlan_hwaccel_put_tag(gro_skb, htons(ETH_P_8021Q), vid);
792 }
793
794 if (dev->features & NETIF_F_RXHASH)
795 skb_set_hash(gro_skb,
796 be32_to_cpu(cqe->immed_rss_invalid),
797 PKT_HASH_TYPE_L3);
798
799 skb_record_rx_queue(gro_skb, cq->ring);
800 skb_mark_napi_id(gro_skb, &cq->napi);
801
802 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
803 timestamp = mlx4_en_get_cqe_ts(cqe);
804 mlx4_en_fill_hwtstamps(mdev,
805 skb_hwtstamps(gro_skb),
806 timestamp);
807 }
808
809 napi_gro_frags(&cq->napi);
810 goto next;
811 }
812
813 /* GRO not possible, complete processing here */
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000814 skb = mlx4_en_rx_skb(priv, rx_desc, frags, length);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700815 if (!skb) {
816 priv->stats.rx_dropped++;
817 goto next;
818 }
819
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000820 if (unlikely(priv->validate_loopback)) {
821 validate_loopback(priv, skb);
822 goto next;
823 }
824
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700825 skb->ip_summed = ip_summed;
826 skb->protocol = eth_type_trans(skb, dev);
David S. Miller0c8dfc82009-01-27 16:22:32 -0800827 skb_record_rx_queue(skb, cq->ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700828
Tom Herbert9ca86002014-08-27 21:27:53 -0700829 if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
830 skb->csum_level = 1;
Or Gerlitz837052d2013-12-23 16:09:44 +0200831
Yevgeny Petrilinad861072011-10-18 01:51:24 +0000832 if (dev->features & NETIF_F_RXHASH)
Tom Herbert69174412013-12-17 23:31:23 -0800833 skb_set_hash(skb,
834 be32_to_cpu(cqe->immed_rss_invalid),
835 PKT_HASH_TYPE_L3);
Yevgeny Petrilinad861072011-10-18 01:51:24 +0000836
Amir Vadaiec693d42013-04-23 06:06:49 +0000837 if ((be32_to_cpu(cqe->vlan_my_qpn) &
838 MLX4_CQE_VLAN_PRESENT_MASK) &&
839 (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000840 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(cqe->sl_vid));
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000841
Amir Vadaiec693d42013-04-23 06:06:49 +0000842 if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
843 timestamp = mlx4_en_get_cqe_ts(cqe);
844 mlx4_en_fill_hwtstamps(mdev, skb_hwtstamps(skb),
845 timestamp);
846 }
847
Eliezer Tamir8b80cda2013-07-10 17:13:26 +0300848 skb_mark_napi_id(skb, &cq->napi);
Amir Vadai9e77a2b2013-06-18 16:18:27 +0300849
Eric Dumazete6a76752014-01-09 10:30:13 -0800850 if (!mlx4_en_cq_busy_polling(cq))
851 napi_gro_receive(&cq->napi, skb);
852 else
853 netif_receive_skb(skb);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700854
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700855next:
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000856 for (nr = 0; nr < priv->num_frags; nr++)
857 mlx4_en_free_frag(priv, frags, nr);
858
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700859 ++cq->mcq.cons_index;
860 index = (cq->mcq.cons_index) & ring->size_mask;
Ido Shamayb1b6b4d2014-09-18 11:51:01 +0300861 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
Ben Hutchingsf1d29a32012-11-16 12:44:56 +0000862 if (++polled == budget)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700863 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700864 }
865
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700866out:
867 AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
868 mlx4_cq_set_ci(&cq->mcq);
869 wmb(); /* ensure HW sees CQ consumer before we post new buffers */
870 ring->cons = cq->mcq.cons_index;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000871 mlx4_en_refill_rx_buffers(priv, ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700872 mlx4_en_update_rx_prod_db(ring);
873 return polled;
874}
875
876
877void mlx4_en_rx_irq(struct mlx4_cq *mcq)
878{
879 struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
880 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
881
Eric Dumazet477b35b2014-10-29 16:54:45 -0700882 if (likely(priv->port_up))
883 napi_schedule_irqoff(&cq->napi);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700884 else
885 mlx4_en_arm_cq(priv, cq);
886}
887
888/* Rx CQ polling - called by NAPI */
889int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
890{
891 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
892 struct net_device *dev = cq->dev;
893 struct mlx4_en_priv *priv = netdev_priv(dev);
894 int done;
895
Amir Vadai9e77a2b2013-06-18 16:18:27 +0300896 if (!mlx4_en_cq_lock_napi(cq))
897 return budget;
898
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700899 done = mlx4_en_process_rx_cq(dev, cq, budget);
900
Amir Vadai9e77a2b2013-06-18 16:18:27 +0300901 mlx4_en_cq_unlock_napi(cq);
902
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700903 /* If we used up all the quota - we're probably not done yet... */
Yuval Atias2eacc232014-05-14 12:15:10 +0300904 if (done == budget) {
Amir Vadai35f6f452014-06-29 11:54:55 +0300905 int cpu_curr;
906 const struct cpumask *aff;
907
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700908 INC_PERF_COUNTER(priv->pstats.napi_quota);
Amir Vadai35f6f452014-06-29 11:54:55 +0300909
910 cpu_curr = smp_processor_id();
911 aff = irq_desc_get_irq_data(cq->irq_desc)->affinity;
912
Eric Dumazet2e1af7d2014-11-10 14:07:20 -0800913 if (likely(cpumask_test_cpu(cpu_curr, aff)))
914 return budget;
915
916 /* Current cpu is not according to smp_irq_affinity -
917 * probably affinity changed. need to stop this NAPI
918 * poll, and restart it on the right CPU
919 */
920 done = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700921 }
Eric Dumazet1a288172014-11-06 21:10:11 -0800922 /* Done for now */
923 napi_complete_done(napi, done);
924 mlx4_en_arm_cq(priv, cq);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700925 return done;
926}
927
Eric Dumazet51151a12013-06-23 08:17:56 -0700928static const int frag_sizes[] = {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700929 FRAG_SZ0,
930 FRAG_SZ1,
931 FRAG_SZ2,
932 FRAG_SZ3
933};
934
935void mlx4_en_calc_rx_buf(struct net_device *dev)
936{
937 struct mlx4_en_priv *priv = netdev_priv(dev);
Yishai Hadasd5b8dff2014-07-08 11:25:23 +0300938 int eff_mtu = dev->mtu + ETH_HLEN + VLAN_HLEN;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700939 int buf_size = 0;
940 int i = 0;
941
942 while (buf_size < eff_mtu) {
943 priv->frag_info[i].frag_size =
944 (eff_mtu > buf_size + frag_sizes[i]) ?
945 frag_sizes[i] : eff_mtu - buf_size;
946 priv->frag_info[i].frag_prefix_size = buf_size;
Ido Shamay5f6e9802014-11-02 16:26:15 +0200947 priv->frag_info[i].frag_stride = ALIGN(frag_sizes[i],
948 SMP_CACHE_BYTES);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700949 buf_size += priv->frag_info[i].frag_size;
950 i++;
951 }
952
953 priv->num_frags = i;
954 priv->rx_skb_size = eff_mtu;
Thadeu Lima de Souza Cascardo4cce66c2012-07-16 07:01:53 +0000955 priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700956
Joe Perches1a91de22014-05-07 12:52:57 -0700957 en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
958 eff_mtu, priv->num_frags);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700959 for (i = 0; i < priv->num_frags; i++) {
Eric Dumazet51151a12013-06-23 08:17:56 -0700960 en_err(priv,
Ido Shamay5f6e9802014-11-02 16:26:15 +0200961 " frag:%d - size:%d prefix:%d stride:%d\n",
Eric Dumazet51151a12013-06-23 08:17:56 -0700962 i,
963 priv->frag_info[i].frag_size,
964 priv->frag_info[i].frag_prefix_size,
Eric Dumazet51151a12013-06-23 08:17:56 -0700965 priv->frag_info[i].frag_stride);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700966 }
967}
968
969/* RSS related functions */
970
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700971static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
972 struct mlx4_en_rx_ring *ring,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700973 enum mlx4_qp_state *state,
974 struct mlx4_qp *qp)
975{
976 struct mlx4_en_dev *mdev = priv->mdev;
977 struct mlx4_qp_context *context;
978 int err = 0;
979
Joe Perches14f8dc42013-02-07 11:46:27 +0000980 context = kmalloc(sizeof(*context), GFP_KERNEL);
981 if (!context)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700982 return -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700983
Jiri Kosina40f22872014-05-11 15:15:12 +0300984 err = mlx4_qp_alloc(mdev->dev, qpn, qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700985 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000986 en_err(priv, "Failed to allocate qp #%x\n", qpn);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700987 goto out;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700988 }
989 qp->event = mlx4_en_sqp_event;
990
991 memset(context, 0, sizeof *context);
Yevgeny Petrilin00d7d7b2010-08-24 03:45:20 +0000992 mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
Amir Vadai0e98b522012-04-04 21:33:24 +0000993 qpn, ring->cqn, -1, context);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -0700994 context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700995
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +0000996 /* Cancel FCS removal if FW allows */
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -0500997 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +0000998 context->param3 |= cpu_to_be32(1 << 29);
Yevgeny Petrilin4a5f4dd2011-11-14 14:25:36 -0500999 ring->fcs_del = ETH_FCS_LEN;
1000 } else
1001 ring->fcs_del = 0;
Yevgeny Petrilinf3a9d1f2011-10-18 01:50:42 +00001002
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001003 err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001004 if (err) {
1005 mlx4_qp_remove(mdev->dev, qp);
1006 mlx4_qp_free(mdev->dev, qp);
1007 }
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001008 mlx4_en_update_rx_prod_db(ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001009out:
1010 kfree(context);
1011 return err;
1012}
1013
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001014int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
1015{
1016 int err;
1017 u32 qpn;
1018
1019 err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn);
1020 if (err) {
1021 en_err(priv, "Failed reserving drop qpn\n");
1022 return err;
1023 }
Jiri Kosina40f22872014-05-11 15:15:12 +03001024 err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp, GFP_KERNEL);
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001025 if (err) {
1026 en_err(priv, "Failed allocating drop qp\n");
1027 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1028 return err;
1029 }
1030
1031 return 0;
1032}
1033
1034void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
1035{
1036 u32 qpn;
1037
1038 qpn = priv->drop_qp.qpn;
1039 mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
1040 mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
1041 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1042}
1043
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001044/* Allocate rx qp's and configure them according to rss map */
1045int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
1046{
1047 struct mlx4_en_dev *mdev = priv->mdev;
1048 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1049 struct mlx4_qp_context context;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001050 struct mlx4_rss_context *rss_context;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001051 int rss_rings;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001052 void *ptr;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001053 u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
Or Gerlitz1202d462011-11-26 19:55:02 +00001054 MLX4_RSS_TCP_IPV6);
Yevgeny Petrilin9f519f62009-08-06 19:28:18 -07001055 int i, qpn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001056 int err = 0;
1057 int good_qps = 0;
Yevgeny Petrilinad861072011-10-18 01:51:24 +00001058 static const u32 rsskey[10] = { 0xD181C62C, 0xF7F4DB5B, 0x1983A2FC,
1059 0x943E1ADB, 0xD9389E6B, 0xD1039C2C, 0xA74499AD,
1060 0x593D56D9, 0xF3253C06, 0x2ADC1FFC};
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001061
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001062 en_dbg(DRV, priv, "Configuring rss steering\n");
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001063 err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
1064 priv->rx_ring_num,
1065 &rss_map->base_qpn);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001066 if (err) {
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001067 en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001068 return err;
1069 }
1070
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001071 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001072 qpn = rss_map->base_qpn + i;
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001073 err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001074 &rss_map->state[i],
1075 &rss_map->qps[i]);
1076 if (err)
1077 goto rss_err;
1078
1079 ++good_qps;
1080 }
1081
1082 /* Configure RSS indirection qp */
Jiri Kosina40f22872014-05-11 15:15:12 +03001083 err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, &rss_map->indir_qp, GFP_KERNEL);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001084 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001085 en_err(priv, "Failed to allocate RSS indirection QP\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001086 goto rss_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001087 }
1088 rss_map->indir_qp.event = mlx4_en_sqp_event;
1089 mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
Eugenia Emantayev41d942d2013-11-07 12:19:52 +02001090 priv->rx_ring[0]->cqn, -1, &context);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001091
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001092 if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
1093 rss_rings = priv->rx_ring_num;
1094 else
1095 rss_rings = priv->prof->rss_rings;
1096
Or Gerlitz876f6e62011-11-26 19:54:58 +00001097 ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
1098 + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
Joe Perches43d620c2011-06-16 19:08:06 +00001099 rss_context = ptr;
Yevgeny Petrilin93d3e362012-01-17 22:54:55 +00001100 rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001101 (rss_map->base_qpn));
Yevgeny Petrilin89efea22011-12-19 21:53:38 +00001102 rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
Or Gerlitz1202d462011-11-26 19:55:02 +00001103 if (priv->mdev->profile.udp_rss) {
1104 rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
1105 rss_context->base_qpn_udp = rss_context->default_qpn;
1106 }
Or Gerlitz837052d2013-12-23 16:09:44 +02001107
1108 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1109 en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
1110 rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
1111 }
1112
Yevgeny Petrilin05339432010-08-24 03:46:42 +00001113 rss_context->flags = rss_mask;
Or Gerlitz876f6e62011-11-26 19:54:58 +00001114 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
Yevgeny Petrilinad861072011-10-18 01:51:24 +00001115 for (i = 0; i < 10; i++)
Yevgeny Petrilin39b2c4e2012-03-06 04:04:07 +00001116 rss_context->rss_key[i] = cpu_to_be32(rsskey[i]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001117
1118 err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
1119 &rss_map->indir_qp, &rss_map->indir_state);
1120 if (err)
1121 goto indir_err;
1122
1123 return 0;
1124
1125indir_err:
1126 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1127 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1128 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1129 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001130rss_err:
1131 for (i = 0; i < good_qps; i++) {
1132 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1133 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1134 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1135 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1136 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001137 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001138 return err;
1139}
1140
1141void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
1142{
1143 struct mlx4_en_dev *mdev = priv->mdev;
1144 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1145 int i;
1146
1147 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1148 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
1149 mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
1150 mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001151
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001152 for (i = 0; i < priv->rx_ring_num; i++) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001153 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1154 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1155 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1156 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1157 }
Yevgeny Petrilinb6b912e2009-08-06 19:27:51 -07001158 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001159}