Merge branch '10GbE' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/net...
[muen/linux.git] / drivers / net / ethernet / hisilicon / hns3 / hns3pf / hclge_main.c
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/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/pci.h>
13 #include <linux/platform_device.h>
14 #include <linux/if_vlan.h>
15 #include <net/rtnetlink.h>
16 #include "hclge_cmd.h"
17 #include "hclge_dcb.h"
18 #include "hclge_main.h"
19 #include "hclge_mbx.h"
20 #include "hclge_mdio.h"
21 #include "hclge_tm.h"
22 #include "hclge_err.h"
23 #include "hnae3.h"
24
25 #define HCLGE_NAME                      "hclge"
26 #define HCLGE_STATS_READ(p, offset) (*((u64 *)((u8 *)(p) + (offset))))
27 #define HCLGE_MAC_STATS_FIELD_OFF(f) (offsetof(struct hclge_mac_stats, f))
28
29 static int hclge_set_mtu(struct hnae3_handle *handle, int new_mtu);
30 static int hclge_init_vlan_config(struct hclge_dev *hdev);
31 static int hclge_reset_ae_dev(struct hnae3_ae_dev *ae_dev);
32 static int hclge_set_umv_space(struct hclge_dev *hdev, u16 space_size,
33                                u16 *allocated_size, bool is_alloc);
34
35 static struct hnae3_ae_algo ae_algo;
36
37 static const struct pci_device_id ae_algo_pci_tbl[] = {
38         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0},
39         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0},
40         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA), 0},
41         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC), 0},
42         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA), 0},
43         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC), 0},
44         {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC), 0},
45         /* required last entry */
46         {0, }
47 };
48
49 MODULE_DEVICE_TABLE(pci, ae_algo_pci_tbl);
50
51 static const char hns3_nic_test_strs[][ETH_GSTRING_LEN] = {
52         "App    Loopback test",
53         "Serdes serial Loopback test",
54         "Serdes parallel Loopback test",
55         "Phy    Loopback test"
56 };
57
58 static const struct hclge_comm_stats_str g_mac_stats_string[] = {
59         {"mac_tx_mac_pause_num",
60                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_mac_pause_num)},
61         {"mac_rx_mac_pause_num",
62                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_mac_pause_num)},
63         {"mac_tx_pfc_pri0_pkt_num",
64                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri0_pkt_num)},
65         {"mac_tx_pfc_pri1_pkt_num",
66                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri1_pkt_num)},
67         {"mac_tx_pfc_pri2_pkt_num",
68                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri2_pkt_num)},
69         {"mac_tx_pfc_pri3_pkt_num",
70                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri3_pkt_num)},
71         {"mac_tx_pfc_pri4_pkt_num",
72                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri4_pkt_num)},
73         {"mac_tx_pfc_pri5_pkt_num",
74                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri5_pkt_num)},
75         {"mac_tx_pfc_pri6_pkt_num",
76                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri6_pkt_num)},
77         {"mac_tx_pfc_pri7_pkt_num",
78                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri7_pkt_num)},
79         {"mac_rx_pfc_pri0_pkt_num",
80                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri0_pkt_num)},
81         {"mac_rx_pfc_pri1_pkt_num",
82                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri1_pkt_num)},
83         {"mac_rx_pfc_pri2_pkt_num",
84                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri2_pkt_num)},
85         {"mac_rx_pfc_pri3_pkt_num",
86                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri3_pkt_num)},
87         {"mac_rx_pfc_pri4_pkt_num",
88                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri4_pkt_num)},
89         {"mac_rx_pfc_pri5_pkt_num",
90                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri5_pkt_num)},
91         {"mac_rx_pfc_pri6_pkt_num",
92                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri6_pkt_num)},
93         {"mac_rx_pfc_pri7_pkt_num",
94                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri7_pkt_num)},
95         {"mac_tx_total_pkt_num",
96                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_pkt_num)},
97         {"mac_tx_total_oct_num",
98                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_oct_num)},
99         {"mac_tx_good_pkt_num",
100                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_pkt_num)},
101         {"mac_tx_bad_pkt_num",
102                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_pkt_num)},
103         {"mac_tx_good_oct_num",
104                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_oct_num)},
105         {"mac_tx_bad_oct_num",
106                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_oct_num)},
107         {"mac_tx_uni_pkt_num",
108                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_uni_pkt_num)},
109         {"mac_tx_multi_pkt_num",
110                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_multi_pkt_num)},
111         {"mac_tx_broad_pkt_num",
112                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_broad_pkt_num)},
113         {"mac_tx_undersize_pkt_num",
114                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undersize_pkt_num)},
115         {"mac_tx_oversize_pkt_num",
116                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_oversize_pkt_num)},
117         {"mac_tx_64_oct_pkt_num",
118                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_64_oct_pkt_num)},
119         {"mac_tx_65_127_oct_pkt_num",
120                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_65_127_oct_pkt_num)},
121         {"mac_tx_128_255_oct_pkt_num",
122                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_128_255_oct_pkt_num)},
123         {"mac_tx_256_511_oct_pkt_num",
124                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_256_511_oct_pkt_num)},
125         {"mac_tx_512_1023_oct_pkt_num",
126                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_512_1023_oct_pkt_num)},
127         {"mac_tx_1024_1518_oct_pkt_num",
128                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1024_1518_oct_pkt_num)},
129         {"mac_tx_1519_2047_oct_pkt_num",
130                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_2047_oct_pkt_num)},
131         {"mac_tx_2048_4095_oct_pkt_num",
132                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_2048_4095_oct_pkt_num)},
133         {"mac_tx_4096_8191_oct_pkt_num",
134                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_4096_8191_oct_pkt_num)},
135         {"mac_tx_8192_9216_oct_pkt_num",
136                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_8192_9216_oct_pkt_num)},
137         {"mac_tx_9217_12287_oct_pkt_num",
138                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_9217_12287_oct_pkt_num)},
139         {"mac_tx_12288_16383_oct_pkt_num",
140                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_12288_16383_oct_pkt_num)},
141         {"mac_tx_1519_max_good_pkt_num",
142                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_good_oct_pkt_num)},
143         {"mac_tx_1519_max_bad_pkt_num",
144                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_bad_oct_pkt_num)},
145         {"mac_rx_total_pkt_num",
146                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_pkt_num)},
147         {"mac_rx_total_oct_num",
148                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_oct_num)},
149         {"mac_rx_good_pkt_num",
150                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_pkt_num)},
151         {"mac_rx_bad_pkt_num",
152                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_pkt_num)},
153         {"mac_rx_good_oct_num",
154                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_oct_num)},
155         {"mac_rx_bad_oct_num",
156                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_oct_num)},
157         {"mac_rx_uni_pkt_num",
158                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_uni_pkt_num)},
159         {"mac_rx_multi_pkt_num",
160                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_multi_pkt_num)},
161         {"mac_rx_broad_pkt_num",
162                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_broad_pkt_num)},
163         {"mac_rx_undersize_pkt_num",
164                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undersize_pkt_num)},
165         {"mac_rx_oversize_pkt_num",
166                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_oversize_pkt_num)},
167         {"mac_rx_64_oct_pkt_num",
168                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_64_oct_pkt_num)},
169         {"mac_rx_65_127_oct_pkt_num",
170                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_65_127_oct_pkt_num)},
171         {"mac_rx_128_255_oct_pkt_num",
172                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_128_255_oct_pkt_num)},
173         {"mac_rx_256_511_oct_pkt_num",
174                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_256_511_oct_pkt_num)},
175         {"mac_rx_512_1023_oct_pkt_num",
176                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_512_1023_oct_pkt_num)},
177         {"mac_rx_1024_1518_oct_pkt_num",
178                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1024_1518_oct_pkt_num)},
179         {"mac_rx_1519_2047_oct_pkt_num",
180                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_2047_oct_pkt_num)},
181         {"mac_rx_2048_4095_oct_pkt_num",
182                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_2048_4095_oct_pkt_num)},
183         {"mac_rx_4096_8191_oct_pkt_num",
184                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_4096_8191_oct_pkt_num)},
185         {"mac_rx_8192_9216_oct_pkt_num",
186                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_8192_9216_oct_pkt_num)},
187         {"mac_rx_9217_12287_oct_pkt_num",
188                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_9217_12287_oct_pkt_num)},
189         {"mac_rx_12288_16383_oct_pkt_num",
190                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_12288_16383_oct_pkt_num)},
191         {"mac_rx_1519_max_good_pkt_num",
192                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_good_oct_pkt_num)},
193         {"mac_rx_1519_max_bad_pkt_num",
194                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_bad_oct_pkt_num)},
195
196         {"mac_tx_fragment_pkt_num",
197                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_fragment_pkt_num)},
198         {"mac_tx_undermin_pkt_num",
199                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undermin_pkt_num)},
200         {"mac_tx_jabber_pkt_num",
201                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_jabber_pkt_num)},
202         {"mac_tx_err_all_pkt_num",
203                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_err_all_pkt_num)},
204         {"mac_tx_from_app_good_pkt_num",
205                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_good_pkt_num)},
206         {"mac_tx_from_app_bad_pkt_num",
207                 HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_bad_pkt_num)},
208         {"mac_rx_fragment_pkt_num",
209                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fragment_pkt_num)},
210         {"mac_rx_undermin_pkt_num",
211                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undermin_pkt_num)},
212         {"mac_rx_jabber_pkt_num",
213                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_jabber_pkt_num)},
214         {"mac_rx_fcs_err_pkt_num",
215                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fcs_err_pkt_num)},
216         {"mac_rx_send_app_good_pkt_num",
217                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_good_pkt_num)},
218         {"mac_rx_send_app_bad_pkt_num",
219                 HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_bad_pkt_num)}
220 };
221
222 static const struct hclge_mac_mgr_tbl_entry_cmd hclge_mgr_table[] = {
223         {
224                 .flags = HCLGE_MAC_MGR_MASK_VLAN_B,
225                 .ethter_type = cpu_to_le16(HCLGE_MAC_ETHERTYPE_LLDP),
226                 .mac_addr_hi32 = cpu_to_le32(htonl(0x0180C200)),
227                 .mac_addr_lo16 = cpu_to_le16(htons(0x000E)),
228                 .i_port_bitmap = 0x1,
229         },
230 };
231
232 static int hclge_mac_update_stats(struct hclge_dev *hdev)
233 {
234 #define HCLGE_MAC_CMD_NUM 21
235 #define HCLGE_RTN_DATA_NUM 4
236
237         u64 *data = (u64 *)(&hdev->hw_stats.mac_stats);
238         struct hclge_desc desc[HCLGE_MAC_CMD_NUM];
239         __le64 *desc_data;
240         int i, k, n;
241         int ret;
242
243         hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_STATS_MAC, true);
244         ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_MAC_CMD_NUM);
245         if (ret) {
246                 dev_err(&hdev->pdev->dev,
247                         "Get MAC pkt stats fail, status = %d.\n", ret);
248
249                 return ret;
250         }
251
252         for (i = 0; i < HCLGE_MAC_CMD_NUM; i++) {
253                 if (unlikely(i == 0)) {
254                         desc_data = (__le64 *)(&desc[i].data[0]);
255                         n = HCLGE_RTN_DATA_NUM - 2;
256                 } else {
257                         desc_data = (__le64 *)(&desc[i]);
258                         n = HCLGE_RTN_DATA_NUM;
259                 }
260                 for (k = 0; k < n; k++) {
261                         *data++ += le64_to_cpu(*desc_data);
262                         desc_data++;
263                 }
264         }
265
266         return 0;
267 }
268
269 static int hclge_tqps_update_stats(struct hnae3_handle *handle)
270 {
271         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
272         struct hclge_vport *vport = hclge_get_vport(handle);
273         struct hclge_dev *hdev = vport->back;
274         struct hnae3_queue *queue;
275         struct hclge_desc desc[1];
276         struct hclge_tqp *tqp;
277         int ret, i;
278
279         for (i = 0; i < kinfo->num_tqps; i++) {
280                 queue = handle->kinfo.tqp[i];
281                 tqp = container_of(queue, struct hclge_tqp, q);
282                 /* command : HCLGE_OPC_QUERY_IGU_STAT */
283                 hclge_cmd_setup_basic_desc(&desc[0],
284                                            HCLGE_OPC_QUERY_RX_STATUS,
285                                            true);
286
287                 desc[0].data[0] = cpu_to_le32((tqp->index & 0x1ff));
288                 ret = hclge_cmd_send(&hdev->hw, desc, 1);
289                 if (ret) {
290                         dev_err(&hdev->pdev->dev,
291                                 "Query tqp stat fail, status = %d,queue = %d\n",
292                                 ret,    i);
293                         return ret;
294                 }
295                 tqp->tqp_stats.rcb_rx_ring_pktnum_rcd +=
296                         le32_to_cpu(desc[0].data[1]);
297         }
298
299         for (i = 0; i < kinfo->num_tqps; i++) {
300                 queue = handle->kinfo.tqp[i];
301                 tqp = container_of(queue, struct hclge_tqp, q);
302                 /* command : HCLGE_OPC_QUERY_IGU_STAT */
303                 hclge_cmd_setup_basic_desc(&desc[0],
304                                            HCLGE_OPC_QUERY_TX_STATUS,
305                                            true);
306
307                 desc[0].data[0] = cpu_to_le32((tqp->index & 0x1ff));
308                 ret = hclge_cmd_send(&hdev->hw, desc, 1);
309                 if (ret) {
310                         dev_err(&hdev->pdev->dev,
311                                 "Query tqp stat fail, status = %d,queue = %d\n",
312                                 ret, i);
313                         return ret;
314                 }
315                 tqp->tqp_stats.rcb_tx_ring_pktnum_rcd +=
316                         le32_to_cpu(desc[0].data[1]);
317         }
318
319         return 0;
320 }
321
322 static u64 *hclge_tqps_get_stats(struct hnae3_handle *handle, u64 *data)
323 {
324         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
325         struct hclge_tqp *tqp;
326         u64 *buff = data;
327         int i;
328
329         for (i = 0; i < kinfo->num_tqps; i++) {
330                 tqp = container_of(kinfo->tqp[i], struct hclge_tqp, q);
331                 *buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd;
332         }
333
334         for (i = 0; i < kinfo->num_tqps; i++) {
335                 tqp = container_of(kinfo->tqp[i], struct hclge_tqp, q);
336                 *buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd;
337         }
338
339         return buff;
340 }
341
342 static int hclge_tqps_get_sset_count(struct hnae3_handle *handle, int stringset)
343 {
344         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
345
346         return kinfo->num_tqps * (2);
347 }
348
349 static u8 *hclge_tqps_get_strings(struct hnae3_handle *handle, u8 *data)
350 {
351         struct hnae3_knic_private_info *kinfo = &handle->kinfo;
352         u8 *buff = data;
353         int i = 0;
354
355         for (i = 0; i < kinfo->num_tqps; i++) {
356                 struct hclge_tqp *tqp = container_of(handle->kinfo.tqp[i],
357                         struct hclge_tqp, q);
358                 snprintf(buff, ETH_GSTRING_LEN, "txq%d_pktnum_rcd",
359                          tqp->index);
360                 buff = buff + ETH_GSTRING_LEN;
361         }
362
363         for (i = 0; i < kinfo->num_tqps; i++) {
364                 struct hclge_tqp *tqp = container_of(kinfo->tqp[i],
365                         struct hclge_tqp, q);
366                 snprintf(buff, ETH_GSTRING_LEN, "rxq%d_pktnum_rcd",
367                          tqp->index);
368                 buff = buff + ETH_GSTRING_LEN;
369         }
370
371         return buff;
372 }
373
374 static u64 *hclge_comm_get_stats(void *comm_stats,
375                                  const struct hclge_comm_stats_str strs[],
376                                  int size, u64 *data)
377 {
378         u64 *buf = data;
379         u32 i;
380
381         for (i = 0; i < size; i++)
382                 buf[i] = HCLGE_STATS_READ(comm_stats, strs[i].offset);
383
384         return buf + size;
385 }
386
387 static u8 *hclge_comm_get_strings(u32 stringset,
388                                   const struct hclge_comm_stats_str strs[],
389                                   int size, u8 *data)
390 {
391         char *buff = (char *)data;
392         u32 i;
393
394         if (stringset != ETH_SS_STATS)
395                 return buff;
396
397         for (i = 0; i < size; i++) {
398                 snprintf(buff, ETH_GSTRING_LEN,
399                          strs[i].desc);
400                 buff = buff + ETH_GSTRING_LEN;
401         }
402
403         return (u8 *)buff;
404 }
405
406 static void hclge_update_netstat(struct hclge_hw_stats *hw_stats,
407                                  struct net_device_stats *net_stats)
408 {
409         net_stats->tx_dropped = 0;
410         net_stats->rx_errors = hw_stats->mac_stats.mac_rx_oversize_pkt_num;
411         net_stats->rx_errors += hw_stats->mac_stats.mac_rx_undersize_pkt_num;
412         net_stats->rx_errors += hw_stats->mac_stats.mac_rx_fcs_err_pkt_num;
413
414         net_stats->multicast = hw_stats->mac_stats.mac_tx_multi_pkt_num;
415         net_stats->multicast += hw_stats->mac_stats.mac_rx_multi_pkt_num;
416
417         net_stats->rx_crc_errors = hw_stats->mac_stats.mac_rx_fcs_err_pkt_num;
418         net_stats->rx_length_errors =
419                 hw_stats->mac_stats.mac_rx_undersize_pkt_num;
420         net_stats->rx_length_errors +=
421                 hw_stats->mac_stats.mac_rx_oversize_pkt_num;
422         net_stats->rx_over_errors =
423                 hw_stats->mac_stats.mac_rx_oversize_pkt_num;
424 }
425
426 static void hclge_update_stats_for_all(struct hclge_dev *hdev)
427 {
428         struct hnae3_handle *handle;
429         int status;
430
431         handle = &hdev->vport[0].nic;
432         if (handle->client) {
433                 status = hclge_tqps_update_stats(handle);
434                 if (status) {
435                         dev_err(&hdev->pdev->dev,
436                                 "Update TQPS stats fail, status = %d.\n",
437                                 status);
438                 }
439         }
440
441         status = hclge_mac_update_stats(hdev);
442         if (status)
443                 dev_err(&hdev->pdev->dev,
444                         "Update MAC stats fail, status = %d.\n", status);
445
446         hclge_update_netstat(&hdev->hw_stats, &handle->kinfo.netdev->stats);
447 }
448
449 static void hclge_update_stats(struct hnae3_handle *handle,
450                                struct net_device_stats *net_stats)
451 {
452         struct hclge_vport *vport = hclge_get_vport(handle);
453         struct hclge_dev *hdev = vport->back;
454         struct hclge_hw_stats *hw_stats = &hdev->hw_stats;
455         int status;
456
457         if (test_and_set_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state))
458                 return;
459
460         status = hclge_mac_update_stats(hdev);
461         if (status)
462                 dev_err(&hdev->pdev->dev,
463                         "Update MAC stats fail, status = %d.\n",
464                         status);
465
466         status = hclge_tqps_update_stats(handle);
467         if (status)
468                 dev_err(&hdev->pdev->dev,
469                         "Update TQPS stats fail, status = %d.\n",
470                         status);
471
472         hclge_update_netstat(hw_stats, net_stats);
473
474         clear_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state);
475 }
476
477 static int hclge_get_sset_count(struct hnae3_handle *handle, int stringset)
478 {
479 #define HCLGE_LOOPBACK_TEST_FLAGS (HNAE3_SUPPORT_APP_LOOPBACK |\
480                 HNAE3_SUPPORT_PHY_LOOPBACK |\
481                 HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK |\
482                 HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK)
483
484         struct hclge_vport *vport = hclge_get_vport(handle);
485         struct hclge_dev *hdev = vport->back;
486         int count = 0;
487
488         /* Loopback test support rules:
489          * mac: only GE mode support
490          * serdes: all mac mode will support include GE/XGE/LGE/CGE
491          * phy: only support when phy device exist on board
492          */
493         if (stringset == ETH_SS_TEST) {
494                 /* clear loopback bit flags at first */
495                 handle->flags = (handle->flags & (~HCLGE_LOOPBACK_TEST_FLAGS));
496                 if (hdev->pdev->revision >= 0x21 ||
497                     hdev->hw.mac.speed == HCLGE_MAC_SPEED_10M ||
498                     hdev->hw.mac.speed == HCLGE_MAC_SPEED_100M ||
499                     hdev->hw.mac.speed == HCLGE_MAC_SPEED_1G) {
500                         count += 1;
501                         handle->flags |= HNAE3_SUPPORT_APP_LOOPBACK;
502                 }
503
504                 count += 2;
505                 handle->flags |= HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
506                 handle->flags |= HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
507         } else if (stringset == ETH_SS_STATS) {
508                 count = ARRAY_SIZE(g_mac_stats_string) +
509                         hclge_tqps_get_sset_count(handle, stringset);
510         }
511
512         return count;
513 }
514
515 static void hclge_get_strings(struct hnae3_handle *handle,
516                               u32 stringset,
517                               u8 *data)
518 {
519         u8 *p = (char *)data;
520         int size;
521
522         if (stringset == ETH_SS_STATS) {
523                 size = ARRAY_SIZE(g_mac_stats_string);
524                 p = hclge_comm_get_strings(stringset,
525                                            g_mac_stats_string,
526                                            size,
527                                            p);
528                 p = hclge_tqps_get_strings(handle, p);
529         } else if (stringset == ETH_SS_TEST) {
530                 if (handle->flags & HNAE3_SUPPORT_APP_LOOPBACK) {
531                         memcpy(p,
532                                hns3_nic_test_strs[HNAE3_LOOP_APP],
533                                ETH_GSTRING_LEN);
534                         p += ETH_GSTRING_LEN;
535                 }
536                 if (handle->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK) {
537                         memcpy(p,
538                                hns3_nic_test_strs[HNAE3_LOOP_SERIAL_SERDES],
539                                ETH_GSTRING_LEN);
540                         p += ETH_GSTRING_LEN;
541                 }
542                 if (handle->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK) {
543                         memcpy(p,
544                                hns3_nic_test_strs[HNAE3_LOOP_PARALLEL_SERDES],
545                                ETH_GSTRING_LEN);
546                         p += ETH_GSTRING_LEN;
547                 }
548                 if (handle->flags & HNAE3_SUPPORT_PHY_LOOPBACK) {
549                         memcpy(p,
550                                hns3_nic_test_strs[HNAE3_LOOP_PHY],
551                                ETH_GSTRING_LEN);
552                         p += ETH_GSTRING_LEN;
553                 }
554         }
555 }
556
557 static void hclge_get_stats(struct hnae3_handle *handle, u64 *data)
558 {
559         struct hclge_vport *vport = hclge_get_vport(handle);
560         struct hclge_dev *hdev = vport->back;
561         u64 *p;
562
563         p = hclge_comm_get_stats(&hdev->hw_stats.mac_stats,
564                                  g_mac_stats_string,
565                                  ARRAY_SIZE(g_mac_stats_string),
566                                  data);
567         p = hclge_tqps_get_stats(handle, p);
568 }
569
570 static int hclge_parse_func_status(struct hclge_dev *hdev,
571                                    struct hclge_func_status_cmd *status)
572 {
573         if (!(status->pf_state & HCLGE_PF_STATE_DONE))
574                 return -EINVAL;
575
576         /* Set the pf to main pf */
577         if (status->pf_state & HCLGE_PF_STATE_MAIN)
578                 hdev->flag |= HCLGE_FLAG_MAIN;
579         else
580                 hdev->flag &= ~HCLGE_FLAG_MAIN;
581
582         return 0;
583 }
584
585 static int hclge_query_function_status(struct hclge_dev *hdev)
586 {
587         struct hclge_func_status_cmd *req;
588         struct hclge_desc desc;
589         int timeout = 0;
590         int ret;
591
592         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_FUNC_STATUS, true);
593         req = (struct hclge_func_status_cmd *)desc.data;
594
595         do {
596                 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
597                 if (ret) {
598                         dev_err(&hdev->pdev->dev,
599                                 "query function status failed %d.\n",
600                                 ret);
601
602                         return ret;
603                 }
604
605                 /* Check pf reset is done */
606                 if (req->pf_state)
607                         break;
608                 usleep_range(1000, 2000);
609         } while (timeout++ < 5);
610
611         ret = hclge_parse_func_status(hdev, req);
612
613         return ret;
614 }
615
616 static int hclge_query_pf_resource(struct hclge_dev *hdev)
617 {
618         struct hclge_pf_res_cmd *req;
619         struct hclge_desc desc;
620         int ret;
621
622         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_PF_RSRC, true);
623         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
624         if (ret) {
625                 dev_err(&hdev->pdev->dev,
626                         "query pf resource failed %d.\n", ret);
627                 return ret;
628         }
629
630         req = (struct hclge_pf_res_cmd *)desc.data;
631         hdev->num_tqps = __le16_to_cpu(req->tqp_num);
632         hdev->pkt_buf_size = __le16_to_cpu(req->buf_size) << HCLGE_BUF_UNIT_S;
633
634         if (hnae3_dev_roce_supported(hdev)) {
635                 hdev->roce_base_msix_offset =
636                 hnae3_get_field(__le16_to_cpu(req->msixcap_localid_ba_rocee),
637                                 HCLGE_MSIX_OFT_ROCEE_M, HCLGE_MSIX_OFT_ROCEE_S);
638                 hdev->num_roce_msi =
639                 hnae3_get_field(__le16_to_cpu(req->pf_intr_vector_number),
640                                 HCLGE_PF_VEC_NUM_M, HCLGE_PF_VEC_NUM_S);
641
642                 /* PF should have NIC vectors and Roce vectors,
643                  * NIC vectors are queued before Roce vectors.
644                  */
645                 hdev->num_msi = hdev->num_roce_msi  +
646                                 hdev->roce_base_msix_offset;
647         } else {
648                 hdev->num_msi =
649                 hnae3_get_field(__le16_to_cpu(req->pf_intr_vector_number),
650                                 HCLGE_PF_VEC_NUM_M, HCLGE_PF_VEC_NUM_S);
651         }
652
653         return 0;
654 }
655
656 static int hclge_parse_speed(int speed_cmd, int *speed)
657 {
658         switch (speed_cmd) {
659         case 6:
660                 *speed = HCLGE_MAC_SPEED_10M;
661                 break;
662         case 7:
663                 *speed = HCLGE_MAC_SPEED_100M;
664                 break;
665         case 0:
666                 *speed = HCLGE_MAC_SPEED_1G;
667                 break;
668         case 1:
669                 *speed = HCLGE_MAC_SPEED_10G;
670                 break;
671         case 2:
672                 *speed = HCLGE_MAC_SPEED_25G;
673                 break;
674         case 3:
675                 *speed = HCLGE_MAC_SPEED_40G;
676                 break;
677         case 4:
678                 *speed = HCLGE_MAC_SPEED_50G;
679                 break;
680         case 5:
681                 *speed = HCLGE_MAC_SPEED_100G;
682                 break;
683         default:
684                 return -EINVAL;
685         }
686
687         return 0;
688 }
689
690 static void hclge_parse_fiber_link_mode(struct hclge_dev *hdev,
691                                         u8 speed_ability)
692 {
693         unsigned long *supported = hdev->hw.mac.supported;
694
695         if (speed_ability & HCLGE_SUPPORT_1G_BIT)
696                 set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
697                         supported);
698
699         if (speed_ability & HCLGE_SUPPORT_10G_BIT)
700                 set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
701                         supported);
702
703         if (speed_ability & HCLGE_SUPPORT_25G_BIT)
704                 set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
705                         supported);
706
707         if (speed_ability & HCLGE_SUPPORT_50G_BIT)
708                 set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
709                         supported);
710
711         if (speed_ability & HCLGE_SUPPORT_100G_BIT)
712                 set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
713                         supported);
714
715         set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
716         set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
717 }
718
719 static void hclge_parse_link_mode(struct hclge_dev *hdev, u8 speed_ability)
720 {
721         u8 media_type = hdev->hw.mac.media_type;
722
723         if (media_type != HNAE3_MEDIA_TYPE_FIBER)
724                 return;
725
726         hclge_parse_fiber_link_mode(hdev, speed_ability);
727 }
728
729 static void hclge_parse_cfg(struct hclge_cfg *cfg, struct hclge_desc *desc)
730 {
731         struct hclge_cfg_param_cmd *req;
732         u64 mac_addr_tmp_high;
733         u64 mac_addr_tmp;
734         int i;
735
736         req = (struct hclge_cfg_param_cmd *)desc[0].data;
737
738         /* get the configuration */
739         cfg->vmdq_vport_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
740                                               HCLGE_CFG_VMDQ_M,
741                                               HCLGE_CFG_VMDQ_S);
742         cfg->tc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
743                                       HCLGE_CFG_TC_NUM_M, HCLGE_CFG_TC_NUM_S);
744         cfg->tqp_desc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
745                                             HCLGE_CFG_TQP_DESC_N_M,
746                                             HCLGE_CFG_TQP_DESC_N_S);
747
748         cfg->phy_addr = hnae3_get_field(__le32_to_cpu(req->param[1]),
749                                         HCLGE_CFG_PHY_ADDR_M,
750                                         HCLGE_CFG_PHY_ADDR_S);
751         cfg->media_type = hnae3_get_field(__le32_to_cpu(req->param[1]),
752                                           HCLGE_CFG_MEDIA_TP_M,
753                                           HCLGE_CFG_MEDIA_TP_S);
754         cfg->rx_buf_len = hnae3_get_field(__le32_to_cpu(req->param[1]),
755                                           HCLGE_CFG_RX_BUF_LEN_M,
756                                           HCLGE_CFG_RX_BUF_LEN_S);
757         /* get mac_address */
758         mac_addr_tmp = __le32_to_cpu(req->param[2]);
759         mac_addr_tmp_high = hnae3_get_field(__le32_to_cpu(req->param[3]),
760                                             HCLGE_CFG_MAC_ADDR_H_M,
761                                             HCLGE_CFG_MAC_ADDR_H_S);
762
763         mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
764
765         cfg->default_speed = hnae3_get_field(__le32_to_cpu(req->param[3]),
766                                              HCLGE_CFG_DEFAULT_SPEED_M,
767                                              HCLGE_CFG_DEFAULT_SPEED_S);
768         cfg->rss_size_max = hnae3_get_field(__le32_to_cpu(req->param[3]),
769                                             HCLGE_CFG_RSS_SIZE_M,
770                                             HCLGE_CFG_RSS_SIZE_S);
771
772         for (i = 0; i < ETH_ALEN; i++)
773                 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
774
775         req = (struct hclge_cfg_param_cmd *)desc[1].data;
776         cfg->numa_node_map = __le32_to_cpu(req->param[0]);
777
778         cfg->speed_ability = hnae3_get_field(__le32_to_cpu(req->param[1]),
779                                              HCLGE_CFG_SPEED_ABILITY_M,
780                                              HCLGE_CFG_SPEED_ABILITY_S);
781         cfg->umv_space = hnae3_get_field(__le32_to_cpu(req->param[1]),
782                                          HCLGE_CFG_UMV_TBL_SPACE_M,
783                                          HCLGE_CFG_UMV_TBL_SPACE_S);
784         if (!cfg->umv_space)
785                 cfg->umv_space = HCLGE_DEFAULT_UMV_SPACE_PER_PF;
786 }
787
788 /* hclge_get_cfg: query the static parameter from flash
789  * @hdev: pointer to struct hclge_dev
790  * @hcfg: the config structure to be getted
791  */
792 static int hclge_get_cfg(struct hclge_dev *hdev, struct hclge_cfg *hcfg)
793 {
794         struct hclge_desc desc[HCLGE_PF_CFG_DESC_NUM];
795         struct hclge_cfg_param_cmd *req;
796         int i, ret;
797
798         for (i = 0; i < HCLGE_PF_CFG_DESC_NUM; i++) {
799                 u32 offset = 0;
800
801                 req = (struct hclge_cfg_param_cmd *)desc[i].data;
802                 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_GET_CFG_PARAM,
803                                            true);
804                 hnae3_set_field(offset, HCLGE_CFG_OFFSET_M,
805                                 HCLGE_CFG_OFFSET_S, i * HCLGE_CFG_RD_LEN_BYTES);
806                 /* Len should be united by 4 bytes when send to hardware */
807                 hnae3_set_field(offset, HCLGE_CFG_RD_LEN_M, HCLGE_CFG_RD_LEN_S,
808                                 HCLGE_CFG_RD_LEN_BYTES / HCLGE_CFG_RD_LEN_UNIT);
809                 req->offset = cpu_to_le32(offset);
810         }
811
812         ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PF_CFG_DESC_NUM);
813         if (ret) {
814                 dev_err(&hdev->pdev->dev, "get config failed %d.\n", ret);
815                 return ret;
816         }
817
818         hclge_parse_cfg(hcfg, desc);
819
820         return 0;
821 }
822
823 static int hclge_get_cap(struct hclge_dev *hdev)
824 {
825         int ret;
826
827         ret = hclge_query_function_status(hdev);
828         if (ret) {
829                 dev_err(&hdev->pdev->dev,
830                         "query function status error %d.\n", ret);
831                 return ret;
832         }
833
834         /* get pf resource */
835         ret = hclge_query_pf_resource(hdev);
836         if (ret)
837                 dev_err(&hdev->pdev->dev, "query pf resource error %d.\n", ret);
838
839         return ret;
840 }
841
842 static int hclge_configure(struct hclge_dev *hdev)
843 {
844         struct hclge_cfg cfg;
845         int ret, i;
846
847         ret = hclge_get_cfg(hdev, &cfg);
848         if (ret) {
849                 dev_err(&hdev->pdev->dev, "get mac mode error %d.\n", ret);
850                 return ret;
851         }
852
853         hdev->num_vmdq_vport = cfg.vmdq_vport_num;
854         hdev->base_tqp_pid = 0;
855         hdev->rss_size_max = cfg.rss_size_max;
856         hdev->rx_buf_len = cfg.rx_buf_len;
857         ether_addr_copy(hdev->hw.mac.mac_addr, cfg.mac_addr);
858         hdev->hw.mac.media_type = cfg.media_type;
859         hdev->hw.mac.phy_addr = cfg.phy_addr;
860         hdev->num_desc = cfg.tqp_desc_num;
861         hdev->tm_info.num_pg = 1;
862         hdev->tc_max = cfg.tc_num;
863         hdev->tm_info.hw_pfc_map = 0;
864         hdev->wanted_umv_size = cfg.umv_space;
865
866         ret = hclge_parse_speed(cfg.default_speed, &hdev->hw.mac.speed);
867         if (ret) {
868                 dev_err(&hdev->pdev->dev, "Get wrong speed ret=%d.\n", ret);
869                 return ret;
870         }
871
872         hclge_parse_link_mode(hdev, cfg.speed_ability);
873
874         if ((hdev->tc_max > HNAE3_MAX_TC) ||
875             (hdev->tc_max < 1)) {
876                 dev_warn(&hdev->pdev->dev, "TC num = %d.\n",
877                          hdev->tc_max);
878                 hdev->tc_max = 1;
879         }
880
881         /* Dev does not support DCB */
882         if (!hnae3_dev_dcb_supported(hdev)) {
883                 hdev->tc_max = 1;
884                 hdev->pfc_max = 0;
885         } else {
886                 hdev->pfc_max = hdev->tc_max;
887         }
888
889         hdev->tm_info.num_tc = hdev->tc_max;
890
891         /* Currently not support uncontiuous tc */
892         for (i = 0; i < hdev->tm_info.num_tc; i++)
893                 hnae3_set_bit(hdev->hw_tc_map, i, 1);
894
895         hdev->tx_sch_mode = HCLGE_FLAG_TC_BASE_SCH_MODE;
896
897         return ret;
898 }
899
900 static int hclge_config_tso(struct hclge_dev *hdev, int tso_mss_min,
901                             int tso_mss_max)
902 {
903         struct hclge_cfg_tso_status_cmd *req;
904         struct hclge_desc desc;
905         u16 tso_mss;
906
907         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TSO_GENERIC_CONFIG, false);
908
909         req = (struct hclge_cfg_tso_status_cmd *)desc.data;
910
911         tso_mss = 0;
912         hnae3_set_field(tso_mss, HCLGE_TSO_MSS_MIN_M,
913                         HCLGE_TSO_MSS_MIN_S, tso_mss_min);
914         req->tso_mss_min = cpu_to_le16(tso_mss);
915
916         tso_mss = 0;
917         hnae3_set_field(tso_mss, HCLGE_TSO_MSS_MIN_M,
918                         HCLGE_TSO_MSS_MIN_S, tso_mss_max);
919         req->tso_mss_max = cpu_to_le16(tso_mss);
920
921         return hclge_cmd_send(&hdev->hw, &desc, 1);
922 }
923
924 static int hclge_alloc_tqps(struct hclge_dev *hdev)
925 {
926         struct hclge_tqp *tqp;
927         int i;
928
929         hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
930                                   sizeof(struct hclge_tqp), GFP_KERNEL);
931         if (!hdev->htqp)
932                 return -ENOMEM;
933
934         tqp = hdev->htqp;
935
936         for (i = 0; i < hdev->num_tqps; i++) {
937                 tqp->dev = &hdev->pdev->dev;
938                 tqp->index = i;
939
940                 tqp->q.ae_algo = &ae_algo;
941                 tqp->q.buf_size = hdev->rx_buf_len;
942                 tqp->q.desc_num = hdev->num_desc;
943                 tqp->q.io_base = hdev->hw.io_base + HCLGE_TQP_REG_OFFSET +
944                         i * HCLGE_TQP_REG_SIZE;
945
946                 tqp++;
947         }
948
949         return 0;
950 }
951
952 static int hclge_map_tqps_to_func(struct hclge_dev *hdev, u16 func_id,
953                                   u16 tqp_pid, u16 tqp_vid, bool is_pf)
954 {
955         struct hclge_tqp_map_cmd *req;
956         struct hclge_desc desc;
957         int ret;
958
959         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_SET_TQP_MAP, false);
960
961         req = (struct hclge_tqp_map_cmd *)desc.data;
962         req->tqp_id = cpu_to_le16(tqp_pid);
963         req->tqp_vf = func_id;
964         req->tqp_flag = !is_pf << HCLGE_TQP_MAP_TYPE_B |
965                         1 << HCLGE_TQP_MAP_EN_B;
966         req->tqp_vid = cpu_to_le16(tqp_vid);
967
968         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
969         if (ret)
970                 dev_err(&hdev->pdev->dev, "TQP map failed %d.\n", ret);
971
972         return ret;
973 }
974
975 static int  hclge_assign_tqp(struct hclge_vport *vport)
976 {
977         struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
978         struct hclge_dev *hdev = vport->back;
979         int i, alloced;
980
981         for (i = 0, alloced = 0; i < hdev->num_tqps &&
982              alloced < kinfo->num_tqps; i++) {
983                 if (!hdev->htqp[i].alloced) {
984                         hdev->htqp[i].q.handle = &vport->nic;
985                         hdev->htqp[i].q.tqp_index = alloced;
986                         hdev->htqp[i].q.desc_num = kinfo->num_desc;
987                         kinfo->tqp[alloced] = &hdev->htqp[i].q;
988                         hdev->htqp[i].alloced = true;
989                         alloced++;
990                 }
991         }
992         vport->alloc_tqps = kinfo->num_tqps;
993
994         return 0;
995 }
996
997 static int hclge_knic_setup(struct hclge_vport *vport,
998                             u16 num_tqps, u16 num_desc)
999 {
1000         struct hnae3_handle *nic = &vport->nic;
1001         struct hnae3_knic_private_info *kinfo = &nic->kinfo;
1002         struct hclge_dev *hdev = vport->back;
1003         int i, ret;
1004
1005         kinfo->num_desc = num_desc;
1006         kinfo->rx_buf_len = hdev->rx_buf_len;
1007         kinfo->num_tc = min_t(u16, num_tqps, hdev->tm_info.num_tc);
1008         kinfo->rss_size
1009                 = min_t(u16, hdev->rss_size_max, num_tqps / kinfo->num_tc);
1010         kinfo->num_tqps = kinfo->rss_size * kinfo->num_tc;
1011
1012         for (i = 0; i < HNAE3_MAX_TC; i++) {
1013                 if (hdev->hw_tc_map & BIT(i)) {
1014                         kinfo->tc_info[i].enable = true;
1015                         kinfo->tc_info[i].tqp_offset = i * kinfo->rss_size;
1016                         kinfo->tc_info[i].tqp_count = kinfo->rss_size;
1017                         kinfo->tc_info[i].tc = i;
1018                 } else {
1019                         /* Set to default queue if TC is disable */
1020                         kinfo->tc_info[i].enable = false;
1021                         kinfo->tc_info[i].tqp_offset = 0;
1022                         kinfo->tc_info[i].tqp_count = 1;
1023                         kinfo->tc_info[i].tc = 0;
1024                 }
1025         }
1026
1027         kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps,
1028                                   sizeof(struct hnae3_queue *), GFP_KERNEL);
1029         if (!kinfo->tqp)
1030                 return -ENOMEM;
1031
1032         ret = hclge_assign_tqp(vport);
1033         if (ret)
1034                 dev_err(&hdev->pdev->dev, "fail to assign TQPs %d.\n", ret);
1035
1036         return ret;
1037 }
1038
1039 static int hclge_map_tqp_to_vport(struct hclge_dev *hdev,
1040                                   struct hclge_vport *vport)
1041 {
1042         struct hnae3_handle *nic = &vport->nic;
1043         struct hnae3_knic_private_info *kinfo;
1044         u16 i;
1045
1046         kinfo = &nic->kinfo;
1047         for (i = 0; i < kinfo->num_tqps; i++) {
1048                 struct hclge_tqp *q =
1049                         container_of(kinfo->tqp[i], struct hclge_tqp, q);
1050                 bool is_pf;
1051                 int ret;
1052
1053                 is_pf = !(vport->vport_id);
1054                 ret = hclge_map_tqps_to_func(hdev, vport->vport_id, q->index,
1055                                              i, is_pf);
1056                 if (ret)
1057                         return ret;
1058         }
1059
1060         return 0;
1061 }
1062
1063 static int hclge_map_tqp(struct hclge_dev *hdev)
1064 {
1065         struct hclge_vport *vport = hdev->vport;
1066         u16 i, num_vport;
1067
1068         num_vport = hdev->num_vmdq_vport + hdev->num_req_vfs + 1;
1069         for (i = 0; i < num_vport; i++) {
1070                 int ret;
1071
1072                 ret = hclge_map_tqp_to_vport(hdev, vport);
1073                 if (ret)
1074                         return ret;
1075
1076                 vport++;
1077         }
1078
1079         return 0;
1080 }
1081
1082 static void hclge_unic_setup(struct hclge_vport *vport, u16 num_tqps)
1083 {
1084         /* this would be initialized later */
1085 }
1086
1087 static int hclge_vport_setup(struct hclge_vport *vport, u16 num_tqps)
1088 {
1089         struct hnae3_handle *nic = &vport->nic;
1090         struct hclge_dev *hdev = vport->back;
1091         int ret;
1092
1093         nic->pdev = hdev->pdev;
1094         nic->ae_algo = &ae_algo;
1095         nic->numa_node_mask = hdev->numa_node_mask;
1096
1097         if (hdev->ae_dev->dev_type == HNAE3_DEV_KNIC) {
1098                 ret = hclge_knic_setup(vport, num_tqps, hdev->num_desc);
1099                 if (ret) {
1100                         dev_err(&hdev->pdev->dev, "knic setup failed %d\n",
1101                                 ret);
1102                         return ret;
1103                 }
1104         } else {
1105                 hclge_unic_setup(vport, num_tqps);
1106         }
1107
1108         return 0;
1109 }
1110
1111 static int hclge_alloc_vport(struct hclge_dev *hdev)
1112 {
1113         struct pci_dev *pdev = hdev->pdev;
1114         struct hclge_vport *vport;
1115         u32 tqp_main_vport;
1116         u32 tqp_per_vport;
1117         int num_vport, i;
1118         int ret;
1119
1120         /* We need to alloc a vport for main NIC of PF */
1121         num_vport = hdev->num_vmdq_vport + hdev->num_req_vfs + 1;
1122
1123         if (hdev->num_tqps < num_vport) {
1124                 dev_err(&hdev->pdev->dev, "tqps(%d) is less than vports(%d)",
1125                         hdev->num_tqps, num_vport);
1126                 return -EINVAL;
1127         }
1128
1129         /* Alloc the same number of TQPs for every vport */
1130         tqp_per_vport = hdev->num_tqps / num_vport;
1131         tqp_main_vport = tqp_per_vport + hdev->num_tqps % num_vport;
1132
1133         vport = devm_kcalloc(&pdev->dev, num_vport, sizeof(struct hclge_vport),
1134                              GFP_KERNEL);
1135         if (!vport)
1136                 return -ENOMEM;
1137
1138         hdev->vport = vport;
1139         hdev->num_alloc_vport = num_vport;
1140
1141         if (IS_ENABLED(CONFIG_PCI_IOV))
1142                 hdev->num_alloc_vfs = hdev->num_req_vfs;
1143
1144         for (i = 0; i < num_vport; i++) {
1145                 vport->back = hdev;
1146                 vport->vport_id = i;
1147
1148                 if (i == 0)
1149                         ret = hclge_vport_setup(vport, tqp_main_vport);
1150                 else
1151                         ret = hclge_vport_setup(vport, tqp_per_vport);
1152                 if (ret) {
1153                         dev_err(&pdev->dev,
1154                                 "vport setup failed for vport %d, %d\n",
1155                                 i, ret);
1156                         return ret;
1157                 }
1158
1159                 vport++;
1160         }
1161
1162         return 0;
1163 }
1164
1165 static int  hclge_cmd_alloc_tx_buff(struct hclge_dev *hdev,
1166                                     struct hclge_pkt_buf_alloc *buf_alloc)
1167 {
1168 /* TX buffer size is unit by 128 byte */
1169 #define HCLGE_BUF_SIZE_UNIT_SHIFT       7
1170 #define HCLGE_BUF_SIZE_UPDATE_EN_MSK    BIT(15)
1171         struct hclge_tx_buff_alloc_cmd *req;
1172         struct hclge_desc desc;
1173         int ret;
1174         u8 i;
1175
1176         req = (struct hclge_tx_buff_alloc_cmd *)desc.data;
1177
1178         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TX_BUFF_ALLOC, 0);
1179         for (i = 0; i < HCLGE_TC_NUM; i++) {
1180                 u32 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
1181
1182                 req->tx_pkt_buff[i] =
1183                         cpu_to_le16((buf_size >> HCLGE_BUF_SIZE_UNIT_SHIFT) |
1184                                      HCLGE_BUF_SIZE_UPDATE_EN_MSK);
1185         }
1186
1187         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1188         if (ret)
1189                 dev_err(&hdev->pdev->dev, "tx buffer alloc cmd failed %d.\n",
1190                         ret);
1191
1192         return ret;
1193 }
1194
1195 static int hclge_tx_buffer_alloc(struct hclge_dev *hdev,
1196                                  struct hclge_pkt_buf_alloc *buf_alloc)
1197 {
1198         int ret = hclge_cmd_alloc_tx_buff(hdev, buf_alloc);
1199
1200         if (ret)
1201                 dev_err(&hdev->pdev->dev, "tx buffer alloc failed %d\n", ret);
1202
1203         return ret;
1204 }
1205
1206 static int hclge_get_tc_num(struct hclge_dev *hdev)
1207 {
1208         int i, cnt = 0;
1209
1210         for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1211                 if (hdev->hw_tc_map & BIT(i))
1212                         cnt++;
1213         return cnt;
1214 }
1215
1216 static int hclge_get_pfc_enalbe_num(struct hclge_dev *hdev)
1217 {
1218         int i, cnt = 0;
1219
1220         for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1221                 if (hdev->hw_tc_map & BIT(i) &&
1222                     hdev->tm_info.hw_pfc_map & BIT(i))
1223                         cnt++;
1224         return cnt;
1225 }
1226
1227 /* Get the number of pfc enabled TCs, which have private buffer */
1228 static int hclge_get_pfc_priv_num(struct hclge_dev *hdev,
1229                                   struct hclge_pkt_buf_alloc *buf_alloc)
1230 {
1231         struct hclge_priv_buf *priv;
1232         int i, cnt = 0;
1233
1234         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1235                 priv = &buf_alloc->priv_buf[i];
1236                 if ((hdev->tm_info.hw_pfc_map & BIT(i)) &&
1237                     priv->enable)
1238                         cnt++;
1239         }
1240
1241         return cnt;
1242 }
1243
1244 /* Get the number of pfc disabled TCs, which have private buffer */
1245 static int hclge_get_no_pfc_priv_num(struct hclge_dev *hdev,
1246                                      struct hclge_pkt_buf_alloc *buf_alloc)
1247 {
1248         struct hclge_priv_buf *priv;
1249         int i, cnt = 0;
1250
1251         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1252                 priv = &buf_alloc->priv_buf[i];
1253                 if (hdev->hw_tc_map & BIT(i) &&
1254                     !(hdev->tm_info.hw_pfc_map & BIT(i)) &&
1255                     priv->enable)
1256                         cnt++;
1257         }
1258
1259         return cnt;
1260 }
1261
1262 static u32 hclge_get_rx_priv_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1263 {
1264         struct hclge_priv_buf *priv;
1265         u32 rx_priv = 0;
1266         int i;
1267
1268         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1269                 priv = &buf_alloc->priv_buf[i];
1270                 if (priv->enable)
1271                         rx_priv += priv->buf_size;
1272         }
1273         return rx_priv;
1274 }
1275
1276 static u32 hclge_get_tx_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1277 {
1278         u32 i, total_tx_size = 0;
1279
1280         for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1281                 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
1282
1283         return total_tx_size;
1284 }
1285
1286 static bool  hclge_is_rx_buf_ok(struct hclge_dev *hdev,
1287                                 struct hclge_pkt_buf_alloc *buf_alloc,
1288                                 u32 rx_all)
1289 {
1290         u32 shared_buf_min, shared_buf_tc, shared_std;
1291         int tc_num, pfc_enable_num;
1292         u32 shared_buf;
1293         u32 rx_priv;
1294         int i;
1295
1296         tc_num = hclge_get_tc_num(hdev);
1297         pfc_enable_num = hclge_get_pfc_enalbe_num(hdev);
1298
1299         if (hnae3_dev_dcb_supported(hdev))
1300                 shared_buf_min = 2 * hdev->mps + HCLGE_DEFAULT_DV;
1301         else
1302                 shared_buf_min = 2 * hdev->mps + HCLGE_DEFAULT_NON_DCB_DV;
1303
1304         shared_buf_tc = pfc_enable_num * hdev->mps +
1305                         (tc_num - pfc_enable_num) * hdev->mps / 2 +
1306                         hdev->mps;
1307         shared_std = max_t(u32, shared_buf_min, shared_buf_tc);
1308
1309         rx_priv = hclge_get_rx_priv_buff_alloced(buf_alloc);
1310         if (rx_all <= rx_priv + shared_std)
1311                 return false;
1312
1313         shared_buf = rx_all - rx_priv;
1314         buf_alloc->s_buf.buf_size = shared_buf;
1315         buf_alloc->s_buf.self.high = shared_buf;
1316         buf_alloc->s_buf.self.low =  2 * hdev->mps;
1317
1318         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1319                 if ((hdev->hw_tc_map & BIT(i)) &&
1320                     (hdev->tm_info.hw_pfc_map & BIT(i))) {
1321                         buf_alloc->s_buf.tc_thrd[i].low = hdev->mps;
1322                         buf_alloc->s_buf.tc_thrd[i].high = 2 * hdev->mps;
1323                 } else {
1324                         buf_alloc->s_buf.tc_thrd[i].low = 0;
1325                         buf_alloc->s_buf.tc_thrd[i].high = hdev->mps;
1326                 }
1327         }
1328
1329         return true;
1330 }
1331
1332 static int hclge_tx_buffer_calc(struct hclge_dev *hdev,
1333                                 struct hclge_pkt_buf_alloc *buf_alloc)
1334 {
1335         u32 i, total_size;
1336
1337         total_size = hdev->pkt_buf_size;
1338
1339         /* alloc tx buffer for all enabled tc */
1340         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1341                 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
1342
1343                 if (total_size < HCLGE_DEFAULT_TX_BUF)
1344                         return -ENOMEM;
1345
1346                 if (hdev->hw_tc_map & BIT(i))
1347                         priv->tx_buf_size = HCLGE_DEFAULT_TX_BUF;
1348                 else
1349                         priv->tx_buf_size = 0;
1350
1351                 total_size -= priv->tx_buf_size;
1352         }
1353
1354         return 0;
1355 }
1356
1357 /* hclge_rx_buffer_calc: calculate the rx private buffer size for all TCs
1358  * @hdev: pointer to struct hclge_dev
1359  * @buf_alloc: pointer to buffer calculation data
1360  * @return: 0: calculate sucessful, negative: fail
1361  */
1362 static int hclge_rx_buffer_calc(struct hclge_dev *hdev,
1363                                 struct hclge_pkt_buf_alloc *buf_alloc)
1364 {
1365 #define HCLGE_BUF_SIZE_UNIT     128
1366         u32 rx_all = hdev->pkt_buf_size, aligned_mps;
1367         int no_pfc_priv_num, pfc_priv_num;
1368         struct hclge_priv_buf *priv;
1369         int i;
1370
1371         aligned_mps = round_up(hdev->mps, HCLGE_BUF_SIZE_UNIT);
1372         rx_all -= hclge_get_tx_buff_alloced(buf_alloc);
1373
1374         /* When DCB is not supported, rx private
1375          * buffer is not allocated.
1376          */
1377         if (!hnae3_dev_dcb_supported(hdev)) {
1378                 if (!hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
1379                         return -ENOMEM;
1380
1381                 return 0;
1382         }
1383
1384         /* step 1, try to alloc private buffer for all enabled tc */
1385         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1386                 priv = &buf_alloc->priv_buf[i];
1387                 if (hdev->hw_tc_map & BIT(i)) {
1388                         priv->enable = 1;
1389                         if (hdev->tm_info.hw_pfc_map & BIT(i)) {
1390                                 priv->wl.low = aligned_mps;
1391                                 priv->wl.high = priv->wl.low + aligned_mps;
1392                                 priv->buf_size = priv->wl.high +
1393                                                 HCLGE_DEFAULT_DV;
1394                         } else {
1395                                 priv->wl.low = 0;
1396                                 priv->wl.high = 2 * aligned_mps;
1397                                 priv->buf_size = priv->wl.high;
1398                         }
1399                 } else {
1400                         priv->enable = 0;
1401                         priv->wl.low = 0;
1402                         priv->wl.high = 0;
1403                         priv->buf_size = 0;
1404                 }
1405         }
1406
1407         if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
1408                 return 0;
1409
1410         /* step 2, try to decrease the buffer size of
1411          * no pfc TC's private buffer
1412          */
1413         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1414                 priv = &buf_alloc->priv_buf[i];
1415
1416                 priv->enable = 0;
1417                 priv->wl.low = 0;
1418                 priv->wl.high = 0;
1419                 priv->buf_size = 0;
1420
1421                 if (!(hdev->hw_tc_map & BIT(i)))
1422                         continue;
1423
1424                 priv->enable = 1;
1425
1426                 if (hdev->tm_info.hw_pfc_map & BIT(i)) {
1427                         priv->wl.low = 128;
1428                         priv->wl.high = priv->wl.low + aligned_mps;
1429                         priv->buf_size = priv->wl.high + HCLGE_DEFAULT_DV;
1430                 } else {
1431                         priv->wl.low = 0;
1432                         priv->wl.high = aligned_mps;
1433                         priv->buf_size = priv->wl.high;
1434                 }
1435         }
1436
1437         if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
1438                 return 0;
1439
1440         /* step 3, try to reduce the number of pfc disabled TCs,
1441          * which have private buffer
1442          */
1443         /* get the total no pfc enable TC number, which have private buffer */
1444         no_pfc_priv_num = hclge_get_no_pfc_priv_num(hdev, buf_alloc);
1445
1446         /* let the last to be cleared first */
1447         for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
1448                 priv = &buf_alloc->priv_buf[i];
1449
1450                 if (hdev->hw_tc_map & BIT(i) &&
1451                     !(hdev->tm_info.hw_pfc_map & BIT(i))) {
1452                         /* Clear the no pfc TC private buffer */
1453                         priv->wl.low = 0;
1454                         priv->wl.high = 0;
1455                         priv->buf_size = 0;
1456                         priv->enable = 0;
1457                         no_pfc_priv_num--;
1458                 }
1459
1460                 if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
1461                     no_pfc_priv_num == 0)
1462                         break;
1463         }
1464
1465         if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
1466                 return 0;
1467
1468         /* step 4, try to reduce the number of pfc enabled TCs
1469          * which have private buffer.
1470          */
1471         pfc_priv_num = hclge_get_pfc_priv_num(hdev, buf_alloc);
1472
1473         /* let the last to be cleared first */
1474         for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
1475                 priv = &buf_alloc->priv_buf[i];
1476
1477                 if (hdev->hw_tc_map & BIT(i) &&
1478                     hdev->tm_info.hw_pfc_map & BIT(i)) {
1479                         /* Reduce the number of pfc TC with private buffer */
1480                         priv->wl.low = 0;
1481                         priv->enable = 0;
1482                         priv->wl.high = 0;
1483                         priv->buf_size = 0;
1484                         pfc_priv_num--;
1485                 }
1486
1487                 if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
1488                     pfc_priv_num == 0)
1489                         break;
1490         }
1491         if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
1492                 return 0;
1493
1494         return -ENOMEM;
1495 }
1496
1497 static int hclge_rx_priv_buf_alloc(struct hclge_dev *hdev,
1498                                    struct hclge_pkt_buf_alloc *buf_alloc)
1499 {
1500         struct hclge_rx_priv_buff_cmd *req;
1501         struct hclge_desc desc;
1502         int ret;
1503         int i;
1504
1505         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_PRIV_BUFF_ALLOC, false);
1506         req = (struct hclge_rx_priv_buff_cmd *)desc.data;
1507
1508         /* Alloc private buffer TCs */
1509         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1510                 struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
1511
1512                 req->buf_num[i] =
1513                         cpu_to_le16(priv->buf_size >> HCLGE_BUF_UNIT_S);
1514                 req->buf_num[i] |=
1515                         cpu_to_le16(1 << HCLGE_TC0_PRI_BUF_EN_B);
1516         }
1517
1518         req->shared_buf =
1519                 cpu_to_le16((buf_alloc->s_buf.buf_size >> HCLGE_BUF_UNIT_S) |
1520                             (1 << HCLGE_TC0_PRI_BUF_EN_B));
1521
1522         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1523         if (ret)
1524                 dev_err(&hdev->pdev->dev,
1525                         "rx private buffer alloc cmd failed %d\n", ret);
1526
1527         return ret;
1528 }
1529
1530 static int hclge_rx_priv_wl_config(struct hclge_dev *hdev,
1531                                    struct hclge_pkt_buf_alloc *buf_alloc)
1532 {
1533         struct hclge_rx_priv_wl_buf *req;
1534         struct hclge_priv_buf *priv;
1535         struct hclge_desc desc[2];
1536         int i, j;
1537         int ret;
1538
1539         for (i = 0; i < 2; i++) {
1540                 hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_RX_PRIV_WL_ALLOC,
1541                                            false);
1542                 req = (struct hclge_rx_priv_wl_buf *)desc[i].data;
1543
1544                 /* The first descriptor set the NEXT bit to 1 */
1545                 if (i == 0)
1546                         desc[i].flag |= cpu_to_le16(HCLGE_CMD_FLAG_NEXT);
1547                 else
1548                         desc[i].flag &= ~cpu_to_le16(HCLGE_CMD_FLAG_NEXT);
1549
1550                 for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
1551                         u32 idx = i * HCLGE_TC_NUM_ONE_DESC + j;
1552
1553                         priv = &buf_alloc->priv_buf[idx];
1554                         req->tc_wl[j].high =
1555                                 cpu_to_le16(priv->wl.high >> HCLGE_BUF_UNIT_S);
1556                         req->tc_wl[j].high |=
1557                                 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1558                         req->tc_wl[j].low =
1559                                 cpu_to_le16(priv->wl.low >> HCLGE_BUF_UNIT_S);
1560                         req->tc_wl[j].low |=
1561                                  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1562                 }
1563         }
1564
1565         /* Send 2 descriptor at one time */
1566         ret = hclge_cmd_send(&hdev->hw, desc, 2);
1567         if (ret)
1568                 dev_err(&hdev->pdev->dev,
1569                         "rx private waterline config cmd failed %d\n",
1570                         ret);
1571         return ret;
1572 }
1573
1574 static int hclge_common_thrd_config(struct hclge_dev *hdev,
1575                                     struct hclge_pkt_buf_alloc *buf_alloc)
1576 {
1577         struct hclge_shared_buf *s_buf = &buf_alloc->s_buf;
1578         struct hclge_rx_com_thrd *req;
1579         struct hclge_desc desc[2];
1580         struct hclge_tc_thrd *tc;
1581         int i, j;
1582         int ret;
1583
1584         for (i = 0; i < 2; i++) {
1585                 hclge_cmd_setup_basic_desc(&desc[i],
1586                                            HCLGE_OPC_RX_COM_THRD_ALLOC, false);
1587                 req = (struct hclge_rx_com_thrd *)&desc[i].data;
1588
1589                 /* The first descriptor set the NEXT bit to 1 */
1590                 if (i == 0)
1591                         desc[i].flag |= cpu_to_le16(HCLGE_CMD_FLAG_NEXT);
1592                 else
1593                         desc[i].flag &= ~cpu_to_le16(HCLGE_CMD_FLAG_NEXT);
1594
1595                 for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
1596                         tc = &s_buf->tc_thrd[i * HCLGE_TC_NUM_ONE_DESC + j];
1597
1598                         req->com_thrd[j].high =
1599                                 cpu_to_le16(tc->high >> HCLGE_BUF_UNIT_S);
1600                         req->com_thrd[j].high |=
1601                                  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1602                         req->com_thrd[j].low =
1603                                 cpu_to_le16(tc->low >> HCLGE_BUF_UNIT_S);
1604                         req->com_thrd[j].low |=
1605                                  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1606                 }
1607         }
1608
1609         /* Send 2 descriptors at one time */
1610         ret = hclge_cmd_send(&hdev->hw, desc, 2);
1611         if (ret)
1612                 dev_err(&hdev->pdev->dev,
1613                         "common threshold config cmd failed %d\n", ret);
1614         return ret;
1615 }
1616
1617 static int hclge_common_wl_config(struct hclge_dev *hdev,
1618                                   struct hclge_pkt_buf_alloc *buf_alloc)
1619 {
1620         struct hclge_shared_buf *buf = &buf_alloc->s_buf;
1621         struct hclge_rx_com_wl *req;
1622         struct hclge_desc desc;
1623         int ret;
1624
1625         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_COM_WL_ALLOC, false);
1626
1627         req = (struct hclge_rx_com_wl *)desc.data;
1628         req->com_wl.high = cpu_to_le16(buf->self.high >> HCLGE_BUF_UNIT_S);
1629         req->com_wl.high |=  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1630
1631         req->com_wl.low = cpu_to_le16(buf->self.low >> HCLGE_BUF_UNIT_S);
1632         req->com_wl.low |=  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
1633
1634         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1635         if (ret)
1636                 dev_err(&hdev->pdev->dev,
1637                         "common waterline config cmd failed %d\n", ret);
1638
1639         return ret;
1640 }
1641
1642 int hclge_buffer_alloc(struct hclge_dev *hdev)
1643 {
1644         struct hclge_pkt_buf_alloc *pkt_buf;
1645         int ret;
1646
1647         pkt_buf = kzalloc(sizeof(*pkt_buf), GFP_KERNEL);
1648         if (!pkt_buf)
1649                 return -ENOMEM;
1650
1651         ret = hclge_tx_buffer_calc(hdev, pkt_buf);
1652         if (ret) {
1653                 dev_err(&hdev->pdev->dev,
1654                         "could not calc tx buffer size for all TCs %d\n", ret);
1655                 goto out;
1656         }
1657
1658         ret = hclge_tx_buffer_alloc(hdev, pkt_buf);
1659         if (ret) {
1660                 dev_err(&hdev->pdev->dev,
1661                         "could not alloc tx buffers %d\n", ret);
1662                 goto out;
1663         }
1664
1665         ret = hclge_rx_buffer_calc(hdev, pkt_buf);
1666         if (ret) {
1667                 dev_err(&hdev->pdev->dev,
1668                         "could not calc rx priv buffer size for all TCs %d\n",
1669                         ret);
1670                 goto out;
1671         }
1672
1673         ret = hclge_rx_priv_buf_alloc(hdev, pkt_buf);
1674         if (ret) {
1675                 dev_err(&hdev->pdev->dev, "could not alloc rx priv buffer %d\n",
1676                         ret);
1677                 goto out;
1678         }
1679
1680         if (hnae3_dev_dcb_supported(hdev)) {
1681                 ret = hclge_rx_priv_wl_config(hdev, pkt_buf);
1682                 if (ret) {
1683                         dev_err(&hdev->pdev->dev,
1684                                 "could not configure rx private waterline %d\n",
1685                                 ret);
1686                         goto out;
1687                 }
1688
1689                 ret = hclge_common_thrd_config(hdev, pkt_buf);
1690                 if (ret) {
1691                         dev_err(&hdev->pdev->dev,
1692                                 "could not configure common threshold %d\n",
1693                                 ret);
1694                         goto out;
1695                 }
1696         }
1697
1698         ret = hclge_common_wl_config(hdev, pkt_buf);
1699         if (ret)
1700                 dev_err(&hdev->pdev->dev,
1701                         "could not configure common waterline %d\n", ret);
1702
1703 out:
1704         kfree(pkt_buf);
1705         return ret;
1706 }
1707
1708 static int hclge_init_roce_base_info(struct hclge_vport *vport)
1709 {
1710         struct hnae3_handle *roce = &vport->roce;
1711         struct hnae3_handle *nic = &vport->nic;
1712
1713         roce->rinfo.num_vectors = vport->back->num_roce_msi;
1714
1715         if (vport->back->num_msi_left < vport->roce.rinfo.num_vectors ||
1716             vport->back->num_msi_left == 0)
1717                 return -EINVAL;
1718
1719         roce->rinfo.base_vector = vport->back->roce_base_vector;
1720
1721         roce->rinfo.netdev = nic->kinfo.netdev;
1722         roce->rinfo.roce_io_base = vport->back->hw.io_base;
1723
1724         roce->pdev = nic->pdev;
1725         roce->ae_algo = nic->ae_algo;
1726         roce->numa_node_mask = nic->numa_node_mask;
1727
1728         return 0;
1729 }
1730
1731 static int hclge_init_msi(struct hclge_dev *hdev)
1732 {
1733         struct pci_dev *pdev = hdev->pdev;
1734         int vectors;
1735         int i;
1736
1737         vectors = pci_alloc_irq_vectors(pdev, 1, hdev->num_msi,
1738                                         PCI_IRQ_MSI | PCI_IRQ_MSIX);
1739         if (vectors < 0) {
1740                 dev_err(&pdev->dev,
1741                         "failed(%d) to allocate MSI/MSI-X vectors\n",
1742                         vectors);
1743                 return vectors;
1744         }
1745         if (vectors < hdev->num_msi)
1746                 dev_warn(&hdev->pdev->dev,
1747                          "requested %d MSI/MSI-X, but allocated %d MSI/MSI-X\n",
1748                          hdev->num_msi, vectors);
1749
1750         hdev->num_msi = vectors;
1751         hdev->num_msi_left = vectors;
1752         hdev->base_msi_vector = pdev->irq;
1753         hdev->roce_base_vector = hdev->base_msi_vector +
1754                                 hdev->roce_base_msix_offset;
1755
1756         hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
1757                                            sizeof(u16), GFP_KERNEL);
1758         if (!hdev->vector_status) {
1759                 pci_free_irq_vectors(pdev);
1760                 return -ENOMEM;
1761         }
1762
1763         for (i = 0; i < hdev->num_msi; i++)
1764                 hdev->vector_status[i] = HCLGE_INVALID_VPORT;
1765
1766         hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
1767                                         sizeof(int), GFP_KERNEL);
1768         if (!hdev->vector_irq) {
1769                 pci_free_irq_vectors(pdev);
1770                 return -ENOMEM;
1771         }
1772
1773         return 0;
1774 }
1775
1776 static u8 hclge_check_speed_dup(u8 duplex, int speed)
1777 {
1778
1779         if (!(speed == HCLGE_MAC_SPEED_10M || speed == HCLGE_MAC_SPEED_100M))
1780                 duplex = HCLGE_MAC_FULL;
1781
1782         return duplex;
1783 }
1784
1785 static int hclge_cfg_mac_speed_dup_hw(struct hclge_dev *hdev, int speed,
1786                                       u8 duplex)
1787 {
1788         struct hclge_config_mac_speed_dup_cmd *req;
1789         struct hclge_desc desc;
1790         int ret;
1791
1792         req = (struct hclge_config_mac_speed_dup_cmd *)desc.data;
1793
1794         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_SPEED_DUP, false);
1795
1796         hnae3_set_bit(req->speed_dup, HCLGE_CFG_DUPLEX_B, !!duplex);
1797
1798         switch (speed) {
1799         case HCLGE_MAC_SPEED_10M:
1800                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1801                                 HCLGE_CFG_SPEED_S, 6);
1802                 break;
1803         case HCLGE_MAC_SPEED_100M:
1804                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1805                                 HCLGE_CFG_SPEED_S, 7);
1806                 break;
1807         case HCLGE_MAC_SPEED_1G:
1808                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1809                                 HCLGE_CFG_SPEED_S, 0);
1810                 break;
1811         case HCLGE_MAC_SPEED_10G:
1812                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1813                                 HCLGE_CFG_SPEED_S, 1);
1814                 break;
1815         case HCLGE_MAC_SPEED_25G:
1816                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1817                                 HCLGE_CFG_SPEED_S, 2);
1818                 break;
1819         case HCLGE_MAC_SPEED_40G:
1820                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1821                                 HCLGE_CFG_SPEED_S, 3);
1822                 break;
1823         case HCLGE_MAC_SPEED_50G:
1824                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1825                                 HCLGE_CFG_SPEED_S, 4);
1826                 break;
1827         case HCLGE_MAC_SPEED_100G:
1828                 hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M,
1829                                 HCLGE_CFG_SPEED_S, 5);
1830                 break;
1831         default:
1832                 dev_err(&hdev->pdev->dev, "invalid speed (%d)\n", speed);
1833                 return -EINVAL;
1834         }
1835
1836         hnae3_set_bit(req->mac_change_fec_en, HCLGE_CFG_MAC_SPEED_CHANGE_EN_B,
1837                       1);
1838
1839         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1840         if (ret) {
1841                 dev_err(&hdev->pdev->dev,
1842                         "mac speed/duplex config cmd failed %d.\n", ret);
1843                 return ret;
1844         }
1845
1846         return 0;
1847 }
1848
1849 int hclge_cfg_mac_speed_dup(struct hclge_dev *hdev, int speed, u8 duplex)
1850 {
1851         int ret;
1852
1853         duplex = hclge_check_speed_dup(duplex, speed);
1854         if (hdev->hw.mac.speed == speed && hdev->hw.mac.duplex == duplex)
1855                 return 0;
1856
1857         ret = hclge_cfg_mac_speed_dup_hw(hdev, speed, duplex);
1858         if (ret)
1859                 return ret;
1860
1861         hdev->hw.mac.speed = speed;
1862         hdev->hw.mac.duplex = duplex;
1863
1864         return 0;
1865 }
1866
1867 static int hclge_cfg_mac_speed_dup_h(struct hnae3_handle *handle, int speed,
1868                                      u8 duplex)
1869 {
1870         struct hclge_vport *vport = hclge_get_vport(handle);
1871         struct hclge_dev *hdev = vport->back;
1872
1873         return hclge_cfg_mac_speed_dup(hdev, speed, duplex);
1874 }
1875
1876 static int hclge_query_mac_an_speed_dup(struct hclge_dev *hdev, int *speed,
1877                                         u8 *duplex)
1878 {
1879         struct hclge_query_an_speed_dup_cmd *req;
1880         struct hclge_desc desc;
1881         int speed_tmp;
1882         int ret;
1883
1884         req = (struct hclge_query_an_speed_dup_cmd *)desc.data;
1885
1886         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_AN_RESULT, true);
1887         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1888         if (ret) {
1889                 dev_err(&hdev->pdev->dev,
1890                         "mac speed/autoneg/duplex query cmd failed %d\n",
1891                         ret);
1892                 return ret;
1893         }
1894
1895         *duplex = hnae3_get_bit(req->an_syn_dup_speed, HCLGE_QUERY_DUPLEX_B);
1896         speed_tmp = hnae3_get_field(req->an_syn_dup_speed, HCLGE_QUERY_SPEED_M,
1897                                     HCLGE_QUERY_SPEED_S);
1898
1899         ret = hclge_parse_speed(speed_tmp, speed);
1900         if (ret)
1901                 dev_err(&hdev->pdev->dev,
1902                         "could not parse speed(=%d), %d\n", speed_tmp, ret);
1903
1904         return ret;
1905 }
1906
1907 static int hclge_set_autoneg_en(struct hclge_dev *hdev, bool enable)
1908 {
1909         struct hclge_config_auto_neg_cmd *req;
1910         struct hclge_desc desc;
1911         u32 flag = 0;
1912         int ret;
1913
1914         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_AN_MODE, false);
1915
1916         req = (struct hclge_config_auto_neg_cmd *)desc.data;
1917         hnae3_set_bit(flag, HCLGE_MAC_CFG_AN_EN_B, !!enable);
1918         req->cfg_an_cmd_flag = cpu_to_le32(flag);
1919
1920         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1921         if (ret)
1922                 dev_err(&hdev->pdev->dev, "auto neg set cmd failed %d.\n",
1923                         ret);
1924
1925         return ret;
1926 }
1927
1928 static int hclge_set_autoneg(struct hnae3_handle *handle, bool enable)
1929 {
1930         struct hclge_vport *vport = hclge_get_vport(handle);
1931         struct hclge_dev *hdev = vport->back;
1932
1933         return hclge_set_autoneg_en(hdev, enable);
1934 }
1935
1936 static int hclge_get_autoneg(struct hnae3_handle *handle)
1937 {
1938         struct hclge_vport *vport = hclge_get_vport(handle);
1939         struct hclge_dev *hdev = vport->back;
1940         struct phy_device *phydev = hdev->hw.mac.phydev;
1941
1942         if (phydev)
1943                 return phydev->autoneg;
1944
1945         return hdev->hw.mac.autoneg;
1946 }
1947
1948 static int hclge_mac_init(struct hclge_dev *hdev)
1949 {
1950         struct hnae3_handle *handle = &hdev->vport[0].nic;
1951         struct net_device *netdev = handle->kinfo.netdev;
1952         struct hclge_mac *mac = &hdev->hw.mac;
1953         int mtu;
1954         int ret;
1955
1956         hdev->hw.mac.duplex = HCLGE_MAC_FULL;
1957         ret = hclge_cfg_mac_speed_dup_hw(hdev, hdev->hw.mac.speed,
1958                                          hdev->hw.mac.duplex);
1959         if (ret) {
1960                 dev_err(&hdev->pdev->dev,
1961                         "Config mac speed dup fail ret=%d\n", ret);
1962                 return ret;
1963         }
1964
1965         mac->link = 0;
1966
1967         if (netdev)
1968                 mtu = netdev->mtu;
1969         else
1970                 mtu = ETH_DATA_LEN;
1971
1972         ret = hclge_set_mtu(handle, mtu);
1973         if (ret)
1974                 dev_err(&hdev->pdev->dev,
1975                         "set mtu failed ret=%d\n", ret);
1976
1977         return ret;
1978 }
1979
1980 static void hclge_mbx_task_schedule(struct hclge_dev *hdev)
1981 {
1982         if (!test_and_set_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state))
1983                 schedule_work(&hdev->mbx_service_task);
1984 }
1985
1986 static void hclge_reset_task_schedule(struct hclge_dev *hdev)
1987 {
1988         if (!test_and_set_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state))
1989                 schedule_work(&hdev->rst_service_task);
1990 }
1991
1992 static void hclge_task_schedule(struct hclge_dev *hdev)
1993 {
1994         if (!test_bit(HCLGE_STATE_DOWN, &hdev->state) &&
1995             !test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
1996             !test_and_set_bit(HCLGE_STATE_SERVICE_SCHED, &hdev->state))
1997                 (void)schedule_work(&hdev->service_task);
1998 }
1999
2000 static int hclge_get_mac_link_status(struct hclge_dev *hdev)
2001 {
2002         struct hclge_link_status_cmd *req;
2003         struct hclge_desc desc;
2004         int link_status;
2005         int ret;
2006
2007         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_LINK_STATUS, true);
2008         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2009         if (ret) {
2010                 dev_err(&hdev->pdev->dev, "get link status cmd failed %d\n",
2011                         ret);
2012                 return ret;
2013         }
2014
2015         req = (struct hclge_link_status_cmd *)desc.data;
2016         link_status = req->status & HCLGE_LINK_STATUS_UP_M;
2017
2018         return !!link_status;
2019 }
2020
2021 static int hclge_get_mac_phy_link(struct hclge_dev *hdev)
2022 {
2023         int mac_state;
2024         int link_stat;
2025
2026         if (test_bit(HCLGE_STATE_DOWN, &hdev->state))
2027                 return 0;
2028
2029         mac_state = hclge_get_mac_link_status(hdev);
2030
2031         if (hdev->hw.mac.phydev) {
2032                 if (hdev->hw.mac.phydev->state == PHY_RUNNING)
2033                         link_stat = mac_state &
2034                                 hdev->hw.mac.phydev->link;
2035                 else
2036                         link_stat = 0;
2037
2038         } else {
2039                 link_stat = mac_state;
2040         }
2041
2042         return !!link_stat;
2043 }
2044
2045 static void hclge_update_link_status(struct hclge_dev *hdev)
2046 {
2047         struct hnae3_client *client = hdev->nic_client;
2048         struct hnae3_handle *handle;
2049         int state;
2050         int i;
2051
2052         if (!client)
2053                 return;
2054         state = hclge_get_mac_phy_link(hdev);
2055         if (state != hdev->hw.mac.link) {
2056                 for (i = 0; i < hdev->num_vmdq_vport + 1; i++) {
2057                         handle = &hdev->vport[i].nic;
2058                         client->ops->link_status_change(handle, state);
2059                 }
2060                 hdev->hw.mac.link = state;
2061         }
2062 }
2063
2064 static int hclge_update_speed_duplex(struct hclge_dev *hdev)
2065 {
2066         struct hclge_mac mac = hdev->hw.mac;
2067         u8 duplex;
2068         int speed;
2069         int ret;
2070
2071         /* get the speed and duplex as autoneg'result from mac cmd when phy
2072          * doesn't exit.
2073          */
2074         if (mac.phydev || !mac.autoneg)
2075                 return 0;
2076
2077         ret = hclge_query_mac_an_speed_dup(hdev, &speed, &duplex);
2078         if (ret) {
2079                 dev_err(&hdev->pdev->dev,
2080                         "mac autoneg/speed/duplex query failed %d\n", ret);
2081                 return ret;
2082         }
2083
2084         ret = hclge_cfg_mac_speed_dup(hdev, speed, duplex);
2085         if (ret) {
2086                 dev_err(&hdev->pdev->dev,
2087                         "mac speed/duplex config failed %d\n", ret);
2088                 return ret;
2089         }
2090
2091         return 0;
2092 }
2093
2094 static int hclge_update_speed_duplex_h(struct hnae3_handle *handle)
2095 {
2096         struct hclge_vport *vport = hclge_get_vport(handle);
2097         struct hclge_dev *hdev = vport->back;
2098
2099         return hclge_update_speed_duplex(hdev);
2100 }
2101
2102 static int hclge_get_status(struct hnae3_handle *handle)
2103 {
2104         struct hclge_vport *vport = hclge_get_vport(handle);
2105         struct hclge_dev *hdev = vport->back;
2106
2107         hclge_update_link_status(hdev);
2108
2109         return hdev->hw.mac.link;
2110 }
2111
2112 static void hclge_service_timer(struct timer_list *t)
2113 {
2114         struct hclge_dev *hdev = from_timer(hdev, t, service_timer);
2115
2116         mod_timer(&hdev->service_timer, jiffies + HZ);
2117         hdev->hw_stats.stats_timer++;
2118         hclge_task_schedule(hdev);
2119 }
2120
2121 static void hclge_service_complete(struct hclge_dev *hdev)
2122 {
2123         WARN_ON(!test_bit(HCLGE_STATE_SERVICE_SCHED, &hdev->state));
2124
2125         /* Flush memory before next watchdog */
2126         smp_mb__before_atomic();
2127         clear_bit(HCLGE_STATE_SERVICE_SCHED, &hdev->state);
2128 }
2129
2130 static u32 hclge_check_event_cause(struct hclge_dev *hdev, u32 *clearval)
2131 {
2132         u32 rst_src_reg;
2133         u32 cmdq_src_reg;
2134
2135         /* fetch the events from their corresponding regs */
2136         rst_src_reg = hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS);
2137         cmdq_src_reg = hclge_read_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG);
2138
2139         /* Assumption: If by any chance reset and mailbox events are reported
2140          * together then we will only process reset event in this go and will
2141          * defer the processing of the mailbox events. Since, we would have not
2142          * cleared RX CMDQ event this time we would receive again another
2143          * interrupt from H/W just for the mailbox.
2144          */
2145
2146         /* check for vector0 reset event sources */
2147         if (BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) & rst_src_reg) {
2148                 set_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state);
2149                 set_bit(HNAE3_GLOBAL_RESET, &hdev->reset_pending);
2150                 *clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
2151                 return HCLGE_VECTOR0_EVENT_RST;
2152         }
2153
2154         if (BIT(HCLGE_VECTOR0_CORERESET_INT_B) & rst_src_reg) {
2155                 set_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state);
2156                 set_bit(HNAE3_CORE_RESET, &hdev->reset_pending);
2157                 *clearval = BIT(HCLGE_VECTOR0_CORERESET_INT_B);
2158                 return HCLGE_VECTOR0_EVENT_RST;
2159         }
2160
2161         if (BIT(HCLGE_VECTOR0_IMPRESET_INT_B) & rst_src_reg) {
2162                 set_bit(HNAE3_IMP_RESET, &hdev->reset_pending);
2163                 *clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
2164                 return HCLGE_VECTOR0_EVENT_RST;
2165         }
2166
2167         /* check for vector0 mailbox(=CMDQ RX) event source */
2168         if (BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_reg) {
2169                 cmdq_src_reg &= ~BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B);
2170                 *clearval = cmdq_src_reg;
2171                 return HCLGE_VECTOR0_EVENT_MBX;
2172         }
2173
2174         return HCLGE_VECTOR0_EVENT_OTHER;
2175 }
2176
2177 static void hclge_clear_event_cause(struct hclge_dev *hdev, u32 event_type,
2178                                     u32 regclr)
2179 {
2180         switch (event_type) {
2181         case HCLGE_VECTOR0_EVENT_RST:
2182                 hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG, regclr);
2183                 break;
2184         case HCLGE_VECTOR0_EVENT_MBX:
2185                 hclge_write_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG, regclr);
2186                 break;
2187         default:
2188                 break;
2189         }
2190 }
2191
2192 static void hclge_clear_all_event_cause(struct hclge_dev *hdev)
2193 {
2194         hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_RST,
2195                                 BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) |
2196                                 BIT(HCLGE_VECTOR0_CORERESET_INT_B) |
2197                                 BIT(HCLGE_VECTOR0_IMPRESET_INT_B));
2198         hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_MBX, 0);
2199 }
2200
2201 static void hclge_enable_vector(struct hclge_misc_vector *vector, bool enable)
2202 {
2203         writel(enable ? 1 : 0, vector->addr);
2204 }
2205
2206 static irqreturn_t hclge_misc_irq_handle(int irq, void *data)
2207 {
2208         struct hclge_dev *hdev = data;
2209         u32 event_cause;
2210         u32 clearval;
2211
2212         hclge_enable_vector(&hdev->misc_vector, false);
2213         event_cause = hclge_check_event_cause(hdev, &clearval);
2214
2215         /* vector 0 interrupt is shared with reset and mailbox source events.*/
2216         switch (event_cause) {
2217         case HCLGE_VECTOR0_EVENT_RST:
2218                 hclge_reset_task_schedule(hdev);
2219                 break;
2220         case HCLGE_VECTOR0_EVENT_MBX:
2221                 /* If we are here then,
2222                  * 1. Either we are not handling any mbx task and we are not
2223                  *    scheduled as well
2224                  *                        OR
2225                  * 2. We could be handling a mbx task but nothing more is
2226                  *    scheduled.
2227                  * In both cases, we should schedule mbx task as there are more
2228                  * mbx messages reported by this interrupt.
2229                  */
2230                 hclge_mbx_task_schedule(hdev);
2231                 break;
2232         default:
2233                 dev_warn(&hdev->pdev->dev,
2234                          "received unknown or unhandled event of vector0\n");
2235                 break;
2236         }
2237
2238         /* clear the source of interrupt if it is not cause by reset */
2239         if (event_cause == HCLGE_VECTOR0_EVENT_MBX) {
2240                 hclge_clear_event_cause(hdev, event_cause, clearval);
2241                 hclge_enable_vector(&hdev->misc_vector, true);
2242         }
2243
2244         return IRQ_HANDLED;
2245 }
2246
2247 static void hclge_free_vector(struct hclge_dev *hdev, int vector_id)
2248 {
2249         if (hdev->vector_status[vector_id] == HCLGE_INVALID_VPORT) {
2250                 dev_warn(&hdev->pdev->dev,
2251                          "vector(vector_id %d) has been freed.\n", vector_id);
2252                 return;
2253         }
2254
2255         hdev->vector_status[vector_id] = HCLGE_INVALID_VPORT;
2256         hdev->num_msi_left += 1;
2257         hdev->num_msi_used -= 1;
2258 }
2259
2260 static void hclge_get_misc_vector(struct hclge_dev *hdev)
2261 {
2262         struct hclge_misc_vector *vector = &hdev->misc_vector;
2263
2264         vector->vector_irq = pci_irq_vector(hdev->pdev, 0);
2265
2266         vector->addr = hdev->hw.io_base + HCLGE_MISC_VECTOR_REG_BASE;
2267         hdev->vector_status[0] = 0;
2268
2269         hdev->num_msi_left -= 1;
2270         hdev->num_msi_used += 1;
2271 }
2272
2273 static int hclge_misc_irq_init(struct hclge_dev *hdev)
2274 {
2275         int ret;
2276
2277         hclge_get_misc_vector(hdev);
2278
2279         /* this would be explicitly freed in the end */
2280         ret = request_irq(hdev->misc_vector.vector_irq, hclge_misc_irq_handle,
2281                           0, "hclge_misc", hdev);
2282         if (ret) {
2283                 hclge_free_vector(hdev, 0);
2284                 dev_err(&hdev->pdev->dev, "request misc irq(%d) fail\n",
2285                         hdev->misc_vector.vector_irq);
2286         }
2287
2288         return ret;
2289 }
2290
2291 static void hclge_misc_irq_uninit(struct hclge_dev *hdev)
2292 {
2293         free_irq(hdev->misc_vector.vector_irq, hdev);
2294         hclge_free_vector(hdev, 0);
2295 }
2296
2297 static int hclge_notify_client(struct hclge_dev *hdev,
2298                                enum hnae3_reset_notify_type type)
2299 {
2300         struct hnae3_client *client = hdev->nic_client;
2301         u16 i;
2302
2303         if (!client->ops->reset_notify)
2304                 return -EOPNOTSUPP;
2305
2306         for (i = 0; i < hdev->num_vmdq_vport + 1; i++) {
2307                 struct hnae3_handle *handle = &hdev->vport[i].nic;
2308                 int ret;
2309
2310                 ret = client->ops->reset_notify(handle, type);
2311                 if (ret)
2312                         return ret;
2313         }
2314
2315         return 0;
2316 }
2317
2318 static int hclge_reset_wait(struct hclge_dev *hdev)
2319 {
2320 #define HCLGE_RESET_WATI_MS     100
2321 #define HCLGE_RESET_WAIT_CNT    5
2322         u32 val, reg, reg_bit;
2323         u32 cnt = 0;
2324
2325         switch (hdev->reset_type) {
2326         case HNAE3_GLOBAL_RESET:
2327                 reg = HCLGE_GLOBAL_RESET_REG;
2328                 reg_bit = HCLGE_GLOBAL_RESET_BIT;
2329                 break;
2330         case HNAE3_CORE_RESET:
2331                 reg = HCLGE_GLOBAL_RESET_REG;
2332                 reg_bit = HCLGE_CORE_RESET_BIT;
2333                 break;
2334         case HNAE3_FUNC_RESET:
2335                 reg = HCLGE_FUN_RST_ING;
2336                 reg_bit = HCLGE_FUN_RST_ING_B;
2337                 break;
2338         default:
2339                 dev_err(&hdev->pdev->dev,
2340                         "Wait for unsupported reset type: %d\n",
2341                         hdev->reset_type);
2342                 return -EINVAL;
2343         }
2344
2345         val = hclge_read_dev(&hdev->hw, reg);
2346         while (hnae3_get_bit(val, reg_bit) && cnt < HCLGE_RESET_WAIT_CNT) {
2347                 msleep(HCLGE_RESET_WATI_MS);
2348                 val = hclge_read_dev(&hdev->hw, reg);
2349                 cnt++;
2350         }
2351
2352         if (cnt >= HCLGE_RESET_WAIT_CNT) {
2353                 dev_warn(&hdev->pdev->dev,
2354                          "Wait for reset timeout: %d\n", hdev->reset_type);
2355                 return -EBUSY;
2356         }
2357
2358         return 0;
2359 }
2360
2361 int hclge_func_reset_cmd(struct hclge_dev *hdev, int func_id)
2362 {
2363         struct hclge_desc desc;
2364         struct hclge_reset_cmd *req = (struct hclge_reset_cmd *)desc.data;
2365         int ret;
2366
2367         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_RST_TRIGGER, false);
2368         hnae3_set_bit(req->mac_func_reset, HCLGE_CFG_RESET_FUNC_B, 1);
2369         req->fun_reset_vfid = func_id;
2370
2371         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2372         if (ret)
2373                 dev_err(&hdev->pdev->dev,
2374                         "send function reset cmd fail, status =%d\n", ret);
2375
2376         return ret;
2377 }
2378
2379 static void hclge_do_reset(struct hclge_dev *hdev)
2380 {
2381         struct pci_dev *pdev = hdev->pdev;
2382         u32 val;
2383
2384         switch (hdev->reset_type) {
2385         case HNAE3_GLOBAL_RESET:
2386                 val = hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG);
2387                 hnae3_set_bit(val, HCLGE_GLOBAL_RESET_BIT, 1);
2388                 hclge_write_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG, val);
2389                 dev_info(&pdev->dev, "Global Reset requested\n");
2390                 break;
2391         case HNAE3_CORE_RESET:
2392                 val = hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG);
2393                 hnae3_set_bit(val, HCLGE_CORE_RESET_BIT, 1);
2394                 hclge_write_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG, val);
2395                 dev_info(&pdev->dev, "Core Reset requested\n");
2396                 break;
2397         case HNAE3_FUNC_RESET:
2398                 dev_info(&pdev->dev, "PF Reset requested\n");
2399                 hclge_func_reset_cmd(hdev, 0);
2400                 /* schedule again to check later */
2401                 set_bit(HNAE3_FUNC_RESET, &hdev->reset_pending);
2402                 hclge_reset_task_schedule(hdev);
2403                 break;
2404         default:
2405                 dev_warn(&pdev->dev,
2406                          "Unsupported reset type: %d\n", hdev->reset_type);
2407                 break;
2408         }
2409 }
2410
2411 static enum hnae3_reset_type hclge_get_reset_level(struct hclge_dev *hdev,
2412                                                    unsigned long *addr)
2413 {
2414         enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
2415
2416         /* return the highest priority reset level amongst all */
2417         if (test_bit(HNAE3_GLOBAL_RESET, addr))
2418                 rst_level = HNAE3_GLOBAL_RESET;
2419         else if (test_bit(HNAE3_CORE_RESET, addr))
2420                 rst_level = HNAE3_CORE_RESET;
2421         else if (test_bit(HNAE3_IMP_RESET, addr))
2422                 rst_level = HNAE3_IMP_RESET;
2423         else if (test_bit(HNAE3_FUNC_RESET, addr))
2424                 rst_level = HNAE3_FUNC_RESET;
2425
2426         /* now, clear all other resets */
2427         clear_bit(HNAE3_GLOBAL_RESET, addr);
2428         clear_bit(HNAE3_CORE_RESET, addr);
2429         clear_bit(HNAE3_IMP_RESET, addr);
2430         clear_bit(HNAE3_FUNC_RESET, addr);
2431
2432         return rst_level;
2433 }
2434
2435 static void hclge_clear_reset_cause(struct hclge_dev *hdev)
2436 {
2437         u32 clearval = 0;
2438
2439         switch (hdev->reset_type) {
2440         case HNAE3_IMP_RESET:
2441                 clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
2442                 break;
2443         case HNAE3_GLOBAL_RESET:
2444                 clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
2445                 break;
2446         case HNAE3_CORE_RESET:
2447                 clearval = BIT(HCLGE_VECTOR0_CORERESET_INT_B);
2448                 break;
2449         default:
2450                 break;
2451         }
2452
2453         if (!clearval)
2454                 return;
2455
2456         hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG, clearval);
2457         hclge_enable_vector(&hdev->misc_vector, true);
2458 }
2459
2460 static void hclge_reset(struct hclge_dev *hdev)
2461 {
2462         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
2463         struct hnae3_handle *handle;
2464
2465         /* Initialize ae_dev reset status as well, in case enet layer wants to
2466          * know if device is undergoing reset
2467          */
2468         ae_dev->reset_type = hdev->reset_type;
2469         /* perform reset of the stack & ae device for a client */
2470         handle = &hdev->vport[0].nic;
2471         rtnl_lock();
2472         hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
2473         rtnl_unlock();
2474
2475         if (!hclge_reset_wait(hdev)) {
2476                 rtnl_lock();
2477                 hclge_notify_client(hdev, HNAE3_UNINIT_CLIENT);
2478                 hclge_reset_ae_dev(hdev->ae_dev);
2479                 hclge_notify_client(hdev, HNAE3_INIT_CLIENT);
2480
2481                 hclge_clear_reset_cause(hdev);
2482         } else {
2483                 rtnl_lock();
2484                 /* schedule again to check pending resets later */
2485                 set_bit(hdev->reset_type, &hdev->reset_pending);
2486                 hclge_reset_task_schedule(hdev);
2487         }
2488
2489         hclge_notify_client(hdev, HNAE3_UP_CLIENT);
2490         handle->last_reset_time = jiffies;
2491         rtnl_unlock();
2492         ae_dev->reset_type = HNAE3_NONE_RESET;
2493 }
2494
2495 static void hclge_reset_event(struct pci_dev *pdev, struct hnae3_handle *handle)
2496 {
2497         struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
2498         struct hclge_dev *hdev = ae_dev->priv;
2499
2500         /* We might end up getting called broadly because of 2 below cases:
2501          * 1. Recoverable error was conveyed through APEI and only way to bring
2502          *    normalcy is to reset.
2503          * 2. A new reset request from the stack due to timeout
2504          *
2505          * For the first case,error event might not have ae handle available.
2506          * check if this is a new reset request and we are not here just because
2507          * last reset attempt did not succeed and watchdog hit us again. We will
2508          * know this if last reset request did not occur very recently (watchdog
2509          * timer = 5*HZ, let us check after sufficiently large time, say 4*5*Hz)
2510          * In case of new request we reset the "reset level" to PF reset.
2511          * And if it is a repeat reset request of the most recent one then we
2512          * want to make sure we throttle the reset request. Therefore, we will
2513          * not allow it again before 3*HZ times.
2514          */
2515         if (!handle)
2516                 handle = &hdev->vport[0].nic;
2517
2518         if (time_before(jiffies, (handle->last_reset_time + 3 * HZ)))
2519                 return;
2520         else if (time_after(jiffies, (handle->last_reset_time + 4 * 5 * HZ)))
2521                 handle->reset_level = HNAE3_FUNC_RESET;
2522
2523         dev_info(&hdev->pdev->dev, "received reset event , reset type is %d",
2524                  handle->reset_level);
2525
2526         /* request reset & schedule reset task */
2527         set_bit(handle->reset_level, &hdev->reset_request);
2528         hclge_reset_task_schedule(hdev);
2529
2530         if (handle->reset_level < HNAE3_GLOBAL_RESET)
2531                 handle->reset_level++;
2532 }
2533
2534 static void hclge_reset_subtask(struct hclge_dev *hdev)
2535 {
2536         /* check if there is any ongoing reset in the hardware. This status can
2537          * be checked from reset_pending. If there is then, we need to wait for
2538          * hardware to complete reset.
2539          *    a. If we are able to figure out in reasonable time that hardware
2540          *       has fully resetted then, we can proceed with driver, client
2541          *       reset.
2542          *    b. else, we can come back later to check this status so re-sched
2543          *       now.
2544          */
2545         hdev->reset_type = hclge_get_reset_level(hdev, &hdev->reset_pending);
2546         if (hdev->reset_type != HNAE3_NONE_RESET)
2547                 hclge_reset(hdev);
2548
2549         /* check if we got any *new* reset requests to be honored */
2550         hdev->reset_type = hclge_get_reset_level(hdev, &hdev->reset_request);
2551         if (hdev->reset_type != HNAE3_NONE_RESET)
2552                 hclge_do_reset(hdev);
2553
2554         hdev->reset_type = HNAE3_NONE_RESET;
2555 }
2556
2557 static void hclge_reset_service_task(struct work_struct *work)
2558 {
2559         struct hclge_dev *hdev =
2560                 container_of(work, struct hclge_dev, rst_service_task);
2561
2562         if (test_and_set_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
2563                 return;
2564
2565         clear_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state);
2566
2567         hclge_reset_subtask(hdev);
2568
2569         clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
2570 }
2571
2572 static void hclge_mailbox_service_task(struct work_struct *work)
2573 {
2574         struct hclge_dev *hdev =
2575                 container_of(work, struct hclge_dev, mbx_service_task);
2576
2577         if (test_and_set_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state))
2578                 return;
2579
2580         clear_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state);
2581
2582         hclge_mbx_handler(hdev);
2583
2584         clear_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state);
2585 }
2586
2587 static void hclge_service_task(struct work_struct *work)
2588 {
2589         struct hclge_dev *hdev =
2590                 container_of(work, struct hclge_dev, service_task);
2591
2592         if (hdev->hw_stats.stats_timer >= HCLGE_STATS_TIMER_INTERVAL) {
2593                 hclge_update_stats_for_all(hdev);
2594                 hdev->hw_stats.stats_timer = 0;
2595         }
2596
2597         hclge_update_speed_duplex(hdev);
2598         hclge_update_link_status(hdev);
2599         hclge_service_complete(hdev);
2600 }
2601
2602 struct hclge_vport *hclge_get_vport(struct hnae3_handle *handle)
2603 {
2604         /* VF handle has no client */
2605         if (!handle->client)
2606                 return container_of(handle, struct hclge_vport, nic);
2607         else if (handle->client->type == HNAE3_CLIENT_ROCE)
2608                 return container_of(handle, struct hclge_vport, roce);
2609         else
2610                 return container_of(handle, struct hclge_vport, nic);
2611 }
2612
2613 static int hclge_get_vector(struct hnae3_handle *handle, u16 vector_num,
2614                             struct hnae3_vector_info *vector_info)
2615 {
2616         struct hclge_vport *vport = hclge_get_vport(handle);
2617         struct hnae3_vector_info *vector = vector_info;
2618         struct hclge_dev *hdev = vport->back;
2619         int alloc = 0;
2620         int i, j;
2621
2622         vector_num = min(hdev->num_msi_left, vector_num);
2623
2624         for (j = 0; j < vector_num; j++) {
2625                 for (i = 1; i < hdev->num_msi; i++) {
2626                         if (hdev->vector_status[i] == HCLGE_INVALID_VPORT) {
2627                                 vector->vector = pci_irq_vector(hdev->pdev, i);
2628                                 vector->io_addr = hdev->hw.io_base +
2629                                         HCLGE_VECTOR_REG_BASE +
2630                                         (i - 1) * HCLGE_VECTOR_REG_OFFSET +
2631                                         vport->vport_id *
2632                                         HCLGE_VECTOR_VF_OFFSET;
2633                                 hdev->vector_status[i] = vport->vport_id;
2634                                 hdev->vector_irq[i] = vector->vector;
2635
2636                                 vector++;
2637                                 alloc++;
2638
2639                                 break;
2640                         }
2641                 }
2642         }
2643         hdev->num_msi_left -= alloc;
2644         hdev->num_msi_used += alloc;
2645
2646         return alloc;
2647 }
2648
2649 static int hclge_get_vector_index(struct hclge_dev *hdev, int vector)
2650 {
2651         int i;
2652
2653         for (i = 0; i < hdev->num_msi; i++)
2654                 if (vector == hdev->vector_irq[i])
2655                         return i;
2656
2657         return -EINVAL;
2658 }
2659
2660 static int hclge_put_vector(struct hnae3_handle *handle, int vector)
2661 {
2662         struct hclge_vport *vport = hclge_get_vport(handle);
2663         struct hclge_dev *hdev = vport->back;
2664         int vector_id;
2665
2666         vector_id = hclge_get_vector_index(hdev, vector);
2667         if (vector_id < 0) {
2668                 dev_err(&hdev->pdev->dev,
2669                         "Get vector index fail. vector_id =%d\n", vector_id);
2670                 return vector_id;
2671         }
2672
2673         hclge_free_vector(hdev, vector_id);
2674
2675         return 0;
2676 }
2677
2678 static u32 hclge_get_rss_key_size(struct hnae3_handle *handle)
2679 {
2680         return HCLGE_RSS_KEY_SIZE;
2681 }
2682
2683 static u32 hclge_get_rss_indir_size(struct hnae3_handle *handle)
2684 {
2685         return HCLGE_RSS_IND_TBL_SIZE;
2686 }
2687
2688 static int hclge_set_rss_algo_key(struct hclge_dev *hdev,
2689                                   const u8 hfunc, const u8 *key)
2690 {
2691         struct hclge_rss_config_cmd *req;
2692         struct hclge_desc desc;
2693         int key_offset;
2694         int key_size;
2695         int ret;
2696
2697         req = (struct hclge_rss_config_cmd *)desc.data;
2698
2699         for (key_offset = 0; key_offset < 3; key_offset++) {
2700                 hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RSS_GENERIC_CONFIG,
2701                                            false);
2702
2703                 req->hash_config |= (hfunc & HCLGE_RSS_HASH_ALGO_MASK);
2704                 req->hash_config |= (key_offset << HCLGE_RSS_HASH_KEY_OFFSET_B);
2705
2706                 if (key_offset == 2)
2707                         key_size =
2708                         HCLGE_RSS_KEY_SIZE - HCLGE_RSS_HASH_KEY_NUM * 2;
2709                 else
2710                         key_size = HCLGE_RSS_HASH_KEY_NUM;
2711
2712                 memcpy(req->hash_key,
2713                        key + key_offset * HCLGE_RSS_HASH_KEY_NUM, key_size);
2714
2715                 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2716                 if (ret) {
2717                         dev_err(&hdev->pdev->dev,
2718                                 "Configure RSS config fail, status = %d\n",
2719                                 ret);
2720                         return ret;
2721                 }
2722         }
2723         return 0;
2724 }
2725
2726 static int hclge_set_rss_indir_table(struct hclge_dev *hdev, const u8 *indir)
2727 {
2728         struct hclge_rss_indirection_table_cmd *req;
2729         struct hclge_desc desc;
2730         int i, j;
2731         int ret;
2732
2733         req = (struct hclge_rss_indirection_table_cmd *)desc.data;
2734
2735         for (i = 0; i < HCLGE_RSS_CFG_TBL_NUM; i++) {
2736                 hclge_cmd_setup_basic_desc
2737                         (&desc, HCLGE_OPC_RSS_INDIR_TABLE, false);
2738
2739                 req->start_table_index =
2740                         cpu_to_le16(i * HCLGE_RSS_CFG_TBL_SIZE);
2741                 req->rss_set_bitmap = cpu_to_le16(HCLGE_RSS_SET_BITMAP_MSK);
2742
2743                 for (j = 0; j < HCLGE_RSS_CFG_TBL_SIZE; j++)
2744                         req->rss_result[j] =
2745                                 indir[i * HCLGE_RSS_CFG_TBL_SIZE + j];
2746
2747                 ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2748                 if (ret) {
2749                         dev_err(&hdev->pdev->dev,
2750                                 "Configure rss indir table fail,status = %d\n",
2751                                 ret);
2752                         return ret;
2753                 }
2754         }
2755         return 0;
2756 }
2757
2758 static int hclge_set_rss_tc_mode(struct hclge_dev *hdev, u16 *tc_valid,
2759                                  u16 *tc_size, u16 *tc_offset)
2760 {
2761         struct hclge_rss_tc_mode_cmd *req;
2762         struct hclge_desc desc;
2763         int ret;
2764         int i;
2765
2766         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RSS_TC_MODE, false);
2767         req = (struct hclge_rss_tc_mode_cmd *)desc.data;
2768
2769         for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2770                 u16 mode = 0;
2771
2772                 hnae3_set_bit(mode, HCLGE_RSS_TC_VALID_B, (tc_valid[i] & 0x1));
2773                 hnae3_set_field(mode, HCLGE_RSS_TC_SIZE_M,
2774                                 HCLGE_RSS_TC_SIZE_S, tc_size[i]);
2775                 hnae3_set_field(mode, HCLGE_RSS_TC_OFFSET_M,
2776                                 HCLGE_RSS_TC_OFFSET_S, tc_offset[i]);
2777
2778                 req->rss_tc_mode[i] = cpu_to_le16(mode);
2779         }
2780
2781         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2782         if (ret)
2783                 dev_err(&hdev->pdev->dev,
2784                         "Configure rss tc mode fail, status = %d\n", ret);
2785
2786         return ret;
2787 }
2788
2789 static void hclge_get_rss_type(struct hclge_vport *vport)
2790 {
2791         if (vport->rss_tuple_sets.ipv4_tcp_en ||
2792             vport->rss_tuple_sets.ipv4_udp_en ||
2793             vport->rss_tuple_sets.ipv4_sctp_en ||
2794             vport->rss_tuple_sets.ipv6_tcp_en ||
2795             vport->rss_tuple_sets.ipv6_udp_en ||
2796             vport->rss_tuple_sets.ipv6_sctp_en)
2797                 vport->nic.kinfo.rss_type = PKT_HASH_TYPE_L4;
2798         else if (vport->rss_tuple_sets.ipv4_fragment_en ||
2799                  vport->rss_tuple_sets.ipv6_fragment_en)
2800                 vport->nic.kinfo.rss_type = PKT_HASH_TYPE_L3;
2801         else
2802                 vport->nic.kinfo.rss_type = PKT_HASH_TYPE_NONE;
2803 }
2804
2805 static int hclge_set_rss_input_tuple(struct hclge_dev *hdev)
2806 {
2807         struct hclge_rss_input_tuple_cmd *req;
2808         struct hclge_desc desc;
2809         int ret;
2810
2811         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RSS_INPUT_TUPLE, false);
2812
2813         req = (struct hclge_rss_input_tuple_cmd *)desc.data;
2814
2815         /* Get the tuple cfg from pf */
2816         req->ipv4_tcp_en = hdev->vport[0].rss_tuple_sets.ipv4_tcp_en;
2817         req->ipv4_udp_en = hdev->vport[0].rss_tuple_sets.ipv4_udp_en;
2818         req->ipv4_sctp_en = hdev->vport[0].rss_tuple_sets.ipv4_sctp_en;
2819         req->ipv4_fragment_en = hdev->vport[0].rss_tuple_sets.ipv4_fragment_en;
2820         req->ipv6_tcp_en = hdev->vport[0].rss_tuple_sets.ipv6_tcp_en;
2821         req->ipv6_udp_en = hdev->vport[0].rss_tuple_sets.ipv6_udp_en;
2822         req->ipv6_sctp_en = hdev->vport[0].rss_tuple_sets.ipv6_sctp_en;
2823         req->ipv6_fragment_en = hdev->vport[0].rss_tuple_sets.ipv6_fragment_en;
2824         hclge_get_rss_type(&hdev->vport[0]);
2825         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2826         if (ret)
2827                 dev_err(&hdev->pdev->dev,
2828                         "Configure rss input fail, status = %d\n", ret);
2829         return ret;
2830 }
2831
2832 static int hclge_get_rss(struct hnae3_handle *handle, u32 *indir,
2833                          u8 *key, u8 *hfunc)
2834 {
2835         struct hclge_vport *vport = hclge_get_vport(handle);
2836         int i;
2837
2838         /* Get hash algorithm */
2839         if (hfunc) {
2840                 switch (vport->rss_algo) {
2841                 case HCLGE_RSS_HASH_ALGO_TOEPLITZ:
2842                         *hfunc = ETH_RSS_HASH_TOP;
2843                         break;
2844                 case HCLGE_RSS_HASH_ALGO_SIMPLE:
2845                         *hfunc = ETH_RSS_HASH_XOR;
2846                         break;
2847                 default:
2848                         *hfunc = ETH_RSS_HASH_UNKNOWN;
2849                         break;
2850                 }
2851         }
2852
2853         /* Get the RSS Key required by the user */
2854         if (key)
2855                 memcpy(key, vport->rss_hash_key, HCLGE_RSS_KEY_SIZE);
2856
2857         /* Get indirect table */
2858         if (indir)
2859                 for (i = 0; i < HCLGE_RSS_IND_TBL_SIZE; i++)
2860                         indir[i] =  vport->rss_indirection_tbl[i];
2861
2862         return 0;
2863 }
2864
2865 static int hclge_set_rss(struct hnae3_handle *handle, const u32 *indir,
2866                          const  u8 *key, const  u8 hfunc)
2867 {
2868         struct hclge_vport *vport = hclge_get_vport(handle);
2869         struct hclge_dev *hdev = vport->back;
2870         u8 hash_algo;
2871         int ret, i;
2872
2873         /* Set the RSS Hash Key if specififed by the user */
2874         if (key) {
2875                 switch (hfunc) {
2876                 case ETH_RSS_HASH_TOP:
2877                         hash_algo = HCLGE_RSS_HASH_ALGO_TOEPLITZ;
2878                         break;
2879                 case ETH_RSS_HASH_XOR:
2880                         hash_algo = HCLGE_RSS_HASH_ALGO_SIMPLE;
2881                         break;
2882                 case ETH_RSS_HASH_NO_CHANGE:
2883                         hash_algo = vport->rss_algo;
2884                         break;
2885                 default:
2886                         return -EINVAL;
2887                 }
2888
2889                 ret = hclge_set_rss_algo_key(hdev, hash_algo, key);
2890                 if (ret)
2891                         return ret;
2892
2893                 /* Update the shadow RSS key with user specified qids */
2894                 memcpy(vport->rss_hash_key, key, HCLGE_RSS_KEY_SIZE);
2895                 vport->rss_algo = hash_algo;
2896         }
2897
2898         /* Update the shadow RSS table with user specified qids */
2899         for (i = 0; i < HCLGE_RSS_IND_TBL_SIZE; i++)
2900                 vport->rss_indirection_tbl[i] = indir[i];
2901
2902         /* Update the hardware */
2903         return hclge_set_rss_indir_table(hdev, vport->rss_indirection_tbl);
2904 }
2905
2906 static u8 hclge_get_rss_hash_bits(struct ethtool_rxnfc *nfc)
2907 {
2908         u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGE_S_PORT_BIT : 0;
2909
2910         if (nfc->data & RXH_L4_B_2_3)
2911                 hash_sets |= HCLGE_D_PORT_BIT;
2912         else
2913                 hash_sets &= ~HCLGE_D_PORT_BIT;
2914
2915         if (nfc->data & RXH_IP_SRC)
2916                 hash_sets |= HCLGE_S_IP_BIT;
2917         else
2918                 hash_sets &= ~HCLGE_S_IP_BIT;
2919
2920         if (nfc->data & RXH_IP_DST)
2921                 hash_sets |= HCLGE_D_IP_BIT;
2922         else
2923                 hash_sets &= ~HCLGE_D_IP_BIT;
2924
2925         if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW)
2926                 hash_sets |= HCLGE_V_TAG_BIT;
2927
2928         return hash_sets;
2929 }
2930
2931 static int hclge_set_rss_tuple(struct hnae3_handle *handle,
2932                                struct ethtool_rxnfc *nfc)
2933 {
2934         struct hclge_vport *vport = hclge_get_vport(handle);
2935         struct hclge_dev *hdev = vport->back;
2936         struct hclge_rss_input_tuple_cmd *req;
2937         struct hclge_desc desc;
2938         u8 tuple_sets;
2939         int ret;
2940
2941         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
2942                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
2943                 return -EINVAL;
2944
2945         req = (struct hclge_rss_input_tuple_cmd *)desc.data;
2946         hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RSS_INPUT_TUPLE, false);
2947
2948         req->ipv4_tcp_en = vport->rss_tuple_sets.ipv4_tcp_en;
2949         req->ipv4_udp_en = vport->rss_tuple_sets.ipv4_udp_en;
2950         req->ipv4_sctp_en = vport->rss_tuple_sets.ipv4_sctp_en;
2951         req->ipv4_fragment_en = vport->rss_tuple_sets.ipv4_fragment_en;
2952         req->ipv6_tcp_en = vport->rss_tuple_sets.ipv6_tcp_en;
2953         req->ipv6_udp_en = vport->rss_tuple_sets.ipv6_udp_en;
2954         req->ipv6_sctp_en = vport->rss_tuple_sets.ipv6_sctp_en;
2955         req->ipv6_fragment_en = vport->rss_tuple_sets.ipv6_fragment_en;
2956
2957         tuple_sets = hclge_get_rss_hash_bits(nfc);
2958         switch (nfc->flow_type) {
2959         case TCP_V4_FLOW:
2960                 req->ipv4_tcp_en = tuple_sets;
2961                 break;
2962         case TCP_V6_FLOW:
2963                 req->ipv6_tcp_en = tuple_sets;
2964                 break;
2965         case UDP_V4_FLOW:
2966                 req->ipv4_udp_en = tuple_sets;
2967                 break;
2968         case UDP_V6_FLOW:
2969                 req->ipv6_udp_en = tuple_sets;
2970                 break;
2971         case SCTP_V4_FLOW:
2972                 req->ipv4_sctp_en = tuple_sets;
2973                 break;
2974         case SCTP_V6_FLOW:
2975                 if ((nfc->data & RXH_L4_B_0_1) ||
2976                     (nfc->data & RXH_L4_B_2_3))
2977                         return -EINVAL;
2978
2979                 req->ipv6_sctp_en = tuple_sets;
2980                 break;
2981         case IPV4_FLOW:
2982                 req->ipv4_fragment_en = HCLGE_RSS_INPUT_TUPLE_OTHER;
2983                 break;
2984         case IPV6_FLOW:
2985                 req->ipv6_fragment_en = HCLGE_RSS_INPUT_TUPLE_OTHER;
2986                 break;
2987         default:
2988                 return -EINVAL;
2989         }
2990
2991         ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2992         if (ret) {
2993                 dev_err(&hdev->pdev->dev,
2994                         "Set rss tuple fail, status = %d\n", ret);
2995                 return ret;
2996         }
2997
2998         vport->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en;
2999         vport->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en;
3000         vport->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en;
3001         vport->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en;
3002         vport->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en;
3003         vport->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en;
3004         vport->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en;
3005         vport->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en;
3006         hclge_get_rss_type(vport);
3007         return 0;
3008 }
3009
3010 static int hclge_get_rss_tuple(struct hnae3_handle *handle,
3011                                struct ethtool_rxnfc *nfc)
3012 {
3013         struct hclge_vport *vport = hclge_get_vport(handle);
3014         u8 tuple_sets;
3015
3016         nfc->data = 0;
3017
3018         switch (nfc->flow_type) {
3019         case TCP_V4_FLOW:
3020                 tuple_sets = vport->rss_tuple_sets.ipv4_tcp_en;
3021                 break;
3022         case UDP_V4_FLOW:
3023                 tuple_sets = vport->rss_tuple_sets.ipv4_udp_en;
3024                 break;
3025         case TCP_V6_FLOW:
3026                 tuple_sets = vport->rss_tuple_sets.ipv6_tcp_en;
3027                 break;
3028         case UDP_V6_FLOW:
3029                 tuple_sets = vport->rss_tuple_sets.ipv6_udp_en;
3030                 break;
3031         case SCTP_V4_FLOW:
3032                 tuple_sets = vport->rss_tuple_sets.ipv4_sctp_en;
3033                 break;
3034         case SCTP_V6_FLOW:
3035                 tuple_sets = vport->rss_tuple_sets.ipv6_sctp_en;
3036                 break;
3037         case IPV4_FLOW:
3038         case IPV6_FLOW:
3039                 tuple_sets = HCLGE_S_IP_BIT | HCLGE_D_IP_BIT;
3040                 break;
3041         default:
3042                 return -EINVAL;
3043         }
3044
3045         if (!tuple_sets)
3046                 return 0;
3047
3048         if (tuple_sets & HCLGE_D_PORT_BIT)
3049                 nfc->data |= RXH_L4_B_2_3;
3050         if (tuple_sets & HCLGE_S_PORT_BIT)
3051                 nfc->data |= RXH_L4_B_0_1;
3052         if (tuple_sets & HCLGE_D_IP_BIT)
3053                 nfc->data |= RXH_IP_DST;
3054         if (tuple_sets & HCLGE_S_IP_BIT)
3055                 nfc->data |= RXH_IP_SRC;
3056
3057         return 0;
3058 }
3059
3060 static int hclge_get_tc_size(struct hnae3_handle *handle)
3061 {
3062         struct hclge_vport *vport = hclge_get_vport(handle);
3063         struct hclge_dev *hdev = vport->back;
3064
3065         return hdev->rss_size_max;
3066 }