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