1
2
3
4#include <linux/etherdevice.h>
5#include <linux/string.h>
6#include <linux/phy.h>
7#include <linux/sfp.h>
8
9#include "hns3_enet.h"
10
11struct hns3_stats {
12 char stats_string[ETH_GSTRING_LEN];
13 int stats_offset;
14};
15
16struct hns3_sfp_type {
17 u8 type;
18 u8 ext_type;
19};
20
21struct hns3_pflag_desc {
22 char name[ETH_GSTRING_LEN];
23 void (*handler)(struct net_device *netdev, bool enable);
24};
25
26
27#define HNS3_TQP_STAT(_string, _member) { \
28 .stats_string = _string, \
29 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
30 offsetof(struct ring_stats, _member), \
31}
32
33static const struct hns3_stats hns3_txq_stats[] = {
34
35 HNS3_TQP_STAT("dropped", sw_err_cnt),
36 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
37 HNS3_TQP_STAT("packets", tx_pkts),
38 HNS3_TQP_STAT("bytes", tx_bytes),
39 HNS3_TQP_STAT("more", tx_more),
40 HNS3_TQP_STAT("wake", restart_queue),
41 HNS3_TQP_STAT("busy", tx_busy),
42 HNS3_TQP_STAT("copy", tx_copy),
43 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
44 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
45 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
46 HNS3_TQP_STAT("tso_err", tx_tso_err),
47 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
48 HNS3_TQP_STAT("hw_limitation", hw_limitation),
49 HNS3_TQP_STAT("bounce", tx_bounce),
50 HNS3_TQP_STAT("spare_full", tx_spare_full),
51 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
52 HNS3_TQP_STAT("sgl", tx_sgl),
53 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
54 HNS3_TQP_STAT("map_sg_err", map_sg_err),
55};
56
57#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
58
59static const struct hns3_stats hns3_rxq_stats[] = {
60
61 HNS3_TQP_STAT("dropped", sw_err_cnt),
62 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
63 HNS3_TQP_STAT("packets", rx_pkts),
64 HNS3_TQP_STAT("bytes", rx_bytes),
65 HNS3_TQP_STAT("errors", rx_err_cnt),
66 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
67 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
68 HNS3_TQP_STAT("err_bd_num", err_bd_num),
69 HNS3_TQP_STAT("l2_err", l2_err),
70 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
71 HNS3_TQP_STAT("csum_complete", csum_complete),
72 HNS3_TQP_STAT("multicast", rx_multicast),
73 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
74 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
75 HNS3_TQP_STAT("frag_alloc", frag_alloc),
76};
77
78#define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
79
80#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
81
82#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
83
84#define HNS3_SELF_TEST_TYPE_NUM 4
85#define HNS3_NIC_LB_TEST_PKT_NUM 1
86#define HNS3_NIC_LB_TEST_RING_ID 0
87#define HNS3_NIC_LB_TEST_PACKET_SIZE 128
88#define HNS3_NIC_LB_SETUP_USEC 10000
89
90
91#define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
92#define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
93#define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
94
95static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
96{
97 struct hnae3_handle *h = hns3_get_handle(ndev);
98 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
99 int ret;
100
101 if (!h->ae_algo->ops->set_loopback ||
102 !h->ae_algo->ops->set_promisc_mode)
103 return -EOPNOTSUPP;
104
105 switch (loop) {
106 case HNAE3_LOOP_SERIAL_SERDES:
107 case HNAE3_LOOP_PARALLEL_SERDES:
108 case HNAE3_LOOP_APP:
109 case HNAE3_LOOP_PHY:
110 ret = h->ae_algo->ops->set_loopback(h, loop, en);
111 break;
112 default:
113 ret = -ENOTSUPP;
114 break;
115 }
116
117 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
118 return ret;
119
120 if (en)
121 h->ae_algo->ops->set_promisc_mode(h, true, true);
122 else
123
124 hns3_request_update_promisc_mode(h);
125
126 return ret;
127}
128
129static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
130{
131 struct hnae3_handle *h = hns3_get_handle(ndev);
132 int ret;
133
134 ret = hns3_nic_reset_all_ring(h);
135 if (ret)
136 return ret;
137
138 ret = hns3_lp_setup(ndev, loop_mode, true);
139 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
140
141 return ret;
142}
143
144static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
145{
146 int ret;
147
148 ret = hns3_lp_setup(ndev, loop_mode, false);
149 if (ret) {
150 netdev_err(ndev, "lb_setup return error: %d\n", ret);
151 return ret;
152 }
153
154 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
155
156 return 0;
157}
158
159static void hns3_lp_setup_skb(struct sk_buff *skb)
160{
161#define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
162
163 struct net_device *ndev = skb->dev;
164 struct hnae3_handle *handle;
165 struct hnae3_ae_dev *ae_dev;
166 unsigned char *packet;
167 struct ethhdr *ethh;
168 unsigned int i;
169
170 skb_reserve(skb, NET_IP_ALIGN);
171 ethh = skb_put(skb, sizeof(struct ethhdr));
172 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
173
174 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
175
176
177
178
179
180
181 handle = hns3_get_handle(ndev);
182 ae_dev = pci_get_drvdata(handle->pdev);
183 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
184 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
185 eth_zero_addr(ethh->h_source);
186 ethh->h_proto = htons(ETH_P_ARP);
187 skb_reset_mac_header(skb);
188
189 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
190 packet[i] = (unsigned char)(i & 0xff);
191}
192
193static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
194 struct sk_buff *skb)
195{
196 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
197 unsigned char *packet = skb->data;
198 u32 len = skb_headlen(skb);
199 u32 i;
200
201 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
202
203 for (i = 0; i < len; i++)
204 if (packet[i] != (unsigned char)(i & 0xff))
205 break;
206
207
208 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
209 tqp_vector->rx_group.total_packets++;
210 else
211 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
212 skb->data, len, true);
213
214 dev_kfree_skb_any(skb);
215}
216
217static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
218{
219 struct hnae3_handle *h = priv->ae_handle;
220 struct hnae3_knic_private_info *kinfo;
221 u32 i, rcv_good_pkt_total = 0;
222
223 kinfo = &h->kinfo;
224 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
225 struct hns3_enet_ring *ring = &priv->ring[i];
226 struct hns3_enet_ring_group *rx_group;
227 u64 pre_rx_pkt;
228
229 rx_group = &ring->tqp_vector->rx_group;
230 pre_rx_pkt = rx_group->total_packets;
231
232 preempt_disable();
233 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
234 preempt_enable();
235
236 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
237 rx_group->total_packets = pre_rx_pkt;
238 }
239 return rcv_good_pkt_total;
240}
241
242static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
243 u32 end_ringid, u32 budget)
244{
245 u32 i;
246
247 for (i = start_ringid; i <= end_ringid; i++) {
248 struct hns3_enet_ring *ring = &priv->ring[i];
249
250 hns3_clean_tx_ring(ring, 0);
251 }
252}
253
254
255
256
257
258
259static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
260{
261 struct hns3_nic_priv *priv = netdev_priv(ndev);
262 struct sk_buff *skb;
263 u32 i, good_cnt;
264 int ret_val = 0;
265
266 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
267 GFP_KERNEL);
268 if (!skb)
269 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
270
271 skb->dev = ndev;
272 hns3_lp_setup_skb(skb);
273 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
274
275 good_cnt = 0;
276 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
277 netdev_tx_t tx_ret;
278
279 skb_get(skb);
280 tx_ret = hns3_nic_net_xmit(skb, ndev);
281 if (tx_ret == NETDEV_TX_OK) {
282 good_cnt++;
283 } else {
284 kfree_skb(skb);
285 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
286 tx_ret);
287 }
288 }
289 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
290 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
291 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
292 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
293 goto out;
294 }
295
296
297 msleep(200);
298
299 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
300 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
301 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
302 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
303 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
304 }
305
306out:
307 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
308 HNS3_NIC_LB_TEST_RING_ID,
309 HNS3_NIC_LB_TEST_PKT_NUM);
310
311 kfree_skb(skb);
312 return ret_val;
313}
314
315
316
317
318
319
320
321static void hns3_self_test(struct net_device *ndev,
322 struct ethtool_test *eth_test, u64 *data)
323{
324 struct hns3_nic_priv *priv = netdev_priv(ndev);
325 struct hnae3_handle *h = priv->ae_handle;
326 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
327 bool if_running = netif_running(ndev);
328 int test_index = 0;
329 u32 i;
330
331 if (hns3_nic_resetting(ndev)) {
332 netdev_err(ndev, "dev resetting!");
333 return;
334 }
335
336
337 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
338 return;
339
340 netif_dbg(h, drv, ndev, "self test start");
341
342 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
343 st_param[HNAE3_LOOP_APP][1] =
344 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
345
346 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
347 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
348 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
349
350 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
351 HNAE3_LOOP_PARALLEL_SERDES;
352 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
353 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
354
355 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
356 st_param[HNAE3_LOOP_PHY][1] =
357 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
358
359 if (if_running)
360 ndev->netdev_ops->ndo_stop(ndev);
361
362#if IS_ENABLED(CONFIG_VLAN_8021Q)
363
364 if (h->ae_algo->ops->enable_vlan_filter)
365 h->ae_algo->ops->enable_vlan_filter(h, false);
366#endif
367
368
369
370
371
372 if (h->ae_algo->ops->halt_autoneg)
373 h->ae_algo->ops->halt_autoneg(h, true);
374
375 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
376
377 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
378 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
379
380 if (!st_param[i][1])
381 continue;
382
383 data[test_index] = hns3_lp_up(ndev, loop_type);
384 if (!data[test_index])
385 data[test_index] = hns3_lp_run_test(ndev, loop_type);
386
387 hns3_lp_down(ndev, loop_type);
388
389 if (data[test_index])
390 eth_test->flags |= ETH_TEST_FL_FAILED;
391
392 test_index++;
393 }
394
395 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
396
397 if (h->ae_algo->ops->halt_autoneg)
398 h->ae_algo->ops->halt_autoneg(h, false);
399
400#if IS_ENABLED(CONFIG_VLAN_8021Q)
401 if (h->ae_algo->ops->enable_vlan_filter)
402 h->ae_algo->ops->enable_vlan_filter(h, true);
403#endif
404
405 if (if_running)
406 ndev->netdev_ops->ndo_open(ndev);
407
408 netif_dbg(h, drv, ndev, "self test end\n");
409}
410
411static void hns3_update_limit_promisc_mode(struct net_device *netdev,
412 bool enable)
413{
414 struct hnae3_handle *handle = hns3_get_handle(netdev);
415
416 if (enable)
417 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
418 else
419 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
420
421 hns3_request_update_promisc_mode(handle);
422}
423
424static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
425 { "limit_promisc", hns3_update_limit_promisc_mode }
426};
427
428static int hns3_get_sset_count(struct net_device *netdev, int stringset)
429{
430 struct hnae3_handle *h = hns3_get_handle(netdev);
431 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
432
433 if (!ops->get_sset_count)
434 return -EOPNOTSUPP;
435
436 switch (stringset) {
437 case ETH_SS_STATS:
438 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
439 ops->get_sset_count(h, stringset));
440
441 case ETH_SS_TEST:
442 return ops->get_sset_count(h, stringset);
443
444 case ETH_SS_PRIV_FLAGS:
445 return HNAE3_PFLAG_MAX;
446
447 default:
448 return -EOPNOTSUPP;
449 }
450}
451
452static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
453 u32 stat_count, u32 num_tqps, const char *prefix)
454{
455#define MAX_PREFIX_SIZE (6 + 4)
456 u32 size_left;
457 u32 i, j;
458 u32 n1;
459
460 for (i = 0; i < num_tqps; i++) {
461 for (j = 0; j < stat_count; j++) {
462 data[ETH_GSTRING_LEN - 1] = '\0';
463
464
465 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
466 prefix, i);
467 size_left = (ETH_GSTRING_LEN - 1) - n1;
468
469
470 strncat(data, stats[j].stats_string, size_left);
471 data += ETH_GSTRING_LEN;
472 }
473 }
474
475 return data;
476}
477
478static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
479{
480 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
481 const char tx_prefix[] = "txq";
482 const char rx_prefix[] = "rxq";
483
484
485 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
486 kinfo->num_tqps, tx_prefix);
487
488
489 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
490 kinfo->num_tqps, rx_prefix);
491
492 return data;
493}
494
495static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
496{
497 struct hnae3_handle *h = hns3_get_handle(netdev);
498 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
499 char *buff = (char *)data;
500 int i;
501
502 if (!ops->get_strings)
503 return;
504
505 switch (stringset) {
506 case ETH_SS_STATS:
507 buff = hns3_get_strings_tqps(h, buff);
508 ops->get_strings(h, stringset, (u8 *)buff);
509 break;
510 case ETH_SS_TEST:
511 ops->get_strings(h, stringset, data);
512 break;
513 case ETH_SS_PRIV_FLAGS:
514 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
515 snprintf(buff, ETH_GSTRING_LEN, "%s",
516 hns3_priv_flags[i].name);
517 buff += ETH_GSTRING_LEN;
518 }
519 break;
520 default:
521 break;
522 }
523}
524
525static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
526{
527 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
528 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
529 struct hns3_enet_ring *ring;
530 u8 *stat;
531 int i, j;
532
533
534 for (i = 0; i < kinfo->num_tqps; i++) {
535 ring = &nic_priv->ring[i];
536 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
537 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
538 *data++ = *(u64 *)stat;
539 }
540 }
541
542
543 for (i = 0; i < kinfo->num_tqps; i++) {
544 ring = &nic_priv->ring[i + kinfo->num_tqps];
545 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
546 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
547 *data++ = *(u64 *)stat;
548 }
549 }
550
551 return data;
552}
553
554
555
556
557
558
559static void hns3_get_stats(struct net_device *netdev,
560 struct ethtool_stats *stats, u64 *data)
561{
562 struct hnae3_handle *h = hns3_get_handle(netdev);
563 u64 *p = data;
564
565 if (hns3_nic_resetting(netdev)) {
566 netdev_err(netdev, "dev resetting, could not get stats\n");
567 return;
568 }
569
570 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
571 netdev_err(netdev, "could not get any statistics\n");
572 return;
573 }
574
575 h->ae_algo->ops->update_stats(h, &netdev->stats);
576
577
578 p = hns3_get_stats_tqps(h, p);
579
580
581 h->ae_algo->ops->get_stats(h, p);
582}
583
584static void hns3_get_drvinfo(struct net_device *netdev,
585 struct ethtool_drvinfo *drvinfo)
586{
587 struct hns3_nic_priv *priv = netdev_priv(netdev);
588 struct hnae3_handle *h = priv->ae_handle;
589 u32 fw_version;
590
591 if (!h->ae_algo->ops->get_fw_version) {
592 netdev_err(netdev, "could not get fw version!\n");
593 return;
594 }
595
596 strncpy(drvinfo->driver, h->pdev->driver->name,
597 sizeof(drvinfo->driver));
598 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
599
600 strncpy(drvinfo->bus_info, pci_name(h->pdev),
601 sizeof(drvinfo->bus_info));
602 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
603
604 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
605
606 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
607 "%lu.%lu.%lu.%lu",
608 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
609 HNAE3_FW_VERSION_BYTE3_SHIFT),
610 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
611 HNAE3_FW_VERSION_BYTE2_SHIFT),
612 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
613 HNAE3_FW_VERSION_BYTE1_SHIFT),
614 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
615 HNAE3_FW_VERSION_BYTE0_SHIFT));
616}
617
618static u32 hns3_get_link(struct net_device *netdev)
619{
620 struct hnae3_handle *h = hns3_get_handle(netdev);
621
622 if (h->ae_algo->ops->get_status)
623 return h->ae_algo->ops->get_status(h);
624 else
625 return 0;
626}
627
628static void hns3_get_ringparam(struct net_device *netdev,
629 struct ethtool_ringparam *param)
630{
631 struct hns3_nic_priv *priv = netdev_priv(netdev);
632 struct hnae3_handle *h = priv->ae_handle;
633 int queue_num = h->kinfo.num_tqps;
634
635 if (hns3_nic_resetting(netdev)) {
636 netdev_err(netdev, "dev resetting!");
637 return;
638 }
639
640 param->tx_max_pending = HNS3_RING_MAX_PENDING;
641 param->rx_max_pending = HNS3_RING_MAX_PENDING;
642
643 param->tx_pending = priv->ring[0].desc_num;
644 param->rx_pending = priv->ring[queue_num].desc_num;
645}
646
647static void hns3_get_pauseparam(struct net_device *netdev,
648 struct ethtool_pauseparam *param)
649{
650 struct hnae3_handle *h = hns3_get_handle(netdev);
651 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
652
653 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
654 return;
655
656 if (h->ae_algo->ops->get_pauseparam)
657 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
658 ¶m->rx_pause, ¶m->tx_pause);
659}
660
661static int hns3_set_pauseparam(struct net_device *netdev,
662 struct ethtool_pauseparam *param)
663{
664 struct hnae3_handle *h = hns3_get_handle(netdev);
665 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
666
667 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
668 return -EOPNOTSUPP;
669
670 netif_dbg(h, drv, netdev,
671 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
672 param->autoneg, param->rx_pause, param->tx_pause);
673
674 if (h->ae_algo->ops->set_pauseparam)
675 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
676 param->rx_pause,
677 param->tx_pause);
678 return -EOPNOTSUPP;
679}
680
681static void hns3_get_ksettings(struct hnae3_handle *h,
682 struct ethtool_link_ksettings *cmd)
683{
684 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
685
686
687 if (ops->get_ksettings_an_result)
688 ops->get_ksettings_an_result(h,
689 &cmd->base.autoneg,
690 &cmd->base.speed,
691 &cmd->base.duplex);
692
693
694 if (ops->get_link_mode)
695 ops->get_link_mode(h,
696 cmd->link_modes.supported,
697 cmd->link_modes.advertising);
698
699
700 if (ops->get_mdix_mode)
701 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
702 &cmd->base.eth_tp_mdix);
703}
704
705static int hns3_get_link_ksettings(struct net_device *netdev,
706 struct ethtool_link_ksettings *cmd)
707{
708 struct hnae3_handle *h = hns3_get_handle(netdev);
709 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
710 const struct hnae3_ae_ops *ops;
711 u8 module_type;
712 u8 media_type;
713 u8 link_stat;
714
715 ops = h->ae_algo->ops;
716 if (ops->get_media_type)
717 ops->get_media_type(h, &media_type, &module_type);
718 else
719 return -EOPNOTSUPP;
720
721 switch (media_type) {
722 case HNAE3_MEDIA_TYPE_NONE:
723 cmd->base.port = PORT_NONE;
724 hns3_get_ksettings(h, cmd);
725 break;
726 case HNAE3_MEDIA_TYPE_FIBER:
727 if (module_type == HNAE3_MODULE_TYPE_CR)
728 cmd->base.port = PORT_DA;
729 else
730 cmd->base.port = PORT_FIBRE;
731
732 hns3_get_ksettings(h, cmd);
733 break;
734 case HNAE3_MEDIA_TYPE_BACKPLANE:
735 cmd->base.port = PORT_NONE;
736 hns3_get_ksettings(h, cmd);
737 break;
738 case HNAE3_MEDIA_TYPE_COPPER:
739 cmd->base.port = PORT_TP;
740 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
741 ops->get_phy_link_ksettings)
742 ops->get_phy_link_ksettings(h, cmd);
743 else if (!netdev->phydev)
744 hns3_get_ksettings(h, cmd);
745 else
746 phy_ethtool_ksettings_get(netdev->phydev, cmd);
747 break;
748 default:
749
750 netdev_warn(netdev, "Unknown media type");
751 return 0;
752 }
753
754
755 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
756
757 link_stat = hns3_get_link(netdev);
758 if (!link_stat) {
759 cmd->base.speed = SPEED_UNKNOWN;
760 cmd->base.duplex = DUPLEX_UNKNOWN;
761 }
762
763 return 0;
764}
765
766static int hns3_check_ksettings_param(const struct net_device *netdev,
767 const struct ethtool_link_ksettings *cmd)
768{
769 struct hnae3_handle *handle = hns3_get_handle(netdev);
770 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
771 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
772 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
773 u8 autoneg;
774 u32 speed;
775 u8 duplex;
776 int ret;
777
778
779
780
781 if (cmd->base.autoneg)
782 return 0;
783
784 if (ops->get_ksettings_an_result) {
785 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
786 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
787 cmd->base.duplex == duplex)
788 return 0;
789 }
790
791 if (ops->get_media_type)
792 ops->get_media_type(handle, &media_type, &module_type);
793
794 if (cmd->base.duplex == DUPLEX_HALF &&
795 media_type != HNAE3_MEDIA_TYPE_COPPER) {
796 netdev_err(netdev,
797 "only copper port supports half duplex!");
798 return -EINVAL;
799 }
800
801 if (ops->check_port_speed) {
802 ret = ops->check_port_speed(handle, cmd->base.speed);
803 if (ret) {
804 netdev_err(netdev, "unsupported speed\n");
805 return ret;
806 }
807 }
808
809 return 0;
810}
811
812static int hns3_set_link_ksettings(struct net_device *netdev,
813 const struct ethtool_link_ksettings *cmd)
814{
815 struct hnae3_handle *handle = hns3_get_handle(netdev);
816 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
817 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
818 int ret;
819
820
821 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
822 return -EINVAL;
823
824 netif_dbg(handle, drv, netdev,
825 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
826 netdev->phydev ? "phy" : "mac",
827 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
828
829
830 if (netdev->phydev) {
831 if (cmd->base.speed == SPEED_1000 &&
832 cmd->base.autoneg == AUTONEG_DISABLE)
833 return -EINVAL;
834
835 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
836 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
837 ops->set_phy_link_ksettings) {
838 return ops->set_phy_link_ksettings(handle, cmd);
839 }
840
841 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
842 return -EOPNOTSUPP;
843
844 ret = hns3_check_ksettings_param(netdev, cmd);
845 if (ret)
846 return ret;
847
848 if (ops->set_autoneg) {
849 ret = ops->set_autoneg(handle, cmd->base.autoneg);
850 if (ret)
851 return ret;
852 }
853
854
855
856
857 if (cmd->base.autoneg) {
858 netdev_info(netdev,
859 "autoneg is on, ignore the speed and duplex\n");
860 return 0;
861 }
862
863 if (ops->cfg_mac_speed_dup_h)
864 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
865 cmd->base.duplex);
866
867 return ret;
868}
869
870static u32 hns3_get_rss_key_size(struct net_device *netdev)
871{
872 struct hnae3_handle *h = hns3_get_handle(netdev);
873
874 if (!h->ae_algo->ops->get_rss_key_size)
875 return 0;
876
877 return h->ae_algo->ops->get_rss_key_size(h);
878}
879
880static u32 hns3_get_rss_indir_size(struct net_device *netdev)
881{
882 struct hnae3_handle *h = hns3_get_handle(netdev);
883 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
884
885 return ae_dev->dev_specs.rss_ind_tbl_size;
886}
887
888static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
889 u8 *hfunc)
890{
891 struct hnae3_handle *h = hns3_get_handle(netdev);
892
893 if (!h->ae_algo->ops->get_rss)
894 return -EOPNOTSUPP;
895
896 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
897}
898
899static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
900 const u8 *key, const u8 hfunc)
901{
902 struct hnae3_handle *h = hns3_get_handle(netdev);
903 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
904
905 if (!h->ae_algo->ops->set_rss)
906 return -EOPNOTSUPP;
907
908 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
909 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
910 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
911 netdev_err(netdev, "hash func not supported\n");
912 return -EOPNOTSUPP;
913 }
914
915 if (!indir) {
916 netdev_err(netdev,
917 "set rss failed for indir is empty\n");
918 return -EOPNOTSUPP;
919 }
920
921 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
922}
923
924static int hns3_get_rxnfc(struct net_device *netdev,
925 struct ethtool_rxnfc *cmd,
926 u32 *rule_locs)
927{
928 struct hnae3_handle *h = hns3_get_handle(netdev);
929
930 switch (cmd->cmd) {
931 case ETHTOOL_GRXRINGS:
932 cmd->data = h->kinfo.num_tqps;
933 return 0;
934 case ETHTOOL_GRXFH:
935 if (h->ae_algo->ops->get_rss_tuple)
936 return h->ae_algo->ops->get_rss_tuple(h, cmd);
937 return -EOPNOTSUPP;
938 case ETHTOOL_GRXCLSRLCNT:
939 if (h->ae_algo->ops->get_fd_rule_cnt)
940 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
941 return -EOPNOTSUPP;
942 case ETHTOOL_GRXCLSRULE:
943 if (h->ae_algo->ops->get_fd_rule_info)
944 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
945 return -EOPNOTSUPP;
946 case ETHTOOL_GRXCLSRLALL:
947 if (h->ae_algo->ops->get_fd_all_rules)
948 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
949 rule_locs);
950 return -EOPNOTSUPP;
951 default:
952 return -EOPNOTSUPP;
953 }
954}
955
956static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
957 u32 tx_desc_num, u32 rx_desc_num)
958{
959 struct hnae3_handle *h = priv->ae_handle;
960 int i;
961
962 h->kinfo.num_tx_desc = tx_desc_num;
963 h->kinfo.num_rx_desc = rx_desc_num;
964
965 for (i = 0; i < h->kinfo.num_tqps; i++) {
966 priv->ring[i].desc_num = tx_desc_num;
967 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
968 }
969}
970
971static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
972{
973 struct hnae3_handle *handle = priv->ae_handle;
974 struct hns3_enet_ring *tmp_rings;
975 int i;
976
977 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
978 sizeof(struct hns3_enet_ring), GFP_KERNEL);
979 if (!tmp_rings)
980 return NULL;
981
982 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
983 memcpy(&tmp_rings[i], &priv->ring[i],
984 sizeof(struct hns3_enet_ring));
985 tmp_rings[i].skb = NULL;
986 }
987
988 return tmp_rings;
989}
990
991static int hns3_check_ringparam(struct net_device *ndev,
992 struct ethtool_ringparam *param)
993{
994 if (hns3_nic_resetting(ndev))
995 return -EBUSY;
996
997 if (param->rx_mini_pending || param->rx_jumbo_pending)
998 return -EINVAL;
999
1000 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1001 param->tx_pending < HNS3_RING_MIN_PENDING ||
1002 param->rx_pending > HNS3_RING_MAX_PENDING ||
1003 param->rx_pending < HNS3_RING_MIN_PENDING) {
1004 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1005 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1006 return -EINVAL;
1007 }
1008
1009 return 0;
1010}
1011
1012static int hns3_set_ringparam(struct net_device *ndev,
1013 struct ethtool_ringparam *param)
1014{
1015 struct hns3_nic_priv *priv = netdev_priv(ndev);
1016 struct hnae3_handle *h = priv->ae_handle;
1017 struct hns3_enet_ring *tmp_rings;
1018 bool if_running = netif_running(ndev);
1019 u32 old_tx_desc_num, new_tx_desc_num;
1020 u32 old_rx_desc_num, new_rx_desc_num;
1021 u16 queue_num = h->kinfo.num_tqps;
1022 int ret, i;
1023
1024 ret = hns3_check_ringparam(ndev, param);
1025 if (ret)
1026 return ret;
1027
1028
1029 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1030 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1031 old_tx_desc_num = priv->ring[0].desc_num;
1032 old_rx_desc_num = priv->ring[queue_num].desc_num;
1033 if (old_tx_desc_num == new_tx_desc_num &&
1034 old_rx_desc_num == new_rx_desc_num)
1035 return 0;
1036
1037 tmp_rings = hns3_backup_ringparam(priv);
1038 if (!tmp_rings) {
1039 netdev_err(ndev,
1040 "backup ring param failed by allocating memory fail\n");
1041 return -ENOMEM;
1042 }
1043
1044 netdev_info(ndev,
1045 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1046 old_tx_desc_num, old_rx_desc_num,
1047 new_tx_desc_num, new_rx_desc_num);
1048
1049 if (if_running)
1050 ndev->netdev_ops->ndo_stop(ndev);
1051
1052 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1053 ret = hns3_init_all_ring(priv);
1054 if (ret) {
1055 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1056 ret);
1057
1058 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1059 old_rx_desc_num);
1060 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1061 memcpy(&priv->ring[i], &tmp_rings[i],
1062 sizeof(struct hns3_enet_ring));
1063 } else {
1064 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1065 hns3_fini_ring(&tmp_rings[i]);
1066 }
1067
1068 kfree(tmp_rings);
1069
1070 if (if_running)
1071 ret = ndev->netdev_ops->ndo_open(ndev);
1072
1073 return ret;
1074}
1075
1076static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1077{
1078 struct hnae3_handle *h = hns3_get_handle(netdev);
1079
1080 switch (cmd->cmd) {
1081 case ETHTOOL_SRXFH:
1082 if (h->ae_algo->ops->set_rss_tuple)
1083 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1084 return -EOPNOTSUPP;
1085 case ETHTOOL_SRXCLSRLINS:
1086 if (h->ae_algo->ops->add_fd_entry)
1087 return h->ae_algo->ops->add_fd_entry(h, cmd);
1088 return -EOPNOTSUPP;
1089 case ETHTOOL_SRXCLSRLDEL:
1090 if (h->ae_algo->ops->del_fd_entry)
1091 return h->ae_algo->ops->del_fd_entry(h, cmd);
1092 return -EOPNOTSUPP;
1093 default:
1094 return -EOPNOTSUPP;
1095 }
1096}
1097
1098static int hns3_nway_reset(struct net_device *netdev)
1099{
1100 struct hnae3_handle *handle = hns3_get_handle(netdev);
1101 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1102 struct phy_device *phy = netdev->phydev;
1103 int autoneg;
1104
1105 if (!netif_running(netdev))
1106 return 0;
1107
1108 if (hns3_nic_resetting(netdev)) {
1109 netdev_err(netdev, "dev resetting!");
1110 return -EBUSY;
1111 }
1112
1113 if (!ops->get_autoneg || !ops->restart_autoneg)
1114 return -EOPNOTSUPP;
1115
1116 autoneg = ops->get_autoneg(handle);
1117 if (autoneg != AUTONEG_ENABLE) {
1118 netdev_err(netdev,
1119 "Autoneg is off, don't support to restart it\n");
1120 return -EINVAL;
1121 }
1122
1123 netif_dbg(handle, drv, netdev,
1124 "nway reset (using %s)\n", phy ? "phy" : "mac");
1125
1126 if (phy)
1127 return genphy_restart_aneg(phy);
1128
1129 return ops->restart_autoneg(handle);
1130}
1131
1132static void hns3_get_channels(struct net_device *netdev,
1133 struct ethtool_channels *ch)
1134{
1135 struct hnae3_handle *h = hns3_get_handle(netdev);
1136
1137 if (h->ae_algo->ops->get_channels)
1138 h->ae_algo->ops->get_channels(h, ch);
1139}
1140
1141static int hns3_get_coalesce(struct net_device *netdev,
1142 struct ethtool_coalesce *cmd)
1143{
1144 struct hns3_nic_priv *priv = netdev_priv(netdev);
1145 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1146 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1147 struct hnae3_handle *h = priv->ae_handle;
1148
1149 if (hns3_nic_resetting(netdev))
1150 return -EBUSY;
1151
1152 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1153 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1154
1155 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1156 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1157
1158 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1159 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1160
1161 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1162 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1163
1164 return 0;
1165}
1166
1167static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1168 struct ethtool_coalesce *cmd)
1169{
1170 struct hnae3_handle *handle = hns3_get_handle(netdev);
1171 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1172 u32 rx_gl, tx_gl;
1173
1174 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1175 netdev_err(netdev,
1176 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1177 ae_dev->dev_specs.max_int_gl);
1178 return -EINVAL;
1179 }
1180
1181 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1182 netdev_err(netdev,
1183 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1184 ae_dev->dev_specs.max_int_gl);
1185 return -EINVAL;
1186 }
1187
1188
1189
1190
1191 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1192 return 0;
1193
1194 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1195 if (rx_gl != cmd->rx_coalesce_usecs) {
1196 netdev_info(netdev,
1197 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1198 cmd->rx_coalesce_usecs, rx_gl);
1199 }
1200
1201 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1202 if (tx_gl != cmd->tx_coalesce_usecs) {
1203 netdev_info(netdev,
1204 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1205 cmd->tx_coalesce_usecs, tx_gl);
1206 }
1207
1208 return 0;
1209}
1210
1211static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1212 struct ethtool_coalesce *cmd)
1213{
1214 u32 rl;
1215
1216 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1217 netdev_err(netdev,
1218 "tx_usecs_high must be same as rx_usecs_high.\n");
1219 return -EINVAL;
1220 }
1221
1222 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1223 netdev_err(netdev,
1224 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1225 HNS3_INT_RL_MAX);
1226 return -EINVAL;
1227 }
1228
1229 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1230 if (rl != cmd->rx_coalesce_usecs_high) {
1231 netdev_info(netdev,
1232 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1233 cmd->rx_coalesce_usecs_high, rl);
1234 }
1235
1236 return 0;
1237}
1238
1239static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1240 struct ethtool_coalesce *cmd)
1241{
1242 struct hnae3_handle *handle = hns3_get_handle(netdev);
1243 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1244
1245 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1246 !ae_dev->dev_specs.int_ql_max) {
1247 netdev_err(netdev, "coalesced frames is not supported\n");
1248 return -EOPNOTSUPP;
1249 }
1250
1251 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1252 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1253 netdev_err(netdev,
1254 "invalid coalesced_frames value, range is 0-%u\n",
1255 ae_dev->dev_specs.int_ql_max);
1256 return -ERANGE;
1257 }
1258
1259 return 0;
1260}
1261
1262static int hns3_check_coalesce_para(struct net_device *netdev,
1263 struct ethtool_coalesce *cmd)
1264{
1265 int ret;
1266
1267 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1268 if (ret) {
1269 netdev_err(netdev,
1270 "Check gl coalesce param fail. ret = %d\n", ret);
1271 return ret;
1272 }
1273
1274 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1275 if (ret) {
1276 netdev_err(netdev,
1277 "Check rl coalesce param fail. ret = %d\n", ret);
1278 return ret;
1279 }
1280
1281 return hns3_check_ql_coalesce_param(netdev, cmd);
1282}
1283
1284static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1285 struct ethtool_coalesce *cmd,
1286 u32 queue)
1287{
1288 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1289 struct hns3_nic_priv *priv = netdev_priv(netdev);
1290 struct hnae3_handle *h = priv->ae_handle;
1291 int queue_num = h->kinfo.num_tqps;
1292
1293 tx_vector = priv->ring[queue].tqp_vector;
1294 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1295
1296 tx_vector->tx_group.coal.adapt_enable =
1297 cmd->use_adaptive_tx_coalesce;
1298 rx_vector->rx_group.coal.adapt_enable =
1299 cmd->use_adaptive_rx_coalesce;
1300
1301 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1302 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1303
1304 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1305 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1306
1307 hns3_set_vector_coalesce_tx_gl(tx_vector,
1308 tx_vector->tx_group.coal.int_gl);
1309 hns3_set_vector_coalesce_rx_gl(rx_vector,
1310 rx_vector->rx_group.coal.int_gl);
1311
1312 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1313 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1314
1315 if (tx_vector->tx_group.coal.ql_enable)
1316 hns3_set_vector_coalesce_tx_ql(tx_vector,
1317 tx_vector->tx_group.coal.int_ql);
1318 if (rx_vector->rx_group.coal.ql_enable)
1319 hns3_set_vector_coalesce_rx_ql(rx_vector,
1320 rx_vector->rx_group.coal.int_ql);
1321}
1322
1323static int hns3_set_coalesce(struct net_device *netdev,
1324 struct ethtool_coalesce *cmd)
1325{
1326 struct hnae3_handle *h = hns3_get_handle(netdev);
1327 struct hns3_nic_priv *priv = netdev_priv(netdev);
1328 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1329 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1330 u16 queue_num = h->kinfo.num_tqps;
1331 int ret;
1332 int i;
1333
1334 if (hns3_nic_resetting(netdev))
1335 return -EBUSY;
1336
1337 ret = hns3_check_coalesce_para(netdev, cmd);
1338 if (ret)
1339 return ret;
1340
1341 h->kinfo.int_rl_setting =
1342 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1343
1344 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1345 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1346
1347 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1348 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1349
1350 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1351 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1352
1353 for (i = 0; i < queue_num; i++)
1354 hns3_set_coalesce_per_queue(netdev, cmd, i);
1355
1356 return 0;
1357}
1358
1359static int hns3_get_regs_len(struct net_device *netdev)
1360{
1361 struct hnae3_handle *h = hns3_get_handle(netdev);
1362
1363 if (!h->ae_algo->ops->get_regs_len)
1364 return -EOPNOTSUPP;
1365
1366 return h->ae_algo->ops->get_regs_len(h);
1367}
1368
1369static void hns3_get_regs(struct net_device *netdev,
1370 struct ethtool_regs *cmd, void *data)
1371{
1372 struct hnae3_handle *h = hns3_get_handle(netdev);
1373
1374 if (!h->ae_algo->ops->get_regs)
1375 return;
1376
1377 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1378}
1379
1380static int hns3_set_phys_id(struct net_device *netdev,
1381 enum ethtool_phys_id_state state)
1382{
1383 struct hnae3_handle *h = hns3_get_handle(netdev);
1384
1385 if (!h->ae_algo->ops->set_led_id)
1386 return -EOPNOTSUPP;
1387
1388 return h->ae_algo->ops->set_led_id(h, state);
1389}
1390
1391static u32 hns3_get_msglevel(struct net_device *netdev)
1392{
1393 struct hnae3_handle *h = hns3_get_handle(netdev);
1394
1395 return h->msg_enable;
1396}
1397
1398static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1399{
1400 struct hnae3_handle *h = hns3_get_handle(netdev);
1401
1402 h->msg_enable = msg_level;
1403}
1404
1405
1406static unsigned int loc_to_eth_fec(u8 loc_fec)
1407{
1408 u32 eth_fec = 0;
1409
1410 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1411 eth_fec |= ETHTOOL_FEC_AUTO;
1412 if (loc_fec & BIT(HNAE3_FEC_RS))
1413 eth_fec |= ETHTOOL_FEC_RS;
1414 if (loc_fec & BIT(HNAE3_FEC_BASER))
1415 eth_fec |= ETHTOOL_FEC_BASER;
1416
1417
1418 if (!eth_fec)
1419 eth_fec = ETHTOOL_FEC_OFF;
1420
1421 return eth_fec;
1422}
1423
1424
1425static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1426{
1427 u32 loc_fec = 0;
1428
1429 if (eth_fec & ETHTOOL_FEC_OFF)
1430 return loc_fec;
1431
1432 if (eth_fec & ETHTOOL_FEC_AUTO)
1433 loc_fec |= BIT(HNAE3_FEC_AUTO);
1434 if (eth_fec & ETHTOOL_FEC_RS)
1435 loc_fec |= BIT(HNAE3_FEC_RS);
1436 if (eth_fec & ETHTOOL_FEC_BASER)
1437 loc_fec |= BIT(HNAE3_FEC_BASER);
1438
1439 return loc_fec;
1440}
1441
1442static int hns3_get_fecparam(struct net_device *netdev,
1443 struct ethtool_fecparam *fec)
1444{
1445 struct hnae3_handle *handle = hns3_get_handle(netdev);
1446 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1447 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1448 u8 fec_ability;
1449 u8 fec_mode;
1450
1451 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1452 return -EOPNOTSUPP;
1453
1454 if (!ops->get_fec)
1455 return -EOPNOTSUPP;
1456
1457 ops->get_fec(handle, &fec_ability, &fec_mode);
1458
1459 fec->fec = loc_to_eth_fec(fec_ability);
1460 fec->active_fec = loc_to_eth_fec(fec_mode);
1461
1462 return 0;
1463}
1464
1465static int hns3_set_fecparam(struct net_device *netdev,
1466 struct ethtool_fecparam *fec)
1467{
1468 struct hnae3_handle *handle = hns3_get_handle(netdev);
1469 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1470 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1471 u32 fec_mode;
1472
1473 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1474 return -EOPNOTSUPP;
1475
1476 if (!ops->set_fec)
1477 return -EOPNOTSUPP;
1478 fec_mode = eth_to_loc_fec(fec->fec);
1479
1480 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1481
1482 return ops->set_fec(handle, fec_mode);
1483}
1484
1485static int hns3_get_module_info(struct net_device *netdev,
1486 struct ethtool_modinfo *modinfo)
1487{
1488#define HNS3_SFF_8636_V1_3 0x03
1489
1490 struct hnae3_handle *handle = hns3_get_handle(netdev);
1491 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1492 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1493 struct hns3_sfp_type sfp_type;
1494 int ret;
1495
1496 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1497 !ops->get_module_eeprom)
1498 return -EOPNOTSUPP;
1499
1500 memset(&sfp_type, 0, sizeof(sfp_type));
1501 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1502 (u8 *)&sfp_type);
1503 if (ret)
1504 return ret;
1505
1506 switch (sfp_type.type) {
1507 case SFF8024_ID_SFP:
1508 modinfo->type = ETH_MODULE_SFF_8472;
1509 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1510 break;
1511 case SFF8024_ID_QSFP_8438:
1512 modinfo->type = ETH_MODULE_SFF_8436;
1513 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1514 break;
1515 case SFF8024_ID_QSFP_8436_8636:
1516 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1517 modinfo->type = ETH_MODULE_SFF_8436;
1518 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1519 } else {
1520 modinfo->type = ETH_MODULE_SFF_8636;
1521 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1522 }
1523 break;
1524 case SFF8024_ID_QSFP28_8636:
1525 modinfo->type = ETH_MODULE_SFF_8636;
1526 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1527 break;
1528 default:
1529 netdev_err(netdev, "Optical module unknown: %#x\n",
1530 sfp_type.type);
1531 return -EINVAL;
1532 }
1533
1534 return 0;
1535}
1536
1537static int hns3_get_module_eeprom(struct net_device *netdev,
1538 struct ethtool_eeprom *ee, u8 *data)
1539{
1540 struct hnae3_handle *handle = hns3_get_handle(netdev);
1541 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1542 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1543
1544 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1545 !ops->get_module_eeprom)
1546 return -EOPNOTSUPP;
1547
1548 if (!ee->len)
1549 return -EINVAL;
1550
1551 memset(data, 0, ee->len);
1552
1553 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1554}
1555
1556static u32 hns3_get_priv_flags(struct net_device *netdev)
1557{
1558 struct hnae3_handle *handle = hns3_get_handle(netdev);
1559
1560 return handle->priv_flags;
1561}
1562
1563static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1564{
1565 u32 i;
1566
1567 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1568 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1569 netdev_err(h->netdev, "%s is unsupported\n",
1570 hns3_priv_flags[i].name);
1571 return -EOPNOTSUPP;
1572 }
1573
1574 return 0;
1575}
1576
1577static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1578{
1579 struct hnae3_handle *handle = hns3_get_handle(netdev);
1580 u32 changed = pflags ^ handle->priv_flags;
1581 int ret;
1582 u32 i;
1583
1584 ret = hns3_check_priv_flags(handle, changed);
1585 if (ret)
1586 return ret;
1587
1588 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1589 if (changed & BIT(i)) {
1590 bool enable = !(handle->priv_flags & BIT(i));
1591
1592 if (enable)
1593 handle->priv_flags |= BIT(i);
1594 else
1595 handle->priv_flags &= ~BIT(i);
1596 hns3_priv_flags[i].handler(netdev, enable);
1597 }
1598 }
1599
1600 return 0;
1601}
1602
1603static int hns3_get_tunable(struct net_device *netdev,
1604 const struct ethtool_tunable *tuna,
1605 void *data)
1606{
1607 struct hns3_nic_priv *priv = netdev_priv(netdev);
1608 int ret = 0;
1609
1610 switch (tuna->id) {
1611 case ETHTOOL_TX_COPYBREAK:
1612
1613 *(u32 *)data = priv->tx_copybreak;
1614 break;
1615 case ETHTOOL_RX_COPYBREAK:
1616 *(u32 *)data = priv->rx_copybreak;
1617 break;
1618 default:
1619 ret = -EOPNOTSUPP;
1620 break;
1621 }
1622
1623 return ret;
1624}
1625
1626static int hns3_set_tunable(struct net_device *netdev,
1627 const struct ethtool_tunable *tuna,
1628 const void *data)
1629{
1630 struct hns3_nic_priv *priv = netdev_priv(netdev);
1631 struct hnae3_handle *h = priv->ae_handle;
1632 int i, ret = 0;
1633
1634 switch (tuna->id) {
1635 case ETHTOOL_TX_COPYBREAK:
1636 priv->tx_copybreak = *(u32 *)data;
1637
1638 for (i = 0; i < h->kinfo.num_tqps; i++)
1639 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1640
1641 break;
1642 case ETHTOOL_RX_COPYBREAK:
1643 priv->rx_copybreak = *(u32 *)data;
1644
1645 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1646 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1647
1648 break;
1649 default:
1650 ret = -EOPNOTSUPP;
1651 break;
1652 }
1653
1654 return ret;
1655}
1656
1657#define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1658 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1659 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1660 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1661 ETHTOOL_COALESCE_MAX_FRAMES)
1662
1663static int hns3_get_ts_info(struct net_device *netdev,
1664 struct ethtool_ts_info *info)
1665{
1666 struct hnae3_handle *handle = hns3_get_handle(netdev);
1667
1668 if (handle->ae_algo->ops->get_ts_info)
1669 return handle->ae_algo->ops->get_ts_info(handle, info);
1670
1671 return ethtool_op_get_ts_info(netdev, info);
1672}
1673
1674static const struct ethtool_ops hns3vf_ethtool_ops = {
1675 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1676 .get_drvinfo = hns3_get_drvinfo,
1677 .get_ringparam = hns3_get_ringparam,
1678 .set_ringparam = hns3_set_ringparam,
1679 .get_strings = hns3_get_strings,
1680 .get_ethtool_stats = hns3_get_stats,
1681 .get_sset_count = hns3_get_sset_count,
1682 .get_rxnfc = hns3_get_rxnfc,
1683 .set_rxnfc = hns3_set_rxnfc,
1684 .get_rxfh_key_size = hns3_get_rss_key_size,
1685 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1686 .get_rxfh = hns3_get_rss,
1687 .set_rxfh = hns3_set_rss,
1688 .get_link_ksettings = hns3_get_link_ksettings,
1689 .get_channels = hns3_get_channels,
1690 .set_channels = hns3_set_channels,
1691 .get_coalesce = hns3_get_coalesce,
1692 .set_coalesce = hns3_set_coalesce,
1693 .get_regs_len = hns3_get_regs_len,
1694 .get_regs = hns3_get_regs,
1695 .get_link = hns3_get_link,
1696 .get_msglevel = hns3_get_msglevel,
1697 .set_msglevel = hns3_set_msglevel,
1698 .get_priv_flags = hns3_get_priv_flags,
1699 .set_priv_flags = hns3_set_priv_flags,
1700 .get_tunable = hns3_get_tunable,
1701 .set_tunable = hns3_set_tunable,
1702};
1703
1704static const struct ethtool_ops hns3_ethtool_ops = {
1705 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1706 .self_test = hns3_self_test,
1707 .get_drvinfo = hns3_get_drvinfo,
1708 .get_link = hns3_get_link,
1709 .get_ringparam = hns3_get_ringparam,
1710 .set_ringparam = hns3_set_ringparam,
1711 .get_pauseparam = hns3_get_pauseparam,
1712 .set_pauseparam = hns3_set_pauseparam,
1713 .get_strings = hns3_get_strings,
1714 .get_ethtool_stats = hns3_get_stats,
1715 .get_sset_count = hns3_get_sset_count,
1716 .get_rxnfc = hns3_get_rxnfc,
1717 .set_rxnfc = hns3_set_rxnfc,
1718 .get_rxfh_key_size = hns3_get_rss_key_size,
1719 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1720 .get_rxfh = hns3_get_rss,
1721 .set_rxfh = hns3_set_rss,
1722 .get_link_ksettings = hns3_get_link_ksettings,
1723 .set_link_ksettings = hns3_set_link_ksettings,
1724 .nway_reset = hns3_nway_reset,
1725 .get_channels = hns3_get_channels,
1726 .set_channels = hns3_set_channels,
1727 .get_coalesce = hns3_get_coalesce,
1728 .set_coalesce = hns3_set_coalesce,
1729 .get_regs_len = hns3_get_regs_len,
1730 .get_regs = hns3_get_regs,
1731 .set_phys_id = hns3_set_phys_id,
1732 .get_msglevel = hns3_get_msglevel,
1733 .set_msglevel = hns3_set_msglevel,
1734 .get_fecparam = hns3_get_fecparam,
1735 .set_fecparam = hns3_set_fecparam,
1736 .get_module_info = hns3_get_module_info,
1737 .get_module_eeprom = hns3_get_module_eeprom,
1738 .get_priv_flags = hns3_get_priv_flags,
1739 .set_priv_flags = hns3_set_priv_flags,
1740 .get_ts_info = hns3_get_ts_info,
1741 .get_tunable = hns3_get_tunable,
1742 .set_tunable = hns3_set_tunable,
1743};
1744
1745void hns3_ethtool_set_ops(struct net_device *netdev)
1746{
1747 struct hnae3_handle *h = hns3_get_handle(netdev);
1748
1749 if (h->flags & HNAE3_SUPPORT_VF)
1750 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1751 else
1752 netdev->ethtool_ops = &hns3_ethtool_ops;
1753}
1754