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 9e705c121SKalle Valo * 10e705c121SKalle Valo * This program is free software; you can redistribute it and/or modify 11e705c121SKalle Valo * it under the terms of version 2 of the GNU General Public License as 12e705c121SKalle Valo * published by the Free Software Foundation. 13e705c121SKalle Valo * 14e705c121SKalle Valo * This program is distributed in the hope that it will be useful, but 15e705c121SKalle Valo * WITHOUT ANY WARRANTY; without even the implied warranty of 16e705c121SKalle Valo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17e705c121SKalle Valo * General Public License for more details. 18e705c121SKalle Valo * 19e705c121SKalle Valo * You should have received a copy of the GNU General Public License 20e705c121SKalle Valo * along with this program; if not, write to the Free Software 21e705c121SKalle Valo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 22e705c121SKalle Valo * USA 23e705c121SKalle Valo * 24e705c121SKalle Valo * The full GNU General Public License is included in this distribution 25e705c121SKalle Valo * in the file called COPYING. 26e705c121SKalle Valo * 27e705c121SKalle Valo * Contact Information: 28d01c5366SEmmanuel Grumbach * Intel Linux Wireless <linuxwifi@intel.com> 29e705c121SKalle Valo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 30e705c121SKalle Valo * 31e705c121SKalle Valo * BSD LICENSE 32e705c121SKalle Valo * 33e705c121SKalle Valo * Copyright(c) 2015 Intel Mobile Communications GmbH 34e705c121SKalle Valo * All rights reserved. 35e705c121SKalle Valo * 36e705c121SKalle Valo * Redistribution and use in source and binary forms, with or without 37e705c121SKalle Valo * modification, are permitted provided that the following conditions 38e705c121SKalle Valo * are met: 39e705c121SKalle Valo * 40e705c121SKalle Valo * * Redistributions of source code must retain the above copyright 41e705c121SKalle Valo * notice, this list of conditions and the following disclaimer. 42e705c121SKalle Valo * * Redistributions in binary form must reproduce the above copyright 43e705c121SKalle Valo * notice, this list of conditions and the following disclaimer in 44e705c121SKalle Valo * the documentation and/or other materials provided with the 45e705c121SKalle Valo * distribution. 46e705c121SKalle Valo * * Neither the name Intel Corporation nor the names of its 47e705c121SKalle Valo * contributors may be used to endorse or promote products derived 48e705c121SKalle Valo * from this software without specific prior written permission. 49e705c121SKalle Valo * 50e705c121SKalle Valo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51e705c121SKalle Valo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52e705c121SKalle Valo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53e705c121SKalle Valo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 54e705c121SKalle Valo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 55e705c121SKalle Valo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 56e705c121SKalle Valo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57e705c121SKalle Valo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58e705c121SKalle Valo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59e705c121SKalle Valo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 60e705c121SKalle Valo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61e705c121SKalle Valo * 62e705c121SKalle Valo *****************************************************************************/ 63e705c121SKalle Valo #include <linux/kernel.h> 6439bdb17eSSharon Dvir #include <linux/bsearch.h> 6539bdb17eSSharon Dvir 66e705c121SKalle Valo #include "iwl-trans.h" 6739bdb17eSSharon Dvir #include "iwl-drv.h" 683cd1980bSSara Sharon #include "iwl-fh.h" 69e705c121SKalle Valo 70e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 71e705c121SKalle Valo struct device *dev, 72e705c121SKalle Valo const struct iwl_cfg *cfg, 73e705c121SKalle Valo const struct iwl_trans_ops *ops, 74e705c121SKalle Valo size_t dev_cmd_headroom) 75e705c121SKalle Valo { 76e705c121SKalle Valo struct iwl_trans *trans; 77e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 78e705c121SKalle Valo static struct lock_class_key __key; 79e705c121SKalle Valo #endif 80e705c121SKalle Valo 815a41a86cSSharon Dvir trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL); 82e705c121SKalle Valo if (!trans) 83e705c121SKalle Valo return NULL; 84e705c121SKalle Valo 85e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 86e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 87e705c121SKalle Valo &__key, 0); 88e705c121SKalle Valo #endif 89e705c121SKalle Valo 90e705c121SKalle Valo trans->dev = dev; 91e705c121SKalle Valo trans->cfg = cfg; 92e705c121SKalle Valo trans->ops = ops; 93e705c121SKalle Valo trans->dev_cmd_headroom = dev_cmd_headroom; 94e705c121SKalle Valo trans->num_rx_queues = 1; 95e705c121SKalle Valo 96e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 97e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev)); 98e705c121SKalle Valo trans->dev_cmd_pool = 99e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name, 100e705c121SKalle Valo sizeof(struct iwl_device_cmd) 101e705c121SKalle Valo + trans->dev_cmd_headroom, 102e705c121SKalle Valo sizeof(void *), 103e705c121SKalle Valo SLAB_HWCACHE_ALIGN, 104e705c121SKalle Valo NULL); 105e705c121SKalle Valo if (!trans->dev_cmd_pool) 1065a41a86cSSharon Dvir return NULL; 107e705c121SKalle Valo 108e705c121SKalle Valo return trans; 109e705c121SKalle Valo } 110e705c121SKalle Valo 111e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans) 112e705c121SKalle Valo { 113e705c121SKalle Valo kmem_cache_destroy(trans->dev_cmd_pool); 114e705c121SKalle Valo } 11592fe8343SEmmanuel Grumbach 11692fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 11792fe8343SEmmanuel Grumbach { 11892fe8343SEmmanuel Grumbach int ret; 11992fe8343SEmmanuel Grumbach 12092fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 12192fe8343SEmmanuel Grumbach test_bit(STATUS_RFKILL, &trans->status))) 12292fe8343SEmmanuel Grumbach return -ERFKILL; 12392fe8343SEmmanuel Grumbach 12492fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 12592fe8343SEmmanuel Grumbach return -EIO; 12692fe8343SEmmanuel Grumbach 12792fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 12892fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 12992fe8343SEmmanuel Grumbach return -EIO; 13092fe8343SEmmanuel Grumbach } 13192fe8343SEmmanuel Grumbach 13292fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 13392fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC))) 13492fe8343SEmmanuel Grumbach return -EINVAL; 13592fe8343SEmmanuel Grumbach 13692fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 13792fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 13892fe8343SEmmanuel Grumbach 1395b88792cSSara Sharon if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) 1405b88792cSSara Sharon cmd->id = DEF_ID(cmd->id); 1415b88792cSSara Sharon 14292fe8343SEmmanuel Grumbach ret = trans->ops->send_cmd(trans, cmd); 14392fe8343SEmmanuel Grumbach 14492fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 14592fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map); 14692fe8343SEmmanuel Grumbach 14792fe8343SEmmanuel Grumbach return ret; 14892fe8343SEmmanuel Grumbach } 14992fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 15039bdb17eSSharon Dvir 15139bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names. 15239bdb17eSSharon Dvir * Used in the binary search over a list of host commands. 15339bdb17eSSharon Dvir * 15439bdb17eSSharon Dvir * @key: command_id that we're looking for. 15539bdb17eSSharon Dvir * @elt: struct iwl_hcmd_names candidate for match. 15639bdb17eSSharon Dvir * 15739bdb17eSSharon Dvir * @return 0 iff equal. 15839bdb17eSSharon Dvir */ 15939bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt) 16039bdb17eSSharon Dvir { 16139bdb17eSSharon Dvir const struct iwl_hcmd_names *name = elt; 16239bdb17eSSharon Dvir u8 cmd1 = *(u8 *)key; 16339bdb17eSSharon Dvir u8 cmd2 = name->cmd_id; 16439bdb17eSSharon Dvir 16539bdb17eSSharon Dvir return (cmd1 - cmd2); 16639bdb17eSSharon Dvir } 16739bdb17eSSharon Dvir 16839bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id) 16939bdb17eSSharon Dvir { 17039bdb17eSSharon Dvir u8 grp, cmd; 17139bdb17eSSharon Dvir struct iwl_hcmd_names *ret; 17239bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 17339bdb17eSSharon Dvir size_t size = sizeof(struct iwl_hcmd_names); 17439bdb17eSSharon Dvir 17539bdb17eSSharon Dvir grp = iwl_cmd_groupid(id); 17639bdb17eSSharon Dvir cmd = iwl_cmd_opcode(id); 17739bdb17eSSharon Dvir 17839bdb17eSSharon Dvir if (!trans->command_groups || grp >= trans->command_groups_size || 17939bdb17eSSharon Dvir !trans->command_groups[grp].arr) 18039bdb17eSSharon Dvir return "UNKNOWN"; 18139bdb17eSSharon Dvir 18239bdb17eSSharon Dvir arr = &trans->command_groups[grp]; 18339bdb17eSSharon Dvir ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp); 18439bdb17eSSharon Dvir if (!ret) 18539bdb17eSSharon Dvir return "UNKNOWN"; 18639bdb17eSSharon Dvir return ret->cmd_name; 18739bdb17eSSharon Dvir } 18839bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string); 18939bdb17eSSharon Dvir 19039bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans) 19139bdb17eSSharon Dvir { 19239bdb17eSSharon Dvir int i, j; 19339bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr; 19439bdb17eSSharon Dvir 19539bdb17eSSharon Dvir for (i = 0; i < trans->command_groups_size; i++) { 19639bdb17eSSharon Dvir arr = &trans->command_groups[i]; 19739bdb17eSSharon Dvir if (!arr->arr) 19839bdb17eSSharon Dvir continue; 19939bdb17eSSharon Dvir for (j = 0; j < arr->size - 1; j++) 20039bdb17eSSharon Dvir if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id) 20139bdb17eSSharon Dvir return -1; 20239bdb17eSSharon Dvir } 20339bdb17eSSharon Dvir return 0; 20439bdb17eSSharon Dvir } 20539bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted); 206