1/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <linux/slab.h>
19#include <linux/if_ether.h>
20
21#include "htt.h"
22#include "core.h"
23#include "debug.h"
24
25static const enum htt_t2h_msg_type htt_main_t2h_msg_types[] = {
26	[HTT_MAIN_T2H_MSG_TYPE_VERSION_CONF] = HTT_T2H_MSG_TYPE_VERSION_CONF,
27	[HTT_MAIN_T2H_MSG_TYPE_RX_IND] = HTT_T2H_MSG_TYPE_RX_IND,
28	[HTT_MAIN_T2H_MSG_TYPE_RX_FLUSH] = HTT_T2H_MSG_TYPE_RX_FLUSH,
29	[HTT_MAIN_T2H_MSG_TYPE_PEER_MAP] = HTT_T2H_MSG_TYPE_PEER_MAP,
30	[HTT_MAIN_T2H_MSG_TYPE_PEER_UNMAP] = HTT_T2H_MSG_TYPE_PEER_UNMAP,
31	[HTT_MAIN_T2H_MSG_TYPE_RX_ADDBA] = HTT_T2H_MSG_TYPE_RX_ADDBA,
32	[HTT_MAIN_T2H_MSG_TYPE_RX_DELBA] = HTT_T2H_MSG_TYPE_RX_DELBA,
33	[HTT_MAIN_T2H_MSG_TYPE_TX_COMPL_IND] = HTT_T2H_MSG_TYPE_TX_COMPL_IND,
34	[HTT_MAIN_T2H_MSG_TYPE_PKTLOG] = HTT_T2H_MSG_TYPE_PKTLOG,
35	[HTT_MAIN_T2H_MSG_TYPE_STATS_CONF] = HTT_T2H_MSG_TYPE_STATS_CONF,
36	[HTT_MAIN_T2H_MSG_TYPE_RX_FRAG_IND] = HTT_T2H_MSG_TYPE_RX_FRAG_IND,
37	[HTT_MAIN_T2H_MSG_TYPE_SEC_IND] = HTT_T2H_MSG_TYPE_SEC_IND,
38	[HTT_MAIN_T2H_MSG_TYPE_TX_INSPECT_IND] =
39		HTT_T2H_MSG_TYPE_TX_INSPECT_IND,
40	[HTT_MAIN_T2H_MSG_TYPE_MGMT_TX_COMPL_IND] =
41		HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION,
42	[HTT_MAIN_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND] =
43		HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND,
44	[HTT_MAIN_T2H_MSG_TYPE_RX_PN_IND] = HTT_T2H_MSG_TYPE_RX_PN_IND,
45	[HTT_MAIN_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND] =
46		HTT_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND,
47	[HTT_MAIN_T2H_MSG_TYPE_TEST] = HTT_T2H_MSG_TYPE_TEST,
48};
49
50static const enum htt_t2h_msg_type htt_10x_t2h_msg_types[] = {
51	[HTT_10X_T2H_MSG_TYPE_VERSION_CONF] = HTT_T2H_MSG_TYPE_VERSION_CONF,
52	[HTT_10X_T2H_MSG_TYPE_RX_IND] = HTT_T2H_MSG_TYPE_RX_IND,
53	[HTT_10X_T2H_MSG_TYPE_RX_FLUSH] = HTT_T2H_MSG_TYPE_RX_FLUSH,
54	[HTT_10X_T2H_MSG_TYPE_PEER_MAP] = HTT_T2H_MSG_TYPE_PEER_MAP,
55	[HTT_10X_T2H_MSG_TYPE_PEER_UNMAP] = HTT_T2H_MSG_TYPE_PEER_UNMAP,
56	[HTT_10X_T2H_MSG_TYPE_RX_ADDBA] = HTT_T2H_MSG_TYPE_RX_ADDBA,
57	[HTT_10X_T2H_MSG_TYPE_RX_DELBA] = HTT_T2H_MSG_TYPE_RX_DELBA,
58	[HTT_10X_T2H_MSG_TYPE_TX_COMPL_IND] = HTT_T2H_MSG_TYPE_TX_COMPL_IND,
59	[HTT_10X_T2H_MSG_TYPE_PKTLOG] = HTT_T2H_MSG_TYPE_PKTLOG,
60	[HTT_10X_T2H_MSG_TYPE_STATS_CONF] = HTT_T2H_MSG_TYPE_STATS_CONF,
61	[HTT_10X_T2H_MSG_TYPE_RX_FRAG_IND] = HTT_T2H_MSG_TYPE_RX_FRAG_IND,
62	[HTT_10X_T2H_MSG_TYPE_SEC_IND] = HTT_T2H_MSG_TYPE_SEC_IND,
63	[HTT_10X_T2H_MSG_TYPE_RC_UPDATE_IND] = HTT_T2H_MSG_TYPE_RC_UPDATE_IND,
64	[HTT_10X_T2H_MSG_TYPE_TX_INSPECT_IND] = HTT_T2H_MSG_TYPE_TX_INSPECT_IND,
65	[HTT_10X_T2H_MSG_TYPE_TEST] = HTT_T2H_MSG_TYPE_TEST,
66	[HTT_10X_T2H_MSG_TYPE_CHAN_CHANGE] = HTT_T2H_MSG_TYPE_CHAN_CHANGE,
67	[HTT_10X_T2H_MSG_TYPE_AGGR_CONF] = HTT_T2H_MSG_TYPE_AGGR_CONF,
68	[HTT_10X_T2H_MSG_TYPE_STATS_NOUPLOAD] = HTT_T2H_MSG_TYPE_STATS_NOUPLOAD,
69	[HTT_10X_T2H_MSG_TYPE_MGMT_TX_COMPL_IND] =
70		HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION,
71};
72
73static const enum htt_t2h_msg_type htt_tlv_t2h_msg_types[] = {
74	[HTT_TLV_T2H_MSG_TYPE_VERSION_CONF] = HTT_T2H_MSG_TYPE_VERSION_CONF,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LV_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND] =
94		HTT_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND,
95	[HTT_TLV_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND] =
96		HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND,
97	[HTT_TLV_T2H_MSG_TYPE_WDI_IPA_OP_RESPONSE] =
98		HTT_T2H_MSG_TYPE_WDI_IPA_OP_RESPONSE,
99	[HTT_TLV_T2H_MSG_TYPE_CHAN_CHANGE] = HTT_T2H_MSG_TYPE_CHAN_CHANGE,
100	[HTT_TLV_T2H_MSG_TYPE_RX_OFLD_PKT_ERR] =
101		HTT_T2H_MSG_TYPE_RX_OFLD_PKT_ERR,
102	[HTT_TLV_T2H_MSG_TYPE_TEST] = HTT_T2H_MSG_TYPE_TEST,
103};
104
105static const enum htt_t2h_msg_type htt_10_4_t2h_msg_types[] = {
106	[HTT_10_4_T2H_MSG_TYPE_VERSION_CONF] = HTT_T2H_MSG_TYPE_VERSION_CONF,
107	[HTT_10_4_T2H_MSG_TYPE_RX_IND] = HTT_T2H_MSG_TYPE_RX_IND,
108	[HTT_10_4_T2H_MSG_TYPE_RX_FLUSH] = HTT_T2H_MSG_TYPE_RX_FLUSH,
109	[HTT_10_4_T2H_MSG_TYPE_PEER_MAP] = HTT_T2H_MSG_TYPE_PEER_MAP,
110	[HTT_10_4_T2H_MSG_TYPE_PEER_UNMAP] = HTT_T2H_MSG_TYPE_PEER_UNMAP,
111	[HTT_10_4_T2H_MSG_TYPE_RX_ADDBA] = HTT_T2H_MSG_TYPE_RX_ADDBA,
112	[HTT_10_4_T2H_MSG_TYPE_RX_DELBA] = HTT_T2H_MSG_TYPE_RX_DELBA,
113	[HTT_10_4_T2H_MSG_TYPE_TX_COMPL_IND] = HTT_T2H_MSG_TYPE_TX_COMPL_IND,
114	[HTT_10_4_T2H_MSG_TYPE_PKTLOG] = HTT_T2H_MSG_TYPE_PKTLOG,
115	[HTT_10_4_T2H_MSG_TYPE_STATS_CONF] = HTT_T2H_MSG_TYPE_STATS_CONF,
116	[HTT_10_4_T2H_MSG_TYPE_RX_FRAG_IND] = HTT_T2H_MSG_TYPE_RX_FRAG_IND,
117	[HTT_10_4_T2H_MSG_TYPE_SEC_IND] = HTT_T2H_MSG_TYPE_SEC_IND,
118	[HTT_10_4_T2H_MSG_TYPE_RC_UPDATE_IND] = HTT_T2H_MSG_TYPE_RC_UPDATE_IND,
119	[HTT_10_4_T2H_MSG_TYPE_TX_INSPECT_IND] =
120				HTT_T2H_MSG_TYPE_TX_INSPECT_IND,
121	[HTT_10_4_T2H_MSG_TYPE_MGMT_TX_COMPL_IND] =
122				HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION,
123	[HTT_10_4_T2H_MSG_TYPE_CHAN_CHANGE] = HTT_T2H_MSG_TYPE_CHAN_CHANGE,
124	[HTT_10_4_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND] =
125				HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND,
126	[HTT_10_4_T2H_MSG_TYPE_RX_PN_IND] = HTT_T2H_MSG_TYPE_RX_PN_IND,
127	[HTT_10_4_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND] =
128				HTT_T2H_MSG_TYPE_RX_OFFLOAD_DELIVER_IND,
129	[HTT_10_4_T2H_MSG_TYPE_TEST] = HTT_T2H_MSG_TYPE_TEST,
130	[HTT_10_4_T2H_MSG_TYPE_EN_STATS] = HTT_T2H_MSG_TYPE_EN_STATS,
131	[HTT_10_4_T2H_MSG_TYPE_AGGR_CONF] = HTT_T2H_MSG_TYPE_AGGR_CONF,
132	[HTT_10_4_T2H_MSG_TYPE_TX_FETCH_IND] =
133				HTT_T2H_MSG_TYPE_TX_FETCH_IND,
134	[HTT_10_4_T2H_MSG_TYPE_TX_FETCH_CONF] =
135				HTT_T2H_MSG_TYPE_TX_FETCH_CONF,
136	[HTT_10_4_T2H_MSG_TYPE_STATS_NOUPLOAD] =
137				HTT_T2H_MSG_TYPE_STATS_NOUPLOAD,
138	[HTT_10_4_T2H_MSG_TYPE_TX_LOW_LATENCY_IND] =
139				HTT_T2H_MSG_TYPE_TX_LOW_LATENCY_IND,
140};
141
142int ath10k_htt_connect(struct ath10k_htt *htt)
143{
144	struct ath10k_htc_svc_conn_req conn_req;
145	struct ath10k_htc_svc_conn_resp conn_resp;
146	int status;
147
148	memset(&conn_req, 0, sizeof(conn_req));
149	memset(&conn_resp, 0, sizeof(conn_resp));
150
151	conn_req.ep_ops.ep_tx_complete = ath10k_htt_htc_tx_complete;
152	conn_req.ep_ops.ep_rx_complete = ath10k_htt_t2h_msg_handler;
153
154	/* connect to control service */
155	conn_req.service_id = ATH10K_HTC_SVC_ID_HTT_DATA_MSG;
156
157	status = ath10k_htc_connect_service(&htt->ar->htc, &conn_req,
158					    &conn_resp);
159
160	if (status)
161		return status;
162
163	htt->eid = conn_resp.eid;
164
165	return 0;
166}
167
168int ath10k_htt_init(struct ath10k *ar)
169{
170	struct ath10k_htt *htt = &ar->htt;
171
172	htt->ar = ar;
173
174	/*
175	 * Prefetch enough data to satisfy target
176	 * classification engine.
177	 * This is for LL chips. HL chips will probably
178	 * transfer all frame in the tx fragment.
179	 */
180	htt->prefetch_len =
181		36 + /* 802.11 + qos + ht */
182		4 + /* 802.1q */
183		8 + /* llc snap */
184		2; /* ip4 dscp or ip6 priority */
185
186	switch (ar->htt.op_version) {
187	case ATH10K_FW_HTT_OP_VERSION_10_4:
188		ar->htt.t2h_msg_types = htt_10_4_t2h_msg_types;
189		ar->htt.t2h_msg_types_max = HTT_10_4_T2H_NUM_MSGS;
190		break;
191	case ATH10K_FW_HTT_OP_VERSION_10_1:
192		ar->htt.t2h_msg_types = htt_10x_t2h_msg_types;
193		ar->htt.t2h_msg_types_max = HTT_10X_T2H_NUM_MSGS;
194		break;
195	case ATH10K_FW_HTT_OP_VERSION_TLV:
196		ar->htt.t2h_msg_types = htt_tlv_t2h_msg_types;
197		ar->htt.t2h_msg_types_max = HTT_TLV_T2H_NUM_MSGS;
198		break;
199	case ATH10K_FW_HTT_OP_VERSION_MAIN:
200		ar->htt.t2h_msg_types = htt_main_t2h_msg_types;
201		ar->htt.t2h_msg_types_max = HTT_MAIN_T2H_NUM_MSGS;
202		break;
203	case ATH10K_FW_HTT_OP_VERSION_MAX:
204	case ATH10K_FW_HTT_OP_VERSION_UNSET:
205		WARN_ON(1);
206		return -EINVAL;
207	}
208	return 0;
209}
210
211#define HTT_TARGET_VERSION_TIMEOUT_HZ (3*HZ)
212
213static int ath10k_htt_verify_version(struct ath10k_htt *htt)
214{
215	struct ath10k *ar = htt->ar;
216
217	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt target version %d.%d\n",
218		   htt->target_version_major, htt->target_version_minor);
219
220	if (htt->target_version_major != 2 &&
221	    htt->target_version_major != 3) {
222		ath10k_err(ar, "unsupported htt major version %d. supported versions are 2 and 3\n",
223			   htt->target_version_major);
224		return -ENOTSUPP;
225	}
226
227	return 0;
228}
229
230int ath10k_htt_setup(struct ath10k_htt *htt)
231{
232	struct ath10k *ar = htt->ar;
233	int status;
234
235	init_completion(&htt->target_version_received);
236
237	status = ath10k_htt_h2t_ver_req_msg(htt);
238	if (status)
239		return status;
240
241	status = wait_for_completion_timeout(&htt->target_version_received,
242					     HTT_TARGET_VERSION_TIMEOUT_HZ);
243	if (status == 0) {
244		ath10k_warn(ar, "htt version request timed out\n");
245		return -ETIMEDOUT;
246	}
247
248	status = ath10k_htt_verify_version(htt);
249	if (status) {
250		ath10k_warn(ar, "failed to verify htt version: %d\n",
251			    status);
252		return status;
253	}
254
255	status = ath10k_htt_send_frag_desc_bank_cfg(htt);
256	if (status)
257		return status;
258
259	status = ath10k_htt_send_rx_ring_cfg_ll(htt);
260	if (status) {
261		ath10k_warn(ar, "failed to setup rx ring: %d\n",
262			    status);
263		return status;
264	}
265
266	status = ath10k_htt_h2t_aggr_cfg_msg(htt,
267					     htt->max_num_ampdu,
268					     htt->max_num_amsdu);
269	if (status) {
270		ath10k_warn(ar, "failed to setup amsdu/ampdu limit: %d\n",
271			    status);
272		return status;
273	}
274
275	return 0;
276}
277