1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2021, Microsoft Corporation. */
7 #include <linux/dma-mapping.h>
8 #include <linux/netdevice.h>
10 #include "shm_channel.h"
12 #define GDMA_STATUS_MORE_ENTRIES 0x00000105
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 enum gdma_request_type {
19 GDMA_VERIFY_VF_DRIVER_VERSION = 1,
20 GDMA_QUERY_MAX_RESOURCES = 2,
21 GDMA_LIST_DEVICES = 3,
22 GDMA_REGISTER_DEVICE = 4,
23 GDMA_DEREGISTER_DEVICE = 5,
24 GDMA_GENERATE_TEST_EQE = 10,
25 GDMA_CREATE_QUEUE = 12,
26 GDMA_DISABLE_QUEUE = 13,
27 GDMA_ALLOCATE_RESOURCE_RANGE = 22,
28 GDMA_DESTROY_RESOURCE_RANGE = 24,
29 GDMA_CREATE_DMA_REGION = 25,
30 GDMA_DMA_REGION_ADD_PAGES = 26,
31 GDMA_DESTROY_DMA_REGION = 27,
36 GDMA_QUERY_HWC_TIMEOUT = 84, /* 0x54 */
39 #define GDMA_RESOURCE_DOORBELL_PAGE 27
41 enum gdma_queue_type {
49 enum gdma_work_request_flags {
51 GDMA_WR_OOB_IN_SGL = BIT(0),
52 GDMA_WR_PAD_BY_SGE0 = BIT(1),
56 GDMA_EQE_COMPLETION = 3,
57 GDMA_EQE_TEST_EVENT = 64,
58 GDMA_EQE_HWC_INIT_EQ_ID_DB = 129,
59 GDMA_EQE_HWC_INIT_DATA = 130,
60 GDMA_EQE_HWC_INIT_DONE = 131,
61 GDMA_EQE_HWC_SOC_RECONFIG = 132,
62 GDMA_EQE_HWC_SOC_RECONFIG_DATA = 133,
71 struct gdma_resource {
72 /* Protect the bitmap */
75 /* The bitmap size in bits. */
78 /* The bitmap tracks the resources. */
82 union gdma_doorbell_entry {
112 struct gdma_msg_hdr {
131 struct gdma_req_hdr {
132 struct gdma_msg_hdr req;
133 struct gdma_msg_hdr resp; /* The expected response */
134 struct gdma_dev_id dev_id;
138 struct gdma_resp_hdr {
139 struct gdma_msg_hdr response;
140 struct gdma_dev_id dev_id;
146 struct gdma_general_req {
147 struct gdma_req_hdr hdr;
150 #define GDMA_MESSAGE_V1 1
151 #define GDMA_MESSAGE_V2 2
153 struct gdma_general_resp {
154 struct gdma_resp_hdr hdr;
157 #define GDMA_STANDARD_HEADER_TYPE 0
159 static inline void mana_gd_init_req_hdr(struct gdma_req_hdr *hdr, u32 code,
160 u32 req_size, u32 resp_size)
162 hdr->req.hdr_type = GDMA_STANDARD_HEADER_TYPE;
163 hdr->req.msg_type = code;
164 hdr->req.msg_version = GDMA_MESSAGE_V1;
165 hdr->req.msg_size = req_size;
167 hdr->resp.hdr_type = GDMA_STANDARD_HEADER_TYPE;
168 hdr->resp.msg_type = code;
169 hdr->resp.msg_version = GDMA_MESSAGE_V1;
170 hdr->resp.msg_size = resp_size;
173 /* The 16-byte struct is part of the GDMA work queue entry (WQE). */
180 struct gdma_wqe_request {
181 struct gdma_sge *sgl;
185 const void *inline_oob_data;
188 u32 client_data_unit;
191 enum gdma_page_type {
195 #define GDMA_INVALID_DMA_REGION 0
197 struct gdma_mem_info {
200 dma_addr_t dma_handle;
204 /* Allocated by the PF driver */
205 u64 dma_region_handle;
208 #define REGISTER_ATB_MST_MKEY_LOWER_SIZE 8
211 struct gdma_context *gdma_context;
213 struct gdma_dev_id dev_id;
219 /* GDMA driver specific pointer */
222 struct auxiliary_device *adev;
225 #define MINIMUM_SUPPORTED_PAGE_SIZE PAGE_SIZE
227 #define GDMA_CQE_SIZE 64
228 #define GDMA_EQE_SIZE 16
229 #define GDMA_MAX_SQE_SIZE 512
230 #define GDMA_MAX_RQE_SIZE 256
232 #define GDMA_COMP_DATA_SIZE 0x3C
234 #define GDMA_EVENT_DATA_SIZE 0xC
236 /* The WQE size must be a multiple of the Basic Unit, which is 32 bytes. */
237 #define GDMA_WQE_BU_SIZE 32
239 #define INVALID_PDID UINT_MAX
240 #define INVALID_DOORBELL UINT_MAX
241 #define INVALID_MEM_KEY UINT_MAX
242 #define INVALID_QUEUE_ID UINT_MAX
243 #define INVALID_PCI_MSIX_INDEX UINT_MAX
246 u32 cqe_data[GDMA_COMP_DATA_SIZE / 4];
252 u32 details[GDMA_EVENT_DATA_SIZE / 4];
259 struct gdma_queue *eq;
262 typedef void gdma_eq_callback(void *context, struct gdma_queue *q,
263 struct gdma_event *e);
265 typedef void gdma_cq_callback(void *context, struct gdma_queue *q);
267 /* The 'head' is the producer index. For SQ/RQ, when the driver posts a WQE
268 * (Note: the WQE size must be a multiple of the 32-byte Basic Unit), the
269 * driver increases the 'head' in BUs rather than in bytes, and notifies
270 * the HW of the updated head. For EQ/CQ, the driver uses the 'head' to track
271 * the HW head, and increases the 'head' by 1 for every processed EQE/CQE.
273 * The 'tail' is the consumer index for SQ/RQ. After the CQE of the SQ/RQ is
274 * processed, the driver increases the 'tail' to indicate that WQEs have
275 * been consumed by the HW, so the driver can post new WQEs into the SQ/RQ.
277 * The driver doesn't use the 'tail' for EQ/CQ, because the driver ensures
278 * that the EQ/CQ is big enough so they can't overflow, and the driver uses
279 * the owner bits mechanism to detect if the queue has become empty.
282 struct gdma_dev *gdma_dev;
284 enum gdma_queue_type type;
287 struct gdma_mem_info mem_info;
292 bool monitor_avl_buf;
297 /* Extra fields specific to EQ/CQ. */
302 gdma_eq_callback *callback;
305 unsigned int msix_index;
307 u32 log2_throttle_limit;
311 gdma_cq_callback *callback;
314 struct gdma_queue *parent; /* For CQ/EQ relationship */
319 struct gdma_queue_spec {
320 enum gdma_queue_type type;
321 bool monitor_avl_buf;
322 unsigned int queue_size;
324 /* Extra fields specific to EQ/CQ. */
327 gdma_eq_callback *callback;
330 unsigned long log2_throttle_limit;
334 gdma_cq_callback *callback;
337 struct gdma_queue *parent_eq;
343 #define MANA_IRQ_NAME_SZ 32
345 struct gdma_irq_context {
346 void (*handler)(void *arg);
348 char name[MANA_IRQ_NAME_SZ];
351 struct gdma_context {
354 /* Per-vPort max number of queues */
355 unsigned int max_num_queues;
356 unsigned int max_num_msix;
357 unsigned int num_msix_usable;
358 struct gdma_resource msix_resource;
359 struct gdma_irq_context *irq_contexts;
364 /* This maps a CQ index to the queue structure. */
365 unsigned int max_num_cqs;
366 struct gdma_queue **cq_table;
368 /* Protect eq_test_event and test_event_eq_id */
369 struct mutex eq_test_event_mutex;
370 struct completion eq_test_event;
371 u32 test_event_eq_id;
375 void __iomem *bar0_va;
376 void __iomem *shm_base;
377 void __iomem *db_page_base;
378 phys_addr_t phys_db_page_base;
382 /* Shared memory chanenl (used to bootstrap HWC) */
383 struct shm_channel shm_channel;
385 /* Hardware communication channel (HWC) */
388 /* Azure network adapter */
389 struct gdma_dev mana;
392 #define MAX_NUM_GDMA_DEVICES 4
394 static inline bool mana_gd_is_mana(struct gdma_dev *gd)
396 return gd->dev_id.type == GDMA_DEVICE_MANA;
399 static inline bool mana_gd_is_hwc(struct gdma_dev *gd)
401 return gd->dev_id.type == GDMA_DEVICE_HWC;
404 u8 *mana_gd_get_wqe_ptr(const struct gdma_queue *wq, u32 wqe_offset);
405 u32 mana_gd_wq_avail_space(struct gdma_queue *wq);
407 int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq);
409 int mana_gd_create_hwc_queue(struct gdma_dev *gd,
410 const struct gdma_queue_spec *spec,
411 struct gdma_queue **queue_ptr);
413 int mana_gd_create_mana_eq(struct gdma_dev *gd,
414 const struct gdma_queue_spec *spec,
415 struct gdma_queue **queue_ptr);
417 int mana_gd_create_mana_wq_cq(struct gdma_dev *gd,
418 const struct gdma_queue_spec *spec,
419 struct gdma_queue **queue_ptr);
421 void mana_gd_destroy_queue(struct gdma_context *gc, struct gdma_queue *queue);
423 int mana_gd_poll_cq(struct gdma_queue *cq, struct gdma_comp *comp, int num_cqe);
425 void mana_gd_ring_cq(struct gdma_queue *cq, u8 arm_bit);
436 u32 inline_oob_size_div4:3;
437 u32 client_oob_in_sgl :1;
439 u32 client_data_unit :14;
445 #define INLINE_OOB_SMALL_SIZE 8
446 #define INLINE_OOB_LARGE_SIZE 24
448 #define MAX_TX_WQE_SIZE 512
449 #define MAX_RX_WQE_SIZE 256
451 #define MAX_TX_WQE_SGL_ENTRIES ((GDMA_MAX_SQE_SIZE - \
452 sizeof(struct gdma_sge) - INLINE_OOB_SMALL_SIZE) / \
453 sizeof(struct gdma_sge))
455 #define MAX_RX_WQE_SGL_ENTRIES ((GDMA_MAX_RQE_SIZE - \
456 sizeof(struct gdma_sge)) / sizeof(struct gdma_sge))
459 u32 cqe_data[GDMA_COMP_DATA_SIZE / 4];
473 #define GDMA_CQE_OWNER_BITS 3
475 #define GDMA_CQE_OWNER_MASK ((1 << GDMA_CQE_OWNER_BITS) - 1)
477 #define SET_ARM_BIT 1
479 #define GDMA_EQE_OWNER_BITS 3
481 union gdma_eqe_info {
493 #define GDMA_EQE_OWNER_MASK ((1 << GDMA_EQE_OWNER_BITS) - 1)
494 #define INITIALIZED_OWNER_BIT(log2_num_entries) (1UL << (log2_num_entries))
497 u32 details[GDMA_EVENT_DATA_SIZE / 4];
501 #define GDMA_REG_DB_PAGE_OFFSET 8
502 #define GDMA_REG_DB_PAGE_SIZE 0x10
503 #define GDMA_REG_SHM_OFFSET 0x18
505 #define GDMA_PF_REG_DB_PAGE_SIZE 0xD0
506 #define GDMA_PF_REG_DB_PAGE_OFF 0xC8
507 #define GDMA_PF_REG_SHM_OFF 0x70
509 #define GDMA_SRIOV_REG_CFG_BASE_OFF 0x108
511 #define MANA_PF_DEVICE_ID 0x00B9
512 #define MANA_VF_DEVICE_ID 0x00BA
514 struct gdma_posted_wqe_info {
518 /* GDMA_GENERATE_TEST_EQE */
519 struct gdma_generate_test_event_req {
520 struct gdma_req_hdr hdr;
524 /* GDMA_VERIFY_VF_DRIVER_VERSION */
526 GDMA_PROTOCOL_V1 = 1,
527 GDMA_PROTOCOL_FIRST = GDMA_PROTOCOL_V1,
528 GDMA_PROTOCOL_LAST = GDMA_PROTOCOL_V1,
531 #define GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT BIT(0)
533 /* Advertise to the NIC firmware: the NAPI work_done variable race is fixed,
534 * so the driver is able to reliably support features like busy_poll.
536 #define GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX BIT(2)
537 #define GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG BIT(3)
539 #define GDMA_DRV_CAP_FLAGS1 \
540 (GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT | \
541 GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX | \
542 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG)
544 #define GDMA_DRV_CAP_FLAGS2 0
546 #define GDMA_DRV_CAP_FLAGS3 0
548 #define GDMA_DRV_CAP_FLAGS4 0
550 struct gdma_verify_ver_req {
551 struct gdma_req_hdr hdr;
553 /* Mandatory fields required for protocol establishment */
554 u64 protocol_ver_min;
555 u64 protocol_ver_max;
557 /* Gdma Driver Capability Flags */
558 u64 gd_drv_cap_flags1;
559 u64 gd_drv_cap_flags2;
560 u64 gd_drv_cap_flags3;
561 u64 gd_drv_cap_flags4;
563 /* Advisory fields */
565 u32 os_type; /* Linux = 0x10; Windows = 0x20; Other = 0x30 */
578 struct gdma_verify_ver_resp {
579 struct gdma_resp_hdr hdr;
580 u64 gdma_protocol_ver;
587 /* GDMA_QUERY_MAX_RESOURCES */
588 struct gdma_query_max_resources_resp {
589 struct gdma_resp_hdr hdr;
602 /* GDMA_LIST_DEVICES */
603 struct gdma_list_devices_resp {
604 struct gdma_resp_hdr hdr;
607 struct gdma_dev_id devs[64];
610 /* GDMA_REGISTER_DEVICE */
611 struct gdma_register_device_resp {
612 struct gdma_resp_hdr hdr;
618 struct gdma_allocate_resource_range_req {
619 struct gdma_req_hdr hdr;
623 u32 allocated_resources;
626 struct gdma_allocate_resource_range_resp {
627 struct gdma_resp_hdr hdr;
628 u32 allocated_resources;
631 struct gdma_destroy_resource_range_req {
632 struct gdma_req_hdr hdr;
635 u32 allocated_resources;
638 /* GDMA_CREATE_QUEUE */
639 struct gdma_create_queue_req {
640 struct gdma_req_hdr hdr;
648 u32 log2_throttle_limit;
649 u32 eq_pci_msix_index;
652 u8 rq_drop_on_overrun;
653 u8 rq_err_on_wqe_overflow;
654 u8 rq_chain_rec_wqes;
659 struct gdma_create_queue_resp {
660 struct gdma_resp_hdr hdr;
664 /* GDMA_DISABLE_QUEUE */
665 struct gdma_disable_queue_req {
666 struct gdma_req_hdr hdr;
669 u32 alloc_res_id_on_creation;
672 /* GDMA_QUERY_HWC_TIMEOUT */
673 struct gdma_query_hwc_timeout_req {
674 struct gdma_req_hdr hdr;
679 struct gdma_query_hwc_timeout_resp {
680 struct gdma_resp_hdr hdr;
699 enum gdma_mr_access_flags {
700 GDMA_ACCESS_FLAG_LOCAL_READ = BIT_ULL(0),
701 GDMA_ACCESS_FLAG_LOCAL_WRITE = BIT_ULL(1),
702 GDMA_ACCESS_FLAG_REMOTE_READ = BIT_ULL(2),
703 GDMA_ACCESS_FLAG_REMOTE_WRITE = BIT_ULL(3),
704 GDMA_ACCESS_FLAG_REMOTE_ATOMIC = BIT_ULL(4),
707 /* GDMA_CREATE_DMA_REGION */
708 struct gdma_create_dma_region_req {
709 struct gdma_req_hdr hdr;
711 /* The total size of the DMA region */
714 /* The offset in the first page */
717 /* enum gdma_page_type */
720 /* The total number of pages */
723 /* If page_addr_list_len is smaller than page_count,
724 * the remaining page addresses will be added via the
725 * message GDMA_DMA_REGION_ADD_PAGES.
727 u32 page_addr_list_len;
728 u64 page_addr_list[];
731 struct gdma_create_dma_region_resp {
732 struct gdma_resp_hdr hdr;
733 u64 dma_region_handle;
736 /* GDMA_DMA_REGION_ADD_PAGES */
737 struct gdma_dma_region_add_pages_req {
738 struct gdma_req_hdr hdr;
740 u64 dma_region_handle;
742 u32 page_addr_list_len;
745 u64 page_addr_list[];
748 /* GDMA_DESTROY_DMA_REGION */
749 struct gdma_destroy_dma_region_req {
750 struct gdma_req_hdr hdr;
752 u64 dma_region_handle;
756 GDMA_PD_FLAG_INVALID = 0,
759 struct gdma_create_pd_req {
760 struct gdma_req_hdr hdr;
761 enum gdma_pd_flags flags;
765 struct gdma_create_pd_resp {
766 struct gdma_resp_hdr hdr;
772 struct gdma_destroy_pd_req {
773 struct gdma_req_hdr hdr;
777 struct gdma_destory_pd_resp {
778 struct gdma_resp_hdr hdr;
782 /* Guest Virtual Address - MRs of this type allow access
783 * to memory mapped by PTEs associated with this MR using a virtual
784 * address that is set up in the MST
786 GDMA_MR_TYPE_GVA = 2,
789 struct gdma_create_mr_params {
791 enum gdma_mr_type mr_type;
794 u64 dma_region_handle;
796 enum gdma_mr_access_flags access_flags;
801 struct gdma_create_mr_request {
802 struct gdma_req_hdr hdr;
804 enum gdma_mr_type mr_type;
809 u64 dma_region_handle;
811 enum gdma_mr_access_flags access_flags;
818 struct gdma_create_mr_response {
819 struct gdma_resp_hdr hdr;
825 struct gdma_destroy_mr_request {
826 struct gdma_req_hdr hdr;
830 struct gdma_destroy_mr_response {
831 struct gdma_resp_hdr hdr;
834 int mana_gd_verify_vf_version(struct pci_dev *pdev);
836 int mana_gd_register_device(struct gdma_dev *gd);
837 int mana_gd_deregister_device(struct gdma_dev *gd);
839 int mana_gd_post_work_request(struct gdma_queue *wq,
840 const struct gdma_wqe_request *wqe_req,
841 struct gdma_posted_wqe_info *wqe_info);
843 int mana_gd_post_and_ring(struct gdma_queue *queue,
844 const struct gdma_wqe_request *wqe,
845 struct gdma_posted_wqe_info *wqe_info);
847 int mana_gd_alloc_res_map(u32 res_avail, struct gdma_resource *r);
848 void mana_gd_free_res_map(struct gdma_resource *r);
850 void mana_gd_wq_ring_doorbell(struct gdma_context *gc,
851 struct gdma_queue *queue);
853 int mana_gd_alloc_memory(struct gdma_context *gc, unsigned int length,
854 struct gdma_mem_info *gmi);
856 void mana_gd_free_memory(struct gdma_mem_info *gmi);
858 int mana_gd_send_request(struct gdma_context *gc, u32 req_len, const void *req,
859 u32 resp_len, void *resp);
861 int mana_gd_destroy_dma_region(struct gdma_context *gc, u64 dma_region_handle);