gianfar_ethtool.c 15 KB

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