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