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