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  * Copyright(c) 2019 - 2020 Intel Corporation
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called COPYING.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <linuxwifi@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  * BSD LICENSE
29  *
30  * Copyright(c) 2015 Intel Mobile Communications GmbH
31  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
32  * Copyright(c) 2019 - 2020 Intel Corporation
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  *  * Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *  * Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *  * Neither the name Intel Corporation nor the names of its
46  *    contributors may be used to endorse or promote products derived
47  *    from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  *****************************************************************************/
62 #include <linux/kernel.h>
63 #include <linux/bsearch.h>
64 
65 #include "fw/api/tx.h"
66 #include "iwl-trans.h"
67 #include "iwl-drv.h"
68 #include "iwl-fh.h"
69 #include <linux/dmapool.h>
70 
71 struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
72 				  struct device *dev,
73 				  const struct iwl_trans_ops *ops,
74 				  const struct iwl_cfg_trans_params *cfg_trans)
75 {
76 	struct iwl_trans *trans;
77 	int txcmd_size, txcmd_align;
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 	trans->trans_cfg = cfg_trans;
87 	if (!cfg_trans->gen2) {
88 		txcmd_size = sizeof(struct iwl_tx_cmd);
89 		txcmd_align = sizeof(void *);
90 	} else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) {
91 		txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
92 		txcmd_align = 64;
93 	} else {
94 		txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
95 		txcmd_align = 128;
96 	}
97 
98 	txcmd_size += sizeof(struct iwl_cmd_header);
99 	txcmd_size += 36; /* biggest possible 802.11 header */
100 
101 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
102 	if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align))
103 		return ERR_PTR(-EINVAL);
104 
105 #ifdef CONFIG_LOCKDEP
106 	lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
107 			 &__key, 0);
108 #endif
109 
110 	trans->dev = dev;
111 	trans->ops = ops;
112 	trans->num_rx_queues = 1;
113 
114 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
115 		trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl);
116 	else
117 		trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
118 	/*
119 	 * For gen2 devices, we use a single allocation for each byte-count
120 	 * table, but they're pretty small (1k) so use a DMA pool that we
121 	 * allocate here.
122 	 */
123 	if (trans->trans_cfg->gen2) {
124 		trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev,
125 						       trans->txqs.bc_tbl_size,
126 						       256, 0);
127 		if (!trans->txqs.bc_pool)
128 			return NULL;
129 	}
130 
131 	if (trans->trans_cfg->use_tfh) {
132 		trans->txqs.tfd.addr_size = 64;
133 		trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
134 		trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
135 	} else {
136 		trans->txqs.tfd.addr_size = 36;
137 		trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
138 		trans->txqs.tfd.size = sizeof(struct iwl_tfd);
139 	}
140 	trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
141 
142 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
143 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
144 	trans->dev_cmd_pool =
145 		kmem_cache_create(trans->dev_cmd_pool_name,
146 				  txcmd_size, txcmd_align,
147 				  SLAB_HWCACHE_ALIGN, NULL);
148 	if (!trans->dev_cmd_pool)
149 		return NULL;
150 
151 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
152 
153 	return trans;
154 }
155 
156 void iwl_trans_free(struct iwl_trans *trans)
157 {
158 	kmem_cache_destroy(trans->dev_cmd_pool);
159 }
160 
161 int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
162 {
163 	int ret;
164 
165 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
166 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
167 		return -ERFKILL;
168 
169 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
170 		return -EIO;
171 
172 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
173 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
174 		return -EIO;
175 	}
176 
177 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
178 		    !(cmd->flags & CMD_ASYNC)))
179 		return -EINVAL;
180 
181 	if (!(cmd->flags & CMD_ASYNC))
182 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
183 
184 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
185 		cmd->id = DEF_ID(cmd->id);
186 
187 	ret = trans->ops->send_cmd(trans, cmd);
188 
189 	if (!(cmd->flags & CMD_ASYNC))
190 		lock_map_release(&trans->sync_cmd_lockdep_map);
191 
192 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
193 		return -EIO;
194 
195 	return ret;
196 }
197 IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
198 
199 /* Comparator for struct iwl_hcmd_names.
200  * Used in the binary search over a list of host commands.
201  *
202  * @key: command_id that we're looking for.
203  * @elt: struct iwl_hcmd_names candidate for match.
204  *
205  * @return 0 iff equal.
206  */
207 static int iwl_hcmd_names_cmp(const void *key, const void *elt)
208 {
209 	const struct iwl_hcmd_names *name = elt;
210 	u8 cmd1 = *(u8 *)key;
211 	u8 cmd2 = name->cmd_id;
212 
213 	return (cmd1 - cmd2);
214 }
215 
216 const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
217 {
218 	u8 grp, cmd;
219 	struct iwl_hcmd_names *ret;
220 	const struct iwl_hcmd_arr *arr;
221 	size_t size = sizeof(struct iwl_hcmd_names);
222 
223 	grp = iwl_cmd_groupid(id);
224 	cmd = iwl_cmd_opcode(id);
225 
226 	if (!trans->command_groups || grp >= trans->command_groups_size ||
227 	    !trans->command_groups[grp].arr)
228 		return "UNKNOWN";
229 
230 	arr = &trans->command_groups[grp];
231 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
232 	if (!ret)
233 		return "UNKNOWN";
234 	return ret->cmd_name;
235 }
236 IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
237 
238 int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
239 {
240 	int i, j;
241 	const struct iwl_hcmd_arr *arr;
242 
243 	for (i = 0; i < trans->command_groups_size; i++) {
244 		arr = &trans->command_groups[i];
245 		if (!arr->arr)
246 			continue;
247 		for (j = 0; j < arr->size - 1; j++)
248 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
249 				return -1;
250 	}
251 	return 0;
252 }
253 IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
254