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