1*8e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2*8e99ea8dSJohannes Berg /* 3*8e99ea8dSJohannes Berg * Copyright (C) 2015 Intel Mobile Communications GmbH 4*8e99ea8dSJohannes Berg * Copyright (C) 2016-2017 Intel Deutschland GmbH 5*8e99ea8dSJohannes Berg * Copyright (C) 2019-2020 Intel Corporation 6*8e99ea8dSJohannes 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> 16e705c121SKalle Valo 17e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 18e705c121SKalle Valo struct device *dev, 19a89c72ffSJohannes Berg const struct iwl_trans_ops *ops, 20fda1bd0dSMordechay Goodstein const struct iwl_cfg_trans_params *cfg_trans) 21e705c121SKalle Valo { 22e705c121SKalle Valo struct iwl_trans *trans; 23fda1bd0dSMordechay Goodstein int txcmd_size, txcmd_align; 24e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 25e705c121SKalle Valo static struct lock_class_key __key; 26e705c121SKalle Valo #endif 27e705c121SKalle Valo 285a41a86cSSharon Dvir trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL); 29e705c121SKalle Valo if (!trans) 30e705c121SKalle Valo return NULL; 31e705c121SKalle Valo 32fda1bd0dSMordechay Goodstein trans->trans_cfg = cfg_trans; 33fda1bd0dSMordechay Goodstein if (!cfg_trans->gen2) { 34fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd); 35fda1bd0dSMordechay Goodstein txcmd_align = sizeof(void *); 36fda1bd0dSMordechay Goodstein } else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) { 37fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen2); 38fda1bd0dSMordechay Goodstein txcmd_align = 64; 39fda1bd0dSMordechay Goodstein } else { 40fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen3); 41fda1bd0dSMordechay Goodstein txcmd_align = 128; 42fda1bd0dSMordechay Goodstein } 43fda1bd0dSMordechay Goodstein 44fda1bd0dSMordechay Goodstein txcmd_size += sizeof(struct iwl_cmd_header); 45fda1bd0dSMordechay Goodstein txcmd_size += 36; /* biggest possible 802.11 header */ 46fda1bd0dSMordechay Goodstein 47fda1bd0dSMordechay Goodstein /* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */ 48fda1bd0dSMordechay Goodstein if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align)) 49fda1bd0dSMordechay Goodstein return ERR_PTR(-EINVAL); 50fda1bd0dSMordechay Goodstein 51e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 52e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 53e705c121SKalle Valo &__key, 0); 54e705c121SKalle Valo #endif 55e705c121SKalle Valo 56e705c121SKalle Valo trans->dev = dev; 57e705c121SKalle Valo trans->ops = ops; 58e705c121SKalle Valo trans->num_rx_queues = 1; 59e705c121SKalle Valo 60a26014e2SMordechay Goodstein if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 61a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl); 62a26014e2SMordechay Goodstein else 63a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl); 64a26014e2SMordechay Goodstein /* 65a26014e2SMordechay Goodstein * For gen2 devices, we use a single allocation for each byte-count 66a26014e2SMordechay Goodstein * table, but they're pretty small (1k) so use a DMA pool that we 67a26014e2SMordechay Goodstein * allocate here. 68a26014e2SMordechay Goodstein */ 69a26014e2SMordechay Goodstein if (trans->trans_cfg->gen2) { 70a26014e2SMordechay Goodstein trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev, 71a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size, 72a26014e2SMordechay Goodstein 256, 0); 73a26014e2SMordechay Goodstein if (!trans->txqs.bc_pool) 74a26014e2SMordechay Goodstein return NULL; 75a26014e2SMordechay Goodstein } 76885375d0SMordechay Goodstein 77885375d0SMordechay Goodstein if (trans->trans_cfg->use_tfh) { 78885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 64; 79885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS; 80885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd); 81885375d0SMordechay Goodstein } else { 82885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 36; 83885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS; 84885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfd); 85885375d0SMordechay Goodstein } 86885375d0SMordechay Goodstein trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans); 87885375d0SMordechay Goodstein 88e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 89e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev)); 90e705c121SKalle Valo trans->dev_cmd_pool = 91e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name, 92fda1bd0dSMordechay Goodstein txcmd_size, txcmd_align, 93a89c72ffSJohannes Berg SLAB_HWCACHE_ALIGN, NULL); 94e705c121SKalle Valo if (!trans->dev_cmd_pool) 955a41a86cSSharon Dvir return NULL; 96e705c121SKalle Valo 97d6d517b7SSara Sharon WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty); 98d6d517b7SSara Sharon 990cd1ad2dSMordechay Goodstein trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page); 1000cd1ad2dSMordechay Goodstein if (!trans->txqs.tso_hdr_page) { 1010cd1ad2dSMordechay Goodstein kmem_cache_destroy(trans->dev_cmd_pool); 1020cd1ad2dSMordechay Goodstein return NULL; 1030cd1ad2dSMordechay Goodstein } 1040cd1ad2dSMordechay Goodstein 105e705c121SKalle Valo return trans; 106e705c121SKalle Valo } 107e705c121SKalle Valo 108e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans) 109e705c121SKalle Valo { 1100cd1ad2dSMordechay Goodstein int i; 1110cd1ad2dSMordechay Goodstein 1120cd1ad2dSMordechay Goodstein for_each_possible_cpu(i) { 1130cd1ad2dSMordechay Goodstein struct iwl_tso_hdr_page *p = 1140cd1ad2dSMordechay Goodstein per_cpu_ptr(trans->txqs.tso_hdr_page, i); 1150cd1ad2dSMordechay Goodstein 1160cd1ad2dSMordechay Goodstein if (p->page) 1170cd1ad2dSMordechay Goodstein __free_page(p->page); 1180cd1ad2dSMordechay Goodstein } 1190cd1ad2dSMordechay Goodstein 1200cd1ad2dSMordechay Goodstein free_percpu(trans->txqs.tso_hdr_page); 1210cd1ad2dSMordechay Goodstein 122e705c121SKalle Valo kmem_cache_destroy(trans->dev_cmd_pool); 123e705c121SKalle Valo } 12492fe8343SEmmanuel Grumbach 12592fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 12692fe8343SEmmanuel Grumbach { 12792fe8343SEmmanuel Grumbach int ret; 12892fe8343SEmmanuel Grumbach 12992fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 130326477e4SJohannes Berg test_bit(STATUS_RFKILL_OPMODE, &trans->status))) 13192fe8343SEmmanuel Grumbach return -ERFKILL; 13292fe8343SEmmanuel Grumbach 13392fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 13492fe8343SEmmanuel Grumbach return -EIO; 13592fe8343SEmmanuel Grumbach 13692fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 13792fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 13892fe8343SEmmanuel Grumbach return -EIO; 13992fe8343SEmmanuel Grumbach } 14092fe8343SEmmanuel Grumbach 14192fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 14292fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC))) 14392fe8343SEmmanuel Grumbach return -EINVAL; 14492fe8343SEmmanuel Grumbach 14592fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 14692fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 14792fe8343SEmmanuel Grumbach 148b7d96bcaSLuca Coelho if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) 1495b88792cSSara Sharon cmd->id = DEF_ID(cmd->id); 1505b88792cSSara Sharon 15192fe8343SEmmanuel Grumbach ret = trans->ops->send_cmd(trans, cmd); 15292fe8343SEmmanuel Grumbach 15392fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 15492fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map); 15592fe8343SEmmanuel Grumbach 1560ec971fdSJohannes Berg if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt)) 1570ec971fdSJohannes Berg return -EIO; 1580ec971fdSJohannes Berg 15992fe8343SEmmanuel Grumbach return ret; 16092fe8343SEmmanuel Grumbach } 16192fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 16239bdb17eSSharon Dvir 16339bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names. 16439bdb17eSSharon Dvir * Used in the binary search over a list of host commands. 16539bdb17eSSharon Dvir * 16639bdb17eSSharon Dvir * @key: command_id that we're looking for. 16739bdb17eSSharon Dvir * @elt: struct iwl_hcmd_names candidate for match. 16839bdb17eSSharon Dvir * 16939bdb17eSSharon Dvir * @return 0 iff equal. 17039bdb17eSSharon Dvir */ 17139bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt) 17239bdb17eSSharon Dvir { 17339bdb17eSSharon Dvir const struct iwl_hcmd_names *name = elt; 17439bdb17eSSharon Dvir u8 cmd1 = *(u8 *)key; 17539bdb17eSSharon Dvir u8 cmd2 = name->cmd_id; 17639bdb17eSSharon Dvir 17739bdb17eSSharon Dvir return (cmd1 - cmd2); 17839bdb17eSSharon Dvir } 17939bdb17eSSharon Dvir 18039bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id) 18139bdb17eSSharon Dvir { 18239bdb17eSSharon Dvir u8 grp, cmd; 18339bdb17eSSharon Dvir struct iwl_hcmd_names *ret; 18439bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 18539bdb17eSSharon Dvir size_t size = sizeof(struct iwl_hcmd_names); 18639bdb17eSSharon Dvir 18739bdb17eSSharon Dvir grp = iwl_cmd_groupid(id); 18839bdb17eSSharon Dvir cmd = iwl_cmd_opcode(id); 18939bdb17eSSharon Dvir 19039bdb17eSSharon Dvir if (!trans->command_groups || grp >= trans->command_groups_size || 19139bdb17eSSharon Dvir !trans->command_groups[grp].arr) 19239bdb17eSSharon Dvir return "UNKNOWN"; 19339bdb17eSSharon Dvir 19439bdb17eSSharon Dvir arr = &trans->command_groups[grp]; 19539bdb17eSSharon Dvir ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp); 19639bdb17eSSharon Dvir if (!ret) 19739bdb17eSSharon Dvir return "UNKNOWN"; 19839bdb17eSSharon Dvir return ret->cmd_name; 19939bdb17eSSharon Dvir } 20039bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string); 20139bdb17eSSharon Dvir 20239bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans) 20339bdb17eSSharon Dvir { 20439bdb17eSSharon Dvir int i, j; 20539bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 20639bdb17eSSharon Dvir 20739bdb17eSSharon Dvir for (i = 0; i < trans->command_groups_size; i++) { 20839bdb17eSSharon Dvir arr = &trans->command_groups[i]; 20939bdb17eSSharon Dvir if (!arr->arr) 21039bdb17eSSharon Dvir continue; 21139bdb17eSSharon Dvir for (j = 0; j < arr->size - 1; j++) 21239bdb17eSSharon Dvir if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id) 21339bdb17eSSharon Dvir return -1; 21439bdb17eSSharon Dvir } 21539bdb17eSSharon Dvir return 0; 21639bdb17eSSharon Dvir } 21739bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted); 218