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