dpaa_ethtool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* Copyright 2008-2016 Freescale Semiconductor, Inc.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * * Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * * Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. * * Neither the name of Freescale Semiconductor nor the
  11. * names of its contributors may be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. *
  15. * ALTERNATIVELY, this software may be distributed under the terms of the
  16. * GNU General Public License ("GPL") as published by the Free Software
  17. * Foundation, either version 2 of that License or (at your option) any
  18. * later version.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/string.h>
  33. #include "dpaa_eth.h"
  34. #include "mac.h"
  35. static const char dpaa_stats_percpu[][ETH_GSTRING_LEN] = {
  36. "interrupts",
  37. "rx packets",
  38. "tx packets",
  39. "tx confirm",
  40. "tx S/G",
  41. "tx error",
  42. "rx error",
  43. };
  44. static char dpaa_stats_global[][ETH_GSTRING_LEN] = {
  45. /* dpa rx errors */
  46. "rx dma error",
  47. "rx frame physical error",
  48. "rx frame size error",
  49. "rx header error",
  50. /* demultiplexing errors */
  51. "qman cg_tdrop",
  52. "qman wred",
  53. "qman error cond",
  54. "qman early window",
  55. "qman late window",
  56. "qman fq tdrop",
  57. "qman fq retired",
  58. "qman orp disabled",
  59. /* congestion related stats */
  60. "congestion time (ms)",
  61. "entered congestion",
  62. "congested (0/1)"
  63. };
  64. #define DPAA_STATS_PERCPU_LEN ARRAY_SIZE(dpaa_stats_percpu)
  65. #define DPAA_STATS_GLOBAL_LEN ARRAY_SIZE(dpaa_stats_global)
  66. static int dpaa_get_link_ksettings(struct net_device *net_dev,
  67. struct ethtool_link_ksettings *cmd)
  68. {
  69. if (!net_dev->phydev) {
  70. netdev_dbg(net_dev, "phy device not initialized\n");
  71. return 0;
  72. }
  73. phy_ethtool_ksettings_get(net_dev->phydev, cmd);
  74. return 0;
  75. }
  76. static int dpaa_set_link_ksettings(struct net_device *net_dev,
  77. const struct ethtool_link_ksettings *cmd)
  78. {
  79. int err;
  80. if (!net_dev->phydev) {
  81. netdev_err(net_dev, "phy device not initialized\n");
  82. return -ENODEV;
  83. }
  84. err = phy_ethtool_ksettings_set(net_dev->phydev, cmd);
  85. if (err < 0)
  86. netdev_err(net_dev, "phy_ethtool_ksettings_set() = %d\n", err);
  87. return err;
  88. }
  89. static void dpaa_get_drvinfo(struct net_device *net_dev,
  90. struct ethtool_drvinfo *drvinfo)
  91. {
  92. int len;
  93. strlcpy(drvinfo->driver, KBUILD_MODNAME,
  94. sizeof(drvinfo->driver));
  95. len = snprintf(drvinfo->version, sizeof(drvinfo->version),
  96. "%X", 0);
  97. len = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  98. "%X", 0);
  99. if (len >= sizeof(drvinfo->fw_version)) {
  100. /* Truncated output */
  101. netdev_notice(net_dev, "snprintf() = %d\n", len);
  102. }
  103. strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
  104. sizeof(drvinfo->bus_info));
  105. }
  106. static u32 dpaa_get_msglevel(struct net_device *net_dev)
  107. {
  108. return ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable;
  109. }
  110. static void dpaa_set_msglevel(struct net_device *net_dev,
  111. u32 msg_enable)
  112. {
  113. ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable = msg_enable;
  114. }
  115. static int dpaa_nway_reset(struct net_device *net_dev)
  116. {
  117. int err;
  118. if (!net_dev->phydev) {
  119. netdev_err(net_dev, "phy device not initialized\n");
  120. return -ENODEV;
  121. }
  122. err = 0;
  123. if (net_dev->phydev->autoneg) {
  124. err = phy_start_aneg(net_dev->phydev);
  125. if (err < 0)
  126. netdev_err(net_dev, "phy_start_aneg() = %d\n",
  127. err);
  128. }
  129. return err;
  130. }
  131. static void dpaa_get_pauseparam(struct net_device *net_dev,
  132. struct ethtool_pauseparam *epause)
  133. {
  134. struct mac_device *mac_dev;
  135. struct dpaa_priv *priv;
  136. priv = netdev_priv(net_dev);
  137. mac_dev = priv->mac_dev;
  138. if (!net_dev->phydev) {
  139. netdev_err(net_dev, "phy device not initialized\n");
  140. return;
  141. }
  142. epause->autoneg = mac_dev->autoneg_pause;
  143. epause->rx_pause = mac_dev->rx_pause_active;
  144. epause->tx_pause = mac_dev->tx_pause_active;
  145. }
  146. static int dpaa_set_pauseparam(struct net_device *net_dev,
  147. struct ethtool_pauseparam *epause)
  148. {
  149. struct mac_device *mac_dev;
  150. struct phy_device *phydev;
  151. bool rx_pause, tx_pause;
  152. struct dpaa_priv *priv;
  153. u32 newadv, oldadv;
  154. int err;
  155. priv = netdev_priv(net_dev);
  156. mac_dev = priv->mac_dev;
  157. phydev = net_dev->phydev;
  158. if (!phydev) {
  159. netdev_err(net_dev, "phy device not initialized\n");
  160. return -ENODEV;
  161. }
  162. if (!(phydev->supported & SUPPORTED_Pause) ||
  163. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  164. (epause->rx_pause != epause->tx_pause)))
  165. return -EINVAL;
  166. /* The MAC should know how to handle PAUSE frame autonegotiation before
  167. * adjust_link is triggered by a forced renegotiation of sym/asym PAUSE
  168. * settings.
  169. */
  170. mac_dev->autoneg_pause = !!epause->autoneg;
  171. mac_dev->rx_pause_req = !!epause->rx_pause;
  172. mac_dev->tx_pause_req = !!epause->tx_pause;
  173. /* Determine the sym/asym advertised PAUSE capabilities from the desired
  174. * rx/tx pause settings.
  175. */
  176. newadv = 0;
  177. if (epause->rx_pause)
  178. newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  179. if (epause->tx_pause)
  180. newadv |= ADVERTISED_Asym_Pause;
  181. oldadv = phydev->advertising &
  182. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  183. /* If there are differences between the old and the new advertised
  184. * values, restart PHY autonegotiation and advertise the new values.
  185. */
  186. if (oldadv != newadv) {
  187. phydev->advertising &= ~(ADVERTISED_Pause
  188. | ADVERTISED_Asym_Pause);
  189. phydev->advertising |= newadv;
  190. if (phydev->autoneg) {
  191. err = phy_start_aneg(phydev);
  192. if (err < 0)
  193. netdev_err(net_dev, "phy_start_aneg() = %d\n",
  194. err);
  195. }
  196. }
  197. fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
  198. err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
  199. if (err < 0)
  200. netdev_err(net_dev, "set_mac_active_pause() = %d\n", err);
  201. return err;
  202. }
  203. static int dpaa_get_sset_count(struct net_device *net_dev, int type)
  204. {
  205. unsigned int total_stats, num_stats;
  206. num_stats = num_online_cpus() + 1;
  207. total_stats = num_stats * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM) +
  208. DPAA_STATS_GLOBAL_LEN;
  209. switch (type) {
  210. case ETH_SS_STATS:
  211. return total_stats;
  212. default:
  213. return -EOPNOTSUPP;
  214. }
  215. }
  216. static void copy_stats(struct dpaa_percpu_priv *percpu_priv, int num_cpus,
  217. int crr_cpu, u64 *bp_count, u64 *data)
  218. {
  219. int num_values = num_cpus + 1;
  220. int crr = 0, j;
  221. /* update current CPU's stats and also add them to the total values */
  222. data[crr * num_values + crr_cpu] = percpu_priv->in_interrupt;
  223. data[crr++ * num_values + num_cpus] += percpu_priv->in_interrupt;
  224. data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_packets;
  225. data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_packets;
  226. data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_packets;
  227. data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_packets;
  228. data[crr * num_values + crr_cpu] = percpu_priv->tx_confirm;
  229. data[crr++ * num_values + num_cpus] += percpu_priv->tx_confirm;
  230. data[crr * num_values + crr_cpu] = percpu_priv->tx_frag_skbuffs;
  231. data[crr++ * num_values + num_cpus] += percpu_priv->tx_frag_skbuffs;
  232. data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_errors;
  233. data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_errors;
  234. data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_errors;
  235. data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_errors;
  236. for (j = 0; j < DPAA_BPS_NUM; j++) {
  237. data[crr * num_values + crr_cpu] = bp_count[j];
  238. data[crr++ * num_values + num_cpus] += bp_count[j];
  239. }
  240. }
  241. static void dpaa_get_ethtool_stats(struct net_device *net_dev,
  242. struct ethtool_stats *stats, u64 *data)
  243. {
  244. u64 bp_count[DPAA_BPS_NUM], cg_time, cg_num;
  245. struct dpaa_percpu_priv *percpu_priv;
  246. struct dpaa_rx_errors rx_errors;
  247. unsigned int num_cpus, offset;
  248. struct dpaa_ern_cnt ern_cnt;
  249. struct dpaa_bp *dpaa_bp;
  250. struct dpaa_priv *priv;
  251. int total_stats, i, j;
  252. bool cg_status;
  253. total_stats = dpaa_get_sset_count(net_dev, ETH_SS_STATS);
  254. priv = netdev_priv(net_dev);
  255. num_cpus = num_online_cpus();
  256. memset(&bp_count, 0, sizeof(bp_count));
  257. memset(&rx_errors, 0, sizeof(struct dpaa_rx_errors));
  258. memset(&ern_cnt, 0, sizeof(struct dpaa_ern_cnt));
  259. memset(data, 0, total_stats * sizeof(u64));
  260. for_each_online_cpu(i) {
  261. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  262. for (j = 0; j < DPAA_BPS_NUM; j++) {
  263. dpaa_bp = priv->dpaa_bps[j];
  264. if (!dpaa_bp->percpu_count)
  265. continue;
  266. bp_count[j] = *(per_cpu_ptr(dpaa_bp->percpu_count, i));
  267. }
  268. rx_errors.dme += percpu_priv->rx_errors.dme;
  269. rx_errors.fpe += percpu_priv->rx_errors.fpe;
  270. rx_errors.fse += percpu_priv->rx_errors.fse;
  271. rx_errors.phe += percpu_priv->rx_errors.phe;
  272. ern_cnt.cg_tdrop += percpu_priv->ern_cnt.cg_tdrop;
  273. ern_cnt.wred += percpu_priv->ern_cnt.wred;
  274. ern_cnt.err_cond += percpu_priv->ern_cnt.err_cond;
  275. ern_cnt.early_window += percpu_priv->ern_cnt.early_window;
  276. ern_cnt.late_window += percpu_priv->ern_cnt.late_window;
  277. ern_cnt.fq_tdrop += percpu_priv->ern_cnt.fq_tdrop;
  278. ern_cnt.fq_retired += percpu_priv->ern_cnt.fq_retired;
  279. ern_cnt.orp_zero += percpu_priv->ern_cnt.orp_zero;
  280. copy_stats(percpu_priv, num_cpus, i, bp_count, data);
  281. }
  282. offset = (num_cpus + 1) * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM);
  283. memcpy(data + offset, &rx_errors, sizeof(struct dpaa_rx_errors));
  284. offset += sizeof(struct dpaa_rx_errors) / sizeof(u64);
  285. memcpy(data + offset, &ern_cnt, sizeof(struct dpaa_ern_cnt));
  286. /* gather congestion related counters */
  287. cg_num = 0;
  288. cg_status = 0;
  289. cg_time = jiffies_to_msecs(priv->cgr_data.congested_jiffies);
  290. if (qman_query_cgr_congested(&priv->cgr_data.cgr, &cg_status) == 0) {
  291. cg_num = priv->cgr_data.cgr_congested_count;
  292. /* reset congestion stats (like QMan API does */
  293. priv->cgr_data.congested_jiffies = 0;
  294. priv->cgr_data.cgr_congested_count = 0;
  295. }
  296. offset += sizeof(struct dpaa_ern_cnt) / sizeof(u64);
  297. data[offset++] = cg_time;
  298. data[offset++] = cg_num;
  299. data[offset++] = cg_status;
  300. }
  301. static void dpaa_get_strings(struct net_device *net_dev, u32 stringset,
  302. u8 *data)
  303. {
  304. unsigned int i, j, num_cpus, size;
  305. char string_cpu[ETH_GSTRING_LEN];
  306. u8 *strings;
  307. memset(string_cpu, 0, sizeof(string_cpu));
  308. strings = data;
  309. num_cpus = num_online_cpus();
  310. size = DPAA_STATS_GLOBAL_LEN * ETH_GSTRING_LEN;
  311. for (i = 0; i < DPAA_STATS_PERCPU_LEN; i++) {
  312. for (j = 0; j < num_cpus; j++) {
  313. snprintf(string_cpu, ETH_GSTRING_LEN, "%s [CPU %d]",
  314. dpaa_stats_percpu[i], j);
  315. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  316. strings += ETH_GSTRING_LEN;
  317. }
  318. snprintf(string_cpu, ETH_GSTRING_LEN, "%s [TOTAL]",
  319. dpaa_stats_percpu[i]);
  320. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  321. strings += ETH_GSTRING_LEN;
  322. }
  323. for (i = 0; i < DPAA_BPS_NUM; i++) {
  324. for (j = 0; j < num_cpus; j++) {
  325. snprintf(string_cpu, ETH_GSTRING_LEN,
  326. "bpool %c [CPU %d]", 'a' + i, j);
  327. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  328. strings += ETH_GSTRING_LEN;
  329. }
  330. snprintf(string_cpu, ETH_GSTRING_LEN, "bpool %c [TOTAL]",
  331. 'a' + i);
  332. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  333. strings += ETH_GSTRING_LEN;
  334. }
  335. memcpy(strings, dpaa_stats_global, size);
  336. }
  337. static int dpaa_get_hash_opts(struct net_device *dev,
  338. struct ethtool_rxnfc *cmd)
  339. {
  340. struct dpaa_priv *priv = netdev_priv(dev);
  341. cmd->data = 0;
  342. switch (cmd->flow_type) {
  343. case TCP_V4_FLOW:
  344. case TCP_V6_FLOW:
  345. case UDP_V4_FLOW:
  346. case UDP_V6_FLOW:
  347. if (priv->keygen_in_use)
  348. cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  349. /* Fall through */
  350. case IPV4_FLOW:
  351. case IPV6_FLOW:
  352. case SCTP_V4_FLOW:
  353. case SCTP_V6_FLOW:
  354. case AH_ESP_V4_FLOW:
  355. case AH_ESP_V6_FLOW:
  356. case AH_V4_FLOW:
  357. case AH_V6_FLOW:
  358. case ESP_V4_FLOW:
  359. case ESP_V6_FLOW:
  360. if (priv->keygen_in_use)
  361. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  362. break;
  363. default:
  364. cmd->data = 0;
  365. break;
  366. }
  367. return 0;
  368. }
  369. static int dpaa_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  370. u32 *unused)
  371. {
  372. int ret = -EOPNOTSUPP;
  373. switch (cmd->cmd) {
  374. case ETHTOOL_GRXFH:
  375. ret = dpaa_get_hash_opts(dev, cmd);
  376. break;
  377. default:
  378. break;
  379. }
  380. return ret;
  381. }
  382. static void dpaa_set_hash(struct net_device *net_dev, bool enable)
  383. {
  384. struct mac_device *mac_dev;
  385. struct fman_port *rxport;
  386. struct dpaa_priv *priv;
  387. priv = netdev_priv(net_dev);
  388. mac_dev = priv->mac_dev;
  389. rxport = mac_dev->port[0];
  390. fman_port_use_kg_hash(rxport, enable);
  391. priv->keygen_in_use = enable;
  392. }
  393. static int dpaa_set_hash_opts(struct net_device *dev,
  394. struct ethtool_rxnfc *nfc)
  395. {
  396. int ret = -EINVAL;
  397. /* we support hashing on IPv4/v6 src/dest IP and L4 src/dest port */
  398. if (nfc->data &
  399. ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
  400. return -EINVAL;
  401. switch (nfc->flow_type) {
  402. case TCP_V4_FLOW:
  403. case TCP_V6_FLOW:
  404. case UDP_V4_FLOW:
  405. case UDP_V6_FLOW:
  406. case IPV4_FLOW:
  407. case IPV6_FLOW:
  408. case SCTP_V4_FLOW:
  409. case SCTP_V6_FLOW:
  410. case AH_ESP_V4_FLOW:
  411. case AH_ESP_V6_FLOW:
  412. case AH_V4_FLOW:
  413. case AH_V6_FLOW:
  414. case ESP_V4_FLOW:
  415. case ESP_V6_FLOW:
  416. dpaa_set_hash(dev, !!nfc->data);
  417. ret = 0;
  418. break;
  419. default:
  420. break;
  421. }
  422. return ret;
  423. }
  424. static int dpaa_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  425. {
  426. int ret = -EOPNOTSUPP;
  427. switch (cmd->cmd) {
  428. case ETHTOOL_SRXFH:
  429. ret = dpaa_set_hash_opts(dev, cmd);
  430. break;
  431. default:
  432. break;
  433. }
  434. return ret;
  435. }
  436. const struct ethtool_ops dpaa_ethtool_ops = {
  437. .get_drvinfo = dpaa_get_drvinfo,
  438. .get_msglevel = dpaa_get_msglevel,
  439. .set_msglevel = dpaa_set_msglevel,
  440. .nway_reset = dpaa_nway_reset,
  441. .get_pauseparam = dpaa_get_pauseparam,
  442. .set_pauseparam = dpaa_set_pauseparam,
  443. .get_link = ethtool_op_get_link,
  444. .get_sset_count = dpaa_get_sset_count,
  445. .get_ethtool_stats = dpaa_get_ethtool_stats,
  446. .get_strings = dpaa_get_strings,
  447. .get_link_ksettings = dpaa_get_link_ksettings,
  448. .set_link_ksettings = dpaa_set_link_ksettings,
  449. .get_rxnfc = dpaa_get_rxnfc,
  450. .set_rxnfc = dpaa_set_rxnfc,
  451. };