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/phy.h>
7
8 #include "hns3_enet.h"
9
10 struct hns3_stats {
11 char stats_string[ETH_GSTRING_LEN];
12 int stats_offset;
13 };
14
15 /* tqp related stats */
16 #define HNS3_TQP_STAT(_string, _member) { \
17 .stats_string = _string, \
18 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
19 offsetof(struct ring_stats, _member), \
20 }
21
22 static const struct hns3_stats hns3_txq_stats[] = {
23 /* Tx per-queue statistics */
24 HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
25 HNS3_TQP_STAT("tx_dropped", sw_err_cnt),
26 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
27 HNS3_TQP_STAT("packets", tx_pkts),
28 HNS3_TQP_STAT("bytes", tx_bytes),
29 HNS3_TQP_STAT("errors", tx_err_cnt),
30 HNS3_TQP_STAT("tx_wake", restart_queue),
31 HNS3_TQP_STAT("tx_busy", tx_busy),
32 };
33
34 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
35
36 static const struct hns3_stats hns3_rxq_stats[] = {
37 /* Rx per-queue statistics */
38 HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
39 HNS3_TQP_STAT("rx_dropped", sw_err_cnt),
40 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
41 HNS3_TQP_STAT("packets", rx_pkts),
42 HNS3_TQP_STAT("bytes", rx_bytes),
43 HNS3_TQP_STAT("errors", rx_err_cnt),
44 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
45 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
46 HNS3_TQP_STAT("non_vld_descs", non_vld_descs),
47 HNS3_TQP_STAT("err_bd_num", err_bd_num),
48 HNS3_TQP_STAT("l2_err", l2_err),
49 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
50 };
51
52 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
53
54 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
55
56 #define HNS3_SELF_TEST_TYPE_NUM 2
57 #define HNS3_NIC_LB_TEST_PKT_NUM 1
58 #define HNS3_NIC_LB_TEST_RING_ID 0
59 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
60
61 /* Nic loopback test err */
62 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
63 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
64 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
65
66 struct hns3_link_mode_mapping {
67 u32 hns3_link_mode;
68 u32 ethtool_link_mode;
69 };
70
hns3_lp_setup(struct net_device * ndev,enum hnae3_loop loop,bool en)71 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
72 {
73 struct hnae3_handle *h = hns3_get_handle(ndev);
74 int ret;
75
76 if (!h->ae_algo->ops->set_loopback ||
77 !h->ae_algo->ops->set_promisc_mode)
78 return -EOPNOTSUPP;
79
80 switch (loop) {
81 case HNAE3_MAC_INTER_LOOP_SERDES:
82 case HNAE3_MAC_INTER_LOOP_MAC:
83 ret = h->ae_algo->ops->set_loopback(h, loop, en);
84 break;
85 default:
86 ret = -ENOTSUPP;
87 break;
88 }
89
90 if (ret)
91 return ret;
92
93 h->ae_algo->ops->set_promisc_mode(h, en, en);
94
95 return ret;
96 }
97
hns3_lp_up(struct net_device * ndev,enum hnae3_loop loop_mode)98 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
99 {
100 struct hnae3_handle *h = hns3_get_handle(ndev);
101 int ret;
102
103 ret = hns3_nic_reset_all_ring(h);
104 if (ret)
105 return ret;
106
107 ret = hns3_lp_setup(ndev, loop_mode, true);
108 usleep_range(10000, 20000);
109
110 return 0;
111 }
112
hns3_lp_down(struct net_device * ndev,enum hnae3_loop loop_mode)113 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
114 {
115 int ret;
116
117 ret = hns3_lp_setup(ndev, loop_mode, false);
118 if (ret) {
119 netdev_err(ndev, "lb_setup return error: %d\n", ret);
120 return ret;
121 }
122
123 usleep_range(10000, 20000);
124
125 return 0;
126 }
127
hns3_lp_setup_skb(struct sk_buff * skb)128 static void hns3_lp_setup_skb(struct sk_buff *skb)
129 {
130 struct net_device *ndev = skb->dev;
131 unsigned char *packet;
132 struct ethhdr *ethh;
133 unsigned int i;
134
135 skb_reserve(skb, NET_IP_ALIGN);
136 ethh = skb_put(skb, sizeof(struct ethhdr));
137 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
138
139 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
140 ethh->h_dest[5] += 0x1f;
141 eth_zero_addr(ethh->h_source);
142 ethh->h_proto = htons(ETH_P_ARP);
143 skb_reset_mac_header(skb);
144
145 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
146 packet[i] = (unsigned char)(i & 0xff);
147 }
148
hns3_lb_check_skb_data(struct hns3_enet_ring * ring,struct sk_buff * skb)149 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
150 struct sk_buff *skb)
151 {
152 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
153 unsigned char *packet = skb->data;
154 u32 len = skb_headlen(skb);
155 u32 i;
156
157 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
158
159 for (i = 0; i < len; i++)
160 if (packet[i] != (unsigned char)(i & 0xff))
161 break;
162
163 /* The packet is correctly received */
164 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
165 tqp_vector->rx_group.total_packets++;
166 else
167 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
168 skb->data, len, true);
169
170 dev_kfree_skb_any(skb);
171 }
172
hns3_lb_check_rx_ring(struct hns3_nic_priv * priv,u32 budget)173 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
174 {
175 struct hnae3_handle *h = priv->ae_handle;
176 struct hnae3_knic_private_info *kinfo;
177 u32 i, rcv_good_pkt_total = 0;
178
179 kinfo = &h->kinfo;
180 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
181 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
182 struct hns3_enet_ring_group *rx_group;
183 u64 pre_rx_pkt;
184
185 rx_group = &ring->tqp_vector->rx_group;
186 pre_rx_pkt = rx_group->total_packets;
187
188 preempt_disable();
189 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
190 preempt_enable();
191
192 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
193 rx_group->total_packets = pre_rx_pkt;
194 }
195 return rcv_good_pkt_total;
196 }
197
hns3_lb_clear_tx_ring(struct hns3_nic_priv * priv,u32 start_ringid,u32 end_ringid,u32 budget)198 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
199 u32 end_ringid, u32 budget)
200 {
201 u32 i;
202
203 for (i = start_ringid; i <= end_ringid; i++) {
204 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
205
206 hns3_clean_tx_ring(ring, budget);
207 }
208 }
209
210 /**
211 * hns3_lp_run_test - run loopback test
212 * @ndev: net device
213 * @mode: loopback type
214 */
hns3_lp_run_test(struct net_device * ndev,enum hnae3_loop mode)215 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
216 {
217 struct hns3_nic_priv *priv = netdev_priv(ndev);
218 struct sk_buff *skb;
219 u32 i, good_cnt;
220 int ret_val = 0;
221
222 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
223 GFP_KERNEL);
224 if (!skb)
225 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
226
227 skb->dev = ndev;
228 hns3_lp_setup_skb(skb);
229 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
230
231 good_cnt = 0;
232 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
233 netdev_tx_t tx_ret;
234
235 skb_get(skb);
236 tx_ret = hns3_nic_net_xmit(skb, ndev);
237 if (tx_ret == NETDEV_TX_OK) {
238 good_cnt++;
239 } else {
240 kfree_skb(skb);
241 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
242 tx_ret);
243 }
244 }
245 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
246 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
247 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
248 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
249 goto out;
250 }
251
252 /* Allow 200 milliseconds for packets to go from Tx to Rx */
253 msleep(200);
254
255 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
256 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
257 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
258 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
259 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
260 }
261
262 out:
263 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
264 HNS3_NIC_LB_TEST_RING_ID,
265 HNS3_NIC_LB_TEST_PKT_NUM);
266
267 kfree_skb(skb);
268 return ret_val;
269 }
270
271 /**
272 * hns3_nic_self_test - self test
273 * @ndev: net device
274 * @eth_test: test cmd
275 * @data: test result
276 */
hns3_self_test(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)277 static void hns3_self_test(struct net_device *ndev,
278 struct ethtool_test *eth_test, u64 *data)
279 {
280 struct hns3_nic_priv *priv = netdev_priv(ndev);
281 struct hnae3_handle *h = priv->ae_handle;
282 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
283 bool if_running = netif_running(ndev);
284 #if IS_ENABLED(CONFIG_VLAN_8021Q)
285 bool dis_vlan_filter;
286 #endif
287 int test_index = 0;
288 u32 i;
289
290 /* Only do offline selftest, or pass by default */
291 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
292 return;
293
294 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
295 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
296 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
297
298 st_param[HNAE3_MAC_INTER_LOOP_SERDES][0] = HNAE3_MAC_INTER_LOOP_SERDES;
299 st_param[HNAE3_MAC_INTER_LOOP_SERDES][1] =
300 h->flags & HNAE3_SUPPORT_SERDES_LOOPBACK;
301
302 if (if_running)
303 ndev->netdev_ops->ndo_stop(ndev);
304
305 #if IS_ENABLED(CONFIG_VLAN_8021Q)
306 /* Disable the vlan filter for selftest does not support it */
307 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
308 h->ae_algo->ops->enable_vlan_filter;
309 if (dis_vlan_filter)
310 h->ae_algo->ops->enable_vlan_filter(h, false);
311 #endif
312
313 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
314
315 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
316 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
317
318 if (!st_param[i][1])
319 continue;
320
321 data[test_index] = hns3_lp_up(ndev, loop_type);
322 if (!data[test_index]) {
323 data[test_index] = hns3_lp_run_test(ndev, loop_type);
324 hns3_lp_down(ndev, loop_type);
325 }
326
327 if (data[test_index])
328 eth_test->flags |= ETH_TEST_FL_FAILED;
329
330 test_index++;
331 }
332
333 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
334
335 #if IS_ENABLED(CONFIG_VLAN_8021Q)
336 if (dis_vlan_filter)
337 h->ae_algo->ops->enable_vlan_filter(h, true);
338 #endif
339
340 if (if_running)
341 ndev->netdev_ops->ndo_open(ndev);
342 }
343
hns3_get_sset_count(struct net_device * netdev,int stringset)344 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
345 {
346 struct hnae3_handle *h = hns3_get_handle(netdev);
347 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
348
349 if (!ops->get_sset_count)
350 return -EOPNOTSUPP;
351
352 switch (stringset) {
353 case ETH_SS_STATS:
354 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
355 ops->get_sset_count(h, stringset));
356
357 case ETH_SS_TEST:
358 return ops->get_sset_count(h, stringset);
359 }
360
361 return 0;
362 }
363
hns3_update_strings(u8 * data,const struct hns3_stats * stats,u32 stat_count,u32 num_tqps,const char * prefix)364 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
365 u32 stat_count, u32 num_tqps, const char *prefix)
366 {
367 #define MAX_PREFIX_SIZE (6 + 4)
368 u32 size_left;
369 u32 i, j;
370 u32 n1;
371
372 for (i = 0; i < num_tqps; i++) {
373 for (j = 0; j < stat_count; j++) {
374 data[ETH_GSTRING_LEN - 1] = '\0';
375
376 /* first, prepend the prefix string */
377 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
378 prefix, i);
379 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
380 size_left = (ETH_GSTRING_LEN - 1) - n1;
381
382 /* now, concatenate the stats string to it */
383 strncat(data, stats[j].stats_string, size_left);
384 data += ETH_GSTRING_LEN;
385 }
386 }
387
388 return data;
389 }
390
hns3_get_strings_tqps(struct hnae3_handle * handle,u8 * data)391 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
392 {
393 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
394 const char tx_prefix[] = "txq";
395 const char rx_prefix[] = "rxq";
396
397 /* get strings for Tx */
398 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
399 kinfo->num_tqps, tx_prefix);
400
401 /* get strings for Rx */
402 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
403 kinfo->num_tqps, rx_prefix);
404
405 return data;
406 }
407
hns3_get_strings(struct net_device * netdev,u32 stringset,u8 * data)408 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
409 {
410 struct hnae3_handle *h = hns3_get_handle(netdev);
411 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
412 char *buff = (char *)data;
413
414 if (!ops->get_strings)
415 return;
416
417 switch (stringset) {
418 case ETH_SS_STATS:
419 buff = hns3_get_strings_tqps(h, buff);
420 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
421 break;
422 case ETH_SS_TEST:
423 ops->get_strings(h, stringset, data);
424 break;
425 }
426 }
427
hns3_get_stats_tqps(struct hnae3_handle * handle,u64 * data)428 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
429 {
430 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
431 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
432 struct hns3_enet_ring *ring;
433 u8 *stat;
434 int i, j;
435
436 /* get stats for Tx */
437 for (i = 0; i < kinfo->num_tqps; i++) {
438 ring = nic_priv->ring_data[i].ring;
439 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
440 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
441 *data++ = *(u64 *)stat;
442 }
443 }
444
445 /* get stats for Rx */
446 for (i = 0; i < kinfo->num_tqps; i++) {
447 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
448 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
449 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
450 *data++ = *(u64 *)stat;
451 }
452 }
453
454 return data;
455 }
456
457 /* hns3_get_stats - get detail statistics.
458 * @netdev: net device
459 * @stats: statistics info.
460 * @data: statistics data.
461 */
hns3_get_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)462 static void hns3_get_stats(struct net_device *netdev,
463 struct ethtool_stats *stats, u64 *data)
464 {
465 struct hnae3_handle *h = hns3_get_handle(netdev);
466 u64 *p = data;
467
468 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
469 netdev_err(netdev, "could not get any statistics\n");
470 return;
471 }
472
473 h->ae_algo->ops->update_stats(h, &netdev->stats);
474
475 /* get per-queue stats */
476 p = hns3_get_stats_tqps(h, p);
477
478 /* get MAC & other misc hardware stats */
479 h->ae_algo->ops->get_stats(h, p);
480 }
481
hns3_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)482 static void hns3_get_drvinfo(struct net_device *netdev,
483 struct ethtool_drvinfo *drvinfo)
484 {
485 struct hns3_nic_priv *priv = netdev_priv(netdev);
486 struct hnae3_handle *h = priv->ae_handle;
487
488 strncpy(drvinfo->version, hns3_driver_version,
489 sizeof(drvinfo->version));
490 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
491
492 strncpy(drvinfo->driver, h->pdev->driver->name,
493 sizeof(drvinfo->driver));
494 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
495
496 strncpy(drvinfo->bus_info, pci_name(h->pdev),
497 sizeof(drvinfo->bus_info));
498 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
499
500 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
501 priv->ae_handle->ae_algo->ops->get_fw_version(h));
502 }
503
hns3_get_link(struct net_device * netdev)504 static u32 hns3_get_link(struct net_device *netdev)
505 {
506 struct hnae3_handle *h = hns3_get_handle(netdev);
507
508 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
509 return h->ae_algo->ops->get_status(h);
510 else
511 return 0;
512 }
513
hns3_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * param)514 static void hns3_get_ringparam(struct net_device *netdev,
515 struct ethtool_ringparam *param)
516 {
517 struct hns3_nic_priv *priv = netdev_priv(netdev);
518 struct hnae3_handle *h = priv->ae_handle;
519 int queue_num = h->kinfo.num_tqps;
520
521 param->tx_max_pending = HNS3_RING_MAX_PENDING;
522 param->rx_max_pending = HNS3_RING_MAX_PENDING;
523
524 param->tx_pending = priv->ring_data[0].ring->desc_num;
525 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
526 }
527
hns3_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * param)528 static void hns3_get_pauseparam(struct net_device *netdev,
529 struct ethtool_pauseparam *param)
530 {
531 struct hnae3_handle *h = hns3_get_handle(netdev);
532
533 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
534 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
535 ¶m->rx_pause, ¶m->tx_pause);
536 }
537
hns3_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * param)538 static int hns3_set_pauseparam(struct net_device *netdev,
539 struct ethtool_pauseparam *param)
540 {
541 struct hnae3_handle *h = hns3_get_handle(netdev);
542
543 if (h->ae_algo->ops->set_pauseparam)
544 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
545 param->rx_pause,
546 param->tx_pause);
547 return -EOPNOTSUPP;
548 }
549
hns3_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)550 static int hns3_get_link_ksettings(struct net_device *netdev,
551 struct ethtool_link_ksettings *cmd)
552 {
553 struct hnae3_handle *h = hns3_get_handle(netdev);
554 u32 flowctrl_adv = 0;
555 u8 link_stat;
556
557 if (!h->ae_algo || !h->ae_algo->ops)
558 return -EOPNOTSUPP;
559
560 /* 1.auto_neg & speed & duplex from cmd */
561 if (netdev->phydev) {
562 phy_ethtool_ksettings_get(netdev->phydev, cmd);
563
564 return 0;
565 }
566
567 if (h->ae_algo->ops->get_ksettings_an_result)
568 h->ae_algo->ops->get_ksettings_an_result(h,
569 &cmd->base.autoneg,
570 &cmd->base.speed,
571 &cmd->base.duplex);
572 else
573 return -EOPNOTSUPP;
574
575 link_stat = hns3_get_link(netdev);
576 if (!link_stat) {
577 cmd->base.speed = SPEED_UNKNOWN;
578 cmd->base.duplex = DUPLEX_UNKNOWN;
579 }
580
581 /* 2.get link mode and port type*/
582 if (h->ae_algo->ops->get_link_mode)
583 h->ae_algo->ops->get_link_mode(h,
584 cmd->link_modes.supported,
585 cmd->link_modes.advertising);
586
587 cmd->base.port = PORT_NONE;
588 if (h->ae_algo->ops->get_port_type)
589 h->ae_algo->ops->get_port_type(h,
590 &cmd->base.port);
591
592 /* 3.mdix_ctrl&mdix get from phy reg */
593 if (h->ae_algo->ops->get_mdix_mode)
594 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
595 &cmd->base.eth_tp_mdix);
596 /* 4.mdio_support */
597 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
598
599 /* 5.get flow control setttings */
600 if (h->ae_algo->ops->get_flowctrl_adv)
601 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
602
603 if (flowctrl_adv & ADVERTISED_Pause)
604 ethtool_link_ksettings_add_link_mode(cmd, advertising,
605 Pause);
606
607 if (flowctrl_adv & ADVERTISED_Asym_Pause)
608 ethtool_link_ksettings_add_link_mode(cmd, advertising,
609 Asym_Pause);
610
611 return 0;
612 }
613
hns3_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * cmd)614 static int hns3_set_link_ksettings(struct net_device *netdev,
615 const struct ethtool_link_ksettings *cmd)
616 {
617 /* Only support ksettings_set for netdev with phy attached for now */
618 if (netdev->phydev)
619 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
620
621 return -EOPNOTSUPP;
622 }
623
hns3_get_rss_key_size(struct net_device * netdev)624 static u32 hns3_get_rss_key_size(struct net_device *netdev)
625 {
626 struct hnae3_handle *h = hns3_get_handle(netdev);
627
628 if (!h->ae_algo || !h->ae_algo->ops ||
629 !h->ae_algo->ops->get_rss_key_size)
630 return 0;
631
632 return h->ae_algo->ops->get_rss_key_size(h);
633 }
634
hns3_get_rss_indir_size(struct net_device * netdev)635 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
636 {
637 struct hnae3_handle *h = hns3_get_handle(netdev);
638
639 if (!h->ae_algo || !h->ae_algo->ops ||
640 !h->ae_algo->ops->get_rss_indir_size)
641 return 0;
642
643 return h->ae_algo->ops->get_rss_indir_size(h);
644 }
645
hns3_get_rss(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)646 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
647 u8 *hfunc)
648 {
649 struct hnae3_handle *h = hns3_get_handle(netdev);
650
651 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
652 return -EOPNOTSUPP;
653
654 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
655 }
656
hns3_set_rss(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)657 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
658 const u8 *key, const u8 hfunc)
659 {
660 struct hnae3_handle *h = hns3_get_handle(netdev);
661
662 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
663 return -EOPNOTSUPP;
664
665 /* currently we only support Toeplitz hash */
666 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
667 netdev_err(netdev,
668 "hash func not supported (only Toeplitz hash)\n");
669 return -EOPNOTSUPP;
670 }
671 if (!indir) {
672 netdev_err(netdev,
673 "set rss failed for indir is empty\n");
674 return -EOPNOTSUPP;
675 }
676
677 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
678 }
679
hns3_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)680 static int hns3_get_rxnfc(struct net_device *netdev,
681 struct ethtool_rxnfc *cmd,
682 u32 *rule_locs)
683 {
684 struct hnae3_handle *h = hns3_get_handle(netdev);
685
686 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
687 return -EOPNOTSUPP;
688
689 switch (cmd->cmd) {
690 case ETHTOOL_GRXRINGS:
691 cmd->data = h->kinfo.rss_size;
692 break;
693 case ETHTOOL_GRXFH:
694 return h->ae_algo->ops->get_rss_tuple(h, cmd);
695 default:
696 return -EOPNOTSUPP;
697 }
698
699 return 0;
700 }
701
hns3_change_all_ring_bd_num(struct hns3_nic_priv * priv,u32 new_desc_num)702 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
703 u32 new_desc_num)
704 {
705 struct hnae3_handle *h = priv->ae_handle;
706 int i;
707
708 h->kinfo.num_desc = new_desc_num;
709
710 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
711 priv->ring_data[i].ring->desc_num = new_desc_num;
712
713 return hns3_init_all_ring(priv);
714 }
715
hns3_set_ringparam(struct net_device * ndev,struct ethtool_ringparam * param)716 static int hns3_set_ringparam(struct net_device *ndev,
717 struct ethtool_ringparam *param)
718 {
719 struct hns3_nic_priv *priv = netdev_priv(ndev);
720 struct hnae3_handle *h = priv->ae_handle;
721 bool if_running = netif_running(ndev);
722 u32 old_desc_num, new_desc_num;
723 int ret;
724
725 if (param->rx_mini_pending || param->rx_jumbo_pending)
726 return -EINVAL;
727
728 if (param->tx_pending != param->rx_pending) {
729 netdev_err(ndev,
730 "Descriptors of tx and rx must be equal");
731 return -EINVAL;
732 }
733
734 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
735 param->tx_pending < HNS3_RING_MIN_PENDING) {
736 netdev_err(ndev,
737 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
738 param->tx_pending, HNS3_RING_MIN_PENDING,
739 HNS3_RING_MAX_PENDING);
740 return -EINVAL;
741 }
742
743 new_desc_num = param->tx_pending;
744
745 /* Hardware requires that its descriptors must be multiple of eight */
746 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
747 old_desc_num = h->kinfo.num_desc;
748 if (old_desc_num == new_desc_num)
749 return 0;
750
751 netdev_info(ndev,
752 "Changing descriptor count from %d to %d.\n",
753 old_desc_num, new_desc_num);
754
755 if (if_running)
756 dev_close(ndev);
757
758 ret = hns3_uninit_all_ring(priv);
759 if (ret)
760 return ret;
761
762 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
763 if (ret) {
764 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
765 if (ret) {
766 netdev_err(ndev,
767 "Revert to old bd num fail, ret=%d.\n", ret);
768 return ret;
769 }
770 }
771
772 if (if_running)
773 ret = dev_open(ndev);
774
775 return ret;
776 }
777
hns3_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)778 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
779 {
780 struct hnae3_handle *h = hns3_get_handle(netdev);
781
782 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
783 return -EOPNOTSUPP;
784
785 switch (cmd->cmd) {
786 case ETHTOOL_SRXFH:
787 return h->ae_algo->ops->set_rss_tuple(h, cmd);
788 default:
789 return -EOPNOTSUPP;
790 }
791 }
792
hns3_nway_reset(struct net_device * netdev)793 static int hns3_nway_reset(struct net_device *netdev)
794 {
795 struct phy_device *phy = netdev->phydev;
796
797 if (!netif_running(netdev))
798 return 0;
799
800 /* Only support nway_reset for netdev with phy attached for now */
801 if (!phy)
802 return -EOPNOTSUPP;
803
804 if (phy->autoneg != AUTONEG_ENABLE)
805 return -EINVAL;
806
807 return genphy_restart_aneg(phy);
808 }
809
hns3_get_channels(struct net_device * netdev,struct ethtool_channels * ch)810 static void hns3_get_channels(struct net_device *netdev,
811 struct ethtool_channels *ch)
812 {
813 struct hnae3_handle *h = hns3_get_handle(netdev);
814
815 if (h->ae_algo->ops->get_channels)
816 h->ae_algo->ops->get_channels(h, ch);
817 }
818
hns3_get_coalesce_per_queue(struct net_device * netdev,u32 queue,struct ethtool_coalesce * cmd)819 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
820 struct ethtool_coalesce *cmd)
821 {
822 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
823 struct hns3_nic_priv *priv = netdev_priv(netdev);
824 struct hnae3_handle *h = priv->ae_handle;
825 u16 queue_num = h->kinfo.num_tqps;
826
827 if (queue >= queue_num) {
828 netdev_err(netdev,
829 "Invalid queue value %d! Queue max id=%d\n",
830 queue, queue_num - 1);
831 return -EINVAL;
832 }
833
834 tx_vector = priv->ring_data[queue].ring->tqp_vector;
835 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
836
837 cmd->use_adaptive_tx_coalesce =
838 tx_vector->tx_group.coal.gl_adapt_enable;
839 cmd->use_adaptive_rx_coalesce =
840 rx_vector->rx_group.coal.gl_adapt_enable;
841
842 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
843 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
844
845 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
846 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
847
848 return 0;
849 }
850
hns3_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd)851 static int hns3_get_coalesce(struct net_device *netdev,
852 struct ethtool_coalesce *cmd)
853 {
854 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
855 }
856
hns3_check_gl_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd)857 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
858 struct ethtool_coalesce *cmd)
859 {
860 u32 rx_gl, tx_gl;
861
862 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
863 netdev_err(netdev,
864 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
865 HNS3_INT_GL_MAX);
866 return -EINVAL;
867 }
868
869 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
870 netdev_err(netdev,
871 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
872 HNS3_INT_GL_MAX);
873 return -EINVAL;
874 }
875
876 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
877 if (rx_gl != cmd->rx_coalesce_usecs) {
878 netdev_info(netdev,
879 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
880 cmd->rx_coalesce_usecs, rx_gl);
881 }
882
883 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
884 if (tx_gl != cmd->tx_coalesce_usecs) {
885 netdev_info(netdev,
886 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
887 cmd->tx_coalesce_usecs, tx_gl);
888 }
889
890 return 0;
891 }
892
hns3_check_rl_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd)893 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
894 struct ethtool_coalesce *cmd)
895 {
896 u32 rl;
897
898 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
899 netdev_err(netdev,
900 "tx_usecs_high must be same as rx_usecs_high.\n");
901 return -EINVAL;
902 }
903
904 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
905 netdev_err(netdev,
906 "Invalid usecs_high value, usecs_high range is 0-%d\n",
907 HNS3_INT_RL_MAX);
908 return -EINVAL;
909 }
910
911 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
912 if (rl != cmd->rx_coalesce_usecs_high) {
913 netdev_info(netdev,
914 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
915 cmd->rx_coalesce_usecs_high, rl);
916 }
917
918 return 0;
919 }
920
hns3_check_coalesce_para(struct net_device * netdev,struct ethtool_coalesce * cmd)921 static int hns3_check_coalesce_para(struct net_device *netdev,
922 struct ethtool_coalesce *cmd)
923 {
924 int ret;
925
926 ret = hns3_check_gl_coalesce_para(netdev, cmd);
927 if (ret) {
928 netdev_err(netdev,
929 "Check gl coalesce param fail. ret = %d\n", ret);
930 return ret;
931 }
932
933 ret = hns3_check_rl_coalesce_para(netdev, cmd);
934 if (ret) {
935 netdev_err(netdev,
936 "Check rl coalesce param fail. ret = %d\n", ret);
937 return ret;
938 }
939
940 if (cmd->use_adaptive_tx_coalesce == 1 ||
941 cmd->use_adaptive_rx_coalesce == 1) {
942 netdev_info(netdev,
943 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
944 cmd->use_adaptive_tx_coalesce,
945 cmd->use_adaptive_rx_coalesce);
946 }
947
948 return 0;
949 }
950
hns3_set_coalesce_per_queue(struct net_device * netdev,struct ethtool_coalesce * cmd,u32 queue)951 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
952 struct ethtool_coalesce *cmd,
953 u32 queue)
954 {
955 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
956 struct hns3_nic_priv *priv = netdev_priv(netdev);
957 struct hnae3_handle *h = priv->ae_handle;
958 int queue_num = h->kinfo.num_tqps;
959
960 tx_vector = priv->ring_data[queue].ring->tqp_vector;
961 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
962
963 tx_vector->tx_group.coal.gl_adapt_enable =
964 cmd->use_adaptive_tx_coalesce;
965 rx_vector->rx_group.coal.gl_adapt_enable =
966 cmd->use_adaptive_rx_coalesce;
967
968 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
969 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
970
971 hns3_set_vector_coalesce_tx_gl(tx_vector,
972 tx_vector->tx_group.coal.int_gl);
973 hns3_set_vector_coalesce_rx_gl(rx_vector,
974 rx_vector->rx_group.coal.int_gl);
975
976 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
977 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
978 }
979
hns3_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd)980 static int hns3_set_coalesce(struct net_device *netdev,
981 struct ethtool_coalesce *cmd)
982 {
983 struct hnae3_handle *h = hns3_get_handle(netdev);
984 u16 queue_num = h->kinfo.num_tqps;
985 int ret;
986 int i;
987
988 ret = hns3_check_coalesce_para(netdev, cmd);
989 if (ret)
990 return ret;
991
992 h->kinfo.int_rl_setting =
993 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
994
995 for (i = 0; i < queue_num; i++)
996 hns3_set_coalesce_per_queue(netdev, cmd, i);
997
998 return 0;
999 }
1000
hns3_get_regs_len(struct net_device * netdev)1001 static int hns3_get_regs_len(struct net_device *netdev)
1002 {
1003 struct hnae3_handle *h = hns3_get_handle(netdev);
1004
1005 if (!h->ae_algo->ops->get_regs_len)
1006 return -EOPNOTSUPP;
1007
1008 return h->ae_algo->ops->get_regs_len(h);
1009 }
1010
hns3_get_regs(struct net_device * netdev,struct ethtool_regs * cmd,void * data)1011 static void hns3_get_regs(struct net_device *netdev,
1012 struct ethtool_regs *cmd, void *data)
1013 {
1014 struct hnae3_handle *h = hns3_get_handle(netdev);
1015
1016 if (!h->ae_algo->ops->get_regs)
1017 return;
1018
1019 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1020 }
1021
hns3_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1022 static int hns3_set_phys_id(struct net_device *netdev,
1023 enum ethtool_phys_id_state state)
1024 {
1025 struct hnae3_handle *h = hns3_get_handle(netdev);
1026
1027 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1028 return -EOPNOTSUPP;
1029
1030 return h->ae_algo->ops->set_led_id(h, state);
1031 }
1032
1033 static const struct ethtool_ops hns3vf_ethtool_ops = {
1034 .get_drvinfo = hns3_get_drvinfo,
1035 .get_ringparam = hns3_get_ringparam,
1036 .set_ringparam = hns3_set_ringparam,
1037 .get_strings = hns3_get_strings,
1038 .get_ethtool_stats = hns3_get_stats,
1039 .get_sset_count = hns3_get_sset_count,
1040 .get_rxnfc = hns3_get_rxnfc,
1041 .get_rxfh_key_size = hns3_get_rss_key_size,
1042 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1043 .get_rxfh = hns3_get_rss,
1044 .set_rxfh = hns3_set_rss,
1045 .get_link_ksettings = hns3_get_link_ksettings,
1046 .get_channels = hns3_get_channels,
1047 .get_coalesce = hns3_get_coalesce,
1048 .set_coalesce = hns3_set_coalesce,
1049 .get_link = hns3_get_link,
1050 };
1051
1052 static const struct ethtool_ops hns3_ethtool_ops = {
1053 .self_test = hns3_self_test,
1054 .get_drvinfo = hns3_get_drvinfo,
1055 .get_link = hns3_get_link,
1056 .get_ringparam = hns3_get_ringparam,
1057 .set_ringparam = hns3_set_ringparam,
1058 .get_pauseparam = hns3_get_pauseparam,
1059 .set_pauseparam = hns3_set_pauseparam,
1060 .get_strings = hns3_get_strings,
1061 .get_ethtool_stats = hns3_get_stats,
1062 .get_sset_count = hns3_get_sset_count,
1063 .get_rxnfc = hns3_get_rxnfc,
1064 .set_rxnfc = hns3_set_rxnfc,
1065 .get_rxfh_key_size = hns3_get_rss_key_size,
1066 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1067 .get_rxfh = hns3_get_rss,
1068 .set_rxfh = hns3_set_rss,
1069 .get_link_ksettings = hns3_get_link_ksettings,
1070 .set_link_ksettings = hns3_set_link_ksettings,
1071 .nway_reset = hns3_nway_reset,
1072 .get_channels = hns3_get_channels,
1073 .set_channels = hns3_set_channels,
1074 .get_coalesce = hns3_get_coalesce,
1075 .set_coalesce = hns3_set_coalesce,
1076 .get_regs_len = hns3_get_regs_len,
1077 .get_regs = hns3_get_regs,
1078 .set_phys_id = hns3_set_phys_id,
1079 };
1080
hns3_ethtool_set_ops(struct net_device * netdev)1081 void hns3_ethtool_set_ops(struct net_device *netdev)
1082 {
1083 struct hnae3_handle *h = hns3_get_handle(netdev);
1084
1085 if (h->flags & HNAE3_SUPPORT_VF)
1086 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1087 else
1088 netdev->ethtool_ops = &hns3_ethtool_ops;
1089 }
1090