382bd208ceeccae1eb9a56794a71fd990101a1af
[dpdk.git] / drivers / net / mlx5 / mlx5_txpp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 #include <rte_ether.h>
5 #include <rte_ethdev_driver.h>
6 #include <rte_interrupts.h>
7 #include <rte_alarm.h>
8 #include <rte_malloc.h>
9
10 #include "mlx5.h"
11 #include "mlx5_rxtx.h"
12
13 /* Destroy Event Queue Notification Channel. */
14 static void
15 mlx5_txpp_destroy_eqn(struct mlx5_dev_ctx_shared *sh)
16 {
17         if (sh->txpp.echan) {
18                 mlx5_glue->devx_destroy_event_channel(sh->txpp.echan);
19                 sh->txpp.echan = NULL;
20         }
21         sh->txpp.eqn = 0;
22 }
23
24 /* Create Event Queue Notification Channel. */
25 static int
26 mlx5_txpp_create_eqn(struct mlx5_dev_ctx_shared *sh)
27 {
28         uint32_t lcore;
29
30         MLX5_ASSERT(!sh->txpp.echan);
31         lcore = (uint32_t)rte_lcore_to_cpu_id(-1);
32         if (mlx5_glue->devx_query_eqn(sh->ctx, lcore, &sh->txpp.eqn)) {
33                 rte_errno = errno;
34                 DRV_LOG(ERR, "Failed to query EQ number %d.", rte_errno);
35                 sh->txpp.eqn = 0;
36                 return -rte_errno;
37         }
38         sh->txpp.echan = mlx5_glue->devx_create_event_channel(sh->ctx,
39                         MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA);
40         if (!sh->txpp.echan) {
41                 sh->txpp.eqn = 0;
42                 rte_errno = errno;
43                 DRV_LOG(ERR, "Failed to create event channel %d.",
44                         rte_errno);
45                 return -rte_errno;
46         }
47         return 0;
48 }
49
50 static void
51 mlx5_txpp_destroy_clock_queue(struct mlx5_dev_ctx_shared *sh)
52 {
53         struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
54
55         if (wq->sq)
56                 claim_zero(mlx5_devx_cmd_destroy(wq->sq));
57         if (wq->sq_umem)
58                 claim_zero(mlx5_glue->devx_umem_dereg(wq->sq_umem));
59         if (wq->sq_buf)
60                 rte_free((void *)(uintptr_t)wq->sq_buf);
61         if (wq->cq)
62                 claim_zero(mlx5_devx_cmd_destroy(wq->cq));
63         if (wq->cq_umem)
64                 claim_zero(mlx5_glue->devx_umem_dereg(wq->cq_umem));
65         if (wq->cq_buf)
66                 rte_free((void *)(uintptr_t)wq->cq_buf);
67         memset(wq, 0, sizeof(*wq));
68 }
69
70 static void
71 mlx5_txpp_fill_wqe_clock_queue(struct mlx5_dev_ctx_shared *sh)
72 {
73         struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
74         struct mlx5_wqe *wqe = (struct mlx5_wqe *)(uintptr_t)wq->wqes;
75         struct mlx5_wqe_cseg *cs = &wqe->cseg;
76         uint32_t wqe_size, opcode, i;
77         uint8_t *dst;
78
79         /* For test purposes fill the WQ with SEND inline packet. */
80         if (sh->txpp.test) {
81                 wqe_size = RTE_ALIGN(MLX5_TXPP_TEST_PKT_SIZE +
82                                      MLX5_WQE_CSEG_SIZE +
83                                      2 * MLX5_WQE_ESEG_SIZE -
84                                      MLX5_ESEG_MIN_INLINE_SIZE,
85                                      MLX5_WSEG_SIZE);
86                 opcode = MLX5_OPCODE_SEND;
87         } else {
88                 wqe_size = MLX5_WSEG_SIZE;
89                 opcode = MLX5_OPCODE_NOP;
90         }
91         cs->opcode = rte_cpu_to_be_32(opcode | 0); /* Index is ignored. */
92         cs->sq_ds = rte_cpu_to_be_32((wq->sq->id << 8) |
93                                      (wqe_size / MLX5_WSEG_SIZE));
94         cs->flags = RTE_BE32(MLX5_COMP_ALWAYS << MLX5_COMP_MODE_OFFSET);
95         cs->misc = RTE_BE32(0);
96         wqe_size = RTE_ALIGN(wqe_size, MLX5_WQE_SIZE);
97         if (sh->txpp.test) {
98                 struct mlx5_wqe_eseg *es = &wqe->eseg;
99                 struct rte_ether_hdr *eth_hdr;
100                 struct rte_ipv4_hdr *ip_hdr;
101                 struct rte_udp_hdr *udp_hdr;
102
103                 /* Build the inline test packet pattern. */
104                 MLX5_ASSERT(wqe_size <= MLX5_WQE_SIZE_MAX);
105                 MLX5_ASSERT(MLX5_TXPP_TEST_PKT_SIZE >=
106                                 (sizeof(struct rte_ether_hdr) +
107                                  sizeof(struct rte_ipv4_hdr)));
108                 es->flags = 0;
109                 es->cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
110                 es->swp_offs = 0;
111                 es->metadata = 0;
112                 es->swp_flags = 0;
113                 es->mss = 0;
114                 es->inline_hdr_sz = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE);
115                 /* Build test packet L2 header (Ethernet). */
116                 dst = (uint8_t *)&es->inline_data;
117                 eth_hdr = (struct rte_ether_hdr *)dst;
118                 rte_eth_random_addr(&eth_hdr->d_addr.addr_bytes[0]);
119                 rte_eth_random_addr(&eth_hdr->s_addr.addr_bytes[0]);
120                 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
121                 /* Build test packet L3 header (IP v4). */
122                 dst += sizeof(struct rte_ether_hdr);
123                 ip_hdr = (struct rte_ipv4_hdr *)dst;
124                 ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
125                 ip_hdr->type_of_service = 0;
126                 ip_hdr->fragment_offset = 0;
127                 ip_hdr->time_to_live = 64;
128                 ip_hdr->next_proto_id = IPPROTO_UDP;
129                 ip_hdr->packet_id = 0;
130                 ip_hdr->total_length = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE -
131                                                 sizeof(struct rte_ether_hdr));
132                 /* use RFC5735 / RFC2544 reserved network test addresses */
133                 ip_hdr->src_addr = RTE_BE32((198U << 24) | (18 << 16) |
134                                             (0 << 8) | 1);
135                 ip_hdr->dst_addr = RTE_BE32((198U << 24) | (18 << 16) |
136                                             (0 << 8) | 2);
137                 if (MLX5_TXPP_TEST_PKT_SIZE <
138                                         (sizeof(struct rte_ether_hdr) +
139                                          sizeof(struct rte_ipv4_hdr) +
140                                          sizeof(struct rte_udp_hdr)))
141                         goto wcopy;
142                 /* Build test packet L4 header (UDP). */
143                 dst += sizeof(struct rte_ipv4_hdr);
144                 udp_hdr = (struct rte_udp_hdr *)dst;
145                 udp_hdr->src_port = RTE_BE16(9); /* RFC863 Discard. */
146                 udp_hdr->dst_port = RTE_BE16(9);
147                 udp_hdr->dgram_len = RTE_BE16(MLX5_TXPP_TEST_PKT_SIZE -
148                                               sizeof(struct rte_ether_hdr) -
149                                               sizeof(struct rte_ipv4_hdr));
150                 udp_hdr->dgram_cksum = 0;
151                 /* Fill the test packet data. */
152                 dst += sizeof(struct rte_udp_hdr);
153                 for (i = sizeof(struct rte_ether_hdr) +
154                         sizeof(struct rte_ipv4_hdr) +
155                         sizeof(struct rte_udp_hdr);
156                                 i < MLX5_TXPP_TEST_PKT_SIZE; i++)
157                         *dst++ = (uint8_t)(i & 0xFF);
158         }
159 wcopy:
160         /* Duplicate the pattern to the next WQEs. */
161         dst = (uint8_t *)(uintptr_t)wq->sq_buf;
162         for (i = 1; i < MLX5_TXPP_CLKQ_SIZE; i++) {
163                 dst += wqe_size;
164                 rte_memcpy(dst, (void *)(uintptr_t)wq->sq_buf, wqe_size);
165         }
166 }
167
168 /* Creates the Clock Queue for packet pacing, returns zero on success. */
169 static int
170 mlx5_txpp_create_clock_queue(struct mlx5_dev_ctx_shared *sh)
171 {
172         struct mlx5_devx_create_sq_attr sq_attr = { 0 };
173         struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
174         struct mlx5_devx_cq_attr cq_attr = { 0 };
175         struct mlx5_txpp_wq *wq = &sh->txpp.clock_queue;
176         size_t page_size = sysconf(_SC_PAGESIZE);
177         uint32_t umem_size, umem_dbrec;
178         int ret;
179
180         /* Allocate memory buffer for CQEs and doorbell record. */
181         umem_size = sizeof(struct mlx5_cqe) * MLX5_TXPP_CLKQ_SIZE;
182         umem_dbrec = RTE_ALIGN(umem_size, MLX5_DBR_SIZE);
183         umem_size += MLX5_DBR_SIZE;
184         wq->cq_buf = rte_zmalloc_socket(__func__, umem_size,
185                                         page_size, sh->numa_node);
186         if (!wq->cq_buf) {
187                 DRV_LOG(ERR, "Failed to allocate memory for Clock Queue.");
188                 return -ENOMEM;
189         }
190         /* Register allocated buffer in user space with DevX. */
191         wq->cq_umem = mlx5_glue->devx_umem_reg(sh->ctx,
192                                                (void *)(uintptr_t)wq->cq_buf,
193                                                umem_size,
194                                                IBV_ACCESS_LOCAL_WRITE);
195         if (!wq->cq_umem) {
196                 rte_errno = errno;
197                 DRV_LOG(ERR, "Failed to register umem for Clock Queue.");
198                 goto error;
199         }
200         /* Create completion queue object for Clock Queue. */
201         cq_attr.cqe_size = (sizeof(struct mlx5_cqe) == 128) ?
202                             MLX5_CQE_SIZE_128B : MLX5_CQE_SIZE_64B;
203         cq_attr.use_first_only = 1;
204         cq_attr.overrun_ignore = 1;
205         cq_attr.uar_page_id = sh->tx_uar->page_id;
206         cq_attr.eqn = sh->txpp.eqn;
207         cq_attr.q_umem_valid = 1;
208         cq_attr.q_umem_offset = 0;
209         cq_attr.q_umem_id = wq->cq_umem->umem_id;
210         cq_attr.db_umem_valid = 1;
211         cq_attr.db_umem_offset = umem_dbrec;
212         cq_attr.db_umem_id = wq->cq_umem->umem_id;
213         cq_attr.log_cq_size = rte_log2_u32(MLX5_TXPP_CLKQ_SIZE);
214         cq_attr.log_page_size = rte_log2_u32(page_size);
215         wq->cq = mlx5_devx_cmd_create_cq(sh->ctx, &cq_attr);
216         if (!wq->cq) {
217                 rte_errno = errno;
218                 DRV_LOG(ERR, "Failed to create CQ for Clock Queue.");
219                 goto error;
220         }
221         wq->cq_dbrec = RTE_PTR_ADD(wq->cq_buf, umem_dbrec);
222         wq->cq_ci = 0;
223         /* Allocate memory buffer for Send Queue WQEs. */
224         if (sh->txpp.test) {
225                 wq->sq_size = RTE_ALIGN(MLX5_TXPP_TEST_PKT_SIZE +
226                                         MLX5_WQE_CSEG_SIZE +
227                                         2 * MLX5_WQE_ESEG_SIZE -
228                                         MLX5_ESEG_MIN_INLINE_SIZE,
229                                         MLX5_WQE_SIZE) / MLX5_WQE_SIZE;
230                 wq->sq_size *= MLX5_TXPP_CLKQ_SIZE;
231         } else {
232                 wq->sq_size = MLX5_TXPP_CLKQ_SIZE;
233         }
234         /* There should not be WQE leftovers in the cyclic queue. */
235         MLX5_ASSERT(wq->sq_size == (1 << log2above(wq->sq_size)));
236         umem_size =  MLX5_WQE_SIZE * wq->sq_size;
237         umem_dbrec = RTE_ALIGN(umem_size, MLX5_DBR_SIZE);
238         umem_size += MLX5_DBR_SIZE;
239         wq->sq_buf = rte_zmalloc_socket(__func__, umem_size,
240                                         page_size, sh->numa_node);
241         if (!wq->sq_buf) {
242                 DRV_LOG(ERR, "Failed to allocate memory for Clock Queue.");
243                 rte_errno = ENOMEM;
244                 goto error;
245         }
246         /* Register allocated buffer in user space with DevX. */
247         wq->sq_umem = mlx5_glue->devx_umem_reg(sh->ctx,
248                                                (void *)(uintptr_t)wq->sq_buf,
249                                                umem_size,
250                                                IBV_ACCESS_LOCAL_WRITE);
251         if (!wq->sq_umem) {
252                 rte_errno = errno;
253                 DRV_LOG(ERR, "Failed to register umem for Clock Queue.");
254                 goto error;
255         }
256         /* Create send queue object for Clock Queue. */
257         if (sh->txpp.test) {
258                 sq_attr.tis_lst_sz = 1;
259                 sq_attr.tis_num = sh->tis->id;
260                 sq_attr.non_wire = 0;
261                 sq_attr.static_sq_wq = 1;
262         } else {
263                 sq_attr.non_wire = 1;
264                 sq_attr.static_sq_wq = 1;
265         }
266         sq_attr.state = MLX5_SQC_STATE_RST;
267         sq_attr.cqn = wq->cq->id;
268         sq_attr.wq_attr.cd_slave = 1;
269         sq_attr.wq_attr.uar_page = sh->tx_uar->page_id;
270         sq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
271         sq_attr.wq_attr.pd = sh->pdn;
272         sq_attr.wq_attr.log_wq_stride = rte_log2_u32(MLX5_WQE_SIZE);
273         sq_attr.wq_attr.log_wq_sz = rte_log2_u32(wq->sq_size);
274         sq_attr.wq_attr.dbr_umem_valid = 1;
275         sq_attr.wq_attr.dbr_addr = umem_dbrec;
276         sq_attr.wq_attr.dbr_umem_id = wq->sq_umem->umem_id;
277         sq_attr.wq_attr.wq_umem_valid = 1;
278         sq_attr.wq_attr.wq_umem_id = wq->sq_umem->umem_id;
279         /* umem_offset must be zero for static_sq_wq queue. */
280         sq_attr.wq_attr.wq_umem_offset = 0;
281         wq->sq = mlx5_devx_cmd_create_sq(sh->ctx, &sq_attr);
282         if (!wq->sq) {
283                 rte_errno = errno;
284                 DRV_LOG(ERR, "Failed to create SQ for Clock Queue.");
285                 goto error;
286         }
287         wq->sq_dbrec = RTE_PTR_ADD(wq->sq_buf, umem_dbrec +
288                                    MLX5_SND_DBR * sizeof(uint32_t));
289         /* Build the WQEs in the Send Queue before goto Ready state. */
290         mlx5_txpp_fill_wqe_clock_queue(sh);
291         /* Change queue state to ready. */
292         msq_attr.sq_state = MLX5_SQC_STATE_RST;
293         msq_attr.state = MLX5_SQC_STATE_RDY;
294         wq->sq_ci = 0;
295         ret = mlx5_devx_cmd_modify_sq(wq->sq, &msq_attr);
296         if (ret) {
297                 DRV_LOG(ERR, "Failed to set SQ ready state Clock Queue.");
298                 goto error;
299         }
300         return 0;
301 error:
302         ret = -rte_errno;
303         mlx5_txpp_destroy_clock_queue(sh);
304         rte_errno = -ret;
305         return ret;
306 }
307
308 /*
309  * The routine initializes the packet pacing infrastructure:
310  * - allocates PP context
311  * - Clock CQ/SQ
312  * - Rearm CQ/SQ
313  * - attaches rearm interrupt handler
314  *
315  * Returns 0 on success, negative otherwise
316  */
317 static int
318 mlx5_txpp_create(struct mlx5_dev_ctx_shared *sh, struct mlx5_priv *priv)
319 {
320         int tx_pp = priv->config.tx_pp;
321         int ret;
322
323         /* Store the requested pacing parameters. */
324         sh->txpp.tick = tx_pp >= 0 ? tx_pp : -tx_pp;
325         sh->txpp.test = !!(tx_pp < 0);
326         sh->txpp.skew = priv->config.tx_skew;
327         sh->txpp.freq = priv->config.hca_attr.dev_freq_khz;
328         ret = mlx5_txpp_create_eqn(sh);
329         if (ret)
330                 goto exit;
331         ret = mlx5_txpp_create_clock_queue(sh);
332         if (ret)
333                 goto exit;
334 exit:
335         if (ret) {
336                 mlx5_txpp_destroy_clock_queue(sh);
337                 mlx5_txpp_destroy_eqn(sh);
338                 sh->txpp.tick = 0;
339                 sh->txpp.test = 0;
340                 sh->txpp.skew = 0;
341         }
342         return ret;
343 }
344
345 /*
346  * The routine destroys the packet pacing infrastructure:
347  * - detaches rearm interrupt handler
348  * - Rearm CQ/SQ
349  * - Clock CQ/SQ
350  * - PP context
351  */
352 static void
353 mlx5_txpp_destroy(struct mlx5_dev_ctx_shared *sh)
354 {
355         mlx5_txpp_destroy_clock_queue(sh);
356         mlx5_txpp_destroy_eqn(sh);
357         sh->txpp.tick = 0;
358         sh->txpp.test = 0;
359         sh->txpp.skew = 0;
360 }
361
362 /**
363  * Creates and starts packet pacing infrastructure on specified device.
364  *
365  * @param dev
366  *   Pointer to Ethernet device structure.
367  *
368  * @return
369  *   0 on success, a negative errno value otherwise and rte_errno is set.
370  */
371 int
372 mlx5_txpp_start(struct rte_eth_dev *dev)
373 {
374         struct mlx5_priv *priv = dev->data->dev_private;
375         struct mlx5_dev_ctx_shared *sh = priv->sh;
376         int err = 0;
377         int ret;
378
379         if (!priv->config.tx_pp) {
380                 /* Packet pacing is not requested for the device. */
381                 MLX5_ASSERT(priv->txpp_en == 0);
382                 return 0;
383         }
384         if (priv->txpp_en) {
385                 /* Packet pacing is already enabled for the device. */
386                 MLX5_ASSERT(sh->txpp.refcnt);
387                 return 0;
388         }
389         if (priv->config.tx_pp > 0) {
390                 ret = rte_mbuf_dynflag_lookup
391                                 (RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
392                 if (ret < 0)
393                         return 0;
394         }
395         ret = pthread_mutex_lock(&sh->txpp.mutex);
396         MLX5_ASSERT(!ret);
397         RTE_SET_USED(ret);
398         if (sh->txpp.refcnt) {
399                 priv->txpp_en = 1;
400                 ++sh->txpp.refcnt;
401         } else {
402                 err = mlx5_txpp_create(sh, priv);
403                 if (!err) {
404                         MLX5_ASSERT(sh->txpp.tick);
405                         priv->txpp_en = 1;
406                         sh->txpp.refcnt = 1;
407                 } else {
408                         rte_errno = -err;
409                 }
410         }
411         ret = pthread_mutex_unlock(&sh->txpp.mutex);
412         MLX5_ASSERT(!ret);
413         RTE_SET_USED(ret);
414         return err;
415 }
416
417 /**
418  * Stops and destroys packet pacing infrastructure on specified device.
419  *
420  * @param dev
421  *   Pointer to Ethernet device structure.
422  *
423  * @return
424  *   0 on success, a negative errno value otherwise and rte_errno is set.
425  */
426 void
427 mlx5_txpp_stop(struct rte_eth_dev *dev)
428 {
429         struct mlx5_priv *priv = dev->data->dev_private;
430         struct mlx5_dev_ctx_shared *sh = priv->sh;
431         int ret;
432
433         if (!priv->txpp_en) {
434                 /* Packet pacing is already disabled for the device. */
435                 return;
436         }
437         priv->txpp_en = 0;
438         ret = pthread_mutex_lock(&sh->txpp.mutex);
439         MLX5_ASSERT(!ret);
440         RTE_SET_USED(ret);
441         MLX5_ASSERT(sh->txpp.refcnt);
442         if (!sh->txpp.refcnt || --sh->txpp.refcnt)
443                 return;
444         /* No references any more, do actual destroy. */
445         mlx5_txpp_destroy(sh);
446         ret = pthread_mutex_unlock(&sh->txpp.mutex);
447         MLX5_ASSERT(!ret);
448         RTE_SET_USED(ret);
449 }