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