hns_ethtool.c 33 KB

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