xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_internal.h (revision 3149adf58a329e17232a4c0e58d460d025edd55a)
1 /*
2  * Copyright (c) 2013-2018 The Linux Foundation. All rights reserved.
3  *
4  * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5  *
6  *
7  * Permission to use, copy, modify, and/or distribute this software for
8  * any purpose with or without fee is hereby granted, provided that the
9  * above copyright notice and this permission notice appear in all
10  * copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19  * PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 /*
23  * This file was originally distributed by Qualcomm Atheros, Inc.
24  * under proprietary terms before Copyright ownership was assigned
25  * to the Linux Foundation.
26  */
27 
28 #ifndef _HTC_INTERNAL_H_
29 #define _HTC_INTERNAL_H_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34 
35 #include "htc_api.h"
36 #include "htc_packet.h"
37 #include <hif.h>
38 #include <htc.h>
39 #include <qdf_atomic.h>
40 #include <qdf_event.h>
41 #include <qdf_lock.h>
42 #include <qdf_nbuf.h>
43 #include <qdf_timer.h>
44 #include <qdf_types.h>
45 
46 /* HTC operational parameters */
47 #define HTC_TARGET_RESPONSE_TIMEOUT         2000        /* in ms */
48 #define HTC_TARGET_DEBUG_INTR_MASK          0x01
49 #define HTC_TARGET_CREDIT_INTR_MASK         0xF0
50 #define HTC_MIN_MSG_PER_BUNDLE              2
51 #if defined(HIF_USB)
52 #define HTC_MAX_MSG_PER_BUNDLE_RX           11
53 #define HTC_MAX_MSG_PER_BUNDLE_TX           8
54 #else
55 #define HTC_MAX_MSG_PER_BUNDLE_RX           64
56 #define HTC_MAX_MSG_PER_BUNDLE              16
57 #define HTC_MAX_MSG_PER_BUNDLE_TX           32
58 #endif
59 
60 #ifdef HIF_SDIO
61 #define UPDATE_ALT_CREDIT(tar, val) (tar->AltDataCreditSize = (uint16_t) val)
62 #else
63 #define UPDATE_ALT_CREDIT(tar, val) /* no-op */
64 #endif
65 
66 /*
67  * HTC_MAX_TX_BUNDLE_SEND_LIMIT -
68  * This value is in units of tx frame fragments.
69  * It needs to be at least as large as the maximum number of tx frames in a
70  * HTC download bundle times the average number of fragments in each such frame
71  * (In certain operating systems, such as Linux, we expect to only have
72  * a single fragment per frame anyway.)
73  */
74 #define HTC_MAX_TX_BUNDLE_SEND_LIMIT        255
75 
76 #define HTC_PACKET_CONTAINER_ALLOCATION     32
77 #define NUM_CONTROL_TX_BUFFERS              2
78 #define HTC_CONTROL_BUFFER_SIZE             (HTC_MAX_CONTROL_MESSAGE_LENGTH + \
79 					     HTC_HDR_LENGTH)
80 #define HTC_CONTROL_BUFFER_ALIGN            32
81 #define HTC_TARGET_RESPONSE_POLL_MS         10
82 #if !defined(A_SIMOS_DEVHOST)
83 #define HTC_TARGET_MAX_RESPONSE_POLL        200 /* actual HW */
84 #else
85 #define HTC_TARGET_MAX_RESPONSE_POLL        600 /* host + target simulation */
86 #endif
87 
88 #define HTC_SERVICE_TX_PACKET_TAG  HTC_TX_PACKET_TAG_INTERNAL
89 
90 #define HTC_CREDIT_HISTORY_MAX              1024
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 	/* Need to call HIF to fetch rx?  (Not currently supported.) */
145 	int dl_is_polled;
146 	/* not currently supported */
147 	/* qdf_timer_t dl_poll_timer; */
148 
149 	/* lookup queue to match netbufs to htc packets */
150 	HTC_PACKET_QUEUE TxLookupQueue;
151 	/* temporary hold queue for back compatibility */
152 	HTC_PACKET_QUEUE RxBufferHoldQueue;
153 	/* TX seq no (helpful) for debugging */
154 	uint8_t SeqNo;
155 	/* serialization */
156 	qdf_atomic_t TxProcessCount;
157 	struct _HTC_TARGET *target;
158 	/* TX credits available on this endpoint */
159 	int TxCredits;
160 	/* size in bytes of each credit (set by HTC) */
161 	int TxCreditSize;
162 	/* credits required per max message (precalculated) */
163 	int TxCreditsPerMaxMsg;
164 #ifdef HTC_EP_STAT_PROFILING
165 	/* endpoint statistics */
166 	struct htc_endpoint_stats endpoint_stats;
167 #endif
168 	bool TxCreditFlowEnabled;
169 	bool async_update;  /* packets can be queued asynchronously */
170 	qdf_spinlock_t lookup_queue_lock;
171 } HTC_ENDPOINT;
172 
173 #ifdef HTC_EP_STAT_PROFILING
174 #define INC_HTC_EP_STAT(p, stat, count) ((p)->endpoint_stats.stat += (count))
175 #else
176 #define INC_HTC_EP_STAT(p, stat, count)
177 #endif
178 
179 struct htc_service_tx_credit_allocation {
180 	uint16_t service_id;
181 	uint8_t CreditAllocation;
182 };
183 
184 #define HTC_MAX_SERVICE_ALLOC_ENTRIES 8
185 
186 /* Error codes for HTC layer packet stats*/
187 enum ol_ath_htc_pkt_ecodes {
188 	/* error- get packet at head of HTC_PACKET_Q */
189 	GET_HTC_PKT_Q_FAIL = 0,
190 	HTC_PKT_Q_EMPTY,
191 	HTC_SEND_Q_EMPTY
192 };
193 /* our HTC target state */
194 typedef struct _HTC_TARGET {
195 	struct hif_opaque_softc *hif_dev;
196 	HTC_ENDPOINT endpoint[ENDPOINT_MAX];
197 	qdf_spinlock_t HTCLock;
198 	qdf_spinlock_t HTCRxLock;
199 	qdf_spinlock_t HTCTxLock;
200 	qdf_spinlock_t HTCCreditLock;
201 	uint32_t HTCStateFlags;
202 	void *host_handle;
203 	struct htc_init_info HTCInitInfo;
204 	HTC_PACKET *pHTCPacketStructPool; /* pool of HTC packets */
205 	HTC_PACKET_QUEUE ControlBufferTXFreeList;
206 	uint8_t CtrlResponseBuffer[HTC_MAX_CONTROL_MESSAGE_LENGTH];
207 	int CtrlResponseLength;
208 	qdf_event_t ctrl_response_valid;
209 	bool CtrlResponseProcessing;
210 	int TotalTransmitCredits;
211 	struct htc_service_tx_credit_allocation
212 		ServiceTxAllocTable[HTC_MAX_SERVICE_ALLOC_ENTRIES];
213 	int TargetCreditSize;
214 #ifdef RX_SG_SUPPORT
215 	qdf_nbuf_queue_t RxSgQueue;
216 	bool IsRxSgInprogress;
217 	uint32_t CurRxSgTotalLen;               /* current total length */
218 	uint32_t ExpRxSgTotalLen;               /* expected total length */
219 #endif
220 	qdf_device_t osdev;
221 	struct ol_ath_htc_stats htc_pkt_stats;
222 	HTC_PACKET *pBundleFreeList;
223 	uint32_t ce_send_cnt;
224 	uint32_t TX_comp_cnt;
225 	uint8_t MaxMsgsPerHTCBundle;
226 	qdf_work_t queue_kicker;
227 
228 #ifdef HIF_SDIO
229 	A_UINT16 AltDataCreditSize;
230 #endif
231 	A_UINT32 avail_tx_credits;
232 #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
233 	A_UINT32 rx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_RX];
234 	A_UINT32 tx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_TX];
235 #endif
236 
237 	uint32_t con_mode;
238 
239 	/*
240 	 * This flag is from the mboxping tool. It indicates that we cannot
241 	 * drop it. Besides, nodrop pkts have higher priority than normal pkts.
242 	 */
243 	A_BOOL is_nodrop_pkt;
244 
245 	/*
246 	 * Number of WMI endpoints used.
247 	 * Default value is 1. But it should be overidden after htc_create to
248 	 * reflect the actual count.
249 	 */
250 	uint8_t wmi_ep_count;
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 void htc_recv_init(HTC_TARGET *target);
307 QDF_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target);
308 void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
309 HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target);
310 uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id);
311 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID);
312 void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket);
313 void htc_process_credit_rpt(HTC_TARGET *target,
314 			    HTC_CREDIT_REPORT *pRpt,
315 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
316 void htc_fw_event_handler(void *context, QDF_STATUS status);
317 void htc_send_complete_check_cleanup(void *context);
318 #ifdef FEATURE_RUNTIME_PM
319 void htc_kick_queues(void *context);
320 #endif
321 
322 static inline void htc_send_complete_poll_timer_stop(HTC_ENDPOINT *
323 						     pEndpoint) {
324 	LOCK_HTC_TX(pEndpoint->target);
325 	if (pEndpoint->ul_poll_timer_active) {
326 		/* qdf_timer_stop(&pEndpoint->ul_poll_timer); */
327 		pEndpoint->ul_poll_timer_active = 0;
328 	}
329 	UNLOCK_HTC_TX(pEndpoint->target);
330 }
331 
332 static inline void htc_send_complete_poll_timer_start(HTC_ENDPOINT *
333 						      pEndpoint) {
334 	LOCK_HTC_TX(pEndpoint->target);
335 	if (pEndpoint->ul_outstanding_cnt
336 	    && !pEndpoint->ul_poll_timer_active) {
337 		/* qdf_timer_start(
338 		 * &pEndpoint->ul_poll_timer, HTC_POLL_CLEANUP_PERIOD_MS);
339 		 */
340 		pEndpoint->ul_poll_timer_active = 1;
341 	}
342 	UNLOCK_HTC_TX(pEndpoint->target);
343 }
344 
345 static inline void
346 htc_send_complete_check(HTC_ENDPOINT *pEndpoint, int force) {
347 	/*
348 	 * Stop the polling-cleanup timer that will result in a later call to
349 	 * this function.  It may get started again below, if there are still
350 	 * outsending sends.
351 	 */
352 	htc_send_complete_poll_timer_stop(pEndpoint);
353 	/*
354 	 * Check whether HIF has any prior sends that have finished,
355 	 * have not had the post-processing done.
356 	 */
357 	hif_send_complete_check(pEndpoint->target->hif_dev,
358 				pEndpoint->UL_PipeID, force);
359 	/*
360 	 * If there are still outstanding sends after polling, start a timer
361 	 * to check again a little later.
362 	 */
363 	htc_send_complete_poll_timer_start(pEndpoint);
364 }
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #ifndef DEBUG_BUNDLE
371 #define DEBUG_BUNDLE 0
372 #endif
373 
374 #if defined(HIF_SDIO) || defined(HIF_USB)
375 #ifndef ENABLE_BUNDLE_TX
376 #define ENABLE_BUNDLE_TX 1
377 #endif
378 
379 #ifndef ENABLE_BUNDLE_RX
380 #define ENABLE_BUNDLE_RX 1
381 #endif
382 #endif /*defined(HIF_SDIO) || defined(HIF_USB)*/
383 
384 #if defined ENABLE_BUNDLE_TX
385 #define HTC_TX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
386 #else
387 #define HTC_TX_BUNDLE_ENABLED(target) 0
388 #endif
389 
390 #if defined ENABLE_BUNDLE_RX
391 #define HTC_RX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
392 #else
393 #define HTC_RX_BUNDLE_ENABLED(target) 0
394 #endif
395 
396 #define HTC_ENABLE_BUNDLE(target) (target->MaxMsgsPerHTCBundle > 1)
397 
398 #endif /* !_HTC_HOST_INTERNAL_H_ */
399