1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2021, Microsoft Corporation. */
10 #include "hw_channel.h"
12 /* Microsoft Azure Network Adapter (MANA)'s definitions
14 * Structures labeled with "HW DATA" are exchanged with the hardware. All of
15 * them are naturally aligned and hence don't need __packed.
18 /* MANA protocol version */
19 #define MANA_MAJOR_VERSION 0
20 #define MANA_MINOR_VERSION 1
21 #define MANA_MICRO_VERSION 1
23 typedef u64 mana_handle_t;
24 #define INVALID_MANA_HANDLE ((mana_handle_t)-1)
27 TRI_STATE_UNKNOWN = -1,
32 /* Number of entries for hardware indirection table must be in power of 2 */
33 #define MANA_INDIRECT_TABLE_SIZE 64
34 #define MANA_INDIRECT_TABLE_MASK (MANA_INDIRECT_TABLE_SIZE - 1)
36 /* The Toeplitz hash key's length in bytes: should be multiple of 8 */
37 #define MANA_HASH_KEY_SIZE 40
39 #define COMP_ENTRY_SIZE 64
41 #define RX_BUFFERS_PER_QUEUE 512
43 #define MAX_SEND_BUFFERS_PER_QUEUE 256
45 #define EQ_SIZE (8 * PAGE_SIZE)
46 #define LOG2_EQ_THROTTLE 3
48 #define MAX_PORTS_IN_MANA_DEV 256
50 /* Update this count whenever the respective structures are changed */
51 #define MANA_STATS_RX_COUNT 5
52 #define MANA_STATS_TX_COUNT 11
54 struct mana_stats_rx {
60 struct u64_stats_sync syncp;
63 struct mana_stats_tx {
69 u64 tso_inner_packets;
75 struct u64_stats_sync syncp;
79 struct gdma_queue *gdma_sq;
92 struct net_device *ndev;
94 /* The SKBs are sent to the HW and we are waiting for the CQEs. */
95 struct sk_buff_head pending_skbs;
96 struct netdev_queue *net_txq;
98 atomic_t pending_sends;
100 struct mana_stats_tx stats;
103 /* skb data and frags dma mappings */
104 struct mana_skb_head {
105 /* GSO pkts may have 2 SGEs for the linear part*/
106 dma_addr_t dma_handle[MAX_SKB_FRAGS + 2];
108 u32 size[MAX_SKB_FRAGS + 2];
111 #define MANA_HEADROOM sizeof(struct mana_skb_head)
113 enum mana_tx_pkt_format {
114 MANA_SHORT_PKT_FMT = 0,
115 MANA_LONG_PKT_FMT = 1,
118 struct mana_tx_short_oob {
120 u32 is_outer_ipv4 : 1;
121 u32 is_outer_ipv6 : 1;
122 u32 comp_iphdr_csum : 1;
123 u32 comp_tcp_csum : 1;
124 u32 comp_udp_csum : 1;
125 u32 supress_txcqe_gen : 1;
128 u32 trans_off : 10; /* Transport header offset */
130 u32 short_vp_offset : 8;
133 struct mana_tx_long_oob {
135 u32 inner_is_ipv6 : 1;
136 u32 inner_tcp_opt : 1;
137 u32 inject_vlan_pri_tag : 1;
139 u32 pcp : 3; /* 802.1Q */
140 u32 dei : 1; /* 802.1Q */
141 u32 vlan_id : 12; /* 802.1Q */
143 u32 inner_frame_offset : 10;
144 u32 inner_ip_rel_offset : 6;
145 u32 long_vp_offset : 12;
153 struct mana_tx_short_oob s_oob;
154 struct mana_tx_long_oob l_oob;
165 CQE_RX_COALESCED_4 = 2,
166 CQE_RX_OBJECT_FENCE = 3,
167 CQE_RX_TRUNCATED = 4,
171 CQE_TX_MTU_DROP = 34,
172 CQE_TX_INVALID_OOB = 35,
173 CQE_TX_INVALID_ETH_TYPE = 36,
174 CQE_TX_HDR_PROCESSING_ERROR = 37,
175 CQE_TX_VF_DISABLED = 38,
176 CQE_TX_VPORT_IDX_OUT_OF_RANGE = 39,
177 CQE_TX_VPORT_DISABLED = 40,
178 CQE_TX_VLAN_TAGGING_VIOLATION = 41,
181 #define MANA_CQE_COMPLETION 1
183 struct mana_cqe_header {
189 /* NDIS HASH Types */
190 #define NDIS_HASH_IPV4 BIT(0)
191 #define NDIS_HASH_TCP_IPV4 BIT(1)
192 #define NDIS_HASH_UDP_IPV4 BIT(2)
193 #define NDIS_HASH_IPV6 BIT(3)
194 #define NDIS_HASH_TCP_IPV6 BIT(4)
195 #define NDIS_HASH_UDP_IPV6 BIT(5)
196 #define NDIS_HASH_IPV6_EX BIT(6)
197 #define NDIS_HASH_TCP_IPV6_EX BIT(7)
198 #define NDIS_HASH_UDP_IPV6_EX BIT(8)
200 #define MANA_HASH_L3 (NDIS_HASH_IPV4 | NDIS_HASH_IPV6 | NDIS_HASH_IPV6_EX)
201 #define MANA_HASH_L4 \
202 (NDIS_HASH_TCP_IPV4 | NDIS_HASH_UDP_IPV4 | NDIS_HASH_TCP_IPV6 | \
203 NDIS_HASH_UDP_IPV6 | NDIS_HASH_TCP_IPV6_EX | NDIS_HASH_UDP_IPV6_EX)
205 struct mana_rxcomp_perpkt_info {
212 #define MANA_RXCOMP_OOB_NUM_PPI 4
214 /* Receive completion OOB */
215 struct mana_rxcomp_oob {
216 struct mana_cqe_header cqe_hdr;
219 u32 rx_vlantag_present : 1;
220 u32 rx_outer_iphdr_csum_succeed : 1;
221 u32 rx_outer_iphdr_csum_fail : 1;
224 u32 rx_iphdr_csum_succeed : 1;
225 u32 rx_iphdr_csum_fail : 1;
226 u32 rx_tcp_csum_succeed : 1;
227 u32 rx_tcp_csum_fail : 1;
228 u32 rx_udp_csum_succeed : 1;
229 u32 rx_udp_csum_fail : 1;
232 struct mana_rxcomp_perpkt_info ppi[MANA_RXCOMP_OOB_NUM_PPI];
237 struct mana_tx_comp_oob {
238 struct mana_cqe_header cqe_hdr;
242 u32 tx_sgl_offset : 5;
243 u32 tx_wqe_offset : 27;
250 #define CQE_POLLING_BUFFER 512
253 struct gdma_queue *gdma_cq;
255 /* Cache the CQ id (used to verify if each CQE comes to the right CQ. */
258 /* Type of the CQ: TX or RX */
259 enum mana_cq_type type;
261 /* Pointer to the mana_rxq that is pushing RX CQEs to the queue.
262 * Only and must be non-NULL if type is MANA_CQ_TYPE_RX.
264 struct mana_rxq *rxq;
266 /* Pointer to the mana_txq that is pushing TX CQEs to the queue.
267 * Only and must be non-NULL if type is MANA_CQ_TYPE_TX.
269 struct mana_txq *txq;
271 /* Buffer which the CQ handler can copy the CQE's into. */
272 struct gdma_comp gdma_comp_buf[CQE_POLLING_BUFFER];
275 struct napi_struct napi;
280 struct mana_recv_buf_oob {
281 /* A valid GDMA work request representing the data buffer. */
282 struct gdma_wqe_request wqe_req;
285 bool from_pool; /* allocated from a page pool */
287 /* SGL of the buffer going to be sent has part of the work request. */
289 struct gdma_sge sgl[MAX_RX_WQE_SGL_ENTRIES];
291 /* Required to store the result of mana_gd_post_work_request.
292 * gdma_posted_wqe_info.wqe_size_in_bu is required for progressing the
293 * work queue when the WQE is consumed.
295 struct gdma_posted_wqe_info wqe_inf;
298 #define MANA_RXBUF_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) \
301 #define MANA_XDP_MTU_MAX (PAGE_SIZE - MANA_RXBUF_PAD - XDP_PACKET_HEADROOM)
304 struct gdma_queue *gdma_rq;
305 /* Cache the gdma receive queue id */
308 /* Index of RQ in the vPort, not gdma receive queue id */
317 struct mana_cq rx_cq;
319 struct completion fence_event;
321 struct net_device *ndev;
323 /* Total number of receive buffers to be allocated */
328 struct mana_stats_rx stats;
330 struct bpf_prog __rcu *bpf_prog;
331 struct xdp_rxq_info xdp_rxq;
332 void *xdp_save_va; /* for reusing */
334 int xdp_rc; /* XDP redirect return code */
336 struct page_pool *page_pool;
338 /* MUST BE THE LAST MEMBER:
339 * Each receive buffer has an associated mana_recv_buf_oob.
341 struct mana_recv_buf_oob rx_oobs[] __counted_by(num_rx_buf);
347 struct mana_cq tx_cq;
349 mana_handle_t tx_object;
352 struct mana_ethtool_stats {
355 u64 hc_rx_discards_no_wqe;
356 u64 hc_rx_err_vport_disabled;
358 u64 hc_rx_ucast_pkts;
359 u64 hc_rx_ucast_bytes;
360 u64 hc_rx_bcast_pkts;
361 u64 hc_rx_bcast_bytes;
362 u64 hc_rx_mcast_pkts;
363 u64 hc_rx_mcast_bytes;
364 u64 hc_tx_err_gf_disabled;
365 u64 hc_tx_err_vport_disabled;
366 u64 hc_tx_err_inval_vportoffset_pkt;
367 u64 hc_tx_err_vlan_enforcement;
368 u64 hc_tx_err_eth_type_enforcement;
369 u64 hc_tx_err_sa_enforcement;
370 u64 hc_tx_err_sqpdid_enforcement;
371 u64 hc_tx_err_cqpdid_enforcement;
372 u64 hc_tx_err_mtu_violation;
373 u64 hc_tx_err_inval_oob;
375 u64 hc_tx_ucast_pkts;
376 u64 hc_tx_ucast_bytes;
377 u64 hc_tx_bcast_pkts;
378 u64 hc_tx_bcast_bytes;
379 u64 hc_tx_mcast_pkts;
380 u64 hc_tx_mcast_bytes;
383 u64 tx_cqe_unknown_type;
384 u64 rx_coalesced_err;
385 u64 rx_cqe_unknown_type;
388 struct mana_context {
389 struct gdma_dev *gdma_dev;
395 struct net_device *ports[MAX_PORTS_IN_MANA_DEV];
398 struct mana_port_context {
399 struct mana_context *ac;
400 struct net_device *ndev;
402 u8 mac_addr[ETH_ALEN];
404 enum TRI_STATE rss_state;
406 mana_handle_t default_rxobj;
407 bool tx_shortform_allowed;
410 struct mana_tx_qp *tx_qp;
412 /* Indirection Table for RX & TX. The values are queue indexes */
413 u32 indir_table[MANA_INDIRECT_TABLE_SIZE];
415 /* Indirection table containing RxObject Handles */
416 mana_handle_t rxobj_table[MANA_INDIRECT_TABLE_SIZE];
418 /* Hash key used by the NIC */
419 u8 hashkey[MANA_HASH_KEY_SIZE];
421 /* This points to an array of num_queues of RQ pointers. */
422 struct mana_rxq **rxqs;
424 /* pre-allocated rx buffer array */
429 u32 rxbpre_alloc_size;
432 struct bpf_prog *bpf_prog;
434 /* Create num_queues EQs, SQs, SQ-CQs, RQs and RQ-CQs, respectively. */
435 unsigned int max_queues;
436 unsigned int num_queues;
438 mana_handle_t port_handle;
439 mana_handle_t pf_filter_handle;
441 /* Mutex for sharing access to vport_use_count */
442 struct mutex vport_mutex;
448 bool port_st_save; /* Saved port state */
450 struct mana_ethtool_stats eth_stats;
453 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev);
454 int mana_config_rss(struct mana_port_context *ac, enum TRI_STATE rx,
455 bool update_hash, bool update_tab);
457 int mana_alloc_queues(struct net_device *ndev);
458 int mana_attach(struct net_device *ndev);
459 int mana_detach(struct net_device *ndev, bool from_close);
461 int mana_probe(struct gdma_dev *gd, bool resuming);
462 void mana_remove(struct gdma_dev *gd, bool suspending);
464 void mana_xdp_tx(struct sk_buff *skb, struct net_device *ndev);
465 int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
467 u32 mana_run_xdp(struct net_device *ndev, struct mana_rxq *rxq,
468 struct xdp_buff *xdp, void *buf_va, uint pkt_len);
469 struct bpf_prog *mana_xdp_get(struct mana_port_context *apc);
470 void mana_chn_setxdp(struct mana_port_context *apc, struct bpf_prog *prog);
471 int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf);
472 void mana_query_gf_stats(struct mana_port_context *apc);
474 extern const struct ethtool_ops mana_ethtool_ops;
476 /* A CQ can be created not associated with any EQ */
477 #define GDMA_CQ_NO_EQ 0xffff
479 struct mana_obj_spec {
487 enum mana_command_code {
488 MANA_QUERY_DEV_CONFIG = 0x20001,
489 MANA_QUERY_GF_STAT = 0x20002,
490 MANA_CONFIG_VPORT_TX = 0x20003,
491 MANA_CREATE_WQ_OBJ = 0x20004,
492 MANA_DESTROY_WQ_OBJ = 0x20005,
493 MANA_FENCE_RQ = 0x20006,
494 MANA_CONFIG_VPORT_RX = 0x20007,
495 MANA_QUERY_VPORT_CONFIG = 0x20008,
497 /* Privileged commands for the PF mode */
498 MANA_REGISTER_FILTER = 0x28000,
499 MANA_DEREGISTER_FILTER = 0x28001,
500 MANA_REGISTER_HW_PORT = 0x28003,
501 MANA_DEREGISTER_HW_PORT = 0x28004,
504 /* Query Device Configuration */
505 struct mana_query_device_cfg_req {
506 struct gdma_req_hdr hdr;
508 /* MANA Nic Driver Capability flags */
509 u64 mn_drv_cap_flags1;
510 u64 mn_drv_cap_flags2;
511 u64 mn_drv_cap_flags3;
512 u64 mn_drv_cap_flags4;
521 struct mana_query_device_cfg_resp {
522 struct gdma_resp_hdr hdr;
539 /* Query vPort Configuration */
540 struct mana_query_vport_cfg_req {
541 struct gdma_req_hdr hdr;
545 struct mana_query_vport_cfg_resp {
546 struct gdma_resp_hdr hdr;
549 u32 num_indirection_ent;
556 /* Configure vPort */
557 struct mana_config_vport_req {
558 struct gdma_req_hdr hdr;
564 struct mana_config_vport_resp {
565 struct gdma_resp_hdr hdr;
567 u8 short_form_allowed;
571 /* Create WQ Object */
572 struct mana_create_wqobj_req {
573 struct gdma_req_hdr hdr;
581 u32 cq_moderation_ctx_id;
585 struct mana_create_wqobj_resp {
586 struct gdma_resp_hdr hdr;
589 mana_handle_t wq_obj;
592 /* Destroy WQ Object */
593 struct mana_destroy_wqobj_req {
594 struct gdma_req_hdr hdr;
597 mana_handle_t wq_obj_handle;
600 struct mana_destroy_wqobj_resp {
601 struct gdma_resp_hdr hdr;
605 struct mana_fence_rq_req {
606 struct gdma_req_hdr hdr;
607 mana_handle_t wq_obj_handle;
610 struct mana_fence_rq_resp {
611 struct gdma_resp_hdr hdr;
615 struct mana_query_gf_stat_req {
616 struct gdma_req_hdr hdr;
620 struct mana_query_gf_stat_resp {
621 struct gdma_resp_hdr hdr;
623 /* rx errors/discards */
624 u64 rx_discards_nowqe;
625 u64 rx_err_vport_disabled;
626 /* rx bytes/packets */
628 u64 hc_rx_ucast_pkts;
629 u64 hc_rx_ucast_bytes;
630 u64 hc_rx_bcast_pkts;
631 u64 hc_rx_bcast_bytes;
632 u64 hc_rx_mcast_pkts;
633 u64 hc_rx_mcast_bytes;
635 u64 tx_err_gf_disabled;
636 u64 tx_err_vport_disabled;
637 u64 tx_err_inval_vport_offset_pkt;
638 u64 tx_err_vlan_enforcement;
639 u64 tx_err_ethtype_enforcement;
640 u64 tx_err_SA_enforcement;
641 u64 tx_err_SQPDID_enforcement;
642 u64 tx_err_CQPDID_enforcement;
643 u64 tx_err_mtu_violation;
644 u64 tx_err_inval_oob;
645 /* tx bytes/packets */
647 u64 hc_tx_ucast_pkts;
648 u64 hc_tx_ucast_bytes;
649 u64 hc_tx_bcast_pkts;
650 u64 hc_tx_bcast_bytes;
651 u64 hc_tx_mcast_pkts;
652 u64 hc_tx_mcast_bytes;
657 /* Configure vPort Rx Steering */
658 struct mana_cfg_rx_steer_req_v2 {
659 struct gdma_req_hdr hdr;
661 u16 num_indir_entries;
662 u16 indir_tab_offset;
665 u8 update_default_rxobj;
669 mana_handle_t default_rxobj;
670 u8 hashkey[MANA_HASH_KEY_SIZE];
671 u8 cqe_coalescing_enable;
673 mana_handle_t indir_tab[] __counted_by(num_indir_entries);
676 struct mana_cfg_rx_steer_resp {
677 struct gdma_resp_hdr hdr;
680 /* Register HW vPort */
681 struct mana_register_hw_vport_req {
682 struct gdma_req_hdr hdr;
684 u8 is_pf_default_vport;
686 u8 allow_all_ether_types;
692 struct mana_register_hw_vport_resp {
693 struct gdma_resp_hdr hdr;
694 mana_handle_t hw_vport_handle;
697 /* Deregister HW vPort */
698 struct mana_deregister_hw_vport_req {
699 struct gdma_req_hdr hdr;
700 mana_handle_t hw_vport_handle;
703 struct mana_deregister_hw_vport_resp {
704 struct gdma_resp_hdr hdr;
707 /* Register filter */
708 struct mana_register_filter_req {
709 struct gdma_req_hdr hdr;
722 struct mana_register_filter_resp {
723 struct gdma_resp_hdr hdr;
724 mana_handle_t filter_handle;
727 /* Deregister filter */
728 struct mana_deregister_filter_req {
729 struct gdma_req_hdr hdr;
730 mana_handle_t filter_handle;
733 struct mana_deregister_filter_resp {
734 struct gdma_resp_hdr hdr;
737 /* Requested GF stats Flags */
738 /* Rx discards/Errors */
739 #define STATISTICS_FLAGS_RX_DISCARDS_NO_WQE 0x0000000000000001
740 #define STATISTICS_FLAGS_RX_ERRORS_VPORT_DISABLED 0x0000000000000002
742 #define STATISTICS_FLAGS_HC_RX_BYTES 0x0000000000000004
743 #define STATISTICS_FLAGS_HC_RX_UCAST_PACKETS 0x0000000000000008
744 #define STATISTICS_FLAGS_HC_RX_UCAST_BYTES 0x0000000000000010
745 #define STATISTICS_FLAGS_HC_RX_MCAST_PACKETS 0x0000000000000020
746 #define STATISTICS_FLAGS_HC_RX_MCAST_BYTES 0x0000000000000040
747 #define STATISTICS_FLAGS_HC_RX_BCAST_PACKETS 0x0000000000000080
748 #define STATISTICS_FLAGS_HC_RX_BCAST_BYTES 0x0000000000000100
750 #define STATISTICS_FLAGS_TX_ERRORS_GF_DISABLED 0x0000000000000200
751 #define STATISTICS_FLAGS_TX_ERRORS_VPORT_DISABLED 0x0000000000000400
752 #define STATISTICS_FLAGS_TX_ERRORS_INVAL_VPORT_OFFSET_PACKETS \
754 #define STATISTICS_FLAGS_TX_ERRORS_VLAN_ENFORCEMENT 0x0000000000001000
755 #define STATISTICS_FLAGS_TX_ERRORS_ETH_TYPE_ENFORCEMENT \
757 #define STATISTICS_FLAGS_TX_ERRORS_SA_ENFORCEMENT 0x0000000000004000
758 #define STATISTICS_FLAGS_TX_ERRORS_SQPDID_ENFORCEMENT 0x0000000000008000
759 #define STATISTICS_FLAGS_TX_ERRORS_CQPDID_ENFORCEMENT 0x0000000000010000
760 #define STATISTICS_FLAGS_TX_ERRORS_MTU_VIOLATION 0x0000000000020000
761 #define STATISTICS_FLAGS_TX_ERRORS_INVALID_OOB 0x0000000000040000
763 #define STATISTICS_FLAGS_HC_TX_BYTES 0x0000000000080000
764 #define STATISTICS_FLAGS_HC_TX_UCAST_PACKETS 0x0000000000100000
765 #define STATISTICS_FLAGS_HC_TX_UCAST_BYTES 0x0000000000200000
766 #define STATISTICS_FLAGS_HC_TX_MCAST_PACKETS 0x0000000000400000
767 #define STATISTICS_FLAGS_HC_TX_MCAST_BYTES 0x0000000000800000
768 #define STATISTICS_FLAGS_HC_TX_BCAST_PACKETS 0x0000000001000000
769 #define STATISTICS_FLAGS_HC_TX_BCAST_BYTES 0x0000000002000000
771 #define STATISTICS_FLAGS_TX_ERRORS_GDMA_ERROR 0x0000000004000000
773 #define MANA_MAX_NUM_QUEUES 64
775 #define MANA_SHORT_VPORT_OFFSET_MAX ((1U << 8) - 1)
777 struct mana_tx_package {
778 struct gdma_wqe_request wqe_req;
779 struct gdma_sge sgl_array[5];
780 struct gdma_sge *sgl_ptr;
782 struct mana_tx_oob tx_oob;
784 struct gdma_posted_wqe_info wqe_info;
787 int mana_create_wq_obj(struct mana_port_context *apc,
789 u32 wq_type, struct mana_obj_spec *wq_spec,
790 struct mana_obj_spec *cq_spec,
791 mana_handle_t *wq_obj);
793 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
794 mana_handle_t wq_obj);
796 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
798 void mana_uncfg_vport(struct mana_port_context *apc);