The restrictions on the host command data chunks (a chunk can be either NOCOPY or DUP, only a single DUP chunk, no COPY chunk after a NOCOPY/DUP one, and the size limit of the copied part) should be unified to all bus types, so that the opmode will face the same policy regardless of the bus. Move these checks out of the PCIe enqueue functions into iwl_trans_send_cmd(). Assisted-by: LLM Reviewed-by: Emmanuel Grumbach Signed-off-by: Miri Korenblit --- .../net/wireless/intel/iwlwifi/iwl-trans.c | 50 +++++++++++++++++-- .../net/wireless/intel/iwlwifi/pcie/tx-gen2.c | 40 +-------------- drivers/net/wireless/intel/iwlwifi/pcie/tx.c | 42 +--------------- 3 files changed, 51 insertions(+), 81 deletions(-) diff --git a/drivers/net/wireless/intel/iwlwifi/iwl-trans.c b/drivers/net/wireless/intel/iwlwifi/iwl-trans.c index 5c38297dd4df..ff44ce35f703 100644 --- a/drivers/net/wireless/intel/iwlwifi/iwl-trans.c +++ b/drivers/net/wireless/intel/iwlwifi/iwl-trans.c @@ -300,6 +300,46 @@ void iwl_trans_free(struct iwl_trans *trans) cancel_delayed_work_sync(&trans->restart.wk); } +static int iwl_trans_validate_hcmd(struct iwl_trans *trans, + const struct iwl_host_cmd *cmd) +{ + u32 copy_size = iwl_cmd_groupid(cmd->id) ? + sizeof(struct iwl_cmd_header_wide) : + sizeof(struct iwl_cmd_header); + bool had_nocopy = false; + bool had_dup = false; + + for (int i = 0; i < IWL_MAX_CMD_CHUNKS_PER_HCMD; i++) { + if (!cmd->len[i]) + continue; + + if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) { + if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) + return -EINVAL; + had_nocopy = true; + } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { + /* only allowed once */ + if (WARN_ON(had_dup)) + return -EINVAL; + /* also not copied to the static buffer */ + had_nocopy = true; + had_dup = true; + } else { + /* NOCOPY/DUP must not be followed by normal! */ + if (WARN_ON(had_nocopy)) + return -EINVAL; + copy_size += cmd->len[i]; + } + } + + if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE, + "Command %s (%#x) is too large (%u bytes)\n", + iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) + return -EINVAL; + + return 0; +} + int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) { int ret; @@ -318,14 +358,18 @@ int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) "bad state = %d\n", trans->state)) return -EIO; - if (!(cmd->flags & CMD_ASYNC)) - lock_map_acquire_read(&trans->sync_cmd_lockdep_map); - if (trans->conf.wide_cmd_header && !iwl_cmd_groupid(cmd->id)) { if (cmd->id != REPLY_ERROR) cmd->id = DEF_ID(cmd->id); } + ret = iwl_trans_validate_hcmd(trans, cmd); + if (ret) + return ret; + + if (!(cmd->flags & CMD_ASYNC)) + lock_map_acquire_read(&trans->sync_cmd_lockdep_map); + ret = trans->ops->send_cmd(trans, cmd); if (!(cmd->flags & CMD_ASYNC)) diff --git a/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c b/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c index d9e29b5f8297..317cfccc6426 100644 --- a/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c +++ b/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c @@ -1154,7 +1154,6 @@ int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans, dma_addr_t phys_addr; int i, cmd_pos, idx; u16 copy_size, cmd_size, tb0_size; - bool had_nocopy = false; u8 group_id = iwl_cmd_groupid(cmd->id); const u8 *cmddata[IWL_MAX_CMD_CHUNKS_PER_HCMD]; u16 cmdlen[IWL_MAX_CMD_CHUNKS_PER_HCMD]; @@ -1184,52 +1183,17 @@ int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans, copy_size += copy; } - if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) { - had_nocopy = true; - if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) { - idx = -EINVAL; - goto free_dup_buf; - } - } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { - /* - * This is also a chunk that isn't copied - * to the static buffer so set had_nocopy. - */ - had_nocopy = true; - - /* only allowed once */ - if (WARN_ON(dup_buf)) { - idx = -EINVAL; - goto free_dup_buf; - } - + if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { dup_buf = kmemdup(cmddata[i], cmdlen[i], GFP_ATOMIC); if (!dup_buf) return -ENOMEM; - } else { - /* NOCOPY must not be followed by normal! */ - if (WARN_ON(had_nocopy)) { - idx = -EINVAL; - goto free_dup_buf; - } + } else if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)) { copy_size += cmdlen[i]; } cmd_size += cmd->len[i]; } - /* - * If any of the command structures end up being larger than the - * TFD_MAX_PAYLOAD_SIZE and they aren't dynamically allocated into - * separate TFDs, then we will need to increase the size of the buffers - */ - if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE, - "Command %s (%#x) is too large (%d bytes)\n", - iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) { - idx = -EINVAL; - goto free_dup_buf; - } - spin_lock_bh(&txq->lock); idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); diff --git a/drivers/net/wireless/intel/iwlwifi/pcie/tx.c b/drivers/net/wireless/intel/iwlwifi/pcie/tx.c index 2dc83a9932c3..ea0279948ef4 100644 --- a/drivers/net/wireless/intel/iwlwifi/pcie/tx.c +++ b/drivers/net/wireless/intel/iwlwifi/pcie/tx.c @@ -1366,7 +1366,6 @@ int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans, dma_addr_t phys_addr; int idx; u16 copy_size, cmd_size, tb0_size; - bool had_nocopy = false; u8 group_id = iwl_cmd_groupid(cmd->id); int i, ret; u32 cmd_pos; @@ -1407,54 +1406,17 @@ int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans, copy_size += copy; } - if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) { - had_nocopy = true; - if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) { - idx = -EINVAL; - goto free_dup_buf; - } - } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { - /* - * This is also a chunk that isn't copied - * to the static buffer so set had_nocopy. - */ - had_nocopy = true; - - /* only allowed once */ - if (WARN_ON(dup_buf)) { - idx = -EINVAL; - goto free_dup_buf; - } - + if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { dup_buf = kmemdup(cmddata[i], cmdlen[i], GFP_ATOMIC); if (!dup_buf) return -ENOMEM; - } else { - /* NOCOPY must not be followed by normal! */ - if (WARN_ON(had_nocopy)) { - idx = -EINVAL; - goto free_dup_buf; - } + } else if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)) { copy_size += cmdlen[i]; } cmd_size += cmd->len[i]; } - /* - * If any of the command structures end up being larger than - * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically - * allocated into separate TFDs, then we will need to - * increase the size of the buffers. - */ - if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE, - "Command %s (%#x) is too large (%d bytes)\n", - iwl_get_cmd_string(trans, cmd->id), - cmd->id, copy_size)) { - idx = -EINVAL; - goto free_dup_buf; - } - spin_lock_bh(&txq->lock); if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) { -- 2.34.1