43684dbc3aceb969a2b8b656ab80bbe1783d1c73
[dpdk.git] / drivers / net / mlx5 / mlx5_mp.c
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 <time.h>
8
9 #include <rte_eal.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_string_fns.h>
12
13 #include <mlx5_common_mp.h>
14
15 #include "mlx5.h"
16 #include "mlx5_rxtx.h"
17 #include "mlx5_utils.h"
18
19 int
20 mlx5_mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
21 {
22         struct rte_mp_msg mp_res;
23         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
24         const struct mlx5_mp_param *param =
25                 (const struct mlx5_mp_param *)mp_msg->param;
26         struct rte_eth_dev *dev;
27         struct mlx5_priv *priv;
28         struct mlx5_mr_cache entry;
29         uint32_t lkey;
30         int ret;
31
32         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
33         if (!rte_eth_dev_is_valid_port(param->port_id)) {
34                 rte_errno = ENODEV;
35                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
36                 return -rte_errno;
37         }
38         dev = &rte_eth_devices[param->port_id];
39         priv = dev->data->dev_private;
40         switch (param->type) {
41         case MLX5_MP_REQ_CREATE_MR:
42                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
43                 lkey = mlx5_mr_create_primary(dev, &entry, param->args.addr);
44                 if (lkey == UINT32_MAX)
45                         res->result = -rte_errno;
46                 ret = rte_mp_reply(&mp_res, peer);
47                 break;
48         case MLX5_MP_REQ_VERBS_CMD_FD:
49                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
50                 mp_res.num_fds = 1;
51                 mp_res.fds[0] = priv->sh->ctx->cmd_fd;
52                 res->result = 0;
53                 ret = rte_mp_reply(&mp_res, peer);
54                 break;
55         case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
56                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
57                 res->result = mlx5_queue_state_modify_primary
58                                         (dev, &param->args.state_modify);
59                 ret = rte_mp_reply(&mp_res, peer);
60                 break;
61         default:
62                 rte_errno = EINVAL;
63                 DRV_LOG(ERR, "port %u invalid mp request type",
64                         dev->data->port_id);
65                 return -rte_errno;
66         }
67         return ret;
68 }
69
70 /**
71  * IPC message handler of a secondary process.
72  *
73  * @param[in] dev
74  *   Pointer to Ethernet structure.
75  * @param[in] peer
76  *   Pointer to the peer socket path.
77  *
78  * @return
79  *   0 on success, a negative errno value otherwise and rte_errno is set.
80  */
81 int
82 mlx5_mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
83 {
84         struct rte_mp_msg mp_res;
85         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
86         const struct mlx5_mp_param *param =
87                 (const struct mlx5_mp_param *)mp_msg->param;
88         struct rte_eth_dev *dev;
89         struct mlx5_priv *priv;
90         int ret;
91
92         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
93         if (!rte_eth_dev_is_valid_port(param->port_id)) {
94                 rte_errno = ENODEV;
95                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
96                 return -rte_errno;
97         }
98         dev = &rte_eth_devices[param->port_id];
99         priv = dev->data->dev_private;
100         switch (param->type) {
101         case MLX5_MP_REQ_START_RXTX:
102                 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
103                 rte_mb();
104                 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
105                 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
106                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
107                 res->result = 0;
108                 ret = rte_mp_reply(&mp_res, peer);
109                 break;
110         case MLX5_MP_REQ_STOP_RXTX:
111                 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
112                 dev->rx_pkt_burst = removed_rx_burst;
113                 dev->tx_pkt_burst = removed_tx_burst;
114                 rte_mb();
115                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
116                 res->result = 0;
117                 ret = rte_mp_reply(&mp_res, peer);
118                 break;
119         default:
120                 rte_errno = EINVAL;
121                 DRV_LOG(ERR, "port %u invalid mp request type",
122                         dev->data->port_id);
123                 return -rte_errno;
124         }
125         return ret;
126 }
127
128 /**
129  * Broadcast request of stopping/starting data-path to secondary processes.
130  *
131  * @param[in] dev
132  *   Pointer to Ethernet structure.
133  * @param[in] type
134  *   Request type.
135  */
136 static void
137 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
138 {
139         struct rte_mp_msg mp_req;
140         struct rte_mp_msg *mp_res;
141         struct rte_mp_reply mp_rep;
142         struct mlx5_mp_param *res;
143         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
144         struct mlx5_priv *priv = dev->data->dev_private;
145         int ret;
146         int i;
147
148         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
149         if (!mlx5_shared_data->secondary_cnt)
150                 return;
151         if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
152                 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
153                         dev->data->port_id, type);
154                 return;
155         }
156         mp_init_msg(&priv->mp_id, &mp_req, type);
157         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
158         if (ret) {
159                 if (rte_errno != ENOTSUP)
160                         DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
161                                 dev->data->port_id, type);
162                 goto exit;
163         }
164         if (mp_rep.nb_sent != mp_rep.nb_received) {
165                 DRV_LOG(ERR,
166                         "port %u not all secondaries responded (req_type %d)",
167                         dev->data->port_id, type);
168                 goto exit;
169         }
170         for (i = 0; i < mp_rep.nb_received; i++) {
171                 mp_res = &mp_rep.msgs[i];
172                 res = (struct mlx5_mp_param *)mp_res->param;
173                 if (res->result) {
174                         DRV_LOG(ERR, "port %u request failed on secondary #%d",
175                                 dev->data->port_id, i);
176                         goto exit;
177                 }
178         }
179 exit:
180         free(mp_rep.msgs);
181 }
182
183 /**
184  * Broadcast request of starting data-path to secondary processes. The request
185  * is synchronous.
186  *
187  * @param[in] dev
188  *   Pointer to Ethernet structure.
189  */
190 void
191 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
192 {
193         mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
194 }
195
196 /**
197  * Broadcast request of stopping data-path to secondary processes. The request
198  * is synchronous.
199  *
200  * @param[in] dev
201  *   Pointer to Ethernet structure.
202  */
203 void
204 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
205 {
206         mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
207 }