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>
11 #include "ionic_lif.h"
12 #include "ionic_txrx.h"
14 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
15 void *data, size_t len);
17 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
18 const skb_frag_t *frag,
19 size_t offset, size_t len);
21 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
22 struct ionic_tx_desc_info *desc_info);
24 static void ionic_tx_clean(struct ionic_queue *q,
25 struct ionic_tx_desc_info *desc_info,
26 struct ionic_txq_comp *comp);
28 static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell)
30 ionic_q_post(q, ring_dbell);
33 static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell)
35 ionic_q_post(q, ring_dbell);
38 bool ionic_txq_poke_doorbell(struct ionic_queue *q)
40 struct netdev_queue *netdev_txq;
41 unsigned long now, then, dif;
42 struct net_device *netdev;
44 netdev = q->lif->netdev;
45 netdev_txq = netdev_get_tx_queue(netdev, q->index);
47 HARD_TX_LOCK(netdev, netdev_txq, smp_processor_id());
49 if (q->tail_idx == q->head_idx) {
50 HARD_TX_UNLOCK(netdev, netdev_txq);
54 now = READ_ONCE(jiffies);
55 then = q->dbell_jiffies;
58 if (dif > q->dbell_deadline) {
59 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
60 q->dbval | q->head_idx);
62 q->dbell_jiffies = now;
65 HARD_TX_UNLOCK(netdev, netdev_txq);
70 bool ionic_rxq_poke_doorbell(struct ionic_queue *q)
72 unsigned long now, then, dif;
74 /* no lock, called from rx napi or txrx napi, nothing else can fill */
76 if (q->tail_idx == q->head_idx)
79 now = READ_ONCE(jiffies);
80 then = q->dbell_jiffies;
83 if (dif > q->dbell_deadline) {
84 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
85 q->dbval | q->head_idx);
87 q->dbell_jiffies = now;
89 dif = 2 * q->dbell_deadline;
90 if (dif > IONIC_RX_MAX_DOORBELL_DEADLINE)
91 dif = IONIC_RX_MAX_DOORBELL_DEADLINE;
93 q->dbell_deadline = dif;
99 static inline struct ionic_txq_sg_elem *ionic_tx_sg_elems(struct ionic_queue *q)
101 if (likely(q->sg_desc_size == sizeof(struct ionic_txq_sg_desc_v1)))
102 return q->txq_sgl_v1[q->head_idx].elems;
104 return q->txq_sgl[q->head_idx].elems;
107 static inline struct netdev_queue *q_to_ndq(struct net_device *netdev,
108 struct ionic_queue *q)
110 return netdev_get_tx_queue(netdev, q->index);
113 static void *ionic_rx_buf_va(struct ionic_buf_info *buf_info)
115 return page_address(buf_info->page) + buf_info->page_offset;
118 static dma_addr_t ionic_rx_buf_pa(struct ionic_buf_info *buf_info)
120 return buf_info->dma_addr + buf_info->page_offset;
123 static unsigned int ionic_rx_buf_size(struct ionic_buf_info *buf_info)
125 return min_t(u32, IONIC_MAX_BUF_LEN, IONIC_PAGE_SIZE - buf_info->page_offset);
128 static int ionic_rx_page_alloc(struct ionic_queue *q,
129 struct ionic_buf_info *buf_info)
131 struct ionic_rx_stats *stats;
136 stats = q_to_rx_stats(q);
138 if (unlikely(!buf_info)) {
139 net_err_ratelimited("%s: %s invalid buf_info in alloc\n",
140 dev_name(dev), q->name);
144 page = alloc_pages(IONIC_PAGE_GFP_MASK, 0);
145 if (unlikely(!page)) {
146 net_err_ratelimited("%s: %s page alloc failed\n",
147 dev_name(dev), q->name);
152 buf_info->dma_addr = dma_map_page(dev, page, 0,
153 IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
154 if (unlikely(dma_mapping_error(dev, buf_info->dma_addr))) {
155 __free_pages(page, 0);
156 net_err_ratelimited("%s: %s dma map failed\n",
157 dev_name(dev), q->name);
158 stats->dma_map_err++;
162 buf_info->page = page;
163 buf_info->page_offset = 0;
168 static void ionic_rx_page_free(struct ionic_queue *q,
169 struct ionic_buf_info *buf_info)
171 struct device *dev = q->dev;
173 if (unlikely(!buf_info)) {
174 net_err_ratelimited("%s: %s invalid buf_info in free\n",
175 dev_name(dev), q->name);
182 dma_unmap_page(dev, buf_info->dma_addr, IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
183 __free_pages(buf_info->page, 0);
184 buf_info->page = NULL;
187 static bool ionic_rx_buf_recycle(struct ionic_queue *q,
188 struct ionic_buf_info *buf_info, u32 len)
192 /* don't re-use pages allocated in low-mem condition */
193 if (page_is_pfmemalloc(buf_info->page))
196 /* don't re-use buffers from non-local numa nodes */
197 if (page_to_nid(buf_info->page) != numa_mem_id())
200 size = ALIGN(len, q->xdp_rxq_info ? IONIC_PAGE_SIZE : IONIC_PAGE_SPLIT_SZ);
201 buf_info->page_offset += size;
202 if (buf_info->page_offset >= IONIC_PAGE_SIZE)
205 get_page(buf_info->page);
210 static void ionic_rx_add_skb_frag(struct ionic_queue *q,
212 struct ionic_buf_info *buf_info,
217 dma_sync_single_range_for_cpu(q->dev, ionic_rx_buf_pa(buf_info),
218 off, len, DMA_FROM_DEVICE);
220 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
221 buf_info->page, buf_info->page_offset + off,
225 if (!ionic_rx_buf_recycle(q, buf_info, len)) {
226 dma_unmap_page(q->dev, buf_info->dma_addr,
227 IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
228 buf_info->page = NULL;
232 static struct sk_buff *ionic_rx_build_skb(struct ionic_queue *q,
233 struct ionic_rx_desc_info *desc_info,
234 unsigned int headroom,
236 unsigned int num_sg_elems,
239 struct ionic_buf_info *buf_info;
240 struct ionic_rx_stats *stats;
245 stats = q_to_rx_stats(q);
247 buf_info = &desc_info->bufs[0];
248 prefetchw(buf_info->page);
250 skb = napi_get_frags(&q_to_qcq(q)->napi);
251 if (unlikely(!skb)) {
252 net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
253 dev_name(q->dev), q->name);
259 frag_len = min_t(u16, len,
260 IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
262 frag_len = min_t(u16, len, ionic_rx_buf_size(buf_info));
264 if (unlikely(!buf_info->page))
265 goto err_bad_buf_page;
266 ionic_rx_add_skb_frag(q, skb, buf_info, headroom, frag_len, synced);
270 for (i = 0; i < num_sg_elems; i++, buf_info++) {
271 if (unlikely(!buf_info->page))
272 goto err_bad_buf_page;
273 frag_len = min_t(u16, len, ionic_rx_buf_size(buf_info));
274 ionic_rx_add_skb_frag(q, skb, buf_info, 0, frag_len, synced);
285 static struct sk_buff *ionic_rx_copybreak(struct net_device *netdev,
286 struct ionic_queue *q,
287 struct ionic_rx_desc_info *desc_info,
288 unsigned int headroom,
292 struct ionic_buf_info *buf_info;
293 struct ionic_rx_stats *stats;
294 struct device *dev = q->dev;
297 stats = q_to_rx_stats(q);
299 buf_info = &desc_info->bufs[0];
301 skb = napi_alloc_skb(&q_to_qcq(q)->napi, len);
302 if (unlikely(!skb)) {
303 net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
304 dev_name(dev), q->name);
309 if (unlikely(!buf_info->page)) {
315 dma_sync_single_range_for_cpu(dev, ionic_rx_buf_pa(buf_info),
316 headroom, len, DMA_FROM_DEVICE);
317 skb_copy_to_linear_data(skb, ionic_rx_buf_va(buf_info) + headroom, len);
318 dma_sync_single_range_for_device(dev, ionic_rx_buf_pa(buf_info),
319 headroom, len, DMA_FROM_DEVICE);
322 skb->protocol = eth_type_trans(skb, netdev);
327 static void ionic_xdp_tx_desc_clean(struct ionic_queue *q,
328 struct ionic_tx_desc_info *desc_info)
330 unsigned int nbufs = desc_info->nbufs;
331 struct ionic_buf_info *buf_info;
332 struct device *dev = q->dev;
338 buf_info = desc_info->bufs;
339 dma_unmap_single(dev, buf_info->dma_addr,
340 buf_info->len, DMA_TO_DEVICE);
341 if (desc_info->act == XDP_TX)
342 __free_pages(buf_info->page, 0);
343 buf_info->page = NULL;
346 for (i = 1; i < nbufs + 1 && buf_info->page; i++, buf_info++) {
347 dma_unmap_page(dev, buf_info->dma_addr,
348 buf_info->len, DMA_TO_DEVICE);
349 if (desc_info->act == XDP_TX)
350 __free_pages(buf_info->page, 0);
351 buf_info->page = NULL;
354 if (desc_info->act == XDP_REDIRECT)
355 xdp_return_frame(desc_info->xdpf);
357 desc_info->nbufs = 0;
358 desc_info->xdpf = NULL;
362 static int ionic_xdp_post_frame(struct ionic_queue *q, struct xdp_frame *frame,
363 enum xdp_action act, struct page *page, int off,
366 struct ionic_tx_desc_info *desc_info;
367 struct ionic_buf_info *buf_info;
368 struct ionic_tx_stats *stats;
369 struct ionic_txq_desc *desc;
370 size_t len = frame->len;
374 desc_info = &q->tx_info[q->head_idx];
375 desc = &q->txq[q->head_idx];
376 buf_info = desc_info->bufs;
377 stats = q_to_tx_stats(q);
379 dma_addr = ionic_tx_map_single(q, frame->data, len);
380 if (dma_mapping_error(q->dev, dma_addr)) {
381 stats->dma_map_err++;
384 buf_info->dma_addr = dma_addr;
386 buf_info->page = page;
387 buf_info->page_offset = off;
389 desc_info->nbufs = 1;
390 desc_info->xdpf = frame;
391 desc_info->act = act;
393 if (xdp_frame_has_frags(frame)) {
394 struct ionic_txq_sg_elem *elem;
395 struct skb_shared_info *sinfo;
396 struct ionic_buf_info *bi;
401 sinfo = xdp_get_shared_info_from_frame(frame);
403 elem = ionic_tx_sg_elems(q);
404 for (i = 0; i < sinfo->nr_frags; i++, frag++, bi++) {
405 dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
406 if (dma_mapping_error(q->dev, dma_addr)) {
407 stats->dma_map_err++;
408 ionic_tx_desc_unmap_bufs(q, desc_info);
411 bi->dma_addr = dma_addr;
412 bi->len = skb_frag_size(frag);
413 bi->page = skb_frag_page(frag);
415 elem->addr = cpu_to_le64(bi->dma_addr);
416 elem->len = cpu_to_le16(bi->len);
423 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
424 0, (desc_info->nbufs - 1), buf_info->dma_addr);
425 desc->cmd = cpu_to_le64(cmd);
426 desc->len = cpu_to_le16(len);
427 desc->csum_start = 0;
428 desc->csum_offset = 0;
434 ionic_txq_post(q, ring_doorbell);
439 int ionic_xdp_xmit(struct net_device *netdev, int n,
440 struct xdp_frame **xdp_frames, u32 flags)
442 struct ionic_lif *lif = netdev_priv(netdev);
443 struct ionic_queue *txq;
444 struct netdev_queue *nq;
450 if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state)))
453 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
456 /* AdminQ is assumed on cpu 0, while we attempt to affinitize the
457 * TxRx queue pairs 0..n-1 on cpus 1..n. We try to keep with that
458 * affinitization here, but of course irqbalance and friends might
459 * have juggled things anyway, so we have to check for the 0 case.
461 cpu = smp_processor_id();
462 qi = cpu ? (cpu - 1) % lif->nxqs : cpu;
464 txq = &lif->txqcqs[qi]->q;
465 nq = netdev_get_tx_queue(netdev, txq->index);
466 __netif_tx_lock(nq, cpu);
467 txq_trans_cond_update(nq);
469 if (netif_tx_queue_stopped(nq) ||
470 !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
471 ionic_q_space_avail(txq),
473 __netif_tx_unlock(nq);
477 space = min_t(int, n, ionic_q_space_avail(txq));
478 for (nxmit = 0; nxmit < space ; nxmit++) {
479 if (ionic_xdp_post_frame(txq, xdp_frames[nxmit],
481 virt_to_page(xdp_frames[nxmit]->data),
488 if (flags & XDP_XMIT_FLUSH)
489 ionic_dbell_ring(lif->kern_dbpage, txq->hw_type,
490 txq->dbval | txq->head_idx);
492 netif_txq_maybe_stop(q_to_ndq(netdev, txq),
493 ionic_q_space_avail(txq),
495 __netif_tx_unlock(nq);
500 static bool ionic_run_xdp(struct ionic_rx_stats *stats,
501 struct net_device *netdev,
502 struct bpf_prog *xdp_prog,
503 struct ionic_queue *rxq,
504 struct ionic_buf_info *buf_info,
507 u32 xdp_action = XDP_ABORTED;
508 struct xdp_buff xdp_buf;
509 struct ionic_queue *txq;
510 struct netdev_queue *nq;
511 struct xdp_frame *xdpf;
516 xdp_init_buff(&xdp_buf, IONIC_PAGE_SIZE, rxq->xdp_rxq_info);
517 frag_len = min_t(u16, len, IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
518 xdp_prepare_buff(&xdp_buf, ionic_rx_buf_va(buf_info),
519 XDP_PACKET_HEADROOM, frag_len, false);
521 dma_sync_single_range_for_cpu(rxq->dev, ionic_rx_buf_pa(buf_info),
522 XDP_PACKET_HEADROOM, len,
525 prefetchw(&xdp_buf.data_hard_start);
527 /* We limit MTU size to one buffer if !xdp_has_frags, so
528 * if the recv len is bigger than one buffer
529 * then we know we have frag info to gather
531 remain_len = len - frag_len;
533 struct skb_shared_info *sinfo;
534 struct ionic_buf_info *bi;
538 sinfo = xdp_get_shared_info_from_buff(&xdp_buf);
540 sinfo->xdp_frags_size = 0;
541 xdp_buff_set_frags_flag(&xdp_buf);
544 if (unlikely(sinfo->nr_frags >= MAX_SKB_FRAGS)) {
549 frag = &sinfo->frags[sinfo->nr_frags];
552 frag_len = min_t(u16, remain_len, ionic_rx_buf_size(bi));
553 dma_sync_single_range_for_cpu(rxq->dev, ionic_rx_buf_pa(bi),
554 0, frag_len, DMA_FROM_DEVICE);
555 skb_frag_fill_page_desc(frag, bi->page, 0, frag_len);
556 sinfo->xdp_frags_size += frag_len;
557 remain_len -= frag_len;
559 if (page_is_pfmemalloc(bi->page))
560 xdp_buff_set_frag_pfmemalloc(&xdp_buf);
561 } while (remain_len > 0);
564 xdp_action = bpf_prog_run_xdp(xdp_prog, &xdp_buf);
566 switch (xdp_action) {
569 return false; /* false = we didn't consume the packet */
572 ionic_rx_page_free(rxq, buf_info);
577 xdpf = xdp_convert_buff_to_frame(&xdp_buf);
582 nq = netdev_get_tx_queue(netdev, txq->index);
583 __netif_tx_lock(nq, smp_processor_id());
584 txq_trans_cond_update(nq);
586 if (netif_tx_queue_stopped(nq) ||
587 !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
588 ionic_q_space_avail(txq),
590 __netif_tx_unlock(nq);
594 dma_unmap_page(rxq->dev, buf_info->dma_addr,
595 IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
597 err = ionic_xdp_post_frame(txq, xdpf, XDP_TX,
599 buf_info->page_offset,
601 __netif_tx_unlock(nq);
603 netdev_dbg(netdev, "tx ionic_xdp_post_frame err %d\n", err);
608 /* the Tx completion will free the buffers */
612 /* unmap the pages before handing them to a different device */
613 dma_unmap_page(rxq->dev, buf_info->dma_addr,
614 IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
616 err = xdp_do_redirect(netdev, &xdp_buf, xdp_prog);
618 netdev_dbg(netdev, "xdp_do_redirect err %d\n", err);
621 buf_info->page = NULL;
622 rxq->xdp_flush = true;
623 stats->xdp_redirect++;
634 trace_xdp_exception(netdev, xdp_prog, xdp_action);
635 ionic_rx_page_free(rxq, buf_info);
636 stats->xdp_aborted++;
641 static void ionic_rx_clean(struct ionic_queue *q,
642 struct ionic_rx_desc_info *desc_info,
643 struct ionic_rxq_comp *comp)
645 struct net_device *netdev = q->lif->netdev;
646 struct ionic_qcq *qcq = q_to_qcq(q);
647 struct ionic_rx_stats *stats;
648 struct bpf_prog *xdp_prog;
649 unsigned int headroom;
655 stats = q_to_rx_stats(q);
662 len = le16_to_cpu(comp->len);
666 xdp_prog = READ_ONCE(q->lif->xdp_prog);
668 if (ionic_run_xdp(stats, netdev, xdp_prog, q, desc_info->bufs, len))
673 headroom = q->xdp_rxq_info ? XDP_PACKET_HEADROOM : 0;
674 use_copybreak = len <= q->lif->rx_copybreak;
676 skb = ionic_rx_copybreak(netdev, q, desc_info,
677 headroom, len, synced);
679 skb = ionic_rx_build_skb(q, desc_info, headroom, len,
680 comp->num_sg_elems, synced);
682 if (unlikely(!skb)) {
687 skb_record_rx_queue(skb, q->index);
689 if (likely(netdev->features & NETIF_F_RXHASH)) {
690 switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) {
691 case IONIC_PKT_TYPE_IPV4:
692 case IONIC_PKT_TYPE_IPV6:
693 skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
696 case IONIC_PKT_TYPE_IPV4_TCP:
697 case IONIC_PKT_TYPE_IPV6_TCP:
698 case IONIC_PKT_TYPE_IPV4_UDP:
699 case IONIC_PKT_TYPE_IPV6_UDP:
700 skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
706 if (likely(netdev->features & NETIF_F_RXCSUM) &&
707 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC)) {
708 skb->ip_summed = CHECKSUM_COMPLETE;
709 skb->csum = (__force __wsum)le16_to_cpu(comp->csum);
710 stats->csum_complete++;
715 if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) ||
716 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) ||
717 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD)))
720 if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
721 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) {
722 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
723 le16_to_cpu(comp->vlan_tci));
724 stats->vlan_stripped++;
727 if (unlikely(q->features & IONIC_RXQ_F_HWSTAMP)) {
728 __le64 *cq_desc_hwstamp;
734 sizeof(struct ionic_rxq_comp) -
735 IONIC_HWSTAMP_CQ_NEGOFFSET;
737 hwstamp = le64_to_cpu(*cq_desc_hwstamp);
739 if (hwstamp != IONIC_HWSTAMP_INVALID) {
740 skb_hwtstamps(skb)->hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
741 stats->hwstamp_valid++;
743 stats->hwstamp_invalid++;
748 napi_gro_receive(&qcq->napi, skb);
750 napi_gro_frags(&qcq->napi);
753 bool ionic_rx_service(struct ionic_cq *cq)
755 struct ionic_rx_desc_info *desc_info;
756 struct ionic_queue *q = cq->bound_q;
757 struct ionic_rxq_comp *comp;
759 comp = &((struct ionic_rxq_comp *)cq->base)[cq->tail_idx];
761 if (!color_match(comp->pkt_type_color, cq->done_color))
764 /* check for empty queue */
765 if (q->tail_idx == q->head_idx)
768 if (q->tail_idx != le16_to_cpu(comp->comp_index))
771 desc_info = &q->rx_info[q->tail_idx];
772 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
774 /* clean the related q entry, only one per qc completion */
775 ionic_rx_clean(q, desc_info, comp);
780 static inline void ionic_write_cmb_desc(struct ionic_queue *q,
783 /* Since Rx and Tx descriptors are the same size, we can
784 * save an instruction or two and skip the qtype check.
786 if (unlikely(q_to_qcq(q)->flags & IONIC_QCQ_F_CMB_RINGS))
787 memcpy_toio(&q->cmb_txq[q->head_idx], desc, sizeof(q->cmb_txq[0]));
790 void ionic_rx_fill(struct ionic_queue *q)
792 struct net_device *netdev = q->lif->netdev;
793 struct ionic_rx_desc_info *desc_info;
794 struct ionic_rxq_sg_elem *sg_elem;
795 struct ionic_buf_info *buf_info;
796 unsigned int fill_threshold;
797 struct ionic_rxq_desc *desc;
798 unsigned int remain_len;
799 unsigned int frag_len;
806 n_fill = ionic_q_space_avail(q);
808 fill_threshold = min_t(unsigned int, IONIC_RX_FILL_THRESHOLD,
809 q->num_descs / IONIC_RX_FILL_DIV);
810 if (n_fill < fill_threshold)
813 len = netdev->mtu + VLAN_ETH_HLEN;
815 for (i = n_fill; i; i--) {
816 unsigned int headroom;
817 unsigned int buf_len;
821 desc = &q->rxq[q->head_idx];
822 desc_info = &q->rx_info[q->head_idx];
823 buf_info = &desc_info->bufs[0];
825 if (!buf_info->page) { /* alloc a new buffer? */
826 if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
833 /* fill main descriptor - buf[0]
834 * XDP uses space in the first buffer, so account for
835 * head room, tail room, and ip header in the first frag size.
837 headroom = q->xdp_rxq_info ? XDP_PACKET_HEADROOM : 0;
839 buf_len = IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN;
841 buf_len = ionic_rx_buf_size(buf_info);
842 frag_len = min_t(u16, len, buf_len);
844 desc->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info) + headroom);
845 desc->len = cpu_to_le16(frag_len);
846 remain_len -= frag_len;
850 /* fill sg descriptors - buf[1..n] */
851 sg_elem = q->rxq_sgl[q->head_idx].elems;
852 for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++, sg_elem++) {
853 if (!buf_info->page) { /* alloc a new sg buffer? */
854 if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
861 sg_elem->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info));
862 frag_len = min_t(u16, remain_len, ionic_rx_buf_size(buf_info));
863 sg_elem->len = cpu_to_le16(frag_len);
864 remain_len -= frag_len;
869 /* clear end sg element as a sentinel */
870 if (j < q->max_sg_elems)
871 memset(sg_elem, 0, sizeof(*sg_elem));
873 desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG :
874 IONIC_RXQ_DESC_OPCODE_SIMPLE;
875 desc_info->nbufs = nfrags;
877 ionic_write_cmb_desc(q, desc);
879 ionic_rxq_post(q, false);
882 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
883 q->dbval | q->head_idx);
885 q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
886 q->dbell_jiffies = jiffies;
888 mod_timer(&q_to_qcq(q)->napi_qcq->napi_deadline,
889 jiffies + IONIC_NAPI_DEADLINE);
892 void ionic_rx_empty(struct ionic_queue *q)
894 struct ionic_rx_desc_info *desc_info;
895 struct ionic_buf_info *buf_info;
898 for (i = 0; i < q->num_descs; i++) {
899 desc_info = &q->rx_info[i];
900 for (j = 0; j < ARRAY_SIZE(desc_info->bufs); j++) {
901 buf_info = &desc_info->bufs[j];
903 ionic_rx_page_free(q, buf_info);
906 desc_info->nbufs = 0;
913 static void ionic_dim_update(struct ionic_qcq *qcq, int napi_mode)
915 struct dim_sample dim_sample;
916 struct ionic_lif *lif;
920 if (!qcq->intr.dim_coal_hw)
924 qi = qcq->cq.bound_q->index;
927 case IONIC_LIF_F_TX_DIM_INTR:
928 pkts = lif->txqstats[qi].pkts;
929 bytes = lif->txqstats[qi].bytes;
931 case IONIC_LIF_F_RX_DIM_INTR:
932 pkts = lif->rxqstats[qi].pkts;
933 bytes = lif->rxqstats[qi].bytes;
936 pkts = lif->txqstats[qi].pkts + lif->rxqstats[qi].pkts;
937 bytes = lif->txqstats[qi].bytes + lif->rxqstats[qi].bytes;
941 dim_update_sample(qcq->cq.bound_intr->rearm_count,
942 pkts, bytes, &dim_sample);
944 net_dim(&qcq->dim, dim_sample);
947 int ionic_tx_napi(struct napi_struct *napi, int budget)
949 struct ionic_qcq *qcq = napi_to_qcq(napi);
950 struct ionic_cq *cq = napi_to_cq(napi);
954 work_done = ionic_tx_cq_service(cq, budget);
956 if (unlikely(!budget))
959 if (work_done < budget && napi_complete_done(napi, work_done)) {
960 ionic_dim_update(qcq, IONIC_LIF_F_TX_DIM_INTR);
961 flags |= IONIC_INTR_CRED_UNMASK;
962 cq->bound_intr->rearm_count++;
965 if (work_done || flags) {
966 flags |= IONIC_INTR_CRED_RESET_COALESCE;
967 ionic_intr_credits(cq->idev->intr_ctrl,
968 cq->bound_intr->index,
972 if (!work_done && ionic_txq_poke_doorbell(&qcq->q))
973 mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
978 static void ionic_xdp_do_flush(struct ionic_cq *cq)
980 if (cq->bound_q->xdp_flush) {
982 cq->bound_q->xdp_flush = false;
986 int ionic_rx_napi(struct napi_struct *napi, int budget)
988 struct ionic_qcq *qcq = napi_to_qcq(napi);
989 struct ionic_cq *cq = napi_to_cq(napi);
993 if (unlikely(!budget))
996 work_done = ionic_cq_service(cq, budget,
997 ionic_rx_service, NULL, NULL);
999 ionic_rx_fill(cq->bound_q);
1001 ionic_xdp_do_flush(cq);
1002 if (work_done < budget && napi_complete_done(napi, work_done)) {
1003 ionic_dim_update(qcq, IONIC_LIF_F_RX_DIM_INTR);
1004 flags |= IONIC_INTR_CRED_UNMASK;
1005 cq->bound_intr->rearm_count++;
1008 if (work_done || flags) {
1009 flags |= IONIC_INTR_CRED_RESET_COALESCE;
1010 ionic_intr_credits(cq->idev->intr_ctrl,
1011 cq->bound_intr->index,
1015 if (!work_done && ionic_rxq_poke_doorbell(&qcq->q))
1016 mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
1021 int ionic_txrx_napi(struct napi_struct *napi, int budget)
1023 struct ionic_qcq *rxqcq = napi_to_qcq(napi);
1024 struct ionic_cq *rxcq = napi_to_cq(napi);
1025 unsigned int qi = rxcq->bound_q->index;
1026 struct ionic_qcq *txqcq;
1027 struct ionic_lif *lif;
1028 struct ionic_cq *txcq;
1029 bool resched = false;
1030 u32 rx_work_done = 0;
1031 u32 tx_work_done = 0;
1034 lif = rxcq->bound_q->lif;
1035 txqcq = lif->txqcqs[qi];
1036 txcq = &lif->txqcqs[qi]->cq;
1038 tx_work_done = ionic_tx_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT);
1040 if (unlikely(!budget))
1043 rx_work_done = ionic_cq_service(rxcq, budget,
1044 ionic_rx_service, NULL, NULL);
1046 ionic_rx_fill(rxcq->bound_q);
1048 ionic_xdp_do_flush(rxcq);
1049 if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) {
1050 ionic_dim_update(rxqcq, 0);
1051 flags |= IONIC_INTR_CRED_UNMASK;
1052 rxcq->bound_intr->rearm_count++;
1055 if (rx_work_done || flags) {
1056 flags |= IONIC_INTR_CRED_RESET_COALESCE;
1057 ionic_intr_credits(rxcq->idev->intr_ctrl, rxcq->bound_intr->index,
1058 tx_work_done + rx_work_done, flags);
1061 if (!rx_work_done && ionic_rxq_poke_doorbell(&rxqcq->q))
1063 if (!tx_work_done && ionic_txq_poke_doorbell(&txqcq->q))
1066 mod_timer(&rxqcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
1068 return rx_work_done;
1071 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
1072 void *data, size_t len)
1074 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1075 struct device *dev = q->dev;
1076 dma_addr_t dma_addr;
1078 dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE);
1079 if (dma_mapping_error(dev, dma_addr)) {
1080 net_warn_ratelimited("%s: DMA single map failed on %s!\n",
1081 dev_name(dev), q->name);
1082 stats->dma_map_err++;
1088 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
1089 const skb_frag_t *frag,
1090 size_t offset, size_t len)
1092 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1093 struct device *dev = q->dev;
1094 dma_addr_t dma_addr;
1096 dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE);
1097 if (dma_mapping_error(dev, dma_addr)) {
1098 net_warn_ratelimited("%s: DMA frag map failed on %s!\n",
1099 dev_name(dev), q->name);
1100 stats->dma_map_err++;
1105 static int ionic_tx_map_skb(struct ionic_queue *q, struct sk_buff *skb,
1106 struct ionic_tx_desc_info *desc_info)
1108 struct ionic_buf_info *buf_info = desc_info->bufs;
1109 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1110 struct device *dev = q->dev;
1111 dma_addr_t dma_addr;
1112 unsigned int nfrags;
1116 dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb));
1117 if (dma_mapping_error(dev, dma_addr)) {
1118 stats->dma_map_err++;
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));
1129 if (dma_mapping_error(dev, dma_addr)) {
1130 stats->dma_map_err++;
1133 buf_info->dma_addr = dma_addr;
1134 buf_info->len = skb_frag_size(frag);
1138 desc_info->nbufs = 1 + nfrags;
1143 /* unwind the frag mappings and the head mapping */
1144 while (frag_idx > 0) {
1147 dma_unmap_page(dev, buf_info->dma_addr,
1148 buf_info->len, DMA_TO_DEVICE);
1150 dma_unmap_single(dev, buf_info->dma_addr, buf_info->len, DMA_TO_DEVICE);
1154 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
1155 struct ionic_tx_desc_info *desc_info)
1157 struct ionic_buf_info *buf_info = desc_info->bufs;
1158 struct device *dev = q->dev;
1161 if (!desc_info->nbufs)
1164 dma_unmap_single(dev, (dma_addr_t)buf_info->dma_addr,
1165 buf_info->len, DMA_TO_DEVICE);
1167 for (i = 1; i < desc_info->nbufs; i++, buf_info++)
1168 dma_unmap_page(dev, (dma_addr_t)buf_info->dma_addr,
1169 buf_info->len, DMA_TO_DEVICE);
1171 desc_info->nbufs = 0;
1174 static void ionic_tx_clean(struct ionic_queue *q,
1175 struct ionic_tx_desc_info *desc_info,
1176 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);
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, 1);
1231 static bool ionic_tx_service(struct ionic_cq *cq,
1232 unsigned int *total_pkts, unsigned int *total_bytes)
1234 struct ionic_tx_desc_info *desc_info;
1235 struct ionic_queue *q = cq->bound_q;
1236 struct ionic_txq_comp *comp;
1237 unsigned int bytes = 0;
1238 unsigned int pkts = 0;
1241 comp = &((struct ionic_txq_comp *)cq->base)[cq->tail_idx];
1243 if (!color_match(comp->color, cq->done_color))
1246 /* clean the related q entries, there could be
1247 * several q entries completed for each cq completion
1250 desc_info = &q->tx_info[q->tail_idx];
1251 desc_info->bytes = 0;
1252 index = q->tail_idx;
1253 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1254 ionic_tx_clean(q, desc_info, comp);
1255 if (desc_info->skb) {
1257 bytes += desc_info->bytes;
1258 desc_info->skb = NULL;
1260 } while (index != le16_to_cpu(comp->comp_index));
1262 (*total_pkts) += pkts;
1263 (*total_bytes) += bytes;
1268 unsigned int ionic_tx_cq_service(struct ionic_cq *cq, unsigned int work_to_do)
1270 unsigned int work_done = 0;
1271 unsigned int bytes = 0;
1272 unsigned int pkts = 0;
1274 if (work_to_do == 0)
1277 while (ionic_tx_service(cq, &pkts, &bytes)) {
1278 if (cq->tail_idx == cq->num_descs - 1)
1279 cq->done_color = !cq->done_color;
1280 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
1282 if (++work_done >= work_to_do)
1287 struct ionic_queue *q = cq->bound_q;
1289 if (likely(!ionic_txq_hwstamp_enabled(q)))
1290 netif_txq_completed_wake(q_to_ndq(q->lif->netdev, q),
1292 ionic_q_space_avail(q),
1293 IONIC_TSO_DESCS_NEEDED);
1299 void ionic_tx_flush(struct ionic_cq *cq)
1303 work_done = ionic_tx_cq_service(cq, cq->num_descs);
1305 ionic_intr_credits(cq->idev->intr_ctrl, cq->bound_intr->index,
1306 work_done, IONIC_INTR_CRED_RESET_COALESCE);
1309 void ionic_tx_empty(struct ionic_queue *q)
1311 struct ionic_tx_desc_info *desc_info;
1315 /* walk the not completed tx entries, if any */
1316 while (q->head_idx != q->tail_idx) {
1317 desc_info = &q->tx_info[q->tail_idx];
1318 desc_info->bytes = 0;
1319 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1320 ionic_tx_clean(q, desc_info, NULL);
1321 if (desc_info->skb) {
1323 bytes += desc_info->bytes;
1324 desc_info->skb = NULL;
1328 if (likely(!ionic_txq_hwstamp_enabled(q))) {
1329 struct netdev_queue *ndq = q_to_ndq(q->lif->netdev, q);
1331 netdev_tx_completed_queue(ndq, pkts, bytes);
1332 netdev_tx_reset_queue(ndq);
1336 static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb)
1340 err = skb_cow_head(skb, 0);
1344 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1345 inner_ip_hdr(skb)->check = 0;
1346 inner_tcp_hdr(skb)->check =
1347 ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
1348 inner_ip_hdr(skb)->daddr,
1350 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1351 inner_tcp_hdr(skb)->check =
1352 ~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
1353 &inner_ipv6_hdr(skb)->daddr,
1360 static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb)
1364 err = skb_cow_head(skb, 0);
1368 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1369 ip_hdr(skb)->check = 0;
1370 tcp_hdr(skb)->check =
1371 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1374 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1375 tcp_v6_gso_csum_prep(skb);
1381 static void ionic_tx_tso_post(struct net_device *netdev, struct ionic_queue *q,
1382 struct ionic_tx_desc_info *desc_info,
1383 struct sk_buff *skb,
1384 dma_addr_t addr, u8 nsge, u16 len,
1385 unsigned int hdrlen, unsigned int mss,
1387 u16 vlan_tci, bool has_vlan,
1388 bool start, bool done)
1390 struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1394 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1395 flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1396 flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0;
1397 flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0;
1399 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr);
1400 desc->cmd = cpu_to_le64(cmd);
1401 desc->len = cpu_to_le16(len);
1402 desc->vlan_tci = cpu_to_le16(vlan_tci);
1403 desc->hdr_len = cpu_to_le16(hdrlen);
1404 desc->mss = cpu_to_le16(mss);
1406 ionic_write_cmb_desc(q, desc);
1409 skb_tx_timestamp(skb);
1410 if (likely(!ionic_txq_hwstamp_enabled(q)))
1411 netdev_tx_sent_queue(q_to_ndq(netdev, q), skb->len);
1412 ionic_txq_post(q, false);
1414 ionic_txq_post(q, done);
1418 static int ionic_tx_tso(struct net_device *netdev, struct ionic_queue *q,
1419 struct sk_buff *skb)
1421 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1422 struct ionic_tx_desc_info *desc_info;
1423 struct ionic_buf_info *buf_info;
1424 struct ionic_txq_sg_elem *elem;
1425 struct ionic_txq_desc *desc;
1426 unsigned int chunk_len;
1427 unsigned int frag_rem;
1428 unsigned int tso_rem;
1429 unsigned int seg_rem;
1430 dma_addr_t desc_addr;
1431 dma_addr_t frag_addr;
1432 unsigned int hdrlen;
1444 desc_info = &q->tx_info[q->head_idx];
1446 if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1450 mss = skb_shinfo(skb)->gso_size;
1451 outer_csum = (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
1455 SKB_GSO_UDP_TUNNEL |
1456 SKB_GSO_UDP_TUNNEL_CSUM));
1457 has_vlan = !!skb_vlan_tag_present(skb);
1458 vlan_tci = skb_vlan_tag_get(skb);
1459 encap = skb->encapsulation;
1461 /* Preload inner-most TCP csum field with IP pseudo hdr
1462 * calculated with IP length set to zero. HW will later
1463 * add in length to each TCP segment resulting from the TSO.
1467 err = ionic_tx_tcp_inner_pseudo_csum(skb);
1469 err = ionic_tx_tcp_pseudo_csum(skb);
1471 /* clean up mapping from ionic_tx_map_skb */
1472 ionic_tx_desc_unmap_bufs(q, desc_info);
1477 hdrlen = skb_inner_tcp_all_headers(skb);
1479 hdrlen = skb_tcp_all_headers(skb);
1481 desc_info->skb = skb;
1482 buf_info = desc_info->bufs;
1484 seg_rem = min(tso_rem, hdrlen + mss);
1491 while (tso_rem > 0) {
1497 /* use fragments until we have enough to post a single descriptor */
1498 while (seg_rem > 0) {
1499 /* if the fragment is exhausted then move to the next one */
1500 if (frag_rem == 0) {
1501 /* grab the next fragment */
1502 frag_addr = buf_info->dma_addr;
1503 frag_rem = buf_info->len;
1506 chunk_len = min(frag_rem, seg_rem);
1508 /* fill main descriptor */
1509 desc = &q->txq[q->head_idx];
1510 elem = ionic_tx_sg_elems(q);
1511 desc_addr = frag_addr;
1512 desc_len = chunk_len;
1514 /* fill sg descriptor */
1515 elem->addr = cpu_to_le64(frag_addr);
1516 elem->len = cpu_to_le16(chunk_len);
1520 frag_addr += chunk_len;
1521 frag_rem -= chunk_len;
1522 tso_rem -= chunk_len;
1523 seg_rem -= chunk_len;
1525 seg_rem = min(tso_rem, mss);
1526 done = (tso_rem == 0);
1527 /* post descriptor */
1528 ionic_tx_tso_post(netdev, q, desc_info, skb,
1529 desc_addr, desc_nsge, desc_len,
1530 hdrlen, mss, outer_csum, vlan_tci, has_vlan,
1533 /* Buffer information is stored with the first tso descriptor */
1534 desc_info = &q->tx_info[q->head_idx];
1535 desc_info->nbufs = 0;
1538 stats->pkts += DIV_ROUND_UP(len - hdrlen, mss);
1539 stats->bytes += len;
1541 stats->tso_bytes = len;
1546 static void ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb,
1547 struct ionic_tx_desc_info *desc_info)
1549 struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1550 struct ionic_buf_info *buf_info = desc_info->bufs;
1551 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1557 has_vlan = !!skb_vlan_tag_present(skb);
1558 encap = skb->encapsulation;
1560 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1561 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1563 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL,
1564 flags, skb_shinfo(skb)->nr_frags,
1565 buf_info->dma_addr);
1566 desc->cmd = cpu_to_le64(cmd);
1567 desc->len = cpu_to_le16(buf_info->len);
1569 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1570 stats->vlan_inserted++;
1574 desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb));
1575 desc->csum_offset = cpu_to_le16(skb->csum_offset);
1577 ionic_write_cmb_desc(q, desc);
1579 if (skb_csum_is_sctp(skb))
1580 stats->crc32_csum++;
1585 static void ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb,
1586 struct ionic_tx_desc_info *desc_info)
1588 struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1589 struct ionic_buf_info *buf_info = desc_info->bufs;
1590 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1596 has_vlan = !!skb_vlan_tag_present(skb);
1597 encap = skb->encapsulation;
1599 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1600 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1602 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
1603 flags, skb_shinfo(skb)->nr_frags,
1604 buf_info->dma_addr);
1605 desc->cmd = cpu_to_le64(cmd);
1606 desc->len = cpu_to_le16(buf_info->len);
1608 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1609 stats->vlan_inserted++;
1613 desc->csum_start = 0;
1614 desc->csum_offset = 0;
1616 ionic_write_cmb_desc(q, desc);
1621 static void ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb,
1622 struct ionic_tx_desc_info *desc_info)
1624 struct ionic_buf_info *buf_info = &desc_info->bufs[1];
1625 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1626 struct ionic_txq_sg_elem *elem;
1629 elem = ionic_tx_sg_elems(q);
1630 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, buf_info++, elem++) {
1631 elem->addr = cpu_to_le64(buf_info->dma_addr);
1632 elem->len = cpu_to_le16(buf_info->len);
1635 stats->frags += skb_shinfo(skb)->nr_frags;
1638 static int ionic_tx(struct net_device *netdev, struct ionic_queue *q,
1639 struct sk_buff *skb)
1641 struct ionic_tx_desc_info *desc_info = &q->tx_info[q->head_idx];
1642 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1643 bool ring_dbell = true;
1645 if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1648 desc_info->skb = skb;
1650 /* set up the initial descriptor */
1651 if (skb->ip_summed == CHECKSUM_PARTIAL)
1652 ionic_tx_calc_csum(q, skb, desc_info);
1654 ionic_tx_calc_no_csum(q, skb, desc_info);
1657 ionic_tx_skb_frags(q, skb, desc_info);
1659 skb_tx_timestamp(skb);
1661 stats->bytes += skb->len;
1663 if (likely(!ionic_txq_hwstamp_enabled(q))) {
1664 struct netdev_queue *ndq = q_to_ndq(netdev, q);
1666 if (unlikely(!ionic_q_has_space(q, MAX_SKB_FRAGS + 1)))
1667 netif_tx_stop_queue(ndq);
1668 ring_dbell = __netdev_tx_sent_queue(ndq, skb->len,
1669 netdev_xmit_more());
1671 ionic_txq_post(q, ring_dbell);
1676 static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb)
1678 int nr_frags = skb_shinfo(skb)->nr_frags;
1679 bool too_many_frags = false;
1691 /* Each desc is mss long max, so a descriptor for each gso_seg */
1692 if (skb_is_gso(skb)) {
1693 ndescs = skb_shinfo(skb)->gso_segs;
1701 if (unlikely(nr_frags > q->max_sg_elems)) {
1702 too_many_frags = true;
1709 /* We need to scan the skb to be sure that none of the MTU sized
1710 * packets in the TSO will require more sgs per descriptor than we
1711 * can support. We loop through the frags, add up the lengths for
1712 * a packet, and count the number of sgs used per packet.
1715 frag = skb_shinfo(skb)->frags;
1716 encap = skb->encapsulation;
1718 /* start with just hdr in first part of first descriptor */
1720 hdrlen = skb_inner_tcp_all_headers(skb);
1722 hdrlen = skb_tcp_all_headers(skb);
1723 seg_rem = min_t(int, tso_rem, hdrlen + skb_shinfo(skb)->gso_size);
1726 while (tso_rem > 0) {
1728 while (seg_rem > 0) {
1731 /* We add the +1 because we can take buffers for one
1732 * more than we have SGs: one for the initial desc data
1733 * in addition to the SG segments that might follow.
1735 if (desc_bufs > q->max_sg_elems + 1) {
1736 too_many_frags = true;
1740 if (frag_rem == 0) {
1741 frag_rem = skb_frag_size(frag);
1744 chunk_len = min(frag_rem, seg_rem);
1745 frag_rem -= chunk_len;
1746 tso_rem -= chunk_len;
1747 seg_rem -= chunk_len;
1750 seg_rem = min_t(int, tso_rem, skb_shinfo(skb)->gso_size);
1754 if (too_many_frags) {
1755 struct ionic_tx_stats *stats = q_to_tx_stats(q);
1757 err = skb_linearize(skb);
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;