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