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/bitmap.h> 6 #include <linux/dma-mapping.h> 7 #include <linux/idr.h> 8 #include <linux/io.h> 9 #include <linux/irqreturn.h> 10 #include <linux/log2.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/seq_file.h> 13 #include <linux/slab.h> 14 #include <linux/uacce.h> 15 #include <linux/uaccess.h> 16 #include <uapi/misc/uacce/hisi_qm.h> 17 #include <linux/hisi_acc_qm.h> 18 #include "qm_common.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 26 #define QM_IRQ_VECTOR_MASK GENMASK(15, 0) 27 #define QM_IRQ_TYPE_MASK GENMASK(15, 0) 28 #define QM_IRQ_TYPE_SHIFT 16 29 #define QM_ABN_IRQ_TYPE_MASK GENMASK(7, 0) 30 31 /* mailbox */ 32 #define QM_MB_PING_ALL_VFS 0xffff 33 #define QM_MB_CMD_DATA_SHIFT 32 34 #define QM_MB_CMD_DATA_MASK GENMASK(31, 0) 35 #define QM_MB_STATUS_MASK GENMASK(12, 9) 36 37 /* sqc shift */ 38 #define QM_SQ_HOP_NUM_SHIFT 0 39 #define QM_SQ_PAGE_SIZE_SHIFT 4 40 #define QM_SQ_BUF_SIZE_SHIFT 8 41 #define QM_SQ_SQE_SIZE_SHIFT 12 42 #define QM_SQ_PRIORITY_SHIFT 0 43 #define QM_SQ_ORDERS_SHIFT 4 44 #define QM_SQ_TYPE_SHIFT 8 45 #define QM_QC_PASID_ENABLE 0x1 46 #define QM_QC_PASID_ENABLE_SHIFT 7 47 48 #define QM_SQ_TYPE_MASK GENMASK(3, 0) 49 #define QM_SQ_TAIL_IDX(sqc) ((le16_to_cpu((sqc)->w11) >> 6) & 0x1) 50 51 /* cqc shift */ 52 #define QM_CQ_HOP_NUM_SHIFT 0 53 #define QM_CQ_PAGE_SIZE_SHIFT 4 54 #define QM_CQ_BUF_SIZE_SHIFT 8 55 #define QM_CQ_CQE_SIZE_SHIFT 12 56 #define QM_CQ_PHASE_SHIFT 0 57 #define QM_CQ_FLAG_SHIFT 1 58 59 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1) 60 #define QM_QC_CQE_SIZE 4 61 #define QM_CQ_TAIL_IDX(cqc) ((le16_to_cpu((cqc)->w11) >> 6) & 0x1) 62 63 /* eqc shift */ 64 #define QM_EQE_AEQE_SIZE (2UL << 12) 65 #define QM_EQC_PHASE_SHIFT 16 66 67 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1) 68 #define QM_EQE_CQN_MASK GENMASK(15, 0) 69 70 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1) 71 #define QM_AEQE_TYPE_SHIFT 17 72 #define QM_AEQE_CQN_MASK GENMASK(15, 0) 73 #define QM_CQ_OVERFLOW 0 74 #define QM_EQ_OVERFLOW 1 75 #define QM_CQE_ERROR 2 76 77 #define QM_XQ_DEPTH_SHIFT 16 78 #define QM_XQ_DEPTH_MASK GENMASK(15, 0) 79 80 #define QM_DOORBELL_CMD_SQ 0 81 #define QM_DOORBELL_CMD_CQ 1 82 #define QM_DOORBELL_CMD_EQ 2 83 #define QM_DOORBELL_CMD_AEQ 3 84 85 #define QM_DOORBELL_BASE_V1 0x340 86 #define QM_DB_CMD_SHIFT_V1 16 87 #define QM_DB_INDEX_SHIFT_V1 32 88 #define QM_DB_PRIORITY_SHIFT_V1 48 89 #define QM_PAGE_SIZE 0x0034 90 #define QM_QP_DB_INTERVAL 0x10000 91 #define QM_DB_TIMEOUT_CFG 0x100074 92 #define QM_DB_TIMEOUT_SET 0x1fffff 93 94 #define QM_MEM_START_INIT 0x100040 95 #define QM_MEM_INIT_DONE 0x100044 96 #define QM_VFT_CFG_RDY 0x10006c 97 #define QM_VFT_CFG_OP_WR 0x100058 98 #define QM_VFT_CFG_TYPE 0x10005c 99 #define QM_VFT_CFG 0x100060 100 #define QM_VFT_CFG_OP_ENABLE 0x100054 101 #define QM_PM_CTRL 0x100148 102 #define QM_IDLE_DISABLE BIT(9) 103 104 #define QM_VFT_CFG_DATA_L 0x100064 105 #define QM_VFT_CFG_DATA_H 0x100068 106 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8) 107 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12) 108 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16) 109 #define QM_SQC_VFT_START_SQN_SHIFT 28 110 #define QM_SQC_VFT_VALID (1ULL << 44) 111 #define QM_SQC_VFT_SQN_SHIFT 45 112 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8) 113 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12) 114 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16) 115 #define QM_CQC_VFT_VALID (1ULL << 28) 116 117 #define QM_SQC_VFT_BASE_SHIFT_V2 28 118 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(15, 0) 119 #define QM_SQC_VFT_NUM_SHIFT_V2 45 120 #define QM_SQC_VFT_NUM_MASK_V2 GENMASK(9, 0) 121 122 #define QM_ABNORMAL_INT_SOURCE 0x100000 123 #define QM_ABNORMAL_INT_MASK 0x100004 124 #define QM_ABNORMAL_INT_MASK_VALUE 0x7fff 125 #define QM_ABNORMAL_INT_STATUS 0x100008 126 #define QM_ABNORMAL_INT_SET 0x10000c 127 #define QM_ABNORMAL_INF00 0x100010 128 #define QM_FIFO_OVERFLOW_TYPE 0xc0 129 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6 130 #define QM_FIFO_OVERFLOW_VF 0x3f 131 #define QM_ABNORMAL_INF01 0x100014 132 #define QM_DB_TIMEOUT_TYPE 0xc0 133 #define QM_DB_TIMEOUT_TYPE_SHIFT 6 134 #define QM_DB_TIMEOUT_VF 0x3f 135 #define QM_RAS_CE_ENABLE 0x1000ec 136 #define QM_RAS_FE_ENABLE 0x1000f0 137 #define QM_RAS_NFE_ENABLE 0x1000f4 138 #define QM_RAS_CE_THRESHOLD 0x1000f8 139 #define QM_RAS_CE_TIMES_PER_IRQ 1 140 #define QM_OOO_SHUTDOWN_SEL 0x1040f8 141 #define QM_ECC_MBIT BIT(2) 142 #define QM_DB_TIMEOUT BIT(10) 143 #define QM_OF_FIFO_OF BIT(11) 144 145 #define QM_RESET_WAIT_TIMEOUT 400 146 #define QM_PEH_VENDOR_ID 0x1000d8 147 #define ACC_VENDOR_ID_VALUE 0x5a5a 148 #define QM_PEH_DFX_INFO0 0x1000fc 149 #define QM_PEH_DFX_INFO1 0x100100 150 #define QM_PEH_DFX_MASK (BIT(0) | BIT(2)) 151 #define QM_PEH_MSI_FINISH_MASK GENMASK(19, 16) 152 #define ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT 3 153 #define ACC_PEH_MSI_DISABLE GENMASK(31, 0) 154 #define ACC_MASTER_GLOBAL_CTRL_SHUTDOWN 0x1 155 #define ACC_MASTER_TRANS_RETURN_RW 3 156 #define ACC_MASTER_TRANS_RETURN 0x300150 157 #define ACC_MASTER_GLOBAL_CTRL 0x300000 158 #define ACC_AM_CFG_PORT_WR_EN 0x30001c 159 #define QM_RAS_NFE_MBIT_DISABLE ~QM_ECC_MBIT 160 #define ACC_AM_ROB_ECC_INT_STS 0x300104 161 #define ACC_ROB_ECC_ERR_MULTPL BIT(1) 162 #define QM_MSI_CAP_ENABLE BIT(16) 163 164 /* interfunction communication */ 165 #define QM_IFC_READY_STATUS 0x100128 166 #define QM_IFC_INT_SET_P 0x100130 167 #define QM_IFC_INT_CFG 0x100134 168 #define QM_IFC_INT_SOURCE_P 0x100138 169 #define QM_IFC_INT_SOURCE_V 0x0020 170 #define QM_IFC_INT_MASK 0x0024 171 #define QM_IFC_INT_STATUS 0x0028 172 #define QM_IFC_INT_SET_V 0x002C 173 #define QM_IFC_SEND_ALL_VFS GENMASK(6, 0) 174 #define QM_IFC_INT_SOURCE_CLR GENMASK(63, 0) 175 #define QM_IFC_INT_SOURCE_MASK BIT(0) 176 #define QM_IFC_INT_DISABLE BIT(0) 177 #define QM_IFC_INT_STATUS_MASK BIT(0) 178 #define QM_IFC_INT_SET_MASK BIT(0) 179 #define QM_WAIT_DST_ACK 10 180 #define QM_MAX_PF_WAIT_COUNT 10 181 #define QM_MAX_VF_WAIT_COUNT 40 182 #define QM_VF_RESET_WAIT_US 20000 183 #define QM_VF_RESET_WAIT_CNT 3000 184 #define QM_VF_RESET_WAIT_TIMEOUT_US \ 185 (QM_VF_RESET_WAIT_US * QM_VF_RESET_WAIT_CNT) 186 187 #define POLL_PERIOD 10 188 #define POLL_TIMEOUT 1000 189 #define WAIT_PERIOD_US_MAX 200 190 #define WAIT_PERIOD_US_MIN 100 191 #define MAX_WAIT_COUNTS 1000 192 #define QM_CACHE_WB_START 0x204 193 #define QM_CACHE_WB_DONE 0x208 194 #define QM_FUNC_CAPS_REG 0x3100 195 #define QM_CAPBILITY_VERSION GENMASK(7, 0) 196 197 #define PCI_BAR_2 2 198 #define PCI_BAR_4 4 199 #define QMC_ALIGN(sz) ALIGN(sz, 32) 200 201 #define QM_DBG_READ_LEN 256 202 #define QM_PCI_COMMAND_INVALID ~0 203 #define QM_RESET_STOP_TX_OFFSET 1 204 #define QM_RESET_STOP_RX_OFFSET 2 205 206 #define WAIT_PERIOD 20 207 #define REMOVE_WAIT_DELAY 10 208 209 #define QM_QOS_PARAM_NUM 2 210 #define QM_QOS_MAX_VAL 1000 211 #define QM_QOS_RATE 100 212 #define QM_QOS_EXPAND_RATE 1000 213 #define QM_SHAPER_CIR_B_MASK GENMASK(7, 0) 214 #define QM_SHAPER_CIR_U_MASK GENMASK(10, 8) 215 #define QM_SHAPER_CIR_S_MASK GENMASK(14, 11) 216 #define QM_SHAPER_FACTOR_CIR_U_SHIFT 8 217 #define QM_SHAPER_FACTOR_CIR_S_SHIFT 11 218 #define QM_SHAPER_FACTOR_CBS_B_SHIFT 15 219 #define QM_SHAPER_FACTOR_CBS_S_SHIFT 19 220 #define QM_SHAPER_CBS_B 1 221 #define QM_SHAPER_VFT_OFFSET 6 222 #define QM_QOS_MIN_ERROR_RATE 5 223 #define QM_SHAPER_MIN_CBS_S 8 224 #define QM_QOS_TICK 0x300U 225 #define QM_QOS_DIVISOR_CLK 0x1f40U 226 #define QM_QOS_MAX_CIR_B 200 227 #define QM_QOS_MIN_CIR_B 100 228 #define QM_QOS_MAX_CIR_U 6 229 #define QM_AUTOSUSPEND_DELAY 3000 230 231 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \ 232 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \ 233 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \ 234 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \ 235 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 236 237 #define QM_MK_CQC_DW3_V2(cqe_sz, cq_depth) \ 238 ((((u32)cq_depth) - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 239 240 #define QM_MK_SQC_W13(priority, orders, alg_type) \ 241 (((priority) << QM_SQ_PRIORITY_SHIFT) | \ 242 ((orders) << QM_SQ_ORDERS_SHIFT) | \ 243 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT)) 244 245 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \ 246 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \ 247 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \ 248 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \ 249 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 250 251 #define QM_MK_SQC_DW3_V2(sqe_sz, sq_depth) \ 252 ((((u32)sq_depth) - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 253 254 #define INIT_QC_COMMON(qc, base, pasid) do { \ 255 (qc)->head = 0; \ 256 (qc)->tail = 0; \ 257 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \ 258 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \ 259 (qc)->dw3 = 0; \ 260 (qc)->w8 = 0; \ 261 (qc)->rsvd0 = 0; \ 262 (qc)->pasid = cpu_to_le16(pasid); \ 263 (qc)->w11 = 0; \ 264 (qc)->rsvd1 = 0; \ 265 } while (0) 266 267 enum vft_type { 268 SQC_VFT = 0, 269 CQC_VFT, 270 SHAPER_VFT, 271 }; 272 273 enum acc_err_result { 274 ACC_ERR_NONE, 275 ACC_ERR_NEED_RESET, 276 ACC_ERR_RECOVERED, 277 }; 278 279 enum qm_alg_type { 280 ALG_TYPE_0, 281 ALG_TYPE_1, 282 }; 283 284 enum qm_mb_cmd { 285 QM_PF_FLR_PREPARE = 0x01, 286 QM_PF_SRST_PREPARE, 287 QM_PF_RESET_DONE, 288 QM_VF_PREPARE_DONE, 289 QM_VF_PREPARE_FAIL, 290 QM_VF_START_DONE, 291 QM_VF_START_FAIL, 292 QM_PF_SET_QOS, 293 QM_VF_GET_QOS, 294 }; 295 296 enum qm_basic_type { 297 QM_TOTAL_QP_NUM_CAP = 0x0, 298 QM_FUNC_MAX_QP_CAP, 299 QM_XEQ_DEPTH_CAP, 300 QM_QP_DEPTH_CAP, 301 QM_EQ_IRQ_TYPE_CAP, 302 QM_AEQ_IRQ_TYPE_CAP, 303 QM_ABN_IRQ_TYPE_CAP, 304 QM_PF2VF_IRQ_TYPE_CAP, 305 QM_PF_IRQ_NUM_CAP, 306 QM_VF_IRQ_NUM_CAP, 307 }; 308 309 static const struct hisi_qm_cap_info qm_cap_info_comm[] = { 310 {QM_SUPPORT_DB_ISOLATION, 0x30, 0, BIT(0), 0x0, 0x0, 0x0}, 311 {QM_SUPPORT_FUNC_QOS, 0x3100, 0, BIT(8), 0x0, 0x0, 0x1}, 312 {QM_SUPPORT_STOP_QP, 0x3100, 0, BIT(9), 0x0, 0x0, 0x1}, 313 {QM_SUPPORT_MB_COMMAND, 0x3100, 0, BIT(11), 0x0, 0x0, 0x1}, 314 {QM_SUPPORT_SVA_PREFETCH, 0x3100, 0, BIT(14), 0x0, 0x0, 0x1}, 315 }; 316 317 static const struct hisi_qm_cap_info qm_cap_info_pf[] = { 318 {QM_SUPPORT_RPM, 0x3100, 0, BIT(13), 0x0, 0x0, 0x1}, 319 }; 320 321 static const struct hisi_qm_cap_info qm_cap_info_vf[] = { 322 {QM_SUPPORT_RPM, 0x3100, 0, BIT(12), 0x0, 0x0, 0x0}, 323 }; 324 325 static const struct hisi_qm_cap_info qm_basic_info[] = { 326 {QM_TOTAL_QP_NUM_CAP, 0x100158, 0, GENMASK(10, 0), 0x1000, 0x400, 0x400}, 327 {QM_FUNC_MAX_QP_CAP, 0x100158, 11, GENMASK(10, 0), 0x1000, 0x400, 0x400}, 328 {QM_XEQ_DEPTH_CAP, 0x3104, 0, GENMASK(31, 0), 0x800, 0x4000800, 0x4000800}, 329 {QM_QP_DEPTH_CAP, 0x3108, 0, GENMASK(31, 0), 0x4000400, 0x4000400, 0x4000400}, 330 {QM_EQ_IRQ_TYPE_CAP, 0x310c, 0, GENMASK(31, 0), 0x10000, 0x10000, 0x10000}, 331 {QM_AEQ_IRQ_TYPE_CAP, 0x3110, 0, GENMASK(31, 0), 0x0, 0x10001, 0x10001}, 332 {QM_ABN_IRQ_TYPE_CAP, 0x3114, 0, GENMASK(31, 0), 0x0, 0x10003, 0x10003}, 333 {QM_PF2VF_IRQ_TYPE_CAP, 0x3118, 0, GENMASK(31, 0), 0x0, 0x0, 0x10002}, 334 {QM_PF_IRQ_NUM_CAP, 0x311c, 16, GENMASK(15, 0), 0x1, 0x4, 0x4}, 335 {QM_VF_IRQ_NUM_CAP, 0x311c, 0, GENMASK(15, 0), 0x1, 0x2, 0x3}, 336 }; 337 338 struct qm_mailbox { 339 __le16 w0; 340 __le16 queue_num; 341 __le32 base_l; 342 __le32 base_h; 343 __le32 rsvd; 344 }; 345 346 struct qm_doorbell { 347 __le16 queue_num; 348 __le16 cmd; 349 __le16 index; 350 __le16 priority; 351 }; 352 353 struct hisi_qm_resource { 354 struct hisi_qm *qm; 355 int distance; 356 struct list_head list; 357 }; 358 359 /** 360 * struct qm_hw_err - Structure describing the device errors 361 * @list: hardware error list 362 * @timestamp: timestamp when the error occurred 363 */ 364 struct qm_hw_err { 365 struct list_head list; 366 unsigned long long timestamp; 367 }; 368 369 struct hisi_qm_hw_ops { 370 int (*get_vft)(struct hisi_qm *qm, u32 *base, u32 *number); 371 void (*qm_db)(struct hisi_qm *qm, u16 qn, 372 u8 cmd, u16 index, u8 priority); 373 int (*debug_init)(struct hisi_qm *qm); 374 void (*hw_error_init)(struct hisi_qm *qm); 375 void (*hw_error_uninit)(struct hisi_qm *qm); 376 enum acc_err_result (*hw_error_handle)(struct hisi_qm *qm); 377 int (*set_msi)(struct hisi_qm *qm, bool set); 378 }; 379 380 struct hisi_qm_hw_error { 381 u32 int_msk; 382 const char *msg; 383 }; 384 385 static const struct hisi_qm_hw_error qm_hw_error[] = { 386 { .int_msk = BIT(0), .msg = "qm_axi_rresp" }, 387 { .int_msk = BIT(1), .msg = "qm_axi_bresp" }, 388 { .int_msk = BIT(2), .msg = "qm_ecc_mbit" }, 389 { .int_msk = BIT(3), .msg = "qm_ecc_1bit" }, 390 { .int_msk = BIT(4), .msg = "qm_acc_get_task_timeout" }, 391 { .int_msk = BIT(5), .msg = "qm_acc_do_task_timeout" }, 392 { .int_msk = BIT(6), .msg = "qm_acc_wb_not_ready_timeout" }, 393 { .int_msk = BIT(7), .msg = "qm_sq_cq_vf_invalid" }, 394 { .int_msk = BIT(8), .msg = "qm_cq_vf_invalid" }, 395 { .int_msk = BIT(9), .msg = "qm_sq_vf_invalid" }, 396 { .int_msk = BIT(10), .msg = "qm_db_timeout" }, 397 { .int_msk = BIT(11), .msg = "qm_of_fifo_of" }, 398 { .int_msk = BIT(12), .msg = "qm_db_random_invalid" }, 399 { .int_msk = BIT(13), .msg = "qm_mailbox_timeout" }, 400 { .int_msk = BIT(14), .msg = "qm_flr_timeout" }, 401 { /* sentinel */ } 402 }; 403 404 static const char * const qm_db_timeout[] = { 405 "sq", "cq", "eq", "aeq", 406 }; 407 408 static const char * const qm_fifo_overflow[] = { 409 "cq", "eq", "aeq", 410 }; 411 412 static const char * const qp_s[] = { 413 "none", "init", "start", "stop", "close", 414 }; 415 416 struct qm_typical_qos_table { 417 u32 start; 418 u32 end; 419 u32 val; 420 }; 421 422 /* the qos step is 100 */ 423 static struct qm_typical_qos_table shaper_cir_s[] = { 424 {100, 100, 4}, 425 {200, 200, 3}, 426 {300, 500, 2}, 427 {600, 1000, 1}, 428 {1100, 100000, 0}, 429 }; 430 431 static struct qm_typical_qos_table shaper_cbs_s[] = { 432 {100, 200, 9}, 433 {300, 500, 11}, 434 {600, 1000, 12}, 435 {1100, 10000, 16}, 436 {10100, 25000, 17}, 437 {25100, 50000, 18}, 438 {50100, 100000, 19} 439 }; 440 441 static void qm_irqs_unregister(struct hisi_qm *qm); 442 443 static bool qm_avail_state(struct hisi_qm *qm, enum qm_state new) 444 { 445 enum qm_state curr = atomic_read(&qm->status.flags); 446 bool avail = false; 447 448 switch (curr) { 449 case QM_INIT: 450 if (new == QM_START || new == QM_CLOSE) 451 avail = true; 452 break; 453 case QM_START: 454 if (new == QM_STOP) 455 avail = true; 456 break; 457 case QM_STOP: 458 if (new == QM_CLOSE || new == QM_START) 459 avail = true; 460 break; 461 default: 462 break; 463 } 464 465 dev_dbg(&qm->pdev->dev, "change qm state from %s to %s\n", 466 qm_s[curr], qm_s[new]); 467 468 if (!avail) 469 dev_warn(&qm->pdev->dev, "Can not change qm state from %s to %s\n", 470 qm_s[curr], qm_s[new]); 471 472 return avail; 473 } 474 475 static bool qm_qp_avail_state(struct hisi_qm *qm, struct hisi_qp *qp, 476 enum qp_state new) 477 { 478 enum qm_state qm_curr = atomic_read(&qm->status.flags); 479 enum qp_state qp_curr = 0; 480 bool avail = false; 481 482 if (qp) 483 qp_curr = atomic_read(&qp->qp_status.flags); 484 485 switch (new) { 486 case QP_INIT: 487 if (qm_curr == QM_START || qm_curr == QM_INIT) 488 avail = true; 489 break; 490 case QP_START: 491 if ((qm_curr == QM_START && qp_curr == QP_INIT) || 492 (qm_curr == QM_START && qp_curr == QP_STOP)) 493 avail = true; 494 break; 495 case QP_STOP: 496 if ((qm_curr == QM_START && qp_curr == QP_START) || 497 (qp_curr == QP_INIT)) 498 avail = true; 499 break; 500 case QP_CLOSE: 501 if ((qm_curr == QM_START && qp_curr == QP_INIT) || 502 (qm_curr == QM_START && qp_curr == QP_STOP) || 503 (qm_curr == QM_STOP && qp_curr == QP_STOP) || 504 (qm_curr == QM_STOP && qp_curr == QP_INIT)) 505 avail = true; 506 break; 507 default: 508 break; 509 } 510 511 dev_dbg(&qm->pdev->dev, "change qp state from %s to %s in QM %s\n", 512 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]); 513 514 if (!avail) 515 dev_warn(&qm->pdev->dev, 516 "Can not change qp state from %s to %s in QM %s\n", 517 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]); 518 519 return avail; 520 } 521 522 static u32 qm_get_hw_error_status(struct hisi_qm *qm) 523 { 524 return readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 525 } 526 527 static u32 qm_get_dev_err_status(struct hisi_qm *qm) 528 { 529 return qm->err_ini->get_dev_hw_err_status(qm); 530 } 531 532 /* Check if the error causes the master ooo block */ 533 static bool qm_check_dev_error(struct hisi_qm *qm) 534 { 535 u32 val, dev_val; 536 537 if (qm->fun_type == QM_HW_VF) 538 return false; 539 540 val = qm_get_hw_error_status(qm) & qm->err_info.qm_shutdown_mask; 541 dev_val = qm_get_dev_err_status(qm) & qm->err_info.dev_shutdown_mask; 542 543 return val || dev_val; 544 } 545 546 static int qm_wait_reset_finish(struct hisi_qm *qm) 547 { 548 int delay = 0; 549 550 /* All reset requests need to be queued for processing */ 551 while (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) { 552 msleep(++delay); 553 if (delay > QM_RESET_WAIT_TIMEOUT) 554 return -EBUSY; 555 } 556 557 return 0; 558 } 559 560 static int qm_reset_prepare_ready(struct hisi_qm *qm) 561 { 562 struct pci_dev *pdev = qm->pdev; 563 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 564 565 /* 566 * PF and VF on host doesnot support resetting at the 567 * same time on Kunpeng920. 568 */ 569 if (qm->ver < QM_HW_V3) 570 return qm_wait_reset_finish(pf_qm); 571 572 return qm_wait_reset_finish(qm); 573 } 574 575 static void qm_reset_bit_clear(struct hisi_qm *qm) 576 { 577 struct pci_dev *pdev = qm->pdev; 578 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 579 580 if (qm->ver < QM_HW_V3) 581 clear_bit(QM_RESETTING, &pf_qm->misc_ctl); 582 583 clear_bit(QM_RESETTING, &qm->misc_ctl); 584 } 585 586 static void qm_mb_pre_init(struct qm_mailbox *mailbox, u8 cmd, 587 u64 base, u16 queue, bool op) 588 { 589 mailbox->w0 = cpu_to_le16((cmd) | 590 ((op) ? 0x1 << QM_MB_OP_SHIFT : 0) | 591 (0x1 << QM_MB_BUSY_SHIFT)); 592 mailbox->queue_num = cpu_to_le16(queue); 593 mailbox->base_l = cpu_to_le32(lower_32_bits(base)); 594 mailbox->base_h = cpu_to_le32(upper_32_bits(base)); 595 mailbox->rsvd = 0; 596 } 597 598 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */ 599 int hisi_qm_wait_mb_ready(struct hisi_qm *qm) 600 { 601 u32 val; 602 603 return readl_relaxed_poll_timeout(qm->io_base + QM_MB_CMD_SEND_BASE, 604 val, !((val >> QM_MB_BUSY_SHIFT) & 605 0x1), POLL_PERIOD, POLL_TIMEOUT); 606 } 607 EXPORT_SYMBOL_GPL(hisi_qm_wait_mb_ready); 608 609 /* 128 bit should be written to hardware at one time to trigger a mailbox */ 610 static void qm_mb_write(struct hisi_qm *qm, const void *src) 611 { 612 void __iomem *fun_base = qm->io_base + QM_MB_CMD_SEND_BASE; 613 614 #if IS_ENABLED(CONFIG_ARM64) 615 unsigned long tmp0 = 0, tmp1 = 0; 616 #endif 617 618 if (!IS_ENABLED(CONFIG_ARM64)) { 619 memcpy_toio(fun_base, src, 16); 620 dma_wmb(); 621 return; 622 } 623 624 #if IS_ENABLED(CONFIG_ARM64) 625 asm volatile("ldp %0, %1, %3\n" 626 "stp %0, %1, %2\n" 627 "dmb oshst\n" 628 : "=&r" (tmp0), 629 "=&r" (tmp1), 630 "+Q" (*((char __iomem *)fun_base)) 631 : "Q" (*((char *)src)) 632 : "memory"); 633 #endif 634 } 635 636 static int qm_mb_nolock(struct hisi_qm *qm, struct qm_mailbox *mailbox) 637 { 638 int ret; 639 u32 val; 640 641 if (unlikely(hisi_qm_wait_mb_ready(qm))) { 642 dev_err(&qm->pdev->dev, "QM mailbox is busy to start!\n"); 643 ret = -EBUSY; 644 goto mb_busy; 645 } 646 647 qm_mb_write(qm, mailbox); 648 649 if (unlikely(hisi_qm_wait_mb_ready(qm))) { 650 dev_err(&qm->pdev->dev, "QM mailbox operation timeout!\n"); 651 ret = -ETIMEDOUT; 652 goto mb_busy; 653 } 654 655 val = readl(qm->io_base + QM_MB_CMD_SEND_BASE); 656 if (val & QM_MB_STATUS_MASK) { 657 dev_err(&qm->pdev->dev, "QM mailbox operation failed!\n"); 658 ret = -EIO; 659 goto mb_busy; 660 } 661 662 return 0; 663 664 mb_busy: 665 atomic64_inc(&qm->debug.dfx.mb_err_cnt); 666 return ret; 667 } 668 669 int hisi_qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue, 670 bool op) 671 { 672 struct qm_mailbox mailbox; 673 int ret; 674 675 dev_dbg(&qm->pdev->dev, "QM mailbox request to q%u: %u-%llx\n", 676 queue, cmd, (unsigned long long)dma_addr); 677 678 qm_mb_pre_init(&mailbox, cmd, dma_addr, queue, op); 679 680 mutex_lock(&qm->mailbox_lock); 681 ret = qm_mb_nolock(qm, &mailbox); 682 mutex_unlock(&qm->mailbox_lock); 683 684 return ret; 685 } 686 EXPORT_SYMBOL_GPL(hisi_qm_mb); 687 688 static void qm_db_v1(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 689 { 690 u64 doorbell; 691 692 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V1) | 693 ((u64)index << QM_DB_INDEX_SHIFT_V1) | 694 ((u64)priority << QM_DB_PRIORITY_SHIFT_V1); 695 696 writeq(doorbell, qm->io_base + QM_DOORBELL_BASE_V1); 697 } 698 699 static void qm_db_v2(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 700 { 701 void __iomem *io_base = qm->io_base; 702 u16 randata = 0; 703 u64 doorbell; 704 705 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ) 706 io_base = qm->db_io_base + (u64)qn * qm->db_interval + 707 QM_DOORBELL_SQ_CQ_BASE_V2; 708 else 709 io_base += QM_DOORBELL_EQ_AEQ_BASE_V2; 710 711 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) | 712 ((u64)randata << QM_DB_RAND_SHIFT_V2) | 713 ((u64)index << QM_DB_INDEX_SHIFT_V2) | 714 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2); 715 716 writeq(doorbell, io_base); 717 } 718 719 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 720 { 721 dev_dbg(&qm->pdev->dev, "QM doorbell request: qn=%u, cmd=%u, index=%u\n", 722 qn, cmd, index); 723 724 qm->ops->qm_db(qm, qn, cmd, index, priority); 725 } 726 727 static void qm_disable_clock_gate(struct hisi_qm *qm) 728 { 729 u32 val; 730 731 /* if qm enables clock gating in Kunpeng930, qos will be inaccurate. */ 732 if (qm->ver < QM_HW_V3) 733 return; 734 735 val = readl(qm->io_base + QM_PM_CTRL); 736 val |= QM_IDLE_DISABLE; 737 writel(val, qm->io_base + QM_PM_CTRL); 738 } 739 740 static int qm_dev_mem_reset(struct hisi_qm *qm) 741 { 742 u32 val; 743 744 writel(0x1, qm->io_base + QM_MEM_START_INIT); 745 return readl_relaxed_poll_timeout(qm->io_base + QM_MEM_INIT_DONE, val, 746 val & BIT(0), POLL_PERIOD, 747 POLL_TIMEOUT); 748 } 749 750 /** 751 * hisi_qm_get_hw_info() - Get device information. 752 * @qm: The qm which want to get information. 753 * @info_table: Array for storing device information. 754 * @index: Index in info_table. 755 * @is_read: Whether read from reg, 0: not support read from reg. 756 * 757 * This function returns device information the caller needs. 758 */ 759 u32 hisi_qm_get_hw_info(struct hisi_qm *qm, 760 const struct hisi_qm_cap_info *info_table, 761 u32 index, bool is_read) 762 { 763 u32 val; 764 765 switch (qm->ver) { 766 case QM_HW_V1: 767 return info_table[index].v1_val; 768 case QM_HW_V2: 769 return info_table[index].v2_val; 770 default: 771 if (!is_read) 772 return info_table[index].v3_val; 773 774 val = readl(qm->io_base + info_table[index].offset); 775 return (val >> info_table[index].shift) & info_table[index].mask; 776 } 777 } 778 EXPORT_SYMBOL_GPL(hisi_qm_get_hw_info); 779 780 static void qm_get_xqc_depth(struct hisi_qm *qm, u16 *low_bits, 781 u16 *high_bits, enum qm_basic_type type) 782 { 783 u32 depth; 784 785 depth = hisi_qm_get_hw_info(qm, qm_basic_info, type, qm->cap_ver); 786 *low_bits = depth & QM_XQ_DEPTH_MASK; 787 *high_bits = (depth >> QM_XQ_DEPTH_SHIFT) & QM_XQ_DEPTH_MASK; 788 } 789 790 static u32 qm_get_irq_num(struct hisi_qm *qm) 791 { 792 if (qm->fun_type == QM_HW_PF) 793 return hisi_qm_get_hw_info(qm, qm_basic_info, QM_PF_IRQ_NUM_CAP, qm->cap_ver); 794 795 return hisi_qm_get_hw_info(qm, qm_basic_info, QM_VF_IRQ_NUM_CAP, qm->cap_ver); 796 } 797 798 static int qm_pm_get_sync(struct hisi_qm *qm) 799 { 800 struct device *dev = &qm->pdev->dev; 801 int ret; 802 803 if (!test_bit(QM_SUPPORT_RPM, &qm->caps)) 804 return 0; 805 806 ret = pm_runtime_resume_and_get(dev); 807 if (ret < 0) { 808 dev_err(dev, "failed to get_sync(%d).\n", ret); 809 return ret; 810 } 811 812 return 0; 813 } 814 815 static void qm_pm_put_sync(struct hisi_qm *qm) 816 { 817 struct device *dev = &qm->pdev->dev; 818 819 if (!test_bit(QM_SUPPORT_RPM, &qm->caps)) 820 return; 821 822 pm_runtime_mark_last_busy(dev); 823 pm_runtime_put_autosuspend(dev); 824 } 825 826 static void qm_cq_head_update(struct hisi_qp *qp) 827 { 828 if (qp->qp_status.cq_head == qp->cq_depth - 1) { 829 qp->qp_status.cqc_phase = !qp->qp_status.cqc_phase; 830 qp->qp_status.cq_head = 0; 831 } else { 832 qp->qp_status.cq_head++; 833 } 834 } 835 836 static void qm_poll_req_cb(struct hisi_qp *qp) 837 { 838 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head; 839 struct hisi_qm *qm = qp->qm; 840 841 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) { 842 dma_rmb(); 843 qp->req_cb(qp, qp->sqe + qm->sqe_size * 844 le16_to_cpu(cqe->sq_head)); 845 qm_cq_head_update(qp); 846 cqe = qp->cqe + qp->qp_status.cq_head; 847 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, 848 qp->qp_status.cq_head, 0); 849 atomic_dec(&qp->qp_status.used); 850 851 cond_resched(); 852 } 853 854 /* set c_flag */ 855 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, qp->qp_status.cq_head, 1); 856 } 857 858 static int qm_get_complete_eqe_num(struct hisi_qm_poll_data *poll_data) 859 { 860 struct hisi_qm *qm = poll_data->qm; 861 struct qm_eqe *eqe = qm->eqe + qm->status.eq_head; 862 u16 eq_depth = qm->eq_depth; 863 int eqe_num = 0; 864 u16 cqn; 865 866 while (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) { 867 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK; 868 poll_data->qp_finish_id[eqe_num] = cqn; 869 eqe_num++; 870 871 if (qm->status.eq_head == eq_depth - 1) { 872 qm->status.eqc_phase = !qm->status.eqc_phase; 873 eqe = qm->eqe; 874 qm->status.eq_head = 0; 875 } else { 876 eqe++; 877 qm->status.eq_head++; 878 } 879 880 if (eqe_num == (eq_depth >> 1) - 1) 881 break; 882 } 883 884 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 885 886 return eqe_num; 887 } 888 889 static void qm_work_process(struct work_struct *work) 890 { 891 struct hisi_qm_poll_data *poll_data = 892 container_of(work, struct hisi_qm_poll_data, work); 893 struct hisi_qm *qm = poll_data->qm; 894 struct hisi_qp *qp; 895 int eqe_num, i; 896 897 /* Get qp id of completed tasks and re-enable the interrupt. */ 898 eqe_num = qm_get_complete_eqe_num(poll_data); 899 for (i = eqe_num - 1; i >= 0; i--) { 900 qp = &qm->qp_array[poll_data->qp_finish_id[i]]; 901 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP)) 902 continue; 903 904 if (qp->event_cb) { 905 qp->event_cb(qp); 906 continue; 907 } 908 909 if (likely(qp->req_cb)) 910 qm_poll_req_cb(qp); 911 } 912 } 913 914 static bool do_qm_eq_irq(struct hisi_qm *qm) 915 { 916 struct qm_eqe *eqe = qm->eqe + qm->status.eq_head; 917 struct hisi_qm_poll_data *poll_data; 918 u16 cqn; 919 920 if (!readl(qm->io_base + QM_VF_EQ_INT_SOURCE)) 921 return false; 922 923 if (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) { 924 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK; 925 poll_data = &qm->poll_data[cqn]; 926 queue_work(qm->wq, &poll_data->work); 927 928 return true; 929 } 930 931 return false; 932 } 933 934 static irqreturn_t qm_eq_irq(int irq, void *data) 935 { 936 struct hisi_qm *qm = data; 937 bool ret; 938 939 ret = do_qm_eq_irq(qm); 940 if (ret) 941 return IRQ_HANDLED; 942 943 atomic64_inc(&qm->debug.dfx.err_irq_cnt); 944 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 945 946 return IRQ_NONE; 947 } 948 949 static irqreturn_t qm_mb_cmd_irq(int irq, void *data) 950 { 951 struct hisi_qm *qm = data; 952 u32 val; 953 954 val = readl(qm->io_base + QM_IFC_INT_STATUS); 955 val &= QM_IFC_INT_STATUS_MASK; 956 if (!val) 957 return IRQ_NONE; 958 959 if (test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl)) { 960 dev_warn(&qm->pdev->dev, "Driver is down, message cannot be processed!\n"); 961 return IRQ_HANDLED; 962 } 963 964 schedule_work(&qm->cmd_process); 965 966 return IRQ_HANDLED; 967 } 968 969 static void qm_set_qp_disable(struct hisi_qp *qp, int offset) 970 { 971 u32 *addr; 972 973 if (qp->is_in_kernel) 974 return; 975 976 addr = (u32 *)(qp->qdma.va + qp->qdma.size) - offset; 977 *addr = 1; 978 979 /* make sure setup is completed */ 980 smp_wmb(); 981 } 982 983 static void qm_disable_qp(struct hisi_qm *qm, u32 qp_id) 984 { 985 struct hisi_qp *qp = &qm->qp_array[qp_id]; 986 987 qm_set_qp_disable(qp, QM_RESET_STOP_TX_OFFSET); 988 hisi_qm_stop_qp(qp); 989 qm_set_qp_disable(qp, QM_RESET_STOP_RX_OFFSET); 990 } 991 992 static void qm_reset_function(struct hisi_qm *qm) 993 { 994 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(qm->pdev)); 995 struct device *dev = &qm->pdev->dev; 996 int ret; 997 998 if (qm_check_dev_error(pf_qm)) 999 return; 1000 1001 ret = qm_reset_prepare_ready(qm); 1002 if (ret) { 1003 dev_err(dev, "reset function not ready\n"); 1004 return; 1005 } 1006 1007 ret = hisi_qm_stop(qm, QM_DOWN); 1008 if (ret) { 1009 dev_err(dev, "failed to stop qm when reset function\n"); 1010 goto clear_bit; 1011 } 1012 1013 ret = hisi_qm_start(qm); 1014 if (ret) 1015 dev_err(dev, "failed to start qm when reset function\n"); 1016 1017 clear_bit: 1018 qm_reset_bit_clear(qm); 1019 } 1020 1021 static irqreturn_t qm_aeq_thread(int irq, void *data) 1022 { 1023 struct hisi_qm *qm = data; 1024 struct qm_aeqe *aeqe = qm->aeqe + qm->status.aeq_head; 1025 u16 aeq_depth = qm->aeq_depth; 1026 u32 type, qp_id; 1027 1028 while (QM_AEQE_PHASE(aeqe) == qm->status.aeqc_phase) { 1029 type = le32_to_cpu(aeqe->dw0) >> QM_AEQE_TYPE_SHIFT; 1030 qp_id = le32_to_cpu(aeqe->dw0) & QM_AEQE_CQN_MASK; 1031 1032 switch (type) { 1033 case QM_EQ_OVERFLOW: 1034 dev_err(&qm->pdev->dev, "eq overflow, reset function\n"); 1035 qm_reset_function(qm); 1036 return IRQ_HANDLED; 1037 case QM_CQ_OVERFLOW: 1038 dev_err(&qm->pdev->dev, "cq overflow, stop qp(%u)\n", 1039 qp_id); 1040 fallthrough; 1041 case QM_CQE_ERROR: 1042 qm_disable_qp(qm, qp_id); 1043 break; 1044 default: 1045 dev_err(&qm->pdev->dev, "unknown error type %u\n", 1046 type); 1047 break; 1048 } 1049 1050 if (qm->status.aeq_head == aeq_depth - 1) { 1051 qm->status.aeqc_phase = !qm->status.aeqc_phase; 1052 aeqe = qm->aeqe; 1053 qm->status.aeq_head = 0; 1054 } else { 1055 aeqe++; 1056 qm->status.aeq_head++; 1057 } 1058 } 1059 1060 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0); 1061 1062 return IRQ_HANDLED; 1063 } 1064 1065 static irqreturn_t qm_aeq_irq(int irq, void *data) 1066 { 1067 struct hisi_qm *qm = data; 1068 1069 atomic64_inc(&qm->debug.dfx.aeq_irq_cnt); 1070 if (!readl(qm->io_base + QM_VF_AEQ_INT_SOURCE)) 1071 return IRQ_NONE; 1072 1073 return IRQ_WAKE_THREAD; 1074 } 1075 1076 static void qm_init_qp_status(struct hisi_qp *qp) 1077 { 1078 struct hisi_qp_status *qp_status = &qp->qp_status; 1079 1080 qp_status->sq_tail = 0; 1081 qp_status->cq_head = 0; 1082 qp_status->cqc_phase = true; 1083 atomic_set(&qp_status->used, 0); 1084 } 1085 1086 static void qm_init_prefetch(struct hisi_qm *qm) 1087 { 1088 struct device *dev = &qm->pdev->dev; 1089 u32 page_type = 0x0; 1090 1091 if (!test_bit(QM_SUPPORT_SVA_PREFETCH, &qm->caps)) 1092 return; 1093 1094 switch (PAGE_SIZE) { 1095 case SZ_4K: 1096 page_type = 0x0; 1097 break; 1098 case SZ_16K: 1099 page_type = 0x1; 1100 break; 1101 case SZ_64K: 1102 page_type = 0x2; 1103 break; 1104 default: 1105 dev_err(dev, "system page size is not support: %lu, default set to 4KB", 1106 PAGE_SIZE); 1107 } 1108 1109 writel(page_type, qm->io_base + QM_PAGE_SIZE); 1110 } 1111 1112 /* 1113 * acc_shaper_para_calc() Get the IR value by the qos formula, the return value 1114 * is the expected qos calculated. 1115 * the formula: 1116 * IR = X Mbps if ir = 1 means IR = 100 Mbps, if ir = 10000 means = 10Gbps 1117 * 1118 * IR_b * (2 ^ IR_u) * 8000 1119 * IR(Mbps) = ------------------------- 1120 * Tick * (2 ^ IR_s) 1121 */ 1122 static u32 acc_shaper_para_calc(u64 cir_b, u64 cir_u, u64 cir_s) 1123 { 1124 return ((cir_b * QM_QOS_DIVISOR_CLK) * (1 << cir_u)) / 1125 (QM_QOS_TICK * (1 << cir_s)); 1126 } 1127 1128 static u32 acc_shaper_calc_cbs_s(u32 ir) 1129 { 1130 int table_size = ARRAY_SIZE(shaper_cbs_s); 1131 int i; 1132 1133 for (i = 0; i < table_size; i++) { 1134 if (ir >= shaper_cbs_s[i].start && ir <= shaper_cbs_s[i].end) 1135 return shaper_cbs_s[i].val; 1136 } 1137 1138 return QM_SHAPER_MIN_CBS_S; 1139 } 1140 1141 static u32 acc_shaper_calc_cir_s(u32 ir) 1142 { 1143 int table_size = ARRAY_SIZE(shaper_cir_s); 1144 int i; 1145 1146 for (i = 0; i < table_size; i++) { 1147 if (ir >= shaper_cir_s[i].start && ir <= shaper_cir_s[i].end) 1148 return shaper_cir_s[i].val; 1149 } 1150 1151 return 0; 1152 } 1153 1154 static int qm_get_shaper_para(u32 ir, struct qm_shaper_factor *factor) 1155 { 1156 u32 cir_b, cir_u, cir_s, ir_calc; 1157 u32 error_rate; 1158 1159 factor->cbs_s = acc_shaper_calc_cbs_s(ir); 1160 cir_s = acc_shaper_calc_cir_s(ir); 1161 1162 for (cir_b = QM_QOS_MIN_CIR_B; cir_b <= QM_QOS_MAX_CIR_B; cir_b++) { 1163 for (cir_u = 0; cir_u <= QM_QOS_MAX_CIR_U; cir_u++) { 1164 ir_calc = acc_shaper_para_calc(cir_b, cir_u, cir_s); 1165 1166 error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir; 1167 if (error_rate <= QM_QOS_MIN_ERROR_RATE) { 1168 factor->cir_b = cir_b; 1169 factor->cir_u = cir_u; 1170 factor->cir_s = cir_s; 1171 return 0; 1172 } 1173 } 1174 } 1175 1176 return -EINVAL; 1177 } 1178 1179 static void qm_vft_data_cfg(struct hisi_qm *qm, enum vft_type type, u32 base, 1180 u32 number, struct qm_shaper_factor *factor) 1181 { 1182 u64 tmp = 0; 1183 1184 if (number > 0) { 1185 switch (type) { 1186 case SQC_VFT: 1187 if (qm->ver == QM_HW_V1) { 1188 tmp = QM_SQC_VFT_BUF_SIZE | 1189 QM_SQC_VFT_SQC_SIZE | 1190 QM_SQC_VFT_INDEX_NUMBER | 1191 QM_SQC_VFT_VALID | 1192 (u64)base << QM_SQC_VFT_START_SQN_SHIFT; 1193 } else { 1194 tmp = (u64)base << QM_SQC_VFT_START_SQN_SHIFT | 1195 QM_SQC_VFT_VALID | 1196 (u64)(number - 1) << QM_SQC_VFT_SQN_SHIFT; 1197 } 1198 break; 1199 case CQC_VFT: 1200 if (qm->ver == QM_HW_V1) { 1201 tmp = QM_CQC_VFT_BUF_SIZE | 1202 QM_CQC_VFT_SQC_SIZE | 1203 QM_CQC_VFT_INDEX_NUMBER | 1204 QM_CQC_VFT_VALID; 1205 } else { 1206 tmp = QM_CQC_VFT_VALID; 1207 } 1208 break; 1209 case SHAPER_VFT: 1210 if (factor) { 1211 tmp = factor->cir_b | 1212 (factor->cir_u << QM_SHAPER_FACTOR_CIR_U_SHIFT) | 1213 (factor->cir_s << QM_SHAPER_FACTOR_CIR_S_SHIFT) | 1214 (QM_SHAPER_CBS_B << QM_SHAPER_FACTOR_CBS_B_SHIFT) | 1215 (factor->cbs_s << QM_SHAPER_FACTOR_CBS_S_SHIFT); 1216 } 1217 break; 1218 } 1219 } 1220 1221 writel(lower_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_L); 1222 writel(upper_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_H); 1223 } 1224 1225 static int qm_set_vft_common(struct hisi_qm *qm, enum vft_type type, 1226 u32 fun_num, u32 base, u32 number) 1227 { 1228 struct qm_shaper_factor *factor = NULL; 1229 unsigned int val; 1230 int ret; 1231 1232 if (type == SHAPER_VFT && test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) 1233 factor = &qm->factor[fun_num]; 1234 1235 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 1236 val & BIT(0), POLL_PERIOD, 1237 POLL_TIMEOUT); 1238 if (ret) 1239 return ret; 1240 1241 writel(0x0, qm->io_base + QM_VFT_CFG_OP_WR); 1242 writel(type, qm->io_base + QM_VFT_CFG_TYPE); 1243 if (type == SHAPER_VFT) 1244 fun_num |= base << QM_SHAPER_VFT_OFFSET; 1245 1246 writel(fun_num, qm->io_base + QM_VFT_CFG); 1247 1248 qm_vft_data_cfg(qm, type, base, number, factor); 1249 1250 writel(0x0, qm->io_base + QM_VFT_CFG_RDY); 1251 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE); 1252 1253 return readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 1254 val & BIT(0), POLL_PERIOD, 1255 POLL_TIMEOUT); 1256 } 1257 1258 static int qm_shaper_init_vft(struct hisi_qm *qm, u32 fun_num) 1259 { 1260 u32 qos = qm->factor[fun_num].func_qos; 1261 int ret, i; 1262 1263 ret = qm_get_shaper_para(qos * QM_QOS_RATE, &qm->factor[fun_num]); 1264 if (ret) { 1265 dev_err(&qm->pdev->dev, "failed to calculate shaper parameter!\n"); 1266 return ret; 1267 } 1268 writel(qm->type_rate, qm->io_base + QM_SHAPER_CFG); 1269 for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) { 1270 /* The base number of queue reuse for different alg type */ 1271 ret = qm_set_vft_common(qm, SHAPER_VFT, fun_num, i, 1); 1272 if (ret) 1273 return ret; 1274 } 1275 1276 return 0; 1277 } 1278 1279 /* The config should be conducted after qm_dev_mem_reset() */ 1280 static int qm_set_sqc_cqc_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 1281 u32 number) 1282 { 1283 int ret, i; 1284 1285 for (i = SQC_VFT; i <= CQC_VFT; i++) { 1286 ret = qm_set_vft_common(qm, i, fun_num, base, number); 1287 if (ret) 1288 return ret; 1289 } 1290 1291 /* init default shaper qos val */ 1292 if (test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) { 1293 ret = qm_shaper_init_vft(qm, fun_num); 1294 if (ret) 1295 goto back_sqc_cqc; 1296 } 1297 1298 return 0; 1299 back_sqc_cqc: 1300 for (i = SQC_VFT; i <= CQC_VFT; i++) 1301 qm_set_vft_common(qm, i, fun_num, 0, 0); 1302 1303 return ret; 1304 } 1305 1306 static int qm_get_vft_v2(struct hisi_qm *qm, u32 *base, u32 *number) 1307 { 1308 u64 sqc_vft; 1309 int ret; 1310 1311 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1); 1312 if (ret) 1313 return ret; 1314 1315 sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) | 1316 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32); 1317 *base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2); 1318 *number = (QM_SQC_VFT_NUM_MASK_V2 & 1319 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1; 1320 1321 return 0; 1322 } 1323 1324 void *hisi_qm_ctx_alloc(struct hisi_qm *qm, size_t ctx_size, 1325 dma_addr_t *dma_addr) 1326 { 1327 struct device *dev = &qm->pdev->dev; 1328 void *ctx_addr; 1329 1330 ctx_addr = kzalloc(ctx_size, GFP_KERNEL); 1331 if (!ctx_addr) 1332 return ERR_PTR(-ENOMEM); 1333 1334 *dma_addr = dma_map_single(dev, ctx_addr, ctx_size, DMA_FROM_DEVICE); 1335 if (dma_mapping_error(dev, *dma_addr)) { 1336 dev_err(dev, "DMA mapping error!\n"); 1337 kfree(ctx_addr); 1338 return ERR_PTR(-ENOMEM); 1339 } 1340 1341 return ctx_addr; 1342 } 1343 1344 void hisi_qm_ctx_free(struct hisi_qm *qm, size_t ctx_size, 1345 const void *ctx_addr, dma_addr_t *dma_addr) 1346 { 1347 struct device *dev = &qm->pdev->dev; 1348 1349 dma_unmap_single(dev, *dma_addr, ctx_size, DMA_FROM_DEVICE); 1350 kfree(ctx_addr); 1351 } 1352 1353 static int qm_dump_sqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id) 1354 { 1355 return hisi_qm_mb(qm, QM_MB_CMD_SQC, dma_addr, qp_id, 1); 1356 } 1357 1358 static int qm_dump_cqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id) 1359 { 1360 return hisi_qm_mb(qm, QM_MB_CMD_CQC, dma_addr, qp_id, 1); 1361 } 1362 1363 static void qm_hw_error_init_v1(struct hisi_qm *qm) 1364 { 1365 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK); 1366 } 1367 1368 static void qm_hw_error_cfg(struct hisi_qm *qm) 1369 { 1370 struct hisi_qm_err_info *err_info = &qm->err_info; 1371 1372 qm->error_mask = err_info->nfe | err_info->ce | err_info->fe; 1373 /* clear QM hw residual error source */ 1374 writel(qm->error_mask, qm->io_base + QM_ABNORMAL_INT_SOURCE); 1375 1376 /* configure error type */ 1377 writel(err_info->ce, qm->io_base + QM_RAS_CE_ENABLE); 1378 writel(QM_RAS_CE_TIMES_PER_IRQ, qm->io_base + QM_RAS_CE_THRESHOLD); 1379 writel(err_info->nfe, qm->io_base + QM_RAS_NFE_ENABLE); 1380 writel(err_info->fe, qm->io_base + QM_RAS_FE_ENABLE); 1381 } 1382 1383 static void qm_hw_error_init_v2(struct hisi_qm *qm) 1384 { 1385 u32 irq_unmask; 1386 1387 qm_hw_error_cfg(qm); 1388 1389 irq_unmask = ~qm->error_mask; 1390 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1391 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK); 1392 } 1393 1394 static void qm_hw_error_uninit_v2(struct hisi_qm *qm) 1395 { 1396 u32 irq_mask = qm->error_mask; 1397 1398 irq_mask |= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1399 writel(irq_mask, qm->io_base + QM_ABNORMAL_INT_MASK); 1400 } 1401 1402 static void qm_hw_error_init_v3(struct hisi_qm *qm) 1403 { 1404 u32 irq_unmask; 1405 1406 qm_hw_error_cfg(qm); 1407 1408 /* enable close master ooo when hardware error happened */ 1409 writel(qm->err_info.qm_shutdown_mask, qm->io_base + QM_OOO_SHUTDOWN_SEL); 1410 1411 irq_unmask = ~qm->error_mask; 1412 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1413 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK); 1414 } 1415 1416 static void qm_hw_error_uninit_v3(struct hisi_qm *qm) 1417 { 1418 u32 irq_mask = qm->error_mask; 1419 1420 irq_mask |= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1421 writel(irq_mask, qm->io_base + QM_ABNORMAL_INT_MASK); 1422 1423 /* disable close master ooo when hardware error happened */ 1424 writel(0x0, qm->io_base + QM_OOO_SHUTDOWN_SEL); 1425 } 1426 1427 static void qm_log_hw_error(struct hisi_qm *qm, u32 error_status) 1428 { 1429 const struct hisi_qm_hw_error *err; 1430 struct device *dev = &qm->pdev->dev; 1431 u32 reg_val, type, vf_num; 1432 int i; 1433 1434 for (i = 0; i < ARRAY_SIZE(qm_hw_error); i++) { 1435 err = &qm_hw_error[i]; 1436 if (!(err->int_msk & error_status)) 1437 continue; 1438 1439 dev_err(dev, "%s [error status=0x%x] found\n", 1440 err->msg, err->int_msk); 1441 1442 if (err->int_msk & QM_DB_TIMEOUT) { 1443 reg_val = readl(qm->io_base + QM_ABNORMAL_INF01); 1444 type = (reg_val & QM_DB_TIMEOUT_TYPE) >> 1445 QM_DB_TIMEOUT_TYPE_SHIFT; 1446 vf_num = reg_val & QM_DB_TIMEOUT_VF; 1447 dev_err(dev, "qm %s doorbell timeout in function %u\n", 1448 qm_db_timeout[type], vf_num); 1449 } else if (err->int_msk & QM_OF_FIFO_OF) { 1450 reg_val = readl(qm->io_base + QM_ABNORMAL_INF00); 1451 type = (reg_val & QM_FIFO_OVERFLOW_TYPE) >> 1452 QM_FIFO_OVERFLOW_TYPE_SHIFT; 1453 vf_num = reg_val & QM_FIFO_OVERFLOW_VF; 1454 1455 if (type < ARRAY_SIZE(qm_fifo_overflow)) 1456 dev_err(dev, "qm %s fifo overflow in function %u\n", 1457 qm_fifo_overflow[type], vf_num); 1458 else 1459 dev_err(dev, "unknown error type\n"); 1460 } 1461 } 1462 } 1463 1464 static enum acc_err_result qm_hw_error_handle_v2(struct hisi_qm *qm) 1465 { 1466 u32 error_status, tmp; 1467 1468 /* read err sts */ 1469 tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 1470 error_status = qm->error_mask & tmp; 1471 1472 if (error_status) { 1473 if (error_status & QM_ECC_MBIT) 1474 qm->err_status.is_qm_ecc_mbit = true; 1475 1476 qm_log_hw_error(qm, error_status); 1477 if (error_status & qm->err_info.qm_reset_mask) 1478 return ACC_ERR_NEED_RESET; 1479 1480 writel(error_status, qm->io_base + QM_ABNORMAL_INT_SOURCE); 1481 writel(qm->err_info.nfe, qm->io_base + QM_RAS_NFE_ENABLE); 1482 } 1483 1484 return ACC_ERR_RECOVERED; 1485 } 1486 1487 static int qm_get_mb_cmd(struct hisi_qm *qm, u64 *msg, u16 fun_num) 1488 { 1489 struct qm_mailbox mailbox; 1490 int ret; 1491 1492 qm_mb_pre_init(&mailbox, QM_MB_CMD_DST, 0, fun_num, 0); 1493 mutex_lock(&qm->mailbox_lock); 1494 ret = qm_mb_nolock(qm, &mailbox); 1495 if (ret) 1496 goto err_unlock; 1497 1498 *msg = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) | 1499 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32); 1500 1501 err_unlock: 1502 mutex_unlock(&qm->mailbox_lock); 1503 return ret; 1504 } 1505 1506 static void qm_clear_cmd_interrupt(struct hisi_qm *qm, u64 vf_mask) 1507 { 1508 u32 val; 1509 1510 if (qm->fun_type == QM_HW_PF) 1511 writeq(vf_mask, qm->io_base + QM_IFC_INT_SOURCE_P); 1512 1513 val = readl(qm->io_base + QM_IFC_INT_SOURCE_V); 1514 val |= QM_IFC_INT_SOURCE_MASK; 1515 writel(val, qm->io_base + QM_IFC_INT_SOURCE_V); 1516 } 1517 1518 static void qm_handle_vf_msg(struct hisi_qm *qm, u32 vf_id) 1519 { 1520 struct device *dev = &qm->pdev->dev; 1521 u32 cmd; 1522 u64 msg; 1523 int ret; 1524 1525 ret = qm_get_mb_cmd(qm, &msg, vf_id); 1526 if (ret) { 1527 dev_err(dev, "failed to get msg from VF(%u)!\n", vf_id); 1528 return; 1529 } 1530 1531 cmd = msg & QM_MB_CMD_DATA_MASK; 1532 switch (cmd) { 1533 case QM_VF_PREPARE_FAIL: 1534 dev_err(dev, "failed to stop VF(%u)!\n", vf_id); 1535 break; 1536 case QM_VF_START_FAIL: 1537 dev_err(dev, "failed to start VF(%u)!\n", vf_id); 1538 break; 1539 case QM_VF_PREPARE_DONE: 1540 case QM_VF_START_DONE: 1541 break; 1542 default: 1543 dev_err(dev, "unsupported cmd %u sent by VF(%u)!\n", cmd, vf_id); 1544 break; 1545 } 1546 } 1547 1548 static int qm_wait_vf_prepare_finish(struct hisi_qm *qm) 1549 { 1550 struct device *dev = &qm->pdev->dev; 1551 u32 vfs_num = qm->vfs_num; 1552 int cnt = 0; 1553 int ret = 0; 1554 u64 val; 1555 u32 i; 1556 1557 if (!qm->vfs_num || !test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) 1558 return 0; 1559 1560 while (true) { 1561 val = readq(qm->io_base + QM_IFC_INT_SOURCE_P); 1562 /* All VFs send command to PF, break */ 1563 if ((val & GENMASK(vfs_num, 1)) == GENMASK(vfs_num, 1)) 1564 break; 1565 1566 if (++cnt > QM_MAX_PF_WAIT_COUNT) { 1567 ret = -EBUSY; 1568 break; 1569 } 1570 1571 msleep(QM_WAIT_DST_ACK); 1572 } 1573 1574 /* PF check VFs msg */ 1575 for (i = 1; i <= vfs_num; i++) { 1576 if (val & BIT(i)) 1577 qm_handle_vf_msg(qm, i); 1578 else 1579 dev_err(dev, "VF(%u) not ping PF!\n", i); 1580 } 1581 1582 /* PF clear interrupt to ack VFs */ 1583 qm_clear_cmd_interrupt(qm, val); 1584 1585 return ret; 1586 } 1587 1588 static void qm_trigger_vf_interrupt(struct hisi_qm *qm, u32 fun_num) 1589 { 1590 u32 val; 1591 1592 val = readl(qm->io_base + QM_IFC_INT_CFG); 1593 val &= ~QM_IFC_SEND_ALL_VFS; 1594 val |= fun_num; 1595 writel(val, qm->io_base + QM_IFC_INT_CFG); 1596 1597 val = readl(qm->io_base + QM_IFC_INT_SET_P); 1598 val |= QM_IFC_INT_SET_MASK; 1599 writel(val, qm->io_base + QM_IFC_INT_SET_P); 1600 } 1601 1602 static void qm_trigger_pf_interrupt(struct hisi_qm *qm) 1603 { 1604 u32 val; 1605 1606 val = readl(qm->io_base + QM_IFC_INT_SET_V); 1607 val |= QM_IFC_INT_SET_MASK; 1608 writel(val, qm->io_base + QM_IFC_INT_SET_V); 1609 } 1610 1611 static int qm_ping_single_vf(struct hisi_qm *qm, u64 cmd, u32 fun_num) 1612 { 1613 struct device *dev = &qm->pdev->dev; 1614 struct qm_mailbox mailbox; 1615 int cnt = 0; 1616 u64 val; 1617 int ret; 1618 1619 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, fun_num, 0); 1620 mutex_lock(&qm->mailbox_lock); 1621 ret = qm_mb_nolock(qm, &mailbox); 1622 if (ret) { 1623 dev_err(dev, "failed to send command to vf(%u)!\n", fun_num); 1624 goto err_unlock; 1625 } 1626 1627 qm_trigger_vf_interrupt(qm, fun_num); 1628 while (true) { 1629 msleep(QM_WAIT_DST_ACK); 1630 val = readq(qm->io_base + QM_IFC_READY_STATUS); 1631 /* if VF respond, PF notifies VF successfully. */ 1632 if (!(val & BIT(fun_num))) 1633 goto err_unlock; 1634 1635 if (++cnt > QM_MAX_PF_WAIT_COUNT) { 1636 dev_err(dev, "failed to get response from VF(%u)!\n", fun_num); 1637 ret = -ETIMEDOUT; 1638 break; 1639 } 1640 } 1641 1642 err_unlock: 1643 mutex_unlock(&qm->mailbox_lock); 1644 return ret; 1645 } 1646 1647 static int qm_ping_all_vfs(struct hisi_qm *qm, u64 cmd) 1648 { 1649 struct device *dev = &qm->pdev->dev; 1650 u32 vfs_num = qm->vfs_num; 1651 struct qm_mailbox mailbox; 1652 u64 val = 0; 1653 int cnt = 0; 1654 int ret; 1655 u32 i; 1656 1657 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, QM_MB_PING_ALL_VFS, 0); 1658 mutex_lock(&qm->mailbox_lock); 1659 /* PF sends command to all VFs by mailbox */ 1660 ret = qm_mb_nolock(qm, &mailbox); 1661 if (ret) { 1662 dev_err(dev, "failed to send command to VFs!\n"); 1663 mutex_unlock(&qm->mailbox_lock); 1664 return ret; 1665 } 1666 1667 qm_trigger_vf_interrupt(qm, QM_IFC_SEND_ALL_VFS); 1668 while (true) { 1669 msleep(QM_WAIT_DST_ACK); 1670 val = readq(qm->io_base + QM_IFC_READY_STATUS); 1671 /* If all VFs acked, PF notifies VFs successfully. */ 1672 if (!(val & GENMASK(vfs_num, 1))) { 1673 mutex_unlock(&qm->mailbox_lock); 1674 return 0; 1675 } 1676 1677 if (++cnt > QM_MAX_PF_WAIT_COUNT) 1678 break; 1679 } 1680 1681 mutex_unlock(&qm->mailbox_lock); 1682 1683 /* Check which vf respond timeout. */ 1684 for (i = 1; i <= vfs_num; i++) { 1685 if (val & BIT(i)) 1686 dev_err(dev, "failed to get response from VF(%u)!\n", i); 1687 } 1688 1689 return -ETIMEDOUT; 1690 } 1691 1692 static int qm_ping_pf(struct hisi_qm *qm, u64 cmd) 1693 { 1694 struct qm_mailbox mailbox; 1695 int cnt = 0; 1696 u32 val; 1697 int ret; 1698 1699 qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, 0, 0); 1700 mutex_lock(&qm->mailbox_lock); 1701 ret = qm_mb_nolock(qm, &mailbox); 1702 if (ret) { 1703 dev_err(&qm->pdev->dev, "failed to send command to PF!\n"); 1704 goto unlock; 1705 } 1706 1707 qm_trigger_pf_interrupt(qm); 1708 /* Waiting for PF response */ 1709 while (true) { 1710 msleep(QM_WAIT_DST_ACK); 1711 val = readl(qm->io_base + QM_IFC_INT_SET_V); 1712 if (!(val & QM_IFC_INT_STATUS_MASK)) 1713 break; 1714 1715 if (++cnt > QM_MAX_VF_WAIT_COUNT) { 1716 ret = -ETIMEDOUT; 1717 break; 1718 } 1719 } 1720 1721 unlock: 1722 mutex_unlock(&qm->mailbox_lock); 1723 return ret; 1724 } 1725 1726 static int qm_stop_qp(struct hisi_qp *qp) 1727 { 1728 return hisi_qm_mb(qp->qm, QM_MB_CMD_STOP_QP, 0, qp->qp_id, 0); 1729 } 1730 1731 static int qm_set_msi(struct hisi_qm *qm, bool set) 1732 { 1733 struct pci_dev *pdev = qm->pdev; 1734 1735 if (set) { 1736 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64, 1737 0); 1738 } else { 1739 pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64, 1740 ACC_PEH_MSI_DISABLE); 1741 if (qm->err_status.is_qm_ecc_mbit || 1742 qm->err_status.is_dev_ecc_mbit) 1743 return 0; 1744 1745 mdelay(1); 1746 if (readl(qm->io_base + QM_PEH_DFX_INFO0)) 1747 return -EFAULT; 1748 } 1749 1750 return 0; 1751 } 1752 1753 static void qm_wait_msi_finish(struct hisi_qm *qm) 1754 { 1755 struct pci_dev *pdev = qm->pdev; 1756 u32 cmd = ~0; 1757 int cnt = 0; 1758 u32 val; 1759 int ret; 1760 1761 while (true) { 1762 pci_read_config_dword(pdev, pdev->msi_cap + 1763 PCI_MSI_PENDING_64, &cmd); 1764 if (!cmd) 1765 break; 1766 1767 if (++cnt > MAX_WAIT_COUNTS) { 1768 pci_warn(pdev, "failed to empty MSI PENDING!\n"); 1769 break; 1770 } 1771 1772 udelay(1); 1773 } 1774 1775 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO0, 1776 val, !(val & QM_PEH_DFX_MASK), 1777 POLL_PERIOD, POLL_TIMEOUT); 1778 if (ret) 1779 pci_warn(pdev, "failed to empty PEH MSI!\n"); 1780 1781 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO1, 1782 val, !(val & QM_PEH_MSI_FINISH_MASK), 1783 POLL_PERIOD, POLL_TIMEOUT); 1784 if (ret) 1785 pci_warn(pdev, "failed to finish MSI operation!\n"); 1786 } 1787 1788 static int qm_set_msi_v3(struct hisi_qm *qm, bool set) 1789 { 1790 struct pci_dev *pdev = qm->pdev; 1791 int ret = -ETIMEDOUT; 1792 u32 cmd, i; 1793 1794 pci_read_config_dword(pdev, pdev->msi_cap, &cmd); 1795 if (set) 1796 cmd |= QM_MSI_CAP_ENABLE; 1797 else 1798 cmd &= ~QM_MSI_CAP_ENABLE; 1799 1800 pci_write_config_dword(pdev, pdev->msi_cap, cmd); 1801 if (set) { 1802 for (i = 0; i < MAX_WAIT_COUNTS; i++) { 1803 pci_read_config_dword(pdev, pdev->msi_cap, &cmd); 1804 if (cmd & QM_MSI_CAP_ENABLE) 1805 return 0; 1806 1807 udelay(1); 1808 } 1809 } else { 1810 udelay(WAIT_PERIOD_US_MIN); 1811 qm_wait_msi_finish(qm); 1812 ret = 0; 1813 } 1814 1815 return ret; 1816 } 1817 1818 static const struct hisi_qm_hw_ops qm_hw_ops_v1 = { 1819 .qm_db = qm_db_v1, 1820 .hw_error_init = qm_hw_error_init_v1, 1821 .set_msi = qm_set_msi, 1822 }; 1823 1824 static const struct hisi_qm_hw_ops qm_hw_ops_v2 = { 1825 .get_vft = qm_get_vft_v2, 1826 .qm_db = qm_db_v2, 1827 .hw_error_init = qm_hw_error_init_v2, 1828 .hw_error_uninit = qm_hw_error_uninit_v2, 1829 .hw_error_handle = qm_hw_error_handle_v2, 1830 .set_msi = qm_set_msi, 1831 }; 1832 1833 static const struct hisi_qm_hw_ops qm_hw_ops_v3 = { 1834 .get_vft = qm_get_vft_v2, 1835 .qm_db = qm_db_v2, 1836 .hw_error_init = qm_hw_error_init_v3, 1837 .hw_error_uninit = qm_hw_error_uninit_v3, 1838 .hw_error_handle = qm_hw_error_handle_v2, 1839 .set_msi = qm_set_msi_v3, 1840 }; 1841 1842 static void *qm_get_avail_sqe(struct hisi_qp *qp) 1843 { 1844 struct hisi_qp_status *qp_status = &qp->qp_status; 1845 u16 sq_tail = qp_status->sq_tail; 1846 1847 if (unlikely(atomic_read(&qp->qp_status.used) == qp->sq_depth - 1)) 1848 return NULL; 1849 1850 return qp->sqe + sq_tail * qp->qm->sqe_size; 1851 } 1852 1853 static void hisi_qm_unset_hw_reset(struct hisi_qp *qp) 1854 { 1855 u64 *addr; 1856 1857 /* Use last 64 bits of DUS to reset status. */ 1858 addr = (u64 *)(qp->qdma.va + qp->qdma.size) - QM_RESET_STOP_TX_OFFSET; 1859 *addr = 0; 1860 } 1861 1862 static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type) 1863 { 1864 struct device *dev = &qm->pdev->dev; 1865 struct hisi_qp *qp; 1866 int qp_id; 1867 1868 if (!qm_qp_avail_state(qm, NULL, QP_INIT)) 1869 return ERR_PTR(-EPERM); 1870 1871 if (qm->qp_in_used == qm->qp_num) { 1872 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n", 1873 qm->qp_num); 1874 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt); 1875 return ERR_PTR(-EBUSY); 1876 } 1877 1878 qp_id = idr_alloc_cyclic(&qm->qp_idr, NULL, 0, qm->qp_num, GFP_ATOMIC); 1879 if (qp_id < 0) { 1880 dev_info_ratelimited(dev, "All %u queues of QM are busy!\n", 1881 qm->qp_num); 1882 atomic64_inc(&qm->debug.dfx.create_qp_err_cnt); 1883 return ERR_PTR(-EBUSY); 1884 } 1885 1886 qp = &qm->qp_array[qp_id]; 1887 hisi_qm_unset_hw_reset(qp); 1888 memset(qp->cqe, 0, sizeof(struct qm_cqe) * qp->cq_depth); 1889 1890 qp->event_cb = NULL; 1891 qp->req_cb = NULL; 1892 qp->qp_id = qp_id; 1893 qp->alg_type = alg_type; 1894 qp->is_in_kernel = true; 1895 qm->qp_in_used++; 1896 atomic_set(&qp->qp_status.flags, QP_INIT); 1897 1898 return qp; 1899 } 1900 1901 /** 1902 * hisi_qm_create_qp() - Create a queue pair from qm. 1903 * @qm: The qm we create a qp from. 1904 * @alg_type: Accelerator specific algorithm type in sqc. 1905 * 1906 * Return created qp, negative error code if failed. 1907 */ 1908 static struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type) 1909 { 1910 struct hisi_qp *qp; 1911 int ret; 1912 1913 ret = qm_pm_get_sync(qm); 1914 if (ret) 1915 return ERR_PTR(ret); 1916 1917 down_write(&qm->qps_lock); 1918 qp = qm_create_qp_nolock(qm, alg_type); 1919 up_write(&qm->qps_lock); 1920 1921 if (IS_ERR(qp)) 1922 qm_pm_put_sync(qm); 1923 1924 return qp; 1925 } 1926 1927 /** 1928 * hisi_qm_release_qp() - Release a qp back to its qm. 1929 * @qp: The qp we want to release. 1930 * 1931 * This function releases the resource of a qp. 1932 */ 1933 static void hisi_qm_release_qp(struct hisi_qp *qp) 1934 { 1935 struct hisi_qm *qm = qp->qm; 1936 1937 down_write(&qm->qps_lock); 1938 1939 if (!qm_qp_avail_state(qm, qp, QP_CLOSE)) { 1940 up_write(&qm->qps_lock); 1941 return; 1942 } 1943 1944 qm->qp_in_used--; 1945 idr_remove(&qm->qp_idr, qp->qp_id); 1946 1947 up_write(&qm->qps_lock); 1948 1949 qm_pm_put_sync(qm); 1950 } 1951 1952 static int qm_sq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 1953 { 1954 struct hisi_qm *qm = qp->qm; 1955 struct device *dev = &qm->pdev->dev; 1956 enum qm_hw_ver ver = qm->ver; 1957 struct qm_sqc *sqc; 1958 dma_addr_t sqc_dma; 1959 int ret; 1960 1961 sqc = kzalloc(sizeof(struct qm_sqc), GFP_KERNEL); 1962 if (!sqc) 1963 return -ENOMEM; 1964 1965 INIT_QC_COMMON(sqc, qp->sqe_dma, pasid); 1966 if (ver == QM_HW_V1) { 1967 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm->sqe_size)); 1968 sqc->w8 = cpu_to_le16(qp->sq_depth - 1); 1969 } else { 1970 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V2(qm->sqe_size, qp->sq_depth)); 1971 sqc->w8 = 0; /* rand_qc */ 1972 } 1973 sqc->cq_num = cpu_to_le16(qp_id); 1974 sqc->w13 = cpu_to_le16(QM_MK_SQC_W13(0, 1, qp->alg_type)); 1975 1976 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel) 1977 sqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE << 1978 QM_QC_PASID_ENABLE_SHIFT); 1979 1980 sqc_dma = dma_map_single(dev, sqc, sizeof(struct qm_sqc), 1981 DMA_TO_DEVICE); 1982 if (dma_mapping_error(dev, sqc_dma)) { 1983 kfree(sqc); 1984 return -ENOMEM; 1985 } 1986 1987 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC, sqc_dma, qp_id, 0); 1988 dma_unmap_single(dev, sqc_dma, sizeof(struct qm_sqc), DMA_TO_DEVICE); 1989 kfree(sqc); 1990 1991 return ret; 1992 } 1993 1994 static int qm_cq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 1995 { 1996 struct hisi_qm *qm = qp->qm; 1997 struct device *dev = &qm->pdev->dev; 1998 enum qm_hw_ver ver = qm->ver; 1999 struct qm_cqc *cqc; 2000 dma_addr_t cqc_dma; 2001 int ret; 2002 2003 cqc = kzalloc(sizeof(struct qm_cqc), GFP_KERNEL); 2004 if (!cqc) 2005 return -ENOMEM; 2006 2007 INIT_QC_COMMON(cqc, qp->cqe_dma, pasid); 2008 if (ver == QM_HW_V1) { 2009 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0, 2010 QM_QC_CQE_SIZE)); 2011 cqc->w8 = cpu_to_le16(qp->cq_depth - 1); 2012 } else { 2013 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V2(QM_QC_CQE_SIZE, qp->cq_depth)); 2014 cqc->w8 = 0; /* rand_qc */ 2015 } 2016 cqc->dw6 = cpu_to_le32(1 << QM_CQ_PHASE_SHIFT | 1 << QM_CQ_FLAG_SHIFT); 2017 2018 if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel) 2019 cqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE); 2020 2021 cqc_dma = dma_map_single(dev, cqc, sizeof(struct qm_cqc), 2022 DMA_TO_DEVICE); 2023 if (dma_mapping_error(dev, cqc_dma)) { 2024 kfree(cqc); 2025 return -ENOMEM; 2026 } 2027 2028 ret = hisi_qm_mb(qm, QM_MB_CMD_CQC, cqc_dma, qp_id, 0); 2029 dma_unmap_single(dev, cqc_dma, sizeof(struct qm_cqc), DMA_TO_DEVICE); 2030 kfree(cqc); 2031 2032 return ret; 2033 } 2034 2035 static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid) 2036 { 2037 int ret; 2038 2039 qm_init_qp_status(qp); 2040 2041 ret = qm_sq_ctx_cfg(qp, qp_id, pasid); 2042 if (ret) 2043 return ret; 2044 2045 return qm_cq_ctx_cfg(qp, qp_id, pasid); 2046 } 2047 2048 static int qm_start_qp_nolock(struct hisi_qp *qp, unsigned long arg) 2049 { 2050 struct hisi_qm *qm = qp->qm; 2051 struct device *dev = &qm->pdev->dev; 2052 int qp_id = qp->qp_id; 2053 u32 pasid = arg; 2054 int ret; 2055 2056 if (!qm_qp_avail_state(qm, qp, QP_START)) 2057 return -EPERM; 2058 2059 ret = qm_qp_ctx_cfg(qp, qp_id, pasid); 2060 if (ret) 2061 return ret; 2062 2063 atomic_set(&qp->qp_status.flags, QP_START); 2064 dev_dbg(dev, "queue %d started\n", qp_id); 2065 2066 return 0; 2067 } 2068 2069 /** 2070 * hisi_qm_start_qp() - Start a qp into running. 2071 * @qp: The qp we want to start to run. 2072 * @arg: Accelerator specific argument. 2073 * 2074 * After this function, qp can receive request from user. Return 0 if 2075 * successful, negative error code if failed. 2076 */ 2077 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg) 2078 { 2079 struct hisi_qm *qm = qp->qm; 2080 int ret; 2081 2082 down_write(&qm->qps_lock); 2083 ret = qm_start_qp_nolock(qp, arg); 2084 up_write(&qm->qps_lock); 2085 2086 return ret; 2087 } 2088 EXPORT_SYMBOL_GPL(hisi_qm_start_qp); 2089 2090 /** 2091 * qp_stop_fail_cb() - call request cb. 2092 * @qp: stopped failed qp. 2093 * 2094 * Callback function should be called whether task completed or not. 2095 */ 2096 static void qp_stop_fail_cb(struct hisi_qp *qp) 2097 { 2098 int qp_used = atomic_read(&qp->qp_status.used); 2099 u16 cur_tail = qp->qp_status.sq_tail; 2100 u16 sq_depth = qp->sq_depth; 2101 u16 cur_head = (cur_tail + sq_depth - qp_used) % sq_depth; 2102 struct hisi_qm *qm = qp->qm; 2103 u16 pos; 2104 int i; 2105 2106 for (i = 0; i < qp_used; i++) { 2107 pos = (i + cur_head) % sq_depth; 2108 qp->req_cb(qp, qp->sqe + (u32)(qm->sqe_size * pos)); 2109 atomic_dec(&qp->qp_status.used); 2110 } 2111 } 2112 2113 /** 2114 * qm_drain_qp() - Drain a qp. 2115 * @qp: The qp we want to drain. 2116 * 2117 * Determine whether the queue is cleared by judging the tail pointers of 2118 * sq and cq. 2119 */ 2120 static int qm_drain_qp(struct hisi_qp *qp) 2121 { 2122 size_t size = sizeof(struct qm_sqc) + sizeof(struct qm_cqc); 2123 struct hisi_qm *qm = qp->qm; 2124 struct device *dev = &qm->pdev->dev; 2125 struct qm_sqc *sqc; 2126 struct qm_cqc *cqc; 2127 dma_addr_t dma_addr; 2128 int ret = 0, i = 0; 2129 void *addr; 2130 2131 /* No need to judge if master OOO is blocked. */ 2132 if (qm_check_dev_error(qm)) 2133 return 0; 2134 2135 /* Kunpeng930 supports drain qp by device */ 2136 if (test_bit(QM_SUPPORT_STOP_QP, &qm->caps)) { 2137 ret = qm_stop_qp(qp); 2138 if (ret) 2139 dev_err(dev, "Failed to stop qp(%u)!\n", qp->qp_id); 2140 return ret; 2141 } 2142 2143 addr = hisi_qm_ctx_alloc(qm, size, &dma_addr); 2144 if (IS_ERR(addr)) { 2145 dev_err(dev, "Failed to alloc ctx for sqc and cqc!\n"); 2146 return -ENOMEM; 2147 } 2148 2149 while (++i) { 2150 ret = qm_dump_sqc_raw(qm, dma_addr, qp->qp_id); 2151 if (ret) { 2152 dev_err_ratelimited(dev, "Failed to dump sqc!\n"); 2153 break; 2154 } 2155 sqc = addr; 2156 2157 ret = qm_dump_cqc_raw(qm, (dma_addr + sizeof(struct qm_sqc)), 2158 qp->qp_id); 2159 if (ret) { 2160 dev_err_ratelimited(dev, "Failed to dump cqc!\n"); 2161 break; 2162 } 2163 cqc = addr + sizeof(struct qm_sqc); 2164 2165 if ((sqc->tail == cqc->tail) && 2166 (QM_SQ_TAIL_IDX(sqc) == QM_CQ_TAIL_IDX(cqc))) 2167 break; 2168 2169 if (i == MAX_WAIT_COUNTS) { 2170 dev_err(dev, "Fail to empty queue %u!\n", qp->qp_id); 2171 ret = -EBUSY; 2172 break; 2173 } 2174 2175 usleep_range(WAIT_PERIOD_US_MIN, WAIT_PERIOD_US_MAX); 2176 } 2177 2178 hisi_qm_ctx_free(qm, size, addr, &dma_addr); 2179 2180 return ret; 2181 } 2182 2183 static int qm_stop_qp_nolock(struct hisi_qp *qp) 2184 { 2185 struct device *dev = &qp->qm->pdev->dev; 2186 int ret; 2187 2188 /* 2189 * It is allowed to stop and release qp when reset, If the qp is 2190 * stopped when reset but still want to be released then, the 2191 * is_resetting flag should be set negative so that this qp will not 2192 * be restarted after reset. 2193 */ 2194 if (atomic_read(&qp->qp_status.flags) == QP_STOP) { 2195 qp->is_resetting = false; 2196 return 0; 2197 } 2198 2199 if (!qm_qp_avail_state(qp->qm, qp, QP_STOP)) 2200 return -EPERM; 2201 2202 atomic_set(&qp->qp_status.flags, QP_STOP); 2203 2204 ret = qm_drain_qp(qp); 2205 if (ret) 2206 dev_err(dev, "Failed to drain out data for stopping!\n"); 2207 2208 2209 flush_workqueue(qp->qm->wq); 2210 if (unlikely(qp->is_resetting && atomic_read(&qp->qp_status.used))) 2211 qp_stop_fail_cb(qp); 2212 2213 dev_dbg(dev, "stop queue %u!", qp->qp_id); 2214 2215 return 0; 2216 } 2217 2218 /** 2219 * hisi_qm_stop_qp() - Stop a qp in qm. 2220 * @qp: The qp we want to stop. 2221 * 2222 * This function is reverse of hisi_qm_start_qp. Return 0 if successful. 2223 */ 2224 int hisi_qm_stop_qp(struct hisi_qp *qp) 2225 { 2226 int ret; 2227 2228 down_write(&qp->qm->qps_lock); 2229 ret = qm_stop_qp_nolock(qp); 2230 up_write(&qp->qm->qps_lock); 2231 2232 return ret; 2233 } 2234 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp); 2235 2236 /** 2237 * hisi_qp_send() - Queue up a task in the hardware queue. 2238 * @qp: The qp in which to put the message. 2239 * @msg: The message. 2240 * 2241 * This function will return -EBUSY if qp is currently full, and -EAGAIN 2242 * if qp related qm is resetting. 2243 * 2244 * Note: This function may run with qm_irq_thread and ACC reset at same time. 2245 * It has no race with qm_irq_thread. However, during hisi_qp_send, ACC 2246 * reset may happen, we have no lock here considering performance. This 2247 * causes current qm_db sending fail or can not receive sended sqe. QM 2248 * sync/async receive function should handle the error sqe. ACC reset 2249 * done function should clear used sqe to 0. 2250 */ 2251 int hisi_qp_send(struct hisi_qp *qp, const void *msg) 2252 { 2253 struct hisi_qp_status *qp_status = &qp->qp_status; 2254 u16 sq_tail = qp_status->sq_tail; 2255 u16 sq_tail_next = (sq_tail + 1) % qp->sq_depth; 2256 void *sqe = qm_get_avail_sqe(qp); 2257 2258 if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP || 2259 atomic_read(&qp->qm->status.flags) == QM_STOP || 2260 qp->is_resetting)) { 2261 dev_info_ratelimited(&qp->qm->pdev->dev, "QP is stopped or resetting\n"); 2262 return -EAGAIN; 2263 } 2264 2265 if (!sqe) 2266 return -EBUSY; 2267 2268 memcpy(sqe, msg, qp->qm->sqe_size); 2269 2270 qm_db(qp->qm, qp->qp_id, QM_DOORBELL_CMD_SQ, sq_tail_next, 0); 2271 atomic_inc(&qp->qp_status.used); 2272 qp_status->sq_tail = sq_tail_next; 2273 2274 return 0; 2275 } 2276 EXPORT_SYMBOL_GPL(hisi_qp_send); 2277 2278 static void hisi_qm_cache_wb(struct hisi_qm *qm) 2279 { 2280 unsigned int val; 2281 2282 if (qm->ver == QM_HW_V1) 2283 return; 2284 2285 writel(0x1, qm->io_base + QM_CACHE_WB_START); 2286 if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE, 2287 val, val & BIT(0), POLL_PERIOD, 2288 POLL_TIMEOUT)) 2289 dev_err(&qm->pdev->dev, "QM writeback sqc cache fail!\n"); 2290 } 2291 2292 static void qm_qp_event_notifier(struct hisi_qp *qp) 2293 { 2294 wake_up_interruptible(&qp->uacce_q->wait); 2295 } 2296 2297 /* This function returns free number of qp in qm. */ 2298 static int hisi_qm_get_available_instances(struct uacce_device *uacce) 2299 { 2300 struct hisi_qm *qm = uacce->priv; 2301 int ret; 2302 2303 down_read(&qm->qps_lock); 2304 ret = qm->qp_num - qm->qp_in_used; 2305 up_read(&qm->qps_lock); 2306 2307 return ret; 2308 } 2309 2310 static void hisi_qm_set_hw_reset(struct hisi_qm *qm, int offset) 2311 { 2312 int i; 2313 2314 for (i = 0; i < qm->qp_num; i++) 2315 qm_set_qp_disable(&qm->qp_array[i], offset); 2316 } 2317 2318 static int hisi_qm_uacce_get_queue(struct uacce_device *uacce, 2319 unsigned long arg, 2320 struct uacce_queue *q) 2321 { 2322 struct hisi_qm *qm = uacce->priv; 2323 struct hisi_qp *qp; 2324 u8 alg_type = 0; 2325 2326 qp = hisi_qm_create_qp(qm, alg_type); 2327 if (IS_ERR(qp)) 2328 return PTR_ERR(qp); 2329 2330 q->priv = qp; 2331 q->uacce = uacce; 2332 qp->uacce_q = q; 2333 qp->event_cb = qm_qp_event_notifier; 2334 qp->pasid = arg; 2335 qp->is_in_kernel = false; 2336 2337 return 0; 2338 } 2339 2340 static void hisi_qm_uacce_put_queue(struct uacce_queue *q) 2341 { 2342 struct hisi_qp *qp = q->priv; 2343 2344 hisi_qm_release_qp(qp); 2345 } 2346 2347 /* map sq/cq/doorbell to user space */ 2348 static int hisi_qm_uacce_mmap(struct uacce_queue *q, 2349 struct vm_area_struct *vma, 2350 struct uacce_qfile_region *qfr) 2351 { 2352 struct hisi_qp *qp = q->priv; 2353 struct hisi_qm *qm = qp->qm; 2354 resource_size_t phys_base = qm->db_phys_base + 2355 qp->qp_id * qm->db_interval; 2356 size_t sz = vma->vm_end - vma->vm_start; 2357 struct pci_dev *pdev = qm->pdev; 2358 struct device *dev = &pdev->dev; 2359 unsigned long vm_pgoff; 2360 int ret; 2361 2362 switch (qfr->type) { 2363 case UACCE_QFRT_MMIO: 2364 if (qm->ver == QM_HW_V1) { 2365 if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR) 2366 return -EINVAL; 2367 } else if (!test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps)) { 2368 if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR + 2369 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE)) 2370 return -EINVAL; 2371 } else { 2372 if (sz > qm->db_interval) 2373 return -EINVAL; 2374 } 2375 2376 vm_flags_set(vma, VM_IO); 2377 2378 return remap_pfn_range(vma, vma->vm_start, 2379 phys_base >> PAGE_SHIFT, 2380 sz, pgprot_noncached(vma->vm_page_prot)); 2381 case UACCE_QFRT_DUS: 2382 if (sz != qp->qdma.size) 2383 return -EINVAL; 2384 2385 /* 2386 * dma_mmap_coherent() requires vm_pgoff as 0 2387 * restore vm_pfoff to initial value for mmap() 2388 */ 2389 vm_pgoff = vma->vm_pgoff; 2390 vma->vm_pgoff = 0; 2391 ret = dma_mmap_coherent(dev, vma, qp->qdma.va, 2392 qp->qdma.dma, sz); 2393 vma->vm_pgoff = vm_pgoff; 2394 return ret; 2395 2396 default: 2397 return -EINVAL; 2398 } 2399 } 2400 2401 static int hisi_qm_uacce_start_queue(struct uacce_queue *q) 2402 { 2403 struct hisi_qp *qp = q->priv; 2404 2405 return hisi_qm_start_qp(qp, qp->pasid); 2406 } 2407 2408 static void hisi_qm_uacce_stop_queue(struct uacce_queue *q) 2409 { 2410 hisi_qm_stop_qp(q->priv); 2411 } 2412 2413 static int hisi_qm_is_q_updated(struct uacce_queue *q) 2414 { 2415 struct hisi_qp *qp = q->priv; 2416 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head; 2417 int updated = 0; 2418 2419 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) { 2420 /* make sure to read data from memory */ 2421 dma_rmb(); 2422 qm_cq_head_update(qp); 2423 cqe = qp->cqe + qp->qp_status.cq_head; 2424 updated = 1; 2425 } 2426 2427 return updated; 2428 } 2429 2430 static void qm_set_sqctype(struct uacce_queue *q, u16 type) 2431 { 2432 struct hisi_qm *qm = q->uacce->priv; 2433 struct hisi_qp *qp = q->priv; 2434 2435 down_write(&qm->qps_lock); 2436 qp->alg_type = type; 2437 up_write(&qm->qps_lock); 2438 } 2439 2440 static long hisi_qm_uacce_ioctl(struct uacce_queue *q, unsigned int cmd, 2441 unsigned long arg) 2442 { 2443 struct hisi_qp *qp = q->priv; 2444 struct hisi_qp_info qp_info; 2445 struct hisi_qp_ctx qp_ctx; 2446 2447 if (cmd == UACCE_CMD_QM_SET_QP_CTX) { 2448 if (copy_from_user(&qp_ctx, (void __user *)arg, 2449 sizeof(struct hisi_qp_ctx))) 2450 return -EFAULT; 2451 2452 if (qp_ctx.qc_type != 0 && qp_ctx.qc_type != 1) 2453 return -EINVAL; 2454 2455 qm_set_sqctype(q, qp_ctx.qc_type); 2456 qp_ctx.id = qp->qp_id; 2457 2458 if (copy_to_user((void __user *)arg, &qp_ctx, 2459 sizeof(struct hisi_qp_ctx))) 2460 return -EFAULT; 2461 2462 return 0; 2463 } else if (cmd == UACCE_CMD_QM_SET_QP_INFO) { 2464 if (copy_from_user(&qp_info, (void __user *)arg, 2465 sizeof(struct hisi_qp_info))) 2466 return -EFAULT; 2467 2468 qp_info.sqe_size = qp->qm->sqe_size; 2469 qp_info.sq_depth = qp->sq_depth; 2470 qp_info.cq_depth = qp->cq_depth; 2471 2472 if (copy_to_user((void __user *)arg, &qp_info, 2473 sizeof(struct hisi_qp_info))) 2474 return -EFAULT; 2475 2476 return 0; 2477 } 2478 2479 return -EINVAL; 2480 } 2481 2482 /** 2483 * qm_hw_err_isolate() - Try to set the isolation status of the uacce device 2484 * according to user's configuration of error threshold. 2485 * @qm: the uacce device 2486 */ 2487 static int qm_hw_err_isolate(struct hisi_qm *qm) 2488 { 2489 struct qm_hw_err *err, *tmp, *hw_err; 2490 struct qm_err_isolate *isolate; 2491 u32 count = 0; 2492 2493 isolate = &qm->isolate_data; 2494 2495 #define SECONDS_PER_HOUR 3600 2496 2497 /* All the hw errs are processed by PF driver */ 2498 if (qm->uacce->is_vf || isolate->is_isolate || !isolate->err_threshold) 2499 return 0; 2500 2501 hw_err = kzalloc(sizeof(*hw_err), GFP_KERNEL); 2502 if (!hw_err) 2503 return -ENOMEM; 2504 2505 /* 2506 * Time-stamp every slot AER error. Then check the AER error log when the 2507 * next device AER error occurred. if the device slot AER error count exceeds 2508 * the setting error threshold in one hour, the isolated state will be set 2509 * to true. And the AER error logs that exceed one hour will be cleared. 2510 */ 2511 mutex_lock(&isolate->isolate_lock); 2512 hw_err->timestamp = jiffies; 2513 list_for_each_entry_safe(err, tmp, &isolate->qm_hw_errs, list) { 2514 if ((hw_err->timestamp - err->timestamp) / HZ > 2515 SECONDS_PER_HOUR) { 2516 list_del(&err->list); 2517 kfree(err); 2518 } else { 2519 count++; 2520 } 2521 } 2522 list_add(&hw_err->list, &isolate->qm_hw_errs); 2523 mutex_unlock(&isolate->isolate_lock); 2524 2525 if (count >= isolate->err_threshold) 2526 isolate->is_isolate = true; 2527 2528 return 0; 2529 } 2530 2531 static void qm_hw_err_destroy(struct hisi_qm *qm) 2532 { 2533 struct qm_hw_err *err, *tmp; 2534 2535 mutex_lock(&qm->isolate_data.isolate_lock); 2536 list_for_each_entry_safe(err, tmp, &qm->isolate_data.qm_hw_errs, list) { 2537 list_del(&err->list); 2538 kfree(err); 2539 } 2540 mutex_unlock(&qm->isolate_data.isolate_lock); 2541 } 2542 2543 static enum uacce_dev_state hisi_qm_get_isolate_state(struct uacce_device *uacce) 2544 { 2545 struct hisi_qm *qm = uacce->priv; 2546 struct hisi_qm *pf_qm; 2547 2548 if (uacce->is_vf) 2549 pf_qm = pci_get_drvdata(pci_physfn(qm->pdev)); 2550 else 2551 pf_qm = qm; 2552 2553 return pf_qm->isolate_data.is_isolate ? 2554 UACCE_DEV_ISOLATE : UACCE_DEV_NORMAL; 2555 } 2556 2557 static int hisi_qm_isolate_threshold_write(struct uacce_device *uacce, u32 num) 2558 { 2559 struct hisi_qm *qm = uacce->priv; 2560 2561 /* Must be set by PF */ 2562 if (uacce->is_vf) 2563 return -EPERM; 2564 2565 if (qm->isolate_data.is_isolate) 2566 return -EPERM; 2567 2568 qm->isolate_data.err_threshold = num; 2569 2570 /* After the policy is updated, need to reset the hardware err list */ 2571 qm_hw_err_destroy(qm); 2572 2573 return 0; 2574 } 2575 2576 static u32 hisi_qm_isolate_threshold_read(struct uacce_device *uacce) 2577 { 2578 struct hisi_qm *qm = uacce->priv; 2579 struct hisi_qm *pf_qm; 2580 2581 if (uacce->is_vf) { 2582 pf_qm = pci_get_drvdata(pci_physfn(qm->pdev)); 2583 return pf_qm->isolate_data.err_threshold; 2584 } 2585 2586 return qm->isolate_data.err_threshold; 2587 } 2588 2589 static const struct uacce_ops uacce_qm_ops = { 2590 .get_available_instances = hisi_qm_get_available_instances, 2591 .get_queue = hisi_qm_uacce_get_queue, 2592 .put_queue = hisi_qm_uacce_put_queue, 2593 .start_queue = hisi_qm_uacce_start_queue, 2594 .stop_queue = hisi_qm_uacce_stop_queue, 2595 .mmap = hisi_qm_uacce_mmap, 2596 .ioctl = hisi_qm_uacce_ioctl, 2597 .is_q_updated = hisi_qm_is_q_updated, 2598 .get_isolate_state = hisi_qm_get_isolate_state, 2599 .isolate_err_threshold_write = hisi_qm_isolate_threshold_write, 2600 .isolate_err_threshold_read = hisi_qm_isolate_threshold_read, 2601 }; 2602 2603 static void qm_remove_uacce(struct hisi_qm *qm) 2604 { 2605 struct uacce_device *uacce = qm->uacce; 2606 2607 if (qm->use_sva) { 2608 qm_hw_err_destroy(qm); 2609 uacce_remove(uacce); 2610 qm->uacce = NULL; 2611 } 2612 } 2613 2614 static int qm_alloc_uacce(struct hisi_qm *qm) 2615 { 2616 struct pci_dev *pdev = qm->pdev; 2617 struct uacce_device *uacce; 2618 unsigned long mmio_page_nr; 2619 unsigned long dus_page_nr; 2620 u16 sq_depth, cq_depth; 2621 struct uacce_interface interface = { 2622 .flags = UACCE_DEV_SVA, 2623 .ops = &uacce_qm_ops, 2624 }; 2625 int ret; 2626 2627 ret = strscpy(interface.name, dev_driver_string(&pdev->dev), 2628 sizeof(interface.name)); 2629 if (ret < 0) 2630 return -ENAMETOOLONG; 2631 2632 uacce = uacce_alloc(&pdev->dev, &interface); 2633 if (IS_ERR(uacce)) 2634 return PTR_ERR(uacce); 2635 2636 if (uacce->flags & UACCE_DEV_SVA) { 2637 qm->use_sva = true; 2638 } else { 2639 /* only consider sva case */ 2640 qm_remove_uacce(qm); 2641 return -EINVAL; 2642 } 2643 2644 uacce->is_vf = pdev->is_virtfn; 2645 uacce->priv = qm; 2646 2647 if (qm->ver == QM_HW_V1) 2648 uacce->api_ver = HISI_QM_API_VER_BASE; 2649 else if (qm->ver == QM_HW_V2) 2650 uacce->api_ver = HISI_QM_API_VER2_BASE; 2651 else 2652 uacce->api_ver = HISI_QM_API_VER3_BASE; 2653 2654 if (qm->ver == QM_HW_V1) 2655 mmio_page_nr = QM_DOORBELL_PAGE_NR; 2656 else if (!test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps)) 2657 mmio_page_nr = QM_DOORBELL_PAGE_NR + 2658 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE; 2659 else 2660 mmio_page_nr = qm->db_interval / PAGE_SIZE; 2661 2662 qm_get_xqc_depth(qm, &sq_depth, &cq_depth, QM_QP_DEPTH_CAP); 2663 2664 /* Add one more page for device or qp status */ 2665 dus_page_nr = (PAGE_SIZE - 1 + qm->sqe_size * sq_depth + 2666 sizeof(struct qm_cqe) * cq_depth + PAGE_SIZE) >> 2667 PAGE_SHIFT; 2668 2669 uacce->qf_pg_num[UACCE_QFRT_MMIO] = mmio_page_nr; 2670 uacce->qf_pg_num[UACCE_QFRT_DUS] = dus_page_nr; 2671 2672 qm->uacce = uacce; 2673 INIT_LIST_HEAD(&qm->isolate_data.qm_hw_errs); 2674 mutex_init(&qm->isolate_data.isolate_lock); 2675 2676 return 0; 2677 } 2678 2679 /** 2680 * qm_frozen() - Try to froze QM to cut continuous queue request. If 2681 * there is user on the QM, return failure without doing anything. 2682 * @qm: The qm needed to be fronzen. 2683 * 2684 * This function frozes QM, then we can do SRIOV disabling. 2685 */ 2686 static int qm_frozen(struct hisi_qm *qm) 2687 { 2688 if (test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl)) 2689 return 0; 2690 2691 down_write(&qm->qps_lock); 2692 2693 if (!qm->qp_in_used) { 2694 qm->qp_in_used = qm->qp_num; 2695 up_write(&qm->qps_lock); 2696 set_bit(QM_DRIVER_REMOVING, &qm->misc_ctl); 2697 return 0; 2698 } 2699 2700 up_write(&qm->qps_lock); 2701 2702 return -EBUSY; 2703 } 2704 2705 static int qm_try_frozen_vfs(struct pci_dev *pdev, 2706 struct hisi_qm_list *qm_list) 2707 { 2708 struct hisi_qm *qm, *vf_qm; 2709 struct pci_dev *dev; 2710 int ret = 0; 2711 2712 if (!qm_list || !pdev) 2713 return -EINVAL; 2714 2715 /* Try to frozen all the VFs as disable SRIOV */ 2716 mutex_lock(&qm_list->lock); 2717 list_for_each_entry(qm, &qm_list->list, list) { 2718 dev = qm->pdev; 2719 if (dev == pdev) 2720 continue; 2721 if (pci_physfn(dev) == pdev) { 2722 vf_qm = pci_get_drvdata(dev); 2723 ret = qm_frozen(vf_qm); 2724 if (ret) 2725 goto frozen_fail; 2726 } 2727 } 2728 2729 frozen_fail: 2730 mutex_unlock(&qm_list->lock); 2731 2732 return ret; 2733 } 2734 2735 /** 2736 * hisi_qm_wait_task_finish() - Wait until the task is finished 2737 * when removing the driver. 2738 * @qm: The qm needed to wait for the task to finish. 2739 * @qm_list: The list of all available devices. 2740 */ 2741 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 2742 { 2743 while (qm_frozen(qm) || 2744 ((qm->fun_type == QM_HW_PF) && 2745 qm_try_frozen_vfs(qm->pdev, qm_list))) { 2746 msleep(WAIT_PERIOD); 2747 } 2748 2749 while (test_bit(QM_RST_SCHED, &qm->misc_ctl) || 2750 test_bit(QM_RESETTING, &qm->misc_ctl)) 2751 msleep(WAIT_PERIOD); 2752 2753 if (test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) 2754 flush_work(&qm->cmd_process); 2755 2756 udelay(REMOVE_WAIT_DELAY); 2757 } 2758 EXPORT_SYMBOL_GPL(hisi_qm_wait_task_finish); 2759 2760 static void hisi_qp_memory_uninit(struct hisi_qm *qm, int num) 2761 { 2762 struct device *dev = &qm->pdev->dev; 2763 struct qm_dma *qdma; 2764 int i; 2765 2766 for (i = num - 1; i >= 0; i--) { 2767 qdma = &qm->qp_array[i].qdma; 2768 dma_free_coherent(dev, qdma->size, qdma->va, qdma->dma); 2769 kfree(qm->poll_data[i].qp_finish_id); 2770 } 2771 2772 kfree(qm->poll_data); 2773 kfree(qm->qp_array); 2774 } 2775 2776 static int hisi_qp_memory_init(struct hisi_qm *qm, size_t dma_size, int id, 2777 u16 sq_depth, u16 cq_depth) 2778 { 2779 struct device *dev = &qm->pdev->dev; 2780 size_t off = qm->sqe_size * sq_depth; 2781 struct hisi_qp *qp; 2782 int ret = -ENOMEM; 2783 2784 qm->poll_data[id].qp_finish_id = kcalloc(qm->qp_num, sizeof(u16), 2785 GFP_KERNEL); 2786 if (!qm->poll_data[id].qp_finish_id) 2787 return -ENOMEM; 2788 2789 qp = &qm->qp_array[id]; 2790 qp->qdma.va = dma_alloc_coherent(dev, dma_size, &qp->qdma.dma, 2791 GFP_KERNEL); 2792 if (!qp->qdma.va) 2793 goto err_free_qp_finish_id; 2794 2795 qp->sqe = qp->qdma.va; 2796 qp->sqe_dma = qp->qdma.dma; 2797 qp->cqe = qp->qdma.va + off; 2798 qp->cqe_dma = qp->qdma.dma + off; 2799 qp->qdma.size = dma_size; 2800 qp->sq_depth = sq_depth; 2801 qp->cq_depth = cq_depth; 2802 qp->qm = qm; 2803 qp->qp_id = id; 2804 2805 return 0; 2806 2807 err_free_qp_finish_id: 2808 kfree(qm->poll_data[id].qp_finish_id); 2809 return ret; 2810 } 2811 2812 static void hisi_qm_pre_init(struct hisi_qm *qm) 2813 { 2814 struct pci_dev *pdev = qm->pdev; 2815 2816 if (qm->ver == QM_HW_V1) 2817 qm->ops = &qm_hw_ops_v1; 2818 else if (qm->ver == QM_HW_V2) 2819 qm->ops = &qm_hw_ops_v2; 2820 else 2821 qm->ops = &qm_hw_ops_v3; 2822 2823 pci_set_drvdata(pdev, qm); 2824 mutex_init(&qm->mailbox_lock); 2825 init_rwsem(&qm->qps_lock); 2826 qm->qp_in_used = 0; 2827 if (test_bit(QM_SUPPORT_RPM, &qm->caps)) { 2828 if (!acpi_device_power_manageable(ACPI_COMPANION(&pdev->dev))) 2829 dev_info(&pdev->dev, "_PS0 and _PR0 are not defined"); 2830 } 2831 } 2832 2833 static void qm_cmd_uninit(struct hisi_qm *qm) 2834 { 2835 u32 val; 2836 2837 if (!test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) 2838 return; 2839 2840 val = readl(qm->io_base + QM_IFC_INT_MASK); 2841 val |= QM_IFC_INT_DISABLE; 2842 writel(val, qm->io_base + QM_IFC_INT_MASK); 2843 } 2844 2845 static void qm_cmd_init(struct hisi_qm *qm) 2846 { 2847 u32 val; 2848 2849 if (!test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) 2850 return; 2851 2852 /* Clear communication interrupt source */ 2853 qm_clear_cmd_interrupt(qm, QM_IFC_INT_SOURCE_CLR); 2854 2855 /* Enable pf to vf communication reg. */ 2856 val = readl(qm->io_base + QM_IFC_INT_MASK); 2857 val &= ~QM_IFC_INT_DISABLE; 2858 writel(val, qm->io_base + QM_IFC_INT_MASK); 2859 } 2860 2861 static void qm_put_pci_res(struct hisi_qm *qm) 2862 { 2863 struct pci_dev *pdev = qm->pdev; 2864 2865 if (test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps)) 2866 iounmap(qm->db_io_base); 2867 2868 iounmap(qm->io_base); 2869 pci_release_mem_regions(pdev); 2870 } 2871 2872 static void hisi_qm_pci_uninit(struct hisi_qm *qm) 2873 { 2874 struct pci_dev *pdev = qm->pdev; 2875 2876 pci_free_irq_vectors(pdev); 2877 qm_put_pci_res(qm); 2878 pci_disable_device(pdev); 2879 } 2880 2881 static void hisi_qm_set_state(struct hisi_qm *qm, u8 state) 2882 { 2883 if (qm->ver > QM_HW_V2 && qm->fun_type == QM_HW_VF) 2884 writel(state, qm->io_base + QM_VF_STATE); 2885 } 2886 2887 static void hisi_qm_unint_work(struct hisi_qm *qm) 2888 { 2889 destroy_workqueue(qm->wq); 2890 } 2891 2892 static void hisi_qm_memory_uninit(struct hisi_qm *qm) 2893 { 2894 struct device *dev = &qm->pdev->dev; 2895 2896 hisi_qp_memory_uninit(qm, qm->qp_num); 2897 if (qm->qdma.va) { 2898 hisi_qm_cache_wb(qm); 2899 dma_free_coherent(dev, qm->qdma.size, 2900 qm->qdma.va, qm->qdma.dma); 2901 } 2902 2903 idr_destroy(&qm->qp_idr); 2904 2905 if (test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) 2906 kfree(qm->factor); 2907 } 2908 2909 /** 2910 * hisi_qm_uninit() - Uninitialize qm. 2911 * @qm: The qm needed uninit. 2912 * 2913 * This function uninits qm related device resources. 2914 */ 2915 void hisi_qm_uninit(struct hisi_qm *qm) 2916 { 2917 qm_cmd_uninit(qm); 2918 hisi_qm_unint_work(qm); 2919 down_write(&qm->qps_lock); 2920 2921 if (!qm_avail_state(qm, QM_CLOSE)) { 2922 up_write(&qm->qps_lock); 2923 return; 2924 } 2925 2926 hisi_qm_memory_uninit(qm); 2927 hisi_qm_set_state(qm, QM_NOT_READY); 2928 up_write(&qm->qps_lock); 2929 2930 qm_irqs_unregister(qm); 2931 hisi_qm_pci_uninit(qm); 2932 if (qm->use_sva) { 2933 uacce_remove(qm->uacce); 2934 qm->uacce = NULL; 2935 } 2936 } 2937 EXPORT_SYMBOL_GPL(hisi_qm_uninit); 2938 2939 /** 2940 * hisi_qm_get_vft() - Get vft from a qm. 2941 * @qm: The qm we want to get its vft. 2942 * @base: The base number of queue in vft. 2943 * @number: The number of queues in vft. 2944 * 2945 * We can allocate multiple queues to a qm by configuring virtual function 2946 * table. We get related configures by this function. Normally, we call this 2947 * function in VF driver to get the queue information. 2948 * 2949 * qm hw v1 does not support this interface. 2950 */ 2951 static int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number) 2952 { 2953 if (!base || !number) 2954 return -EINVAL; 2955 2956 if (!qm->ops->get_vft) { 2957 dev_err(&qm->pdev->dev, "Don't support vft read!\n"); 2958 return -EINVAL; 2959 } 2960 2961 return qm->ops->get_vft(qm, base, number); 2962 } 2963 2964 /** 2965 * hisi_qm_set_vft() - Set vft to a qm. 2966 * @qm: The qm we want to set its vft. 2967 * @fun_num: The function number. 2968 * @base: The base number of queue in vft. 2969 * @number: The number of queues in vft. 2970 * 2971 * This function is alway called in PF driver, it is used to assign queues 2972 * among PF and VFs. 2973 * 2974 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1) 2975 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1) 2976 * (VF function number 0x2) 2977 */ 2978 static int hisi_qm_set_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 2979 u32 number) 2980 { 2981 u32 max_q_num = qm->ctrl_qp_num; 2982 2983 if (base >= max_q_num || number > max_q_num || 2984 (base + number) > max_q_num) 2985 return -EINVAL; 2986 2987 return qm_set_sqc_cqc_vft(qm, fun_num, base, number); 2988 } 2989 2990 static void qm_init_eq_aeq_status(struct hisi_qm *qm) 2991 { 2992 struct hisi_qm_status *status = &qm->status; 2993 2994 status->eq_head = 0; 2995 status->aeq_head = 0; 2996 status->eqc_phase = true; 2997 status->aeqc_phase = true; 2998 } 2999 3000 static void qm_enable_eq_aeq_interrupts(struct hisi_qm *qm) 3001 { 3002 /* Clear eq/aeq interrupt source */ 3003 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0); 3004 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 3005 3006 writel(0x0, qm->io_base + QM_VF_EQ_INT_MASK); 3007 writel(0x0, qm->io_base + QM_VF_AEQ_INT_MASK); 3008 } 3009 3010 static void qm_disable_eq_aeq_interrupts(struct hisi_qm *qm) 3011 { 3012 writel(0x1, qm->io_base + QM_VF_EQ_INT_MASK); 3013 writel(0x1, qm->io_base + QM_VF_AEQ_INT_MASK); 3014 } 3015 3016 static int qm_eq_ctx_cfg(struct hisi_qm *qm) 3017 { 3018 struct device *dev = &qm->pdev->dev; 3019 struct qm_eqc *eqc; 3020 dma_addr_t eqc_dma; 3021 int ret; 3022 3023 eqc = kzalloc(sizeof(struct qm_eqc), GFP_KERNEL); 3024 if (!eqc) 3025 return -ENOMEM; 3026 3027 eqc->base_l = cpu_to_le32(lower_32_bits(qm->eqe_dma)); 3028 eqc->base_h = cpu_to_le32(upper_32_bits(qm->eqe_dma)); 3029 if (qm->ver == QM_HW_V1) 3030 eqc->dw3 = cpu_to_le32(QM_EQE_AEQE_SIZE); 3031 eqc->dw6 = cpu_to_le32(((u32)qm->eq_depth - 1) | (1 << QM_EQC_PHASE_SHIFT)); 3032 3033 eqc_dma = dma_map_single(dev, eqc, sizeof(struct qm_eqc), 3034 DMA_TO_DEVICE); 3035 if (dma_mapping_error(dev, eqc_dma)) { 3036 kfree(eqc); 3037 return -ENOMEM; 3038 } 3039 3040 ret = hisi_qm_mb(qm, QM_MB_CMD_EQC, eqc_dma, 0, 0); 3041 dma_unmap_single(dev, eqc_dma, sizeof(struct qm_eqc), DMA_TO_DEVICE); 3042 kfree(eqc); 3043 3044 return ret; 3045 } 3046 3047 static int qm_aeq_ctx_cfg(struct hisi_qm *qm) 3048 { 3049 struct device *dev = &qm->pdev->dev; 3050 struct qm_aeqc *aeqc; 3051 dma_addr_t aeqc_dma; 3052 int ret; 3053 3054 aeqc = kzalloc(sizeof(struct qm_aeqc), GFP_KERNEL); 3055 if (!aeqc) 3056 return -ENOMEM; 3057 3058 aeqc->base_l = cpu_to_le32(lower_32_bits(qm->aeqe_dma)); 3059 aeqc->base_h = cpu_to_le32(upper_32_bits(qm->aeqe_dma)); 3060 aeqc->dw6 = cpu_to_le32(((u32)qm->aeq_depth - 1) | (1 << QM_EQC_PHASE_SHIFT)); 3061 3062 aeqc_dma = dma_map_single(dev, aeqc, sizeof(struct qm_aeqc), 3063 DMA_TO_DEVICE); 3064 if (dma_mapping_error(dev, aeqc_dma)) { 3065 kfree(aeqc); 3066 return -ENOMEM; 3067 } 3068 3069 ret = hisi_qm_mb(qm, QM_MB_CMD_AEQC, aeqc_dma, 0, 0); 3070 dma_unmap_single(dev, aeqc_dma, sizeof(struct qm_aeqc), DMA_TO_DEVICE); 3071 kfree(aeqc); 3072 3073 return ret; 3074 } 3075 3076 static int qm_eq_aeq_ctx_cfg(struct hisi_qm *qm) 3077 { 3078 struct device *dev = &qm->pdev->dev; 3079 int ret; 3080 3081 qm_init_eq_aeq_status(qm); 3082 3083 ret = qm_eq_ctx_cfg(qm); 3084 if (ret) { 3085 dev_err(dev, "Set eqc failed!\n"); 3086 return ret; 3087 } 3088 3089 return qm_aeq_ctx_cfg(qm); 3090 } 3091 3092 static int __hisi_qm_start(struct hisi_qm *qm) 3093 { 3094 int ret; 3095 3096 WARN_ON(!qm->qdma.va); 3097 3098 if (qm->fun_type == QM_HW_PF) { 3099 ret = hisi_qm_set_vft(qm, 0, qm->qp_base, qm->qp_num); 3100 if (ret) 3101 return ret; 3102 } 3103 3104 ret = qm_eq_aeq_ctx_cfg(qm); 3105 if (ret) 3106 return ret; 3107 3108 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0); 3109 if (ret) 3110 return ret; 3111 3112 ret = hisi_qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0); 3113 if (ret) 3114 return ret; 3115 3116 qm_init_prefetch(qm); 3117 qm_enable_eq_aeq_interrupts(qm); 3118 3119 return 0; 3120 } 3121 3122 /** 3123 * hisi_qm_start() - start qm 3124 * @qm: The qm to be started. 3125 * 3126 * This function starts a qm, then we can allocate qp from this qm. 3127 */ 3128 int hisi_qm_start(struct hisi_qm *qm) 3129 { 3130 struct device *dev = &qm->pdev->dev; 3131 int ret = 0; 3132 3133 down_write(&qm->qps_lock); 3134 3135 if (!qm_avail_state(qm, QM_START)) { 3136 up_write(&qm->qps_lock); 3137 return -EPERM; 3138 } 3139 3140 dev_dbg(dev, "qm start with %u queue pairs\n", qm->qp_num); 3141 3142 if (!qm->qp_num) { 3143 dev_err(dev, "qp_num should not be 0\n"); 3144 ret = -EINVAL; 3145 goto err_unlock; 3146 } 3147 3148 ret = __hisi_qm_start(qm); 3149 if (!ret) 3150 atomic_set(&qm->status.flags, QM_START); 3151 3152 hisi_qm_set_state(qm, QM_READY); 3153 err_unlock: 3154 up_write(&qm->qps_lock); 3155 return ret; 3156 } 3157 EXPORT_SYMBOL_GPL(hisi_qm_start); 3158 3159 static int qm_restart(struct hisi_qm *qm) 3160 { 3161 struct device *dev = &qm->pdev->dev; 3162 struct hisi_qp *qp; 3163 int ret, i; 3164 3165 ret = hisi_qm_start(qm); 3166 if (ret < 0) 3167 return ret; 3168 3169 down_write(&qm->qps_lock); 3170 for (i = 0; i < qm->qp_num; i++) { 3171 qp = &qm->qp_array[i]; 3172 if (atomic_read(&qp->qp_status.flags) == QP_STOP && 3173 qp->is_resetting == true) { 3174 ret = qm_start_qp_nolock(qp, 0); 3175 if (ret < 0) { 3176 dev_err(dev, "Failed to start qp%d!\n", i); 3177 3178 up_write(&qm->qps_lock); 3179 return ret; 3180 } 3181 qp->is_resetting = false; 3182 } 3183 } 3184 up_write(&qm->qps_lock); 3185 3186 return 0; 3187 } 3188 3189 /* Stop started qps in reset flow */ 3190 static int qm_stop_started_qp(struct hisi_qm *qm) 3191 { 3192 struct device *dev = &qm->pdev->dev; 3193 struct hisi_qp *qp; 3194 int i, ret; 3195 3196 for (i = 0; i < qm->qp_num; i++) { 3197 qp = &qm->qp_array[i]; 3198 if (qp && atomic_read(&qp->qp_status.flags) == QP_START) { 3199 qp->is_resetting = true; 3200 ret = qm_stop_qp_nolock(qp); 3201 if (ret < 0) { 3202 dev_err(dev, "Failed to stop qp%d!\n", i); 3203 return ret; 3204 } 3205 } 3206 } 3207 3208 return 0; 3209 } 3210 3211 /** 3212 * qm_clear_queues() - Clear all queues memory in a qm. 3213 * @qm: The qm in which the queues will be cleared. 3214 * 3215 * This function clears all queues memory in a qm. Reset of accelerator can 3216 * use this to clear queues. 3217 */ 3218 static void qm_clear_queues(struct hisi_qm *qm) 3219 { 3220 struct hisi_qp *qp; 3221 int i; 3222 3223 for (i = 0; i < qm->qp_num; i++) { 3224 qp = &qm->qp_array[i]; 3225 if (qp->is_in_kernel && qp->is_resetting) 3226 memset(qp->qdma.va, 0, qp->qdma.size); 3227 } 3228 3229 memset(qm->qdma.va, 0, qm->qdma.size); 3230 } 3231 3232 /** 3233 * hisi_qm_stop() - Stop a qm. 3234 * @qm: The qm which will be stopped. 3235 * @r: The reason to stop qm. 3236 * 3237 * This function stops qm and its qps, then qm can not accept request. 3238 * Related resources are not released at this state, we can use hisi_qm_start 3239 * to let qm start again. 3240 */ 3241 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r) 3242 { 3243 struct device *dev = &qm->pdev->dev; 3244 int ret = 0; 3245 3246 down_write(&qm->qps_lock); 3247 3248 qm->status.stop_reason = r; 3249 if (!qm_avail_state(qm, QM_STOP)) { 3250 ret = -EPERM; 3251 goto err_unlock; 3252 } 3253 3254 if (qm->status.stop_reason == QM_SOFT_RESET || 3255 qm->status.stop_reason == QM_DOWN) { 3256 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET); 3257 ret = qm_stop_started_qp(qm); 3258 if (ret < 0) { 3259 dev_err(dev, "Failed to stop started qp!\n"); 3260 goto err_unlock; 3261 } 3262 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET); 3263 } 3264 3265 qm_disable_eq_aeq_interrupts(qm); 3266 if (qm->fun_type == QM_HW_PF) { 3267 ret = hisi_qm_set_vft(qm, 0, 0, 0); 3268 if (ret < 0) { 3269 dev_err(dev, "Failed to set vft!\n"); 3270 ret = -EBUSY; 3271 goto err_unlock; 3272 } 3273 } 3274 3275 qm_clear_queues(qm); 3276 atomic_set(&qm->status.flags, QM_STOP); 3277 3278 err_unlock: 3279 up_write(&qm->qps_lock); 3280 return ret; 3281 } 3282 EXPORT_SYMBOL_GPL(hisi_qm_stop); 3283 3284 static void qm_hw_error_init(struct hisi_qm *qm) 3285 { 3286 if (!qm->ops->hw_error_init) { 3287 dev_err(&qm->pdev->dev, "QM doesn't support hw error handling!\n"); 3288 return; 3289 } 3290 3291 qm->ops->hw_error_init(qm); 3292 } 3293 3294 static void qm_hw_error_uninit(struct hisi_qm *qm) 3295 { 3296 if (!qm->ops->hw_error_uninit) { 3297 dev_err(&qm->pdev->dev, "Unexpected QM hw error uninit!\n"); 3298 return; 3299 } 3300 3301 qm->ops->hw_error_uninit(qm); 3302 } 3303 3304 static enum acc_err_result qm_hw_error_handle(struct hisi_qm *qm) 3305 { 3306 if (!qm->ops->hw_error_handle) { 3307 dev_err(&qm->pdev->dev, "QM doesn't support hw error report!\n"); 3308 return ACC_ERR_NONE; 3309 } 3310 3311 return qm->ops->hw_error_handle(qm); 3312 } 3313 3314 /** 3315 * hisi_qm_dev_err_init() - Initialize device error configuration. 3316 * @qm: The qm for which we want to do error initialization. 3317 * 3318 * Initialize QM and device error related configuration. 3319 */ 3320 void hisi_qm_dev_err_init(struct hisi_qm *qm) 3321 { 3322 if (qm->fun_type == QM_HW_VF) 3323 return; 3324 3325 qm_hw_error_init(qm); 3326 3327 if (!qm->err_ini->hw_err_enable) { 3328 dev_err(&qm->pdev->dev, "Device doesn't support hw error init!\n"); 3329 return; 3330 } 3331 qm->err_ini->hw_err_enable(qm); 3332 } 3333 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init); 3334 3335 /** 3336 * hisi_qm_dev_err_uninit() - Uninitialize device error configuration. 3337 * @qm: The qm for which we want to do error uninitialization. 3338 * 3339 * Uninitialize QM and device error related configuration. 3340 */ 3341 void hisi_qm_dev_err_uninit(struct hisi_qm *qm) 3342 { 3343 if (qm->fun_type == QM_HW_VF) 3344 return; 3345 3346 qm_hw_error_uninit(qm); 3347 3348 if (!qm->err_ini->hw_err_disable) { 3349 dev_err(&qm->pdev->dev, "Unexpected device hw error uninit!\n"); 3350 return; 3351 } 3352 qm->err_ini->hw_err_disable(qm); 3353 } 3354 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit); 3355 3356 /** 3357 * hisi_qm_free_qps() - free multiple queue pairs. 3358 * @qps: The queue pairs need to be freed. 3359 * @qp_num: The num of queue pairs. 3360 */ 3361 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num) 3362 { 3363 int i; 3364 3365 if (!qps || qp_num <= 0) 3366 return; 3367 3368 for (i = qp_num - 1; i >= 0; i--) 3369 hisi_qm_release_qp(qps[i]); 3370 } 3371 EXPORT_SYMBOL_GPL(hisi_qm_free_qps); 3372 3373 static void free_list(struct list_head *head) 3374 { 3375 struct hisi_qm_resource *res, *tmp; 3376 3377 list_for_each_entry_safe(res, tmp, head, list) { 3378 list_del(&res->list); 3379 kfree(res); 3380 } 3381 } 3382 3383 static int hisi_qm_sort_devices(int node, struct list_head *head, 3384 struct hisi_qm_list *qm_list) 3385 { 3386 struct hisi_qm_resource *res, *tmp; 3387 struct hisi_qm *qm; 3388 struct list_head *n; 3389 struct device *dev; 3390 int dev_node; 3391 3392 list_for_each_entry(qm, &qm_list->list, list) { 3393 dev = &qm->pdev->dev; 3394 3395 dev_node = dev_to_node(dev); 3396 if (dev_node < 0) 3397 dev_node = 0; 3398 3399 res = kzalloc(sizeof(*res), GFP_KERNEL); 3400 if (!res) 3401 return -ENOMEM; 3402 3403 res->qm = qm; 3404 res->distance = node_distance(dev_node, node); 3405 n = head; 3406 list_for_each_entry(tmp, head, list) { 3407 if (res->distance < tmp->distance) { 3408 n = &tmp->list; 3409 break; 3410 } 3411 } 3412 list_add_tail(&res->list, n); 3413 } 3414 3415 return 0; 3416 } 3417 3418 /** 3419 * hisi_qm_alloc_qps_node() - Create multiple queue pairs. 3420 * @qm_list: The list of all available devices. 3421 * @qp_num: The number of queue pairs need created. 3422 * @alg_type: The algorithm type. 3423 * @node: The numa node. 3424 * @qps: The queue pairs need created. 3425 * 3426 * This function will sort all available device according to numa distance. 3427 * Then try to create all queue pairs from one device, if all devices do 3428 * not meet the requirements will return error. 3429 */ 3430 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num, 3431 u8 alg_type, int node, struct hisi_qp **qps) 3432 { 3433 struct hisi_qm_resource *tmp; 3434 int ret = -ENODEV; 3435 LIST_HEAD(head); 3436 int i; 3437 3438 if (!qps || !qm_list || qp_num <= 0) 3439 return -EINVAL; 3440 3441 mutex_lock(&qm_list->lock); 3442 if (hisi_qm_sort_devices(node, &head, qm_list)) { 3443 mutex_unlock(&qm_list->lock); 3444 goto err; 3445 } 3446 3447 list_for_each_entry(tmp, &head, list) { 3448 for (i = 0; i < qp_num; i++) { 3449 qps[i] = hisi_qm_create_qp(tmp->qm, alg_type); 3450 if (IS_ERR(qps[i])) { 3451 hisi_qm_free_qps(qps, i); 3452 break; 3453 } 3454 } 3455 3456 if (i == qp_num) { 3457 ret = 0; 3458 break; 3459 } 3460 } 3461 3462 mutex_unlock(&qm_list->lock); 3463 if (ret) 3464 pr_info("Failed to create qps, node[%d], alg[%u], qp[%d]!\n", 3465 node, alg_type, qp_num); 3466 3467 err: 3468 free_list(&head); 3469 return ret; 3470 } 3471 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node); 3472 3473 static int qm_vf_q_assign(struct hisi_qm *qm, u32 num_vfs) 3474 { 3475 u32 remain_q_num, vfs_q_num, act_q_num, q_num, i, j; 3476 u32 max_qp_num = qm->max_qp_num; 3477 u32 q_base = qm->qp_num; 3478 int ret; 3479 3480 if (!num_vfs) 3481 return -EINVAL; 3482 3483 vfs_q_num = qm->ctrl_qp_num - qm->qp_num; 3484 3485 /* If vfs_q_num is less than num_vfs, return error. */ 3486 if (vfs_q_num < num_vfs) 3487 return -EINVAL; 3488 3489 q_num = vfs_q_num / num_vfs; 3490 remain_q_num = vfs_q_num % num_vfs; 3491 3492 for (i = num_vfs; i > 0; i--) { 3493 /* 3494 * if q_num + remain_q_num > max_qp_num in last vf, divide the 3495 * remaining queues equally. 3496 */ 3497 if (i == num_vfs && q_num + remain_q_num <= max_qp_num) { 3498 act_q_num = q_num + remain_q_num; 3499 remain_q_num = 0; 3500 } else if (remain_q_num > 0) { 3501 act_q_num = q_num + 1; 3502 remain_q_num--; 3503 } else { 3504 act_q_num = q_num; 3505 } 3506 3507 act_q_num = min(act_q_num, max_qp_num); 3508 ret = hisi_qm_set_vft(qm, i, q_base, act_q_num); 3509 if (ret) { 3510 for (j = num_vfs; j > i; j--) 3511 hisi_qm_set_vft(qm, j, 0, 0); 3512 return ret; 3513 } 3514 q_base += act_q_num; 3515 } 3516 3517 return 0; 3518 } 3519 3520 static int qm_clear_vft_config(struct hisi_qm *qm) 3521 { 3522 int ret; 3523 u32 i; 3524 3525 for (i = 1; i <= qm->vfs_num; i++) { 3526 ret = hisi_qm_set_vft(qm, i, 0, 0); 3527 if (ret) 3528 return ret; 3529 } 3530 qm->vfs_num = 0; 3531 3532 return 0; 3533 } 3534 3535 static int qm_func_shaper_enable(struct hisi_qm *qm, u32 fun_index, u32 qos) 3536 { 3537 struct device *dev = &qm->pdev->dev; 3538 u32 ir = qos * QM_QOS_RATE; 3539 int ret, total_vfs, i; 3540 3541 total_vfs = pci_sriov_get_totalvfs(qm->pdev); 3542 if (fun_index > total_vfs) 3543 return -EINVAL; 3544 3545 qm->factor[fun_index].func_qos = qos; 3546 3547 ret = qm_get_shaper_para(ir, &qm->factor[fun_index]); 3548 if (ret) { 3549 dev_err(dev, "failed to calculate shaper parameter!\n"); 3550 return -EINVAL; 3551 } 3552 3553 for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) { 3554 /* The base number of queue reuse for different alg type */ 3555 ret = qm_set_vft_common(qm, SHAPER_VFT, fun_index, i, 1); 3556 if (ret) { 3557 dev_err(dev, "type: %d, failed to set shaper vft!\n", i); 3558 return -EINVAL; 3559 } 3560 } 3561 3562 return 0; 3563 } 3564 3565 static u32 qm_get_shaper_vft_qos(struct hisi_qm *qm, u32 fun_index) 3566 { 3567 u64 cir_u = 0, cir_b = 0, cir_s = 0; 3568 u64 shaper_vft, ir_calc, ir; 3569 unsigned int val; 3570 u32 error_rate; 3571 int ret; 3572 3573 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 3574 val & BIT(0), POLL_PERIOD, 3575 POLL_TIMEOUT); 3576 if (ret) 3577 return 0; 3578 3579 writel(0x1, qm->io_base + QM_VFT_CFG_OP_WR); 3580 writel(SHAPER_VFT, qm->io_base + QM_VFT_CFG_TYPE); 3581 writel(fun_index, qm->io_base + QM_VFT_CFG); 3582 3583 writel(0x0, qm->io_base + QM_VFT_CFG_RDY); 3584 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE); 3585 3586 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 3587 val & BIT(0), POLL_PERIOD, 3588 POLL_TIMEOUT); 3589 if (ret) 3590 return 0; 3591 3592 shaper_vft = readl(qm->io_base + QM_VFT_CFG_DATA_L) | 3593 ((u64)readl(qm->io_base + QM_VFT_CFG_DATA_H) << 32); 3594 3595 cir_b = shaper_vft & QM_SHAPER_CIR_B_MASK; 3596 cir_u = shaper_vft & QM_SHAPER_CIR_U_MASK; 3597 cir_u = cir_u >> QM_SHAPER_FACTOR_CIR_U_SHIFT; 3598 3599 cir_s = shaper_vft & QM_SHAPER_CIR_S_MASK; 3600 cir_s = cir_s >> QM_SHAPER_FACTOR_CIR_S_SHIFT; 3601 3602 ir_calc = acc_shaper_para_calc(cir_b, cir_u, cir_s); 3603 3604 ir = qm->factor[fun_index].func_qos * QM_QOS_RATE; 3605 3606 error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir; 3607 if (error_rate > QM_QOS_MIN_ERROR_RATE) { 3608 pci_err(qm->pdev, "error_rate: %u, get function qos is error!\n", error_rate); 3609 return 0; 3610 } 3611 3612 return ir; 3613 } 3614 3615 static void qm_vf_get_qos(struct hisi_qm *qm, u32 fun_num) 3616 { 3617 struct device *dev = &qm->pdev->dev; 3618 u64 mb_cmd; 3619 u32 qos; 3620 int ret; 3621 3622 qos = qm_get_shaper_vft_qos(qm, fun_num); 3623 if (!qos) { 3624 dev_err(dev, "function(%u) failed to get qos by PF!\n", fun_num); 3625 return; 3626 } 3627 3628 mb_cmd = QM_PF_SET_QOS | (u64)qos << QM_MB_CMD_DATA_SHIFT; 3629 ret = qm_ping_single_vf(qm, mb_cmd, fun_num); 3630 if (ret) 3631 dev_err(dev, "failed to send cmd to VF(%u)!\n", fun_num); 3632 } 3633 3634 static int qm_vf_read_qos(struct hisi_qm *qm) 3635 { 3636 int cnt = 0; 3637 int ret = -EINVAL; 3638 3639 /* reset mailbox qos val */ 3640 qm->mb_qos = 0; 3641 3642 /* vf ping pf to get function qos */ 3643 ret = qm_ping_pf(qm, QM_VF_GET_QOS); 3644 if (ret) { 3645 pci_err(qm->pdev, "failed to send cmd to PF to get qos!\n"); 3646 return ret; 3647 } 3648 3649 while (true) { 3650 msleep(QM_WAIT_DST_ACK); 3651 if (qm->mb_qos) 3652 break; 3653 3654 if (++cnt > QM_MAX_VF_WAIT_COUNT) { 3655 pci_err(qm->pdev, "PF ping VF timeout!\n"); 3656 return -ETIMEDOUT; 3657 } 3658 } 3659 3660 return ret; 3661 } 3662 3663 static ssize_t qm_algqos_read(struct file *filp, char __user *buf, 3664 size_t count, loff_t *pos) 3665 { 3666 struct hisi_qm *qm = filp->private_data; 3667 char tbuf[QM_DBG_READ_LEN]; 3668 u32 qos_val, ir; 3669 int ret; 3670 3671 ret = hisi_qm_get_dfx_access(qm); 3672 if (ret) 3673 return ret; 3674 3675 /* Mailbox and reset cannot be operated at the same time */ 3676 if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) { 3677 pci_err(qm->pdev, "dev resetting, read alg qos failed!\n"); 3678 ret = -EAGAIN; 3679 goto err_put_dfx_access; 3680 } 3681 3682 if (qm->fun_type == QM_HW_PF) { 3683 ir = qm_get_shaper_vft_qos(qm, 0); 3684 } else { 3685 ret = qm_vf_read_qos(qm); 3686 if (ret) 3687 goto err_get_status; 3688 ir = qm->mb_qos; 3689 } 3690 3691 qos_val = ir / QM_QOS_RATE; 3692 ret = scnprintf(tbuf, QM_DBG_READ_LEN, "%u\n", qos_val); 3693 3694 ret = simple_read_from_buffer(buf, count, pos, tbuf, ret); 3695 3696 err_get_status: 3697 clear_bit(QM_RESETTING, &qm->misc_ctl); 3698 err_put_dfx_access: 3699 hisi_qm_put_dfx_access(qm); 3700 return ret; 3701 } 3702 3703 static ssize_t qm_get_qos_value(struct hisi_qm *qm, const char *buf, 3704 unsigned long *val, 3705 unsigned int *fun_index) 3706 { 3707 const struct bus_type *bus_type = qm->pdev->dev.bus; 3708 char tbuf_bdf[QM_DBG_READ_LEN] = {0}; 3709 char val_buf[QM_DBG_READ_LEN] = {0}; 3710 struct pci_dev *pdev; 3711 struct device *dev; 3712 int ret; 3713 3714 ret = sscanf(buf, "%s %s", tbuf_bdf, val_buf); 3715 if (ret != QM_QOS_PARAM_NUM) 3716 return -EINVAL; 3717 3718 ret = kstrtoul(val_buf, 10, val); 3719 if (ret || *val == 0 || *val > QM_QOS_MAX_VAL) { 3720 pci_err(qm->pdev, "input qos value is error, please set 1~1000!\n"); 3721 return -EINVAL; 3722 } 3723 3724 dev = bus_find_device_by_name(bus_type, NULL, tbuf_bdf); 3725 if (!dev) { 3726 pci_err(qm->pdev, "input pci bdf number is error!\n"); 3727 return -ENODEV; 3728 } 3729 3730 pdev = container_of(dev, struct pci_dev, dev); 3731 3732 *fun_index = pdev->devfn; 3733 3734 return 0; 3735 } 3736 3737 static ssize_t qm_algqos_write(struct file *filp, const char __user *buf, 3738 size_t count, loff_t *pos) 3739 { 3740 struct hisi_qm *qm = filp->private_data; 3741 char tbuf[QM_DBG_READ_LEN]; 3742 unsigned int fun_index; 3743 unsigned long val; 3744 int len, ret; 3745 3746 if (*pos != 0) 3747 return 0; 3748 3749 if (count >= QM_DBG_READ_LEN) 3750 return -ENOSPC; 3751 3752 len = simple_write_to_buffer(tbuf, QM_DBG_READ_LEN - 1, pos, buf, count); 3753 if (len < 0) 3754 return len; 3755 3756 tbuf[len] = '\0'; 3757 ret = qm_get_qos_value(qm, tbuf, &val, &fun_index); 3758 if (ret) 3759 return ret; 3760 3761 /* Mailbox and reset cannot be operated at the same time */ 3762 if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) { 3763 pci_err(qm->pdev, "dev resetting, write alg qos failed!\n"); 3764 return -EAGAIN; 3765 } 3766 3767 ret = qm_pm_get_sync(qm); 3768 if (ret) { 3769 ret = -EINVAL; 3770 goto err_get_status; 3771 } 3772 3773 ret = qm_func_shaper_enable(qm, fun_index, val); 3774 if (ret) { 3775 pci_err(qm->pdev, "failed to enable function shaper!\n"); 3776 ret = -EINVAL; 3777 goto err_put_sync; 3778 } 3779 3780 pci_info(qm->pdev, "the qos value of function%u is set to %lu.\n", 3781 fun_index, val); 3782 ret = count; 3783 3784 err_put_sync: 3785 qm_pm_put_sync(qm); 3786 err_get_status: 3787 clear_bit(QM_RESETTING, &qm->misc_ctl); 3788 return ret; 3789 } 3790 3791 static const struct file_operations qm_algqos_fops = { 3792 .owner = THIS_MODULE, 3793 .open = simple_open, 3794 .read = qm_algqos_read, 3795 .write = qm_algqos_write, 3796 }; 3797 3798 /** 3799 * hisi_qm_set_algqos_init() - Initialize function qos debugfs files. 3800 * @qm: The qm for which we want to add debugfs files. 3801 * 3802 * Create function qos debugfs files, VF ping PF to get function qos. 3803 */ 3804 void hisi_qm_set_algqos_init(struct hisi_qm *qm) 3805 { 3806 if (qm->fun_type == QM_HW_PF) 3807 debugfs_create_file("alg_qos", 0644, qm->debug.debug_root, 3808 qm, &qm_algqos_fops); 3809 else if (test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) 3810 debugfs_create_file("alg_qos", 0444, qm->debug.debug_root, 3811 qm, &qm_algqos_fops); 3812 } 3813 3814 static void hisi_qm_init_vf_qos(struct hisi_qm *qm, int total_func) 3815 { 3816 int i; 3817 3818 for (i = 1; i <= total_func; i++) 3819 qm->factor[i].func_qos = QM_QOS_MAX_VAL; 3820 } 3821 3822 /** 3823 * hisi_qm_sriov_enable() - enable virtual functions 3824 * @pdev: the PCIe device 3825 * @max_vfs: the number of virtual functions to enable 3826 * 3827 * Returns the number of enabled VFs. If there are VFs enabled already or 3828 * max_vfs is more than the total number of device can be enabled, returns 3829 * failure. 3830 */ 3831 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs) 3832 { 3833 struct hisi_qm *qm = pci_get_drvdata(pdev); 3834 int pre_existing_vfs, num_vfs, total_vfs, ret; 3835 3836 ret = qm_pm_get_sync(qm); 3837 if (ret) 3838 return ret; 3839 3840 total_vfs = pci_sriov_get_totalvfs(pdev); 3841 pre_existing_vfs = pci_num_vf(pdev); 3842 if (pre_existing_vfs) { 3843 pci_err(pdev, "%d VFs already enabled. Please disable pre-enabled VFs!\n", 3844 pre_existing_vfs); 3845 goto err_put_sync; 3846 } 3847 3848 if (max_vfs > total_vfs) { 3849 pci_err(pdev, "%d VFs is more than total VFs %d!\n", max_vfs, total_vfs); 3850 ret = -ERANGE; 3851 goto err_put_sync; 3852 } 3853 3854 num_vfs = max_vfs; 3855 3856 if (test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) 3857 hisi_qm_init_vf_qos(qm, num_vfs); 3858 3859 ret = qm_vf_q_assign(qm, num_vfs); 3860 if (ret) { 3861 pci_err(pdev, "Can't assign queues for VF!\n"); 3862 goto err_put_sync; 3863 } 3864 3865 qm->vfs_num = num_vfs; 3866 3867 ret = pci_enable_sriov(pdev, num_vfs); 3868 if (ret) { 3869 pci_err(pdev, "Can't enable VF!\n"); 3870 qm_clear_vft_config(qm); 3871 goto err_put_sync; 3872 } 3873 3874 pci_info(pdev, "VF enabled, vfs_num(=%d)!\n", num_vfs); 3875 3876 return num_vfs; 3877 3878 err_put_sync: 3879 qm_pm_put_sync(qm); 3880 return ret; 3881 } 3882 EXPORT_SYMBOL_GPL(hisi_qm_sriov_enable); 3883 3884 /** 3885 * hisi_qm_sriov_disable - disable virtual functions 3886 * @pdev: the PCI device. 3887 * @is_frozen: true when all the VFs are frozen. 3888 * 3889 * Return failure if there are VFs assigned already or VF is in used. 3890 */ 3891 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen) 3892 { 3893 struct hisi_qm *qm = pci_get_drvdata(pdev); 3894 int ret; 3895 3896 if (pci_vfs_assigned(pdev)) { 3897 pci_err(pdev, "Failed to disable VFs as VFs are assigned!\n"); 3898 return -EPERM; 3899 } 3900 3901 /* While VF is in used, SRIOV cannot be disabled. */ 3902 if (!is_frozen && qm_try_frozen_vfs(pdev, qm->qm_list)) { 3903 pci_err(pdev, "Task is using its VF!\n"); 3904 return -EBUSY; 3905 } 3906 3907 pci_disable_sriov(pdev); 3908 3909 ret = qm_clear_vft_config(qm); 3910 if (ret) 3911 return ret; 3912 3913 qm_pm_put_sync(qm); 3914 3915 return 0; 3916 } 3917 EXPORT_SYMBOL_GPL(hisi_qm_sriov_disable); 3918 3919 /** 3920 * hisi_qm_sriov_configure - configure the number of VFs 3921 * @pdev: The PCI device 3922 * @num_vfs: The number of VFs need enabled 3923 * 3924 * Enable SR-IOV according to num_vfs, 0 means disable. 3925 */ 3926 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs) 3927 { 3928 if (num_vfs == 0) 3929 return hisi_qm_sriov_disable(pdev, false); 3930 else 3931 return hisi_qm_sriov_enable(pdev, num_vfs); 3932 } 3933 EXPORT_SYMBOL_GPL(hisi_qm_sriov_configure); 3934 3935 static enum acc_err_result qm_dev_err_handle(struct hisi_qm *qm) 3936 { 3937 u32 err_sts; 3938 3939 if (!qm->err_ini->get_dev_hw_err_status) { 3940 dev_err(&qm->pdev->dev, "Device doesn't support get hw error status!\n"); 3941 return ACC_ERR_NONE; 3942 } 3943 3944 /* get device hardware error status */ 3945 err_sts = qm->err_ini->get_dev_hw_err_status(qm); 3946 if (err_sts) { 3947 if (err_sts & qm->err_info.ecc_2bits_mask) 3948 qm->err_status.is_dev_ecc_mbit = true; 3949 3950 if (qm->err_ini->log_dev_hw_err) 3951 qm->err_ini->log_dev_hw_err(qm, err_sts); 3952 3953 if (err_sts & qm->err_info.dev_reset_mask) 3954 return ACC_ERR_NEED_RESET; 3955 3956 if (qm->err_ini->clear_dev_hw_err_status) 3957 qm->err_ini->clear_dev_hw_err_status(qm, err_sts); 3958 } 3959 3960 return ACC_ERR_RECOVERED; 3961 } 3962 3963 static enum acc_err_result qm_process_dev_error(struct hisi_qm *qm) 3964 { 3965 enum acc_err_result qm_ret, dev_ret; 3966 3967 /* log qm error */ 3968 qm_ret = qm_hw_error_handle(qm); 3969 3970 /* log device error */ 3971 dev_ret = qm_dev_err_handle(qm); 3972 3973 return (qm_ret == ACC_ERR_NEED_RESET || 3974 dev_ret == ACC_ERR_NEED_RESET) ? 3975 ACC_ERR_NEED_RESET : ACC_ERR_RECOVERED; 3976 } 3977 3978 /** 3979 * hisi_qm_dev_err_detected() - Get device and qm error status then log it. 3980 * @pdev: The PCI device which need report error. 3981 * @state: The connectivity between CPU and device. 3982 * 3983 * We register this function into PCIe AER handlers, It will report device or 3984 * qm hardware error status when error occur. 3985 */ 3986 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev, 3987 pci_channel_state_t state) 3988 { 3989 struct hisi_qm *qm = pci_get_drvdata(pdev); 3990 enum acc_err_result ret; 3991 3992 if (pdev->is_virtfn) 3993 return PCI_ERS_RESULT_NONE; 3994 3995 pci_info(pdev, "PCI error detected, state(=%u)!!\n", state); 3996 if (state == pci_channel_io_perm_failure) 3997 return PCI_ERS_RESULT_DISCONNECT; 3998 3999 ret = qm_process_dev_error(qm); 4000 if (ret == ACC_ERR_NEED_RESET) 4001 return PCI_ERS_RESULT_NEED_RESET; 4002 4003 return PCI_ERS_RESULT_RECOVERED; 4004 } 4005 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected); 4006 4007 static int qm_check_req_recv(struct hisi_qm *qm) 4008 { 4009 struct pci_dev *pdev = qm->pdev; 4010 int ret; 4011 u32 val; 4012 4013 if (qm->ver >= QM_HW_V3) 4014 return 0; 4015 4016 writel(ACC_VENDOR_ID_VALUE, qm->io_base + QM_PEH_VENDOR_ID); 4017 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val, 4018 (val == ACC_VENDOR_ID_VALUE), 4019 POLL_PERIOD, POLL_TIMEOUT); 4020 if (ret) { 4021 dev_err(&pdev->dev, "Fails to read QM reg!\n"); 4022 return ret; 4023 } 4024 4025 writel(PCI_VENDOR_ID_HUAWEI, qm->io_base + QM_PEH_VENDOR_ID); 4026 ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val, 4027 (val == PCI_VENDOR_ID_HUAWEI), 4028 POLL_PERIOD, POLL_TIMEOUT); 4029 if (ret) 4030 dev_err(&pdev->dev, "Fails to read QM reg in the second time!\n"); 4031 4032 return ret; 4033 } 4034 4035 static int qm_set_pf_mse(struct hisi_qm *qm, bool set) 4036 { 4037 struct pci_dev *pdev = qm->pdev; 4038 u16 cmd; 4039 int i; 4040 4041 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 4042 if (set) 4043 cmd |= PCI_COMMAND_MEMORY; 4044 else 4045 cmd &= ~PCI_COMMAND_MEMORY; 4046 4047 pci_write_config_word(pdev, PCI_COMMAND, cmd); 4048 for (i = 0; i < MAX_WAIT_COUNTS; i++) { 4049 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 4050 if (set == ((cmd & PCI_COMMAND_MEMORY) >> 1)) 4051 return 0; 4052 4053 udelay(1); 4054 } 4055 4056 return -ETIMEDOUT; 4057 } 4058 4059 static int qm_set_vf_mse(struct hisi_qm *qm, bool set) 4060 { 4061 struct pci_dev *pdev = qm->pdev; 4062 u16 sriov_ctrl; 4063 int pos; 4064 int i; 4065 4066 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); 4067 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl); 4068 if (set) 4069 sriov_ctrl |= PCI_SRIOV_CTRL_MSE; 4070 else 4071 sriov_ctrl &= ~PCI_SRIOV_CTRL_MSE; 4072 pci_write_config_word(pdev, pos + PCI_SRIOV_CTRL, sriov_ctrl); 4073 4074 for (i = 0; i < MAX_WAIT_COUNTS; i++) { 4075 pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl); 4076 if (set == (sriov_ctrl & PCI_SRIOV_CTRL_MSE) >> 4077 ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT) 4078 return 0; 4079 4080 udelay(1); 4081 } 4082 4083 return -ETIMEDOUT; 4084 } 4085 4086 static int qm_vf_reset_prepare(struct hisi_qm *qm, 4087 enum qm_stop_reason stop_reason) 4088 { 4089 struct hisi_qm_list *qm_list = qm->qm_list; 4090 struct pci_dev *pdev = qm->pdev; 4091 struct pci_dev *virtfn; 4092 struct hisi_qm *vf_qm; 4093 int ret = 0; 4094 4095 mutex_lock(&qm_list->lock); 4096 list_for_each_entry(vf_qm, &qm_list->list, list) { 4097 virtfn = vf_qm->pdev; 4098 if (virtfn == pdev) 4099 continue; 4100 4101 if (pci_physfn(virtfn) == pdev) { 4102 /* save VFs PCIE BAR configuration */ 4103 pci_save_state(virtfn); 4104 4105 ret = hisi_qm_stop(vf_qm, stop_reason); 4106 if (ret) 4107 goto stop_fail; 4108 } 4109 } 4110 4111 stop_fail: 4112 mutex_unlock(&qm_list->lock); 4113 return ret; 4114 } 4115 4116 static int qm_try_stop_vfs(struct hisi_qm *qm, u64 cmd, 4117 enum qm_stop_reason stop_reason) 4118 { 4119 struct pci_dev *pdev = qm->pdev; 4120 int ret; 4121 4122 if (!qm->vfs_num) 4123 return 0; 4124 4125 /* Kunpeng930 supports to notify VFs to stop before PF reset */ 4126 if (test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) { 4127 ret = qm_ping_all_vfs(qm, cmd); 4128 if (ret) 4129 pci_err(pdev, "failed to send cmd to all VFs before PF reset!\n"); 4130 } else { 4131 ret = qm_vf_reset_prepare(qm, stop_reason); 4132 if (ret) 4133 pci_err(pdev, "failed to prepare reset, ret = %d.\n", ret); 4134 } 4135 4136 return ret; 4137 } 4138 4139 static int qm_controller_reset_prepare(struct hisi_qm *qm) 4140 { 4141 struct pci_dev *pdev = qm->pdev; 4142 int ret; 4143 4144 ret = qm_reset_prepare_ready(qm); 4145 if (ret) { 4146 pci_err(pdev, "Controller reset not ready!\n"); 4147 return ret; 4148 } 4149 4150 /* PF obtains the information of VF by querying the register. */ 4151 qm_cmd_uninit(qm); 4152 4153 /* Whether VFs stop successfully, soft reset will continue. */ 4154 ret = qm_try_stop_vfs(qm, QM_PF_SRST_PREPARE, QM_SOFT_RESET); 4155 if (ret) 4156 pci_err(pdev, "failed to stop vfs by pf in soft reset.\n"); 4157 4158 ret = hisi_qm_stop(qm, QM_SOFT_RESET); 4159 if (ret) { 4160 pci_err(pdev, "Fails to stop QM!\n"); 4161 qm_reset_bit_clear(qm); 4162 return ret; 4163 } 4164 4165 if (qm->use_sva) { 4166 ret = qm_hw_err_isolate(qm); 4167 if (ret) 4168 pci_err(pdev, "failed to isolate hw err!\n"); 4169 } 4170 4171 ret = qm_wait_vf_prepare_finish(qm); 4172 if (ret) 4173 pci_err(pdev, "failed to stop by vfs in soft reset!\n"); 4174 4175 clear_bit(QM_RST_SCHED, &qm->misc_ctl); 4176 4177 return 0; 4178 } 4179 4180 static void qm_dev_ecc_mbit_handle(struct hisi_qm *qm) 4181 { 4182 u32 nfe_enb = 0; 4183 4184 /* Kunpeng930 hardware automatically close master ooo when NFE occurs */ 4185 if (qm->ver >= QM_HW_V3) 4186 return; 4187 4188 if (!qm->err_status.is_dev_ecc_mbit && 4189 qm->err_status.is_qm_ecc_mbit && 4190 qm->err_ini->close_axi_master_ooo) { 4191 qm->err_ini->close_axi_master_ooo(qm); 4192 } else if (qm->err_status.is_dev_ecc_mbit && 4193 !qm->err_status.is_qm_ecc_mbit && 4194 !qm->err_ini->close_axi_master_ooo) { 4195 nfe_enb = readl(qm->io_base + QM_RAS_NFE_ENABLE); 4196 writel(nfe_enb & QM_RAS_NFE_MBIT_DISABLE, 4197 qm->io_base + QM_RAS_NFE_ENABLE); 4198 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SET); 4199 } 4200 } 4201 4202 static int qm_soft_reset(struct hisi_qm *qm) 4203 { 4204 struct pci_dev *pdev = qm->pdev; 4205 int ret; 4206 u32 val; 4207 4208 /* Ensure all doorbells and mailboxes received by QM */ 4209 ret = qm_check_req_recv(qm); 4210 if (ret) 4211 return ret; 4212 4213 if (qm->vfs_num) { 4214 ret = qm_set_vf_mse(qm, false); 4215 if (ret) { 4216 pci_err(pdev, "Fails to disable vf MSE bit.\n"); 4217 return ret; 4218 } 4219 } 4220 4221 ret = qm->ops->set_msi(qm, false); 4222 if (ret) { 4223 pci_err(pdev, "Fails to disable PEH MSI bit.\n"); 4224 return ret; 4225 } 4226 4227 qm_dev_ecc_mbit_handle(qm); 4228 4229 /* OOO register set and check */ 4230 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN, 4231 qm->io_base + ACC_MASTER_GLOBAL_CTRL); 4232 4233 /* If bus lock, reset chip */ 4234 ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN, 4235 val, 4236 (val == ACC_MASTER_TRANS_RETURN_RW), 4237 POLL_PERIOD, POLL_TIMEOUT); 4238 if (ret) { 4239 pci_emerg(pdev, "Bus lock! Please reset system.\n"); 4240 return ret; 4241 } 4242 4243 if (qm->err_ini->close_sva_prefetch) 4244 qm->err_ini->close_sva_prefetch(qm); 4245 4246 ret = qm_set_pf_mse(qm, false); 4247 if (ret) { 4248 pci_err(pdev, "Fails to disable pf MSE bit.\n"); 4249 return ret; 4250 } 4251 4252 /* The reset related sub-control registers are not in PCI BAR */ 4253 if (ACPI_HANDLE(&pdev->dev)) { 4254 unsigned long long value = 0; 4255 acpi_status s; 4256 4257 s = acpi_evaluate_integer(ACPI_HANDLE(&pdev->dev), 4258 qm->err_info.acpi_rst, 4259 NULL, &value); 4260 if (ACPI_FAILURE(s)) { 4261 pci_err(pdev, "NO controller reset method!\n"); 4262 return -EIO; 4263 } 4264 4265 if (value) { 4266 pci_err(pdev, "Reset step %llu failed!\n", value); 4267 return -EIO; 4268 } 4269 } else { 4270 pci_err(pdev, "No reset method!\n"); 4271 return -EINVAL; 4272 } 4273 4274 return 0; 4275 } 4276 4277 static int qm_vf_reset_done(struct hisi_qm *qm) 4278 { 4279 struct hisi_qm_list *qm_list = qm->qm_list; 4280 struct pci_dev *pdev = qm->pdev; 4281 struct pci_dev *virtfn; 4282 struct hisi_qm *vf_qm; 4283 int ret = 0; 4284 4285 mutex_lock(&qm_list->lock); 4286 list_for_each_entry(vf_qm, &qm_list->list, list) { 4287 virtfn = vf_qm->pdev; 4288 if (virtfn == pdev) 4289 continue; 4290 4291 if (pci_physfn(virtfn) == pdev) { 4292 /* enable VFs PCIE BAR configuration */ 4293 pci_restore_state(virtfn); 4294 4295 ret = qm_restart(vf_qm); 4296 if (ret) 4297 goto restart_fail; 4298 } 4299 } 4300 4301 restart_fail: 4302 mutex_unlock(&qm_list->lock); 4303 return ret; 4304 } 4305 4306 static int qm_try_start_vfs(struct hisi_qm *qm, enum qm_mb_cmd cmd) 4307 { 4308 struct pci_dev *pdev = qm->pdev; 4309 int ret; 4310 4311 if (!qm->vfs_num) 4312 return 0; 4313 4314 ret = qm_vf_q_assign(qm, qm->vfs_num); 4315 if (ret) { 4316 pci_err(pdev, "failed to assign VFs, ret = %d.\n", ret); 4317 return ret; 4318 } 4319 4320 /* Kunpeng930 supports to notify VFs to start after PF reset. */ 4321 if (test_bit(QM_SUPPORT_MB_COMMAND, &qm->caps)) { 4322 ret = qm_ping_all_vfs(qm, cmd); 4323 if (ret) 4324 pci_warn(pdev, "failed to send cmd to all VFs after PF reset!\n"); 4325 } else { 4326 ret = qm_vf_reset_done(qm); 4327 if (ret) 4328 pci_warn(pdev, "failed to start vfs, ret = %d.\n", ret); 4329 } 4330 4331 return ret; 4332 } 4333 4334 static int qm_dev_hw_init(struct hisi_qm *qm) 4335 { 4336 return qm->err_ini->hw_init(qm); 4337 } 4338 4339 static void qm_restart_prepare(struct hisi_qm *qm) 4340 { 4341 u32 value; 4342 4343 if (qm->err_ini->open_sva_prefetch) 4344 qm->err_ini->open_sva_prefetch(qm); 4345 4346 if (qm->ver >= QM_HW_V3) 4347 return; 4348 4349 if (!qm->err_status.is_qm_ecc_mbit && 4350 !qm->err_status.is_dev_ecc_mbit) 4351 return; 4352 4353 /* temporarily close the OOO port used for PEH to write out MSI */ 4354 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN); 4355 writel(value & ~qm->err_info.msi_wr_port, 4356 qm->io_base + ACC_AM_CFG_PORT_WR_EN); 4357 4358 /* clear dev ecc 2bit error source if having */ 4359 value = qm_get_dev_err_status(qm) & qm->err_info.ecc_2bits_mask; 4360 if (value && qm->err_ini->clear_dev_hw_err_status) 4361 qm->err_ini->clear_dev_hw_err_status(qm, value); 4362 4363 /* clear QM ecc mbit error source */ 4364 writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SOURCE); 4365 4366 /* clear AM Reorder Buffer ecc mbit source */ 4367 writel(ACC_ROB_ECC_ERR_MULTPL, qm->io_base + ACC_AM_ROB_ECC_INT_STS); 4368 } 4369 4370 static void qm_restart_done(struct hisi_qm *qm) 4371 { 4372 u32 value; 4373 4374 if (qm->ver >= QM_HW_V3) 4375 goto clear_flags; 4376 4377 if (!qm->err_status.is_qm_ecc_mbit && 4378 !qm->err_status.is_dev_ecc_mbit) 4379 return; 4380 4381 /* open the OOO port for PEH to write out MSI */ 4382 value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN); 4383 value |= qm->err_info.msi_wr_port; 4384 writel(value, qm->io_base + ACC_AM_CFG_PORT_WR_EN); 4385 4386 clear_flags: 4387 qm->err_status.is_qm_ecc_mbit = false; 4388 qm->err_status.is_dev_ecc_mbit = false; 4389 } 4390 4391 static int qm_controller_reset_done(struct hisi_qm *qm) 4392 { 4393 struct pci_dev *pdev = qm->pdev; 4394 int ret; 4395 4396 ret = qm->ops->set_msi(qm, true); 4397 if (ret) { 4398 pci_err(pdev, "Fails to enable PEH MSI bit!\n"); 4399 return ret; 4400 } 4401 4402 ret = qm_set_pf_mse(qm, true); 4403 if (ret) { 4404 pci_err(pdev, "Fails to enable pf MSE bit!\n"); 4405 return ret; 4406 } 4407 4408 if (qm->vfs_num) { 4409 ret = qm_set_vf_mse(qm, true); 4410 if (ret) { 4411 pci_err(pdev, "Fails to enable vf MSE bit!\n"); 4412 return ret; 4413 } 4414 } 4415 4416 ret = qm_dev_hw_init(qm); 4417 if (ret) { 4418 pci_err(pdev, "Failed to init device\n"); 4419 return ret; 4420 } 4421 4422 qm_restart_prepare(qm); 4423 hisi_qm_dev_err_init(qm); 4424 if (qm->err_ini->open_axi_master_ooo) 4425 qm->err_ini->open_axi_master_ooo(qm); 4426 4427 ret = qm_dev_mem_reset(qm); 4428 if (ret) { 4429 pci_err(pdev, "failed to reset device memory\n"); 4430 return ret; 4431 } 4432 4433 ret = qm_restart(qm); 4434 if (ret) { 4435 pci_err(pdev, "Failed to start QM!\n"); 4436 return ret; 4437 } 4438 4439 ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE); 4440 if (ret) 4441 pci_err(pdev, "failed to start vfs by pf in soft reset.\n"); 4442 4443 ret = qm_wait_vf_prepare_finish(qm); 4444 if (ret) 4445 pci_err(pdev, "failed to start by vfs in soft reset!\n"); 4446 4447 qm_cmd_init(qm); 4448 qm_restart_done(qm); 4449 4450 qm_reset_bit_clear(qm); 4451 4452 return 0; 4453 } 4454 4455 static int qm_controller_reset(struct hisi_qm *qm) 4456 { 4457 struct pci_dev *pdev = qm->pdev; 4458 int ret; 4459 4460 pci_info(pdev, "Controller resetting...\n"); 4461 4462 ret = qm_controller_reset_prepare(qm); 4463 if (ret) { 4464 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET); 4465 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET); 4466 clear_bit(QM_RST_SCHED, &qm->misc_ctl); 4467 return ret; 4468 } 4469 4470 hisi_qm_show_last_dfx_regs(qm); 4471 if (qm->err_ini->show_last_dfx_regs) 4472 qm->err_ini->show_last_dfx_regs(qm); 4473 4474 ret = qm_soft_reset(qm); 4475 if (ret) 4476 goto err_reset; 4477 4478 ret = qm_controller_reset_done(qm); 4479 if (ret) 4480 goto err_reset; 4481 4482 pci_info(pdev, "Controller reset complete\n"); 4483 4484 return 0; 4485 4486 err_reset: 4487 pci_err(pdev, "Controller reset failed (%d)\n", ret); 4488 qm_reset_bit_clear(qm); 4489 4490 /* if resetting fails, isolate the device */ 4491 if (qm->use_sva) 4492 qm->isolate_data.is_isolate = true; 4493 return ret; 4494 } 4495 4496 /** 4497 * hisi_qm_dev_slot_reset() - slot reset 4498 * @pdev: the PCIe device 4499 * 4500 * This function offers QM relate PCIe device reset interface. Drivers which 4501 * use QM can use this function as slot_reset in its struct pci_error_handlers. 4502 */ 4503 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev) 4504 { 4505 struct hisi_qm *qm = pci_get_drvdata(pdev); 4506 int ret; 4507 4508 if (pdev->is_virtfn) 4509 return PCI_ERS_RESULT_RECOVERED; 4510 4511 /* reset pcie device controller */ 4512 ret = qm_controller_reset(qm); 4513 if (ret) { 4514 pci_err(pdev, "Controller reset failed (%d)\n", ret); 4515 return PCI_ERS_RESULT_DISCONNECT; 4516 } 4517 4518 return PCI_ERS_RESULT_RECOVERED; 4519 } 4520 EXPORT_SYMBOL_GPL(hisi_qm_dev_slot_reset); 4521 4522 void hisi_qm_reset_prepare(struct pci_dev *pdev) 4523 { 4524 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 4525 struct hisi_qm *qm = pci_get_drvdata(pdev); 4526 u32 delay = 0; 4527 int ret; 4528 4529 hisi_qm_dev_err_uninit(pf_qm); 4530 4531 /* 4532 * Check whether there is an ECC mbit error, If it occurs, need to 4533 * wait for soft reset to fix it. 4534 */ 4535 while (qm_check_dev_error(pf_qm)) { 4536 msleep(++delay); 4537 if (delay > QM_RESET_WAIT_TIMEOUT) 4538 return; 4539 } 4540 4541 ret = qm_reset_prepare_ready(qm); 4542 if (ret) { 4543 pci_err(pdev, "FLR not ready!\n"); 4544 return; 4545 } 4546 4547 /* PF obtains the information of VF by querying the register. */ 4548 if (qm->fun_type == QM_HW_PF) 4549 qm_cmd_uninit(qm); 4550 4551 ret = qm_try_stop_vfs(qm, QM_PF_FLR_PREPARE, QM_DOWN); 4552 if (ret) 4553 pci_err(pdev, "failed to stop vfs by pf in FLR.\n"); 4554 4555 ret = hisi_qm_stop(qm, QM_DOWN); 4556 if (ret) { 4557 pci_err(pdev, "Failed to stop QM, ret = %d.\n", ret); 4558 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET); 4559 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET); 4560 return; 4561 } 4562 4563 ret = qm_wait_vf_prepare_finish(qm); 4564 if (ret) 4565 pci_err(pdev, "failed to stop by vfs in FLR!\n"); 4566 4567 pci_info(pdev, "FLR resetting...\n"); 4568 } 4569 EXPORT_SYMBOL_GPL(hisi_qm_reset_prepare); 4570 4571 static bool qm_flr_reset_complete(struct pci_dev *pdev) 4572 { 4573 struct pci_dev *pf_pdev = pci_physfn(pdev); 4574 struct hisi_qm *qm = pci_get_drvdata(pf_pdev); 4575 u32 id; 4576 4577 pci_read_config_dword(qm->pdev, PCI_COMMAND, &id); 4578 if (id == QM_PCI_COMMAND_INVALID) { 4579 pci_err(pdev, "Device can not be used!\n"); 4580 return false; 4581 } 4582 4583 return true; 4584 } 4585 4586 void hisi_qm_reset_done(struct pci_dev *pdev) 4587 { 4588 struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev)); 4589 struct hisi_qm *qm = pci_get_drvdata(pdev); 4590 int ret; 4591 4592 if (qm->fun_type == QM_HW_PF) { 4593 ret = qm_dev_hw_init(qm); 4594 if (ret) { 4595 pci_err(pdev, "Failed to init PF, ret = %d.\n", ret); 4596 goto flr_done; 4597 } 4598 } 4599 4600 hisi_qm_dev_err_init(pf_qm); 4601 4602 ret = qm_restart(qm); 4603 if (ret) { 4604 pci_err(pdev, "Failed to start QM, ret = %d.\n", ret); 4605 goto flr_done; 4606 } 4607 4608 ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE); 4609 if (ret) 4610 pci_err(pdev, "failed to start vfs by pf in FLR.\n"); 4611 4612 ret = qm_wait_vf_prepare_finish(qm); 4613 if (ret) 4614 pci_err(pdev, "failed to start by vfs in FLR!\n"); 4615 4616 flr_done: 4617 if (qm->fun_type == QM_HW_PF) 4618 qm_cmd_init(qm); 4619 4620 if (qm_flr_reset_complete(pdev)) 4621 pci_info(pdev, "FLR reset complete\n"); 4622 4623 qm_reset_bit_clear(qm); 4624 } 4625 EXPORT_SYMBOL_GPL(hisi_qm_reset_done); 4626 4627 static irqreturn_t qm_abnormal_irq(int irq, void *data) 4628 { 4629 struct hisi_qm *qm = data; 4630 enum acc_err_result ret; 4631 4632 atomic64_inc(&qm->debug.dfx.abnormal_irq_cnt); 4633 ret = qm_process_dev_error(qm); 4634 if (ret == ACC_ERR_NEED_RESET && 4635 !test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl) && 4636 !test_and_set_bit(QM_RST_SCHED, &qm->misc_ctl)) 4637 schedule_work(&qm->rst_work); 4638 4639 return IRQ_HANDLED; 4640 } 4641 4642 /** 4643 * hisi_qm_dev_shutdown() - Shutdown device. 4644 * @pdev: The device will be shutdown. 4645 * 4646 * This function will stop qm when OS shutdown or rebooting. 4647 */ 4648 void hisi_qm_dev_shutdown(struct pci_dev *pdev) 4649 { 4650 struct hisi_qm *qm = pci_get_drvdata(pdev); 4651 int ret; 4652 4653 ret = hisi_qm_stop(qm, QM_DOWN); 4654 if (ret) 4655 dev_err(&pdev->dev, "Fail to stop qm in shutdown!\n"); 4656 4657 hisi_qm_cache_wb(qm); 4658 } 4659 EXPORT_SYMBOL_GPL(hisi_qm_dev_shutdown); 4660 4661 static void hisi_qm_controller_reset(struct work_struct *rst_work) 4662 { 4663 struct hisi_qm *qm = container_of(rst_work, struct hisi_qm, rst_work); 4664 int ret; 4665 4666 ret = qm_pm_get_sync(qm); 4667 if (ret) { 4668 clear_bit(QM_RST_SCHED, &qm->misc_ctl); 4669 return; 4670 } 4671 4672 /* reset pcie device controller */ 4673 ret = qm_controller_reset(qm); 4674 if (ret) 4675 dev_err(&qm->pdev->dev, "controller reset failed (%d)\n", ret); 4676 4677 qm_pm_put_sync(qm); 4678 } 4679 4680 static void qm_pf_reset_vf_prepare(struct hisi_qm *qm, 4681 enum qm_stop_reason stop_reason) 4682 { 4683 enum qm_mb_cmd cmd = QM_VF_PREPARE_DONE; 4684 struct pci_dev *pdev = qm->pdev; 4685 int ret; 4686 4687 ret = qm_reset_prepare_ready(qm); 4688 if (ret) { 4689 dev_err(&pdev->dev, "reset prepare not ready!\n"); 4690 atomic_set(&qm->status.flags, QM_STOP); 4691 cmd = QM_VF_PREPARE_FAIL; 4692 goto err_prepare; 4693 } 4694 4695 ret = hisi_qm_stop(qm, stop_reason); 4696 if (ret) { 4697 dev_err(&pdev->dev, "failed to stop QM, ret = %d.\n", ret); 4698 atomic_set(&qm->status.flags, QM_STOP); 4699 cmd = QM_VF_PREPARE_FAIL; 4700 goto err_prepare; 4701 } else { 4702 goto out; 4703 } 4704 4705 err_prepare: 4706 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET); 4707 hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET); 4708 out: 4709 pci_save_state(pdev); 4710 ret = qm_ping_pf(qm, cmd); 4711 if (ret) 4712 dev_warn(&pdev->dev, "PF responds timeout in reset prepare!\n"); 4713 } 4714 4715 static void qm_pf_reset_vf_done(struct hisi_qm *qm) 4716 { 4717 enum qm_mb_cmd cmd = QM_VF_START_DONE; 4718 struct pci_dev *pdev = qm->pdev; 4719 int ret; 4720 4721 pci_restore_state(pdev); 4722 ret = hisi_qm_start(qm); 4723 if (ret) { 4724 dev_err(&pdev->dev, "failed to start QM, ret = %d.\n", ret); 4725 cmd = QM_VF_START_FAIL; 4726 } 4727 4728 qm_cmd_init(qm); 4729 ret = qm_ping_pf(qm, cmd); 4730 if (ret) 4731 dev_warn(&pdev->dev, "PF responds timeout in reset done!\n"); 4732 4733 qm_reset_bit_clear(qm); 4734 } 4735 4736 static int qm_wait_pf_reset_finish(struct hisi_qm *qm) 4737 { 4738 struct device *dev = &qm->pdev->dev; 4739 u32 val, cmd; 4740 u64 msg; 4741 int ret; 4742 4743 /* Wait for reset to finish */ 4744 ret = readl_relaxed_poll_timeout(qm->io_base + QM_IFC_INT_SOURCE_V, val, 4745 val == BIT(0), QM_VF_RESET_WAIT_US, 4746 QM_VF_RESET_WAIT_TIMEOUT_US); 4747 /* hardware completion status should be available by this time */ 4748 if (ret) { 4749 dev_err(dev, "couldn't get reset done status from PF, timeout!\n"); 4750 return -ETIMEDOUT; 4751 } 4752 4753 /* 4754 * Whether message is got successfully, 4755 * VF needs to ack PF by clearing the interrupt. 4756 */ 4757 ret = qm_get_mb_cmd(qm, &msg, 0); 4758 qm_clear_cmd_interrupt(qm, 0); 4759 if (ret) { 4760 dev_err(dev, "failed to get msg from PF in reset done!\n"); 4761 return ret; 4762 } 4763 4764 cmd = msg & QM_MB_CMD_DATA_MASK; 4765 if (cmd != QM_PF_RESET_DONE) { 4766 dev_err(dev, "the cmd(%u) is not reset done!\n", cmd); 4767 ret = -EINVAL; 4768 } 4769 4770 return ret; 4771 } 4772 4773 static void qm_pf_reset_vf_process(struct hisi_qm *qm, 4774 enum qm_stop_reason stop_reason) 4775 { 4776 struct device *dev = &qm->pdev->dev; 4777 int ret; 4778 4779 dev_info(dev, "device reset start...\n"); 4780 4781 /* The message is obtained by querying the register during resetting */ 4782 qm_cmd_uninit(qm); 4783 qm_pf_reset_vf_prepare(qm, stop_reason); 4784 4785 ret = qm_wait_pf_reset_finish(qm); 4786 if (ret) 4787 goto err_get_status; 4788 4789 qm_pf_reset_vf_done(qm); 4790 4791 dev_info(dev, "device reset done.\n"); 4792 4793 return; 4794 4795 err_get_status: 4796 qm_cmd_init(qm); 4797 qm_reset_bit_clear(qm); 4798 } 4799 4800 static void qm_handle_cmd_msg(struct hisi_qm *qm, u32 fun_num) 4801 { 4802 struct device *dev = &qm->pdev->dev; 4803 u64 msg; 4804 u32 cmd; 4805 int ret; 4806 4807 /* 4808 * Get the msg from source by sending mailbox. Whether message is got 4809 * successfully, destination needs to ack source by clearing the interrupt. 4810 */ 4811 ret = qm_get_mb_cmd(qm, &msg, fun_num); 4812 qm_clear_cmd_interrupt(qm, BIT(fun_num)); 4813 if (ret) { 4814 dev_err(dev, "failed to get msg from source!\n"); 4815 return; 4816 } 4817 4818 cmd = msg & QM_MB_CMD_DATA_MASK; 4819 switch (cmd) { 4820 case QM_PF_FLR_PREPARE: 4821 qm_pf_reset_vf_process(qm, QM_DOWN); 4822 break; 4823 case QM_PF_SRST_PREPARE: 4824 qm_pf_reset_vf_process(qm, QM_SOFT_RESET); 4825 break; 4826 case QM_VF_GET_QOS: 4827 qm_vf_get_qos(qm, fun_num); 4828 break; 4829 case QM_PF_SET_QOS: 4830 qm->mb_qos = msg >> QM_MB_CMD_DATA_SHIFT; 4831 break; 4832 default: 4833 dev_err(dev, "unsupported cmd %u sent by function(%u)!\n", cmd, fun_num); 4834 break; 4835 } 4836 } 4837 4838 static void qm_cmd_process(struct work_struct *cmd_process) 4839 { 4840 struct hisi_qm *qm = container_of(cmd_process, 4841 struct hisi_qm, cmd_process); 4842 u32 vfs_num = qm->vfs_num; 4843 u64 val; 4844 u32 i; 4845 4846 if (qm->fun_type == QM_HW_PF) { 4847 val = readq(qm->io_base + QM_IFC_INT_SOURCE_P); 4848 if (!val) 4849 return; 4850 4851 for (i = 1; i <= vfs_num; i++) { 4852 if (val & BIT(i)) 4853 qm_handle_cmd_msg(qm, i); 4854 } 4855 4856 return; 4857 } 4858 4859 qm_handle_cmd_msg(qm, 0); 4860 } 4861 4862 /** 4863 * hisi_qm_alg_register() - Register alg to crypto and add qm to qm_list. 4864 * @qm: The qm needs add. 4865 * @qm_list: The qm list. 4866 * 4867 * This function adds qm to qm list, and will register algorithm to 4868 * crypto when the qm list is empty. 4869 */ 4870 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 4871 { 4872 struct device *dev = &qm->pdev->dev; 4873 int flag = 0; 4874 int ret = 0; 4875 4876 mutex_lock(&qm_list->lock); 4877 if (list_empty(&qm_list->list)) 4878 flag = 1; 4879 list_add_tail(&qm->list, &qm_list->list); 4880 mutex_unlock(&qm_list->lock); 4881 4882 if (qm->ver <= QM_HW_V2 && qm->use_sva) { 4883 dev_info(dev, "HW V2 not both use uacce sva mode and hardware crypto algs.\n"); 4884 return 0; 4885 } 4886 4887 if (flag) { 4888 ret = qm_list->register_to_crypto(qm); 4889 if (ret) { 4890 mutex_lock(&qm_list->lock); 4891 list_del(&qm->list); 4892 mutex_unlock(&qm_list->lock); 4893 } 4894 } 4895 4896 return ret; 4897 } 4898 EXPORT_SYMBOL_GPL(hisi_qm_alg_register); 4899 4900 /** 4901 * hisi_qm_alg_unregister() - Unregister alg from crypto and delete qm from 4902 * qm list. 4903 * @qm: The qm needs delete. 4904 * @qm_list: The qm list. 4905 * 4906 * This function deletes qm from qm list, and will unregister algorithm 4907 * from crypto when the qm list is empty. 4908 */ 4909 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list) 4910 { 4911 mutex_lock(&qm_list->lock); 4912 list_del(&qm->list); 4913 mutex_unlock(&qm_list->lock); 4914 4915 if (qm->ver <= QM_HW_V2 && qm->use_sva) 4916 return; 4917 4918 if (list_empty(&qm_list->list)) 4919 qm_list->unregister_from_crypto(qm); 4920 } 4921 EXPORT_SYMBOL_GPL(hisi_qm_alg_unregister); 4922 4923 static void qm_unregister_abnormal_irq(struct hisi_qm *qm) 4924 { 4925 struct pci_dev *pdev = qm->pdev; 4926 u32 irq_vector, val; 4927 4928 if (qm->fun_type == QM_HW_VF) 4929 return; 4930 4931 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_ABN_IRQ_TYPE_CAP, qm->cap_ver); 4932 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_ABN_IRQ_TYPE_MASK)) 4933 return; 4934 4935 irq_vector = val & QM_IRQ_VECTOR_MASK; 4936 free_irq(pci_irq_vector(pdev, irq_vector), qm); 4937 } 4938 4939 static int qm_register_abnormal_irq(struct hisi_qm *qm) 4940 { 4941 struct pci_dev *pdev = qm->pdev; 4942 u32 irq_vector, val; 4943 int ret; 4944 4945 if (qm->fun_type == QM_HW_VF) 4946 return 0; 4947 4948 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_ABN_IRQ_TYPE_CAP, qm->cap_ver); 4949 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_ABN_IRQ_TYPE_MASK)) 4950 return 0; 4951 4952 irq_vector = val & QM_IRQ_VECTOR_MASK; 4953 ret = request_irq(pci_irq_vector(pdev, irq_vector), qm_abnormal_irq, 0, qm->dev_name, qm); 4954 if (ret) 4955 dev_err(&qm->pdev->dev, "failed to request abnormal irq, ret = %d", ret); 4956 4957 return ret; 4958 } 4959 4960 static void qm_unregister_mb_cmd_irq(struct hisi_qm *qm) 4961 { 4962 struct pci_dev *pdev = qm->pdev; 4963 u32 irq_vector, val; 4964 4965 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_PF2VF_IRQ_TYPE_CAP, qm->cap_ver); 4966 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 4967 return; 4968 4969 irq_vector = val & QM_IRQ_VECTOR_MASK; 4970 free_irq(pci_irq_vector(pdev, irq_vector), qm); 4971 } 4972 4973 static int qm_register_mb_cmd_irq(struct hisi_qm *qm) 4974 { 4975 struct pci_dev *pdev = qm->pdev; 4976 u32 irq_vector, val; 4977 int ret; 4978 4979 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_PF2VF_IRQ_TYPE_CAP, qm->cap_ver); 4980 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 4981 return 0; 4982 4983 irq_vector = val & QM_IRQ_VECTOR_MASK; 4984 ret = request_irq(pci_irq_vector(pdev, irq_vector), qm_mb_cmd_irq, 0, qm->dev_name, qm); 4985 if (ret) 4986 dev_err(&pdev->dev, "failed to request function communication irq, ret = %d", ret); 4987 4988 return ret; 4989 } 4990 4991 static void qm_unregister_aeq_irq(struct hisi_qm *qm) 4992 { 4993 struct pci_dev *pdev = qm->pdev; 4994 u32 irq_vector, val; 4995 4996 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_AEQ_IRQ_TYPE_CAP, qm->cap_ver); 4997 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 4998 return; 4999 5000 irq_vector = val & QM_IRQ_VECTOR_MASK; 5001 free_irq(pci_irq_vector(pdev, irq_vector), qm); 5002 } 5003 5004 static int qm_register_aeq_irq(struct hisi_qm *qm) 5005 { 5006 struct pci_dev *pdev = qm->pdev; 5007 u32 irq_vector, val; 5008 int ret; 5009 5010 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_AEQ_IRQ_TYPE_CAP, qm->cap_ver); 5011 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 5012 return 0; 5013 5014 irq_vector = val & QM_IRQ_VECTOR_MASK; 5015 ret = request_threaded_irq(pci_irq_vector(pdev, irq_vector), qm_aeq_irq, 5016 qm_aeq_thread, 0, qm->dev_name, qm); 5017 if (ret) 5018 dev_err(&pdev->dev, "failed to request eq irq, ret = %d", ret); 5019 5020 return ret; 5021 } 5022 5023 static void qm_unregister_eq_irq(struct hisi_qm *qm) 5024 { 5025 struct pci_dev *pdev = qm->pdev; 5026 u32 irq_vector, val; 5027 5028 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_EQ_IRQ_TYPE_CAP, qm->cap_ver); 5029 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 5030 return; 5031 5032 irq_vector = val & QM_IRQ_VECTOR_MASK; 5033 free_irq(pci_irq_vector(pdev, irq_vector), qm); 5034 } 5035 5036 static int qm_register_eq_irq(struct hisi_qm *qm) 5037 { 5038 struct pci_dev *pdev = qm->pdev; 5039 u32 irq_vector, val; 5040 int ret; 5041 5042 val = hisi_qm_get_hw_info(qm, qm_basic_info, QM_EQ_IRQ_TYPE_CAP, qm->cap_ver); 5043 if (!((val >> QM_IRQ_TYPE_SHIFT) & QM_IRQ_TYPE_MASK)) 5044 return 0; 5045 5046 irq_vector = val & QM_IRQ_VECTOR_MASK; 5047 ret = request_irq(pci_irq_vector(pdev, irq_vector), qm_eq_irq, 0, qm->dev_name, qm); 5048 if (ret) 5049 dev_err(&pdev->dev, "failed to request eq irq, ret = %d", ret); 5050 5051 return ret; 5052 } 5053 5054 static void qm_irqs_unregister(struct hisi_qm *qm) 5055 { 5056 qm_unregister_mb_cmd_irq(qm); 5057 qm_unregister_abnormal_irq(qm); 5058 qm_unregister_aeq_irq(qm); 5059 qm_unregister_eq_irq(qm); 5060 } 5061 5062 static int qm_irqs_register(struct hisi_qm *qm) 5063 { 5064 int ret; 5065 5066 ret = qm_register_eq_irq(qm); 5067 if (ret) 5068 return ret; 5069 5070 ret = qm_register_aeq_irq(qm); 5071 if (ret) 5072 goto free_eq_irq; 5073 5074 ret = qm_register_abnormal_irq(qm); 5075 if (ret) 5076 goto free_aeq_irq; 5077 5078 ret = qm_register_mb_cmd_irq(qm); 5079 if (ret) 5080 goto free_abnormal_irq; 5081 5082 return 0; 5083 5084 free_abnormal_irq: 5085 qm_unregister_abnormal_irq(qm); 5086 free_aeq_irq: 5087 qm_unregister_aeq_irq(qm); 5088 free_eq_irq: 5089 qm_unregister_eq_irq(qm); 5090 return ret; 5091 } 5092 5093 static int qm_get_qp_num(struct hisi_qm *qm) 5094 { 5095 struct device *dev = &qm->pdev->dev; 5096 bool is_db_isolation; 5097 5098 /* VF's qp_num assigned by PF in v2, and VF can get qp_num by vft. */ 5099 if (qm->fun_type == QM_HW_VF) { 5100 if (qm->ver != QM_HW_V1) 5101 /* v2 starts to support get vft by mailbox */ 5102 return hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num); 5103 5104 return 0; 5105 } 5106 5107 is_db_isolation = test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps); 5108 qm->ctrl_qp_num = hisi_qm_get_hw_info(qm, qm_basic_info, QM_TOTAL_QP_NUM_CAP, true); 5109 qm->max_qp_num = hisi_qm_get_hw_info(qm, qm_basic_info, 5110 QM_FUNC_MAX_QP_CAP, is_db_isolation); 5111 5112 if (qm->qp_num <= qm->max_qp_num) 5113 return 0; 5114 5115 if (test_bit(QM_MODULE_PARAM, &qm->misc_ctl)) { 5116 /* Check whether the set qp number is valid */ 5117 dev_err(dev, "qp num(%u) is more than max qp num(%u)!\n", 5118 qm->qp_num, qm->max_qp_num); 5119 return -EINVAL; 5120 } 5121 5122 dev_info(dev, "Default qp num(%u) is too big, reset it to Function's max qp num(%u)!\n", 5123 qm->qp_num, qm->max_qp_num); 5124 qm->qp_num = qm->max_qp_num; 5125 qm->debug.curr_qm_qp_num = qm->qp_num; 5126 5127 return 0; 5128 } 5129 5130 static void qm_get_hw_caps(struct hisi_qm *qm) 5131 { 5132 const struct hisi_qm_cap_info *cap_info = qm->fun_type == QM_HW_PF ? 5133 qm_cap_info_pf : qm_cap_info_vf; 5134 u32 size = qm->fun_type == QM_HW_PF ? ARRAY_SIZE(qm_cap_info_pf) : 5135 ARRAY_SIZE(qm_cap_info_vf); 5136 u32 val, i; 5137 5138 /* Doorbell isolate register is a independent register. */ 5139 val = hisi_qm_get_hw_info(qm, qm_cap_info_comm, QM_SUPPORT_DB_ISOLATION, true); 5140 if (val) 5141 set_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps); 5142 5143 if (qm->ver >= QM_HW_V3) { 5144 val = readl(qm->io_base + QM_FUNC_CAPS_REG); 5145 qm->cap_ver = val & QM_CAPBILITY_VERSION; 5146 } 5147 5148 /* Get PF/VF common capbility */ 5149 for (i = 1; i < ARRAY_SIZE(qm_cap_info_comm); i++) { 5150 val = hisi_qm_get_hw_info(qm, qm_cap_info_comm, i, qm->cap_ver); 5151 if (val) 5152 set_bit(qm_cap_info_comm[i].type, &qm->caps); 5153 } 5154 5155 /* Get PF/VF different capbility */ 5156 for (i = 0; i < size; i++) { 5157 val = hisi_qm_get_hw_info(qm, cap_info, i, qm->cap_ver); 5158 if (val) 5159 set_bit(cap_info[i].type, &qm->caps); 5160 } 5161 } 5162 5163 static int qm_get_pci_res(struct hisi_qm *qm) 5164 { 5165 struct pci_dev *pdev = qm->pdev; 5166 struct device *dev = &pdev->dev; 5167 int ret; 5168 5169 ret = pci_request_mem_regions(pdev, qm->dev_name); 5170 if (ret < 0) { 5171 dev_err(dev, "Failed to request mem regions!\n"); 5172 return ret; 5173 } 5174 5175 qm->phys_base = pci_resource_start(pdev, PCI_BAR_2); 5176 qm->io_base = ioremap(qm->phys_base, pci_resource_len(pdev, PCI_BAR_2)); 5177 if (!qm->io_base) { 5178 ret = -EIO; 5179 goto err_request_mem_regions; 5180 } 5181 5182 qm_get_hw_caps(qm); 5183 if (test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps)) { 5184 qm->db_interval = QM_QP_DB_INTERVAL; 5185 qm->db_phys_base = pci_resource_start(pdev, PCI_BAR_4); 5186 qm->db_io_base = ioremap(qm->db_phys_base, 5187 pci_resource_len(pdev, PCI_BAR_4)); 5188 if (!qm->db_io_base) { 5189 ret = -EIO; 5190 goto err_ioremap; 5191 } 5192 } else { 5193 qm->db_phys_base = qm->phys_base; 5194 qm->db_io_base = qm->io_base; 5195 qm->db_interval = 0; 5196 } 5197 5198 ret = qm_get_qp_num(qm); 5199 if (ret) 5200 goto err_db_ioremap; 5201 5202 return 0; 5203 5204 err_db_ioremap: 5205 if (test_bit(QM_SUPPORT_DB_ISOLATION, &qm->caps)) 5206 iounmap(qm->db_io_base); 5207 err_ioremap: 5208 iounmap(qm->io_base); 5209 err_request_mem_regions: 5210 pci_release_mem_regions(pdev); 5211 return ret; 5212 } 5213 5214 static int hisi_qm_pci_init(struct hisi_qm *qm) 5215 { 5216 struct pci_dev *pdev = qm->pdev; 5217 struct device *dev = &pdev->dev; 5218 unsigned int num_vec; 5219 int ret; 5220 5221 ret = pci_enable_device_mem(pdev); 5222 if (ret < 0) { 5223 dev_err(dev, "Failed to enable device mem!\n"); 5224 return ret; 5225 } 5226 5227 ret = qm_get_pci_res(qm); 5228 if (ret) 5229 goto err_disable_pcidev; 5230 5231 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 5232 if (ret < 0) 5233 goto err_get_pci_res; 5234 pci_set_master(pdev); 5235 5236 num_vec = qm_get_irq_num(qm); 5237 ret = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSI); 5238 if (ret < 0) { 5239 dev_err(dev, "Failed to enable MSI vectors!\n"); 5240 goto err_get_pci_res; 5241 } 5242 5243 return 0; 5244 5245 err_get_pci_res: 5246 qm_put_pci_res(qm); 5247 err_disable_pcidev: 5248 pci_disable_device(pdev); 5249 return ret; 5250 } 5251 5252 static int hisi_qm_init_work(struct hisi_qm *qm) 5253 { 5254 int i; 5255 5256 for (i = 0; i < qm->qp_num; i++) 5257 INIT_WORK(&qm->poll_data[i].work, qm_work_process); 5258 5259 if (qm->fun_type == QM_HW_PF) 5260 INIT_WORK(&qm->rst_work, hisi_qm_controller_reset); 5261 5262 if (qm->ver > QM_HW_V2) 5263 INIT_WORK(&qm->cmd_process, qm_cmd_process); 5264 5265 qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM | 5266 WQ_UNBOUND, num_online_cpus(), 5267 pci_name(qm->pdev)); 5268 if (!qm->wq) { 5269 pci_err(qm->pdev, "failed to alloc workqueue!\n"); 5270 return -ENOMEM; 5271 } 5272 5273 return 0; 5274 } 5275 5276 static int hisi_qp_alloc_memory(struct hisi_qm *qm) 5277 { 5278 struct device *dev = &qm->pdev->dev; 5279 u16 sq_depth, cq_depth; 5280 size_t qp_dma_size; 5281 int i, ret; 5282 5283 qm->qp_array = kcalloc(qm->qp_num, sizeof(struct hisi_qp), GFP_KERNEL); 5284 if (!qm->qp_array) 5285 return -ENOMEM; 5286 5287 qm->poll_data = kcalloc(qm->qp_num, sizeof(struct hisi_qm_poll_data), GFP_KERNEL); 5288 if (!qm->poll_data) { 5289 kfree(qm->qp_array); 5290 return -ENOMEM; 5291 } 5292 5293 qm_get_xqc_depth(qm, &sq_depth, &cq_depth, QM_QP_DEPTH_CAP); 5294 5295 /* one more page for device or qp statuses */ 5296 qp_dma_size = qm->sqe_size * sq_depth + sizeof(struct qm_cqe) * cq_depth; 5297 qp_dma_size = PAGE_ALIGN(qp_dma_size) + PAGE_SIZE; 5298 for (i = 0; i < qm->qp_num; i++) { 5299 qm->poll_data[i].qm = qm; 5300 ret = hisi_qp_memory_init(qm, qp_dma_size, i, sq_depth, cq_depth); 5301 if (ret) 5302 goto err_init_qp_mem; 5303 5304 dev_dbg(dev, "allocate qp dma buf size=%zx)\n", qp_dma_size); 5305 } 5306 5307 return 0; 5308 err_init_qp_mem: 5309 hisi_qp_memory_uninit(qm, i); 5310 5311 return ret; 5312 } 5313 5314 static int hisi_qm_memory_init(struct hisi_qm *qm) 5315 { 5316 struct device *dev = &qm->pdev->dev; 5317 int ret, total_func; 5318 size_t off = 0; 5319 5320 if (test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) { 5321 total_func = pci_sriov_get_totalvfs(qm->pdev) + 1; 5322 qm->factor = kcalloc(total_func, sizeof(struct qm_shaper_factor), GFP_KERNEL); 5323 if (!qm->factor) 5324 return -ENOMEM; 5325 5326 /* Only the PF value needs to be initialized */ 5327 qm->factor[0].func_qos = QM_QOS_MAX_VAL; 5328 } 5329 5330 #define QM_INIT_BUF(qm, type, num) do { \ 5331 (qm)->type = ((qm)->qdma.va + (off)); \ 5332 (qm)->type##_dma = (qm)->qdma.dma + (off); \ 5333 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \ 5334 } while (0) 5335 5336 idr_init(&qm->qp_idr); 5337 qm_get_xqc_depth(qm, &qm->eq_depth, &qm->aeq_depth, QM_XEQ_DEPTH_CAP); 5338 qm->qdma.size = QMC_ALIGN(sizeof(struct qm_eqe) * qm->eq_depth) + 5339 QMC_ALIGN(sizeof(struct qm_aeqe) * qm->aeq_depth) + 5340 QMC_ALIGN(sizeof(struct qm_sqc) * qm->qp_num) + 5341 QMC_ALIGN(sizeof(struct qm_cqc) * qm->qp_num); 5342 qm->qdma.va = dma_alloc_coherent(dev, qm->qdma.size, &qm->qdma.dma, 5343 GFP_ATOMIC); 5344 dev_dbg(dev, "allocate qm dma buf size=%zx)\n", qm->qdma.size); 5345 if (!qm->qdma.va) { 5346 ret = -ENOMEM; 5347 goto err_destroy_idr; 5348 } 5349 5350 QM_INIT_BUF(qm, eqe, qm->eq_depth); 5351 QM_INIT_BUF(qm, aeqe, qm->aeq_depth); 5352 QM_INIT_BUF(qm, sqc, qm->qp_num); 5353 QM_INIT_BUF(qm, cqc, qm->qp_num); 5354 5355 ret = hisi_qp_alloc_memory(qm); 5356 if (ret) 5357 goto err_alloc_qp_array; 5358 5359 return 0; 5360 5361 err_alloc_qp_array: 5362 dma_free_coherent(dev, qm->qdma.size, qm->qdma.va, qm->qdma.dma); 5363 err_destroy_idr: 5364 idr_destroy(&qm->qp_idr); 5365 if (test_bit(QM_SUPPORT_FUNC_QOS, &qm->caps)) 5366 kfree(qm->factor); 5367 5368 return ret; 5369 } 5370 5371 /** 5372 * hisi_qm_init() - Initialize configures about qm. 5373 * @qm: The qm needing init. 5374 * 5375 * This function init qm, then we can call hisi_qm_start to put qm into work. 5376 */ 5377 int hisi_qm_init(struct hisi_qm *qm) 5378 { 5379 struct pci_dev *pdev = qm->pdev; 5380 struct device *dev = &pdev->dev; 5381 int ret; 5382 5383 hisi_qm_pre_init(qm); 5384 5385 ret = hisi_qm_pci_init(qm); 5386 if (ret) 5387 return ret; 5388 5389 ret = qm_irqs_register(qm); 5390 if (ret) 5391 goto err_pci_init; 5392 5393 if (qm->fun_type == QM_HW_PF) { 5394 /* Set the doorbell timeout to QM_DB_TIMEOUT_CFG ns. */ 5395 writel(QM_DB_TIMEOUT_SET, qm->io_base + QM_DB_TIMEOUT_CFG); 5396 qm_disable_clock_gate(qm); 5397 ret = qm_dev_mem_reset(qm); 5398 if (ret) { 5399 dev_err(dev, "failed to reset device memory\n"); 5400 goto err_irq_register; 5401 } 5402 } 5403 5404 if (qm->mode == UACCE_MODE_SVA) { 5405 ret = qm_alloc_uacce(qm); 5406 if (ret < 0) 5407 dev_warn(dev, "fail to alloc uacce (%d)\n", ret); 5408 } 5409 5410 ret = hisi_qm_memory_init(qm); 5411 if (ret) 5412 goto err_alloc_uacce; 5413 5414 ret = hisi_qm_init_work(qm); 5415 if (ret) 5416 goto err_free_qm_memory; 5417 5418 qm_cmd_init(qm); 5419 atomic_set(&qm->status.flags, QM_INIT); 5420 5421 return 0; 5422 5423 err_free_qm_memory: 5424 hisi_qm_memory_uninit(qm); 5425 err_alloc_uacce: 5426 qm_remove_uacce(qm); 5427 err_irq_register: 5428 qm_irqs_unregister(qm); 5429 err_pci_init: 5430 hisi_qm_pci_uninit(qm); 5431 return ret; 5432 } 5433 EXPORT_SYMBOL_GPL(hisi_qm_init); 5434 5435 /** 5436 * hisi_qm_get_dfx_access() - Try to get dfx access. 5437 * @qm: pointer to accelerator device. 5438 * 5439 * Try to get dfx access, then user can get message. 5440 * 5441 * If device is in suspended, return failure, otherwise 5442 * bump up the runtime PM usage counter. 5443 */ 5444 int hisi_qm_get_dfx_access(struct hisi_qm *qm) 5445 { 5446 struct device *dev = &qm->pdev->dev; 5447 5448 if (pm_runtime_suspended(dev)) { 5449 dev_info(dev, "can not read/write - device in suspended.\n"); 5450 return -EAGAIN; 5451 } 5452 5453 return qm_pm_get_sync(qm); 5454 } 5455 EXPORT_SYMBOL_GPL(hisi_qm_get_dfx_access); 5456 5457 /** 5458 * hisi_qm_put_dfx_access() - Put dfx access. 5459 * @qm: pointer to accelerator device. 5460 * 5461 * Put dfx access, drop runtime PM usage counter. 5462 */ 5463 void hisi_qm_put_dfx_access(struct hisi_qm *qm) 5464 { 5465 qm_pm_put_sync(qm); 5466 } 5467 EXPORT_SYMBOL_GPL(hisi_qm_put_dfx_access); 5468 5469 /** 5470 * hisi_qm_pm_init() - Initialize qm runtime PM. 5471 * @qm: pointer to accelerator device. 5472 * 5473 * Function that initialize qm runtime PM. 5474 */ 5475 void hisi_qm_pm_init(struct hisi_qm *qm) 5476 { 5477 struct device *dev = &qm->pdev->dev; 5478 5479 if (!test_bit(QM_SUPPORT_RPM, &qm->caps)) 5480 return; 5481 5482 pm_runtime_set_autosuspend_delay(dev, QM_AUTOSUSPEND_DELAY); 5483 pm_runtime_use_autosuspend(dev); 5484 pm_runtime_put_noidle(dev); 5485 } 5486 EXPORT_SYMBOL_GPL(hisi_qm_pm_init); 5487 5488 /** 5489 * hisi_qm_pm_uninit() - Uninitialize qm runtime PM. 5490 * @qm: pointer to accelerator device. 5491 * 5492 * Function that uninitialize qm runtime PM. 5493 */ 5494 void hisi_qm_pm_uninit(struct hisi_qm *qm) 5495 { 5496 struct device *dev = &qm->pdev->dev; 5497 5498 if (!test_bit(QM_SUPPORT_RPM, &qm->caps)) 5499 return; 5500 5501 pm_runtime_get_noresume(dev); 5502 pm_runtime_dont_use_autosuspend(dev); 5503 } 5504 EXPORT_SYMBOL_GPL(hisi_qm_pm_uninit); 5505 5506 static int qm_prepare_for_suspend(struct hisi_qm *qm) 5507 { 5508 struct pci_dev *pdev = qm->pdev; 5509 int ret; 5510 u32 val; 5511 5512 ret = qm->ops->set_msi(qm, false); 5513 if (ret) { 5514 pci_err(pdev, "failed to disable MSI before suspending!\n"); 5515 return ret; 5516 } 5517 5518 /* shutdown OOO register */ 5519 writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN, 5520 qm->io_base + ACC_MASTER_GLOBAL_CTRL); 5521 5522 ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN, 5523 val, 5524 (val == ACC_MASTER_TRANS_RETURN_RW), 5525 POLL_PERIOD, POLL_TIMEOUT); 5526 if (ret) { 5527 pci_emerg(pdev, "Bus lock! Please reset system.\n"); 5528 return ret; 5529 } 5530 5531 ret = qm_set_pf_mse(qm, false); 5532 if (ret) 5533 pci_err(pdev, "failed to disable MSE before suspending!\n"); 5534 5535 return ret; 5536 } 5537 5538 static int qm_rebuild_for_resume(struct hisi_qm *qm) 5539 { 5540 struct pci_dev *pdev = qm->pdev; 5541 int ret; 5542 5543 ret = qm_set_pf_mse(qm, true); 5544 if (ret) { 5545 pci_err(pdev, "failed to enable MSE after resuming!\n"); 5546 return ret; 5547 } 5548 5549 ret = qm->ops->set_msi(qm, true); 5550 if (ret) { 5551 pci_err(pdev, "failed to enable MSI after resuming!\n"); 5552 return ret; 5553 } 5554 5555 ret = qm_dev_hw_init(qm); 5556 if (ret) { 5557 pci_err(pdev, "failed to init device after resuming\n"); 5558 return ret; 5559 } 5560 5561 qm_cmd_init(qm); 5562 hisi_qm_dev_err_init(qm); 5563 /* Set the doorbell timeout to QM_DB_TIMEOUT_CFG ns. */ 5564 writel(QM_DB_TIMEOUT_SET, qm->io_base + QM_DB_TIMEOUT_CFG); 5565 qm_disable_clock_gate(qm); 5566 ret = qm_dev_mem_reset(qm); 5567 if (ret) 5568 pci_err(pdev, "failed to reset device memory\n"); 5569 5570 return ret; 5571 } 5572 5573 /** 5574 * hisi_qm_suspend() - Runtime suspend of given device. 5575 * @dev: device to suspend. 5576 * 5577 * Function that suspend the device. 5578 */ 5579 int hisi_qm_suspend(struct device *dev) 5580 { 5581 struct pci_dev *pdev = to_pci_dev(dev); 5582 struct hisi_qm *qm = pci_get_drvdata(pdev); 5583 int ret; 5584 5585 pci_info(pdev, "entering suspended state\n"); 5586 5587 ret = hisi_qm_stop(qm, QM_NORMAL); 5588 if (ret) { 5589 pci_err(pdev, "failed to stop qm(%d)\n", ret); 5590 return ret; 5591 } 5592 5593 ret = qm_prepare_for_suspend(qm); 5594 if (ret) 5595 pci_err(pdev, "failed to prepare suspended(%d)\n", ret); 5596 5597 return ret; 5598 } 5599 EXPORT_SYMBOL_GPL(hisi_qm_suspend); 5600 5601 /** 5602 * hisi_qm_resume() - Runtime resume of given device. 5603 * @dev: device to resume. 5604 * 5605 * Function that resume the device. 5606 */ 5607 int hisi_qm_resume(struct device *dev) 5608 { 5609 struct pci_dev *pdev = to_pci_dev(dev); 5610 struct hisi_qm *qm = pci_get_drvdata(pdev); 5611 int ret; 5612 5613 pci_info(pdev, "resuming from suspend state\n"); 5614 5615 ret = qm_rebuild_for_resume(qm); 5616 if (ret) { 5617 pci_err(pdev, "failed to rebuild resume(%d)\n", ret); 5618 return ret; 5619 } 5620 5621 ret = hisi_qm_start(qm); 5622 if (ret) { 5623 if (qm_check_dev_error(qm)) { 5624 pci_info(pdev, "failed to start qm due to device error, device will be reset!\n"); 5625 return 0; 5626 } 5627 5628 pci_err(pdev, "failed to start qm(%d)!\n", ret); 5629 } 5630 5631 return ret; 5632 } 5633 EXPORT_SYMBOL_GPL(hisi_qm_resume); 5634 5635 MODULE_LICENSE("GPL v2"); 5636 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 5637 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver"); 5638