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