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 	}
130885375d0SMordechay Goodstein 
131885375d0SMordechay Goodstein 	if (trans->trans_cfg->use_tfh) {
132885375d0SMordechay Goodstein 		trans->txqs.tfd.addr_size = 64;
133885375d0SMordechay Goodstein 		trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
134885375d0SMordechay Goodstein 		trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
135885375d0SMordechay Goodstein 	} else {
136885375d0SMordechay Goodstein 		trans->txqs.tfd.addr_size = 36;
137885375d0SMordechay Goodstein 		trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
138885375d0SMordechay Goodstein 		trans->txqs.tfd.size = sizeof(struct iwl_tfd);
139885375d0SMordechay Goodstein 	}
140885375d0SMordechay Goodstein 	trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
141885375d0SMordechay Goodstein 
142e705c121SKalle Valo 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
143e705c121SKalle Valo 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
144e705c121SKalle Valo 	trans->dev_cmd_pool =
145e705c121SKalle Valo 		kmem_cache_create(trans->dev_cmd_pool_name,
146fda1bd0dSMordechay Goodstein 				  txcmd_size, txcmd_align,
147a89c72ffSJohannes Berg 				  SLAB_HWCACHE_ALIGN, NULL);
148e705c121SKalle Valo 	if (!trans->dev_cmd_pool)
1495a41a86cSSharon Dvir 		return NULL;
150e705c121SKalle Valo 
151d6d517b7SSara Sharon 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
152d6d517b7SSara Sharon 
153e705c121SKalle Valo 	return trans;
154e705c121SKalle Valo }
155e705c121SKalle Valo 
156e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans)
157e705c121SKalle Valo {
158e705c121SKalle Valo 	kmem_cache_destroy(trans->dev_cmd_pool);
159e705c121SKalle Valo }
16092fe8343SEmmanuel Grumbach 
16192fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
16292fe8343SEmmanuel Grumbach {
16392fe8343SEmmanuel Grumbach 	int ret;
16492fe8343SEmmanuel Grumbach 
16592fe8343SEmmanuel Grumbach 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
166326477e4SJohannes Berg 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
16792fe8343SEmmanuel Grumbach 		return -ERFKILL;
16892fe8343SEmmanuel Grumbach 
16992fe8343SEmmanuel Grumbach 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
17092fe8343SEmmanuel Grumbach 		return -EIO;
17192fe8343SEmmanuel Grumbach 
17292fe8343SEmmanuel Grumbach 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
17392fe8343SEmmanuel Grumbach 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
17492fe8343SEmmanuel Grumbach 		return -EIO;
17592fe8343SEmmanuel Grumbach 	}
17692fe8343SEmmanuel Grumbach 
17792fe8343SEmmanuel Grumbach 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
17892fe8343SEmmanuel Grumbach 		    !(cmd->flags & CMD_ASYNC)))
17992fe8343SEmmanuel Grumbach 		return -EINVAL;
18092fe8343SEmmanuel Grumbach 
18192fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
18292fe8343SEmmanuel Grumbach 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
18392fe8343SEmmanuel Grumbach 
1845b88792cSSara Sharon 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
1855b88792cSSara Sharon 		cmd->id = DEF_ID(cmd->id);
1865b88792cSSara Sharon 
18792fe8343SEmmanuel Grumbach 	ret = trans->ops->send_cmd(trans, cmd);
18892fe8343SEmmanuel Grumbach 
18992fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
19092fe8343SEmmanuel Grumbach 		lock_map_release(&trans->sync_cmd_lockdep_map);
19192fe8343SEmmanuel Grumbach 
1920ec971fdSJohannes Berg 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
1930ec971fdSJohannes Berg 		return -EIO;
1940ec971fdSJohannes Berg 
19592fe8343SEmmanuel Grumbach 	return ret;
19692fe8343SEmmanuel Grumbach }
19792fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
19839bdb17eSSharon Dvir 
19939bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names.
20039bdb17eSSharon Dvir  * Used in the binary search over a list of host commands.
20139bdb17eSSharon Dvir  *
20239bdb17eSSharon Dvir  * @key: command_id that we're looking for.
20339bdb17eSSharon Dvir  * @elt: struct iwl_hcmd_names candidate for match.
20439bdb17eSSharon Dvir  *
20539bdb17eSSharon Dvir  * @return 0 iff equal.
20639bdb17eSSharon Dvir  */
20739bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt)
20839bdb17eSSharon Dvir {
20939bdb17eSSharon Dvir 	const struct iwl_hcmd_names *name = elt;
21039bdb17eSSharon Dvir 	u8 cmd1 = *(u8 *)key;
21139bdb17eSSharon Dvir 	u8 cmd2 = name->cmd_id;
21239bdb17eSSharon Dvir 
21339bdb17eSSharon Dvir 	return (cmd1 - cmd2);
21439bdb17eSSharon Dvir }
21539bdb17eSSharon Dvir 
21639bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
21739bdb17eSSharon Dvir {
21839bdb17eSSharon Dvir 	u8 grp, cmd;
21939bdb17eSSharon Dvir 	struct iwl_hcmd_names *ret;
22039bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
22139bdb17eSSharon Dvir 	size_t size = sizeof(struct iwl_hcmd_names);
22239bdb17eSSharon Dvir 
22339bdb17eSSharon Dvir 	grp = iwl_cmd_groupid(id);
22439bdb17eSSharon Dvir 	cmd = iwl_cmd_opcode(id);
22539bdb17eSSharon Dvir 
22639bdb17eSSharon Dvir 	if (!trans->command_groups || grp >= trans->command_groups_size ||
22739bdb17eSSharon Dvir 	    !trans->command_groups[grp].arr)
22839bdb17eSSharon Dvir 		return "UNKNOWN";
22939bdb17eSSharon Dvir 
23039bdb17eSSharon Dvir 	arr = &trans->command_groups[grp];
23139bdb17eSSharon Dvir 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
23239bdb17eSSharon Dvir 	if (!ret)
23339bdb17eSSharon Dvir 		return "UNKNOWN";
23439bdb17eSSharon Dvir 	return ret->cmd_name;
23539bdb17eSSharon Dvir }
23639bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
23739bdb17eSSharon Dvir 
23839bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
23939bdb17eSSharon Dvir {
24039bdb17eSSharon Dvir 	int i, j;
24139bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
24239bdb17eSSharon Dvir 
24339bdb17eSSharon Dvir 	for (i = 0; i < trans->command_groups_size; i++) {
24439bdb17eSSharon Dvir 		arr = &trans->command_groups[i];
24539bdb17eSSharon Dvir 		if (!arr->arr)
24639bdb17eSSharon Dvir 			continue;
24739bdb17eSSharon Dvir 		for (j = 0; j < arr->size - 1; j++)
24839bdb17eSSharon Dvir 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
24939bdb17eSSharon Dvir 				return -1;
25039bdb17eSSharon Dvir 	}
25139bdb17eSSharon Dvir 	return 0;
25239bdb17eSSharon Dvir }
25339bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
254