en_ethtool.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. switch (mdev->dev->rev_id) {
  44. case 0xa0:
  45. if (dev->dev_id >= MLX4_EN_CX3_LOW_ID && dev->dev_id <= MLX4_EN_CX3_HIGH_ID)
  46. sprintf(drvinfo->driver, DRV_NAME " (%s_CX-3)", mdev->dev->board_id);
  47. else
  48. sprintf(drvinfo->driver, DRV_NAME " (%s_CX)", mdev->dev->board_id);
  49. break;
  50. case 0xb0:
  51. sprintf(drvinfo->driver, DRV_NAME " (%s_CX-2)", mdev->dev->board_id);
  52. break;
  53. default:
  54. sprintf(drvinfo->driver, DRV_NAME " (%s)", mdev->dev->board_id);
  55. break;
  56. }
  57. strncpy(drvinfo->version, DRV_VERSION " (" DRV_RELDATE ")", 32);
  58. sprintf(drvinfo->fw_version, "%d.%d.%d",
  59. (u16) (mdev->dev->caps.fw_ver >> 32),
  60. (u16) ((mdev->dev->caps.fw_ver >> 16) & 0xffff),
  61. (u16) (mdev->dev->caps.fw_ver & 0xffff));
  62. strncpy(drvinfo->bus_info, pci_name(mdev->dev->pdev), 32);
  63. drvinfo->n_stats = 0;
  64. drvinfo->regdump_len = 0;
  65. drvinfo->eedump_len = 0;
  66. }
  67. static u32 mlx4_en_get_tso(struct net_device *dev)
  68. {
  69. return (dev->features & NETIF_F_TSO) != 0;
  70. }
  71. static int mlx4_en_set_tso(struct net_device *dev, u32 data)
  72. {
  73. struct mlx4_en_priv *priv = netdev_priv(dev);
  74. if (data) {
  75. if (!priv->mdev->LSO_support)
  76. return -EPERM;
  77. dev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
  78. } else
  79. dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  80. return 0;
  81. }
  82. static u32 mlx4_en_get_rx_csum(struct net_device *dev)
  83. {
  84. struct mlx4_en_priv *priv = netdev_priv(dev);
  85. return priv->rx_csum;
  86. }
  87. static int mlx4_en_set_rx_csum(struct net_device *dev, u32 data)
  88. {
  89. struct mlx4_en_priv *priv = netdev_priv(dev);
  90. priv->rx_csum = (data != 0);
  91. return 0;
  92. }
  93. static const char main_strings[][ETH_GSTRING_LEN] = {
  94. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  95. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  96. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  97. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  98. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  99. "tx_heartbeat_errors", "tx_window_errors",
  100. /* port statistics */
  101. "tso_packets",
  102. "queue_stopped", "wake_queue", "tx_timeout", "rx_alloc_failed",
  103. "rx_csum_good", "rx_csum_none", "tx_chksum_offload",
  104. /* packet statistics */
  105. "broadcast", "rx_prio_0", "rx_prio_1", "rx_prio_2", "rx_prio_3",
  106. "rx_prio_4", "rx_prio_5", "rx_prio_6", "rx_prio_7", "tx_prio_0",
  107. "tx_prio_1", "tx_prio_2", "tx_prio_3", "tx_prio_4", "tx_prio_5",
  108. "tx_prio_6", "tx_prio_7",
  109. };
  110. #define NUM_MAIN_STATS 21
  111. #define NUM_ALL_STATS (NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PKT_STATS + NUM_PERF_STATS)
  112. static const char mlx4_en_test_names[][ETH_GSTRING_LEN]= {
  113. "Interupt Test",
  114. "Link Test",
  115. "Speed Test",
  116. "Register Test",
  117. "Loopback Test",
  118. };
  119. static u32 mlx4_en_get_msglevel(struct net_device *dev)
  120. {
  121. return ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable;
  122. }
  123. static void mlx4_en_set_msglevel(struct net_device *dev, u32 val)
  124. {
  125. ((struct mlx4_en_priv *) netdev_priv(dev))->msg_enable = val;
  126. }
  127. static void mlx4_en_get_wol(struct net_device *netdev,
  128. struct ethtool_wolinfo *wol)
  129. {
  130. struct mlx4_en_priv *priv = netdev_priv(netdev);
  131. int err = 0;
  132. u64 config = 0;
  133. if (!priv->mdev->dev->caps.wol) {
  134. wol->supported = 0;
  135. wol->wolopts = 0;
  136. return;
  137. }
  138. err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
  139. if (err) {
  140. en_err(priv, "Failed to get WoL information\n");
  141. return;
  142. }
  143. if (config & MLX4_EN_WOL_MAGIC)
  144. wol->supported = WAKE_MAGIC;
  145. else
  146. wol->supported = 0;
  147. if (config & MLX4_EN_WOL_ENABLED)
  148. wol->wolopts = WAKE_MAGIC;
  149. else
  150. wol->wolopts = 0;
  151. }
  152. static int mlx4_en_set_wol(struct net_device *netdev,
  153. struct ethtool_wolinfo *wol)
  154. {
  155. struct mlx4_en_priv *priv = netdev_priv(netdev);
  156. u64 config = 0;
  157. int err = 0;
  158. if (!priv->mdev->dev->caps.wol)
  159. return -EOPNOTSUPP;
  160. if (wol->supported & ~WAKE_MAGIC)
  161. return -EINVAL;
  162. err = mlx4_wol_read(priv->mdev->dev, &config, priv->port);
  163. if (err) {
  164. en_err(priv, "Failed to get WoL info, unable to modify\n");
  165. return err;
  166. }
  167. if (wol->wolopts & WAKE_MAGIC) {
  168. config |= MLX4_EN_WOL_DO_MODIFY | MLX4_EN_WOL_ENABLED |
  169. MLX4_EN_WOL_MAGIC;
  170. } else {
  171. config &= ~(MLX4_EN_WOL_ENABLED | MLX4_EN_WOL_MAGIC);
  172. config |= MLX4_EN_WOL_DO_MODIFY;
  173. }
  174. err = mlx4_wol_write(priv->mdev->dev, config, priv->port);
  175. if (err)
  176. en_err(priv, "Failed to set WoL information\n");
  177. return err;
  178. }
  179. static int mlx4_en_get_sset_count(struct net_device *dev, int sset)
  180. {
  181. struct mlx4_en_priv *priv = netdev_priv(dev);
  182. switch (sset) {
  183. case ETH_SS_STATS:
  184. return NUM_ALL_STATS +
  185. (priv->tx_ring_num + priv->rx_ring_num) * 2;
  186. case ETH_SS_TEST:
  187. return MLX4_EN_NUM_SELF_TEST - !(priv->mdev->dev->caps.loopback_support) * 2;
  188. default:
  189. return -EOPNOTSUPP;
  190. }
  191. }
  192. static void mlx4_en_get_ethtool_stats(struct net_device *dev,
  193. struct ethtool_stats *stats, uint64_t *data)
  194. {
  195. struct mlx4_en_priv *priv = netdev_priv(dev);
  196. int index = 0;
  197. int i;
  198. spin_lock_bh(&priv->stats_lock);
  199. for (i = 0; i < NUM_MAIN_STATS; i++)
  200. data[index++] = ((unsigned long *) &priv->stats)[i];
  201. for (i = 0; i < NUM_PORT_STATS; i++)
  202. data[index++] = ((unsigned long *) &priv->port_stats)[i];
  203. for (i = 0; i < priv->tx_ring_num; i++) {
  204. data[index++] = priv->tx_ring[i].packets;
  205. data[index++] = priv->tx_ring[i].bytes;
  206. }
  207. for (i = 0; i < priv->rx_ring_num; i++) {
  208. data[index++] = priv->rx_ring[i].packets;
  209. data[index++] = priv->rx_ring[i].bytes;
  210. }
  211. for (i = 0; i < NUM_PKT_STATS; i++)
  212. data[index++] = ((unsigned long *) &priv->pkstats)[i];
  213. spin_unlock_bh(&priv->stats_lock);
  214. }
  215. static void mlx4_en_self_test(struct net_device *dev,
  216. struct ethtool_test *etest, u64 *buf)
  217. {
  218. mlx4_en_ex_selftest(dev, &etest->flags, buf);
  219. }
  220. static void mlx4_en_get_strings(struct net_device *dev,
  221. uint32_t stringset, uint8_t *data)
  222. {
  223. struct mlx4_en_priv *priv = netdev_priv(dev);
  224. int index = 0;
  225. int i;
  226. switch (stringset) {
  227. case ETH_SS_TEST:
  228. for (i = 0; i < MLX4_EN_NUM_SELF_TEST - 2; i++)
  229. strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
  230. if (priv->mdev->dev->caps.loopback_support)
  231. for (; i < MLX4_EN_NUM_SELF_TEST; i++)
  232. strcpy(data + i * ETH_GSTRING_LEN, mlx4_en_test_names[i]);
  233. break;
  234. case ETH_SS_STATS:
  235. /* Add main counters */
  236. for (i = 0; i < NUM_MAIN_STATS; i++)
  237. strcpy(data + (index++) * ETH_GSTRING_LEN, main_strings[i]);
  238. for (i = 0; i< NUM_PORT_STATS; i++)
  239. strcpy(data + (index++) * ETH_GSTRING_LEN,
  240. main_strings[i + NUM_MAIN_STATS]);
  241. for (i = 0; i < priv->tx_ring_num; i++) {
  242. sprintf(data + (index++) * ETH_GSTRING_LEN,
  243. "tx%d_packets", i);
  244. sprintf(data + (index++) * ETH_GSTRING_LEN,
  245. "tx%d_bytes", i);
  246. }
  247. for (i = 0; i < priv->rx_ring_num; i++) {
  248. sprintf(data + (index++) * ETH_GSTRING_LEN,
  249. "rx%d_packets", i);
  250. sprintf(data + (index++) * ETH_GSTRING_LEN,
  251. "rx%d_bytes", i);
  252. }
  253. for (i = 0; i< NUM_PKT_STATS; i++)
  254. strcpy(data + (index++) * ETH_GSTRING_LEN,
  255. main_strings[i + NUM_MAIN_STATS + NUM_PORT_STATS]);
  256. break;
  257. }
  258. }
  259. static int mlx4_en_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  260. {
  261. struct mlx4_en_priv *priv = netdev_priv(dev);
  262. int trans_type;
  263. cmd->autoneg = AUTONEG_DISABLE;
  264. cmd->supported = SUPPORTED_10000baseT_Full;
  265. cmd->advertising = ADVERTISED_10000baseT_Full;
  266. if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
  267. return -ENOMEM;
  268. trans_type = priv->port_state.transciver;
  269. if (netif_carrier_ok(dev)) {
  270. cmd->speed = priv->port_state.link_speed;
  271. cmd->duplex = DUPLEX_FULL;
  272. } else {
  273. cmd->speed = -1;
  274. cmd->duplex = -1;
  275. }
  276. if (trans_type > 0 && trans_type <= 0xC) {
  277. cmd->port = PORT_FIBRE;
  278. cmd->transceiver = XCVR_EXTERNAL;
  279. cmd->supported |= SUPPORTED_FIBRE;
  280. cmd->advertising |= ADVERTISED_FIBRE;
  281. } else if (trans_type == 0x80 || trans_type == 0) {
  282. cmd->port = PORT_TP;
  283. cmd->transceiver = XCVR_INTERNAL;
  284. cmd->supported |= SUPPORTED_TP;
  285. cmd->advertising |= ADVERTISED_TP;
  286. } else {
  287. cmd->port = -1;
  288. cmd->transceiver = -1;
  289. }
  290. return 0;
  291. }
  292. static int mlx4_en_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  293. {
  294. if ((cmd->autoneg == AUTONEG_ENABLE) ||
  295. (cmd->speed != SPEED_10000) || (cmd->duplex != DUPLEX_FULL))
  296. return -EINVAL;
  297. /* Nothing to change */
  298. return 0;
  299. }
  300. static int mlx4_en_get_coalesce(struct net_device *dev,
  301. struct ethtool_coalesce *coal)
  302. {
  303. struct mlx4_en_priv *priv = netdev_priv(dev);
  304. coal->tx_coalesce_usecs = 0;
  305. coal->tx_max_coalesced_frames = 0;
  306. coal->rx_coalesce_usecs = priv->rx_usecs;
  307. coal->rx_max_coalesced_frames = priv->rx_frames;
  308. coal->pkt_rate_low = priv->pkt_rate_low;
  309. coal->rx_coalesce_usecs_low = priv->rx_usecs_low;
  310. coal->pkt_rate_high = priv->pkt_rate_high;
  311. coal->rx_coalesce_usecs_high = priv->rx_usecs_high;
  312. coal->rate_sample_interval = priv->sample_interval;
  313. coal->use_adaptive_rx_coalesce = priv->adaptive_rx_coal;
  314. return 0;
  315. }
  316. static int mlx4_en_set_coalesce(struct net_device *dev,
  317. struct ethtool_coalesce *coal)
  318. {
  319. struct mlx4_en_priv *priv = netdev_priv(dev);
  320. int err, i;
  321. priv->rx_frames = (coal->rx_max_coalesced_frames ==
  322. MLX4_EN_AUTO_CONF) ?
  323. MLX4_EN_RX_COAL_TARGET :
  324. coal->rx_max_coalesced_frames;
  325. priv->rx_usecs = (coal->rx_coalesce_usecs ==
  326. MLX4_EN_AUTO_CONF) ?
  327. MLX4_EN_RX_COAL_TIME :
  328. coal->rx_coalesce_usecs;
  329. /* Set adaptive coalescing params */
  330. priv->pkt_rate_low = coal->pkt_rate_low;
  331. priv->rx_usecs_low = coal->rx_coalesce_usecs_low;
  332. priv->pkt_rate_high = coal->pkt_rate_high;
  333. priv->rx_usecs_high = coal->rx_coalesce_usecs_high;
  334. priv->sample_interval = coal->rate_sample_interval;
  335. priv->adaptive_rx_coal = coal->use_adaptive_rx_coalesce;
  336. priv->last_moder_time = MLX4_EN_AUTO_CONF;
  337. if (priv->adaptive_rx_coal)
  338. return 0;
  339. for (i = 0; i < priv->rx_ring_num; i++) {
  340. priv->rx_cq[i].moder_cnt = priv->rx_frames;
  341. priv->rx_cq[i].moder_time = priv->rx_usecs;
  342. err = mlx4_en_set_cq_moder(priv, &priv->rx_cq[i]);
  343. if (err)
  344. return err;
  345. }
  346. return 0;
  347. }
  348. static int mlx4_en_set_pauseparam(struct net_device *dev,
  349. struct ethtool_pauseparam *pause)
  350. {
  351. struct mlx4_en_priv *priv = netdev_priv(dev);
  352. struct mlx4_en_dev *mdev = priv->mdev;
  353. int err;
  354. priv->prof->tx_pause = pause->tx_pause != 0;
  355. priv->prof->rx_pause = pause->rx_pause != 0;
  356. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  357. priv->rx_skb_size + ETH_FCS_LEN,
  358. priv->prof->tx_pause,
  359. priv->prof->tx_ppp,
  360. priv->prof->rx_pause,
  361. priv->prof->rx_ppp);
  362. if (err)
  363. en_err(priv, "Failed setting pause params\n");
  364. return err;
  365. }
  366. static void mlx4_en_get_pauseparam(struct net_device *dev,
  367. struct ethtool_pauseparam *pause)
  368. {
  369. struct mlx4_en_priv *priv = netdev_priv(dev);
  370. pause->tx_pause = priv->prof->tx_pause;
  371. pause->rx_pause = priv->prof->rx_pause;
  372. }
  373. static int mlx4_en_set_ringparam(struct net_device *dev,
  374. struct ethtool_ringparam *param)
  375. {
  376. struct mlx4_en_priv *priv = netdev_priv(dev);
  377. struct mlx4_en_dev *mdev = priv->mdev;
  378. u32 rx_size, tx_size;
  379. int port_up = 0;
  380. int err = 0;
  381. if (param->rx_jumbo_pending || param->rx_mini_pending)
  382. return -EINVAL;
  383. rx_size = roundup_pow_of_two(param->rx_pending);
  384. rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
  385. rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
  386. tx_size = roundup_pow_of_two(param->tx_pending);
  387. tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
  388. tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
  389. if (rx_size == (priv->port_up ? priv->rx_ring[0].actual_size :
  390. priv->rx_ring[0].size) &&
  391. tx_size == priv->tx_ring[0].size)
  392. return 0;
  393. mutex_lock(&mdev->state_lock);
  394. if (priv->port_up) {
  395. port_up = 1;
  396. mlx4_en_stop_port(dev);
  397. }
  398. mlx4_en_free_resources(priv, true);
  399. priv->prof->tx_ring_size = tx_size;
  400. priv->prof->rx_ring_size = rx_size;
  401. err = mlx4_en_alloc_resources(priv);
  402. if (err) {
  403. en_err(priv, "Failed reallocating port resources\n");
  404. goto out;
  405. }
  406. if (port_up) {
  407. err = mlx4_en_start_port(dev);
  408. if (err)
  409. en_err(priv, "Failed starting port\n");
  410. }
  411. out:
  412. mutex_unlock(&mdev->state_lock);
  413. return err;
  414. }
  415. static void mlx4_en_get_ringparam(struct net_device *dev,
  416. struct ethtool_ringparam *param)
  417. {
  418. struct mlx4_en_priv *priv = netdev_priv(dev);
  419. memset(param, 0, sizeof(*param));
  420. param->rx_max_pending = MLX4_EN_MAX_RX_SIZE;
  421. param->tx_max_pending = MLX4_EN_MAX_TX_SIZE;
  422. param->rx_pending = priv->port_up ?
  423. priv->rx_ring[0].actual_size : priv->rx_ring[0].size;
  424. param->tx_pending = priv->tx_ring[0].size;
  425. }
  426. const struct ethtool_ops mlx4_en_ethtool_ops = {
  427. .get_drvinfo = mlx4_en_get_drvinfo,
  428. .get_settings = mlx4_en_get_settings,
  429. .set_settings = mlx4_en_set_settings,
  430. #ifdef NETIF_F_TSO
  431. .get_tso = mlx4_en_get_tso,
  432. .set_tso = mlx4_en_set_tso,
  433. #endif
  434. .get_sg = ethtool_op_get_sg,
  435. .set_sg = ethtool_op_set_sg,
  436. .get_link = ethtool_op_get_link,
  437. .get_rx_csum = mlx4_en_get_rx_csum,
  438. .set_rx_csum = mlx4_en_set_rx_csum,
  439. .get_tx_csum = ethtool_op_get_tx_csum,
  440. .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
  441. .get_strings = mlx4_en_get_strings,
  442. .get_sset_count = mlx4_en_get_sset_count,
  443. .get_ethtool_stats = mlx4_en_get_ethtool_stats,
  444. .self_test = mlx4_en_self_test,
  445. .get_wol = mlx4_en_get_wol,
  446. .set_wol = mlx4_en_set_wol,
  447. .get_msglevel = mlx4_en_get_msglevel,
  448. .set_msglevel = mlx4_en_set_msglevel,
  449. .get_coalesce = mlx4_en_get_coalesce,
  450. .set_coalesce = mlx4_en_set_coalesce,
  451. .get_pauseparam = mlx4_en_get_pauseparam,
  452. .set_pauseparam = mlx4_en_set_pauseparam,
  453. .get_ringparam = mlx4_en_get_ringparam,
  454. .set_ringparam = mlx4_en_set_ringparam,
  455. .get_flags = ethtool_op_get_flags,
  456. };