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