1 /*
2 * Copyright (c) 2014-2015 Hisilicon Limited.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14
15 #include "hns_enet.h"
16
17 #define HNS_PHY_PAGE_MDIX 0
18 #define HNS_PHY_PAGE_LED 3
19 #define HNS_PHY_PAGE_COPPER 0
20
21 #define HNS_PHY_PAGE_REG 22 /* Page Selection Reg. */
22 #define HNS_PHY_CSC_REG 16 /* Copper Specific Control Register */
23 #define HNS_PHY_CSS_REG 17 /* Copper Specific Status Register */
24 #define HNS_LED_FC_REG 16 /* LED Function Control Reg. */
25 #define HNS_LED_PC_REG 17 /* LED Polarity Control Reg. */
26
27 #define HNS_LED_FORCE_ON 9
28 #define HNS_LED_FORCE_OFF 8
29
30 #define HNS_CHIP_VERSION 660
31 #define HNS_NET_STATS_CNT 26
32
33 #define PHY_MDIX_CTRL_S (5)
34 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
35
36 #define PHY_MDIX_STATUS_B (6)
37 #define PHY_SPEED_DUP_RESOLVE_B (11)
38
39 /**
40 *hns_nic_get_link - get current link status
41 *@net_dev: net_device
42 *retuen 0 - success , negative --fail
43 */
hns_nic_get_link(struct net_device * net_dev)44 static u32 hns_nic_get_link(struct net_device *net_dev)
45 {
46 struct hns_nic_priv *priv = netdev_priv(net_dev);
47 u32 link_stat = priv->link;
48 struct hnae_handle *h;
49
50 assert(priv && priv->ae_handle);
51 h = priv->ae_handle;
52
53 if (priv->phy) {
54 if (!genphy_update_link(priv->phy))
55 link_stat = priv->phy->link;
56 else
57 link_stat = 0;
58 }
59
60 if (h->dev && h->dev->ops && h->dev->ops->get_status)
61 link_stat = link_stat && h->dev->ops->get_status(h);
62 else
63 link_stat = 0;
64
65 return link_stat;
66 }
67
hns_get_mdix_mode(struct net_device * net_dev,struct ethtool_cmd * cmd)68 static void hns_get_mdix_mode(struct net_device *net_dev,
69 struct ethtool_cmd *cmd)
70 {
71 int mdix_ctrl, mdix, retval, is_resolved;
72 struct hns_nic_priv *priv = netdev_priv(net_dev);
73 struct phy_device *phy_dev = priv->phy;
74
75 if (!phy_dev || !phy_dev->bus) {
76 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
77 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
78 return;
79 }
80
81 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
82 HNS_PHY_PAGE_MDIX);
83
84 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSC_REG);
85 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
86
87 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSS_REG);
88 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
89 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
90
91 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
92 HNS_PHY_PAGE_COPPER);
93
94 switch (mdix_ctrl) {
95 case 0x0:
96 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
97 break;
98 case 0x1:
99 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
100 break;
101 case 0x3:
102 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
103 break;
104 default:
105 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
106 break;
107 }
108
109 if (!is_resolved)
110 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
111 else if (mdix)
112 cmd->eth_tp_mdix = ETH_TP_MDI_X;
113 else
114 cmd->eth_tp_mdix = ETH_TP_MDI;
115 }
116
117 /**
118 *hns_nic_get_settings - implement ethtool get settings
119 *@net_dev: net_device
120 *@cmd: ethtool_cmd
121 *retuen 0 - success , negative --fail
122 */
hns_nic_get_settings(struct net_device * net_dev,struct ethtool_cmd * cmd)123 static int hns_nic_get_settings(struct net_device *net_dev,
124 struct ethtool_cmd *cmd)
125 {
126 struct hns_nic_priv *priv = netdev_priv(net_dev);
127 struct hnae_handle *h;
128 u32 link_stat;
129 int ret;
130 u8 duplex;
131 u16 speed;
132
133 if (!priv || !priv->ae_handle)
134 return -ESRCH;
135
136 h = priv->ae_handle;
137 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
138 return -ESRCH;
139
140 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
141 if (ret < 0) {
142 netdev_err(net_dev, "%s get_info error!\n", __func__);
143 return -EINVAL;
144 }
145
146 /* When there is no phy, autoneg is off. */
147 cmd->autoneg = false;
148 ethtool_cmd_speed_set(cmd, speed);
149 cmd->duplex = duplex;
150
151 if (priv->phy)
152 (void)phy_ethtool_gset(priv->phy, cmd);
153
154 link_stat = hns_nic_get_link(net_dev);
155 if (!link_stat) {
156 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
157 cmd->duplex = DUPLEX_UNKNOWN;
158 }
159
160 if (cmd->autoneg)
161 cmd->advertising |= ADVERTISED_Autoneg;
162
163 cmd->supported |= h->if_support;
164 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
165 cmd->supported |= SUPPORTED_TP;
166 cmd->advertising |= ADVERTISED_1000baseT_Full;
167 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
168 cmd->supported |= SUPPORTED_FIBRE;
169 cmd->advertising |= ADVERTISED_10000baseKR_Full;
170 }
171
172 if (h->port_type == HNAE_PORT_SERVICE) {
173 cmd->port = PORT_FIBRE;
174 cmd->supported |= SUPPORTED_Pause;
175 } else {
176 cmd->port = PORT_TP;
177 }
178
179 cmd->transceiver = XCVR_EXTERNAL;
180 cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
181 hns_get_mdix_mode(net_dev, cmd);
182
183 return 0;
184 }
185
186 /**
187 *hns_nic_set_settings - implement ethtool set settings
188 *@net_dev: net_device
189 *@cmd: ethtool_cmd
190 *retuen 0 - success , negative --fail
191 */
hns_nic_set_settings(struct net_device * net_dev,struct ethtool_cmd * cmd)192 static int hns_nic_set_settings(struct net_device *net_dev,
193 struct ethtool_cmd *cmd)
194 {
195 struct hns_nic_priv *priv = netdev_priv(net_dev);
196 struct hnae_handle *h;
197 u32 speed;
198
199 if (!netif_running(net_dev))
200 return -ESRCH;
201
202 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
203 !priv->ae_handle->dev->ops)
204 return -ENODEV;
205
206 h = priv->ae_handle;
207 speed = ethtool_cmd_speed(cmd);
208
209 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
210 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
211 cmd->duplex != DUPLEX_FULL)
212 return -EINVAL;
213 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
214 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
215 return -EINVAL;
216
217 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
218 return -EINVAL;
219 if (priv->phy)
220 return phy_ethtool_sset(priv->phy, cmd);
221
222 if ((speed != SPEED_10 && speed != SPEED_100 &&
223 speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
224 cmd->duplex != DUPLEX_FULL))
225 return -EINVAL;
226 } else {
227 netdev_err(net_dev, "Not supported!");
228 return -ENOTSUPP;
229 }
230
231 if (h->dev->ops->adjust_link) {
232 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
233 return 0;
234 }
235
236 netdev_err(net_dev, "Not supported!");
237 return -ENOTSUPP;
238 }
239
240 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
241 "Mac Loopback test",
242 "Serdes Loopback test",
243 "Phy Loopback test"
244 };
245
hns_nic_config_phy_loopback(struct phy_device * phy_dev,u8 en)246 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
247 {
248 #define COPPER_CONTROL_REG 0
249 #define PHY_LOOP_BACK BIT(14)
250 u16 val = 0;
251
252 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
253 return -ENOTSUPP;
254
255 if (en) {
256 /* speed : 1000M */
257 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
258 HNS_PHY_PAGE_REG, 2);
259 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
260 21, 0x1046);
261 /* Force Master */
262 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
263 9, 0x1F00);
264 /* Soft-reset */
265 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
266 0, 0x9140);
267 /* If autoneg disabled,two soft-reset operations */
268 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
269 0, 0x9140);
270 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
271 22, 0xFA);
272
273 /* Default is 0x0400 */
274 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
275 1, 0x418);
276
277 /* Force 1000M Link, Default is 0x0200 */
278 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
279 7, 0x20C);
280 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
281 22, 0);
282
283 /* Enable MAC loop-back */
284 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
285 COPPER_CONTROL_REG);
286 val |= PHY_LOOP_BACK;
287 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
288 COPPER_CONTROL_REG, val);
289 } else {
290 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
291 22, 0xFA);
292 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
293 1, 0x400);
294 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
295 7, 0x200);
296 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
297 22, 0);
298
299 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
300 COPPER_CONTROL_REG);
301 val &= ~PHY_LOOP_BACK;
302 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
303 COPPER_CONTROL_REG, val);
304 }
305 return 0;
306 }
307
__lb_setup(struct net_device * ndev,enum hnae_loop loop)308 static int __lb_setup(struct net_device *ndev,
309 enum hnae_loop loop)
310 {
311 int ret = 0;
312 struct hns_nic_priv *priv = netdev_priv(ndev);
313 struct phy_device *phy_dev = priv->phy;
314 struct hnae_handle *h = priv->ae_handle;
315
316 switch (loop) {
317 case MAC_INTERNALLOOP_PHY:
318 if ((phy_dev) && (!phy_dev->is_c45))
319 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
320 break;
321 case MAC_INTERNALLOOP_MAC:
322 if ((h->dev->ops->set_loopback) &&
323 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
324 ret = h->dev->ops->set_loopback(h, loop, 0x1);
325 break;
326 case MAC_INTERNALLOOP_SERDES:
327 if (h->dev->ops->set_loopback)
328 ret = h->dev->ops->set_loopback(h, loop, 0x1);
329 break;
330 case MAC_LOOP_NONE:
331 if ((phy_dev) && (!phy_dev->is_c45))
332 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
333
334 if (h->dev->ops->set_loopback) {
335 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
336 ret |= h->dev->ops->set_loopback(h,
337 MAC_INTERNALLOOP_MAC, 0x0);
338
339 ret |= h->dev->ops->set_loopback(h,
340 MAC_INTERNALLOOP_SERDES, 0x0);
341 }
342 break;
343 default:
344 ret = -EINVAL;
345 break;
346 }
347
348 return ret;
349 }
350
__lb_up(struct net_device * ndev,enum hnae_loop loop_mode)351 static int __lb_up(struct net_device *ndev,
352 enum hnae_loop loop_mode)
353 {
354 struct hns_nic_priv *priv = netdev_priv(ndev);
355 struct hnae_handle *h = priv->ae_handle;
356 int speed, duplex;
357 int ret;
358
359 hns_nic_net_reset(ndev);
360
361 if (priv->phy) {
362 phy_disconnect(priv->phy);
363 msleep(100);
364
365 ret = hns_nic_init_phy(ndev, h);
366 if (ret)
367 return ret;
368 }
369
370 ret = __lb_setup(ndev, loop_mode);
371 if (ret)
372 return ret;
373
374 msleep(100);
375
376 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
377 if (ret)
378 return ret;
379
380 if (priv->phy)
381 phy_start(priv->phy);
382
383 /* link adjust duplex*/
384 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
385 speed = 1000;
386 else
387 speed = 10000;
388 duplex = 1;
389
390 h->dev->ops->adjust_link(h, speed, duplex);
391
392 return 0;
393 }
394
__lb_other_process(struct hns_nic_ring_data * ring_data,struct sk_buff * skb)395 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
396 struct sk_buff *skb)
397 {
398 struct net_device *ndev;
399 struct hnae_ring *ring;
400 struct netdev_queue *dev_queue;
401 struct sk_buff *new_skb;
402 unsigned int frame_size;
403 int check_ok;
404 u32 i;
405 char buff[33]; /* 32B data and the last character '\0' */
406
407 if (!ring_data) { /* Just for doing create frame*/
408 frame_size = skb->len;
409 memset(skb->data, 0xFF, frame_size);
410 frame_size &= ~1ul;
411 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
412 memset(&skb->data[frame_size / 2 + 10], 0xBE,
413 frame_size / 2 - 11);
414 memset(&skb->data[frame_size / 2 + 12], 0xAF,
415 frame_size / 2 - 13);
416 return;
417 }
418
419 ring = ring_data->ring;
420 ndev = ring_data->napi.dev;
421 if (is_tx_ring(ring)) { /* for tx queue reset*/
422 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
423 netdev_tx_reset_queue(dev_queue);
424 return;
425 }
426
427 frame_size = skb->len;
428 frame_size &= ~1ul;
429 /* for mutl buffer*/
430 new_skb = skb_copy(skb, GFP_ATOMIC);
431 dev_kfree_skb_any(skb);
432 skb = new_skb;
433
434 check_ok = 0;
435 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
436 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
437 (*(skb->data + frame_size / 2 + 12) == 0xAF))
438 check_ok = 1;
439 }
440
441 if (check_ok) {
442 ndev->stats.rx_packets++;
443 ndev->stats.rx_bytes += skb->len;
444 } else {
445 ndev->stats.rx_frame_errors++;
446 for (i = 0; i < skb->len; i++) {
447 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
448 "%02x", *(skb->data + i));
449 if ((i % 16 == 15) || (i == skb->len - 1))
450 pr_info("%s\n", buff);
451 }
452 }
453 dev_kfree_skb_any(skb);
454 }
455
__lb_clean_rings(struct hns_nic_priv * priv,int ringid0,int ringid1,int budget)456 static int __lb_clean_rings(struct hns_nic_priv *priv,
457 int ringid0, int ringid1, int budget)
458 {
459 int i, ret;
460 struct hns_nic_ring_data *ring_data;
461 struct net_device *ndev = priv->netdev;
462 unsigned long rx_packets = ndev->stats.rx_packets;
463 unsigned long rx_bytes = ndev->stats.rx_bytes;
464 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
465
466 for (i = ringid0; i <= ringid1; i++) {
467 ring_data = &priv->ring_data[i];
468 (void)ring_data->poll_one(ring_data,
469 budget, __lb_other_process);
470 }
471 ret = (int)(ndev->stats.rx_packets - rx_packets);
472 ndev->stats.rx_packets = rx_packets;
473 ndev->stats.rx_bytes = rx_bytes;
474 ndev->stats.rx_frame_errors = rx_frame_errors;
475 return ret;
476 }
477
478 /**
479 * nic_run_loopback_test - run loopback test
480 * @nic_dev: net device
481 * @loopback_type: loopback type
482 */
__lb_run_test(struct net_device * ndev,enum hnae_loop loop_mode)483 static int __lb_run_test(struct net_device *ndev,
484 enum hnae_loop loop_mode)
485 {
486 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
487 #define NIC_LB_TEST_RING_ID 0
488 #define NIC_LB_TEST_FRAME_SIZE 128
489 /* nic loopback test err */
490 #define NIC_LB_TEST_NO_MEM_ERR 1
491 #define NIC_LB_TEST_TX_CNT_ERR 2
492 #define NIC_LB_TEST_RX_CNT_ERR 3
493 #define NIC_LB_TEST_RX_PKG_ERR 4
494 struct hns_nic_priv *priv = netdev_priv(ndev);
495 struct hnae_handle *h = priv->ae_handle;
496 int i, j, lc, good_cnt, ret_val = 0;
497 unsigned int size;
498 netdev_tx_t tx_ret_val;
499 struct sk_buff *skb;
500
501 size = NIC_LB_TEST_FRAME_SIZE;
502 /* allocate test skb */
503 skb = alloc_skb(size, GFP_KERNEL);
504 if (!skb)
505 return NIC_LB_TEST_NO_MEM_ERR;
506
507 /* place data into test skb */
508 (void)skb_put(skb, size);
509 __lb_other_process(NULL, skb);
510 skb->queue_mapping = NIC_LB_TEST_RING_ID;
511
512 lc = 1;
513 for (j = 0; j < lc; j++) {
514 /* reset count of good packets */
515 good_cnt = 0;
516 /* place 64 packets on the transmit queue*/
517 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
518 (void)skb_get(skb);
519
520 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
521 ndev, skb,
522 &tx_ring_data(priv, skb->queue_mapping));
523 if (tx_ret_val == NETDEV_TX_OK)
524 good_cnt++;
525 else
526 break;
527 }
528 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
529 ret_val = NIC_LB_TEST_TX_CNT_ERR;
530 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
531 hns_nic_test_strs[loop_mode], good_cnt,
532 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
533 break;
534 }
535
536 /* allow 100 milliseconds for packets to go from Tx to Rx */
537 msleep(100);
538
539 good_cnt = __lb_clean_rings(priv,
540 h->q_num, h->q_num * 2 - 1,
541 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
542 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
543 ret_val = NIC_LB_TEST_RX_CNT_ERR;
544 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
545 hns_nic_test_strs[loop_mode], good_cnt,
546 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
547 break;
548 }
549 (void)__lb_clean_rings(priv,
550 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
551 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
552 }
553
554 /* free the original skb */
555 kfree_skb(skb);
556
557 return ret_val;
558 }
559
__lb_down(struct net_device * ndev)560 static int __lb_down(struct net_device *ndev)
561 {
562 struct hns_nic_priv *priv = netdev_priv(ndev);
563 struct hnae_handle *h = priv->ae_handle;
564 int ret;
565
566 ret = __lb_setup(ndev, MAC_LOOP_NONE);
567 if (ret)
568 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
569 __func__,
570 ret);
571
572 if (priv->phy)
573 phy_stop(priv->phy);
574
575 if (h->dev->ops->stop)
576 h->dev->ops->stop(h);
577
578 usleep_range(10000, 20000);
579 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
580
581 hns_nic_net_reset(ndev);
582
583 return 0;
584 }
585
586 /**
587 * hns_nic_self_test - self test
588 * @dev: net device
589 * @eth_test: test cmd
590 * @data: test result
591 */
hns_nic_self_test(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)592 static void hns_nic_self_test(struct net_device *ndev,
593 struct ethtool_test *eth_test, u64 *data)
594 {
595 struct hns_nic_priv *priv = netdev_priv(ndev);
596 bool if_running = netif_running(ndev);
597 #define SELF_TEST_TPYE_NUM 3
598 int st_param[SELF_TEST_TPYE_NUM][2];
599 int i;
600 int test_index = 0;
601
602 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
603 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
604 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
605 st_param[1][1] = 1; /*serdes must exist*/
606 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
607 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
608 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
609
610 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
611 set_bit(NIC_STATE_TESTING, &priv->state);
612
613 if (if_running)
614 (void)dev_close(ndev);
615
616 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
617 if (!st_param[i][1])
618 continue; /* NEXT testing */
619
620 data[test_index] = __lb_up(ndev,
621 (enum hnae_loop)st_param[i][0]);
622 if (!data[test_index]) {
623 data[test_index] = __lb_run_test(
624 ndev, (enum hnae_loop)st_param[i][0]);
625 (void)__lb_down(ndev);
626 }
627
628 if (data[test_index])
629 eth_test->flags |= ETH_TEST_FL_FAILED;
630
631 test_index++;
632 }
633
634 hns_nic_net_reset(priv->netdev);
635
636 clear_bit(NIC_STATE_TESTING, &priv->state);
637
638 if (if_running)
639 (void)dev_open(ndev);
640 }
641 /* Online tests aren't run; pass by default */
642
643 (void)msleep_interruptible(4 * 1000);
644 }
645
646 /**
647 * hns_nic_get_drvinfo - get net driver info
648 * @dev: net device
649 * @drvinfo: driver info
650 */
hns_nic_get_drvinfo(struct net_device * net_dev,struct ethtool_drvinfo * drvinfo)651 static void hns_nic_get_drvinfo(struct net_device *net_dev,
652 struct ethtool_drvinfo *drvinfo)
653 {
654 struct hns_nic_priv *priv = netdev_priv(net_dev);
655
656 assert(priv);
657
658 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
659 sizeof(drvinfo->version));
660 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
661
662 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
663 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
664
665 strncpy(drvinfo->bus_info, priv->dev->bus->name,
666 sizeof(drvinfo->bus_info));
667 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
668
669 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
670 }
671
672 /**
673 * hns_get_ringparam - get ring parameter
674 * @dev: net device
675 * @param: ethtool parameter
676 */
hns_get_ringparam(struct net_device * net_dev,struct ethtool_ringparam * param)677 void hns_get_ringparam(struct net_device *net_dev,
678 struct ethtool_ringparam *param)
679 {
680 struct hns_nic_priv *priv = netdev_priv(net_dev);
681 struct hnae_ae_ops *ops;
682 struct hnae_queue *queue;
683 u32 uplimit = 0;
684
685 queue = priv->ae_handle->qs[0];
686 ops = priv->ae_handle->dev->ops;
687
688 if (ops->get_ring_bdnum_limit)
689 ops->get_ring_bdnum_limit(queue, &uplimit);
690
691 param->rx_max_pending = uplimit;
692 param->tx_max_pending = uplimit;
693 param->rx_pending = queue->rx_ring.desc_num;
694 param->tx_pending = queue->tx_ring.desc_num;
695 }
696
697 /**
698 * hns_get_pauseparam - get pause parameter
699 * @dev: net device
700 * @param: pause parameter
701 */
hns_get_pauseparam(struct net_device * net_dev,struct ethtool_pauseparam * param)702 static void hns_get_pauseparam(struct net_device *net_dev,
703 struct ethtool_pauseparam *param)
704 {
705 struct hns_nic_priv *priv = netdev_priv(net_dev);
706 struct hnae_ae_ops *ops;
707
708 ops = priv->ae_handle->dev->ops;
709
710 if (ops->get_pauseparam)
711 ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
712 ¶m->rx_pause, ¶m->tx_pause);
713 }
714
715 /**
716 * hns_set_pauseparam - set pause parameter
717 * @dev: net device
718 * @param: pause parameter
719 *
720 * Return 0 on success, negative on failure
721 */
hns_set_pauseparam(struct net_device * net_dev,struct ethtool_pauseparam * param)722 static int hns_set_pauseparam(struct net_device *net_dev,
723 struct ethtool_pauseparam *param)
724 {
725 struct hns_nic_priv *priv = netdev_priv(net_dev);
726 struct hnae_handle *h;
727 struct hnae_ae_ops *ops;
728
729 assert(priv || priv->ae_handle);
730
731 h = priv->ae_handle;
732 ops = h->dev->ops;
733
734 if (!ops->set_pauseparam)
735 return -ESRCH;
736
737 return ops->set_pauseparam(priv->ae_handle, param->autoneg,
738 param->rx_pause, param->tx_pause);
739 }
740
741 /**
742 * hns_get_coalesce - get coalesce info.
743 * @dev: net device
744 * @ec: coalesce info.
745 *
746 * Return 0 on success, negative on failure.
747 */
hns_get_coalesce(struct net_device * net_dev,struct ethtool_coalesce * ec)748 static int hns_get_coalesce(struct net_device *net_dev,
749 struct ethtool_coalesce *ec)
750 {
751 struct hns_nic_priv *priv = netdev_priv(net_dev);
752 struct hnae_ae_ops *ops;
753
754 ops = priv->ae_handle->dev->ops;
755
756 ec->use_adaptive_rx_coalesce = 1;
757 ec->use_adaptive_tx_coalesce = 1;
758
759 if ((!ops->get_coalesce_usecs) ||
760 (!ops->get_rx_max_coalesced_frames))
761 return -ESRCH;
762
763 ops->get_coalesce_usecs(priv->ae_handle,
764 &ec->tx_coalesce_usecs,
765 &ec->rx_coalesce_usecs);
766
767 ops->get_rx_max_coalesced_frames(
768 priv->ae_handle,
769 &ec->tx_max_coalesced_frames,
770 &ec->rx_max_coalesced_frames);
771
772 return 0;
773 }
774
775 /**
776 * hns_set_coalesce - set coalesce info.
777 * @dev: net device
778 * @ec: coalesce info.
779 *
780 * Return 0 on success, negative on failure.
781 */
hns_set_coalesce(struct net_device * net_dev,struct ethtool_coalesce * ec)782 static int hns_set_coalesce(struct net_device *net_dev,
783 struct ethtool_coalesce *ec)
784 {
785 struct hns_nic_priv *priv = netdev_priv(net_dev);
786 struct hnae_ae_ops *ops;
787 int ret;
788
789 assert(priv || priv->ae_handle);
790
791 ops = priv->ae_handle->dev->ops;
792
793 if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
794 return -EINVAL;
795
796 if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
797 return -EINVAL;
798
799 if ((!ops->set_coalesce_usecs) ||
800 (!ops->set_coalesce_frames))
801 return -ESRCH;
802
803 ops->set_coalesce_usecs(priv->ae_handle,
804 ec->rx_coalesce_usecs);
805
806 ret = ops->set_coalesce_frames(
807 priv->ae_handle,
808 ec->rx_max_coalesced_frames);
809
810 return ret;
811 }
812
813 /**
814 * hns_get_channels - get channel info.
815 * @dev: net device
816 * @ch: channel info.
817 */
hns_get_channels(struct net_device * net_dev,struct ethtool_channels * ch)818 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
819 {
820 struct hns_nic_priv *priv = netdev_priv(net_dev);
821
822 ch->max_rx = priv->ae_handle->q_num;
823 ch->max_tx = priv->ae_handle->q_num;
824
825 ch->rx_count = priv->ae_handle->q_num;
826 ch->tx_count = priv->ae_handle->q_num;
827 }
828
829 /**
830 * get_ethtool_stats - get detail statistics.
831 * @dev: net device
832 * @stats: statistics info.
833 * @data: statistics data.
834 */
hns_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)835 void hns_get_ethtool_stats(struct net_device *netdev,
836 struct ethtool_stats *stats, u64 *data)
837 {
838 u64 *p = data;
839 struct hns_nic_priv *priv = netdev_priv(netdev);
840 struct hnae_handle *h = priv->ae_handle;
841 const struct rtnl_link_stats64 *net_stats;
842 struct rtnl_link_stats64 temp;
843
844 if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
845 netdev_err(netdev, "get_stats or update_stats is null!\n");
846 return;
847 }
848
849 h->dev->ops->update_stats(h, &netdev->stats);
850
851 net_stats = dev_get_stats(netdev, &temp);
852
853 /* get netdev statistics */
854 p[0] = net_stats->rx_packets;
855 p[1] = net_stats->tx_packets;
856 p[2] = net_stats->rx_bytes;
857 p[3] = net_stats->tx_bytes;
858 p[4] = net_stats->rx_errors;
859 p[5] = net_stats->tx_errors;
860 p[6] = net_stats->rx_dropped;
861 p[7] = net_stats->tx_dropped;
862 p[8] = net_stats->multicast;
863 p[9] = net_stats->collisions;
864 p[10] = net_stats->rx_over_errors;
865 p[11] = net_stats->rx_crc_errors;
866 p[12] = net_stats->rx_frame_errors;
867 p[13] = net_stats->rx_fifo_errors;
868 p[14] = net_stats->rx_missed_errors;
869 p[15] = net_stats->tx_aborted_errors;
870 p[16] = net_stats->tx_carrier_errors;
871 p[17] = net_stats->tx_fifo_errors;
872 p[18] = net_stats->tx_heartbeat_errors;
873 p[19] = net_stats->rx_length_errors;
874 p[20] = net_stats->tx_window_errors;
875 p[21] = net_stats->rx_compressed;
876 p[22] = net_stats->tx_compressed;
877
878 p[23] = netdev->rx_dropped.counter;
879 p[24] = netdev->tx_dropped.counter;
880
881 p[25] = priv->tx_timeout_count;
882
883 /* get driver statistics */
884 h->dev->ops->get_stats(h, &p[26]);
885 }
886
887 /**
888 * get_strings: Return a set of strings that describe the requested objects
889 * @dev: net device
890 * @stats: string set ID.
891 * @data: objects data.
892 */
hns_get_strings(struct net_device * netdev,u32 stringset,u8 * data)893 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
894 {
895 struct hns_nic_priv *priv = netdev_priv(netdev);
896 struct hnae_handle *h = priv->ae_handle;
897 char *buff = (char *)data;
898
899 if (!h->dev->ops->get_strings) {
900 netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
901 return;
902 }
903
904 if (stringset == ETH_SS_TEST) {
905 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
906 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
907 ETH_GSTRING_LEN);
908 buff += ETH_GSTRING_LEN;
909 }
910 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
911 ETH_GSTRING_LEN);
912 buff += ETH_GSTRING_LEN;
913 if ((priv->phy) && (!priv->phy->is_c45))
914 memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
915 ETH_GSTRING_LEN);
916
917 } else {
918 snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
919 buff = buff + ETH_GSTRING_LEN;
920 snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
921 buff = buff + ETH_GSTRING_LEN;
922 snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
923 buff = buff + ETH_GSTRING_LEN;
924 snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
925 buff = buff + ETH_GSTRING_LEN;
926 snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
927 buff = buff + ETH_GSTRING_LEN;
928 snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
929 buff = buff + ETH_GSTRING_LEN;
930 snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
931 buff = buff + ETH_GSTRING_LEN;
932 snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
933 buff = buff + ETH_GSTRING_LEN;
934 snprintf(buff, ETH_GSTRING_LEN, "multicast");
935 buff = buff + ETH_GSTRING_LEN;
936 snprintf(buff, ETH_GSTRING_LEN, "collisions");
937 buff = buff + ETH_GSTRING_LEN;
938 snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
939 buff = buff + ETH_GSTRING_LEN;
940 snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
941 buff = buff + ETH_GSTRING_LEN;
942 snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
943 buff = buff + ETH_GSTRING_LEN;
944 snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
945 buff = buff + ETH_GSTRING_LEN;
946 snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
947 buff = buff + ETH_GSTRING_LEN;
948 snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
949 buff = buff + ETH_GSTRING_LEN;
950 snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
951 buff = buff + ETH_GSTRING_LEN;
952 snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
953 buff = buff + ETH_GSTRING_LEN;
954 snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
955 buff = buff + ETH_GSTRING_LEN;
956 snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
957 buff = buff + ETH_GSTRING_LEN;
958 snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
959 buff = buff + ETH_GSTRING_LEN;
960 snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
961 buff = buff + ETH_GSTRING_LEN;
962 snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
963 buff = buff + ETH_GSTRING_LEN;
964 snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
965 buff = buff + ETH_GSTRING_LEN;
966 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
967 buff = buff + ETH_GSTRING_LEN;
968
969 snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
970 buff = buff + ETH_GSTRING_LEN;
971
972 h->dev->ops->get_strings(h, stringset, (u8 *)buff);
973 }
974 }
975
976 /**
977 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
978 * @dev: net device
979 * @stringset: string set index, 0: self test string; 1: statistics string.
980 *
981 * Return string set count.
982 */
hns_get_sset_count(struct net_device * netdev,int stringset)983 int hns_get_sset_count(struct net_device *netdev, int stringset)
984 {
985 struct hns_nic_priv *priv = netdev_priv(netdev);
986 struct hnae_handle *h = priv->ae_handle;
987 struct hnae_ae_ops *ops = h->dev->ops;
988
989 if (!ops->get_sset_count) {
990 netdev_err(netdev, "get_sset_count is null!\n");
991 return -EOPNOTSUPP;
992 }
993 if (stringset == ETH_SS_TEST) {
994 u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
995
996 if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
997 cnt--;
998
999 if ((!priv->phy) || (priv->phy->is_c45))
1000 cnt--;
1001
1002 return cnt;
1003 } else {
1004 return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1005 }
1006 }
1007
1008 /**
1009 * hns_phy_led_set - set phy LED status.
1010 * @dev: net device
1011 * @value: LED state.
1012 *
1013 * Return 0 on success, negative on failure.
1014 */
hns_phy_led_set(struct net_device * netdev,int value)1015 int hns_phy_led_set(struct net_device *netdev, int value)
1016 {
1017 int retval;
1018 struct hns_nic_priv *priv = netdev_priv(netdev);
1019 struct phy_device *phy_dev = priv->phy;
1020
1021 if (!phy_dev->bus) {
1022 netdev_err(netdev, "phy_dev->bus is null!\n");
1023 return -EINVAL;
1024 }
1025 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1026 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1027 retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FC_REG,
1028 value);
1029 retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
1030 HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1031 if (retval) {
1032 netdev_err(netdev, "mdiobus_write fail !\n");
1033 return retval;
1034 }
1035 return 0;
1036 }
1037
1038 /**
1039 * nic_set_phys_id - set phy identify LED.
1040 * @dev: net device
1041 * @state: LED state.
1042 *
1043 * Return 0 on success, negative on failure.
1044 */
hns_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1045 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1046 {
1047 struct hns_nic_priv *priv = netdev_priv(netdev);
1048 struct hnae_handle *h = priv->ae_handle;
1049 struct phy_device *phy_dev = priv->phy;
1050 int ret;
1051
1052 if (phy_dev)
1053 switch (state) {
1054 case ETHTOOL_ID_ACTIVE:
1055 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1056 HNS_PHY_PAGE_REG,
1057 HNS_PHY_PAGE_LED);
1058 if (ret)
1059 return ret;
1060
1061 priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
1062 phy_dev->addr,
1063 HNS_LED_FC_REG);
1064
1065 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1066 HNS_PHY_PAGE_REG,
1067 HNS_PHY_PAGE_COPPER);
1068 if (ret)
1069 return ret;
1070 return 2;
1071 case ETHTOOL_ID_ON:
1072 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1073 if (ret)
1074 return ret;
1075 break;
1076 case ETHTOOL_ID_OFF:
1077 ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1078 if (ret)
1079 return ret;
1080 break;
1081 case ETHTOOL_ID_INACTIVE:
1082 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1083 HNS_PHY_PAGE_REG,
1084 HNS_PHY_PAGE_LED);
1085 if (ret)
1086 return ret;
1087
1088 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1089 HNS_LED_FC_REG, priv->phy_led_val);
1090 if (ret)
1091 return ret;
1092
1093 ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
1094 HNS_PHY_PAGE_REG,
1095 HNS_PHY_PAGE_COPPER);
1096 if (ret)
1097 return ret;
1098 break;
1099 default:
1100 return -EINVAL;
1101 }
1102 else
1103 switch (state) {
1104 case ETHTOOL_ID_ACTIVE:
1105 return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1106 case ETHTOOL_ID_ON:
1107 return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1108 case ETHTOOL_ID_OFF:
1109 return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1110 case ETHTOOL_ID_INACTIVE:
1111 return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1112 default:
1113 return -EINVAL;
1114 }
1115
1116 return 0;
1117 }
1118
1119 /**
1120 * hns_get_regs - get net device register
1121 * @dev: net device
1122 * @cmd: ethtool cmd
1123 * @date: register data
1124 */
hns_get_regs(struct net_device * net_dev,struct ethtool_regs * cmd,void * data)1125 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1126 void *data)
1127 {
1128 struct hns_nic_priv *priv = netdev_priv(net_dev);
1129 struct hnae_ae_ops *ops;
1130
1131 assert(priv || priv->ae_handle);
1132
1133 ops = priv->ae_handle->dev->ops;
1134
1135 cmd->version = HNS_CHIP_VERSION;
1136 if (!ops->get_regs) {
1137 netdev_err(net_dev, "ops->get_regs is null!\n");
1138 return;
1139 }
1140 ops->get_regs(priv->ae_handle, data);
1141 }
1142
1143 /**
1144 * nic_get_regs_len - get total register len.
1145 * @dev: net device
1146 *
1147 * Return total register len.
1148 */
hns_get_regs_len(struct net_device * net_dev)1149 static int hns_get_regs_len(struct net_device *net_dev)
1150 {
1151 u32 reg_num;
1152 struct hns_nic_priv *priv = netdev_priv(net_dev);
1153 struct hnae_ae_ops *ops;
1154
1155 assert(priv || priv->ae_handle);
1156
1157 ops = priv->ae_handle->dev->ops;
1158 if (!ops->get_regs_len) {
1159 netdev_err(net_dev, "ops->get_regs_len is null!\n");
1160 return -EOPNOTSUPP;
1161 }
1162
1163 reg_num = ops->get_regs_len(priv->ae_handle);
1164 if (reg_num > 0)
1165 return reg_num * sizeof(u32);
1166 else
1167 return reg_num; /* error code */
1168 }
1169
1170 /**
1171 * hns_nic_nway_reset - nway reset
1172 * @dev: net device
1173 *
1174 * Return 0 on success, negative on failure
1175 */
hns_nic_nway_reset(struct net_device * netdev)1176 static int hns_nic_nway_reset(struct net_device *netdev)
1177 {
1178 int ret = 0;
1179 struct hns_nic_priv *priv = netdev_priv(netdev);
1180 struct phy_device *phy = priv->phy;
1181
1182 if (netif_running(netdev)) {
1183 if (phy)
1184 ret = genphy_restart_aneg(phy);
1185 }
1186
1187 return ret;
1188 }
1189
1190 static struct ethtool_ops hns_ethtool_ops = {
1191 .get_drvinfo = hns_nic_get_drvinfo,
1192 .get_link = hns_nic_get_link,
1193 .get_settings = hns_nic_get_settings,
1194 .set_settings = hns_nic_set_settings,
1195 .get_ringparam = hns_get_ringparam,
1196 .get_pauseparam = hns_get_pauseparam,
1197 .set_pauseparam = hns_set_pauseparam,
1198 .get_coalesce = hns_get_coalesce,
1199 .set_coalesce = hns_set_coalesce,
1200 .get_channels = hns_get_channels,
1201 .self_test = hns_nic_self_test,
1202 .get_strings = hns_get_strings,
1203 .get_sset_count = hns_get_sset_count,
1204 .get_ethtool_stats = hns_get_ethtool_stats,
1205 .set_phys_id = hns_set_phys_id,
1206 .get_regs_len = hns_get_regs_len,
1207 .get_regs = hns_get_regs,
1208 .nway_reset = hns_nic_nway_reset,
1209 };
1210
hns_ethtool_set_ops(struct net_device * ndev)1211 void hns_ethtool_set_ops(struct net_device *ndev)
1212 {
1213 ndev->ethtool_ops = &hns_ethtool_ops;
1214 }
1215