nfp_net_ethtool.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * Copyright (C) 2015-2017 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * nfp_net_ethtool.c
  35. * Netronome network device driver: ethtool support
  36. * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
  37. * Jason McMullan <jason.mcmullan@netronome.com>
  38. * Rolf Neugebauer <rolf.neugebauer@netronome.com>
  39. * Brad Petrus <brad.petrus@netronome.com>
  40. */
  41. #include <linux/bitfield.h>
  42. #include <linux/kernel.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/pci.h>
  47. #include <linux/ethtool.h>
  48. #include "nfpcore/nfp.h"
  49. #include "nfpcore/nfp_nsp.h"
  50. #include "nfp_net_ctrl.h"
  51. #include "nfp_net.h"
  52. enum nfp_dump_diag {
  53. NFP_DUMP_NSP_DIAG = 0,
  54. };
  55. /* Support for stats. Returns netdev, driver, and device stats */
  56. enum { NETDEV_ET_STATS, NFP_NET_DRV_ET_STATS, NFP_NET_DEV_ET_STATS };
  57. struct _nfp_net_et_stats {
  58. char name[ETH_GSTRING_LEN];
  59. int type;
  60. int sz;
  61. int off;
  62. };
  63. #define NN_ET_NETDEV_STAT(m) NETDEV_ET_STATS, \
  64. FIELD_SIZEOF(struct net_device_stats, m), \
  65. offsetof(struct net_device_stats, m)
  66. /* For stats in the control BAR (other than Q stats) */
  67. #define NN_ET_DEV_STAT(m) NFP_NET_DEV_ET_STATS, \
  68. sizeof(u64), \
  69. (m)
  70. static const struct _nfp_net_et_stats nfp_net_et_stats[] = {
  71. /* netdev stats */
  72. {"rx_packets", NN_ET_NETDEV_STAT(rx_packets)},
  73. {"tx_packets", NN_ET_NETDEV_STAT(tx_packets)},
  74. {"rx_bytes", NN_ET_NETDEV_STAT(rx_bytes)},
  75. {"tx_bytes", NN_ET_NETDEV_STAT(tx_bytes)},
  76. {"rx_errors", NN_ET_NETDEV_STAT(rx_errors)},
  77. {"tx_errors", NN_ET_NETDEV_STAT(tx_errors)},
  78. {"rx_dropped", NN_ET_NETDEV_STAT(rx_dropped)},
  79. {"tx_dropped", NN_ET_NETDEV_STAT(tx_dropped)},
  80. {"multicast", NN_ET_NETDEV_STAT(multicast)},
  81. {"collisions", NN_ET_NETDEV_STAT(collisions)},
  82. {"rx_over_errors", NN_ET_NETDEV_STAT(rx_over_errors)},
  83. {"rx_crc_errors", NN_ET_NETDEV_STAT(rx_crc_errors)},
  84. {"rx_frame_errors", NN_ET_NETDEV_STAT(rx_frame_errors)},
  85. {"rx_fifo_errors", NN_ET_NETDEV_STAT(rx_fifo_errors)},
  86. {"rx_missed_errors", NN_ET_NETDEV_STAT(rx_missed_errors)},
  87. {"tx_aborted_errors", NN_ET_NETDEV_STAT(tx_aborted_errors)},
  88. {"tx_carrier_errors", NN_ET_NETDEV_STAT(tx_carrier_errors)},
  89. {"tx_fifo_errors", NN_ET_NETDEV_STAT(tx_fifo_errors)},
  90. /* Stats from the device */
  91. {"dev_rx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_DISCARDS)},
  92. {"dev_rx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_ERRORS)},
  93. {"dev_rx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_OCTETS)},
  94. {"dev_rx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_UC_OCTETS)},
  95. {"dev_rx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_OCTETS)},
  96. {"dev_rx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_OCTETS)},
  97. {"dev_rx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_FRAMES)},
  98. {"dev_rx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_FRAMES)},
  99. {"dev_rx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_FRAMES)},
  100. {"dev_tx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_DISCARDS)},
  101. {"dev_tx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_ERRORS)},
  102. {"dev_tx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_OCTETS)},
  103. {"dev_tx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_UC_OCTETS)},
  104. {"dev_tx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_OCTETS)},
  105. {"dev_tx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_OCTETS)},
  106. {"dev_tx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_FRAMES)},
  107. {"dev_tx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_FRAMES)},
  108. {"dev_tx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_FRAMES)},
  109. {"bpf_pass_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_FRAMES)},
  110. {"bpf_pass_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_BYTES)},
  111. /* see comments in outro functions in nfp_bpf_jit.c to find out
  112. * how different BPF modes use app-specific counters
  113. */
  114. {"bpf_app1_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_FRAMES)},
  115. {"bpf_app1_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_BYTES)},
  116. {"bpf_app2_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_FRAMES)},
  117. {"bpf_app2_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_BYTES)},
  118. {"bpf_app3_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_FRAMES)},
  119. {"bpf_app3_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_BYTES)},
  120. };
  121. #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
  122. #define NN_ET_RVEC_STATS_LEN (nn->dp.num_r_vecs * 3)
  123. #define NN_ET_RVEC_GATHER_STATS 7
  124. #define NN_ET_QUEUE_STATS_LEN ((nn->dp.num_tx_rings + nn->dp.num_rx_rings) * 2)
  125. #define NN_ET_STATS_LEN (NN_ET_GLOBAL_STATS_LEN + NN_ET_RVEC_GATHER_STATS + \
  126. NN_ET_RVEC_STATS_LEN + NN_ET_QUEUE_STATS_LEN)
  127. static void nfp_net_get_nspinfo(struct nfp_net *nn, char *version)
  128. {
  129. struct nfp_nsp *nsp;
  130. if (!nn->cpp)
  131. return;
  132. nsp = nfp_nsp_open(nn->cpp);
  133. if (IS_ERR(nsp))
  134. return;
  135. snprintf(version, ETHTOOL_FWVERS_LEN, "sp:%hu.%hu",
  136. nfp_nsp_get_abi_ver_major(nsp),
  137. nfp_nsp_get_abi_ver_minor(nsp));
  138. nfp_nsp_close(nsp);
  139. }
  140. static void nfp_net_get_drvinfo(struct net_device *netdev,
  141. struct ethtool_drvinfo *drvinfo)
  142. {
  143. char nsp_version[ETHTOOL_FWVERS_LEN] = {};
  144. struct nfp_net *nn = netdev_priv(netdev);
  145. strlcpy(drvinfo->driver, nn->pdev->driver->name,
  146. sizeof(drvinfo->driver));
  147. strlcpy(drvinfo->version, nfp_driver_version, sizeof(drvinfo->version));
  148. nfp_net_get_nspinfo(nn, nsp_version);
  149. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  150. "%d.%d.%d.%d %s",
  151. nn->fw_ver.resv, nn->fw_ver.class,
  152. nn->fw_ver.major, nn->fw_ver.minor, nsp_version);
  153. strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
  154. sizeof(drvinfo->bus_info));
  155. drvinfo->n_stats = NN_ET_STATS_LEN;
  156. drvinfo->regdump_len = NFP_NET_CFG_BAR_SZ;
  157. }
  158. /**
  159. * nfp_net_get_link_ksettings - Get Link Speed settings
  160. * @netdev: network interface device structure
  161. * @cmd: ethtool command
  162. *
  163. * Reports speed settings based on info in the BAR provided by the fw.
  164. */
  165. static int
  166. nfp_net_get_link_ksettings(struct net_device *netdev,
  167. struct ethtool_link_ksettings *cmd)
  168. {
  169. static const u32 ls_to_ethtool[] = {
  170. [NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED] = 0,
  171. [NFP_NET_CFG_STS_LINK_RATE_UNKNOWN] = SPEED_UNKNOWN,
  172. [NFP_NET_CFG_STS_LINK_RATE_1G] = SPEED_1000,
  173. [NFP_NET_CFG_STS_LINK_RATE_10G] = SPEED_10000,
  174. [NFP_NET_CFG_STS_LINK_RATE_25G] = SPEED_25000,
  175. [NFP_NET_CFG_STS_LINK_RATE_40G] = SPEED_40000,
  176. [NFP_NET_CFG_STS_LINK_RATE_50G] = SPEED_50000,
  177. [NFP_NET_CFG_STS_LINK_RATE_100G] = SPEED_100000,
  178. };
  179. struct nfp_net *nn = netdev_priv(netdev);
  180. u32 sts, ls;
  181. ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
  182. cmd->base.port = PORT_OTHER;
  183. cmd->base.speed = SPEED_UNKNOWN;
  184. cmd->base.duplex = DUPLEX_UNKNOWN;
  185. if (nn->eth_port)
  186. cmd->base.autoneg = nn->eth_port->aneg != NFP_ANEG_DISABLED ?
  187. AUTONEG_ENABLE : AUTONEG_DISABLE;
  188. if (!netif_carrier_ok(netdev))
  189. return 0;
  190. /* Use link speed from ETH table if available, otherwise try the BAR */
  191. if (nn->eth_port) {
  192. int err;
  193. if (nfp_net_link_changed_read_clear(nn)) {
  194. err = nfp_net_refresh_eth_port(nn);
  195. if (err)
  196. return err;
  197. }
  198. cmd->base.port = nn->eth_port->port_type;
  199. cmd->base.speed = nn->eth_port->speed;
  200. cmd->base.duplex = DUPLEX_FULL;
  201. return 0;
  202. }
  203. sts = nn_readl(nn, NFP_NET_CFG_STS);
  204. ls = FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts);
  205. if (ls == NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED)
  206. return -EOPNOTSUPP;
  207. if (ls == NFP_NET_CFG_STS_LINK_RATE_UNKNOWN ||
  208. ls >= ARRAY_SIZE(ls_to_ethtool))
  209. return 0;
  210. cmd->base.speed = ls_to_ethtool[sts];
  211. cmd->base.duplex = DUPLEX_FULL;
  212. return 0;
  213. }
  214. static int
  215. nfp_net_set_link_ksettings(struct net_device *netdev,
  216. const struct ethtool_link_ksettings *cmd)
  217. {
  218. struct nfp_net *nn = netdev_priv(netdev);
  219. struct nfp_nsp *nsp;
  220. int err;
  221. if (!nn->eth_port)
  222. return -EOPNOTSUPP;
  223. if (netif_running(netdev)) {
  224. nn_warn(nn, "Changing settings not allowed on an active interface. It may cause the port to be disabled until reboot.\n");
  225. return -EBUSY;
  226. }
  227. nsp = nfp_eth_config_start(nn->cpp, nn->eth_port->index);
  228. if (IS_ERR(nsp))
  229. return PTR_ERR(nsp);
  230. err = __nfp_eth_set_aneg(nsp, cmd->base.autoneg == AUTONEG_ENABLE ?
  231. NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
  232. if (err)
  233. goto err_bad_set;
  234. if (cmd->base.speed != SPEED_UNKNOWN) {
  235. u32 speed = cmd->base.speed / nn->eth_port->lanes;
  236. err = __nfp_eth_set_speed(nsp, speed);
  237. if (err)
  238. goto err_bad_set;
  239. }
  240. err = nfp_eth_config_commit_end(nsp);
  241. if (err > 0)
  242. return 0; /* no change */
  243. nfp_net_refresh_port_table(nn);
  244. return err;
  245. err_bad_set:
  246. nfp_eth_config_cleanup_end(nsp);
  247. return err;
  248. }
  249. static void nfp_net_get_ringparam(struct net_device *netdev,
  250. struct ethtool_ringparam *ring)
  251. {
  252. struct nfp_net *nn = netdev_priv(netdev);
  253. ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
  254. ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
  255. ring->rx_pending = nn->dp.rxd_cnt;
  256. ring->tx_pending = nn->dp.txd_cnt;
  257. }
  258. static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
  259. {
  260. struct nfp_net_dp *dp;
  261. dp = nfp_net_clone_dp(nn);
  262. if (!dp)
  263. return -ENOMEM;
  264. dp->rxd_cnt = rxd_cnt;
  265. dp->txd_cnt = txd_cnt;
  266. return nfp_net_ring_reconfig(nn, dp, NULL);
  267. }
  268. static int nfp_net_set_ringparam(struct net_device *netdev,
  269. struct ethtool_ringparam *ring)
  270. {
  271. struct nfp_net *nn = netdev_priv(netdev);
  272. u32 rxd_cnt, txd_cnt;
  273. /* We don't have separate queues/rings for small/large frames. */
  274. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  275. return -EINVAL;
  276. /* Round up to supported values */
  277. rxd_cnt = roundup_pow_of_two(ring->rx_pending);
  278. txd_cnt = roundup_pow_of_two(ring->tx_pending);
  279. if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
  280. txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
  281. return -EINVAL;
  282. if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
  283. return 0;
  284. nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
  285. nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
  286. return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
  287. }
  288. static void nfp_net_get_strings(struct net_device *netdev,
  289. u32 stringset, u8 *data)
  290. {
  291. struct nfp_net *nn = netdev_priv(netdev);
  292. u8 *p = data;
  293. int i;
  294. switch (stringset) {
  295. case ETH_SS_STATS:
  296. for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
  297. memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
  298. p += ETH_GSTRING_LEN;
  299. }
  300. for (i = 0; i < nn->dp.num_r_vecs; i++) {
  301. sprintf(p, "rvec_%u_rx_pkts", i);
  302. p += ETH_GSTRING_LEN;
  303. sprintf(p, "rvec_%u_tx_pkts", i);
  304. p += ETH_GSTRING_LEN;
  305. sprintf(p, "rvec_%u_tx_busy", i);
  306. p += ETH_GSTRING_LEN;
  307. }
  308. strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
  309. p += ETH_GSTRING_LEN;
  310. strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
  311. p += ETH_GSTRING_LEN;
  312. strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
  313. p += ETH_GSTRING_LEN;
  314. strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
  315. p += ETH_GSTRING_LEN;
  316. strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
  317. p += ETH_GSTRING_LEN;
  318. strncpy(p, "tx_gather", ETH_GSTRING_LEN);
  319. p += ETH_GSTRING_LEN;
  320. strncpy(p, "tx_lso", ETH_GSTRING_LEN);
  321. p += ETH_GSTRING_LEN;
  322. for (i = 0; i < nn->dp.num_tx_rings; i++) {
  323. sprintf(p, "txq_%u_pkts", i);
  324. p += ETH_GSTRING_LEN;
  325. sprintf(p, "txq_%u_bytes", i);
  326. p += ETH_GSTRING_LEN;
  327. }
  328. for (i = 0; i < nn->dp.num_rx_rings; i++) {
  329. sprintf(p, "rxq_%u_pkts", i);
  330. p += ETH_GSTRING_LEN;
  331. sprintf(p, "rxq_%u_bytes", i);
  332. p += ETH_GSTRING_LEN;
  333. }
  334. break;
  335. }
  336. }
  337. static void nfp_net_get_stats(struct net_device *netdev,
  338. struct ethtool_stats *stats, u64 *data)
  339. {
  340. u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
  341. struct nfp_net *nn = netdev_priv(netdev);
  342. struct rtnl_link_stats64 *netdev_stats;
  343. struct rtnl_link_stats64 temp = {};
  344. u64 tmp[NN_ET_RVEC_GATHER_STATS];
  345. u8 __iomem *io_p;
  346. int i, j, k;
  347. u8 *p;
  348. netdev_stats = dev_get_stats(netdev, &temp);
  349. for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
  350. switch (nfp_net_et_stats[i].type) {
  351. case NETDEV_ET_STATS:
  352. p = (char *)netdev_stats + nfp_net_et_stats[i].off;
  353. data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
  354. *(u64 *)p : *(u32 *)p;
  355. break;
  356. case NFP_NET_DEV_ET_STATS:
  357. io_p = nn->dp.ctrl_bar + nfp_net_et_stats[i].off;
  358. data[i] = readq(io_p);
  359. break;
  360. }
  361. }
  362. for (j = 0; j < nn->dp.num_r_vecs; j++) {
  363. unsigned int start;
  364. do {
  365. start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
  366. data[i++] = nn->r_vecs[j].rx_pkts;
  367. tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
  368. tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
  369. tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
  370. } while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
  371. do {
  372. start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
  373. data[i++] = nn->r_vecs[j].tx_pkts;
  374. data[i++] = nn->r_vecs[j].tx_busy;
  375. tmp[3] = nn->r_vecs[j].hw_csum_tx;
  376. tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
  377. tmp[5] = nn->r_vecs[j].tx_gather;
  378. tmp[6] = nn->r_vecs[j].tx_lso;
  379. } while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
  380. for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
  381. gathered_stats[k] += tmp[k];
  382. }
  383. for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
  384. data[i++] = gathered_stats[j];
  385. for (j = 0; j < nn->dp.num_tx_rings; j++) {
  386. io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
  387. data[i++] = readq(io_p);
  388. io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
  389. data[i++] = readq(io_p);
  390. }
  391. for (j = 0; j < nn->dp.num_rx_rings; j++) {
  392. io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
  393. data[i++] = readq(io_p);
  394. io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
  395. data[i++] = readq(io_p);
  396. }
  397. }
  398. static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
  399. {
  400. struct nfp_net *nn = netdev_priv(netdev);
  401. switch (sset) {
  402. case ETH_SS_STATS:
  403. return NN_ET_STATS_LEN;
  404. default:
  405. return -EOPNOTSUPP;
  406. }
  407. }
  408. /* RX network flow classification (RSS, filters, etc)
  409. */
  410. static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
  411. {
  412. static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
  413. [TCP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_TCP,
  414. [TCP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_TCP,
  415. [UDP_V4_FLOW] = NFP_NET_CFG_RSS_IPV4_UDP,
  416. [UDP_V6_FLOW] = NFP_NET_CFG_RSS_IPV6_UDP,
  417. [IPV4_FLOW] = NFP_NET_CFG_RSS_IPV4,
  418. [IPV6_FLOW] = NFP_NET_CFG_RSS_IPV6,
  419. };
  420. if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
  421. return 0;
  422. return xlate_ethtool_to_nfp[flow_type];
  423. }
  424. static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
  425. struct ethtool_rxnfc *cmd)
  426. {
  427. u32 nfp_rss_flag;
  428. cmd->data = 0;
  429. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
  430. return -EOPNOTSUPP;
  431. nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
  432. if (!nfp_rss_flag)
  433. return -EINVAL;
  434. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  435. if (nn->rss_cfg & nfp_rss_flag)
  436. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  437. return 0;
  438. }
  439. static int nfp_net_get_rxnfc(struct net_device *netdev,
  440. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  441. {
  442. struct nfp_net *nn = netdev_priv(netdev);
  443. switch (cmd->cmd) {
  444. case ETHTOOL_GRXRINGS:
  445. cmd->data = nn->dp.num_rx_rings;
  446. return 0;
  447. case ETHTOOL_GRXFH:
  448. return nfp_net_get_rss_hash_opts(nn, cmd);
  449. default:
  450. return -EOPNOTSUPP;
  451. }
  452. }
  453. static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
  454. struct ethtool_rxnfc *nfc)
  455. {
  456. u32 new_rss_cfg = nn->rss_cfg;
  457. u32 nfp_rss_flag;
  458. int err;
  459. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
  460. return -EOPNOTSUPP;
  461. /* RSS only supports IP SA/DA and L4 src/dst ports */
  462. if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
  463. RXH_L4_B_0_1 | RXH_L4_B_2_3))
  464. return -EINVAL;
  465. /* We need at least the IP SA/DA fields for hashing */
  466. if (!(nfc->data & RXH_IP_SRC) ||
  467. !(nfc->data & RXH_IP_DST))
  468. return -EINVAL;
  469. nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
  470. if (!nfp_rss_flag)
  471. return -EINVAL;
  472. switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  473. case 0:
  474. new_rss_cfg &= ~nfp_rss_flag;
  475. break;
  476. case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
  477. new_rss_cfg |= nfp_rss_flag;
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
  483. new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
  484. if (new_rss_cfg == nn->rss_cfg)
  485. return 0;
  486. writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
  487. err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
  488. if (err)
  489. return err;
  490. nn->rss_cfg = new_rss_cfg;
  491. nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
  492. return 0;
  493. }
  494. static int nfp_net_set_rxnfc(struct net_device *netdev,
  495. struct ethtool_rxnfc *cmd)
  496. {
  497. struct nfp_net *nn = netdev_priv(netdev);
  498. switch (cmd->cmd) {
  499. case ETHTOOL_SRXFH:
  500. return nfp_net_set_rss_hash_opt(nn, cmd);
  501. default:
  502. return -EOPNOTSUPP;
  503. }
  504. }
  505. static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
  506. {
  507. struct nfp_net *nn = netdev_priv(netdev);
  508. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
  509. return 0;
  510. return ARRAY_SIZE(nn->rss_itbl);
  511. }
  512. static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
  513. {
  514. struct nfp_net *nn = netdev_priv(netdev);
  515. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
  516. return -EOPNOTSUPP;
  517. return nfp_net_rss_key_sz(nn);
  518. }
  519. static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
  520. u8 *hfunc)
  521. {
  522. struct nfp_net *nn = netdev_priv(netdev);
  523. int i;
  524. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
  525. return -EOPNOTSUPP;
  526. if (indir)
  527. for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
  528. indir[i] = nn->rss_itbl[i];
  529. if (key)
  530. memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
  531. if (hfunc) {
  532. *hfunc = nn->rss_hfunc;
  533. if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
  534. *hfunc = ETH_RSS_HASH_UNKNOWN;
  535. }
  536. return 0;
  537. }
  538. static int nfp_net_set_rxfh(struct net_device *netdev,
  539. const u32 *indir, const u8 *key,
  540. const u8 hfunc)
  541. {
  542. struct nfp_net *nn = netdev_priv(netdev);
  543. int i;
  544. if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
  545. !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
  546. return -EOPNOTSUPP;
  547. if (!key && !indir)
  548. return 0;
  549. if (key) {
  550. memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
  551. nfp_net_rss_write_key(nn);
  552. }
  553. if (indir) {
  554. for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
  555. nn->rss_itbl[i] = indir[i];
  556. nfp_net_rss_write_itbl(nn);
  557. }
  558. return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
  559. }
  560. /* Dump BAR registers
  561. */
  562. static int nfp_net_get_regs_len(struct net_device *netdev)
  563. {
  564. return NFP_NET_CFG_BAR_SZ;
  565. }
  566. static void nfp_net_get_regs(struct net_device *netdev,
  567. struct ethtool_regs *regs, void *p)
  568. {
  569. struct nfp_net *nn = netdev_priv(netdev);
  570. u32 *regs_buf = p;
  571. int i;
  572. regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
  573. for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
  574. regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
  575. }
  576. static int nfp_net_get_coalesce(struct net_device *netdev,
  577. struct ethtool_coalesce *ec)
  578. {
  579. struct nfp_net *nn = netdev_priv(netdev);
  580. if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
  581. return -EINVAL;
  582. ec->rx_coalesce_usecs = nn->rx_coalesce_usecs;
  583. ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
  584. ec->tx_coalesce_usecs = nn->tx_coalesce_usecs;
  585. ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
  586. return 0;
  587. }
  588. /* Other debug dumps
  589. */
  590. static int
  591. nfp_dump_nsp_diag(struct nfp_net *nn, struct ethtool_dump *dump, void *buffer)
  592. {
  593. struct nfp_resource *res;
  594. int ret;
  595. if (!nn->cpp)
  596. return -EOPNOTSUPP;
  597. dump->version = 1;
  598. dump->flag = NFP_DUMP_NSP_DIAG;
  599. res = nfp_resource_acquire(nn->cpp, NFP_RESOURCE_NSP_DIAG);
  600. if (IS_ERR(res))
  601. return PTR_ERR(res);
  602. if (buffer) {
  603. if (dump->len != nfp_resource_size(res)) {
  604. ret = -EINVAL;
  605. goto exit_release;
  606. }
  607. ret = nfp_cpp_read(nn->cpp, nfp_resource_cpp_id(res),
  608. nfp_resource_address(res),
  609. buffer, dump->len);
  610. if (ret != dump->len)
  611. ret = ret < 0 ? ret : -EIO;
  612. else
  613. ret = 0;
  614. } else {
  615. dump->len = nfp_resource_size(res);
  616. ret = 0;
  617. }
  618. exit_release:
  619. nfp_resource_release(res);
  620. return ret;
  621. }
  622. static int nfp_net_set_dump(struct net_device *netdev, struct ethtool_dump *val)
  623. {
  624. struct nfp_net *nn = netdev_priv(netdev);
  625. if (!nn->cpp)
  626. return -EOPNOTSUPP;
  627. if (val->flag != NFP_DUMP_NSP_DIAG)
  628. return -EINVAL;
  629. nn->ethtool_dump_flag = val->flag;
  630. return 0;
  631. }
  632. static int
  633. nfp_net_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
  634. {
  635. return nfp_dump_nsp_diag(netdev_priv(netdev), dump, NULL);
  636. }
  637. static int
  638. nfp_net_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
  639. void *buffer)
  640. {
  641. return nfp_dump_nsp_diag(netdev_priv(netdev), dump, buffer);
  642. }
  643. static int nfp_net_set_coalesce(struct net_device *netdev,
  644. struct ethtool_coalesce *ec)
  645. {
  646. struct nfp_net *nn = netdev_priv(netdev);
  647. unsigned int factor;
  648. if (ec->rx_coalesce_usecs_irq ||
  649. ec->rx_max_coalesced_frames_irq ||
  650. ec->tx_coalesce_usecs_irq ||
  651. ec->tx_max_coalesced_frames_irq ||
  652. ec->stats_block_coalesce_usecs ||
  653. ec->use_adaptive_rx_coalesce ||
  654. ec->use_adaptive_tx_coalesce ||
  655. ec->pkt_rate_low ||
  656. ec->rx_coalesce_usecs_low ||
  657. ec->rx_max_coalesced_frames_low ||
  658. ec->tx_coalesce_usecs_low ||
  659. ec->tx_max_coalesced_frames_low ||
  660. ec->pkt_rate_high ||
  661. ec->rx_coalesce_usecs_high ||
  662. ec->rx_max_coalesced_frames_high ||
  663. ec->tx_coalesce_usecs_high ||
  664. ec->tx_max_coalesced_frames_high ||
  665. ec->rate_sample_interval)
  666. return -EOPNOTSUPP;
  667. /* Compute factor used to convert coalesce '_usecs' parameters to
  668. * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
  669. * count.
  670. */
  671. factor = nn->me_freq_mhz / 16;
  672. /* Each pair of (usecs, max_frames) fields specifies that interrupts
  673. * should be coalesced until
  674. * (usecs > 0 && time_since_first_completion >= usecs) ||
  675. * (max_frames > 0 && completed_frames >= max_frames)
  676. *
  677. * It is illegal to set both usecs and max_frames to zero as this would
  678. * cause interrupts to never be generated. To disable coalescing, set
  679. * usecs = 0 and max_frames = 1.
  680. *
  681. * Some implementations ignore the value of max_frames and use the
  682. * condition time_since_first_completion >= usecs
  683. */
  684. if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
  685. return -EINVAL;
  686. /* ensure valid configuration */
  687. if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
  688. return -EINVAL;
  689. if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
  690. return -EINVAL;
  691. if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
  692. return -EINVAL;
  693. if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
  694. return -EINVAL;
  695. if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
  696. return -EINVAL;
  697. if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
  698. return -EINVAL;
  699. /* configuration is valid */
  700. nn->rx_coalesce_usecs = ec->rx_coalesce_usecs;
  701. nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
  702. nn->tx_coalesce_usecs = ec->tx_coalesce_usecs;
  703. nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
  704. /* write configuration to device */
  705. nfp_net_coalesce_write_cfg(nn);
  706. return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
  707. }
  708. static void nfp_net_get_channels(struct net_device *netdev,
  709. struct ethtool_channels *channel)
  710. {
  711. struct nfp_net *nn = netdev_priv(netdev);
  712. unsigned int num_tx_rings;
  713. num_tx_rings = nn->dp.num_tx_rings;
  714. if (nn->dp.xdp_prog)
  715. num_tx_rings -= nn->dp.num_rx_rings;
  716. channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
  717. channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
  718. channel->max_combined = min(channel->max_rx, channel->max_tx);
  719. channel->max_other = NFP_NET_NON_Q_VECTORS;
  720. channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
  721. channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
  722. channel->tx_count = num_tx_rings - channel->combined_count;
  723. channel->other_count = NFP_NET_NON_Q_VECTORS;
  724. }
  725. static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
  726. unsigned int total_tx)
  727. {
  728. struct nfp_net_dp *dp;
  729. dp = nfp_net_clone_dp(nn);
  730. if (!dp)
  731. return -ENOMEM;
  732. dp->num_rx_rings = total_rx;
  733. dp->num_tx_rings = total_tx;
  734. /* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
  735. if (dp->xdp_prog)
  736. dp->num_tx_rings += total_rx;
  737. return nfp_net_ring_reconfig(nn, dp, NULL);
  738. }
  739. static int nfp_net_set_channels(struct net_device *netdev,
  740. struct ethtool_channels *channel)
  741. {
  742. struct nfp_net *nn = netdev_priv(netdev);
  743. unsigned int total_rx, total_tx;
  744. /* Reject unsupported */
  745. if (!channel->combined_count ||
  746. channel->other_count != NFP_NET_NON_Q_VECTORS ||
  747. (channel->rx_count && channel->tx_count))
  748. return -EINVAL;
  749. total_rx = channel->combined_count + channel->rx_count;
  750. total_tx = channel->combined_count + channel->tx_count;
  751. if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
  752. total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
  753. return -EINVAL;
  754. return nfp_net_set_num_rings(nn, total_rx, total_tx);
  755. }
  756. static const struct ethtool_ops nfp_net_ethtool_ops = {
  757. .get_drvinfo = nfp_net_get_drvinfo,
  758. .get_link = ethtool_op_get_link,
  759. .get_ringparam = nfp_net_get_ringparam,
  760. .set_ringparam = nfp_net_set_ringparam,
  761. .get_strings = nfp_net_get_strings,
  762. .get_ethtool_stats = nfp_net_get_stats,
  763. .get_sset_count = nfp_net_get_sset_count,
  764. .get_rxnfc = nfp_net_get_rxnfc,
  765. .set_rxnfc = nfp_net_set_rxnfc,
  766. .get_rxfh_indir_size = nfp_net_get_rxfh_indir_size,
  767. .get_rxfh_key_size = nfp_net_get_rxfh_key_size,
  768. .get_rxfh = nfp_net_get_rxfh,
  769. .set_rxfh = nfp_net_set_rxfh,
  770. .get_regs_len = nfp_net_get_regs_len,
  771. .get_regs = nfp_net_get_regs,
  772. .set_dump = nfp_net_set_dump,
  773. .get_dump_flag = nfp_net_get_dump_flag,
  774. .get_dump_data = nfp_net_get_dump_data,
  775. .get_coalesce = nfp_net_get_coalesce,
  776. .set_coalesce = nfp_net_set_coalesce,
  777. .get_channels = nfp_net_get_channels,
  778. .set_channels = nfp_net_set_channels,
  779. .get_link_ksettings = nfp_net_get_link_ksettings,
  780. .set_link_ksettings = nfp_net_set_link_ksettings,
  781. };
  782. void nfp_net_set_ethtool_ops(struct net_device *netdev)
  783. {
  784. netdev->ethtool_ops = &nfp_net_ethtool_ops;
  785. }