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