dpaa_ethtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_settings(struct net_device *net_dev,
  67. struct ethtool_cmd *et_cmd)
  68. {
  69. int err;
  70. if (!net_dev->phydev) {
  71. netdev_dbg(net_dev, "phy device not initialized\n");
  72. return 0;
  73. }
  74. err = phy_ethtool_gset(net_dev->phydev, et_cmd);
  75. return err;
  76. }
  77. static int dpaa_set_settings(struct net_device *net_dev,
  78. struct ethtool_cmd *et_cmd)
  79. {
  80. int err;
  81. if (!net_dev->phydev) {
  82. netdev_err(net_dev, "phy device not initialized\n");
  83. return -ENODEV;
  84. }
  85. err = phy_ethtool_sset(net_dev->phydev, et_cmd);
  86. if (err < 0)
  87. netdev_err(net_dev, "phy_ethtool_sset() = %d\n", err);
  88. return err;
  89. }
  90. static void dpaa_get_drvinfo(struct net_device *net_dev,
  91. struct ethtool_drvinfo *drvinfo)
  92. {
  93. int len;
  94. strlcpy(drvinfo->driver, KBUILD_MODNAME,
  95. sizeof(drvinfo->driver));
  96. len = snprintf(drvinfo->version, sizeof(drvinfo->version),
  97. "%X", 0);
  98. len = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  99. "%X", 0);
  100. if (len >= sizeof(drvinfo->fw_version)) {
  101. /* Truncated output */
  102. netdev_notice(net_dev, "snprintf() = %d\n", len);
  103. }
  104. strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
  105. sizeof(drvinfo->bus_info));
  106. }
  107. static u32 dpaa_get_msglevel(struct net_device *net_dev)
  108. {
  109. return ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable;
  110. }
  111. static void dpaa_set_msglevel(struct net_device *net_dev,
  112. u32 msg_enable)
  113. {
  114. ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable = msg_enable;
  115. }
  116. static int dpaa_nway_reset(struct net_device *net_dev)
  117. {
  118. int err;
  119. if (!net_dev->phydev) {
  120. netdev_err(net_dev, "phy device not initialized\n");
  121. return -ENODEV;
  122. }
  123. err = 0;
  124. if (net_dev->phydev->autoneg) {
  125. err = phy_start_aneg(net_dev->phydev);
  126. if (err < 0)
  127. netdev_err(net_dev, "phy_start_aneg() = %d\n",
  128. err);
  129. }
  130. return err;
  131. }
  132. static void dpaa_get_pauseparam(struct net_device *net_dev,
  133. struct ethtool_pauseparam *epause)
  134. {
  135. struct mac_device *mac_dev;
  136. struct dpaa_priv *priv;
  137. priv = netdev_priv(net_dev);
  138. mac_dev = priv->mac_dev;
  139. if (!net_dev->phydev) {
  140. netdev_err(net_dev, "phy device not initialized\n");
  141. return;
  142. }
  143. epause->autoneg = mac_dev->autoneg_pause;
  144. epause->rx_pause = mac_dev->rx_pause_active;
  145. epause->tx_pause = mac_dev->tx_pause_active;
  146. }
  147. static int dpaa_set_pauseparam(struct net_device *net_dev,
  148. struct ethtool_pauseparam *epause)
  149. {
  150. struct mac_device *mac_dev;
  151. struct phy_device *phydev;
  152. bool rx_pause, tx_pause;
  153. struct dpaa_priv *priv;
  154. u32 newadv, oldadv;
  155. int err;
  156. priv = netdev_priv(net_dev);
  157. mac_dev = priv->mac_dev;
  158. phydev = net_dev->phydev;
  159. if (!phydev) {
  160. netdev_err(net_dev, "phy device not initialized\n");
  161. return -ENODEV;
  162. }
  163. if (!(phydev->supported & SUPPORTED_Pause) ||
  164. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  165. (epause->rx_pause != epause->tx_pause)))
  166. return -EINVAL;
  167. /* The MAC should know how to handle PAUSE frame autonegotiation before
  168. * adjust_link is triggered by a forced renegotiation of sym/asym PAUSE
  169. * settings.
  170. */
  171. mac_dev->autoneg_pause = !!epause->autoneg;
  172. mac_dev->rx_pause_req = !!epause->rx_pause;
  173. mac_dev->tx_pause_req = !!epause->tx_pause;
  174. /* Determine the sym/asym advertised PAUSE capabilities from the desired
  175. * rx/tx pause settings.
  176. */
  177. newadv = 0;
  178. if (epause->rx_pause)
  179. newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  180. if (epause->tx_pause)
  181. newadv |= ADVERTISED_Asym_Pause;
  182. oldadv = phydev->advertising &
  183. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  184. /* If there are differences between the old and the new advertised
  185. * values, restart PHY autonegotiation and advertise the new values.
  186. */
  187. if (oldadv != newadv) {
  188. phydev->advertising &= ~(ADVERTISED_Pause
  189. | ADVERTISED_Asym_Pause);
  190. phydev->advertising |= newadv;
  191. if (phydev->autoneg) {
  192. err = phy_start_aneg(phydev);
  193. if (err < 0)
  194. netdev_err(net_dev, "phy_start_aneg() = %d\n",
  195. err);
  196. }
  197. }
  198. fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
  199. err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
  200. if (err < 0)
  201. netdev_err(net_dev, "set_mac_active_pause() = %d\n", err);
  202. return err;
  203. }
  204. static int dpaa_get_sset_count(struct net_device *net_dev, int type)
  205. {
  206. unsigned int total_stats, num_stats;
  207. num_stats = num_online_cpus() + 1;
  208. total_stats = num_stats * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM) +
  209. DPAA_STATS_GLOBAL_LEN;
  210. switch (type) {
  211. case ETH_SS_STATS:
  212. return total_stats;
  213. default:
  214. return -EOPNOTSUPP;
  215. }
  216. }
  217. static void copy_stats(struct dpaa_percpu_priv *percpu_priv, int num_cpus,
  218. int crr_cpu, u64 *bp_count, u64 *data)
  219. {
  220. int num_values = num_cpus + 1;
  221. int crr = 0, j;
  222. /* update current CPU's stats and also add them to the total values */
  223. data[crr * num_values + crr_cpu] = percpu_priv->in_interrupt;
  224. data[crr++ * num_values + num_cpus] += percpu_priv->in_interrupt;
  225. data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_packets;
  226. data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_packets;
  227. data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_packets;
  228. data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_packets;
  229. data[crr * num_values + crr_cpu] = percpu_priv->tx_confirm;
  230. data[crr++ * num_values + num_cpus] += percpu_priv->tx_confirm;
  231. data[crr * num_values + crr_cpu] = percpu_priv->tx_frag_skbuffs;
  232. data[crr++ * num_values + num_cpus] += percpu_priv->tx_frag_skbuffs;
  233. data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_errors;
  234. data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_errors;
  235. data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_errors;
  236. data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_errors;
  237. for (j = 0; j < DPAA_BPS_NUM; j++) {
  238. data[crr * num_values + crr_cpu] = bp_count[j];
  239. data[crr++ * num_values + num_cpus] += bp_count[j];
  240. }
  241. }
  242. static void dpaa_get_ethtool_stats(struct net_device *net_dev,
  243. struct ethtool_stats *stats, u64 *data)
  244. {
  245. u64 bp_count[DPAA_BPS_NUM], cg_time, cg_num;
  246. struct dpaa_percpu_priv *percpu_priv;
  247. struct dpaa_rx_errors rx_errors;
  248. unsigned int num_cpus, offset;
  249. struct dpaa_ern_cnt ern_cnt;
  250. struct dpaa_bp *dpaa_bp;
  251. struct dpaa_priv *priv;
  252. int total_stats, i, j;
  253. bool cg_status;
  254. total_stats = dpaa_get_sset_count(net_dev, ETH_SS_STATS);
  255. priv = netdev_priv(net_dev);
  256. num_cpus = num_online_cpus();
  257. memset(&bp_count, 0, sizeof(bp_count));
  258. memset(&rx_errors, 0, sizeof(struct dpaa_rx_errors));
  259. memset(&ern_cnt, 0, sizeof(struct dpaa_ern_cnt));
  260. memset(data, 0, total_stats * sizeof(u64));
  261. for_each_online_cpu(i) {
  262. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  263. for (j = 0; j < DPAA_BPS_NUM; j++) {
  264. dpaa_bp = priv->dpaa_bps[j];
  265. if (!dpaa_bp->percpu_count)
  266. continue;
  267. bp_count[j] = *(per_cpu_ptr(dpaa_bp->percpu_count, i));
  268. }
  269. rx_errors.dme += percpu_priv->rx_errors.dme;
  270. rx_errors.fpe += percpu_priv->rx_errors.fpe;
  271. rx_errors.fse += percpu_priv->rx_errors.fse;
  272. rx_errors.phe += percpu_priv->rx_errors.phe;
  273. ern_cnt.cg_tdrop += percpu_priv->ern_cnt.cg_tdrop;
  274. ern_cnt.wred += percpu_priv->ern_cnt.wred;
  275. ern_cnt.err_cond += percpu_priv->ern_cnt.err_cond;
  276. ern_cnt.early_window += percpu_priv->ern_cnt.early_window;
  277. ern_cnt.late_window += percpu_priv->ern_cnt.late_window;
  278. ern_cnt.fq_tdrop += percpu_priv->ern_cnt.fq_tdrop;
  279. ern_cnt.fq_retired += percpu_priv->ern_cnt.fq_retired;
  280. ern_cnt.orp_zero += percpu_priv->ern_cnt.orp_zero;
  281. copy_stats(percpu_priv, num_cpus, i, bp_count, data);
  282. }
  283. offset = (num_cpus + 1) * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM);
  284. memcpy(data + offset, &rx_errors, sizeof(struct dpaa_rx_errors));
  285. offset += sizeof(struct dpaa_rx_errors) / sizeof(u64);
  286. memcpy(data + offset, &ern_cnt, sizeof(struct dpaa_ern_cnt));
  287. /* gather congestion related counters */
  288. cg_num = 0;
  289. cg_status = 0;
  290. cg_time = jiffies_to_msecs(priv->cgr_data.congested_jiffies);
  291. if (qman_query_cgr_congested(&priv->cgr_data.cgr, &cg_status) == 0) {
  292. cg_num = priv->cgr_data.cgr_congested_count;
  293. /* reset congestion stats (like QMan API does */
  294. priv->cgr_data.congested_jiffies = 0;
  295. priv->cgr_data.cgr_congested_count = 0;
  296. }
  297. offset += sizeof(struct dpaa_ern_cnt) / sizeof(u64);
  298. data[offset++] = cg_time;
  299. data[offset++] = cg_num;
  300. data[offset++] = cg_status;
  301. }
  302. static void dpaa_get_strings(struct net_device *net_dev, u32 stringset,
  303. u8 *data)
  304. {
  305. unsigned int i, j, num_cpus, size;
  306. char string_cpu[ETH_GSTRING_LEN];
  307. u8 *strings;
  308. memset(string_cpu, 0, sizeof(string_cpu));
  309. strings = data;
  310. num_cpus = num_online_cpus();
  311. size = DPAA_STATS_GLOBAL_LEN * ETH_GSTRING_LEN;
  312. for (i = 0; i < DPAA_STATS_PERCPU_LEN; i++) {
  313. for (j = 0; j < num_cpus; j++) {
  314. snprintf(string_cpu, ETH_GSTRING_LEN, "%s [CPU %d]",
  315. dpaa_stats_percpu[i], j);
  316. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  317. strings += ETH_GSTRING_LEN;
  318. }
  319. snprintf(string_cpu, ETH_GSTRING_LEN, "%s [TOTAL]",
  320. dpaa_stats_percpu[i]);
  321. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  322. strings += ETH_GSTRING_LEN;
  323. }
  324. for (i = 0; i < DPAA_BPS_NUM; i++) {
  325. for (j = 0; j < num_cpus; j++) {
  326. snprintf(string_cpu, ETH_GSTRING_LEN,
  327. "bpool %c [CPU %d]", 'a' + i, j);
  328. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  329. strings += ETH_GSTRING_LEN;
  330. }
  331. snprintf(string_cpu, ETH_GSTRING_LEN, "bpool %c [TOTAL]",
  332. 'a' + i);
  333. memcpy(strings, string_cpu, ETH_GSTRING_LEN);
  334. strings += ETH_GSTRING_LEN;
  335. }
  336. memcpy(strings, dpaa_stats_global, size);
  337. }
  338. const struct ethtool_ops dpaa_ethtool_ops = {
  339. .get_settings = dpaa_get_settings,
  340. .set_settings = dpaa_set_settings,
  341. .get_drvinfo = dpaa_get_drvinfo,
  342. .get_msglevel = dpaa_get_msglevel,
  343. .set_msglevel = dpaa_set_msglevel,
  344. .nway_reset = dpaa_nway_reset,
  345. .get_pauseparam = dpaa_get_pauseparam,
  346. .set_pauseparam = dpaa_set_pauseparam,
  347. .get_link = ethtool_op_get_link,
  348. .get_sset_count = dpaa_get_sset_count,
  349. .get_ethtool_stats = dpaa_get_ethtool_stats,
  350. .get_strings = dpaa_get_strings,
  351. };