gianfar_ethtool.c 16 KB

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