en_ethtool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/netdevice.h>
  36. #include "mlx4_en.h"
  37. #include "en_port.h"
  38. static void
  39. mlx4_en_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
  40. {
  41. struct mlx4_en_priv *priv = netdev_priv(dev);
  42. struct mlx4_en_dev *mdev = priv->mdev;
  43. strncpy(drvinfo->driver, DRV_NAME, 32);
  44. strncpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")", 32);
  45. sprintf(drvinfo->fw_version, "%d.%d.%d",
  46. (u16) (mdev->dev->caps.fw_ver >> 32),
  47. (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
  48. (u16) (mdev->dev->caps.fw_ver & 0xffff));
  49. strncpy(drvinfo->bus_info, pci_name(mdev->dev->pdev), 32);
  50. drvinfo->n_stats = 0;
  51. drvinfo->regdump_len = 0;
  52. drvinfo->eedump_len = 0;
  53. }
  54. static const char main_strings[][ETH_GSTRING_LEN] = {
  55. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  56. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  57. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  58. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  59. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  60. "tx_heartbeat_errors", "tx_window_errors",
  61. /* port statistics */
  62. "tso_packets",
  63. "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
  64. "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
  65. /* packet statistics */
  66. "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
  67. "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
  68. "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
  69. "tx_prio_6", "tx_prio_7",
  70. };
  71. #define NUM_MAIN_STATS 21
  72. #define NUM_ALL_STATS (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
  73. static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
  74. "Interupt Test",
  75. "Link Test",
  76. "Speed Test",
  77. "Register Test",
  78. "Loopback Test",
  79. };
  80. static u32 mlx4_en_get_msglevel(struct net_device *dev)
  81. {
  82. return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
  83. }
  84. static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
  85. {
  86. ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
  87. }
  88. static void mlx4_en_get_wol(struct net_device *netdev,
  89. struct ethtool_wolinfo *wol)
  90. {
  91. struct mlx4_en_priv *priv = netdev_priv(netdev);
  92. int err = 0;
  93. u64 config = 0;
  94. if (!priv->mdev->dev->caps.wol) {
  95. wol->supported = 0;
  96. wol->wolopts = 0;
  97. return;
  98. }
  99. err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
  100. if (err) {
  101. en_err(priv, "Failed to get WoL information\n");
  102. return;
  103. }
  104. if (config & MLX4_EN_WOL_MAGIC)
  105. wol->supported = WAKE_MAGIC;
  106. else
  107. wol->supported = 0;
  108. if (config & MLX4_EN_WOL_ENABLED)
  109. wol->wolopts = WAKE_MAGIC;
  110. else
  111. wol->wolopts = 0;
  112. }
  113. static int mlx4_en_set_wol(struct net_device *netdev,
  114. struct ethtool_wolinfo *wol)
  115. {
  116. struct mlx4_en_priv *priv = netdev_priv(netdev);
  117. u64 config = 0;
  118. int err = 0;
  119. if (!priv->mdev->dev->caps.wol)
  120. return -EOPNOTSUPP;
  121. if (wol->supported & ~WAKE_MAGIC)
  122. return -EINVAL;
  123. err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
  124. if (err) {
  125. en_err(priv, "Failed to get WoL info, unable to modify\n");
  126. return err;
  127. }
  128. if (wol->wolopts & WAKE_MAGIC) {
  129. config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
  130. MLX4_EN_WOL_MAGIC;
  131. } else {
  132. config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
  133. config |= MLX4_EN_WOL_DO_MODIFY;
  134. }
  135. err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
  136. if (err)
  137. en_err(priv, "Failed to set WoL information\n");
  138. return err;
  139. }
  140. static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
  141. {
  142. struct mlx4_en_priv *priv = netdev_priv(dev);
  143. switch (sset) {
  144. case ETH_SS_STATS:
  145. return NUM_ALL_STATS +
  146. (priv->tx_ring_num + priv->rx_ring_num) * 2;
  147. case ETH_SS_TEST:
  148. return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.loopback_support) * 2;
  149. default:
  150. return -EOPNOTSUPP;
  151. }
  152. }
  153. static void mlx4_en_get_ethtool_stats(struct net_device *dev,
  154. struct ethtool_stats *stats, uint64_t *data)
  155. {
  156. struct mlx4_en_priv *priv = netdev_priv(dev);
  157. int index = 0;
  158. int i;
  159. spin_lock_bh(&priv->stats_lock);
  160. for (i = 0; i < NUM_MAIN_STATS; i++)
  161. data[index++] = ((unsigned long *) &priv->stats)[i];
  162. for (i = 0; i < NUM_PORT_STATS; i++)
  163. data[index++] = ((unsigned long *) &priv->port_stats)[i];
  164. for (i = 0; i < priv->tx_ring_num; i++) {
  165. data[index++] = priv->tx_ring[i].packets;
  166. data[index++] = priv->tx_ring[i].bytes;
  167. }
  168. for (i = 0; i < priv->rx_ring_num; i++) {
  169. data[index++] = priv->rx_ring[i].packets;
  170. data[index++] = priv->rx_ring[i].bytes;
  171. }
  172. for (i = 0; i < NUM_PKT_STATS; i++)
  173. data[index++] = ((unsigned long *) &priv->pkstats)[i];
  174. spin_unlock_bh(&priv->stats_lock);
  175. }
  176. static void mlx4_en_self_test(struct net_device *dev,
  177. struct ethtool_test *etest, u64 *buf)
  178. {
  179. mlx4_en_ex_selftest(dev, &etest->flags, buf);
  180. }
  181. static void mlx4_en_get_strings(struct net_device *dev,
  182. uint32_t stringset, uint8_t *data)
  183. {
  184. struct mlx4_en_priv *priv = netdev_priv(dev);
  185. int index = 0;
  186. int i;
  187. switch (stringset) {
  188. case ETH_SS_TEST:
  189. for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
  190. strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
  191. if (priv->mdev->dev->caps.loopback_support)
  192. for (; i < MLX4_EN_NUM_SELF_TEST; i++)
  193. strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
  194. break;
  195. case ETH_SS_STATS:
  196. /* Add main counters */
  197. for (i = 0; i < NUM_MAIN_STATS; i++)
  198. strcpy(data + (index++) * ETH_GSTRING_LEN, main_strings[i]);
  199. for (i = 0; i< NUM_PORT_STATS; i++)
  200. strcpy(data + (index++) * ETH_GSTRING_LEN,
  201. main_strings[i + NUM_MAIN_STATS]);
  202. for (i = 0; i < priv->tx_ring_num; i++) {
  203. sprintf(data + (index++) * ETH_GSTRING_LEN,
  204. "tx%d_packets", i);
  205. sprintf(data + (index++) * ETH_GSTRING_LEN,
  206. "tx%d_bytes", i);
  207. }
  208. for (i = 0; i < priv->rx_ring_num; i++) {
  209. sprintf(data + (index++) * ETH_GSTRING_LEN,
  210. "rx%d_packets", i);
  211. sprintf(data + (index++) * ETH_GSTRING_LEN,
  212. "rx%d_bytes", i);
  213. }
  214. for (i = 0; i< NUM_PKT_STATS; i++)
  215. strcpy(data + (index++) * ETH_GSTRING_LEN,
  216. main_strings[i + NUM_MAIN_STATS + NUM_PORT_STATS]);
  217. break;
  218. }
  219. }
  220. static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  221. {
  222. struct mlx4_en_priv *priv = netdev_priv(dev);
  223. int trans_type;
  224. cmd->autoneg = AUTONEG_DISABLE;
  225. cmd->supported = SUPPORTED_10000baseT_Full;
  226. cmd->advertising = ADVERTISED_10000baseT_Full;
  227. if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
  228. return -ENOMEM;
  229. trans_type = priv->port_state.transciver;
  230. if (netif_carrier_ok(dev)) {
  231. ethtool_cmd_speed_set(cmd, priv->port_state.link_speed);
  232. cmd->duplex = DUPLEX_FULL;
  233. } else {
  234. ethtool_cmd_speed_set(cmd, -1);
  235. cmd->duplex = -1;
  236. }
  237. if (trans_type > 0 && trans_type <= 0xC) {
  238. cmd->port = PORT_FIBRE;
  239. cmd->transceiver = XCVR_EXTERNAL;
  240. cmd->supported |= SUPPORTED_FIBRE;
  241. cmd->advertising |= ADVERTISED_FIBRE;
  242. } else if (trans_type == 0x80 || trans_type == 0) {
  243. cmd->port = PORT_TP;
  244. cmd->transceiver = XCVR_INTERNAL;
  245. cmd->supported |= SUPPORTED_TP;
  246. cmd->advertising |= ADVERTISED_TP;
  247. } else {
  248. cmd->port = -1;
  249. cmd->transceiver = -1;
  250. }
  251. return 0;
  252. }
  253. static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  254. {
  255. if ((cmd->autoneg == AUTONEG_ENABLE) ||
  256. (ethtool_cmd_speed(cmd) != SPEED_10000) ||
  257. (cmd->duplex != DUPLEX_FULL))
  258. return -EINVAL;
  259. /* Nothing to change */
  260. return 0;
  261. }
  262. static int mlx4_en_get_coalesce(struct net_device *dev,
  263. struct ethtool_coalesce *coal)
  264. {
  265. struct mlx4_en_priv *priv = netdev_priv(dev);
  266. coal->tx_coalesce_usecs = 0;
  267. coal->tx_max_coalesced_frames = 0;
  268. coal->rx_coalesce_usecs = priv->rx_usecs;
  269. coal->rx_max_coalesced_frames = priv->rx_frames;
  270. coal->pkt_rate_low = priv->pkt_rate_low;
  271. coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
  272. coal->pkt_rate_high = priv->pkt_rate_high;
  273. coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
  274. coal->rate_sample_interval = priv->sample_interval;
  275. coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
  276. return 0;
  277. }
  278. static int mlx4_en_set_coalesce(struct net_device *dev,
  279. struct ethtool_coalesce *coal)
  280. {
  281. struct mlx4_en_priv *priv = netdev_priv(dev);
  282. int err, i;
  283. priv->rx_frames = (coal->rx_max_coalesced_frames ==
  284. MLX4_EN_AUTO_CONF) ?
  285. MLX4_EN_RX_COAL_TARGET :
  286. coal->rx_max_coalesced_frames;
  287. priv->rx_usecs = (coal->rx_coalesce_usecs ==
  288. MLX4_EN_AUTO_CONF) ?
  289. MLX4_EN_RX_COAL_TIME :
  290. coal->rx_coalesce_usecs;
  291. /* Set adaptive coalescing params */
  292. priv->pkt_rate_low = coal->pkt_rate_low;
  293. priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
  294. priv->pkt_rate_high = coal->pkt_rate_high;
  295. priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
  296. priv->sample_interval = coal->rate_sample_interval;
  297. priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
  298. priv->last_moder_time = MLX4_EN_AUTO_CONF;
  299. if (priv->adaptive_rx_coal)
  300. return 0;
  301. for (i = 0; i < priv->rx_ring_num; i++) {
  302. priv->rx_cq[i].moder_cnt = priv->rx_frames;
  303. priv->rx_cq[i].moder_time = priv->rx_usecs;
  304. err = mlx4_en_set_cq_moder(priv, &priv->rx_cq[i]);
  305. if (err)
  306. return err;
  307. }
  308. return 0;
  309. }
  310. static int mlx4_en_set_pauseparam(struct net_device *dev,
  311. struct ethtool_pauseparam *pause)
  312. {
  313. struct mlx4_en_priv *priv = netdev_priv(dev);
  314. struct mlx4_en_dev *mdev = priv->mdev;
  315. int err;
  316. priv->prof->tx_pause = pause->tx_pause != 0;
  317. priv->prof->rx_pause = pause->rx_pause != 0;
  318. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  319. priv->rx_skb_size + ETH_FCS_LEN,
  320. priv->prof->tx_pause,
  321. priv->prof->tx_ppp,
  322. priv->prof->rx_pause,
  323. priv->prof->rx_ppp);
  324. if (err)
  325. en_err(priv, "Failed setting pause params\n");
  326. return err;
  327. }
  328. static void mlx4_en_get_pauseparam(struct net_device *dev,
  329. struct ethtool_pauseparam *pause)
  330. {
  331. struct mlx4_en_priv *priv = netdev_priv(dev);
  332. pause->tx_pause = priv->prof->tx_pause;
  333. pause->rx_pause = priv->prof->rx_pause;
  334. }
  335. static int mlx4_en_set_ringparam(struct net_device *dev,
  336. struct ethtool_ringparam *param)
  337. {
  338. struct mlx4_en_priv *priv = netdev_priv(dev);
  339. struct mlx4_en_dev *mdev = priv->mdev;
  340. u32 rx_size, tx_size;
  341. int port_up = 0;
  342. int err = 0;
  343. if (param->rx_jumbo_pending || param->rx_mini_pending)
  344. return -EINVAL;
  345. rx_size = roundup_pow_of_two(param->rx_pending);
  346. rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
  347. rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
  348. tx_size = roundup_pow_of_two(param->tx_pending);
  349. tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
  350. tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
  351. if (rx_size == (priv->port_up ? priv->rx_ring[0].actual_size :
  352. priv->rx_ring[0].size) &&
  353. tx_size == priv->tx_ring[0].size)
  354. return 0;
  355. mutex_lock(&mdev->state_lock);
  356. if (priv->port_up) {
  357. port_up = 1;
  358. mlx4_en_stop_port(dev);
  359. }
  360. mlx4_en_free_resources(priv, true);
  361. priv->prof->tx_ring_size = tx_size;
  362. priv->prof->rx_ring_size = rx_size;
  363. err = mlx4_en_alloc_resources(priv);
  364. if (err) {
  365. en_err(priv, "Failed reallocating port resources\n");
  366. goto out;
  367. }
  368. if (port_up) {
  369. err = mlx4_en_start_port(dev);
  370. if (err)
  371. en_err(priv, "Failed starting port\n");
  372. }
  373. out:
  374. mutex_unlock(&mdev->state_lock);
  375. return err;
  376. }
  377. static void mlx4_en_get_ringparam(struct net_device *dev,
  378. struct ethtool_ringparam *param)
  379. {
  380. struct mlx4_en_priv *priv = netdev_priv(dev);
  381. memset(param, 0, sizeof(*param));
  382. param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
  383. param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
  384. param->rx_pending = priv->port_up ?
  385. priv->rx_ring[0].actual_size : priv->rx_ring[0].size;
  386. param->tx_pending = priv->tx_ring[0].size;
  387. }
  388. const struct ethtool_ops mlx4_en_ethtool_ops = {
  389. .get_drvinfo = mlx4_en_get_drvinfo,
  390. .get_settings = mlx4_en_get_settings,
  391. .set_settings = mlx4_en_set_settings,
  392. .get_link = ethtool_op_get_link,
  393. .get_strings = mlx4_en_get_strings,
  394. .get_sset_count = mlx4_en_get_sset_count,
  395. .get_ethtool_stats = mlx4_en_get_ethtool_stats,
  396. .self_test = mlx4_en_self_test,
  397. .get_wol = mlx4_en_get_wol,
  398. .set_wol = mlx4_en_set_wol,
  399. .get_msglevel = mlx4_en_get_msglevel,
  400. .set_msglevel = mlx4_en_set_msglevel,
  401. .get_coalesce = mlx4_en_get_coalesce,
  402. .set_coalesce = mlx4_en_set_coalesce,
  403. .get_pauseparam = mlx4_en_get_pauseparam,
  404. .set_pauseparam = mlx4_en_set_pauseparam,
  405. .get_ringparam = mlx4_en_get_ringparam,
  406. .set_ringparam = mlx4_en_set_ringparam,
  407. };