ucc_geth_ethtool.c 11 KB

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