1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 HiSilicon Limited.
6 #include <ethdev_driver.h>
7 #include <rte_string_fns.h>
10 #include "hns3_ethdev.h"
11 #include "hns3_logs.h"
12 #include "hns3_rxtx.h"
15 static bool hns3_inited;
18 * Initialize IPC message.
21 * Pointer to Ethernet structure.
23 * Pointer to message to fill in.
28 mp_init_msg(struct rte_eth_dev *dev, struct rte_mp_msg *msg,
29 enum hns3_mp_req_type type)
31 struct hns3_mp_param *param = (struct hns3_mp_param *)msg->param;
33 memset(msg, 0, sizeof(*msg));
34 strlcpy(msg->name, HNS3_MP_NAME, sizeof(msg->name));
35 msg->len_param = sizeof(*param);
37 param->port_id = dev->data->port_id;
41 * IPC message handler of primary process.
44 * Pointer to Ethernet structure.
46 * Pointer to the peer socket path.
49 * 0 on success, a negative errno value otherwise and rte_errno is set.
52 mp_primary_handle(const struct rte_mp_msg *mp_msg __rte_unused,
53 const void *peer __rte_unused)
59 * IPC message handler of a secondary process.
62 * Pointer to Ethernet structure.
64 * Pointer to the peer socket path.
67 * 0 on success, a negative errno value otherwise and rte_errno is set.
70 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
72 struct rte_mp_msg mp_res;
73 struct hns3_mp_param *res = (struct hns3_mp_param *)mp_res.param;
74 const struct hns3_mp_param *param =
75 (const struct hns3_mp_param *)mp_msg->param;
76 eth_tx_prep_t prep = NULL;
77 struct rte_eth_dev *dev;
80 if (!rte_eth_dev_is_valid_port(param->port_id)) {
82 PMD_INIT_LOG(ERR, "port %d invalid port ID", param->port_id);
85 dev = &rte_eth_devices[param->port_id];
86 switch (param->type) {
87 case HNS3_MP_REQ_START_RXTX:
88 PMD_INIT_LOG(INFO, "port %u starting datapath",
90 hns3_set_rxtx_function(dev);
92 case HNS3_MP_REQ_STOP_RXTX:
93 PMD_INIT_LOG(INFO, "port %u stopping datapath",
95 hns3_set_rxtx_function(dev);
97 case HNS3_MP_REQ_START_TX:
98 PMD_INIT_LOG(INFO, "port %u starting Tx datapath",
100 dev->tx_pkt_burst = hns3_get_tx_function(dev, &prep);
101 dev->tx_pkt_prepare = prep;
103 case HNS3_MP_REQ_STOP_TX:
104 PMD_INIT_LOG(INFO, "port %u stopping Tx datapath",
106 dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
107 dev->tx_pkt_prepare = NULL;
111 PMD_INIT_LOG(ERR, "port %u invalid mp request type",
117 mp_init_msg(dev, &mp_res, param->type);
119 ret = rte_mp_reply(&mp_res, peer);
125 mp_req_type_is_valid(enum hns3_mp_req_type type)
127 return type == HNS3_MP_REQ_START_RXTX ||
128 type == HNS3_MP_REQ_STOP_RXTX ||
129 type == HNS3_MP_REQ_START_TX ||
130 type == HNS3_MP_REQ_STOP_TX;
134 * Broadcast request of stopping/starting data-path to secondary processes.
137 * Pointer to Ethernet structure.
142 mp_req_on_rxtx(struct rte_eth_dev *dev, enum hns3_mp_req_type type)
144 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
145 struct rte_mp_msg mp_req;
146 struct rte_mp_msg *mp_res;
147 struct rte_mp_reply mp_rep;
148 struct hns3_mp_param *res;
153 if (rte_eal_process_type() == RTE_PROC_SECONDARY || !hw->secondary_cnt)
155 if (!mp_req_type_is_valid(type)) {
156 hns3_err(hw, "port %u unknown request (req_type %d)",
157 dev->data->port_id, type);
160 mp_init_msg(dev, &mp_req, type);
161 ts.tv_sec = HNS3_MP_REQ_TIMEOUT_SEC;
163 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
165 hns3_err(hw, "port %u failed to request stop/start Rx/Tx (%d)",
166 dev->data->port_id, type);
169 if (mp_rep.nb_sent != mp_rep.nb_received) {
171 "port %u not all secondaries responded (req_type %d)",
172 dev->data->port_id, type);
175 for (i = 0; i < mp_rep.nb_received; i++) {
176 mp_res = &mp_rep.msgs[i];
177 res = (struct hns3_mp_param *)mp_res->param;
179 hns3_err(hw, "port %u request failed on secondary #%d",
180 dev->data->port_id, i);
189 * Broadcast request of starting data-path to secondary processes. The request
193 * Pointer to Ethernet structure.
195 void hns3_mp_req_start_rxtx(struct rte_eth_dev *dev)
197 mp_req_on_rxtx(dev, HNS3_MP_REQ_START_RXTX);
201 * Broadcast request of stopping data-path to secondary processes. The request
205 * Pointer to Ethernet structure.
207 void hns3_mp_req_stop_rxtx(struct rte_eth_dev *dev)
209 mp_req_on_rxtx(dev, HNS3_MP_REQ_STOP_RXTX);
213 hns3_mp_req_stop_tx(struct rte_eth_dev *dev)
215 mp_req_on_rxtx(dev, HNS3_MP_REQ_STOP_TX);
219 hns3_mp_req_start_tx(struct rte_eth_dev *dev)
221 mp_req_on_rxtx(dev, HNS3_MP_REQ_START_TX);
225 * Initialize by primary process.
227 int hns3_mp_init_primary(void)
232 /* primary is allowed to not support IPC */
233 ret = rte_mp_action_register(HNS3_MP_NAME, mp_primary_handle);
234 if (ret && rte_errno != ENOTSUP)
244 * Un-initialize by primary process.
246 void hns3_mp_uninit_primary(void)
249 rte_mp_action_unregister(HNS3_MP_NAME);
253 * Initialize by secondary process.
255 int hns3_mp_init_secondary(void)
260 ret = rte_mp_action_register(HNS3_MP_NAME, mp_secondary_handle);