xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_send.c (revision 8cfe6b10058a04cafb17eed051f2ddf11bee8931)
1 /*
2  * Copyright (c) 2013-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 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 #include "htc_debug.h"
21 #include "htc_internal.h"
22 #include "htc_credit_history.h"
23 #include <qdf_mem.h>            /* qdf_mem_malloc */
24 #include <qdf_nbuf.h>           /* qdf_nbuf_t */
25 #include "qdf_module.h"
26 
27 /* #define USB_HIF_SINGLE_PIPE_DATA_SCHED */
28 /* #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED */
29 #define DATA_EP_SIZE 4
30 /* #endif */
31 #define HTC_DATA_RESOURCE_THRS 256
32 #define HTC_DATA_MINDESC_PERPACKET 2
33 
34 /* maximum number of requeue attempts before print */
35 #define MAX_REQUEUE_WARN 5
36 
37 enum HTC_SEND_QUEUE_RESULT {
38 	HTC_SEND_QUEUE_OK = 0,  /* packet was queued */
39 	HTC_SEND_QUEUE_DROP = 1, /* this packet should be dropped */
40 };
41 
42 #ifndef DEBUG_CREDIT
43 #define DEBUG_CREDIT 0
44 #endif
45 
46 #if DEBUG_CREDIT
47 /* bit mask to enable debug certain endpoint */
48 static unsigned int ep_debug_mask =
49 	(1 << ENDPOINT_0) | (1 << ENDPOINT_1) | (1 << ENDPOINT_2);
50 #endif
51 
52 #ifdef QCA_WIFI_EMULATION
53 #define HTC_EMULATION_DELAY_IN_MS 20
54 /**
55  * htc_add_emulation_delay() - Adds a delay in before proceeding, only for
56  *                             emulation
57  *
58  * Return: None
59  */
60 static inline void htc_add_emulation_delay(void)
61 {
62 	qdf_mdelay(HTC_EMULATION_DELAY_IN_MS);
63 }
64 #else
65 static inline void htc_add_emulation_delay(void)
66 {
67 }
68 #endif
69 
70 void htc_dump_counter_info(HTC_HANDLE HTCHandle)
71 {
72 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
73 
74 	if (!target)
75 		return;
76 
77 	AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
78 			("\n%s: ce_send_cnt = %d, TX_comp_cnt = %d\n",
79 			 __func__, target->ce_send_cnt, target->TX_comp_cnt));
80 }
81 
82 int htc_get_tx_queue_depth(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
83 {
84 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
85 	HTC_ENDPOINT *endpoint = &target->endpoint[endpoint_id];
86 
87 	return HTC_PACKET_QUEUE_DEPTH(&endpoint->TxQueue);
88 }
89 qdf_export_symbol(htc_get_tx_queue_depth);
90 
91 void htc_get_control_endpoint_tx_host_credits(HTC_HANDLE HTCHandle,
92 					      int *credits)
93 {
94 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
95 	HTC_ENDPOINT *pEndpoint;
96 	int i;
97 
98 	if (!credits || !target) {
99 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: invalid args", __func__));
100 		return;
101 	}
102 
103 	*credits = 0;
104 	LOCK_HTC_TX(target);
105 	for (i = 0; i < ENDPOINT_MAX; i++) {
106 		pEndpoint = &target->endpoint[i];
107 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
108 			*credits = pEndpoint->TxCredits;
109 			break;
110 		}
111 	}
112 	UNLOCK_HTC_TX(target);
113 }
114 
115 static inline void restore_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
116 {
117 	qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
118 
119 	if (pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
120 		qdf_nbuf_unmap(target->osdev, netbuf, QDF_DMA_TO_DEVICE);
121 		pPacket->PktInfo.AsTx.Flags &= ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
122 	}
123 	if (pPacket->PktInfo.AsTx.Flags &
124 		HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA) {
125 		qdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
126 		pPacket->PktInfo.AsTx.Flags &=
127 			~HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
128 	}
129 }
130 
131 static void send_packet_completion(HTC_TARGET *target, HTC_PACKET *pPacket)
132 {
133 	HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
134 	HTC_EP_SEND_PKT_COMPLETE EpTxComplete;
135 
136 	if ((pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) &&
137 	    (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)))
138 		target->nbuf_nfc_unmap_count++;
139 
140 	restore_tx_packet(target, pPacket);
141 
142 	/*
143 	 * In case of SSR, we cannot call the upper layer completion
144 	 * callbacks, hence just free the nbuf and HTC packet here.
145 	 */
146 	if (target->hif_dev && hif_get_target_status(target->hif_dev)) {
147 		htc_free_control_tx_packet(target, pPacket);
148 		return;
149 	}
150 
151 	/* do completion */
152 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
153 			("HTC calling ep %d send complete callback on packet %pK\n",
154 			 pEndpoint->Id, pPacket));
155 
156 	EpTxComplete = pEndpoint->EpCallBacks.EpTxComplete;
157 	if (EpTxComplete)
158 		EpTxComplete(pEndpoint->EpCallBacks.pContext, pPacket);
159 	else
160 		qdf_nbuf_free(pPacket->pPktContext);
161 
162 
163 }
164 
165 #ifdef FEATURE_RUNTIME_PM
166 /**
167  * log_packet_info() - Log HTC packet information
168  *
169  * @target: handle of HTC context
170  * @pPacket: handle of HTC packet
171  *
172  * Return: None
173  */
174 static void log_packet_info(HTC_TARGET *target, HTC_PACKET *pPacket)
175 {
176 	HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
177 	HTC_EP_LOG_PKT ep_log_pkt;
178 	qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
179 
180 	ep_log_pkt = pEndpoint->EpCallBacks.ep_log_pkt;
181 	if (ep_log_pkt) {
182 		qdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
183 		ep_log_pkt(pEndpoint->EpCallBacks.pContext, pPacket);
184 		qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
185 	}
186 }
187 
188 /**
189  * htc_inc_htt_runtime_cnt() - Increment htc htt runtime count
190  * @target: handle of HTC context
191  *
192  * Return: None
193  */
194 static inline
195 void htc_inc_htt_runtime_cnt(HTC_TARGET *target)
196 {
197 	qdf_atomic_inc(&target->htc_htt_runtime_cnt);
198 }
199 #else
200 static void log_packet_info(HTC_TARGET *target, HTC_PACKET *pPacket)
201 {
202 }
203 
204 static inline
205 void htc_inc_htt_runtime_cnt(HTC_TARGET *target)
206 {
207 }
208 #endif
209 
210 void htc_send_complete_check_cleanup(void *context)
211 {
212 	HTC_ENDPOINT *pEndpoint = (HTC_ENDPOINT *) context;
213 
214 	htc_send_complete_check(pEndpoint, 1);
215 }
216 
217 HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target)
218 {
219 	HTC_PACKET *pPacket;
220 	HTC_PACKET_QUEUE *pQueueSave;
221 	qdf_nbuf_t netbuf;
222 
223 	LOCK_HTC_TX(target);
224 	if (!target->pBundleFreeList) {
225 		UNLOCK_HTC_TX(target);
226 		netbuf = qdf_nbuf_alloc(NULL,
227 					target->MaxMsgsPerHTCBundle *
228 					target->TargetCreditSize, 0, 4, false);
229 		AR_DEBUG_ASSERT(netbuf);
230 		if (!netbuf)
231 			return NULL;
232 		pPacket = qdf_mem_malloc(sizeof(HTC_PACKET));
233 		AR_DEBUG_ASSERT(pPacket);
234 		if (!pPacket) {
235 			qdf_nbuf_free(netbuf);
236 			return NULL;
237 		}
238 		pQueueSave = qdf_mem_malloc(sizeof(HTC_PACKET_QUEUE));
239 		AR_DEBUG_ASSERT(pQueueSave);
240 		if (!pQueueSave) {
241 			qdf_nbuf_free(netbuf);
242 			qdf_mem_free(pPacket);
243 			return NULL;
244 		}
245 		INIT_HTC_PACKET_QUEUE(pQueueSave);
246 		pPacket->pContext = pQueueSave;
247 		SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
248 		pPacket->pBuffer = qdf_nbuf_data(netbuf);
249 		pPacket->BufferLength = qdf_nbuf_len(netbuf);
250 
251 		/* store the original head room so that we can restore this
252 		 * when we "free" the packet.
253 		 * free packet puts the packet back on the free list
254 		 */
255 		pPacket->netbufOrigHeadRoom = qdf_nbuf_headroom(netbuf);
256 		return pPacket;
257 	}
258 	/* already done malloc - restore from free list */
259 	pPacket = target->pBundleFreeList;
260 	AR_DEBUG_ASSERT(pPacket);
261 	if (!pPacket) {
262 		UNLOCK_HTC_TX(target);
263 		return NULL;
264 	}
265 	target->pBundleFreeList = (HTC_PACKET *) pPacket->ListLink.pNext;
266 	UNLOCK_HTC_TX(target);
267 	pPacket->ListLink.pNext = NULL;
268 
269 	return pPacket;
270 }
271 
272 void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
273 {
274 	uint32_t curentHeadRoom;
275 	qdf_nbuf_t netbuf;
276 	HTC_PACKET_QUEUE *pQueueSave;
277 
278 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
279 	AR_DEBUG_ASSERT(netbuf);
280 	if (!netbuf) {
281 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
282 				("\n%s: Invalid netbuf in HTC Packet\n",
283 				__func__));
284 		return;
285 	}
286 	/* HIF adds data to the headroom section of the nbuf, restore their
287 	 * original size. If this is not done, headroom keeps shrinking with
288 	 * every HIF send and eventually HIF ends up doing another malloc big
289 	 * enough to store the data + its header
290 	 */
291 
292 	curentHeadRoom = qdf_nbuf_headroom(netbuf);
293 	qdf_nbuf_pull_head(netbuf,
294 			   pPacket->netbufOrigHeadRoom - curentHeadRoom);
295 	qdf_nbuf_trim_tail(netbuf, qdf_nbuf_len(netbuf));
296 
297 	/* restore the pBuffer pointer. HIF changes this */
298 	pPacket->pBuffer = qdf_nbuf_data(netbuf);
299 	pPacket->BufferLength = qdf_nbuf_len(netbuf);
300 
301 	/* restore queue */
302 	pQueueSave = (HTC_PACKET_QUEUE *) pPacket->pContext;
303 	if (qdf_unlikely(!pQueueSave)) {
304 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
305 				("\n%s: Invalid pQueueSave in HTC Packet\n",
306 				__func__));
307 		AR_DEBUG_ASSERT(pQueueSave);
308 	} else
309 		INIT_HTC_PACKET_QUEUE(pQueueSave);
310 
311 	LOCK_HTC_TX(target);
312 	if (!target->pBundleFreeList) {
313 		target->pBundleFreeList = pPacket;
314 		pPacket->ListLink.pNext = NULL;
315 	} else {
316 		pPacket->ListLink.pNext = (DL_LIST *) target->pBundleFreeList;
317 		target->pBundleFreeList = pPacket;
318 	}
319 	UNLOCK_HTC_TX(target);
320 }
321 
322 #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
323 
324 /**
325  * htc_send_update_tx_bundle_stats() - update tx bundle stats depends
326  *				on max bundle size
327  * @target: hif context
328  * @data_len: tx data len
329  * @TxCreditSize: endpoint tx credit size
330  *
331  * Return: None
332  */
333 static inline void
334 htc_send_update_tx_bundle_stats(HTC_TARGET *target,
335 				qdf_size_t data_len,
336 				int TxCreditSize)
337 {
338 	int index = ((data_len + TxCreditSize - 1) / TxCreditSize) - 1;
339 
340 	if (index < HTC_MAX_MSG_PER_BUNDLE_TX)
341 		target->tx_bundle_stats[index]++;
342 }
343 
344 /**
345  * htc_issue_tx_bundle_stats_inc() - increment in tx bundle stats
346  *				on max bundle size
347  * @target: hif context
348  *
349  * Return: None
350  */
351 static inline void
352 htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
353 {
354 	target->tx_bundle_stats[0]++;
355 }
356 #else
357 
358 static inline void
359 htc_send_update_tx_bundle_stats(HTC_TARGET *target,
360 				qdf_size_t data_len,
361 				int TxCreditSize)
362 {
363 }
364 
365 static inline void
366 htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
367 {
368 }
369 #endif
370 
371 #if defined(HIF_USB) || defined(HIF_SDIO)
372 #ifdef ENABLE_BUNDLE_TX
373 static QDF_STATUS htc_send_bundled_netbuf(HTC_TARGET *target,
374 					HTC_ENDPOINT *pEndpoint,
375 					unsigned char *pBundleBuffer,
376 					HTC_PACKET *pPacketTx)
377 {
378 	qdf_size_t data_len;
379 	QDF_STATUS status;
380 	qdf_nbuf_t bundleBuf;
381 	uint32_t data_attr = 0;
382 
383 	bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
384 	data_len = pBundleBuffer - qdf_nbuf_data(bundleBuf);
385 	qdf_nbuf_put_tail(bundleBuf, data_len);
386 	SET_HTC_PACKET_INFO_TX(pPacketTx,
387 			       target,
388 			       pBundleBuffer,
389 			       data_len,
390 			       pEndpoint->Id, HTC_TX_PACKET_TAG_BUNDLED);
391 	LOCK_HTC_TX(target);
392 	HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacketTx);
393 	pEndpoint->ul_outstanding_cnt++;
394 	UNLOCK_HTC_TX(target);
395 #if DEBUG_BUNDLE
396 	qdf_print(" Send bundle EP%d buffer size:0x%x, total:0x%x, count:%d.",
397 		  pEndpoint->Id,
398 		  pEndpoint->TxCreditSize,
399 		  data_len, data_len / pEndpoint->TxCreditSize);
400 #endif
401 
402 	htc_send_update_tx_bundle_stats(target, data_len,
403 					pEndpoint->TxCreditSize);
404 
405 	status = hif_send_head(target->hif_dev,
406 			       pEndpoint->UL_PipeID,
407 			       pEndpoint->Id, data_len,
408 			       bundleBuf, data_attr);
409 	if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
410 		HTC_PACKET_QUEUE requeue;
411 
412 		qdf_print("hif_send_head failed(len=%zu).", data_len);
413 		INIT_HTC_PACKET_QUEUE(&requeue);
414 		LOCK_HTC_TX(target);
415 		pEndpoint->ul_outstanding_cnt--;
416 		HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacketTx);
417 
418 		if (pPacketTx->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
419 			HTC_PACKET *temp_packet;
420 			HTC_PACKET_QUEUE *packet_queue =
421 				(HTC_PACKET_QUEUE *)pPacketTx->pContext;
422 
423 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(packet_queue,
424 							      temp_packet) {
425 				HTC_PACKET_ENQUEUE(&requeue, temp_packet);
426 			} HTC_PACKET_QUEUE_ITERATE_END;
427 
428 			UNLOCK_HTC_TX(target);
429 			free_htc_bundle_packet(target, pPacketTx);
430 			LOCK_HTC_TX(target);
431 
432 		} else {
433 			HTC_PACKET_ENQUEUE(&requeue, pPacketTx);
434 		}
435 
436 		HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
437 						  &requeue);
438 		UNLOCK_HTC_TX(target);
439 	}
440 	return status;
441 }
442 
443 #ifdef QCA_TX_PADDING_CREDIT_SUPPORT
444 #define SDIO_BLOCK_SIZE		512
445 static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
446 {
447 	int ret = 0;
448 
449 	if (!ep || !ep->EpCallBacks.pContext ||
450 	    !ep->EpCallBacks.ep_padding_credit_update)
451 		return 1;
452 
453 	ret = ep->EpCallBacks.ep_padding_credit_update(ep->EpCallBacks.pContext,
454 						       0);
455 
456 	if (ret < 2)
457 		AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s ret %d\n", __func__, ret));
458 
459 	return ret;
460 }
461 
462 static bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
463 				       HTC_PACKET *p_last_htc_pkt,
464 				       unsigned char *p_last_pkt_bundle_buffer,
465 				       unsigned char **p_bundle_buffer,
466 				       int tot_data_len)
467 {
468 	bool extra_tx_credit = FALSE;
469 	HTC_FRAME_HDR *p_htc_hdr;
470 	int first_buf_bundled_len = 0, last_buf_len = 0;
471 	int sdio_pad = 0, free_space = 0;
472 	int (*update_ep_padding_credit)(void *, int);
473 
474 	update_ep_padding_credit = ep->EpCallBacks.ep_padding_credit_update;
475 
476 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
477 			("%s Tot data_len = %d\n", __func__, tot_data_len));
478 
479 	if (!p_last_htc_pkt)
480 		return extra_tx_credit;
481 
482 	last_buf_len = (p_last_htc_pkt->ActualLength + HTC_HDR_LENGTH);
483 	if (tot_data_len != last_buf_len) {
484 		first_buf_bundled_len = tot_data_len - ep->TxCreditSize;
485 		free_space = tot_data_len -
486 					(first_buf_bundled_len + last_buf_len);
487 	} else {
488 		free_space = ep->TxCreditSize - tot_data_len;
489 	}
490 
491 	sdio_pad = SDIO_BLOCK_SIZE - ((first_buf_bundled_len + last_buf_len) %
492 			SDIO_BLOCK_SIZE);
493 
494 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
495 			("%s first_buf_bundled_len = %d last_buf_len = %d\n",
496 			__func__, first_buf_bundled_len, last_buf_len));
497 
498 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
499 			("%s sdio_pad = %d free_space = %d\n", __func__,
500 			sdio_pad, free_space));
501 
502 	if (sdio_pad <= free_space) {
503 		if (p_bundle_buffer && *p_bundle_buffer) {
504 			/* Align Tx bundled buf to avoid a extra Padding buf */
505 			*p_bundle_buffer -= (free_space - sdio_pad);
506 		}
507 	} else {
508 		/* Extra Padding Buffer needed, consume extra tx credit */
509 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
510 				("%s Used a Tx credit for Padding Buffer\n",
511 				 __func__));
512 		p_htc_hdr = (HTC_FRAME_HDR *)(p_last_pkt_bundle_buffer);
513 		p_htc_hdr->Flags |= HTC_FLAGS_PADDING_CHECK;
514 		extra_tx_credit = TRUE;
515 		if (ep->EpCallBacks.ep_padding_credit_update) {
516 			/* Decrement 1 credit at host,
517 			 * due to extra tx credit consumed by padding buffer
518 			 */
519 			update_ep_padding_credit(ep->EpCallBacks.pContext, -1);
520 		}
521 	}
522 	return extra_tx_credit;
523 }
524 #else
525 static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
526 {
527 	return 1;
528 }
529 
530 static bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
531 				       HTC_PACKET *p_last_htc_pkt,
532 				       unsigned char *p_last_pkt_bundle_buffer,
533 				       unsigned char **p_bundle_buffer,
534 				       int tot_data_len)
535 {
536 	return FALSE;
537 }
538 #endif
539 
540 /**
541  * htc_issue_packets_bundle() - HTC function to send bundle packets from a queue
542  * @target: HTC target on which packets need to be sent
543  * @pEndpoint: logical endpoint on which packets needs to be sent
544  * @pPktQueue: HTC packet queue containing the list of packets to be sent
545  *
546  * Return: void
547  */
548 static void htc_issue_packets_bundle(HTC_TARGET *target,
549 				     HTC_ENDPOINT *pEndpoint,
550 				     HTC_PACKET_QUEUE *pPktQueue)
551 {
552 	int i, frag_count, nbytes;
553 	qdf_nbuf_t netbuf, bundleBuf;
554 	unsigned char *pBundleBuffer = NULL;
555 	HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
556 	HTC_FRAME_HDR *pHtcHdr;
557 	int last_credit_pad = 0;
558 	int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
559 		0;
560 	HTC_PACKET_QUEUE *pQueueSave = NULL;
561 	HTC_PACKET *p_last_htc_pkt = NULL;
562 	unsigned char *p_last_pkt_bundle_buffer = NULL;
563 
564 	bundlesSpaceRemaining =
565 		target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
566 	pPacketTx = allocate_htc_bundle_packet(target);
567 	if (!pPacketTx) {
568 		/* good time to panic */
569 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
570 				("allocate_htc_bundle_packet failed\n"));
571 		AR_DEBUG_ASSERT(false);
572 		return;
573 	}
574 	bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
575 	pBundleBuffer = qdf_nbuf_data(bundleBuf);
576 	pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
577 	while (1) {
578 		if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
579 			if (htc_tx_pad_credit_avail(pEndpoint) < 1)
580 				break;
581 		}
582 		pPacket = htc_packet_dequeue(pPktQueue);
583 		if (!pPacket)
584 			break;
585 		creditPad = 0;
586 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
587 		creditRemainder = transferLength % pEndpoint->TxCreditSize;
588 		if (creditRemainder != 0) {
589 			if (transferLength < pEndpoint->TxCreditSize) {
590 				creditPad = pEndpoint->TxCreditSize -
591 					    transferLength;
592 			} else {
593 				creditPad = creditRemainder;
594 			}
595 			transferLength += creditPad;
596 		}
597 
598 		if (bundlesSpaceRemaining < transferLength) {
599 			htc_handle_extra_tx_credit(pEndpoint, p_last_htc_pkt,
600 						   p_last_pkt_bundle_buffer,
601 						   &pBundleBuffer,
602 						   pBundleBuffer -
603 						   qdf_nbuf_data(bundleBuf));
604 
605 			/* send out previous buffer */
606 			htc_send_bundled_netbuf(target, pEndpoint,
607 						pBundleBuffer - last_credit_pad,
608 						pPacketTx);
609 			/* One packet has been dequeued from sending queue when enter
610 			 * this loop, so need to add 1 back for this checking.
611 			 */
612 			if ((HTC_PACKET_QUEUE_DEPTH(pPktQueue) + 1) <
613 			    HTC_MIN_MSG_PER_BUNDLE) {
614 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
615 				return;
616 			}
617 			bundlesSpaceRemaining =
618 				target->MaxMsgsPerHTCBundle *
619 				pEndpoint->TxCreditSize;
620 			pPacketTx = allocate_htc_bundle_packet(target);
621 			if (!pPacketTx) {
622 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
623 				/* good time to panic */
624 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
625 						("allocate_htc_bundle_packet failed\n"));
626 				AR_DEBUG_ASSERT(false);
627 				return;
628 			}
629 			bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
630 			pBundleBuffer = qdf_nbuf_data(bundleBuf);
631 			pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
632 		}
633 
634 		p_last_htc_pkt = pPacket;
635 		p_last_pkt_bundle_buffer = pBundleBuffer;
636 
637 		bundlesSpaceRemaining -= transferLength;
638 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
639 
640 		if (hif_get_bus_type(target->hif_dev) != QDF_BUS_TYPE_USB) {
641 			pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(
642 								netbuf, 0);
643 			HTC_WRITE32(pHtcHdr,
644 				SM(pPacket->ActualLength,
645 				HTC_FRAME_HDR_PAYLOADLEN) |
646 				SM(pPacket->PktInfo.AsTx.SendFlags |
647 				HTC_FLAGS_SEND_BUNDLE,
648 				HTC_FRAME_HDR_FLAGS) |
649 				SM(pPacket->Endpoint,
650 				HTC_FRAME_HDR_ENDPOINTID));
651 			HTC_WRITE32((uint32_t *) pHtcHdr + 1,
652 				SM(pPacket->PktInfo.AsTx.SeqNo,
653 				HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
654 				HTC_FRAME_HDR_RESERVED));
655 			pHtcHdr->reserved = creditPad;
656 		}
657 		frag_count = qdf_nbuf_get_num_frags(netbuf);
658 		nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
659 		for (i = 0; i < frag_count && nbytes > 0; i++) {
660 			int frag_len = qdf_nbuf_get_frag_len(netbuf, i);
661 			unsigned char *frag_addr =
662 				qdf_nbuf_get_frag_vaddr(netbuf, i);
663 			if (frag_len > nbytes)
664 				frag_len = nbytes;
665 			qdf_mem_copy(pBundleBuffer, frag_addr, frag_len);
666 			nbytes -= frag_len;
667 			pBundleBuffer += frag_len;
668 		}
669 		HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
670 		pBundleBuffer += creditPad;
671 
672 		/* last one can't be packed. */
673 		if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)
674 			last_credit_pad = creditPad;
675 	}
676 	/* send out remaining buffer */
677 	if (pBundleBuffer != qdf_nbuf_data(bundleBuf)) {
678 		htc_handle_extra_tx_credit(pEndpoint, p_last_htc_pkt,
679 					   p_last_pkt_bundle_buffer,
680 					   &pBundleBuffer,
681 					   pBundleBuffer -
682 					   qdf_nbuf_data(bundleBuf));
683 
684 		htc_send_bundled_netbuf(target, pEndpoint,
685 					pBundleBuffer - last_credit_pad,
686 					pPacketTx);
687 	} else {
688 		free_htc_bundle_packet(target, pPacketTx);
689 	}
690 }
691 #endif /* ENABLE_BUNDLE_TX */
692 #else
693 static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
694 {
695 	return 1;
696 }
697 
698 bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
699 				HTC_PACKET *p_last_htc_pkt,
700 				unsigned char *p_last_pkt_bundle_buffer,
701 				unsigned char **p_bundle_buffer,
702 				int tot_data_len);
703 bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
704 				HTC_PACKET *p_last_htc_pkt,
705 				unsigned char *p_last_pkt_bundle_buffer,
706 				unsigned char **p_bundle_buffer,
707 				int tot_data_len)
708 {
709 	return FALSE;
710 }
711 
712 static void htc_issue_packets_bundle(HTC_TARGET *target,
713 				     HTC_ENDPOINT *pEndpoint,
714 				     HTC_PACKET_QUEUE *pPktQueue)
715 {
716 }
717 #endif
718 
719 /**
720  * htc_issue_packets() - HTC function to send packets from a queue
721  * @target: HTC target on which packets need to be sent
722  * @pEndpoint: logical endpoint on which packets needs to be sent
723  * @pPktQueue: HTC packet queue containing the list of packets to be sent
724  *
725  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
726  */
727 static QDF_STATUS htc_issue_packets(HTC_TARGET *target,
728 				  HTC_ENDPOINT *pEndpoint,
729 				  HTC_PACKET_QUEUE *pPktQueue)
730 {
731 	QDF_STATUS status = QDF_STATUS_SUCCESS;
732 	qdf_nbuf_t netbuf;
733 	HTC_PACKET *pPacket = NULL;
734 	uint16_t payloadLen;
735 	HTC_FRAME_HDR *pHtcHdr;
736 	uint32_t data_attr = 0;
737 	enum qdf_bus_type bus_type;
738 	QDF_STATUS ret;
739 	bool rt_put = false;
740 	bool used_extra_tx_credit = false;
741 	uint8_t *buf = NULL;
742 	int (*update_ep_padding_credit)(void *, int);
743 	void *ctx = NULL;
744 	bool rt_put_in_resp;
745 	int32_t sys_state = HIF_SYSTEM_PM_STATE_ON;
746 
747 	update_ep_padding_credit =
748 			pEndpoint->EpCallBacks.ep_padding_credit_update;
749 
750 	bus_type = hif_get_bus_type(target->hif_dev);
751 
752 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
753 			("+htc_issue_packets: Queue: %pK, Pkts %d\n", pPktQueue,
754 			 HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
755 	while (true) {
756 		rt_put_in_resp = false;
757 		if (HTC_TX_BUNDLE_ENABLED(target) &&
758 		    HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
759 		    HTC_MIN_MSG_PER_BUNDLE) {
760 			switch (bus_type) {
761 			case QDF_BUS_TYPE_SDIO:
762 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
763 					break;
764 				if (update_ep_padding_credit) {
765 					if (htc_tx_pad_credit_avail
766 					    (pEndpoint) < 1)
767 						break;
768 				}
769 				fallthrough;
770 			case QDF_BUS_TYPE_USB:
771 				htc_issue_packets_bundle(target,
772 							pEndpoint,
773 							pPktQueue);
774 				break;
775 			default:
776 				break;
777 			}
778 		}
779 		/* if not bundling or there was a packet that could not be
780 		 * placed in a bundle, and send it by normal way
781 		 */
782 		if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
783 			if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
784 				status = QDF_STATUS_E_FAILURE;
785 				break;
786 			}
787 		}
788 
789 		pPacket = htc_packet_dequeue(pPktQueue);
790 		if (!pPacket) {
791 			/* local queue is fully drained */
792 			break;
793 		}
794 
795 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
796 		AR_DEBUG_ASSERT(netbuf);
797 		/* Non-credit enabled endpoints have been mapped and setup by
798 		 * now, so no need to revisit the HTC headers
799 		 */
800 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
801 
802 			payloadLen = pPacket->ActualLength;
803 			/* setup HTC frame header */
804 
805 			pHtcHdr = (HTC_FRAME_HDR *)
806 				qdf_nbuf_get_frag_vaddr(netbuf, 0);
807 			if (qdf_unlikely(!pHtcHdr)) {
808 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
809 						("%s Invalid pHtcHdr\n",
810 						 __func__));
811 				AR_DEBUG_ASSERT(pHtcHdr);
812 				status = QDF_STATUS_E_FAILURE;
813 				break;
814 			}
815 
816 			HTC_WRITE32(pHtcHdr,
817 					SM(payloadLen,
818 						HTC_FRAME_HDR_PAYLOADLEN) |
819 					SM(pPacket->PktInfo.AsTx.SendFlags,
820 						HTC_FRAME_HDR_FLAGS) |
821 					SM(pPacket->Endpoint,
822 						HTC_FRAME_HDR_ENDPOINTID));
823 			HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
824 				    SM(pPacket->PktInfo.AsTx.SeqNo,
825 				       HTC_FRAME_HDR_CONTROLBYTES1));
826 
827 			/*
828 			 * Now that the HTC frame header has been added, the
829 			 * netbuf can be mapped.  This only applies to non-data
830 			 * frames, since data frames were already mapped as they
831 			 * entered into the driver.
832 			 */
833 			ret = qdf_nbuf_map(target->osdev,
834 				GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
835 				QDF_DMA_TO_DEVICE);
836 			if (ret != QDF_STATUS_SUCCESS) {
837 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
838 					("%s nbuf Map Fail Endpnt %pK\n",
839 					__func__, pEndpoint));
840 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
841 				status = QDF_STATUS_E_FAILURE;
842 				break;
843 			}
844 			pPacket->PktInfo.AsTx.Flags |=
845 				HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
846 		}
847 
848 		if (!pEndpoint->async_update) {
849 			LOCK_HTC_TX(target);
850 		}
851 		/* store in look up queue to match completions */
852 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
853 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
854 		pEndpoint->ul_outstanding_cnt++;
855 		if (!pEndpoint->async_update) {
856 			UNLOCK_HTC_TX(target);
857 			hif_send_complete_check(target->hif_dev,
858 					pEndpoint->UL_PipeID, false);
859 		}
860 
861 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_SYSTEM_SUSPEND) {
862 			sys_state = hif_system_pm_get_state(target->hif_dev);
863 			hif_system_pm_set_state_suspending(target->hif_dev);
864 		}
865 
866 		htc_packet_set_magic_cookie(pPacket, HTC_PACKET_MAGIC_COOKIE);
867 		/*
868 		 * For HTT messages without a response from fw,
869 		 *   do the runtime put here.
870 		 * otherwise runtime put will be done when the fw response comes
871 		 */
872 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT) {
873 			rt_put = true;
874 		} else if (pPacket->PktInfo.AsTx.Tag ==
875 			 HTC_TX_PACKET_TAG_RTPM_PUT_RC) {
876 			rt_put_in_resp = true;
877 			htc_inc_htt_runtime_cnt(target);
878 		}
879 
880 #if DEBUG_BUNDLE
881 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
882 			  pEndpoint->Id,
883 			  pEndpoint->TxCreditSize,
884 			  HTC_HDR_LENGTH + pPacket->ActualLength);
885 #endif
886 		buf = (uint8_t *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
887 		used_extra_tx_credit =
888 			htc_handle_extra_tx_credit(pEndpoint, pPacket, buf,
889 						   NULL, pPacket->ActualLength +
890 						   HTC_HDR_LENGTH);
891 
892 		status = hif_send_head(target->hif_dev,
893 				       pEndpoint->UL_PipeID, pEndpoint->Id,
894 				       HTC_HDR_LENGTH + pPacket->ActualLength,
895 				       netbuf, data_attr);
896 
897 		if (status != QDF_STATUS_SUCCESS) {
898 			if (rt_put_in_resp)
899 				htc_dec_return_htt_runtime_cnt((void *)target);
900 
901 			if (pPacket->PktInfo.AsTx.Tag ==
902 			    HTC_TX_PACKET_SYSTEM_SUSPEND)
903 				__hif_system_pm_set_state(target->hif_dev,
904 							  sys_state);
905 
906 			if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
907 				if (used_extra_tx_credit) {
908 					ctx = pEndpoint->EpCallBacks.pContext;
909 					update_ep_padding_credit(ctx, 1);
910 				}
911 			}
912 		}
913 
914 		htc_issue_tx_bundle_stats_inc(target);
915 
916 		target->ce_send_cnt++;
917 		pEndpoint->htc_send_cnt++;
918 
919 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
920 			if (status != QDF_STATUS_E_RESOURCES) {
921 				/* TODO : if more than 1 endpoint maps to the
922 				 * same PipeID it is possible to run out of
923 				 * resources in the HIF layer. Don't emit the
924 				 * error
925 				 */
926 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
927 						("hif_send Failed status:%d\n",
928 						 status));
929 			} else {
930 				if (target->htc_pkt_dbg) {
931 					if (pEndpoint->num_requeues_warn >
932 						MAX_REQUEUE_WARN) {
933 						hif_print_napi_stats(target->hif_dev);
934 					}
935 				}
936 			}
937 
938 			/* only unmap if we mapped in this function */
939 			if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
940 				qdf_nbuf_unmap(target->osdev,
941 					GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
942 					QDF_DMA_TO_DEVICE);
943 				pPacket->PktInfo.AsTx.Flags &=
944 					~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
945 			}
946 
947 			if (!pEndpoint->async_update) {
948 				LOCK_HTC_TX(target);
949 			}
950 			target->ce_send_cnt--;
951 			pEndpoint->htc_send_cnt--;
952 			pEndpoint->ul_outstanding_cnt--;
953 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
954 			htc_packet_set_magic_cookie(pPacket, 0);
955 			/* put it back into the callers queue */
956 			HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
957 			/* reclaim credits */
958 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue,
959 							      pPacket) {
960 			    pEndpoint->TxCredits +=
961 				pPacket->PktInfo.AsTx.CreditsUsed;
962 			} HTC_PACKET_QUEUE_ITERATE_END;
963 			if (!pEndpoint->async_update) {
964 				UNLOCK_HTC_TX(target);
965 			}
966 			break;
967 		}
968 		if (rt_put) {
969 			hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_HTT);
970 			rt_put = false;
971 		}
972 	}
973 
974 	if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
975 		if (((status == QDF_STATUS_E_RESOURCES) &&
976 		     (pEndpoint->num_requeues_warn > MAX_REQUEUE_WARN)) ||
977 		     (status != QDF_STATUS_E_RESOURCES)) {
978 			QDF_TRACE(QDF_MODULE_ID_HIF, QDF_TRACE_LEVEL_INFO,
979 				  "failed pkt:0x%pK status:%d endpoint:%d",
980 				  pPacket, status, pEndpoint->Id);
981 		}
982 
983 	}
984 
985 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets\n"));
986 
987 	return status;
988 }
989 
990 #ifdef FEATURE_RUNTIME_PM
991 /**
992  * extract_htc_pm_packets() - move pm packets from endpoint into queue
993  * @endpoint: which endpoint to extract packets from
994  * @queue: a queue to store extracted packets in.
995  *
996  * remove pm packets from the endpoint's tx queue.
997  * queue them into a queue
998  */
999 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
1000 				HTC_PACKET_QUEUE *queue)
1001 {
1002 	HTC_PACKET *packet;
1003 
1004 	/* only WMI endpoint has power management packets */
1005 	if (endpoint->service_id != WMI_CONTROL_SVC)
1006 		return;
1007 
1008 	ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
1009 			HTC_PACKET, ListLink) {
1010 		if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
1011 			HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
1012 			HTC_PACKET_ENQUEUE(queue, packet);
1013 		}
1014 	} ITERATE_END
1015 }
1016 
1017 /**
1018  * queue_htc_pm_packets() - queue pm packets with priority
1019  * @endpoint: endpoint to queue packets to
1020  * @queue: queue of pm packets to enqueue
1021  *
1022  * suspend resume packets get special treatment & priority.
1023  * need to queue them at the front of the queue.
1024  */
1025 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
1026 				 HTC_PACKET_QUEUE *queue)
1027 {
1028 	if (endpoint->service_id != WMI_CONTROL_SVC)
1029 		return;
1030 
1031 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
1032 }
1033 
1034 /**
1035  * htc_dec_wmi_runtime_cnt: Decrement htc wmi runtime count
1036  * @target: HTC target
1037  * @rtpm_code: RTPM code
1038  *
1039  * Return: None
1040  */
1041 static inline
1042 void htc_dec_wmi_runtime_cnt(HTC_TARGET *target, uint8_t rtpm_code)
1043 {
1044 	if (rtpm_code == HIF_RTPM_ID_WMI)
1045 		qdf_atomic_dec(&target->htc_wmi_runtime_cnt);
1046 }
1047 
1048 /**
1049  * htc_inc_wmi_runtime_cnt: Increment htc wmi runtime count
1050  * @target: HTC target
1051  * @rtpm_code: RTPM code
1052  *
1053  * Return: None
1054  */
1055 static inline
1056 void htc_inc_wmi_runtime_cnt(HTC_TARGET *target, uint8_t rtpm_code)
1057 {
1058 	if (rtpm_code == HIF_RTPM_ID_WMI)
1059 		qdf_atomic_inc(&target->htc_wmi_runtime_cnt);
1060 }
1061 #else
1062 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
1063 		HTC_PACKET_QUEUE *queue)
1064 {}
1065 
1066 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
1067 		HTC_PACKET_QUEUE *queue)
1068 {}
1069 
1070 static inline
1071 void htc_dec_wmi_runtime_cnt(HTC_TARGET *target, uint8_t rtpm_code)
1072 {
1073 }
1074 
1075 static inline
1076 void htc_inc_wmi_runtime_cnt(HTC_TARGET *target, uint8_t rtpm_code)
1077 {
1078 }
1079 #endif
1080 
1081 /**
1082  * htc_send_pkts_get_rtpm_id() - get runtime pm dbgid by service_id
1083  * @service_id: service for endpoint
1084  *
1085  * For service_id HTT_DATA_MSG_SVC, HTT message donot have a tx complete
1086  * from CE level, so they need runtime put which only can happen in fw
1087  * response. runtime put will happens at 2 ways.
1088  *    1 if packet tag HTC_TX_PACKET_TAG_RUNTIME_PUT, runtime put
1089  *      will be just in htc_issue_packets. as such pkt doesn't have
1090  *      a response from fw.
1091  *    2 other pkt must have a response from fw, it will be handled
1092  *      by fw response using htc_pm_runtime_put.
1093  *
1094  * For other service_id, they have tx_completion from CE, so they will be
1095  * handled in htc_tx_completion_handler, except packet tag as
1096  * HTC_TX_PACKET_TAG_AUTO_PM, pm related wmi cmd don't need a runtime
1097  * put/get.
1098  *
1099  *
1100  * Return: rtpm id to trace who used it
1101  */
1102 static unsigned int
1103 htc_send_pkts_get_rtpm_id(HTC_SERVICE_ID service_id)
1104 {
1105 	if (service_id == HTT_DATA_MSG_SVC)
1106 		return HIF_RTPM_ID_HTT;
1107 	else
1108 		return HIF_RTPM_ID_WMI;
1109 }
1110 
1111 #ifdef SYSTEM_PM_CHECK
1112 /**
1113  * extract_htc_system_resume_pkts() - Move system pm resume packets from
1114  *                                    endpoint into queue
1115  * @endpoint: which endpoint to extract packets from
1116  * @queue: a queue to store extracted packets in.
1117  *
1118  * Remove pm packets from the endpoint's tx queue and enqueue
1119  * them into a queue
1120  */
1121 static void extract_htc_system_resume_pkts(HTC_ENDPOINT *endpoint,
1122 					   HTC_PACKET_QUEUE *queue)
1123 {
1124 	HTC_PACKET *packet;
1125 
1126 	/* only WMI endpoint has power management packets */
1127 	if (endpoint->service_id != WMI_CONTROL_SVC)
1128 		return;
1129 
1130 	ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
1131 				       HTC_PACKET, ListLink) {
1132 		if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_SYSTEM_RESUME) {
1133 			HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
1134 			HTC_PACKET_ENQUEUE(queue, packet);
1135 		}
1136 	} ITERATE_END
1137 }
1138 #else
1139 static inline
1140 void extract_htc_system_resume_pkts(HTC_ENDPOINT *endpoint,
1141 				    HTC_PACKET_QUEUE *queue)
1142 {
1143 }
1144 #endif
1145 
1146 /**
1147  * get_htc_send_packets_credit_based() - get packets based on available credits
1148  * @target: HTC target on which packets need to be sent
1149  * @pEndpoint: logical endpoint on which packets needs to be sent
1150  * @pQueue: HTC packet queue containing the list of packets to be sent
1151  *
1152  * Get HTC send packets from TX queue on an endpoint based on available credits.
1153  * The function moves the packets from TX queue of the endpoint to pQueue.
1154  *
1155  * Return: None
1156  */
1157 static void get_htc_send_packets_credit_based(HTC_TARGET *target,
1158 					      HTC_ENDPOINT *pEndpoint,
1159 					      HTC_PACKET_QUEUE *pQueue)
1160 {
1161 	int creditsRequired;
1162 	int remainder;
1163 	uint8_t sendFlags;
1164 	HTC_PACKET *pPacket;
1165 	unsigned int transferLength;
1166 	HTC_PACKET_QUEUE *tx_queue;
1167 	HTC_PACKET_QUEUE pm_queue;
1168 	bool do_pm_get = false;
1169 	unsigned int rtpm_code = 0;
1170 	int ret;
1171 	HTC_PACKET_QUEUE sys_pm_queue;
1172 	bool sys_pm_check = false;
1173 
1174 	/*** NOTE : the TX lock is held when this function is called ***/
1175 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1176 			("+get_htc_send_packets_credit_based\n"));
1177 
1178 	INIT_HTC_PACKET_QUEUE(&pm_queue);
1179 	extract_htc_pm_packets(pEndpoint, &pm_queue);
1180 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
1181 		do_pm_get = true;
1182 
1183 		INIT_HTC_PACKET_QUEUE(&sys_pm_queue);
1184 		extract_htc_system_resume_pkts(pEndpoint, &sys_pm_queue);
1185 		if (HTC_QUEUE_EMPTY(&sys_pm_queue)) {
1186 			tx_queue = &pEndpoint->TxQueue;
1187 			sys_pm_check = true;
1188 		} else {
1189 			tx_queue = &sys_pm_queue;
1190 		}
1191 	} else {
1192 		tx_queue = &pm_queue;
1193 	}
1194 
1195 	/* loop until we can grab as many packets out of the queue as we can */
1196 	while (true) {
1197 		if (do_pm_get) {
1198 			rtpm_code = htc_send_pkts_get_rtpm_id(
1199 							pEndpoint->service_id);
1200 			ret = hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code);
1201 			if (ret) {
1202 				/* bus suspended, runtime resume issued */
1203 				QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
1204 				pPacket = htc_get_pkt_at_head(tx_queue);
1205 				if (!pPacket)
1206 					break;
1207 				log_packet_info(target, pPacket);
1208 				break;
1209 			}
1210 			htc_inc_wmi_runtime_cnt(target, rtpm_code);
1211 		}
1212 
1213 		sendFlags = 0;
1214 		/* get packet at head, but don't remove it */
1215 		pPacket = htc_get_pkt_at_head(tx_queue);
1216 		if (!pPacket) {
1217 			if (do_pm_get) {
1218 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1219 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1220 			}
1221 			break;
1222 		}
1223 
1224 		if (sys_pm_check &&
1225 		    hif_system_pm_state_check(target->hif_dev)) {
1226 			if (do_pm_get) {
1227 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1228 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1229 			}
1230 			break;
1231 		}
1232 
1233 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1234 				(" Got head packet:%pK , Queue Depth: %d\n",
1235 				 pPacket,
1236 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
1237 
1238 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
1239 
1240 		if (transferLength <= pEndpoint->TxCreditSize) {
1241 			creditsRequired = 1;
1242 		} else {
1243 			/* figure out how many credits this message requires */
1244 			creditsRequired =
1245 				transferLength / pEndpoint->TxCreditSize;
1246 			remainder = transferLength % pEndpoint->TxCreditSize;
1247 
1248 			if (remainder)
1249 				creditsRequired++;
1250 		}
1251 
1252 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1253 				(" Credits Required:%d   Got:%d\n",
1254 				 creditsRequired, pEndpoint->TxCredits));
1255 
1256 		if (pEndpoint->Id == ENDPOINT_0) {
1257 			/*
1258 			 * endpoint 0 is special, it always has a credit and
1259 			 * does not require credit based flow control
1260 			 */
1261 			creditsRequired = 0;
1262 		} else {
1263 
1264 			if (pEndpoint->TxCredits < creditsRequired) {
1265 #if DEBUG_CREDIT
1266 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1267 						("EP%d,No Credit now.%d < %d\n",
1268 						 pEndpoint->Id,
1269 						 pEndpoint->TxCredits,
1270 						 creditsRequired));
1271 #endif
1272 				if (do_pm_get) {
1273 					hif_rtpm_put(HIF_RTPM_PUT_ASYNC,
1274 						     rtpm_code);
1275 					htc_dec_wmi_runtime_cnt(target,
1276 								rtpm_code);
1277 				}
1278 
1279 				break;
1280 			}
1281 
1282 			pEndpoint->TxCredits -= creditsRequired;
1283 			INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
1284 					creditsRequired);
1285 
1286 			/* check if we need credits back from the target */
1287 			if (pEndpoint->TxCredits <=
1288 			    pEndpoint->TxCreditsPerMaxMsg) {
1289 				/* tell the target we need credits ASAP! */
1290 				sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
1291 				if (pEndpoint->service_id == WMI_CONTROL_SVC) {
1292 					htc_credit_record(HTC_REQUEST_CREDIT,
1293 							  pEndpoint->TxCredits,
1294 							  HTC_PACKET_QUEUE_DEPTH
1295 							  (tx_queue));
1296 					hif_latency_detect_credit_record_time(
1297 						HIF_REQUEST_CREDIT,
1298 						target->hif_dev);
1299 				}
1300 				INC_HTC_EP_STAT(pEndpoint,
1301 						TxCreditLowIndications, 1);
1302 #if DEBUG_CREDIT
1303 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1304 						(" EP%d Needs Credits\n",
1305 						 pEndpoint->Id));
1306 #endif
1307 			}
1308 		}
1309 
1310 		/* now we can fully dequeue */
1311 		pPacket = htc_packet_dequeue(tx_queue);
1312 		if (pPacket) {
1313 			/* save the number of credits this packet consumed */
1314 			pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
1315 			/* save send flags */
1316 			pPacket->PktInfo.AsTx.SendFlags = sendFlags;
1317 
1318 			/* queue this packet into the caller's queue */
1319 			HTC_PACKET_ENQUEUE(pQueue, pPacket);
1320 		}
1321 	}
1322 
1323 	if (!HTC_QUEUE_EMPTY(&pm_queue))
1324 		queue_htc_pm_packets(pEndpoint, &pm_queue);
1325 
1326 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1327 			("-get_htc_send_packets_credit_based\n"));
1328 
1329 }
1330 
1331 static void get_htc_send_packets(HTC_TARGET *target,
1332 				 HTC_ENDPOINT *pEndpoint,
1333 				 HTC_PACKET_QUEUE *pQueue, int Resources)
1334 {
1335 
1336 	HTC_PACKET *pPacket;
1337 	HTC_PACKET_QUEUE *tx_queue;
1338 	HTC_PACKET_QUEUE pm_queue;
1339 	bool do_pm_get = false;
1340 	unsigned int rtpm_code = 0;
1341 	int ret;
1342 
1343 	/*** NOTE : the TX lock is held when this function is called ***/
1344 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1345 			("+get_htc_send_packets %d resources\n", Resources));
1346 
1347 	INIT_HTC_PACKET_QUEUE(&pm_queue);
1348 	extract_htc_pm_packets(pEndpoint, &pm_queue);
1349 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
1350 		tx_queue = &pEndpoint->TxQueue;
1351 		do_pm_get = true;
1352 	} else {
1353 		tx_queue = &pm_queue;
1354 	}
1355 
1356 	/* loop until we can grab as many packets out of the queue as we can */
1357 	while (Resources > 0) {
1358 		int num_frags;
1359 
1360 		if (do_pm_get) {
1361 			rtpm_code =
1362 				htc_send_pkts_get_rtpm_id(
1363 							pEndpoint->service_id);
1364 			ret = hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code);
1365 			if (ret) {
1366 				/* bus suspended, runtime resume issued */
1367 				QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
1368 				pPacket = htc_get_pkt_at_head(tx_queue);
1369 				if (!pPacket)
1370 					break;
1371 				log_packet_info(target, pPacket);
1372 				break;
1373 			}
1374 			htc_inc_wmi_runtime_cnt(target, rtpm_code);
1375 
1376 		}
1377 
1378 		ret = hif_system_pm_state_check(target->hif_dev);
1379 		if (ret) {
1380 			if (do_pm_get) {
1381 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1382 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1383 			}
1384 			break;
1385 		}
1386 
1387 		pPacket = htc_packet_dequeue(tx_queue);
1388 		if (!pPacket) {
1389 			if (do_pm_get) {
1390 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1391 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1392 			}
1393 			break;
1394 		}
1395 
1396 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1397 				(" Got packet:%pK , New Queue Depth: %d\n",
1398 				 pPacket,
1399 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
1400 		/* For non-credit path the sequence number is already embedded
1401 		 * in the constructed HTC header
1402 		 */
1403 		pPacket->PktInfo.AsTx.SendFlags = 0;
1404 		pPacket->PktInfo.AsTx.CreditsUsed = 0;
1405 		/* queue this packet into the caller's queue */
1406 		HTC_PACKET_ENQUEUE(pQueue, pPacket);
1407 
1408 		/*
1409 		 * FIX THIS:
1410 		 * For now, avoid calling qdf_nbuf_get_num_frags before calling
1411 		 * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
1412 		 * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
1413 		 * been done.
1414 		 * Assume that the non-data netbufs, i.e. WMI message netbufs,
1415 		 * consist of a single fragment.
1416 		 */
1417 		/* WMI messages are in a single-fragment network buf */
1418 		num_frags =
1419 			(pPacket->PktInfo.AsTx.
1420 			 Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
1421 			qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
1422 						       (pPacket));
1423 		Resources -= num_frags;
1424 	}
1425 
1426 	if (!HTC_QUEUE_EMPTY(&pm_queue))
1427 		queue_htc_pm_packets(pEndpoint, &pm_queue);
1428 
1429 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
1430 
1431 }
1432 
1433 /**
1434  * htc_try_send() - Send packets in a queue on an endpoint
1435  * @target: HTC target on which packets need to be sent
1436  * @pEndpoint: logical endpoint on which packets needs to be sent
1437  * @pCallersSendQueue: packet queue containing the list of packets to be sent
1438  *
1439  * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
1440  *         be sent or the packet should be dropped by the upper layer
1441  */
1442 static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
1443 					  HTC_ENDPOINT *pEndpoint,
1444 					  HTC_PACKET_QUEUE *pCallersSendQueue)
1445 {
1446 	/* temp queue to hold packets at various stages */
1447 	HTC_PACKET_QUEUE sendQueue;
1448 	HTC_PACKET *pPacket;
1449 	int tx_resources;
1450 	int overflow;
1451 	enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
1452 	QDF_STATUS status;
1453 
1454 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
1455 					 pCallersSendQueue,
1456 					 (pCallersSendQueue ==
1457 					  NULL) ? 0 :
1458 					 HTC_PACKET_QUEUE_DEPTH
1459 						 (pCallersSendQueue)));
1460 
1461 	/* init the local send queue */
1462 	INIT_HTC_PACKET_QUEUE(&sendQueue);
1463 
1464 	do {
1465 
1466 		/* caller didn't provide a queue, just wants us to check
1467 		 * queues and send
1468 		 */
1469 		if (!pCallersSendQueue)
1470 			break;
1471 
1472 		if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
1473 			/* empty queue */
1474 			OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1475 							HTC_PKT_Q_EMPTY);
1476 			result = HTC_SEND_QUEUE_DROP;
1477 			break;
1478 		}
1479 
1480 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
1481 		    pEndpoint->MaxTxQueueDepth) {
1482 			/* we've already overflowed */
1483 			overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1484 		} else {
1485 			/* figure out how much we will overflow by */
1486 			overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1487 			overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1488 			/* get how much we will overflow the TX queue by */
1489 			overflow -= pEndpoint->MaxTxQueueDepth;
1490 		}
1491 
1492 		/* if overflow is negative or zero, we are okay */
1493 		if (overflow > 0) {
1494 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1495 					("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
1496 					 pEndpoint->Id, overflow,
1497 					 HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
1498 								TxQueue),
1499 					 pEndpoint->MaxTxQueueDepth));
1500 		}
1501 		if ((overflow <= 0)
1502 		    || (!pEndpoint->EpCallBacks.EpSendFull)) {
1503 			/* all packets will fit or caller did not provide send
1504 			 * full indication handler
1505 			 * just move all of them to local sendQueue object
1506 			 */
1507 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
1508 							  pCallersSendQueue);
1509 		} else {
1510 			int i;
1511 			int goodPkts =
1512 				HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
1513 				overflow;
1514 
1515 			A_ASSERT(goodPkts >= 0);
1516 			/* we have overflowed and callback is provided. Dequeue
1517 			 * all non-overflow packets into the sendqueue
1518 			 */
1519 			for (i = 0; i < goodPkts; i++) {
1520 				/* pop off caller's queue */
1521 				pPacket = htc_packet_dequeue(pCallersSendQueue);
1522 				A_ASSERT(pPacket);
1523 				if (pPacket)
1524 					/* insert into local queue */
1525 					HTC_PACKET_ENQUEUE(&sendQueue,
1526 							   pPacket);
1527 			}
1528 
1529 			/* the caller's queue has all the packets that won't fit
1530 			 * walk through the caller's queue and indicate each one
1531 			 * to the send full handler
1532 			 */
1533 			ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
1534 						       QueueHead, pPacket,
1535 						       HTC_PACKET, ListLink) {
1536 
1537 				AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1538 						("Indicating overflowed TX packet: %pK\n",
1539 						 pPacket));
1540 				/*
1541 				 * Remove headroom reserved for HTC_FRAME_HDR
1542 				 * before giving the packet back to the user via
1543 				 * the EpSendFull callback.
1544 				 */
1545 				qdf_nbuf_pull_head
1546 					(GET_HTC_PACKET_NET_BUF_CONTEXT
1547 						(pPacket),
1548 					sizeof(HTC_FRAME_HDR));
1549 				pPacket->PktInfo.AsTx.Flags &=
1550 					~HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1551 
1552 				if (pEndpoint->EpCallBacks.
1553 				    EpSendFull(pEndpoint->EpCallBacks.pContext,
1554 					       pPacket) == HTC_SEND_FULL_DROP) {
1555 					/* callback wants the packet dropped */
1556 					INC_HTC_EP_STAT(pEndpoint, TxDropped,
1557 							1);
1558 					/* leave this one in the caller's queue
1559 					 * for cleanup
1560 					 */
1561 				} else {
1562 					/* callback wants to keep this packet,
1563 					 * remove from caller's queue
1564 					 */
1565 					HTC_PACKET_REMOVE(pCallersSendQueue,
1566 							  pPacket);
1567 					/* put it in the send queue
1568 					 * add HTC_FRAME_HDR space reservation
1569 					 * again
1570 					 */
1571 					qdf_nbuf_push_head
1572 						(GET_HTC_PACKET_NET_BUF_CONTEXT
1573 							(pPacket),
1574 						sizeof(HTC_FRAME_HDR));
1575 					pPacket->PktInfo.AsTx.Flags |=
1576 						HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1577 
1578 					HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1579 				}
1580 
1581 			}
1582 			ITERATE_END;
1583 
1584 			if (HTC_QUEUE_EMPTY(&sendQueue)) {
1585 				/* no packets made it in, caller will cleanup */
1586 				OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1587 							HTC_SEND_Q_EMPTY);
1588 				result = HTC_SEND_QUEUE_DROP;
1589 				break;
1590 			}
1591 		}
1592 
1593 	} while (false);
1594 
1595 	if (result != HTC_SEND_QUEUE_OK) {
1596 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
1597 			result));
1598 		return result;
1599 	}
1600 
1601 	LOCK_HTC_TX(target);
1602 
1603 	if (!HTC_QUEUE_EMPTY(&sendQueue)) {
1604 		if (target->is_nodrop_pkt) {
1605 			/*
1606 			 * nodrop pkts have higher priority than normal pkts,
1607 			 * insert nodrop pkt to head for proper
1608 			 * start/termination of test.
1609 			 */
1610 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1611 					&sendQueue);
1612 			target->is_nodrop_pkt = false;
1613 		} else {
1614 			/* transfer packets to tail */
1615 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
1616 					&sendQueue);
1617 			A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
1618 			INIT_HTC_PACKET_QUEUE(&sendQueue);
1619 		}
1620 	}
1621 
1622 	/* increment tx processing count on entry */
1623 	if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
1624 		/* another thread or task is draining the TX queues on this
1625 		 * endpoint that thread will reset the tx processing count when
1626 		 * the queue is drained
1627 		 */
1628 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1629 		UNLOCK_HTC_TX(target);
1630 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
1631 		return HTC_SEND_QUEUE_OK;
1632 	}
1633 
1634 	/***** beyond this point only 1 thread may enter ******/
1635 
1636 	/* now drain the endpoint TX queue for transmission as long as we have
1637 	 * enough transmit resources
1638 	 */
1639 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1640 		tx_resources =
1641 			hif_get_free_queue_number(target->hif_dev,
1642 						  pEndpoint->UL_PipeID);
1643 	} else {
1644 		tx_resources = 0;
1645 	}
1646 
1647 	while (true) {
1648 
1649 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
1650 			break;
1651 
1652 		if (pEndpoint->async_update &&
1653 			(!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
1654 			(!tx_resources)) {
1655 			hif_schedule_ce_tasklet(target->hif_dev,
1656 						pEndpoint->UL_PipeID);
1657 			break;
1658 		}
1659 
1660 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1661 #if DEBUG_CREDIT
1662 			int cred = pEndpoint->TxCredits;
1663 #endif
1664 			/* credit based mechanism provides flow control based on
1665 			 * target transmit resource availability, we assume that
1666 			 * the HIF layer will always have bus resources greater
1667 			 * than target transmit resources
1668 			 */
1669 			get_htc_send_packets_credit_based(target, pEndpoint,
1670 							  &sendQueue);
1671 #if DEBUG_CREDIT
1672 			if (ep_debug_mask & (1 << pEndpoint->Id)) {
1673 				if (cred - pEndpoint->TxCredits > 0) {
1674 					AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1675 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1676 							 pEndpoint->Id, cred,
1677 							 cred -
1678 							 pEndpoint->TxCredits,
1679 							 pEndpoint->TxCredits));
1680 				}
1681 			}
1682 #endif
1683 		} else {
1684 
1685 		/*
1686 		* Header and payload belongs to the different fragments and
1687 		* consume 2 resource for one HTC package but USB combine into
1688 		* one transfer.And one WMI message only consumes one single
1689 		* resource.
1690 		*/
1691 			if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
1692 				hif_get_bus_type(target->hif_dev) ==
1693 						QDF_BUS_TYPE_USB) {
1694 				if (pEndpoint->service_id ==
1695 					WMI_CONTROL_SVC)
1696 					tx_resources =
1697 					    HTC_MAX_MSG_PER_BUNDLE_TX;
1698 				else
1699 					tx_resources =
1700 					    (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
1701 			}
1702 			/* get all the packets for this endpoint that we can for
1703 			 * this pass
1704 			 */
1705 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1706 					     tx_resources);
1707 		}
1708 
1709 		if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
1710 			/* didn't get any packets due to a lack of resources or
1711 			 * TX queue was drained
1712 			 */
1713 			break;
1714 		}
1715 
1716 		if (!pEndpoint->async_update)
1717 			UNLOCK_HTC_TX(target);
1718 
1719 		/* send what we can */
1720 		status = htc_issue_packets(target, pEndpoint, &sendQueue);
1721 		if (status) {
1722 			int i;
1723 			unsigned int rtpm_code;
1724 
1725 			result = HTC_SEND_QUEUE_DROP;
1726 
1727 			switch (status) {
1728 			case  QDF_STATUS_E_RESOURCES:
1729 				if (pEndpoint->num_requeues_warn <= MAX_REQUEUE_WARN) {
1730 					pEndpoint->num_requeues_warn++;
1731 					pEndpoint->total_num_requeues++;
1732 					break;
1733 				} else {
1734 					pEndpoint->total_num_requeues++;
1735 					pEndpoint->num_requeues_warn = 0;
1736 				}
1737 				fallthrough;
1738 			default:
1739 				QDF_TRACE(QDF_MODULE_ID_HIF, QDF_TRACE_LEVEL_INFO,
1740 					  "htc_issue_packets, failed status:%d"
1741 					  "endpoint:%d, put it back to head of"
1742 					  "callersSendQueue", result, pEndpoint->Id);
1743 				break;
1744 			}
1745 
1746 			rtpm_code = htc_send_pkts_get_rtpm_id(
1747 							pEndpoint->service_id);
1748 			for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--) {
1749 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1750 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1751 			}
1752 
1753 			if (!pEndpoint->async_update) {
1754 				LOCK_HTC_TX(target);
1755 			}
1756 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1757 							  &sendQueue);
1758 			break;
1759 		}  else {
1760 			if (pEndpoint->num_requeues_warn)
1761 				pEndpoint->num_requeues_warn = 0;
1762 		}
1763 
1764 		if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1765 			tx_resources =
1766 				hif_get_free_queue_number(target->hif_dev,
1767 							  pEndpoint->UL_PipeID);
1768 		}
1769 
1770 		if (!pEndpoint->async_update) {
1771 			LOCK_HTC_TX(target);
1772 		}
1773 
1774 	}
1775 
1776 	/* done with this endpoint, we can clear the count */
1777 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1778 
1779 	UNLOCK_HTC_TX(target);
1780 
1781 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
1782 
1783 	return HTC_SEND_QUEUE_OK;
1784 }
1785 
1786 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1787 static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
1788 					  HTC_ENDPOINT_ID id)
1789 {
1790 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1791 	HTC_ENDPOINT *pEndpoint;
1792 	HTC_ENDPOINT_ID eid;
1793 	HTC_PACKET_QUEUE *pTxQueue;
1794 	uint16_t resources;
1795 	uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
1796 
1797 	if (id < ENDPOINT_2 || id > ENDPOINT_5)
1798 		return 1;
1799 
1800 	for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
1801 		pEndpoint = &target->endpoint[eid];
1802 		pTxQueue = &pEndpoint->TxQueue;
1803 
1804 		if (HTC_QUEUE_EMPTY(pTxQueue))
1805 			acQueueStatus[eid - 2] = 1;
1806 	}
1807 
1808 	switch (id) {
1809 	case ENDPOINT_2:        /* BE */
1810 		return acQueueStatus[0] && acQueueStatus[2]
1811 			&& acQueueStatus[3];
1812 	case ENDPOINT_3:        /* BK */
1813 		return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
1814 			&& acQueueStatus[3];
1815 	case ENDPOINT_4:        /* VI */
1816 		return acQueueStatus[2] && acQueueStatus[3];
1817 	case ENDPOINT_5:        /* VO */
1818 		return acQueueStatus[3];
1819 	default:
1820 		return 0;
1821 	}
1822 
1823 }
1824 
1825 static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
1826 					  HTC_PACKET_QUEUE *pPktQueue,
1827 					  HTC_ENDPOINT_ID eid)
1828 {
1829 	HTC_ENDPOINT *pEndpoint;
1830 	HTC_PACKET_QUEUE *pTxQueue;
1831 	HTC_PACKET *pPacket;
1832 	int goodPkts;
1833 
1834 	pEndpoint = &target->endpoint[eid];
1835 	pTxQueue = &pEndpoint->TxQueue;
1836 
1837 	LOCK_HTC_TX(target);
1838 
1839 	goodPkts =
1840 		pEndpoint->MaxTxQueueDepth -
1841 		HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1842 
1843 	if (goodPkts > 0) {
1844 		while (!HTC_QUEUE_EMPTY(pPktQueue)) {
1845 			pPacket = htc_packet_dequeue(pPktQueue);
1846 			HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1847 			goodPkts--;
1848 
1849 			if (goodPkts <= 0)
1850 				break;
1851 		}
1852 	}
1853 
1854 	if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
1855 		ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
1856 					       HTC_PACKET, ListLink) {
1857 
1858 			if (pEndpoint->EpCallBacks.
1859 			    EpSendFull(pEndpoint->EpCallBacks.pContext,
1860 				       pPacket) == HTC_SEND_FULL_DROP) {
1861 				INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
1862 			} else {
1863 				HTC_PACKET_REMOVE(pPktQueue, pPacket);
1864 				HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1865 			}
1866 		}
1867 		ITERATE_END;
1868 	}
1869 
1870 	UNLOCK_HTC_TX(target);
1871 
1872 	return A_OK;
1873 }
1874 
1875 #endif
1876 
1877 static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
1878 				HTC_PACKET *pPacket)
1879 {
1880 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1881 	HTC_ENDPOINT *pEndpoint;
1882 	HTC_PACKET_QUEUE pPktQueue;
1883 	qdf_nbuf_t netbuf;
1884 	HTC_FRAME_HDR *htc_hdr;
1885 	QDF_STATUS status;
1886 
1887 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1888 			("+__htc_send_pkt\n"));
1889 
1890 	/* get packet at head to figure out which endpoint these packets will
1891 	 * go into
1892 	 */
1893 	if (!pPacket) {
1894 		OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
1895 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1896 		return QDF_STATUS_E_INVAL;
1897 	}
1898 
1899 	if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1900 	    (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1901 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1902 			("%s endpoint is invalid\n", __func__));
1903 		AR_DEBUG_ASSERT(0);
1904 		return QDF_STATUS_E_INVAL;
1905 	}
1906 	pEndpoint = &target->endpoint[pPacket->Endpoint];
1907 
1908 	if (!pEndpoint->service_id) {
1909 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
1910 								__func__));
1911 		return QDF_STATUS_E_INVAL;
1912 	}
1913 
1914 #ifdef HTC_EP_STAT_PROFILING
1915 	LOCK_HTC_TX(target);
1916 	INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
1917 	UNLOCK_HTC_TX(target);
1918 #endif
1919 
1920 	/* provide room in each packet's netbuf for the HTC frame header */
1921 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1922 	AR_DEBUG_ASSERT(netbuf);
1923 	if (!netbuf)
1924 		return QDF_STATUS_E_INVAL;
1925 
1926 	qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
1927 	pPacket->PktInfo.AsTx.Flags |=
1928 		HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1929 	/* setup HTC frame header */
1930 	htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1931 	AR_DEBUG_ASSERT(htc_hdr);
1932 	if (!htc_hdr)
1933 		return QDF_STATUS_E_INVAL;
1934 
1935 	HTC_WRITE32(htc_hdr,
1936 		    SM(pPacket->ActualLength,
1937 		       HTC_FRAME_HDR_PAYLOADLEN) |
1938 		    SM(pPacket->Endpoint,
1939 		       HTC_FRAME_HDR_ENDPOINTID));
1940 	LOCK_HTC_TX(target);
1941 
1942 	pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1943 	pEndpoint->SeqNo++;
1944 
1945 	HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
1946 		    SM(pPacket->PktInfo.AsTx.SeqNo,
1947 		       HTC_FRAME_HDR_CONTROLBYTES1));
1948 
1949 	UNLOCK_HTC_TX(target);
1950 
1951 	/*
1952 	 * For flow control enabled endpoints mapping is done in
1953 	 * htc_issue_packets and for non flow control enabled endpoints
1954 	 * its done here.
1955 	 */
1956 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1957 		pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
1958 		status = qdf_nbuf_map(target->osdev,
1959 				      GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
1960 				      QDF_DMA_TO_DEVICE);
1961 		if (status == QDF_STATUS_SUCCESS) {
1962 			target->nbuf_nfc_map_count++;
1963 		} else {
1964 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1965 					("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
1966 					 __func__, pEndpoint, pEndpoint->SeqNo));
1967 			return status;
1968 		}
1969 	}
1970 
1971 	INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
1972 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1973 	if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
1974 		htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
1975 	else
1976 		htc_try_send(target, pEndpoint, &pPktQueue);
1977 #else
1978 	htc_try_send(target, pEndpoint, &pPktQueue);
1979 #endif
1980 
1981 	/* do completion on any packets that couldn't get in */
1982 	while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
1983 		pPacket = htc_packet_dequeue(&pPktQueue);
1984 
1985 		if (HTC_STOPPING(target))
1986 			pPacket->Status = QDF_STATUS_E_CANCELED;
1987 		else
1988 			pPacket->Status = QDF_STATUS_E_RESOURCES;
1989 
1990 		send_packet_completion(target, pPacket);
1991 	}
1992 
1993 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1994 
1995 	return QDF_STATUS_SUCCESS;
1996 }
1997 
1998 /* HTC API - htc_send_pkt */
1999 QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
2000 {
2001 	if (!htc_handle) {
2002 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2003 				("%s: HTCHandle is NULL \n", __func__));
2004 		return QDF_STATUS_E_FAILURE;
2005 	}
2006 
2007 	if (!htc_packet) {
2008 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2009 				("%s: pPacket is NULL \n", __func__));
2010 		return QDF_STATUS_E_FAILURE;
2011 	}
2012 
2013 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2014 			("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
2015 			 htc_packet->Endpoint, htc_packet->pBuffer,
2016 			 htc_packet->ActualLength));
2017 	return __htc_send_pkt(htc_handle, htc_packet);
2018 }
2019 qdf_export_symbol(htc_send_pkt);
2020 
2021 #ifdef ATH_11AC_TXCOMPACT
2022 /**
2023  * htc_send_data_pkt() - send single data packet on an endpoint
2024  * @htc_hdl: pointer to HTC handle
2025  * @netbuf: network buffer containing the data to be sent
2026  * @ep_id: endpoint identifier
2027  * @actual_length: length of data that needs to be transmitted
2028  *
2029  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2030  */
2031 QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
2032 			     int actual_length)
2033 {
2034 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2035 	HTC_ENDPOINT *pEndpoint;
2036 	HTC_FRAME_HDR *p_htc_hdr;
2037 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2038 	int tx_resources;
2039 	uint32_t data_attr = 0;
2040 	int htc_payload_len = actual_length;
2041 	unsigned int rtpm_code;
2042 
2043 	pEndpoint = &target->endpoint[ep_id];
2044 
2045 	tx_resources = hif_get_free_queue_number(target->hif_dev,
2046 						 pEndpoint->UL_PipeID);
2047 
2048 	if (tx_resources < HTC_DATA_RESOURCE_THRS) {
2049 		if (pEndpoint->ul_is_polled) {
2050 			hif_send_complete_check(pEndpoint->target->hif_dev,
2051 						pEndpoint->UL_PipeID, 1);
2052 			tx_resources =
2053 				hif_get_free_queue_number(target->hif_dev,
2054 							  pEndpoint->UL_PipeID);
2055 		}
2056 		if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
2057 			return QDF_STATUS_E_FAILURE;
2058 	}
2059 
2060 	rtpm_code = htc_send_pkts_get_rtpm_id(
2061 					pEndpoint->service_id);
2062 	if (hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code))
2063 		return QDF_STATUS_E_FAILURE;
2064 
2065 	p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
2066 	AR_DEBUG_ASSERT(p_htc_hdr);
2067 
2068 	data_attr = qdf_nbuf_data_attr_get(netbuf);
2069 
2070 	if (target->htc_hdr_length_check)
2071 		htc_payload_len = actual_length - HTC_HEADER_LEN;
2072 
2073 	HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
2074 		    | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
2075 	/*
2076 	 * If the HIF pipe for the data endpoint is polled rather than
2077 	 * interrupt-driven, this is a good point to check whether any
2078 	 * data previously sent through the HIF pipe have finished being
2079 	 * sent.
2080 	 * Since this may result in callbacks to htc_tx_completion_handler,
2081 	 * which can take the HTC tx lock, make the hif_send_complete_check
2082 	 * call before acquiring the HTC tx lock.
2083 	 * Call hif_send_complete_check directly, rather than calling
2084 	 * htc_send_complete_check, and call the PollTimerStart separately
2085 	 * after calling hif_send_head, so the timer will be started to
2086 	 * check for completion of the new outstanding download (in the
2087 	 * unexpected event that other polling calls don't catch it).
2088 	 */
2089 
2090 	LOCK_HTC_TX(target);
2091 
2092 	HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
2093 		    SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
2094 
2095 	pEndpoint->SeqNo++;
2096 
2097 	QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
2098 	DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
2099 		QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
2100 		sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
2101 	status = hif_send_head(target->hif_dev,
2102 			       pEndpoint->UL_PipeID,
2103 			       pEndpoint->Id, actual_length, netbuf, data_attr);
2104 
2105 	UNLOCK_HTC_TX(target);
2106 	return status;
2107 }
2108 #else                           /*ATH_11AC_TXCOMPACT */
2109 
2110 /**
2111  * htc_send_data_pkt() - htc_send_data_pkt
2112  * @HTCHandle: pointer to HTC handle
2113  * @pPacket: pointer to HTC_PACKET
2114  * @more_data: indicates whether more data is to follow
2115  *
2116  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2117  */
2118 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
2119 			   uint8_t more_data)
2120 {
2121 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2122 	HTC_ENDPOINT *pEndpoint;
2123 	HTC_FRAME_HDR *pHtcHdr;
2124 	HTC_PACKET_QUEUE sendQueue;
2125 	qdf_nbuf_t netbuf = NULL;
2126 	int tx_resources;
2127 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2128 	uint32_t data_attr = 0;
2129 	bool used_extra_tx_credit = false;
2130 
2131 	if (pPacket) {
2132 		if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
2133 		   (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
2134 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2135 				("%s endpoint is invalid\n", __func__));
2136 			AR_DEBUG_ASSERT(0);
2137 			return QDF_STATUS_E_INVAL;
2138 		}
2139 		pEndpoint = &target->endpoint[pPacket->Endpoint];
2140 
2141 		/* add HTC_FRAME_HDR in the initial fragment */
2142 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
2143 		pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
2144 		AR_DEBUG_ASSERT(pHtcHdr);
2145 
2146 		HTC_WRITE32(pHtcHdr,
2147 				SM(pPacket->ActualLength,
2148 				       HTC_FRAME_HDR_PAYLOADLEN) |
2149 				SM(pPacket->PktInfo.AsTx.SendFlags,
2150 					HTC_FRAME_HDR_FLAGS) |
2151 				SM(pPacket->Endpoint,
2152 					HTC_FRAME_HDR_ENDPOINTID));
2153 		/*
2154 		 * If the HIF pipe for the data endpoint is polled rather than
2155 		 * interrupt-driven, this is a good point to check whether any
2156 		 * data previously sent through the HIF pipe have finished being
2157 		 * sent. Since this may result in callbacks to
2158 		 * htc_tx_completion_handler, which can take the HTC tx lock,
2159 		 * make the hif_send_complete_check call before acquiring the
2160 		 * HTC tx lock.
2161 		 * Call hif_send_complete_check directly, rather than calling
2162 		 * htc_send_complete_check, and call the PollTimerStart
2163 		 * separately after calling hif_send_head, so the timer will be
2164 		 * started to check for completion of the new outstanding
2165 		 * download (in the unexpected event that other polling calls
2166 		 * don't catch it).
2167 		 */
2168 		if (pEndpoint->ul_is_polled) {
2169 			htc_send_complete_poll_timer_stop(pEndpoint);
2170 			hif_send_complete_check(pEndpoint->target->hif_dev,
2171 						pEndpoint->UL_PipeID, 0);
2172 		}
2173 
2174 		LOCK_HTC_TX(target);
2175 
2176 		pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
2177 		pEndpoint->SeqNo++;
2178 
2179 		HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
2180 			    SM(pPacket->PktInfo.AsTx.SeqNo,
2181 			       HTC_FRAME_HDR_CONTROLBYTES1));
2182 
2183 		/* append new packet to pEndpoint->TxQueue */
2184 		HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
2185 		if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
2186 			UNLOCK_HTC_TX(target);
2187 			return QDF_STATUS_SUCCESS;
2188 		}
2189 
2190 		QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
2191 		DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
2192 			  QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
2193 				sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
2194 	} else {
2195 		LOCK_HTC_TX(target);
2196 		pEndpoint = &target->endpoint[1];
2197 	}
2198 
2199 	/* increment tx processing count on entry */
2200 	qdf_atomic_inc(&pEndpoint->TxProcessCount);
2201 	if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
2202 		/*
2203 		 * Another thread or task is draining the TX queues on this
2204 		 * endpoint. That thread will reset the tx processing count when
2205 		 * the queue is drained.
2206 		 */
2207 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
2208 		UNLOCK_HTC_TX(target);
2209 		return QDF_STATUS_SUCCESS;
2210 	}
2211 
2212 	/***** beyond this point only 1 thread may enter ******/
2213 
2214 	INIT_HTC_PACKET_QUEUE(&sendQueue);
2215 	if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
2216 #if DEBUG_CREDIT
2217 		int cred = pEndpoint->TxCredits;
2218 #endif
2219 		get_htc_send_packets_credit_based(target, pEndpoint,
2220 						 &sendQueue);
2221 #if DEBUG_CREDIT
2222 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2223 			if (cred - pEndpoint->TxCredits > 0) {
2224 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2225 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
2226 						 pEndpoint->Id, cred,
2227 						 cred - pEndpoint->TxCredits,
2228 						 pEndpoint->TxCredits));
2229 			}
2230 		}
2231 #endif
2232 		UNLOCK_HTC_TX(target);
2233 	}
2234 
2235 	else if (HTC_TX_BUNDLE_ENABLED(target)) {
2236 		if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) {
2237 			if (hif_get_free_queue_number(target->hif_dev,
2238 						      pEndpoint->UL_PipeID))
2239 				/*
2240 				 * Header and payload belongs to the different
2241 				 * fragments and consume 2 resource for one HTC
2242 				 * package but USB combine into one transfer.
2243 				 */
2244 				get_htc_send_packets(target, pEndpoint,
2245 						     &sendQueue,
2246 						     HTC_MAX_MSG_PER_BUNDLE_TX
2247 						     * 2);
2248 		} else {
2249 			/* Dequeue max packets from endpoint tx queue */
2250 			get_htc_send_packets(target, pEndpoint, &sendQueue,
2251 					     HTC_MAX_TX_BUNDLE_SEND_LIMIT);
2252 		}
2253 		UNLOCK_HTC_TX(target);
2254 	} else {
2255 		/*
2256 		 * Now drain the endpoint TX queue for transmission as long as
2257 		 * we have enough transmit resources
2258 		 */
2259 		tx_resources =
2260 			hif_get_free_queue_number(target->hif_dev,
2261 						  pEndpoint->UL_PipeID);
2262 		get_htc_send_packets(target, pEndpoint, &sendQueue,
2263 				     tx_resources);
2264 		UNLOCK_HTC_TX(target);
2265 	}
2266 
2267 	/* send what we can */
2268 	while (true) {
2269 		if (HTC_TX_BUNDLE_ENABLED(target) &&
2270 		    (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
2271 		     HTC_MIN_MSG_PER_BUNDLE) &&
2272 		    (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
2273 		     hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
2274 			if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2275 				if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
2276 					status = QDF_STATUS_E_RESOURCES;
2277 					/* put the sendQueue back at the front
2278 					 * of pEndpoint->TxQueue
2279 					 */
2280 					LOCK_HTC_TX(target);
2281 					HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
2282 							&pEndpoint->TxQueue,
2283 							&sendQueue);
2284 					UNLOCK_HTC_TX(target);
2285 					break;
2286 				}
2287 			}
2288 			htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
2289 		}
2290 		if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2291 			if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
2292 				status = QDF_STATUS_E_RESOURCES;
2293 				/* put the sendQueue back at the front
2294 				 * of pEndpoint->TxQueue
2295 				 */
2296 				LOCK_HTC_TX(target);
2297 				HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
2298 							&pEndpoint->TxQueue,
2299 							&sendQueue);
2300 				UNLOCK_HTC_TX(target);
2301 				break;
2302 			}
2303 		}
2304 		pPacket = htc_packet_dequeue(&sendQueue);
2305 		if (!pPacket)
2306 			break;
2307 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
2308 		pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
2309 
2310 		LOCK_HTC_TX(target);
2311 		/* store in look up queue to match completions */
2312 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
2313 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
2314 		pEndpoint->ul_outstanding_cnt++;
2315 		UNLOCK_HTC_TX(target);
2316 
2317 		used_extra_tx_credit =
2318 				htc_handle_extra_tx_credit(pEndpoint, pPacket,
2319 							   (uint8_t *)pHtcHdr,
2320 							   NULL,
2321 							   pPacket->ActualLength
2322 							   + HTC_HDR_LENGTH);
2323 
2324 		status = hif_send_head(target->hif_dev,
2325 				       pEndpoint->UL_PipeID,
2326 				       pEndpoint->Id,
2327 				       HTC_HDR_LENGTH + pPacket->ActualLength,
2328 				       netbuf, data_attr);
2329 		if (status != QDF_STATUS_SUCCESS) {
2330 			if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2331 				if (used_extra_tx_credit) {
2332 					pEndpoint->EpCallBacks.
2333 					ep_padding_credit_update
2334 					(pEndpoint->EpCallBacks.pContext, 1);
2335 				}
2336 			}
2337 		}
2338 #if DEBUG_BUNDLE
2339 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
2340 			  pEndpoint->Id,
2341 			  pEndpoint->TxCreditSize,
2342 			  HTC_HDR_LENGTH + pPacket->ActualLength);
2343 #endif
2344 
2345 		htc_issue_tx_bundle_stats_inc(target);
2346 
2347 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
2348 			LOCK_HTC_TX(target);
2349 			pEndpoint->ul_outstanding_cnt--;
2350 			/* remove this packet from the tx completion queue */
2351 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
2352 
2353 			/*
2354 			 * Don't bother reclaiming credits - HTC flow control
2355 			 * is not applicable to tx data.
2356 			 * In LL systems, there is no download flow control,
2357 			 * since there's virtually no download delay.
2358 			 * In HL systems, the txrx SW explicitly performs the
2359 			 * tx flow control.
2360 			 */
2361 			/* pEndpoint->TxCredits +=
2362 			 * pPacket->PktInfo.AsTx.CreditsUsed;
2363 			 */
2364 
2365 			/* put this frame back at the front of the sendQueue */
2366 			HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
2367 
2368 			/* put the sendQueue back at the front of
2369 			 * pEndpoint->TxQueue
2370 			 */
2371 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
2372 							  &sendQueue);
2373 			UNLOCK_HTC_TX(target);
2374 			break;  /* still need to reset TxProcessCount */
2375 		}
2376 	}
2377 	/* done with this endpoint, we can clear the count */
2378 	qdf_atomic_init(&pEndpoint->TxProcessCount);
2379 
2380 	if (pEndpoint->ul_is_polled) {
2381 		/*
2382 		 * Start a cleanup timer to poll for download completion.
2383 		 * The download completion should be noticed promptly from
2384 		 * other polling calls, but the timer provides a safety net
2385 		 * in case other polling calls don't occur as expected.
2386 		 */
2387 		htc_send_complete_poll_timer_start(pEndpoint);
2388 	}
2389 
2390 	return status;
2391 }
2392 #endif /*ATH_11AC_TXCOMPACT */
2393 qdf_export_symbol(htc_send_data_pkt);
2394 
2395 /*
2396  * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
2397  * since upper layers expects HTC_PACKET containers we use the completed netbuf
2398  * and lookup its corresponding HTC packet buffer from a lookup list.
2399  * This is extra overhead that can be fixed by re-aligning HIF interfaces
2400  * with HTC.
2401  *
2402  */
2403 static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
2404 					HTC_ENDPOINT *pEndpoint,
2405 					qdf_nbuf_t netbuf)
2406 {
2407 	HTC_PACKET *pPacket = NULL;
2408 	HTC_PACKET *pFoundPacket = NULL;
2409 	HTC_PACKET_QUEUE lookupQueue;
2410 
2411 	INIT_HTC_PACKET_QUEUE(&lookupQueue);
2412 	LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2413 
2414 	LOCK_HTC_TX(target);
2415 	/* mark that HIF has indicated the send complete for another packet */
2416 	pEndpoint->ul_outstanding_cnt--;
2417 
2418 	/* Dequeue first packet directly because of in-order completion */
2419 	pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
2420 	if (qdf_unlikely(!pPacket)) {
2421 		UNLOCK_HTC_TX(target);
2422 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2423 		return NULL;
2424 	}
2425 	if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
2426 		UNLOCK_HTC_TX(target);
2427 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2428 		return pPacket;
2429 	}
2430 	HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
2431 
2432 	/*
2433 	 * Move TX lookup queue to temp queue because most of packets that are
2434 	 * not index 0 are not top 10 packets.
2435 	 */
2436 	HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
2437 					  &pEndpoint->TxLookupQueue);
2438 
2439 	ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
2440 				       HTC_PACKET, ListLink) {
2441 
2442 		if (!pPacket) {
2443 			pFoundPacket = pPacket;
2444 			break;
2445 		}
2446 		/* check for removal */
2447 		if (netbuf ==
2448 		    (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
2449 			/* found it */
2450 			HTC_PACKET_REMOVE(&lookupQueue, pPacket);
2451 			pFoundPacket = pPacket;
2452 			break;
2453 		}
2454 
2455 	}
2456 	ITERATE_END;
2457 
2458 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
2459 					  &lookupQueue);
2460 	UNLOCK_HTC_TX(target);
2461 	UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2462 
2463 	return pFoundPacket;
2464 }
2465 
2466 /**
2467  * htc_tx_completion_handler() - htc tx completion handler
2468  * @Context: pointer to HTC_TARGET structure
2469  * @netbuf: pointer to netbuf for which completion handler is being called
2470  * @EpID: end point Id on which the packet was sent
2471  * @toeplitz_hash_result: toeplitz hash result
2472  *
2473  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2474  */
2475 QDF_STATUS htc_tx_completion_handler(void *Context,
2476 				   qdf_nbuf_t netbuf, unsigned int EpID,
2477 				   uint32_t toeplitz_hash_result)
2478 {
2479 	HTC_TARGET *target = (HTC_TARGET *) Context;
2480 	HTC_ENDPOINT *pEndpoint;
2481 	HTC_PACKET *pPacket;
2482 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
2483 	HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
2484 		ENDPOINT_2, ENDPOINT_3 };
2485 	int epidIdx;
2486 	uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
2487 	uint16_t resources;
2488 	uint16_t resourcesMax;
2489 #endif
2490 
2491 	pEndpoint = &target->endpoint[EpID];
2492 	target->TX_comp_cnt++;
2493 	pEndpoint->htc_comp_cnt++;
2494 
2495 	do {
2496 		pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
2497 		if (!pPacket) {
2498 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2499 					("HTC TX lookup failed!\n"));
2500 			/* may have already been flushed and freed */
2501 			netbuf = NULL;
2502 			break;
2503 		}
2504 		if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM &&
2505 		    pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RUNTIME_PUT &&
2506 		    pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RTPM_PUT_RC) {
2507 			hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_WMI);
2508 			htc_dec_wmi_runtime_cnt(target, HIF_RTPM_ID_WMI);
2509 		}
2510 
2511 
2512 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
2513 			HTC_PACKET *pPacketTemp;
2514 			HTC_PACKET_QUEUE *pQueueSave =
2515 				(HTC_PACKET_QUEUE *) pPacket->pContext;
2516 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
2517 							      pPacketTemp) {
2518 				pPacket->Status = QDF_STATUS_SUCCESS;
2519 				send_packet_completion(target, pPacketTemp);
2520 			}
2521 			HTC_PACKET_QUEUE_ITERATE_END;
2522 			free_htc_bundle_packet(target, pPacket);
2523 
2524 			if (hif_get_bus_type(target->hif_dev) ==
2525 					     QDF_BUS_TYPE_USB) {
2526 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
2527 					htc_try_send(target, pEndpoint, NULL);
2528 			}
2529 
2530 			return QDF_STATUS_SUCCESS;
2531 		}
2532 		/* will be giving this buffer back to upper layers */
2533 		netbuf = NULL;
2534 		pPacket->Status = QDF_STATUS_SUCCESS;
2535 		send_packet_completion(target, pPacket);
2536 
2537 	} while (false);
2538 
2539 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
2540 		/* note: when using TX credit flow, the re-checking of queues
2541 		 * happens when credits flow back from the target. In the non-TX
2542 		 * credit case, we recheck after the packet completes
2543 		 */
2544 		if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
2545 				(!pEndpoint->async_update)) {
2546 			htc_try_send(target, pEndpoint, NULL);
2547 		}
2548 	}
2549 
2550 	return QDF_STATUS_SUCCESS;
2551 }
2552 
2553 #ifdef WLAN_FEATURE_FASTPATH
2554 /**
2555  * htc_ctrl_msg_cmpl() - checks for tx completion for the endpoint specified
2556  * @htc_pdev: pointer to the htc context
2557  * @htc_ep_id: end point id
2558  *
2559  * checks HTC tx completion
2560  *
2561  * Return: none
2562  */
2563 void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
2564 {
2565 	HTC_TARGET      *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
2566 	HTC_ENDPOINT    *pendpoint = &target->endpoint[htc_ep_id];
2567 
2568 	htc_send_complete_check(pendpoint, 1);
2569 }
2570 qdf_export_symbol(htc_ctrl_msg_cmpl);
2571 #endif
2572 
2573 /* callback when TX resources become available */
2574 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
2575 {
2576 	int i;
2577 	HTC_TARGET *target = (HTC_TARGET *) context;
2578 	HTC_ENDPOINT *pEndpoint = NULL;
2579 
2580 	for (i = 0; i < ENDPOINT_MAX; i++) {
2581 		pEndpoint = &target->endpoint[i];
2582 		if (pEndpoint->service_id != 0) {
2583 			if (pEndpoint->UL_PipeID == pipeID)
2584 				break;
2585 		}
2586 	}
2587 
2588 	if (i >= ENDPOINT_MAX) {
2589 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2590 				("Invalid pipe indicated for TX resource avail : %d!\n",
2591 				 pipeID));
2592 		return;
2593 	}
2594 
2595 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2596 			("HIF indicated more resources for pipe:%d\n",
2597 			 pipeID));
2598 
2599 	htc_try_send(target, pEndpoint, NULL);
2600 }
2601 
2602 #ifdef FEATURE_RUNTIME_PM
2603 /**
2604  * htc_kick_queues() - resumes tx transactions of suspended endpoints
2605  * @context: pointer to the htc target context
2606  *
2607  * Iterates through the endpoints and provides a context to empty queues
2608  * int the hif layer when they are stalled due to runtime suspend.
2609  *
2610  * Return: none
2611  */
2612 void htc_kick_queues(void *context)
2613 {
2614 	int i;
2615 	HTC_TARGET *target = (HTC_TARGET *)context;
2616 	HTC_ENDPOINT *endpoint = NULL;
2617 
2618 	if (hif_rtpm_get(HIF_RTPM_GET_SYNC, HIF_RTPM_ID_HTT))
2619 		return;
2620 
2621 	for (i = 0; i < ENDPOINT_MAX; i++) {
2622 		endpoint = &target->endpoint[i];
2623 
2624 		if (endpoint->service_id == 0)
2625 			continue;
2626 
2627 		if (endpoint->EpCallBacks.ep_resume_tx_queue)
2628 			endpoint->EpCallBacks.ep_resume_tx_queue(
2629 					endpoint->EpCallBacks.pContext);
2630 
2631 		htc_try_send(target, endpoint, NULL);
2632 	}
2633 
2634 	hif_fastpath_resume(target->hif_dev);
2635 
2636 	hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_HTT);
2637 }
2638 #endif
2639 
2640 /* flush endpoint TX queue */
2641 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
2642 			   HTC_TX_TAG Tag)
2643 {
2644 	HTC_PACKET *pPacket;
2645 
2646 	LOCK_HTC_TX(target);
2647 	while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2648 		pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
2649 
2650 		if (pPacket) {
2651 			/* let the sender know the packet was not delivered */
2652 			pPacket->Status = QDF_STATUS_E_CANCELED;
2653 			send_packet_completion(target, pPacket);
2654 		}
2655 	}
2656 	UNLOCK_HTC_TX(target);
2657 }
2658 
2659 /* flush pending entries in endpoint TX Lookup queue */
2660 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
2661 				  HTC_ENDPOINT_ID endpoint_id,
2662 				  bool call_ep_callback)
2663 {
2664 	HTC_PACKET *packet;
2665 	HTC_ENDPOINT *endpoint;
2666 
2667 	endpoint = &target->endpoint[endpoint_id];
2668 
2669 	if (!endpoint && endpoint->service_id == 0)
2670 		return;
2671 
2672 	LOCK_HTC_TX(target);
2673 	while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
2674 		packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
2675 
2676 		if (packet) {
2677 			if (call_ep_callback == true) {
2678 				packet->Status = QDF_STATUS_E_CANCELED;
2679 				send_packet_completion(target, packet);
2680 			} else {
2681 				qdf_mem_free(packet);
2682 			}
2683 		}
2684 	}
2685 	UNLOCK_HTC_TX(target);
2686 }
2687 
2688 /* HTC API to flush an endpoint's TX queue*/
2689 void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
2690 			HTC_TX_TAG Tag)
2691 {
2692 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2693 	HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
2694 
2695 	if (pEndpoint->service_id == 0) {
2696 		AR_DEBUG_ASSERT(false);
2697 		/* not in use.. */
2698 		return;
2699 	}
2700 
2701 	htc_flush_endpoint_tx(target, pEndpoint, Tag);
2702 }
2703 
2704 /* HTC API to indicate activity to the credit distribution function */
2705 void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
2706 				  HTC_ENDPOINT_ID Endpoint, bool Active)
2707 {
2708 	/*  TODO */
2709 }
2710 
2711 bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
2712 {
2713 	return true;
2714 }
2715 
2716 void htc_set_pkt_dbg(HTC_HANDLE handle, A_BOOL dbg_flag)
2717 {
2718 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(handle);
2719 
2720 	target->htc_pkt_dbg = dbg_flag;
2721 }
2722 
2723 void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
2724 {
2725 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2726 
2727 	target->is_nodrop_pkt = isNodropPkt;
2728 }
2729 
2730 void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
2731 {
2732 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2733 
2734 	target->htc_hdr_length_check = htc_hdr_length_check;
2735 }
2736 
2737 /**
2738  * htc_process_credit_rpt() - process credit report, call distribution function
2739  * @target: pointer to HTC_TARGET
2740  * @pRpt: pointer to HTC_CREDIT_REPORT
2741  * @NumEntries: number of entries in credit report
2742  * @FromEndpoint: endpoint for which  credit report is received
2743  *
2744  * Return: A_OK for success or an appropriate A_STATUS error
2745  */
2746 void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
2747 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
2748 {
2749 	int i;
2750 	HTC_ENDPOINT *pEndpoint;
2751 	int totalCredits = 0;
2752 	uint8_t rpt_credits, rpt_ep_id;
2753 
2754 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2755 			("+htc_process_credit_rpt, Credit Report Entries:%d\n",
2756 			 NumEntries));
2757 
2758 	/* lock out TX while we update credits */
2759 	LOCK_HTC_TX(target);
2760 
2761 	for (i = 0; i < NumEntries; i++, pRpt++) {
2762 
2763 		rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
2764 
2765 		if (rpt_ep_id >= ENDPOINT_MAX) {
2766 			AR_DEBUG_ASSERT(false);
2767 			break;
2768 		}
2769 
2770 		rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
2771 
2772 		pEndpoint = &target->endpoint[rpt_ep_id];
2773 #if DEBUG_CREDIT
2774 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2775 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2776 					(" <HTC> Increase EP%d %d + %d = %d credits\n",
2777 					 rpt_ep_id, pEndpoint->TxCredits,
2778 					 rpt_credits,
2779 					 pEndpoint->TxCredits + rpt_credits));
2780 		}
2781 #endif
2782 
2783 #ifdef HTC_EP_STAT_PROFILING
2784 
2785 		INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
2786 		INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
2787 
2788 		if (FromEndpoint == rpt_ep_id) {
2789 			/* this credit report arrived on the same endpoint
2790 			 * indicating it arrived in an RX packet
2791 			 */
2792 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
2793 					rpt_credits);
2794 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
2795 		} else if (FromEndpoint == ENDPOINT_0) {
2796 			/* this credit arrived on endpoint 0 as a NULL msg */
2797 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
2798 					rpt_credits);
2799 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
2800 		} else {
2801 			/* arrived on another endpoint */
2802 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
2803 					rpt_credits);
2804 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
2805 		}
2806 
2807 #endif
2808 
2809 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
2810 			htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
2811 					  pEndpoint->TxCredits + rpt_credits,
2812 					  HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
2813 							TxQueue));
2814 			hif_latency_detect_credit_record_time(
2815 					HIF_PROCESS_CREDIT_REPORT,
2816 					target->hif_dev);
2817 		}
2818 
2819 		pEndpoint->TxCredits += rpt_credits;
2820 
2821 		if (pEndpoint->TxCredits
2822 		    && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2823 			UNLOCK_HTC_TX(target);
2824 #ifdef ATH_11AC_TXCOMPACT
2825 			htc_try_send(target, pEndpoint, NULL);
2826 #else
2827 			if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
2828 				htc_send_data_pkt((HTC_HANDLE)target, NULL, 0);
2829 			else
2830 				htc_try_send(target, pEndpoint, NULL);
2831 #endif
2832 			LOCK_HTC_TX(target);
2833 		}
2834 		totalCredits += rpt_credits;
2835 	}
2836 
2837 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2838 			("  Report indicated %d credits to distribute\n",
2839 			 totalCredits));
2840 
2841 	UNLOCK_HTC_TX(target);
2842 
2843 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
2844 }
2845 
2846 /* function to fetch stats from htc layer*/
2847 struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
2848 {
2849 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2850 
2851 	return &(target->htc_pkt_stats);
2852 }
2853 
2854 #ifdef SYSTEM_PM_CHECK
2855 void htc_system_resume(HTC_HANDLE htc)
2856 {
2857 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc);
2858 	HTC_ENDPOINT *endpoint = NULL;
2859 	int i;
2860 
2861 	if (!target)
2862 		return;
2863 
2864 	for (i = 0; i < ENDPOINT_MAX; i++) {
2865 		endpoint = &target->endpoint[i];
2866 
2867 		if (endpoint->service_id == 0)
2868 			continue;
2869 
2870 		htc_try_send(target, endpoint, NULL);
2871 	}
2872 }
2873 #endif
2874