xref: /dpdk/drivers/net/sfc/sfc_tx.c (revision c7e9729d)
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 "sfc.h"
11 #include "sfc_debug.h"
12 #include "sfc_log.h"
13 #include "sfc_ev.h"
14 #include "sfc_tx.h"
15 #include "sfc_tweak.h"
16 #include "sfc_kvargs.h"
17 
18 /*
19  * Maximum number of TX queue flush attempts in case of
20  * failure or flush timeout
21  */
22 #define SFC_TX_QFLUSH_ATTEMPTS		(3)
23 
24 /*
25  * Time to wait between event queue polling attempts when waiting for TX
26  * queue flush done or flush failed events
27  */
28 #define SFC_TX_QFLUSH_POLL_WAIT_MS	(1)
29 
30 /*
31  * Maximum number of event queue polling attempts when waiting for TX queue
32  * flush done or flush failed events; it defines TX queue flush attempt timeout
33  * together with SFC_TX_QFLUSH_POLL_WAIT_MS
34  */
35 #define SFC_TX_QFLUSH_POLL_ATTEMPTS	(2000)
36 
37 uint64_t
38 sfc_tx_get_dev_offload_caps(struct sfc_adapter *sa)
39 {
40 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
41 	uint64_t caps = 0;
42 
43 	if ((sa->dp_tx->features & SFC_DP_TX_FEAT_VLAN_INSERT) &&
44 	    encp->enc_hw_tx_insert_vlan_enabled)
45 		caps |= DEV_TX_OFFLOAD_VLAN_INSERT;
46 
47 	if (sa->dp_tx->features & SFC_DP_TX_FEAT_MULTI_SEG)
48 		caps |= DEV_TX_OFFLOAD_MULTI_SEGS;
49 
50 	if ((~sa->dp_tx->features & SFC_DP_TX_FEAT_MULTI_POOL) &&
51 	    (~sa->dp_tx->features & SFC_DP_TX_FEAT_REFCNT))
52 		caps |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
53 
54 	return caps;
55 }
56 
57 uint64_t
58 sfc_tx_get_queue_offload_caps(struct sfc_adapter *sa)
59 {
60 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
61 	uint64_t caps = 0;
62 
63 	caps |= DEV_TX_OFFLOAD_IPV4_CKSUM;
64 	caps |= DEV_TX_OFFLOAD_UDP_CKSUM;
65 	caps |= DEV_TX_OFFLOAD_TCP_CKSUM;
66 
67 	if (encp->enc_tunnel_encapsulations_supported)
68 		caps |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
69 
70 	if (sa->tso)
71 		caps |= DEV_TX_OFFLOAD_TCP_TSO;
72 
73 	return caps;
74 }
75 
76 static void
77 sfc_tx_log_offloads(struct sfc_adapter *sa, const char *offload_group,
78 		    const char *verdict, uint64_t offloads)
79 {
80 	unsigned long long bit;
81 
82 	while ((bit = __builtin_ffsll(offloads)) != 0) {
83 		uint64_t flag = (1ULL << --bit);
84 
85 		sfc_err(sa, "Tx %s offload %s %s", offload_group,
86 			rte_eth_dev_tx_offload_name(flag), verdict);
87 
88 		offloads &= ~flag;
89 	}
90 }
91 
92 static int
93 sfc_tx_queue_offload_mismatch(struct sfc_adapter *sa, uint64_t requested)
94 {
95 	uint64_t mandatory = sa->eth_dev->data->dev_conf.txmode.offloads;
96 	uint64_t supported = sfc_tx_get_dev_offload_caps(sa) |
97 			     sfc_tx_get_queue_offload_caps(sa);
98 	uint64_t rejected = requested & ~supported;
99 	uint64_t missing = (requested & mandatory) ^ mandatory;
100 	boolean_t mismatch = B_FALSE;
101 
102 	if (rejected) {
103 		sfc_tx_log_offloads(sa, "queue", "is unsupported", rejected);
104 		mismatch = B_TRUE;
105 	}
106 
107 	if (missing) {
108 		sfc_tx_log_offloads(sa, "queue", "must be set", missing);
109 		mismatch = B_TRUE;
110 	}
111 
112 	return mismatch;
113 }
114 
115 static int
116 sfc_tx_qcheck_conf(struct sfc_adapter *sa, unsigned int txq_max_fill_level,
117 		   const struct rte_eth_txconf *tx_conf)
118 {
119 	int rc = 0;
120 
121 	if (tx_conf->tx_rs_thresh != 0) {
122 		sfc_err(sa, "RS bit in transmit descriptor is not supported");
123 		rc = EINVAL;
124 	}
125 
126 	if (tx_conf->tx_free_thresh > txq_max_fill_level) {
127 		sfc_err(sa,
128 			"TxQ free threshold too large: %u vs maximum %u",
129 			tx_conf->tx_free_thresh, txq_max_fill_level);
130 		rc = EINVAL;
131 	}
132 
133 	if (tx_conf->tx_thresh.pthresh != 0 ||
134 	    tx_conf->tx_thresh.hthresh != 0 ||
135 	    tx_conf->tx_thresh.wthresh != 0) {
136 		sfc_warn(sa,
137 			"prefetch/host/writeback thresholds are not supported");
138 	}
139 
140 	/* We either perform both TCP and UDP offload, or no offload at all */
141 	if (((tx_conf->offloads & DEV_TX_OFFLOAD_TCP_CKSUM) == 0) !=
142 	    ((tx_conf->offloads & DEV_TX_OFFLOAD_UDP_CKSUM) == 0)) {
143 		sfc_err(sa, "TCP and UDP offloads can't be set independently");
144 		rc = EINVAL;
145 	}
146 
147 	if (sfc_tx_queue_offload_mismatch(sa, tx_conf->offloads))
148 		rc = EINVAL;
149 
150 	return rc;
151 }
152 
153 void
154 sfc_tx_qflush_done(struct sfc_txq *txq)
155 {
156 	txq->state |= SFC_TXQ_FLUSHED;
157 	txq->state &= ~SFC_TXQ_FLUSHING;
158 }
159 
160 int
161 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
162 	     uint16_t nb_tx_desc, unsigned int socket_id,
163 	     const struct rte_eth_txconf *tx_conf)
164 {
165 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
166 	unsigned int txq_entries;
167 	unsigned int evq_entries;
168 	unsigned int txq_max_fill_level;
169 	struct sfc_txq_info *txq_info;
170 	struct sfc_evq *evq;
171 	struct sfc_txq *txq;
172 	int rc = 0;
173 	struct sfc_dp_tx_qcreate_info info;
174 
175 	sfc_log_init(sa, "TxQ = %u", sw_index);
176 
177 	rc = sa->dp_tx->qsize_up_rings(nb_tx_desc, &txq_entries, &evq_entries,
178 				       &txq_max_fill_level);
179 	if (rc != 0)
180 		goto fail_size_up_rings;
181 	SFC_ASSERT(txq_entries >= EFX_TXQ_MINNDESCS);
182 	SFC_ASSERT(txq_entries <= sa->txq_max_entries);
183 	SFC_ASSERT(txq_entries >= nb_tx_desc);
184 	SFC_ASSERT(txq_max_fill_level <= nb_tx_desc);
185 
186 	rc = sfc_tx_qcheck_conf(sa, txq_max_fill_level, tx_conf);
187 	if (rc != 0)
188 		goto fail_bad_conf;
189 
190 	SFC_ASSERT(sw_index < sa->txq_count);
191 	txq_info = &sa->txq_info[sw_index];
192 
193 	txq_info->entries = txq_entries;
194 
195 	rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_TX, sw_index,
196 			  evq_entries, socket_id, &evq);
197 	if (rc != 0)
198 		goto fail_ev_qinit;
199 
200 	rc = ENOMEM;
201 	txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
202 	if (txq == NULL)
203 		goto fail_txq_alloc;
204 
205 	txq_info->txq = txq;
206 
207 	txq->hw_index = sw_index;
208 	txq->evq = evq;
209 	txq->free_thresh =
210 		(tx_conf->tx_free_thresh) ? tx_conf->tx_free_thresh :
211 		SFC_TX_DEFAULT_FREE_THRESH;
212 	txq->flags = tx_conf->txq_flags;
213 	txq->offloads = tx_conf->offloads;
214 
215 	rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
216 			   socket_id, &txq->mem);
217 	if (rc != 0)
218 		goto fail_dma_alloc;
219 
220 	memset(&info, 0, sizeof(info));
221 	info.max_fill_level = txq_max_fill_level;
222 	info.free_thresh = txq->free_thresh;
223 	info.flags = tx_conf->txq_flags;
224 	info.offloads = tx_conf->offloads;
225 	info.txq_entries = txq_info->entries;
226 	info.dma_desc_size_max = encp->enc_tx_dma_desc_size_max;
227 	info.txq_hw_ring = txq->mem.esm_base;
228 	info.evq_entries = evq_entries;
229 	info.evq_hw_ring = evq->mem.esm_base;
230 	info.hw_index = txq->hw_index;
231 	info.mem_bar = sa->mem_bar.esb_base;
232 	info.vi_window_shift = encp->enc_vi_window_shift;
233 
234 	rc = sa->dp_tx->qcreate(sa->eth_dev->data->port_id, sw_index,
235 				&RTE_ETH_DEV_TO_PCI(sa->eth_dev)->addr,
236 				socket_id, &info, &txq->dp);
237 	if (rc != 0)
238 		goto fail_dp_tx_qinit;
239 
240 	evq->dp_txq = txq->dp;
241 
242 	txq->state = SFC_TXQ_INITIALIZED;
243 
244 	txq_info->deferred_start = (tx_conf->tx_deferred_start != 0);
245 
246 	return 0;
247 
248 fail_dp_tx_qinit:
249 	sfc_dma_free(sa, &txq->mem);
250 
251 fail_dma_alloc:
252 	txq_info->txq = NULL;
253 	rte_free(txq);
254 
255 fail_txq_alloc:
256 	sfc_ev_qfini(evq);
257 
258 fail_ev_qinit:
259 	txq_info->entries = 0;
260 
261 fail_bad_conf:
262 fail_size_up_rings:
263 	sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
264 	return rc;
265 }
266 
267 void
268 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
269 {
270 	struct sfc_txq_info *txq_info;
271 	struct sfc_txq *txq;
272 
273 	sfc_log_init(sa, "TxQ = %u", sw_index);
274 
275 	SFC_ASSERT(sw_index < sa->txq_count);
276 	txq_info = &sa->txq_info[sw_index];
277 
278 	txq = txq_info->txq;
279 	SFC_ASSERT(txq != NULL);
280 	SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
281 
282 	sa->dp_tx->qdestroy(txq->dp);
283 	txq->dp = NULL;
284 
285 	txq_info->txq = NULL;
286 	txq_info->entries = 0;
287 
288 	sfc_dma_free(sa, &txq->mem);
289 
290 	sfc_ev_qfini(txq->evq);
291 	txq->evq = NULL;
292 
293 	rte_free(txq);
294 }
295 
296 static int
297 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
298 {
299 	sfc_log_init(sa, "TxQ = %u", sw_index);
300 
301 	return 0;
302 }
303 
304 static int
305 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
306 {
307 	uint64_t offloads_supported = sfc_tx_get_dev_offload_caps(sa) |
308 				      sfc_tx_get_queue_offload_caps(sa);
309 	uint64_t offloads_rejected = txmode->offloads & ~offloads_supported;
310 	int rc = 0;
311 
312 	switch (txmode->mq_mode) {
313 	case ETH_MQ_TX_NONE:
314 		break;
315 	default:
316 		sfc_err(sa, "Tx multi-queue mode %u not supported",
317 			txmode->mq_mode);
318 		rc = EINVAL;
319 	}
320 
321 	/*
322 	 * These features are claimed to be i40e-specific,
323 	 * but it does make sense to double-check their absence
324 	 */
325 	if (txmode->hw_vlan_reject_tagged) {
326 		sfc_err(sa, "Rejecting tagged packets not supported");
327 		rc = EINVAL;
328 	}
329 
330 	if (txmode->hw_vlan_reject_untagged) {
331 		sfc_err(sa, "Rejecting untagged packets not supported");
332 		rc = EINVAL;
333 	}
334 
335 	if (txmode->hw_vlan_insert_pvid) {
336 		sfc_err(sa, "Port-based VLAN insertion not supported");
337 		rc = EINVAL;
338 	}
339 
340 	if (offloads_rejected) {
341 		sfc_tx_log_offloads(sa, "device", "is unsupported",
342 				    offloads_rejected);
343 		rc = EINVAL;
344 	}
345 
346 	return rc;
347 }
348 
349 /**
350  * Destroy excess queues that are no longer needed after reconfiguration
351  * or complete close.
352  */
353 static void
354 sfc_tx_fini_queues(struct sfc_adapter *sa, unsigned int nb_tx_queues)
355 {
356 	int sw_index;
357 
358 	SFC_ASSERT(nb_tx_queues <= sa->txq_count);
359 
360 	sw_index = sa->txq_count;
361 	while (--sw_index >= (int)nb_tx_queues) {
362 		if (sa->txq_info[sw_index].txq != NULL)
363 			sfc_tx_qfini(sa, sw_index);
364 	}
365 
366 	sa->txq_count = nb_tx_queues;
367 }
368 
369 int
370 sfc_tx_configure(struct sfc_adapter *sa)
371 {
372 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
373 	const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
374 	const unsigned int nb_tx_queues = sa->eth_dev->data->nb_tx_queues;
375 	int rc = 0;
376 
377 	sfc_log_init(sa, "nb_tx_queues=%u (old %u)",
378 		     nb_tx_queues, sa->txq_count);
379 
380 	/*
381 	 * The datapath implementation assumes absence of boundary
382 	 * limits on Tx DMA descriptors. Addition of these checks on
383 	 * datapath would simply make the datapath slower.
384 	 */
385 	if (encp->enc_tx_dma_desc_boundary != 0) {
386 		rc = ENOTSUP;
387 		goto fail_tx_dma_desc_boundary;
388 	}
389 
390 	rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
391 	if (rc != 0)
392 		goto fail_check_mode;
393 
394 	if (nb_tx_queues == sa->txq_count)
395 		goto done;
396 
397 	if (sa->txq_info == NULL) {
398 		sa->txq_info = rte_calloc_socket("sfc-txqs", nb_tx_queues,
399 						 sizeof(sa->txq_info[0]), 0,
400 						 sa->socket_id);
401 		if (sa->txq_info == NULL)
402 			goto fail_txqs_alloc;
403 	} else {
404 		struct sfc_txq_info *new_txq_info;
405 
406 		if (nb_tx_queues < sa->txq_count)
407 			sfc_tx_fini_queues(sa, nb_tx_queues);
408 
409 		new_txq_info =
410 			rte_realloc(sa->txq_info,
411 				    nb_tx_queues * sizeof(sa->txq_info[0]), 0);
412 		if (new_txq_info == NULL && nb_tx_queues > 0)
413 			goto fail_txqs_realloc;
414 
415 		sa->txq_info = new_txq_info;
416 		if (nb_tx_queues > sa->txq_count)
417 			memset(&sa->txq_info[sa->txq_count], 0,
418 			       (nb_tx_queues - sa->txq_count) *
419 			       sizeof(sa->txq_info[0]));
420 	}
421 
422 	while (sa->txq_count < nb_tx_queues) {
423 		rc = sfc_tx_qinit_info(sa, sa->txq_count);
424 		if (rc != 0)
425 			goto fail_tx_qinit_info;
426 
427 		sa->txq_count++;
428 	}
429 
430 done:
431 	return 0;
432 
433 fail_tx_qinit_info:
434 fail_txqs_realloc:
435 fail_txqs_alloc:
436 	sfc_tx_close(sa);
437 
438 fail_check_mode:
439 fail_tx_dma_desc_boundary:
440 	sfc_log_init(sa, "failed (rc = %d)", rc);
441 	return rc;
442 }
443 
444 void
445 sfc_tx_close(struct sfc_adapter *sa)
446 {
447 	sfc_tx_fini_queues(sa, 0);
448 
449 	rte_free(sa->txq_info);
450 	sa->txq_info = NULL;
451 }
452 
453 int
454 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
455 {
456 	uint64_t offloads_supported = sfc_tx_get_dev_offload_caps(sa) |
457 				      sfc_tx_get_queue_offload_caps(sa);
458 	struct rte_eth_dev_data *dev_data;
459 	struct sfc_txq_info *txq_info;
460 	struct sfc_txq *txq;
461 	struct sfc_evq *evq;
462 	uint16_t flags = 0;
463 	unsigned int desc_index;
464 	int rc = 0;
465 
466 	sfc_log_init(sa, "TxQ = %u", sw_index);
467 
468 	SFC_ASSERT(sw_index < sa->txq_count);
469 	txq_info = &sa->txq_info[sw_index];
470 
471 	txq = txq_info->txq;
472 
473 	SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
474 
475 	evq = txq->evq;
476 
477 	rc = sfc_ev_qstart(evq, sfc_evq_index_by_txq_sw_index(sa, sw_index));
478 	if (rc != 0)
479 		goto fail_ev_qstart;
480 
481 	/*
482 	 * The absence of ETH_TXQ_FLAGS_IGNORE is associated with a legacy
483 	 * application which expects that IPv4 checksum offload is enabled
484 	 * all the time as there is no legacy flag to turn off the offload.
485 	 */
486 	if ((txq->offloads & DEV_TX_OFFLOAD_IPV4_CKSUM) ||
487 	    (~txq->flags & ETH_TXQ_FLAGS_IGNORE))
488 		flags |= EFX_TXQ_CKSUM_IPV4;
489 
490 	if ((txq->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
491 	    ((~txq->flags & ETH_TXQ_FLAGS_IGNORE) &&
492 	     (offloads_supported & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)))
493 		flags |= EFX_TXQ_CKSUM_INNER_IPV4;
494 
495 	if ((txq->offloads & DEV_TX_OFFLOAD_TCP_CKSUM) ||
496 	    (txq->offloads & DEV_TX_OFFLOAD_UDP_CKSUM)) {
497 		flags |= EFX_TXQ_CKSUM_TCPUDP;
498 
499 		if ((~txq->flags & ETH_TXQ_FLAGS_IGNORE) &&
500 		    (offloads_supported & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM))
501 			flags |= EFX_TXQ_CKSUM_INNER_TCPUDP;
502 	}
503 
504 	/*
505 	 * The absence of ETH_TXQ_FLAGS_IGNORE is associated with a legacy
506 	 * application. In turn, the absence of ETH_TXQ_FLAGS_NOXSUMTCP is
507 	 * associated specifically with a legacy application which expects
508 	 * both TCP checksum offload and TSO to be enabled because the legacy
509 	 * API does not provide a dedicated mechanism to control TSO.
510 	 */
511 	if ((txq->offloads & DEV_TX_OFFLOAD_TCP_TSO) ||
512 	    ((~txq->flags & ETH_TXQ_FLAGS_IGNORE) &&
513 	     (~txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP)))
514 		flags |= EFX_TXQ_FATSOV2;
515 
516 	rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
517 			    txq_info->entries, 0 /* not used on EF10 */,
518 			    flags, evq->common,
519 			    &txq->common, &desc_index);
520 	if (rc != 0) {
521 		if (sa->tso && (rc == ENOSPC))
522 			sfc_err(sa, "ran out of TSO contexts");
523 
524 		goto fail_tx_qcreate;
525 	}
526 
527 	efx_tx_qenable(txq->common);
528 
529 	txq->state |= SFC_TXQ_STARTED;
530 
531 	rc = sa->dp_tx->qstart(txq->dp, evq->read_ptr, desc_index);
532 	if (rc != 0)
533 		goto fail_dp_qstart;
534 
535 	/*
536 	 * It seems to be used by DPDK for debug purposes only ('rte_ether')
537 	 */
538 	dev_data = sa->eth_dev->data;
539 	dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
540 
541 	return 0;
542 
543 fail_dp_qstart:
544 	txq->state = SFC_TXQ_INITIALIZED;
545 	efx_tx_qdestroy(txq->common);
546 
547 fail_tx_qcreate:
548 	sfc_ev_qstop(evq);
549 
550 fail_ev_qstart:
551 	return rc;
552 }
553 
554 void
555 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
556 {
557 	struct rte_eth_dev_data *dev_data;
558 	struct sfc_txq_info *txq_info;
559 	struct sfc_txq *txq;
560 	unsigned int retry_count;
561 	unsigned int wait_count;
562 	int rc;
563 
564 	sfc_log_init(sa, "TxQ = %u", sw_index);
565 
566 	SFC_ASSERT(sw_index < sa->txq_count);
567 	txq_info = &sa->txq_info[sw_index];
568 
569 	txq = txq_info->txq;
570 
571 	if (txq->state == SFC_TXQ_INITIALIZED)
572 		return;
573 
574 	SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
575 
576 	sa->dp_tx->qstop(txq->dp, &txq->evq->read_ptr);
577 
578 	/*
579 	 * Retry TX queue flushing in case of flush failed or
580 	 * timeout; in the worst case it can delay for 6 seconds
581 	 */
582 	for (retry_count = 0;
583 	     ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
584 	     (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
585 	     ++retry_count) {
586 		rc = efx_tx_qflush(txq->common);
587 		if (rc != 0) {
588 			txq->state |= (rc == EALREADY) ?
589 				SFC_TXQ_FLUSHED : SFC_TXQ_FLUSH_FAILED;
590 			break;
591 		}
592 
593 		/*
594 		 * Wait for TX queue flush done or flush failed event at least
595 		 * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
596 		 * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
597 		 * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
598 		 */
599 		wait_count = 0;
600 		do {
601 			rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
602 			sfc_ev_qpoll(txq->evq);
603 		} while ((txq->state & SFC_TXQ_FLUSHING) &&
604 			 wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
605 
606 		if (txq->state & SFC_TXQ_FLUSHING)
607 			sfc_err(sa, "TxQ %u flush timed out", sw_index);
608 
609 		if (txq->state & SFC_TXQ_FLUSHED)
610 			sfc_notice(sa, "TxQ %u flushed", sw_index);
611 	}
612 
613 	sa->dp_tx->qreap(txq->dp);
614 
615 	txq->state = SFC_TXQ_INITIALIZED;
616 
617 	efx_tx_qdestroy(txq->common);
618 
619 	sfc_ev_qstop(txq->evq);
620 
621 	/*
622 	 * It seems to be used by DPDK for debug purposes only ('rte_ether')
623 	 */
624 	dev_data = sa->eth_dev->data;
625 	dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
626 }
627 
628 int
629 sfc_tx_start(struct sfc_adapter *sa)
630 {
631 	unsigned int sw_index;
632 	int rc = 0;
633 
634 	sfc_log_init(sa, "txq_count = %u", sa->txq_count);
635 
636 	if (sa->tso) {
637 		if (!efx_nic_cfg_get(sa->nic)->enc_fw_assisted_tso_v2_enabled) {
638 			sfc_warn(sa, "TSO support was unable to be restored");
639 			sa->tso = B_FALSE;
640 		}
641 	}
642 
643 	rc = efx_tx_init(sa->nic);
644 	if (rc != 0)
645 		goto fail_efx_tx_init;
646 
647 	for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
648 		if (!(sa->txq_info[sw_index].deferred_start) ||
649 		    sa->txq_info[sw_index].deferred_started) {
650 			rc = sfc_tx_qstart(sa, sw_index);
651 			if (rc != 0)
652 				goto fail_tx_qstart;
653 		}
654 	}
655 
656 	return 0;
657 
658 fail_tx_qstart:
659 	while (sw_index-- > 0)
660 		sfc_tx_qstop(sa, sw_index);
661 
662 	efx_tx_fini(sa->nic);
663 
664 fail_efx_tx_init:
665 	sfc_log_init(sa, "failed (rc = %d)", rc);
666 	return rc;
667 }
668 
669 void
670 sfc_tx_stop(struct sfc_adapter *sa)
671 {
672 	unsigned int sw_index;
673 
674 	sfc_log_init(sa, "txq_count = %u", sa->txq_count);
675 
676 	sw_index = sa->txq_count;
677 	while (sw_index-- > 0) {
678 		if (sa->txq_info[sw_index].txq != NULL)
679 			sfc_tx_qstop(sa, sw_index);
680 	}
681 
682 	efx_tx_fini(sa->nic);
683 }
684 
685 static void
686 sfc_efx_tx_reap(struct sfc_efx_txq *txq)
687 {
688 	unsigned int completed;
689 
690 	sfc_ev_qpoll(txq->evq);
691 
692 	for (completed = txq->completed;
693 	     completed != txq->pending; completed++) {
694 		struct sfc_efx_tx_sw_desc *txd;
695 
696 		txd = &txq->sw_ring[completed & txq->ptr_mask];
697 
698 		if (txd->mbuf != NULL) {
699 			rte_pktmbuf_free(txd->mbuf);
700 			txd->mbuf = NULL;
701 		}
702 	}
703 
704 	txq->completed = completed;
705 }
706 
707 /*
708  * The function is used to insert or update VLAN tag;
709  * the firmware has state of the firmware tag to insert per TxQ
710  * (controlled by option descriptors), hence, if the tag of the
711  * packet to be sent is different from one remembered by the firmware,
712  * the function will update it
713  */
714 static unsigned int
715 sfc_efx_tx_maybe_insert_tag(struct sfc_efx_txq *txq, struct rte_mbuf *m,
716 			    efx_desc_t **pend)
717 {
718 	uint16_t this_tag = ((m->ol_flags & PKT_TX_VLAN_PKT) ?
719 			     m->vlan_tci : 0);
720 
721 	if (this_tag == txq->hw_vlan_tci)
722 		return 0;
723 
724 	/*
725 	 * The expression inside SFC_ASSERT() is not desired to be checked in
726 	 * a non-debug build because it might be too expensive on the data path
727 	 */
728 	SFC_ASSERT(efx_nic_cfg_get(txq->evq->sa->nic)->enc_hw_tx_insert_vlan_enabled);
729 
730 	efx_tx_qdesc_vlantci_create(txq->common, rte_cpu_to_be_16(this_tag),
731 				    *pend);
732 	(*pend)++;
733 	txq->hw_vlan_tci = this_tag;
734 
735 	return 1;
736 }
737 
738 static uint16_t
739 sfc_efx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
740 {
741 	struct sfc_dp_txq *dp_txq = (struct sfc_dp_txq *)tx_queue;
742 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
743 	unsigned int added = txq->added;
744 	unsigned int pushed = added;
745 	unsigned int pkts_sent = 0;
746 	efx_desc_t *pend = &txq->pend_desc[0];
747 	const unsigned int hard_max_fill = txq->max_fill_level;
748 	const unsigned int soft_max_fill = hard_max_fill - txq->free_thresh;
749 	unsigned int fill_level = added - txq->completed;
750 	boolean_t reap_done;
751 	int rc __rte_unused;
752 	struct rte_mbuf **pktp;
753 
754 	if (unlikely((txq->flags & SFC_EFX_TXQ_FLAG_RUNNING) == 0))
755 		goto done;
756 
757 	/*
758 	 * If insufficient space for a single packet is present,
759 	 * we should reap; otherwise, we shouldn't do that all the time
760 	 * to avoid latency increase
761 	 */
762 	reap_done = (fill_level > soft_max_fill);
763 
764 	if (reap_done) {
765 		sfc_efx_tx_reap(txq);
766 		/*
767 		 * Recalculate fill level since 'txq->completed'
768 		 * might have changed on reap
769 		 */
770 		fill_level = added - txq->completed;
771 	}
772 
773 	for (pkts_sent = 0, pktp = &tx_pkts[0];
774 	     (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
775 	     pkts_sent++, pktp++) {
776 		struct rte_mbuf		*m_seg = *pktp;
777 		size_t			pkt_len = m_seg->pkt_len;
778 		unsigned int		pkt_descs = 0;
779 		size_t			in_off = 0;
780 
781 		/*
782 		 * Here VLAN TCI is expected to be zero in case if no
783 		 * DEV_TX_OFFLOAD_VLAN_INSERT capability is advertised;
784 		 * if the calling app ignores the absence of
785 		 * DEV_TX_OFFLOAD_VLAN_INSERT and pushes VLAN TCI, then
786 		 * TX_ERROR will occur
787 		 */
788 		pkt_descs += sfc_efx_tx_maybe_insert_tag(txq, m_seg, &pend);
789 
790 		if (m_seg->ol_flags & PKT_TX_TCP_SEG) {
791 			/*
792 			 * We expect correct 'pkt->l[2, 3, 4]_len' values
793 			 * to be set correctly by the caller
794 			 */
795 			if (sfc_efx_tso_do(txq, added, &m_seg, &in_off, &pend,
796 					   &pkt_descs, &pkt_len) != 0) {
797 				/* We may have reached this place for
798 				 * one of the following reasons:
799 				 *
800 				 * 1) Packet header length is greater
801 				 *    than SFC_TSOH_STD_LEN
802 				 * 2) TCP header starts at more then
803 				 *    208 bytes into the frame
804 				 *
805 				 * We will deceive RTE saying that we have sent
806 				 * the packet, but we will actually drop it.
807 				 * Hence, we should revert 'pend' to the
808 				 * previous state (in case we have added
809 				 * VLAN descriptor) and start processing
810 				 * another one packet. But the original
811 				 * mbuf shouldn't be orphaned
812 				 */
813 				pend -= pkt_descs;
814 
815 				rte_pktmbuf_free(*pktp);
816 
817 				continue;
818 			}
819 
820 			/*
821 			 * We've only added 2 FATSOv2 option descriptors
822 			 * and 1 descriptor for the linearized packet header.
823 			 * The outstanding work will be done in the same manner
824 			 * as for the usual non-TSO path
825 			 */
826 		}
827 
828 		for (; m_seg != NULL; m_seg = m_seg->next) {
829 			efsys_dma_addr_t	next_frag;
830 			size_t			seg_len;
831 
832 			seg_len = m_seg->data_len;
833 			next_frag = rte_mbuf_data_iova(m_seg);
834 
835 			/*
836 			 * If we've started TSO transaction few steps earlier,
837 			 * we'll skip packet header using an offset in the
838 			 * current segment (which has been set to the
839 			 * first one containing payload)
840 			 */
841 			seg_len -= in_off;
842 			next_frag += in_off;
843 			in_off = 0;
844 
845 			do {
846 				efsys_dma_addr_t	frag_addr = next_frag;
847 				size_t			frag_len;
848 
849 				/*
850 				 * It is assumed here that there is no
851 				 * limitation on address boundary
852 				 * crossing by DMA descriptor.
853 				 */
854 				frag_len = MIN(seg_len, txq->dma_desc_size_max);
855 				next_frag += frag_len;
856 				seg_len -= frag_len;
857 				pkt_len -= frag_len;
858 
859 				efx_tx_qdesc_dma_create(txq->common,
860 							frag_addr, frag_len,
861 							(pkt_len == 0),
862 							pend++);
863 
864 				pkt_descs++;
865 			} while (seg_len != 0);
866 		}
867 
868 		added += pkt_descs;
869 
870 		fill_level += pkt_descs;
871 		if (unlikely(fill_level > hard_max_fill)) {
872 			/*
873 			 * Our estimation for maximum number of descriptors
874 			 * required to send a packet seems to be wrong.
875 			 * Try to reap (if we haven't yet).
876 			 */
877 			if (!reap_done) {
878 				sfc_efx_tx_reap(txq);
879 				reap_done = B_TRUE;
880 				fill_level = added - txq->completed;
881 				if (fill_level > hard_max_fill) {
882 					pend -= pkt_descs;
883 					break;
884 				}
885 			} else {
886 				pend -= pkt_descs;
887 				break;
888 			}
889 		}
890 
891 		/* Assign mbuf to the last used desc */
892 		txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
893 	}
894 
895 	if (likely(pkts_sent > 0)) {
896 		rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
897 				       pend - &txq->pend_desc[0],
898 				       txq->completed, &txq->added);
899 		SFC_ASSERT(rc == 0);
900 
901 		if (likely(pushed != txq->added))
902 			efx_tx_qpush(txq->common, txq->added, pushed);
903 	}
904 
905 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
906 	if (!reap_done)
907 		sfc_efx_tx_reap(txq);
908 #endif
909 
910 done:
911 	return pkts_sent;
912 }
913 
914 struct sfc_txq *
915 sfc_txq_by_dp_txq(const struct sfc_dp_txq *dp_txq)
916 {
917 	const struct sfc_dp_queue *dpq = &dp_txq->dpq;
918 	struct rte_eth_dev *eth_dev;
919 	struct sfc_adapter *sa;
920 	struct sfc_txq *txq;
921 
922 	SFC_ASSERT(rte_eth_dev_is_valid_port(dpq->port_id));
923 	eth_dev = &rte_eth_devices[dpq->port_id];
924 
925 	sa = eth_dev->data->dev_private;
926 
927 	SFC_ASSERT(dpq->queue_id < sa->txq_count);
928 	txq = sa->txq_info[dpq->queue_id].txq;
929 
930 	SFC_ASSERT(txq != NULL);
931 	return txq;
932 }
933 
934 static sfc_dp_tx_qsize_up_rings_t sfc_efx_tx_qsize_up_rings;
935 static int
936 sfc_efx_tx_qsize_up_rings(uint16_t nb_tx_desc,
937 			  unsigned int *txq_entries,
938 			  unsigned int *evq_entries,
939 			  unsigned int *txq_max_fill_level)
940 {
941 	*txq_entries = nb_tx_desc;
942 	*evq_entries = nb_tx_desc;
943 	*txq_max_fill_level = EFX_TXQ_LIMIT(*txq_entries);
944 	return 0;
945 }
946 
947 static sfc_dp_tx_qcreate_t sfc_efx_tx_qcreate;
948 static int
949 sfc_efx_tx_qcreate(uint16_t port_id, uint16_t queue_id,
950 		   const struct rte_pci_addr *pci_addr,
951 		   int socket_id,
952 		   const struct sfc_dp_tx_qcreate_info *info,
953 		   struct sfc_dp_txq **dp_txqp)
954 {
955 	struct sfc_efx_txq *txq;
956 	struct sfc_txq *ctrl_txq;
957 	int rc;
958 
959 	rc = ENOMEM;
960 	txq = rte_zmalloc_socket("sfc-efx-txq", sizeof(*txq),
961 				 RTE_CACHE_LINE_SIZE, socket_id);
962 	if (txq == NULL)
963 		goto fail_txq_alloc;
964 
965 	sfc_dp_queue_init(&txq->dp.dpq, port_id, queue_id, pci_addr);
966 
967 	rc = ENOMEM;
968 	txq->pend_desc = rte_calloc_socket("sfc-efx-txq-pend-desc",
969 					   EFX_TXQ_LIMIT(info->txq_entries),
970 					   sizeof(*txq->pend_desc), 0,
971 					   socket_id);
972 	if (txq->pend_desc == NULL)
973 		goto fail_pend_desc_alloc;
974 
975 	rc = ENOMEM;
976 	txq->sw_ring = rte_calloc_socket("sfc-efx-txq-sw_ring",
977 					 info->txq_entries,
978 					 sizeof(*txq->sw_ring),
979 					 RTE_CACHE_LINE_SIZE, socket_id);
980 	if (txq->sw_ring == NULL)
981 		goto fail_sw_ring_alloc;
982 
983 	ctrl_txq = sfc_txq_by_dp_txq(&txq->dp);
984 	if (ctrl_txq->evq->sa->tso) {
985 		rc = sfc_efx_tso_alloc_tsoh_objs(txq->sw_ring,
986 						 info->txq_entries, socket_id);
987 		if (rc != 0)
988 			goto fail_alloc_tsoh_objs;
989 	}
990 
991 	txq->evq = ctrl_txq->evq;
992 	txq->ptr_mask = info->txq_entries - 1;
993 	txq->max_fill_level = info->max_fill_level;
994 	txq->free_thresh = info->free_thresh;
995 	txq->dma_desc_size_max = info->dma_desc_size_max;
996 
997 	*dp_txqp = &txq->dp;
998 	return 0;
999 
1000 fail_alloc_tsoh_objs:
1001 	rte_free(txq->sw_ring);
1002 
1003 fail_sw_ring_alloc:
1004 	rte_free(txq->pend_desc);
1005 
1006 fail_pend_desc_alloc:
1007 	rte_free(txq);
1008 
1009 fail_txq_alloc:
1010 	return rc;
1011 }
1012 
1013 static sfc_dp_tx_qdestroy_t sfc_efx_tx_qdestroy;
1014 static void
1015 sfc_efx_tx_qdestroy(struct sfc_dp_txq *dp_txq)
1016 {
1017 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
1018 
1019 	sfc_efx_tso_free_tsoh_objs(txq->sw_ring, txq->ptr_mask + 1);
1020 	rte_free(txq->sw_ring);
1021 	rte_free(txq->pend_desc);
1022 	rte_free(txq);
1023 }
1024 
1025 static sfc_dp_tx_qstart_t sfc_efx_tx_qstart;
1026 static int
1027 sfc_efx_tx_qstart(struct sfc_dp_txq *dp_txq,
1028 		  __rte_unused unsigned int evq_read_ptr,
1029 		  unsigned int txq_desc_index)
1030 {
1031 	/* libefx-based datapath is specific to libefx-based PMD */
1032 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
1033 	struct sfc_txq *ctrl_txq = sfc_txq_by_dp_txq(dp_txq);
1034 
1035 	txq->common = ctrl_txq->common;
1036 
1037 	txq->pending = txq->completed = txq->added = txq_desc_index;
1038 	txq->hw_vlan_tci = 0;
1039 
1040 	txq->flags |= (SFC_EFX_TXQ_FLAG_STARTED | SFC_EFX_TXQ_FLAG_RUNNING);
1041 
1042 	return 0;
1043 }
1044 
1045 static sfc_dp_tx_qstop_t sfc_efx_tx_qstop;
1046 static void
1047 sfc_efx_tx_qstop(struct sfc_dp_txq *dp_txq,
1048 		 __rte_unused unsigned int *evq_read_ptr)
1049 {
1050 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
1051 
1052 	txq->flags &= ~SFC_EFX_TXQ_FLAG_RUNNING;
1053 }
1054 
1055 static sfc_dp_tx_qreap_t sfc_efx_tx_qreap;
1056 static void
1057 sfc_efx_tx_qreap(struct sfc_dp_txq *dp_txq)
1058 {
1059 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
1060 	unsigned int txds;
1061 
1062 	sfc_efx_tx_reap(txq);
1063 
1064 	for (txds = 0; txds <= txq->ptr_mask; txds++) {
1065 		if (txq->sw_ring[txds].mbuf != NULL) {
1066 			rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
1067 			txq->sw_ring[txds].mbuf = NULL;
1068 		}
1069 	}
1070 
1071 	txq->flags &= ~SFC_EFX_TXQ_FLAG_STARTED;
1072 }
1073 
1074 static sfc_dp_tx_qdesc_status_t sfc_efx_tx_qdesc_status;
1075 static int
1076 sfc_efx_tx_qdesc_status(struct sfc_dp_txq *dp_txq, uint16_t offset)
1077 {
1078 	struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
1079 
1080 	if (unlikely(offset > txq->ptr_mask))
1081 		return -EINVAL;
1082 
1083 	if (unlikely(offset >= txq->max_fill_level))
1084 		return RTE_ETH_TX_DESC_UNAVAIL;
1085 
1086 	/*
1087 	 * Poll EvQ to derive up-to-date 'txq->pending' figure;
1088 	 * it is required for the queue to be running, but the
1089 	 * check is omitted because API design assumes that it
1090 	 * is the duty of the caller to satisfy all conditions
1091 	 */
1092 	SFC_ASSERT((txq->flags & SFC_EFX_TXQ_FLAG_RUNNING) ==
1093 		   SFC_EFX_TXQ_FLAG_RUNNING);
1094 	sfc_ev_qpoll(txq->evq);
1095 
1096 	/*
1097 	 * Ring tail is 'txq->pending', and although descriptors
1098 	 * between 'txq->completed' and 'txq->pending' are still
1099 	 * in use by the driver, they should be reported as DONE
1100 	 */
1101 	if (unlikely(offset < (txq->added - txq->pending)))
1102 		return RTE_ETH_TX_DESC_FULL;
1103 
1104 	/*
1105 	 * There is no separate return value for unused descriptors;
1106 	 * the latter will be reported as DONE because genuine DONE
1107 	 * descriptors will be freed anyway in SW on the next burst
1108 	 */
1109 	return RTE_ETH_TX_DESC_DONE;
1110 }
1111 
1112 struct sfc_dp_tx sfc_efx_tx = {
1113 	.dp = {
1114 		.name		= SFC_KVARG_DATAPATH_EFX,
1115 		.type		= SFC_DP_TX,
1116 		.hw_fw_caps	= 0,
1117 	},
1118 	.features		= SFC_DP_TX_FEAT_VLAN_INSERT |
1119 				  SFC_DP_TX_FEAT_TSO |
1120 				  SFC_DP_TX_FEAT_MULTI_POOL |
1121 				  SFC_DP_TX_FEAT_REFCNT |
1122 				  SFC_DP_TX_FEAT_MULTI_SEG,
1123 	.qsize_up_rings		= sfc_efx_tx_qsize_up_rings,
1124 	.qcreate		= sfc_efx_tx_qcreate,
1125 	.qdestroy		= sfc_efx_tx_qdestroy,
1126 	.qstart			= sfc_efx_tx_qstart,
1127 	.qstop			= sfc_efx_tx_qstop,
1128 	.qreap			= sfc_efx_tx_qreap,
1129 	.qdesc_status		= sfc_efx_tx_qdesc_status,
1130 	.pkt_burst		= sfc_efx_xmit_pkts,
1131 };
1132