net/hinic: set link down and up
[dpdk.git] / drivers / net / hinic / base / hinic_pmd_niccfg.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Huawei Technologies Co., Ltd
3  */
4
5 #include "hinic_compat.h"
6 #include "hinic_pmd_hwdev.h"
7 #include "hinic_pmd_hwif.h"
8 #include "hinic_pmd_eqs.h"
9 #include "hinic_pmd_wq.h"
10 #include "hinic_pmd_mgmt.h"
11 #include "hinic_pmd_cmdq.h"
12 #include "hinic_pmd_niccfg.h"
13 #include "hinic_pmd_mbox.h"
14
15 #define l2nic_msg_to_mgmt_sync(hwdev, cmd, buf_in,              \
16                                in_size, buf_out, out_size)      \
17         hinic_msg_to_mgmt_sync(hwdev, HINIC_MOD_L2NIC, cmd,     \
18                         buf_in, in_size,                        \
19                         buf_out, out_size, 0)
20
21
22 #define TCAM_SET        0x1
23 #define TCAM_CLEAR      0x2
24
25 struct hinic_port_qfilter_info {
26         struct hinic_mgmt_msg_head mgmt_msg_head;
27
28         u16 func_id;
29         u8 normal_type_enable;
30         u8 filter_type_enable;
31         u8 filter_enable;
32         u8 filter_type;
33         u8 qid;
34         u8 fdir_flag;
35         u32 key;
36 };
37
38 /**
39  * hinic_init_function_table - Initialize function table.
40  *
41  * @param hwdev
42  *   The hardware interface of a nic device.
43  * @param rx_buf_sz
44  *   Receive buffer size.
45  *
46  * @return
47  *   0 on success.
48  *   negative error value otherwise.
49  */
50 int hinic_init_function_table(void *hwdev, u16 rx_buf_sz)
51 {
52         struct hinic_function_table function_table;
53         u16 out_size = sizeof(function_table);
54         int err;
55
56         if (!hwdev) {
57                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
58                 return -EINVAL;
59         }
60
61         memset(&function_table, 0, sizeof(function_table));
62         function_table.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
63         function_table.func_id = hinic_global_func_id(hwdev);
64         function_table.mtu = 0x3FFF;    /* default, max mtu */
65         function_table.rx_wqe_buf_size = rx_buf_sz;
66
67         err = hinic_msg_to_mgmt_sync(hwdev, HINIC_MOD_L2NIC,
68                                      HINIC_PORT_CMD_INIT_FUNC,
69                                      &function_table, sizeof(function_table),
70                                      &function_table, &out_size, 0);
71         if (err || function_table.mgmt_msg_head.status || !out_size) {
72                 PMD_DRV_LOG(ERR,
73                         "Failed to init func table, err: %d, status: 0x%x, out size: 0x%x",
74                         err, function_table.mgmt_msg_head.status, out_size);
75                 return -EFAULT;
76         }
77
78         return 0;
79 }
80
81 /**
82  * hinic_get_base_qpn - Get global queue number.
83  *
84  * @param hwdev
85  *   The hardware interface of a nic device.
86  * @param global_qpn
87  *   Global queue number.
88  *
89  * @return
90  *   0 on success.
91  *   negative error value otherwise.
92  */
93 int hinic_get_base_qpn(void *hwdev, u16 *global_qpn)
94 {
95         struct hinic_cmd_qpn cmd_qpn;
96         u16 out_size = sizeof(cmd_qpn);
97         int err;
98
99         if (!hwdev || !global_qpn) {
100                 PMD_DRV_LOG(ERR, "Hwdev or global_qpn is NULL");
101                 return -EINVAL;
102         }
103
104         memset(&cmd_qpn, 0, sizeof(cmd_qpn));
105         cmd_qpn.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
106         cmd_qpn.func_id = hinic_global_func_id(hwdev);
107
108         err = hinic_msg_to_mgmt_sync(hwdev, HINIC_MOD_L2NIC,
109                                      HINIC_PORT_CMD_GET_GLOBAL_QPN,
110                                      &cmd_qpn, sizeof(cmd_qpn), &cmd_qpn,
111                                      &out_size, 0);
112         if (err || !out_size || cmd_qpn.mgmt_msg_head.status) {
113                 PMD_DRV_LOG(ERR,
114                         "Failed to get base qpn, err: %d, status: 0x%x, out size: 0x%x",
115                         err, cmd_qpn.mgmt_msg_head.status, out_size);
116                 return -EINVAL;
117         }
118
119         *global_qpn = cmd_qpn.base_qpn;
120
121         return 0;
122 }
123
124 /**
125  * hinic_set_mac - Init mac_vlan table in NIC.
126  *
127  * @param hwdev
128  *   The hardware interface of a nic device.
129  * @param mac_addr
130  *   MAC address.
131  * @param vlan_id
132  *   Set 0 for mac_vlan table initialization.
133  * @param func_id
134  *   Global function id of NIC.
135  *
136  * @return
137  *   0 on success.
138  *   negative error value otherwise.
139  */
140 int hinic_set_mac(void *hwdev, u8 *mac_addr, u16 vlan_id, u16 func_id)
141 {
142         struct hinic_port_mac_set mac_info;
143         u16 out_size = sizeof(mac_info);
144         int err;
145
146         if (!hwdev || !mac_addr) {
147                 PMD_DRV_LOG(ERR, "Hwdev or mac_addr is NULL");
148                 return -EINVAL;
149         }
150
151         memset(&mac_info, 0, sizeof(mac_info));
152         mac_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
153         mac_info.func_id = func_id;
154         mac_info.vlan_id = vlan_id;
155         memmove(mac_info.mac, mac_addr, ETH_ALEN);
156
157         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info,
158                                      sizeof(mac_info), &mac_info, &out_size);
159         if (err || !out_size || (mac_info.mgmt_msg_head.status &&
160             mac_info.mgmt_msg_head.status != HINIC_PF_SET_VF_ALREADY)) {
161                 PMD_DRV_LOG(ERR, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x",
162                         err, mac_info.mgmt_msg_head.status, out_size);
163                 return -EINVAL;
164         }
165
166         if (mac_info.mgmt_msg_head.status == HINIC_PF_SET_VF_ALREADY) {
167                 PMD_DRV_LOG(WARNING, "PF has already set vf mac, Ignore set operation.");
168                 return HINIC_PF_SET_VF_ALREADY;
169         }
170
171         return 0;
172 }
173
174 /**
175  * hinic_del_mac - Uninit mac_vlan table in NIC.
176  *
177  * @param hwdev
178  *   The hardware interface of a nic device.
179  * @param mac_addr
180  *   MAC address.
181  * @param vlan_id
182  *   Set 0 for mac_vlan table initialization.
183  * @param func_id
184  *   Global function id of NIC.
185  *
186  * @return
187  *   0 on success.
188  *   negative error value otherwise.
189  */
190 int hinic_del_mac(void *hwdev, u8 *mac_addr, u16 vlan_id, u16 func_id)
191 {
192         struct hinic_port_mac_set mac_info;
193         u16 out_size = sizeof(mac_info);
194         int err;
195
196         if (!hwdev || !mac_addr) {
197                 PMD_DRV_LOG(ERR, "Hwdev or mac_addr is NULL");
198                 return -EINVAL;
199         }
200
201         if (vlan_id >= VLAN_N_VID) {
202                 PMD_DRV_LOG(ERR, "Invalid VLAN number");
203                 return -EINVAL;
204         }
205
206         memset(&mac_info, 0, sizeof(mac_info));
207         mac_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
208         mac_info.func_id = func_id;
209         mac_info.vlan_id = vlan_id;
210         memmove(mac_info.mac, mac_addr, ETH_ALEN);
211
212         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_DEL_MAC, &mac_info,
213                                      sizeof(mac_info), &mac_info, &out_size);
214         if (err || !out_size || (mac_info.mgmt_msg_head.status &&
215                 mac_info.mgmt_msg_head.status != HINIC_PF_SET_VF_ALREADY)) {
216                 PMD_DRV_LOG(ERR, "Failed to delete MAC, err: %d, status: 0x%x, out size: 0x%x",
217                         err, mac_info.mgmt_msg_head.status, out_size);
218                 return -EINVAL;
219         }
220         if (mac_info.mgmt_msg_head.status == HINIC_PF_SET_VF_ALREADY) {
221                 PMD_DRV_LOG(WARNING, "PF has already set vf mac, Ignore delete operation.");
222                 return HINIC_PF_SET_VF_ALREADY;
223         }
224
225         return 0;
226 }
227
228 /**
229  * hinic_get_default_mac - Get default mac address from hardware.
230  *
231  * @param hwdev
232  *   The hardware interface of a nic device.
233  * @param mac_addr
234  *   MAC address.
235  *
236  * @return
237  *   0 on success.
238  *   negative error value otherwise.
239  */
240 int hinic_get_default_mac(void *hwdev, u8 *mac_addr)
241 {
242         struct hinic_port_mac_set mac_info;
243         u16 out_size = sizeof(mac_info);
244         int err;
245
246         if (!hwdev || !mac_addr) {
247                 PMD_DRV_LOG(ERR, "Hwdev or mac_addr is NULL");
248                 return -EINVAL;
249         }
250
251         memset(&mac_info, 0, sizeof(mac_info));
252         mac_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
253         mac_info.func_id = hinic_global_func_id(hwdev);
254
255         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_MAC,
256                                      &mac_info, sizeof(mac_info),
257                                      &mac_info, &out_size);
258         if (err || !out_size || mac_info.mgmt_msg_head.status) {
259                 PMD_DRV_LOG(ERR, "Failed to get mac, err: %d, status: 0x%x, out size: 0x%x",
260                         err, mac_info.mgmt_msg_head.status, out_size);
261                 return -EINVAL;
262         }
263
264         memmove(mac_addr, mac_info.mac, ETH_ALEN);
265
266         return 0;
267 }
268
269 /**
270 *  hinic_update_mac - Update mac address to hardware.
271 *
272 * @param hwdev
273 *   The hardware interface of a nic device.
274 * @param old_mac
275 *   Old mac address.
276 * @param new_mac
277 *   New mac address.
278 * @param vlan_id
279 *   Set 0 for mac_vlan table initialization.
280 * @param func_id
281 *   Global function id of NIC.
282 *
283 * @return
284 *   0 on success.
285 *   negative error value otherwise.
286 */
287 int hinic_update_mac(void *hwdev, u8 *old_mac, u8 *new_mac, u16 vlan_id,
288                      u16 func_id)
289 {
290         struct hinic_port_mac_update mac_info;
291         u16 out_size = sizeof(mac_info);
292         int err;
293
294         if (!hwdev || !old_mac || !new_mac) {
295                 PMD_DRV_LOG(ERR, "Hwdev, old_mac or new_mac is NULL\n");
296                 return -EINVAL;
297         }
298
299         memset(&mac_info, 0, sizeof(mac_info));
300         mac_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
301         mac_info.func_id = func_id;
302         mac_info.vlan_id = vlan_id;
303         memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
304         memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
305
306         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_UPDATE_MAC,
307                                      &mac_info, sizeof(mac_info),
308                                      &mac_info, &out_size);
309         if (err || !out_size ||
310             (mac_info.mgmt_msg_head.status &&
311              mac_info.mgmt_msg_head.status != HINIC_PF_SET_VF_ALREADY)) {
312                 PMD_DRV_LOG(ERR, "Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
313                             err, mac_info.mgmt_msg_head.status, out_size);
314                 return -EINVAL;
315         }
316         if (mac_info.mgmt_msg_head.status == HINIC_PF_SET_VF_ALREADY) {
317                 PMD_DRV_LOG(WARNING, "PF has already set vf mac, Ignore update operation.\n");
318                 return HINIC_PF_SET_VF_ALREADY;
319         }
320
321         return 0;
322 }
323
324 /**
325  * hinic_set_port_mtu -  Set MTU to port.
326  *
327  * @param hwdev
328  *   The hardware interface of a nic device.
329  * @param new_mtu
330  *   MTU size.
331  *
332  * @return
333  *   0 on success.
334  *   negative error value otherwise.
335  */
336 int hinic_set_port_mtu(void *hwdev, u32 new_mtu)
337 {
338         struct hinic_mtu mtu_info;
339         u16 out_size = sizeof(mtu_info);
340         int err;
341
342         if (!hwdev) {
343                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
344                 return -EINVAL;
345         }
346
347         memset(&mtu_info, 0, sizeof(mtu_info));
348         mtu_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
349         mtu_info.func_id = hinic_global_func_id(hwdev);
350         mtu_info.mtu = new_mtu;
351
352         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_CHANGE_MTU,
353                                      &mtu_info, sizeof(mtu_info),
354                                      &mtu_info, &out_size);
355         if (err || !out_size || mtu_info.mgmt_msg_head.status) {
356                 PMD_DRV_LOG(ERR, "Failed to set mtu, err: %d, status: 0x%x, out size: 0x%x",
357                         err, mtu_info.mgmt_msg_head.status, out_size);
358                 return -EINVAL;
359         }
360
361         return 0;
362 }
363
364 /**
365  * hinic_add_remove_vlan - Add or remove vlan id to vlan elb table.
366  *
367  * @param hwdev
368  *   The hardware interface of a nic device.
369  * @param vlan_id
370  *   Vlan id.
371  * @param func_id
372  *   Global function id of NIC.
373  * @param add
374  *   Add or remove operation.
375  *
376  * @return
377  *   0 on success.
378  *   negative error value otherwise.
379  */
380 int hinic_add_remove_vlan(void *hwdev, u16 vlan_id, u16 func_id, bool add)
381 {
382         struct hinic_vlan_config vlan_info;
383         u16 out_size = sizeof(vlan_info);
384         u8 cmd;
385         int err;
386
387         if (!hwdev) {
388                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
389                 return -EINVAL;
390         }
391
392         cmd = add ? HINIC_PORT_CMD_ADD_VLAN : HINIC_PORT_CMD_DEL_VLAN;
393
394         memset(&vlan_info, 0, sizeof(vlan_info));
395         vlan_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
396         vlan_info.func_id = func_id;
397         vlan_info.vlan_id = vlan_id;
398
399         err = l2nic_msg_to_mgmt_sync(hwdev, cmd, &vlan_info,
400                                      sizeof(vlan_info), &vlan_info,
401                                      &out_size);
402         if (err || !out_size || vlan_info.mgmt_msg_head.status) {
403                 PMD_DRV_LOG(ERR,
404                         "Failed to %s vlan, err: %d, status: 0x%x, out size: 0x%x\n",
405                         add ? "add" : "remove", err,
406                         vlan_info.mgmt_msg_head.status, out_size);
407                 return -EINVAL;
408         }
409
410         return 0;
411 }
412
413 /**
414  * hinic_config_vlan_filter - Enable or Disable vlan filter.
415  *
416  * @param hwdev
417  *   The hardware interface of a nic device.
418  * @param vlan_filter_ctrl
419  *   Enable or Disable.
420  *
421  * @return
422  *   0 on success.
423  *   negative error value otherwise.
424  */
425 int hinic_config_vlan_filter(void *hwdev, u32 vlan_filter_ctrl)
426 {
427         struct hinic_hwdev *nic_hwdev = (struct hinic_hwdev *)hwdev;
428         struct hinic_vlan_filter vlan_filter;
429         u16 out_size = sizeof(vlan_filter);
430         int err;
431
432         if (!hwdev)
433                 return -EINVAL;
434
435         memset(&vlan_filter, 0, sizeof(vlan_filter));
436         vlan_filter.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
437         vlan_filter.func_id = hinic_global_func_id(nic_hwdev);
438         vlan_filter.vlan_filter_ctrl = vlan_filter_ctrl;
439
440         err = l2nic_msg_to_mgmt_sync(nic_hwdev, HINIC_PORT_CMD_SET_VLAN_FILTER,
441                                      &vlan_filter, sizeof(vlan_filter),
442                                      &vlan_filter, &out_size);
443         if (vlan_filter.mgmt_msg_head.status == HINIC_MGMT_CMD_UNSUPPORTED) {
444                 err = HINIC_MGMT_CMD_UNSUPPORTED;
445         } else if ((err == HINIC_MBOX_VF_CMD_ERROR) &&
446                 (HINIC_IS_VF(nic_hwdev))) {
447                 err = HINIC_MGMT_CMD_UNSUPPORTED;
448         } else if (err || !out_size || vlan_filter.mgmt_msg_head.status) {
449                 PMD_DRV_LOG(ERR,
450                         "Failed to config vlan filter, vlan_filter_ctrl: 0x%x, err: %d, status: 0x%x, out size: 0x%x\n",
451                         vlan_filter_ctrl, err,
452                         vlan_filter.mgmt_msg_head.status, out_size);
453                 err = -EINVAL;
454         }
455
456         return err;
457 }
458
459 /**
460  * hinic_set_rx_vlan_offload - Enable or Disable vlan offload.
461  *
462  * @param hwdev
463  *   The hardware interface of a nic device.
464  * @param en
465  *   Enable or Disable.
466  *
467  * @return
468  *   0 on success.
469  *   negative error value otherwise.
470  */
471 int hinic_set_rx_vlan_offload(void *hwdev, u8 en)
472 {
473         struct hinic_vlan_offload vlan_cfg;
474         u16 out_size = sizeof(vlan_cfg);
475         int err;
476
477         if (!hwdev) {
478                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
479                 return -EINVAL;
480         }
481
482         memset(&vlan_cfg, 0, sizeof(vlan_cfg));
483         vlan_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
484         vlan_cfg.func_id = hinic_global_func_id(hwdev);
485         vlan_cfg.vlan_rx_offload = en;
486
487         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD,
488                                         &vlan_cfg, sizeof(vlan_cfg),
489                                         &vlan_cfg, &out_size);
490         if (err || !out_size || vlan_cfg.mgmt_msg_head.status) {
491                 PMD_DRV_LOG(ERR,
492                         "Failed to set rx vlan offload, err: %d, status: 0x%x, out size: 0x%x\n",
493                         err, vlan_cfg.mgmt_msg_head.status, out_size);
494                 return -EINVAL;
495         }
496
497         return 0;
498 }
499
500 /**
501  * hinic_get_link_status - Get link status from hardware.
502  *
503  * @param hwdev
504  *   The hardware interface of a nic device.
505  * @param link_state
506  *   Link status.
507  *
508  * @return
509  *   0 on success.
510  *   negative error value otherwise.
511  */
512 int hinic_get_link_status(void *hwdev, u8 *link_state)
513 {
514         struct hinic_get_link get_link;
515         u16 out_size = sizeof(get_link);
516         int err;
517
518         if (!hwdev || !link_state) {
519                 PMD_DRV_LOG(ERR, "Hwdev or link_state is NULL");
520                 return -EINVAL;
521         }
522
523         memset(&get_link, 0, sizeof(get_link));
524         get_link.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
525         get_link.func_id = hinic_global_func_id(hwdev);
526
527         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_LINK_STATE,
528                                      &get_link, sizeof(get_link),
529                                      &get_link, &out_size);
530         if (err || !out_size || get_link.mgmt_msg_head.status) {
531                 PMD_DRV_LOG(ERR, "Failed to get link state, err: %d, status: 0x%x, out size: 0x%x",
532                         err, get_link.mgmt_msg_head.status, out_size);
533                 return -EINVAL;
534         }
535
536         *link_state = get_link.link_status;
537
538         return 0;
539 }
540
541 /**
542  * hinic_set_vport_enable - Notify firmware that driver is ready or not.
543  *
544  * @param hwdev
545  *   The hardware interface of a nic device.
546  * @param enable
547  *   1: driver is ready; 0: driver is not ok.
548  *
549  * @return
550  *   0 on success.
551  *   negative error value otherwise.
552  */
553 int hinic_set_vport_enable(void *hwdev, bool enable)
554 {
555         struct hinic_vport_state en_state;
556         u16 out_size = sizeof(en_state);
557         int err;
558
559         if (!hwdev) {
560                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
561                 return -EINVAL;
562         }
563
564         memset(&en_state, 0, sizeof(en_state));
565         en_state.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
566         en_state.func_id = hinic_global_func_id(hwdev);
567         en_state.state = (enable ? 1 : 0);
568
569         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_VPORT_ENABLE,
570                                      &en_state, sizeof(en_state),
571                                      &en_state, &out_size);
572         if (err || !out_size || en_state.mgmt_msg_head.status) {
573                 PMD_DRV_LOG(ERR, "Failed to set vport state, err: %d, status: 0x%x, out size: 0x%x",
574                         err, en_state.mgmt_msg_head.status, out_size);
575                 return -EINVAL;
576         }
577
578         return 0;
579 }
580
581 /**
582  * hinic_set_port_enable - Open MAG to receive packets.
583  *
584  * @param hwdev
585  *   The hardware interface of a nic device.
586  * @param enable
587  *   1: open MAG; 0: close MAG.
588  *
589  * @return
590  *   0 on success.
591  *   negative error value otherwise.
592  */
593 int hinic_set_port_enable(void *hwdev, bool enable)
594 {
595         struct hinic_port_state en_state;
596         u16 out_size = sizeof(en_state);
597         int err;
598
599         if (!hwdev) {
600                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
601                 return -EINVAL;
602         }
603
604         if (HINIC_IS_VF((struct hinic_hwdev *)hwdev))
605                 return 0;
606
607         memset(&en_state, 0, sizeof(en_state));
608         en_state.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
609         en_state.state = (enable ? HINIC_PORT_ENABLE : HINIC_PORT_DISABLE);
610
611         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_PORT_ENABLE,
612                                      &en_state, sizeof(en_state),
613                                      &en_state, &out_size);
614         if (err || !out_size || en_state.mgmt_msg_head.status) {
615                 PMD_DRV_LOG(ERR, "Failed to set phy port state, err: %d, status: 0x%x, out size: 0x%x",
616                         err, en_state.mgmt_msg_head.status, out_size);
617                 return -EINVAL;
618         }
619
620         return 0;
621 }
622
623 int hinic_get_port_info(void *hwdev, struct nic_port_info *port_info)
624 {
625         struct hinic_port_info port_msg;
626         u16 out_size = sizeof(port_msg);
627         int err;
628
629         if (!hwdev || !port_info) {
630                 PMD_DRV_LOG(ERR, "Hwdev or port_info is NULL");
631                 return -EINVAL;
632         }
633
634         memset(&port_msg, 0, sizeof(port_msg));
635         port_msg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
636         port_msg.func_id = hinic_global_func_id(hwdev);
637
638         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_PORT_INFO,
639                                      &port_msg, sizeof(port_msg),
640                                      &port_msg, &out_size);
641         if (err || !out_size || port_msg.mgmt_msg_head.status) {
642                 PMD_DRV_LOG(ERR,
643                         "Failed to get port info, err: %d, status: 0x%x, out size: 0x%x",
644                         err, port_msg.mgmt_msg_head.status, out_size);
645                 return err;
646         }
647
648         port_info->autoneg_cap = port_msg.autoneg_cap;
649         port_info->autoneg_state = port_msg.autoneg_state;
650         port_info->duplex = port_msg.duplex;
651         port_info->port_type = port_msg.port_type;
652         port_info->speed = port_msg.speed;
653
654         return 0;
655 }
656
657 int hinic_set_pause_config(void *hwdev, struct nic_pause_config nic_pause)
658 {
659         struct hinic_pause_config pause_info;
660         u16 out_size = sizeof(pause_info);
661         int err;
662
663         if (!hwdev) {
664                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
665                 return -EINVAL;
666         }
667
668         memset(&pause_info, 0, sizeof(pause_info));
669         pause_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
670         pause_info.func_id = hinic_global_func_id(hwdev);
671         pause_info.auto_neg = nic_pause.auto_neg;
672         pause_info.rx_pause = nic_pause.rx_pause;
673         pause_info.tx_pause = nic_pause.tx_pause;
674
675         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_PAUSE_INFO,
676                                      &pause_info, sizeof(pause_info),
677                                      &pause_info, &out_size);
678         if (err || !out_size || pause_info.mgmt_msg_head.status) {
679                 PMD_DRV_LOG(ERR, "Failed to set pause info, err: %d, status: 0x%x, out size: 0x%x",
680                         err, pause_info.mgmt_msg_head.status, out_size);
681                 return -EINVAL;
682         }
683
684         return 0;
685 }
686
687 int hinic_dcb_set_ets(void *hwdev, u8 *up_tc, u8 *pg_bw,
688                       u8 *pgid, u8 *up_bw, u8 *prio)
689 {
690         struct hinic_up_ets_cfg ets;
691         u16 out_size = sizeof(ets);
692         u16 up_bw_t = 0;
693         u8 pg_bw_t = 0;
694         int i, err;
695
696         if (!hwdev || !up_tc || !pg_bw || !pgid || !up_bw || !prio) {
697                 PMD_DRV_LOG(ERR, "Hwdev, up_tc, pg_bw, pgid, up_bw or prio is NULL");
698                 return -EINVAL;
699         }
700
701         for (i = 0; i < HINIC_DCB_TC_MAX; i++) {
702                 up_bw_t += *(up_bw + i);
703                 pg_bw_t += *(pg_bw + i);
704
705                 if (*(up_tc + i) > HINIC_DCB_TC_MAX) {
706                         PMD_DRV_LOG(ERR,
707                                 "Invalid up %d mapping tc: %d", i,
708                                 *(up_tc + i));
709                         return -EINVAL;
710                 }
711         }
712
713         if (pg_bw_t != 100 || (up_bw_t % 100) != 0) {
714                 PMD_DRV_LOG(ERR,
715                         "Invalid pg_bw: %d or up_bw: %d", pg_bw_t, up_bw_t);
716                 return -EINVAL;
717         }
718
719         memset(&ets, 0, sizeof(ets));
720         ets.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
721         ets.port_id = 0;        /* reserved */
722         memcpy(ets.up_tc, up_tc, HINIC_DCB_TC_MAX);
723         memcpy(ets.pg_bw, pg_bw, HINIC_DCB_UP_MAX);
724         memcpy(ets.pgid, pgid, HINIC_DCB_UP_MAX);
725         memcpy(ets.up_bw, up_bw, HINIC_DCB_UP_MAX);
726         memcpy(ets.prio, prio, HINIC_DCB_UP_MAX);
727
728         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_ETS,
729                                      &ets, sizeof(ets), &ets, &out_size);
730         if (err || ets.mgmt_msg_head.status || !out_size) {
731                 PMD_DRV_LOG(ERR,
732                         "Failed to set ets, err: %d, status: 0x%x, out size: 0x%x",
733                         err, ets.mgmt_msg_head.status, out_size);
734                 return -EINVAL;
735         }
736
737         return 0;
738 }
739
740 int hinic_get_vport_stats(void *hwdev, struct hinic_vport_stats *stats)
741 {
742         struct hinic_port_stats_info vport_stats_cmd;
743         struct hinic_cmd_vport_stats vport_stats_rsp;
744         u16 out_size = sizeof(vport_stats_rsp);
745         int err;
746
747         if (!hwdev || !stats) {
748                 PMD_DRV_LOG(ERR, "Hwdev or stats is NULL");
749                 return -EINVAL;
750         }
751
752         memset(&vport_stats_rsp, 0, sizeof(vport_stats_rsp));
753         memset(&vport_stats_cmd, 0, sizeof(vport_stats_cmd));
754         vport_stats_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
755         vport_stats_cmd.stats_version = HINIC_PORT_STATS_VERSION;
756         vport_stats_cmd.func_id = hinic_global_func_id(hwdev);
757         vport_stats_cmd.stats_size = sizeof(vport_stats_rsp);
758
759         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_VPORT_STAT,
760                                      &vport_stats_cmd, sizeof(vport_stats_cmd),
761                                      &vport_stats_rsp, &out_size);
762         if (err || !out_size || vport_stats_rsp.mgmt_msg_head.status) {
763                 PMD_DRV_LOG(ERR,
764                         "Get vport stats from fw failed, err: %d, status: 0x%x, out size: 0x%x",
765                         err, vport_stats_rsp.mgmt_msg_head.status, out_size);
766                 return -EFAULT;
767         }
768
769         memcpy(stats, &vport_stats_rsp.stats, sizeof(*stats));
770
771         return 0;
772 }
773
774 int hinic_get_phy_port_stats(void *hwdev, struct hinic_phy_port_stats *stats)
775 {
776         struct hinic_port_stats_info port_stats_cmd;
777         struct hinic_port_stats port_stats_rsp;
778         u16 out_size = sizeof(port_stats_rsp);
779         int err;
780
781         if (!hwdev || !stats) {
782                 PMD_DRV_LOG(ERR, "Hwdev or stats is NULL");
783                 return -EINVAL;
784         }
785
786         memset(&port_stats_rsp, 0, sizeof(port_stats_rsp));
787         memset(&port_stats_cmd, 0, sizeof(port_stats_cmd));
788         port_stats_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
789         port_stats_cmd.stats_version = HINIC_PORT_STATS_VERSION;
790         port_stats_cmd.stats_size = sizeof(port_stats_rsp);
791
792         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_PORT_STATISTICS,
793                                      &port_stats_cmd, sizeof(port_stats_cmd),
794                                      &port_stats_rsp, &out_size);
795         if (err || !out_size || port_stats_rsp.mgmt_msg_head.status) {
796                 PMD_DRV_LOG(ERR,
797                         "Failed to get port statistics, err: %d, status: 0x%x, out size: 0x%x",
798                         err, port_stats_rsp.mgmt_msg_head.status, out_size);
799                 return -EFAULT;
800         }
801
802         memcpy(stats, &port_stats_rsp.stats, sizeof(*stats));
803
804         return 0;
805 }
806
807 int hinic_set_rss_type(void *hwdev, u32 tmpl_idx, struct nic_rss_type rss_type)
808 {
809         struct nic_rss_context_tbl *ctx_tbl;
810         struct hinic_cmd_buf *cmd_buf;
811         u32 ctx = 0;
812         u64 out_param;
813         int err;
814
815         if (!hwdev) {
816                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
817                 return -EINVAL;
818         }
819
820         cmd_buf = hinic_alloc_cmd_buf(hwdev);
821         if (!cmd_buf) {
822                 PMD_DRV_LOG(ERR, "Failed to allocate cmd buf");
823                 return -ENOMEM;
824         }
825
826         ctx |= HINIC_RSS_TYPE_SET(1, VALID) |
827                 HINIC_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
828                 HINIC_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
829                 HINIC_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
830                 HINIC_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
831                 HINIC_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
832                 HINIC_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
833                 HINIC_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
834                 HINIC_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
835
836         cmd_buf->size = sizeof(struct nic_rss_context_tbl);
837
838         ctx_tbl = (struct nic_rss_context_tbl *)cmd_buf->buf;
839         ctx_tbl->group_index = cpu_to_be32(tmpl_idx);
840         ctx_tbl->offset = 0;
841         ctx_tbl->size = sizeof(u32);
842         ctx_tbl->size = cpu_to_be32(ctx_tbl->size);
843         ctx_tbl->rsvd = 0;
844         ctx_tbl->ctx = cpu_to_be32(ctx);
845
846         /* cfg the rss context table by command queue */
847         err = hinic_cmdq_direct_resp(hwdev, HINIC_ACK_TYPE_CMDQ,
848                                      HINIC_MOD_L2NIC,
849                                      HINIC_UCODE_CMD_SET_RSS_CONTEXT_TABLE,
850                                      cmd_buf, &out_param, 0);
851
852         hinic_free_cmd_buf(hwdev, cmd_buf);
853
854         if (err || out_param != 0) {
855                 PMD_DRV_LOG(ERR, "Failed to set rss context table");
856                 return -EFAULT;
857         }
858
859         return 0;
860 }
861
862 int hinic_get_rss_type(void *hwdev, u32 tmpl_idx, struct nic_rss_type *rss_type)
863 {
864         struct hinic_rss_context_table ctx_tbl;
865         u16 out_size = sizeof(ctx_tbl);
866         int err;
867
868         if (!hwdev || !rss_type) {
869                 PMD_DRV_LOG(ERR, "Hwdev or rss_type is NULL");
870                 return -EINVAL;
871         }
872
873         ctx_tbl.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
874         ctx_tbl.func_id = hinic_global_func_id(hwdev);
875         ctx_tbl.template_id = (u8)tmpl_idx;
876
877         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_RSS_CTX_TBL,
878                                      &ctx_tbl, sizeof(ctx_tbl),
879                                      &ctx_tbl, &out_size);
880         if (err || !out_size || ctx_tbl.mgmt_msg_head.status) {
881                 PMD_DRV_LOG(ERR,
882                         "Failed to get hash type, err: %d, status: 0x%x, out size: 0x%x",
883                         err, ctx_tbl.mgmt_msg_head.status, out_size);
884                 return -EINVAL;
885         }
886
887         rss_type->ipv4 = HINIC_RSS_TYPE_GET(ctx_tbl.context, IPV4);
888         rss_type->ipv6 = HINIC_RSS_TYPE_GET(ctx_tbl.context, IPV6);
889         rss_type->ipv6_ext = HINIC_RSS_TYPE_GET(ctx_tbl.context, IPV6_EXT);
890         rss_type->tcp_ipv4 = HINIC_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV4);
891         rss_type->tcp_ipv6 = HINIC_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6);
892         rss_type->tcp_ipv6_ext =
893                         HINIC_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6_EXT);
894         rss_type->udp_ipv4 = HINIC_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV4);
895         rss_type->udp_ipv6 = HINIC_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV6);
896
897         return 0;
898 }
899
900 int hinic_rss_set_template_tbl(void *hwdev, u32 tmpl_idx, u8 *temp)
901 {
902         struct hinic_rss_template_key temp_key;
903         u16 out_size = sizeof(temp_key);
904         int err;
905
906         if (!hwdev || !temp) {
907                 PMD_DRV_LOG(ERR, "Hwdev or temp is NULL");
908                 return -EINVAL;
909         }
910
911         memset(&temp_key, 0, sizeof(temp_key));
912         temp_key.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
913         temp_key.func_id = hinic_global_func_id(hwdev);
914         temp_key.template_id = (u8)tmpl_idx;
915         memcpy(temp_key.key, temp, HINIC_RSS_KEY_SIZE);
916
917         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL,
918                                      &temp_key, sizeof(temp_key),
919                                      &temp_key, &out_size);
920         if (err || !out_size || temp_key.mgmt_msg_head.status) {
921                 PMD_DRV_LOG(ERR,
922                         "Failed to set hash key, err: %d, status: 0x%x, out size: 0x%x",
923                         err, temp_key.mgmt_msg_head.status, out_size);
924                 return -EINVAL;
925         }
926
927         return 0;
928 }
929
930 int hinic_rss_get_template_tbl(void *hwdev, u32 tmpl_idx, u8 *temp)
931 {
932         struct hinic_rss_template_key temp_key;
933         u16 out_size = sizeof(temp_key);
934         int err;
935
936         if (!hwdev || !temp) {
937                 PMD_DRV_LOG(ERR, "Hwdev or temp is NULL");
938                 return -EINVAL;
939         }
940
941         memset(&temp_key, 0, sizeof(temp_key));
942         temp_key.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
943         temp_key.func_id = hinic_global_func_id(hwdev);
944         temp_key.template_id = (u8)tmpl_idx;
945
946         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL,
947                                      &temp_key, sizeof(temp_key),
948                                      &temp_key, &out_size);
949         if (err || !out_size || temp_key.mgmt_msg_head.status) {
950                 PMD_DRV_LOG(ERR, "Failed to get hash key, err: %d, status: 0x%x, out size: 0x%x",
951                         err, temp_key.mgmt_msg_head.status, out_size);
952                 return -EINVAL;
953         }
954
955         memcpy(temp, temp_key.key, HINIC_RSS_KEY_SIZE);
956
957         return 0;
958 }
959
960 /**
961  * hinic_rss_set_hash_engine - Init rss hash function.
962  *
963  * @param hwdev
964  *   The hardware interface of a nic device.
965  * @param tmpl_idx
966  *   Index of rss template from NIC.
967  * @param type
968  *   Hash function, such as Toeplitz or XOR.
969  *
970  * @return
971  *   0 on success.
972  *   negative error value otherwise.
973  */
974 int hinic_rss_set_hash_engine(void *hwdev, u8 tmpl_idx, u8 type)
975 {
976         struct hinic_rss_engine_type hash_type;
977         u16 out_size = sizeof(hash_type);
978         int err;
979
980         if (!hwdev) {
981                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
982                 return -EINVAL;
983         }
984
985         memset(&hash_type, 0, sizeof(hash_type));
986         hash_type.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
987         hash_type.func_id = hinic_global_func_id(hwdev);
988         hash_type.hash_engine = type;
989         hash_type.template_id = tmpl_idx;
990
991         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RSS_HASH_ENGINE,
992                                      &hash_type, sizeof(hash_type),
993                                      &hash_type, &out_size);
994         if (err || !out_size || hash_type.mgmt_msg_head.status) {
995                 PMD_DRV_LOG(ERR, "Failed to get hash engine, err: %d, status: 0x%x, out size: 0x%x",
996                         err, hash_type.mgmt_msg_head.status, out_size);
997                 return -EINVAL;
998         }
999
1000         return 0;
1001 }
1002
1003 int hinic_rss_set_indir_tbl(void *hwdev, u32 tmpl_idx, u32 *indir_table)
1004 {
1005         struct nic_rss_indirect_tbl *indir_tbl;
1006         struct hinic_cmd_buf *cmd_buf;
1007         int i;
1008         u32 *temp;
1009         u32 indir_size;
1010         u64 out_param;
1011         int err;
1012
1013         if (!hwdev || !indir_table) {
1014                 PMD_DRV_LOG(ERR, "Hwdev or indir_table is NULL");
1015                 return -EINVAL;
1016         }
1017
1018         cmd_buf = hinic_alloc_cmd_buf(hwdev);
1019         if (!cmd_buf) {
1020                 PMD_DRV_LOG(ERR, "Failed to allocate cmd buf");
1021                 return -ENOMEM;
1022         }
1023
1024         cmd_buf->size = sizeof(struct nic_rss_indirect_tbl);
1025         indir_tbl = cmd_buf->buf;
1026         indir_tbl->group_index = cpu_to_be32(tmpl_idx);
1027
1028         for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++) {
1029                 indir_tbl->entry[i] = (u8)(*(indir_table + i));
1030
1031                 if (0x3 == (i & 0x3)) {
1032                         temp = (u32 *)&indir_tbl->entry[i - 3];
1033                         *temp = cpu_to_be32(*temp);
1034                 }
1035         }
1036
1037         /* configure the rss indirect table by command queue */
1038         indir_size = HINIC_RSS_INDIR_SIZE / 2;
1039         indir_tbl->offset = 0;
1040         indir_tbl->size = cpu_to_be32(indir_size);
1041
1042         err = hinic_cmdq_direct_resp(hwdev, HINIC_ACK_TYPE_CMDQ,
1043                                      HINIC_MOD_L2NIC,
1044                                      HINIC_UCODE_CMD_SET_RSS_INDIR_TABLE,
1045                                      cmd_buf, &out_param, 0);
1046         if (err || out_param != 0) {
1047                 PMD_DRV_LOG(ERR, "Failed to set rss indir table");
1048                 err = -EFAULT;
1049                 goto free_buf;
1050         }
1051
1052         indir_tbl->offset = cpu_to_be32(indir_size);
1053         indir_tbl->size = cpu_to_be32(indir_size);
1054         memcpy(indir_tbl->entry, &indir_tbl->entry[indir_size], indir_size);
1055
1056         err = hinic_cmdq_direct_resp(hwdev, HINIC_ACK_TYPE_CMDQ,
1057                                      HINIC_MOD_L2NIC,
1058                                      HINIC_UCODE_CMD_SET_RSS_INDIR_TABLE,
1059                                      cmd_buf, &out_param, 0);
1060         if (err || out_param != 0) {
1061                 PMD_DRV_LOG(ERR, "Failed to set rss indir table");
1062                 err = -EFAULT;
1063         }
1064
1065 free_buf:
1066         hinic_free_cmd_buf(hwdev, cmd_buf);
1067
1068         return err;
1069 }
1070
1071 int hinic_rss_get_indir_tbl(void *hwdev, u32 tmpl_idx, u32 *indir_table)
1072 {
1073         struct hinic_rss_indir_table rss_cfg;
1074         u16 out_size = sizeof(rss_cfg);
1075         int err = 0, i;
1076
1077         if (!hwdev || !indir_table) {
1078                 PMD_DRV_LOG(ERR, "Hwdev or indir_table is NULL");
1079                 return -EINVAL;
1080         }
1081
1082         memset(&rss_cfg, 0, sizeof(rss_cfg));
1083         rss_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1084         rss_cfg.func_id = hinic_global_func_id(hwdev);
1085         rss_cfg.template_id = (u8)tmpl_idx;
1086
1087         err = l2nic_msg_to_mgmt_sync(hwdev,
1088                                      HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL,
1089                                      &rss_cfg, sizeof(rss_cfg), &rss_cfg,
1090                                      &out_size);
1091         if (err || !out_size || rss_cfg.mgmt_msg_head.status) {
1092                 PMD_DRV_LOG(ERR, "Failed to get indir table, err: %d, status: 0x%x, out size: 0x%x",
1093                         err, rss_cfg.mgmt_msg_head.status, out_size);
1094                 return -EINVAL;
1095         }
1096
1097         hinic_be32_to_cpu(rss_cfg.indir, HINIC_RSS_INDIR_SIZE);
1098         for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
1099                 indir_table[i] = rss_cfg.indir[i];
1100
1101         return 0;
1102 }
1103
1104 int hinic_rss_cfg(void *hwdev, u8 rss_en, u8 tmpl_idx, u8 tc_num, u8 *prio_tc)
1105 {
1106         struct hinic_rss_config rss_cfg;
1107         u16 out_size = sizeof(rss_cfg);
1108         int err;
1109
1110         /* micro code required: number of TC should be power of 2 */
1111         if (!hwdev || !prio_tc || (tc_num & (tc_num - 1))) {
1112                 PMD_DRV_LOG(ERR, "Hwdev or prio_tc is NULL, or tc_num: %u Not power of 2",
1113                         tc_num);
1114                 return -EINVAL;
1115         }
1116
1117         memset(&rss_cfg, 0, sizeof(rss_cfg));
1118         rss_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1119         rss_cfg.func_id = hinic_global_func_id(hwdev);
1120         rss_cfg.rss_en = rss_en;
1121         rss_cfg.template_id = tmpl_idx;
1122         rss_cfg.rq_priority_number = tc_num ? (u8)ilog2(tc_num) : 0;
1123
1124         memcpy(rss_cfg.prio_tc, prio_tc, HINIC_DCB_UP_MAX);
1125
1126         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_RSS_CFG,
1127                                      &rss_cfg, sizeof(rss_cfg), &rss_cfg,
1128                                      &out_size);
1129         if (err || !out_size || rss_cfg.mgmt_msg_head.status) {
1130                 PMD_DRV_LOG(ERR, "Failed to set rss cfg, err: %d, status: 0x%x, out size: 0x%x",
1131                         err, rss_cfg.mgmt_msg_head.status, out_size);
1132                 return -EINVAL;
1133         }
1134
1135         return 0;
1136 }
1137
1138 /**
1139  * hinic_rss_template_alloc - Get rss template id from the chip,
1140  * all functions share 96 templates.
1141  *
1142  * @param hwdev
1143  *   The hardware interface of a nic device.
1144  * @param tmpl_idx
1145  *   Index of rss template from chip.
1146  *
1147  * @return
1148  *   0 on success.
1149  *   negative error value otherwise.
1150  */
1151 int hinic_rss_template_alloc(void *hwdev, u8 *tmpl_idx)
1152 {
1153         struct hinic_rss_template_mgmt template_mgmt;
1154         u16 out_size = sizeof(template_mgmt);
1155         int err;
1156
1157         if (!hwdev || !tmpl_idx) {
1158                 PMD_DRV_LOG(ERR, "Hwdev or tmpl_idx is NULL");
1159                 return -EINVAL;
1160         }
1161
1162         memset(&template_mgmt, 0, sizeof(template_mgmt));
1163         template_mgmt.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1164         template_mgmt.func_id = hinic_global_func_id(hwdev);
1165         template_mgmt.cmd = NIC_RSS_CMD_TEMP_ALLOC;
1166
1167         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_RSS_TEMP_MGR,
1168                                      &template_mgmt, sizeof(template_mgmt),
1169                                      &template_mgmt, &out_size);
1170         if (err || !out_size || template_mgmt.mgmt_msg_head.status) {
1171                 PMD_DRV_LOG(ERR, "Failed to alloc rss template, err: %d, status: 0x%x, out size: 0x%x",
1172                         err, template_mgmt.mgmt_msg_head.status, out_size);
1173                 return -EINVAL;
1174         }
1175
1176         *tmpl_idx = template_mgmt.template_id;
1177
1178         return 0;
1179 }
1180
1181 /**
1182  * hinic_rss_template_free - Free rss template id to the chip.
1183  *
1184  * @param hwdev
1185  *   The hardware interface of a nic device.
1186  * @param tmpl_idx
1187  *   Index of rss template from chip.
1188  *
1189  * @return
1190  *   0 on success.
1191  *   negative error value otherwise.
1192  */
1193 int hinic_rss_template_free(void *hwdev, u8 tmpl_idx)
1194 {
1195         struct hinic_rss_template_mgmt template_mgmt;
1196         u16 out_size = sizeof(template_mgmt);
1197         int err;
1198
1199         if (!hwdev) {
1200                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1201                 return -EINVAL;
1202         }
1203
1204         memset(&template_mgmt, 0, sizeof(template_mgmt));
1205         template_mgmt.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1206         template_mgmt.func_id = hinic_global_func_id(hwdev);
1207         template_mgmt.template_id = tmpl_idx;
1208         template_mgmt.cmd = NIC_RSS_CMD_TEMP_FREE;
1209
1210         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_RSS_TEMP_MGR,
1211                                      &template_mgmt, sizeof(template_mgmt),
1212                                      &template_mgmt, &out_size);
1213         if (err || !out_size || template_mgmt.mgmt_msg_head.status) {
1214                 PMD_DRV_LOG(ERR, "Failed to free rss template, err: %d, status: 0x%x, out size: 0x%x",
1215                         err, template_mgmt.mgmt_msg_head.status, out_size);
1216                 return -EINVAL;
1217         }
1218
1219         return 0;
1220 }
1221
1222 /**
1223  * hinic_set_rx_vhd_mode - Change rx buffer size after initialization.
1224  *
1225  * @param hwdev
1226  *   The hardware interface of a nic device.
1227  * @param vhd_mode
1228  *   Not needed.
1229  * @param rx_buf_sz
1230  *   receive buffer size.
1231  *
1232  * @return
1233  *   0 on success.
1234  *   negative error value otherwise.
1235  */
1236 int hinic_set_rx_vhd_mode(void *hwdev, u16 vhd_mode, u16 rx_buf_sz)
1237 {
1238         struct hinic_set_vhd_mode vhd_mode_cfg;
1239         u16 out_size = sizeof(vhd_mode_cfg);
1240         int err;
1241
1242         if (!hwdev) {
1243                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1244                 return -EINVAL;
1245         }
1246
1247         memset(&vhd_mode_cfg, 0, sizeof(vhd_mode_cfg));
1248
1249         vhd_mode_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1250         vhd_mode_cfg.func_id = hinic_global_func_id(hwdev);
1251         vhd_mode_cfg.vhd_type = vhd_mode;
1252         vhd_mode_cfg.rx_wqe_buffer_size = rx_buf_sz;
1253
1254         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_VHD_CFG,
1255                                      &vhd_mode_cfg, sizeof(vhd_mode_cfg),
1256                                      &vhd_mode_cfg, &out_size);
1257         if (err || !out_size || vhd_mode_cfg.mgmt_msg_head.status) {
1258                 PMD_DRV_LOG(ERR,
1259                         "Failed to set vhd mode, err: %d, status: 0x%x, out size: 0x%x",
1260                         err, vhd_mode_cfg.mgmt_msg_head.status, out_size);
1261
1262                 return -EIO;
1263         }
1264
1265         return 0;
1266 }
1267
1268 int hinic_set_rx_mode(void *hwdev, u32 enable)
1269 {
1270         struct hinic_rx_mode_config rx_mode_cfg;
1271         u16 out_size = sizeof(rx_mode_cfg);
1272         int err;
1273
1274         if (!hwdev) {
1275                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1276                 return -EINVAL;
1277         }
1278
1279         memset(&rx_mode_cfg, 0, sizeof(rx_mode_cfg));
1280         rx_mode_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1281         rx_mode_cfg.func_id = hinic_global_func_id(hwdev);
1282         rx_mode_cfg.rx_mode = enable;
1283
1284         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RX_MODE,
1285                                      &rx_mode_cfg, sizeof(rx_mode_cfg),
1286                                      &rx_mode_cfg, &out_size);
1287         if (err || !out_size || rx_mode_cfg.mgmt_msg_head.status) {
1288                 PMD_DRV_LOG(ERR, "Failed to set rx mode, err: %d, status: 0x%x, out size: 0x%x",
1289                         err, rx_mode_cfg.mgmt_msg_head.status, out_size);
1290                 return -EINVAL;
1291         }
1292
1293         return 0;
1294 }
1295
1296 int hinic_set_rx_csum_offload(void *hwdev, u32 en)
1297 {
1298         struct hinic_checksum_offload rx_csum_cfg;
1299         u16 out_size = sizeof(rx_csum_cfg);
1300         int err;
1301
1302         if (!hwdev) {
1303                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1304                 return -EINVAL;
1305         }
1306
1307         memset(&rx_csum_cfg, 0, sizeof(rx_csum_cfg));
1308         rx_csum_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1309         rx_csum_cfg.func_id = hinic_global_func_id(hwdev);
1310         rx_csum_cfg.rx_csum_offload = en;
1311
1312         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RX_CSUM,
1313                                      &rx_csum_cfg, sizeof(rx_csum_cfg),
1314                                      &rx_csum_cfg, &out_size);
1315         if (err || !out_size || rx_csum_cfg.mgmt_msg_head.status) {
1316                 PMD_DRV_LOG(ERR,
1317                         "Failed to set rx csum offload, err: %d, status: 0x%x, out size: 0x%x",
1318                         err, rx_csum_cfg.mgmt_msg_head.status, out_size);
1319                 return -EINVAL;
1320         }
1321
1322         return 0;
1323 }
1324
1325 int hinic_set_rx_lro(void *hwdev, u8 ipv4_en, u8 ipv6_en, u8 max_wqe_num)
1326 {
1327         struct hinic_lro_config lro_cfg;
1328         u16 out_size = sizeof(lro_cfg);
1329         int err;
1330
1331         if (!hwdev) {
1332                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1333                 return -EINVAL;
1334         }
1335
1336         memset(&lro_cfg, 0, sizeof(lro_cfg));
1337         lro_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1338         lro_cfg.func_id = hinic_global_func_id(hwdev);
1339         lro_cfg.lro_ipv4_en = ipv4_en;
1340         lro_cfg.lro_ipv6_en = ipv6_en;
1341         lro_cfg.lro_max_wqe_num = max_wqe_num;
1342
1343         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_LRO,
1344                                      &lro_cfg, sizeof(lro_cfg), &lro_cfg,
1345                                      &out_size);
1346         if (err || !out_size || lro_cfg.mgmt_msg_head.status) {
1347                 PMD_DRV_LOG(ERR, "Failed to set lro offload, err: %d, status: 0x%x, out size: 0x%x",
1348                         err, lro_cfg.mgmt_msg_head.status, out_size);
1349                 return -EINVAL;
1350         }
1351
1352         return 0;
1353 }
1354
1355 int hinic_set_anti_attack(void *hwdev, bool enable)
1356 {
1357         struct hinic_port_anti_attack_rate rate;
1358         u16 out_size = sizeof(rate);
1359         int err;
1360
1361         if (!hwdev) {
1362                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1363                 return -EINVAL;
1364         }
1365
1366         memset(&rate, 0, sizeof(rate));
1367         rate.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1368         rate.func_id = hinic_global_func_id(hwdev);
1369         rate.enable = enable;
1370         rate.cir = ANTI_ATTACK_DEFAULT_CIR;
1371         rate.xir = ANTI_ATTACK_DEFAULT_XIR;
1372         rate.cbs = ANTI_ATTACK_DEFAULT_CBS;
1373         rate.xbs = ANTI_ATTACK_DEFAULT_XBS;
1374
1375         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_ANTI_ATTACK_RATE,
1376                                      &rate, sizeof(rate), &rate,
1377                                      &out_size);
1378         if (err || !out_size || rate.mgmt_msg_head.status) {
1379                 PMD_DRV_LOG(ERR, "can't %s port Anti-Attack rate limit, err: %d, status: 0x%x, out size: 0x%x",
1380                         (enable ? "enable" : "disable"), err,
1381                         rate.mgmt_msg_head.status, out_size);
1382                 return -EINVAL;
1383         }
1384
1385         return 0;
1386 }
1387
1388 /* Set autoneg status and restart port link status */
1389 int hinic_reset_port_link_cfg(void *hwdev)
1390 {
1391         struct hinic_reset_link_cfg reset_cfg;
1392         u16 out_size = sizeof(reset_cfg);
1393         int err;
1394
1395         memset(&reset_cfg, 0, sizeof(reset_cfg));
1396         reset_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1397         reset_cfg.func_id = hinic_global_func_id(hwdev);
1398
1399         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_RESET_LINK_CFG,
1400                                      &reset_cfg, sizeof(reset_cfg),
1401                                      &reset_cfg, &out_size);
1402         if (err || !out_size || reset_cfg.mgmt_msg_head.status) {
1403                 PMD_DRV_LOG(ERR, "Reset port link configure failed, err: %d, status: 0x%x, out size: 0x%x",
1404                         err, reset_cfg.mgmt_msg_head.status, out_size);
1405                 return -EFAULT;
1406         }
1407
1408         return 0;
1409 }
1410
1411 /**
1412  * hinic_vf_func_init - Register VF to PF.
1413  *
1414  * @param hwdev
1415  *   The hardware interface of a nic device.
1416  *
1417  * @return
1418  *   0 on success.
1419  *   negative error value otherwise.
1420  */
1421 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1422 {
1423         int err, state = 0;
1424
1425         if (!HINIC_IS_VF(hwdev))
1426                 return 0;
1427
1428         err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1429                         HINIC_PORT_CMD_VF_REGISTER, &state, sizeof(state),
1430                         NULL, NULL, 0);
1431         if (err) {
1432                 PMD_DRV_LOG(ERR, "Fail to register vf");
1433                 return err;
1434         }
1435
1436         return 0;
1437 }
1438
1439 /**
1440  * hinic_vf_func_free - Unregister VF from PF.
1441  *
1442  * @param hwdev
1443  *   The hardware interface of a nic device.
1444  */
1445 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1446 {
1447         int err;
1448
1449         if (hinic_func_type(hwdev) != TYPE_VF)
1450                 return;
1451
1452         err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1453                                 HINIC_PORT_CMD_VF_UNREGISTER, &err, sizeof(err),
1454                                 NULL, NULL, 0);
1455         if (err)
1456                 PMD_DRV_LOG(ERR, "Fail to unregister VF, err: %d", err);
1457 }
1458
1459 int hinic_set_fast_recycle_mode(void *hwdev, u8 mode)
1460 {
1461         struct hinic_fast_recycled_mode fast_recycled_mode;
1462         u16 out_size = sizeof(fast_recycled_mode);
1463         int err;
1464
1465         if (!hwdev) {
1466                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1467                 return -EINVAL;
1468         }
1469
1470         memset(&fast_recycled_mode, 0, sizeof(fast_recycled_mode));
1471         fast_recycled_mode.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1472         fast_recycled_mode.func_id = hinic_global_func_id(hwdev);
1473         fast_recycled_mode.fast_recycled_mode = mode;
1474
1475         err = hinic_msg_to_mgmt_sync(hwdev, HINIC_MOD_COMM,
1476                                      HINIC_MGMT_CMD_FAST_RECYCLE_MODE_SET,
1477                                      &fast_recycled_mode,
1478                                      sizeof(fast_recycled_mode),
1479                                      &fast_recycled_mode, &out_size, 0);
1480         if (err || fast_recycled_mode.mgmt_msg_head.status || !out_size) {
1481                 PMD_DRV_LOG(ERR,
1482                         "Failed to set recycle mode, ret = %d",
1483                         fast_recycled_mode.mgmt_msg_head.status);
1484                 return -EFAULT;
1485         }
1486
1487         return 0;
1488 }
1489
1490 int hinic_clear_vport_stats(struct hinic_hwdev *hwdev)
1491 {
1492         struct hinic_clear_vport_stats clear_vport_stats;
1493         u16 out_size = sizeof(clear_vport_stats);
1494         int err;
1495
1496         if (!hwdev) {
1497                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1498                 return -EINVAL;
1499         }
1500
1501         memset(&clear_vport_stats, 0, sizeof(clear_vport_stats));
1502         clear_vport_stats.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1503         clear_vport_stats.func_id = hinic_global_func_id(hwdev);
1504
1505         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_CLEAN_VPORT_STAT,
1506                                      &clear_vport_stats,
1507                                      sizeof(clear_vport_stats),
1508                                      &clear_vport_stats, &out_size);
1509         if (err || !out_size || clear_vport_stats.mgmt_msg_head.status) {
1510                 PMD_DRV_LOG(ERR, "Failed to clear vport statistics, err: %d, status: 0x%x, out size: 0x%x",
1511                         err, clear_vport_stats.mgmt_msg_head.status, out_size);
1512                 return -EINVAL;
1513         }
1514
1515         return 0;
1516 }
1517
1518 int hinic_clear_phy_port_stats(struct hinic_hwdev *hwdev)
1519 {
1520         struct hinic_clear_port_stats clear_phy_port_stats;
1521         u16 out_size = sizeof(clear_phy_port_stats);
1522         int err;
1523
1524         if (!hwdev) {
1525                 PMD_DRV_LOG(ERR, "Hwdev is NULL");
1526                 return -EINVAL;
1527         }
1528
1529         memset(&clear_phy_port_stats, 0, sizeof(clear_phy_port_stats));
1530         clear_phy_port_stats.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1531         clear_phy_port_stats.func_id = hinic_global_func_id(hwdev);
1532
1533         err = l2nic_msg_to_mgmt_sync(hwdev,
1534                                      HINIC_PORT_CMD_CLEAR_PORT_STATISTICS,
1535                                      &clear_phy_port_stats,
1536                                      sizeof(clear_phy_port_stats),
1537                                      &clear_phy_port_stats, &out_size);
1538         if (err || !out_size || clear_phy_port_stats.mgmt_msg_head.status) {
1539                 PMD_DRV_LOG(ERR, "Failed to clear phy port statistics, err: %d, status: 0x%x, out size: 0x%x",
1540                         err, clear_phy_port_stats.mgmt_msg_head.status,
1541                         out_size);
1542                 return -EINVAL;
1543         }
1544
1545         return 0;
1546 }
1547
1548 int hinic_set_link_status_follow(void *hwdev,
1549                                  enum hinic_link_follow_status status)
1550 {
1551         struct hinic_set_link_follow follow;
1552         u16 out_size = sizeof(follow);
1553         int err;
1554
1555         if (!hwdev)
1556                 return -EINVAL;
1557
1558         if (HINIC_IS_VF((struct hinic_hwdev *)hwdev))
1559                 return 0;
1560
1561         if (status >= HINIC_LINK_FOLLOW_STATUS_MAX) {
1562                 PMD_DRV_LOG(ERR,
1563                         "Invalid link follow status: %d", status);
1564                 return -EINVAL;
1565         }
1566
1567         memset(&follow, 0, sizeof(follow));
1568         follow.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1569         follow.func_id = hinic_global_func_id(hwdev);
1570         follow.follow_status = status;
1571
1572         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_LINK_FOLLOW,
1573                                      &follow, sizeof(follow),
1574                                      &follow, &out_size);
1575         if ((follow.mgmt_msg_head.status != HINIC_MGMT_CMD_UNSUPPORTED &&
1576              follow.mgmt_msg_head.status) || err || !out_size) {
1577                 PMD_DRV_LOG(ERR,
1578                         "Failed to set link status follow phy port status, err: %d, status: 0x%x, out size: 0x%x",
1579                         err, follow.mgmt_msg_head.status, out_size);
1580                 return -EFAULT;
1581         }
1582
1583         return follow.mgmt_msg_head.status;
1584 }
1585
1586 int hinic_get_link_mode(void *hwdev, u32 *supported, u32 *advertised)
1587 {
1588         struct hinic_link_mode_cmd link_mode;
1589         u16 out_size = sizeof(link_mode);
1590         int err;
1591
1592         if (!hwdev || !supported || !advertised)
1593                 return -EINVAL;
1594
1595         memset(&link_mode, 0, sizeof(link_mode));
1596         link_mode.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1597         link_mode.func_id = hinic_global_func_id(hwdev);
1598
1599         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_GET_LINK_MODE,
1600                                      &link_mode, sizeof(link_mode),
1601                                      &link_mode, &out_size);
1602         if (err || !out_size || link_mode.mgmt_msg_head.status) {
1603                 PMD_DRV_LOG(ERR,
1604                         "Failed to get link mode, err: %d, status: 0x%x, out size: 0x%x",
1605                         err, link_mode.mgmt_msg_head.status, out_size);
1606                 return -EINVAL;
1607         }
1608
1609         *supported = link_mode.supported;
1610         *advertised = link_mode.advertised;
1611
1612         return 0;
1613 }
1614
1615 /**
1616  * hinic_set_xsfp_tx_status - Enable or disable the fiber in
1617  * tx direction when set link up or down.
1618  *
1619  * @param hwdev
1620  *   The hardware interface of a nic device.
1621  * @param enable
1622  *   Enable or Disable.
1623  *
1624  * @return
1625  *   0 on success.
1626  *   negative error value otherwise.
1627  */
1628 int hinic_set_xsfp_tx_status(void *hwdev, bool enable)
1629 {
1630         struct hinic_set_xsfp_status xsfp_status;
1631         u16 out_size = sizeof(struct hinic_set_xsfp_status);
1632         int err;
1633
1634         memset(&xsfp_status, 0, sizeof(xsfp_status));
1635         xsfp_status.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1636         xsfp_status.port_id = hinic_global_func_id(hwdev);
1637         xsfp_status.xsfp_tx_dis = ((enable == 0) ? 1 : 0);
1638
1639         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_XSFP_STATUS,
1640                 &xsfp_status, sizeof(struct hinic_set_xsfp_status),
1641                 &xsfp_status, &out_size);
1642         if (err || !out_size || xsfp_status.mgmt_msg_head.status) {
1643                 PMD_DRV_LOG(ERR,
1644                         "Failed to %s port xsfp status, err: %d, status: 0x%x, out size: 0x%x\n",
1645                         enable ? "Disable" : "Enable", err,
1646                         xsfp_status.mgmt_msg_head.status, out_size);
1647                 return -EFAULT;
1648         }
1649
1650         return 0;
1651 }
1652
1653 /**
1654  * hinic_flush_qp_res - Flush tx && rx chip resources in case of set vport
1655  * fake failed when device start.
1656  *
1657  * @param hwdev
1658  *   The hardware interface of a nic device.
1659  *
1660  * @return
1661  *   0 on success.
1662  *   negative error value otherwise.
1663  */
1664 int hinic_flush_qp_res(void *hwdev)
1665 {
1666         struct hinic_clear_qp_resource qp_res;
1667         u16 out_size = sizeof(qp_res);
1668         int err;
1669
1670         memset(&qp_res, 0, sizeof(qp_res));
1671         qp_res.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1672         qp_res.func_id = hinic_global_func_id(hwdev);
1673
1674         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_CLEAR_QP_RES,
1675                                      &qp_res, sizeof(qp_res), &qp_res,
1676                                      &out_size);
1677         if (err || !out_size || qp_res.mgmt_msg_head.status) {
1678                 PMD_DRV_LOG(ERR, "Failed to clear sq resources, err: %d, status: 0x%x, out size: 0x%x",
1679                         err, qp_res.mgmt_msg_head.status, out_size);
1680                 return -EINVAL;
1681         }
1682
1683         return 0;
1684 }
1685
1686 /**
1687  * hinic_vf_get_default_cos - Get default cos of VF.
1688  *
1689  * @param hwdev
1690  *   The hardware interface of a nic device.
1691  * @param cos_id
1692  *   Cos value.
1693  *
1694  * @return
1695  *   0 on success.
1696  *   negative error value otherwise.
1697  */
1698 int hinic_vf_get_default_cos(struct hinic_hwdev *hwdev, u8 *cos_id)
1699 {
1700         struct hinic_vf_default_cos vf_cos;
1701         u16 out_size = sizeof(vf_cos);
1702         int err;
1703
1704         memset(&vf_cos, 0, sizeof(vf_cos));
1705         vf_cos.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1706
1707         err = hinic_msg_to_mgmt_sync(hwdev, HINIC_MOD_L2NIC,
1708                                      HINIC_PORT_CMD_GET_VF_COS, &vf_cos,
1709                                      sizeof(vf_cos), &vf_cos,
1710                                      &out_size, 0);
1711         if (err || !out_size || vf_cos.mgmt_msg_head.status) {
1712                 PMD_DRV_LOG(ERR, "Get VF default cos failed, err: %d, status: 0x%x, out size: 0x%x",
1713                         err, vf_cos.mgmt_msg_head.status, out_size);
1714                 return -EFAULT;
1715         }
1716         *cos_id = vf_cos.state.default_cos;
1717
1718         return 0;
1719 }
1720
1721 /**
1722  * hinic_set_fdir_filter - Set fdir filter for control path
1723  * packet to notify firmware.
1724  *
1725  * @param hwdev
1726  *   The hardware interface of a nic device.
1727  * @param filter_type
1728  *   Packet type to filter.
1729  * @param qid
1730  *   Rx qid to filter.
1731  * @param type_enable
1732  *   The status of pkt type filter.
1733  * @param enable
1734  *   Fdir function Enable or Disable.
1735  * @return
1736  *   0 on success,
1737  *   negative error value otherwise.
1738  */
1739 int hinic_set_fdir_filter(void *hwdev, u8 filter_type, u8 qid, u8 type_enable,
1740                           bool enable)
1741 {
1742         struct hinic_port_qfilter_info port_filer_cmd;
1743         u16 out_size = sizeof(port_filer_cmd);
1744         int err;
1745
1746         if (!hwdev)
1747                 return -EINVAL;
1748
1749         memset(&port_filer_cmd, 0, sizeof(port_filer_cmd));
1750         port_filer_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1751         port_filer_cmd.func_id = hinic_global_func_id(hwdev);
1752         port_filer_cmd.filter_enable = (u8)enable;
1753         port_filer_cmd.filter_type = filter_type;
1754         port_filer_cmd.qid = qid;
1755         port_filer_cmd.filter_type_enable = type_enable;
1756         port_filer_cmd.fdir_flag = 0;
1757
1758         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_Q_FILTER,
1759                         &port_filer_cmd, sizeof(port_filer_cmd),
1760                         &port_filer_cmd, &out_size);
1761         if (err || !out_size || port_filer_cmd.mgmt_msg_head.status) {
1762                 PMD_DRV_LOG(ERR, "Set port Q filter failed, err: %d, status: 0x%x, out size: 0x%x, type: 0x%x,"
1763                         " enable: 0x%x, qid: 0x%x, filter_type_enable: 0x%x\n",
1764                         err, port_filer_cmd.mgmt_msg_head.status, out_size,
1765                         filter_type, enable, qid, type_enable);
1766                 return -EFAULT;
1767         }
1768
1769         return 0;
1770 }
1771
1772 /**
1773  * hinic_set_normal_filter - Set fdir filter for IO path packet.
1774  *
1775  * @param hwdev
1776  *   The hardware interface of a nic device.
1777  * @param qid
1778  *   Rx qid to filter.
1779  * @param normal_type_enable
1780  *   IO path packet function Enable or Disable
1781  * @param key
1782  *   IO path packet filter key value, such as DIP from pkt.
1783  * @param enable
1784  *   Fdir function Enable or Disable.
1785  * @param flag
1786  *   Filter flag, such as dip or others.
1787  * @return
1788  *   0 on success,
1789  *   negative error value otherwise.
1790  */
1791 int hinic_set_normal_filter(void *hwdev, u8 qid, u8 normal_type_enable,
1792                                 u32 key, bool enable, u8 flag)
1793 {
1794         struct hinic_port_qfilter_info port_filer_cmd;
1795         u16 out_size = sizeof(port_filer_cmd);
1796         int err;
1797
1798         if (!hwdev)
1799                 return -EINVAL;
1800
1801         memset(&port_filer_cmd, 0, sizeof(port_filer_cmd));
1802         port_filer_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1803         port_filer_cmd.func_id = hinic_global_func_id(hwdev);
1804         port_filer_cmd.filter_enable = (u8)enable;
1805         port_filer_cmd.qid = qid;
1806         port_filer_cmd.normal_type_enable = normal_type_enable;
1807         port_filer_cmd.fdir_flag = flag; /* fdir flag: support dip */
1808         port_filer_cmd.key = key;
1809
1810         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_Q_FILTER,
1811                         &port_filer_cmd, sizeof(port_filer_cmd),
1812                         &port_filer_cmd, &out_size);
1813         if (err || !out_size || port_filer_cmd.mgmt_msg_head.status) {
1814                 PMD_DRV_LOG(ERR, "Set normal filter failed, err: %d, status: 0x%x, out size: 0x%x, fdir_flag: 0x%x,"
1815                         " enable: 0x%x, qid: 0x%x, normal_type_enable: 0x%x, key:0x%x\n",
1816                         err, port_filer_cmd.mgmt_msg_head.status, out_size,
1817                         flag, enable, qid, normal_type_enable, key);
1818                 return -EFAULT;
1819         }
1820
1821         return 0;
1822 }
1823
1824 /**
1825  * hinic_set_fdir_tcam - Set fdir filter for control packet
1826  * by tcam table to notify hardware.
1827  *
1828  * @param hwdev
1829  *   The hardware interface of a nic device.
1830  * @param type_mask
1831  *   Index of TCAM.
1832  * @param filter_rule
1833  *   TCAM rule for control packet, such as lacp or bgp.
1834  * @param filter_action
1835  *   TCAM action for control packet, such as accept or drop.
1836  * @return
1837  *   0 on success,
1838  *   negative error value otherwise.
1839  */
1840 int hinic_set_fdir_tcam(void *hwdev, u16 type_mask,
1841                         struct tag_pa_rule *filter_rule,
1842                         struct tag_pa_action *filter_action)
1843 {
1844         struct hinic_fdir_tcam_info port_tcam_cmd;
1845         u16 out_size = sizeof(port_tcam_cmd);
1846         int err;
1847
1848         if (!hwdev)
1849                 return -EINVAL;
1850
1851         memset(&port_tcam_cmd, 0, sizeof(port_tcam_cmd));
1852         port_tcam_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1853         port_tcam_cmd.tcam_index = type_mask;
1854         port_tcam_cmd.flag = TCAM_SET;
1855         memcpy((void *)&port_tcam_cmd.filter_rule,
1856                 (void *)filter_rule, sizeof(struct tag_pa_rule));
1857         memcpy((void *)&port_tcam_cmd.filter_action,
1858                 (void *)filter_action, sizeof(struct tag_pa_action));
1859
1860         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_TCAM_FILTER,
1861                         &port_tcam_cmd, sizeof(port_tcam_cmd),
1862                         &port_tcam_cmd, &out_size);
1863         if (err || !out_size || port_tcam_cmd.mgmt_msg_head.status) {
1864                 PMD_DRV_LOG(ERR, "Set tcam table failed, err: %d, status: 0x%x, out size: 0x%x\n",
1865                         err, port_tcam_cmd.mgmt_msg_head.status, out_size);
1866                 return -EFAULT;
1867         }
1868
1869         return 0;
1870 }
1871
1872 /**
1873  * hinic_clear_fdir_tcam - Clear fdir filter TCAM table for control packet.
1874  *
1875  * @param hwdev
1876  *   The hardware interface of a nic device.
1877  * @param type_mask
1878  *   Index of TCAM.
1879  * @return
1880  *   0 on success,
1881  *   negative error value otherwise.
1882  */
1883 int hinic_clear_fdir_tcam(void *hwdev, u16 type_mask)
1884 {
1885         struct hinic_fdir_tcam_info port_tcam_cmd;
1886         u16 out_size = sizeof(port_tcam_cmd);
1887         int err;
1888
1889         if (!hwdev)
1890                 return -EINVAL;
1891
1892         memset(&port_tcam_cmd, 0, sizeof(port_tcam_cmd));
1893         port_tcam_cmd.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
1894         port_tcam_cmd.tcam_index = type_mask;
1895         port_tcam_cmd.flag = TCAM_CLEAR;
1896
1897         err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_TCAM_FILTER,
1898                         &port_tcam_cmd, sizeof(port_tcam_cmd),
1899                         &port_tcam_cmd, &out_size);
1900         if (err || !out_size || port_tcam_cmd.mgmt_msg_head.status) {
1901                 PMD_DRV_LOG(ERR, "Clear tcam table failed, err: %d, status: 0x%x, out size: 0x%x\n",
1902                         err, port_tcam_cmd.mgmt_msg_head.status, out_size);
1903                 return -EFAULT;
1904         }
1905
1906         return 0;
1907 }