xref: /linux/include/net/mana/gdma.h (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2021, Microsoft Corporation. */
3 
4 #ifndef _GDMA_H
5 #define _GDMA_H
6 
7 #include <linux/dma-mapping.h>
8 #include <linux/netdevice.h>
9 #include <rdma/ib_verbs.h>
10 
11 #include "shm_channel.h"
12 
13 #define GDMA_STATUS_MORE_ENTRIES	0x00000105
14 #define GDMA_STATUS_CMD_UNSUPPORTED	0xffffffff
15 
16 /* Structures labeled with "HW DATA" are exchanged with the hardware. All of
17  * them are naturally aligned and hence don't need __packed.
18  */
19 
20 enum gdma_request_type {
21 	GDMA_VERIFY_VF_DRIVER_VERSION	= 1,
22 	GDMA_QUERY_MAX_RESOURCES	= 2,
23 	GDMA_LIST_DEVICES		= 3,
24 	GDMA_REGISTER_DEVICE		= 4,
25 	GDMA_DEREGISTER_DEVICE		= 5,
26 	GDMA_GENERATE_TEST_EQE		= 10,
27 	GDMA_CREATE_QUEUE		= 12,
28 	GDMA_DISABLE_QUEUE		= 13,
29 	GDMA_ALLOCATE_RESOURCE_RANGE	= 22,
30 	GDMA_DESTROY_RESOURCE_RANGE	= 24,
31 	GDMA_CREATE_DMA_REGION		= 25,
32 	GDMA_DMA_REGION_ADD_PAGES	= 26,
33 	GDMA_DESTROY_DMA_REGION		= 27,
34 	GDMA_CREATE_PD			= 29,
35 	GDMA_DESTROY_PD			= 30,
36 	GDMA_CREATE_MR			= 31,
37 	GDMA_DESTROY_MR			= 32,
38 	GDMA_QUERY_HWC_TIMEOUT		= 84, /* 0x54 */
39 	GDMA_ALLOC_DM			= 96, /* 0x60 */
40 	GDMA_DESTROY_DM			= 97, /* 0x61 */
41 };
42 
43 #define GDMA_RESOURCE_DOORBELL_PAGE	27
44 
45 enum gdma_queue_type {
46 	GDMA_INVALID_QUEUE,
47 	GDMA_SQ,
48 	GDMA_RQ,
49 	GDMA_CQ,
50 	GDMA_EQ,
51 	GDMA_DIM,
52 };
53 
54 enum gdma_work_request_flags {
55 	GDMA_WR_NONE			= 0,
56 	GDMA_WR_OOB_IN_SGL		= BIT(0),
57 	GDMA_WR_PAD_BY_SGE0		= BIT(1),
58 	GDMA_WR_IB_SGL			= BIT(2),
59 };
60 
61 enum gdma_eqe_type {
62 	GDMA_EQE_COMPLETION		= 3,
63 	GDMA_EQE_THROTTLE		= 4,
64 	GDMA_EQE_TEST_EVENT		= 64,
65 	GDMA_EQE_HWC_INIT_EQ_ID_DB	= 129,
66 	GDMA_EQE_HWC_INIT_DATA		= 130,
67 	GDMA_EQE_HWC_INIT_DONE		= 131,
68 	GDMA_EQE_HWC_FPGA_RECONFIG	= 132,
69 	GDMA_EQE_HWC_SOC_RECONFIG_DATA	= 133,
70 	GDMA_EQE_HWC_SOC_SERVICE	= 134,
71 	GDMA_EQE_HWC_RESET_REQUEST	= 135,
72 	GDMA_EQE_RNIC_QP_FATAL		= 176,
73 };
74 
75 enum {
76 	GDMA_DEVICE_NONE	= 0,
77 	GDMA_DEVICE_HWC		= 1,
78 	GDMA_DEVICE_MANA	= 2,
79 	GDMA_DEVICE_MANA_IB	= 3,
80 };
81 
82 enum gdma_service_type {
83 	GDMA_SERVICE_TYPE_NONE		= 0,
84 	GDMA_SERVICE_TYPE_RDMA_SUSPEND	= 1,
85 	GDMA_SERVICE_TYPE_RDMA_RESUME	= 2,
86 };
87 
88 struct mana_service_work {
89 	struct work_struct work;
90 	struct gdma_dev *gdma_dev;
91 	enum gdma_service_type event;
92 };
93 
94 struct gdma_resource {
95 	/* Protect the bitmap */
96 	spinlock_t lock;
97 
98 	/* The bitmap size in bits. */
99 	u32 size;
100 
101 	/* The bitmap tracks the resources. */
102 	unsigned long *map;
103 };
104 
105 union gdma_doorbell_entry {
106 	u64	as_uint64;
107 
108 	struct {
109 		u64 id		: 24;
110 		u64 reserved	: 8;
111 		u64 tail_ptr	: 31;
112 		u64 arm		: 1;
113 	} cq;
114 
115 	struct {
116 		u64 id		: 24;
117 		u64 wqe_cnt	: 8;
118 		u64 tail_ptr	: 32;
119 	} rq;
120 
121 	struct {
122 		u64 id		: 24;
123 		u64 reserved	: 8;
124 		u64 tail_ptr	: 32;
125 	} sq;
126 
127 	struct {
128 		u64 id		: 16;
129 		u64 reserved	: 16;
130 		u64 tail_ptr	: 31;
131 		u64 arm		: 1;
132 	} eq;
133 
134 	struct {
135 		u64 id           : 24;
136 		u64 reserved     : 8;
137 		u64 mod_usec     : 10;
138 		u64 reserve1     : 5;
139 		u64 mod_usec_vld : 1;
140 		u64 mod_comps    : 8;
141 		u64 reserve2     : 7;
142 		u64 mod_comps_vld: 1;
143 	} dim;
144 }; /* HW DATA */
145 
146 struct gdma_msg_hdr {
147 	u32 hdr_type;
148 	u32 msg_type;
149 	u16 msg_version;
150 	u16 hwc_msg_id;
151 	u32 msg_size;
152 }; /* HW DATA */
153 
154 struct gdma_dev_id {
155 	union {
156 		struct {
157 			u16 type;
158 			u16 instance;
159 		};
160 
161 		u32 as_uint32;
162 	};
163 }; /* HW DATA */
164 
165 struct gdma_req_hdr {
166 	struct gdma_msg_hdr req;
167 	struct gdma_msg_hdr resp; /* The expected response */
168 	struct gdma_dev_id dev_id;
169 	u32 activity_id;
170 }; /* HW DATA */
171 
172 struct gdma_resp_hdr {
173 	struct gdma_msg_hdr response;
174 	struct gdma_dev_id dev_id;
175 	u32 activity_id;
176 	u32 status;
177 	u32 reserved;
178 }; /* HW DATA */
179 
180 struct gdma_general_req {
181 	struct gdma_req_hdr hdr;
182 }; /* HW DATA */
183 
184 #define GDMA_MESSAGE_V1 1
185 #define GDMA_MESSAGE_V2 2
186 #define GDMA_MESSAGE_V3 3
187 #define GDMA_MESSAGE_V4 4
188 #define GDMA_MESSAGE_V5 5
189 
190 struct gdma_general_resp {
191 	struct gdma_resp_hdr hdr;
192 }; /* HW DATA */
193 
194 #define GDMA_STANDARD_HEADER_TYPE 0
195 
196 static inline void mana_gd_init_req_hdr(struct gdma_req_hdr *hdr, u32 code,
197 					u32 req_size, u32 resp_size)
198 {
199 	hdr->req.hdr_type = GDMA_STANDARD_HEADER_TYPE;
200 	hdr->req.msg_type = code;
201 	hdr->req.msg_version = GDMA_MESSAGE_V1;
202 	hdr->req.msg_size = req_size;
203 
204 	hdr->resp.hdr_type = GDMA_STANDARD_HEADER_TYPE;
205 	hdr->resp.msg_type = code;
206 	hdr->resp.msg_version = GDMA_MESSAGE_V1;
207 	hdr->resp.msg_size = resp_size;
208 }
209 
210 /* The 16-byte struct is part of the GDMA work queue entry (WQE). */
211 struct gdma_sge {
212 	u64 address;
213 	u32 mem_key;
214 	u32 size;
215 }; /* HW DATA */
216 
217 struct gdma_wqe_request {
218 	union {
219 		struct gdma_sge *sgl;
220 		struct ib_sge *ib_sgl;
221 	};
222 	u32 num_sge;
223 
224 	u32 inline_oob_size;
225 	const void *inline_oob_data;
226 
227 	u32 flags;
228 	u32 client_data_unit;
229 };
230 
231 enum gdma_page_type {
232 	GDMA_PAGE_TYPE_4K,
233 };
234 
235 #define GDMA_INVALID_DMA_REGION 0
236 
237 struct mana_serv_work {
238 	struct work_struct serv_work;
239 	struct pci_dev *pdev;
240 	enum gdma_eqe_type type;
241 };
242 
243 struct gdma_mem_info {
244 	struct device *dev;
245 
246 	dma_addr_t dma_handle;
247 	void *virt_addr;
248 	u64 length;
249 
250 	/* Scattered fallback: when @nr_pages > 0 the ring is that many
251 	 * PAGE_SIZE coherent allocations in @pages_va/@pages_dma, not
252 	 * @virt_addr/@dma_handle.
253 	 */
254 	void **pages_va;
255 	dma_addr_t *pages_dma;
256 	unsigned int nr_pages;
257 
258 	/* Allocated by the PF driver */
259 	u64 dma_region_handle;
260 };
261 
262 #define REGISTER_ATB_MST_MKEY_LOWER_SIZE 8
263 
264 struct gdma_dev {
265 	struct gdma_context *gdma_context;
266 
267 	struct gdma_dev_id dev_id;
268 
269 	u32 pdid;
270 	u32 doorbell;
271 	u32 gpa_mkey;
272 
273 	/* GDMA driver specific pointer */
274 	void *driver_data;
275 
276 	struct auxiliary_device *adev;
277 	bool is_suspended;
278 	bool rdma_teardown;
279 };
280 
281 /* MANA_PAGE_SIZE is the DMA unit */
282 #define MANA_PAGE_SHIFT 12
283 #define MANA_PAGE_SIZE BIT(MANA_PAGE_SHIFT)
284 #define MANA_PAGE_ALIGN(x) ALIGN((x), MANA_PAGE_SIZE)
285 #define MANA_PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), MANA_PAGE_SIZE)
286 #define MANA_PFN(a) ((a) >> MANA_PAGE_SHIFT)
287 
288 /* Required by HW */
289 #define MANA_MIN_QSIZE MANA_PAGE_SIZE
290 
291 #define GDMA_CQE_SIZE 64
292 #define GDMA_EQE_SIZE 16
293 #define GDMA_MAX_SQE_SIZE 512
294 #define GDMA_MAX_RQE_SIZE 256
295 #define GDMA_EQ_MAX_CHILD_CQ 2
296 
297 #define GDMA_COMP_DATA_SIZE 0x3C
298 
299 #define GDMA_EVENT_DATA_SIZE 0xC
300 
301 /* The WQE size must be a multiple of the Basic Unit, which is 32 bytes. */
302 #define GDMA_WQE_BU_SIZE 32
303 
304 #define INVALID_PDID		UINT_MAX
305 #define INVALID_DOORBELL	UINT_MAX
306 #define INVALID_MEM_KEY		UINT_MAX
307 #define INVALID_QUEUE_ID	UINT_MAX
308 #define INVALID_PCI_MSIX_INDEX  UINT_MAX
309 
310 struct gdma_comp {
311 	u32 cqe_data[GDMA_COMP_DATA_SIZE / 4];
312 	u32 wq_num;
313 	bool is_sq;
314 };
315 
316 struct gdma_event {
317 	u32 details[GDMA_EVENT_DATA_SIZE / 4];
318 	u8  type;
319 };
320 
321 struct gdma_queue;
322 
323 struct gdma_eq_stats {
324 	u64_stats_t throttle_count;
325 	struct u64_stats_sync syncp;
326 };
327 
328 struct mana_eq {
329 	struct gdma_queue	*eq;
330 	struct dentry		*mana_eq_debugfs;
331 };
332 
333 typedef void gdma_eq_callback(void *context, struct gdma_queue *q,
334 			      struct gdma_event *e);
335 
336 typedef void gdma_cq_callback(void *context, struct gdma_queue *q);
337 
338 /* The 'head' is the producer index. For SQ/RQ, when the driver posts a WQE
339  * (Note: the WQE size must be a multiple of the 32-byte Basic Unit), the
340  * driver increases the 'head' in BUs rather than in bytes, and notifies
341  * the HW of the updated head. For EQ/CQ, the driver uses the 'head' to track
342  * the HW head, and increases the 'head' by 1 for every processed EQE/CQE.
343  *
344  * The 'tail' is the consumer index for SQ/RQ. After the CQE of the SQ/RQ is
345  * processed, the driver increases the 'tail' to indicate that WQEs have
346  * been consumed by the HW, so the driver can post new WQEs into the SQ/RQ.
347  *
348  * The driver doesn't use the 'tail' for EQ/CQ, because the driver ensures
349  * that the EQ/CQ is big enough so they can't overflow, and the driver uses
350  * the owner bits mechanism to detect if the queue has become empty.
351  */
352 struct gdma_queue {
353 	struct gdma_dev *gdma_dev;
354 
355 	enum gdma_queue_type type;
356 	u32 id;
357 
358 	struct gdma_mem_info mem_info;
359 
360 	void *queue_mem_ptr;
361 	u32 queue_size;
362 
363 	bool monitor_avl_buf;
364 
365 	u32 head;
366 	u32 tail;
367 	struct list_head entry;
368 
369 	/* Extra fields specific to EQ/CQ. */
370 	union {
371 		struct {
372 			bool disable_needed;
373 
374 			gdma_eq_callback *callback;
375 			void *context;
376 
377 			unsigned int msix_index;
378 			unsigned int irq;
379 
380 			u32 log2_throttle_limit;
381 
382 			struct gdma_eq_stats stats;
383 
384 			struct gdma_queue __rcu *child_cq[GDMA_EQ_MAX_CHILD_CQ];
385 		} eq;
386 
387 		struct {
388 			gdma_cq_callback *callback;
389 			void *context;
390 
391 			struct gdma_queue *parent; /* For CQ/EQ relationship */
392 		} cq;
393 	};
394 };
395 
396 struct gdma_queue_spec {
397 	enum gdma_queue_type type;
398 	bool monitor_avl_buf;
399 	unsigned int queue_size;
400 
401 	/* Extra fields specific to EQ/CQ. */
402 	union {
403 		struct {
404 			gdma_eq_callback *callback;
405 			void *context;
406 
407 			unsigned long log2_throttle_limit;
408 			unsigned int msix_index;
409 		} eq;
410 
411 		struct {
412 			gdma_cq_callback *callback;
413 			void *context;
414 
415 			struct gdma_queue *parent_eq;
416 
417 		} cq;
418 	};
419 };
420 
421 #define MANA_IRQ_NAME_SZ 32
422 
423 struct gdma_irq_context {
424 	void (*handler)(void *arg);
425 	/* Protect the eq_list */
426 	spinlock_t lock;
427 	struct list_head eq_list;
428 	char name[MANA_IRQ_NAME_SZ];
429 	unsigned int msi;
430 	unsigned int irq;
431 	refcount_t refcount;
432 	unsigned int bitmap_refs;
433 	bool dyn_msix;
434 };
435 
436 enum gdma_context_flags {
437 	GC_PROBE_SUCCEEDED	= 0,
438 	GC_IN_SERVICE		= 1,
439 };
440 
441 struct gdma_context;
442 
443 /**
444  * struct gdma_bus_ops - transport-specific operations
445  *
446  * Lets the bus-agnostic GDMA core drive PCI and CDX devices without
447  * testing configuration symbols. Each bus driver installs its own
448  * instance in gdma_context.bus_ops at probe time.
449  *
450  * @bus_name: Bus token used to build the per-vector IRQ names.
451  * @adev_prefix: Prefix used for auxiliary-device match names.
452  * @msix_can_alloc_dyn: True if the bus can allocate MSI-X vectors after
453  *	probe time. Buses that size their vector pool at probe and cannot
454  *	grow it later leave this NULL.
455  * @msix_virq: Return the Linux IRQ number backing an MSI vector, or
456  *	-EINVAL if the vector has not been allocated yet.
457  * @msix_alloc_at: Allocate an MSI vector on demand and return its IRQ
458  *	number, updating @msi with the index actually assigned. Buses that
459  *	allocate their whole pool up front leave this NULL.
460  * @msix_free: Release a vector obtained from @msix_alloc_at. Only needed
461  *	by buses that implement @msix_alloc_at.
462  * @msix_vec_count: Return the number of entries in the device MSI-X table,
463  *	used to clamp the vector count the core asks for. Buses that cannot
464  *	report a table size leave this NULL and the clamp is skipped.
465  * @setup_hwc_irqs: Set up the interrupts the hardware channel needs before
466  *	it is created.
467  * @setup_remaining_irqs: Set up the remaining interrupts once the device
468  *	resource limits are known.
469  * @remove_irqs: Release the interrupts set up by @setup_hwc_irqs and
470  *	@setup_remaining_irqs.
471  * @dev_reset: Reset the device using the bus-level reset mechanism.
472  * @schedule_serv_work: Queue device servicing or recovery. Buses without
473  *	a servicing path leave this NULL.
474  * @drv_cap_flags1: Extra capability bits this transport can honour, OR'ed
475  *	into GDMA_DRV_CAP_FLAGS1 when the driver version is negotiated.
476  */
477 struct gdma_bus_ops {
478 	const char *bus_name;
479 	const char *adev_prefix;
480 	bool (*msix_can_alloc_dyn)(struct gdma_context *gc);
481 	int (*msix_virq)(struct gdma_context *gc, int msi);
482 	int (*msix_alloc_at)(struct gdma_context *gc, int *msi);
483 	void (*msix_free)(struct gdma_context *gc, int msi, int irq);
484 	int (*msix_vec_count)(struct gdma_context *gc);
485 	int (*setup_hwc_irqs)(struct gdma_context *gc);
486 	int (*setup_remaining_irqs)(struct gdma_context *gc);
487 	void (*remove_irqs)(struct gdma_context *gc);
488 	int (*dev_reset)(struct gdma_context *gc);
489 	int (*schedule_serv_work)(struct gdma_context *gc,
490 				  enum gdma_eqe_type type);
491 	u64 drv_cap_flags1;
492 };
493 
494 struct gdma_context {
495 	struct device		*dev;
496 	struct dentry		*mana_pci_debugfs;
497 
498 	/* Transport-specific operations, installed by the bus driver */
499 	const struct gdma_bus_ops *bus_ops;
500 
501 	/* Hardware max number of queues */
502 	unsigned int		max_num_queues;
503 	/* Per-vPort max number of queues */
504 	unsigned int		max_num_queues_vport;
505 	/* Number of vPorts reported by the device, capped at
506 	 * MAX_PORTS_IN_MANA_DEV
507 	 */
508 	u16			num_ports;
509 	unsigned int		max_num_msix;
510 	unsigned int		num_msix_usable;
511 	struct xarray		irq_contexts;
512 
513 	/* L2 MTU */
514 	u16 adapter_mtu;
515 
516 	/* NIC supports CQE x8 coalescing */
517 	bool cqe8_coalescing_sup;
518 
519 	/* This maps a CQ index to the queue structure. */
520 	unsigned int		max_num_cqs;
521 	struct gdma_queue	**cq_table;
522 
523 	/* Protect eq_test_event and test_event_eq_id  */
524 	struct mutex		eq_test_event_mutex;
525 	struct completion	eq_test_event;
526 	u32			test_event_eq_id;
527 
528 	bool			is_pf;
529 	bool			is_pf2;
530 
531 	phys_addr_t		bar0_pa;
532 	void __iomem		*bar0_va;
533 	resource_size_t		bar0_size;
534 	void __iomem		*shm_base;
535 	void __iomem		*db_page_base;
536 	phys_addr_t		phys_db_page_base;
537 	u64 db_page_off;
538 	u64 db_page_size;
539 	int                     numa_node;
540 
541 	/* Shared memory chanenl (used to bootstrap HWC) */
542 	struct shm_channel	shm_channel;
543 
544 	/* Hardware communication channel (HWC) */
545 	struct gdma_dev		hwc;
546 
547 	/* Azure network adapter */
548 	struct gdma_dev		mana;
549 
550 	/* Azure RDMA adapter */
551 	struct gdma_dev		mana_ib;
552 
553 	u64 pf_cap_flags1;
554 	u64 gdma_protocol_ver;
555 
556 	struct workqueue_struct *service_wq;
557 
558 	unsigned long		flags;
559 
560 	/* Protect access to GIC context */
561 	struct mutex		gic_mutex;
562 
563 	/* Indicate if this device is sharing MSI for EQs on MANA */
564 	bool msi_sharing;
565 
566 	/* Bitmap tracks where MSI is allocated when it is not shared for EQs */
567 	unsigned long *msi_bitmap;
568 };
569 
570 static inline bool mana_gd_is_mana(struct gdma_dev *gd)
571 {
572 	return gd->dev_id.type == GDMA_DEVICE_MANA;
573 }
574 
575 static inline bool mana_gd_is_hwc(struct gdma_dev *gd)
576 {
577 	return gd->dev_id.type == GDMA_DEVICE_HWC;
578 }
579 
580 u8 *mana_gd_get_wqe_ptr(const struct gdma_queue *wq, u32 wqe_offset);
581 u32 mana_gd_wq_avail_space(struct gdma_queue *wq);
582 
583 int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq);
584 
585 int mana_gd_create_hwc_queue(struct gdma_dev *gd,
586 			     const struct gdma_queue_spec *spec,
587 			     struct gdma_queue **queue_ptr);
588 
589 int mana_gd_create_mana_eq(struct gdma_dev *gd,
590 			   const struct gdma_queue_spec *spec,
591 			   struct gdma_queue **queue_ptr);
592 
593 int mana_gd_create_mana_wq_cq(struct gdma_dev *gd,
594 			      const struct gdma_queue_spec *spec,
595 			      struct gdma_queue **queue_ptr);
596 
597 void mana_gd_destroy_queue(struct gdma_context *gc, struct gdma_queue *queue);
598 
599 int mana_gd_poll_cq(struct gdma_queue *cq, struct gdma_comp *comp, int num_cqe);
600 
601 void mana_gd_ring_cq(struct gdma_queue *cq, u8 arm_bit);
602 
603 ssize_t mana_gd_read_ring(struct gdma_queue *q, char __user *buf,
604 			  size_t count, loff_t *pos);
605 
606 int mana_schedule_serv_work(struct gdma_context *gc, enum gdma_eqe_type type);
607 
608 int mana_gd_alloc_service_wq(struct gdma_context *gc);
609 
610 void mana_gd_free_service_wq(struct gdma_context *gc);
611 
612 void mana_gd_ring_dim(struct gdma_queue *cq, u32 mod_usec, bool mod_usec_vld,
613 		      u32 mod_comps, bool mod_comps_vld);
614 
615 bool mana_gd_detach_cq(struct gdma_context *gc, struct gdma_queue *queue);
616 
617 struct gdma_wqe {
618 	u32 reserved	:24;
619 	u32 last_vbytes	:8;
620 
621 	union {
622 		u32 flags;
623 
624 		struct {
625 			u32 num_sge		:8;
626 			u32 inline_oob_size_div4:3;
627 			u32 client_oob_in_sgl	:1;
628 			u32 reserved1		:4;
629 			u32 client_data_unit	:14;
630 			u32 reserved2		:2;
631 		};
632 	};
633 }; /* HW DATA */
634 
635 #define INLINE_OOB_SMALL_SIZE 8
636 #define INLINE_OOB_LARGE_SIZE 24
637 #define INLINE_OOB_EXTRA_LARGE_SIZE 32
638 
639 #define MANA_MAX_TX_WQE_SGL_ENTRIES 30
640 
641 #define MAX_TX_WQE_SIZE 512
642 #define MAX_RX_WQE_SIZE 256
643 
644 #define MAX_TX_WQE_SGL_ENTRIES	((GDMA_MAX_SQE_SIZE -			   \
645 			sizeof(struct gdma_sge) - INLINE_OOB_SMALL_SIZE) / \
646 			sizeof(struct gdma_sge))
647 
648 #define MAX_RX_WQE_SGL_ENTRIES	((GDMA_MAX_RQE_SIZE -			   \
649 			sizeof(struct gdma_sge)) / sizeof(struct gdma_sge))
650 
651 struct gdma_cqe {
652 	u32 cqe_data[GDMA_COMP_DATA_SIZE / 4];
653 
654 	union {
655 		u32 as_uint32;
656 
657 		struct {
658 			u32 wq_num	: 24;
659 			u32 is_sq	: 1;
660 			u32 reserved	: 4;
661 			u32 owner_bits	: 3;
662 		};
663 	} cqe_info;
664 }; /* HW DATA */
665 
666 #define GDMA_CQE_OWNER_BITS 3
667 
668 #define GDMA_CQE_OWNER_MASK ((1 << GDMA_CQE_OWNER_BITS) - 1)
669 
670 #define SET_ARM_BIT 1
671 
672 #define GDMA_EQE_OWNER_BITS 3
673 
674 union gdma_eqe_info {
675 	u32 as_uint32;
676 
677 	struct {
678 		u32 type	: 8;
679 		u32 reserved1	: 8;
680 		u32 client_id	: 2;
681 		u32 reserved2	: 11;
682 		u32 owner_bits	: 3;
683 	};
684 }; /* HW DATA */
685 
686 #define GDMA_EQE_OWNER_MASK ((1 << GDMA_EQE_OWNER_BITS) - 1)
687 #define INITIALIZED_OWNER_BIT(log2_num_entries) (1UL << (log2_num_entries))
688 
689 struct gdma_eqe {
690 	u32 details[GDMA_EVENT_DATA_SIZE / 4];
691 	u32 eqe_info;
692 }; /* HW DATA */
693 
694 #define GDMA_REG_DB_PAGE_OFFSET	8
695 #define GDMA_REG_DB_PAGE_SIZE	0x10
696 #define GDMA_REG_SHM_OFFSET	0x18
697 
698 #define GDMA_PF_REG_DB_PAGE_SIZE	0xD0
699 #define GDMA_PF_REG_DB_PAGE_OFF		0xC8
700 #define GDMA_PF_REG_SHM_OFF		0x70
701 
702 #define GDMA_SRIOV_REG_CFG_BASE_OFF	0x108
703 
704 #define MANA_PF_DEVICE_ID 0x00B9
705 #define MANA_PF2_DEVICE_ID 0x00C1
706 #define MANA_VF_DEVICE_ID 0x00BA
707 
708 #define MANA_CDX_DEVICE_ID 0x00C2
709 
710 struct gdma_posted_wqe_info {
711 	u32 wqe_size_in_bu;
712 	/* Unmasked WQE start offset in GDMA basic units. */
713 	u32 wqe_offset;
714 };
715 
716 /* GDMA_GENERATE_TEST_EQE */
717 struct gdma_generate_test_event_req {
718 	struct gdma_req_hdr hdr;
719 	u32 queue_index;
720 }; /* HW DATA */
721 
722 /* GDMA_VERIFY_VF_DRIVER_VERSION */
723 enum {
724 	GDMA_PROTOCOL_V1	= 1,
725 	GDMA_PROTOCOL_FIRST	= GDMA_PROTOCOL_V1,
726 	GDMA_PROTOCOL_LAST	= GDMA_PROTOCOL_V1,
727 };
728 
729 #define GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT BIT(0)
730 
731 /* Advertise to the NIC firmware: the NAPI work_done variable race is fixed,
732  * so the driver is able to reliably support features like busy_poll.
733  */
734 #define GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX BIT(2)
735 #define GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG BIT(3)
736 #define GDMA_DRV_CAP_FLAG_1_GDMA_PAGES_4MB_1GB_2GB BIT(4)
737 #define GDMA_DRV_CAP_FLAG_1_VARIABLE_INDIRECTION_TABLE_SUPPORT BIT(5)
738 #define GDMA_DRV_CAP_FLAG_1_HW_VPORT_LINK_AWARE BIT(6)
739 
740 /* Driver can handle holes (zeros) in the device list */
741 #define GDMA_DRV_CAP_FLAG_1_DEV_LIST_HOLES_SUP BIT(11)
742 
743 /* Driver supports dynamic MSI-X vector allocation */
744 #define GDMA_DRV_CAP_FLAG_1_DYNAMIC_IRQ_ALLOC_SUPPORT BIT(13)
745 
746 /* Driver can self reset on EQE notification */
747 #define GDMA_DRV_CAP_FLAG_1_SELF_RESET_ON_EQE BIT(14)
748 
749 /* Driver can self reset on FPGA Reconfig EQE notification */
750 #define GDMA_DRV_CAP_FLAG_1_HANDLE_RECONFIG_EQE BIT(17)
751 
752 /* Driver detects stalled send queues and recovers them */
753 #define GDMA_DRV_CAP_FLAG_1_HANDLE_STALL_SQ_RECOVERY BIT(18)
754 
755 /* Driver supports separate EQ/MSIs for each vPort */
756 #define GDMA_DRV_CAP_FLAG_1_EQ_MSI_UNSHARE_MULTI_VPORT BIT(19)
757 
758 /* Driver supports linearizing the skb when num_sge exceeds hardware limit */
759 #define GDMA_DRV_CAP_FLAG_1_SKB_LINEARIZE BIT(20)
760 
761 /* Driver can send HWC periodically to query stats */
762 #define GDMA_DRV_CAP_FLAG_1_PERIODIC_STATS_QUERY BIT(21)
763 
764 /* Driver can handle hardware recovery events during probe */
765 #define GDMA_DRV_CAP_FLAG_1_PROBE_RECOVERY BIT(22)
766 
767 /* Driver supports self recovery on Hardware Channel timeouts */
768 #define GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECOVERY BIT(25)
769 
770 /* Driver supports dynamic interrupt moderation - DIM */
771 #define GDMA_DRV_CAP_FLAG_1_DYN_INTERRUPT_MODERATION BIT(28)
772 
773 /* Driver supports non-contiguous queue buffers */
774 #define GDMA_DRV_CAP_FLAG_1_NON_CONTIGUOUS_BUFFERS BIT(30)
775 
776 /* Driver supports handling throttled EQEs */
777 #define GDMA_DRV_CAP_FLAG_1_THROTTLED_EVENT_QUEUE BIT_ULL(32)
778 
779 /* Capabilities in the PCI-only group below rely on dynamic MSI-X allocation
780  * and on the servicing and reset paths reached through
781  * mana_schedule_serv_work(). Transports that provide neither leave
782  * gdma_bus_ops.drv_cap_flags1 unset so the group is not advertised.
783  */
784 #define GDMA_DRV_CAP_FLAGS1_PCI \
785 	(GDMA_DRV_CAP_FLAG_1_DYNAMIC_IRQ_ALLOC_SUPPORT | \
786 	 GDMA_DRV_CAP_FLAG_1_SELF_RESET_ON_EQE | \
787 	 GDMA_DRV_CAP_FLAG_1_HANDLE_RECONFIG_EQE | \
788 	 GDMA_DRV_CAP_FLAG_1_PROBE_RECOVERY | \
789 	 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECOVERY)
790 
791 #define GDMA_DRV_CAP_FLAGS1 \
792 	(GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT | \
793 	 GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX | \
794 	 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG | \
795 	 GDMA_DRV_CAP_FLAG_1_VARIABLE_INDIRECTION_TABLE_SUPPORT | \
796 	 GDMA_DRV_CAP_FLAG_1_DEV_LIST_HOLES_SUP | \
797 	 GDMA_DRV_CAP_FLAG_1_HW_VPORT_LINK_AWARE | \
798 	 GDMA_DRV_CAP_FLAG_1_PERIODIC_STATS_QUERY | \
799 	 GDMA_DRV_CAP_FLAG_1_SKB_LINEARIZE | \
800 	 GDMA_DRV_CAP_FLAG_1_HANDLE_STALL_SQ_RECOVERY | \
801 	 GDMA_DRV_CAP_FLAG_1_EQ_MSI_UNSHARE_MULTI_VPORT | \
802 	 GDMA_DRV_CAP_FLAG_1_DYN_INTERRUPT_MODERATION | \
803 	 GDMA_DRV_CAP_FLAG_1_NON_CONTIGUOUS_BUFFERS | \
804 	 GDMA_DRV_CAP_FLAG_1_THROTTLED_EVENT_QUEUE)
805 
806 #define GDMA_DRV_CAP_FLAGS2 0
807 
808 #define GDMA_DRV_CAP_FLAGS3 0
809 
810 #define GDMA_DRV_CAP_FLAGS4 0
811 
812 struct gdma_verify_ver_req {
813 	struct gdma_req_hdr hdr;
814 
815 	/* Mandatory fields required for protocol establishment */
816 	u64 protocol_ver_min;
817 	u64 protocol_ver_max;
818 
819 	/* Gdma Driver Capability Flags */
820 	u64 gd_drv_cap_flags1;
821 	u64 gd_drv_cap_flags2;
822 	u64 gd_drv_cap_flags3;
823 	u64 gd_drv_cap_flags4;
824 
825 	/* Advisory fields */
826 	u64 drv_ver;
827 	u32 os_type; /* Linux = 0x10; Windows = 0x20; Other = 0x30 */
828 	u32 reserved;
829 	u32 os_ver_major;
830 	u32 os_ver_minor;
831 	u32 os_ver_build;
832 	u32 os_ver_platform;
833 	u64 reserved_2;
834 	u8 os_ver_str1[128];
835 	u8 os_ver_str2[128];
836 	u8 os_ver_str3[128];
837 	u8 os_ver_str4[128];
838 }; /* HW DATA */
839 
840 /* HW supports dynamic interrupt moderation - DIM */
841 #define GDMA_PF_CAP_FLAG_1_DYN_INTERRUPT_MODERATION BIT(15)
842 
843 struct gdma_verify_ver_resp {
844 	struct gdma_resp_hdr hdr;
845 	u64 gdma_protocol_ver;
846 	u64 pf_cap_flags1;
847 	u64 pf_cap_flags2;
848 	u64 pf_cap_flags3;
849 	u64 pf_cap_flags4;
850 }; /* HW DATA */
851 
852 /* GDMA_QUERY_MAX_RESOURCES */
853 struct gdma_query_max_resources_resp {
854 	struct gdma_resp_hdr hdr;
855 	u32 status;
856 	u32 max_sq;
857 	u32 max_rq;
858 	u32 max_cq;
859 	u32 max_eq;
860 	u32 max_db;
861 	u32 max_mst;
862 	u32 max_cq_mod_ctx;
863 	u32 max_mod_cq;
864 	u32 max_msix;
865 }; /* HW DATA */
866 
867 /* GDMA_LIST_DEVICES */
868 #define GDMA_DEV_LIST_SIZE 64
869 struct gdma_list_devices_resp {
870 	struct gdma_resp_hdr hdr;
871 	u32 num_of_devs;
872 	u32 reserved;
873 	struct gdma_dev_id devs[GDMA_DEV_LIST_SIZE];
874 }; /* HW DATA */
875 
876 /* GDMA_REGISTER_DEVICE */
877 struct gdma_register_device_resp {
878 	struct gdma_resp_hdr hdr;
879 	u32 pdid;
880 	u32 gpa_mkey;
881 	u32 db_id;
882 }; /* HW DATA */
883 
884 struct gdma_allocate_resource_range_req {
885 	struct gdma_req_hdr hdr;
886 	u32 resource_type;
887 	u32 num_resources;
888 	u32 alignment;
889 	u32 allocated_resources;
890 };
891 
892 struct gdma_allocate_resource_range_resp {
893 	struct gdma_resp_hdr hdr;
894 	u32 allocated_resources;
895 };
896 
897 struct gdma_destroy_resource_range_req {
898 	struct gdma_req_hdr hdr;
899 	u32 resource_type;
900 	u32 num_resources;
901 	u32 allocated_resources;
902 };
903 
904 /* GDMA_CREATE_QUEUE */
905 struct gdma_create_queue_req {
906 	struct gdma_req_hdr hdr;
907 	u32 type;
908 	u32 reserved1;
909 	u32 pdid;
910 	u32 doolbell_id;
911 	u64 gdma_region;
912 	u32 reserved2;
913 	u32 queue_size;
914 	u32 log2_throttle_limit;
915 	u32 eq_pci_msix_index;
916 	u32 cq_mod_ctx_id;
917 	u32 cq_parent_eq_id;
918 	u8  rq_drop_on_overrun;
919 	u8  rq_err_on_wqe_overflow;
920 	u8  rq_chain_rec_wqes;
921 	u8  sq_hw_db;
922 	u32 reserved3;
923 }; /* HW DATA */
924 
925 struct gdma_create_queue_resp {
926 	struct gdma_resp_hdr hdr;
927 	u32 queue_index;
928 }; /* HW DATA */
929 
930 /* GDMA_DISABLE_QUEUE */
931 struct gdma_disable_queue_req {
932 	struct gdma_req_hdr hdr;
933 	u32 type;
934 	u32 queue_index;
935 	u32 alloc_res_id_on_creation;
936 }; /* HW DATA */
937 
938 /* GDMA_QUERY_HWC_TIMEOUT */
939 struct gdma_query_hwc_timeout_req {
940 	struct gdma_req_hdr hdr;
941 	u32 timeout_ms;
942 	u32 reserved;
943 };
944 
945 struct gdma_query_hwc_timeout_resp {
946 	struct gdma_resp_hdr hdr;
947 	u32 timeout_ms;
948 	u32 reserved;
949 };
950 
951 enum gdma_mr_access_flags {
952 	GDMA_ACCESS_FLAG_LOCAL_READ = BIT_ULL(0),
953 	GDMA_ACCESS_FLAG_LOCAL_WRITE = BIT_ULL(1),
954 	GDMA_ACCESS_FLAG_REMOTE_READ = BIT_ULL(2),
955 	GDMA_ACCESS_FLAG_REMOTE_WRITE = BIT_ULL(3),
956 	GDMA_ACCESS_FLAG_REMOTE_ATOMIC = BIT_ULL(4),
957 	GDMA_ACCESS_FLAG_BIND_MW = BIT_ULL(5),
958 };
959 
960 /* GDMA_CREATE_DMA_REGION */
961 struct gdma_create_dma_region_req {
962 	struct gdma_req_hdr hdr;
963 
964 	/* The total size of the DMA region */
965 	u64 length;
966 
967 	/* The offset in the first page */
968 	u32 offset_in_page;
969 
970 	/* enum gdma_page_type */
971 	u32 gdma_page_type;
972 
973 	/* The total number of pages */
974 	u32 page_count;
975 
976 	/* If page_addr_list_len is smaller than page_count,
977 	 * the remaining page addresses will be added via the
978 	 * message GDMA_DMA_REGION_ADD_PAGES.
979 	 */
980 	u32 page_addr_list_len;
981 	u64 page_addr_list[];
982 }; /* HW DATA */
983 
984 struct gdma_create_dma_region_resp {
985 	struct gdma_resp_hdr hdr;
986 	u64 dma_region_handle;
987 }; /* HW DATA */
988 
989 /* GDMA_DMA_REGION_ADD_PAGES */
990 struct gdma_dma_region_add_pages_req {
991 	struct gdma_req_hdr hdr;
992 
993 	u64 dma_region_handle;
994 
995 	u32 page_addr_list_len;
996 	u32 reserved3;
997 
998 	u64 page_addr_list[];
999 }; /* HW DATA */
1000 
1001 /* GDMA_DESTROY_DMA_REGION */
1002 struct gdma_destroy_dma_region_req {
1003 	struct gdma_req_hdr hdr;
1004 
1005 	u64 dma_region_handle;
1006 }; /* HW DATA */
1007 
1008 enum gdma_pd_flags {
1009 	GDMA_PD_FLAG_ALLOW_GPA_MR = BIT(0),
1010 	GDMA_PD_FLAG_SHORT_PDN = BIT(2),
1011 };
1012 
1013 struct gdma_create_pd_req {
1014 	struct gdma_req_hdr hdr;
1015 	enum gdma_pd_flags flags;
1016 	u32 reserved;
1017 };/* HW DATA */
1018 
1019 struct gdma_create_pd_resp {
1020 	struct gdma_resp_hdr hdr;
1021 	u64 pd_handle;
1022 	u32 pd_id;
1023 	u32 reserved;
1024 };/* HW DATA */
1025 
1026 struct gdma_destroy_pd_req {
1027 	struct gdma_req_hdr hdr;
1028 	u64 pd_handle;
1029 };/* HW DATA */
1030 
1031 struct gdma_destroy_pd_resp {
1032 	struct gdma_resp_hdr hdr;
1033 };/* HW DATA */
1034 
1035 enum gdma_mr_type {
1036 	/*
1037 	 * Guest Physical Address - MRs of this type allow access
1038 	 * to any DMA-mapped memory using bus-logical address
1039 	 */
1040 	GDMA_MR_TYPE_GPA = 1,
1041 	/* Guest Virtual Address - MRs of this type allow access
1042 	 * to memory mapped by PTEs associated with this MR using a virtual
1043 	 * address that is set up in the MST
1044 	 */
1045 	GDMA_MR_TYPE_GVA = 2,
1046 	/* Guest zero-based address MRs */
1047 	GDMA_MR_TYPE_ZBVA = 4,
1048 	/* Device address MRs */
1049 	GDMA_MR_TYPE_DM = 5,
1050 	/* Memory Window type 1 */
1051 	GDMA_MR_TYPE_MW1 = 6,
1052 	/* Memory Window type 2 */
1053 	GDMA_MR_TYPE_MW2 = 7,
1054 };
1055 
1056 struct gdma_create_mr_params {
1057 	u64 pd_handle;
1058 	enum gdma_mr_type mr_type;
1059 	union {
1060 		struct {
1061 			u64 dma_region_handle;
1062 			u64 virtual_address;
1063 			enum gdma_mr_access_flags access_flags;
1064 		} gva;
1065 		struct {
1066 			u64 dma_region_handle;
1067 			enum gdma_mr_access_flags access_flags;
1068 		} zbva;
1069 		struct {
1070 			u64 dm_handle;
1071 			u64 offset;
1072 			u64 length;
1073 			enum gdma_mr_access_flags access_flags;
1074 		} da;
1075 	};
1076 };
1077 
1078 struct gdma_create_mr_request {
1079 	struct gdma_req_hdr hdr;
1080 	u64 pd_handle;
1081 	enum gdma_mr_type mr_type;
1082 	u32 reserved_1;
1083 
1084 	union {
1085 		struct {
1086 			u64 dma_region_handle;
1087 			u64 virtual_address;
1088 			enum gdma_mr_access_flags access_flags;
1089 		} __packed gva;
1090 		struct {
1091 			u64 dma_region_handle;
1092 			enum gdma_mr_access_flags access_flags;
1093 		} __packed zbva;
1094 		struct {
1095 			u64 dm_handle;
1096 			u64 offset;
1097 			enum gdma_mr_access_flags access_flags;
1098 		} __packed da;
1099 	} __packed;
1100 	u32 reserved_2;
1101 	union {
1102 		struct {
1103 			u64 length;
1104 		} da_ext;
1105 	};
1106 };/* HW DATA */
1107 
1108 struct gdma_create_mr_response {
1109 	struct gdma_resp_hdr hdr;
1110 	u64 mr_handle;
1111 	u32 lkey;
1112 	u32 rkey;
1113 };/* HW DATA */
1114 
1115 struct gdma_destroy_mr_request {
1116 	struct gdma_req_hdr hdr;
1117 	u64 mr_handle;
1118 };/* HW DATA */
1119 
1120 struct gdma_destroy_mr_response {
1121 	struct gdma_resp_hdr hdr;
1122 };/* HW DATA */
1123 
1124 struct gdma_alloc_dm_req {
1125 	struct gdma_req_hdr hdr;
1126 	u64 length;
1127 	u32 alignment;
1128 	u32 flags;
1129 }; /* HW Data */
1130 
1131 struct gdma_alloc_dm_resp {
1132 	struct gdma_resp_hdr hdr;
1133 	u64 dm_handle;
1134 }; /* HW Data */
1135 
1136 struct gdma_destroy_dm_req {
1137 	struct gdma_req_hdr hdr;
1138 	u64 dm_handle;
1139 }; /* HW Data */
1140 
1141 struct gdma_destroy_dm_resp {
1142 	struct gdma_resp_hdr hdr;
1143 }; /* HW Data */
1144 
1145 int mana_gd_verify_vf_version(struct gdma_context *gc);
1146 int mana_gd_query_max_resources(struct gdma_context *gc);
1147 int mana_gd_setup(struct gdma_context *gc);
1148 void mana_gd_cleanup(struct gdma_context *gc);
1149 int mana_gd_detect_devices(struct gdma_context *gc);
1150 int mana_gd_init_registers(struct gdma_context *gc);
1151 irqreturn_t mana_gd_intr(int irq, void *arg);
1152 void mana_gd_process_eq_events(void *arg);
1153 
1154 static inline u32 mana_gd_r32(struct gdma_context *g, u64 offset)
1155 {
1156 	return readl(g->bar0_va + offset);
1157 }
1158 
1159 static inline u64 mana_gd_r64(struct gdma_context *g, u64 offset)
1160 {
1161 	return readq(g->bar0_va + offset);
1162 }
1163 
1164 int mana_gd_register_device(struct gdma_dev *gd);
1165 int mana_gd_deregister_device(struct gdma_dev *gd);
1166 
1167 int mana_gd_post_work_request(struct gdma_queue *wq,
1168 			      const struct gdma_wqe_request *wqe_req,
1169 			      struct gdma_posted_wqe_info *wqe_info);
1170 
1171 int mana_gd_post_and_ring(struct gdma_queue *queue,
1172 			  const struct gdma_wqe_request *wqe,
1173 			  struct gdma_posted_wqe_info *wqe_info);
1174 
1175 int mana_gd_alloc_res_map(u32 res_avail, struct gdma_resource *r);
1176 void mana_gd_free_res_map(struct gdma_resource *r);
1177 
1178 void mana_gd_wq_ring_doorbell(struct gdma_context *gc,
1179 			      struct gdma_queue *queue);
1180 void mana_gd_wq_ring_doorbell_ext(struct gdma_context *gc, struct gdma_queue *queue,
1181 				  u32 tail_ptr, u8 wqe_cnt, u8 client_offset);
1182 
1183 int mana_gd_alloc_memory(struct gdma_context *gc, unsigned int length,
1184 			 struct gdma_mem_info *gmi, bool allow_scatter);
1185 
1186 void mana_gd_free_memory(struct gdma_mem_info *gmi);
1187 
1188 int mana_gd_send_request(struct gdma_context *gc, u32 req_len, const void *req,
1189 			 u32 resp_len, void *resp);
1190 
1191 int mana_gd_destroy_dma_region(struct gdma_context *gc, u64 dma_region_handle);
1192 void mana_register_debugfs(void);
1193 void mana_unregister_debugfs(void);
1194 
1195 int mana_rdma_service_event(struct gdma_context *gc, enum gdma_service_type event);
1196 
1197 
1198 bool mana_need_log(struct gdma_context *gc, int err);
1199 
1200 struct gdma_irq_context *mana_gd_get_gic(struct gdma_context *gc,
1201 					 bool use_msi_bitmap,
1202 					 int *msi_requested);
1203 void mana_gd_put_gic(struct gdma_context *gc, bool use_msi_bitmap, int msi);
1204 int mana_gd_query_device_cfg(struct gdma_context *gc, u32 proto_major_ver,
1205 			     u32 proto_minor_ver, u32 proto_micro_ver,
1206 			     u16 *max_num_vports, u8 *bm_hostmode);
1207 int mana_gd_dev_reset(struct gdma_context *gc);
1208 
1209 #endif /* _GDMA_H */
1210