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