net:hns: Add support of Hip06 SoC to the Hislicon Network Subsystem
[deliverable/linux.git] / drivers / net / ethernet / hisilicon / hns / hns_ethtool.c
CommitLineData
b5996f11 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>
b5996f11 14#include "hns_enet.h"
15
16#define HNS_PHY_PAGE_MDIX 0
17#define HNS_PHY_PAGE_LED 3
18#define HNS_PHY_PAGE_COPPER 0
19
20#define HNS_PHY_PAGE_REG 22 /* Page Selection Reg. */
21#define HNS_PHY_CSC_REG 16 /* Copper Specific Control Register */
22#define HNS_PHY_CSS_REG 17 /* Copper Specific Status Register */
23#define HNS_LED_FC_REG 16 /* LED Function Control Reg. */
24#define HNS_LED_PC_REG 17 /* LED Polarity Control Reg. */
25
26#define HNS_LED_FORCE_ON 9
27#define HNS_LED_FORCE_OFF 8
28
29#define HNS_CHIP_VERSION 660
30#define HNS_NET_STATS_CNT 26
31
32#define PHY_MDIX_CTRL_S (5)
33#define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
34
35#define PHY_MDIX_STATUS_B (6)
36#define PHY_SPEED_DUP_RESOLVE_B (11)
37
38/**
39 *hns_nic_get_link - get current link status
40 *@net_dev: net_device
41 *retuen 0 - success , negative --fail
42 */
43static u32 hns_nic_get_link(struct net_device *net_dev)
44{
45 struct hns_nic_priv *priv = netdev_priv(net_dev);
46 u32 link_stat = priv->link;
47 struct hnae_handle *h;
48
49 assert(priv && priv->ae_handle);
50 h = priv->ae_handle;
51
52 if (priv->phy) {
53 if (!genphy_update_link(priv->phy))
54 link_stat = priv->phy->link;
55 else
56 link_stat = 0;
57 }
58
59 if (h->dev && h->dev->ops && h->dev->ops->get_status)
60 link_stat = link_stat && h->dev->ops->get_status(h);
61 else
62 link_stat = 0;
63
64 return link_stat;
65}
66
67static void hns_get_mdix_mode(struct net_device *net_dev,
68 struct ethtool_cmd *cmd)
69{
70 int mdix_ctrl, mdix, retval, is_resolved;
71 struct hns_nic_priv *priv = netdev_priv(net_dev);
72 struct phy_device *phy_dev = priv->phy;
73
74 if (!phy_dev || !phy_dev->bus) {
75 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
76 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
77 return;
78 }
79
80 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
81 HNS_PHY_PAGE_MDIX);
82
83 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSC_REG);
84 mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
85
86 retval = mdiobus_read(phy_dev->bus, phy_dev->addr, HNS_PHY_CSS_REG);
87 mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
88 is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
89
90 (void)mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_PHY_PAGE_REG,
91 HNS_PHY_PAGE_COPPER);
92
93 switch (mdix_ctrl) {
94 case 0x0:
95 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI;
96 break;
97 case 0x1:
98 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_X;
99 break;
100 case 0x3:
101 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
102 break;
103 default:
104 cmd->eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
105 break;
106 }
107
108 if (!is_resolved)
109 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
110 else if (mdix)
111 cmd->eth_tp_mdix = ETH_TP_MDI_X;
112 else
113 cmd->eth_tp_mdix = ETH_TP_MDI;
114}
115
116/**
117 *hns_nic_get_settings - implement ethtool get settings
118 *@net_dev: net_device
119 *@cmd: ethtool_cmd
120 *retuen 0 - success , negative --fail
121 */
122static int hns_nic_get_settings(struct net_device *net_dev,
123 struct ethtool_cmd *cmd)
124{
125 struct hns_nic_priv *priv = netdev_priv(net_dev);
126 struct hnae_handle *h;
127 u32 link_stat;
128 int ret;
129 u8 duplex;
130 u16 speed;
131
132 if (!priv || !priv->ae_handle)
133 return -ESRCH;
134
135 h = priv->ae_handle;
136 if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
137 return -ESRCH;
138
139 ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
140 if (ret < 0) {
141 netdev_err(net_dev, "%s get_info error!\n", __func__);
142 return -EINVAL;
143 }
144
145 /* When there is no phy, autoneg is off. */
146 cmd->autoneg = false;
147 ethtool_cmd_speed_set(cmd, speed);
148 cmd->duplex = duplex;
149
150 if (priv->phy)
151 (void)phy_ethtool_gset(priv->phy, cmd);
152
153 link_stat = hns_nic_get_link(net_dev);
154 if (!link_stat) {
155 ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
156 cmd->duplex = DUPLEX_UNKNOWN;
157 }
158
159 if (cmd->autoneg)
160 cmd->advertising |= ADVERTISED_Autoneg;
161
162 cmd->supported |= h->if_support;
163 if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
164 cmd->supported |= SUPPORTED_TP;
165 cmd->advertising |= ADVERTISED_1000baseT_Full;
166 } else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
167 cmd->supported |= SUPPORTED_FIBRE;
168 cmd->advertising |= ADVERTISED_10000baseKR_Full;
169 }
170
171 if (h->port_type == HNAE_PORT_SERVICE) {
172 cmd->port = PORT_FIBRE;
173 cmd->supported |= SUPPORTED_Pause;
174 } else {
175 cmd->port = PORT_TP;
176 }
177
178 cmd->transceiver = XCVR_EXTERNAL;
179 cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
180 hns_get_mdix_mode(net_dev, cmd);
181
182 return 0;
183}
184
185/**
186 *hns_nic_set_settings - implement ethtool set settings
187 *@net_dev: net_device
188 *@cmd: ethtool_cmd
189 *retuen 0 - success , negative --fail
190 */
191static int hns_nic_set_settings(struct net_device *net_dev,
192 struct ethtool_cmd *cmd)
193{
194 struct hns_nic_priv *priv = netdev_priv(net_dev);
195 struct hnae_handle *h;
b5996f11 196 u32 speed;
b5996f11 197
198 if (!netif_running(net_dev))
199 return -ESRCH;
200
201 if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
202 !priv->ae_handle->dev->ops)
203 return -ENODEV;
204
205 h = priv->ae_handle;
b5996f11 206 speed = ethtool_cmd_speed(cmd);
b5996f11 207
208 if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
20ddb1d3
CX
209 if (cmd->autoneg == AUTONEG_ENABLE || speed != SPEED_10000 ||
210 cmd->duplex != DUPLEX_FULL)
b5996f11 211 return -EINVAL;
212 } else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
20ddb1d3 213 if (!priv->phy && cmd->autoneg == AUTONEG_ENABLE)
b5996f11 214 return -EINVAL;
215
20ddb1d3 216 if (speed == SPEED_1000 && cmd->duplex == DUPLEX_HALF)
b5996f11 217 return -EINVAL;
20ddb1d3
CX
218 if (priv->phy)
219 return phy_ethtool_sset(priv->phy, cmd);
b5996f11 220
20ddb1d3
CX
221 if ((speed != SPEED_10 && speed != SPEED_100 &&
222 speed != SPEED_1000) || (cmd->duplex != DUPLEX_HALF &&
223 cmd->duplex != DUPLEX_FULL))
b5996f11 224 return -EINVAL;
225 } else {
226 netdev_err(net_dev, "Not supported!");
227 return -ENOTSUPP;
228 }
229
20ddb1d3
CX
230 if (h->dev->ops->adjust_link) {
231 h->dev->ops->adjust_link(h, (int)speed, cmd->duplex);
b5996f11 232 return 0;
233 }
20ddb1d3 234
b5996f11 235 netdev_err(net_dev, "Not supported!");
236 return -ENOTSUPP;
237}
238
239static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
240 "Mac Loopback test",
241 "Serdes Loopback test",
242 "Phy Loopback test"
243};
244
245static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
246{
247#define COPPER_CONTROL_REG 0
248#define PHY_LOOP_BACK BIT(14)
249 u16 val = 0;
250
251 if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
252 return -ENOTSUPP;
253
254 if (en) {
255 /* speed : 1000M */
256 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
257 HNS_PHY_PAGE_REG, 2);
258 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
259 21, 0x1046);
260 /* Force Master */
261 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
262 9, 0x1F00);
263 /* Soft-reset */
264 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
265 0, 0x9140);
266 /* If autoneg disabled,two soft-reset operations */
267 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
268 0, 0x9140);
269 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
270 22, 0xFA);
271
272 /* Default is 0x0400 */
273 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
274 1, 0x418);
275
276 /* Force 1000M Link, Default is 0x0200 */
277 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
278 7, 0x20C);
279 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
280 22, 0);
281
282 /* Enable MAC loop-back */
283 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
284 COPPER_CONTROL_REG);
285 val |= PHY_LOOP_BACK;
286 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
287 COPPER_CONTROL_REG, val);
288 } else {
289 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
290 22, 0xFA);
291 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
292 1, 0x400);
293 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
294 7, 0x200);
295 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
296 22, 0);
297
298 val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
299 COPPER_CONTROL_REG);
300 val &= ~PHY_LOOP_BACK;
301 (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
302 COPPER_CONTROL_REG, val);
303 }
304 return 0;
305}
306
307static int __lb_setup(struct net_device *ndev,
308 enum hnae_loop loop)
309{
310 int ret = 0;
311 struct hns_nic_priv *priv = netdev_priv(ndev);
312 struct phy_device *phy_dev = priv->phy;
313 struct hnae_handle *h = priv->ae_handle;
314
315 switch (loop) {
316 case MAC_INTERNALLOOP_PHY:
317 if ((phy_dev) && (!phy_dev->is_c45))
318 ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
319 break;
320 case MAC_INTERNALLOOP_MAC:
321 if ((h->dev->ops->set_loopback) &&
322 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
323 ret = h->dev->ops->set_loopback(h, loop, 0x1);
324 break;
325 case MAC_INTERNALLOOP_SERDES:
326 if (h->dev->ops->set_loopback)
327 ret = h->dev->ops->set_loopback(h, loop, 0x1);
328 break;
329 case MAC_LOOP_NONE:
330 if ((phy_dev) && (!phy_dev->is_c45))
331 ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
332
333 if (h->dev->ops->set_loopback) {
334 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
335 ret |= h->dev->ops->set_loopback(h,
336 MAC_INTERNALLOOP_MAC, 0x0);
337
338 ret |= h->dev->ops->set_loopback(h,
339 MAC_INTERNALLOOP_SERDES, 0x0);
340 }
341 break;
342 default:
343 ret = -EINVAL;
344 break;
345 }
346
347 return ret;
348}
349
350static int __lb_up(struct net_device *ndev,
351 enum hnae_loop loop_mode)
352{
353 struct hns_nic_priv *priv = netdev_priv(ndev);
354 struct hnae_handle *h = priv->ae_handle;
355 int speed, duplex;
356 int ret;
357
358 hns_nic_net_reset(ndev);
359
360 if (priv->phy) {
361 phy_disconnect(priv->phy);
362 msleep(100);
363
364 ret = hns_nic_init_phy(ndev, h);
365 if (ret)
366 return ret;
367 }
368
369 ret = __lb_setup(ndev, loop_mode);
370 if (ret)
371 return ret;
372
373 msleep(100);
374
375 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
376 if (ret)
377 return ret;
378
379 if (priv->phy)
380 phy_start(priv->phy);
381
382 /* link adjust duplex*/
383 if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
384 speed = 1000;
385 else
386 speed = 10000;
387 duplex = 1;
388
389 h->dev->ops->adjust_link(h, speed, duplex);
390
391 return 0;
392}
393
394static void __lb_other_process(struct hns_nic_ring_data *ring_data,
395 struct sk_buff *skb)
396{
397 struct net_device *ndev;
398 struct hnae_ring *ring;
399 struct netdev_queue *dev_queue;
400 struct sk_buff *new_skb;
401 unsigned int frame_size;
402 int check_ok;
403 u32 i;
404 char buff[33]; /* 32B data and the last character '\0' */
405
406 if (!ring_data) { /* Just for doing create frame*/
407 frame_size = skb->len;
408 memset(skb->data, 0xFF, frame_size);
409 frame_size &= ~1ul;
410 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
411 memset(&skb->data[frame_size / 2 + 10], 0xBE,
412 frame_size / 2 - 11);
413 memset(&skb->data[frame_size / 2 + 12], 0xAF,
414 frame_size / 2 - 13);
415 return;
416 }
417
418 ring = ring_data->ring;
419 ndev = ring_data->napi.dev;
420 if (is_tx_ring(ring)) { /* for tx queue reset*/
421 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
422 netdev_tx_reset_queue(dev_queue);
423 return;
424 }
425
426 frame_size = skb->len;
427 frame_size &= ~1ul;
428 /* for mutl buffer*/
429 new_skb = skb_copy(skb, GFP_ATOMIC);
430 dev_kfree_skb_any(skb);
431 skb = new_skb;
432
433 check_ok = 0;
434 if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
435 if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
436 (*(skb->data + frame_size / 2 + 12) == 0xAF))
437 check_ok = 1;
438 }
439
440 if (check_ok) {
441 ndev->stats.rx_packets++;
442 ndev->stats.rx_bytes += skb->len;
443 } else {
444 ndev->stats.rx_frame_errors++;
445 for (i = 0; i < skb->len; i++) {
446 snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
447 "%02x", *(skb->data + i));
448 if ((i % 16 == 15) || (i == skb->len - 1))
449 pr_info("%s\n", buff);
450 }
451 }
452 dev_kfree_skb_any(skb);
453}
454
455static int __lb_clean_rings(struct hns_nic_priv *priv,
456 int ringid0, int ringid1, int budget)
457{
458 int i, ret;
459 struct hns_nic_ring_data *ring_data;
460 struct net_device *ndev = priv->netdev;
461 unsigned long rx_packets = ndev->stats.rx_packets;
462 unsigned long rx_bytes = ndev->stats.rx_bytes;
463 unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
464
465 for (i = ringid0; i <= ringid1; i++) {
466 ring_data = &priv->ring_data[i];
467 (void)ring_data->poll_one(ring_data,
468 budget, __lb_other_process);
469 }
470 ret = (int)(ndev->stats.rx_packets - rx_packets);
471 ndev->stats.rx_packets = rx_packets;
472 ndev->stats.rx_bytes = rx_bytes;
473 ndev->stats.rx_frame_errors = rx_frame_errors;
474 return ret;
475}
476
477/**
478 * nic_run_loopback_test - run loopback test
479 * @nic_dev: net device
480 * @loopback_type: loopback type
481 */
482static int __lb_run_test(struct net_device *ndev,
483 enum hnae_loop loop_mode)
484{
485#define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
486#define NIC_LB_TEST_RING_ID 0
487#define NIC_LB_TEST_FRAME_SIZE 128
488/* nic loopback test err */
489#define NIC_LB_TEST_NO_MEM_ERR 1
490#define NIC_LB_TEST_TX_CNT_ERR 2
491#define NIC_LB_TEST_RX_CNT_ERR 3
492#define NIC_LB_TEST_RX_PKG_ERR 4
493 struct hns_nic_priv *priv = netdev_priv(ndev);
494 struct hnae_handle *h = priv->ae_handle;
495 int i, j, lc, good_cnt, ret_val = 0;
496 unsigned int size;
497 netdev_tx_t tx_ret_val;
498 struct sk_buff *skb;
499
500 size = NIC_LB_TEST_FRAME_SIZE;
501 /* allocate test skb */
502 skb = alloc_skb(size, GFP_KERNEL);
503 if (!skb)
504 return NIC_LB_TEST_NO_MEM_ERR;
505
506 /* place data into test skb */
507 (void)skb_put(skb, size);
508 __lb_other_process(NULL, skb);
509 skb->queue_mapping = NIC_LB_TEST_RING_ID;
510
511 lc = 1;
512 for (j = 0; j < lc; j++) {
513 /* reset count of good packets */
514 good_cnt = 0;
515 /* place 64 packets on the transmit queue*/
516 for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
517 (void)skb_get(skb);
518
519 tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
520 ndev, skb,
521 &tx_ring_data(priv, skb->queue_mapping));
522 if (tx_ret_val == NETDEV_TX_OK)
523 good_cnt++;
524 else
525 break;
526 }
527 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
528 ret_val = NIC_LB_TEST_TX_CNT_ERR;
529 dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
530 hns_nic_test_strs[loop_mode], good_cnt,
531 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
532 break;
533 }
534
535 /* allow 100 milliseconds for packets to go from Tx to Rx */
536 msleep(100);
537
538 good_cnt = __lb_clean_rings(priv,
539 h->q_num, h->q_num * 2 - 1,
540 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
541 if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
542 ret_val = NIC_LB_TEST_RX_CNT_ERR;
543 dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
544 hns_nic_test_strs[loop_mode], good_cnt,
545 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
546 break;
547 }
548 (void)__lb_clean_rings(priv,
549 NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
550 NIC_LB_TEST_PKT_NUM_PER_CYCLE);
551 }
552
553 /* free the original skb */
554 kfree_skb(skb);
555
556 return ret_val;
557}
558
559static int __lb_down(struct net_device *ndev)
560{
561 struct hns_nic_priv *priv = netdev_priv(ndev);
562 struct hnae_handle *h = priv->ae_handle;
563 int ret;
564
565 ret = __lb_setup(ndev, MAC_LOOP_NONE);
566 if (ret)
567 netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
568 __func__,
569 ret);
570
571 if (priv->phy)
572 phy_stop(priv->phy);
573
574 if (h->dev->ops->stop)
575 h->dev->ops->stop(h);
576
577 usleep_range(10000, 20000);
578 (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
579
580 hns_nic_net_reset(ndev);
581
582 return 0;
583}
584
585/**
586 * hns_nic_self_test - self test
587 * @dev: net device
588 * @eth_test: test cmd
589 * @data: test result
590 */
591static void hns_nic_self_test(struct net_device *ndev,
592 struct ethtool_test *eth_test, u64 *data)
593{
594 struct hns_nic_priv *priv = netdev_priv(ndev);
595 bool if_running = netif_running(ndev);
596#define SELF_TEST_TPYE_NUM 3
597 int st_param[SELF_TEST_TPYE_NUM][2];
598 int i;
599 int test_index = 0;
600
601 st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
602 st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
603 st_param[1][0] = MAC_INTERNALLOOP_SERDES;
604 st_param[1][1] = 1; /*serdes must exist*/
605 st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
606 st_param[2][1] = ((!!(priv->ae_handle->phy_node)) &&
607 (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
608
609 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
610 set_bit(NIC_STATE_TESTING, &priv->state);
611
612 if (if_running)
613 (void)dev_close(ndev);
614
615 for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
616 if (!st_param[i][1])
617 continue; /* NEXT testing */
618
619 data[test_index] = __lb_up(ndev,
620 (enum hnae_loop)st_param[i][0]);
621 if (!data[test_index]) {
622 data[test_index] = __lb_run_test(
623 ndev, (enum hnae_loop)st_param[i][0]);
624 (void)__lb_down(ndev);
625 }
626
627 if (data[test_index])
628 eth_test->flags |= ETH_TEST_FL_FAILED;
629
630 test_index++;
631 }
632
633 hns_nic_net_reset(priv->netdev);
634
635 clear_bit(NIC_STATE_TESTING, &priv->state);
636
637 if (if_running)
638 (void)dev_open(ndev);
639 }
640 /* Online tests aren't run; pass by default */
641
642 (void)msleep_interruptible(4 * 1000);
643}
644
645/**
646 * hns_nic_get_drvinfo - get net driver info
647 * @dev: net device
648 * @drvinfo: driver info
649 */
650static void hns_nic_get_drvinfo(struct net_device *net_dev,
651 struct ethtool_drvinfo *drvinfo)
652{
653 struct hns_nic_priv *priv = netdev_priv(net_dev);
654
655 assert(priv);
656
657 strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
658 sizeof(drvinfo->version));
659 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
660
661 strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
662 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
663
664 strncpy(drvinfo->bus_info, priv->dev->bus->name,
665 sizeof(drvinfo->bus_info));
666 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
667
668 strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
13ac695e 669 drvinfo->eedump_len = 0;
b5996f11 670}
671
672/**
673 * hns_get_ringparam - get ring parameter
674 * @dev: net device
675 * @param: ethtool parameter
676 */
677void 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 */
702static 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, &param->autoneg,
712 &param->rx_pause, &param->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 */
722static 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 */
748static 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 */
782static 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 */
818void 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 */
835void 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 */
893void 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 */
983int 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 */
1015int 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 */
1045int 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 */
1125void 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 */
1149static 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 */
1176static 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
1190static 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
1211void hns_ethtool_set_ops(struct net_device *ndev)
1212{
1213 ndev->ethtool_ops = &hns_ethtool_ops;
1214}
This page took 0.08667 seconds and 5 git commands to generate.