Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | // SPDX-License-Identifier: GPL-2.0-only /* * SolidRun DPU driver for control plane * * Copyright (C) 2022-2023 SolidRun * * Author: Alvaro Karsz <alvaro.karsz@solid-run.com> * */ #include <linux/iopoll.h> #include "snet_vdpa.h" enum snet_ctrl_opcodes { SNET_CTRL_OP_DESTROY = 1, SNET_CTRL_OP_READ_VQ_STATE, SNET_CTRL_OP_SUSPEND, SNET_CTRL_OP_RESUME, }; #define SNET_CTRL_TIMEOUT 2000000 #define SNET_CTRL_DATA_SIZE_MASK 0x0000FFFF #define SNET_CTRL_IN_PROCESS_MASK 0x00010000 #define SNET_CTRL_CHUNK_RDY_MASK 0x00020000 #define SNET_CTRL_ERROR_MASK 0x0FFC0000 #define SNET_VAL_TO_ERR(val) (-(((val) & SNET_CTRL_ERROR_MASK) >> 18)) #define SNET_EMPTY_CTRL(val) (((val) & SNET_CTRL_ERROR_MASK) || \ !((val) & SNET_CTRL_IN_PROCESS_MASK)) #define SNET_DATA_READY(val) ((val) & (SNET_CTRL_ERROR_MASK | SNET_CTRL_CHUNK_RDY_MASK)) /* Control register used to read data from the DPU */ struct snet_ctrl_reg_ctrl { /* Chunk size in 4B words */ u16 data_size; /* We are in the middle of a command */ u16 in_process:1; /* A data chunk is ready and can be consumed */ u16 chunk_ready:1; /* Error code */ u16 error:10; /* Saved for future usage */ u16 rsvd:4; }; /* Opcode register */ struct snet_ctrl_reg_op { u16 opcode; /* Only if VQ index is relevant for the command */ u16 vq_idx; }; struct snet_ctrl_regs { struct snet_ctrl_reg_op op; struct snet_ctrl_reg_ctrl ctrl; u32 rsvd; u32 data[]; }; static struct snet_ctrl_regs __iomem *snet_get_ctrl(struct snet *snet) { return snet->bar + snet->psnet->cfg.ctrl_off; } static int snet_wait_for_empty_ctrl(struct snet_ctrl_regs __iomem *regs) { u32 val; return readx_poll_timeout(ioread32, ®s->ctrl, val, SNET_EMPTY_CTRL(val), 10, SNET_CTRL_TIMEOUT); } static int snet_wait_for_empty_op(struct snet_ctrl_regs __iomem *regs) { u32 val; return readx_poll_timeout(ioread32, ®s->op, val, !val, 10, SNET_CTRL_TIMEOUT); } static int snet_wait_for_data(struct snet_ctrl_regs __iomem *regs) { u32 val; return readx_poll_timeout(ioread32, ®s->ctrl, val, SNET_DATA_READY(val), 10, SNET_CTRL_TIMEOUT); } static u32 snet_read32_word(struct snet_ctrl_regs __iomem *ctrl_regs, u16 word_idx) { return ioread32(&ctrl_regs->data[word_idx]); } static u32 snet_read_ctrl(struct snet_ctrl_regs __iomem *ctrl_regs) { return ioread32(&ctrl_regs->ctrl); } static void snet_write_ctrl(struct snet_ctrl_regs __iomem *ctrl_regs, u32 val) { iowrite32(val, &ctrl_regs->ctrl); } static void snet_write_op(struct snet_ctrl_regs __iomem *ctrl_regs, u32 val) { iowrite32(val, &ctrl_regs->op); } static int snet_wait_for_dpu_completion(struct snet_ctrl_regs __iomem *ctrl_regs) { /* Wait until the DPU finishes completely. * It will clear the opcode register. */ return snet_wait_for_empty_op(ctrl_regs); } /* Reading ctrl from the DPU: * buf_size must be 4B aligned * * Steps: * * (1) Verify that the DPU is not in the middle of another operation by * reading the in_process and error bits in the control register. * (2) Write the request opcode and the VQ idx in the opcode register * and write the buffer size in the control register. * (3) Start readind chunks of data, chunk_ready bit indicates that a * data chunk is available, we signal that we read the data by clearing the bit. * (4) Detect that the transfer is completed when the in_process bit * in the control register is cleared or when the an error appears. */ static int snet_ctrl_read_from_dpu(struct snet *snet, u16 opcode, u16 vq_idx, void *buffer, u32 buf_size) { struct pci_dev *pdev = snet->pdev; struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet); u32 *bfr_ptr = (u32 *)buffer; u32 val; u16 buf_words; int ret; u16 words, i, tot_words = 0; /* Supported for config 2+ */ if (!SNET_CFG_VER(snet, 2)) return -EOPNOTSUPP; if (!IS_ALIGNED(buf_size, 4)) return -EINVAL; mutex_lock(&snet->ctrl_lock); buf_words = buf_size / 4; /* Make sure control register is empty */ ret = snet_wait_for_empty_ctrl(regs); if (ret) { SNET_WARN(pdev, "Timeout waiting for previous control data to be consumed\n"); goto exit; } /* We need to write the buffer size in the control register, and the opcode + vq index in * the opcode register. * We use a spinlock to serialize the writes. */ spin_lock(&snet->ctrl_spinlock); snet_write_ctrl(regs, buf_words); snet_write_op(regs, opcode | (vq_idx << 16)); spin_unlock(&snet->ctrl_spinlock); while (buf_words != tot_words) { ret = snet_wait_for_data(regs); if (ret) { SNET_WARN(pdev, "Timeout waiting for control data\n"); goto exit; } val = snet_read_ctrl(regs); /* Error? */ if (val & SNET_CTRL_ERROR_MASK) { ret = SNET_VAL_TO_ERR(val); SNET_WARN(pdev, "Error while reading control data from DPU, err %d\n", ret); goto exit; } words = min_t(u16, val & SNET_CTRL_DATA_SIZE_MASK, buf_words - tot_words); for (i = 0; i < words; i++) { *bfr_ptr = snet_read32_word(regs, i); bfr_ptr++; } tot_words += words; /* Is the job completed? */ if (!(val & SNET_CTRL_IN_PROCESS_MASK)) break; /* Clear the chunk ready bit and continue */ val &= ~SNET_CTRL_CHUNK_RDY_MASK; snet_write_ctrl(regs, val); } ret = snet_wait_for_dpu_completion(regs); if (ret) SNET_WARN(pdev, "Timeout waiting for the DPU to complete a control command\n"); exit: mutex_unlock(&snet->ctrl_lock); return ret; } /* Send a control message to the DPU using the old mechanism * used with config version 1. */ static int snet_send_ctrl_msg_old(struct snet *snet, u32 opcode) { struct pci_dev *pdev = snet->pdev; struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet); int ret; mutex_lock(&snet->ctrl_lock); /* Old mechanism uses just 1 register, the opcode register. * Make sure that the opcode register is empty, and that the DPU isn't * processing an old message. */ ret = snet_wait_for_empty_op(regs); if (ret) { SNET_WARN(pdev, "Timeout waiting for previous control message to be ACKed\n"); goto exit; } /* Write the message */ snet_write_op(regs, opcode); /* DPU ACKs the message by clearing the opcode register */ ret = snet_wait_for_empty_op(regs); if (ret) SNET_WARN(pdev, "Timeout waiting for a control message to be ACKed\n"); exit: mutex_unlock(&snet->ctrl_lock); return ret; } /* Send a control message to the DPU. * A control message is a message without payload. */ static int snet_send_ctrl_msg(struct snet *snet, u16 opcode, u16 vq_idx) { struct pci_dev *pdev = snet->pdev; struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet); u32 val; int ret; /* If config version is not 2+, use the old mechanism */ if (!SNET_CFG_VER(snet, 2)) return snet_send_ctrl_msg_old(snet, opcode); mutex_lock(&snet->ctrl_lock); /* Make sure control register is empty */ ret = snet_wait_for_empty_ctrl(regs); if (ret) { SNET_WARN(pdev, "Timeout waiting for previous control data to be consumed\n"); goto exit; } /* We need to clear the control register and write the opcode + vq index in the opcode * register. * We use a spinlock to serialize the writes. */ spin_lock(&snet->ctrl_spinlock); snet_write_ctrl(regs, 0); snet_write_op(regs, opcode | (vq_idx << 16)); spin_unlock(&snet->ctrl_spinlock); /* The DPU ACKs control messages by setting the chunk ready bit * without data. */ ret = snet_wait_for_data(regs); if (ret) { SNET_WARN(pdev, "Timeout waiting for control message to be ACKed\n"); goto exit; } /* Check for errors */ val = snet_read_ctrl(regs); ret = SNET_VAL_TO_ERR(val); /* Clear the chunk ready bit */ val &= ~SNET_CTRL_CHUNK_RDY_MASK; snet_write_ctrl(regs, val); ret = snet_wait_for_dpu_completion(regs); if (ret) SNET_WARN(pdev, "Timeout waiting for DPU to complete a control command, err %d\n", ret); exit: mutex_unlock(&snet->ctrl_lock); return ret; } void snet_ctrl_clear(struct snet *snet) { struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet); snet_write_op(regs, 0); } int snet_destroy_dev(struct snet *snet) { return snet_send_ctrl_msg(snet, SNET_CTRL_OP_DESTROY, 0); } int snet_read_vq_state(struct snet *snet, u16 idx, struct vdpa_vq_state *state) { return snet_ctrl_read_from_dpu(snet, SNET_CTRL_OP_READ_VQ_STATE, idx, state, sizeof(*state)); } int snet_suspend_dev(struct snet *snet) { return snet_send_ctrl_msg(snet, SNET_CTRL_OP_SUSPEND, 0); } int snet_resume_dev(struct snet *snet) { return snet_send_ctrl_msg(snet, SNET_CTRL_OP_RESUME, 0); } |