1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 HiSilicon Limited. */ 3 #include <asm/page.h> 4 #include <linux/acpi.h> 5 #include <linux/aer.h> 6 #include <linux/bitmap.h> 7 #include <linux/debugfs.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/idr.h> 10 #include <linux/io.h> 11 #include <linux/irqreturn.h> 12 #include <linux/log2.h> 13 #include <linux/seq_file.h> 14 #include <linux/slab.h> 15 #include <linux/uacce.h> 16 #include <linux/uaccess.h> 17 #include <uapi/misc/uacce/hisi_qm.h> 18 #include "qm.h" 19 20 /* eq/aeq irq enable */ 21 #define QM_VF_AEQ_INT_SOURCE 0x0 22 #define QM_VF_AEQ_INT_MASK 0x4 23 #define QM_VF_EQ_INT_SOURCE 0x8 24 #define QM_VF_EQ_INT_MASK 0xc 25 #define QM_IRQ_NUM_V1 1 26 #define QM_IRQ_NUM_PF_V2 4 27 #define QM_IRQ_NUM_VF_V2 2 28 29 #define QM_EQ_EVENT_IRQ_VECTOR 0 30 #define QM_AEQ_EVENT_IRQ_VECTOR 1 31 #define QM_ABNORMAL_EVENT_IRQ_VECTOR 3 32 33 /* mailbox */ 34 #define QM_MB_CMD_SQC 0x0 35 #define QM_MB_CMD_CQC 0x1 36 #define QM_MB_CMD_EQC 0x2 37 #define QM_MB_CMD_AEQC 0x3 38 #define QM_MB_CMD_SQC_BT 0x4 39 #define QM_MB_CMD_CQC_BT 0x5 40 #define QM_MB_CMD_SQC_VFT_V2 0x6 41 42 #define QM_MB_CMD_SEND_BASE 0x300 43 #define QM_MB_EVENT_SHIFT 8 44 #define QM_MB_BUSY_SHIFT 13 45 #define QM_MB_OP_SHIFT 14 46 #define QM_MB_CMD_DATA_ADDR_L 0x304 47 #define QM_MB_CMD_DATA_ADDR_H 0x308 48 49 /* sqc shift */ 50 #define QM_SQ_HOP_NUM_SHIFT 0 51 #define QM_SQ_PAGE_SIZE_SHIFT 4 52 #define QM_SQ_BUF_SIZE_SHIFT 8 53 #define QM_SQ_SQE_SIZE_SHIFT 12 54 #define QM_SQ_PRIORITY_SHIFT 0 55 #define QM_SQ_ORDERS_SHIFT 4 56 #define QM_SQ_TYPE_SHIFT 8 57 #define QM_QC_PASID_ENABLE 0x1 58 #define QM_QC_PASID_ENABLE_SHIFT 7 59 60 #define QM_SQ_TYPE_MASK GENMASK(3, 0) 61 #define QM_SQ_TAIL_IDX(sqc) ((le16_to_cpu((sqc)->w11) >> 6) & 0x1) 62 63 /* cqc shift */ 64 #define QM_CQ_HOP_NUM_SHIFT 0 65 #define QM_CQ_PAGE_SIZE_SHIFT 4 66 #define QM_CQ_BUF_SIZE_SHIFT 8 67 #define QM_CQ_CQE_SIZE_SHIFT 12 68 #define QM_CQ_PHASE_SHIFT 0 69 #define QM_CQ_FLAG_SHIFT 1 70 71 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1) 72 #define QM_QC_CQE_SIZE 4 73 #define QM_CQ_TAIL_IDX(cqc) ((le16_to_cpu((cqc)->w11) >> 6) & 0x1) 74 75 /* eqc shift */ 76 #define QM_EQE_AEQE_SIZE (2UL << 12) 77 #define QM_EQC_PHASE_SHIFT 16 78 79 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1) 80 #define QM_EQE_CQN_MASK GENMASK(15, 0) 81 82 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1) 83 #define QM_AEQE_TYPE_SHIFT 17 84 85 #define QM_DOORBELL_CMD_SQ 0 86 #define QM_DOORBELL_CMD_CQ 1 87 #define QM_DOORBELL_CMD_EQ 2 88 #define QM_DOORBELL_CMD_AEQ 3 89 90 #define QM_DOORBELL_BASE_V1 0x340 91 #define QM_DB_CMD_SHIFT_V1 16 92 #define QM_DB_INDEX_SHIFT_V1 32 93 #define QM_DB_PRIORITY_SHIFT_V1 48 94 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000 95 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000 96 #define QM_QUE_ISO_CFG_V 0x0030 97 #define QM_QUE_ISO_EN 0x100154 98 #define QM_CAPBILITY 0x100158 99 #define QM_QP_NUN_MASK GENMASK(10, 0) 100 #define QM_QP_DB_INTERVAL 0x10000 101 #define QM_QP_MAX_NUM_SHIFT 11 102 #define QM_DB_CMD_SHIFT_V2 12 103 #define QM_DB_RAND_SHIFT_V2 16 104 #define QM_DB_INDEX_SHIFT_V2 32 105 #define QM_DB_PRIORITY_SHIFT_V2 48 106 107 #define QM_MEM_START_INIT 0x100040 108 #define QM_MEM_INIT_DONE 0x100044 109 #define QM_VFT_CFG_RDY 0x10006c 110 #define QM_VFT_CFG_OP_WR 0x100058 111 #define QM_VFT_CFG_TYPE 0x10005c 112 #define QM_SQC_VFT 0x0 113 #define QM_CQC_VFT 0x1 114 #define QM_VFT_CFG 0x100060 115 #define QM_VFT_CFG_OP_ENABLE 0x100054 116 117 #define QM_VFT_CFG_DATA_L 0x100064 118 #define QM_VFT_CFG_DATA_H 0x100068 119 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8) 120 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12) 121 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16) 122 #define QM_SQC_VFT_START_SQN_SHIFT 28 123 #define QM_SQC_VFT_VALID (1ULL << 44) 124 #define QM_SQC_VFT_SQN_SHIFT 45 125 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8) 126 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12) 127 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16) 128 #define QM_CQC_VFT_VALID (1ULL << 28) 129 130 #define QM_SQC_VFT_BASE_SHIFT_V2 28 131 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(15, 0) 132 #define QM_SQC_VFT_NUM_SHIFT_V2 45 133 #define QM_SQC_VFT_NUM_MASK_v2 GENMASK(9, 0) 134 135 #define QM_DFX_CNT_CLR_CE 0x100118 136 137 #define QM_ABNORMAL_INT_SOURCE 0x100000 138 #define QM_ABNORMAL_INT_SOURCE_CLR GENMASK(12, 0) 139 #define QM_ABNORMAL_INT_MASK 0x100004 140 #define QM_ABNORMAL_INT_MASK_VALUE 0x1fff 141 #define QM_ABNORMAL_INT_STATUS 0x100008 142 #define QM_ABNORMAL_INT_SET 0x10000c 143 #define QM_ABNORMAL_INF00 0x100010 144 #define QM_FIFO_OVERFLOW_TYPE 0xc0 145 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6 146 #define QM_FIFO_OVERFLOW_VF 0x3f 147 #define QM_ABNORMAL_INF01 0x100014 148 #define QM_DB_TIMEOUT_TYPE 0xc0 149 #define QM_DB_TIMEOUT_TYPE_SHIFT 6 150 #define QM_DB_TIMEOUT_VF 0x3f 151 #define QM_RAS_CE_ENABLE 0x1000ec 152 #define QM_RAS_FE_ENABLE 0x1000f0 153 #define QM_RAS_NFE_ENABLE 0x1000f4 154 #define QM_RAS_CE_THRESHOLD 0x1000f8 155 #define QM_RAS_CE_TIMES_PER_IRQ 1 156 #define QM_RAS_MSI_INT_SEL 0x1040f4 157 158 #define QM_RESET_WAIT_TIMEOUT 400 159 #define QM_PEH_VENDOR_ID 0x1000d8 160 #define ACC_VENDOR_ID_VALUE 0x5a5a 161 #define QM_PEH_DFX_INFO0 0x1000fc 162 #define ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT 3 163 #define ACC_PEH_MSI_DISABLE GENMASK(31, 0) 164 #define ACC_MASTER_GLOBAL_CTRL_SHUTDOWN 0x1 165 #define ACC_MASTER_TRANS_RETURN_RW 3 166 #define ACC_MASTER_TRANS_RETURN 0x300150 167 #define ACC_MASTER_GLOBAL_CTRL 0x300000 168 #define ACC_AM_CFG_PORT_WR_EN 0x30001c 169 #define QM_RAS_NFE_MBIT_DISABLE ~QM_ECC_MBIT 170 #define ACC_AM_ROB_ECC_INT_STS 0x300104 171 #define ACC_ROB_ECC_ERR_MULTPL BIT(1) 172 173 #define QM_DFX_MB_CNT_VF 0x104010 174 #define QM_DFX_DB_CNT_VF 0x104020 175 #define QM_DFX_SQE_CNT_VF_SQN 0x104030 176 #define QM_DFX_CQE_CNT_VF_CQN 0x104040 177 #define QM_DFX_QN_SHIFT 16 178 #define CURRENT_FUN_MASK GENMASK(5, 0) 179 #define CURRENT_Q_MASK GENMASK(31, 16) 180 181 #define POLL_PERIOD 10 182 #define POLL_TIMEOUT 1000 183 #define WAIT_PERIOD_US_MAX 200 184 #define WAIT_PERIOD_US_MIN 100 185 #define MAX_WAIT_COUNTS 1000 186 #define QM_CACHE_WB_START 0x204 187 #define QM_CACHE_WB_DONE 0x208 188 189 #define PCI_BAR_2 2 190 #define PCI_BAR_4 4 191 #define QM_SQE_DATA_ALIGN_MASK GENMASK(6, 0) 192 #define QMC_ALIGN(sz) ALIGN(sz, 32) 193 194 #define QM_DBG_READ_LEN 256 195 #define QM_DBG_WRITE_LEN 1024 196 #define QM_DBG_TMP_BUF_LEN 22 197 #define QM_PCI_COMMAND_INVALID ~0 198 199 #define WAIT_PERIOD 20 200 #define REMOVE_WAIT_DELAY 10 201 #define QM_SQE_ADDR_MASK GENMASK(7, 0) 202 #define QM_EQ_DEPTH (1024 * 2) 203 204 #define QM_DRIVER_REMOVING 0 205 #define QM_RST_SCHED 1 206 #define QM_RESETTING 2 207 208 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \ 209 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \ 210 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \ 211 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \ 212 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 213 214 #define QM_MK_CQC_DW3_V2(cqe_sz) \ 215 ((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 216 217 #define QM_MK_SQC_W13(priority, orders, alg_type) \ 218 (((priority) << QM_SQ_PRIORITY_SHIFT) | \ 219 ((orders) << QM_SQ_ORDERS_SHIFT) | \ 220 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT)) 221 222 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \ 223 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \ 224 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \ 225 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \ 226 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 227 228 #define QM_MK_SQC_DW3_V2(sqe_sz) \ 229 ((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 230 231 #define INIT_QC_COMMON(qc, base, pasid) do { \ 232 (qc)->head = 0; \ 233 (qc)->tail = 0; \ 234 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \ 235 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \ 236 (qc)->dw3 = 0; \ 237 (qc)->w8 = 0; \ 238 (qc)->rsvd0 = 0; \ 239 (qc)->pasid = cpu_to_le16(pasid); \ 240 (qc)->w11 = 0; \ 241 (qc)->rsvd1 = 0; \ 242 } while (0) 243 244 enum vft_type { 245 SQC_VFT = 0, 246 CQC_VFT, 247 }; 248 249 enum acc_err_result { 250 ACC_ERR_NONE, 251 ACC_ERR_NEED_RESET, 252 ACC_ERR_RECOVERED, 253 }; 254 255 struct qm_cqe { 256 __le32 rsvd0; 257 __le16 cmd_id; 258 __le16 rsvd1; 259 __le16 sq_head; 260 __le16 sq_num; 261 __le16 rsvd2; 262 __le16 w7; 263 }; 264 265 struct qm_eqe { 266 __le32 dw0; 267 }; 268 269 struct qm_aeqe { 270 __le32 dw0; 271 }; 272 273 struct qm_sqc { 274 __le16 head; 275 __le16 tail; 276 __le32 base_l; 277 __le32 base_h; 278 __le32 dw3; 279 __le16 w8; 280 __le16 rsvd0; 281 __le16 pasid; 282 __le16 w11; 283 __le16 cq_num; 284 __le16 w13; 285 __le32 rsvd1; 286 }; 287 288 struct qm_cqc { 289 __le16 head; 290 __le16 tail; 291 __le32 base_l; 292 __le32 base_h; 293 __le32 dw3; 294 __le16 w8; 295 __le16 rsvd0; 296 __le16 pasid; 297 __le16 w11; 298 __le32 dw6; 299 __le32 rsvd1; 300 }; 301 302 struct qm_eqc { 303 __le16 head; 304 __le16 tail; 305 __le32 base_l; 306 __le32 base_h; 307 __le32 dw3; 308 __le32 rsvd[2]; 309 __le32 dw6; 310 }; 311 312 struct qm_aeqc { 313 __le16 head; 314 __le16 tail; 315 __le32 base_l; 316 __le32 base_h; 317 __le32 dw3; 318 __le32 rsvd[2]; 319 __le32 dw6; 320 }; 321 322 struct qm_mailbox { 323 __le16 w0; 324 __le16 queue_num; 325 __le32 base_l; 326 __le32 base_h; 327 __le32 rsvd; 328 }; 329 330 struct qm_doorbell { 331 __le16 queue_num; 332 __le16 cmd; 333 __le16 index; 334 __le16 priority; 335 }; 336 337 struct hisi_qm_resource { 338 struct hisi_qm *qm; 339 int distance; 340 struct list_head list; 341 }; 342 343 struct hisi_qm_hw_ops { 344 int (*get_vft)(struct hisi_qm *qm, u32 *base, u32 *number); 345 void (*qm_db)(struct hisi_qm *qm, u16 qn, 346 u8 cmd, u16 index, u8 priority); 347 u32 (*get_irq_num)(struct hisi_qm *qm); 348 int (*debug_init)(struct hisi_qm *qm); 349 void (*hw_error_init)(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe); 350 void (*hw_error_uninit)(struct hisi_qm *qm); 351 enum acc_err_result (*hw_error_handle)(struct hisi_qm *qm); 352 }; 353 354 struct qm_dfx_item { 355 const char *name; 356 u32 offset; 357 }; 358 359 static struct qm_dfx_item qm_dfx_files[] = { 360 {"err_irq", offsetof(struct qm_dfx, err_irq_cnt)}, 361 {"aeq_irq", offsetof(struct qm_dfx, aeq_irq_cnt)}, 362 {"abnormal_irq", offsetof(struct qm_dfx, abnormal_irq_cnt)}, 363 {"create_qp_err", offsetof(struct qm_dfx, create_qp_err_cnt)}, 364 {"mb_err", offsetof(struct qm_dfx, mb_err_cnt)}, 365 }; 366 367 static const char * const qm_debug_file_name[] = { 368 [CURRENT_QM] = "current_qm", 369 [CURRENT_Q] = "current_q", 370 [CLEAR_ENABLE] = "clear_enable", 371 }; 372 373 struct hisi_qm_hw_error { 374 u32 int_msk; 375 const char *msg; 376 }; 377 378 static const struct hisi_qm_hw_error qm_hw_error[] = { 379 { .int_msk = BIT(0), .msg = "qm_axi_rresp" }, 380 { .int_msk = BIT(1), .msg = "qm_axi_bresp" }, 381 { .int_msk = BIT(2), .msg = "qm_ecc_mbit" }, 382 { .int_msk = BIT(3), .msg = "qm_ecc_1bit" }, 383 { .int_msk = BIT(4), .msg = "qm_acc_get_task_timeout" }, 384 { .int_msk = BIT(5), .msg = "qm_acc_do_task_timeout" }, 385 { .int_msk = BIT(6), .msg = "qm_acc_wb_not_ready_timeout" }, 386 { .int_msk = BIT(7), .msg = "qm_sq_cq_vf_invalid" }, 387 { .int_msk = BIT(8), .msg = "qm_cq_vf_invalid" }, 388 { .int_msk = BIT(9), .msg = "qm_sq_vf_invalid" }, 389 { .int_msk = BIT(10), .msg = "qm_db_timeout" }, 390 { .int_msk = BIT(11), .msg = "qm_of_fifo_of" }, 391 { .int_msk = BIT(12), .msg = "qm_db_random_invalid" }, 392 { /* sentinel */ } 393 }; 394 395 static const char * const qm_db_timeout[] = { 396 "sq", "cq", "eq", "aeq", 397 }; 398 399 static const char * const qm_fifo_overflow[] = { 400 "cq", "eq", "aeq", 401 }; 402 403 static const char * const qm_s[] = { 404 "init", "start", "close", "stop", 405 }; 406 407 static const char * const qp_s[] = { 408 "none", "init", "start", "stop", "close", 409 }; 410 411 static bool qm_avail_state(struct hisi_qm *qm, enum qm_state new) 412 { 413 enum qm_state curr = atomic_read(&qm->status.flags); 414 bool avail = false; 415 416 switch (curr) { 417 case QM_INIT: 418 if (new == QM_START || new == QM_CLOSE) 419 avail = true; 420 break; 421 case QM_START: 422 if (new == QM_STOP) 423 avail = true; 424 break; 425 case QM_STOP: 426 if (new == QM_CLOSE || new == QM_START) 427 avail = true; 428 break; 429 default: 430 break; 431 } 432 433 dev_dbg(&qm->pdev->dev, "change qm state from %s to %s\n", 434 qm_s[curr], qm_s[new]); 435 436 if (!avail) 437 dev_warn(&qm->pdev->dev, "Can not change qm state from %s to %s\n", 438 qm_s[curr], qm_s[new]); 439 440 return avail; 441 } 442 443 static bool qm_qp_avail_state(struct hisi_qm *qm, struct hisi_qp *qp, 444 enum qp_state new) 445 { 446 enum qm_state qm_curr = atomic_read(&qm->status.flags); 447 enum qp_state qp_curr = 0; 448 bool avail = false; 449 450 if (qp) 451 qp_curr = atomic_read(&qp->qp_status.flags); 452 453 switch (new) { 454 case QP_INIT: 455 if (qm_curr == QM_START || qm_curr == QM_INIT) 456 avail = true; 457 break; 458 case QP_START: 459 if ((qm_curr == QM_START && qp_curr == QP_INIT) || 460 (qm_curr == QM_START && qp_curr == QP_STOP)) 461 avail = true; 462 break; 463 case QP_STOP: 464 if ((qm_curr == QM_START && qp_curr == QP_START) || 465 (qp_curr == QP_INIT)) 466 avail = true; 467 break; 468 case QP_CLOSE: 469 if ((qm_curr == QM_START && qp_curr == QP_INIT) || 470 (qm_curr == QM_START && qp_curr == QP_STOP) || 471 (qm_curr == QM_STOP && qp_curr == QP_STOP) || 472 (qm_curr == QM_STOP && qp_curr == QP_INIT)) 473 avail = true; 474 break; 475 default: 476 break; 477 } 478 479 dev_dbg(&qm->pdev->dev, "change qp state from %s to %s in QM %s\n", 480 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]); 481 482 if (!avail) 483 dev_warn(&qm->pdev->dev, 484 "Can not change qp state from %s to %s in QM %s\n", 485 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]); 486 487 return avail; 488 } 489 490 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */ 491 static int qm_wait_mb_ready(struct hisi_qm *qm) 492 { 493 u32 val; 494 495 return readl_relaxed_poll_timeout(qm->io_base + QM_MB_CMD_SEND_BASE, 496 val, !((val >> QM_MB_BUSY_SHIFT) & 497 0x1), POLL_PERIOD, POLL_TIMEOUT); 498 } 499 500 /* 128 bit should be written to hardware at one time to trigger a mailbox */ 501 static void qm_mb_write(struct hisi_qm *qm, const void *src) 502 { 503 void __iomem *fun_base = qm->io_base + QM_MB_CMD_SEND_BASE; 504 unsigned long tmp0 = 0, tmp1 = 0; 505 506 if (!IS_ENABLED(CONFIG_ARM64)) { 507 memcpy_toio(fun_base, src, 16); 508 wmb(); 509 return; 510 } 511 512 asm volatile("ldp %0, %1, %3\n" 513 "stp %0, %1, %2\n" 514 "dsb sy\n" 515 : "=&r" (tmp0), 516 "=&r" (tmp1), 517 "+Q" (*((char __iomem *)fun_base)) 518 : "Q" (*((char *)src)) 519 : "memory"); 520 } 521 522 static int qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue, 523 bool op) 524 { 525 struct qm_mailbox mailbox; 526 int ret = 0; 527 528 dev_dbg(&qm->pdev->dev, "QM mailbox request to q%u: %u-%llx\n", 529 queue, cmd, (unsigned long long)dma_addr); 530 531 mailbox.w0 = cpu_to_le16(cmd | 532 (op ? 0x1 << QM_MB_OP_SHIFT : 0) | 533 (0x1 << QM_MB_BUSY_SHIFT)); 534 mailbox.queue_num = cpu_to_le16(queue); 535 mailbox.base_l = cpu_to_le32(lower_32_bits(dma_addr)); 536 mailbox.base_h = cpu_to_le32(upper_32_bits(dma_addr)); 537 mailbox.rsvd = 0; 538 539 mutex_lock(&qm->mailbox_lock); 540 541 if (unlikely(qm_wait_mb_ready(qm))) { 542 ret = -EBUSY; 543 dev_err(&qm->pdev->dev, "QM mailbox is busy to start!\n"); 544 goto busy_unlock; 545 } 546 547 qm_mb_write(qm, &mailbox); 548 549 if (unlikely(qm_wait_mb_ready(qm))) { 550 ret = -EBUSY; 551 dev_err(&qm->pdev->dev, "QM mailbox operation timeout!\n"); 552 goto busy_unlock; 553 } 554 555 busy_unlock: 556 mutex_unlock(&qm->mailbox_lock); 557 558 if (ret) 559 atomic64_inc(&qm->debug.dfx.mb_err_cnt); 560 return ret; 561 } 562 563 static void qm_db_v1(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 564 { 565 u64 doorbell; 566 567 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V1) | 568 ((u64)index << QM_DB_INDEX_SHIFT_V1) | 569 ((u64)priority << QM_DB_PRIORITY_SHIFT_V1); 570 571 writeq(doorbell, qm->io_base + QM_DOORBELL_BASE_V1); 572 } 573 574 static void qm_db_v2(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 575 { 576 void __iomem *io_base = qm->io_base; 577 u16 randata = 0; 578 u64 doorbell; 579 580 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ) 581 io_base = qm->db_io_base + (u64)qn * qm->db_interval + 582 QM_DOORBELL_SQ_CQ_BASE_V2; 583 else 584 io_base += QM_DOORBELL_EQ_AEQ_BASE_V2; 585 586 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) | 587 ((u64)randata << QM_DB_RAND_SHIFT_V2) | 588 ((u64)index << QM_DB_INDEX_SHIFT_V2) | 589 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2); 590 591 writeq(doorbell, io_base); 592 } 593 594 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 595 { 596 dev_dbg(&qm->pdev->dev, "QM doorbell request: qn=%u, cmd=%u, index=%u\n", 597 qn, cmd, index); 598 599 qm->ops->qm_db(qm, qn, cmd, index, priority); 600 } 601 602 static int qm_dev_mem_reset(struct hisi_qm *qm) 603 { 604 u32 val; 605 606 writel(0x1, qm->io_base + QM_MEM_START_INIT); 607 return readl_relaxed_poll_timeout(qm->io_base + QM_MEM_INIT_DONE, val, 608 val & BIT(0), POLL_PERIOD, 609 POLL_TIMEOUT); 610 } 611 612 static u32 qm_get_irq_num_v1(struct hisi_qm *qm) 613 { 614 return QM_IRQ_NUM_V1; 615 } 616 617 static u32 qm_get_irq_num_v2(struct hisi_qm *qm) 618 { 619 if (qm->fun_type == QM_HW_PF) 620 return QM_IRQ_NUM_PF_V2; 621 else 622 return QM_IRQ_NUM_VF_V2; 623 } 624 625 static struct hisi_qp *qm_to_hisi_qp(struct hisi_qm *qm, struct qm_eqe *eqe) 626 { 627 u16 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK; 628 629 return &qm->qp_array[cqn]; 630 } 631 632 static void qm_cq_head_update(struct hisi_qp *qp) 633 { 634 if (qp->qp_status.cq_head == QM_Q_DEPTH - 1) { 635 qp->qp_status.cqc_phase = !qp->qp_status.cqc_phase; 636 qp->qp_status.cq_head = 0; 637 } else { 638 qp->qp_status.cq_head++; 639 } 640 } 641 642 static void qm_poll_qp(struct hisi_qp *qp, struct hisi_qm *qm) 643 { 644 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP)) 645 return; 646 647 if (qp->event_cb) { 648 qp->event_cb(qp); 649 return; 650 } 651 652 if (qp->req_cb) { 653 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head; 654 655 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) { 656 dma_rmb(); 657 qp->req_cb(qp, qp->sqe + qm->sqe_size * 658 le16_to_cpu(cqe->sq_head)); 659 qm_cq_head_update(qp); 660 cqe = qp->cqe + qp->qp_status.cq_head; 661 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, 662 qp->qp_status.cq_head, 0); 663 atomic_dec(&qp->qp_status.used); 664 } 665 666 /* set c_flag */ 667 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, 668 qp->qp_status.cq_head, 1); 669 } 670 } 671 672 static void qm_work_process(struct work_struct *work) 673 { 674 struct hisi_qm *qm = container_of(work, struct hisi_qm, work); 675 struct qm_eqe *eqe = qm->eqe + qm->status.eq_head; 676 struct hisi_qp *qp; 677 int eqe_num = 0; 678 679 while (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) { 680 eqe_num++; 681 qp = qm_to_hisi_qp(qm, eqe); 682 qm_poll_qp(qp, qm); 683 684 if (qm->status.eq_head == QM_EQ_DEPTH - 1) { 685 qm->status.eqc_phase = !qm->status.eqc_phase; 686 eqe = qm->eqe; 687 qm->status.eq_head = 0; 688 } else { 689 eqe++; 690 qm->status.eq_head++; 691 } 692 693 if (eqe_num == QM_EQ_DEPTH / 2 - 1) { 694 eqe_num = 0; 695 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 696 } 697 } 698 699 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 700 } 701 702 static irqreturn_t do_qm_irq(int irq, void *data) 703 { 704 struct hisi_qm *qm = (struct hisi_qm *)data; 705 706 /* the workqueue created by device driver of QM */ 707 if (qm->wq) 708 queue_work(qm->wq, &qm->work); 709 else 710 schedule_work(&qm->work); 711 712 return IRQ_HANDLED; 713 } 714 715 static irqreturn_t qm_irq(int irq, void *data) 716 { 717 struct hisi_qm *qm = data; 718 719 if (readl(qm->io_base + QM_VF_EQ_INT_SOURCE)) 720 return do_qm_irq(irq, data); 721 722 atomic64_inc(&qm->debug.dfx.err_irq_cnt); 723 dev_err(&qm->pdev->dev, "invalid int source\n"); 724 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 725 726 return IRQ_NONE; 727 } 728 729 static irqreturn_t qm_aeq_irq(int irq, void *data) 730 { 731 struct hisi_qm *qm = data; 732 struct qm_aeqe *aeqe = qm->aeqe + qm->status.aeq_head; 733 u32 type; 734 735 atomic64_inc(&qm->debug.dfx.aeq_irq_cnt); 736 if (!readl(qm->io_base + QM_VF_AEQ_INT_SOURCE)) 737 return IRQ_NONE; 738 739 while (QM_AEQE_PHASE(aeqe) == qm->status.aeqc_phase) { 740 type = le32_to_cpu(aeqe->dw0) >> QM_AEQE_TYPE_SHIFT; 741 if (type < ARRAY_SIZE(qm_fifo_overflow)) 742 dev_err(&qm->pdev->dev, "%s overflow\n", 743 qm_fifo_overflow[type]); 744 else 745 dev_err(&qm->pdev->dev, "unknown error type %u\n", 746 type); 747 748 if (qm->status.aeq_head == QM_Q_DEPTH - 1) { 749 qm->status.aeqc_phase = !qm->status.aeqc_phase; 750 aeqe = qm->aeqe; 751 qm->status.aeq_head = 0; 752 } else { 753 aeqe++; 754 qm->status.aeq_head++; 755 } 756 757 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0); 758 } 759 760 return IRQ_HANDLED; 761 } 762 763 static void qm_irq_unregister(struct hisi_qm *qm) 764 { 765 struct pci_dev *pdev = qm->pdev; 766 767 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm); 768 769 if (qm->ver == QM_HW_V1) 770 return; 771 772 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm); 773 774 if (qm->fun_type == QM_HW_PF) 775 free_irq(pci_irq_vector(pdev, 776 QM_ABNORMAL_EVENT_IRQ_VECTOR), qm); 777 } 778 779 static void qm_init_qp_status(struct hisi_qp *qp) 780 { 781 struct hisi_qp_status *qp_status = &qp->qp_status; 782 783 qp_status->sq_tail = 0; 784 qp_status->cq_head = 0; 785 qp_status->cqc_phase = true; 786 atomic_set(&qp_status->used, 0); 787 } 788 789 static void qm_vft_data_cfg(struct hisi_qm *qm, enum vft_type type, u32 base, 790 u32 number) 791 { 792 u64 tmp = 0; 793 794 if (number > 0) { 795 switch (type) { 796 case SQC_VFT: 797 if (qm->ver == QM_HW_V1) { 798 tmp = QM_SQC_VFT_BUF_SIZE | 799 QM_SQC_VFT_SQC_SIZE | 800 QM_SQC_VFT_INDEX_NUMBER | 801 QM_SQC_VFT_VALID | 802 (u64)base << QM_SQC_VFT_START_SQN_SHIFT; 803 } else { 804 tmp = (u64)base << QM_SQC_VFT_START_SQN_SHIFT | 805 QM_SQC_VFT_VALID | 806 (u64)(number - 1) << QM_SQC_VFT_SQN_SHIFT; 807 } 808 break; 809 case CQC_VFT: 810 if (qm->ver == QM_HW_V1) { 811 tmp = QM_CQC_VFT_BUF_SIZE | 812 QM_CQC_VFT_SQC_SIZE | 813 QM_CQC_VFT_INDEX_NUMBER | 814 QM_CQC_VFT_VALID; 815 } else { 816 tmp = QM_CQC_VFT_VALID; 817 } 818 break; 819 } 820 } 821 822 writel(lower_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_L); 823 writel(upper_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_H); 824 } 825 826 static int qm_set_vft_common(struct hisi_qm *qm, enum vft_type type, 827 u32 fun_num, u32 base, u32 number) 828 { 829 unsigned int val; 830 int ret; 831 832 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 833 val & BIT(0), POLL_PERIOD, 834 POLL_TIMEOUT); 835 if (ret) 836 return ret; 837 838 writel(0x0, qm->io_base + QM_VFT_CFG_OP_WR); 839 writel(type, qm->io_base + QM_VFT_CFG_TYPE); 840 writel(fun_num, qm->io_base + QM_VFT_CFG); 841 842 qm_vft_data_cfg(qm, type, base, number); 843 844 writel(0x0, qm->io_base + QM_VFT_CFG_RDY); 845 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE); 846 847 return readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 848 val & BIT(0), POLL_PERIOD, 849 POLL_TIMEOUT); 850 } 851 852 /* The config should be conducted after qm_dev_mem_reset() */ 853 static int qm_set_sqc_cqc_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 854 u32 number) 855 { 856 int ret, i; 857 858 for (i = SQC_VFT; i <= CQC_VFT; i++) { 859 ret = qm_set_vft_common(qm, i, fun_num, base, number); 860 if (ret) 861 return ret; 862 } 863 864 return 0; 865 } 866 867 static int qm_get_vft_v2(struct hisi_qm *qm, u32 *base, u32 *number) 868 { 869 u64 sqc_vft; 870 int ret; 871 872 ret = qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1); 873 if (ret) 874 return ret; 875 876 sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) | 877 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32); 878 *base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2); 879 *number = (QM_SQC_VFT_NUM_MASK_v2 & 880 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1; 881 882 return 0; 883 } 884 885 static int qm_get_vf_qp_num(struct hisi_qm *qm, u32 fun_num) 886 { 887 u32 remain_q_num, vfq_num; 888 u32 num_vfs = qm->vfs_num; 889 890 vfq_num = (qm->ctrl_qp_num - qm->qp_num) / num_vfs; 891 if (vfq_num >= qm->max_qp_num) 892 return qm->max_qp_num; 893 894 remain_q_num = (qm->ctrl_qp_num - qm->qp_num) % num_vfs; 895 if (vfq_num + remain_q_num <= qm->max_qp_num) 896 return fun_num == num_vfs ? vfq_num + remain_q_num : vfq_num; 897 898 /* 899 * if vfq_num + remain_q_num > max_qp_num, the last VFs, 900 * each with one more queue. 901 */ 902 return fun_num + remain_q_num > num_vfs ? vfq_num + 1 : vfq_num; 903 } 904 905 static struct hisi_qm *file_to_qm(struct debugfs_file *file) 906 { 907 struct qm_debug *debug = file->debug; 908 909 return container_of(debug, struct hisi_qm, debug); 910 } 911 912 static u32 current_q_read(struct debugfs_file *file) 913 { 914 struct hisi_qm *qm = file_to_qm(file); 915 916 return readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) >> QM_DFX_QN_SHIFT; 917 } 918 919 static int current_q_write(struct debugfs_file *file, u32 val) 920 { 921 struct hisi_qm *qm = file_to_qm(file); 922 u32 tmp; 923 924 if (val >= qm->debug.curr_qm_qp_num) 925 return -EINVAL; 926 927 tmp = val << QM_DFX_QN_SHIFT | 928 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_FUN_MASK); 929 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 930 931 tmp = val << QM_DFX_QN_SHIFT | 932 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_FUN_MASK); 933 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 934 935 return 0; 936 } 937 938 static u32 clear_enable_read(struct debugfs_file *file) 939 { 940 struct hisi_qm *qm = file_to_qm(file); 941 942 return readl(qm->io_base + QM_DFX_CNT_CLR_CE); 943 } 944 945 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */ 946 static int clear_enable_write(struct debugfs_file *file, u32 rd_clr_ctrl) 947 { 948 struct hisi_qm *qm = file_to_qm(file); 949 950 if (rd_clr_ctrl > 1) 951 return -EINVAL; 952 953 writel(rd_clr_ctrl, qm->io_base + QM_DFX_CNT_CLR_CE); 954 955 return 0; 956 } 957 958 static u32 current_qm_read(struct debugfs_file *file) 959 { 960 struct hisi_qm *qm = file_to_qm(file); 961 962 return readl(qm->io_base + QM_DFX_MB_CNT_VF); 963 } 964 965 static int current_qm_write(struct debugfs_file *file, u32 val) 966 { 967 struct hisi_qm *qm = file_to_qm(file); 968 u32 tmp; 969 970 if (val > qm->vfs_num) 971 return -EINVAL; 972 973 /* According PF or VF Dev ID to calculation curr_qm_qp_num and store */ 974 if (!val) 975 qm->debug.curr_qm_qp_num = qm->qp_num; 976 else 977 qm->debug.curr_qm_qp_num = qm_get_vf_qp_num(qm, val); 978 979 writel(val, qm->io_base + QM_DFX_MB_CNT_VF); 980 writel(val, qm->io_base + QM_DFX_DB_CNT_VF); 981 982 tmp = val | 983 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK); 984 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 985 986 tmp = val | 987 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK); 988 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 989 990 return 0; 991 } 992 993 static ssize_t qm_debug_read(struct file *filp, char __user *buf, 994 size_t count, loff_t *pos) 995 { 996 struct debugfs_file *file = filp->private_data; 997 enum qm_debug_file index = file->index; 998 char tbuf[QM_DBG_TMP_BUF_LEN]; 999 u32 val; 1000 int ret; 1001 1002 mutex_lock(&file->lock); 1003 switch (index) { 1004 case CURRENT_QM: 1005 val = current_qm_read(file); 1006 break; 1007 case CURRENT_Q: 1008 val = current_q_read(file); 1009 break; 1010 case CLEAR_ENABLE: 1011 val = clear_enable_read(file); 1012 break; 1013 default: 1014 mutex_unlock(&file->lock); 1015 return -EINVAL; 1016 } 1017 mutex_unlock(&file->lock); 1018 1019 ret = scnprintf(tbuf, QM_DBG_TMP_BUF_LEN, "%u\n", val); 1020 return simple_read_from_buffer(buf, count, pos, tbuf, ret); 1021 } 1022 1023 static ssize_t qm_debug_write(struct file *filp, const char __user *buf, 1024 size_t count, loff_t *pos) 1025 { 1026 struct debugfs_file *file = filp->private_data; 1027 enum qm_debug_file index = file->index; 1028 unsigned long val; 1029 char tbuf[QM_DBG_TMP_BUF_LEN]; 1030 int len, ret; 1031 1032 if (*pos != 0) 1033 return 0; 1034 1035 if (count >= QM_DBG_TMP_BUF_LEN) 1036 return -ENOSPC; 1037 1038 len = simple_write_to_buffer(tbuf, QM_DBG_TMP_BUF_LEN - 1, pos, buf, 1039 count); 1040 if (len < 0) 1041 return len; 1042 1043 tbuf[len] = '\0'; 1044 if (kstrtoul(tbuf, 0, &val)) 1045 return -EFAULT; 1046 1047 mutex_lock(&file->lock); 1048 switch (index) { 1049 case CURRENT_QM: 1050 ret = current_qm_write(file, val); 1051 break; 1052 case CURRENT_Q: 1053 ret = current_q_write(file, val); 1054 break; 1055 case CLEAR_ENABLE: 1056 ret = clear_enable_write(file, val); 1057 break; 1058 default: 1059 ret = -EINVAL; 1060 } 1061 mutex_unlock(&file->lock); 1062 1063 if (ret) 1064 return ret; 1065 1066 return count; 1067 } 1068 1069 static const struct file_operations qm_debug_fops = { 1070 .owner = THIS_MODULE, 1071 .open = simple_open, 1072 .read = qm_debug_read, 1073 .write = qm_debug_write, 1074 }; 1075 1076 struct qm_dfx_registers { 1077 char *reg_name; 1078 u64 reg_offset; 1079 }; 1080 1081 #define CNT_CYC_REGS_NUM 10 1082 static struct qm_dfx_registers qm_dfx_regs[] = { 1083 /* XXX_CNT are reading clear register */ 1084 {"QM_ECC_1BIT_CNT ", 0x104000ull}, 1085 {"QM_ECC_MBIT_CNT ", 0x104008ull}, 1086 {"QM_DFX_MB_CNT ", 0x104018ull}, 1087 {"QM_DFX_DB_CNT ", 0x104028ull}, 1088 {"QM_DFX_SQE_CNT ", 0x104038ull}, 1089 {"QM_DFX_CQE_CNT ", 0x104048ull}, 1090 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050ull}, 1091 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058ull}, 1092 {"QM_DFX_ACC_FINISH_CNT ", 0x104060ull}, 1093 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4ull}, 1094 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull}, 1095 {"QM_ECC_1BIT_INF ", 0x104004ull}, 1096 {"QM_ECC_MBIT_INF ", 0x10400cull}, 1097 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0ull}, 1098 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4ull}, 1099 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8ull}, 1100 {"QM_DFX_FF_ST0 ", 0x1040c8ull}, 1101 {"QM_DFX_FF_ST1 ", 0x1040ccull}, 1102 {"QM_DFX_FF_ST2 ", 0x1040d0ull}, 1103 {"QM_DFX_FF_ST3 ", 0x1040d4ull}, 1104 {"QM_DFX_FF_ST4 ", 0x1040d8ull}, 1105 {"QM_DFX_FF_ST5 ", 0x1040dcull}, 1106 {"QM_DFX_FF_ST6 ", 0x1040e0ull}, 1107 {"QM_IN_IDLE_ST ", 0x1040e4ull}, 1108 { NULL, 0} 1109 }; 1110 1111 static struct qm_dfx_registers qm_vf_dfx_regs[] = { 1112 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull}, 1113 { NULL, 0} 1114 }; 1115 1116 static int qm_regs_show(struct seq_file *s, void *unused) 1117 { 1118 struct hisi_qm *qm = s->private; 1119 struct qm_dfx_registers *regs; 1120 u32 val; 1121 1122 if (qm->fun_type == QM_HW_PF) 1123 regs = qm_dfx_regs; 1124 else 1125 regs = qm_vf_dfx_regs; 1126 1127 while (regs->reg_name) { 1128 val = readl(qm->io_base + regs->reg_offset); 1129 seq_printf(s, "%s= 0x%08x\n", regs->reg_name, val); 1130 regs++; 1131 } 1132 1133 return 0; 1134 } 1135 1136 DEFINE_SHOW_ATTRIBUTE(qm_regs); 1137 1138 static ssize_t qm_cmd_read(struct file *filp, char __user *buffer, 1139 size_t count, loff_t *pos) 1140 { 1141 char buf[QM_DBG_READ_LEN]; 1142 int len; 1143 1144 len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n", 1145 "Please echo help to cmd to get help information"); 1146 1147 return simple_read_from_buffer(buffer, count, pos, buf, len); 1148 } 1149 1150 static void *qm_ctx_alloc(struct hisi_qm *qm, size_t ctx_size, 1151 dma_addr_t *dma_addr) 1152 { 1153 struct device *dev = &qm->pdev->dev; 1154 void *ctx_addr; 1155 1156 ctx_addr = kzalloc(ctx_size, GFP_KERNEL); 1157 if (!ctx_addr) 1158 return ERR_PTR(-ENOMEM); 1159 1160 *dma_addr = dma_map_single(dev, ctx_addr, ctx_size, DMA_FROM_DEVICE); 1161 if (dma_mapping_error(dev, *dma_addr)) { 1162 dev_err(dev, "DMA mapping error!\n"); 1163 kfree(ctx_addr); 1164 return ERR_PTR(-ENOMEM); 1165 } 1166 1167 return ctx_addr; 1168 } 1169 1170 static void qm_ctx_free(struct hisi_qm *qm, size_t ctx_size, 1171 const void *ctx_addr, dma_addr_t *dma_addr) 1172 { 1173 struct device *dev = &qm->pdev->dev; 1174 1175 dma_unmap_single(dev, *dma_addr, ctx_size, DMA_FROM_DEVICE); 1176 kfree(ctx_addr); 1177 } 1178 1179 static int dump_show(struct hisi_qm *qm, void *info, 1180 unsigned int info_size, char *info_name) 1181 { 1182 struct device *dev = &qm->pdev->dev; 1183 u8 *info_buf, *info_curr = info; 1184 u32 i; 1185 #define BYTE_PER_DW 4 1186 1187 info_buf = kzalloc(info_size, GFP_KERNEL); 1188 if (!info_buf) 1189 return -ENOMEM; 1190 1191 for (i = 0; i < info_size; i++, info_curr++) { 1192 if (i % BYTE_PER_DW == 0) 1193 info_buf[i + 3UL] = *info_curr; 1194 else if (i % BYTE_PER_DW == 1) 1195 info_buf[i + 1UL] = *info_curr; 1196 else if (i % BYTE_PER_DW == 2) 1197 info_buf[i - 1] = *info_curr; 1198 else if (i % BYTE_PER_DW == 3) 1199 info_buf[i - 3] = *info_curr; 1200 } 1201 1202 dev_info(dev, "%s DUMP\n", info_name); 1203 for (i = 0; i < info_size; i += BYTE_PER_DW) { 1204 pr_info("DW%u: %02X%02X %02X%02X\n", i / BYTE_PER_DW, 1205 info_buf[i], info_buf[i + 1UL], 1206 info_buf[i + 2UL], info_buf[i + 3UL]); 1207 } 1208 1209 kfree(info_buf); 1210 1211 return 0; 1212 } 1213 1214 static int qm_dump_sqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id) 1215 { 1216 return qm_mb(qm, QM_MB_CMD_SQC, dma_addr, qp_id, 1); 1217 } 1218 1219 static int qm_dump_cqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id) 1220 { 1221 return qm_mb(qm, QM_MB_CMD_CQC, dma_addr, qp_id, 1); 1222 } 1223 1224 static int qm_sqc_dump(struct hisi_qm *qm, const char *s) 1225 { 1226 struct device *dev = &qm->pdev->dev; 1227 struct qm_sqc *sqc, *sqc_curr; 1228 dma_addr_t sqc_dma; 1229 u32 qp_id; 1230 int ret; 1231 1232 if (!s) 1233 return -EINVAL; 1234 1235 ret = kstrtou32(s, 0, &qp_id); 1236 if (ret || qp_id >= qm->qp_num) { 1237 dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1); 1238 return -EINVAL; 1239 } 1240 1241 sqc = qm_ctx_alloc(qm, sizeof(*sqc), &sqc_dma); 1242 if (IS_ERR(sqc)) 1243 return PTR_ERR(sqc); 1244 1245 ret = qm_dump_sqc_raw(qm, sqc_dma, qp_id); 1246 if (ret) { 1247 down_read(&qm->qps_lock); 1248 if (qm->sqc) { 1249 sqc_curr = qm->sqc + qp_id; 1250 1251 ret = dump_show(qm, sqc_curr, sizeof(*sqc), 1252 "SOFT SQC"); 1253 if (ret) 1254 dev_info(dev, "Show soft sqc failed!\n"); 1255 } 1256 up_read(&qm->qps_lock); 1257 1258 goto err_free_ctx; 1259 } 1260 1261 ret = dump_show(qm, sqc, sizeof(*sqc), "SQC"); 1262 if (ret) 1263 dev_info(dev, "Show hw sqc failed!\n"); 1264 1265 err_free_ctx: 1266 qm_ctx_free(qm, sizeof(*sqc), sqc, &sqc_dma); 1267 return ret; 1268 } 1269 1270 static int qm_cqc_dump(struct hisi_qm *qm, const char *s) 1271 { 1272 struct device *dev = &qm->pdev->dev; 1273 struct qm_cqc *cqc, *cqc_curr; 1274 dma_addr_t cqc_dma; 1275 u32 qp_id; 1276 int ret; 1277 1278 if (!s) 1279 return -EINVAL; 1280 1281 ret = kstrtou32(s, 0, &qp_id); 1282 if (ret || qp_id >= qm->qp_num) { 1283 dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1); 1284 return -EINVAL; 1285 } 1286 1287 cqc = qm_ctx_alloc(qm, sizeof(*cqc), &cqc_dma); 1288 if (IS_ERR(cqc)) 1289 return PTR_ERR(cqc); 1290 1291 ret = qm_dump_cqc_raw(qm, cqc_dma, qp_id); 1292 if (ret) { 1293 down_read(&qm->qps_lock); 1294 if (qm->cqc) { 1295 cqc_curr = qm->cqc + qp_id; 1296 1297 ret = dump_show(qm, cqc_curr, sizeof(*cqc), 1298 "SOFT CQC"); 1299 if (ret) 1300 dev_info(dev, "Show soft cqc failed!\n"); 1301 } 1302 up_read(&qm->qps_lock); 1303 1304 goto err_free_ctx; 1305 } 1306 1307 ret = dump_show(qm, cqc, sizeof(*cqc), "CQC"); 1308 if (ret) 1309 dev_info(dev, "Show hw cqc failed!\n"); 1310 1311 err_free_ctx: 1312 qm_ctx_free(qm, sizeof(*cqc), cqc, &cqc_dma); 1313 return ret; 1314 } 1315 1316 static int qm_eqc_aeqc_dump(struct hisi_qm *qm, char *s, size_t size, 1317 int cmd, char *name) 1318 { 1319 struct device *dev = &qm->pdev->dev; 1320 dma_addr_t xeqc_dma; 1321 void *xeqc; 1322 int ret; 1323 1324 if (strsep(&s, " ")) { 1325 dev_err(dev, "Please do not input extra characters!\n"); 1326 return -EINVAL; 1327 } 1328 1329 xeqc = qm_ctx_alloc(qm, size, &xeqc_dma); 1330 if (IS_ERR(xeqc)) 1331 return PTR_ERR(xeqc); 1332 1333 ret = qm_mb(qm, cmd, xeqc_dma, 0, 1); 1334 if (ret) 1335 goto err_free_ctx; 1336 1337 ret = dump_show(qm, xeqc, size, name); 1338 if (ret) 1339 dev_info(dev, "Show hw %s failed!\n", name); 1340 1341 err_free_ctx: 1342 qm_ctx_free(qm, size, xeqc, &xeqc_dma); 1343 return ret; 1344 } 1345 1346 static int q_dump_param_parse(struct hisi_qm *qm, char *s, 1347 u32 *e_id, u32 *q_id) 1348 { 1349 struct device *dev = &qm->pdev->dev; 1350 unsigned int qp_num = qm->qp_num; 1351 char *presult; 1352 int ret; 1353 1354 presult = strsep(&s, " "); 1355 if (!presult) { 1356 dev_err(dev, "Please input qp number!\n"); 1357 return -EINVAL; 1358 } 1359 1360 ret = kstrtou32(presult, 0, q_id); 1361 if (ret || *q_id >= qp_num) { 1362 dev_err(dev, "Please input qp num (0-%u)", qp_num - 1); 1363 return -EINVAL; 1364 } 1365 1366 presult = strsep(&s, " "); 1367 if (!presult) { 1368 dev_err(dev, "Please input sqe number!\n"); 1369 return -EINVAL; 1370 } 1371 1372 ret = kstrtou32(presult, 0, e_id); 1373 if (ret || *e_id >= QM_Q_DEPTH) { 1374 dev_err(dev, "Please input sqe num (0-%d)", QM_Q_DEPTH - 1); 1375 return -EINVAL; 1376 } 1377 1378 if (strsep(&s, " ")) { 1379 dev_err(dev, "Please do not input extra characters!\n"); 1380 return -EINVAL; 1381 } 1382 1383 return 0; 1384 } 1385 1386 static int qm_sq_dump(struct hisi_qm *qm, char *s) 1387 { 1388 struct device *dev = &qm->pdev->dev; 1389 void *sqe, *sqe_curr; 1390 struct hisi_qp *qp; 1391 u32 qp_id, sqe_id; 1392 int ret; 1393 1394 ret = q_dump_param_parse(qm, s, &sqe_id, &qp_id); 1395 if (ret) 1396 return ret; 1397 1398 sqe = kzalloc(qm->sqe_size * QM_Q_DEPTH, GFP_KERNEL); 1399 if (!sqe) 1400 return -ENOMEM; 1401 1402 qp = &qm->qp_array[qp_id]; 1403 memcpy(sqe, qp->sqe, qm->sqe_size * QM_Q_DEPTH); 1404 sqe_curr = sqe + (u32)(sqe_id * qm->sqe_size); 1405 memset(sqe_curr + qm->debug.sqe_mask_offset, QM_SQE_ADDR_MASK, 1406 qm->debug.sqe_mask_len); 1407 1408 ret = dump_show(qm, sqe_curr, qm->sqe_size, "SQE"); 1409 if (ret) 1410 dev_info(dev, "Show sqe failed!\n"); 1411 1412 kfree(sqe); 1413 1414 return ret; 1415 } 1416 1417 static int qm_cq_dump(struct hisi_qm *qm, char *s) 1418 { 1419 struct device *dev = &qm->pdev->dev; 1420 struct qm_cqe *cqe_curr; 1421 struct hisi_qp *qp; 1422 u32 qp_id, cqe_id; 1423 int ret; 1424 1425 ret = q_dump_param_parse(qm, s, &cqe_id, &qp_id); 1426 if (ret) 1427 return ret; 1428 1429 qp = &qm->qp_array[qp_id]; 1430 cqe_curr = qp->cqe + cqe_id; 1431 ret = dump_show(qm, cqe_curr, sizeof(struct qm_cqe), "CQE"); 1432 if (ret) 1433 dev_info(dev, "Show cqe failed!\n"); 1434 1435 return ret; 1436 } 1437 1438 static int qm_eq_aeq_dump(struct hisi_qm *qm, const char *s, 1439 size_t size, char *name) 1440 { 1441 struct device *dev = &qm->pdev->dev; 1442 void *xeqe; 1443 u32 xeqe_id; 1444 int ret; 1445 1446 if (!s) 1447 return -EINVAL; 1448 1449 ret = kstrtou32(s, 0, &xeqe_id); 1450 if (ret) 1451 return -EINVAL; 1452 1453 if (!strcmp(name, "EQE") && xeqe_id >= QM_EQ_DEPTH) { 1454 dev_err(dev, "Please input eqe num (0-%d)", QM_EQ_DEPTH - 1); 1455 return -EINVAL; 1456 } else if (!strcmp(name, "AEQE") && xeqe_id >= QM_Q_DEPTH) { 1457 dev_err(dev, "Please input aeqe num (0-%d)", QM_Q_DEPTH - 1); 1458 return -EINVAL; 1459 } 1460 1461 down_read(&qm->qps_lock); 1462 1463 if (qm->eqe && !strcmp(name, "EQE")) { 1464 xeqe = qm->eqe + xeqe_id; 1465 } else if (qm->aeqe && !strcmp(name, "AEQE")) { 1466 xeqe = qm->aeqe + xeqe_id; 1467 } else { 1468 ret = -EINVAL; 1469 goto err_unlock; 1470 } 1471 1472 ret = dump_show(qm, xeqe, size, name); 1473 if (ret) 1474 dev_info(dev, "Show %s failed!\n", name); 1475 1476 err_unlock: 1477 up_read(&qm->qps_lock); 1478 return ret; 1479 } 1480 1481 static int qm_dbg_help(struct hisi_qm *qm, char *s) 1482 { 1483 struct device *dev = &qm->pdev->dev; 1484 1485 if (strsep(&s, " ")) { 1486 dev_err(dev, "Please do not input extra characters!\n"); 1487 return -EINVAL; 1488 } 1489 1490 dev_info(dev, "available commands:\n"); 1491 dev_info(dev, "sqc <num>\n"); 1492 dev_info(dev, "cqc <num>\n"); 1493 dev_info(dev, "eqc\n"); 1494 dev_info(dev, "aeqc\n"); 1495 dev_info(dev, "sq <num> <e>\n"); 1496 dev_info(dev, "cq <num> <e>\n"); 1497 dev_info(dev, "eq <e>\n"); 1498 dev_info(dev, "aeq <e>\n"); 1499 1500 return 0; 1501 } 1502 1503 static int qm_cmd_write_dump(struct hisi_qm *qm, const char *cmd_buf) 1504 { 1505 struct device *dev = &qm->pdev->dev; 1506 char *presult, *s, *s_tmp; 1507 int ret; 1508 1509 s = kstrdup(cmd_buf, GFP_KERNEL); 1510 if (!s) 1511 return -ENOMEM; 1512 1513 s_tmp = s; 1514 presult = strsep(&s, " "); 1515 if (!presult) { 1516 ret = -EINVAL; 1517 goto err_buffer_free; 1518 } 1519 1520 if (!strcmp(presult, "sqc")) 1521 ret = qm_sqc_dump(qm, s); 1522 else if (!strcmp(presult, "cqc")) 1523 ret = qm_cqc_dump(qm, s); 1524 else if (!strcmp(presult, "eqc")) 1525 ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_eqc), 1526 QM_MB_CMD_EQC, "EQC"); 1527 else if (!strcmp(presult, "aeqc")) 1528 ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_aeqc), 1529 QM_MB_CMD_AEQC, "AEQC"); 1530 else if (!strcmp(presult, "sq")) 1531 ret = qm_sq_dump(qm, s); 1532 else if (!strcmp(presult, "cq")) 1533 ret = qm_cq_dump(qm, s); 1534 else if (!strcmp(presult, "eq")) 1535 ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_eqe), "EQE"); 1536 else if (!strcmp(presult, "aeq")) 1537 ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_aeqe), "AEQE"); 1538 else if (!strcmp(presult, "help")) 1539 ret = qm_dbg_help(qm, s); 1540 else 1541 ret = -EINVAL; 1542 1543 if (ret) 1544 dev_info(dev, "Please echo help\n"); 1545 1546 err_buffer_free: 1547 kfree(s_tmp); 1548 1549 return ret; 1550 } 1551 1552 static ssize_t qm_cmd_write(struct file *filp, const char __user *buffer, 1553 size_t count, loff_t *pos) 1554 { 1555 struct hisi_qm *qm = filp->private_data; 1556 char *cmd_buf, *cmd_buf_tmp; 1557 int ret; 1558 1559 if (*pos) 1560 return 0; 1561 1562 /* Judge if the instance is being reset. */ 1563 if (unlikely(atomic_read(&qm->status.flags) == QM_STOP)) 1564 return 0; 1565 1566 if (count > QM_DBG_WRITE_LEN) 1567 return -ENOSPC; 1568 1569 cmd_buf = kzalloc(count + 1, GFP_KERNEL); 1570 if (!cmd_buf) 1571 return -ENOMEM; 1572 1573 if (copy_from_user(cmd_buf, buffer, count)) { 1574 kfree(cmd_buf); 1575 return -EFAULT; 1576 } 1577 1578 cmd_buf[count] = '\0'; 1579 1580 cmd_buf_tmp = strchr(cmd_buf, '\n'); 1581 if (cmd_buf_tmp) { 1582 *cmd_buf_tmp = '\0'; 1583 count = cmd_buf_tmp - cmd_buf + 1; 1584 } 1585 1586 ret = qm_cmd_write_dump(qm, cmd_buf); 1587 if (ret) { 1588 kfree(cmd_buf); 1589 return ret; 1590 } 1591 1592 kfree(cmd_buf); 1593 1594 return count; 1595 } 1596 1597 static const struct file_operations qm_cmd_fops = { 1598 .owner = THIS_MODULE, 1599 .open = simple_open, 1600 .read = qm_cmd_read, 1601 .write = qm_cmd_write, 1602 }; 1603 1604 static void qm_create_debugfs_file(struct hisi_qm *qm, struct dentry *dir, 1605 enum qm_debug_file index) 1606 { 1607 struct debugfs_file *file = qm->debug.files + index; 1608 1609 debugfs_create_file(qm_debug_file_name[index], 0600, dir, file, 1610 &qm_debug_fops); 1611 1612 file->index = index; 1613 mutex_init(&file->lock); 1614 file->debug = &qm->debug; 1615 } 1616 1617 static void qm_hw_error_init_v1(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe) 1618 { 1619 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK); 1620 } 1621 1622 static void qm_hw_error_init_v2(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe) 1623 { 1624 u32 irq_enable = ce | nfe | fe; 1625 u32 irq_unmask = ~irq_enable; 1626 1627 qm->error_mask = ce | nfe | fe; 1628 1629 /* clear QM hw residual error source */ 1630 writel(QM_ABNORMAL_INT_SOURCE_CLR, 1631 qm->io_base + QM_ABNORMAL_INT_SOURCE); 1632 1633 /* configure error type */ 1634 writel(ce, qm->io_base + QM_RAS_CE_ENABLE); 1635 writel(QM_RAS_CE_TIMES_PER_IRQ, qm->io_base + QM_RAS_CE_THRESHOLD); 1636 writel(nfe, qm->io_base + QM_RAS_NFE_ENABLE); 1637 writel(fe, qm->io_base + QM_RAS_FE_ENABLE); 1638 1639 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1640 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK); 1641 } 1642 1643 static void qm_hw_error_uninit_v2(struct hisi_qm *qm) 1644 { 1645 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK); 1646 } 1647 1648 static void qm_log_hw_error(struct hisi_qm *qm, u32 error_status) 1649 { 1650 const struct hisi_qm_hw_error *err; 1651 struct device *dev = &qm->pdev->dev; 1652 u32 reg_val, type, vf_num; 1653 int i; 1654 1655 for (i = 0; i < ARRAY_SIZE(qm_hw_error); i++) { 1656 err = &qm_hw_error[i]; 1657 if (!(err->int_msk & error_status)) 1658 continue; 1659 1660 dev_err(dev, "%s [error status=0x%x] found\n", 1661 err->msg, err->int_msk); 1662 1663 if (err->int_msk & QM_DB_TIMEOUT) { 1664 reg_val = readl(qm->io_base + QM_ABNORMAL_INF01); 1665 type = (reg_val & QM_DB_TIMEOUT_TYPE) >> 1666 QM_DB_TIMEOUT_TYPE_SHIFT; 1667 vf_num = reg_val & QM_DB_TIMEOUT_VF; 1668 dev_err(dev, "qm %s doorbell timeout in function %u\n", 1669 qm_db_timeout[type], vf_num); 1670 } else if (err->int_msk & QM_OF_FIFO_OF) { 1671 reg_val = readl(qm->io_base + QM_ABNORMAL_INF00); 1672 type = (reg_val & QM_FIFO_OVERFLOW_TYPE) >> 1673 QM_FIFO_OVERFLOW_TYPE_SHIFT; 1674 vf_num = reg_val & QM_FIFO_OVERFLOW_VF; 1675 1676 if (type < ARRAY_SIZE(qm_fifo_overflow)) 1677 dev_err(dev, "qm %s fifo overflow in function %u\n", 1678 qm_fifo_overflow[type], vf_num); 1679 else 1680 dev_err(dev, "unknown error type\n"); 1681 } 1682 } 1683 } 1684 1685 static enum acc_err_result qm_hw_error_handle_v2(struct hisi_qm *qm) 1686 { 1687 u32 error_status, tmp, val; 1688 1689 /* read err sts */ 1690 tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 1691 error_status = qm->error_mask & tmp; 1692 1693 if (error_status) { 1694 if (error_status & QM_ECC_MBIT) 1695 qm->err_status.is_qm_ecc_mbit = true; 1696 1697 qm_log_hw_error(qm, error_status); 1698 val = error_status | QM_DB_RANDOM_INVALID | QM_BASE_CE; 1699 /* ce error does not need to be reset */ 1700 if (val == (QM_DB_RANDOM_INVALID | QM_BASE_CE)) { 1701 writel(error_status, qm->io_base + 1702 QM_ABNORMAL_INT_SOURCE); 1703 writel(qm->err_ini->err_info.nfe, 1704 qm->io_base + QM_RAS_NFE_ENABLE); 1705 return ACC_ERR_RECOVERED; 1706 } 1707 1708 return ACC_ERR_NEED_RESET; 1709 } 1710 1711 return ACC_ERR_RECOVERED; 1712 } 1713 1714 static const struct hisi_qm_hw_ops qm_hw_ops_v1 = { 1715 .qm_db = qm_db_v1, 1716 .get_irq_num = qm_get_irq_num_v1, 1717 .hw_error_init = qm_hw_error_init_v1, 1718 }; 1719 1720 static const struct hisi_qm_hw_ops qm_hw_ops_v2 = { 1721 .get_vft = qm_get_vft_v2, 1722 .qm_db = qm_db_v2, 1723 .get_irq_num = qm_get_irq_num_v2, 1724 .hw_error_init = qm_hw_error_init_v2, 1725 .hw_error_uninit = qm_hw_error_uninit_v2, 1726 .hw_error_handle = qm_hw_error_handle_v2, 1727 }; 1728 1729 static void *qm_get_avail_sqe(struct hisi_qp *qp) 1730 { 1731 struct hisi_qp_status *qp_status = &qp->qp_status; 1732 u16 sq_tail = qp_status->sq_tail; 1733 1734 if (unlikely(atomic_read(&qp->qp_status.used) == QM_Q_DEPTH - 1)) 1735 return NULL; 1736 1737 return qp->sqe + sq_tail * qp->qm->sqe_size; 1738 } 1739 1740 static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type) 1741 { 1742 struct device *dev = &qm->pdev->dev; 1743 struct hisi_qp *qp; 1744 int qp_id; 1745 1746 if (!qm_qp_avail_state(qm, NULL, QP_INIT)) 1747 return ERR_PTR(-EPERM); 1748 1749 if (qm->qp_in_used == qm->qp_num) { 1750 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n", 1751 qm->qp_num); 1752 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt); 1753 return ERR_PTR(-EBUSY); 1754 } 1755 1756 qp_id = idr_alloc_cyclic(&qm->qp_idr, NULL, 0, qm->qp_num, GFP_ATOMIC); 1757 if (qp_id < 0) { 1758 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n", 1759 qm->qp_num); 1760 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt); 1761 return ERR_PTR(-EBUSY); 1762 } 1763 1764 qp = &qm->qp_array[qp_id]; 1765 1766 memset(qp->cqe, 0, sizeof(struct qm_cqe) * QM_Q_DEPTH); 1767 1768 qp->event_cb = NULL; 1769 qp->req_cb = NULL; 1770 qp->qp_id = qp_id; 1771 qp->alg_type = alg_type; 1772 qp->is_in_kernel = true; 1773 qm->qp_in_used++; 1774 atomic_set(&qp->qp_status.flags, QP_INIT); 1775 1776 return qp; 1777 } 1778 1779 /** 1780 * hisi_qm_create_qp() - Create a queue pair from qm. 1781 * @qm: The qm we create a qp from. 1782 * @alg_type: Accelerator specific algorithm type in sqc. 1783 * 1784 * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating 1785 * qp memory fails. 1786 */ 1787 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type) 1788 { 1789 struct hisi_qp *qp; 1790 1791 down_write(&qm->qps_lock); 1792 qp = qm_create_qp_nolock(qm, alg_type); 1793 up_write(&qm->qps_lock); 1794 1795 return qp; 1796 } 1797 EXPORT_SYMBOL_GPL(hisi_qm_create_qp); 1798 1799 /** 1800 * hisi_qm_release_qp() - Release a qp back to its qm. 1801 * @qp: The qp we want to release. 1802 * 1803 * This function releases the resource of a qp. 1804 */ 1805 void hisi_qm_release_qp(struct hisi_qp *qp) 1806 { 1807 struct hisi_qm *qm = qp->qm; 1808 1809 down_write(&qm->qps_lock); 1810 1811 if (!qm_qp_avail_state(qm, qp, QP_CLOSE)) { 1812 up_write(&qm->qps_lock); 1813 return; 1814 } 1815 1816 qm->qp_in_used--; 1817 idr_remove(&qm->qp_idr, qp->qp_id); 1818 1819 up_write(&qm->qps_lock); 1820 } 1821 EXPORT_SYMBOL_GPL(hisi_qm_release_qp); 1822 1823 static int qm_sq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 1824 { 1825 struct hisi_qm *qm = qp->qm; 1826 struct device *dev = &qm->pdev->dev; 1827 enum qm_hw_ver ver = qm->ver; 1828 struct qm_sqc *sqc; 1829 dma_addr_t sqc_dma; 1830 int ret; 1831 1832 sqc = kzalloc(sizeof(struct qm_sqc), GFP_KERNEL); 1833 if (!sqc) 1834 return -ENOMEM; 1835 1836 INIT_QC_COMMON(sqc, qp->sqe_dma, pasid); 1837 if (ver == QM_HW_V1) { 1838 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm->sqe_size)); 1839 sqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1); 1840 } else { 1841 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V2(qm->sqe_size)); 1842 sqc->w8 = 0; /* rand_qc */ 1843 } 1844 sqc->cq_num = cpu_to_le16(qp_id); 1845 sqc->w13 = cpu_to_le16(QM_MK_SQC_W13(0, 1, qp->alg_type)); 1846 1847 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel) 1848 sqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE << 1849 QM_QC_PASID_ENABLE_SHIFT); 1850 1851 sqc_dma = dma_map_single(dev, sqc, sizeof(struct qm_sqc), 1852 DMA_TO_DEVICE); 1853 if (dma_mapping_error(dev, sqc_dma)) { 1854 kfree(sqc); 1855 return -ENOMEM; 1856 } 1857 1858 ret = qm_mb(qm, QM_MB_CMD_SQC, sqc_dma, qp_id, 0); 1859 dma_unmap_single(dev, sqc_dma, sizeof(struct qm_sqc), DMA_TO_DEVICE); 1860 kfree(sqc); 1861 1862 return ret; 1863 } 1864 1865 static int qm_cq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 1866 { 1867 struct hisi_qm *qm = qp->qm; 1868 struct device *dev = &qm->pdev->dev; 1869 enum qm_hw_ver ver = qm->ver; 1870 struct qm_cqc *cqc; 1871 dma_addr_t cqc_dma; 1872 int ret; 1873 1874 cqc = kzalloc(sizeof(struct qm_cqc), GFP_KERNEL); 1875 if (!cqc) 1876 return -ENOMEM; 1877 1878 INIT_QC_COMMON(cqc, qp->cqe_dma, pasid); 1879 if (ver == QM_HW_V1) { 1880 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0, 1881 QM_QC_CQE_SIZE)); 1882 cqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1); 1883 } else { 1884 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V2(QM_QC_CQE_SIZE)); 1885 cqc->w8 = 0; /* rand_qc */ 1886 } 1887 cqc->dw6 = cpu_to_le32(1 << QM_CQ_PHASE_SHIFT | 1 << QM_CQ_FLAG_SHIFT); 1888 1889 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel) 1890 cqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE); 1891 1892 cqc_dma = dma_map_single(dev, cqc, sizeof(struct qm_cqc), 1893 DMA_TO_DEVICE); 1894 if (dma_mapping_error(dev, cqc_dma)) { 1895 kfree(cqc); 1896 return -ENOMEM; 1897 } 1898 1899 ret = qm_mb(qm, QM_MB_CMD_CQC, cqc_dma, qp_id, 0); 1900 dma_unmap_single(dev, cqc_dma, sizeof(struct qm_cqc), DMA_TO_DEVICE); 1901 kfree(cqc); 1902 1903 return ret; 1904 } 1905 1906 static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 1907 { 1908 int ret; 1909 1910 qm_init_qp_status(qp); 1911 1912 ret = qm_sq_ctx_cfg(qp, qp_id, pasid); 1913 if (ret) 1914 return ret; 1915 1916 return qm_cq_ctx_cfg(qp, qp_id, pasid); 1917 } 1918 1919 static int qm_start_qp_nolock(struct hisi_qp *qp, unsigned long arg) 1920 { 1921 struct hisi_qm *qm = qp->qm; 1922 struct device *dev = &qm->pdev->dev; 1923 int qp_id = qp->qp_id; 1924 u32 pasid = arg; 1925 int ret; 1926 1927 if (!qm_qp_avail_state(qm, qp, QP_START)) 1928 return -EPERM; 1929 1930 ret = qm_qp_ctx_cfg(qp, qp_id, pasid); 1931 if (ret) 1932 return ret; 1933 1934 atomic_set(&qp->qp_status.flags, QP_START); 1935 dev_dbg(dev, "queue %d started\n", qp_id); 1936 1937 return 0; 1938 } 1939 1940 /** 1941 * hisi_qm_start_qp() - Start a qp into running. 1942 * @qp: The qp we want to start to run. 1943 * @arg: Accelerator specific argument. 1944 * 1945 * After this function, qp can receive request from user. Return 0 if 1946 * successful, Return -EBUSY if failed. 1947 */ 1948 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg) 1949 { 1950 struct hisi_qm *qm = qp->qm; 1951 int ret; 1952 1953 down_write(&qm->qps_lock); 1954 ret = qm_start_qp_nolock(qp, arg); 1955 up_write(&qm->qps_lock); 1956 1957 return ret; 1958 } 1959 EXPORT_SYMBOL_GPL(hisi_qm_start_qp); 1960 1961 /** 1962 * qp_stop_fail_cb() - call request cb. 1963 * @qp: stopped failed qp. 1964 * 1965 * Callback function should be called whether task completed or not. 1966 */ 1967 static void qp_stop_fail_cb(struct hisi_qp *qp) 1968 { 1969 int qp_used = atomic_read(&qp->qp_status.used); 1970 u16 cur_tail = qp->qp_status.sq_tail; 1971 u16 cur_head = (cur_tail + QM_Q_DEPTH - qp_used) % QM_Q_DEPTH; 1972 struct hisi_qm *qm = qp->qm; 1973 u16 pos; 1974 int i; 1975 1976 for (i = 0; i < qp_used; i++) { 1977 pos = (i + cur_head) % QM_Q_DEPTH; 1978 qp->req_cb(qp, qp->sqe + (u32)(qm->sqe_size * pos)); 1979 atomic_dec(&qp->qp_status.used); 1980 } 1981 } 1982 1983 /** 1984 * qm_drain_qp() - Drain a qp. 1985 * @qp: The qp we want to drain. 1986 * 1987 * Determine whether the queue is cleared by judging the tail pointers of 1988 * sq and cq. 1989 */ 1990 static int qm_drain_qp(struct hisi_qp *qp) 1991 { 1992 size_t size = sizeof(struct qm_sqc) + sizeof(struct qm_cqc); 1993 struct hisi_qm *qm = qp->qm; 1994 struct device *dev = &qm->pdev->dev; 1995 struct qm_sqc *sqc; 1996 struct qm_cqc *cqc; 1997 dma_addr_t dma_addr; 1998 int ret = 0, i = 0; 1999 void *addr; 2000 2001 /* 2002 * No need to judge if ECC multi-bit error occurs because the 2003 * master OOO will be blocked. 2004 */ 2005 if (qm->err_status.is_qm_ecc_mbit || qm->err_status.is_dev_ecc_mbit) 2006 return 0; 2007 2008 addr = qm_ctx_alloc(qm, size, &dma_addr); 2009 if (IS_ERR(addr)) { 2010 dev_err(dev, "Failed to alloc ctx for sqc and cqc!\n"); 2011 return -ENOMEM; 2012 } 2013 2014 while (++i) { 2015 ret = qm_dump_sqc_raw(qm, dma_addr, qp->qp_id); 2016 if (ret) { 2017 dev_err_ratelimited(dev, "Failed to dump sqc!\n"); 2018 break; 2019 } 2020 sqc = addr; 2021 2022 ret = qm_dump_cqc_raw(qm, (dma_addr + sizeof(struct qm_sqc)), 2023 qp->qp_id); 2024 if (ret) { 2025 dev_err_ratelimited(dev, "Failed to dump cqc!\n"); 2026 break; 2027 } 2028 cqc = addr + sizeof(struct qm_sqc); 2029 2030 if ((sqc->tail == cqc->tail) && 2031 (QM_SQ_TAIL_IDX(sqc) == QM_CQ_TAIL_IDX(cqc))) 2032 break; 2033 2034 if (i == MAX_WAIT_COUNTS) { 2035 dev_err(dev, "Fail to empty queue %u!\n", qp->qp_id); 2036 ret = -EBUSY; 2037 break; 2038 } 2039 2040 usleep_range(WAIT_PERIOD_US_MIN, WAIT_PERIOD_US_MAX); 2041 } 2042 2043 qm_ctx_free(qm, size, addr, &dma_addr); 2044 2045 return ret; 2046 } 2047 2048 static int qm_stop_qp_nolock(struct hisi_qp *qp) 2049 { 2050 struct device *dev = &qp->qm->pdev->dev; 2051 int ret; 2052 2053 /* 2054 * It is allowed to stop and release qp when reset, If the qp is 2055 * stopped when reset but still want to be released then, the 2056 * is_resetting flag should be set negative so that this qp will not 2057 * be restarted after reset. 2058 */ 2059 if (atomic_read(&qp->qp_status.flags) == QP_STOP) { 2060 qp->is_resetting = false; 2061 return 0; 2062 } 2063 2064 if (!qm_qp_avail_state(qp->qm, qp, QP_STOP)) 2065 return -EPERM; 2066 2067 atomic_set(&qp->qp_status.flags, QP_STOP); 2068 2069 ret = qm_drain_qp(qp); 2070 if (ret) 2071 dev_err(dev, "Failed to drain out data for stopping!\n"); 2072 2073 if (qp->qm->wq) 2074 flush_workqueue(qp->qm->wq); 2075 else 2076 flush_work(&qp->qm->work); 2077 2078 if (unlikely(qp->is_resetting && atomic_read(&qp->qp_status.used))) 2079 qp_stop_fail_cb(qp); 2080 2081 dev_dbg(dev, "stop queue %u!", qp->qp_id); 2082 2083 return 0; 2084 } 2085 2086 /** 2087 * hisi_qm_stop_qp() - Stop a qp in qm. 2088 * @qp: The qp we want to stop. 2089 * 2090 * This function is reverse of hisi_qm_start_qp. Return 0 if successful. 2091 */ 2092 int hisi_qm_stop_qp(struct hisi_qp *qp) 2093 { 2094 int ret; 2095 2096 down_write(&qp->qm->qps_lock); 2097 ret = qm_stop_qp_nolock(qp); 2098 up_write(&qp->qm->qps_lock); 2099 2100 return ret; 2101 } 2102 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp); 2103 2104 /** 2105 * hisi_qp_send() - Queue up a task in the hardware queue. 2106 * @qp: The qp in which to put the message. 2107 * @msg: The message. 2108 * 2109 * This function will return -EBUSY if qp is currently full, and -EAGAIN 2110 * if qp related qm is resetting. 2111 * 2112 * Note: This function may run with qm_irq_thread and ACC reset at same time. 2113 * It has no race with qm_irq_thread. However, during hisi_qp_send, ACC 2114 * reset may happen, we have no lock here considering performance. This 2115 * causes current qm_db sending fail or can not receive sended sqe. QM 2116 * sync/async receive function should handle the error sqe. ACC reset 2117 * done function should clear used sqe to 0. 2118 */ 2119 int hisi_qp_send(struct hisi_qp *qp, const void *msg) 2120 { 2121 struct hisi_qp_status *qp_status = &qp->qp_status; 2122 u16 sq_tail = qp_status->sq_tail; 2123 u16 sq_tail_next = (sq_tail + 1) % QM_Q_DEPTH; 2124 void *sqe = qm_get_avail_sqe(qp); 2125 2126 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP || 2127 atomic_read(&qp->qm->status.flags) == QM_STOP || 2128 qp->is_resetting)) { 2129 dev_info_ratelimited(&qp->qm->pdev->dev, "QP is stopped or resetting\n"); 2130 return -EAGAIN; 2131 } 2132 2133 if (!sqe) 2134 return -EBUSY; 2135 2136 memcpy(sqe, msg, qp->qm->sqe_size); 2137 2138 qm_db(qp->qm, qp->qp_id, QM_DOORBELL_CMD_SQ, sq_tail_next, 0); 2139 atomic_inc(&qp->qp_status.used); 2140 qp_status->sq_tail = sq_tail_next; 2141 2142 return 0; 2143 } 2144 EXPORT_SYMBOL_GPL(hisi_qp_send); 2145 2146 static void hisi_qm_cache_wb(struct hisi_qm *qm) 2147 { 2148 unsigned int val; 2149 2150 if (qm->ver == QM_HW_V1) 2151 return; 2152 2153 writel(0x1, qm->io_base + QM_CACHE_WB_START); 2154 if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE, 2155 val, val & BIT(0), POLL_PERIOD, 2156 POLL_TIMEOUT)) 2157 dev_err(&qm->pdev->dev, "QM writeback sqc cache fail!\n"); 2158 } 2159 2160 static void qm_qp_event_notifier(struct hisi_qp *qp) 2161 { 2162 wake_up_interruptible(&qp->uacce_q->wait); 2163 } 2164 2165 static int hisi_qm_get_available_instances(struct uacce_device *uacce) 2166 { 2167 return hisi_qm_get_free_qp_num(uacce->priv); 2168 } 2169 2170 static int hisi_qm_uacce_get_queue(struct uacce_device *uacce, 2171 unsigned long arg, 2172 struct uacce_queue *q) 2173 { 2174 struct hisi_qm *qm = uacce->priv; 2175 struct hisi_qp *qp; 2176 u8 alg_type = 0; 2177 2178 qp = hisi_qm_create_qp(qm, alg_type); 2179 if (IS_ERR(qp)) 2180 return PTR_ERR(qp); 2181 2182 q->priv = qp; 2183 q->uacce = uacce; 2184 qp->uacce_q = q; 2185 qp->event_cb = qm_qp_event_notifier; 2186 qp->pasid = arg; 2187 qp->is_in_kernel = false; 2188 2189 return 0; 2190 } 2191 2192 static void hisi_qm_uacce_put_queue(struct uacce_queue *q) 2193 { 2194 struct hisi_qp *qp = q->priv; 2195 2196 hisi_qm_cache_wb(qp->qm); 2197 hisi_qm_release_qp(qp); 2198 } 2199 2200 /* map sq/cq/doorbell to user space */ 2201 static int hisi_qm_uacce_mmap(struct uacce_queue *q, 2202 struct vm_area_struct *vma, 2203 struct uacce_qfile_region *qfr) 2204 { 2205 struct hisi_qp *qp = q->priv; 2206 struct hisi_qm *qm = qp->qm; 2207 resource_size_t phys_base = qm->db_phys_base + 2208 qp->qp_id * qm->db_interval; 2209 size_t sz = vma->vm_end - vma->vm_start; 2210 struct pci_dev *pdev = qm->pdev; 2211 struct device *dev = &pdev->dev; 2212 unsigned long vm_pgoff; 2213 int ret; 2214 2215 switch (qfr->type) { 2216 case UACCE_QFRT_MMIO: 2217 if (qm->ver == QM_HW_V1) { 2218 if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR) 2219 return -EINVAL; 2220 } else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation) { 2221 if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR + 2222 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE)) 2223 return -EINVAL; 2224 } else { 2225 if (sz > qm->db_interval) 2226 return -EINVAL; 2227 } 2228 2229 vma->vm_flags |= VM_IO; 2230 2231 return remap_pfn_range(vma, vma->vm_start, 2232 phys_base >> PAGE_SHIFT, 2233 sz, pgprot_noncached(vma->vm_page_prot)); 2234 case UACCE_QFRT_DUS: 2235 if (sz != qp->qdma.size) 2236 return -EINVAL; 2237 2238 /* 2239 * dma_mmap_coherent() requires vm_pgoff as 0 2240 * restore vm_pfoff to initial value for mmap() 2241 */ 2242 vm_pgoff = vma->vm_pgoff; 2243 vma->vm_pgoff = 0; 2244 ret = dma_mmap_coherent(dev, vma, qp->qdma.va, 2245 qp->qdma.dma, sz); 2246 vma->vm_pgoff = vm_pgoff; 2247 return ret; 2248 2249 default: 2250 return -EINVAL; 2251 } 2252 } 2253 2254 static int hisi_qm_uacce_start_queue(struct uacce_queue *q) 2255 { 2256 struct hisi_qp *qp = q->priv; 2257 2258 return hisi_qm_start_qp(qp, qp->pasid); 2259 } 2260 2261 static void hisi_qm_uacce_stop_queue(struct uacce_queue *q) 2262 { 2263 hisi_qm_stop_qp(q->priv); 2264 } 2265 2266 static void qm_set_sqctype(struct uacce_queue *q, u16 type) 2267 { 2268 struct hisi_qm *qm = q->uacce->priv; 2269 struct hisi_qp *qp = q->priv; 2270 2271 down_write(&qm->qps_lock); 2272 qp->alg_type = type; 2273 up_write(&qm->qps_lock); 2274 } 2275 2276 static long hisi_qm_uacce_ioctl(struct uacce_queue *q, unsigned int cmd, 2277 unsigned long arg) 2278 { 2279 struct hisi_qp *qp = q->priv; 2280 struct hisi_qp_ctx qp_ctx; 2281 2282 if (cmd == UACCE_CMD_QM_SET_QP_CTX) { 2283 if (copy_from_user(&qp_ctx, (void __user *)arg, 2284 sizeof(struct hisi_qp_ctx))) 2285 return -EFAULT; 2286 2287 if (qp_ctx.qc_type != 0 && qp_ctx.qc_type != 1) 2288 return -EINVAL; 2289 2290 qm_set_sqctype(q, qp_ctx.qc_type); 2291 qp_ctx.id = qp->qp_id; 2292 2293 if (copy_to_user((void __user *)arg, &qp_ctx, 2294 sizeof(struct hisi_qp_ctx))) 2295 return -EFAULT; 2296 } else { 2297 return -EINVAL; 2298 } 2299 2300 return 0; 2301 } 2302 2303 static const struct uacce_ops uacce_qm_ops = { 2304 .get_available_instances = hisi_qm_get_available_instances, 2305 .get_queue = hisi_qm_uacce_get_queue, 2306 .put_queue = hisi_qm_uacce_put_queue, 2307 .start_queue = hisi_qm_uacce_start_queue, 2308 .stop_queue = hisi_qm_uacce_stop_queue, 2309 .mmap = hisi_qm_uacce_mmap, 2310 .ioctl = hisi_qm_uacce_ioctl, 2311 }; 2312 2313 static int qm_alloc_uacce(struct hisi_qm *qm) 2314 { 2315 struct pci_dev *pdev = qm->pdev; 2316 struct uacce_device *uacce; 2317 unsigned long mmio_page_nr; 2318 unsigned long dus_page_nr; 2319 struct uacce_interface interface = { 2320 .flags = UACCE_DEV_SVA, 2321 .ops = &uacce_qm_ops, 2322 }; 2323 int ret; 2324 2325 ret = strscpy(interface.name, pdev->driver->name, 2326 sizeof(interface.name)); 2327 if (ret < 0) 2328 return -ENAMETOOLONG; 2329 2330 uacce = uacce_alloc(&pdev->dev, &interface); 2331 if (IS_ERR(uacce)) 2332 return PTR_ERR(uacce); 2333 2334 if (uacce->flags & UACCE_DEV_SVA && qm->mode == UACCE_MODE_SVA) { 2335 qm->use_sva = true; 2336 } else { 2337 /* only consider sva case */ 2338 uacce_remove(uacce); 2339 qm->uacce = NULL; 2340 return -EINVAL; 2341 } 2342 2343 uacce->is_vf = pdev->is_virtfn; 2344 uacce->priv = qm; 2345 uacce->algs = qm->algs; 2346 2347 if (qm->ver == QM_HW_V1) 2348 uacce->api_ver = HISI_QM_API_VER_BASE; 2349 else if (qm->ver == QM_HW_V2) 2350 uacce->api_ver = HISI_QM_API_VER2_BASE; 2351 else 2352 uacce->api_ver = HISI_QM_API_VER3_BASE; 2353 2354 if (qm->ver == QM_HW_V1) 2355 mmio_page_nr = QM_DOORBELL_PAGE_NR; 2356 else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation) 2357 mmio_page_nr = QM_DOORBELL_PAGE_NR + 2358 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE; 2359 else 2360 mmio_page_nr = qm->db_interval / PAGE_SIZE; 2361 2362 dus_page_nr = (PAGE_SIZE - 1 + qm->sqe_size * QM_Q_DEPTH + 2363 sizeof(struct qm_cqe) * QM_Q_DEPTH) >> PAGE_SHIFT; 2364 2365 uacce->qf_pg_num[UACCE_QFRT_MMIO] = mmio_page_nr; 2366 uacce->qf_pg_num[UACCE_QFRT_DUS] = dus_page_nr; 2367 2368 qm->uacce = uacce; 2369 2370 return 0; 2371 } 2372 2373 /** 2374 * qm_frozen() - Try to froze QM to cut continuous queue request. If 2375 * there is user on the QM, return failure without doing anything. 2376 * @qm: The qm needed to be fronzen. 2377 * 2378 * This function frozes QM, then we can do SRIOV disabling. 2379 */ 2380 static int qm_frozen(struct hisi_qm *qm) 2381 { 2382 if (test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl)) 2383 return 0; 2384 2385 down_write(&qm->qps_lock); 2386 2387 if (!qm->qp_in_used) { 2388 qm->qp_in_used = qm->qp_num; 2389 up_write(&qm->qps_lock); 2390 set_bit(QM_DRIVER_REMOVING, &qm->misc_ctl); 2391 return 0; 2392 } 2393 2394 up_write(&qm->qps_lock); 2395 2396 return -EBUSY; 2397 } 2398 2399 static int qm_try_frozen_vfs(struct pci_dev *pdev, 2400 struct hisi_qm_list *qm_list) 2401 { 2402 struct hisi_qm *qm, *vf_qm; 2403 struct pci_dev *dev; 2404 int ret = 0; 2405 2406 if (!qm_list || !pdev) 2407 return -EINVAL; 2408 2409 /* Try to frozen all the VFs as disable SRIOV */ 2410 mutex_lock(&qm_list->lock); 2411 list_for_each_entry(qm, &qm_list->list, list) { 2412 dev = qm->pdev; 2413 if (dev == pdev) 2414 continue; 2415 if (pci_physfn(dev) == pdev) { 2416 vf_qm = pci_get_drvdata(dev); 2417 ret = qm_frozen(vf_qm); 2418 if (ret) 2419 goto frozen_fail; 2420 } 2421 } 2422 2423 frozen_fail: 2424 mutex_unlock(&qm_list->lock); 2425 2426 return ret; 2427 } 2428 2429 /** 2430 * hisi_qm_wait_task_finish() - Wait until the task is finished 2431 * when removing the driver. 2432 * @qm: The qm needed to wait for the task to finish. 2433 * @qm_list: The list of all available devices. 2434 */ 2435 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 2436 { 2437 while (qm_frozen(qm) || 2438 ((qm->fun_type == QM_HW_PF) && 2439 qm_try_frozen_vfs(qm->pdev, qm_list))) { 2440 msleep(WAIT_PERIOD); 2441 } 2442 2443 while (test_bit(QM_RST_SCHED, &qm->misc_ctl) || 2444 test_bit(QM_RESETTING, &qm->misc_ctl)) 2445 msleep(WAIT_PERIOD); 2446 2447 udelay(REMOVE_WAIT_DELAY); 2448 } 2449 EXPORT_SYMBOL_GPL(hisi_qm_wait_task_finish); 2450 2451 /** 2452 * hisi_qm_get_free_qp_num() - Get free number of qp in qm. 2453 * @qm: The qm which want to get free qp. 2454 * 2455 * This function return free number of qp in qm. 2456 */ 2457 int hisi_qm_get_free_qp_num(struct hisi_qm *qm) 2458 { 2459 int ret; 2460 2461 down_read(&qm->qps_lock); 2462 ret = qm->qp_num - qm->qp_in_used; 2463 up_read(&qm->qps_lock); 2464 2465 return ret; 2466 } 2467 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num); 2468 2469 static void hisi_qp_memory_uninit(struct hisi_qm *qm, int num) 2470 { 2471 struct device *dev = &qm->pdev->dev; 2472 struct qm_dma *qdma; 2473 int i; 2474 2475 for (i = num - 1; i >= 0; i--) { 2476 qdma = &qm->qp_array[i].qdma; 2477 dma_free_coherent(dev, qdma->size, qdma->va, qdma->dma); 2478 } 2479 2480 kfree(qm->qp_array); 2481 } 2482 2483 static int hisi_qp_memory_init(struct hisi_qm *qm, size_t dma_size, int id) 2484 { 2485 struct device *dev = &qm->pdev->dev; 2486 size_t off = qm->sqe_size * QM_Q_DEPTH; 2487 struct hisi_qp *qp; 2488 2489 qp = &qm->qp_array[id]; 2490 qp->qdma.va = dma_alloc_coherent(dev, dma_size, &qp->qdma.dma, 2491 GFP_KERNEL); 2492 if (!qp->qdma.va) 2493 return -ENOMEM; 2494 2495 qp->sqe = qp->qdma.va; 2496 qp->sqe_dma = qp->qdma.dma; 2497 qp->cqe = qp->qdma.va + off; 2498 qp->cqe_dma = qp->qdma.dma + off; 2499 qp->qdma.size = dma_size; 2500 qp->qm = qm; 2501 qp->qp_id = id; 2502 2503 return 0; 2504 } 2505 2506 static int hisi_qm_memory_init(struct hisi_qm *qm) 2507 { 2508 struct device *dev = &qm->pdev->dev; 2509 size_t qp_dma_size, off = 0; 2510 int i, ret = 0; 2511 2512 #define QM_INIT_BUF(qm, type, num) do { \ 2513 (qm)->type = ((qm)->qdma.va + (off)); \ 2514 (qm)->type##_dma = (qm)->qdma.dma + (off); \ 2515 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \ 2516 } while (0) 2517 2518 idr_init(&qm->qp_idr); 2519 qm->qdma.size = QMC_ALIGN(sizeof(struct qm_eqe) * QM_EQ_DEPTH) + 2520 QMC_ALIGN(sizeof(struct qm_aeqe) * QM_Q_DEPTH) + 2521 QMC_ALIGN(sizeof(struct qm_sqc) * qm->qp_num) + 2522 QMC_ALIGN(sizeof(struct qm_cqc) * qm->qp_num); 2523 qm->qdma.va = dma_alloc_coherent(dev, qm->qdma.size, &qm->qdma.dma, 2524 GFP_ATOMIC); 2525 dev_dbg(dev, "allocate qm dma buf size=%zx)\n", qm->qdma.size); 2526 if (!qm->qdma.va) 2527 return -ENOMEM; 2528 2529 QM_INIT_BUF(qm, eqe, QM_EQ_DEPTH); 2530 QM_INIT_BUF(qm, aeqe, QM_Q_DEPTH); 2531 QM_INIT_BUF(qm, sqc, qm->qp_num); 2532 QM_INIT_BUF(qm, cqc, qm->qp_num); 2533 2534 qm->qp_array = kcalloc(qm->qp_num, sizeof(struct hisi_qp), GFP_KERNEL); 2535 if (!qm->qp_array) { 2536 ret = -ENOMEM; 2537 goto err_alloc_qp_array; 2538 } 2539 2540 /* one more page for device or qp statuses */ 2541 qp_dma_size = qm->sqe_size * QM_Q_DEPTH + 2542 sizeof(struct qm_cqe) * QM_Q_DEPTH; 2543 qp_dma_size = PAGE_ALIGN(qp_dma_size); 2544 for (i = 0; i < qm->qp_num; i++) { 2545 ret = hisi_qp_memory_init(qm, qp_dma_size, i); 2546 if (ret) 2547 goto err_init_qp_mem; 2548 2549 dev_dbg(dev, "allocate qp dma buf size=%zx)\n", qp_dma_size); 2550 } 2551 2552 return ret; 2553 2554 err_init_qp_mem: 2555 hisi_qp_memory_uninit(qm, i); 2556 err_alloc_qp_array: 2557 dma_free_coherent(dev, qm->qdma.size, qm->qdma.va, qm->qdma.dma); 2558 2559 return ret; 2560 } 2561 2562 static void hisi_qm_pre_init(struct hisi_qm *qm) 2563 { 2564 struct pci_dev *pdev = qm->pdev; 2565 2566 if (qm->ver == QM_HW_V1) 2567 qm->ops = &qm_hw_ops_v1; 2568 else 2569 qm->ops = &qm_hw_ops_v2; 2570 2571 pci_set_drvdata(pdev, qm); 2572 mutex_init(&qm->mailbox_lock); 2573 init_rwsem(&qm->qps_lock); 2574 qm->qp_in_used = 0; 2575 qm->misc_ctl = false; 2576 } 2577 2578 static void qm_put_pci_res(struct hisi_qm *qm) 2579 { 2580 struct pci_dev *pdev = qm->pdev; 2581 2582 if (qm->use_db_isolation) 2583 iounmap(qm->db_io_base); 2584 2585 iounmap(qm->io_base); 2586 pci_release_mem_regions(pdev); 2587 } 2588 2589 static void hisi_qm_pci_uninit(struct hisi_qm *qm) 2590 { 2591 struct pci_dev *pdev = qm->pdev; 2592 2593 pci_free_irq_vectors(pdev); 2594 qm_put_pci_res(qm); 2595 pci_disable_device(pdev); 2596 } 2597 2598 /** 2599 * hisi_qm_uninit() - Uninitialize qm. 2600 * @qm: The qm needed uninit. 2601 * 2602 * This function uninits qm related device resources. 2603 */ 2604 void hisi_qm_uninit(struct hisi_qm *qm) 2605 { 2606 struct pci_dev *pdev = qm->pdev; 2607 struct device *dev = &pdev->dev; 2608 2609 down_write(&qm->qps_lock); 2610 2611 if (!qm_avail_state(qm, QM_CLOSE)) { 2612 up_write(&qm->qps_lock); 2613 return; 2614 } 2615 2616 hisi_qp_memory_uninit(qm, qm->qp_num); 2617 idr_destroy(&qm->qp_idr); 2618 2619 if (qm->qdma.va) { 2620 hisi_qm_cache_wb(qm); 2621 dma_free_coherent(dev, qm->qdma.size, 2622 qm->qdma.va, qm->qdma.dma); 2623 memset(&qm->qdma, 0, sizeof(qm->qdma)); 2624 } 2625 2626 qm_irq_unregister(qm); 2627 hisi_qm_pci_uninit(qm); 2628 uacce_remove(qm->uacce); 2629 qm->uacce = NULL; 2630 2631 up_write(&qm->qps_lock); 2632 } 2633 EXPORT_SYMBOL_GPL(hisi_qm_uninit); 2634 2635 /** 2636 * hisi_qm_get_vft() - Get vft from a qm. 2637 * @qm: The qm we want to get its vft. 2638 * @base: The base number of queue in vft. 2639 * @number: The number of queues in vft. 2640 * 2641 * We can allocate multiple queues to a qm by configuring virtual function 2642 * table. We get related configures by this function. Normally, we call this 2643 * function in VF driver to get the queue information. 2644 * 2645 * qm hw v1 does not support this interface. 2646 */ 2647 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number) 2648 { 2649 if (!base || !number) 2650 return -EINVAL; 2651 2652 if (!qm->ops->get_vft) { 2653 dev_err(&qm->pdev->dev, "Don't support vft read!\n"); 2654 return -EINVAL; 2655 } 2656 2657 return qm->ops->get_vft(qm, base, number); 2658 } 2659 EXPORT_SYMBOL_GPL(hisi_qm_get_vft); 2660 2661 /** 2662 * hisi_qm_set_vft() - Set vft to a qm. 2663 * @qm: The qm we want to set its vft. 2664 * @fun_num: The function number. 2665 * @base: The base number of queue in vft. 2666 * @number: The number of queues in vft. 2667 * 2668 * This function is alway called in PF driver, it is used to assign queues 2669 * among PF and VFs. 2670 * 2671 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1) 2672 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1) 2673 * (VF function number 0x2) 2674 */ 2675 static int hisi_qm_set_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 2676 u32 number) 2677 { 2678 u32 max_q_num = qm->ctrl_qp_num; 2679 2680 if (base >= max_q_num || number > max_q_num || 2681 (base + number) > max_q_num) 2682 return -EINVAL; 2683 2684 return qm_set_sqc_cqc_vft(qm, fun_num, base, number); 2685 } 2686 2687 static void qm_init_eq_aeq_status(struct hisi_qm *qm) 2688 { 2689 struct hisi_qm_status *status = &qm->status; 2690 2691 status->eq_head = 0; 2692 status->aeq_head = 0; 2693 status->eqc_phase = true; 2694 status->aeqc_phase = true; 2695 } 2696 2697 static int qm_eq_ctx_cfg(struct hisi_qm *qm) 2698 { 2699 struct device *dev = &qm->pdev->dev; 2700 struct qm_eqc *eqc; 2701 dma_addr_t eqc_dma; 2702 int ret; 2703 2704 eqc = kzalloc(sizeof(struct qm_eqc), GFP_KERNEL); 2705 if (!eqc) 2706 return -ENOMEM; 2707 2708 eqc->base_l = cpu_to_le32(lower_32_bits(qm->eqe_dma)); 2709 eqc->base_h = cpu_to_le32(upper_32_bits(qm->eqe_dma)); 2710 if (qm->ver == QM_HW_V1) 2711 eqc->dw3 = cpu_to_le32(QM_EQE_AEQE_SIZE); 2712 eqc->dw6 = cpu_to_le32((QM_EQ_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT)); 2713 2714 eqc_dma = dma_map_single(dev, eqc, sizeof(struct qm_eqc), 2715 DMA_TO_DEVICE); 2716 if (dma_mapping_error(dev, eqc_dma)) { 2717 kfree(eqc); 2718 return -ENOMEM; 2719 } 2720 2721 ret = qm_mb(qm, QM_MB_CMD_EQC, eqc_dma, 0, 0); 2722 dma_unmap_single(dev, eqc_dma, sizeof(struct qm_eqc), DMA_TO_DEVICE); 2723 kfree(eqc); 2724 2725 return ret; 2726 } 2727 2728 static int qm_aeq_ctx_cfg(struct hisi_qm *qm) 2729 { 2730 struct device *dev = &qm->pdev->dev; 2731 struct qm_aeqc *aeqc; 2732 dma_addr_t aeqc_dma; 2733 int ret; 2734 2735 aeqc = kzalloc(sizeof(struct qm_aeqc), GFP_KERNEL); 2736 if (!aeqc) 2737 return -ENOMEM; 2738 2739 aeqc->base_l = cpu_to_le32(lower_32_bits(qm->aeqe_dma)); 2740 aeqc->base_h = cpu_to_le32(upper_32_bits(qm->aeqe_dma)); 2741 aeqc->dw6 = cpu_to_le32((QM_Q_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT)); 2742 2743 aeqc_dma = dma_map_single(dev, aeqc, sizeof(struct qm_aeqc), 2744 DMA_TO_DEVICE); 2745 if (dma_mapping_error(dev, aeqc_dma)) { 2746 kfree(aeqc); 2747 return -ENOMEM; 2748 } 2749 2750 ret = qm_mb(qm, QM_MB_CMD_AEQC, aeqc_dma, 0, 0); 2751 dma_unmap_single(dev, aeqc_dma, sizeof(struct qm_aeqc), DMA_TO_DEVICE); 2752 kfree(aeqc); 2753 2754 return ret; 2755 } 2756 2757 static int qm_eq_aeq_ctx_cfg(struct hisi_qm *qm) 2758 { 2759 struct device *dev = &qm->pdev->dev; 2760 int ret; 2761 2762 qm_init_eq_aeq_status(qm); 2763 2764 ret = qm_eq_ctx_cfg(qm); 2765 if (ret) { 2766 dev_err(dev, "Set eqc failed!\n"); 2767 return ret; 2768 } 2769 2770 return qm_aeq_ctx_cfg(qm); 2771 } 2772 2773 static int __hisi_qm_start(struct hisi_qm *qm) 2774 { 2775 int ret; 2776 2777 WARN_ON(!qm->qdma.va); 2778 2779 if (qm->fun_type == QM_HW_PF) { 2780 ret = qm_dev_mem_reset(qm); 2781 if (ret) 2782 return ret; 2783 2784 ret = hisi_qm_set_vft(qm, 0, qm->qp_base, qm->qp_num); 2785 if (ret) 2786 return ret; 2787 } 2788 2789 ret = qm_eq_aeq_ctx_cfg(qm); 2790 if (ret) 2791 return ret; 2792 2793 ret = qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0); 2794 if (ret) 2795 return ret; 2796 2797 ret = qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0); 2798 if (ret) 2799 return ret; 2800 2801 writel(0x0, qm->io_base + QM_VF_EQ_INT_MASK); 2802 writel(0x0, qm->io_base + QM_VF_AEQ_INT_MASK); 2803 2804 return 0; 2805 } 2806 2807 /** 2808 * hisi_qm_start() - start qm 2809 * @qm: The qm to be started. 2810 * 2811 * This function starts a qm, then we can allocate qp from this qm. 2812 */ 2813 int hisi_qm_start(struct hisi_qm *qm) 2814 { 2815 struct device *dev = &qm->pdev->dev; 2816 int ret = 0; 2817 2818 down_write(&qm->qps_lock); 2819 2820 if (!qm_avail_state(qm, QM_START)) { 2821 up_write(&qm->qps_lock); 2822 return -EPERM; 2823 } 2824 2825 dev_dbg(dev, "qm start with %u queue pairs\n", qm->qp_num); 2826 2827 if (!qm->qp_num) { 2828 dev_err(dev, "qp_num should not be 0\n"); 2829 ret = -EINVAL; 2830 goto err_unlock; 2831 } 2832 2833 ret = __hisi_qm_start(qm); 2834 if (!ret) 2835 atomic_set(&qm->status.flags, QM_START); 2836 2837 err_unlock: 2838 up_write(&qm->qps_lock); 2839 return ret; 2840 } 2841 EXPORT_SYMBOL_GPL(hisi_qm_start); 2842 2843 static int qm_restart(struct hisi_qm *qm) 2844 { 2845 struct device *dev = &qm->pdev->dev; 2846 struct hisi_qp *qp; 2847 int ret, i; 2848 2849 ret = hisi_qm_start(qm); 2850 if (ret < 0) 2851 return ret; 2852 2853 down_write(&qm->qps_lock); 2854 for (i = 0; i < qm->qp_num; i++) { 2855 qp = &qm->qp_array[i]; 2856 if (atomic_read(&qp->qp_status.flags) == QP_STOP && 2857 qp->is_resetting == true) { 2858 ret = qm_start_qp_nolock(qp, 0); 2859 if (ret < 0) { 2860 dev_err(dev, "Failed to start qp%d!\n", i); 2861 2862 up_write(&qm->qps_lock); 2863 return ret; 2864 } 2865 qp->is_resetting = false; 2866 } 2867 } 2868 up_write(&qm->qps_lock); 2869 2870 return 0; 2871 } 2872 2873 /* Stop started qps in reset flow */ 2874 static int qm_stop_started_qp(struct hisi_qm *qm) 2875 { 2876 struct device *dev = &qm->pdev->dev; 2877 struct hisi_qp *qp; 2878 int i, ret; 2879 2880 for (i = 0; i < qm->qp_num; i++) { 2881 qp = &qm->qp_array[i]; 2882 if (qp && atomic_read(&qp->qp_status.flags) == QP_START) { 2883 qp->is_resetting = true; 2884 ret = qm_stop_qp_nolock(qp); 2885 if (ret < 0) { 2886 dev_err(dev, "Failed to stop qp%d!\n", i); 2887 return ret; 2888 } 2889 } 2890 } 2891 2892 return 0; 2893 } 2894 2895 2896 /** 2897 * qm_clear_queues() - Clear all queues memory in a qm. 2898 * @qm: The qm in which the queues will be cleared. 2899 * 2900 * This function clears all queues memory in a qm. Reset of accelerator can 2901 * use this to clear queues. 2902 */ 2903 static void qm_clear_queues(struct hisi_qm *qm) 2904 { 2905 struct hisi_qp *qp; 2906 int i; 2907 2908 for (i = 0; i < qm->qp_num; i++) { 2909 qp = &qm->qp_array[i]; 2910 if (qp->is_resetting) 2911 memset(qp->qdma.va, 0, qp->qdma.size); 2912 } 2913 2914 memset(qm->qdma.va, 0, qm->qdma.size); 2915 } 2916 2917 /** 2918 * hisi_qm_stop() - Stop a qm. 2919 * @qm: The qm which will be stopped. 2920 * @r: The reason to stop qm. 2921 * 2922 * This function stops qm and its qps, then qm can not accept request. 2923 * Related resources are not released at this state, we can use hisi_qm_start 2924 * to let qm start again. 2925 */ 2926 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r) 2927 { 2928 struct device *dev = &qm->pdev->dev; 2929 int ret = 0; 2930 2931 down_write(&qm->qps_lock); 2932 2933 qm->status.stop_reason = r; 2934 if (!qm_avail_state(qm, QM_STOP)) { 2935 ret = -EPERM; 2936 goto err_unlock; 2937 } 2938 2939 if (qm->status.stop_reason == QM_SOFT_RESET || 2940 qm->status.stop_reason == QM_FLR) { 2941 ret = qm_stop_started_qp(qm); 2942 if (ret < 0) { 2943 dev_err(dev, "Failed to stop started qp!\n"); 2944 goto err_unlock; 2945 } 2946 } 2947 2948 /* Mask eq and aeq irq */ 2949 writel(0x1, qm->io_base + QM_VF_EQ_INT_MASK); 2950 writel(0x1, qm->io_base + QM_VF_AEQ_INT_MASK); 2951 2952 if (qm->fun_type == QM_HW_PF) { 2953 ret = hisi_qm_set_vft(qm, 0, 0, 0); 2954 if (ret < 0) { 2955 dev_err(dev, "Failed to set vft!\n"); 2956 ret = -EBUSY; 2957 goto err_unlock; 2958 } 2959 } 2960 2961 qm_clear_queues(qm); 2962 atomic_set(&qm->status.flags, QM_STOP); 2963 2964 err_unlock: 2965 up_write(&qm->qps_lock); 2966 return ret; 2967 } 2968 EXPORT_SYMBOL_GPL(hisi_qm_stop); 2969 2970 static ssize_t qm_status_read(struct file *filp, char __user *buffer, 2971 size_t count, loff_t *pos) 2972 { 2973 struct hisi_qm *qm = filp->private_data; 2974 char buf[QM_DBG_READ_LEN]; 2975 int val, len; 2976 2977 val = atomic_read(&qm->status.flags); 2978 len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n", qm_s[val]); 2979 2980 return simple_read_from_buffer(buffer, count, pos, buf, len); 2981 } 2982 2983 static const struct file_operations qm_status_fops = { 2984 .owner = THIS_MODULE, 2985 .open = simple_open, 2986 .read = qm_status_read, 2987 }; 2988 2989 static int qm_debugfs_atomic64_set(void *data, u64 val) 2990 { 2991 if (val) 2992 return -EINVAL; 2993 2994 atomic64_set((atomic64_t *)data, 0); 2995 2996 return 0; 2997 } 2998 2999 static int qm_debugfs_atomic64_get(void *data, u64 *val) 3000 { 3001 *val = atomic64_read((atomic64_t *)data); 3002 3003 return 0; 3004 } 3005 3006 DEFINE_DEBUGFS_ATTRIBUTE(qm_atomic64_ops, qm_debugfs_atomic64_get, 3007 qm_debugfs_atomic64_set, "%llu\n"); 3008 3009 /** 3010 * hisi_qm_debug_init() - Initialize qm related debugfs files. 3011 * @qm: The qm for which we want to add debugfs files. 3012 * 3013 * Create qm related debugfs files. 3014 */ 3015 void hisi_qm_debug_init(struct hisi_qm *qm) 3016 { 3017 struct qm_dfx *dfx = &qm->debug.dfx; 3018 struct dentry *qm_d; 3019 void *data; 3020 int i; 3021 3022 qm_d = debugfs_create_dir("qm", qm->debug.debug_root); 3023 qm->debug.qm_d = qm_d; 3024 3025 /* only show this in PF */ 3026 if (qm->fun_type == QM_HW_PF) { 3027 qm_create_debugfs_file(qm, qm->debug.debug_root, CURRENT_QM); 3028 for (i = CURRENT_Q; i < DEBUG_FILE_NUM; i++) 3029 qm_create_debugfs_file(qm, qm_d, i); 3030 } 3031 3032 debugfs_create_file("regs", 0444, qm->debug.qm_d, qm, &qm_regs_fops); 3033 3034 debugfs_create_file("cmd", 0444, qm->debug.qm_d, qm, &qm_cmd_fops); 3035 3036 debugfs_create_file("status", 0444, qm->debug.qm_d, qm, 3037 &qm_status_fops); 3038 for (i = 0; i < ARRAY_SIZE(qm_dfx_files); i++) { 3039 data = (atomic64_t *)((uintptr_t)dfx + qm_dfx_files[i].offset); 3040 debugfs_create_file(qm_dfx_files[i].name, 3041 0644, 3042 qm_d, 3043 data, 3044 &qm_atomic64_ops); 3045 } 3046 } 3047 EXPORT_SYMBOL_GPL(hisi_qm_debug_init); 3048 3049 /** 3050 * hisi_qm_debug_regs_clear() - clear qm debug related registers. 3051 * @qm: The qm for which we want to clear its debug registers. 3052 */ 3053 void hisi_qm_debug_regs_clear(struct hisi_qm *qm) 3054 { 3055 struct qm_dfx_registers *regs; 3056 int i; 3057 3058 /* clear current_qm */ 3059 writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF); 3060 writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF); 3061 3062 /* clear current_q */ 3063 writel(0x0, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 3064 writel(0x0, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 3065 3066 /* 3067 * these registers are reading and clearing, so clear them after 3068 * reading them. 3069 */ 3070 writel(0x1, qm->io_base + QM_DFX_CNT_CLR_CE); 3071 3072 regs = qm_dfx_regs; 3073 for (i = 0; i < CNT_CYC_REGS_NUM; i++) { 3074 readl(qm->io_base + regs->reg_offset); 3075 regs++; 3076 } 3077 3078 writel(0x0, qm->io_base + QM_DFX_CNT_CLR_CE); 3079 } 3080 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear); 3081 3082 static void qm_hw_error_init(struct hisi_qm *qm) 3083 { 3084 const struct hisi_qm_err_info *err_info = &qm->err_ini->err_info; 3085 3086 if (!qm->ops->hw_error_init) { 3087 dev_err(&qm->pdev->dev, "QM doesn't support hw error handling!\n"); 3088 return; 3089 } 3090 3091 qm->ops->hw_error_init(qm, err_info->ce, err_info->nfe, err_info->fe); 3092 } 3093 3094 static void qm_hw_error_uninit(struct hisi_qm *qm) 3095 { 3096 if (!qm->ops->hw_error_uninit) { 3097 dev_err(&qm->pdev->dev, "Unexpected QM hw error uninit!\n"); 3098 return; 3099 } 3100 3101 qm->ops->hw_error_uninit(qm); 3102 } 3103 3104 static enum acc_err_result qm_hw_error_handle(struct hisi_qm *qm) 3105 { 3106 if (!qm->ops->hw_error_handle) { 3107 dev_err(&qm->pdev->dev, "QM doesn't support hw error report!\n"); 3108 return ACC_ERR_NONE; 3109 } 3110 3111 return qm->ops->hw_error_handle(qm); 3112 } 3113 3114 /** 3115 * hisi_qm_dev_err_init() - Initialize device error configuration. 3116 * @qm: The qm for which we want to do error initialization. 3117 * 3118 * Initialize QM and device error related configuration. 3119 */ 3120 void hisi_qm_dev_err_init(struct hisi_qm *qm) 3121 { 3122 if (qm->fun_type == QM_HW_VF) 3123 return; 3124 3125 qm_hw_error_init(qm); 3126 3127 if (!qm->err_ini->hw_err_enable) { 3128 dev_err(&qm->pdev->dev, "Device doesn't support hw error init!\n"); 3129 return; 3130 } 3131 qm->err_ini->hw_err_enable(qm); 3132 } 3133 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init); 3134 3135 /** 3136 * hisi_qm_dev_err_uninit() - Uninitialize device error configuration. 3137 * @qm: The qm for which we want to do error uninitialization. 3138 * 3139 * Uninitialize QM and device error related configuration. 3140 */ 3141 void hisi_qm_dev_err_uninit(struct hisi_qm *qm) 3142 { 3143 if (qm->fun_type == QM_HW_VF) 3144 return; 3145 3146 qm_hw_error_uninit(qm); 3147 3148 if (!qm->err_ini->hw_err_disable) { 3149 dev_err(&qm->pdev->dev, "Unexpected device hw error uninit!\n"); 3150 return; 3151 } 3152 qm->err_ini->hw_err_disable(qm); 3153 } 3154 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit); 3155 3156 /** 3157 * hisi_qm_free_qps() - free multiple queue pairs. 3158 * @qps: The queue pairs need to be freed. 3159 * @qp_num: The num of queue pairs. 3160 */ 3161 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num) 3162 { 3163 int i; 3164 3165 if (!qps || qp_num <= 0) 3166 return; 3167 3168 for (i = qp_num - 1; i >= 0; i--) 3169 hisi_qm_release_qp(qps[i]); 3170 } 3171 EXPORT_SYMBOL_GPL(hisi_qm_free_qps); 3172 3173 static void free_list(struct list_head *head) 3174 { 3175 struct hisi_qm_resource *res, *tmp; 3176 3177 list_for_each_entry_safe(res, tmp, head, list) { 3178 list_del(&res->list); 3179 kfree(res); 3180 } 3181 } 3182 3183 static int hisi_qm_sort_devices(int node, struct list_head *head, 3184 struct hisi_qm_list *qm_list) 3185 { 3186 struct hisi_qm_resource *res, *tmp; 3187 struct hisi_qm *qm; 3188 struct list_head *n; 3189 struct device *dev; 3190 int dev_node = 0; 3191 3192 list_for_each_entry(qm, &qm_list->list, list) { 3193 dev = &qm->pdev->dev; 3194 3195 if (IS_ENABLED(CONFIG_NUMA)) { 3196 dev_node = dev_to_node(dev); 3197 if (dev_node < 0) 3198 dev_node = 0; 3199 } 3200 3201 res = kzalloc(sizeof(*res), GFP_KERNEL); 3202 if (!res) 3203 return -ENOMEM; 3204 3205 res->qm = qm; 3206 res->distance = node_distance(dev_node, node); 3207 n = head; 3208 list_for_each_entry(tmp, head, list) { 3209 if (res->distance < tmp->distance) { 3210 n = &tmp->list; 3211 break; 3212 } 3213 } 3214 list_add_tail(&res->list, n); 3215 } 3216 3217 return 0; 3218 } 3219 3220 /** 3221 * hisi_qm_alloc_qps_node() - Create multiple queue pairs. 3222 * @qm_list: The list of all available devices. 3223 * @qp_num: The number of queue pairs need created. 3224 * @alg_type: The algorithm type. 3225 * @node: The numa node. 3226 * @qps: The queue pairs need created. 3227 * 3228 * This function will sort all available device according to numa distance. 3229 * Then try to create all queue pairs from one device, if all devices do 3230 * not meet the requirements will return error. 3231 */ 3232 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num, 3233 u8 alg_type, int node, struct hisi_qp **qps) 3234 { 3235 struct hisi_qm_resource *tmp; 3236 int ret = -ENODEV; 3237 LIST_HEAD(head); 3238 int i; 3239 3240 if (!qps || !qm_list || qp_num <= 0) 3241 return -EINVAL; 3242 3243 mutex_lock(&qm_list->lock); 3244 if (hisi_qm_sort_devices(node, &head, qm_list)) { 3245 mutex_unlock(&qm_list->lock); 3246 goto err; 3247 } 3248 3249 list_for_each_entry(tmp, &head, list) { 3250 for (i = 0; i < qp_num; i++) { 3251 qps[i] = hisi_qm_create_qp(tmp->qm, alg_type); 3252 if (IS_ERR(qps[i])) { 3253 hisi_qm_free_qps(qps, i); 3254 break; 3255 } 3256 } 3257 3258 if (i == qp_num) { 3259 ret = 0; 3260 break; 3261 } 3262 } 3263 3264 mutex_unlock(&qm_list->lock); 3265 if (ret) 3266 pr_info("Failed to create qps, node[%d], alg[%u], qp[%d]!\n", 3267 node, alg_type, qp_num); 3268 3269 err: 3270 free_list(&head); 3271 return ret; 3272 } 3273 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node); 3274 3275 static int qm_vf_q_assign(struct hisi_qm *qm, u32 num_vfs) 3276 { 3277 u32 remain_q_num, vfs_q_num, act_q_num, q_num, i, j; 3278 u32 max_qp_num = qm->max_qp_num; 3279 u32 q_base = qm->qp_num; 3280 int ret; 3281 3282 if (!num_vfs) 3283 return -EINVAL; 3284 3285 vfs_q_num = qm->ctrl_qp_num - qm->qp_num; 3286 3287 /* If vfs_q_num is less than num_vfs, return error. */ 3288 if (vfs_q_num < num_vfs) 3289 return -EINVAL; 3290 3291 q_num = vfs_q_num / num_vfs; 3292 remain_q_num = vfs_q_num % num_vfs; 3293 3294 for (i = num_vfs; i > 0; i--) { 3295 /* 3296 * if q_num + remain_q_num > max_qp_num in last vf, divide the 3297 * remaining queues equally. 3298 */ 3299 if (i == num_vfs && q_num + remain_q_num <= max_qp_num) { 3300 act_q_num = q_num + remain_q_num; 3301 remain_q_num = 0; 3302 } else if (remain_q_num > 0) { 3303 act_q_num = q_num + 1; 3304 remain_q_num--; 3305 } else { 3306 act_q_num = q_num; 3307 } 3308 3309 act_q_num = min_t(int, act_q_num, max_qp_num); 3310 ret = hisi_qm_set_vft(qm, i, q_base, act_q_num); 3311 if (ret) { 3312 for (j = num_vfs; j > i; j--) 3313 hisi_qm_set_vft(qm, j, 0, 0); 3314 return ret; 3315 } 3316 q_base += act_q_num; 3317 } 3318 3319 return 0; 3320 } 3321 3322 static int qm_clear_vft_config(struct hisi_qm *qm) 3323 { 3324 int ret; 3325 u32 i; 3326 3327 for (i = 1; i <= qm->vfs_num; i++) { 3328 ret = hisi_qm_set_vft(qm, i, 0, 0); 3329 if (ret) 3330 return ret; 3331 } 3332 qm->vfs_num = 0; 3333 3334 return 0; 3335 } 3336 3337 /** 3338 * hisi_qm_sriov_enable() - enable virtual functions 3339 * @pdev: the PCIe device 3340 * @max_vfs: the number of virtual functions to enable 3341 * 3342 * Returns the number of enabled VFs. If there are VFs enabled already or 3343 * max_vfs is more than the total number of device can be enabled, returns 3344 * failure. 3345 */ 3346 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs) 3347 { 3348 struct hisi_qm *qm = pci_get_drvdata(pdev); 3349 int pre_existing_vfs, num_vfs, total_vfs, ret; 3350 3351 total_vfs = pci_sriov_get_totalvfs(pdev); 3352 pre_existing_vfs = pci_num_vf(pdev); 3353 if (pre_existing_vfs) { 3354 pci_err(pdev, "%d VFs already enabled. Please disable pre-enabled VFs!\n", 3355 pre_existing_vfs); 3356 return 0; 3357 } 3358 3359 num_vfs = min_t(int, max_vfs, total_vfs); 3360 ret = qm_vf_q_assign(qm, num_vfs); 3361 if (ret) { 3362 pci_err(pdev, "Can't assign queues for VF!\n"); 3363 return ret; 3364 } 3365 3366 qm->vfs_num = num_vfs; 3367 3368 ret = pci_enable_sriov(pdev, num_vfs); 3369 if (ret) { 3370 pci_err(pdev, "Can't enable VF!\n"); 3371 qm_clear_vft_config(qm); 3372 return ret; 3373 } 3374 3375 pci_info(pdev, "VF enabled, vfs_num(=%d)!\n", num_vfs); 3376 3377 return num_vfs; 3378 } 3379 EXPORT_SYMBOL_GPL(hisi_qm_sriov_enable); 3380 3381 /** 3382 * hisi_qm_sriov_disable - disable virtual functions 3383 * @pdev: the PCI device. 3384 * @is_frozen: true when all the VFs are frozen. 3385 * 3386 * Return failure if there are VFs assigned already or VF is in used. 3387 */ 3388 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen) 3389 { 3390 struct hisi_qm *qm = pci_get_drvdata(pdev); 3391 3392 if (pci_vfs_assigned(pdev)) { 3393 pci_err(pdev, "Failed to disable VFs as VFs are assigned!\n"); 3394 return -EPERM; 3395 } 3396 3397 /* While VF is in used, SRIOV cannot be disabled. */ 3398 if (!is_frozen && qm_try_frozen_vfs(pdev, qm->qm_list)) { 3399 pci_err(pdev, "Task is using its VF!\n"); 3400 return -EBUSY; 3401 } 3402 3403 pci_disable_sriov(pdev); 3404 return qm_clear_vft_config(qm); 3405 } 3406 EXPORT_SYMBOL_GPL(hisi_qm_sriov_disable); 3407 3408 /** 3409 * hisi_qm_sriov_configure - configure the number of VFs 3410 * @pdev: The PCI device 3411 * @num_vfs: The number of VFs need enabled 3412 * 3413 * Enable SR-IOV according to num_vfs, 0 means disable. 3414 */ 3415 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs) 3416 { 3417 if (num_vfs == 0) 3418 return hisi_qm_sriov_disable(pdev, false); 3419 else 3420 return hisi_qm_sriov_enable(pdev, num_vfs); 3421 } 3422 EXPORT_SYMBOL_GPL(hisi_qm_sriov_configure); 3423 3424 static enum acc_err_result qm_dev_err_handle(struct hisi_qm *qm) 3425 { 3426 u32 err_sts; 3427 3428 if (!qm->err_ini->get_dev_hw_err_status) { 3429 dev_err(&qm->pdev->dev, "Device doesn't support get hw error status!\n"); 3430 return ACC_ERR_NONE; 3431 } 3432 3433 /* get device hardware error status */ 3434 err_sts = qm->err_ini->get_dev_hw_err_status(qm); 3435 if (err_sts) { 3436 if (err_sts & qm->err_ini->err_info.ecc_2bits_mask) 3437 qm->err_status.is_dev_ecc_mbit = true; 3438 3439 if (qm->err_ini->log_dev_hw_err) 3440 qm->err_ini->log_dev_hw_err(qm, err_sts); 3441 3442 /* ce error does not need to be reset */ 3443 if ((err_sts | qm->err_ini->err_info.dev_ce_mask) == 3444 qm->err_ini->err_info.dev_ce_mask) { 3445 if (qm->err_ini->clear_dev_hw_err_status) 3446 qm->err_ini->clear_dev_hw_err_status(qm, 3447 err_sts); 3448 3449 return ACC_ERR_RECOVERED; 3450 } 3451 3452 return ACC_ERR_NEED_RESET; 3453 } 3454 3455 return ACC_ERR_RECOVERED; 3456 } 3457 3458 static enum acc_err_result qm_process_dev_error(struct hisi_qm *qm) 3459 { 3460 enum acc_err_result qm_ret, dev_ret; 3461 3462 /* log qm error */ 3463 qm_ret = qm_hw_error_handle(qm); 3464 3465 /* log device error */ 3466 dev_ret = qm_dev_err_handle(qm); 3467 3468 return (qm_ret == ACC_ERR_NEED_RESET || 3469 dev_ret == ACC_ERR_NEED_RESET) ? 3470 ACC_ERR_NEED_RESET : ACC_ERR_RECOVERED; 3471 } 3472 3473 /** 3474 * hisi_qm_dev_err_detected() - Get device and qm error status then log it. 3475 * @pdev: The PCI device which need report error. 3476 * @state: The connectivity between CPU and device. 3477 * 3478 * We register this function into PCIe AER handlers, It will report device or 3479 * qm hardware error status when error occur. 3480 */ 3481 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev, 3482 pci_channel_state_t state) 3483 { 3484 struct hisi_qm *qm = pci_get_drvdata(pdev); 3485 enum acc_err_result ret; 3486 3487 if (pdev->is_virtfn) 3488 return PCI_ERS_RESULT_NONE; 3489 3490 pci_info(pdev, "PCI error detected, state(=%u)!!\n", state); 3491 if (state == pci_channel_io_perm_failure) 3492 return PCI_ERS_RESULT_DISCONNECT; 3493 3494 ret = qm_process_dev_error(qm); 3495 if (ret == ACC_ERR_NEED_RESET) 3496 return PCI_ERS_RESULT_NEED_RESET; 3497 3498 return PCI_ERS_RESULT_RECOVERED; 3499 } 3500 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected); 3501 3502 static u32 qm_get_hw_error_status(struct hisi_qm *qm) 3503 { 3504 return readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 3505 } 3506 3507 static int qm_check_req_recv(struct hisi_qm *qm) 3508 { 3509 struct pci_dev *pdev = qm->pdev; 3510 int ret; 3511 u32 val; 3512 3513 writel(ACC_VENDOR_ID_VALUE, qm->io_base + QM_PEH_VENDOR_ID); 3514 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val, 3515 (val == ACC_VENDOR_ID_VALUE), 3516 POLL_PERIOD, POLL_TIMEOUT); 3517 if (ret) { 3518 dev_err(&pdev->dev, "Fails to read QM reg!\n"); 3519 return ret; 3520 } 3521 3522 writel(PCI_VENDOR_ID_HUAWEI, qm->io_base + QM_PEH_VENDOR_ID); 3523 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val, 3524 (val == PCI_VENDOR_ID_HUAWEI), 3525 POLL_PERIOD, POLL_TIMEOUT); 3526 if (ret) 3527 dev_err(&pdev->dev, "Fails to read QM reg in the second time!\n"); 3528 3529 return ret; 3530 } 3531 3532 static int qm_set_pf_mse(struct hisi_qm *qm, bool set) 3533 { 3534 struct pci_dev *pdev = qm->pdev; 3535 u16 cmd; 3536 int i; 3537 3538 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 3539 if (set) 3540 cmd |= PCI_COMMAND_MEMORY; 3541 else 3542 cmd &= ~PCI_COMMAND_MEMORY; 3543 3544 pci_write_config_word(pdev, PCI_COMMAND, cmd); 3545 for (i = 0; i < MAX_WAIT_COUNTS; i++) { 3546 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 3547 if (set == ((cmd & PCI_COMMAND_MEMORY) >> 1)) 3548 return 0; 3549 3550 udelay(1); 3551 } 3552 3553 return -ETIMEDOUT; 3554 } 3555 3556 static int qm_set_vf_mse(struct hisi_qm *qm, bool set) 3557 { 3558 struct pci_dev *pdev = qm->pdev; 3559 u16 sriov_ctrl; 3560 int pos; 3561 int i; 3562 3563 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); 3564 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl); 3565 if (set) 3566 sriov_ctrl |= PCI_SRIOV_CTRL_MSE; 3567 else 3568 sriov_ctrl &= ~PCI_SRIOV_CTRL_MSE; 3569 pci_write_config_word(pdev, pos + PCI_SRIOV_CTRL, sriov_ctrl); 3570 3571 for (i = 0; i < MAX_WAIT_COUNTS; i++) { 3572 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl); 3573 if (set == (sriov_ctrl & PCI_SRIOV_CTRL_MSE) >> 3574 ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT) 3575 return 0; 3576 3577 udelay(1); 3578 } 3579 3580 return -ETIMEDOUT; 3581 } 3582 3583 static int qm_set_msi(struct hisi_qm *qm, bool set) 3584 { 3585 struct pci_dev *pdev = qm->pdev; 3586 3587 if (set) { 3588 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64, 3589 0); 3590 } else { 3591 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64, 3592 ACC_PEH_MSI_DISABLE); 3593 if (qm->err_status.is_qm_ecc_mbit || 3594 qm->err_status.is_dev_ecc_mbit) 3595 return 0; 3596 3597 mdelay(1); 3598 if (readl(qm->io_base + QM_PEH_DFX_INFO0)) 3599 return -EFAULT; 3600 } 3601 3602 return 0; 3603 } 3604 3605 static int qm_vf_reset_prepare(struct hisi_qm *qm, 3606 enum qm_stop_reason stop_reason) 3607 { 3608 struct hisi_qm_list *qm_list = qm->qm_list; 3609 struct pci_dev *pdev = qm->pdev; 3610 struct pci_dev *virtfn; 3611 struct hisi_qm *vf_qm; 3612 int ret = 0; 3613 3614 mutex_lock(&qm_list->lock); 3615 list_for_each_entry(vf_qm, &qm_list->list, list) { 3616 virtfn = vf_qm->pdev; 3617 if (virtfn == pdev) 3618 continue; 3619 3620 if (pci_physfn(virtfn) == pdev) { 3621 /* save VFs PCIE BAR configuration */ 3622 pci_save_state(virtfn); 3623 3624 ret = hisi_qm_stop(vf_qm, stop_reason); 3625 if (ret) 3626 goto stop_fail; 3627 } 3628 } 3629 3630 stop_fail: 3631 mutex_unlock(&qm_list->lock); 3632 return ret; 3633 } 3634 3635 static int qm_reset_prepare_ready(struct hisi_qm *qm) 3636 { 3637 struct pci_dev *pdev = qm->pdev; 3638 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 3639 int delay = 0; 3640 3641 /* All reset requests need to be queued for processing */ 3642 while (test_and_set_bit(QM_RESETTING, &pf_qm->misc_ctl)) { 3643 msleep(++delay); 3644 if (delay > QM_RESET_WAIT_TIMEOUT) 3645 return -EBUSY; 3646 } 3647 3648 return 0; 3649 } 3650 3651 static int qm_controller_reset_prepare(struct hisi_qm *qm) 3652 { 3653 struct pci_dev *pdev = qm->pdev; 3654 int ret; 3655 3656 ret = qm_reset_prepare_ready(qm); 3657 if (ret) { 3658 pci_err(pdev, "Controller reset not ready!\n"); 3659 return ret; 3660 } 3661 3662 if (qm->vfs_num) { 3663 ret = qm_vf_reset_prepare(qm, QM_SOFT_RESET); 3664 if (ret) { 3665 pci_err(pdev, "Fails to stop VFs!\n"); 3666 clear_bit(QM_RESETTING, &qm->misc_ctl); 3667 return ret; 3668 } 3669 } 3670 3671 ret = hisi_qm_stop(qm, QM_SOFT_RESET); 3672 if (ret) { 3673 pci_err(pdev, "Fails to stop QM!\n"); 3674 clear_bit(QM_RESETTING, &qm->misc_ctl); 3675 return ret; 3676 } 3677 3678 clear_bit(QM_RST_SCHED, &qm->misc_ctl); 3679 3680 return 0; 3681 } 3682 3683 static void qm_dev_ecc_mbit_handle(struct hisi_qm *qm) 3684 { 3685 u32 nfe_enb = 0; 3686 3687 if (!qm->err_status.is_dev_ecc_mbit && 3688 qm->err_status.is_qm_ecc_mbit && 3689 qm->err_ini->close_axi_master_ooo) { 3690 3691 qm->err_ini->close_axi_master_ooo(qm); 3692 3693 } else if (qm->err_status.is_dev_ecc_mbit && 3694 !qm->err_status.is_qm_ecc_mbit && 3695 !qm->err_ini->close_axi_master_ooo) { 3696 3697 nfe_enb = readl(qm->io_base + QM_RAS_NFE_ENABLE); 3698 writel(nfe_enb & QM_RAS_NFE_MBIT_DISABLE, 3699 qm->io_base + QM_RAS_NFE_ENABLE); 3700 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SET); 3701 } 3702 } 3703 3704 static int qm_soft_reset(struct hisi_qm *qm) 3705 { 3706 struct pci_dev *pdev = qm->pdev; 3707 int ret; 3708 u32 val; 3709 3710 /* Ensure all doorbells and mailboxes received by QM */ 3711 ret = qm_check_req_recv(qm); 3712 if (ret) 3713 return ret; 3714 3715 if (qm->vfs_num) { 3716 ret = qm_set_vf_mse(qm, false); 3717 if (ret) { 3718 pci_err(pdev, "Fails to disable vf MSE bit.\n"); 3719 return ret; 3720 } 3721 } 3722 3723 ret = qm_set_msi(qm, false); 3724 if (ret) { 3725 pci_err(pdev, "Fails to disable PEH MSI bit.\n"); 3726 return ret; 3727 } 3728 3729 qm_dev_ecc_mbit_handle(qm); 3730 3731 /* OOO register set and check */ 3732 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN, 3733 qm->io_base + ACC_MASTER_GLOBAL_CTRL); 3734 3735 /* If bus lock, reset chip */ 3736 ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN, 3737 val, 3738 (val == ACC_MASTER_TRANS_RETURN_RW), 3739 POLL_PERIOD, POLL_TIMEOUT); 3740 if (ret) { 3741 pci_emerg(pdev, "Bus lock! Please reset system.\n"); 3742 return ret; 3743 } 3744 3745 ret = qm_set_pf_mse(qm, false); 3746 if (ret) { 3747 pci_err(pdev, "Fails to disable pf MSE bit.\n"); 3748 return ret; 3749 } 3750 3751 /* The reset related sub-control registers are not in PCI BAR */ 3752 if (ACPI_HANDLE(&pdev->dev)) { 3753 unsigned long long value = 0; 3754 acpi_status s; 3755 3756 s = acpi_evaluate_integer(ACPI_HANDLE(&pdev->dev), 3757 qm->err_ini->err_info.acpi_rst, 3758 NULL, &value); 3759 if (ACPI_FAILURE(s)) { 3760 pci_err(pdev, "NO controller reset method!\n"); 3761 return -EIO; 3762 } 3763 3764 if (value) { 3765 pci_err(pdev, "Reset step %llu failed!\n", value); 3766 return -EIO; 3767 } 3768 } else { 3769 pci_err(pdev, "No reset method!\n"); 3770 return -EINVAL; 3771 } 3772 3773 return 0; 3774 } 3775 3776 static int qm_vf_reset_done(struct hisi_qm *qm) 3777 { 3778 struct hisi_qm_list *qm_list = qm->qm_list; 3779 struct pci_dev *pdev = qm->pdev; 3780 struct pci_dev *virtfn; 3781 struct hisi_qm *vf_qm; 3782 int ret = 0; 3783 3784 mutex_lock(&qm_list->lock); 3785 list_for_each_entry(vf_qm, &qm_list->list, list) { 3786 virtfn = vf_qm->pdev; 3787 if (virtfn == pdev) 3788 continue; 3789 3790 if (pci_physfn(virtfn) == pdev) { 3791 /* enable VFs PCIE BAR configuration */ 3792 pci_restore_state(virtfn); 3793 3794 ret = qm_restart(vf_qm); 3795 if (ret) 3796 goto restart_fail; 3797 } 3798 } 3799 3800 restart_fail: 3801 mutex_unlock(&qm_list->lock); 3802 return ret; 3803 } 3804 3805 static u32 qm_get_dev_err_status(struct hisi_qm *qm) 3806 { 3807 return qm->err_ini->get_dev_hw_err_status(qm); 3808 } 3809 3810 static int qm_dev_hw_init(struct hisi_qm *qm) 3811 { 3812 return qm->err_ini->hw_init(qm); 3813 } 3814 3815 static void qm_restart_prepare(struct hisi_qm *qm) 3816 { 3817 u32 value; 3818 3819 if (!qm->err_status.is_qm_ecc_mbit && 3820 !qm->err_status.is_dev_ecc_mbit) 3821 return; 3822 3823 /* temporarily close the OOO port used for PEH to write out MSI */ 3824 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN); 3825 writel(value & ~qm->err_ini->err_info.msi_wr_port, 3826 qm->io_base + ACC_AM_CFG_PORT_WR_EN); 3827 3828 /* clear dev ecc 2bit error source if having */ 3829 value = qm_get_dev_err_status(qm) & 3830 qm->err_ini->err_info.ecc_2bits_mask; 3831 if (value && qm->err_ini->clear_dev_hw_err_status) 3832 qm->err_ini->clear_dev_hw_err_status(qm, value); 3833 3834 /* clear QM ecc mbit error source */ 3835 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SOURCE); 3836 3837 /* clear AM Reorder Buffer ecc mbit source */ 3838 writel(ACC_ROB_ECC_ERR_MULTPL, qm->io_base + ACC_AM_ROB_ECC_INT_STS); 3839 3840 if (qm->err_ini->open_axi_master_ooo) 3841 qm->err_ini->open_axi_master_ooo(qm); 3842 } 3843 3844 static void qm_restart_done(struct hisi_qm *qm) 3845 { 3846 u32 value; 3847 3848 if (!qm->err_status.is_qm_ecc_mbit && 3849 !qm->err_status.is_dev_ecc_mbit) 3850 return; 3851 3852 /* open the OOO port for PEH to write out MSI */ 3853 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN); 3854 value |= qm->err_ini->err_info.msi_wr_port; 3855 writel(value, qm->io_base + ACC_AM_CFG_PORT_WR_EN); 3856 3857 qm->err_status.is_qm_ecc_mbit = false; 3858 qm->err_status.is_dev_ecc_mbit = false; 3859 } 3860 3861 static int qm_controller_reset_done(struct hisi_qm *qm) 3862 { 3863 struct pci_dev *pdev = qm->pdev; 3864 int ret; 3865 3866 ret = qm_set_msi(qm, true); 3867 if (ret) { 3868 pci_err(pdev, "Fails to enable PEH MSI bit!\n"); 3869 return ret; 3870 } 3871 3872 ret = qm_set_pf_mse(qm, true); 3873 if (ret) { 3874 pci_err(pdev, "Fails to enable pf MSE bit!\n"); 3875 return ret; 3876 } 3877 3878 if (qm->vfs_num) { 3879 ret = qm_set_vf_mse(qm, true); 3880 if (ret) { 3881 pci_err(pdev, "Fails to enable vf MSE bit!\n"); 3882 return ret; 3883 } 3884 } 3885 3886 ret = qm_dev_hw_init(qm); 3887 if (ret) { 3888 pci_err(pdev, "Failed to init device\n"); 3889 return ret; 3890 } 3891 3892 qm_restart_prepare(qm); 3893 3894 ret = qm_restart(qm); 3895 if (ret) { 3896 pci_err(pdev, "Failed to start QM!\n"); 3897 return ret; 3898 } 3899 3900 if (qm->vfs_num) { 3901 ret = qm_vf_q_assign(qm, qm->vfs_num); 3902 if (ret) { 3903 pci_err(pdev, "Failed to assign queue!\n"); 3904 return ret; 3905 } 3906 } 3907 3908 ret = qm_vf_reset_done(qm); 3909 if (ret) { 3910 pci_err(pdev, "Failed to start VFs!\n"); 3911 return -EPERM; 3912 } 3913 3914 hisi_qm_dev_err_init(qm); 3915 qm_restart_done(qm); 3916 3917 clear_bit(QM_RESETTING, &qm->misc_ctl); 3918 3919 return 0; 3920 } 3921 3922 static int qm_controller_reset(struct hisi_qm *qm) 3923 { 3924 struct pci_dev *pdev = qm->pdev; 3925 int ret; 3926 3927 pci_info(pdev, "Controller resetting...\n"); 3928 3929 ret = qm_controller_reset_prepare(qm); 3930 if (ret) { 3931 clear_bit(QM_RST_SCHED, &qm->misc_ctl); 3932 return ret; 3933 } 3934 3935 ret = qm_soft_reset(qm); 3936 if (ret) { 3937 pci_err(pdev, "Controller reset failed (%d)\n", ret); 3938 clear_bit(QM_RESETTING, &qm->misc_ctl); 3939 return ret; 3940 } 3941 3942 ret = qm_controller_reset_done(qm); 3943 if (ret) { 3944 clear_bit(QM_RESETTING, &qm->misc_ctl); 3945 return ret; 3946 } 3947 3948 pci_info(pdev, "Controller reset complete\n"); 3949 3950 return 0; 3951 } 3952 3953 /** 3954 * hisi_qm_dev_slot_reset() - slot reset 3955 * @pdev: the PCIe device 3956 * 3957 * This function offers QM relate PCIe device reset interface. Drivers which 3958 * use QM can use this function as slot_reset in its struct pci_error_handlers. 3959 */ 3960 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev) 3961 { 3962 struct hisi_qm *qm = pci_get_drvdata(pdev); 3963 int ret; 3964 3965 if (pdev->is_virtfn) 3966 return PCI_ERS_RESULT_RECOVERED; 3967 3968 pci_aer_clear_nonfatal_status(pdev); 3969 3970 /* reset pcie device controller */ 3971 ret = qm_controller_reset(qm); 3972 if (ret) { 3973 pci_err(pdev, "Controller reset failed (%d)\n", ret); 3974 return PCI_ERS_RESULT_DISCONNECT; 3975 } 3976 3977 return PCI_ERS_RESULT_RECOVERED; 3978 } 3979 EXPORT_SYMBOL_GPL(hisi_qm_dev_slot_reset); 3980 3981 /* check the interrupt is ecc-mbit error or not */ 3982 static int qm_check_dev_error(struct hisi_qm *qm) 3983 { 3984 int ret; 3985 3986 if (qm->fun_type == QM_HW_VF) 3987 return 0; 3988 3989 ret = qm_get_hw_error_status(qm) & QM_ECC_MBIT; 3990 if (ret) 3991 return ret; 3992 3993 return (qm_get_dev_err_status(qm) & 3994 qm->err_ini->err_info.ecc_2bits_mask); 3995 } 3996 3997 void hisi_qm_reset_prepare(struct pci_dev *pdev) 3998 { 3999 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 4000 struct hisi_qm *qm = pci_get_drvdata(pdev); 4001 u32 delay = 0; 4002 int ret; 4003 4004 hisi_qm_dev_err_uninit(pf_qm); 4005 4006 /* 4007 * Check whether there is an ECC mbit error, If it occurs, need to 4008 * wait for soft reset to fix it. 4009 */ 4010 while (qm_check_dev_error(pf_qm)) { 4011 msleep(++delay); 4012 if (delay > QM_RESET_WAIT_TIMEOUT) 4013 return; 4014 } 4015 4016 ret = qm_reset_prepare_ready(qm); 4017 if (ret) { 4018 pci_err(pdev, "FLR not ready!\n"); 4019 return; 4020 } 4021 4022 if (qm->vfs_num) { 4023 ret = qm_vf_reset_prepare(qm, QM_FLR); 4024 if (ret) { 4025 pci_err(pdev, "Failed to prepare reset, ret = %d.\n", 4026 ret); 4027 return; 4028 } 4029 } 4030 4031 ret = hisi_qm_stop(qm, QM_FLR); 4032 if (ret) { 4033 pci_err(pdev, "Failed to stop QM, ret = %d.\n", ret); 4034 return; 4035 } 4036 4037 pci_info(pdev, "FLR resetting...\n"); 4038 } 4039 EXPORT_SYMBOL_GPL(hisi_qm_reset_prepare); 4040 4041 static bool qm_flr_reset_complete(struct pci_dev *pdev) 4042 { 4043 struct pci_dev *pf_pdev = pci_physfn(pdev); 4044 struct hisi_qm *qm = pci_get_drvdata(pf_pdev); 4045 u32 id; 4046 4047 pci_read_config_dword(qm->pdev, PCI_COMMAND, &id); 4048 if (id == QM_PCI_COMMAND_INVALID) { 4049 pci_err(pdev, "Device can not be used!\n"); 4050 return false; 4051 } 4052 4053 return true; 4054 } 4055 4056 void hisi_qm_reset_done(struct pci_dev *pdev) 4057 { 4058 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 4059 struct hisi_qm *qm = pci_get_drvdata(pdev); 4060 int ret; 4061 4062 hisi_qm_dev_err_init(pf_qm); 4063 4064 ret = qm_restart(qm); 4065 if (ret) { 4066 pci_err(pdev, "Failed to start QM, ret = %d.\n", ret); 4067 goto flr_done; 4068 } 4069 4070 if (qm->fun_type == QM_HW_PF) { 4071 ret = qm_dev_hw_init(qm); 4072 if (ret) { 4073 pci_err(pdev, "Failed to init PF, ret = %d.\n", ret); 4074 goto flr_done; 4075 } 4076 4077 if (!qm->vfs_num) 4078 goto flr_done; 4079 4080 ret = qm_vf_q_assign(qm, qm->vfs_num); 4081 if (ret) { 4082 pci_err(pdev, "Failed to assign VFs, ret = %d.\n", ret); 4083 goto flr_done; 4084 } 4085 4086 ret = qm_vf_reset_done(qm); 4087 if (ret) { 4088 pci_err(pdev, "Failed to start VFs, ret = %d.\n", ret); 4089 goto flr_done; 4090 } 4091 } 4092 4093 flr_done: 4094 if (qm_flr_reset_complete(pdev)) 4095 pci_info(pdev, "FLR reset complete\n"); 4096 4097 clear_bit(QM_RESETTING, &qm->misc_ctl); 4098 } 4099 EXPORT_SYMBOL_GPL(hisi_qm_reset_done); 4100 4101 static irqreturn_t qm_abnormal_irq(int irq, void *data) 4102 { 4103 struct hisi_qm *qm = data; 4104 enum acc_err_result ret; 4105 4106 atomic64_inc(&qm->debug.dfx.abnormal_irq_cnt); 4107 ret = qm_process_dev_error(qm); 4108 if (ret == ACC_ERR_NEED_RESET && 4109 !test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl) && 4110 !test_and_set_bit(QM_RST_SCHED, &qm->misc_ctl)) 4111 schedule_work(&qm->rst_work); 4112 4113 return IRQ_HANDLED; 4114 } 4115 4116 static int qm_irq_register(struct hisi_qm *qm) 4117 { 4118 struct pci_dev *pdev = qm->pdev; 4119 int ret; 4120 4121 ret = request_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), 4122 qm_irq, 0, qm->dev_name, qm); 4123 if (ret) 4124 return ret; 4125 4126 if (qm->ver != QM_HW_V1) { 4127 ret = request_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), 4128 qm_aeq_irq, 0, qm->dev_name, qm); 4129 if (ret) 4130 goto err_aeq_irq; 4131 4132 if (qm->fun_type == QM_HW_PF) { 4133 ret = request_irq(pci_irq_vector(pdev, 4134 QM_ABNORMAL_EVENT_IRQ_VECTOR), 4135 qm_abnormal_irq, 0, qm->dev_name, qm); 4136 if (ret) 4137 goto err_abonormal_irq; 4138 } 4139 } 4140 4141 return 0; 4142 4143 err_abonormal_irq: 4144 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm); 4145 err_aeq_irq: 4146 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm); 4147 return ret; 4148 } 4149 4150 /** 4151 * hisi_qm_dev_shutdown() - Shutdown device. 4152 * @pdev: The device will be shutdown. 4153 * 4154 * This function will stop qm when OS shutdown or rebooting. 4155 */ 4156 void hisi_qm_dev_shutdown(struct pci_dev *pdev) 4157 { 4158 struct hisi_qm *qm = pci_get_drvdata(pdev); 4159 int ret; 4160 4161 ret = hisi_qm_stop(qm, QM_NORMAL); 4162 if (ret) 4163 dev_err(&pdev->dev, "Fail to stop qm in shutdown!\n"); 4164 } 4165 EXPORT_SYMBOL_GPL(hisi_qm_dev_shutdown); 4166 4167 static void hisi_qm_controller_reset(struct work_struct *rst_work) 4168 { 4169 struct hisi_qm *qm = container_of(rst_work, struct hisi_qm, rst_work); 4170 int ret; 4171 4172 /* reset pcie device controller */ 4173 ret = qm_controller_reset(qm); 4174 if (ret) 4175 dev_err(&qm->pdev->dev, "controller reset failed (%d)\n", ret); 4176 4177 } 4178 4179 /** 4180 * hisi_qm_alg_register() - Register alg to crypto and add qm to qm_list. 4181 * @qm: The qm needs add. 4182 * @qm_list: The qm list. 4183 * 4184 * This function adds qm to qm list, and will register algorithm to 4185 * crypto when the qm list is empty. 4186 */ 4187 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 4188 { 4189 int flag = 0; 4190 int ret = 0; 4191 /* HW V2 not support both use uacce sva mode and hardware crypto algs */ 4192 if (qm->ver <= QM_HW_V2 && qm->use_sva) 4193 return 0; 4194 4195 mutex_lock(&qm_list->lock); 4196 if (list_empty(&qm_list->list)) 4197 flag = 1; 4198 list_add_tail(&qm->list, &qm_list->list); 4199 mutex_unlock(&qm_list->lock); 4200 4201 if (flag) { 4202 ret = qm_list->register_to_crypto(qm); 4203 if (ret) { 4204 mutex_lock(&qm_list->lock); 4205 list_del(&qm->list); 4206 mutex_unlock(&qm_list->lock); 4207 } 4208 } 4209 4210 return ret; 4211 } 4212 EXPORT_SYMBOL_GPL(hisi_qm_alg_register); 4213 4214 /** 4215 * hisi_qm_alg_unregister() - Unregister alg from crypto and delete qm from 4216 * qm list. 4217 * @qm: The qm needs delete. 4218 * @qm_list: The qm list. 4219 * 4220 * This function deletes qm from qm list, and will unregister algorithm 4221 * from crypto when the qm list is empty. 4222 */ 4223 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 4224 { 4225 if (qm->ver <= QM_HW_V2 && qm->use_sva) 4226 return; 4227 4228 mutex_lock(&qm_list->lock); 4229 list_del(&qm->list); 4230 mutex_unlock(&qm_list->lock); 4231 4232 if (list_empty(&qm_list->list)) 4233 qm_list->unregister_from_crypto(qm); 4234 } 4235 EXPORT_SYMBOL_GPL(hisi_qm_alg_unregister); 4236 4237 static int qm_get_qp_num(struct hisi_qm *qm) 4238 { 4239 if (qm->ver == QM_HW_V1) 4240 qm->ctrl_qp_num = QM_QNUM_V1; 4241 else if (qm->ver == QM_HW_V2) 4242 qm->ctrl_qp_num = QM_QNUM_V2; 4243 else 4244 qm->ctrl_qp_num = readl(qm->io_base + QM_CAPBILITY) & 4245 QM_QP_NUN_MASK; 4246 4247 if (qm->use_db_isolation) 4248 qm->max_qp_num = (readl(qm->io_base + QM_CAPBILITY) >> 4249 QM_QP_MAX_NUM_SHIFT) & QM_QP_NUN_MASK; 4250 else 4251 qm->max_qp_num = qm->ctrl_qp_num; 4252 4253 /* check if qp number is valid */ 4254 if (qm->qp_num > qm->max_qp_num) { 4255 dev_err(&qm->pdev->dev, "qp num(%u) is more than max qp num(%u)!\n", 4256 qm->qp_num, qm->max_qp_num); 4257 return -EINVAL; 4258 } 4259 4260 return 0; 4261 } 4262 4263 static int qm_get_pci_res(struct hisi_qm *qm) 4264 { 4265 struct pci_dev *pdev = qm->pdev; 4266 struct device *dev = &pdev->dev; 4267 int ret; 4268 4269 ret = pci_request_mem_regions(pdev, qm->dev_name); 4270 if (ret < 0) { 4271 dev_err(dev, "Failed to request mem regions!\n"); 4272 return ret; 4273 } 4274 4275 qm->phys_base = pci_resource_start(pdev, PCI_BAR_2); 4276 qm->io_base = ioremap(qm->phys_base, pci_resource_len(pdev, PCI_BAR_2)); 4277 if (!qm->io_base) { 4278 ret = -EIO; 4279 goto err_request_mem_regions; 4280 } 4281 4282 if (qm->ver > QM_HW_V2) { 4283 if (qm->fun_type == QM_HW_PF) 4284 qm->use_db_isolation = readl(qm->io_base + 4285 QM_QUE_ISO_EN) & BIT(0); 4286 else 4287 qm->use_db_isolation = readl(qm->io_base + 4288 QM_QUE_ISO_CFG_V) & BIT(0); 4289 } 4290 4291 if (qm->use_db_isolation) { 4292 qm->db_interval = QM_QP_DB_INTERVAL; 4293 qm->db_phys_base = pci_resource_start(pdev, PCI_BAR_4); 4294 qm->db_io_base = ioremap(qm->db_phys_base, 4295 pci_resource_len(pdev, PCI_BAR_4)); 4296 if (!qm->db_io_base) { 4297 ret = -EIO; 4298 goto err_ioremap; 4299 } 4300 } else { 4301 qm->db_phys_base = qm->phys_base; 4302 qm->db_io_base = qm->io_base; 4303 qm->db_interval = 0; 4304 } 4305 4306 if (qm->fun_type == QM_HW_PF) { 4307 ret = qm_get_qp_num(qm); 4308 if (ret) 4309 goto err_db_ioremap; 4310 } 4311 4312 return 0; 4313 4314 err_db_ioremap: 4315 if (qm->use_db_isolation) 4316 iounmap(qm->db_io_base); 4317 err_ioremap: 4318 iounmap(qm->io_base); 4319 err_request_mem_regions: 4320 pci_release_mem_regions(pdev); 4321 return ret; 4322 } 4323 4324 static int hisi_qm_pci_init(struct hisi_qm *qm) 4325 { 4326 struct pci_dev *pdev = qm->pdev; 4327 struct device *dev = &pdev->dev; 4328 unsigned int num_vec; 4329 int ret; 4330 4331 ret = pci_enable_device_mem(pdev); 4332 if (ret < 0) { 4333 dev_err(dev, "Failed to enable device mem!\n"); 4334 return ret; 4335 } 4336 4337 ret = qm_get_pci_res(qm); 4338 if (ret) 4339 goto err_disable_pcidev; 4340 4341 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 4342 if (ret < 0) 4343 goto err_get_pci_res; 4344 pci_set_master(pdev); 4345 4346 if (!qm->ops->get_irq_num) { 4347 ret = -EOPNOTSUPP; 4348 goto err_get_pci_res; 4349 } 4350 num_vec = qm->ops->get_irq_num(qm); 4351 ret = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSI); 4352 if (ret < 0) { 4353 dev_err(dev, "Failed to enable MSI vectors!\n"); 4354 goto err_get_pci_res; 4355 } 4356 4357 return 0; 4358 4359 err_get_pci_res: 4360 qm_put_pci_res(qm); 4361 err_disable_pcidev: 4362 pci_disable_device(pdev); 4363 return ret; 4364 } 4365 4366 /** 4367 * hisi_qm_init() - Initialize configures about qm. 4368 * @qm: The qm needing init. 4369 * 4370 * This function init qm, then we can call hisi_qm_start to put qm into work. 4371 */ 4372 int hisi_qm_init(struct hisi_qm *qm) 4373 { 4374 struct pci_dev *pdev = qm->pdev; 4375 struct device *dev = &pdev->dev; 4376 int ret; 4377 4378 hisi_qm_pre_init(qm); 4379 4380 ret = hisi_qm_pci_init(qm); 4381 if (ret) 4382 return ret; 4383 4384 ret = qm_irq_register(qm); 4385 if (ret) 4386 goto err_pci_init; 4387 4388 if (qm->fun_type == QM_HW_VF && qm->ver != QM_HW_V1) { 4389 /* v2 starts to support get vft by mailbox */ 4390 ret = hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num); 4391 if (ret) 4392 goto err_irq_register; 4393 } 4394 4395 ret = qm_alloc_uacce(qm); 4396 if (ret < 0) 4397 dev_warn(dev, "fail to alloc uacce (%d)\n", ret); 4398 4399 ret = hisi_qm_memory_init(qm); 4400 if (ret) 4401 goto err_alloc_uacce; 4402 4403 INIT_WORK(&qm->work, qm_work_process); 4404 if (qm->fun_type == QM_HW_PF) 4405 INIT_WORK(&qm->rst_work, hisi_qm_controller_reset); 4406 4407 atomic_set(&qm->status.flags, QM_INIT); 4408 4409 return 0; 4410 4411 err_alloc_uacce: 4412 uacce_remove(qm->uacce); 4413 qm->uacce = NULL; 4414 err_irq_register: 4415 qm_irq_unregister(qm); 4416 err_pci_init: 4417 hisi_qm_pci_uninit(qm); 4418 return ret; 4419 } 4420 EXPORT_SYMBOL_GPL(hisi_qm_init); 4421 4422 MODULE_LICENSE("GPL v2"); 4423 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 4424 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver"); 4425