1e705c121SKalle Valo /****************************************************************************** 2e705c121SKalle Valo * 3e705c121SKalle Valo * This file is provided under a dual BSD/GPLv2 license. When using or 4e705c121SKalle Valo * redistributing this file, you may do so under either license. 5e705c121SKalle Valo * 6e705c121SKalle Valo * GPL LICENSE SUMMARY 7e705c121SKalle Valo * 8e705c121SKalle Valo * Copyright(c) 2015 Intel Mobile Communications GmbH 9d621d3c7SLuca Coelho * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 10fda1bd0dSMordechay Goodstein * Copyright(c) 2019 - 2020 Intel Corporation 11e705c121SKalle Valo * 12e705c121SKalle Valo * This program is free software; you can redistribute it and/or modify 13e705c121SKalle Valo * it under the terms of version 2 of the GNU General Public License as 14e705c121SKalle Valo * published by the Free Software Foundation. 15e705c121SKalle Valo * 16e705c121SKalle Valo * This program is distributed in the hope that it will be useful, but 17e705c121SKalle Valo * WITHOUT ANY WARRANTY; without even the implied warranty of 18e705c121SKalle Valo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19e705c121SKalle Valo * General Public License for more details. 20e705c121SKalle Valo * 21e705c121SKalle Valo * The full GNU General Public License is included in this distribution 22e705c121SKalle Valo * in the file called COPYING. 23e705c121SKalle Valo * 24e705c121SKalle Valo * Contact Information: 25d01c5366SEmmanuel Grumbach * Intel Linux Wireless <linuxwifi@intel.com> 26e705c121SKalle Valo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 27e705c121SKalle Valo * 28e705c121SKalle Valo * BSD LICENSE 29e705c121SKalle Valo * 30e705c121SKalle Valo * Copyright(c) 2015 Intel Mobile Communications GmbH 31d621d3c7SLuca Coelho * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 32fda1bd0dSMordechay Goodstein * Copyright(c) 2019 - 2020 Intel Corporation 33e705c121SKalle Valo * All rights reserved. 34e705c121SKalle Valo * 35e705c121SKalle Valo * Redistribution and use in source and binary forms, with or without 36e705c121SKalle Valo * modification, are permitted provided that the following conditions 37e705c121SKalle Valo * are met: 38e705c121SKalle Valo * 39e705c121SKalle Valo * * Redistributions of source code must retain the above copyright 40e705c121SKalle Valo * notice, this list of conditions and the following disclaimer. 41e705c121SKalle Valo * * Redistributions in binary form must reproduce the above copyright 42e705c121SKalle Valo * notice, this list of conditions and the following disclaimer in 43e705c121SKalle Valo * the documentation and/or other materials provided with the 44e705c121SKalle Valo * distribution. 45e705c121SKalle Valo * * Neither the name Intel Corporation nor the names of its 46e705c121SKalle Valo * contributors may be used to endorse or promote products derived 47e705c121SKalle Valo * from this software without specific prior written permission. 48e705c121SKalle Valo * 49e705c121SKalle Valo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 50e705c121SKalle Valo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 51e705c121SKalle Valo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 52e705c121SKalle Valo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 53e705c121SKalle Valo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 54e705c121SKalle Valo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 55e705c121SKalle Valo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56e705c121SKalle Valo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57e705c121SKalle Valo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58e705c121SKalle Valo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 59e705c121SKalle Valo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60e705c121SKalle Valo * 61e705c121SKalle Valo *****************************************************************************/ 62e705c121SKalle Valo #include <linux/kernel.h> 6339bdb17eSSharon Dvir #include <linux/bsearch.h> 6439bdb17eSSharon Dvir 65fda1bd0dSMordechay Goodstein #include "fw/api/tx.h" 66e705c121SKalle Valo #include "iwl-trans.h" 6739bdb17eSSharon Dvir #include "iwl-drv.h" 683cd1980bSSara Sharon #include "iwl-fh.h" 690cd1ad2dSMordechay Goodstein #include "queue/tx.h" 70a26014e2SMordechay Goodstein #include <linux/dmapool.h> 71e705c121SKalle Valo 72e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 73e705c121SKalle Valo struct device *dev, 74a89c72ffSJohannes Berg const struct iwl_trans_ops *ops, 75fda1bd0dSMordechay Goodstein const struct iwl_cfg_trans_params *cfg_trans) 76e705c121SKalle Valo { 77e705c121SKalle Valo struct iwl_trans *trans; 78fda1bd0dSMordechay Goodstein int txcmd_size, txcmd_align; 79e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 80e705c121SKalle Valo static struct lock_class_key __key; 81e705c121SKalle Valo #endif 82e705c121SKalle Valo 835a41a86cSSharon Dvir trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL); 84e705c121SKalle Valo if (!trans) 85e705c121SKalle Valo return NULL; 86e705c121SKalle Valo 87fda1bd0dSMordechay Goodstein trans->trans_cfg = cfg_trans; 88fda1bd0dSMordechay Goodstein if (!cfg_trans->gen2) { 89fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd); 90fda1bd0dSMordechay Goodstein txcmd_align = sizeof(void *); 91fda1bd0dSMordechay Goodstein } else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) { 92fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen2); 93fda1bd0dSMordechay Goodstein txcmd_align = 64; 94fda1bd0dSMordechay Goodstein } else { 95fda1bd0dSMordechay Goodstein txcmd_size = sizeof(struct iwl_tx_cmd_gen3); 96fda1bd0dSMordechay Goodstein txcmd_align = 128; 97fda1bd0dSMordechay Goodstein } 98fda1bd0dSMordechay Goodstein 99fda1bd0dSMordechay Goodstein txcmd_size += sizeof(struct iwl_cmd_header); 100fda1bd0dSMordechay Goodstein txcmd_size += 36; /* biggest possible 802.11 header */ 101fda1bd0dSMordechay Goodstein 102fda1bd0dSMordechay Goodstein /* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */ 103fda1bd0dSMordechay Goodstein if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align)) 104fda1bd0dSMordechay Goodstein return ERR_PTR(-EINVAL); 105fda1bd0dSMordechay Goodstein 106e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 107e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 108e705c121SKalle Valo &__key, 0); 109e705c121SKalle Valo #endif 110e705c121SKalle Valo 111e705c121SKalle Valo trans->dev = dev; 112e705c121SKalle Valo trans->ops = ops; 113e705c121SKalle Valo trans->num_rx_queues = 1; 114e705c121SKalle Valo 115a26014e2SMordechay Goodstein if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 116a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl); 117a26014e2SMordechay Goodstein else 118a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl); 119a26014e2SMordechay Goodstein /* 120a26014e2SMordechay Goodstein * For gen2 devices, we use a single allocation for each byte-count 121a26014e2SMordechay Goodstein * table, but they're pretty small (1k) so use a DMA pool that we 122a26014e2SMordechay Goodstein * allocate here. 123a26014e2SMordechay Goodstein */ 124a26014e2SMordechay Goodstein if (trans->trans_cfg->gen2) { 125a26014e2SMordechay Goodstein trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev, 126a26014e2SMordechay Goodstein trans->txqs.bc_tbl_size, 127a26014e2SMordechay Goodstein 256, 0); 128a26014e2SMordechay Goodstein if (!trans->txqs.bc_pool) 129a26014e2SMordechay Goodstein return NULL; 130a26014e2SMordechay Goodstein } 131885375d0SMordechay Goodstein 132885375d0SMordechay Goodstein if (trans->trans_cfg->use_tfh) { 133885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 64; 134885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS; 135885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd); 136885375d0SMordechay Goodstein } else { 137885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 36; 138885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS; 139885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfd); 140885375d0SMordechay Goodstein } 141885375d0SMordechay Goodstein trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans); 142885375d0SMordechay Goodstein 143e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 144e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev)); 145e705c121SKalle Valo trans->dev_cmd_pool = 146e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name, 147fda1bd0dSMordechay Goodstein txcmd_size, txcmd_align, 148a89c72ffSJohannes Berg SLAB_HWCACHE_ALIGN, NULL); 149e705c121SKalle Valo if (!trans->dev_cmd_pool) 1505a41a86cSSharon Dvir return NULL; 151e705c121SKalle Valo 152d6d517b7SSara Sharon WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty); 153d6d517b7SSara Sharon 1540cd1ad2dSMordechay Goodstein trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page); 1550cd1ad2dSMordechay Goodstein if (!trans->txqs.tso_hdr_page) { 1560cd1ad2dSMordechay Goodstein kmem_cache_destroy(trans->dev_cmd_pool); 1570cd1ad2dSMordechay Goodstein return NULL; 1580cd1ad2dSMordechay Goodstein } 1590cd1ad2dSMordechay Goodstein 160e705c121SKalle Valo return trans; 161e705c121SKalle Valo } 162e705c121SKalle Valo 163e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans) 164e705c121SKalle Valo { 1650cd1ad2dSMordechay Goodstein int i; 1660cd1ad2dSMordechay Goodstein 1670cd1ad2dSMordechay Goodstein for_each_possible_cpu(i) { 1680cd1ad2dSMordechay Goodstein struct iwl_tso_hdr_page *p = 1690cd1ad2dSMordechay Goodstein per_cpu_ptr(trans->txqs.tso_hdr_page, i); 1700cd1ad2dSMordechay Goodstein 1710cd1ad2dSMordechay Goodstein if (p->page) 1720cd1ad2dSMordechay Goodstein __free_page(p->page); 1730cd1ad2dSMordechay Goodstein } 1740cd1ad2dSMordechay Goodstein 1750cd1ad2dSMordechay Goodstein free_percpu(trans->txqs.tso_hdr_page); 1760cd1ad2dSMordechay Goodstein 177e705c121SKalle Valo kmem_cache_destroy(trans->dev_cmd_pool); 178e705c121SKalle Valo } 17992fe8343SEmmanuel Grumbach 18092fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 18192fe8343SEmmanuel Grumbach { 18292fe8343SEmmanuel Grumbach int ret; 18392fe8343SEmmanuel Grumbach 18492fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 185326477e4SJohannes Berg test_bit(STATUS_RFKILL_OPMODE, &trans->status))) 18692fe8343SEmmanuel Grumbach return -ERFKILL; 18792fe8343SEmmanuel Grumbach 18892fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 18992fe8343SEmmanuel Grumbach return -EIO; 19092fe8343SEmmanuel Grumbach 19192fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 19292fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 19392fe8343SEmmanuel Grumbach return -EIO; 19492fe8343SEmmanuel Grumbach } 19592fe8343SEmmanuel Grumbach 19692fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 19792fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC))) 19892fe8343SEmmanuel Grumbach return -EINVAL; 19992fe8343SEmmanuel Grumbach 20092fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 20192fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 20292fe8343SEmmanuel Grumbach 2030a8159cbSMordechay Goodstein if (!iwl_cmd_groupid(cmd->id)) 2045b88792cSSara Sharon cmd->id = DEF_ID(cmd->id); 2055b88792cSSara Sharon 20692fe8343SEmmanuel Grumbach ret = trans->ops->send_cmd(trans, cmd); 20792fe8343SEmmanuel Grumbach 20892fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 20992fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map); 21092fe8343SEmmanuel Grumbach 2110ec971fdSJohannes Berg if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt)) 2120ec971fdSJohannes Berg return -EIO; 2130ec971fdSJohannes Berg 21492fe8343SEmmanuel Grumbach return ret; 21592fe8343SEmmanuel Grumbach } 21692fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 21739bdb17eSSharon Dvir 21839bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names. 21939bdb17eSSharon Dvir * Used in the binary search over a list of host commands. 22039bdb17eSSharon Dvir * 22139bdb17eSSharon Dvir * @key: command_id that we're looking for. 22239bdb17eSSharon Dvir * @elt: struct iwl_hcmd_names candidate for match. 22339bdb17eSSharon Dvir * 22439bdb17eSSharon Dvir * @return 0 iff equal. 22539bdb17eSSharon Dvir */ 22639bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt) 22739bdb17eSSharon Dvir { 22839bdb17eSSharon Dvir const struct iwl_hcmd_names *name = elt; 22939bdb17eSSharon Dvir u8 cmd1 = *(u8 *)key; 23039bdb17eSSharon Dvir u8 cmd2 = name->cmd_id; 23139bdb17eSSharon Dvir 23239bdb17eSSharon Dvir return (cmd1 - cmd2); 23339bdb17eSSharon Dvir } 23439bdb17eSSharon Dvir 23539bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id) 23639bdb17eSSharon Dvir { 23739bdb17eSSharon Dvir u8 grp, cmd; 23839bdb17eSSharon Dvir struct iwl_hcmd_names *ret; 23939bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 24039bdb17eSSharon Dvir size_t size = sizeof(struct iwl_hcmd_names); 24139bdb17eSSharon Dvir 24239bdb17eSSharon Dvir grp = iwl_cmd_groupid(id); 24339bdb17eSSharon Dvir cmd = iwl_cmd_opcode(id); 24439bdb17eSSharon Dvir 24539bdb17eSSharon Dvir if (!trans->command_groups || grp >= trans->command_groups_size || 24639bdb17eSSharon Dvir !trans->command_groups[grp].arr) 24739bdb17eSSharon Dvir return "UNKNOWN"; 24839bdb17eSSharon Dvir 24939bdb17eSSharon Dvir arr = &trans->command_groups[grp]; 25039bdb17eSSharon Dvir ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp); 25139bdb17eSSharon Dvir if (!ret) 25239bdb17eSSharon Dvir return "UNKNOWN"; 25339bdb17eSSharon Dvir return ret->cmd_name; 25439bdb17eSSharon Dvir } 25539bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string); 25639bdb17eSSharon Dvir 25739bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans) 25839bdb17eSSharon Dvir { 25939bdb17eSSharon Dvir int i, j; 26039bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 26139bdb17eSSharon Dvir 26239bdb17eSSharon Dvir for (i = 0; i < trans->command_groups_size; i++) { 26339bdb17eSSharon Dvir arr = &trans->command_groups[i]; 26439bdb17eSSharon Dvir if (!arr->arr) 26539bdb17eSSharon Dvir continue; 26639bdb17eSSharon Dvir for (j = 0; j < arr->size - 1; j++) 26739bdb17eSSharon Dvir if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id) 26839bdb17eSSharon Dvir return -1; 26939bdb17eSSharon Dvir } 27039bdb17eSSharon Dvir return 0; 27139bdb17eSSharon Dvir } 27239bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted); 273