1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
6 #include <linux/string_choices.h>
7 #include <linux/phy.h>
8 #include <linux/sfp.h>
9
10 #include "hns3_enet.h"
11 #include "hns3_ethtool.h"
12
13 /* tqp related stats */
14 #define HNS3_TQP_STAT(_string, _member) { \
15 .stats_string = _string, \
16 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
17 offsetof(struct ring_stats, _member), \
18 }
19
20 static const struct hns3_stats hns3_txq_stats[] = {
21 /* Tx per-queue statistics */
22 HNS3_TQP_STAT("dropped", sw_err_cnt),
23 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
24 HNS3_TQP_STAT("packets", tx_pkts),
25 HNS3_TQP_STAT("bytes", tx_bytes),
26 HNS3_TQP_STAT("more", tx_more),
27 HNS3_TQP_STAT("push", tx_push),
28 HNS3_TQP_STAT("mem_doorbell", tx_mem_doorbell),
29 HNS3_TQP_STAT("wake", restart_queue),
30 HNS3_TQP_STAT("busy", tx_busy),
31 HNS3_TQP_STAT("copy", tx_copy),
32 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
33 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
34 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
35 HNS3_TQP_STAT("tso_err", tx_tso_err),
36 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
37 HNS3_TQP_STAT("hw_limitation", hw_limitation),
38 HNS3_TQP_STAT("bounce", tx_bounce),
39 HNS3_TQP_STAT("spare_full", tx_spare_full),
40 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
41 HNS3_TQP_STAT("sgl", tx_sgl),
42 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
43 HNS3_TQP_STAT("map_sg_err", map_sg_err),
44 };
45
46 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
47
48 static const struct hns3_stats hns3_rxq_stats[] = {
49 /* Rx per-queue statistics */
50 HNS3_TQP_STAT("dropped", sw_err_cnt),
51 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
52 HNS3_TQP_STAT("packets", rx_pkts),
53 HNS3_TQP_STAT("bytes", rx_bytes),
54 HNS3_TQP_STAT("errors", rx_err_cnt),
55 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
56 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
57 HNS3_TQP_STAT("err_bd_num", err_bd_num),
58 HNS3_TQP_STAT("l2_err", l2_err),
59 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
60 HNS3_TQP_STAT("csum_complete", csum_complete),
61 HNS3_TQP_STAT("multicast", rx_multicast),
62 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
63 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
64 HNS3_TQP_STAT("frag_alloc", frag_alloc),
65 };
66
67 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
68
69 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
70
71 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
72
73 #define HNS3_NIC_LB_TEST_PKT_NUM 1
74 #define HNS3_NIC_LB_TEST_RING_ID 0
75 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
76 #define HNS3_NIC_LB_SETUP_USEC 10000
77
78 /* Nic loopback test err */
79 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
80 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
81 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
82 #define HNS3_NIC_LB_TEST_UNEXECUTED 4
83
84 static int hns3_get_sset_count(struct net_device *netdev, int stringset);
85
hns3_lp_setup(struct net_device * ndev,enum hnae3_loop loop,bool en)86 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
87 {
88 struct hnae3_handle *h = hns3_get_handle(ndev);
89 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
90 int ret;
91
92 if (!h->ae_algo->ops->set_loopback ||
93 !h->ae_algo->ops->set_promisc_mode)
94 return -EOPNOTSUPP;
95
96 switch (loop) {
97 case HNAE3_LOOP_SERIAL_SERDES:
98 case HNAE3_LOOP_PARALLEL_SERDES:
99 case HNAE3_LOOP_APP:
100 case HNAE3_LOOP_PHY:
101 case HNAE3_LOOP_EXTERNAL:
102 ret = h->ae_algo->ops->set_loopback(h, loop, en);
103 break;
104 default:
105 ret = -ENOTSUPP;
106 break;
107 }
108
109 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
110 return ret;
111
112 if (en)
113 h->ae_algo->ops->set_promisc_mode(h, true, true);
114 else
115 /* recover promisc mode before loopback test */
116 hns3_request_update_promisc_mode(h);
117
118 return ret;
119 }
120
hns3_lp_up(struct net_device * ndev,enum hnae3_loop loop_mode)121 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
122 {
123 struct hnae3_handle *h = hns3_get_handle(ndev);
124 int ret;
125
126 ret = hns3_nic_reset_all_ring(h);
127 if (ret)
128 return ret;
129
130 ret = hns3_lp_setup(ndev, loop_mode, true);
131 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
132
133 return ret;
134 }
135
hns3_lp_down(struct net_device * ndev,enum hnae3_loop loop_mode)136 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
137 {
138 int ret;
139
140 ret = hns3_lp_setup(ndev, loop_mode, false);
141 if (ret) {
142 netdev_err(ndev, "lb_setup return error: %d\n", ret);
143 return ret;
144 }
145
146 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
147
148 return 0;
149 }
150
hns3_lp_setup_skb(struct sk_buff * skb)151 static void hns3_lp_setup_skb(struct sk_buff *skb)
152 {
153 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
154
155 struct net_device *ndev = skb->dev;
156 struct hnae3_handle *handle;
157 struct hnae3_ae_dev *ae_dev;
158 unsigned char *packet;
159 struct ethhdr *ethh;
160 unsigned int i;
161
162 skb_reserve(skb, NET_IP_ALIGN);
163 ethh = skb_put(skb, sizeof(struct ethhdr));
164 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
165
166 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
167
168 /* The dst mac addr of loopback packet is the same as the host'
169 * mac addr, the SSU component may loop back the packet to host
170 * before the packet reaches mac or serdes, which will defect
171 * the purpose of mac or serdes selftest.
172 */
173 handle = hns3_get_handle(ndev);
174 ae_dev = hns3_get_ae_dev(handle);
175 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
176 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
177 eth_zero_addr(ethh->h_source);
178 ethh->h_proto = htons(ETH_P_ARP);
179 skb_reset_mac_header(skb);
180
181 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
182 packet[i] = (unsigned char)(i & 0xff);
183 }
184
hns3_lb_check_skb_data(struct hns3_enet_ring * ring,struct sk_buff * skb)185 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
186 struct sk_buff *skb)
187 {
188 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
189 unsigned char *packet = skb->data;
190 u32 len = skb_headlen(skb);
191 u32 i;
192
193 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
194
195 for (i = 0; i < len; i++)
196 if (packet[i] != (unsigned char)(i & 0xff))
197 break;
198
199 /* The packet is correctly received */
200 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
201 tqp_vector->rx_group.total_packets++;
202 else
203 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
204 skb->data, len, true);
205
206 dev_kfree_skb_any(skb);
207 }
208
hns3_lb_check_rx_ring(struct hns3_nic_priv * priv,u32 budget)209 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
210 {
211 struct hnae3_handle *h = priv->ae_handle;
212 struct hnae3_knic_private_info *kinfo;
213 u32 i, rcv_good_pkt_total = 0;
214
215 kinfo = &h->kinfo;
216 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
217 struct hns3_enet_ring *ring = &priv->ring[i];
218 struct hns3_enet_ring_group *rx_group;
219 u64 pre_rx_pkt;
220
221 rx_group = &ring->tqp_vector->rx_group;
222 pre_rx_pkt = rx_group->total_packets;
223
224 preempt_disable();
225 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
226 preempt_enable();
227
228 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
229 rx_group->total_packets = pre_rx_pkt;
230 }
231 return rcv_good_pkt_total;
232 }
233
hns3_lb_clear_tx_ring(struct hns3_nic_priv * priv,u32 start_ringid,u32 end_ringid)234 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
235 u32 end_ringid)
236 {
237 u32 i;
238
239 for (i = start_ringid; i <= end_ringid; i++) {
240 struct hns3_enet_ring *ring = &priv->ring[i];
241
242 hns3_clean_tx_ring(ring, 0);
243 }
244 }
245
246 /**
247 * hns3_lp_run_test - run loopback test
248 * @ndev: net device
249 * @mode: loopback type
250 *
251 * Return: %0 for success or a NIC loopback test error code on failure
252 */
hns3_lp_run_test(struct net_device * ndev,enum hnae3_loop mode)253 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
254 {
255 struct hns3_nic_priv *priv = netdev_priv(ndev);
256 struct sk_buff *skb;
257 u32 i, good_cnt;
258 int ret_val = 0;
259
260 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
261 GFP_KERNEL);
262 if (!skb)
263 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
264
265 skb->dev = ndev;
266 hns3_lp_setup_skb(skb);
267 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
268
269 good_cnt = 0;
270 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
271 netdev_tx_t tx_ret;
272
273 skb_get(skb);
274 tx_ret = hns3_nic_net_xmit(skb, ndev);
275 if (tx_ret == NETDEV_TX_OK) {
276 good_cnt++;
277 } else {
278 kfree_skb(skb);
279 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
280 tx_ret);
281 }
282 }
283 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
284 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
285 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
286 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
287 goto out;
288 }
289
290 /* Allow 200 milliseconds for packets to go from Tx to Rx */
291 msleep(200);
292
293 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
294 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
295 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
296 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
297 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
298 }
299
300 out:
301 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
302 HNS3_NIC_LB_TEST_RING_ID);
303
304 kfree_skb(skb);
305 return ret_val;
306 }
307
hns3_set_selftest_param(struct hnae3_handle * h,int (* st_param)[2])308 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
309 {
310 st_param[HNAE3_LOOP_EXTERNAL][0] = HNAE3_LOOP_EXTERNAL;
311 st_param[HNAE3_LOOP_EXTERNAL][1] =
312 h->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK;
313
314 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
315 st_param[HNAE3_LOOP_APP][1] =
316 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
317
318 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
319 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
320 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
321
322 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
323 HNAE3_LOOP_PARALLEL_SERDES;
324 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
325 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
326
327 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
328 st_param[HNAE3_LOOP_PHY][1] =
329 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
330 }
331
hns3_selftest_prepare(struct net_device * ndev,bool if_running)332 static void hns3_selftest_prepare(struct net_device *ndev, bool if_running)
333 {
334 struct hns3_nic_priv *priv = netdev_priv(ndev);
335 struct hnae3_handle *h = priv->ae_handle;
336
337 if (if_running)
338 ndev->netdev_ops->ndo_stop(ndev);
339
340 #if IS_ENABLED(CONFIG_VLAN_8021Q)
341 /* Disable the vlan filter for selftest does not support it */
342 if (h->ae_algo->ops->enable_vlan_filter &&
343 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
344 h->ae_algo->ops->enable_vlan_filter(h, false);
345 #endif
346
347 /* Tell firmware to stop mac autoneg before loopback test start,
348 * otherwise loopback test may be failed when the port is still
349 * negotiating.
350 */
351 if (h->ae_algo->ops->halt_autoneg)
352 h->ae_algo->ops->halt_autoneg(h, true);
353
354 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
355 }
356
hns3_selftest_restore(struct net_device * ndev,bool if_running)357 static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
358 {
359 struct hns3_nic_priv *priv = netdev_priv(ndev);
360 struct hnae3_handle *h = priv->ae_handle;
361
362 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
363
364 if (h->ae_algo->ops->halt_autoneg)
365 h->ae_algo->ops->halt_autoneg(h, false);
366
367 #if IS_ENABLED(CONFIG_VLAN_8021Q)
368 if (h->ae_algo->ops->enable_vlan_filter &&
369 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
370 h->ae_algo->ops->enable_vlan_filter(h, true);
371 #endif
372
373 if (if_running)
374 ndev->netdev_ops->ndo_open(ndev);
375 }
376
hns3_do_selftest(struct net_device * ndev,int (* st_param)[2],struct ethtool_test * eth_test,u64 * data)377 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
378 struct ethtool_test *eth_test, u64 *data)
379 {
380 int test_index = HNAE3_LOOP_APP;
381 u32 i;
382
383 for (i = HNAE3_LOOP_APP; i < HNAE3_LOOP_NONE; i++) {
384 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
385
386 if (!st_param[i][1])
387 continue;
388
389 data[test_index] = hns3_lp_up(ndev, loop_type);
390 if (!data[test_index])
391 data[test_index] = hns3_lp_run_test(ndev, loop_type);
392
393 hns3_lp_down(ndev, loop_type);
394
395 if (data[test_index])
396 eth_test->flags |= ETH_TEST_FL_FAILED;
397
398 test_index++;
399 }
400 }
401
hns3_do_external_lb(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)402 static void hns3_do_external_lb(struct net_device *ndev,
403 struct ethtool_test *eth_test, u64 *data)
404 {
405 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_up(ndev, HNAE3_LOOP_EXTERNAL);
406 if (!data[HNAE3_LOOP_EXTERNAL])
407 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_run_test(ndev, HNAE3_LOOP_EXTERNAL);
408 hns3_lp_down(ndev, HNAE3_LOOP_EXTERNAL);
409
410 if (data[HNAE3_LOOP_EXTERNAL])
411 eth_test->flags |= ETH_TEST_FL_FAILED;
412
413 eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
414 }
415
416 /**
417 * hns3_self_test - self test
418 * @ndev: net device
419 * @eth_test: test cmd
420 * @data: test result
421 */
hns3_self_test(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)422 static void hns3_self_test(struct net_device *ndev,
423 struct ethtool_test *eth_test, u64 *data)
424 {
425 int cnt = hns3_get_sset_count(ndev, ETH_SS_TEST);
426 struct hns3_nic_priv *priv = netdev_priv(ndev);
427 struct hnae3_handle *h = priv->ae_handle;
428 int st_param[HNAE3_LOOP_NONE][2];
429 bool if_running = netif_running(ndev);
430 int i;
431
432 /* initialize the loopback test result, avoid marking an unexcuted
433 * loopback test as PASS.
434 */
435 for (i = 0; i < cnt; i++)
436 data[i] = HNS3_NIC_LB_TEST_UNEXECUTED;
437
438 if (hns3_nic_resetting(ndev)) {
439 netdev_err(ndev, "dev resetting!\n");
440 goto failure;
441 }
442
443 if (!(eth_test->flags & ETH_TEST_FL_OFFLINE))
444 goto failure;
445
446 if (netif_msg_ifdown(h))
447 netdev_info(ndev, "self test start\n");
448
449 hns3_set_selftest_param(h, st_param);
450
451 /* external loopback test requires that the link is up and the duplex is
452 * full, do external test first to reduce the whole test time
453 */
454 if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) {
455 hns3_external_lb_prepare(ndev, if_running);
456 hns3_do_external_lb(ndev, eth_test, data);
457 hns3_external_lb_restore(ndev, if_running);
458 }
459
460 hns3_selftest_prepare(ndev, if_running);
461 hns3_do_selftest(ndev, st_param, eth_test, data);
462 hns3_selftest_restore(ndev, if_running);
463
464 if (netif_msg_ifdown(h))
465 netdev_info(ndev, "self test end\n");
466 return;
467
468 failure:
469 eth_test->flags |= ETH_TEST_FL_FAILED;
470 }
471
hns3_update_limit_promisc_mode(struct net_device * netdev,bool enable)472 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
473 bool enable)
474 {
475 struct hnae3_handle *handle = hns3_get_handle(netdev);
476
477 if (enable)
478 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
479 else
480 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
481
482 hns3_request_update_promisc_mode(handle);
483 }
484
485 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
486 { "limit_promisc", hns3_update_limit_promisc_mode }
487 };
488
hns3_get_sset_count(struct net_device * netdev,int stringset)489 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
490 {
491 struct hnae3_handle *h = hns3_get_handle(netdev);
492 const struct hnae3_ae_ops *ops = hns3_get_ops(h);
493
494 if (!ops->get_sset_count)
495 return -EOPNOTSUPP;
496
497 switch (stringset) {
498 case ETH_SS_STATS:
499 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
500 ops->get_sset_count(h, stringset));
501
502 case ETH_SS_TEST:
503 return ops->get_sset_count(h, stringset);
504
505 case ETH_SS_PRIV_FLAGS:
506 return HNAE3_PFLAG_MAX;
507
508 default:
509 return -EOPNOTSUPP;
510 }
511 }
512
hns3_update_strings(u8 ** data,const struct hns3_stats * stats,u32 stat_count,u32 num_tqps,const char * prefix)513 static void hns3_update_strings(u8 **data, const struct hns3_stats *stats,
514 u32 stat_count, u32 num_tqps,
515 const char *prefix)
516 {
517 u32 i, j;
518
519 for (i = 0; i < num_tqps; i++)
520 for (j = 0; j < stat_count; j++)
521 ethtool_sprintf(data, "%s%u_%s", prefix, i,
522 stats[j].stats_string);
523 }
524
hns3_get_strings_tqps(struct hnae3_handle * handle,u8 ** data)525 static void hns3_get_strings_tqps(struct hnae3_handle *handle, u8 **data)
526 {
527 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
528 const char tx_prefix[] = "txq";
529 const char rx_prefix[] = "rxq";
530
531 /* get strings for Tx */
532 hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
533 kinfo->num_tqps, tx_prefix);
534
535 /* get strings for Rx */
536 hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
537 kinfo->num_tqps, rx_prefix);
538 }
539
hns3_get_strings(struct net_device * netdev,u32 stringset,u8 * data)540 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
541 {
542 struct hnae3_handle *h = hns3_get_handle(netdev);
543 const struct hnae3_ae_ops *ops = hns3_get_ops(h);
544 u32 i;
545
546 if (!ops->get_strings)
547 return;
548
549 switch (stringset) {
550 case ETH_SS_STATS:
551 hns3_get_strings_tqps(h, &data);
552 ops->get_strings(h, stringset, &data);
553 break;
554 case ETH_SS_TEST:
555 ops->get_strings(h, stringset, &data);
556 break;
557 case ETH_SS_PRIV_FLAGS:
558 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++)
559 ethtool_puts(&data, hns3_priv_flags[i].name);
560 break;
561 default:
562 break;
563 }
564 }
565
hns3_get_stats_tqps(struct hnae3_handle * handle,u64 * data)566 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
567 {
568 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
569 struct hns3_nic_priv *nic_priv = handle->priv;
570 struct hns3_enet_ring *ring;
571 u8 *stat;
572 u32 i, j;
573
574 /* get stats for Tx */
575 for (i = 0; i < kinfo->num_tqps; i++) {
576 ring = &nic_priv->ring[i];
577 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
578 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
579 *data++ = *(u64 *)stat;
580 }
581 }
582
583 /* get stats for Rx */
584 for (i = 0; i < kinfo->num_tqps; i++) {
585 ring = &nic_priv->ring[i + kinfo->num_tqps];
586 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
587 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
588 *data++ = *(u64 *)stat;
589 }
590 }
591
592 return data;
593 }
594
595 /* hns3_get_stats - get detail statistics.
596 * @netdev: net device
597 * @stats: statistics info.
598 * @data: statistics data.
599 */
hns3_get_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)600 static void hns3_get_stats(struct net_device *netdev,
601 struct ethtool_stats *stats, u64 *data)
602 {
603 struct hnae3_handle *h = hns3_get_handle(netdev);
604 u64 *p = data;
605
606 if (hns3_nic_resetting(netdev)) {
607 netdev_err(netdev, "dev resetting, could not get stats\n");
608 return;
609 }
610
611 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
612 netdev_err(netdev, "could not get any statistics\n");
613 return;
614 }
615
616 h->ae_algo->ops->update_stats(h);
617
618 /* get per-queue stats */
619 p = hns3_get_stats_tqps(h, p);
620
621 /* get MAC & other misc hardware stats */
622 h->ae_algo->ops->get_stats(h, p);
623 }
624
hns3_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)625 static void hns3_get_drvinfo(struct net_device *netdev,
626 struct ethtool_drvinfo *drvinfo)
627 {
628 struct hns3_nic_priv *priv = netdev_priv(netdev);
629 struct hnae3_handle *h = priv->ae_handle;
630 u32 fw_version;
631
632 if (!h->ae_algo->ops->get_fw_version) {
633 netdev_err(netdev, "could not get fw version!\n");
634 return;
635 }
636
637 strscpy(drvinfo->driver, dev_driver_string(&h->pdev->dev),
638 sizeof(drvinfo->driver));
639
640 strscpy(drvinfo->bus_info, pci_name(h->pdev),
641 sizeof(drvinfo->bus_info));
642
643 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
644
645 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
646 "%lu.%lu.%lu.%lu",
647 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
648 HNAE3_FW_VERSION_BYTE3_SHIFT),
649 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
650 HNAE3_FW_VERSION_BYTE2_SHIFT),
651 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
652 HNAE3_FW_VERSION_BYTE1_SHIFT),
653 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
654 HNAE3_FW_VERSION_BYTE0_SHIFT));
655 }
656
hns3_get_link(struct net_device * netdev)657 static u32 hns3_get_link(struct net_device *netdev)
658 {
659 struct hnae3_handle *h = hns3_get_handle(netdev);
660
661 if (h->ae_algo->ops->get_status)
662 return h->ae_algo->ops->get_status(h);
663 else
664 return 0;
665 }
666
hns3_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)667 static void hns3_get_ringparam(struct net_device *netdev,
668 struct ethtool_ringparam *param,
669 struct kernel_ethtool_ringparam *kernel_param,
670 struct netlink_ext_ack *extack)
671 {
672 struct hns3_nic_priv *priv = netdev_priv(netdev);
673 struct hnae3_handle *h = priv->ae_handle;
674 int rx_queue_index = h->kinfo.num_tqps;
675
676 if (hns3_nic_resetting(netdev) || !priv->ring) {
677 netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n");
678 return;
679 }
680
681 param->tx_max_pending = HNS3_RING_MAX_PENDING;
682 param->rx_max_pending = HNS3_RING_MAX_PENDING;
683
684 param->tx_pending = priv->ring[0].desc_num;
685 param->rx_pending = priv->ring[rx_queue_index].desc_num;
686 kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
687 kernel_param->tx_push = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE,
688 &priv->state);
689 }
690
hns3_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * param)691 static void hns3_get_pauseparam(struct net_device *netdev,
692 struct ethtool_pauseparam *param)
693 {
694 struct hnae3_handle *h = hns3_get_handle(netdev);
695 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
696
697 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
698 return;
699
700 if (h->ae_algo->ops->get_pauseparam)
701 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
702 ¶m->rx_pause, ¶m->tx_pause);
703 }
704
hns3_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * param)705 static int hns3_set_pauseparam(struct net_device *netdev,
706 struct ethtool_pauseparam *param)
707 {
708 struct hnae3_handle *h = hns3_get_handle(netdev);
709 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
710
711 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
712 return -EOPNOTSUPP;
713
714 netif_dbg(h, drv, netdev,
715 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
716 param->autoneg, param->rx_pause, param->tx_pause);
717
718 if (h->ae_algo->ops->set_pauseparam)
719 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
720 param->rx_pause,
721 param->tx_pause);
722 return -EOPNOTSUPP;
723 }
724
hns3_get_ksettings(struct hnae3_handle * h,struct ethtool_link_ksettings * cmd)725 static void hns3_get_ksettings(struct hnae3_handle *h,
726 struct ethtool_link_ksettings *cmd)
727 {
728 const struct hnae3_ae_ops *ops = hns3_get_ops(h);
729
730 /* 1.auto_neg & speed & duplex from cmd */
731 if (ops->get_ksettings_an_result)
732 ops->get_ksettings_an_result(h,
733 &cmd->base.autoneg,
734 &cmd->base.speed,
735 &cmd->base.duplex,
736 &cmd->lanes);
737
738 /* 2.get link mode */
739 if (ops->get_link_mode)
740 ops->get_link_mode(h,
741 cmd->link_modes.supported,
742 cmd->link_modes.advertising);
743
744 /* 3.mdix_ctrl&mdix get from phy reg */
745 if (ops->get_mdix_mode)
746 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
747 &cmd->base.eth_tp_mdix);
748 }
749
hns3_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)750 static int hns3_get_link_ksettings(struct net_device *netdev,
751 struct ethtool_link_ksettings *cmd)
752 {
753 struct hnae3_handle *h = hns3_get_handle(netdev);
754 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
755 const struct hnae3_ae_ops *ops;
756 u8 module_type;
757 u8 media_type;
758 u8 link_stat;
759
760 ops = h->ae_algo->ops;
761 if (ops->get_media_type)
762 ops->get_media_type(h, &media_type, &module_type);
763 else
764 return -EOPNOTSUPP;
765
766 switch (media_type) {
767 case HNAE3_MEDIA_TYPE_NONE:
768 cmd->base.port = PORT_NONE;
769 hns3_get_ksettings(h, cmd);
770 break;
771 case HNAE3_MEDIA_TYPE_FIBER:
772 if (module_type == HNAE3_MODULE_TYPE_UNKNOWN)
773 cmd->base.port = PORT_OTHER;
774 else if (module_type == HNAE3_MODULE_TYPE_CR)
775 cmd->base.port = PORT_DA;
776 else
777 cmd->base.port = PORT_FIBRE;
778
779 hns3_get_ksettings(h, cmd);
780 break;
781 case HNAE3_MEDIA_TYPE_BACKPLANE:
782 cmd->base.port = PORT_NONE;
783 hns3_get_ksettings(h, cmd);
784 break;
785 case HNAE3_MEDIA_TYPE_COPPER:
786 cmd->base.port = PORT_TP;
787 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
788 ops->get_phy_link_ksettings)
789 ops->get_phy_link_ksettings(h, cmd);
790 else if (!netdev->phydev)
791 hns3_get_ksettings(h, cmd);
792 else
793 phy_ethtool_ksettings_get(netdev->phydev, cmd);
794 break;
795 default:
796
797 netdev_warn(netdev, "Unknown media type\n");
798 return 0;
799 }
800
801 /* mdio_support */
802 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
803
804 link_stat = hns3_get_link(netdev);
805 if (!link_stat) {
806 cmd->base.speed = SPEED_UNKNOWN;
807 cmd->base.duplex = DUPLEX_UNKNOWN;
808 }
809
810 return 0;
811 }
812
hns3_check_ksettings_param(const struct net_device * netdev,const struct ethtool_link_ksettings * cmd)813 static int hns3_check_ksettings_param(const struct net_device *netdev,
814 const struct ethtool_link_ksettings *cmd)
815 {
816 struct hnae3_handle *handle = hns3_get_handle(netdev);
817 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
818 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
819 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
820 u32 lane_num;
821 u8 autoneg;
822 u32 speed;
823 u8 duplex;
824 int ret;
825
826 /* hw doesn't support use specified speed and duplex to negotiate,
827 * unnecessary to check them when autoneg on.
828 */
829 if (cmd->base.autoneg)
830 return 0;
831
832 if (ops->get_ksettings_an_result) {
833 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex, &lane_num);
834 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
835 cmd->base.duplex == duplex && cmd->lanes == lane_num)
836 return 0;
837 }
838
839 if (ops->get_media_type)
840 ops->get_media_type(handle, &media_type, &module_type);
841
842 if (cmd->base.duplex == DUPLEX_HALF &&
843 media_type != HNAE3_MEDIA_TYPE_COPPER) {
844 netdev_err(netdev,
845 "only copper port supports half duplex!\n");
846 return -EINVAL;
847 }
848
849 if (ops->check_port_speed) {
850 ret = ops->check_port_speed(handle, cmd->base.speed);
851 if (ret) {
852 netdev_err(netdev, "unsupported speed\n");
853 return ret;
854 }
855 }
856
857 return 0;
858 }
859
hns3_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * cmd)860 static int hns3_set_link_ksettings(struct net_device *netdev,
861 const struct ethtool_link_ksettings *cmd)
862 {
863 struct hnae3_handle *handle = hns3_get_handle(netdev);
864 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
865 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
866 int ret;
867
868 /* Chip don't support this mode. */
869 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
870 return -EINVAL;
871
872 if (cmd->lanes && !hnae3_ae_dev_lane_num_supported(ae_dev))
873 return -EOPNOTSUPP;
874
875 netif_dbg(handle, drv, netdev,
876 "set link(%s): autoneg=%u, speed=%u, duplex=%u, lanes=%u\n",
877 netdev->phydev ? "phy" : "mac",
878 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex,
879 cmd->lanes);
880
881 /* Only support ksettings_set for netdev with phy attached for now */
882 if (netdev->phydev) {
883 if (cmd->base.speed == SPEED_1000 &&
884 cmd->base.autoneg == AUTONEG_DISABLE)
885 return -EINVAL;
886
887 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
888 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
889 ops->set_phy_link_ksettings) {
890 return ops->set_phy_link_ksettings(handle, cmd);
891 }
892
893 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
894 return -EOPNOTSUPP;
895
896 ret = hns3_check_ksettings_param(netdev, cmd);
897 if (ret)
898 return ret;
899
900 if (ops->set_autoneg) {
901 ret = ops->set_autoneg(handle, cmd->base.autoneg);
902 if (ret)
903 return ret;
904 }
905
906 /* hw doesn't support use specified speed and duplex to negotiate,
907 * ignore them when autoneg on.
908 */
909 if (cmd->base.autoneg) {
910 netdev_info(netdev,
911 "autoneg is on, ignore the speed and duplex\n");
912 return 0;
913 }
914
915 if (ops->cfg_mac_speed_dup_h)
916 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
917 cmd->base.duplex, (u8)(cmd->lanes));
918
919 return ret;
920 }
921
hns3_get_rss_key_size(struct net_device * netdev)922 static u32 hns3_get_rss_key_size(struct net_device *netdev)
923 {
924 struct hnae3_handle *h = hns3_get_handle(netdev);
925
926 if (!h->ae_algo->ops->get_rss_key_size)
927 return 0;
928
929 return h->ae_algo->ops->get_rss_key_size(h);
930 }
931
hns3_get_rss_indir_size(struct net_device * netdev)932 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
933 {
934 struct hnae3_handle *h = hns3_get_handle(netdev);
935 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
936
937 return ae_dev->dev_specs.rss_ind_tbl_size;
938 }
939
hns3_get_rss(struct net_device * netdev,struct ethtool_rxfh_param * rxfh)940 static int hns3_get_rss(struct net_device *netdev,
941 struct ethtool_rxfh_param *rxfh)
942 {
943 struct hnae3_handle *h = hns3_get_handle(netdev);
944
945 if (!h->ae_algo->ops->get_rss)
946 return -EOPNOTSUPP;
947
948 return h->ae_algo->ops->get_rss(h, rxfh->indir, rxfh->key,
949 &rxfh->hfunc);
950 }
951
hns3_set_rss(struct net_device * netdev,struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)952 static int hns3_set_rss(struct net_device *netdev,
953 struct ethtool_rxfh_param *rxfh,
954 struct netlink_ext_ack *extack)
955 {
956 struct hnae3_handle *h = hns3_get_handle(netdev);
957 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
958
959 if (!h->ae_algo->ops->set_rss)
960 return -EOPNOTSUPP;
961
962 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
963 rxfh->hfunc != ETH_RSS_HASH_TOP) ||
964 (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
965 rxfh->hfunc != ETH_RSS_HASH_TOP &&
966 rxfh->hfunc != ETH_RSS_HASH_XOR)) {
967 netdev_err(netdev, "hash func not supported\n");
968 return -EOPNOTSUPP;
969 }
970
971 if (!rxfh->indir) {
972 netdev_err(netdev,
973 "set rss failed for indir is empty\n");
974 return -EOPNOTSUPP;
975 }
976
977 return h->ae_algo->ops->set_rss(h, rxfh->indir, rxfh->key,
978 rxfh->hfunc);
979 }
980
hns3_get_rxfh_fields(struct net_device * netdev,struct ethtool_rxfh_fields * cmd)981 static int hns3_get_rxfh_fields(struct net_device *netdev,
982 struct ethtool_rxfh_fields *cmd)
983 {
984 struct hnae3_handle *h = hns3_get_handle(netdev);
985
986 if (h->ae_algo->ops->get_rss_tuple)
987 return h->ae_algo->ops->get_rss_tuple(h, cmd);
988 return -EOPNOTSUPP;
989 }
990
hns3_get_rx_ring_count(struct net_device * netdev)991 static u32 hns3_get_rx_ring_count(struct net_device *netdev)
992 {
993 struct hnae3_handle *h = hns3_get_handle(netdev);
994
995 return h->kinfo.num_tqps;
996 }
997
hns3_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)998 static int hns3_get_rxnfc(struct net_device *netdev,
999 struct ethtool_rxnfc *cmd,
1000 u32 *rule_locs)
1001 {
1002 struct hnae3_handle *h = hns3_get_handle(netdev);
1003
1004 switch (cmd->cmd) {
1005 case ETHTOOL_GRXCLSRLCNT:
1006 if (h->ae_algo->ops->get_fd_rule_cnt)
1007 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
1008 return -EOPNOTSUPP;
1009 case ETHTOOL_GRXCLSRULE:
1010 if (h->ae_algo->ops->get_fd_rule_info)
1011 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
1012 return -EOPNOTSUPP;
1013 case ETHTOOL_GRXCLSRLALL:
1014 if (h->ae_algo->ops->get_fd_all_rules)
1015 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
1016 rule_locs);
1017 return -EOPNOTSUPP;
1018 default:
1019 return -EOPNOTSUPP;
1020 }
1021 }
1022
1023 static const struct hns3_reset_type_map hns3_reset_type[] = {
1024 {ETH_RESET_MGMT, HNAE3_IMP_RESET},
1025 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
1026 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
1027 };
1028
1029 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
1030 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
1031 };
1032
hns3_set_reset(struct net_device * netdev,u32 * flags)1033 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
1034 {
1035 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
1036 struct hnae3_handle *h = hns3_get_handle(netdev);
1037 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
1038 const struct hnae3_ae_ops *ops = hns3_get_ops(h);
1039 const struct hns3_reset_type_map *rst_type_map;
1040 enum ethtool_reset_flags rst_flags;
1041 u32 i, size;
1042
1043 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
1044 return -EBUSY;
1045
1046 if (!ops->set_default_reset_request || !ops->reset_event)
1047 return -EOPNOTSUPP;
1048
1049 if (h->flags & HNAE3_SUPPORT_VF) {
1050 rst_type_map = hns3vf_reset_type;
1051 size = ARRAY_SIZE(hns3vf_reset_type);
1052 } else {
1053 rst_type_map = hns3_reset_type;
1054 size = ARRAY_SIZE(hns3_reset_type);
1055 }
1056
1057 for (i = 0; i < size; i++) {
1058 if (rst_type_map[i].rst_flags == *flags) {
1059 rst_type = rst_type_map[i].rst_type;
1060 rst_flags = rst_type_map[i].rst_flags;
1061 break;
1062 }
1063 }
1064
1065 if (rst_type == HNAE3_NONE_RESET ||
1066 (rst_type == HNAE3_IMP_RESET &&
1067 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1068 return -EOPNOTSUPP;
1069
1070 netdev_info(netdev, "Setting reset type %d\n", rst_type);
1071
1072 ops->set_default_reset_request(ae_dev, rst_type);
1073
1074 ops->reset_event(h->pdev, h);
1075
1076 *flags &= ~rst_flags;
1077
1078 return 0;
1079 }
1080
hns3_change_all_ring_bd_num(struct hns3_nic_priv * priv,u32 tx_desc_num,u32 rx_desc_num)1081 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1082 u32 tx_desc_num, u32 rx_desc_num)
1083 {
1084 struct hnae3_handle *h = priv->ae_handle;
1085 int i;
1086
1087 h->kinfo.num_tx_desc = tx_desc_num;
1088 h->kinfo.num_rx_desc = rx_desc_num;
1089
1090 for (i = 0; i < h->kinfo.num_tqps; i++) {
1091 priv->ring[i].desc_num = tx_desc_num;
1092 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1093 }
1094 }
1095
hns3_backup_ringparam(struct hns3_nic_priv * priv)1096 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1097 {
1098 struct hnae3_handle *handle = priv->ae_handle;
1099 struct hns3_enet_ring *tmp_rings;
1100 int i;
1101
1102 tmp_rings = kzalloc_objs(struct hns3_enet_ring,
1103 handle->kinfo.num_tqps * 2);
1104 if (!tmp_rings)
1105 return NULL;
1106
1107 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1108 memcpy(&tmp_rings[i], &priv->ring[i],
1109 sizeof(struct hns3_enet_ring));
1110 tmp_rings[i].skb = NULL;
1111 }
1112
1113 return tmp_rings;
1114 }
1115
hns3_check_ringparam(struct net_device * ndev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param)1116 static int hns3_check_ringparam(struct net_device *ndev,
1117 struct ethtool_ringparam *param,
1118 struct kernel_ethtool_ringparam *kernel_param)
1119 {
1120 #define RX_BUF_LEN_2K 2048
1121 #define RX_BUF_LEN_4K 4096
1122
1123 struct hns3_nic_priv *priv = netdev_priv(ndev);
1124
1125 if (hns3_nic_resetting(ndev) || !priv->ring) {
1126 netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n");
1127 return -EBUSY;
1128 }
1129
1130
1131 if (param->rx_mini_pending || param->rx_jumbo_pending)
1132 return -EINVAL;
1133
1134 if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
1135 kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
1136 netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
1137 return -EINVAL;
1138 }
1139
1140 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1141 param->tx_pending < HNS3_RING_MIN_PENDING ||
1142 param->rx_pending > HNS3_RING_MAX_PENDING ||
1143 param->rx_pending < HNS3_RING_MIN_PENDING) {
1144 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1145 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1146 return -EINVAL;
1147 }
1148
1149 return 0;
1150 }
1151
1152 static bool
hns3_is_ringparam_changed(struct net_device * ndev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct hns3_ring_param * old_ringparam,struct hns3_ring_param * new_ringparam)1153 hns3_is_ringparam_changed(struct net_device *ndev,
1154 struct ethtool_ringparam *param,
1155 struct kernel_ethtool_ringparam *kernel_param,
1156 struct hns3_ring_param *old_ringparam,
1157 struct hns3_ring_param *new_ringparam)
1158 {
1159 struct hns3_nic_priv *priv = netdev_priv(ndev);
1160 struct hnae3_handle *h = priv->ae_handle;
1161 u16 queue_num = h->kinfo.num_tqps;
1162
1163 new_ringparam->tx_desc_num = ALIGN(param->tx_pending,
1164 HNS3_RING_BD_MULTIPLE);
1165 new_ringparam->rx_desc_num = ALIGN(param->rx_pending,
1166 HNS3_RING_BD_MULTIPLE);
1167 old_ringparam->tx_desc_num = priv->ring[0].desc_num;
1168 old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num;
1169 old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size;
1170 new_ringparam->rx_buf_len = kernel_param->rx_buf_len;
1171
1172 if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num &&
1173 old_ringparam->rx_desc_num == new_ringparam->rx_desc_num &&
1174 old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) {
1175 netdev_info(ndev, "descriptor number and rx buffer length not changed\n");
1176 return false;
1177 }
1178
1179 return true;
1180 }
1181
hns3_change_rx_buf_len(struct net_device * ndev,u32 rx_buf_len)1182 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
1183 {
1184 struct hns3_nic_priv *priv = netdev_priv(ndev);
1185 struct hnae3_handle *h = priv->ae_handle;
1186 int i;
1187
1188 h->kinfo.rx_buf_len = rx_buf_len;
1189
1190 for (i = 0; i < h->kinfo.num_tqps; i++) {
1191 h->kinfo.tqp[i]->buf_size = rx_buf_len;
1192 priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
1193 }
1194
1195 return 0;
1196 }
1197
hns3_set_tx_push(struct net_device * netdev,u32 tx_push)1198 static int hns3_set_tx_push(struct net_device *netdev, u32 tx_push)
1199 {
1200 struct hns3_nic_priv *priv = netdev_priv(netdev);
1201 struct hnae3_handle *h = hns3_get_handle(netdev);
1202 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(h);
1203 u32 old_state = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1204
1205 if (!test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps) && tx_push)
1206 return -EOPNOTSUPP;
1207
1208 if (tx_push == old_state)
1209 return 0;
1210
1211 netdev_dbg(netdev, "Changing tx push from %s to %s\n",
1212 str_on_off(old_state), str_on_off(tx_push));
1213
1214 if (tx_push)
1215 set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1216 else
1217 clear_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
1218
1219 return 0;
1220 }
1221
hns3_set_ringparam(struct net_device * ndev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)1222 static int hns3_set_ringparam(struct net_device *ndev,
1223 struct ethtool_ringparam *param,
1224 struct kernel_ethtool_ringparam *kernel_param,
1225 struct netlink_ext_ack *extack)
1226 {
1227 struct hns3_ring_param old_ringparam, new_ringparam;
1228 struct hns3_nic_priv *priv = netdev_priv(ndev);
1229 struct hnae3_handle *h = priv->ae_handle;
1230 struct hns3_enet_ring *tmp_rings;
1231 bool if_running = netif_running(ndev);
1232 int ret, i;
1233
1234 ret = hns3_check_ringparam(ndev, param, kernel_param);
1235 if (ret)
1236 return ret;
1237
1238 ret = hns3_set_tx_push(ndev, kernel_param->tx_push);
1239 if (ret)
1240 return ret;
1241
1242 if (!hns3_is_ringparam_changed(ndev, param, kernel_param,
1243 &old_ringparam, &new_ringparam))
1244 return 0;
1245
1246 tmp_rings = hns3_backup_ringparam(priv);
1247 if (!tmp_rings) {
1248 netdev_err(ndev, "backup ring param failed by allocating memory fail\n");
1249 return -ENOMEM;
1250 }
1251
1252 netdev_info(ndev,
1253 "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n",
1254 old_ringparam.tx_desc_num, old_ringparam.rx_desc_num,
1255 new_ringparam.tx_desc_num, new_ringparam.rx_desc_num,
1256 old_ringparam.rx_buf_len, new_ringparam.rx_buf_len);
1257
1258 if (if_running)
1259 ndev->netdev_ops->ndo_stop(ndev);
1260
1261 hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
1262 new_ringparam.rx_desc_num);
1263 hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len);
1264 ret = hns3_init_all_ring(priv);
1265 if (ret) {
1266 netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1267 ret);
1268
1269 hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len);
1270 hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num,
1271 old_ringparam.rx_desc_num);
1272 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1273 memcpy(&priv->ring[i], &tmp_rings[i],
1274 sizeof(struct hns3_enet_ring));
1275 } else {
1276 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1277 hns3_fini_ring(&tmp_rings[i]);
1278 }
1279
1280 kfree(tmp_rings);
1281
1282 if (if_running)
1283 ret = ndev->netdev_ops->ndo_open(ndev);
1284
1285 return ret;
1286 }
1287
hns3_set_rxfh_fields(struct net_device * netdev,const struct ethtool_rxfh_fields * cmd,struct netlink_ext_ack * extack)1288 static int hns3_set_rxfh_fields(struct net_device *netdev,
1289 const struct ethtool_rxfh_fields *cmd,
1290 struct netlink_ext_ack *extack)
1291 {
1292 struct hnae3_handle *h = hns3_get_handle(netdev);
1293
1294 if (h->ae_algo->ops->set_rss_tuple)
1295 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1296 return -EOPNOTSUPP;
1297 }
1298
hns3_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)1299 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1300 {
1301 struct hnae3_handle *h = hns3_get_handle(netdev);
1302
1303 switch (cmd->cmd) {
1304 case ETHTOOL_SRXCLSRLINS:
1305 if (h->ae_algo->ops->add_fd_entry)
1306 return h->ae_algo->ops->add_fd_entry(h, cmd);
1307 return -EOPNOTSUPP;
1308 case ETHTOOL_SRXCLSRLDEL:
1309 if (h->ae_algo->ops->del_fd_entry)
1310 return h->ae_algo->ops->del_fd_entry(h, cmd);
1311 return -EOPNOTSUPP;
1312 default:
1313 return -EOPNOTSUPP;
1314 }
1315 }
1316
hns3_nway_reset(struct net_device * netdev)1317 static int hns3_nway_reset(struct net_device *netdev)
1318 {
1319 struct hnae3_handle *handle = hns3_get_handle(netdev);
1320 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1321 struct phy_device *phy = netdev->phydev;
1322 int autoneg;
1323
1324 if (!netif_running(netdev))
1325 return 0;
1326
1327 if (hns3_nic_resetting(netdev)) {
1328 netdev_err(netdev, "dev resetting!\n");
1329 return -EBUSY;
1330 }
1331
1332 if (!ops->get_autoneg || !ops->restart_autoneg)
1333 return -EOPNOTSUPP;
1334
1335 autoneg = ops->get_autoneg(handle);
1336 if (autoneg != AUTONEG_ENABLE) {
1337 netdev_err(netdev,
1338 "Autoneg is off, don't support to restart it\n");
1339 return -EINVAL;
1340 }
1341
1342 netif_dbg(handle, drv, netdev,
1343 "nway reset (using %s)\n", phy ? "phy" : "mac");
1344
1345 if (phy)
1346 return genphy_restart_aneg(phy);
1347
1348 return ops->restart_autoneg(handle);
1349 }
1350
hns3_get_channels(struct net_device * netdev,struct ethtool_channels * ch)1351 static void hns3_get_channels(struct net_device *netdev,
1352 struct ethtool_channels *ch)
1353 {
1354 struct hnae3_handle *h = hns3_get_handle(netdev);
1355
1356 if (h->ae_algo->ops->get_channels)
1357 h->ae_algo->ops->get_channels(h, ch);
1358 }
1359
hns3_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1360 static int hns3_get_coalesce(struct net_device *netdev,
1361 struct ethtool_coalesce *cmd,
1362 struct kernel_ethtool_coalesce *kernel_coal,
1363 struct netlink_ext_ack *extack)
1364 {
1365 struct hns3_nic_priv *priv = netdev_priv(netdev);
1366 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1367 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1368 struct hnae3_handle *h = priv->ae_handle;
1369
1370 if (hns3_nic_resetting(netdev))
1371 return -EBUSY;
1372
1373 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1374 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1375
1376 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1377 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1378
1379 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1380 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1381
1382 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1383 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1384
1385 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1386 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1387 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1388 DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1389
1390 return 0;
1391 }
1392
hns3_check_gl_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd)1393 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1394 struct ethtool_coalesce *cmd)
1395 {
1396 struct hnae3_handle *handle = hns3_get_handle(netdev);
1397 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1398 u32 rx_gl, tx_gl;
1399
1400 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1401 netdev_err(netdev,
1402 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1403 ae_dev->dev_specs.max_int_gl);
1404 return -EINVAL;
1405 }
1406
1407 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1408 netdev_err(netdev,
1409 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1410 ae_dev->dev_specs.max_int_gl);
1411 return -EINVAL;
1412 }
1413
1414 /* device version above V3(include V3), GL uses 1us unit,
1415 * so the round down is not needed.
1416 */
1417 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1418 return 0;
1419
1420 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1421 if (rx_gl != cmd->rx_coalesce_usecs) {
1422 netdev_info(netdev,
1423 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1424 cmd->rx_coalesce_usecs, rx_gl);
1425 }
1426
1427 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1428 if (tx_gl != cmd->tx_coalesce_usecs) {
1429 netdev_info(netdev,
1430 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1431 cmd->tx_coalesce_usecs, tx_gl);
1432 }
1433
1434 return 0;
1435 }
1436
hns3_check_rl_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd)1437 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1438 struct ethtool_coalesce *cmd)
1439 {
1440 u32 rl;
1441
1442 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1443 netdev_err(netdev,
1444 "tx_usecs_high must be same as rx_usecs_high.\n");
1445 return -EINVAL;
1446 }
1447
1448 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1449 netdev_err(netdev,
1450 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1451 HNS3_INT_RL_MAX);
1452 return -EINVAL;
1453 }
1454
1455 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1456 if (rl != cmd->rx_coalesce_usecs_high) {
1457 netdev_info(netdev,
1458 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1459 cmd->rx_coalesce_usecs_high, rl);
1460 }
1461
1462 return 0;
1463 }
1464
hns3_check_ql_coalesce_param(struct net_device * netdev,struct ethtool_coalesce * cmd)1465 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1466 struct ethtool_coalesce *cmd)
1467 {
1468 struct hnae3_handle *handle = hns3_get_handle(netdev);
1469 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1470
1471 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1472 !ae_dev->dev_specs.int_ql_max) {
1473 netdev_err(netdev, "coalesced frames is not supported\n");
1474 return -EOPNOTSUPP;
1475 }
1476
1477 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1478 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1479 netdev_err(netdev,
1480 "invalid coalesced_frames value, range is 0-%u\n",
1481 ae_dev->dev_specs.int_ql_max);
1482 return -ERANGE;
1483 }
1484
1485 return 0;
1486 }
1487
1488 static int
hns3_check_cqe_coalesce_param(struct net_device * netdev,struct kernel_ethtool_coalesce * kernel_coal)1489 hns3_check_cqe_coalesce_param(struct net_device *netdev,
1490 struct kernel_ethtool_coalesce *kernel_coal)
1491 {
1492 struct hnae3_handle *handle = hns3_get_handle(netdev);
1493 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1494
1495 if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) &&
1496 !hnae3_ae_dev_cq_supported(ae_dev)) {
1497 netdev_err(netdev, "coalesced cqe mode is not supported\n");
1498 return -EOPNOTSUPP;
1499 }
1500
1501 return 0;
1502 }
1503
1504 static int
hns3_check_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd,struct kernel_ethtool_coalesce * kernel_coal)1505 hns3_check_coalesce_para(struct net_device *netdev,
1506 struct ethtool_coalesce *cmd,
1507 struct kernel_ethtool_coalesce *kernel_coal)
1508 {
1509 int ret;
1510
1511 ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal);
1512 if (ret)
1513 return ret;
1514
1515 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1516 if (ret) {
1517 netdev_err(netdev,
1518 "Check gl coalesce param fail. ret = %d\n", ret);
1519 return ret;
1520 }
1521
1522 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1523 if (ret) {
1524 netdev_err(netdev,
1525 "Check rl coalesce param fail. ret = %d\n", ret);
1526 return ret;
1527 }
1528
1529 return hns3_check_ql_coalesce_param(netdev, cmd);
1530 }
1531
hns3_set_coalesce_per_queue(struct net_device * netdev,struct ethtool_coalesce * cmd,u32 queue)1532 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1533 struct ethtool_coalesce *cmd,
1534 u32 queue)
1535 {
1536 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1537 struct hns3_nic_priv *priv = netdev_priv(netdev);
1538 struct hnae3_handle *h = priv->ae_handle;
1539 int queue_num = h->kinfo.num_tqps;
1540
1541 tx_vector = priv->ring[queue].tqp_vector;
1542 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1543
1544 tx_vector->tx_group.coal.adapt_enable =
1545 cmd->use_adaptive_tx_coalesce;
1546 rx_vector->rx_group.coal.adapt_enable =
1547 cmd->use_adaptive_rx_coalesce;
1548
1549 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1550 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1551
1552 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1553 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1554
1555 hns3_set_vector_coalesce_tx_gl(tx_vector,
1556 tx_vector->tx_group.coal.int_gl);
1557 hns3_set_vector_coalesce_rx_gl(rx_vector,
1558 rx_vector->rx_group.coal.int_gl);
1559
1560 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1561 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1562
1563 if (tx_vector->tx_group.coal.ql_enable)
1564 hns3_set_vector_coalesce_tx_ql(tx_vector,
1565 tx_vector->tx_group.coal.int_ql);
1566 if (rx_vector->rx_group.coal.ql_enable)
1567 hns3_set_vector_coalesce_rx_ql(rx_vector,
1568 rx_vector->rx_group.coal.int_ql);
1569 }
1570
hns3_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1571 static int hns3_set_coalesce(struct net_device *netdev,
1572 struct ethtool_coalesce *cmd,
1573 struct kernel_ethtool_coalesce *kernel_coal,
1574 struct netlink_ext_ack *extack)
1575 {
1576 struct hnae3_handle *h = hns3_get_handle(netdev);
1577 struct hns3_nic_priv *priv = netdev_priv(netdev);
1578 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1579 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1580 u16 queue_num = h->kinfo.num_tqps;
1581 enum dim_cq_period_mode tx_mode;
1582 enum dim_cq_period_mode rx_mode;
1583 int ret;
1584 int i;
1585
1586 if (hns3_nic_resetting(netdev))
1587 return -EBUSY;
1588
1589 ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal);
1590 if (ret)
1591 return ret;
1592
1593 h->kinfo.int_rl_setting =
1594 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1595
1596 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1597 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1598
1599 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1600 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1601
1602 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1603 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1604
1605 for (i = 0; i < queue_num; i++)
1606 hns3_set_coalesce_per_queue(netdev, cmd, i);
1607
1608 tx_mode = kernel_coal->use_cqe_mode_tx ?
1609 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1610 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1611 rx_mode = kernel_coal->use_cqe_mode_rx ?
1612 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1613 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1614 hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1615
1616 return 0;
1617 }
1618
hns3_get_regs_len(struct net_device * netdev)1619 static int hns3_get_regs_len(struct net_device *netdev)
1620 {
1621 struct hnae3_handle *h = hns3_get_handle(netdev);
1622
1623 if (!h->ae_algo->ops->get_regs_len)
1624 return -EOPNOTSUPP;
1625
1626 return h->ae_algo->ops->get_regs_len(h);
1627 }
1628
hns3_get_regs(struct net_device * netdev,struct ethtool_regs * cmd,void * data)1629 static void hns3_get_regs(struct net_device *netdev,
1630 struct ethtool_regs *cmd, void *data)
1631 {
1632 struct hnae3_handle *h = hns3_get_handle(netdev);
1633
1634 if (!h->ae_algo->ops->get_regs)
1635 return;
1636
1637 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1638 }
1639
hns3_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1640 static int hns3_set_phys_id(struct net_device *netdev,
1641 enum ethtool_phys_id_state state)
1642 {
1643 struct hnae3_handle *h = hns3_get_handle(netdev);
1644
1645 if (!h->ae_algo->ops->set_led_id)
1646 return -EOPNOTSUPP;
1647
1648 return h->ae_algo->ops->set_led_id(h, state);
1649 }
1650
hns3_get_msglevel(struct net_device * netdev)1651 static u32 hns3_get_msglevel(struct net_device *netdev)
1652 {
1653 struct hnae3_handle *h = hns3_get_handle(netdev);
1654
1655 return h->msg_enable;
1656 }
1657
hns3_set_msglevel(struct net_device * netdev,u32 msg_level)1658 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1659 {
1660 struct hnae3_handle *h = hns3_get_handle(netdev);
1661
1662 h->msg_enable = msg_level;
1663 }
1664
hns3_get_fec_stats(struct net_device * netdev,struct ethtool_fec_stats * fec_stats,struct ethtool_fec_hist * hist)1665 static void hns3_get_fec_stats(struct net_device *netdev,
1666 struct ethtool_fec_stats *fec_stats,
1667 struct ethtool_fec_hist *hist)
1668 {
1669 struct hnae3_handle *handle = hns3_get_handle(netdev);
1670 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1671 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1672
1673 if (!hnae3_ae_dev_fec_stats_supported(ae_dev) || !ops->get_fec_stats)
1674 return;
1675
1676 ops->get_fec_stats(handle, fec_stats);
1677 }
1678
1679 /* Translate local fec value into ethtool value. */
loc_to_eth_fec(u8 loc_fec)1680 static unsigned int loc_to_eth_fec(u8 loc_fec)
1681 {
1682 u32 eth_fec = 0;
1683
1684 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1685 eth_fec |= ETHTOOL_FEC_AUTO;
1686 if (loc_fec & BIT(HNAE3_FEC_RS))
1687 eth_fec |= ETHTOOL_FEC_RS;
1688 if (loc_fec & BIT(HNAE3_FEC_LLRS))
1689 eth_fec |= ETHTOOL_FEC_LLRS;
1690 if (loc_fec & BIT(HNAE3_FEC_BASER))
1691 eth_fec |= ETHTOOL_FEC_BASER;
1692 if (loc_fec & BIT(HNAE3_FEC_NONE))
1693 eth_fec |= ETHTOOL_FEC_OFF;
1694
1695 return eth_fec;
1696 }
1697
1698 /* Translate ethtool fec value into local value. */
eth_to_loc_fec(unsigned int eth_fec)1699 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1700 {
1701 u32 loc_fec = 0;
1702
1703 if (eth_fec & ETHTOOL_FEC_OFF)
1704 loc_fec |= BIT(HNAE3_FEC_NONE);
1705 if (eth_fec & ETHTOOL_FEC_AUTO)
1706 loc_fec |= BIT(HNAE3_FEC_AUTO);
1707 if (eth_fec & ETHTOOL_FEC_RS)
1708 loc_fec |= BIT(HNAE3_FEC_RS);
1709 if (eth_fec & ETHTOOL_FEC_LLRS)
1710 loc_fec |= BIT(HNAE3_FEC_LLRS);
1711 if (eth_fec & ETHTOOL_FEC_BASER)
1712 loc_fec |= BIT(HNAE3_FEC_BASER);
1713
1714 return loc_fec;
1715 }
1716
hns3_get_fecparam(struct net_device * netdev,struct ethtool_fecparam * fec)1717 static int hns3_get_fecparam(struct net_device *netdev,
1718 struct ethtool_fecparam *fec)
1719 {
1720 struct hnae3_handle *handle = hns3_get_handle(netdev);
1721 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1722 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1723 u8 fec_ability;
1724 u8 fec_mode;
1725
1726 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1727 return -EOPNOTSUPP;
1728
1729 if (!ops->get_fec)
1730 return -EOPNOTSUPP;
1731
1732 ops->get_fec(handle, &fec_ability, &fec_mode);
1733
1734 fec->fec = loc_to_eth_fec(fec_ability);
1735 fec->active_fec = loc_to_eth_fec(fec_mode);
1736 if (!fec->active_fec)
1737 fec->active_fec = ETHTOOL_FEC_OFF;
1738
1739 return 0;
1740 }
1741
hns3_set_fecparam(struct net_device * netdev,struct ethtool_fecparam * fec)1742 static int hns3_set_fecparam(struct net_device *netdev,
1743 struct ethtool_fecparam *fec)
1744 {
1745 struct hnae3_handle *handle = hns3_get_handle(netdev);
1746 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1747 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1748 u32 fec_mode;
1749
1750 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1751 return -EOPNOTSUPP;
1752
1753 if (!ops->set_fec)
1754 return -EOPNOTSUPP;
1755 fec_mode = eth_to_loc_fec(fec->fec);
1756
1757 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1758
1759 return ops->set_fec(handle, fec_mode);
1760 }
1761
hns3_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)1762 static int hns3_get_module_info(struct net_device *netdev,
1763 struct ethtool_modinfo *modinfo)
1764 {
1765 #define HNS3_SFF_8636_V1_3 0x03
1766
1767 struct hnae3_handle *handle = hns3_get_handle(netdev);
1768 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1769 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1770 struct hns3_sfp_type sfp_type;
1771 int ret;
1772
1773 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1774 !ops->get_module_eeprom)
1775 return -EOPNOTSUPP;
1776
1777 memset(&sfp_type, 0, sizeof(sfp_type));
1778 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1779 (u8 *)&sfp_type);
1780 if (ret)
1781 return ret;
1782
1783 switch (sfp_type.type) {
1784 case SFF8024_ID_SFP:
1785 modinfo->type = ETH_MODULE_SFF_8472;
1786 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1787 break;
1788 case SFF8024_ID_QSFP_8438:
1789 modinfo->type = ETH_MODULE_SFF_8436;
1790 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1791 break;
1792 case SFF8024_ID_QSFP_8436_8636:
1793 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1794 modinfo->type = ETH_MODULE_SFF_8436;
1795 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1796 } else {
1797 modinfo->type = ETH_MODULE_SFF_8636;
1798 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1799 }
1800 break;
1801 case SFF8024_ID_QSFP28_8636:
1802 modinfo->type = ETH_MODULE_SFF_8636;
1803 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1804 break;
1805 default:
1806 netdev_err(netdev, "Optical module unknown: %#x\n",
1807 sfp_type.type);
1808 return -EINVAL;
1809 }
1810
1811 return 0;
1812 }
1813
hns3_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)1814 static int hns3_get_module_eeprom(struct net_device *netdev,
1815 struct ethtool_eeprom *ee, u8 *data)
1816 {
1817 struct hnae3_handle *handle = hns3_get_handle(netdev);
1818 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
1819 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
1820
1821 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1822 !ops->get_module_eeprom)
1823 return -EOPNOTSUPP;
1824
1825 if (!ee->len)
1826 return -EINVAL;
1827
1828 memset(data, 0, ee->len);
1829
1830 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1831 }
1832
hns3_get_priv_flags(struct net_device * netdev)1833 static u32 hns3_get_priv_flags(struct net_device *netdev)
1834 {
1835 struct hnae3_handle *handle = hns3_get_handle(netdev);
1836
1837 return handle->priv_flags;
1838 }
1839
hns3_check_priv_flags(struct hnae3_handle * h,u32 changed)1840 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1841 {
1842 u32 i;
1843
1844 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1845 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1846 netdev_err(h->netdev, "%s is unsupported\n",
1847 hns3_priv_flags[i].name);
1848 return -EOPNOTSUPP;
1849 }
1850
1851 return 0;
1852 }
1853
hns3_set_priv_flags(struct net_device * netdev,u32 pflags)1854 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1855 {
1856 struct hnae3_handle *handle = hns3_get_handle(netdev);
1857 u32 changed = pflags ^ handle->priv_flags;
1858 int ret;
1859 u32 i;
1860
1861 ret = hns3_check_priv_flags(handle, changed);
1862 if (ret)
1863 return ret;
1864
1865 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1866 if (changed & BIT(i)) {
1867 bool enable = !(handle->priv_flags & BIT(i));
1868
1869 if (enable)
1870 handle->priv_flags |= BIT(i);
1871 else
1872 handle->priv_flags &= ~BIT(i);
1873 hns3_priv_flags[i].handler(netdev, enable);
1874 }
1875 }
1876
1877 return 0;
1878 }
1879
hns3_get_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,void * data)1880 static int hns3_get_tunable(struct net_device *netdev,
1881 const struct ethtool_tunable *tuna,
1882 void *data)
1883 {
1884 struct hns3_nic_priv *priv = netdev_priv(netdev);
1885 struct hnae3_handle *h = priv->ae_handle;
1886 int ret = 0;
1887
1888 switch (tuna->id) {
1889 case ETHTOOL_TX_COPYBREAK:
1890 /* all the tx rings have the same tx_copybreak */
1891 *(u32 *)data = priv->tx_copybreak;
1892 break;
1893 case ETHTOOL_RX_COPYBREAK:
1894 *(u32 *)data = priv->rx_copybreak;
1895 break;
1896 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1897 *(u32 *)data = h->kinfo.tx_spare_buf_size;
1898 break;
1899 default:
1900 ret = -EOPNOTSUPP;
1901 break;
1902 }
1903
1904 return ret;
1905 }
1906
hns3_set_tx_spare_buf_size(struct net_device * netdev,u32 data)1907 static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
1908 u32 data)
1909 {
1910 struct hns3_nic_priv *priv = netdev_priv(netdev);
1911 struct hnae3_handle *h = priv->ae_handle;
1912 int ret;
1913
1914 h->kinfo.tx_spare_buf_size = data;
1915
1916 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
1917 if (ret)
1918 return ret;
1919
1920 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1921 if (ret)
1922 return ret;
1923
1924 ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
1925 if (ret)
1926 return ret;
1927
1928 ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
1929 if (ret)
1930 hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1931
1932 return ret;
1933 }
1934
hns3_check_tx_copybreak(struct net_device * netdev,u32 copybreak)1935 static int hns3_check_tx_copybreak(struct net_device *netdev, u32 copybreak)
1936 {
1937 struct hns3_nic_priv *priv = netdev_priv(netdev);
1938
1939 if (copybreak < priv->min_tx_copybreak) {
1940 netdev_err(netdev, "tx copybreak %u should be no less than %u!\n",
1941 copybreak, priv->min_tx_copybreak);
1942 return -EINVAL;
1943 }
1944 return 0;
1945 }
1946
hns3_check_tx_spare_buf_size(struct net_device * netdev,u32 buf_size)1947 static int hns3_check_tx_spare_buf_size(struct net_device *netdev, u32 buf_size)
1948 {
1949 struct hns3_nic_priv *priv = netdev_priv(netdev);
1950
1951 if (buf_size < priv->min_tx_spare_buf_size) {
1952 netdev_err(netdev,
1953 "tx spare buf size %u should be no less than %u!\n",
1954 buf_size, priv->min_tx_spare_buf_size);
1955 return -EINVAL;
1956 }
1957 return 0;
1958 }
1959
hns3_set_tunable(struct net_device * netdev,const struct ethtool_tunable * tuna,const void * data)1960 static int hns3_set_tunable(struct net_device *netdev,
1961 const struct ethtool_tunable *tuna,
1962 const void *data)
1963 {
1964 struct hns3_nic_priv *priv = netdev_priv(netdev);
1965 u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1966 struct hnae3_handle *h = priv->ae_handle;
1967 int i, ret = 0;
1968
1969 if (hns3_nic_resetting(netdev) || !priv->ring) {
1970 netdev_err(netdev, "failed to set tunable value, dev resetting!\n");
1971 return -EBUSY;
1972 }
1973
1974 switch (tuna->id) {
1975 case ETHTOOL_TX_COPYBREAK:
1976 ret = hns3_check_tx_copybreak(netdev, *(u32 *)data);
1977 if (ret)
1978 return ret;
1979
1980 priv->tx_copybreak = *(u32 *)data;
1981
1982 for (i = 0; i < h->kinfo.num_tqps; i++)
1983 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1984
1985 break;
1986 case ETHTOOL_RX_COPYBREAK:
1987 priv->rx_copybreak = *(u32 *)data;
1988
1989 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1990 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1991
1992 break;
1993 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1994 ret = hns3_check_tx_spare_buf_size(netdev, *(u32 *)data);
1995 if (ret)
1996 return ret;
1997
1998 old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
1999 new_tx_spare_buf_size = *(u32 *)data;
2000 netdev_info(netdev, "request to set tx spare buf size from %u to %u\n",
2001 old_tx_spare_buf_size, new_tx_spare_buf_size);
2002 ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
2003 if (ret ||
2004 (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) {
2005 int ret1;
2006
2007 netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n");
2008 ret1 = hns3_set_tx_spare_buf_size(netdev,
2009 old_tx_spare_buf_size);
2010 if (ret1) {
2011 netdev_err(netdev, "revert to old tx spare buf size fail\n");
2012 return ret1;
2013 }
2014
2015 return ret;
2016 }
2017
2018 if (!priv->ring->tx_spare)
2019 netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n");
2020 else
2021 netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n",
2022 priv->ring->tx_spare->len);
2023
2024 break;
2025 default:
2026 ret = -EOPNOTSUPP;
2027 break;
2028 }
2029
2030 return ret;
2031 }
2032
2033 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
2034 ETHTOOL_COALESCE_USE_ADAPTIVE | \
2035 ETHTOOL_COALESCE_RX_USECS_HIGH | \
2036 ETHTOOL_COALESCE_TX_USECS_HIGH | \
2037 ETHTOOL_COALESCE_MAX_FRAMES | \
2038 ETHTOOL_COALESCE_USE_CQE)
2039
2040 #define HNS3_ETHTOOL_RING (ETHTOOL_RING_USE_RX_BUF_LEN | \
2041 ETHTOOL_RING_USE_TX_PUSH)
2042
hns3_get_ts_info(struct net_device * netdev,struct kernel_ethtool_ts_info * info)2043 static int hns3_get_ts_info(struct net_device *netdev,
2044 struct kernel_ethtool_ts_info *info)
2045 {
2046 struct hnae3_handle *handle = hns3_get_handle(netdev);
2047
2048 if (handle->ae_algo->ops->get_ts_info)
2049 return handle->ae_algo->ops->get_ts_info(handle, info);
2050
2051 return ethtool_op_get_ts_info(netdev, info);
2052 }
2053
2054 static const struct hns3_ethtool_link_ext_state_mapping
2055 hns3_link_ext_state_map[] = {
2056 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
2057 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
2058 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
2059 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
2060
2061 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2062 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
2063 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2064 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
2065 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
2066 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
2067
2068 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2069 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
2070 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2071 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
2072 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
2073 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
2074
2075 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2076 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
2077 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2078 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
2079 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
2080 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
2081
2082 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
2083 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
2084 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
2085
2086 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
2087 };
2088
hns3_get_link_ext_state(struct net_device * netdev,struct ethtool_link_ext_state_info * info)2089 static int hns3_get_link_ext_state(struct net_device *netdev,
2090 struct ethtool_link_ext_state_info *info)
2091 {
2092 const struct hns3_ethtool_link_ext_state_mapping *map;
2093 struct hnae3_handle *h = hns3_get_handle(netdev);
2094 u32 status_code, i;
2095 int ret;
2096
2097 if (netif_carrier_ok(netdev))
2098 return -ENODATA;
2099
2100 if (!h->ae_algo->ops->get_link_diagnosis_info)
2101 return -EOPNOTSUPP;
2102
2103 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
2104 if (ret)
2105 return ret;
2106
2107 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
2108 map = &hns3_link_ext_state_map[i];
2109 if (map->status_code == status_code) {
2110 info->link_ext_state = map->link_ext_state;
2111 info->__link_ext_substate = map->link_ext_substate;
2112 return 0;
2113 }
2114 }
2115
2116 return -ENODATA;
2117 }
2118
hns3_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)2119 static void hns3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2120 {
2121 struct hnae3_handle *handle = hns3_get_handle(netdev);
2122 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
2123 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
2124
2125 if (!hnae3_ae_dev_wol_supported(ae_dev))
2126 return;
2127
2128 ops->get_wol(handle, wol);
2129 }
2130
hns3_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)2131 static int hns3_set_wol(struct net_device *netdev,
2132 struct ethtool_wolinfo *wol)
2133 {
2134 struct hnae3_handle *handle = hns3_get_handle(netdev);
2135 const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
2136 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
2137
2138 if (!hnae3_ae_dev_wol_supported(ae_dev))
2139 return -EOPNOTSUPP;
2140
2141 return ops->set_wol(handle, wol);
2142 }
2143
2144 static const struct ethtool_ops hns3vf_ethtool_ops = {
2145 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2146 .supported_ring_params = HNS3_ETHTOOL_RING,
2147 .get_drvinfo = hns3_get_drvinfo,
2148 .get_ringparam = hns3_get_ringparam,
2149 .set_ringparam = hns3_set_ringparam,
2150 .get_strings = hns3_get_strings,
2151 .get_ethtool_stats = hns3_get_stats,
2152 .get_sset_count = hns3_get_sset_count,
2153 .get_rxnfc = hns3_get_rxnfc,
2154 .set_rxnfc = hns3_set_rxnfc,
2155 .get_rx_ring_count = hns3_get_rx_ring_count,
2156 .get_rxfh_key_size = hns3_get_rss_key_size,
2157 .get_rxfh_indir_size = hns3_get_rss_indir_size,
2158 .get_rxfh = hns3_get_rss,
2159 .set_rxfh = hns3_set_rss,
2160 .get_rxfh_fields = hns3_get_rxfh_fields,
2161 .set_rxfh_fields = hns3_set_rxfh_fields,
2162 .get_link_ksettings = hns3_get_link_ksettings,
2163 .get_channels = hns3_get_channels,
2164 .set_channels = hns3_set_channels,
2165 .get_coalesce = hns3_get_coalesce,
2166 .set_coalesce = hns3_set_coalesce,
2167 .get_regs_len = hns3_get_regs_len,
2168 .get_regs = hns3_get_regs,
2169 .get_link = hns3_get_link,
2170 .get_msglevel = hns3_get_msglevel,
2171 .set_msglevel = hns3_set_msglevel,
2172 .get_priv_flags = hns3_get_priv_flags,
2173 .set_priv_flags = hns3_set_priv_flags,
2174 .get_tunable = hns3_get_tunable,
2175 .set_tunable = hns3_set_tunable,
2176 .reset = hns3_set_reset,
2177 };
2178
2179 static const struct ethtool_ops hns3_ethtool_ops = {
2180 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2181 .supported_ring_params = HNS3_ETHTOOL_RING,
2182 .cap_link_lanes_supported = true,
2183 .self_test = hns3_self_test,
2184 .get_drvinfo = hns3_get_drvinfo,
2185 .get_link = hns3_get_link,
2186 .get_ringparam = hns3_get_ringparam,
2187 .set_ringparam = hns3_set_ringparam,
2188 .get_pauseparam = hns3_get_pauseparam,
2189 .set_pauseparam = hns3_set_pauseparam,
2190 .get_strings = hns3_get_strings,
2191 .get_ethtool_stats = hns3_get_stats,
2192 .get_sset_count = hns3_get_sset_count,
2193 .get_rxnfc = hns3_get_rxnfc,
2194 .set_rxnfc = hns3_set_rxnfc,
2195 .get_rx_ring_count = hns3_get_rx_ring_count,
2196 .get_rxfh_key_size = hns3_get_rss_key_size,
2197 .get_rxfh_indir_size = hns3_get_rss_indir_size,
2198 .get_rxfh = hns3_get_rss,
2199 .set_rxfh = hns3_set_rss,
2200 .get_rxfh_fields = hns3_get_rxfh_fields,
2201 .set_rxfh_fields = hns3_set_rxfh_fields,
2202 .get_link_ksettings = hns3_get_link_ksettings,
2203 .set_link_ksettings = hns3_set_link_ksettings,
2204 .nway_reset = hns3_nway_reset,
2205 .get_channels = hns3_get_channels,
2206 .set_channels = hns3_set_channels,
2207 .get_coalesce = hns3_get_coalesce,
2208 .set_coalesce = hns3_set_coalesce,
2209 .get_regs_len = hns3_get_regs_len,
2210 .get_regs = hns3_get_regs,
2211 .set_phys_id = hns3_set_phys_id,
2212 .get_msglevel = hns3_get_msglevel,
2213 .set_msglevel = hns3_set_msglevel,
2214 .get_fecparam = hns3_get_fecparam,
2215 .set_fecparam = hns3_set_fecparam,
2216 .get_fec_stats = hns3_get_fec_stats,
2217 .get_module_info = hns3_get_module_info,
2218 .get_module_eeprom = hns3_get_module_eeprom,
2219 .get_priv_flags = hns3_get_priv_flags,
2220 .set_priv_flags = hns3_set_priv_flags,
2221 .get_ts_info = hns3_get_ts_info,
2222 .get_tunable = hns3_get_tunable,
2223 .set_tunable = hns3_set_tunable,
2224 .reset = hns3_set_reset,
2225 .get_link_ext_state = hns3_get_link_ext_state,
2226 .get_wol = hns3_get_wol,
2227 .set_wol = hns3_set_wol,
2228 };
2229
hns3_ethtool_set_ops(struct net_device * netdev)2230 void hns3_ethtool_set_ops(struct net_device *netdev)
2231 {
2232 struct hnae3_handle *h = hns3_get_handle(netdev);
2233
2234 if (h->flags & HNAE3_SUPPORT_VF)
2235 netdev->ethtool_ops = &hns3vf_ethtool_ops;
2236 else
2237 netdev->ethtool_ops = &hns3_ethtool_ops;
2238 }
2239