1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/ip6_checksum.h>
8 #include <net/netdev_queues.h>
9 #include <net/page_pool/helpers.h>
12 #include "ionic_lif.h"
13 #include "ionic_txrx.h"
15 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
16 void *data, size_t len);
18 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
19 const skb_frag_t *frag,
20 size_t offset, size_t len);
22 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
23 struct ionic_tx_desc_info *desc_info);
25 static void ionic_tx_clean(struct ionic_queue *q,
26 struct ionic_tx_desc_info *desc_info,
27 struct ionic_txq_comp *comp,
30 static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell)
32 ionic_q_post(q, ring_dbell);
35 static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell)
37 ionic_q_post(q, ring_dbell);
40 bool ionic_txq_poke_doorbell(struct ionic_queue *q)
42 struct netdev_queue *netdev_txq;
43 unsigned long now, then, dif;
44 struct net_device *netdev;
46 netdev = q->lif->netdev;
47 netdev_txq = netdev_get_tx_queue(netdev, q->index);
49 HARD_TX_LOCK(netdev, netdev_txq, smp_processor_id());
51 if (q->tail_idx == q->head_idx) {
52 HARD_TX_UNLOCK(netdev, netdev_txq);
56 now = READ_ONCE(jiffies);
57 then = q->dbell_jiffies;
60 if (dif > q->dbell_deadline) {
61 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
62 q->dbval | q->head_idx);
64 q->dbell_jiffies = now;
67 HARD_TX_UNLOCK(netdev, netdev_txq);
72 bool ionic_rxq_poke_doorbell(struct ionic_queue *q)
74 unsigned long now, then, dif;
76 /* no lock, called from rx napi or txrx napi, nothing else can fill */
78 if (q->tail_idx == q->head_idx)
81 now = READ_ONCE(jiffies);
82 then = q->dbell_jiffies;
85 if (dif > q->dbell_deadline) {
86 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
87 q->dbval | q->head_idx);
89 q->dbell_jiffies = now;
91 dif = 2 * q->dbell_deadline;
92 if (dif > IONIC_RX_MAX_DOORBELL_DEADLINE)
93 dif = IONIC_RX_MAX_DOORBELL_DEADLINE;
95 q->dbell_deadline = dif;
101 static inline struct ionic_txq_sg_elem *ionic_tx_sg_elems(struct ionic_queue *q)
103 if (likely(q->sg_desc_size == sizeof(struct ionic_txq_sg_desc_v1)))
104 return q->txq_sgl_v1[q->head_idx].elems;
106 return q->txq_sgl[q->head_idx].elems;
109 static inline struct netdev_queue *q_to_ndq(struct net_device *netdev,
110 struct ionic_queue *q)
112 return netdev_get_tx_queue(netdev, q->index);
115 static void *ionic_rx_buf_va(struct ionic_buf_info *buf_info)
117 return page_address(buf_info->page) + buf_info->page_offset;
120 static dma_addr_t ionic_rx_buf_pa(struct ionic_buf_info *buf_info)
122 return page_pool_get_dma_addr(buf_info->page) + buf_info->page_offset;
125 static void __ionic_rx_put_buf(struct ionic_queue *q,
126 struct ionic_buf_info *buf_info,
132 page_pool_put_full_page(q->page_pool, buf_info->page, recycle_direct);
133 buf_info->page = NULL;
135 buf_info->page_offset = 0;
139 static void ionic_rx_put_buf(struct ionic_queue *q,
140 struct ionic_buf_info *buf_info)
142 __ionic_rx_put_buf(q, buf_info, false);
145 static void ionic_rx_put_buf_direct(struct ionic_queue *q,
146 struct ionic_buf_info *buf_info)
148 __ionic_rx_put_buf(q, buf_info, true);
151 static void ionic_rx_add_skb_frag(struct ionic_queue *q,
153 struct ionic_buf_info *buf_info,
154 u32 headroom, u32 len,
158 page_pool_dma_sync_for_cpu(q->page_pool,
160 buf_info->page_offset + headroom,
163 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
164 buf_info->page, buf_info->page_offset + headroom,
167 /* napi_gro_frags() will release/recycle the
168 * page_pool buffers from the frags list
170 buf_info->page = NULL;
172 buf_info->page_offset = 0;
175 static struct sk_buff *ionic_rx_build_skb(struct ionic_queue *q,
176 struct ionic_rx_desc_info *desc_info,
177 unsigned int headroom,
179 unsigned int num_sg_elems,
182 struct ionic_buf_info *buf_info;
187 buf_info = &desc_info->bufs[0];
188 prefetchw(buf_info->page);
190 skb = napi_get_frags(&q_to_qcq(q)->napi);
191 if (unlikely(!skb)) {
192 net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
193 dev_name(q->dev), q->name);
194 q_to_rx_stats(q)->alloc_err++;
197 skb_mark_for_recycle(skb);
200 frag_len = min_t(u16, len,
201 IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
203 frag_len = min_t(u16, len, IONIC_PAGE_SIZE);
205 if (unlikely(!buf_info->page))
206 goto err_bad_buf_page;
207 ionic_rx_add_skb_frag(q, skb, buf_info, headroom, frag_len, synced);
211 for (i = 0; i < num_sg_elems; i++, buf_info++) {
212 if (unlikely(!buf_info->page))
213 goto err_bad_buf_page;
214 frag_len = min_t(u16, len, buf_info->len);
215 ionic_rx_add_skb_frag(q, skb, buf_info, 0, frag_len, synced);
226 static struct sk_buff *ionic_rx_copybreak(struct net_device *netdev,
227 struct ionic_queue *q,
228 struct ionic_rx_desc_info *desc_info,
229 unsigned int headroom,
231 unsigned int num_sg_elems,
234 struct ionic_buf_info *buf_info;
235 struct device *dev = q->dev;
239 buf_info = &desc_info->bufs[0];
241 skb = napi_alloc_skb(&q_to_qcq(q)->napi, len);
242 if (unlikely(!skb)) {
243 net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
244 dev_name(dev), q->name);
245 q_to_rx_stats(q)->alloc_err++;
248 skb_mark_for_recycle(skb);
251 page_pool_dma_sync_for_cpu(q->page_pool,
253 buf_info->page_offset + headroom,
256 skb_copy_to_linear_data(skb, ionic_rx_buf_va(buf_info) + headroom, len);
259 skb->protocol = eth_type_trans(skb, netdev);
261 /* recycle the Rx buffer now that we're done with it */
262 ionic_rx_put_buf_direct(q, buf_info);
264 for (i = 0; i < num_sg_elems; i++, buf_info++)
265 ionic_rx_put_buf_direct(q, buf_info);
270 static void ionic_xdp_tx_desc_clean(struct ionic_queue *q,
271 struct ionic_tx_desc_info *desc_info,
274 struct xdp_frame_bulk bq;
276 if (!desc_info->nbufs)
279 xdp_frame_bulk_init(&bq);
280 rcu_read_lock(); /* need for xdp_return_frame_bulk */
282 if (desc_info->act == XDP_TX) {
284 xdp_return_frame_rx_napi(desc_info->xdpf);
286 xdp_return_frame(desc_info->xdpf);
287 } else if (desc_info->act == XDP_REDIRECT) {
288 ionic_tx_desc_unmap_bufs(q, desc_info);
289 xdp_return_frame_bulk(desc_info->xdpf, &bq);
292 xdp_flush_frame_bulk(&bq);
295 desc_info->nbufs = 0;
296 desc_info->xdpf = NULL;
300 static int ionic_xdp_post_frame(struct ionic_queue *q, struct xdp_frame *frame,
301 enum xdp_action act, struct page *page, int off,
304 struct ionic_tx_desc_info *desc_info;
305 struct ionic_buf_info *buf_info;
306 struct ionic_tx_stats *stats;
307 struct ionic_txq_desc *desc;
308 size_t len = frame->len;
312 desc_info = &q->tx_info[q->head_idx];
313 desc = &q->txq[q->head_idx];
314 buf_info = desc_info->bufs;
315 stats = q_to_tx_stats(q);
318 dma_addr = page_pool_get_dma_addr(page) +
319 off + XDP_PACKET_HEADROOM;
320 dma_sync_single_for_device(q->dev, dma_addr,
322 } else /* XDP_REDIRECT */ {
323 dma_addr = ionic_tx_map_single(q, frame->data, len);
328 buf_info->dma_addr = dma_addr;
330 buf_info->page = page;
331 buf_info->page_offset = off;
333 desc_info->nbufs = 1;
334 desc_info->xdpf = frame;
335 desc_info->act = act;
337 if (xdp_frame_has_frags(frame)) {
338 struct ionic_txq_sg_elem *elem;
339 struct skb_shared_info *sinfo;
340 struct ionic_buf_info *bi;
345 sinfo = xdp_get_shared_info_from_frame(frame);
347 elem = ionic_tx_sg_elems(q);
348 for (i = 0; i < sinfo->nr_frags; i++, frag++, bi++) {
350 struct page *pg = skb_frag_page(frag);
352 dma_addr = page_pool_get_dma_addr(pg) +
354 dma_sync_single_for_device(q->dev, dma_addr,
358 dma_addr = ionic_tx_map_frag(q, frag, 0,
359 skb_frag_size(frag));
360 if (dma_mapping_error(q->dev, dma_addr)) {
361 ionic_tx_desc_unmap_bufs(q, desc_info);
365 bi->dma_addr = dma_addr;
366 bi->len = skb_frag_size(frag);
367 bi->page = skb_frag_page(frag);
369 elem->addr = cpu_to_le64(bi->dma_addr);
370 elem->len = cpu_to_le16(bi->len);
377 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
378 0, (desc_info->nbufs - 1), buf_info->dma_addr);
379 desc->cmd = cpu_to_le64(cmd);
380 desc->len = cpu_to_le16(len);
381 desc->csum_start = 0;
382 desc->csum_offset = 0;
388 ionic_txq_post(q, ring_doorbell);
393 int ionic_xdp_xmit(struct net_device *netdev, int n,
394 struct xdp_frame **xdp_frames, u32 flags)
396 struct ionic_lif *lif = netdev_priv(netdev);
397 struct ionic_queue *txq;
398 struct netdev_queue *nq;
404 if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state)))
407 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
410 /* AdminQ is assumed on cpu 0, while we attempt to affinitize the
411 * TxRx queue pairs 0..n-1 on cpus 1..n. We try to keep with that
412 * affinitization here, but of course irqbalance and friends might
413 * have juggled things anyway, so we have to check for the 0 case.
415 cpu = smp_processor_id();
416 qi = cpu ? (cpu - 1) % lif->nxqs : cpu;
418 txq = &lif->txqcqs[qi]->q;
419 nq = netdev_get_tx_queue(netdev, txq->index);
420 __netif_tx_lock(nq, cpu);
421 txq_trans_cond_update(nq);
423 if (netif_tx_queue_stopped(nq) ||
424 !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
425 ionic_q_space_avail(txq),
427 __netif_tx_unlock(nq);
431 space = min_t(int, n, ionic_q_space_avail(txq));
432 for (nxmit = 0; nxmit < space ; nxmit++) {
433 if (ionic_xdp_post_frame(txq, xdp_frames[nxmit],
435 virt_to_page(xdp_frames[nxmit]->data),
442 if (flags & XDP_XMIT_FLUSH)
443 ionic_dbell_ring(lif->kern_dbpage, txq->hw_type,
444 txq->dbval | txq->head_idx);
446 netif_txq_maybe_stop(q_to_ndq(netdev, txq),
447 ionic_q_space_avail(txq),
449 __netif_tx_unlock(nq);
454 static void ionic_xdp_rx_unlink_bufs(struct ionic_queue *q,
455 struct ionic_buf_info *buf_info,
460 for (i = 0; i < nbufs; i++) {
461 buf_info->page = NULL;
466 static bool ionic_run_xdp(struct ionic_rx_stats *stats,
467 struct net_device *netdev,
468 struct bpf_prog *xdp_prog,
469 struct ionic_queue *rxq,
470 struct ionic_buf_info *buf_info,
473 u32 xdp_action = XDP_ABORTED;
474 struct xdp_buff xdp_buf;
475 struct ionic_queue *txq;
476 struct netdev_queue *nq;
477 struct xdp_frame *xdpf;
483 xdp_init_buff(&xdp_buf, IONIC_PAGE_SIZE, rxq->xdp_rxq_info);
484 frag_len = min_t(u16, len, IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
485 xdp_prepare_buff(&xdp_buf, ionic_rx_buf_va(buf_info),
486 XDP_PACKET_HEADROOM, frag_len, false);
487 page_pool_dma_sync_for_cpu(rxq->page_pool, buf_info->page,
488 buf_info->page_offset + XDP_PACKET_HEADROOM,
490 prefetchw(&xdp_buf.data_hard_start);
492 /* We limit MTU size to one buffer if !xdp_has_frags, so
493 * if the recv len is bigger than one buffer
494 * then we know we have frag info to gather
496 remain_len = len - frag_len;
498 struct skb_shared_info *sinfo;
499 struct ionic_buf_info *bi;
503 sinfo = xdp_get_shared_info_from_buff(&xdp_buf);
505 sinfo->xdp_frags_size = 0;
506 xdp_buff_set_frags_flag(&xdp_buf);
509 if (unlikely(sinfo->nr_frags >= MAX_SKB_FRAGS)) {
514 frag = &sinfo->frags[sinfo->nr_frags];
517 frag_len = min_t(u16, remain_len, bi->len);
518 page_pool_dma_sync_for_cpu(rxq->page_pool, bi->page,
519 buf_info->page_offset,
521 skb_frag_fill_page_desc(frag, bi->page, 0, frag_len);
522 sinfo->xdp_frags_size += frag_len;
523 remain_len -= frag_len;
525 if (page_is_pfmemalloc(bi->page))
526 xdp_buff_set_frag_pfmemalloc(&xdp_buf);
527 } while (remain_len > 0);
528 nbufs += sinfo->nr_frags;
531 xdp_action = bpf_prog_run_xdp(xdp_prog, &xdp_buf);
533 switch (xdp_action) {
536 return false; /* false = we didn't consume the packet */
539 ionic_rx_put_buf_direct(rxq, buf_info);
544 xdpf = xdp_convert_buff_to_frame(&xdp_buf);
551 nq = netdev_get_tx_queue(netdev, txq->index);
552 __netif_tx_lock(nq, smp_processor_id());
553 txq_trans_cond_update(nq);
555 if (netif_tx_queue_stopped(nq) ||
556 !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
557 ionic_q_space_avail(txq),
559 __netif_tx_unlock(nq);
564 err = ionic_xdp_post_frame(txq, xdpf, XDP_TX,
566 buf_info->page_offset,
568 __netif_tx_unlock(nq);
570 netdev_dbg(netdev, "tx ionic_xdp_post_frame err %d\n", err);
573 ionic_xdp_rx_unlink_bufs(rxq, buf_info, nbufs);
578 err = xdp_do_redirect(netdev, &xdp_buf, xdp_prog);
580 netdev_dbg(netdev, "xdp_do_redirect err %d\n", err);
583 ionic_xdp_rx_unlink_bufs(rxq, buf_info, nbufs);
584 rxq->xdp_flush = true;
585 stats->xdp_redirect++;
595 ionic_rx_put_buf_direct(rxq, buf_info);
596 trace_xdp_exception(netdev, xdp_prog, xdp_action);
597 stats->xdp_aborted++;
603 static void ionic_rx_clean(struct ionic_queue *q,
604 struct ionic_rx_desc_info *desc_info,
605 struct ionic_rxq_comp *comp,
606 struct bpf_prog *xdp_prog)
608 struct net_device *netdev = q->lif->netdev;
609 struct ionic_qcq *qcq = q_to_qcq(q);
610 struct ionic_rx_stats *stats;
611 unsigned int headroom = 0;
617 stats = q_to_rx_stats(q);
619 if (unlikely(comp->status)) {
620 /* Most likely status==2 and the pkt received was bigger
621 * than the buffer available: comp->len will show the
622 * pkt size received that didn't fit the advertised desc.len
624 dev_dbg(q->dev, "q%d drop comp->status %d comp->len %d desc->len %d\n",
625 q->index, comp->status, comp->len, q->rxq[q->head_idx].len);
631 len = le16_to_cpu(comp->len);
636 if (ionic_run_xdp(stats, netdev, xdp_prog, q, desc_info->bufs, len))
639 headroom = XDP_PACKET_HEADROOM;
642 use_copybreak = len <= q->lif->rx_copybreak;
644 skb = ionic_rx_copybreak(netdev, q, desc_info,
646 comp->num_sg_elems, synced);
648 skb = ionic_rx_build_skb(q, desc_info, headroom, len,
649 comp->num_sg_elems, synced);
651 if (unlikely(!skb)) {
656 skb_record_rx_queue(skb, q->index);
658 if (likely(netdev->features & NETIF_F_RXHASH)) {
659 switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) {
660 case IONIC_PKT_TYPE_IPV4:
661 case IONIC_PKT_TYPE_IPV6:
662 skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
665 case IONIC_PKT_TYPE_IPV4_TCP:
666 case IONIC_PKT_TYPE_IPV6_TCP:
667 case IONIC_PKT_TYPE_IPV4_UDP:
668 case IONIC_PKT_TYPE_IPV6_UDP:
669 skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
675 if (likely(netdev->features & NETIF_F_RXCSUM) &&
676 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC)) {
677 skb->ip_summed = CHECKSUM_COMPLETE;
678 skb->csum = (__force __wsum)le16_to_cpu(comp->csum);
679 stats->csum_complete++;
684 if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) ||
685 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) ||
686 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD)))
689 if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
690 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) {
691 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
692 le16_to_cpu(comp->vlan_tci));
693 stats->vlan_stripped++;
696 if (unlikely(q->features & IONIC_RXQ_F_HWSTAMP)) {
697 __le64 *cq_desc_hwstamp;
703 sizeof(struct ionic_rxq_comp) -
704 IONIC_HWSTAMP_CQ_NEGOFFSET;
706 hwstamp = le64_to_cpu(*cq_desc_hwstamp);
708 if (hwstamp != IONIC_HWSTAMP_INVALID) {
709 skb_hwtstamps(skb)->hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
710 stats->hwstamp_valid++;
712 stats->hwstamp_invalid++;
717 napi_gro_receive(&qcq->napi, skb);
719 napi_gro_frags(&qcq->napi);
722 static bool __ionic_rx_service(struct ionic_cq *cq, struct bpf_prog *xdp_prog)
724 struct ionic_rx_desc_info *desc_info;
725 struct ionic_queue *q = cq->bound_q;
726 struct ionic_rxq_comp *comp;
728 comp = &((struct ionic_rxq_comp *)cq->base)[cq->tail_idx];
730 if (!color_match(comp->pkt_type_color, cq->done_color))
733 /* check for empty queue */
734 if (q->tail_idx == q->head_idx)
737 if (q->tail_idx != le16_to_cpu(comp->comp_index))
740 desc_info = &q->rx_info[q->tail_idx];
741 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
743 /* clean the related q entry, only one per qc completion */
744 ionic_rx_clean(q, desc_info, comp, xdp_prog);
749 bool ionic_rx_service(struct ionic_cq *cq)
751 return __ionic_rx_service(cq, NULL);
754 static inline void ionic_write_cmb_desc(struct ionic_queue *q,
757 /* Since Rx and Tx descriptors are the same size, we can
758 * save an instruction or two and skip the qtype check.
760 if (unlikely(q_to_qcq(q)->flags & IONIC_QCQ_F_CMB_RINGS))
761 memcpy_toio(&q->cmb_txq[q->head_idx], desc, sizeof(q->cmb_txq[0]));
764 void ionic_rx_fill(struct ionic_queue *q, struct bpf_prog *xdp_prog)
766 struct net_device *netdev = q->lif->netdev;
767 struct ionic_rx_desc_info *desc_info;
768 struct ionic_rxq_sg_elem *sg_elem;
769 struct ionic_buf_info *buf_info;
770 unsigned int fill_threshold;
771 struct ionic_rxq_desc *desc;
772 unsigned int first_frag_len;
773 unsigned int first_buf_len;
774 unsigned int headroom = 0;
775 unsigned int remain_len;
776 unsigned int frag_len;
783 n_fill = ionic_q_space_avail(q);
785 fill_threshold = min_t(unsigned int, IONIC_RX_FILL_THRESHOLD,
786 q->num_descs / IONIC_RX_FILL_DIV);
787 if (n_fill < fill_threshold)
790 len = netdev->mtu + VLAN_ETH_HLEN;
793 /* Always alloc the full size buffer, but only need
794 * the actual frag_len in the descriptor
795 * XDP uses space in the first buffer, so account for
796 * head room, tail room, and ip header in the first frag size.
798 headroom = XDP_PACKET_HEADROOM;
799 first_buf_len = IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN + headroom;
800 first_frag_len = min_t(u16, len + headroom, first_buf_len);
802 /* Use MTU size if smaller than max buffer size */
803 first_frag_len = min_t(u16, len, IONIC_PAGE_SIZE);
804 first_buf_len = first_frag_len;
807 for (i = n_fill; i; i--) {
808 /* fill main descriptor - buf[0] */
811 desc = &q->rxq[q->head_idx];
812 desc_info = &q->rx_info[q->head_idx];
813 buf_info = &desc_info->bufs[0];
815 buf_info->len = first_buf_len;
816 frag_len = first_frag_len - headroom;
818 /* get a new buffer if we can't reuse one */
820 buf_info->page = page_pool_alloc(q->page_pool,
821 &buf_info->page_offset,
824 if (unlikely(!buf_info->page)) {
829 desc->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info) + headroom);
830 desc->len = cpu_to_le16(frag_len);
831 remain_len -= frag_len;
835 /* fill sg descriptors - buf[1..n] */
836 sg_elem = q->rxq_sgl[q->head_idx].elems;
837 for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++, sg_elem++) {
838 frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE);
840 /* Recycle any leftover buffers that are too small to reuse */
841 if (unlikely(buf_info->page && buf_info->len < frag_len))
842 ionic_rx_put_buf_direct(q, buf_info);
844 /* Get new buffer if needed */
845 if (!buf_info->page) {
846 buf_info->len = frag_len;
847 buf_info->page = page_pool_alloc(q->page_pool,
848 &buf_info->page_offset,
851 if (unlikely(!buf_info->page)) {
857 sg_elem->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info));
858 sg_elem->len = cpu_to_le16(frag_len);
859 remain_len -= frag_len;
864 /* clear end sg element as a sentinel */
865 if (j < q->max_sg_elems)
866 memset(sg_elem, 0, sizeof(*sg_elem));
868 desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG :
869 IONIC_RXQ_DESC_OPCODE_SIMPLE;
870 desc_info->nbufs = nfrags;
872 ionic_write_cmb_desc(q, desc);
874 ionic_rxq_post(q, false);
877 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
878 q->dbval | q->head_idx);
880 q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
881 q->dbell_jiffies = jiffies;
884 void ionic_rx_empty(struct ionic_queue *q)
886 struct ionic_rx_desc_info *desc_info;
889 for (i = 0; i < q->num_descs; i++) {
890 desc_info = &q->rx_info[i];
891 for (j = 0; j < ARRAY_SIZE(desc_info->bufs); j++)
892 ionic_rx_put_buf(q, &desc_info->bufs[j]);
893 desc_info->nbufs = 0;
900 static void ionic_dim_update(struct ionic_qcq *qcq, int napi_mode)
902 struct dim_sample dim_sample;
903 struct ionic_lif *lif;
907 if (!qcq->intr.dim_coal_hw)
911 qi = qcq->cq.bound_q->index;
914 case IONIC_LIF_F_TX_DIM_INTR:
915 pkts = lif->txqstats[qi].pkts;
916 bytes = lif->txqstats[qi].bytes;
918 case IONIC_LIF_F_RX_DIM_INTR:
919 pkts = lif->rxqstats[qi].pkts;
920 bytes = lif->rxqstats[qi].bytes;
923 pkts = lif->txqstats[qi].pkts + lif->rxqstats[qi].pkts;
924 bytes = lif->txqstats[qi].bytes + lif->rxqstats[qi].bytes;
928 dim_update_sample(qcq->cq.bound_intr->rearm_count,
929 pkts, bytes, &dim_sample);
931 net_dim(&qcq->dim, &dim_sample);
934 int ionic_tx_napi(struct napi_struct *napi, int budget)
936 struct ionic_qcq *qcq = napi_to_qcq(napi);
937 struct ionic_cq *cq = napi_to_cq(napi);
941 work_done = ionic_tx_cq_service(cq, budget, !!budget);
943 if (unlikely(!budget))
946 if (work_done < budget && napi_complete_done(napi, work_done)) {
947 ionic_dim_update(qcq, IONIC_LIF_F_TX_DIM_INTR);
948 flags |= IONIC_INTR_CRED_UNMASK;
949 cq->bound_intr->rearm_count++;
952 if (work_done || flags) {
953 flags |= IONIC_INTR_CRED_RESET_COALESCE;
954 ionic_intr_credits(cq->idev->intr_ctrl,
955 cq->bound_intr->index,
959 if (!work_done && cq->bound_q->lif->doorbell_wa)
960 ionic_txq_poke_doorbell(&qcq->q);
965 static void ionic_xdp_do_flush(struct ionic_cq *cq)
967 if (cq->bound_q->xdp_flush) {
969 cq->bound_q->xdp_flush = false;
973 static unsigned int ionic_rx_cq_service(struct ionic_cq *cq,
974 unsigned int work_to_do)
976 struct ionic_queue *q = cq->bound_q;
977 unsigned int work_done = 0;
978 struct bpf_prog *xdp_prog;
983 xdp_prog = READ_ONCE(q->xdp_prog);
984 while (__ionic_rx_service(cq, xdp_prog)) {
985 if (cq->tail_idx == cq->num_descs - 1)
986 cq->done_color = !cq->done_color;
988 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
990 if (++work_done >= work_to_do)
993 ionic_rx_fill(q, xdp_prog);
994 ionic_xdp_do_flush(cq);
999 int ionic_rx_napi(struct napi_struct *napi, int budget)
1001 struct ionic_qcq *qcq = napi_to_qcq(napi);
1002 struct ionic_cq *cq = napi_to_cq(napi);
1006 if (unlikely(!budget))
1009 work_done = ionic_rx_cq_service(cq, budget);
1011 if (work_done < budget && napi_complete_done(napi, work_done)) {
1012 ionic_dim_update(qcq, IONIC_LIF_F_RX_DIM_INTR);
1013 flags |= IONIC_INTR_CRED_UNMASK;
1014 cq->bound_intr->rearm_count++;
1017 if (work_done || flags) {
1018 flags |= IONIC_INTR_CRED_RESET_COALESCE;
1019 ionic_intr_credits(cq->idev->intr_ctrl,
1020 cq->bound_intr->index,
1024 if (!work_done && cq->bound_q->lif->doorbell_wa)
1025 ionic_rxq_poke_doorbell(&qcq->q);
1030 int ionic_txrx_napi(struct napi_struct *napi, int budget)
1032 struct ionic_qcq *rxqcq = napi_to_qcq(napi);
1033 struct ionic_cq *rxcq = napi_to_cq(napi);
1034 unsigned int qi = rxcq->bound_q->index;
1035 struct ionic_qcq *txqcq;
1036 struct ionic_lif *lif;
1037 struct ionic_cq *txcq;
1038 u32 rx_work_done = 0;
1039 u32 tx_work_done = 0;
1042 lif = rxcq->bound_q->lif;
1043 txqcq = lif->txqcqs[qi];
1044 txcq = &lif->txqcqs[qi]->cq;
1046 tx_work_done = ionic_tx_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT, !!budget);
1048 if (unlikely(!budget))
1051 rx_work_done = ionic_rx_cq_service(rxcq, budget);
1053 if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) {
1054 ionic_dim_update(rxqcq, 0);
1055 flags |= IONIC_INTR_CRED_UNMASK;
1056 rxcq->bound_intr->rearm_count++;
1059 if (rx_work_done || flags) {
1060 flags |= IONIC_INTR_CRED_RESET_COALESCE;
1061 ionic_intr_credits(rxcq->idev->intr_ctrl, rxcq->bound_intr->index,
1062 tx_work_done + rx_work_done, flags);
1065 if (lif->doorbell_wa) {
1067 ionic_rxq_poke_doorbell(&rxqcq->q);
1069 ionic_txq_poke_doorbell(&txqcq->q);
1072 return rx_work_done;
1075 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
1076 void *data, size_t len)
1078 struct device *dev = q->dev;
1079 dma_addr_t dma_addr;
1081 dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE);
1082 if (unlikely(dma_mapping_error(dev, dma_addr))) {
1083 net_warn_ratelimited("%s: DMA single map failed on %s!\n",
1084 dev_name(dev), q->name);
1085 q_to_tx_stats(q)->dma_map_err++;
1091 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
1092 const skb_frag_t *frag,
1093 size_t offset, size_t len)
1095 struct device *dev = q->dev;
1096 dma_addr_t dma_addr;
1098 dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE);
1099 if (unlikely(dma_mapping_error(dev, dma_addr))) {
1100 net_warn_ratelimited("%s: DMA frag map failed on %s!\n",
1101 dev_name(dev), q->name);
1102 q_to_tx_stats(q)->dma_map_err++;
1108 static int ionic_tx_map_skb(struct ionic_queue *q, struct sk_buff *skb,
1109 struct ionic_tx_desc_info *desc_info)
1111 struct ionic_buf_info *buf_info = desc_info->bufs;
1112 struct device *dev = q->dev;
1113 dma_addr_t dma_addr;
1114 unsigned int nfrags;
1118 dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb));
1121 buf_info->dma_addr = dma_addr;
1122 buf_info->len = skb_headlen(skb);
1125 frag = skb_shinfo(skb)->frags;
1126 nfrags = skb_shinfo(skb)->nr_frags;
1127 for (frag_idx = 0; frag_idx < nfrags; frag_idx++, frag++) {
1128 dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
1131 buf_info->dma_addr = dma_addr;
1132 buf_info->len = skb_frag_size(frag);
1136 desc_info->nbufs = 1 + nfrags;
1141 /* unwind the frag mappings and the head mapping */
1142 while (frag_idx > 0) {
1145 dma_unmap_page(dev, buf_info->dma_addr,
1146 buf_info->len, DMA_TO_DEVICE);
1148 dma_unmap_single(dev, desc_info->bufs[0].dma_addr,
1149 desc_info->bufs[0].len, DMA_TO_DEVICE);
1153 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
1154 struct ionic_tx_desc_info *desc_info)
1156 struct ionic_buf_info *buf_info = desc_info->bufs;
1157 struct device *dev = q->dev;
1160 if (!desc_info->nbufs)
1163 dma_unmap_single(dev, buf_info->dma_addr,
1164 buf_info->len, DMA_TO_DEVICE);
1166 for (i = 1; i < desc_info->nbufs; i++, buf_info++)
1167 dma_unmap_page(dev, buf_info->dma_addr,
1168 buf_info->len, DMA_TO_DEVICE);
1170 desc_info->nbufs = 0;
1173 static void ionic_tx_clean(struct ionic_queue *q,
1174 struct ionic_tx_desc_info *desc_info,
1175 struct ionic_txq_comp *comp,
1178 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1179 struct ionic_qcq *qcq = q_to_qcq(q);
1180 struct sk_buff *skb;
1182 if (desc_info->xdpf) {
1183 ionic_xdp_tx_desc_clean(q->partner, desc_info, in_napi);
1186 if (unlikely(__netif_subqueue_stopped(q->lif->netdev, q->index)))
1187 netif_wake_subqueue(q->lif->netdev, q->index);
1192 ionic_tx_desc_unmap_bufs(q, desc_info);
1194 skb = desc_info->skb;
1198 if (unlikely(ionic_txq_hwstamp_enabled(q))) {
1200 struct skb_shared_hwtstamps hwts = {};
1201 __le64 *cq_desc_hwstamp;
1207 sizeof(struct ionic_txq_comp) -
1208 IONIC_HWSTAMP_CQ_NEGOFFSET;
1210 hwstamp = le64_to_cpu(*cq_desc_hwstamp);
1212 if (hwstamp != IONIC_HWSTAMP_INVALID) {
1213 hwts.hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
1215 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1216 skb_tstamp_tx(skb, &hwts);
1218 stats->hwstamp_valid++;
1220 stats->hwstamp_invalid++;
1225 desc_info->bytes = skb->len;
1228 napi_consume_skb(skb, likely(in_napi) ? 1 : 0);
1231 static bool ionic_tx_service(struct ionic_cq *cq,
1232 unsigned int *total_pkts,
1233 unsigned int *total_bytes,
1236 struct ionic_tx_desc_info *desc_info;
1237 struct ionic_queue *q = cq->bound_q;
1238 struct ionic_txq_comp *comp;
1239 unsigned int bytes = 0;
1240 unsigned int pkts = 0;
1243 comp = &((struct ionic_txq_comp *)cq->base)[cq->tail_idx];
1245 if (!color_match(comp->color, cq->done_color))
1248 /* clean the related q entries, there could be
1249 * several q entries completed for each cq completion
1252 desc_info = &q->tx_info[q->tail_idx];
1253 desc_info->bytes = 0;
1254 index = q->tail_idx;
1255 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1256 ionic_tx_clean(q, desc_info, comp, in_napi);
1257 if (desc_info->skb) {
1259 bytes += desc_info->bytes;
1260 desc_info->skb = NULL;
1262 } while (index != le16_to_cpu(comp->comp_index));
1264 (*total_pkts) += pkts;
1265 (*total_bytes) += bytes;
1270 unsigned int ionic_tx_cq_service(struct ionic_cq *cq,
1271 unsigned int work_to_do,
1274 unsigned int work_done = 0;
1275 unsigned int bytes = 0;
1276 unsigned int pkts = 0;
1278 if (work_to_do == 0)
1281 while (ionic_tx_service(cq, &pkts, &bytes, in_napi)) {
1282 if (cq->tail_idx == cq->num_descs - 1)
1283 cq->done_color = !cq->done_color;
1284 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
1286 if (++work_done >= work_to_do)
1291 struct ionic_queue *q = cq->bound_q;
1293 if (likely(!ionic_txq_hwstamp_enabled(q)))
1294 netif_txq_completed_wake(q_to_ndq(q->lif->netdev, q),
1296 ionic_q_space_avail(q),
1297 IONIC_TSO_DESCS_NEEDED);
1303 void ionic_tx_flush(struct ionic_cq *cq)
1307 work_done = ionic_tx_cq_service(cq, cq->num_descs, false);
1309 ionic_intr_credits(cq->idev->intr_ctrl, cq->bound_intr->index,
1310 work_done, IONIC_INTR_CRED_RESET_COALESCE);
1313 void ionic_tx_empty(struct ionic_queue *q)
1315 struct ionic_tx_desc_info *desc_info;
1319 /* walk the not completed tx entries, if any */
1320 while (q->head_idx != q->tail_idx) {
1321 desc_info = &q->tx_info[q->tail_idx];
1322 desc_info->bytes = 0;
1323 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1324 ionic_tx_clean(q, desc_info, NULL, false);
1325 if (desc_info->skb) {
1327 bytes += desc_info->bytes;
1328 desc_info->skb = NULL;
1332 if (likely(!ionic_txq_hwstamp_enabled(q))) {
1333 struct netdev_queue *ndq = q_to_ndq(q->lif->netdev, q);
1335 netdev_tx_completed_queue(ndq, pkts, bytes);
1336 netdev_tx_reset_queue(ndq);
1340 static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb)
1344 err = skb_cow_head(skb, 0);
1348 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1349 inner_ip_hdr(skb)->check = 0;
1350 inner_tcp_hdr(skb)->check =
1351 ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
1352 inner_ip_hdr(skb)->daddr,
1354 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1355 inner_tcp_hdr(skb)->check =
1356 ~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
1357 &inner_ipv6_hdr(skb)->daddr,
1364 static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb)
1368 err = skb_cow_head(skb, 0);
1372 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1373 ip_hdr(skb)->check = 0;
1374 tcp_hdr(skb)->check =
1375 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1378 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1379 tcp_v6_gso_csum_prep(skb);
1385 static void ionic_tx_tso_post(struct net_device *netdev, struct ionic_queue *q,
1386 struct ionic_txq_desc *desc,
1387 struct sk_buff *skb,
1388 dma_addr_t addr, u8 nsge, u16 len,
1389 unsigned int hdrlen, unsigned int mss,
1391 u16 vlan_tci, bool has_vlan,
1392 bool start, bool done)
1397 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1398 flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1399 flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0;
1400 flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0;
1402 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr);
1403 desc->cmd = cpu_to_le64(cmd);
1404 desc->len = cpu_to_le16(len);
1405 desc->vlan_tci = cpu_to_le16(vlan_tci);
1406 desc->hdr_len = cpu_to_le16(hdrlen);
1407 desc->mss = cpu_to_le16(mss);
1409 ionic_write_cmb_desc(q, desc);
1412 skb_tx_timestamp(skb);
1413 if (likely(!ionic_txq_hwstamp_enabled(q)))
1414 netdev_tx_sent_queue(q_to_ndq(netdev, q), skb->len);
1415 ionic_txq_post(q, false);
1417 ionic_txq_post(q, done);
1421 static int ionic_tx_tso(struct net_device *netdev, struct ionic_queue *q,
1422 struct sk_buff *skb)
1424 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1425 struct ionic_tx_desc_info *desc_info;
1426 struct ionic_buf_info *buf_info;
1427 struct ionic_txq_sg_elem *elem;
1428 struct ionic_txq_desc *desc;
1429 unsigned int chunk_len;
1430 unsigned int frag_rem;
1431 unsigned int tso_rem;
1432 unsigned int seg_rem;
1433 dma_addr_t desc_addr;
1434 dma_addr_t frag_addr;
1435 unsigned int hdrlen;
1447 desc_info = &q->tx_info[q->head_idx];
1449 if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1453 mss = skb_shinfo(skb)->gso_size;
1454 outer_csum = (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
1458 SKB_GSO_UDP_TUNNEL |
1459 SKB_GSO_UDP_TUNNEL_CSUM));
1460 has_vlan = !!skb_vlan_tag_present(skb);
1461 vlan_tci = skb_vlan_tag_get(skb);
1462 encap = skb->encapsulation;
1464 /* Preload inner-most TCP csum field with IP pseudo hdr
1465 * calculated with IP length set to zero. HW will later
1466 * add in length to each TCP segment resulting from the TSO.
1470 err = ionic_tx_tcp_inner_pseudo_csum(skb);
1472 err = ionic_tx_tcp_pseudo_csum(skb);
1473 if (unlikely(err)) {
1474 /* clean up mapping from ionic_tx_map_skb */
1475 ionic_tx_desc_unmap_bufs(q, desc_info);
1480 hdrlen = skb_inner_tcp_all_headers(skb);
1482 hdrlen = skb_tcp_all_headers(skb);
1484 desc_info->skb = skb;
1485 buf_info = desc_info->bufs;
1487 seg_rem = min(tso_rem, hdrlen + mss);
1494 while (tso_rem > 0) {
1500 /* use fragments until we have enough to post a single descriptor */
1501 while (seg_rem > 0) {
1502 /* if the fragment is exhausted then move to the next one */
1503 if (frag_rem == 0) {
1504 /* grab the next fragment */
1505 frag_addr = buf_info->dma_addr;
1506 frag_rem = buf_info->len;
1509 chunk_len = min(frag_rem, seg_rem);
1511 /* fill main descriptor */
1512 desc = &q->txq[q->head_idx];
1513 elem = ionic_tx_sg_elems(q);
1514 desc_addr = frag_addr;
1515 desc_len = chunk_len;
1517 /* fill sg descriptor */
1518 elem->addr = cpu_to_le64(frag_addr);
1519 elem->len = cpu_to_le16(chunk_len);
1523 frag_addr += chunk_len;
1524 frag_rem -= chunk_len;
1525 tso_rem -= chunk_len;
1526 seg_rem -= chunk_len;
1528 seg_rem = min(tso_rem, mss);
1529 done = (tso_rem == 0);
1530 /* post descriptor */
1531 ionic_tx_tso_post(netdev, q, desc, skb, desc_addr, desc_nsge,
1532 desc_len, hdrlen, mss, outer_csum, vlan_tci,
1533 has_vlan, start, done);
1535 /* Buffer information is stored with the first tso descriptor */
1536 desc_info = &q->tx_info[q->head_idx];
1537 desc_info->nbufs = 0;
1540 stats->pkts += DIV_ROUND_UP(len - hdrlen, mss);
1541 stats->bytes += len;
1543 stats->tso_bytes = len;
1548 static void ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb,
1549 struct ionic_tx_desc_info *desc_info)
1551 struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1552 struct ionic_buf_info *buf_info = desc_info->bufs;
1553 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1559 has_vlan = !!skb_vlan_tag_present(skb);
1560 encap = skb->encapsulation;
1562 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1563 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1565 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL,
1566 flags, skb_shinfo(skb)->nr_frags,
1567 buf_info->dma_addr);
1568 desc->cmd = cpu_to_le64(cmd);
1569 desc->len = cpu_to_le16(buf_info->len);
1571 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1572 stats->vlan_inserted++;
1576 desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb));
1577 desc->csum_offset = cpu_to_le16(skb->csum_offset);
1579 ionic_write_cmb_desc(q, desc);
1581 if (skb_csum_is_sctp(skb))
1582 stats->crc32_csum++;
1587 static void ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb,
1588 struct ionic_tx_desc_info *desc_info)
1590 struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1591 struct ionic_buf_info *buf_info = desc_info->bufs;
1592 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1598 has_vlan = !!skb_vlan_tag_present(skb);
1599 encap = skb->encapsulation;
1601 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1602 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1604 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
1605 flags, skb_shinfo(skb)->nr_frags,
1606 buf_info->dma_addr);
1607 desc->cmd = cpu_to_le64(cmd);
1608 desc->len = cpu_to_le16(buf_info->len);
1610 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1611 stats->vlan_inserted++;
1615 desc->csum_start = 0;
1616 desc->csum_offset = 0;
1618 ionic_write_cmb_desc(q, desc);
1623 static void ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb,
1624 struct ionic_tx_desc_info *desc_info)
1626 struct ionic_buf_info *buf_info = &desc_info->bufs[1];
1627 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1628 struct ionic_txq_sg_elem *elem;
1631 elem = ionic_tx_sg_elems(q);
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, buf_info++, elem++) {
1633 elem->addr = cpu_to_le64(buf_info->dma_addr);
1634 elem->len = cpu_to_le16(buf_info->len);
1637 stats->frags += skb_shinfo(skb)->nr_frags;
1640 static int ionic_tx(struct net_device *netdev, struct ionic_queue *q,
1641 struct sk_buff *skb)
1643 struct ionic_tx_desc_info *desc_info = &q->tx_info[q->head_idx];
1644 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1645 bool ring_dbell = true;
1647 if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1650 desc_info->skb = skb;
1652 /* set up the initial descriptor */
1653 if (skb->ip_summed == CHECKSUM_PARTIAL)
1654 ionic_tx_calc_csum(q, skb, desc_info);
1656 ionic_tx_calc_no_csum(q, skb, desc_info);
1659 ionic_tx_skb_frags(q, skb, desc_info);
1661 skb_tx_timestamp(skb);
1663 stats->bytes += skb->len;
1665 if (likely(!ionic_txq_hwstamp_enabled(q))) {
1666 struct netdev_queue *ndq = q_to_ndq(netdev, q);
1668 if (unlikely(!ionic_q_has_space(q, MAX_SKB_FRAGS + 1)))
1669 netif_tx_stop_queue(ndq);
1670 ring_dbell = __netdev_tx_sent_queue(ndq, skb->len,
1671 netdev_xmit_more());
1673 ionic_txq_post(q, ring_dbell);
1678 static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb)
1680 int nr_frags = skb_shinfo(skb)->nr_frags;
1681 bool too_many_frags = false;
1693 /* Each desc is mss long max, so a descriptor for each gso_seg */
1694 if (skb_is_gso(skb)) {
1695 ndescs = skb_shinfo(skb)->gso_segs;
1703 if (unlikely(nr_frags > q->max_sg_elems)) {
1704 too_many_frags = true;
1711 /* We need to scan the skb to be sure that none of the MTU sized
1712 * packets in the TSO will require more sgs per descriptor than we
1713 * can support. We loop through the frags, add up the lengths for
1714 * a packet, and count the number of sgs used per packet.
1717 frag = skb_shinfo(skb)->frags;
1718 encap = skb->encapsulation;
1720 /* start with just hdr in first part of first descriptor */
1722 hdrlen = skb_inner_tcp_all_headers(skb);
1724 hdrlen = skb_tcp_all_headers(skb);
1725 seg_rem = min_t(int, tso_rem, hdrlen + skb_shinfo(skb)->gso_size);
1728 while (tso_rem > 0) {
1730 while (seg_rem > 0) {
1733 /* We add the +1 because we can take buffers for one
1734 * more than we have SGs: one for the initial desc data
1735 * in addition to the SG segments that might follow.
1737 if (desc_bufs > q->max_sg_elems + 1) {
1738 too_many_frags = true;
1742 if (frag_rem == 0) {
1743 frag_rem = skb_frag_size(frag);
1746 chunk_len = min(frag_rem, seg_rem);
1747 frag_rem -= chunk_len;
1748 tso_rem -= chunk_len;
1749 seg_rem -= chunk_len;
1752 seg_rem = min_t(int, tso_rem, skb_shinfo(skb)->gso_size);
1756 if (too_many_frags) {
1757 err = skb_linearize(skb);
1760 q_to_tx_stats(q)->linearize++;
1766 static netdev_tx_t ionic_start_hwstamp_xmit(struct sk_buff *skb,
1767 struct net_device *netdev)
1769 struct ionic_lif *lif = netdev_priv(netdev);
1770 struct ionic_queue *q;
1773 /* Does not stop/start txq, because we post to a separate tx queue
1774 * for timestamping, and if a packet can't be posted immediately to
1775 * the timestamping queue, it is dropped.
1778 q = &lif->hwstamp_txq->q;
1779 ndescs = ionic_tx_descs_needed(q, skb);
1780 if (unlikely(ndescs < 0))
1783 if (unlikely(!ionic_q_has_space(q, ndescs)))
1786 skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP;
1787 if (skb_is_gso(skb))
1788 err = ionic_tx_tso(netdev, q, skb);
1790 err = ionic_tx(netdev, q, skb);
1795 return NETDEV_TX_OK;
1800 return NETDEV_TX_OK;
1803 netdev_tx_t ionic_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1805 u16 queue_index = skb_get_queue_mapping(skb);
1806 struct ionic_lif *lif = netdev_priv(netdev);
1807 struct ionic_queue *q;
1811 if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state))) {
1813 return NETDEV_TX_OK;
1816 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1817 if (lif->hwstamp_txq && lif->phc->ts_config_tx_mode)
1818 return ionic_start_hwstamp_xmit(skb, netdev);
1820 if (unlikely(queue_index >= lif->nxqs))
1822 q = &lif->txqcqs[queue_index]->q;
1824 ndescs = ionic_tx_descs_needed(q, skb);
1828 if (!netif_txq_maybe_stop(q_to_ndq(netdev, q),
1829 ionic_q_space_avail(q),
1831 return NETDEV_TX_BUSY;
1833 if (skb_is_gso(skb))
1834 err = ionic_tx_tso(netdev, q, skb);
1836 err = ionic_tx(netdev, q, skb);
1841 return NETDEV_TX_OK;
1846 return NETDEV_TX_OK;