| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | // Copyright (c) 2025 Broadcom. |
| 3 | |
| 4 | #ifndef __BNG_FW_H__ |
| 5 | #define __BNG_FW_H__ |
| 6 | |
| 7 | #include "bng_tlv.h" |
| 8 | |
| 9 | /* FW DB related */ |
| 10 | #define BNG_FW_CMDQ_TRIG_VAL 1 |
| 11 | #define BNG_FW_COMM_PCI_BAR_REGION 0 |
| 12 | #define BNG_FW_COMM_CONS_PCI_BAR_REGION 2 |
| 13 | #define BNG_FW_DBR_BASE_PAGE_SHIFT 12 |
| 14 | #define BNG_FW_COMM_SIZE 0x104 |
| 15 | #define BNG_FW_COMM_BASE_OFFSET 0x600 |
| 16 | #define BNG_FW_COMM_TRIG_OFFSET 0x100 |
| 17 | #define BNG_FW_PF_VF_COMM_PROD_OFFSET 0xc |
| 18 | #define BNG_FW_CREQ_DB_LEN 8 |
| 19 | |
| 20 | /* CREQ */ |
| 21 | #define BNG_FW_CREQE_MAX_CNT (64 * 1024) |
| 22 | #define BNG_FW_CREQE_UNITS 16 |
| 23 | #define BNG_FW_CREQ_ENTRY_POLL_BUDGET 0x100 |
| 24 | #define BNG_FW_CREQ_CMP_VALID(hdr, pass) \ |
| 25 | (!!((hdr)->v & CREQ_BASE_V) == \ |
| 26 | !((pass) & BNG_RE_FLAG_EPOCH_CONS_MASK)) |
| 27 | #define BNG_FW_CREQ_ENTRY_POLL_BUDGET 0x100 |
| 28 | |
| 29 | /* CMDQ */ |
| 30 | struct bng_fw_cmdqe { |
| 31 | u8 data[16]; |
| 32 | }; |
| 33 | |
| 34 | #define BNG_FW_CMDQE_MAX_CNT 8192 |
| 35 | #define BNG_FW_CMDQE_UNITS sizeof(struct bng_fw_cmdqe) |
| 36 | #define BNG_FW_CMDQE_BYTES(depth) ((depth) * BNG_FW_CMDQE_UNITS) |
| 37 | |
| 38 | #define BNG_FW_MAX_COOKIE_VALUE (BNG_FW_CMDQE_MAX_CNT - 1) |
| 39 | #define BNG_FW_CMD_IS_BLOCKING 0x8000 |
| 40 | |
| 41 | /* Crsq buf is 1024-Byte */ |
| 42 | struct bng_re_crsbe { |
| 43 | u8 data[1024]; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | static inline u32 bng_fw_cmdqe_npages(u32 depth) |
| 48 | { |
| 49 | u32 npages; |
| 50 | |
| 51 | npages = BNG_FW_CMDQE_BYTES(depth) / PAGE_SIZE; |
| 52 | if (BNG_FW_CMDQE_BYTES(depth) % PAGE_SIZE) |
| 53 | npages++; |
| 54 | return npages; |
| 55 | } |
| 56 | |
| 57 | static inline u32 bng_fw_cmdqe_page_size(u32 depth) |
| 58 | { |
| 59 | return (bng_fw_cmdqe_npages(depth) * PAGE_SIZE); |
| 60 | } |
| 61 | struct bng_re_cmdq_mbox { |
| 62 | struct bng_re_reg_desc reg; |
| 63 | void __iomem *prod; |
| 64 | void __iomem *db; |
| 65 | }; |
| 66 | |
| 67 | /* HWQ */ |
| 68 | struct bng_re_cmdq_ctx { |
| 69 | struct bng_re_hwq hwq; |
| 70 | struct bng_re_cmdq_mbox cmdq_mbox; |
| 71 | unsigned long flags; |
| 72 | #define FIRMWARE_INITIALIZED_FLAG (0) |
| 73 | #define FIRMWARE_STALL_DETECTED (3) |
| 74 | #define FIRMWARE_FIRST_FLAG (31) |
| 75 | wait_queue_head_t waitq; |
| 76 | u32 seq_num; |
| 77 | }; |
| 78 | |
| 79 | struct bng_re_creq_db { |
| 80 | struct bng_re_reg_desc reg; |
| 81 | struct bng_re_db_info dbinfo; |
| 82 | }; |
| 83 | |
| 84 | struct bng_re_creq_stat { |
| 85 | u64 creq_qp_event_processed; |
| 86 | u64 creq_func_event_processed; |
| 87 | }; |
| 88 | |
| 89 | struct bng_re_creq_ctx { |
| 90 | struct bng_re_hwq hwq; |
| 91 | struct bng_re_creq_db creq_db; |
| 92 | struct bng_re_creq_stat stats; |
| 93 | struct tasklet_struct creq_tasklet; |
| 94 | u16 ring_id; |
| 95 | int msix_vec; |
| 96 | bool irq_handler_avail; |
| 97 | char *irq_name; |
| 98 | }; |
| 99 | |
| 100 | struct bng_re_crsqe { |
| 101 | struct creq_qp_event *resp; |
| 102 | u32 req_size; |
| 103 | /* Free slots at the time of submission */ |
| 104 | u32 free_slots; |
| 105 | u8 opcode; |
| 106 | bool is_waiter_alive; |
| 107 | bool is_in_used; |
| 108 | }; |
| 109 | |
| 110 | struct bng_re_rcfw_sbuf { |
| 111 | void *sb; |
| 112 | dma_addr_t dma_addr; |
| 113 | u32 size; |
| 114 | }; |
| 115 | |
| 116 | /* RoCE FW Communication Channels */ |
| 117 | struct bng_re_rcfw { |
| 118 | struct pci_dev *pdev; |
| 119 | struct bng_re_res *res; |
| 120 | struct bng_re_cmdq_ctx cmdq; |
| 121 | struct bng_re_creq_ctx creq; |
| 122 | struct bng_re_crsqe *crsqe_tbl; |
| 123 | /* To synchronize the qp-handle hash table */ |
| 124 | spinlock_t tbl_lock; |
| 125 | u32 cmdq_depth; |
| 126 | /* cached from chip cctx for quick reference in slow path */ |
| 127 | u16 max_timeout; |
| 128 | atomic_t rcfw_intr_enabled; |
| 129 | }; |
| 130 | |
| 131 | struct bng_re_cmdqmsg { |
| 132 | struct cmdq_base *req; |
| 133 | struct creq_base *resp; |
| 134 | void *sb; |
| 135 | u32 req_sz; |
| 136 | u32 res_sz; |
| 137 | u8 block; |
| 138 | }; |
| 139 | |
| 140 | static inline void bng_re_rcfw_cmd_prep(struct cmdq_base *req, |
| 141 | u8 opcode, u8 cmd_size) |
| 142 | { |
| 143 | req->opcode = opcode; |
| 144 | req->cmd_size = cmd_size; |
| 145 | } |
| 146 | |
| 147 | static inline void bng_re_fill_cmdqmsg(struct bng_re_cmdqmsg *msg, |
| 148 | void *req, void *resp, void *sb, |
| 149 | u32 req_sz, u32 res_sz, u8 block) |
| 150 | { |
| 151 | msg->req = req; |
| 152 | msg->resp = resp; |
| 153 | msg->sb = sb; |
| 154 | msg->req_sz = req_sz; |
| 155 | msg->res_sz = res_sz; |
| 156 | msg->block = block; |
| 157 | } |
| 158 | |
| 159 | /* Get the number of command units required for the req. The |
| 160 | * function returns correct value only if called before |
| 161 | * setting using bng_re_set_cmd_slots |
| 162 | */ |
| 163 | static inline u32 bng_re_get_cmd_slots(struct cmdq_base *req) |
| 164 | { |
| 165 | u32 cmd_units = 0; |
| 166 | |
| 167 | if (HAS_TLV_HEADER(req)) { |
| 168 | struct roce_tlv *tlv_req = (struct roce_tlv *)req; |
| 169 | |
| 170 | cmd_units = tlv_req->total_size; |
| 171 | } else { |
| 172 | cmd_units = (req->cmd_size + BNG_FW_CMDQE_UNITS - 1) / |
| 173 | BNG_FW_CMDQE_UNITS; |
| 174 | } |
| 175 | |
| 176 | return cmd_units; |
| 177 | } |
| 178 | |
| 179 | static inline u32 bng_re_set_cmd_slots(struct cmdq_base *req) |
| 180 | { |
| 181 | u32 cmd_byte = 0; |
| 182 | |
| 183 | if (HAS_TLV_HEADER(req)) { |
| 184 | struct roce_tlv *tlv_req = (struct roce_tlv *)req; |
| 185 | |
| 186 | cmd_byte = tlv_req->total_size * BNG_FW_CMDQE_UNITS; |
| 187 | } else { |
| 188 | cmd_byte = req->cmd_size; |
| 189 | req->cmd_size = (req->cmd_size + BNG_FW_CMDQE_UNITS - 1) / |
| 190 | BNG_FW_CMDQE_UNITS; |
| 191 | } |
| 192 | |
| 193 | return cmd_byte; |
| 194 | } |
| 195 | |
| 196 | void bng_re_free_rcfw_channel(struct bng_re_rcfw *rcfw); |
| 197 | int bng_re_alloc_fw_channel(struct bng_re_res *res, |
| 198 | struct bng_re_rcfw *rcfw); |
| 199 | int bng_re_enable_fw_channel(struct bng_re_rcfw *rcfw, |
| 200 | int msix_vector, |
| 201 | int cp_bar_reg_off); |
| 202 | void bng_re_disable_rcfw_channel(struct bng_re_rcfw *rcfw); |
| 203 | int bng_re_rcfw_start_irq(struct bng_re_rcfw *rcfw, int msix_vector, |
| 204 | bool need_init); |
| 205 | void bng_re_rcfw_stop_irq(struct bng_re_rcfw *rcfw, bool kill); |
| 206 | int bng_re_rcfw_send_message(struct bng_re_rcfw *rcfw, |
| 207 | struct bng_re_cmdqmsg *msg); |
| 208 | int bng_re_init_rcfw(struct bng_re_rcfw *rcfw, |
| 209 | struct bng_re_stats *stats_ctx); |
| 210 | int bng_re_deinit_rcfw(struct bng_re_rcfw *rcfw); |
| 211 | #endif |
| 212 | |