ucc_geth_ethtool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2007 Freescale Semiconductor, Inc. All rights reserved.
  4. *
  5. * Description: QE UCC Gigabit Ethernet Ethtool API Set
  6. *
  7. * Author: Li Yang <leoli@freescale.com>
  8. *
  9. * Limitation:
  10. * Can only get/set settings of the first queue.
  11. * Need to re-open the interface manually after changing some parameters.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/stddef.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/mm.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/mii.h>
  26. #include <linux/phy.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/types.h>
  31. #include "ucc_geth.h"
  32. static const char hw_stat_gstrings[][ETH_GSTRING_LEN] = {
  33. "tx-64-frames",
  34. "tx-65-127-frames",
  35. "tx-128-255-frames",
  36. "rx-64-frames",
  37. "rx-65-127-frames",
  38. "rx-128-255-frames",
  39. "tx-bytes-ok",
  40. "tx-pause-frames",
  41. "tx-multicast-frames",
  42. "tx-broadcast-frames",
  43. "rx-frames",
  44. "rx-bytes-ok",
  45. "rx-bytes-all",
  46. "rx-multicast-frames",
  47. "rx-broadcast-frames",
  48. "stats-counter-carry",
  49. "stats-counter-mask",
  50. "rx-dropped-frames",
  51. };
  52. static const char tx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
  53. "tx-single-collision",
  54. "tx-multiple-collision",
  55. "tx-late-collision",
  56. "tx-aborted-frames",
  57. "tx-lost-frames",
  58. "tx-carrier-sense-errors",
  59. "tx-frames-ok",
  60. "tx-excessive-differ-frames",
  61. "tx-256-511-frames",
  62. "tx-512-1023-frames",
  63. "tx-1024-1518-frames",
  64. "tx-jumbo-frames",
  65. };
  66. static const char rx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
  67. "rx-crc-errors",
  68. "rx-alignment-errors",
  69. "rx-in-range-length-errors",
  70. "rx-out-of-range-length-errors",
  71. "rx-too-long-frames",
  72. "rx-runt",
  73. "rx-very-long-event",
  74. "rx-symbol-errors",
  75. "rx-busy-drop-frames",
  76. "reserved",
  77. "reserved",
  78. "rx-mismatch-drop-frames",
  79. "rx-small-than-64",
  80. "rx-256-511-frames",
  81. "rx-512-1023-frames",
  82. "rx-1024-1518-frames",
  83. "rx-jumbo-frames",
  84. "rx-mac-error-loss",
  85. "rx-pause-frames",
  86. "reserved",
  87. "rx-vlan-removed",
  88. "rx-vlan-replaced",
  89. "rx-vlan-inserted",
  90. "rx-ip-checksum-errors",
  91. };
  92. #define UEC_HW_STATS_LEN ARRAY_SIZE(hw_stat_gstrings)
  93. #define UEC_TX_FW_STATS_LEN ARRAY_SIZE(tx_fw_stat_gstrings)
  94. #define UEC_RX_FW_STATS_LEN ARRAY_SIZE(rx_fw_stat_gstrings)
  95. static int
  96. uec_get_ksettings(struct net_device *netdev, struct ethtool_link_ksettings *cmd)
  97. {
  98. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  99. return phylink_ethtool_ksettings_get(ugeth->phylink, cmd);
  100. }
  101. static int
  102. uec_set_ksettings(struct net_device *netdev,
  103. const struct ethtool_link_ksettings *cmd)
  104. {
  105. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  106. return phylink_ethtool_ksettings_set(ugeth->phylink, cmd);
  107. }
  108. static void
  109. uec_get_pauseparam(struct net_device *netdev,
  110. struct ethtool_pauseparam *pause)
  111. {
  112. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  113. return phylink_ethtool_get_pauseparam(ugeth->phylink, pause);
  114. }
  115. static int
  116. uec_set_pauseparam(struct net_device *netdev,
  117. struct ethtool_pauseparam *pause)
  118. {
  119. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  120. ugeth->ug_info->receiveFlowControl = pause->rx_pause;
  121. ugeth->ug_info->transmitFlowControl = pause->tx_pause;
  122. return phylink_ethtool_set_pauseparam(ugeth->phylink, pause);
  123. }
  124. static uint32_t
  125. uec_get_msglevel(struct net_device *netdev)
  126. {
  127. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  128. return ugeth->msg_enable;
  129. }
  130. static void
  131. uec_set_msglevel(struct net_device *netdev, uint32_t data)
  132. {
  133. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  134. ugeth->msg_enable = data;
  135. }
  136. static int
  137. uec_get_regs_len(struct net_device *netdev)
  138. {
  139. return sizeof(struct ucc_geth);
  140. }
  141. static void
  142. uec_get_regs(struct net_device *netdev,
  143. struct ethtool_regs *regs, void *p)
  144. {
  145. int i;
  146. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  147. u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
  148. u32 *buff = p;
  149. for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
  150. buff[i] = in_be32(&ug_regs[i]);
  151. }
  152. static void
  153. uec_get_ringparam(struct net_device *netdev,
  154. struct ethtool_ringparam *ring,
  155. struct kernel_ethtool_ringparam *kernel_ring,
  156. struct netlink_ext_ack *extack)
  157. {
  158. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  159. struct ucc_geth_info *ug_info = ugeth->ug_info;
  160. int queue = 0;
  161. ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  162. ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  163. ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  164. ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  165. ring->rx_pending = ug_info->bdRingLenRx[queue];
  166. ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
  167. ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
  168. ring->tx_pending = ug_info->bdRingLenTx[queue];
  169. }
  170. static int
  171. uec_set_ringparam(struct net_device *netdev,
  172. struct ethtool_ringparam *ring,
  173. struct kernel_ethtool_ringparam *kernel_ring,
  174. struct netlink_ext_ack *extack)
  175. {
  176. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  177. struct ucc_geth_info *ug_info = ugeth->ug_info;
  178. int queue = 0, ret = 0;
  179. if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
  180. netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
  181. UCC_GETH_RX_BD_RING_SIZE_MIN);
  182. return -EINVAL;
  183. }
  184. if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
  185. netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
  186. UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
  187. return -EINVAL;
  188. }
  189. if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
  190. netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
  191. UCC_GETH_TX_BD_RING_SIZE_MIN);
  192. return -EINVAL;
  193. }
  194. if (netif_running(netdev))
  195. return -EBUSY;
  196. ug_info->bdRingLenRx[queue] = ring->rx_pending;
  197. ug_info->bdRingLenTx[queue] = ring->tx_pending;
  198. return ret;
  199. }
  200. static int uec_get_sset_count(struct net_device *netdev, int sset)
  201. {
  202. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  203. u32 stats_mode = ugeth->ug_info->statisticsMode;
  204. int len = 0;
  205. switch (sset) {
  206. case ETH_SS_STATS:
  207. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
  208. len += UEC_HW_STATS_LEN;
  209. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
  210. len += UEC_TX_FW_STATS_LEN;
  211. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
  212. len += UEC_RX_FW_STATS_LEN;
  213. return len;
  214. default:
  215. return -EOPNOTSUPP;
  216. }
  217. }
  218. static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
  219. {
  220. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  221. u32 stats_mode = ugeth->ug_info->statisticsMode;
  222. int i;
  223. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
  224. for (i = 0; i < UEC_HW_STATS_LEN; i++)
  225. ethtool_puts(&buf, hw_stat_gstrings[i]);
  226. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
  227. for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
  228. ethtool_puts(&buf, tx_fw_stat_gstrings[i]);
  229. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
  230. for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
  231. ethtool_puts(&buf, rx_fw_stat_gstrings[i]);
  232. }
  233. static void uec_get_ethtool_stats(struct net_device *netdev,
  234. struct ethtool_stats *stats, uint64_t *data)
  235. {
  236. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  237. u32 stats_mode = ugeth->ug_info->statisticsMode;
  238. u32 __iomem *base;
  239. int i, j = 0;
  240. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
  241. if (ugeth->ug_regs)
  242. base = (u32 __iomem *)&ugeth->ug_regs->tx64;
  243. else
  244. base = NULL;
  245. for (i = 0; i < UEC_HW_STATS_LEN; i++)
  246. data[j++] = base ? in_be32(&base[i]) : 0;
  247. }
  248. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
  249. base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
  250. for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
  251. data[j++] = base ? in_be32(&base[i]) : 0;
  252. }
  253. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
  254. base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
  255. for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
  256. data[j++] = base ? in_be32(&base[i]) : 0;
  257. }
  258. }
  259. /* Report driver information */
  260. static void
  261. uec_get_drvinfo(struct net_device *netdev,
  262. struct ethtool_drvinfo *drvinfo)
  263. {
  264. strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  265. strscpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
  266. }
  267. #ifdef CONFIG_PM
  268. static void uec_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  269. {
  270. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  271. phylink_ethtool_get_wol(ugeth->phylink, wol);
  272. if (qe_alive_during_sleep())
  273. wol->supported |= WAKE_MAGIC;
  274. wol->wolopts |= ugeth->wol_en;
  275. }
  276. static int uec_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  277. {
  278. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  279. int ret = 0;
  280. ret = phylink_ethtool_set_wol(ugeth->phylink, wol);
  281. if (ret == -EOPNOTSUPP) {
  282. ugeth->phy_wol_en = 0;
  283. } else if (ret) {
  284. return ret;
  285. } else {
  286. ugeth->phy_wol_en = wol->wolopts;
  287. goto out;
  288. }
  289. /* If the PHY isn't handling the WoL and the MAC is asked to more than
  290. * WAKE_MAGIC, error-out
  291. */
  292. if (!ugeth->phy_wol_en &&
  293. wol->wolopts & ~WAKE_MAGIC)
  294. return -EINVAL;
  295. if (wol->wolopts & WAKE_MAGIC &&
  296. !qe_alive_during_sleep())
  297. return -EINVAL;
  298. out:
  299. ugeth->wol_en = wol->wolopts;
  300. device_set_wakeup_enable(&netdev->dev, ugeth->wol_en);
  301. return 0;
  302. }
  303. #else
  304. #define uec_get_wol NULL
  305. #define uec_set_wol NULL
  306. #endif /* CONFIG_PM */
  307. static const struct ethtool_ops uec_ethtool_ops = {
  308. .get_drvinfo = uec_get_drvinfo,
  309. .get_regs_len = uec_get_regs_len,
  310. .get_regs = uec_get_regs,
  311. .get_msglevel = uec_get_msglevel,
  312. .set_msglevel = uec_set_msglevel,
  313. .nway_reset = phy_ethtool_nway_reset,
  314. .get_link = ethtool_op_get_link,
  315. .get_ringparam = uec_get_ringparam,
  316. .set_ringparam = uec_set_ringparam,
  317. .get_pauseparam = uec_get_pauseparam,
  318. .set_pauseparam = uec_set_pauseparam,
  319. .get_sset_count = uec_get_sset_count,
  320. .get_strings = uec_get_strings,
  321. .get_ethtool_stats = uec_get_ethtool_stats,
  322. .get_wol = uec_get_wol,
  323. .set_wol = uec_set_wol,
  324. .get_ts_info = ethtool_op_get_ts_info,
  325. .get_link_ksettings = uec_get_ksettings,
  326. .set_link_ksettings = uec_set_ksettings,
  327. };
  328. void uec_set_ethtool_ops(struct net_device *netdev)
  329. {
  330. netdev->ethtool_ops = &uec_ethtool_ops;
  331. }