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