ucc_geth_ethtool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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-collision",
  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. phy_ethtool_ksettings_get(phydev, cmd);
  107. return 0;
  108. }
  109. static int
  110. uec_set_ksettings(struct net_device *netdev,
  111. const struct ethtool_link_ksettings *cmd)
  112. {
  113. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  114. struct phy_device *phydev = ugeth->phydev;
  115. if (!phydev)
  116. return -ENODEV;
  117. return phy_ethtool_ksettings_set(phydev, cmd);
  118. }
  119. static void
  120. uec_get_pauseparam(struct net_device *netdev,
  121. struct ethtool_pauseparam *pause)
  122. {
  123. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  124. pause->autoneg = ugeth->phydev->autoneg;
  125. if (ugeth->ug_info->receiveFlowControl)
  126. pause->rx_pause = 1;
  127. if (ugeth->ug_info->transmitFlowControl)
  128. pause->tx_pause = 1;
  129. }
  130. static int
  131. uec_set_pauseparam(struct net_device *netdev,
  132. struct ethtool_pauseparam *pause)
  133. {
  134. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  135. int ret = 0;
  136. ugeth->ug_info->receiveFlowControl = pause->rx_pause;
  137. ugeth->ug_info->transmitFlowControl = pause->tx_pause;
  138. if (ugeth->phydev->autoneg) {
  139. if (netif_running(netdev)) {
  140. /* FIXME: automatically restart */
  141. netdev_info(netdev, "Please re-open the interface\n");
  142. }
  143. } else {
  144. struct ucc_geth_info *ug_info = ugeth->ug_info;
  145. ret = init_flow_control_params(ug_info->aufc,
  146. ug_info->receiveFlowControl,
  147. ug_info->transmitFlowControl,
  148. ug_info->pausePeriod,
  149. ug_info->extensionField,
  150. &ugeth->uccf->uf_regs->upsmr,
  151. &ugeth->ug_regs->uempr,
  152. &ugeth->ug_regs->maccfg1);
  153. }
  154. return ret;
  155. }
  156. static uint32_t
  157. uec_get_msglevel(struct net_device *netdev)
  158. {
  159. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  160. return ugeth->msg_enable;
  161. }
  162. static void
  163. uec_set_msglevel(struct net_device *netdev, uint32_t data)
  164. {
  165. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  166. ugeth->msg_enable = data;
  167. }
  168. static int
  169. uec_get_regs_len(struct net_device *netdev)
  170. {
  171. return sizeof(struct ucc_geth);
  172. }
  173. static void
  174. uec_get_regs(struct net_device *netdev,
  175. struct ethtool_regs *regs, void *p)
  176. {
  177. int i;
  178. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  179. u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
  180. u32 *buff = p;
  181. for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
  182. buff[i] = in_be32(&ug_regs[i]);
  183. }
  184. static void
  185. uec_get_ringparam(struct net_device *netdev,
  186. struct ethtool_ringparam *ring)
  187. {
  188. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  189. struct ucc_geth_info *ug_info = ugeth->ug_info;
  190. int queue = 0;
  191. ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  192. ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  193. ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  194. ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
  195. ring->rx_pending = ug_info->bdRingLenRx[queue];
  196. ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
  197. ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
  198. ring->tx_pending = ug_info->bdRingLenTx[queue];
  199. }
  200. static int
  201. uec_set_ringparam(struct net_device *netdev,
  202. struct ethtool_ringparam *ring)
  203. {
  204. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  205. struct ucc_geth_info *ug_info = ugeth->ug_info;
  206. int queue = 0, ret = 0;
  207. if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
  208. netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
  209. UCC_GETH_RX_BD_RING_SIZE_MIN);
  210. return -EINVAL;
  211. }
  212. if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
  213. netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
  214. UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
  215. return -EINVAL;
  216. }
  217. if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
  218. netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
  219. UCC_GETH_TX_BD_RING_SIZE_MIN);
  220. return -EINVAL;
  221. }
  222. if (netif_running(netdev))
  223. return -EBUSY;
  224. ug_info->bdRingLenRx[queue] = ring->rx_pending;
  225. ug_info->bdRingLenTx[queue] = ring->tx_pending;
  226. return ret;
  227. }
  228. static int uec_get_sset_count(struct net_device *netdev, int sset)
  229. {
  230. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  231. u32 stats_mode = ugeth->ug_info->statisticsMode;
  232. int len = 0;
  233. switch (sset) {
  234. case ETH_SS_STATS:
  235. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
  236. len += UEC_HW_STATS_LEN;
  237. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
  238. len += UEC_TX_FW_STATS_LEN;
  239. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
  240. len += UEC_RX_FW_STATS_LEN;
  241. return len;
  242. default:
  243. return -EOPNOTSUPP;
  244. }
  245. }
  246. static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
  247. {
  248. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  249. u32 stats_mode = ugeth->ug_info->statisticsMode;
  250. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
  251. memcpy(buf, hw_stat_gstrings, UEC_HW_STATS_LEN *
  252. ETH_GSTRING_LEN);
  253. buf += UEC_HW_STATS_LEN * ETH_GSTRING_LEN;
  254. }
  255. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
  256. memcpy(buf, tx_fw_stat_gstrings, UEC_TX_FW_STATS_LEN *
  257. ETH_GSTRING_LEN);
  258. buf += UEC_TX_FW_STATS_LEN * ETH_GSTRING_LEN;
  259. }
  260. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
  261. memcpy(buf, rx_fw_stat_gstrings, UEC_RX_FW_STATS_LEN *
  262. ETH_GSTRING_LEN);
  263. }
  264. static void uec_get_ethtool_stats(struct net_device *netdev,
  265. struct ethtool_stats *stats, uint64_t *data)
  266. {
  267. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  268. u32 stats_mode = ugeth->ug_info->statisticsMode;
  269. u32 __iomem *base;
  270. int i, j = 0;
  271. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
  272. if (ugeth->ug_regs)
  273. base = (u32 __iomem *)&ugeth->ug_regs->tx64;
  274. else
  275. base = NULL;
  276. for (i = 0; i < UEC_HW_STATS_LEN; i++)
  277. data[j++] = base ? in_be32(&base[i]) : 0;
  278. }
  279. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
  280. base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
  281. for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
  282. data[j++] = base ? in_be32(&base[i]) : 0;
  283. }
  284. if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
  285. base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
  286. for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
  287. data[j++] = base ? in_be32(&base[i]) : 0;
  288. }
  289. }
  290. /* Report driver information */
  291. static void
  292. uec_get_drvinfo(struct net_device *netdev,
  293. struct ethtool_drvinfo *drvinfo)
  294. {
  295. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  296. strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  297. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  298. strlcpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
  299. }
  300. #ifdef CONFIG_PM
  301. static void uec_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  302. {
  303. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  304. struct phy_device *phydev = ugeth->phydev;
  305. if (phydev && phydev->irq)
  306. wol->supported |= WAKE_PHY;
  307. if (qe_alive_during_sleep())
  308. wol->supported |= WAKE_MAGIC;
  309. wol->wolopts = ugeth->wol_en;
  310. }
  311. static int uec_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  312. {
  313. struct ucc_geth_private *ugeth = netdev_priv(netdev);
  314. struct phy_device *phydev = ugeth->phydev;
  315. if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
  316. return -EINVAL;
  317. else if (wol->wolopts & WAKE_PHY && (!phydev || !phydev->irq))
  318. return -EINVAL;
  319. else if (wol->wolopts & WAKE_MAGIC && !qe_alive_during_sleep())
  320. return -EINVAL;
  321. ugeth->wol_en = wol->wolopts;
  322. device_set_wakeup_enable(&netdev->dev, ugeth->wol_en);
  323. return 0;
  324. }
  325. #else
  326. #define uec_get_wol NULL
  327. #define uec_set_wol NULL
  328. #endif /* CONFIG_PM */
  329. static const struct ethtool_ops uec_ethtool_ops = {
  330. .get_drvinfo = uec_get_drvinfo,
  331. .get_regs_len = uec_get_regs_len,
  332. .get_regs = uec_get_regs,
  333. .get_msglevel = uec_get_msglevel,
  334. .set_msglevel = uec_set_msglevel,
  335. .nway_reset = phy_ethtool_nway_reset,
  336. .get_link = ethtool_op_get_link,
  337. .get_ringparam = uec_get_ringparam,
  338. .set_ringparam = uec_set_ringparam,
  339. .get_pauseparam = uec_get_pauseparam,
  340. .set_pauseparam = uec_set_pauseparam,
  341. .get_sset_count = uec_get_sset_count,
  342. .get_strings = uec_get_strings,
  343. .get_ethtool_stats = uec_get_ethtool_stats,
  344. .get_wol = uec_get_wol,
  345. .set_wol = uec_set_wol,
  346. .get_ts_info = ethtool_op_get_ts_info,
  347. .get_link_ksettings = uec_get_ksettings,
  348. .set_link_ksettings = uec_set_ksettings,
  349. };
  350. void uec_set_ethtool_ops(struct net_device *netdev)
  351. {
  352. netdev->ethtool_ops = &uec_ethtool_ops;
  353. }