1 /* QLogic qede NIC Driver
2 * Copyright (c) 2015-2017 QLogic Corporation
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/bpf_trace.h>
36 #include <net/udp_tunnel.h>
40 #include <linux/if_ether.h>
41 #include <linux/if_vlan.h>
42 #include <net/ip6_checksum.h>
45 #include <linux/qed/qed_if.h>
47 /*********************************
48 * Content also used by slowpath *
49 *********************************/
51 int qede_alloc_rx_buffer(struct qede_rx_queue *rxq, bool allow_lazy)
53 struct sw_rx_data *sw_rx_data;
54 struct eth_rx_bd *rx_bd;
58 /* In case lazy-allocation is allowed, postpone allocation until the
59 * end of the NAPI run. We'd still need to make sure the Rx ring has
60 * sufficient buffers to guarantee an additional Rx interrupt.
62 if (allow_lazy && likely(rxq->filled_buffers > 12)) {
63 rxq->filled_buffers--;
67 data = alloc_pages(GFP_ATOMIC, 0);
71 /* Map the entire page as it would be used
72 * for multiple RX buffer segment size mapping.
74 mapping = dma_map_page(rxq->dev, data, 0,
75 PAGE_SIZE, rxq->data_direction);
76 if (unlikely(dma_mapping_error(rxq->dev, mapping))) {
81 sw_rx_data = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
82 sw_rx_data->page_offset = 0;
83 sw_rx_data->data = data;
84 sw_rx_data->mapping = mapping;
86 /* Advance PROD and get BD pointer */
87 rx_bd = (struct eth_rx_bd *)qed_chain_produce(&rxq->rx_bd_ring);
89 rx_bd->addr.hi = cpu_to_le32(upper_32_bits(mapping));
90 rx_bd->addr.lo = cpu_to_le32(lower_32_bits(mapping) +
94 rxq->filled_buffers++;
99 /* Unmap the data and free skb */
100 int qede_free_tx_pkt(struct qede_dev *edev, struct qede_tx_queue *txq, int *len)
102 u16 idx = txq->sw_tx_cons;
103 struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
104 struct eth_tx_1st_bd *first_bd;
105 struct eth_tx_bd *tx_data_bd;
106 int bds_consumed = 0;
108 bool data_split = txq->sw_tx_ring.skbs[idx].flags & QEDE_TSO_SPLIT_BD;
109 int i, split_bd_len = 0;
111 if (unlikely(!skb)) {
113 "skb is null for txq idx=%d txq->sw_tx_cons=%d txq->sw_tx_prod=%d\n",
114 idx, txq->sw_tx_cons, txq->sw_tx_prod);
120 first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
124 nbds = first_bd->data.nbds;
127 struct eth_tx_bd *split = (struct eth_tx_bd *)
128 qed_chain_consume(&txq->tx_pbl);
129 split_bd_len = BD_UNMAP_LEN(split);
132 dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
133 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
135 /* Unmap the data of the skb frags */
136 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, bds_consumed++) {
137 tx_data_bd = (struct eth_tx_bd *)
138 qed_chain_consume(&txq->tx_pbl);
139 dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
140 BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
143 while (bds_consumed++ < nbds)
144 qed_chain_consume(&txq->tx_pbl);
147 dev_kfree_skb_any(skb);
148 txq->sw_tx_ring.skbs[idx].skb = NULL;
149 txq->sw_tx_ring.skbs[idx].flags = 0;
154 /* Unmap the data and free skb when mapping failed during start_xmit */
155 static void qede_free_failed_tx_pkt(struct qede_tx_queue *txq,
156 struct eth_tx_1st_bd *first_bd,
157 int nbd, bool data_split)
159 u16 idx = txq->sw_tx_prod;
160 struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
161 struct eth_tx_bd *tx_data_bd;
162 int i, split_bd_len = 0;
164 /* Return prod to its position before this skb was handled */
165 qed_chain_set_prod(&txq->tx_pbl,
166 le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
168 first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
171 struct eth_tx_bd *split = (struct eth_tx_bd *)
172 qed_chain_produce(&txq->tx_pbl);
173 split_bd_len = BD_UNMAP_LEN(split);
177 dma_unmap_single(txq->dev, BD_UNMAP_ADDR(first_bd),
178 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
180 /* Unmap the data of the skb frags */
181 for (i = 0; i < nbd; i++) {
182 tx_data_bd = (struct eth_tx_bd *)
183 qed_chain_produce(&txq->tx_pbl);
184 if (tx_data_bd->nbytes)
185 dma_unmap_page(txq->dev,
186 BD_UNMAP_ADDR(tx_data_bd),
187 BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
190 /* Return again prod to its position before this skb was handled */
191 qed_chain_set_prod(&txq->tx_pbl,
192 le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
195 dev_kfree_skb_any(skb);
196 txq->sw_tx_ring.skbs[idx].skb = NULL;
197 txq->sw_tx_ring.skbs[idx].flags = 0;
200 static u32 qede_xmit_type(struct sk_buff *skb, int *ipv6_ext)
202 u32 rc = XMIT_L4_CSUM;
205 if (skb->ip_summed != CHECKSUM_PARTIAL)
208 l3_proto = vlan_get_protocol(skb);
209 if (l3_proto == htons(ETH_P_IPV6) &&
210 (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
213 if (skb->encapsulation) {
215 if (skb_is_gso(skb)) {
216 unsigned short gso_type = skb_shinfo(skb)->gso_type;
218 if ((gso_type & SKB_GSO_UDP_TUNNEL_CSUM) ||
219 (gso_type & SKB_GSO_GRE_CSUM))
220 rc |= XMIT_ENC_GSO_L4_CSUM;
233 static void qede_set_params_for_ipv6_ext(struct sk_buff *skb,
234 struct eth_tx_2nd_bd *second_bd,
235 struct eth_tx_3rd_bd *third_bd)
238 u16 bd2_bits1 = 0, bd2_bits2 = 0;
240 bd2_bits1 |= (1 << ETH_TX_DATA_2ND_BD_IPV6_EXT_SHIFT);
242 bd2_bits2 |= ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) &
243 ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_MASK)
244 << ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_SHIFT;
246 bd2_bits1 |= (ETH_L4_PSEUDO_CSUM_CORRECT_LENGTH <<
247 ETH_TX_DATA_2ND_BD_L4_PSEUDO_CSUM_MODE_SHIFT);
249 if (vlan_get_protocol(skb) == htons(ETH_P_IPV6))
250 l4_proto = ipv6_hdr(skb)->nexthdr;
252 l4_proto = ip_hdr(skb)->protocol;
254 if (l4_proto == IPPROTO_UDP)
255 bd2_bits1 |= 1 << ETH_TX_DATA_2ND_BD_L4_UDP_SHIFT;
258 third_bd->data.bitfields |=
259 cpu_to_le16(((tcp_hdrlen(skb) / 4) &
260 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_MASK) <<
261 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_SHIFT);
263 second_bd->data.bitfields1 = cpu_to_le16(bd2_bits1);
264 second_bd->data.bitfields2 = cpu_to_le16(bd2_bits2);
267 static int map_frag_to_bd(struct qede_tx_queue *txq,
268 skb_frag_t *frag, struct eth_tx_bd *bd)
272 /* Map skb non-linear frag data for DMA */
273 mapping = skb_frag_dma_map(txq->dev, frag, 0,
274 skb_frag_size(frag), DMA_TO_DEVICE);
275 if (unlikely(dma_mapping_error(txq->dev, mapping)))
278 /* Setup the data pointer of the frag data */
279 BD_SET_UNMAP_ADDR_LEN(bd, mapping, skb_frag_size(frag));
284 static u16 qede_get_skb_hlen(struct sk_buff *skb, bool is_encap_pkt)
287 return (skb_inner_transport_header(skb) +
288 inner_tcp_hdrlen(skb) - skb->data);
290 return (skb_transport_header(skb) +
291 tcp_hdrlen(skb) - skb->data);
294 /* +2 for 1st BD for headers and 2nd BD for headlen (if required) */
295 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
296 static bool qede_pkt_req_lin(struct sk_buff *skb, u8 xmit_type)
298 int allowed_frags = ETH_TX_MAX_BDS_PER_NON_LSO_PACKET - 1;
300 if (xmit_type & XMIT_LSO) {
303 hlen = qede_get_skb_hlen(skb, xmit_type & XMIT_ENC);
305 /* linear payload would require its own BD */
306 if (skb_headlen(skb) > hlen)
310 return (skb_shinfo(skb)->nr_frags > allowed_frags);
314 static inline void qede_update_tx_producer(struct qede_tx_queue *txq)
316 /* wmb makes sure that the BDs data is updated before updating the
317 * producer, otherwise FW may read old data from the BDs.
321 writel(txq->tx_db.raw, txq->doorbell_addr);
323 /* mmiowb is needed to synchronize doorbell writes from more than one
324 * processor. It guarantees that the write arrives to the device before
325 * the queue lock is released and another start_xmit is called (possibly
326 * on another CPU). Without this barrier, the next doorbell can bypass
327 * this doorbell. This is applicable to IA64/Altix systems.
332 static int qede_xdp_xmit(struct qede_dev *edev, struct qede_fastpath *fp,
333 struct sw_rx_data *metadata, u16 padding, u16 length)
335 struct qede_tx_queue *txq = fp->xdp_tx;
336 struct eth_tx_1st_bd *first_bd;
337 u16 idx = txq->sw_tx_prod;
340 if (!qed_chain_get_elem_left(&txq->tx_pbl)) {
345 first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
347 memset(first_bd, 0, sizeof(*first_bd));
348 first_bd->data.bd_flags.bitfields =
349 BIT(ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT);
351 val = (length & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
352 ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
354 first_bd->data.bitfields |= cpu_to_le16(val);
355 first_bd->data.nbds = 1;
357 /* We can safely ignore the offset, as it's 0 for XDP */
358 BD_SET_UNMAP_ADDR_LEN(first_bd, metadata->mapping + padding, length);
360 /* Synchronize the buffer back to device, as program [probably]
363 dma_sync_single_for_device(&edev->pdev->dev,
364 metadata->mapping + padding,
365 length, PCI_DMA_TODEVICE);
367 txq->sw_tx_ring.xdp[idx].page = metadata->data;
368 txq->sw_tx_ring.xdp[idx].mapping = metadata->mapping;
369 txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
371 /* Mark the fastpath for future XDP doorbell */
377 int qede_txq_has_work(struct qede_tx_queue *txq)
381 /* Tell compiler that consumer and producer can change */
383 hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
384 if (qed_chain_get_cons_idx(&txq->tx_pbl) == hw_bd_cons + 1)
387 return hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl);
390 static void qede_xdp_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
394 hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
397 while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
398 qed_chain_consume(&txq->tx_pbl);
399 idx = txq->sw_tx_cons;
401 dma_unmap_page(&edev->pdev->dev,
402 txq->sw_tx_ring.xdp[idx].mapping,
403 PAGE_SIZE, DMA_BIDIRECTIONAL);
404 __free_page(txq->sw_tx_ring.xdp[idx].page);
406 txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
411 static int qede_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
413 struct netdev_queue *netdev_txq;
415 unsigned int pkts_compl = 0, bytes_compl = 0;
418 netdev_txq = netdev_get_tx_queue(edev->ndev, txq->index);
420 hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
423 while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
426 rc = qede_free_tx_pkt(edev, txq, &len);
428 DP_NOTICE(edev, "hw_bd_cons = %d, chain_cons=%d\n",
430 qed_chain_get_cons_idx(&txq->tx_pbl));
436 txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
440 netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);
442 /* Need to make the tx_bd_cons update visible to start_xmit()
443 * before checking for netif_tx_queue_stopped(). Without the
444 * memory barrier, there is a small possibility that
445 * start_xmit() will miss it and cause the queue to be stopped
447 * On the other hand we need an rmb() here to ensure the proper
448 * ordering of bit testing in the following
449 * netif_tx_queue_stopped(txq) call.
453 if (unlikely(netif_tx_queue_stopped(netdev_txq))) {
454 /* Taking tx_lock is needed to prevent reenabling the queue
455 * while it's empty. This could have happen if rx_action() gets
456 * suspended in qede_tx_int() after the condition before
457 * netif_tx_wake_queue(), while tx_action (qede_start_xmit()):
459 * stops the queue->sees fresh tx_bd_cons->releases the queue->
460 * sends some packets consuming the whole queue again->
464 __netif_tx_lock(netdev_txq, smp_processor_id());
466 if ((netif_tx_queue_stopped(netdev_txq)) &&
467 (edev->state == QEDE_STATE_OPEN) &&
468 (qed_chain_get_elem_left(&txq->tx_pbl)
469 >= (MAX_SKB_FRAGS + 1))) {
470 netif_tx_wake_queue(netdev_txq);
471 DP_VERBOSE(edev, NETIF_MSG_TX_DONE,
472 "Wake queue was called\n");
475 __netif_tx_unlock(netdev_txq);
481 bool qede_has_rx_work(struct qede_rx_queue *rxq)
483 u16 hw_comp_cons, sw_comp_cons;
485 /* Tell compiler that status block fields can change */
488 hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
489 sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
491 return hw_comp_cons != sw_comp_cons;
494 static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
496 qed_chain_consume(&rxq->rx_bd_ring);
500 /* This function reuses the buffer(from an offset) from
501 * consumer index to producer index in the bd ring
503 static inline void qede_reuse_page(struct qede_rx_queue *rxq,
504 struct sw_rx_data *curr_cons)
506 struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
507 struct sw_rx_data *curr_prod;
508 dma_addr_t new_mapping;
510 curr_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
511 *curr_prod = *curr_cons;
513 new_mapping = curr_prod->mapping + curr_prod->page_offset;
515 rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(new_mapping));
516 rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(new_mapping) +
520 curr_cons->data = NULL;
523 /* In case of allocation failures reuse buffers
524 * from consumer index to produce buffers for firmware
526 void qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq, u8 count)
528 struct sw_rx_data *curr_cons;
530 for (; count > 0; count--) {
531 curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
532 qede_reuse_page(rxq, curr_cons);
533 qede_rx_bd_ring_consume(rxq);
537 static inline int qede_realloc_rx_buffer(struct qede_rx_queue *rxq,
538 struct sw_rx_data *curr_cons)
540 /* Move to the next segment in the page */
541 curr_cons->page_offset += rxq->rx_buf_seg_size;
543 if (curr_cons->page_offset == PAGE_SIZE) {
544 if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
545 /* Since we failed to allocate new buffer
546 * current buffer can be used again.
548 curr_cons->page_offset -= rxq->rx_buf_seg_size;
553 dma_unmap_page(rxq->dev, curr_cons->mapping,
554 PAGE_SIZE, rxq->data_direction);
556 /* Increment refcount of the page as we don't want
557 * network stack to take the ownership of the page
558 * which can be recycled multiple times by the driver.
560 page_ref_inc(curr_cons->data);
561 qede_reuse_page(rxq, curr_cons);
567 void qede_update_rx_prod(struct qede_dev *edev, struct qede_rx_queue *rxq)
569 u16 bd_prod = qed_chain_get_prod_idx(&rxq->rx_bd_ring);
570 u16 cqe_prod = qed_chain_get_prod_idx(&rxq->rx_comp_ring);
571 struct eth_rx_prod_data rx_prods = {0};
573 /* Update producers */
574 rx_prods.bd_prod = cpu_to_le16(bd_prod);
575 rx_prods.cqe_prod = cpu_to_le16(cqe_prod);
577 /* Make sure that the BD and SGE data is updated before updating the
578 * producers since FW might read the BD/SGE right after the producer
583 internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
586 /* mmiowb is needed to synchronize doorbell writes from more than one
587 * processor. It guarantees that the write arrives to the device before
588 * the napi lock is released and another qede_poll is called (possibly
589 * on another CPU). Without this barrier, the next doorbell can bypass
590 * this doorbell. This is applicable to IA64/Altix systems.
595 static void qede_get_rxhash(struct sk_buff *skb, u8 bitfields, __le32 rss_hash)
597 enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
598 enum rss_hash_type htype;
601 htype = GET_FIELD(bitfields, ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
603 hash_type = ((htype == RSS_HASH_TYPE_IPV4) ||
604 (htype == RSS_HASH_TYPE_IPV6)) ?
605 PKT_HASH_TYPE_L3 : PKT_HASH_TYPE_L4;
606 hash = le32_to_cpu(rss_hash);
608 skb_set_hash(skb, hash, hash_type);
611 static void qede_set_skb_csum(struct sk_buff *skb, u8 csum_flag)
613 skb_checksum_none_assert(skb);
615 if (csum_flag & QEDE_CSUM_UNNECESSARY)
616 skb->ip_summed = CHECKSUM_UNNECESSARY;
618 if (csum_flag & QEDE_TUNN_CSUM_UNNECESSARY) {
620 skb->encapsulation = 1;
624 static inline void qede_skb_receive(struct qede_dev *edev,
625 struct qede_fastpath *fp,
626 struct qede_rx_queue *rxq,
627 struct sk_buff *skb, u16 vlan_tag)
630 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
632 napi_gro_receive(&fp->napi, skb);
635 static void qede_set_gro_params(struct qede_dev *edev,
637 struct eth_fast_path_rx_tpa_start_cqe *cqe)
639 u16 parsing_flags = le16_to_cpu(cqe->pars_flags.flags);
641 if (((parsing_flags >> PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) &
642 PARSING_AND_ERR_FLAGS_L3TYPE_MASK) == 2)
643 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
645 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
647 skb_shinfo(skb)->gso_size = __le16_to_cpu(cqe->len_on_first_bd) -
651 static int qede_fill_frag_skb(struct qede_dev *edev,
652 struct qede_rx_queue *rxq,
653 u8 tpa_agg_index, u16 len_on_bd)
655 struct sw_rx_data *current_bd = &rxq->sw_rx_ring[rxq->sw_rx_cons &
657 struct qede_agg_info *tpa_info = &rxq->tpa_info[tpa_agg_index];
658 struct sk_buff *skb = tpa_info->skb;
660 if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
663 /* Add one frag and update the appropriate fields in the skb */
664 skb_fill_page_desc(skb, tpa_info->frag_id++,
665 current_bd->data, current_bd->page_offset,
668 if (unlikely(qede_realloc_rx_buffer(rxq, current_bd))) {
669 /* Incr page ref count to reuse on allocation failure
670 * so that it doesn't get freed while freeing SKB.
672 page_ref_inc(current_bd->data);
676 qed_chain_consume(&rxq->rx_bd_ring);
679 skb->data_len += len_on_bd;
680 skb->truesize += rxq->rx_buf_seg_size;
681 skb->len += len_on_bd;
686 tpa_info->state = QEDE_AGG_STATE_ERROR;
687 qede_recycle_rx_bd_ring(rxq, 1);
692 static bool qede_tunn_exist(u16 flag)
694 return !!(flag & (PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
695 PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT));
698 static u8 qede_check_tunn_csum(u16 flag)
703 if (flag & (PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
704 PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT))
705 csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
706 PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;
708 if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
709 PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
710 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
711 PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
712 tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
715 csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
716 PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
717 PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
718 PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
720 if (csum_flag & flag)
721 return QEDE_CSUM_ERROR;
723 return QEDE_CSUM_UNNECESSARY | tcsum;
726 static void qede_tpa_start(struct qede_dev *edev,
727 struct qede_rx_queue *rxq,
728 struct eth_fast_path_rx_tpa_start_cqe *cqe)
730 struct qede_agg_info *tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
731 struct eth_rx_bd *rx_bd_cons = qed_chain_consume(&rxq->rx_bd_ring);
732 struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
733 struct sw_rx_data *replace_buf = &tpa_info->buffer;
734 dma_addr_t mapping = tpa_info->buffer_mapping;
735 struct sw_rx_data *sw_rx_data_cons;
736 struct sw_rx_data *sw_rx_data_prod;
738 sw_rx_data_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
739 sw_rx_data_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
741 /* Use pre-allocated replacement buffer - we can't release the agg.
742 * start until its over and we don't want to risk allocation failing
743 * here, so re-allocate when aggregation will be over.
745 sw_rx_data_prod->mapping = replace_buf->mapping;
747 sw_rx_data_prod->data = replace_buf->data;
748 rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(mapping));
749 rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(mapping));
750 sw_rx_data_prod->page_offset = replace_buf->page_offset;
754 /* move partial skb from cons to pool (don't unmap yet)
755 * save mapping, incase we drop the packet later on.
757 tpa_info->buffer = *sw_rx_data_cons;
758 mapping = HILO_U64(le32_to_cpu(rx_bd_cons->addr.hi),
759 le32_to_cpu(rx_bd_cons->addr.lo));
761 tpa_info->buffer_mapping = mapping;
764 /* set tpa state to start only if we are able to allocate skb
765 * for this aggregation, otherwise mark as error and aggregation will
768 tpa_info->skb = netdev_alloc_skb(edev->ndev,
769 le16_to_cpu(cqe->len_on_first_bd));
770 if (unlikely(!tpa_info->skb)) {
771 DP_NOTICE(edev, "Failed to allocate SKB for gro\n");
772 tpa_info->state = QEDE_AGG_STATE_ERROR;
776 /* Start filling in the aggregation info */
777 skb_put(tpa_info->skb, le16_to_cpu(cqe->len_on_first_bd));
778 tpa_info->frag_id = 0;
779 tpa_info->state = QEDE_AGG_STATE_START;
781 /* Store some information from first CQE */
782 tpa_info->start_cqe_placement_offset = cqe->placement_offset;
783 tpa_info->start_cqe_bd_len = le16_to_cpu(cqe->len_on_first_bd);
784 if ((le16_to_cpu(cqe->pars_flags.flags) >>
785 PARSING_AND_ERR_FLAGS_TAG8021QEXIST_SHIFT) &
786 PARSING_AND_ERR_FLAGS_TAG8021QEXIST_MASK)
787 tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
789 tpa_info->vlan_tag = 0;
791 qede_get_rxhash(tpa_info->skb, cqe->bitfields, cqe->rss_hash);
793 /* This is needed in order to enable forwarding support */
794 qede_set_gro_params(edev, tpa_info->skb, cqe);
796 cons_buf: /* We still need to handle bd_len_list to consume buffers */
797 if (likely(cqe->ext_bd_len_list[0]))
798 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
799 le16_to_cpu(cqe->ext_bd_len_list[0]));
801 if (unlikely(cqe->ext_bd_len_list[1])) {
803 "Unlikely - got a TPA aggregation with more than one ext_bd_len_list entry in the TPA start\n");
804 tpa_info->state = QEDE_AGG_STATE_ERROR;
809 static void qede_gro_ip_csum(struct sk_buff *skb)
811 const struct iphdr *iph = ip_hdr(skb);
814 skb_set_transport_header(skb, sizeof(struct iphdr));
817 th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
818 iph->saddr, iph->daddr, 0);
820 tcp_gro_complete(skb);
823 static void qede_gro_ipv6_csum(struct sk_buff *skb)
825 struct ipv6hdr *iph = ipv6_hdr(skb);
828 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
831 th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
832 &iph->saddr, &iph->daddr, 0);
833 tcp_gro_complete(skb);
837 static void qede_gro_receive(struct qede_dev *edev,
838 struct qede_fastpath *fp,
842 /* FW can send a single MTU sized packet from gro flow
843 * due to aggregation timeout/last segment etc. which
844 * is not expected to be a gro packet. If a skb has zero
845 * frags then simply push it in the stack as non gso skb.
847 if (unlikely(!skb->data_len)) {
848 skb_shinfo(skb)->gso_type = 0;
849 skb_shinfo(skb)->gso_size = 0;
854 if (skb_shinfo(skb)->gso_size) {
855 skb_reset_network_header(skb);
857 switch (skb->protocol) {
858 case htons(ETH_P_IP):
859 qede_gro_ip_csum(skb);
861 case htons(ETH_P_IPV6):
862 qede_gro_ipv6_csum(skb);
866 "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
867 ntohs(skb->protocol));
873 skb_record_rx_queue(skb, fp->rxq->rxq_id);
874 qede_skb_receive(edev, fp, fp->rxq, skb, vlan_tag);
877 static inline void qede_tpa_cont(struct qede_dev *edev,
878 struct qede_rx_queue *rxq,
879 struct eth_fast_path_rx_tpa_cont_cqe *cqe)
883 for (i = 0; cqe->len_list[i]; i++)
884 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
885 le16_to_cpu(cqe->len_list[i]));
889 "Strange - TPA cont with more than a single len_list entry\n");
892 static int qede_tpa_end(struct qede_dev *edev,
893 struct qede_fastpath *fp,
894 struct eth_fast_path_rx_tpa_end_cqe *cqe)
896 struct qede_rx_queue *rxq = fp->rxq;
897 struct qede_agg_info *tpa_info;
901 tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
904 for (i = 0; cqe->len_list[i]; i++)
905 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
906 le16_to_cpu(cqe->len_list[i]));
909 "Strange - TPA emd with more than a single len_list entry\n");
911 if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
915 if (unlikely(cqe->num_of_bds != tpa_info->frag_id + 1))
917 "Strange - TPA had %02x BDs, but SKB has only %d frags\n",
918 cqe->num_of_bds, tpa_info->frag_id);
919 if (unlikely(skb->len != le16_to_cpu(cqe->total_packet_len)))
921 "Strange - total packet len [cqe] is %4x but SKB has len %04x\n",
922 le16_to_cpu(cqe->total_packet_len), skb->len);
925 page_address(tpa_info->buffer.data) +
926 tpa_info->start_cqe_placement_offset +
927 tpa_info->buffer.page_offset, tpa_info->start_cqe_bd_len);
929 /* Finalize the SKB */
930 skb->protocol = eth_type_trans(skb, edev->ndev);
931 skb->ip_summed = CHECKSUM_UNNECESSARY;
933 /* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
934 * to skb_shinfo(skb)->gso_segs
936 NAPI_GRO_CB(skb)->count = le16_to_cpu(cqe->num_of_coalesced_segs);
938 qede_gro_receive(edev, fp, skb, tpa_info->vlan_tag);
940 tpa_info->state = QEDE_AGG_STATE_NONE;
944 tpa_info->state = QEDE_AGG_STATE_NONE;
945 dev_kfree_skb_any(tpa_info->skb);
946 tpa_info->skb = NULL;
950 static u8 qede_check_notunn_csum(u16 flag)
955 if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
956 PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
957 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
958 PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
959 csum = QEDE_CSUM_UNNECESSARY;
962 csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
963 PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
965 if (csum_flag & flag)
966 return QEDE_CSUM_ERROR;
971 static u8 qede_check_csum(u16 flag)
973 if (!qede_tunn_exist(flag))
974 return qede_check_notunn_csum(flag);
976 return qede_check_tunn_csum(flag);
979 static bool qede_pkt_is_ip_fragmented(struct eth_fast_path_rx_reg_cqe *cqe,
982 u8 tun_pars_flg = cqe->tunnel_pars_flags.flags;
984 if ((tun_pars_flg & (ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_MASK <<
985 ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_SHIFT)) ||
986 (flag & (PARSING_AND_ERR_FLAGS_IPV4FRAG_MASK <<
987 PARSING_AND_ERR_FLAGS_IPV4FRAG_SHIFT)))
993 /* Return true iff packet is to be passed to stack */
994 static bool qede_rx_xdp(struct qede_dev *edev,
995 struct qede_fastpath *fp,
996 struct qede_rx_queue *rxq,
997 struct bpf_prog *prog,
998 struct sw_rx_data *bd,
999 struct eth_fast_path_rx_reg_cqe *cqe,
1000 u16 *data_offset, u16 *len)
1002 struct xdp_buff xdp;
1003 enum xdp_action act;
1005 xdp.data_hard_start = page_address(bd->data);
1006 xdp.data = xdp.data_hard_start + *data_offset;
1007 xdp_set_data_meta_invalid(&xdp);
1008 xdp.data_end = xdp.data + *len;
1009 xdp.rxq = &rxq->xdp_rxq;
1011 /* Queues always have a full reset currently, so for the time
1012 * being until there's atomic program replace just mark read
1013 * side for map helpers.
1016 act = bpf_prog_run_xdp(prog, &xdp);
1019 /* Recalculate, as XDP might have changed the headers */
1020 *data_offset = xdp.data - xdp.data_hard_start;
1021 *len = xdp.data_end - xdp.data;
1023 if (act == XDP_PASS)
1026 /* Count number of packets not to be passed to stack */
1031 /* We need the replacement buffer before transmit. */
1032 if (qede_alloc_rx_buffer(rxq, true)) {
1033 qede_recycle_rx_bd_ring(rxq, 1);
1034 trace_xdp_exception(edev->ndev, prog, act);
1038 /* Now if there's a transmission problem, we'd still have to
1039 * throw current buffer, as replacement was already allocated.
1041 if (qede_xdp_xmit(edev, fp, bd, *data_offset, *len)) {
1042 dma_unmap_page(rxq->dev, bd->mapping,
1043 PAGE_SIZE, DMA_BIDIRECTIONAL);
1044 __free_page(bd->data);
1045 trace_xdp_exception(edev->ndev, prog, act);
1048 /* Regardless, we've consumed an Rx BD */
1049 qede_rx_bd_ring_consume(rxq);
1053 bpf_warn_invalid_xdp_action(act);
1055 trace_xdp_exception(edev->ndev, prog, act);
1057 qede_recycle_rx_bd_ring(rxq, cqe->bd_num);
1063 static struct sk_buff *qede_rx_allocate_skb(struct qede_dev *edev,
1064 struct qede_rx_queue *rxq,
1065 struct sw_rx_data *bd, u16 len,
1068 unsigned int offset = bd->page_offset + pad;
1069 struct skb_frag_struct *frag;
1070 struct page *page = bd->data;
1071 unsigned int pull_len;
1072 struct sk_buff *skb;
1075 /* Allocate a new SKB with a sufficient large header len */
1076 skb = netdev_alloc_skb(edev->ndev, QEDE_RX_HDR_SIZE);
1080 /* Copy data into SKB - if it's small, we can simply copy it and
1081 * re-use the already allcoated & mapped memory.
1083 if (len + pad <= edev->rx_copybreak) {
1084 skb_put_data(skb, page_address(page) + offset, len);
1085 qede_reuse_page(rxq, bd);
1089 frag = &skb_shinfo(skb)->frags[0];
1091 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1092 page, offset, len, rxq->rx_buf_seg_size);
1094 va = skb_frag_address(frag);
1095 pull_len = eth_get_headlen(va, QEDE_RX_HDR_SIZE);
1097 /* Align the pull_len to optimize memcpy */
1098 memcpy(skb->data, va, ALIGN(pull_len, sizeof(long)));
1100 /* Correct the skb & frag sizes offset after the pull */
1101 skb_frag_size_sub(frag, pull_len);
1102 frag->page_offset += pull_len;
1103 skb->data_len -= pull_len;
1104 skb->tail += pull_len;
1106 if (unlikely(qede_realloc_rx_buffer(rxq, bd))) {
1107 /* Incr page ref count to reuse on allocation failure so
1108 * that it doesn't get freed while freeing SKB [as its
1109 * already mapped there].
1112 dev_kfree_skb_any(skb);
1117 /* We've consumed the first BD and prepared an SKB */
1118 qede_rx_bd_ring_consume(rxq);
1122 static int qede_rx_build_jumbo(struct qede_dev *edev,
1123 struct qede_rx_queue *rxq,
1124 struct sk_buff *skb,
1125 struct eth_fast_path_rx_reg_cqe *cqe,
1128 u16 pkt_len = le16_to_cpu(cqe->pkt_len);
1129 struct sw_rx_data *bd;
1133 pkt_len -= first_bd_len;
1135 /* We've already used one BD for the SKB. Now take care of the rest */
1136 for (num_frags = cqe->bd_num - 1; num_frags > 0; num_frags--) {
1137 u16 cur_size = pkt_len > rxq->rx_buf_size ? rxq->rx_buf_size :
1140 if (unlikely(!cur_size)) {
1142 "Still got %d BDs for mapping jumbo, but length became 0\n",
1147 /* We need a replacement buffer for each BD */
1148 if (unlikely(qede_alloc_rx_buffer(rxq, true)))
1151 /* Now that we've allocated the replacement buffer,
1152 * we can safely consume the next BD and map it to the SKB.
1154 bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1155 bd = &rxq->sw_rx_ring[bd_cons_idx];
1156 qede_rx_bd_ring_consume(rxq);
1158 dma_unmap_page(rxq->dev, bd->mapping,
1159 PAGE_SIZE, DMA_FROM_DEVICE);
1161 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
1162 bd->data, 0, cur_size);
1164 skb->truesize += PAGE_SIZE;
1165 skb->data_len += cur_size;
1166 skb->len += cur_size;
1167 pkt_len -= cur_size;
1170 if (unlikely(pkt_len))
1172 "Mapped all BDs of jumbo, but still have %d bytes\n",
1179 static int qede_rx_process_tpa_cqe(struct qede_dev *edev,
1180 struct qede_fastpath *fp,
1181 struct qede_rx_queue *rxq,
1182 union eth_rx_cqe *cqe,
1183 enum eth_rx_cqe_type type)
1186 case ETH_RX_CQE_TYPE_TPA_START:
1187 qede_tpa_start(edev, rxq, &cqe->fast_path_tpa_start);
1189 case ETH_RX_CQE_TYPE_TPA_CONT:
1190 qede_tpa_cont(edev, rxq, &cqe->fast_path_tpa_cont);
1192 case ETH_RX_CQE_TYPE_TPA_END:
1193 return qede_tpa_end(edev, fp, &cqe->fast_path_tpa_end);
1199 static int qede_rx_process_cqe(struct qede_dev *edev,
1200 struct qede_fastpath *fp,
1201 struct qede_rx_queue *rxq)
1203 struct bpf_prog *xdp_prog = READ_ONCE(rxq->xdp_prog);
1204 struct eth_fast_path_rx_reg_cqe *fp_cqe;
1205 u16 len, pad, bd_cons_idx, parse_flag;
1206 enum eth_rx_cqe_type cqe_type;
1207 union eth_rx_cqe *cqe;
1208 struct sw_rx_data *bd;
1209 struct sk_buff *skb;
1213 /* Get the CQE from the completion ring */
1214 cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1215 cqe_type = cqe->fast_path_regular.type;
1217 /* Process an unlikely slowpath event */
1218 if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
1219 struct eth_slow_path_rx_cqe *sp_cqe;
1221 sp_cqe = (struct eth_slow_path_rx_cqe *)cqe;
1222 edev->ops->eth_cqe_completion(edev->cdev, fp->id, sp_cqe);
1226 /* Handle TPA cqes */
1227 if (cqe_type != ETH_RX_CQE_TYPE_REGULAR)
1228 return qede_rx_process_tpa_cqe(edev, fp, rxq, cqe, cqe_type);
1230 /* Get the data from the SW ring; Consume it only after it's evident
1231 * we wouldn't recycle it.
1233 bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1234 bd = &rxq->sw_rx_ring[bd_cons_idx];
1236 fp_cqe = &cqe->fast_path_regular;
1237 len = le16_to_cpu(fp_cqe->len_on_first_bd);
1238 pad = fp_cqe->placement_offset + rxq->rx_headroom;
1240 /* Run eBPF program if one is attached */
1242 if (!qede_rx_xdp(edev, fp, rxq, xdp_prog, bd, fp_cqe,
1246 /* If this is an error packet then drop it */
1247 flags = cqe->fast_path_regular.pars_flags.flags;
1248 parse_flag = le16_to_cpu(flags);
1250 csum_flag = qede_check_csum(parse_flag);
1251 if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
1252 if (qede_pkt_is_ip_fragmented(fp_cqe, parse_flag)) {
1256 "CQE has error, flags = %x, dropping incoming packet\n",
1258 rxq->rx_hw_errors++;
1259 qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
1264 /* Basic validation passed; Need to prepare an SKB. This would also
1265 * guarantee to finally consume the first BD upon success.
1267 skb = qede_rx_allocate_skb(edev, rxq, bd, len, pad);
1269 rxq->rx_alloc_errors++;
1270 qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
1274 /* In case of Jumbo packet, several PAGE_SIZEd buffers will be pointed
1277 if (fp_cqe->bd_num > 1) {
1278 u16 unmapped_frags = qede_rx_build_jumbo(edev, rxq, skb,
1281 if (unlikely(unmapped_frags > 0)) {
1282 qede_recycle_rx_bd_ring(rxq, unmapped_frags);
1283 dev_kfree_skb_any(skb);
1288 /* The SKB contains all the data. Now prepare meta-magic */
1289 skb->protocol = eth_type_trans(skb, edev->ndev);
1290 qede_get_rxhash(skb, fp_cqe->bitfields, fp_cqe->rss_hash);
1291 qede_set_skb_csum(skb, csum_flag);
1292 skb_record_rx_queue(skb, rxq->rxq_id);
1293 qede_ptp_record_rx_ts(edev, cqe, skb);
1295 /* SKB is prepared - pass it to stack */
1296 qede_skb_receive(edev, fp, rxq, skb, le16_to_cpu(fp_cqe->vlan_tag));
1301 static int qede_rx_int(struct qede_fastpath *fp, int budget)
1303 struct qede_rx_queue *rxq = fp->rxq;
1304 struct qede_dev *edev = fp->edev;
1305 int work_done = 0, rcv_pkts = 0;
1306 u16 hw_comp_cons, sw_comp_cons;
1308 hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1309 sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1311 /* Memory barrier to prevent the CPU from doing speculative reads of CQE
1312 * / BD in the while-loop before reading hw_comp_cons. If the CQE is
1313 * read before it is written by FW, then FW writes CQE and SB, and then
1314 * the CPU reads the hw_comp_cons, it will use an old CQE.
1318 /* Loop to complete all indicated BDs */
1319 while ((sw_comp_cons != hw_comp_cons) && (work_done < budget)) {
1320 rcv_pkts += qede_rx_process_cqe(edev, fp, rxq);
1321 qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1322 sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1326 rxq->rcv_pkts += rcv_pkts;
1328 /* Allocate replacement buffers */
1329 while (rxq->num_rx_buffers - rxq->filled_buffers)
1330 if (qede_alloc_rx_buffer(rxq, false))
1333 /* Update producers */
1334 qede_update_rx_prod(edev, rxq);
1339 static bool qede_poll_is_more_work(struct qede_fastpath *fp)
1341 qed_sb_update_sb_idx(fp->sb_info);
1343 /* *_has_*_work() reads the status block, thus we need to ensure that
1344 * status block indices have been actually read (qed_sb_update_sb_idx)
1345 * prior to this check (*_has_*_work) so that we won't write the
1346 * "newer" value of the status block to HW (if there was a DMA right
1347 * after qede_has_rx_work and if there is no rmb, the memory reading
1348 * (qed_sb_update_sb_idx) may be postponed to right before *_ack_sb).
1349 * In this case there will never be another interrupt until there is
1350 * another update of the status block, while there is still unhandled
1355 if (likely(fp->type & QEDE_FASTPATH_RX))
1356 if (qede_has_rx_work(fp->rxq))
1359 if (fp->type & QEDE_FASTPATH_XDP)
1360 if (qede_txq_has_work(fp->xdp_tx))
1363 if (likely(fp->type & QEDE_FASTPATH_TX))
1364 if (qede_txq_has_work(fp->txq))
1370 /*********************
1371 * NDO & API related *
1372 *********************/
1373 int qede_poll(struct napi_struct *napi, int budget)
1375 struct qede_fastpath *fp = container_of(napi, struct qede_fastpath,
1377 struct qede_dev *edev = fp->edev;
1378 int rx_work_done = 0;
1380 if (likely(fp->type & QEDE_FASTPATH_TX) && qede_txq_has_work(fp->txq))
1381 qede_tx_int(edev, fp->txq);
1383 if ((fp->type & QEDE_FASTPATH_XDP) && qede_txq_has_work(fp->xdp_tx))
1384 qede_xdp_tx_int(edev, fp->xdp_tx);
1386 rx_work_done = (likely(fp->type & QEDE_FASTPATH_RX) &&
1387 qede_has_rx_work(fp->rxq)) ?
1388 qede_rx_int(fp, budget) : 0;
1389 if (rx_work_done < budget) {
1390 if (!qede_poll_is_more_work(fp)) {
1391 napi_complete_done(napi, rx_work_done);
1393 /* Update and reenable interrupts */
1394 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1396 rx_work_done = budget;
1401 u16 xdp_prod = qed_chain_get_prod_idx(&fp->xdp_tx->tx_pbl);
1404 fp->xdp_tx->tx_db.data.bd_prod = cpu_to_le16(xdp_prod);
1405 qede_update_tx_producer(fp->xdp_tx);
1408 return rx_work_done;
1411 irqreturn_t qede_msix_fp_int(int irq, void *fp_cookie)
1413 struct qede_fastpath *fp = fp_cookie;
1415 qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
1417 napi_schedule_irqoff(&fp->napi);
1421 /* Main transmit function */
1422 netdev_tx_t qede_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1424 struct qede_dev *edev = netdev_priv(ndev);
1425 struct netdev_queue *netdev_txq;
1426 struct qede_tx_queue *txq;
1427 struct eth_tx_1st_bd *first_bd;
1428 struct eth_tx_2nd_bd *second_bd = NULL;
1429 struct eth_tx_3rd_bd *third_bd = NULL;
1430 struct eth_tx_bd *tx_data_bd = NULL;
1431 u16 txq_index, val = 0;
1434 int rc, frag_idx = 0, ipv6_ext = 0;
1438 bool data_split = false;
1440 /* Get tx-queue context and netdev index */
1441 txq_index = skb_get_queue_mapping(skb);
1442 WARN_ON(txq_index >= QEDE_TSS_COUNT(edev));
1443 txq = edev->fp_array[edev->fp_num_rx + txq_index].txq;
1444 netdev_txq = netdev_get_tx_queue(ndev, txq_index);
1446 WARN_ON(qed_chain_get_elem_left(&txq->tx_pbl) < (MAX_SKB_FRAGS + 1));
1448 xmit_type = qede_xmit_type(skb, &ipv6_ext);
1450 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
1451 if (qede_pkt_req_lin(skb, xmit_type)) {
1452 if (skb_linearize(skb)) {
1454 "SKB linearization failed - silently dropping this SKB\n");
1455 dev_kfree_skb_any(skb);
1456 return NETDEV_TX_OK;
1461 /* Fill the entry in the SW ring and the BDs in the FW ring */
1462 idx = txq->sw_tx_prod;
1463 txq->sw_tx_ring.skbs[idx].skb = skb;
1464 first_bd = (struct eth_tx_1st_bd *)
1465 qed_chain_produce(&txq->tx_pbl);
1466 memset(first_bd, 0, sizeof(*first_bd));
1467 first_bd->data.bd_flags.bitfields =
1468 1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1470 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1471 qede_ptp_tx_ts(edev, skb);
1473 /* Map skb linear data for DMA and set in the first BD */
1474 mapping = dma_map_single(txq->dev, skb->data,
1475 skb_headlen(skb), DMA_TO_DEVICE);
1476 if (unlikely(dma_mapping_error(txq->dev, mapping))) {
1477 DP_NOTICE(edev, "SKB mapping failed\n");
1478 qede_free_failed_tx_pkt(txq, first_bd, 0, false);
1479 qede_update_tx_producer(txq);
1480 return NETDEV_TX_OK;
1483 BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1485 /* In case there is IPv6 with extension headers or LSO we need 2nd and
1488 if (unlikely((xmit_type & XMIT_LSO) | ipv6_ext)) {
1489 second_bd = (struct eth_tx_2nd_bd *)
1490 qed_chain_produce(&txq->tx_pbl);
1491 memset(second_bd, 0, sizeof(*second_bd));
1494 third_bd = (struct eth_tx_3rd_bd *)
1495 qed_chain_produce(&txq->tx_pbl);
1496 memset(third_bd, 0, sizeof(*third_bd));
1499 /* We need to fill in additional data in second_bd... */
1500 tx_data_bd = (struct eth_tx_bd *)second_bd;
1503 if (skb_vlan_tag_present(skb)) {
1504 first_bd->data.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
1505 first_bd->data.bd_flags.bitfields |=
1506 1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
1509 /* Fill the parsing flags & params according to the requested offload */
1510 if (xmit_type & XMIT_L4_CSUM) {
1511 /* We don't re-calculate IP checksum as it is already done by
1514 first_bd->data.bd_flags.bitfields |=
1515 1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;
1517 if (xmit_type & XMIT_ENC) {
1518 first_bd->data.bd_flags.bitfields |=
1519 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1521 val |= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1524 /* Legacy FW had flipped behavior in regard to this bit -
1525 * I.e., needed to set to prevent FW from touching encapsulated
1526 * packets when it didn't need to.
1528 if (unlikely(txq->is_legacy))
1529 val ^= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1531 /* If the packet is IPv6 with extension header, indicate that
1532 * to FW and pass few params, since the device cracker doesn't
1533 * support parsing IPv6 with extension header/s.
1535 if (unlikely(ipv6_ext))
1536 qede_set_params_for_ipv6_ext(skb, second_bd, third_bd);
1539 if (xmit_type & XMIT_LSO) {
1540 first_bd->data.bd_flags.bitfields |=
1541 (1 << ETH_TX_1ST_BD_FLAGS_LSO_SHIFT);
1542 third_bd->data.lso_mss =
1543 cpu_to_le16(skb_shinfo(skb)->gso_size);
1545 if (unlikely(xmit_type & XMIT_ENC)) {
1546 first_bd->data.bd_flags.bitfields |=
1547 1 << ETH_TX_1ST_BD_FLAGS_TUNN_IP_CSUM_SHIFT;
1549 if (xmit_type & XMIT_ENC_GSO_L4_CSUM) {
1550 u8 tmp = ETH_TX_1ST_BD_FLAGS_TUNN_L4_CSUM_SHIFT;
1552 first_bd->data.bd_flags.bitfields |= 1 << tmp;
1554 hlen = qede_get_skb_hlen(skb, true);
1556 first_bd->data.bd_flags.bitfields |=
1557 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1558 hlen = qede_get_skb_hlen(skb, false);
1561 /* @@@TBD - if will not be removed need to check */
1562 third_bd->data.bitfields |=
1563 cpu_to_le16(1 << ETH_TX_DATA_3RD_BD_HDR_NBD_SHIFT);
1565 /* Make life easier for FW guys who can't deal with header and
1566 * data on same BD. If we need to split, use the second bd...
1568 if (unlikely(skb_headlen(skb) > hlen)) {
1569 DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1570 "TSO split header size is %d (%x:%x)\n",
1571 first_bd->nbytes, first_bd->addr.hi,
1574 mapping = HILO_U64(le32_to_cpu(first_bd->addr.hi),
1575 le32_to_cpu(first_bd->addr.lo)) +
1578 BD_SET_UNMAP_ADDR_LEN(tx_data_bd, mapping,
1579 le16_to_cpu(first_bd->nbytes) -
1582 /* this marks the BD as one that has no
1583 * individual mapping
1585 txq->sw_tx_ring.skbs[idx].flags |= QEDE_TSO_SPLIT_BD;
1587 first_bd->nbytes = cpu_to_le16(hlen);
1589 tx_data_bd = (struct eth_tx_bd *)third_bd;
1593 val |= ((skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
1594 ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT);
1597 first_bd->data.bitfields = cpu_to_le16(val);
1599 /* Handle fragmented skb */
1600 /* special handle for frags inside 2nd and 3rd bds.. */
1601 while (tx_data_bd && frag_idx < skb_shinfo(skb)->nr_frags) {
1602 rc = map_frag_to_bd(txq,
1603 &skb_shinfo(skb)->frags[frag_idx],
1606 qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1607 qede_update_tx_producer(txq);
1608 return NETDEV_TX_OK;
1611 if (tx_data_bd == (struct eth_tx_bd *)second_bd)
1612 tx_data_bd = (struct eth_tx_bd *)third_bd;
1619 /* map last frags into 4th, 5th .... */
1620 for (; frag_idx < skb_shinfo(skb)->nr_frags; frag_idx++, nbd++) {
1621 tx_data_bd = (struct eth_tx_bd *)
1622 qed_chain_produce(&txq->tx_pbl);
1624 memset(tx_data_bd, 0, sizeof(*tx_data_bd));
1626 rc = map_frag_to_bd(txq,
1627 &skb_shinfo(skb)->frags[frag_idx],
1630 qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1631 qede_update_tx_producer(txq);
1632 return NETDEV_TX_OK;
1636 /* update the first BD with the actual num BDs */
1637 first_bd->data.nbds = nbd;
1639 netdev_tx_sent_queue(netdev_txq, skb->len);
1641 skb_tx_timestamp(skb);
1643 /* Advance packet producer only before sending the packet since mapping
1644 * of pages may fail.
1646 txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
1648 /* 'next page' entries are counted in the producer value */
1649 txq->tx_db.data.bd_prod =
1650 cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
1652 if (!skb->xmit_more || netif_xmit_stopped(netdev_txq))
1653 qede_update_tx_producer(txq);
1655 if (unlikely(qed_chain_get_elem_left(&txq->tx_pbl)
1656 < (MAX_SKB_FRAGS + 1))) {
1658 qede_update_tx_producer(txq);
1660 netif_tx_stop_queue(netdev_txq);
1662 DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1663 "Stop queue was called\n");
1664 /* paired memory barrier is in qede_tx_int(), we have to keep
1665 * ordering of set_bit() in netif_tx_stop_queue() and read of
1670 if ((qed_chain_get_elem_left(&txq->tx_pbl) >=
1671 (MAX_SKB_FRAGS + 1)) &&
1672 (edev->state == QEDE_STATE_OPEN)) {
1673 netif_tx_wake_queue(netdev_txq);
1674 DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1675 "Wake queue was called\n");
1679 return NETDEV_TX_OK;
1682 /* 8B udp header + 8B base tunnel header + 32B option length */
1683 #define QEDE_MAX_TUN_HDR_LEN 48
1685 netdev_features_t qede_features_check(struct sk_buff *skb,
1686 struct net_device *dev,
1687 netdev_features_t features)
1689 if (skb->encapsulation) {
1692 switch (vlan_get_protocol(skb)) {
1693 case htons(ETH_P_IP):
1694 l4_proto = ip_hdr(skb)->protocol;
1696 case htons(ETH_P_IPV6):
1697 l4_proto = ipv6_hdr(skb)->nexthdr;
1703 /* Disable offloads for geneve tunnels, as HW can't parse
1704 * the geneve header which has option length greater than 32b
1705 * and disable offloads for the ports which are not offloaded.
1707 if (l4_proto == IPPROTO_UDP) {
1708 struct qede_dev *edev = netdev_priv(dev);
1709 u16 hdrlen, vxln_port, gnv_port;
1711 hdrlen = QEDE_MAX_TUN_HDR_LEN;
1712 vxln_port = edev->vxlan_dst_port;
1713 gnv_port = edev->geneve_dst_port;
1715 if ((skb_inner_mac_header(skb) -
1716 skb_transport_header(skb)) > hdrlen ||
1717 (ntohs(udp_hdr(skb)->dest) != vxln_port &&
1718 ntohs(udp_hdr(skb)->dest) != gnv_port))
1719 return features & ~(NETIF_F_CSUM_MASK |