gianfar_ethtool.c 14 KB

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