xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_send.c (revision dcad508db8d600a025fbdf5331b7527274d176d6)
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 				if (HTC_PACKET_QUEUE_DEPTH(pQueue) > 0)
1204 					AR_DEBUG_PRINTF(ATH_DEBUG_WARN,
1205 							(" pQueue depth: %d\n",
1206 							 HTC_PACKET_QUEUE_DEPTH(pQueue)));
1207 
1208 				pPacket = htc_get_pkt_at_head(tx_queue);
1209 				if (!pPacket)
1210 					break;
1211 				log_packet_info(target, pPacket);
1212 				break;
1213 			}
1214 			htc_inc_wmi_runtime_cnt(target, rtpm_code);
1215 		}
1216 
1217 		sendFlags = 0;
1218 		/* get packet at head, but don't remove it */
1219 		pPacket = htc_get_pkt_at_head(tx_queue);
1220 		if (!pPacket) {
1221 			if (do_pm_get) {
1222 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1223 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1224 			}
1225 			break;
1226 		}
1227 
1228 		if (sys_pm_check &&
1229 		    hif_system_pm_state_check(target->hif_dev)) {
1230 			if (do_pm_get) {
1231 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1232 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1233 			}
1234 			break;
1235 		}
1236 
1237 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1238 				(" Got head packet:%pK , Queue Depth: %d\n",
1239 				 pPacket,
1240 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
1241 
1242 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
1243 
1244 		if (transferLength <= pEndpoint->TxCreditSize) {
1245 			creditsRequired = 1;
1246 		} else {
1247 			/* figure out how many credits this message requires */
1248 			creditsRequired =
1249 				transferLength / pEndpoint->TxCreditSize;
1250 			remainder = transferLength % pEndpoint->TxCreditSize;
1251 
1252 			if (remainder)
1253 				creditsRequired++;
1254 		}
1255 
1256 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1257 				(" Credits Required:%d   Got:%d\n",
1258 				 creditsRequired, pEndpoint->TxCredits));
1259 
1260 		if (pEndpoint->Id == ENDPOINT_0) {
1261 			/*
1262 			 * endpoint 0 is special, it always has a credit and
1263 			 * does not require credit based flow control
1264 			 */
1265 			creditsRequired = 0;
1266 		} else {
1267 
1268 			if (pEndpoint->TxCredits < creditsRequired) {
1269 #if DEBUG_CREDIT
1270 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1271 						("EP%d,No Credit now.%d < %d\n",
1272 						 pEndpoint->Id,
1273 						 pEndpoint->TxCredits,
1274 						 creditsRequired));
1275 #endif
1276 				if (do_pm_get) {
1277 					hif_rtpm_put(HIF_RTPM_PUT_ASYNC,
1278 						     rtpm_code);
1279 					htc_dec_wmi_runtime_cnt(target,
1280 								rtpm_code);
1281 				}
1282 
1283 				break;
1284 			}
1285 
1286 			pEndpoint->TxCredits -= creditsRequired;
1287 			INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
1288 					creditsRequired);
1289 
1290 			/* check if we need credits back from the target */
1291 			if (pEndpoint->TxCredits <=
1292 			    pEndpoint->TxCreditsPerMaxMsg) {
1293 				/* tell the target we need credits ASAP! */
1294 				sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
1295 				if (pEndpoint->service_id == WMI_CONTROL_SVC) {
1296 					htc_credit_record(HTC_REQUEST_CREDIT,
1297 							  pEndpoint->TxCredits,
1298 							  HTC_PACKET_QUEUE_DEPTH
1299 							  (tx_queue));
1300 					hif_latency_detect_credit_record_time(
1301 						HIF_REQUEST_CREDIT,
1302 						target->hif_dev);
1303 				}
1304 				INC_HTC_EP_STAT(pEndpoint,
1305 						TxCreditLowIndications, 1);
1306 #if DEBUG_CREDIT
1307 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1308 						(" EP%d Needs Credits\n",
1309 						 pEndpoint->Id));
1310 #endif
1311 			}
1312 		}
1313 
1314 		/* now we can fully dequeue */
1315 		pPacket = htc_packet_dequeue(tx_queue);
1316 		if (pPacket) {
1317 			/* save the number of credits this packet consumed */
1318 			pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
1319 			/* save send flags */
1320 			pPacket->PktInfo.AsTx.SendFlags = sendFlags;
1321 
1322 			/* queue this packet into the caller's queue */
1323 			HTC_PACKET_ENQUEUE(pQueue, pPacket);
1324 		}
1325 	}
1326 
1327 	if (!HTC_QUEUE_EMPTY(&pm_queue))
1328 		queue_htc_pm_packets(pEndpoint, &pm_queue);
1329 
1330 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1331 			("-get_htc_send_packets_credit_based\n"));
1332 
1333 }
1334 
1335 static void get_htc_send_packets(HTC_TARGET *target,
1336 				 HTC_ENDPOINT *pEndpoint,
1337 				 HTC_PACKET_QUEUE *pQueue, int Resources)
1338 {
1339 
1340 	HTC_PACKET *pPacket;
1341 	HTC_PACKET_QUEUE *tx_queue;
1342 	HTC_PACKET_QUEUE pm_queue;
1343 	bool do_pm_get = false;
1344 	unsigned int rtpm_code = 0;
1345 	int ret;
1346 
1347 	/*** NOTE : the TX lock is held when this function is called ***/
1348 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1349 			("+get_htc_send_packets %d resources\n", Resources));
1350 
1351 	INIT_HTC_PACKET_QUEUE(&pm_queue);
1352 	extract_htc_pm_packets(pEndpoint, &pm_queue);
1353 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
1354 		tx_queue = &pEndpoint->TxQueue;
1355 		do_pm_get = true;
1356 	} else {
1357 		tx_queue = &pm_queue;
1358 	}
1359 
1360 	/* loop until we can grab as many packets out of the queue as we can */
1361 	while (Resources > 0) {
1362 		int num_frags;
1363 
1364 		if (do_pm_get) {
1365 			rtpm_code =
1366 				htc_send_pkts_get_rtpm_id(
1367 							pEndpoint->service_id);
1368 			ret = hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code);
1369 			if (ret) {
1370 				/* bus suspended, runtime resume issued */
1371 				QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
1372 				pPacket = htc_get_pkt_at_head(tx_queue);
1373 				if (!pPacket)
1374 					break;
1375 				log_packet_info(target, pPacket);
1376 				break;
1377 			}
1378 			htc_inc_wmi_runtime_cnt(target, rtpm_code);
1379 
1380 		}
1381 
1382 		ret = hif_system_pm_state_check(target->hif_dev);
1383 		if (ret) {
1384 			if (do_pm_get) {
1385 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1386 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1387 			}
1388 			break;
1389 		}
1390 
1391 		pPacket = htc_packet_dequeue(tx_queue);
1392 		if (!pPacket) {
1393 			if (do_pm_get) {
1394 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1395 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1396 			}
1397 			break;
1398 		}
1399 
1400 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1401 				(" Got packet:%pK , New Queue Depth: %d\n",
1402 				 pPacket,
1403 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
1404 		/* For non-credit path the sequence number is already embedded
1405 		 * in the constructed HTC header
1406 		 */
1407 		pPacket->PktInfo.AsTx.SendFlags = 0;
1408 		pPacket->PktInfo.AsTx.CreditsUsed = 0;
1409 		/* queue this packet into the caller's queue */
1410 		HTC_PACKET_ENQUEUE(pQueue, pPacket);
1411 
1412 		/*
1413 		 * FIX THIS:
1414 		 * For now, avoid calling qdf_nbuf_get_num_frags before calling
1415 		 * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
1416 		 * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
1417 		 * been done.
1418 		 * Assume that the non-data netbufs, i.e. WMI message netbufs,
1419 		 * consist of a single fragment.
1420 		 */
1421 		/* WMI messages are in a single-fragment network buf */
1422 		num_frags =
1423 			(pPacket->PktInfo.AsTx.
1424 			 Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
1425 			qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
1426 						       (pPacket));
1427 		Resources -= num_frags;
1428 	}
1429 
1430 	if (!HTC_QUEUE_EMPTY(&pm_queue))
1431 		queue_htc_pm_packets(pEndpoint, &pm_queue);
1432 
1433 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
1434 
1435 }
1436 
1437 /**
1438  * htc_try_send() - Send packets in a queue on an endpoint
1439  * @target: HTC target on which packets need to be sent
1440  * @pEndpoint: logical endpoint on which packets needs to be sent
1441  * @pCallersSendQueue: packet queue containing the list of packets to be sent
1442  *
1443  * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
1444  *         be sent or the packet should be dropped by the upper layer
1445  */
1446 static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
1447 					  HTC_ENDPOINT *pEndpoint,
1448 					  HTC_PACKET_QUEUE *pCallersSendQueue)
1449 {
1450 	/* temp queue to hold packets at various stages */
1451 	HTC_PACKET_QUEUE sendQueue;
1452 	HTC_PACKET *pPacket;
1453 	int tx_resources;
1454 	int overflow;
1455 	enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
1456 	QDF_STATUS status;
1457 
1458 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
1459 					 pCallersSendQueue,
1460 					 (pCallersSendQueue ==
1461 					  NULL) ? 0 :
1462 					 HTC_PACKET_QUEUE_DEPTH
1463 						 (pCallersSendQueue)));
1464 
1465 	/* init the local send queue */
1466 	INIT_HTC_PACKET_QUEUE(&sendQueue);
1467 
1468 	do {
1469 
1470 		/* caller didn't provide a queue, just wants us to check
1471 		 * queues and send
1472 		 */
1473 		if (!pCallersSendQueue)
1474 			break;
1475 
1476 		if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
1477 			/* empty queue */
1478 			OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1479 							HTC_PKT_Q_EMPTY);
1480 			result = HTC_SEND_QUEUE_DROP;
1481 			break;
1482 		}
1483 
1484 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
1485 		    pEndpoint->MaxTxQueueDepth) {
1486 			/* we've already overflowed */
1487 			overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1488 		} else {
1489 			/* figure out how much we will overflow by */
1490 			overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1491 			overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1492 			/* get how much we will overflow the TX queue by */
1493 			overflow -= pEndpoint->MaxTxQueueDepth;
1494 		}
1495 
1496 		/* if overflow is negative or zero, we are okay */
1497 		if (overflow > 0) {
1498 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1499 					("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
1500 					 pEndpoint->Id, overflow,
1501 					 HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
1502 								TxQueue),
1503 					 pEndpoint->MaxTxQueueDepth));
1504 		}
1505 		if ((overflow <= 0)
1506 		    || (!pEndpoint->EpCallBacks.EpSendFull)) {
1507 			/* all packets will fit or caller did not provide send
1508 			 * full indication handler
1509 			 * just move all of them to local sendQueue object
1510 			 */
1511 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
1512 							  pCallersSendQueue);
1513 		} else {
1514 			int i;
1515 			int goodPkts =
1516 				HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
1517 				overflow;
1518 
1519 			A_ASSERT(goodPkts >= 0);
1520 			/* we have overflowed and callback is provided. Dequeue
1521 			 * all non-overflow packets into the sendqueue
1522 			 */
1523 			for (i = 0; i < goodPkts; i++) {
1524 				/* pop off caller's queue */
1525 				pPacket = htc_packet_dequeue(pCallersSendQueue);
1526 				A_ASSERT(pPacket);
1527 				if (pPacket)
1528 					/* insert into local queue */
1529 					HTC_PACKET_ENQUEUE(&sendQueue,
1530 							   pPacket);
1531 			}
1532 
1533 			/* the caller's queue has all the packets that won't fit
1534 			 * walk through the caller's queue and indicate each one
1535 			 * to the send full handler
1536 			 */
1537 			ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
1538 						       QueueHead, pPacket,
1539 						       HTC_PACKET, ListLink) {
1540 
1541 				AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1542 						("Indicating overflowed TX packet: %pK\n",
1543 						 pPacket));
1544 				/*
1545 				 * Remove headroom reserved for HTC_FRAME_HDR
1546 				 * before giving the packet back to the user via
1547 				 * the EpSendFull callback.
1548 				 */
1549 				qdf_nbuf_pull_head
1550 					(GET_HTC_PACKET_NET_BUF_CONTEXT
1551 						(pPacket),
1552 					sizeof(HTC_FRAME_HDR));
1553 				pPacket->PktInfo.AsTx.Flags &=
1554 					~HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1555 
1556 				if (pEndpoint->EpCallBacks.
1557 				    EpSendFull(pEndpoint->EpCallBacks.pContext,
1558 					       pPacket) == HTC_SEND_FULL_DROP) {
1559 					/* callback wants the packet dropped */
1560 					INC_HTC_EP_STAT(pEndpoint, TxDropped,
1561 							1);
1562 					/* leave this one in the caller's queue
1563 					 * for cleanup
1564 					 */
1565 				} else {
1566 					/* callback wants to keep this packet,
1567 					 * remove from caller's queue
1568 					 */
1569 					HTC_PACKET_REMOVE(pCallersSendQueue,
1570 							  pPacket);
1571 					/* put it in the send queue
1572 					 * add HTC_FRAME_HDR space reservation
1573 					 * again
1574 					 */
1575 					qdf_nbuf_push_head
1576 						(GET_HTC_PACKET_NET_BUF_CONTEXT
1577 							(pPacket),
1578 						sizeof(HTC_FRAME_HDR));
1579 					pPacket->PktInfo.AsTx.Flags |=
1580 						HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1581 
1582 					HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1583 				}
1584 
1585 			}
1586 			ITERATE_END;
1587 
1588 			if (HTC_QUEUE_EMPTY(&sendQueue)) {
1589 				/* no packets made it in, caller will cleanup */
1590 				OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1591 							HTC_SEND_Q_EMPTY);
1592 				result = HTC_SEND_QUEUE_DROP;
1593 				break;
1594 			}
1595 		}
1596 
1597 	} while (false);
1598 
1599 	if (result != HTC_SEND_QUEUE_OK) {
1600 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
1601 			result));
1602 		return result;
1603 	}
1604 
1605 	LOCK_HTC_TX(target);
1606 
1607 	if (!HTC_QUEUE_EMPTY(&sendQueue)) {
1608 		if (target->is_nodrop_pkt) {
1609 			/*
1610 			 * nodrop pkts have higher priority than normal pkts,
1611 			 * insert nodrop pkt to head for proper
1612 			 * start/termination of test.
1613 			 */
1614 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1615 					&sendQueue);
1616 			target->is_nodrop_pkt = false;
1617 		} else {
1618 			/* transfer packets to tail */
1619 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
1620 					&sendQueue);
1621 			A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
1622 			INIT_HTC_PACKET_QUEUE(&sendQueue);
1623 		}
1624 	}
1625 
1626 	/* increment tx processing count on entry */
1627 	if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
1628 		/* another thread or task is draining the TX queues on this
1629 		 * endpoint that thread will reset the tx processing count when
1630 		 * the queue is drained
1631 		 */
1632 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1633 		UNLOCK_HTC_TX(target);
1634 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
1635 		return HTC_SEND_QUEUE_OK;
1636 	}
1637 
1638 	/***** beyond this point only 1 thread may enter ******/
1639 
1640 	/* now drain the endpoint TX queue for transmission as long as we have
1641 	 * enough transmit resources
1642 	 */
1643 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1644 		tx_resources =
1645 			hif_get_free_queue_number(target->hif_dev,
1646 						  pEndpoint->UL_PipeID);
1647 	} else {
1648 		tx_resources = 0;
1649 	}
1650 
1651 	while (true) {
1652 
1653 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
1654 			break;
1655 
1656 		if (pEndpoint->async_update &&
1657 			(!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
1658 			(!tx_resources)) {
1659 			hif_schedule_ce_tasklet(target->hif_dev,
1660 						pEndpoint->UL_PipeID);
1661 			break;
1662 		}
1663 
1664 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1665 #if DEBUG_CREDIT
1666 			int cred = pEndpoint->TxCredits;
1667 #endif
1668 			/* credit based mechanism provides flow control based on
1669 			 * target transmit resource availability, we assume that
1670 			 * the HIF layer will always have bus resources greater
1671 			 * than target transmit resources
1672 			 */
1673 			get_htc_send_packets_credit_based(target, pEndpoint,
1674 							  &sendQueue);
1675 #if DEBUG_CREDIT
1676 			if (ep_debug_mask & (1 << pEndpoint->Id)) {
1677 				if (cred - pEndpoint->TxCredits > 0) {
1678 					AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1679 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1680 							 pEndpoint->Id, cred,
1681 							 cred -
1682 							 pEndpoint->TxCredits,
1683 							 pEndpoint->TxCredits));
1684 				}
1685 			}
1686 #endif
1687 		} else {
1688 
1689 		/*
1690 		* Header and payload belongs to the different fragments and
1691 		* consume 2 resource for one HTC package but USB combine into
1692 		* one transfer.And one WMI message only consumes one single
1693 		* resource.
1694 		*/
1695 			if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
1696 				hif_get_bus_type(target->hif_dev) ==
1697 						QDF_BUS_TYPE_USB) {
1698 				if (pEndpoint->service_id ==
1699 					WMI_CONTROL_SVC)
1700 					tx_resources =
1701 					    HTC_MAX_MSG_PER_BUNDLE_TX;
1702 				else
1703 					tx_resources =
1704 					    (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
1705 			}
1706 			/* get all the packets for this endpoint that we can for
1707 			 * this pass
1708 			 */
1709 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1710 					     tx_resources);
1711 		}
1712 
1713 		if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
1714 			/* didn't get any packets due to a lack of resources or
1715 			 * TX queue was drained
1716 			 */
1717 			break;
1718 		}
1719 
1720 		if (!pEndpoint->async_update)
1721 			UNLOCK_HTC_TX(target);
1722 
1723 		/* send what we can */
1724 		status = htc_issue_packets(target, pEndpoint, &sendQueue);
1725 		if (status) {
1726 			int i;
1727 			unsigned int rtpm_code;
1728 
1729 			result = HTC_SEND_QUEUE_DROP;
1730 
1731 			switch (status) {
1732 			case  QDF_STATUS_E_RESOURCES:
1733 				if (pEndpoint->num_requeues_warn <= MAX_REQUEUE_WARN) {
1734 					pEndpoint->num_requeues_warn++;
1735 					pEndpoint->total_num_requeues++;
1736 					break;
1737 				} else {
1738 					pEndpoint->total_num_requeues++;
1739 					pEndpoint->num_requeues_warn = 0;
1740 				}
1741 				fallthrough;
1742 			default:
1743 				QDF_TRACE(QDF_MODULE_ID_HIF, QDF_TRACE_LEVEL_INFO,
1744 					  "htc_issue_packets, failed status:%d"
1745 					  "endpoint:%d, put it back to head of"
1746 					  "callersSendQueue", result, pEndpoint->Id);
1747 				break;
1748 			}
1749 
1750 			rtpm_code = htc_send_pkts_get_rtpm_id(
1751 							pEndpoint->service_id);
1752 			for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--) {
1753 				hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
1754 				htc_dec_wmi_runtime_cnt(target, rtpm_code);
1755 			}
1756 
1757 			if (!pEndpoint->async_update) {
1758 				LOCK_HTC_TX(target);
1759 			}
1760 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1761 							  &sendQueue);
1762 			break;
1763 		}  else {
1764 			if (pEndpoint->num_requeues_warn)
1765 				pEndpoint->num_requeues_warn = 0;
1766 		}
1767 
1768 		if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1769 			tx_resources =
1770 				hif_get_free_queue_number(target->hif_dev,
1771 							  pEndpoint->UL_PipeID);
1772 		}
1773 
1774 		if (!pEndpoint->async_update) {
1775 			LOCK_HTC_TX(target);
1776 		}
1777 
1778 	}
1779 
1780 	/* done with this endpoint, we can clear the count */
1781 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1782 
1783 	UNLOCK_HTC_TX(target);
1784 
1785 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
1786 
1787 	return HTC_SEND_QUEUE_OK;
1788 }
1789 
1790 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1791 static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
1792 					  HTC_ENDPOINT_ID id)
1793 {
1794 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1795 	HTC_ENDPOINT *pEndpoint;
1796 	HTC_ENDPOINT_ID eid;
1797 	HTC_PACKET_QUEUE *pTxQueue;
1798 	uint16_t resources;
1799 	uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
1800 
1801 	if (id < ENDPOINT_2 || id > ENDPOINT_5)
1802 		return 1;
1803 
1804 	for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
1805 		pEndpoint = &target->endpoint[eid];
1806 		pTxQueue = &pEndpoint->TxQueue;
1807 
1808 		if (HTC_QUEUE_EMPTY(pTxQueue))
1809 			acQueueStatus[eid - 2] = 1;
1810 	}
1811 
1812 	switch (id) {
1813 	case ENDPOINT_2:        /* BE */
1814 		return acQueueStatus[0] && acQueueStatus[2]
1815 			&& acQueueStatus[3];
1816 	case ENDPOINT_3:        /* BK */
1817 		return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
1818 			&& acQueueStatus[3];
1819 	case ENDPOINT_4:        /* VI */
1820 		return acQueueStatus[2] && acQueueStatus[3];
1821 	case ENDPOINT_5:        /* VO */
1822 		return acQueueStatus[3];
1823 	default:
1824 		return 0;
1825 	}
1826 
1827 }
1828 
1829 static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
1830 					  HTC_PACKET_QUEUE *pPktQueue,
1831 					  HTC_ENDPOINT_ID eid)
1832 {
1833 	HTC_ENDPOINT *pEndpoint;
1834 	HTC_PACKET_QUEUE *pTxQueue;
1835 	HTC_PACKET *pPacket;
1836 	int goodPkts;
1837 
1838 	pEndpoint = &target->endpoint[eid];
1839 	pTxQueue = &pEndpoint->TxQueue;
1840 
1841 	LOCK_HTC_TX(target);
1842 
1843 	goodPkts =
1844 		pEndpoint->MaxTxQueueDepth -
1845 		HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1846 
1847 	if (goodPkts > 0) {
1848 		while (!HTC_QUEUE_EMPTY(pPktQueue)) {
1849 			pPacket = htc_packet_dequeue(pPktQueue);
1850 			HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1851 			goodPkts--;
1852 
1853 			if (goodPkts <= 0)
1854 				break;
1855 		}
1856 	}
1857 
1858 	if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
1859 		ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
1860 					       HTC_PACKET, ListLink) {
1861 
1862 			if (pEndpoint->EpCallBacks.
1863 			    EpSendFull(pEndpoint->EpCallBacks.pContext,
1864 				       pPacket) == HTC_SEND_FULL_DROP) {
1865 				INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
1866 			} else {
1867 				HTC_PACKET_REMOVE(pPktQueue, pPacket);
1868 				HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1869 			}
1870 		}
1871 		ITERATE_END;
1872 	}
1873 
1874 	UNLOCK_HTC_TX(target);
1875 
1876 	return A_OK;
1877 }
1878 
1879 #endif
1880 
1881 static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
1882 				HTC_PACKET *pPacket)
1883 {
1884 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1885 	HTC_ENDPOINT *pEndpoint;
1886 	HTC_PACKET_QUEUE pPktQueue;
1887 	qdf_nbuf_t netbuf;
1888 	HTC_FRAME_HDR *htc_hdr;
1889 	QDF_STATUS status;
1890 
1891 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1892 			("+__htc_send_pkt\n"));
1893 
1894 	/* get packet at head to figure out which endpoint these packets will
1895 	 * go into
1896 	 */
1897 	if (!pPacket) {
1898 		OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
1899 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1900 		return QDF_STATUS_E_INVAL;
1901 	}
1902 
1903 	if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1904 	    (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1905 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1906 			("%s endpoint is invalid\n", __func__));
1907 		AR_DEBUG_ASSERT(0);
1908 		return QDF_STATUS_E_INVAL;
1909 	}
1910 	pEndpoint = &target->endpoint[pPacket->Endpoint];
1911 
1912 	if (!pEndpoint->service_id) {
1913 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
1914 								__func__));
1915 		return QDF_STATUS_E_INVAL;
1916 	}
1917 
1918 #ifdef HTC_EP_STAT_PROFILING
1919 	LOCK_HTC_TX(target);
1920 	INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
1921 	UNLOCK_HTC_TX(target);
1922 #endif
1923 
1924 	/* provide room in each packet's netbuf for the HTC frame header */
1925 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1926 	AR_DEBUG_ASSERT(netbuf);
1927 	if (!netbuf)
1928 		return QDF_STATUS_E_INVAL;
1929 
1930 	qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
1931 	pPacket->PktInfo.AsTx.Flags |=
1932 		HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
1933 	/* setup HTC frame header */
1934 	htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1935 	AR_DEBUG_ASSERT(htc_hdr);
1936 	if (!htc_hdr)
1937 		return QDF_STATUS_E_INVAL;
1938 
1939 	HTC_WRITE32(htc_hdr,
1940 		    SM(pPacket->ActualLength,
1941 		       HTC_FRAME_HDR_PAYLOADLEN) |
1942 		    SM(pPacket->Endpoint,
1943 		       HTC_FRAME_HDR_ENDPOINTID));
1944 	LOCK_HTC_TX(target);
1945 
1946 	pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1947 	pEndpoint->SeqNo++;
1948 
1949 	HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
1950 		    SM(pPacket->PktInfo.AsTx.SeqNo,
1951 		       HTC_FRAME_HDR_CONTROLBYTES1));
1952 
1953 	UNLOCK_HTC_TX(target);
1954 
1955 	/*
1956 	 * For flow control enabled endpoints mapping is done in
1957 	 * htc_issue_packets and for non flow control enabled endpoints
1958 	 * its done here.
1959 	 */
1960 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1961 		pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
1962 		status = qdf_nbuf_map(target->osdev,
1963 				      GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
1964 				      QDF_DMA_TO_DEVICE);
1965 		if (status == QDF_STATUS_SUCCESS) {
1966 			target->nbuf_nfc_map_count++;
1967 		} else {
1968 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1969 					("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
1970 					 __func__, pEndpoint, pEndpoint->SeqNo));
1971 			return status;
1972 		}
1973 	}
1974 
1975 	INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
1976 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1977 	if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
1978 		htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
1979 	else
1980 		htc_try_send(target, pEndpoint, &pPktQueue);
1981 #else
1982 	htc_try_send(target, pEndpoint, &pPktQueue);
1983 #endif
1984 
1985 	/* do completion on any packets that couldn't get in */
1986 	while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
1987 		pPacket = htc_packet_dequeue(&pPktQueue);
1988 
1989 		if (HTC_STOPPING(target))
1990 			pPacket->Status = QDF_STATUS_E_CANCELED;
1991 		else
1992 			pPacket->Status = QDF_STATUS_E_RESOURCES;
1993 
1994 		send_packet_completion(target, pPacket);
1995 	}
1996 
1997 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1998 
1999 	return QDF_STATUS_SUCCESS;
2000 }
2001 
2002 #ifdef CUSTOM_CB_SCHEDULER_SUPPORT
2003 /**
2004  * htc_get_endpoint_ul_pipeid() - Helper API to get uplink pipe ID
2005  * @htc_handle: HTC handle
2006  * @endpoint_id: Endpoint ID
2007  * @ul_pipeid: Pointer to uplink pipe ID
2008  *
2009  * return: QDF_STATUS
2010  */
2011 static QDF_STATUS
2012 htc_get_endpoint_ul_pipeid(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id,
2013 			   uint8_t *ul_pipeid)
2014 {
2015 	HTC_TARGET *target;
2016 	HTC_ENDPOINT *end_point;
2017 
2018 	if (!htc_handle) {
2019 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2020 				("%s: HTCHandle is NULL\n", __func__));
2021 		return QDF_STATUS_E_NULL_VALUE;
2022 	}
2023 	target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
2024 
2025 	if (endpoint_id >= ENDPOINT_MAX || endpoint_id <= ENDPOINT_UNUSED) {
2026 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2027 				("%s endpoint is invalid\n", __func__));
2028 		AR_DEBUG_ASSERT(0);
2029 		return QDF_STATUS_E_INVAL;
2030 	}
2031 	end_point = &target->endpoint[endpoint_id];
2032 
2033 	if (!end_point->service_id) {
2034 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
2035 						 __func__));
2036 		return QDF_STATUS_E_INVAL;
2037 	}
2038 
2039 	*ul_pipeid = end_point->UL_PipeID;
2040 
2041 	return QDF_STATUS_SUCCESS;
2042 }
2043 
2044 QDF_STATUS
2045 htc_register_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id,
2046 		       void (*custom_cb)(void *), void *custom_cb_context)
2047 {
2048 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2049 	uint8_t ul_pipeid;
2050 
2051 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
2052 
2053 	status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
2054 					    &ul_pipeid);
2055 	if (QDF_IS_STATUS_ERROR(status)) {
2056 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
2057 						 __func__));
2058 		goto exit;
2059 	}
2060 
2061 	status = hif_register_ce_custom_cb(htc_get_hif_device(htc_handle),
2062 					   ul_pipeid, custom_cb,
2063 					   custom_cb_context);
2064 	if (QDF_IS_STATUS_ERROR(status)) {
2065 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to register cb\n",
2066 						 __func__));
2067 		goto exit;
2068 	}
2069 
2070 exit:
2071 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
2072 
2073 	return status;
2074 }
2075 
2076 QDF_STATUS
2077 htc_unregister_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
2078 {
2079 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2080 	uint8_t ul_pipeid;
2081 
2082 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
2083 
2084 	status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
2085 					    &ul_pipeid);
2086 	if (QDF_IS_STATUS_ERROR(status)) {
2087 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
2088 						 __func__));
2089 		goto exit;
2090 	}
2091 
2092 	status = hif_unregister_ce_custom_cb(htc_get_hif_device(htc_handle),
2093 					     ul_pipeid);
2094 	if (QDF_IS_STATUS_ERROR(status)) {
2095 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to unregister cb\n",
2096 						 __func__));
2097 		status = QDF_STATUS_E_INVAL;
2098 		goto exit;
2099 	}
2100 
2101 exit:
2102 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
2103 
2104 	return status;
2105 }
2106 
2107 QDF_STATUS
2108 htc_enable_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
2109 {
2110 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2111 	uint8_t ul_pipeid;
2112 
2113 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
2114 
2115 	status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
2116 					    &ul_pipeid);
2117 	if (QDF_IS_STATUS_ERROR(status)) {
2118 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
2119 						 __func__));
2120 		goto exit;
2121 	}
2122 
2123 	status = hif_enable_ce_custom_cb(htc_get_hif_device(htc_handle),
2124 					 ul_pipeid);
2125 	if (QDF_IS_STATUS_ERROR(status)) {
2126 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to enable cb\n",
2127 						 __func__));
2128 		status = QDF_STATUS_E_INVAL;
2129 		goto exit;
2130 	}
2131 
2132 exit:
2133 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
2134 
2135 	return status;
2136 }
2137 
2138 QDF_STATUS
2139 htc_disable_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
2140 {
2141 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2142 	uint8_t ul_pipeid;
2143 
2144 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
2145 
2146 	status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
2147 					    &ul_pipeid);
2148 	if (QDF_IS_STATUS_ERROR(status)) {
2149 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
2150 						 __func__));
2151 		goto exit;
2152 	}
2153 
2154 	status = hif_disable_ce_custom_cb(htc_get_hif_device(htc_handle),
2155 					  ul_pipeid);
2156 	if (QDF_IS_STATUS_ERROR(status)) {
2157 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to disable cb\n",
2158 						 __func__));
2159 		status = QDF_STATUS_E_INVAL;
2160 		goto exit;
2161 	}
2162 
2163 exit:
2164 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
2165 
2166 	return status;
2167 }
2168 #endif /* CUSTOM_CB_SCHEDULER_SUPPORT */
2169 
2170 /* HTC API - htc_send_pkt */
2171 QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
2172 {
2173 	if (!htc_handle) {
2174 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2175 				("%s: HTCHandle is NULL \n", __func__));
2176 		return QDF_STATUS_E_FAILURE;
2177 	}
2178 
2179 	if (!htc_packet) {
2180 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2181 				("%s: pPacket is NULL \n", __func__));
2182 		return QDF_STATUS_E_FAILURE;
2183 	}
2184 
2185 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2186 			("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
2187 			 htc_packet->Endpoint, htc_packet->pBuffer,
2188 			 htc_packet->ActualLength));
2189 	return __htc_send_pkt(htc_handle, htc_packet);
2190 }
2191 qdf_export_symbol(htc_send_pkt);
2192 
2193 #ifdef ATH_11AC_TXCOMPACT
2194 /**
2195  * htc_send_data_pkt() - send single data packet on an endpoint
2196  * @htc_hdl: pointer to HTC handle
2197  * @netbuf: network buffer containing the data to be sent
2198  * @ep_id: endpoint identifier
2199  * @actual_length: length of data that needs to be transmitted
2200  *
2201  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2202  */
2203 QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
2204 			     int actual_length)
2205 {
2206 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2207 	HTC_ENDPOINT *pEndpoint;
2208 	HTC_FRAME_HDR *p_htc_hdr;
2209 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2210 	int tx_resources;
2211 	uint32_t data_attr = 0;
2212 	int htc_payload_len = actual_length;
2213 	unsigned int rtpm_code;
2214 
2215 	pEndpoint = &target->endpoint[ep_id];
2216 
2217 	tx_resources = hif_get_free_queue_number(target->hif_dev,
2218 						 pEndpoint->UL_PipeID);
2219 
2220 	if (tx_resources < HTC_DATA_RESOURCE_THRS) {
2221 		if (pEndpoint->ul_is_polled) {
2222 			hif_send_complete_check(pEndpoint->target->hif_dev,
2223 						pEndpoint->UL_PipeID, 1);
2224 			tx_resources =
2225 				hif_get_free_queue_number(target->hif_dev,
2226 							  pEndpoint->UL_PipeID);
2227 		}
2228 		if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
2229 			return QDF_STATUS_E_FAILURE;
2230 	}
2231 
2232 	rtpm_code = htc_send_pkts_get_rtpm_id(
2233 					pEndpoint->service_id);
2234 	if (hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code))
2235 		return QDF_STATUS_E_FAILURE;
2236 
2237 	p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
2238 	AR_DEBUG_ASSERT(p_htc_hdr);
2239 
2240 	data_attr = qdf_nbuf_data_attr_get(netbuf);
2241 
2242 	if (target->htc_hdr_length_check)
2243 		htc_payload_len = actual_length - HTC_HEADER_LEN;
2244 
2245 	HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
2246 		    | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
2247 	/*
2248 	 * If the HIF pipe for the data endpoint is polled rather than
2249 	 * interrupt-driven, this is a good point to check whether any
2250 	 * data previously sent through the HIF pipe have finished being
2251 	 * sent.
2252 	 * Since this may result in callbacks to htc_tx_completion_handler,
2253 	 * which can take the HTC tx lock, make the hif_send_complete_check
2254 	 * call before acquiring the HTC tx lock.
2255 	 * Call hif_send_complete_check directly, rather than calling
2256 	 * htc_send_complete_check, and call the PollTimerStart separately
2257 	 * after calling hif_send_head, so the timer will be started to
2258 	 * check for completion of the new outstanding download (in the
2259 	 * unexpected event that other polling calls don't catch it).
2260 	 */
2261 
2262 	LOCK_HTC_TX(target);
2263 
2264 	HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
2265 		    SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
2266 
2267 	pEndpoint->SeqNo++;
2268 
2269 	QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
2270 	DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
2271 		QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
2272 		sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
2273 	status = hif_send_head(target->hif_dev,
2274 			       pEndpoint->UL_PipeID,
2275 			       pEndpoint->Id, actual_length, netbuf, data_attr);
2276 
2277 	UNLOCK_HTC_TX(target);
2278 	return status;
2279 }
2280 #else                           /*ATH_11AC_TXCOMPACT */
2281 
2282 /**
2283  * htc_send_data_pkt() - htc_send_data_pkt
2284  * @HTCHandle: pointer to HTC handle
2285  * @pPacket: pointer to HTC_PACKET
2286  * @more_data: indicates whether more data is to follow
2287  *
2288  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2289  */
2290 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
2291 			   uint8_t more_data)
2292 {
2293 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2294 	HTC_ENDPOINT *pEndpoint;
2295 	HTC_FRAME_HDR *pHtcHdr;
2296 	HTC_PACKET_QUEUE sendQueue;
2297 	qdf_nbuf_t netbuf = NULL;
2298 	int tx_resources;
2299 	QDF_STATUS status = QDF_STATUS_SUCCESS;
2300 	uint32_t data_attr = 0;
2301 	bool used_extra_tx_credit = false;
2302 
2303 	if (pPacket) {
2304 		if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
2305 		   (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
2306 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2307 				("%s endpoint is invalid\n", __func__));
2308 			AR_DEBUG_ASSERT(0);
2309 			return QDF_STATUS_E_INVAL;
2310 		}
2311 		pEndpoint = &target->endpoint[pPacket->Endpoint];
2312 
2313 		/* add HTC_FRAME_HDR in the initial fragment */
2314 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
2315 		pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
2316 		AR_DEBUG_ASSERT(pHtcHdr);
2317 
2318 		HTC_WRITE32(pHtcHdr,
2319 				SM(pPacket->ActualLength,
2320 				       HTC_FRAME_HDR_PAYLOADLEN) |
2321 				SM(pPacket->PktInfo.AsTx.SendFlags,
2322 					HTC_FRAME_HDR_FLAGS) |
2323 				SM(pPacket->Endpoint,
2324 					HTC_FRAME_HDR_ENDPOINTID));
2325 		/*
2326 		 * If the HIF pipe for the data endpoint is polled rather than
2327 		 * interrupt-driven, this is a good point to check whether any
2328 		 * data previously sent through the HIF pipe have finished being
2329 		 * sent. Since this may result in callbacks to
2330 		 * htc_tx_completion_handler, which can take the HTC tx lock,
2331 		 * make the hif_send_complete_check call before acquiring the
2332 		 * HTC tx lock.
2333 		 * Call hif_send_complete_check directly, rather than calling
2334 		 * htc_send_complete_check, and call the PollTimerStart
2335 		 * separately after calling hif_send_head, so the timer will be
2336 		 * started to check for completion of the new outstanding
2337 		 * download (in the unexpected event that other polling calls
2338 		 * don't catch it).
2339 		 */
2340 		if (pEndpoint->ul_is_polled) {
2341 			htc_send_complete_poll_timer_stop(pEndpoint);
2342 			hif_send_complete_check(pEndpoint->target->hif_dev,
2343 						pEndpoint->UL_PipeID, 0);
2344 		}
2345 
2346 		LOCK_HTC_TX(target);
2347 
2348 		pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
2349 		pEndpoint->SeqNo++;
2350 
2351 		HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
2352 			    SM(pPacket->PktInfo.AsTx.SeqNo,
2353 			       HTC_FRAME_HDR_CONTROLBYTES1));
2354 
2355 		/* append new packet to pEndpoint->TxQueue */
2356 		HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
2357 		if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
2358 			UNLOCK_HTC_TX(target);
2359 			return QDF_STATUS_SUCCESS;
2360 		}
2361 
2362 		QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
2363 		DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
2364 			  QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
2365 				sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
2366 	} else {
2367 		LOCK_HTC_TX(target);
2368 		pEndpoint = &target->endpoint[1];
2369 	}
2370 
2371 	/* increment tx processing count on entry */
2372 	qdf_atomic_inc(&pEndpoint->TxProcessCount);
2373 	if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
2374 		/*
2375 		 * Another thread or task is draining the TX queues on this
2376 		 * endpoint. That thread will reset the tx processing count when
2377 		 * the queue is drained.
2378 		 */
2379 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
2380 		UNLOCK_HTC_TX(target);
2381 		return QDF_STATUS_SUCCESS;
2382 	}
2383 
2384 	/***** beyond this point only 1 thread may enter ******/
2385 
2386 	INIT_HTC_PACKET_QUEUE(&sendQueue);
2387 	if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
2388 #if DEBUG_CREDIT
2389 		int cred = pEndpoint->TxCredits;
2390 #endif
2391 		get_htc_send_packets_credit_based(target, pEndpoint,
2392 						 &sendQueue);
2393 #if DEBUG_CREDIT
2394 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2395 			if (cred - pEndpoint->TxCredits > 0) {
2396 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2397 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
2398 						 pEndpoint->Id, cred,
2399 						 cred - pEndpoint->TxCredits,
2400 						 pEndpoint->TxCredits));
2401 			}
2402 		}
2403 #endif
2404 		UNLOCK_HTC_TX(target);
2405 	}
2406 
2407 	else if (HTC_TX_BUNDLE_ENABLED(target)) {
2408 		if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) {
2409 			if (hif_get_free_queue_number(target->hif_dev,
2410 						      pEndpoint->UL_PipeID))
2411 				/*
2412 				 * Header and payload belongs to the different
2413 				 * fragments and consume 2 resource for one HTC
2414 				 * package but USB combine into one transfer.
2415 				 */
2416 				get_htc_send_packets(target, pEndpoint,
2417 						     &sendQueue,
2418 						     HTC_MAX_MSG_PER_BUNDLE_TX
2419 						     * 2);
2420 		} else {
2421 			/* Dequeue max packets from endpoint tx queue */
2422 			get_htc_send_packets(target, pEndpoint, &sendQueue,
2423 					     HTC_MAX_TX_BUNDLE_SEND_LIMIT);
2424 		}
2425 		UNLOCK_HTC_TX(target);
2426 	} else {
2427 		/*
2428 		 * Now drain the endpoint TX queue for transmission as long as
2429 		 * we have enough transmit resources
2430 		 */
2431 		tx_resources =
2432 			hif_get_free_queue_number(target->hif_dev,
2433 						  pEndpoint->UL_PipeID);
2434 		get_htc_send_packets(target, pEndpoint, &sendQueue,
2435 				     tx_resources);
2436 		UNLOCK_HTC_TX(target);
2437 	}
2438 
2439 	/* send what we can */
2440 	while (true) {
2441 		if (HTC_TX_BUNDLE_ENABLED(target) &&
2442 		    (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
2443 		     HTC_MIN_MSG_PER_BUNDLE) &&
2444 		    (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
2445 		     hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
2446 			if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2447 				if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
2448 					status = QDF_STATUS_E_RESOURCES;
2449 					/* put the sendQueue back at the front
2450 					 * of pEndpoint->TxQueue
2451 					 */
2452 					LOCK_HTC_TX(target);
2453 					HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
2454 							&pEndpoint->TxQueue,
2455 							&sendQueue);
2456 					UNLOCK_HTC_TX(target);
2457 					break;
2458 				}
2459 			}
2460 			htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
2461 		}
2462 		if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2463 			if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
2464 				status = QDF_STATUS_E_RESOURCES;
2465 				/* put the sendQueue back at the front
2466 				 * of pEndpoint->TxQueue
2467 				 */
2468 				LOCK_HTC_TX(target);
2469 				HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
2470 							&pEndpoint->TxQueue,
2471 							&sendQueue);
2472 				UNLOCK_HTC_TX(target);
2473 				break;
2474 			}
2475 		}
2476 		pPacket = htc_packet_dequeue(&sendQueue);
2477 		if (!pPacket)
2478 			break;
2479 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
2480 		pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
2481 
2482 		LOCK_HTC_TX(target);
2483 		/* store in look up queue to match completions */
2484 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
2485 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
2486 		pEndpoint->ul_outstanding_cnt++;
2487 		UNLOCK_HTC_TX(target);
2488 
2489 		used_extra_tx_credit =
2490 				htc_handle_extra_tx_credit(pEndpoint, pPacket,
2491 							   (uint8_t *)pHtcHdr,
2492 							   NULL,
2493 							   pPacket->ActualLength
2494 							   + HTC_HDR_LENGTH);
2495 
2496 		status = hif_send_head(target->hif_dev,
2497 				       pEndpoint->UL_PipeID,
2498 				       pEndpoint->Id,
2499 				       HTC_HDR_LENGTH + pPacket->ActualLength,
2500 				       netbuf, data_attr);
2501 		if (status != QDF_STATUS_SUCCESS) {
2502 			if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
2503 				if (used_extra_tx_credit) {
2504 					pEndpoint->EpCallBacks.
2505 					ep_padding_credit_update
2506 					(pEndpoint->EpCallBacks.pContext, 1);
2507 				}
2508 			}
2509 		}
2510 #if DEBUG_BUNDLE
2511 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
2512 			  pEndpoint->Id,
2513 			  pEndpoint->TxCreditSize,
2514 			  HTC_HDR_LENGTH + pPacket->ActualLength);
2515 #endif
2516 
2517 		htc_issue_tx_bundle_stats_inc(target);
2518 
2519 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
2520 			LOCK_HTC_TX(target);
2521 			pEndpoint->ul_outstanding_cnt--;
2522 			/* remove this packet from the tx completion queue */
2523 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
2524 
2525 			/*
2526 			 * Don't bother reclaiming credits - HTC flow control
2527 			 * is not applicable to tx data.
2528 			 * In LL systems, there is no download flow control,
2529 			 * since there's virtually no download delay.
2530 			 * In HL systems, the txrx SW explicitly performs the
2531 			 * tx flow control.
2532 			 */
2533 			/* pEndpoint->TxCredits +=
2534 			 * pPacket->PktInfo.AsTx.CreditsUsed;
2535 			 */
2536 
2537 			/* put this frame back at the front of the sendQueue */
2538 			HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
2539 
2540 			/* put the sendQueue back at the front of
2541 			 * pEndpoint->TxQueue
2542 			 */
2543 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
2544 							  &sendQueue);
2545 			UNLOCK_HTC_TX(target);
2546 			break;  /* still need to reset TxProcessCount */
2547 		}
2548 	}
2549 	/* done with this endpoint, we can clear the count */
2550 	qdf_atomic_init(&pEndpoint->TxProcessCount);
2551 
2552 	if (pEndpoint->ul_is_polled) {
2553 		/*
2554 		 * Start a cleanup timer to poll for download completion.
2555 		 * The download completion should be noticed promptly from
2556 		 * other polling calls, but the timer provides a safety net
2557 		 * in case other polling calls don't occur as expected.
2558 		 */
2559 		htc_send_complete_poll_timer_start(pEndpoint);
2560 	}
2561 
2562 	return status;
2563 }
2564 #endif /*ATH_11AC_TXCOMPACT */
2565 qdf_export_symbol(htc_send_data_pkt);
2566 
2567 /*
2568  * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
2569  * since upper layers expects HTC_PACKET containers we use the completed netbuf
2570  * and lookup its corresponding HTC packet buffer from a lookup list.
2571  * This is extra overhead that can be fixed by re-aligning HIF interfaces
2572  * with HTC.
2573  *
2574  */
2575 static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
2576 					HTC_ENDPOINT *pEndpoint,
2577 					qdf_nbuf_t netbuf)
2578 {
2579 	HTC_PACKET *pPacket = NULL;
2580 	HTC_PACKET *pFoundPacket = NULL;
2581 	HTC_PACKET_QUEUE lookupQueue;
2582 
2583 	INIT_HTC_PACKET_QUEUE(&lookupQueue);
2584 	LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2585 
2586 	LOCK_HTC_TX(target);
2587 	/* mark that HIF has indicated the send complete for another packet */
2588 	pEndpoint->ul_outstanding_cnt--;
2589 
2590 	/* Dequeue first packet directly because of in-order completion */
2591 	pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
2592 	if (qdf_unlikely(!pPacket)) {
2593 		UNLOCK_HTC_TX(target);
2594 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2595 		return NULL;
2596 	}
2597 	if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
2598 		UNLOCK_HTC_TX(target);
2599 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2600 		return pPacket;
2601 	}
2602 	HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
2603 
2604 	/*
2605 	 * Move TX lookup queue to temp queue because most of packets that are
2606 	 * not index 0 are not top 10 packets.
2607 	 */
2608 	HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
2609 					  &pEndpoint->TxLookupQueue);
2610 
2611 	ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
2612 				       HTC_PACKET, ListLink) {
2613 
2614 		if (!pPacket) {
2615 			pFoundPacket = pPacket;
2616 			break;
2617 		}
2618 		/* check for removal */
2619 		if (netbuf ==
2620 		    (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
2621 			/* found it */
2622 			HTC_PACKET_REMOVE(&lookupQueue, pPacket);
2623 			pFoundPacket = pPacket;
2624 			break;
2625 		}
2626 
2627 	}
2628 	ITERATE_END;
2629 
2630 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
2631 					  &lookupQueue);
2632 	UNLOCK_HTC_TX(target);
2633 	UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
2634 
2635 	return pFoundPacket;
2636 }
2637 
2638 /**
2639  * htc_tx_completion_handler() - htc tx completion handler
2640  * @Context: pointer to HTC_TARGET structure
2641  * @netbuf: pointer to netbuf for which completion handler is being called
2642  * @EpID: end point Id on which the packet was sent
2643  * @toeplitz_hash_result: toeplitz hash result
2644  *
2645  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
2646  */
2647 QDF_STATUS htc_tx_completion_handler(void *Context,
2648 				   qdf_nbuf_t netbuf, unsigned int EpID,
2649 				   uint32_t toeplitz_hash_result)
2650 {
2651 	HTC_TARGET *target = (HTC_TARGET *) Context;
2652 	HTC_ENDPOINT *pEndpoint;
2653 	HTC_PACKET *pPacket;
2654 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
2655 	HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
2656 		ENDPOINT_2, ENDPOINT_3 };
2657 	int epidIdx;
2658 	uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
2659 	uint16_t resources;
2660 	uint16_t resourcesMax;
2661 #endif
2662 
2663 	pEndpoint = &target->endpoint[EpID];
2664 	target->TX_comp_cnt++;
2665 	pEndpoint->htc_comp_cnt++;
2666 
2667 	do {
2668 		pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
2669 		if (!pPacket) {
2670 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2671 					("HTC TX lookup failed!\n"));
2672 			/* may have already been flushed and freed */
2673 			netbuf = NULL;
2674 			break;
2675 		}
2676 		if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM &&
2677 		    pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RUNTIME_PUT &&
2678 		    pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RTPM_PUT_RC) {
2679 			hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_WMI);
2680 			htc_dec_wmi_runtime_cnt(target, HIF_RTPM_ID_WMI);
2681 		}
2682 
2683 
2684 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
2685 			HTC_PACKET *pPacketTemp;
2686 			HTC_PACKET_QUEUE *pQueueSave =
2687 				(HTC_PACKET_QUEUE *) pPacket->pContext;
2688 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
2689 							      pPacketTemp) {
2690 				pPacket->Status = QDF_STATUS_SUCCESS;
2691 				send_packet_completion(target, pPacketTemp);
2692 			}
2693 			HTC_PACKET_QUEUE_ITERATE_END;
2694 			free_htc_bundle_packet(target, pPacket);
2695 
2696 			if (hif_get_bus_type(target->hif_dev) ==
2697 					     QDF_BUS_TYPE_USB) {
2698 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
2699 					htc_try_send(target, pEndpoint, NULL);
2700 			}
2701 
2702 			return QDF_STATUS_SUCCESS;
2703 		}
2704 		/* will be giving this buffer back to upper layers */
2705 		netbuf = NULL;
2706 		pPacket->Status = QDF_STATUS_SUCCESS;
2707 		send_packet_completion(target, pPacket);
2708 
2709 	} while (false);
2710 
2711 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
2712 		/* note: when using TX credit flow, the re-checking of queues
2713 		 * happens when credits flow back from the target. In the non-TX
2714 		 * credit case, we recheck after the packet completes
2715 		 */
2716 		if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
2717 				(!pEndpoint->async_update)) {
2718 			htc_try_send(target, pEndpoint, NULL);
2719 		}
2720 	}
2721 
2722 	return QDF_STATUS_SUCCESS;
2723 }
2724 
2725 #ifdef WLAN_FEATURE_FASTPATH
2726 /**
2727  * htc_ctrl_msg_cmpl() - checks for tx completion for the endpoint specified
2728  * @htc_pdev: pointer to the htc context
2729  * @htc_ep_id: end point id
2730  *
2731  * checks HTC tx completion
2732  *
2733  * Return: none
2734  */
2735 void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
2736 {
2737 	HTC_TARGET      *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
2738 	HTC_ENDPOINT    *pendpoint = &target->endpoint[htc_ep_id];
2739 
2740 	htc_send_complete_check(pendpoint, 1);
2741 }
2742 qdf_export_symbol(htc_ctrl_msg_cmpl);
2743 #endif
2744 
2745 /* callback when TX resources become available */
2746 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
2747 {
2748 	int i;
2749 	HTC_TARGET *target = (HTC_TARGET *) context;
2750 	HTC_ENDPOINT *pEndpoint = NULL;
2751 
2752 	for (i = 0; i < ENDPOINT_MAX; i++) {
2753 		pEndpoint = &target->endpoint[i];
2754 		if (pEndpoint->service_id != 0) {
2755 			if (pEndpoint->UL_PipeID == pipeID)
2756 				break;
2757 		}
2758 	}
2759 
2760 	if (i >= ENDPOINT_MAX) {
2761 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2762 				("Invalid pipe indicated for TX resource avail : %d!\n",
2763 				 pipeID));
2764 		return;
2765 	}
2766 
2767 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2768 			("HIF indicated more resources for pipe:%d\n",
2769 			 pipeID));
2770 
2771 	htc_try_send(target, pEndpoint, NULL);
2772 }
2773 
2774 #ifdef FEATURE_RUNTIME_PM
2775 /**
2776  * htc_kick_queues() - resumes tx transactions of suspended endpoints
2777  * @context: pointer to the htc target context
2778  *
2779  * Iterates through the endpoints and provides a context to empty queues
2780  * int the hif layer when they are stalled due to runtime suspend.
2781  *
2782  * Return: none
2783  */
2784 void htc_kick_queues(void *context)
2785 {
2786 	int i;
2787 	HTC_TARGET *target = (HTC_TARGET *)context;
2788 	HTC_ENDPOINT *endpoint = NULL;
2789 
2790 	if (hif_rtpm_get(HIF_RTPM_GET_SYNC, HIF_RTPM_ID_HTT))
2791 		return;
2792 
2793 	for (i = 0; i < ENDPOINT_MAX; i++) {
2794 		endpoint = &target->endpoint[i];
2795 
2796 		if (endpoint->service_id == 0)
2797 			continue;
2798 
2799 		if (endpoint->EpCallBacks.ep_resume_tx_queue)
2800 			endpoint->EpCallBacks.ep_resume_tx_queue(
2801 					endpoint->EpCallBacks.pContext);
2802 
2803 		htc_try_send(target, endpoint, NULL);
2804 	}
2805 
2806 	hif_fastpath_resume(target->hif_dev);
2807 
2808 	hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_HTT);
2809 }
2810 #endif
2811 
2812 /* flush endpoint TX queue */
2813 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
2814 			   HTC_TX_TAG Tag)
2815 {
2816 	HTC_PACKET *pPacket;
2817 
2818 	LOCK_HTC_TX(target);
2819 	while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2820 		pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
2821 
2822 		if (pPacket) {
2823 			/* let the sender know the packet was not delivered */
2824 			pPacket->Status = QDF_STATUS_E_CANCELED;
2825 			send_packet_completion(target, pPacket);
2826 		}
2827 	}
2828 	UNLOCK_HTC_TX(target);
2829 }
2830 
2831 /* flush pending entries in endpoint TX Lookup queue */
2832 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
2833 				  HTC_ENDPOINT_ID endpoint_id,
2834 				  bool call_ep_callback)
2835 {
2836 	HTC_PACKET *packet;
2837 	HTC_ENDPOINT *endpoint;
2838 
2839 	endpoint = &target->endpoint[endpoint_id];
2840 
2841 	if (!endpoint && endpoint->service_id == 0)
2842 		return;
2843 
2844 	LOCK_HTC_TX(target);
2845 	while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
2846 		packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
2847 
2848 		if (packet) {
2849 			if (call_ep_callback == true) {
2850 				packet->Status = QDF_STATUS_E_CANCELED;
2851 				send_packet_completion(target, packet);
2852 			} else {
2853 				qdf_mem_free(packet);
2854 			}
2855 		}
2856 	}
2857 	UNLOCK_HTC_TX(target);
2858 }
2859 
2860 /* HTC API to flush an endpoint's TX queue*/
2861 void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
2862 			HTC_TX_TAG Tag)
2863 {
2864 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2865 	HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
2866 
2867 	if (pEndpoint->service_id == 0) {
2868 		AR_DEBUG_ASSERT(false);
2869 		/* not in use.. */
2870 		return;
2871 	}
2872 
2873 	htc_flush_endpoint_tx(target, pEndpoint, Tag);
2874 }
2875 
2876 /* HTC API to indicate activity to the credit distribution function */
2877 void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
2878 				  HTC_ENDPOINT_ID Endpoint, bool Active)
2879 {
2880 	/*  TODO */
2881 }
2882 
2883 bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
2884 {
2885 	return true;
2886 }
2887 
2888 void htc_set_pkt_dbg(HTC_HANDLE handle, A_BOOL dbg_flag)
2889 {
2890 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(handle);
2891 
2892 	target->htc_pkt_dbg = dbg_flag;
2893 }
2894 
2895 void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
2896 {
2897 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2898 
2899 	target->is_nodrop_pkt = isNodropPkt;
2900 }
2901 
2902 void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
2903 {
2904 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2905 
2906 	target->htc_hdr_length_check = htc_hdr_length_check;
2907 }
2908 
2909 /**
2910  * htc_process_credit_rpt() - process credit report, call distribution function
2911  * @target: pointer to HTC_TARGET
2912  * @pRpt: pointer to HTC_CREDIT_REPORT
2913  * @NumEntries: number of entries in credit report
2914  * @FromEndpoint: endpoint for which  credit report is received
2915  *
2916  * Return: A_OK for success or an appropriate A_STATUS error
2917  */
2918 void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
2919 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
2920 {
2921 	int i;
2922 	HTC_ENDPOINT *pEndpoint;
2923 	int totalCredits = 0;
2924 	uint8_t rpt_credits, rpt_ep_id;
2925 
2926 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2927 			("+htc_process_credit_rpt, Credit Report Entries:%d\n",
2928 			 NumEntries));
2929 
2930 	/* lock out TX while we update credits */
2931 	LOCK_HTC_TX(target);
2932 
2933 	for (i = 0; i < NumEntries; i++, pRpt++) {
2934 
2935 		rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
2936 
2937 		if (rpt_ep_id >= ENDPOINT_MAX) {
2938 			AR_DEBUG_ASSERT(false);
2939 			break;
2940 		}
2941 
2942 		rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
2943 
2944 		pEndpoint = &target->endpoint[rpt_ep_id];
2945 #if DEBUG_CREDIT
2946 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2947 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2948 					(" <HTC> Increase EP%d %d + %d = %d credits\n",
2949 					 rpt_ep_id, pEndpoint->TxCredits,
2950 					 rpt_credits,
2951 					 pEndpoint->TxCredits + rpt_credits));
2952 		}
2953 #endif
2954 
2955 #ifdef HTC_EP_STAT_PROFILING
2956 
2957 		INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
2958 		INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
2959 
2960 		if (FromEndpoint == rpt_ep_id) {
2961 			/* this credit report arrived on the same endpoint
2962 			 * indicating it arrived in an RX packet
2963 			 */
2964 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
2965 					rpt_credits);
2966 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
2967 		} else if (FromEndpoint == ENDPOINT_0) {
2968 			/* this credit arrived on endpoint 0 as a NULL msg */
2969 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
2970 					rpt_credits);
2971 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
2972 		} else {
2973 			/* arrived on another endpoint */
2974 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
2975 					rpt_credits);
2976 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
2977 		}
2978 
2979 #endif
2980 
2981 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
2982 			htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
2983 					  pEndpoint->TxCredits + rpt_credits,
2984 					  HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
2985 							TxQueue));
2986 			hif_latency_detect_credit_record_time(
2987 					HIF_PROCESS_CREDIT_REPORT,
2988 					target->hif_dev);
2989 		}
2990 
2991 		pEndpoint->TxCredits += rpt_credits;
2992 
2993 		if (pEndpoint->TxCredits
2994 		    && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2995 			UNLOCK_HTC_TX(target);
2996 #ifdef ATH_11AC_TXCOMPACT
2997 			htc_try_send(target, pEndpoint, NULL);
2998 #else
2999 			if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
3000 				htc_send_data_pkt((HTC_HANDLE)target, NULL, 0);
3001 			else
3002 				htc_try_send(target, pEndpoint, NULL);
3003 #endif
3004 			LOCK_HTC_TX(target);
3005 		}
3006 		totalCredits += rpt_credits;
3007 	}
3008 
3009 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
3010 			("  Report indicated %d credits to distribute\n",
3011 			 totalCredits));
3012 
3013 	UNLOCK_HTC_TX(target);
3014 
3015 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
3016 }
3017 
3018 /* function to fetch stats from htc layer*/
3019 struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
3020 {
3021 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
3022 
3023 	return &(target->htc_pkt_stats);
3024 }
3025 
3026 #ifdef SYSTEM_PM_CHECK
3027 void htc_system_resume(HTC_HANDLE htc)
3028 {
3029 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc);
3030 	HTC_ENDPOINT *endpoint = NULL;
3031 	int i;
3032 
3033 	if (!target)
3034 		return;
3035 
3036 	for (i = 0; i < ENDPOINT_MAX; i++) {
3037 		endpoint = &target->endpoint[i];
3038 
3039 		if (endpoint->service_id == 0)
3040 			continue;
3041 
3042 		htc_try_send(target, endpoint, NULL);
3043 	}
3044 }
3045 #endif
3046