gianfar_ethtool.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * drivers/net/gianfar_ethtool.c
  3. *
  4. * Gianfar Ethernet Driver
  5. * Ethtool support for Gianfar Enet
  6. * Based on e1000 ethtool support
  7. *
  8. * Author: Andy Fleming
  9. * Maintainer: Kumar Gala
  10. *
  11. * Copyright (c) 2003,2004 Freescale Semiconductor, Inc.
  12. *
  13. * This software may be used and distributed according to
  14. * the terms of the GNU Public License, Version 2, incorporated herein
  15. * by reference.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mm.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/module.h>
  33. #include <linux/crc32.h>
  34. #include <asm/types.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/mii.h>
  37. #include <linux/phy.h>
  38. #include "gianfar.h"
  39. extern void gfar_start(struct net_device *dev);
  40. extern int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
  41. #define GFAR_MAX_COAL_USECS 0xffff
  42. #define GFAR_MAX_COAL_FRAMES 0xff
  43. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  44. u64 * buf);
  45. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
  46. static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
  47. static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
  48. static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
  49. static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
  50. static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
  51. static char stat_gstrings[][ETH_GSTRING_LEN] = {
  52. "rx-dropped-by-kernel",
  53. "rx-large-frame-errors",
  54. "rx-short-frame-errors",
  55. "rx-non-octet-errors",
  56. "rx-crc-errors",
  57. "rx-overrun-errors",
  58. "rx-busy-errors",
  59. "rx-babbling-errors",
  60. "rx-truncated-frames",
  61. "ethernet-bus-error",
  62. "tx-babbling-errors",
  63. "tx-underrun-errors",
  64. "rx-skb-missing-errors",
  65. "tx-timeout-errors",
  66. "tx-rx-64-frames",
  67. "tx-rx-65-127-frames",
  68. "tx-rx-128-255-frames",
  69. "tx-rx-256-511-frames",
  70. "tx-rx-512-1023-frames",
  71. "tx-rx-1024-1518-frames",
  72. "tx-rx-1519-1522-good-vlan",
  73. "rx-bytes",
  74. "rx-packets",
  75. "rx-fcs-errors",
  76. "receive-multicast-packet",
  77. "receive-broadcast-packet",
  78. "rx-control-frame-packets",
  79. "rx-pause-frame-packets",
  80. "rx-unknown-op-code",
  81. "rx-alignment-error",
  82. "rx-frame-length-error",
  83. "rx-code-error",
  84. "rx-carrier-sense-error",
  85. "rx-undersize-packets",
  86. "rx-oversize-packets",
  87. "rx-fragmented-frames",
  88. "rx-jabber-frames",
  89. "rx-dropped-frames",
  90. "tx-byte-counter",
  91. "tx-packets",
  92. "tx-multicast-packets",
  93. "tx-broadcast-packets",
  94. "tx-pause-control-frames",
  95. "tx-deferral-packets",
  96. "tx-excessive-deferral-packets",
  97. "tx-single-collision-packets",
  98. "tx-multiple-collision-packets",
  99. "tx-late-collision-packets",
  100. "tx-excessive-collision-packets",
  101. "tx-total-collision",
  102. "reserved",
  103. "tx-dropped-frames",
  104. "tx-jabber-frames",
  105. "tx-fcs-errors",
  106. "tx-control-frames",
  107. "tx-oversize-frames",
  108. "tx-undersize-frames",
  109. "tx-fragmented-frames",
  110. };
  111. /* Fill in a buffer with the strings which correspond to the
  112. * stats */
  113. static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
  114. {
  115. struct gfar_private *priv = netdev_priv(dev);
  116. if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  117. memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
  118. else
  119. memcpy(buf, stat_gstrings,
  120. GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
  121. }
  122. /* Fill in an array of 64-bit statistics from various sources.
  123. * This array will be appended to the end of the ethtool_stats
  124. * structure, and returned to user space
  125. */
  126. static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
  127. {
  128. int i;
  129. struct gfar_private *priv = netdev_priv(dev);
  130. u64 *extra = (u64 *) & priv->extra_stats;
  131. if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  132. u32 __iomem *rmon = (u32 __iomem *) & priv->regs->rmon;
  133. struct gfar_stats *stats = (struct gfar_stats *) buf;
  134. for (i = 0; i < GFAR_RMON_LEN; i++)
  135. stats->rmon[i] = (u64) gfar_read(&rmon[i]);
  136. for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
  137. stats->extra[i] = extra[i];
  138. } else
  139. for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
  140. buf[i] = extra[i];
  141. }
  142. static int gfar_sset_count(struct net_device *dev, int sset)
  143. {
  144. struct gfar_private *priv = netdev_priv(dev);
  145. switch (sset) {
  146. case ETH_SS_STATS:
  147. if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
  148. return GFAR_STATS_LEN;
  149. else
  150. return GFAR_EXTRA_STATS_LEN;
  151. default:
  152. return -EOPNOTSUPP;
  153. }
  154. }
  155. /* Fills in the drvinfo structure with some basic info */
  156. static void gfar_gdrvinfo(struct net_device *dev, struct
  157. ethtool_drvinfo *drvinfo)
  158. {
  159. strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
  160. strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
  161. strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
  162. strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
  163. drvinfo->regdump_len = 0;
  164. drvinfo->eedump_len = 0;
  165. }
  166. static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
  167. {
  168. struct gfar_private *priv = netdev_priv(dev);
  169. struct phy_device *phydev = priv->phydev;
  170. if (NULL == phydev)
  171. return -ENODEV;
  172. return phy_ethtool_sset(phydev, cmd);
  173. }
  174. /* Return the current settings in the ethtool_cmd structure */
  175. static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  176. {
  177. struct gfar_private *priv = netdev_priv(dev);
  178. struct phy_device *phydev = priv->phydev;
  179. if (NULL == phydev)
  180. return -ENODEV;
  181. cmd->maxtxpkt = priv->txcount;
  182. cmd->maxrxpkt = priv->rxcount;
  183. return phy_ethtool_gset(phydev, cmd);
  184. }
  185. /* Return the length of the register structure */
  186. static int gfar_reglen(struct net_device *dev)
  187. {
  188. return sizeof (struct gfar);
  189. }
  190. /* Return a dump of the GFAR register space */
  191. static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
  192. {
  193. int i;
  194. struct gfar_private *priv = netdev_priv(dev);
  195. u32 __iomem *theregs = (u32 __iomem *) priv->regs;
  196. u32 *buf = (u32 *) regbuf;
  197. for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
  198. buf[i] = gfar_read(&theregs[i]);
  199. }
  200. /* Convert microseconds to ethernet clock ticks, which changes
  201. * depending on what speed the controller is running at */
  202. static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
  203. {
  204. unsigned int count;
  205. /* The timer is different, depending on the interface speed */
  206. switch (priv->phydev->speed) {
  207. case SPEED_1000:
  208. count = GFAR_GBIT_TIME;
  209. break;
  210. case SPEED_100:
  211. count = GFAR_100_TIME;
  212. break;
  213. case SPEED_10:
  214. default:
  215. count = GFAR_10_TIME;
  216. break;
  217. }
  218. /* Make sure we return a number greater than 0
  219. * if usecs > 0 */
  220. return ((usecs * 1000 + count - 1) / count);
  221. }
  222. /* Convert ethernet clock ticks to microseconds */
  223. static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
  224. {
  225. unsigned int count;
  226. /* The timer is different, depending on the interface speed */
  227. switch (priv->phydev->speed) {
  228. case SPEED_1000:
  229. count = GFAR_GBIT_TIME;
  230. break;
  231. case SPEED_100:
  232. count = GFAR_100_TIME;
  233. break;
  234. case SPEED_10:
  235. default:
  236. count = GFAR_10_TIME;
  237. break;
  238. }
  239. /* Make sure we return a number greater than 0 */
  240. /* if ticks is > 0 */
  241. return ((ticks * count) / 1000);
  242. }
  243. /* Get the coalescing parameters, and put them in the cvals
  244. * structure. */
  245. static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
  246. {
  247. struct gfar_private *priv = netdev_priv(dev);
  248. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  249. return -EOPNOTSUPP;
  250. if (NULL == priv->phydev)
  251. return -ENODEV;
  252. cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
  253. cvals->rx_max_coalesced_frames = priv->rxcount;
  254. cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
  255. cvals->tx_max_coalesced_frames = priv->txcount;
  256. cvals->use_adaptive_rx_coalesce = 0;
  257. cvals->use_adaptive_tx_coalesce = 0;
  258. cvals->pkt_rate_low = 0;
  259. cvals->rx_coalesce_usecs_low = 0;
  260. cvals->rx_max_coalesced_frames_low = 0;
  261. cvals->tx_coalesce_usecs_low = 0;
  262. cvals->tx_max_coalesced_frames_low = 0;
  263. /* When the packet rate is below pkt_rate_high but above
  264. * pkt_rate_low (both measured in packets per second) the
  265. * normal {rx,tx}_* coalescing parameters are used.
  266. */
  267. /* When the packet rate is (measured in packets per second)
  268. * is above pkt_rate_high, the {rx,tx}_*_high parameters are
  269. * used.
  270. */
  271. cvals->pkt_rate_high = 0;
  272. cvals->rx_coalesce_usecs_high = 0;
  273. cvals->rx_max_coalesced_frames_high = 0;
  274. cvals->tx_coalesce_usecs_high = 0;
  275. cvals->tx_max_coalesced_frames_high = 0;
  276. /* How often to do adaptive coalescing packet rate sampling,
  277. * measured in seconds. Must not be zero.
  278. */
  279. cvals->rate_sample_interval = 0;
  280. return 0;
  281. }
  282. /* Change the coalescing values.
  283. * Both cvals->*_usecs and cvals->*_frames have to be > 0
  284. * in order for coalescing to be active
  285. */
  286. static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
  287. {
  288. struct gfar_private *priv = netdev_priv(dev);
  289. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
  290. return -EOPNOTSUPP;
  291. /* Set up rx coalescing */
  292. if ((cvals->rx_coalesce_usecs == 0) ||
  293. (cvals->rx_max_coalesced_frames == 0))
  294. priv->rxcoalescing = 0;
  295. else
  296. priv->rxcoalescing = 1;
  297. if (NULL == priv->phydev)
  298. return -ENODEV;
  299. /* Check the bounds of the values */
  300. if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  301. pr_info("Coalescing is limited to %d microseconds\n",
  302. GFAR_MAX_COAL_USECS);
  303. return -EINVAL;
  304. }
  305. if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  306. pr_info("Coalescing is limited to %d frames\n",
  307. GFAR_MAX_COAL_FRAMES);
  308. return -EINVAL;
  309. }
  310. priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
  311. priv->rxcount = cvals->rx_max_coalesced_frames;
  312. /* Set up tx coalescing */
  313. if ((cvals->tx_coalesce_usecs == 0) ||
  314. (cvals->tx_max_coalesced_frames == 0))
  315. priv->txcoalescing = 0;
  316. else
  317. priv->txcoalescing = 1;
  318. /* Check the bounds of the values */
  319. if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
  320. pr_info("Coalescing is limited to %d microseconds\n",
  321. GFAR_MAX_COAL_USECS);
  322. return -EINVAL;
  323. }
  324. if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
  325. pr_info("Coalescing is limited to %d frames\n",
  326. GFAR_MAX_COAL_FRAMES);
  327. return -EINVAL;
  328. }
  329. priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
  330. priv->txcount = cvals->tx_max_coalesced_frames;
  331. if (priv->rxcoalescing)
  332. gfar_write(&priv->regs->rxic,
  333. mk_ic_value(priv->rxcount, priv->rxtime));
  334. else
  335. gfar_write(&priv->regs->rxic, 0);
  336. if (priv->txcoalescing)
  337. gfar_write(&priv->regs->txic,
  338. mk_ic_value(priv->txcount, priv->txtime));
  339. else
  340. gfar_write(&priv->regs->txic, 0);
  341. return 0;
  342. }
  343. /* Fills in rvals with the current ring parameters. Currently,
  344. * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
  345. * jumbo are ignored by the driver */
  346. static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
  347. {
  348. struct gfar_private *priv = netdev_priv(dev);
  349. rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
  350. rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
  351. rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
  352. rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
  353. /* Values changeable by the user. The valid values are
  354. * in the range 1 to the "*_max_pending" counterpart above.
  355. */
  356. rvals->rx_pending = priv->rx_ring_size;
  357. rvals->rx_mini_pending = priv->rx_ring_size;
  358. rvals->rx_jumbo_pending = priv->rx_ring_size;
  359. rvals->tx_pending = priv->tx_ring_size;
  360. }
  361. /* Change the current ring parameters, stopping the controller if
  362. * necessary so that we don't mess things up while we're in
  363. * motion. We wait for the ring to be clean before reallocating
  364. * the rings. */
  365. static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
  366. {
  367. struct gfar_private *priv = netdev_priv(dev);
  368. int err = 0;
  369. if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
  370. return -EINVAL;
  371. if (!is_power_of_2(rvals->rx_pending)) {
  372. printk("%s: Ring sizes must be a power of 2\n",
  373. dev->name);
  374. return -EINVAL;
  375. }
  376. if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
  377. return -EINVAL;
  378. if (!is_power_of_2(rvals->tx_pending)) {
  379. printk("%s: Ring sizes must be a power of 2\n",
  380. dev->name);
  381. return -EINVAL;
  382. }
  383. if (dev->flags & IFF_UP) {
  384. unsigned long flags;
  385. /* Halt TX and RX, and process the frames which
  386. * have already been received */
  387. spin_lock_irqsave(&priv->txlock, flags);
  388. spin_lock(&priv->rxlock);
  389. gfar_halt(dev);
  390. gfar_clean_rx_ring(dev, priv->rx_ring_size);
  391. spin_unlock(&priv->rxlock);
  392. spin_unlock_irqrestore(&priv->txlock, flags);
  393. /* Now we take down the rings to rebuild them */
  394. stop_gfar(dev);
  395. }
  396. /* Change the size */
  397. priv->rx_ring_size = rvals->rx_pending;
  398. priv->tx_ring_size = rvals->tx_pending;
  399. /* Rebuild the rings with the new size */
  400. if (dev->flags & IFF_UP)
  401. err = startup_gfar(dev);
  402. return err;
  403. }
  404. static int gfar_set_rx_csum(struct net_device *dev, uint32_t data)
  405. {
  406. struct gfar_private *priv = netdev_priv(dev);
  407. int err = 0;
  408. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
  409. return -EOPNOTSUPP;
  410. if (dev->flags & IFF_UP) {
  411. unsigned long flags;
  412. /* Halt TX and RX, and process the frames which
  413. * have already been received */
  414. spin_lock_irqsave(&priv->txlock, flags);
  415. spin_lock(&priv->rxlock);
  416. gfar_halt(dev);
  417. gfar_clean_rx_ring(dev, priv->rx_ring_size);
  418. spin_unlock(&priv->rxlock);
  419. spin_unlock_irqrestore(&priv->txlock, flags);
  420. /* Now we take down the rings to rebuild them */
  421. stop_gfar(dev);
  422. }
  423. priv->rx_csum_enable = data;
  424. if (dev->flags & IFF_UP)
  425. err = startup_gfar(dev);
  426. return err;
  427. }
  428. static uint32_t gfar_get_rx_csum(struct net_device *dev)
  429. {
  430. struct gfar_private *priv = netdev_priv(dev);
  431. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
  432. return 0;
  433. return priv->rx_csum_enable;
  434. }
  435. static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
  436. {
  437. unsigned long flags;
  438. struct gfar_private *priv = netdev_priv(dev);
  439. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
  440. return -EOPNOTSUPP;
  441. spin_lock_irqsave(&priv->txlock, flags);
  442. gfar_halt(dev);
  443. if (data)
  444. dev->features |= NETIF_F_IP_CSUM;
  445. else
  446. dev->features &= ~NETIF_F_IP_CSUM;
  447. gfar_start(dev);
  448. spin_unlock_irqrestore(&priv->txlock, flags);
  449. return 0;
  450. }
  451. static uint32_t gfar_get_tx_csum(struct net_device *dev)
  452. {
  453. struct gfar_private *priv = netdev_priv(dev);
  454. if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
  455. return 0;
  456. return (dev->features & NETIF_F_IP_CSUM) != 0;
  457. }
  458. static uint32_t gfar_get_msglevel(struct net_device *dev)
  459. {
  460. struct gfar_private *priv = netdev_priv(dev);
  461. return priv->msg_enable;
  462. }
  463. static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
  464. {
  465. struct gfar_private *priv = netdev_priv(dev);
  466. priv->msg_enable = data;
  467. }
  468. const struct ethtool_ops gfar_ethtool_ops = {
  469. .get_settings = gfar_gsettings,
  470. .set_settings = gfar_ssettings,
  471. .get_drvinfo = gfar_gdrvinfo,
  472. .get_regs_len = gfar_reglen,
  473. .get_regs = gfar_get_regs,
  474. .get_link = ethtool_op_get_link,
  475. .get_coalesce = gfar_gcoalesce,
  476. .set_coalesce = gfar_scoalesce,
  477. .get_ringparam = gfar_gringparam,
  478. .set_ringparam = gfar_sringparam,
  479. .get_strings = gfar_gstrings,
  480. .get_sset_count = gfar_sset_count,
  481. .get_ethtool_stats = gfar_fill_stats,
  482. .get_rx_csum = gfar_get_rx_csum,
  483. .get_tx_csum = gfar_get_tx_csum,
  484. .set_rx_csum = gfar_set_rx_csum,
  485. .set_tx_csum = gfar_set_tx_csum,
  486. .get_msglevel = gfar_get_msglevel,
  487. .set_msglevel = gfar_set_msglevel,
  488. };