gianfar_ethtool.c 15 KB

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