Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2019 6WIND S.A.
3 : : * Copyright 2019 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <time.h>
9 : : #include <unistd.h>
10 : :
11 : : #include <rte_eal.h>
12 : : #include <ethdev_driver.h>
13 : : #include <rte_string_fns.h>
14 : :
15 : : #include "mlx4.h"
16 : : #include "mlx4_rxtx.h"
17 : : #include "mlx4_utils.h"
18 : :
19 : : /**
20 : : * Initialize IPC message.
21 : : *
22 : : * @param[in] dev
23 : : * Pointer to Ethernet structure.
24 : : * @param[out] msg
25 : : * Pointer to message to fill in.
26 : : * @param[in] type
27 : : * Message type.
28 : : */
29 : : static inline void
30 : 0 : mp_init_msg(struct rte_eth_dev *dev, struct rte_mp_msg *msg,
31 : : enum mlx4_mp_req_type type)
32 : : {
33 : : struct mlx4_mp_param *param = (struct mlx4_mp_param *)msg->param;
34 : :
35 : : memset(msg, 0, sizeof(*msg));
36 : 0 : strlcpy(msg->name, MLX4_MP_NAME, sizeof(msg->name));
37 : 0 : msg->len_param = sizeof(*param);
38 : 0 : param->type = type;
39 : 0 : param->port_id = dev->data->port_id;
40 : 0 : }
41 : :
42 : : /**
43 : : * IPC message handler of primary process.
44 : : *
45 : : * @param[in] dev
46 : : * Pointer to Ethernet structure.
47 : : * @param[in] peer
48 : : * Pointer to the peer socket path.
49 : : *
50 : : * @return
51 : : * 0 on success, negative errno value otherwise and rte_errno is set.
52 : : */
53 : : static int
54 : 0 : mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
55 : : {
56 : : struct rte_mp_msg mp_res;
57 : : struct mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
58 : : const struct mlx4_mp_param *param =
59 : : (const struct mlx4_mp_param *)mp_msg->param;
60 : : struct rte_eth_dev *dev;
61 : : struct mlx4_priv *priv;
62 : : struct mlx4_mr_cache entry;
63 : : uint32_t lkey;
64 : : int ret;
65 : :
66 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
67 [ # # ]: 0 : if (!rte_eth_dev_is_valid_port(param->port_id)) {
68 : 0 : rte_errno = ENODEV;
69 : 0 : ERROR("port %u invalid port ID", param->port_id);
70 : 0 : return -rte_errno;
71 : : }
72 : 0 : dev = &rte_eth_devices[param->port_id];
73 : 0 : priv = dev->data->dev_private;
74 [ # # # ]: 0 : switch (param->type) {
75 : 0 : case MLX4_MP_REQ_CREATE_MR:
76 : 0 : mp_init_msg(dev, &mp_res, param->type);
77 : 0 : lkey = mlx4_mr_create_primary(dev, &entry, param->args.addr);
78 [ # # ]: 0 : if (lkey == UINT32_MAX)
79 : 0 : res->result = -rte_errno;
80 : 0 : ret = rte_mp_reply(&mp_res, peer);
81 : 0 : break;
82 : 0 : case MLX4_MP_REQ_VERBS_CMD_FD:
83 : 0 : mp_init_msg(dev, &mp_res, param->type);
84 : 0 : mp_res.num_fds = 1;
85 : 0 : mp_res.fds[0] = priv->ctx->cmd_fd;
86 : 0 : res->result = 0;
87 : 0 : ret = rte_mp_reply(&mp_res, peer);
88 : 0 : break;
89 : 0 : default:
90 : 0 : rte_errno = EINVAL;
91 : 0 : ERROR("port %u invalid mp request type", dev->data->port_id);
92 : 0 : return -rte_errno;
93 : : }
94 : : return ret;
95 : : }
96 : :
97 : : /**
98 : : * IPC message handler of a secondary process.
99 : : *
100 : : * @param[in] dev
101 : : * Pointer to Ethernet structure.
102 : : * @param[in] peer
103 : : * Pointer to the peer socket path.
104 : : *
105 : : * @return
106 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
107 : : */
108 : : static int
109 : 0 : mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
110 : : {
111 : : struct rte_mp_msg mp_res;
112 : : struct mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
113 : : const struct mlx4_mp_param *param =
114 : : (const struct mlx4_mp_param *)mp_msg->param;
115 : : struct rte_eth_dev *dev;
116 : : #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
117 : : struct mlx4_proc_priv *ppriv;
118 : : #endif
119 : : int ret;
120 : :
121 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
122 [ # # ]: 0 : if (!rte_eth_dev_is_valid_port(param->port_id)) {
123 : 0 : rte_errno = ENODEV;
124 : 0 : ERROR("port %u invalid port ID", param->port_id);
125 : 0 : return -rte_errno;
126 : : }
127 : 0 : dev = &rte_eth_devices[param->port_id];
128 [ # # # ]: 0 : switch (param->type) {
129 : 0 : case MLX4_MP_REQ_START_RXTX:
130 : 0 : INFO("port %u starting datapath", dev->data->port_id);
131 : 0 : dev->tx_pkt_burst = mlx4_tx_burst;
132 : 0 : dev->rx_pkt_burst = mlx4_rx_burst;
133 : : #ifdef HAVE_IBV_MLX4_UAR_MMAP_OFFSET
134 : 0 : ppriv = (struct mlx4_proc_priv *)dev->process_private;
135 [ # # ]: 0 : if (ppriv->uar_table_sz != dev->data->nb_tx_queues) {
136 : 0 : mlx4_tx_uar_uninit_secondary(dev);
137 : 0 : mlx4_proc_priv_uninit(dev);
138 : 0 : ret = mlx4_proc_priv_init(dev);
139 [ # # ]: 0 : if (ret) {
140 : 0 : close(mp_msg->fds[0]);
141 : 0 : return -rte_errno;
142 : : }
143 : 0 : ret = mlx4_tx_uar_init_secondary(dev, mp_msg->fds[0]);
144 [ # # ]: 0 : if (ret) {
145 : 0 : close(mp_msg->fds[0]);
146 : 0 : mlx4_proc_priv_uninit(dev);
147 : 0 : return -rte_errno;
148 : : }
149 : : }
150 : : #endif
151 : 0 : close(mp_msg->fds[0]);
152 : 0 : rte_eth_fp_ops[param->port_id].rx_pkt_burst = dev->rx_pkt_burst;
153 : 0 : rte_eth_fp_ops[param->port_id].tx_pkt_burst = dev->tx_pkt_burst;
154 : 0 : rte_eth_fp_ops[param->port_id].rxq.data = dev->data->rx_queues;
155 : 0 : rte_eth_fp_ops[param->port_id].txq.data = dev->data->tx_queues;
156 : : rte_mb();
157 : 0 : mp_init_msg(dev, &mp_res, param->type);
158 : 0 : res->result = 0;
159 : 0 : ret = rte_mp_reply(&mp_res, peer);
160 : 0 : break;
161 : 0 : case MLX4_MP_REQ_STOP_RXTX:
162 : 0 : INFO("port %u stopping datapath", dev->data->port_id);
163 : 0 : dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
164 : 0 : dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
165 : 0 : rte_eth_fp_ops[param->port_id].rx_pkt_burst = rte_eth_pkt_burst_dummy;
166 : 0 : rte_eth_fp_ops[param->port_id].tx_pkt_burst = rte_eth_pkt_burst_dummy;
167 : : rte_mb();
168 : 0 : mp_init_msg(dev, &mp_res, param->type);
169 : 0 : res->result = 0;
170 : 0 : ret = rte_mp_reply(&mp_res, peer);
171 : 0 : break;
172 : 0 : default:
173 : 0 : rte_errno = EINVAL;
174 : 0 : ERROR("port %u invalid mp request type", dev->data->port_id);
175 : 0 : return -rte_errno;
176 : : }
177 : : return ret;
178 : : }
179 : :
180 : : /**
181 : : * Broadcast request of stopping/starting data-path to secondary processes.
182 : : *
183 : : * @param[in] dev
184 : : * Pointer to Ethernet structure.
185 : : * @param[in] type
186 : : * Request type.
187 : : */
188 : : static void
189 : 0 : mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx4_mp_req_type type)
190 : : {
191 : : struct rte_mp_msg mp_req;
192 : : struct rte_mp_msg *mp_res;
193 : : struct rte_mp_reply mp_rep;
194 : : struct mlx4_mp_param *res __rte_unused;
195 : 0 : struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
196 : : struct mlx4_priv *priv;
197 : : int ret;
198 : : int i;
199 : :
200 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
201 [ # # ]: 0 : if (!mlx4_shared_data->secondary_cnt)
202 : 0 : return;
203 [ # # ]: 0 : if (type != MLX4_MP_REQ_START_RXTX && type != MLX4_MP_REQ_STOP_RXTX) {
204 : 0 : ERROR("port %u unknown request (req_type %d)",
205 : : dev->data->port_id, type);
206 : 0 : return;
207 : : }
208 : 0 : mp_init_msg(dev, &mp_req, type);
209 [ # # ]: 0 : if (type == MLX4_MP_REQ_START_RXTX) {
210 : 0 : priv = dev->data->dev_private;
211 : 0 : mp_req.num_fds = 1;
212 : 0 : mp_req.fds[0] = priv->ctx->cmd_fd;
213 : : }
214 : 0 : ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
215 [ # # ]: 0 : if (ret) {
216 [ # # ]: 0 : if (rte_errno != ENOTSUP)
217 : 0 : ERROR("port %u failed to request stop/start Rx/Tx (%d)",
218 : : dev->data->port_id, type);
219 : 0 : goto exit;
220 : : }
221 [ # # ]: 0 : if (mp_rep.nb_sent != mp_rep.nb_received) {
222 : 0 : ERROR("port %u not all secondaries responded (req_type %d)",
223 : : dev->data->port_id, type);
224 : 0 : goto exit;
225 : : }
226 [ # # ]: 0 : for (i = 0; i < mp_rep.nb_received; i++) {
227 : 0 : mp_res = &mp_rep.msgs[i];
228 : : res = (struct mlx4_mp_param *)mp_res->param;
229 [ # # ]: 0 : if (res->result) {
230 : 0 : ERROR("port %u request failed on secondary #%d",
231 : : dev->data->port_id, i);
232 : 0 : goto exit;
233 : : }
234 : : }
235 : 0 : exit:
236 : 0 : free(mp_rep.msgs);
237 : : }
238 : :
239 : : /**
240 : : * Broadcast request of starting data-path to secondary processes. The request
241 : : * is synchronous.
242 : : *
243 : : * @param[in] dev
244 : : * Pointer to Ethernet structure.
245 : : */
246 : : void
247 : 0 : mlx4_mp_req_start_rxtx(struct rte_eth_dev *dev)
248 : : {
249 : 0 : mp_req_on_rxtx(dev, MLX4_MP_REQ_START_RXTX);
250 : 0 : }
251 : :
252 : : /**
253 : : * Broadcast request of stopping data-path to secondary processes. The request
254 : : * is synchronous.
255 : : *
256 : : * @param[in] dev
257 : : * Pointer to Ethernet structure.
258 : : */
259 : : void
260 : 0 : mlx4_mp_req_stop_rxtx(struct rte_eth_dev *dev)
261 : : {
262 : 0 : mp_req_on_rxtx(dev, MLX4_MP_REQ_STOP_RXTX);
263 : 0 : }
264 : :
265 : : /**
266 : : * Request Memory Region creation to the primary process.
267 : : *
268 : : * @param[in] dev
269 : : * Pointer to Ethernet structure.
270 : : * @param addr
271 : : * Target virtual address to register.
272 : : *
273 : : * @return
274 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
275 : : */
276 : : int
277 : 0 : mlx4_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
278 : : {
279 : : struct rte_mp_msg mp_req;
280 : : struct rte_mp_msg *mp_res;
281 : : struct rte_mp_reply mp_rep;
282 : : struct mlx4_mp_param *req = (struct mlx4_mp_param *)mp_req.param;
283 : : struct mlx4_mp_param *res;
284 : 0 : struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
285 : : int ret;
286 : :
287 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
288 : 0 : mp_init_msg(dev, &mp_req, MLX4_MP_REQ_CREATE_MR);
289 : 0 : req->args.addr = addr;
290 : 0 : ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
291 [ # # ]: 0 : if (ret) {
292 : 0 : ERROR("port %u request to primary process failed",
293 : : dev->data->port_id);
294 : 0 : return -rte_errno;
295 : : }
296 : : MLX4_ASSERT(mp_rep.nb_received == 1);
297 : 0 : mp_res = &mp_rep.msgs[0];
298 : : res = (struct mlx4_mp_param *)mp_res->param;
299 : 0 : ret = res->result;
300 [ # # ]: 0 : if (ret)
301 : 0 : rte_errno = -ret;
302 : 0 : free(mp_rep.msgs);
303 : 0 : return ret;
304 : : }
305 : :
306 : : /**
307 : : * IPC message handler of primary process.
308 : : *
309 : : * @param[in] dev
310 : : * Pointer to Ethernet structure.
311 : : *
312 : : * @return
313 : : * fd on success, a negative errno value otherwise and rte_errno is set.
314 : : */
315 : : int
316 : 0 : mlx4_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
317 : : {
318 : : struct rte_mp_msg mp_req;
319 : : struct rte_mp_msg *mp_res;
320 : : struct rte_mp_reply mp_rep;
321 : : struct mlx4_mp_param *res;
322 : 0 : struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
323 : : int ret;
324 : :
325 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
326 : 0 : mp_init_msg(dev, &mp_req, MLX4_MP_REQ_VERBS_CMD_FD);
327 : 0 : ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
328 [ # # ]: 0 : if (ret) {
329 : 0 : ERROR("port %u request to primary process failed",
330 : : dev->data->port_id);
331 : 0 : return -rte_errno;
332 : : }
333 : : MLX4_ASSERT(mp_rep.nb_received == 1);
334 : 0 : mp_res = &mp_rep.msgs[0];
335 : : res = (struct mlx4_mp_param *)mp_res->param;
336 [ # # ]: 0 : if (res->result) {
337 : 0 : rte_errno = -res->result;
338 : 0 : ERROR("port %u failed to get command FD from primary process",
339 : : dev->data->port_id);
340 : 0 : ret = -rte_errno;
341 : 0 : goto exit;
342 : : }
343 : : MLX4_ASSERT(mp_res->num_fds == 1);
344 : 0 : ret = mp_res->fds[0];
345 : 0 : DEBUG("port %u command FD from primary is %d",
346 : : dev->data->port_id, ret);
347 : 0 : exit:
348 : 0 : free(mp_rep.msgs);
349 : 0 : return ret;
350 : : }
351 : :
352 : : /**
353 : : * Initialize by primary process.
354 : : */
355 : : int
356 : 0 : mlx4_mp_init_primary(void)
357 : : {
358 : : int ret;
359 : :
360 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
361 : :
362 : : /* primary is allowed to not support IPC */
363 : 0 : ret = rte_mp_action_register(MLX4_MP_NAME, mp_primary_handle);
364 [ # # # # ]: 0 : if (ret && rte_errno != ENOTSUP)
365 : 0 : return -1;
366 : : return 0;
367 : : }
368 : :
369 : : /**
370 : : * Un-initialize by primary process.
371 : : */
372 : : void
373 : 0 : mlx4_mp_uninit_primary(void)
374 : : {
375 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
376 : 0 : rte_mp_action_unregister(MLX4_MP_NAME);
377 : 0 : }
378 : :
379 : : /**
380 : : * Initialize by secondary process.
381 : : */
382 : : int
383 : 0 : mlx4_mp_init_secondary(void)
384 : : {
385 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
386 : 0 : return rte_mp_action_register(MLX4_MP_NAME, mp_secondary_handle);
387 : : }
388 : :
389 : : /**
390 : : * Un-initialize by secondary process.
391 : : */
392 : : void
393 : 0 : mlx4_mp_uninit_secondary(void)
394 : : {
395 : : MLX4_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
396 : 0 : rte_mp_action_unregister(MLX4_MP_NAME);
397 : 0 : }
|