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"
69a26014e2SMordechay Goodstein #include <linux/dmapool.h>
70e705c121SKalle Valo 
71e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
72e705c121SKalle Valo 				  struct device *dev,
73a89c72ffSJohannes Berg 				  const struct iwl_trans_ops *ops,
74fda1bd0dSMordechay Goodstein 				  const struct iwl_cfg_trans_params *cfg_trans)
75e705c121SKalle Valo {
76e705c121SKalle Valo 	struct iwl_trans *trans;
77fda1bd0dSMordechay Goodstein 	int txcmd_size, txcmd_align;
78e705c121SKalle Valo #ifdef CONFIG_LOCKDEP
79e705c121SKalle Valo 	static struct lock_class_key __key;
80e705c121SKalle Valo #endif
81e705c121SKalle Valo 
825a41a86cSSharon Dvir 	trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL);
83e705c121SKalle Valo 	if (!trans)
84e705c121SKalle Valo 		return NULL;
85e705c121SKalle Valo 
86fda1bd0dSMordechay Goodstein 	trans->trans_cfg = cfg_trans;
87fda1bd0dSMordechay Goodstein 	if (!cfg_trans->gen2) {
88fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd);
89fda1bd0dSMordechay Goodstein 		txcmd_align = sizeof(void *);
90fda1bd0dSMordechay Goodstein 	} else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) {
91fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
92fda1bd0dSMordechay Goodstein 		txcmd_align = 64;
93fda1bd0dSMordechay Goodstein 	} else {
94fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
95fda1bd0dSMordechay Goodstein 		txcmd_align = 128;
96fda1bd0dSMordechay Goodstein 	}
97fda1bd0dSMordechay Goodstein 
98fda1bd0dSMordechay Goodstein 	txcmd_size += sizeof(struct iwl_cmd_header);
99fda1bd0dSMordechay Goodstein 	txcmd_size += 36; /* biggest possible 802.11 header */
100fda1bd0dSMordechay Goodstein 
101fda1bd0dSMordechay Goodstein 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
102fda1bd0dSMordechay Goodstein 	if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align))
103fda1bd0dSMordechay Goodstein 		return ERR_PTR(-EINVAL);
104fda1bd0dSMordechay Goodstein 
105e705c121SKalle Valo #ifdef CONFIG_LOCKDEP
106e705c121SKalle Valo 	lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
107e705c121SKalle Valo 			 &__key, 0);
108e705c121SKalle Valo #endif
109e705c121SKalle Valo 
110e705c121SKalle Valo 	trans->dev = dev;
111e705c121SKalle Valo 	trans->ops = ops;
112e705c121SKalle Valo 	trans->num_rx_queues = 1;
113e705c121SKalle Valo 
114a26014e2SMordechay Goodstein 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
115a26014e2SMordechay Goodstein 		trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl);
116a26014e2SMordechay Goodstein 	else
117a26014e2SMordechay Goodstein 		trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
118a26014e2SMordechay Goodstein 	/*
119a26014e2SMordechay Goodstein 	 * For gen2 devices, we use a single allocation for each byte-count
120a26014e2SMordechay Goodstein 	 * table, but they're pretty small (1k) so use a DMA pool that we
121a26014e2SMordechay Goodstein 	 * allocate here.
122a26014e2SMordechay Goodstein 	 */
123a26014e2SMordechay Goodstein 	if (trans->trans_cfg->gen2) {
124a26014e2SMordechay Goodstein 		trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev,
125a26014e2SMordechay Goodstein 						       trans->txqs.bc_tbl_size,
126a26014e2SMordechay Goodstein 						       256, 0);
127a26014e2SMordechay Goodstein 		if (!trans->txqs.bc_pool)
128a26014e2SMordechay Goodstein 			return NULL;
129a26014e2SMordechay Goodstein 	}
130e705c121SKalle Valo 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
131e705c121SKalle Valo 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
132e705c121SKalle Valo 	trans->dev_cmd_pool =
133e705c121SKalle Valo 		kmem_cache_create(trans->dev_cmd_pool_name,
134fda1bd0dSMordechay Goodstein 				  txcmd_size, txcmd_align,
135a89c72ffSJohannes Berg 				  SLAB_HWCACHE_ALIGN, NULL);
136e705c121SKalle Valo 	if (!trans->dev_cmd_pool)
1375a41a86cSSharon Dvir 		return NULL;
138e705c121SKalle Valo 
139d6d517b7SSara Sharon 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
140d6d517b7SSara Sharon 
141e705c121SKalle Valo 	return trans;
142e705c121SKalle Valo }
143e705c121SKalle Valo 
144e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans)
145e705c121SKalle Valo {
146e705c121SKalle Valo 	kmem_cache_destroy(trans->dev_cmd_pool);
147e705c121SKalle Valo }
14892fe8343SEmmanuel Grumbach 
14992fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
15092fe8343SEmmanuel Grumbach {
15192fe8343SEmmanuel Grumbach 	int ret;
15292fe8343SEmmanuel Grumbach 
15392fe8343SEmmanuel Grumbach 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
154326477e4SJohannes Berg 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
15592fe8343SEmmanuel Grumbach 		return -ERFKILL;
15692fe8343SEmmanuel Grumbach 
15792fe8343SEmmanuel Grumbach 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
15892fe8343SEmmanuel Grumbach 		return -EIO;
15992fe8343SEmmanuel Grumbach 
16092fe8343SEmmanuel Grumbach 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
16192fe8343SEmmanuel Grumbach 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
16292fe8343SEmmanuel Grumbach 		return -EIO;
16392fe8343SEmmanuel Grumbach 	}
16492fe8343SEmmanuel Grumbach 
16592fe8343SEmmanuel Grumbach 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
16692fe8343SEmmanuel Grumbach 		    !(cmd->flags & CMD_ASYNC)))
16792fe8343SEmmanuel Grumbach 		return -EINVAL;
16892fe8343SEmmanuel Grumbach 
16992fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
17092fe8343SEmmanuel Grumbach 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
17192fe8343SEmmanuel Grumbach 
1725b88792cSSara Sharon 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
1735b88792cSSara Sharon 		cmd->id = DEF_ID(cmd->id);
1745b88792cSSara Sharon 
17592fe8343SEmmanuel Grumbach 	ret = trans->ops->send_cmd(trans, cmd);
17692fe8343SEmmanuel Grumbach 
17792fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
17892fe8343SEmmanuel Grumbach 		lock_map_release(&trans->sync_cmd_lockdep_map);
17992fe8343SEmmanuel Grumbach 
1800ec971fdSJohannes Berg 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
1810ec971fdSJohannes Berg 		return -EIO;
1820ec971fdSJohannes Berg 
18392fe8343SEmmanuel Grumbach 	return ret;
18492fe8343SEmmanuel Grumbach }
18592fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
18639bdb17eSSharon Dvir 
18739bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names.
18839bdb17eSSharon Dvir  * Used in the binary search over a list of host commands.
18939bdb17eSSharon Dvir  *
19039bdb17eSSharon Dvir  * @key: command_id that we're looking for.
19139bdb17eSSharon Dvir  * @elt: struct iwl_hcmd_names candidate for match.
19239bdb17eSSharon Dvir  *
19339bdb17eSSharon Dvir  * @return 0 iff equal.
19439bdb17eSSharon Dvir  */
19539bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt)
19639bdb17eSSharon Dvir {
19739bdb17eSSharon Dvir 	const struct iwl_hcmd_names *name = elt;
19839bdb17eSSharon Dvir 	u8 cmd1 = *(u8 *)key;
19939bdb17eSSharon Dvir 	u8 cmd2 = name->cmd_id;
20039bdb17eSSharon Dvir 
20139bdb17eSSharon Dvir 	return (cmd1 - cmd2);
20239bdb17eSSharon Dvir }
20339bdb17eSSharon Dvir 
20439bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
20539bdb17eSSharon Dvir {
20639bdb17eSSharon Dvir 	u8 grp, cmd;
20739bdb17eSSharon Dvir 	struct iwl_hcmd_names *ret;
20839bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
20939bdb17eSSharon Dvir 	size_t size = sizeof(struct iwl_hcmd_names);
21039bdb17eSSharon Dvir 
21139bdb17eSSharon Dvir 	grp = iwl_cmd_groupid(id);
21239bdb17eSSharon Dvir 	cmd = iwl_cmd_opcode(id);
21339bdb17eSSharon Dvir 
21439bdb17eSSharon Dvir 	if (!trans->command_groups || grp >= trans->command_groups_size ||
21539bdb17eSSharon Dvir 	    !trans->command_groups[grp].arr)
21639bdb17eSSharon Dvir 		return "UNKNOWN";
21739bdb17eSSharon Dvir 
21839bdb17eSSharon Dvir 	arr = &trans->command_groups[grp];
21939bdb17eSSharon Dvir 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
22039bdb17eSSharon Dvir 	if (!ret)
22139bdb17eSSharon Dvir 		return "UNKNOWN";
22239bdb17eSSharon Dvir 	return ret->cmd_name;
22339bdb17eSSharon Dvir }
22439bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
22539bdb17eSSharon Dvir 
22639bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
22739bdb17eSSharon Dvir {
22839bdb17eSSharon Dvir 	int i, j;
22939bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
23039bdb17eSSharon Dvir 
23139bdb17eSSharon Dvir 	for (i = 0; i < trans->command_groups_size; i++) {
23239bdb17eSSharon Dvir 		arr = &trans->command_groups[i];
23339bdb17eSSharon Dvir 		if (!arr->arr)
23439bdb17eSSharon Dvir 			continue;
23539bdb17eSSharon Dvir 		for (j = 0; j < arr->size - 1; j++)
23639bdb17eSSharon Dvir 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
23739bdb17eSSharon Dvir 				return -1;
23839bdb17eSSharon Dvir 	}
23939bdb17eSSharon Dvir 	return 0;
24039bdb17eSSharon Dvir }
24139bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
242