Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2021 HiSilicon Limited.
3 : : */
4 : :
5 : : #include <rte_alarm.h>
6 : : #include <bus_pci_driver.h>
7 : : #include <ethdev_pci.h>
8 : :
9 : : #include "hns3_common.h"
10 : : #include "hns3_dump.h"
11 : : #include "hns3_logs.h"
12 : : #include "hns3_rxtx.h"
13 : : #include "hns3_intr.h"
14 : : #include "hns3_regs.h"
15 : : #include "hns3_dcb.h"
16 : : #include "hns3_mp.h"
17 : : #include "hns3_flow.h"
18 : : #include "hns3_ptp.h"
19 : : #include "hns3_ethdev.h"
20 : :
21 : : #define HNS3_SERVICE_INTERVAL 1000000 /* us */
22 : : #define HNS3_SERVICE_QUICK_INTERVAL 10
23 : : #define HNS3_INVALID_PVID 0xFFFF
24 : :
25 : : #define HNS3_FILTER_TYPE_VF 0
26 : : #define HNS3_FILTER_TYPE_PORT 1
27 : : #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
28 : : #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
29 : : #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
30 : : #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
31 : : #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
32 : : #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
33 : : | HNS3_FILTER_FE_ROCE_EGRESS_B)
34 : : #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
35 : : | HNS3_FILTER_FE_ROCE_INGRESS_B)
36 : :
37 : : /* Reset related Registers */
38 : : #define HNS3_GLOBAL_RESET_BIT 0
39 : : #define HNS3_CORE_RESET_BIT 1
40 : : #define HNS3_IMP_RESET_BIT 2
41 : : #define HNS3_FUN_RST_ING_B 0
42 : :
43 : : #define HNS3_VECTOR0_IMP_RESET_INT_B 1
44 : : #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
45 : : #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
46 : : #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
47 : : #define HNS3_VECTOR0_TRIGGER_IMP_RESET_B 7U
48 : :
49 : : #define HNS3_RESET_WAIT_MS 100
50 : : #define HNS3_RESET_WAIT_CNT 200
51 : :
52 : : enum hns3_evt_cause {
53 : : HNS3_VECTOR0_EVENT_RST,
54 : : HNS3_VECTOR0_EVENT_MBX,
55 : : HNS3_VECTOR0_EVENT_ERR,
56 : : HNS3_VECTOR0_EVENT_PTP,
57 : : HNS3_VECTOR0_EVENT_OTHER,
58 : : };
59 : :
60 : : struct hns3_intr_state {
61 : : uint32_t vector0_state;
62 : : uint32_t cmdq_state;
63 : : uint32_t hw_err_state;
64 : : };
65 : :
66 : : #define HNS3_SPEEDS_SUPP_FEC (RTE_ETH_LINK_SPEED_10G | \
67 : : RTE_ETH_LINK_SPEED_25G | \
68 : : RTE_ETH_LINK_SPEED_40G | \
69 : : RTE_ETH_LINK_SPEED_50G | \
70 : : RTE_ETH_LINK_SPEED_100G | \
71 : : RTE_ETH_LINK_SPEED_200G)
72 : :
73 : : static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = {
74 : : { RTE_ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
75 : : RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
76 : : RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
77 : :
78 : : { RTE_ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
79 : : RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
80 : : RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
81 : : RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
82 : :
83 : : { RTE_ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
84 : : RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
85 : : RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
86 : :
87 : : { RTE_ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
88 : : RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
89 : : RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
90 : : RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
91 : :
92 : : { RTE_ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
93 : : RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
94 : : RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
95 : :
96 : : { RTE_ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
97 : : RTE_ETH_FEC_MODE_CAPA_MASK(RS) |
98 : : RTE_ETH_FEC_MODE_CAPA_MASK(LLRS) }
99 : : };
100 : :
101 : : static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
102 : : RTE_ATOMIC(uint64_t) *levels);
103 : : static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
104 : : static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
105 : : int on);
106 : : static int hns3_update_link_info(struct rte_eth_dev *eth_dev);
107 : : static bool hns3_update_link_status(struct hns3_hw *hw);
108 : :
109 : : static int hns3_add_mc_mac_addr(struct hns3_hw *hw,
110 : : struct rte_ether_addr *mac_addr);
111 : : static int hns3_remove_mc_mac_addr(struct hns3_hw *hw,
112 : : struct rte_ether_addr *mac_addr);
113 : : static int hns3_restore_fec(struct hns3_hw *hw);
114 : : static int hns3_query_dev_fec_info(struct hns3_hw *hw);
115 : : static int hns3_do_stop(struct hns3_adapter *hns);
116 : : static int hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds);
117 : : static int hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable);
118 : :
119 : :
120 : : static void
121 : : hns3_pf_disable_irq0(struct hns3_hw *hw)
122 : : {
123 : 0 : hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
124 : : }
125 : :
126 : : static void
127 : : hns3_pf_enable_irq0(struct hns3_hw *hw)
128 : : {
129 : 0 : hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
130 : 0 : }
131 : :
132 : : static enum hns3_evt_cause
133 : 0 : hns3_proc_imp_reset_event(struct hns3_adapter *hns, uint32_t *vec_val)
134 : : {
135 : : struct hns3_hw *hw = &hns->hw;
136 : :
137 : 0 : rte_atomic_store_explicit(&hw->reset.disable_cmd, 1, rte_memory_order_relaxed);
138 : 0 : hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
139 : 0 : *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
140 : 0 : hw->reset.stats.imp_cnt++;
141 : 0 : hns3_warn(hw, "IMP reset detected, clear reset status");
142 : :
143 : 0 : return HNS3_VECTOR0_EVENT_RST;
144 : : }
145 : :
146 : : static enum hns3_evt_cause
147 : 0 : hns3_proc_global_reset_event(struct hns3_adapter *hns, uint32_t *vec_val)
148 : : {
149 : : struct hns3_hw *hw = &hns->hw;
150 : :
151 : 0 : rte_atomic_store_explicit(&hw->reset.disable_cmd, 1, rte_memory_order_relaxed);
152 : 0 : hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
153 : 0 : *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
154 : 0 : hw->reset.stats.global_cnt++;
155 : 0 : hns3_warn(hw, "Global reset detected, clear reset status");
156 : :
157 : 0 : return HNS3_VECTOR0_EVENT_RST;
158 : : }
159 : :
160 : : static void
161 : : hns3_query_intr_state(struct hns3_hw *hw, struct hns3_intr_state *state)
162 : : {
163 : 0 : state->vector0_state = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
164 : 0 : state->cmdq_state = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
165 : 0 : state->hw_err_state = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
166 : : }
167 : :
168 : : static enum hns3_evt_cause
169 : 0 : hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
170 : : {
171 : : struct hns3_hw *hw = &hns->hw;
172 : : struct hns3_intr_state state;
173 : : uint32_t val;
174 : : enum hns3_evt_cause ret;
175 : :
176 : : hns3_query_intr_state(hw, &state);
177 : :
178 : : /*
179 : : * Assumption: If by any chance reset and mailbox events are reported
180 : : * together then we will only process reset event and defer the
181 : : * processing of the mailbox events. Since, we would have not cleared
182 : : * RX CMDQ event this time we would receive again another interrupt
183 : : * from H/W just for the mailbox.
184 : : */
185 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & state.vector0_state) { /* IMP */
186 : 0 : ret = hns3_proc_imp_reset_event(hns, &val);
187 : 0 : goto out;
188 : : }
189 : :
190 : : /* Global reset */
191 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & state.vector0_state) {
192 : 0 : ret = hns3_proc_global_reset_event(hns, &val);
193 : 0 : goto out;
194 : : }
195 : :
196 : : /* Check for vector0 1588 event source */
197 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_1588_INT_B) & state.vector0_state) {
198 : 0 : val = BIT(HNS3_VECTOR0_1588_INT_B);
199 : : ret = HNS3_VECTOR0_EVENT_PTP;
200 : 0 : goto out;
201 : : }
202 : :
203 : : /* check for vector0 msix event source */
204 [ # # ]: 0 : if (state.vector0_state & HNS3_VECTOR0_REG_MSIX_MASK ||
205 [ # # ]: 0 : state.hw_err_state & HNS3_RAS_REG_NFE_MASK) {
206 : 0 : val = state.vector0_state | state.hw_err_state;
207 : : ret = HNS3_VECTOR0_EVENT_ERR;
208 : 0 : goto out;
209 : : }
210 : :
211 : : /* check for vector0 mailbox(=CMDQ RX) event source */
212 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & state.cmdq_state) {
213 : 0 : state.cmdq_state &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
214 : 0 : val = state.cmdq_state;
215 : : ret = HNS3_VECTOR0_EVENT_MBX;
216 : 0 : goto out;
217 : : }
218 : :
219 : 0 : val = state.vector0_state;
220 : : ret = HNS3_VECTOR0_EVENT_OTHER;
221 : :
222 : 0 : out:
223 : 0 : *clearval = val;
224 : 0 : return ret;
225 : : }
226 : :
227 : : void
228 : 0 : hns3_clear_reset_event(struct hns3_hw *hw)
229 : : {
230 : : uint32_t clearval = 0;
231 : :
232 [ # # # ]: 0 : switch (hw->reset.level) {
233 : : case HNS3_IMP_RESET:
234 : : clearval = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
235 : : break;
236 : : case HNS3_GLOBAL_RESET:
237 : : clearval = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
238 : : break;
239 : : default:
240 : : break;
241 : : }
242 : :
243 : : if (clearval == 0)
244 : : return;
245 : :
246 : 0 : hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, clearval);
247 : :
248 : : hns3_pf_enable_irq0(hw);
249 : : }
250 : :
251 : : static void
252 : : hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
253 : : {
254 : 0 : if (event_type == HNS3_VECTOR0_EVENT_RST ||
255 [ # # ]: 0 : event_type == HNS3_VECTOR0_EVENT_PTP)
256 : 0 : hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
257 [ # # ]: 0 : else if (event_type == HNS3_VECTOR0_EVENT_MBX)
258 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
259 : : }
260 : :
261 : : static void
262 : 0 : hns3_clear_all_event_cause(struct hns3_hw *hw)
263 : : {
264 : : uint32_t vector0_int_stats;
265 : :
266 : 0 : vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
267 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
268 : 0 : hns3_warn(hw, "Probe during IMP reset interrupt");
269 : :
270 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
271 : 0 : hns3_warn(hw, "Probe during Global reset interrupt");
272 : :
273 : : hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
274 : : BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
275 : : BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
276 : : BIT(HNS3_VECTOR0_CORERESET_INT_B));
277 : : hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
278 : : hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_PTP,
279 : : BIT(HNS3_VECTOR0_1588_INT_B));
280 : 0 : }
281 : :
282 : : static void
283 : 0 : hns3_handle_mac_tnl(struct hns3_hw *hw)
284 : : {
285 : : struct hns3_cmd_desc desc;
286 : : uint32_t status;
287 : : int ret;
288 : :
289 : : /* query and clear mac tnl interrupt */
290 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_TNL_INT, true);
291 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
292 [ # # ]: 0 : if (ret) {
293 : 0 : hns3_err(hw, "failed to query mac tnl int, ret = %d.", ret);
294 : 0 : return;
295 : : }
296 : :
297 : 0 : status = rte_le_to_cpu_32(desc.data[0]);
298 [ # # ]: 0 : if (status) {
299 : 0 : hns3_warn(hw, "mac tnl int occurs, status = 0x%x.", status);
300 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_MAC_TNL_INT,
301 : : false);
302 : 0 : desc.data[0] = rte_cpu_to_le_32(HNS3_MAC_TNL_INT_CLR);
303 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
304 [ # # ]: 0 : if (ret)
305 : 0 : hns3_err(hw, "failed to clear mac tnl int, ret = %d.",
306 : : ret);
307 : : }
308 : : }
309 : :
310 : : static void
311 : 0 : hns3_delay_before_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
312 : : {
313 : : #define IMPRESET_WAIT_MS_TIME 5
314 : :
315 [ # # # # ]: 0 : if (event_type == HNS3_VECTOR0_EVENT_RST &&
316 : 0 : regclr & BIT(HNS3_VECTOR0_IMPRESET_INT_B) &&
317 [ # # ]: 0 : hw->revision >= PCI_REVISION_ID_HIP09_A) {
318 : : rte_delay_ms(IMPRESET_WAIT_MS_TIME);
319 : 0 : hns3_dbg(hw, "wait firmware watchdog initialization completed.");
320 : : }
321 : 0 : }
322 : :
323 : : static bool
324 : 0 : hns3_reset_event_valid(struct hns3_hw *hw)
325 : : {
326 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
327 : : enum hns3_reset_level new_req = HNS3_NONE_RESET;
328 : : enum hns3_reset_level last_req;
329 : : uint32_t vector0_int;
330 : :
331 : 0 : vector0_int = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
332 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int)
333 : : new_req = HNS3_IMP_RESET;
334 [ # # ]: 0 : else if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int)
335 : : new_req = HNS3_GLOBAL_RESET;
336 : : if (new_req == HNS3_NONE_RESET)
337 : : return true;
338 : :
339 : 0 : last_req = hns3_get_reset_level(hns, &hw->reset.pending);
340 [ # # ]: 0 : if (last_req == HNS3_NONE_RESET)
341 : : return true;
342 : :
343 [ # # ]: 0 : if (new_req > last_req)
344 : : return true;
345 : :
346 : 0 : hns3_warn(hw, "last_req (%u) less than or equal to new_req (%u) ignore",
347 : : last_req, new_req);
348 : 0 : return false;
349 : : }
350 : :
351 : : static void
352 : 0 : hns3_interrupt_handler(void *param)
353 : : {
354 : : struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
355 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
356 : 0 : struct hns3_hw *hw = &hns->hw;
357 : : enum hns3_evt_cause event_cause;
358 : : struct hns3_intr_state state;
359 : 0 : uint32_t clearval = 0;
360 : :
361 [ # # ]: 0 : if (!hns3_reset_event_valid(hw))
362 : 0 : return;
363 : :
364 : : /* Disable interrupt */
365 : : hns3_pf_disable_irq0(hw);
366 : :
367 : 0 : event_cause = hns3_check_event_cause(hns, &clearval);
368 : : hns3_query_intr_state(hw, &state);
369 : 0 : hns3_delay_before_clear_event_cause(hw, event_cause, clearval);
370 : : hns3_clear_event_cause(hw, event_cause, clearval);
371 : : /* vector 0 interrupt is shared with reset and mailbox source events. */
372 [ # # ]: 0 : if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
373 : 0 : hns3_warn(hw, "received interrupt: vector0_int_stat:0x%x "
374 : : "ras_int_stat:0x%x cmdq_int_stat:0x%x",
375 : : state.vector0_state, state.hw_err_state,
376 : : state.cmdq_state);
377 : 0 : hns3_handle_mac_tnl(hw);
378 : 0 : hns3_handle_error(hns);
379 [ # # ]: 0 : } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
380 : 0 : hns3_warn(hw, "received reset interrupt");
381 : 0 : hns3_schedule_reset(hns);
382 [ # # ]: 0 : } else if (event_cause == HNS3_VECTOR0_EVENT_MBX) {
383 : 0 : hns3pf_handle_mbx_msg(hw);
384 [ # # ]: 0 : } else if (event_cause != HNS3_VECTOR0_EVENT_PTP) {
385 : 0 : hns3_warn(hw, "received unknown event: vector0_int_stat:0x%x "
386 : : "ras_int_stat:0x%x cmdq_int_stat:0x%x",
387 : : state.vector0_state, state.hw_err_state,
388 : : state.cmdq_state);
389 : : }
390 : :
391 : : /* Enable interrupt if it is not cause by reset */
392 : 0 : if (event_cause == HNS3_VECTOR0_EVENT_ERR ||
393 : : event_cause == HNS3_VECTOR0_EVENT_MBX ||
394 [ # # ]: 0 : event_cause == HNS3_VECTOR0_EVENT_PTP ||
395 : : event_cause == HNS3_VECTOR0_EVENT_OTHER)
396 : : hns3_pf_enable_irq0(hw);
397 : : }
398 : :
399 : : static int
400 : 0 : hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
401 : : {
402 : : #define HNS3_VLAN_ID_OFFSET_STEP 160
403 : : #define HNS3_VLAN_BYTE_SIZE 8
404 : : struct hns3_vlan_filter_pf_cfg_cmd *req;
405 : 0 : struct hns3_hw *hw = &hns->hw;
406 : : uint8_t vlan_offset_byte_val;
407 : : struct hns3_cmd_desc desc;
408 : : uint8_t vlan_offset_byte;
409 : : uint8_t vlan_offset_base;
410 : : int ret;
411 : :
412 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
413 : :
414 : 0 : vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
415 : 0 : vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
416 : : HNS3_VLAN_BYTE_SIZE;
417 : 0 : vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
418 : :
419 : : req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
420 : 0 : req->vlan_offset = vlan_offset_base;
421 : 0 : req->vlan_cfg = on ? 0 : 1;
422 : 0 : req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
423 : :
424 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
425 [ # # ]: 0 : if (ret)
426 : 0 : hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
427 : : vlan_id, ret);
428 : :
429 : 0 : return ret;
430 : : }
431 : :
432 : : static void
433 : 0 : hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
434 : : {
435 : : struct hns3_user_vlan_table *vlan_entry;
436 : : struct hns3_pf *pf = &hns->pf;
437 : :
438 [ # # ]: 0 : LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
439 [ # # ]: 0 : if (vlan_entry->vlan_id == vlan_id) {
440 [ # # ]: 0 : if (vlan_entry->hd_tbl_status)
441 : 0 : hns3_set_port_vlan_filter(hns, vlan_id, 0);
442 [ # # ]: 0 : LIST_REMOVE(vlan_entry, next);
443 : 0 : rte_free(vlan_entry);
444 : 0 : break;
445 : : }
446 : : }
447 : 0 : }
448 : :
449 : : static void
450 : 0 : hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
451 : : bool writen_to_tbl)
452 : : {
453 : : struct hns3_user_vlan_table *vlan_entry;
454 : : struct hns3_hw *hw = &hns->hw;
455 : : struct hns3_pf *pf = &hns->pf;
456 : :
457 [ # # ]: 0 : LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
458 [ # # ]: 0 : if (vlan_entry->vlan_id == vlan_id)
459 : : return;
460 : : }
461 : :
462 : 0 : vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
463 [ # # ]: 0 : if (vlan_entry == NULL) {
464 : 0 : hns3_err(hw, "Failed to malloc hns3 vlan table");
465 : 0 : return;
466 : : }
467 : :
468 : 0 : vlan_entry->hd_tbl_status = writen_to_tbl;
469 : 0 : vlan_entry->vlan_id = vlan_id;
470 : :
471 [ # # ]: 0 : LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
472 : : }
473 : :
474 : : static int
475 : 0 : hns3_restore_vlan_table(struct hns3_adapter *hns)
476 : : {
477 : : struct hns3_user_vlan_table *vlan_entry;
478 : : struct hns3_hw *hw = &hns->hw;
479 : : struct hns3_pf *pf = &hns->pf;
480 : : uint16_t vlan_id;
481 : : int ret = 0;
482 : :
483 [ # # ]: 0 : if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
484 : 0 : return hns3_vlan_pvid_configure(hns,
485 : 0 : hw->port_base_vlan_cfg.pvid, 1);
486 : :
487 [ # # ]: 0 : LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
488 [ # # ]: 0 : if (vlan_entry->hd_tbl_status) {
489 : 0 : vlan_id = vlan_entry->vlan_id;
490 : 0 : ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
491 [ # # ]: 0 : if (ret)
492 : : break;
493 : : }
494 : : }
495 : :
496 : : return ret;
497 : : }
498 : :
499 : : static int
500 : 0 : hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
501 : : {
502 : : struct hns3_hw *hw = &hns->hw;
503 : : bool writen_to_tbl = false;
504 : : int ret = 0;
505 : :
506 : : /*
507 : : * When vlan filter is enabled, hardware regards packets without vlan
508 : : * as packets with vlan 0. So, to receive packets without vlan, vlan id
509 : : * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
510 : : */
511 [ # # ]: 0 : if (on == 0 && vlan_id == 0)
512 : : return 0;
513 : :
514 : : /*
515 : : * When port base vlan enabled, we use port base vlan as the vlan
516 : : * filter condition. In this case, we don't update vlan filter table
517 : : * when user add new vlan or remove exist vlan, just update the
518 : : * vlan list. The vlan id in vlan list will be written in vlan filter
519 : : * table until port base vlan disabled
520 : : */
521 [ # # ]: 0 : if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
522 : 0 : ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
523 : : writen_to_tbl = true;
524 : : }
525 : :
526 [ # # ]: 0 : if (ret == 0) {
527 [ # # ]: 0 : if (on)
528 : 0 : hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
529 : : else
530 : 0 : hns3_rm_dev_vlan_table(hns, vlan_id);
531 : : }
532 : : return ret;
533 : : }
534 : :
535 : : static int
536 : 0 : hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
537 : : {
538 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
539 : : struct hns3_hw *hw = &hns->hw;
540 : : int ret;
541 : :
542 : 0 : rte_spinlock_lock(&hw->lock);
543 : 0 : ret = hns3_vlan_filter_configure(hns, vlan_id, on);
544 : : rte_spinlock_unlock(&hw->lock);
545 : 0 : return ret;
546 : : }
547 : :
548 : : static int
549 : 0 : hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
550 : : uint16_t tpid)
551 : : {
552 : : struct hns3_rx_vlan_type_cfg_cmd *rx_req;
553 : : struct hns3_tx_vlan_type_cfg_cmd *tx_req;
554 : 0 : struct hns3_hw *hw = &hns->hw;
555 : : struct hns3_cmd_desc desc;
556 : : int ret;
557 : :
558 [ # # ]: 0 : if ((vlan_type != RTE_ETH_VLAN_TYPE_INNER &&
559 : : vlan_type != RTE_ETH_VLAN_TYPE_OUTER)) {
560 : 0 : hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
561 : 0 : return -EINVAL;
562 : : }
563 : :
564 [ # # ]: 0 : if (tpid != RTE_ETHER_TYPE_VLAN) {
565 : 0 : hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
566 : 0 : return -EINVAL;
567 : : }
568 : :
569 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
570 : : rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
571 : :
572 [ # # ]: 0 : if (vlan_type == RTE_ETH_VLAN_TYPE_OUTER) {
573 : 0 : rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
574 : 0 : rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
575 : : } else if (vlan_type == RTE_ETH_VLAN_TYPE_INNER) {
576 : 0 : rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
577 : 0 : rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
578 : 0 : rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
579 : 0 : rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
580 : : }
581 : :
582 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
583 [ # # ]: 0 : if (ret) {
584 : 0 : hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
585 : : ret);
586 : 0 : return ret;
587 : : }
588 : :
589 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
590 : :
591 : : tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
592 : 0 : tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
593 : 0 : tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
594 : :
595 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
596 [ # # ]: 0 : if (ret)
597 : 0 : hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
598 : : ret);
599 : : return ret;
600 : : }
601 : :
602 : : static int
603 : 0 : hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
604 : : uint16_t tpid)
605 : : {
606 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
607 : : struct hns3_hw *hw = &hns->hw;
608 : : int ret;
609 : :
610 : 0 : rte_spinlock_lock(&hw->lock);
611 : 0 : ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
612 : : rte_spinlock_unlock(&hw->lock);
613 : 0 : return ret;
614 : : }
615 : :
616 : : static int
617 : 0 : hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
618 : : struct hns3_rx_vtag_cfg *vcfg)
619 : : {
620 : : struct hns3_vport_vtag_rx_cfg_cmd *req;
621 : 0 : struct hns3_hw *hw = &hns->hw;
622 : : struct hns3_cmd_desc desc;
623 : : uint16_t vport_id;
624 : : uint8_t bitmap;
625 : : int ret;
626 : :
627 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
628 : :
629 : : req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
630 : 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
631 : : vcfg->strip_tag1_en ? 1 : 0);
632 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
633 : : vcfg->strip_tag2_en ? 1 : 0);
634 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
635 : : vcfg->vlan1_vlan_prionly ? 1 : 0);
636 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
637 : : vcfg->vlan2_vlan_prionly ? 1 : 0);
638 : :
639 : : /* firmware will ignore this configuration for PCI_REVISION_ID_HIP08 */
640 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
641 : : vcfg->strip_tag1_discard_en ? 1 : 0);
642 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
643 : : vcfg->strip_tag2_discard_en ? 1 : 0);
644 : : /*
645 : : * In current version VF is not supported when PF is driven by DPDK
646 : : * driver, just need to configure parameters for PF vport.
647 : : */
648 : : vport_id = HNS3_PF_FUNC_ID;
649 : 0 : req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
650 : : bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
651 : 0 : req->vf_bitmap[req->vf_offset] = bitmap;
652 : :
653 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
654 [ # # ]: 0 : if (ret)
655 : 0 : hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
656 : 0 : return ret;
657 : : }
658 : :
659 : : static int
660 : 0 : hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
661 : : {
662 : : struct hns3_rx_vtag_cfg rxvlan_cfg;
663 : : struct hns3_hw *hw = &hns->hw;
664 : : int ret;
665 : :
666 [ # # ]: 0 : if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
667 : 0 : rxvlan_cfg.strip_tag1_en = false;
668 : 0 : rxvlan_cfg.strip_tag2_en = enable;
669 : 0 : rxvlan_cfg.strip_tag2_discard_en = false;
670 : : } else {
671 : 0 : rxvlan_cfg.strip_tag1_en = enable;
672 : 0 : rxvlan_cfg.strip_tag2_en = true;
673 : 0 : rxvlan_cfg.strip_tag2_discard_en = true;
674 : : }
675 : :
676 : 0 : rxvlan_cfg.strip_tag1_discard_en = false;
677 : 0 : rxvlan_cfg.vlan1_vlan_prionly = false;
678 : 0 : rxvlan_cfg.vlan2_vlan_prionly = false;
679 : 0 : rxvlan_cfg.rx_vlan_offload_en = enable;
680 : :
681 : 0 : ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
682 [ # # ]: 0 : if (ret) {
683 [ # # ]: 0 : hns3_err(hw, "%s strip rx vtag failed, ret = %d.",
684 : : enable ? "enable" : "disable", ret);
685 : 0 : return ret;
686 : : }
687 : :
688 : 0 : memcpy(&hns->pf.vtag_config.rx_vcfg, &rxvlan_cfg,
689 : : sizeof(struct hns3_rx_vtag_cfg));
690 : :
691 : 0 : return ret;
692 : : }
693 : :
694 : : static int
695 : 0 : hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
696 : : uint8_t fe_type, bool filter_en, uint8_t vf_id)
697 : : {
698 : : struct hns3_vlan_filter_ctrl_cmd *req;
699 : : struct hns3_cmd_desc desc;
700 : : int ret;
701 : :
702 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
703 : :
704 : : req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
705 : 0 : req->vlan_type = vlan_type;
706 [ # # ]: 0 : req->vlan_fe = filter_en ? fe_type : 0;
707 : 0 : req->vf_id = vf_id;
708 : :
709 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
710 [ # # ]: 0 : if (ret)
711 : 0 : hns3_err(hw, "set vlan filter fail, ret =%d", ret);
712 : :
713 : 0 : return ret;
714 : : }
715 : :
716 : : static int
717 : 0 : hns3_vlan_filter_init(struct hns3_adapter *hns)
718 : : {
719 : 0 : struct hns3_hw *hw = &hns->hw;
720 : : int ret;
721 : :
722 : 0 : ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
723 : : HNS3_FILTER_FE_EGRESS, false,
724 : : HNS3_PF_FUNC_ID);
725 [ # # ]: 0 : if (ret) {
726 : 0 : hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
727 : 0 : return ret;
728 : : }
729 : :
730 : 0 : ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
731 : : HNS3_FILTER_FE_INGRESS, false,
732 : : HNS3_PF_FUNC_ID);
733 [ # # ]: 0 : if (ret)
734 : 0 : hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
735 : :
736 : : return ret;
737 : : }
738 : :
739 : : static int
740 : 0 : hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
741 : : {
742 : 0 : struct hns3_hw *hw = &hns->hw;
743 : : int ret;
744 : :
745 : 0 : ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
746 : : HNS3_FILTER_FE_INGRESS, enable,
747 : : HNS3_PF_FUNC_ID);
748 [ # # ]: 0 : if (ret)
749 [ # # ]: 0 : hns3_err(hw, "failed to %s port vlan filter, ret = %d",
750 : : enable ? "enable" : "disable", ret);
751 : :
752 : 0 : return ret;
753 : : }
754 : :
755 : : static int
756 : 0 : hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
757 : : {
758 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
759 : : struct hns3_hw *hw = &hns->hw;
760 : : struct rte_eth_rxmode *rxmode;
761 : : unsigned int tmp_mask;
762 : : bool enable;
763 : : int ret = 0;
764 : :
765 : 0 : rte_spinlock_lock(&hw->lock);
766 : 0 : rxmode = &dev->data->dev_conf.rxmode;
767 : 0 : tmp_mask = (unsigned int)mask;
768 [ # # ]: 0 : if (tmp_mask & RTE_ETH_VLAN_FILTER_MASK) {
769 : : /* ignore vlan filter configuration during promiscuous mode */
770 [ # # ]: 0 : if (!dev->data->promiscuous) {
771 : : /* Enable or disable VLAN filter */
772 : 0 : enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ?
773 : 0 : true : false;
774 : :
775 : 0 : ret = hns3_enable_vlan_filter(hns, enable);
776 [ # # ]: 0 : if (ret) {
777 : : rte_spinlock_unlock(&hw->lock);
778 [ # # ]: 0 : hns3_err(hw, "failed to %s rx filter, ret = %d",
779 : : enable ? "enable" : "disable", ret);
780 : 0 : return ret;
781 : : }
782 : : }
783 : : }
784 : :
785 [ # # ]: 0 : if (tmp_mask & RTE_ETH_VLAN_STRIP_MASK) {
786 : : /* Enable or disable VLAN stripping */
787 : 0 : enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP ?
788 : 0 : true : false;
789 : :
790 : 0 : ret = hns3_en_hw_strip_rxvtag(hns, enable);
791 [ # # ]: 0 : if (ret) {
792 : : rte_spinlock_unlock(&hw->lock);
793 [ # # ]: 0 : hns3_err(hw, "failed to %s rx strip, ret = %d",
794 : : enable ? "enable" : "disable", ret);
795 : 0 : return ret;
796 : : }
797 : : }
798 : :
799 : : rte_spinlock_unlock(&hw->lock);
800 : :
801 : 0 : return ret;
802 : : }
803 : :
804 : : static int
805 : 0 : hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
806 : : struct hns3_tx_vtag_cfg *vcfg)
807 : : {
808 : : struct hns3_vport_vtag_tx_cfg_cmd *req;
809 : : struct hns3_cmd_desc desc;
810 : 0 : struct hns3_hw *hw = &hns->hw;
811 : : uint16_t vport_id;
812 : : uint8_t bitmap;
813 : : int ret;
814 : :
815 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
816 : :
817 : : req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
818 : 0 : req->def_vlan_tag1 = vcfg->default_tag1;
819 : 0 : req->def_vlan_tag2 = vcfg->default_tag2;
820 : 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
821 : : vcfg->accept_tag1 ? 1 : 0);
822 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
823 : : vcfg->accept_untag1 ? 1 : 0);
824 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
825 : : vcfg->accept_tag2 ? 1 : 0);
826 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
827 : : vcfg->accept_untag2 ? 1 : 0);
828 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
829 : : vcfg->insert_tag1_en ? 1 : 0);
830 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
831 : : vcfg->insert_tag2_en ? 1 : 0);
832 : 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
833 : :
834 : : /* firmware will ignore this configuration for PCI_REVISION_ID_HIP08 */
835 [ # # ]: 0 : hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
836 : : vcfg->tag_shift_mode_en ? 1 : 0);
837 : :
838 : : /*
839 : : * In current version VF is not supported when PF is driven by DPDK
840 : : * driver, just need to configure parameters for PF vport.
841 : : */
842 : : vport_id = HNS3_PF_FUNC_ID;
843 : 0 : req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
844 : : bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
845 : 0 : req->vf_bitmap[req->vf_offset] = bitmap;
846 : :
847 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
848 [ # # ]: 0 : if (ret)
849 : 0 : hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
850 : :
851 : 0 : return ret;
852 : : }
853 : :
854 : : static int
855 : 0 : hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
856 : : uint16_t pvid)
857 : : {
858 : : struct hns3_hw *hw = &hns->hw;
859 : : struct hns3_tx_vtag_cfg txvlan_cfg;
860 : : int ret;
861 : :
862 [ # # ]: 0 : if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
863 : 0 : txvlan_cfg.accept_tag1 = true;
864 : 0 : txvlan_cfg.insert_tag1_en = false;
865 : 0 : txvlan_cfg.default_tag1 = 0;
866 : : } else {
867 : 0 : txvlan_cfg.accept_tag1 =
868 : 0 : hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
869 : 0 : txvlan_cfg.insert_tag1_en = true;
870 : 0 : txvlan_cfg.default_tag1 = pvid;
871 : : }
872 : :
873 : 0 : txvlan_cfg.accept_untag1 = true;
874 : 0 : txvlan_cfg.accept_tag2 = true;
875 : 0 : txvlan_cfg.accept_untag2 = true;
876 : 0 : txvlan_cfg.insert_tag2_en = false;
877 : 0 : txvlan_cfg.default_tag2 = 0;
878 : 0 : txvlan_cfg.tag_shift_mode_en = true;
879 : :
880 : 0 : ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
881 [ # # ]: 0 : if (ret) {
882 : 0 : hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
883 : : ret);
884 : 0 : return ret;
885 : : }
886 : :
887 : 0 : memcpy(&hns->pf.vtag_config.tx_vcfg, &txvlan_cfg,
888 : : sizeof(struct hns3_tx_vtag_cfg));
889 : :
890 : 0 : return ret;
891 : : }
892 : :
893 : :
894 : : static void
895 : 0 : hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
896 : : {
897 : : struct hns3_user_vlan_table *vlan_entry;
898 : : struct hns3_pf *pf = &hns->pf;
899 : :
900 [ # # ]: 0 : LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
901 [ # # ]: 0 : if (vlan_entry->hd_tbl_status) {
902 : 0 : hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
903 : 0 : vlan_entry->hd_tbl_status = false;
904 : : }
905 : : }
906 : :
907 [ # # ]: 0 : if (is_del_list) {
908 : 0 : vlan_entry = LIST_FIRST(&pf->vlan_list);
909 [ # # ]: 0 : while (vlan_entry) {
910 [ # # ]: 0 : LIST_REMOVE(vlan_entry, next);
911 : 0 : rte_free(vlan_entry);
912 : 0 : vlan_entry = LIST_FIRST(&pf->vlan_list);
913 : : }
914 : : }
915 : 0 : }
916 : :
917 : : static void
918 : 0 : hns3_add_all_vlan_table(struct hns3_adapter *hns)
919 : : {
920 : : struct hns3_user_vlan_table *vlan_entry;
921 : : struct hns3_pf *pf = &hns->pf;
922 : :
923 [ # # ]: 0 : LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
924 [ # # ]: 0 : if (!vlan_entry->hd_tbl_status) {
925 : 0 : hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
926 : 0 : vlan_entry->hd_tbl_status = true;
927 : : }
928 : : }
929 : 0 : }
930 : :
931 : : static void
932 : 0 : hns3_remove_all_vlan_table(struct hns3_adapter *hns)
933 : : {
934 : : struct hns3_hw *hw = &hns->hw;
935 : : int ret;
936 : :
937 : 0 : hns3_rm_all_vlan_table(hns, true);
938 [ # # ]: 0 : if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
939 : 0 : ret = hns3_set_port_vlan_filter(hns,
940 : : hw->port_base_vlan_cfg.pvid, 0);
941 [ # # ]: 0 : if (ret) {
942 : 0 : hns3_err(hw, "Failed to remove all vlan table, ret =%d",
943 : : ret);
944 : 0 : return;
945 : : }
946 : : }
947 : : }
948 : :
949 : : static int
950 : 0 : hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
951 : : uint16_t port_base_vlan_state, uint16_t new_pvid)
952 : : {
953 : : struct hns3_hw *hw = &hns->hw;
954 : : uint16_t old_pvid;
955 : : int ret;
956 : :
957 [ # # ]: 0 : if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
958 : 0 : old_pvid = hw->port_base_vlan_cfg.pvid;
959 [ # # ]: 0 : if (old_pvid != HNS3_INVALID_PVID) {
960 : 0 : ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
961 [ # # ]: 0 : if (ret) {
962 : 0 : hns3_err(hw, "failed to remove old pvid %u, "
963 : : "ret = %d", old_pvid, ret);
964 : 0 : return ret;
965 : : }
966 : : }
967 : :
968 : 0 : hns3_rm_all_vlan_table(hns, false);
969 : 0 : ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
970 [ # # ]: 0 : if (ret) {
971 : 0 : hns3_err(hw, "failed to add new pvid %u, ret = %d",
972 : : new_pvid, ret);
973 : 0 : return ret;
974 : : }
975 : : } else {
976 : 0 : ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
977 [ # # ]: 0 : if (ret) {
978 : 0 : hns3_err(hw, "failed to remove pvid %u, ret = %d",
979 : : new_pvid, ret);
980 : 0 : return ret;
981 : : }
982 : :
983 : 0 : hns3_add_all_vlan_table(hns);
984 : : }
985 : : return 0;
986 : : }
987 : :
988 : : static int
989 : 0 : hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
990 : : {
991 : : struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
992 : : struct hns3_rx_vtag_cfg rx_vlan_cfg;
993 : : bool rx_strip_en;
994 : : int ret;
995 : :
996 : 0 : rx_strip_en = old_cfg->rx_vlan_offload_en;
997 [ # # ]: 0 : if (on) {
998 : 0 : rx_vlan_cfg.strip_tag1_en = rx_strip_en;
999 : 0 : rx_vlan_cfg.strip_tag2_en = true;
1000 : 0 : rx_vlan_cfg.strip_tag2_discard_en = true;
1001 : : } else {
1002 : 0 : rx_vlan_cfg.strip_tag1_en = false;
1003 : 0 : rx_vlan_cfg.strip_tag2_en = rx_strip_en;
1004 : 0 : rx_vlan_cfg.strip_tag2_discard_en = false;
1005 : : }
1006 : 0 : rx_vlan_cfg.strip_tag1_discard_en = false;
1007 : 0 : rx_vlan_cfg.vlan1_vlan_prionly = false;
1008 : 0 : rx_vlan_cfg.vlan2_vlan_prionly = false;
1009 : 0 : rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
1010 : :
1011 : 0 : ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
1012 [ # # ]: 0 : if (ret)
1013 : : return ret;
1014 : :
1015 : 0 : memcpy(&hns->pf.vtag_config.rx_vcfg, &rx_vlan_cfg,
1016 : : sizeof(struct hns3_rx_vtag_cfg));
1017 : :
1018 : 0 : return ret;
1019 : : }
1020 : :
1021 : : static int
1022 : 0 : hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
1023 : : {
1024 : : struct hns3_hw *hw = &hns->hw;
1025 : : uint16_t port_base_vlan_state;
1026 : : int ret, err;
1027 : :
1028 [ # # # # ]: 0 : if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
1029 [ # # ]: 0 : if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
1030 : 0 : hns3_warn(hw, "Invalid operation! As current pvid set "
1031 : : "is %u, disable pvid %u is invalid",
1032 : : hw->port_base_vlan_cfg.pvid, pvid);
1033 : 0 : return 0;
1034 : : }
1035 : :
1036 : 0 : port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
1037 : : HNS3_PORT_BASE_VLAN_DISABLE;
1038 : 0 : ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
1039 [ # # ]: 0 : if (ret) {
1040 : 0 : hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
1041 : : ret);
1042 : 0 : return ret;
1043 : : }
1044 : :
1045 : 0 : ret = hns3_en_pvid_strip(hns, on);
1046 [ # # ]: 0 : if (ret) {
1047 : 0 : hns3_err(hw, "failed to config rx vlan strip for pvid, "
1048 : : "ret = %d", ret);
1049 : 0 : goto pvid_vlan_strip_fail;
1050 : : }
1051 : :
1052 [ # # ]: 0 : if (pvid == HNS3_INVALID_PVID)
1053 : 0 : goto out;
1054 : 0 : ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
1055 [ # # ]: 0 : if (ret) {
1056 : 0 : hns3_err(hw, "failed to update vlan filter entries, ret = %d",
1057 : : ret);
1058 : 0 : goto vlan_filter_set_fail;
1059 : : }
1060 : :
1061 : 0 : out:
1062 : 0 : hw->port_base_vlan_cfg.state = port_base_vlan_state;
1063 [ # # ]: 0 : hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
1064 : 0 : return ret;
1065 : :
1066 : : vlan_filter_set_fail:
1067 : 0 : err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state ==
1068 : : HNS3_PORT_BASE_VLAN_ENABLE);
1069 [ # # ]: 0 : if (err)
1070 : 0 : hns3_err(hw, "fail to rollback pvid strip, ret = %d", err);
1071 : :
1072 : 0 : pvid_vlan_strip_fail:
1073 : 0 : err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state,
1074 : 0 : hw->port_base_vlan_cfg.pvid);
1075 [ # # ]: 0 : if (err)
1076 : 0 : hns3_err(hw, "fail to rollback txvlan status, ret = %d", err);
1077 : :
1078 : : return ret;
1079 : : }
1080 : :
1081 : : static int
1082 : 0 : hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1083 : : {
1084 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1085 : 0 : struct hns3_hw *hw = &hns->hw;
1086 : : bool pvid_en_state_change;
1087 : : uint16_t pvid_state;
1088 : : int ret;
1089 : :
1090 [ # # ]: 0 : if (pvid > RTE_ETHER_MAX_VLAN_ID) {
1091 : 0 : hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
1092 : : RTE_ETHER_MAX_VLAN_ID);
1093 : 0 : return -EINVAL;
1094 : : }
1095 : :
1096 : : /*
1097 : : * If PVID configuration state change, should refresh the PVID
1098 : : * configuration state in struct hns3_tx_queue/hns3_rx_queue.
1099 : : */
1100 : 0 : pvid_state = hw->port_base_vlan_cfg.state;
1101 [ # # ]: 0 : if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
1102 [ # # ]: 0 : (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
1103 : : pvid_en_state_change = false;
1104 : : else
1105 : : pvid_en_state_change = true;
1106 : :
1107 : 0 : rte_spinlock_lock(&hw->lock);
1108 : 0 : ret = hns3_vlan_pvid_configure(hns, pvid, on);
1109 : : rte_spinlock_unlock(&hw->lock);
1110 [ # # ]: 0 : if (ret)
1111 : : return ret;
1112 : : /*
1113 : : * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1114 : : * need be processed by PMD.
1115 : : */
1116 [ # # ]: 0 : if (pvid_en_state_change &&
1117 [ # # ]: 0 : hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1118 : 0 : hns3_update_all_queues_pvid_proc_en(hw);
1119 : :
1120 : : return 0;
1121 : : }
1122 : :
1123 : : static int
1124 : 0 : hns3_default_vlan_config(struct hns3_adapter *hns)
1125 : : {
1126 : : struct hns3_hw *hw = &hns->hw;
1127 : : int ret;
1128 : :
1129 : : /*
1130 : : * When vlan filter is enabled, hardware regards packets without vlan
1131 : : * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1132 : : * table, packets without vlan won't be received. So, add vlan 0 as
1133 : : * the default vlan.
1134 : : */
1135 : 0 : ret = hns3_vlan_filter_configure(hns, 0, 1);
1136 [ # # ]: 0 : if (ret)
1137 : 0 : hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1138 : 0 : return ret;
1139 : : }
1140 : :
1141 : : static int
1142 : 0 : hns3_init_vlan_config(struct hns3_adapter *hns)
1143 : : {
1144 : : struct hns3_hw *hw = &hns->hw;
1145 : : int ret;
1146 : :
1147 : : /*
1148 : : * This function can be called in the initialization and reset process,
1149 : : * when in reset process, it means that hardware had been reseted
1150 : : * successfully and we need to restore the hardware configuration to
1151 : : * ensure that the hardware configuration remains unchanged before and
1152 : : * after reset.
1153 : : */
1154 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed) == 0) {
1155 : 0 : hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1156 : 0 : hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1157 : : }
1158 : :
1159 : 0 : ret = hns3_vlan_filter_init(hns);
1160 [ # # ]: 0 : if (ret) {
1161 : 0 : hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1162 : 0 : return ret;
1163 : : }
1164 : :
1165 : 0 : ret = hns3_vlan_tpid_configure(hns, RTE_ETH_VLAN_TYPE_INNER,
1166 : : RTE_ETHER_TYPE_VLAN);
1167 [ # # ]: 0 : if (ret) {
1168 : 0 : hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1169 : 0 : return ret;
1170 : : }
1171 : :
1172 : : /*
1173 : : * When in the reinit dev stage of the reset process, the following
1174 : : * vlan-related configurations may differ from those at initialization,
1175 : : * we will restore configurations to hardware in hns3_restore_vlan_table
1176 : : * and hns3_restore_vlan_conf later.
1177 : : */
1178 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed) == 0) {
1179 : 0 : ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1180 [ # # ]: 0 : if (ret) {
1181 : 0 : hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1182 : 0 : return ret;
1183 : : }
1184 : :
1185 : 0 : ret = hns3_en_hw_strip_rxvtag(hns, false);
1186 [ # # ]: 0 : if (ret) {
1187 : 0 : hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1188 : : ret);
1189 : 0 : return ret;
1190 : : }
1191 : : }
1192 : :
1193 : 0 : return hns3_default_vlan_config(hns);
1194 : : }
1195 : :
1196 : : static int
1197 : 0 : hns3_restore_vlan_conf(struct hns3_adapter *hns)
1198 : : {
1199 : : struct hns3_pf *pf = &hns->pf;
1200 : : struct hns3_hw *hw = &hns->hw;
1201 : : uint64_t offloads;
1202 : : bool enable;
1203 : : int ret;
1204 : :
1205 [ # # ]: 0 : if (!hw->data->promiscuous) {
1206 : : /* restore vlan filter states */
1207 : 0 : offloads = hw->data->dev_conf.rxmode.offloads;
1208 : 0 : enable = offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ? true : false;
1209 : 0 : ret = hns3_enable_vlan_filter(hns, enable);
1210 [ # # ]: 0 : if (ret) {
1211 : 0 : hns3_err(hw, "failed to restore vlan rx filter conf, "
1212 : : "ret = %d", ret);
1213 : 0 : return ret;
1214 : : }
1215 : : }
1216 : :
1217 : 0 : ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1218 [ # # ]: 0 : if (ret) {
1219 : 0 : hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1220 : 0 : return ret;
1221 : : }
1222 : :
1223 : 0 : ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1224 [ # # ]: 0 : if (ret)
1225 : 0 : hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1226 : :
1227 : : return ret;
1228 : : }
1229 : :
1230 : : static int
1231 : 0 : hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1232 : : {
1233 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1234 : : struct rte_eth_dev_data *data = dev->data;
1235 : : struct rte_eth_txmode *txmode;
1236 : : struct hns3_hw *hw = &hns->hw;
1237 : : int mask;
1238 : : int ret;
1239 : :
1240 : : txmode = &data->dev_conf.txmode;
1241 [ # # ]: 0 : if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1242 : 0 : hns3_warn(hw,
1243 : : "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1244 : : "configuration is not supported! Ignore these two "
1245 : : "parameters: hw_vlan_reject_tagged(%u), "
1246 : : "hw_vlan_reject_untagged(%u)",
1247 : : txmode->hw_vlan_reject_tagged,
1248 : : txmode->hw_vlan_reject_untagged);
1249 : :
1250 : : /* Apply vlan offload setting */
1251 : : mask = RTE_ETH_VLAN_STRIP_MASK | RTE_ETH_VLAN_FILTER_MASK;
1252 : 0 : ret = hns3_vlan_offload_set(dev, mask);
1253 [ # # ]: 0 : if (ret) {
1254 : 0 : hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1255 : : ret);
1256 : 0 : return ret;
1257 : : }
1258 : :
1259 : : /*
1260 : : * If pvid config is not set in rte_eth_conf, driver needn't to set
1261 : : * VLAN pvid related configuration to hardware.
1262 : : */
1263 [ # # ]: 0 : if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1264 : : return 0;
1265 : :
1266 : : /* Apply pvid setting */
1267 : 0 : ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1268 : 0 : txmode->hw_vlan_insert_pvid);
1269 [ # # ]: 0 : if (ret)
1270 : 0 : hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1271 : : txmode->pvid, ret);
1272 : :
1273 : : return ret;
1274 : : }
1275 : :
1276 : : static int
1277 : 0 : hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1278 : : unsigned int tso_mss_max)
1279 : : {
1280 : : struct hns3_cfg_tso_status_cmd *req;
1281 : : struct hns3_cmd_desc desc;
1282 : : uint16_t tso_mss;
1283 : :
1284 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1285 : :
1286 : : req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1287 : :
1288 : : tso_mss = 0;
1289 : 0 : hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1290 : : tso_mss_min);
1291 : 0 : req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1292 : :
1293 : : tso_mss = 0;
1294 : 0 : hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1295 : : tso_mss_max);
1296 : 0 : req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1297 : :
1298 : 0 : return hns3_cmd_send(hw, &desc, 1);
1299 : : }
1300 : :
1301 : : static int
1302 : 0 : hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1303 : : uint16_t *allocated_size, bool is_alloc)
1304 : : {
1305 : : struct hns3_umv_spc_alc_cmd *req;
1306 : : struct hns3_cmd_desc desc;
1307 : : int ret;
1308 : :
1309 : : req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1310 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1311 : 0 : hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1312 : 0 : req->space_size = rte_cpu_to_le_32(space_size);
1313 : :
1314 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
1315 [ # # ]: 0 : if (ret) {
1316 [ # # ]: 0 : PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1317 : : is_alloc ? "allocate" : "free", ret);
1318 : 0 : return ret;
1319 : : }
1320 : :
1321 [ # # ]: 0 : if (is_alloc && allocated_size)
1322 : 0 : *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1323 : :
1324 : : return 0;
1325 : : }
1326 : :
1327 : : static int
1328 : 0 : hns3_init_umv_space(struct hns3_hw *hw)
1329 : : {
1330 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1331 : : struct hns3_pf *pf = &hns->pf;
1332 : 0 : uint16_t allocated_size = 0;
1333 : : uint16_t wanted_umv_size;
1334 : : int ret;
1335 : :
1336 : 0 : wanted_umv_size = pf->cfg_umv_size;
1337 [ # # ]: 0 : if (wanted_umv_size == 0)
1338 : 0 : wanted_umv_size = pf->def_umv_size;
1339 [ # # ]: 0 : if (wanted_umv_size == 0)
1340 : : wanted_umv_size = HNS3_DEFAULT_UMV_SPACE_PER_PF;
1341 : :
1342 : 0 : ret = hns3_set_umv_space(hw, wanted_umv_size, &allocated_size,
1343 : : true);
1344 [ # # ]: 0 : if (ret)
1345 : : return ret;
1346 : :
1347 [ # # ]: 0 : if (allocated_size < wanted_umv_size)
1348 : 0 : PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1349 : : wanted_umv_size, allocated_size);
1350 : :
1351 [ # # ]: 0 : pf->max_umv_size = (!!allocated_size) ? allocated_size :
1352 : : wanted_umv_size;
1353 : 0 : pf->used_umv_size = 0;
1354 : 0 : return 0;
1355 : : }
1356 : :
1357 : : static int
1358 : 0 : hns3_uninit_umv_space(struct hns3_hw *hw)
1359 : : {
1360 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1361 : : struct hns3_pf *pf = &hns->pf;
1362 : : int ret;
1363 : :
1364 [ # # ]: 0 : if (pf->max_umv_size == 0)
1365 : : return 0;
1366 : :
1367 : 0 : ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1368 [ # # ]: 0 : if (ret)
1369 : : return ret;
1370 : :
1371 : 0 : pf->max_umv_size = 0;
1372 : :
1373 : 0 : return 0;
1374 : : }
1375 : :
1376 : : static bool
1377 : : hns3_is_umv_space_full(struct hns3_hw *hw)
1378 : : {
1379 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1380 : : struct hns3_pf *pf = &hns->pf;
1381 : : bool is_full;
1382 : :
1383 : 0 : is_full = (pf->used_umv_size >= pf->max_umv_size);
1384 : :
1385 : : return is_full;
1386 : : }
1387 : :
1388 : : static void
1389 : : hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1390 : : {
1391 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1392 : : struct hns3_pf *pf = &hns->pf;
1393 : :
1394 : : if (is_free) {
1395 [ # # ]: 0 : if (pf->used_umv_size > 0)
1396 : 0 : pf->used_umv_size--;
1397 : : } else
1398 : 0 : pf->used_umv_size++;
1399 : 0 : }
1400 : :
1401 : : static void
1402 : 0 : hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1403 : : const uint8_t *addr, bool is_mc)
1404 : : {
1405 : : const unsigned char *mac_addr = addr;
1406 : 0 : uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1407 : 0 : ((uint32_t)mac_addr[2] << 16) |
1408 : 0 : ((uint32_t)mac_addr[1] << 8) |
1409 : 0 : (uint32_t)mac_addr[0];
1410 : 0 : uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1411 : :
1412 : 0 : hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1413 [ # # ]: 0 : if (is_mc) {
1414 : 0 : hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1415 : 0 : hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1416 : 0 : hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1417 : : }
1418 : :
1419 : 0 : new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1420 : 0 : new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1421 : 0 : }
1422 : :
1423 : : static int
1424 : 0 : hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1425 : : uint8_t resp_code,
1426 : : enum hns3_mac_vlan_tbl_opcode op)
1427 : : {
1428 [ # # ]: 0 : if (cmdq_resp) {
1429 : 0 : hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1430 : : cmdq_resp);
1431 : 0 : return -EIO;
1432 : : }
1433 : :
1434 [ # # ]: 0 : if (op == HNS3_MAC_VLAN_ADD) {
1435 [ # # ]: 0 : if (resp_code == 0 || resp_code == 1) {
1436 : : return 0;
1437 [ # # ]: 0 : } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1438 : 0 : hns3_err(hw, "add mac addr failed for uc_overflow");
1439 : 0 : return -ENOSPC;
1440 [ # # ]: 0 : } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1441 : 0 : hns3_err(hw, "add mac addr failed for mc_overflow");
1442 : 0 : return -ENOSPC;
1443 : : }
1444 : :
1445 : 0 : hns3_err(hw, "add mac addr failed for undefined, code=%u",
1446 : : resp_code);
1447 : 0 : return -EIO;
1448 [ # # ]: 0 : } else if (op == HNS3_MAC_VLAN_REMOVE) {
1449 [ # # ]: 0 : if (resp_code == 0) {
1450 : : return 0;
1451 [ # # ]: 0 : } else if (resp_code == 1) {
1452 : 0 : hns3_dbg(hw, "remove mac addr failed for miss");
1453 : 0 : return -ENOENT;
1454 : : }
1455 : :
1456 : 0 : hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1457 : : resp_code);
1458 : 0 : return -EIO;
1459 [ # # ]: 0 : } else if (op == HNS3_MAC_VLAN_LKUP) {
1460 [ # # ]: 0 : if (resp_code == 0) {
1461 : : return 0;
1462 [ # # ]: 0 : } else if (resp_code == 1) {
1463 : 0 : hns3_dbg(hw, "lookup mac addr failed for miss");
1464 : 0 : return -ENOENT;
1465 : : }
1466 : :
1467 : 0 : hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1468 : : resp_code);
1469 : 0 : return -EIO;
1470 : : }
1471 : :
1472 : 0 : hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1473 : : op);
1474 : :
1475 : 0 : return -EINVAL;
1476 : : }
1477 : :
1478 : : static int
1479 : 0 : hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1480 : : struct hns3_mac_vlan_tbl_entry_cmd *req,
1481 : : struct hns3_cmd_desc *desc, uint8_t desc_num)
1482 : : {
1483 : : uint8_t resp_code;
1484 : : uint16_t retval;
1485 : : int ret;
1486 : : int i;
1487 : :
1488 [ # # ]: 0 : if (desc_num == HNS3_MC_MAC_VLAN_OPS_DESC_NUM) {
1489 [ # # ]: 0 : for (i = 0; i < desc_num - 1; i++) {
1490 : 0 : hns3_cmd_setup_basic_desc(&desc[i],
1491 : : HNS3_OPC_MAC_VLAN_ADD, true);
1492 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1493 [ # # ]: 0 : if (i == 0)
1494 : 0 : memcpy(desc[i].data, req,
1495 : : sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1496 : : }
1497 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_MAC_VLAN_ADD,
1498 : : true);
1499 : : } else {
1500 : 0 : hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD,
1501 : : true);
1502 : 0 : memcpy(desc[0].data, req,
1503 : : sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1504 : : }
1505 : 0 : ret = hns3_cmd_send(hw, desc, desc_num);
1506 [ # # ]: 0 : if (ret) {
1507 : 0 : hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1508 : : ret);
1509 : 0 : return ret;
1510 : : }
1511 : 0 : resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1512 : 0 : retval = rte_le_to_cpu_16(desc[0].retval);
1513 : :
1514 : 0 : return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1515 : : HNS3_MAC_VLAN_LKUP);
1516 : : }
1517 : :
1518 : : static int
1519 : 0 : hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1520 : : struct hns3_mac_vlan_tbl_entry_cmd *req,
1521 : : struct hns3_cmd_desc *desc, uint8_t desc_num)
1522 : : {
1523 : : uint8_t resp_code;
1524 : : uint16_t retval;
1525 : : int cfg_status;
1526 : : int ret;
1527 : : int i;
1528 : :
1529 [ # # ]: 0 : if (desc_num == HNS3_UC_MAC_VLAN_OPS_DESC_NUM) {
1530 : 0 : hns3_cmd_setup_basic_desc(desc, HNS3_OPC_MAC_VLAN_ADD, false);
1531 : 0 : memcpy(desc->data, req,
1532 : : sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1533 : 0 : ret = hns3_cmd_send(hw, desc, desc_num);
1534 : 0 : resp_code = (rte_le_to_cpu_32(desc->data[0]) >> 8) & 0xff;
1535 : 0 : retval = rte_le_to_cpu_16(desc->retval);
1536 : :
1537 : 0 : cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1538 : : HNS3_MAC_VLAN_ADD);
1539 : : } else {
1540 [ # # ]: 0 : for (i = 0; i < desc_num; i++) {
1541 : 0 : hns3_cmd_reuse_desc(&desc[i], false);
1542 [ # # ]: 0 : if (i == desc_num - 1)
1543 : 0 : desc[i].flag &=
1544 : : rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1545 : : else
1546 : 0 : desc[i].flag |=
1547 : : rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1548 : : }
1549 : 0 : memcpy(desc[0].data, req,
1550 : : sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1551 : 0 : desc[0].retval = 0;
1552 : 0 : ret = hns3_cmd_send(hw, desc, desc_num);
1553 : 0 : resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1554 : 0 : retval = rte_le_to_cpu_16(desc[0].retval);
1555 : :
1556 : 0 : cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1557 : : HNS3_MAC_VLAN_ADD);
1558 : : }
1559 : :
1560 [ # # ]: 0 : if (ret) {
1561 : 0 : hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1562 : 0 : return ret;
1563 : : }
1564 : :
1565 : : return cfg_status;
1566 : : }
1567 : :
1568 : : static int
1569 : 0 : hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1570 : : struct hns3_mac_vlan_tbl_entry_cmd *req)
1571 : : {
1572 : : struct hns3_cmd_desc desc;
1573 : : uint8_t resp_code;
1574 : : uint16_t retval;
1575 : : int ret;
1576 : :
1577 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1578 : :
1579 : : memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1580 : :
1581 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
1582 [ # # ]: 0 : if (ret) {
1583 : 0 : hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1584 : 0 : return ret;
1585 : : }
1586 : 0 : resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1587 : 0 : retval = rte_le_to_cpu_16(desc.retval);
1588 : :
1589 : 0 : return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1590 : : HNS3_MAC_VLAN_REMOVE);
1591 : : }
1592 : :
1593 : : static int
1594 [ # # ]: 0 : hns3_add_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1595 : : {
1596 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1597 : : struct hns3_mac_vlan_tbl_entry_cmd req;
1598 : : struct hns3_pf *pf = &hns->pf;
1599 : : struct hns3_cmd_desc desc;
1600 : : char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1601 : : uint16_t egress_port = 0;
1602 : : uint8_t vf_id;
1603 : : int ret;
1604 : :
1605 : : /* check if mac addr is valid */
1606 : : if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1607 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1608 : : mac_addr);
1609 : 0 : hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1610 : : mac_str);
1611 : 0 : return -EINVAL;
1612 : : }
1613 : :
1614 : : memset(&req, 0, sizeof(req));
1615 : :
1616 : : /*
1617 : : * In current version VF is not supported when PF is driven by DPDK
1618 : : * driver, just need to configure parameters for PF vport.
1619 : : */
1620 : : vf_id = HNS3_PF_FUNC_ID;
1621 : : hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1622 : : HNS3_MAC_EPORT_VFID_S, vf_id);
1623 : :
1624 : : req.egress_port = rte_cpu_to_le_16(egress_port);
1625 : :
1626 : 0 : hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1627 : :
1628 : : /*
1629 : : * Lookup the mac address in the mac_vlan table, and add
1630 : : * it if the entry is inexistent. Repeated unicast entry
1631 : : * is not allowed in the mac vlan table.
1632 : : */
1633 : 0 : ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc,
1634 : : HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1635 [ # # ]: 0 : if (ret == -ENOENT) {
1636 [ # # ]: 0 : if (!hns3_is_umv_space_full(hw)) {
1637 : 0 : ret = hns3_add_mac_vlan_tbl(hw, &req, &desc,
1638 : : HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1639 [ # # ]: 0 : if (!ret)
1640 : : hns3_update_umv_space(hw, false);
1641 : 0 : return ret;
1642 : : }
1643 : :
1644 : 0 : hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1645 : :
1646 : 0 : return -ENOSPC;
1647 : : }
1648 : :
1649 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1650 : :
1651 : : /* check if we just hit the duplicate */
1652 [ # # ]: 0 : if (ret == 0) {
1653 : 0 : hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1654 : 0 : return 0;
1655 : : }
1656 : :
1657 : 0 : hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1658 : : mac_str);
1659 : :
1660 : 0 : return ret;
1661 : : }
1662 : :
1663 : : static int
1664 [ # # ]: 0 : hns3_remove_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1665 : : {
1666 : : struct hns3_mac_vlan_tbl_entry_cmd req;
1667 : : char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1668 : : int ret;
1669 : :
1670 : : /* check if mac addr is valid */
1671 : : if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1672 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1673 : : mac_addr);
1674 : 0 : hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1675 : : mac_str);
1676 : 0 : return -EINVAL;
1677 : : }
1678 : :
1679 : : memset(&req, 0, sizeof(req));
1680 : : hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1681 : 0 : hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1682 : 0 : ret = hns3_remove_mac_vlan_tbl(hw, &req);
1683 [ # # ]: 0 : if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1684 : : return 0;
1685 [ # # ]: 0 : else if (ret == 0)
1686 : : hns3_update_umv_space(hw, true);
1687 : :
1688 : : return ret;
1689 : : }
1690 : :
1691 : : static int
1692 : 0 : hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1693 : : struct rte_ether_addr *mac_addr)
1694 : : {
1695 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1696 : : struct rte_ether_addr *oaddr;
1697 : : char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1698 : : int ret, ret_val;
1699 : :
1700 : 0 : rte_spinlock_lock(&hw->lock);
1701 : 0 : oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1702 : 0 : ret = hw->ops.del_uc_mac_addr(hw, oaddr);
1703 [ # # ]: 0 : if (ret) {
1704 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1705 : : oaddr);
1706 : 0 : hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1707 : : mac_str, ret);
1708 : :
1709 : : rte_spinlock_unlock(&hw->lock);
1710 : 0 : return ret;
1711 : : }
1712 : :
1713 : 0 : ret = hw->ops.add_uc_mac_addr(hw, mac_addr);
1714 [ # # ]: 0 : if (ret) {
1715 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1716 : : mac_addr);
1717 : 0 : hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1718 : 0 : goto err_add_uc_addr;
1719 : : }
1720 : :
1721 : 0 : ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1722 [ # # ]: 0 : if (ret) {
1723 : 0 : hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1724 : 0 : goto err_pause_addr_cfg;
1725 : : }
1726 : :
1727 : : rte_ether_addr_copy(mac_addr,
1728 : : (struct rte_ether_addr *)hw->mac.mac_addr);
1729 : : rte_spinlock_unlock(&hw->lock);
1730 : :
1731 : 0 : return 0;
1732 : :
1733 : : err_pause_addr_cfg:
1734 : 0 : ret_val = hw->ops.del_uc_mac_addr(hw, mac_addr);
1735 [ # # ]: 0 : if (ret_val) {
1736 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1737 : : mac_addr);
1738 : 0 : hns3_warn(hw,
1739 : : "Failed to roll back to del set mac addr(%s): %d",
1740 : : mac_str, ret_val);
1741 : : }
1742 : :
1743 : 0 : err_add_uc_addr:
1744 : 0 : ret_val = hw->ops.add_uc_mac_addr(hw, oaddr);
1745 [ # # ]: 0 : if (ret_val) {
1746 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, oaddr);
1747 : 0 : hns3_warn(hw, "Failed to restore old uc mac addr(%s): %d",
1748 : : mac_str, ret_val);
1749 : : }
1750 : : rte_spinlock_unlock(&hw->lock);
1751 : :
1752 : 0 : return ret;
1753 : : }
1754 : :
1755 : : static void
1756 : 0 : hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1757 : : {
1758 : : #define HNS3_VF_NUM_IN_FIRST_DESC 192
1759 : : uint8_t word_num;
1760 : : uint8_t bit_num;
1761 : :
1762 [ # # ]: 0 : if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1763 : 0 : word_num = vfid / 32;
1764 : 0 : bit_num = vfid % 32;
1765 [ # # ]: 0 : if (clr)
1766 : 0 : desc[1].data[word_num] &=
1767 : 0 : rte_cpu_to_le_32(~(1UL << bit_num));
1768 : : else
1769 : 0 : desc[1].data[word_num] |=
1770 : 0 : rte_cpu_to_le_32(1UL << bit_num);
1771 : : } else {
1772 : 0 : word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1773 : 0 : bit_num = vfid % 32;
1774 [ # # ]: 0 : if (clr)
1775 : 0 : desc[2].data[word_num] &=
1776 : 0 : rte_cpu_to_le_32(~(1UL << bit_num));
1777 : : else
1778 : 0 : desc[2].data[word_num] |=
1779 : 0 : rte_cpu_to_le_32(1UL << bit_num);
1780 : : }
1781 : 0 : }
1782 : :
1783 : : static int
1784 [ # # ]: 0 : hns3_add_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1785 : : {
1786 : : struct hns3_cmd_desc desc[HNS3_MC_MAC_VLAN_OPS_DESC_NUM];
1787 : : struct hns3_mac_vlan_tbl_entry_cmd req;
1788 : : char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1789 : : uint8_t vf_id;
1790 : : int ret;
1791 : : int idx;
1792 : :
1793 : : /* Check if mac addr is valid */
1794 [ # # ]: 0 : if (!rte_is_multicast_ether_addr(mac_addr)) {
1795 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1796 : : mac_addr);
1797 : 0 : hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1798 : : mac_str);
1799 : 0 : return -EINVAL;
1800 : : }
1801 : :
1802 : : memset(&req, 0, sizeof(req));
1803 : : hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1804 : 0 : hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1805 : 0 : ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1806 : : HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1807 [ # # ]: 0 : if (ret) {
1808 : : /* This mac addr do not exist, add new entry for it */
1809 [ # # ]: 0 : for (idx = 0; idx < HNS3_MC_MAC_VLAN_OPS_DESC_NUM; idx++)
1810 : 0 : memset(desc[idx].data, 0, sizeof(desc[idx].data));
1811 : : }
1812 : :
1813 : : /*
1814 : : * In current version VF is not supported when PF is driven by DPDK
1815 : : * driver, just need to configure parameters for PF vport.
1816 : : */
1817 : : vf_id = HNS3_PF_FUNC_ID;
1818 : 0 : hns3_update_desc_vfid(desc, vf_id, false);
1819 : 0 : ret = hns3_add_mac_vlan_tbl(hw, &req, desc,
1820 : : HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1821 [ # # ]: 0 : if (ret) {
1822 [ # # ]: 0 : if (ret == -ENOSPC)
1823 : 0 : hns3_err(hw, "mc mac vlan table is full");
1824 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1825 : : mac_addr);
1826 : 0 : hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1827 : : }
1828 : :
1829 : : return ret;
1830 : : }
1831 : :
1832 : : static int
1833 [ # # ]: 0 : hns3_remove_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1834 : : {
1835 : : struct hns3_mac_vlan_tbl_entry_cmd req;
1836 : : struct hns3_cmd_desc desc[3];
1837 : : char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1838 : : uint8_t vf_id;
1839 : : int ret;
1840 : :
1841 : : /* Check if mac addr is valid */
1842 [ # # ]: 0 : if (!rte_is_multicast_ether_addr(mac_addr)) {
1843 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1844 : : mac_addr);
1845 : 0 : hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1846 : : mac_str);
1847 : 0 : return -EINVAL;
1848 : : }
1849 : :
1850 : : memset(&req, 0, sizeof(req));
1851 : : hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1852 : 0 : hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1853 : 0 : ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1854 : : HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1855 [ # # ]: 0 : if (ret == 0) {
1856 : : /*
1857 : : * This mac addr exist, remove this handle's VFID for it.
1858 : : * In current version VF is not supported when PF is driven by
1859 : : * DPDK driver, just need to configure parameters for PF vport.
1860 : : */
1861 : : vf_id = HNS3_PF_FUNC_ID;
1862 : 0 : hns3_update_desc_vfid(desc, vf_id, true);
1863 : :
1864 : : /* All the vfid is zero, so need to delete this entry */
1865 : 0 : ret = hns3_remove_mac_vlan_tbl(hw, &req);
1866 [ # # ]: 0 : } else if (ret == -ENOENT) {
1867 : : /* This mac addr doesn't exist. */
1868 : : return 0;
1869 : : }
1870 : :
1871 [ # # ]: 0 : if (ret) {
1872 : 0 : hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1873 : : mac_addr);
1874 : 0 : hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1875 : : }
1876 : :
1877 : : return ret;
1878 : : }
1879 : :
1880 : : static int
1881 : 0 : hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
1882 : : enum hns3_ring_type queue_type, uint16_t queue_id)
1883 : : {
1884 : : struct hns3_cmd_desc desc;
1885 : : struct hns3_ctrl_vector_chain_cmd *req =
1886 : : (struct hns3_ctrl_vector_chain_cmd *)desc.data;
1887 : : enum hns3_opcode_type op;
1888 : : uint16_t tqp_type_and_id = 0;
1889 : : uint16_t type;
1890 : : uint16_t gl;
1891 : : int ret;
1892 : :
1893 [ # # ]: 0 : op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
1894 : 0 : hns3_cmd_setup_basic_desc(&desc, op, false);
1895 : 0 : req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
1896 : : HNS3_TQP_INT_ID_L_S);
1897 : 0 : req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
1898 : : HNS3_TQP_INT_ID_H_S);
1899 : :
1900 [ # # ]: 0 : if (queue_type == HNS3_RING_TYPE_RX)
1901 : : gl = HNS3_RING_GL_RX;
1902 : : else
1903 : : gl = HNS3_RING_GL_TX;
1904 : :
1905 : 0 : type = queue_type;
1906 : :
1907 : 0 : hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
1908 : : type);
1909 : 0 : hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
1910 : 0 : hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
1911 : : gl);
1912 : 0 : req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
1913 : 0 : req->int_cause_num = 1;
1914 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
1915 [ # # ]: 0 : if (ret) {
1916 [ # # ]: 0 : hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.",
1917 : : en ? "Map" : "Unmap", queue_id, vector_id, ret);
1918 : 0 : return ret;
1919 : : }
1920 : :
1921 : : return 0;
1922 : : }
1923 : :
1924 : : static int
1925 : 0 : hns3_setup_dcb(struct rte_eth_dev *dev)
1926 : : {
1927 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1928 : : struct hns3_hw *hw = &hns->hw;
1929 : : int ret;
1930 : :
1931 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB)) {
1932 : 0 : hns3_err(hw, "this port does not support dcb configurations.");
1933 : 0 : return -EOPNOTSUPP;
1934 : : }
1935 : :
1936 [ # # ]: 0 : if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
1937 : 0 : hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
1938 : 0 : return -EOPNOTSUPP;
1939 : : }
1940 : :
1941 : 0 : ret = hns3_dcb_configure(hns);
1942 [ # # ]: 0 : if (ret)
1943 : 0 : hns3_err(hw, "failed to config dcb: %d", ret);
1944 : :
1945 : : return ret;
1946 : : }
1947 : :
1948 : : static int
1949 : 0 : hns3_check_link_speed(struct hns3_hw *hw, uint32_t link_speeds)
1950 : : {
1951 : : int ret;
1952 : :
1953 : : /*
1954 : : * Some hardware doesn't support auto-negotiation, but users may not
1955 : : * configure link_speeds (default 0), which means auto-negotiation.
1956 : : * In this case, it should return success.
1957 : : */
1958 [ # # ]: 0 : if (link_speeds == RTE_ETH_LINK_SPEED_AUTONEG &&
1959 [ # # ]: 0 : hw->mac.support_autoneg == 0)
1960 : : return 0;
1961 : :
1962 [ # # ]: 0 : if (link_speeds != RTE_ETH_LINK_SPEED_AUTONEG) {
1963 : 0 : ret = hns3_check_port_speed(hw, link_speeds);
1964 [ # # ]: 0 : if (ret)
1965 : 0 : return ret;
1966 : : }
1967 : :
1968 : : return 0;
1969 : : }
1970 : :
1971 : : static int
1972 : 0 : hns3_check_dev_conf(struct rte_eth_dev *dev)
1973 : : {
1974 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1975 : : struct rte_eth_conf *conf = &dev->data->dev_conf;
1976 : : int ret;
1977 : :
1978 : 0 : ret = hns3_check_dev_mq_mode(dev);
1979 [ # # ]: 0 : if (ret)
1980 : : return ret;
1981 : :
1982 : 0 : return hns3_check_link_speed(hw, conf->link_speeds);
1983 : : }
1984 : :
1985 : : static int
1986 : 0 : hns3_dev_configure(struct rte_eth_dev *dev)
1987 : : {
1988 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1989 : 0 : struct rte_eth_conf *conf = &dev->data->dev_conf;
1990 : 0 : enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
1991 : 0 : struct hns3_hw *hw = &hns->hw;
1992 : 0 : uint16_t nb_rx_q = dev->data->nb_rx_queues;
1993 : 0 : uint16_t nb_tx_q = dev->data->nb_tx_queues;
1994 : : struct rte_eth_rss_conf rss_conf;
1995 : : bool gro_en;
1996 : : int ret;
1997 : :
1998 : 0 : hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
1999 : :
2000 : : /*
2001 : : * Some versions of hardware network engine does not support
2002 : : * individually enable/disable/reset the Tx or Rx queue. These devices
2003 : : * must enable/disable/reset Tx and Rx queues at the same time. When the
2004 : : * numbers of Tx queues allocated by upper applications are not equal to
2005 : : * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2006 : : * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2007 : : * work as usual. But these fake queues are imperceptible, and can not
2008 : : * be used by upper applications.
2009 : : */
2010 : 0 : ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2011 [ # # ]: 0 : if (ret) {
2012 : 0 : hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret);
2013 : 0 : hw->cfg_max_queues = 0;
2014 : 0 : return ret;
2015 : : }
2016 : :
2017 : 0 : hw->adapter_state = HNS3_NIC_CONFIGURING;
2018 : 0 : ret = hns3_check_dev_conf(dev);
2019 [ # # ]: 0 : if (ret)
2020 : 0 : goto cfg_err;
2021 : :
2022 [ # # ]: 0 : if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
2023 : 0 : ret = hns3_setup_dcb(dev);
2024 [ # # ]: 0 : if (ret)
2025 : 0 : goto cfg_err;
2026 : : }
2027 : :
2028 [ # # ]: 0 : if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
2029 : 0 : conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
2030 : 0 : rss_conf = conf->rx_adv_conf.rss_conf;
2031 : 0 : ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2032 [ # # ]: 0 : if (ret)
2033 : 0 : goto cfg_err;
2034 : : }
2035 : :
2036 : 0 : ret = hns3_dev_mtu_set(dev, conf->rxmode.mtu);
2037 [ # # ]: 0 : if (ret != 0)
2038 : 0 : goto cfg_err;
2039 : :
2040 : 0 : ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf);
2041 [ # # ]: 0 : if (ret)
2042 : 0 : goto cfg_err;
2043 : :
2044 : 0 : ret = hns3_dev_configure_vlan(dev);
2045 [ # # ]: 0 : if (ret)
2046 : 0 : goto cfg_err;
2047 : :
2048 : : /* config hardware GRO */
2049 : 0 : gro_en = conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false;
2050 : 0 : ret = hns3_config_gro(hw, gro_en);
2051 [ # # ]: 0 : if (ret)
2052 : 0 : goto cfg_err;
2053 : :
2054 : 0 : hns3_init_rx_ptype_tble(dev);
2055 : 0 : hw->adapter_state = HNS3_NIC_CONFIGURED;
2056 : :
2057 : 0 : return 0;
2058 : :
2059 : 0 : cfg_err:
2060 : 0 : hw->cfg_max_queues = 0;
2061 : 0 : (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2062 : 0 : hw->adapter_state = HNS3_NIC_INITIALIZED;
2063 : :
2064 : 0 : return ret;
2065 : : }
2066 : :
2067 : : static int
2068 : 0 : hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2069 : : {
2070 : : struct hns3_config_max_frm_size_cmd *req;
2071 : : struct hns3_cmd_desc desc;
2072 : :
2073 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2074 : :
2075 : : req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2076 : 0 : req->max_frm_size = rte_cpu_to_le_16(new_mps);
2077 : 0 : req->min_frm_size = RTE_ETHER_MIN_LEN;
2078 : :
2079 : 0 : return hns3_cmd_send(hw, &desc, 1);
2080 : : }
2081 : :
2082 : : static int
2083 : 0 : hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2084 : : {
2085 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2086 : : int err;
2087 : : int ret;
2088 : :
2089 : 0 : ret = hns3_set_mac_mtu(hw, mps);
2090 [ # # ]: 0 : if (ret) {
2091 : 0 : hns3_err(hw, "failed to set mtu, ret = %d", ret);
2092 : 0 : return ret;
2093 : : }
2094 : :
2095 : 0 : ret = hns3_buffer_alloc(hw);
2096 [ # # ]: 0 : if (ret) {
2097 : 0 : hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2098 : 0 : goto rollback;
2099 : : }
2100 : :
2101 : 0 : hns->pf.mps = mps;
2102 : :
2103 : 0 : return 0;
2104 : :
2105 : : rollback:
2106 : 0 : err = hns3_set_mac_mtu(hw, hns->pf.mps);
2107 [ # # ]: 0 : if (err)
2108 : 0 : hns3_err(hw, "fail to rollback MTU, err = %d", err);
2109 : :
2110 : : return ret;
2111 : : }
2112 : :
2113 : : static int
2114 : 0 : hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2115 : : {
2116 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
2117 : 0 : uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2118 : 0 : struct hns3_hw *hw = &hns->hw;
2119 : : int ret;
2120 : :
2121 [ # # ]: 0 : if (dev->data->dev_started) {
2122 : 0 : hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2123 : : "before configuration", dev->data->port_id);
2124 : 0 : return -EBUSY;
2125 : : }
2126 : :
2127 : 0 : rte_spinlock_lock(&hw->lock);
2128 : 0 : frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2129 : :
2130 : : /*
2131 : : * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2132 : : * assign to "uint16_t" type variable.
2133 : : */
2134 : 0 : ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2135 [ # # ]: 0 : if (ret) {
2136 : : rte_spinlock_unlock(&hw->lock);
2137 : 0 : hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2138 : : dev->data->port_id, mtu, ret);
2139 : 0 : return ret;
2140 : : }
2141 : :
2142 : : rte_spinlock_unlock(&hw->lock);
2143 : :
2144 : 0 : return 0;
2145 : : }
2146 : :
2147 : : static uint32_t
2148 : 0 : hns3_get_copper_port_speed_capa(uint32_t supported_speed)
2149 : : {
2150 : : uint32_t speed_capa = 0;
2151 : :
2152 [ # # ]: 0 : if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT)
2153 : : speed_capa |= RTE_ETH_LINK_SPEED_10M_HD;
2154 [ # # ]: 0 : if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT)
2155 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_10M;
2156 [ # # ]: 0 : if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT)
2157 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_100M_HD;
2158 [ # # ]: 0 : if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT)
2159 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_100M;
2160 [ # # ]: 0 : if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT)
2161 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_1G;
2162 : :
2163 : 0 : return speed_capa;
2164 : : }
2165 : :
2166 : : static uint32_t
2167 : 0 : hns3_get_firber_port_speed_capa(uint32_t supported_speed)
2168 : : {
2169 : : uint32_t speed_capa = 0;
2170 : :
2171 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT)
2172 : : speed_capa |= RTE_ETH_LINK_SPEED_1G;
2173 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT)
2174 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_10G;
2175 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT)
2176 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_25G;
2177 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT)
2178 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_40G;
2179 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT)
2180 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_50G;
2181 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT)
2182 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_100G;
2183 [ # # ]: 0 : if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT)
2184 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_200G;
2185 : :
2186 : 0 : return speed_capa;
2187 : : }
2188 : :
2189 : : uint32_t
2190 : 0 : hns3_get_speed_capa(struct hns3_hw *hw)
2191 : : {
2192 : : struct hns3_mac *mac = &hw->mac;
2193 : : uint32_t speed_capa;
2194 : :
2195 [ # # ]: 0 : if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
2196 : : speed_capa =
2197 : 0 : hns3_get_copper_port_speed_capa(mac->supported_speed);
2198 : : else
2199 : : speed_capa =
2200 : 0 : hns3_get_firber_port_speed_capa(mac->supported_speed);
2201 : :
2202 [ # # ]: 0 : if (mac->support_autoneg == 0)
2203 : 0 : speed_capa |= RTE_ETH_LINK_SPEED_FIXED;
2204 : :
2205 : 0 : return speed_capa;
2206 : : }
2207 : :
2208 : : static int
2209 : 0 : hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
2210 : : {
2211 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2212 : : int ret;
2213 : :
2214 : 0 : (void)hns3_update_link_status(hw);
2215 : :
2216 : 0 : ret = hns3_update_link_info(eth_dev);
2217 [ # # ]: 0 : if (ret)
2218 : 0 : hw->mac.link_status = RTE_ETH_LINK_DOWN;
2219 : :
2220 : 0 : return ret;
2221 : : }
2222 : :
2223 : : static void
2224 : 0 : hns3_setup_linkstatus(struct rte_eth_dev *eth_dev,
2225 : : struct rte_eth_link *new_link)
2226 : : {
2227 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2228 : : struct hns3_mac *mac = &hw->mac;
2229 : :
2230 [ # # ]: 0 : switch (mac->link_speed) {
2231 : 0 : case RTE_ETH_SPEED_NUM_10M:
2232 : : case RTE_ETH_SPEED_NUM_100M:
2233 : : case RTE_ETH_SPEED_NUM_1G:
2234 : : case RTE_ETH_SPEED_NUM_10G:
2235 : : case RTE_ETH_SPEED_NUM_25G:
2236 : : case RTE_ETH_SPEED_NUM_40G:
2237 : : case RTE_ETH_SPEED_NUM_50G:
2238 : : case RTE_ETH_SPEED_NUM_100G:
2239 : : case RTE_ETH_SPEED_NUM_200G:
2240 [ # # ]: 0 : if (mac->link_status)
2241 : 0 : new_link->link_speed = mac->link_speed;
2242 : : break;
2243 : 0 : default:
2244 [ # # ]: 0 : if (mac->link_status)
2245 : 0 : new_link->link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
2246 : : break;
2247 : : }
2248 : :
2249 [ # # ]: 0 : if (!mac->link_status)
2250 : 0 : new_link->link_speed = RTE_ETH_SPEED_NUM_NONE;
2251 : :
2252 : 0 : new_link->link_duplex = mac->link_duplex;
2253 : 0 : new_link->link_status = mac->link_status ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN;
2254 : 0 : new_link->link_autoneg = mac->link_autoneg;
2255 : 0 : }
2256 : :
2257 : : static int
2258 : 0 : hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
2259 : : {
2260 : : #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */
2261 : : #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */
2262 : :
2263 [ # # ]: 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2264 : : uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES;
2265 : : struct hns3_mac *mac = &hw->mac;
2266 : : struct rte_eth_link new_link;
2267 : : int ret;
2268 : :
2269 : : memset(&new_link, 0, sizeof(new_link));
2270 : : /* When port is stopped, report link down. */
2271 [ # # ]: 0 : if (eth_dev->data->dev_started == 0) {
2272 : 0 : new_link.link_autoneg = mac->link_autoneg;
2273 : 0 : new_link.link_duplex = mac->link_duplex;
2274 : : new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
2275 : : new_link.link_status = RTE_ETH_LINK_DOWN;
2276 : 0 : goto out;
2277 : : }
2278 : :
2279 : : do {
2280 : 0 : ret = hns3_update_port_link_info(eth_dev);
2281 [ # # ]: 0 : if (ret) {
2282 : 0 : hns3_err(hw, "failed to get port link info, ret = %d.",
2283 : : ret);
2284 : 0 : break;
2285 : : }
2286 : :
2287 [ # # # # ]: 0 : if (!wait_to_complete || mac->link_status == RTE_ETH_LINK_UP)
2288 : : break;
2289 : :
2290 : : rte_delay_ms(HNS3_LINK_CHECK_INTERVAL);
2291 [ # # ]: 0 : } while (retry_cnt--);
2292 : :
2293 : 0 : hns3_setup_linkstatus(eth_dev, &new_link);
2294 : :
2295 [ # # ]: 0 : out:
2296 : 0 : return rte_eth_linkstatus_set(eth_dev, &new_link);
2297 : : }
2298 : :
2299 : : static int
2300 : 0 : hns3_dev_set_link_up(struct rte_eth_dev *dev)
2301 : : {
2302 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2303 : : int ret;
2304 : :
2305 : : /*
2306 : : * The "tx_pkt_burst" will be restored. But the secondary process does
2307 : : * not support the mechanism for notifying the primary process.
2308 : : */
2309 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2310 : 0 : hns3_err(hw, "secondary process does not support to set link up.");
2311 : 0 : return -ENOTSUP;
2312 : : }
2313 : :
2314 : : /*
2315 : : * If device isn't started Rx/Tx function is still disabled, setting
2316 : : * link up is not allowed. But it is probably better to return success
2317 : : * to reduce the impact on the upper layer.
2318 : : */
2319 [ # # ]: 0 : if (hw->adapter_state != HNS3_NIC_STARTED) {
2320 : 0 : hns3_info(hw, "device isn't started, can't set link up.");
2321 : 0 : return 0;
2322 : : }
2323 : :
2324 [ # # ]: 0 : if (!hw->set_link_down)
2325 : : return 0;
2326 : :
2327 : 0 : rte_spinlock_lock(&hw->lock);
2328 : 0 : ret = hns3_cfg_mac_mode(hw, true);
2329 [ # # ]: 0 : if (ret) {
2330 : : rte_spinlock_unlock(&hw->lock);
2331 : 0 : hns3_err(hw, "failed to set link up, ret = %d", ret);
2332 : 0 : return ret;
2333 : : }
2334 : :
2335 : 0 : hw->set_link_down = false;
2336 : 0 : hns3_start_tx_datapath(dev);
2337 : : rte_spinlock_unlock(&hw->lock);
2338 : :
2339 : 0 : return 0;
2340 : : }
2341 : :
2342 : : static int
2343 : 0 : hns3_dev_set_link_down(struct rte_eth_dev *dev)
2344 : : {
2345 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2346 : : int ret;
2347 : :
2348 : : /*
2349 : : * The "tx_pkt_burst" will be set to dummy function. But the secondary
2350 : : * process does not support the mechanism for notifying the primary
2351 : : * process.
2352 : : */
2353 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2354 : 0 : hns3_err(hw, "secondary process does not support to set link down.");
2355 : 0 : return -ENOTSUP;
2356 : : }
2357 : :
2358 : : /*
2359 : : * If device isn't started or the API has been called, link status is
2360 : : * down, return success.
2361 : : */
2362 [ # # # # ]: 0 : if (hw->adapter_state != HNS3_NIC_STARTED || hw->set_link_down)
2363 : : return 0;
2364 : :
2365 : 0 : rte_spinlock_lock(&hw->lock);
2366 : 0 : hns3_stop_tx_datapath(dev);
2367 : 0 : ret = hns3_cfg_mac_mode(hw, false);
2368 [ # # ]: 0 : if (ret) {
2369 : 0 : hns3_start_tx_datapath(dev);
2370 : : rte_spinlock_unlock(&hw->lock);
2371 : 0 : hns3_err(hw, "failed to set link down, ret = %d", ret);
2372 : 0 : return ret;
2373 : : }
2374 : :
2375 : 0 : hw->set_link_down = true;
2376 : : rte_spinlock_unlock(&hw->lock);
2377 : :
2378 : 0 : return 0;
2379 : : }
2380 : :
2381 : : static int
2382 : : hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2383 : : {
2384 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2385 : : struct hns3_pf *pf = &hns->pf;
2386 : :
2387 [ # # ]: 0 : if (!(status->pf_state & HNS3_PF_STATE_DONE))
2388 : : return -EINVAL;
2389 : :
2390 : 0 : pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2391 : :
2392 : 0 : return 0;
2393 : : }
2394 : :
2395 : : static int
2396 : 0 : hns3_query_function_status(struct hns3_hw *hw)
2397 : : {
2398 : : #define HNS3_QUERY_MAX_CNT 10
2399 : : #define HNS3_QUERY_SLEEP_MSCOEND 1
2400 : : struct hns3_func_status_cmd *req;
2401 : : struct hns3_cmd_desc desc;
2402 : : int timeout = 0;
2403 : : int ret;
2404 : :
2405 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2406 : : req = (struct hns3_func_status_cmd *)desc.data;
2407 : :
2408 : : do {
2409 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
2410 [ # # ]: 0 : if (ret) {
2411 : 0 : PMD_INIT_LOG(ERR, "query function status failed %d",
2412 : : ret);
2413 : 0 : return ret;
2414 : : }
2415 : :
2416 : : /* Check pf reset is done */
2417 [ # # ]: 0 : if (req->pf_state)
2418 : : break;
2419 : :
2420 : : rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2421 [ # # ]: 0 : } while (timeout++ < HNS3_QUERY_MAX_CNT);
2422 : :
2423 : : return hns3_parse_func_status(hw, req);
2424 : : }
2425 : :
2426 : : static int
2427 : : hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2428 : : {
2429 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2430 : : struct hns3_pf *pf = &hns->pf;
2431 : :
2432 [ # # ]: 0 : if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2433 : : /*
2434 : : * The total_tqps_num obtained from firmware is maximum tqp
2435 : : * numbers of this port, which should be used for PF and VFs.
2436 : : * There is no need for pf to have so many tqp numbers in
2437 : : * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2438 : : * coming from config file, is assigned to maximum queue number
2439 : : * for the PF of this port by user. So users can modify the
2440 : : * maximum queue number of PF according to their own application
2441 : : * scenarios, which is more flexible to use. In addition, many
2442 : : * memories can be saved due to allocating queue statistics
2443 : : * room according to the actual number of queues required. The
2444 : : * maximum queue number of PF for network engine with
2445 : : * revision_id greater than 0x30 is assigned by config file.
2446 : : */
2447 : : if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2448 : : hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2449 : : "must be greater than 0.",
2450 : : RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2451 : : return -EINVAL;
2452 : : }
2453 : :
2454 : 0 : hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2455 : : hw->total_tqps_num);
2456 : : } else {
2457 : : /*
2458 : : * Due to the limitation on the number of PF interrupts
2459 : : * available, the maximum queue number assigned to PF on
2460 : : * the network engine with revision_id 0x21 is 64.
2461 : : */
2462 : 0 : hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2463 : : HNS3_MAX_TQP_NUM_HIP08_PF);
2464 : : }
2465 : :
2466 : : return 0;
2467 : : }
2468 : :
2469 : : static int
2470 : 0 : hns3_query_pf_resource(struct hns3_hw *hw)
2471 : : {
2472 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2473 : : struct hns3_pf *pf = &hns->pf;
2474 : : struct hns3_pf_res_cmd *req;
2475 : : struct hns3_cmd_desc desc;
2476 : : int ret;
2477 : :
2478 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2479 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
2480 [ # # ]: 0 : if (ret) {
2481 : 0 : PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2482 : 0 : return ret;
2483 : : }
2484 : :
2485 : : req = (struct hns3_pf_res_cmd *)desc.data;
2486 : 0 : hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2487 : 0 : rte_le_to_cpu_16(req->ext_tqp_num);
2488 [ # # ]: 0 : if (hw->total_tqps_num == 0) {
2489 : 0 : PMD_INIT_LOG(ERR, "the total tqp number of the port is 0.");
2490 : 0 : return -EINVAL;
2491 : : }
2492 : : ret = hns3_get_pf_max_tqp_num(hw);
2493 : : if (ret)
2494 : : return ret;
2495 : :
2496 : 0 : pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2497 : 0 : pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2498 : :
2499 [ # # ]: 0 : if (req->tx_buf_size)
2500 : 0 : pf->tx_buf_size =
2501 : 0 : rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2502 : : else
2503 : 0 : pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2504 : :
2505 : 0 : pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2506 : :
2507 [ # # ]: 0 : if (req->dv_buf_size)
2508 : 0 : pf->dv_buf_size =
2509 : 0 : rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2510 : : else
2511 : 0 : pf->dv_buf_size = HNS3_DEFAULT_DV;
2512 : :
2513 : 0 : pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2514 : :
2515 : 0 : hw->num_msi =
2516 : 0 : hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2517 : : HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2518 : :
2519 : 0 : return 0;
2520 : : }
2521 : :
2522 : : static void
2523 : 0 : hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2524 : : {
2525 : : struct hns3_cfg_param_cmd *req;
2526 : : uint64_t mac_addr_tmp_high;
2527 : : uint8_t ext_rss_size_max;
2528 : : uint64_t mac_addr_tmp;
2529 : : uint32_t i;
2530 : :
2531 : : req = (struct hns3_cfg_param_cmd *)desc[0].data;
2532 : :
2533 : : /* get the configuration */
2534 : 0 : cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2535 : : HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2536 : :
2537 : 0 : cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2538 : : HNS3_CFG_PHY_ADDR_M,
2539 : : HNS3_CFG_PHY_ADDR_S);
2540 : 0 : cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2541 : : HNS3_CFG_MEDIA_TP_M,
2542 : : HNS3_CFG_MEDIA_TP_S);
2543 : : /* get mac address */
2544 : 0 : mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2545 : 0 : mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2546 : : HNS3_CFG_MAC_ADDR_H_M,
2547 : : HNS3_CFG_MAC_ADDR_H_S);
2548 : :
2549 : 0 : mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2550 : :
2551 : 0 : cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2552 : : HNS3_CFG_DEFAULT_SPEED_M,
2553 : : HNS3_CFG_DEFAULT_SPEED_S);
2554 : 0 : cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2555 : : HNS3_CFG_RSS_SIZE_M,
2556 : : HNS3_CFG_RSS_SIZE_S);
2557 : :
2558 [ # # ]: 0 : for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2559 : 0 : cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2560 : :
2561 : : req = (struct hns3_cfg_param_cmd *)desc[1].data;
2562 : 0 : cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2563 : :
2564 : 0 : cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2565 : : HNS3_CFG_SPEED_ABILITY_M,
2566 : : HNS3_CFG_SPEED_ABILITY_S);
2567 : 0 : cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2568 : : HNS3_CFG_UMV_TBL_SPACE_M,
2569 : : HNS3_CFG_UMV_TBL_SPACE_S);
2570 : :
2571 : 0 : ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2572 : : HNS3_CFG_EXT_RSS_SIZE_M,
2573 : : HNS3_CFG_EXT_RSS_SIZE_S);
2574 : : /*
2575 : : * Field ext_rss_size_max obtained from firmware will be more flexible
2576 : : * for future changes and expansions, which is an exponent of 2, instead
2577 : : * of reading out directly. If this field is not zero, hns3 PF PMD
2578 : : * uses it as rss_size_max under one TC. Device, whose revision
2579 : : * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2580 : : * maximum number of queues supported under a TC through this field.
2581 : : */
2582 [ # # ]: 0 : if (ext_rss_size_max)
2583 : 0 : cfg->rss_size_max = 1U << ext_rss_size_max;
2584 : 0 : }
2585 : :
2586 : : /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2587 : : * @hw: pointer to struct hns3_hw
2588 : : * @hcfg: the config structure to be getted
2589 : : */
2590 : : static int
2591 : 0 : hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2592 : : {
2593 : : struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2594 : : struct hns3_cfg_param_cmd *req;
2595 : : uint32_t offset;
2596 : : uint32_t i;
2597 : : int ret;
2598 : :
2599 [ # # ]: 0 : for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2600 : : offset = 0;
2601 : 0 : req = (struct hns3_cfg_param_cmd *)desc[i].data;
2602 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2603 : : true);
2604 : 0 : hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2605 : : i * HNS3_CFG_RD_LEN_BYTES);
2606 : : /* Len should be divided by 4 when send to hardware */
2607 : 0 : hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2608 : : HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2609 : 0 : req->offset = rte_cpu_to_le_32(offset);
2610 : : }
2611 : :
2612 : 0 : ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2613 [ # # ]: 0 : if (ret) {
2614 : 0 : PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2615 : 0 : return ret;
2616 : : }
2617 : :
2618 : 0 : hns3_parse_cfg(hcfg, desc);
2619 : :
2620 : 0 : return 0;
2621 : : }
2622 : :
2623 : : static int
2624 : 0 : hns3_parse_speed(int speed_cmd, uint32_t *speed)
2625 : : {
2626 [ # # # # : 0 : switch (speed_cmd) {
# # # # #
# ]
2627 : 0 : case HNS3_CFG_SPEED_10M:
2628 : 0 : *speed = RTE_ETH_SPEED_NUM_10M;
2629 : 0 : break;
2630 : 0 : case HNS3_CFG_SPEED_100M:
2631 : 0 : *speed = RTE_ETH_SPEED_NUM_100M;
2632 : 0 : break;
2633 : 0 : case HNS3_CFG_SPEED_1G:
2634 : 0 : *speed = RTE_ETH_SPEED_NUM_1G;
2635 : 0 : break;
2636 : 0 : case HNS3_CFG_SPEED_10G:
2637 : 0 : *speed = RTE_ETH_SPEED_NUM_10G;
2638 : 0 : break;
2639 : 0 : case HNS3_CFG_SPEED_25G:
2640 : 0 : *speed = RTE_ETH_SPEED_NUM_25G;
2641 : 0 : break;
2642 : 0 : case HNS3_CFG_SPEED_40G:
2643 : 0 : *speed = RTE_ETH_SPEED_NUM_40G;
2644 : 0 : break;
2645 : 0 : case HNS3_CFG_SPEED_50G:
2646 : 0 : *speed = RTE_ETH_SPEED_NUM_50G;
2647 : 0 : break;
2648 : 0 : case HNS3_CFG_SPEED_100G:
2649 : 0 : *speed = RTE_ETH_SPEED_NUM_100G;
2650 : 0 : break;
2651 : 0 : case HNS3_CFG_SPEED_200G:
2652 : 0 : *speed = RTE_ETH_SPEED_NUM_200G;
2653 : 0 : break;
2654 : : default:
2655 : : return -EINVAL;
2656 : : }
2657 : :
2658 : : return 0;
2659 : : }
2660 : :
2661 : : static int
2662 : 0 : hns3_get_capability(struct hns3_hw *hw)
2663 : : {
2664 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2665 : : struct hns3_pf *pf = &hns->pf;
2666 : : int ret;
2667 : :
2668 : 0 : ret = hns3_query_mac_stats_reg_num(hw);
2669 [ # # ]: 0 : if (ret)
2670 : : return ret;
2671 : :
2672 [ # # ]: 0 : if (hw->revision < PCI_REVISION_ID_HIP09_A) {
2673 : 0 : hns3_set_default_dev_specifications(hw);
2674 : 0 : hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
2675 : 0 : hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
2676 : 0 : hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
2677 : 0 : hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
2678 : 0 : hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
2679 : 0 : hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
2680 : 0 : pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
2681 : 0 : hw->rss_info.ipv6_sctp_offload_supported = false;
2682 : 0 : hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE;
2683 : 0 : pf->support_multi_tc_pause = false;
2684 : 0 : hw->rx_dma_addr_align = HNS3_RX_DMA_ADDR_ALIGN_64;
2685 : 0 : hw->strip_crc_ptype = HNS3_STRIP_CRC_PTYPE_TCP;
2686 : 0 : return 0;
2687 : : }
2688 : :
2689 : 0 : ret = hns3_query_dev_specifications(hw);
2690 [ # # ]: 0 : if (ret) {
2691 : 0 : PMD_INIT_LOG(ERR,
2692 : : "failed to query dev specifications, ret = %d",
2693 : : ret);
2694 : 0 : return ret;
2695 : : }
2696 : :
2697 : 0 : hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
2698 : 0 : hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
2699 : 0 : hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
2700 : 0 : hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
2701 : 0 : hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
2702 : 0 : pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
2703 : 0 : hw->rss_info.ipv6_sctp_offload_supported = true;
2704 : 0 : hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE;
2705 : 0 : pf->support_multi_tc_pause = true;
2706 : 0 : hw->rx_dma_addr_align = HNS3_RX_DMA_ADDR_ALIGN_128;
2707 : 0 : hw->strip_crc_ptype = HNS3_STRIP_CRC_PTYPE_IP;
2708 : :
2709 : 0 : return 0;
2710 : : }
2711 : :
2712 : : static int
2713 : 0 : hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
2714 : : {
2715 : : int ret;
2716 : :
2717 [ # # # ]: 0 : switch (media_type) {
2718 : 0 : case HNS3_MEDIA_TYPE_COPPER:
2719 [ # # ]: 0 : if (!hns3_dev_get_support(hw, COPPER)) {
2720 : 0 : PMD_INIT_LOG(ERR,
2721 : : "Media type is copper, not supported.");
2722 : : ret = -EOPNOTSUPP;
2723 : : } else {
2724 : : ret = 0;
2725 : : }
2726 : : break;
2727 : : case HNS3_MEDIA_TYPE_FIBER:
2728 : : case HNS3_MEDIA_TYPE_BACKPLANE:
2729 : : ret = 0;
2730 : : break;
2731 : 0 : default:
2732 : 0 : PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
2733 : : ret = -EINVAL;
2734 : 0 : break;
2735 : : }
2736 : :
2737 : 0 : return ret;
2738 : : }
2739 : :
2740 : : static int
2741 : 0 : hns3_get_board_configuration(struct hns3_hw *hw)
2742 : : {
2743 : : #define HNS3_RSS_SIZE_MAX_DEFAULT 64
2744 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2745 : : struct hns3_pf *pf = &hns->pf;
2746 : : struct hns3_cfg cfg;
2747 : : int ret;
2748 : :
2749 : : memset(&cfg, 0, sizeof(cfg));
2750 : 0 : ret = hns3_get_board_cfg(hw, &cfg);
2751 [ # # ]: 0 : if (ret) {
2752 : 0 : PMD_INIT_LOG(ERR, "get board config failed %d", ret);
2753 : 0 : return ret;
2754 : : }
2755 : :
2756 : 0 : ret = hns3_check_media_type(hw, cfg.media_type);
2757 [ # # ]: 0 : if (ret)
2758 : : return ret;
2759 : :
2760 : 0 : hw->mac.media_type = cfg.media_type;
2761 : 0 : hw->rss_size_max = cfg.rss_size_max;
2762 [ # # ]: 0 : if (hw->rss_size_max == 0) {
2763 : 0 : PMD_INIT_LOG(WARNING, "rss_size_max is 0, already adjust to %u.",
2764 : : HNS3_RSS_SIZE_MAX_DEFAULT);
2765 : 0 : hw->rss_size_max = HNS3_RSS_SIZE_MAX_DEFAULT;
2766 : : }
2767 : 0 : memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
2768 : 0 : hw->mac.phy_addr = cfg.phy_addr;
2769 : 0 : hw->dcb_info.num_pg = 1;
2770 : 0 : hw->dcb_info.hw_pfc_map = 0;
2771 : :
2772 : 0 : ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
2773 [ # # ]: 0 : if (ret) {
2774 : 0 : PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
2775 : : cfg.default_speed, ret);
2776 : 0 : return ret;
2777 : : }
2778 : :
2779 : 0 : hw->dcb_info.tc_max = cfg.tc_num;
2780 [ # # ]: 0 : if (hw->dcb_info.tc_max > HNS3_MAX_TC_NUM || hw->dcb_info.tc_max < 1) {
2781 : 0 : PMD_INIT_LOG(WARNING,
2782 : : "Get TC num(%u) from flash, set TC num to 1",
2783 : : hw->dcb_info.tc_max);
2784 : 0 : hw->dcb_info.tc_max = 1;
2785 : : }
2786 : :
2787 : : /* Dev does not support DCB */
2788 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB)) {
2789 : 0 : hw->dcb_info.tc_max = 1;
2790 : 0 : hw->dcb_info.pfc_max = 0;
2791 : : } else
2792 : 0 : hw->dcb_info.pfc_max = hw->dcb_info.tc_max;
2793 : :
2794 : 0 : hw->dcb_info.num_tc = 1;
2795 : 0 : hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
2796 : : hw->tqps_num / hw->dcb_info.num_tc);
2797 : 0 : hns3_set_bit(hw->dcb_info.hw_tc_map, 0, 1);
2798 : 0 : pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
2799 : :
2800 : 0 : pf->cfg_umv_size = cfg.umv_space;
2801 : :
2802 : 0 : return ret;
2803 : : }
2804 : :
2805 : : static int
2806 : 0 : hns3_get_configuration(struct hns3_hw *hw)
2807 : : {
2808 : : int ret;
2809 : :
2810 : 0 : ret = hns3_query_function_status(hw);
2811 [ # # ]: 0 : if (ret) {
2812 : 0 : PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
2813 : 0 : return ret;
2814 : : }
2815 : :
2816 : : /* Get device capability */
2817 : 0 : ret = hns3_get_capability(hw);
2818 [ # # ]: 0 : if (ret) {
2819 : 0 : PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
2820 : 0 : return ret;
2821 : : }
2822 : :
2823 : : /* Get pf resource */
2824 : 0 : ret = hns3_query_pf_resource(hw);
2825 [ # # ]: 0 : if (ret) {
2826 : 0 : PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
2827 : 0 : return ret;
2828 : : }
2829 : :
2830 : 0 : ret = hns3_get_board_configuration(hw);
2831 [ # # ]: 0 : if (ret) {
2832 : 0 : PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
2833 : 0 : return ret;
2834 : : }
2835 : :
2836 : 0 : ret = hns3_query_dev_fec_info(hw);
2837 [ # # ]: 0 : if (ret)
2838 : 0 : PMD_INIT_LOG(ERR,
2839 : : "failed to query FEC information, ret = %d", ret);
2840 : :
2841 : : return ret;
2842 : : }
2843 : :
2844 : : static int
2845 : 0 : hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
2846 : : uint16_t tqp_vid, bool is_pf)
2847 : : {
2848 : : struct hns3_tqp_map_cmd *req;
2849 : : struct hns3_cmd_desc desc;
2850 : : int ret;
2851 : :
2852 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
2853 : :
2854 : : req = (struct hns3_tqp_map_cmd *)desc.data;
2855 : 0 : req->tqp_id = rte_cpu_to_le_16(tqp_pid);
2856 : 0 : req->tqp_vf = func_id;
2857 : 0 : req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
2858 [ # # ]: 0 : if (!is_pf)
2859 : 0 : req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
2860 : 0 : req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
2861 : :
2862 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
2863 [ # # ]: 0 : if (ret)
2864 : 0 : PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
2865 : :
2866 : 0 : return ret;
2867 : : }
2868 : :
2869 : : static int
2870 : 0 : hns3_map_tqp(struct hns3_hw *hw)
2871 : : {
2872 : : uint16_t i;
2873 : : int ret;
2874 : :
2875 : : /*
2876 : : * In current version, VF is not supported when PF is driven by DPDK
2877 : : * driver, so we assign total tqps_num tqps allocated to this port
2878 : : * to PF.
2879 : : */
2880 [ # # ]: 0 : for (i = 0; i < hw->total_tqps_num; i++) {
2881 : 0 : ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
2882 [ # # ]: 0 : if (ret)
2883 : 0 : return ret;
2884 : : }
2885 : :
2886 : : return 0;
2887 : : }
2888 : :
2889 : : static int
2890 : 0 : hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
2891 : : {
2892 : : struct hns3_config_mac_speed_dup_cmd *req;
2893 : : struct hns3_cmd_desc desc;
2894 : : int ret;
2895 : :
2896 : : req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
2897 : :
2898 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
2899 : :
2900 [ # # ]: 0 : hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
2901 : :
2902 [ # # # # : 0 : switch (speed) {
# # # # #
# ]
2903 : 0 : case RTE_ETH_SPEED_NUM_10M:
2904 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2905 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
2906 : 0 : break;
2907 : 0 : case RTE_ETH_SPEED_NUM_100M:
2908 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2909 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
2910 : 0 : break;
2911 : 0 : case RTE_ETH_SPEED_NUM_1G:
2912 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2913 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
2914 : 0 : break;
2915 : 0 : case RTE_ETH_SPEED_NUM_10G:
2916 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2917 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
2918 : 0 : break;
2919 : 0 : case RTE_ETH_SPEED_NUM_25G:
2920 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2921 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
2922 : 0 : break;
2923 : 0 : case RTE_ETH_SPEED_NUM_40G:
2924 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2925 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
2926 : 0 : break;
2927 : 0 : case RTE_ETH_SPEED_NUM_50G:
2928 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2929 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
2930 : 0 : break;
2931 : 0 : case RTE_ETH_SPEED_NUM_100G:
2932 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2933 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
2934 : 0 : break;
2935 : 0 : case RTE_ETH_SPEED_NUM_200G:
2936 : 0 : hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2937 : : HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
2938 : 0 : break;
2939 : 0 : default:
2940 : 0 : PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
2941 : 0 : return -EINVAL;
2942 : : }
2943 : :
2944 : 0 : hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
2945 : :
2946 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
2947 [ # # ]: 0 : if (ret)
2948 : 0 : PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
2949 : :
2950 : : return ret;
2951 : : }
2952 : :
2953 : : static int
2954 : : hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2955 : : {
2956 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2957 : : struct hns3_pf *pf = &hns->pf;
2958 : : struct hns3_priv_buf *priv;
2959 : : uint32_t i, total_size;
2960 : :
2961 : 0 : total_size = pf->pkt_buf_size;
2962 : :
2963 : : /* alloc tx buffer for all enabled tc */
2964 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2965 : : priv = &buf_alloc->priv_buf[i];
2966 : :
2967 [ # # ]: 0 : if (hw->dcb_info.hw_tc_map & BIT(i)) {
2968 [ # # ]: 0 : if (total_size < pf->tx_buf_size)
2969 : : return -ENOMEM;
2970 : :
2971 : 0 : priv->tx_buf_size = pf->tx_buf_size;
2972 : : } else
2973 : 0 : priv->tx_buf_size = 0;
2974 : :
2975 : 0 : total_size -= priv->tx_buf_size;
2976 : : }
2977 : :
2978 : : return 0;
2979 : : }
2980 : :
2981 : : static int
2982 : 0 : hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2983 : : {
2984 : : /* TX buffer size is unit by 128 byte */
2985 : : #define HNS3_BUF_SIZE_UNIT_SHIFT 7
2986 : : #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
2987 : : struct hns3_tx_buff_alloc_cmd *req;
2988 : : struct hns3_cmd_desc desc;
2989 : : uint32_t buf_size;
2990 : : uint32_t i;
2991 : : int ret;
2992 : :
2993 : : req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
2994 : :
2995 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
2996 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2997 : 0 : buf_size = buf_alloc->priv_buf[i].tx_buf_size;
2998 : :
2999 : 0 : buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3000 : 0 : req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3001 : : HNS3_BUF_SIZE_UPDATE_EN_MSK);
3002 : : }
3003 : :
3004 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3005 [ # # ]: 0 : if (ret)
3006 : 0 : PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3007 : :
3008 : 0 : return ret;
3009 : : }
3010 : :
3011 : : static int
3012 : : hns3_get_tc_num(struct hns3_hw *hw)
3013 : : {
3014 : : int cnt = 0;
3015 : : uint8_t i;
3016 : :
3017 [ # # # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3018 [ # # # # ]: 0 : if (hw->dcb_info.hw_tc_map & BIT(i))
3019 : 0 : cnt++;
3020 : : return cnt;
3021 : : }
3022 : :
3023 : : static uint32_t
3024 : : hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3025 : : {
3026 : : struct hns3_priv_buf *priv;
3027 : : uint32_t rx_priv = 0;
3028 : : int i;
3029 : :
3030 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3031 : : priv = &buf_alloc->priv_buf[i];
3032 [ # # ]: 0 : if (priv->enable)
3033 : 0 : rx_priv += priv->buf_size;
3034 : : }
3035 : : return rx_priv;
3036 : : }
3037 : :
3038 : : static uint32_t
3039 : : hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3040 : : {
3041 : : uint32_t total_tx_size = 0;
3042 : : uint32_t i;
3043 : :
3044 [ # # # # : 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++)
# # # # #
# ]
3045 : 0 : total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3046 : :
3047 : : return total_tx_size;
3048 : : }
3049 : :
3050 : : /* Get the number of pfc enabled TCs, which have private buffer */
3051 : : static int
3052 : : hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3053 : : {
3054 : : struct hns3_priv_buf *priv;
3055 : : int cnt = 0;
3056 : : uint8_t i;
3057 : :
3058 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3059 : 0 : priv = &buf_alloc->priv_buf[i];
3060 [ # # # # ]: 0 : if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3061 : 0 : cnt++;
3062 : : }
3063 : :
3064 : : return cnt;
3065 : : }
3066 : :
3067 : : /* Get the number of pfc disabled TCs, which have private buffer */
3068 : : static int
3069 : : hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3070 : : struct hns3_pkt_buf_alloc *buf_alloc)
3071 : : {
3072 : : struct hns3_priv_buf *priv;
3073 : : int cnt = 0;
3074 : : uint8_t i;
3075 : :
3076 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3077 : 0 : priv = &buf_alloc->priv_buf[i];
3078 [ # # ]: 0 : if (hw->dcb_info.hw_tc_map & BIT(i) &&
3079 [ # # # # ]: 0 : !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3080 : 0 : cnt++;
3081 : : }
3082 : :
3083 : : return cnt;
3084 : : }
3085 : :
3086 : : static bool
3087 : 0 : hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3088 : : uint32_t rx_all)
3089 : : {
3090 : : uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3091 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3092 : : struct hns3_pf *pf = &hns->pf;
3093 : : uint32_t shared_buf, aligned_mps;
3094 : : uint32_t rx_priv;
3095 : : uint8_t tc_num;
3096 : : uint8_t i;
3097 : :
3098 : 0 : tc_num = hns3_get_tc_num(hw);
3099 : 0 : aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3100 : :
3101 [ # # ]: 0 : if (hns3_dev_get_support(hw, DCB))
3102 : 0 : shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3103 : 0 : pf->dv_buf_size;
3104 : : else
3105 : 0 : shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3106 : 0 : + pf->dv_buf_size;
3107 : :
3108 : 0 : shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3109 : 0 : shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3110 : : HNS3_BUF_SIZE_UNIT);
3111 : :
3112 : : rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3113 [ # # ]: 0 : if (rx_all < rx_priv + shared_std)
3114 : : return false;
3115 : :
3116 : 0 : shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3117 : 0 : buf_alloc->s_buf.buf_size = shared_buf;
3118 [ # # ]: 0 : if (hns3_dev_get_support(hw, DCB)) {
3119 : 0 : buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3120 : 0 : buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3121 : 0 : - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3122 : : HNS3_BUF_SIZE_UNIT);
3123 : : } else {
3124 : 0 : buf_alloc->s_buf.self.high =
3125 : 0 : aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3126 : 0 : buf_alloc->s_buf.self.low = aligned_mps;
3127 : : }
3128 : :
3129 [ # # ]: 0 : if (hns3_dev_get_support(hw, DCB)) {
3130 : 0 : hi_thrd = shared_buf - pf->dv_buf_size;
3131 : :
3132 [ # # ]: 0 : if (tc_num <= NEED_RESERVE_TC_NUM)
3133 : 0 : hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3134 : : BUF_MAX_PERCENT;
3135 : :
3136 [ # # ]: 0 : if (tc_num)
3137 : 0 : hi_thrd = hi_thrd / tc_num;
3138 : :
3139 : 0 : hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3140 : 0 : hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3141 : 0 : lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3142 : : } else {
3143 : 0 : hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3144 : : lo_thrd = aligned_mps;
3145 : : }
3146 : :
3147 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3148 : 0 : buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3149 : 0 : buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3150 : : }
3151 : :
3152 : : return true;
3153 : : }
3154 : :
3155 : : static bool
3156 : 0 : hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3157 : : struct hns3_pkt_buf_alloc *buf_alloc)
3158 : : {
3159 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3160 : : struct hns3_pf *pf = &hns->pf;
3161 : : struct hns3_priv_buf *priv;
3162 : : uint32_t aligned_mps;
3163 : : uint32_t rx_all;
3164 : : uint8_t i;
3165 : :
3166 : 0 : rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3167 : 0 : aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3168 : :
3169 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3170 : 0 : priv = &buf_alloc->priv_buf[i];
3171 : :
3172 : 0 : priv->enable = 0;
3173 : 0 : priv->wl.low = 0;
3174 : 0 : priv->wl.high = 0;
3175 : 0 : priv->buf_size = 0;
3176 : :
3177 [ # # ]: 0 : if (!(hw->dcb_info.hw_tc_map & BIT(i)))
3178 : 0 : continue;
3179 : :
3180 : 0 : priv->enable = 1;
3181 [ # # ]: 0 : if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3182 [ # # ]: 0 : priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3183 : 0 : priv->wl.high = roundup(priv->wl.low + aligned_mps,
3184 : : HNS3_BUF_SIZE_UNIT);
3185 : : } else {
3186 : : priv->wl.low = 0;
3187 [ # # ]: 0 : priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3188 : : aligned_mps;
3189 : : }
3190 : :
3191 : 0 : priv->buf_size = priv->wl.high + pf->dv_buf_size;
3192 : : }
3193 : :
3194 : 0 : return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3195 : : }
3196 : :
3197 : : static bool
3198 : 0 : hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3199 : : struct hns3_pkt_buf_alloc *buf_alloc)
3200 : : {
3201 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3202 : : struct hns3_pf *pf = &hns->pf;
3203 : : struct hns3_priv_buf *priv;
3204 : : int no_pfc_priv_num;
3205 : : uint32_t rx_all;
3206 : : uint8_t mask;
3207 : : int i;
3208 : :
3209 : 0 : rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3210 : : no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3211 : :
3212 : : /* let the last to be cleared first */
3213 [ # # ]: 0 : for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3214 : : priv = &buf_alloc->priv_buf[i];
3215 : 0 : mask = BIT((uint8_t)i);
3216 [ # # ]: 0 : if (hw->dcb_info.hw_tc_map & mask &&
3217 [ # # ]: 0 : !(hw->dcb_info.hw_pfc_map & mask)) {
3218 : : /* Clear the no pfc TC private buffer */
3219 : 0 : priv->wl.low = 0;
3220 : 0 : priv->wl.high = 0;
3221 : 0 : priv->buf_size = 0;
3222 : 0 : priv->enable = 0;
3223 : 0 : no_pfc_priv_num--;
3224 : : }
3225 : :
3226 [ # # # # ]: 0 : if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3227 : : no_pfc_priv_num == 0)
3228 : : break;
3229 : : }
3230 : :
3231 : 0 : return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3232 : : }
3233 : :
3234 : : static bool
3235 : 0 : hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3236 : : struct hns3_pkt_buf_alloc *buf_alloc)
3237 : : {
3238 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3239 : : struct hns3_pf *pf = &hns->pf;
3240 : : struct hns3_priv_buf *priv;
3241 : : uint32_t rx_all;
3242 : : int pfc_priv_num;
3243 : : uint8_t mask;
3244 : : int i;
3245 : :
3246 : 0 : rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3247 : : pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3248 : :
3249 : : /* let the last to be cleared first */
3250 [ # # ]: 0 : for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3251 : : priv = &buf_alloc->priv_buf[i];
3252 : 0 : mask = BIT((uint8_t)i);
3253 [ # # # # ]: 0 : if (hw->dcb_info.hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3254 : : /* Reduce the number of pfc TC with private buffer */
3255 : 0 : priv->wl.low = 0;
3256 : 0 : priv->enable = 0;
3257 : 0 : priv->wl.high = 0;
3258 : 0 : priv->buf_size = 0;
3259 : 0 : pfc_priv_num--;
3260 : : }
3261 [ # # # # ]: 0 : if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3262 : : pfc_priv_num == 0)
3263 : : break;
3264 : : }
3265 : :
3266 : 0 : return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3267 : : }
3268 : :
3269 : : static bool
3270 : 0 : hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3271 : : struct hns3_pkt_buf_alloc *buf_alloc)
3272 : : {
3273 : : #define COMPENSATE_BUFFER 0x3C00
3274 : : #define COMPENSATE_HALF_MPS_NUM 5
3275 : : #define PRIV_WL_GAP 0x1800
3276 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3277 : : struct hns3_pf *pf = &hns->pf;
3278 : 0 : uint32_t tc_num = hns3_get_tc_num(hw);
3279 : 0 : uint32_t half_mps = pf->mps >> 1;
3280 : : struct hns3_priv_buf *priv;
3281 : : uint32_t min_rx_priv;
3282 : : uint32_t rx_priv;
3283 : : uint8_t i;
3284 : :
3285 : 0 : rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3286 [ # # ]: 0 : if (tc_num)
3287 : 0 : rx_priv = rx_priv / tc_num;
3288 : :
3289 [ # # ]: 0 : if (tc_num <= NEED_RESERVE_TC_NUM)
3290 : 0 : rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3291 : :
3292 : : /*
3293 : : * Minimum value of private buffer in rx direction (min_rx_priv) is
3294 : : * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3295 : : * buffer if rx_priv is greater than min_rx_priv.
3296 : : */
3297 : 0 : min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3298 : 0 : COMPENSATE_HALF_MPS_NUM * half_mps;
3299 : 0 : min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3300 : 0 : rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3301 [ # # ]: 0 : if (rx_priv < min_rx_priv)
3302 : : return false;
3303 : :
3304 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3305 : 0 : priv = &buf_alloc->priv_buf[i];
3306 : 0 : priv->enable = 0;
3307 : 0 : priv->wl.low = 0;
3308 : 0 : priv->wl.high = 0;
3309 : 0 : priv->buf_size = 0;
3310 : :
3311 [ # # ]: 0 : if (!(hw->dcb_info.hw_tc_map & BIT(i)))
3312 : 0 : continue;
3313 : :
3314 : 0 : priv->enable = 1;
3315 : 0 : priv->buf_size = rx_priv;
3316 : 0 : priv->wl.high = rx_priv - pf->dv_buf_size;
3317 : 0 : priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3318 : : }
3319 : :
3320 : 0 : buf_alloc->s_buf.buf_size = 0;
3321 : :
3322 : 0 : return true;
3323 : : }
3324 : :
3325 : : /*
3326 : : * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3327 : : * @hw: pointer to struct hns3_hw
3328 : : * @buf_alloc: pointer to buffer calculation data
3329 : : * @return: 0: calculate successful, negative: fail
3330 : : */
3331 : : static int
3332 : 0 : hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3333 : : {
3334 : : /* When DCB is not supported, rx private buffer is not allocated. */
3335 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB)) {
3336 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3337 : : struct hns3_pf *pf = &hns->pf;
3338 : 0 : uint32_t rx_all = pf->pkt_buf_size;
3339 : :
3340 : 0 : rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3341 [ # # ]: 0 : if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3342 : : return -ENOMEM;
3343 : :
3344 : 0 : return 0;
3345 : : }
3346 : :
3347 : : /*
3348 : : * Try to allocate privated packet buffer for all TCs without share
3349 : : * buffer.
3350 : : */
3351 [ # # ]: 0 : if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3352 : : return 0;
3353 : :
3354 : : /*
3355 : : * Try to allocate privated packet buffer for all TCs with share
3356 : : * buffer.
3357 : : */
3358 [ # # ]: 0 : if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3359 : : return 0;
3360 : :
3361 : : /*
3362 : : * For different application scenes, the enabled port number, TC number
3363 : : * and no_drop TC number are different. In order to obtain the better
3364 : : * performance, software could allocate the buffer size and configure
3365 : : * the waterline by trying to decrease the private buffer size according
3366 : : * to the order, namely, waterline of valid tc, pfc disabled tc, pfc
3367 : : * enabled tc.
3368 : : */
3369 [ # # ]: 0 : if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3370 : : return 0;
3371 : :
3372 [ # # ]: 0 : if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3373 : : return 0;
3374 : :
3375 [ # # ]: 0 : if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3376 : 0 : return 0;
3377 : :
3378 : : return -ENOMEM;
3379 : : }
3380 : :
3381 : : static int
3382 : 0 : hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3383 : : {
3384 : : struct hns3_rx_priv_buff_cmd *req;
3385 : : struct hns3_cmd_desc desc;
3386 : : uint32_t buf_size;
3387 : : int ret;
3388 : : int i;
3389 : :
3390 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3391 : : req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3392 : :
3393 : : /* Alloc private buffer TCs */
3394 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3395 : : struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3396 : :
3397 : 0 : req->buf_num[i] =
3398 : 0 : rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3399 : 0 : req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3400 : : }
3401 : :
3402 : 0 : buf_size = buf_alloc->s_buf.buf_size;
3403 : 0 : req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3404 : : (1 << HNS3_TC0_PRI_BUF_EN_B));
3405 : :
3406 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3407 [ # # ]: 0 : if (ret)
3408 : 0 : PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3409 : :
3410 : 0 : return ret;
3411 : : }
3412 : :
3413 : : static int
3414 : 0 : hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3415 : : {
3416 : : #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3417 : : struct hns3_rx_priv_wl_buf *req;
3418 : : struct hns3_priv_buf *priv;
3419 : : struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3420 : : int i, j;
3421 : : int ret;
3422 : :
3423 [ # # ]: 0 : for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3424 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3425 : : false);
3426 : 0 : req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3427 : :
3428 : : /* The first descriptor set the NEXT bit to 1 */
3429 [ # # ]: 0 : if (i == 0)
3430 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3431 : : else
3432 : 0 : desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3433 : :
3434 [ # # ]: 0 : for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3435 : 0 : uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3436 : :
3437 : : priv = &buf_alloc->priv_buf[idx];
3438 : 0 : req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3439 : : HNS3_BUF_UNIT_S);
3440 : 0 : req->tc_wl[j].high |=
3441 : : rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3442 : 0 : req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3443 : : HNS3_BUF_UNIT_S);
3444 : 0 : req->tc_wl[j].low |=
3445 : : rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3446 : : }
3447 : : }
3448 : :
3449 : : /* Send 2 descriptor at one time */
3450 : 0 : ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3451 [ # # ]: 0 : if (ret)
3452 : 0 : PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3453 : : ret);
3454 : 0 : return ret;
3455 : : }
3456 : :
3457 : : static int
3458 : 0 : hns3_common_thrd_config(struct hns3_hw *hw,
3459 : : struct hns3_pkt_buf_alloc *buf_alloc)
3460 : : {
3461 : : #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3462 : : struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3463 : : struct hns3_rx_com_thrd *req;
3464 : : struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3465 : : struct hns3_tc_thrd *tc;
3466 : : int tc_idx;
3467 : : int i, j;
3468 : : int ret;
3469 : :
3470 [ # # ]: 0 : for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3471 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3472 : : false);
3473 : 0 : req = (struct hns3_rx_com_thrd *)&desc[i].data;
3474 : :
3475 : : /* The first descriptor set the NEXT bit to 1 */
3476 [ # # ]: 0 : if (i == 0)
3477 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3478 : : else
3479 : 0 : desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3480 : :
3481 [ # # ]: 0 : for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3482 : 0 : tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3483 : : tc = &s_buf->tc_thrd[tc_idx];
3484 : :
3485 : 0 : req->com_thrd[j].high =
3486 : 0 : rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3487 : 0 : req->com_thrd[j].high |=
3488 : : rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3489 : 0 : req->com_thrd[j].low =
3490 : 0 : rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3491 : 0 : req->com_thrd[j].low |=
3492 : : rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3493 : : }
3494 : : }
3495 : :
3496 : : /* Send 2 descriptors at one time */
3497 : 0 : ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3498 [ # # ]: 0 : if (ret)
3499 : 0 : PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3500 : :
3501 : 0 : return ret;
3502 : : }
3503 : :
3504 : : static int
3505 : 0 : hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3506 : : {
3507 : : struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3508 : : struct hns3_rx_com_wl *req;
3509 : : struct hns3_cmd_desc desc;
3510 : : int ret;
3511 : :
3512 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3513 : :
3514 : : req = (struct hns3_rx_com_wl *)desc.data;
3515 : 0 : req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3516 : 0 : req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3517 : :
3518 : 0 : req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3519 : 0 : req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3520 : :
3521 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3522 [ # # ]: 0 : if (ret)
3523 : 0 : PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3524 : :
3525 : 0 : return ret;
3526 : : }
3527 : :
3528 : : int
3529 : 0 : hns3_buffer_alloc(struct hns3_hw *hw)
3530 : : {
3531 : : struct hns3_pkt_buf_alloc pkt_buf;
3532 : : int ret;
3533 : :
3534 : : memset(&pkt_buf, 0, sizeof(pkt_buf));
3535 : : ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3536 [ # # ]: 0 : if (ret) {
3537 : 0 : PMD_INIT_LOG(ERR,
3538 : : "could not calc tx buffer size for all TCs %d",
3539 : : ret);
3540 : 0 : return ret;
3541 : : }
3542 : :
3543 : 0 : ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3544 [ # # ]: 0 : if (ret) {
3545 : 0 : PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3546 : 0 : return ret;
3547 : : }
3548 : :
3549 : 0 : ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3550 [ # # ]: 0 : if (ret) {
3551 : 0 : PMD_INIT_LOG(ERR,
3552 : : "could not calc rx priv buffer size for all TCs %d",
3553 : : ret);
3554 : 0 : return ret;
3555 : : }
3556 : :
3557 : 0 : ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3558 [ # # ]: 0 : if (ret) {
3559 : 0 : PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3560 : 0 : return ret;
3561 : : }
3562 : :
3563 [ # # ]: 0 : if (hns3_dev_get_support(hw, DCB)) {
3564 : 0 : ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3565 [ # # ]: 0 : if (ret) {
3566 : 0 : PMD_INIT_LOG(ERR,
3567 : : "could not configure rx private waterline %d",
3568 : : ret);
3569 : 0 : return ret;
3570 : : }
3571 : :
3572 : 0 : ret = hns3_common_thrd_config(hw, &pkt_buf);
3573 [ # # ]: 0 : if (ret) {
3574 : 0 : PMD_INIT_LOG(ERR,
3575 : : "could not configure common threshold %d",
3576 : : ret);
3577 : 0 : return ret;
3578 : : }
3579 : : }
3580 : :
3581 : 0 : ret = hns3_common_wl_config(hw, &pkt_buf);
3582 [ # # ]: 0 : if (ret)
3583 : 0 : PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3584 : : ret);
3585 : :
3586 : : return ret;
3587 : : }
3588 : :
3589 : : static int
3590 : 0 : hns3_mac_init(struct hns3_hw *hw)
3591 : : {
3592 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3593 : : struct hns3_mac *mac = &hw->mac;
3594 : : struct hns3_pf *pf = &hns->pf;
3595 : : int ret;
3596 : :
3597 : 0 : pf->support_sfp_query = true;
3598 : 0 : mac->link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3599 : 0 : ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3600 [ # # ]: 0 : if (ret) {
3601 : 0 : PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3602 : 0 : return ret;
3603 : : }
3604 : :
3605 : 0 : mac->link_status = RTE_ETH_LINK_DOWN;
3606 : :
3607 : 0 : return hns3_config_mtu(hw, pf->mps);
3608 : : }
3609 : :
3610 : : static int
3611 : 0 : hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3612 : : {
3613 : : #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3614 : : #define HNS3_ETHERTYPE_ALREADY_ADD 1
3615 : : #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3616 : : #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3617 : : int return_status;
3618 : :
3619 [ # # ]: 0 : if (cmdq_resp) {
3620 : 0 : PMD_INIT_LOG(ERR,
3621 : : "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.",
3622 : : cmdq_resp);
3623 : 0 : return -EIO;
3624 : : }
3625 : :
3626 [ # # # # ]: 0 : switch (resp_code) {
3627 : : case HNS3_ETHERTYPE_SUCCESS_ADD:
3628 : : case HNS3_ETHERTYPE_ALREADY_ADD:
3629 : : return_status = 0;
3630 : : break;
3631 : 0 : case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3632 : 0 : PMD_INIT_LOG(ERR,
3633 : : "add mac ethertype failed for manager table overflow.");
3634 : : return_status = -EIO;
3635 : 0 : break;
3636 : 0 : case HNS3_ETHERTYPE_KEY_CONFLICT:
3637 : 0 : PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3638 : : return_status = -EIO;
3639 : 0 : break;
3640 : 0 : default:
3641 : 0 : PMD_INIT_LOG(ERR,
3642 : : "add mac ethertype failed for undefined, code=%u.",
3643 : : resp_code);
3644 : : return_status = -EIO;
3645 : 0 : break;
3646 : : }
3647 : :
3648 : : return return_status;
3649 : : }
3650 : :
3651 : : static int
3652 : 0 : hns3_add_mgr_tbl(struct hns3_hw *hw,
3653 : : const struct hns3_mac_mgr_tbl_entry_cmd *req)
3654 : : {
3655 : : struct hns3_cmd_desc desc;
3656 : : uint8_t resp_code;
3657 : : uint16_t retval;
3658 : : int ret;
3659 : :
3660 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3661 : : memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3662 : :
3663 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3664 [ # # ]: 0 : if (ret) {
3665 : 0 : PMD_INIT_LOG(ERR,
3666 : : "add mac ethertype failed for cmd_send, ret =%d.",
3667 : : ret);
3668 : 0 : return ret;
3669 : : }
3670 : :
3671 : 0 : resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3672 : 0 : retval = rte_le_to_cpu_16(desc.retval);
3673 : :
3674 : 0 : return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3675 : : }
3676 : :
3677 : : static void
3678 : : hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3679 : : int *table_item_num)
3680 : : {
3681 : : struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3682 : :
3683 : : /*
3684 : : * In current version, we add one item in management table as below:
3685 : : * 0x0180C200000E -- LLDP MC address
3686 : : */
3687 : : tbl = mgr_table;
3688 : 0 : tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3689 : 0 : tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3690 : 0 : tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3691 : 0 : tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3692 : 0 : tbl->i_port_bitmap = 0x1;
3693 : : *table_item_num = 1;
3694 : : }
3695 : :
3696 : : static int
3697 : 0 : hns3_init_mgr_tbl(struct hns3_hw *hw)
3698 : : {
3699 : : #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3700 : : struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3701 : : int table_item_num;
3702 : : int ret;
3703 : : int i;
3704 : :
3705 : : memset(mgr_table, 0, sizeof(mgr_table));
3706 : : hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3707 [ # # ]: 0 : for (i = 0; i < table_item_num; i++) {
3708 : 0 : ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3709 [ # # ]: 0 : if (ret) {
3710 : 0 : PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3711 : : ret);
3712 : 0 : return ret;
3713 : : }
3714 : : }
3715 : :
3716 : : return 0;
3717 : : }
3718 : :
3719 : : static void
3720 : 0 : hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
3721 : : bool en_mc, bool en_bc, int vport_id)
3722 : : {
3723 [ # # ]: 0 : if (!param)
3724 : : return;
3725 : :
3726 : : memset(param, 0, sizeof(struct hns3_promisc_param));
3727 [ # # ]: 0 : if (en_uc)
3728 : 0 : param->enable = HNS3_PROMISC_EN_UC;
3729 [ # # ]: 0 : if (en_mc)
3730 : 0 : param->enable |= HNS3_PROMISC_EN_MC;
3731 [ # # ]: 0 : if (en_bc)
3732 : 0 : param->enable |= HNS3_PROMISC_EN_BC;
3733 : 0 : param->vf_id = vport_id;
3734 : : }
3735 : :
3736 : : static int
3737 : 0 : hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
3738 : : {
3739 : : struct hns3_promisc_cfg_cmd *req;
3740 : : struct hns3_cmd_desc desc;
3741 : : int ret;
3742 : :
3743 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
3744 : :
3745 : : req = (struct hns3_promisc_cfg_cmd *)desc.data;
3746 : 0 : req->vf_id = param->vf_id;
3747 : 0 : req->flag = (param->enable << HNS3_PROMISC_EN_B) |
3748 : : HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
3749 : :
3750 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3751 [ # # ]: 0 : if (ret)
3752 : 0 : PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
3753 : :
3754 : 0 : return ret;
3755 : : }
3756 : :
3757 : : static int
3758 : 0 : hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
3759 : : {
3760 : : struct hns3_promisc_param param;
3761 : : bool en_bc_pmc = true;
3762 : : uint8_t vf_id;
3763 : :
3764 : : /*
3765 : : * In current version VF is not supported when PF is driven by DPDK
3766 : : * driver, just need to configure parameters for PF vport.
3767 : : */
3768 : : vf_id = HNS3_PF_FUNC_ID;
3769 : :
3770 : 0 : hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
3771 : 0 : return hns3_cmd_set_promisc_mode(hw, ¶m);
3772 : : }
3773 : :
3774 : : static int
3775 : 0 : hns3_promisc_init(struct hns3_hw *hw)
3776 : : {
3777 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3778 : : struct hns3_pf *pf = &hns->pf;
3779 : : struct hns3_promisc_param param;
3780 : : uint16_t func_id;
3781 : : int ret;
3782 : :
3783 : 0 : ret = hns3_set_promisc_mode(hw, false, false);
3784 [ # # ]: 0 : if (ret) {
3785 : 0 : PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
3786 : 0 : return ret;
3787 : : }
3788 : :
3789 : : /*
3790 : : * In current version VFs are not supported when PF is driven by DPDK
3791 : : * driver. After PF has been taken over by DPDK, the original VF will
3792 : : * be invalid. So, there is a possibility of entry residues. It should
3793 : : * clear VFs's promisc mode to avoid unnecessary bandwidth usage
3794 : : * during init.
3795 : : */
3796 [ # # ]: 0 : for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
3797 : 0 : hns3_promisc_param_init(¶m, false, false, false, func_id);
3798 : 0 : ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3799 [ # # ]: 0 : if (ret) {
3800 : 0 : PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
3801 : : " ret = %d", func_id, ret);
3802 : 0 : return ret;
3803 : : }
3804 : : }
3805 : :
3806 : : return 0;
3807 : : }
3808 : :
3809 : : static void
3810 : 0 : hns3_promisc_uninit(struct hns3_hw *hw)
3811 : : {
3812 : : struct hns3_promisc_param param;
3813 : : uint16_t func_id;
3814 : : int ret;
3815 : :
3816 : : func_id = HNS3_PF_FUNC_ID;
3817 : :
3818 : : /*
3819 : : * In current version VFs are not supported when PF is driven by
3820 : : * DPDK driver, and VFs' promisc mode status has been cleared during
3821 : : * init and their status will not change. So just clear PF's promisc
3822 : : * mode status during uninit.
3823 : : */
3824 : : hns3_promisc_param_init(¶m, false, false, false, func_id);
3825 : 0 : ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3826 [ # # ]: 0 : if (ret)
3827 : 0 : PMD_INIT_LOG(ERR, "failed to clear promisc status during"
3828 : : " uninit, ret = %d", ret);
3829 : 0 : }
3830 : :
3831 : : static int
3832 : 0 : hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
3833 : : {
3834 : 0 : bool allmulti = dev->data->all_multicast ? true : false;
3835 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
3836 : 0 : struct hns3_hw *hw = &hns->hw;
3837 : : uint64_t offloads;
3838 : : int err;
3839 : : int ret;
3840 : :
3841 : 0 : rte_spinlock_lock(&hw->lock);
3842 : 0 : ret = hns3_set_promisc_mode(hw, true, true);
3843 [ # # ]: 0 : if (ret) {
3844 : : rte_spinlock_unlock(&hw->lock);
3845 : 0 : hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
3846 : : ret);
3847 : 0 : return ret;
3848 : : }
3849 : :
3850 : : /*
3851 : : * When promiscuous mode was enabled, disable the vlan filter to let
3852 : : * all packets coming in the receiving direction.
3853 : : */
3854 : 0 : offloads = dev->data->dev_conf.rxmode.offloads;
3855 [ # # ]: 0 : if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
3856 : 0 : ret = hns3_enable_vlan_filter(hns, false);
3857 [ # # ]: 0 : if (ret) {
3858 : 0 : hns3_err(hw, "failed to enable promiscuous mode due to "
3859 : : "failure to disable vlan filter, ret = %d",
3860 : : ret);
3861 : 0 : err = hns3_set_promisc_mode(hw, false, allmulti);
3862 [ # # ]: 0 : if (err)
3863 : 0 : hns3_err(hw, "failed to restore promiscuous "
3864 : : "status after disable vlan filter "
3865 : : "failed during enabling promiscuous "
3866 : : "mode, ret = %d", ret);
3867 : : }
3868 : : }
3869 : :
3870 : : rte_spinlock_unlock(&hw->lock);
3871 : :
3872 : 0 : return ret;
3873 : : }
3874 : :
3875 : : static int
3876 : 0 : hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
3877 : : {
3878 : 0 : bool allmulti = dev->data->all_multicast ? true : false;
3879 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
3880 : 0 : struct hns3_hw *hw = &hns->hw;
3881 : : uint64_t offloads;
3882 : : int err;
3883 : : int ret;
3884 : :
3885 : : /* If now in all_multicast mode, must remain in all_multicast mode. */
3886 : 0 : rte_spinlock_lock(&hw->lock);
3887 : 0 : ret = hns3_set_promisc_mode(hw, false, allmulti);
3888 [ # # ]: 0 : if (ret) {
3889 : : rte_spinlock_unlock(&hw->lock);
3890 : 0 : hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
3891 : : ret);
3892 : 0 : return ret;
3893 : : }
3894 : : /* when promiscuous mode was disabled, restore the vlan filter status */
3895 : 0 : offloads = dev->data->dev_conf.rxmode.offloads;
3896 [ # # ]: 0 : if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
3897 : 0 : ret = hns3_enable_vlan_filter(hns, true);
3898 [ # # ]: 0 : if (ret) {
3899 : 0 : hns3_err(hw, "failed to disable promiscuous mode due to"
3900 : : " failure to restore vlan filter, ret = %d",
3901 : : ret);
3902 : 0 : err = hns3_set_promisc_mode(hw, true, true);
3903 [ # # ]: 0 : if (err)
3904 : 0 : hns3_err(hw, "failed to restore promiscuous "
3905 : : "status after enabling vlan filter "
3906 : : "failed during disabling promiscuous "
3907 : : "mode, ret = %d", ret);
3908 : : }
3909 : : }
3910 : : rte_spinlock_unlock(&hw->lock);
3911 : :
3912 : 0 : return ret;
3913 : : }
3914 : :
3915 : : static int
3916 : 0 : hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
3917 : : {
3918 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
3919 : 0 : struct hns3_hw *hw = &hns->hw;
3920 : : int ret;
3921 : :
3922 [ # # ]: 0 : if (dev->data->promiscuous)
3923 : : return 0;
3924 : :
3925 : 0 : rte_spinlock_lock(&hw->lock);
3926 : 0 : ret = hns3_set_promisc_mode(hw, false, true);
3927 : : rte_spinlock_unlock(&hw->lock);
3928 [ # # ]: 0 : if (ret)
3929 : 0 : hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
3930 : : ret);
3931 : :
3932 : : return ret;
3933 : : }
3934 : :
3935 : : static int
3936 : 0 : hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
3937 : : {
3938 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
3939 : 0 : struct hns3_hw *hw = &hns->hw;
3940 : : int ret;
3941 : :
3942 : : /* If now in promiscuous mode, must remain in all_multicast mode. */
3943 [ # # ]: 0 : if (dev->data->promiscuous)
3944 : : return 0;
3945 : :
3946 : 0 : rte_spinlock_lock(&hw->lock);
3947 : 0 : ret = hns3_set_promisc_mode(hw, false, false);
3948 : : rte_spinlock_unlock(&hw->lock);
3949 [ # # ]: 0 : if (ret)
3950 : 0 : hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
3951 : : ret);
3952 : :
3953 : : return ret;
3954 : : }
3955 : :
3956 : : static int
3957 : 0 : hns3_dev_promisc_restore(struct hns3_adapter *hns)
3958 : : {
3959 : 0 : struct hns3_hw *hw = &hns->hw;
3960 : 0 : bool allmulti = hw->data->all_multicast ? true : false;
3961 : : int ret;
3962 : :
3963 [ # # ]: 0 : if (hw->data->promiscuous) {
3964 : 0 : ret = hns3_set_promisc_mode(hw, true, true);
3965 [ # # ]: 0 : if (ret)
3966 : 0 : hns3_err(hw, "failed to restore promiscuous mode, "
3967 : : "ret = %d", ret);
3968 : 0 : return ret;
3969 : : }
3970 : :
3971 : 0 : ret = hns3_set_promisc_mode(hw, false, allmulti);
3972 [ # # ]: 0 : if (ret)
3973 : 0 : hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
3974 : : ret);
3975 : : return ret;
3976 : : }
3977 : :
3978 : : static int
3979 : 0 : hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info)
3980 : : {
3981 : : struct hns3_sfp_info_cmd *resp;
3982 : : uint32_t local_pause, lp_pause;
3983 : : struct hns3_cmd_desc desc;
3984 : : int ret;
3985 : :
3986 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
3987 : : resp = (struct hns3_sfp_info_cmd *)desc.data;
3988 : 0 : resp->query_type = HNS3_ACTIVE_QUERY;
3989 : :
3990 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
3991 [ # # ]: 0 : if (ret == -EOPNOTSUPP) {
3992 : 0 : hns3_warn(hw, "firmware does not support get SFP info,"
3993 : : " ret = %d.", ret);
3994 : 0 : return ret;
3995 [ # # ]: 0 : } else if (ret) {
3996 : 0 : hns3_err(hw, "get sfp info failed, ret = %d.", ret);
3997 : 0 : return ret;
3998 : : }
3999 : :
4000 : : /*
4001 : : * In some case, the speed of MAC obtained from firmware may be 0, it
4002 : : * shouldn't be set to mac->speed.
4003 : : */
4004 [ # # ]: 0 : if (!rte_le_to_cpu_32(resp->sfp_speed))
4005 : : return 0;
4006 : :
4007 : 0 : mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed);
4008 : : /*
4009 : : * if resp->supported_speed is 0, it means it's an old version
4010 : : * firmware, do not update these params.
4011 : : */
4012 [ # # ]: 0 : if (resp->supported_speed) {
4013 : 0 : mac_info->query_type = HNS3_ACTIVE_QUERY;
4014 : 0 : mac_info->supported_speed =
4015 : : rte_le_to_cpu_32(resp->supported_speed);
4016 : 0 : mac_info->support_autoneg = resp->autoneg_ability;
4017 : 0 : mac_info->link_autoneg = (resp->autoneg == 0) ? RTE_ETH_LINK_FIXED
4018 : 0 : : RTE_ETH_LINK_AUTONEG;
4019 : 0 : mac_info->fec_capa = resp->fec_ability;
4020 : 0 : local_pause = resp->pause_status & HNS3_FIBER_LOCAL_PAUSE_MASK;
4021 : 0 : lp_pause = (resp->pause_status & HNS3_FIBER_LP_PAUSE_MASK) >>
4022 : : HNS3_FIBER_LP_PAUSE_S;
4023 : 0 : mac_info->advertising =
4024 : 0 : local_pause << HNS3_PHY_LINK_MODE_PAUSE_S;
4025 : 0 : mac_info->lp_advertising =
4026 : 0 : lp_pause << HNS3_PHY_LINK_MODE_PAUSE_S;
4027 : : } else {
4028 : 0 : mac_info->query_type = HNS3_DEFAULT_QUERY;
4029 : : }
4030 : :
4031 : : return 0;
4032 : : }
4033 : :
4034 : : static uint8_t
4035 : : hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4036 : : {
4037 : 0 : if (!(speed == RTE_ETH_SPEED_NUM_10M || speed == RTE_ETH_SPEED_NUM_100M))
4038 : : duplex = RTE_ETH_LINK_FULL_DUPLEX;
4039 : :
4040 : : return duplex;
4041 : : }
4042 : :
4043 : : static int
4044 [ # # ]: 0 : hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4045 : : {
4046 : : struct hns3_mac *mac = &hw->mac;
4047 : : int ret;
4048 : :
4049 : : duplex = hns3_check_speed_dup(duplex, speed);
4050 [ # # # # ]: 0 : if (mac->link_speed == speed && mac->link_duplex == duplex)
4051 : : return 0;
4052 : :
4053 : 0 : ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4054 [ # # ]: 0 : if (ret)
4055 : : return ret;
4056 : :
4057 : 0 : ret = hns3_port_shaper_update(hw, speed);
4058 [ # # ]: 0 : if (ret)
4059 : : return ret;
4060 : :
4061 : 0 : mac->link_speed = speed;
4062 : 0 : mac->link_duplex = duplex;
4063 : :
4064 : 0 : return 0;
4065 : : }
4066 : :
4067 : : static int
4068 : 0 : hns3_update_fiber_link_info(struct hns3_hw *hw)
4069 : : {
4070 : : struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4071 : : struct hns3_mac *mac = &hw->mac;
4072 : : struct hns3_mac mac_info;
4073 : : int ret;
4074 : :
4075 : : /* If firmware do not support get SFP/qSFP speed, return directly */
4076 [ # # ]: 0 : if (!pf->support_sfp_query)
4077 : : return 0;
4078 : :
4079 : : memset(&mac_info, 0, sizeof(struct hns3_mac));
4080 : 0 : ret = hns3_get_sfp_info(hw, &mac_info);
4081 [ # # ]: 0 : if (ret == -EOPNOTSUPP) {
4082 : 0 : pf->support_sfp_query = false;
4083 : 0 : return ret;
4084 [ # # ]: 0 : } else if (ret)
4085 : : return ret;
4086 : :
4087 : : /* Do nothing if no SFP */
4088 [ # # ]: 0 : if (mac_info.link_speed == RTE_ETH_SPEED_NUM_NONE)
4089 : : return 0;
4090 : :
4091 : : /*
4092 : : * If query_type is HNS3_ACTIVE_QUERY, it is no need
4093 : : * to reconfigure the speed of MAC. Otherwise, it indicates
4094 : : * that the current firmware only supports to obtain the
4095 : : * speed of the SFP, and the speed of MAC needs to reconfigure.
4096 : : */
4097 : 0 : mac->query_type = mac_info.query_type;
4098 [ # # ]: 0 : if (mac->query_type == HNS3_ACTIVE_QUERY) {
4099 [ # # ]: 0 : if (mac_info.link_speed != mac->link_speed) {
4100 : 0 : ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4101 [ # # ]: 0 : if (ret)
4102 : : return ret;
4103 : : }
4104 : :
4105 : 0 : mac->link_speed = mac_info.link_speed;
4106 : 0 : mac->supported_speed = mac_info.supported_speed;
4107 : 0 : mac->support_autoneg = mac_info.support_autoneg;
4108 : 0 : mac->link_autoneg = mac_info.link_autoneg;
4109 : 0 : mac->fec_capa = mac_info.fec_capa;
4110 : 0 : mac->advertising = mac_info.advertising;
4111 : 0 : mac->lp_advertising = mac_info.lp_advertising;
4112 : :
4113 : 0 : return 0;
4114 : : }
4115 : :
4116 : : /* Config full duplex for SFP */
4117 : 0 : return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed,
4118 : : RTE_ETH_LINK_FULL_DUPLEX);
4119 : : }
4120 : :
4121 : : static void
4122 : : hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4123 : : {
4124 : : #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f
4125 : :
4126 : : struct hns3_phy_params_bd0_cmd *req;
4127 : : uint32_t supported;
4128 : :
4129 : : req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4130 : 0 : mac->link_speed = rte_le_to_cpu_32(req->speed);
4131 : 0 : mac->link_duplex = hns3_get_bit(req->duplex,
4132 : : HNS3_PHY_DUPLEX_CFG_B);
4133 : 0 : mac->link_autoneg = hns3_get_bit(req->autoneg,
4134 : : HNS3_PHY_AUTONEG_CFG_B);
4135 : 0 : mac->advertising = rte_le_to_cpu_32(req->advertising);
4136 : 0 : mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4137 : 0 : supported = rte_le_to_cpu_32(req->supported);
4138 : 0 : mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
4139 : 0 : mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4140 : : }
4141 : :
4142 : : static int
4143 : 0 : hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4144 : : {
4145 : : struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4146 : : uint16_t i;
4147 : : int ret;
4148 : :
4149 [ # # ]: 0 : for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4150 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4151 : : true);
4152 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4153 : : }
4154 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4155 : :
4156 : 0 : ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4157 [ # # ]: 0 : if (ret) {
4158 : 0 : hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4159 : 0 : return ret;
4160 : : }
4161 : :
4162 : : hns3_parse_copper_phy_params(desc, mac);
4163 : :
4164 : 0 : return 0;
4165 : : }
4166 : :
4167 : : static int
4168 : 0 : hns3_update_copper_link_info(struct hns3_hw *hw)
4169 : : {
4170 : : struct hns3_mac *mac = &hw->mac;
4171 : : struct hns3_mac mac_info;
4172 : : int ret;
4173 : :
4174 : : memset(&mac_info, 0, sizeof(struct hns3_mac));
4175 : 0 : ret = hns3_get_copper_phy_params(hw, &mac_info);
4176 [ # # ]: 0 : if (ret)
4177 : : return ret;
4178 : :
4179 [ # # ]: 0 : if (mac_info.link_speed != mac->link_speed) {
4180 : 0 : ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4181 [ # # ]: 0 : if (ret)
4182 : : return ret;
4183 : : }
4184 : :
4185 : 0 : mac->link_speed = mac_info.link_speed;
4186 : 0 : mac->link_duplex = mac_info.link_duplex;
4187 : 0 : mac->link_autoneg = mac_info.link_autoneg;
4188 : 0 : mac->supported_speed = mac_info.supported_speed;
4189 : 0 : mac->advertising = mac_info.advertising;
4190 : 0 : mac->lp_advertising = mac_info.lp_advertising;
4191 : 0 : mac->support_autoneg = mac_info.support_autoneg;
4192 : :
4193 : 0 : return 0;
4194 : : }
4195 : :
4196 : : static int
4197 : 0 : hns3_update_link_info(struct rte_eth_dev *eth_dev)
4198 : : {
4199 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
4200 : 0 : struct hns3_hw *hw = &hns->hw;
4201 : :
4202 [ # # ]: 0 : if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4203 : 0 : return hns3_update_copper_link_info(hw);
4204 : :
4205 : 0 : return hns3_update_fiber_link_info(hw);
4206 : : }
4207 : :
4208 : : static int
4209 : 0 : hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4210 : : {
4211 : : struct hns3_config_mac_mode_cmd *req;
4212 : : struct hns3_cmd_desc desc;
4213 : : uint32_t loop_en = 0;
4214 : : uint8_t val = 0;
4215 : : int ret;
4216 : :
4217 : : req = (struct hns3_config_mac_mode_cmd *)desc.data;
4218 : :
4219 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4220 [ # # ]: 0 : if (enable)
4221 : : val = 1;
4222 : 0 : hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4223 : 0 : hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4224 : 0 : hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4225 : 0 : hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4226 : : hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4227 : : hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4228 : : hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4229 : : hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4230 : 0 : hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4231 : 0 : hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4232 : :
4233 : : /*
4234 : : * If RTE_ETH_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4235 : : * when receiving frames. Otherwise, CRC will be stripped.
4236 : : */
4237 [ # # ]: 0 : if (hw->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
4238 : : hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4239 : : else
4240 : 0 : hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4241 : 0 : hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4242 : 0 : hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4243 : 0 : hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4244 : 0 : req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4245 : :
4246 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
4247 [ # # ]: 0 : if (ret)
4248 : 0 : PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4249 : :
4250 : 0 : return ret;
4251 : : }
4252 : :
4253 : : static int
4254 : 0 : hns3_get_mac_link_status(struct hns3_hw *hw)
4255 : : {
4256 : : struct hns3_link_status_cmd *req;
4257 : : struct hns3_cmd_desc desc;
4258 : : int link_status;
4259 : : int ret;
4260 : :
4261 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4262 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
4263 [ # # ]: 0 : if (ret) {
4264 : 0 : hns3_err(hw, "get link status cmd failed %d", ret);
4265 : 0 : return RTE_ETH_LINK_DOWN;
4266 : : }
4267 : :
4268 : : req = (struct hns3_link_status_cmd *)desc.data;
4269 : 0 : link_status = req->status & HNS3_LINK_STATUS_UP_M;
4270 : :
4271 : 0 : return !!link_status;
4272 : : }
4273 : :
4274 : : static bool
4275 : 0 : hns3_update_link_status(struct hns3_hw *hw)
4276 : : {
4277 : : int state;
4278 : :
4279 : 0 : state = hns3_get_mac_link_status(hw);
4280 [ # # ]: 0 : if (state != hw->mac.link_status) {
4281 : 0 : hw->mac.link_status = state;
4282 [ # # ]: 0 : hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4283 : 0 : return true;
4284 : : }
4285 : :
4286 : : return false;
4287 : : }
4288 : :
4289 : : void
4290 : 0 : hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
4291 : : {
4292 : 0 : struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4293 : : struct rte_eth_link new_link;
4294 : : int ret;
4295 : :
4296 [ # # ]: 0 : if (query)
4297 : 0 : hns3_update_port_link_info(dev);
4298 : :
4299 : : memset(&new_link, 0, sizeof(new_link));
4300 : 0 : hns3_setup_linkstatus(dev, &new_link);
4301 : :
4302 : : ret = rte_eth_linkstatus_set(dev, &new_link);
4303 [ # # ]: 0 : if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
4304 : 0 : hns3_start_report_lse(dev);
4305 : 0 : }
4306 : :
4307 : : static void
4308 : 0 : hns3_service_handler(void *param)
4309 : : {
4310 : : struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4311 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
4312 : 0 : struct hns3_hw *hw = &hns->hw;
4313 : :
4314 [ # # ]: 0 : if (!hns3_is_reset_pending(hns)) {
4315 : 0 : hns3_update_linkstatus_and_event(hw, true);
4316 : 0 : hns3_update_hw_stats(hw);
4317 : : } else {
4318 : 0 : hns3_warn(hw, "Cancel the query when reset is pending");
4319 : : }
4320 : :
4321 : 0 : rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4322 : 0 : }
4323 : :
4324 : : static int
4325 : 0 : hns3_init_hardware(struct hns3_adapter *hns)
4326 : : {
4327 : 0 : struct hns3_hw *hw = &hns->hw;
4328 : : int ret;
4329 : :
4330 : : /*
4331 : : * All queue-related HW operations must be performed after the TCAM
4332 : : * table is configured.
4333 : : */
4334 : 0 : ret = hns3_map_tqp(hw);
4335 [ # # ]: 0 : if (ret) {
4336 : 0 : PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4337 : 0 : return ret;
4338 : : }
4339 : :
4340 : 0 : ret = hns3_init_umv_space(hw);
4341 [ # # ]: 0 : if (ret) {
4342 : 0 : PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4343 : 0 : return ret;
4344 : : }
4345 : :
4346 : 0 : ret = hns3_mac_init(hw);
4347 [ # # ]: 0 : if (ret) {
4348 : 0 : PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4349 : 0 : goto err_mac_init;
4350 : : }
4351 : :
4352 : 0 : ret = hns3_init_mgr_tbl(hw);
4353 [ # # ]: 0 : if (ret) {
4354 : 0 : PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4355 : 0 : goto err_mac_init;
4356 : : }
4357 : :
4358 : 0 : ret = hns3_promisc_init(hw);
4359 [ # # ]: 0 : if (ret) {
4360 : 0 : PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4361 : : ret);
4362 : 0 : goto err_mac_init;
4363 : : }
4364 : :
4365 : 0 : ret = hns3_init_vlan_config(hns);
4366 [ # # ]: 0 : if (ret) {
4367 : 0 : PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4368 : 0 : goto err_mac_init;
4369 : : }
4370 : :
4371 : 0 : ret = hns3_dcb_init(hw);
4372 [ # # ]: 0 : if (ret) {
4373 : 0 : PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4374 : 0 : goto rm_vlan_table;
4375 : : }
4376 : :
4377 : 0 : ret = hns3_init_fd_config(hns);
4378 [ # # ]: 0 : if (ret) {
4379 : 0 : PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4380 : 0 : goto rm_vlan_table;
4381 : : }
4382 : :
4383 : 0 : ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4384 [ # # ]: 0 : if (ret) {
4385 : 0 : PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4386 : 0 : goto rm_vlan_table;
4387 : : }
4388 : :
4389 : 0 : ret = hns3_config_gro(hw, false);
4390 [ # # ]: 0 : if (ret) {
4391 : 0 : PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4392 : 0 : goto rm_vlan_table;
4393 : : }
4394 : :
4395 : : /*
4396 : : * In the initialization clearing the all hardware mapping relationship
4397 : : * configurations between queues and interrupt vectors is needed, so
4398 : : * some error caused by the residual configurations, such as the
4399 : : * unexpected interrupt, can be avoid.
4400 : : */
4401 : 0 : ret = hns3_init_ring_with_vector(hw);
4402 [ # # ]: 0 : if (ret) {
4403 : 0 : PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4404 : 0 : goto rm_vlan_table;
4405 : : }
4406 : :
4407 : 0 : ret = hns3_ptp_init(hw);
4408 [ # # ]: 0 : if (ret) {
4409 : 0 : PMD_INIT_LOG(ERR, "Failed to init PTP, ret = %d", ret);
4410 : 0 : goto rm_vlan_table;
4411 : : }
4412 : :
4413 : : return 0;
4414 : 0 : rm_vlan_table:
4415 : 0 : hns3_rm_all_vlan_table(hns, true);
4416 : 0 : err_mac_init:
4417 : 0 : hns3_uninit_umv_space(hw);
4418 : 0 : return ret;
4419 : : }
4420 : :
4421 : : static void
4422 : 0 : hns3_uninit_hardware(struct hns3_hw *hw)
4423 : : {
4424 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4425 : :
4426 : 0 : (void)hns3_uninit_umv_space(hw);
4427 : 0 : hns3_ptp_uninit(hw);
4428 : 0 : hns3_rm_all_vlan_table(hns, true);
4429 : 0 : }
4430 : :
4431 : : static int
4432 : 0 : hns3_clear_hw(struct hns3_hw *hw)
4433 : : {
4434 : : struct hns3_cmd_desc desc;
4435 : : int ret;
4436 : :
4437 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4438 : :
4439 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
4440 [ # # ]: 0 : if (ret && ret != -EOPNOTSUPP)
4441 : 0 : return ret;
4442 : :
4443 : : return 0;
4444 : : }
4445 : :
4446 : : static void
4447 : : hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4448 : : {
4449 : : uint32_t val;
4450 : :
4451 : : /*
4452 : : * The new firmware support report more hardware error types by
4453 : : * msix mode. These errors are defined as RAS errors in hardware
4454 : : * and belong to a different type from the MSI-x errors processed
4455 : : * by the network driver.
4456 : : *
4457 : : * Network driver should open the new error report on initialization.
4458 : : */
4459 : 0 : val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4460 : 0 : hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4461 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4462 : : }
4463 : :
4464 : : static uint32_t
4465 : 0 : hns3_set_firber_default_support_speed(struct hns3_hw *hw)
4466 : : {
4467 : : struct hns3_mac *mac = &hw->mac;
4468 : :
4469 [ # # # # : 0 : switch (mac->link_speed) {
# # # # ]
4470 : : case RTE_ETH_SPEED_NUM_1G:
4471 : : return HNS3_FIBER_LINK_SPEED_1G_BIT;
4472 : 0 : case RTE_ETH_SPEED_NUM_10G:
4473 : 0 : return HNS3_FIBER_LINK_SPEED_10G_BIT;
4474 : 0 : case RTE_ETH_SPEED_NUM_25G:
4475 : 0 : return HNS3_FIBER_LINK_SPEED_25G_BIT;
4476 : 0 : case RTE_ETH_SPEED_NUM_40G:
4477 : 0 : return HNS3_FIBER_LINK_SPEED_40G_BIT;
4478 : 0 : case RTE_ETH_SPEED_NUM_50G:
4479 : 0 : return HNS3_FIBER_LINK_SPEED_50G_BIT;
4480 : 0 : case RTE_ETH_SPEED_NUM_100G:
4481 : 0 : return HNS3_FIBER_LINK_SPEED_100G_BIT;
4482 : 0 : case RTE_ETH_SPEED_NUM_200G:
4483 : 0 : return HNS3_FIBER_LINK_SPEED_200G_BIT;
4484 : 0 : default:
4485 : 0 : hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed);
4486 : 0 : return 0;
4487 : : }
4488 : : }
4489 : :
4490 : : /*
4491 : : * Validity of supported_speed for fiber and copper media type can be
4492 : : * guaranteed by the following policy:
4493 : : * Copper:
4494 : : * Although the initialization of the phy in the firmware may not be
4495 : : * completed, the firmware can guarantees that the supported_speed is
4496 : : * an valid value.
4497 : : * Firber:
4498 : : * If the version of firmware supports the active query way of the
4499 : : * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained
4500 : : * through it. If unsupported, use the SFP's speed as the value of the
4501 : : * supported_speed.
4502 : : */
4503 : : static int
4504 : 0 : hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev)
4505 : : {
4506 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
4507 : 0 : struct hns3_hw *hw = &hns->hw;
4508 : : struct hns3_mac *mac = &hw->mac;
4509 : : int ret;
4510 : :
4511 : 0 : ret = hns3_update_link_info(eth_dev);
4512 [ # # ]: 0 : if (ret)
4513 : : return ret;
4514 : :
4515 [ # # ]: 0 : if (mac->media_type == HNS3_MEDIA_TYPE_FIBER ||
4516 : : mac->media_type == HNS3_MEDIA_TYPE_BACKPLANE) {
4517 : : /*
4518 : : * Some firmware does not support the report of supported_speed,
4519 : : * and only report the effective speed of SFP/backplane. In this
4520 : : * case, it is necessary to use the SFP/backplane's speed as the
4521 : : * supported_speed.
4522 : : */
4523 [ # # ]: 0 : if (mac->supported_speed == 0)
4524 : 0 : mac->supported_speed =
4525 : 0 : hns3_set_firber_default_support_speed(hw);
4526 : : }
4527 : :
4528 : : return 0;
4529 : : }
4530 : :
4531 : : static int
4532 : 0 : hns3_init_pf(struct rte_eth_dev *eth_dev)
4533 : : {
4534 : 0 : struct rte_pci_device *pci_dev = RTE_CLASS_TO_BUS_DEVICE(eth_dev, *pci_dev);
4535 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
4536 : 0 : struct hns3_hw *hw = &hns->hw;
4537 : : int ret;
4538 : :
4539 : 0 : PMD_INIT_FUNC_TRACE();
4540 : :
4541 : : /* Get hardware io base address from pcie BAR2 IO space */
4542 : 0 : hw->io_base = pci_dev->mem_resource[2].addr;
4543 : :
4544 : 0 : ret = hns3_get_pci_revision_id(hw, &hw->revision);
4545 [ # # ]: 0 : if (ret)
4546 : : return ret;
4547 : :
4548 : : /* Firmware command queue initialize */
4549 : 0 : ret = hns3_cmd_init_queue(hw);
4550 [ # # ]: 0 : if (ret) {
4551 : 0 : PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4552 : 0 : goto err_cmd_init_queue;
4553 : : }
4554 : :
4555 : 0 : hns3_clear_all_event_cause(hw);
4556 : :
4557 : : /* Firmware command initialize */
4558 : 0 : ret = hns3_cmd_init(hw);
4559 [ # # ]: 0 : if (ret) {
4560 : 0 : PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4561 : 0 : goto err_cmd_init;
4562 : : }
4563 : :
4564 : 0 : hns3_tx_push_init(eth_dev);
4565 : :
4566 : : /*
4567 : : * To ensure that the hardware environment is clean during
4568 : : * initialization, the driver actively clear the hardware environment
4569 : : * during initialization, including PF and corresponding VFs' vlan, mac,
4570 : : * flow table configurations, etc.
4571 : : */
4572 : 0 : ret = hns3_clear_hw(hw);
4573 [ # # ]: 0 : if (ret) {
4574 : 0 : PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4575 : 0 : goto err_cmd_init;
4576 : : }
4577 : :
4578 : : hns3_config_all_msix_error(hw, true);
4579 : :
4580 : 0 : ret = rte_intr_callback_register(pci_dev->intr_handle,
4581 : : hns3_interrupt_handler,
4582 : : eth_dev);
4583 [ # # ]: 0 : if (ret) {
4584 : 0 : PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4585 : 0 : goto err_intr_callback_register;
4586 : : }
4587 : :
4588 : : /* Enable interrupt */
4589 : 0 : rte_intr_enable(pci_dev->intr_handle);
4590 : : hns3_pf_enable_irq0(hw);
4591 : :
4592 : : /* Get configuration */
4593 : 0 : ret = hns3_get_configuration(hw);
4594 [ # # ]: 0 : if (ret) {
4595 : 0 : PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4596 : 0 : goto err_get_config;
4597 : : }
4598 : :
4599 : 0 : ret = hns3_stats_init(hw);
4600 [ # # ]: 0 : if (ret)
4601 : 0 : goto err_get_config;
4602 : :
4603 : 0 : ret = hns3_init_hardware(hns);
4604 [ # # ]: 0 : if (ret) {
4605 : 0 : PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4606 : 0 : goto err_init_hw;
4607 : : }
4608 : :
4609 : : /* Initialize flow director filter list & hash */
4610 : 0 : ret = hns3_fdir_filter_init(hns);
4611 [ # # ]: 0 : if (ret) {
4612 : 0 : PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4613 : 0 : goto err_fdir;
4614 : : }
4615 : :
4616 : 0 : hns3_rss_set_default_args(hw);
4617 : :
4618 : 0 : ret = hns3_enable_hw_error_intr(hns, true);
4619 [ # # ]: 0 : if (ret) {
4620 : 0 : PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4621 : : ret);
4622 : 0 : goto err_enable_intr;
4623 : : }
4624 : :
4625 : 0 : ret = hns3_get_port_supported_speed(eth_dev);
4626 [ # # ]: 0 : if (ret) {
4627 : 0 : PMD_INIT_LOG(ERR, "failed to get speed capabilities supported "
4628 : : "by device, ret = %d.", ret);
4629 : 0 : goto err_supported_speed;
4630 : : }
4631 : :
4632 : 0 : hns3_tm_conf_init(eth_dev);
4633 : :
4634 : 0 : return 0;
4635 : :
4636 : : err_supported_speed:
4637 : 0 : (void)hns3_enable_hw_error_intr(hns, false);
4638 : 0 : err_enable_intr:
4639 : 0 : hns3_fdir_filter_uninit(hns);
4640 : 0 : err_fdir:
4641 : 0 : hns3_uninit_hardware(hw);
4642 : 0 : err_init_hw:
4643 : 0 : hns3_stats_uninit(hw);
4644 : 0 : err_get_config:
4645 : : hns3_pf_disable_irq0(hw);
4646 : 0 : rte_intr_disable(pci_dev->intr_handle);
4647 : 0 : hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4648 : : eth_dev);
4649 : 0 : err_intr_callback_register:
4650 : 0 : err_cmd_init:
4651 : 0 : hns3_cmd_uninit(hw);
4652 : 0 : hns3_cmd_destroy_queue(hw);
4653 : 0 : err_cmd_init_queue:
4654 : 0 : hw->io_base = NULL;
4655 : :
4656 : 0 : return ret;
4657 : : }
4658 : :
4659 : : static void
4660 : 0 : hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4661 : : {
4662 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
4663 : 0 : struct rte_pci_device *pci_dev = RTE_CLASS_TO_BUS_DEVICE(eth_dev, *pci_dev);
4664 : 0 : struct hns3_hw *hw = &hns->hw;
4665 : :
4666 : 0 : PMD_INIT_FUNC_TRACE();
4667 : :
4668 : 0 : hns3_tm_conf_uninit(eth_dev);
4669 : 0 : hns3_enable_hw_error_intr(hns, false);
4670 : 0 : hns3_rss_uninit(hns);
4671 : 0 : (void)hns3_config_gro(hw, false);
4672 : 0 : hns3_promisc_uninit(hw);
4673 : 0 : hns3_flow_uninit(eth_dev);
4674 : 0 : hns3_fdir_filter_uninit(hns);
4675 : 0 : hns3_uninit_hardware(hw);
4676 : 0 : hns3_stats_uninit(hw);
4677 : 0 : hns3_config_mac_tnl_int(hw, false);
4678 : : hns3_pf_disable_irq0(hw);
4679 : 0 : rte_intr_disable(pci_dev->intr_handle);
4680 : 0 : hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4681 : : eth_dev);
4682 : : hns3_config_all_msix_error(hw, false);
4683 : 0 : hns3_cmd_uninit(hw);
4684 : 0 : hns3_cmd_destroy_queue(hw);
4685 : 0 : hw->io_base = NULL;
4686 : 0 : }
4687 : :
4688 : : static uint32_t
4689 : : hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds)
4690 : : {
4691 : : uint32_t speed_bit;
4692 : :
4693 [ # # ]: 0 : switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4694 : : case RTE_ETH_LINK_SPEED_10M:
4695 : : speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT;
4696 : : break;
4697 : : case RTE_ETH_LINK_SPEED_10M_HD:
4698 : : speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT;
4699 : : break;
4700 : : case RTE_ETH_LINK_SPEED_100M:
4701 : : speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT;
4702 : : break;
4703 : : case RTE_ETH_LINK_SPEED_100M_HD:
4704 : : speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT;
4705 : : break;
4706 : : case RTE_ETH_LINK_SPEED_1G:
4707 : : speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT;
4708 : : break;
4709 : : default:
4710 : : speed_bit = 0;
4711 : : break;
4712 : : }
4713 : :
4714 : : return speed_bit;
4715 : : }
4716 : :
4717 : : static uint32_t
4718 : : hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds)
4719 : : {
4720 : : uint32_t speed_bit;
4721 : :
4722 [ # # # # : 0 : switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
# # # # ]
4723 : : case RTE_ETH_LINK_SPEED_1G:
4724 : : speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT;
4725 : : break;
4726 : 0 : case RTE_ETH_LINK_SPEED_10G:
4727 : : speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT;
4728 : 0 : break;
4729 : 0 : case RTE_ETH_LINK_SPEED_25G:
4730 : : speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT;
4731 : 0 : break;
4732 : 0 : case RTE_ETH_LINK_SPEED_40G:
4733 : : speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT;
4734 : 0 : break;
4735 : 0 : case RTE_ETH_LINK_SPEED_50G:
4736 : : speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT;
4737 : 0 : break;
4738 : 0 : case RTE_ETH_LINK_SPEED_100G:
4739 : : speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT;
4740 : 0 : break;
4741 : 0 : case RTE_ETH_LINK_SPEED_200G:
4742 : : speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT;
4743 : 0 : break;
4744 : 0 : default:
4745 : : speed_bit = 0;
4746 : 0 : break;
4747 : : }
4748 : :
4749 : : return speed_bit;
4750 : : }
4751 : :
4752 : : static int
4753 : 0 : hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds)
4754 : : {
4755 : : struct hns3_mac *mac = &hw->mac;
4756 : 0 : uint32_t supported_speed = mac->supported_speed;
4757 : : uint32_t speed_bit = 0;
4758 : :
4759 [ # # ]: 0 : if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
4760 : : speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds);
4761 : : else
4762 : : speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds);
4763 : :
4764 [ # # ]: 0 : if (!(speed_bit & supported_speed)) {
4765 : 0 : hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.",
4766 : : link_speeds);
4767 : 0 : return -EINVAL;
4768 : : }
4769 : :
4770 : : return 0;
4771 : : }
4772 : :
4773 : : static uint32_t
4774 : 0 : hns3_get_link_speed(uint32_t link_speeds)
4775 : : {
4776 : : uint32_t speed = RTE_ETH_SPEED_NUM_NONE;
4777 : :
4778 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_10M ||
4779 : : link_speeds & RTE_ETH_LINK_SPEED_10M_HD)
4780 : : speed = RTE_ETH_SPEED_NUM_10M;
4781 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_100M ||
4782 : : link_speeds & RTE_ETH_LINK_SPEED_100M_HD)
4783 : : speed = RTE_ETH_SPEED_NUM_100M;
4784 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_1G)
4785 : : speed = RTE_ETH_SPEED_NUM_1G;
4786 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_10G)
4787 : : speed = RTE_ETH_SPEED_NUM_10G;
4788 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_25G)
4789 : : speed = RTE_ETH_SPEED_NUM_25G;
4790 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_40G)
4791 : : speed = RTE_ETH_SPEED_NUM_40G;
4792 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_50G)
4793 : : speed = RTE_ETH_SPEED_NUM_50G;
4794 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_100G)
4795 : : speed = RTE_ETH_SPEED_NUM_100G;
4796 [ # # ]: 0 : if (link_speeds & RTE_ETH_LINK_SPEED_200G)
4797 : : speed = RTE_ETH_SPEED_NUM_200G;
4798 : :
4799 : 0 : return speed;
4800 : : }
4801 : :
4802 : : static uint8_t
4803 : : hns3_get_link_duplex(uint32_t link_speeds)
4804 : : {
4805 : 0 : if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) ||
4806 : : (link_speeds & RTE_ETH_LINK_SPEED_100M_HD))
4807 : : return RTE_ETH_LINK_HALF_DUPLEX;
4808 : : else
4809 : 0 : return RTE_ETH_LINK_FULL_DUPLEX;
4810 : : }
4811 : :
4812 : : static int
4813 : 0 : hns3_copper_port_link_speed_cfg(struct hns3_hw *hw,
4814 : : struct hns3_set_link_speed_cfg *cfg)
4815 : : {
4816 : : struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4817 : : struct hns3_phy_params_bd0_cmd *req;
4818 : : uint16_t i;
4819 : :
4820 [ # # ]: 0 : for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4821 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4822 : : false);
4823 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4824 : : }
4825 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false);
4826 : : req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4827 : 0 : req->autoneg = cfg->autoneg;
4828 : :
4829 : : /*
4830 : : * The full speed capability is used to negotiate when
4831 : : * auto-negotiation is enabled.
4832 : : */
4833 [ # # ]: 0 : if (cfg->autoneg) {
4834 : 0 : req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT |
4835 : : HNS3_PHY_LINK_SPEED_10M_HD_BIT |
4836 : : HNS3_PHY_LINK_SPEED_100M_BIT |
4837 : : HNS3_PHY_LINK_SPEED_100M_HD_BIT |
4838 : : HNS3_PHY_LINK_SPEED_1000M_BIT;
4839 : : } else {
4840 : 0 : req->speed = cfg->speed;
4841 : 0 : req->duplex = cfg->duplex;
4842 : : }
4843 : :
4844 : 0 : return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4845 : : }
4846 : :
4847 : : static int
4848 : 0 : hns3_set_copper_port_link_speed(struct hns3_hw *hw,
4849 : : struct hns3_set_link_speed_cfg *cfg)
4850 : : {
4851 : : #define HNS3_PHY_PARAM_CFG_RETRY_TIMES 10
4852 : : #define HNS3_PHY_PARAM_CFG_RETRY_DELAY_MS 100
4853 : : uint32_t retry_cnt = 0;
4854 : : int ret;
4855 : :
4856 : : /*
4857 : : * The initialization of copper port contains the following two steps.
4858 : : * 1. Configure firmware takeover the PHY. The firmware will start an
4859 : : * asynchronous task to initialize the PHY chip.
4860 : : * 2. Configure work speed and duplex.
4861 : : * In earlier versions of the firmware, when the asynchronous task is not
4862 : : * finished, the firmware will return -ENOTBLK in the second step. And this
4863 : : * will lead to driver failed to initialize. Here add retry for this case.
4864 : : */
4865 : 0 : ret = hns3_copper_port_link_speed_cfg(hw, cfg);
4866 [ # # # # ]: 0 : while (ret == -ENOTBLK && retry_cnt++ < HNS3_PHY_PARAM_CFG_RETRY_TIMES) {
4867 : : rte_delay_ms(HNS3_PHY_PARAM_CFG_RETRY_DELAY_MS);
4868 : 0 : ret = hns3_copper_port_link_speed_cfg(hw, cfg);
4869 : : }
4870 : :
4871 : 0 : return ret;
4872 : : }
4873 : :
4874 : : static int
4875 : 0 : hns3_set_autoneg(struct hns3_hw *hw, bool enable)
4876 : : {
4877 : : struct hns3_config_auto_neg_cmd *req;
4878 : : struct hns3_cmd_desc desc;
4879 : : uint32_t flag = 0;
4880 : : int ret;
4881 : :
4882 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false);
4883 : :
4884 : : req = (struct hns3_config_auto_neg_cmd *)desc.data;
4885 [ # # ]: 0 : if (enable)
4886 : : hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1);
4887 : 0 : req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag);
4888 : :
4889 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
4890 [ # # ]: 0 : if (ret)
4891 : 0 : hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret);
4892 : :
4893 : 0 : return ret;
4894 : : }
4895 : :
4896 : : static int
4897 : 0 : hns3_set_fiber_port_link_speed(struct hns3_hw *hw,
4898 : : struct hns3_set_link_speed_cfg *cfg)
4899 : : {
4900 : : int ret;
4901 : :
4902 [ # # ]: 0 : if (hw->mac.support_autoneg) {
4903 : 0 : ret = hns3_set_autoneg(hw, cfg->autoneg);
4904 [ # # ]: 0 : if (ret) {
4905 : 0 : hns3_err(hw, "failed to configure auto-negotiation.");
4906 : 0 : return ret;
4907 : : }
4908 : :
4909 : : /*
4910 : : * To enable auto-negotiation, we only need to open the switch
4911 : : * of auto-negotiation, then firmware sets all speed
4912 : : * capabilities.
4913 : : */
4914 [ # # ]: 0 : if (cfg->autoneg)
4915 : : return 0;
4916 : : }
4917 : :
4918 : : /*
4919 : : * Some hardware doesn't support auto-negotiation, but users may not
4920 : : * configure link_speeds (default 0), which means auto-negotiation.
4921 : : * In this case, a warning message need to be printed, instead of
4922 : : * an error.
4923 : : */
4924 [ # # ]: 0 : if (cfg->autoneg) {
4925 : 0 : hns3_warn(hw, "auto-negotiation is not supported, use default fixed speed!");
4926 : 0 : return 0;
4927 : : }
4928 : :
4929 : 0 : return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex);
4930 : : }
4931 : :
4932 : : const char *
4933 : 0 : hns3_get_media_type_name(uint8_t media_type)
4934 : : {
4935 [ # # ]: 0 : if (media_type == HNS3_MEDIA_TYPE_FIBER)
4936 : : return "fiber";
4937 [ # # ]: 0 : else if (media_type == HNS3_MEDIA_TYPE_COPPER)
4938 : : return "copper";
4939 [ # # ]: 0 : else if (media_type == HNS3_MEDIA_TYPE_BACKPLANE)
4940 : : return "backplane";
4941 : : else
4942 : 0 : return "unknown";
4943 : : }
4944 : :
4945 : : static int
4946 : 0 : hns3_set_port_link_speed(struct hns3_hw *hw,
4947 : : struct hns3_set_link_speed_cfg *cfg)
4948 : : {
4949 : : int ret;
4950 : :
4951 [ # # ]: 0 : if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4952 : 0 : ret = hns3_set_copper_port_link_speed(hw, cfg);
4953 : : else
4954 : 0 : ret = hns3_set_fiber_port_link_speed(hw, cfg);
4955 : :
4956 [ # # ]: 0 : if (ret) {
4957 : 0 : hns3_err(hw, "failed to set %s port link speed, ret = %d.",
4958 : : hns3_get_media_type_name(hw->mac.media_type),
4959 : : ret);
4960 : 0 : return ret;
4961 : : }
4962 : :
4963 : : return 0;
4964 : : }
4965 : :
4966 : : static int
4967 : 0 : hns3_apply_link_speed(struct hns3_hw *hw)
4968 : : {
4969 [ # # ]: 0 : struct rte_eth_conf *conf = &hw->data->dev_conf;
4970 : : struct hns3_set_link_speed_cfg cfg;
4971 : :
4972 : : memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg));
4973 : 0 : cfg.autoneg = (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) ?
4974 : 0 : RTE_ETH_LINK_AUTONEG : RTE_ETH_LINK_FIXED;
4975 [ # # ]: 0 : if (cfg.autoneg != RTE_ETH_LINK_AUTONEG) {
4976 [ # # ]: 0 : cfg.speed = hns3_get_link_speed(conf->link_speeds);
4977 : 0 : cfg.duplex = hns3_get_link_duplex(conf->link_speeds);
4978 : : }
4979 : :
4980 : 0 : return hns3_set_port_link_speed(hw, &cfg);
4981 : : }
4982 : :
4983 : : static int
4984 : 0 : hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
4985 : : {
4986 : 0 : struct hns3_hw *hw = &hns->hw;
4987 : : bool link_en;
4988 : : int ret;
4989 : :
4990 : 0 : ret = hns3_update_queue_map_configure(hns);
4991 [ # # ]: 0 : if (ret) {
4992 : 0 : hns3_err(hw, "failed to update queue mapping configuration, ret = %d",
4993 : : ret);
4994 : 0 : return ret;
4995 : : }
4996 : :
4997 : : /* Note: hns3_tm_conf_update must be called after configuring DCB. */
4998 : 0 : ret = hns3_tm_conf_update(hw);
4999 [ # # ]: 0 : if (ret) {
5000 : 0 : PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
5001 : 0 : return ret;
5002 : : }
5003 : :
5004 : 0 : hns3_enable_rxd_adv_layout(hw);
5005 : :
5006 : 0 : ret = hns3_init_queues(hns, reset_queue);
5007 [ # # ]: 0 : if (ret) {
5008 : 0 : PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
5009 : 0 : return ret;
5010 : : }
5011 : :
5012 : 0 : link_en = hw->set_link_down ? false : true;
5013 : 0 : ret = hns3_cfg_mac_mode(hw, link_en);
5014 [ # # ]: 0 : if (ret) {
5015 : 0 : PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5016 : 0 : goto err_config_mac_mode;
5017 : : }
5018 : :
5019 : 0 : ret = hns3_apply_link_speed(hw);
5020 [ # # ]: 0 : if (ret)
5021 : 0 : goto err_set_link_speed;
5022 : :
5023 : 0 : return hns3_restore_filter(hns);
5024 : :
5025 : : err_set_link_speed:
5026 : 0 : (void)hns3_cfg_mac_mode(hw, false);
5027 : :
5028 : 0 : err_config_mac_mode:
5029 : 0 : hns3_dev_release_mbufs(hns);
5030 : : /*
5031 : : * Here is exception handling, hns3_reset_all_tqps will have the
5032 : : * corresponding error message if it is handled incorrectly, so it is
5033 : : * not necessary to check hns3_reset_all_tqps return value, here keep
5034 : : * ret as the error code causing the exception.
5035 : : */
5036 : 0 : (void)hns3_reset_all_tqps(hns);
5037 : 0 : return ret;
5038 : : }
5039 : :
5040 : : static int
5041 : 0 : hns3_dev_start(struct rte_eth_dev *dev)
5042 : : {
5043 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
5044 : 0 : struct hns3_hw *hw = &hns->hw;
5045 : 0 : bool old_state = hw->set_link_down;
5046 : : int ret;
5047 : :
5048 : 0 : PMD_INIT_FUNC_TRACE();
5049 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed))
5050 : : return -EBUSY;
5051 : :
5052 : 0 : rte_spinlock_lock(&hw->lock);
5053 : 0 : hw->adapter_state = HNS3_NIC_STARTING;
5054 : :
5055 : : /*
5056 : : * If the dev_set_link_down() API has been called, the "set_link_down"
5057 : : * flag can be cleared by dev_start() API. In addition, the flag should
5058 : : * also be cleared before calling hns3_do_start() so that MAC can be
5059 : : * enabled in dev_start stage.
5060 : : */
5061 : 0 : hw->set_link_down = false;
5062 : 0 : ret = hns3_do_start(hns, true);
5063 [ # # ]: 0 : if (ret)
5064 : 0 : goto do_start_fail;
5065 : :
5066 : 0 : ret = hns3_map_rx_interrupt(dev);
5067 [ # # ]: 0 : if (ret)
5068 : 0 : goto map_rx_inter_err;
5069 : :
5070 : : /*
5071 : : * There are three register used to control the status of a TQP
5072 : : * (contains a pair of Tx queue and Rx queue) in the new version network
5073 : : * engine. One is used to control the enabling of Tx queue, the other is
5074 : : * used to control the enabling of Rx queue, and the last is the master
5075 : : * switch used to control the enabling of the tqp. The Tx register and
5076 : : * TQP register must be enabled at the same time to enable a Tx queue.
5077 : : * The same applies to the Rx queue. For the older network engine, this
5078 : : * function only refresh the enabled flag, and it is used to update the
5079 : : * status of queue in the dpdk framework.
5080 : : */
5081 : 0 : ret = hns3_start_all_txqs(dev);
5082 [ # # ]: 0 : if (ret)
5083 : 0 : goto start_all_txqs_fail;
5084 : :
5085 : 0 : ret = hns3_start_all_rxqs(dev);
5086 [ # # ]: 0 : if (ret)
5087 : 0 : goto start_all_rxqs_fail;
5088 : :
5089 : 0 : hw->adapter_state = HNS3_NIC_STARTED;
5090 : : rte_spinlock_unlock(&hw->lock);
5091 : :
5092 : 0 : hns3_rx_scattered_calc(dev);
5093 : 0 : hns3_start_rxtx_datapath(dev);
5094 : :
5095 : : /* Enable interrupt of all rx queues before enabling queues */
5096 : 0 : hns3_dev_all_rx_queue_intr_enable(hw, true);
5097 : :
5098 : : /*
5099 : : * After finished the initialization, enable tqps to receive/transmit
5100 : : * packets and refresh all queue status.
5101 : : */
5102 : 0 : hns3_start_tqps(hw);
5103 : :
5104 : 0 : hns3_tm_dev_start_proc(hw);
5105 : :
5106 [ # # ]: 0 : if (dev->data->dev_conf.intr_conf.lsc != 0)
5107 : 0 : hns3_dev_link_update(dev, 0);
5108 : 0 : rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5109 : :
5110 : 0 : hns3_info(hw, "hns3 dev start successful!");
5111 : :
5112 : 0 : return 0;
5113 : :
5114 : : start_all_rxqs_fail:
5115 : 0 : hns3_stop_all_txqs(dev);
5116 : 0 : start_all_txqs_fail:
5117 : 0 : hns3_unmap_rx_interrupt(dev);
5118 : 0 : map_rx_inter_err:
5119 : 0 : (void)hns3_do_stop(hns);
5120 : 0 : do_start_fail:
5121 : 0 : hw->set_link_down = old_state;
5122 : 0 : hw->adapter_state = HNS3_NIC_CONFIGURED;
5123 : : rte_spinlock_unlock(&hw->lock);
5124 : :
5125 : 0 : return ret;
5126 : : }
5127 : :
5128 : : static int
5129 : 0 : hns3_do_stop(struct hns3_adapter *hns)
5130 : : {
5131 : 0 : struct hns3_hw *hw = &hns->hw;
5132 : : int ret;
5133 : :
5134 : : /*
5135 : : * The "hns3_do_stop" function will also be called by .stop_service to
5136 : : * prepare reset. At the time of global or IMP reset, the command cannot
5137 : : * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5138 : : * accessed during the reset process. So the mbuf can not be released
5139 : : * during reset and is required to be released after the reset is
5140 : : * completed.
5141 : : */
5142 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed) == 0)
5143 : 0 : hns3_dev_release_mbufs(hns);
5144 : :
5145 : 0 : ret = hns3_cfg_mac_mode(hw, false);
5146 [ # # ]: 0 : if (ret)
5147 : : return ret;
5148 : 0 : hw->mac.link_status = RTE_ETH_LINK_DOWN;
5149 : :
5150 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.disable_cmd, rte_memory_order_relaxed) == 0) {
5151 : 0 : hns3_configure_all_mac_addr(hns, true);
5152 : 0 : ret = hns3_reset_all_tqps(hns);
5153 [ # # ]: 0 : if (ret) {
5154 : 0 : hns3_err(hw, "failed to reset all queues ret = %d.",
5155 : : ret);
5156 : 0 : return ret;
5157 : : }
5158 : : }
5159 : :
5160 : : return 0;
5161 : : }
5162 : :
5163 : : static int
5164 : 0 : hns3_dev_stop(struct rte_eth_dev *dev)
5165 : : {
5166 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
5167 : 0 : struct hns3_hw *hw = &hns->hw;
5168 : :
5169 : 0 : PMD_INIT_FUNC_TRACE();
5170 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed) != 0) {
5171 : 0 : hns3_warn(hw, "device is resetting, stop operation is not allowed.");
5172 : 0 : return -EBUSY;
5173 : : }
5174 : :
5175 : 0 : dev->data->dev_started = 0;
5176 : :
5177 : 0 : hw->adapter_state = HNS3_NIC_STOPPING;
5178 : 0 : hns3_stop_rxtx_datapath(dev);
5179 : :
5180 : 0 : rte_spinlock_lock(&hw->lock);
5181 : 0 : hns3_tm_dev_stop_proc(hw);
5182 : 0 : hns3_config_mac_tnl_int(hw, false);
5183 : 0 : hns3_stop_tqps(hw);
5184 : 0 : hns3_do_stop(hns);
5185 : 0 : hns3_unmap_rx_interrupt(dev);
5186 : 0 : hw->adapter_state = HNS3_NIC_CONFIGURED;
5187 : 0 : hns3_rx_scattered_reset(dev);
5188 : 0 : rte_eal_alarm_cancel(hns3_service_handler, dev);
5189 : 0 : hns3_stop_report_lse(dev);
5190 : : rte_spinlock_unlock(&hw->lock);
5191 : :
5192 : 0 : return 0;
5193 : : }
5194 : :
5195 : : static int
5196 : 0 : hns3_dev_close(struct rte_eth_dev *eth_dev)
5197 : : {
5198 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
5199 : : struct hns3_hw *hw = &hns->hw;
5200 : : int ret = 0;
5201 : :
5202 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5203 : 0 : hns3_mp_uninit(eth_dev);
5204 : 0 : return 0;
5205 : : }
5206 : :
5207 [ # # ]: 0 : if (hw->adapter_state == HNS3_NIC_STARTED)
5208 : 0 : ret = hns3_dev_stop(eth_dev);
5209 : :
5210 : 0 : hw->adapter_state = HNS3_NIC_CLOSING;
5211 : 0 : hns3_reset_abort(hns);
5212 : 0 : hw->adapter_state = HNS3_NIC_CLOSED;
5213 : :
5214 : 0 : hns3_configure_all_mc_mac_addr(hns, true);
5215 : 0 : hns3_remove_all_vlan_table(hns);
5216 : 0 : hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5217 : 0 : hns3_uninit_pf(eth_dev);
5218 : 0 : hns3_free_all_queues(eth_dev);
5219 : 0 : rte_free(hw->reset.wait_data);
5220 : 0 : hns3_mp_uninit(eth_dev);
5221 : 0 : hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5222 : :
5223 : 0 : return ret;
5224 : : }
5225 : :
5226 : : static void
5227 : : hns3_get_autoneg_rxtx_pause(struct hns3_hw *hw, bool *rx_pause, bool *tx_pause)
5228 : : {
5229 : : struct hns3_mac *mac = &hw->mac;
5230 : 0 : uint32_t advertising = mac->advertising;
5231 : 0 : uint32_t lp_advertising = mac->lp_advertising;
5232 : : *rx_pause = false;
5233 : : *tx_pause = false;
5234 : :
5235 : 0 : if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) {
5236 : : *rx_pause = true;
5237 : : *tx_pause = true;
5238 [ # # ]: 0 : } else if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) {
5239 [ # # ]: 0 : if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5240 : : *rx_pause = true;
5241 [ # # ]: 0 : else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5242 : : *tx_pause = true;
5243 : : }
5244 : : }
5245 : :
5246 : : static enum hns3_fc_mode
5247 [ # # ]: 0 : hns3_get_autoneg_fc_mode(struct hns3_hw *hw)
5248 : : {
5249 : : enum hns3_fc_mode current_mode;
5250 : : bool rx_pause = false;
5251 : : bool tx_pause = false;
5252 : :
5253 : : hns3_get_autoneg_rxtx_pause(hw, &rx_pause, &tx_pause);
5254 : :
5255 [ # # ]: 0 : if (rx_pause && tx_pause)
5256 : : current_mode = HNS3_FC_FULL;
5257 [ # # ]: 0 : else if (rx_pause)
5258 : : current_mode = HNS3_FC_RX_PAUSE;
5259 [ # # ]: 0 : else if (tx_pause)
5260 : : current_mode = HNS3_FC_TX_PAUSE;
5261 : : else
5262 : : current_mode = HNS3_FC_NONE;
5263 : :
5264 : 0 : return current_mode;
5265 : : }
5266 : :
5267 : : static enum hns3_fc_mode
5268 : 0 : hns3_get_current_fc_mode(struct rte_eth_dev *dev)
5269 : : {
5270 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5271 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5272 : : struct hns3_mac *mac = &hw->mac;
5273 : :
5274 : : /*
5275 : : * If the link auto-negotiation of the nic is disabled, or the flow
5276 : : * control auto-negotiation is not supported, the forced flow control
5277 : : * mode is used.
5278 : : */
5279 [ # # # # ]: 0 : if (mac->link_autoneg == 0 || !pf->support_fc_autoneg)
5280 : 0 : return hw->requested_fc_mode;
5281 : :
5282 : : /*
5283 : : * When the flow control mode is obtained, the device may not complete
5284 : : * auto-negotiation. It is necessary to wait for link establishment.
5285 : : */
5286 : 0 : (void)hns3_dev_link_update(dev, 1);
5287 : :
5288 : 0 : return hns3_get_autoneg_fc_mode(hw);
5289 : : }
5290 : :
5291 : : int
5292 : 0 : hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5293 : : {
5294 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5295 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5296 : : enum hns3_fc_mode current_mode;
5297 : :
5298 : 0 : current_mode = hns3_get_current_fc_mode(dev);
5299 [ # # # # ]: 0 : switch (current_mode) {
5300 : 0 : case HNS3_FC_FULL:
5301 : 0 : fc_conf->mode = RTE_ETH_FC_FULL;
5302 : 0 : break;
5303 : 0 : case HNS3_FC_TX_PAUSE:
5304 : 0 : fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
5305 : 0 : break;
5306 : 0 : case HNS3_FC_RX_PAUSE:
5307 : 0 : fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
5308 : 0 : break;
5309 : 0 : case HNS3_FC_NONE:
5310 : : default:
5311 : 0 : fc_conf->mode = RTE_ETH_FC_NONE;
5312 : 0 : break;
5313 : : }
5314 : :
5315 : 0 : fc_conf->pause_time = pf->pause_time;
5316 [ # # ]: 0 : fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0;
5317 : :
5318 : 0 : return 0;
5319 : : }
5320 : :
5321 : : static int
5322 : 0 : hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg)
5323 : : {
5324 : : struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5325 : :
5326 [ # # ]: 0 : if (!pf->support_fc_autoneg) {
5327 [ # # ]: 0 : if (autoneg != 0) {
5328 : 0 : hns3_err(hw, "unsupported fc auto-negotiation.");
5329 : 0 : return -EOPNOTSUPP;
5330 : : }
5331 : :
5332 : : return 0;
5333 : : }
5334 : :
5335 : : /*
5336 : : * If flow control auto-negotiation of the NIC is supported, all
5337 : : * auto-negotiation features are supported.
5338 : : */
5339 [ # # ]: 0 : if (autoneg != hw->mac.link_autoneg) {
5340 : 0 : hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!");
5341 : 0 : return -EOPNOTSUPP;
5342 : : }
5343 : :
5344 : : return 0;
5345 : : }
5346 : :
5347 : : static int
5348 : 0 : hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5349 : : {
5350 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5351 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5352 : : int ret;
5353 : :
5354 [ # # # # ]: 0 : if (fc_conf->high_water || fc_conf->low_water ||
5355 [ # # # # ]: 0 : fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5356 : 0 : hns3_err(hw, "Unsupported flow control settings specified, "
5357 : : "high_water(%u), low_water(%u), send_xon(%u) and "
5358 : : "mac_ctrl_frame_fwd(%u) must be set to '0'",
5359 : : fc_conf->high_water, fc_conf->low_water,
5360 : : fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5361 : 0 : return -EINVAL;
5362 : : }
5363 : :
5364 : 0 : ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg);
5365 [ # # ]: 0 : if (ret)
5366 : : return ret;
5367 : :
5368 [ # # ]: 0 : if (!fc_conf->pause_time) {
5369 : 0 : hns3_err(hw, "Invalid pause time %u setting.",
5370 : : fc_conf->pause_time);
5371 : 0 : return -EINVAL;
5372 : : }
5373 : :
5374 [ # # ]: 0 : if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5375 : : hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5376 : 0 : hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5377 : : "current_fc_status = %d", hw->current_fc_status);
5378 : 0 : return -EOPNOTSUPP;
5379 : : }
5380 : :
5381 [ # # # # ]: 0 : if (hw->dcb_info.num_tc > 1 && !pf->support_multi_tc_pause) {
5382 : 0 : hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
5383 : 0 : return -EOPNOTSUPP;
5384 : : }
5385 : :
5386 : 0 : rte_spinlock_lock(&hw->lock);
5387 : 0 : ret = hns3_fc_enable(dev, fc_conf);
5388 : : rte_spinlock_unlock(&hw->lock);
5389 : :
5390 : 0 : return ret;
5391 : : }
5392 : :
5393 : : static int
5394 : 0 : hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5395 : : struct rte_eth_pfc_conf *pfc_conf)
5396 : : {
5397 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5398 : : int ret;
5399 : :
5400 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB)) {
5401 : 0 : hns3_err(hw, "This port does not support dcb configurations.");
5402 : 0 : return -EOPNOTSUPP;
5403 : : }
5404 : :
5405 [ # # # # ]: 0 : if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5406 [ # # # # ]: 0 : pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5407 : 0 : hns3_err(hw, "Unsupported flow control settings specified, "
5408 : : "high_water(%u), low_water(%u), send_xon(%u) and "
5409 : : "mac_ctrl_frame_fwd(%u) must be set to '0'",
5410 : : pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5411 : : pfc_conf->fc.send_xon,
5412 : : pfc_conf->fc.mac_ctrl_frame_fwd);
5413 : 0 : return -EINVAL;
5414 : : }
5415 [ # # ]: 0 : if (pfc_conf->fc.autoneg) {
5416 : 0 : hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5417 : 0 : return -EINVAL;
5418 : : }
5419 [ # # ]: 0 : if (pfc_conf->fc.pause_time == 0) {
5420 : 0 : hns3_err(hw, "Invalid pause time %u setting.",
5421 : : pfc_conf->fc.pause_time);
5422 : 0 : return -EINVAL;
5423 : : }
5424 : :
5425 [ # # ]: 0 : if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5426 : : hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5427 : 0 : hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5428 : : "current_fc_status = %d", hw->current_fc_status);
5429 : 0 : return -EOPNOTSUPP;
5430 : : }
5431 : :
5432 : 0 : rte_spinlock_lock(&hw->lock);
5433 : 0 : ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5434 : : rte_spinlock_unlock(&hw->lock);
5435 : :
5436 : 0 : return ret;
5437 : : }
5438 : :
5439 : : static int
5440 : 0 : hns3_reinit_dev(struct hns3_adapter *hns)
5441 : : {
5442 : 0 : struct hns3_hw *hw = &hns->hw;
5443 : : int ret;
5444 : :
5445 : 0 : ret = hns3_cmd_init(hw);
5446 [ # # ]: 0 : if (ret) {
5447 : 0 : hns3_err(hw, "Failed to init cmd: %d", ret);
5448 : 0 : return ret;
5449 : : }
5450 : :
5451 : 0 : ret = hns3_init_hardware(hns);
5452 [ # # ]: 0 : if (ret) {
5453 : 0 : hns3_err(hw, "Failed to init hardware: %d", ret);
5454 : 0 : return ret;
5455 : : }
5456 : :
5457 : 0 : ret = hns3_reset_all_tqps(hns);
5458 [ # # ]: 0 : if (ret) {
5459 : 0 : hns3_err(hw, "Failed to reset all queues: %d", ret);
5460 : 0 : return ret;
5461 : : }
5462 : :
5463 : 0 : ret = hns3_enable_hw_error_intr(hns, true);
5464 [ # # ]: 0 : if (ret) {
5465 : 0 : hns3_err(hw, "fail to enable hw error interrupts: %d",
5466 : : ret);
5467 : 0 : return ret;
5468 : : }
5469 : 0 : hns3_info(hw, "Reset done, driver initialization finished.");
5470 : :
5471 : 0 : return 0;
5472 : : }
5473 : :
5474 : : static bool
5475 : 0 : is_pf_reset_done(struct hns3_hw *hw)
5476 : : {
5477 : : uint32_t val, reg, reg_bit;
5478 : :
5479 [ # # ]: 0 : switch (hw->reset.level) {
5480 : : case HNS3_IMP_RESET:
5481 : : reg = HNS3_GLOBAL_RESET_REG;
5482 : : reg_bit = HNS3_IMP_RESET_BIT;
5483 : : break;
5484 : : case HNS3_GLOBAL_RESET:
5485 : : reg = HNS3_GLOBAL_RESET_REG;
5486 : : reg_bit = HNS3_GLOBAL_RESET_BIT;
5487 : : break;
5488 : : case HNS3_FUNC_RESET:
5489 : : reg = HNS3_FUN_RST_ING;
5490 : : reg_bit = HNS3_FUN_RST_ING_B;
5491 : : break;
5492 : 0 : case HNS3_FLR_RESET:
5493 : : default:
5494 : 0 : hns3_err(hw, "Wait for unsupported reset level: %d",
5495 : : hw->reset.level);
5496 : 0 : return true;
5497 : : }
5498 : 0 : val = hns3_read_dev(hw, reg);
5499 [ # # ]: 0 : if (hns3_get_bit(val, reg_bit))
5500 : : return false;
5501 : : else
5502 : 0 : return true;
5503 : : }
5504 : :
5505 : : static enum hns3_reset_level
5506 : : hns3_detect_reset_event(struct hns3_hw *hw)
5507 : : {
5508 : : enum hns3_reset_level new_req = HNS3_NONE_RESET;
5509 : : uint32_t vector0_intr_state;
5510 : :
5511 : 0 : vector0_intr_state = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
5512 [ # # ]: 0 : if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_intr_state)
5513 : : new_req = HNS3_IMP_RESET;
5514 [ # # ]: 0 : else if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_intr_state)
5515 : : new_req = HNS3_GLOBAL_RESET;
5516 : :
5517 : : return new_req;
5518 : : }
5519 : :
5520 : : bool
5521 : 0 : hns3_is_reset_pending(struct hns3_adapter *hns)
5522 : : {
5523 : : enum hns3_reset_level new_req;
5524 : : struct hns3_hw *hw = &hns->hw;
5525 : : enum hns3_reset_level last_req;
5526 : :
5527 : : /*
5528 : : * Only primary can process can process the reset event,
5529 : : * so don't check reset event in secondary.
5530 : : */
5531 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5532 : : return false;
5533 : :
5534 : : new_req = hns3_detect_reset_event(hw);
5535 : : if (new_req == HNS3_NONE_RESET)
5536 : : return false;
5537 : :
5538 : 0 : last_req = hns3_get_reset_level(hns, &hw->reset.pending);
5539 [ # # ]: 0 : if (last_req == HNS3_NONE_RESET || last_req < new_req) {
5540 : 0 : rte_atomic_store_explicit(&hw->reset.disable_cmd, 1, rte_memory_order_relaxed);
5541 : 0 : hns3_schedule_delayed_reset(hns);
5542 : 0 : hns3_warn(hw, "High level reset detected, delay do reset");
5543 : 0 : return true;
5544 : : }
5545 : 0 : last_req = hns3_get_reset_level(hns, &hw->reset.request);
5546 [ # # # # : 0 : if (last_req != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
# # ]
5547 : : hw->reset.level < last_req) {
5548 : 0 : hns3_warn(hw, "High level reset %d is request", last_req);
5549 : 0 : return true;
5550 : : }
5551 : : return false;
5552 : : }
5553 : :
5554 : : static int
5555 : 0 : hns3_wait_hardware_ready(struct hns3_adapter *hns)
5556 : : {
5557 : : struct hns3_hw *hw = &hns->hw;
5558 : 0 : struct hns3_wait_data *wait_data = hw->reset.wait_data;
5559 : : struct timeval tv;
5560 : :
5561 [ # # ]: 0 : if (wait_data->result == HNS3_WAIT_SUCCESS)
5562 : : return 0;
5563 [ # # ]: 0 : else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5564 : 0 : hns3_clock_gettime(&tv);
5565 : 0 : hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5566 : : tv.tv_sec, tv.tv_usec);
5567 : 0 : return -ETIME;
5568 [ # # ]: 0 : } else if (wait_data->result == HNS3_WAIT_REQUEST)
5569 : : return -EAGAIN;
5570 : :
5571 : 0 : wait_data->hns = hns;
5572 : 0 : wait_data->check_completion = is_pf_reset_done;
5573 : 0 : wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5574 : 0 : HNS3_RESET_WAIT_MS + hns3_clock_gettime_ms();
5575 : 0 : wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5576 : 0 : wait_data->count = HNS3_RESET_WAIT_CNT;
5577 : 0 : wait_data->result = HNS3_WAIT_REQUEST;
5578 : 0 : rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5579 : 0 : return -EAGAIN;
5580 : : }
5581 : :
5582 : : static int
5583 : 0 : hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5584 : : {
5585 : : struct hns3_cmd_desc desc;
5586 : : struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5587 : :
5588 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5589 : 0 : hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5590 : 0 : req->fun_reset_vfid = func_id;
5591 : :
5592 : 0 : return hns3_cmd_send(hw, &desc, 1);
5593 : : }
5594 : :
5595 : : static void
5596 : 0 : hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5597 : : {
5598 : : struct hns3_hw *hw = &hns->hw;
5599 : : struct timeval tv;
5600 : : uint32_t val;
5601 : :
5602 : 0 : hns3_clock_gettime(&tv);
5603 [ # # # # ]: 0 : if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5604 : 0 : hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5605 : 0 : hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5606 : : tv.tv_sec, tv.tv_usec);
5607 : 0 : return;
5608 : : }
5609 : :
5610 [ # # # # ]: 0 : switch (reset_level) {
5611 : 0 : case HNS3_IMP_RESET:
5612 : 0 : val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5613 : 0 : hns3_set_bit(val, HNS3_VECTOR0_TRIGGER_IMP_RESET_B, 1);
5614 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
5615 : 0 : hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5616 : : tv.tv_sec, tv.tv_usec);
5617 : 0 : break;
5618 : 0 : case HNS3_GLOBAL_RESET:
5619 : 0 : val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5620 : 0 : hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5621 : 0 : hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5622 : 0 : hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5623 : : tv.tv_sec, tv.tv_usec);
5624 : 0 : break;
5625 : 0 : case HNS3_FUNC_RESET:
5626 : 0 : hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5627 : : tv.tv_sec, tv.tv_usec);
5628 : : /* schedule again to check later */
5629 : 0 : hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5630 : 0 : hns3_schedule_reset(hns);
5631 : 0 : break;
5632 : 0 : default:
5633 : 0 : hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5634 : 0 : return;
5635 : : }
5636 : 0 : hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5637 : : }
5638 : :
5639 : : static enum hns3_reset_level
5640 [ # # ]: 0 : hns3_get_reset_level(struct hns3_adapter *hns, RTE_ATOMIC(uint64_t) *levels)
5641 : : {
5642 : : struct hns3_hw *hw = &hns->hw;
5643 : : enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5644 : :
5645 : : /* Return the highest priority reset level amongst all */
5646 [ # # ]: 0 : if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5647 : : reset_level = HNS3_IMP_RESET;
5648 [ # # ]: 0 : else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5649 : : reset_level = HNS3_GLOBAL_RESET;
5650 [ # # ]: 0 : else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5651 : : reset_level = HNS3_FUNC_RESET;
5652 [ # # ]: 0 : else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5653 : : reset_level = HNS3_FLR_RESET;
5654 : :
5655 [ # # # # ]: 0 : if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5656 : 0 : return HNS3_NONE_RESET;
5657 : :
5658 : : return reset_level;
5659 : : }
5660 : :
5661 : : static void
5662 : 0 : hns3_record_imp_error(struct hns3_adapter *hns)
5663 : : {
5664 : : struct hns3_hw *hw = &hns->hw;
5665 : : uint32_t reg_val;
5666 : :
5667 : 0 : reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5668 [ # # ]: 0 : if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
5669 : 0 : hns3_warn(hw, "Detected IMP RD poison!");
5670 : 0 : hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
5671 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5672 : : }
5673 : :
5674 [ # # ]: 0 : if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
5675 : 0 : hns3_warn(hw, "Detected IMP CMDQ error!");
5676 : 0 : hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
5677 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5678 : : }
5679 : 0 : }
5680 : :
5681 : : static int
5682 : 0 : hns3_prepare_reset(struct hns3_adapter *hns)
5683 : : {
5684 : 0 : struct hns3_hw *hw = &hns->hw;
5685 : : uint32_t reg_val;
5686 : : int ret;
5687 : :
5688 [ # # # ]: 0 : switch (hw->reset.level) {
5689 : 0 : case HNS3_FUNC_RESET:
5690 : 0 : ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5691 [ # # ]: 0 : if (ret)
5692 : : return ret;
5693 : :
5694 : : /*
5695 : : * After performaning pf reset, it is not necessary to do the
5696 : : * mailbox handling or send any command to firmware, because
5697 : : * any mailbox handling or command to firmware is only valid
5698 : : * after hns3_cmd_init is called.
5699 : : */
5700 : 0 : rte_atomic_store_explicit(&hw->reset.disable_cmd, 1, rte_memory_order_relaxed);
5701 : 0 : hw->reset.stats.request_cnt++;
5702 : 0 : break;
5703 : 0 : case HNS3_IMP_RESET:
5704 : 0 : hns3_record_imp_error(hns);
5705 : 0 : reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5706 : 0 : hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5707 : : BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5708 : : break;
5709 : : default:
5710 : : break;
5711 : : }
5712 : : return 0;
5713 : : }
5714 : :
5715 : : static int
5716 : 0 : hns3_set_rst_done(struct hns3_hw *hw)
5717 : : {
5718 : : struct hns3_pf_rst_done_cmd *req;
5719 : : struct hns3_cmd_desc desc;
5720 : :
5721 : : req = (struct hns3_pf_rst_done_cmd *)desc.data;
5722 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5723 : 0 : req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5724 : 0 : return hns3_cmd_send(hw, &desc, 1);
5725 : : }
5726 : :
5727 : : static int
5728 : 0 : hns3_stop_service(struct hns3_adapter *hns)
5729 : : {
5730 : 0 : struct hns3_hw *hw = &hns->hw;
5731 : : struct rte_eth_dev *eth_dev;
5732 : :
5733 : 0 : eth_dev = &rte_eth_devices[hw->data->port_id];
5734 : 0 : hw->mac.link_status = RTE_ETH_LINK_DOWN;
5735 [ # # ]: 0 : if (hw->adapter_state == HNS3_NIC_STARTED) {
5736 : 0 : rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5737 : 0 : hns3_update_linkstatus_and_event(hw, false);
5738 : : }
5739 : 0 : hns3_stop_rxtx_datapath(eth_dev);
5740 : :
5741 : 0 : rte_spinlock_lock(&hw->lock);
5742 [ # # # # ]: 0 : if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5743 : : hw->adapter_state == HNS3_NIC_STOPPING) {
5744 : 0 : hns3_enable_all_queues(hw, false);
5745 : 0 : hns3_do_stop(hns);
5746 : 0 : hw->reset.mbuf_deferred_free = true;
5747 : : } else
5748 : 0 : hw->reset.mbuf_deferred_free = false;
5749 : :
5750 : : /*
5751 : : * It is cumbersome for hardware to pick-and-choose entries for deletion
5752 : : * from table space. Hence, for function reset software intervention is
5753 : : * required to delete the entries
5754 : : */
5755 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.disable_cmd, rte_memory_order_relaxed) == 0)
5756 : 0 : hns3_configure_all_mc_mac_addr(hns, true);
5757 : : rte_spinlock_unlock(&hw->lock);
5758 : :
5759 : 0 : return 0;
5760 : : }
5761 : :
5762 : : static int
5763 : 0 : hns3_start_service(struct hns3_adapter *hns)
5764 : : {
5765 : 0 : struct hns3_hw *hw = &hns->hw;
5766 : : struct rte_eth_dev *eth_dev;
5767 : :
5768 [ # # ]: 0 : if (hw->reset.level == HNS3_IMP_RESET ||
5769 : : hw->reset.level == HNS3_GLOBAL_RESET)
5770 : 0 : hns3_set_rst_done(hw);
5771 : 0 : eth_dev = &rte_eth_devices[hw->data->port_id];
5772 : 0 : hns3_start_rxtx_datapath(eth_dev);
5773 [ # # ]: 0 : if (hw->adapter_state == HNS3_NIC_STARTED) {
5774 : : /*
5775 : : * This API parent function already hold the hns3_hw.lock, the
5776 : : * hns3_service_handler may report lse, in bonding application
5777 : : * it will call driver's ops which may acquire the hns3_hw.lock
5778 : : * again, thus lead to deadlock.
5779 : : * We defer calls hns3_service_handler to avoid the deadlock.
5780 : : */
5781 : 0 : rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
5782 : : hns3_service_handler, eth_dev);
5783 : :
5784 : : /* Enable interrupt of all rx queues before enabling queues */
5785 : 0 : hns3_dev_all_rx_queue_intr_enable(hw, true);
5786 : : /*
5787 : : * Enable state of each rxq and txq will be recovered after
5788 : : * reset, so we need to restore them before enable all tqps;
5789 : : */
5790 : 0 : hns3_restore_tqp_enable_state(hw);
5791 : : /*
5792 : : * When finished the initialization, enable queues to receive
5793 : : * and transmit packets.
5794 : : */
5795 : 0 : hns3_enable_all_queues(hw, true);
5796 : : }
5797 : :
5798 : 0 : return 0;
5799 : : }
5800 : :
5801 : : static int
5802 : 0 : hns3_restore_conf(struct hns3_adapter *hns)
5803 : : {
5804 : 0 : struct hns3_hw *hw = &hns->hw;
5805 : : int ret;
5806 : :
5807 : 0 : ret = hns3_configure_all_mac_addr(hns, false);
5808 [ # # ]: 0 : if (ret)
5809 : : return ret;
5810 : :
5811 : 0 : ret = hns3_configure_all_mc_mac_addr(hns, false);
5812 [ # # ]: 0 : if (ret)
5813 : 0 : goto err_mc_mac;
5814 : :
5815 : 0 : ret = hns3_dev_promisc_restore(hns);
5816 [ # # ]: 0 : if (ret)
5817 : 0 : goto err_promisc;
5818 : :
5819 : 0 : ret = hns3_restore_vlan_table(hns);
5820 [ # # ]: 0 : if (ret)
5821 : 0 : goto err_promisc;
5822 : :
5823 : 0 : ret = hns3_restore_vlan_conf(hns);
5824 [ # # ]: 0 : if (ret)
5825 : 0 : goto err_promisc;
5826 : :
5827 : 0 : ret = hns3_restore_ptp(hns);
5828 [ # # ]: 0 : if (ret)
5829 : 0 : goto err_promisc;
5830 : :
5831 : 0 : ret = hns3_restore_rx_interrupt(hw);
5832 [ # # ]: 0 : if (ret)
5833 : 0 : goto err_promisc;
5834 : :
5835 : 0 : ret = hns3_restore_gro_conf(hw);
5836 [ # # ]: 0 : if (ret)
5837 : 0 : goto err_promisc;
5838 : :
5839 : 0 : ret = hns3_restore_fec(hw);
5840 [ # # ]: 0 : if (ret)
5841 : 0 : goto err_promisc;
5842 : :
5843 [ # # ]: 0 : if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5844 : 0 : ret = hns3_do_start(hns, false);
5845 [ # # ]: 0 : if (ret)
5846 : 0 : goto err_promisc;
5847 : 0 : hns3_info(hw, "hns3 dev restart successful!");
5848 [ # # ]: 0 : } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5849 : 0 : hw->adapter_state = HNS3_NIC_CONFIGURED;
5850 : : return 0;
5851 : :
5852 : 0 : err_promisc:
5853 : 0 : hns3_configure_all_mc_mac_addr(hns, true);
5854 : 0 : err_mc_mac:
5855 : 0 : hns3_configure_all_mac_addr(hns, true);
5856 : 0 : return ret;
5857 : : }
5858 : :
5859 : : static void
5860 : 0 : hns3_reset_service(void *param)
5861 : : {
5862 : : struct hns3_adapter *hns = (struct hns3_adapter *)param;
5863 : : struct hns3_hw *hw = &hns->hw;
5864 : : enum hns3_reset_level reset_level;
5865 : : struct timeval tv_delta;
5866 : : struct timeval tv_start;
5867 : : struct timeval tv;
5868 : : uint64_t msec;
5869 : : int ret;
5870 : :
5871 : : /*
5872 : : * The interrupt is not triggered within the delay time.
5873 : : * The interrupt may have been lost. It is necessary to handle
5874 : : * the interrupt to recover from the error.
5875 : : */
5876 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.schedule, rte_memory_order_relaxed) ==
5877 : : SCHEDULE_DEFERRED) {
5878 : 0 : rte_atomic_store_explicit(&hw->reset.schedule, SCHEDULE_REQUESTED,
5879 : : rte_memory_order_relaxed);
5880 : 0 : hns3_err(hw, "Handling interrupts in delayed tasks");
5881 : 0 : hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
5882 : 0 : reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5883 [ # # ]: 0 : if (reset_level == HNS3_NONE_RESET) {
5884 : 0 : hns3_err(hw, "No reset level is set, try IMP reset");
5885 : : hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
5886 : : }
5887 : : }
5888 : 0 : rte_atomic_store_explicit(&hw->reset.schedule, SCHEDULE_NONE, rte_memory_order_relaxed);
5889 : :
5890 : : /*
5891 : : * Check if there is any ongoing reset in the hardware. This status can
5892 : : * be checked from reset_pending. If there is then, we need to wait for
5893 : : * hardware to complete reset.
5894 : : * a. If we are able to figure out in reasonable time that hardware
5895 : : * has fully resetted then, we can proceed with driver, client
5896 : : * reset.
5897 : : * b. else, we can come back later to check this status so re-sched
5898 : : * now.
5899 : : */
5900 : 0 : reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5901 [ # # ]: 0 : if (reset_level != HNS3_NONE_RESET) {
5902 : 0 : hns3_clock_gettime(&tv_start);
5903 : 0 : ret = hns3_reset_process(hns, reset_level);
5904 : 0 : hns3_clock_gettime(&tv);
5905 [ # # ]: 0 : timersub(&tv, &tv_start, &tv_delta);
5906 : 0 : msec = hns3_clock_calctime_ms(&tv_delta);
5907 [ # # ]: 0 : if (msec > HNS3_RESET_PROCESS_MS)
5908 : 0 : hns3_err(hw, "%d handle long time delta %" PRIu64 " ms time=%ld.%.6ld",
5909 : : hw->reset.level, msec,
5910 : : tv.tv_sec, tv.tv_usec);
5911 [ # # ]: 0 : if (ret == -EAGAIN)
5912 : 0 : return;
5913 : : }
5914 : :
5915 : : /* Check if we got any *new* reset requests to be honored */
5916 : 0 : reset_level = hns3_get_reset_level(hns, &hw->reset.request);
5917 [ # # ]: 0 : if (reset_level != HNS3_NONE_RESET)
5918 : 0 : hns3_msix_process(hns, reset_level);
5919 : : }
5920 : :
5921 : : static uint32_t
5922 : 0 : hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
5923 : : uint32_t speed_capa)
5924 : : {
5925 : : uint32_t speed_bit;
5926 : : uint32_t num = 0;
5927 : : uint32_t i;
5928 : :
5929 [ # # ]: 0 : for (i = 0; i < RTE_DIM(speed_fec_capa_tbl); i++) {
5930 : : speed_bit =
5931 : 0 : rte_eth_speed_bitflag(speed_fec_capa_tbl[i].speed,
5932 : : RTE_ETH_LINK_FULL_DUPLEX);
5933 [ # # ]: 0 : if ((speed_capa & speed_bit) == 0)
5934 : 0 : continue;
5935 : :
5936 : 0 : speed_fec_capa[num].speed = speed_fec_capa_tbl[i].speed;
5937 : 0 : speed_fec_capa[num].capa = speed_fec_capa_tbl[i].capa;
5938 : 0 : num++;
5939 : : }
5940 : :
5941 : 0 : return num;
5942 : : }
5943 : :
5944 : : static int
5945 : 0 : hns3_fec_get_capability(struct rte_eth_dev *dev,
5946 : : struct rte_eth_fec_capa *speed_fec_capa,
5947 : : unsigned int num)
5948 : : {
5949 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5950 : : unsigned int speed_num;
5951 : : uint32_t speed_capa;
5952 : :
5953 : 0 : speed_capa = hns3_get_speed_capa(hw);
5954 : : /* speed_num counts number of speed capabilities */
5955 [ # # ]: 0 : speed_num = rte_popcount32(speed_capa & HNS3_SPEEDS_SUPP_FEC);
5956 [ # # ]: 0 : if (speed_num == 0)
5957 : : return -ENOTSUP;
5958 : :
5959 [ # # ]: 0 : if (speed_fec_capa == NULL)
5960 : : return speed_num;
5961 : :
5962 [ # # ]: 0 : if (num < speed_num) {
5963 : 0 : hns3_err(hw, "not enough array size(%u) to store FEC capabilities, should not be less than %u",
5964 : : num, speed_num);
5965 : 0 : return -EINVAL;
5966 : : }
5967 : :
5968 : 0 : return hns3_get_speed_fec_capa(speed_fec_capa, speed_capa);
5969 : : }
5970 : :
5971 : :
5972 : : static int
5973 : 0 : get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
5974 : : {
5975 : : struct hns3_config_fec_cmd *req;
5976 : : struct hns3_cmd_desc desc;
5977 : : int ret;
5978 : :
5979 : : /*
5980 : : * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
5981 : : * in device of link speed
5982 : : * below 10 Gbps.
5983 : : */
5984 [ # # ]: 0 : if (hw->mac.link_speed < RTE_ETH_SPEED_NUM_10G) {
5985 : 0 : *state = 0;
5986 : 0 : return 0;
5987 : : }
5988 : :
5989 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
5990 : : req = (struct hns3_config_fec_cmd *)desc.data;
5991 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
5992 [ # # ]: 0 : if (ret) {
5993 : 0 : hns3_err(hw, "get current fec auto state failed, ret = %d",
5994 : : ret);
5995 : 0 : return ret;
5996 : : }
5997 : :
5998 : 0 : *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
5999 : 0 : return 0;
6000 : : }
6001 : :
6002 : : static int
6003 : 0 : hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6004 : : {
6005 : : struct hns3_sfp_info_cmd *resp;
6006 : : uint32_t tmp_fec_capa;
6007 : 0 : uint8_t auto_state = 0;
6008 : : struct hns3_cmd_desc desc;
6009 : : int ret;
6010 : :
6011 : : /*
6012 : : * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6013 : : * configured FEC mode is returned.
6014 : : * If link is up, current FEC mode is returned.
6015 : : */
6016 [ # # ]: 0 : if (hw->mac.link_status == RTE_ETH_LINK_DOWN) {
6017 : 0 : ret = get_current_fec_auto_state(hw, &auto_state);
6018 [ # # ]: 0 : if (ret)
6019 : : return ret;
6020 : :
6021 [ # # ]: 0 : if (auto_state == 0x1) {
6022 : 0 : *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6023 : 0 : return 0;
6024 : : }
6025 : : }
6026 : :
6027 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
6028 : : resp = (struct hns3_sfp_info_cmd *)desc.data;
6029 : 0 : resp->query_type = HNS3_ACTIVE_QUERY;
6030 : :
6031 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
6032 [ # # ]: 0 : if (ret == -EOPNOTSUPP) {
6033 : 0 : hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6034 : 0 : return ret;
6035 [ # # ]: 0 : } else if (ret) {
6036 : 0 : hns3_err(hw, "get FEC failed, ret = %d", ret);
6037 : 0 : return ret;
6038 : : }
6039 : :
6040 : : /*
6041 : : * FEC mode order defined in hns3 hardware is inconsistent with
6042 : : * that defined in the ethdev library. So the sequence needs
6043 : : * to be converted.
6044 : : */
6045 [ # # ]: 0 : switch (resp->active_fec) {
6046 : : case HNS3_MAC_FEC_OFF:
6047 : : tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6048 : : break;
6049 : : case HNS3_MAC_FEC_BASER:
6050 : : tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6051 : : break;
6052 : : case HNS3_MAC_FEC_RS:
6053 : : tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6054 : : break;
6055 : : case HNS3_MAC_FEC_LLRS:
6056 : : tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(LLRS);
6057 : : break;
6058 : : default:
6059 : : tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6060 : : break;
6061 : : }
6062 : :
6063 : 0 : *fec_capa = tmp_fec_capa;
6064 : 0 : return 0;
6065 : : }
6066 : :
6067 : : static int
6068 : 0 : hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6069 : : {
6070 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6071 : :
6072 : 0 : return hns3_fec_get_internal(hw, fec_capa);
6073 : : }
6074 : :
6075 : : static int
6076 : 0 : hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6077 : : {
6078 : : struct hns3_config_fec_cmd *req;
6079 : : struct hns3_cmd_desc desc;
6080 : : int ret;
6081 : :
6082 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6083 : :
6084 : : req = (struct hns3_config_fec_cmd *)desc.data;
6085 [ # # # # : 0 : switch (mode) {
# # ]
6086 : 0 : case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6087 : 0 : hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6088 : : HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6089 : 0 : break;
6090 : 0 : case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6091 : 0 : hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6092 : : HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6093 : 0 : break;
6094 : 0 : case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6095 : 0 : hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6096 : : HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6097 : 0 : break;
6098 : 0 : case RTE_ETH_FEC_MODE_CAPA_MASK(LLRS):
6099 : 0 : hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6100 : : HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_LLRS);
6101 : 0 : break;
6102 : 0 : case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6103 : 0 : hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6104 : 0 : break;
6105 : : default:
6106 : : return 0;
6107 : : }
6108 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
6109 [ # # ]: 0 : if (ret)
6110 : 0 : hns3_err(hw, "set fec mode failed, ret = %d", ret);
6111 : :
6112 : : return ret;
6113 : : }
6114 : :
6115 : : static uint32_t
6116 : 0 : hns3_parse_hw_fec_capa(uint8_t hw_fec_capa)
6117 : : {
6118 : : const struct {
6119 : : uint32_t hw_fec_capa;
6120 : : uint32_t fec_capa;
6121 : 0 : } fec_capa_map[] = {
6122 : : { HNS3_FIBER_FEC_AUTO_BIT, RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) },
6123 : : { HNS3_FIBER_FEC_BASER_BIT, RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
6124 : : { HNS3_FIBER_FEC_RS_BIT, RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
6125 : : { HNS3_FIBER_FEC_LLRS_BIT, RTE_ETH_FEC_MODE_CAPA_MASK(LLRS) },
6126 : : { HNS3_FIBER_FEC_NOFEC_BIT, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) },
6127 : : };
6128 : : uint32_t capa = 0;
6129 : : uint32_t i;
6130 : :
6131 [ # # ]: 0 : for (i = 0; i < RTE_DIM(fec_capa_map); i++) {
6132 [ # # ]: 0 : if ((hw_fec_capa & fec_capa_map[i].hw_fec_capa) != 0)
6133 : 0 : capa |= fec_capa_map[i].fec_capa;
6134 : : }
6135 : :
6136 : 0 : return capa;
6137 : : }
6138 : :
6139 : : static uint32_t
6140 : 0 : hns3_get_current_speed_fec_cap(struct hns3_mac *mac)
6141 : : {
6142 : : uint32_t i;
6143 : :
6144 [ # # ]: 0 : if (mac->fec_capa != 0)
6145 : 0 : return hns3_parse_hw_fec_capa(mac->fec_capa);
6146 : :
6147 [ # # ]: 0 : for (i = 0; i < RTE_DIM(speed_fec_capa_tbl); i++) {
6148 [ # # ]: 0 : if (mac->link_speed == speed_fec_capa_tbl[i].speed)
6149 : 0 : return speed_fec_capa_tbl[i].capa;
6150 : : }
6151 : :
6152 : : return 0;
6153 : : }
6154 : :
6155 : : static int
6156 : 0 : hns3_fec_mode_valid(struct rte_eth_dev *dev, uint32_t mode)
6157 : : {
6158 [ # # ]: 0 : struct hns3_adapter *hns = dev->data->dev_private;
6159 : : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6160 : : uint32_t cur_capa;
6161 : :
6162 [ # # ]: 0 : if (rte_popcount32(mode) != 1) {
6163 : 0 : hns3_err(hw, "FEC mode(0x%x) should be only one bit set", mode);
6164 : 0 : return -EINVAL;
6165 : : }
6166 : :
6167 : : /*
6168 : : * Check whether the configured mode is within the FEC capability.
6169 : : * If not, the configured mode will not be supported.
6170 : : */
6171 : 0 : cur_capa = hns3_get_current_speed_fec_cap(&hw->mac);
6172 [ # # ]: 0 : if ((cur_capa & mode) == 0) {
6173 : 0 : hns3_err(hw, "unsupported FEC mode(0x%x)", mode);
6174 : 0 : return -EINVAL;
6175 : : }
6176 : :
6177 : : return 0;
6178 : : }
6179 : :
6180 : : static int
6181 : 0 : hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6182 : : {
6183 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
6184 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6185 : : struct hns3_pf *pf = &hns->pf;
6186 : : int ret;
6187 : :
6188 : 0 : ret = hns3_fec_mode_valid(dev, mode);
6189 [ # # ]: 0 : if (ret != 0)
6190 : : return ret;
6191 : :
6192 : 0 : rte_spinlock_lock(&hw->lock);
6193 : 0 : ret = hns3_set_fec_hw(hw, mode);
6194 [ # # ]: 0 : if (ret) {
6195 : : rte_spinlock_unlock(&hw->lock);
6196 : 0 : return ret;
6197 : : }
6198 : :
6199 : 0 : pf->fec_mode = mode;
6200 : : rte_spinlock_unlock(&hw->lock);
6201 : :
6202 : 0 : return 0;
6203 : : }
6204 : :
6205 : : static int
6206 : 0 : hns3_restore_fec(struct hns3_hw *hw)
6207 : : {
6208 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6209 : : struct hns3_pf *pf = &hns->pf;
6210 : 0 : uint32_t mode = pf->fec_mode;
6211 : : int ret;
6212 : :
6213 : 0 : ret = hns3_set_fec_hw(hw, mode);
6214 [ # # ]: 0 : if (ret)
6215 : 0 : hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6216 : : mode, ret);
6217 : :
6218 : 0 : return ret;
6219 : : }
6220 : :
6221 : : static int
6222 : 0 : hns3_query_dev_fec_info(struct hns3_hw *hw)
6223 : : {
6224 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6225 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6226 : : int ret;
6227 : :
6228 : 0 : ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6229 [ # # ]: 0 : if (ret)
6230 : 0 : hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6231 : :
6232 : 0 : return ret;
6233 : : }
6234 : :
6235 : : static bool
6236 : 0 : hns3_optical_module_existed(struct hns3_hw *hw)
6237 : : {
6238 : : struct hns3_cmd_desc desc;
6239 : : bool existed;
6240 : : int ret;
6241 : :
6242 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6243 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
6244 [ # # ]: 0 : if (ret) {
6245 : 0 : hns3_err(hw,
6246 : : "fail to get optical module exist state, ret = %d.",
6247 : : ret);
6248 : 0 : return false;
6249 : : }
6250 : 0 : existed = !!desc.data[0];
6251 : :
6252 : 0 : return existed;
6253 : : }
6254 : :
6255 : : static int
6256 : 0 : hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6257 : : uint32_t len, uint8_t *data)
6258 : : {
6259 : : #define HNS3_SFP_INFO_CMD_NUM 6
6260 : : #define HNS3_SFP_INFO_MAX_LEN \
6261 : : (HNS3_SFP_INFO_BD0_LEN + \
6262 : : (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6263 : : struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6264 : : struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6265 : : uint16_t read_len;
6266 : : uint16_t copy_len;
6267 : : int ret;
6268 : : int i;
6269 : :
6270 [ # # ]: 0 : for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6271 : 0 : hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6272 : : true);
6273 [ # # ]: 0 : if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6274 : 0 : desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6275 : : }
6276 : :
6277 : : sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6278 : 0 : sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6279 : 0 : read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6280 : 0 : sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6281 : :
6282 : 0 : ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6283 [ # # ]: 0 : if (ret) {
6284 : 0 : hns3_err(hw, "fail to get module EEPROM info, ret = %d.",
6285 : : ret);
6286 : 0 : return ret;
6287 : : }
6288 : :
6289 : : /* The data format in BD0 is different with the others. */
6290 : 0 : copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6291 : : memcpy(data, sfp_info_bd0->data, copy_len);
6292 : : read_len = copy_len;
6293 : :
6294 [ # # ]: 0 : for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6295 [ # # ]: 0 : if (read_len >= len)
6296 : : break;
6297 : :
6298 : 0 : copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6299 : 0 : memcpy(data + read_len, desc[i].data, copy_len);
6300 : 0 : read_len += copy_len;
6301 : : }
6302 : :
6303 : 0 : return (int)read_len;
6304 : : }
6305 : :
6306 : : static int
6307 : 0 : hns3_get_module_eeprom(struct rte_eth_dev *dev,
6308 : : struct rte_dev_eeprom_info *info)
6309 : : {
6310 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
6311 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6312 : 0 : uint32_t offset = info->offset;
6313 : 0 : uint32_t len = info->length;
6314 : 0 : uint8_t *data = info->data;
6315 : : uint32_t read_len = 0;
6316 : :
6317 [ # # ]: 0 : if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6318 : : return -ENOTSUP;
6319 : :
6320 [ # # ]: 0 : if (!hns3_optical_module_existed(hw)) {
6321 : 0 : hns3_err(hw, "fail to read module EEPROM: no module is connected.");
6322 : 0 : return -EIO;
6323 : : }
6324 : :
6325 [ # # ]: 0 : while (read_len < len) {
6326 : : int ret;
6327 : 0 : ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6328 : : len - read_len,
6329 : : data + read_len);
6330 [ # # ]: 0 : if (ret < 0)
6331 : : return -EIO;
6332 : 0 : read_len += ret;
6333 : : }
6334 : :
6335 : : return 0;
6336 : : }
6337 : :
6338 : : static int
6339 : 0 : hns3_get_module_info(struct rte_eth_dev *dev,
6340 : : struct rte_eth_dev_module_info *modinfo)
6341 : : {
6342 : : #define HNS3_SFF8024_ID_SFP 0x03
6343 : : #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6344 : : #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6345 : : #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6346 : : #define HNS3_SFF_8636_V1_3 0x03
6347 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
6348 : : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6349 : : struct rte_dev_eeprom_info info;
6350 : : struct hns3_sfp_type sfp_type;
6351 : : int ret;
6352 : :
6353 : : memset(&sfp_type, 0, sizeof(sfp_type));
6354 : : memset(&info, 0, sizeof(info));
6355 : 0 : info.data = (uint8_t *)&sfp_type;
6356 : 0 : info.length = sizeof(sfp_type);
6357 : 0 : ret = hns3_get_module_eeprom(dev, &info);
6358 [ # # ]: 0 : if (ret)
6359 : : return ret;
6360 : :
6361 [ # # # # : 0 : switch (sfp_type.type) {
# ]
6362 : 0 : case HNS3_SFF8024_ID_SFP:
6363 : 0 : modinfo->type = RTE_ETH_MODULE_SFF_8472;
6364 : 0 : modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6365 : 0 : break;
6366 : 0 : case HNS3_SFF8024_ID_QSFP_8438:
6367 : 0 : modinfo->type = RTE_ETH_MODULE_SFF_8436;
6368 : 0 : modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6369 : 0 : break;
6370 : 0 : case HNS3_SFF8024_ID_QSFP_8436_8636:
6371 [ # # ]: 0 : if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6372 : 0 : modinfo->type = RTE_ETH_MODULE_SFF_8436;
6373 : 0 : modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6374 : : } else {
6375 : 0 : modinfo->type = RTE_ETH_MODULE_SFF_8636;
6376 : 0 : modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6377 : : }
6378 : : break;
6379 : 0 : case HNS3_SFF8024_ID_QSFP28_8636:
6380 : 0 : modinfo->type = RTE_ETH_MODULE_SFF_8636;
6381 : 0 : modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6382 : 0 : break;
6383 : 0 : default:
6384 : 0 : hns3_err(hw, "unknown module, type = %u, extra_type = %u.",
6385 : : sfp_type.type, sfp_type.ext_type);
6386 : 0 : return -EINVAL;
6387 : : }
6388 : :
6389 : : return 0;
6390 : : }
6391 : :
6392 : : static const struct eth_dev_ops hns3_eth_dev_ops = {
6393 : : .dev_configure = hns3_dev_configure,
6394 : : .dev_start = hns3_dev_start,
6395 : : .dev_stop = hns3_dev_stop,
6396 : : .dev_close = hns3_dev_close,
6397 : : .promiscuous_enable = hns3_dev_promiscuous_enable,
6398 : : .promiscuous_disable = hns3_dev_promiscuous_disable,
6399 : : .allmulticast_enable = hns3_dev_allmulticast_enable,
6400 : : .allmulticast_disable = hns3_dev_allmulticast_disable,
6401 : : .mtu_set = hns3_dev_mtu_set,
6402 : : .stats_get = hns3_stats_get,
6403 : : .stats_reset = hns3_stats_reset,
6404 : : .xstats_get = hns3_dev_xstats_get,
6405 : : .xstats_get_names = hns3_dev_xstats_get_names,
6406 : : .xstats_reset = hns3_dev_xstats_reset,
6407 : : .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6408 : : .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6409 : : .dev_infos_get = hns3_dev_infos_get,
6410 : : .fw_version_get = hns3_fw_version_get,
6411 : : .rx_queue_setup = hns3_rx_queue_setup,
6412 : : .tx_queue_setup = hns3_tx_queue_setup,
6413 : : .rx_queue_release = hns3_dev_rx_queue_release,
6414 : : .tx_queue_release = hns3_dev_tx_queue_release,
6415 : : .rx_queue_start = hns3_dev_rx_queue_start,
6416 : : .rx_queue_stop = hns3_dev_rx_queue_stop,
6417 : : .tx_queue_start = hns3_dev_tx_queue_start,
6418 : : .tx_queue_stop = hns3_dev_tx_queue_stop,
6419 : : .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6420 : : .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6421 : : .rxq_info_get = hns3_rxq_info_get,
6422 : : .txq_info_get = hns3_txq_info_get,
6423 : : .rx_burst_mode_get = hns3_rx_burst_mode_get,
6424 : : .tx_burst_mode_get = hns3_tx_burst_mode_get,
6425 : : .flow_ctrl_get = hns3_flow_ctrl_get,
6426 : : .flow_ctrl_set = hns3_flow_ctrl_set,
6427 : : .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6428 : : .mac_addr_add = hns3_add_mac_addr,
6429 : : .mac_addr_remove = hns3_remove_mac_addr,
6430 : : .mac_addr_set = hns3_set_default_mac_addr,
6431 : : .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6432 : : .link_update = hns3_dev_link_update,
6433 : : .dev_set_link_up = hns3_dev_set_link_up,
6434 : : .dev_set_link_down = hns3_dev_set_link_down,
6435 : : .rss_hash_update = hns3_dev_rss_hash_update,
6436 : : .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6437 : : .reta_update = hns3_dev_rss_reta_update,
6438 : : .reta_query = hns3_dev_rss_reta_query,
6439 : : .flow_ops_get = hns3_dev_flow_ops_get,
6440 : : .vlan_filter_set = hns3_vlan_filter_set,
6441 : : .vlan_tpid_set = hns3_vlan_tpid_set,
6442 : : .vlan_offload_set = hns3_vlan_offload_set,
6443 : : .vlan_pvid_set = hns3_vlan_pvid_set,
6444 : : .get_reg = hns3_get_regs,
6445 : : .get_module_info = hns3_get_module_info,
6446 : : .get_module_eeprom = hns3_get_module_eeprom,
6447 : : .get_dcb_info = hns3_get_dcb_info,
6448 : : .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6449 : : .fec_get_capability = hns3_fec_get_capability,
6450 : : .fec_get = hns3_fec_get,
6451 : : .fec_set = hns3_fec_set,
6452 : : .tm_ops_get = hns3_tm_ops_get,
6453 : : .tx_done_cleanup = hns3_tx_done_cleanup,
6454 : : .timesync_enable = hns3_timesync_enable,
6455 : : .timesync_disable = hns3_timesync_disable,
6456 : : .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp,
6457 : : .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp,
6458 : : .timesync_adjust_time = hns3_timesync_adjust_time,
6459 : : .timesync_read_time = hns3_timesync_read_time,
6460 : : .timesync_write_time = hns3_timesync_write_time,
6461 : : .eth_dev_priv_dump = hns3_eth_dev_priv_dump,
6462 : : .eth_rx_descriptor_dump = hns3_rx_descriptor_dump,
6463 : : .eth_tx_descriptor_dump = hns3_tx_descriptor_dump,
6464 : : .get_monitor_addr = hns3_get_monitor_addr,
6465 : : };
6466 : :
6467 : : static const struct hns3_reset_ops hns3_reset_ops = {
6468 : : .reset_service = hns3_reset_service,
6469 : : .stop_service = hns3_stop_service,
6470 : : .prepare_reset = hns3_prepare_reset,
6471 : : .wait_hardware_ready = hns3_wait_hardware_ready,
6472 : : .reinit_dev = hns3_reinit_dev,
6473 : : .restore_conf = hns3_restore_conf,
6474 : : .start_service = hns3_start_service,
6475 : : };
6476 : :
6477 : : static void
6478 : : hns3_init_hw_ops(struct hns3_hw *hw)
6479 : : {
6480 : 0 : hw->ops.add_mc_mac_addr = hns3_add_mc_mac_addr;
6481 : 0 : hw->ops.del_mc_mac_addr = hns3_remove_mc_mac_addr;
6482 : 0 : hw->ops.add_uc_mac_addr = hns3_add_uc_mac_addr;
6483 : 0 : hw->ops.del_uc_mac_addr = hns3_remove_uc_mac_addr;
6484 : 0 : hw->ops.bind_ring_with_vector = hns3_bind_ring_with_vector;
6485 : : }
6486 : :
6487 : : static int
6488 : 0 : hns3_dev_init(struct rte_eth_dev *eth_dev)
6489 : : {
6490 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
6491 : 0 : struct hns3_hw *hw = &hns->hw;
6492 : : int ret;
6493 : :
6494 : 0 : PMD_INIT_FUNC_TRACE();
6495 : :
6496 : 0 : hns3_flow_init(eth_dev);
6497 : :
6498 : 0 : hns3_set_rxtx_function(eth_dev);
6499 : 0 : eth_dev->dev_ops = &hns3_eth_dev_ops;
6500 : 0 : eth_dev->rx_queue_count = hns3_rx_queue_count;
6501 : 0 : ret = hns3_mp_init(eth_dev);
6502 [ # # ]: 0 : if (ret)
6503 : 0 : goto err_mp_init;
6504 : :
6505 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6506 : 0 : hns3_tx_push_init(eth_dev);
6507 : 0 : return 0;
6508 : : }
6509 : :
6510 : 0 : hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6511 : 0 : hns->is_vf = false;
6512 : 0 : hw->data = eth_dev->data;
6513 : 0 : hns3_parse_devargs(eth_dev);
6514 : :
6515 : : /*
6516 : : * Set default max packet size according to the mtu
6517 : : * default vale in DPDK frame.
6518 : : */
6519 : 0 : hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6520 : :
6521 : 0 : ret = hns3_reset_init(hw);
6522 [ # # ]: 0 : if (ret)
6523 : 0 : goto err_init_reset;
6524 : 0 : hw->reset.ops = &hns3_reset_ops;
6525 : :
6526 : : hns3_init_hw_ops(hw);
6527 : 0 : ret = hns3_init_pf(eth_dev);
6528 [ # # ]: 0 : if (ret) {
6529 : 0 : PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6530 : 0 : goto err_init_pf;
6531 : : }
6532 : :
6533 : 0 : ret = hns3_init_mac_addrs(eth_dev);
6534 [ # # ]: 0 : if (ret != 0)
6535 : 0 : goto err_init_mac_addrs;
6536 : :
6537 : 0 : hw->adapter_state = HNS3_NIC_INITIALIZED;
6538 : :
6539 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.schedule, rte_memory_order_relaxed) ==
6540 : : SCHEDULE_PENDING) {
6541 : 0 : hns3_err(hw, "Reschedule reset service after dev_init");
6542 : 0 : hns3_schedule_reset(hns);
6543 : : } else {
6544 : : /* IMP will wait ready flag before reset */
6545 : 0 : hns3_notify_reset_ready(hw, false);
6546 : : }
6547 : :
6548 : 0 : hns3_info(hw, "hns3 dev initialization successful!");
6549 : 0 : return 0;
6550 : :
6551 : : err_init_mac_addrs:
6552 : 0 : hns3_uninit_pf(eth_dev);
6553 : :
6554 : 0 : err_init_pf:
6555 : 0 : rte_free(hw->reset.wait_data);
6556 : :
6557 : 0 : err_init_reset:
6558 : 0 : hns3_mp_uninit(eth_dev);
6559 : :
6560 : 0 : err_mp_init:
6561 : 0 : eth_dev->dev_ops = NULL;
6562 : 0 : eth_dev->rx_pkt_burst = NULL;
6563 : 0 : eth_dev->rx_descriptor_status = NULL;
6564 : 0 : eth_dev->tx_pkt_burst = NULL;
6565 : 0 : eth_dev->tx_pkt_prepare = NULL;
6566 : 0 : eth_dev->tx_descriptor_status = NULL;
6567 : 0 : return ret;
6568 : : }
6569 : :
6570 : : static int
6571 : 0 : hns3_dev_uninit(struct rte_eth_dev *eth_dev)
6572 : : {
6573 : 0 : struct hns3_adapter *hns = eth_dev->data->dev_private;
6574 : : struct hns3_hw *hw = &hns->hw;
6575 : :
6576 : 0 : PMD_INIT_FUNC_TRACE();
6577 : :
6578 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6579 : 0 : hns3_mp_uninit(eth_dev);
6580 : 0 : return 0;
6581 : : }
6582 : :
6583 [ # # ]: 0 : if (hw->adapter_state < HNS3_NIC_CLOSING)
6584 : 0 : hns3_dev_close(eth_dev);
6585 : :
6586 : 0 : hw->adapter_state = HNS3_NIC_REMOVED;
6587 : 0 : return 0;
6588 : : }
6589 : :
6590 : : static int
6591 : 0 : eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
6592 : : struct rte_pci_device *pci_dev)
6593 : : {
6594 : 0 : return rte_eth_dev_pci_generic_probe(pci_dev,
6595 : : sizeof(struct hns3_adapter),
6596 : : hns3_dev_init);
6597 : : }
6598 : :
6599 : : static int
6600 : 0 : eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
6601 : : {
6602 : 0 : return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
6603 : : }
6604 : :
6605 : : static const struct rte_pci_id pci_id_hns3_map[] = {
6606 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
6607 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
6608 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
6609 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
6610 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
6611 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
6612 : : { .vendor_id = 0, }, /* sentinel */
6613 : : };
6614 : :
6615 : : static struct rte_pci_driver rte_hns3_pmd = {
6616 : : .id_table = pci_id_hns3_map,
6617 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
6618 : : .probe = eth_hns3_pci_probe,
6619 : : .remove = eth_hns3_pci_remove,
6620 : : };
6621 : :
6622 : 306 : RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
6623 : : RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
6624 : : RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
6625 : : RTE_PMD_REGISTER_PARAM_STRING(net_hns3,
6626 : : HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
6627 : : HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
6628 : : HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> "
6629 : : HNS3_DEVARG_MBX_TIME_LIMIT_MS "=<uint16> "
6630 : : HNS3_DEVARG_FDIR_VLAN_MATCH_MODE "=strict|nostrict "
6631 : : HNS3_DEVARG_FDIR_TUPLE_CONFIG "=+outvlan-insmac|+outvlan-indmac|"
6632 : : "+outvlan-insip|+outvlan-indip"
6633 : : "+outvlan-sctptag|+outvlan-tunvni "
6634 : : HNS3_DEVARG_FDIR_INDEX_CONFIG "=hash|priority ");
6635 [ - + ]: 306 : RTE_LOG_REGISTER_SUFFIX(hns3_logtype_init, init, NOTICE);
6636 [ - + ]: 306 : RTE_LOG_REGISTER_SUFFIX(hns3_logtype_driver, driver, NOTICE);
6637 : : #ifdef RTE_ETHDEV_DEBUG_RX
6638 : : RTE_LOG_REGISTER_SUFFIX(hns3_logtype_rx, rx, DEBUG);
6639 : : #endif
6640 : : #ifdef RTE_ETHDEV_DEBUG_TX
6641 : : RTE_LOG_REGISTER_SUFFIX(hns3_logtype_tx, tx, DEBUG);
6642 : : #endif
|