dpaa2-ethtool.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /* Copyright 2014-2016 Freescale Semiconductor Inc.
  3. * Copyright 2016-2022 NXP
  4. */
  5. #include <linux/net_tstamp.h>
  6. #include <linux/nospec.h>
  7. #include "dpni.h" /* DPNI_LINK_OPT_* */
  8. #include "dpaa2-eth.h"
  9. /* To be kept in sync with DPNI statistics */
  10. static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
  11. "[hw] rx frames",
  12. "[hw] rx bytes",
  13. "[hw] rx mcast frames",
  14. "[hw] rx mcast bytes",
  15. "[hw] rx bcast frames",
  16. "[hw] rx bcast bytes",
  17. "[hw] tx frames",
  18. "[hw] tx bytes",
  19. "[hw] tx mcast frames",
  20. "[hw] tx mcast bytes",
  21. "[hw] tx bcast frames",
  22. "[hw] tx bcast bytes",
  23. "[hw] rx filtered frames",
  24. "[hw] rx discarded frames",
  25. "[hw] rx nobuffer discards",
  26. "[hw] tx discarded frames",
  27. "[hw] tx confirmed frames",
  28. "[hw] tx dequeued bytes",
  29. "[hw] tx dequeued frames",
  30. "[hw] tx rejected bytes",
  31. "[hw] tx rejected frames",
  32. "[hw] tx pending frames",
  33. };
  34. #define DPAA2_ETH_NUM_STATS ARRAY_SIZE(dpaa2_ethtool_stats)
  35. static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
  36. /* per-cpu stats */
  37. "[drv] tx conf frames",
  38. "[drv] tx conf bytes",
  39. "[drv] tx sg frames",
  40. "[drv] tx sg bytes",
  41. "[drv] tx tso frames",
  42. "[drv] tx tso bytes",
  43. "[drv] rx sg frames",
  44. "[drv] rx sg bytes",
  45. "[drv] tx converted sg frames",
  46. "[drv] tx converted sg bytes",
  47. "[drv] enqueue portal busy",
  48. /* Channel stats */
  49. "[drv] dequeue portal busy",
  50. "[drv] channel pull errors",
  51. "[drv] cdan",
  52. "[drv] xdp drop",
  53. "[drv] xdp tx",
  54. "[drv] xdp tx errors",
  55. "[drv] xdp redirect",
  56. /* FQ stats */
  57. "[qbman] rx pending frames",
  58. "[qbman] rx pending bytes",
  59. "[qbman] tx conf pending frames",
  60. "[qbman] tx conf pending bytes",
  61. "[qbman] buffer count",
  62. };
  63. #define DPAA2_ETH_NUM_EXTRA_STATS ARRAY_SIZE(dpaa2_ethtool_extras)
  64. static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
  65. struct ethtool_drvinfo *drvinfo)
  66. {
  67. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  68. strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
  69. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  70. "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
  71. strscpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
  72. sizeof(drvinfo->bus_info));
  73. }
  74. static int dpaa2_eth_nway_reset(struct net_device *net_dev)
  75. {
  76. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  77. int err = -EOPNOTSUPP;
  78. mutex_lock(&priv->mac_lock);
  79. if (dpaa2_eth_is_type_phy(priv))
  80. err = phylink_ethtool_nway_reset(priv->mac->phylink);
  81. mutex_unlock(&priv->mac_lock);
  82. return err;
  83. }
  84. static int
  85. dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
  86. struct ethtool_link_ksettings *link_settings)
  87. {
  88. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  89. int err;
  90. mutex_lock(&priv->mac_lock);
  91. if (dpaa2_eth_is_type_phy(priv)) {
  92. err = phylink_ethtool_ksettings_get(priv->mac->phylink,
  93. link_settings);
  94. mutex_unlock(&priv->mac_lock);
  95. return err;
  96. }
  97. mutex_unlock(&priv->mac_lock);
  98. link_settings->base.autoneg = AUTONEG_DISABLE;
  99. if (!(priv->link_state.options & DPNI_LINK_OPT_HALF_DUPLEX))
  100. link_settings->base.duplex = DUPLEX_FULL;
  101. link_settings->base.speed = priv->link_state.rate;
  102. return 0;
  103. }
  104. static int
  105. dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
  106. const struct ethtool_link_ksettings *link_settings)
  107. {
  108. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  109. int err = -EOPNOTSUPP;
  110. mutex_lock(&priv->mac_lock);
  111. if (dpaa2_eth_is_type_phy(priv))
  112. err = phylink_ethtool_ksettings_set(priv->mac->phylink,
  113. link_settings);
  114. mutex_unlock(&priv->mac_lock);
  115. return err;
  116. }
  117. static void dpaa2_eth_get_pauseparam(struct net_device *net_dev,
  118. struct ethtool_pauseparam *pause)
  119. {
  120. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  121. u64 link_options = priv->link_state.options;
  122. mutex_lock(&priv->mac_lock);
  123. if (dpaa2_eth_is_type_phy(priv)) {
  124. phylink_ethtool_get_pauseparam(priv->mac->phylink, pause);
  125. mutex_unlock(&priv->mac_lock);
  126. return;
  127. }
  128. mutex_unlock(&priv->mac_lock);
  129. pause->rx_pause = dpaa2_eth_rx_pause_enabled(link_options);
  130. pause->tx_pause = dpaa2_eth_tx_pause_enabled(link_options);
  131. pause->autoneg = AUTONEG_DISABLE;
  132. }
  133. static int dpaa2_eth_set_pauseparam(struct net_device *net_dev,
  134. struct ethtool_pauseparam *pause)
  135. {
  136. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  137. struct dpni_link_cfg cfg = {0};
  138. int err;
  139. if (!dpaa2_eth_has_pause_support(priv)) {
  140. netdev_info(net_dev, "No pause frame support for DPNI version < %d.%d\n",
  141. DPNI_PAUSE_VER_MAJOR, DPNI_PAUSE_VER_MINOR);
  142. return -EOPNOTSUPP;
  143. }
  144. mutex_lock(&priv->mac_lock);
  145. if (dpaa2_eth_is_type_phy(priv)) {
  146. err = phylink_ethtool_set_pauseparam(priv->mac->phylink,
  147. pause);
  148. mutex_unlock(&priv->mac_lock);
  149. return err;
  150. }
  151. mutex_unlock(&priv->mac_lock);
  152. if (pause->autoneg)
  153. return -EOPNOTSUPP;
  154. cfg.rate = priv->link_state.rate;
  155. cfg.options = priv->link_state.options;
  156. if (pause->rx_pause)
  157. cfg.options |= DPNI_LINK_OPT_PAUSE;
  158. else
  159. cfg.options &= ~DPNI_LINK_OPT_PAUSE;
  160. if (!!pause->rx_pause ^ !!pause->tx_pause)
  161. cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
  162. else
  163. cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
  164. if (cfg.options == priv->link_state.options)
  165. return 0;
  166. err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
  167. if (err) {
  168. netdev_err(net_dev, "dpni_set_link_state failed\n");
  169. return err;
  170. }
  171. priv->link_state.options = cfg.options;
  172. return 0;
  173. }
  174. static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
  175. u8 *data)
  176. {
  177. int i;
  178. switch (stringset) {
  179. case ETH_SS_STATS:
  180. for (i = 0; i < DPAA2_ETH_NUM_STATS; i++)
  181. ethtool_puts(&data, dpaa2_ethtool_stats[i]);
  182. for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++)
  183. ethtool_puts(&data, dpaa2_ethtool_extras[i]);
  184. dpaa2_mac_get_strings(&data);
  185. break;
  186. }
  187. }
  188. static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
  189. {
  190. switch (sset) {
  191. case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
  192. return DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS +
  193. dpaa2_mac_get_sset_count();
  194. default:
  195. return -EOPNOTSUPP;
  196. }
  197. }
  198. /** Fill in hardware counters, as returned by MC.
  199. */
  200. static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
  201. struct ethtool_stats *stats,
  202. u64 *data)
  203. {
  204. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  205. union dpni_statistics dpni_stats;
  206. int dpni_stats_page_size[DPNI_STATISTICS_CNT] = {
  207. sizeof(dpni_stats.page_0),
  208. sizeof(dpni_stats.page_1),
  209. sizeof(dpni_stats.page_2),
  210. sizeof(dpni_stats.page_3),
  211. sizeof(dpni_stats.page_4),
  212. sizeof(dpni_stats.page_5),
  213. sizeof(dpni_stats.page_6),
  214. };
  215. u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
  216. u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
  217. struct dpaa2_eth_ch_stats *ch_stats;
  218. struct dpaa2_eth_drv_stats *extras;
  219. u32 buf_cnt, buf_cnt_total = 0;
  220. int j, k, err, num_cnt, i = 0;
  221. u32 fcnt, bcnt;
  222. memset(data, 0,
  223. sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
  224. /* Print standard counters, from DPNI statistics */
  225. for (j = 0; j <= 6; j++) {
  226. /* We're not interested in pages 4 & 5 for now */
  227. if (j == 4 || j == 5)
  228. continue;
  229. err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
  230. j, &dpni_stats);
  231. if (err == -EINVAL)
  232. /* Older firmware versions don't support all pages */
  233. memset(&dpni_stats, 0, sizeof(dpni_stats));
  234. else if (err)
  235. netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
  236. num_cnt = dpni_stats_page_size[j] / sizeof(u64);
  237. for (k = 0; k < num_cnt; k++)
  238. *(data + i++) = dpni_stats.raw.counter[k];
  239. }
  240. /* Print per-cpu extra stats */
  241. for_each_online_cpu(k) {
  242. extras = per_cpu_ptr(priv->percpu_extras, k);
  243. for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
  244. *((__u64 *)data + i + j) += *((__u64 *)extras + j);
  245. }
  246. i += j;
  247. /* Per-channel stats */
  248. for (k = 0; k < priv->num_channels; k++) {
  249. ch_stats = &priv->channel[k]->stats;
  250. for (j = 0; j < DPAA2_ETH_CH_STATS; j++)
  251. *((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
  252. }
  253. i += j;
  254. for (j = 0; j < priv->num_fqs; j++) {
  255. /* Print FQ instantaneous counts */
  256. err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
  257. &fcnt, &bcnt);
  258. if (err) {
  259. netdev_warn(net_dev, "FQ query error %d", err);
  260. return;
  261. }
  262. if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
  263. fcnt_tx_total += fcnt;
  264. bcnt_tx_total += bcnt;
  265. } else {
  266. fcnt_rx_total += fcnt;
  267. bcnt_rx_total += bcnt;
  268. }
  269. }
  270. *(data + i++) = fcnt_rx_total;
  271. *(data + i++) = bcnt_rx_total;
  272. *(data + i++) = fcnt_tx_total;
  273. *(data + i++) = bcnt_tx_total;
  274. for (j = 0; j < priv->num_bps; j++) {
  275. err = dpaa2_io_query_bp_count(NULL, priv->bp[j]->bpid, &buf_cnt);
  276. if (err) {
  277. netdev_warn(net_dev, "Buffer count query error %d\n", err);
  278. return;
  279. }
  280. buf_cnt_total += buf_cnt;
  281. }
  282. *(data + i++) = buf_cnt_total;
  283. mutex_lock(&priv->mac_lock);
  284. if (dpaa2_eth_has_mac(priv))
  285. dpaa2_mac_get_ethtool_stats(priv->mac, data + i);
  286. mutex_unlock(&priv->mac_lock);
  287. }
  288. static int dpaa2_eth_prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
  289. void *key, void *mask, u64 *fields)
  290. {
  291. int off;
  292. if (eth_mask->h_proto) {
  293. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
  294. *(__be16 *)(key + off) = eth_value->h_proto;
  295. *(__be16 *)(mask + off) = eth_mask->h_proto;
  296. *fields |= DPAA2_ETH_DIST_ETHTYPE;
  297. }
  298. if (!is_zero_ether_addr(eth_mask->h_source)) {
  299. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
  300. ether_addr_copy(key + off, eth_value->h_source);
  301. ether_addr_copy(mask + off, eth_mask->h_source);
  302. *fields |= DPAA2_ETH_DIST_ETHSRC;
  303. }
  304. if (!is_zero_ether_addr(eth_mask->h_dest)) {
  305. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
  306. ether_addr_copy(key + off, eth_value->h_dest);
  307. ether_addr_copy(mask + off, eth_mask->h_dest);
  308. *fields |= DPAA2_ETH_DIST_ETHDST;
  309. }
  310. return 0;
  311. }
  312. static int dpaa2_eth_prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
  313. struct ethtool_usrip4_spec *uip_mask,
  314. void *key, void *mask, u64 *fields)
  315. {
  316. int off;
  317. u32 tmp_value, tmp_mask;
  318. if (uip_mask->tos || uip_mask->ip_ver)
  319. return -EOPNOTSUPP;
  320. if (uip_mask->ip4src) {
  321. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
  322. *(__be32 *)(key + off) = uip_value->ip4src;
  323. *(__be32 *)(mask + off) = uip_mask->ip4src;
  324. *fields |= DPAA2_ETH_DIST_IPSRC;
  325. }
  326. if (uip_mask->ip4dst) {
  327. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
  328. *(__be32 *)(key + off) = uip_value->ip4dst;
  329. *(__be32 *)(mask + off) = uip_mask->ip4dst;
  330. *fields |= DPAA2_ETH_DIST_IPDST;
  331. }
  332. if (uip_mask->proto) {
  333. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
  334. *(u8 *)(key + off) = uip_value->proto;
  335. *(u8 *)(mask + off) = uip_mask->proto;
  336. *fields |= DPAA2_ETH_DIST_IPPROTO;
  337. }
  338. if (uip_mask->l4_4_bytes) {
  339. tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
  340. tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
  341. off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
  342. *(__be16 *)(key + off) = htons(tmp_value >> 16);
  343. *(__be16 *)(mask + off) = htons(tmp_mask >> 16);
  344. *fields |= DPAA2_ETH_DIST_L4SRC;
  345. off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
  346. *(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
  347. *(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
  348. *fields |= DPAA2_ETH_DIST_L4DST;
  349. }
  350. /* Only apply the rule for IPv4 frames */
  351. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
  352. *(__be16 *)(key + off) = htons(ETH_P_IP);
  353. *(__be16 *)(mask + off) = htons(0xFFFF);
  354. *fields |= DPAA2_ETH_DIST_ETHTYPE;
  355. return 0;
  356. }
  357. static int dpaa2_eth_prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
  358. struct ethtool_tcpip4_spec *l4_mask,
  359. void *key, void *mask, u8 l4_proto, u64 *fields)
  360. {
  361. int off;
  362. if (l4_mask->tos)
  363. return -EOPNOTSUPP;
  364. if (l4_mask->ip4src) {
  365. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
  366. *(__be32 *)(key + off) = l4_value->ip4src;
  367. *(__be32 *)(mask + off) = l4_mask->ip4src;
  368. *fields |= DPAA2_ETH_DIST_IPSRC;
  369. }
  370. if (l4_mask->ip4dst) {
  371. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
  372. *(__be32 *)(key + off) = l4_value->ip4dst;
  373. *(__be32 *)(mask + off) = l4_mask->ip4dst;
  374. *fields |= DPAA2_ETH_DIST_IPDST;
  375. }
  376. if (l4_mask->psrc) {
  377. off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
  378. *(__be16 *)(key + off) = l4_value->psrc;
  379. *(__be16 *)(mask + off) = l4_mask->psrc;
  380. *fields |= DPAA2_ETH_DIST_L4SRC;
  381. }
  382. if (l4_mask->pdst) {
  383. off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
  384. *(__be16 *)(key + off) = l4_value->pdst;
  385. *(__be16 *)(mask + off) = l4_mask->pdst;
  386. *fields |= DPAA2_ETH_DIST_L4DST;
  387. }
  388. /* Only apply the rule for IPv4 frames with the specified L4 proto */
  389. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
  390. *(__be16 *)(key + off) = htons(ETH_P_IP);
  391. *(__be16 *)(mask + off) = htons(0xFFFF);
  392. *fields |= DPAA2_ETH_DIST_ETHTYPE;
  393. off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
  394. *(u8 *)(key + off) = l4_proto;
  395. *(u8 *)(mask + off) = 0xFF;
  396. *fields |= DPAA2_ETH_DIST_IPPROTO;
  397. return 0;
  398. }
  399. static int dpaa2_eth_prep_ext_rule(struct ethtool_flow_ext *ext_value,
  400. struct ethtool_flow_ext *ext_mask,
  401. void *key, void *mask, u64 *fields)
  402. {
  403. int off;
  404. if (ext_mask->vlan_etype)
  405. return -EOPNOTSUPP;
  406. if (ext_mask->vlan_tci) {
  407. off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
  408. *(__be16 *)(key + off) = ext_value->vlan_tci;
  409. *(__be16 *)(mask + off) = ext_mask->vlan_tci;
  410. *fields |= DPAA2_ETH_DIST_VLAN;
  411. }
  412. return 0;
  413. }
  414. static int dpaa2_eth_prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
  415. struct ethtool_flow_ext *ext_mask,
  416. void *key, void *mask, u64 *fields)
  417. {
  418. int off;
  419. if (!is_zero_ether_addr(ext_mask->h_dest)) {
  420. off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
  421. ether_addr_copy(key + off, ext_value->h_dest);
  422. ether_addr_copy(mask + off, ext_mask->h_dest);
  423. *fields |= DPAA2_ETH_DIST_ETHDST;
  424. }
  425. return 0;
  426. }
  427. static int dpaa2_eth_prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key,
  428. void *mask, u64 *fields)
  429. {
  430. int err;
  431. switch (fs->flow_type & 0xFF) {
  432. case ETHER_FLOW:
  433. err = dpaa2_eth_prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
  434. key, mask, fields);
  435. break;
  436. case IP_USER_FLOW:
  437. err = dpaa2_eth_prep_uip_rule(&fs->h_u.usr_ip4_spec,
  438. &fs->m_u.usr_ip4_spec, key, mask, fields);
  439. break;
  440. case TCP_V4_FLOW:
  441. err = dpaa2_eth_prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
  442. key, mask, IPPROTO_TCP, fields);
  443. break;
  444. case UDP_V4_FLOW:
  445. err = dpaa2_eth_prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
  446. key, mask, IPPROTO_UDP, fields);
  447. break;
  448. case SCTP_V4_FLOW:
  449. err = dpaa2_eth_prep_l4_rule(&fs->h_u.sctp_ip4_spec,
  450. &fs->m_u.sctp_ip4_spec, key, mask,
  451. IPPROTO_SCTP, fields);
  452. break;
  453. default:
  454. return -EOPNOTSUPP;
  455. }
  456. if (err)
  457. return err;
  458. if (fs->flow_type & FLOW_EXT) {
  459. err = dpaa2_eth_prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
  460. if (err)
  461. return err;
  462. }
  463. if (fs->flow_type & FLOW_MAC_EXT) {
  464. err = dpaa2_eth_prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key,
  465. mask, fields);
  466. if (err)
  467. return err;
  468. }
  469. return 0;
  470. }
  471. static int dpaa2_eth_do_cls_rule(struct net_device *net_dev,
  472. struct ethtool_rx_flow_spec *fs,
  473. bool add)
  474. {
  475. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  476. struct device *dev = net_dev->dev.parent;
  477. struct dpni_rule_cfg rule_cfg = { 0 };
  478. struct dpni_fs_action_cfg fs_act = { 0 };
  479. dma_addr_t key_iova;
  480. u64 fields = 0;
  481. void *key_buf;
  482. int i, err;
  483. if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
  484. fs->ring_cookie >= dpaa2_eth_queue_count(priv))
  485. return -EINVAL;
  486. rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
  487. /* allocate twice the key size, for the actual key and for mask */
  488. key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
  489. if (!key_buf)
  490. return -ENOMEM;
  491. /* Fill the key and mask memory areas */
  492. err = dpaa2_eth_prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
  493. if (err)
  494. goto free_mem;
  495. if (!dpaa2_eth_fs_mask_enabled(priv)) {
  496. /* Masking allows us to configure a maximal key during init and
  497. * use it for all flow steering rules. Without it, we include
  498. * in the key only the fields actually used, so we need to
  499. * extract the others from the final key buffer.
  500. *
  501. * Program the FS key if needed, or return error if previously
  502. * set key can't be used for the current rule. User needs to
  503. * delete existing rules in this case to allow for the new one.
  504. */
  505. if (!priv->rx_cls_fields) {
  506. err = dpaa2_eth_set_cls(net_dev, fields);
  507. if (err)
  508. goto free_mem;
  509. priv->rx_cls_fields = fields;
  510. } else if (priv->rx_cls_fields != fields) {
  511. netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
  512. err = -EOPNOTSUPP;
  513. goto free_mem;
  514. }
  515. dpaa2_eth_cls_trim_rule(key_buf, fields);
  516. rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
  517. }
  518. key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
  519. DMA_TO_DEVICE);
  520. if (dma_mapping_error(dev, key_iova)) {
  521. err = -ENOMEM;
  522. goto free_mem;
  523. }
  524. rule_cfg.key_iova = key_iova;
  525. if (dpaa2_eth_fs_mask_enabled(priv))
  526. rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
  527. if (add) {
  528. if (fs->ring_cookie == RX_CLS_FLOW_DISC)
  529. fs_act.options |= DPNI_FS_OPT_DISCARD;
  530. else
  531. fs_act.flow_id = fs->ring_cookie;
  532. }
  533. for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
  534. if (add)
  535. err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token,
  536. i, fs->location, &rule_cfg,
  537. &fs_act);
  538. else
  539. err = dpni_remove_fs_entry(priv->mc_io, 0,
  540. priv->mc_token, i,
  541. &rule_cfg);
  542. if (err || priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
  543. break;
  544. }
  545. dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
  546. free_mem:
  547. kfree(key_buf);
  548. return err;
  549. }
  550. static int dpaa2_eth_num_cls_rules(struct dpaa2_eth_priv *priv)
  551. {
  552. int i, rules = 0;
  553. for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
  554. if (priv->cls_rules[i].in_use)
  555. rules++;
  556. return rules;
  557. }
  558. static int dpaa2_eth_update_cls_rule(struct net_device *net_dev,
  559. struct ethtool_rx_flow_spec *new_fs,
  560. unsigned int location)
  561. {
  562. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  563. struct dpaa2_eth_cls_rule *rule;
  564. int err = -EINVAL;
  565. if (!priv->rx_cls_enabled)
  566. return -EOPNOTSUPP;
  567. if (location >= dpaa2_eth_fs_count(priv))
  568. return -EINVAL;
  569. rule = &priv->cls_rules[location];
  570. /* If a rule is present at the specified location, delete it. */
  571. if (rule->in_use) {
  572. err = dpaa2_eth_do_cls_rule(net_dev, &rule->fs, false);
  573. if (err)
  574. return err;
  575. rule->in_use = 0;
  576. if (!dpaa2_eth_fs_mask_enabled(priv) &&
  577. !dpaa2_eth_num_cls_rules(priv))
  578. priv->rx_cls_fields = 0;
  579. }
  580. /* If no new entry to add, return here */
  581. if (!new_fs)
  582. return err;
  583. err = dpaa2_eth_do_cls_rule(net_dev, new_fs, true);
  584. if (err)
  585. return err;
  586. rule->in_use = 1;
  587. rule->fs = *new_fs;
  588. return 0;
  589. }
  590. static u32 dpaa2_eth_get_rx_ring_count(struct net_device *net_dev)
  591. {
  592. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  593. return dpaa2_eth_queue_count(priv);
  594. }
  595. static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
  596. struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
  597. {
  598. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  599. int max_rules = dpaa2_eth_fs_count(priv);
  600. int i, j = 0;
  601. switch (rxnfc->cmd) {
  602. case ETHTOOL_GRXCLSRLCNT:
  603. rxnfc->rule_cnt = 0;
  604. rxnfc->rule_cnt = dpaa2_eth_num_cls_rules(priv);
  605. rxnfc->data = max_rules;
  606. break;
  607. case ETHTOOL_GRXCLSRULE:
  608. if (rxnfc->fs.location >= max_rules)
  609. return -EINVAL;
  610. rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
  611. max_rules);
  612. if (!priv->cls_rules[rxnfc->fs.location].in_use)
  613. return -EINVAL;
  614. rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
  615. break;
  616. case ETHTOOL_GRXCLSRLALL:
  617. for (i = 0; i < max_rules; i++) {
  618. if (!priv->cls_rules[i].in_use)
  619. continue;
  620. if (j == rxnfc->rule_cnt)
  621. return -EMSGSIZE;
  622. rule_locs[j++] = i;
  623. }
  624. rxnfc->rule_cnt = j;
  625. rxnfc->data = max_rules;
  626. break;
  627. default:
  628. return -EOPNOTSUPP;
  629. }
  630. return 0;
  631. }
  632. static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
  633. struct ethtool_rxnfc *rxnfc)
  634. {
  635. int err = 0;
  636. switch (rxnfc->cmd) {
  637. case ETHTOOL_SRXCLSRLINS:
  638. err = dpaa2_eth_update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
  639. break;
  640. case ETHTOOL_SRXCLSRLDEL:
  641. err = dpaa2_eth_update_cls_rule(net_dev, NULL, rxnfc->fs.location);
  642. break;
  643. default:
  644. err = -EOPNOTSUPP;
  645. }
  646. return err;
  647. }
  648. static int dpaa2_eth_get_rxfh_fields(struct net_device *net_dev,
  649. struct ethtool_rxfh_fields *rxnfc)
  650. {
  651. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  652. /* we purposely ignore cmd->flow_type for now, because the
  653. * classifier only supports a single set of fields for all
  654. * protocols
  655. */
  656. rxnfc->data = priv->rx_hash_fields;
  657. return 0;
  658. }
  659. static int dpaa2_eth_set_rxfh_fields(struct net_device *net_dev,
  660. const struct ethtool_rxfh_fields *rxnfc,
  661. struct netlink_ext_ack *extack)
  662. {
  663. if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
  664. return -EOPNOTSUPP;
  665. return dpaa2_eth_set_hash(net_dev, rxnfc->data);
  666. }
  667. int dpaa2_phc_index = -1;
  668. EXPORT_SYMBOL(dpaa2_phc_index);
  669. static int dpaa2_eth_get_ts_info(struct net_device *dev,
  670. struct kernel_ethtool_ts_info *info)
  671. {
  672. if (!dpaa2_ptp)
  673. return ethtool_op_get_ts_info(dev, info);
  674. info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
  675. SOF_TIMESTAMPING_RX_HARDWARE |
  676. SOF_TIMESTAMPING_RAW_HARDWARE;
  677. info->phc_index = dpaa2_phc_index;
  678. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  679. (1 << HWTSTAMP_TX_ON) |
  680. (1 << HWTSTAMP_TX_ONESTEP_SYNC);
  681. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  682. (1 << HWTSTAMP_FILTER_ALL);
  683. return 0;
  684. }
  685. static int dpaa2_eth_get_tunable(struct net_device *net_dev,
  686. const struct ethtool_tunable *tuna,
  687. void *data)
  688. {
  689. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  690. int err = 0;
  691. switch (tuna->id) {
  692. case ETHTOOL_RX_COPYBREAK:
  693. *(u32 *)data = priv->rx_copybreak;
  694. break;
  695. default:
  696. err = -EOPNOTSUPP;
  697. break;
  698. }
  699. return err;
  700. }
  701. static int dpaa2_eth_set_tunable(struct net_device *net_dev,
  702. const struct ethtool_tunable *tuna,
  703. const void *data)
  704. {
  705. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  706. int err = 0;
  707. switch (tuna->id) {
  708. case ETHTOOL_RX_COPYBREAK:
  709. priv->rx_copybreak = *(u32 *)data;
  710. break;
  711. default:
  712. err = -EOPNOTSUPP;
  713. break;
  714. }
  715. return err;
  716. }
  717. static int dpaa2_eth_get_coalesce(struct net_device *dev,
  718. struct ethtool_coalesce *ic,
  719. struct kernel_ethtool_coalesce *kernel_coal,
  720. struct netlink_ext_ack *extack)
  721. {
  722. struct dpaa2_eth_priv *priv = netdev_priv(dev);
  723. struct dpaa2_io *dpio = priv->channel[0]->dpio;
  724. dpaa2_io_get_irq_coalescing(dpio, &ic->rx_coalesce_usecs);
  725. ic->use_adaptive_rx_coalesce = dpaa2_io_get_adaptive_coalescing(dpio);
  726. return 0;
  727. }
  728. static int dpaa2_eth_set_coalesce(struct net_device *dev,
  729. struct ethtool_coalesce *ic,
  730. struct kernel_ethtool_coalesce *kernel_coal,
  731. struct netlink_ext_ack *extack)
  732. {
  733. struct dpaa2_eth_priv *priv = netdev_priv(dev);
  734. struct dpaa2_io *dpio;
  735. int prev_adaptive;
  736. u32 prev_rx_usecs;
  737. int i, j, err;
  738. /* Keep track of the previous value, just in case we fail */
  739. dpio = priv->channel[0]->dpio;
  740. dpaa2_io_get_irq_coalescing(dpio, &prev_rx_usecs);
  741. prev_adaptive = dpaa2_io_get_adaptive_coalescing(dpio);
  742. /* Setup new value for rx coalescing */
  743. for (i = 0; i < priv->num_channels; i++) {
  744. dpio = priv->channel[i]->dpio;
  745. dpaa2_io_set_adaptive_coalescing(dpio,
  746. ic->use_adaptive_rx_coalesce);
  747. err = dpaa2_io_set_irq_coalescing(dpio, ic->rx_coalesce_usecs);
  748. if (err)
  749. goto restore_rx_usecs;
  750. }
  751. return 0;
  752. restore_rx_usecs:
  753. for (j = 0; j < i; j++) {
  754. dpio = priv->channel[j]->dpio;
  755. dpaa2_io_set_irq_coalescing(dpio, prev_rx_usecs);
  756. dpaa2_io_set_adaptive_coalescing(dpio, prev_adaptive);
  757. }
  758. return err;
  759. }
  760. static void dpaa2_eth_get_channels(struct net_device *net_dev,
  761. struct ethtool_channels *channels)
  762. {
  763. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  764. int queue_count = dpaa2_eth_queue_count(priv);
  765. channels->max_rx = queue_count;
  766. channels->max_tx = queue_count;
  767. channels->rx_count = queue_count;
  768. channels->tx_count = queue_count;
  769. /* Tx confirmation and Rx error */
  770. channels->max_other = queue_count + 1;
  771. channels->max_combined = channels->max_rx +
  772. channels->max_tx +
  773. channels->max_other;
  774. /* Tx conf and Rx err */
  775. channels->other_count = queue_count + 1;
  776. channels->combined_count = channels->rx_count +
  777. channels->tx_count +
  778. channels->other_count;
  779. }
  780. const struct ethtool_ops dpaa2_ethtool_ops = {
  781. .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
  782. ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
  783. .get_drvinfo = dpaa2_eth_get_drvinfo,
  784. .nway_reset = dpaa2_eth_nway_reset,
  785. .get_link = ethtool_op_get_link,
  786. .get_link_ksettings = dpaa2_eth_get_link_ksettings,
  787. .set_link_ksettings = dpaa2_eth_set_link_ksettings,
  788. .get_pauseparam = dpaa2_eth_get_pauseparam,
  789. .set_pauseparam = dpaa2_eth_set_pauseparam,
  790. .get_sset_count = dpaa2_eth_get_sset_count,
  791. .get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
  792. .get_strings = dpaa2_eth_get_strings,
  793. .get_rxnfc = dpaa2_eth_get_rxnfc,
  794. .set_rxnfc = dpaa2_eth_set_rxnfc,
  795. .get_rx_ring_count = dpaa2_eth_get_rx_ring_count,
  796. .get_rxfh_fields = dpaa2_eth_get_rxfh_fields,
  797. .set_rxfh_fields = dpaa2_eth_set_rxfh_fields,
  798. .get_ts_info = dpaa2_eth_get_ts_info,
  799. .get_tunable = dpaa2_eth_get_tunable,
  800. .set_tunable = dpaa2_eth_set_tunable,
  801. .get_coalesce = dpaa2_eth_get_coalesce,
  802. .set_coalesce = dpaa2_eth_set_coalesce,
  803. .get_channels = dpaa2_eth_get_channels,
  804. };