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> 6492fe8343SEmmanuel Grumbach #include "iwl-drv.h" 65e705c121SKalle Valo #include "iwl-trans.h" 66e705c121SKalle Valo 67e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size, 68e705c121SKalle Valo struct device *dev, 69e705c121SKalle Valo const struct iwl_cfg *cfg, 70e705c121SKalle Valo const struct iwl_trans_ops *ops, 71e705c121SKalle Valo size_t dev_cmd_headroom) 72e705c121SKalle Valo { 73e705c121SKalle Valo struct iwl_trans *trans; 74e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 75e705c121SKalle Valo static struct lock_class_key __key; 76e705c121SKalle Valo #endif 77e705c121SKalle Valo 78e705c121SKalle Valo trans = kzalloc(sizeof(*trans) + priv_size, GFP_KERNEL); 79e705c121SKalle Valo if (!trans) 80e705c121SKalle Valo return NULL; 81e705c121SKalle Valo 82e705c121SKalle Valo #ifdef CONFIG_LOCKDEP 83e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map", 84e705c121SKalle Valo &__key, 0); 85e705c121SKalle Valo #endif 86e705c121SKalle Valo 87e705c121SKalle Valo trans->dev = dev; 88e705c121SKalle Valo trans->cfg = cfg; 89e705c121SKalle Valo trans->ops = ops; 90e705c121SKalle Valo trans->dev_cmd_headroom = dev_cmd_headroom; 91e705c121SKalle Valo trans->num_rx_queues = 1; 92e705c121SKalle Valo 93e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name), 94e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev)); 95e705c121SKalle Valo trans->dev_cmd_pool = 96e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name, 97e705c121SKalle Valo sizeof(struct iwl_device_cmd) 98e705c121SKalle Valo + trans->dev_cmd_headroom, 99e705c121SKalle Valo sizeof(void *), 100e705c121SKalle Valo SLAB_HWCACHE_ALIGN, 101e705c121SKalle Valo NULL); 102e705c121SKalle Valo if (!trans->dev_cmd_pool) 103e705c121SKalle Valo goto free; 104e705c121SKalle Valo 105e705c121SKalle Valo return trans; 106e705c121SKalle Valo free: 107e705c121SKalle Valo kfree(trans); 108e705c121SKalle Valo return NULL; 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 kfree(trans); 115e705c121SKalle Valo } 11692fe8343SEmmanuel Grumbach 11792fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) 11892fe8343SEmmanuel Grumbach { 11992fe8343SEmmanuel Grumbach int ret; 12092fe8343SEmmanuel Grumbach 12192fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) && 12292fe8343SEmmanuel Grumbach test_bit(STATUS_RFKILL, &trans->status))) 12392fe8343SEmmanuel Grumbach return -ERFKILL; 12492fe8343SEmmanuel Grumbach 12592fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status))) 12692fe8343SEmmanuel Grumbach return -EIO; 12792fe8343SEmmanuel Grumbach 12892fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) { 12992fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state); 13092fe8343SEmmanuel Grumbach return -EIO; 13192fe8343SEmmanuel Grumbach } 13292fe8343SEmmanuel Grumbach 13392fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) && 13492fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC))) 13592fe8343SEmmanuel Grumbach return -EINVAL; 13692fe8343SEmmanuel Grumbach 13792fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 13892fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map); 13992fe8343SEmmanuel Grumbach 14092fe8343SEmmanuel Grumbach ret = trans->ops->send_cmd(trans, cmd); 14192fe8343SEmmanuel Grumbach 14292fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC)) 14392fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map); 14492fe8343SEmmanuel Grumbach 14592fe8343SEmmanuel Grumbach return ret; 14692fe8343SEmmanuel Grumbach } 14792fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd); 148