gianfar_ethtool.c 15 KB

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