1 /****************************************************************************** 2 * 3 * This file is provided under a dual BSD/GPLv2 license. When using or 4 * redistributing this file, you may do so under either license. 5 * 6 * GPL LICENSE SUMMARY 7 * 8 * Copyright(c) 2015 Intel Mobile Communications GmbH 9 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of version 2 of the GNU General Public License as 13 * published by the Free Software Foundation. 14 * 15 * This program is distributed in the hope that it will be useful, but 16 * WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 23 * USA 24 * 25 * The full GNU General Public License is included in this distribution 26 * in the file called COPYING. 27 * 28 * Contact Information: 29 * Intel Linux Wireless <linuxwifi@intel.com> 30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 31 * 32 * BSD LICENSE 33 * 34 * Copyright(c) 2015 Intel Mobile Communications GmbH 35 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 42 * * Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * * Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in 46 * the documentation and/or other materials provided with the 47 * distribution. 48 * * Neither the name Intel Corporation nor the names of its 49 * contributors may be used to endorse or promote products derived 50 * from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63 * 64 *****************************************************************************/ 65 #include <linux/kernel.h> 66 #include <linux/bsearch.h> 67 68 #include "iwl-trans.h" 69 #include "iwl-drv.h" 70 #include "iwl-fh.h" 71 72 struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 73 struct device *dev, 74 const struct iwl_cfg *cfg, 75 const struct iwl_trans_ops *ops) 76 { 77 struct iwl_trans *trans; 78 #ifdef CONFIG_LOCKDEP 79 static struct lock_class_key __key; 80 #endif 81 82 trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL); 83 if (!trans) 84 return NULL; 85 86 #ifdef CONFIG_LOCKDEP 87 lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 88 &__key, 0); 89 #endif 90 91 trans->dev = dev; 92 trans->cfg = cfg; 93 trans->ops = ops; 94 trans->num_rx_queues = 1; 95 96 snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 97 "iwl_cmd_pool:%s", dev_name(trans->dev)); 98 trans->dev_cmd_pool = 99 kmem_cache_create(trans->dev_cmd_pool_name, 100 sizeof(struct iwl_device_cmd), 101 sizeof(void *), 102 SLAB_HWCACHE_ALIGN, 103 NULL); 104 if (!trans->dev_cmd_pool) 105 return NULL; 106 107 WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty); 108 109 return trans; 110 } 111 112 void iwl_trans_free(struct iwl_trans *trans) 113 { 114 kmem_cache_destroy(trans->dev_cmd_pool); 115 } 116 117 int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 118 { 119 int ret; 120 121 if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 122 test_bit(STATUS_RFKILL_OPMODE, &trans->status))) 123 return -ERFKILL; 124 125 if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 126 return -EIO; 127 128 if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 129 IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 130 return -EIO; 131 } 132 133 if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 134 !(cmd->flags & CMD_ASYNC))) 135 return -EINVAL; 136 137 if (!(cmd->flags & CMD_ASYNC)) 138 lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 139 140 if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) 141 cmd->id = DEF_ID(cmd->id); 142 143 ret = trans->ops->send_cmd(trans, cmd); 144 145 if (!(cmd->flags & CMD_ASYNC)) 146 lock_map_release(&trans->sync_cmd_lockdep_map); 147 148 if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt)) 149 return -EIO; 150 151 return ret; 152 } 153 IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 154 155 /* Comparator for struct iwl_hcmd_names. 156 * Used in the binary search over a list of host commands. 157 * 158 * @key: command_id that we're looking for. 159 * @elt: struct iwl_hcmd_names candidate for match. 160 * 161 * @return 0 iff equal. 162 */ 163 static int iwl_hcmd_names_cmp(const void *key, const void *elt) 164 { 165 const struct iwl_hcmd_names *name = elt; 166 u8 cmd1 = *(u8 *)key; 167 u8 cmd2 = name->cmd_id; 168 169 return (cmd1 - cmd2); 170 } 171 172 const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id) 173 { 174 u8 grp, cmd; 175 struct iwl_hcmd_names *ret; 176 const struct iwl_hcmd_arr *arr; 177 size_t size = sizeof(struct iwl_hcmd_names); 178 179 grp = iwl_cmd_groupid(id); 180 cmd = iwl_cmd_opcode(id); 181 182 if (!trans->command_groups || grp >= trans->command_groups_size || 183 !trans->command_groups[grp].arr) 184 return "UNKNOWN"; 185 186 arr = &trans->command_groups[grp]; 187 ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp); 188 if (!ret) 189 return "UNKNOWN"; 190 return ret->cmd_name; 191 } 192 IWL_EXPORT_SYMBOL(iwl_get_cmd_string); 193 194 int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans) 195 { 196 int i, j; 197 const struct iwl_hcmd_arr *arr; 198 199 for (i = 0; i < trans->command_groups_size; i++) { 200 arr = &trans->command_groups[i]; 201 if (!arr->arr) 202 continue; 203 for (j = 0; j < arr->size - 1; j++) 204 if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id) 205 return -1; 206 } 207 return 0; 208 } 209 IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted); 210 211 void iwl_trans_ref(struct iwl_trans *trans) 212 { 213 if (trans->ops->ref) 214 trans->ops->ref(trans); 215 } 216 IWL_EXPORT_SYMBOL(iwl_trans_ref); 217 218 void iwl_trans_unref(struct iwl_trans *trans) 219 { 220 if (trans->ops->unref) 221 trans->ops->unref(trans); 222 } 223 IWL_EXPORT_SYMBOL(iwl_trans_unref); 224