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