ethtool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2008 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/netdevice.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/rtnetlink.h>
  13. #include "net_driver.h"
  14. #include "efx.h"
  15. #include "ethtool.h"
  16. #include "falcon.h"
  17. #include "gmii.h"
  18. #include "mac.h"
  19. static int efx_ethtool_set_tx_csum(struct net_device *net_dev, u32 enable);
  20. struct ethtool_string {
  21. char name[ETH_GSTRING_LEN];
  22. };
  23. struct efx_ethtool_stat {
  24. const char *name;
  25. enum {
  26. EFX_ETHTOOL_STAT_SOURCE_mac_stats,
  27. EFX_ETHTOOL_STAT_SOURCE_nic,
  28. EFX_ETHTOOL_STAT_SOURCE_channel
  29. } source;
  30. unsigned offset;
  31. u64(*get_stat) (void *field); /* Reader function */
  32. };
  33. /* Initialiser for a struct #efx_ethtool_stat with type-checking */
  34. #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
  35. get_stat_function) { \
  36. .name = #stat_name, \
  37. .source = EFX_ETHTOOL_STAT_SOURCE_##source_name, \
  38. .offset = ((((field_type *) 0) == \
  39. &((struct efx_##source_name *)0)->field) ? \
  40. offsetof(struct efx_##source_name, field) : \
  41. offsetof(struct efx_##source_name, field)), \
  42. .get_stat = get_stat_function, \
  43. }
  44. static u64 efx_get_uint_stat(void *field)
  45. {
  46. return *(unsigned int *)field;
  47. }
  48. static u64 efx_get_ulong_stat(void *field)
  49. {
  50. return *(unsigned long *)field;
  51. }
  52. static u64 efx_get_u64_stat(void *field)
  53. {
  54. return *(u64 *) field;
  55. }
  56. static u64 efx_get_atomic_stat(void *field)
  57. {
  58. return atomic_read((atomic_t *) field);
  59. }
  60. #define EFX_ETHTOOL_ULONG_MAC_STAT(field) \
  61. EFX_ETHTOOL_STAT(field, mac_stats, field, \
  62. unsigned long, efx_get_ulong_stat)
  63. #define EFX_ETHTOOL_U64_MAC_STAT(field) \
  64. EFX_ETHTOOL_STAT(field, mac_stats, field, \
  65. u64, efx_get_u64_stat)
  66. #define EFX_ETHTOOL_UINT_NIC_STAT(name) \
  67. EFX_ETHTOOL_STAT(name, nic, n_##name, \
  68. unsigned int, efx_get_uint_stat)
  69. #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field) \
  70. EFX_ETHTOOL_STAT(field, nic, field, \
  71. atomic_t, efx_get_atomic_stat)
  72. #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field) \
  73. EFX_ETHTOOL_STAT(field, channel, n_##field, \
  74. unsigned int, efx_get_uint_stat)
  75. static struct efx_ethtool_stat efx_ethtool_stats[] = {
  76. EFX_ETHTOOL_U64_MAC_STAT(tx_bytes),
  77. EFX_ETHTOOL_U64_MAC_STAT(tx_good_bytes),
  78. EFX_ETHTOOL_U64_MAC_STAT(tx_bad_bytes),
  79. EFX_ETHTOOL_ULONG_MAC_STAT(tx_packets),
  80. EFX_ETHTOOL_ULONG_MAC_STAT(tx_bad),
  81. EFX_ETHTOOL_ULONG_MAC_STAT(tx_pause),
  82. EFX_ETHTOOL_ULONG_MAC_STAT(tx_control),
  83. EFX_ETHTOOL_ULONG_MAC_STAT(tx_unicast),
  84. EFX_ETHTOOL_ULONG_MAC_STAT(tx_multicast),
  85. EFX_ETHTOOL_ULONG_MAC_STAT(tx_broadcast),
  86. EFX_ETHTOOL_ULONG_MAC_STAT(tx_lt64),
  87. EFX_ETHTOOL_ULONG_MAC_STAT(tx_64),
  88. EFX_ETHTOOL_ULONG_MAC_STAT(tx_65_to_127),
  89. EFX_ETHTOOL_ULONG_MAC_STAT(tx_128_to_255),
  90. EFX_ETHTOOL_ULONG_MAC_STAT(tx_256_to_511),
  91. EFX_ETHTOOL_ULONG_MAC_STAT(tx_512_to_1023),
  92. EFX_ETHTOOL_ULONG_MAC_STAT(tx_1024_to_15xx),
  93. EFX_ETHTOOL_ULONG_MAC_STAT(tx_15xx_to_jumbo),
  94. EFX_ETHTOOL_ULONG_MAC_STAT(tx_gtjumbo),
  95. EFX_ETHTOOL_ULONG_MAC_STAT(tx_collision),
  96. EFX_ETHTOOL_ULONG_MAC_STAT(tx_single_collision),
  97. EFX_ETHTOOL_ULONG_MAC_STAT(tx_multiple_collision),
  98. EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_collision),
  99. EFX_ETHTOOL_ULONG_MAC_STAT(tx_deferred),
  100. EFX_ETHTOOL_ULONG_MAC_STAT(tx_late_collision),
  101. EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_deferred),
  102. EFX_ETHTOOL_ULONG_MAC_STAT(tx_non_tcpudp),
  103. EFX_ETHTOOL_ULONG_MAC_STAT(tx_mac_src_error),
  104. EFX_ETHTOOL_ULONG_MAC_STAT(tx_ip_src_error),
  105. EFX_ETHTOOL_U64_MAC_STAT(rx_bytes),
  106. EFX_ETHTOOL_U64_MAC_STAT(rx_good_bytes),
  107. EFX_ETHTOOL_U64_MAC_STAT(rx_bad_bytes),
  108. EFX_ETHTOOL_ULONG_MAC_STAT(rx_packets),
  109. EFX_ETHTOOL_ULONG_MAC_STAT(rx_good),
  110. EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad),
  111. EFX_ETHTOOL_ULONG_MAC_STAT(rx_pause),
  112. EFX_ETHTOOL_ULONG_MAC_STAT(rx_control),
  113. EFX_ETHTOOL_ULONG_MAC_STAT(rx_unicast),
  114. EFX_ETHTOOL_ULONG_MAC_STAT(rx_multicast),
  115. EFX_ETHTOOL_ULONG_MAC_STAT(rx_broadcast),
  116. EFX_ETHTOOL_ULONG_MAC_STAT(rx_lt64),
  117. EFX_ETHTOOL_ULONG_MAC_STAT(rx_64),
  118. EFX_ETHTOOL_ULONG_MAC_STAT(rx_65_to_127),
  119. EFX_ETHTOOL_ULONG_MAC_STAT(rx_128_to_255),
  120. EFX_ETHTOOL_ULONG_MAC_STAT(rx_256_to_511),
  121. EFX_ETHTOOL_ULONG_MAC_STAT(rx_512_to_1023),
  122. EFX_ETHTOOL_ULONG_MAC_STAT(rx_1024_to_15xx),
  123. EFX_ETHTOOL_ULONG_MAC_STAT(rx_15xx_to_jumbo),
  124. EFX_ETHTOOL_ULONG_MAC_STAT(rx_gtjumbo),
  125. EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_lt64),
  126. EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_64_to_15xx),
  127. EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_15xx_to_jumbo),
  128. EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_gtjumbo),
  129. EFX_ETHTOOL_ULONG_MAC_STAT(rx_overflow),
  130. EFX_ETHTOOL_ULONG_MAC_STAT(rx_missed),
  131. EFX_ETHTOOL_ULONG_MAC_STAT(rx_false_carrier),
  132. EFX_ETHTOOL_ULONG_MAC_STAT(rx_symbol_error),
  133. EFX_ETHTOOL_ULONG_MAC_STAT(rx_align_error),
  134. EFX_ETHTOOL_ULONG_MAC_STAT(rx_length_error),
  135. EFX_ETHTOOL_ULONG_MAC_STAT(rx_internal_error),
  136. EFX_ETHTOOL_UINT_NIC_STAT(rx_nodesc_drop_cnt),
  137. EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
  138. EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
  139. EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
  140. EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
  141. EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
  142. };
  143. /* Number of ethtool statistics */
  144. #define EFX_ETHTOOL_NUM_STATS ARRAY_SIZE(efx_ethtool_stats)
  145. /**************************************************************************
  146. *
  147. * Ethtool operations
  148. *
  149. **************************************************************************
  150. */
  151. /* Identify device by flashing LEDs */
  152. static int efx_ethtool_phys_id(struct net_device *net_dev, u32 seconds)
  153. {
  154. struct efx_nic *efx = net_dev->priv;
  155. efx->board_info.blink(efx, 1);
  156. schedule_timeout_interruptible(seconds * HZ);
  157. efx->board_info.blink(efx, 0);
  158. return 0;
  159. }
  160. /* This must be called with rtnl_lock held. */
  161. int efx_ethtool_get_settings(struct net_device *net_dev,
  162. struct ethtool_cmd *ecmd)
  163. {
  164. struct efx_nic *efx = net_dev->priv;
  165. int rc;
  166. mutex_lock(&efx->mac_lock);
  167. rc = falcon_xmac_get_settings(efx, ecmd);
  168. mutex_unlock(&efx->mac_lock);
  169. return rc;
  170. }
  171. /* This must be called with rtnl_lock held. */
  172. int efx_ethtool_set_settings(struct net_device *net_dev,
  173. struct ethtool_cmd *ecmd)
  174. {
  175. struct efx_nic *efx = net_dev->priv;
  176. int rc;
  177. mutex_lock(&efx->mac_lock);
  178. rc = falcon_xmac_set_settings(efx, ecmd);
  179. mutex_unlock(&efx->mac_lock);
  180. if (!rc)
  181. efx_reconfigure_port(efx);
  182. return rc;
  183. }
  184. static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
  185. struct ethtool_drvinfo *info)
  186. {
  187. struct efx_nic *efx = net_dev->priv;
  188. strlcpy(info->driver, EFX_DRIVER_NAME, sizeof(info->driver));
  189. strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
  190. strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
  191. }
  192. static int efx_ethtool_get_stats_count(struct net_device *net_dev)
  193. {
  194. return EFX_ETHTOOL_NUM_STATS;
  195. }
  196. static void efx_ethtool_get_strings(struct net_device *net_dev,
  197. u32 string_set, u8 *strings)
  198. {
  199. struct ethtool_string *ethtool_strings =
  200. (struct ethtool_string *)strings;
  201. int i;
  202. if (string_set == ETH_SS_STATS)
  203. for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++)
  204. strncpy(ethtool_strings[i].name,
  205. efx_ethtool_stats[i].name,
  206. sizeof(ethtool_strings[i].name));
  207. }
  208. static void efx_ethtool_get_stats(struct net_device *net_dev,
  209. struct ethtool_stats *stats,
  210. u64 *data)
  211. {
  212. struct efx_nic *efx = net_dev->priv;
  213. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  214. struct efx_ethtool_stat *stat;
  215. struct efx_channel *channel;
  216. int i;
  217. EFX_BUG_ON_PARANOID(stats->n_stats != EFX_ETHTOOL_NUM_STATS);
  218. /* Update MAC and NIC statistics */
  219. net_dev->get_stats(net_dev);
  220. /* Fill detailed statistics buffer */
  221. for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++) {
  222. stat = &efx_ethtool_stats[i];
  223. switch (stat->source) {
  224. case EFX_ETHTOOL_STAT_SOURCE_mac_stats:
  225. data[i] = stat->get_stat((void *)mac_stats +
  226. stat->offset);
  227. break;
  228. case EFX_ETHTOOL_STAT_SOURCE_nic:
  229. data[i] = stat->get_stat((void *)efx + stat->offset);
  230. break;
  231. case EFX_ETHTOOL_STAT_SOURCE_channel:
  232. data[i] = 0;
  233. efx_for_each_channel(channel, efx)
  234. data[i] += stat->get_stat((void *)channel +
  235. stat->offset);
  236. break;
  237. }
  238. }
  239. }
  240. static int efx_ethtool_set_tx_csum(struct net_device *net_dev, u32 enable)
  241. {
  242. struct efx_nic *efx = net_dev->priv;
  243. int rc;
  244. rc = ethtool_op_set_tx_csum(net_dev, enable);
  245. if (rc)
  246. return rc;
  247. efx_flush_queues(efx);
  248. return 0;
  249. }
  250. static int efx_ethtool_set_rx_csum(struct net_device *net_dev, u32 enable)
  251. {
  252. struct efx_nic *efx = net_dev->priv;
  253. /* No way to stop the hardware doing the checks; we just
  254. * ignore the result.
  255. */
  256. efx->rx_checksum_enabled = (enable ? 1 : 0);
  257. return 0;
  258. }
  259. static u32 efx_ethtool_get_rx_csum(struct net_device *net_dev)
  260. {
  261. struct efx_nic *efx = net_dev->priv;
  262. return efx->rx_checksum_enabled;
  263. }
  264. /* Restart autonegotiation */
  265. static int efx_ethtool_nway_reset(struct net_device *net_dev)
  266. {
  267. struct efx_nic *efx = net_dev->priv;
  268. return mii_nway_restart(&efx->mii);
  269. }
  270. static u32 efx_ethtool_get_link(struct net_device *net_dev)
  271. {
  272. struct efx_nic *efx = net_dev->priv;
  273. return efx->link_up;
  274. }
  275. static int efx_ethtool_get_coalesce(struct net_device *net_dev,
  276. struct ethtool_coalesce *coalesce)
  277. {
  278. struct efx_nic *efx = net_dev->priv;
  279. struct efx_tx_queue *tx_queue;
  280. struct efx_rx_queue *rx_queue;
  281. struct efx_channel *channel;
  282. memset(coalesce, 0, sizeof(*coalesce));
  283. /* Find lowest IRQ moderation across all used TX queues */
  284. coalesce->tx_coalesce_usecs_irq = ~((u32) 0);
  285. efx_for_each_tx_queue(tx_queue, efx) {
  286. channel = tx_queue->channel;
  287. if (channel->irq_moderation < coalesce->tx_coalesce_usecs_irq) {
  288. if (channel->used_flags != EFX_USED_BY_RX_TX)
  289. coalesce->tx_coalesce_usecs_irq =
  290. channel->irq_moderation;
  291. else
  292. coalesce->tx_coalesce_usecs_irq = 0;
  293. }
  294. }
  295. /* Find lowest IRQ moderation across all used RX queues */
  296. coalesce->rx_coalesce_usecs_irq = ~((u32) 0);
  297. efx_for_each_rx_queue(rx_queue, efx) {
  298. channel = rx_queue->channel;
  299. if (channel->irq_moderation < coalesce->rx_coalesce_usecs_irq)
  300. coalesce->rx_coalesce_usecs_irq =
  301. channel->irq_moderation;
  302. }
  303. return 0;
  304. }
  305. /* Set coalescing parameters
  306. * The difficulties occur for shared channels
  307. */
  308. static int efx_ethtool_set_coalesce(struct net_device *net_dev,
  309. struct ethtool_coalesce *coalesce)
  310. {
  311. struct efx_nic *efx = net_dev->priv;
  312. struct efx_channel *channel;
  313. struct efx_tx_queue *tx_queue;
  314. unsigned tx_usecs, rx_usecs;
  315. if (coalesce->use_adaptive_rx_coalesce ||
  316. coalesce->use_adaptive_tx_coalesce)
  317. return -EOPNOTSUPP;
  318. if (coalesce->rx_coalesce_usecs || coalesce->tx_coalesce_usecs) {
  319. EFX_ERR(efx, "invalid coalescing setting. "
  320. "Only rx/tx_coalesce_usecs_irq are supported\n");
  321. return -EOPNOTSUPP;
  322. }
  323. rx_usecs = coalesce->rx_coalesce_usecs_irq;
  324. tx_usecs = coalesce->tx_coalesce_usecs_irq;
  325. /* If the channel is shared only allow RX parameters to be set */
  326. efx_for_each_tx_queue(tx_queue, efx) {
  327. if ((tx_queue->channel->used_flags == EFX_USED_BY_RX_TX) &&
  328. tx_usecs) {
  329. EFX_ERR(efx, "Channel is shared. "
  330. "Only RX coalescing may be set\n");
  331. return -EOPNOTSUPP;
  332. }
  333. }
  334. efx_init_irq_moderation(efx, tx_usecs, rx_usecs);
  335. /* Reset channel to pick up new moderation value. Note that
  336. * this may change the value of the irq_moderation field
  337. * (e.g. to allow for hardware timer granularity).
  338. */
  339. efx_for_each_channel(channel, efx)
  340. falcon_set_int_moderation(channel);
  341. return 0;
  342. }
  343. static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
  344. struct ethtool_pauseparam *pause)
  345. {
  346. struct efx_nic *efx = net_dev->priv;
  347. enum efx_fc_type flow_control = efx->flow_control;
  348. int rc;
  349. flow_control &= ~(EFX_FC_RX | EFX_FC_TX | EFX_FC_AUTO);
  350. flow_control |= pause->rx_pause ? EFX_FC_RX : 0;
  351. flow_control |= pause->tx_pause ? EFX_FC_TX : 0;
  352. flow_control |= pause->autoneg ? EFX_FC_AUTO : 0;
  353. /* Try to push the pause parameters */
  354. mutex_lock(&efx->mac_lock);
  355. rc = falcon_xmac_set_pause(efx, flow_control);
  356. mutex_unlock(&efx->mac_lock);
  357. if (!rc)
  358. efx_reconfigure_port(efx);
  359. return rc;
  360. }
  361. static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
  362. struct ethtool_pauseparam *pause)
  363. {
  364. struct efx_nic *efx = net_dev->priv;
  365. pause->rx_pause = (efx->flow_control & EFX_FC_RX) ? 1 : 0;
  366. pause->tx_pause = (efx->flow_control & EFX_FC_TX) ? 1 : 0;
  367. pause->autoneg = (efx->flow_control & EFX_FC_AUTO) ? 1 : 0;
  368. }
  369. struct ethtool_ops efx_ethtool_ops = {
  370. .get_settings = efx_ethtool_get_settings,
  371. .set_settings = efx_ethtool_set_settings,
  372. .get_drvinfo = efx_ethtool_get_drvinfo,
  373. .nway_reset = efx_ethtool_nway_reset,
  374. .get_link = efx_ethtool_get_link,
  375. .get_coalesce = efx_ethtool_get_coalesce,
  376. .set_coalesce = efx_ethtool_set_coalesce,
  377. .get_pauseparam = efx_ethtool_get_pauseparam,
  378. .set_pauseparam = efx_ethtool_set_pauseparam,
  379. .get_rx_csum = efx_ethtool_get_rx_csum,
  380. .set_rx_csum = efx_ethtool_set_rx_csum,
  381. .get_tx_csum = ethtool_op_get_tx_csum,
  382. .set_tx_csum = efx_ethtool_set_tx_csum,
  383. .get_sg = ethtool_op_get_sg,
  384. .set_sg = ethtool_op_set_sg,
  385. .get_flags = ethtool_op_get_flags,
  386. .set_flags = ethtool_op_set_flags,
  387. .get_strings = efx_ethtool_get_strings,
  388. .phys_id = efx_ethtool_phys_id,
  389. .get_stats_count = efx_ethtool_get_stats_count,
  390. .get_ethtool_stats = efx_ethtool_get_stats,
  391. };