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