hns3_ethtool.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (c) 2016-2017 Hisilicon Limited.
  3. #include <linux/etherdevice.h>
  4. #include <linux/string.h>
  5. #include <linux/phy.h>
  6. #include "hns3_enet.h"
  7. struct hns3_stats {
  8. char stats_string[ETH_GSTRING_LEN];
  9. int stats_offset;
  10. };
  11. /* tqp related stats */
  12. #define HNS3_TQP_STAT(_string, _member) { \
  13. .stats_string = _string, \
  14. .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
  15. offsetof(struct ring_stats, _member), \
  16. }
  17. static const struct hns3_stats hns3_txq_stats[] = {
  18. /* Tx per-queue statistics */
  19. HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
  20. HNS3_TQP_STAT("dropped", sw_err_cnt),
  21. HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  22. HNS3_TQP_STAT("packets", tx_pkts),
  23. HNS3_TQP_STAT("bytes", tx_bytes),
  24. HNS3_TQP_STAT("errors", tx_err_cnt),
  25. HNS3_TQP_STAT("wake", restart_queue),
  26. HNS3_TQP_STAT("busy", tx_busy),
  27. HNS3_TQP_STAT("copy", tx_copy),
  28. HNS3_TQP_STAT("vlan_err", tx_vlan_err),
  29. HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
  30. HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
  31. HNS3_TQP_STAT("tso_err", tx_tso_err),
  32. };
  33. #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  34. static const struct hns3_stats hns3_rxq_stats[] = {
  35. /* Rx per-queue statistics */
  36. HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
  37. HNS3_TQP_STAT("dropped", sw_err_cnt),
  38. HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  39. HNS3_TQP_STAT("packets", rx_pkts),
  40. HNS3_TQP_STAT("bytes", rx_bytes),
  41. HNS3_TQP_STAT("errors", rx_err_cnt),
  42. HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
  43. HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
  44. HNS3_TQP_STAT("err_bd_num", err_bd_num),
  45. HNS3_TQP_STAT("l2_err", l2_err),
  46. HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
  47. HNS3_TQP_STAT("multicast", rx_multicast),
  48. HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
  49. };
  50. #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  51. #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  52. #define HNS3_SELF_TEST_TYPE_NUM 4
  53. #define HNS3_NIC_LB_TEST_PKT_NUM 1
  54. #define HNS3_NIC_LB_TEST_RING_ID 0
  55. #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
  56. #define HNS3_NIC_LB_SETUP_USEC 10000
  57. /* Nic loopback test err */
  58. #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
  59. #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
  60. #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
  61. static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
  62. {
  63. struct hnae3_handle *h = hns3_get_handle(ndev);
  64. bool vlan_filter_enable;
  65. int ret;
  66. if (!h->ae_algo->ops->set_loopback ||
  67. !h->ae_algo->ops->set_promisc_mode)
  68. return -EOPNOTSUPP;
  69. switch (loop) {
  70. case HNAE3_LOOP_SERIAL_SERDES:
  71. case HNAE3_LOOP_PARALLEL_SERDES:
  72. case HNAE3_LOOP_APP:
  73. case HNAE3_LOOP_PHY:
  74. ret = h->ae_algo->ops->set_loopback(h, loop, en);
  75. break;
  76. default:
  77. ret = -ENOTSUPP;
  78. break;
  79. }
  80. if (ret || h->pdev->revision >= 0x21)
  81. return ret;
  82. if (en) {
  83. h->ae_algo->ops->set_promisc_mode(h, true, true);
  84. } else {
  85. /* recover promisc mode before loopback test */
  86. hns3_update_promisc_mode(ndev, h->netdev_flags);
  87. vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
  88. hns3_enable_vlan_filter(ndev, vlan_filter_enable);
  89. }
  90. return ret;
  91. }
  92. static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
  93. {
  94. struct hnae3_handle *h = hns3_get_handle(ndev);
  95. int ret;
  96. ret = hns3_nic_reset_all_ring(h);
  97. if (ret)
  98. return ret;
  99. ret = hns3_lp_setup(ndev, loop_mode, true);
  100. usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
  101. return ret;
  102. }
  103. static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
  104. {
  105. int ret;
  106. ret = hns3_lp_setup(ndev, loop_mode, false);
  107. if (ret) {
  108. netdev_err(ndev, "lb_setup return error: %d\n", ret);
  109. return ret;
  110. }
  111. usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
  112. return 0;
  113. }
  114. static void hns3_lp_setup_skb(struct sk_buff *skb)
  115. {
  116. #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
  117. struct net_device *ndev = skb->dev;
  118. struct hnae3_handle *handle;
  119. unsigned char *packet;
  120. struct ethhdr *ethh;
  121. unsigned int i;
  122. skb_reserve(skb, NET_IP_ALIGN);
  123. ethh = skb_put(skb, sizeof(struct ethhdr));
  124. packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
  125. memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
  126. /* The dst mac addr of loopback packet is the same as the host'
  127. * mac addr, the SSU component may loop back the packet to host
  128. * before the packet reaches mac or serdes, which will defect
  129. * the purpose of mac or serdes selftest.
  130. */
  131. handle = hns3_get_handle(ndev);
  132. if (handle->pdev->revision == 0x20)
  133. ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
  134. eth_zero_addr(ethh->h_source);
  135. ethh->h_proto = htons(ETH_P_ARP);
  136. skb_reset_mac_header(skb);
  137. for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
  138. packet[i] = (unsigned char)(i & 0xff);
  139. }
  140. static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
  141. struct sk_buff *skb)
  142. {
  143. struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
  144. unsigned char *packet = skb->data;
  145. u32 len = skb_headlen(skb);
  146. u32 i;
  147. len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
  148. for (i = 0; i < len; i++)
  149. if (packet[i] != (unsigned char)(i & 0xff))
  150. break;
  151. /* The packet is correctly received */
  152. if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
  153. tqp_vector->rx_group.total_packets++;
  154. else
  155. print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
  156. skb->data, len, true);
  157. dev_kfree_skb_any(skb);
  158. }
  159. static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
  160. {
  161. struct hnae3_handle *h = priv->ae_handle;
  162. struct hnae3_knic_private_info *kinfo;
  163. u32 i, rcv_good_pkt_total = 0;
  164. kinfo = &h->kinfo;
  165. for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
  166. struct hns3_enet_ring *ring = priv->ring_data[i].ring;
  167. struct hns3_enet_ring_group *rx_group;
  168. u64 pre_rx_pkt;
  169. rx_group = &ring->tqp_vector->rx_group;
  170. pre_rx_pkt = rx_group->total_packets;
  171. preempt_disable();
  172. hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
  173. preempt_enable();
  174. rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
  175. rx_group->total_packets = pre_rx_pkt;
  176. }
  177. return rcv_good_pkt_total;
  178. }
  179. static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
  180. u32 end_ringid, u32 budget)
  181. {
  182. u32 i;
  183. for (i = start_ringid; i <= end_ringid; i++) {
  184. struct hns3_enet_ring *ring = priv->ring_data[i].ring;
  185. hns3_clean_tx_ring(ring);
  186. }
  187. }
  188. /**
  189. * hns3_lp_run_test - run loopback test
  190. * @ndev: net device
  191. * @mode: loopback type
  192. */
  193. static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
  194. {
  195. struct hns3_nic_priv *priv = netdev_priv(ndev);
  196. struct sk_buff *skb;
  197. u32 i, good_cnt;
  198. int ret_val = 0;
  199. skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
  200. GFP_KERNEL);
  201. if (!skb)
  202. return HNS3_NIC_LB_TEST_NO_MEM_ERR;
  203. skb->dev = ndev;
  204. hns3_lp_setup_skb(skb);
  205. skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
  206. good_cnt = 0;
  207. for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
  208. netdev_tx_t tx_ret;
  209. skb_get(skb);
  210. tx_ret = hns3_nic_net_xmit(skb, ndev);
  211. if (tx_ret == NETDEV_TX_OK) {
  212. good_cnt++;
  213. } else {
  214. kfree_skb(skb);
  215. netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
  216. tx_ret);
  217. }
  218. }
  219. if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
  220. ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
  221. netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
  222. mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
  223. goto out;
  224. }
  225. /* Allow 200 milliseconds for packets to go from Tx to Rx */
  226. msleep(200);
  227. good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
  228. if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
  229. ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
  230. netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
  231. mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
  232. }
  233. out:
  234. hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
  235. HNS3_NIC_LB_TEST_RING_ID,
  236. HNS3_NIC_LB_TEST_PKT_NUM);
  237. kfree_skb(skb);
  238. return ret_val;
  239. }
  240. /**
  241. * hns3_nic_self_test - self test
  242. * @ndev: net device
  243. * @eth_test: test cmd
  244. * @data: test result
  245. */
  246. static void hns3_self_test(struct net_device *ndev,
  247. struct ethtool_test *eth_test, u64 *data)
  248. {
  249. struct hns3_nic_priv *priv = netdev_priv(ndev);
  250. struct hnae3_handle *h = priv->ae_handle;
  251. int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
  252. bool if_running = netif_running(ndev);
  253. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  254. bool dis_vlan_filter;
  255. #endif
  256. int test_index = 0;
  257. u32 i;
  258. if (hns3_nic_resetting(ndev)) {
  259. netdev_err(ndev, "dev resetting!");
  260. return;
  261. }
  262. /* Only do offline selftest, or pass by default */
  263. if (eth_test->flags != ETH_TEST_FL_OFFLINE)
  264. return;
  265. netif_dbg(h, drv, ndev, "self test start");
  266. st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
  267. st_param[HNAE3_LOOP_APP][1] =
  268. h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
  269. st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
  270. st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
  271. h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
  272. st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
  273. HNAE3_LOOP_PARALLEL_SERDES;
  274. st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
  275. h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
  276. st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
  277. st_param[HNAE3_LOOP_PHY][1] =
  278. h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
  279. if (if_running)
  280. ndev->netdev_ops->ndo_stop(ndev);
  281. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  282. /* Disable the vlan filter for selftest does not support it */
  283. dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  284. h->ae_algo->ops->enable_vlan_filter;
  285. if (dis_vlan_filter)
  286. h->ae_algo->ops->enable_vlan_filter(h, false);
  287. #endif
  288. /* Tell firmware to stop mac autoneg before loopback test start,
  289. * otherwise loopback test may be failed when the port is still
  290. * negotiating.
  291. */
  292. if (h->ae_algo->ops->halt_autoneg)
  293. h->ae_algo->ops->halt_autoneg(h, true);
  294. set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
  295. for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
  296. enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
  297. if (!st_param[i][1])
  298. continue;
  299. data[test_index] = hns3_lp_up(ndev, loop_type);
  300. if (!data[test_index])
  301. data[test_index] = hns3_lp_run_test(ndev, loop_type);
  302. hns3_lp_down(ndev, loop_type);
  303. if (data[test_index])
  304. eth_test->flags |= ETH_TEST_FL_FAILED;
  305. test_index++;
  306. }
  307. clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
  308. if (h->ae_algo->ops->halt_autoneg)
  309. h->ae_algo->ops->halt_autoneg(h, false);
  310. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  311. if (dis_vlan_filter)
  312. h->ae_algo->ops->enable_vlan_filter(h, true);
  313. #endif
  314. if (if_running)
  315. ndev->netdev_ops->ndo_open(ndev);
  316. netif_dbg(h, drv, ndev, "self test end\n");
  317. }
  318. static int hns3_get_sset_count(struct net_device *netdev, int stringset)
  319. {
  320. struct hnae3_handle *h = hns3_get_handle(netdev);
  321. const struct hnae3_ae_ops *ops = h->ae_algo->ops;
  322. if (!ops->get_sset_count)
  323. return -EOPNOTSUPP;
  324. switch (stringset) {
  325. case ETH_SS_STATS:
  326. return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
  327. ops->get_sset_count(h, stringset));
  328. case ETH_SS_TEST:
  329. return ops->get_sset_count(h, stringset);
  330. default:
  331. return -EOPNOTSUPP;
  332. }
  333. }
  334. static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
  335. u32 stat_count, u32 num_tqps, const char *prefix)
  336. {
  337. #define MAX_PREFIX_SIZE (6 + 4)
  338. u32 size_left;
  339. u32 i, j;
  340. u32 n1;
  341. for (i = 0; i < num_tqps; i++) {
  342. for (j = 0; j < stat_count; j++) {
  343. data[ETH_GSTRING_LEN - 1] = '\0';
  344. /* first, prepend the prefix string */
  345. n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
  346. prefix, i);
  347. n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
  348. size_left = (ETH_GSTRING_LEN - 1) - n1;
  349. /* now, concatenate the stats string to it */
  350. strncat(data, stats[j].stats_string, size_left);
  351. data += ETH_GSTRING_LEN;
  352. }
  353. }
  354. return data;
  355. }
  356. static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
  357. {
  358. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  359. const char tx_prefix[] = "txq";
  360. const char rx_prefix[] = "rxq";
  361. /* get strings for Tx */
  362. data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
  363. kinfo->num_tqps, tx_prefix);
  364. /* get strings for Rx */
  365. data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
  366. kinfo->num_tqps, rx_prefix);
  367. return data;
  368. }
  369. static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  370. {
  371. struct hnae3_handle *h = hns3_get_handle(netdev);
  372. const struct hnae3_ae_ops *ops = h->ae_algo->ops;
  373. char *buff = (char *)data;
  374. if (!ops->get_strings)
  375. return;
  376. switch (stringset) {
  377. case ETH_SS_STATS:
  378. buff = hns3_get_strings_tqps(h, buff);
  379. ops->get_strings(h, stringset, (u8 *)buff);
  380. break;
  381. case ETH_SS_TEST:
  382. ops->get_strings(h, stringset, data);
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
  389. {
  390. struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
  391. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  392. struct hns3_enet_ring *ring;
  393. u8 *stat;
  394. int i, j;
  395. /* get stats for Tx */
  396. for (i = 0; i < kinfo->num_tqps; i++) {
  397. ring = nic_priv->ring_data[i].ring;
  398. for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
  399. stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
  400. *data++ = *(u64 *)stat;
  401. }
  402. }
  403. /* get stats for Rx */
  404. for (i = 0; i < kinfo->num_tqps; i++) {
  405. ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
  406. for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
  407. stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
  408. *data++ = *(u64 *)stat;
  409. }
  410. }
  411. return data;
  412. }
  413. /* hns3_get_stats - get detail statistics.
  414. * @netdev: net device
  415. * @stats: statistics info.
  416. * @data: statistics data.
  417. */
  418. static void hns3_get_stats(struct net_device *netdev,
  419. struct ethtool_stats *stats, u64 *data)
  420. {
  421. struct hnae3_handle *h = hns3_get_handle(netdev);
  422. u64 *p = data;
  423. if (hns3_nic_resetting(netdev)) {
  424. netdev_err(netdev, "dev resetting, could not get stats\n");
  425. return;
  426. }
  427. if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
  428. netdev_err(netdev, "could not get any statistics\n");
  429. return;
  430. }
  431. h->ae_algo->ops->update_stats(h, &netdev->stats);
  432. /* get per-queue stats */
  433. p = hns3_get_stats_tqps(h, p);
  434. /* get MAC & other misc hardware stats */
  435. h->ae_algo->ops->get_stats(h, p);
  436. }
  437. static void hns3_get_drvinfo(struct net_device *netdev,
  438. struct ethtool_drvinfo *drvinfo)
  439. {
  440. struct hns3_nic_priv *priv = netdev_priv(netdev);
  441. struct hnae3_handle *h = priv->ae_handle;
  442. u32 fw_version;
  443. if (!h->ae_algo->ops->get_fw_version) {
  444. netdev_err(netdev, "could not get fw version!\n");
  445. return;
  446. }
  447. strncpy(drvinfo->version, hns3_driver_version,
  448. sizeof(drvinfo->version));
  449. drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
  450. strncpy(drvinfo->driver, h->pdev->driver->name,
  451. sizeof(drvinfo->driver));
  452. drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
  453. strncpy(drvinfo->bus_info, pci_name(h->pdev),
  454. sizeof(drvinfo->bus_info));
  455. drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
  456. fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
  457. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  458. "%lu.%lu.%lu.%lu",
  459. hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
  460. HNAE3_FW_VERSION_BYTE3_SHIFT),
  461. hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
  462. HNAE3_FW_VERSION_BYTE2_SHIFT),
  463. hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
  464. HNAE3_FW_VERSION_BYTE1_SHIFT),
  465. hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
  466. HNAE3_FW_VERSION_BYTE0_SHIFT));
  467. }
  468. static u32 hns3_get_link(struct net_device *netdev)
  469. {
  470. struct hnae3_handle *h = hns3_get_handle(netdev);
  471. if (h->ae_algo->ops->get_status)
  472. return h->ae_algo->ops->get_status(h);
  473. else
  474. return 0;
  475. }
  476. static void hns3_get_ringparam(struct net_device *netdev,
  477. struct ethtool_ringparam *param)
  478. {
  479. struct hns3_nic_priv *priv = netdev_priv(netdev);
  480. struct hnae3_handle *h = priv->ae_handle;
  481. int queue_num = h->kinfo.num_tqps;
  482. if (hns3_nic_resetting(netdev)) {
  483. netdev_err(netdev, "dev resetting!");
  484. return;
  485. }
  486. param->tx_max_pending = HNS3_RING_MAX_PENDING;
  487. param->rx_max_pending = HNS3_RING_MAX_PENDING;
  488. param->tx_pending = priv->ring_data[0].ring->desc_num;
  489. param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
  490. }
  491. static void hns3_get_pauseparam(struct net_device *netdev,
  492. struct ethtool_pauseparam *param)
  493. {
  494. struct hnae3_handle *h = hns3_get_handle(netdev);
  495. if (h->ae_algo->ops->get_pauseparam)
  496. h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
  497. &param->rx_pause, &param->tx_pause);
  498. }
  499. static int hns3_set_pauseparam(struct net_device *netdev,
  500. struct ethtool_pauseparam *param)
  501. {
  502. struct hnae3_handle *h = hns3_get_handle(netdev);
  503. netif_dbg(h, drv, netdev,
  504. "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
  505. param->autoneg, param->rx_pause, param->tx_pause);
  506. if (h->ae_algo->ops->set_pauseparam)
  507. return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
  508. param->rx_pause,
  509. param->tx_pause);
  510. return -EOPNOTSUPP;
  511. }
  512. static void hns3_get_ksettings(struct hnae3_handle *h,
  513. struct ethtool_link_ksettings *cmd)
  514. {
  515. const struct hnae3_ae_ops *ops = h->ae_algo->ops;
  516. /* 1.auto_neg & speed & duplex from cmd */
  517. if (ops->get_ksettings_an_result)
  518. ops->get_ksettings_an_result(h,
  519. &cmd->base.autoneg,
  520. &cmd->base.speed,
  521. &cmd->base.duplex);
  522. /* 2.get link mode */
  523. if (ops->get_link_mode)
  524. ops->get_link_mode(h,
  525. cmd->link_modes.supported,
  526. cmd->link_modes.advertising);
  527. /* 3.mdix_ctrl&mdix get from phy reg */
  528. if (ops->get_mdix_mode)
  529. ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
  530. &cmd->base.eth_tp_mdix);
  531. }
  532. static int hns3_get_link_ksettings(struct net_device *netdev,
  533. struct ethtool_link_ksettings *cmd)
  534. {
  535. struct hnae3_handle *h = hns3_get_handle(netdev);
  536. const struct hnae3_ae_ops *ops;
  537. u8 module_type;
  538. u8 media_type;
  539. u8 link_stat;
  540. ops = h->ae_algo->ops;
  541. if (ops->get_media_type)
  542. ops->get_media_type(h, &media_type, &module_type);
  543. else
  544. return -EOPNOTSUPP;
  545. switch (media_type) {
  546. case HNAE3_MEDIA_TYPE_NONE:
  547. cmd->base.port = PORT_NONE;
  548. hns3_get_ksettings(h, cmd);
  549. break;
  550. case HNAE3_MEDIA_TYPE_FIBER:
  551. if (module_type == HNAE3_MODULE_TYPE_CR)
  552. cmd->base.port = PORT_DA;
  553. else
  554. cmd->base.port = PORT_FIBRE;
  555. hns3_get_ksettings(h, cmd);
  556. break;
  557. case HNAE3_MEDIA_TYPE_BACKPLANE:
  558. cmd->base.port = PORT_NONE;
  559. hns3_get_ksettings(h, cmd);
  560. break;
  561. case HNAE3_MEDIA_TYPE_COPPER:
  562. cmd->base.port = PORT_TP;
  563. if (!netdev->phydev)
  564. hns3_get_ksettings(h, cmd);
  565. else
  566. phy_ethtool_ksettings_get(netdev->phydev, cmd);
  567. break;
  568. default:
  569. netdev_warn(netdev, "Unknown media type");
  570. return 0;
  571. }
  572. /* mdio_support */
  573. cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
  574. link_stat = hns3_get_link(netdev);
  575. if (!link_stat) {
  576. cmd->base.speed = SPEED_UNKNOWN;
  577. cmd->base.duplex = DUPLEX_UNKNOWN;
  578. }
  579. return 0;
  580. }
  581. static int hns3_check_ksettings_param(const struct net_device *netdev,
  582. const struct ethtool_link_ksettings *cmd)
  583. {
  584. struct hnae3_handle *handle = hns3_get_handle(netdev);
  585. const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
  586. u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
  587. u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
  588. u8 autoneg;
  589. u32 speed;
  590. u8 duplex;
  591. int ret;
  592. /* hw doesn't support use specified speed and duplex to negotiate,
  593. * unnecessary to check them when autoneg on.
  594. */
  595. if (cmd->base.autoneg)
  596. return 0;
  597. if (ops->get_ksettings_an_result) {
  598. ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
  599. if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
  600. cmd->base.duplex == duplex)
  601. return 0;
  602. }
  603. if (ops->get_media_type)
  604. ops->get_media_type(handle, &media_type, &module_type);
  605. if (cmd->base.duplex != DUPLEX_FULL &&
  606. media_type != HNAE3_MEDIA_TYPE_COPPER) {
  607. netdev_err(netdev,
  608. "only copper port supports half duplex!");
  609. return -EINVAL;
  610. }
  611. if (ops->check_port_speed) {
  612. ret = ops->check_port_speed(handle, cmd->base.speed);
  613. if (ret) {
  614. netdev_err(netdev, "unsupported speed\n");
  615. return ret;
  616. }
  617. }
  618. return 0;
  619. }
  620. static int hns3_set_link_ksettings(struct net_device *netdev,
  621. const struct ethtool_link_ksettings *cmd)
  622. {
  623. struct hnae3_handle *handle = hns3_get_handle(netdev);
  624. const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
  625. int ret;
  626. /* Chip don't support this mode. */
  627. if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
  628. return -EINVAL;
  629. netif_dbg(handle, drv, netdev,
  630. "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
  631. netdev->phydev ? "phy" : "mac",
  632. cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
  633. /* Only support ksettings_set for netdev with phy attached for now */
  634. if (netdev->phydev)
  635. return phy_ethtool_ksettings_set(netdev->phydev, cmd);
  636. if (handle->pdev->revision == 0x20)
  637. return -EOPNOTSUPP;
  638. ret = hns3_check_ksettings_param(netdev, cmd);
  639. if (ret)
  640. return ret;
  641. if (ops->set_autoneg) {
  642. ret = ops->set_autoneg(handle, cmd->base.autoneg);
  643. if (ret)
  644. return ret;
  645. }
  646. /* hw doesn't support use specified speed and duplex to negotiate,
  647. * ignore them when autoneg on.
  648. */
  649. if (cmd->base.autoneg) {
  650. netdev_info(netdev,
  651. "autoneg is on, ignore the speed and duplex\n");
  652. return 0;
  653. }
  654. if (ops->cfg_mac_speed_dup_h)
  655. ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
  656. cmd->base.duplex);
  657. return ret;
  658. }
  659. static u32 hns3_get_rss_key_size(struct net_device *netdev)
  660. {
  661. struct hnae3_handle *h = hns3_get_handle(netdev);
  662. if (!h->ae_algo->ops->get_rss_key_size)
  663. return 0;
  664. return h->ae_algo->ops->get_rss_key_size(h);
  665. }
  666. static u32 hns3_get_rss_indir_size(struct net_device *netdev)
  667. {
  668. struct hnae3_handle *h = hns3_get_handle(netdev);
  669. if (!h->ae_algo->ops->get_rss_indir_size)
  670. return 0;
  671. return h->ae_algo->ops->get_rss_indir_size(h);
  672. }
  673. static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
  674. u8 *hfunc)
  675. {
  676. struct hnae3_handle *h = hns3_get_handle(netdev);
  677. if (!h->ae_algo->ops->get_rss)
  678. return -EOPNOTSUPP;
  679. return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
  680. }
  681. static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
  682. const u8 *key, const u8 hfunc)
  683. {
  684. struct hnae3_handle *h = hns3_get_handle(netdev);
  685. if (!h->ae_algo->ops->set_rss)
  686. return -EOPNOTSUPP;
  687. if ((h->pdev->revision == 0x20 &&
  688. hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
  689. hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
  690. netdev_err(netdev, "hash func not supported\n");
  691. return -EOPNOTSUPP;
  692. }
  693. if (!indir) {
  694. netdev_err(netdev,
  695. "set rss failed for indir is empty\n");
  696. return -EOPNOTSUPP;
  697. }
  698. return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
  699. }
  700. static int hns3_get_rxnfc(struct net_device *netdev,
  701. struct ethtool_rxnfc *cmd,
  702. u32 *rule_locs)
  703. {
  704. struct hnae3_handle *h = hns3_get_handle(netdev);
  705. switch (cmd->cmd) {
  706. case ETHTOOL_GRXRINGS:
  707. cmd->data = h->kinfo.num_tqps;
  708. return 0;
  709. case ETHTOOL_GRXFH:
  710. if (h->ae_algo->ops->get_rss_tuple)
  711. return h->ae_algo->ops->get_rss_tuple(h, cmd);
  712. return -EOPNOTSUPP;
  713. case ETHTOOL_GRXCLSRLCNT:
  714. if (h->ae_algo->ops->get_fd_rule_cnt)
  715. return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
  716. return -EOPNOTSUPP;
  717. case ETHTOOL_GRXCLSRULE:
  718. if (h->ae_algo->ops->get_fd_rule_info)
  719. return h->ae_algo->ops->get_fd_rule_info(h, cmd);
  720. return -EOPNOTSUPP;
  721. case ETHTOOL_GRXCLSRLALL:
  722. if (h->ae_algo->ops->get_fd_all_rules)
  723. return h->ae_algo->ops->get_fd_all_rules(h, cmd,
  724. rule_locs);
  725. return -EOPNOTSUPP;
  726. default:
  727. return -EOPNOTSUPP;
  728. }
  729. }
  730. static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
  731. u32 tx_desc_num, u32 rx_desc_num)
  732. {
  733. struct hnae3_handle *h = priv->ae_handle;
  734. int i;
  735. h->kinfo.num_tx_desc = tx_desc_num;
  736. h->kinfo.num_rx_desc = rx_desc_num;
  737. for (i = 0; i < h->kinfo.num_tqps; i++) {
  738. priv->ring_data[i].ring->desc_num = tx_desc_num;
  739. priv->ring_data[i + h->kinfo.num_tqps].ring->desc_num =
  740. rx_desc_num;
  741. }
  742. }
  743. static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
  744. {
  745. struct hnae3_handle *handle = priv->ae_handle;
  746. struct hns3_enet_ring *tmp_rings;
  747. int i;
  748. tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
  749. sizeof(struct hns3_enet_ring), GFP_KERNEL);
  750. if (!tmp_rings)
  751. return NULL;
  752. for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
  753. memcpy(&tmp_rings[i], priv->ring_data[i].ring,
  754. sizeof(struct hns3_enet_ring));
  755. tmp_rings[i].skb = NULL;
  756. }
  757. return tmp_rings;
  758. }
  759. static int hns3_check_ringparam(struct net_device *ndev,
  760. struct ethtool_ringparam *param)
  761. {
  762. if (hns3_nic_resetting(ndev))
  763. return -EBUSY;
  764. if (param->rx_mini_pending || param->rx_jumbo_pending)
  765. return -EINVAL;
  766. if (param->tx_pending > HNS3_RING_MAX_PENDING ||
  767. param->tx_pending < HNS3_RING_MIN_PENDING ||
  768. param->rx_pending > HNS3_RING_MAX_PENDING ||
  769. param->rx_pending < HNS3_RING_MIN_PENDING) {
  770. netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
  771. HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
  772. return -EINVAL;
  773. }
  774. return 0;
  775. }
  776. static int hns3_set_ringparam(struct net_device *ndev,
  777. struct ethtool_ringparam *param)
  778. {
  779. struct hns3_nic_priv *priv = netdev_priv(ndev);
  780. struct hnae3_handle *h = priv->ae_handle;
  781. struct hns3_enet_ring *tmp_rings;
  782. bool if_running = netif_running(ndev);
  783. u32 old_tx_desc_num, new_tx_desc_num;
  784. u32 old_rx_desc_num, new_rx_desc_num;
  785. u16 queue_num = h->kinfo.num_tqps;
  786. int ret, i;
  787. ret = hns3_check_ringparam(ndev, param);
  788. if (ret)
  789. return ret;
  790. /* Hardware requires that its descriptors must be multiple of eight */
  791. new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
  792. new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
  793. old_tx_desc_num = priv->ring_data[0].ring->desc_num;
  794. old_rx_desc_num = priv->ring_data[queue_num].ring->desc_num;
  795. if (old_tx_desc_num == new_tx_desc_num &&
  796. old_rx_desc_num == new_rx_desc_num)
  797. return 0;
  798. tmp_rings = hns3_backup_ringparam(priv);
  799. if (!tmp_rings) {
  800. netdev_err(ndev,
  801. "backup ring param failed by allocating memory fail\n");
  802. return -ENOMEM;
  803. }
  804. netdev_info(ndev,
  805. "Changing Tx/Rx ring depth from %d/%d to %d/%d\n",
  806. old_tx_desc_num, old_rx_desc_num,
  807. new_tx_desc_num, new_rx_desc_num);
  808. if (if_running)
  809. ndev->netdev_ops->ndo_stop(ndev);
  810. hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
  811. ret = hns3_init_all_ring(priv);
  812. if (ret) {
  813. netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
  814. ret);
  815. hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
  816. old_rx_desc_num);
  817. for (i = 0; i < h->kinfo.num_tqps * 2; i++)
  818. memcpy(priv->ring_data[i].ring, &tmp_rings[i],
  819. sizeof(struct hns3_enet_ring));
  820. } else {
  821. for (i = 0; i < h->kinfo.num_tqps * 2; i++)
  822. hns3_fini_ring(&tmp_rings[i]);
  823. }
  824. kfree(tmp_rings);
  825. if (if_running)
  826. ret = ndev->netdev_ops->ndo_open(ndev);
  827. return ret;
  828. }
  829. static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  830. {
  831. struct hnae3_handle *h = hns3_get_handle(netdev);
  832. switch (cmd->cmd) {
  833. case ETHTOOL_SRXFH:
  834. if (h->ae_algo->ops->set_rss_tuple)
  835. return h->ae_algo->ops->set_rss_tuple(h, cmd);
  836. return -EOPNOTSUPP;
  837. case ETHTOOL_SRXCLSRLINS:
  838. if (h->ae_algo->ops->add_fd_entry)
  839. return h->ae_algo->ops->add_fd_entry(h, cmd);
  840. return -EOPNOTSUPP;
  841. case ETHTOOL_SRXCLSRLDEL:
  842. if (h->ae_algo->ops->del_fd_entry)
  843. return h->ae_algo->ops->del_fd_entry(h, cmd);
  844. return -EOPNOTSUPP;
  845. default:
  846. return -EOPNOTSUPP;
  847. }
  848. }
  849. static int hns3_nway_reset(struct net_device *netdev)
  850. {
  851. struct hnae3_handle *handle = hns3_get_handle(netdev);
  852. const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
  853. struct phy_device *phy = netdev->phydev;
  854. int autoneg;
  855. if (!netif_running(netdev))
  856. return 0;
  857. if (hns3_nic_resetting(netdev)) {
  858. netdev_err(netdev, "dev resetting!");
  859. return -EBUSY;
  860. }
  861. if (!ops->get_autoneg || !ops->restart_autoneg)
  862. return -EOPNOTSUPP;
  863. autoneg = ops->get_autoneg(handle);
  864. if (autoneg != AUTONEG_ENABLE) {
  865. netdev_err(netdev,
  866. "Autoneg is off, don't support to restart it\n");
  867. return -EINVAL;
  868. }
  869. netif_dbg(handle, drv, netdev,
  870. "nway reset (using %s)\n", phy ? "phy" : "mac");
  871. if (phy)
  872. return genphy_restart_aneg(phy);
  873. if (handle->pdev->revision == 0x20)
  874. return -EOPNOTSUPP;
  875. return ops->restart_autoneg(handle);
  876. }
  877. static void hns3_get_channels(struct net_device *netdev,
  878. struct ethtool_channels *ch)
  879. {
  880. struct hnae3_handle *h = hns3_get_handle(netdev);
  881. if (h->ae_algo->ops->get_channels)
  882. h->ae_algo->ops->get_channels(h, ch);
  883. }
  884. static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
  885. struct ethtool_coalesce *cmd)
  886. {
  887. struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
  888. struct hns3_nic_priv *priv = netdev_priv(netdev);
  889. struct hnae3_handle *h = priv->ae_handle;
  890. u16 queue_num = h->kinfo.num_tqps;
  891. if (hns3_nic_resetting(netdev))
  892. return -EBUSY;
  893. if (queue >= queue_num) {
  894. netdev_err(netdev,
  895. "Invalid queue value %d! Queue max id=%d\n",
  896. queue, queue_num - 1);
  897. return -EINVAL;
  898. }
  899. tx_vector = priv->ring_data[queue].ring->tqp_vector;
  900. rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
  901. cmd->use_adaptive_tx_coalesce =
  902. tx_vector->tx_group.coal.gl_adapt_enable;
  903. cmd->use_adaptive_rx_coalesce =
  904. rx_vector->rx_group.coal.gl_adapt_enable;
  905. cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
  906. cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
  907. cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
  908. cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
  909. return 0;
  910. }
  911. static int hns3_get_coalesce(struct net_device *netdev,
  912. struct ethtool_coalesce *cmd)
  913. {
  914. return hns3_get_coalesce_per_queue(netdev, 0, cmd);
  915. }
  916. static int hns3_check_gl_coalesce_para(struct net_device *netdev,
  917. struct ethtool_coalesce *cmd)
  918. {
  919. u32 rx_gl, tx_gl;
  920. if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
  921. netdev_err(netdev,
  922. "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
  923. HNS3_INT_GL_MAX);
  924. return -EINVAL;
  925. }
  926. if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
  927. netdev_err(netdev,
  928. "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
  929. HNS3_INT_GL_MAX);
  930. return -EINVAL;
  931. }
  932. rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
  933. if (rx_gl != cmd->rx_coalesce_usecs) {
  934. netdev_info(netdev,
  935. "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
  936. cmd->rx_coalesce_usecs, rx_gl);
  937. }
  938. tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
  939. if (tx_gl != cmd->tx_coalesce_usecs) {
  940. netdev_info(netdev,
  941. "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
  942. cmd->tx_coalesce_usecs, tx_gl);
  943. }
  944. return 0;
  945. }
  946. static int hns3_check_rl_coalesce_para(struct net_device *netdev,
  947. struct ethtool_coalesce *cmd)
  948. {
  949. u32 rl;
  950. if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
  951. netdev_err(netdev,
  952. "tx_usecs_high must be same as rx_usecs_high.\n");
  953. return -EINVAL;
  954. }
  955. if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
  956. netdev_err(netdev,
  957. "Invalid usecs_high value, usecs_high range is 0-%d\n",
  958. HNS3_INT_RL_MAX);
  959. return -EINVAL;
  960. }
  961. rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
  962. if (rl != cmd->rx_coalesce_usecs_high) {
  963. netdev_info(netdev,
  964. "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
  965. cmd->rx_coalesce_usecs_high, rl);
  966. }
  967. return 0;
  968. }
  969. static int hns3_check_coalesce_para(struct net_device *netdev,
  970. struct ethtool_coalesce *cmd)
  971. {
  972. int ret;
  973. ret = hns3_check_gl_coalesce_para(netdev, cmd);
  974. if (ret) {
  975. netdev_err(netdev,
  976. "Check gl coalesce param fail. ret = %d\n", ret);
  977. return ret;
  978. }
  979. ret = hns3_check_rl_coalesce_para(netdev, cmd);
  980. if (ret) {
  981. netdev_err(netdev,
  982. "Check rl coalesce param fail. ret = %d\n", ret);
  983. return ret;
  984. }
  985. if (cmd->use_adaptive_tx_coalesce == 1 ||
  986. cmd->use_adaptive_rx_coalesce == 1) {
  987. netdev_info(netdev,
  988. "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
  989. cmd->use_adaptive_tx_coalesce,
  990. cmd->use_adaptive_rx_coalesce);
  991. }
  992. return 0;
  993. }
  994. static void hns3_set_coalesce_per_queue(struct net_device *netdev,
  995. struct ethtool_coalesce *cmd,
  996. u32 queue)
  997. {
  998. struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
  999. struct hns3_nic_priv *priv = netdev_priv(netdev);
  1000. struct hnae3_handle *h = priv->ae_handle;
  1001. int queue_num = h->kinfo.num_tqps;
  1002. tx_vector = priv->ring_data[queue].ring->tqp_vector;
  1003. rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
  1004. tx_vector->tx_group.coal.gl_adapt_enable =
  1005. cmd->use_adaptive_tx_coalesce;
  1006. rx_vector->rx_group.coal.gl_adapt_enable =
  1007. cmd->use_adaptive_rx_coalesce;
  1008. tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
  1009. rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
  1010. hns3_set_vector_coalesce_tx_gl(tx_vector,
  1011. tx_vector->tx_group.coal.int_gl);
  1012. hns3_set_vector_coalesce_rx_gl(rx_vector,
  1013. rx_vector->rx_group.coal.int_gl);
  1014. hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
  1015. hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
  1016. }
  1017. static int hns3_set_coalesce(struct net_device *netdev,
  1018. struct ethtool_coalesce *cmd)
  1019. {
  1020. struct hnae3_handle *h = hns3_get_handle(netdev);
  1021. u16 queue_num = h->kinfo.num_tqps;
  1022. int ret;
  1023. int i;
  1024. if (hns3_nic_resetting(netdev))
  1025. return -EBUSY;
  1026. ret = hns3_check_coalesce_para(netdev, cmd);
  1027. if (ret)
  1028. return ret;
  1029. h->kinfo.int_rl_setting =
  1030. hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
  1031. for (i = 0; i < queue_num; i++)
  1032. hns3_set_coalesce_per_queue(netdev, cmd, i);
  1033. return 0;
  1034. }
  1035. static int hns3_get_regs_len(struct net_device *netdev)
  1036. {
  1037. struct hnae3_handle *h = hns3_get_handle(netdev);
  1038. if (!h->ae_algo->ops->get_regs_len)
  1039. return -EOPNOTSUPP;
  1040. return h->ae_algo->ops->get_regs_len(h);
  1041. }
  1042. static void hns3_get_regs(struct net_device *netdev,
  1043. struct ethtool_regs *cmd, void *data)
  1044. {
  1045. struct hnae3_handle *h = hns3_get_handle(netdev);
  1046. if (!h->ae_algo->ops->get_regs)
  1047. return;
  1048. h->ae_algo->ops->get_regs(h, &cmd->version, data);
  1049. }
  1050. static int hns3_set_phys_id(struct net_device *netdev,
  1051. enum ethtool_phys_id_state state)
  1052. {
  1053. struct hnae3_handle *h = hns3_get_handle(netdev);
  1054. if (!h->ae_algo->ops->set_led_id)
  1055. return -EOPNOTSUPP;
  1056. return h->ae_algo->ops->set_led_id(h, state);
  1057. }
  1058. static u32 hns3_get_msglevel(struct net_device *netdev)
  1059. {
  1060. struct hnae3_handle *h = hns3_get_handle(netdev);
  1061. return h->msg_enable;
  1062. }
  1063. static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
  1064. {
  1065. struct hnae3_handle *h = hns3_get_handle(netdev);
  1066. h->msg_enable = msg_level;
  1067. }
  1068. /* Translate local fec value into ethtool value. */
  1069. static unsigned int loc_to_eth_fec(u8 loc_fec)
  1070. {
  1071. u32 eth_fec = 0;
  1072. if (loc_fec & BIT(HNAE3_FEC_AUTO))
  1073. eth_fec |= ETHTOOL_FEC_AUTO;
  1074. if (loc_fec & BIT(HNAE3_FEC_RS))
  1075. eth_fec |= ETHTOOL_FEC_RS;
  1076. if (loc_fec & BIT(HNAE3_FEC_BASER))
  1077. eth_fec |= ETHTOOL_FEC_BASER;
  1078. /* if nothing is set, then FEC is off */
  1079. if (!eth_fec)
  1080. eth_fec = ETHTOOL_FEC_OFF;
  1081. return eth_fec;
  1082. }
  1083. /* Translate ethtool fec value into local value. */
  1084. static unsigned int eth_to_loc_fec(unsigned int eth_fec)
  1085. {
  1086. u32 loc_fec = 0;
  1087. if (eth_fec & ETHTOOL_FEC_OFF)
  1088. return loc_fec;
  1089. if (eth_fec & ETHTOOL_FEC_AUTO)
  1090. loc_fec |= BIT(HNAE3_FEC_AUTO);
  1091. if (eth_fec & ETHTOOL_FEC_RS)
  1092. loc_fec |= BIT(HNAE3_FEC_RS);
  1093. if (eth_fec & ETHTOOL_FEC_BASER)
  1094. loc_fec |= BIT(HNAE3_FEC_BASER);
  1095. return loc_fec;
  1096. }
  1097. static int hns3_get_fecparam(struct net_device *netdev,
  1098. struct ethtool_fecparam *fec)
  1099. {
  1100. struct hnae3_handle *handle = hns3_get_handle(netdev);
  1101. const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
  1102. u8 fec_ability;
  1103. u8 fec_mode;
  1104. if (handle->pdev->revision == 0x20)
  1105. return -EOPNOTSUPP;
  1106. if (!ops->get_fec)
  1107. return -EOPNOTSUPP;
  1108. ops->get_fec(handle, &fec_ability, &fec_mode);
  1109. fec->fec = loc_to_eth_fec(fec_ability);
  1110. fec->active_fec = loc_to_eth_fec(fec_mode);
  1111. return 0;
  1112. }
  1113. static int hns3_set_fecparam(struct net_device *netdev,
  1114. struct ethtool_fecparam *fec)
  1115. {
  1116. struct hnae3_handle *handle = hns3_get_handle(netdev);
  1117. const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
  1118. u32 fec_mode;
  1119. if (handle->pdev->revision == 0x20)
  1120. return -EOPNOTSUPP;
  1121. if (!ops->set_fec)
  1122. return -EOPNOTSUPP;
  1123. fec_mode = eth_to_loc_fec(fec->fec);
  1124. netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
  1125. return ops->set_fec(handle, fec_mode);
  1126. }
  1127. static const struct ethtool_ops hns3vf_ethtool_ops = {
  1128. .get_drvinfo = hns3_get_drvinfo,
  1129. .get_ringparam = hns3_get_ringparam,
  1130. .set_ringparam = hns3_set_ringparam,
  1131. .get_strings = hns3_get_strings,
  1132. .get_ethtool_stats = hns3_get_stats,
  1133. .get_sset_count = hns3_get_sset_count,
  1134. .get_rxnfc = hns3_get_rxnfc,
  1135. .set_rxnfc = hns3_set_rxnfc,
  1136. .get_rxfh_key_size = hns3_get_rss_key_size,
  1137. .get_rxfh_indir_size = hns3_get_rss_indir_size,
  1138. .get_rxfh = hns3_get_rss,
  1139. .set_rxfh = hns3_set_rss,
  1140. .get_link_ksettings = hns3_get_link_ksettings,
  1141. .get_channels = hns3_get_channels,
  1142. .set_channels = hns3_set_channels,
  1143. .get_coalesce = hns3_get_coalesce,
  1144. .set_coalesce = hns3_set_coalesce,
  1145. .get_regs_len = hns3_get_regs_len,
  1146. .get_regs = hns3_get_regs,
  1147. .get_link = hns3_get_link,
  1148. .get_msglevel = hns3_get_msglevel,
  1149. .set_msglevel = hns3_set_msglevel,
  1150. };
  1151. static const struct ethtool_ops hns3_ethtool_ops = {
  1152. .self_test = hns3_self_test,
  1153. .get_drvinfo = hns3_get_drvinfo,
  1154. .get_link = hns3_get_link,
  1155. .get_ringparam = hns3_get_ringparam,
  1156. .set_ringparam = hns3_set_ringparam,
  1157. .get_pauseparam = hns3_get_pauseparam,
  1158. .set_pauseparam = hns3_set_pauseparam,
  1159. .get_strings = hns3_get_strings,
  1160. .get_ethtool_stats = hns3_get_stats,
  1161. .get_sset_count = hns3_get_sset_count,
  1162. .get_rxnfc = hns3_get_rxnfc,
  1163. .set_rxnfc = hns3_set_rxnfc,
  1164. .get_rxfh_key_size = hns3_get_rss_key_size,
  1165. .get_rxfh_indir_size = hns3_get_rss_indir_size,
  1166. .get_rxfh = hns3_get_rss,
  1167. .set_rxfh = hns3_set_rss,
  1168. .get_link_ksettings = hns3_get_link_ksettings,
  1169. .set_link_ksettings = hns3_set_link_ksettings,
  1170. .nway_reset = hns3_nway_reset,
  1171. .get_channels = hns3_get_channels,
  1172. .set_channels = hns3_set_channels,
  1173. .get_coalesce = hns3_get_coalesce,
  1174. .set_coalesce = hns3_set_coalesce,
  1175. .get_regs_len = hns3_get_regs_len,
  1176. .get_regs = hns3_get_regs,
  1177. .set_phys_id = hns3_set_phys_id,
  1178. .get_msglevel = hns3_get_msglevel,
  1179. .set_msglevel = hns3_set_msglevel,
  1180. .get_fecparam = hns3_get_fecparam,
  1181. .set_fecparam = hns3_set_fecparam,
  1182. };
  1183. void hns3_ethtool_set_ops(struct net_device *netdev)
  1184. {
  1185. struct hnae3_handle *h = hns3_get_handle(netdev);
  1186. if (h->flags & HNAE3_SUPPORT_VF)
  1187. netdev->ethtool_ops = &hns3vf_ethtool_ops;
  1188. else
  1189. netdev->ethtool_ops = &hns3_ethtool_ops;
  1190. }