Debian Codegraph @

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/etherdevice.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/irq.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/pci.h>
14 #include <linux/platform_device.h>
15 #include <linux/if_vlan.h>
16 #include <linux/crash_dump.h>
17
18 #include <net/rtnetlink.h>
19
20 #include "hclge_cmd.h"
21 #include "hclge_dcb.h"
22 #include "hclge_main.h"
23 #include "hclge_mbx.h"
24 #include "hclge_mdio.h"
25 #include "hclge_regs.h"
26 #include "hclge_tm.h"
27 #include "hclge_err.h"
28 #include "hnae3.h"
29 #include "hclge_devlink.h"
30 #include "hclge_comm_cmd.h"
31
32 #include "hclge_trace.h"
33
34 #define HCLGE_NAME "hclge"
35
36 #define HCLGE_BUF_SIZE_UNIT 256U
37 #define HCLGE_BUF_MUL_BY 2
38 #define HCLGE_BUF_DIV_BY 2
39 #define NEED_RESERVE_TC_NUM 2
40 #define BUF_MAX_PERCENT 100
41 #define BUF_RESERVE_PERCENT 90
42
43 #define HCLGE_RESET_MAX_FAIL_CNT 5
44 #define HCLGE_RESET_SYNC_TIME 100
45 #define HCLGE_PF_RESET_SYNC_TIME 20
46 #define HCLGE_PF_RESET_SYNC_CNT 1500
47
48 #define HCLGE_LINK_STATUS_MS 10
49
50 static int hclge_set_mac_mtu(struct hclge_dev *hdev, int new_mps);
51 static int hclge_init_vlan_config(struct hclge_dev *hdev);
52 static void hclge_sync_vlan_filter(struct hclge_dev *hdev);
53 static int hclge_reset_ae_dev(struct hnae3_ae_dev *ae_dev);
54 static bool hclge_get_hw_reset_stat(struct hnae3_handle *handle);
55 static void hclge_rfs_filter_expire(struct hclge_dev *hdev);
56 static int hclge_clear_arfs_rules(struct hclge_dev *hdev);
57 static enum hnae3_reset_type hclge_get_reset_level(struct hnae3_ae_dev *ae_dev,
58 unsigned long *addr);
59 static int hclge_set_default_loopback(struct hclge_dev *hdev);
60
61 static void hclge_sync_mac_table(struct hclge_dev *hdev);
62 static void hclge_restore_hw_table(struct hclge_dev *hdev);
63 static void hclge_sync_promisc_mode(struct hclge_dev *hdev);
64 static void hclge_sync_fd_table(struct hclge_dev *hdev);
65 static void hclge_update_fec_stats(struct hclge_dev *hdev);
66 static int hclge_mac_link_status_wait(struct hclge_dev *hdev, int link_ret,
67 int wait_cnt);
68 static int hclge_update_port_info(struct hclge_dev *hdev);
69
70 static struct hnae3_ae_algo ae_algo;
71
72 static struct workqueue_struct *hclge_wq;
73
74 static const struct pci_device_id ae_algo_pci_tbl[] = {
75 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0},
76 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0},
77 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA), 0},
78 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC), 0},
79 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA), 0},
80 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC), 0},
81 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC), 0},
82 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA), 0},
83 /* required last entry */
84 {0, }
85 };
86
87 MODULE_DEVICE_TABLE(pci, ae_algo_pci_tbl);
88
89 static const char hns3_nic_test_strs[][ETH_GSTRING_LEN] = {
90 "External Loopback test",
91 "App Loopback test",
92 "Serdes serial Loopback test",
93 "Serdes parallel Loopback test",
94 "Phy Loopback test"
95 };
96
97 static const struct hclge_comm_stats_str g_mac_stats_string[] = {
98 {"mac_tx_mac_pause_num", HCLGE_MAC_STATS_MAX_NUM_V1,
99 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_mac_pause_num)},
100 {"mac_rx_mac_pause_num", HCLGE_MAC_STATS_MAX_NUM_V1,
101 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_mac_pause_num)},
102 {"mac_tx_pause_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
103 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pause_xoff_time)},
104 {"mac_rx_pause_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
105 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pause_xoff_time)},
106 {"mac_tx_control_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
107 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_ctrl_pkt_num)},
108 {"mac_rx_control_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
109 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_ctrl_pkt_num)},
110 {"mac_tx_pfc_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
111 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pause_pkt_num)},
112 {"mac_tx_pfc_pri0_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
113 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri0_pkt_num)},
114 {"mac_tx_pfc_pri1_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
115 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri1_pkt_num)},
116 {"mac_tx_pfc_pri2_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
117 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri2_pkt_num)},
118 {"mac_tx_pfc_pri3_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
119 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri3_pkt_num)},
120 {"mac_tx_pfc_pri4_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
121 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri4_pkt_num)},
122 {"mac_tx_pfc_pri5_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
123 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri5_pkt_num)},
124 {"mac_tx_pfc_pri6_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
125 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri6_pkt_num)},
126 {"mac_tx_pfc_pri7_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
127 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri7_pkt_num)},
128 {"mac_tx_pfc_pri0_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
129 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri0_xoff_time)},
130 {"mac_tx_pfc_pri1_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
131 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri1_xoff_time)},
132 {"mac_tx_pfc_pri2_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
133 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri2_xoff_time)},
134 {"mac_tx_pfc_pri3_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
135 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri3_xoff_time)},
136 {"mac_tx_pfc_pri4_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
137 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri4_xoff_time)},
138 {"mac_tx_pfc_pri5_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
139 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri5_xoff_time)},
140 {"mac_tx_pfc_pri6_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
141 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri6_xoff_time)},
142 {"mac_tx_pfc_pri7_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
143 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri7_xoff_time)},
144 {"mac_rx_pfc_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
145 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pause_pkt_num)},
146 {"mac_rx_pfc_pri0_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
147 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri0_pkt_num)},
148 {"mac_rx_pfc_pri1_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
149 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri1_pkt_num)},
150 {"mac_rx_pfc_pri2_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
151 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri2_pkt_num)},
152 {"mac_rx_pfc_pri3_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
153 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri3_pkt_num)},
154 {"mac_rx_pfc_pri4_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
155 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri4_pkt_num)},
156 {"mac_rx_pfc_pri5_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
157 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri5_pkt_num)},
158 {"mac_rx_pfc_pri6_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
159 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri6_pkt_num)},
160 {"mac_rx_pfc_pri7_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
161 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri7_pkt_num)},
162 {"mac_rx_pfc_pri0_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
163 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri0_xoff_time)},
164 {"mac_rx_pfc_pri1_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
165 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri1_xoff_time)},
166 {"mac_rx_pfc_pri2_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
167 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri2_xoff_time)},
168 {"mac_rx_pfc_pri3_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
169 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri3_xoff_time)},
170 {"mac_rx_pfc_pri4_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
171 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri4_xoff_time)},
172 {"mac_rx_pfc_pri5_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
173 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri5_xoff_time)},
174 {"mac_rx_pfc_pri6_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
175 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri6_xoff_time)},
176 {"mac_rx_pfc_pri7_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
177 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri7_xoff_time)},
178 {"mac_tx_total_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
179 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_pkt_num)},
180 {"mac_tx_total_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
181 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_oct_num)},
182 {"mac_tx_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
183 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_pkt_num)},
184 {"mac_tx_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
185 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_pkt_num)},
186 {"mac_tx_good_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
187 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_oct_num)},
188 {"mac_tx_bad_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
189 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_oct_num)},
190 {"mac_tx_uni_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
191 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_uni_pkt_num)},
192 {"mac_tx_multi_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
193 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_multi_pkt_num)},
194 {"mac_tx_broad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
195 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_broad_pkt_num)},
196 {"mac_tx_undersize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
197 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undersize_pkt_num)},
198 {"mac_tx_oversize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
199 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_oversize_pkt_num)},
200 {"mac_tx_64_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
201 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_64_oct_pkt_num)},
202 {"mac_tx_65_127_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
203 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_65_127_oct_pkt_num)},
204 {"mac_tx_128_255_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
205 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_128_255_oct_pkt_num)},
206 {"mac_tx_256_511_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
207 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_256_511_oct_pkt_num)},
208 {"mac_tx_512_1023_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
209 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_512_1023_oct_pkt_num)},
210 {"mac_tx_1024_1518_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
211 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1024_1518_oct_pkt_num)},
212 {"mac_tx_1519_2047_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
213 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_2047_oct_pkt_num)},
214 {"mac_tx_2048_4095_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
215 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_2048_4095_oct_pkt_num)},
216 {"mac_tx_4096_8191_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
217 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_4096_8191_oct_pkt_num)},
218 {"mac_tx_8192_9216_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
219 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_8192_9216_oct_pkt_num)},
220 {"mac_tx_9217_12287_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
221 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_9217_12287_oct_pkt_num)},
222 {"mac_tx_12288_16383_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
223 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_12288_16383_oct_pkt_num)},
224 {"mac_tx_1519_max_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
225 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_good_oct_pkt_num)},
226 {"mac_tx_1519_max_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
227 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_bad_oct_pkt_num)},
228 {"mac_rx_total_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
229 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_pkt_num)},
230 {"mac_rx_total_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
231 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_oct_num)},
232 {"mac_rx_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
233 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_pkt_num)},
234 {"mac_rx_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
235 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_pkt_num)},
236 {"mac_rx_good_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
237 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_oct_num)},
238 {"mac_rx_bad_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
239 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_oct_num)},
240 {"mac_rx_uni_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
241 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_uni_pkt_num)},
242 {"mac_rx_multi_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
243 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_multi_pkt_num)},
244 {"mac_rx_broad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
245 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_broad_pkt_num)},
246 {"mac_rx_undersize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
247 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undersize_pkt_num)},
248 {"mac_rx_oversize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
249 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_oversize_pkt_num)},
250 {"mac_rx_64_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
251 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_64_oct_pkt_num)},
252 {"mac_rx_65_127_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
253 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_65_127_oct_pkt_num)},
254 {"mac_rx_128_255_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
255 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_128_255_oct_pkt_num)},
256 {"mac_rx_256_511_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
257 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_256_511_oct_pkt_num)},
258 {"mac_rx_512_1023_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
259 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_512_1023_oct_pkt_num)},
260 {"mac_rx_1024_1518_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
261 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1024_1518_oct_pkt_num)},
262 {"mac_rx_1519_2047_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
263 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_2047_oct_pkt_num)},
264 {"mac_rx_2048_4095_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
265 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_2048_4095_oct_pkt_num)},
266 {"mac_rx_4096_8191_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
267 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_4096_8191_oct_pkt_num)},
268 {"mac_rx_8192_9216_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
269 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_8192_9216_oct_pkt_num)},
270 {"mac_rx_9217_12287_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
271 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_9217_12287_oct_pkt_num)},
272 {"mac_rx_12288_16383_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
273 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_12288_16383_oct_pkt_num)},
274 {"mac_rx_1519_max_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
275 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_good_oct_pkt_num)},
276 {"mac_rx_1519_max_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
277 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_bad_oct_pkt_num)},
278
279 {"mac_tx_fragment_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
280 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_fragment_pkt_num)},
281 {"mac_tx_undermin_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
282 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undermin_pkt_num)},
283 {"mac_tx_jabber_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
284 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_jabber_pkt_num)},
285 {"mac_tx_err_all_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
286 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_err_all_pkt_num)},
287 {"mac_tx_from_app_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
288 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_good_pkt_num)},
289 {"mac_tx_from_app_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
290 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_bad_pkt_num)},
291 {"mac_rx_fragment_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
292 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fragment_pkt_num)},
293 {"mac_rx_undermin_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
294 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undermin_pkt_num)},
295 {"mac_rx_jabber_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
296 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_jabber_pkt_num)},
297 {"mac_rx_fcs_err_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
298 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fcs_err_pkt_num)},
299 {"mac_rx_send_app_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
300 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_good_pkt_num)},
301 {"mac_rx_send_app_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
302 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_bad_pkt_num)}
303 };
304
305 static const struct hclge_mac_mgr_tbl_entry_cmd hclge_mgr_table[] = {
306 {
307 .flags = HCLGE_MAC_MGR_MASK_VLAN_B,
308 .ethter_type = cpu_to_le16(ETH_P_LLDP),
309 .mac_addr = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e},
310 .i_port_bitmap = 0x1,
311 },
312 };
313
314 static const struct key_info meta_data_key_info[] = {
315 { PACKET_TYPE_ID, 6 },
316 { IP_FRAGEMENT, 1 },
317 { ROCE_TYPE, 1 },
318 { NEXT_KEY, 5 },
319 { VLAN_NUMBER, 2 },
320 { SRC_VPORT, 12 },
321 { DST_VPORT, 12 },
322 { TUNNEL_PACKET, 1 },
323 };
324
325 static const struct key_info tuple_key_info[] = {
326 { OUTER_DST_MAC, 48, KEY_OPT_MAC, -1, -1 },
327 { OUTER_SRC_MAC, 48, KEY_OPT_MAC, -1, -1 },
328 { OUTER_VLAN_TAG_FST, 16, KEY_OPT_LE16, -1, -1 },
329 { OUTER_VLAN_TAG_SEC, 16, KEY_OPT_LE16, -1, -1 },
330 { OUTER_ETH_TYPE, 16, KEY_OPT_LE16, -1, -1 },
331 { OUTER_L2_RSV, 16, KEY_OPT_LE16, -1, -1 },
332 { OUTER_IP_TOS, 8, KEY_OPT_U8, -1, -1 },
333 { OUTER_IP_PROTO, 8, KEY_OPT_U8, -1, -1 },
334 { OUTER_SRC_IP, 32, KEY_OPT_IP, -1, -1 },
335 { OUTER_DST_IP, 32, KEY_OPT_IP, -1, -1 },
336 { OUTER_L3_RSV, 16, KEY_OPT_LE16, -1, -1 },
337 { OUTER_SRC_PORT, 16, KEY_OPT_LE16, -1, -1 },
338 { OUTER_DST_PORT, 16, KEY_OPT_LE16, -1, -1 },
339 { OUTER_L4_RSV, 32, KEY_OPT_LE32, -1, -1 },
340 { OUTER_TUN_VNI, 24, KEY_OPT_VNI, -1, -1 },
341 { OUTER_TUN_FLOW_ID, 8, KEY_OPT_U8, -1, -1 },
342 { INNER_DST_MAC, 48, KEY_OPT_MAC,
343 offsetof(struct hclge_fd_rule, tuples.dst_mac),
344 offsetof(struct hclge_fd_rule, tuples_mask.dst_mac) },
345 { INNER_SRC_MAC, 48, KEY_OPT_MAC,
346 offsetof(struct hclge_fd_rule, tuples.src_mac),
347 offsetof(struct hclge_fd_rule, tuples_mask.src_mac) },
348 { INNER_VLAN_TAG_FST, 16, KEY_OPT_LE16,
349 offsetof(struct hclge_fd_rule, tuples.vlan_tag1),
350 offsetof(struct hclge_fd_rule, tuples_mask.vlan_tag1) },
351 { INNER_VLAN_TAG_SEC, 16, KEY_OPT_LE16, -1, -1 },
352 { INNER_ETH_TYPE, 16, KEY_OPT_LE16,
353 offsetof(struct hclge_fd_rule, tuples.ether_proto),
354 offsetof(struct hclge_fd_rule, tuples_mask.ether_proto) },
355 { INNER_L2_RSV, 16, KEY_OPT_LE16,
356 offsetof(struct hclge_fd_rule, tuples.l2_user_def),
357 offsetof(struct hclge_fd_rule, tuples_mask.l2_user_def) },
358 { INNER_IP_TOS, 8, KEY_OPT_U8,
359 offsetof(struct hclge_fd_rule, tuples.ip_tos),
360 offsetof(struct hclge_fd_rule, tuples_mask.ip_tos) },
361 { INNER_IP_PROTO, 8, KEY_OPT_U8,
362 offsetof(struct hclge_fd_rule, tuples.ip_proto),
363 offsetof(struct hclge_fd_rule, tuples_mask.ip_proto) },
364 { INNER_SRC_IP, 32, KEY_OPT_IP,
365 offsetof(struct hclge_fd_rule, tuples.src_ip),
366 offsetof(struct hclge_fd_rule, tuples_mask.src_ip) },
367 { INNER_DST_IP, 32, KEY_OPT_IP,
368 offsetof(struct hclge_fd_rule, tuples.dst_ip),
369 offsetof(struct hclge_fd_rule, tuples_mask.dst_ip) },
370 { INNER_L3_RSV, 16, KEY_OPT_LE16,
371 offsetof(struct hclge_fd_rule, tuples.l3_user_def),
372 offsetof(struct hclge_fd_rule, tuples_mask.l3_user_def) },
373 { INNER_SRC_PORT, 16, KEY_OPT_LE16,
374 offsetof(struct hclge_fd_rule, tuples.src_port),
375 offsetof(struct hclge_fd_rule, tuples_mask.src_port) },
376 { INNER_DST_PORT, 16, KEY_OPT_LE16,
377 offsetof(struct hclge_fd_rule, tuples.dst_port),
378 offsetof(struct hclge_fd_rule, tuples_mask.dst_port) },
379 { INNER_L4_RSV, 32, KEY_OPT_LE32,
380 offsetof(struct hclge_fd_rule, tuples.l4_user_def),
381 offsetof(struct hclge_fd_rule, tuples_mask.l4_user_def) },
382 };
383
384 /**
385 * hclge_cmd_send - send command to command queue
386 * @hw: pointer to the hw struct
387 * @desc: prefilled descriptor for describing the command
388 * @num : the number of descriptors to be sent
389 *
390 * This is the main send command for command queue, it
391 * sends the queue, cleans the queue, etc
392 **/
393 int hclge_cmd_send(struct hclge_hw *hw, struct hclge_desc *desc, int num)
394 {
395 return hclge_comm_cmd_send(&hw->hw, desc, num);
396 }
397
398 static void hclge_trace_cmd_send(struct hclge_comm_hw *hw, struct hclge_desc *desc,
399 int num, bool is_special)
400 {
401 int i;
402
403 trace_hclge_pf_cmd_send(hw, desc, 0, num);
404
405 if (!is_special) {
406 for (i = 1; i < num; i++)
407 trace_hclge_pf_cmd_send(hw, &desc[i], i, num);
408 } else {
409 for (i = 1; i < num; i++)
410 trace_hclge_pf_special_cmd_send(hw, (__le32 *)&desc[i],
411 i, num);
412 }
413 }
414
415 static void hclge_trace_cmd_get(struct hclge_comm_hw *hw, struct hclge_desc *desc,
416 int num, bool is_special)
417 {
418 int i;
419
420 if (!HCLGE_COMM_SEND_SYNC(le16_to_cpu(desc->flag)))
421 return;
422
423 trace_hclge_pf_cmd_get(hw, desc, 0, num);
424
425 if (!is_special) {
426 for (i = 1; i < num; i++)
427 trace_hclge_pf_cmd_get(hw, &desc[i], i, num);
428 } else {
429 for (i = 1; i < num; i++)
430 trace_hclge_pf_special_cmd_get(hw, (__le32 *)&desc[i],
431 i, num);
432 }
433 }
434
435 static const struct hclge_comm_cmq_ops hclge_cmq_ops = {
436 .trace_cmd_send = hclge_trace_cmd_send,
437 .trace_cmd_get = hclge_trace_cmd_get,
438 };
439
440 static int hclge_mac_update_stats_defective(struct hclge_dev *hdev)
441 {
442 #define HCLGE_MAC_CMD_NUM 21
443
444 u64 *data = (u64 *)(&hdev->mac_stats);
445 struct hclge_desc desc[HCLGE_MAC_CMD_NUM];
446 __le64 *desc_data;
447 u32 data_size;
448 int ret;
449 u32 i;
450
451 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_STATS_MAC, true);
452 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_MAC_CMD_NUM);
453 if (ret) {
454 dev_err(&hdev->pdev->dev,
455 "Get MAC pkt stats fail, status = %d.\n", ret);
456
457 return ret;
458 }
459
460 /* The first desc has a 64-bit header, so data size need to minus 1 */
461 data_size = sizeof(desc) / (sizeof(u64)) - 1;
462
463 desc_data = (__le64 *)(&desc[0].data[0]);
464 for (i = 0; i < data_size; i++) {
465 /* data memory is continuous becase only the first desc has a
466 * header in this command
467 */
468 *data += le64_to_cpu(*desc_data);
469 data++;
470 desc_data++;
471 }
472
473 return 0;
474 }
475
476 static int hclge_mac_update_stats_complete(struct hclge_dev *hdev)
477 {
478 #define HCLGE_REG_NUM_PER_DESC 4
479
480 u32 reg_num = hdev->ae_dev->dev_specs.mac_stats_num;
481 u64 *data = (u64 *)(&hdev->mac_stats);
482 struct hclge_desc *desc;
483 __le64 *desc_data;
484 u32 data_size;
485 u32 desc_num;
486 int ret;
487 u32 i;
488
489 /* The first desc has a 64-bit header, so need to consider it */
490 desc_num = reg_num / HCLGE_REG_NUM_PER_DESC + 1;
491
492 /* This may be called inside atomic sections,
493 * so GFP_ATOMIC is more suitable here
494 */
495 desc = kzalloc_objs(struct hclge_desc, desc_num, GFP_ATOMIC);
496 if (!desc)
497 return -ENOMEM;
498
499 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_STATS_MAC_ALL, true);
500 ret = hclge_cmd_send(&hdev->hw, desc, desc_num);
501 if (ret) {
502 kfree(desc);
503 return ret;
504 }
505
506 data_size = min_t(u32, sizeof(hdev->mac_stats) / sizeof(u64), reg_num);
507
508 desc_data = (__le64 *)(&desc[0].data[0]);
509 for (i = 0; i < data_size; i++) {
510 /* data memory is continuous becase only the first desc has a
511 * header in this command
512 */
513 *data += le64_to_cpu(*desc_data);
514 data++;
515 desc_data++;
516 }
517
518 kfree(desc);
519
520 return 0;
521 }
522
523 static int hclge_mac_query_reg_num(struct hclge_dev *hdev, u32 *reg_num)
524 {
525 struct hclge_desc desc;
526 int ret;
527
528 /* Driver needs total register number of both valid registers and
529 * reserved registers, but the old firmware only returns number
530 * of valid registers in device V2. To be compatible with these
531 * devices, driver uses a fixed value.
532 */
533 if (hdev->ae_dev->dev_version == HNAE3_DEVICE_VERSION_V2) {
534 *reg_num = HCLGE_MAC_STATS_MAX_NUM_V1;
535 return 0;
536 }
537
538 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_MAC_REG_NUM, true);
539 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
540 if (ret) {
541 dev_err(&hdev->pdev->dev,
542 "failed to query mac statistic reg number, ret = %d\n",
543 ret);
544 return ret;
545 }
546
547 *reg_num = le32_to_cpu(desc.data[0]);
548 if (*reg_num == 0) {
549 dev_err(&hdev->pdev->dev,
550 "mac statistic reg number is invalid!\n");
551 return -ENODATA;
552 }
553
554 return 0;
555 }
556
557 int hclge_mac_update_stats(struct hclge_dev *hdev)
558 {
559 /* The firmware supports the new statistics acquisition method */
560 if (hdev->ae_dev->dev_specs.mac_stats_num)
561 return hclge_mac_update_stats_complete(hdev);
562 else
563 return hclge_mac_update_stats_defective(hdev);
564 }
565
566 static int hclge_comm_get_count(struct hclge_dev *hdev,
567 const struct hclge_comm_stats_str strs[],
568 u32 size)
569 {
570 int count = 0;
571 u32 i;
572
573 for (i = 0; i < size; i++)
574 if (strs[i].stats_num <= hdev->ae_dev->dev_specs.mac_stats_num)
575 count++;
576
577 return count;
578 }
579
580 static u64 *hclge_comm_get_stats(struct hclge_dev *hdev,
581 const struct hclge_comm_stats_str strs[],
582 int size, u64 *data)
583 {
584 u64 *buf = data;
585 int i;
586
587 for (i = 0; i < size; i++) {
588 if (strs[i].stats_num > hdev->ae_dev->dev_specs.mac_stats_num)
589 continue;
590
591 *buf = HCLGE_STATS_READ(&hdev->mac_stats, strs[i].offset);
592 buf++;
593 }
594
595 return buf;
596 }
597
598 static void hclge_comm_get_strings(struct hclge_dev *hdev, u32 stringset,
599 const struct hclge_comm_stats_str strs[],
600 int size, u8 **data)
601 {
602 int i;
603
604 if (stringset != ETH_SS_STATS)
605 return;
606
607 for (i = 0; i < size; i++) {
608 if (strs[i].stats_num > hdev->ae_dev->dev_specs.mac_stats_num)
609 continue;
610
611 ethtool_puts(data, strs[i].desc);
612 }
613 }
614
615 static void hclge_update_stats_for_all(struct hclge_dev *hdev)
616 {
617 struct hnae3_handle *handle;
618 int status;
619
620 handle = &hdev->vport[0].nic;
621 if (handle->client) {
622 status = hclge_comm_tqps_update_stats(handle, &hdev->hw.hw);
623 if (status) {
624 dev_err(&hdev->pdev->dev,
625 "Update TQPS stats fail, status = %d.\n",
626 status);
627 }
628 }
629
630 hclge_update_fec_stats(hdev);
631
632 status = hclge_mac_update_stats(hdev);
633 if (status)
634 dev_err(&hdev->pdev->dev,
635 "Update MAC stats fail, status = %d.\n", status);
636 }
637
638 static void hclge_update_stats(struct hnae3_handle *handle)
639 {
640 struct hclge_vport *vport = hclge_get_vport(handle);
641 struct hclge_dev *hdev = vport->back;
642 int status;
643
644 if (test_and_set_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state))
645 return;
646
647 status = hclge_mac_update_stats(hdev);
648 if (status)
649 dev_err(&hdev->pdev->dev,
650 "Update MAC stats fail, status = %d.\n",
651 status);
652
653 status = hclge_comm_tqps_update_stats(handle, &hdev->hw.hw);
654 if (status)
655 dev_err(&hdev->pdev->dev,
656 "Update TQPS stats fail, status = %d.\n",
657 status);
658
659 clear_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state);
660 }
661
662 static int hclge_get_sset_count(struct hnae3_handle *handle, int stringset)
663 {
664 #define HCLGE_LOOPBACK_TEST_FLAGS (HNAE3_SUPPORT_APP_LOOPBACK | \
665 HNAE3_SUPPORT_PHY_LOOPBACK | \
666 HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK | \
667 HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK | \
668 HNAE3_SUPPORT_EXTERNAL_LOOPBACK)
669
670 struct hclge_vport *vport = hclge_get_vport(handle);
671 struct hclge_dev *hdev = vport->back;
672 int count = 0;
673
674 /* Loopback test support rules:
675 * mac: only GE mode support
676 * serdes: all mac mode will support include GE/XGE/LGE/CGE
677 * phy: only support when phy device exist on board
678 */
679 if (stringset == ETH_SS_TEST) {
680 /* clear loopback bit flags at first */
681 handle->flags = (handle->flags & (~HCLGE_LOOPBACK_TEST_FLAGS));
682 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2 ||
683 hdev->hw.mac.speed == HCLGE_MAC_SPEED_10M ||
684 hdev->hw.mac.speed == HCLGE_MAC_SPEED_100M ||
685 hdev->hw.mac.speed == HCLGE_MAC_SPEED_1G) {
686 count += 1;
687 handle->flags |= HNAE3_SUPPORT_APP_LOOPBACK;
688 }
689
690 if (hdev->ae_dev->dev_specs.hilink_version !=
691 HCLGE_HILINK_H60) {
692 count += 1;
693 handle->flags |= HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
694 }
695
696 count += 1;
697 handle->flags |= HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
698 count += 1;
699 handle->flags |= HNAE3_SUPPORT_EXTERNAL_LOOPBACK;
700
701 if ((hdev->hw.mac.phydev && hdev->hw.mac.phydev->drv &&
702 hdev->hw.mac.phydev->drv->set_loopback) ||
703 hnae3_dev_phy_imp_supported(hdev)) {
704 count += 1;
705 handle->flags |= HNAE3_SUPPORT_PHY_LOOPBACK;
706 }
707 } else if (stringset == ETH_SS_STATS) {
708 count = hclge_comm_get_count(hdev, g_mac_stats_string,
709 ARRAY_SIZE(g_mac_stats_string)) +
710 hclge_comm_tqps_get_sset_count(handle);
711 }
712
713 return count;
714 }
715
716 static void hclge_get_strings(struct hnae3_handle *handle, u32 stringset,
717 u8 **data)
718 {
719 struct hclge_vport *vport = hclge_get_vport(handle);
720 struct hclge_dev *hdev = vport->back;
721 const char *str;
722 int size;
723
724 if (stringset == ETH_SS_STATS) {
725 size = ARRAY_SIZE(g_mac_stats_string);
726 hclge_comm_get_strings(hdev, stringset, g_mac_stats_string,
727 size, data);
728 hclge_comm_tqps_get_strings(handle, data);
729 } else if (stringset == ETH_SS_TEST) {
730 if (handle->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK) {
731 str = hns3_nic_test_strs[HNAE3_LOOP_EXTERNAL];
732 ethtool_puts(data, str);
733 }
734 if (handle->flags & HNAE3_SUPPORT_APP_LOOPBACK) {
735 str = hns3_nic_test_strs[HNAE3_LOOP_APP];
736 ethtool_puts(data, str);
737 }
738 if (handle->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK) {
739 str = hns3_nic_test_strs[HNAE3_LOOP_SERIAL_SERDES];
740 ethtool_puts(data, str);
741 }
742 if (handle->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK) {
743 str = hns3_nic_test_strs[HNAE3_LOOP_PARALLEL_SERDES];
744 ethtool_puts(data, str);
745 }
746 if (handle->flags & HNAE3_SUPPORT_PHY_LOOPBACK) {
747 str = hns3_nic_test_strs[HNAE3_LOOP_PHY];
748 ethtool_puts(data, str);
749 }
750 }
751 }
752
753 static void hclge_get_stats(struct hnae3_handle *handle, u64 *data)
754 {
755 struct hclge_vport *vport = hclge_get_vport(handle);
756 struct hclge_dev *hdev = vport->back;
757 u64 *p;
758
759 p = hclge_comm_get_stats(hdev, g_mac_stats_string,
760 ARRAY_SIZE(g_mac_stats_string), data);
761 p = hclge_comm_tqps_get_stats(handle, p);
762 }
763
764 static void hclge_get_mac_stat(struct hnae3_handle *handle,
765 struct hns3_mac_stats *mac_stats)
766 {
767 struct hclge_vport *vport = hclge_get_vport(handle);
768 struct hclge_dev *hdev = vport->back;
769
770 hclge_update_stats(handle);
771
772 mac_stats->tx_pause_cnt = hdev->mac_stats.mac_tx_mac_pause_num;
773 mac_stats->rx_pause_cnt = hdev->mac_stats.mac_rx_mac_pause_num;
774 }
775
776 static int hclge_parse_func_status(struct hclge_dev *hdev,
777 struct hclge_func_status_cmd *status)
778 {
779 #define HCLGE_MAC_ID_MASK 0xF
780
781 if (!(status->pf_state & HCLGE_PF_STATE_DONE))
782 return -EINVAL;
783
784 /* Set the pf to main pf */
785 if (status->pf_state & HCLGE_PF_STATE_MAIN)
786 hdev->flag |= HCLGE_FLAG_MAIN;
787 else
788 hdev->flag &= ~HCLGE_FLAG_MAIN;
789
790 hdev->hw.mac.mac_id = status->mac_id & HCLGE_MAC_ID_MASK;
791 return 0;
792 }
793
794 static int hclge_query_function_status(struct hclge_dev *hdev)
795 {
796 #define HCLGE_QUERY_MAX_CNT 5
797
798 struct hclge_func_status_cmd *req;
799 struct hclge_desc desc;
800 int timeout = 0;
801 int ret;
802
803 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_FUNC_STATUS, true);
804 req = (struct hclge_func_status_cmd *)desc.data;
805
806 do {
807 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
808 if (ret) {
809 dev_err(&hdev->pdev->dev,
810 "query function status failed %d.\n", ret);
811 return ret;
812 }
813
814 /* Check pf reset is done */
815 if (req->pf_state)
816 break;
817 usleep_range(1000, 2000);
818 } while (timeout++ < HCLGE_QUERY_MAX_CNT);
819
820 return hclge_parse_func_status(hdev, req);
821 }
822
823 static int hclge_query_pf_resource(struct hclge_dev *hdev)
824 {
825 struct hclge_pf_res_cmd *req;
826 struct hclge_desc desc;
827 int ret;
828
829 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_PF_RSRC, true);
830 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
831 if (ret) {
832 dev_err(&hdev->pdev->dev,
833 "query pf resource failed %d.\n", ret);
834 return ret;
835 }
836
837 req = (struct hclge_pf_res_cmd *)desc.data;
838 hdev->num_tqps = le16_to_cpu(req->tqp_num) +
839 le16_to_cpu(req->ext_tqp_num);
840 hdev->pkt_buf_size = le16_to_cpu(req->buf_size) << HCLGE_BUF_UNIT_S;
841
842 if (req->tx_buf_size)
843 hdev->tx_buf_size =
844 le16_to_cpu(req->tx_buf_size) << HCLGE_BUF_UNIT_S;
845 else
846 hdev->tx_buf_size = HCLGE_DEFAULT_TX_BUF;
847
848 hdev->tx_buf_size = roundup(hdev->tx_buf_size, HCLGE_BUF_SIZE_UNIT);
849
850 if (req->dv_buf_size)
851 hdev->dv_buf_size =
852 le16_to_cpu(req->dv_buf_size) << HCLGE_BUF_UNIT_S;
853 else
854 hdev->dv_buf_size = HCLGE_DEFAULT_DV;
855
856 hdev->dv_buf_size = roundup(hdev->dv_buf_size, HCLGE_BUF_SIZE_UNIT);
857
858 hdev->num_nic_msi = le16_to_cpu(req->msixcap_localid_number_nic);
859 if (hdev->num_nic_msi < HNAE3_MIN_VECTOR_NUM) {
860 dev_err(&hdev->pdev->dev,
861 "only %u msi resources available, not enough for pf(min:2).\n",
862 hdev->num_nic_msi);
863 return -EINVAL;
864 }
865
866 if (hnae3_dev_roce_supported(hdev)) {
867 hdev->num_roce_msi =
868 le16_to_cpu(req->pf_intr_vector_number_roce);
869
870 /* PF should have NIC vectors and Roce vectors,
871 * NIC vectors are queued before Roce vectors.
872 */
873 hdev->num_msi = hdev->num_nic_msi + hdev->num_roce_msi;
874 } else {
875 hdev->num_msi = hdev->num_nic_msi;
876 }
877
878 return 0;
879 }
880
881 static int hclge_parse_speed(u8 speed_cmd, u32 *speed)
882 {
883 switch (speed_cmd) {
884 case HCLGE_FW_MAC_SPEED_10M:
885 *speed = HCLGE_MAC_SPEED_10M;
886 break;
887 case HCLGE_FW_MAC_SPEED_100M:
888 *speed = HCLGE_MAC_SPEED_100M;
889 break;
890 case HCLGE_FW_MAC_SPEED_1G:
891 *speed = HCLGE_MAC_SPEED_1G;
892 break;
893 case HCLGE_FW_MAC_SPEED_10G:
894 *speed = HCLGE_MAC_SPEED_10G;
895 break;
896 case HCLGE_FW_MAC_SPEED_25G:
897 *speed = HCLGE_MAC_SPEED_25G;
898 break;
899 case HCLGE_FW_MAC_SPEED_40G:
900 *speed = HCLGE_MAC_SPEED_40G;
901 break;
902 case HCLGE_FW_MAC_SPEED_50G:
903 *speed = HCLGE_MAC_SPEED_50G;
904 break;
905 case HCLGE_FW_MAC_SPEED_100G:
906 *speed = HCLGE_MAC_SPEED_100G;
907 break;
908 case HCLGE_FW_MAC_SPEED_200G:
909 *speed = HCLGE_MAC_SPEED_200G;
910 break;
911 default:
912 return -EINVAL;
913 }
914
915 return 0;
916 }
917
918 static const struct hclge_speed_bit_map speed_bit_map[] = {
919 {HCLGE_MAC_SPEED_10M, HCLGE_SUPPORT_10M_BIT},
920 {HCLGE_MAC_SPEED_100M, HCLGE_SUPPORT_100M_BIT},
921 {HCLGE_MAC_SPEED_1G, HCLGE_SUPPORT_1G_BIT},
922 {HCLGE_MAC_SPEED_10G, HCLGE_SUPPORT_10G_BIT},
923 {HCLGE_MAC_SPEED_25G, HCLGE_SUPPORT_25G_BIT},
924 {HCLGE_MAC_SPEED_40G, HCLGE_SUPPORT_40G_BIT},
925 {HCLGE_MAC_SPEED_50G, HCLGE_SUPPORT_50G_BITS},
926 {HCLGE_MAC_SPEED_100G, HCLGE_SUPPORT_100G_BITS},
927 {HCLGE_MAC_SPEED_200G, HCLGE_SUPPORT_200G_BITS},
928 };
929
930 static int hclge_get_speed_bit(u32 speed, u32 *speed_bit)
931 {
932 u16 i;
933
934 for (i = 0; i < ARRAY_SIZE(speed_bit_map); i++) {
935 if (speed == speed_bit_map[i].speed) {
936 *speed_bit = speed_bit_map[i].speed_bit;
937 return 0;
938 }
939 }
940
941 return -EINVAL;
942 }
943
944 static int hclge_check_port_speed(struct hnae3_handle *handle, u32 speed)
945 {
946 struct hclge_vport *vport = hclge_get_vport(handle);
947 struct hclge_dev *hdev = vport->back;
948 u32 speed_ability = hdev->hw.mac.speed_ability;
949 u32 speed_bit = 0;
950 int ret;
951
952 ret = hclge_get_speed_bit(speed, &speed_bit);
953 if (ret)
954 return ret;
955
956 if (speed_bit & speed_ability)
957 return 0;
958
959 return -EINVAL;
960 }
961
962 static void hclge_update_fec_support(struct hclge_mac *mac)
963 {
964 linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, mac->supported);
965 linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, mac->supported);
966 linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, mac->supported);
967 linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
968
969 if (mac->fec_ability & BIT(HNAE3_FEC_BASER))
970 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
971 mac->supported);
972 if (mac->fec_ability & BIT(HNAE3_FEC_RS))
973 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
974 mac->supported);
975 if (mac->fec_ability & BIT(HNAE3_FEC_LLRS))
976 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
977 mac->supported);
978 if (mac->fec_ability & BIT(HNAE3_FEC_NONE))
979 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
980 mac->supported);
981 }
982
983 static const struct hclge_link_mode_bmap hclge_sr_link_mode_bmap[] = {
984 {HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseSR_Full_BIT},
985 {HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseSR_Full_BIT},
986 {HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT},
987 {HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT},
988 {HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseSR_Full_BIT},
989 {HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT},
990 {HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT},
991 {HCLGE_SUPPORT_200G_R4_EXT_BIT,
992 ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT},
993 {HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT},
994 };
995
996 static const struct hclge_link_mode_bmap hclge_lr_link_mode_bmap[] = {
997 {HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseLR_Full_BIT},
998 {HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT},
999 {HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT},
1000 {HCLGE_SUPPORT_100G_R4_BIT,
1001 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT},
1002 {HCLGE_SUPPORT_100G_R2_BIT,
1003 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT},
1004 {HCLGE_SUPPORT_200G_R4_EXT_BIT,
1005 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT},
1006 {HCLGE_SUPPORT_200G_R4_BIT,
1007 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT},
1008 };
1009
1010 static const struct hclge_link_mode_bmap hclge_cr_link_mode_bmap[] = {
1011 {HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseCR_Full_BIT},
1012 {HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseCR_Full_BIT},
1013 {HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT},
1014 {HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT},
1015 {HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseCR_Full_BIT},
1016 {HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT},
1017 {HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT},
1018 {HCLGE_SUPPORT_200G_R4_EXT_BIT,
1019 ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT},
1020 {HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT},
1021 };
1022
1023 static const struct hclge_link_mode_bmap hclge_kr_link_mode_bmap[] = {
1024 {HCLGE_SUPPORT_1G_BIT, ETHTOOL_LINK_MODE_1000baseKX_Full_BIT},
1025 {HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseKR_Full_BIT},
1026 {HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseKR_Full_BIT},
1027 {HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT},
1028 {HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT},
1029 {HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseKR_Full_BIT},
1030 {HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT},
1031 {HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT},
1032 {HCLGE_SUPPORT_200G_R4_EXT_BIT,
1033 ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT},
1034 {HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT},
1035 };
1036
1037 static void hclge_convert_setting_sr(u16 speed_ability,
1038 unsigned long *link_mode)
1039 {
1040 int i;
1041
1042 for (i = 0; i < ARRAY_SIZE(hclge_sr_link_mode_bmap); i++) {
1043 if (speed_ability & hclge_sr_link_mode_bmap[i].support_bit)
1044 linkmode_set_bit(hclge_sr_link_mode_bmap[i].link_mode,
1045 link_mode);
1046 }
1047 }
1048
1049 static void hclge_convert_setting_lr(u16 speed_ability,
1050 unsigned long *link_mode)
1051 {
1052 int i;
1053
1054 for (i = 0; i < ARRAY_SIZE(hclge_lr_link_mode_bmap); i++) {
1055 if (speed_ability & hclge_lr_link_mode_bmap[i].support_bit)
1056 linkmode_set_bit(hclge_lr_link_mode_bmap[i].link_mode,
1057 link_mode);
1058 }
1059 }
1060
1061 static void hclge_convert_setting_cr(u16 speed_ability,
1062 unsigned long *link_mode)
1063 {
1064 int i;
1065
1066 for (i = 0; i < ARRAY_SIZE(hclge_cr_link_mode_bmap); i++) {
1067 if (speed_ability & hclge_cr_link_mode_bmap[i].support_bit)
1068 linkmode_set_bit(hclge_cr_link_mode_bmap[i].link_mode,
1069 link_mode);
1070 }
1071 }
1072
1073 static void hclge_convert_setting_kr(u16 speed_ability,
1074 unsigned long *link_mode)
1075 {
1076 int i;
1077
1078 for (i = 0; i < ARRAY_SIZE(hclge_kr_link_mode_bmap); i++) {
1079 if (speed_ability & hclge_kr_link_mode_bmap[i].support_bit)
1080 linkmode_set_bit(hclge_kr_link_mode_bmap[i].link_mode,
1081 link_mode);
1082 }
1083 }
1084
1085 static void hclge_convert_setting_fec(struct hclge_mac *mac)
1086 {
1087 /* If firmware has reported fec_ability, don't need to convert by speed */
1088 if (mac->fec_ability)
1089 goto out;
1090
1091 switch (mac->speed) {
1092 case HCLGE_MAC_SPEED_10G:
1093 case HCLGE_MAC_SPEED_40G:
1094 mac->fec_ability = BIT(HNAE3_FEC_BASER) | BIT(HNAE3_FEC_AUTO) |
1095 BIT(HNAE3_FEC_NONE);
1096 break;
1097 case HCLGE_MAC_SPEED_25G:
1098 case HCLGE_MAC_SPEED_50G:
1099 mac->fec_ability = BIT(HNAE3_FEC_BASER) | BIT(HNAE3_FEC_RS) |
1100 BIT(HNAE3_FEC_AUTO) | BIT(HNAE3_FEC_NONE);
1101 break;
1102 case HCLGE_MAC_SPEED_100G:
1103 mac->fec_ability = BIT(HNAE3_FEC_RS) | BIT(HNAE3_FEC_AUTO) |
1104 BIT(HNAE3_FEC_NONE);
1105 break;
1106 case HCLGE_MAC_SPEED_200G:
1107 mac->fec_ability = BIT(HNAE3_FEC_RS) | BIT(HNAE3_FEC_AUTO) |
1108 BIT(HNAE3_FEC_LLRS);
1109 break;
1110 default:
1111 mac->fec_ability = 0;
1112 break;
1113 }
1114
1115 out:
1116 hclge_update_fec_support(mac);
1117 }
1118
1119 static void hclge_parse_fiber_link_mode(struct hclge_dev *hdev,
1120 u16 speed_ability)
1121 {
1122 struct hclge_mac *mac = &hdev->hw.mac;
1123
1124 if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1125 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1126 mac->supported);
1127
1128 hclge_convert_setting_sr(speed_ability, mac->supported);
1129 hclge_convert_setting_lr(speed_ability, mac->supported);
1130 hclge_convert_setting_cr(speed_ability, mac->supported);
1131 if (hnae3_dev_fec_supported(hdev))
1132 hclge_convert_setting_fec(mac);
1133
1134 if (hnae3_dev_pause_supported(hdev))
1135 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mac->supported);
1136
1137 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, mac->supported);
1138 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
1139 }
1140
1141 static void hclge_parse_backplane_link_mode(struct hclge_dev *hdev,
1142 u16 speed_ability)
1143 {
1144 struct hclge_mac *mac = &hdev->hw.mac;
1145
1146 hclge_convert_setting_kr(speed_ability, mac->supported);
1147 if (hnae3_dev_fec_supported(hdev))
1148 hclge_convert_setting_fec(mac);
1149
1150 if (hnae3_dev_pause_supported(hdev))
1151 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mac->supported);
1152
1153 linkmode_set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, mac->supported);
1154 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
1155 }
1156
1157 static void hclge_parse_copper_link_mode(struct hclge_dev *hdev,
1158 u16 speed_ability)
1159 {
1160 unsigned long *supported = hdev->hw.mac.supported;
1161
1162 /* default to support all speed for GE port */
1163 if (!speed_ability)
1164 speed_ability = HCLGE_SUPPORT_GE;
1165
1166 if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1167 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1168 supported);
1169
1170 if (speed_ability & HCLGE_SUPPORT_100M_BIT) {
1171 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1172 supported);
1173 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1174 supported);
1175 }
1176
1177 if (speed_ability & HCLGE_SUPPORT_10M_BIT) {
1178 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, supported);
1179 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, supported);
1180 }
1181
1182 if (hnae3_dev_pause_supported(hdev)) {
1183 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
1184 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
1185 }
1186
1187 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
1188 linkmode_set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
1189 }
1190
1191 static void hclge_parse_link_mode(struct hclge_dev *hdev, u16 speed_ability)
1192 {
1193 u8 media_type = hdev->hw.mac.media_type;
1194
1195 if (media_type == HNAE3_MEDIA_TYPE_FIBER)
1196 hclge_parse_fiber_link_mode(hdev, speed_ability);
1197 else if (media_type == HNAE3_MEDIA_TYPE_COPPER)
1198 hclge_parse_copper_link_mode(hdev, speed_ability);
1199 else if (media_type == HNAE3_MEDIA_TYPE_BACKPLANE)
1200 hclge_parse_backplane_link_mode(hdev, speed_ability);
1201 }
1202
1203 static u32 hclge_get_max_speed(u16 speed_ability)
1204 {
1205 if (speed_ability & HCLGE_SUPPORT_200G_BITS)
1206 return HCLGE_MAC_SPEED_200G;
1207
1208 if (speed_ability & HCLGE_SUPPORT_100G_BITS)
1209 return HCLGE_MAC_SPEED_100G;
1210
1211 if (speed_ability & HCLGE_SUPPORT_50G_BITS)
1212 return HCLGE_MAC_SPEED_50G;
1213
1214 if (speed_ability & HCLGE_SUPPORT_40G_BIT)
1215 return HCLGE_MAC_SPEED_40G;
1216
1217 if (speed_ability & HCLGE_SUPPORT_25G_BIT)
1218 return HCLGE_MAC_SPEED_25G;
1219
1220 if (speed_ability & HCLGE_SUPPORT_10G_BIT)
1221 return HCLGE_MAC_SPEED_10G;
1222
1223 if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1224 return HCLGE_MAC_SPEED_1G;
1225
1226 if (speed_ability & HCLGE_SUPPORT_100M_BIT)
1227 return HCLGE_MAC_SPEED_100M;
1228
1229 if (speed_ability & HCLGE_SUPPORT_10M_BIT)
1230 return HCLGE_MAC_SPEED_10M;
1231
1232 return HCLGE_MAC_SPEED_1G;
1233 }
1234
1235 static void hclge_parse_cfg(struct hclge_cfg *cfg, struct hclge_desc *desc)
1236 {
1237 #define HCLGE_TX_SPARE_SIZE_UNIT 4096
1238 #define SPEED_ABILITY_EXT_SHIFT 8
1239
1240 struct hclge_cfg_param_cmd *req;
1241 u64 mac_addr_tmp_high;
1242 u16 speed_ability_ext;
1243 u64 mac_addr_tmp;
1244 unsigned int i;
1245
1246 req = (struct hclge_cfg_param_cmd *)desc[0].data;
1247
1248 /* get the configuration */
1249 cfg->tc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
1250 HCLGE_CFG_TC_NUM_M, HCLGE_CFG_TC_NUM_S);
1251 cfg->tqp_desc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
1252 HCLGE_CFG_TQP_DESC_N_M,
1253 HCLGE_CFG_TQP_DESC_N_S);
1254
1255 cfg->phy_addr = hnae3_get_field(__le32_to_cpu(req->param[1]),
1256 HCLGE_CFG_PHY_ADDR_M,
1257 HCLGE_CFG_PHY_ADDR_S);
1258 cfg->media_type = hnae3_get_field(__le32_to_cpu(req->param[1]),
1259 HCLGE_CFG_MEDIA_TP_M,
1260 HCLGE_CFG_MEDIA_TP_S);
1261 cfg->rx_buf_len = hnae3_get_field(__le32_to_cpu(req->param[1]),
1262 HCLGE_CFG_RX_BUF_LEN_M,
1263 HCLGE_CFG_RX_BUF_LEN_S);
1264 /* get mac_address */
1265 mac_addr_tmp = __le32_to_cpu(req->param[2]);
1266 mac_addr_tmp_high = hnae3_get_field(__le32_to_cpu(req->param[3]),
1267 HCLGE_CFG_MAC_ADDR_H_M,
1268 HCLGE_CFG_MAC_ADDR_H_S);
1269
1270 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
1271
1272 cfg->default_speed = hnae3_get_field(__le32_to_cpu(req->param[3]),
1273 HCLGE_CFG_DEFAULT_SPEED_M,
1274 HCLGE_CFG_DEFAULT_SPEED_S);
1275 cfg->vf_rss_size_max = hnae3_get_field(__le32_to_cpu(req->param[3]),
1276 HCLGE_CFG_RSS_SIZE_M,
1277 HCLGE_CFG_RSS_SIZE_S);
1278
1279 for (i = 0; i < ETH_ALEN; i++)
1280 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
1281
1282 req = (struct hclge_cfg_param_cmd *)desc[1].data;
1283 cfg->numa_node_map = __le32_to_cpu(req->param[0]);
1284
1285 cfg->speed_ability = hnae3_get_field(__le32_to_cpu(req->param[1]),
1286 HCLGE_CFG_SPEED_ABILITY_M,
1287 HCLGE_CFG_SPEED_ABILITY_S);
1288 speed_ability_ext = hnae3_get_field(__le32_to_cpu(req->param[1]),
1289 HCLGE_CFG_SPEED_ABILITY_EXT_M,
1290 HCLGE_CFG_SPEED_ABILITY_EXT_S);
1291 cfg->speed_ability |= speed_ability_ext << SPEED_ABILITY_EXT_SHIFT;
1292
1293 cfg->vlan_fliter_cap = hnae3_get_field(__le32_to_cpu(req->param[1]),
1294 HCLGE_CFG_VLAN_FLTR_CAP_M,
1295 HCLGE_CFG_VLAN_FLTR_CAP_S);
1296
1297 cfg->umv_space = hnae3_get_field(__le32_to_cpu(req->param[1]),
1298 HCLGE_CFG_UMV_TBL_SPACE_M,
1299 HCLGE_CFG_UMV_TBL_SPACE_S);
1300
1301 cfg->pf_rss_size_max = hnae3_get_field(__le32_to_cpu(req->param[2]),
1302 HCLGE_CFG_PF_RSS_SIZE_M,
1303 HCLGE_CFG_PF_RSS_SIZE_S);
1304
1305 /* HCLGE_CFG_PF_RSS_SIZE_M is the PF max rss size, which is a
1306 * power of 2, instead of reading out directly. This would
1307 * be more flexible for future changes and expansions.
1308 * When VF max rss size field is HCLGE_CFG_RSS_SIZE_S,
1309 * it does not make sense if PF's field is 0. In this case, PF and VF
1310 * has the same max rss size filed: HCLGE_CFG_RSS_SIZE_S.
1311 */
1312 cfg->pf_rss_size_max = cfg->pf_rss_size_max ?
1313 1U << cfg->pf_rss_size_max :
1314 cfg->vf_rss_size_max;
1315
1316 /* The unit of the tx spare buffer size queried from configuration
1317 * file is HCLGE_TX_SPARE_SIZE_UNIT(4096) bytes, so a conversion is
1318 * needed here.
1319 */
1320 cfg->tx_spare_buf_size = hnae3_get_field(__le32_to_cpu(req->param[2]),
1321 HCLGE_CFG_TX_SPARE_BUF_SIZE_M,
1322 HCLGE_CFG_TX_SPARE_BUF_SIZE_S);
1323 cfg->tx_spare_buf_size *= HCLGE_TX_SPARE_SIZE_UNIT;
1324 }
1325
1326 /* hclge_get_cfg: query the static parameter from flash
1327 * @hdev: pointer to struct hclge_dev
1328 * @hcfg: the config structure to be getted
1329 */
1330 static int hclge_get_cfg(struct hclge_dev *hdev, struct hclge_cfg *hcfg)
1331 {
1332 struct hclge_desc desc[HCLGE_PF_CFG_DESC_NUM];
1333 struct hclge_cfg_param_cmd *req;
1334 unsigned int i;
1335 int ret;
1336
1337 for (i = 0; i < HCLGE_PF_CFG_DESC_NUM; i++) {
1338 u32 offset = 0;
1339
1340 req = (struct hclge_cfg_param_cmd *)desc[i].data;
1341 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_GET_CFG_PARAM,
1342 true);
1343 hnae3_set_field(offset, HCLGE_CFG_OFFSET_M,
1344 HCLGE_CFG_OFFSET_S, i * HCLGE_CFG_RD_LEN_BYTES);
1345 /* Len should be united by 4 bytes when send to hardware */
1346 hnae3_set_field(offset, HCLGE_CFG_RD_LEN_M, HCLGE_CFG_RD_LEN_S,
1347 HCLGE_CFG_RD_LEN_BYTES / HCLGE_CFG_RD_LEN_UNIT);
1348 req->offset = cpu_to_le32(offset);
1349 }
1350
1351 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PF_CFG_DESC_NUM);
1352 if (ret) {
1353 dev_err(&hdev->pdev->dev, "get config failed %d.\n", ret);
1354 return ret;
1355 }
1356
1357 hclge_parse_cfg(hcfg, desc);
1358
1359 return 0;
1360 }
1361
1362 static void hclge_set_default_dev_specs(struct hclge_dev *hdev)
1363 {
1364 #define HCLGE_MAX_NON_TSO_BD_NUM 8U
1365
1366 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1367
1368 ae_dev->dev_specs.max_non_tso_bd_num = HCLGE_MAX_NON_TSO_BD_NUM;
1369 ae_dev->dev_specs.rss_ind_tbl_size = HCLGE_RSS_IND_TBL_SIZE;
1370 ae_dev->dev_specs.rss_key_size = HCLGE_COMM_RSS_KEY_SIZE;
1371 ae_dev->dev_specs.max_tm_rate = HCLGE_ETHER_MAX_RATE;
1372 ae_dev->dev_specs.max_int_gl = HCLGE_DEF_MAX_INT_GL;
1373 ae_dev->dev_specs.max_frm_size = HCLGE_MAC_MAX_FRAME;
1374 ae_dev->dev_specs.max_qset_num = HCLGE_MAX_QSET_NUM;
1375 ae_dev->dev_specs.umv_size = HCLGE_DEFAULT_UMV_SPACE_PER_PF;
1376 ae_dev->dev_specs.tnl_num = 0;
1377 }
1378
1379 static void hclge_parse_dev_specs(struct hclge_dev *hdev,
1380 struct hclge_desc *desc)
1381 {
1382 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1383 struct hclge_dev_specs_0_cmd *req0;
1384 struct hclge_dev_specs_1_cmd *req1;
1385
1386 req0 = (struct hclge_dev_specs_0_cmd *)desc[0].data;
1387 req1 = (struct hclge_dev_specs_1_cmd *)desc[1].data;
1388
1389 ae_dev->dev_specs.max_non_tso_bd_num = req0->max_non_tso_bd_num;
1390 ae_dev->dev_specs.rss_ind_tbl_size =
1391 le16_to_cpu(req0->rss_ind_tbl_size);
1392 ae_dev->dev_specs.int_ql_max = le16_to_cpu(req0->int_ql_max);
1393 ae_dev->dev_specs.rss_key_size = le16_to_cpu(req0->rss_key_size);
1394 ae_dev->dev_specs.max_tm_rate = le32_to_cpu(req0->max_tm_rate);
1395 ae_dev->dev_specs.max_qset_num = le16_to_cpu(req1->max_qset_num);
1396 ae_dev->dev_specs.max_int_gl = le16_to_cpu(req1->max_int_gl);
1397 ae_dev->dev_specs.max_frm_size = le16_to_cpu(req1->max_frm_size);
1398 ae_dev->dev_specs.umv_size = le16_to_cpu(req1->umv_size);
1399 ae_dev->dev_specs.mc_mac_size = le16_to_cpu(req1->mc_mac_size);
1400 ae_dev->dev_specs.tnl_num = req1->tnl_num;
1401 ae_dev->dev_specs.hilink_version = req1->hilink_version;
1402 }
1403
1404 static void hclge_check_dev_specs(struct hclge_dev *hdev)
1405 {
1406 struct hnae3_dev_specs *dev_specs = &hdev->ae_dev->dev_specs;
1407
1408 if (!dev_specs->max_non_tso_bd_num)
1409 dev_specs->max_non_tso_bd_num = HCLGE_MAX_NON_TSO_BD_NUM;
1410 if (!dev_specs->rss_ind_tbl_size)
1411 dev_specs->rss_ind_tbl_size = HCLGE_RSS_IND_TBL_SIZE;
1412 if (!dev_specs->rss_key_size)
1413 dev_specs->rss_key_size = HCLGE_COMM_RSS_KEY_SIZE;
1414 if (!dev_specs->max_tm_rate)
1415 dev_specs->max_tm_rate = HCLGE_ETHER_MAX_RATE;
1416 if (!dev_specs->max_qset_num)
1417 dev_specs->max_qset_num = HCLGE_MAX_QSET_NUM;
1418 if (!dev_specs->max_int_gl)
1419 dev_specs->max_int_gl = HCLGE_DEF_MAX_INT_GL;
1420 if (!dev_specs->max_frm_size)
1421 dev_specs->max_frm_size = HCLGE_MAC_MAX_FRAME;
1422 if (!dev_specs->umv_size)
1423 dev_specs->umv_size = HCLGE_DEFAULT_UMV_SPACE_PER_PF;
1424 }
1425
1426 static int hclge_query_mac_stats_num(struct hclge_dev *hdev)
1427 {
1428 u32 reg_num = 0;
1429 int ret;
1430
1431 ret = hclge_mac_query_reg_num(hdev, &reg_num);
1432 if (ret && ret != -EOPNOTSUPP)
1433 return ret;
1434
1435 hdev->ae_dev->dev_specs.mac_stats_num = reg_num;
1436 return 0;
1437 }
1438
1439 static int hclge_query_dev_specs(struct hclge_dev *hdev)
1440 {
1441 struct hclge_desc desc[HCLGE_QUERY_DEV_SPECS_BD_NUM];
1442 int ret;
1443 int i;
1444
1445 ret = hclge_query_mac_stats_num(hdev);
1446 if (ret)
1447 return ret;
1448
1449 /* set default specifications as devices lower than version V3 do not
1450 * support querying specifications from firmware.
1451 */
1452 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
1453 hclge_set_default_dev_specs(hdev);
1454 return 0;
1455 }
1456
1457 for (i = 0; i < HCLGE_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
1458 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_DEV_SPECS,
1459 true);
1460 desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
1461 }
1462 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_DEV_SPECS, true);
1463
1464 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_QUERY_DEV_SPECS_BD_NUM);
1465 if (ret)
1466 return ret;
1467
1468 hclge_parse_dev_specs(hdev, desc);
1469 hclge_check_dev_specs(hdev);
1470
1471 return 0;
1472 }
1473
1474 static int hclge_get_cap(struct hclge_dev *hdev)
1475 {
1476 int ret;
1477
1478 ret = hclge_query_function_status(hdev);
1479 if (ret) {
1480 dev_err(&hdev->pdev->dev,
1481 "query function status error %d.\n", ret);
1482 return ret;
1483 }
1484
1485 /* get pf resource */
1486 return hclge_query_pf_resource(hdev);
1487 }
1488
1489 static void hclge_init_kdump_kernel_config(struct hclge_dev *hdev)
1490 {
1491 #define HCLGE_MIN_TX_DESC 64
1492 #define HCLGE_MIN_RX_DESC 64
1493
1494 if (!is_kdump_kernel())
1495 return;
1496
1497 dev_info(&hdev->pdev->dev,
1498 "Running kdump kernel. Using minimal resources\n");
1499
1500 /* minimal queue pairs equals to the number of vports */
1501 hdev->num_tqps = hdev->num_req_vfs + 1;
1502 hdev->num_tx_desc = HCLGE_MIN_TX_DESC;
1503 hdev->num_rx_desc = HCLGE_MIN_RX_DESC;
1504 }
1505
1506 static void hclge_init_tc_config(struct hclge_dev *hdev)
1507 {
1508 unsigned int i;
1509
1510 if (hdev->tc_max > HNAE3_MAX_TC ||
1511 hdev->tc_max < 1) {
1512 dev_warn(&hdev->pdev->dev, "TC num = %u.\n",
1513 hdev->tc_max);
1514 hdev->tc_max = 1;
1515 }
1516
1517 /* Dev does not support DCB */
1518 if (!hnae3_dev_dcb_supported(hdev)) {
1519 hdev->tc_max = 1;
1520 hdev->pfc_max = 0;
1521 } else {
1522 hdev->pfc_max = hdev->tc_max;
1523 }
1524
1525 hdev->tm_info.num_tc = 1;
1526
1527 /* Currently not support uncontiuous tc */
1528 for (i = 0; i < hdev->tm_info.num_tc; i++)
1529 hnae3_set_bit(hdev->hw_tc_map, i, 1);
1530
1531 hdev->tx_sch_mode = HCLGE_FLAG_TC_BASE_SCH_MODE;
1532 }
1533
1534 static int hclge_configure(struct hclge_dev *hdev)
1535 {
1536 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1537 struct hclge_cfg cfg;
1538 int ret;
1539
1540 ret = hclge_get_cfg(hdev, &cfg);
1541 if (ret)
1542 return ret;
1543
1544 hdev->base_tqp_pid = 0;
1545 hdev->vf_rss_size_max = cfg.vf_rss_size_max;
1546 hdev->pf_rss_size_max = cfg.pf_rss_size_max;
1547 hdev->rx_buf_len = cfg.rx_buf_len;
1548 ether_addr_copy(hdev->hw.mac.mac_addr, cfg.mac_addr);
1549 hdev->hw.mac.media_type = cfg.media_type;
1550 hdev->hw.mac.phy_addr = cfg.phy_addr;
1551 hdev->num_tx_desc = cfg.tqp_desc_num;
1552 hdev->num_rx_desc = cfg.tqp_desc_num;
1553 hdev->tm_info.num_pg = 1;
1554 hdev->tc_max = cfg.tc_num;
1555 hdev->tm_info.hw_pfc_map = 0;
1556 if (cfg.umv_space)
1557 hdev->wanted_umv_size = cfg.umv_space;
1558 else
1559 hdev->wanted_umv_size = hdev->ae_dev->dev_specs.umv_size;
1560 hdev->tx_spare_buf_size = cfg.tx_spare_buf_size;
1561 hdev->gro_en = true;
1562 if (cfg.vlan_fliter_cap == HCLGE_VLAN_FLTR_CAN_MDF)
1563 set_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps);
1564
1565 if (hnae3_ae_dev_fd_supported(hdev->ae_dev)) {
1566 hdev->fd_en = true;
1567 hdev->fd_active_type = HCLGE_FD_RULE_NONE;
1568 }
1569
1570 ret = hclge_parse_speed(cfg.default_speed, &hdev->hw.mac.speed);
1571 if (ret) {
1572 dev_err(&hdev->pdev->dev, "failed to parse speed %u, ret = %d\n",
1573 cfg.default_speed, ret);
1574 return ret;
1575 }
1576 hdev->hw.mac.req_speed = hdev->hw.mac.speed;
1577 hdev->hw.mac.req_autoneg = AUTONEG_ENABLE;
1578 hdev->hw.mac.req_duplex = DUPLEX_FULL;
1579
1580 /* When lane_num is 0, the firmware will automatically
1581 * select the appropriate lane_num based on the speed.
1582 */
1583 hdev->hw.mac.req_lane_num = 0;
1584
1585 hclge_parse_link_mode(hdev, cfg.speed_ability);
1586
1587 hdev->hw.mac.max_speed = hclge_get_max_speed(cfg.speed_ability);
1588
1589 hclge_init_tc_config(hdev);
1590 hclge_init_kdump_kernel_config(hdev);
1591
1592 return ret;
1593 }
1594
1595 static int hclge_config_tso(struct hclge_dev *hdev, u16 tso_mss_min,
1596 u16 tso_mss_max)
1597 {
1598 struct hclge_cfg_tso_status_cmd *req;
1599 struct hclge_desc desc;
1600
1601 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TSO_GENERIC_CONFIG, false);
1602
1603 req = (struct hclge_cfg_tso_status_cmd *)desc.data;
1604 req->tso_mss_min = cpu_to_le16(tso_mss_min);
1605 req->tso_mss_max = cpu_to_le16(tso_mss_max);
1606
1607 return hclge_cmd_send(&hdev->hw, &desc, 1);
1608 }
1609
1610 static int hclge_config_gro(struct hclge_dev *hdev)
1611 {
1612 struct hclge_cfg_gro_status_cmd *req;
1613 struct hclge_desc desc;
1614 int ret;
1615
1616 if (!hnae3_ae_dev_gro_supported(hdev->ae_dev))
1617 return 0;
1618
1619 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GRO_GENERIC_CONFIG, false);
1620 req = (struct hclge_cfg_gro_status_cmd *)desc.data;
1621
1622 req->gro_en = hdev->gro_en ? 1 : 0;
1623
1624 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1625 if (ret)
1626 dev_err(&hdev->pdev->dev,
1627 "GRO hardware config cmd failed, ret = %d\n", ret);
1628
1629 return ret;
1630 }
1631
1632 static int hclge_alloc_tqps(struct hclge_dev *hdev)
1633 {
1634 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1635 struct hclge_comm_tqp *tqp;
1636 int i;
1637
1638 hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
1639 sizeof(struct hclge_comm_tqp), GFP_KERNEL);
1640 if (!hdev->htqp)
1641 return -ENOMEM;
1642
1643 tqp = hdev->htqp;
1644
1645 for (i = 0; i < hdev->num_tqps; i++) {
1646 tqp->dev = &hdev->pdev->dev;
1647 tqp->index = i;
1648
1649 tqp->q.ae_algo = &ae_algo;
1650 tqp->q.buf_size = hdev->rx_buf_len;
1651 tqp->q.tx_desc_num = hdev->num_tx_desc;
1652 tqp->q.rx_desc_num = hdev->num_rx_desc;
1653
1654 /* need an extended offset to configure queues >=
1655 * HCLGE_TQP_MAX_SIZE_DEV_V2
1656 */
1657 if (i < HCLGE_TQP_MAX_SIZE_DEV_V2)
1658 tqp->q.io_base = hdev->hw.hw.io_base +
1659 HCLGE_TQP_REG_OFFSET +
1660 i * HCLGE_TQP_REG_SIZE;
1661 else
1662 tqp->q.io_base = hdev->hw.hw.io_base +
1663 HCLGE_TQP_REG_OFFSET +
1664 HCLGE_TQP_EXT_REG_OFFSET +
1665 (i - HCLGE_TQP_MAX_SIZE_DEV_V2) *
1666 HCLGE_TQP_REG_SIZE;
1667
1668 /* when device supports tx push and has device memory,
1669 * the queue can execute push mode or doorbell mode on
1670 * device memory.
1671 */
1672 if (test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps))
1673 tqp->q.mem_base = hdev->hw.hw.mem_base +
1674 HCLGE_TQP_MEM_OFFSET(hdev, i);
1675
1676 tqp++;
1677 }
1678
1679 return 0;
1680 }
1681
1682 static int hclge_map_tqps_to_func(struct hclge_dev *hdev, u16 func_id,
1683 u16 tqp_pid, u16 tqp_vid, bool is_pf)
1684 {
1685 struct hclge_tqp_map_cmd *req;
1686 struct hclge_desc desc;
1687 int ret;
1688
1689 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_SET_TQP_MAP, false);
1690
1691 req = (struct hclge_tqp_map_cmd *)desc.data;
1692 req->tqp_id = cpu_to_le16(tqp_pid);
1693 req->tqp_vf = func_id;
1694 req->tqp_flag = 1U << HCLGE_TQP_MAP_EN_B;
1695 if (!is_pf)
1696 req->tqp_flag |= 1U << HCLGE_TQP_MAP_TYPE_B;
1697 req->tqp_vid = cpu_to_le16(tqp_vid);
1698
1699 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1700 if (ret)
1701 dev_err(&hdev->pdev->dev, "TQP map failed %d.\n", ret);
1702
1703 return ret;
1704 }
1705
1706 static int hclge_assign_tqp(struct hclge_vport *vport, u16 num_tqps)
1707 {
1708 struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
1709 struct hclge_dev *hdev = vport->back;
1710 int i, alloced;
1711
1712 for (i = 0, alloced = 0; i < hdev->num_tqps &&
1713 alloced < num_tqps; i++) {
1714 if (!hdev->htqp[i].alloced) {
1715 hdev->htqp[i].q.handle = &vport->nic;
1716 hdev->htqp[i].q.tqp_index = alloced;
1717 hdev->htqp[i].q.tx_desc_num = kinfo->num_tx_desc;
1718 hdev->htqp[i].q.rx_desc_num = kinfo->num_rx_desc;
1719 kinfo->tqp[alloced] = &hdev->htqp[i].q;
1720 hdev->htqp[i].alloced = true;
1721 alloced++;
1722 }
1723 }
1724 vport->alloc_tqps = alloced;
1725 kinfo->rss_size = min_t(u16, hdev->pf_rss_size_max,
1726 vport->alloc_tqps / hdev->tm_info.num_tc);
1727
1728 /* ensure one to one mapping between irq and queue at default */
1729 kinfo->rss_size = min_t(u16, kinfo->rss_size,
1730 (hdev->num_nic_msi - 1) / hdev->tm_info.num_tc);
1731
1732 return 0;
1733 }
1734
1735 static int hclge_knic_setup(struct hclge_vport *vport, u16 num_tqps,
1736 u16 num_tx_desc, u16 num_rx_desc)
1737
1738 {
1739 struct hnae3_handle *nic = &vport->nic;
1740 struct hnae3_knic_private_info *kinfo = &nic->kinfo;
1741 struct hclge_dev *hdev = vport->back;
1742 int ret;
1743
1744 kinfo->num_tx_desc = num_tx_desc;
1745 kinfo->num_rx_desc = num_rx_desc;
1746
1747 kinfo->rx_buf_len = hdev->rx_buf_len;
1748 kinfo->tx_spare_buf_size = hdev->tx_spare_buf_size;
1749
1750 kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, num_tqps,
1751 sizeof(struct hnae3_queue *), GFP_KERNEL);
1752 if (!kinfo->tqp)
1753 return -ENOMEM;
1754
1755 ret = hclge_assign_tqp(vport, num_tqps);
1756 if (ret)
1757 dev_err(&hdev->pdev->dev, "fail to assign TQPs %d.\n", ret);
1758
1759 return ret;
1760 }
1761
1762 static int hclge_map_tqp_to_vport(struct hclge_dev *hdev,
1763 struct hclge_vport *vport)
1764 {
1765 struct hnae3_handle *nic = &vport->nic;
1766 struct hnae3_knic_private_info *kinfo;
1767 u16 i;
1768
1769 kinfo = &nic->kinfo;
1770 for (i = 0; i < vport->alloc_tqps; i++) {
1771 struct hclge_comm_tqp *q =
1772 container_of(kinfo->tqp[i], struct hclge_comm_tqp, q);
1773 bool is_pf;
1774 int ret;
1775
1776 is_pf = !(vport->vport_id);
1777 ret = hclge_map_tqps_to_func(hdev, vport->vport_id, q->index,
1778 i, is_pf);
1779 if (ret)
1780 return ret;
1781 }
1782
1783 return 0;
1784 }
1785
1786 static int hclge_map_tqp(struct hclge_dev *hdev)
1787 {
1788 struct hclge_vport *vport = hdev->vport;
1789 u16 i, num_vport;
1790
1791 num_vport = hdev->num_req_vfs + 1;
1792 for (i = 0; i < num_vport; i++) {
1793 int ret;
1794
1795 ret = hclge_map_tqp_to_vport(hdev, vport);
1796 if (ret)
1797 return ret;
1798
1799 vport++;
1800 }
1801
1802 return 0;
1803 }
1804
1805 static int hclge_vport_setup(struct hclge_vport *vport, u16 num_tqps)
1806 {
1807 struct hnae3_handle *nic = &vport->nic;
1808 struct hclge_dev *hdev = vport->back;
1809 int ret;
1810
1811 nic->pdev = hdev->pdev;
1812 nic->ae_algo = &ae_algo;
1813 bitmap_copy(nic->numa_node_mask.bits, hdev->numa_node_mask.bits,
1814 MAX_NUMNODES);
1815 nic->kinfo.io_base = hdev->hw.hw.io_base;
1816
1817 ret = hclge_knic_setup(vport, num_tqps,
1818 hdev->num_tx_desc, hdev->num_rx_desc);
1819 if (ret)
1820 dev_err(&hdev->pdev->dev, "knic setup failed %d\n", ret);
1821
1822 return ret;
1823 }
1824
1825 static int hclge_alloc_vport(struct hclge_dev *hdev)
1826 {
1827 struct pci_dev *pdev = hdev->pdev;
1828 struct hclge_vport *vport;
1829 u32 tqp_main_vport;
1830 u32 tqp_per_vport;
1831 int num_vport, i;
1832 int ret;
1833
1834 /* We need to alloc a vport for main NIC of PF */
1835 num_vport = hdev->num_req_vfs + 1;
1836
1837 if (hdev->num_tqps < num_vport) {
1838 dev_err(&hdev->pdev->dev, "tqps(%u) is less than vports(%d)",
1839 hdev->num_tqps, num_vport);
1840 return -EINVAL;
1841 }
1842
1843 /* Alloc the same number of TQPs for every vport */
1844 tqp_per_vport = hdev->num_tqps / num_vport;
1845 tqp_main_vport = tqp_per_vport + hdev->num_tqps % num_vport;
1846
1847 vport = devm_kcalloc(&pdev->dev, num_vport, sizeof(struct hclge_vport),
1848 GFP_KERNEL);
1849 if (!vport)
1850 return -ENOMEM;
1851
1852 hdev->vport = vport;
1853 hdev->num_alloc_vport = num_vport;
1854
1855 if (IS_ENABLED(CONFIG_PCI_IOV))
1856 hdev->num_alloc_vfs = hdev->num_req_vfs;
1857
1858 for (i = 0; i < num_vport; i++) {
1859 vport->back = hdev;
1860 vport->vport_id = i;
1861 vport->vf_info.link_state = IFLA_VF_LINK_STATE_AUTO;
1862 vport->mps = HCLGE_MAC_DEFAULT_FRAME;
1863 vport->port_base_vlan_cfg.state = HNAE3_PORT_BASE_VLAN_DISABLE;
1864 vport->port_base_vlan_cfg.tbl_sta = true;
1865 vport->rxvlan_cfg.rx_vlan_offload_en = true;
1866 vport->req_vlan_fltr_en = true;
1867 INIT_LIST_HEAD(&vport->vlan_list);
1868 INIT_LIST_HEAD(&vport->uc_mac_list);
1869 INIT_LIST_HEAD(&vport->mc_mac_list);
1870 spin_lock_init(&vport->mac_list_lock);
1871
1872 if (i == 0)
1873 ret = hclge_vport_setup(vport, tqp_main_vport);
1874 else
1875 ret = hclge_vport_setup(vport, tqp_per_vport);
1876 if (ret) {
1877 dev_err(&pdev->dev,
1878 "vport setup failed for vport %d, %d\n",
1879 i, ret);
1880 return ret;
1881 }
1882
1883 vport++;
1884 }
1885
1886 return 0;
1887 }
1888
1889 static int hclge_cmd_alloc_tx_buff(struct hclge_dev *hdev,
1890 struct hclge_pkt_buf_alloc *buf_alloc)
1891 {
1892 /* TX buffer size is unit by 128 byte */
1893 #define HCLGE_BUF_SIZE_UNIT_SHIFT 7
1894 #define HCLGE_BUF_SIZE_UPDATE_EN_MSK BIT(15)
1895 struct hclge_tx_buff_alloc_cmd *req;
1896 struct hclge_desc desc;
1897 int ret;
1898 u8 i;
1899
1900 req = (struct hclge_tx_buff_alloc_cmd *)desc.data;
1901
1902 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TX_BUFF_ALLOC, 0);
1903 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1904 u32 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
1905
1906 req->tx_pkt_buff[i] =
1907 cpu_to_le16((buf_size >> HCLGE_BUF_SIZE_UNIT_SHIFT) |
1908 HCLGE_BUF_SIZE_UPDATE_EN_MSK);
1909 }
1910
1911 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1912 if (ret)
1913 dev_err(&hdev->pdev->dev, "tx buffer alloc cmd failed %d.\n",
1914 ret);
1915
1916 return ret;
1917 }
1918
1919 static int hclge_tx_buffer_alloc(struct hclge_dev *hdev,
1920 struct hclge_pkt_buf_alloc *buf_alloc)
1921 {
1922 int ret = hclge_cmd_alloc_tx_buff(hdev, buf_alloc);
1923
1924 if (ret)
1925 dev_err(&hdev->pdev->dev, "tx buffer alloc failed %d\n", ret);
1926
1927 return ret;
1928 }
1929
1930 static u32 hclge_get_tc_num(struct hclge_dev *hdev)
1931 {
1932 unsigned int i;
1933 u32 cnt = 0;
1934
1935 for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1936 if (hdev->hw_tc_map & BIT(i))
1937 cnt++;
1938 return cnt;
1939 }
1940
1941 /* Get the number of pfc enabled TCs, which have private buffer */
1942 static int hclge_get_pfc_priv_num(struct hclge_dev *hdev,
1943 struct hclge_pkt_buf_alloc *buf_alloc)
1944 {
1945 struct hclge_priv_buf *priv;
1946 unsigned int i;
1947 int cnt = 0;
1948
1949 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1950 priv = &buf_alloc->priv_buf[i];
1951 if ((hdev->tm_info.hw_pfc_map & BIT(i)) &&
1952 priv->enable)
1953 cnt++;
1954 }
1955
1956 return cnt;
1957 }
1958
1959 /* Get the number of pfc disabled TCs, which have private buffer */
1960 static int hclge_get_no_pfc_priv_num(struct hclge_dev *hdev,
1961 struct hclge_pkt_buf_alloc *buf_alloc)
1962 {
1963 struct hclge_priv_buf *priv;
1964 unsigned int i;
1965 int cnt = 0;
1966
1967 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1968 priv = &buf_alloc->priv_buf[i];
1969 if (hdev->hw_tc_map & BIT(i) &&
1970 !(hdev->tm_info.hw_pfc_map & BIT(i)) &&
1971 priv->enable)
1972 cnt++;
1973 }
1974
1975 return cnt;
1976 }
1977
1978 static u32 hclge_get_rx_priv_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1979 {
1980 struct hclge_priv_buf *priv;
1981 u32 rx_priv = 0;
1982 int i;
1983
1984 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1985 priv = &buf_alloc->priv_buf[i];
1986 if (priv->enable)
1987 rx_priv += priv->buf_size;
1988 }
1989 return rx_priv;
1990 }
1991
1992 static u32 hclge_get_tx_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1993 {
1994 u32 i, total_tx_size = 0;
1995
1996 for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1997 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
1998
1999 return total_tx_size;
2000 }
2001
2002 static bool hclge_is_rx_buf_ok(struct hclge_dev *hdev,
2003 struct hclge_pkt_buf_alloc *buf_alloc,
2004 u32 rx_all)
2005 {
2006 u32 shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
2007 u32 tc_num = hclge_get_tc_num(hdev);
2008 u32 shared_buf, aligned_mps;
2009 u32 rx_priv;
2010 int i;
2011
2012 aligned_mps = roundup(hdev->mps, HCLGE_BUF_SIZE_UNIT);
2013
2014 if (hnae3_dev_dcb_supported(hdev))
2015 shared_buf_min = HCLGE_BUF_MUL_BY * aligned_mps +
2016 hdev->dv_buf_size;
2017 else
2018 shared_buf_min = aligned_mps + HCLGE_NON_DCB_ADDITIONAL_BUF
2019 + hdev->dv_buf_size;
2020
2021 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
2022 shared_std = roundup(max_t(u32, shared_buf_min, shared_buf_tc),
2023 HCLGE_BUF_SIZE_UNIT);
2024
2025 rx_priv = hclge_get_rx_priv_buff_alloced(buf_alloc);
2026 if (rx_all < rx_priv + shared_std)
2027 return false;
2028
2029 shared_buf = rounddown(rx_all - rx_priv, HCLGE_BUF_SIZE_UNIT);
2030 buf_alloc->s_buf.buf_size = shared_buf;
2031 if (hnae3_dev_dcb_supported(hdev)) {
2032 buf_alloc->s_buf.self.high = shared_buf - hdev->dv_buf_size;
2033 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
2034 - roundup(aligned_mps / HCLGE_BUF_DIV_BY,
2035 HCLGE_BUF_SIZE_UNIT);
2036 } else {
2037 buf_alloc->s_buf.self.high = aligned_mps +
2038 HCLGE_NON_DCB_ADDITIONAL_BUF;
2039 buf_alloc->s_buf.self.low = aligned_mps;
2040 }
2041
2042 if (hnae3_dev_dcb_supported(hdev)) {
2043 hi_thrd = shared_buf - hdev->dv_buf_size;
2044
2045 if (tc_num <= NEED_RESERVE_TC_NUM)
2046 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
2047 / BUF_MAX_PERCENT;
2048
2049 if (tc_num)
2050 hi_thrd = hi_thrd / tc_num;
2051
2052 hi_thrd = max_t(u32, hi_thrd, HCLGE_BUF_MUL_BY * aligned_mps);
2053 hi_thrd = rounddown(hi_thrd, HCLGE_BUF_SIZE_UNIT);
2054 lo_thrd = hi_thrd - aligned_mps / HCLGE_BUF_DIV_BY;
2055 } else {
2056 hi_thrd = aligned_mps + HCLGE_NON_DCB_ADDITIONAL_BUF;
2057 lo_thrd = aligned_mps;
2058 }
2059
2060 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2061 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
2062 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
2063 }
2064
2065 return true;
2066 }
2067
2068 static int hclge_tx_buffer_calc(struct hclge_dev *hdev,
2069 struct hclge_pkt_buf_alloc *buf_alloc)
2070 {
2071 u32 i, total_size;
2072
2073 total_size = hdev->pkt_buf_size;
2074
2075 /* alloc tx buffer for all enabled tc */
2076 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2077 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2078
2079 if (hdev->hw_tc_map & BIT(i)) {
2080 if (total_size < hdev->tx_buf_size)
2081 return -ENOMEM;
2082
2083 priv->tx_buf_size = hdev->tx_buf_size;
2084 } else {
2085 priv->tx_buf_size = 0;
2086 }
2087
2088 total_size -= priv->tx_buf_size;
2089 }
2090
2091 return 0;
2092 }
2093
2094 static bool hclge_rx_buf_calc_all(struct hclge_dev *hdev, bool max,
2095 struct hclge_pkt_buf_alloc *buf_alloc)
2096 {
2097 u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2098 u32 aligned_mps = round_up(hdev->mps, HCLGE_BUF_SIZE_UNIT);
2099 unsigned int i;
2100
2101 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2102 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2103
2104 priv->enable = 0;
2105 priv->wl.low = 0;
2106 priv->wl.high = 0;
2107 priv->buf_size = 0;
2108
2109 if (!(hdev->hw_tc_map & BIT(i)))
2110 continue;
2111
2112 priv->enable = 1;
2113
2114 if (hdev->tm_info.hw_pfc_map & BIT(i)) {
2115 priv->wl.low = max ? aligned_mps : HCLGE_BUF_SIZE_UNIT;
2116 priv->wl.high = roundup(priv->wl.low + aligned_mps,
2117 HCLGE_BUF_SIZE_UNIT);
2118 } else {
2119 priv->wl.low = 0;
2120 priv->wl.high = max ? (aligned_mps * HCLGE_BUF_MUL_BY) :
2121 aligned_mps;
2122 }
2123
2124 priv->buf_size = priv->wl.high + hdev->dv_buf_size;
2125 }
2126
2127 return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2128 }
2129
2130 static bool hclge_drop_nopfc_buf_till_fit(struct hclge_dev *hdev,
2131 struct hclge_pkt_buf_alloc *buf_alloc)
2132 {
2133 u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2134 int no_pfc_priv_num = hclge_get_no_pfc_priv_num(hdev, buf_alloc);
2135 int i;
2136
2137 /* let the last to be cleared first */
2138 for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
2139 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2140 unsigned int mask = BIT((unsigned int)i);
2141
2142 if (hdev->hw_tc_map & mask &&
2143 !(hdev->tm_info.hw_pfc_map & mask)) {
2144 /* Clear the no pfc TC private buffer */
2145 priv->wl.low = 0;
2146 priv->wl.high = 0;
2147 priv->buf_size = 0;
2148 priv->enable = 0;
2149 no_pfc_priv_num--;
2150 }
2151
2152 if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
2153 no_pfc_priv_num == 0)
2154 break;
2155 }
2156
2157 return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2158 }
2159
2160 static bool hclge_drop_pfc_buf_till_fit(struct hclge_dev *hdev,
2161 struct hclge_pkt_buf_alloc *buf_alloc)
2162 {
2163 u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2164 int pfc_priv_num = hclge_get_pfc_priv_num(hdev, buf_alloc);
2165 int i;
2166
2167 /* let the last to be cleared first */
2168 for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
2169 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2170 unsigned int mask = BIT((unsigned int)i);
2171
2172 if (hdev->hw_tc_map & mask &&
2173 hdev->tm_info.hw_pfc_map & mask) {
2174 /* Reduce the number of pfc TC with private buffer */
2175 priv->wl.low = 0;
2176 priv->enable = 0;
2177 priv->wl.high = 0;
2178 priv->buf_size = 0;
2179 pfc_priv_num--;
2180 }
2181
2182 if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
2183 pfc_priv_num == 0)
2184 break;
2185 }
2186
2187 return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2188 }
2189
2190 static bool hclge_only_alloc_priv_buff(struct hclge_dev *hdev,
2191 struct hclge_pkt_buf_alloc *buf_alloc)
2192 {
2193 #define COMPENSATE_BUFFER 0x3C00
2194 #define COMPENSATE_HALF_MPS_NUM 5
2195 #define PRIV_WL_GAP 0x1800
2196
2197 u32 rx_priv = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2198 u32 tc_num = hclge_get_tc_num(hdev);
2199 u32 half_mps = hdev->mps >> 1;
2200 u32 min_rx_priv;
2201 unsigned int i;
2202
2203 if (tc_num)
2204 rx_priv = rx_priv / tc_num;
2205
2206 if (tc_num <= NEED_RESERVE_TC_NUM)
2207 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
2208
2209 min_rx_priv = hdev->dv_buf_size + COMPENSATE_BUFFER +
2210 COMPENSATE_HALF_MPS_NUM * half_mps;
2211 min_rx_priv = round_up(min_rx_priv, HCLGE_BUF_SIZE_UNIT);
2212 rx_priv = round_down(rx_priv, HCLGE_BUF_SIZE_UNIT);
2213 if (rx_priv < min_rx_priv)
2214 return false;
2215
2216 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2217 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2218
2219 priv->enable = 0;
2220 priv->wl.low = 0;
2221 priv->wl.high = 0;
2222 priv->buf_size = 0;
2223
2224 if (!(hdev->hw_tc_map & BIT(i)))
2225 continue;
2226
2227 priv->enable = 1;
2228 priv->buf_size = rx_priv;
2229 priv->wl.high = rx_priv - hdev->dv_buf_size;
2230 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
2231 }
2232
2233 buf_alloc->s_buf.buf_size = 0;
2234
2235 return true;
2236 }
2237
2238 /* hclge_rx_buffer_calc: calculate the rx private buffer size for all TCs
2239 * @hdev: pointer to struct hclge_dev
2240 * @buf_alloc: pointer to buffer calculation data
2241 * @return: 0: calculate successful, negative: fail
2242 */
2243 static int hclge_rx_buffer_calc(struct hclge_dev *hdev,
2244 struct hclge_pkt_buf_alloc *buf_alloc)
2245 {
2246 /* When DCB is not supported, rx private buffer is not allocated. */
2247 if (!hnae3_dev_dcb_supported(hdev)) {
2248 u32 rx_all = hdev->pkt_buf_size;
2249
2250 rx_all -= hclge_get_tx_buff_alloced(buf_alloc);
2251 if (!hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
2252 return -ENOMEM;
2253
2254 return 0;
2255 }
2256
2257 if (hclge_only_alloc_priv_buff(hdev, buf_alloc))
2258 return 0;
2259
2260 if (hclge_rx_buf_calc_all(hdev, true, buf_alloc))
2261 return 0;
2262
2263 /* try to decrease the buffer size */
2264 if (hclge_rx_buf_calc_all(hdev, false, buf_alloc))
2265 return 0;
2266
2267 if (hclge_drop_nopfc_buf_till_fit(hdev, buf_alloc))
2268 return 0;
2269
2270 if (hclge_drop_pfc_buf_till_fit(hdev, buf_alloc))
2271 return 0;
2272
2273 return -ENOMEM;
2274 }
2275
2276 static int hclge_rx_priv_buf_alloc(struct hclge_dev *hdev,
2277 struct hclge_pkt_buf_alloc *buf_alloc)
2278 {
2279 struct hclge_rx_priv_buff_cmd *req;
2280 struct hclge_desc desc;
2281 int ret;
2282 int i;
2283
2284 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_PRIV_BUFF_ALLOC, false);
2285 req = (struct hclge_rx_priv_buff_cmd *)desc.data;
2286
2287 /* Alloc private buffer TCs */
2288 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2289 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2290
2291 req->buf_num[i] =
2292 cpu_to_le16(priv->buf_size >> HCLGE_BUF_UNIT_S);
2293 req->buf_num[i] |=
2294 cpu_to_le16(1 << HCLGE_TC0_PRI_BUF_EN_B);
2295 }
2296
2297 req->shared_buf =
2298 cpu_to_le16((buf_alloc->s_buf.buf_size >> HCLGE_BUF_UNIT_S) |
2299 (1 << HCLGE_TC0_PRI_BUF_EN_B));
2300
2301 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2302 if (ret)
2303 dev_err(&hdev->pdev->dev,
2304 "rx private buffer alloc cmd failed %d\n", ret);
2305
2306 return ret;
2307 }
2308
2309 static int hclge_rx_priv_wl_config(struct hclge_dev *hdev,
2310 struct hclge_pkt_buf_alloc *buf_alloc)
2311 {
2312 struct hclge_rx_priv_wl_buf *req;
2313 struct hclge_priv_buf *priv;
2314 struct hclge_desc desc[2];
2315 int i, j;
2316 int ret;
2317
2318 for (i = 0; i < 2; i++) {
2319 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_RX_PRIV_WL_ALLOC,
2320 false);
2321 req = (struct hclge_rx_priv_wl_buf *)desc[i].data;
2322
2323 /* The first descriptor set the NEXT bit to 1 */
2324 if (i == 0)
2325 desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2326 else
2327 desc[i].flag &= ~cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2328
2329 for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
2330 u32 idx = i * HCLGE_TC_NUM_ONE_DESC + j;
2331
2332 priv = &buf_alloc->priv_buf[idx];
2333 req->tc_wl[j].high =
2334 cpu_to_le16(priv->wl.high >> HCLGE_BUF_UNIT_S);
2335 req->tc_wl[j].high |=
2336 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2337 req->tc_wl[j].low =
2338 cpu_to_le16(priv->wl.low >> HCLGE_BUF_UNIT_S);
2339 req->tc_wl[j].low |=
2340 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2341 }
2342 }
2343
2344 /* Send 2 descriptor at one time */
2345 ret = hclge_cmd_send(&hdev->hw, desc, 2);
2346 if (ret)
2347 dev_err(&hdev->pdev->dev,
2348 "rx private waterline config cmd failed %d\n",
2349 ret);
2350 return ret;
2351 }
2352
2353 static int hclge_common_thrd_config(struct hclge_dev *hdev,
2354 struct hclge_pkt_buf_alloc *buf_alloc)
2355 {
2356 struct hclge_shared_buf *s_buf = &buf_alloc->s_buf;
2357 struct hclge_rx_com_thrd *req;
2358 struct hclge_desc desc[2];
2359 struct hclge_tc_thrd *tc;
2360 int i, j;
2361 int ret;
2362
2363 for (i = 0; i < 2; i++) {
2364 hclge_cmd_setup_basic_desc(&desc[i],
2365 HCLGE_OPC_RX_COM_THRD_ALLOC, false);
2366 req = (struct hclge_rx_com_thrd *)desc[i].data;
2367
2368 /* The first descriptor set the NEXT bit to 1 */
2369 if (i == 0)
2370 desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2371 else
2372 desc[i].flag &= ~cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2373
2374 for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
2375 tc = &s_buf->tc_thrd[i * HCLGE_TC_NUM_ONE_DESC + j];
2376
2377 req->com_thrd[j].high =
2378 cpu_to_le16(tc->high >> HCLGE_BUF_UNIT_S);
2379 req->com_thrd[j].high |=
2380 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2381 req->com_thrd[j].low =
2382 cpu_to_le16(tc->low >> HCLGE_BUF_UNIT_S);
2383 req->com_thrd[j].low |=
2384 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2385 }
2386 }
2387
2388 /* Send 2 descriptors at one time */
2389 ret = hclge_cmd_send(&hdev->hw, desc, 2);
2390 if (ret)
2391 dev_err(&hdev->pdev->dev,
2392 "common threshold config cmd failed %d\n", ret);
2393 return ret;
2394 }
2395
2396 static int hclge_common_wl_config(struct hclge_dev *hdev,
2397 struct hclge_pkt_buf_alloc *buf_alloc)
2398 {
2399 struct hclge_shared_buf *buf = &buf_alloc->s_buf;
2400 struct hclge_rx_com_wl *req;
2401 struct hclge_desc desc;
2402 int ret;
2403
2404 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_COM_WL_ALLOC, false);
2405
2406 req = (struct hclge_rx_com_wl *)desc.data;
2407 req->com_wl.high = cpu_to_le16(buf->self.high >> HCLGE_BUF_UNIT_S);
2408 req->com_wl.high |= cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2409
2410 req->com_wl.low = cpu_to_le16(buf->self.low >> HCLGE_BUF_UNIT_S);
2411 req->com_wl.low |= cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2412
2413 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2414 if (ret)
2415 dev_err(&hdev->pdev->dev,
2416 "common waterline config cmd failed %d\n", ret);
2417
2418 return ret;
2419 }
2420
2421 int hclge_buffer_alloc(struct hclge_dev *hdev)
2422 {
2423 struct hclge_pkt_buf_alloc *pkt_buf;
2424 int ret;
2425
2426 pkt_buf = kzalloc_obj(*pkt_buf);
2427 if (!pkt_buf)
2428 return -ENOMEM;
2429
2430 ret = hclge_tx_buffer_calc(hdev, pkt_buf);
2431 if (ret) {
2432 dev_err(&hdev->pdev->dev,
2433 "could not calc tx buffer size for all TCs %d\n", ret);
2434 goto out;
2435 }
2436
2437 ret = hclge_tx_buffer_alloc(hdev, pkt_buf);
2438 if (ret) {
2439 dev_err(&hdev->pdev->dev,
2440 "could not alloc tx buffers %d\n", ret);
2441 goto out;
2442 }
2443
2444 ret = hclge_rx_buffer_calc(hdev, pkt_buf);
2445 if (ret) {
2446 dev_err(&hdev->pdev->dev,
2447 "could not calc rx priv buffer size for all TCs %d\n",
2448 ret);
2449 goto out;
2450 }
2451
2452 ret = hclge_rx_priv_buf_alloc(hdev, pkt_buf);
2453 if (ret) {
2454 dev_err(&hdev->pdev->dev, "could not alloc rx priv buffer %d\n",
2455 ret);
2456 goto out;
2457 }
2458
2459 if (hnae3_dev_dcb_supported(hdev)) {
2460 ret = hclge_rx_priv_wl_config(hdev, pkt_buf);
2461 if (ret) {
2462 dev_err(&hdev->pdev->dev,
2463 "could not configure rx private waterline %d\n",
2464 ret);
2465 goto out;
2466 }
2467
2468 ret = hclge_common_thrd_config(hdev, pkt_buf);
2469 if (ret) {
2470 dev_err(&hdev->pdev->dev,
2471 "could not configure common threshold %d\n",
2472 ret);
2473 goto out;
2474 }
2475 }
2476
2477 ret = hclge_common_wl_config(hdev, pkt_buf);
2478 if (ret)
2479 dev_err(&hdev->pdev->dev,
2480 "could not configure common waterline %d\n", ret);
2481
2482 out:
2483 kfree(pkt_buf);
2484 return ret;
2485 }
2486
2487 static int hclge_init_roce_base_info(struct hclge_vport *vport)
2488 {
2489 struct hnae3_handle *roce = &vport->roce;
2490 struct hnae3_handle *nic = &vport->nic;
2491 struct hclge_dev *hdev = vport->back;
2492
2493 roce->rinfo.num_vectors = vport->back->num_roce_msi;
2494
2495 if (hdev->num_msi < hdev->num_nic_msi + hdev->num_roce_msi)
2496 return -EINVAL;
2497
2498 roce->rinfo.base_vector = hdev->num_nic_msi;
2499
2500 roce->rinfo.netdev = nic->kinfo.netdev;
2501 roce->rinfo.roce_io_base = hdev->hw.hw.io_base;
2502 roce->rinfo.roce_mem_base = hdev->hw.hw.mem_base;
2503
2504 roce->pdev = nic->pdev;
2505 roce->ae_algo = nic->ae_algo;
2506 bitmap_copy(roce->numa_node_mask.bits, nic->numa_node_mask.bits,
2507 MAX_NUMNODES);
2508
2509 return 0;
2510 }
2511
2512 static int hclge_init_msi(struct hclge_dev *hdev)
2513 {
2514 struct pci_dev *pdev = hdev->pdev;
2515 int vectors;
2516 int i;
2517
2518 vectors = pci_alloc_irq_vectors(pdev, HNAE3_MIN_VECTOR_NUM,
2519 hdev->num_msi,
2520 PCI_IRQ_MSI | PCI_IRQ_MSIX);
2521 if (vectors < 0) {
2522 dev_err(&pdev->dev,
2523 "failed(%d) to allocate MSI/MSI-X vectors\n",
2524 vectors);
2525 return vectors;
2526 }
2527 if (vectors < hdev->num_msi)
2528 dev_warn(&hdev->pdev->dev,
2529 "requested %u MSI/MSI-X, but allocated %d MSI/MSI-X\n",
2530 hdev->num_msi, vectors);
2531
2532 hdev->num_msi = vectors;
2533 hdev->num_msi_left = vectors;
2534
2535 hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
2536 sizeof(u16), GFP_KERNEL);
2537 if (!hdev->vector_status) {
2538 pci_free_irq_vectors(pdev);
2539 return -ENOMEM;
2540 }
2541
2542 for (i = 0; i < hdev->num_msi; i++)
2543 hdev->vector_status[i] = HCLGE_INVALID_VPORT;
2544
2545 hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
2546 sizeof(int), GFP_KERNEL);
2547 if (!hdev->vector_irq) {
2548 pci_free_irq_vectors(pdev);
2549 return -ENOMEM;
2550 }
2551
2552 return 0;
2553 }
2554
2555 static u8 hclge_check_speed_dup(u8 duplex, int speed)
2556 {
2557 if (!(speed == HCLGE_MAC_SPEED_10M || speed == HCLGE_MAC_SPEED_100M))
2558 duplex = HCLGE_MAC_FULL;
2559
2560 return duplex;
2561 }
2562
2563 static struct hclge_mac_speed_map hclge_mac_speed_map_to_fw[] = {
2564 {HCLGE_MAC_SPEED_10M, HCLGE_FW_MAC_SPEED_10M},
2565 {HCLGE_MAC_SPEED_100M, HCLGE_FW_MAC_SPEED_100M},
2566 {HCLGE_MAC_SPEED_1G, HCLGE_FW_MAC_SPEED_1G},
2567 {HCLGE_MAC_SPEED_10G, HCLGE_FW_MAC_SPEED_10G},
2568 {HCLGE_MAC_SPEED_25G, HCLGE_FW_MAC_SPEED_25G},
2569 {HCLGE_MAC_SPEED_40G, HCLGE_FW_MAC_SPEED_40G},
2570 {HCLGE_MAC_SPEED_50G, HCLGE_FW_MAC_SPEED_50G},
2571 {HCLGE_MAC_SPEED_100G, HCLGE_FW_MAC_SPEED_100G},
2572 {HCLGE_MAC_SPEED_200G, HCLGE_FW_MAC_SPEED_200G},
2573 };
2574
2575 static int hclge_convert_to_fw_speed(u32 speed_drv, u32 *speed_fw)
2576 {
2577 u16 i;
2578
2579 for (i = 0; i < ARRAY_SIZE(hclge_mac_speed_map_to_fw); i++) {
2580 if (hclge_mac_speed_map_to_fw[i].speed_drv == speed_drv) {
2581 *speed_fw = hclge_mac_speed_map_to_fw[i].speed_fw;
2582 return 0;
2583 }
2584 }
2585
2586 return -EINVAL;
2587 }
2588
2589 static int hclge_cfg_mac_speed_dup_hw(struct hclge_dev *hdev, int speed,
2590 u8 duplex, u8 lane_num)
2591 {
2592 struct hclge_config_mac_speed_dup_cmd *req;
2593 struct hclge_desc desc;
2594 u32 speed_fw;
2595 int ret;
2596
2597 req = (struct hclge_config_mac_speed_dup_cmd *)desc.data;
2598
2599 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_SPEED_DUP, false);
2600
2601 if (duplex)
2602 hnae3_set_bit(req->speed_dup, HCLGE_CFG_DUPLEX_B, 1);
2603
2604 ret = hclge_convert_to_fw_speed(speed, &speed_fw);
2605 if (ret) {
2606 dev_err(&hdev->pdev->dev, "invalid speed (%d)\n", speed);
2607 return ret;
2608 }
2609
2610 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M, HCLGE_CFG_SPEED_S,
2611 speed_fw);
2612 hnae3_set_bit(req->mac_change_fec_en, HCLGE_CFG_MAC_SPEED_CHANGE_EN_B,
2613 1);
2614 req->lane_num = lane_num;
2615
2616 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2617 if (ret) {
2618 dev_err(&hdev->pdev->dev,
2619 "mac speed/duplex config cmd failed %d.\n", ret);
2620 return ret;
2621 }
2622
2623 return 0;
2624 }
2625
2626 int hclge_cfg_mac_speed_dup(struct hclge_dev *hdev, int speed, u8 duplex, u8 lane_num)
2627 {
2628 struct hclge_mac *mac = &hdev->hw.mac;
2629 int ret;
2630
2631 duplex = hclge_check_speed_dup(duplex, speed);
2632 if (!mac->support_autoneg && mac->speed == (u32)speed &&
2633 mac->duplex == duplex && (mac->lane_num == lane_num || lane_num == 0))
2634 return 0;
2635
2636 ret = hclge_cfg_mac_speed_dup_hw(hdev, speed, duplex, lane_num);
2637 if (ret)
2638 return ret;
2639
2640 hdev->hw.mac.speed = speed;
2641 hdev->hw.mac.duplex = duplex;
2642 if (!lane_num)
2643 hdev->hw.mac.lane_num = lane_num;
2644
2645 return 0;
2646 }
2647
2648 static int hclge_cfg_mac_speed_dup_h(struct hnae3_handle *handle, int speed,
2649 u8 duplex, u8 lane_num)
2650 {
2651 struct hclge_vport *vport = hclge_get_vport(handle);
2652 struct hclge_dev *hdev = vport->back;
2653 int ret;
2654
2655 ret = hclge_cfg_mac_speed_dup(hdev, speed, duplex, lane_num);
2656
2657 if (ret)
2658 return ret;
2659
2660 hdev->hw.mac.req_lane_num = lane_num;
2661 if (speed != SPEED_UNKNOWN)
2662 hdev->hw.mac.req_speed = (u32)speed;
2663 if (duplex != DUPLEX_UNKNOWN)
2664 hdev->hw.mac.req_duplex = duplex;
2665
2666 return 0;
2667 }
2668
2669 static int hclge_set_autoneg_en(struct hclge_dev *hdev, bool enable)
2670 {
2671 struct hclge_config_auto_neg_cmd *req;
2672 struct hclge_desc desc;
2673 u32 flag = 0;
2674 int ret;
2675
2676 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_AN_MODE, false);
2677
2678 req = (struct hclge_config_auto_neg_cmd *)desc.data;
2679 if (enable)
2680 hnae3_set_bit(flag, HCLGE_MAC_CFG_AN_EN_B, 1U);
2681 req->cfg_an_cmd_flag = cpu_to_le32(flag);
2682
2683 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2684 if (ret)
2685 dev_err(&hdev->pdev->dev, "auto neg set cmd failed %d.\n",
2686 ret);
2687
2688 return ret;
2689 }
2690
2691 static int hclge_set_autoneg(struct hnae3_handle *handle, bool enable)
2692 {
2693 struct hclge_vport *vport = hclge_get_vport(handle);
2694 struct hclge_dev *hdev = vport->back;
2695 int ret;
2696
2697 if (!hdev->hw.mac.support_autoneg) {
2698 if (enable) {
2699 dev_err(&hdev->pdev->dev,
2700 "autoneg is not supported by current port\n");
2701 return -EOPNOTSUPP;
2702 } else {
2703 return 0;
2704 }
2705 }
2706
2707 ret = hclge_set_autoneg_en(hdev, enable);
2708 if (!ret)
2709 hdev->hw.mac.req_autoneg = enable;
2710 return ret;
2711 }
2712
2713 static int hclge_get_autoneg(struct hnae3_handle *handle)
2714 {
2715 struct hclge_vport *vport = hclge_get_vport(handle);
2716 struct hclge_dev *hdev = vport->back;
2717 struct phy_device *phydev = hdev->hw.mac.phydev;
2718
2719 if (phydev)
2720 return phydev->autoneg;
2721
2722 return hdev->hw.mac.autoneg;
2723 }
2724
2725 static int hclge_restart_autoneg(struct hnae3_handle *handle)
2726 {
2727 struct hclge_vport *vport = hclge_get_vport(handle);
2728 struct hclge_dev *hdev = vport->back;
2729 int ret;
2730
2731 dev_dbg(&hdev->pdev->dev, "restart autoneg\n");
2732
2733 ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
2734 if (ret)
2735 return ret;
2736 return hclge_notify_client(hdev, HNAE3_UP_CLIENT);
2737 }
2738
2739 static int hclge_halt_autoneg(struct hnae3_handle *handle, bool halt)
2740 {
2741 struct hclge_vport *vport = hclge_get_vport(handle);
2742 struct hclge_dev *hdev = vport->back;
2743
2744 if (hdev->hw.mac.support_autoneg && hdev->hw.mac.autoneg)
2745 return hclge_set_autoneg_en(hdev, !halt);
2746
2747 return 0;
2748 }
2749
2750 static void hclge_parse_fec_stats_lanes(struct hclge_dev *hdev,
2751 struct hclge_desc *desc, u32 desc_len)
2752 {
2753 u32 lane_size = HCLGE_FEC_STATS_MAX_LANES * 2;
2754 u32 desc_index = 0;
2755 u32 data_index = 0;
2756 u32 i;
2757
2758 for (i = 0; i < lane_size; i++) {
2759 if (data_index >= HCLGE_DESC_DATA_LEN) {
2760 desc_index++;
2761 data_index = 0;
2762 }
2763
2764 if (desc_index >= desc_len)
2765 return;
2766
2767 hdev->fec_stats.per_lanes[i] +=
2768 le32_to_cpu(desc[desc_index].data[data_index]);
2769 data_index++;
2770 }
2771 }
2772
2773 static void hclge_parse_fec_stats(struct hclge_dev *hdev,
2774 struct hclge_desc *desc, u32 desc_len)
2775 {
2776 struct hclge_query_fec_stats_cmd *req;
2777
2778 req = (struct hclge_query_fec_stats_cmd *)desc[0].data;
2779
2780 hdev->fec_stats.base_r_lane_num = req->base_r_lane_num;
2781 hdev->fec_stats.rs_corr_blocks +=
2782 le32_to_cpu(req->rs_fec_corr_blocks);
2783 hdev->fec_stats.rs_uncorr_blocks +=
2784 le32_to_cpu(req->rs_fec_uncorr_blocks);
2785 hdev->fec_stats.rs_error_blocks +=
2786 le32_to_cpu(req->rs_fec_error_blocks);
2787 hdev->fec_stats.base_r_corr_blocks +=
2788 le32_to_cpu(req->base_r_fec_corr_blocks);
2789 hdev->fec_stats.base_r_uncorr_blocks +=
2790 le32_to_cpu(req->base_r_fec_uncorr_blocks);
2791
2792 hclge_parse_fec_stats_lanes(hdev, &desc[1], desc_len - 1);
2793 }
2794
2795 static int hclge_update_fec_stats_hw(struct hclge_dev *hdev)
2796 {
2797 struct hclge_desc desc[HCLGE_FEC_STATS_CMD_NUM];
2798 int ret;
2799 u32 i;
2800
2801 for (i = 0; i < HCLGE_FEC_STATS_CMD_NUM; i++) {
2802 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_FEC_STATS,
2803 true);
2804 if (i != (HCLGE_FEC_STATS_CMD_NUM - 1))
2805 desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2806 }
2807
2808 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_FEC_STATS_CMD_NUM);
2809 if (ret)
2810 return ret;
2811
2812 hclge_parse_fec_stats(hdev, desc, HCLGE_FEC_STATS_CMD_NUM);
2813
2814 return 0;
2815 }
2816
2817 static void hclge_update_fec_stats(struct hclge_dev *hdev)
2818 {
2819 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
2820 int ret;
2821
2822 if (!hnae3_ae_dev_fec_stats_supported(ae_dev) ||
2823 test_and_set_bit(HCLGE_STATE_FEC_STATS_UPDATING, &hdev->state))
2824 return;
2825
2826 ret = hclge_update_fec_stats_hw(hdev);
2827 if (ret)
2828 dev_err(&hdev->pdev->dev,
2829 "failed to update fec stats, ret = %d\n", ret);
2830
2831 clear_bit(HCLGE_STATE_FEC_STATS_UPDATING, &hdev->state);
2832 }
2833
2834 static void hclge_get_fec_stats_total(struct hclge_dev *hdev,
2835 struct ethtool_fec_stats *fec_stats)
2836 {
2837 fec_stats->corrected_blocks.total = hdev->fec_stats.rs_corr_blocks;
2838 fec_stats->uncorrectable_blocks.total =
2839 hdev->fec_stats.rs_uncorr_blocks;
2840 }
2841
2842 static void hclge_get_fec_stats_lanes(struct hclge_dev *hdev,
2843 struct ethtool_fec_stats *fec_stats)
2844 {
2845 u32 i;
2846
2847 if (hdev->fec_stats.base_r_lane_num == 0 ||
2848 hdev->fec_stats.base_r_lane_num > HCLGE_FEC_STATS_MAX_LANES) {
2849 dev_err(&hdev->pdev->dev,
2850 "fec stats lane number(%llu) is invalid\n",
2851 hdev->fec_stats.base_r_lane_num);
2852 return;
2853 }
2854
2855 for (i = 0; i < hdev->fec_stats.base_r_lane_num; i++) {
2856 fec_stats->corrected_blocks.lanes[i] =
2857 hdev->fec_stats.base_r_corr_per_lanes[i];
2858 fec_stats->uncorrectable_blocks.lanes[i] =
2859 hdev->fec_stats.base_r_uncorr_per_lanes[i];
2860 }
2861 }
2862
2863 static void hclge_comm_get_fec_stats(struct hclge_dev *hdev,
2864 struct ethtool_fec_stats *fec_stats)
2865 {
2866 u32 fec_mode = hdev->hw.mac.fec_mode;
2867
2868 switch (fec_mode) {
2869 case BIT(HNAE3_FEC_RS):
2870 case BIT(HNAE3_FEC_LLRS):
2871 hclge_get_fec_stats_total(hdev, fec_stats);
2872 break;
2873 case BIT(HNAE3_FEC_BASER):
2874 hclge_get_fec_stats_lanes(hdev, fec_stats);
2875 break;
2876 default:
2877 dev_err(&hdev->pdev->dev,
2878 "fec stats is not supported by current fec mode(0x%x)\n",
2879 fec_mode);
2880 break;
2881 }
2882 }
2883
2884 static void hclge_get_fec_stats(struct hnae3_handle *handle,
2885 struct ethtool_fec_stats *fec_stats)
2886 {
2887 struct hclge_vport *vport = hclge_get_vport(handle);
2888 struct hclge_dev *hdev = vport->back;
2889 u32 fec_mode = hdev->hw.mac.fec_mode;
2890
2891 if (fec_mode == BIT(HNAE3_FEC_NONE) ||
2892 fec_mode == BIT(HNAE3_FEC_AUTO) ||
2893 fec_mode == BIT(HNAE3_FEC_USER_DEF))
2894 return;
2895
2896 hclge_update_fec_stats(hdev);
2897
2898 hclge_comm_get_fec_stats(hdev, fec_stats);
2899 }
2900
2901 static int hclge_set_fec_hw(struct hclge_dev *hdev, u32 fec_mode)
2902 {
2903 struct hclge_config_fec_cmd *req;
2904 struct hclge_desc desc;
2905 int ret;
2906
2907 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_FEC_MODE, false);
2908
2909 req = (struct hclge_config_fec_cmd *)desc.data;
2910 if (fec_mode & BIT(HNAE3_FEC_AUTO))
2911 hnae3_set_bit(req->fec_mode, HCLGE_MAC_CFG_FEC_AUTO_EN_B, 1);
2912 if (fec_mode & BIT(HNAE3_FEC_RS))
2913 hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2914 HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_RS);
2915 if (fec_mode & BIT(HNAE3_FEC_LLRS))
2916 hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2917 HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_LLRS);
2918 if (fec_mode & BIT(HNAE3_FEC_BASER))
2919 hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2920 HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_BASER);
2921
2922 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2923 if (ret)
2924 dev_err(&hdev->pdev->dev, "set fec mode failed %d.\n", ret);
2925
2926 return ret;
2927 }
2928
2929 static int hclge_set_fec(struct hnae3_handle *handle, u32 fec_mode)
2930 {
2931 struct hclge_vport *vport = hclge_get_vport(handle);
2932 struct hclge_dev *hdev = vport->back;
2933 struct hclge_mac *mac = &hdev->hw.mac;
2934 int ret;
2935
2936 if (fec_mode && !(mac->fec_ability & fec_mode)) {
2937 dev_err(&hdev->pdev->dev, "unsupported fec mode\n");
2938 return -EINVAL;
2939 }
2940
2941 ret = hclge_set_fec_hw(hdev, fec_mode);
2942 if (ret)
2943 return ret;
2944
2945 mac->user_fec_mode = fec_mode | BIT(HNAE3_FEC_USER_DEF);
2946 return 0;
2947 }
2948
2949 static void hclge_get_fec(struct hnae3_handle *handle, u8 *fec_ability,
2950 u8 *fec_mode)
2951 {
2952 struct hclge_vport *vport = hclge_get_vport(handle);
2953 struct hclge_dev *hdev = vport->back;
2954 struct hclge_mac *mac = &hdev->hw.mac;
2955
2956 if (fec_ability)
2957 *fec_ability = mac->fec_ability;
2958 if (fec_mode)
2959 *fec_mode = mac->fec_mode;
2960 }
2961
2962 static int hclge_mac_init(struct hclge_dev *hdev)
2963 {
2964 struct hclge_mac *mac = &hdev->hw.mac;
2965 int ret;
2966
2967 hdev->support_sfp_query = true;
2968
2969 if (!test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
2970 hdev->hw.mac.duplex = HCLGE_MAC_FULL;
2971
2972 mac->link = 0;
2973
2974 if (mac->user_fec_mode & BIT(HNAE3_FEC_USER_DEF)) {
2975 ret = hclge_set_fec_hw(hdev, mac->user_fec_mode);
2976 if (ret)
2977 return ret;
2978 }
2979
2980 ret = hclge_set_mac_mtu(hdev, hdev->mps);
2981 if (ret) {
2982 dev_err(&hdev->pdev->dev, "set mtu failed ret=%d\n", ret);
2983 return ret;
2984 }
2985
2986 ret = hclge_set_default_loopback(hdev);
2987 if (ret)
2988 return ret;
2989
2990 ret = hclge_buffer_alloc(hdev);
2991 if (ret)
2992 dev_err(&hdev->pdev->dev,
2993 "allocate buffer fail, ret=%d\n", ret);
2994
2995 return ret;
2996 }
2997
2998 static void hclge_mbx_task_schedule(struct hclge_dev *hdev)
2999 {
3000 if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
3001 !test_and_set_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state)) {
3002 hdev->last_mbx_scheduled = jiffies;
3003 mod_delayed_work(hclge_wq, &hdev->service_task, 0);
3004 }
3005 }
3006
3007 static void hclge_reset_task_schedule(struct hclge_dev *hdev)
3008 {
3009 if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
3010 test_bit(HCLGE_STATE_SERVICE_INITED, &hdev->state) &&
3011 !test_and_set_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state)) {
3012 hdev->last_rst_scheduled = jiffies;
3013 mod_delayed_work(hclge_wq, &hdev->service_task, 0);
3014 }
3015 }
3016
3017 static void hclge_errhand_task_schedule(struct hclge_dev *hdev)
3018 {
3019 if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
3020 !test_and_set_bit(HCLGE_STATE_ERR_SERVICE_SCHED, &hdev->state))
3021 mod_delayed_work(hclge_wq, &hdev->service_task, 0);
3022 }
3023
3024 void hclge_task_schedule(struct hclge_dev *hdev, unsigned long delay_time)
3025 {
3026 if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
3027 !test_bit(HCLGE_STATE_RST_FAIL, &hdev->state))
3028 mod_delayed_work(hclge_wq, &hdev->service_task, delay_time);
3029 }
3030
3031 static int hclge_get_mac_link_status(struct hclge_dev *hdev, int *link_status)
3032 {
3033 struct hclge_link_status_cmd *req;
3034 struct hclge_desc desc;
3035 int ret;
3036
3037 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_LINK_STATUS, true);
3038 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3039 if (ret) {
3040 dev_err(&hdev->pdev->dev, "get link status cmd failed %d\n",
3041 ret);
3042 return ret;
3043 }
3044
3045 req = (struct hclge_link_status_cmd *)desc.data;
3046 *link_status = (req->status & HCLGE_LINK_STATUS_UP_M) > 0 ?
3047 HCLGE_LINK_STATUS_UP : HCLGE_LINK_STATUS_DOWN;
3048
3049 return 0;
3050 }
3051
3052 static int hclge_get_mac_phy_link(struct hclge_dev *hdev, int *link_status)
3053 {
3054 struct phy_device *phydev = hdev->hw.mac.phydev;
3055
3056 *link_status = HCLGE_LINK_STATUS_DOWN;
3057
3058 if (test_bit(HCLGE_STATE_DOWN, &hdev->state))
3059 return 0;
3060
3061 if (phydev && (phydev->state != PHY_RUNNING || !phydev->link))
3062 return 0;
3063
3064 return hclge_get_mac_link_status(hdev, link_status);
3065 }
3066
3067 static void hclge_push_link_status(struct hclge_dev *hdev)
3068 {
3069 struct hclge_vport *vport;
3070 int ret;
3071 u16 i;
3072
3073 for (i = 0; i < pci_num_vf(hdev->pdev); i++) {
3074 vport = &hdev->vport[i + HCLGE_VF_VPORT_START_NUM];
3075
3076 if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state) ||
3077 vport->vf_info.link_state != IFLA_VF_LINK_STATE_AUTO)
3078 continue;
3079
3080 ret = hclge_push_vf_link_status(vport);
3081 if (ret) {
3082 dev_err(&hdev->pdev->dev,
3083 "failed to push link status to vf%u, ret = %d\n",
3084 i, ret);
3085 }
3086 }
3087 }
3088
3089 static void hclge_update_link_status(struct hclge_dev *hdev)
3090 {
3091 struct hnae3_handle *handle = &hdev->vport[0].nic;
3092 struct hnae3_client *client = hdev->nic_client;
3093 int state;
3094 int ret;
3095
3096 if (!client)
3097 return;
3098
3099 if (test_and_set_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state))
3100 return;
3101
3102 ret = hclge_get_mac_phy_link(hdev, &state);
3103 if (ret) {
3104 clear_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
3105 return;
3106 }
3107
3108 if (state != hdev->hw.mac.link) {
3109 hdev->hw.mac.link = state;
3110 if (state == HCLGE_LINK_STATUS_UP)
3111 hclge_update_port_info(hdev);
3112
3113 client->ops->link_status_change(handle, state);
3114 hclge_config_mac_tnl_int(hdev, state);
3115
3116 if (test_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state)) {
3117 struct hnae3_handle *rhandle = &hdev->vport[0].roce;
3118 struct hnae3_client *rclient = hdev->roce_client;
3119
3120 if (rclient && rclient->ops->link_status_change)
3121 rclient->ops->link_status_change(rhandle,
3122 state);
3123 }
3124
3125 hclge_push_link_status(hdev);
3126 }
3127
3128 clear_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
3129 }
3130
3131 static void hclge_update_speed_advertising(struct hclge_mac *mac)
3132 {
3133 u32 speed_ability;
3134
3135 if (hclge_get_speed_bit(mac->speed, &speed_ability))
3136 return;
3137
3138 switch (mac->module_type) {
3139 case HNAE3_MODULE_TYPE_FIBRE_LR:
3140 hclge_convert_setting_lr(speed_ability, mac->advertising);
3141 break;
3142 case HNAE3_MODULE_TYPE_FIBRE_SR:
3143 case HNAE3_MODULE_TYPE_AOC:
3144 hclge_convert_setting_sr(speed_ability, mac->advertising);
3145 break;
3146 case HNAE3_MODULE_TYPE_CR:
3147 hclge_convert_setting_cr(speed_ability, mac->advertising);
3148 break;
3149 case HNAE3_MODULE_TYPE_KR:
3150 hclge_convert_setting_kr(speed_ability, mac->advertising);
3151 break;
3152 default:
3153 break;
3154 }
3155 }
3156
3157 static void hclge_update_fec_advertising(struct hclge_mac *mac)
3158 {
3159 if (mac->fec_mode & BIT(HNAE3_FEC_RS))
3160 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
3161 mac->advertising);
3162 else if (mac->fec_mode & BIT(HNAE3_FEC_LLRS))
3163 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
3164 mac->advertising);
3165 else if (mac->fec_mode & BIT(HNAE3_FEC_BASER))
3166 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
3167 mac->advertising);
3168 else
3169 linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
3170 mac->advertising);
3171 }
3172
3173 static void hclge_update_pause_advertising(struct hclge_dev *hdev)
3174 {
3175 struct hclge_mac *mac = &hdev->hw.mac;
3176 bool rx_en, tx_en;
3177
3178 switch (hdev->fc_mode_last_time) {
3179 case HCLGE_FC_RX_PAUSE:
3180 rx_en = true;
3181 tx_en = false;
3182 break;
3183 case HCLGE_FC_TX_PAUSE:
3184 rx_en = false;
3185 tx_en = true;
3186 break;
3187 case HCLGE_FC_FULL:
3188 rx_en = true;
3189 tx_en = true;
3190 break;
3191 default:
3192 rx_en = false;
3193 tx_en = false;
3194 break;
3195 }
3196
3197 linkmode_set_pause(mac->advertising, tx_en, rx_en);
3198 }
3199
3200 static void hclge_update_advertising(struct hclge_dev *hdev)
3201 {
3202 struct hclge_mac *mac = &hdev->hw.mac;
3203
3204 linkmode_zero(mac->advertising);
3205 hclge_update_speed_advertising(mac);
3206 hclge_update_fec_advertising(mac);
3207 hclge_update_pause_advertising(hdev);
3208 }
3209
3210 static void hclge_update_port_capability(struct hclge_dev *hdev,
3211 struct hclge_mac *mac)
3212 {
3213 if (hnae3_dev_fec_supported(hdev))
3214 hclge_convert_setting_fec(mac);
3215
3216 /* firmware can not identify back plane type, the media type
3217 * read from configuration can help deal it
3218 */
3219 if (mac->media_type == HNAE3_MEDIA_TYPE_BACKPLANE &&
3220 mac->module_type == HNAE3_MODULE_TYPE_UNKNOWN)
3221 mac->module_type = HNAE3_MODULE_TYPE_KR;
3222 else if (mac->media_type == HNAE3_MEDIA_TYPE_COPPER)
3223 mac->module_type = HNAE3_MODULE_TYPE_TP;
3224
3225 if (mac->support_autoneg) {
3226 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mac->supported);
3227 linkmode_copy(mac->advertising, mac->supported);
3228 } else {
3229 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3230 mac->supported);
3231 hclge_update_advertising(hdev);
3232 }
3233 }
3234
3235 static int hclge_get_sfp_speed(struct hclge_dev *hdev, u32 *speed)
3236 {
3237 struct hclge_sfp_info_cmd *resp;
3238 struct hclge_desc desc;
3239 int ret;
3240
3241 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_INFO, true);
3242 resp = (struct hclge_sfp_info_cmd *)desc.data;
3243 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3244 if (ret == -EOPNOTSUPP) {
3245 dev_warn(&hdev->pdev->dev,
3246 "IMP do not support get SFP speed %d\n", ret);
3247 return ret;
3248 } else if (ret) {
3249 dev_err(&hdev->pdev->dev, "get sfp speed failed %d\n", ret);
3250 return ret;
3251 }
3252
3253 *speed = le32_to_cpu(resp->speed);
3254
3255 return 0;
3256 }
3257
3258 static int hclge_get_sfp_info(struct hclge_dev *hdev, struct hclge_mac *mac)
3259 {
3260 struct hclge_sfp_info_cmd *resp;
3261 struct hclge_desc desc;
3262 int ret;
3263
3264 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_INFO, true);
3265 resp = (struct hclge_sfp_info_cmd *)desc.data;
3266
3267 resp->query_type = QUERY_ACTIVE_SPEED;
3268
3269 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3270 if (ret == -EOPNOTSUPP) {
3271 dev_warn(&hdev->pdev->dev,
3272 "IMP does not support get SFP info %d\n", ret);
3273 return ret;
3274 } else if (ret) {
3275 dev_err(&hdev->pdev->dev, "get sfp info failed %d\n", ret);
3276 return ret;
3277 }
3278
3279 /* In some case, mac speed get from IMP may be 0, it shouldn't be
3280 * set to mac->speed.
3281 */
3282 if (!le32_to_cpu(resp->speed))
3283 return 0;
3284
3285 mac->speed = le32_to_cpu(resp->speed);
3286 /* if resp->speed_ability is 0, it means it's an old version
3287 * firmware, do not update these params
3288 */
3289 if (resp->speed_ability) {
3290 mac->module_type = le32_to_cpu(resp->module_type);
3291 mac->speed_ability = le32_to_cpu(resp->speed_ability);
3292 mac->autoneg = resp->autoneg;
3293 mac->support_autoneg = resp->autoneg_ability;
3294 mac->speed_type = QUERY_ACTIVE_SPEED;
3295 mac->lane_num = resp->lane_num;
3296 if (!resp->active_fec)
3297 mac->fec_mode = 0;
3298 else
3299 mac->fec_mode = BIT(resp->active_fec);
3300 mac->fec_ability = resp->fec_ability;
3301 } else {
3302 mac->speed_type = QUERY_SFP_SPEED;
3303 }
3304
3305 return 0;
3306 }
3307
3308 static int hclge_get_phy_link_ksettings(struct hnae3_handle *handle,
3309 struct ethtool_link_ksettings *cmd)
3310 {
3311 struct hclge_desc desc[HCLGE_PHY_LINK_SETTING_BD_NUM];
3312 struct hclge_vport *vport = hclge_get_vport(handle);
3313 struct hclge_phy_link_ksetting_0_cmd *req0;
3314 struct hclge_phy_link_ksetting_1_cmd *req1;
3315 u32 supported, advertising, lp_advertising;
3316 struct hclge_dev *hdev = vport->back;
3317 int ret;
3318
3319 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_PHY_LINK_KSETTING,
3320 true);
3321 desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
3322 hclge_cmd_setup_basic_desc(&desc[1], HCLGE_OPC_PHY_LINK_KSETTING,
3323 true);
3324
3325 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PHY_LINK_SETTING_BD_NUM);
3326 if (ret) {
3327 dev_err(&hdev->pdev->dev,
3328 "failed to get phy link ksetting, ret = %d.\n", ret);
3329 return ret;
3330 }
3331
3332 req0 = (struct hclge_phy_link_ksetting_0_cmd *)desc[0].data;
3333 cmd->base.autoneg = req0->autoneg;
3334 cmd->base.speed = le32_to_cpu(req0->speed);
3335 cmd->base.duplex = req0->duplex;
3336 cmd->base.port = req0->port;
3337 cmd->base.transceiver = req0->transceiver;
3338 cmd->base.phy_address = req0->phy_address;
3339 cmd->base.eth_tp_mdix = req0->eth_tp_mdix;
3340 cmd->base.eth_tp_mdix_ctrl = req0->eth_tp_mdix_ctrl;
3341 supported = le32_to_cpu(req0->supported);
3342 advertising = le32_to_cpu(req0->advertising);
3343 lp_advertising = le32_to_cpu(req0->lp_advertising);
3344 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
3345 supported);
3346 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
3347 advertising);
3348 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
3349 lp_advertising);
3350
3351 req1 = (struct hclge_phy_link_ksetting_1_cmd *)desc[1].data;
3352 cmd->base.master_slave_cfg = req1->master_slave_cfg;
3353 cmd->base.master_slave_state = req1->master_slave_state;
3354
3355 return 0;
3356 }
3357
3358 static int
3359 hclge_ethtool_ksettings_set(struct hnae3_handle *handle,
3360 const struct ethtool_link_ksettings *cmd)
3361 {
3362 struct hclge_desc desc[HCLGE_PHY_LINK_SETTING_BD_NUM];
3363 struct hclge_vport *vport = hclge_get_vport(handle);
3364 struct hclge_phy_link_ksetting_0_cmd *req0;
3365 struct hclge_phy_link_ksetting_1_cmd *req1;
3366 struct hclge_dev *hdev = vport->back;
3367 u32 advertising;
3368 int ret;
3369
3370 if (cmd->base.autoneg == AUTONEG_DISABLE &&
3371 ((cmd->base.speed != SPEED_100 && cmd->base.speed != SPEED_10) ||
3372 (cmd->base.duplex != DUPLEX_HALF &&
3373 cmd->base.duplex != DUPLEX_FULL)))
3374 return -EINVAL;
3375
3376 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_PHY_LINK_KSETTING,
3377 false);
3378 desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
3379 hclge_cmd_setup_basic_desc(&desc[1], HCLGE_OPC_PHY_LINK_KSETTING,
3380 false);
3381
3382 req0 = (struct hclge_phy_link_ksetting_0_cmd *)desc[0].data;
3383 req0->autoneg = cmd->base.autoneg;
3384 req0->speed = cpu_to_le32(cmd->base.speed);
3385 req0->duplex = cmd->base.duplex;
3386 ethtool_convert_link_mode_to_legacy_u32(&advertising,
3387 cmd->link_modes.advertising);
3388 req0->advertising = cpu_to_le32(advertising);
3389 req0->eth_tp_mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
3390
3391 req1 = (struct hclge_phy_link_ksetting_1_cmd *)desc[1].data;
3392 req1->master_slave_cfg = cmd->base.master_slave_cfg;
3393
3394 ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PHY_LINK_SETTING_BD_NUM);
3395 if (ret) {
3396 dev_err(&hdev->pdev->dev,
3397 "failed to set phy link ksettings, ret = %d.\n", ret);
3398 return ret;
3399 }
3400
3401 linkmode_copy(hdev->hw.mac.advertising, cmd->link_modes.advertising);
3402 return 0;
3403 }
3404
3405 static int
3406 hclge_set_phy_link_ksettings(struct hnae3_handle *handle,
3407 const struct ethtool_link_ksettings *cmd)
3408 {
3409 struct hclge_vport *vport = hclge_get_vport(handle);
3410 struct hclge_dev *hdev = vport->back;
3411 int ret = -ENODEV;
3412
3413 if (hnae3_dev_phy_imp_supported(hdev)) {
3414 ret = hclge_ethtool_ksettings_set(handle, cmd);
3415 } else if (handle->netdev->phydev) {
3416 if (cmd->base.speed == SPEED_1000 &&
3417 cmd->base.autoneg == AUTONEG_DISABLE)
3418 return -EINVAL;
3419 ret = phy_ethtool_ksettings_set(handle->netdev->phydev, cmd);
3420 }
3421 if (ret)
3422 return ret;
3423
3424 hdev->hw.mac.req_autoneg = cmd->base.autoneg;
3425 if (cmd->base.speed != SPEED_UNKNOWN)
3426 hdev->hw.mac.req_speed = cmd->base.speed;
3427 if (cmd->base.duplex != DUPLEX_UNKNOWN)
3428 hdev->hw.mac.req_duplex = cmd->base.duplex;
3429
3430 return 0;
3431 }
3432
3433 static int hclge_update_tp_port_info(struct hclge_dev *hdev)
3434 {
3435 struct ethtool_link_ksettings cmd;
3436 int ret;
3437
3438 if (!hnae3_dev_phy_imp_supported(hdev))
3439 return 0;
3440
3441 ret = hclge_get_phy_link_ksettings(&hdev->vport->nic, &cmd);
3442 if (ret)
3443 return ret;
3444
3445 hdev->hw.mac.autoneg = cmd.base.autoneg;
3446 hdev->hw.mac.speed = cmd.base.speed;
3447 hdev->hw.mac.duplex = cmd.base.duplex;
3448 linkmode_copy(hdev->hw.mac.advertising, cmd.link_modes.advertising);
3449
3450 return 0;
3451 }
3452
3453 static int hclge_tp_port_init(struct hclge_dev *hdev)
3454 {
3455 struct ethtool_link_ksettings cmd;
3456
3457 if (!hnae3_dev_phy_imp_supported(hdev))
3458 return 0;
3459
3460 cmd.base.autoneg = hdev->hw.mac.req_autoneg;
3461 cmd.base.speed = hdev->hw.mac.req_speed;
3462 cmd.base.duplex = hdev->hw.mac.req_duplex;
3463 linkmode_copy(cmd.link_modes.advertising, hdev->hw.mac.advertising);
3464
3465 return hclge_set_phy_link_ksettings(&hdev->vport->nic, &cmd);
3466 }
3467
3468 static int hclge_update_port_info(struct hclge_dev *hdev)
3469 {
3470 struct hclge_mac *mac = &hdev->hw.mac;
3471 u32 speed;
3472 int ret;
3473
3474 /* get the port info from SFP cmd if not copper port */
3475 if (mac->media_type == HNAE3_MEDIA_TYPE_COPPER)
3476 return hclge_update_tp_port_info(hdev);
3477
3478 /* if IMP does not support get SFP/qSFP info, return directly */
3479 if (!hdev->support_sfp_query)
3480 return 0;
3481
3482 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
3483 speed = mac->speed;
3484 ret = hclge_get_sfp_info(hdev, mac);
3485 } else {
3486 speed = HCLGE_MAC_SPEED_UNKNOWN;
3487 ret = hclge_get_sfp_speed(hdev, &speed);
3488 }
3489
3490 if (ret == -EOPNOTSUPP) {
3491 hdev->support_sfp_query = false;
3492 return ret;
3493 } else if (ret) {
3494 return ret;
3495 }
3496
3497 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
3498 if (mac->speed_type == QUERY_ACTIVE_SPEED) {
3499 hclge_update_port_capability(hdev, mac);
3500 if (mac->speed != speed)
3501 (void)hclge_tm_port_shaper_cfg(hdev);
3502 return 0;
3503 }
3504 return hclge_cfg_mac_speed_dup(hdev, mac->speed,
3505 HCLGE_MAC_FULL, mac->lane_num);
3506 } else {
3507 if (speed == HCLGE_MAC_SPEED_UNKNOWN)
3508 return 0; /* do nothing if no SFP */
3509
3510 /* must config full duplex for SFP */
3511 return hclge_cfg_mac_speed_dup(hdev, speed, HCLGE_MAC_FULL, 0);
3512 }
3513 }
3514
3515 static int hclge_get_status(struct hnae3_handle *handle)
3516 {
3517 struct hclge_vport *vport = hclge_get_vport(handle);
3518 struct hclge_dev *hdev = vport->back;
3519
3520 hclge_update_link_status(hdev);
3521
3522 return hdev->hw.mac.link;
3523 }
3524
3525 struct hclge_vport *hclge_get_vf_vport(struct hclge_dev *hdev, int vf)
3526 {
3527 if (!pci_num_vf(hdev->pdev)) {
3528 dev_err(&hdev->pdev->dev,
3529 "SRIOV is disabled, can not get vport(%d) info.\n", vf);
3530 return NULL;
3531 }
3532
3533 if (vf < 0 || vf >= pci_num_vf(hdev->pdev)) {
3534 dev_err(&hdev->pdev->dev,
3535 "vf id(%d) is out of range(0 <= vfid < %d)\n",
3536 vf, pci_num_vf(hdev->pdev));
3537 return NULL;
3538 }
3539
3540 /* VF start from 1 in vport */
3541 vf += HCLGE_VF_VPORT_START_NUM;
3542 return &hdev->vport[vf];
3543 }
3544
3545 static int hclge_get_vf_config(struct hnae3_handle *handle, int vf,
3546 struct ifla_vf_info *ivf)
3547 {
3548 struct hclge_vport *vport = hclge_get_vport(handle);
3549 struct hclge_dev *hdev = vport->back;
3550
3551 vport = hclge_get_vf_vport(hdev, vf);
3552 if (!vport)
3553 return -EINVAL;
3554
3555 ivf->vf = vf;
3556 ivf->linkstate = vport->vf_info.link_state;
3557 ivf->spoofchk = vport->vf_info.spoofchk;
3558 ivf->trusted = vport->vf_info.trusted;
3559 ivf->min_tx_rate = 0;
3560 ivf->max_tx_rate = vport->vf_info.max_tx_rate;
3561 ivf->vlan = vport->port_base_vlan_cfg.vlan_info.vlan_tag;
3562 ivf->vlan_proto = htons(vport->port_base_vlan_cfg.vlan_info.vlan_proto);
3563 ivf->qos = vport->port_base_vlan_cfg.vlan_info.qos;
3564 ether_addr_copy(ivf->mac, vport->vf_info.mac);
3565
3566 return 0;
3567 }
3568
3569 static int hclge_set_vf_link_state(struct hnae3_handle *handle, int vf,
3570 int link_state)
3571 {
3572 struct hclge_vport *vport = hclge_get_vport(handle);
3573 struct hclge_dev *hdev = vport->back;
3574 int link_state_old;
3575 int ret;
3576
3577 vport = hclge_get_vf_vport(hdev, vf);
3578 if (!vport)
3579 return -EINVAL;
3580
3581 link_state_old = vport->vf_info.link_state;
3582 vport->vf_info.link_state = link_state;
3583
3584 /* return success directly if the VF is unalive, VF will
3585 * query link state itself when it starts work.
3586 */
3587 if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
3588 return 0;
3589
3590 ret = hclge_push_vf_link_status(vport);
3591 if (ret) {
3592 vport->vf_info.link_state = link_state_old;
3593 dev_err(&hdev->pdev->dev,
3594 "failed to push vf%d link status, ret = %d\n", vf, ret);
3595 }
3596
3597 return ret;
3598 }
3599
3600 static void hclge_set_reset_pending(struct hclge_dev *hdev,
3601 enum hnae3_reset_type reset_type)
3602 {
3603 /* When an incorrect reset type is executed, the get_reset_level
3604 * function generates the HNAE3_NONE_RESET flag. As a result, this
3605 * type do not need to pending.
3606 */
3607 if (reset_type != HNAE3_NONE_RESET)
3608 set_bit(reset_type, &hdev->reset_pending);
3609 }
3610
3611 static u32 hclge_check_event_cause(struct hclge_dev *hdev, u32 *clearval)
3612 {
3613 u32 cmdq_src_reg, msix_src_reg, hw_err_src_reg;
3614
3615 /* fetch the events from their corresponding regs */
3616 cmdq_src_reg = hclge_read_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG);
3617 msix_src_reg = hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS);
3618 hw_err_src_reg = hclge_read_dev(&hdev->hw,
3619 HCLGE_RAS_PF_OTHER_INT_STS_REG);
3620
3621 /* Assumption: If by any chance reset and mailbox events are reported
3622 * together then we will only process reset event in this go and will
3623 * defer the processing of the mailbox events. Since, we would have not
3624 * cleared RX CMDQ event this time we would receive again another
3625 * interrupt from H/W just for the mailbox.
3626 *
3627 * check for vector0 reset event sources
3628 */
3629 if (BIT(HCLGE_VECTOR0_IMPRESET_INT_B) & msix_src_reg) {
3630 dev_info(&hdev->pdev->dev, "IMP reset interrupt\n");
3631 hclge_set_reset_pending(hdev, HNAE3_IMP_RESET);
3632 set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
3633 *clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
3634 hdev->rst_stats.imp_rst_cnt++;
3635 return HCLGE_VECTOR0_EVENT_RST;
3636 }
3637
3638 if (BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) & msix_src_reg) {
3639 dev_info(&hdev->pdev->dev, "global reset interrupt\n");
3640 set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
3641 hclge_set_reset_pending(hdev, HNAE3_GLOBAL_RESET);
3642 *clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
3643 hdev->rst_stats.global_rst_cnt++;
3644 return HCLGE_VECTOR0_EVENT_RST;
3645 }
3646
3647 /* check for vector0 msix event and hardware error event source */
3648 if (msix_src_reg & HCLGE_VECTOR0_REG_MSIX_MASK ||
3649 hw_err_src_reg & HCLGE_RAS_REG_ERR_MASK)
3650 return HCLGE_VECTOR0_EVENT_ERR;
3651
3652 /* check for vector0 ptp event source */
3653 if (BIT(HCLGE_VECTOR0_REG_PTP_INT_B) & msix_src_reg) {
3654 *clearval = msix_src_reg;
3655 return HCLGE_VECTOR0_EVENT_PTP;
3656 }
3657
3658 /* check for vector0 mailbox(=CMDQ RX) event source */
3659 if (BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_reg) {
3660 cmdq_src_reg &= ~BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B);
3661 *clearval = cmdq_src_reg;
3662 return HCLGE_VECTOR0_EVENT_MBX;
3663 }
3664
3665 /* print other vector0 event source */
3666 dev_info(&hdev->pdev->dev,
3667 "INT status: CMDQ(%#x) HW errors(%#x) other(%#x)\n",
3668 cmdq_src_reg, hw_err_src_reg, msix_src_reg);
3669
3670 return HCLGE_VECTOR0_EVENT_OTHER;
3671 }
3672
3673 static void hclge_clear_event_cause(struct hclge_dev *hdev, u32 event_type,
3674 u32 regclr)
3675 {
3676 #define HCLGE_IMP_RESET_DELAY 5
3677
3678 switch (event_type) {
3679 case HCLGE_VECTOR0_EVENT_PTP:
3680 case HCLGE_VECTOR0_EVENT_RST:
3681 if (regclr == BIT(HCLGE_VECTOR0_IMPRESET_INT_B))
3682 mdelay(HCLGE_IMP_RESET_DELAY);
3683
3684 hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG, regclr);
3685 break;
3686 case HCLGE_VECTOR0_EVENT_MBX:
3687 hclge_write_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG, regclr);
3688 break;
3689 default:
3690 break;
3691 }
3692 }
3693
3694 static void hclge_clear_all_event_cause(struct hclge_dev *hdev)
3695 {
3696 hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_RST,
3697 BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) |
3698 BIT(HCLGE_VECTOR0_CORERESET_INT_B) |
3699 BIT(HCLGE_VECTOR0_IMPRESET_INT_B));
3700 hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_MBX, 0);
3701 }
3702
3703 static void hclge_enable_vector(struct hclge_misc_vector *vector, bool enable)
3704 {
3705 writel(enable ? 1 : 0, vector->addr);
3706 }
3707
3708 static irqreturn_t hclge_misc_irq_handle(int irq, void *data)
3709 {
3710 struct hclge_dev *hdev = data;
3711 unsigned long flags;
3712 u32 clearval = 0;
3713 u32 event_cause;
3714
3715 hclge_enable_vector(&hdev->misc_vector, false);
3716 event_cause = hclge_check_event_cause(hdev, &clearval);
3717
3718 /* vector 0 interrupt is shared with reset and mailbox source events. */
3719 switch (event_cause) {
3720 case HCLGE_VECTOR0_EVENT_ERR:
3721 hclge_errhand_task_schedule(hdev);
3722 break;
3723 case HCLGE_VECTOR0_EVENT_RST:
3724 hclge_reset_task_schedule(hdev);
3725 break;
3726 case HCLGE_VECTOR0_EVENT_PTP:
3727 spin_lock_irqsave(&hdev->ptp->lock, flags);
3728 hclge_ptp_clean_tx_hwts(hdev);
3729 spin_unlock_irqrestore(&hdev->ptp->lock, flags);
3730 break;
3731 case HCLGE_VECTOR0_EVENT_MBX:
3732 /* If we are here then,
3733 * 1. Either we are not handling any mbx task and we are not
3734 * scheduled as well
3735 * OR
3736 * 2. We could be handling a mbx task but nothing more is
3737 * scheduled.
3738 * In both cases, we should schedule mbx task as there are more
3739 * mbx messages reported by this interrupt.
3740 */
3741 hclge_mbx_task_schedule(hdev);
3742 break;
3743 default:
3744 dev_warn(&hdev->pdev->dev,
3745 "received unknown or unhandled event of vector0\n");
3746 break;
3747 }
3748
3749 hclge_clear_event_cause(hdev, event_cause, clearval);
3750
3751 /* Enable interrupt if it is not caused by reset event or error event */
3752 if (event_cause == HCLGE_VECTOR0_EVENT_PTP ||
3753 event_cause == HCLGE_VECTOR0_EVENT_MBX ||
3754 event_cause == HCLGE_VECTOR0_EVENT_OTHER)
3755 hclge_enable_vector(&hdev->misc_vector, true);
3756
3757 return IRQ_HANDLED;
3758 }
3759
3760 static void hclge_free_vector(struct hclge_dev *hdev, int vector_id)
3761 {
3762 if (hdev->vector_status[vector_id] == HCLGE_INVALID_VPORT) {
3763 dev_warn(&hdev->pdev->dev,
3764 "vector(vector_id %d) has been freed.\n", vector_id);
3765 return;
3766 }
3767
3768 hdev->vector_status[vector_id] = HCLGE_INVALID_VPORT;
3769 hdev->num_msi_left += 1;
3770 hdev->num_msi_used -= 1;
3771 }
3772
3773 static void hclge_get_misc_vector(struct hclge_dev *hdev)
3774 {
3775 struct hclge_misc_vector *vector = &hdev->misc_vector;
3776
3777 vector->vector_irq = pci_irq_vector(hdev->pdev, 0);
3778
3779 vector->addr = hdev->hw.hw.io_base + HCLGE_MISC_VECTOR_REG_BASE;
3780 hdev->vector_status[0] = 0;
3781
3782 hdev->num_msi_left -= 1;
3783 hdev->num_msi_used += 1;
3784 }
3785
3786 static int hclge_misc_irq_init(struct hclge_dev *hdev)
3787 {
3788 int ret;
3789
3790 hclge_get_misc_vector(hdev);
3791
3792 /* this would be explicitly freed in the end */
3793 snprintf(hdev->misc_vector.name, HNAE3_INT_NAME_LEN, "%s-misc-%s",
3794 HCLGE_NAME, pci_name(hdev->pdev));
3795 ret = request_irq(hdev->misc_vector.vector_irq, hclge_misc_irq_handle,
3796 IRQF_NO_AUTOEN, hdev->misc_vector.name, hdev);
3797 if (ret) {
3798 hclge_free_vector(hdev, 0);
3799 dev_err(&hdev->pdev->dev, "request misc irq(%d) fail\n",
3800 hdev->misc_vector.vector_irq);
3801 }
3802
3803 return ret;
3804 }
3805
3806 static void hclge_misc_irq_uninit(struct hclge_dev *hdev)
3807 {
3808 free_irq(hdev->misc_vector.vector_irq, hdev);
3809 hclge_free_vector(hdev, 0);
3810 }
3811
3812 int hclge_notify_client(struct hclge_dev *hdev,
3813 enum hnae3_reset_notify_type type)
3814 {
3815 struct hnae3_handle *handle = &hdev->vport[0].nic;
3816 struct hnae3_client *client = hdev->nic_client;
3817 int ret;
3818
3819 if (!test_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state) || !client)
3820 return 0;
3821
3822 if (!client->ops->reset_notify)
3823 return -EOPNOTSUPP;
3824
3825 ret = client->ops->reset_notify(handle, type);
3826 if (ret)
3827 dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n",
3828 type, ret);
3829
3830 return ret;
3831 }
3832
3833 static int hclge_notify_roce_client(struct hclge_dev *hdev,
3834 enum hnae3_reset_notify_type type)
3835 {
3836 struct hnae3_handle *handle = &hdev->vport[0].roce;
3837 struct hnae3_client *client = hdev->roce_client;
3838 int ret;
3839
3840 if (!test_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state) || !client)
3841 return 0;
3842
3843 if (!client->ops->reset_notify)
3844 return -EOPNOTSUPP;
3845
3846 ret = client->ops->reset_notify(handle, type);
3847 if (ret)
3848 dev_err(&hdev->pdev->dev, "notify roce client failed %d(%d)",
3849 type, ret);
3850
3851 return ret;
3852 }
3853
3854 static int hclge_reset_wait(struct hclge_dev *hdev)
3855 {
3856 #define HCLGE_RESET_WATI_MS 100
3857 #define HCLGE_RESET_WAIT_CNT 350
3858
3859 u32 val, reg, reg_bit;
3860 u32 cnt = 0;
3861
3862 switch (hdev->reset_type) {
3863 case HNAE3_IMP_RESET:
3864 reg = HCLGE_GLOBAL_RESET_REG;
3865 reg_bit = HCLGE_IMP_RESET_BIT;
3866 break;
3867 case HNAE3_GLOBAL_RESET:
3868 reg = HCLGE_GLOBAL_RESET_REG;
3869 reg_bit = HCLGE_GLOBAL_RESET_BIT;
3870 break;
3871 case HNAE3_FUNC_RESET:
3872 reg = HCLGE_FUN_RST_ING;
3873 reg_bit = HCLGE_FUN_RST_ING_B;
3874 break;
3875 default:
3876 dev_err(&hdev->pdev->dev,
3877 "Wait for unsupported reset type: %d\n",
3878 hdev->reset_type);
3879 return -EINVAL;
3880 }
3881
3882 val = hclge_read_dev(&hdev->hw, reg);
3883 while (hnae3_get_bit(val, reg_bit) && cnt < HCLGE_RESET_WAIT_CNT) {
3884 msleep(HCLGE_RESET_WATI_MS);
3885 val = hclge_read_dev(&hdev->hw, reg);
3886 cnt++;
3887 }
3888
3889 if (cnt >= HCLGE_RESET_WAIT_CNT) {
3890 dev_warn(&hdev->pdev->dev,
3891 "Wait for reset timeout: %d\n", hdev->reset_type);
3892 return -EBUSY;
3893 }
3894
3895 return 0;
3896 }
3897
3898 static int hclge_set_vf_rst(struct hclge_dev *hdev, int func_id, bool reset)
3899 {
3900 struct hclge_vf_rst_cmd *req;
3901 struct hclge_desc desc;
3902
3903 req = (struct hclge_vf_rst_cmd *)desc.data;
3904 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GBL_RST_STATUS, false);
3905 req->dest_vfid = func_id;
3906
3907 if (reset)
3908 req->vf_rst = 0x1;
3909
3910 return hclge_cmd_send(&hdev->hw, &desc, 1);
3911 }
3912
3913 static int hclge_set_all_vf_rst(struct hclge_dev *hdev, bool reset)
3914 {
3915 int i;
3916
3917 for (i = HCLGE_VF_VPORT_START_NUM; i < hdev->num_alloc_vport; i++) {
3918 struct hclge_vport *vport = &hdev->vport[i];
3919 int ret;
3920
3921 /* Send cmd to set/clear VF's FUNC_RST_ING */
3922 ret = hclge_set_vf_rst(hdev, vport->vport_id, reset);
3923 if (ret) {
3924 dev_err(&hdev->pdev->dev,
3925 "set vf(%u) rst failed %d!\n",
3926 vport->vport_id - HCLGE_VF_VPORT_START_NUM,
3927 ret);
3928 return ret;
3929 }
3930
3931 if (!reset ||
3932 !test_bit(HCLGE_VPORT_STATE_INITED, &vport->state))
3933 continue;
3934
3935 if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state) &&
3936 hdev->reset_type == HNAE3_FUNC_RESET) {
3937 set_bit(HCLGE_VPORT_NEED_NOTIFY_RESET,
3938 &vport->need_notify);
3939 continue;
3940 }
3941
3942 /* Inform VF to process the reset.
3943 * hclge_inform_reset_assert_to_vf may fail if VF
3944 * driver is not loaded.
3945 */
3946 ret = hclge_inform_reset_assert_to_vf(vport);
3947 if (ret)
3948 dev_warn(&hdev->pdev->dev,
3949 "inform reset to vf(%u) failed %d!\n",
3950 vport->vport_id - HCLGE_VF_VPORT_START_NUM,
3951 ret);
3952 }
3953
3954 return 0;
3955 }
3956
3957 static void hclge_mailbox_service_task(struct hclge_dev *hdev)
3958 {
3959 if (!test_and_clear_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state) ||
3960 test_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state) ||
3961 test_and_set_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state))
3962 return;
3963
3964 if (time_is_before_jiffies(hdev->last_mbx_scheduled +
3965 HCLGE_MBX_SCHED_TIMEOUT))
3966 dev_warn(&hdev->pdev->dev,
3967 "mbx service task is scheduled after %ums on cpu%u!\n",
3968 jiffies_to_msecs(jiffies - hdev->last_mbx_scheduled),
3969 smp_processor_id());
3970
3971 hclge_mbx_handler(hdev);
3972
3973 clear_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state);
3974 }
3975
3976 static void hclge_func_reset_sync_vf(struct hclge_dev *hdev)
3977 {
3978 struct hclge_pf_rst_sync_cmd *req;
3979 struct hclge_desc desc;
3980 int cnt = 0;
3981 int ret;
3982
3983 req = (struct hclge_pf_rst_sync_cmd *)desc.data;
3984 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_VF_RST_RDY, true);
3985
3986 do {
3987 /* vf need to down netdev by mbx during PF or FLR reset */
3988 hclge_mailbox_service_task(hdev);
3989
3990 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3991 /* for compatible with old firmware, wait
3992 * 100 ms for VF to stop IO
3993 */
3994 if (ret == -EOPNOTSUPP) {
3995 msleep(HCLGE_RESET_SYNC_TIME);
3996 return;
3997 } else if (ret) {
3998 dev_warn(&hdev->pdev->dev, "sync with VF fail %d!\n",
3999 ret);
4000 return;
4001 } else if (req->all_vf_ready) {
4002 return;
4003 }
4004 msleep(HCLGE_PF_RESET_SYNC_TIME);
4005 hclge_comm_cmd_reuse_desc(&desc, true);
4006 } while (cnt++ < HCLGE_PF_RESET_SYNC_CNT);
4007
4008 dev_warn(&hdev->pdev->dev, "sync with VF timeout!\n");
4009 }
4010
4011 void hclge_report_hw_error(struct hclge_dev *hdev,
4012 enum hnae3_hw_error_type type)
4013 {
4014 struct hnae3_client *client = hdev->nic_client;
4015
4016 if (!client || !client->ops->process_hw_error ||
4017 !test_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state))
4018 return;
4019
4020 client->ops->process_hw_error(&hdev->vport[0].nic, type);
4021 }
4022
4023 static void hclge_handle_imp_error(struct hclge_dev *hdev)
4024 {
4025 u32 reg_val;
4026
4027 reg_val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4028 if (reg_val & BIT(HCLGE_VECTOR0_IMP_RD_POISON_B)) {
4029 hclge_report_hw_error(hdev, HNAE3_IMP_RD_POISON_ERROR);
4030 reg_val &= ~BIT(HCLGE_VECTOR0_IMP_RD_POISON_B);
4031 hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, reg_val);
4032 }
4033
4034 if (reg_val & BIT(HCLGE_VECTOR0_IMP_CMDQ_ERR_B)) {
4035 hclge_report_hw_error(hdev, HNAE3_CMDQ_ECC_ERROR);
4036 reg_val &= ~BIT(HCLGE_VECTOR0_IMP_CMDQ_ERR_B);
4037 hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, reg_val);
4038 }
4039 }
4040
4041 int hclge_func_reset_cmd(struct hclge_dev *hdev, int func_id)
4042 {
4043 struct hclge_desc desc;
4044 struct hclge_reset_cmd *req = (struct hclge_reset_cmd *)desc.data;
4045 int ret;
4046
4047 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_RST_TRIGGER, false);
4048 hnae3_set_bit(req->mac_func_reset, HCLGE_CFG_RESET_FUNC_B, 1);
4049 req->fun_reset_vfid = func_id;
4050
4051 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
4052 if (ret)
4053 dev_err(&hdev->pdev->dev,
4054 "send function reset cmd fail, status =%d\n", ret);
4055
4056 return ret;
4057 }
4058
4059 static void hclge_do_reset(struct hclge_dev *hdev)
4060 {
4061 struct hnae3_handle *handle = &hdev->vport[0].nic;
4062 struct pci_dev *pdev = hdev->pdev;
4063 u32 val;
4064
4065 if (hclge_get_hw_reset_stat(handle)) {
4066 dev_info(&pdev->dev, "hardware reset not finish\n");
4067 dev_info(&pdev->dev, "func_rst_reg:0x%x, global_rst_reg:0x%x\n",
4068 hclge_read_dev(&hdev->hw, HCLGE_FUN_RST_ING),
4069 hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG));
4070 return;
4071 }
4072
4073 switch (hdev->reset_type) {
4074 case HNAE3_IMP_RESET:
4075 dev_info(&pdev->dev, "IMP reset requested\n");
4076 val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4077 hnae3_set_bit(val, HCLGE_TRIGGER_IMP_RESET_B, 1);
4078 hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, val);
4079 break;
4080 case HNAE3_GLOBAL_RESET:
4081 dev_info(&pdev->dev, "global reset requested\n");
4082 val = hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG);
4083 hnae3_set_bit(val, HCLGE_GLOBAL_RESET_BIT, 1);
4084 hclge_write_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG, val);
4085 break;
4086 case HNAE3_FUNC_RESET:
4087 dev_info(&pdev->dev, "PF reset requested\n");
4088 /* schedule again to check later */
4089 hclge_set_reset_pending(hdev, HNAE3_FUNC_RESET);
4090 hclge_reset_task_schedule(hdev);
4091 break;
4092 default:
4093 dev_warn(&pdev->dev,
4094 "unsupported reset type: %d\n", hdev->reset_type);
4095 break;
4096 }
4097 }
4098
4099 static enum hnae3_reset_type hclge_get_reset_level(struct hnae3_ae_dev *ae_dev,
4100 unsigned long *addr)
4101 {
4102 enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
4103 struct hclge_dev *hdev = ae_dev->priv;
4104
4105 /* return the highest priority reset level amongst all */
4106 if (test_bit(HNAE3_IMP_RESET, addr)) {
4107 rst_level = HNAE3_IMP_RESET;
4108 clear_bit(HNAE3_IMP_RESET, addr);
4109 clear_bit(HNAE3_GLOBAL_RESET, addr);
4110 clear_bit(HNAE3_FUNC_RESET, addr);
4111 } else if (test_bit(HNAE3_GLOBAL_RESET, addr)) {
4112 rst_level = HNAE3_GLOBAL_RESET;
4113 clear_bit(HNAE3_GLOBAL_RESET, addr);
4114 clear_bit(HNAE3_FUNC_RESET, addr);
4115 } else if (test_bit(HNAE3_FUNC_RESET, addr)) {
4116 rst_level = HNAE3_FUNC_RESET;
4117 clear_bit(HNAE3_FUNC_RESET, addr);
4118 } else if (test_bit(HNAE3_FLR_RESET, addr)) {
4119 rst_level = HNAE3_FLR_RESET;
4120 clear_bit(HNAE3_FLR_RESET, addr);
4121 }
4122
4123 clear_bit(HNAE3_NONE_RESET, addr);
4124
4125 if (hdev->reset_type != HNAE3_NONE_RESET &&
4126 rst_level < hdev->reset_type)
4127 return HNAE3_NONE_RESET;
4128
4129 return rst_level;
4130 }
4131
4132 static void hclge_clear_reset_cause(struct hclge_dev *hdev)
4133 {
4134 u32 clearval = 0;
4135
4136 switch (hdev->reset_type) {
4137 case HNAE3_IMP_RESET:
4138 clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
4139 break;
4140 case HNAE3_GLOBAL_RESET:
4141 clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
4142 break;
4143 default:
4144 break;
4145 }
4146
4147 if (!clearval)
4148 return;
4149
4150 /* For revision 0x20, the reset interrupt source
4151 * can only be cleared after hardware reset done
4152 */
4153 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
4154 hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG,
4155 clearval);
4156
4157 hclge_enable_vector(&hdev->misc_vector, true);
4158 }
4159
4160 static void hclge_reset_handshake(struct hclge_dev *hdev, bool enable)
4161 {
4162 u32 reg_val;
4163
4164 reg_val = hclge_read_dev(&hdev->hw, HCLGE_COMM_NIC_CSQ_DEPTH_REG);
4165 if (enable)
4166 reg_val |= HCLGE_COMM_NIC_SW_RST_RDY;
4167 else
4168 reg_val &= ~HCLGE_COMM_NIC_SW_RST_RDY;
4169
4170 hclge_write_dev(&hdev->hw, HCLGE_COMM_NIC_CSQ_DEPTH_REG, reg_val);
4171 }
4172
4173 static int hclge_func_reset_notify_vf(struct hclge_dev *hdev)
4174 {
4175 int ret;
4176
4177 ret = hclge_set_all_vf_rst(hdev, true);
4178 if (ret)
4179 return ret;
4180
4181 hclge_func_reset_sync_vf(hdev);
4182
4183 return 0;
4184 }
4185
4186 static int hclge_reset_prepare_wait(struct hclge_dev *hdev)
4187 {
4188 u32 reg_val;
4189 int ret = 0;
4190
4191 switch (hdev->reset_type) {
4192 case HNAE3_FUNC_RESET:
4193 ret = hclge_func_reset_notify_vf(hdev);
4194 if (ret)
4195 return ret;
4196
4197 ret = hclge_func_reset_cmd(hdev, 0);
4198 if (ret) {
4199 dev_err(&hdev->pdev->dev,
4200 "asserting function reset fail %d!\n", ret);
4201 return ret;
4202 }
4203
4204 /* After performaning pf reset, it is not necessary to do the
4205 * mailbox handling or send any command to firmware, because
4206 * any mailbox handling or command to firmware is only valid
4207 * after hclge_comm_cmd_init is called.
4208 */
4209 set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
4210 hdev->rst_stats.pf_rst_cnt++;
4211 break;
4212 case HNAE3_FLR_RESET:
4213 ret = hclge_func_reset_notify_vf(hdev);
4214 if (ret)
4215 return ret;
4216 break;
4217 case HNAE3_IMP_RESET:
4218 hclge_handle_imp_error(hdev);
4219 reg_val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4220 hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG,
4221 BIT(HCLGE_VECTOR0_IMP_RESET_INT_B) | reg_val);
4222 break;
4223 default:
4224 break;
4225 }
4226
4227 /* inform hardware that preparatory work is done */
4228 msleep(HCLGE_RESET_SYNC_TIME);
4229 hclge_reset_handshake(hdev, true);
4230 dev_info(&hdev->pdev->dev, "prepare wait ok\n");
4231
4232 return ret;
4233 }
4234
4235 static void hclge_show_rst_info(struct hclge_dev *hdev)
4236 {
4237 char *buf;
4238
4239 buf = kzalloc(HCLGE_DBG_RESET_INFO_LEN, GFP_KERNEL);
4240 if (!buf)
4241 return;
4242
4243 hclge_dbg_dump_rst_info(hdev, buf, HCLGE_DBG_RESET_INFO_LEN);
4244
4245 dev_info(&hdev->pdev->dev, "dump reset info:\n%s", buf);
4246
4247 kfree(buf);
4248 }
4249
4250 static bool hclge_reset_err_handle(struct hclge_dev *hdev)
4251 {
4252 #define MAX_RESET_FAIL_CNT 5
4253
4254 if (hdev->reset_pending) {
4255 dev_info(&hdev->pdev->dev, "Reset pending %lu\n",
4256 hdev->reset_pending);
4257 return true;
4258 } else if (hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS) &
4259 HCLGE_RESET_INT_M) {
4260 dev_info(&hdev->pdev->dev,
4261 "reset failed because new reset interrupt\n");
4262 hclge_clear_reset_cause(hdev);
4263 return false;
4264 } else if (hdev->rst_stats.reset_fail_cnt < MAX_RESET_FAIL_CNT) {
4265 hdev->rst_stats.reset_fail_cnt++;
4266 hclge_set_reset_pending(hdev, hdev->reset_type);
4267 dev_info(&hdev->pdev->dev,
4268 "re-schedule reset task(%u)\n",
4269 hdev->rst_stats.reset_fail_cnt);
4270 return true;
4271 }
4272
4273 hclge_clear_reset_cause(hdev);
4274
4275 /* recover the handshake status when reset fail */
4276 hclge_reset_handshake(hdev, true);
4277
4278 dev_err(&hdev->pdev->dev, "Reset fail!\n");
4279
4280 hclge_show_rst_info(hdev);
4281
4282 set_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
4283
4284 return false;
4285 }
4286
4287 static void hclge_update_reset_level(struct hclge_dev *hdev)
4288 {
4289 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4290 enum hnae3_reset_type reset_level;
4291
4292 /* reset request will not be set during reset, so clear
4293 * pending reset request to avoid unnecessary reset
4294 * caused by the same reason.
4295 */
4296 hclge_get_reset_level(ae_dev, &hdev->reset_request);
4297
4298 /* if default_reset_request has a higher level reset request,
4299 * it should be handled as soon as possible. since some errors
4300 * need this kind of reset to fix.
4301 */
4302 reset_level = hclge_get_reset_level(ae_dev,
4303 &hdev->default_reset_request);
4304 if (reset_level != HNAE3_NONE_RESET)
4305 set_bit(reset_level, &hdev->reset_request);
4306 }
4307
4308 static int hclge_set_rst_done(struct hclge_dev *hdev)
4309 {
4310 struct hclge_pf_rst_done_cmd *req;
4311 struct hclge_desc desc;
4312 int ret;
4313
4314 req = (struct hclge_pf_rst_done_cmd *)desc.data;
4315 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_PF_RST_DONE, false);
4316 req->pf_rst_done |= HCLGE_PF_RESET_DONE_BIT;
4317
4318 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
4319 /* To be compatible with the old firmware, which does not support
4320 * command HCLGE_OPC_PF_RST_DONE, just print a warning and
4321 * return success
4322 */
4323 if (ret == -EOPNOTSUPP) {
4324 dev_warn(&hdev->pdev->dev,
4325 "current firmware does not support command(0x%x)!\n",
4326 HCLGE_OPC_PF_RST_DONE);
4327 return 0;
4328 } else if (ret) {
4329 dev_err(&hdev->pdev->dev, "assert PF reset done fail %d!\n",
4330 ret);
4331 }
4332
4333 return ret;
4334 }
4335
4336 static int hclge_reset_prepare_up(struct hclge_dev *hdev)
4337 {
4338 int ret = 0;
4339
4340 switch (hdev->reset_type) {
4341 case HNAE3_FUNC_RESET:
4342 case HNAE3_FLR_RESET:
4343 ret = hclge_set_all_vf_rst(hdev, false);
4344 break;
4345 case HNAE3_GLOBAL_RESET:
4346 case HNAE3_IMP_RESET:
4347 ret = hclge_set_rst_done(hdev);
4348 break;
4349 default:
4350 break;
4351 }
4352
4353 /* clear up the handshake status after re-initialize done */
4354 hclge_reset_handshake(hdev, false);
4355
4356 return ret;
4357 }
4358
4359 static int hclge_reset_stack(struct hclge_dev *hdev)
4360 {
4361 int ret;
4362
4363 ret = hclge_notify_client(hdev, HNAE3_UNINIT_CLIENT);
4364 if (ret)
4365 return ret;
4366
4367 ret = hclge_reset_ae_dev(hdev->ae_dev);
4368 if (ret)
4369 return ret;
4370
4371 return hclge_notify_client(hdev, HNAE3_INIT_CLIENT);
4372 }
4373
4374 static int hclge_reset_prepare(struct hclge_dev *hdev)
4375 {
4376 int ret;
4377
4378 hdev->rst_stats.reset_cnt++;
4379 /* perform reset of the stack & ae device for a client */
4380 ret = hclge_notify_roce_client(hdev, HNAE3_DOWN_CLIENT);
4381 if (ret)
4382 return ret;
4383
4384 rtnl_lock();
4385 ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
4386 rtnl_unlock();
4387 if (ret)
4388 return ret;
4389
4390 return hclge_reset_prepare_wait(hdev);
4391 }
4392
4393 static int hclge_reset_rebuild(struct hclge_dev *hdev)
4394 {
4395 int ret;
4396
4397 hdev->rst_stats.hw_reset_done_cnt++;
4398
4399 ret = hclge_notify_roce_client(hdev, HNAE3_UNINIT_CLIENT);
4400 if (ret)
4401 return ret;
4402
4403 rtnl_lock();
4404 ret = hclge_reset_stack(hdev);
4405 rtnl_unlock();
4406 if (ret)
4407 return ret;
4408
4409 hclge_clear_reset_cause(hdev);
4410
4411 ret = hclge_notify_roce_client(hdev, HNAE3_INIT_CLIENT);
4412 /* ignore RoCE notify error if it fails HCLGE_RESET_MAX_FAIL_CNT - 1
4413 * times
4414 */
4415 if (ret &&
4416 hdev->rst_stats.reset_fail_cnt < HCLGE_RESET_MAX_FAIL_CNT - 1)
4417 return ret;
4418
4419 ret = hclge_reset_prepare_up(hdev);
4420 if (ret)
4421 return ret;
4422
4423 rtnl_lock();
4424 ret = hclge_notify_client(hdev, HNAE3_UP_CLIENT);
4425 rtnl_unlock();
4426 if (ret)
4427 return ret;
4428
4429 ret = hclge_notify_roce_client(hdev, HNAE3_UP_CLIENT);
4430 if (ret)
4431 return ret;
4432
4433 hdev->last_reset_time = jiffies;
4434 hdev->rst_stats.reset_fail_cnt = 0;
4435 hdev->rst_stats.reset_done_cnt++;
4436 clear_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
4437
4438 hclge_update_reset_level(hdev);
4439
4440 return 0;
4441 }
4442
4443 static void hclge_reset(struct hclge_dev *hdev)
4444 {
4445 if (hclge_reset_prepare(hdev))
4446 goto err_reset;
4447
4448 if (hclge_reset_wait(hdev))
4449 goto err_reset;
4450
4451 if (hclge_reset_rebuild(hdev))
4452 goto err_reset;
4453
4454 return;
4455
4456 err_reset:
4457 if (hclge_reset_err_handle(hdev))
4458 hclge_reset_task_schedule(hdev);
4459 }
4460
4461 static void hclge_reset_event(struct pci_dev *pdev, struct hnae3_handle *handle)
4462 {
4463 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
4464 struct hclge_dev *hdev = ae_dev->priv;
4465
4466 /* We might end up getting called broadly because of 2 below cases:
4467 * 1. Recoverable error was conveyed through APEI and only way to bring
4468 * normalcy is to reset.
4469 * 2. A new reset request from the stack due to timeout
4470 *
4471 * check if this is a new reset request and we are not here just because
4472 * last reset attempt did not succeed and watchdog hit us again. We will
4473 * know this if last reset request did not occur very recently (watchdog
4474 * timer = 5*HZ, let us check after sufficiently large time, say 4*5*Hz)
4475 * In case of new request we reset the "reset level" to PF reset.
4476 * And if it is a repeat reset request of the most recent one then we
4477 * want to make sure we throttle the reset request. Therefore, we will
4478 * not allow it again before 3*HZ times.
4479 */
4480
4481 if (time_before(jiffies, (hdev->last_reset_time +
4482 HCLGE_RESET_INTERVAL))) {
4483 mod_timer(&hdev->reset_timer, jiffies + HCLGE_RESET_INTERVAL);
4484 return;
4485 }
4486
4487 if (hdev->default_reset_request) {
4488 hdev->reset_level =
4489 hclge_get_reset_level(ae_dev,
4490 &hdev->default_reset_request);
4491 } else if (time_after(jiffies, (hdev->last_reset_time + 4 * 5 * HZ))) {
4492 hdev->reset_level = HNAE3_FUNC_RESET;
4493 }
4494
4495 dev_info(&hdev->pdev->dev, "received reset event, reset type is %d\n",
4496 hdev->reset_level);
4497
4498 /* request reset & schedule reset task */
4499 set_bit(hdev->reset_level, &hdev->reset_request);
4500 hclge_reset_task_schedule(hdev);
4501
4502 if (hdev->reset_level < HNAE3_GLOBAL_RESET)
4503 hdev->reset_level++;
4504 }
4505
4506 static void hclge_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
4507 enum hnae3_reset_type rst_type)
4508 {
4509 #define HCLGE_SUPPORT_RESET_TYPE \
4510 (BIT(HNAE3_FLR_RESET) | BIT(HNAE3_FUNC_RESET) | \
4511 BIT(HNAE3_GLOBAL_RESET) | BIT(HNAE3_IMP_RESET))
4512
4513 struct hclge_dev *hdev = ae_dev->priv;
4514
4515 if (!(BIT(rst_type) & HCLGE_SUPPORT_RESET_TYPE)) {
4516 /* To prevent reset triggered by hclge_reset_event */
4517 set_bit(HNAE3_NONE_RESET, &hdev->default_reset_request);
4518 dev_warn(&hdev->pdev->dev, "unsupported reset type %d\n",
4519 rst_type);
4520 return;
4521 }
4522
4523 set_bit(rst_type, &hdev->default_reset_request);
4524 }
4525
4526 static void hclge_reset_timer(struct timer_list *t)
4527 {
4528 struct hclge_dev *hdev = timer_container_of(hdev, t, reset_timer);
4529
4530 /* if default_reset_request has no value, it means that this reset
4531 * request has already be handled, so just return here
4532 */
4533 if (!hdev->default_reset_request)
4534 return;
4535
4536 dev_info(&hdev->pdev->dev,
4537 "triggering reset in reset timer\n");
4538 hclge_reset_event(hdev->pdev, NULL);
4539 }
4540
4541 static void hclge_reset_subtask(struct hclge_dev *hdev)
4542 {
4543 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4544
4545 /* check if there is any ongoing reset in the hardware. This status can
4546 * be checked from reset_pending. If there is then, we need to wait for
4547 * hardware to complete reset.
4548 * a. If we are able to figure out in reasonable time that hardware
4549 * has fully resetted then, we can proceed with driver, client
4550 * reset.
4551 * b. else, we can come back later to check this status so re-sched
4552 * now.
4553 */
4554 hdev->last_reset_time = jiffies;
4555 hdev->reset_type = hclge_get_reset_level(ae_dev, &hdev->reset_pending);
4556 if (hdev->reset_type != HNAE3_NONE_RESET)
4557 hclge_reset(hdev);
4558
4559 /* check if we got any *new* reset requests to be honored */
4560 hdev->reset_type = hclge_get_reset_level(ae_dev, &hdev->reset_request);
4561 if (hdev->reset_type != HNAE3_NONE_RESET)
4562 hclge_do_reset(hdev);
4563
4564 hdev->reset_type = HNAE3_NONE_RESET;
4565 }
4566
4567 static void hclge_handle_err_reset_request(struct hclge_dev *hdev)
4568 {
4569 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4570 enum hnae3_reset_type reset_type;
4571
4572 if (ae_dev->hw_err_reset_req) {
4573 reset_type = hclge_get_reset_level(ae_dev,
4574 &ae_dev->hw_err_reset_req);
4575 hclge_set_def_reset_request(ae_dev, reset_type);
4576 }
4577
4578 if (hdev->default_reset_request && ae_dev->ops->reset_event)
4579 ae_dev->ops->reset_event(hdev->pdev, NULL);
4580
4581 /* enable interrupt after error handling complete */
4582 hclge_enable_vector(&hdev->misc_vector, true);
4583 }
4584
4585 static void hclge_handle_err_recovery(struct hclge_dev *hdev)
4586 {
4587 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4588
4589 ae_dev->hw_err_reset_req = 0;
4590
4591 if (hclge_find_error_source(hdev)) {
4592 hclge_handle_error_info_log(ae_dev);
4593 hclge_handle_mac_tnl(hdev);
4594 hclge_handle_vf_queue_err_ras(hdev);
4595 }
4596
4597 hclge_handle_err_reset_request(hdev);
4598 }
4599
4600 static void hclge_misc_err_recovery(struct hclge_dev *hdev)
4601 {
4602 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4603 struct device *dev = &hdev->pdev->dev;
4604 u32 msix_sts_reg;
4605
4606 msix_sts_reg = hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS);
4607 if (msix_sts_reg & HCLGE_VECTOR0_REG_MSIX_MASK) {
4608 if (hclge_handle_hw_msix_error
4609 (hdev, &hdev->default_reset_request))
4610 dev_info(dev, "received msix interrupt 0x%x\n",
4611 msix_sts_reg);
4612 }
4613
4614 hclge_handle_hw_ras_error(ae_dev);
4615
4616 hclge_handle_err_reset_request(hdev);
4617 }
4618
4619 static void hclge_errhand_service_task(struct hclge_dev *hdev)
4620 {
4621 if (!test_and_clear_bit(HCLGE_STATE_ERR_SERVICE_SCHED, &hdev->state))
4622 return;
4623
4624 if (hnae3_dev_ras_imp_supported(hdev))
4625 hclge_handle_err_recovery(hdev);
4626 else
4627 hclge_misc_err_recovery(hdev);
4628 }
4629
4630 static void hclge_reset_service_task(struct hclge_dev *hdev)
4631 {
4632 if (!test_and_clear_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state))
4633 return;
4634
4635 if (time_is_before_jiffies(hdev->last_rst_scheduled +
4636 HCLGE_RESET_SCHED_TIMEOUT))
4637 dev_warn(&hdev->pdev->dev,
4638 "reset service task is scheduled after %ums on cpu%u!\n",
4639 jiffies_to_msecs(jiffies - hdev->last_rst_scheduled),
4640 smp_processor_id());
4641
4642 down(&hdev->reset_sem);
4643 set_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
4644
4645 hclge_reset_subtask(hdev);
4646
4647 clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
4648 up(&hdev->reset_sem);
4649 }
4650
4651 static void hclge_update_vport_alive(struct hclge_dev *hdev)
4652 {
4653 #define HCLGE_ALIVE_SECONDS_NORMAL 8
4654
4655 unsigned long alive_time = HCLGE_ALIVE_SECONDS_NORMAL * HZ;
4656 int i;
4657
4658 /* start from vport 1 for PF is always alive */
4659 for (i = 1; i < hdev->num_alloc_vport; i++) {
4660 struct hclge_vport *vport = &hdev->vport[i];
4661
4662 if (!test_bit(HCLGE_VPORT_STATE_INITED, &vport->state) ||
4663 !test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
4664 continue;
4665 if (time_after(jiffies, vport->last_active_jiffies +
4666 alive_time)) {
4667 clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
4668 dev_warn(&hdev->pdev->dev,
4669 "VF %u heartbeat timeout\n",
4670 i - HCLGE_VF_VPORT_START_NUM);
4671 }
4672 }
4673 }
4674
4675 static void hclge_periodic_service_task(struct hclge_dev *hdev)
4676 {
4677 unsigned long delta = round_jiffies_relative(HZ);
4678
4679 if (test_bit(HCLGE_STATE_RST_FAIL, &hdev->state))
4680 return;
4681
4682 /* Always handle the link updating to make sure link state is
4683 * updated when it is triggered by mbx.
4684 */
4685 hclge_update_link_status(hdev);
4686 hclge_sync_mac_table(hdev);
4687 hclge_sync_promisc_mode(hdev);
4688 hclge_sync_fd_table(hdev);
4689
4690 if (time_is_after_jiffies(hdev->last_serv_processed + HZ)) {
4691 delta = jiffies - hdev->last_serv_processed;
4692
4693 if (delta < round_jiffies_relative(HZ)) {
4694 delta = round_jiffies_relative(HZ) - delta;
4695 goto out;
4696 }
4697 }
4698
4699 hdev->serv_processed_cnt++;
4700 hclge_update_vport_alive(hdev);
4701
4702 if (test_bit(HCLGE_STATE_DOWN, &hdev->state)) {
4703 hdev->last_serv_processed = jiffies;
4704 goto out;
4705 }
4706
4707 if (!(hdev->serv_processed_cnt % HCLGE_STATS_TIMER_INTERVAL))
4708 hclge_update_stats_for_all(hdev);
4709
4710 hclge_update_port_info(hdev);
4711 hclge_sync_vlan_filter(hdev);
4712
4713 if (!(hdev->serv_processed_cnt % HCLGE_ARFS_EXPIRE_INTERVAL))
4714 hclge_rfs_filter_expire(hdev);
4715
4716 hdev->last_serv_processed = jiffies;
4717
4718 out:
4719 hclge_task_schedule(hdev, delta);
4720 }
4721
4722 static void hclge_ptp_service_task(struct hclge_dev *hdev)
4723 {
4724 unsigned long flags;
4725
4726 if (!test_bit(HCLGE_STATE_PTP_EN, &hdev->state) ||
4727 !test_bit(HCLGE_STATE_PTP_TX_HANDLING, &hdev->state) ||
4728 !time_is_before_jiffies(hdev->ptp->tx_start + HZ))
4729 return;
4730
4731 /* to prevent concurrence with the irq handler */
4732 spin_lock_irqsave(&hdev->ptp->lock, flags);
4733
4734 /* check HCLGE_STATE_PTP_TX_HANDLING here again, since the irq
4735 * handler may handle it just before spin_lock_irqsave().
4736 */
4737 if (test_bit(HCLGE_STATE_PTP_TX_HANDLING, &hdev->state))
4738 hclge_ptp_clean_tx_hwts(hdev);
4739
4740 spin_unlock_irqrestore(&hdev->ptp->lock, flags);
4741 }
4742
4743 static void hclge_service_task(struct work_struct *work)
4744 {
4745 struct hclge_dev *hdev =
4746 container_of(work, struct hclge_dev, service_task.work);
4747
4748 hclge_errhand_service_task(hdev);
4749 hclge_reset_service_task(hdev);
4750 hclge_ptp_service_task(hdev);
4751 hclge_mailbox_service_task(hdev);
4752 hclge_periodic_service_task(hdev);
4753
4754 /* Handle error recovery, reset and mbx again in case periodical task
4755 * delays the handling by calling hclge_task_schedule() in
4756 * hclge_periodic_service_task().
4757 */
4758 hclge_errhand_service_task(hdev);
4759 hclge_reset_service_task(hdev);
4760 hclge_mailbox_service_task(hdev);
4761 }
4762
4763 struct hclge_vport *hclge_get_vport(struct hnae3_handle *handle)
4764 {
4765 /* VF handle has no client */
4766 if (!handle->client)
4767 return container_of(handle, struct hclge_vport, nic);
4768 else if (handle->client->type == HNAE3_CLIENT_ROCE)
4769 return container_of(handle, struct hclge_vport, roce);
4770 else
4771 return container_of(handle, struct hclge_vport, nic);
4772 }
4773
4774 static void hclge_get_vector_info(struct hclge_dev *hdev, u16 idx,
4775 struct hnae3_vector_info *vector_info)
4776 {
4777 #define HCLGE_PF_MAX_VECTOR_NUM_DEV_V2 64
4778
4779 vector_info->vector = pci_irq_vector(hdev->pdev, idx);
4780
4781 /* need an extend offset to config vector >= 64 */
4782 if (idx - 1 < HCLGE_PF_MAX_VECTOR_NUM_DEV_V2)
4783 vector_info->io_addr = hdev->hw.hw.io_base +
4784 HCLGE_VECTOR_REG_BASE +
4785 (idx - 1) * HCLGE_VECTOR_REG_OFFSET;
4786 else
4787 vector_info->io_addr = hdev->hw.hw.io_base +
4788 HCLGE_VECTOR_EXT_REG_BASE +
4789 (idx - 1) / HCLGE_PF_MAX_VECTOR_NUM_DEV_V2 *
4790 HCLGE_VECTOR_REG_OFFSET_H +
4791 (idx - 1) % HCLGE_PF_MAX_VECTOR_NUM_DEV_V2 *
4792 HCLGE_VECTOR_REG_OFFSET;
4793
4794 hdev->vector_status[idx] = hdev->vport[0].vport_id;
4795 hdev->vector_irq[idx] = vector_info->vector;
4796 }
4797
4798 static int hclge_get_vector(struct hnae3_handle *handle, u16 vector_num,
4799 struct hnae3_vector_info *vector_info)
4800 {
4801 struct hclge_vport *vport = hclge_get_vport(handle);
4802 struct hnae3_vector_info *vector = vector_info;
4803 struct hclge_dev *hdev = vport->back;
4804 int alloc = 0;
4805 u16 i = 0;
4806 u16 j;
4807
4808 vector_num = min_t(u16, hdev->num_nic_msi - 1, vector_num);
4809 vector_num = min(hdev->num_msi_left, vector_num);
4810
4811 for (j = 0; j < vector_num; j++) {
4812 while (++i < hdev->num_nic_msi) {
4813 if (hdev->vector_status[i] == HCLGE_INVALID_VPORT) {
4814 hclge_get_vector_info(hdev, i, vector);
4815 vector++;
4816 alloc++;
4817
4818 break;
4819 }
4820 }
4821 }
4822 hdev->num_msi_left -= alloc;
4823 hdev->num_msi_used += alloc;
4824
4825 return alloc;
4826 }
4827
4828 static int hclge_get_vector_index(struct hclge_dev *hdev, int vector)
4829 {
4830 int i;
4831
4832 for (i = 0; i < hdev->num_msi; i++)
4833 if (vector == hdev->vector_irq[i])
4834 return i;
4835
4836 return -EINVAL;
4837 }
4838
4839 static int hclge_put_vector(struct hnae3_handle *handle, int vector)
4840 {
4841 struct hclge_vport *vport = hclge_get_vport(handle);
4842 struct hclge_dev *hdev = vport->back;
4843 int vector_id;
4844
4845 vector_id = hclge_get_vector_index(hdev, vector);
4846 if (vector_id < 0) {
4847 dev_err(&hdev->pdev->dev,
4848 "Get vector index fail. vector = %d\n", vector);
4849 return vector_id;
4850 }
4851
4852 hclge_free_vector(hdev, vector_id);
4853
4854 return 0;
4855 }
4856
4857 static int hclge_get_rss(struct hnae3_handle *handle, u32 *indir,
4858 u8 *key, u8 *hfunc)
4859 {
4860 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
4861 struct hclge_vport *vport = hclge_get_vport(handle);
4862 struct hclge_comm_rss_cfg *rss_cfg = &vport->back->rss_cfg;
4863
4864 hclge_comm_get_rss_hash_info(rss_cfg, key, hfunc);
4865
4866 hclge_comm_get_rss_indir_tbl(rss_cfg, indir,
4867 ae_dev->dev_specs.rss_ind_tbl_size);
4868
4869 return 0;
4870 }
4871
4872 static int hclge_set_rss(struct hnae3_handle *handle, const u32 *indir,
4873 const u8 *key, const u8 hfunc)
4874 {
4875 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
4876 struct hclge_vport *vport = hclge_get_vport(handle);
4877 struct hclge_dev *hdev = vport->back;
4878 struct hclge_comm_rss_cfg *rss_cfg = &hdev->rss_cfg;
4879 int ret, i;
4880
4881 ret = hclge_comm_set_rss_hash_key(rss_cfg, &hdev->hw.hw, key, hfunc);
4882 if (ret) {
4883 dev_err(&hdev->pdev->dev, "invalid hfunc type %u\n", hfunc);
4884 return ret;
4885 }
4886
4887 /* Update the shadow RSS table with user specified qids */
4888 for (i = 0; i < ae_dev->dev_specs.rss_ind_tbl_size; i++)
4889 rss_cfg->rss_indirection_tbl[i] = indir[i];
4890
4891 /* Update the hardware */
4892 return hclge_comm_set_rss_indir_table(ae_dev, &hdev->hw.hw,
4893 rss_cfg->rss_indirection_tbl);
4894 }
4895
4896 static int hclge_set_rss_tuple(struct hnae3_handle *handle,
4897 const struct ethtool_rxfh_fields *nfc)
4898 {
4899 struct hclge_vport *vport = hclge_get_vport(handle);
4900 struct hclge_dev *hdev = vport->back;
4901 int ret;
4902
4903 ret = hclge_comm_set_rss_tuple(hdev->ae_dev, &hdev->hw.hw,
4904 &hdev->rss_cfg, nfc);
4905 if (ret) {
4906 dev_err(&hdev->pdev->dev,
4907 "failed to set rss tuple, ret = %d.\n", ret);
4908 return ret;
4909 }
4910
4911 return 0;
4912 }
4913
4914 static int hclge_get_rss_tuple(struct hnae3_handle *handle,
4915 struct ethtool_rxfh_fields *nfc)
4916 {
4917 struct hclge_vport *vport = hclge_get_vport(handle);
4918 u8 tuple_sets;
4919 int ret;
4920
4921 nfc->data = 0;
4922
4923 ret = hclge_comm_get_rss_tuple(&vport->back->rss_cfg, nfc->flow_type,
4924 &tuple_sets);
4925 if (ret || !tuple_sets)
4926 return ret;
4927
4928 nfc->data = hclge_comm_convert_rss_tuple(tuple_sets);
4929
4930 return 0;
4931 }
4932
4933 static int hclge_get_tc_size(struct hnae3_handle *handle)
4934 {
4935 struct hclge_vport *vport = hclge_get_vport(handle);
4936 struct hclge_dev *hdev = vport->back;
4937
4938 return hdev->pf_rss_size_max;
4939 }
4940
4941 static int hclge_init_rss_tc_mode(struct hclge_dev *hdev)
4942 {
4943 struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
4944 struct hclge_vport *vport = hdev->vport;
4945 u16 tc_offset[HCLGE_MAX_TC_NUM] = {0};
4946 u16 tc_valid[HCLGE_MAX_TC_NUM] = {0};
4947 u16 tc_size[HCLGE_MAX_TC_NUM] = {0};
4948 struct hnae3_tc_info *tc_info;
4949 u16 roundup_size;
4950 u16 rss_size;
4951 int i;
4952
4953 tc_info = &vport->nic.kinfo.tc_info;
4954 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
4955 rss_size = tc_info->tqp_count[i];
4956 tc_valid[i] = 0;
4957
4958 if (!(hdev->hw_tc_map & BIT(i)))
4959 continue;
4960
4961 /* tc_size set to hardware is the log2 of roundup power of two
4962 * of rss_size, the acutal queue size is limited by indirection
4963 * table.
4964 */
4965 if (rss_size > ae_dev->dev_specs.rss_ind_tbl_size ||
4966 rss_size == 0) {
4967 dev_err(&hdev->pdev->dev,
4968 "Configure rss tc size failed, invalid TC_SIZE = %u\n",
4969 rss_size);
4970 return -EINVAL;
4971 }
4972
4973 roundup_size = roundup_pow_of_two(rss_size);
4974 roundup_size = ilog2(roundup_size);
4975
4976 tc_valid[i] = 1;
4977 tc_size[i] = roundup_size;
4978 tc_offset[i] = tc_info->tqp_offset[i];
4979 }
4980
4981 return hclge_comm_set_rss_tc_mode(&hdev->hw.hw, tc_offset, tc_valid,
4982 tc_size);
4983 }
4984
4985 int hclge_rss_init_hw(struct hclge_dev *hdev)
4986 {
4987 u16 *rss_indir = hdev->rss_cfg.rss_indirection_tbl;
4988 u8 *key = hdev->rss_cfg.rss_hash_key;
4989 u8 hfunc = hdev->rss_cfg.rss_algo;
4990 int ret;
4991
4992 ret = hclge_comm_set_rss_indir_table(hdev->ae_dev, &hdev->hw.hw,
4993 rss_indir);
4994 if (ret)
4995 return ret;
4996
4997 ret = hclge_comm_set_rss_algo_key(&hdev->hw.hw, hfunc, key);
4998 if (ret)
4999 return ret;
5000
5001 ret = hclge_comm_set_rss_input_tuple(&hdev->hw.hw, &hdev->rss_cfg);
5002 if (ret)
5003 return ret;
5004
5005 return hclge_init_rss_tc_mode(hdev);
5006 }
5007
5008 int hclge_bind_ring_with_vector(struct hclge_vport *vport,
5009 int vector_id, bool en,
5010 struct hnae3_ring_chain_node *ring_chain)
5011 {
5012 struct hclge_dev *hdev = vport->back;
5013 struct hnae3_ring_chain_node *node;
5014 struct hclge_desc desc;
5015 struct hclge_ctrl_vector_chain_cmd *req =
5016 (struct hclge_ctrl_vector_chain_cmd *)desc.data;
5017 enum hclge_comm_cmd_status status;
5018 enum hclge_opcode_type op;
5019 u16 tqp_type_and_id;
5020 int i;
5021
5022 op = en ? HCLGE_OPC_ADD_RING_TO_VECTOR : HCLGE_OPC_DEL_RING_TO_VECTOR;
5023 hclge_cmd_setup_basic_desc(&desc, op, false);
5024 req->int_vector_id_l = hnae3_get_field(vector_id,
5025 HCLGE_VECTOR_ID_L_M,
5026 HCLGE_VECTOR_ID_L_S);
5027 req->int_vector_id_h = hnae3_get_field(vector_id,
5028 HCLGE_VECTOR_ID_H_M,
5029 HCLGE_VECTOR_ID_H_S);
5030
5031 i = 0;
5032 for (node = ring_chain; node; node = node->next) {
5033 tqp_type_and_id = le16_to_cpu(req->tqp_type_and_id[i]);
5034 hnae3_set_field(tqp_type_and_id, HCLGE_INT_TYPE_M,
5035 HCLGE_INT_TYPE_S,
5036 hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B));
5037 hnae3_set_field(tqp_type_and_id, HCLGE_TQP_ID_M,
5038 HCLGE_TQP_ID_S, node->tqp_index);
5039 hnae3_set_field(tqp_type_and_id, HCLGE_INT_GL_IDX_M,
5040 HCLGE_INT_GL_IDX_S,
5041 hnae3_get_field(node->int_gl_idx,
5042 HNAE3_RING_GL_IDX_M,
5043 HNAE3_RING_GL_IDX_S));
5044 req->tqp_type_and_id[i] = cpu_to_le16(tqp_type_and_id);
5045 if (++i >= HCLGE_VECTOR_ELEMENTS_PER_CMD) {
5046 req->int_cause_num = HCLGE_VECTOR_ELEMENTS_PER_CMD;
5047 req->vfid = vport->vport_id;
5048
5049 status = hclge_cmd_send(&hdev->hw, &desc, 1);
5050 if (status) {
5051 dev_err(&hdev->pdev->dev,
5052 "Map TQP fail, status is %d.\n",
5053 status);
5054 return -EIO;
5055 }
5056 i = 0;
5057
5058 hclge_cmd_setup_basic_desc(&desc,
5059 op,
5060 false);
5061 req->int_vector_id_l =
5062 hnae3_get_field(vector_id,
5063 HCLGE_VECTOR_ID_L_M,
5064 HCLGE_VECTOR_ID_L_S);
5065 req->int_vector_id_h =
5066 hnae3_get_field(vector_id,
5067 HCLGE_VECTOR_ID_H_M,
5068 HCLGE_VECTOR_ID_H_S);
5069 }
5070 }
5071
5072 if (i > 0) {
5073 req->int_cause_num = i;
5074 req->vfid = vport->vport_id;
5075 status = hclge_cmd_send(&hdev->hw, &desc, 1);
5076 if (status) {
5077 dev_err(&hdev->pdev->dev,
5078 "Map TQP fail, status is %d.\n", status);
5079 return -EIO;
5080 }
5081 }
5082
5083 return 0;
5084 }
5085
5086 static int hclge_map_ring_to_vector(struct hnae3_handle *handle, int vector,
5087 struct hnae3_ring_chain_node *ring_chain)
5088 {
5089 struct hclge_vport *vport = hclge_get_vport(handle);
5090 struct hclge_dev *hdev = vport->back;
5091 int vector_id;
5092
5093 vector_id = hclge_get_vector_index(hdev, vector);
5094 if (vector_id < 0) {
5095 dev_err(&hdev->pdev->dev,
5096 "failed to get vector index. vector=%d\n", vector);
5097 return vector_id;
5098 }
5099
5100 return hclge_bind_ring_with_vector(vport, vector_id, true, ring_chain);
5101 }
5102
5103 static int hclge_unmap_ring_frm_vector(struct hnae3_handle *handle, int vector,
5104 struct hnae3_ring_chain_node *ring_chain)
5105 {
5106 struct hclge_vport *vport = hclge_get_vport(handle);
5107 struct hclge_dev *hdev = vport->back;
5108 int vector_id, ret;
5109
5110 if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
5111 return 0;
5112
5113 vector_id = hclge_get_vector_index(hdev, vector);
5114 if (vector_id < 0) {
5115 dev_err(&handle->pdev->dev,
5116 "Get vector index fail. ret =%d\n", vector_id);
5117 return vector_id;
5118 }
5119
5120 ret = hclge_bind_ring_with_vector(vport, vector_id, false, ring_chain);
5121 if (ret)
5122 dev_err(&handle->pdev->dev,
5123 "Unmap ring from vector fail. vectorid=%d, ret =%d\n",
5124 vector_id, ret);
5125
5126 return ret;
5127 }
5128
5129 static int hclge_cmd_set_promisc_mode(struct hclge_dev *hdev, u8 vf_id,
5130 bool en_uc, bool en_mc, bool en_bc)
5131 {
5132 struct hclge_vport *vport = &hdev->vport[vf_id];
5133 struct hnae3_handle *handle = &vport->nic;
5134 struct hclge_promisc_cfg_cmd *req;
5135 struct hclge_desc desc;
5136 bool uc_tx_en = en_uc;
5137 u8 promisc_cfg = 0;
5138 int ret;
5139
5140 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_PROMISC_MODE, false);
5141
5142 req = (struct hclge_promisc_cfg_cmd *)desc.data;
5143 req->vf_id = vf_id;
5144
5145 if (test_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags))
5146 uc_tx_en = false;
5147
5148 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_UC_RX_EN, en_uc ? 1 : 0);
5149 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_MC_RX_EN, en_mc ? 1 : 0);
5150 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_BC_RX_EN, en_bc ? 1 : 0);
5151 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_UC_TX_EN, uc_tx_en ? 1 : 0);
5152 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_MC_TX_EN, en_mc ? 1 : 0);
5153 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_BC_TX_EN, en_bc ? 1 : 0);
5154 req->extend_promisc = promisc_cfg;
5155
5156 /* to be compatible with DEVICE_VERSION_V1/2 */
5157 promisc_cfg = 0;
5158 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_UC, en_uc ? 1 : 0);
5159 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_MC, en_mc ? 1 : 0);
5160 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_BC, en_bc ? 1 : 0);
5161 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_TX_EN, 1);
5162 hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_RX_EN, 1);
5163 req->promisc = promisc_cfg;
5164
5165 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5166 if (ret)
5167 dev_err(&hdev->pdev->dev,
5168 "failed to set vport %u promisc mode, ret = %d.\n",
5169 vf_id, ret);
5170
5171 return ret;
5172 }
5173
5174 int hclge_set_vport_promisc_mode(struct hclge_vport *vport, bool en_uc_pmc,
5175 bool en_mc_pmc, bool en_bc_pmc)
5176 {
5177 return hclge_cmd_set_promisc_mode(vport->back, vport->vport_id,
5178 en_uc_pmc, en_mc_pmc, en_bc_pmc);
5179 }
5180
5181 static int hclge_set_promisc_mode(struct hnae3_handle *handle, bool en_uc_pmc,
5182 bool en_mc_pmc)
5183 {
5184 struct hclge_vport *vport = hclge_get_vport(handle);
5185 struct hclge_dev *hdev = vport->back;
5186 bool en_bc_pmc = true;
5187
5188 /* For device whose version below V2, if broadcast promisc enabled,
5189 * vlan filter is always bypassed. So broadcast promisc should be
5190 * disabled until user enable promisc mode
5191 */
5192 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
5193 en_bc_pmc = handle->netdev_flags & HNAE3_BPE ? true : false;
5194
5195 return hclge_set_vport_promisc_mode(vport, en_uc_pmc, en_mc_pmc,
5196 en_bc_pmc);
5197 }
5198
5199 static void hclge_request_update_promisc_mode(struct hnae3_handle *handle)
5200 {
5201 struct hclge_vport *vport = hclge_get_vport(handle);
5202
5203 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
5204 }
5205
5206 static void hclge_sync_fd_state(struct hclge_dev *hdev)
5207 {
5208 if (hlist_empty(&hdev->fd_rule_list))
5209 hdev->fd_active_type = HCLGE_FD_RULE_NONE;
5210 }
5211
5212 static void hclge_fd_inc_rule_cnt(struct hclge_dev *hdev, u16 location)
5213 {
5214 if (!test_bit(location, hdev->fd_bmap)) {
5215 set_bit(location, hdev->fd_bmap);
5216 hdev->hclge_fd_rule_num++;
5217 }
5218 }
5219
5220 static void hclge_fd_dec_rule_cnt(struct hclge_dev *hdev, u16 location)
5221 {
5222 if (test_bit(location, hdev->fd_bmap)) {
5223 clear_bit(location, hdev->fd_bmap);
5224 hdev->hclge_fd_rule_num--;
5225 }
5226 }
5227
5228 static void hclge_fd_free_node(struct hclge_dev *hdev,
5229 struct hclge_fd_rule *rule)
5230 {
5231 hlist_del(&rule->rule_node);
5232 kfree(rule);
5233 hclge_sync_fd_state(hdev);
5234 }
5235
5236 static void hclge_update_fd_rule_node(struct hclge_dev *hdev,
5237 struct hclge_fd_rule *old_rule,
5238 struct hclge_fd_rule *new_rule,
5239 enum HCLGE_FD_NODE_STATE state)
5240 {
5241 switch (state) {
5242 case HCLGE_FD_TO_ADD:
5243 case HCLGE_FD_ACTIVE:
5244 /* 1) if the new state is TO_ADD, just replace the old rule
5245 * with the same location, no matter its state, because the
5246 * new rule will be configured to the hardware.
5247 * 2) if the new state is ACTIVE, it means the new rule
5248 * has been configured to the hardware, so just replace
5249 * the old rule node with the same location.
5250 * 3) for it doesn't add a new node to the list, so it's
5251 * unnecessary to update the rule number and fd_bmap.
5252 */
5253 new_rule->rule_node.next = old_rule->rule_node.next;
5254 new_rule->rule_node.pprev = old_rule->rule_node.pprev;
5255 memcpy(old_rule, new_rule, sizeof(*old_rule));
5256 kfree(new_rule);
5257 break;
5258 case HCLGE_FD_DELETED:
5259 hclge_fd_dec_rule_cnt(hdev, old_rule->location);
5260 hclge_fd_free_node(hdev, old_rule);
5261 break;
5262 case HCLGE_FD_TO_DEL:
5263 /* if new request is TO_DEL, and old rule is existent
5264 * 1) the state of old rule is TO_DEL, we need do nothing,
5265 * because we delete rule by location, other rule content
5266 * is unncessary.
5267 * 2) the state of old rule is ACTIVE, we need to change its
5268 * state to TO_DEL, so the rule will be deleted when periodic
5269 * task being scheduled.
5270 * 3) the state of old rule is TO_ADD, it means the rule hasn't
5271 * been added to hardware, so we just delete the rule node from
5272 * fd_rule_list directly.
5273 */
5274 if (old_rule->state == HCLGE_FD_TO_ADD) {
5275 hclge_fd_dec_rule_cnt(hdev, old_rule->location);
5276 hclge_fd_free_node(hdev, old_rule);
5277 return;
5278 }
5279 old_rule->state = HCLGE_FD_TO_DEL;
5280 break;
5281 }
5282 }
5283
5284 static struct hclge_fd_rule *hclge_find_fd_rule(struct hlist_head *hlist,
5285 u16 location,
5286 struct hclge_fd_rule **parent)
5287 {
5288 struct hclge_fd_rule *rule;
5289 struct hlist_node *node;
5290
5291 hlist_for_each_entry_safe(rule, node, hlist, rule_node) {
5292 if (rule->location == location)
5293 return rule;
5294 else if (rule->location > location)
5295 return NULL;
5296 /* record the parent node, use to keep the nodes in fd_rule_list
5297 * in ascend order.
5298 */
5299 *parent = rule;
5300 }
5301
5302 return NULL;
5303 }
5304
5305 /* insert fd rule node in ascend order according to rule->location */
5306 static void hclge_fd_insert_rule_node(struct hlist_head *hlist,
5307 struct hclge_fd_rule *rule,
5308 struct hclge_fd_rule *parent)
5309 {
5310 INIT_HLIST_NODE(&rule->rule_node);
5311
5312 if (parent)
5313 hlist_add_behind(&rule->rule_node, &parent->rule_node);
5314 else
5315 hlist_add_head(&rule->rule_node, hlist);
5316 }
5317
5318 static int hclge_fd_set_user_def_cmd(struct hclge_dev *hdev,
5319 struct hclge_fd_user_def_cfg *cfg)
5320 {
5321 struct hclge_fd_user_def_cfg_cmd *req;
5322 struct hclge_desc desc;
5323 u16 data = 0;
5324 int ret;
5325
5326 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_FD_USER_DEF_OP, false);
5327
5328 req = (struct hclge_fd_user_def_cfg_cmd *)desc.data;
5329
5330 hnae3_set_bit(data, HCLGE_FD_USER_DEF_EN_B, cfg[0].ref_cnt > 0);
5331 hnae3_set_field(data, HCLGE_FD_USER_DEF_OFT_M,
5332 HCLGE_FD_USER_DEF_OFT_S, cfg[0].offset);
5333 req->ol2_cfg = cpu_to_le16(data);
5334
5335 data = 0;
5336 hnae3_set_bit(data, HCLGE_FD_USER_DEF_EN_B, cfg[1].ref_cnt > 0);
5337 hnae3_set_field(data, HCLGE_FD_USER_DEF_OFT_M,
5338 HCLGE_FD_USER_DEF_OFT_S, cfg[1].offset);
5339 req->ol3_cfg = cpu_to_le16(data);
5340
5341 data = 0;
5342 hnae3_set_bit(data, HCLGE_FD_USER_DEF_EN_B, cfg[2].ref_cnt > 0);
5343 hnae3_set_field(data, HCLGE_FD_USER_DEF_OFT_M,
5344 HCLGE_FD_USER_DEF_OFT_S, cfg[2].offset);
5345 req->ol4_cfg = cpu_to_le16(data);
5346
5347 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5348 if (ret)
5349 dev_err(&hdev->pdev->dev,
5350 "failed to set fd user def data, ret= %d\n", ret);
5351 return ret;
5352 }
5353
5354 static void hclge_sync_fd_user_def_cfg(struct hclge_dev *hdev, bool locked)
5355 {
5356 int ret;
5357
5358 if (!test_and_clear_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state))
5359 return;
5360
5361 if (!locked)
5362 spin_lock_bh(&hdev->fd_rule_lock);
5363
5364 ret = hclge_fd_set_user_def_cmd(hdev, hdev->fd_cfg.user_def_cfg);
5365 if (ret)
5366 set_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state);
5367
5368 if (!locked)
5369 spin_unlock_bh(&hdev->fd_rule_lock);
5370 }
5371
5372 static int hclge_fd_check_user_def_refcnt(struct hclge_dev *hdev,
5373 struct hclge_fd_rule *rule)
5374 {
5375 struct hlist_head *hlist = &hdev->fd_rule_list;
5376 struct hclge_fd_rule *fd_rule, *parent = NULL;
5377 struct hclge_fd_user_def_info *info, *old_info;
5378 struct hclge_fd_user_def_cfg *cfg;
5379
5380 if (!rule || rule->rule_type != HCLGE_FD_EP_ACTIVE ||
5381 rule->ep.user_def.layer == HCLGE_FD_USER_DEF_NONE)
5382 return 0;
5383
5384 /* for valid layer is start from 1, so need minus 1 to get the cfg */
5385 cfg = &hdev->fd_cfg.user_def_cfg[rule->ep.user_def.layer - 1];
5386 info = &rule->ep.user_def;
5387
5388 if (!cfg->ref_cnt || cfg->offset == info->offset)
5389 return 0;
5390
5391 if (cfg->ref_cnt > 1)
5392 goto error;
5393
5394 fd_rule = hclge_find_fd_rule(hlist, rule->location, &parent);
5395 if (fd_rule) {
5396 old_info = &fd_rule->ep.user_def;
5397 if (info->layer == old_info->layer)
5398 return 0;
5399 }
5400
5401 error:
5402 dev_err(&hdev->pdev->dev,
5403 "No available offset for layer%d fd rule, each layer only support one user def offset.\n",
5404 info->layer + 1);
5405 return -ENOSPC;
5406 }
5407
5408 static void hclge_fd_inc_user_def_refcnt(struct hclge_dev *hdev,
5409 struct hclge_fd_rule *rule)
5410 {
5411 struct hclge_fd_user_def_cfg *cfg;
5412
5413 if (!rule || rule->rule_type != HCLGE_FD_EP_ACTIVE ||
5414 rule->ep.user_def.layer == HCLGE_FD_USER_DEF_NONE)
5415 return;
5416
5417 cfg = &hdev->fd_cfg.user_def_cfg[rule->ep.user_def.layer - 1];
5418 if (!cfg->ref_cnt) {
5419 cfg->offset = rule->ep.user_def.offset;
5420 set_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state);
5421 }
5422 cfg->ref_cnt++;
5423 }
5424
5425 static void hclge_fd_dec_user_def_refcnt(struct hclge_dev *hdev,
5426 struct hclge_fd_rule *rule)
5427 {
5428 struct hclge_fd_user_def_cfg *cfg;
5429
5430 if (!rule || rule->rule_type != HCLGE_FD_EP_ACTIVE ||
5431 rule->ep.user_def.layer == HCLGE_FD_USER_DEF_NONE)
5432 return;
5433
5434 cfg = &hdev->fd_cfg.user_def_cfg[rule->ep.user_def.layer - 1];
5435 if (!cfg->ref_cnt)
5436 return;
5437
5438 cfg->ref_cnt--;
5439 if (!cfg->ref_cnt) {
5440 cfg->offset = 0;
5441 set_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state);
5442 }
5443 }
5444
5445 static void hclge_update_fd_list(struct hclge_dev *hdev,
5446 enum HCLGE_FD_NODE_STATE state, u16 location,
5447 struct hclge_fd_rule *new_rule)
5448 {
5449 struct hlist_head *hlist = &hdev->fd_rule_list;
5450 struct hclge_fd_rule *fd_rule, *parent = NULL;
5451
5452 fd_rule = hclge_find_fd_rule(hlist, location, &parent);
5453 if (fd_rule) {
5454 hclge_fd_dec_user_def_refcnt(hdev, fd_rule);
5455 if (state == HCLGE_FD_ACTIVE)
5456 hclge_fd_inc_user_def_refcnt(hdev, new_rule);
5457 hclge_sync_fd_user_def_cfg(hdev, true);
5458
5459 hclge_update_fd_rule_node(hdev, fd_rule, new_rule, state);
5460 return;
5461 }
5462
5463 /* it's unlikely to fail here, because we have checked the rule
5464 * exist before.
5465 */
5466 if (unlikely(state == HCLGE_FD_TO_DEL || state == HCLGE_FD_DELETED)) {
5467 dev_warn(&hdev->pdev->dev,
5468 "failed to delete fd rule %u, it's inexistent\n",
5469 location);
5470 return;
5471 }
5472
5473 hclge_fd_inc_user_def_refcnt(hdev, new_rule);
5474 hclge_sync_fd_user_def_cfg(hdev, true);
5475
5476 hclge_fd_insert_rule_node(hlist, new_rule, parent);
5477 hclge_fd_inc_rule_cnt(hdev, new_rule->location);
5478
5479 if (state == HCLGE_FD_TO_ADD) {
5480 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
5481 hclge_task_schedule(hdev, 0);
5482 }
5483 }
5484
5485 static int hclge_get_fd_mode(struct hclge_dev *hdev, u8 *fd_mode)
5486 {
5487 struct hclge_get_fd_mode_cmd *req;
5488 struct hclge_desc desc;
5489 int ret;
5490
5491 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_FD_MODE_CTRL, true);
5492
5493 req = (struct hclge_get_fd_mode_cmd *)desc.data;
5494
5495 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5496 if (ret) {
5497 dev_err(&hdev->pdev->dev, "get fd mode fail, ret=%d\n", ret);
5498 return ret;
5499 }
5500
5501 *fd_mode = req->mode;
5502
5503 return ret;
5504 }
5505
5506 static int hclge_get_fd_allocation(struct hclge_dev *hdev,
5507 u32 *stage1_entry_num,
5508 u32 *stage2_entry_num,
5509 u16 *stage1_counter_num,
5510 u16 *stage2_counter_num)
5511 {
5512 struct hclge_get_fd_allocation_cmd *req;
5513 struct hclge_desc desc;
5514 int ret;
5515
5516 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_FD_GET_ALLOCATION, true);
5517
5518 req = (struct hclge_get_fd_allocation_cmd *)desc.data;
5519
5520 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5521 if (ret) {
5522 dev_err(&hdev->pdev->dev, "query fd allocation fail, ret=%d\n",
5523 ret);
5524 return ret;
5525 }
5526
5527 *stage1_entry_num = le32_to_cpu(req->stage1_entry_num);
5528 *stage2_entry_num = le32_to_cpu(req->stage2_entry_num);
5529 *stage1_counter_num = le16_to_cpu(req->stage1_counter_num);
5530 *stage2_counter_num = le16_to_cpu(req->stage2_counter_num);
5531
5532 return ret;
5533 }
5534
5535 static int hclge_set_fd_key_config(struct hclge_dev *hdev,
5536 enum HCLGE_FD_STAGE stage_num)
5537 {
5538 struct hclge_set_fd_key_config_cmd *req;
5539 struct hclge_fd_key_cfg *stage;
5540 struct hclge_desc desc;
5541 int ret;
5542
5543 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_FD_KEY_CONFIG, false);
5544
5545 req = (struct hclge_set_fd_key_config_cmd *)desc.data;
5546 stage = &hdev->fd_cfg.key_cfg[stage_num];
5547 req->stage = stage_num;
5548 req->key_select = stage->key_sel;
5549 req->inner_sipv6_word_en = stage->inner_sipv6_word_en;
5550 req->inner_dipv6_word_en = stage->inner_dipv6_word_en;
5551 req->outer_sipv6_word_en = stage->outer_sipv6_word_en;
5552 req->outer_dipv6_word_en = stage->outer_dipv6_word_en;
5553 req->tuple_mask = cpu_to_le32(~stage->tuple_active);
5554 req->meta_data_mask = cpu_to_le32(~stage->meta_data_active);
5555
5556 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5557 if (ret)
5558 dev_err(&hdev->pdev->dev, "set fd key fail, ret=%d\n", ret);
5559
5560 return ret;
5561 }
5562
5563 static void hclge_fd_disable_user_def(struct hclge_dev *hdev)
5564 {
5565 struct hclge_fd_user_def_cfg *cfg = hdev->fd_cfg.user_def_cfg;
5566
5567 spin_lock_bh(&hdev->fd_rule_lock);
5568 memset(cfg, 0, sizeof(hdev->fd_cfg.user_def_cfg));
5569 spin_unlock_bh(&hdev->fd_rule_lock);
5570
5571 hclge_fd_set_user_def_cmd(hdev, cfg);
5572 }
5573
5574 static int hclge_init_fd_config(struct hclge_dev *hdev)
5575 {
5576 #define LOW_2_WORDS 0x03
5577 struct hclge_fd_key_cfg *key_cfg;
5578 int ret;
5579
5580 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
5581 return 0;
5582
5583 ret = hclge_get_fd_mode(hdev, &hdev->fd_cfg.fd_mode);
5584 if (ret)
5585 return ret;
5586
5587 switch (hdev->fd_cfg.fd_mode) {
5588 case HCLGE_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
5589 hdev->fd_cfg.max_key_length = MAX_KEY_LENGTH;
5590 break;
5591 case HCLGE_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
5592 hdev->fd_cfg.max_key_length = MAX_KEY_LENGTH / 2;
5593 break;
5594 default:
5595 dev_err(&hdev->pdev->dev,
5596 "Unsupported flow director mode %u\n",
5597 hdev->fd_cfg.fd_mode);
5598 return -EOPNOTSUPP;
5599 }
5600
5601 key_cfg = &hdev->fd_cfg.key_cfg[HCLGE_FD_STAGE_1];
5602 key_cfg->key_sel = HCLGE_FD_KEY_BASE_ON_TUPLE;
5603 key_cfg->inner_sipv6_word_en = LOW_2_WORDS;
5604 key_cfg->inner_dipv6_word_en = LOW_2_WORDS;
5605 key_cfg->outer_sipv6_word_en = 0;
5606 key_cfg->outer_dipv6_word_en = 0;
5607
5608 key_cfg->tuple_active = BIT(INNER_VLAN_TAG_FST) | BIT(INNER_ETH_TYPE) |
5609 BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
5610 BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
5611 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
5612
5613 /* If use max 400bit key, we can support tuples for ether type */
5614 if (hdev->fd_cfg.fd_mode == HCLGE_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1) {
5615 key_cfg->tuple_active |=
5616 BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC);
5617 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
5618 key_cfg->tuple_active |= HCLGE_FD_TUPLE_USER_DEF_TUPLES;
5619 }
5620
5621 /* roce_type is used to filter roce frames
5622 * dst_vport is used to specify the rule
5623 */
5624 key_cfg->meta_data_active = BIT(ROCE_TYPE) | BIT(DST_VPORT);
5625
5626 ret = hclge_get_fd_allocation(hdev,
5627 &hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1],
5628 &hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_2],
5629 &hdev->fd_cfg.cnt_num[HCLGE_FD_STAGE_1],
5630 &hdev->fd_cfg.cnt_num[HCLGE_FD_STAGE_2]);
5631 if (ret)
5632 return ret;
5633
5634 return hclge_set_fd_key_config(hdev, HCLGE_FD_STAGE_1);
5635 }
5636
5637 static int hclge_fd_tcam_config(struct hclge_dev *hdev, u8 stage, bool sel_x,
5638 int loc, u8 *key, bool is_add)
5639 {
5640 struct hclge_fd_tcam_config_1_cmd *req1;
5641 struct hclge_fd_tcam_config_2_cmd *req2;
5642 struct hclge_fd_tcam_config_3_cmd *req3;
5643 struct hclge_desc desc[3];
5644 int ret;
5645
5646 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_FD_TCAM_OP, false);
5647 desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
5648 hclge_cmd_setup_basic_desc(&desc[1], HCLGE_OPC_FD_TCAM_OP, false);
5649 desc[1].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
5650 hclge_cmd_setup_basic_desc(&desc[2], HCLGE_OPC_FD_TCAM_OP, false);
5651
5652 req1 = (struct hclge_fd_tcam_config_1_cmd *)desc[0].data;
5653 req2 = (struct hclge_fd_tcam_config_2_cmd *)desc[1].data;
5654 req3 = (struct hclge_fd_tcam_config_3_cmd *)desc[2].data;
5655
5656 req1->stage = stage;
5657 req1->xy_sel = sel_x ? 1 : 0;
5658 hnae3_set_bit(req1->port_info, HCLGE_FD_EPORT_SW_EN_B, 0);
5659 req1->index = cpu_to_le32(loc);
5660 req1->entry_vld = sel_x ? is_add : 0;
5661
5662 if (key) {
5663 memcpy(req1->tcam_data, &key[0], sizeof(req1->tcam_data));
5664 memcpy(req2->tcam_data, &key[sizeof(req1->tcam_data)],
5665 sizeof(req2->tcam_data));
5666 memcpy(req3->tcam_data, &key[sizeof(req1->tcam_data) +
5667 sizeof(req2->tcam_data)], sizeof(req3->tcam_data));
5668 }
5669
5670 ret = hclge_cmd_send(&hdev->hw, desc, 3);
5671 if (ret)
5672 dev_err(&hdev->pdev->dev,
5673 "config tcam key fail, ret=%d\n",
5674 ret);
5675
5676 return ret;
5677 }
5678
5679 static int hclge_fd_ad_config(struct hclge_dev *hdev, u8 stage, int loc,
5680 struct hclge_fd_ad_data *action)
5681 {
5682 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
5683 struct hclge_fd_ad_config_cmd *req;
5684 struct hclge_desc desc;
5685 u64 ad_data = 0;
5686 int ret;
5687
5688 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_FD_AD_OP, false);
5689
5690 req = (struct hclge_fd_ad_config_cmd *)desc.data;
5691 req->index = cpu_to_le32(loc);
5692 req->stage = stage;
5693
5694 hnae3_set_bit(ad_data, HCLGE_FD_AD_WR_RULE_ID_B,
5695 action->write_rule_id_to_bd);
5696 hnae3_set_field(ad_data, HCLGE_FD_AD_RULE_ID_M, HCLGE_FD_AD_RULE_ID_S,
5697 action->rule_id);
5698 if (test_bit(HNAE3_DEV_SUPPORT_FD_FORWARD_TC_B, ae_dev->caps)) {
5699 hnae3_set_bit(ad_data, HCLGE_FD_AD_TC_OVRD_B,
5700 action->override_tc);
5701 hnae3_set_field(ad_data, HCLGE_FD_AD_TC_SIZE_M,
5702 HCLGE_FD_AD_TC_SIZE_S, (u32)action->tc_size);
5703 }
5704 hnae3_set_bit(ad_data, HCLGE_FD_AD_QID_H_B,
5705 action->queue_id >= HCLGE_TQP_MAX_SIZE_DEV_V2 ? 1 : 0);
5706 hnae3_set_bit(ad_data, HCLGE_FD_AD_COUNTER_NUM_H_B,
5707 action->counter_id >= HCLGE_FD_COUNTER_MAX_SIZE_DEV_V2 ?
5708 1 : 0);
5709 ad_data <<= 32;
5710 hnae3_set_bit(ad_data, HCLGE_FD_AD_DROP_B, action->drop_packet);
5711 hnae3_set_bit(ad_data, HCLGE_FD_AD_DIRECT_QID_B,
5712 action->forward_to_direct_queue);
5713 hnae3_set_field(ad_data, HCLGE_FD_AD_QID_L_M, HCLGE_FD_AD_QID_L_S,
5714 action->queue_id);
5715 hnae3_set_bit(ad_data, HCLGE_FD_AD_USE_COUNTER_B, action->use_counter);
5716 hnae3_set_field(ad_data, HCLGE_FD_AD_COUNTER_NUM_L_M,
5717 HCLGE_FD_AD_COUNTER_NUM_L_S, action->counter_id);
5718 hnae3_set_bit(ad_data, HCLGE_FD_AD_NXT_STEP_B, action->use_next_stage);
5719 hnae3_set_field(ad_data, HCLGE_FD_AD_NXT_KEY_M, HCLGE_FD_AD_NXT_KEY_S,
5720 action->next_input_key);
5721
5722 req->ad_data = cpu_to_le64(ad_data);
5723 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5724 if (ret)
5725 dev_err(&hdev->pdev->dev, "fd ad config fail, ret=%d\n", ret);
5726
5727 return ret;
5728 }
5729
5730 static bool hclge_fd_convert_tuple(u32 tuple_bit, u8 *key_x, u8 *key_y,
5731 struct hclge_fd_rule *rule)
5732 {
5733 int offset, moffset, ip_offset;
5734 enum HCLGE_FD_KEY_OPT key_opt;
5735 u16 tmp_x_s, tmp_y_s;
5736 u32 tmp_x_l, tmp_y_l;
5737 u8 *p = (u8 *)rule;
5738 int i;
5739
5740 if (rule->unused_tuple & BIT(tuple_bit))
5741 return true;
5742
5743 key_opt = tuple_key_info[tuple_bit].key_opt;
5744 offset = tuple_key_info[tuple_bit].offset;
5745 moffset = tuple_key_info[tuple_bit].moffset;
5746
5747 switch (key_opt) {
5748 case KEY_OPT_U8:
5749 calc_x(*key_x, p[offset], p[moffset]);
5750 calc_y(*key_y, p[offset], p[moffset]);
5751
5752 return true;
5753 case KEY_OPT_LE16:
5754 calc_x(tmp_x_s, *(u16 *)(&p[offset]), *(u16 *)(&p[moffset]));
5755 calc_y(tmp_y_s, *(u16 *)(&p[offset]), *(u16 *)(&p[moffset]));
5756 *(__le16 *)key_x = cpu_to_le16(tmp_x_s);
5757 *(__le16 *)key_y = cpu_to_le16(tmp_y_s);
5758
5759 return true;
5760 case KEY_OPT_LE32:
5761 calc_x(tmp_x_l, *(u32 *)(&p[offset]), *(u32 *)(&p[moffset]));
5762 calc_y(tmp_y_l, *(u32 *)(&p[offset]), *(u32 *)(&p[moffset]));
5763 *(__le32 *)key_x = cpu_to_le32(tmp_x_l);
5764 *(__le32 *)key_y = cpu_to_le32(tmp_y_l);
5765
5766 return true;
5767 case KEY_OPT_MAC:
5768 for (i = 0; i < ETH_ALEN; i++) {
5769 calc_x(key_x[ETH_ALEN - 1 - i], p[offset + i],
5770 p[moffset + i]);
5771 calc_y(key_y[ETH_ALEN - 1 - i], p[offset + i],
5772 p[moffset + i]);
5773 }
5774
5775 return true;
5776 case KEY_OPT_IP:
5777 ip_offset = IPV4_INDEX * sizeof(u32);
5778 calc_x(tmp_x_l, *(u32 *)(&p[offset + ip_offset]),
5779 *(u32 *)(&p[moffset + ip_offset]));
5780 calc_y(tmp_y_l, *(u32 *)(&p[offset + ip_offset]),
5781 *(u32 *)(&p[moffset + ip_offset]));
5782 *(__le32 *)key_x = cpu_to_le32(tmp_x_l);
5783 *(__le32 *)key_y = cpu_to_le32(tmp_y_l);
5784
5785 return true;
5786 default:
5787 return false;
5788 }
5789 }
5790
5791 static u32 hclge_get_port_number(enum HLCGE_PORT_TYPE port_type, u8 pf_id,
5792 u8 vf_id, u8 network_port_id)
5793 {
5794 u32 port_number = 0;
5795
5796 if (port_type == HOST_PORT) {
5797 hnae3_set_field(port_number, HCLGE_PF_ID_M, HCLGE_PF_ID_S,
5798 pf_id);
5799 hnae3_set_field(port_number, HCLGE_VF_ID_M, HCLGE_VF_ID_S,
5800 vf_id);
5801 hnae3_set_bit(port_number, HCLGE_PORT_TYPE_B, HOST_PORT);
5802 } else {
5803 hnae3_set_field(port_number, HCLGE_NETWORK_PORT_ID_M,
5804 HCLGE_NETWORK_PORT_ID_S, network_port_id);
5805 hnae3_set_bit(port_number, HCLGE_PORT_TYPE_B, NETWORK_PORT);
5806 }
5807
5808 return port_number;
5809 }
5810
5811 static void hclge_fd_convert_meta_data(struct hclge_fd_key_cfg *key_cfg,
5812 __le32 *key_x, __le32 *key_y,
5813 struct hclge_fd_rule *rule)
5814 {
5815 u32 tuple_bit, meta_data = 0, tmp_x, tmp_y, port_number;
5816 u8 cur_pos = 0, tuple_size, shift_bits;
5817 unsigned int i;
5818
5819 for (i = 0; i < MAX_META_DATA; i++) {
5820 tuple_size = meta_data_key_info[i].key_length;
5821 tuple_bit = key_cfg->meta_data_active & BIT(i);
5822
5823 switch (tuple_bit) {
5824 case BIT(ROCE_TYPE):
5825 hnae3_set_bit(meta_data, cur_pos, NIC_PACKET);
5826 cur_pos += tuple_size;
5827 break;
5828 case BIT(DST_VPORT):
5829 port_number = hclge_get_port_number(HOST_PORT, 0,
5830 rule->vf_id, 0);
5831 hnae3_set_field(meta_data,
5832 GENMASK(cur_pos + tuple_size, cur_pos),
5833 cur_pos, port_number);
5834 cur_pos += tuple_size;
5835 break;
5836 default:
5837 break;
5838 }
5839 }
5840
5841 calc_x(tmp_x, meta_data, 0xFFFFFFFF);
5842 calc_y(tmp_y, meta_data, 0xFFFFFFFF);
5843 shift_bits = sizeof(meta_data) * 8 - cur_pos;
5844
5845 *key_x = cpu_to_le32(tmp_x << shift_bits);
5846 *key_y = cpu_to_le32(tmp_y << shift_bits);
5847 }
5848
5849 /* A complete key is combined with meta data key and tuple key.
5850 * Meta data key is stored at the MSB region, and tuple key is stored at
5851 * the LSB region, unused bits will be filled 0.
5852 */
5853 static int hclge_config_key(struct hclge_dev *hdev, u8 stage,
5854 struct hclge_fd_rule *rule)
5855 {
5856 struct hclge_fd_key_cfg *key_cfg = &hdev->fd_cfg.key_cfg[stage];
5857 u8 key_x[MAX_KEY_BYTES], key_y[MAX_KEY_BYTES];
5858 u8 *cur_key_x, *cur_key_y;
5859 u8 meta_data_region;
5860 u8 tuple_size;
5861 int ret;
5862 u32 i;
5863
5864 memset(key_x, 0, sizeof(key_x));
5865 memset(key_y, 0, sizeof(key_y));
5866 cur_key_x = key_x;
5867 cur_key_y = key_y;
5868
5869 for (i = 0; i < MAX_TUPLE; i++) {
5870 bool tuple_valid;
5871
5872 tuple_size = tuple_key_info[i].key_length / 8;
5873 if (!(key_cfg->tuple_active & BIT(i)))
5874 continue;
5875
5876 tuple_valid = hclge_fd_convert_tuple(i, cur_key_x,
5877 cur_key_y, rule);
5878 if (tuple_valid) {
5879 cur_key_x += tuple_size;
5880 cur_key_y += tuple_size;
5881 }
5882 }
5883
5884 meta_data_region = hdev->fd_cfg.max_key_length / 8 -
5885 MAX_META_DATA_LENGTH / 8;
5886
5887 hclge_fd_convert_meta_data(key_cfg,
5888 (__le32 *)(key_x + meta_data_region),
5889 (__le32 *)(key_y + meta_data_region),
5890 rule);
5891
5892 ret = hclge_fd_tcam_config(hdev, stage, false, rule->location, key_y,
5893 true);
5894 if (ret) {
5895 dev_err(&hdev->pdev->dev,
5896 "fd key_y config fail, loc=%u, ret=%d\n",
5897 rule->queue_id, ret);
5898 return ret;
5899 }
5900
5901 ret = hclge_fd_tcam_config(hdev, stage, true, rule->location, key_x,
5902 true);
5903 if (ret)
5904 dev_err(&hdev->pdev->dev,
5905 "fd key_x config fail, loc=%u, ret=%d\n",
5906 rule->queue_id, ret);
5907 return ret;
5908 }
5909
5910 static int hclge_config_action(struct hclge_dev *hdev, u8 stage,
5911 struct hclge_fd_rule *rule)
5912 {
5913 struct hclge_vport *vport = hdev->vport;
5914 struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
5915 struct hclge_fd_ad_data ad_data;
5916
5917 memset(&ad_data, 0, sizeof(struct hclge_fd_ad_data));
5918 ad_data.ad_id = rule->location;
5919
5920 if (rule->action == HCLGE_FD_ACTION_DROP_PACKET) {
5921 ad_data.drop_packet = true;
5922 } else if (rule->action == HCLGE_FD_ACTION_SELECT_TC) {
5923 ad_data.override_tc = true;
5924 ad_data.queue_id =
5925 kinfo->tc_info.tqp_offset[rule->cls_flower.tc];
5926 ad_data.tc_size =
5927 ilog2(kinfo->tc_info.tqp_count[rule->cls_flower.tc]);
5928 } else {
5929 ad_data.forward_to_direct_queue = true;
5930 ad_data.queue_id = rule->queue_id;
5931 }
5932
5933 if (hdev->fd_cfg.cnt_num[HCLGE_FD_STAGE_1]) {
5934 ad_data.use_counter = true;
5935 ad_data.counter_id = rule->vf_id %
5936 hdev->fd_cfg.cnt_num[HCLGE_FD_STAGE_1];
5937 } else {
5938 ad_data.use_counter = false;
5939 ad_data.counter_id = 0;
5940 }
5941
5942 ad_data.use_next_stage = false;
5943 ad_data.next_input_key = 0;
5944
5945 ad_data.write_rule_id_to_bd = true;
5946 ad_data.rule_id = rule->location;
5947
5948 return hclge_fd_ad_config(hdev, stage, ad_data.ad_id, &ad_data);
5949 }
5950
5951 static int hclge_fd_check_tcpip4_tuple(struct ethtool_tcpip4_spec *spec,
5952 u32 *unused_tuple)
5953 {
5954 if (!spec || !unused_tuple)
5955 return -EINVAL;
5956
5957 *unused_tuple |= BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC);
5958
5959 if (!spec->ip4src)
5960 *unused_tuple |= BIT(INNER_SRC_IP);
5961
5962 if (!spec->ip4dst)
5963 *unused_tuple |= BIT(INNER_DST_IP);
5964
5965 if (!spec->psrc)
5966 *unused_tuple |= BIT(INNER_SRC_PORT);
5967
5968 if (!spec->pdst)
5969 *unused_tuple |= BIT(INNER_DST_PORT);
5970
5971 if (!spec->tos)
5972 *unused_tuple |= BIT(INNER_IP_TOS);
5973
5974 return 0;
5975 }
5976
5977 static int hclge_fd_check_ip4_tuple(struct ethtool_usrip4_spec *spec,
5978 u32 *unused_tuple)
5979 {
5980 if (!spec || !unused_tuple)
5981 return -EINVAL;
5982
5983 *unused_tuple |= BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC) |
5984 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
5985
5986 if (!spec->ip4src)
5987 *unused_tuple |= BIT(INNER_SRC_IP);
5988
5989 if (!spec->ip4dst)
5990 *unused_tuple |= BIT(INNER_DST_IP);
5991
5992 if (!spec->tos)
5993 *unused_tuple |= BIT(INNER_IP_TOS);
5994
5995 if (!spec->proto)
5996 *unused_tuple |= BIT(INNER_IP_PROTO);
5997
5998 if (spec->l4_4_bytes)
5999 return -EOPNOTSUPP;
6000
6001 if (spec->ip_ver != ETH_RX_NFC_IP4)
6002 return -EOPNOTSUPP;
6003
6004 return 0;
6005 }
6006
6007 static int hclge_fd_check_tcpip6_tuple(struct ethtool_tcpip6_spec *spec,
6008 u32 *unused_tuple)
6009 {
6010 if (!spec || !unused_tuple)
6011 return -EINVAL;
6012
6013 *unused_tuple |= BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC);
6014
6015 /* check whether src/dst ip address used */
6016 if (ipv6_addr_any((struct in6_addr *)spec->ip6src))
6017 *unused_tuple |= BIT(INNER_SRC_IP);
6018
6019 if (ipv6_addr_any((struct in6_addr *)spec->ip6dst))
6020 *unused_tuple |= BIT(INNER_DST_IP);
6021
6022 if (!spec->psrc)
6023 *unused_tuple |= BIT(INNER_SRC_PORT);
6024
6025 if (!spec->pdst)
6026 *unused_tuple |= BIT(INNER_DST_PORT);
6027
6028 if (!spec->tclass)
6029 *unused_tuple |= BIT(INNER_IP_TOS);
6030
6031 return 0;
6032 }
6033
6034 static int hclge_fd_check_ip6_tuple(struct ethtool_usrip6_spec *spec,
6035 u32 *unused_tuple)
6036 {
6037 if (!spec || !unused_tuple)
6038 return -EINVAL;
6039
6040 *unused_tuple |= BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC) |
6041 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
6042
6043 /* check whether src/dst ip address used */
6044 if (ipv6_addr_any((struct in6_addr *)spec->ip6src))
6045 *unused_tuple |= BIT(INNER_SRC_IP);
6046
6047 if (ipv6_addr_any((struct in6_addr *)spec->ip6dst))
6048 *unused_tuple |= BIT(INNER_DST_IP);
6049
6050 if (!spec->l4_proto)
6051 *unused_tuple |= BIT(INNER_IP_PROTO);
6052
6053 if (!spec->tclass)
6054 *unused_tuple |= BIT(INNER_IP_TOS);
6055
6056 if (spec->l4_4_bytes)
6057 return -EOPNOTSUPP;
6058
6059 return 0;
6060 }
6061
6062 static int hclge_fd_check_ether_tuple(struct ethhdr *spec, u32 *unused_tuple)
6063 {
6064 if (!spec || !unused_tuple)
6065 return -EINVAL;
6066
6067 *unused_tuple |= BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
6068 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT) |
6069 BIT(INNER_IP_TOS) | BIT(INNER_IP_PROTO);
6070
6071 if (is_zero_ether_addr(spec->h_source))
6072 *unused_tuple |= BIT(INNER_SRC_MAC);
6073
6074 if (is_zero_ether_addr(spec->h_dest))
6075 *unused_tuple |= BIT(INNER_DST_MAC);
6076
6077 if (!spec->h_proto)
6078 *unused_tuple |= BIT(INNER_ETH_TYPE);
6079
6080 return 0;
6081 }
6082
6083 static int hclge_fd_check_ext_tuple(struct hclge_dev *hdev,
6084 struct ethtool_rx_flow_spec *fs,
6085 u32 *unused_tuple)
6086 {
6087 if (fs->flow_type & FLOW_EXT) {
6088 if (fs->h_ext.vlan_etype) {
6089 dev_err(&hdev->pdev->dev, "vlan-etype is not supported!\n");
6090 return -EOPNOTSUPP;
6091 }
6092
6093 if (!fs->h_ext.vlan_tci)
6094 *unused_tuple |= BIT(INNER_VLAN_TAG_FST);
6095
6096 if (fs->m_ext.vlan_tci &&
6097 be16_to_cpu(fs->h_ext.vlan_tci) >= VLAN_N_VID) {
6098 dev_err(&hdev->pdev->dev,
6099 "failed to config vlan_tci, invalid vlan_tci: %u, max is %d.\n",
6100 ntohs(fs->h_ext.vlan_tci), VLAN_N_VID - 1);
6101 return -EINVAL;
6102 }
6103 } else {
6104 *unused_tuple |= BIT(INNER_VLAN_TAG_FST);
6105 }
6106
6107 if (fs->flow_type & FLOW_MAC_EXT) {
6108 if (hdev->fd_cfg.fd_mode !=
6109 HCLGE_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1) {
6110 dev_err(&hdev->pdev->dev,
6111 "FLOW_MAC_EXT is not supported in current fd mode!\n");
6112 return -EOPNOTSUPP;
6113 }
6114
6115 if (is_zero_ether_addr(fs->h_ext.h_dest))
6116 *unused_tuple |= BIT(INNER_DST_MAC);
6117 else
6118 *unused_tuple &= ~BIT(INNER_DST_MAC);
6119 }
6120
6121 return 0;
6122 }
6123
6124 static int hclge_fd_get_user_def_layer(u32 flow_type, u32 *unused_tuple,
6125 struct hclge_fd_user_def_info *info)
6126 {
6127 switch (flow_type) {
6128 case ETHER_FLOW:
6129 info->layer = HCLGE_FD_USER_DEF_L2;
6130 *unused_tuple &= ~BIT(INNER_L2_RSV);
6131 break;
6132 case IP_USER_FLOW:
6133 case IPV6_USER_FLOW:
6134 info->layer = HCLGE_FD_USER_DEF_L3;
6135 *unused_tuple &= ~BIT(INNER_L3_RSV);
6136 break;
6137 case TCP_V4_FLOW:
6138 case UDP_V4_FLOW:
6139 case TCP_V6_FLOW:
6140 case UDP_V6_FLOW:
6141 info->layer = HCLGE_FD_USER_DEF_L4;
6142 *unused_tuple &= ~BIT(INNER_L4_RSV);
6143 break;
6144 default:
6145 return -EOPNOTSUPP;
6146 }
6147
6148 return 0;
6149 }
6150
6151 static bool hclge_fd_is_user_def_all_masked(struct ethtool_rx_flow_spec *fs)
6152 {
6153 return be32_to_cpu(fs->m_ext.data[1] | fs->m_ext.data[0]) == 0;
6154 }
6155
6156 static int hclge_fd_parse_user_def_field(struct hclge_dev *hdev,
6157 struct ethtool_rx_flow_spec *fs,
6158 u32 *unused_tuple,
6159 struct hclge_fd_user_def_info *info)
6160 {
6161 u32 tuple_active = hdev->fd_cfg.key_cfg[HCLGE_FD_STAGE_1].tuple_active;
6162 u32 flow_type = fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT);
6163 u16 data, offset, data_mask, offset_mask;
6164 int ret;
6165
6166 info->layer = HCLGE_FD_USER_DEF_NONE;
6167 *unused_tuple |= HCLGE_FD_TUPLE_USER_DEF_TUPLES;
6168
6169 if (!(fs->flow_type & FLOW_EXT) || hclge_fd_is_user_def_all_masked(fs))
6170 return 0;
6171
6172 /* user-def data from ethtool is 64 bit value, the bit0~15 is used
6173 * for data, and bit32~47 is used for offset.
6174 */
6175 data = be32_to_cpu(fs->h_ext.data[1]) & HCLGE_FD_USER_DEF_DATA;
6176 data_mask = be32_to_cpu(fs->m_ext.data[1]) & HCLGE_FD_USER_DEF_DATA;
6177 offset = be32_to_cpu(fs->h_ext.data[0]) & HCLGE_FD_USER_DEF_OFFSET;
6178 offset_mask = be32_to_cpu(fs->m_ext.data[0]) & HCLGE_FD_USER_DEF_OFFSET;
6179
6180 if (!(tuple_active & HCLGE_FD_TUPLE_USER_DEF_TUPLES)) {
6181 dev_err(&hdev->pdev->dev, "user-def bytes are not supported\n");
6182 return -EOPNOTSUPP;
6183 }
6184
6185 if (offset > HCLGE_FD_MAX_USER_DEF_OFFSET) {
6186 dev_err(&hdev->pdev->dev,
6187 "user-def offset[%u] should be no more than %u\n",
6188 offset, HCLGE_FD_MAX_USER_DEF_OFFSET);
6189 return -EINVAL;
6190 }
6191
6192 if (offset_mask != HCLGE_FD_USER_DEF_OFFSET_UNMASK) {
6193 dev_err(&hdev->pdev->dev, "user-def offset can't be masked\n");
6194 return -EINVAL;
6195 }
6196
6197 ret = hclge_fd_get_user_def_layer(flow_type, unused_tuple, info);
6198 if (ret) {
6199 dev_err(&hdev->pdev->dev,
6200 "unsupported flow type for user-def bytes, ret = %d\n",
6201 ret);
6202 return ret;
6203 }
6204
6205 info->data = data;
6206 info->data_mask = data_mask;
6207 info->offset = offset;
6208
6209 return 0;
6210 }
6211
6212 static int hclge_fd_check_spec(struct hclge_dev *hdev,
6213 struct ethtool_rx_flow_spec *fs,
6214 u32 *unused_tuple,
6215 struct hclge_fd_user_def_info *info)
6216 {
6217 u32 flow_type;
6218 int ret;
6219
6220 if (fs->location >= hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1]) {
6221 dev_err(&hdev->pdev->dev,
6222 "failed to config fd rules, invalid rule location: %u, max is %u\n.",
6223 fs->location,
6224 hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1] - 1);
6225 return -EINVAL;
6226 }
6227
6228 ret = hclge_fd_parse_user_def_field(hdev, fs, unused_tuple, info);
6229 if (ret)
6230 return ret;
6231
6232 flow_type = fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT);
6233 switch (flow_type) {
6234 case SCTP_V4_FLOW:
6235 case TCP_V4_FLOW:
6236 case UDP_V4_FLOW:
6237 ret = hclge_fd_check_tcpip4_tuple(&fs->h_u.tcp_ip4_spec,
6238 unused_tuple);
6239 break;
6240 case IP_USER_FLOW:
6241 ret = hclge_fd_check_ip4_tuple(&fs->h_u.usr_ip4_spec,
6242 unused_tuple);
6243 break;
6244 case SCTP_V6_FLOW:
6245 case TCP_V6_FLOW:
6246 case UDP_V6_FLOW:
6247 ret = hclge_fd_check_tcpip6_tuple(&fs->h_u.tcp_ip6_spec,
6248 unused_tuple);
6249 break;
6250 case IPV6_USER_FLOW:
6251 ret = hclge_fd_check_ip6_tuple(&fs->h_u.usr_ip6_spec,
6252 unused_tuple);
6253 break;
6254 case ETHER_FLOW:
6255 if (hdev->fd_cfg.fd_mode !=
6256 HCLGE_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1) {
6257 dev_err(&hdev->pdev->dev,
6258 "ETHER_FLOW is not supported in current fd mode!\n");
6259 return -EOPNOTSUPP;
6260 }
6261
6262 ret = hclge_fd_check_ether_tuple(&fs->h_u.ether_spec,
6263 unused_tuple);
6264 break;
6265 default:
6266 dev_err(&hdev->pdev->dev,
6267 "unsupported protocol type, protocol type = %#x\n",
6268 flow_type);
6269 return -EOPNOTSUPP;
6270 }
6271
6272 if (ret) {
6273 dev_err(&hdev->pdev->dev,
6274 "failed to check flow union tuple, ret = %d\n",
6275 ret);
6276 return ret;
6277 }
6278
6279 return hclge_fd_check_ext_tuple(hdev, fs, unused_tuple);
6280 }
6281
6282 static void hclge_fd_get_tcpip4_tuple(struct ethtool_rx_flow_spec *fs,
6283 struct hclge_fd_rule *rule, u8 ip_proto)
6284 {
6285 rule->tuples.src_ip[IPV4_INDEX] =
6286 be32_to_cpu(fs->h_u.tcp_ip4_spec.ip4src);
6287 rule->tuples_mask.src_ip[IPV4_INDEX] =
6288 be32_to_cpu(fs->m_u.tcp_ip4_spec.ip4src);
6289
6290 rule->tuples.dst_ip[IPV4_INDEX] =
6291 be32_to_cpu(fs->h_u.tcp_ip4_spec.ip4dst);
6292 rule->tuples_mask.dst_ip[IPV4_INDEX] =
6293 be32_to_cpu(fs->m_u.tcp_ip4_spec.ip4dst);
6294
6295 rule->tuples.src_port = be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc);
6296 rule->tuples_mask.src_port = be16_to_cpu(fs->m_u.tcp_ip4_spec.psrc);
6297
6298 rule->tuples.dst_port = be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst);
6299 rule->tuples_mask.dst_port = be16_to_cpu(fs->m_u.tcp_ip4_spec.pdst);
6300
6301 rule->tuples.ip_tos = fs->h_u.tcp_ip4_spec.tos;
6302 rule->tuples_mask.ip_tos = fs->m_u.tcp_ip4_spec.tos;
6303
6304 rule->tuples.ether_proto = ETH_P_IP;
6305 rule->tuples_mask.ether_proto = 0xFFFF;
6306
6307 rule->tuples.ip_proto = ip_proto;
6308 rule->tuples_mask.ip_proto = 0xFF;
6309 }
6310
6311 static void hclge_fd_get_ip4_tuple(struct ethtool_rx_flow_spec *fs,
6312 struct hclge_fd_rule *rule)
6313 {
6314 rule->tuples.src_ip[IPV4_INDEX] =
6315 be32_to_cpu(fs->h_u.usr_ip4_spec.ip4src);
6316 rule->tuples_mask.src_ip[IPV4_INDEX] =
6317 be32_to_cpu(fs->m_u.usr_ip4_spec.ip4src);
6318
6319 rule->tuples.dst_ip[IPV4_INDEX] =
6320 be32_to_cpu(fs->h_u.usr_ip4_spec.ip4dst);
6321 rule->tuples_mask.dst_ip[IPV4_INDEX] =
6322 be32_to_cpu(fs->m_u.usr_ip4_spec.ip4dst);
6323
6324 rule->tuples.ip_tos = fs->h_u.usr_ip4_spec.tos;
6325 rule->tuples_mask.ip_tos = fs->m_u.usr_ip4_spec.tos;
6326
6327 rule->tuples.ip_proto = fs->h_u.usr_ip4_spec.proto;
6328 rule->tuples_mask.ip_proto = fs->m_u.usr_ip4_spec.proto;
6329
6330 rule->tuples.ether_proto = ETH_P_IP;
6331 rule->tuples_mask.ether_proto = 0xFFFF;
6332 }
6333
6334 static void hclge_fd_get_tcpip6_tuple(struct ethtool_rx_flow_spec *fs,
6335 struct hclge_fd_rule *rule, u8 ip_proto)
6336 {
6337 ipv6_addr_be32_to_cpu(rule->tuples.src_ip,
6338 fs->h_u.tcp_ip6_spec.ip6src);
6339 ipv6_addr_be32_to_cpu(rule->tuples_mask.src_ip,
6340 fs->m_u.tcp_ip6_spec.ip6src);
6341
6342 ipv6_addr_be32_to_cpu(rule->tuples.dst_ip,
6343 fs->h_u.tcp_ip6_spec.ip6dst);
6344 ipv6_addr_be32_to_cpu(rule->tuples_mask.dst_ip,
6345 fs->m_u.tcp_ip6_spec.ip6dst);
6346
6347 rule->tuples.src_port = be16_to_cpu(fs->h_u.tcp_ip6_spec.psrc);
6348 rule->tuples_mask.src_port = be16_to_cpu(fs->m_u.tcp_ip6_spec.psrc);
6349
6350 rule->tuples.dst_port = be16_to_cpu(fs->h_u.tcp_ip6_spec.pdst);
6351 rule->tuples_mask.dst_port = be16_to_cpu(fs->m_u.tcp_ip6_spec.pdst);
6352
6353 rule->tuples.ether_proto = ETH_P_IPV6;
6354 rule->tuples_mask.ether_proto = 0xFFFF;
6355
6356 rule->tuples.ip_tos = fs->h_u.tcp_ip6_spec.tclass;
6357 rule->tuples_mask.ip_tos = fs->m_u.tcp_ip6_spec.tclass;
6358
6359 rule->tuples.ip_proto = ip_proto;
6360 rule->tuples_mask.ip_proto = 0xFF;
6361 }
6362
6363 static void hclge_fd_get_ip6_tuple(struct ethtool_rx_flow_spec *fs,
6364 struct hclge_fd_rule *rule)
6365 {
6366 ipv6_addr_be32_to_cpu(rule->tuples.src_ip,
6367 fs->h_u.usr_ip6_spec.ip6src);
6368 ipv6_addr_be32_to_cpu(rule->tuples_mask.src_ip,
6369 fs->m_u.usr_ip6_spec.ip6src);
6370
6371 ipv6_addr_be32_to_cpu(rule->tuples.dst_ip,
6372 fs->h_u.usr_ip6_spec.ip6dst);
6373 ipv6_addr_be32_to_cpu(rule->tuples_mask.dst_ip,
6374 fs->m_u.usr_ip6_spec.ip6dst);
6375
6376 rule->tuples.ip_proto = fs->h_u.usr_ip6_spec.l4_proto;
6377 rule->tuples_mask.ip_proto = fs->m_u.usr_ip6_spec.l4_proto;
6378
6379 rule->tuples.ip_tos = fs->h_u.tcp_ip6_spec.tclass;
6380 rule->tuples_mask.ip_tos = fs->m_u.tcp_ip6_spec.tclass;
6381
6382 rule->tuples.ether_proto = ETH_P_IPV6;
6383 rule->tuples_mask.ether_proto = 0xFFFF;
6384 }
6385
6386 static void hclge_fd_get_ether_tuple(struct ethtool_rx_flow_spec *fs,
6387 struct hclge_fd_rule *rule)
6388 {
6389 ether_addr_copy(rule->tuples.src_mac, fs->h_u.ether_spec.h_source);
6390 ether_addr_copy(rule->tuples_mask.src_mac, fs->m_u.ether_spec.h_source);
6391
6392 ether_addr_copy(rule->tuples.dst_mac, fs->h_u.ether_spec.h_dest);
6393 ether_addr_copy(rule->tuples_mask.dst_mac, fs->m_u.ether_spec.h_dest);
6394
6395 rule->tuples.ether_proto = be16_to_cpu(fs->h_u.ether_spec.h_proto);
6396 rule->tuples_mask.ether_proto = be16_to_cpu(fs->m_u.ether_spec.h_proto);
6397 }
6398
6399 static void hclge_fd_get_user_def_tuple(struct hclge_fd_user_def_info *info,
6400 struct hclge_fd_rule *rule)
6401 {
6402 switch (info->layer) {
6403 case HCLGE_FD_USER_DEF_L2:
6404 rule->tuples.l2_user_def = info->data;
6405 rule->tuples_mask.l2_user_def = info->data_mask;
6406 break;
6407 case HCLGE_FD_USER_DEF_L3:
6408 rule->tuples.l3_user_def = info->data;
6409 rule->tuples_mask.l3_user_def = info->data_mask;
6410 break;
6411 case HCLGE_FD_USER_DEF_L4:
6412 rule->tuples.l4_user_def = (u32)info->data << 16;
6413 rule->tuples_mask.l4_user_def = (u32)info->data_mask << 16;
6414 break;
6415 default:
6416 break;
6417 }
6418
6419 rule->ep.user_def = *info;
6420 }
6421
6422 static int hclge_fd_get_tuple(struct ethtool_rx_flow_spec *fs,
6423 struct hclge_fd_rule *rule,
6424 struct hclge_fd_user_def_info *info)
6425 {
6426 u32 flow_type = fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT);
6427
6428 switch (flow_type) {
6429 case SCTP_V4_FLOW:
6430 hclge_fd_get_tcpip4_tuple(fs, rule, IPPROTO_SCTP);
6431 break;
6432 case TCP_V4_FLOW:
6433 hclge_fd_get_tcpip4_tuple(fs, rule, IPPROTO_TCP);
6434 break;
6435 case UDP_V4_FLOW:
6436 hclge_fd_get_tcpip4_tuple(fs, rule, IPPROTO_UDP);
6437 break;
6438 case IP_USER_FLOW:
6439 hclge_fd_get_ip4_tuple(fs, rule);
6440 break;
6441 case SCTP_V6_FLOW:
6442 hclge_fd_get_tcpip6_tuple(fs, rule, IPPROTO_SCTP);
6443 break;
6444 case TCP_V6_FLOW:
6445 hclge_fd_get_tcpip6_tuple(fs, rule, IPPROTO_TCP);
6446 break;
6447 case UDP_V6_FLOW:
6448 hclge_fd_get_tcpip6_tuple(fs, rule, IPPROTO_UDP);
6449 break;
6450 case IPV6_USER_FLOW:
6451 hclge_fd_get_ip6_tuple(fs, rule);
6452 break;
6453 case ETHER_FLOW:
6454 hclge_fd_get_ether_tuple(fs, rule);
6455 break;
6456 default:
6457 return -EOPNOTSUPP;
6458 }
6459
6460 if (fs->flow_type & FLOW_EXT) {
6461 rule->tuples.vlan_tag1 = be16_to_cpu(fs->h_ext.vlan_tci);
6462 rule->tuples_mask.vlan_tag1 = be16_to_cpu(fs->m_ext.vlan_tci);
6463 hclge_fd_get_user_def_tuple(info, rule);
6464 }
6465
6466 if (fs->flow_type & FLOW_MAC_EXT) {
6467 ether_addr_copy(rule->tuples.dst_mac, fs->h_ext.h_dest);
6468 ether_addr_copy(rule->tuples_mask.dst_mac, fs->m_ext.h_dest);
6469 }
6470
6471 return 0;
6472 }
6473
6474 static int hclge_fd_config_rule(struct hclge_dev *hdev,
6475 struct hclge_fd_rule *rule)
6476 {
6477 int ret;
6478
6479 ret = hclge_config_action(hdev, HCLGE_FD_STAGE_1, rule);
6480 if (ret)
6481 return ret;
6482
6483 return hclge_config_key(hdev, HCLGE_FD_STAGE_1, rule);
6484 }
6485
6486 static int hclge_add_fd_entry_common(struct hclge_dev *hdev,
6487 struct hclge_fd_rule *rule)
6488 {
6489 int ret;
6490
6491 spin_lock_bh(&hdev->fd_rule_lock);
6492
6493 if (hdev->fd_active_type != rule->rule_type &&
6494 (hdev->fd_active_type == HCLGE_FD_TC_FLOWER_ACTIVE ||
6495 hdev->fd_active_type == HCLGE_FD_EP_ACTIVE)) {
6496 dev_err(&hdev->pdev->dev,
6497 "mode conflict(new type %d, active type %d), please delete existent rules first\n",
6498 rule->rule_type, hdev->fd_active_type);
6499 spin_unlock_bh(&hdev->fd_rule_lock);
6500 return -EINVAL;
6501 }
6502
6503 ret = hclge_fd_check_user_def_refcnt(hdev, rule);
6504 if (ret)
6505 goto out;
6506
6507 ret = hclge_clear_arfs_rules(hdev);
6508 if (ret)
6509 goto out;
6510
6511 ret = hclge_fd_config_rule(hdev, rule);
6512 if (ret)
6513 goto out;
6514
6515 rule->state = HCLGE_FD_ACTIVE;
6516 hdev->fd_active_type = rule->rule_type;
6517 hclge_update_fd_list(hdev, rule->state, rule->location, rule);
6518
6519 out:
6520 spin_unlock_bh(&hdev->fd_rule_lock);
6521 return ret;
6522 }
6523
6524 static bool hclge_is_cls_flower_active(struct hnae3_handle *handle)
6525 {
6526 struct hclge_vport *vport = hclge_get_vport(handle);
6527 struct hclge_dev *hdev = vport->back;
6528
6529 return hdev->fd_active_type == HCLGE_FD_TC_FLOWER_ACTIVE;
6530 }
6531
6532 static int hclge_fd_parse_ring_cookie(struct hclge_dev *hdev, u64 ring_cookie,
6533 u16 *vport_id, u8 *action, u16 *queue_id)
6534 {
6535 struct hclge_vport *vport = hdev->vport;
6536
6537 if (ring_cookie == RX_CLS_FLOW_DISC) {
6538 *action = HCLGE_FD_ACTION_DROP_PACKET;
6539 } else {
6540 u32 ring = ethtool_get_flow_spec_ring(ring_cookie);
6541 u8 vf = ethtool_get_flow_spec_ring_vf(ring_cookie);
6542 u16 tqps;
6543
6544 /* To keep consistent with user's configuration, minus 1 when
6545 * printing 'vf', because vf id from ethtool is added 1 for vf.
6546 */
6547 if (vf > hdev->num_req_vfs) {
6548 dev_err(&hdev->pdev->dev,
6549 "Error: vf id (%u) should be less than %u\n",
6550 vf - 1U, hdev->num_req_vfs);
6551 return -EINVAL;
6552 }
6553
6554 *vport_id = vf ? hdev->vport[vf].vport_id : vport->vport_id;
6555 tqps = hdev->vport[vf].nic.kinfo.num_tqps;
6556
6557 if (ring >= tqps) {
6558 dev_err(&hdev->pdev->dev,
6559 "Error: queue id (%u) > max tqp num (%u)\n",
6560 ring, tqps - 1U);
6561 return -EINVAL;
6562 }
6563
6564 *action = HCLGE_FD_ACTION_SELECT_QUEUE;
6565 *queue_id = ring;
6566 }
6567
6568 return 0;
6569 }
6570
6571 static int hclge_add_fd_entry(struct hnae3_handle *handle,
6572 struct ethtool_rxnfc *cmd)
6573 {
6574 struct hclge_vport *vport = hclge_get_vport(handle);
6575 struct hclge_dev *hdev = vport->back;
6576 struct hclge_fd_user_def_info info;
6577 u16 dst_vport_id = 0, q_index = 0;
6578 struct ethtool_rx_flow_spec *fs;
6579 struct hclge_fd_rule *rule;
6580 u32 unused = 0;
6581 u8 action;
6582 int ret;
6583
6584 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev)) {
6585 dev_err(&hdev->pdev->dev,
6586 "flow table director is not supported\n");
6587 return -EOPNOTSUPP;
6588 }
6589
6590 if (!hdev->fd_en) {
6591 dev_err(&hdev->pdev->dev,
6592 "please enable flow director first\n");
6593 return -EOPNOTSUPP;
6594 }
6595
6596 fs = (struct ethtool_rx_flow_spec *)&cmd->fs;
6597
6598 ret = hclge_fd_check_spec(hdev, fs, &unused, &info);
6599 if (ret)
6600 return ret;
6601
6602 ret = hclge_fd_parse_ring_cookie(hdev, fs->ring_cookie, &dst_vport_id,
6603 &action, &q_index);
6604 if (ret)
6605 return ret;
6606
6607 rule = kzalloc_obj(*rule);
6608 if (!rule)
6609 return -ENOMEM;
6610
6611 ret = hclge_fd_get_tuple(fs, rule, &info);
6612 if (ret) {
6613 kfree(rule);
6614 return ret;
6615 }
6616
6617 rule->flow_type = fs->flow_type;
6618 rule->location = fs->location;
6619 rule->unused_tuple = unused;
6620 rule->vf_id = dst_vport_id;
6621 rule->queue_id = q_index;
6622 rule->action = action;
6623 rule->rule_type = HCLGE_FD_EP_ACTIVE;
6624
6625 ret = hclge_add_fd_entry_common(hdev, rule);
6626 if (ret)
6627 kfree(rule);
6628
6629 return ret;
6630 }
6631
6632 static int hclge_del_fd_entry(struct hnae3_handle *handle,
6633 struct ethtool_rxnfc *cmd)
6634 {
6635 struct hclge_vport *vport = hclge_get_vport(handle);
6636 struct hclge_dev *hdev = vport->back;
6637 struct ethtool_rx_flow_spec *fs;
6638 int ret;
6639
6640 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
6641 return -EOPNOTSUPP;
6642
6643 fs = (struct ethtool_rx_flow_spec *)&cmd->fs;
6644
6645 if (fs->location >= hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1])
6646 return -EINVAL;
6647
6648 spin_lock_bh(&hdev->fd_rule_lock);
6649 if (hdev->fd_active_type == HCLGE_FD_TC_FLOWER_ACTIVE ||
6650 !test_bit(fs->location, hdev->fd_bmap)) {
6651 dev_err(&hdev->pdev->dev,
6652 "Delete fail, rule %u is inexistent\n", fs->location);
6653 spin_unlock_bh(&hdev->fd_rule_lock);
6654 return -ENOENT;
6655 }
6656
6657 ret = hclge_fd_tcam_config(hdev, HCLGE_FD_STAGE_1, true, fs->location,
6658 NULL, false);
6659 if (ret)
6660 goto out;
6661
6662 hclge_update_fd_list(hdev, HCLGE_FD_DELETED, fs->location, NULL);
6663
6664 out:
6665 spin_unlock_bh(&hdev->fd_rule_lock);
6666 return ret;
6667 }
6668
6669 static void hclge_clear_fd_rules_in_list(struct hclge_dev *hdev,
6670 bool clear_list)
6671 {
6672 struct hclge_fd_rule *rule;
6673 struct hlist_node *node;
6674 u16 location;
6675
6676 spin_lock_bh(&hdev->fd_rule_lock);
6677
6678 for_each_set_bit(location, hdev->fd_bmap,
6679 hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1])
6680 hclge_fd_tcam_config(hdev, HCLGE_FD_STAGE_1, true, location,
6681 NULL, false);
6682
6683 if (clear_list) {
6684 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list,
6685 rule_node) {
6686 hlist_del(&rule->rule_node);
6687 kfree(rule);
6688 }
6689 hdev->fd_active_type = HCLGE_FD_RULE_NONE;
6690 hdev->hclge_fd_rule_num = 0;
6691 bitmap_zero(hdev->fd_bmap,
6692 hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1]);
6693 }
6694
6695 spin_unlock_bh(&hdev->fd_rule_lock);
6696 }
6697
6698 static void hclge_del_all_fd_entries(struct hclge_dev *hdev)
6699 {
6700 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
6701 return;
6702
6703 hclge_clear_fd_rules_in_list(hdev, true);
6704 hclge_fd_disable_user_def(hdev);
6705 }
6706
6707 static int hclge_restore_fd_entries(struct hnae3_handle *handle)
6708 {
6709 struct hclge_vport *vport = hclge_get_vport(handle);
6710 struct hclge_dev *hdev = vport->back;
6711 struct hclge_fd_rule *rule;
6712 struct hlist_node *node;
6713
6714 /* Return ok here, because reset error handling will check this
6715 * return value. If error is returned here, the reset process will
6716 * fail.
6717 */
6718 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
6719 return 0;
6720
6721 /* if fd is disabled, should not restore it when reset */
6722 if (!hdev->fd_en)
6723 return 0;
6724
6725 spin_lock_bh(&hdev->fd_rule_lock);
6726 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list, rule_node) {
6727 if (rule->state == HCLGE_FD_ACTIVE)
6728 rule->state = HCLGE_FD_TO_ADD;
6729 }
6730 spin_unlock_bh(&hdev->fd_rule_lock);
6731 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
6732
6733 return 0;
6734 }
6735
6736 static int hclge_get_fd_rule_cnt(struct hnae3_handle *handle,
6737 struct ethtool_rxnfc *cmd)
6738 {
6739 struct hclge_vport *vport = hclge_get_vport(handle);
6740 struct hclge_dev *hdev = vport->back;
6741
6742 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev) || hclge_is_cls_flower_active(handle))
6743 return -EOPNOTSUPP;
6744
6745 cmd->rule_cnt = hdev->hclge_fd_rule_num;
6746 cmd->data = hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1];
6747
6748 return 0;
6749 }
6750
6751 static void hclge_fd_get_tcpip4_info(struct hclge_fd_rule *rule,
6752 struct ethtool_tcpip4_spec *spec,
6753 struct ethtool_tcpip4_spec *spec_mask)
6754 {
6755 spec->ip4src = cpu_to_be32(rule->tuples.src_ip[IPV4_INDEX]);
6756 spec_mask->ip4src = rule->unused_tuple & BIT(INNER_SRC_IP) ?
6757 0 : cpu_to_be32(rule->tuples_mask.src_ip[IPV4_INDEX]);
6758
6759 spec->ip4dst = cpu_to_be32(rule->tuples.dst_ip[IPV4_INDEX]);
6760 spec_mask->ip4dst = rule->unused_tuple & BIT(INNER_DST_IP) ?
6761 0 : cpu_to_be32(rule->tuples_mask.dst_ip[IPV4_INDEX]);
6762
6763 spec->psrc = cpu_to_be16(rule->tuples.src_port);
6764 spec_mask->psrc = rule->unused_tuple & BIT(INNER_SRC_PORT) ?
6765 0 : cpu_to_be16(rule->tuples_mask.src_port);
6766
6767 spec->pdst = cpu_to_be16(rule->tuples.dst_port);
6768 spec_mask->pdst = rule->unused_tuple & BIT(INNER_DST_PORT) ?
6769 0 : cpu_to_be16(rule->tuples_mask.dst_port);
6770
6771 spec->tos = rule->tuples.ip_tos;
6772 spec_mask->tos = rule->unused_tuple & BIT(INNER_IP_TOS) ?
6773 0 : rule->tuples_mask.ip_tos;
6774 }
6775
6776 static void hclge_fd_get_ip4_info(struct hclge_fd_rule *rule,
6777 struct ethtool_usrip4_spec *spec,
6778 struct ethtool_usrip4_spec *spec_mask)
6779 {
6780 spec->ip4src = cpu_to_be32(rule->tuples.src_ip[IPV4_INDEX]);
6781 spec_mask->ip4src = rule->unused_tuple & BIT(INNER_SRC_IP) ?
6782 0 : cpu_to_be32(rule->tuples_mask.src_ip[IPV4_INDEX]);
6783
6784 spec->ip4dst = cpu_to_be32(rule->tuples.dst_ip[IPV4_INDEX]);
6785 spec_mask->ip4dst = rule->unused_tuple & BIT(INNER_DST_IP) ?
6786 0 : cpu_to_be32(rule->tuples_mask.dst_ip[IPV4_INDEX]);
6787
6788 spec->tos = rule->tuples.ip_tos;
6789 spec_mask->tos = rule->unused_tuple & BIT(INNER_IP_TOS) ?
6790 0 : rule->tuples_mask.ip_tos;
6791
6792 spec->proto = rule->tuples.ip_proto;
6793 spec_mask->proto = rule->unused_tuple & BIT(INNER_IP_PROTO) ?
6794 0 : rule->tuples_mask.ip_proto;
6795
6796 spec->ip_ver = ETH_RX_NFC_IP4;
6797 }
6798
6799 static void hclge_fd_get_tcpip6_info(struct hclge_fd_rule *rule,
6800 struct ethtool_tcpip6_spec *spec,
6801 struct ethtool_tcpip6_spec *spec_mask)
6802 {
6803 ipv6_addr_cpu_to_be32(spec->ip6src, rule->tuples.src_ip);
6804 ipv6_addr_cpu_to_be32(spec->ip6dst, rule->tuples.dst_ip);
6805 if (rule->unused_tuple & BIT(INNER_SRC_IP))
6806 memset(spec_mask->ip6src, 0, sizeof(spec_mask->ip6src));
6807 else
6808 ipv6_addr_cpu_to_be32(spec_mask->ip6src,
6809 rule->tuples_mask.src_ip);
6810
6811 if (rule->unused_tuple & BIT(INNER_DST_IP))
6812 memset(spec_mask->ip6dst, 0, sizeof(spec_mask->ip6dst));
6813 else
6814 ipv6_addr_cpu_to_be32(spec_mask->ip6dst,
6815 rule->tuples_mask.dst_ip);
6816
6817 spec->tclass = rule->tuples.ip_tos;
6818 spec_mask->tclass = rule->unused_tuple & BIT(INNER_IP_TOS) ?
6819 0 : rule->tuples_mask.ip_tos;
6820
6821 spec->psrc = cpu_to_be16(rule->tuples.src_port);
6822 spec_mask->psrc = rule->unused_tuple & BIT(INNER_SRC_PORT) ?
6823 0 : cpu_to_be16(rule->tuples_mask.src_port);
6824
6825 spec->pdst = cpu_to_be16(rule->tuples.dst_port);
6826 spec_mask->pdst = rule->unused_tuple & BIT(INNER_DST_PORT) ?
6827 0 : cpu_to_be16(rule->tuples_mask.dst_port);
6828 }
6829
6830 static void hclge_fd_get_ip6_info(struct hclge_fd_rule *rule,
6831 struct ethtool_usrip6_spec *spec,
6832 struct ethtool_usrip6_spec *spec_mask)
6833 {
6834 ipv6_addr_cpu_to_be32(spec->ip6src, rule->tuples.src_ip);
6835 ipv6_addr_cpu_to_be32(spec->ip6dst, rule->tuples.dst_ip);
6836 if (rule->unused_tuple & BIT(INNER_SRC_IP))
6837 memset(spec_mask->ip6src, 0, sizeof(spec_mask->ip6src));
6838 else
6839 ipv6_addr_cpu_to_be32(spec_mask->ip6src,
6840 rule->tuples_mask.src_ip);
6841
6842 if (rule->unused_tuple & BIT(INNER_DST_IP))
6843 memset(spec_mask->ip6dst, 0, sizeof(spec_mask->ip6dst));
6844 else
6845 ipv6_addr_cpu_to_be32(spec_mask->ip6dst,
6846 rule->tuples_mask.dst_ip);
6847
6848 spec->tclass = rule->tuples.ip_tos;
6849 spec_mask->tclass = rule->unused_tuple & BIT(INNER_IP_TOS) ?
6850 0 : rule->tuples_mask.ip_tos;
6851
6852 spec->l4_proto = rule->tuples.ip_proto;
6853 spec_mask->l4_proto = rule->unused_tuple & BIT(INNER_IP_PROTO) ?
6854 0 : rule->tuples_mask.ip_proto;
6855 }
6856
6857 static void hclge_fd_get_ether_info(struct hclge_fd_rule *rule,
6858 struct ethhdr *spec,
6859 struct ethhdr *spec_mask)
6860 {
6861 ether_addr_copy(spec->h_source, rule->tuples.src_mac);
6862 ether_addr_copy(spec->h_dest, rule->tuples.dst_mac);
6863
6864 if (rule->unused_tuple & BIT(INNER_SRC_MAC))
6865 eth_zero_addr(spec_mask->h_source);
6866 else
6867 ether_addr_copy(spec_mask->h_source, rule->tuples_mask.src_mac);
6868
6869 if (rule->unused_tuple & BIT(INNER_DST_MAC))
6870 eth_zero_addr(spec_mask->h_dest);
6871 else
6872 ether_addr_copy(spec_mask->h_dest, rule->tuples_mask.dst_mac);
6873
6874 spec->h_proto = cpu_to_be16(rule->tuples.ether_proto);
6875 spec_mask->h_proto = rule->unused_tuple & BIT(INNER_ETH_TYPE) ?
6876 0 : cpu_to_be16(rule->tuples_mask.ether_proto);
6877 }
6878
6879 static void hclge_fd_get_user_def_info(struct ethtool_rx_flow_spec *fs,
6880 struct hclge_fd_rule *rule)
6881 {
6882 if ((rule->unused_tuple & HCLGE_FD_TUPLE_USER_DEF_TUPLES) ==
6883 HCLGE_FD_TUPLE_USER_DEF_TUPLES) {
6884 fs->h_ext.data[0] = 0;
6885 fs->h_ext.data[1] = 0;
6886 fs->m_ext.data[0] = 0;
6887 fs->m_ext.data[1] = 0;
6888 } else {
6889 fs->h_ext.data[0] = cpu_to_be32(rule->ep.user_def.offset);
6890 fs->h_ext.data[1] = cpu_to_be32(rule->ep.user_def.data);
6891 fs->m_ext.data[0] =
6892 cpu_to_be32(HCLGE_FD_USER_DEF_OFFSET_UNMASK);
6893 fs->m_ext.data[1] = cpu_to_be32(rule->ep.user_def.data_mask);
6894 }
6895 }
6896
6897 static void hclge_fd_get_ext_info(struct ethtool_rx_flow_spec *fs,
6898 struct hclge_fd_rule *rule)
6899 {
6900 if (fs->flow_type & FLOW_EXT) {
6901 fs->h_ext.vlan_tci = cpu_to_be16(rule->tuples.vlan_tag1);
6902 fs->m_ext.vlan_tci =
6903 rule->unused_tuple & BIT(INNER_VLAN_TAG_FST) ?
6904 0 : cpu_to_be16(rule->tuples_mask.vlan_tag1);
6905
6906 hclge_fd_get_user_def_info(fs, rule);
6907 }
6908
6909 if (fs->flow_type & FLOW_MAC_EXT) {
6910 ether_addr_copy(fs->h_ext.h_dest, rule->tuples.dst_mac);
6911 if (rule->unused_tuple & BIT(INNER_DST_MAC))
6912 eth_zero_addr(fs->m_u.ether_spec.h_dest);
6913 else
6914 ether_addr_copy(fs->m_u.ether_spec.h_dest,
6915 rule->tuples_mask.dst_mac);
6916 }
6917 }
6918
6919 static struct hclge_fd_rule *hclge_get_fd_rule(struct hclge_dev *hdev,
6920 u16 location)
6921 {
6922 struct hclge_fd_rule *rule = NULL;
6923 struct hlist_node *node2;
6924
6925 hlist_for_each_entry_safe(rule, node2, &hdev->fd_rule_list, rule_node) {
6926 if (rule->location == location)
6927 return rule;
6928 else if (rule->location > location)
6929 return NULL;
6930 }
6931
6932 return NULL;
6933 }
6934
6935 static void hclge_fd_get_ring_cookie(struct ethtool_rx_flow_spec *fs,
6936 struct hclge_fd_rule *rule)
6937 {
6938 if (rule->action == HCLGE_FD_ACTION_DROP_PACKET) {
6939 fs->ring_cookie = RX_CLS_FLOW_DISC;
6940 } else {
6941 u64 vf_id;
6942
6943 fs->ring_cookie = rule->queue_id;
6944 vf_id = rule->vf_id;
6945 vf_id <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
6946 fs->ring_cookie |= vf_id;
6947 }
6948 }
6949
6950 static int hclge_get_fd_rule_info(struct hnae3_handle *handle,
6951 struct ethtool_rxnfc *cmd)
6952 {
6953 struct hclge_vport *vport = hclge_get_vport(handle);
6954 struct hclge_fd_rule *rule = NULL;
6955 struct hclge_dev *hdev = vport->back;
6956 struct ethtool_rx_flow_spec *fs;
6957
6958 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
6959 return -EOPNOTSUPP;
6960
6961 fs = (struct ethtool_rx_flow_spec *)&cmd->fs;
6962
6963 spin_lock_bh(&hdev->fd_rule_lock);
6964
6965 rule = hclge_get_fd_rule(hdev, fs->location);
6966 if (!rule) {
6967 spin_unlock_bh(&hdev->fd_rule_lock);
6968 return -ENOENT;
6969 }
6970
6971 fs->flow_type = rule->flow_type;
6972 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
6973 case SCTP_V4_FLOW:
6974 case TCP_V4_FLOW:
6975 case UDP_V4_FLOW:
6976 hclge_fd_get_tcpip4_info(rule, &fs->h_u.tcp_ip4_spec,
6977 &fs->m_u.tcp_ip4_spec);
6978 break;
6979 case IP_USER_FLOW:
6980 hclge_fd_get_ip4_info(rule, &fs->h_u.usr_ip4_spec,
6981 &fs->m_u.usr_ip4_spec);
6982 break;
6983 case SCTP_V6_FLOW:
6984 case TCP_V6_FLOW:
6985 case UDP_V6_FLOW:
6986 hclge_fd_get_tcpip6_info(rule, &fs->h_u.tcp_ip6_spec,
6987 &fs->m_u.tcp_ip6_spec);
6988 break;
6989 case IPV6_USER_FLOW:
6990 hclge_fd_get_ip6_info(rule, &fs->h_u.usr_ip6_spec,
6991 &fs->m_u.usr_ip6_spec);
6992 break;
6993 /* The flow type of fd rule has been checked before adding in to rule
6994 * list. As other flow types have been handled, it must be ETHER_FLOW
6995 * for the default case
6996 */
6997 default:
6998 hclge_fd_get_ether_info(rule, &fs->h_u.ether_spec,
6999 &fs->m_u.ether_spec);
7000 break;
7001 }
7002
7003 hclge_fd_get_ext_info(fs, rule);
7004
7005 hclge_fd_get_ring_cookie(fs, rule);
7006
7007 spin_unlock_bh(&hdev->fd_rule_lock);
7008
7009 return 0;
7010 }
7011
7012 static int hclge_get_all_rules(struct hnae3_handle *handle,
7013 struct ethtool_rxnfc *cmd, u32 *rule_locs)
7014 {
7015 struct hclge_vport *vport = hclge_get_vport(handle);
7016 struct hclge_dev *hdev = vport->back;
7017 struct hclge_fd_rule *rule;
7018 struct hlist_node *node2;
7019 u32 cnt = 0;
7020
7021 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
7022 return -EOPNOTSUPP;
7023
7024 cmd->data = hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1];
7025
7026 spin_lock_bh(&hdev->fd_rule_lock);
7027 hlist_for_each_entry_safe(rule, node2,
7028 &hdev->fd_rule_list, rule_node) {
7029 if (cnt == cmd->rule_cnt) {
7030 spin_unlock_bh(&hdev->fd_rule_lock);
7031 return -EMSGSIZE;
7032 }
7033
7034 if (rule->state == HCLGE_FD_TO_DEL)
7035 continue;
7036
7037 rule_locs[cnt] = rule->location;
7038 cnt++;
7039 }
7040
7041 spin_unlock_bh(&hdev->fd_rule_lock);
7042
7043 cmd->rule_cnt = cnt;
7044
7045 return 0;
7046 }
7047
7048 static void hclge_fd_get_flow_tuples(const struct flow_keys *fkeys,
7049 struct hclge_fd_rule_tuples *tuples)
7050 {
7051 #define flow_ip6_src fkeys->addrs.v6addrs.src.in6_u.u6_addr32
7052 #define flow_ip6_dst fkeys->addrs.v6addrs.dst.in6_u.u6_addr32
7053
7054 tuples->ether_proto = be16_to_cpu(fkeys->basic.n_proto);
7055 tuples->ip_proto = fkeys->basic.ip_proto;
7056 tuples->dst_port = be16_to_cpu(fkeys->ports.dst);
7057
7058 if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
7059 tuples->src_ip[3] = be32_to_cpu(fkeys->addrs.v4addrs.src);
7060 tuples->dst_ip[3] = be32_to_cpu(fkeys->addrs.v4addrs.dst);
7061 } else {
7062 int i;
7063
7064 for (i = 0; i < IPV6_ADDR_WORDS; i++) {
7065 tuples->src_ip[i] = be32_to_cpu(flow_ip6_src[i]);
7066 tuples->dst_ip[i] = be32_to_cpu(flow_ip6_dst[i]);
7067 }
7068 }
7069 }
7070
7071 /* traverse all rules, check whether an existed rule has the same tuples */
7072 static struct hclge_fd_rule *
7073 hclge_fd_search_flow_keys(struct hclge_dev *hdev,
7074 const struct hclge_fd_rule_tuples *tuples)
7075 {
7076 struct hclge_fd_rule *rule = NULL;
7077 struct hlist_node *node;
7078
7079 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list, rule_node) {
7080 if (!memcmp(tuples, &rule->tuples, sizeof(*tuples)))
7081 return rule;
7082 }
7083
7084 return NULL;
7085 }
7086
7087 static void hclge_fd_build_arfs_rule(const struct hclge_fd_rule_tuples *tuples,
7088 struct hclge_fd_rule *rule)
7089 {
7090 rule->unused_tuple = BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC) |
7091 BIT(INNER_VLAN_TAG_FST) | BIT(INNER_IP_TOS) |
7092 BIT(INNER_SRC_PORT);
7093 rule->action = 0;
7094 rule->vf_id = 0;
7095 rule->rule_type = HCLGE_FD_ARFS_ACTIVE;
7096 rule->state = HCLGE_FD_TO_ADD;
7097 if (tuples->ether_proto == ETH_P_IP) {
7098 if (tuples->ip_proto == IPPROTO_TCP)
7099 rule->flow_type = TCP_V4_FLOW;
7100 else
7101 rule->flow_type = UDP_V4_FLOW;
7102 } else {
7103 if (tuples->ip_proto == IPPROTO_TCP)
7104 rule->flow_type = TCP_V6_FLOW;
7105 else
7106 rule->flow_type = UDP_V6_FLOW;
7107 }
7108 memcpy(&rule->tuples, tuples, sizeof(rule->tuples));
7109 memset(&rule->tuples_mask, 0xFF, sizeof(rule->tuples_mask));
7110 }
7111
7112 static int hclge_add_fd_entry_by_arfs(struct hnae3_handle *handle, u16 queue_id,
7113 u16 flow_id, struct flow_keys *fkeys)
7114 {
7115 struct hclge_vport *vport = hclge_get_vport(handle);
7116 struct hclge_fd_rule_tuples new_tuples = {};
7117 struct hclge_dev *hdev = vport->back;
7118 struct hclge_fd_rule *rule;
7119 u16 bit_id;
7120
7121 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
7122 return -EOPNOTSUPP;
7123
7124 /* when there is already fd rule existed add by user,
7125 * arfs should not work
7126 */
7127 spin_lock_bh(&hdev->fd_rule_lock);
7128 if (hdev->fd_active_type != HCLGE_FD_ARFS_ACTIVE &&
7129 hdev->fd_active_type != HCLGE_FD_RULE_NONE) {
7130 spin_unlock_bh(&hdev->fd_rule_lock);
7131 return -EOPNOTSUPP;
7132 }
7133
7134 hclge_fd_get_flow_tuples(fkeys, &new_tuples);
7135
7136 /* check is there flow director filter existed for this flow,
7137 * if not, create a new filter for it;
7138 * if filter exist with different queue id, modify the filter;
7139 * if filter exist with same queue id, do nothing
7140 */
7141 rule = hclge_fd_search_flow_keys(hdev, &new_tuples);
7142 if (!rule) {
7143 bit_id = find_first_zero_bit(hdev->fd_bmap, MAX_FD_FILTER_NUM);
7144 if (bit_id >= hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1]) {
7145 spin_unlock_bh(&hdev->fd_rule_lock);
7146 return -ENOSPC;
7147 }
7148
7149 rule = kzalloc_obj(*rule, GFP_ATOMIC);
7150 if (!rule) {
7151 spin_unlock_bh(&hdev->fd_rule_lock);
7152 return -ENOMEM;
7153 }
7154
7155 rule->location = bit_id;
7156 rule->arfs.flow_id = flow_id;
7157 rule->queue_id = queue_id;
7158 hclge_fd_build_arfs_rule(&new_tuples, rule);
7159 hclge_update_fd_list(hdev, rule->state, rule->location, rule);
7160 hdev->fd_active_type = HCLGE_FD_ARFS_ACTIVE;
7161 } else if (rule->queue_id != queue_id) {
7162 rule->queue_id = queue_id;
7163 rule->state = HCLGE_FD_TO_ADD;
7164 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
7165 hclge_task_schedule(hdev, 0);
7166 }
7167 spin_unlock_bh(&hdev->fd_rule_lock);
7168 return rule->location;
7169 }
7170
7171 static void hclge_rfs_filter_expire(struct hclge_dev *hdev)
7172 {
7173 #ifdef CONFIG_RFS_ACCEL
7174 struct hnae3_handle *handle = &hdev->vport[0].nic;
7175 struct hclge_fd_rule *rule;
7176 struct hlist_node *node;
7177
7178 spin_lock_bh(&hdev->fd_rule_lock);
7179 if (hdev->fd_active_type != HCLGE_FD_ARFS_ACTIVE) {
7180 spin_unlock_bh(&hdev->fd_rule_lock);
7181 return;
7182 }
7183 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list, rule_node) {
7184 if (rule->state != HCLGE_FD_ACTIVE)
7185 continue;
7186 if (rps_may_expire_flow(handle->netdev, rule->queue_id,
7187 rule->arfs.flow_id, rule->location)) {
7188 rule->state = HCLGE_FD_TO_DEL;
7189 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
7190 }
7191 }
7192 spin_unlock_bh(&hdev->fd_rule_lock);
7193 #endif
7194 }
7195
7196 /* make sure being called after lock up with fd_rule_lock */
7197 static int hclge_clear_arfs_rules(struct hclge_dev *hdev)
7198 {
7199 #ifdef CONFIG_RFS_ACCEL
7200 struct hclge_fd_rule *rule;
7201 struct hlist_node *node;
7202 int ret;
7203
7204 if (hdev->fd_active_type != HCLGE_FD_ARFS_ACTIVE)
7205 return 0;
7206
7207 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list, rule_node) {
7208 switch (rule->state) {
7209 case HCLGE_FD_TO_DEL:
7210 case HCLGE_FD_ACTIVE:
7211 ret = hclge_fd_tcam_config(hdev, HCLGE_FD_STAGE_1, true,
7212 rule->location, NULL, false);
7213 if (ret)
7214 return ret;
7215 fallthrough;
7216 case HCLGE_FD_TO_ADD:
7217 hclge_fd_dec_rule_cnt(hdev, rule->location);
7218 hlist_del(&rule->rule_node);
7219 kfree(rule);
7220 break;
7221 default:
7222 break;
7223 }
7224 }
7225 hclge_sync_fd_state(hdev);
7226
7227 #endif
7228 return 0;
7229 }
7230
7231 static void hclge_get_cls_key_basic(const struct flow_rule *flow,
7232 struct hclge_fd_rule *rule)
7233 {
7234 if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_BASIC)) {
7235 struct flow_match_basic match;
7236 u16 ethtype_key, ethtype_mask;
7237
7238 flow_rule_match_basic(flow, &match);
7239 ethtype_key = ntohs(match.key->n_proto);
7240 ethtype_mask = ntohs(match.mask->n_proto);
7241
7242 if (ethtype_key == ETH_P_ALL) {
7243 ethtype_key = 0;
7244 ethtype_mask = 0;
7245 }
7246 rule->tuples.ether_proto = ethtype_key;
7247 rule->tuples_mask.ether_proto = ethtype_mask;
7248 rule->tuples.ip_proto = match.key->ip_proto;
7249 rule->tuples_mask.ip_proto = match.mask->ip_proto;
7250 } else {
7251 rule->unused_tuple |= BIT(INNER_IP_PROTO);
7252 rule->unused_tuple |= BIT(INNER_ETH_TYPE);
7253 }
7254 }
7255
7256 static void hclge_get_cls_key_mac(const struct flow_rule *flow,
7257 struct hclge_fd_rule *rule)
7258 {
7259 if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7260 struct flow_match_eth_addrs match;
7261
7262 flow_rule_match_eth_addrs(flow, &match);
7263 ether_addr_copy(rule->tuples.dst_mac, match.key->dst);
7264 ether_addr_copy(rule->tuples_mask.dst_mac, match.mask->dst);
7265 ether_addr_copy(rule->tuples.src_mac, match.key->src);
7266 ether_addr_copy(rule->tuples_mask.src_mac, match.mask->src);
7267 } else {
7268 rule->unused_tuple |= BIT(INNER_DST_MAC);
7269 rule->unused_tuple |= BIT(INNER_SRC_MAC);
7270 }
7271 }
7272
7273 static void hclge_get_cls_key_vlan(const struct flow_rule *flow,
7274 struct hclge_fd_rule *rule)
7275 {
7276 if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_VLAN)) {
7277 struct flow_match_vlan match;
7278
7279 flow_rule_match_vlan(flow, &match);
7280 rule->tuples.vlan_tag1 = match.key->vlan_id |
7281 (match.key->vlan_priority << VLAN_PRIO_SHIFT);
7282 rule->tuples_mask.vlan_tag1 = match.mask->vlan_id |
7283 (match.mask->vlan_priority << VLAN_PRIO_SHIFT);
7284 } else {
7285 rule->unused_tuple |= BIT(INNER_VLAN_TAG_FST);
7286 }
7287 }
7288
7289 static int hclge_get_cls_key_ip(const struct flow_rule *flow,
7290 struct hclge_fd_rule *rule,
7291 struct netlink_ext_ack *extack)
7292 {
7293 u16 addr_type = 0;
7294
7295 if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_CONTROL)) {
7296 struct flow_match_control match;
7297
7298 flow_rule_match_control(flow, &match);
7299 addr_type = match.key->addr_type;
7300
7301 if (flow_rule_has_control_flags(match.mask->flags, extack))
7302 return -EOPNOTSUPP;
7303 }
7304
7305 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7306 struct flow_match_ipv4_addrs match;
7307
7308 flow_rule_match_ipv4_addrs(flow, &match);
7309 rule->tuples.src_ip[IPV4_INDEX] = be32_to_cpu(match.key->src);
7310 rule->tuples_mask.src_ip[IPV4_INDEX] =
7311 be32_to_cpu(match.mask->src);
7312 rule->tuples.dst_ip[IPV4_INDEX] = be32_to_cpu(match.key->dst);
7313 rule->tuples_mask.dst_ip[IPV4_INDEX] =
7314 be32_to_cpu(match.mask->dst);
7315 } else if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7316 struct flow_match_ipv6_addrs match;
7317
7318 flow_rule_match_ipv6_addrs(flow, &match);
7319 ipv6_addr_be32_to_cpu(rule->tuples.src_ip,
7320 match.key->src.s6_addr32);
7321 ipv6_addr_be32_to_cpu(rule->tuples_mask.src_ip,
7322 match.mask->src.s6_addr32);
7323 ipv6_addr_be32_to_cpu(rule->tuples.dst_ip,
7324 match.key->dst.s6_addr32);
7325 ipv6_addr_be32_to_cpu(rule->tuples_mask.dst_ip,
7326 match.mask->dst.s6_addr32);
7327 } else {
7328 rule->unused_tuple |= BIT(INNER_SRC_IP);
7329 rule->unused_tuple |= BIT(INNER_DST_IP);
7330 }
7331
7332 return 0;
7333 }
7334
7335 static void hclge_get_cls_key_port(const struct flow_rule *flow,
7336 struct hclge_fd_rule *rule)
7337 {
7338 if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_PORTS)) {
7339 struct flow_match_ports match;
7340
7341 flow_rule_match_ports(flow, &match);
7342
7343 rule->tuples.src_port = be16_to_cpu(match.key->src);
7344 rule->tuples_mask.src_port = be16_to_cpu(match.mask->src);
7345 rule->tuples.dst_port = be16_to_cpu(match.key->dst);
7346 rule->tuples_mask.dst_port = be16_to_cpu(match.mask->dst);
7347 } else {
7348 rule->unused_tuple |= BIT(INNER_SRC_PORT);
7349 rule->unused_tuple |= BIT(INNER_DST_PORT);
7350 }
7351 }
7352
7353 static int hclge_parse_cls_flower(struct hclge_dev *hdev,
7354 struct flow_cls_offload *cls_flower,
7355 struct hclge_fd_rule *rule)
7356 {
7357 struct flow_rule *flow = flow_cls_offload_flow_rule(cls_flower);
7358 struct netlink_ext_ack *extack = cls_flower->common.extack;
7359 struct flow_dissector *dissector = flow->match.dissector;
7360 int ret;
7361
7362 if (dissector->used_keys &
7363 ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
7364 BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
7365 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7366 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
7367 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7368 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7369 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS))) {
7370 dev_err(&hdev->pdev->dev, "unsupported key set: %#llx\n",
7371 dissector->used_keys);
7372 return -EOPNOTSUPP;
7373 }
7374
7375 hclge_get_cls_key_basic(flow, rule);
7376 hclge_get_cls_key_mac(flow, rule);
7377 hclge_get_cls_key_vlan(flow, rule);
7378
7379 ret = hclge_get_cls_key_ip(flow, rule, extack);
7380 if (ret)
7381 return ret;
7382
7383 hclge_get_cls_key_port(flow, rule);
7384
7385 return 0;
7386 }
7387
7388 static int hclge_check_cls_flower(struct hclge_dev *hdev,
7389 struct flow_cls_offload *cls_flower, int tc)
7390 {
7391 u32 prio = cls_flower->common.prio;
7392
7393 if (tc < 0 || tc > hdev->tc_max) {
7394 dev_err(&hdev->pdev->dev, "invalid traffic class\n");
7395 return -EINVAL;
7396 }
7397
7398 if (prio == 0 ||
7399 prio > hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1]) {
7400 dev_err(&hdev->pdev->dev,
7401 "prio %u should be in range[1, %u]\n",
7402 prio, hdev->fd_cfg.rule_num[HCLGE_FD_STAGE_1]);
7403 return -EINVAL;
7404 }
7405
7406 if (test_bit(prio - 1, hdev->fd_bmap)) {
7407 dev_err(&hdev->pdev->dev, "prio %u is already used\n", prio);
7408 return -EINVAL;
7409 }
7410 return 0;
7411 }
7412
7413 static int hclge_add_cls_flower(struct hnae3_handle *handle,
7414 struct flow_cls_offload *cls_flower,
7415 int tc)
7416 {
7417 struct hclge_vport *vport = hclge_get_vport(handle);
7418 struct hclge_dev *hdev = vport->back;
7419 struct hclge_fd_rule *rule;
7420 int ret;
7421
7422 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev)) {
7423 dev_err(&hdev->pdev->dev,
7424 "cls flower is not supported\n");
7425 return -EOPNOTSUPP;
7426 }
7427
7428 ret = hclge_check_cls_flower(hdev, cls_flower, tc);
7429 if (ret) {
7430 dev_err(&hdev->pdev->dev,
7431 "failed to check cls flower params, ret = %d\n", ret);
7432 return ret;
7433 }
7434
7435 rule = kzalloc_obj(*rule);
7436 if (!rule)
7437 return -ENOMEM;
7438
7439 ret = hclge_parse_cls_flower(hdev, cls_flower, rule);
7440 if (ret) {
7441 kfree(rule);
7442 return ret;
7443 }
7444
7445 rule->action = HCLGE_FD_ACTION_SELECT_TC;
7446 rule->cls_flower.tc = tc;
7447 rule->location = cls_flower->common.prio - 1;
7448 rule->vf_id = 0;
7449 rule->cls_flower.cookie = cls_flower->cookie;
7450 rule->rule_type = HCLGE_FD_TC_FLOWER_ACTIVE;
7451
7452 ret = hclge_add_fd_entry_common(hdev, rule);
7453 if (ret)
7454 kfree(rule);
7455
7456 return ret;
7457 }
7458
7459 static struct hclge_fd_rule *hclge_find_cls_flower(struct hclge_dev *hdev,
7460 unsigned long cookie)
7461 {
7462 struct hclge_fd_rule *rule;
7463 struct hlist_node *node;
7464
7465 hlist_for_each_entry_safe(rule, node, &hdev->fd_rule_list, rule_node) {
7466 if (rule->cls_flower.cookie == cookie)
7467 return rule;
7468 }
7469
7470 return NULL;
7471 }
7472
7473 static int hclge_del_cls_flower(struct hnae3_handle *handle,
7474 struct flow_cls_offload *cls_flower)
7475 {
7476 struct hclge_vport *vport = hclge_get_vport(handle);
7477 struct hclge_dev *hdev = vport->back;
7478 struct hclge_fd_rule *rule;
7479 int ret;
7480
7481 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
7482 return -EOPNOTSUPP;
7483
7484 spin_lock_bh(&hdev->fd_rule_lock);
7485
7486 rule = hclge_find_cls_flower(hdev, cls_flower->cookie);
7487 if (!rule) {
7488 spin_unlock_bh(&hdev->fd_rule_lock);
7489 return -EINVAL;
7490 }
7491
7492 ret = hclge_fd_tcam_config(hdev, HCLGE_FD_STAGE_1, true, rule->location,
7493 NULL, false);
7494 if (ret) {
7495 /* if tcam config fail, set rule state to TO_DEL,
7496 * so the rule will be deleted when periodic
7497 * task being scheduled.
7498 */
7499 hclge_update_fd_list(hdev, HCLGE_FD_TO_DEL, rule->location, NULL);
7500 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
7501 spin_unlock_bh(&hdev->fd_rule_lock);
7502 return ret;
7503 }
7504
7505 hclge_update_fd_list(hdev, HCLGE_FD_DELETED, rule->location, NULL);
7506 spin_unlock_bh(&hdev->fd_rule_lock);
7507
7508 return 0;
7509 }
7510
7511 static void hclge_sync_fd_list(struct hclge_dev *hdev, struct hlist_head *hlist)
7512 {
7513 struct hclge_fd_rule *rule;
7514 struct hlist_node *node;
7515 int ret = 0;
7516
7517 if (!test_and_clear_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state))
7518 return;
7519
7520 spin_lock_bh(&hdev->fd_rule_lock);
7521
7522 hlist_for_each_entry_safe(rule, node, hlist, rule_node) {
7523 switch (rule->state) {
7524 case HCLGE_FD_TO_ADD:
7525 ret = hclge_fd_config_rule(hdev, rule);
7526 if (ret)
7527 goto out;
7528 rule->state = HCLGE_FD_ACTIVE;
7529 break;
7530 case HCLGE_FD_TO_DEL:
7531 ret = hclge_fd_tcam_config(hdev, HCLGE_FD_STAGE_1, true,
7532 rule->location, NULL, false);
7533 if (ret)
7534 goto out;
7535 hclge_fd_dec_rule_cnt(hdev, rule->location);
7536 hclge_fd_free_node(hdev, rule);
7537 break;
7538 default:
7539 break;
7540 }
7541 }
7542
7543 out:
7544 if (ret)
7545 set_bit(HCLGE_STATE_FD_TBL_CHANGED, &hdev->state);
7546
7547 spin_unlock_bh(&hdev->fd_rule_lock);
7548 }
7549
7550 static void hclge_sync_fd_table(struct hclge_dev *hdev)
7551 {
7552 if (!hnae3_ae_dev_fd_supported(hdev->ae_dev))
7553 return;
7554
7555 if (test_and_clear_bit(HCLGE_STATE_FD_CLEAR_ALL, &hdev->state)) {
7556 bool clear_list = hdev->fd_active_type == HCLGE_FD_ARFS_ACTIVE;
7557
7558 hclge_clear_fd_rules_in_list(hdev, clear_list);
7559 }
7560
7561 hclge_sync_fd_user_def_cfg(hdev, false);
7562
7563 hclge_sync_fd_list(hdev, &hdev->fd_rule_list);
7564 }
7565
7566 static bool hclge_get_hw_reset_stat(struct hnae3_handle *handle)
7567 {
7568 struct hclge_vport *vport = hclge_get_vport(handle);
7569 struct hclge_dev *hdev = vport->back;
7570
7571 return hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG) ||
7572 hclge_read_dev(&hdev->hw, HCLGE_FUN_RST_ING);
7573 }
7574
7575 static bool hclge_get_cmdq_stat(struct hnae3_handle *handle)
7576 {
7577 struct hclge_vport *vport = hclge_get_vport(handle);
7578 struct hclge_dev *hdev = vport->back;
7579
7580 return test_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
7581 }
7582
7583 static bool hclge_ae_dev_resetting(struct hnae3_handle *handle)
7584 {
7585 struct hclge_vport *vport = hclge_get_vport(handle);
7586 struct hclge_dev *hdev = vport->back;
7587
7588 return test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
7589 }
7590
7591 static unsigned long hclge_ae_dev_reset_cnt(struct hnae3_handle *handle)
7592 {
7593 struct hclge_vport *vport = hclge_get_vport(handle);
7594 struct hclge_dev *hdev = vport->back;
7595
7596 return hdev->rst_stats.hw_reset_done_cnt;
7597 }
7598
7599 static void hclge_enable_fd(struct hnae3_handle *handle, bool enable)
7600 {
7601 struct hclge_vport *vport = hclge_get_vport(handle);
7602 struct hclge_dev *hdev = vport->back;
7603
7604 hdev->fd_en = enable;
7605
7606 if (!enable)
7607 set_bit(HCLGE_STATE_FD_CLEAR_ALL, &hdev->state);
7608 else
7609 hclge_restore_fd_entries(handle);
7610
7611 hclge_task_schedule(hdev, 0);
7612 }
7613
7614 static void hclge_cfg_mac_mode(struct hclge_dev *hdev, bool enable)
7615 {
7616 #define HCLGE_LINK_STATUS_WAIT_CNT 3
7617
7618 struct hclge_desc desc;
7619 struct hclge_config_mac_mode_cmd *req =
7620 (struct hclge_config_mac_mode_cmd *)desc.data;
7621 u32 loop_en = 0;
7622 int ret;
7623
7624 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAC_MODE, false);
7625
7626 if (enable) {
7627 hnae3_set_bit(loop_en, HCLGE_MAC_TX_EN_B, 1U);
7628 hnae3_set_bit(loop_en, HCLGE_MAC_RX_EN_B, 1U);
7629 hnae3_set_bit(loop_en, HCLGE_MAC_PAD_TX_B, 1U);
7630 hnae3_set_bit(loop_en, HCLGE_MAC_PAD_RX_B, 1U);
7631 hnae3_set_bit(loop_en, HCLGE_MAC_FCS_TX_B, 1U);
7632 hnae3_set_bit(loop_en, HCLGE_MAC_RX_FCS_B, 1U);
7633 hnae3_set_bit(loop_en, HCLGE_MAC_RX_FCS_STRIP_B, 1U);
7634 hnae3_set_bit(loop_en, HCLGE_MAC_TX_OVERSIZE_TRUNCATE_B, 1U);
7635 hnae3_set_bit(loop_en, HCLGE_MAC_RX_OVERSIZE_TRUNCATE_B, 1U);
7636 hnae3_set_bit(loop_en, HCLGE_MAC_TX_UNDER_MIN_ERR_B, 1U);
7637 }
7638
7639 req->txrx_pad_fcs_loop_en = cpu_to_le32(loop_en);
7640
7641 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7642 if (ret) {
7643 dev_err(&hdev->pdev->dev,
7644 "mac enable fail, ret =%d.\n", ret);
7645 return;
7646 }
7647
7648 if (!enable)
7649 hclge_mac_link_status_wait(hdev, HCLGE_LINK_STATUS_DOWN,
7650 HCLGE_LINK_STATUS_WAIT_CNT);
7651 }
7652
7653 static int hclge_config_switch_param(struct hclge_dev *hdev, int vfid,
7654 u8 switch_param, u8 param_mask)
7655 {
7656 struct hclge_mac_vlan_switch_cmd *req;
7657 struct hclge_desc desc;
7658 u32 func_id;
7659 int ret;
7660
7661 func_id = hclge_get_port_number(HOST_PORT, 0, vfid, 0);
7662 req = (struct hclge_mac_vlan_switch_cmd *)desc.data;
7663
7664 /* read current config parameter */
7665 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_SWITCH_PARAM,
7666 true);
7667 req->roce_sel = HCLGE_MAC_VLAN_NIC_SEL;
7668 req->func_id = cpu_to_le32(func_id);
7669
7670 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7671 if (ret) {
7672 dev_err(&hdev->pdev->dev,
7673 "read mac vlan switch parameter fail, ret = %d\n", ret);
7674 return ret;
7675 }
7676
7677 /* modify and write new config parameter */
7678 hclge_comm_cmd_reuse_desc(&desc, false);
7679 req->switch_param = (req->switch_param & param_mask) | switch_param;
7680 req->param_mask = param_mask;
7681
7682 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7683 if (ret)
7684 dev_err(&hdev->pdev->dev,
7685 "set mac vlan switch parameter fail, ret = %d\n", ret);
7686 return ret;
7687 }
7688
7689 static void hclge_phy_link_status_wait(struct hclge_dev *hdev,
7690 int link_ret)
7691 {
7692 #define HCLGE_PHY_LINK_STATUS_NUM 200
7693
7694 struct phy_device *phydev = hdev->hw.mac.phydev;
7695 int i = 0;
7696 int ret;
7697
7698 do {
7699 ret = phy_read_status(phydev);
7700 if (ret) {
7701 dev_err(&hdev->pdev->dev,
7702 "phy update link status fail, ret = %d\n", ret);
7703 return;
7704 }
7705
7706 if (phydev->link == link_ret)
7707 break;
7708
7709 msleep(HCLGE_LINK_STATUS_MS);
7710 } while (++i < HCLGE_PHY_LINK_STATUS_NUM);
7711 }
7712
7713 static int hclge_mac_link_status_wait(struct hclge_dev *hdev, int link_ret,
7714 int wait_cnt)
7715 {
7716 int link_status;
7717 int i = 0;
7718 int ret;
7719
7720 do {
7721 ret = hclge_get_mac_link_status(hdev, &link_status);
7722 if (ret)
7723 return ret;
7724 if (link_status == link_ret)
7725 return 0;
7726
7727 msleep(HCLGE_LINK_STATUS_MS);
7728 } while (++i < wait_cnt);
7729 return -EBUSY;
7730 }
7731
7732 static int hclge_mac_phy_link_status_wait(struct hclge_dev *hdev, bool en,
7733 bool is_phy)
7734 {
7735 #define HCLGE_MAC_LINK_STATUS_NUM 100
7736
7737 int link_ret;
7738
7739 link_ret = en ? HCLGE_LINK_STATUS_UP : HCLGE_LINK_STATUS_DOWN;
7740
7741 if (is_phy)
7742 hclge_phy_link_status_wait(hdev, link_ret);
7743
7744 return hclge_mac_link_status_wait(hdev, link_ret,
7745 HCLGE_MAC_LINK_STATUS_NUM);
7746 }
7747
7748 static int hclge_set_app_loopback(struct hclge_dev *hdev, bool en)
7749 {
7750 struct hclge_config_mac_mode_cmd *req;
7751 struct hclge_desc desc;
7752 u32 loop_en;
7753 int ret;
7754
7755 req = (struct hclge_config_mac_mode_cmd *)&desc.data[0];
7756 /* 1 Read out the MAC mode config at first */
7757 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAC_MODE, true);
7758 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7759 if (ret) {
7760 dev_err(&hdev->pdev->dev,
7761 "mac loopback get fail, ret =%d.\n", ret);
7762 return ret;
7763 }
7764
7765 /* 2 Then setup the loopback flag */
7766 loop_en = le32_to_cpu(req->txrx_pad_fcs_loop_en);
7767 hnae3_set_bit(loop_en, HCLGE_MAC_APP_LP_B, en ? 1 : 0);
7768
7769 req->txrx_pad_fcs_loop_en = cpu_to_le32(loop_en);
7770
7771 /* 3 Config mac work mode with loopback flag
7772 * and its original configure parameters
7773 */
7774 hclge_comm_cmd_reuse_desc(&desc, false);
7775 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7776 if (ret)
7777 dev_err(&hdev->pdev->dev,
7778 "mac loopback set fail, ret =%d.\n", ret);
7779 return ret;
7780 }
7781
7782 static int hclge_cfg_common_loopback_cmd_send(struct hclge_dev *hdev, bool en,
7783 enum hnae3_loop loop_mode)
7784 {
7785 struct hclge_common_lb_cmd *req;
7786 struct hclge_desc desc;
7787 u8 loop_mode_b;
7788 int ret;
7789
7790 req = (struct hclge_common_lb_cmd *)desc.data;
7791 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_COMMON_LOOPBACK, false);
7792
7793 switch (loop_mode) {
7794 case HNAE3_LOOP_SERIAL_SERDES:
7795 loop_mode_b = HCLGE_CMD_SERDES_SERIAL_INNER_LOOP_B;
7796 break;
7797 case HNAE3_LOOP_PARALLEL_SERDES:
7798 loop_mode_b = HCLGE_CMD_SERDES_PARALLEL_INNER_LOOP_B;
7799 break;
7800 case HNAE3_LOOP_PHY:
7801 loop_mode_b = HCLGE_CMD_GE_PHY_INNER_LOOP_B;
7802 break;
7803 default:
7804 dev_err(&hdev->pdev->dev,
7805 "unsupported loopback mode %d\n", loop_mode);
7806 return -ENOTSUPP;
7807 }
7808
7809 req->mask = loop_mode_b;
7810 if (en)
7811 req->enable = loop_mode_b;
7812
7813 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7814 if (ret)
7815 dev_err(&hdev->pdev->dev,
7816 "failed to send loopback cmd, loop_mode = %d, ret = %d\n",
7817 loop_mode, ret);
7818
7819 return ret;
7820 }
7821
7822 static int hclge_cfg_common_loopback_wait(struct hclge_dev *hdev)
7823 {
7824 #define HCLGE_COMMON_LB_RETRY_MS 10
7825 #define HCLGE_COMMON_LB_RETRY_NUM 100
7826
7827 struct hclge_common_lb_cmd *req;
7828 struct hclge_desc desc;
7829 u32 i = 0;
7830 int ret;
7831
7832 req = (struct hclge_common_lb_cmd *)desc.data;
7833
7834 do {
7835 msleep(HCLGE_COMMON_LB_RETRY_MS);
7836 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_COMMON_LOOPBACK,
7837 true);
7838 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7839 if (ret) {
7840 dev_err(&hdev->pdev->dev,
7841 "failed to get loopback done status, ret = %d\n",
7842 ret);
7843 return ret;
7844 }
7845 } while (++i < HCLGE_COMMON_LB_RETRY_NUM &&
7846 !(req->result & HCLGE_CMD_COMMON_LB_DONE_B));
7847
7848 if (!(req->result & HCLGE_CMD_COMMON_LB_DONE_B)) {
7849 dev_err(&hdev->pdev->dev, "wait loopback timeout\n");
7850 return -EBUSY;
7851 } else if (!(req->result & HCLGE_CMD_COMMON_LB_SUCCESS_B)) {
7852 dev_err(&hdev->pdev->dev, "failed to do loopback test\n");
7853 return -EIO;
7854 }
7855
7856 return 0;
7857 }
7858
7859 static int hclge_cfg_common_loopback(struct hclge_dev *hdev, bool en,
7860 enum hnae3_loop loop_mode)
7861 {
7862 int ret;
7863
7864 ret = hclge_cfg_common_loopback_cmd_send(hdev, en, loop_mode);
7865 if (ret)
7866 return ret;
7867
7868 return hclge_cfg_common_loopback_wait(hdev);
7869 }
7870
7871 static int hclge_set_common_loopback(struct hclge_dev *hdev, bool en,
7872 enum hnae3_loop loop_mode)
7873 {
7874 int ret;
7875
7876 ret = hclge_cfg_common_loopback(hdev, en, loop_mode);
7877 if (ret)
7878 return ret;
7879
7880 hclge_cfg_mac_mode(hdev, en);
7881
7882 ret = hclge_mac_phy_link_status_wait(hdev, en, false);
7883 if (ret)
7884 dev_err(&hdev->pdev->dev,
7885 "serdes loopback config mac mode timeout\n");
7886
7887 return ret;
7888 }
7889
7890 static int hclge_enable_phy_loopback(struct hclge_dev *hdev,
7891 struct phy_device *phydev)
7892 {
7893 int ret;
7894
7895 if (!phydev->suspended) {
7896 ret = phy_suspend(phydev);
7897 if (ret)
7898 return ret;
7899 }
7900
7901 ret = phy_resume(phydev);
7902 if (ret)
7903 return ret;
7904
7905 return phy_loopback(phydev, true, 0);
7906 }
7907
7908 static int hclge_disable_phy_loopback(struct hclge_dev *hdev,
7909 struct phy_device *phydev)
7910 {
7911 int ret;
7912
7913 ret = phy_loopback(phydev, false, 0);
7914 if (ret)
7915 return ret;
7916
7917 return phy_suspend(phydev);
7918 }
7919
7920 static int hclge_set_phy_loopback(struct hclge_dev *hdev, bool en)
7921 {
7922 struct phy_device *phydev = hdev->hw.mac.phydev;
7923 int ret;
7924
7925 if (!phydev) {
7926 if (hnae3_dev_phy_imp_supported(hdev))
7927 return hclge_set_common_loopback(hdev, en,
7928 HNAE3_LOOP_PHY);
7929 return -ENOTSUPP;
7930 }
7931
7932 if (en)
7933 ret = hclge_enable_phy_loopback(hdev, phydev);
7934 else
7935 ret = hclge_disable_phy_loopback(hdev, phydev);
7936 if (ret) {
7937 dev_err(&hdev->pdev->dev,
7938 "set phy loopback fail, ret = %d\n", ret);
7939 return ret;
7940 }
7941
7942 hclge_cfg_mac_mode(hdev, en);
7943
7944 ret = hclge_mac_phy_link_status_wait(hdev, en, true);
7945 if (ret)
7946 dev_err(&hdev->pdev->dev,
7947 "phy loopback config mac mode timeout\n");
7948
7949 return ret;
7950 }
7951
7952 static int hclge_tqp_enable_cmd_send(struct hclge_dev *hdev, u16 tqp_id,
7953 u16 stream_id, bool enable)
7954 {
7955 struct hclge_desc desc;
7956 struct hclge_cfg_com_tqp_queue_cmd *req =
7957 (struct hclge_cfg_com_tqp_queue_cmd *)desc.data;
7958
7959 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_COM_TQP_QUEUE, false);
7960 req->tqp_id = cpu_to_le16(tqp_id);
7961 req->stream_id = cpu_to_le16(stream_id);
7962 if (enable)
7963 req->enable |= 1U << HCLGE_TQP_ENABLE_B;
7964
7965 return hclge_cmd_send(&hdev->hw, &desc, 1);
7966 }
7967
7968 static int hclge_tqp_enable(struct hnae3_handle *handle, bool enable)
7969 {
7970 struct hclge_vport *vport = hclge_get_vport(handle);
7971 struct hclge_dev *hdev = vport->back;
7972 int ret;
7973 u16 i;
7974
7975 for (i = 0; i < handle->kinfo.num_tqps; i++) {
7976 ret = hclge_tqp_enable_cmd_send(hdev, i, 0, enable);
7977 if (ret)
7978 return ret;
7979 }
7980 return 0;
7981 }
7982
7983 static int hclge_set_loopback(struct hnae3_handle *handle,
7984 enum hnae3_loop loop_mode, bool en)
7985 {
7986 struct hclge_vport *vport = hclge_get_vport(handle);
7987 struct hclge_dev *hdev = vport->back;
7988 int ret = 0;
7989
7990 /* Loopback can be enabled in three places: SSU, MAC, and serdes. By
7991 * default, SSU loopback is enabled, so if the SMAC and the DMAC are
7992 * the same, the packets are looped back in the SSU. If SSU loopback
7993 * is disabled, packets can reach MAC even if SMAC is the same as DMAC.
7994 */
7995 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
7996 u8 switch_param = en ? 0 : BIT(HCLGE_SWITCH_ALW_LPBK_B);
7997
7998 ret = hclge_config_switch_param(hdev, PF_VPORT_ID, switch_param,
7999 HCLGE_SWITCH_ALW_LPBK_MASK);
8000 if (ret)
8001 return ret;
8002 }
8003
8004 switch (loop_mode) {
8005 case HNAE3_LOOP_APP:
8006 ret = hclge_set_app_loopback(hdev, en);
8007 break;
8008 case HNAE3_LOOP_SERIAL_SERDES:
8009 case HNAE3_LOOP_PARALLEL_SERDES:
8010 ret = hclge_set_common_loopback(hdev, en, loop_mode);
8011 break;
8012 case HNAE3_LOOP_PHY:
8013 ret = hclge_set_phy_loopback(hdev, en);
8014 break;
8015 case HNAE3_LOOP_EXTERNAL:
8016 break;
8017 default:
8018 ret = -ENOTSUPP;
8019 dev_err(&hdev->pdev->dev,
8020 "loop_mode %d is not supported\n", loop_mode);
8021 break;
8022 }
8023
8024 if (ret)
8025 return ret;
8026
8027 ret = hclge_tqp_enable(handle, en);
8028 if (ret)
8029 dev_err(&hdev->pdev->dev, "failed to %s tqp in loopback, ret = %d\n",
8030 str_enable_disable(en), ret);
8031
8032 return ret;
8033 }
8034
8035 static int hclge_set_default_loopback(struct hclge_dev *hdev)
8036 {
8037 int ret;
8038
8039 ret = hclge_set_app_loopback(hdev, false);
8040 if (ret)
8041 return ret;
8042
8043 ret = hclge_cfg_common_loopback(hdev, false, HNAE3_LOOP_SERIAL_SERDES);
8044 if (ret)
8045 return ret;
8046
8047 return hclge_cfg_common_loopback(hdev, false,
8048 HNAE3_LOOP_PARALLEL_SERDES);
8049 }
8050
8051 static void hclge_flush_link_update(struct hclge_dev *hdev)
8052 {
8053 #define HCLGE_FLUSH_LINK_TIMEOUT 100000
8054
8055 unsigned long last = hdev->serv_processed_cnt;
8056 int i = 0;
8057
8058 while (test_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state) &&
8059 i++ < HCLGE_FLUSH_LINK_TIMEOUT &&
8060 last == hdev->serv_processed_cnt)
8061 usleep_range(1, 1);
8062 }
8063
8064 static void hclge_set_timer_task(struct hnae3_handle *handle, bool enable)
8065 {
8066 struct hclge_vport *vport = hclge_get_vport(handle);
8067 struct hclge_dev *hdev = vport->back;
8068
8069 if (enable) {
8070 hclge_task_schedule(hdev, 0);
8071 } else {
8072 /* Set the DOWN flag here to disable link updating */
8073 set_bit(HCLGE_STATE_DOWN, &hdev->state);
8074
8075 smp_mb__after_atomic(); /* flush memory to make sure DOWN is seen by service task */
8076 hclge_flush_link_update(hdev);
8077 }
8078 }
8079
8080 static int hclge_ae_start(struct hnae3_handle *handle)
8081 {
8082 struct hclge_vport *vport = hclge_get_vport(handle);
8083 struct hclge_dev *hdev = vport->back;
8084
8085 /* mac enable */
8086 hclge_cfg_mac_mode(hdev, true);
8087 clear_bit(HCLGE_STATE_DOWN, &hdev->state);
8088 hdev->hw.mac.link = 0;
8089
8090 /* reset tqp stats */
8091 hclge_comm_reset_tqp_stats(handle);
8092
8093 hclge_mac_start_phy(hdev);
8094
8095 return 0;
8096 }
8097
8098 static void hclge_ae_stop(struct hnae3_handle *handle)
8099 {
8100 struct hclge_vport *vport = hclge_get_vport(handle);
8101 struct hclge_dev *hdev = vport->back;
8102
8103 set_bit(HCLGE_STATE_DOWN, &hdev->state);
8104 spin_lock_bh(&hdev->fd_rule_lock);
8105 hclge_clear_arfs_rules(hdev);
8106 spin_unlock_bh(&hdev->fd_rule_lock);
8107
8108 /* If it is not PF reset or FLR, the firmware will disable the MAC,
8109 * so it only need to stop phy here.
8110 */
8111 if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state)) {
8112 hclge_pfc_pause_en_cfg(hdev, HCLGE_PFC_TX_RX_DISABLE,
8113 HCLGE_PFC_DISABLE);
8114 if (hdev->reset_type != HNAE3_FUNC_RESET &&
8115 hdev->reset_type != HNAE3_FLR_RESET) {
8116 hclge_mac_stop_phy(hdev);
8117 hclge_update_link_status(hdev);
8118 return;
8119 }
8120 }
8121
8122 hclge_reset_tqp(handle);
8123
8124 hclge_config_mac_tnl_int(hdev, false);
8125
8126 /* Mac disable */
8127 hclge_cfg_mac_mode(hdev, false);
8128
8129 hclge_mac_stop_phy(hdev);
8130
8131 /* reset tqp stats */
8132 hclge_comm_reset_tqp_stats(handle);
8133 hclge_update_link_status(hdev);
8134 }
8135
8136 int hclge_vport_start(struct hclge_vport *vport)
8137 {
8138 struct hclge_dev *hdev = vport->back;
8139
8140 set_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
8141 set_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
8142 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
8143 vport->last_active_jiffies = jiffies;
8144 vport->need_notify = 0;
8145
8146 if (test_bit(vport->vport_id, hdev->vport_config_block)) {
8147 if (vport->vport_id) {
8148 hclge_restore_mac_table_common(vport);
8149 hclge_restore_vport_vlan_table(vport);
8150 } else {
8151 hclge_restore_hw_table(hdev);
8152 }
8153 }
8154
8155 clear_bit(vport->vport_id, hdev->vport_config_block);
8156
8157 return 0;
8158 }
8159
8160 void hclge_vport_stop(struct hclge_vport *vport)
8161 {
8162 clear_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
8163 clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
8164 vport->need_notify = 0;
8165 }
8166
8167 static int hclge_client_start(struct hnae3_handle *handle)
8168 {
8169 struct hclge_vport *vport = hclge_get_vport(handle);
8170
8171 return hclge_vport_start(vport);
8172 }
8173
8174 static void hclge_client_stop(struct hnae3_handle *handle)
8175 {
8176 struct hclge_vport *vport = hclge_get_vport(handle);
8177
8178 hclge_vport_stop(vport);
8179 }
8180
8181 static int hclge_get_mac_vlan_cmd_status(struct hclge_vport *vport,
8182 u16 cmdq_resp, u8 resp_code,
8183 enum hclge_mac_vlan_tbl_opcode op)
8184 {
8185 struct hclge_dev *hdev = vport->back;
8186
8187 if (cmdq_resp) {
8188 dev_err(&hdev->pdev->dev,
8189 "cmdq execute failed for get_mac_vlan_cmd_status,status=%u.\n",
8190 cmdq_resp);
8191 return -EIO;
8192 }
8193
8194 if (op == HCLGE_MAC_VLAN_ADD) {
8195 if (!resp_code || resp_code == 1)
8196 return 0;
8197 else if (resp_code == HCLGE_ADD_UC_OVERFLOW ||
8198 resp_code == HCLGE_ADD_MC_OVERFLOW)
8199 return -ENOSPC;
8200
8201 dev_err(&hdev->pdev->dev,
8202 "add mac addr failed for undefined, code=%u.\n",
8203 resp_code);
8204 return -EIO;
8205 } else if (op == HCLGE_MAC_VLAN_REMOVE) {
8206 if (!resp_code) {
8207 return 0;
8208 } else if (resp_code == 1) {
8209 dev_dbg(&hdev->pdev->dev,
8210 "remove mac addr failed for miss.\n");
8211 return -ENOENT;
8212 }
8213
8214 dev_err(&hdev->pdev->dev,
8215 "remove mac addr failed for undefined, code=%u.\n",
8216 resp_code);
8217 return -EIO;
8218 } else if (op == HCLGE_MAC_VLAN_LKUP) {
8219 if (!resp_code) {
8220 return 0;
8221 } else if (resp_code == 1) {
8222 dev_dbg(&hdev->pdev->dev,
8223 "lookup mac addr failed for miss.\n");
8224 return -ENOENT;
8225 }
8226
8227 dev_err(&hdev->pdev->dev,
8228 "lookup mac addr failed for undefined, code=%u.\n",
8229 resp_code);
8230 return -EIO;
8231 }
8232
8233 dev_err(&hdev->pdev->dev,
8234 "unknown opcode for get_mac_vlan_cmd_status, opcode=%d.\n", op);
8235
8236 return -EINVAL;
8237 }
8238
8239 static int hclge_update_desc_vfid(struct hclge_desc *desc, int vfid, bool clr)
8240 {
8241 #define HCLGE_VF_NUM_IN_FIRST_DESC 192
8242
8243 unsigned int word_num;
8244 unsigned int bit_num;
8245
8246 if (vfid > 255 || vfid < 0)
8247 return -EIO;
8248
8249 if (vfid >= 0 && vfid < HCLGE_VF_NUM_IN_FIRST_DESC) {
8250 word_num = vfid / 32;
8251 bit_num = vfid % 32;
8252 if (clr)
8253 desc[1].data[word_num] &= cpu_to_le32(~(1U << bit_num));
8254 else
8255 desc[1].data[word_num] |= cpu_to_le32(1 << bit_num);
8256 } else {
8257 word_num = (vfid - HCLGE_VF_NUM_IN_FIRST_DESC) / 32;
8258 bit_num = vfid % 32;
8259 if (clr)
8260 desc[2].data[word_num] &= cpu_to_le32(~(1U << bit_num));
8261 else
8262 desc[2].data[word_num] |= cpu_to_le32(1 << bit_num);
8263 }
8264
8265 return 0;
8266 }
8267
8268 static bool hclge_is_all_function_id_zero(struct hclge_desc *desc)
8269 {
8270 #define HCLGE_DESC_NUMBER 3
8271 #define HCLGE_FUNC_NUMBER_PER_DESC 6
8272 int i, j;
8273
8274 for (i = 1; i < HCLGE_DESC_NUMBER; i++)
8275 for (j = 0; j < HCLGE_FUNC_NUMBER_PER_DESC; j++)
8276 if (desc[i].data[j])
8277 return false;
8278
8279 return true;
8280 }
8281
8282 static void hclge_prepare_mac_addr(struct hclge_mac_vlan_tbl_entry_cmd *new_req,
8283 const u8 *addr, bool is_mc)
8284 {
8285 const unsigned char *mac_addr = addr;
8286 u32 high_val = mac_addr[2] << 16 | (mac_addr[3] << 24) |
8287 (mac_addr[0]) | (mac_addr[1] << 8);
8288 u32 low_val = mac_addr[4] | (mac_addr[5] << 8);
8289
8290 hnae3_set_bit(new_req->flags, HCLGE_MAC_VLAN_BIT0_EN_B, 1);
8291 if (is_mc) {
8292 hnae3_set_bit(new_req->entry_type, HCLGE_MAC_VLAN_BIT1_EN_B, 1);
8293 hnae3_set_bit(new_req->mc_mac_en, HCLGE_MAC_VLAN_BIT0_EN_B, 1);
8294 }
8295
8296 new_req->mac_addr_hi32 = cpu_to_le32(high_val);
8297 new_req->mac_addr_lo16 = cpu_to_le16(low_val & 0xffff);
8298 }
8299
8300 static int hclge_remove_mac_vlan_tbl(struct hclge_vport *vport,
8301 struct hclge_mac_vlan_tbl_entry_cmd *req)
8302 {
8303 struct hclge_dev *hdev = vport->back;
8304 struct hclge_desc desc;
8305 u8 resp_code;
8306 u16 retval;
8307 int ret;
8308
8309 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_REMOVE, false);
8310
8311 memcpy(desc.data, req, sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
8312
8313 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8314 if (ret) {
8315 dev_err(&hdev->pdev->dev,
8316 "del mac addr failed for cmd_send, ret =%d.\n",
8317 ret);
8318 return ret;
8319 }
8320 resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
8321 retval = le16_to_cpu(desc.retval);
8322
8323 return hclge_get_mac_vlan_cmd_status(vport, retval, resp_code,
8324 HCLGE_MAC_VLAN_REMOVE);
8325 }
8326
8327 static int hclge_lookup_mac_vlan_tbl(struct hclge_vport *vport,
8328 struct hclge_mac_vlan_tbl_entry_cmd *req,
8329 struct hclge_desc *desc,
8330 bool is_mc)
8331 {
8332 struct hclge_dev *hdev = vport->back;
8333 u8 resp_code;
8334 u16 retval;
8335 int ret;
8336
8337 hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_MAC_VLAN_ADD, true);
8338 if (is_mc) {
8339 desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
8340 memcpy(desc[0].data,
8341 req,
8342 sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
8343 hclge_cmd_setup_basic_desc(&desc[1],
8344 HCLGE_OPC_MAC_VLAN_ADD,
8345 true);
8346 desc[1].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
8347 hclge_cmd_setup_basic_desc(&desc[2],
8348 HCLGE_OPC_MAC_VLAN_ADD,
8349 true);
8350 ret = hclge_cmd_send(&hdev->hw, desc, 3);
8351 } else {
8352 memcpy(desc[0].data,
8353 req,
8354 sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
8355 ret = hclge_cmd_send(&hdev->hw, desc, 1);
8356 }
8357 if (ret) {
8358 dev_err(&hdev->pdev->dev,
8359 "lookup mac addr failed for cmd_send, ret =%d.\n",
8360 ret);
8361 return ret;
8362 }
8363 resp_code = (le32_to_cpu(desc[0].data[0]) >> 8) & 0xff;
8364 retval = le16_to_cpu(desc[0].retval);
8365
8366 return hclge_get_mac_vlan_cmd_status(vport, retval, resp_code,
8367 HCLGE_MAC_VLAN_LKUP);
8368 }
8369
8370 static int hclge_add_mac_vlan_tbl(struct hclge_vport *vport,
8371 struct hclge_mac_vlan_tbl_entry_cmd *req,
8372 struct hclge_desc *mc_desc)
8373 {
8374 struct hclge_dev *hdev = vport->back;
8375 int cfg_status;
8376 u8 resp_code;
8377 u16 retval;
8378 int ret;
8379
8380 if (!mc_desc) {
8381 struct hclge_desc desc;
8382
8383 hclge_cmd_setup_basic_desc(&desc,
8384 HCLGE_OPC_MAC_VLAN_ADD,
8385 false);
8386 memcpy(desc.data, req,
8387 sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
8388 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8389 resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
8390 retval = le16_to_cpu(desc.retval);
8391
8392 cfg_status = hclge_get_mac_vlan_cmd_status(vport, retval,
8393 resp_code,
8394 HCLGE_MAC_VLAN_ADD);
8395 } else {
8396 hclge_comm_cmd_reuse_desc(&mc_desc[0], false);
8397 mc_desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
8398 hclge_comm_cmd_reuse_desc(&mc_desc[1], false);
8399 mc_desc[1].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
8400 hclge_comm_cmd_reuse_desc(&mc_desc[2], false);
8401 mc_desc[2].flag &= cpu_to_le16(~HCLGE_COMM_CMD_FLAG_NEXT);
8402 memcpy(mc_desc[0].data, req,
8403 sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
8404 ret = hclge_cmd_send(&hdev->hw, mc_desc, 3);
8405 resp_code = (le32_to_cpu(mc_desc[0].data[0]) >> 8) & 0xff;
8406 retval = le16_to_cpu(mc_desc[0].retval);
8407
8408 cfg_status = hclge_get_mac_vlan_cmd_status(vport, retval,
8409 resp_code,
8410 HCLGE_MAC_VLAN_ADD);
8411 }
8412
8413 if (ret) {
8414 dev_err(&hdev->pdev->dev,
8415 "add mac addr failed for cmd_send, ret =%d.\n",
8416 ret);
8417 return ret;
8418 }
8419
8420 return cfg_status;
8421 }
8422
8423 static int hclge_set_umv_space(struct hclge_dev *hdev, u16 space_size,
8424 u16 *allocated_size)
8425 {
8426 struct hclge_umv_spc_alc_cmd *req;
8427 struct hclge_desc desc;
8428 int ret;
8429
8430 req = (struct hclge_umv_spc_alc_cmd *)desc.data;
8431 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_ALLOCATE, false);
8432
8433 req->space_size = cpu_to_le32(space_size);
8434
8435 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8436 if (ret) {
8437 dev_err(&hdev->pdev->dev, "failed to set umv space, ret = %d\n",
8438 ret);
8439 return ret;
8440 }
8441
8442 *allocated_size = le32_to_cpu(desc.data[1]);
8443
8444 return 0;
8445 }
8446
8447 static int hclge_init_umv_space(struct hclge_dev *hdev)
8448 {
8449 u16 allocated_size = 0;
8450 int ret;
8451
8452 ret = hclge_set_umv_space(hdev, hdev->wanted_umv_size, &allocated_size);
8453 if (ret)
8454 return ret;
8455
8456 if (allocated_size < hdev->wanted_umv_size)
8457 dev_warn(&hdev->pdev->dev,
8458 "failed to alloc umv space, want %u, get %u\n",
8459 hdev->wanted_umv_size, allocated_size);
8460
8461 hdev->max_umv_size = allocated_size;
8462 hdev->priv_umv_size = hdev->max_umv_size / (hdev->num_alloc_vport + 1);
8463 hdev->share_umv_size = hdev->priv_umv_size +
8464 hdev->max_umv_size % (hdev->num_alloc_vport + 1);
8465
8466 if (hdev->ae_dev->dev_specs.mc_mac_size)
8467 set_bit(HNAE3_DEV_SUPPORT_MC_MAC_MNG_B, hdev->ae_dev->caps);
8468
8469 return 0;
8470 }
8471
8472 static void hclge_reset_umv_space(struct hclge_dev *hdev)
8473 {
8474 struct hclge_vport *vport;
8475 int i;
8476
8477 for (i = 0; i < hdev->num_alloc_vport; i++) {
8478 vport = &hdev->vport[i];
8479 vport->used_umv_num = 0;
8480 }
8481
8482 mutex_lock(&hdev->vport_lock);
8483 hdev->share_umv_size = hdev->priv_umv_size +
8484 hdev->max_umv_size % (hdev->num_alloc_vport + 1);
8485 mutex_unlock(&hdev->vport_lock);
8486
8487 hdev->used_mc_mac_num = 0;
8488 }
8489
8490 static bool hclge_is_umv_space_full(struct hclge_vport *vport, bool need_lock)
8491 {
8492 struct hclge_dev *hdev = vport->back;
8493 bool is_full;
8494
8495 if (need_lock)
8496 mutex_lock(&hdev->vport_lock);
8497
8498 is_full = (vport->used_umv_num >= hdev->priv_umv_size &&
8499 hdev->share_umv_size == 0);
8500
8501 if (need_lock)
8502 mutex_unlock(&hdev->vport_lock);
8503
8504 return is_full;
8505 }
8506
8507 static void hclge_update_umv_space(struct hclge_vport *vport, bool is_free)
8508 {
8509 struct hclge_dev *hdev = vport->back;
8510
8511 if (is_free) {
8512 if (vport->used_umv_num > hdev->priv_umv_size)
8513 hdev->share_umv_size++;
8514
8515 if (vport->used_umv_num > 0)
8516 vport->used_umv_num--;
8517 } else {
8518 if (vport->used_umv_num >= hdev->priv_umv_size &&
8519 hdev->share_umv_size > 0)
8520 hdev->share_umv_size--;
8521 vport->used_umv_num++;
8522 }
8523 }
8524
8525 static struct hclge_mac_node *hclge_find_mac_node(struct list_head *list,
8526 const u8 *mac_addr)
8527 {
8528 struct hclge_mac_node *mac_node, *tmp;
8529
8530 list_for_each_entry_safe(mac_node, tmp, list, node)
8531 if (ether_addr_equal(mac_addr, mac_node->mac_addr))
8532 return mac_node;
8533
8534 return NULL;
8535 }
8536
8537 static void hclge_update_mac_node(struct hclge_mac_node *mac_node,
8538 enum HCLGE_MAC_NODE_STATE state)
8539 {
8540 switch (state) {
8541 /* from set_rx_mode or tmp_add_list */
8542 case HCLGE_MAC_TO_ADD:
8543 if (mac_node->state == HCLGE_MAC_TO_DEL)
8544 mac_node->state = HCLGE_MAC_ACTIVE;
8545 break;
8546 /* only from set_rx_mode */
8547 case HCLGE_MAC_TO_DEL:
8548 if (mac_node->state == HCLGE_MAC_TO_ADD) {
8549 list_del(&mac_node->node);
8550 kfree(mac_node);
8551 } else {
8552 mac_node->state = HCLGE_MAC_TO_DEL;
8553 }
8554 break;
8555 /* only from tmp_add_list, the mac_node->state won't be
8556 * ACTIVE.
8557 */
8558 case HCLGE_MAC_ACTIVE:
8559 if (mac_node->state == HCLGE_MAC_TO_ADD)
8560 mac_node->state = HCLGE_MAC_ACTIVE;
8561
8562 break;
8563 }
8564 }
8565
8566 int hclge_update_mac_list(struct hclge_vport *vport,
8567 enum HCLGE_MAC_NODE_STATE state,
8568 enum HCLGE_MAC_ADDR_TYPE mac_type,
8569 const unsigned char *addr)
8570 {
8571 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
8572 struct hclge_dev *hdev = vport->back;
8573 struct hclge_mac_node *mac_node;
8574 struct list_head *list;
8575
8576 list = (mac_type == HCLGE_MAC_ADDR_UC) ?
8577 &vport->uc_mac_list : &vport->mc_mac_list;
8578
8579 spin_lock_bh(&vport->mac_list_lock);
8580
8581 /* if the mac addr is already in the mac list, no need to add a new
8582 * one into it, just check the mac addr state, convert it to a new
8583 * state, or just remove it, or do nothing.
8584 */
8585 mac_node = hclge_find_mac_node(list, addr);
8586 if (mac_node) {
8587 hclge_update_mac_node(mac_node, state);
8588 spin_unlock_bh(&vport->mac_list_lock);
8589 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
8590 return 0;
8591 }
8592
8593 /* if this address is never added, unnecessary to delete */
8594 if (state == HCLGE_MAC_TO_DEL) {
8595 spin_unlock_bh(&vport->mac_list_lock);
8596 hnae3_format_mac_addr(format_mac_addr, addr);
8597 dev_err(&hdev->pdev->dev,
8598 "failed to delete address %s from mac list\n",
8599 format_mac_addr);
8600 return -ENOENT;
8601 }
8602
8603 mac_node = kzalloc_obj(*mac_node, GFP_ATOMIC);
8604 if (!mac_node) {
8605 spin_unlock_bh(&vport->mac_list_lock);
8606 return -ENOMEM;
8607 }
8608
8609 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
8610
8611 mac_node->state = state;
8612 ether_addr_copy(mac_node->mac_addr, addr);
8613 list_add_tail(&mac_node->node, list);
8614
8615 spin_unlock_bh(&vport->mac_list_lock);
8616
8617 return 0;
8618 }
8619
8620 static int hclge_add_uc_addr(struct hnae3_handle *handle,
8621 const unsigned char *addr)
8622 {
8623 struct hclge_vport *vport = hclge_get_vport(handle);
8624
8625 return hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD, HCLGE_MAC_ADDR_UC,
8626 addr);
8627 }
8628
8629 int hclge_add_uc_addr_common(struct hclge_vport *vport,
8630 const unsigned char *addr)
8631 {
8632 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
8633 struct hclge_dev *hdev = vport->back;
8634 struct hclge_mac_vlan_tbl_entry_cmd req;
8635 struct hclge_desc desc;
8636 u16 egress_port = 0;
8637 int ret;
8638
8639 /* mac addr check */
8640 if (is_zero_ether_addr(addr) ||
8641 is_broadcast_ether_addr(addr) ||
8642 is_multicast_ether_addr(addr)) {
8643 hnae3_format_mac_addr(format_mac_addr, addr);
8644 dev_err(&hdev->pdev->dev,
8645 "Set_uc mac err! invalid mac:%s. is_zero:%d,is_br=%d,is_mul=%d\n",
8646 format_mac_addr, is_zero_ether_addr(addr),
8647 is_broadcast_ether_addr(addr),
8648 is_multicast_ether_addr(addr));
8649 return -EINVAL;
8650 }
8651
8652 memset(&req, 0, sizeof(req));
8653
8654 hnae3_set_field(egress_port, HCLGE_MAC_EPORT_VFID_M,
8655 HCLGE_MAC_EPORT_VFID_S, vport->vport_id);
8656
8657 req.egress_port = cpu_to_le16(egress_port);
8658
8659 hclge_prepare_mac_addr(&req, addr, false);
8660
8661 /* Lookup the mac address in the mac_vlan table, and add
8662 * it if the entry is inexistent. Repeated unicast entry
8663 * is not allowed in the mac vlan table.
8664 */
8665 ret = hclge_lookup_mac_vlan_tbl(vport, &req, &desc, false);
8666 if (ret == -ENOENT) {
8667 mutex_lock(&hdev->vport_lock);
8668 if (!hclge_is_umv_space_full(vport, false)) {
8669 ret = hclge_add_mac_vlan_tbl(vport, &req, NULL);
8670 if (!ret)
8671 hclge_update_umv_space(vport, false);
8672 mutex_unlock(&hdev->vport_lock);
8673 return ret;
8674 }
8675 mutex_unlock(&hdev->vport_lock);
8676
8677 if (!(vport->overflow_promisc_flags & HNAE3_OVERFLOW_UPE))
8678 dev_err(&hdev->pdev->dev, "UC MAC table full(%u)\n",
8679 hdev->priv_umv_size);
8680
8681 return -ENOSPC;
8682 }
8683
8684 /* check if we just hit the duplicate */
8685 if (!ret)
8686 return -EEXIST;
8687
8688 return ret;
8689 }
8690
8691 static int hclge_rm_uc_addr(struct hnae3_handle *handle,
8692 const unsigned char *addr)
8693 {
8694 struct hclge_vport *vport = hclge_get_vport(handle);
8695
8696 return hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL, HCLGE_MAC_ADDR_UC,
8697 addr);
8698 }
8699
8700 int hclge_rm_uc_addr_common(struct hclge_vport *vport,
8701 const unsigned char *addr)
8702 {
8703 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
8704 struct hclge_dev *hdev = vport->back;
8705 struct hclge_mac_vlan_tbl_entry_cmd req;
8706 int ret;
8707
8708 /* mac addr check */
8709 if (is_zero_ether_addr(addr) ||
8710 is_broadcast_ether_addr(addr) ||
8711 is_multicast_ether_addr(addr)) {
8712 hnae3_format_mac_addr(format_mac_addr, addr);
8713 dev_dbg(&hdev->pdev->dev, "Remove mac err! invalid mac:%s.\n",
8714 format_mac_addr);
8715 return -EINVAL;
8716 }
8717
8718 memset(&req, 0, sizeof(req));
8719 hnae3_set_bit(req.entry_type, HCLGE_MAC_VLAN_BIT0_EN_B, 0);
8720 hclge_prepare_mac_addr(&req, addr, false);
8721 ret = hclge_remove_mac_vlan_tbl(vport, &req);
8722 if (!ret || ret == -ENOENT) {
8723 mutex_lock(&hdev->vport_lock);
8724 hclge_update_umv_space(vport, true);
8725 mutex_unlock(&hdev->vport_lock);
8726 return 0;
8727 }
8728
8729 return ret;
8730 }
8731
8732 static int hclge_add_mc_addr(struct hnae3_handle *handle,
8733 const unsigned char *addr)
8734 {
8735 struct hclge_vport *vport = hclge_get_vport(handle);
8736
8737 return hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD, HCLGE_MAC_ADDR_MC,
8738 addr);
8739 }
8740
8741 int hclge_add_mc_addr_common(struct hclge_vport *vport,
8742 const unsigned char *addr)
8743 {
8744 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
8745 struct hclge_dev *hdev = vport->back;
8746 struct hclge_mac_vlan_tbl_entry_cmd req;
8747 struct hclge_desc desc[3];
8748 bool is_new_addr = false;
8749 int status;
8750
8751 /* mac addr check */
8752 if (!is_multicast_ether_addr(addr)) {
8753 hnae3_format_mac_addr(format_mac_addr, addr);
8754 dev_err(&hdev->pdev->dev,
8755 "Add mc mac err! invalid mac:%s.\n",
8756 format_mac_addr);
8757 return -EINVAL;
8758 }
8759 memset(&req, 0, sizeof(req));
8760 hclge_prepare_mac_addr(&req, addr, true);
8761 status = hclge_lookup_mac_vlan_tbl(vport, &req, desc, true);
8762 if (status) {
8763 if (hnae3_ae_dev_mc_mac_mng_supported(hdev->ae_dev) &&
8764 hdev->used_mc_mac_num >=
8765 hdev->ae_dev->dev_specs.mc_mac_size)
8766 goto err_no_space;
8767
8768 is_new_addr = true;
8769
8770 /* This mac addr do not exist, add new entry for it */
8771 memset(desc[0].data, 0, sizeof(desc[0].data));
8772 memset(desc[1].data, 0, sizeof(desc[0].data));
8773 memset(desc[2].data, 0, sizeof(desc[0].data));
8774 }
8775 status = hclge_update_desc_vfid(desc, vport->vport_id, false);
8776 if (status)
8777 return status;
8778 status = hclge_add_mac_vlan_tbl(vport, &req, desc);
8779 if (status == -ENOSPC)
8780 goto err_no_space;
8781 else if (!status && is_new_addr)
8782 hdev->used_mc_mac_num++;
8783
8784 return status;
8785
8786 err_no_space:
8787 /* if already overflow, not to print each time */
8788 if (!(vport->overflow_promisc_flags & HNAE3_OVERFLOW_MPE)) {
8789 vport->overflow_promisc_flags |= HNAE3_OVERFLOW_MPE;
8790 dev_err(&hdev->pdev->dev, "mc mac vlan table is full\n");
8791 }
8792
8793 return -ENOSPC;
8794 }
8795
8796 static int hclge_rm_mc_addr(struct hnae3_handle *handle,
8797 const unsigned char *addr)
8798 {
8799 struct hclge_vport *vport = hclge_get_vport(handle);
8800
8801 return hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL, HCLGE_MAC_ADDR_MC,
8802 addr);
8803 }
8804
8805 int hclge_rm_mc_addr_common(struct hclge_vport *vport,
8806 const unsigned char *addr)
8807 {
8808 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
8809 struct hclge_dev *hdev = vport->back;
8810 struct hclge_mac_vlan_tbl_entry_cmd req;
8811 enum hclge_comm_cmd_status status;
8812 struct hclge_desc desc[3];
8813
8814 /* mac addr check */
8815 if (!is_multicast_ether_addr(addr)) {
8816 hnae3_format_mac_addr(format_mac_addr, addr);
8817 dev_dbg(&hdev->pdev->dev,
8818 "Remove mc mac err! invalid mac:%s.\n",
8819 format_mac_addr);
8820 return -EINVAL;
8821 }
8822
8823 memset(&req, 0, sizeof(req));
8824 hclge_prepare_mac_addr(&req, addr, true);
8825 status = hclge_lookup_mac_vlan_tbl(vport, &req, desc, true);
8826 if (!status) {
8827 /* This mac addr exist, remove this handle's VFID for it */
8828 status = hclge_update_desc_vfid(desc, vport->vport_id, true);
8829 if (status)
8830 return status;
8831
8832 if (hclge_is_all_function_id_zero(desc)) {
8833 /* All the vfid is zero, so need to delete this entry */
8834 status = hclge_remove_mac_vlan_tbl(vport, &req);
8835 if (!status)
8836 hdev->used_mc_mac_num--;
8837 } else {
8838 /* Not all the vfid is zero, update the vfid */
8839 status = hclge_add_mac_vlan_tbl(vport, &req, desc);
8840 }
8841 } else if (status == -ENOENT) {
8842 status = 0;
8843 }
8844
8845 return status;
8846 }
8847
8848 static void hclge_sync_vport_mac_list(struct hclge_vport *vport,
8849 struct list_head *list,
8850 enum HCLGE_MAC_ADDR_TYPE mac_type)
8851 {
8852 int (*sync)(struct hclge_vport *vport, const unsigned char *addr);
8853 struct hclge_mac_node *mac_node, *tmp;
8854 int ret;
8855
8856 if (mac_type == HCLGE_MAC_ADDR_UC)
8857 sync = hclge_add_uc_addr_common;
8858 else
8859 sync = hclge_add_mc_addr_common;
8860
8861 list_for_each_entry_safe(mac_node, tmp, list, node) {
8862 ret = sync(vport, mac_node->mac_addr);
8863 if (!ret) {
8864 mac_node->state = HCLGE_MAC_ACTIVE;
8865 } else {
8866 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE,
8867 &vport->state);
8868
8869 /* If one unicast mac address is existing in hardware,
8870 * we need to try whether other unicast mac addresses
8871 * are new addresses that can be added.
8872 * Multicast mac address can be reusable, even though
8873 * there is no space to add new multicast mac address,
8874 * we should check whether other mac addresses are
8875 * existing in hardware for reuse.
8876 */
8877 if ((mac_type == HCLGE_MAC_ADDR_UC && ret != -EEXIST) ||
8878 (mac_type == HCLGE_MAC_ADDR_MC && ret != -ENOSPC))
8879 break;
8880 }
8881 }
8882 }
8883
8884 static void hclge_unsync_vport_mac_list(struct hclge_vport *vport,
8885 struct list_head *list,
8886 enum HCLGE_MAC_ADDR_TYPE mac_type)
8887 {
8888 int (*unsync)(struct hclge_vport *vport, const unsigned char *addr);
8889 struct hclge_mac_node *mac_node, *tmp;
8890 int ret;
8891
8892 if (mac_type == HCLGE_MAC_ADDR_UC)
8893 unsync = hclge_rm_uc_addr_common;
8894 else
8895 unsync = hclge_rm_mc_addr_common;
8896
8897 list_for_each_entry_safe(mac_node, tmp, list, node) {
8898 ret = unsync(vport, mac_node->mac_addr);
8899 if (!ret || ret == -ENOENT) {
8900 list_del(&mac_node->node);
8901 kfree(mac_node);
8902 } else {
8903 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE,
8904 &vport->state);
8905 break;
8906 }
8907 }
8908 }
8909
8910 static bool hclge_sync_from_add_list(struct list_head *add_list,
8911 struct list_head *mac_list)
8912 {
8913 struct hclge_mac_node *mac_node, *tmp, *new_node;
8914 bool all_added = true;
8915
8916 list_for_each_entry_safe(mac_node, tmp, add_list, node) {
8917 if (mac_node->state == HCLGE_MAC_TO_ADD)
8918 all_added = false;
8919
8920 /* if the mac address from tmp_add_list is not in the
8921 * uc/mc_mac_list, it means have received a TO_DEL request
8922 * during the time window of adding the mac address into mac
8923 * table. if mac_node state is ACTIVE, then change it to TO_DEL,
8924 * then it will be removed at next time. else it must be TO_ADD,
8925 * this address hasn't been added into mac table,
8926 * so just remove the mac node.
8927 */
8928 new_node = hclge_find_mac_node(mac_list, mac_node->mac_addr);
8929 if (new_node) {
8930 hclge_update_mac_node(new_node, mac_node->state);
8931 list_del(&mac_node->node);
8932 kfree(mac_node);
8933 } else if (mac_node->state == HCLGE_MAC_ACTIVE) {
8934 mac_node->state = HCLGE_MAC_TO_DEL;
8935 list_move_tail(&mac_node->node, mac_list);
8936 } else {
8937 list_del(&mac_node->node);
8938 kfree(mac_node);
8939 }
8940 }
8941
8942 return all_added;
8943 }
8944
8945 static void hclge_sync_from_del_list(struct list_head *del_list,
8946 struct list_head *mac_list)
8947 {
8948 struct hclge_mac_node *mac_node, *tmp, *new_node;
8949
8950 list_for_each_entry_safe(mac_node, tmp, del_list, node) {
8951 new_node = hclge_find_mac_node(mac_list, mac_node->mac_addr);
8952 if (new_node) {
8953 /* If the mac addr exists in the mac list, it means
8954 * received a new TO_ADD request during the time window
8955 * of configuring the mac address. For the mac node
8956 * state is TO_ADD, and the address is already in the
8957 * in the hardware(due to delete fail), so we just need
8958 * to change the mac node state to ACTIVE.
8959 */
8960 new_node->state = HCLGE_MAC_ACTIVE;
8961 list_del(&mac_node->node);
8962 kfree(mac_node);
8963 } else {
8964 list_move_tail(&mac_node->node, mac_list);
8965 }
8966 }
8967 }
8968
8969 static void hclge_update_overflow_flags(struct hclge_vport *vport,
8970 enum HCLGE_MAC_ADDR_TYPE mac_type,
8971 bool is_all_added)
8972 {
8973 if (mac_type == HCLGE_MAC_ADDR_UC) {
8974 if (is_all_added)
8975 vport->overflow_promisc_flags &= ~HNAE3_OVERFLOW_UPE;
8976 else if (hclge_is_umv_space_full(vport, true))
8977 vport->overflow_promisc_flags |= HNAE3_OVERFLOW_UPE;
8978 } else {
8979 if (is_all_added)
8980 vport->overflow_promisc_flags &= ~HNAE3_OVERFLOW_MPE;
8981 else
8982 vport->overflow_promisc_flags |= HNAE3_OVERFLOW_MPE;
8983 }
8984 }
8985
8986 static void hclge_sync_vport_mac_table(struct hclge_vport *vport,
8987 enum HCLGE_MAC_ADDR_TYPE mac_type)
8988 {
8989 struct hclge_mac_node *mac_node, *tmp, *new_node;
8990 struct list_head tmp_add_list, tmp_del_list;
8991 struct list_head *list;
8992 bool all_added;
8993
8994 INIT_LIST_HEAD(&tmp_add_list);
8995 INIT_LIST_HEAD(&tmp_del_list);
8996
8997 /* move the mac addr to the tmp_add_list and tmp_del_list, then
8998 * we can add/delete these mac addr outside the spin lock
8999 */
9000 list = (mac_type == HCLGE_MAC_ADDR_UC) ?
9001 &vport->uc_mac_list : &vport->mc_mac_list;
9002
9003 spin_lock_bh(&vport->mac_list_lock);
9004
9005 list_for_each_entry_safe(mac_node, tmp, list, node) {
9006 switch (mac_node->state) {
9007 case HCLGE_MAC_TO_DEL:
9008 list_move_tail(&mac_node->node, &tmp_del_list);
9009 break;
9010 case HCLGE_MAC_TO_ADD:
9011 new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
9012 if (!new_node)
9013 goto stop_traverse;
9014 ether_addr_copy(new_node->mac_addr, mac_node->mac_addr);
9015 new_node->state = mac_node->state;
9016 list_add_tail(&new_node->node, &tmp_add_list);
9017 break;
9018 default:
9019 break;
9020 }
9021 }
9022
9023 stop_traverse:
9024 spin_unlock_bh(&vport->mac_list_lock);
9025
9026 /* delete first, in order to get max mac table space for adding */
9027 hclge_unsync_vport_mac_list(vport, &tmp_del_list, mac_type);
9028 hclge_sync_vport_mac_list(vport, &tmp_add_list, mac_type);
9029
9030 /* if some mac addresses were added/deleted fail, move back to the
9031 * mac_list, and retry at next time.
9032 */
9033 spin_lock_bh(&vport->mac_list_lock);
9034
9035 hclge_sync_from_del_list(&tmp_del_list, list);
9036 all_added = hclge_sync_from_add_list(&tmp_add_list, list);
9037
9038 spin_unlock_bh(&vport->mac_list_lock);
9039
9040 hclge_update_overflow_flags(vport, mac_type, all_added);
9041 }
9042
9043 static bool hclge_need_sync_mac_table(struct hclge_vport *vport)
9044 {
9045 struct hclge_dev *hdev = vport->back;
9046
9047 if (test_bit(vport->vport_id, hdev->vport_config_block))
9048 return false;
9049
9050 if (test_and_clear_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state))
9051 return true;
9052
9053 return false;
9054 }
9055
9056 static void hclge_sync_mac_table(struct hclge_dev *hdev)
9057 {
9058 int i;
9059
9060 for (i = 0; i < hdev->num_alloc_vport; i++) {
9061 struct hclge_vport *vport = &hdev->vport[i];
9062
9063 if (!hclge_need_sync_mac_table(vport))
9064 continue;
9065
9066 hclge_sync_vport_mac_table(vport, HCLGE_MAC_ADDR_UC);
9067 hclge_sync_vport_mac_table(vport, HCLGE_MAC_ADDR_MC);
9068 }
9069 }
9070
9071 static void hclge_build_del_list(struct list_head *list,
9072 bool is_del_list,
9073 struct list_head *tmp_del_list)
9074 {
9075 struct hclge_mac_node *mac_cfg, *tmp;
9076
9077 list_for_each_entry_safe(mac_cfg, tmp, list, node) {
9078 switch (mac_cfg->state) {
9079 case HCLGE_MAC_TO_DEL:
9080 case HCLGE_MAC_ACTIVE:
9081 list_move_tail(&mac_cfg->node, tmp_del_list);
9082 break;
9083 case HCLGE_MAC_TO_ADD:
9084 if (is_del_list) {
9085 list_del(&mac_cfg->node);
9086 kfree(mac_cfg);
9087 }
9088 break;
9089 }
9090 }
9091 }
9092
9093 static void hclge_unsync_del_list(struct hclge_vport *vport,
9094 int (*unsync)(struct hclge_vport *vport,
9095 const unsigned char *addr),
9096 bool is_del_list,
9097 struct list_head *tmp_del_list)
9098 {
9099 struct hclge_mac_node *mac_cfg, *tmp;
9100 int ret;
9101
9102 list_for_each_entry_safe(mac_cfg, tmp, tmp_del_list, node) {
9103 ret = unsync(vport, mac_cfg->mac_addr);
9104 if (!ret || ret == -ENOENT) {
9105 /* clear all mac addr from hardware, but remain these
9106 * mac addr in the mac list, and restore them after
9107 * vf reset finished.
9108 */
9109 if (!is_del_list &&
9110 mac_cfg->state == HCLGE_MAC_ACTIVE) {
9111 mac_cfg->state = HCLGE_MAC_TO_ADD;
9112 } else {
9113 list_del(&mac_cfg->node);
9114 kfree(mac_cfg);
9115 }
9116 } else if (is_del_list) {
9117 mac_cfg->state = HCLGE_MAC_TO_DEL;
9118 }
9119 }
9120 }
9121
9122 void hclge_rm_vport_all_mac_table(struct hclge_vport *vport, bool is_del_list,
9123 enum HCLGE_MAC_ADDR_TYPE mac_type)
9124 {
9125 int (*unsync)(struct hclge_vport *vport, const unsigned char *addr);
9126 struct hclge_dev *hdev = vport->back;
9127 struct list_head tmp_del_list, *list;
9128
9129 if (mac_type == HCLGE_MAC_ADDR_UC) {
9130 list = &vport->uc_mac_list;
9131 unsync = hclge_rm_uc_addr_common;
9132 } else {
9133 list = &vport->mc_mac_list;
9134 unsync = hclge_rm_mc_addr_common;
9135 }
9136
9137 INIT_LIST_HEAD(&tmp_del_list);
9138
9139 if (!is_del_list)
9140 set_bit(vport->vport_id, hdev->vport_config_block);
9141
9142 spin_lock_bh(&vport->mac_list_lock);
9143
9144 hclge_build_del_list(list, is_del_list, &tmp_del_list);
9145
9146 spin_unlock_bh(&vport->mac_list_lock);
9147
9148 hclge_unsync_del_list(vport, unsync, is_del_list, &tmp_del_list);
9149
9150 spin_lock_bh(&vport->mac_list_lock);
9151
9152 hclge_sync_from_del_list(&tmp_del_list, list);
9153
9154 spin_unlock_bh(&vport->mac_list_lock);
9155 }
9156
9157 /* remove all mac address when uninitailize */
9158 static void hclge_uninit_vport_mac_list(struct hclge_vport *vport,
9159 enum HCLGE_MAC_ADDR_TYPE mac_type)
9160 {
9161 struct hclge_mac_node *mac_node, *tmp;
9162 struct hclge_dev *hdev = vport->back;
9163 struct list_head tmp_del_list, *list;
9164
9165 INIT_LIST_HEAD(&tmp_del_list);
9166
9167 list = (mac_type == HCLGE_MAC_ADDR_UC) ?
9168 &vport->uc_mac_list : &vport->mc_mac_list;
9169
9170 spin_lock_bh(&vport->mac_list_lock);
9171
9172 list_for_each_entry_safe(mac_node, tmp, list, node) {
9173 switch (mac_node->state) {
9174 case HCLGE_MAC_TO_DEL:
9175 case HCLGE_MAC_ACTIVE:
9176 list_move_tail(&mac_node->node, &tmp_del_list);
9177 break;
9178 case HCLGE_MAC_TO_ADD:
9179 list_del(&mac_node->node);
9180 kfree(mac_node);
9181 break;
9182 }
9183 }
9184
9185 spin_unlock_bh(&vport->mac_list_lock);
9186
9187 hclge_unsync_vport_mac_list(vport, &tmp_del_list, mac_type);
9188
9189 if (!list_empty(&tmp_del_list))
9190 dev_warn(&hdev->pdev->dev,
9191 "uninit %s mac list for vport %u not completely.\n",
9192 mac_type == HCLGE_MAC_ADDR_UC ? "uc" : "mc",
9193 vport->vport_id);
9194
9195 list_for_each_entry_safe(mac_node, tmp, &tmp_del_list, node) {
9196 list_del(&mac_node->node);
9197 kfree(mac_node);
9198 }
9199 }
9200
9201 static void hclge_uninit_mac_table(struct hclge_dev *hdev)
9202 {
9203 struct hclge_vport *vport;
9204 int i;
9205
9206 for (i = 0; i < hdev->num_alloc_vport; i++) {
9207 vport = &hdev->vport[i];
9208 hclge_uninit_vport_mac_list(vport, HCLGE_MAC_ADDR_UC);
9209 hclge_uninit_vport_mac_list(vport, HCLGE_MAC_ADDR_MC);
9210 }
9211 }
9212
9213 static int hclge_get_mac_ethertype_cmd_status(struct hclge_dev *hdev,
9214 u16 cmdq_resp, u8 resp_code)
9215 {
9216 #define HCLGE_ETHERTYPE_SUCCESS_ADD 0
9217 #define HCLGE_ETHERTYPE_ALREADY_ADD 1
9218 #define HCLGE_ETHERTYPE_MGR_TBL_OVERFLOW 2
9219 #define HCLGE_ETHERTYPE_KEY_CONFLICT 3
9220
9221 int return_status;
9222
9223 if (cmdq_resp) {
9224 dev_err(&hdev->pdev->dev,
9225 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
9226 cmdq_resp);
9227 return -EIO;
9228 }
9229
9230 switch (resp_code) {
9231 case HCLGE_ETHERTYPE_SUCCESS_ADD:
9232 case HCLGE_ETHERTYPE_ALREADY_ADD:
9233 return_status = 0;
9234 break;
9235 case HCLGE_ETHERTYPE_MGR_TBL_OVERFLOW:
9236 dev_err(&hdev->pdev->dev,
9237 "add mac ethertype failed for manager table overflow.\n");
9238 return_status = -EIO;
9239 break;
9240 case HCLGE_ETHERTYPE_KEY_CONFLICT:
9241 dev_err(&hdev->pdev->dev,
9242 "add mac ethertype failed for key conflict.\n");
9243 return_status = -EIO;
9244 break;
9245 default:
9246 dev_err(&hdev->pdev->dev,
9247 "add mac ethertype failed for undefined, code=%u.\n",
9248 resp_code);
9249 return_status = -EIO;
9250 }
9251
9252 return return_status;
9253 }
9254
9255 static int hclge_set_vf_mac(struct hnae3_handle *handle, int vf,
9256 u8 *mac_addr)
9257 {
9258 struct hclge_vport *vport = hclge_get_vport(handle);
9259 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
9260 struct hclge_dev *hdev = vport->back;
9261
9262 vport = hclge_get_vf_vport(hdev, vf);
9263 if (!vport)
9264 return -EINVAL;
9265
9266 hnae3_format_mac_addr(format_mac_addr, mac_addr);
9267 if (ether_addr_equal(mac_addr, vport->vf_info.mac)) {
9268 dev_info(&hdev->pdev->dev,
9269 "Specified MAC(=%s) is same as before, no change committed!\n",
9270 format_mac_addr);
9271 return 0;
9272 }
9273
9274 ether_addr_copy(vport->vf_info.mac, mac_addr);
9275
9276 /* there is a timewindow for PF to know VF unalive, it may
9277 * cause send mailbox fail, but it doesn't matter, VF will
9278 * query it when reinit.
9279 */
9280 if (test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state)) {
9281 dev_info(&hdev->pdev->dev,
9282 "MAC of VF %d has been set to %s, and it will be reinitialized!\n",
9283 vf, format_mac_addr);
9284 (void)hclge_inform_reset_assert_to_vf(vport);
9285 return 0;
9286 }
9287
9288 dev_info(&hdev->pdev->dev,
9289 "MAC of VF %d has been set to %s, will be active after VF reset\n",
9290 vf, format_mac_addr);
9291 return 0;
9292 }
9293
9294 static int hclge_add_mgr_tbl(struct hclge_dev *hdev,
9295 const struct hclge_mac_mgr_tbl_entry_cmd *req)
9296 {
9297 struct hclge_desc desc;
9298 u8 resp_code;
9299 u16 retval;
9300 int ret;
9301
9302 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_ETHTYPE_ADD, false);
9303 memcpy(desc.data, req, sizeof(struct hclge_mac_mgr_tbl_entry_cmd));
9304
9305 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9306 if (ret) {
9307 dev_err(&hdev->pdev->dev,
9308 "add mac ethertype failed for cmd_send, ret =%d.\n",
9309 ret);
9310 return ret;
9311 }
9312
9313 resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
9314 retval = le16_to_cpu(desc.retval);
9315
9316 return hclge_get_mac_ethertype_cmd_status(hdev, retval, resp_code);
9317 }
9318
9319 static int init_mgr_tbl(struct hclge_dev *hdev)
9320 {
9321 int ret;
9322 u32 i;
9323
9324 for (i = 0; i < ARRAY_SIZE(hclge_mgr_table); i++) {
9325 ret = hclge_add_mgr_tbl(hdev, &hclge_mgr_table[i]);
9326 if (ret) {
9327 dev_err(&hdev->pdev->dev,
9328 "add mac ethertype failed, ret =%d.\n",
9329 ret);
9330 return ret;
9331 }
9332 }
9333
9334 return 0;
9335 }
9336
9337 static void hclge_get_mac_addr(struct hnae3_handle *handle, u8 *p)
9338 {
9339 struct hclge_vport *vport = hclge_get_vport(handle);
9340 struct hclge_dev *hdev = vport->back;
9341
9342 ether_addr_copy(p, hdev->hw.mac.mac_addr);
9343 }
9344
9345 int hclge_update_mac_node_for_dev_addr(struct hclge_vport *vport,
9346 const u8 *old_addr, const u8 *new_addr)
9347 {
9348 struct list_head *list = &vport->uc_mac_list;
9349 struct hclge_mac_node *old_node, *new_node;
9350
9351 new_node = hclge_find_mac_node(list, new_addr);
9352 if (!new_node) {
9353 new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
9354 if (!new_node)
9355 return -ENOMEM;
9356
9357 new_node->state = HCLGE_MAC_TO_ADD;
9358 ether_addr_copy(new_node->mac_addr, new_addr);
9359 list_add(&new_node->node, list);
9360 } else {
9361 if (new_node->state == HCLGE_MAC_TO_DEL)
9362 new_node->state = HCLGE_MAC_ACTIVE;
9363
9364 /* make sure the new addr is in the list head, avoid dev
9365 * addr may be not re-added into mac table for the umv space
9366 * limitation after global/imp reset which will clear mac
9367 * table by hardware.
9368 */
9369 list_move(&new_node->node, list);
9370 }
9371
9372 if (old_addr && !ether_addr_equal(old_addr, new_addr)) {
9373 old_node = hclge_find_mac_node(list, old_addr);
9374 if (old_node) {
9375 if (old_node->state == HCLGE_MAC_TO_ADD) {
9376 list_del(&old_node->node);
9377 kfree(old_node);
9378 } else {
9379 old_node->state = HCLGE_MAC_TO_DEL;
9380 }
9381 }
9382 }
9383
9384 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
9385
9386 return 0;
9387 }
9388
9389 static int hclge_set_mac_addr(struct hnae3_handle *handle, const void *p,
9390 bool is_first)
9391 {
9392 const unsigned char *new_addr = (const unsigned char *)p;
9393 struct hclge_vport *vport = hclge_get_vport(handle);
9394 char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
9395 struct hclge_dev *hdev = vport->back;
9396 unsigned char *old_addr = NULL;
9397 int ret;
9398
9399 /* mac addr check */
9400 if (is_zero_ether_addr(new_addr) ||
9401 is_broadcast_ether_addr(new_addr) ||
9402 is_multicast_ether_addr(new_addr)) {
9403 hnae3_format_mac_addr(format_mac_addr, new_addr);
9404 dev_err(&hdev->pdev->dev,
9405 "change uc mac err! invalid mac: %s.\n",
9406 format_mac_addr);
9407 return -EINVAL;
9408 }
9409
9410 ret = hclge_pause_addr_cfg(hdev, new_addr);
9411 if (ret) {
9412 dev_err(&hdev->pdev->dev,
9413 "failed to configure mac pause address, ret = %d\n",
9414 ret);
9415 return ret;
9416 }
9417
9418 if (!is_first)
9419 old_addr = hdev->hw.mac.mac_addr;
9420
9421 spin_lock_bh(&vport->mac_list_lock);
9422 ret = hclge_update_mac_node_for_dev_addr(vport, old_addr, new_addr);
9423 if (ret) {
9424 hnae3_format_mac_addr(format_mac_addr, new_addr);
9425 dev_err(&hdev->pdev->dev,
9426 "failed to change the mac addr:%s, ret = %d\n",
9427 format_mac_addr, ret);
9428 spin_unlock_bh(&vport->mac_list_lock);
9429
9430 if (!is_first)
9431 hclge_pause_addr_cfg(hdev, old_addr);
9432
9433 return ret;
9434 }
9435 /* we must update dev addr with spin lock protect, preventing dev addr
9436 * being removed by set_rx_mode path.
9437 */
9438 ether_addr_copy(hdev->hw.mac.mac_addr, new_addr);
9439 spin_unlock_bh(&vport->mac_list_lock);
9440
9441 hclge_task_schedule(hdev, 0);
9442
9443 return 0;
9444 }
9445
9446 static int hclge_mii_ioctl(struct hclge_dev *hdev, struct ifreq *ifr, int cmd)
9447 {
9448 struct mii_ioctl_data *data = if_mii(ifr);
9449
9450 if (!hnae3_dev_phy_imp_supported(hdev))
9451 return -EOPNOTSUPP;
9452
9453 switch (cmd) {
9454 case SIOCGMIIPHY:
9455 data->phy_id = hdev->hw.mac.phy_addr;
9456 /* this command reads phy id and register at the same time */
9457 fallthrough;
9458 case SIOCGMIIREG:
9459 return hclge_read_phy_reg(hdev, data->reg_num, &data->val_out);
9460
9461 case SIOCSMIIREG:
9462 return hclge_write_phy_reg(hdev, data->reg_num, data->val_in);
9463 default:
9464 return -EOPNOTSUPP;
9465 }
9466 }
9467
9468 static int hclge_do_ioctl(struct hnae3_handle *handle, struct ifreq *ifr,
9469 int cmd)
9470 {
9471 struct hclge_vport *vport = hclge_get_vport(handle);
9472 struct hclge_dev *hdev = vport->back;
9473
9474 if (!hdev->hw.mac.phydev)
9475 return hclge_mii_ioctl(hdev, ifr, cmd);
9476
9477 return phy_mii_ioctl(hdev->hw.mac.phydev, ifr, cmd);
9478 }
9479
9480 static int hclge_set_port_vlan_filter_bypass(struct hclge_dev *hdev, u8 vf_id,
9481 bool bypass_en)
9482 {
9483 struct hclge_port_vlan_filter_bypass_cmd *req;
9484 struct hclge_desc desc;
9485 int ret;
9486
9487 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_PORT_VLAN_BYPASS, false);
9488 req = (struct hclge_port_vlan_filter_bypass_cmd *)desc.data;
9489 req->vf_id = vf_id;
9490 hnae3_set_bit(req->bypass_state, HCLGE_INGRESS_BYPASS_B,
9491 bypass_en ? 1 : 0);
9492
9493 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9494 if (ret)
9495 dev_err(&hdev->pdev->dev,
9496 "failed to set vport%u port vlan filter bypass state, ret = %d.\n",
9497 vf_id, ret);
9498
9499 return ret;
9500 }
9501
9502 static int hclge_set_vlan_filter_ctrl(struct hclge_dev *hdev, u8 vlan_type,
9503 u8 fe_type, bool filter_en, u8 vf_id)
9504 {
9505 struct hclge_vlan_filter_ctrl_cmd *req;
9506 struct hclge_desc desc;
9507 int ret;
9508
9509 /* read current vlan filter parameter */
9510 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_FILTER_CTRL, true);
9511 req = (struct hclge_vlan_filter_ctrl_cmd *)desc.data;
9512 req->vlan_type = vlan_type;
9513 req->vf_id = vf_id;
9514
9515 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9516 if (ret) {
9517 dev_err(&hdev->pdev->dev, "failed to get vport%u vlan filter config, ret = %d.\n",
9518 vf_id, ret);
9519 return ret;
9520 }
9521
9522 /* modify and write new config parameter */
9523 hclge_comm_cmd_reuse_desc(&desc, false);
9524 req->vlan_fe = filter_en ?
9525 (req->vlan_fe | fe_type) : (req->vlan_fe & ~fe_type);
9526
9527 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9528 if (ret)
9529 dev_err(&hdev->pdev->dev, "failed to set vport%u vlan filter, ret = %d.\n",
9530 vf_id, ret);
9531
9532 return ret;
9533 }
9534
9535 static int hclge_set_vport_vlan_filter(struct hclge_vport *vport, bool enable)
9536 {
9537 struct hclge_dev *hdev = vport->back;
9538 struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
9539 int ret;
9540
9541 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
9542 return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
9543 HCLGE_FILTER_FE_EGRESS_V1_B,
9544 enable, vport->vport_id);
9545
9546 ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
9547 HCLGE_FILTER_FE_EGRESS, enable,
9548 vport->vport_id);
9549 if (ret)
9550 return ret;
9551
9552 if (test_bit(HNAE3_DEV_SUPPORT_PORT_VLAN_BYPASS_B, ae_dev->caps)) {
9553 ret = hclge_set_port_vlan_filter_bypass(hdev, vport->vport_id,
9554 !enable);
9555 } else if (!vport->vport_id) {
9556 if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps))
9557 enable = false;
9558
9559 ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_PORT,
9560 HCLGE_FILTER_FE_INGRESS,
9561 enable, 0);
9562 }
9563
9564 return ret;
9565 }
9566
9567 static bool hclge_need_enable_vport_vlan_filter(struct hclge_vport *vport)
9568 {
9569 struct hnae3_handle *handle = &vport->nic;
9570 struct hclge_vport_vlan_cfg *vlan, *tmp;
9571 struct hclge_dev *hdev = vport->back;
9572
9573 if (vport->vport_id) {
9574 if (vport->port_base_vlan_cfg.state !=
9575 HNAE3_PORT_BASE_VLAN_DISABLE)
9576 return true;
9577
9578 if (vport->vf_info.trusted && vport->vf_info.request_uc_en)
9579 return false;
9580 } else if (handle->netdev_flags & HNAE3_USER_UPE) {
9581 return false;
9582 }
9583
9584 if (!vport->req_vlan_fltr_en)
9585 return false;
9586
9587 /* compatible with former device, always enable vlan filter */
9588 if (!test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps))
9589 return true;
9590
9591 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node)
9592 if (vlan->vlan_id != 0)
9593 return true;
9594
9595 return false;
9596 }
9597
9598 static int __hclge_enable_vport_vlan_filter(struct hclge_vport *vport,
9599 bool request_en)
9600 {
9601 bool need_en;
9602 int ret;
9603
9604 need_en = hclge_need_enable_vport_vlan_filter(vport);
9605 if (need_en == vport->cur_vlan_fltr_en)
9606 return 0;
9607
9608 ret = hclge_set_vport_vlan_filter(vport, need_en);
9609 if (ret)
9610 return ret;
9611
9612 vport->cur_vlan_fltr_en = need_en;
9613
9614 return 0;
9615 }
9616
9617 int hclge_enable_vport_vlan_filter(struct hclge_vport *vport, bool request_en)
9618 {
9619 struct hclge_dev *hdev = vport->back;
9620 int ret;
9621
9622 mutex_lock(&hdev->vport_lock);
9623 vport->req_vlan_fltr_en = request_en;
9624 ret = __hclge_enable_vport_vlan_filter(vport, request_en);
9625 mutex_unlock(&hdev->vport_lock);
9626
9627 return ret;
9628 }
9629
9630 static int hclge_enable_vlan_filter(struct hnae3_handle *handle, bool enable)
9631 {
9632 struct hclge_vport *vport = hclge_get_vport(handle);
9633
9634 return hclge_enable_vport_vlan_filter(vport, enable);
9635 }
9636
9637 static int hclge_set_vf_vlan_filter_cmd(struct hclge_dev *hdev, u16 vfid,
9638 bool is_kill, u16 vlan,
9639 struct hclge_desc *desc)
9640 {
9641 struct hclge_vlan_filter_vf_cfg_cmd *req0;
9642 struct hclge_vlan_filter_vf_cfg_cmd *req1;
9643 u8 vf_byte_val;
9644 u8 vf_byte_off;
9645 int ret;
9646
9647 hclge_cmd_setup_basic_desc(&desc[0],
9648 HCLGE_OPC_VLAN_FILTER_VF_CFG, false);
9649 hclge_cmd_setup_basic_desc(&desc[1],
9650 HCLGE_OPC_VLAN_FILTER_VF_CFG, false);
9651
9652 desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
9653
9654 vf_byte_off = vfid / 8;
9655 vf_byte_val = 1 << (vfid % 8);
9656
9657 req0 = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[0].data;
9658 req1 = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[1].data;
9659
9660 req0->vlan_id = cpu_to_le16(vlan);
9661 req0->vlan_cfg = is_kill;
9662
9663 if (vf_byte_off < HCLGE_MAX_VF_BYTES)
9664 req0->vf_bitmap[vf_byte_off] = vf_byte_val;
9665 else
9666 req1->vf_bitmap[vf_byte_off - HCLGE_MAX_VF_BYTES] = vf_byte_val;
9667
9668 ret = hclge_cmd_send(&hdev->hw, desc, 2);
9669 if (ret) {
9670 dev_err(&hdev->pdev->dev,
9671 "Send vf vlan command fail, ret =%d.\n",
9672 ret);
9673 return ret;
9674 }
9675
9676 return 0;
9677 }
9678
9679 static int hclge_check_vf_vlan_cmd_status(struct hclge_dev *hdev, u16 vfid,
9680 bool is_kill, struct hclge_desc *desc)
9681 {
9682 struct hclge_vlan_filter_vf_cfg_cmd *req;
9683
9684 req = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[0].data;
9685
9686 if (!is_kill) {
9687 #define HCLGE_VF_VLAN_NO_ENTRY 2
9688 if (!req->resp_code || req->resp_code == 1)
9689 return 0;
9690
9691 if (req->resp_code == HCLGE_VF_VLAN_NO_ENTRY) {
9692 set_bit(vfid, hdev->vf_vlan_full);
9693 dev_warn(&hdev->pdev->dev,
9694 "vf vlan table is full, vf vlan filter is disabled\n");
9695 return 0;
9696 }
9697
9698 dev_err(&hdev->pdev->dev,
9699 "Add vf vlan filter fail, ret =%u.\n",
9700 req->resp_code);
9701 } else {
9702 #define HCLGE_VF_VLAN_DEL_NO_FOUND 1
9703 if (!req->resp_code)
9704 return 0;
9705
9706 /* vf vlan filter is disabled when vf vlan table is full,
9707 * then new vlan id will not be added into vf vlan table.
9708 * Just return 0 without warning, avoid massive verbose
9709 * print logs when unload.
9710 */
9711 if (req->resp_code == HCLGE_VF_VLAN_DEL_NO_FOUND)
9712 return 0;
9713
9714 dev_err(&hdev->pdev->dev,
9715 "Kill vf vlan filter fail, ret =%u.\n",
9716 req->resp_code);
9717 }
9718
9719 return -EIO;
9720 }
9721
9722 static int hclge_set_vf_vlan_common(struct hclge_dev *hdev, u16 vfid,
9723 bool is_kill, u16 vlan)
9724 {
9725 struct hclge_vport *vport = &hdev->vport[vfid];
9726 struct hclge_desc desc[2];
9727 int ret;
9728
9729 /* if vf vlan table is full, firmware will close vf vlan filter, it
9730 * is unable and unnecessary to add new vlan id to vf vlan filter.
9731 * If spoof check is enable, and vf vlan is full, it shouldn't add
9732 * new vlan, because tx packets with these vlan id will be dropped.
9733 */
9734 if (test_bit(vfid, hdev->vf_vlan_full) && !is_kill) {
9735 if (vport->vf_info.spoofchk && vlan) {
9736 dev_err(&hdev->pdev->dev,
9737 "Can't add vlan due to spoof check is on and vf vlan table is full\n");
9738 return -EPERM;
9739 }
9740 return 0;
9741 }
9742
9743 ret = hclge_set_vf_vlan_filter_cmd(hdev, vfid, is_kill, vlan, desc);
9744 if (ret)
9745 return ret;
9746
9747 return hclge_check_vf_vlan_cmd_status(hdev, vfid, is_kill, desc);
9748 }
9749
9750 static int hclge_set_port_vlan_filter(struct hclge_dev *hdev, __be16 proto,
9751 u16 vlan_id, bool is_kill)
9752 {
9753 struct hclge_vlan_filter_pf_cfg_cmd *req;
9754 struct hclge_desc desc;
9755 u8 vlan_offset_byte_val;
9756 u8 vlan_offset_byte;
9757 u8 vlan_offset_160;
9758 int ret;
9759
9760 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_FILTER_PF_CFG, false);
9761
9762 vlan_offset_160 = vlan_id / HCLGE_VLAN_ID_OFFSET_STEP;
9763 vlan_offset_byte = (vlan_id % HCLGE_VLAN_ID_OFFSET_STEP) /
9764 HCLGE_VLAN_BYTE_SIZE;
9765 vlan_offset_byte_val = 1 << (vlan_id % HCLGE_VLAN_BYTE_SIZE);
9766
9767 req = (struct hclge_vlan_filter_pf_cfg_cmd *)desc.data;
9768 req->vlan_offset = vlan_offset_160;
9769 req->vlan_cfg = is_kill;
9770 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
9771
9772 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9773 if (ret)
9774 dev_err(&hdev->pdev->dev,
9775 "port vlan command, send fail, ret =%d.\n", ret);
9776 return ret;
9777 }
9778
9779 static bool hclge_need_update_port_vlan(struct hclge_dev *hdev, u16 vport_id,
9780 u16 vlan_id, bool is_kill)
9781 {
9782 /* vlan 0 may be added twice when 8021q module is enabled */
9783 if (!is_kill && !vlan_id &&
9784 test_bit(vport_id, hdev->vlan_table[vlan_id]))
9785 return false;
9786
9787 if (!is_kill && test_and_set_bit(vport_id, hdev->vlan_table[vlan_id])) {
9788 dev_warn(&hdev->pdev->dev,
9789 "Add port vlan failed, vport %u is already in vlan %u\n",
9790 vport_id, vlan_id);
9791 return false;
9792 }
9793
9794 if (is_kill &&
9795 !test_and_clear_bit(vport_id, hdev->vlan_table[vlan_id])) {
9796 dev_warn(&hdev->pdev->dev,
9797 "Delete port vlan failed, vport %u is not in vlan %u\n",
9798 vport_id, vlan_id);
9799 return false;
9800 }
9801
9802 return true;
9803 }
9804
9805 static int hclge_set_vlan_filter_hw(struct hclge_dev *hdev, __be16 proto,
9806 u16 vport_id, u16 vlan_id,
9807 bool is_kill)
9808 {
9809 u16 vport_idx, vport_num = 0;
9810 int ret;
9811
9812 if (is_kill && !vlan_id)
9813 return 0;
9814
9815 if (vlan_id >= VLAN_N_VID)
9816 return -EINVAL;
9817
9818 ret = hclge_set_vf_vlan_common(hdev, vport_id, is_kill, vlan_id);
9819 if (ret) {
9820 dev_err(&hdev->pdev->dev,
9821 "Set %u vport vlan filter config fail, ret =%d.\n",
9822 vport_id, ret);
9823 return ret;
9824 }
9825
9826 if (!hclge_need_update_port_vlan(hdev, vport_id, vlan_id, is_kill))
9827 return 0;
9828
9829 for_each_set_bit(vport_idx, hdev->vlan_table[vlan_id], HCLGE_VPORT_NUM)
9830 vport_num++;
9831
9832 if ((is_kill && vport_num == 0) || (!is_kill && vport_num == 1))
9833 ret = hclge_set_port_vlan_filter(hdev, proto, vlan_id,
9834 is_kill);
9835
9836 return ret;
9837 }
9838
9839 static int hclge_set_vlan_tx_offload_cfg(struct hclge_vport *vport)
9840 {
9841 struct hclge_tx_vtag_cfg *vcfg = &vport->txvlan_cfg;
9842 struct hclge_vport_vtag_tx_cfg_cmd *req;
9843 struct hclge_dev *hdev = vport->back;
9844 struct hclge_desc desc;
9845 u16 bmap_index;
9846 int status;
9847
9848 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_PORT_TX_CFG, false);
9849
9850 req = (struct hclge_vport_vtag_tx_cfg_cmd *)desc.data;
9851 req->def_vlan_tag1 = cpu_to_le16(vcfg->default_tag1);
9852 req->def_vlan_tag2 = cpu_to_le16(vcfg->default_tag2);
9853 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_TAG1_B,
9854 vcfg->accept_tag1 ? 1 : 0);
9855 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_UNTAG1_B,
9856 vcfg->accept_untag1 ? 1 : 0);
9857 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_TAG2_B,
9858 vcfg->accept_tag2 ? 1 : 0);
9859 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_UNTAG2_B,
9860 vcfg->accept_untag2 ? 1 : 0);
9861 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_PORT_INS_TAG1_EN_B,
9862 vcfg->insert_tag1_en ? 1 : 0);
9863 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_PORT_INS_TAG2_EN_B,
9864 vcfg->insert_tag2_en ? 1 : 0);
9865 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_TAG_SHIFT_MODE_EN_B,
9866 vcfg->tag_shift_mode_en ? 1 : 0);
9867 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_CFG_NIC_ROCE_SEL_B, 0);
9868
9869 req->vf_offset = vport->vport_id / HCLGE_VF_NUM_PER_CMD;
9870 bmap_index = vport->vport_id % HCLGE_VF_NUM_PER_CMD /
9871 HCLGE_VF_NUM_PER_BYTE;
9872 req->vf_bitmap[bmap_index] =
9873 1U << (vport->vport_id % HCLGE_VF_NUM_PER_BYTE);
9874
9875 status = hclge_cmd_send(&hdev->hw, &desc, 1);
9876 if (status)
9877 dev_err(&hdev->pdev->dev,
9878 "Send port txvlan cfg command fail, ret =%d\n",
9879 status);
9880
9881 return status;
9882 }
9883
9884 static int hclge_set_vlan_rx_offload_cfg(struct hclge_vport *vport)
9885 {
9886 struct hclge_rx_vtag_cfg *vcfg = &vport->rxvlan_cfg;
9887 struct hclge_vport_vtag_rx_cfg_cmd *req;
9888 struct hclge_dev *hdev = vport->back;
9889 struct hclge_desc desc;
9890 u16 bmap_index;
9891 int status;
9892
9893 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_PORT_RX_CFG, false);
9894
9895 req = (struct hclge_vport_vtag_rx_cfg_cmd *)desc.data;
9896 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_REM_TAG1_EN_B,
9897 vcfg->strip_tag1_en ? 1 : 0);
9898 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_REM_TAG2_EN_B,
9899 vcfg->strip_tag2_en ? 1 : 0);
9900 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_SHOW_TAG1_EN_B,
9901 vcfg->vlan1_vlan_prionly ? 1 : 0);
9902 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_SHOW_TAG2_EN_B,
9903 vcfg->vlan2_vlan_prionly ? 1 : 0);
9904 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_DISCARD_TAG1_EN_B,
9905 vcfg->strip_tag1_discard_en ? 1 : 0);
9906 hnae3_set_bit(req->vport_vlan_cfg, HCLGE_DISCARD_TAG2_EN_B,
9907 vcfg->strip_tag2_discard_en ? 1 : 0);
9908
9909 req->vf_offset = vport->vport_id / HCLGE_VF_NUM_PER_CMD;
9910 bmap_index = vport->vport_id % HCLGE_VF_NUM_PER_CMD /
9911 HCLGE_VF_NUM_PER_BYTE;
9912 req->vf_bitmap[bmap_index] =
9913 1U << (vport->vport_id % HCLGE_VF_NUM_PER_BYTE);
9914
9915 status = hclge_cmd_send(&hdev->hw, &desc, 1);
9916 if (status)
9917 dev_err(&hdev->pdev->dev,
9918 "Send port rxvlan cfg command fail, ret =%d\n",
9919 status);
9920
9921 return status;
9922 }
9923
9924 static int hclge_vlan_offload_cfg(struct hclge_vport *vport,
9925 u16 port_base_vlan_state,
9926 u16 vlan_tag, u8 qos)
9927 {
9928 int ret;
9929
9930 if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
9931 vport->txvlan_cfg.accept_tag1 = true;
9932 vport->txvlan_cfg.insert_tag1_en = false;
9933 vport->txvlan_cfg.default_tag1 = 0;
9934 } else {
9935 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(vport->nic.pdev);
9936
9937 vport->txvlan_cfg.accept_tag1 =
9938 ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3;
9939 vport->txvlan_cfg.insert_tag1_en = true;
9940 vport->txvlan_cfg.default_tag1 = (qos << VLAN_PRIO_SHIFT) |
9941 vlan_tag;
9942 }
9943
9944 vport->txvlan_cfg.accept_untag1 = true;
9945
9946 /* accept_tag2 and accept_untag2 are not supported on
9947 * pdev revision(0x20), new revision support them,
9948 * this two fields can not be configured by user.
9949 */
9950 vport->txvlan_cfg.accept_tag2 = true;
9951 vport->txvlan_cfg.accept_untag2 = true;
9952 vport->txvlan_cfg.insert_tag2_en = false;
9953 vport->txvlan_cfg.default_tag2 = 0;
9954 vport->txvlan_cfg.tag_shift_mode_en = true;
9955
9956 if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
9957 vport->rxvlan_cfg.strip_tag1_en = false;
9958 vport->rxvlan_cfg.strip_tag2_en =
9959 vport->rxvlan_cfg.rx_vlan_offload_en;
9960 vport->rxvlan_cfg.strip_tag2_discard_en = false;
9961 } else {
9962 vport->rxvlan_cfg.strip_tag1_en =
9963 vport->rxvlan_cfg.rx_vlan_offload_en;
9964 vport->rxvlan_cfg.strip_tag2_en = true;
9965 vport->rxvlan_cfg.strip_tag2_discard_en = true;
9966 }
9967
9968 vport->rxvlan_cfg.strip_tag1_discard_en = false;
9969 vport->rxvlan_cfg.vlan1_vlan_prionly = false;
9970 vport->rxvlan_cfg.vlan2_vlan_prionly = false;
9971
9972 ret = hclge_set_vlan_tx_offload_cfg(vport);
9973 if (ret)
9974 return ret;
9975
9976 return hclge_set_vlan_rx_offload_cfg(vport);
9977 }
9978
9979 static int hclge_set_vlan_protocol_type(struct hclge_dev *hdev)
9980 {
9981 struct hclge_rx_vlan_type_cfg_cmd *rx_req;
9982 struct hclge_tx_vlan_type_cfg_cmd *tx_req;
9983 struct hclge_desc desc;
9984 int status;
9985
9986 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_TYPE_ID, false);
9987 rx_req = (struct hclge_rx_vlan_type_cfg_cmd *)desc.data;
9988 rx_req->ot_fst_vlan_type =
9989 cpu_to_le16(hdev->vlan_type_cfg.rx_ot_fst_vlan_type);
9990 rx_req->ot_sec_vlan_type =
9991 cpu_to_le16(hdev->vlan_type_cfg.rx_ot_sec_vlan_type);
9992 rx_req->in_fst_vlan_type =
9993 cpu_to_le16(hdev->vlan_type_cfg.rx_in_fst_vlan_type);
9994 rx_req->in_sec_vlan_type =
9995 cpu_to_le16(hdev->vlan_type_cfg.rx_in_sec_vlan_type);
9996
9997 status = hclge_cmd_send(&hdev->hw, &desc, 1);
9998 if (status) {
9999 dev_err(&hdev->pdev->dev,
10000 "Send rxvlan protocol type command fail, ret =%d\n",
10001 status);
10002 return status;
10003 }
10004
10005 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_INSERT, false);
10006
10007 tx_req = (struct hclge_tx_vlan_type_cfg_cmd *)desc.data;
10008 tx_req->ot_vlan_type = cpu_to_le16(hdev->vlan_type_cfg.tx_ot_vlan_type);
10009 tx_req->in_vlan_type = cpu_to_le16(hdev->vlan_type_cfg.tx_in_vlan_type);
10010
10011 status = hclge_cmd_send(&hdev->hw, &desc, 1);
10012 if (status)
10013 dev_err(&hdev->pdev->dev,
10014 "Send txvlan protocol type command fail, ret =%d\n",
10015 status);
10016
10017 return status;
10018 }
10019
10020 static int hclge_init_vlan_filter(struct hclge_dev *hdev)
10021 {
10022 struct hclge_vport *vport;
10023 bool enable = true;
10024 int ret;
10025 int i;
10026
10027 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
10028 return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
10029 HCLGE_FILTER_FE_EGRESS_V1_B,
10030 true, 0);
10031
10032 /* for revision 0x21, vf vlan filter is per function */
10033 for (i = 0; i < hdev->num_alloc_vport; i++) {
10034 vport = &hdev->vport[i];
10035 ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
10036 HCLGE_FILTER_FE_EGRESS, true,
10037 vport->vport_id);
10038 if (ret)
10039 return ret;
10040 vport->cur_vlan_fltr_en = true;
10041 }
10042
10043 if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps) &&
10044 !test_bit(HNAE3_DEV_SUPPORT_PORT_VLAN_BYPASS_B, hdev->ae_dev->caps))
10045 enable = false;
10046
10047 return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_PORT,
10048 HCLGE_FILTER_FE_INGRESS, enable, 0);
10049 }
10050
10051 static int hclge_init_vlan_type(struct hclge_dev *hdev)
10052 {
10053 hdev->vlan_type_cfg.rx_in_fst_vlan_type = ETH_P_8021Q;
10054 hdev->vlan_type_cfg.rx_in_sec_vlan_type = ETH_P_8021Q;
10055 hdev->vlan_type_cfg.rx_ot_fst_vlan_type = ETH_P_8021Q;
10056 hdev->vlan_type_cfg.rx_ot_sec_vlan_type = ETH_P_8021Q;
10057 hdev->vlan_type_cfg.tx_ot_vlan_type = ETH_P_8021Q;
10058 hdev->vlan_type_cfg.tx_in_vlan_type = ETH_P_8021Q;
10059
10060 return hclge_set_vlan_protocol_type(hdev);
10061 }
10062
10063 static int hclge_init_vport_vlan_offload(struct hclge_dev *hdev)
10064 {
10065 struct hclge_port_base_vlan_config *cfg;
10066 struct hclge_vport *vport;
10067 int ret;
10068 int i;
10069
10070 for (i = 0; i < hdev->num_alloc_vport; i++) {
10071 vport = &hdev->vport[i];
10072 cfg = &vport->port_base_vlan_cfg;
10073
10074 ret = hclge_vlan_offload_cfg(vport, cfg->state,
10075 cfg->vlan_info.vlan_tag,
10076 cfg->vlan_info.qos);
10077 if (ret)
10078 return ret;
10079 }
10080 return 0;
10081 }
10082
10083 static int hclge_init_vlan_config(struct hclge_dev *hdev)
10084 {
10085 struct hnae3_handle *handle = &hdev->vport[0].nic;
10086 int ret;
10087
10088 ret = hclge_init_vlan_filter(hdev);
10089 if (ret)
10090 return ret;
10091
10092 ret = hclge_init_vlan_type(hdev);
10093 if (ret)
10094 return ret;
10095
10096 ret = hclge_init_vport_vlan_offload(hdev);
10097 if (ret)
10098 return ret;
10099
10100 return hclge_set_vlan_filter(handle, htons(ETH_P_8021Q), 0, false);
10101 }
10102
10103 static void hclge_add_vport_vlan_table(struct hclge_vport *vport, u16 vlan_id,
10104 bool writen_to_tbl)
10105 {
10106 struct hclge_vport_vlan_cfg *vlan, *tmp;
10107 struct hclge_dev *hdev = vport->back;
10108
10109 mutex_lock(&hdev->vport_lock);
10110
10111 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10112 if (vlan->vlan_id == vlan_id) {
10113 mutex_unlock(&hdev->vport_lock);
10114 return;
10115 }
10116 }
10117
10118 vlan = kzalloc_obj(*vlan);
10119 if (!vlan) {
10120 mutex_unlock(&hdev->vport_lock);
10121 return;
10122 }
10123
10124 vlan->hd_tbl_status = writen_to_tbl;
10125 vlan->vlan_id = vlan_id;
10126
10127 list_add_tail(&vlan->node, &vport->vlan_list);
10128 mutex_unlock(&hdev->vport_lock);
10129 }
10130
10131 static int hclge_add_vport_all_vlan_table(struct hclge_vport *vport)
10132 {
10133 struct hclge_vport_vlan_cfg *vlan, *tmp;
10134 struct hclge_dev *hdev = vport->back;
10135 int ret;
10136
10137 mutex_lock(&hdev->vport_lock);
10138
10139 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10140 if (!vlan->hd_tbl_status) {
10141 ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
10142 vport->vport_id,
10143 vlan->vlan_id, false);
10144 if (ret) {
10145 dev_err(&hdev->pdev->dev,
10146 "restore vport vlan list failed, ret=%d\n",
10147 ret);
10148
10149 mutex_unlock(&hdev->vport_lock);
10150 return ret;
10151 }
10152 }
10153 vlan->hd_tbl_status = true;
10154 }
10155
10156 mutex_unlock(&hdev->vport_lock);
10157
10158 return 0;
10159 }
10160
10161 static void hclge_rm_vport_vlan_table(struct hclge_vport *vport, u16 vlan_id,
10162 bool is_write_tbl)
10163 {
10164 struct hclge_vport_vlan_cfg *vlan, *tmp;
10165 struct hclge_dev *hdev = vport->back;
10166
10167 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10168 if (vlan->vlan_id == vlan_id) {
10169 if (is_write_tbl && vlan->hd_tbl_status)
10170 hclge_set_vlan_filter_hw(hdev,
10171 htons(ETH_P_8021Q),
10172 vport->vport_id,
10173 vlan_id,
10174 true);
10175
10176 list_del(&vlan->node);
10177 kfree(vlan);
10178 break;
10179 }
10180 }
10181 }
10182
10183 void hclge_rm_vport_all_vlan_table(struct hclge_vport *vport, bool is_del_list)
10184 {
10185 struct hclge_vport_vlan_cfg *vlan, *tmp;
10186 struct hclge_dev *hdev = vport->back;
10187
10188 mutex_lock(&hdev->vport_lock);
10189
10190 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10191 if (vlan->hd_tbl_status)
10192 hclge_set_vlan_filter_hw(hdev,
10193 htons(ETH_P_8021Q),
10194 vport->vport_id,
10195 vlan->vlan_id,
10196 true);
10197
10198 vlan->hd_tbl_status = false;
10199 if (is_del_list) {
10200 list_del(&vlan->node);
10201 kfree(vlan);
10202 }
10203 }
10204 clear_bit(vport->vport_id, hdev->vf_vlan_full);
10205 mutex_unlock(&hdev->vport_lock);
10206 }
10207
10208 void hclge_uninit_vport_vlan_table(struct hclge_dev *hdev)
10209 {
10210 struct hclge_vport_vlan_cfg *vlan, *tmp;
10211 struct hclge_vport *vport;
10212 int i;
10213
10214 mutex_lock(&hdev->vport_lock);
10215
10216 for (i = 0; i < hdev->num_alloc_vport; i++) {
10217 vport = &hdev->vport[i];
10218 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10219 list_del(&vlan->node);
10220 kfree(vlan);
10221 }
10222 }
10223
10224 mutex_unlock(&hdev->vport_lock);
10225 }
10226
10227 void hclge_restore_vport_port_base_vlan_config(struct hclge_dev *hdev)
10228 {
10229 struct hclge_vlan_info *vlan_info;
10230 struct hclge_vport *vport;
10231 u16 vlan_proto;
10232 u16 vlan_id;
10233 u16 state;
10234 int vf_id;
10235 int ret;
10236
10237 /* PF should restore all vfs port base vlan */
10238 for (vf_id = 0; vf_id < hdev->num_alloc_vfs; vf_id++) {
10239 vport = &hdev->vport[vf_id + HCLGE_VF_VPORT_START_NUM];
10240 vlan_info = vport->port_base_vlan_cfg.tbl_sta ?
10241 &vport->port_base_vlan_cfg.vlan_info :
10242 &vport->port_base_vlan_cfg.old_vlan_info;
10243
10244 vlan_id = vlan_info->vlan_tag;
10245 vlan_proto = vlan_info->vlan_proto;
10246 state = vport->port_base_vlan_cfg.state;
10247
10248 if (state != HNAE3_PORT_BASE_VLAN_DISABLE) {
10249 clear_bit(vport->vport_id, hdev->vlan_table[vlan_id]);
10250 ret = hclge_set_vlan_filter_hw(hdev, htons(vlan_proto),
10251 vport->vport_id,
10252 vlan_id, false);
10253 vport->port_base_vlan_cfg.tbl_sta = ret == 0;
10254 }
10255 }
10256 }
10257
10258 void hclge_restore_vport_vlan_table(struct hclge_vport *vport)
10259 {
10260 struct hclge_vport_vlan_cfg *vlan, *tmp;
10261 struct hclge_dev *hdev = vport->back;
10262 int ret;
10263
10264 mutex_lock(&hdev->vport_lock);
10265
10266 if (vport->port_base_vlan_cfg.state == HNAE3_PORT_BASE_VLAN_DISABLE) {
10267 list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
10268 ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
10269 vport->vport_id,
10270 vlan->vlan_id, false);
10271 if (ret)
10272 break;
10273 vlan->hd_tbl_status = true;
10274 }
10275 }
10276
10277 mutex_unlock(&hdev->vport_lock);
10278 }
10279
10280 /* For global reset and imp reset, hardware will clear the mac table,
10281 * so we change the mac address state from ACTIVE to TO_ADD, then they
10282 * can be restored in the service task after reset complete. Furtherly,
10283 * the mac addresses with state TO_DEL or DEL_FAIL are unnecessary to
10284 * be restored after reset, so just remove these mac nodes from mac_list.
10285 */
10286 static void hclge_mac_node_convert_for_reset(struct list_head *list)
10287 {
10288 struct hclge_mac_node *mac_node, *tmp;
10289
10290 list_for_each_entry_safe(mac_node, tmp, list, node) {
10291 if (mac_node->state == HCLGE_MAC_ACTIVE) {
10292 mac_node->state = HCLGE_MAC_TO_ADD;
10293 } else if (mac_node->state == HCLGE_MAC_TO_DEL) {
10294 list_del(&mac_node->node);
10295 kfree(mac_node);
10296 }
10297 }
10298 }
10299
10300 void hclge_restore_mac_table_common(struct hclge_vport *vport)
10301 {
10302 spin_lock_bh(&vport->mac_list_lock);
10303
10304 hclge_mac_node_convert_for_reset(&vport->uc_mac_list);
10305 hclge_mac_node_convert_for_reset(&vport->mc_mac_list);
10306 set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
10307
10308 spin_unlock_bh(&vport->mac_list_lock);
10309 }
10310
10311 static void hclge_restore_hw_table(struct hclge_dev *hdev)
10312 {
10313 struct hclge_vport *vport = &hdev->vport[0];
10314 struct hnae3_handle *handle = &vport->nic;
10315
10316 hclge_restore_mac_table_common(vport);
10317 hclge_restore_vport_port_base_vlan_config(hdev);
10318 hclge_restore_vport_vlan_table(vport);
10319 set_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state);
10320 hclge_restore_fd_entries(handle);
10321 }
10322
10323 int hclge_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
10324 {
10325 struct hclge_vport *vport = hclge_get_vport(handle);
10326
10327 if (vport->port_base_vlan_cfg.state == HNAE3_PORT_BASE_VLAN_DISABLE) {
10328 vport->rxvlan_cfg.strip_tag1_en = false;
10329 vport->rxvlan_cfg.strip_tag2_en = enable;
10330 vport->rxvlan_cfg.strip_tag2_discard_en = false;
10331 } else {
10332 vport->rxvlan_cfg.strip_tag1_en = enable;
10333 vport->rxvlan_cfg.strip_tag2_en = true;
10334 vport->rxvlan_cfg.strip_tag2_discard_en = true;
10335 }
10336
10337 vport->rxvlan_cfg.strip_tag1_discard_en = false;
10338 vport->rxvlan_cfg.vlan1_vlan_prionly = false;
10339 vport->rxvlan_cfg.vlan2_vlan_prionly = false;
10340 vport->rxvlan_cfg.rx_vlan_offload_en = enable;
10341
10342 return hclge_set_vlan_rx_offload_cfg(vport);
10343 }
10344
10345 static void hclge_set_vport_vlan_fltr_change(struct hclge_vport *vport)
10346 {
10347 struct hclge_dev *hdev = vport->back;
10348
10349 if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps))
10350 set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE, &vport->state);
10351 }
10352
10353 static int hclge_update_vlan_filter_entries(struct hclge_vport *vport,
10354 u16 port_base_vlan_state,
10355 struct hclge_vlan_info *new_info,
10356 struct hclge_vlan_info *old_info)
10357 {
10358 struct hclge_dev *hdev = vport->back;
10359 int ret;
10360
10361 if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_ENABLE) {
10362 hclge_rm_vport_all_vlan_table(vport, false);
10363 /* force clear VLAN 0 */
10364 ret = hclge_set_vf_vlan_common(hdev, vport->vport_id, true, 0);
10365 if (ret)
10366 return ret;
10367 return hclge_set_vlan_filter_hw(hdev,
10368 htons(new_info->vlan_proto),
10369 vport->vport_id,
10370 new_info->vlan_tag,
10371 false);
10372 }
10373
10374 vport->port_base_vlan_cfg.tbl_sta = false;
10375
10376 /* force add VLAN 0 */
10377 ret = hclge_set_vf_vlan_common(hdev, vport->vport_id, false, 0);
10378 if (ret)
10379 return ret;
10380
10381 ret = hclge_set_vlan_filter_hw(hdev, htons(old_info->vlan_proto),
10382 vport->vport_id, old_info->vlan_tag,
10383 true);
10384 if (ret)
10385 return ret;
10386
10387 return hclge_add_vport_all_vlan_table(vport);
10388 }
10389
10390 static bool hclge_need_update_vlan_filter(const struct hclge_vlan_info *new_cfg,
10391 const struct hclge_vlan_info *old_cfg)
10392 {
10393 if (new_cfg->vlan_tag != old_cfg->vlan_tag)
10394 return true;
10395
10396 if (new_cfg->vlan_tag == 0 && (new_cfg->qos == 0 || old_cfg->qos == 0))
10397 return true;
10398
10399 return false;
10400 }
10401
10402 static int hclge_modify_port_base_vlan_tag(struct hclge_vport *vport,
10403 struct hclge_vlan_info *new_info,
10404 struct hclge_vlan_info *old_info)
10405 {
10406 struct hclge_dev *hdev = vport->back;
10407 int ret;
10408
10409 /* add new VLAN tag */
10410 ret = hclge_set_vlan_filter_hw(hdev, htons(new_info->vlan_proto),
10411 vport->vport_id, new_info->vlan_tag,
10412 false);
10413 if (ret)
10414 return ret;
10415
10416 vport->port_base_vlan_cfg.tbl_sta = false;
10417 /* remove old VLAN tag */
10418 if (old_info->vlan_tag == 0)
10419 ret = hclge_set_vf_vlan_common(hdev, vport->vport_id,
10420 true, 0);
10421 else
10422 ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
10423 vport->vport_id,
10424 old_info->vlan_tag, true);
10425 if (ret)
10426 dev_err(&hdev->pdev->dev,
10427 "failed to clear vport%u port base vlan %u, ret = %d.\n",
10428 vport->vport_id, old_info->vlan_tag, ret);
10429
10430 return ret;
10431 }
10432
10433 int hclge_update_port_base_vlan_cfg(struct hclge_vport *vport, u16 state,
10434 struct hclge_vlan_info *vlan_info)
10435 {
10436 struct hnae3_handle *nic = &vport->nic;
10437 struct hclge_vlan_info *old_vlan_info;
10438 int ret;
10439
10440 old_vlan_info = &vport->port_base_vlan_cfg.vlan_info;
10441
10442 ret = hclge_vlan_offload_cfg(vport, state, vlan_info->vlan_tag,
10443 vlan_info->qos);
10444 if (ret)
10445 return ret;
10446
10447 if (!hclge_need_update_vlan_filter(vlan_info, old_vlan_info))
10448 goto out;
10449
10450 if (state == HNAE3_PORT_BASE_VLAN_MODIFY)
10451 ret = hclge_modify_port_base_vlan_tag(vport, vlan_info,
10452 old_vlan_info);
10453 else
10454 ret = hclge_update_vlan_filter_entries(vport, state, vlan_info,
10455 old_vlan_info);
10456 if (ret)
10457 return ret;
10458
10459 out:
10460 vport->port_base_vlan_cfg.state = state;
10461 if (state == HNAE3_PORT_BASE_VLAN_DISABLE)
10462 nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_DISABLE;
10463 else
10464 nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE;
10465
10466 vport->port_base_vlan_cfg.old_vlan_info = *old_vlan_info;
10467 vport->port_base_vlan_cfg.vlan_info = *vlan_info;
10468 vport->port_base_vlan_cfg.tbl_sta = true;
10469 hclge_set_vport_vlan_fltr_change(vport);
10470
10471 return 0;
10472 }
10473
10474 static u16 hclge_get_port_base_vlan_state(struct hclge_vport *vport,
10475 enum hnae3_port_base_vlan_state state,
10476 u16 vlan, u8 qos)
10477 {
10478 if (state == HNAE3_PORT_BASE_VLAN_DISABLE) {
10479 if (!vlan && !qos)
10480 return HNAE3_PORT_BASE_VLAN_NOCHANGE;
10481
10482 return HNAE3_PORT_BASE_VLAN_ENABLE;
10483 }
10484
10485 if (!vlan && !qos)
10486 return HNAE3_PORT_BASE_VLAN_DISABLE;
10487
10488 if (vport->port_base_vlan_cfg.vlan_info.vlan_tag == vlan &&
10489 vport->port_base_vlan_cfg.vlan_info.qos == qos)
10490 return HNAE3_PORT_BASE_VLAN_NOCHANGE;
10491
10492 return HNAE3_PORT_BASE_VLAN_MODIFY;
10493 }
10494
10495 static int hclge_set_vf_vlan_filter(struct hnae3_handle *handle, int vfid,
10496 u16 vlan, u8 qos, __be16 proto)
10497 {
10498 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
10499 struct hclge_vport *vport = hclge_get_vport(handle);
10500 struct hclge_dev *hdev = vport->back;
10501 struct hclge_vlan_info vlan_info;
10502 u16 state;
10503 int ret;
10504
10505 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
10506 return -EOPNOTSUPP;
10507
10508 vport = hclge_get_vf_vport(hdev, vfid);
10509 if (!vport)
10510 return -EINVAL;
10511
10512 /* qos is a 3 bits value, so can not be bigger than 7 */
10513 if (vlan > VLAN_N_VID - 1 || qos > 7)
10514 return -EINVAL;
10515 if (proto != htons(ETH_P_8021Q))
10516 return -EPROTONOSUPPORT;
10517
10518 state = hclge_get_port_base_vlan_state(vport,
10519 vport->port_base_vlan_cfg.state,
10520 vlan, qos);
10521 if (state == HNAE3_PORT_BASE_VLAN_NOCHANGE)
10522 return 0;
10523
10524 vlan_info.vlan_tag = vlan;
10525 vlan_info.qos = qos;
10526 vlan_info.vlan_proto = ntohs(proto);
10527
10528 ret = hclge_update_port_base_vlan_cfg(vport, state, &vlan_info);
10529 if (ret) {
10530 dev_err(&hdev->pdev->dev,
10531 "failed to update port base vlan for vf %d, ret = %d\n",
10532 vfid, ret);
10533 return ret;
10534 }
10535
10536 /* there is a timewindow for PF to know VF unalive, it may
10537 * cause send mailbox fail, but it doesn't matter, VF will
10538 * query it when reinit.
10539 * for DEVICE_VERSION_V3, vf doesn't need to know about the port based
10540 * VLAN state.
10541 */
10542 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
10543 if (test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
10544 (void)hclge_push_vf_port_base_vlan_info(&hdev->vport[0],
10545 vport->vport_id,
10546 state,
10547 &vlan_info);
10548 else
10549 set_bit(HCLGE_VPORT_NEED_NOTIFY_VF_VLAN,
10550 &vport->need_notify);
10551 }
10552 return 0;
10553 }
10554
10555 static void hclge_clear_vf_vlan(struct hclge_dev *hdev)
10556 {
10557 struct hclge_vlan_info *vlan_info;
10558 struct hclge_vport *vport;
10559 int ret;
10560 int vf;
10561
10562 /* clear port base vlan for all vf */
10563 for (vf = HCLGE_VF_VPORT_START_NUM; vf < hdev->num_alloc_vport; vf++) {
10564 vport = &hdev->vport[vf];
10565 vlan_info = &vport->port_base_vlan_cfg.vlan_info;
10566
10567 ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
10568 vport->vport_id,
10569 vlan_info->vlan_tag, true);
10570 if (ret)
10571 dev_err(&hdev->pdev->dev,
10572 "failed to clear vf vlan for vf%d, ret = %d\n",
10573 vf - HCLGE_VF_VPORT_START_NUM, ret);
10574 }
10575 }
10576
10577 int hclge_set_vlan_filter(struct hnae3_handle *handle, __be16 proto,
10578 u16 vlan_id, bool is_kill)
10579 {
10580 struct hclge_vport *vport = hclge_get_vport(handle);
10581 struct hclge_dev *hdev = vport->back;
10582 bool writen_to_tbl = false;
10583 int ret = 0;
10584
10585 if (vlan_id >= VLAN_N_VID)
10586 return -EINVAL;
10587
10588 /* When device is resetting or reset failed, firmware is unable to
10589 * handle mailbox. Just record the vlan id, and remove it after
10590 * reset finished.
10591 */
10592 mutex_lock(&hdev->vport_lock);
10593 if ((test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
10594 test_bit(HCLGE_STATE_RST_FAIL, &hdev->state)) && is_kill) {
10595 set_bit(vlan_id, vport->vlan_del_fail_bmap);
10596 mutex_unlock(&hdev->vport_lock);
10597 return -EBUSY;
10598 } else if (!is_kill && test_bit(vlan_id, vport->vlan_del_fail_bmap)) {
10599 clear_bit(vlan_id, vport->vlan_del_fail_bmap);
10600 }
10601 mutex_unlock(&hdev->vport_lock);
10602
10603 /* when port base vlan enabled, we use port base vlan as the vlan
10604 * filter entry. In this case, we don't update vlan filter table
10605 * when user add new vlan or remove exist vlan, just update the vport
10606 * vlan list. The vlan id in vlan list will be writen in vlan filter
10607 * table until port base vlan disabled
10608 */
10609 if (handle->port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
10610 ret = hclge_set_vlan_filter_hw(hdev, proto, vport->vport_id,
10611 vlan_id, is_kill);
10612 writen_to_tbl = true;
10613 }
10614
10615 if (!ret) {
10616 if (!is_kill) {
10617 hclge_add_vport_vlan_table(vport, vlan_id,
10618 writen_to_tbl);
10619 } else if (is_kill && vlan_id != 0) {
10620 mutex_lock(&hdev->vport_lock);
10621 hclge_rm_vport_vlan_table(vport, vlan_id, false);
10622 mutex_unlock(&hdev->vport_lock);
10623 }
10624 } else if (is_kill) {
10625 /* when remove hw vlan filter failed, record the vlan id,
10626 * and try to remove it from hw later, to be consistence
10627 * with stack
10628 */
10629 mutex_lock(&hdev->vport_lock);
10630 set_bit(vlan_id, vport->vlan_del_fail_bmap);
10631 mutex_unlock(&hdev->vport_lock);
10632 }
10633
10634 hclge_set_vport_vlan_fltr_change(vport);
10635
10636 return ret;
10637 }
10638
10639 static void hclge_sync_vlan_fltr_state(struct hclge_dev *hdev)
10640 {
10641 struct hclge_vport *vport;
10642 int ret;
10643 u16 i;
10644
10645 for (i = 0; i < hdev->num_alloc_vport; i++) {
10646 vport = &hdev->vport[i];
10647 if (!test_and_clear_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
10648 &vport->state))
10649 continue;
10650
10651 mutex_lock(&hdev->vport_lock);
10652 ret = __hclge_enable_vport_vlan_filter(vport,
10653 vport->req_vlan_fltr_en);
10654 if (ret) {
10655 dev_err(&hdev->pdev->dev,
10656 "failed to sync vlan filter state for vport%u, ret = %d\n",
10657 vport->vport_id, ret);
10658 set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
10659 &vport->state);
10660 mutex_unlock(&hdev->vport_lock);
10661 return;
10662 }
10663 mutex_unlock(&hdev->vport_lock);
10664 }
10665 }
10666
10667 static void hclge_sync_vlan_filter(struct hclge_dev *hdev)
10668 {
10669 #define HCLGE_MAX_SYNC_COUNT 60
10670
10671 int i, ret, sync_cnt = 0;
10672 u16 vlan_id;
10673
10674 mutex_lock(&hdev->vport_lock);
10675 /* start from vport 1 for PF is always alive */
10676 for (i = 0; i < hdev->num_alloc_vport; i++) {
10677 struct hclge_vport *vport = &hdev->vport[i];
10678
10679 vlan_id = find_first_bit(vport->vlan_del_fail_bmap,
10680 VLAN_N_VID);
10681 while (vlan_id != VLAN_N_VID) {
10682 ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
10683 vport->vport_id, vlan_id,
10684 true);
10685 if (ret && ret != -EINVAL) {
10686 mutex_unlock(&hdev->vport_lock);
10687 return;
10688 }
10689
10690 clear_bit(vlan_id, vport->vlan_del_fail_bmap);
10691 hclge_rm_vport_vlan_table(vport, vlan_id, false);
10692 hclge_set_vport_vlan_fltr_change(vport);
10693
10694 sync_cnt++;
10695 if (sync_cnt >= HCLGE_MAX_SYNC_COUNT) {
10696 mutex_unlock(&hdev->vport_lock);
10697 return;
10698 }
10699
10700 vlan_id = find_first_bit(vport->vlan_del_fail_bmap,
10701 VLAN_N_VID);
10702 }
10703 }
10704 mutex_unlock(&hdev->vport_lock);
10705
10706 hclge_sync_vlan_fltr_state(hdev);
10707 }
10708
10709 static int hclge_set_mac_mtu(struct hclge_dev *hdev, int new_mps)
10710 {
10711 struct hclge_config_max_frm_size_cmd *req;
10712 struct hclge_desc desc;
10713
10714 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAX_FRM_SIZE, false);
10715
10716 req = (struct hclge_config_max_frm_size_cmd *)desc.data;
10717 req->max_frm_size = cpu_to_le16(new_mps);
10718 req->min_frm_size = HCLGE_MAC_MIN_FRAME;
10719
10720 return hclge_cmd_send(&hdev->hw, &desc, 1);
10721 }
10722
10723 static int hclge_set_mtu(struct hnae3_handle *handle, int new_mtu)
10724 {
10725 struct hclge_vport *vport = hclge_get_vport(handle);
10726
10727 return hclge_set_vport_mtu(vport, new_mtu);
10728 }
10729
10730 int hclge_set_vport_mtu(struct hclge_vport *vport, int new_mtu)
10731 {
10732 struct hclge_dev *hdev = vport->back;
10733 int i, max_frm_size, ret;
10734
10735 /* HW supprt 2 layer vlan */
10736 max_frm_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + 2 * VLAN_HLEN;
10737 if (max_frm_size < HCLGE_MAC_MIN_FRAME ||
10738 max_frm_size > hdev->ae_dev->dev_specs.max_frm_size)
10739 return -EINVAL;
10740
10741 max_frm_size = max(max_frm_size, HCLGE_MAC_DEFAULT_FRAME);
10742 mutex_lock(&hdev->vport_lock);
10743 /* VF's mps must fit within hdev->mps */
10744 if (vport->vport_id && (u32)max_frm_size > hdev->mps) {
10745 mutex_unlock(&hdev->vport_lock);
10746 return -EINVAL;
10747 } else if (vport->vport_id) {
10748 vport->mps = max_frm_size;
10749 mutex_unlock(&hdev->vport_lock);
10750 return 0;
10751 }
10752
10753 /* PF's mps must be greater then VF's mps */
10754 for (i = 1; i < hdev->num_alloc_vport; i++)
10755 if ((u32)max_frm_size < hdev->vport[i].mps) {
10756 dev_err(&hdev->pdev->dev,
10757 "failed to set pf mtu for less than vport %d, mps = %u.\n",
10758 i, hdev->vport[i].mps);
10759 mutex_unlock(&hdev->vport_lock);
10760 return -EINVAL;
10761 }
10762
10763 hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
10764
10765 ret = hclge_set_mac_mtu(hdev, max_frm_size);
10766 if (ret) {
10767 dev_err(&hdev->pdev->dev,
10768 "Change mtu fail, ret =%d\n", ret);
10769 goto out;
10770 }
10771
10772 hdev->mps = max_frm_size;
10773 vport->mps = max_frm_size;
10774
10775 ret = hclge_buffer_alloc(hdev);
10776 if (ret)
10777 dev_err(&hdev->pdev->dev,
10778 "Allocate buffer fail, ret =%d\n", ret);
10779
10780 out:
10781 hclge_notify_client(hdev, HNAE3_UP_CLIENT);
10782 mutex_unlock(&hdev->vport_lock);
10783 return ret;
10784 }
10785
10786 static int hclge_reset_tqp_cmd_send(struct hclge_dev *hdev, u16 queue_id,
10787 bool enable)
10788 {
10789 struct hclge_reset_tqp_queue_cmd *req;
10790 struct hclge_desc desc;
10791 int ret;
10792
10793 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RESET_TQP_QUEUE, false);
10794
10795 req = (struct hclge_reset_tqp_queue_cmd *)desc.data;
10796 req->tqp_id = cpu_to_le16(queue_id);
10797 if (enable)
10798 hnae3_set_bit(req->reset_req, HCLGE_TQP_RESET_B, 1U);
10799
10800 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10801 if (ret) {
10802 dev_err(&hdev->pdev->dev,
10803 "Send tqp reset cmd error, status =%d\n", ret);
10804 return ret;
10805 }
10806
10807 return 0;
10808 }
10809
10810 static int hclge_get_reset_status(struct hclge_dev *hdev, u16 queue_id,
10811 u8 *reset_status)
10812 {
10813 struct hclge_reset_tqp_queue_cmd *req;
10814 struct hclge_desc desc;
10815 int ret;
10816
10817 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RESET_TQP_QUEUE, true);
10818
10819 req = (struct hclge_reset_tqp_queue_cmd *)desc.data;
10820 req->tqp_id = cpu_to_le16(queue_id);
10821
10822 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10823 if (ret) {
10824 dev_err(&hdev->pdev->dev,
10825 "Get reset status error, status =%d\n", ret);
10826 return ret;
10827 }
10828
10829 *reset_status = hnae3_get_bit(req->ready_to_reset, HCLGE_TQP_RESET_B);
10830
10831 return 0;
10832 }
10833
10834 u16 hclge_covert_handle_qid_global(struct hnae3_handle *handle, u16 queue_id)
10835 {
10836 struct hclge_comm_tqp *tqp;
10837 struct hnae3_queue *queue;
10838
10839 queue = handle->kinfo.tqp[queue_id];
10840 tqp = container_of(queue, struct hclge_comm_tqp, q);
10841
10842 return tqp->index;
10843 }
10844
10845 static int hclge_reset_tqp_cmd(struct hnae3_handle *handle)
10846 {
10847 struct hclge_vport *vport = hclge_get_vport(handle);
10848 struct hclge_dev *hdev = vport->back;
10849 u16 reset_try_times = 0;
10850 u8 reset_status;
10851 u16 queue_gid;
10852 int ret;
10853 u16 i;
10854
10855 for (i = 0; i < handle->kinfo.num_tqps; i++) {
10856 queue_gid = hclge_covert_handle_qid_global(handle, i);
10857 ret = hclge_reset_tqp_cmd_send(hdev, queue_gid, true);
10858 if (ret) {
10859 dev_err(&hdev->pdev->dev,
10860 "failed to send reset tqp cmd, ret = %d\n",
10861 ret);
10862 return ret;
10863 }
10864
10865 while (reset_try_times++ < HCLGE_TQP_RESET_TRY_TIMES) {
10866 ret = hclge_get_reset_status(hdev, queue_gid,
10867 &reset_status);
10868 if (ret)
10869 return ret;
10870
10871 if (reset_status)
10872 break;
10873
10874 /* Wait for tqp hw reset */
10875 usleep_range(1000, 1200);
10876 }
10877
10878 if (reset_try_times >= HCLGE_TQP_RESET_TRY_TIMES) {
10879 dev_err(&hdev->pdev->dev,
10880 "wait for tqp hw reset timeout\n");
10881 return -ETIME;
10882 }
10883
10884 ret = hclge_reset_tqp_cmd_send(hdev, queue_gid, false);
10885 if (ret) {
10886 dev_err(&hdev->pdev->dev,
10887 "failed to deassert soft reset, ret = %d\n",
10888 ret);
10889 return ret;
10890 }
10891 reset_try_times = 0;
10892 }
10893 return 0;
10894 }
10895
10896 static int hclge_reset_rcb(struct hnae3_handle *handle)
10897 {
10898 #define HCLGE_RESET_RCB_NOT_SUPPORT 0U
10899 #define HCLGE_RESET_RCB_SUCCESS 1U
10900
10901 struct hclge_vport *vport = hclge_get_vport(handle);
10902 struct hclge_dev *hdev = vport->back;
10903 struct hclge_reset_cmd *req;
10904 struct hclge_desc desc;
10905 u8 return_status;
10906 u16 queue_gid;
10907 int ret;
10908
10909 queue_gid = hclge_covert_handle_qid_global(handle, 0);
10910
10911 req = (struct hclge_reset_cmd *)desc.data;
10912 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_RST_TRIGGER, false);
10913 hnae3_set_bit(req->fun_reset_rcb, HCLGE_CFG_RESET_RCB_B, 1);
10914 req->fun_reset_rcb_vqid_start = cpu_to_le16(queue_gid);
10915 req->fun_reset_rcb_vqid_num = cpu_to_le16(handle->kinfo.num_tqps);
10916
10917 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10918 if (ret) {
10919 dev_err(&hdev->pdev->dev,
10920 "failed to send rcb reset cmd, ret = %d\n", ret);
10921 return ret;
10922 }
10923
10924 return_status = req->fun_reset_rcb_return_status;
10925 if (return_status == HCLGE_RESET_RCB_SUCCESS)
10926 return 0;
10927
10928 if (return_status != HCLGE_RESET_RCB_NOT_SUPPORT) {
10929 dev_err(&hdev->pdev->dev, "failed to reset rcb, ret = %u\n",
10930 return_status);
10931 return -EIO;
10932 }
10933
10934 /* if reset rcb cmd is unsupported, we need to send reset tqp cmd
10935 * again to reset all tqps
10936 */
10937 return hclge_reset_tqp_cmd(handle);
10938 }
10939
10940 int hclge_reset_tqp(struct hnae3_handle *handle)
10941 {
10942 struct hclge_vport *vport = hclge_get_vport(handle);
10943 struct hclge_dev *hdev = vport->back;
10944 int ret;
10945
10946 /* only need to disable PF's tqp */
10947 if (!vport->vport_id) {
10948 ret = hclge_tqp_enable(handle, false);
10949 if (ret) {
10950 dev_err(&hdev->pdev->dev,
10951 "failed to disable tqp, ret = %d\n", ret);
10952 return ret;
10953 }
10954 }
10955
10956 return hclge_reset_rcb(handle);
10957 }
10958
10959 static u32 hclge_get_fw_version(struct hnae3_handle *handle)
10960 {
10961 struct hclge_vport *vport = hclge_get_vport(handle);
10962 struct hclge_dev *hdev = vport->back;
10963
10964 return hdev->fw_version;
10965 }
10966
10967 int hclge_query_scc_version(struct hclge_dev *hdev, u32 *scc_version)
10968 {
10969 struct hclge_comm_query_scc_cmd *resp;
10970 struct hclge_desc desc;
10971 int ret;
10972
10973 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_SCC_VER, 1);
10974 resp = (struct hclge_comm_query_scc_cmd *)desc.data;
10975
10976 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10977 if (ret)
10978 return ret;
10979
10980 *scc_version = le32_to_cpu(resp->scc_version);
10981
10982 return 0;
10983 }
10984
10985 static void hclge_set_flowctrl_adv(struct hclge_dev *hdev, u32 rx_en, u32 tx_en)
10986 {
10987 struct phy_device *phydev = hdev->hw.mac.phydev;
10988
10989 if (!phydev)
10990 return;
10991
10992 phy_set_asym_pause(phydev, rx_en, tx_en);
10993 }
10994
10995 static int hclge_cfg_pauseparam(struct hclge_dev *hdev, u32 rx_en, u32 tx_en)
10996 {
10997 int ret;
10998
10999 if (hdev->tm_info.fc_mode == HCLGE_FC_PFC)
11000 return 0;
11001
11002 ret = hclge_mac_pause_en_cfg(hdev, tx_en, rx_en);
11003 if (ret)
11004 dev_err(&hdev->pdev->dev,
11005 "configure pauseparam error, ret = %d.\n", ret);
11006
11007 return ret;
11008 }
11009
11010 int hclge_cfg_flowctrl(struct hclge_dev *hdev)
11011 {
11012 struct phy_device *phydev = hdev->hw.mac.phydev;
11013 u16 remote_advertising = 0;
11014 u16 local_advertising;
11015 u32 rx_pause, tx_pause;
11016 u8 flowctl;
11017
11018 if (!phydev->link)
11019 return 0;
11020
11021 if (!phydev->autoneg)
11022 return hclge_mac_pause_setup_hw(hdev);
11023
11024 local_advertising = linkmode_adv_to_lcl_adv_t(phydev->advertising);
11025
11026 if (phydev->pause)
11027 remote_advertising = LPA_PAUSE_CAP;
11028
11029 if (phydev->asym_pause)
11030 remote_advertising |= LPA_PAUSE_ASYM;
11031
11032 flowctl = mii_resolve_flowctrl_fdx(local_advertising,
11033 remote_advertising);
11034 tx_pause = flowctl & FLOW_CTRL_TX;
11035 rx_pause = flowctl & FLOW_CTRL_RX;
11036
11037 if (phydev->duplex == HCLGE_MAC_HALF) {
11038 tx_pause = 0;
11039 rx_pause = 0;
11040 }
11041
11042 return hclge_cfg_pauseparam(hdev, rx_pause, tx_pause);
11043 }
11044
11045 static void hclge_get_pauseparam(struct hnae3_handle *handle, u32 *auto_neg,
11046 u32 *rx_en, u32 *tx_en)
11047 {
11048 struct hclge_vport *vport = hclge_get_vport(handle);
11049 struct hclge_dev *hdev = vport->back;
11050 u8 media_type = hdev->hw.mac.media_type;
11051
11052 *auto_neg = (media_type == HNAE3_MEDIA_TYPE_COPPER) ?
11053 hclge_get_autoneg(handle) : 0;
11054
11055 if (hdev->tm_info.fc_mode == HCLGE_FC_PFC) {
11056 *rx_en = 0;
11057 *tx_en = 0;
11058 return;
11059 }
11060
11061 if (hdev->tm_info.fc_mode == HCLGE_FC_RX_PAUSE) {
11062 *rx_en = 1;
11063 *tx_en = 0;
11064 } else if (hdev->tm_info.fc_mode == HCLGE_FC_TX_PAUSE) {
11065 *tx_en = 1;
11066 *rx_en = 0;
11067 } else if (hdev->tm_info.fc_mode == HCLGE_FC_FULL) {
11068 *rx_en = 1;
11069 *tx_en = 1;
11070 } else {
11071 *rx_en = 0;
11072 *tx_en = 0;
11073 }
11074 }
11075
11076 static void hclge_record_user_pauseparam(struct hclge_dev *hdev,
11077 u32 rx_en, u32 tx_en)
11078 {
11079 if (rx_en && tx_en)
11080 hdev->fc_mode_last_time = HCLGE_FC_FULL;
11081 else if (rx_en && !tx_en)
11082 hdev->fc_mode_last_time = HCLGE_FC_RX_PAUSE;
11083 else if (!rx_en && tx_en)
11084 hdev->fc_mode_last_time = HCLGE_FC_TX_PAUSE;
11085 else
11086 hdev->fc_mode_last_time = HCLGE_FC_NONE;
11087
11088 hdev->tm_info.fc_mode = hdev->fc_mode_last_time;
11089 }
11090
11091 static int hclge_set_pauseparam(struct hnae3_handle *handle, u32 auto_neg,
11092 u32 rx_en, u32 tx_en)
11093 {
11094 struct hclge_vport *vport = hclge_get_vport(handle);
11095 struct hclge_dev *hdev = vport->back;
11096 struct phy_device *phydev = hdev->hw.mac.phydev;
11097 u32 fc_autoneg;
11098
11099 if (phydev || hnae3_dev_phy_imp_supported(hdev)) {
11100 fc_autoneg = hclge_get_autoneg(handle);
11101 if (auto_neg != fc_autoneg) {
11102 dev_info(&hdev->pdev->dev,
11103 "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
11104 return -EOPNOTSUPP;
11105 }
11106 }
11107
11108 if (hdev->tm_info.fc_mode == HCLGE_FC_PFC) {
11109 dev_info(&hdev->pdev->dev,
11110 "Priority flow control enabled. Cannot set link flow control.\n");
11111 return -EOPNOTSUPP;
11112 }
11113
11114 hclge_set_flowctrl_adv(hdev, rx_en, tx_en);
11115
11116 hclge_record_user_pauseparam(hdev, rx_en, tx_en);
11117
11118 if (!auto_neg || hnae3_dev_phy_imp_supported(hdev))
11119 return hclge_cfg_pauseparam(hdev, rx_en, tx_en);
11120
11121 if (phydev)
11122 return phy_start_aneg(phydev);
11123
11124 return -EOPNOTSUPP;
11125 }
11126
11127 static void hclge_get_ksettings_an_result(struct hnae3_handle *handle,
11128 u8 *auto_neg, u32 *speed, u8 *duplex, u32 *lane_num)
11129 {
11130 struct hclge_vport *vport = hclge_get_vport(handle);
11131 struct hclge_dev *hdev = vport->back;
11132
11133 if (speed)
11134 *speed = hdev->hw.mac.speed;
11135 if (duplex)
11136 *duplex = hdev->hw.mac.duplex;
11137 if (auto_neg)
11138 *auto_neg = hdev->hw.mac.autoneg;
11139 if (lane_num)
11140 *lane_num = hdev->hw.mac.lane_num;
11141 }
11142
11143 static void hclge_get_media_type(struct hnae3_handle *handle, u8 *media_type,
11144 u8 *module_type)
11145 {
11146 struct hclge_vport *vport = hclge_get_vport(handle);
11147 struct hclge_dev *hdev = vport->back;
11148
11149 /* When nic is down, the service task is not running, doesn't update
11150 * the port information per second. Query the port information before
11151 * return the media type, ensure getting the correct media information.
11152 */
11153 hclge_update_port_info(hdev);
11154
11155 if (media_type)
11156 *media_type = hdev->hw.mac.media_type;
11157
11158 if (module_type)
11159 *module_type = hdev->hw.mac.module_type;
11160 }
11161
11162 static void hclge_get_mdix_mode(struct hnae3_handle *handle,
11163 u8 *tp_mdix_ctrl, u8 *tp_mdix)
11164 {
11165 struct hclge_vport *vport = hclge_get_vport(handle);
11166 struct hclge_dev *hdev = vport->back;
11167 struct phy_device *phydev = hdev->hw.mac.phydev;
11168 int mdix_ctrl, mdix, is_resolved;
11169 unsigned int retval;
11170
11171 if (!phydev) {
11172 *tp_mdix_ctrl = ETH_TP_MDI_INVALID;
11173 *tp_mdix = ETH_TP_MDI_INVALID;
11174 return;
11175 }
11176
11177 phy_write(phydev, HCLGE_PHY_PAGE_REG, HCLGE_PHY_PAGE_MDIX);
11178
11179 retval = phy_read(phydev, HCLGE_PHY_CSC_REG);
11180 mdix_ctrl = hnae3_get_field(retval, HCLGE_PHY_MDIX_CTRL_M,
11181 HCLGE_PHY_MDIX_CTRL_S);
11182
11183 retval = phy_read(phydev, HCLGE_PHY_CSS_REG);
11184 mdix = hnae3_get_bit(retval, HCLGE_PHY_MDIX_STATUS_B);
11185 is_resolved = hnae3_get_bit(retval, HCLGE_PHY_SPEED_DUP_RESOLVE_B);
11186
11187 phy_write(phydev, HCLGE_PHY_PAGE_REG, HCLGE_PHY_PAGE_COPPER);
11188
11189 switch (mdix_ctrl) {
11190 case 0x0:
11191 *tp_mdix_ctrl = ETH_TP_MDI;
11192 break;
11193 case 0x1:
11194 *tp_mdix_ctrl = ETH_TP_MDI_X;
11195 break;
11196 case 0x3:
11197 *tp_mdix_ctrl = ETH_TP_MDI_AUTO;
11198 break;
11199 default:
11200 *tp_mdix_ctrl = ETH_TP_MDI_INVALID;
11201 break;
11202 }
11203
11204 if (!is_resolved)
11205 *tp_mdix = ETH_TP_MDI_INVALID;
11206 else if (mdix)
11207 *tp_mdix = ETH_TP_MDI_X;
11208 else
11209 *tp_mdix = ETH_TP_MDI;
11210 }
11211
11212 static void hclge_info_show(struct hclge_dev *hdev)
11213 {
11214 struct hnae3_handle *handle = &hdev->vport->nic;
11215 struct device *dev = &hdev->pdev->dev;
11216
11217 dev_info(dev, "PF info begin:\n");
11218
11219 dev_info(dev, "Task queue pairs numbers: %u\n", hdev->num_tqps);
11220 dev_info(dev, "Desc num per TX queue: %u\n", hdev->num_tx_desc);
11221 dev_info(dev, "Desc num per RX queue: %u\n", hdev->num_rx_desc);
11222 dev_info(dev, "Numbers of vports: %u\n", hdev->num_alloc_vport);
11223 dev_info(dev, "Numbers of VF for this PF: %u\n", hdev->num_req_vfs);
11224 dev_info(dev, "HW tc map: 0x%x\n", hdev->hw_tc_map);
11225 dev_info(dev, "Total buffer size for TX/RX: %u\n", hdev->pkt_buf_size);
11226 dev_info(dev, "TX buffer size for each TC: %u\n", hdev->tx_buf_size);
11227 dev_info(dev, "DV buffer size for each TC: %u\n", hdev->dv_buf_size);
11228 dev_info(dev, "This is %s PF\n",
11229 hdev->flag & HCLGE_FLAG_MAIN ? "main" : "not main");
11230 dev_info(dev, "DCB %s\n",
11231 str_enable_disable(handle->kinfo.tc_info.dcb_ets_active));
11232 dev_info(dev, "MQPRIO %s\n",
11233 str_enable_disable(handle->kinfo.tc_info.mqprio_active));
11234 dev_info(dev, "Default tx spare buffer size: %u\n",
11235 hdev->tx_spare_buf_size);
11236
11237 dev_info(dev, "PF info end.\n");
11238 }
11239
11240 static int hclge_init_nic_client_instance(struct hnae3_ae_dev *ae_dev,
11241 struct hclge_vport *vport)
11242 {
11243 struct hnae3_client *client = vport->nic.client;
11244 struct hclge_dev *hdev = ae_dev->priv;
11245 u32 rst_cnt = hdev->rst_stats.reset_cnt;
11246 int ret;
11247
11248 ret = client->ops->init_instance(&vport->nic);
11249 if (ret)
11250 return ret;
11251
11252 set_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
11253 if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
11254 rst_cnt != hdev->rst_stats.reset_cnt) {
11255 ret = -EBUSY;
11256 goto init_nic_err;
11257 }
11258
11259 /* Enable nic hw error interrupts */
11260 ret = hclge_config_nic_hw_error(hdev, true);
11261 if (ret) {
11262 dev_err(&ae_dev->pdev->dev,
11263 "fail(%d) to enable hw error interrupts\n", ret);
11264 goto init_nic_err;
11265 }
11266
11267 hnae3_set_client_init_flag(client, ae_dev, 1);
11268
11269 if (netif_msg_drv(&hdev->vport->nic))
11270 hclge_info_show(hdev);
11271
11272 return ret;
11273
11274 init_nic_err:
11275 clear_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
11276 while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
11277 msleep(HCLGE_WAIT_RESET_DONE);
11278
11279 client->ops->uninit_instance(&vport->nic, 0);
11280
11281 return ret;
11282 }
11283
11284 static int hclge_init_roce_client_instance(struct hnae3_ae_dev *ae_dev,
11285 struct hclge_vport *vport)
11286 {
11287 struct hclge_dev *hdev = ae_dev->priv;
11288 struct hnae3_client *client;
11289 u32 rst_cnt;
11290 int ret;
11291
11292 if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client ||
11293 !hdev->nic_client)
11294 return 0;
11295
11296 client = hdev->roce_client;
11297 ret = hclge_init_roce_base_info(vport);
11298 if (ret)
11299 return ret;
11300
11301 rst_cnt = hdev->rst_stats.reset_cnt;
11302 ret = client->ops->init_instance(&vport->roce);
11303 if (ret)
11304 return ret;
11305
11306 set_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
11307 if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
11308 rst_cnt != hdev->rst_stats.reset_cnt) {
11309 ret = -EBUSY;
11310 goto init_roce_err;
11311 }
11312
11313 /* Enable roce ras interrupts */
11314 ret = hclge_config_rocee_ras_interrupt(hdev, true);
11315 if (ret) {
11316 dev_err(&ae_dev->pdev->dev,
11317 "fail(%d) to enable roce ras interrupts\n", ret);
11318 goto init_roce_err;
11319 }
11320
11321 hnae3_set_client_init_flag(client, ae_dev, 1);
11322
11323 return 0;
11324
11325 init_roce_err:
11326 clear_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
11327 while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
11328 msleep(HCLGE_WAIT_RESET_DONE);
11329
11330 hdev->roce_client->ops->uninit_instance(&vport->roce, 0);
11331
11332 return ret;
11333 }
11334
11335 static int hclge_init_client_instance(struct hnae3_client *client,
11336 struct hnae3_ae_dev *ae_dev)
11337 {
11338 struct hclge_dev *hdev = ae_dev->priv;
11339 struct hclge_vport *vport = &hdev->vport[0];
11340 int ret;
11341
11342 switch (client->type) {
11343 case HNAE3_CLIENT_KNIC:
11344 hdev->nic_client = client;
11345 vport->nic.client = client;
11346 ret = hclge_init_nic_client_instance(ae_dev, vport);
11347 if (ret)
11348 goto clear_nic;
11349
11350 ret = hclge_init_roce_client_instance(ae_dev, vport);
11351 if (ret)
11352 goto clear_roce;
11353
11354 break;
11355 case HNAE3_CLIENT_ROCE:
11356 if (hnae3_dev_roce_supported(hdev)) {
11357 hdev->roce_client = client;
11358 vport->roce.client = client;
11359 }
11360
11361 ret = hclge_init_roce_client_instance(ae_dev, vport);
11362 if (ret)
11363 goto clear_roce;
11364
11365 break;
11366 default:
11367 return -EINVAL;
11368 }
11369
11370 return 0;
11371
11372 clear_nic:
11373 hdev->nic_client = NULL;
11374 vport->nic.client = NULL;
11375 return ret;
11376 clear_roce:
11377 hdev->roce_client = NULL;
11378 vport->roce.client = NULL;
11379 return ret;
11380 }
11381
11382 static bool hclge_uninit_need_wait(struct hclge_dev *hdev)
11383 {
11384 return test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
11385 test_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
11386 }
11387
11388 static void hclge_uninit_client_instance(struct hnae3_client *client,
11389 struct hnae3_ae_dev *ae_dev)
11390 {
11391 struct hclge_dev *hdev = ae_dev->priv;
11392 struct hclge_vport *vport = &hdev->vport[0];
11393
11394 if (hdev->roce_client) {
11395 clear_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
11396 while (hclge_uninit_need_wait(hdev))
11397 msleep(HCLGE_WAIT_RESET_DONE);
11398
11399 hdev->roce_client->ops->uninit_instance(&vport->roce, 0);
11400 hdev->roce_client = NULL;
11401 vport->roce.client = NULL;
11402 }
11403 if (client->type == HNAE3_CLIENT_ROCE)
11404 return;
11405 if (hdev->nic_client && client->ops->uninit_instance) {
11406 clear_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
11407 while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
11408 msleep(HCLGE_WAIT_RESET_DONE);
11409
11410 client->ops->uninit_instance(&vport->nic, 0);
11411 hdev->nic_client = NULL;
11412 vport->nic.client = NULL;
11413 }
11414 }
11415
11416 static int hclge_dev_mem_map(struct hclge_dev *hdev)
11417 {
11418 struct pci_dev *pdev = hdev->pdev;
11419 struct hclge_hw *hw = &hdev->hw;
11420
11421 /* for device does not have device memory, return directly */
11422 if (!(pci_select_bars(pdev, IORESOURCE_MEM) & BIT(HCLGE_MEM_BAR)))
11423 return 0;
11424
11425 hw->hw.mem_base =
11426 devm_ioremap_wc(&pdev->dev,
11427 pci_resource_start(pdev, HCLGE_MEM_BAR),
11428 pci_resource_len(pdev, HCLGE_MEM_BAR));
11429 if (!hw->hw.mem_base) {
11430 dev_err(&pdev->dev, "failed to map device memory\n");
11431 return -EFAULT;
11432 }
11433
11434 return 0;
11435 }
11436
11437 static int hclge_pci_init(struct hclge_dev *hdev)
11438 {
11439 struct pci_dev *pdev = hdev->pdev;
11440 struct hclge_hw *hw;
11441 int ret;
11442
11443 ret = pci_enable_device(pdev);
11444 if (ret) {
11445 dev_err(&pdev->dev, "failed to enable PCI device\n");
11446 return ret;
11447 }
11448
11449 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
11450 if (ret) {
11451 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11452 if (ret) {
11453 dev_err(&pdev->dev,
11454 "can't set consistent PCI DMA\n");
11455 goto err_disable_device;
11456 }
11457 dev_warn(&pdev->dev, "set DMA mask to 32 bits\n");
11458 }
11459
11460 ret = pci_request_regions(pdev, HCLGE_DRIVER_NAME);
11461 if (ret) {
11462 dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
11463 goto err_disable_device;
11464 }
11465
11466 pci_set_master(pdev);
11467 hw = &hdev->hw;
11468 hw->hw.io_base = pcim_iomap(pdev, 2, 0);
11469 if (!hw->hw.io_base) {
11470 dev_err(&pdev->dev, "Can't map configuration register space\n");
11471 ret = -ENOMEM;
11472 goto err_release_regions;
11473 }
11474
11475 ret = hclge_dev_mem_map(hdev);
11476 if (ret)
11477 goto err_unmap_io_base;
11478
11479 hdev->num_req_vfs = pci_sriov_get_totalvfs(pdev);
11480
11481 return 0;
11482
11483 err_unmap_io_base:
11484 pcim_iounmap(pdev, hdev->hw.hw.io_base);
11485 err_release_regions:
11486 pci_release_regions(pdev);
11487 err_disable_device:
11488 pci_disable_device(pdev);
11489
11490 return ret;
11491 }
11492
11493 static void hclge_pci_uninit(struct hclge_dev *hdev)
11494 {
11495 struct pci_dev *pdev = hdev->pdev;
11496
11497 if (hdev->hw.hw.mem_base)
11498 devm_iounmap(&pdev->dev, hdev->hw.hw.mem_base);
11499
11500 pcim_iounmap(pdev, hdev->hw.hw.io_base);
11501 pci_free_irq_vectors(pdev);
11502 pci_release_regions(pdev);
11503 pci_disable_device(pdev);
11504 }
11505
11506 static void hclge_state_init(struct hclge_dev *hdev)
11507 {
11508 set_bit(HCLGE_STATE_SERVICE_INITED, &hdev->state);
11509 set_bit(HCLGE_STATE_DOWN, &hdev->state);
11510 clear_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state);
11511 clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
11512 clear_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
11513 clear_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state);
11514 clear_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state);
11515 }
11516
11517 static void hclge_state_uninit(struct hclge_dev *hdev)
11518 {
11519 set_bit(HCLGE_STATE_DOWN, &hdev->state);
11520 set_bit(HCLGE_STATE_REMOVING, &hdev->state);
11521
11522 if (hdev->reset_timer.function)
11523 timer_delete_sync(&hdev->reset_timer);
11524 if (hdev->service_task.work.func)
11525 cancel_delayed_work_sync(&hdev->service_task);
11526 }
11527
11528 static void hclge_reset_prepare_general(struct hnae3_ae_dev *ae_dev,
11529 enum hnae3_reset_type rst_type)
11530 {
11531 #define HCLGE_RESET_RETRY_WAIT_MS 500
11532 #define HCLGE_RESET_RETRY_CNT 5
11533
11534 struct hclge_dev *hdev = ae_dev->priv;
11535 int retry_cnt = 0;
11536 int ret;
11537
11538 while (retry_cnt++ < HCLGE_RESET_RETRY_CNT) {
11539 down(&hdev->reset_sem);
11540 set_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
11541 hdev->reset_type = rst_type;
11542 ret = hclge_reset_prepare(hdev);
11543 if (!ret && !hdev->reset_pending)
11544 break;
11545
11546 dev_err(&hdev->pdev->dev,
11547 "failed to prepare to reset, ret=%d, reset_pending:0x%lx, retry_cnt:%d\n",
11548 ret, hdev->reset_pending, retry_cnt);
11549 clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
11550 up(&hdev->reset_sem);
11551 msleep(HCLGE_RESET_RETRY_WAIT_MS);
11552 }
11553
11554 /* disable misc vector before reset done */
11555 hclge_enable_vector(&hdev->misc_vector, false);
11556 set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
11557
11558 if (hdev->reset_type == HNAE3_FLR_RESET)
11559 hdev->rst_stats.flr_rst_cnt++;
11560 }
11561
11562 static void hclge_reset_done(struct hnae3_ae_dev *ae_dev)
11563 {
11564 struct hclge_dev *hdev = ae_dev->priv;
11565 int ret;
11566
11567 hclge_enable_vector(&hdev->misc_vector, true);
11568
11569 ret = hclge_reset_rebuild(hdev);
11570 if (ret)
11571 dev_err(&hdev->pdev->dev, "fail to rebuild, ret=%d\n", ret);
11572
11573 hdev->reset_type = HNAE3_NONE_RESET;
11574 if (test_and_clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
11575 up(&hdev->reset_sem);
11576 }
11577
11578 static void hclge_clear_resetting_state(struct hclge_dev *hdev)
11579 {
11580 u16 i;
11581
11582 for (i = 0; i < hdev->num_alloc_vport; i++) {
11583 struct hclge_vport *vport = &hdev->vport[i];
11584 int ret;
11585
11586 /* Send cmd to clear vport's FUNC_RST_ING */
11587 ret = hclge_set_vf_rst(hdev, vport->vport_id, false);
11588 if (ret)
11589 dev_warn(&hdev->pdev->dev,
11590 "clear vport(%u) rst failed %d!\n",
11591 vport->vport_id, ret);
11592 }
11593 }
11594
11595 static int hclge_clear_hw_resource(struct hclge_dev *hdev)
11596 {
11597 struct hclge_desc desc;
11598 int ret;
11599
11600 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CLEAR_HW_RESOURCE, false);
11601
11602 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
11603 /* This new command is only supported by new firmware, it will
11604 * fail with older firmware. Error value -EOPNOSUPP can only be
11605 * returned by older firmware running this command, to keep code
11606 * backward compatible we will override this value and return
11607 * success.
11608 */
11609 if (ret && ret != -EOPNOTSUPP) {
11610 dev_err(&hdev->pdev->dev,
11611 "failed to clear hw resource, ret = %d\n", ret);
11612 return ret;
11613 }
11614 return 0;
11615 }
11616
11617 static void hclge_init_rxd_adv_layout(struct hclge_dev *hdev)
11618 {
11619 if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
11620 hclge_write_dev(&hdev->hw, HCLGE_RXD_ADV_LAYOUT_EN_REG, 1);
11621 }
11622
11623 static void hclge_uninit_rxd_adv_layout(struct hclge_dev *hdev)
11624 {
11625 if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
11626 hclge_write_dev(&hdev->hw, HCLGE_RXD_ADV_LAYOUT_EN_REG, 0);
11627 }
11628
11629 static struct hclge_wol_info *hclge_get_wol_info(struct hnae3_handle *handle)
11630 {
11631 struct hclge_vport *vport = hclge_get_vport(handle);
11632
11633 return &vport->back->hw.mac.wol;
11634 }
11635
11636 static int hclge_get_wol_supported_mode(struct hclge_dev *hdev,
11637 u32 *wol_supported)
11638 {
11639 struct hclge_query_wol_supported_cmd *wol_supported_cmd;
11640 struct hclge_desc desc;
11641 int ret;
11642
11643 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_WOL_GET_SUPPORTED_MODE,
11644 true);
11645 wol_supported_cmd = (struct hclge_query_wol_supported_cmd *)desc.data;
11646
11647 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
11648 if (ret) {
11649 dev_err(&hdev->pdev->dev,
11650 "failed to query wol supported, ret = %d\n", ret);
11651 return ret;
11652 }
11653
11654 *wol_supported = le32_to_cpu(wol_supported_cmd->supported_wake_mode);
11655
11656 return 0;
11657 }
11658
11659 static int hclge_set_wol_cfg(struct hclge_dev *hdev,
11660 struct hclge_wol_info *wol_info)
11661 {
11662 struct hclge_wol_cfg_cmd *wol_cfg_cmd;
11663 struct hclge_desc desc;
11664 int ret;
11665
11666 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_WOL_CFG, false);
11667 wol_cfg_cmd = (struct hclge_wol_cfg_cmd *)desc.data;
11668 wol_cfg_cmd->wake_on_lan_mode = cpu_to_le32(wol_info->wol_current_mode);
11669 wol_cfg_cmd->sopass_size = wol_info->wol_sopass_size;
11670 memcpy(wol_cfg_cmd->sopass, wol_info->wol_sopass, SOPASS_MAX);
11671
11672 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
11673 if (ret)
11674 dev_err(&hdev->pdev->dev,
11675 "failed to set wol config, ret = %d\n", ret);
11676
11677 return ret;
11678 }
11679
11680 static int hclge_update_wol(struct hclge_dev *hdev)
11681 {
11682 struct hclge_wol_info *wol_info = &hdev->hw.mac.wol;
11683
11684 if (!hnae3_ae_dev_wol_supported(hdev->ae_dev))
11685 return 0;
11686
11687 return hclge_set_wol_cfg(hdev, wol_info);
11688 }
11689
11690 static int hclge_init_wol(struct hclge_dev *hdev)
11691 {
11692 struct hclge_wol_info *wol_info = &hdev->hw.mac.wol;
11693 int ret;
11694
11695 if (!hnae3_ae_dev_wol_supported(hdev->ae_dev))
11696 return 0;
11697
11698 memset(wol_info, 0, sizeof(struct hclge_wol_info));
11699 ret = hclge_get_wol_supported_mode(hdev,
11700 &wol_info->wol_support_mode);
11701 if (ret) {
11702 wol_info->wol_support_mode = 0;
11703 return ret;
11704 }
11705
11706 return hclge_update_wol(hdev);
11707 }
11708
11709 static void hclge_get_wol(struct hnae3_handle *handle,
11710 struct ethtool_wolinfo *wol)
11711 {
11712 struct hclge_wol_info *wol_info = hclge_get_wol_info(handle);
11713
11714 wol->supported = wol_info->wol_support_mode;
11715 wol->wolopts = wol_info->wol_current_mode;
11716 if (wol_info->wol_current_mode & WAKE_MAGICSECURE)
11717 memcpy(wol->sopass, wol_info->wol_sopass, SOPASS_MAX);
11718 }
11719
11720 static int hclge_set_wol(struct hnae3_handle *handle,
11721 struct ethtool_wolinfo *wol)
11722 {
11723 struct hclge_wol_info *wol_info = hclge_get_wol_info(handle);
11724 struct hclge_vport *vport = hclge_get_vport(handle);
11725 u32 wol_mode;
11726 int ret;
11727
11728 wol_mode = wol->wolopts;
11729 if (wol_mode & ~wol_info->wol_support_mode)
11730 return -EINVAL;
11731
11732 wol_info->wol_current_mode = wol_mode;
11733 if (wol_mode & WAKE_MAGICSECURE) {
11734 memcpy(wol_info->wol_sopass, wol->sopass, SOPASS_MAX);
11735 wol_info->wol_sopass_size = SOPASS_MAX;
11736 } else {
11737 wol_info->wol_sopass_size = 0;
11738 }
11739
11740 ret = hclge_set_wol_cfg(vport->back, wol_info);
11741 if (ret)
11742 wol_info->wol_current_mode = 0;
11743
11744 return ret;
11745 }
11746
11747 static int hclge_set_autoneg_speed_dup(struct hclge_dev *hdev)
11748 {
11749 int ret;
11750
11751 if (hdev->hw.mac.support_autoneg) {
11752 ret = hclge_set_autoneg_en(hdev, hdev->hw.mac.req_autoneg);
11753 if (ret)
11754 return ret;
11755 }
11756
11757 if (!hdev->hw.mac.req_autoneg) {
11758 ret = hclge_cfg_mac_speed_dup_hw(hdev, hdev->hw.mac.req_speed,
11759 hdev->hw.mac.req_duplex,
11760 hdev->hw.mac.req_lane_num);
11761 if (ret)
11762 return ret;
11763 }
11764
11765 return 0;
11766 }
11767
11768 static int hclge_init_ae_dev(struct hnae3_ae_dev *ae_dev)
11769 {
11770 struct pci_dev *pdev = ae_dev->pdev;
11771 struct hclge_dev *hdev;
11772 int ret;
11773
11774 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
11775 if (!hdev)
11776 return -ENOMEM;
11777
11778 hdev->pdev = pdev;
11779 hdev->ae_dev = ae_dev;
11780 hdev->reset_type = HNAE3_NONE_RESET;
11781 hdev->reset_level = HNAE3_FUNC_RESET;
11782 ae_dev->priv = hdev;
11783
11784 /* HW supprt 2 layer vlan */
11785 hdev->mps = ETH_FRAME_LEN + ETH_FCS_LEN + 2 * VLAN_HLEN;
11786
11787 mutex_init(&hdev->vport_lock);
11788 spin_lock_init(&hdev->fd_rule_lock);
11789 sema_init(&hdev->reset_sem, 1);
11790
11791 ret = hclge_pci_init(hdev);
11792 if (ret)
11793 goto out;
11794
11795 /* Firmware command queue initialize */
11796 ret = hclge_comm_cmd_queue_init(hdev->pdev, &hdev->hw.hw);
11797 if (ret)
11798 goto err_pci_uninit;
11799
11800 /* Firmware command initialize */
11801 hclge_comm_cmd_init_ops(&hdev->hw.hw, &hclge_cmq_ops);
11802 ret = hclge_comm_cmd_init(hdev->ae_dev, &hdev->hw.hw, &hdev->fw_version,
11803 true, hdev->reset_pending);
11804 if (ret)
11805 goto err_cmd_uninit;
11806
11807 ret = hclge_clear_hw_resource(hdev);
11808 if (ret)
11809 goto err_cmd_uninit;
11810
11811 ret = hclge_get_cap(hdev);
11812 if (ret)
11813 goto err_cmd_uninit;
11814
11815 ret = hclge_query_dev_specs(hdev);
11816 if (ret) {
11817 dev_err(&pdev->dev, "failed to query dev specifications, ret = %d.\n",
11818 ret);
11819 goto err_cmd_uninit;
11820 }
11821
11822 ret = hclge_configure(hdev);
11823 if (ret) {
11824 dev_err(&pdev->dev, "Configure dev error, ret = %d.\n", ret);
11825 goto err_cmd_uninit;
11826 }
11827
11828 ret = hclge_init_msi(hdev);
11829 if (ret) {
11830 dev_err(&pdev->dev, "Init MSI/MSI-X error, ret = %d.\n", ret);
11831 goto err_cmd_uninit;
11832 }
11833
11834 ret = hclge_misc_irq_init(hdev);
11835 if (ret)
11836 goto err_msi_uninit;
11837
11838 ret = hclge_alloc_tqps(hdev);
11839 if (ret) {
11840 dev_err(&pdev->dev, "Allocate TQPs error, ret = %d.\n", ret);
11841 goto err_msi_irq_uninit;
11842 }
11843
11844 ret = hclge_alloc_vport(hdev);
11845 if (ret)
11846 goto err_msi_irq_uninit;
11847
11848 ret = hclge_map_tqp(hdev);
11849 if (ret)
11850 goto err_msi_irq_uninit;
11851
11852 if (hdev->hw.mac.media_type == HNAE3_MEDIA_TYPE_COPPER) {
11853 clear_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps);
11854 if (hnae3_dev_phy_imp_supported(hdev))
11855 ret = hclge_update_tp_port_info(hdev);
11856 else
11857 ret = hclge_mac_mdio_config(hdev);
11858
11859 if (ret)
11860 goto err_msi_irq_uninit;
11861 }
11862
11863 ret = hclge_init_umv_space(hdev);
11864 if (ret)
11865 goto err_mdiobus_unreg;
11866
11867 ret = hclge_mac_init(hdev);
11868 if (ret) {
11869 dev_err(&pdev->dev, "Mac init error, ret = %d\n", ret);
11870 goto err_mdiobus_unreg;
11871 }
11872
11873 ret = hclge_config_tso(hdev, HCLGE_TSO_MSS_MIN, HCLGE_TSO_MSS_MAX);
11874 if (ret) {
11875 dev_err(&pdev->dev, "Enable tso fail, ret =%d\n", ret);
11876 goto err_mdiobus_unreg;
11877 }
11878
11879 ret = hclge_config_gro(hdev);
11880 if (ret)
11881 goto err_mdiobus_unreg;
11882
11883 ret = hclge_init_vlan_config(hdev);
11884 if (ret) {
11885 dev_err(&pdev->dev, "VLAN init fail, ret =%d\n", ret);
11886 goto err_mdiobus_unreg;
11887 }
11888
11889 ret = hclge_tm_schd_init(hdev);
11890 if (ret) {
11891 dev_err(&pdev->dev, "tm schd init fail, ret =%d\n", ret);
11892 goto err_mdiobus_unreg;
11893 }
11894
11895 ret = hclge_comm_rss_init_cfg(&hdev->vport->nic, hdev->ae_dev,
11896 &hdev->rss_cfg);
11897 if (ret) {
11898 dev_err(&pdev->dev, "failed to init rss cfg, ret = %d\n", ret);
11899 goto err_mdiobus_unreg;
11900 }
11901
11902 ret = hclge_rss_init_hw(hdev);
11903 if (ret) {
11904 dev_err(&pdev->dev, "Rss init fail, ret =%d\n", ret);
11905 goto err_mdiobus_unreg;
11906 }
11907
11908 ret = init_mgr_tbl(hdev);
11909 if (ret) {
11910 dev_err(&pdev->dev, "manager table init fail, ret =%d\n", ret);
11911 goto err_mdiobus_unreg;
11912 }
11913
11914 ret = hclge_init_fd_config(hdev);
11915 if (ret) {
11916 dev_err(&pdev->dev,
11917 "fd table init fail, ret=%d\n", ret);
11918 goto err_mdiobus_unreg;
11919 }
11920
11921 ret = hclge_ptp_init(hdev);
11922 if (ret)
11923 goto err_mdiobus_unreg;
11924
11925 ret = hclge_update_port_info(hdev);
11926 if (ret)
11927 goto err_ptp_uninit;
11928
11929 if (hdev->hw.mac.media_type != HNAE3_MEDIA_TYPE_COPPER)
11930 hdev->hw.mac.req_autoneg = hdev->hw.mac.autoneg;
11931
11932 ret = hclge_set_autoneg_speed_dup(hdev);
11933 if (ret) {
11934 dev_err(&pdev->dev,
11935 "failed to set autoneg speed duplex, ret = %d\n", ret);
11936 goto err_ptp_uninit;
11937 }
11938
11939 INIT_KFIFO(hdev->mac_tnl_log);
11940
11941 hclge_dcb_ops_set(hdev);
11942
11943 timer_setup(&hdev->reset_timer, hclge_reset_timer, 0);
11944 INIT_DELAYED_WORK(&hdev->service_task, hclge_service_task);
11945
11946 hclge_clear_all_event_cause(hdev);
11947 hclge_clear_resetting_state(hdev);
11948
11949 /* Log and clear the hw errors those already occurred */
11950 if (hnae3_dev_ras_imp_supported(hdev))
11951 hclge_handle_occurred_error(hdev);
11952 else
11953 hclge_handle_all_hns_hw_errors(ae_dev);
11954
11955 /* request delayed reset for the error recovery because an immediate
11956 * global reset on a PF affecting pending initialization of other PFs
11957 */
11958 if (ae_dev->hw_err_reset_req) {
11959 enum hnae3_reset_type reset_level;
11960
11961 reset_level = hclge_get_reset_level(ae_dev,
11962 &ae_dev->hw_err_reset_req);
11963 hclge_set_def_reset_request(ae_dev, reset_level);
11964 mod_timer(&hdev->reset_timer, jiffies + HCLGE_RESET_INTERVAL);
11965 }
11966
11967 hclge_init_rxd_adv_layout(hdev);
11968
11969 ret = hclge_init_wol(hdev);
11970 if (ret)
11971 dev_warn(&pdev->dev,
11972 "failed to wake on lan init, ret = %d\n", ret);
11973
11974 ret = hclge_devlink_init(hdev);
11975 if (ret)
11976 goto err_ptp_uninit;
11977
11978 hclge_state_init(hdev);
11979 hdev->last_reset_time = jiffies;
11980
11981 /* Enable MISC vector(vector0) */
11982 enable_irq(hdev->misc_vector.vector_irq);
11983 hclge_enable_vector(&hdev->misc_vector, true);
11984
11985 dev_info(&hdev->pdev->dev, "%s driver initialization finished.\n",
11986 HCLGE_DRIVER_NAME);
11987
11988 hclge_task_schedule(hdev, round_jiffies_relative(HZ));
11989 return 0;
11990
11991 err_ptp_uninit:
11992 hclge_ptp_uninit(hdev);
11993 err_mdiobus_unreg:
11994 if (hdev->hw.mac.phydev)
11995 mdiobus_unregister(hdev->hw.mac.mdio_bus);
11996 err_msi_irq_uninit:
11997 hclge_misc_irq_uninit(hdev);
11998 err_msi_uninit:
11999 pci_free_irq_vectors(pdev);
12000 err_cmd_uninit:
12001 hclge_comm_cmd_uninit(hdev->ae_dev, &hdev->hw.hw);
12002 err_pci_uninit:
12003 pcim_iounmap(pdev, hdev->hw.hw.io_base);
12004 pci_release_regions(pdev);
12005 pci_disable_device(pdev);
12006 out:
12007 mutex_destroy(&hdev->vport_lock);
12008 return ret;
12009 }
12010
12011 static void hclge_stats_clear(struct hclge_dev *hdev)
12012 {
12013 memset(&hdev->mac_stats, 0, sizeof(hdev->mac_stats));
12014 memset(&hdev->fec_stats, 0, sizeof(hdev->fec_stats));
12015 }
12016
12017 static int hclge_set_mac_spoofchk(struct hclge_dev *hdev, int vf, bool enable)
12018 {
12019 return hclge_config_switch_param(hdev, vf, enable,
12020 HCLGE_SWITCH_ANTI_SPOOF_MASK);
12021 }
12022
12023 static int hclge_set_vlan_spoofchk(struct hclge_dev *hdev, int vf, bool enable)
12024 {
12025 return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
12026 HCLGE_FILTER_FE_NIC_INGRESS_B,
12027 enable, vf);
12028 }
12029
12030 static int hclge_set_vf_spoofchk_hw(struct hclge_dev *hdev, int vf, bool enable)
12031 {
12032 int ret;
12033
12034 ret = hclge_set_mac_spoofchk(hdev, vf, enable);
12035 if (ret) {
12036 dev_err(&hdev->pdev->dev,
12037 "Set vf %d mac spoof check %s failed, ret=%d\n",
12038 vf, str_on_off(enable), ret);
12039 return ret;
12040 }
12041
12042 ret = hclge_set_vlan_spoofchk(hdev, vf, enable);
12043 if (ret)
12044 dev_err(&hdev->pdev->dev,
12045 "Set vf %d vlan spoof check %s failed, ret=%d\n",
12046 vf, str_on_off(enable), ret);
12047
12048 return ret;
12049 }
12050
12051 static int hclge_set_vf_spoofchk(struct hnae3_handle *handle, int vf,
12052 bool enable)
12053 {
12054 struct hclge_vport *vport = hclge_get_vport(handle);
12055 struct hclge_dev *hdev = vport->back;
12056 u32 new_spoofchk = enable ? 1 : 0;
12057 int ret;
12058
12059 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
12060 return -EOPNOTSUPP;
12061
12062 vport = hclge_get_vf_vport(hdev, vf);
12063 if (!vport)
12064 return -EINVAL;
12065
12066 if (vport->vf_info.spoofchk == new_spoofchk)
12067 return 0;
12068
12069 if (enable && test_bit(vport->vport_id, hdev->vf_vlan_full))
12070 dev_warn(&hdev->pdev->dev,
12071 "vf %d vlan table is full, enable spoof check may cause its packet send fail\n",
12072 vf);
12073 else if (enable && hclge_is_umv_space_full(vport, true))
12074 dev_warn(&hdev->pdev->dev,
12075 "vf %d mac table is full, enable spoof check may cause its packet send fail\n",
12076 vf);
12077
12078 ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id, enable);
12079 if (ret)
12080 return ret;
12081
12082 vport->vf_info.spoofchk = new_spoofchk;
12083 return 0;
12084 }
12085
12086 static int hclge_reset_vport_spoofchk(struct hclge_dev *hdev)
12087 {
12088 struct hclge_vport *vport = hdev->vport;
12089 int ret;
12090 int i;
12091
12092 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
12093 return 0;
12094
12095 /* resume the vf spoof check state after reset */
12096 for (i = 0; i < hdev->num_alloc_vport; i++) {
12097 ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id,
12098 vport->vf_info.spoofchk);
12099 if (ret)
12100 return ret;
12101
12102 vport++;
12103 }
12104
12105 return 0;
12106 }
12107
12108 static int hclge_set_vf_trust(struct hnae3_handle *handle, int vf, bool enable)
12109 {
12110 struct hclge_vport *vport = hclge_get_vport(handle);
12111 struct hclge_dev *hdev = vport->back;
12112 u32 new_trusted = enable ? 1 : 0;
12113
12114 vport = hclge_get_vf_vport(hdev, vf);
12115 if (!vport)
12116 return -EINVAL;
12117
12118 if (vport->vf_info.trusted == new_trusted)
12119 return 0;
12120
12121 vport->vf_info.trusted = new_trusted;
12122 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
12123 hclge_task_schedule(hdev, 0);
12124
12125 return 0;
12126 }
12127
12128 static void hclge_reset_vf_rate(struct hclge_dev *hdev)
12129 {
12130 int ret;
12131 int vf;
12132
12133 /* reset vf rate to default value */
12134 for (vf = HCLGE_VF_VPORT_START_NUM; vf < hdev->num_alloc_vport; vf++) {
12135 struct hclge_vport *vport = &hdev->vport[vf];
12136
12137 vport->vf_info.max_tx_rate = 0;
12138 ret = hclge_tm_qs_shaper_cfg(vport, vport->vf_info.max_tx_rate);
12139 if (ret)
12140 dev_err(&hdev->pdev->dev,
12141 "vf%d failed to reset to default, ret=%d\n",
12142 vf - HCLGE_VF_VPORT_START_NUM, ret);
12143 }
12144 }
12145
12146 static int hclge_vf_rate_param_check(struct hclge_dev *hdev,
12147 int min_tx_rate, int max_tx_rate)
12148 {
12149 if (min_tx_rate != 0 ||
12150 max_tx_rate < 0 || (u32)max_tx_rate > hdev->hw.mac.max_speed) {
12151 dev_err(&hdev->pdev->dev,
12152 "min_tx_rate:%d [0], max_tx_rate:%d [0, %u]\n",
12153 min_tx_rate, max_tx_rate, hdev->hw.mac.max_speed);
12154 return -EINVAL;
12155 }
12156
12157 return 0;
12158 }
12159
12160 static int hclge_set_vf_rate(struct hnae3_handle *handle, int vf,
12161 int min_tx_rate, int max_tx_rate, bool force)
12162 {
12163 struct hclge_vport *vport = hclge_get_vport(handle);
12164 struct hclge_dev *hdev = vport->back;
12165 int ret;
12166
12167 ret = hclge_vf_rate_param_check(hdev, min_tx_rate, max_tx_rate);
12168 if (ret)
12169 return ret;
12170
12171 vport = hclge_get_vf_vport(hdev, vf);
12172 if (!vport)
12173 return -EINVAL;
12174
12175 if (!force && (u32)max_tx_rate == vport->vf_info.max_tx_rate)
12176 return 0;
12177
12178 ret = hclge_tm_qs_shaper_cfg(vport, max_tx_rate);
12179 if (ret)
12180 return ret;
12181
12182 vport->vf_info.max_tx_rate = max_tx_rate;
12183
12184 return 0;
12185 }
12186
12187 static int hclge_resume_vf_rate(struct hclge_dev *hdev)
12188 {
12189 struct hnae3_handle *handle = &hdev->vport->nic;
12190 struct hclge_vport *vport;
12191 int ret;
12192 int vf;
12193
12194 /* resume the vf max_tx_rate after reset */
12195 for (vf = 0; vf < pci_num_vf(hdev->pdev); vf++) {
12196 vport = hclge_get_vf_vport(hdev, vf);
12197 if (!vport)
12198 return -EINVAL;
12199
12200 /* zero means max rate, after reset, firmware already set it to
12201 * max rate, so just continue.
12202 */
12203 if (!vport->vf_info.max_tx_rate)
12204 continue;
12205
12206 ret = hclge_set_vf_rate(handle, vf, 0,
12207 vport->vf_info.max_tx_rate, true);
12208 if (ret) {
12209 dev_err(&hdev->pdev->dev,
12210 "vf%d failed to resume tx_rate:%u, ret=%d\n",
12211 vf, vport->vf_info.max_tx_rate, ret);
12212 return ret;
12213 }
12214 }
12215
12216 return 0;
12217 }
12218
12219 static void hclge_reset_vport_state(struct hclge_dev *hdev)
12220 {
12221 struct hclge_vport *vport = hdev->vport;
12222 int i;
12223
12224 for (i = 0; i < hdev->num_alloc_vport; i++) {
12225 clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
12226 vport++;
12227 }
12228 }
12229
12230 static int hclge_reset_ae_dev(struct hnae3_ae_dev *ae_dev)
12231 {
12232 struct hclge_dev *hdev = ae_dev->priv;
12233 struct pci_dev *pdev = ae_dev->pdev;
12234 int ret;
12235
12236 set_bit(HCLGE_STATE_DOWN, &hdev->state);
12237
12238 hclge_stats_clear(hdev);
12239 /* NOTE: pf reset needn't to clear or restore pf and vf table entry.
12240 * so here should not clean table in memory.
12241 */
12242 if (hdev->reset_type == HNAE3_IMP_RESET ||
12243 hdev->reset_type == HNAE3_GLOBAL_RESET) {
12244 memset(hdev->vlan_table, 0, sizeof(hdev->vlan_table));
12245 memset(hdev->vf_vlan_full, 0, sizeof(hdev->vf_vlan_full));
12246 bitmap_set(hdev->vport_config_block, 0, hdev->num_alloc_vport);
12247 hclge_reset_umv_space(hdev);
12248 }
12249
12250 ret = hclge_comm_cmd_init(hdev->ae_dev, &hdev->hw.hw, &hdev->fw_version,
12251 true, hdev->reset_pending);
12252 if (ret) {
12253 dev_err(&pdev->dev, "Cmd queue init failed\n");
12254 return ret;
12255 }
12256
12257 ret = hclge_map_tqp(hdev);
12258 if (ret) {
12259 dev_err(&pdev->dev, "Map tqp error, ret = %d.\n", ret);
12260 return ret;
12261 }
12262
12263 ret = hclge_mac_init(hdev);
12264 if (ret) {
12265 dev_err(&pdev->dev, "Mac init error, ret = %d\n", ret);
12266 return ret;
12267 }
12268
12269 ret = hclge_set_autoneg_speed_dup(hdev);
12270 if (ret) {
12271 dev_err(&pdev->dev,
12272 "failed to set autoneg speed duplex, ret = %d\n", ret);
12273 return ret;
12274 }
12275
12276 ret = hclge_tp_port_init(hdev);
12277 if (ret) {
12278 dev_err(&pdev->dev, "failed to init tp port, ret = %d\n",
12279 ret);
12280 return ret;
12281 }
12282
12283 ret = hclge_config_tso(hdev, HCLGE_TSO_MSS_MIN, HCLGE_TSO_MSS_MAX);
12284 if (ret) {
12285 dev_err(&pdev->dev, "Enable tso fail, ret =%d\n", ret);
12286 return ret;
12287 }
12288
12289 ret = hclge_config_gro(hdev);
12290 if (ret)
12291 return ret;
12292
12293 ret = hclge_init_vlan_config(hdev);
12294 if (ret) {
12295 dev_err(&pdev->dev, "VLAN init fail, ret =%d\n", ret);
12296 return ret;
12297 }
12298
12299 hclge_reset_tc_config(hdev);
12300
12301 ret = hclge_tm_init_hw(hdev, true);
12302 if (ret) {
12303 dev_err(&pdev->dev, "tm init hw fail, ret =%d\n", ret);
12304 return ret;
12305 }
12306
12307 ret = hclge_rss_init_hw(hdev);
12308 if (ret) {
12309 dev_err(&pdev->dev, "Rss init fail, ret =%d\n", ret);
12310 return ret;
12311 }
12312
12313 ret = init_mgr_tbl(hdev);
12314 if (ret) {
12315 dev_err(&pdev->dev,
12316 "failed to reinit manager table, ret = %d\n", ret);
12317 return ret;
12318 }
12319
12320 ret = hclge_init_fd_config(hdev);
12321 if (ret) {
12322 dev_err(&pdev->dev, "fd table init fail, ret=%d\n", ret);
12323 return ret;
12324 }
12325
12326 ret = hclge_ptp_init(hdev);
12327 if (ret)
12328 return ret;
12329
12330 /* Log and clear the hw errors those already occurred */
12331 if (hnae3_dev_ras_imp_supported(hdev))
12332 hclge_handle_occurred_error(hdev);
12333 else
12334 hclge_handle_all_hns_hw_errors(ae_dev);
12335
12336 /* Re-enable the hw error interrupts because
12337 * the interrupts get disabled on global reset.
12338 */
12339 ret = hclge_config_nic_hw_error(hdev, true);
12340 if (ret) {
12341 dev_err(&pdev->dev,
12342 "fail(%d) to re-enable NIC hw error interrupts\n",
12343 ret);
12344 return ret;
12345 }
12346
12347 if (hdev->roce_client) {
12348 ret = hclge_config_rocee_ras_interrupt(hdev, true);
12349 if (ret) {
12350 dev_err(&pdev->dev,
12351 "fail(%d) to re-enable roce ras interrupts\n",
12352 ret);
12353 return ret;
12354 }
12355 }
12356
12357 hclge_reset_vport_state(hdev);
12358 ret = hclge_reset_vport_spoofchk(hdev);
12359 if (ret)
12360 return ret;
12361
12362 ret = hclge_resume_vf_rate(hdev);
12363 if (ret)
12364 return ret;
12365
12366 hclge_init_rxd_adv_layout(hdev);
12367
12368 ret = hclge_update_wol(hdev);
12369 if (ret)
12370 dev_warn(&pdev->dev,
12371 "failed to update wol config, ret = %d\n", ret);
12372
12373 dev_info(&pdev->dev, "Reset done, %s driver initialization finished.\n",
12374 HCLGE_DRIVER_NAME);
12375
12376 return 0;
12377 }
12378
12379 static void hclge_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
12380 {
12381 struct hclge_dev *hdev = ae_dev->priv;
12382 struct hclge_mac *mac = &hdev->hw.mac;
12383
12384 hclge_reset_vf_rate(hdev);
12385 hclge_clear_vf_vlan(hdev);
12386 hclge_state_uninit(hdev);
12387 hclge_ptp_uninit(hdev);
12388 hclge_uninit_rxd_adv_layout(hdev);
12389 hclge_uninit_mac_table(hdev);
12390 hclge_del_all_fd_entries(hdev);
12391
12392 if (mac->phydev)
12393 mdiobus_unregister(mac->mdio_bus);
12394
12395 /* Disable MISC vector(vector0) */
12396 hclge_enable_vector(&hdev->misc_vector, false);
12397 disable_irq(hdev->misc_vector.vector_irq);
12398
12399 /* Disable all hw interrupts */
12400 hclge_config_mac_tnl_int(hdev, false);
12401 hclge_config_nic_hw_error(hdev, false);
12402 hclge_config_rocee_ras_interrupt(hdev, false);
12403
12404 hclge_comm_cmd_uninit(hdev->ae_dev, &hdev->hw.hw);
12405 hclge_misc_irq_uninit(hdev);
12406 hclge_devlink_uninit(hdev);
12407 hclge_pci_uninit(hdev);
12408 hclge_uninit_vport_vlan_table(hdev);
12409 mutex_destroy(&hdev->vport_lock);
12410 ae_dev->priv = NULL;
12411 }
12412
12413 static u32 hclge_get_max_channels(struct hnae3_handle *handle)
12414 {
12415 struct hclge_vport *vport = hclge_get_vport(handle);
12416 struct hclge_dev *hdev = vport->back;
12417
12418 return min_t(u32, hdev->pf_rss_size_max, vport->alloc_tqps);
12419 }
12420
12421 static void hclge_get_channels(struct hnae3_handle *handle,
12422 struct ethtool_channels *ch)
12423 {
12424 ch->max_combined = hclge_get_max_channels(handle);
12425 ch->other_count = 1;
12426 ch->max_other = 1;
12427 ch->combined_count = handle->kinfo.rss_size;
12428 }
12429
12430 static void hclge_get_tqps_and_rss_info(struct hnae3_handle *handle,
12431 u16 *alloc_tqps, u16 *max_rss_size)
12432 {
12433 struct hclge_vport *vport = hclge_get_vport(handle);
12434 struct hclge_dev *hdev = vport->back;
12435
12436 *alloc_tqps = vport->alloc_tqps;
12437 *max_rss_size = hdev->pf_rss_size_max;
12438 }
12439
12440 static int hclge_set_rss_tc_mode_cfg(struct hnae3_handle *handle)
12441 {
12442 struct hclge_vport *vport = hclge_get_vport(handle);
12443 u16 tc_offset[HCLGE_MAX_TC_NUM] = {0};
12444 struct hclge_dev *hdev = vport->back;
12445 u16 tc_size[HCLGE_MAX_TC_NUM] = {0};
12446 u16 tc_valid[HCLGE_MAX_TC_NUM];
12447 u16 roundup_size;
12448 unsigned int i;
12449
12450 roundup_size = roundup_pow_of_two(vport->nic.kinfo.rss_size);
12451 roundup_size = ilog2(roundup_size);
12452 /* Set the RSS TC mode according to the new RSS size */
12453 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
12454 tc_valid[i] = 0;
12455
12456 if (!(hdev->hw_tc_map & BIT(i)))
12457 continue;
12458
12459 tc_valid[i] = 1;
12460 tc_size[i] = roundup_size;
12461 tc_offset[i] = vport->nic.kinfo.rss_size * i;
12462 }
12463
12464 return hclge_comm_set_rss_tc_mode(&hdev->hw.hw, tc_offset, tc_valid,
12465 tc_size);
12466 }
12467
12468 static int hclge_set_channels(struct hnae3_handle *handle, u32 new_tqps_num,
12469 bool rxfh_configured)
12470 {
12471 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
12472 struct hclge_vport *vport = hclge_get_vport(handle);
12473 struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
12474 struct hclge_dev *hdev = vport->back;
12475 u16 cur_rss_size = kinfo->rss_size;
12476 u16 cur_tqps = kinfo->num_tqps;
12477 u32 *rss_indir;
12478 unsigned int i;
12479 int ret;
12480
12481 kinfo->req_rss_size = new_tqps_num;
12482
12483 ret = hclge_tm_vport_map_update(hdev);
12484 if (ret) {
12485 dev_err(&hdev->pdev->dev, "tm vport map fail, ret =%d\n", ret);
12486 return ret;
12487 }
12488
12489 ret = hclge_set_rss_tc_mode_cfg(handle);
12490 if (ret)
12491 return ret;
12492
12493 /* RSS indirection table has been configured by user */
12494 if (rxfh_configured)
12495 goto out;
12496
12497 /* Reinitializes the rss indirect table according to the new RSS size */
12498 rss_indir = kcalloc(ae_dev->dev_specs.rss_ind_tbl_size, sizeof(u32),
12499 GFP_KERNEL);
12500 if (!rss_indir)
12501 return -ENOMEM;
12502
12503 for (i = 0; i < ae_dev->dev_specs.rss_ind_tbl_size; i++)
12504 rss_indir[i] = i % kinfo->rss_size;
12505
12506 ret = hclge_set_rss(handle, rss_indir, NULL, 0);
12507 if (ret)
12508 dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n",
12509 ret);
12510
12511 kfree(rss_indir);
12512
12513 out:
12514 if (!ret)
12515 dev_info(&hdev->pdev->dev,
12516 "Channels changed, rss_size from %u to %u, tqps from %u to %u",
12517 cur_rss_size, kinfo->rss_size,
12518 cur_tqps, kinfo->rss_size * kinfo->tc_info.num_tc);
12519
12520 return ret;
12521 }
12522
12523 static int hclge_set_led_status(struct hclge_dev *hdev, u8 locate_led_status)
12524 {
12525 struct hclge_set_led_state_cmd *req;
12526 struct hclge_desc desc;
12527 int ret;
12528
12529 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_LED_STATUS_CFG, false);
12530
12531 req = (struct hclge_set_led_state_cmd *)desc.data;
12532 hnae3_set_field(req->locate_led_config, HCLGE_LED_LOCATE_STATE_M,
12533 HCLGE_LED_LOCATE_STATE_S, locate_led_status);
12534
12535 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
12536 if (ret)
12537 dev_err(&hdev->pdev->dev,
12538 "Send set led state cmd error, ret =%d\n", ret);
12539
12540 return ret;
12541 }
12542
12543 enum hclge_led_status {
12544 HCLGE_LED_OFF,
12545 HCLGE_LED_ON,
12546 HCLGE_LED_NO_CHANGE = 0xFF,
12547 };
12548
12549 static int hclge_set_led_id(struct hnae3_handle *handle,
12550 enum ethtool_phys_id_state status)
12551 {
12552 struct hclge_vport *vport = hclge_get_vport(handle);
12553 struct hclge_dev *hdev = vport->back;
12554
12555 switch (status) {
12556 case ETHTOOL_ID_ACTIVE:
12557 return hclge_set_led_status(hdev, HCLGE_LED_ON);
12558 case ETHTOOL_ID_INACTIVE:
12559 return hclge_set_led_status(hdev, HCLGE_LED_OFF);
12560 default:
12561 return -EINVAL;
12562 }
12563 }
12564
12565 static void hclge_get_link_mode(struct hnae3_handle *handle,
12566 unsigned long *supported,
12567 unsigned long *advertising)
12568 {
12569 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
12570 struct hclge_vport *vport = hclge_get_vport(handle);
12571 struct hclge_dev *hdev = vport->back;
12572 unsigned int idx = 0;
12573
12574 for (; idx < size; idx++) {
12575 supported[idx] = hdev->hw.mac.supported[idx];
12576 advertising[idx] = hdev->hw.mac.advertising[idx];
12577 }
12578 }
12579
12580 static int hclge_gro_en(struct hnae3_handle *handle, bool enable)
12581 {
12582 struct hclge_vport *vport = hclge_get_vport(handle);
12583 struct hclge_dev *hdev = vport->back;
12584 bool gro_en_old = hdev->gro_en;
12585 int ret;
12586
12587 hdev->gro_en = enable;
12588 ret = hclge_config_gro(hdev);
12589 if (ret)
12590 hdev->gro_en = gro_en_old;
12591
12592 return ret;
12593 }
12594
12595 static int hclge_sync_vport_promisc_mode(struct hclge_vport *vport)
12596 {
12597 struct hnae3_handle *handle = &vport->nic;
12598 struct hclge_dev *hdev = vport->back;
12599 bool uc_en = false;
12600 bool mc_en = false;
12601 u8 tmp_flags;
12602 bool bc_en;
12603 int ret;
12604
12605 if (vport->last_promisc_flags != vport->overflow_promisc_flags) {
12606 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
12607 vport->last_promisc_flags = vport->overflow_promisc_flags;
12608 }
12609
12610 if (!test_and_clear_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE,
12611 &vport->state))
12612 return 0;
12613
12614 /* for PF */
12615 if (!vport->vport_id) {
12616 tmp_flags = handle->netdev_flags | vport->last_promisc_flags;
12617 ret = hclge_set_promisc_mode(handle, tmp_flags & HNAE3_UPE,
12618 tmp_flags & HNAE3_MPE);
12619 if (!ret)
12620 set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
12621 &vport->state);
12622 else
12623 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE,
12624 &vport->state);
12625 return ret;
12626 }
12627
12628 /* for VF */
12629 if (vport->vf_info.trusted) {
12630 uc_en = vport->vf_info.request_uc_en > 0 ||
12631 vport->overflow_promisc_flags & HNAE3_OVERFLOW_UPE;
12632 mc_en = vport->vf_info.request_mc_en > 0 ||
12633 vport->overflow_promisc_flags & HNAE3_OVERFLOW_MPE;
12634 }
12635 bc_en = vport->vf_info.request_bc_en > 0;
12636
12637 ret = hclge_cmd_set_promisc_mode(hdev, vport->vport_id, uc_en,
12638 mc_en, bc_en);
12639 if (ret) {
12640 set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
12641 return ret;
12642 }
12643 hclge_set_vport_vlan_fltr_change(vport);
12644
12645 return 0;
12646 }
12647
12648 static void hclge_sync_promisc_mode(struct hclge_dev *hdev)
12649 {
12650 struct hclge_vport *vport;
12651 int ret;
12652 u16 i;
12653
12654 for (i = 0; i < hdev->num_alloc_vport; i++) {
12655 vport = &hdev->vport[i];
12656
12657 ret = hclge_sync_vport_promisc_mode(vport);
12658 if (ret)
12659 return;
12660 }
12661 }
12662
12663 static bool hclge_module_existed(struct hclge_dev *hdev)
12664 {
12665 struct hclge_desc desc;
12666 u32 existed;
12667 int ret;
12668
12669 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_EXIST, true);
12670 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
12671 if (ret) {
12672 dev_err(&hdev->pdev->dev,
12673 "failed to get SFP exist state, ret = %d\n", ret);
12674 return false;
12675 }
12676
12677 existed = le32_to_cpu(desc.data[0]);
12678
12679 return existed != 0;
12680 }
12681
12682 /* need 6 bds(total 140 bytes) in one reading
12683 * return the number of bytes actually read, 0 means read failed.
12684 */
12685 static u16 hclge_get_sfp_eeprom_info(struct hclge_dev *hdev, u32 offset,
12686 u32 len, u8 *data)
12687 {
12688 struct hclge_desc desc[HCLGE_SFP_INFO_CMD_NUM];
12689 struct hclge_sfp_info_bd0_cmd *sfp_info_bd0;
12690 u16 read_len;
12691 u16 copy_len;
12692 int ret;
12693 int i;
12694
12695 /* setup all 6 bds to read module eeprom info. */
12696 for (i = 0; i < HCLGE_SFP_INFO_CMD_NUM; i++) {
12697 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_GET_SFP_EEPROM,
12698 true);
12699
12700 /* bd0~bd4 need next flag */
12701 if (i < HCLGE_SFP_INFO_CMD_NUM - 1)
12702 desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
12703 }
12704
12705 /* setup bd0, this bd contains offset and read length. */
12706 sfp_info_bd0 = (struct hclge_sfp_info_bd0_cmd *)desc[0].data;
12707 sfp_info_bd0->offset = cpu_to_le16((u16)offset);
12708 read_len = min_t(u16, len, HCLGE_SFP_INFO_MAX_LEN);
12709 sfp_info_bd0->read_len = cpu_to_le16(read_len);
12710
12711 ret = hclge_cmd_send(&hdev->hw, desc, i);
12712 if (ret) {
12713 dev_err(&hdev->pdev->dev,
12714 "failed to get SFP eeprom info, ret = %d\n", ret);
12715 return 0;
12716 }
12717
12718 /* copy sfp info from bd0 to out buffer. */
12719 copy_len = min_t(u16, len, HCLGE_SFP_INFO_BD0_LEN);
12720 memcpy(data, sfp_info_bd0->data, copy_len);
12721 read_len = copy_len;
12722
12723 /* copy sfp info from bd1~bd5 to out buffer if needed. */
12724 for (i = 1; i < HCLGE_SFP_INFO_CMD_NUM; i++) {
12725 if (read_len >= len)
12726 return read_len;
12727
12728 copy_len = min_t(u16, len - read_len, HCLGE_SFP_INFO_BDX_LEN);
12729 memcpy(data + read_len, desc[i].data, copy_len);
12730 read_len += copy_len;
12731 }
12732
12733 return read_len;
12734 }
12735
12736 static int hclge_get_module_eeprom(struct hnae3_handle *handle, u32 offset,
12737 u32 len, u8 *data)
12738 {
12739 struct hclge_vport *vport = hclge_get_vport(handle);
12740 struct hclge_dev *hdev = vport->back;
12741 u32 read_len = 0;
12742 u16 data_len;
12743
12744 if (hdev->hw.mac.media_type != HNAE3_MEDIA_TYPE_FIBER)
12745 return -EOPNOTSUPP;
12746
12747 if (!hclge_module_existed(hdev))
12748 return -ENXIO;
12749
12750 while (read_len < len) {
12751 data_len = hclge_get_sfp_eeprom_info(hdev,
12752 offset + read_len,
12753 len - read_len,
12754 data + read_len);
12755 if (!data_len)
12756 return -EIO;
12757
12758 read_len += data_len;
12759 }
12760
12761 return 0;
12762 }
12763
12764 static int hclge_get_link_diagnosis_info(struct hnae3_handle *handle,
12765 u32 *status_code)
12766 {
12767 struct hclge_vport *vport = hclge_get_vport(handle);
12768 struct hclge_dev *hdev = vport->back;
12769 struct hclge_desc desc;
12770 int ret;
12771
12772 if (hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2)
12773 return -EOPNOTSUPP;
12774
12775 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_LINK_DIAGNOSIS, true);
12776 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
12777 if (ret) {
12778 dev_err(&hdev->pdev->dev,
12779 "failed to query link diagnosis info, ret = %d\n", ret);
12780 return ret;
12781 }
12782
12783 *status_code = le32_to_cpu(desc.data[0]);
12784 return 0;
12785 }
12786
12787 /* After disable sriov, VF still has some config and info need clean,
12788 * which configed by PF.
12789 */
12790 static void hclge_clear_vport_vf_info(struct hclge_vport *vport, int vfid)
12791 {
12792 struct hclge_dev *hdev = vport->back;
12793 struct hclge_vlan_info vlan_info;
12794 int ret;
12795
12796 clear_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
12797 clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
12798 vport->need_notify = 0;
12799 vport->mps = 0;
12800
12801 /* after disable sriov, clean VF rate configured by PF */
12802 ret = hclge_tm_qs_shaper_cfg(vport, 0);
12803 if (ret)
12804 dev_err(&hdev->pdev->dev,
12805 "failed to clean vf%d rate config, ret = %d\n",
12806 vfid, ret);
12807
12808 vlan_info.vlan_tag = 0;
12809 vlan_info.qos = 0;
12810 vlan_info.vlan_proto = ETH_P_8021Q;
12811 ret = hclge_update_port_base_vlan_cfg(vport,
12812 HNAE3_PORT_BASE_VLAN_DISABLE,
12813 &vlan_info);
12814 if (ret)
12815 dev_err(&hdev->pdev->dev,
12816 "failed to clean vf%d port base vlan, ret = %d\n",
12817 vfid, ret);
12818
12819 ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id, false);
12820 if (ret)
12821 dev_err(&hdev->pdev->dev,
12822 "failed to clean vf%d spoof config, ret = %d\n",
12823 vfid, ret);
12824
12825 memset(&vport->vf_info, 0, sizeof(vport->vf_info));
12826 }
12827
12828 static void hclge_clean_vport_config(struct hnae3_ae_dev *ae_dev, int num_vfs)
12829 {
12830 struct hclge_dev *hdev = ae_dev->priv;
12831 struct hclge_vport *vport;
12832 int i;
12833
12834 for (i = 0; i < num_vfs; i++) {
12835 vport = &hdev->vport[i + HCLGE_VF_VPORT_START_NUM];
12836
12837 hclge_clear_vport_vf_info(vport, i);
12838 }
12839 }
12840
12841 static int hclge_get_dscp_prio(struct hnae3_handle *h, u8 dscp, u8 *tc_mode,
12842 u8 *priority)
12843 {
12844 struct hclge_vport *vport = hclge_get_vport(h);
12845
12846 if (dscp >= HNAE3_MAX_DSCP)
12847 return -EINVAL;
12848
12849 if (tc_mode)
12850 *tc_mode = vport->nic.kinfo.tc_map_mode;
12851 if (priority)
12852 *priority = vport->nic.kinfo.dscp_prio[dscp] == HNAE3_PRIO_ID_INVALID ? 0 :
12853 vport->nic.kinfo.dscp_prio[dscp];
12854
12855 return 0;
12856 }
12857
12858 static const struct hnae3_ae_ops hclge_ops = {
12859 .init_ae_dev = hclge_init_ae_dev,
12860 .uninit_ae_dev = hclge_uninit_ae_dev,
12861 .reset_prepare = hclge_reset_prepare_general,
12862 .reset_done = hclge_reset_done,
12863 .init_client_instance = hclge_init_client_instance,
12864 .uninit_client_instance = hclge_uninit_client_instance,
12865 .map_ring_to_vector = hclge_map_ring_to_vector,
12866 .unmap_ring_from_vector = hclge_unmap_ring_frm_vector,
12867 .get_vector = hclge_get_vector,
12868 .put_vector = hclge_put_vector,
12869 .set_promisc_mode = hclge_set_promisc_mode,
12870 .request_update_promisc_mode = hclge_request_update_promisc_mode,
12871 .set_loopback = hclge_set_loopback,
12872 .start = hclge_ae_start,
12873 .stop = hclge_ae_stop,
12874 .client_start = hclge_client_start,
12875 .client_stop = hclge_client_stop,
12876 .get_status = hclge_get_status,
12877 .get_ksettings_an_result = hclge_get_ksettings_an_result,
12878 .cfg_mac_speed_dup_h = hclge_cfg_mac_speed_dup_h,
12879 .get_media_type = hclge_get_media_type,
12880 .check_port_speed = hclge_check_port_speed,
12881 .get_fec_stats = hclge_get_fec_stats,
12882 .get_fec = hclge_get_fec,
12883 .set_fec = hclge_set_fec,
12884 .get_rss_key_size = hclge_comm_get_rss_key_size,
12885 .get_rss = hclge_get_rss,
12886 .set_rss = hclge_set_rss,
12887 .set_rss_tuple = hclge_set_rss_tuple,
12888 .get_rss_tuple = hclge_get_rss_tuple,
12889 .get_tc_size = hclge_get_tc_size,
12890 .get_mac_addr = hclge_get_mac_addr,
12891 .set_mac_addr = hclge_set_mac_addr,
12892 .do_ioctl = hclge_do_ioctl,
12893 .add_uc_addr = hclge_add_uc_addr,
12894 .rm_uc_addr = hclge_rm_uc_addr,
12895 .add_mc_addr = hclge_add_mc_addr,
12896 .rm_mc_addr = hclge_rm_mc_addr,
12897 .set_autoneg = hclge_set_autoneg,
12898 .get_autoneg = hclge_get_autoneg,
12899 .restart_autoneg = hclge_restart_autoneg,
12900 .halt_autoneg = hclge_halt_autoneg,
12901 .get_pauseparam = hclge_get_pauseparam,
12902 .set_pauseparam = hclge_set_pauseparam,
12903 .set_mtu = hclge_set_mtu,
12904 .reset_queue = hclge_reset_tqp,
12905 .get_stats = hclge_get_stats,
12906 .get_mac_stats = hclge_get_mac_stat,
12907 .update_stats = hclge_update_stats,
12908 .get_strings = hclge_get_strings,
12909 .get_sset_count = hclge_get_sset_count,
12910 .get_fw_version = hclge_get_fw_version,
12911 .get_mdix_mode = hclge_get_mdix_mode,
12912 .enable_vlan_filter = hclge_enable_vlan_filter,
12913 .set_vlan_filter = hclge_set_vlan_filter,
12914 .set_vf_vlan_filter = hclge_set_vf_vlan_filter,
12915 .enable_hw_strip_rxvtag = hclge_en_hw_strip_rxvtag,
12916 .reset_event = hclge_reset_event,
12917 .get_reset_level = hclge_get_reset_level,
12918 .set_default_reset_request = hclge_set_def_reset_request,
12919 .get_tqps_and_rss_info = hclge_get_tqps_and_rss_info,
12920 .set_channels = hclge_set_channels,
12921 .get_channels = hclge_get_channels,
12922 .get_regs_len = hclge_get_regs_len,
12923 .get_regs = hclge_get_regs,
12924 .set_led_id = hclge_set_led_id,
12925 .get_link_mode = hclge_get_link_mode,
12926 .add_fd_entry = hclge_add_fd_entry,
12927 .del_fd_entry = hclge_del_fd_entry,
12928 .get_fd_rule_cnt = hclge_get_fd_rule_cnt,
12929 .get_fd_rule_info = hclge_get_fd_rule_info,
12930 .get_fd_all_rules = hclge_get_all_rules,
12931 .enable_fd = hclge_enable_fd,
12932 .add_arfs_entry = hclge_add_fd_entry_by_arfs,
12933 .dbg_get_read_func = hclge_dbg_get_read_func,
12934 .handle_hw_ras_error = hclge_handle_hw_ras_error,
12935 .get_hw_reset_stat = hclge_get_hw_reset_stat,
12936 .ae_dev_resetting = hclge_ae_dev_resetting,
12937 .ae_dev_reset_cnt = hclge_ae_dev_reset_cnt,
12938 .set_gro_en = hclge_gro_en,
12939 .get_global_queue_id = hclge_covert_handle_qid_global,
12940 .set_timer_task = hclge_set_timer_task,
12941 .mac_connect_phy = hclge_mac_connect_phy,
12942 .mac_disconnect_phy = hclge_mac_disconnect_phy,
12943 .get_vf_config = hclge_get_vf_config,
12944 .set_vf_link_state = hclge_set_vf_link_state,
12945 .set_vf_spoofchk = hclge_set_vf_spoofchk,
12946 .set_vf_trust = hclge_set_vf_trust,
12947 .set_vf_rate = hclge_set_vf_rate,
12948 .set_vf_mac = hclge_set_vf_mac,
12949 .get_module_eeprom = hclge_get_module_eeprom,
12950 .get_cmdq_stat = hclge_get_cmdq_stat,
12951 .add_cls_flower = hclge_add_cls_flower,
12952 .del_cls_flower = hclge_del_cls_flower,
12953 .cls_flower_active = hclge_is_cls_flower_active,
12954 .get_phy_link_ksettings = hclge_get_phy_link_ksettings,
12955 .set_phy_link_ksettings = hclge_set_phy_link_ksettings,
12956 .set_tx_hwts_info = hclge_ptp_set_tx_info,
12957 .get_rx_hwts = hclge_ptp_get_rx_hwts,
12958 .get_ts_info = hclge_ptp_get_ts_info,
12959 .get_link_diagnosis_info = hclge_get_link_diagnosis_info,
12960 .clean_vf_config = hclge_clean_vport_config,
12961 .get_dscp_prio = hclge_get_dscp_prio,
12962 .get_wol = hclge_get_wol,
12963 .set_wol = hclge_set_wol,
12964 .hwtstamp_get = hclge_ptp_get_cfg,
12965 .hwtstamp_set = hclge_ptp_set_cfg,
12966 };
12967
12968 static struct hnae3_ae_algo ae_algo = {
12969 .ops = &hclge_ops,
12970 .pdev_id_table = ae_algo_pci_tbl,
12971 };
12972
12973 static int __init hclge_init(void)
12974 {
12975 pr_debug("%s is initializing\n", HCLGE_NAME);
12976
12977 hclge_wq = alloc_workqueue("%s", WQ_UNBOUND, 0,
12978 HCLGE_NAME);
12979 if (!hclge_wq) {
12980 pr_err("%s: failed to create workqueue\n", HCLGE_NAME);
12981 return -ENOMEM;
12982 }
12983
12984 hnae3_register_ae_algo(&ae_algo);
12985
12986 return 0;
12987 }
12988
12989 static void __exit hclge_exit(void)
12990 {
12991 hnae3_acquire_unload_lock();
12992 hnae3_unregister_ae_algo_prepare(&ae_algo);
12993 hnae3_unregister_ae_algo(&ae_algo);
12994 destroy_workqueue(hclge_wq);
12995 hnae3_release_unload_lock();
12996 }
12997 module_init(hclge_init);
12998 module_exit(hclge_exit);
12999
13000 MODULE_LICENSE("GPL");
13001 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
13002 MODULE_DESCRIPTION("HCLGE Driver");
13003 MODULE_VERSION(HCLGE_MOD_VERSION);