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