1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2015 Intel Mobile Communications GmbH
4  * Copyright (C) 2016-2017 Intel Deutschland GmbH
5  * Copyright (C) 2019-2020 Intel Corporation
6  */
7 #include <linux/kernel.h>
8 #include <linux/bsearch.h>
9 
10 #include "fw/api/tx.h"
11 #include "iwl-trans.h"
12 #include "iwl-drv.h"
13 #include "iwl-fh.h"
14 #include "queue/tx.h"
15 #include <linux/dmapool.h>
16 
17 struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
18 				  struct device *dev,
19 				  const struct iwl_trans_ops *ops,
20 				  const struct iwl_cfg_trans_params *cfg_trans)
21 {
22 	struct iwl_trans *trans;
23 	int txcmd_size, txcmd_align;
24 #ifdef CONFIG_LOCKDEP
25 	static struct lock_class_key __key;
26 #endif
27 
28 	trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL);
29 	if (!trans)
30 		return NULL;
31 
32 	trans->trans_cfg = cfg_trans;
33 	if (!cfg_trans->gen2) {
34 		txcmd_size = sizeof(struct iwl_tx_cmd);
35 		txcmd_align = sizeof(void *);
36 	} else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) {
37 		txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
38 		txcmd_align = 64;
39 	} else {
40 		txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
41 		txcmd_align = 128;
42 	}
43 
44 	txcmd_size += sizeof(struct iwl_cmd_header);
45 	txcmd_size += 36; /* biggest possible 802.11 header */
46 
47 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
48 	if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align))
49 		return ERR_PTR(-EINVAL);
50 
51 #ifdef CONFIG_LOCKDEP
52 	lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
53 			 &__key, 0);
54 #endif
55 
56 	trans->dev = dev;
57 	trans->ops = ops;
58 	trans->num_rx_queues = 1;
59 
60 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
61 		trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl);
62 	else
63 		trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
64 	/*
65 	 * For gen2 devices, we use a single allocation for each byte-count
66 	 * table, but they're pretty small (1k) so use a DMA pool that we
67 	 * allocate here.
68 	 */
69 	if (trans->trans_cfg->gen2) {
70 		trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev,
71 						       trans->txqs.bc_tbl_size,
72 						       256, 0);
73 		if (!trans->txqs.bc_pool)
74 			return NULL;
75 	}
76 
77 	if (trans->trans_cfg->use_tfh) {
78 		trans->txqs.tfd.addr_size = 64;
79 		trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
80 		trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
81 	} else {
82 		trans->txqs.tfd.addr_size = 36;
83 		trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
84 		trans->txqs.tfd.size = sizeof(struct iwl_tfd);
85 	}
86 	trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
87 
88 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
89 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
90 	trans->dev_cmd_pool =
91 		kmem_cache_create(trans->dev_cmd_pool_name,
92 				  txcmd_size, txcmd_align,
93 				  SLAB_HWCACHE_ALIGN, NULL);
94 	if (!trans->dev_cmd_pool)
95 		return NULL;
96 
97 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
98 
99 	trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
100 	if (!trans->txqs.tso_hdr_page) {
101 		kmem_cache_destroy(trans->dev_cmd_pool);
102 		return NULL;
103 	}
104 
105 	return trans;
106 }
107 
108 void iwl_trans_free(struct iwl_trans *trans)
109 {
110 	int i;
111 
112 	for_each_possible_cpu(i) {
113 		struct iwl_tso_hdr_page *p =
114 			per_cpu_ptr(trans->txqs.tso_hdr_page, i);
115 
116 		if (p->page)
117 			__free_page(p->page);
118 	}
119 
120 	free_percpu(trans->txqs.tso_hdr_page);
121 
122 	kmem_cache_destroy(trans->dev_cmd_pool);
123 }
124 
125 int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
126 {
127 	int ret;
128 
129 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
130 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
131 		return -ERFKILL;
132 
133 	/*
134 	 * We can't test IWL_MVM_STATUS_IN_D3 in mvm->status because this
135 	 * bit is set early in the D3 flow, before we send all the commands
136 	 * that configure the firmware for D3 operation (power, patterns, ...)
137 	 * and we don't want to flag all those with CMD_SEND_IN_D3.
138 	 * So use the system_pm_mode instead. The only command sent after
139 	 * we set system_pm_mode is D3_CONFIG_CMD, which we now flag with
140 	 * CMD_SEND_IN_D3.
141 	 */
142 	if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 &&
143 		     !(cmd->flags & CMD_SEND_IN_D3)))
144 		return -EHOSTDOWN;
145 
146 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
147 		return -EIO;
148 
149 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
150 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
151 		return -EIO;
152 	}
153 
154 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
155 		    !(cmd->flags & CMD_ASYNC)))
156 		return -EINVAL;
157 
158 	if (!(cmd->flags & CMD_ASYNC))
159 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
160 
161 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
162 		cmd->id = DEF_ID(cmd->id);
163 
164 	ret = trans->ops->send_cmd(trans, cmd);
165 
166 	if (!(cmd->flags & CMD_ASYNC))
167 		lock_map_release(&trans->sync_cmd_lockdep_map);
168 
169 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
170 		return -EIO;
171 
172 	return ret;
173 }
174 IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
175 
176 /* Comparator for struct iwl_hcmd_names.
177  * Used in the binary search over a list of host commands.
178  *
179  * @key: command_id that we're looking for.
180  * @elt: struct iwl_hcmd_names candidate for match.
181  *
182  * @return 0 iff equal.
183  */
184 static int iwl_hcmd_names_cmp(const void *key, const void *elt)
185 {
186 	const struct iwl_hcmd_names *name = elt;
187 	u8 cmd1 = *(u8 *)key;
188 	u8 cmd2 = name->cmd_id;
189 
190 	return (cmd1 - cmd2);
191 }
192 
193 const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
194 {
195 	u8 grp, cmd;
196 	struct iwl_hcmd_names *ret;
197 	const struct iwl_hcmd_arr *arr;
198 	size_t size = sizeof(struct iwl_hcmd_names);
199 
200 	grp = iwl_cmd_groupid(id);
201 	cmd = iwl_cmd_opcode(id);
202 
203 	if (!trans->command_groups || grp >= trans->command_groups_size ||
204 	    !trans->command_groups[grp].arr)
205 		return "UNKNOWN";
206 
207 	arr = &trans->command_groups[grp];
208 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
209 	if (!ret)
210 		return "UNKNOWN";
211 	return ret->cmd_name;
212 }
213 IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
214 
215 int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
216 {
217 	int i, j;
218 	const struct iwl_hcmd_arr *arr;
219 
220 	for (i = 0; i < trans->command_groups_size; i++) {
221 		arr = &trans->command_groups[i];
222 		if (!arr->arr)
223 			continue;
224 		for (j = 0; j < arr->size - 1; j++)
225 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
226 				return -1;
227 	}
228 	return 0;
229 }
230 IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
231