1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
#include <linux/if_vlan.h>
#include "hinic3_hwdev.h"
#include "hinic3_hwif.h"
#include "hinic3_mbox.h"
#include "hinic3_nic_cfg.h"
#include "hinic3_nic_dev.h"
#include "hinic3_nic_io.h"
static int hinic3_feature_nego(struct hinic3_hwdev *hwdev, u8 opcode,
u64 *s_feature, u16 size)
{
struct l2nic_cmd_feature_nego feature_nego = {};
struct mgmt_msg_params msg_params = {};
int err;
feature_nego.func_id = hinic3_global_func_id(hwdev);
feature_nego.opcode = opcode;
if (opcode == MGMT_MSG_CMD_OP_SET)
memcpy(feature_nego.s_feature, s_feature, size * sizeof(u64));
mgmt_msg_params_init_default(&msg_params, &feature_nego,
sizeof(feature_nego));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_FEATURE_NEGO, &msg_params);
if (err || feature_nego.msg_head.status) {
dev_err(hwdev->dev, "Failed to negotiate nic feature, err:%d, status: 0x%x\n",
err, feature_nego.msg_head.status);
return -EIO;
}
if (opcode == MGMT_MSG_CMD_OP_GET)
memcpy(s_feature, feature_nego.s_feature, size * sizeof(u64));
return 0;
}
int hinic3_set_nic_feature_to_hw(struct hinic3_nic_dev *nic_dev)
{
return hinic3_feature_nego(nic_dev->hwdev, MGMT_MSG_CMD_OP_SET,
&nic_dev->nic_io->feature_cap, 1);
}
bool hinic3_test_support(struct hinic3_nic_dev *nic_dev,
enum hinic3_nic_feature_cap feature_bits)
{
return (nic_dev->nic_io->feature_cap & feature_bits) == feature_bits;
}
void hinic3_update_nic_feature(struct hinic3_nic_dev *nic_dev, u64 feature_cap)
{
nic_dev->nic_io->feature_cap = feature_cap;
}
static int hinic3_set_function_table(struct hinic3_hwdev *hwdev, u32 cfg_bitmap,
const struct l2nic_func_tbl_cfg *cfg)
{
struct l2nic_cmd_set_func_tbl cmd_func_tbl = {};
struct mgmt_msg_params msg_params = {};
int err;
cmd_func_tbl.func_id = hinic3_global_func_id(hwdev);
cmd_func_tbl.cfg_bitmap = cfg_bitmap;
cmd_func_tbl.tbl_cfg = *cfg;
mgmt_msg_params_init_default(&msg_params, &cmd_func_tbl,
sizeof(cmd_func_tbl));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_SET_FUNC_TBL, &msg_params);
if (err || cmd_func_tbl.msg_head.status) {
dev_err(hwdev->dev,
"Failed to set func table, bitmap: 0x%x, err: %d, status: 0x%x\n",
cfg_bitmap, err, cmd_func_tbl.msg_head.status);
return -EFAULT;
}
return 0;
}
int hinic3_set_port_mtu(struct net_device *netdev, u16 new_mtu)
{
struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
struct l2nic_func_tbl_cfg func_tbl_cfg = {};
struct hinic3_hwdev *hwdev = nic_dev->hwdev;
func_tbl_cfg.mtu = new_mtu;
return hinic3_set_function_table(hwdev, BIT(L2NIC_FUNC_TBL_CFG_MTU),
&func_tbl_cfg);
}
static int hinic3_check_mac_info(struct hinic3_hwdev *hwdev, u8 status,
u16 vlan_id)
{
if ((status && status != MGMT_STATUS_EXIST) ||
((vlan_id & BIT(15)) && status == MGMT_STATUS_EXIST)) {
return -EINVAL;
}
return 0;
}
int hinic3_set_mac(struct hinic3_hwdev *hwdev, const u8 *mac_addr, u16 vlan_id,
u16 func_id)
{
struct l2nic_cmd_set_mac mac_info = {};
struct mgmt_msg_params msg_params = {};
int err;
if ((vlan_id & HINIC3_VLAN_ID_MASK) >= VLAN_N_VID) {
dev_err(hwdev->dev, "Invalid VLAN number: %d\n",
(vlan_id & HINIC3_VLAN_ID_MASK));
return -EINVAL;
}
mac_info.func_id = func_id;
mac_info.vlan_id = vlan_id;
ether_addr_copy(mac_info.mac, mac_addr);
mgmt_msg_params_init_default(&msg_params, &mac_info, sizeof(mac_info));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_SET_MAC, &msg_params);
if (err || hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
mac_info.vlan_id)) {
dev_err(hwdev->dev,
"Failed to update MAC, err: %d, status: 0x%x\n",
err, mac_info.msg_head.status);
return -EIO;
}
if (mac_info.msg_head.status == MGMT_STATUS_PF_SET_VF_ALREADY) {
dev_warn(hwdev->dev, "PF has already set VF mac, Ignore set operation\n");
return 0;
}
if (mac_info.msg_head.status == MGMT_STATUS_EXIST) {
dev_warn(hwdev->dev, "MAC is repeated. Ignore update operation\n");
return 0;
}
return 0;
}
int hinic3_del_mac(struct hinic3_hwdev *hwdev, const u8 *mac_addr, u16 vlan_id,
u16 func_id)
{
struct l2nic_cmd_set_mac mac_info = {};
struct mgmt_msg_params msg_params = {};
int err;
if ((vlan_id & HINIC3_VLAN_ID_MASK) >= VLAN_N_VID) {
dev_err(hwdev->dev, "Invalid VLAN number: %d\n",
(vlan_id & HINIC3_VLAN_ID_MASK));
return -EINVAL;
}
mac_info.func_id = func_id;
mac_info.vlan_id = vlan_id;
ether_addr_copy(mac_info.mac, mac_addr);
mgmt_msg_params_init_default(&msg_params, &mac_info, sizeof(mac_info));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_DEL_MAC, &msg_params);
if (err) {
dev_err(hwdev->dev,
"Failed to delete MAC, err: %d, status: 0x%x\n",
err, mac_info.msg_head.status);
return err;
}
return 0;
}
int hinic3_update_mac(struct hinic3_hwdev *hwdev, const u8 *old_mac,
u8 *new_mac, u16 vlan_id, u16 func_id)
{
struct l2nic_cmd_update_mac mac_info = {};
struct mgmt_msg_params msg_params = {};
int err;
if ((vlan_id & HINIC3_VLAN_ID_MASK) >= VLAN_N_VID) {
dev_err(hwdev->dev, "Invalid VLAN number: %d\n",
(vlan_id & HINIC3_VLAN_ID_MASK));
return -EINVAL;
}
mac_info.func_id = func_id;
mac_info.vlan_id = vlan_id;
ether_addr_copy(mac_info.old_mac, old_mac);
ether_addr_copy(mac_info.new_mac, new_mac);
mgmt_msg_params_init_default(&msg_params, &mac_info, sizeof(mac_info));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_UPDATE_MAC, &msg_params);
if (err || hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
mac_info.vlan_id)) {
dev_err(hwdev->dev,
"Failed to update MAC, err: %d, status: 0x%x\n",
err, mac_info.msg_head.status);
return -EIO;
}
return 0;
}
int hinic3_force_drop_tx_pkt(struct hinic3_hwdev *hwdev)
{
struct l2nic_cmd_force_pkt_drop pkt_drop = {};
struct mgmt_msg_params msg_params = {};
int err;
pkt_drop.port = hinic3_physical_port_id(hwdev);
mgmt_msg_params_init_default(&msg_params, &pkt_drop, sizeof(pkt_drop));
err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
L2NIC_CMD_FORCE_PKT_DROP, &msg_params);
if ((pkt_drop.msg_head.status != MGMT_STATUS_CMD_UNSUPPORTED &&
pkt_drop.msg_head.status) || err) {
dev_err(hwdev->dev,
"Failed to set force tx packets drop, err: %d, status: 0x%x\n",
err, pkt_drop.msg_head.status);
return -EFAULT;
}
return pkt_drop.msg_head.status;
}
|