xref: /linux/drivers/net/ethernet/qlogic/qede/qede_fp.c (revision 93a3545d812ae7cfe4426374e00a7d8f64ac02e0)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* QLogic qede NIC Driver
3  * Copyright (c) 2015-2017  QLogic Corporation
4  * Copyright (c) 2019-2020 Marvell International Ltd.
5  */
6 
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/skbuff.h>
10 #include <linux/bpf_trace.h>
11 #include <net/udp_tunnel.h>
12 #include <linux/ip.h>
13 #include <net/ipv6.h>
14 #include <net/tcp.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_vlan.h>
17 #include <net/ip6_checksum.h>
18 #include "qede_ptp.h"
19 
20 #include <linux/qed/qed_if.h>
21 #include "qede.h"
22 /*********************************
23  * Content also used by slowpath *
24  *********************************/
25 
26 int qede_alloc_rx_buffer(struct qede_rx_queue *rxq, bool allow_lazy)
27 {
28 	struct sw_rx_data *sw_rx_data;
29 	struct eth_rx_bd *rx_bd;
30 	dma_addr_t mapping;
31 	struct page *data;
32 
33 	/* In case lazy-allocation is allowed, postpone allocation until the
34 	 * end of the NAPI run. We'd still need to make sure the Rx ring has
35 	 * sufficient buffers to guarantee an additional Rx interrupt.
36 	 */
37 	if (allow_lazy && likely(rxq->filled_buffers > 12)) {
38 		rxq->filled_buffers--;
39 		return 0;
40 	}
41 
42 	data = alloc_pages(GFP_ATOMIC, 0);
43 	if (unlikely(!data))
44 		return -ENOMEM;
45 
46 	/* Map the entire page as it would be used
47 	 * for multiple RX buffer segment size mapping.
48 	 */
49 	mapping = dma_map_page(rxq->dev, data, 0,
50 			       PAGE_SIZE, rxq->data_direction);
51 	if (unlikely(dma_mapping_error(rxq->dev, mapping))) {
52 		__free_page(data);
53 		return -ENOMEM;
54 	}
55 
56 	sw_rx_data = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
57 	sw_rx_data->page_offset = 0;
58 	sw_rx_data->data = data;
59 	sw_rx_data->mapping = mapping;
60 
61 	/* Advance PROD and get BD pointer */
62 	rx_bd = (struct eth_rx_bd *)qed_chain_produce(&rxq->rx_bd_ring);
63 	WARN_ON(!rx_bd);
64 	rx_bd->addr.hi = cpu_to_le32(upper_32_bits(mapping));
65 	rx_bd->addr.lo = cpu_to_le32(lower_32_bits(mapping) +
66 				     rxq->rx_headroom);
67 
68 	rxq->sw_rx_prod++;
69 	rxq->filled_buffers++;
70 
71 	return 0;
72 }
73 
74 /* Unmap the data and free skb */
75 int qede_free_tx_pkt(struct qede_dev *edev, struct qede_tx_queue *txq, int *len)
76 {
77 	u16 idx = txq->sw_tx_cons;
78 	struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
79 	struct eth_tx_1st_bd *first_bd;
80 	struct eth_tx_bd *tx_data_bd;
81 	int bds_consumed = 0;
82 	int nbds;
83 	bool data_split = txq->sw_tx_ring.skbs[idx].flags & QEDE_TSO_SPLIT_BD;
84 	int i, split_bd_len = 0;
85 
86 	if (unlikely(!skb)) {
87 		DP_ERR(edev,
88 		       "skb is null for txq idx=%d txq->sw_tx_cons=%d txq->sw_tx_prod=%d\n",
89 		       idx, txq->sw_tx_cons, txq->sw_tx_prod);
90 		return -1;
91 	}
92 
93 	*len = skb->len;
94 
95 	first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
96 
97 	bds_consumed++;
98 
99 	nbds = first_bd->data.nbds;
100 
101 	if (data_split) {
102 		struct eth_tx_bd *split = (struct eth_tx_bd *)
103 			qed_chain_consume(&txq->tx_pbl);
104 		split_bd_len = BD_UNMAP_LEN(split);
105 		bds_consumed++;
106 	}
107 	dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
108 			 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
109 
110 	/* Unmap the data of the skb frags */
111 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, bds_consumed++) {
112 		tx_data_bd = (struct eth_tx_bd *)
113 			qed_chain_consume(&txq->tx_pbl);
114 		dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
115 			       BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
116 	}
117 
118 	while (bds_consumed++ < nbds)
119 		qed_chain_consume(&txq->tx_pbl);
120 
121 	/* Free skb */
122 	dev_kfree_skb_any(skb);
123 	txq->sw_tx_ring.skbs[idx].skb = NULL;
124 	txq->sw_tx_ring.skbs[idx].flags = 0;
125 
126 	return 0;
127 }
128 
129 /* Unmap the data and free skb when mapping failed during start_xmit */
130 static void qede_free_failed_tx_pkt(struct qede_tx_queue *txq,
131 				    struct eth_tx_1st_bd *first_bd,
132 				    int nbd, bool data_split)
133 {
134 	u16 idx = txq->sw_tx_prod;
135 	struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
136 	struct eth_tx_bd *tx_data_bd;
137 	int i, split_bd_len = 0;
138 
139 	/* Return prod to its position before this skb was handled */
140 	qed_chain_set_prod(&txq->tx_pbl,
141 			   le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
142 
143 	first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
144 
145 	if (data_split) {
146 		struct eth_tx_bd *split = (struct eth_tx_bd *)
147 					  qed_chain_produce(&txq->tx_pbl);
148 		split_bd_len = BD_UNMAP_LEN(split);
149 		nbd--;
150 	}
151 
152 	dma_unmap_single(txq->dev, BD_UNMAP_ADDR(first_bd),
153 			 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
154 
155 	/* Unmap the data of the skb frags */
156 	for (i = 0; i < nbd; i++) {
157 		tx_data_bd = (struct eth_tx_bd *)
158 			qed_chain_produce(&txq->tx_pbl);
159 		if (tx_data_bd->nbytes)
160 			dma_unmap_page(txq->dev,
161 				       BD_UNMAP_ADDR(tx_data_bd),
162 				       BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
163 	}
164 
165 	/* Return again prod to its position before this skb was handled */
166 	qed_chain_set_prod(&txq->tx_pbl,
167 			   le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
168 
169 	/* Free skb */
170 	dev_kfree_skb_any(skb);
171 	txq->sw_tx_ring.skbs[idx].skb = NULL;
172 	txq->sw_tx_ring.skbs[idx].flags = 0;
173 }
174 
175 static u32 qede_xmit_type(struct sk_buff *skb, int *ipv6_ext)
176 {
177 	u32 rc = XMIT_L4_CSUM;
178 	__be16 l3_proto;
179 
180 	if (skb->ip_summed != CHECKSUM_PARTIAL)
181 		return XMIT_PLAIN;
182 
183 	l3_proto = vlan_get_protocol(skb);
184 	if (l3_proto == htons(ETH_P_IPV6) &&
185 	    (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
186 		*ipv6_ext = 1;
187 
188 	if (skb->encapsulation) {
189 		rc |= XMIT_ENC;
190 		if (skb_is_gso(skb)) {
191 			unsigned short gso_type = skb_shinfo(skb)->gso_type;
192 
193 			if ((gso_type & SKB_GSO_UDP_TUNNEL_CSUM) ||
194 			    (gso_type & SKB_GSO_GRE_CSUM))
195 				rc |= XMIT_ENC_GSO_L4_CSUM;
196 
197 			rc |= XMIT_LSO;
198 			return rc;
199 		}
200 	}
201 
202 	if (skb_is_gso(skb))
203 		rc |= XMIT_LSO;
204 
205 	return rc;
206 }
207 
208 static void qede_set_params_for_ipv6_ext(struct sk_buff *skb,
209 					 struct eth_tx_2nd_bd *second_bd,
210 					 struct eth_tx_3rd_bd *third_bd)
211 {
212 	u8 l4_proto;
213 	u16 bd2_bits1 = 0, bd2_bits2 = 0;
214 
215 	bd2_bits1 |= (1 << ETH_TX_DATA_2ND_BD_IPV6_EXT_SHIFT);
216 
217 	bd2_bits2 |= ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) &
218 		     ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_MASK)
219 		    << ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_SHIFT;
220 
221 	bd2_bits1 |= (ETH_L4_PSEUDO_CSUM_CORRECT_LENGTH <<
222 		      ETH_TX_DATA_2ND_BD_L4_PSEUDO_CSUM_MODE_SHIFT);
223 
224 	if (vlan_get_protocol(skb) == htons(ETH_P_IPV6))
225 		l4_proto = ipv6_hdr(skb)->nexthdr;
226 	else
227 		l4_proto = ip_hdr(skb)->protocol;
228 
229 	if (l4_proto == IPPROTO_UDP)
230 		bd2_bits1 |= 1 << ETH_TX_DATA_2ND_BD_L4_UDP_SHIFT;
231 
232 	if (third_bd)
233 		third_bd->data.bitfields |=
234 			cpu_to_le16(((tcp_hdrlen(skb) / 4) &
235 				ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_MASK) <<
236 				ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_SHIFT);
237 
238 	second_bd->data.bitfields1 = cpu_to_le16(bd2_bits1);
239 	second_bd->data.bitfields2 = cpu_to_le16(bd2_bits2);
240 }
241 
242 static int map_frag_to_bd(struct qede_tx_queue *txq,
243 			  skb_frag_t *frag, struct eth_tx_bd *bd)
244 {
245 	dma_addr_t mapping;
246 
247 	/* Map skb non-linear frag data for DMA */
248 	mapping = skb_frag_dma_map(txq->dev, frag, 0,
249 				   skb_frag_size(frag), DMA_TO_DEVICE);
250 	if (unlikely(dma_mapping_error(txq->dev, mapping)))
251 		return -ENOMEM;
252 
253 	/* Setup the data pointer of the frag data */
254 	BD_SET_UNMAP_ADDR_LEN(bd, mapping, skb_frag_size(frag));
255 
256 	return 0;
257 }
258 
259 static u16 qede_get_skb_hlen(struct sk_buff *skb, bool is_encap_pkt)
260 {
261 	if (is_encap_pkt)
262 		return (skb_inner_transport_header(skb) +
263 			inner_tcp_hdrlen(skb) - skb->data);
264 	else
265 		return (skb_transport_header(skb) +
266 			tcp_hdrlen(skb) - skb->data);
267 }
268 
269 /* +2 for 1st BD for headers and 2nd BD for headlen (if required) */
270 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
271 static bool qede_pkt_req_lin(struct sk_buff *skb, u8 xmit_type)
272 {
273 	int allowed_frags = ETH_TX_MAX_BDS_PER_NON_LSO_PACKET - 1;
274 
275 	if (xmit_type & XMIT_LSO) {
276 		int hlen;
277 
278 		hlen = qede_get_skb_hlen(skb, xmit_type & XMIT_ENC);
279 
280 		/* linear payload would require its own BD */
281 		if (skb_headlen(skb) > hlen)
282 			allowed_frags--;
283 	}
284 
285 	return (skb_shinfo(skb)->nr_frags > allowed_frags);
286 }
287 #endif
288 
289 static inline void qede_update_tx_producer(struct qede_tx_queue *txq)
290 {
291 	/* wmb makes sure that the BDs data is updated before updating the
292 	 * producer, otherwise FW may read old data from the BDs.
293 	 */
294 	wmb();
295 	barrier();
296 	writel(txq->tx_db.raw, txq->doorbell_addr);
297 
298 	/* Fence required to flush the write combined buffer, since another
299 	 * CPU may write to the same doorbell address and data may be lost
300 	 * due to relaxed order nature of write combined bar.
301 	 */
302 	wmb();
303 }
304 
305 static int qede_xdp_xmit(struct qede_dev *edev, struct qede_fastpath *fp,
306 			 struct sw_rx_data *metadata, u16 padding, u16 length)
307 {
308 	struct qede_tx_queue *txq = fp->xdp_tx;
309 	struct eth_tx_1st_bd *first_bd;
310 	u16 idx = txq->sw_tx_prod;
311 	u16 val;
312 
313 	if (!qed_chain_get_elem_left(&txq->tx_pbl)) {
314 		txq->stopped_cnt++;
315 		return -ENOMEM;
316 	}
317 
318 	first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
319 
320 	memset(first_bd, 0, sizeof(*first_bd));
321 	first_bd->data.bd_flags.bitfields =
322 	    BIT(ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT);
323 
324 	val = (length & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
325 	       ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
326 
327 	first_bd->data.bitfields |= cpu_to_le16(val);
328 	first_bd->data.nbds = 1;
329 
330 	/* We can safely ignore the offset, as it's 0 for XDP */
331 	BD_SET_UNMAP_ADDR_LEN(first_bd, metadata->mapping + padding, length);
332 
333 	/* Synchronize the buffer back to device, as program [probably]
334 	 * has changed it.
335 	 */
336 	dma_sync_single_for_device(&edev->pdev->dev,
337 				   metadata->mapping + padding,
338 				   length, PCI_DMA_TODEVICE);
339 
340 	txq->sw_tx_ring.xdp[idx].page = metadata->data;
341 	txq->sw_tx_ring.xdp[idx].mapping = metadata->mapping;
342 	txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
343 
344 	/* Mark the fastpath for future XDP doorbell */
345 	fp->xdp_xmit = 1;
346 
347 	return 0;
348 }
349 
350 int qede_txq_has_work(struct qede_tx_queue *txq)
351 {
352 	u16 hw_bd_cons;
353 
354 	/* Tell compiler that consumer and producer can change */
355 	barrier();
356 	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
357 	if (qed_chain_get_cons_idx(&txq->tx_pbl) == hw_bd_cons + 1)
358 		return 0;
359 
360 	return hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl);
361 }
362 
363 static void qede_xdp_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
364 {
365 	u16 hw_bd_cons, idx;
366 
367 	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
368 	barrier();
369 
370 	while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
371 		qed_chain_consume(&txq->tx_pbl);
372 		idx = txq->sw_tx_cons;
373 
374 		dma_unmap_page(&edev->pdev->dev,
375 			       txq->sw_tx_ring.xdp[idx].mapping,
376 			       PAGE_SIZE, DMA_BIDIRECTIONAL);
377 		__free_page(txq->sw_tx_ring.xdp[idx].page);
378 
379 		txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
380 		txq->xmit_pkts++;
381 	}
382 }
383 
384 static int qede_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
385 {
386 	unsigned int pkts_compl = 0, bytes_compl = 0;
387 	struct netdev_queue *netdev_txq;
388 	u16 hw_bd_cons;
389 	int rc;
390 
391 	netdev_txq = netdev_get_tx_queue(edev->ndev, txq->ndev_txq_id);
392 
393 	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
394 	barrier();
395 
396 	while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
397 		int len = 0;
398 
399 		rc = qede_free_tx_pkt(edev, txq, &len);
400 		if (rc) {
401 			DP_NOTICE(edev, "hw_bd_cons = %d, chain_cons=%d\n",
402 				  hw_bd_cons,
403 				  qed_chain_get_cons_idx(&txq->tx_pbl));
404 			break;
405 		}
406 
407 		bytes_compl += len;
408 		pkts_compl++;
409 		txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
410 		txq->xmit_pkts++;
411 	}
412 
413 	netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);
414 
415 	/* Need to make the tx_bd_cons update visible to start_xmit()
416 	 * before checking for netif_tx_queue_stopped().  Without the
417 	 * memory barrier, there is a small possibility that
418 	 * start_xmit() will miss it and cause the queue to be stopped
419 	 * forever.
420 	 * On the other hand we need an rmb() here to ensure the proper
421 	 * ordering of bit testing in the following
422 	 * netif_tx_queue_stopped(txq) call.
423 	 */
424 	smp_mb();
425 
426 	if (unlikely(netif_tx_queue_stopped(netdev_txq))) {
427 		/* Taking tx_lock is needed to prevent reenabling the queue
428 		 * while it's empty. This could have happen if rx_action() gets
429 		 * suspended in qede_tx_int() after the condition before
430 		 * netif_tx_wake_queue(), while tx_action (qede_start_xmit()):
431 		 *
432 		 * stops the queue->sees fresh tx_bd_cons->releases the queue->
433 		 * sends some packets consuming the whole queue again->
434 		 * stops the queue
435 		 */
436 
437 		__netif_tx_lock(netdev_txq, smp_processor_id());
438 
439 		if ((netif_tx_queue_stopped(netdev_txq)) &&
440 		    (edev->state == QEDE_STATE_OPEN) &&
441 		    (qed_chain_get_elem_left(&txq->tx_pbl)
442 		      >= (MAX_SKB_FRAGS + 1))) {
443 			netif_tx_wake_queue(netdev_txq);
444 			DP_VERBOSE(edev, NETIF_MSG_TX_DONE,
445 				   "Wake queue was called\n");
446 		}
447 
448 		__netif_tx_unlock(netdev_txq);
449 	}
450 
451 	return 0;
452 }
453 
454 bool qede_has_rx_work(struct qede_rx_queue *rxq)
455 {
456 	u16 hw_comp_cons, sw_comp_cons;
457 
458 	/* Tell compiler that status block fields can change */
459 	barrier();
460 
461 	hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
462 	sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
463 
464 	return hw_comp_cons != sw_comp_cons;
465 }
466 
467 static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
468 {
469 	qed_chain_consume(&rxq->rx_bd_ring);
470 	rxq->sw_rx_cons++;
471 }
472 
473 /* This function reuses the buffer(from an offset) from
474  * consumer index to producer index in the bd ring
475  */
476 static inline void qede_reuse_page(struct qede_rx_queue *rxq,
477 				   struct sw_rx_data *curr_cons)
478 {
479 	struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
480 	struct sw_rx_data *curr_prod;
481 	dma_addr_t new_mapping;
482 
483 	curr_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
484 	*curr_prod = *curr_cons;
485 
486 	new_mapping = curr_prod->mapping + curr_prod->page_offset;
487 
488 	rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(new_mapping));
489 	rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(new_mapping) +
490 					  rxq->rx_headroom);
491 
492 	rxq->sw_rx_prod++;
493 	curr_cons->data = NULL;
494 }
495 
496 /* In case of allocation failures reuse buffers
497  * from consumer index to produce buffers for firmware
498  */
499 void qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq, u8 count)
500 {
501 	struct sw_rx_data *curr_cons;
502 
503 	for (; count > 0; count--) {
504 		curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
505 		qede_reuse_page(rxq, curr_cons);
506 		qede_rx_bd_ring_consume(rxq);
507 	}
508 }
509 
510 static inline int qede_realloc_rx_buffer(struct qede_rx_queue *rxq,
511 					 struct sw_rx_data *curr_cons)
512 {
513 	/* Move to the next segment in the page */
514 	curr_cons->page_offset += rxq->rx_buf_seg_size;
515 
516 	if (curr_cons->page_offset == PAGE_SIZE) {
517 		if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
518 			/* Since we failed to allocate new buffer
519 			 * current buffer can be used again.
520 			 */
521 			curr_cons->page_offset -= rxq->rx_buf_seg_size;
522 
523 			return -ENOMEM;
524 		}
525 
526 		dma_unmap_page(rxq->dev, curr_cons->mapping,
527 			       PAGE_SIZE, rxq->data_direction);
528 	} else {
529 		/* Increment refcount of the page as we don't want
530 		 * network stack to take the ownership of the page
531 		 * which can be recycled multiple times by the driver.
532 		 */
533 		page_ref_inc(curr_cons->data);
534 		qede_reuse_page(rxq, curr_cons);
535 	}
536 
537 	return 0;
538 }
539 
540 void qede_update_rx_prod(struct qede_dev *edev, struct qede_rx_queue *rxq)
541 {
542 	u16 bd_prod = qed_chain_get_prod_idx(&rxq->rx_bd_ring);
543 	u16 cqe_prod = qed_chain_get_prod_idx(&rxq->rx_comp_ring);
544 	struct eth_rx_prod_data rx_prods = {0};
545 
546 	/* Update producers */
547 	rx_prods.bd_prod = cpu_to_le16(bd_prod);
548 	rx_prods.cqe_prod = cpu_to_le16(cqe_prod);
549 
550 	/* Make sure that the BD and SGE data is updated before updating the
551 	 * producers since FW might read the BD/SGE right after the producer
552 	 * is updated.
553 	 */
554 	wmb();
555 
556 	internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
557 			(u32 *)&rx_prods);
558 }
559 
560 static void qede_get_rxhash(struct sk_buff *skb, u8 bitfields, __le32 rss_hash)
561 {
562 	enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
563 	enum rss_hash_type htype;
564 	u32 hash = 0;
565 
566 	htype = GET_FIELD(bitfields, ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
567 	if (htype) {
568 		hash_type = ((htype == RSS_HASH_TYPE_IPV4) ||
569 			     (htype == RSS_HASH_TYPE_IPV6)) ?
570 			    PKT_HASH_TYPE_L3 : PKT_HASH_TYPE_L4;
571 		hash = le32_to_cpu(rss_hash);
572 	}
573 	skb_set_hash(skb, hash, hash_type);
574 }
575 
576 static void qede_set_skb_csum(struct sk_buff *skb, u8 csum_flag)
577 {
578 	skb_checksum_none_assert(skb);
579 
580 	if (csum_flag & QEDE_CSUM_UNNECESSARY)
581 		skb->ip_summed = CHECKSUM_UNNECESSARY;
582 
583 	if (csum_flag & QEDE_TUNN_CSUM_UNNECESSARY) {
584 		skb->csum_level = 1;
585 		skb->encapsulation = 1;
586 	}
587 }
588 
589 static inline void qede_skb_receive(struct qede_dev *edev,
590 				    struct qede_fastpath *fp,
591 				    struct qede_rx_queue *rxq,
592 				    struct sk_buff *skb, u16 vlan_tag)
593 {
594 	if (vlan_tag)
595 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
596 
597 	napi_gro_receive(&fp->napi, skb);
598 }
599 
600 static void qede_set_gro_params(struct qede_dev *edev,
601 				struct sk_buff *skb,
602 				struct eth_fast_path_rx_tpa_start_cqe *cqe)
603 {
604 	u16 parsing_flags = le16_to_cpu(cqe->pars_flags.flags);
605 
606 	if (((parsing_flags >> PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) &
607 	    PARSING_AND_ERR_FLAGS_L3TYPE_MASK) == 2)
608 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
609 	else
610 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
611 
612 	skb_shinfo(skb)->gso_size = __le16_to_cpu(cqe->len_on_first_bd) -
613 				    cqe->header_len;
614 }
615 
616 static int qede_fill_frag_skb(struct qede_dev *edev,
617 			      struct qede_rx_queue *rxq,
618 			      u8 tpa_agg_index, u16 len_on_bd)
619 {
620 	struct sw_rx_data *current_bd = &rxq->sw_rx_ring[rxq->sw_rx_cons &
621 							 NUM_RX_BDS_MAX];
622 	struct qede_agg_info *tpa_info = &rxq->tpa_info[tpa_agg_index];
623 	struct sk_buff *skb = tpa_info->skb;
624 
625 	if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
626 		goto out;
627 
628 	/* Add one frag and update the appropriate fields in the skb */
629 	skb_fill_page_desc(skb, tpa_info->frag_id++,
630 			   current_bd->data,
631 			   current_bd->page_offset + rxq->rx_headroom,
632 			   len_on_bd);
633 
634 	if (unlikely(qede_realloc_rx_buffer(rxq, current_bd))) {
635 		/* Incr page ref count to reuse on allocation failure
636 		 * so that it doesn't get freed while freeing SKB.
637 		 */
638 		page_ref_inc(current_bd->data);
639 		goto out;
640 	}
641 
642 	qede_rx_bd_ring_consume(rxq);
643 
644 	skb->data_len += len_on_bd;
645 	skb->truesize += rxq->rx_buf_seg_size;
646 	skb->len += len_on_bd;
647 
648 	return 0;
649 
650 out:
651 	tpa_info->state = QEDE_AGG_STATE_ERROR;
652 	qede_recycle_rx_bd_ring(rxq, 1);
653 
654 	return -ENOMEM;
655 }
656 
657 static bool qede_tunn_exist(u16 flag)
658 {
659 	return !!(flag & (PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
660 			  PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT));
661 }
662 
663 static u8 qede_check_tunn_csum(u16 flag)
664 {
665 	u16 csum_flag = 0;
666 	u8 tcsum = 0;
667 
668 	if (flag & (PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
669 		    PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT))
670 		csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
671 			     PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;
672 
673 	if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
674 		    PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
675 		csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
676 			     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
677 		tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
678 	}
679 
680 	csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
681 		     PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
682 		     PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
683 		     PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
684 
685 	if (csum_flag & flag)
686 		return QEDE_CSUM_ERROR;
687 
688 	return QEDE_CSUM_UNNECESSARY | tcsum;
689 }
690 
691 static inline struct sk_buff *
692 qede_build_skb(struct qede_rx_queue *rxq,
693 	       struct sw_rx_data *bd, u16 len, u16 pad)
694 {
695 	struct sk_buff *skb;
696 	void *buf;
697 
698 	buf = page_address(bd->data) + bd->page_offset;
699 	skb = build_skb(buf, rxq->rx_buf_seg_size);
700 
701 	skb_reserve(skb, pad);
702 	skb_put(skb, len);
703 
704 	return skb;
705 }
706 
707 static struct sk_buff *
708 qede_tpa_rx_build_skb(struct qede_dev *edev,
709 		      struct qede_rx_queue *rxq,
710 		      struct sw_rx_data *bd, u16 len, u16 pad,
711 		      bool alloc_skb)
712 {
713 	struct sk_buff *skb;
714 
715 	skb = qede_build_skb(rxq, bd, len, pad);
716 	bd->page_offset += rxq->rx_buf_seg_size;
717 
718 	if (bd->page_offset == PAGE_SIZE) {
719 		if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
720 			DP_NOTICE(edev,
721 				  "Failed to allocate RX buffer for tpa start\n");
722 			bd->page_offset -= rxq->rx_buf_seg_size;
723 			page_ref_inc(bd->data);
724 			dev_kfree_skb_any(skb);
725 			return NULL;
726 		}
727 	} else {
728 		page_ref_inc(bd->data);
729 		qede_reuse_page(rxq, bd);
730 	}
731 
732 	/* We've consumed the first BD and prepared an SKB */
733 	qede_rx_bd_ring_consume(rxq);
734 
735 	return skb;
736 }
737 
738 static struct sk_buff *
739 qede_rx_build_skb(struct qede_dev *edev,
740 		  struct qede_rx_queue *rxq,
741 		  struct sw_rx_data *bd, u16 len, u16 pad)
742 {
743 	struct sk_buff *skb = NULL;
744 
745 	/* For smaller frames still need to allocate skb, memcpy
746 	 * data and benefit in reusing the page segment instead of
747 	 * un-mapping it.
748 	 */
749 	if ((len + pad <= edev->rx_copybreak)) {
750 		unsigned int offset = bd->page_offset + pad;
751 
752 		skb = netdev_alloc_skb(edev->ndev, QEDE_RX_HDR_SIZE);
753 		if (unlikely(!skb))
754 			return NULL;
755 
756 		skb_reserve(skb, pad);
757 		skb_put_data(skb, page_address(bd->data) + offset, len);
758 		qede_reuse_page(rxq, bd);
759 		goto out;
760 	}
761 
762 	skb = qede_build_skb(rxq, bd, len, pad);
763 
764 	if (unlikely(qede_realloc_rx_buffer(rxq, bd))) {
765 		/* Incr page ref count to reuse on allocation failure so
766 		 * that it doesn't get freed while freeing SKB [as its
767 		 * already mapped there].
768 		 */
769 		page_ref_inc(bd->data);
770 		dev_kfree_skb_any(skb);
771 		return NULL;
772 	}
773 out:
774 	/* We've consumed the first BD and prepared an SKB */
775 	qede_rx_bd_ring_consume(rxq);
776 
777 	return skb;
778 }
779 
780 static void qede_tpa_start(struct qede_dev *edev,
781 			   struct qede_rx_queue *rxq,
782 			   struct eth_fast_path_rx_tpa_start_cqe *cqe)
783 {
784 	struct qede_agg_info *tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
785 	struct sw_rx_data *sw_rx_data_cons;
786 	u16 pad;
787 
788 	sw_rx_data_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
789 	pad = cqe->placement_offset + rxq->rx_headroom;
790 
791 	tpa_info->skb = qede_tpa_rx_build_skb(edev, rxq, sw_rx_data_cons,
792 					      le16_to_cpu(cqe->len_on_first_bd),
793 					      pad, false);
794 	tpa_info->buffer.page_offset = sw_rx_data_cons->page_offset;
795 	tpa_info->buffer.mapping = sw_rx_data_cons->mapping;
796 
797 	if (unlikely(!tpa_info->skb)) {
798 		DP_NOTICE(edev, "Failed to allocate SKB for gro\n");
799 
800 		/* Consume from ring but do not produce since
801 		 * this might be used by FW still, it will be re-used
802 		 * at TPA end.
803 		 */
804 		tpa_info->tpa_start_fail = true;
805 		qede_rx_bd_ring_consume(rxq);
806 		tpa_info->state = QEDE_AGG_STATE_ERROR;
807 		goto cons_buf;
808 	}
809 
810 	tpa_info->frag_id = 0;
811 	tpa_info->state = QEDE_AGG_STATE_START;
812 
813 	if ((le16_to_cpu(cqe->pars_flags.flags) >>
814 	     PARSING_AND_ERR_FLAGS_TAG8021QEXIST_SHIFT) &
815 	    PARSING_AND_ERR_FLAGS_TAG8021QEXIST_MASK)
816 		tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
817 	else
818 		tpa_info->vlan_tag = 0;
819 
820 	qede_get_rxhash(tpa_info->skb, cqe->bitfields, cqe->rss_hash);
821 
822 	/* This is needed in order to enable forwarding support */
823 	qede_set_gro_params(edev, tpa_info->skb, cqe);
824 
825 cons_buf: /* We still need to handle bd_len_list to consume buffers */
826 	if (likely(cqe->bw_ext_bd_len_list[0]))
827 		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
828 				   le16_to_cpu(cqe->bw_ext_bd_len_list[0]));
829 
830 	if (unlikely(cqe->bw_ext_bd_len_list[1])) {
831 		DP_ERR(edev,
832 		       "Unlikely - got a TPA aggregation with more than one bw_ext_bd_len_list entry in the TPA start\n");
833 		tpa_info->state = QEDE_AGG_STATE_ERROR;
834 	}
835 }
836 
837 #ifdef CONFIG_INET
838 static void qede_gro_ip_csum(struct sk_buff *skb)
839 {
840 	const struct iphdr *iph = ip_hdr(skb);
841 	struct tcphdr *th;
842 
843 	skb_set_transport_header(skb, sizeof(struct iphdr));
844 	th = tcp_hdr(skb);
845 
846 	th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
847 				  iph->saddr, iph->daddr, 0);
848 
849 	tcp_gro_complete(skb);
850 }
851 
852 static void qede_gro_ipv6_csum(struct sk_buff *skb)
853 {
854 	struct ipv6hdr *iph = ipv6_hdr(skb);
855 	struct tcphdr *th;
856 
857 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
858 	th = tcp_hdr(skb);
859 
860 	th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
861 				  &iph->saddr, &iph->daddr, 0);
862 	tcp_gro_complete(skb);
863 }
864 #endif
865 
866 static void qede_gro_receive(struct qede_dev *edev,
867 			     struct qede_fastpath *fp,
868 			     struct sk_buff *skb,
869 			     u16 vlan_tag)
870 {
871 	/* FW can send a single MTU sized packet from gro flow
872 	 * due to aggregation timeout/last segment etc. which
873 	 * is not expected to be a gro packet. If a skb has zero
874 	 * frags then simply push it in the stack as non gso skb.
875 	 */
876 	if (unlikely(!skb->data_len)) {
877 		skb_shinfo(skb)->gso_type = 0;
878 		skb_shinfo(skb)->gso_size = 0;
879 		goto send_skb;
880 	}
881 
882 #ifdef CONFIG_INET
883 	if (skb_shinfo(skb)->gso_size) {
884 		skb_reset_network_header(skb);
885 
886 		switch (skb->protocol) {
887 		case htons(ETH_P_IP):
888 			qede_gro_ip_csum(skb);
889 			break;
890 		case htons(ETH_P_IPV6):
891 			qede_gro_ipv6_csum(skb);
892 			break;
893 		default:
894 			DP_ERR(edev,
895 			       "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
896 			       ntohs(skb->protocol));
897 		}
898 	}
899 #endif
900 
901 send_skb:
902 	skb_record_rx_queue(skb, fp->rxq->rxq_id);
903 	qede_skb_receive(edev, fp, fp->rxq, skb, vlan_tag);
904 }
905 
906 static inline void qede_tpa_cont(struct qede_dev *edev,
907 				 struct qede_rx_queue *rxq,
908 				 struct eth_fast_path_rx_tpa_cont_cqe *cqe)
909 {
910 	int i;
911 
912 	for (i = 0; cqe->len_list[i]; i++)
913 		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
914 				   le16_to_cpu(cqe->len_list[i]));
915 
916 	if (unlikely(i > 1))
917 		DP_ERR(edev,
918 		       "Strange - TPA cont with more than a single len_list entry\n");
919 }
920 
921 static int qede_tpa_end(struct qede_dev *edev,
922 			struct qede_fastpath *fp,
923 			struct eth_fast_path_rx_tpa_end_cqe *cqe)
924 {
925 	struct qede_rx_queue *rxq = fp->rxq;
926 	struct qede_agg_info *tpa_info;
927 	struct sk_buff *skb;
928 	int i;
929 
930 	tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
931 	skb = tpa_info->skb;
932 
933 	if (tpa_info->buffer.page_offset == PAGE_SIZE)
934 		dma_unmap_page(rxq->dev, tpa_info->buffer.mapping,
935 			       PAGE_SIZE, rxq->data_direction);
936 
937 	for (i = 0; cqe->len_list[i]; i++)
938 		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
939 				   le16_to_cpu(cqe->len_list[i]));
940 	if (unlikely(i > 1))
941 		DP_ERR(edev,
942 		       "Strange - TPA emd with more than a single len_list entry\n");
943 
944 	if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
945 		goto err;
946 
947 	/* Sanity */
948 	if (unlikely(cqe->num_of_bds != tpa_info->frag_id + 1))
949 		DP_ERR(edev,
950 		       "Strange - TPA had %02x BDs, but SKB has only %d frags\n",
951 		       cqe->num_of_bds, tpa_info->frag_id);
952 	if (unlikely(skb->len != le16_to_cpu(cqe->total_packet_len)))
953 		DP_ERR(edev,
954 		       "Strange - total packet len [cqe] is %4x but SKB has len %04x\n",
955 		       le16_to_cpu(cqe->total_packet_len), skb->len);
956 
957 	/* Finalize the SKB */
958 	skb->protocol = eth_type_trans(skb, edev->ndev);
959 	skb->ip_summed = CHECKSUM_UNNECESSARY;
960 
961 	/* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
962 	 * to skb_shinfo(skb)->gso_segs
963 	 */
964 	NAPI_GRO_CB(skb)->count = le16_to_cpu(cqe->num_of_coalesced_segs);
965 
966 	qede_gro_receive(edev, fp, skb, tpa_info->vlan_tag);
967 
968 	tpa_info->state = QEDE_AGG_STATE_NONE;
969 
970 	return 1;
971 err:
972 	tpa_info->state = QEDE_AGG_STATE_NONE;
973 
974 	if (tpa_info->tpa_start_fail) {
975 		qede_reuse_page(rxq, &tpa_info->buffer);
976 		tpa_info->tpa_start_fail = false;
977 	}
978 
979 	dev_kfree_skb_any(tpa_info->skb);
980 	tpa_info->skb = NULL;
981 	return 0;
982 }
983 
984 static u8 qede_check_notunn_csum(u16 flag)
985 {
986 	u16 csum_flag = 0;
987 	u8 csum = 0;
988 
989 	if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
990 		    PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
991 		csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
992 			     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
993 		csum = QEDE_CSUM_UNNECESSARY;
994 	}
995 
996 	csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
997 		     PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
998 
999 	if (csum_flag & flag)
1000 		return QEDE_CSUM_ERROR;
1001 
1002 	return csum;
1003 }
1004 
1005 static u8 qede_check_csum(u16 flag)
1006 {
1007 	if (!qede_tunn_exist(flag))
1008 		return qede_check_notunn_csum(flag);
1009 	else
1010 		return qede_check_tunn_csum(flag);
1011 }
1012 
1013 static bool qede_pkt_is_ip_fragmented(struct eth_fast_path_rx_reg_cqe *cqe,
1014 				      u16 flag)
1015 {
1016 	u8 tun_pars_flg = cqe->tunnel_pars_flags.flags;
1017 
1018 	if ((tun_pars_flg & (ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_MASK <<
1019 			     ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_SHIFT)) ||
1020 	    (flag & (PARSING_AND_ERR_FLAGS_IPV4FRAG_MASK <<
1021 		     PARSING_AND_ERR_FLAGS_IPV4FRAG_SHIFT)))
1022 		return true;
1023 
1024 	return false;
1025 }
1026 
1027 /* Return true iff packet is to be passed to stack */
1028 static bool qede_rx_xdp(struct qede_dev *edev,
1029 			struct qede_fastpath *fp,
1030 			struct qede_rx_queue *rxq,
1031 			struct bpf_prog *prog,
1032 			struct sw_rx_data *bd,
1033 			struct eth_fast_path_rx_reg_cqe *cqe,
1034 			u16 *data_offset, u16 *len)
1035 {
1036 	struct xdp_buff xdp;
1037 	enum xdp_action act;
1038 
1039 	xdp.data_hard_start = page_address(bd->data);
1040 	xdp.data = xdp.data_hard_start + *data_offset;
1041 	xdp_set_data_meta_invalid(&xdp);
1042 	xdp.data_end = xdp.data + *len;
1043 	xdp.rxq = &rxq->xdp_rxq;
1044 	xdp.frame_sz = rxq->rx_buf_seg_size; /* PAGE_SIZE when XDP enabled */
1045 
1046 	/* Queues always have a full reset currently, so for the time
1047 	 * being until there's atomic program replace just mark read
1048 	 * side for map helpers.
1049 	 */
1050 	rcu_read_lock();
1051 	act = bpf_prog_run_xdp(prog, &xdp);
1052 	rcu_read_unlock();
1053 
1054 	/* Recalculate, as XDP might have changed the headers */
1055 	*data_offset = xdp.data - xdp.data_hard_start;
1056 	*len = xdp.data_end - xdp.data;
1057 
1058 	if (act == XDP_PASS)
1059 		return true;
1060 
1061 	/* Count number of packets not to be passed to stack */
1062 	rxq->xdp_no_pass++;
1063 
1064 	switch (act) {
1065 	case XDP_TX:
1066 		/* We need the replacement buffer before transmit. */
1067 		if (qede_alloc_rx_buffer(rxq, true)) {
1068 			qede_recycle_rx_bd_ring(rxq, 1);
1069 			trace_xdp_exception(edev->ndev, prog, act);
1070 			return false;
1071 		}
1072 
1073 		/* Now if there's a transmission problem, we'd still have to
1074 		 * throw current buffer, as replacement was already allocated.
1075 		 */
1076 		if (qede_xdp_xmit(edev, fp, bd, *data_offset, *len)) {
1077 			dma_unmap_page(rxq->dev, bd->mapping,
1078 				       PAGE_SIZE, DMA_BIDIRECTIONAL);
1079 			__free_page(bd->data);
1080 			trace_xdp_exception(edev->ndev, prog, act);
1081 		}
1082 
1083 		/* Regardless, we've consumed an Rx BD */
1084 		qede_rx_bd_ring_consume(rxq);
1085 		return false;
1086 
1087 	default:
1088 		bpf_warn_invalid_xdp_action(act);
1089 		/* Fall through */
1090 	case XDP_ABORTED:
1091 		trace_xdp_exception(edev->ndev, prog, act);
1092 		/* Fall through */
1093 	case XDP_DROP:
1094 		qede_recycle_rx_bd_ring(rxq, cqe->bd_num);
1095 	}
1096 
1097 	return false;
1098 }
1099 
1100 static int qede_rx_build_jumbo(struct qede_dev *edev,
1101 			       struct qede_rx_queue *rxq,
1102 			       struct sk_buff *skb,
1103 			       struct eth_fast_path_rx_reg_cqe *cqe,
1104 			       u16 first_bd_len)
1105 {
1106 	u16 pkt_len = le16_to_cpu(cqe->pkt_len);
1107 	struct sw_rx_data *bd;
1108 	u16 bd_cons_idx;
1109 	u8 num_frags;
1110 
1111 	pkt_len -= first_bd_len;
1112 
1113 	/* We've already used one BD for the SKB. Now take care of the rest */
1114 	for (num_frags = cqe->bd_num - 1; num_frags > 0; num_frags--) {
1115 		u16 cur_size = pkt_len > rxq->rx_buf_size ? rxq->rx_buf_size :
1116 		    pkt_len;
1117 
1118 		if (unlikely(!cur_size)) {
1119 			DP_ERR(edev,
1120 			       "Still got %d BDs for mapping jumbo, but length became 0\n",
1121 			       num_frags);
1122 			goto out;
1123 		}
1124 
1125 		/* We need a replacement buffer for each BD */
1126 		if (unlikely(qede_alloc_rx_buffer(rxq, true)))
1127 			goto out;
1128 
1129 		/* Now that we've allocated the replacement buffer,
1130 		 * we can safely consume the next BD and map it to the SKB.
1131 		 */
1132 		bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1133 		bd = &rxq->sw_rx_ring[bd_cons_idx];
1134 		qede_rx_bd_ring_consume(rxq);
1135 
1136 		dma_unmap_page(rxq->dev, bd->mapping,
1137 			       PAGE_SIZE, DMA_FROM_DEVICE);
1138 
1139 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
1140 				   bd->data, rxq->rx_headroom, cur_size);
1141 
1142 		skb->truesize += PAGE_SIZE;
1143 		skb->data_len += cur_size;
1144 		skb->len += cur_size;
1145 		pkt_len -= cur_size;
1146 	}
1147 
1148 	if (unlikely(pkt_len))
1149 		DP_ERR(edev,
1150 		       "Mapped all BDs of jumbo, but still have %d bytes\n",
1151 		       pkt_len);
1152 
1153 out:
1154 	return num_frags;
1155 }
1156 
1157 static int qede_rx_process_tpa_cqe(struct qede_dev *edev,
1158 				   struct qede_fastpath *fp,
1159 				   struct qede_rx_queue *rxq,
1160 				   union eth_rx_cqe *cqe,
1161 				   enum eth_rx_cqe_type type)
1162 {
1163 	switch (type) {
1164 	case ETH_RX_CQE_TYPE_TPA_START:
1165 		qede_tpa_start(edev, rxq, &cqe->fast_path_tpa_start);
1166 		return 0;
1167 	case ETH_RX_CQE_TYPE_TPA_CONT:
1168 		qede_tpa_cont(edev, rxq, &cqe->fast_path_tpa_cont);
1169 		return 0;
1170 	case ETH_RX_CQE_TYPE_TPA_END:
1171 		return qede_tpa_end(edev, fp, &cqe->fast_path_tpa_end);
1172 	default:
1173 		return 0;
1174 	}
1175 }
1176 
1177 static int qede_rx_process_cqe(struct qede_dev *edev,
1178 			       struct qede_fastpath *fp,
1179 			       struct qede_rx_queue *rxq)
1180 {
1181 	struct bpf_prog *xdp_prog = READ_ONCE(rxq->xdp_prog);
1182 	struct eth_fast_path_rx_reg_cqe *fp_cqe;
1183 	u16 len, pad, bd_cons_idx, parse_flag;
1184 	enum eth_rx_cqe_type cqe_type;
1185 	union eth_rx_cqe *cqe;
1186 	struct sw_rx_data *bd;
1187 	struct sk_buff *skb;
1188 	__le16 flags;
1189 	u8 csum_flag;
1190 
1191 	/* Get the CQE from the completion ring */
1192 	cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1193 	cqe_type = cqe->fast_path_regular.type;
1194 
1195 	/* Process an unlikely slowpath event */
1196 	if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
1197 		struct eth_slow_path_rx_cqe *sp_cqe;
1198 
1199 		sp_cqe = (struct eth_slow_path_rx_cqe *)cqe;
1200 		edev->ops->eth_cqe_completion(edev->cdev, fp->id, sp_cqe);
1201 		return 0;
1202 	}
1203 
1204 	/* Handle TPA cqes */
1205 	if (cqe_type != ETH_RX_CQE_TYPE_REGULAR)
1206 		return qede_rx_process_tpa_cqe(edev, fp, rxq, cqe, cqe_type);
1207 
1208 	/* Get the data from the SW ring; Consume it only after it's evident
1209 	 * we wouldn't recycle it.
1210 	 */
1211 	bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1212 	bd = &rxq->sw_rx_ring[bd_cons_idx];
1213 
1214 	fp_cqe = &cqe->fast_path_regular;
1215 	len = le16_to_cpu(fp_cqe->len_on_first_bd);
1216 	pad = fp_cqe->placement_offset + rxq->rx_headroom;
1217 
1218 	/* Run eBPF program if one is attached */
1219 	if (xdp_prog)
1220 		if (!qede_rx_xdp(edev, fp, rxq, xdp_prog, bd, fp_cqe,
1221 				 &pad, &len))
1222 			return 0;
1223 
1224 	/* If this is an error packet then drop it */
1225 	flags = cqe->fast_path_regular.pars_flags.flags;
1226 	parse_flag = le16_to_cpu(flags);
1227 
1228 	csum_flag = qede_check_csum(parse_flag);
1229 	if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
1230 		if (qede_pkt_is_ip_fragmented(fp_cqe, parse_flag))
1231 			rxq->rx_ip_frags++;
1232 		else
1233 			rxq->rx_hw_errors++;
1234 	}
1235 
1236 	/* Basic validation passed; Need to prepare an SKB. This would also
1237 	 * guarantee to finally consume the first BD upon success.
1238 	 */
1239 	skb = qede_rx_build_skb(edev, rxq, bd, len, pad);
1240 	if (!skb) {
1241 		rxq->rx_alloc_errors++;
1242 		qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
1243 		return 0;
1244 	}
1245 
1246 	/* In case of Jumbo packet, several PAGE_SIZEd buffers will be pointed
1247 	 * by a single cqe.
1248 	 */
1249 	if (fp_cqe->bd_num > 1) {
1250 		u16 unmapped_frags = qede_rx_build_jumbo(edev, rxq, skb,
1251 							 fp_cqe, len);
1252 
1253 		if (unlikely(unmapped_frags > 0)) {
1254 			qede_recycle_rx_bd_ring(rxq, unmapped_frags);
1255 			dev_kfree_skb_any(skb);
1256 			return 0;
1257 		}
1258 	}
1259 
1260 	/* The SKB contains all the data. Now prepare meta-magic */
1261 	skb->protocol = eth_type_trans(skb, edev->ndev);
1262 	qede_get_rxhash(skb, fp_cqe->bitfields, fp_cqe->rss_hash);
1263 	qede_set_skb_csum(skb, csum_flag);
1264 	skb_record_rx_queue(skb, rxq->rxq_id);
1265 	qede_ptp_record_rx_ts(edev, cqe, skb);
1266 
1267 	/* SKB is prepared - pass it to stack */
1268 	qede_skb_receive(edev, fp, rxq, skb, le16_to_cpu(fp_cqe->vlan_tag));
1269 
1270 	return 1;
1271 }
1272 
1273 static int qede_rx_int(struct qede_fastpath *fp, int budget)
1274 {
1275 	struct qede_rx_queue *rxq = fp->rxq;
1276 	struct qede_dev *edev = fp->edev;
1277 	int work_done = 0, rcv_pkts = 0;
1278 	u16 hw_comp_cons, sw_comp_cons;
1279 
1280 	hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1281 	sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1282 
1283 	/* Memory barrier to prevent the CPU from doing speculative reads of CQE
1284 	 * / BD in the while-loop before reading hw_comp_cons. If the CQE is
1285 	 * read before it is written by FW, then FW writes CQE and SB, and then
1286 	 * the CPU reads the hw_comp_cons, it will use an old CQE.
1287 	 */
1288 	rmb();
1289 
1290 	/* Loop to complete all indicated BDs */
1291 	while ((sw_comp_cons != hw_comp_cons) && (work_done < budget)) {
1292 		rcv_pkts += qede_rx_process_cqe(edev, fp, rxq);
1293 		qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1294 		sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1295 		work_done++;
1296 	}
1297 
1298 	rxq->rcv_pkts += rcv_pkts;
1299 
1300 	/* Allocate replacement buffers */
1301 	while (rxq->num_rx_buffers - rxq->filled_buffers)
1302 		if (qede_alloc_rx_buffer(rxq, false))
1303 			break;
1304 
1305 	/* Update producers */
1306 	qede_update_rx_prod(edev, rxq);
1307 
1308 	return work_done;
1309 }
1310 
1311 static bool qede_poll_is_more_work(struct qede_fastpath *fp)
1312 {
1313 	qed_sb_update_sb_idx(fp->sb_info);
1314 
1315 	/* *_has_*_work() reads the status block, thus we need to ensure that
1316 	 * status block indices have been actually read (qed_sb_update_sb_idx)
1317 	 * prior to this check (*_has_*_work) so that we won't write the
1318 	 * "newer" value of the status block to HW (if there was a DMA right
1319 	 * after qede_has_rx_work and if there is no rmb, the memory reading
1320 	 * (qed_sb_update_sb_idx) may be postponed to right before *_ack_sb).
1321 	 * In this case there will never be another interrupt until there is
1322 	 * another update of the status block, while there is still unhandled
1323 	 * work.
1324 	 */
1325 	rmb();
1326 
1327 	if (likely(fp->type & QEDE_FASTPATH_RX))
1328 		if (qede_has_rx_work(fp->rxq))
1329 			return true;
1330 
1331 	if (fp->type & QEDE_FASTPATH_XDP)
1332 		if (qede_txq_has_work(fp->xdp_tx))
1333 			return true;
1334 
1335 	if (likely(fp->type & QEDE_FASTPATH_TX)) {
1336 		int cos;
1337 
1338 		for_each_cos_in_txq(fp->edev, cos) {
1339 			if (qede_txq_has_work(&fp->txq[cos]))
1340 				return true;
1341 		}
1342 	}
1343 
1344 	return false;
1345 }
1346 
1347 /*********************
1348  * NDO & API related *
1349  *********************/
1350 int qede_poll(struct napi_struct *napi, int budget)
1351 {
1352 	struct qede_fastpath *fp = container_of(napi, struct qede_fastpath,
1353 						napi);
1354 	struct qede_dev *edev = fp->edev;
1355 	int rx_work_done = 0;
1356 
1357 	if (likely(fp->type & QEDE_FASTPATH_TX)) {
1358 		int cos;
1359 
1360 		for_each_cos_in_txq(fp->edev, cos) {
1361 			if (qede_txq_has_work(&fp->txq[cos]))
1362 				qede_tx_int(edev, &fp->txq[cos]);
1363 		}
1364 	}
1365 
1366 	if ((fp->type & QEDE_FASTPATH_XDP) && qede_txq_has_work(fp->xdp_tx))
1367 		qede_xdp_tx_int(edev, fp->xdp_tx);
1368 
1369 	rx_work_done = (likely(fp->type & QEDE_FASTPATH_RX) &&
1370 			qede_has_rx_work(fp->rxq)) ?
1371 			qede_rx_int(fp, budget) : 0;
1372 	if (rx_work_done < budget) {
1373 		if (!qede_poll_is_more_work(fp)) {
1374 			napi_complete_done(napi, rx_work_done);
1375 
1376 			/* Update and reenable interrupts */
1377 			qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1378 		} else {
1379 			rx_work_done = budget;
1380 		}
1381 	}
1382 
1383 	if (fp->xdp_xmit) {
1384 		u16 xdp_prod = qed_chain_get_prod_idx(&fp->xdp_tx->tx_pbl);
1385 
1386 		fp->xdp_xmit = 0;
1387 		fp->xdp_tx->tx_db.data.bd_prod = cpu_to_le16(xdp_prod);
1388 		qede_update_tx_producer(fp->xdp_tx);
1389 	}
1390 
1391 	return rx_work_done;
1392 }
1393 
1394 irqreturn_t qede_msix_fp_int(int irq, void *fp_cookie)
1395 {
1396 	struct qede_fastpath *fp = fp_cookie;
1397 
1398 	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
1399 
1400 	napi_schedule_irqoff(&fp->napi);
1401 	return IRQ_HANDLED;
1402 }
1403 
1404 /* Main transmit function */
1405 netdev_tx_t qede_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1406 {
1407 	struct qede_dev *edev = netdev_priv(ndev);
1408 	struct netdev_queue *netdev_txq;
1409 	struct qede_tx_queue *txq;
1410 	struct eth_tx_1st_bd *first_bd;
1411 	struct eth_tx_2nd_bd *second_bd = NULL;
1412 	struct eth_tx_3rd_bd *third_bd = NULL;
1413 	struct eth_tx_bd *tx_data_bd = NULL;
1414 	u16 txq_index, val = 0;
1415 	u8 nbd = 0;
1416 	dma_addr_t mapping;
1417 	int rc, frag_idx = 0, ipv6_ext = 0;
1418 	u8 xmit_type;
1419 	u16 idx;
1420 	u16 hlen;
1421 	bool data_split = false;
1422 
1423 	/* Get tx-queue context and netdev index */
1424 	txq_index = skb_get_queue_mapping(skb);
1425 	WARN_ON(txq_index >= QEDE_TSS_COUNT(edev) * edev->dev_info.num_tc);
1426 	txq = QEDE_NDEV_TXQ_ID_TO_TXQ(edev, txq_index);
1427 	netdev_txq = netdev_get_tx_queue(ndev, txq_index);
1428 
1429 	WARN_ON(qed_chain_get_elem_left(&txq->tx_pbl) < (MAX_SKB_FRAGS + 1));
1430 
1431 	xmit_type = qede_xmit_type(skb, &ipv6_ext);
1432 
1433 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
1434 	if (qede_pkt_req_lin(skb, xmit_type)) {
1435 		if (skb_linearize(skb)) {
1436 			txq->tx_mem_alloc_err++;
1437 
1438 			dev_kfree_skb_any(skb);
1439 			return NETDEV_TX_OK;
1440 		}
1441 	}
1442 #endif
1443 
1444 	/* Fill the entry in the SW ring and the BDs in the FW ring */
1445 	idx = txq->sw_tx_prod;
1446 	txq->sw_tx_ring.skbs[idx].skb = skb;
1447 	first_bd = (struct eth_tx_1st_bd *)
1448 		   qed_chain_produce(&txq->tx_pbl);
1449 	memset(first_bd, 0, sizeof(*first_bd));
1450 	first_bd->data.bd_flags.bitfields =
1451 		1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1452 
1453 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1454 		qede_ptp_tx_ts(edev, skb);
1455 
1456 	/* Map skb linear data for DMA and set in the first BD */
1457 	mapping = dma_map_single(txq->dev, skb->data,
1458 				 skb_headlen(skb), DMA_TO_DEVICE);
1459 	if (unlikely(dma_mapping_error(txq->dev, mapping))) {
1460 		DP_NOTICE(edev, "SKB mapping failed\n");
1461 		qede_free_failed_tx_pkt(txq, first_bd, 0, false);
1462 		qede_update_tx_producer(txq);
1463 		return NETDEV_TX_OK;
1464 	}
1465 	nbd++;
1466 	BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1467 
1468 	/* In case there is IPv6 with extension headers or LSO we need 2nd and
1469 	 * 3rd BDs.
1470 	 */
1471 	if (unlikely((xmit_type & XMIT_LSO) | ipv6_ext)) {
1472 		second_bd = (struct eth_tx_2nd_bd *)
1473 			qed_chain_produce(&txq->tx_pbl);
1474 		memset(second_bd, 0, sizeof(*second_bd));
1475 
1476 		nbd++;
1477 		third_bd = (struct eth_tx_3rd_bd *)
1478 			qed_chain_produce(&txq->tx_pbl);
1479 		memset(third_bd, 0, sizeof(*third_bd));
1480 
1481 		nbd++;
1482 		/* We need to fill in additional data in second_bd... */
1483 		tx_data_bd = (struct eth_tx_bd *)second_bd;
1484 	}
1485 
1486 	if (skb_vlan_tag_present(skb)) {
1487 		first_bd->data.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
1488 		first_bd->data.bd_flags.bitfields |=
1489 			1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
1490 	}
1491 
1492 	/* Fill the parsing flags & params according to the requested offload */
1493 	if (xmit_type & XMIT_L4_CSUM) {
1494 		/* We don't re-calculate IP checksum as it is already done by
1495 		 * the upper stack
1496 		 */
1497 		first_bd->data.bd_flags.bitfields |=
1498 			1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;
1499 
1500 		if (xmit_type & XMIT_ENC) {
1501 			first_bd->data.bd_flags.bitfields |=
1502 				1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1503 
1504 			val |= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1505 		}
1506 
1507 		/* Legacy FW had flipped behavior in regard to this bit -
1508 		 * I.e., needed to set to prevent FW from touching encapsulated
1509 		 * packets when it didn't need to.
1510 		 */
1511 		if (unlikely(txq->is_legacy))
1512 			val ^= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1513 
1514 		/* If the packet is IPv6 with extension header, indicate that
1515 		 * to FW and pass few params, since the device cracker doesn't
1516 		 * support parsing IPv6 with extension header/s.
1517 		 */
1518 		if (unlikely(ipv6_ext))
1519 			qede_set_params_for_ipv6_ext(skb, second_bd, third_bd);
1520 	}
1521 
1522 	if (xmit_type & XMIT_LSO) {
1523 		first_bd->data.bd_flags.bitfields |=
1524 			(1 << ETH_TX_1ST_BD_FLAGS_LSO_SHIFT);
1525 		third_bd->data.lso_mss =
1526 			cpu_to_le16(skb_shinfo(skb)->gso_size);
1527 
1528 		if (unlikely(xmit_type & XMIT_ENC)) {
1529 			first_bd->data.bd_flags.bitfields |=
1530 				1 << ETH_TX_1ST_BD_FLAGS_TUNN_IP_CSUM_SHIFT;
1531 
1532 			if (xmit_type & XMIT_ENC_GSO_L4_CSUM) {
1533 				u8 tmp = ETH_TX_1ST_BD_FLAGS_TUNN_L4_CSUM_SHIFT;
1534 
1535 				first_bd->data.bd_flags.bitfields |= 1 << tmp;
1536 			}
1537 			hlen = qede_get_skb_hlen(skb, true);
1538 		} else {
1539 			first_bd->data.bd_flags.bitfields |=
1540 				1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1541 			hlen = qede_get_skb_hlen(skb, false);
1542 		}
1543 
1544 		/* @@@TBD - if will not be removed need to check */
1545 		third_bd->data.bitfields |=
1546 			cpu_to_le16(1 << ETH_TX_DATA_3RD_BD_HDR_NBD_SHIFT);
1547 
1548 		/* Make life easier for FW guys who can't deal with header and
1549 		 * data on same BD. If we need to split, use the second bd...
1550 		 */
1551 		if (unlikely(skb_headlen(skb) > hlen)) {
1552 			DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1553 				   "TSO split header size is %d (%x:%x)\n",
1554 				   first_bd->nbytes, first_bd->addr.hi,
1555 				   first_bd->addr.lo);
1556 
1557 			mapping = HILO_U64(le32_to_cpu(first_bd->addr.hi),
1558 					   le32_to_cpu(first_bd->addr.lo)) +
1559 					   hlen;
1560 
1561 			BD_SET_UNMAP_ADDR_LEN(tx_data_bd, mapping,
1562 					      le16_to_cpu(first_bd->nbytes) -
1563 					      hlen);
1564 
1565 			/* this marks the BD as one that has no
1566 			 * individual mapping
1567 			 */
1568 			txq->sw_tx_ring.skbs[idx].flags |= QEDE_TSO_SPLIT_BD;
1569 
1570 			first_bd->nbytes = cpu_to_le16(hlen);
1571 
1572 			tx_data_bd = (struct eth_tx_bd *)third_bd;
1573 			data_split = true;
1574 		}
1575 	} else {
1576 		val |= ((skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
1577 			 ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT);
1578 	}
1579 
1580 	first_bd->data.bitfields = cpu_to_le16(val);
1581 
1582 	/* Handle fragmented skb */
1583 	/* special handle for frags inside 2nd and 3rd bds.. */
1584 	while (tx_data_bd && frag_idx < skb_shinfo(skb)->nr_frags) {
1585 		rc = map_frag_to_bd(txq,
1586 				    &skb_shinfo(skb)->frags[frag_idx],
1587 				    tx_data_bd);
1588 		if (rc) {
1589 			qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1590 			qede_update_tx_producer(txq);
1591 			return NETDEV_TX_OK;
1592 		}
1593 
1594 		if (tx_data_bd == (struct eth_tx_bd *)second_bd)
1595 			tx_data_bd = (struct eth_tx_bd *)third_bd;
1596 		else
1597 			tx_data_bd = NULL;
1598 
1599 		frag_idx++;
1600 	}
1601 
1602 	/* map last frags into 4th, 5th .... */
1603 	for (; frag_idx < skb_shinfo(skb)->nr_frags; frag_idx++, nbd++) {
1604 		tx_data_bd = (struct eth_tx_bd *)
1605 			     qed_chain_produce(&txq->tx_pbl);
1606 
1607 		memset(tx_data_bd, 0, sizeof(*tx_data_bd));
1608 
1609 		rc = map_frag_to_bd(txq,
1610 				    &skb_shinfo(skb)->frags[frag_idx],
1611 				    tx_data_bd);
1612 		if (rc) {
1613 			qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1614 			qede_update_tx_producer(txq);
1615 			return NETDEV_TX_OK;
1616 		}
1617 	}
1618 
1619 	/* update the first BD with the actual num BDs */
1620 	first_bd->data.nbds = nbd;
1621 
1622 	netdev_tx_sent_queue(netdev_txq, skb->len);
1623 
1624 	skb_tx_timestamp(skb);
1625 
1626 	/* Advance packet producer only before sending the packet since mapping
1627 	 * of pages may fail.
1628 	 */
1629 	txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
1630 
1631 	/* 'next page' entries are counted in the producer value */
1632 	txq->tx_db.data.bd_prod =
1633 		cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
1634 
1635 	if (!netdev_xmit_more() || netif_xmit_stopped(netdev_txq))
1636 		qede_update_tx_producer(txq);
1637 
1638 	if (unlikely(qed_chain_get_elem_left(&txq->tx_pbl)
1639 		      < (MAX_SKB_FRAGS + 1))) {
1640 		if (netdev_xmit_more())
1641 			qede_update_tx_producer(txq);
1642 
1643 		netif_tx_stop_queue(netdev_txq);
1644 		txq->stopped_cnt++;
1645 		DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1646 			   "Stop queue was called\n");
1647 		/* paired memory barrier is in qede_tx_int(), we have to keep
1648 		 * ordering of set_bit() in netif_tx_stop_queue() and read of
1649 		 * fp->bd_tx_cons
1650 		 */
1651 		smp_mb();
1652 
1653 		if ((qed_chain_get_elem_left(&txq->tx_pbl) >=
1654 		     (MAX_SKB_FRAGS + 1)) &&
1655 		    (edev->state == QEDE_STATE_OPEN)) {
1656 			netif_tx_wake_queue(netdev_txq);
1657 			DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1658 				   "Wake queue was called\n");
1659 		}
1660 	}
1661 
1662 	return NETDEV_TX_OK;
1663 }
1664 
1665 u16 qede_select_queue(struct net_device *dev, struct sk_buff *skb,
1666 		      struct net_device *sb_dev)
1667 {
1668 	struct qede_dev *edev = netdev_priv(dev);
1669 	int total_txq;
1670 
1671 	total_txq = QEDE_TSS_COUNT(edev) * edev->dev_info.num_tc;
1672 
1673 	return QEDE_TSS_COUNT(edev) ?
1674 		netdev_pick_tx(dev, skb, NULL) % total_txq :  0;
1675 }
1676 
1677 /* 8B udp header + 8B base tunnel header + 32B option length */
1678 #define QEDE_MAX_TUN_HDR_LEN 48
1679 
1680 netdev_features_t qede_features_check(struct sk_buff *skb,
1681 				      struct net_device *dev,
1682 				      netdev_features_t features)
1683 {
1684 	if (skb->encapsulation) {
1685 		u8 l4_proto = 0;
1686 
1687 		switch (vlan_get_protocol(skb)) {
1688 		case htons(ETH_P_IP):
1689 			l4_proto = ip_hdr(skb)->protocol;
1690 			break;
1691 		case htons(ETH_P_IPV6):
1692 			l4_proto = ipv6_hdr(skb)->nexthdr;
1693 			break;
1694 		default:
1695 			return features;
1696 		}
1697 
1698 		/* Disable offloads for geneve tunnels, as HW can't parse
1699 		 * the geneve header which has option length greater than 32b
1700 		 * and disable offloads for the ports which are not offloaded.
1701 		 */
1702 		if (l4_proto == IPPROTO_UDP) {
1703 			struct qede_dev *edev = netdev_priv(dev);
1704 			u16 hdrlen, vxln_port, gnv_port;
1705 
1706 			hdrlen = QEDE_MAX_TUN_HDR_LEN;
1707 			vxln_port = edev->vxlan_dst_port;
1708 			gnv_port = edev->geneve_dst_port;
1709 
1710 			if ((skb_inner_mac_header(skb) -
1711 			     skb_transport_header(skb)) > hdrlen ||
1712 			     (ntohs(udp_hdr(skb)->dest) != vxln_port &&
1713 			      ntohs(udp_hdr(skb)->dest) != gnv_port))
1714 				return features & ~(NETIF_F_CSUM_MASK |
1715 						    NETIF_F_GSO_MASK);
1716 		}
1717 	}
1718 
1719 	return features;
1720 }
1721