hns3_ethtool.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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("tx_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("tx_wake", restart_queue),
  26. HNS3_TQP_STAT("tx_busy", tx_busy),
  27. };
  28. #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  29. static const struct hns3_stats hns3_rxq_stats[] = {
  30. /* Rx per-queue statistics */
  31. HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
  32. HNS3_TQP_STAT("rx_dropped", sw_err_cnt),
  33. HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  34. HNS3_TQP_STAT("packets", rx_pkts),
  35. HNS3_TQP_STAT("bytes", rx_bytes),
  36. HNS3_TQP_STAT("errors", rx_err_cnt),
  37. HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
  38. HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
  39. HNS3_TQP_STAT("non_vld_descs", non_vld_descs),
  40. HNS3_TQP_STAT("err_bd_num", err_bd_num),
  41. HNS3_TQP_STAT("l2_err", l2_err),
  42. HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
  43. };
  44. #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  45. #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  46. #define HNS3_SELF_TEST_TYPE_NUM 2
  47. #define HNS3_NIC_LB_TEST_PKT_NUM 1
  48. #define HNS3_NIC_LB_TEST_RING_ID 0
  49. #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
  50. /* Nic loopback test err */
  51. #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
  52. #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
  53. #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
  54. struct hns3_link_mode_mapping {
  55. u32 hns3_link_mode;
  56. u32 ethtool_link_mode;
  57. };
  58. static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
  59. {
  60. struct hnae3_handle *h = hns3_get_handle(ndev);
  61. int ret;
  62. if (!h->ae_algo->ops->set_loopback ||
  63. !h->ae_algo->ops->set_promisc_mode)
  64. return -EOPNOTSUPP;
  65. switch (loop) {
  66. case HNAE3_MAC_INTER_LOOP_SERDES:
  67. case HNAE3_MAC_INTER_LOOP_MAC:
  68. ret = h->ae_algo->ops->set_loopback(h, loop, en);
  69. break;
  70. default:
  71. ret = -ENOTSUPP;
  72. break;
  73. }
  74. if (ret)
  75. return ret;
  76. h->ae_algo->ops->set_promisc_mode(h, en, en);
  77. return ret;
  78. }
  79. static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
  80. {
  81. struct hnae3_handle *h = hns3_get_handle(ndev);
  82. int ret;
  83. ret = hns3_nic_reset_all_ring(h);
  84. if (ret)
  85. return ret;
  86. ret = hns3_lp_setup(ndev, loop_mode, true);
  87. usleep_range(10000, 20000);
  88. return 0;
  89. }
  90. static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
  91. {
  92. int ret;
  93. ret = hns3_lp_setup(ndev, loop_mode, false);
  94. if (ret) {
  95. netdev_err(ndev, "lb_setup return error: %d\n", ret);
  96. return ret;
  97. }
  98. usleep_range(10000, 20000);
  99. return 0;
  100. }
  101. static void hns3_lp_setup_skb(struct sk_buff *skb)
  102. {
  103. struct net_device *ndev = skb->dev;
  104. unsigned char *packet;
  105. struct ethhdr *ethh;
  106. unsigned int i;
  107. skb_reserve(skb, NET_IP_ALIGN);
  108. ethh = skb_put(skb, sizeof(struct ethhdr));
  109. packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
  110. memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
  111. ethh->h_dest[5] += 0x1f;
  112. eth_zero_addr(ethh->h_source);
  113. ethh->h_proto = htons(ETH_P_ARP);
  114. skb_reset_mac_header(skb);
  115. for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
  116. packet[i] = (unsigned char)(i & 0xff);
  117. }
  118. static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
  119. struct sk_buff *skb)
  120. {
  121. struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
  122. unsigned char *packet = skb->data;
  123. u32 len = skb_headlen(skb);
  124. u32 i;
  125. len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
  126. for (i = 0; i < len; i++)
  127. if (packet[i] != (unsigned char)(i & 0xff))
  128. break;
  129. /* The packet is correctly received */
  130. if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
  131. tqp_vector->rx_group.total_packets++;
  132. else
  133. print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
  134. skb->data, len, true);
  135. dev_kfree_skb_any(skb);
  136. }
  137. static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
  138. {
  139. struct hnae3_handle *h = priv->ae_handle;
  140. struct hnae3_knic_private_info *kinfo;
  141. u32 i, rcv_good_pkt_total = 0;
  142. kinfo = &h->kinfo;
  143. for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
  144. struct hns3_enet_ring *ring = priv->ring_data[i].ring;
  145. struct hns3_enet_ring_group *rx_group;
  146. u64 pre_rx_pkt;
  147. rx_group = &ring->tqp_vector->rx_group;
  148. pre_rx_pkt = rx_group->total_packets;
  149. preempt_disable();
  150. hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
  151. preempt_enable();
  152. rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
  153. rx_group->total_packets = pre_rx_pkt;
  154. }
  155. return rcv_good_pkt_total;
  156. }
  157. static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
  158. u32 end_ringid, u32 budget)
  159. {
  160. u32 i;
  161. for (i = start_ringid; i <= end_ringid; i++) {
  162. struct hns3_enet_ring *ring = priv->ring_data[i].ring;
  163. hns3_clean_tx_ring(ring, budget);
  164. }
  165. }
  166. /**
  167. * hns3_lp_run_test - run loopback test
  168. * @ndev: net device
  169. * @mode: loopback type
  170. */
  171. static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
  172. {
  173. struct hns3_nic_priv *priv = netdev_priv(ndev);
  174. struct sk_buff *skb;
  175. u32 i, good_cnt;
  176. int ret_val = 0;
  177. skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
  178. GFP_KERNEL);
  179. if (!skb)
  180. return HNS3_NIC_LB_TEST_NO_MEM_ERR;
  181. skb->dev = ndev;
  182. hns3_lp_setup_skb(skb);
  183. skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
  184. good_cnt = 0;
  185. for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
  186. netdev_tx_t tx_ret;
  187. skb_get(skb);
  188. tx_ret = hns3_nic_net_xmit(skb, ndev);
  189. if (tx_ret == NETDEV_TX_OK) {
  190. good_cnt++;
  191. } else {
  192. kfree_skb(skb);
  193. netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
  194. tx_ret);
  195. }
  196. }
  197. if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
  198. ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
  199. netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
  200. mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
  201. goto out;
  202. }
  203. /* Allow 200 milliseconds for packets to go from Tx to Rx */
  204. msleep(200);
  205. good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
  206. if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
  207. ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
  208. netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
  209. mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
  210. }
  211. out:
  212. hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
  213. HNS3_NIC_LB_TEST_RING_ID,
  214. HNS3_NIC_LB_TEST_PKT_NUM);
  215. kfree_skb(skb);
  216. return ret_val;
  217. }
  218. /**
  219. * hns3_nic_self_test - self test
  220. * @ndev: net device
  221. * @eth_test: test cmd
  222. * @data: test result
  223. */
  224. static void hns3_self_test(struct net_device *ndev,
  225. struct ethtool_test *eth_test, u64 *data)
  226. {
  227. struct hns3_nic_priv *priv = netdev_priv(ndev);
  228. struct hnae3_handle *h = priv->ae_handle;
  229. int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
  230. bool if_running = netif_running(ndev);
  231. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  232. bool dis_vlan_filter;
  233. #endif
  234. int test_index = 0;
  235. u32 i;
  236. /* Only do offline selftest, or pass by default */
  237. if (eth_test->flags != ETH_TEST_FL_OFFLINE)
  238. return;
  239. st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
  240. st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
  241. h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
  242. st_param[HNAE3_MAC_INTER_LOOP_SERDES][0] = HNAE3_MAC_INTER_LOOP_SERDES;
  243. st_param[HNAE3_MAC_INTER_LOOP_SERDES][1] =
  244. h->flags & HNAE3_SUPPORT_SERDES_LOOPBACK;
  245. if (if_running)
  246. ndev->netdev_ops->ndo_stop(ndev);
  247. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  248. /* Disable the vlan filter for selftest does not support it */
  249. dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  250. h->ae_algo->ops->enable_vlan_filter;
  251. if (dis_vlan_filter)
  252. h->ae_algo->ops->enable_vlan_filter(h, false);
  253. #endif
  254. set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
  255. for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
  256. enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
  257. if (!st_param[i][1])
  258. continue;
  259. data[test_index] = hns3_lp_up(ndev, loop_type);
  260. if (!data[test_index]) {
  261. data[test_index] = hns3_lp_run_test(ndev, loop_type);
  262. hns3_lp_down(ndev, loop_type);
  263. }
  264. if (data[test_index])
  265. eth_test->flags |= ETH_TEST_FL_FAILED;
  266. test_index++;
  267. }
  268. clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
  269. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  270. if (dis_vlan_filter)
  271. h->ae_algo->ops->enable_vlan_filter(h, true);
  272. #endif
  273. if (if_running)
  274. ndev->netdev_ops->ndo_open(ndev);
  275. }
  276. static int hns3_get_sset_count(struct net_device *netdev, int stringset)
  277. {
  278. struct hnae3_handle *h = hns3_get_handle(netdev);
  279. const struct hnae3_ae_ops *ops = h->ae_algo->ops;
  280. if (!ops->get_sset_count)
  281. return -EOPNOTSUPP;
  282. switch (stringset) {
  283. case ETH_SS_STATS:
  284. return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
  285. ops->get_sset_count(h, stringset));
  286. case ETH_SS_TEST:
  287. return ops->get_sset_count(h, stringset);
  288. }
  289. return 0;
  290. }
  291. static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
  292. u32 stat_count, u32 num_tqps, const char *prefix)
  293. {
  294. #define MAX_PREFIX_SIZE (6 + 4)
  295. u32 size_left;
  296. u32 i, j;
  297. u32 n1;
  298. for (i = 0; i < num_tqps; i++) {
  299. for (j = 0; j < stat_count; j++) {
  300. data[ETH_GSTRING_LEN - 1] = '\0';
  301. /* first, prepend the prefix string */
  302. n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
  303. prefix, i);
  304. n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
  305. size_left = (ETH_GSTRING_LEN - 1) - n1;
  306. /* now, concatenate the stats string to it */
  307. strncat(data, stats[j].stats_string, size_left);
  308. data += ETH_GSTRING_LEN;
  309. }
  310. }
  311. return data;
  312. }
  313. static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
  314. {
  315. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  316. const char tx_prefix[] = "txq";
  317. const char rx_prefix[] = "rxq";
  318. /* get strings for Tx */
  319. data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
  320. kinfo->num_tqps, tx_prefix);
  321. /* get strings for Rx */
  322. data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
  323. kinfo->num_tqps, rx_prefix);
  324. return data;
  325. }
  326. static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  327. {
  328. struct hnae3_handle *h = hns3_get_handle(netdev);
  329. const struct hnae3_ae_ops *ops = h->ae_algo->ops;
  330. char *buff = (char *)data;
  331. if (!ops->get_strings)
  332. return;
  333. switch (stringset) {
  334. case ETH_SS_STATS:
  335. buff = hns3_get_strings_tqps(h, buff);
  336. h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
  337. break;
  338. case ETH_SS_TEST:
  339. ops->get_strings(h, stringset, data);
  340. break;
  341. }
  342. }
  343. static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
  344. {
  345. struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
  346. struct hnae3_knic_private_info *kinfo = &handle->kinfo;
  347. struct hns3_enet_ring *ring;
  348. u8 *stat;
  349. int i, j;
  350. /* get stats for Tx */
  351. for (i = 0; i < kinfo->num_tqps; i++) {
  352. ring = nic_priv->ring_data[i].ring;
  353. for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
  354. stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
  355. *data++ = *(u64 *)stat;
  356. }
  357. }
  358. /* get stats for Rx */
  359. for (i = 0; i < kinfo->num_tqps; i++) {
  360. ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
  361. for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
  362. stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
  363. *data++ = *(u64 *)stat;
  364. }
  365. }
  366. return data;
  367. }
  368. /* hns3_get_stats - get detail statistics.
  369. * @netdev: net device
  370. * @stats: statistics info.
  371. * @data: statistics data.
  372. */
  373. static void hns3_get_stats(struct net_device *netdev,
  374. struct ethtool_stats *stats, u64 *data)
  375. {
  376. struct hnae3_handle *h = hns3_get_handle(netdev);
  377. u64 *p = data;
  378. if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
  379. netdev_err(netdev, "could not get any statistics\n");
  380. return;
  381. }
  382. h->ae_algo->ops->update_stats(h, &netdev->stats);
  383. /* get per-queue stats */
  384. p = hns3_get_stats_tqps(h, p);
  385. /* get MAC & other misc hardware stats */
  386. h->ae_algo->ops->get_stats(h, p);
  387. }
  388. static void hns3_get_drvinfo(struct net_device *netdev,
  389. struct ethtool_drvinfo *drvinfo)
  390. {
  391. struct hns3_nic_priv *priv = netdev_priv(netdev);
  392. struct hnae3_handle *h = priv->ae_handle;
  393. strncpy(drvinfo->version, hns3_driver_version,
  394. sizeof(drvinfo->version));
  395. drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
  396. strncpy(drvinfo->driver, h->pdev->driver->name,
  397. sizeof(drvinfo->driver));
  398. drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
  399. strncpy(drvinfo->bus_info, pci_name(h->pdev),
  400. sizeof(drvinfo->bus_info));
  401. drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
  402. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
  403. priv->ae_handle->ae_algo->ops->get_fw_version(h));
  404. }
  405. static u32 hns3_get_link(struct net_device *netdev)
  406. {
  407. struct hnae3_handle *h = hns3_get_handle(netdev);
  408. if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
  409. return h->ae_algo->ops->get_status(h);
  410. else
  411. return 0;
  412. }
  413. static void hns3_get_ringparam(struct net_device *netdev,
  414. struct ethtool_ringparam *param)
  415. {
  416. struct hns3_nic_priv *priv = netdev_priv(netdev);
  417. struct hnae3_handle *h = priv->ae_handle;
  418. int queue_num = h->kinfo.num_tqps;
  419. param->tx_max_pending = HNS3_RING_MAX_PENDING;
  420. param->rx_max_pending = HNS3_RING_MAX_PENDING;
  421. param->tx_pending = priv->ring_data[0].ring->desc_num;
  422. param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
  423. }
  424. static void hns3_get_pauseparam(struct net_device *netdev,
  425. struct ethtool_pauseparam *param)
  426. {
  427. struct hnae3_handle *h = hns3_get_handle(netdev);
  428. if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
  429. h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
  430. &param->rx_pause, &param->tx_pause);
  431. }
  432. static int hns3_set_pauseparam(struct net_device *netdev,
  433. struct ethtool_pauseparam *param)
  434. {
  435. struct hnae3_handle *h = hns3_get_handle(netdev);
  436. if (h->ae_algo->ops->set_pauseparam)
  437. return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
  438. param->rx_pause,
  439. param->tx_pause);
  440. return -EOPNOTSUPP;
  441. }
  442. static int hns3_get_link_ksettings(struct net_device *netdev,
  443. struct ethtool_link_ksettings *cmd)
  444. {
  445. struct hnae3_handle *h = hns3_get_handle(netdev);
  446. u32 flowctrl_adv = 0;
  447. u8 link_stat;
  448. if (!h->ae_algo || !h->ae_algo->ops)
  449. return -EOPNOTSUPP;
  450. /* 1.auto_neg & speed & duplex from cmd */
  451. if (netdev->phydev) {
  452. phy_ethtool_ksettings_get(netdev->phydev, cmd);
  453. return 0;
  454. }
  455. if (h->ae_algo->ops->get_ksettings_an_result)
  456. h->ae_algo->ops->get_ksettings_an_result(h,
  457. &cmd->base.autoneg,
  458. &cmd->base.speed,
  459. &cmd->base.duplex);
  460. else
  461. return -EOPNOTSUPP;
  462. link_stat = hns3_get_link(netdev);
  463. if (!link_stat) {
  464. cmd->base.speed = SPEED_UNKNOWN;
  465. cmd->base.duplex = DUPLEX_UNKNOWN;
  466. }
  467. /* 2.get link mode and port type*/
  468. if (h->ae_algo->ops->get_link_mode)
  469. h->ae_algo->ops->get_link_mode(h,
  470. cmd->link_modes.supported,
  471. cmd->link_modes.advertising);
  472. cmd->base.port = PORT_NONE;
  473. if (h->ae_algo->ops->get_port_type)
  474. h->ae_algo->ops->get_port_type(h,
  475. &cmd->base.port);
  476. /* 3.mdix_ctrl&mdix get from phy reg */
  477. if (h->ae_algo->ops->get_mdix_mode)
  478. h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
  479. &cmd->base.eth_tp_mdix);
  480. /* 4.mdio_support */
  481. cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
  482. /* 5.get flow control setttings */
  483. if (h->ae_algo->ops->get_flowctrl_adv)
  484. h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
  485. if (flowctrl_adv & ADVERTISED_Pause)
  486. ethtool_link_ksettings_add_link_mode(cmd, advertising,
  487. Pause);
  488. if (flowctrl_adv & ADVERTISED_Asym_Pause)
  489. ethtool_link_ksettings_add_link_mode(cmd, advertising,
  490. Asym_Pause);
  491. return 0;
  492. }
  493. static int hns3_set_link_ksettings(struct net_device *netdev,
  494. const struct ethtool_link_ksettings *cmd)
  495. {
  496. /* Only support ksettings_set for netdev with phy attached for now */
  497. if (netdev->phydev)
  498. return phy_ethtool_ksettings_set(netdev->phydev, cmd);
  499. return -EOPNOTSUPP;
  500. }
  501. static u32 hns3_get_rss_key_size(struct net_device *netdev)
  502. {
  503. struct hnae3_handle *h = hns3_get_handle(netdev);
  504. if (!h->ae_algo || !h->ae_algo->ops ||
  505. !h->ae_algo->ops->get_rss_key_size)
  506. return 0;
  507. return h->ae_algo->ops->get_rss_key_size(h);
  508. }
  509. static u32 hns3_get_rss_indir_size(struct net_device *netdev)
  510. {
  511. struct hnae3_handle *h = hns3_get_handle(netdev);
  512. if (!h->ae_algo || !h->ae_algo->ops ||
  513. !h->ae_algo->ops->get_rss_indir_size)
  514. return 0;
  515. return h->ae_algo->ops->get_rss_indir_size(h);
  516. }
  517. static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
  518. u8 *hfunc)
  519. {
  520. struct hnae3_handle *h = hns3_get_handle(netdev);
  521. if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
  522. return -EOPNOTSUPP;
  523. return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
  524. }
  525. static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
  526. const u8 *key, const u8 hfunc)
  527. {
  528. struct hnae3_handle *h = hns3_get_handle(netdev);
  529. if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
  530. return -EOPNOTSUPP;
  531. /* currently we only support Toeplitz hash */
  532. if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
  533. netdev_err(netdev,
  534. "hash func not supported (only Toeplitz hash)\n");
  535. return -EOPNOTSUPP;
  536. }
  537. if (!indir) {
  538. netdev_err(netdev,
  539. "set rss failed for indir is empty\n");
  540. return -EOPNOTSUPP;
  541. }
  542. return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
  543. }
  544. static int hns3_get_rxnfc(struct net_device *netdev,
  545. struct ethtool_rxnfc *cmd,
  546. u32 *rule_locs)
  547. {
  548. struct hnae3_handle *h = hns3_get_handle(netdev);
  549. if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
  550. return -EOPNOTSUPP;
  551. switch (cmd->cmd) {
  552. case ETHTOOL_GRXRINGS:
  553. cmd->data = h->kinfo.rss_size;
  554. break;
  555. case ETHTOOL_GRXFH:
  556. return h->ae_algo->ops->get_rss_tuple(h, cmd);
  557. default:
  558. return -EOPNOTSUPP;
  559. }
  560. return 0;
  561. }
  562. static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
  563. u32 new_desc_num)
  564. {
  565. struct hnae3_handle *h = priv->ae_handle;
  566. int i;
  567. h->kinfo.num_desc = new_desc_num;
  568. for (i = 0; i < h->kinfo.num_tqps * 2; i++)
  569. priv->ring_data[i].ring->desc_num = new_desc_num;
  570. return hns3_init_all_ring(priv);
  571. }
  572. static int hns3_set_ringparam(struct net_device *ndev,
  573. struct ethtool_ringparam *param)
  574. {
  575. struct hns3_nic_priv *priv = netdev_priv(ndev);
  576. struct hnae3_handle *h = priv->ae_handle;
  577. bool if_running = netif_running(ndev);
  578. u32 old_desc_num, new_desc_num;
  579. int ret;
  580. if (param->rx_mini_pending || param->rx_jumbo_pending)
  581. return -EINVAL;
  582. if (param->tx_pending != param->rx_pending) {
  583. netdev_err(ndev,
  584. "Descriptors of tx and rx must be equal");
  585. return -EINVAL;
  586. }
  587. if (param->tx_pending > HNS3_RING_MAX_PENDING ||
  588. param->tx_pending < HNS3_RING_MIN_PENDING) {
  589. netdev_err(ndev,
  590. "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
  591. param->tx_pending, HNS3_RING_MIN_PENDING,
  592. HNS3_RING_MAX_PENDING);
  593. return -EINVAL;
  594. }
  595. new_desc_num = param->tx_pending;
  596. /* Hardware requires that its descriptors must be multiple of eight */
  597. new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
  598. old_desc_num = h->kinfo.num_desc;
  599. if (old_desc_num == new_desc_num)
  600. return 0;
  601. netdev_info(ndev,
  602. "Changing descriptor count from %d to %d.\n",
  603. old_desc_num, new_desc_num);
  604. if (if_running)
  605. dev_close(ndev);
  606. ret = hns3_uninit_all_ring(priv);
  607. if (ret)
  608. return ret;
  609. ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
  610. if (ret) {
  611. ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
  612. if (ret) {
  613. netdev_err(ndev,
  614. "Revert to old bd num fail, ret=%d.\n", ret);
  615. return ret;
  616. }
  617. }
  618. if (if_running)
  619. ret = dev_open(ndev);
  620. return ret;
  621. }
  622. static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  623. {
  624. struct hnae3_handle *h = hns3_get_handle(netdev);
  625. if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
  626. return -EOPNOTSUPP;
  627. switch (cmd->cmd) {
  628. case ETHTOOL_SRXFH:
  629. return h->ae_algo->ops->set_rss_tuple(h, cmd);
  630. default:
  631. return -EOPNOTSUPP;
  632. }
  633. }
  634. static int hns3_nway_reset(struct net_device *netdev)
  635. {
  636. struct phy_device *phy = netdev->phydev;
  637. if (!netif_running(netdev))
  638. return 0;
  639. /* Only support nway_reset for netdev with phy attached for now */
  640. if (!phy)
  641. return -EOPNOTSUPP;
  642. if (phy->autoneg != AUTONEG_ENABLE)
  643. return -EINVAL;
  644. return genphy_restart_aneg(phy);
  645. }
  646. static void hns3_get_channels(struct net_device *netdev,
  647. struct ethtool_channels *ch)
  648. {
  649. struct hnae3_handle *h = hns3_get_handle(netdev);
  650. if (h->ae_algo->ops->get_channels)
  651. h->ae_algo->ops->get_channels(h, ch);
  652. }
  653. static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
  654. struct ethtool_coalesce *cmd)
  655. {
  656. struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
  657. struct hns3_nic_priv *priv = netdev_priv(netdev);
  658. struct hnae3_handle *h = priv->ae_handle;
  659. u16 queue_num = h->kinfo.num_tqps;
  660. if (queue >= queue_num) {
  661. netdev_err(netdev,
  662. "Invalid queue value %d! Queue max id=%d\n",
  663. queue, queue_num - 1);
  664. return -EINVAL;
  665. }
  666. tx_vector = priv->ring_data[queue].ring->tqp_vector;
  667. rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
  668. cmd->use_adaptive_tx_coalesce =
  669. tx_vector->tx_group.coal.gl_adapt_enable;
  670. cmd->use_adaptive_rx_coalesce =
  671. rx_vector->rx_group.coal.gl_adapt_enable;
  672. cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
  673. cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
  674. cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
  675. cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
  676. return 0;
  677. }
  678. static int hns3_get_coalesce(struct net_device *netdev,
  679. struct ethtool_coalesce *cmd)
  680. {
  681. return hns3_get_coalesce_per_queue(netdev, 0, cmd);
  682. }
  683. static int hns3_check_gl_coalesce_para(struct net_device *netdev,
  684. struct ethtool_coalesce *cmd)
  685. {
  686. u32 rx_gl, tx_gl;
  687. if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
  688. netdev_err(netdev,
  689. "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
  690. HNS3_INT_GL_MAX);
  691. return -EINVAL;
  692. }
  693. if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
  694. netdev_err(netdev,
  695. "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
  696. HNS3_INT_GL_MAX);
  697. return -EINVAL;
  698. }
  699. rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
  700. if (rx_gl != cmd->rx_coalesce_usecs) {
  701. netdev_info(netdev,
  702. "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
  703. cmd->rx_coalesce_usecs, rx_gl);
  704. }
  705. tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
  706. if (tx_gl != cmd->tx_coalesce_usecs) {
  707. netdev_info(netdev,
  708. "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
  709. cmd->tx_coalesce_usecs, tx_gl);
  710. }
  711. return 0;
  712. }
  713. static int hns3_check_rl_coalesce_para(struct net_device *netdev,
  714. struct ethtool_coalesce *cmd)
  715. {
  716. u32 rl;
  717. if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
  718. netdev_err(netdev,
  719. "tx_usecs_high must be same as rx_usecs_high.\n");
  720. return -EINVAL;
  721. }
  722. if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
  723. netdev_err(netdev,
  724. "Invalid usecs_high value, usecs_high range is 0-%d\n",
  725. HNS3_INT_RL_MAX);
  726. return -EINVAL;
  727. }
  728. rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
  729. if (rl != cmd->rx_coalesce_usecs_high) {
  730. netdev_info(netdev,
  731. "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
  732. cmd->rx_coalesce_usecs_high, rl);
  733. }
  734. return 0;
  735. }
  736. static int hns3_check_coalesce_para(struct net_device *netdev,
  737. struct ethtool_coalesce *cmd)
  738. {
  739. int ret;
  740. ret = hns3_check_gl_coalesce_para(netdev, cmd);
  741. if (ret) {
  742. netdev_err(netdev,
  743. "Check gl coalesce param fail. ret = %d\n", ret);
  744. return ret;
  745. }
  746. ret = hns3_check_rl_coalesce_para(netdev, cmd);
  747. if (ret) {
  748. netdev_err(netdev,
  749. "Check rl coalesce param fail. ret = %d\n", ret);
  750. return ret;
  751. }
  752. if (cmd->use_adaptive_tx_coalesce == 1 ||
  753. cmd->use_adaptive_rx_coalesce == 1) {
  754. netdev_info(netdev,
  755. "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
  756. cmd->use_adaptive_tx_coalesce,
  757. cmd->use_adaptive_rx_coalesce);
  758. }
  759. return 0;
  760. }
  761. static void hns3_set_coalesce_per_queue(struct net_device *netdev,
  762. struct ethtool_coalesce *cmd,
  763. u32 queue)
  764. {
  765. struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
  766. struct hns3_nic_priv *priv = netdev_priv(netdev);
  767. struct hnae3_handle *h = priv->ae_handle;
  768. int queue_num = h->kinfo.num_tqps;
  769. tx_vector = priv->ring_data[queue].ring->tqp_vector;
  770. rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
  771. tx_vector->tx_group.coal.gl_adapt_enable =
  772. cmd->use_adaptive_tx_coalesce;
  773. rx_vector->rx_group.coal.gl_adapt_enable =
  774. cmd->use_adaptive_rx_coalesce;
  775. tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
  776. rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
  777. hns3_set_vector_coalesce_tx_gl(tx_vector,
  778. tx_vector->tx_group.coal.int_gl);
  779. hns3_set_vector_coalesce_rx_gl(rx_vector,
  780. rx_vector->rx_group.coal.int_gl);
  781. hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
  782. hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
  783. }
  784. static int hns3_set_coalesce(struct net_device *netdev,
  785. struct ethtool_coalesce *cmd)
  786. {
  787. struct hnae3_handle *h = hns3_get_handle(netdev);
  788. u16 queue_num = h->kinfo.num_tqps;
  789. int ret;
  790. int i;
  791. ret = hns3_check_coalesce_para(netdev, cmd);
  792. if (ret)
  793. return ret;
  794. h->kinfo.int_rl_setting =
  795. hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
  796. for (i = 0; i < queue_num; i++)
  797. hns3_set_coalesce_per_queue(netdev, cmd, i);
  798. return 0;
  799. }
  800. static int hns3_get_regs_len(struct net_device *netdev)
  801. {
  802. struct hnae3_handle *h = hns3_get_handle(netdev);
  803. if (!h->ae_algo->ops->get_regs_len)
  804. return -EOPNOTSUPP;
  805. return h->ae_algo->ops->get_regs_len(h);
  806. }
  807. static void hns3_get_regs(struct net_device *netdev,
  808. struct ethtool_regs *cmd, void *data)
  809. {
  810. struct hnae3_handle *h = hns3_get_handle(netdev);
  811. if (!h->ae_algo->ops->get_regs)
  812. return;
  813. h->ae_algo->ops->get_regs(h, &cmd->version, data);
  814. }
  815. static int hns3_set_phys_id(struct net_device *netdev,
  816. enum ethtool_phys_id_state state)
  817. {
  818. struct hnae3_handle *h = hns3_get_handle(netdev);
  819. if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
  820. return -EOPNOTSUPP;
  821. return h->ae_algo->ops->set_led_id(h, state);
  822. }
  823. static const struct ethtool_ops hns3vf_ethtool_ops = {
  824. .get_drvinfo = hns3_get_drvinfo,
  825. .get_ringparam = hns3_get_ringparam,
  826. .set_ringparam = hns3_set_ringparam,
  827. .get_strings = hns3_get_strings,
  828. .get_ethtool_stats = hns3_get_stats,
  829. .get_sset_count = hns3_get_sset_count,
  830. .get_rxnfc = hns3_get_rxnfc,
  831. .get_rxfh_key_size = hns3_get_rss_key_size,
  832. .get_rxfh_indir_size = hns3_get_rss_indir_size,
  833. .get_rxfh = hns3_get_rss,
  834. .set_rxfh = hns3_set_rss,
  835. .get_link_ksettings = hns3_get_link_ksettings,
  836. .get_channels = hns3_get_channels,
  837. .get_coalesce = hns3_get_coalesce,
  838. .set_coalesce = hns3_set_coalesce,
  839. .get_link = hns3_get_link,
  840. };
  841. static const struct ethtool_ops hns3_ethtool_ops = {
  842. .self_test = hns3_self_test,
  843. .get_drvinfo = hns3_get_drvinfo,
  844. .get_link = hns3_get_link,
  845. .get_ringparam = hns3_get_ringparam,
  846. .set_ringparam = hns3_set_ringparam,
  847. .get_pauseparam = hns3_get_pauseparam,
  848. .set_pauseparam = hns3_set_pauseparam,
  849. .get_strings = hns3_get_strings,
  850. .get_ethtool_stats = hns3_get_stats,
  851. .get_sset_count = hns3_get_sset_count,
  852. .get_rxnfc = hns3_get_rxnfc,
  853. .set_rxnfc = hns3_set_rxnfc,
  854. .get_rxfh_key_size = hns3_get_rss_key_size,
  855. .get_rxfh_indir_size = hns3_get_rss_indir_size,
  856. .get_rxfh = hns3_get_rss,
  857. .set_rxfh = hns3_set_rss,
  858. .get_link_ksettings = hns3_get_link_ksettings,
  859. .set_link_ksettings = hns3_set_link_ksettings,
  860. .nway_reset = hns3_nway_reset,
  861. .get_channels = hns3_get_channels,
  862. .set_channels = hns3_set_channels,
  863. .get_coalesce = hns3_get_coalesce,
  864. .set_coalesce = hns3_set_coalesce,
  865. .get_regs_len = hns3_get_regs_len,
  866. .get_regs = hns3_get_regs,
  867. .set_phys_id = hns3_set_phys_id,
  868. };
  869. void hns3_ethtool_set_ops(struct net_device *netdev)
  870. {
  871. struct hnae3_handle *h = hns3_get_handle(netdev);
  872. if (h->flags & HNAE3_SUPPORT_VF)
  873. netdev->ethtool_ops = &hns3vf_ethtool_ops;
  874. else
  875. netdev->ethtool_ops = &hns3_ethtool_ops;
  876. }