xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_internal.h (revision d0c05845839e5f2ba5a8dcebe0cd3e4cd4e8dfcf)
1 /*
2  * Copyright (c) 2013-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _HTC_INTERNAL_H_
21 #define _HTC_INTERNAL_H_
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
26 
27 #include "htc_api.h"
28 #include "htc_packet.h"
29 #include <hif.h>
30 #include <htc.h>
31 #include <qdf_atomic.h>
32 #include <qdf_event.h>
33 #include <qdf_lock.h>
34 #include <qdf_nbuf.h>
35 #include <qdf_timer.h>
36 #include <qdf_types.h>
37 
38 /* HTC operational parameters */
39 #define HTC_TARGET_RESPONSE_TIMEOUT         2000        /* in ms */
40 #define HTC_TARGET_DEBUG_INTR_MASK          0x01
41 #define HTC_TARGET_CREDIT_INTR_MASK         0xF0
42 #define HTC_MIN_MSG_PER_BUNDLE              2
43 
44 #if defined(HIF_USB)
45 
46 #define HTC_MAX_MSG_PER_BUNDLE_RX           11
47 #if defined(CFG_HTC_MAX_MSG_PER_BUNDLE_TX)
48 #define HTC_MAX_MSG_PER_BUNDLE_TX	 CFG_HTC_MAX_MSG_PER_BUNDLE_TX
49 #else
50 #define HTC_MAX_MSG_PER_BUNDLE_TX	8
51 #endif /* CFG_HTC_MAX_MSG_PER_BUNDLE_TX */
52 #else
53 #define HTC_MAX_MSG_PER_BUNDLE_RX           64
54 #define HTC_MAX_MSG_PER_BUNDLE              16
55 #define HTC_MAX_MSG_PER_BUNDLE_TX           32
56 #endif
57 
58 #ifdef HIF_SDIO
59 #define UPDATE_ALT_CREDIT(tar, val) (tar->AltDataCreditSize = (uint16_t) val)
60 #else
61 #define UPDATE_ALT_CREDIT(tar, val) /* no-op */
62 #endif
63 
64 /*
65  * HTC_MAX_TX_BUNDLE_SEND_LIMIT -
66  * This value is in units of tx frame fragments.
67  * It needs to be at least as large as the maximum number of tx frames in a
68  * HTC download bundle times the average number of fragments in each such frame
69  * (In certain operating systems, such as Linux, we expect to only have
70  * a single fragment per frame anyway.)
71  */
72 #define HTC_MAX_TX_BUNDLE_SEND_LIMIT        255
73 
74 #define HTC_PACKET_CONTAINER_ALLOCATION     32
75 #define NUM_CONTROL_TX_BUFFERS              2
76 #define HTC_CONTROL_BUFFER_SIZE             (HTC_MAX_CONTROL_MESSAGE_LENGTH + \
77 					     HTC_HDR_LENGTH)
78 #define HTC_CONTROL_BUFFER_ALIGN            32
79 #define HTC_TARGET_RESPONSE_POLL_MS         10
80 #if !defined(A_SIMOS_DEVHOST)
81 #define HTC_TARGET_MAX_RESPONSE_POLL        200 /* actual HW */
82 #else
83 #define HTC_TARGET_MAX_RESPONSE_POLL        600 /* host + target simulation */
84 #endif
85 
86 #define HTC_SERVICE_TX_PACKET_TAG  HTC_TX_PACKET_TAG_INTERNAL
87 
88 #ifndef HTC_CREDIT_HISTORY_MAX
89 #define HTC_CREDIT_HISTORY_MAX              1024
90 #endif
91 
92 #define HTC_IS_EPPING_ENABLED(_x)           ((_x) == QDF_GLOBAL_EPPING_MODE)
93 
94 enum htc_credit_exchange_type {
95 	HTC_REQUEST_CREDIT,
96 	HTC_PROCESS_CREDIT_REPORT,
97 	HTC_SUSPEND_ACK,
98 	HTC_SUSPEND_NACK,
99 	HTC_INITIAL_WAKE_UP,
100 };
101 
102 static inline const char*
103 htc_credit_exchange_type_str(enum htc_credit_exchange_type type)
104 {
105 	switch (type) {
106 	case HTC_REQUEST_CREDIT:
107 		return "HTC_REQUEST_CREDIT";
108 	case HTC_PROCESS_CREDIT_REPORT:
109 		return "HTC_PROCESS_CREDIT_REPORT";
110 	case HTC_SUSPEND_ACK:
111 		return "HTC_SUSPEND_ACK";
112 	case HTC_SUSPEND_NACK:
113 		return "HTC_SUSPEND_NACK";
114 	case HTC_INITIAL_WAKE_UP:
115 		return "HTC_INITIAL_WAKE_UP";
116 	default:
117 		return "Unknown htc_credit_exchange_type";
118 	}
119 }
120 
121 typedef struct _HTC_ENDPOINT {
122 	HTC_ENDPOINT_ID Id;
123 
124 	/* service ID this endpoint is bound to
125 	 * non-zero value means this endpoint is in use
126 	 */
127 	HTC_SERVICE_ID service_id;
128 
129 	/* callbacks associated with this endpoint */
130 	struct htc_ep_callbacks EpCallBacks;
131 	/* HTC frame buffer TX queue */
132 	HTC_PACKET_QUEUE TxQueue;
133 	/* max depth of the TX queue before calling driver's full handler */
134 	int MaxTxQueueDepth;
135 	/* max length of endpoint message */
136 	int MaxMsgLength;
137 	uint8_t UL_PipeID;
138 	uint8_t DL_PipeID;
139 	/* Need to call HIF to get tx completion callbacks? */
140 	int ul_is_polled;
141 	qdf_timer_t ul_poll_timer;
142 	int ul_poll_timer_active;
143 	int ul_outstanding_cnt;
144 	uint32_t htc_send_cnt;
145 	uint32_t htc_comp_cnt;
146 	/* Need to call HIF to fetch rx?  (Not currently supported.) */
147 	int dl_is_polled;
148 	/* not currently supported */
149 	/* qdf_timer_t dl_poll_timer; */
150 
151 	/* lookup queue to match netbufs to htc packets */
152 	HTC_PACKET_QUEUE TxLookupQueue;
153 	/* temporary hold queue for back compatibility */
154 	HTC_PACKET_QUEUE RxBufferHoldQueue;
155 	/* TX seq no (helpful) for debugging */
156 	uint8_t SeqNo;
157 	/* serialization */
158 	qdf_atomic_t TxProcessCount;
159 	struct _HTC_TARGET *target;
160 	/* TX credits available on this endpoint */
161 	int TxCredits;
162 	/* size in bytes of each credit (set by HTC) */
163 	int TxCreditSize;
164 	/* credits required per max message (precalculated) */
165 	int TxCreditsPerMaxMsg;
166 #ifdef HTC_EP_STAT_PROFILING
167 	/* endpoint statistics */
168 	struct htc_endpoint_stats endpoint_stats;
169 #endif
170 	bool TxCreditFlowEnabled;
171 	bool async_update;  /* packets can be queued asynchronously */
172 	qdf_spinlock_t lookup_queue_lock;
173 
174 	/* number of consecutive requeue attempts used for print */
175 	uint32_t num_requeues_warn;
176 	/* total number of requeue attempts */
177 	uint32_t total_num_requeues;
178 
179 } HTC_ENDPOINT;
180 
181 #ifdef HTC_EP_STAT_PROFILING
182 #define INC_HTC_EP_STAT(p, stat, count) ((p)->endpoint_stats.stat += (count))
183 #else
184 #define INC_HTC_EP_STAT(p, stat, count)
185 #endif
186 
187 struct htc_service_tx_credit_allocation {
188 	uint16_t service_id;
189 	uint8_t CreditAllocation;
190 };
191 
192 #define HTC_MAX_SERVICE_ALLOC_ENTRIES 8
193 
194 /* Error codes for HTC layer packet stats*/
195 enum ol_ath_htc_pkt_ecodes {
196 	/* error- get packet at head of HTC_PACKET_Q */
197 	GET_HTC_PKT_Q_FAIL = 0,
198 	HTC_PKT_Q_EMPTY,
199 	HTC_SEND_Q_EMPTY
200 };
201 
202 /* our HTC target state */
203 typedef struct _HTC_TARGET {
204 	struct hif_opaque_softc *hif_dev;
205 	HTC_ENDPOINT endpoint[ENDPOINT_MAX];
206 	qdf_spinlock_t HTCLock;
207 	qdf_spinlock_t HTCRxLock;
208 	qdf_spinlock_t HTCTxLock;
209 	uint32_t HTCStateFlags;
210 	void *host_handle;
211 	struct htc_init_info HTCInitInfo;
212 	HTC_PACKET *pHTCPacketStructPool; /* pool of HTC packets */
213 	HTC_PACKET_QUEUE ControlBufferTXFreeList;
214 	uint8_t CtrlResponseBuffer[HTC_MAX_CONTROL_MESSAGE_LENGTH];
215 	int CtrlResponseLength;
216 	qdf_event_t ctrl_response_valid;
217 	bool CtrlResponseProcessing;
218 	int TotalTransmitCredits;
219 	struct htc_service_tx_credit_allocation
220 		ServiceTxAllocTable[HTC_MAX_SERVICE_ALLOC_ENTRIES];
221 	int TargetCreditSize;
222 #ifdef RX_SG_SUPPORT
223 	qdf_nbuf_queue_t RxSgQueue;
224 	bool IsRxSgInprogress;
225 	uint32_t CurRxSgTotalLen;               /* current total length */
226 	uint32_t ExpRxSgTotalLen;               /* expected total length */
227 #endif
228 	qdf_device_t osdev;
229 	struct ol_ath_htc_stats htc_pkt_stats;
230 	HTC_PACKET *pBundleFreeList;
231 	uint32_t ce_send_cnt;
232 	uint32_t TX_comp_cnt;
233 	uint8_t MaxMsgsPerHTCBundle;
234 	qdf_work_t queue_kicker;
235 
236 #ifdef HIF_SDIO
237 	uint16_t AltDataCreditSize;
238 #endif
239 	uint32_t avail_tx_credits;
240 #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
241 	uint32_t rx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_RX];
242 	uint32_t tx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_TX];
243 #endif
244 
245 	uint32_t con_mode;
246 
247 	/*
248 	 * This flag is from the mboxping tool. It indicates that we cannot
249 	 * drop it. Besides, nodrop pkts have higher priority than normal pkts.
250 	 */
251 	A_BOOL is_nodrop_pkt;
252 
253 	/*
254 	 * Number of WMI endpoints used.
255 	 * Default value is 1. But it should be overidden after htc_create to
256 	 * reflect the actual count.
257 	 */
258 	uint8_t wmi_ep_count;
259 	/* Flag to indicate whether htc header length check is required */
260 	bool htc_hdr_length_check;
261 
262 	/* flag to enable packet send debug */
263 	bool htc_pkt_dbg;
264 
265 #ifdef FEATURE_RUNTIME_PM
266 	/* Runtime count for H2T msg with response */
267 	qdf_atomic_t htc_runtime_cnt;
268 #endif
269 	/* Non flow ctrl enabled endpoints nbuf map unmap count */
270 	uint32_t nbuf_nfc_map_count;
271 	uint32_t nbuf_nfc_unmap_count;
272 } HTC_TARGET;
273 
274 
275 #ifdef RX_SG_SUPPORT
276 #define RESET_RX_SG_CONFIG(_target) \
277 do { \
278 	_target->ExpRxSgTotalLen = 0; \
279 	_target->CurRxSgTotalLen = 0; \
280 	_target->IsRxSgInprogress = false; \
281 } while (0)
282 #endif
283 
284 #define HTC_STATE_STOPPING         (1 << 0)
285 #define HTC_STOPPING(t)            ((t)->HTCStateFlags & HTC_STATE_STOPPING)
286 #define LOCK_HTC(t)                qdf_spin_lock_bh(&(t)->HTCLock)
287 #define UNLOCK_HTC(t)              qdf_spin_unlock_bh(&(t)->HTCLock)
288 #define LOCK_HTC_RX(t)             qdf_spin_lock_bh(&(t)->HTCRxLock)
289 #define UNLOCK_HTC_RX(t)           qdf_spin_unlock_bh(&(t)->HTCRxLock)
290 #define LOCK_HTC_TX(t)             qdf_spin_lock_bh(&(t)->HTCTxLock)
291 #define UNLOCK_HTC_TX(t)           qdf_spin_unlock_bh(&(t)->HTCTxLock)
292 #define LOCK_HTC_EP_TX_LOOKUP(t)   qdf_spin_lock_bh(&(t)->lookup_queue_lock)
293 #define UNLOCK_HTC_EP_TX_LOOKUP(t) qdf_spin_unlock_bh(&(t)->lookup_queue_lock)
294 
295 #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((HTC_TARGET *)(hnd))
296 
297 #define IS_TX_CREDIT_FLOW_ENABLED(ep)  ((ep)->TxCreditFlowEnabled)
298 
299 #define HTC_POLL_CLEANUP_PERIOD_MS 10   /* milliseconds */
300 
301 /* Macro to Increment the  HTC_PACKET_ERRORS for Tx.*/
302 #define OL_ATH_HTC_PKT_ERROR_COUNT_INCR(_target, _ecode)	\
303 	do { \
304 		if (_ecode == GET_HTC_PKT_Q_FAIL) \
305 		(_target->htc_pkt_stats.htc_get_pkt_q_fail_count) += 1; \
306 		if (_ecode == HTC_PKT_Q_EMPTY) \
307 		(_target->htc_pkt_stats.htc_pkt_q_empty_count) += 1; \
308 		if (_ecode == HTC_SEND_Q_EMPTY) \
309 		(_target->htc_pkt_stats.htc_send_q_empty_count) += 1; \
310 	} while (0)
311 /* internal HTC functions */
312 
313 QDF_STATUS htc_rx_completion_handler(void *Context, qdf_nbuf_t netbuf,
314 				   uint8_t pipeID);
315 QDF_STATUS htc_tx_completion_handler(void *Context, qdf_nbuf_t netbuf,
316 					unsigned int transferID,
317 					uint32_t toeplitz_hash_result);
318 
319 HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target);
320 void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
321 
322 HTC_PACKET *allocate_htc_packet_container(HTC_TARGET *target);
323 void free_htc_packet_container(HTC_TARGET *target, HTC_PACKET *pPacket);
324 void htc_flush_rx_hold_queue(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint);
325 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
326 			   HTC_TX_TAG Tag);
327 
328 /**
329  * htc_flush_endpoint_txlookupQ() - Flush EP's lookup queue
330  * @target: HTC target
331  * @endpoint_id: EP ID
332  * @call_ep_callback: whether to call EP tx completion callback
333  *
334  * Return: void
335  */
336 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
337 				  HTC_ENDPOINT_ID endpoint_id,
338 				  bool call_ep_callback);
339 
340 void htc_recv_init(HTC_TARGET *target);
341 QDF_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target);
342 void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
343 HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target);
344 uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id);
345 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID);
346 void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket);
347 void htc_process_credit_rpt(HTC_TARGET *target,
348 			    HTC_CREDIT_REPORT *pRpt,
349 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
350 void htc_fw_event_handler(void *context, QDF_STATUS status);
351 void htc_send_complete_check_cleanup(void *context);
352 #ifdef FEATURE_RUNTIME_PM
353 void htc_kick_queues(void *context);
354 #endif
355 
356 static inline void htc_send_complete_poll_timer_stop(HTC_ENDPOINT *
357 						     pEndpoint) {
358 	LOCK_HTC_TX(pEndpoint->target);
359 	if (pEndpoint->ul_poll_timer_active) {
360 		/* qdf_timer_stop(&pEndpoint->ul_poll_timer); */
361 		pEndpoint->ul_poll_timer_active = 0;
362 	}
363 	UNLOCK_HTC_TX(pEndpoint->target);
364 }
365 
366 static inline void htc_send_complete_poll_timer_start(HTC_ENDPOINT *
367 						      pEndpoint) {
368 	LOCK_HTC_TX(pEndpoint->target);
369 	if (pEndpoint->ul_outstanding_cnt
370 	    && !pEndpoint->ul_poll_timer_active) {
371 		/* qdf_timer_start(
372 		 * &pEndpoint->ul_poll_timer, HTC_POLL_CLEANUP_PERIOD_MS);
373 		 */
374 		pEndpoint->ul_poll_timer_active = 1;
375 	}
376 	UNLOCK_HTC_TX(pEndpoint->target);
377 }
378 
379 static inline void
380 htc_send_complete_check(HTC_ENDPOINT *pEndpoint, int force) {
381 	/*
382 	 * Stop the polling-cleanup timer that will result in a later call to
383 	 * this function.  It may get started again below, if there are still
384 	 * outsending sends.
385 	 */
386 	htc_send_complete_poll_timer_stop(pEndpoint);
387 	/*
388 	 * Check whether HIF has any prior sends that have finished,
389 	 * have not had the post-processing done.
390 	 */
391 	hif_send_complete_check(pEndpoint->target->hif_dev,
392 				pEndpoint->UL_PipeID, force);
393 	/*
394 	 * If there are still outstanding sends after polling, start a timer
395 	 * to check again a little later.
396 	 */
397 	htc_send_complete_poll_timer_start(pEndpoint);
398 }
399 
400 #ifdef __cplusplus
401 }
402 #endif
403 
404 #ifndef DEBUG_BUNDLE
405 #define DEBUG_BUNDLE 0
406 #endif
407 
408 /* HTC Control message receive timeout msec */
409 #define HTC_CONTROL_RX_TIMEOUT     6000
410 
411 #if defined(HIF_SDIO) || defined(HIF_USB)
412 #ifndef ENABLE_BUNDLE_TX
413 #define ENABLE_BUNDLE_TX 1
414 #endif
415 
416 #ifndef ENABLE_BUNDLE_RX
417 #define ENABLE_BUNDLE_RX 1
418 #endif
419 #endif /*defined(HIF_SDIO) || defined(HIF_USB)*/
420 
421 #if defined ENABLE_BUNDLE_TX
422 #define HTC_TX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
423 #else
424 #define HTC_TX_BUNDLE_ENABLED(target) 0
425 #endif
426 
427 #if defined ENABLE_BUNDLE_RX
428 #define HTC_RX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
429 #else
430 #define HTC_RX_BUNDLE_ENABLED(target) 0
431 #endif
432 
433 #define HTC_ENABLE_BUNDLE(target) (target->MaxMsgsPerHTCBundle > 1)
434 
435 #endif /* !_HTC_HOST_INTERNAL_H_ */
436