en_port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/if_vlan.h>
  34. #include <linux/mlx4/device.h>
  35. #include <linux/mlx4/cmd.h>
  36. #include "en_port.h"
  37. #include "mlx4_en.h"
  38. int mlx4_SET_VLAN_FLTR(struct mlx4_dev *dev, struct mlx4_en_priv *priv)
  39. {
  40. struct mlx4_cmd_mailbox *mailbox;
  41. struct mlx4_set_vlan_fltr_mbox *filter;
  42. int i;
  43. int j;
  44. int index = 0;
  45. u32 entry;
  46. int err = 0;
  47. mailbox = mlx4_alloc_cmd_mailbox(dev);
  48. if (IS_ERR(mailbox))
  49. return PTR_ERR(mailbox);
  50. filter = mailbox->buf;
  51. for (i = VLAN_FLTR_SIZE - 1; i >= 0; i--) {
  52. entry = 0;
  53. for (j = 0; j < 32; j++)
  54. if (test_bit(index++, priv->active_vlans))
  55. entry |= 1 << j;
  56. filter->entry[i] = cpu_to_be32(entry);
  57. }
  58. err = mlx4_cmd(dev, mailbox->dma, priv->port, 0, MLX4_CMD_SET_VLAN_FLTR,
  59. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  60. mlx4_free_cmd_mailbox(dev, mailbox);
  61. return err;
  62. }
  63. int mlx4_en_QUERY_PORT(struct mlx4_en_dev *mdev, u8 port)
  64. {
  65. struct mlx4_en_query_port_context *qport_context;
  66. struct mlx4_en_priv *priv = netdev_priv(mdev->pndev[port]);
  67. struct mlx4_en_port_state *state = &priv->port_state;
  68. struct mlx4_cmd_mailbox *mailbox;
  69. int err;
  70. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  71. if (IS_ERR(mailbox))
  72. return PTR_ERR(mailbox);
  73. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  74. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  75. MLX4_CMD_WRAPPED);
  76. if (err)
  77. goto out;
  78. qport_context = mailbox->buf;
  79. /* This command is always accessed from Ethtool context
  80. * already synchronized, no need in locking */
  81. state->link_state = !!(qport_context->link_up & MLX4_EN_LINK_UP_MASK);
  82. switch (qport_context->link_speed & MLX4_EN_SPEED_MASK) {
  83. case MLX4_EN_100M_SPEED:
  84. state->link_speed = SPEED_100;
  85. break;
  86. case MLX4_EN_1G_SPEED:
  87. state->link_speed = SPEED_1000;
  88. break;
  89. case MLX4_EN_10G_SPEED_XAUI:
  90. case MLX4_EN_10G_SPEED_XFI:
  91. state->link_speed = SPEED_10000;
  92. break;
  93. case MLX4_EN_20G_SPEED:
  94. state->link_speed = SPEED_20000;
  95. break;
  96. case MLX4_EN_40G_SPEED:
  97. state->link_speed = SPEED_40000;
  98. break;
  99. case MLX4_EN_56G_SPEED:
  100. state->link_speed = SPEED_56000;
  101. break;
  102. default:
  103. state->link_speed = -1;
  104. break;
  105. }
  106. state->transceiver = qport_context->transceiver;
  107. state->flags = 0; /* Reset and recalculate the port flags */
  108. state->flags |= (qport_context->link_up & MLX4_EN_ANC_MASK) ?
  109. MLX4_EN_PORT_ANC : 0;
  110. state->flags |= (qport_context->autoneg & MLX4_EN_AUTONEG_MASK) ?
  111. MLX4_EN_PORT_ANE : 0;
  112. out:
  113. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  114. return err;
  115. }
  116. /* Each counter set is located in struct mlx4_en_stat_out_mbox
  117. * with a const offset between its prio components.
  118. * This function runs over a counter set and sum all of it's prio components.
  119. */
  120. static unsigned long en_stats_adder(__be64 *start, __be64 *next, int num)
  121. {
  122. __be64 *curr = start;
  123. unsigned long ret = 0;
  124. int i;
  125. int offset = next - start;
  126. for (i = 0; i < num; i++) {
  127. ret += be64_to_cpu(*curr);
  128. curr += offset;
  129. }
  130. return ret;
  131. }
  132. void mlx4_en_fold_software_stats(struct net_device *dev)
  133. {
  134. struct mlx4_en_priv *priv = netdev_priv(dev);
  135. struct mlx4_en_dev *mdev = priv->mdev;
  136. unsigned long packets, bytes;
  137. int i;
  138. if (!priv->port_up || mlx4_is_master(mdev->dev))
  139. return;
  140. packets = 0;
  141. bytes = 0;
  142. for (i = 0; i < priv->rx_ring_num; i++) {
  143. const struct mlx4_en_rx_ring *ring = priv->rx_ring[i];
  144. packets += READ_ONCE(ring->packets);
  145. bytes += READ_ONCE(ring->bytes);
  146. }
  147. dev->stats.rx_packets = packets;
  148. dev->stats.rx_bytes = bytes;
  149. packets = 0;
  150. bytes = 0;
  151. for (i = 0; i < priv->tx_ring_num[TX]; i++) {
  152. const struct mlx4_en_tx_ring *ring = priv->tx_ring[TX][i];
  153. packets += READ_ONCE(ring->packets);
  154. bytes += READ_ONCE(ring->bytes);
  155. }
  156. dev->stats.tx_packets = packets;
  157. dev->stats.tx_bytes = bytes;
  158. }
  159. int mlx4_en_DUMP_ETH_STATS(struct mlx4_en_dev *mdev, u8 port, u8 reset)
  160. {
  161. struct mlx4_counter tmp_counter_stats;
  162. struct mlx4_en_stat_out_mbox *mlx4_en_stats;
  163. struct mlx4_en_stat_out_flow_control_mbox *flowstats;
  164. struct net_device *dev = mdev->pndev[port];
  165. struct mlx4_en_priv *priv = netdev_priv(dev);
  166. struct net_device_stats *stats = &dev->stats;
  167. struct mlx4_cmd_mailbox *mailbox;
  168. u64 in_mod = reset << 8 | port;
  169. int err;
  170. int i, counter_index;
  171. unsigned long sw_tx_dropped = 0;
  172. unsigned long sw_rx_dropped = 0;
  173. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  174. if (IS_ERR(mailbox))
  175. return PTR_ERR(mailbox);
  176. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, in_mod, 0,
  177. MLX4_CMD_DUMP_ETH_STATS, MLX4_CMD_TIME_CLASS_B,
  178. MLX4_CMD_NATIVE);
  179. if (err)
  180. goto out;
  181. mlx4_en_stats = mailbox->buf;
  182. spin_lock_bh(&priv->stats_lock);
  183. mlx4_en_fold_software_stats(dev);
  184. priv->port_stats.rx_chksum_good = 0;
  185. priv->port_stats.rx_chksum_none = 0;
  186. priv->port_stats.rx_chksum_complete = 0;
  187. priv->xdp_stats.rx_xdp_drop = 0;
  188. priv->xdp_stats.rx_xdp_tx = 0;
  189. priv->xdp_stats.rx_xdp_tx_full = 0;
  190. for (i = 0; i < priv->rx_ring_num; i++) {
  191. const struct mlx4_en_rx_ring *ring = priv->rx_ring[i];
  192. sw_rx_dropped += READ_ONCE(ring->dropped);
  193. priv->port_stats.rx_chksum_good += READ_ONCE(ring->csum_ok);
  194. priv->port_stats.rx_chksum_none += READ_ONCE(ring->csum_none);
  195. priv->port_stats.rx_chksum_complete += READ_ONCE(ring->csum_complete);
  196. priv->xdp_stats.rx_xdp_drop += READ_ONCE(ring->xdp_drop);
  197. priv->xdp_stats.rx_xdp_tx += READ_ONCE(ring->xdp_tx);
  198. priv->xdp_stats.rx_xdp_tx_full += READ_ONCE(ring->xdp_tx_full);
  199. }
  200. priv->port_stats.tx_chksum_offload = 0;
  201. priv->port_stats.queue_stopped = 0;
  202. priv->port_stats.wake_queue = 0;
  203. priv->port_stats.tso_packets = 0;
  204. priv->port_stats.xmit_more = 0;
  205. for (i = 0; i < priv->tx_ring_num[TX]; i++) {
  206. const struct mlx4_en_tx_ring *ring = priv->tx_ring[TX][i];
  207. sw_tx_dropped += READ_ONCE(ring->tx_dropped);
  208. priv->port_stats.tx_chksum_offload += READ_ONCE(ring->tx_csum);
  209. priv->port_stats.queue_stopped += READ_ONCE(ring->queue_stopped);
  210. priv->port_stats.wake_queue += READ_ONCE(ring->wake_queue);
  211. priv->port_stats.tso_packets += READ_ONCE(ring->tso_packets);
  212. priv->port_stats.xmit_more += READ_ONCE(ring->xmit_more);
  213. }
  214. if (mlx4_is_master(mdev->dev)) {
  215. stats->rx_packets = en_stats_adder(&mlx4_en_stats->RTOT_prio_0,
  216. &mlx4_en_stats->RTOT_prio_1,
  217. NUM_PRIORITIES);
  218. stats->tx_packets = en_stats_adder(&mlx4_en_stats->TTOT_prio_0,
  219. &mlx4_en_stats->TTOT_prio_1,
  220. NUM_PRIORITIES);
  221. stats->rx_bytes = en_stats_adder(&mlx4_en_stats->ROCT_prio_0,
  222. &mlx4_en_stats->ROCT_prio_1,
  223. NUM_PRIORITIES);
  224. stats->tx_bytes = en_stats_adder(&mlx4_en_stats->TOCT_prio_0,
  225. &mlx4_en_stats->TOCT_prio_1,
  226. NUM_PRIORITIES);
  227. }
  228. /* net device stats */
  229. stats->rx_errors = be64_to_cpu(mlx4_en_stats->PCS) +
  230. be32_to_cpu(mlx4_en_stats->RJBBR) +
  231. be32_to_cpu(mlx4_en_stats->RCRC) +
  232. be32_to_cpu(mlx4_en_stats->RRUNT) +
  233. be64_to_cpu(mlx4_en_stats->RInRangeLengthErr) +
  234. be64_to_cpu(mlx4_en_stats->ROutRangeLengthErr) +
  235. be32_to_cpu(mlx4_en_stats->RSHORT) +
  236. en_stats_adder(&mlx4_en_stats->RGIANT_prio_0,
  237. &mlx4_en_stats->RGIANT_prio_1,
  238. NUM_PRIORITIES);
  239. stats->tx_errors = en_stats_adder(&mlx4_en_stats->TGIANT_prio_0,
  240. &mlx4_en_stats->TGIANT_prio_1,
  241. NUM_PRIORITIES);
  242. stats->multicast = en_stats_adder(&mlx4_en_stats->MCAST_prio_0,
  243. &mlx4_en_stats->MCAST_prio_1,
  244. NUM_PRIORITIES);
  245. stats->rx_dropped = be32_to_cpu(mlx4_en_stats->RDROP) +
  246. sw_rx_dropped;
  247. stats->rx_length_errors = be32_to_cpu(mlx4_en_stats->RdropLength);
  248. stats->rx_crc_errors = be32_to_cpu(mlx4_en_stats->RCRC);
  249. stats->rx_fifo_errors = be32_to_cpu(mlx4_en_stats->RdropOvflw);
  250. stats->tx_dropped = be32_to_cpu(mlx4_en_stats->TDROP) +
  251. sw_tx_dropped;
  252. /* RX stats */
  253. priv->pkstats.rx_multicast_packets = stats->multicast;
  254. priv->pkstats.rx_broadcast_packets =
  255. en_stats_adder(&mlx4_en_stats->RBCAST_prio_0,
  256. &mlx4_en_stats->RBCAST_prio_1,
  257. NUM_PRIORITIES);
  258. priv->pkstats.rx_jabbers = be32_to_cpu(mlx4_en_stats->RJBBR);
  259. priv->pkstats.rx_in_range_length_error =
  260. be64_to_cpu(mlx4_en_stats->RInRangeLengthErr);
  261. priv->pkstats.rx_out_range_length_error =
  262. be64_to_cpu(mlx4_en_stats->ROutRangeLengthErr);
  263. /* Tx stats */
  264. priv->pkstats.tx_multicast_packets =
  265. en_stats_adder(&mlx4_en_stats->TMCAST_prio_0,
  266. &mlx4_en_stats->TMCAST_prio_1,
  267. NUM_PRIORITIES);
  268. priv->pkstats.tx_broadcast_packets =
  269. en_stats_adder(&mlx4_en_stats->TBCAST_prio_0,
  270. &mlx4_en_stats->TBCAST_prio_1,
  271. NUM_PRIORITIES);
  272. priv->pkstats.rx_prio[0][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_0);
  273. priv->pkstats.rx_prio[0][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_0);
  274. priv->pkstats.rx_prio[1][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_1);
  275. priv->pkstats.rx_prio[1][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_1);
  276. priv->pkstats.rx_prio[2][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_2);
  277. priv->pkstats.rx_prio[2][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_2);
  278. priv->pkstats.rx_prio[3][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_3);
  279. priv->pkstats.rx_prio[3][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_3);
  280. priv->pkstats.rx_prio[4][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_4);
  281. priv->pkstats.rx_prio[4][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_4);
  282. priv->pkstats.rx_prio[5][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_5);
  283. priv->pkstats.rx_prio[5][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_5);
  284. priv->pkstats.rx_prio[6][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_6);
  285. priv->pkstats.rx_prio[6][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_6);
  286. priv->pkstats.rx_prio[7][0] = be64_to_cpu(mlx4_en_stats->RTOT_prio_7);
  287. priv->pkstats.rx_prio[7][1] = be64_to_cpu(mlx4_en_stats->ROCT_prio_7);
  288. priv->pkstats.rx_prio[8][0] = be64_to_cpu(mlx4_en_stats->RTOT_novlan);
  289. priv->pkstats.rx_prio[8][1] = be64_to_cpu(mlx4_en_stats->ROCT_novlan);
  290. priv->pkstats.tx_prio[0][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_0);
  291. priv->pkstats.tx_prio[0][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_0);
  292. priv->pkstats.tx_prio[1][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_1);
  293. priv->pkstats.tx_prio[1][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_1);
  294. priv->pkstats.tx_prio[2][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_2);
  295. priv->pkstats.tx_prio[2][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_2);
  296. priv->pkstats.tx_prio[3][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_3);
  297. priv->pkstats.tx_prio[3][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_3);
  298. priv->pkstats.tx_prio[4][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_4);
  299. priv->pkstats.tx_prio[4][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_4);
  300. priv->pkstats.tx_prio[5][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_5);
  301. priv->pkstats.tx_prio[5][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_5);
  302. priv->pkstats.tx_prio[6][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_6);
  303. priv->pkstats.tx_prio[6][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_6);
  304. priv->pkstats.tx_prio[7][0] = be64_to_cpu(mlx4_en_stats->TTOT_prio_7);
  305. priv->pkstats.tx_prio[7][1] = be64_to_cpu(mlx4_en_stats->TOCT_prio_7);
  306. priv->pkstats.tx_prio[8][0] = be64_to_cpu(mlx4_en_stats->TTOT_novlan);
  307. priv->pkstats.tx_prio[8][1] = be64_to_cpu(mlx4_en_stats->TOCT_novlan);
  308. spin_unlock_bh(&priv->stats_lock);
  309. memset(&tmp_counter_stats, 0, sizeof(tmp_counter_stats));
  310. counter_index = mlx4_get_default_counter_index(mdev->dev, port);
  311. err = mlx4_get_counter_stats(mdev->dev, counter_index,
  312. &tmp_counter_stats, reset);
  313. /* 0xffs indicates invalid value */
  314. memset(mailbox->buf, 0xff, sizeof(*flowstats) * MLX4_NUM_PRIORITIES);
  315. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN) {
  316. memset(mailbox->buf, 0,
  317. sizeof(*flowstats) * MLX4_NUM_PRIORITIES);
  318. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma,
  319. in_mod | MLX4_DUMP_ETH_STATS_FLOW_CONTROL,
  320. 0, MLX4_CMD_DUMP_ETH_STATS,
  321. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  322. if (err)
  323. goto out;
  324. }
  325. flowstats = mailbox->buf;
  326. spin_lock_bh(&priv->stats_lock);
  327. if (tmp_counter_stats.counter_mode == 0) {
  328. priv->pf_stats.rx_bytes = be64_to_cpu(tmp_counter_stats.rx_bytes);
  329. priv->pf_stats.tx_bytes = be64_to_cpu(tmp_counter_stats.tx_bytes);
  330. priv->pf_stats.rx_packets = be64_to_cpu(tmp_counter_stats.rx_frames);
  331. priv->pf_stats.tx_packets = be64_to_cpu(tmp_counter_stats.tx_frames);
  332. }
  333. for (i = 0; i < MLX4_NUM_PRIORITIES; i++) {
  334. priv->rx_priority_flowstats[i].rx_pause =
  335. be64_to_cpu(flowstats[i].rx_pause);
  336. priv->rx_priority_flowstats[i].rx_pause_duration =
  337. be64_to_cpu(flowstats[i].rx_pause_duration);
  338. priv->rx_priority_flowstats[i].rx_pause_transition =
  339. be64_to_cpu(flowstats[i].rx_pause_transition);
  340. priv->tx_priority_flowstats[i].tx_pause =
  341. be64_to_cpu(flowstats[i].tx_pause);
  342. priv->tx_priority_flowstats[i].tx_pause_duration =
  343. be64_to_cpu(flowstats[i].tx_pause_duration);
  344. priv->tx_priority_flowstats[i].tx_pause_transition =
  345. be64_to_cpu(flowstats[i].tx_pause_transition);
  346. }
  347. /* if pfc is not in use, all priorities counters have the same value */
  348. priv->rx_flowstats.rx_pause =
  349. be64_to_cpu(flowstats[0].rx_pause);
  350. priv->rx_flowstats.rx_pause_duration =
  351. be64_to_cpu(flowstats[0].rx_pause_duration);
  352. priv->rx_flowstats.rx_pause_transition =
  353. be64_to_cpu(flowstats[0].rx_pause_transition);
  354. priv->tx_flowstats.tx_pause =
  355. be64_to_cpu(flowstats[0].tx_pause);
  356. priv->tx_flowstats.tx_pause_duration =
  357. be64_to_cpu(flowstats[0].tx_pause_duration);
  358. priv->tx_flowstats.tx_pause_transition =
  359. be64_to_cpu(flowstats[0].tx_pause_transition);
  360. spin_unlock_bh(&priv->stats_lock);
  361. out:
  362. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  363. return err;
  364. }