hns_ethtool.c 33 KB

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