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