18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 28e99ea8dSJohannes Berg /* 38e99ea8dSJohannes Berg * Copyright (C) 2015 Intel Mobile Communications GmbH 48e99ea8dSJohannes Berg * Copyright (C) 2016-2017 Intel Deutschland GmbH 58e99ea8dSJohannes Berg * Copyright (C) 2019-2020 Intel Corporation 68e99ea8dSJohannes Berg */ 7e705c121SKalle Valo #include <linux/kernel.h> 839bdb17eSSharon Dvir #include <linux/bsearch.h> 939bdb17eSSharon Dvir 10fda1bd0dSMordechay Goodstein #include "fw/api/tx.h" 11e705c121SKalle Valo #include "iwl-trans.h" 1239bdb17eSSharon Dvir #include "iwl-drv.h" 133cd1980bSSara Sharon #include "iwl-fh.h" 140cd1ad2dSMordechay Goodstein #include "queue/tx.h" 15a26014e2SMordechay Goodstein #include <linux/dmapool.h> 16*781b9ae4SMukesh Sisodiya #include "fw/api/commands.h" 17e705c121SKalle Valo 18e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 19e705c121SKalle Valo struct device *dev, 20a89c72ffSJohannes Berg const struct iwl_trans_ops *ops, 21fda1bd0dSMordechay Goodstein const struct iwl_cfg_trans_params *cfg_trans) 22e705c121SKalle Valo { 23e705c121SKalle Valo struct iwl_trans *trans; 24fda1bd0dSMordechay Goodstein int txcmd_size, txcmd_align; 25e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 26e705c121SKalle Valo static struct lock_class_key __key; 27e705c121SKalle Valo #endif 28e705c121SKalle Valo 295a41a86cSSharon Dvir trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL); 30e705c121SKalle Valo if (!trans) 31e705c121SKalle Valo return NULL; 32e705c121SKalle Valo 33fda1bd0dSMordechay Goodstein trans->trans_cfg = cfg_trans; 34fda1bd0dSMordechay Goodstein if (!cfg_trans->gen2) { 35fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd); 36fda1bd0dSMordechay Goodstein txcmd_align = sizeof(void *); 37fda1bd0dSMordechay Goodstein } else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) { 38fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen2); 39fda1bd0dSMordechay Goodstein txcmd_align = 64; 40fda1bd0dSMordechay Goodstein } else { 41fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen3); 42fda1bd0dSMordechay Goodstein txcmd_align = 128; 43fda1bd0dSMordechay Goodstein } 44fda1bd0dSMordechay Goodstein 45fda1bd0dSMordechay Goodstein txcmd_size += sizeof(struct iwl_cmd_header); 46fda1bd0dSMordechay Goodstein txcmd_size += 36; /* biggest possible 802.11 header */ 47fda1bd0dSMordechay Goodstein 48fda1bd0dSMordechay Goodstein /* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */ 49fda1bd0dSMordechay Goodstein if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align)) 50fda1bd0dSMordechay Goodstein return ERR_PTR(-EINVAL); 51fda1bd0dSMordechay Goodstein 52e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 53e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 54e705c121SKalle Valo &__key, 0); 55e705c121SKalle Valo #endif 56e705c121SKalle Valo 57e705c121SKalle Valo trans->dev = dev; 58e705c121SKalle Valo trans->ops = ops; 59e705c121SKalle Valo trans->num_rx_queues = 1; 60e705c121SKalle Valo 61a26014e2SMordechay Goodstein if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 62a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl); 63a26014e2SMordechay Goodstein else 64a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl); 65a26014e2SMordechay Goodstein /* 66a26014e2SMordechay Goodstein * For gen2 devices, we use a single allocation for each byte-count 67a26014e2SMordechay Goodstein * table, but they're pretty small (1k) so use a DMA pool that we 68a26014e2SMordechay Goodstein * allocate here. 69a26014e2SMordechay Goodstein */ 70a26014e2SMordechay Goodstein if (trans->trans_cfg->gen2) { 71a26014e2SMordechay Goodstein trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev, 72a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size, 73a26014e2SMordechay Goodstein 256, 0); 74a26014e2SMordechay Goodstein if (!trans->txqs.bc_pool) 75a26014e2SMordechay Goodstein return NULL; 76a26014e2SMordechay Goodstein } 77885375d0SMordechay Goodstein 78885375d0SMordechay Goodstein if (trans->trans_cfg->use_tfh) { 79885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 64; 80885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS; 81885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd); 82885375d0SMordechay Goodstein } else { 83885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 36; 84885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS; 85885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfd); 86885375d0SMordechay Goodstein } 87885375d0SMordechay Goodstein trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans); 88885375d0SMordechay Goodstein 89e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 90e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev)); 91e705c121SKalle Valo trans->dev_cmd_pool = 92e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name, 93fda1bd0dSMordechay Goodstein txcmd_size, txcmd_align, 94a89c72ffSJohannes Berg SLAB_HWCACHE_ALIGN, NULL); 95e705c121SKalle Valo if (!trans->dev_cmd_pool) 965a41a86cSSharon Dvir return NULL; 97e705c121SKalle Valo 98d6d517b7SSara Sharon WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty); 99d6d517b7SSara Sharon 1000cd1ad2dSMordechay Goodstein trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page); 1010cd1ad2dSMordechay Goodstein if (!trans->txqs.tso_hdr_page) { 1020cd1ad2dSMordechay Goodstein kmem_cache_destroy(trans->dev_cmd_pool); 1030cd1ad2dSMordechay Goodstein return NULL; 1040cd1ad2dSMordechay Goodstein } 1050cd1ad2dSMordechay Goodstein 10613f028b4SMordechay Goodstein /* Initialize the wait queue for commands */ 10713f028b4SMordechay Goodstein init_waitqueue_head(&trans->wait_command_queue); 10813f028b4SMordechay Goodstein 109e705c121SKalle Valo return trans; 110e705c121SKalle Valo } 111e705c121SKalle Valo 112e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans) 113e705c121SKalle Valo { 1140cd1ad2dSMordechay Goodstein int i; 1150cd1ad2dSMordechay Goodstein 1160cd1ad2dSMordechay Goodstein for_each_possible_cpu(i) { 1170cd1ad2dSMordechay Goodstein struct iwl_tso_hdr_page *p = 1180cd1ad2dSMordechay Goodstein per_cpu_ptr(trans->txqs.tso_hdr_page, i); 1190cd1ad2dSMordechay Goodstein 1200cd1ad2dSMordechay Goodstein if (p->page) 1210cd1ad2dSMordechay Goodstein __free_page(p->page); 1220cd1ad2dSMordechay Goodstein } 1230cd1ad2dSMordechay Goodstein 1240cd1ad2dSMordechay Goodstein free_percpu(trans->txqs.tso_hdr_page); 1250cd1ad2dSMordechay Goodstein 126e705c121SKalle Valo kmem_cache_destroy(trans->dev_cmd_pool); 127e705c121SKalle Valo } 12892fe8343SEmmanuel Grumbach 12992fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 13092fe8343SEmmanuel Grumbach { 13192fe8343SEmmanuel Grumbach int ret; 13292fe8343SEmmanuel Grumbach 13392fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 134326477e4SJohannes Berg test_bit(STATUS_RFKILL_OPMODE, &trans->status))) 13592fe8343SEmmanuel Grumbach return -ERFKILL; 13692fe8343SEmmanuel Grumbach 137708a39aaSHaim Dreyfuss /* 138708a39aaSHaim Dreyfuss * We can't test IWL_MVM_STATUS_IN_D3 in mvm->status because this 139708a39aaSHaim Dreyfuss * bit is set early in the D3 flow, before we send all the commands 140708a39aaSHaim Dreyfuss * that configure the firmware for D3 operation (power, patterns, ...) 141708a39aaSHaim Dreyfuss * and we don't want to flag all those with CMD_SEND_IN_D3. 142708a39aaSHaim Dreyfuss * So use the system_pm_mode instead. The only command sent after 143708a39aaSHaim Dreyfuss * we set system_pm_mode is D3_CONFIG_CMD, which we now flag with 144708a39aaSHaim Dreyfuss * CMD_SEND_IN_D3. 145708a39aaSHaim Dreyfuss */ 146708a39aaSHaim Dreyfuss if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 && 147708a39aaSHaim Dreyfuss !(cmd->flags & CMD_SEND_IN_D3))) 148708a39aaSHaim Dreyfuss return -EHOSTDOWN; 149708a39aaSHaim Dreyfuss 15092fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 15192fe8343SEmmanuel Grumbach return -EIO; 15292fe8343SEmmanuel Grumbach 15392fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 15492fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 15592fe8343SEmmanuel Grumbach return -EIO; 15692fe8343SEmmanuel Grumbach } 15792fe8343SEmmanuel Grumbach 15892fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 15992fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC))) 16092fe8343SEmmanuel Grumbach return -EINVAL; 16192fe8343SEmmanuel Grumbach 16292fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 16392fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 16492fe8343SEmmanuel Grumbach 165*781b9ae4SMukesh Sisodiya if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) { 166*781b9ae4SMukesh Sisodiya if (cmd->id != REPLY_ERROR) 1675b88792cSSara Sharon cmd->id = DEF_ID(cmd->id); 168*781b9ae4SMukesh Sisodiya } 1695b88792cSSara Sharon 17013f028b4SMordechay Goodstein ret = iwl_trans_txq_send_hcmd(trans, cmd); 17192fe8343SEmmanuel Grumbach 17292fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 17392fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map); 17492fe8343SEmmanuel Grumbach 1750ec971fdSJohannes Berg if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt)) 1760ec971fdSJohannes Berg return -EIO; 1770ec971fdSJohannes Berg 17892fe8343SEmmanuel Grumbach return ret; 17992fe8343SEmmanuel Grumbach } 18092fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 18139bdb17eSSharon Dvir 18239bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names. 18339bdb17eSSharon Dvir * Used in the binary search over a list of host commands. 18439bdb17eSSharon Dvir * 18539bdb17eSSharon Dvir * @key: command_id that we're looking for. 18639bdb17eSSharon Dvir * @elt: struct iwl_hcmd_names candidate for match. 18739bdb17eSSharon Dvir * 18839bdb17eSSharon Dvir * @return 0 iff equal. 18939bdb17eSSharon Dvir */ 19039bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt) 19139bdb17eSSharon Dvir { 19239bdb17eSSharon Dvir const struct iwl_hcmd_names *name = elt; 19339bdb17eSSharon Dvir u8 cmd1 = *(u8 *)key; 19439bdb17eSSharon Dvir u8 cmd2 = name->cmd_id; 19539bdb17eSSharon Dvir 19639bdb17eSSharon Dvir return (cmd1 - cmd2); 19739bdb17eSSharon Dvir } 19839bdb17eSSharon Dvir 19939bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id) 20039bdb17eSSharon Dvir { 20139bdb17eSSharon Dvir u8 grp, cmd; 20239bdb17eSSharon Dvir struct iwl_hcmd_names *ret; 20339bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 20439bdb17eSSharon Dvir size_t size = sizeof(struct iwl_hcmd_names); 20539bdb17eSSharon Dvir 20639bdb17eSSharon Dvir grp = iwl_cmd_groupid(id); 20739bdb17eSSharon Dvir cmd = iwl_cmd_opcode(id); 20839bdb17eSSharon Dvir 20939bdb17eSSharon Dvir if (!trans->command_groups || grp >= trans->command_groups_size || 21039bdb17eSSharon Dvir !trans->command_groups[grp].arr) 21139bdb17eSSharon Dvir return "UNKNOWN"; 21239bdb17eSSharon Dvir 21339bdb17eSSharon Dvir arr = &trans->command_groups[grp]; 21439bdb17eSSharon Dvir ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp); 21539bdb17eSSharon Dvir if (!ret) 21639bdb17eSSharon Dvir return "UNKNOWN"; 21739bdb17eSSharon Dvir return ret->cmd_name; 21839bdb17eSSharon Dvir } 21939bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string); 22039bdb17eSSharon Dvir 22139bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans) 22239bdb17eSSharon Dvir { 22339bdb17eSSharon Dvir int i, j; 22439bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 22539bdb17eSSharon Dvir 22639bdb17eSSharon Dvir for (i = 0; i < trans->command_groups_size; i++) { 22739bdb17eSSharon Dvir arr = &trans->command_groups[i]; 22839bdb17eSSharon Dvir if (!arr->arr) 22939bdb17eSSharon Dvir continue; 23039bdb17eSSharon Dvir for (j = 0; j < arr->size - 1; j++) 23139bdb17eSSharon Dvir if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id) 23239bdb17eSSharon Dvir return -1; 23339bdb17eSSharon Dvir } 23439bdb17eSSharon Dvir return 0; 23539bdb17eSSharon Dvir } 23639bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted); 237