- ret = -EBUSY;
- for (i = 0; i < timeout_ms; i++) {
- if (mlx5_devx_regex_register_read(ctx, id, address, value))
- return -1;
- if ((*value & expected_mask) == expected_value) {
- ret = 0;
- break;
- }
- rte_delay_us(1000);
- }
- return ret;
-}
-
-static int
-rxp_start_engine(struct ibv_context *ctx, uint8_t id)
-{
- uint32_t ctrl;
- int ret;
-
- ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
- if (ret)
- return ret;
- ctrl |= MLX5_RXP_CSR_CTRL_GO;
- ctrl |= MLX5_RXP_CSR_CTRL_DISABLE_L2C;
- ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
- return ret;
-}
-
-static int
-rxp_stop_engine(struct ibv_context *ctx, uint8_t id)
-{
- uint32_t ctrl;
- int ret;
-
- ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
- if (ret)
- return ret;
- ctrl &= ~MLX5_RXP_CSR_CTRL_GO;
- ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
- return ret;
-}
-
-static int
-rxp_init_rtru(struct mlx5_regex_priv *priv, uint8_t id, uint32_t init_bits)
-{
- uint32_t ctrl_value;
- uint32_t poll_value;
- uint32_t expected_value;
- uint32_t expected_mask;
- struct ibv_context *ctx = priv->cdev->ctx;
- int ret = 0;
-
- /* Read the rtru ctrl CSR. */
- ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- &ctrl_value);
- if (ret)
- return -1;
- /* Clear any previous init modes. */
- ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_MASK);
- if (ctrl_value & MLX5_RXP_RTRU_CSR_CTRL_INIT) {
- ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
- mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- ctrl_value);
- }
- /* Set the init_mode bits in the rtru ctrl CSR. */
- ctrl_value |= init_bits;
- mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- ctrl_value);
- /* Need to sleep for a short period after pulsing the rtru init bit. */
- rte_delay_us(20000);
- /* Poll the rtru status CSR until all the init done bits are set. */
- DRV_LOG(DEBUG, "waiting for RXP rule memory to complete init");
- /* Set the init bit in the rtru ctrl CSR. */
- ctrl_value |= MLX5_RXP_RTRU_CSR_CTRL_INIT;
- mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- ctrl_value);
- /* Clear the init bit in the rtru ctrl CSR */
- ctrl_value &= ~MLX5_RXP_RTRU_CSR_CTRL_INIT;
- mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- ctrl_value);
- /* Check that the following bits are set in the RTRU_CSR. */
- if (init_bits == MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_L1_L2) {
- /* Must be incremental mode */
- expected_value = MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE;
- } else {
- expected_value = MLX5_RXP_RTRU_CSR_STATUS_IM_INIT_DONE |
- MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE;
- }
- if (priv->is_bf2)
- expected_value |= MLX5_RXP_RTRU_CSR_STATUS_L2C_INIT_DONE;
-
-
- expected_mask = expected_value;
- ret = rxp_poll_csr_for_value(ctx, &poll_value,
- MLX5_RXP_RTRU_CSR_STATUS,
- expected_value, expected_mask,
- MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT, id);
- if (ret)
- return ret;
- DRV_LOG(DEBUG, "rule memory initialise: 0x%08X", poll_value);
- /* Clear the init bit in the rtru ctrl CSR */
- ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
- mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
- ctrl_value);
- return 0;
-}
-
-static int
-rxp_parse_line(char *line, uint32_t *type, uint32_t *address, uint64_t *value)
-{
- char *cur_pos;
-
- if (*line == '\0' || *line == '#')
- return 1;
- *type = strtoul(line, &cur_pos, 10);
- if (*cur_pos != ',' && *cur_pos != '\0')
- return -1;
- *address = strtoul(cur_pos+1, &cur_pos, 16);
- if (*cur_pos != ',' && *cur_pos != '\0')
- return -1;
- *value = strtoul(cur_pos+1, &cur_pos, 16);
- if (*cur_pos != ',' && *cur_pos != '\0')
- return -1;
- return 0;
-}
-
-static uint32_t
-rxp_get_reg_address(uint32_t address)
-{
- uint32_t block;
- uint32_t reg;
-
- block = (address >> 16) & 0xFFFF;
- if (block == 0)
- reg = MLX5_RXP_CSR_BASE_ADDRESS;
- else if (block == 1)
- reg = MLX5_RXP_RTRU_CSR_BASE_ADDRESS;
- else {
- DRV_LOG(ERR, "Invalid ROF register 0x%08X!", address);
- return UINT32_MAX;
- }
- reg += (address & 0xFFFF) * MLX5_RXP_CSR_WIDTH;
- return reg;
-}
-
-#define MLX5_RXP_NUM_LINES_PER_BLOCK 8
-
-static int
-rxp_program_rof(struct mlx5_regex_priv *priv, const char *buf, uint32_t len,
- uint8_t id)
-{
- static const char del[] = "\n\r";
- char *line;
- char *tmp;
- uint32_t type = 0;
- uint32_t address;
- uint64_t val;
- uint32_t reg_val;
- int ret;
- int skip = -1;
- int last = 0;
- uint32_t temp;
- uint32_t tmp_addr;
- uint32_t rof_rule_addr;
- uint64_t tmp_write_swap[4];
- struct mlx5_rxp_rof_entry rules[8];
- int i;
- int db_free;
- int j;
-
- tmp = rte_malloc("", len, 0);
- if (!tmp)
- return -ENOMEM;
- memcpy(tmp, buf, len);
- db_free = mlnx_update_database(priv, id);
- if (db_free < 0) {
- DRV_LOG(ERR, "Failed to setup db memory!");
- rte_free(tmp);
- return db_free;
- }
- for (line = strtok(tmp, del), j = 0; line; line = strtok(NULL, del),
- j++, last = type) {
- ret = rxp_parse_line(line, &type, &address, &val);
- if (ret != 0) {
- if (ret < 0)
- goto parse_error;
- continue;
- }
- switch (type) {
- case MLX5_RXP_ROF_ENTRY_EQ:
- if (skip == 0 && address == 0)
- skip = 1;
- tmp_addr = rxp_get_reg_address(address);
- if (tmp_addr == UINT32_MAX)
- goto parse_error;
- ret = mlx5_devx_regex_register_read(priv->cdev->ctx, id,
- tmp_addr, ®_val);
- if (ret)
- goto parse_error;
- if (skip == -1 && address == 0) {
- if (val == reg_val) {
- skip = 0;
- continue;
- }
- } else if (skip == 0) {
- if (val != reg_val) {
- DRV_LOG(ERR,
- "got %08X expected == %" PRIx64,
- reg_val, val);
- goto parse_error;
- }
- }
- break;
- case MLX5_RXP_ROF_ENTRY_GTE:
- if (skip == 0 && address == 0)
- skip = 1;
- tmp_addr = rxp_get_reg_address(address);
- if (tmp_addr == UINT32_MAX)
- goto parse_error;
- ret = mlx5_devx_regex_register_read(priv->cdev->ctx, id,
- tmp_addr, ®_val);
- if (ret)
- goto parse_error;
- if (skip == -1 && address == 0) {
- if (reg_val >= val) {
- skip = 0;
- continue;
- }
- } else if (skip == 0) {
- if (reg_val < val) {
- DRV_LOG(ERR,
- "got %08X expected >= %" PRIx64,
- reg_val, val);
- goto parse_error;
- }
- }
- break;
- case MLX5_RXP_ROF_ENTRY_LTE:
- tmp_addr = rxp_get_reg_address(address);
- if (tmp_addr == UINT32_MAX)
- goto parse_error;
- ret = mlx5_devx_regex_register_read(priv->cdev->ctx, id,
- tmp_addr, ®_val);
- if (ret)
- goto parse_error;
- if (skip == 0 && address == 0 &&
- last != MLX5_RXP_ROF_ENTRY_GTE) {
- skip = 1;
- } else if (skip == 0 && address == 0 &&
- last == MLX5_RXP_ROF_ENTRY_GTE) {
- if (reg_val > val)
- skip = -1;
- continue;
- }
- if (skip == -1 && address == 0) {
- if (reg_val <= val) {
- skip = 0;
- continue;
- }
- } else if (skip == 0) {
- if (reg_val > val) {
- DRV_LOG(ERR,
- "got %08X expected <= %" PRIx64,
- reg_val, val);
- goto parse_error;
- }
- }
- break;
- case MLX5_RXP_ROF_ENTRY_CHECKSUM:
- break;
- case MLX5_RXP_ROF_ENTRY_CHECKSUM_EX_EM:
- if (skip)
- continue;
- tmp_addr = rxp_get_reg_address(address);
- if (tmp_addr == UINT32_MAX)
- goto parse_error;
-
- ret = mlx5_devx_regex_register_read(priv->cdev->ctx, id,
- tmp_addr, ®_val);
- if (ret) {
- DRV_LOG(ERR, "RXP CSR read failed!");
- return ret;
- }
- if (reg_val != val) {
- DRV_LOG(ERR, "got %08X expected <= %" PRIx64,
- reg_val, val);
- goto parse_error;
- }
- break;
- case MLX5_RXP_ROF_ENTRY_IM:
- if (skip)
- continue;
- /*
- * NOTE: All rules written to RXP must be carried out in
- * triplets of: 2xData + 1xAddr.
- * No optimisation is currently allowed in this
- * sequence to perform less writes.
- */
- temp = val;
- ret |= mlx5_devx_regex_register_write
- (priv->cdev->ctx, id,
- MLX5_RXP_RTRU_CSR_DATA_0, temp);
- temp = (uint32_t)(val >> 32);
- ret |= mlx5_devx_regex_register_write
- (priv->cdev->ctx, id,
- MLX5_RXP_RTRU_CSR_DATA_0 +
- MLX5_RXP_CSR_WIDTH, temp);
- temp = address;
- ret |= mlx5_devx_regex_register_write
- (priv->cdev->ctx, id,
- MLX5_RXP_RTRU_CSR_ADDR, temp);
- if (ret) {
- DRV_LOG(ERR,
- "Failed to copy instructions to RXP.");
- goto parse_error;
- }
- break;
- case MLX5_RXP_ROF_ENTRY_EM:
- if (skip)
- continue;
- for (i = 0; i < MLX5_RXP_NUM_LINES_PER_BLOCK; i++) {
- ret = rxp_parse_line(line, &type,
- &rules[i].addr,
- &rules[i].value);
- if (ret != 0)
- goto parse_error;
- if (i < (MLX5_RXP_NUM_LINES_PER_BLOCK - 1)) {
- line = strtok(NULL, del);
- if (!line)
- goto parse_error;
- }
- }
- if ((uint8_t *)((uint8_t *)
- priv->db[id].ptr +
- ((rules[7].addr <<
- MLX5_RXP_INST_OFFSET))) >=
- ((uint8_t *)((uint8_t *)
- priv->db[id].ptr + MLX5_MAX_DB_SIZE))) {
- DRV_LOG(ERR, "DB exceeded memory!");
- goto parse_error;
- }
- /*
- * Rule address Offset to align with RXP
- * external instruction offset.
- */
- rof_rule_addr = (rules[0].addr << MLX5_RXP_INST_OFFSET);
- /* 32 byte instruction swap (sw work around)! */
- tmp_write_swap[0] = le64toh(rules[4].value);
- tmp_write_swap[1] = le64toh(rules[5].value);
- tmp_write_swap[2] = le64toh(rules[6].value);
- tmp_write_swap[3] = le64toh(rules[7].value);
- /* Write only 4 of the 8 instructions. */
- memcpy((uint8_t *)((uint8_t *)
- priv->db[id].ptr + rof_rule_addr),
- &tmp_write_swap, (sizeof(uint64_t) * 4));
- /* Write 1st 4 rules of block after last 4. */
- rof_rule_addr = (rules[4].addr << MLX5_RXP_INST_OFFSET);
- tmp_write_swap[0] = le64toh(rules[0].value);
- tmp_write_swap[1] = le64toh(rules[1].value);
- tmp_write_swap[2] = le64toh(rules[2].value);
- tmp_write_swap[3] = le64toh(rules[3].value);
- memcpy((uint8_t *)((uint8_t *)
- priv->db[id].ptr + rof_rule_addr),
- &tmp_write_swap, (sizeof(uint64_t) * 4));
- break;
- default:
- break;
- }
-
- }
- rte_free(tmp);
- return 0;
-parse_error:
- rte_free(tmp);
- return ret;
-}
-
-static int
-mlnx_resume_database(struct mlx5_regex_priv *priv, uint8_t id)
-{
- mlx5_devx_regex_database_resume(priv->cdev->ctx, id);
- return 0;
-}
-
-/*
- * Assign db memory for RXP programming.
- */
-static int
-mlnx_update_database(struct mlx5_regex_priv *priv, uint8_t id)
-{
- unsigned int i;
- uint8_t db_free = MLX5_RXP_DB_NOT_ASSIGNED;
- uint8_t eng_assigned = MLX5_RXP_DB_NOT_ASSIGNED;
-
- /* Check which database rxp_eng is currently located if any? */
- for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
- i++) {
- if (priv->db[i].db_assigned_to_eng_num == id) {
- eng_assigned = i;
- break;
- }
- }
- /*
- * If private mode then, we can keep the same db ptr as RXP will be
- * programming EM itself if necessary, however need to see if
- * programmed yet.
- */
- if ((priv->prog_mode == MLX5_RXP_PRIVATE_PROG_MODE) &&
- (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED))
- return eng_assigned;
- /* Check for inactive db memory to use. */
- for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
- i++) {
- if (priv->db[i].active == true)
- continue; /* Already in use, so skip db. */
- /* Set this db to active now as free to use. */
- priv->db[i].active = true;
- /* Now unassign last db index in use by RXP Eng. */
- if (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED) {
- priv->db[eng_assigned].active = false;
- priv->db[eng_assigned].db_assigned_to_eng_num =
- MLX5_RXP_DB_NOT_ASSIGNED;
-
- /* Set all DB memory to 0's before setting up DB. */
- memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
- }
- /* Now reassign new db index with RXP Engine. */
- priv->db[i].db_assigned_to_eng_num = id;
- db_free = i;
- break;
- }
- if (db_free == MLX5_RXP_DB_NOT_ASSIGNED)
- return -1;
- return db_free;
-}
-
-/*
- * Program RXP instruction db to RXP engine/s.
- */
-static int
-program_rxp_rules(struct mlx5_regex_priv *priv, const char *buf, uint32_t len,
- uint8_t id)
-{
- int ret;
- uint32_t val;
- struct ibv_context *ctx = priv->cdev->ctx;
-
- ret = rxp_init_eng(priv, id);
- if (ret < 0)
- return ret;
- /* Confirm the RXP is initialised. */
- if (mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_STATUS, &val)) {
- DRV_LOG(ERR, "Failed to read from RXP!");