xref: /dpdk/drivers/net/mlx5/mlx5_txpp.c (revision 99f9d799ce21ab22e922ffec8aad51d56e24d04d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 #include <fcntl.h>
5 #include <stdint.h>
6 
7 #include <rte_ether.h>
8 #include <ethdev_driver.h>
9 #include <rte_interrupts.h>
10 #include <rte_alarm.h>
11 #include <rte_malloc.h>
12 #include <rte_cycles.h>
13 #include <rte_eal_paging.h>
14 
15 #include <mlx5_malloc.h>
16 #include <mlx5_common_devx.h>
17 
18 #include "mlx5.h"
19 #include "mlx5_rx.h"
20 #include "mlx5_tx.h"
21 #include "mlx5_common_os.h"
22 
23 static_assert(sizeof(struct mlx5_cqe_ts) == sizeof(rte_int128_t),
24 		"Wrong timestamp CQE part size");
25 
26 static const char * const mlx5_txpp_stat_names[] = {
27 	"tx_pp_missed_interrupt_errors", /* Missed service interrupt. */
28 	"tx_pp_rearm_queue_errors", /* Rearm Queue errors. */
29 	"tx_pp_clock_queue_errors", /* Clock Queue errors. */
30 	"tx_pp_timestamp_past_errors", /* Timestamp in the past. */
31 	"tx_pp_timestamp_future_errors", /* Timestamp in the distant future. */
32 	"tx_pp_jitter", /* Timestamp jitter (one Clock Queue completion). */
33 	"tx_pp_wander", /* Timestamp wander (half of Clock Queue CQEs). */
34 	"tx_pp_sync_lost", /* Scheduling synchronization lost. */
35 };
36 
37 /* Destroy Event Queue Notification Channel. */
38 static void
39 mlx5_txpp_destroy_event_channel(struct mlx5_dev_ctx_shared *sh)
40 {
41 	if (sh->txpp.echan) {
42 		mlx5_os_devx_destroy_event_channel(sh->txpp.echan);
43 		sh->txpp.echan = NULL;
44 	}
45 }
46 
47 /* Create Event Queue Notification Channel. */
48 static int
49 mlx5_txpp_create_event_channel(struct mlx5_dev_ctx_shared *sh)
50 {
51 	MLX5_ASSERT(!sh->txpp.echan);
52 	sh->txpp.echan = mlx5_os_devx_create_event_channel(sh->ctx,
53 			MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA);
54 	if (!sh->txpp.echan) {
55 		rte_errno = errno;
56 		DRV_LOG(ERR, "Failed to create event channel %d.", rte_errno);
57 		return -rte_errno;
58 	}
59 	return 0;
60 }
61 
62 static void
63 mlx5_txpp_free_pp_index(struct mlx5_dev_ctx_shared *sh)
64 {
65 #ifdef HAVE_MLX5DV_PP_ALLOC
66 	if (sh->txpp.pp) {
67 		mlx5_glue->dv_free_pp(sh->txpp.pp);
68 		sh->txpp.pp = NULL;
69 		sh->txpp.pp_id = 0;
70 	}
71 #else
72 	RTE_SET_USED(sh);
73 	DRV_LOG(ERR, "Freeing pacing index is not supported.");
74 #endif
75 }
76 
77 /* Allocate Packet Pacing index from kernel via mlx5dv call. */
78 static int
79 mlx5_txpp_alloc_pp_index(struct mlx5_dev_ctx_shared *sh)
80 {
81 #ifdef HAVE_MLX5DV_PP_ALLOC
82 	uint32_t pp[MLX5_ST_SZ_DW(set_pp_rate_limit_context)];
83 	uint64_t rate;
84 
85 	MLX5_ASSERT(!sh->txpp.pp);
86 	memset(&pp, 0, sizeof(pp));
87 	rate = NS_PER_S / sh->txpp.tick;
88 	if (rate * sh->txpp.tick != NS_PER_S)
89 		DRV_LOG(WARNING, "Packet pacing frequency is not precise.");
90 	if (sh->txpp.test) {
91 		uint32_t len;
92 
93 		len = RTE_MAX(MLX5_TXPP_TEST_PKT_SIZE,
94 			      (size_t)RTE_ETHER_MIN_LEN);
95 		MLX5_SET(set_pp_rate_limit_context, &pp,
96 			 burst_upper_bound, len);
97 		MLX5_SET(set_pp_rate_limit_context, &pp,
98 			 typical_packet_size, len);
99 		/* Convert packets per second into kilobits. */
100 		rate = (rate * len) / (1000ul / CHAR_BIT);
101 		DRV_LOG(INFO, "Packet pacing rate set to %" PRIu64, rate);
102 	}
103 	MLX5_SET(set_pp_rate_limit_context, &pp, rate_limit, rate);
104 	MLX5_SET(set_pp_rate_limit_context, &pp, rate_mode,
105 		 sh->txpp.test ? MLX5_DATA_RATE : MLX5_WQE_RATE);
106 	sh->txpp.pp = mlx5_glue->dv_alloc_pp
107 				(sh->ctx, sizeof(pp), &pp,
108 				 MLX5DV_PP_ALLOC_FLAGS_DEDICATED_INDEX);
109 	if (sh->txpp.pp == NULL) {
110 		DRV_LOG(ERR, "Failed to allocate packet pacing index.");
111 		rte_errno = errno;
112 		return -errno;
113 	}
114 	if (!((struct mlx5dv_pp *)sh->txpp.pp)->index) {
115 		DRV_LOG(ERR, "Zero packet pacing index allocated.");
116 		mlx5_txpp_free_pp_index(sh);
117 		rte_errno = ENOTSUP;
118 		return -ENOTSUP;
119 	}
120 	sh->txpp.pp_id = ((struct mlx5dv_pp *)(sh->txpp.pp))->index;
121 	return 0;
122 #else
123 	RTE_SET_USED(sh);
124 	DRV_LOG(ERR, "Allocating pacing index is not supported.");
125 	rte_errno = ENOTSUP;
126 	return -ENOTSUP;
127 #endif
128 }
129 
130 static void
131 mlx5_txpp_destroy_send_queue(struct mlx5_txpp_wq *wq)
132 {
133 	mlx5_devx_sq_destroy(&wq->sq_obj);
134 	mlx5_devx_cq_destroy(&wq->cq_obj);
135 	memset(wq, 0, sizeof(*wq));
136 }
137 
138 static void
139 mlx5_txpp_destroy_rearm_queue(struct mlx5_dev_ctx_shared *sh)
140 {
141 	struct mlx5_txpp_wq *wq = &sh->txpp.rearm_queue;
142 
143 	mlx5_txpp_destroy_send_queue(wq);
144 }
145 
146 static void
147 mlx5_txpp_destroy_clock_queue(struct mlx5_dev_ctx_shared *sh)
148 {
149 	struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
150 
151 	mlx5_txpp_destroy_send_queue(wq);
152 	if (sh->txpp.tsa) {
153 		mlx5_free(sh->txpp.tsa);
154 		sh->txpp.tsa = NULL;
155 	}
156 }
157 
158 static void
159 mlx5_txpp_doorbell_rearm_queue(struct mlx5_dev_ctx_shared *sh, uint16_t ci)
160 {
161 	struct mlx5_txpp_wq *wq = &sh->txpp.rearm_queue;
162 	struct mlx5_wqe *wqe = (struct mlx5_wqe *)(uintptr_t)wq->sq_obj.wqes;
163 	union {
164 		uint32_t w32[2];
165 		uint64_t w64;
166 	} cs;
167 	void *reg_addr;
168 
169 	wq->sq_ci = ci + 1;
170 	cs.w32[0] = rte_cpu_to_be_32(rte_be_to_cpu_32
171 			(wqe[ci & (wq->sq_size - 1)].ctrl[0]) | (ci - 1) << 8);
172 	cs.w32[1] = wqe[ci & (wq->sq_size - 1)].ctrl[1];
173 	/* Update SQ doorbell record with new SQ ci. */
174 	rte_compiler_barrier();
175 	*wq->sq_obj.db_rec = rte_cpu_to_be_32(wq->sq_ci);
176 	/* Make sure the doorbell record is updated. */
177 	rte_wmb();
178 	/* Write to doorbel register to start processing. */
179 	reg_addr = mlx5_os_get_devx_uar_reg_addr(sh->tx_uar);
180 	__mlx5_uar_write64_relaxed(cs.w64, reg_addr, NULL);
181 	rte_wmb();
182 }
183 
184 static void
185 mlx5_txpp_fill_wqe_rearm_queue(struct mlx5_dev_ctx_shared *sh)
186 {
187 	struct mlx5_txpp_wq *wq = &sh->txpp.rearm_queue;
188 	struct mlx5_wqe *wqe = (struct mlx5_wqe *)(uintptr_t)wq->sq_obj.wqes;
189 	uint32_t i;
190 
191 	for (i = 0; i < wq->sq_size; i += 2) {
192 		struct mlx5_wqe_cseg *cs;
193 		struct mlx5_wqe_qseg *qs;
194 		uint32_t index;
195 
196 		/* Build SEND_EN request with slave WQE index. */
197 		cs = &wqe[i + 0].cseg;
198 		cs->opcode = RTE_BE32(MLX5_OPCODE_SEND_EN | 0);
199 		cs->sq_ds = rte_cpu_to_be_32((wq->sq_obj.sq->id << 8) | 2);
200 		cs->flags = RTE_BE32(MLX5_COMP_ALWAYS <<
201 				     MLX5_COMP_MODE_OFFSET);
202 		cs->misc = RTE_BE32(0);
203 		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
204 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM) &
205 			((1 << MLX5_WQ_INDEX_WIDTH) - 1);
206 		qs->max_index = rte_cpu_to_be_32(index);
207 		qs->qpn_cqn =
208 			   rte_cpu_to_be_32(sh->txpp.clock_queue.sq_obj.sq->id);
209 		/* Build WAIT request with slave CQE index. */
210 		cs = &wqe[i + 1].cseg;
211 		cs->opcode = RTE_BE32(MLX5_OPCODE_WAIT | 0);
212 		cs->sq_ds = rte_cpu_to_be_32((wq->sq_obj.sq->id << 8) | 2);
213 		cs->flags = RTE_BE32(MLX5_COMP_ONLY_ERR <<
214 				     MLX5_COMP_MODE_OFFSET);
215 		cs->misc = RTE_BE32(0);
216 		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
217 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM / 2) &
218 			((1 << MLX5_CQ_INDEX_WIDTH) - 1);
219 		qs->max_index = rte_cpu_to_be_32(index);
220 		qs->qpn_cqn =
221 			   rte_cpu_to_be_32(sh->txpp.clock_queue.cq_obj.cq->id);
222 	}
223 }
224 
225 /* Creates the Rearm Queue to fire the requests to Clock Queue in realtime. */
226 static int
227 mlx5_txpp_create_rearm_queue(struct mlx5_dev_ctx_shared *sh)
228 {
229 	struct mlx5_devx_create_sq_attr sq_attr = {
230 		.cd_master = 1,
231 		.state = MLX5_SQC_STATE_RST,
232 		.tis_lst_sz = 1,
233 		.tis_num = sh->tis->id,
234 		.wq_attr = (struct mlx5_devx_wq_attr){
235 			.pd = sh->pdn,
236 			.uar_page = mlx5_os_get_devx_uar_page_id(sh->tx_uar),
237 		},
238 		.ts_format = mlx5_ts_format_conv(sh->sq_ts_format),
239 	};
240 	struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
241 	struct mlx5_devx_cq_attr cq_attr = {
242 		.uar_page_id = mlx5_os_get_devx_uar_page_id(sh->tx_uar),
243 	};
244 	struct mlx5_txpp_wq *wq = &sh->txpp.rearm_queue;
245 	int ret;
246 
247 	/* Create completion queue object for Rearm Queue. */
248 	ret = mlx5_devx_cq_create(sh->ctx, &wq->cq_obj,
249 				  log2above(MLX5_TXPP_REARM_CQ_SIZE), &cq_attr,
250 				  sh->numa_node);
251 	if (ret) {
252 		DRV_LOG(ERR, "Failed to create CQ for Rearm Queue.");
253 		return ret;
254 	}
255 	wq->cq_ci = 0;
256 	wq->arm_sn = 0;
257 	wq->sq_size = MLX5_TXPP_REARM_SQ_SIZE;
258 	MLX5_ASSERT(wq->sq_size == (1 << log2above(wq->sq_size)));
259 	/* Create send queue object for Rearm Queue. */
260 	sq_attr.cqn = wq->cq_obj.cq->id;
261 	/* There should be no WQE leftovers in the cyclic queue. */
262 	ret = mlx5_devx_sq_create(sh->ctx, &wq->sq_obj,
263 				  log2above(MLX5_TXPP_REARM_SQ_SIZE), &sq_attr,
264 				  sh->numa_node);
265 	if (ret) {
266 		rte_errno = errno;
267 		DRV_LOG(ERR, "Failed to create SQ for Rearm Queue.");
268 		goto error;
269 	}
270 	/* Build the WQEs in the Send Queue before goto Ready state. */
271 	mlx5_txpp_fill_wqe_rearm_queue(sh);
272 	/* Change queue state to ready. */
273 	msq_attr.sq_state = MLX5_SQC_STATE_RST;
274 	msq_attr.state = MLX5_SQC_STATE_RDY;
275 	ret = mlx5_devx_cmd_modify_sq(wq->sq_obj.sq, &msq_attr);
276 	if (ret) {
277 		DRV_LOG(ERR, "Failed to set SQ ready state Rearm Queue.");
278 		goto error;
279 	}
280 	return 0;
281 error:
282 	ret = -rte_errno;
283 	mlx5_txpp_destroy_rearm_queue(sh);
284 	rte_errno = -ret;
285 	return ret;
286 }
287 
288 static void
289 mlx5_txpp_fill_wqe_clock_queue(struct mlx5_dev_ctx_shared *sh)
290 {
291 	struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
292 	struct mlx5_wqe *wqe = (struct mlx5_wqe *)(uintptr_t)wq->sq_obj.wqes;
293 	struct mlx5_wqe_cseg *cs = &wqe->cseg;
294 	uint32_t wqe_size, opcode, i;
295 	uint8_t *dst;
296 
297 	/* For test purposes fill the WQ with SEND inline packet. */
298 	if (sh->txpp.test) {
299 		wqe_size = RTE_ALIGN(MLX5_TXPP_TEST_PKT_SIZE +
300 				     MLX5_WQE_CSEG_SIZE +
301 				     2 * MLX5_WQE_ESEG_SIZE -
302 				     MLX5_ESEG_MIN_INLINE_SIZE,
303 				     MLX5_WSEG_SIZE);
304 		opcode = MLX5_OPCODE_SEND;
305 	} else {
306 		wqe_size = MLX5_WSEG_SIZE;
307 		opcode = MLX5_OPCODE_NOP;
308 	}
309 	cs->opcode = rte_cpu_to_be_32(opcode | 0); /* Index is ignored. */
310 	cs->sq_ds = rte_cpu_to_be_32((wq->sq_obj.sq->id << 8) |
311 				     (wqe_size / MLX5_WSEG_SIZE));
312 	cs->flags = RTE_BE32(MLX5_COMP_ALWAYS << MLX5_COMP_MODE_OFFSET);
313 	cs->misc = RTE_BE32(0);
314 	wqe_size = RTE_ALIGN(wqe_size, MLX5_WQE_SIZE);
315 	if (sh->txpp.test) {
316 		struct mlx5_wqe_eseg *es = &wqe->eseg;
317 		struct rte_ether_hdr *eth_hdr;
318 		struct rte_ipv4_hdr *ip_hdr;
319 		struct rte_udp_hdr *udp_hdr;
320 
321 		/* Build the inline test packet pattern. */
322 		MLX5_ASSERT(wqe_size <= MLX5_WQE_SIZE_MAX);
323 		MLX5_ASSERT(MLX5_TXPP_TEST_PKT_SIZE >=
324 				(sizeof(struct rte_ether_hdr) +
325 				 sizeof(struct rte_ipv4_hdr)));
326 		es->flags = 0;
327 		es->cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
328 		es->swp_offs = 0;
329 		es->metadata = 0;
330 		es->swp_flags = 0;
331 		es->mss = 0;
332 		es->inline_hdr_sz = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE);
333 		/* Build test packet L2 header (Ethernet). */
334 		dst = (uint8_t *)&es->inline_data;
335 		eth_hdr = (struct rte_ether_hdr *)dst;
336 		rte_eth_random_addr(&eth_hdr->d_addr.addr_bytes[0]);
337 		rte_eth_random_addr(&eth_hdr->s_addr.addr_bytes[0]);
338 		eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
339 		/* Build test packet L3 header (IP v4). */
340 		dst += sizeof(struct rte_ether_hdr);
341 		ip_hdr = (struct rte_ipv4_hdr *)dst;
342 		ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
343 		ip_hdr->type_of_service = 0;
344 		ip_hdr->fragment_offset = 0;
345 		ip_hdr->time_to_live = 64;
346 		ip_hdr->next_proto_id = IPPROTO_UDP;
347 		ip_hdr->packet_id = 0;
348 		ip_hdr->total_length = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE -
349 						sizeof(struct rte_ether_hdr));
350 		/* use RFC5735 / RFC2544 reserved network test addresses */
351 		ip_hdr->src_addr = RTE_BE32((198U << 24) | (18 << 16) |
352 					    (0 << 8) | 1);
353 		ip_hdr->dst_addr = RTE_BE32((198U << 24) | (18 << 16) |
354 					    (0 << 8) | 2);
355 		if (MLX5_TXPP_TEST_PKT_SIZE <
356 					(sizeof(struct rte_ether_hdr) +
357 					 sizeof(struct rte_ipv4_hdr) +
358 					 sizeof(struct rte_udp_hdr)))
359 			goto wcopy;
360 		/* Build test packet L4 header (UDP). */
361 		dst += sizeof(struct rte_ipv4_hdr);
362 		udp_hdr = (struct rte_udp_hdr *)dst;
363 		udp_hdr->src_port = RTE_BE16(9); /* RFC863 Discard. */
364 		udp_hdr->dst_port = RTE_BE16(9);
365 		udp_hdr->dgram_len = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE -
366 					      sizeof(struct rte_ether_hdr) -
367 					      sizeof(struct rte_ipv4_hdr));
368 		udp_hdr->dgram_cksum = 0;
369 		/* Fill the test packet data. */
370 		dst += sizeof(struct rte_udp_hdr);
371 		for (i = sizeof(struct rte_ether_hdr) +
372 			sizeof(struct rte_ipv4_hdr) +
373 			sizeof(struct rte_udp_hdr);
374 				i < MLX5_TXPP_TEST_PKT_SIZE; i++)
375 			*dst++ = (uint8_t)(i & 0xFF);
376 	}
377 wcopy:
378 	/* Duplicate the pattern to the next WQEs. */
379 	dst = (uint8_t *)(uintptr_t)wq->sq_obj.umem_buf;
380 	for (i = 1; i < MLX5_TXPP_CLKQ_SIZE; i++) {
381 		dst += wqe_size;
382 		rte_memcpy(dst, (void *)(uintptr_t)wq->sq_obj.umem_buf,
383 			   wqe_size);
384 	}
385 }
386 
387 /* Creates the Clock Queue for packet pacing, returns zero on success. */
388 static int
389 mlx5_txpp_create_clock_queue(struct mlx5_dev_ctx_shared *sh)
390 {
391 	struct mlx5_devx_create_sq_attr sq_attr = { 0 };
392 	struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
393 	struct mlx5_devx_cq_attr cq_attr = {
394 		.use_first_only = 1,
395 		.overrun_ignore = 1,
396 		.uar_page_id = mlx5_os_get_devx_uar_page_id(sh->tx_uar),
397 	};
398 	struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
399 	int ret;
400 
401 	sh->txpp.tsa = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
402 				   MLX5_TXPP_REARM_SQ_SIZE *
403 				   sizeof(struct mlx5_txpp_ts),
404 				   0, sh->numa_node);
405 	if (!sh->txpp.tsa) {
406 		DRV_LOG(ERR, "Failed to allocate memory for CQ stats.");
407 		return -ENOMEM;
408 	}
409 	sh->txpp.ts_p = 0;
410 	sh->txpp.ts_n = 0;
411 	/* Create completion queue object for Clock Queue. */
412 	ret = mlx5_devx_cq_create(sh->ctx, &wq->cq_obj,
413 				  log2above(MLX5_TXPP_CLKQ_SIZE), &cq_attr,
414 				  sh->numa_node);
415 	if (ret) {
416 		DRV_LOG(ERR, "Failed to create CQ for Clock Queue.");
417 		goto error;
418 	}
419 	wq->cq_ci = 0;
420 	/* Allocate memory buffer for Send Queue WQEs. */
421 	if (sh->txpp.test) {
422 		wq->sq_size = RTE_ALIGN(MLX5_TXPP_TEST_PKT_SIZE +
423 					MLX5_WQE_CSEG_SIZE +
424 					2 * MLX5_WQE_ESEG_SIZE -
425 					MLX5_ESEG_MIN_INLINE_SIZE,
426 					MLX5_WQE_SIZE) / MLX5_WQE_SIZE;
427 		wq->sq_size *= MLX5_TXPP_CLKQ_SIZE;
428 	} else {
429 		wq->sq_size = MLX5_TXPP_CLKQ_SIZE;
430 	}
431 	/* There should not be WQE leftovers in the cyclic queue. */
432 	MLX5_ASSERT(wq->sq_size == (1 << log2above(wq->sq_size)));
433 	/* Create send queue object for Clock Queue. */
434 	if (sh->txpp.test) {
435 		sq_attr.tis_lst_sz = 1;
436 		sq_attr.tis_num = sh->tis->id;
437 		sq_attr.non_wire = 0;
438 		sq_attr.static_sq_wq = 1;
439 	} else {
440 		sq_attr.non_wire = 1;
441 		sq_attr.static_sq_wq = 1;
442 	}
443 	sq_attr.cqn = wq->cq_obj.cq->id;
444 	sq_attr.packet_pacing_rate_limit_index = sh->txpp.pp_id;
445 	sq_attr.wq_attr.cd_slave = 1;
446 	sq_attr.wq_attr.uar_page = mlx5_os_get_devx_uar_page_id(sh->tx_uar);
447 	sq_attr.wq_attr.pd = sh->pdn;
448 	sq_attr.ts_format = mlx5_ts_format_conv(sh->sq_ts_format);
449 	ret = mlx5_devx_sq_create(sh->ctx, &wq->sq_obj, log2above(wq->sq_size),
450 				  &sq_attr, sh->numa_node);
451 	if (ret) {
452 		rte_errno = errno;
453 		DRV_LOG(ERR, "Failed to create SQ for Clock Queue.");
454 		goto error;
455 	}
456 	/* Build the WQEs in the Send Queue before goto Ready state. */
457 	mlx5_txpp_fill_wqe_clock_queue(sh);
458 	/* Change queue state to ready. */
459 	msq_attr.sq_state = MLX5_SQC_STATE_RST;
460 	msq_attr.state = MLX5_SQC_STATE_RDY;
461 	wq->sq_ci = 0;
462 	ret = mlx5_devx_cmd_modify_sq(wq->sq_obj.sq, &msq_attr);
463 	if (ret) {
464 		DRV_LOG(ERR, "Failed to set SQ ready state Clock Queue.");
465 		goto error;
466 	}
467 	return 0;
468 error:
469 	ret = -rte_errno;
470 	mlx5_txpp_destroy_clock_queue(sh);
471 	rte_errno = -ret;
472 	return ret;
473 }
474 
475 /* Enable notification from the Rearm Queue CQ. */
476 static inline void
477 mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
478 {
479 	void *base_addr;
480 
481 	struct mlx5_txpp_wq *aq = &sh->txpp.rearm_queue;
482 	uint32_t arm_sn = aq->arm_sn << MLX5_CQ_SQN_OFFSET;
483 	uint32_t db_hi = arm_sn | MLX5_CQ_DBR_CMD_ALL | aq->cq_ci;
484 	uint64_t db_be =
485 		rte_cpu_to_be_64(((uint64_t)db_hi << 32) | aq->cq_obj.cq->id);
486 	base_addr = mlx5_os_get_devx_uar_base_addr(sh->tx_uar);
487 	uint32_t *addr = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL);
488 
489 	rte_compiler_barrier();
490 	aq->cq_obj.db_rec[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(db_hi);
491 	rte_wmb();
492 #ifdef RTE_ARCH_64
493 	*(uint64_t *)addr = db_be;
494 #else
495 	*(uint32_t *)addr = db_be;
496 	rte_io_wmb();
497 	*((uint32_t *)addr + 1) = db_be >> 32;
498 #endif
499 	aq->arm_sn++;
500 }
501 
502 #if defined(RTE_ARCH_X86_64)
503 static inline int
504 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
505 				rte_int128_t *exp,
506 				const rte_int128_t *src)
507 {
508 	uint8_t res;
509 
510 	asm volatile (MPLOCKED
511 		      "cmpxchg16b %[dst];"
512 		      " sete %[res]"
513 		      : [dst] "=m" (dst->val[0]),
514 			"=a" (exp->val[0]),
515 			"=d" (exp->val[1]),
516 			[res] "=r" (res)
517 		      : "b" (src->val[0]),
518 			"c" (src->val[1]),
519 			"a" (exp->val[0]),
520 			"d" (exp->val[1]),
521 			"m" (dst->val[0])
522 		      : "memory");
523 
524 	return res;
525 }
526 #endif
527 
528 static inline void
529 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)
530 {
531 	/*
532 	 * The only CQE of Clock Queue is being continuously
533 	 * update by hardware with soecified rate. We have to
534 	 * read timestump and WQE completion index atomically.
535 	 */
536 #if defined(RTE_ARCH_X86_64)
537 	rte_int128_t src;
538 
539 	memset(&src, 0, sizeof(src));
540 	*ts = src;
541 	/* if (*from == *ts) *from = *src else *ts = *from; */
542 	mlx5_atomic128_compare_exchange(from, ts, &src);
543 #else
544 	uint64_t *cqe = (uint64_t *)from;
545 
546 	/*
547 	 * Power architecture does not support 16B compare-and-swap.
548 	 * ARM implements it in software, code below is more relevant.
549 	 */
550 	for (;;) {
551 		uint64_t tm, op;
552 		uint64_t *ps;
553 
554 		rte_compiler_barrier();
555 		tm = __atomic_load_n(cqe + 0, __ATOMIC_RELAXED);
556 		op = __atomic_load_n(cqe + 1, __ATOMIC_RELAXED);
557 		rte_compiler_barrier();
558 		if (tm != __atomic_load_n(cqe + 0, __ATOMIC_RELAXED))
559 			continue;
560 		if (op != __atomic_load_n(cqe + 1, __ATOMIC_RELAXED))
561 			continue;
562 		ps = (uint64_t *)ts;
563 		ps[0] = tm;
564 		ps[1] = op;
565 		return;
566 	}
567 #endif
568 }
569 
570 /* Stores timestamp in the cache structure to share data with datapath. */
571 static inline void
572 mlx5_txpp_cache_timestamp(struct mlx5_dev_ctx_shared *sh,
573 			   uint64_t ts, uint64_t ci)
574 {
575 	ci = ci << (64 - MLX5_CQ_INDEX_WIDTH);
576 	ci |= (ts << MLX5_CQ_INDEX_WIDTH) >> MLX5_CQ_INDEX_WIDTH;
577 	rte_compiler_barrier();
578 	__atomic_store_n(&sh->txpp.ts.ts, ts, __ATOMIC_RELAXED);
579 	__atomic_store_n(&sh->txpp.ts.ci_ts, ci, __ATOMIC_RELAXED);
580 	rte_wmb();
581 }
582 
583 /* Reads timestamp from Clock Queue CQE and stores in the cache. */
584 static inline void
585 mlx5_txpp_update_timestamp(struct mlx5_dev_ctx_shared *sh)
586 {
587 	struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
588 	struct mlx5_cqe *cqe = (struct mlx5_cqe *)(uintptr_t)wq->cq_obj.cqes;
589 	union {
590 		rte_int128_t u128;
591 		struct mlx5_cqe_ts cts;
592 	} to;
593 	uint64_t ts;
594 	uint16_t ci;
595 
596 	mlx5_atomic_read_cqe((rte_int128_t *)&cqe->timestamp, &to.u128);
597 	if (to.cts.op_own >> 4) {
598 		DRV_LOG(DEBUG, "Clock Queue error sync lost.");
599 		__atomic_fetch_add(&sh->txpp.err_clock_queue,
600 				   1, __ATOMIC_RELAXED);
601 		sh->txpp.sync_lost = 1;
602 		return;
603 	}
604 	ci = rte_be_to_cpu_16(to.cts.wqe_counter);
605 	ts = rte_be_to_cpu_64(to.cts.timestamp);
606 	ts = mlx5_txpp_convert_rx_ts(sh, ts);
607 	wq->cq_ci += (ci - wq->sq_ci) & UINT16_MAX;
608 	wq->sq_ci = ci;
609 	mlx5_txpp_cache_timestamp(sh, ts, wq->cq_ci);
610 }
611 
612 /* Waits for the first completion on Clock Queue to init timestamp. */
613 static inline void
614 mlx5_txpp_init_timestamp(struct mlx5_dev_ctx_shared *sh)
615 {
616 	struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
617 	uint32_t wait;
618 
619 	sh->txpp.ts_p = 0;
620 	sh->txpp.ts_n = 0;
621 	for (wait = 0; wait < MLX5_TXPP_WAIT_INIT_TS; wait++) {
622 		mlx5_txpp_update_timestamp(sh);
623 		if (wq->sq_ci)
624 			return;
625 		/* Wait one millisecond and try again. */
626 		rte_delay_us_sleep(US_PER_S / MS_PER_S);
627 	}
628 	DRV_LOG(ERR, "Unable to initialize timestamp.");
629 	sh->txpp.sync_lost = 1;
630 }
631 
632 #ifdef HAVE_IBV_DEVX_EVENT
633 /* Gather statistics for timestamp from Clock Queue CQE. */
634 static inline void
635 mlx5_txpp_gather_timestamp(struct mlx5_dev_ctx_shared *sh)
636 {
637 	/* Check whether we have a valid timestamp. */
638 	if (!sh->txpp.clock_queue.sq_ci && !sh->txpp.ts_n)
639 		return;
640 	MLX5_ASSERT(sh->txpp.ts_p < MLX5_TXPP_REARM_SQ_SIZE);
641 	__atomic_store_n(&sh->txpp.tsa[sh->txpp.ts_p].ts,
642 			 sh->txpp.ts.ts, __ATOMIC_RELAXED);
643 	__atomic_store_n(&sh->txpp.tsa[sh->txpp.ts_p].ci_ts,
644 			 sh->txpp.ts.ci_ts, __ATOMIC_RELAXED);
645 	if (++sh->txpp.ts_p >= MLX5_TXPP_REARM_SQ_SIZE)
646 		sh->txpp.ts_p = 0;
647 	if (sh->txpp.ts_n < MLX5_TXPP_REARM_SQ_SIZE)
648 		++sh->txpp.ts_n;
649 }
650 
651 /* Handles Rearm Queue completions in periodic service. */
652 static __rte_always_inline void
653 mlx5_txpp_handle_rearm_queue(struct mlx5_dev_ctx_shared *sh)
654 {
655 	struct mlx5_txpp_wq *wq = &sh->txpp.rearm_queue;
656 	uint32_t cq_ci = wq->cq_ci;
657 	bool error = false;
658 	int ret;
659 
660 	do {
661 		volatile struct mlx5_cqe *cqe;
662 
663 		cqe = &wq->cq_obj.cqes[cq_ci & (MLX5_TXPP_REARM_CQ_SIZE - 1)];
664 		ret = check_cqe(cqe, MLX5_TXPP_REARM_CQ_SIZE, cq_ci);
665 		switch (ret) {
666 		case MLX5_CQE_STATUS_ERR:
667 			error = true;
668 			++cq_ci;
669 			break;
670 		case MLX5_CQE_STATUS_SW_OWN:
671 			wq->sq_ci += 2;
672 			++cq_ci;
673 			break;
674 		case MLX5_CQE_STATUS_HW_OWN:
675 			break;
676 		default:
677 			MLX5_ASSERT(false);
678 			break;
679 		}
680 	} while (ret != MLX5_CQE_STATUS_HW_OWN);
681 	if (likely(cq_ci != wq->cq_ci)) {
682 		/* Check whether we have missed interrupts. */
683 		if (cq_ci - wq->cq_ci != 1) {
684 			DRV_LOG(DEBUG, "Rearm Queue missed interrupt.");
685 			__atomic_fetch_add(&sh->txpp.err_miss_int,
686 					   1, __ATOMIC_RELAXED);
687 			/* Check sync lost on wqe index. */
688 			if (cq_ci - wq->cq_ci >=
689 				(((1UL << MLX5_WQ_INDEX_WIDTH) /
690 				  MLX5_TXPP_REARM) - 1))
691 				error = 1;
692 		}
693 		/* Update doorbell record to notify hardware. */
694 		rte_compiler_barrier();
695 		*wq->cq_obj.db_rec = rte_cpu_to_be_32(cq_ci);
696 		rte_wmb();
697 		wq->cq_ci = cq_ci;
698 		/* Fire new requests to Rearm Queue. */
699 		if (error) {
700 			DRV_LOG(DEBUG, "Rearm Queue error sync lost.");
701 			__atomic_fetch_add(&sh->txpp.err_rearm_queue,
702 					   1, __ATOMIC_RELAXED);
703 			sh->txpp.sync_lost = 1;
704 		}
705 	}
706 }
707 
708 /* Handles Clock Queue completions in periodic service. */
709 static __rte_always_inline void
710 mlx5_txpp_handle_clock_queue(struct mlx5_dev_ctx_shared *sh)
711 {
712 	mlx5_txpp_update_timestamp(sh);
713 	mlx5_txpp_gather_timestamp(sh);
714 }
715 #endif
716 
717 /* Invoked periodically on Rearm Queue completions. */
718 void
719 mlx5_txpp_interrupt_handler(void *cb_arg)
720 {
721 #ifndef HAVE_IBV_DEVX_EVENT
722 	RTE_SET_USED(cb_arg);
723 	return;
724 #else
725 	struct mlx5_dev_ctx_shared *sh = cb_arg;
726 	union {
727 		struct mlx5dv_devx_async_event_hdr event_resp;
728 		uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
729 	} out;
730 
731 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
732 	/* Process events in the loop. Only rearm completions are expected. */
733 	while (mlx5_glue->devx_get_event
734 				(sh->txpp.echan,
735 				 &out.event_resp,
736 				 sizeof(out.buf)) >=
737 				 (ssize_t)sizeof(out.event_resp.cookie)) {
738 		mlx5_txpp_handle_rearm_queue(sh);
739 		mlx5_txpp_handle_clock_queue(sh);
740 		mlx5_txpp_cq_arm(sh);
741 		mlx5_txpp_doorbell_rearm_queue
742 					(sh, sh->txpp.rearm_queue.sq_ci - 1);
743 	}
744 #endif /* HAVE_IBV_DEVX_ASYNC */
745 }
746 
747 static void
748 mlx5_txpp_stop_service(struct mlx5_dev_ctx_shared *sh)
749 {
750 	if (!sh->txpp.intr_handle.fd)
751 		return;
752 	mlx5_intr_callback_unregister(&sh->txpp.intr_handle,
753 				      mlx5_txpp_interrupt_handler, sh);
754 	sh->txpp.intr_handle.fd = 0;
755 }
756 
757 /* Attach interrupt handler and fires first request to Rearm Queue. */
758 static int
759 mlx5_txpp_start_service(struct mlx5_dev_ctx_shared *sh)
760 {
761 	uint16_t event_nums[1] = {0};
762 	int ret;
763 	int fd;
764 
765 	sh->txpp.err_miss_int = 0;
766 	sh->txpp.err_rearm_queue = 0;
767 	sh->txpp.err_clock_queue = 0;
768 	sh->txpp.err_ts_past = 0;
769 	sh->txpp.err_ts_future = 0;
770 	/* Attach interrupt handler to process Rearm Queue completions. */
771 	fd = mlx5_os_get_devx_channel_fd(sh->txpp.echan);
772 	ret = mlx5_os_set_nonblock_channel_fd(fd);
773 	if (ret) {
774 		DRV_LOG(ERR, "Failed to change event channel FD.");
775 		rte_errno = errno;
776 		return -rte_errno;
777 	}
778 	memset(&sh->txpp.intr_handle, 0, sizeof(sh->txpp.intr_handle));
779 	fd = mlx5_os_get_devx_channel_fd(sh->txpp.echan);
780 	sh->txpp.intr_handle.fd = fd;
781 	sh->txpp.intr_handle.type = RTE_INTR_HANDLE_EXT;
782 	if (rte_intr_callback_register(&sh->txpp.intr_handle,
783 				       mlx5_txpp_interrupt_handler, sh)) {
784 		sh->txpp.intr_handle.fd = 0;
785 		DRV_LOG(ERR, "Failed to register CQE interrupt %d.", rte_errno);
786 		return -rte_errno;
787 	}
788 	/* Subscribe CQ event to the event channel controlled by the driver. */
789 	ret = mlx5_os_devx_subscribe_devx_event(sh->txpp.echan,
790 					    sh->txpp.rearm_queue.cq_obj.cq->obj,
791 					     sizeof(event_nums), event_nums, 0);
792 	if (ret) {
793 		DRV_LOG(ERR, "Failed to subscribe CQE event.");
794 		rte_errno = errno;
795 		return -errno;
796 	}
797 	/* Enable interrupts in the CQ. */
798 	mlx5_txpp_cq_arm(sh);
799 	/* Fire the first request on Rearm Queue. */
800 	mlx5_txpp_doorbell_rearm_queue(sh, sh->txpp.rearm_queue.sq_size - 1);
801 	mlx5_txpp_init_timestamp(sh);
802 	return 0;
803 }
804 
805 /*
806  * The routine initializes the packet pacing infrastructure:
807  * - allocates PP context
808  * - Clock CQ/SQ
809  * - Rearm CQ/SQ
810  * - attaches rearm interrupt handler
811  * - starts Clock Queue
812  *
813  * Returns 0 on success, negative otherwise
814  */
815 static int
816 mlx5_txpp_create(struct mlx5_dev_ctx_shared *sh, struct mlx5_priv *priv)
817 {
818 	int tx_pp = priv->config.tx_pp;
819 	int ret;
820 
821 	/* Store the requested pacing parameters. */
822 	sh->txpp.tick = tx_pp >= 0 ? tx_pp : -tx_pp;
823 	sh->txpp.test = !!(tx_pp < 0);
824 	sh->txpp.skew = priv->config.tx_skew;
825 	sh->txpp.freq = priv->config.hca_attr.dev_freq_khz;
826 	ret = mlx5_txpp_create_event_channel(sh);
827 	if (ret)
828 		goto exit;
829 	ret = mlx5_txpp_alloc_pp_index(sh);
830 	if (ret)
831 		goto exit;
832 	ret = mlx5_txpp_create_clock_queue(sh);
833 	if (ret)
834 		goto exit;
835 	ret = mlx5_txpp_create_rearm_queue(sh);
836 	if (ret)
837 		goto exit;
838 	ret = mlx5_txpp_start_service(sh);
839 	if (ret)
840 		goto exit;
841 exit:
842 	if (ret) {
843 		mlx5_txpp_stop_service(sh);
844 		mlx5_txpp_destroy_rearm_queue(sh);
845 		mlx5_txpp_destroy_clock_queue(sh);
846 		mlx5_txpp_free_pp_index(sh);
847 		mlx5_txpp_destroy_event_channel(sh);
848 		sh->txpp.tick = 0;
849 		sh->txpp.test = 0;
850 		sh->txpp.skew = 0;
851 	}
852 	return ret;
853 }
854 
855 /*
856  * The routine destroys the packet pacing infrastructure:
857  * - detaches rearm interrupt handler
858  * - Rearm CQ/SQ
859  * - Clock CQ/SQ
860  * - PP context
861  */
862 static void
863 mlx5_txpp_destroy(struct mlx5_dev_ctx_shared *sh)
864 {
865 	mlx5_txpp_stop_service(sh);
866 	mlx5_txpp_destroy_rearm_queue(sh);
867 	mlx5_txpp_destroy_clock_queue(sh);
868 	mlx5_txpp_free_pp_index(sh);
869 	mlx5_txpp_destroy_event_channel(sh);
870 	sh->txpp.tick = 0;
871 	sh->txpp.test = 0;
872 	sh->txpp.skew = 0;
873 }
874 
875 /**
876  * Creates and starts packet pacing infrastructure on specified device.
877  *
878  * @param dev
879  *   Pointer to Ethernet device structure.
880  *
881  * @return
882  *   0 on success, a negative errno value otherwise and rte_errno is set.
883  */
884 int
885 mlx5_txpp_start(struct rte_eth_dev *dev)
886 {
887 	struct mlx5_priv *priv = dev->data->dev_private;
888 	struct mlx5_dev_ctx_shared *sh = priv->sh;
889 	int err = 0;
890 	int ret;
891 
892 	if (!priv->config.tx_pp) {
893 		/* Packet pacing is not requested for the device. */
894 		MLX5_ASSERT(priv->txpp_en == 0);
895 		return 0;
896 	}
897 	if (priv->txpp_en) {
898 		/* Packet pacing is already enabled for the device. */
899 		MLX5_ASSERT(sh->txpp.refcnt);
900 		return 0;
901 	}
902 	if (priv->config.tx_pp > 0) {
903 		ret = rte_mbuf_dynflag_lookup
904 				(RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
905 		if (ret < 0)
906 			return 0;
907 	}
908 	ret = pthread_mutex_lock(&sh->txpp.mutex);
909 	MLX5_ASSERT(!ret);
910 	RTE_SET_USED(ret);
911 	if (sh->txpp.refcnt) {
912 		priv->txpp_en = 1;
913 		++sh->txpp.refcnt;
914 	} else {
915 		err = mlx5_txpp_create(sh, priv);
916 		if (!err) {
917 			MLX5_ASSERT(sh->txpp.tick);
918 			priv->txpp_en = 1;
919 			sh->txpp.refcnt = 1;
920 		} else {
921 			rte_errno = -err;
922 		}
923 	}
924 	ret = pthread_mutex_unlock(&sh->txpp.mutex);
925 	MLX5_ASSERT(!ret);
926 	RTE_SET_USED(ret);
927 	return err;
928 }
929 
930 /**
931  * Stops and destroys packet pacing infrastructure on specified device.
932  *
933  * @param dev
934  *   Pointer to Ethernet device structure.
935  *
936  * @return
937  *   0 on success, a negative errno value otherwise and rte_errno is set.
938  */
939 void
940 mlx5_txpp_stop(struct rte_eth_dev *dev)
941 {
942 	struct mlx5_priv *priv = dev->data->dev_private;
943 	struct mlx5_dev_ctx_shared *sh = priv->sh;
944 	int ret;
945 
946 	if (!priv->txpp_en) {
947 		/* Packet pacing is already disabled for the device. */
948 		return;
949 	}
950 	priv->txpp_en = 0;
951 	ret = pthread_mutex_lock(&sh->txpp.mutex);
952 	MLX5_ASSERT(!ret);
953 	RTE_SET_USED(ret);
954 	MLX5_ASSERT(sh->txpp.refcnt);
955 	if (!sh->txpp.refcnt || --sh->txpp.refcnt)
956 		return;
957 	/* No references any more, do actual destroy. */
958 	mlx5_txpp_destroy(sh);
959 	ret = pthread_mutex_unlock(&sh->txpp.mutex);
960 	MLX5_ASSERT(!ret);
961 	RTE_SET_USED(ret);
962 }
963 
964 /*
965  * Read the current clock counter of an Ethernet device
966  *
967  * This returns the current raw clock value of an Ethernet device. It is
968  * a raw amount of ticks, with no given time reference.
969  * The value returned here is from the same clock than the one
970  * filling timestamp field of Rx/Tx packets when using hardware timestamp
971  * offload. Therefore it can be used to compute a precise conversion of
972  * the device clock to the real time.
973  *
974  * @param dev
975  *   Pointer to Ethernet device structure.
976  * @param clock
977  *   Pointer to the uint64_t that holds the raw clock value.
978  *
979  * @return
980  *   - 0: Success.
981  *   - -ENOTSUP: The function is not supported in this mode. Requires
982  *     packet pacing module configured and started (tx_pp devarg)
983  */
984 int
985 mlx5_txpp_read_clock(struct rte_eth_dev *dev, uint64_t *timestamp)
986 {
987 	struct mlx5_priv *priv = dev->data->dev_private;
988 	struct mlx5_dev_ctx_shared *sh = priv->sh;
989 	int ret;
990 
991 	if (sh->txpp.refcnt) {
992 		struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
993 		struct mlx5_cqe *cqe =
994 				(struct mlx5_cqe *)(uintptr_t)wq->cq_obj.cqes;
995 		union {
996 			rte_int128_t u128;
997 			struct mlx5_cqe_ts cts;
998 		} to;
999 		uint64_t ts;
1000 
1001 		mlx5_atomic_read_cqe((rte_int128_t *)&cqe->timestamp, &to.u128);
1002 		if (to.cts.op_own >> 4) {
1003 			DRV_LOG(DEBUG, "Clock Queue error sync lost.");
1004 			__atomic_fetch_add(&sh->txpp.err_clock_queue,
1005 					   1, __ATOMIC_RELAXED);
1006 			sh->txpp.sync_lost = 1;
1007 			return -EIO;
1008 		}
1009 		ts = rte_be_to_cpu_64(to.cts.timestamp);
1010 		ts = mlx5_txpp_convert_rx_ts(sh, ts);
1011 		*timestamp = ts;
1012 		return 0;
1013 	}
1014 	/* Not supported in isolated mode - kernel does not see the CQEs. */
1015 	if (priv->isolated || rte_eal_process_type() != RTE_PROC_PRIMARY)
1016 		return -ENOTSUP;
1017 	ret = mlx5_read_clock(dev, timestamp);
1018 	return ret;
1019 }
1020 
1021 /**
1022  * DPDK callback to clear device extended statistics.
1023  *
1024  * @param dev
1025  *   Pointer to Ethernet device structure.
1026  *
1027  * @return
1028  *   0 on success and stats is reset, negative errno value otherwise and
1029  *   rte_errno is set.
1030  */
1031 int mlx5_txpp_xstats_reset(struct rte_eth_dev *dev)
1032 {
1033 	struct mlx5_priv *priv = dev->data->dev_private;
1034 	struct mlx5_dev_ctx_shared *sh = priv->sh;
1035 
1036 	__atomic_store_n(&sh->txpp.err_miss_int, 0, __ATOMIC_RELAXED);
1037 	__atomic_store_n(&sh->txpp.err_rearm_queue, 0, __ATOMIC_RELAXED);
1038 	__atomic_store_n(&sh->txpp.err_clock_queue, 0, __ATOMIC_RELAXED);
1039 	__atomic_store_n(&sh->txpp.err_ts_past, 0, __ATOMIC_RELAXED);
1040 	__atomic_store_n(&sh->txpp.err_ts_future, 0, __ATOMIC_RELAXED);
1041 	return 0;
1042 }
1043 
1044 /**
1045  * Routine to retrieve names of extended device statistics
1046  * for packet send scheduling. It appends the specific stats names
1047  * after the parts filled by preceding modules (eth stats, etc.)
1048  *
1049  * @param dev
1050  *   Pointer to Ethernet device structure.
1051  * @param[out] xstats_names
1052  *   Buffer to insert names into.
1053  * @param n
1054  *   Number of names.
1055  * @param n_used
1056  *   Number of names filled by preceding statistics modules.
1057  *
1058  * @return
1059  *   Number of xstats names.
1060  */
1061 int mlx5_txpp_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
1062 			       struct rte_eth_xstat_name *xstats_names,
1063 			       unsigned int n, unsigned int n_used)
1064 {
1065 	unsigned int n_txpp = RTE_DIM(mlx5_txpp_stat_names);
1066 	unsigned int i;
1067 
1068 	if (n >= n_used + n_txpp && xstats_names) {
1069 		for (i = 0; i < n_txpp; ++i) {
1070 			strncpy(xstats_names[i + n_used].name,
1071 				mlx5_txpp_stat_names[i],
1072 				RTE_ETH_XSTATS_NAME_SIZE);
1073 			xstats_names[i + n_used].name
1074 					[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;
1075 		}
1076 	}
1077 	return n_used + n_txpp;
1078 }
1079 
1080 static inline void
1081 mlx5_txpp_read_tsa(struct mlx5_dev_txpp *txpp,
1082 		   struct mlx5_txpp_ts *tsa, uint16_t idx)
1083 {
1084 	do {
1085 		uint64_t ts, ci;
1086 
1087 		ts = __atomic_load_n(&txpp->tsa[idx].ts, __ATOMIC_RELAXED);
1088 		ci = __atomic_load_n(&txpp->tsa[idx].ci_ts, __ATOMIC_RELAXED);
1089 		rte_compiler_barrier();
1090 		if ((ci ^ ts) << MLX5_CQ_INDEX_WIDTH != 0)
1091 			continue;
1092 		if (__atomic_load_n(&txpp->tsa[idx].ts,
1093 				    __ATOMIC_RELAXED) != ts)
1094 			continue;
1095 		if (__atomic_load_n(&txpp->tsa[idx].ci_ts,
1096 				    __ATOMIC_RELAXED) != ci)
1097 			continue;
1098 		tsa->ts = ts;
1099 		tsa->ci_ts = ci;
1100 		return;
1101 	} while (true);
1102 }
1103 
1104 /*
1105  * Jitter reflects the clock change between
1106  * neighbours Clock Queue completions.
1107  */
1108 static uint64_t
1109 mlx5_txpp_xstats_jitter(struct mlx5_dev_txpp *txpp)
1110 {
1111 	struct mlx5_txpp_ts tsa0, tsa1;
1112 	int64_t dts, dci;
1113 	uint16_t ts_p;
1114 
1115 	if (txpp->ts_n < 2) {
1116 		/* No gathered enough reports yet. */
1117 		return 0;
1118 	}
1119 	do {
1120 		int ts_0, ts_1;
1121 
1122 		ts_p = txpp->ts_p;
1123 		rte_compiler_barrier();
1124 		ts_0 = ts_p - 2;
1125 		if (ts_0 < 0)
1126 			ts_0 += MLX5_TXPP_REARM_SQ_SIZE;
1127 		ts_1 = ts_p - 1;
1128 		if (ts_1 < 0)
1129 			ts_1 += MLX5_TXPP_REARM_SQ_SIZE;
1130 		mlx5_txpp_read_tsa(txpp, &tsa0, ts_0);
1131 		mlx5_txpp_read_tsa(txpp, &tsa1, ts_1);
1132 		rte_compiler_barrier();
1133 	} while (ts_p != txpp->ts_p);
1134 	/* We have two neighbor reports, calculate the jitter. */
1135 	dts = tsa1.ts - tsa0.ts;
1136 	dci = (tsa1.ci_ts >> (64 - MLX5_CQ_INDEX_WIDTH)) -
1137 	      (tsa0.ci_ts >> (64 - MLX5_CQ_INDEX_WIDTH));
1138 	if (dci < 0)
1139 		dci += 1 << MLX5_CQ_INDEX_WIDTH;
1140 	dci *= txpp->tick;
1141 	return (dts > dci) ? dts - dci : dci - dts;
1142 }
1143 
1144 /*
1145  * Wander reflects the long-term clock change
1146  * over the entire length of all Clock Queue completions.
1147  */
1148 static uint64_t
1149 mlx5_txpp_xstats_wander(struct mlx5_dev_txpp *txpp)
1150 {
1151 	struct mlx5_txpp_ts tsa0, tsa1;
1152 	int64_t dts, dci;
1153 	uint16_t ts_p;
1154 
1155 	if (txpp->ts_n < MLX5_TXPP_REARM_SQ_SIZE) {
1156 		/* No gathered enough reports yet. */
1157 		return 0;
1158 	}
1159 	do {
1160 		int ts_0, ts_1;
1161 
1162 		ts_p = txpp->ts_p;
1163 		rte_compiler_barrier();
1164 		ts_0 = ts_p - MLX5_TXPP_REARM_SQ_SIZE / 2 - 1;
1165 		if (ts_0 < 0)
1166 			ts_0 += MLX5_TXPP_REARM_SQ_SIZE;
1167 		ts_1 = ts_p - 1;
1168 		if (ts_1 < 0)
1169 			ts_1 += MLX5_TXPP_REARM_SQ_SIZE;
1170 		mlx5_txpp_read_tsa(txpp, &tsa0, ts_0);
1171 		mlx5_txpp_read_tsa(txpp, &tsa1, ts_1);
1172 		rte_compiler_barrier();
1173 	} while (ts_p != txpp->ts_p);
1174 	/* We have two neighbor reports, calculate the jitter. */
1175 	dts = tsa1.ts - tsa0.ts;
1176 	dci = (tsa1.ci_ts >> (64 - MLX5_CQ_INDEX_WIDTH)) -
1177 	      (tsa0.ci_ts >> (64 - MLX5_CQ_INDEX_WIDTH));
1178 	dci += 1 << MLX5_CQ_INDEX_WIDTH;
1179 	dci *= txpp->tick;
1180 	return (dts > dci) ? dts - dci : dci - dts;
1181 }
1182 
1183 /**
1184  * Routine to retrieve extended device statistics
1185  * for packet send scheduling. It appends the specific statistics
1186  * after the parts filled by preceding modules (eth stats, etc.)
1187  *
1188  * @param dev
1189  *   Pointer to Ethernet device.
1190  * @param[out] stats
1191  *   Pointer to rte extended stats table.
1192  * @param n
1193  *   The size of the stats table.
1194  * @param n_used
1195  *   Number of stats filled by preceding statistics modules.
1196  *
1197  * @return
1198  *   Number of extended stats on success and stats is filled,
1199  *   negative on error and rte_errno is set.
1200  */
1201 int
1202 mlx5_txpp_xstats_get(struct rte_eth_dev *dev,
1203 		     struct rte_eth_xstat *stats,
1204 		     unsigned int n, unsigned int n_used)
1205 {
1206 	unsigned int n_txpp = RTE_DIM(mlx5_txpp_stat_names);
1207 
1208 	if (n >= n_used + n_txpp && stats) {
1209 		struct mlx5_priv *priv = dev->data->dev_private;
1210 		struct mlx5_dev_ctx_shared *sh = priv->sh;
1211 		unsigned int i;
1212 
1213 		for (i = 0; i < n_txpp; ++i)
1214 			stats[n_used + i].id = n_used + i;
1215 		stats[n_used + 0].value =
1216 				__atomic_load_n(&sh->txpp.err_miss_int,
1217 						__ATOMIC_RELAXED);
1218 		stats[n_used + 1].value =
1219 				__atomic_load_n(&sh->txpp.err_rearm_queue,
1220 						__ATOMIC_RELAXED);
1221 		stats[n_used + 2].value =
1222 				__atomic_load_n(&sh->txpp.err_clock_queue,
1223 						__ATOMIC_RELAXED);
1224 		stats[n_used + 3].value =
1225 				__atomic_load_n(&sh->txpp.err_ts_past,
1226 						__ATOMIC_RELAXED);
1227 		stats[n_used + 4].value =
1228 				__atomic_load_n(&sh->txpp.err_ts_future,
1229 						__ATOMIC_RELAXED);
1230 		stats[n_used + 5].value = mlx5_txpp_xstats_jitter(&sh->txpp);
1231 		stats[n_used + 6].value = mlx5_txpp_xstats_wander(&sh->txpp);
1232 		stats[n_used + 7].value = sh->txpp.sync_lost;
1233 	}
1234 	return n_used + n_txpp;
1235 }
1236