xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/ce/ce_bmi.c (revision 4865edfd190c086bbe2c69aae12a8226f877b91e)
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 QDF_STATUS hif_exchange_bmi_msg(struct hif_opaque_softc *hif_ctx,
119 				qdf_dma_addr_t bmi_cmd_da,
120 				qdf_dma_addr_t bmi_rsp_da,
121 				uint8_t *bmi_request,
122 				uint32_t request_length,
123 				uint8_t *bmi_response,
124 				uint32_t *bmi_response_lengthp,
125 				uint32_t TimeoutMS)
126 {
127 	struct hif_softc *scn = HIF_GET_SOFTC(hif_ctx);
128 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_ctx);
129 	struct HIF_CE_pipe_info *send_pipe_info =
130 		&(hif_state->pipe_info[BMI_CE_NUM_TO_TARG]);
131 	struct CE_handle *ce_send_hdl = send_pipe_info->ce_hdl;
132 	qdf_dma_addr_t CE_request, CE_response = 0;
133 	struct BMI_transaction *transaction = NULL;
134 	int status = QDF_STATUS_SUCCESS;
135 	struct HIF_CE_pipe_info *recv_pipe_info =
136 		&(hif_state->pipe_info[BMI_CE_NUM_TO_HOST]);
137 	struct CE_handle *ce_recv = recv_pipe_info->ce_hdl;
138 	unsigned int mux_id = 0;
139 	unsigned int transaction_id = 0xffff;
140 	unsigned int user_flags = 0;
141 #ifdef BMI_RSP_POLLING
142 	qdf_dma_addr_t buf;
143 	unsigned int completed_nbytes, id, flags;
144 	int i;
145 #endif
146 
147 	transaction =
148 		(struct BMI_transaction *)qdf_mem_malloc(sizeof(*transaction));
149 	if (unlikely(!transaction)) {
150 		HIF_ERROR("%s: no memory", __func__);
151 		return QDF_STATUS_E_NOMEM;
152 	}
153 	transaction_id = (mux_id & MUX_ID_MASK) |
154 		(transaction_id & TRANSACTION_ID_MASK);
155 #ifdef QCA_WIFI_3_0
156 	user_flags &= DESC_DATA_FLAG_MASK;
157 #endif
158 	A_TARGET_ACCESS_LIKELY(scn);
159 
160 	/* Initialize bmi_transaction_sem to block */
161 	qdf_semaphore_init(&transaction->bmi_transaction_sem);
162 	qdf_semaphore_acquire(&transaction->bmi_transaction_sem);
163 
164 	transaction->hif_state = hif_state;
165 	transaction->bmi_request_host = bmi_request;
166 	transaction->bmi_request_length = request_length;
167 	transaction->bmi_response_length = 0;
168 	transaction->bmi_timeout_ms = TimeoutMS;
169 	transaction->bmi_transaction_flags = 0;
170 
171 	/*
172 	 * CE_request = dma_map_single(dev,
173 	 * (void *)bmi_request, request_length, DMA_TO_DEVICE);
174 	 */
175 	CE_request = bmi_cmd_da;
176 	transaction->bmi_request_CE = CE_request;
177 
178 	if (bmi_response) {
179 
180 		/*
181 		 * CE_response = dma_map_single(dev, bmi_response,
182 		 * BMI_DATASZ_MAX, DMA_FROM_DEVICE);
183 		 */
184 		CE_response = bmi_rsp_da;
185 		transaction->bmi_response_host = bmi_response;
186 		transaction->bmi_response_CE = CE_response;
187 		/* dma_cache_sync(dev, bmi_response,
188 		 *      BMI_DATASZ_MAX, DMA_FROM_DEVICE);
189 		 */
190 		qdf_mem_dma_sync_single_for_device(scn->qdf_dev,
191 					       CE_response,
192 					       BMI_DATASZ_MAX,
193 					       DMA_FROM_DEVICE);
194 		ce_recv_buf_enqueue(ce_recv, transaction,
195 				    transaction->bmi_response_CE);
196 		/* NB: see HIF_BMI_recv_done */
197 	} else {
198 		transaction->bmi_response_host = NULL;
199 		transaction->bmi_response_CE = 0;
200 	}
201 
202 	/* dma_cache_sync(dev, bmi_request, request_length, DMA_TO_DEVICE); */
203 	qdf_mem_dma_sync_single_for_device(scn->qdf_dev, CE_request,
204 				       request_length, DMA_TO_DEVICE);
205 
206 	status =
207 		ce_send(ce_send_hdl, transaction,
208 			CE_request, request_length,
209 			transaction_id, 0, user_flags);
210 	ASSERT(status == QDF_STATUS_SUCCESS);
211 	/* NB: see hif_bmi_send_done */
212 
213 	/* TBDXXX: handle timeout */
214 
215 	/* Wait for BMI request/response transaction to complete */
216 	/* Always just wait for BMI request here if
217 	 * BMI_RSP_POLLING is defined
218 	 */
219 	while (qdf_semaphore_acquire
220 		       (&transaction->bmi_transaction_sem)) {
221 		/*need some break out condition(time out?) */
222 	}
223 
224 	if (bmi_response) {
225 #ifdef BMI_RSP_POLLING
226 		/* Fix EV118783, do not wait a semaphore for the BMI response
227 		 * since the relative interruption may be lost.
228 		 * poll the BMI response instead.
229 		 */
230 		i = 0;
231 		while (ce_completed_recv_next(
232 			    ce_recv, NULL, NULL, &buf,
233 			    &completed_nbytes, &id,
234 			    &flags) != QDF_STATUS_SUCCESS) {
235 			if (i++ > BMI_RSP_TO_MILLISEC) {
236 				HIF_ERROR("%s:error, can't get bmi response",
237 					__func__);
238 				status = QDF_STATUS_E_BUSY;
239 				break;
240 			}
241 			OS_DELAY(1000);
242 		}
243 
244 		if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp)
245 			*bmi_response_lengthp = completed_nbytes;
246 #else
247 		if ((status == QDF_STATUS_SUCCESS) && bmi_response_lengthp) {
248 			*bmi_response_lengthp =
249 				transaction->bmi_response_length;
250 		}
251 #endif
252 
253 	}
254 
255 	/* dma_unmap_single(dev, transaction->bmi_request_CE,
256 	 *     request_length, DMA_TO_DEVICE);
257 	 * bus_unmap_single(scn->sc_osdev,
258 	 *     transaction->bmi_request_CE,
259 	 *     request_length, BUS_DMA_TODEVICE);
260 	 */
261 
262 	if (status != QDF_STATUS_SUCCESS) {
263 		qdf_dma_addr_t unused_buffer;
264 		unsigned int unused_nbytes;
265 		unsigned int unused_id;
266 		unsigned int toeplitz_hash_result;
267 
268 		ce_cancel_send_next(ce_send_hdl,
269 			NULL, NULL, &unused_buffer,
270 			&unused_nbytes, &unused_id,
271 			&toeplitz_hash_result);
272 	}
273 
274 	A_TARGET_ACCESS_UNLIKELY(scn);
275 	qdf_mem_free(transaction);
276 	return status;
277 }
278 qdf_export_symbol(hif_exchange_bmi_msg);
279 
280 #ifdef BMI_RSP_POLLING
281 #define BMI_RSP_CB_REGISTER 0
282 #else
283 #define BMI_RSP_CB_REGISTER 1
284 #endif
285 
286 /**
287  * hif_register_bmi_callbacks() - register bmi callbacks
288  * @hif_sc: hif context
289  *
290  * Bmi phase uses different copy complete callbacks than mission mode.
291  */
292 void hif_register_bmi_callbacks(struct hif_softc *hif_sc)
293 {
294 	struct HIF_CE_pipe_info *pipe_info;
295 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(hif_sc);
296 
297 	/*
298 	 * Initially, establish CE completion handlers for use with BMI.
299 	 * These are overwritten with generic handlers after we exit BMI phase.
300 	 */
301 	pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_TARG];
302 	ce_send_cb_register(pipe_info->ce_hdl, hif_bmi_send_done, pipe_info, 0);
303 
304 	if (BMI_RSP_CB_REGISTER) {
305 		pipe_info = &hif_state->pipe_info[BMI_CE_NUM_TO_HOST];
306 		ce_recv_cb_register(
307 			pipe_info->ce_hdl, hif_bmi_recv_data, pipe_info, 0);
308 	}
309 }
310