• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "queue/tx.h"
70 #include <linux/dmapool.h>
71 
iwl_trans_alloc(unsigned int priv_size,struct device * dev,const struct iwl_trans_ops * ops,const struct iwl_cfg_trans_params * cfg_trans)72 struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
73 				  struct device *dev,
74 				  const struct iwl_trans_ops *ops,
75 				  const struct iwl_cfg_trans_params *cfg_trans)
76 {
77 	struct iwl_trans *trans;
78 	int txcmd_size, txcmd_align;
79 #ifdef CONFIG_LOCKDEP
80 	static struct lock_class_key __key;
81 #endif
82 
83 	trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL);
84 	if (!trans)
85 		return NULL;
86 
87 	trans->trans_cfg = cfg_trans;
88 	if (!cfg_trans->gen2) {
89 		txcmd_size = sizeof(struct iwl_tx_cmd);
90 		txcmd_align = sizeof(void *);
91 	} else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) {
92 		txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
93 		txcmd_align = 64;
94 	} else {
95 		txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
96 		txcmd_align = 128;
97 	}
98 
99 	txcmd_size += sizeof(struct iwl_cmd_header);
100 	txcmd_size += 36; /* biggest possible 802.11 header */
101 
102 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
103 	if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align))
104 		return ERR_PTR(-EINVAL);
105 
106 #ifdef CONFIG_LOCKDEP
107 	lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
108 			 &__key, 0);
109 #endif
110 
111 	trans->dev = dev;
112 	trans->ops = ops;
113 	trans->num_rx_queues = 1;
114 
115 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
116 		trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl);
117 	else
118 		trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
119 	/*
120 	 * For gen2 devices, we use a single allocation for each byte-count
121 	 * table, but they're pretty small (1k) so use a DMA pool that we
122 	 * allocate here.
123 	 */
124 	if (trans->trans_cfg->gen2) {
125 		trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev,
126 						       trans->txqs.bc_tbl_size,
127 						       256, 0);
128 		if (!trans->txqs.bc_pool)
129 			return NULL;
130 	}
131 
132 	if (trans->trans_cfg->use_tfh) {
133 		trans->txqs.tfd.addr_size = 64;
134 		trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
135 		trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
136 	} else {
137 		trans->txqs.tfd.addr_size = 36;
138 		trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
139 		trans->txqs.tfd.size = sizeof(struct iwl_tfd);
140 	}
141 	trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
142 
143 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
144 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
145 	trans->dev_cmd_pool =
146 		kmem_cache_create(trans->dev_cmd_pool_name,
147 				  txcmd_size, txcmd_align,
148 				  SLAB_HWCACHE_ALIGN, NULL);
149 	if (!trans->dev_cmd_pool)
150 		return NULL;
151 
152 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
153 
154 	trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
155 	if (!trans->txqs.tso_hdr_page) {
156 		kmem_cache_destroy(trans->dev_cmd_pool);
157 		return NULL;
158 	}
159 
160 	return trans;
161 }
162 
iwl_trans_free(struct iwl_trans * trans)163 void iwl_trans_free(struct iwl_trans *trans)
164 {
165 	int i;
166 
167 	for_each_possible_cpu(i) {
168 		struct iwl_tso_hdr_page *p =
169 			per_cpu_ptr(trans->txqs.tso_hdr_page, i);
170 
171 		if (p->page)
172 			__free_page(p->page);
173 	}
174 
175 	free_percpu(trans->txqs.tso_hdr_page);
176 
177 	kmem_cache_destroy(trans->dev_cmd_pool);
178 }
179 
iwl_trans_send_cmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)180 int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
181 {
182 	int ret;
183 
184 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
185 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
186 		return -ERFKILL;
187 
188 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
189 		return -EIO;
190 
191 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
192 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
193 		return -EIO;
194 	}
195 
196 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
197 		    !(cmd->flags & CMD_ASYNC)))
198 		return -EINVAL;
199 
200 	if (!(cmd->flags & CMD_ASYNC))
201 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
202 
203 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
204 		cmd->id = DEF_ID(cmd->id);
205 
206 	ret = trans->ops->send_cmd(trans, cmd);
207 
208 	if (!(cmd->flags & CMD_ASYNC))
209 		lock_map_release(&trans->sync_cmd_lockdep_map);
210 
211 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
212 		return -EIO;
213 
214 	return ret;
215 }
216 IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
217 
218 /* Comparator for struct iwl_hcmd_names.
219  * Used in the binary search over a list of host commands.
220  *
221  * @key: command_id that we're looking for.
222  * @elt: struct iwl_hcmd_names candidate for match.
223  *
224  * @return 0 iff equal.
225  */
iwl_hcmd_names_cmp(const void * key,const void * elt)226 static int iwl_hcmd_names_cmp(const void *key, const void *elt)
227 {
228 	const struct iwl_hcmd_names *name = elt;
229 	u8 cmd1 = *(u8 *)key;
230 	u8 cmd2 = name->cmd_id;
231 
232 	return (cmd1 - cmd2);
233 }
234 
iwl_get_cmd_string(struct iwl_trans * trans,u32 id)235 const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
236 {
237 	u8 grp, cmd;
238 	struct iwl_hcmd_names *ret;
239 	const struct iwl_hcmd_arr *arr;
240 	size_t size = sizeof(struct iwl_hcmd_names);
241 
242 	grp = iwl_cmd_groupid(id);
243 	cmd = iwl_cmd_opcode(id);
244 
245 	if (!trans->command_groups || grp >= trans->command_groups_size ||
246 	    !trans->command_groups[grp].arr)
247 		return "UNKNOWN";
248 
249 	arr = &trans->command_groups[grp];
250 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
251 	if (!ret)
252 		return "UNKNOWN";
253 	return ret->cmd_name;
254 }
255 IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
256 
iwl_cmd_groups_verify_sorted(const struct iwl_trans_config * trans)257 int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
258 {
259 	int i, j;
260 	const struct iwl_hcmd_arr *arr;
261 
262 	for (i = 0; i < trans->command_groups_size; i++) {
263 		arr = &trans->command_groups[i];
264 		if (!arr->arr)
265 			continue;
266 		for (j = 0; j < arr->size - 1; j++)
267 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
268 				return -1;
269 	}
270 	return 0;
271 }
272 IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
273