xref: /linux/include/net/mana/mana.h (revision 8ca4fc323d2e4ab9dabbdd57633af40b0c7e6af9)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2021, Microsoft Corporation. */
3 
4 #ifndef _MANA_H
5 #define _MANA_H
6 
7 #include "gdma.h"
8 #include "hw_channel.h"
9 
10 /* Microsoft Azure Network Adapter (MANA)'s definitions
11  *
12  * Structures labeled with "HW DATA" are exchanged with the hardware. All of
13  * them are naturally aligned and hence don't need __packed.
14  */
15 
16 /* MANA protocol version */
17 #define MANA_MAJOR_VERSION	0
18 #define MANA_MINOR_VERSION	1
19 #define MANA_MICRO_VERSION	1
20 
21 typedef u64 mana_handle_t;
22 #define INVALID_MANA_HANDLE ((mana_handle_t)-1)
23 
24 enum TRI_STATE {
25 	TRI_STATE_UNKNOWN = -1,
26 	TRI_STATE_FALSE = 0,
27 	TRI_STATE_TRUE = 1
28 };
29 
30 /* Number of entries for hardware indirection table must be in power of 2 */
31 #define MANA_INDIRECT_TABLE_SIZE 64
32 #define MANA_INDIRECT_TABLE_MASK (MANA_INDIRECT_TABLE_SIZE - 1)
33 
34 /* The Toeplitz hash key's length in bytes: should be multiple of 8 */
35 #define MANA_HASH_KEY_SIZE 40
36 
37 #define COMP_ENTRY_SIZE 64
38 
39 #define ADAPTER_MTU_SIZE 1500
40 #define MAX_FRAME_SIZE (ADAPTER_MTU_SIZE + 14)
41 
42 #define RX_BUFFERS_PER_QUEUE 512
43 
44 #define MAX_SEND_BUFFERS_PER_QUEUE 256
45 
46 #define EQ_SIZE (8 * PAGE_SIZE)
47 #define LOG2_EQ_THROTTLE 3
48 
49 #define MAX_PORTS_IN_MANA_DEV 256
50 
51 struct mana_stats_rx {
52 	u64 packets;
53 	u64 bytes;
54 	u64 xdp_drop;
55 	u64 xdp_tx;
56 	u64 xdp_redirect;
57 	struct u64_stats_sync syncp;
58 };
59 
60 struct mana_stats_tx {
61 	u64 packets;
62 	u64 bytes;
63 	u64 xdp_xmit;
64 	struct u64_stats_sync syncp;
65 };
66 
67 struct mana_txq {
68 	struct gdma_queue *gdma_sq;
69 
70 	union {
71 		u32 gdma_txq_id;
72 		struct {
73 			u32 reserved1	: 10;
74 			u32 vsq_frame	: 14;
75 			u32 reserved2	: 8;
76 		};
77 	};
78 
79 	u16 vp_offset;
80 
81 	struct net_device *ndev;
82 
83 	/* The SKBs are sent to the HW and we are waiting for the CQEs. */
84 	struct sk_buff_head pending_skbs;
85 	struct netdev_queue *net_txq;
86 
87 	atomic_t pending_sends;
88 
89 	struct mana_stats_tx stats;
90 };
91 
92 /* skb data and frags dma mappings */
93 struct mana_skb_head {
94 	dma_addr_t dma_handle[MAX_SKB_FRAGS + 1];
95 
96 	u32 size[MAX_SKB_FRAGS + 1];
97 };
98 
99 #define MANA_HEADROOM sizeof(struct mana_skb_head)
100 
101 enum mana_tx_pkt_format {
102 	MANA_SHORT_PKT_FMT	= 0,
103 	MANA_LONG_PKT_FMT	= 1,
104 };
105 
106 struct mana_tx_short_oob {
107 	u32 pkt_fmt		: 2;
108 	u32 is_outer_ipv4	: 1;
109 	u32 is_outer_ipv6	: 1;
110 	u32 comp_iphdr_csum	: 1;
111 	u32 comp_tcp_csum	: 1;
112 	u32 comp_udp_csum	: 1;
113 	u32 supress_txcqe_gen	: 1;
114 	u32 vcq_num		: 24;
115 
116 	u32 trans_off		: 10; /* Transport header offset */
117 	u32 vsq_frame		: 14;
118 	u32 short_vp_offset	: 8;
119 }; /* HW DATA */
120 
121 struct mana_tx_long_oob {
122 	u32 is_encap		: 1;
123 	u32 inner_is_ipv6	: 1;
124 	u32 inner_tcp_opt	: 1;
125 	u32 inject_vlan_pri_tag : 1;
126 	u32 reserved1		: 12;
127 	u32 pcp			: 3;  /* 802.1Q */
128 	u32 dei			: 1;  /* 802.1Q */
129 	u32 vlan_id		: 12; /* 802.1Q */
130 
131 	u32 inner_frame_offset	: 10;
132 	u32 inner_ip_rel_offset : 6;
133 	u32 long_vp_offset	: 12;
134 	u32 reserved2		: 4;
135 
136 	u32 reserved3;
137 	u32 reserved4;
138 }; /* HW DATA */
139 
140 struct mana_tx_oob {
141 	struct mana_tx_short_oob s_oob;
142 	struct mana_tx_long_oob l_oob;
143 }; /* HW DATA */
144 
145 enum mana_cq_type {
146 	MANA_CQ_TYPE_RX,
147 	MANA_CQ_TYPE_TX,
148 };
149 
150 enum mana_cqe_type {
151 	CQE_INVALID			= 0,
152 	CQE_RX_OKAY			= 1,
153 	CQE_RX_COALESCED_4		= 2,
154 	CQE_RX_OBJECT_FENCE		= 3,
155 	CQE_RX_TRUNCATED		= 4,
156 
157 	CQE_TX_OKAY			= 32,
158 	CQE_TX_SA_DROP			= 33,
159 	CQE_TX_MTU_DROP			= 34,
160 	CQE_TX_INVALID_OOB		= 35,
161 	CQE_TX_INVALID_ETH_TYPE		= 36,
162 	CQE_TX_HDR_PROCESSING_ERROR	= 37,
163 	CQE_TX_VF_DISABLED		= 38,
164 	CQE_TX_VPORT_IDX_OUT_OF_RANGE	= 39,
165 	CQE_TX_VPORT_DISABLED		= 40,
166 	CQE_TX_VLAN_TAGGING_VIOLATION	= 41,
167 };
168 
169 #define MANA_CQE_COMPLETION 1
170 
171 struct mana_cqe_header {
172 	u32 cqe_type	: 6;
173 	u32 client_type	: 2;
174 	u32 vendor_err	: 24;
175 }; /* HW DATA */
176 
177 /* NDIS HASH Types */
178 #define NDIS_HASH_IPV4		BIT(0)
179 #define NDIS_HASH_TCP_IPV4	BIT(1)
180 #define NDIS_HASH_UDP_IPV4	BIT(2)
181 #define NDIS_HASH_IPV6		BIT(3)
182 #define NDIS_HASH_TCP_IPV6	BIT(4)
183 #define NDIS_HASH_UDP_IPV6	BIT(5)
184 #define NDIS_HASH_IPV6_EX	BIT(6)
185 #define NDIS_HASH_TCP_IPV6_EX	BIT(7)
186 #define NDIS_HASH_UDP_IPV6_EX	BIT(8)
187 
188 #define MANA_HASH_L3 (NDIS_HASH_IPV4 | NDIS_HASH_IPV6 | NDIS_HASH_IPV6_EX)
189 #define MANA_HASH_L4                                                         \
190 	(NDIS_HASH_TCP_IPV4 | NDIS_HASH_UDP_IPV4 | NDIS_HASH_TCP_IPV6 |      \
191 	 NDIS_HASH_UDP_IPV6 | NDIS_HASH_TCP_IPV6_EX | NDIS_HASH_UDP_IPV6_EX)
192 
193 struct mana_rxcomp_perpkt_info {
194 	u32 pkt_len	: 16;
195 	u32 reserved1	: 16;
196 	u32 reserved2;
197 	u32 pkt_hash;
198 }; /* HW DATA */
199 
200 #define MANA_RXCOMP_OOB_NUM_PPI 4
201 
202 /* Receive completion OOB */
203 struct mana_rxcomp_oob {
204 	struct mana_cqe_header cqe_hdr;
205 
206 	u32 rx_vlan_id			: 12;
207 	u32 rx_vlantag_present		: 1;
208 	u32 rx_outer_iphdr_csum_succeed	: 1;
209 	u32 rx_outer_iphdr_csum_fail	: 1;
210 	u32 reserved1			: 1;
211 	u32 rx_hashtype			: 9;
212 	u32 rx_iphdr_csum_succeed	: 1;
213 	u32 rx_iphdr_csum_fail		: 1;
214 	u32 rx_tcp_csum_succeed		: 1;
215 	u32 rx_tcp_csum_fail		: 1;
216 	u32 rx_udp_csum_succeed		: 1;
217 	u32 rx_udp_csum_fail		: 1;
218 	u32 reserved2			: 1;
219 
220 	struct mana_rxcomp_perpkt_info ppi[MANA_RXCOMP_OOB_NUM_PPI];
221 
222 	u32 rx_wqe_offset;
223 }; /* HW DATA */
224 
225 struct mana_tx_comp_oob {
226 	struct mana_cqe_header cqe_hdr;
227 
228 	u32 tx_data_offset;
229 
230 	u32 tx_sgl_offset	: 5;
231 	u32 tx_wqe_offset	: 27;
232 
233 	u32 reserved[12];
234 }; /* HW DATA */
235 
236 struct mana_rxq;
237 
238 #define CQE_POLLING_BUFFER 512
239 
240 struct mana_cq {
241 	struct gdma_queue *gdma_cq;
242 
243 	/* Cache the CQ id (used to verify if each CQE comes to the right CQ. */
244 	u32 gdma_id;
245 
246 	/* Type of the CQ: TX or RX */
247 	enum mana_cq_type type;
248 
249 	/* Pointer to the mana_rxq that is pushing RX CQEs to the queue.
250 	 * Only and must be non-NULL if type is MANA_CQ_TYPE_RX.
251 	 */
252 	struct mana_rxq *rxq;
253 
254 	/* Pointer to the mana_txq that is pushing TX CQEs to the queue.
255 	 * Only and must be non-NULL if type is MANA_CQ_TYPE_TX.
256 	 */
257 	struct mana_txq *txq;
258 
259 	/* Buffer which the CQ handler can copy the CQE's into. */
260 	struct gdma_comp gdma_comp_buf[CQE_POLLING_BUFFER];
261 
262 	/* NAPI data */
263 	struct napi_struct napi;
264 	int work_done;
265 	int budget;
266 };
267 
268 struct mana_recv_buf_oob {
269 	/* A valid GDMA work request representing the data buffer. */
270 	struct gdma_wqe_request wqe_req;
271 
272 	void *buf_va;
273 	dma_addr_t buf_dma_addr;
274 
275 	/* SGL of the buffer going to be sent has part of the work request. */
276 	u32 num_sge;
277 	struct gdma_sge sgl[MAX_RX_WQE_SGL_ENTRIES];
278 
279 	/* Required to store the result of mana_gd_post_work_request.
280 	 * gdma_posted_wqe_info.wqe_size_in_bu is required for progressing the
281 	 * work queue when the WQE is consumed.
282 	 */
283 	struct gdma_posted_wqe_info wqe_inf;
284 };
285 
286 struct mana_rxq {
287 	struct gdma_queue *gdma_rq;
288 	/* Cache the gdma receive queue id */
289 	u32 gdma_id;
290 
291 	/* Index of RQ in the vPort, not gdma receive queue id */
292 	u32 rxq_idx;
293 
294 	u32 datasize;
295 
296 	mana_handle_t rxobj;
297 
298 	struct mana_cq rx_cq;
299 
300 	struct completion fence_event;
301 
302 	struct net_device *ndev;
303 
304 	/* Total number of receive buffers to be allocated */
305 	u32 num_rx_buf;
306 
307 	u32 buf_index;
308 
309 	struct mana_stats_rx stats;
310 
311 	struct bpf_prog __rcu *bpf_prog;
312 	struct xdp_rxq_info xdp_rxq;
313 	struct page *xdp_save_page;
314 	bool xdp_flush;
315 	int xdp_rc; /* XDP redirect return code */
316 
317 	/* MUST BE THE LAST MEMBER:
318 	 * Each receive buffer has an associated mana_recv_buf_oob.
319 	 */
320 	struct mana_recv_buf_oob rx_oobs[];
321 };
322 
323 struct mana_tx_qp {
324 	struct mana_txq txq;
325 
326 	struct mana_cq tx_cq;
327 
328 	mana_handle_t tx_object;
329 };
330 
331 struct mana_ethtool_stats {
332 	u64 stop_queue;
333 	u64 wake_queue;
334 };
335 
336 struct mana_context {
337 	struct gdma_dev *gdma_dev;
338 
339 	u16 num_ports;
340 
341 	struct mana_eq *eqs;
342 
343 	struct net_device *ports[MAX_PORTS_IN_MANA_DEV];
344 };
345 
346 struct mana_port_context {
347 	struct mana_context *ac;
348 	struct net_device *ndev;
349 
350 	u8 mac_addr[ETH_ALEN];
351 
352 	enum TRI_STATE rss_state;
353 
354 	mana_handle_t default_rxobj;
355 	bool tx_shortform_allowed;
356 	u16 tx_vp_offset;
357 
358 	struct mana_tx_qp *tx_qp;
359 
360 	/* Indirection Table for RX & TX. The values are queue indexes */
361 	u32 indir_table[MANA_INDIRECT_TABLE_SIZE];
362 
363 	/* Indirection table containing RxObject Handles */
364 	mana_handle_t rxobj_table[MANA_INDIRECT_TABLE_SIZE];
365 
366 	/*  Hash key used by the NIC */
367 	u8 hashkey[MANA_HASH_KEY_SIZE];
368 
369 	/* This points to an array of num_queues of RQ pointers. */
370 	struct mana_rxq **rxqs;
371 
372 	struct bpf_prog *bpf_prog;
373 
374 	/* Create num_queues EQs, SQs, SQ-CQs, RQs and RQ-CQs, respectively. */
375 	unsigned int max_queues;
376 	unsigned int num_queues;
377 
378 	mana_handle_t port_handle;
379 	mana_handle_t pf_filter_handle;
380 
381 	/* Mutex for sharing access to vport_use_count */
382 	struct mutex vport_mutex;
383 	int vport_use_count;
384 
385 	u16 port_idx;
386 
387 	bool port_is_up;
388 	bool port_st_save; /* Saved port state */
389 
390 	struct mana_ethtool_stats eth_stats;
391 };
392 
393 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev);
394 int mana_config_rss(struct mana_port_context *ac, enum TRI_STATE rx,
395 		    bool update_hash, bool update_tab);
396 
397 int mana_alloc_queues(struct net_device *ndev);
398 int mana_attach(struct net_device *ndev);
399 int mana_detach(struct net_device *ndev, bool from_close);
400 
401 int mana_probe(struct gdma_dev *gd, bool resuming);
402 void mana_remove(struct gdma_dev *gd, bool suspending);
403 
404 void mana_xdp_tx(struct sk_buff *skb, struct net_device *ndev);
405 int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
406 		  u32 flags);
407 u32 mana_run_xdp(struct net_device *ndev, struct mana_rxq *rxq,
408 		 struct xdp_buff *xdp, void *buf_va, uint pkt_len);
409 struct bpf_prog *mana_xdp_get(struct mana_port_context *apc);
410 void mana_chn_setxdp(struct mana_port_context *apc, struct bpf_prog *prog);
411 int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf);
412 
413 extern const struct ethtool_ops mana_ethtool_ops;
414 
415 struct mana_obj_spec {
416 	u32 queue_index;
417 	u64 gdma_region;
418 	u32 queue_size;
419 	u32 attached_eq;
420 	u32 modr_ctx_id;
421 };
422 
423 enum mana_command_code {
424 	MANA_QUERY_DEV_CONFIG	= 0x20001,
425 	MANA_QUERY_GF_STAT	= 0x20002,
426 	MANA_CONFIG_VPORT_TX	= 0x20003,
427 	MANA_CREATE_WQ_OBJ	= 0x20004,
428 	MANA_DESTROY_WQ_OBJ	= 0x20005,
429 	MANA_FENCE_RQ		= 0x20006,
430 	MANA_CONFIG_VPORT_RX	= 0x20007,
431 	MANA_QUERY_VPORT_CONFIG	= 0x20008,
432 
433 	/* Privileged commands for the PF mode */
434 	MANA_REGISTER_FILTER	= 0x28000,
435 	MANA_DEREGISTER_FILTER	= 0x28001,
436 	MANA_REGISTER_HW_PORT	= 0x28003,
437 	MANA_DEREGISTER_HW_PORT	= 0x28004,
438 };
439 
440 /* Query Device Configuration */
441 struct mana_query_device_cfg_req {
442 	struct gdma_req_hdr hdr;
443 
444 	/* MANA Nic Driver Capability flags */
445 	u64 mn_drv_cap_flags1;
446 	u64 mn_drv_cap_flags2;
447 	u64 mn_drv_cap_flags3;
448 	u64 mn_drv_cap_flags4;
449 
450 	u32 proto_major_ver;
451 	u32 proto_minor_ver;
452 	u32 proto_micro_ver;
453 
454 	u32 reserved;
455 }; /* HW DATA */
456 
457 struct mana_query_device_cfg_resp {
458 	struct gdma_resp_hdr hdr;
459 
460 	u64 pf_cap_flags1;
461 	u64 pf_cap_flags2;
462 	u64 pf_cap_flags3;
463 	u64 pf_cap_flags4;
464 
465 	u16 max_num_vports;
466 	u16 reserved;
467 	u32 max_num_eqs;
468 }; /* HW DATA */
469 
470 /* Query vPort Configuration */
471 struct mana_query_vport_cfg_req {
472 	struct gdma_req_hdr hdr;
473 	u32 vport_index;
474 }; /* HW DATA */
475 
476 struct mana_query_vport_cfg_resp {
477 	struct gdma_resp_hdr hdr;
478 	u32 max_num_sq;
479 	u32 max_num_rq;
480 	u32 num_indirection_ent;
481 	u32 reserved1;
482 	u8 mac_addr[6];
483 	u8 reserved2[2];
484 	mana_handle_t vport;
485 }; /* HW DATA */
486 
487 /* Configure vPort */
488 struct mana_config_vport_req {
489 	struct gdma_req_hdr hdr;
490 	mana_handle_t vport;
491 	u32 pdid;
492 	u32 doorbell_pageid;
493 }; /* HW DATA */
494 
495 struct mana_config_vport_resp {
496 	struct gdma_resp_hdr hdr;
497 	u16 tx_vport_offset;
498 	u8 short_form_allowed;
499 	u8 reserved;
500 }; /* HW DATA */
501 
502 /* Create WQ Object */
503 struct mana_create_wqobj_req {
504 	struct gdma_req_hdr hdr;
505 	mana_handle_t vport;
506 	u32 wq_type;
507 	u32 reserved;
508 	u64 wq_gdma_region;
509 	u64 cq_gdma_region;
510 	u32 wq_size;
511 	u32 cq_size;
512 	u32 cq_moderation_ctx_id;
513 	u32 cq_parent_qid;
514 }; /* HW DATA */
515 
516 struct mana_create_wqobj_resp {
517 	struct gdma_resp_hdr hdr;
518 	u32 wq_id;
519 	u32 cq_id;
520 	mana_handle_t wq_obj;
521 }; /* HW DATA */
522 
523 /* Destroy WQ Object */
524 struct mana_destroy_wqobj_req {
525 	struct gdma_req_hdr hdr;
526 	u32 wq_type;
527 	u32 reserved;
528 	mana_handle_t wq_obj_handle;
529 }; /* HW DATA */
530 
531 struct mana_destroy_wqobj_resp {
532 	struct gdma_resp_hdr hdr;
533 }; /* HW DATA */
534 
535 /* Fence RQ */
536 struct mana_fence_rq_req {
537 	struct gdma_req_hdr hdr;
538 	mana_handle_t wq_obj_handle;
539 }; /* HW DATA */
540 
541 struct mana_fence_rq_resp {
542 	struct gdma_resp_hdr hdr;
543 }; /* HW DATA */
544 
545 /* Configure vPort Rx Steering */
546 struct mana_cfg_rx_steer_req {
547 	struct gdma_req_hdr hdr;
548 	mana_handle_t vport;
549 	u16 num_indir_entries;
550 	u16 indir_tab_offset;
551 	u32 rx_enable;
552 	u32 rss_enable;
553 	u8 update_default_rxobj;
554 	u8 update_hashkey;
555 	u8 update_indir_tab;
556 	u8 reserved;
557 	mana_handle_t default_rxobj;
558 	u8 hashkey[MANA_HASH_KEY_SIZE];
559 }; /* HW DATA */
560 
561 struct mana_cfg_rx_steer_resp {
562 	struct gdma_resp_hdr hdr;
563 }; /* HW DATA */
564 
565 /* Register HW vPort */
566 struct mana_register_hw_vport_req {
567 	struct gdma_req_hdr hdr;
568 	u16 attached_gfid;
569 	u8 is_pf_default_vport;
570 	u8 reserved1;
571 	u8 allow_all_ether_types;
572 	u8 reserved2;
573 	u8 reserved3;
574 	u8 reserved4;
575 }; /* HW DATA */
576 
577 struct mana_register_hw_vport_resp {
578 	struct gdma_resp_hdr hdr;
579 	mana_handle_t hw_vport_handle;
580 }; /* HW DATA */
581 
582 /* Deregister HW vPort */
583 struct mana_deregister_hw_vport_req {
584 	struct gdma_req_hdr hdr;
585 	mana_handle_t hw_vport_handle;
586 }; /* HW DATA */
587 
588 struct mana_deregister_hw_vport_resp {
589 	struct gdma_resp_hdr hdr;
590 }; /* HW DATA */
591 
592 /* Register filter */
593 struct mana_register_filter_req {
594 	struct gdma_req_hdr hdr;
595 	mana_handle_t vport;
596 	u8 mac_addr[6];
597 	u8 reserved1;
598 	u8 reserved2;
599 	u8 reserved3;
600 	u8 reserved4;
601 	u16 reserved5;
602 	u32 reserved6;
603 	u32 reserved7;
604 	u32 reserved8;
605 }; /* HW DATA */
606 
607 struct mana_register_filter_resp {
608 	struct gdma_resp_hdr hdr;
609 	mana_handle_t filter_handle;
610 }; /* HW DATA */
611 
612 /* Deregister filter */
613 struct mana_deregister_filter_req {
614 	struct gdma_req_hdr hdr;
615 	mana_handle_t filter_handle;
616 }; /* HW DATA */
617 
618 struct mana_deregister_filter_resp {
619 	struct gdma_resp_hdr hdr;
620 }; /* HW DATA */
621 
622 #define MANA_MAX_NUM_QUEUES 64
623 
624 #define MANA_SHORT_VPORT_OFFSET_MAX ((1U << 8) - 1)
625 
626 struct mana_tx_package {
627 	struct gdma_wqe_request wqe_req;
628 	struct gdma_sge sgl_array[5];
629 	struct gdma_sge *sgl_ptr;
630 
631 	struct mana_tx_oob tx_oob;
632 
633 	struct gdma_posted_wqe_info wqe_info;
634 };
635 
636 int mana_create_wq_obj(struct mana_port_context *apc,
637 		       mana_handle_t vport,
638 		       u32 wq_type, struct mana_obj_spec *wq_spec,
639 		       struct mana_obj_spec *cq_spec,
640 		       mana_handle_t *wq_obj);
641 
642 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
643 			 mana_handle_t wq_obj);
644 
645 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
646 		   u32 doorbell_pg_id);
647 void mana_uncfg_vport(struct mana_port_context *apc);
648 #endif /* _MANA_H */
649