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