xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/ce/ce_bmi.c (revision 92d87f51612f6c3b2285266215edee8911647c2f)
1 /*
2  * Copyright (c) 2015-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "targcfg.h"
20 #include "qdf_lock.h"
21 #include "qdf_status.h"
22 #include "qdf_status.h"
23 #include <qdf_atomic.h>         /* qdf_atomic_read */
24 #include <targaddrs.h>
25 #include "hif_io32.h"
26 #include <hif.h>
27 #include "regtable.h"
28 #define ATH_MODULE_NAME hif
29 #include <a_debug.h>
30 #include "hif_main.h"
31 #include "ce_api.h"
32 #include "ce_bmi.h"
33 #include "qdf_trace.h"
34 #include "hif_debug.h"
35 #include "bmi_msg.h"
36 #include "qdf_module.h"
37 
38 /* Track a BMI transaction that is in progress */
39 #ifndef BIT
40 #define BIT(n) (1 << (n))
41 #endif
42 
43 enum {
44 	BMI_REQ_SEND_DONE = BIT(0),   /* the bmi tx completion */
45 	BMI_RESP_RECV_DONE = BIT(1),  /* the bmi respond is received */
46 };
47 
48 struct BMI_transaction {
49 	struct HIF_CE_state *hif_state;
50 	qdf_semaphore_t bmi_transaction_sem;
51 	uint8_t *bmi_request_host;        /* Req BMI msg in Host addr space */
52 	qdf_dma_addr_t bmi_request_CE;    /* Req BMI msg in CE addr space */
53 	uint32_t bmi_request_length;      /* Length of BMI request */
54 	uint8_t *bmi_response_host;       /* Rsp BMI msg in Host addr space */
55 	qdf_dma_addr_t bmi_response_CE;   /* Rsp BMI msg in CE addr space */
56 	unsigned int bmi_response_length; /* Length of received response */
57 	unsigned int bmi_timeout_ms;
58 	uint32_t bmi_transaction_flags;   /* flags for the transcation */
59 };
60 
61 /*
62  * send/recv completion functions for BMI.
63  * NB: The "net_buf" parameter is actually just a
64  * straight buffer, not an sk_buff.
65  */
66 void hif_bmi_send_done(struct CE_handle *copyeng, void *ce_context,
67 		  void *transfer_context, qdf_dma_addr_t data,
68 		  unsigned int nbytes,
69 		  unsigned int transfer_id, unsigned int sw_index,
70 		  unsigned int hw_index, uint32_t toeplitz_hash_result)
71 {
72 	struct BMI_transaction *transaction =
73 		(struct BMI_transaction *)transfer_context;
74 
75 #ifdef BMI_RSP_POLLING
76 	/*
77 	 * Fix EV118783, Release a semaphore after sending
78 	 * no matter whether a response is been expecting now.
79 	 */
80 	qdf_semaphore_release(&transaction->bmi_transaction_sem);
81 #else
82 	/*
83 	 * If a response is anticipated, we'll complete the
84 	 * transaction if the response has been received.
85 	 * If no response is anticipated, complete the
86 	 * transaction now.
87 	 */
88 	transaction->bmi_transaction_flags |= BMI_REQ_SEND_DONE;
89 
90 	/* resp is't needed or has already been received,
91 	 * never assume resp comes later then this
92 	 */
93 	if (!transaction->bmi_response_CE ||
94 	    (transaction->bmi_transaction_flags & BMI_RESP_RECV_DONE)) {
95 		qdf_semaphore_release(&transaction->bmi_transaction_sem);
96 	}
97 #endif
98 }
99 
100 #ifndef BMI_RSP_POLLING
101 void hif_bmi_recv_data(struct CE_handle *copyeng, void *ce_context,
102 		  void *transfer_context, qdf_dma_addr_t data,
103 		  unsigned int nbytes,
104 		  unsigned int transfer_id, unsigned int flags)
105 {
106 	struct BMI_transaction *transaction =
107 		(struct BMI_transaction *)transfer_context;
108 
109 	transaction->bmi_response_length = nbytes;
110 	transaction->bmi_transaction_flags |= BMI_RESP_RECV_DONE;
111 
112 	/* when both send/recv are done, the sem can be released */
113 	if (transaction->bmi_transaction_flags & BMI_REQ_SEND_DONE)
114 		qdf_semaphore_release(&transaction->bmi_transaction_sem);
115 }
116 #endif
117 
118 /* Timeout for BMI message exchange */
119 #define HIF_EXCHANGE_BMI_MSG_TIMEOUT 6000
120 
121 QDF_STATUS hif_exchange_bmi_msg(struct hif_opaque_softc *hif_ctx,
122 				qdf_dma_addr_t bmi_cmd_da,
123 				qdf_dma_addr_t bmi_rsp_da,
124 				uint8_t *bmi_request,
125 				uint32_t request_length,
126 				uint8_t *bmi_response,
127 				uint32_t *bmi_response_lengthp,
128 				uint32_t TimeoutMS)
129 {
130 	struct hif_softc *scn = HIF_GET_SOFTC(hif_ctx);
131 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_ctx);
132 	struct HIF_CE_pipe_info *send_pipe_info =
133 		&(hif_state->pipe_info[BMI_CE_NUM_TO_TARG]);
134 	struct CE_handle *ce_send_hdl = send_pipe_info->ce_hdl;
135 	qdf_dma_addr_t CE_request, CE_response = 0;
136 	struct BMI_transaction *transaction = NULL;
137 	int status = QDF_STATUS_SUCCESS;
138 	struct HIF_CE_pipe_info *recv_pipe_info =
139 		&(hif_state->pipe_info[BMI_CE_NUM_TO_HOST]);
140 	struct CE_handle *ce_recv = recv_pipe_info->ce_hdl;
141 	unsigned int mux_id = 0;
142 	unsigned int transaction_id = 0xffff;
143 	unsigned int user_flags = 0;
144 #ifdef BMI_RSP_POLLING
145 	qdf_dma_addr_t buf;
146 	unsigned int completed_nbytes, id, flags;
147 	int i;
148 #endif
149 
150 	transaction =
151 		(struct BMI_transaction *)qdf_mem_malloc(sizeof(*transaction));
152 	if (unlikely(!transaction)) {
153 		HIF_ERROR("%s: no memory", __func__);
154 		return QDF_STATUS_E_NOMEM;
155 	}
156 	transaction_id = (mux_id & MUX_ID_MASK) |
157 		(transaction_id & TRANSACTION_ID_MASK);
158 #ifdef QCA_WIFI_3_0
159 	user_flags &= DESC_DATA_FLAG_MASK;
160 #endif
161 	A_TARGET_ACCESS_LIKELY(scn);
162 
163 	/* Initialize bmi_transaction_sem to block */
164 	qdf_semaphore_init(&transaction->bmi_transaction_sem);
165 	qdf_semaphore_acquire(&transaction->bmi_transaction_sem);
166 
167 	transaction->hif_state = hif_state;
168 	transaction->bmi_request_host = bmi_request;
169 	transaction->bmi_request_length = request_length;
170 	transaction->bmi_response_length = 0;
171 	transaction->bmi_timeout_ms = TimeoutMS;
172 	transaction->bmi_transaction_flags = 0;
173 
174 	/*
175 	 * CE_request = dma_map_single(dev,
176 	 * (void *)bmi_request, request_length, DMA_TO_DEVICE);
177 	 */
178 	CE_request = bmi_cmd_da;
179 	transaction->bmi_request_CE = CE_request;
180 
181 	if (bmi_response) {
182 
183 		/*
184 		 * CE_response = dma_map_single(dev, bmi_response,
185 		 * BMI_DATASZ_MAX, DMA_FROM_DEVICE);
186 		 */
187 		CE_response = bmi_rsp_da;
188 		transaction->bmi_response_host = bmi_response;
189 		transaction->bmi_response_CE = CE_response;
190 		/* dma_cache_sync(dev, bmi_response,
191 		 *      BMI_DATASZ_MAX, DMA_FROM_DEVICE);
192 		 */
193 		qdf_mem_dma_sync_single_for_device(scn->qdf_dev,
194 					       CE_response,
195 					       BMI_DATASZ_MAX,
196 					       DMA_FROM_DEVICE);
197 		ce_recv_buf_enqueue(ce_recv, transaction,
198 				    transaction->bmi_response_CE);
199 		/* NB: see HIF_BMI_recv_done */
200 	} else {
201 		transaction->bmi_response_host = NULL;
202 		transaction->bmi_response_CE = 0;
203 	}
204 
205 	/* dma_cache_sync(dev, bmi_request, request_length, DMA_TO_DEVICE); */
206 	qdf_mem_dma_sync_single_for_device(scn->qdf_dev, CE_request,
207 				       request_length, DMA_TO_DEVICE);
208 
209 	status =
210 		ce_send(ce_send_hdl, transaction,
211 			CE_request, request_length,
212 			transaction_id, 0, user_flags);
213 	ASSERT(status == QDF_STATUS_SUCCESS);
214 	/* NB: see hif_bmi_send_done */
215 
216 	/* TBDXXX: handle timeout */
217 
218 	/* Wait for BMI request/response transaction to complete */
219 	/* Always just wait for BMI request here if
220 	 * BMI_RSP_POLLING is defined
221 	 */
222 	if (qdf_semaphore_acquire_timeout
223 		       (&transaction->bmi_transaction_sem,
224 			HIF_EXCHANGE_BMI_MSG_TIMEOUT)) {
225 		HIF_ERROR("%s: Fatal error, BMI transaction timeout. Please check the HW interface!!",
226 			  __func__);
227 		qdf_mem_free(transaction);
228 		return QDF_STATUS_E_TIMEOUT;
229 	}
230 
231 	if (bmi_response) {
232 #ifdef BMI_RSP_POLLING
233 		/* Fix EV118783, do not wait a semaphore for the BMI response
234 		 * since the relative interruption may be lost.
235 		 * poll the BMI response instead.
236 		 */
237 		i = 0;
238 		while (ce_completed_recv_next(
239 			    ce_recv, NULL, NULL, &buf,
240 			    &completed_nbytes, &id,
241 			    &flags) != QDF_STATUS_SUCCESS) {
242 			if (i++ > BMI_RSP_TO_MILLISEC) {
243 				HIF_ERROR("%s:error, can't get bmi response",
244 					__func__);
245 				status = QDF_STATUS_E_BUSY;
246 				break;
247 			}
248 			OS_DELAY(1000);
249 		}
250 
251 		if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp)
252 			*bmi_response_lengthp = completed_nbytes;
253 #else
254 		if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp) {
255 			*bmi_response_lengthp =
256 				transaction->bmi_response_length;
257 		}
258 #endif
259 
260 	}
261 
262 	/* dma_unmap_single(dev, transaction->bmi_request_CE,
263 	 *     request_length, DMA_TO_DEVICE);
264 	 * bus_unmap_single(scn->sc_osdev,
265 	 *     transaction->bmi_request_CE,
266 	 *     request_length, BUS_DMA_TODEVICE);
267 	 */
268 
269 	if (status != QDF_STATUS_SUCCESS) {
270 		qdf_dma_addr_t unused_buffer;
271 		unsigned int unused_nbytes;
272 		unsigned int unused_id;
273 		unsigned int toeplitz_hash_result;
274 
275 		ce_cancel_send_next(ce_send_hdl,
276 			NULL, NULL, &unused_buffer,
277 			&unused_nbytes, &unused_id,
278 			&toeplitz_hash_result);
279 	}
280 
281 	A_TARGET_ACCESS_UNLIKELY(scn);
282 	qdf_mem_free(transaction);
283 	return status;
284 }
285 qdf_export_symbol(hif_exchange_bmi_msg);
286 
287 #ifdef BMI_RSP_POLLING
288 #define BMI_RSP_CB_REGISTER 0
289 #else
290 #define BMI_RSP_CB_REGISTER 1
291 #endif
292 
293 /**
294  * hif_register_bmi_callbacks() - register bmi callbacks
295  * @hif_sc: hif context
296  *
297  * Bmi phase uses different copy complete callbacks than mission mode.
298  */
299 void hif_register_bmi_callbacks(struct hif_softc *hif_sc)
300 {
301 	struct HIF_CE_pipe_info *pipe_info;
302 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_sc);
303 
304 	/*
305 	 * Initially, establish CE completion handlers for use with BMI.
306 	 * These are overwritten with generic handlers after we exit BMI phase.
307 	 */
308 	pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_TARG];
309 	ce_send_cb_register(pipe_info->ce_hdl, hif_bmi_send_done, pipe_info, 0);
310 
311 	if (BMI_RSP_CB_REGISTER) {
312 		pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_HOST];
313 		ce_recv_cb_register(
314 			pipe_info->ce_hdl, hif_bmi_recv_data, pipe_info, 0);
315 	}
316 }
317