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