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