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