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