net/sfc: add TSO header length check to Tx prepare
[dpdk.git] / drivers / net / sfc / sfc_ef10_tx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2016-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <stdbool.h>
11
12 #include <rte_mbuf.h>
13 #include <rte_io.h>
14 #include <rte_ip.h>
15 #include <rte_tcp.h>
16
17 #include "efx.h"
18 #include "efx_types.h"
19 #include "efx_regs.h"
20 #include "efx_regs_ef10.h"
21
22 #include "sfc_dp_tx.h"
23 #include "sfc_tweak.h"
24 #include "sfc_kvargs.h"
25 #include "sfc_ef10.h"
26 #include "sfc_tso.h"
27
28 #define sfc_ef10_tx_err(dpq, ...) \
29         SFC_DP_LOG(SFC_KVARG_DATAPATH_EF10, ERR, dpq, __VA_ARGS__)
30
31 /** Maximum length of the DMA descriptor data */
32 #define SFC_EF10_TX_DMA_DESC_LEN_MAX \
33         ((1u << ESF_DZ_TX_KER_BYTE_CNT_WIDTH) - 1)
34
35 /**
36  * Maximum number of descriptors/buffers in the Tx ring.
37  * It should guarantee that corresponding event queue never overfill.
38  * EF10 native datapath uses event queue of the same size as Tx queue.
39  * Maximum number of events on datapath can be estimated as number of
40  * Tx queue entries (one event per Tx buffer in the worst case) plus
41  * Tx error and flush events.
42  */
43 #define SFC_EF10_TXQ_LIMIT(_ndesc) \
44         ((_ndesc) - 1 /* head must not step on tail */ - \
45          (SFC_EF10_EV_PER_CACHE_LINE - 1) /* max unused EvQ entries */ - \
46          1 /* Rx error */ - 1 /* flush */)
47
48 struct sfc_ef10_tx_sw_desc {
49         struct rte_mbuf                 *mbuf;
50 };
51
52 struct sfc_ef10_txq {
53         unsigned int                    flags;
54 #define SFC_EF10_TXQ_STARTED            0x1
55 #define SFC_EF10_TXQ_NOT_RUNNING        0x2
56 #define SFC_EF10_TXQ_EXCEPTION          0x4
57
58         unsigned int                    ptr_mask;
59         unsigned int                    added;
60         unsigned int                    completed;
61         unsigned int                    max_fill_level;
62         unsigned int                    free_thresh;
63         unsigned int                    evq_read_ptr;
64         struct sfc_ef10_tx_sw_desc      *sw_ring;
65         efx_qword_t                     *txq_hw_ring;
66         volatile void                   *doorbell;
67         efx_qword_t                     *evq_hw_ring;
68         uint8_t                         *tsoh;
69         rte_iova_t                      tsoh_iova;
70         uint16_t                        tso_tcp_header_offset_limit;
71
72         /* Datapath transmit queue anchor */
73         struct sfc_dp_txq               dp;
74 };
75
76 static inline struct sfc_ef10_txq *
77 sfc_ef10_txq_by_dp_txq(struct sfc_dp_txq *dp_txq)
78 {
79         return container_of(dp_txq, struct sfc_ef10_txq, dp);
80 }
81
82 static bool
83 sfc_ef10_tx_get_event(struct sfc_ef10_txq *txq, efx_qword_t *tx_ev)
84 {
85         volatile efx_qword_t *evq_hw_ring = txq->evq_hw_ring;
86
87         /*
88          * Exception flag is set when reap is done.
89          * It is never done twice per packet burst get and absence of
90          * the flag is checked on burst get entry.
91          */
92         SFC_ASSERT((txq->flags & SFC_EF10_TXQ_EXCEPTION) == 0);
93
94         *tx_ev = evq_hw_ring[txq->evq_read_ptr & txq->ptr_mask];
95
96         if (!sfc_ef10_ev_present(*tx_ev))
97                 return false;
98
99         if (unlikely(EFX_QWORD_FIELD(*tx_ev, FSF_AZ_EV_CODE) !=
100                      FSE_AZ_EV_CODE_TX_EV)) {
101                 /*
102                  * Do not move read_ptr to keep the event for exception
103                  * handling by the control path.
104                  */
105                 txq->flags |= SFC_EF10_TXQ_EXCEPTION;
106                 sfc_ef10_tx_err(&txq->dp.dpq,
107                                 "TxQ exception at EvQ read ptr %#x",
108                                 txq->evq_read_ptr);
109                 return false;
110         }
111
112         txq->evq_read_ptr++;
113         return true;
114 }
115
116 static unsigned int
117 sfc_ef10_tx_process_events(struct sfc_ef10_txq *txq)
118 {
119         const unsigned int curr_done = txq->completed - 1;
120         unsigned int anew_done = curr_done;
121         efx_qword_t tx_ev;
122
123         while (sfc_ef10_tx_get_event(txq, &tx_ev)) {
124                 /*
125                  * DROP_EVENT is an internal to the NIC, software should
126                  * never see it and, therefore, may ignore it.
127                  */
128
129                 /* Update the latest done descriptor */
130                 anew_done = EFX_QWORD_FIELD(tx_ev, ESF_DZ_TX_DESCR_INDX);
131         }
132         return (anew_done - curr_done) & txq->ptr_mask;
133 }
134
135 static void
136 sfc_ef10_tx_reap(struct sfc_ef10_txq *txq)
137 {
138         const unsigned int old_read_ptr = txq->evq_read_ptr;
139         const unsigned int ptr_mask = txq->ptr_mask;
140         unsigned int completed = txq->completed;
141         unsigned int pending = completed;
142
143         pending += sfc_ef10_tx_process_events(txq);
144
145         if (pending != completed) {
146                 struct rte_mbuf *bulk[SFC_TX_REAP_BULK_SIZE];
147                 unsigned int nb = 0;
148
149                 do {
150                         struct sfc_ef10_tx_sw_desc *txd;
151                         struct rte_mbuf *m;
152
153                         txd = &txq->sw_ring[completed & ptr_mask];
154                         if (txd->mbuf == NULL)
155                                 continue;
156
157                         m = rte_pktmbuf_prefree_seg(txd->mbuf);
158                         txd->mbuf = NULL;
159                         if (m == NULL)
160                                 continue;
161
162                         if ((nb == RTE_DIM(bulk)) ||
163                             ((nb != 0) && (m->pool != bulk[0]->pool))) {
164                                 rte_mempool_put_bulk(bulk[0]->pool,
165                                                      (void *)bulk, nb);
166                                 nb = 0;
167                         }
168
169                         bulk[nb++] = m;
170                 } while (++completed != pending);
171
172                 if (nb != 0)
173                         rte_mempool_put_bulk(bulk[0]->pool, (void *)bulk, nb);
174
175                 txq->completed = completed;
176         }
177
178         sfc_ef10_ev_qclear(txq->evq_hw_ring, ptr_mask, old_read_ptr,
179                            txq->evq_read_ptr);
180 }
181
182 static void
183 sfc_ef10_tx_qdesc_dma_create(rte_iova_t addr, uint16_t size, bool eop,
184                              efx_qword_t *edp)
185 {
186         EFX_POPULATE_QWORD_4(*edp,
187                              ESF_DZ_TX_KER_TYPE, 0,
188                              ESF_DZ_TX_KER_CONT, !eop,
189                              ESF_DZ_TX_KER_BYTE_CNT, size,
190                              ESF_DZ_TX_KER_BUF_ADDR, addr);
191 }
192
193 static void
194 sfc_ef10_tx_qdesc_tso2_create(struct sfc_ef10_txq * const txq,
195                               unsigned int added, uint16_t ipv4_id,
196                               uint16_t outer_ipv4_id, uint32_t tcp_seq,
197                               uint16_t tcp_mss)
198 {
199         EFX_POPULATE_QWORD_5(txq->txq_hw_ring[added & txq->ptr_mask],
200                             ESF_DZ_TX_DESC_IS_OPT, 1,
201                             ESF_DZ_TX_OPTION_TYPE,
202                             ESE_DZ_TX_OPTION_DESC_TSO,
203                             ESF_DZ_TX_TSO_OPTION_TYPE,
204                             ESE_DZ_TX_TSO_OPTION_DESC_FATSO2A,
205                             ESF_DZ_TX_TSO_IP_ID, ipv4_id,
206                             ESF_DZ_TX_TSO_TCP_SEQNO, tcp_seq);
207         EFX_POPULATE_QWORD_5(txq->txq_hw_ring[(added + 1) & txq->ptr_mask],
208                             ESF_DZ_TX_DESC_IS_OPT, 1,
209                             ESF_DZ_TX_OPTION_TYPE,
210                             ESE_DZ_TX_OPTION_DESC_TSO,
211                             ESF_DZ_TX_TSO_OPTION_TYPE,
212                             ESE_DZ_TX_TSO_OPTION_DESC_FATSO2B,
213                             ESF_DZ_TX_TSO_TCP_MSS, tcp_mss,
214                             ESF_DZ_TX_TSO_OUTER_IPID, outer_ipv4_id);
215 }
216
217 static inline void
218 sfc_ef10_tx_qpush(struct sfc_ef10_txq *txq, unsigned int added,
219                   unsigned int pushed)
220 {
221         efx_qword_t desc;
222         efx_oword_t oword;
223
224         /*
225          * This improves performance by pushing a TX descriptor at the same
226          * time as the doorbell. The descriptor must be added to the TXQ,
227          * so that can be used if the hardware decides not to use the pushed
228          * descriptor.
229          */
230         desc.eq_u64[0] = txq->txq_hw_ring[pushed & txq->ptr_mask].eq_u64[0];
231         EFX_POPULATE_OWORD_3(oword,
232                 ERF_DZ_TX_DESC_WPTR, added & txq->ptr_mask,
233                 ERF_DZ_TX_DESC_HWORD, EFX_QWORD_FIELD(desc, EFX_DWORD_1),
234                 ERF_DZ_TX_DESC_LWORD, EFX_QWORD_FIELD(desc, EFX_DWORD_0));
235
236         /* DMA sync to device is not required */
237
238         /*
239          * rte_io_wmb() which guarantees that the STORE operations
240          * (i.e. Tx and event descriptor updates) that precede
241          * the rte_io_wmb() call are visible to NIC before the STORE
242          * operations that follow it (i.e. doorbell write).
243          */
244         rte_io_wmb();
245
246         *(volatile __m128i *)txq->doorbell = oword.eo_u128[0];
247 }
248
249 static unsigned int
250 sfc_ef10_tx_pkt_descs_max(const struct rte_mbuf *m)
251 {
252         unsigned int extra_descs_per_seg;
253         unsigned int extra_descs_per_pkt;
254
255         /*
256          * VLAN offload is not supported yet, so no extra descriptors
257          * are required for VLAN option descriptor.
258          */
259
260 /** Maximum length of the mbuf segment data */
261 #define SFC_MBUF_SEG_LEN_MAX            UINT16_MAX
262         RTE_BUILD_BUG_ON(sizeof(m->data_len) != 2);
263
264         /*
265          * Each segment is already counted once below.  So, calculate
266          * how many extra DMA descriptors may be required per segment in
267          * the worst case because of maximum DMA descriptor length limit.
268          * If maximum segment length is less or equal to maximum DMA
269          * descriptor length, no extra DMA descriptors are required.
270          */
271         extra_descs_per_seg =
272                 (SFC_MBUF_SEG_LEN_MAX - 1) / SFC_EF10_TX_DMA_DESC_LEN_MAX;
273
274 /** Maximum length of the packet */
275 #define SFC_MBUF_PKT_LEN_MAX            UINT32_MAX
276         RTE_BUILD_BUG_ON(sizeof(m->pkt_len) != 4);
277
278         /*
279          * One more limitation on maximum number of extra DMA descriptors
280          * comes from slicing entire packet because of DMA descriptor length
281          * limit taking into account that there is at least one segment
282          * which is already counted below (so division of the maximum
283          * packet length minus one with round down).
284          * TSO is not supported yet, so packet length is limited by
285          * maximum PDU size.
286          */
287         extra_descs_per_pkt =
288                 (RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
289                          SFC_MBUF_PKT_LEN_MAX) - 1) /
290                 SFC_EF10_TX_DMA_DESC_LEN_MAX;
291
292         return m->nb_segs + RTE_MIN(m->nb_segs * extra_descs_per_seg,
293                                     extra_descs_per_pkt);
294 }
295
296 static bool
297 sfc_ef10_try_reap(struct sfc_ef10_txq * const txq, unsigned int added,
298                   unsigned int needed_desc, unsigned int *dma_desc_space,
299                   bool *reap_done)
300 {
301         if (*reap_done)
302                 return false;
303
304         if (added != txq->added) {
305                 sfc_ef10_tx_qpush(txq, added, txq->added);
306                 txq->added = added;
307         }
308
309         sfc_ef10_tx_reap(txq);
310         *reap_done = true;
311
312         /*
313          * Recalculate DMA descriptor space since Tx reap may change
314          * the number of completed descriptors
315          */
316         *dma_desc_space = txq->max_fill_level -
317                 (added - txq->completed);
318
319         return (needed_desc <= *dma_desc_space);
320 }
321
322 static uint16_t
323 sfc_ef10_prepare_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
324                       uint16_t nb_pkts)
325 {
326         struct sfc_ef10_txq * const txq = sfc_ef10_txq_by_dp_txq(tx_queue);
327         uint16_t i;
328
329         for (i = 0; i < nb_pkts; i++) {
330                 struct rte_mbuf *m = tx_pkts[i];
331                 int ret;
332
333 #ifdef RTE_LIBRTE_SFC_EFX_DEBUG
334                 /*
335                  * In non-TSO case, check that a packet segments do not exceed
336                  * the size limit. Perform the check in debug mode since MTU
337                  * more than 9k is not supported, but the limit here is 16k-1.
338                  */
339                 if (!(m->ol_flags & PKT_TX_TCP_SEG)) {
340                         struct rte_mbuf *m_seg;
341
342                         for (m_seg = m; m_seg != NULL; m_seg = m_seg->next) {
343                                 if (m_seg->data_len >
344                                     SFC_EF10_TX_DMA_DESC_LEN_MAX) {
345                                         rte_errno = EINVAL;
346                                         break;
347                                 }
348                         }
349                 }
350 #endif
351                 ret = sfc_dp_tx_prepare_pkt(m,
352                                 txq->tso_tcp_header_offset_limit,
353                                 txq->max_fill_level,
354                                 SFC_EF10_TSO_OPT_DESCS_NUM, 0);
355                 if (unlikely(ret != 0)) {
356                         rte_errno = ret;
357                         break;
358                 }
359         }
360
361         return i;
362 }
363
364 static int
365 sfc_ef10_xmit_tso_pkt(struct sfc_ef10_txq * const txq, struct rte_mbuf *m_seg,
366                       unsigned int *added, unsigned int *dma_desc_space,
367                       bool *reap_done)
368 {
369         size_t iph_off = m_seg->l2_len;
370         size_t tcph_off = m_seg->l2_len + m_seg->l3_len;
371         size_t header_len = m_seg->l2_len + m_seg->l3_len + m_seg->l4_len;
372         /* Offset of the payload in the last segment that contains the header */
373         size_t in_off = 0;
374         const struct tcp_hdr *th;
375         uint16_t packet_id = 0;
376         uint32_t sent_seq;
377         uint8_t *hdr_addr;
378         rte_iova_t hdr_iova;
379         struct rte_mbuf *first_m_seg = m_seg;
380         unsigned int pkt_start = *added;
381         unsigned int needed_desc;
382         struct rte_mbuf *m_seg_to_free_up_to = first_m_seg;
383         bool eop;
384
385         /*
386          * Preliminary estimation of required DMA descriptors, including extra
387          * descriptor for TSO header that is needed when the header is
388          * separated from payload in one segment. It does not include
389          * extra descriptors that may appear when a big segment is split across
390          * several descriptors.
391          */
392         needed_desc = m_seg->nb_segs +
393                         (unsigned int)SFC_EF10_TSO_OPT_DESCS_NUM +
394                         (unsigned int)SFC_EF10_TSO_HDR_DESCS_NUM;
395
396         if (needed_desc > *dma_desc_space &&
397             !sfc_ef10_try_reap(txq, pkt_start, needed_desc,
398                                dma_desc_space, reap_done)) {
399                 /*
400                  * If a future Tx reap may increase available DMA descriptor
401                  * space, do not try to send the packet.
402                  */
403                 if (txq->completed != pkt_start)
404                         return ENOSPC;
405                 /*
406                  * Do not allow to send packet if the maximum DMA
407                  * descriptor space is not sufficient to hold TSO
408                  * descriptors, header descriptor and at least 1
409                  * segment descriptor.
410                  */
411                 if (*dma_desc_space < SFC_EF10_TSO_OPT_DESCS_NUM +
412                                 SFC_EF10_TSO_HDR_DESCS_NUM + 1)
413                         return EMSGSIZE;
414         }
415
416         /* Check if the header is not fragmented */
417         if (rte_pktmbuf_data_len(m_seg) >= header_len) {
418                 hdr_addr = rte_pktmbuf_mtod(m_seg, uint8_t *);
419                 hdr_iova = rte_mbuf_data_iova(m_seg);
420                 if (rte_pktmbuf_data_len(m_seg) == header_len) {
421                         /* Cannot send a packet that consists only of header */
422                         if (unlikely(m_seg->next == NULL))
423                                 return EMSGSIZE;
424                         /*
425                          * Associate header mbuf with header descriptor
426                          * which is located after TSO descriptors.
427                          */
428                         txq->sw_ring[(pkt_start + SFC_EF10_TSO_OPT_DESCS_NUM) &
429                                      txq->ptr_mask].mbuf = m_seg;
430                         m_seg = m_seg->next;
431                         in_off = 0;
432
433                         /*
434                          * If there is no payload offset (payload starts at the
435                          * beginning of a segment) then an extra descriptor for
436                          * separated header is not needed.
437                          */
438                         needed_desc--;
439                 } else {
440                         in_off = header_len;
441                 }
442         } else {
443                 unsigned int copied_segs;
444                 unsigned int hdr_addr_off = (*added & txq->ptr_mask) *
445                                 SFC_TSOH_STD_LEN;
446
447                 /*
448                  * Discard a packet if header linearization is needed but
449                  * the header is too big.
450                  * Duplicate Tx prepare check here to avoid spoil of
451                  * memory if Tx prepare is skipped.
452                  */
453                 if (unlikely(header_len > SFC_TSOH_STD_LEN))
454                         return EMSGSIZE;
455
456                 hdr_addr = txq->tsoh + hdr_addr_off;
457                 hdr_iova = txq->tsoh_iova + hdr_addr_off;
458                 copied_segs = sfc_tso_prepare_header(hdr_addr, header_len,
459                                                      &m_seg, &in_off);
460
461                 /* Cannot send a packet that consists only of header */
462                 if (unlikely(m_seg == NULL))
463                         return EMSGSIZE;
464
465                 m_seg_to_free_up_to = m_seg;
466                 /*
467                  * Reduce the number of needed descriptors by the number of
468                  * segments that entirely consist of header data.
469                  */
470                 needed_desc -= copied_segs;
471
472                 /* Extra descriptor for separated header is not needed */
473                 if (in_off == 0)
474                         needed_desc--;
475         }
476
477         /*
478          * Tx prepare has debug-only checks that offload flags are correctly
479          * filled in in TSO mbuf. Use zero IPID if there is no IPv4 flag.
480          * If the packet is still IPv4, HW will simply start from zero IPID.
481          */
482         if (first_m_seg->ol_flags & PKT_TX_IPV4) {
483                 const struct ipv4_hdr *iphe4;
484
485                 iphe4 = (const struct ipv4_hdr *)(hdr_addr + iph_off);
486                 rte_memcpy(&packet_id, &iphe4->packet_id, sizeof(uint16_t));
487                 packet_id = rte_be_to_cpu_16(packet_id);
488         }
489
490         th = (const struct tcp_hdr *)(hdr_addr + tcph_off);
491         rte_memcpy(&sent_seq, &th->sent_seq, sizeof(uint32_t));
492         sent_seq = rte_be_to_cpu_32(sent_seq);
493
494         sfc_ef10_tx_qdesc_tso2_create(txq, *added, packet_id, 0, sent_seq,
495                         first_m_seg->tso_segsz);
496         (*added) += SFC_EF10_TSO_OPT_DESCS_NUM;
497
498         sfc_ef10_tx_qdesc_dma_create(hdr_iova, header_len, false,
499                         &txq->txq_hw_ring[(*added) & txq->ptr_mask]);
500         (*added)++;
501
502         do {
503                 rte_iova_t next_frag = rte_mbuf_data_iova(m_seg);
504                 unsigned int seg_len = rte_pktmbuf_data_len(m_seg);
505                 unsigned int id;
506
507                 next_frag += in_off;
508                 seg_len -= in_off;
509                 in_off = 0;
510
511                 do {
512                         rte_iova_t frag_addr = next_frag;
513                         size_t frag_len;
514
515                         frag_len = RTE_MIN(seg_len,
516                                            SFC_EF10_TX_DMA_DESC_LEN_MAX);
517
518                         next_frag += frag_len;
519                         seg_len -= frag_len;
520
521                         eop = (seg_len == 0 && m_seg->next == NULL);
522
523                         id = (*added) & txq->ptr_mask;
524                         (*added)++;
525
526                         /*
527                          * Initially we assume that one DMA descriptor is needed
528                          * for every segment. When the segment is split across
529                          * several DMA descriptors, increase the estimation.
530                          */
531                         needed_desc += (seg_len != 0);
532
533                         /*
534                          * When no more descriptors can be added, but not all
535                          * segments are processed.
536                          */
537                         if (*added - pkt_start == *dma_desc_space &&
538                             !eop &&
539                             !sfc_ef10_try_reap(txq, pkt_start, needed_desc,
540                                                 dma_desc_space, reap_done)) {
541                                 struct rte_mbuf *m;
542                                 struct rte_mbuf *m_next;
543
544                                 if (txq->completed != pkt_start) {
545                                         unsigned int i;
546
547                                         /*
548                                          * Reset mbuf associations with added
549                                          * descriptors.
550                                          */
551                                         for (i = pkt_start; i != *added; i++) {
552                                                 id = i & txq->ptr_mask;
553                                                 txq->sw_ring[id].mbuf = NULL;
554                                         }
555                                         return ENOSPC;
556                                 }
557
558                                 /* Free the segments that cannot be sent */
559                                 for (m = m_seg->next; m != NULL; m = m_next) {
560                                         m_next = m->next;
561                                         rte_pktmbuf_free_seg(m);
562                                 }
563                                 eop = true;
564                                 /* Ignore the rest of the segment */
565                                 seg_len = 0;
566                         }
567
568                         sfc_ef10_tx_qdesc_dma_create(frag_addr, frag_len,
569                                         eop, &txq->txq_hw_ring[id]);
570
571                 } while (seg_len != 0);
572
573                 txq->sw_ring[id].mbuf = m_seg;
574
575                 m_seg = m_seg->next;
576         } while (!eop);
577
578         /*
579          * Free segments which content was entirely copied to the TSO header
580          * memory space of Tx queue
581          */
582         for (m_seg = first_m_seg; m_seg != m_seg_to_free_up_to;) {
583                 struct rte_mbuf *seg_to_free = m_seg;
584
585                 m_seg = m_seg->next;
586                 rte_pktmbuf_free_seg(seg_to_free);
587         }
588
589         return 0;
590 }
591
592 static uint16_t
593 sfc_ef10_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
594 {
595         struct sfc_ef10_txq * const txq = sfc_ef10_txq_by_dp_txq(tx_queue);
596         unsigned int added;
597         unsigned int dma_desc_space;
598         bool reap_done;
599         struct rte_mbuf **pktp;
600         struct rte_mbuf **pktp_end;
601
602         if (unlikely(txq->flags &
603                      (SFC_EF10_TXQ_NOT_RUNNING | SFC_EF10_TXQ_EXCEPTION)))
604                 return 0;
605
606         added = txq->added;
607         dma_desc_space = txq->max_fill_level - (added - txq->completed);
608
609         reap_done = (dma_desc_space < txq->free_thresh);
610         if (reap_done) {
611                 sfc_ef10_tx_reap(txq);
612                 dma_desc_space = txq->max_fill_level - (added - txq->completed);
613         }
614
615         for (pktp = &tx_pkts[0], pktp_end = &tx_pkts[nb_pkts];
616              pktp != pktp_end;
617              ++pktp) {
618                 struct rte_mbuf *m_seg = *pktp;
619                 unsigned int pkt_start = added;
620                 uint32_t pkt_len;
621
622                 if (likely(pktp + 1 != pktp_end))
623                         rte_mbuf_prefetch_part1(pktp[1]);
624
625                 if (m_seg->ol_flags & PKT_TX_TCP_SEG) {
626                         int rc;
627
628                         rc = sfc_ef10_xmit_tso_pkt(txq, m_seg, &added,
629                                         &dma_desc_space, &reap_done);
630                         if (rc != 0) {
631                                 added = pkt_start;
632
633                                 /* Packet can be sent in following xmit calls */
634                                 if (likely(rc == ENOSPC))
635                                         break;
636
637                                 /*
638                                  * Packet cannot be sent, tell RTE that
639                                  * it is sent, but actually drop it and
640                                  * continue with another packet
641                                  */
642                                 rte_pktmbuf_free(*pktp);
643                                 continue;
644                         }
645
646                         goto dma_desc_space_update;
647                 }
648
649                 if (sfc_ef10_tx_pkt_descs_max(m_seg) > dma_desc_space) {
650                         if (reap_done)
651                                 break;
652
653                         /* Push already prepared descriptors before polling */
654                         if (added != txq->added) {
655                                 sfc_ef10_tx_qpush(txq, added, txq->added);
656                                 txq->added = added;
657                         }
658
659                         sfc_ef10_tx_reap(txq);
660                         reap_done = true;
661                         dma_desc_space = txq->max_fill_level -
662                                 (added - txq->completed);
663                         if (sfc_ef10_tx_pkt_descs_max(m_seg) > dma_desc_space)
664                                 break;
665                 }
666
667                 pkt_len = m_seg->pkt_len;
668                 do {
669                         rte_iova_t seg_addr = rte_mbuf_data_iova(m_seg);
670                         unsigned int seg_len = rte_pktmbuf_data_len(m_seg);
671                         unsigned int id = added & txq->ptr_mask;
672
673                         SFC_ASSERT(seg_len <= SFC_EF10_TX_DMA_DESC_LEN_MAX);
674
675                         pkt_len -= seg_len;
676
677                         sfc_ef10_tx_qdesc_dma_create(seg_addr,
678                                 seg_len, (pkt_len == 0),
679                                 &txq->txq_hw_ring[id]);
680
681                         /*
682                          * rte_pktmbuf_free() is commonly used in DPDK for
683                          * recycling packets - the function checks every
684                          * segment's reference counter and returns the
685                          * buffer to its pool whenever possible;
686                          * nevertheless, freeing mbuf segments one by one
687                          * may entail some performance decline;
688                          * from this point, sfc_efx_tx_reap() does the same job
689                          * on its own and frees buffers in bulks (all mbufs
690                          * within a bulk belong to the same pool);
691                          * from this perspective, individual segment pointers
692                          * must be associated with the corresponding SW
693                          * descriptors independently so that only one loop
694                          * is sufficient on reap to inspect all the buffers
695                          */
696                         txq->sw_ring[id].mbuf = m_seg;
697
698                         ++added;
699
700                 } while ((m_seg = m_seg->next) != 0);
701
702 dma_desc_space_update:
703                 dma_desc_space -= (added - pkt_start);
704         }
705
706         if (likely(added != txq->added)) {
707                 sfc_ef10_tx_qpush(txq, added, txq->added);
708                 txq->added = added;
709         }
710
711 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
712         if (!reap_done)
713                 sfc_ef10_tx_reap(txq);
714 #endif
715
716         return pktp - &tx_pkts[0];
717 }
718
719 static void
720 sfc_ef10_simple_tx_reap(struct sfc_ef10_txq *txq)
721 {
722         const unsigned int old_read_ptr = txq->evq_read_ptr;
723         const unsigned int ptr_mask = txq->ptr_mask;
724         unsigned int completed = txq->completed;
725         unsigned int pending = completed;
726
727         pending += sfc_ef10_tx_process_events(txq);
728
729         if (pending != completed) {
730                 struct rte_mbuf *bulk[SFC_TX_REAP_BULK_SIZE];
731                 unsigned int nb = 0;
732
733                 do {
734                         struct sfc_ef10_tx_sw_desc *txd;
735
736                         txd = &txq->sw_ring[completed & ptr_mask];
737
738                         if (nb == RTE_DIM(bulk)) {
739                                 rte_mempool_put_bulk(bulk[0]->pool,
740                                                      (void *)bulk, nb);
741                                 nb = 0;
742                         }
743
744                         bulk[nb++] = txd->mbuf;
745                 } while (++completed != pending);
746
747                 rte_mempool_put_bulk(bulk[0]->pool, (void *)bulk, nb);
748
749                 txq->completed = completed;
750         }
751
752         sfc_ef10_ev_qclear(txq->evq_hw_ring, ptr_mask, old_read_ptr,
753                            txq->evq_read_ptr);
754 }
755
756 #ifdef RTE_LIBRTE_SFC_EFX_DEBUG
757 static uint16_t
758 sfc_ef10_simple_prepare_pkts(__rte_unused void *tx_queue,
759                              struct rte_mbuf **tx_pkts,
760                              uint16_t nb_pkts)
761 {
762         uint16_t i;
763
764         for (i = 0; i < nb_pkts; i++) {
765                 struct rte_mbuf *m = tx_pkts[i];
766                 int ret;
767
768                 ret = rte_validate_tx_offload(m);
769                 if (unlikely(ret != 0)) {
770                         /*
771                          * Negative error code is returned by
772                          * rte_validate_tx_offload(), but positive are used
773                          * inside net/sfc PMD.
774                          */
775                         SFC_ASSERT(ret < 0);
776                         rte_errno = -ret;
777                         break;
778                 }
779
780                 /* ef10_simple does not support TSO and VLAN insertion */
781                 if (unlikely(m->ol_flags &
782                              (PKT_TX_TCP_SEG | PKT_TX_VLAN_PKT))) {
783                         rte_errno = ENOTSUP;
784                         break;
785                 }
786
787                 /* ef10_simple does not support scattered packets */
788                 if (unlikely(m->nb_segs != 1)) {
789                         rte_errno = ENOTSUP;
790                         break;
791                 }
792
793                 /*
794                  * ef10_simple requires fast-free which ignores reference
795                  * counters
796                  */
797                 if (unlikely(rte_mbuf_refcnt_read(m) != 1)) {
798                         rte_errno = ENOTSUP;
799                         break;
800                 }
801
802                 /* ef10_simple requires single pool for all packets */
803                 if (unlikely(m->pool != tx_pkts[0]->pool)) {
804                         rte_errno = ENOTSUP;
805                         break;
806                 }
807         }
808
809         return i;
810 }
811 #endif
812
813 static uint16_t
814 sfc_ef10_simple_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
815                           uint16_t nb_pkts)
816 {
817         struct sfc_ef10_txq * const txq = sfc_ef10_txq_by_dp_txq(tx_queue);
818         unsigned int ptr_mask;
819         unsigned int added;
820         unsigned int dma_desc_space;
821         bool reap_done;
822         struct rte_mbuf **pktp;
823         struct rte_mbuf **pktp_end;
824
825         if (unlikely(txq->flags &
826                      (SFC_EF10_TXQ_NOT_RUNNING | SFC_EF10_TXQ_EXCEPTION)))
827                 return 0;
828
829         ptr_mask = txq->ptr_mask;
830         added = txq->added;
831         dma_desc_space = txq->max_fill_level - (added - txq->completed);
832
833         reap_done = (dma_desc_space < RTE_MAX(txq->free_thresh, nb_pkts));
834         if (reap_done) {
835                 sfc_ef10_simple_tx_reap(txq);
836                 dma_desc_space = txq->max_fill_level - (added - txq->completed);
837         }
838
839         pktp_end = &tx_pkts[MIN(nb_pkts, dma_desc_space)];
840         for (pktp = &tx_pkts[0]; pktp != pktp_end; ++pktp) {
841                 struct rte_mbuf *pkt = *pktp;
842                 unsigned int id = added & ptr_mask;
843
844                 SFC_ASSERT(rte_pktmbuf_data_len(pkt) <=
845                            SFC_EF10_TX_DMA_DESC_LEN_MAX);
846
847                 sfc_ef10_tx_qdesc_dma_create(rte_mbuf_data_iova(pkt),
848                                              rte_pktmbuf_data_len(pkt),
849                                              true, &txq->txq_hw_ring[id]);
850
851                 txq->sw_ring[id].mbuf = pkt;
852
853                 ++added;
854         }
855
856         if (likely(added != txq->added)) {
857                 sfc_ef10_tx_qpush(txq, added, txq->added);
858                 txq->added = added;
859         }
860
861 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
862         if (!reap_done)
863                 sfc_ef10_simple_tx_reap(txq);
864 #endif
865
866         return pktp - &tx_pkts[0];
867 }
868
869 static sfc_dp_tx_get_dev_info_t sfc_ef10_get_dev_info;
870 static void
871 sfc_ef10_get_dev_info(struct rte_eth_dev_info *dev_info)
872 {
873         /*
874          * Number of descriptors just defines maximum number of pushed
875          * descriptors (fill level).
876          */
877         dev_info->tx_desc_lim.nb_min = 1;
878         dev_info->tx_desc_lim.nb_align = 1;
879 }
880
881 static sfc_dp_tx_qsize_up_rings_t sfc_ef10_tx_qsize_up_rings;
882 static int
883 sfc_ef10_tx_qsize_up_rings(uint16_t nb_tx_desc,
884                            struct sfc_dp_tx_hw_limits *limits,
885                            unsigned int *txq_entries,
886                            unsigned int *evq_entries,
887                            unsigned int *txq_max_fill_level)
888 {
889         /*
890          * rte_ethdev API guarantees that the number meets min, max and
891          * alignment requirements.
892          */
893         if (nb_tx_desc <= limits->txq_min_entries)
894                 *txq_entries = limits->txq_min_entries;
895         else
896                 *txq_entries = rte_align32pow2(nb_tx_desc);
897
898         *evq_entries = *txq_entries;
899
900         *txq_max_fill_level = RTE_MIN(nb_tx_desc,
901                                       SFC_EF10_TXQ_LIMIT(*evq_entries));
902         return 0;
903 }
904
905 static sfc_dp_tx_qcreate_t sfc_ef10_tx_qcreate;
906 static int
907 sfc_ef10_tx_qcreate(uint16_t port_id, uint16_t queue_id,
908                     const struct rte_pci_addr *pci_addr, int socket_id,
909                     const struct sfc_dp_tx_qcreate_info *info,
910                     struct sfc_dp_txq **dp_txqp)
911 {
912         struct sfc_ef10_txq *txq;
913         int rc;
914
915         rc = EINVAL;
916         if (info->txq_entries != info->evq_entries)
917                 goto fail_bad_args;
918
919         rc = ENOMEM;
920         txq = rte_zmalloc_socket("sfc-ef10-txq", sizeof(*txq),
921                                  RTE_CACHE_LINE_SIZE, socket_id);
922         if (txq == NULL)
923                 goto fail_txq_alloc;
924
925         sfc_dp_queue_init(&txq->dp.dpq, port_id, queue_id, pci_addr);
926
927         rc = ENOMEM;
928         txq->sw_ring = rte_calloc_socket("sfc-ef10-txq-sw_ring",
929                                          info->txq_entries,
930                                          sizeof(*txq->sw_ring),
931                                          RTE_CACHE_LINE_SIZE, socket_id);
932         if (txq->sw_ring == NULL)
933                 goto fail_sw_ring_alloc;
934
935         if (info->offloads & DEV_TX_OFFLOAD_TCP_TSO) {
936                 txq->tsoh = rte_calloc_socket("sfc-ef10-txq-tsoh",
937                                               info->txq_entries,
938                                               SFC_TSOH_STD_LEN,
939                                               RTE_CACHE_LINE_SIZE,
940                                               socket_id);
941                 if (txq->tsoh == NULL)
942                         goto fail_tsoh_alloc;
943
944                 txq->tsoh_iova = rte_malloc_virt2iova(txq->tsoh);
945         }
946
947         txq->flags = SFC_EF10_TXQ_NOT_RUNNING;
948         txq->ptr_mask = info->txq_entries - 1;
949         txq->max_fill_level = info->max_fill_level;
950         txq->free_thresh = info->free_thresh;
951         txq->txq_hw_ring = info->txq_hw_ring;
952         txq->doorbell = (volatile uint8_t *)info->mem_bar +
953                         ER_DZ_TX_DESC_UPD_REG_OFST +
954                         (info->hw_index << info->vi_window_shift);
955         txq->evq_hw_ring = info->evq_hw_ring;
956         txq->tso_tcp_header_offset_limit = info->tso_tcp_header_offset_limit;
957
958         *dp_txqp = &txq->dp;
959         return 0;
960
961 fail_tsoh_alloc:
962         rte_free(txq->sw_ring);
963
964 fail_sw_ring_alloc:
965         rte_free(txq);
966
967 fail_txq_alloc:
968 fail_bad_args:
969         return rc;
970 }
971
972 static sfc_dp_tx_qdestroy_t sfc_ef10_tx_qdestroy;
973 static void
974 sfc_ef10_tx_qdestroy(struct sfc_dp_txq *dp_txq)
975 {
976         struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
977
978         rte_free(txq->tsoh);
979         rte_free(txq->sw_ring);
980         rte_free(txq);
981 }
982
983 static sfc_dp_tx_qstart_t sfc_ef10_tx_qstart;
984 static int
985 sfc_ef10_tx_qstart(struct sfc_dp_txq *dp_txq, unsigned int evq_read_ptr,
986                    unsigned int txq_desc_index)
987 {
988         struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
989
990         txq->evq_read_ptr = evq_read_ptr;
991         txq->added = txq->completed = txq_desc_index;
992
993         txq->flags |= SFC_EF10_TXQ_STARTED;
994         txq->flags &= ~(SFC_EF10_TXQ_NOT_RUNNING | SFC_EF10_TXQ_EXCEPTION);
995
996         return 0;
997 }
998
999 static sfc_dp_tx_qstop_t sfc_ef10_tx_qstop;
1000 static void
1001 sfc_ef10_tx_qstop(struct sfc_dp_txq *dp_txq, unsigned int *evq_read_ptr)
1002 {
1003         struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
1004
1005         txq->flags |= SFC_EF10_TXQ_NOT_RUNNING;
1006
1007         *evq_read_ptr = txq->evq_read_ptr;
1008 }
1009
1010 static sfc_dp_tx_qtx_ev_t sfc_ef10_tx_qtx_ev;
1011 static bool
1012 sfc_ef10_tx_qtx_ev(struct sfc_dp_txq *dp_txq, __rte_unused unsigned int id)
1013 {
1014         __rte_unused struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
1015
1016         SFC_ASSERT(txq->flags & SFC_EF10_TXQ_NOT_RUNNING);
1017
1018         /*
1019          * It is safe to ignore Tx event since we reap all mbufs on
1020          * queue purge anyway.
1021          */
1022
1023         return false;
1024 }
1025
1026 static sfc_dp_tx_qreap_t sfc_ef10_tx_qreap;
1027 static void
1028 sfc_ef10_tx_qreap(struct sfc_dp_txq *dp_txq)
1029 {
1030         struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
1031         unsigned int completed;
1032
1033         for (completed = txq->completed; completed != txq->added; ++completed) {
1034                 struct sfc_ef10_tx_sw_desc *txd;
1035
1036                 txd = &txq->sw_ring[completed & txq->ptr_mask];
1037                 if (txd->mbuf != NULL) {
1038                         rte_pktmbuf_free_seg(txd->mbuf);
1039                         txd->mbuf = NULL;
1040                 }
1041         }
1042
1043         txq->flags &= ~SFC_EF10_TXQ_STARTED;
1044 }
1045
1046 static unsigned int
1047 sfc_ef10_tx_qdesc_npending(struct sfc_ef10_txq *txq)
1048 {
1049         const unsigned int curr_done = txq->completed - 1;
1050         unsigned int anew_done = curr_done;
1051         efx_qword_t tx_ev;
1052         const unsigned int evq_old_read_ptr = txq->evq_read_ptr;
1053
1054         if (unlikely(txq->flags &
1055                      (SFC_EF10_TXQ_NOT_RUNNING | SFC_EF10_TXQ_EXCEPTION)))
1056                 return 0;
1057
1058         while (sfc_ef10_tx_get_event(txq, &tx_ev))
1059                 anew_done = EFX_QWORD_FIELD(tx_ev, ESF_DZ_TX_DESCR_INDX);
1060
1061         /*
1062          * The function does not process events, so return event queue read
1063          * pointer to the original position to allow the events that were
1064          * read to be processed later
1065          */
1066         txq->evq_read_ptr = evq_old_read_ptr;
1067
1068         return (anew_done - curr_done) & txq->ptr_mask;
1069 }
1070
1071 static sfc_dp_tx_qdesc_status_t sfc_ef10_tx_qdesc_status;
1072 static int
1073 sfc_ef10_tx_qdesc_status(struct sfc_dp_txq *dp_txq,
1074                          uint16_t offset)
1075 {
1076         struct sfc_ef10_txq *txq = sfc_ef10_txq_by_dp_txq(dp_txq);
1077         unsigned int npending = sfc_ef10_tx_qdesc_npending(txq);
1078
1079         if (unlikely(offset > txq->ptr_mask))
1080                 return -EINVAL;
1081
1082         if (unlikely(offset >= txq->max_fill_level))
1083                 return RTE_ETH_TX_DESC_UNAVAIL;
1084
1085         if (unlikely(offset < npending))
1086                 return RTE_ETH_TX_DESC_FULL;
1087
1088         return RTE_ETH_TX_DESC_DONE;
1089 }
1090
1091 struct sfc_dp_tx sfc_ef10_tx = {
1092         .dp = {
1093                 .name           = SFC_KVARG_DATAPATH_EF10,
1094                 .type           = SFC_DP_TX,
1095                 .hw_fw_caps     = SFC_DP_HW_FW_CAP_EF10,
1096         },
1097         .features               = SFC_DP_TX_FEAT_TSO |
1098                                   SFC_DP_TX_FEAT_MULTI_SEG |
1099                                   SFC_DP_TX_FEAT_MULTI_POOL |
1100                                   SFC_DP_TX_FEAT_REFCNT |
1101                                   SFC_DP_TX_FEAT_MULTI_PROCESS,
1102         .get_dev_info           = sfc_ef10_get_dev_info,
1103         .qsize_up_rings         = sfc_ef10_tx_qsize_up_rings,
1104         .qcreate                = sfc_ef10_tx_qcreate,
1105         .qdestroy               = sfc_ef10_tx_qdestroy,
1106         .qstart                 = sfc_ef10_tx_qstart,
1107         .qtx_ev                 = sfc_ef10_tx_qtx_ev,
1108         .qstop                  = sfc_ef10_tx_qstop,
1109         .qreap                  = sfc_ef10_tx_qreap,
1110         .qdesc_status           = sfc_ef10_tx_qdesc_status,
1111         .pkt_prepare            = sfc_ef10_prepare_pkts,
1112         .pkt_burst              = sfc_ef10_xmit_pkts,
1113 };
1114
1115 struct sfc_dp_tx sfc_ef10_simple_tx = {
1116         .dp = {
1117                 .name           = SFC_KVARG_DATAPATH_EF10_SIMPLE,
1118                 .type           = SFC_DP_TX,
1119         },
1120         .features               = SFC_DP_TX_FEAT_MULTI_PROCESS,
1121         .get_dev_info           = sfc_ef10_get_dev_info,
1122         .qsize_up_rings         = sfc_ef10_tx_qsize_up_rings,
1123         .qcreate                = sfc_ef10_tx_qcreate,
1124         .qdestroy               = sfc_ef10_tx_qdestroy,
1125         .qstart                 = sfc_ef10_tx_qstart,
1126         .qtx_ev                 = sfc_ef10_tx_qtx_ev,
1127         .qstop                  = sfc_ef10_tx_qstop,
1128         .qreap                  = sfc_ef10_tx_qreap,
1129         .qdesc_status           = sfc_ef10_tx_qdesc_status,
1130 #ifdef RTE_LIBRTE_SFC_EFX_DEBUG
1131         .pkt_prepare            = sfc_ef10_simple_prepare_pkts,
1132 #endif
1133         .pkt_burst              = sfc_ef10_simple_xmit_pkts,
1134 };