mana_ethtool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright (c) 2021, Microsoft Corporation. */
  3. #include <linux/inetdevice.h>
  4. #include <linux/etherdevice.h>
  5. #include <linux/ethtool.h>
  6. #include <net/mana/mana.h>
  7. static const struct {
  8. char name[ETH_GSTRING_LEN];
  9. u16 offset;
  10. } mana_eth_stats[] = {
  11. {"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)},
  12. {"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)},
  13. {"hc_rx_discards_no_wqe", offsetof(struct mana_ethtool_stats,
  14. hc_rx_discards_no_wqe)},
  15. {"hc_rx_err_vport_disabled", offsetof(struct mana_ethtool_stats,
  16. hc_rx_err_vport_disabled)},
  17. {"hc_rx_bytes", offsetof(struct mana_ethtool_stats, hc_rx_bytes)},
  18. {"hc_rx_ucast_pkts", offsetof(struct mana_ethtool_stats,
  19. hc_rx_ucast_pkts)},
  20. {"hc_rx_ucast_bytes", offsetof(struct mana_ethtool_stats,
  21. hc_rx_ucast_bytes)},
  22. {"hc_rx_bcast_pkts", offsetof(struct mana_ethtool_stats,
  23. hc_rx_bcast_pkts)},
  24. {"hc_rx_bcast_bytes", offsetof(struct mana_ethtool_stats,
  25. hc_rx_bcast_bytes)},
  26. {"hc_rx_mcast_pkts", offsetof(struct mana_ethtool_stats,
  27. hc_rx_mcast_pkts)},
  28. {"hc_rx_mcast_bytes", offsetof(struct mana_ethtool_stats,
  29. hc_rx_mcast_bytes)},
  30. {"hc_tx_err_gf_disabled", offsetof(struct mana_ethtool_stats,
  31. hc_tx_err_gf_disabled)},
  32. {"hc_tx_err_vport_disabled", offsetof(struct mana_ethtool_stats,
  33. hc_tx_err_vport_disabled)},
  34. {"hc_tx_err_inval_vportoffset_pkt",
  35. offsetof(struct mana_ethtool_stats,
  36. hc_tx_err_inval_vportoffset_pkt)},
  37. {"hc_tx_err_vlan_enforcement", offsetof(struct mana_ethtool_stats,
  38. hc_tx_err_vlan_enforcement)},
  39. {"hc_tx_err_eth_type_enforcement",
  40. offsetof(struct mana_ethtool_stats, hc_tx_err_eth_type_enforcement)},
  41. {"hc_tx_err_sa_enforcement", offsetof(struct mana_ethtool_stats,
  42. hc_tx_err_sa_enforcement)},
  43. {"hc_tx_err_sqpdid_enforcement",
  44. offsetof(struct mana_ethtool_stats, hc_tx_err_sqpdid_enforcement)},
  45. {"hc_tx_err_cqpdid_enforcement",
  46. offsetof(struct mana_ethtool_stats, hc_tx_err_cqpdid_enforcement)},
  47. {"hc_tx_err_mtu_violation", offsetof(struct mana_ethtool_stats,
  48. hc_tx_err_mtu_violation)},
  49. {"hc_tx_err_inval_oob", offsetof(struct mana_ethtool_stats,
  50. hc_tx_err_inval_oob)},
  51. {"hc_tx_err_gdma", offsetof(struct mana_ethtool_stats,
  52. hc_tx_err_gdma)},
  53. {"hc_tx_bytes", offsetof(struct mana_ethtool_stats, hc_tx_bytes)},
  54. {"hc_tx_ucast_pkts", offsetof(struct mana_ethtool_stats,
  55. hc_tx_ucast_pkts)},
  56. {"hc_tx_ucast_bytes", offsetof(struct mana_ethtool_stats,
  57. hc_tx_ucast_bytes)},
  58. {"hc_tx_bcast_pkts", offsetof(struct mana_ethtool_stats,
  59. hc_tx_bcast_pkts)},
  60. {"hc_tx_bcast_bytes", offsetof(struct mana_ethtool_stats,
  61. hc_tx_bcast_bytes)},
  62. {"hc_tx_mcast_pkts", offsetof(struct mana_ethtool_stats,
  63. hc_tx_mcast_pkts)},
  64. {"hc_tx_mcast_bytes", offsetof(struct mana_ethtool_stats,
  65. hc_tx_mcast_bytes)},
  66. {"tx_cq_err", offsetof(struct mana_ethtool_stats, tx_cqe_err)},
  67. {"tx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
  68. tx_cqe_unknown_type)},
  69. {"rx_coalesced_err", offsetof(struct mana_ethtool_stats,
  70. rx_coalesced_err)},
  71. {"rx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
  72. rx_cqe_unknown_type)},
  73. };
  74. static int mana_get_sset_count(struct net_device *ndev, int stringset)
  75. {
  76. struct mana_port_context *apc = netdev_priv(ndev);
  77. unsigned int num_queues = apc->num_queues;
  78. if (stringset != ETH_SS_STATS)
  79. return -EINVAL;
  80. return ARRAY_SIZE(mana_eth_stats) + num_queues *
  81. (MANA_STATS_RX_COUNT + MANA_STATS_TX_COUNT);
  82. }
  83. static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  84. {
  85. struct mana_port_context *apc = netdev_priv(ndev);
  86. unsigned int num_queues = apc->num_queues;
  87. u8 *p = data;
  88. int i;
  89. if (stringset != ETH_SS_STATS)
  90. return;
  91. for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) {
  92. memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN);
  93. p += ETH_GSTRING_LEN;
  94. }
  95. for (i = 0; i < num_queues; i++) {
  96. sprintf(p, "rx_%d_packets", i);
  97. p += ETH_GSTRING_LEN;
  98. sprintf(p, "rx_%d_bytes", i);
  99. p += ETH_GSTRING_LEN;
  100. sprintf(p, "rx_%d_xdp_drop", i);
  101. p += ETH_GSTRING_LEN;
  102. sprintf(p, "rx_%d_xdp_tx", i);
  103. p += ETH_GSTRING_LEN;
  104. sprintf(p, "rx_%d_xdp_redirect", i);
  105. p += ETH_GSTRING_LEN;
  106. }
  107. for (i = 0; i < num_queues; i++) {
  108. sprintf(p, "tx_%d_packets", i);
  109. p += ETH_GSTRING_LEN;
  110. sprintf(p, "tx_%d_bytes", i);
  111. p += ETH_GSTRING_LEN;
  112. sprintf(p, "tx_%d_xdp_xmit", i);
  113. p += ETH_GSTRING_LEN;
  114. sprintf(p, "tx_%d_tso_packets", i);
  115. p += ETH_GSTRING_LEN;
  116. sprintf(p, "tx_%d_tso_bytes", i);
  117. p += ETH_GSTRING_LEN;
  118. sprintf(p, "tx_%d_tso_inner_packets", i);
  119. p += ETH_GSTRING_LEN;
  120. sprintf(p, "tx_%d_tso_inner_bytes", i);
  121. p += ETH_GSTRING_LEN;
  122. sprintf(p, "tx_%d_long_pkt_fmt", i);
  123. p += ETH_GSTRING_LEN;
  124. sprintf(p, "tx_%d_short_pkt_fmt", i);
  125. p += ETH_GSTRING_LEN;
  126. sprintf(p, "tx_%d_csum_partial", i);
  127. p += ETH_GSTRING_LEN;
  128. sprintf(p, "tx_%d_mana_map_err", i);
  129. p += ETH_GSTRING_LEN;
  130. }
  131. }
  132. static void mana_get_ethtool_stats(struct net_device *ndev,
  133. struct ethtool_stats *e_stats, u64 *data)
  134. {
  135. struct mana_port_context *apc = netdev_priv(ndev);
  136. unsigned int num_queues = apc->num_queues;
  137. void *eth_stats = &apc->eth_stats;
  138. struct mana_stats_rx *rx_stats;
  139. struct mana_stats_tx *tx_stats;
  140. unsigned int start;
  141. u64 packets, bytes;
  142. u64 xdp_redirect;
  143. u64 xdp_xmit;
  144. u64 xdp_drop;
  145. u64 xdp_tx;
  146. u64 tso_packets;
  147. u64 tso_bytes;
  148. u64 tso_inner_packets;
  149. u64 tso_inner_bytes;
  150. u64 long_pkt_fmt;
  151. u64 short_pkt_fmt;
  152. u64 csum_partial;
  153. u64 mana_map_err;
  154. int q, i = 0;
  155. if (!apc->port_is_up)
  156. return;
  157. /* we call mana function to update stats from GDMA */
  158. mana_query_gf_stats(apc);
  159. for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
  160. data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
  161. for (q = 0; q < num_queues; q++) {
  162. rx_stats = &apc->rxqs[q]->stats;
  163. do {
  164. start = u64_stats_fetch_begin(&rx_stats->syncp);
  165. packets = rx_stats->packets;
  166. bytes = rx_stats->bytes;
  167. xdp_drop = rx_stats->xdp_drop;
  168. xdp_tx = rx_stats->xdp_tx;
  169. xdp_redirect = rx_stats->xdp_redirect;
  170. } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
  171. data[i++] = packets;
  172. data[i++] = bytes;
  173. data[i++] = xdp_drop;
  174. data[i++] = xdp_tx;
  175. data[i++] = xdp_redirect;
  176. }
  177. for (q = 0; q < num_queues; q++) {
  178. tx_stats = &apc->tx_qp[q].txq.stats;
  179. do {
  180. start = u64_stats_fetch_begin(&tx_stats->syncp);
  181. packets = tx_stats->packets;
  182. bytes = tx_stats->bytes;
  183. xdp_xmit = tx_stats->xdp_xmit;
  184. tso_packets = tx_stats->tso_packets;
  185. tso_bytes = tx_stats->tso_bytes;
  186. tso_inner_packets = tx_stats->tso_inner_packets;
  187. tso_inner_bytes = tx_stats->tso_inner_bytes;
  188. long_pkt_fmt = tx_stats->long_pkt_fmt;
  189. short_pkt_fmt = tx_stats->short_pkt_fmt;
  190. csum_partial = tx_stats->csum_partial;
  191. mana_map_err = tx_stats->mana_map_err;
  192. } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
  193. data[i++] = packets;
  194. data[i++] = bytes;
  195. data[i++] = xdp_xmit;
  196. data[i++] = tso_packets;
  197. data[i++] = tso_bytes;
  198. data[i++] = tso_inner_packets;
  199. data[i++] = tso_inner_bytes;
  200. data[i++] = long_pkt_fmt;
  201. data[i++] = short_pkt_fmt;
  202. data[i++] = csum_partial;
  203. data[i++] = mana_map_err;
  204. }
  205. }
  206. static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
  207. u32 *rules)
  208. {
  209. struct mana_port_context *apc = netdev_priv(ndev);
  210. switch (cmd->cmd) {
  211. case ETHTOOL_GRXRINGS:
  212. cmd->data = apc->num_queues;
  213. return 0;
  214. }
  215. return -EOPNOTSUPP;
  216. }
  217. static u32 mana_get_rxfh_key_size(struct net_device *ndev)
  218. {
  219. return MANA_HASH_KEY_SIZE;
  220. }
  221. static u32 mana_rss_indir_size(struct net_device *ndev)
  222. {
  223. struct mana_port_context *apc = netdev_priv(ndev);
  224. return apc->indir_table_sz;
  225. }
  226. static int mana_get_rxfh(struct net_device *ndev,
  227. struct ethtool_rxfh_param *rxfh)
  228. {
  229. struct mana_port_context *apc = netdev_priv(ndev);
  230. int i;
  231. rxfh->hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  232. if (rxfh->indir) {
  233. for (i = 0; i < apc->indir_table_sz; i++)
  234. rxfh->indir[i] = apc->indir_table[i];
  235. }
  236. if (rxfh->key)
  237. memcpy(rxfh->key, apc->hashkey, MANA_HASH_KEY_SIZE);
  238. return 0;
  239. }
  240. static int mana_set_rxfh(struct net_device *ndev,
  241. struct ethtool_rxfh_param *rxfh,
  242. struct netlink_ext_ack *extack)
  243. {
  244. struct mana_port_context *apc = netdev_priv(ndev);
  245. bool update_hash = false, update_table = false;
  246. u8 save_key[MANA_HASH_KEY_SIZE];
  247. u32 *save_table;
  248. int i, err;
  249. if (!apc->port_is_up)
  250. return -EOPNOTSUPP;
  251. if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
  252. rxfh->hfunc != ETH_RSS_HASH_TOP)
  253. return -EOPNOTSUPP;
  254. save_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
  255. if (!save_table)
  256. return -ENOMEM;
  257. if (rxfh->indir) {
  258. for (i = 0; i < apc->indir_table_sz; i++)
  259. if (rxfh->indir[i] >= apc->num_queues) {
  260. err = -EINVAL;
  261. goto cleanup;
  262. }
  263. update_table = true;
  264. for (i = 0; i < apc->indir_table_sz; i++) {
  265. save_table[i] = apc->indir_table[i];
  266. apc->indir_table[i] = rxfh->indir[i];
  267. }
  268. }
  269. if (rxfh->key) {
  270. update_hash = true;
  271. memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
  272. memcpy(apc->hashkey, rxfh->key, MANA_HASH_KEY_SIZE);
  273. }
  274. err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
  275. if (err) { /* recover to original values */
  276. if (update_table) {
  277. for (i = 0; i < apc->indir_table_sz; i++)
  278. apc->indir_table[i] = save_table[i];
  279. }
  280. if (update_hash)
  281. memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
  282. mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
  283. }
  284. cleanup:
  285. kfree(save_table);
  286. return err;
  287. }
  288. static void mana_get_channels(struct net_device *ndev,
  289. struct ethtool_channels *channel)
  290. {
  291. struct mana_port_context *apc = netdev_priv(ndev);
  292. channel->max_combined = apc->max_queues;
  293. channel->combined_count = apc->num_queues;
  294. }
  295. static int mana_set_channels(struct net_device *ndev,
  296. struct ethtool_channels *channels)
  297. {
  298. struct mana_port_context *apc = netdev_priv(ndev);
  299. unsigned int new_count = channels->combined_count;
  300. unsigned int old_count = apc->num_queues;
  301. int err;
  302. err = mana_pre_alloc_rxbufs(apc, ndev->mtu, new_count);
  303. if (err) {
  304. netdev_err(ndev, "Insufficient memory for new allocations");
  305. return err;
  306. }
  307. err = mana_detach(ndev, false);
  308. if (err) {
  309. netdev_err(ndev, "mana_detach failed: %d\n", err);
  310. goto out;
  311. }
  312. apc->num_queues = new_count;
  313. err = mana_attach(ndev);
  314. if (err) {
  315. apc->num_queues = old_count;
  316. netdev_err(ndev, "mana_attach failed: %d\n", err);
  317. }
  318. out:
  319. mana_pre_dealloc_rxbufs(apc);
  320. return err;
  321. }
  322. static void mana_get_ringparam(struct net_device *ndev,
  323. struct ethtool_ringparam *ring,
  324. struct kernel_ethtool_ringparam *kernel_ring,
  325. struct netlink_ext_ack *extack)
  326. {
  327. struct mana_port_context *apc = netdev_priv(ndev);
  328. ring->rx_pending = apc->rx_queue_size;
  329. ring->tx_pending = apc->tx_queue_size;
  330. ring->rx_max_pending = MAX_RX_BUFFERS_PER_QUEUE;
  331. ring->tx_max_pending = MAX_TX_BUFFERS_PER_QUEUE;
  332. }
  333. static int mana_set_ringparam(struct net_device *ndev,
  334. struct ethtool_ringparam *ring,
  335. struct kernel_ethtool_ringparam *kernel_ring,
  336. struct netlink_ext_ack *extack)
  337. {
  338. struct mana_port_context *apc = netdev_priv(ndev);
  339. u32 new_tx, new_rx;
  340. u32 old_tx, old_rx;
  341. int err;
  342. old_tx = apc->tx_queue_size;
  343. old_rx = apc->rx_queue_size;
  344. if (ring->tx_pending < MIN_TX_BUFFERS_PER_QUEUE) {
  345. NL_SET_ERR_MSG_FMT(extack, "tx:%d less than the min:%d", ring->tx_pending,
  346. MIN_TX_BUFFERS_PER_QUEUE);
  347. return -EINVAL;
  348. }
  349. if (ring->rx_pending < MIN_RX_BUFFERS_PER_QUEUE) {
  350. NL_SET_ERR_MSG_FMT(extack, "rx:%d less than the min:%d", ring->rx_pending,
  351. MIN_RX_BUFFERS_PER_QUEUE);
  352. return -EINVAL;
  353. }
  354. new_rx = roundup_pow_of_two(ring->rx_pending);
  355. new_tx = roundup_pow_of_two(ring->tx_pending);
  356. netdev_info(ndev, "Using nearest power of 2 values for Txq:%d Rxq:%d\n",
  357. new_tx, new_rx);
  358. /* pre-allocating new buffers to prevent failures in mana_attach() later */
  359. apc->rx_queue_size = new_rx;
  360. err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
  361. apc->rx_queue_size = old_rx;
  362. if (err) {
  363. netdev_err(ndev, "Insufficient memory for new allocations\n");
  364. return err;
  365. }
  366. err = mana_detach(ndev, false);
  367. if (err) {
  368. netdev_err(ndev, "mana_detach failed: %d\n", err);
  369. goto out;
  370. }
  371. apc->tx_queue_size = new_tx;
  372. apc->rx_queue_size = new_rx;
  373. err = mana_attach(ndev);
  374. if (err) {
  375. netdev_err(ndev, "mana_attach failed: %d\n", err);
  376. apc->tx_queue_size = old_tx;
  377. apc->rx_queue_size = old_rx;
  378. }
  379. out:
  380. mana_pre_dealloc_rxbufs(apc);
  381. return err;
  382. }
  383. const struct ethtool_ops mana_ethtool_ops = {
  384. .get_ethtool_stats = mana_get_ethtool_stats,
  385. .get_sset_count = mana_get_sset_count,
  386. .get_strings = mana_get_strings,
  387. .get_rxnfc = mana_get_rxnfc,
  388. .get_rxfh_key_size = mana_get_rxfh_key_size,
  389. .get_rxfh_indir_size = mana_rss_indir_size,
  390. .get_rxfh = mana_get_rxfh,
  391. .set_rxfh = mana_set_rxfh,
  392. .get_channels = mana_get_channels,
  393. .set_channels = mana_set_channels,
  394. .get_ringparam = mana_get_ringparam,
  395. .set_ringparam = mana_set_ringparam,
  396. };