xref: /linux/drivers/net/ethernet/mellanox/mlx4/en_rx.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
1 /*
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 
34 #include <linux/bpf.h>
35 #include <linux/bpf_trace.h>
36 #include <linux/mlx4/cq.h>
37 #include <linux/slab.h>
38 #include <linux/mlx4/qp.h>
39 #include <linux/skbuff.h>
40 #include <linux/rculist.h>
41 #include <linux/if_ether.h>
42 #include <linux/if_vlan.h>
43 #include <linux/vmalloc.h>
44 #include <linux/irq.h>
45 #include <linux/skbuff_ref.h>
46 
47 #include <net/ip.h>
48 #if IS_ENABLED(CONFIG_IPV6)
49 #include <net/ip6_checksum.h>
50 #endif
51 #include <net/page_pool/helpers.h>
52 
53 #include "mlx4_en.h"
54 
mlx4_en_alloc_frags(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring,struct mlx4_en_rx_desc * rx_desc,struct mlx4_en_rx_alloc * frags,gfp_t gfp)55 static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
56 			       struct mlx4_en_rx_ring *ring,
57 			       struct mlx4_en_rx_desc *rx_desc,
58 			       struct mlx4_en_rx_alloc *frags,
59 			       gfp_t gfp)
60 {
61 	dma_addr_t dma;
62 	int i;
63 
64 	for (i = 0; i < priv->num_frags; i++, frags++) {
65 		if (!frags->page) {
66 			frags->page = page_pool_alloc_pages(ring->pp, gfp);
67 			if (!frags->page) {
68 				ring->alloc_fail++;
69 				return -ENOMEM;
70 			}
71 			page_pool_fragment_page(frags->page, 1);
72 			frags->page_offset = priv->rx_headroom;
73 
74 			ring->rx_alloc_pages++;
75 		}
76 		dma = page_pool_get_dma_addr(frags->page);
77 		rx_desc->data[i].addr = cpu_to_be64(dma + frags->page_offset);
78 	}
79 	return 0;
80 }
81 
mlx4_en_free_frag(const struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring,struct mlx4_en_rx_alloc * frag)82 static void mlx4_en_free_frag(const struct mlx4_en_priv *priv,
83 			      struct mlx4_en_rx_ring *ring,
84 			      struct mlx4_en_rx_alloc *frag)
85 {
86 	if (frag->page)
87 		page_pool_put_full_page(ring->pp, frag->page, false);
88 	/* We need to clear all fields, otherwise a change of priv->log_rx_info
89 	 * could lead to see garbage later in frag->page.
90 	 */
91 	memset(frag, 0, sizeof(*frag));
92 }
93 
mlx4_en_init_rx_desc(const struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring,int index)94 static void mlx4_en_init_rx_desc(const struct mlx4_en_priv *priv,
95 				 struct mlx4_en_rx_ring *ring, int index)
96 {
97 	struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
98 	int possible_frags;
99 	int i;
100 
101 	/* Set size and memtype fields */
102 	for (i = 0; i < priv->num_frags; i++) {
103 		rx_desc->data[i].byte_count =
104 			cpu_to_be32(priv->frag_info[i].frag_size);
105 		rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
106 	}
107 
108 	/* If the number of used fragments does not fill up the ring stride,
109 	 * remaining (unused) fragments must be padded with null address/size
110 	 * and a special memory key */
111 	possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
112 	for (i = priv->num_frags; i < possible_frags; i++) {
113 		rx_desc->data[i].byte_count = 0;
114 		rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
115 		rx_desc->data[i].addr = 0;
116 	}
117 }
118 
mlx4_en_prepare_rx_desc(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring,int index,gfp_t gfp)119 static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
120 				   struct mlx4_en_rx_ring *ring, int index,
121 				   gfp_t gfp)
122 {
123 	struct mlx4_en_rx_desc *rx_desc = ring->buf +
124 		(index << ring->log_stride);
125 	struct mlx4_en_rx_alloc *frags = ring->rx_info +
126 					(index << priv->log_rx_info);
127 
128 	return mlx4_en_alloc_frags(priv, ring, rx_desc, frags, gfp);
129 }
130 
mlx4_en_is_ring_empty(const struct mlx4_en_rx_ring * ring)131 static bool mlx4_en_is_ring_empty(const struct mlx4_en_rx_ring *ring)
132 {
133 	return ring->prod == ring->cons;
134 }
135 
mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring * ring)136 static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
137 {
138 	*ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
139 }
140 
141 /* slow path */
mlx4_en_free_rx_desc(const struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring,int index)142 static void mlx4_en_free_rx_desc(const struct mlx4_en_priv *priv,
143 				 struct mlx4_en_rx_ring *ring,
144 				 int index)
145 {
146 	struct mlx4_en_rx_alloc *frags;
147 	int nr;
148 
149 	frags = ring->rx_info + (index << priv->log_rx_info);
150 	for (nr = 0; nr < priv->num_frags; nr++) {
151 		en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
152 		mlx4_en_free_frag(priv, ring, frags + nr);
153 	}
154 }
155 
156 /* Function not in fast-path */
mlx4_en_fill_rx_buffers(struct mlx4_en_priv * priv)157 static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
158 {
159 	struct mlx4_en_rx_ring *ring;
160 	int ring_ind;
161 	int buf_ind;
162 	int new_size;
163 
164 	for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
165 		for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
166 			ring = priv->rx_ring[ring_ind];
167 
168 			if (mlx4_en_prepare_rx_desc(priv, ring,
169 						    ring->actual_size,
170 						    GFP_KERNEL)) {
171 				if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
172 					en_err(priv, "Failed to allocate enough rx buffers\n");
173 					return -ENOMEM;
174 				} else {
175 					new_size = rounddown_pow_of_two(ring->actual_size);
176 					en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
177 						ring->actual_size, new_size);
178 					goto reduce_rings;
179 				}
180 			}
181 			ring->actual_size++;
182 			ring->prod++;
183 		}
184 	}
185 	return 0;
186 
187 reduce_rings:
188 	for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
189 		ring = priv->rx_ring[ring_ind];
190 		while (ring->actual_size > new_size) {
191 			ring->actual_size--;
192 			ring->prod--;
193 			mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
194 		}
195 	}
196 
197 	return 0;
198 }
199 
mlx4_en_free_rx_buf(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring)200 static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
201 				struct mlx4_en_rx_ring *ring)
202 {
203 	int index;
204 
205 	en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
206 	       ring->cons, ring->prod);
207 
208 	/* Unmap and free Rx buffers */
209 	for (index = 0; index < ring->size; index++) {
210 		en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
211 		mlx4_en_free_rx_desc(priv, ring, index);
212 	}
213 	ring->cons = 0;
214 	ring->prod = 0;
215 }
216 
mlx4_en_set_num_rx_rings(struct mlx4_en_dev * mdev)217 void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
218 {
219 	int i;
220 	int num_of_eqs;
221 	int num_rx_rings;
222 	struct mlx4_dev *dev = mdev->dev;
223 
224 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
225 		num_of_eqs = max_t(int, MIN_RX_RINGS,
226 				   min_t(int,
227 					 mlx4_get_eqs_per_port(mdev->dev, i),
228 					 DEF_RX_RINGS));
229 
230 		num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
231 			min_t(int, num_of_eqs, num_online_cpus());
232 		mdev->profile.prof[i].rx_ring_num =
233 			rounddown_pow_of_two(num_rx_rings);
234 	}
235 }
236 
mlx4_en_create_rx_ring(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring ** pring,u32 size,u16 stride,int node,int queue_index)237 int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
238 			   struct mlx4_en_rx_ring **pring,
239 			   u32 size, u16 stride, int node, int queue_index)
240 {
241 	struct mlx4_en_dev *mdev = priv->mdev;
242 	struct page_pool_params pp = {};
243 	struct mlx4_en_rx_ring *ring;
244 	int err = -ENOMEM;
245 	int tmp;
246 
247 	ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
248 	if (!ring) {
249 		en_err(priv, "Failed to allocate RX ring structure\n");
250 		return -ENOMEM;
251 	}
252 
253 	ring->prod = 0;
254 	ring->cons = 0;
255 	ring->size = size;
256 	ring->size_mask = size - 1;
257 	ring->stride = stride;
258 	ring->log_stride = ffs(ring->stride) - 1;
259 	ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
260 
261 	pp.flags = PP_FLAG_DMA_MAP;
262 	pp.pool_size = size * DIV_ROUND_UP(priv->rx_skb_size, PAGE_SIZE);
263 	pp.nid = node;
264 	pp.napi = &priv->rx_cq[queue_index]->napi;
265 	pp.netdev = priv->dev;
266 	pp.dev = &mdev->dev->persist->pdev->dev;
267 	pp.dma_dir = priv->dma_dir;
268 
269 	ring->pp = page_pool_create(&pp);
270 	if (!ring->pp)
271 		goto err_ring;
272 
273 	if (xdp_rxq_info_reg(&ring->xdp_rxq, priv->dev, queue_index, 0) < 0)
274 		goto err_pp;
275 
276 	err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq, MEM_TYPE_PAGE_POOL,
277 					 ring->pp);
278 	if (err)
279 		goto err_xdp_info;
280 
281 	tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
282 					sizeof(struct mlx4_en_rx_alloc));
283 	ring->rx_info = kvzalloc_node(tmp, GFP_KERNEL, node);
284 	if (!ring->rx_info) {
285 		err = -ENOMEM;
286 		goto err_xdp_info;
287 	}
288 
289 	en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
290 		 ring->rx_info, tmp);
291 
292 	/* Allocate HW buffers on provided NUMA node */
293 	set_dev_node(&mdev->dev->persist->pdev->dev, node);
294 	err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
295 	set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
296 	if (err)
297 		goto err_info;
298 
299 	ring->buf = ring->wqres.buf.direct.buf;
300 
301 	ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
302 
303 	*pring = ring;
304 	return 0;
305 
306 err_info:
307 	kvfree(ring->rx_info);
308 	ring->rx_info = NULL;
309 err_xdp_info:
310 	xdp_rxq_info_unreg(&ring->xdp_rxq);
311 err_pp:
312 	page_pool_destroy(ring->pp);
313 err_ring:
314 	kfree(ring);
315 	*pring = NULL;
316 
317 	return err;
318 }
319 
mlx4_en_activate_rx_rings(struct mlx4_en_priv * priv)320 int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
321 {
322 	struct mlx4_en_rx_ring *ring;
323 	int i;
324 	int ring_ind;
325 	int err;
326 	int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
327 					DS_SIZE * priv->num_frags);
328 
329 	for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
330 		ring = priv->rx_ring[ring_ind];
331 
332 		ring->prod = 0;
333 		ring->cons = 0;
334 		ring->actual_size = 0;
335 		ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
336 
337 		ring->stride = stride;
338 		if (ring->stride <= TXBB_SIZE) {
339 			/* Stamp first unused send wqe */
340 			__be32 *ptr = (__be32 *)ring->buf;
341 			__be32 stamp = cpu_to_be32(1 << STAMP_SHIFT);
342 			*ptr = stamp;
343 			/* Move pointer to start of rx section */
344 			ring->buf += TXBB_SIZE;
345 		}
346 
347 		ring->log_stride = ffs(ring->stride) - 1;
348 		ring->buf_size = ring->size * ring->stride;
349 
350 		memset(ring->buf, 0, ring->buf_size);
351 		mlx4_en_update_rx_prod_db(ring);
352 
353 		/* Initialize all descriptors */
354 		for (i = 0; i < ring->size; i++)
355 			mlx4_en_init_rx_desc(priv, ring, i);
356 	}
357 	err = mlx4_en_fill_rx_buffers(priv);
358 	if (err)
359 		goto err_buffers;
360 
361 	for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
362 		ring = priv->rx_ring[ring_ind];
363 
364 		ring->size_mask = ring->actual_size - 1;
365 		mlx4_en_update_rx_prod_db(ring);
366 	}
367 
368 	return 0;
369 
370 err_buffers:
371 	for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
372 		mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
373 
374 	ring_ind = priv->rx_ring_num - 1;
375 	while (ring_ind >= 0) {
376 		if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
377 			priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
378 		ring_ind--;
379 	}
380 	return err;
381 }
382 
383 /* We recover from out of memory by scheduling our napi poll
384  * function (mlx4_en_process_cq), which tries to allocate
385  * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
386  */
mlx4_en_recover_from_oom(struct mlx4_en_priv * priv)387 void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
388 {
389 	int ring;
390 
391 	if (!priv->port_up)
392 		return;
393 
394 	for (ring = 0; ring < priv->rx_ring_num; ring++) {
395 		if (mlx4_en_is_ring_empty(priv->rx_ring[ring])) {
396 			local_bh_disable();
397 			napi_schedule(&priv->rx_cq[ring]->napi);
398 			local_bh_enable();
399 		}
400 	}
401 }
402 
mlx4_en_destroy_rx_ring(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring ** pring,u32 size,u16 stride)403 void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
404 			     struct mlx4_en_rx_ring **pring,
405 			     u32 size, u16 stride)
406 {
407 	struct mlx4_en_dev *mdev = priv->mdev;
408 	struct mlx4_en_rx_ring *ring = *pring;
409 	struct bpf_prog *old_prog;
410 
411 	old_prog = rcu_dereference_protected(
412 					ring->xdp_prog,
413 					lockdep_is_held(&mdev->state_lock));
414 	if (old_prog)
415 		bpf_prog_put(old_prog);
416 	xdp_rxq_info_unreg(&ring->xdp_rxq);
417 	mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
418 	kvfree(ring->rx_info);
419 	page_pool_destroy(ring->pp);
420 	ring->rx_info = NULL;
421 	kfree(ring);
422 	*pring = NULL;
423 }
424 
mlx4_en_deactivate_rx_ring(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring)425 void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
426 				struct mlx4_en_rx_ring *ring)
427 {
428 	mlx4_en_free_rx_buf(priv, ring);
429 	if (ring->stride <= TXBB_SIZE)
430 		ring->buf -= TXBB_SIZE;
431 }
432 
433 
mlx4_en_complete_rx_desc(struct mlx4_en_priv * priv,struct mlx4_en_rx_alloc * frags,struct sk_buff * skb,int length)434 static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
435 				    struct mlx4_en_rx_alloc *frags,
436 				    struct sk_buff *skb,
437 				    int length)
438 {
439 	const struct mlx4_en_frag_info *frag_info = priv->frag_info;
440 	unsigned int truesize = 0;
441 	bool release = true;
442 	int nr, frag_size;
443 	struct page *page;
444 	dma_addr_t dma;
445 
446 	/* Collect used fragments while replacing them in the HW descriptors */
447 	for (nr = 0;; frags++) {
448 		frag_size = min_t(int, length, frag_info->frag_size);
449 
450 		page = frags->page;
451 		if (unlikely(!page))
452 			goto fail;
453 
454 		dma = page_pool_get_dma_addr(page);
455 		dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
456 					      frag_size, priv->dma_dir);
457 
458 		__skb_fill_page_desc(skb, nr, page, frags->page_offset,
459 				     frag_size);
460 
461 		truesize += frag_info->frag_stride;
462 		if (frag_info->frag_stride == PAGE_SIZE / 2) {
463 			frags->page_offset ^= PAGE_SIZE / 2;
464 			release = page_count(page) != 1 ||
465 				  atomic_long_read(&page->pp_ref_count) != 1 ||
466 				  page_is_pfmemalloc(page) ||
467 				  page_to_nid(page) != numa_mem_id();
468 		} else if (!priv->rx_headroom) {
469 			/* rx_headroom for non XDP setup is always 0.
470 			 * When XDP is set, the above condition will
471 			 * guarantee page is always released.
472 			 */
473 			u32 sz_align = ALIGN(frag_size, SMP_CACHE_BYTES);
474 
475 			frags->page_offset += sz_align;
476 			release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
477 		}
478 		if (release) {
479 			frags->page = NULL;
480 		} else {
481 			page_pool_ref_page(page);
482 		}
483 
484 		nr++;
485 		length -= frag_size;
486 		if (!length)
487 			break;
488 		frag_info++;
489 	}
490 	skb->truesize += truesize;
491 	return nr;
492 
493 fail:
494 	while (nr > 0) {
495 		nr--;
496 		__skb_frag_unref(skb_shinfo(skb)->frags + nr, false);
497 	}
498 	return 0;
499 }
500 
validate_loopback(struct mlx4_en_priv * priv,void * va)501 static void validate_loopback(struct mlx4_en_priv *priv, void *va)
502 {
503 	const unsigned char *data = va + ETH_HLEN;
504 	int i;
505 
506 	for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++) {
507 		if (data[i] != (unsigned char)i)
508 			return;
509 	}
510 	/* Loopback found */
511 	priv->loopback_ok = 1;
512 }
513 
mlx4_en_refill_rx_buffers(struct mlx4_en_priv * priv,struct mlx4_en_rx_ring * ring)514 static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
515 				      struct mlx4_en_rx_ring *ring)
516 {
517 	u32 missing = ring->actual_size - (ring->prod - ring->cons);
518 
519 	/* Try to batch allocations, but not too much. */
520 	if (missing < 8)
521 		return;
522 	do {
523 		if (mlx4_en_prepare_rx_desc(priv, ring,
524 					    ring->prod & ring->size_mask,
525 					    GFP_ATOMIC | __GFP_MEMALLOC))
526 			break;
527 		ring->prod++;
528 	} while (likely(--missing));
529 
530 	mlx4_en_update_rx_prod_db(ring);
531 }
532 
533 /* When hardware doesn't strip the vlan, we need to calculate the checksum
534  * over it and add it to the hardware's checksum calculation
535  */
get_fixed_vlan_csum(__wsum hw_checksum,struct vlan_hdr * vlanh)536 static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
537 					 struct vlan_hdr *vlanh)
538 {
539 	return csum_add(hw_checksum, *(__wsum *)vlanh);
540 }
541 
542 /* Although the stack expects checksum which doesn't include the pseudo
543  * header, the HW adds it. To address that, we are subtracting the pseudo
544  * header checksum from the checksum value provided by the HW.
545  */
get_fixed_ipv4_csum(__wsum hw_checksum,struct sk_buff * skb,struct iphdr * iph)546 static int get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
547 			       struct iphdr *iph)
548 {
549 	__u16 length_for_csum = 0;
550 	__wsum csum_pseudo_header = 0;
551 	__u8 ipproto = iph->protocol;
552 
553 	if (unlikely(ipproto == IPPROTO_SCTP))
554 		return -1;
555 
556 	length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
557 	csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
558 						length_for_csum, ipproto, 0);
559 	skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
560 	return 0;
561 }
562 
563 #if IS_ENABLED(CONFIG_IPV6)
564 /* In IPv6 packets, hw_checksum lacks 6 bytes from IPv6 header:
565  * 4 first bytes : priority, version, flow_lbl
566  * and 2 additional bytes : nexthdr, hop_limit.
567  */
get_fixed_ipv6_csum(__wsum hw_checksum,struct sk_buff * skb,struct ipv6hdr * ipv6h)568 static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
569 			       struct ipv6hdr *ipv6h)
570 {
571 	__u8 nexthdr = ipv6h->nexthdr;
572 	__wsum temp;
573 
574 	if (unlikely(nexthdr == IPPROTO_FRAGMENT ||
575 		     nexthdr == IPPROTO_HOPOPTS ||
576 		     nexthdr == IPPROTO_SCTP))
577 		return -1;
578 
579 	/* priority, version, flow_lbl */
580 	temp = csum_add(hw_checksum, *(__wsum *)ipv6h);
581 	/* nexthdr and hop_limit */
582 	skb->csum = csum_add(temp, (__force __wsum)*(__be16 *)&ipv6h->nexthdr);
583 	return 0;
584 }
585 #endif
586 
587 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
588 
589 /* We reach this function only after checking that any of
590  * the (IPv4 | IPv6) bits are set in cqe->status.
591  */
check_csum(struct mlx4_cqe * cqe,struct sk_buff * skb,void * va,netdev_features_t dev_features)592 static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
593 		      netdev_features_t dev_features)
594 {
595 	__wsum hw_checksum = 0;
596 	void *hdr;
597 
598 	/* CQE csum doesn't cover padding octets in short ethernet
599 	 * frames. And the pad field is appended prior to calculating
600 	 * and appending the FCS field.
601 	 *
602 	 * Detecting these padded frames requires to verify and parse
603 	 * IP headers, so we simply force all those small frames to skip
604 	 * checksum complete.
605 	 */
606 	if (short_frame(skb->len))
607 		return -EINVAL;
608 
609 	hdr = (u8 *)va + sizeof(struct ethhdr);
610 	hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
611 
612 	if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
613 	    !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
614 		hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
615 		hdr += sizeof(struct vlan_hdr);
616 	}
617 
618 #if IS_ENABLED(CONFIG_IPV6)
619 	if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
620 		return get_fixed_ipv6_csum(hw_checksum, skb, hdr);
621 #endif
622 	return get_fixed_ipv4_csum(hw_checksum, skb, hdr);
623 }
624 
625 #if IS_ENABLED(CONFIG_IPV6)
626 #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4 | MLX4_CQE_STATUS_IPV6)
627 #else
628 #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4)
629 #endif
630 
631 struct mlx4_en_xdp_buff {
632 	struct xdp_buff xdp;
633 	struct mlx4_cqe *cqe;
634 	struct mlx4_en_dev *mdev;
635 	struct mlx4_en_rx_ring *ring;
636 	struct net_device *dev;
637 };
638 
mlx4_en_xdp_rx_timestamp(const struct xdp_md * ctx,u64 * timestamp)639 int mlx4_en_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
640 {
641 	struct mlx4_en_xdp_buff *_ctx = (void *)ctx;
642 
643 	if (unlikely(_ctx->ring->hwtstamp_rx_filter != HWTSTAMP_FILTER_ALL))
644 		return -ENODATA;
645 
646 	*timestamp = mlx4_en_get_hwtstamp(_ctx->mdev,
647 					  mlx4_en_get_cqe_ts(_ctx->cqe));
648 	return 0;
649 }
650 
mlx4_en_xdp_rx_hash(const struct xdp_md * ctx,u32 * hash,enum xdp_rss_hash_type * rss_type)651 int mlx4_en_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
652 			enum xdp_rss_hash_type *rss_type)
653 {
654 	struct mlx4_en_xdp_buff *_ctx = (void *)ctx;
655 	struct mlx4_cqe *cqe = _ctx->cqe;
656 	enum xdp_rss_hash_type xht = 0;
657 	__be16 status;
658 
659 	if (unlikely(!(_ctx->dev->features & NETIF_F_RXHASH)))
660 		return -ENODATA;
661 
662 	*hash = be32_to_cpu(cqe->immed_rss_invalid);
663 	status = cqe->status;
664 	if (status & cpu_to_be16(MLX4_CQE_STATUS_TCP))
665 		xht = XDP_RSS_L4_TCP;
666 	if (status & cpu_to_be16(MLX4_CQE_STATUS_UDP))
667 		xht = XDP_RSS_L4_UDP;
668 	if (status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 | MLX4_CQE_STATUS_IPV4F))
669 		xht |= XDP_RSS_L3_IPV4;
670 	if (status & cpu_to_be16(MLX4_CQE_STATUS_IPV6)) {
671 		xht |= XDP_RSS_L3_IPV6;
672 		if (cqe->ipv6_ext_mask)
673 			xht |= XDP_RSS_L3_DYNHDR;
674 	}
675 	*rss_type = xht;
676 
677 	return 0;
678 }
679 
mlx4_en_process_rx_cq(struct net_device * dev,struct mlx4_en_cq * cq,int budget)680 int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
681 {
682 	struct mlx4_en_priv *priv = netdev_priv(dev);
683 	struct mlx4_en_xdp_buff mxbuf = {};
684 	int factor = priv->cqe_factor;
685 	struct mlx4_en_rx_ring *ring;
686 	struct bpf_prog *xdp_prog;
687 	int cq_ring = cq->ring;
688 	bool doorbell_pending;
689 	bool xdp_redir_flush;
690 	struct mlx4_cqe *cqe;
691 	int polled = 0;
692 	int index;
693 
694 	if (unlikely(!priv->port_up || budget <= 0))
695 		return 0;
696 
697 	ring = priv->rx_ring[cq_ring];
698 
699 	xdp_prog = rcu_dereference_bh(ring->xdp_prog);
700 	xdp_init_buff(&mxbuf.xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
701 	doorbell_pending = false;
702 	xdp_redir_flush = false;
703 
704 	/* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
705 	 * descriptor offset can be deduced from the CQE index instead of
706 	 * reading 'cqe->index' */
707 	index = cq->mcq.cons_index & ring->size_mask;
708 	cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
709 
710 	/* Process all completed CQEs */
711 	while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
712 		    cq->mcq.cons_index & cq->size)) {
713 		struct mlx4_en_rx_alloc *frags;
714 		enum pkt_hash_types hash_type;
715 		struct sk_buff *skb;
716 		unsigned int length;
717 		int ip_summed;
718 		void *va;
719 		int nr;
720 
721 		frags = ring->rx_info + (index << priv->log_rx_info);
722 		va = page_address(frags[0].page) + frags[0].page_offset;
723 		net_prefetchw(va);
724 		/*
725 		 * make sure we read the CQE after we read the ownership bit
726 		 */
727 		dma_rmb();
728 
729 		/* Drop packet on bad receive or bad checksum */
730 		if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
731 						MLX4_CQE_OPCODE_ERROR)) {
732 			en_err(priv, "CQE completed in error - vendor syndrome:%d syndrome:%d\n",
733 			       ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
734 			       ((struct mlx4_err_cqe *)cqe)->syndrome);
735 			goto next;
736 		}
737 		if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
738 			en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
739 			goto next;
740 		}
741 
742 		/* Check if we need to drop the packet if SRIOV is not enabled
743 		 * and not performing the selftest or flb disabled
744 		 */
745 		if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
746 			const struct ethhdr *ethh = va;
747 			dma_addr_t dma;
748 			/* Get pointer to first fragment since we haven't
749 			 * skb yet and cast it to ethhdr struct
750 			 */
751 			dma = page_pool_get_dma_addr(frags[0].page);
752 			dma += frags[0].page_offset;
753 			dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
754 						DMA_FROM_DEVICE);
755 
756 			if (is_multicast_ether_addr(ethh->h_dest)) {
757 				struct mlx4_mac_entry *entry;
758 				struct hlist_head *bucket;
759 				unsigned int mac_hash;
760 
761 				/* Drop the packet, since HW loopback-ed it */
762 				mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
763 				bucket = &priv->mac_hash[mac_hash];
764 				hlist_for_each_entry_rcu_bh(entry, bucket, hlist) {
765 					if (ether_addr_equal_64bits(entry->mac,
766 								    ethh->h_source))
767 						goto next;
768 				}
769 			}
770 		}
771 
772 		if (unlikely(priv->validate_loopback)) {
773 			validate_loopback(priv, va);
774 			goto next;
775 		}
776 
777 		/*
778 		 * Packet is OK - process it.
779 		 */
780 		length = be32_to_cpu(cqe->byte_cnt);
781 		length -= ring->fcs_del;
782 
783 		/* A bpf program gets first chance to drop the packet. It may
784 		 * read bytes but not past the end of the frag.
785 		 */
786 		if (xdp_prog) {
787 			dma_addr_t dma;
788 			void *orig_data;
789 			u32 act;
790 
791 			dma = page_pool_get_dma_addr(frags[0].page);
792 			dma += frags[0].page_offset;
793 			dma_sync_single_for_cpu(priv->ddev, dma,
794 						priv->frag_info[0].frag_size,
795 						DMA_FROM_DEVICE);
796 
797 			xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
798 					 frags[0].page_offset, length, true);
799 			orig_data = mxbuf.xdp.data;
800 			mxbuf.cqe = cqe;
801 			mxbuf.mdev = priv->mdev;
802 			mxbuf.ring = ring;
803 			mxbuf.dev = dev;
804 
805 			act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
806 
807 			length = mxbuf.xdp.data_end - mxbuf.xdp.data;
808 			if (mxbuf.xdp.data != orig_data) {
809 				frags[0].page_offset = mxbuf.xdp.data -
810 					mxbuf.xdp.data_hard_start;
811 				va = mxbuf.xdp.data;
812 			}
813 
814 			switch (act) {
815 			case XDP_PASS:
816 				break;
817 			case XDP_REDIRECT:
818 				if (likely(!xdp_do_redirect(dev, &mxbuf.xdp, xdp_prog))) {
819 					ring->xdp_redirect++;
820 					xdp_redir_flush = true;
821 					frags[0].page = NULL;
822 					goto next;
823 				}
824 				ring->xdp_redirect_fail++;
825 				trace_xdp_exception(dev, xdp_prog, act);
826 				goto xdp_drop_no_cnt;
827 			case XDP_TX:
828 				if (likely(!mlx4_en_xmit_frame(ring, frags, priv,
829 							length, cq_ring,
830 							&doorbell_pending))) {
831 					frags[0].page = NULL;
832 					goto next;
833 				}
834 				trace_xdp_exception(dev, xdp_prog, act);
835 				goto xdp_drop_no_cnt; /* Drop on xmit failure */
836 			default:
837 				bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
838 				fallthrough;
839 			case XDP_ABORTED:
840 				trace_xdp_exception(dev, xdp_prog, act);
841 				fallthrough;
842 			case XDP_DROP:
843 				ring->xdp_drop++;
844 xdp_drop_no_cnt:
845 				goto next;
846 			}
847 		}
848 
849 		ring->bytes += length;
850 		ring->packets++;
851 
852 		skb = napi_get_frags(&cq->napi);
853 		if (unlikely(!skb))
854 			goto next;
855 		skb_mark_for_recycle(skb);
856 
857 		if (unlikely(ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL)) {
858 			u64 timestamp = mlx4_en_get_cqe_ts(cqe);
859 
860 			mlx4_en_fill_hwtstamps(priv->mdev, skb_hwtstamps(skb),
861 					       timestamp);
862 		}
863 		skb_record_rx_queue(skb, cq_ring);
864 
865 		if (likely(dev->features & NETIF_F_RXCSUM)) {
866 			/* TODO: For IP non TCP/UDP packets when csum complete is
867 			 * not an option (not supported or any other reason) we can
868 			 * actually check cqe IPOK status bit and report
869 			 * CHECKSUM_UNNECESSARY rather than CHECKSUM_NONE
870 			 */
871 			if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
872 						       MLX4_CQE_STATUS_UDP)) &&
873 			    (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
874 			    cqe->checksum == cpu_to_be16(0xffff)) {
875 				bool l2_tunnel;
876 
877 				l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
878 					(cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
879 				ip_summed = CHECKSUM_UNNECESSARY;
880 				hash_type = PKT_HASH_TYPE_L4;
881 				if (l2_tunnel)
882 					skb->csum_level = 1;
883 				ring->csum_ok++;
884 			} else {
885 				if (!(priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
886 				      (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IP_ANY))))
887 					goto csum_none;
888 				if (check_csum(cqe, skb, va, dev->features))
889 					goto csum_none;
890 				ip_summed = CHECKSUM_COMPLETE;
891 				hash_type = PKT_HASH_TYPE_L3;
892 				ring->csum_complete++;
893 			}
894 		} else {
895 csum_none:
896 			ip_summed = CHECKSUM_NONE;
897 			hash_type = PKT_HASH_TYPE_L3;
898 			ring->csum_none++;
899 		}
900 		skb->ip_summed = ip_summed;
901 		if (dev->features & NETIF_F_RXHASH)
902 			skb_set_hash(skb,
903 				     be32_to_cpu(cqe->immed_rss_invalid),
904 				     hash_type);
905 
906 		if ((cqe->vlan_my_qpn &
907 		     cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
908 		    (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
909 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
910 					       be16_to_cpu(cqe->sl_vid));
911 		else if ((cqe->vlan_my_qpn &
912 			  cpu_to_be32(MLX4_CQE_SVLAN_PRESENT_MASK)) &&
913 			 (dev->features & NETIF_F_HW_VLAN_STAG_RX))
914 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
915 					       be16_to_cpu(cqe->sl_vid));
916 
917 		nr = mlx4_en_complete_rx_desc(priv, frags, skb, length);
918 		if (likely(nr)) {
919 			skb_shinfo(skb)->nr_frags = nr;
920 			skb->len = length;
921 			skb->data_len = length;
922 			napi_gro_frags(&cq->napi);
923 		} else {
924 			__vlan_hwaccel_clear_tag(skb);
925 			skb_clear_hash(skb);
926 		}
927 next:
928 		++cq->mcq.cons_index;
929 		index = (cq->mcq.cons_index) & ring->size_mask;
930 		cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
931 		if (unlikely(++polled == budget))
932 			break;
933 	}
934 
935 	if (xdp_redir_flush)
936 		xdp_do_flush();
937 
938 	if (likely(polled)) {
939 		if (doorbell_pending) {
940 			priv->tx_cq[TX_XDP][cq_ring]->xdp_busy = true;
941 			mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq_ring]);
942 		}
943 
944 		mlx4_cq_set_ci(&cq->mcq);
945 		wmb(); /* ensure HW sees CQ consumer before we post new buffers */
946 		ring->cons = cq->mcq.cons_index;
947 	}
948 
949 	mlx4_en_refill_rx_buffers(priv, ring);
950 
951 	return polled;
952 }
953 
954 
mlx4_en_rx_irq(struct mlx4_cq * mcq)955 void mlx4_en_rx_irq(struct mlx4_cq *mcq)
956 {
957 	struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
958 	struct mlx4_en_priv *priv = netdev_priv(cq->dev);
959 
960 	if (likely(priv->port_up))
961 		napi_schedule_irqoff(&cq->napi);
962 	else
963 		mlx4_en_arm_cq(priv, cq);
964 }
965 
966 /* Rx CQ polling - called by NAPI */
mlx4_en_poll_rx_cq(struct napi_struct * napi,int budget)967 int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
968 {
969 	struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
970 	struct net_device *dev = cq->dev;
971 	struct mlx4_en_priv *priv = netdev_priv(dev);
972 	struct mlx4_en_cq *xdp_tx_cq = NULL;
973 	bool clean_complete = true;
974 	int done;
975 
976 	if (!budget)
977 		return 0;
978 
979 	if (priv->tx_ring_num[TX_XDP]) {
980 		xdp_tx_cq = priv->tx_cq[TX_XDP][cq->ring];
981 		if (xdp_tx_cq->xdp_busy) {
982 			clean_complete = mlx4_en_process_tx_cq(dev, xdp_tx_cq,
983 							       budget) < budget;
984 			xdp_tx_cq->xdp_busy = !clean_complete;
985 		}
986 	}
987 
988 	done = mlx4_en_process_rx_cq(dev, cq, budget);
989 
990 	/* If we used up all the quota - we're probably not done yet... */
991 	if (done == budget || !clean_complete) {
992 		int cpu_curr;
993 
994 		/* in case we got here because of !clean_complete */
995 		done = budget;
996 
997 		cpu_curr = smp_processor_id();
998 
999 		if (likely(cpumask_test_cpu(cpu_curr, cq->aff_mask)))
1000 			return budget;
1001 
1002 		/* Current cpu is not according to smp_irq_affinity -
1003 		 * probably affinity changed. Need to stop this NAPI
1004 		 * poll, and restart it on the right CPU.
1005 		 * Try to avoid returning a too small value (like 0),
1006 		 * to not fool net_rx_action() and its netdev_budget
1007 		 */
1008 		if (done)
1009 			done--;
1010 	}
1011 	/* Done for now */
1012 	if (likely(napi_complete_done(napi, done)))
1013 		mlx4_en_arm_cq(priv, cq);
1014 	return done;
1015 }
1016 
mlx4_en_calc_rx_buf(struct net_device * dev)1017 void mlx4_en_calc_rx_buf(struct net_device *dev)
1018 {
1019 	struct mlx4_en_priv *priv = netdev_priv(dev);
1020 	int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
1021 	int i = 0;
1022 
1023 	/* bpf requires buffers to be set up as 1 packet per page.
1024 	 * This only works when num_frags == 1.
1025 	 */
1026 	if (priv->tx_ring_num[TX_XDP]) {
1027 		priv->frag_info[0].frag_size = eff_mtu;
1028 		/* This will gain efficient xdp frame recycling at the
1029 		 * expense of more costly truesize accounting
1030 		 */
1031 		priv->frag_info[0].frag_stride = PAGE_SIZE;
1032 		priv->dma_dir = DMA_BIDIRECTIONAL;
1033 		priv->rx_headroom = XDP_PACKET_HEADROOM;
1034 		i = 1;
1035 	} else {
1036 		int frag_size_max = 2048, buf_size = 0;
1037 
1038 		/* should not happen, right ? */
1039 		if (eff_mtu > PAGE_SIZE + (MLX4_EN_MAX_RX_FRAGS - 1) * 2048)
1040 			frag_size_max = PAGE_SIZE;
1041 
1042 		while (buf_size < eff_mtu) {
1043 			int frag_stride, frag_size = eff_mtu - buf_size;
1044 			int pad, nb;
1045 
1046 			if (i < MLX4_EN_MAX_RX_FRAGS - 1)
1047 				frag_size = min(frag_size, frag_size_max);
1048 
1049 			priv->frag_info[i].frag_size = frag_size;
1050 			frag_stride = ALIGN(frag_size, SMP_CACHE_BYTES);
1051 			/* We can only pack 2 1536-bytes frames in on 4K page
1052 			 * Therefore, each frame would consume more bytes (truesize)
1053 			 */
1054 			nb = PAGE_SIZE / frag_stride;
1055 			pad = (PAGE_SIZE - nb * frag_stride) / nb;
1056 			pad &= ~(SMP_CACHE_BYTES - 1);
1057 			priv->frag_info[i].frag_stride = frag_stride + pad;
1058 
1059 			buf_size += frag_size;
1060 			i++;
1061 		}
1062 		priv->dma_dir = DMA_FROM_DEVICE;
1063 		priv->rx_headroom = 0;
1064 	}
1065 
1066 	priv->num_frags = i;
1067 	priv->rx_skb_size = eff_mtu;
1068 	priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
1069 
1070 	en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
1071 	       eff_mtu, priv->num_frags);
1072 	for (i = 0; i < priv->num_frags; i++) {
1073 		en_dbg(DRV,
1074 		       priv,
1075 		       "  frag:%d - size:%d stride:%d\n",
1076 		       i,
1077 		       priv->frag_info[i].frag_size,
1078 		       priv->frag_info[i].frag_stride);
1079 	}
1080 }
1081 
1082 /* RSS related functions */
1083 
mlx4_en_config_rss_qp(struct mlx4_en_priv * priv,int qpn,struct mlx4_en_rx_ring * ring,enum mlx4_qp_state * state,struct mlx4_qp * qp)1084 static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
1085 				 struct mlx4_en_rx_ring *ring,
1086 				 enum mlx4_qp_state *state,
1087 				 struct mlx4_qp *qp)
1088 {
1089 	struct mlx4_en_dev *mdev = priv->mdev;
1090 	struct mlx4_qp_context *context;
1091 	int err = 0;
1092 
1093 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1094 	if (!context)
1095 		return -ENOMEM;
1096 
1097 	err = mlx4_qp_alloc(mdev->dev, qpn, qp);
1098 	if (err) {
1099 		en_err(priv, "Failed to allocate qp #%x\n", qpn);
1100 		goto out;
1101 	}
1102 	qp->event = mlx4_en_sqp_event;
1103 
1104 	mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
1105 				qpn, ring->cqn, -1, context);
1106 	context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
1107 
1108 	/* Cancel FCS removal if FW allows */
1109 	if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
1110 		context->param3 |= cpu_to_be32(1 << 29);
1111 		if (priv->dev->features & NETIF_F_RXFCS)
1112 			ring->fcs_del = 0;
1113 		else
1114 			ring->fcs_del = ETH_FCS_LEN;
1115 	} else
1116 		ring->fcs_del = 0;
1117 
1118 	err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
1119 	if (err) {
1120 		mlx4_qp_remove(mdev->dev, qp);
1121 		mlx4_qp_free(mdev->dev, qp);
1122 	}
1123 	mlx4_en_update_rx_prod_db(ring);
1124 out:
1125 	kfree(context);
1126 	return err;
1127 }
1128 
mlx4_en_create_drop_qp(struct mlx4_en_priv * priv)1129 int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
1130 {
1131 	int err;
1132 	u32 qpn;
1133 
1134 	err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
1135 				    MLX4_RESERVE_A0_QP,
1136 				    MLX4_RES_USAGE_DRIVER);
1137 	if (err) {
1138 		en_err(priv, "Failed reserving drop qpn\n");
1139 		return err;
1140 	}
1141 	err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp);
1142 	if (err) {
1143 		en_err(priv, "Failed allocating drop qp\n");
1144 		mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1145 		return err;
1146 	}
1147 
1148 	return 0;
1149 }
1150 
mlx4_en_destroy_drop_qp(struct mlx4_en_priv * priv)1151 void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
1152 {
1153 	u32 qpn;
1154 
1155 	qpn = priv->drop_qp.qpn;
1156 	mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
1157 	mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
1158 	mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1159 }
1160 
1161 /* Allocate rx qp's and configure them according to rss map */
mlx4_en_config_rss_steer(struct mlx4_en_priv * priv)1162 int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
1163 {
1164 	struct mlx4_en_dev *mdev = priv->mdev;
1165 	struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1166 	struct mlx4_qp_context context;
1167 	struct mlx4_rss_context *rss_context;
1168 	int rss_rings;
1169 	void *ptr;
1170 	u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
1171 			MLX4_RSS_TCP_IPV6);
1172 	int i, qpn;
1173 	int err = 0;
1174 	int good_qps = 0;
1175 	u8 flags;
1176 
1177 	en_dbg(DRV, priv, "Configuring rss steering\n");
1178 
1179 	flags = priv->rx_ring_num == 1 ? MLX4_RESERVE_A0_QP : 0;
1180 	err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
1181 				    priv->rx_ring_num,
1182 				    &rss_map->base_qpn, flags,
1183 				    MLX4_RES_USAGE_DRIVER);
1184 	if (err) {
1185 		en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
1186 		return err;
1187 	}
1188 
1189 	for (i = 0; i < priv->rx_ring_num; i++) {
1190 		qpn = rss_map->base_qpn + i;
1191 		err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
1192 					    &rss_map->state[i],
1193 					    &rss_map->qps[i]);
1194 		if (err)
1195 			goto rss_err;
1196 
1197 		++good_qps;
1198 	}
1199 
1200 	if (priv->rx_ring_num == 1) {
1201 		rss_map->indir_qp = &rss_map->qps[0];
1202 		priv->base_qpn = rss_map->indir_qp->qpn;
1203 		en_info(priv, "Optimized Non-RSS steering\n");
1204 		return 0;
1205 	}
1206 
1207 	rss_map->indir_qp = kzalloc(sizeof(*rss_map->indir_qp), GFP_KERNEL);
1208 	if (!rss_map->indir_qp) {
1209 		err = -ENOMEM;
1210 		goto rss_err;
1211 	}
1212 
1213 	/* Configure RSS indirection qp */
1214 	err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, rss_map->indir_qp);
1215 	if (err) {
1216 		en_err(priv, "Failed to allocate RSS indirection QP\n");
1217 		goto qp_alloc_err;
1218 	}
1219 
1220 	rss_map->indir_qp->event = mlx4_en_sqp_event;
1221 	mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
1222 				priv->rx_ring[0]->cqn, -1, &context);
1223 
1224 	if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
1225 		rss_rings = priv->rx_ring_num;
1226 	else
1227 		rss_rings = priv->prof->rss_rings;
1228 
1229 	ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
1230 					+ MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
1231 	rss_context = ptr;
1232 	rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
1233 					    (rss_map->base_qpn));
1234 	rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
1235 	if (priv->mdev->profile.udp_rss) {
1236 		rss_mask |=  MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
1237 		rss_context->base_qpn_udp = rss_context->default_qpn;
1238 	}
1239 
1240 	if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1241 		en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
1242 		rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
1243 	}
1244 
1245 	rss_context->flags = rss_mask;
1246 	rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1247 	if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
1248 		rss_context->hash_fn = MLX4_RSS_HASH_XOR;
1249 	} else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
1250 		rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1251 		memcpy(rss_context->rss_key, priv->rss_key,
1252 		       MLX4_EN_RSS_KEY_SIZE);
1253 	} else {
1254 		en_err(priv, "Unknown RSS hash function requested\n");
1255 		err = -EINVAL;
1256 		goto indir_err;
1257 	}
1258 
1259 	err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
1260 			       rss_map->indir_qp, &rss_map->indir_state);
1261 	if (err)
1262 		goto indir_err;
1263 
1264 	return 0;
1265 
1266 indir_err:
1267 	mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1268 		       MLX4_QP_STATE_RST, NULL, 0, 0, rss_map->indir_qp);
1269 	mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
1270 	mlx4_qp_free(mdev->dev, rss_map->indir_qp);
1271 qp_alloc_err:
1272 	kfree(rss_map->indir_qp);
1273 	rss_map->indir_qp = NULL;
1274 rss_err:
1275 	for (i = 0; i < good_qps; i++) {
1276 		mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1277 			       MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1278 		mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1279 		mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1280 	}
1281 	mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
1282 	return err;
1283 }
1284 
mlx4_en_release_rss_steer(struct mlx4_en_priv * priv)1285 void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
1286 {
1287 	struct mlx4_en_dev *mdev = priv->mdev;
1288 	struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1289 	int i;
1290 
1291 	if (priv->rx_ring_num > 1) {
1292 		mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1293 			       MLX4_QP_STATE_RST, NULL, 0, 0,
1294 			       rss_map->indir_qp);
1295 		mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
1296 		mlx4_qp_free(mdev->dev, rss_map->indir_qp);
1297 		kfree(rss_map->indir_qp);
1298 		rss_map->indir_qp = NULL;
1299 	}
1300 
1301 	for (i = 0; i < priv->rx_ring_num; i++) {
1302 		mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1303 			       MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1304 		mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1305 		mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1306 	}
1307 	mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
1308 }
1309