en.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2015, 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. #include <linux/if_vlan.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/mlx5/driver.h>
  35. #include <linux/mlx5/qp.h>
  36. #include <linux/mlx5/cq.h>
  37. #include <linux/mlx5/vport.h>
  38. #include "wq.h"
  39. #include "transobj.h"
  40. #include "mlx5_core.h"
  41. #define MLX5E_MAX_NUM_TC 8
  42. #define MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE 0x7
  43. #define MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE 0xa
  44. #define MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE 0xd
  45. #define MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE 0x7
  46. #define MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE 0xa
  47. #define MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE 0xd
  48. #define MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ (16 * 1024)
  49. #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC 0x10
  50. #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS 0x20
  51. #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC 0x10
  52. #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS 0x20
  53. #define MLX5E_PARAMS_DEFAULT_MIN_RX_WQES 0x80
  54. #define MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ 0x7
  55. #define MLX5E_TX_CQ_POLL_BUDGET 128
  56. #define MLX5E_UPDATE_STATS_INTERVAL 200 /* msecs */
  57. static const char vport_strings[][ETH_GSTRING_LEN] = {
  58. /* vport statistics */
  59. "rx_packets",
  60. "rx_bytes",
  61. "tx_packets",
  62. "tx_bytes",
  63. "rx_error_packets",
  64. "rx_error_bytes",
  65. "tx_error_packets",
  66. "tx_error_bytes",
  67. "rx_unicast_packets",
  68. "rx_unicast_bytes",
  69. "tx_unicast_packets",
  70. "tx_unicast_bytes",
  71. "rx_multicast_packets",
  72. "rx_multicast_bytes",
  73. "tx_multicast_packets",
  74. "tx_multicast_bytes",
  75. "rx_broadcast_packets",
  76. "rx_broadcast_bytes",
  77. "tx_broadcast_packets",
  78. "tx_broadcast_bytes",
  79. /* SW counters */
  80. "tso_packets",
  81. "tso_bytes",
  82. "lro_packets",
  83. "lro_bytes",
  84. "rx_csum_good",
  85. "rx_csum_none",
  86. "tx_csum_offload",
  87. "tx_queue_stopped",
  88. "tx_queue_wake",
  89. "tx_queue_dropped",
  90. "rx_wqe_err",
  91. };
  92. struct mlx5e_vport_stats {
  93. /* HW counters */
  94. u64 rx_packets;
  95. u64 rx_bytes;
  96. u64 tx_packets;
  97. u64 tx_bytes;
  98. u64 rx_error_packets;
  99. u64 rx_error_bytes;
  100. u64 tx_error_packets;
  101. u64 tx_error_bytes;
  102. u64 rx_unicast_packets;
  103. u64 rx_unicast_bytes;
  104. u64 tx_unicast_packets;
  105. u64 tx_unicast_bytes;
  106. u64 rx_multicast_packets;
  107. u64 rx_multicast_bytes;
  108. u64 tx_multicast_packets;
  109. u64 tx_multicast_bytes;
  110. u64 rx_broadcast_packets;
  111. u64 rx_broadcast_bytes;
  112. u64 tx_broadcast_packets;
  113. u64 tx_broadcast_bytes;
  114. /* SW counters */
  115. u64 tso_packets;
  116. u64 tso_bytes;
  117. u64 lro_packets;
  118. u64 lro_bytes;
  119. u64 rx_csum_good;
  120. u64 rx_csum_none;
  121. u64 tx_csum_offload;
  122. u64 tx_queue_stopped;
  123. u64 tx_queue_wake;
  124. u64 tx_queue_dropped;
  125. u64 rx_wqe_err;
  126. #define NUM_VPORT_COUNTERS 31
  127. };
  128. static const char rq_stats_strings[][ETH_GSTRING_LEN] = {
  129. "packets",
  130. "csum_none",
  131. "lro_packets",
  132. "lro_bytes",
  133. "wqe_err"
  134. };
  135. struct mlx5e_rq_stats {
  136. u64 packets;
  137. u64 csum_none;
  138. u64 lro_packets;
  139. u64 lro_bytes;
  140. u64 wqe_err;
  141. #define NUM_RQ_STATS 5
  142. };
  143. static const char sq_stats_strings[][ETH_GSTRING_LEN] = {
  144. "packets",
  145. "tso_packets",
  146. "tso_bytes",
  147. "csum_offload_none",
  148. "stopped",
  149. "wake",
  150. "dropped",
  151. "nop"
  152. };
  153. struct mlx5e_sq_stats {
  154. u64 packets;
  155. u64 tso_packets;
  156. u64 tso_bytes;
  157. u64 csum_offload_none;
  158. u64 stopped;
  159. u64 wake;
  160. u64 dropped;
  161. u64 nop;
  162. #define NUM_SQ_STATS 8
  163. };
  164. struct mlx5e_stats {
  165. struct mlx5e_vport_stats vport;
  166. };
  167. struct mlx5e_params {
  168. u8 log_sq_size;
  169. u8 log_rq_size;
  170. u16 num_channels;
  171. u8 default_vlan_prio;
  172. u8 num_tc;
  173. u16 rx_cq_moderation_usec;
  174. u16 rx_cq_moderation_pkts;
  175. u16 tx_cq_moderation_usec;
  176. u16 tx_cq_moderation_pkts;
  177. u16 min_rx_wqes;
  178. u16 rx_hash_log_tbl_sz;
  179. bool lro_en;
  180. u32 lro_wqe_sz;
  181. };
  182. enum {
  183. MLX5E_RQ_STATE_POST_WQES_ENABLE,
  184. };
  185. enum cq_flags {
  186. MLX5E_CQ_HAS_CQES = 1,
  187. };
  188. struct mlx5e_cq {
  189. /* data path - accessed per cqe */
  190. struct mlx5_cqwq wq;
  191. unsigned long flags;
  192. /* data path - accessed per napi poll */
  193. struct napi_struct *napi;
  194. struct mlx5_core_cq mcq;
  195. struct mlx5e_channel *channel;
  196. /* control */
  197. struct mlx5_wq_ctrl wq_ctrl;
  198. } ____cacheline_aligned_in_smp;
  199. struct mlx5e_rq {
  200. /* data path */
  201. struct mlx5_wq_ll wq;
  202. u32 wqe_sz;
  203. struct sk_buff **skb;
  204. struct device *pdev;
  205. struct net_device *netdev;
  206. struct mlx5e_rq_stats stats;
  207. struct mlx5e_cq cq;
  208. unsigned long state;
  209. int ix;
  210. /* control */
  211. struct mlx5_wq_ctrl wq_ctrl;
  212. u32 rqn;
  213. struct mlx5e_channel *channel;
  214. } ____cacheline_aligned_in_smp;
  215. struct mlx5e_tx_skb_cb {
  216. u32 num_bytes;
  217. u8 num_wqebbs;
  218. u8 num_dma;
  219. };
  220. #define MLX5E_TX_SKB_CB(__skb) ((struct mlx5e_tx_skb_cb *)__skb->cb)
  221. struct mlx5e_sq_dma {
  222. dma_addr_t addr;
  223. u32 size;
  224. };
  225. enum {
  226. MLX5E_SQ_STATE_WAKE_TXQ_ENABLE,
  227. };
  228. struct mlx5e_sq {
  229. /* data path */
  230. /* dirtied @completion */
  231. u16 cc;
  232. u32 dma_fifo_cc;
  233. /* dirtied @xmit */
  234. u16 pc ____cacheline_aligned_in_smp;
  235. u32 dma_fifo_pc;
  236. u32 bf_offset;
  237. struct mlx5e_sq_stats stats;
  238. struct mlx5e_cq cq;
  239. /* pointers to per packet info: write@xmit, read@completion */
  240. struct sk_buff **skb;
  241. struct mlx5e_sq_dma *dma_fifo;
  242. /* read only */
  243. struct mlx5_wq_cyc wq;
  244. u32 dma_fifo_mask;
  245. void __iomem *uar_map;
  246. struct netdev_queue *txq;
  247. u32 sqn;
  248. u32 bf_buf_size;
  249. u16 max_inline;
  250. u16 edge;
  251. struct device *pdev;
  252. __be32 mkey_be;
  253. unsigned long state;
  254. /* control path */
  255. struct mlx5_wq_ctrl wq_ctrl;
  256. struct mlx5_uar uar;
  257. struct mlx5e_channel *channel;
  258. int tc;
  259. } ____cacheline_aligned_in_smp;
  260. static inline bool mlx5e_sq_has_room_for(struct mlx5e_sq *sq, u16 n)
  261. {
  262. return (((sq->wq.sz_m1 & (sq->cc - sq->pc)) >= n) ||
  263. (sq->cc == sq->pc));
  264. }
  265. enum channel_flags {
  266. MLX5E_CHANNEL_NAPI_SCHED = 1,
  267. };
  268. struct mlx5e_channel {
  269. /* data path */
  270. struct mlx5e_rq rq;
  271. struct mlx5e_sq sq[MLX5E_MAX_NUM_TC];
  272. struct napi_struct napi;
  273. struct device *pdev;
  274. struct net_device *netdev;
  275. __be32 mkey_be;
  276. u8 num_tc;
  277. unsigned long flags;
  278. int tc_to_txq_map[MLX5E_MAX_NUM_TC];
  279. /* control */
  280. struct mlx5e_priv *priv;
  281. int ix;
  282. int cpu;
  283. };
  284. enum mlx5e_traffic_types {
  285. MLX5E_TT_IPV4_TCP = 0,
  286. MLX5E_TT_IPV6_TCP = 1,
  287. MLX5E_TT_IPV4_UDP = 2,
  288. MLX5E_TT_IPV6_UDP = 3,
  289. MLX5E_TT_IPV4 = 4,
  290. MLX5E_TT_IPV6 = 5,
  291. MLX5E_TT_ANY = 6,
  292. MLX5E_NUM_TT = 7,
  293. };
  294. enum {
  295. MLX5E_RQT_SPREADING = 0,
  296. MLX5E_RQT_DEFAULT_RQ = 1,
  297. MLX5E_NUM_RQT = 2,
  298. };
  299. struct mlx5e_eth_addr_info {
  300. u8 addr[ETH_ALEN + 2];
  301. u32 tt_vec;
  302. u32 ft_ix[MLX5E_NUM_TT]; /* flow table index per traffic type */
  303. };
  304. #define MLX5E_ETH_ADDR_HASH_SIZE (1 << BITS_PER_BYTE)
  305. struct mlx5e_eth_addr_db {
  306. struct hlist_head netdev_uc[MLX5E_ETH_ADDR_HASH_SIZE];
  307. struct hlist_head netdev_mc[MLX5E_ETH_ADDR_HASH_SIZE];
  308. struct mlx5e_eth_addr_info broadcast;
  309. struct mlx5e_eth_addr_info allmulti;
  310. struct mlx5e_eth_addr_info promisc;
  311. bool broadcast_enabled;
  312. bool allmulti_enabled;
  313. bool promisc_enabled;
  314. };
  315. enum {
  316. MLX5E_STATE_ASYNC_EVENTS_ENABLE,
  317. MLX5E_STATE_OPENED,
  318. };
  319. struct mlx5e_vlan_db {
  320. unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
  321. u32 active_vlans_ft_ix[VLAN_N_VID];
  322. u32 untagged_rule_ft_ix;
  323. u32 any_vlan_rule_ft_ix;
  324. bool filter_disabled;
  325. };
  326. struct mlx5e_flow_table {
  327. void *vlan;
  328. void *main;
  329. };
  330. struct mlx5e_priv {
  331. /* priv data path fields - start */
  332. int num_tc;
  333. int default_vlan_prio;
  334. struct mlx5e_sq **txq_to_sq_map;
  335. /* priv data path fields - end */
  336. unsigned long state;
  337. struct mutex state_lock; /* Protects Interface state */
  338. struct mlx5_uar cq_uar;
  339. u32 pdn;
  340. u32 tdn;
  341. struct mlx5_core_mr mr;
  342. struct mlx5e_channel **channel;
  343. u32 tisn[MLX5E_MAX_NUM_TC];
  344. u32 rqtn;
  345. u32 tirn[MLX5E_NUM_TT];
  346. struct mlx5e_flow_table ft;
  347. struct mlx5e_eth_addr_db eth_addr;
  348. struct mlx5e_vlan_db vlan;
  349. struct mlx5e_params params;
  350. spinlock_t async_events_spinlock; /* sync hw events */
  351. struct work_struct update_carrier_work;
  352. struct work_struct set_rx_mode_work;
  353. struct delayed_work update_stats_work;
  354. struct mlx5_core_dev *mdev;
  355. struct net_device *netdev;
  356. struct mlx5e_stats stats;
  357. };
  358. #define MLX5E_NET_IP_ALIGN 2
  359. struct mlx5e_tx_wqe {
  360. struct mlx5_wqe_ctrl_seg ctrl;
  361. struct mlx5_wqe_eth_seg eth;
  362. };
  363. struct mlx5e_rx_wqe {
  364. struct mlx5_wqe_srq_next_seg next;
  365. struct mlx5_wqe_data_seg data;
  366. };
  367. enum mlx5e_link_mode {
  368. MLX5E_1000BASE_CX_SGMII = 0,
  369. MLX5E_1000BASE_KX = 1,
  370. MLX5E_10GBASE_CX4 = 2,
  371. MLX5E_10GBASE_KX4 = 3,
  372. MLX5E_10GBASE_KR = 4,
  373. MLX5E_20GBASE_KR2 = 5,
  374. MLX5E_40GBASE_CR4 = 6,
  375. MLX5E_40GBASE_KR4 = 7,
  376. MLX5E_56GBASE_R4 = 8,
  377. MLX5E_10GBASE_CR = 12,
  378. MLX5E_10GBASE_SR = 13,
  379. MLX5E_10GBASE_ER = 14,
  380. MLX5E_40GBASE_SR4 = 15,
  381. MLX5E_40GBASE_LR4 = 16,
  382. MLX5E_100GBASE_CR4 = 20,
  383. MLX5E_100GBASE_SR4 = 21,
  384. MLX5E_100GBASE_KR4 = 22,
  385. MLX5E_100GBASE_LR4 = 23,
  386. MLX5E_100BASE_TX = 24,
  387. MLX5E_100BASE_T = 25,
  388. MLX5E_10GBASE_T = 26,
  389. MLX5E_25GBASE_CR = 27,
  390. MLX5E_25GBASE_KR = 28,
  391. MLX5E_25GBASE_SR = 29,
  392. MLX5E_50GBASE_CR2 = 30,
  393. MLX5E_50GBASE_KR2 = 31,
  394. MLX5E_LINK_MODES_NUMBER,
  395. };
  396. #define MLX5E_PROT_MASK(link_mode) (1 << link_mode)
  397. void mlx5e_send_nop(struct mlx5e_sq *sq, bool notify_hw);
  398. u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
  399. void *accel_priv, select_queue_fallback_t fallback);
  400. netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev);
  401. void mlx5e_completion_event(struct mlx5_core_cq *mcq);
  402. void mlx5e_cq_error_event(struct mlx5_core_cq *mcq, enum mlx5_event event);
  403. int mlx5e_napi_poll(struct napi_struct *napi, int budget);
  404. bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq);
  405. bool mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget);
  406. bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq);
  407. struct mlx5_cqe64 *mlx5e_get_cqe(struct mlx5e_cq *cq);
  408. void mlx5e_update_stats(struct mlx5e_priv *priv);
  409. int mlx5e_open_flow_table(struct mlx5e_priv *priv);
  410. void mlx5e_close_flow_table(struct mlx5e_priv *priv);
  411. void mlx5e_init_eth_addr(struct mlx5e_priv *priv);
  412. void mlx5e_set_rx_mode_core(struct mlx5e_priv *priv);
  413. void mlx5e_set_rx_mode_work(struct work_struct *work);
  414. int mlx5e_vlan_rx_add_vid(struct net_device *dev, __always_unused __be16 proto,
  415. u16 vid);
  416. int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __always_unused __be16 proto,
  417. u16 vid);
  418. void mlx5e_enable_vlan_filter(struct mlx5e_priv *priv);
  419. void mlx5e_disable_vlan_filter(struct mlx5e_priv *priv);
  420. int mlx5e_add_all_vlan_rules(struct mlx5e_priv *priv);
  421. void mlx5e_del_all_vlan_rules(struct mlx5e_priv *priv);
  422. int mlx5e_open_locked(struct net_device *netdev);
  423. int mlx5e_close_locked(struct net_device *netdev);
  424. int mlx5e_update_priv_params(struct mlx5e_priv *priv,
  425. struct mlx5e_params *new_params);
  426. static inline void mlx5e_tx_notify_hw(struct mlx5e_sq *sq,
  427. struct mlx5e_tx_wqe *wqe)
  428. {
  429. /* ensure wqe is visible to device before updating doorbell record */
  430. dma_wmb();
  431. *sq->wq.db = cpu_to_be32(sq->pc);
  432. /* ensure doorbell record is visible to device before ringing the
  433. * doorbell
  434. */
  435. wmb();
  436. mlx5_write64((__be32 *)&wqe->ctrl,
  437. sq->uar_map + MLX5_BF_OFFSET + sq->bf_offset,
  438. NULL);
  439. sq->bf_offset ^= sq->bf_buf_size;
  440. }
  441. static inline void mlx5e_cq_arm(struct mlx5e_cq *cq)
  442. {
  443. struct mlx5_core_cq *mcq;
  444. mcq = &cq->mcq;
  445. mlx5_cq_arm(mcq, MLX5_CQ_DB_REQ_NOT, mcq->uar->map, NULL, cq->wq.cc);
  446. }
  447. extern const struct ethtool_ops mlx5e_ethtool_ops;