xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_send.c (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
1 /*
2  * Copyright (c) 2013-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "htc_debug.h"
20 #include "htc_internal.h"
21 #include "htc_credit_history.h"
22 #include <qdf_mem.h>            /* qdf_mem_malloc */
23 #include <qdf_nbuf.h>           /* qdf_nbuf_t */
24 #include "qdf_module.h"
25 
26 /* #define USB_HIF_SINGLE_PIPE_DATA_SCHED */
27 /* #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED */
28 #define DATA_EP_SIZE 4
29 /* #endif */
30 #define HTC_DATA_RESOURCE_THRS 256
31 #define HTC_DATA_MINDESC_PERPACKET 2
32 
33 enum HTC_SEND_QUEUE_RESULT {
34 	HTC_SEND_QUEUE_OK = 0,  /* packet was queued */
35 	HTC_SEND_QUEUE_DROP = 1, /* this packet should be dropped */
36 };
37 
38 #ifndef DEBUG_CREDIT
39 #define DEBUG_CREDIT 0
40 #endif
41 
42 #if DEBUG_CREDIT
43 /* bit mask to enable debug certain endpoint */
44 static unsigned int ep_debug_mask =
45 	(1 << ENDPOINT_0) | (1 << ENDPOINT_1) | (1 << ENDPOINT_2);
46 #endif
47 
48 #ifdef QCA_WIFI_NAPIER_EMULATION
49 #define HTC_EMULATION_DELAY_IN_MS 20
50 /**
51  * htc_add_delay(): Adds a delay in before proceeding, only for emulation
52  *
53  * Return: None
54  */
55 static inline void htc_add_emulation_delay(void)
56 {
57 	qdf_mdelay(HTC_EMULATION_DELAY_IN_MS);
58 }
59 #else
60 static inline void htc_add_emulation_delay(void)
61 {
62 }
63 #endif
64 
65 void htc_dump_counter_info(HTC_HANDLE HTCHandle)
66 {
67 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
68 
69 	AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
70 			("\n%s: ce_send_cnt = %d, TX_comp_cnt = %d\n",
71 			 __func__, target->ce_send_cnt, target->TX_comp_cnt));
72 }
73 
74 int htc_get_tx_queue_depth(HTC_HANDLE *htc_handle, HTC_ENDPOINT_ID endpoint_id)
75 {
76 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
77 	HTC_ENDPOINT *endpoint = &target->endpoint[endpoint_id];
78 
79 	return HTC_PACKET_QUEUE_DEPTH(&endpoint->TxQueue);
80 }
81 qdf_export_symbol(htc_get_tx_queue_depth);
82 
83 void htc_get_control_endpoint_tx_host_credits(HTC_HANDLE HTCHandle,
84 					      int *credits)
85 {
86 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
87 	HTC_ENDPOINT *pEndpoint;
88 	int i;
89 
90 	if (!credits || !target) {
91 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: invalid args", __func__));
92 		return;
93 	}
94 
95 	*credits = 0;
96 	LOCK_HTC_TX(target);
97 	for (i = 0; i < ENDPOINT_MAX; i++) {
98 		pEndpoint = &target->endpoint[i];
99 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
100 			*credits = pEndpoint->TxCredits;
101 			break;
102 		}
103 	}
104 	UNLOCK_HTC_TX(target);
105 }
106 
107 static inline void restore_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
108 {
109 	qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
110 
111 	if (pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
112 		qdf_nbuf_unmap(target->osdev, netbuf, QDF_DMA_TO_DEVICE);
113 		pPacket->PktInfo.AsTx.Flags &= ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
114 	}
115 
116 	qdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
117 }
118 
119 static void send_packet_completion(HTC_TARGET *target, HTC_PACKET *pPacket)
120 {
121 	HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
122 	HTC_EP_SEND_PKT_COMPLETE EpTxComplete;
123 
124 	restore_tx_packet(target, pPacket);
125 
126 	/* do completion */
127 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
128 			("HTC calling ep %d send complete callback on packet %pK\n",
129 			 pEndpoint->Id, pPacket));
130 
131 	EpTxComplete = pEndpoint->EpCallBacks.EpTxComplete;
132 	if (EpTxComplete != NULL)
133 		EpTxComplete(pEndpoint->EpCallBacks.pContext, pPacket);
134 	else
135 		qdf_nbuf_free(pPacket->pPktContext);
136 
137 
138 }
139 
140 void htc_send_complete_check_cleanup(void *context)
141 {
142 	HTC_ENDPOINT *pEndpoint = (HTC_ENDPOINT *) context;
143 
144 	htc_send_complete_check(pEndpoint, 1);
145 }
146 
147 HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target)
148 {
149 	HTC_PACKET *pPacket;
150 	HTC_PACKET_QUEUE *pQueueSave;
151 	qdf_nbuf_t netbuf;
152 
153 	LOCK_HTC_TX(target);
154 	if (NULL == target->pBundleFreeList) {
155 		UNLOCK_HTC_TX(target);
156 		netbuf = qdf_nbuf_alloc(NULL,
157 					target->MaxMsgsPerHTCBundle *
158 					target->TargetCreditSize, 0, 4, false);
159 		AR_DEBUG_ASSERT(netbuf);
160 		if (!netbuf)
161 			return NULL;
162 		pPacket = qdf_mem_malloc(sizeof(HTC_PACKET));
163 		AR_DEBUG_ASSERT(pPacket);
164 		if (!pPacket) {
165 			qdf_nbuf_free(netbuf);
166 			return NULL;
167 		}
168 		pQueueSave = qdf_mem_malloc(sizeof(HTC_PACKET_QUEUE));
169 		AR_DEBUG_ASSERT(pQueueSave);
170 		if (!pQueueSave) {
171 			qdf_nbuf_free(netbuf);
172 			qdf_mem_free(pPacket);
173 			return NULL;
174 		}
175 		INIT_HTC_PACKET_QUEUE(pQueueSave);
176 		pPacket->pContext = pQueueSave;
177 		SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
178 		pPacket->pBuffer = qdf_nbuf_data(netbuf);
179 		pPacket->BufferLength = qdf_nbuf_len(netbuf);
180 
181 		/* store the original head room so that we can restore this
182 		 * when we "free" the packet.
183 		 * free packet puts the packet back on the free list
184 		 */
185 		pPacket->netbufOrigHeadRoom = qdf_nbuf_headroom(netbuf);
186 		return pPacket;
187 	}
188 	/* already done malloc - restore from free list */
189 	pPacket = target->pBundleFreeList;
190 	AR_DEBUG_ASSERT(pPacket);
191 	if (!pPacket) {
192 		UNLOCK_HTC_TX(target);
193 		return NULL;
194 	}
195 	target->pBundleFreeList = (HTC_PACKET *) pPacket->ListLink.pNext;
196 	UNLOCK_HTC_TX(target);
197 	pPacket->ListLink.pNext = NULL;
198 
199 	return pPacket;
200 }
201 
202 void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
203 {
204 	uint32_t curentHeadRoom;
205 	qdf_nbuf_t netbuf;
206 	HTC_PACKET_QUEUE *pQueueSave;
207 
208 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
209 	AR_DEBUG_ASSERT(netbuf);
210 	if (!netbuf) {
211 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
212 				("\n%s: Invalid netbuf in HTC Packet\n",
213 				__func__));
214 		return;
215 	}
216 	/* HIF adds data to the headroom section of the nbuf, restore thei
217 	 * original size. If this is not done, headroom keeps shrinking with
218 	 * every HIF send and eventually HIF ends up doing another malloc big
219 	 * enough to store the data + its header
220 	 */
221 
222 	curentHeadRoom = qdf_nbuf_headroom(netbuf);
223 	qdf_nbuf_pull_head(netbuf,
224 			   pPacket->netbufOrigHeadRoom - curentHeadRoom);
225 	qdf_nbuf_trim_tail(netbuf, qdf_nbuf_len(netbuf));
226 
227 	/* restore the pBuffer pointer. HIF changes this */
228 	pPacket->pBuffer = qdf_nbuf_data(netbuf);
229 	pPacket->BufferLength = qdf_nbuf_len(netbuf);
230 
231 	/* restore queue */
232 	pQueueSave = (HTC_PACKET_QUEUE *) pPacket->pContext;
233 	if (qdf_unlikely(!pQueueSave)) {
234 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
235 				("\n%s: Invalid pQueueSave in HTC Packet\n",
236 				__func__));
237 		AR_DEBUG_ASSERT(pQueueSave);
238 	} else
239 		INIT_HTC_PACKET_QUEUE(pQueueSave);
240 
241 	LOCK_HTC_TX(target);
242 	if (target->pBundleFreeList == NULL) {
243 		target->pBundleFreeList = pPacket;
244 		pPacket->ListLink.pNext = NULL;
245 	} else {
246 		pPacket->ListLink.pNext = (DL_LIST *) target->pBundleFreeList;
247 		target->pBundleFreeList = pPacket;
248 	}
249 	UNLOCK_HTC_TX(target);
250 }
251 
252 #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
253 
254 /**
255  * htc_send_update_tx_bundle_stats() - update tx bundle stats depends
256  *				on max bundle size
257  * @target: hif context
258  * @data_len: tx data len
259  * @TxCreditSize: endpoint tx credit size
260  *
261  * Return: None
262  */
263 static inline void
264 htc_send_update_tx_bundle_stats(HTC_TARGET *target,
265 				qdf_size_t data_len,
266 				int TxCreditSize)
267 {
268 	if ((data_len / TxCreditSize) <= HTC_MAX_MSG_PER_BUNDLE_TX)
269 		target->tx_bundle_stats[(data_len / TxCreditSize) - 1]++;
270 }
271 
272 /**
273  * htc_issue_tx_bundle_stats_inc() - increment in tx bundle stats
274  *				on max bundle size
275  * @target: hif context
276  *
277  * Return: None
278  */
279 static inline void
280 htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
281 {
282 	target->tx_bundle_stats[0]++;
283 }
284 #else
285 
286 static inline void
287 htc_send_update_tx_bundle_stats(HTC_TARGET *target,
288 				qdf_size_t data_len,
289 				int TxCreditSize)
290 {
291 }
292 
293 static inline void
294 htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
295 {
296 }
297 #endif
298 
299 #if defined(HIF_USB) || defined(HIF_SDIO)
300 #ifdef ENABLE_BUNDLE_TX
301 static QDF_STATUS htc_send_bundled_netbuf(HTC_TARGET *target,
302 					HTC_ENDPOINT *pEndpoint,
303 					unsigned char *pBundleBuffer,
304 					HTC_PACKET *pPacketTx)
305 {
306 	qdf_size_t data_len;
307 	QDF_STATUS status;
308 	qdf_nbuf_t bundleBuf;
309 	uint32_t data_attr = 0;
310 
311 	bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
312 	data_len = pBundleBuffer - qdf_nbuf_data(bundleBuf);
313 	qdf_nbuf_put_tail(bundleBuf, data_len);
314 	SET_HTC_PACKET_INFO_TX(pPacketTx,
315 			       target,
316 			       pBundleBuffer,
317 			       data_len,
318 			       pEndpoint->Id, HTC_TX_PACKET_TAG_BUNDLED);
319 	LOCK_HTC_TX(target);
320 	HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacketTx);
321 	pEndpoint->ul_outstanding_cnt++;
322 	UNLOCK_HTC_TX(target);
323 #if DEBUG_BUNDLE
324 	qdf_print(" Send bundle EP%d buffer size:0x%x, total:0x%x, count:%d.",
325 		  pEndpoint->Id,
326 		  pEndpoint->TxCreditSize,
327 		  data_len, data_len / pEndpoint->TxCreditSize);
328 #endif
329 
330 	htc_send_update_tx_bundle_stats(target, data_len,
331 					pEndpoint->TxCreditSize);
332 
333 	status = hif_send_head(target->hif_dev,
334 			       pEndpoint->UL_PipeID,
335 			       pEndpoint->Id, data_len,
336 			       bundleBuf, data_attr);
337 	if (status != QDF_STATUS_SUCCESS) {
338 		qdf_print("%s:hif_send_head failed(len=%zu).", __func__,
339 			  data_len);
340 	}
341 	return status;
342 }
343 
344 /**
345  * htc_issue_packets_bundle() - HTC function to send bundle packets from a queue
346  * @target: HTC target on which packets need to be sent
347  * @pEndpoint: logical endpoint on which packets needs to be sent
348  * @pPktQueue: HTC packet queue containing the list of packets to be sent
349  *
350  * Return: void
351  */
352 static void htc_issue_packets_bundle(HTC_TARGET *target,
353 				     HTC_ENDPOINT *pEndpoint,
354 				     HTC_PACKET_QUEUE *pPktQueue)
355 {
356 	int i, frag_count, nbytes;
357 	qdf_nbuf_t netbuf, bundleBuf;
358 	unsigned char *pBundleBuffer = NULL;
359 	HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
360 	HTC_FRAME_HDR *pHtcHdr;
361 	int last_credit_pad = 0;
362 	int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
363 		0;
364 	HTC_PACKET_QUEUE *pQueueSave = NULL;
365 
366 	bundlesSpaceRemaining =
367 		target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
368 	pPacketTx = allocate_htc_bundle_packet(target);
369 	if (!pPacketTx) {
370 		/* good time to panic */
371 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
372 				("allocate_htc_bundle_packet failed\n"));
373 		AR_DEBUG_ASSERT(false);
374 		return;
375 	}
376 	bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
377 	pBundleBuffer = qdf_nbuf_data(bundleBuf);
378 	pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
379 	while (1) {
380 		pPacket = htc_packet_dequeue(pPktQueue);
381 		if (pPacket == NULL)
382 			break;
383 		creditPad = 0;
384 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
385 		creditRemainder = transferLength % pEndpoint->TxCreditSize;
386 		if (creditRemainder != 0) {
387 			if (transferLength < pEndpoint->TxCreditSize) {
388 				creditPad = pEndpoint->TxCreditSize -
389 					    transferLength;
390 			} else {
391 				creditPad = creditRemainder;
392 			}
393 			transferLength += creditPad;
394 		}
395 
396 		if (bundlesSpaceRemaining < transferLength) {
397 			/* send out previous buffer */
398 			htc_send_bundled_netbuf(target, pEndpoint,
399 						pBundleBuffer - last_credit_pad,
400 						pPacketTx);
401 			/* One packet has been dequeued from sending queue when enter
402 			 * this loop, so need to add 1 back for this checking.
403 			 */
404 			if ((HTC_PACKET_QUEUE_DEPTH(pPktQueue) + 1) <
405 			    HTC_MIN_MSG_PER_BUNDLE) {
406 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
407 				return;
408 			}
409 			bundlesSpaceRemaining =
410 				target->MaxMsgsPerHTCBundle *
411 				pEndpoint->TxCreditSize;
412 			pPacketTx = allocate_htc_bundle_packet(target);
413 			if (!pPacketTx) {
414 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
415 				/* good time to panic */
416 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
417 						("allocate_htc_bundle_packet failed\n"));
418 				AR_DEBUG_ASSERT(false);
419 				return;
420 			}
421 			bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
422 			pBundleBuffer = qdf_nbuf_data(bundleBuf);
423 			pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
424 		}
425 
426 		bundlesSpaceRemaining -= transferLength;
427 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
428 
429 		if (hif_get_bus_type(target->hif_dev) != QDF_BUS_TYPE_USB) {
430 			pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(
431 								netbuf, 0);
432 			HTC_WRITE32(pHtcHdr,
433 				SM(pPacket->ActualLength,
434 				HTC_FRAME_HDR_PAYLOADLEN) |
435 				SM(pPacket->PktInfo.AsTx.SendFlags |
436 				HTC_FLAGS_SEND_BUNDLE,
437 				HTC_FRAME_HDR_FLAGS) |
438 				SM(pPacket->Endpoint,
439 				HTC_FRAME_HDR_ENDPOINTID));
440 			HTC_WRITE32((uint32_t *) pHtcHdr + 1,
441 				SM(pPacket->PktInfo.AsTx.SeqNo,
442 				HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
443 				HTC_FRAME_HDR_RESERVED));
444 			pHtcHdr->reserved = creditPad;
445 		}
446 		frag_count = qdf_nbuf_get_num_frags(netbuf);
447 		nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
448 		for (i = 0; i < frag_count && nbytes > 0; i++) {
449 			int frag_len = qdf_nbuf_get_frag_len(netbuf, i);
450 			unsigned char *frag_addr =
451 				qdf_nbuf_get_frag_vaddr(netbuf, i);
452 			if (frag_len > nbytes)
453 				frag_len = nbytes;
454 			qdf_mem_copy(pBundleBuffer, frag_addr, frag_len);
455 			nbytes -= frag_len;
456 			pBundleBuffer += frag_len;
457 		}
458 		HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
459 		pBundleBuffer += creditPad;
460 
461 		/* last one can't be packed. */
462 		if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)
463 			last_credit_pad = creditPad;
464 	}
465 	/* send out remaining buffer */
466 	if (pBundleBuffer != qdf_nbuf_data(bundleBuf))
467 		htc_send_bundled_netbuf(target, pEndpoint,
468 					pBundleBuffer - last_credit_pad,
469 					pPacketTx);
470 	else
471 		free_htc_bundle_packet(target, pPacketTx);
472 }
473 #endif /* ENABLE_BUNDLE_TX */
474 #else
475 static void htc_issue_packets_bundle(HTC_TARGET *target,
476 				     HTC_ENDPOINT *pEndpoint,
477 				     HTC_PACKET_QUEUE *pPktQueue)
478 {
479 }
480 #endif
481 
482 /**
483  * htc_issue_packets() - HTC function to send packets from a queue
484  * @target: HTC target on which packets need to be sent
485  * @pEndpoint: logical endpoint on which packets needs to be sent
486  * @pPktQueue: HTC packet queue containing the list of packets to be sent
487  *
488  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
489  */
490 static QDF_STATUS htc_issue_packets(HTC_TARGET *target,
491 				  HTC_ENDPOINT *pEndpoint,
492 				  HTC_PACKET_QUEUE *pPktQueue)
493 {
494 	QDF_STATUS status = QDF_STATUS_SUCCESS;
495 	qdf_nbuf_t netbuf;
496 	HTC_PACKET *pPacket = NULL;
497 	uint16_t payloadLen;
498 	HTC_FRAME_HDR *pHtcHdr;
499 	uint32_t data_attr = 0;
500 	enum qdf_bus_type bus_type;
501 	QDF_STATUS ret;
502 	bool rt_put = false;
503 
504 	bus_type = hif_get_bus_type(target->hif_dev);
505 
506 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
507 			("+htc_issue_packets: Queue: %pK, Pkts %d\n", pPktQueue,
508 			 HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
509 	while (true) {
510 		if (HTC_TX_BUNDLE_ENABLED(target) &&
511 		    HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
512 		    HTC_MIN_MSG_PER_BUNDLE) {
513 			switch (bus_type) {
514 			case QDF_BUS_TYPE_SDIO:
515 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
516 					break;
517 			case QDF_BUS_TYPE_USB:
518 				htc_issue_packets_bundle(target,
519 							pEndpoint,
520 							pPktQueue);
521 				break;
522 			default:
523 				break;
524 			}
525 		}
526 		/* if not bundling or there was a packet that could not be
527 		 * placed in a bundle, and send it by normal way
528 		 */
529 		pPacket = htc_packet_dequeue(pPktQueue);
530 		if (NULL == pPacket) {
531 			/* local queue is fully drained */
532 			break;
533 		}
534 
535 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
536 		AR_DEBUG_ASSERT(netbuf);
537 		/* Non-credit enabled endpoints have been mapped and setup by
538 		 * now, so no need to revisit the HTC headers
539 		 */
540 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
541 
542 			payloadLen = pPacket->ActualLength;
543 			/* setup HTC frame header */
544 
545 			pHtcHdr = (HTC_FRAME_HDR *)
546 				qdf_nbuf_get_frag_vaddr(netbuf, 0);
547 			if (qdf_unlikely(!pHtcHdr)) {
548 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
549 						("%s Invalid pHtcHdr\n",
550 						 __func__));
551 				AR_DEBUG_ASSERT(pHtcHdr);
552 				status = QDF_STATUS_E_FAILURE;
553 				break;
554 			}
555 
556 			HTC_WRITE32(pHtcHdr,
557 					SM(payloadLen,
558 						HTC_FRAME_HDR_PAYLOADLEN) |
559 					SM(pPacket->PktInfo.AsTx.SendFlags,
560 						HTC_FRAME_HDR_FLAGS) |
561 					SM(pPacket->Endpoint,
562 						HTC_FRAME_HDR_ENDPOINTID));
563 			HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
564 				    SM(pPacket->PktInfo.AsTx.SeqNo,
565 				       HTC_FRAME_HDR_CONTROLBYTES1));
566 
567 			/*
568 			 * Now that the HTC frame header has been added, the
569 			 * netbuf can be mapped.  This only applies to non-data
570 			 * frames, since data frames were already mapped as they
571 			 * entered into the driver.
572 			 */
573 			pPacket->PktInfo.AsTx.Flags |=
574 				HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
575 
576 			ret = qdf_nbuf_map(target->osdev,
577 				GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
578 				QDF_DMA_TO_DEVICE);
579 			if (ret != QDF_STATUS_SUCCESS) {
580 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
581 					("%s nbuf Map Fail Endpnt %pK\n",
582 					__func__, pEndpoint));
583 				HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
584 				status = QDF_STATUS_E_FAILURE;
585 				break;
586 			}
587 		}
588 
589 		if (!pEndpoint->async_update) {
590 			LOCK_HTC_TX(target);
591 		}
592 		/* store in look up queue to match completions */
593 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
594 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
595 		pEndpoint->ul_outstanding_cnt++;
596 		if (!pEndpoint->async_update) {
597 			UNLOCK_HTC_TX(target);
598 			hif_send_complete_check(target->hif_dev,
599 					pEndpoint->UL_PipeID, false);
600 		}
601 
602 		htc_packet_set_magic_cookie(pPacket, HTC_PACKET_MAGIC_COOKIE);
603 		/*
604 		 * For HTT messages without a response from fw,
605 		 *   do the runtime put here.
606 		 * otherwise runtime put will be done when the fw response comes
607 		 */
608 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
609 			rt_put = true;
610 #if DEBUG_BUNDLE
611 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
612 			  pEndpoint->Id,
613 			  pEndpoint->TxCreditSize,
614 			  HTC_HDR_LENGTH + pPacket->ActualLength);
615 #endif
616 		status = hif_send_head(target->hif_dev,
617 				       pEndpoint->UL_PipeID, pEndpoint->Id,
618 				       HTC_HDR_LENGTH + pPacket->ActualLength,
619 				       netbuf, data_attr);
620 
621 		htc_issue_tx_bundle_stats_inc(target);
622 
623 		target->ce_send_cnt++;
624 
625 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
626 			if (status != QDF_STATUS_E_RESOURCES) {
627 				/* TODO : if more than 1 endpoint maps to the
628 				 * same PipeID it is possible to run out of
629 				 * resources in the HIF layer. Don't emit the
630 				 * error
631 				 */
632 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
633 						("hif_send Failed status:%d\n",
634 						 status));
635 			}
636 
637 			/* only unmap if we mapped in this function */
638 			if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
639 				qdf_nbuf_unmap(target->osdev,
640 					GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
641 					QDF_DMA_TO_DEVICE);
642 
643 			if (!pEndpoint->async_update) {
644 				LOCK_HTC_TX(target);
645 			}
646 			target->ce_send_cnt--;
647 			pEndpoint->ul_outstanding_cnt--;
648 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
649 			htc_packet_set_magic_cookie(pPacket, 0);
650 			/* put it back into the callers queue */
651 			HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
652 			/* reclaim credits */
653 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue,
654 							      pPacket) {
655 			    pEndpoint->TxCredits +=
656 				pPacket->PktInfo.AsTx.CreditsUsed;
657 			} HTC_PACKET_QUEUE_ITERATE_END;
658 			if (!pEndpoint->async_update) {
659 				UNLOCK_HTC_TX(target);
660 			}
661 			break;
662 		}
663 		if (rt_put) {
664 			hif_pm_runtime_put(target->hif_dev);
665 			rt_put = false;
666 		}
667 	}
668 	if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
669 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
670 			("htc_issue_packets, failed pkt:0x%pK status:%d",
671 			 pPacket, status));
672 	}
673 
674 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets\n"));
675 
676 	return status;
677 }
678 
679 #ifdef FEATURE_RUNTIME_PM
680 /**
681  * extract_htc_pm_packets(): move pm packets from endpoint into queue
682  * @endpoint: which enpoint to extract packets from
683  * @queue: a queue to store extracted packets in.
684  *
685  * remove pm packets from the endpoint's tx queue.
686  * queue them into a queue
687  */
688 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
689 				HTC_PACKET_QUEUE *queue)
690 {
691 	HTC_PACKET *packet;
692 
693 	/* only WMI endpoint has power management packets */
694 	if (endpoint->service_id != WMI_CONTROL_SVC)
695 		return;
696 
697 	ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
698 			HTC_PACKET, ListLink) {
699 		if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
700 			HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
701 			HTC_PACKET_ENQUEUE(queue, packet);
702 		}
703 	} ITERATE_END
704 }
705 
706 /**
707  * queue_htc_pm_packets(): queue pm packets with priority
708  * @endpoint: enpoint to queue packets to
709  * @queue: queue of pm packets to enque
710  *
711  * suspend resume packets get special treatment & priority.
712  * need to queue them at the front of the queue.
713  */
714 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
715 				 HTC_PACKET_QUEUE *queue)
716 {
717 	if (endpoint->service_id != WMI_CONTROL_SVC)
718 		return;
719 
720 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
721 }
722 #else
723 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
724 		HTC_PACKET_QUEUE *queue)
725 {}
726 
727 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
728 		HTC_PACKET_QUEUE *queue)
729 {}
730 #endif
731 
732 /**
733  * get_htc_send_packets_credit_based() - get packets based on available credits
734  * @target: HTC target on which packets need to be sent
735  * @pEndpoint: logical endpoint on which packets needs to be sent
736  * @pQueue: HTC packet queue containing the list of packets to be sent
737  *
738  * Get HTC send packets from TX queue on an endpoint based on available credits.
739  * The function moves the packets from TX queue of the endpoint to pQueue.
740  *
741  * Return: None
742  */
743 static void get_htc_send_packets_credit_based(HTC_TARGET *target,
744 					      HTC_ENDPOINT *pEndpoint,
745 					      HTC_PACKET_QUEUE *pQueue)
746 {
747 	int creditsRequired;
748 	int remainder;
749 	uint8_t sendFlags;
750 	HTC_PACKET *pPacket;
751 	unsigned int transferLength;
752 	HTC_PACKET_QUEUE *tx_queue;
753 	HTC_PACKET_QUEUE pm_queue;
754 	bool do_pm_get = false;
755 
756 	/*** NOTE : the TX lock is held when this function is called ***/
757 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
758 			("+get_htc_send_packets_credit_based\n"));
759 
760 	INIT_HTC_PACKET_QUEUE(&pm_queue);
761 	extract_htc_pm_packets(pEndpoint, &pm_queue);
762 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
763 		tx_queue = &pEndpoint->TxQueue;
764 		do_pm_get = true;
765 	} else {
766 		tx_queue = &pm_queue;
767 	}
768 
769 	/* loop until we can grab as many packets out of the queue as we can */
770 	while (true) {
771 		if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
772 			/* bus suspended, runtime resume issued */
773 			QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
774 			break;
775 		}
776 
777 		sendFlags = 0;
778 		/* get packet at head, but don't remove it */
779 		pPacket = htc_get_pkt_at_head(tx_queue);
780 		if (pPacket == NULL) {
781 			if (do_pm_get)
782 				hif_pm_runtime_put(target->hif_dev);
783 			break;
784 		}
785 
786 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
787 				(" Got head packet:%pK , Queue Depth: %d\n",
788 				 pPacket,
789 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
790 
791 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
792 
793 		if (transferLength <= pEndpoint->TxCreditSize) {
794 			creditsRequired = 1;
795 		} else {
796 			/* figure out how many credits this message requires */
797 			creditsRequired =
798 				transferLength / pEndpoint->TxCreditSize;
799 			remainder = transferLength % pEndpoint->TxCreditSize;
800 
801 			if (remainder)
802 				creditsRequired++;
803 		}
804 
805 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
806 				(" Credits Required:%d   Got:%d\n",
807 				 creditsRequired, pEndpoint->TxCredits));
808 
809 		if (pEndpoint->Id == ENDPOINT_0) {
810 			/*
811 			 * endpoint 0 is special, it always has a credit and
812 			 * does not require credit based flow control
813 			 */
814 			creditsRequired = 0;
815 		} else {
816 
817 			if (pEndpoint->TxCredits < creditsRequired) {
818 #if DEBUG_CREDIT
819 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
820 						("EP%d,No Credit now.%d < %d\n",
821 						 pEndpoint->Id,
822 						 pEndpoint->TxCredits,
823 						 creditsRequired));
824 #endif
825 				if (do_pm_get)
826 					hif_pm_runtime_put(target->hif_dev);
827 				break;
828 			}
829 
830 			pEndpoint->TxCredits -= creditsRequired;
831 			INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
832 					creditsRequired);
833 
834 			/* check if we need credits back from the target */
835 			if (pEndpoint->TxCredits <=
836 			    pEndpoint->TxCreditsPerMaxMsg) {
837 				/* tell the target we need credits ASAP! */
838 				sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
839 				if (pEndpoint->service_id == WMI_CONTROL_SVC) {
840 					htc_credit_record(HTC_REQUEST_CREDIT,
841 							  pEndpoint->TxCredits,
842 							  HTC_PACKET_QUEUE_DEPTH
843 							  (tx_queue));
844 				}
845 				INC_HTC_EP_STAT(pEndpoint,
846 						TxCreditLowIndications, 1);
847 #if DEBUG_CREDIT
848 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
849 						(" EP%d Needs Credits\n",
850 						 pEndpoint->Id));
851 #endif
852 			}
853 		}
854 
855 		/* now we can fully dequeue */
856 		pPacket = htc_packet_dequeue(tx_queue);
857 		if (pPacket) {
858 			/* save the number of credits this packet consumed */
859 			pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
860 			/* save send flags */
861 			pPacket->PktInfo.AsTx.SendFlags = sendFlags;
862 
863 			/* queue this packet into the caller's queue */
864 			HTC_PACKET_ENQUEUE(pQueue, pPacket);
865 		}
866 	}
867 
868 	if (!HTC_QUEUE_EMPTY(&pm_queue))
869 		queue_htc_pm_packets(pEndpoint, &pm_queue);
870 
871 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
872 			("-get_htc_send_packets_credit_based\n"));
873 
874 }
875 
876 static void get_htc_send_packets(HTC_TARGET *target,
877 				 HTC_ENDPOINT *pEndpoint,
878 				 HTC_PACKET_QUEUE *pQueue, int Resources)
879 {
880 
881 	HTC_PACKET *pPacket;
882 	HTC_PACKET_QUEUE *tx_queue;
883 	HTC_PACKET_QUEUE pm_queue;
884 	bool do_pm_get = false;
885 
886 	/*** NOTE : the TX lock is held when this function is called ***/
887 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
888 			("+get_htc_send_packets %d resources\n", Resources));
889 
890 	INIT_HTC_PACKET_QUEUE(&pm_queue);
891 	extract_htc_pm_packets(pEndpoint, &pm_queue);
892 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
893 		tx_queue = &pEndpoint->TxQueue;
894 		do_pm_get = true;
895 	} else {
896 		tx_queue = &pm_queue;
897 	}
898 
899 	/* loop until we can grab as many packets out of the queue as we can */
900 	while (Resources > 0) {
901 		int num_frags;
902 
903 		if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
904 			/* bus suspended, runtime resume issued */
905 			QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
906 			break;
907 		}
908 
909 		pPacket = htc_packet_dequeue(tx_queue);
910 		if (pPacket == NULL) {
911 			if (do_pm_get)
912 				hif_pm_runtime_put(target->hif_dev);
913 			break;
914 		}
915 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
916 				(" Got packet:%pK , New Queue Depth: %d\n",
917 				 pPacket,
918 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
919 		/* For non-credit path the sequence number is already embedded
920 		 * in the constructed HTC header
921 		 */
922 		pPacket->PktInfo.AsTx.SendFlags = 0;
923 		pPacket->PktInfo.AsTx.CreditsUsed = 0;
924 		/* queue this packet into the caller's queue */
925 		HTC_PACKET_ENQUEUE(pQueue, pPacket);
926 
927 		/*
928 		 * FIX THIS:
929 		 * For now, avoid calling qdf_nbuf_get_num_frags before calling
930 		 * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
931 		 * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
932 		 * been done.
933 		 * Assume that the non-data netbufs, i.e. WMI message netbufs,
934 		 * consist of a single fragment.
935 		 */
936 		/* WMI messages are in a single-fragment network buf */
937 		num_frags =
938 			(pPacket->PktInfo.AsTx.
939 			 Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
940 			qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
941 						       (pPacket));
942 		Resources -= num_frags;
943 	}
944 
945 	if (!HTC_QUEUE_EMPTY(&pm_queue))
946 		queue_htc_pm_packets(pEndpoint, &pm_queue);
947 
948 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
949 
950 }
951 
952 /**
953  * htc_try_send() - Send packets in a queue on an endpoint
954  * @target: HTC target on which packets need to be sent
955  * @pEndpoint: logical endpoint on which packets needs to be sent
956  * @pCallersSendQueue: packet queue containing the list of packets to be sent
957  *
958  * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
959  *         be sent or the packet should be dropped by the upper layer
960  */
961 static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
962 					  HTC_ENDPOINT *pEndpoint,
963 					  HTC_PACKET_QUEUE *pCallersSendQueue)
964 {
965 	/* temp queue to hold packets at various stages */
966 	HTC_PACKET_QUEUE sendQueue;
967 	HTC_PACKET *pPacket;
968 	int tx_resources;
969 	int overflow;
970 	enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
971 
972 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
973 					 pCallersSendQueue,
974 					 (pCallersSendQueue ==
975 					  NULL) ? 0 :
976 					 HTC_PACKET_QUEUE_DEPTH
977 						 (pCallersSendQueue)));
978 
979 	/* init the local send queue */
980 	INIT_HTC_PACKET_QUEUE(&sendQueue);
981 
982 	do {
983 
984 		/* caller didn't provide a queue, just wants us to check
985 		 * queues and send
986 		 */
987 		if (pCallersSendQueue == NULL)
988 			break;
989 
990 		if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
991 			/* empty queue */
992 			OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
993 							HTC_PKT_Q_EMPTY);
994 			result = HTC_SEND_QUEUE_DROP;
995 			break;
996 		}
997 
998 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
999 		    pEndpoint->MaxTxQueueDepth) {
1000 			/* we've already overflowed */
1001 			overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1002 		} else {
1003 			/* figure out how much we will overflow by */
1004 			overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1005 			overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1006 			/* get how much we will overflow the TX queue by */
1007 			overflow -= pEndpoint->MaxTxQueueDepth;
1008 		}
1009 
1010 		/* if overflow is negative or zero, we are okay */
1011 		if (overflow > 0) {
1012 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1013 					("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
1014 					 pEndpoint->Id, overflow,
1015 					 HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
1016 								TxQueue),
1017 					 pEndpoint->MaxTxQueueDepth));
1018 		}
1019 		if ((overflow <= 0)
1020 		    || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
1021 			/* all packets will fit or caller did not provide send
1022 			 * full indication handler
1023 			 * just move all of them to local sendQueue object
1024 			 */
1025 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
1026 							  pCallersSendQueue);
1027 		} else {
1028 			int i;
1029 			int goodPkts =
1030 				HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
1031 				overflow;
1032 
1033 			A_ASSERT(goodPkts >= 0);
1034 			/* we have overflowed and callback is provided. Dequeue
1035 			 * all non-overflow packets into the sendqueue
1036 			 */
1037 			for (i = 0; i < goodPkts; i++) {
1038 				/* pop off caller's queue */
1039 				pPacket = htc_packet_dequeue(pCallersSendQueue);
1040 				A_ASSERT(pPacket != NULL);
1041 				/* insert into local queue */
1042 				HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1043 			}
1044 
1045 			/* the caller's queue has all the packets that won't fit
1046 			 * walk through the caller's queue and indicate each one
1047 			 * to the send full handler
1048 			 */
1049 			ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
1050 						       QueueHead, pPacket,
1051 						       HTC_PACKET, ListLink) {
1052 
1053 				AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1054 						("Indicating overflowed TX packet: %pK\n",
1055 						 pPacket));
1056 				/*
1057 				 * Remove headroom reserved for HTC_FRAME_HDR
1058 				 * before giving the packet back to the user via
1059 				 * the EpSendFull callback.
1060 				 */
1061 				restore_tx_packet(target, pPacket);
1062 
1063 				if (pEndpoint->EpCallBacks.
1064 				    EpSendFull(pEndpoint->EpCallBacks.pContext,
1065 					       pPacket) == HTC_SEND_FULL_DROP) {
1066 					/* callback wants the packet dropped */
1067 					INC_HTC_EP_STAT(pEndpoint, TxDropped,
1068 							1);
1069 					/* leave this one in the caller's queue
1070 					 * for cleanup
1071 					 */
1072 				} else {
1073 					/* callback wants to keep this packet,
1074 					 * remove from caller's queue
1075 					 */
1076 					HTC_PACKET_REMOVE(pCallersSendQueue,
1077 							  pPacket);
1078 					/* put it in the send queue
1079 					 * add HTC_FRAME_HDR space reservation
1080 					 * again
1081 					 */
1082 					qdf_nbuf_push_head
1083 						(GET_HTC_PACKET_NET_BUF_CONTEXT
1084 							(pPacket),
1085 						sizeof(HTC_FRAME_HDR));
1086 
1087 					HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1088 				}
1089 
1090 			}
1091 			ITERATE_END;
1092 
1093 			if (HTC_QUEUE_EMPTY(&sendQueue)) {
1094 				/* no packets made it in, caller will cleanup */
1095 				OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1096 							HTC_SEND_Q_EMPTY);
1097 				result = HTC_SEND_QUEUE_DROP;
1098 				break;
1099 			}
1100 		}
1101 
1102 	} while (false);
1103 
1104 	if (result != HTC_SEND_QUEUE_OK) {
1105 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
1106 			result));
1107 		return result;
1108 	}
1109 
1110 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1111 		tx_resources =
1112 			hif_get_free_queue_number(target->hif_dev,
1113 						  pEndpoint->UL_PipeID);
1114 	} else {
1115 		tx_resources = 0;
1116 	}
1117 
1118 	LOCK_HTC_TX(target);
1119 
1120 	if (!HTC_QUEUE_EMPTY(&sendQueue)) {
1121 		if (target->is_nodrop_pkt) {
1122 			/*
1123 			 * nodrop pkts have higher priority than normal pkts,
1124 			 * insert nodrop pkt to head for proper
1125 			 * start/termination of test.
1126 			 */
1127 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1128 					&sendQueue);
1129 			target->is_nodrop_pkt = false;
1130 		} else {
1131 			/* transfer packets to tail */
1132 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
1133 					&sendQueue);
1134 			A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
1135 			INIT_HTC_PACKET_QUEUE(&sendQueue);
1136 		}
1137 	}
1138 
1139 	/* increment tx processing count on entry */
1140 	if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
1141 		/* another thread or task is draining the TX queues on this
1142 		 * endpoint that thread will reset the tx processing count when
1143 		 * the queue is drained
1144 		 */
1145 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1146 		UNLOCK_HTC_TX(target);
1147 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
1148 		return HTC_SEND_QUEUE_OK;
1149 	}
1150 
1151 	/***** beyond this point only 1 thread may enter ******/
1152 
1153 	/* now drain the endpoint TX queue for transmission as long as we have
1154 	 * enough transmit resources
1155 	 */
1156 	while (true) {
1157 
1158 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
1159 			break;
1160 
1161 		if (pEndpoint->async_update &&
1162 			(!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
1163 			(!tx_resources))
1164 			break;
1165 
1166 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1167 #if DEBUG_CREDIT
1168 			int cred = pEndpoint->TxCredits;
1169 #endif
1170 			/* credit based mechanism provides flow control based on
1171 			 * target transmit resource availability, we assume that
1172 			 * the HIF layer will always have bus resources greater
1173 			 * than target transmit resources
1174 			 */
1175 			get_htc_send_packets_credit_based(target, pEndpoint,
1176 							  &sendQueue);
1177 #if DEBUG_CREDIT
1178 			if (ep_debug_mask & (1 << pEndpoint->Id)) {
1179 				if (cred - pEndpoint->TxCredits > 0) {
1180 					AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1181 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1182 							 pEndpoint->Id, cred,
1183 							 cred -
1184 							 pEndpoint->TxCredits,
1185 							 pEndpoint->TxCredits));
1186 				}
1187 			}
1188 #endif
1189 		} else {
1190 
1191 		/*
1192 		* Header and payload belongs to the different fragments and
1193 		* consume 2 resource for one HTC package but USB combine into
1194 		* one transfer.And one WMI message only consumes one single
1195 		* resource.
1196 		*/
1197 			if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
1198 				hif_get_bus_type(target->hif_dev) ==
1199 						QDF_BUS_TYPE_USB) {
1200 				if (pEndpoint->service_id ==
1201 					WMI_CONTROL_SVC)
1202 					tx_resources =
1203 					    HTC_MAX_MSG_PER_BUNDLE_TX;
1204 				else
1205 					tx_resources =
1206 					    (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
1207 			}
1208 			/* get all the packets for this endpoint that we can for
1209 			 * this pass
1210 			 */
1211 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1212 					     tx_resources);
1213 		}
1214 
1215 		if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
1216 			/* didn't get any packets due to a lack of resources or
1217 			 * TX queue was drained
1218 			 */
1219 			break;
1220 		}
1221 
1222 		if (!pEndpoint->async_update)
1223 			UNLOCK_HTC_TX(target);
1224 
1225 		/* send what we can */
1226 		if (htc_issue_packets(target, pEndpoint, &sendQueue)) {
1227 			int i;
1228 
1229 			result = HTC_SEND_QUEUE_DROP;
1230 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1231 				("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
1232 				 result));
1233 
1234 			for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
1235 				hif_pm_runtime_put(target->hif_dev);
1236 			if (!pEndpoint->async_update) {
1237 				LOCK_HTC_TX(target);
1238 			}
1239 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1240 							  &sendQueue);
1241 			break;
1242 		}
1243 
1244 		if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1245 			tx_resources =
1246 				hif_get_free_queue_number(target->hif_dev,
1247 							  pEndpoint->UL_PipeID);
1248 		}
1249 
1250 		if (!pEndpoint->async_update) {
1251 			LOCK_HTC_TX(target);
1252 		}
1253 
1254 	}
1255 
1256 	/* done with this endpoint, we can clear the count */
1257 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1258 
1259 	UNLOCK_HTC_TX(target);
1260 
1261 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
1262 
1263 	return HTC_SEND_QUEUE_OK;
1264 }
1265 
1266 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1267 static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
1268 					  HTC_ENDPOINT_ID id)
1269 {
1270 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1271 	HTC_ENDPOINT *pEndpoint;
1272 	HTC_ENDPOINT_ID eid;
1273 	HTC_PACKET_QUEUE *pTxQueue;
1274 	uint16_t resources;
1275 	uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
1276 
1277 	if (id < ENDPOINT_2 || id > ENDPOINT_5)
1278 		return 1;
1279 
1280 	for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
1281 		pEndpoint = &target->endpoint[eid];
1282 		pTxQueue = &pEndpoint->TxQueue;
1283 
1284 		if (HTC_QUEUE_EMPTY(pTxQueue))
1285 			acQueueStatus[eid - 2] = 1;
1286 	}
1287 
1288 	switch (id) {
1289 	case ENDPOINT_2:        /* BE */
1290 		return acQueueStatus[0] && acQueueStatus[2]
1291 			&& acQueueStatus[3];
1292 	case ENDPOINT_3:        /* BK */
1293 		return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
1294 			&& acQueueStatus[3];
1295 	case ENDPOINT_4:        /* VI */
1296 		return acQueueStatus[2] && acQueueStatus[3];
1297 	case ENDPOINT_5:        /* VO */
1298 		return acQueueStatus[3];
1299 	default:
1300 		return 0;
1301 	}
1302 
1303 }
1304 
1305 static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
1306 					  HTC_PACKET_QUEUE *pPktQueue,
1307 					  HTC_ENDPOINT_ID eid)
1308 {
1309 	HTC_ENDPOINT *pEndpoint;
1310 	HTC_PACKET_QUEUE *pTxQueue;
1311 	HTC_PACKET *pPacket;
1312 	int goodPkts;
1313 
1314 	pEndpoint = &target->endpoint[eid];
1315 	pTxQueue = &pEndpoint->TxQueue;
1316 
1317 	LOCK_HTC_TX(target);
1318 
1319 	goodPkts =
1320 		pEndpoint->MaxTxQueueDepth -
1321 		HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1322 
1323 	if (goodPkts > 0) {
1324 		while (!HTC_QUEUE_EMPTY(pPktQueue)) {
1325 			pPacket = htc_packet_dequeue(pPktQueue);
1326 			HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1327 			goodPkts--;
1328 
1329 			if (goodPkts <= 0)
1330 				break;
1331 		}
1332 	}
1333 
1334 	if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
1335 		ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
1336 					       HTC_PACKET, ListLink) {
1337 
1338 			if (pEndpoint->EpCallBacks.
1339 			    EpSendFull(pEndpoint->EpCallBacks.pContext,
1340 				       pPacket) == HTC_SEND_FULL_DROP) {
1341 				INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
1342 			} else {
1343 				HTC_PACKET_REMOVE(pPktQueue, pPacket);
1344 				HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1345 			}
1346 		}
1347 		ITERATE_END;
1348 	}
1349 
1350 	UNLOCK_HTC_TX(target);
1351 
1352 	return A_OK;
1353 }
1354 
1355 #endif
1356 
1357 static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
1358 				HTC_PACKET *pPacket)
1359 {
1360 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1361 	HTC_ENDPOINT *pEndpoint;
1362 	HTC_PACKET_QUEUE pPktQueue;
1363 	qdf_nbuf_t netbuf;
1364 	HTC_FRAME_HDR *htc_hdr;
1365 	QDF_STATUS status;
1366 
1367 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1368 			("+__htc_send_pkt\n"));
1369 
1370 	/* get packet at head to figure out which endpoint these packets will
1371 	 * go into
1372 	 */
1373 	if (NULL == pPacket) {
1374 		OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
1375 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1376 		return QDF_STATUS_E_INVAL;
1377 	}
1378 
1379 	if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1380 	    (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1381 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1382 			("%s endpoint is invalid\n", __func__));
1383 		AR_DEBUG_ASSERT(0);
1384 		return QDF_STATUS_E_INVAL;
1385 	}
1386 	pEndpoint = &target->endpoint[pPacket->Endpoint];
1387 
1388 	if (!pEndpoint->service_id) {
1389 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
1390 								__func__));
1391 		return QDF_STATUS_E_INVAL;
1392 	}
1393 
1394 #ifdef HTC_EP_STAT_PROFILING
1395 	LOCK_HTC_TX(target);
1396 	INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
1397 	UNLOCK_HTC_TX(target);
1398 #endif
1399 
1400 	/* provide room in each packet's netbuf for the HTC frame header */
1401 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1402 	AR_DEBUG_ASSERT(netbuf);
1403 	if (!netbuf)
1404 		return QDF_STATUS_E_INVAL;
1405 
1406 	qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
1407 	/* setup HTC frame header */
1408 	htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1409 	AR_DEBUG_ASSERT(htc_hdr);
1410 	if (!htc_hdr)
1411 		return QDF_STATUS_E_INVAL;
1412 
1413 	HTC_WRITE32(htc_hdr,
1414 		    SM(pPacket->ActualLength,
1415 		       HTC_FRAME_HDR_PAYLOADLEN) |
1416 		    SM(pPacket->Endpoint,
1417 		       HTC_FRAME_HDR_ENDPOINTID));
1418 	LOCK_HTC_TX(target);
1419 
1420 	pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1421 	pEndpoint->SeqNo++;
1422 
1423 	HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
1424 		    SM(pPacket->PktInfo.AsTx.SeqNo,
1425 		       HTC_FRAME_HDR_CONTROLBYTES1));
1426 
1427 	UNLOCK_HTC_TX(target);
1428 
1429 	/*
1430 	 * For flow control enabled endpoints mapping is done in
1431 	 * htc_issue_packets and for non flow control enabled endpoints
1432 	 * its done here.
1433 	 */
1434 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1435 		pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
1436 		status = qdf_nbuf_map(target->osdev,
1437 				      GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
1438 				      QDF_DMA_TO_DEVICE);
1439 		if (status != QDF_STATUS_SUCCESS) {
1440 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1441 					("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
1442 					 __func__, pEndpoint, pEndpoint->SeqNo));
1443 			return status;
1444 		}
1445 	}
1446 
1447 	INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
1448 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1449 	if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
1450 		htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
1451 	else
1452 		htc_try_send(target, pEndpoint, &pPktQueue);
1453 #else
1454 	htc_try_send(target, pEndpoint, &pPktQueue);
1455 #endif
1456 
1457 	/* do completion on any packets that couldn't get in */
1458 	while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
1459 		pPacket = htc_packet_dequeue(&pPktQueue);
1460 
1461 		if (HTC_STOPPING(target))
1462 			pPacket->Status = QDF_STATUS_E_CANCELED;
1463 		else
1464 			pPacket->Status = QDF_STATUS_E_RESOURCES;
1465 
1466 		send_packet_completion(target, pPacket);
1467 	}
1468 
1469 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1470 
1471 	return QDF_STATUS_SUCCESS;
1472 }
1473 
1474 /* HTC API - htc_send_pkt */
1475 QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
1476 {
1477 	if (!htc_handle) {
1478 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1479 				("%s: HTCHandle is NULL \n", __func__));
1480 		return QDF_STATUS_E_FAILURE;
1481 	}
1482 
1483 	if (!htc_packet) {
1484 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1485 				("%s: pPacket is NULL \n", __func__));
1486 		return QDF_STATUS_E_FAILURE;
1487 	}
1488 
1489 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1490 			("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
1491 			 htc_packet->Endpoint, htc_packet->pBuffer,
1492 			 htc_packet->ActualLength));
1493 	return __htc_send_pkt(htc_handle, htc_packet);
1494 }
1495 qdf_export_symbol(htc_send_pkt);
1496 
1497 #ifdef ATH_11AC_TXCOMPACT
1498 /**
1499  * htc_send_data_pkt() - send single data packet on an endpoint
1500  * @HTCHandle: pointer to HTC handle
1501  * @netbuf: network buffer containing the data to be sent
1502  * @ActualLength: length of data that needs to be transmitted
1503  *
1504  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1505  */
1506 QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
1507 			     int actual_length)
1508 {
1509 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
1510 	HTC_ENDPOINT *pEndpoint;
1511 	HTC_FRAME_HDR *p_htc_hdr;
1512 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1513 	int tx_resources;
1514 	uint32_t data_attr = 0;
1515 	int htc_payload_len = actual_length;
1516 
1517 	pEndpoint = &target->endpoint[ep_id];
1518 
1519 	tx_resources = hif_get_free_queue_number(target->hif_dev,
1520 						 pEndpoint->UL_PipeID);
1521 
1522 	if (tx_resources < HTC_DATA_RESOURCE_THRS) {
1523 		if (pEndpoint->ul_is_polled) {
1524 			hif_send_complete_check(pEndpoint->target->hif_dev,
1525 						pEndpoint->UL_PipeID, 1);
1526 			tx_resources =
1527 				hif_get_free_queue_number(target->hif_dev,
1528 							  pEndpoint->UL_PipeID);
1529 		}
1530 		if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
1531 			return QDF_STATUS_E_FAILURE;
1532 	}
1533 
1534 	if (hif_pm_runtime_get(target->hif_dev))
1535 		return QDF_STATUS_E_FAILURE;
1536 
1537 	p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1538 	AR_DEBUG_ASSERT(p_htc_hdr);
1539 
1540 	data_attr = qdf_nbuf_data_attr_get(netbuf);
1541 
1542 	if (target->htc_hdr_length_check)
1543 		htc_payload_len = actual_length - HTC_HEADER_LEN;
1544 
1545 	HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
1546 		    | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
1547 	/*
1548 	 * If the HIF pipe for the data endpoint is polled rather than
1549 	 * interrupt-driven, this is a good point to check whether any
1550 	 * data previously sent through the HIF pipe have finished being
1551 	 * sent.
1552 	 * Since this may result in callbacks to htc_tx_completion_handler,
1553 	 * which can take the HTC tx lock, make the hif_send_complete_check
1554 	 * call before acquiring the HTC tx lock.
1555 	 * Call hif_send_complete_check directly, rather than calling
1556 	 * htc_send_complete_check, and call the PollTimerStart separately
1557 	 * after calling hif_send_head, so the timer will be started to
1558 	 * check for completion of the new outstanding download (in the
1559 	 * unexpected event that other polling calls don't catch it).
1560 	 */
1561 
1562 	LOCK_HTC_TX(target);
1563 
1564 	HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
1565 		    SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
1566 
1567 	pEndpoint->SeqNo++;
1568 
1569 	QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1570 	DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1571 		QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1572 		sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1573 	status = hif_send_head(target->hif_dev,
1574 			       pEndpoint->UL_PipeID,
1575 			       pEndpoint->Id, actual_length, netbuf, data_attr);
1576 
1577 	UNLOCK_HTC_TX(target);
1578 	return status;
1579 }
1580 #else                           /*ATH_11AC_TXCOMPACT */
1581 
1582 /**
1583  * htc_send_data_pkt() - htc_send_data_pkt
1584  * @HTCHandle: pointer to HTC handle
1585  * @pPacket: pointer to HTC_PACKET
1586  * @more_data: indicates whether more data is to follow
1587  *
1588  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1589  */
1590 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
1591 			   uint8_t more_data)
1592 {
1593 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1594 	HTC_ENDPOINT *pEndpoint;
1595 	HTC_FRAME_HDR *pHtcHdr;
1596 	HTC_PACKET_QUEUE sendQueue;
1597 	qdf_nbuf_t netbuf = NULL;
1598 	int tx_resources;
1599 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1600 	uint32_t data_attr = 0;
1601 
1602 	if (pPacket) {
1603 		if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1604 		   (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1605 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1606 				("%s endpoint is invalid\n", __func__));
1607 			AR_DEBUG_ASSERT(0);
1608 			return QDF_STATUS_E_INVAL;
1609 		}
1610 		pEndpoint = &target->endpoint[pPacket->Endpoint];
1611 
1612 		/* add HTC_FRAME_HDR in the initial fragment */
1613 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1614 		pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
1615 		AR_DEBUG_ASSERT(pHtcHdr);
1616 
1617 		HTC_WRITE32(pHtcHdr,
1618 				SM(pPacket->ActualLength,
1619 				       HTC_FRAME_HDR_PAYLOADLEN) |
1620 				SM(pPacket->PktInfo.AsTx.SendFlags,
1621 					HTC_FRAME_HDR_FLAGS) |
1622 				SM(pPacket->Endpoint,
1623 					HTC_FRAME_HDR_ENDPOINTID));
1624 		/*
1625 		 * If the HIF pipe for the data endpoint is polled rather than
1626 		 * interrupt-driven, this is a good point to check whether any
1627 		 * data previously sent through the HIF pipe have finished being
1628 		 * sent. Since this may result in callbacks to
1629 		 * htc_tx_completion_handler, which can take the HTC tx lock,
1630 		 * make the hif_send_complete_check call before acquiring the
1631 		 * HTC tx lock.
1632 		 * Call hif_send_complete_check directly, rather than calling
1633 		 * htc_send_complete_check, and call the PollTimerStart
1634 		 * separately after calling hif_send_head, so the timer will be
1635 		 * started to check for completion of the new outstanding
1636 		 * download (in the unexpected event that other polling calls
1637 		 * don't catch it).
1638 		 */
1639 		if (pEndpoint->ul_is_polled) {
1640 			htc_send_complete_poll_timer_stop(pEndpoint);
1641 			hif_send_complete_check(pEndpoint->target->hif_dev,
1642 						pEndpoint->UL_PipeID, 0);
1643 		}
1644 
1645 		LOCK_HTC_TX(target);
1646 
1647 		pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1648 		pEndpoint->SeqNo++;
1649 
1650 		HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
1651 			    SM(pPacket->PktInfo.AsTx.SeqNo,
1652 			       HTC_FRAME_HDR_CONTROLBYTES1));
1653 
1654 		/* append new packet to pEndpoint->TxQueue */
1655 		HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
1656 		if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
1657 			UNLOCK_HTC_TX(target);
1658 			return QDF_STATUS_SUCCESS;
1659 		}
1660 
1661 		QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1662 		DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1663 			  QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1664 				sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1665 	} else {
1666 		LOCK_HTC_TX(target);
1667 		pEndpoint = &target->endpoint[1];
1668 	}
1669 
1670 	/* increment tx processing count on entry */
1671 	qdf_atomic_inc(&pEndpoint->TxProcessCount);
1672 	if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
1673 		/*
1674 		 * Another thread or task is draining the TX queues on this
1675 		 * endpoint. That thread will reset the tx processing count when
1676 		 * the queue is drained.
1677 		 */
1678 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1679 		UNLOCK_HTC_TX(target);
1680 		return QDF_STATUS_SUCCESS;
1681 	}
1682 
1683 	/***** beyond this point only 1 thread may enter ******/
1684 
1685 	INIT_HTC_PACKET_QUEUE(&sendQueue);
1686 	if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1687 #if DEBUG_CREDIT
1688 		int cred = pEndpoint->TxCredits;
1689 #endif
1690 		get_htc_send_packets_credit_based(target, pEndpoint,
1691 						 &sendQueue);
1692 #if DEBUG_CREDIT
1693 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
1694 			if (cred - pEndpoint->TxCredits > 0) {
1695 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1696 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1697 						 pEndpoint->Id, cred,
1698 						 cred - pEndpoint->TxCredits,
1699 						 pEndpoint->TxCredits));
1700 			}
1701 		}
1702 #endif
1703 		UNLOCK_HTC_TX(target);
1704 	}
1705 
1706 	else if (HTC_TX_BUNDLE_ENABLED(target)) {
1707 
1708 		if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) &&
1709 			hif_get_free_queue_number(target->hif_dev,
1710 						pEndpoint->UL_PipeID)) {
1711 			/*
1712 			 * Header and payload belongs to the different fragments
1713 			 * and consume 2 resource for one HTC package but USB
1714 			 * combine into one transfer.
1715 			 */
1716 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1717 				(HTC_MAX_MSG_PER_BUNDLE_TX * 2));
1718 		} else {
1719 		/* Dequeue max packets from endpoint tx queue */
1720 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1721 				     HTC_MAX_TX_BUNDLE_SEND_LIMIT);
1722 		}
1723 
1724 		UNLOCK_HTC_TX(target);
1725 	} else {
1726 		/*
1727 		 * Now drain the endpoint TX queue for transmission as long as
1728 		 * we have enough transmit resources
1729 		 */
1730 		tx_resources =
1731 			hif_get_free_queue_number(target->hif_dev,
1732 						  pEndpoint->UL_PipeID);
1733 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1734 				     tx_resources);
1735 		UNLOCK_HTC_TX(target);
1736 	}
1737 
1738 	/* send what we can */
1739 	while (true) {
1740 		if (HTC_TX_BUNDLE_ENABLED(target) &&
1741 		    (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
1742 		     HTC_MIN_MSG_PER_BUNDLE) &&
1743 		    (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
1744 		     hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
1745 			htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
1746 		}
1747 		pPacket = htc_packet_dequeue(&sendQueue);
1748 		if (pPacket == NULL)
1749 			break;
1750 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1751 
1752 		LOCK_HTC_TX(target);
1753 		/* store in look up queue to match completions */
1754 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
1755 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
1756 		pEndpoint->ul_outstanding_cnt++;
1757 		UNLOCK_HTC_TX(target);
1758 
1759 		status = hif_send_head(target->hif_dev,
1760 				       pEndpoint->UL_PipeID,
1761 				       pEndpoint->Id,
1762 				       HTC_HDR_LENGTH + pPacket->ActualLength,
1763 				       netbuf, data_attr);
1764 #if DEBUG_BUNDLE
1765 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
1766 			  pEndpoint->Id,
1767 			  pEndpoint->TxCreditSize,
1768 			  HTC_HDR_LENGTH + pPacket->ActualLength);
1769 #endif
1770 
1771 		htc_issue_tx_bundle_stats_inc(target);
1772 
1773 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
1774 			LOCK_HTC_TX(target);
1775 			pEndpoint->ul_outstanding_cnt--;
1776 			/* remove this packet from the tx completion queue */
1777 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
1778 
1779 			/*
1780 			 * Don't bother reclaiming credits - HTC flow control
1781 			 * is not applicable to tx data.
1782 			 * In LL systems, there is no download flow control,
1783 			 * since there's virtually no download delay.
1784 			 * In HL systems, the txrx SW explicitly performs the
1785 			 * tx flow control.
1786 			 */
1787 			/* pEndpoint->TxCredits +=
1788 			 * pPacket->PktInfo.AsTx.CreditsUsed;
1789 			 */
1790 
1791 			/* put this frame back at the front of the sendQueue */
1792 			HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
1793 
1794 			/* put the sendQueue back at the front of
1795 			 * pEndpoint->TxQueue
1796 			 */
1797 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1798 							  &sendQueue);
1799 			UNLOCK_HTC_TX(target);
1800 			break;  /* still need to reset TxProcessCount */
1801 		}
1802 	}
1803 	/* done with this endpoint, we can clear the count */
1804 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1805 
1806 	if (pEndpoint->ul_is_polled) {
1807 		/*
1808 		 * Start a cleanup timer to poll for download completion.
1809 		 * The download completion should be noticed promptly from
1810 		 * other polling calls, but the timer provides a safety net
1811 		 * in case other polling calls don't occur as expected.
1812 		 */
1813 		htc_send_complete_poll_timer_start(pEndpoint);
1814 	}
1815 
1816 	return status;
1817 }
1818 #endif /*ATH_11AC_TXCOMPACT */
1819 qdf_export_symbol(htc_send_data_pkt);
1820 
1821 /*
1822  * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
1823  * since upper layers expects HTC_PACKET containers we use the completed netbuf
1824  * and lookup its corresponding HTC packet buffer from a lookup list.
1825  * This is extra overhead that can be fixed by re-aligning HIF interfaces
1826  * with HTC.
1827  *
1828  */
1829 static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
1830 					HTC_ENDPOINT *pEndpoint,
1831 					qdf_nbuf_t netbuf)
1832 {
1833 	HTC_PACKET *pPacket = NULL;
1834 	HTC_PACKET *pFoundPacket = NULL;
1835 	HTC_PACKET_QUEUE lookupQueue;
1836 
1837 	INIT_HTC_PACKET_QUEUE(&lookupQueue);
1838 	LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1839 
1840 	LOCK_HTC_TX(target);
1841 
1842 	/* mark that HIF has indicated the send complete for another packet */
1843 	pEndpoint->ul_outstanding_cnt--;
1844 
1845 	/* Dequeue first packet directly because of in-order completion */
1846 	pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
1847 	if (qdf_unlikely(!pPacket)) {
1848 		UNLOCK_HTC_TX(target);
1849 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1850 		return NULL;
1851 	}
1852 	if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1853 		UNLOCK_HTC_TX(target);
1854 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1855 		return pPacket;
1856 	}
1857 	HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
1858 
1859 	/*
1860 	 * Move TX lookup queue to temp queue because most of packets that are
1861 	 * not index 0 are not top 10 packets.
1862 	 */
1863 	HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
1864 					  &pEndpoint->TxLookupQueue);
1865 
1866 	ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
1867 				       HTC_PACKET, ListLink) {
1868 
1869 		if (NULL == pPacket) {
1870 			pFoundPacket = pPacket;
1871 			break;
1872 		}
1873 		/* check for removal */
1874 		if (netbuf ==
1875 		    (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1876 			/* found it */
1877 			HTC_PACKET_REMOVE(&lookupQueue, pPacket);
1878 			pFoundPacket = pPacket;
1879 			break;
1880 		}
1881 
1882 	}
1883 	ITERATE_END;
1884 
1885 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
1886 					  &lookupQueue);
1887 	UNLOCK_HTC_TX(target);
1888 	UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1889 
1890 	return pFoundPacket;
1891 }
1892 
1893 /**
1894  * htc_tx_completion_handler() - htc tx completion handler
1895  * @Context: pointer to HTC_TARGET structure
1896  * @netbuf: pointer to netbuf for which completion handler is being called
1897  * @EpID: end point Id on which the packet was sent
1898  * @toeplitz_hash_result: toeplitz hash result
1899  *
1900  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1901  */
1902 QDF_STATUS htc_tx_completion_handler(void *Context,
1903 				   qdf_nbuf_t netbuf, unsigned int EpID,
1904 				   uint32_t toeplitz_hash_result)
1905 {
1906 	HTC_TARGET *target = (HTC_TARGET *) Context;
1907 	HTC_ENDPOINT *pEndpoint;
1908 	HTC_PACKET *pPacket;
1909 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1910 	HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
1911 		ENDPOINT_2, ENDPOINT_3 };
1912 	int epidIdx;
1913 	uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
1914 	uint16_t resources;
1915 	uint16_t resourcesMax;
1916 #endif
1917 
1918 	pEndpoint = &target->endpoint[EpID];
1919 	target->TX_comp_cnt++;
1920 
1921 	do {
1922 		pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
1923 		if (NULL == pPacket) {
1924 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1925 					("HTC TX lookup failed!\n"));
1926 			/* may have already been flushed and freed */
1927 			netbuf = NULL;
1928 			break;
1929 		}
1930 		if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
1931 			hif_pm_runtime_put(target->hif_dev);
1932 
1933 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
1934 			HTC_PACKET *pPacketTemp;
1935 			HTC_PACKET_QUEUE *pQueueSave =
1936 				(HTC_PACKET_QUEUE *) pPacket->pContext;
1937 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
1938 							      pPacketTemp) {
1939 				pPacket->Status = QDF_STATUS_SUCCESS;
1940 				send_packet_completion(target, pPacketTemp);
1941 			}
1942 			HTC_PACKET_QUEUE_ITERATE_END;
1943 			free_htc_bundle_packet(target, pPacket);
1944 
1945 			if (hif_get_bus_type(target->hif_dev) ==
1946 					     QDF_BUS_TYPE_USB) {
1947 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
1948 					htc_try_send(target, pEndpoint, NULL);
1949 			}
1950 
1951 			return QDF_STATUS_SUCCESS;
1952 		}
1953 		/* will be giving this buffer back to upper layers */
1954 		netbuf = NULL;
1955 		pPacket->Status = QDF_STATUS_SUCCESS;
1956 		send_packet_completion(target, pPacket);
1957 
1958 	} while (false);
1959 
1960 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1961 		/* note: when using TX credit flow, the re-checking of queues
1962 		 * happens when credits flow back from the target. In the non-TX
1963 		 * credit case, we recheck after the packet completes
1964 		 */
1965 		if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
1966 				(!pEndpoint->async_update)) {
1967 			htc_try_send(target, pEndpoint, NULL);
1968 		}
1969 	}
1970 
1971 	return QDF_STATUS_SUCCESS;
1972 }
1973 
1974 #ifdef WLAN_FEATURE_FASTPATH
1975 /**
1976  * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
1977  * @HTC_HANDLE : pointer to the htc target context
1978  * @htc_ep_id  : end point id
1979  *
1980  * checks HTC tx completion
1981  *
1982  * Return: none
1983  */
1984 void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
1985 {
1986 	HTC_TARGET      *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
1987 	HTC_ENDPOINT    *pendpoint = &target->endpoint[htc_ep_id];
1988 
1989 	htc_send_complete_check(pendpoint, 1);
1990 }
1991 qdf_export_symbol(htc_ctrl_msg_cmpl);
1992 #endif
1993 
1994 /* callback when TX resources become available */
1995 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
1996 {
1997 	int i;
1998 	HTC_TARGET *target = (HTC_TARGET *) context;
1999 	HTC_ENDPOINT *pEndpoint = NULL;
2000 
2001 	for (i = 0; i < ENDPOINT_MAX; i++) {
2002 		pEndpoint = &target->endpoint[i];
2003 		if (pEndpoint->service_id != 0) {
2004 			if (pEndpoint->UL_PipeID == pipeID)
2005 				break;
2006 		}
2007 	}
2008 
2009 	if (i >= ENDPOINT_MAX) {
2010 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2011 				("Invalid pipe indicated for TX resource avail : %d!\n",
2012 				 pipeID));
2013 		return;
2014 	}
2015 
2016 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2017 			("HIF indicated more resources for pipe:%d\n",
2018 			 pipeID));
2019 
2020 	htc_try_send(target, pEndpoint, NULL);
2021 }
2022 
2023 #ifdef FEATURE_RUNTIME_PM
2024 /**
2025  * htc_kick_queues(): resumes tx transactions of suspended endpoints
2026  * @context: pointer to the htc target context
2027  *
2028  * Iterates through the enpoints and provides a context to empty queues
2029  * int the hif layer when they are stalled due to runtime suspend.
2030  *
2031  * Return: none
2032  */
2033 void htc_kick_queues(void *context)
2034 {
2035 	int i;
2036 	HTC_TARGET *target = (HTC_TARGET *)context;
2037 	HTC_ENDPOINT *endpoint = NULL;
2038 
2039 	for (i = 0; i < ENDPOINT_MAX; i++) {
2040 		endpoint = &target->endpoint[i];
2041 
2042 		if (endpoint->service_id == 0)
2043 			continue;
2044 
2045 		if (endpoint->EpCallBacks.ep_resume_tx_queue)
2046 			endpoint->EpCallBacks.ep_resume_tx_queue(
2047 					endpoint->EpCallBacks.pContext);
2048 
2049 		htc_try_send(target, endpoint, NULL);
2050 	}
2051 
2052 	hif_fastpath_resume(target->hif_dev);
2053 }
2054 #endif
2055 
2056 /* flush endpoint TX queue */
2057 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
2058 			   HTC_TX_TAG Tag)
2059 {
2060 	HTC_PACKET *pPacket;
2061 
2062 	LOCK_HTC_TX(target);
2063 	while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2064 		pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
2065 
2066 		if (pPacket) {
2067 			/* let the sender know the packet was not delivered */
2068 			pPacket->Status = QDF_STATUS_E_CANCELED;
2069 			send_packet_completion(target, pPacket);
2070 		}
2071 	}
2072 	UNLOCK_HTC_TX(target);
2073 }
2074 
2075 /* flush pending entries in endpoint TX Lookup queue */
2076 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
2077 				  HTC_ENDPOINT_ID endpoint_id,
2078 				  bool call_ep_callback)
2079 {
2080 	HTC_PACKET *packet;
2081 	HTC_ENDPOINT *endpoint;
2082 
2083 	endpoint = &target->endpoint[endpoint_id];
2084 
2085 	if (!endpoint && endpoint->service_id == 0)
2086 		return;
2087 
2088 	LOCK_HTC_TX(target);
2089 	while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
2090 		packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
2091 
2092 		if (packet) {
2093 			if (call_ep_callback == true) {
2094 				packet->Status = QDF_STATUS_E_CANCELED;
2095 				send_packet_completion(target, packet);
2096 			} else {
2097 				qdf_mem_free(packet);
2098 			}
2099 		}
2100 	}
2101 	UNLOCK_HTC_TX(target);
2102 }
2103 
2104 /* HTC API to flush an endpoint's TX queue*/
2105 void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
2106 			HTC_TX_TAG Tag)
2107 {
2108 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2109 	HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
2110 
2111 	if (pEndpoint->service_id == 0) {
2112 		AR_DEBUG_ASSERT(false);
2113 		/* not in use.. */
2114 		return;
2115 	}
2116 
2117 	htc_flush_endpoint_tx(target, pEndpoint, Tag);
2118 }
2119 
2120 /* HTC API to indicate activity to the credit distribution function */
2121 void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
2122 				  HTC_ENDPOINT_ID Endpoint, bool Active)
2123 {
2124 	/*  TODO */
2125 }
2126 
2127 bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
2128 {
2129 	return true;
2130 }
2131 
2132 void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
2133 {
2134 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2135 
2136 	target->is_nodrop_pkt = isNodropPkt;
2137 }
2138 
2139 void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
2140 {
2141 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2142 
2143 	target->htc_hdr_length_check = htc_hdr_length_check;
2144 }
2145 
2146 /**
2147  * htc_process_credit_rpt() - process credit report, call distribution function
2148  * @target: pointer to HTC_TARGET
2149  * @pRpt: pointer to HTC_CREDIT_REPORT
2150  * @NumEntries: number of entries in credit report
2151  * @FromEndpoint: endpoint for which  credit report is received
2152  *
2153  * Return: A_OK for success or an appropriate A_STATUS error
2154  */
2155 void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
2156 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
2157 {
2158 	int i;
2159 	HTC_ENDPOINT *pEndpoint;
2160 	int totalCredits = 0;
2161 	uint8_t rpt_credits, rpt_ep_id;
2162 
2163 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2164 			("+htc_process_credit_rpt, Credit Report Entries:%d\n",
2165 			 NumEntries));
2166 
2167 	/* lock out TX while we update credits */
2168 	LOCK_HTC_TX(target);
2169 
2170 	for (i = 0; i < NumEntries; i++, pRpt++) {
2171 
2172 		rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
2173 
2174 		if (rpt_ep_id >= ENDPOINT_MAX) {
2175 			AR_DEBUG_ASSERT(false);
2176 			break;
2177 		}
2178 
2179 		rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
2180 
2181 		pEndpoint = &target->endpoint[rpt_ep_id];
2182 #if DEBUG_CREDIT
2183 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2184 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2185 					(" <HTC> Increase EP%d %d + %d = %d credits\n",
2186 					 rpt_ep_id, pEndpoint->TxCredits,
2187 					 rpt_credits,
2188 					 pEndpoint->TxCredits + rpt_credits));
2189 		}
2190 #endif
2191 
2192 #ifdef HTC_EP_STAT_PROFILING
2193 
2194 		INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
2195 		INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
2196 
2197 		if (FromEndpoint == rpt_ep_id) {
2198 			/* this credit report arrived on the same endpoint
2199 			 * indicating it arrived in an RX packet
2200 			 */
2201 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
2202 					rpt_credits);
2203 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
2204 		} else if (FromEndpoint == ENDPOINT_0) {
2205 			/* this credit arrived on endpoint 0 as a NULL msg */
2206 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
2207 					rpt_credits);
2208 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
2209 		} else {
2210 			/* arrived on another endpoint */
2211 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
2212 					rpt_credits);
2213 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
2214 		}
2215 
2216 #endif
2217 
2218 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
2219 			htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
2220 					  pEndpoint->TxCredits + rpt_credits,
2221 					  HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
2222 							TxQueue));
2223 		}
2224 
2225 		pEndpoint->TxCredits += rpt_credits;
2226 
2227 		if (pEndpoint->TxCredits
2228 		    && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2229 			UNLOCK_HTC_TX(target);
2230 #ifdef ATH_11AC_TXCOMPACT
2231 			htc_try_send(target, pEndpoint, NULL);
2232 #else
2233 			if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
2234 				htc_send_data_pkt(target, NULL, 0);
2235 			else
2236 				htc_try_send(target, pEndpoint, NULL);
2237 #endif
2238 			LOCK_HTC_TX(target);
2239 		}
2240 		totalCredits += rpt_credits;
2241 	}
2242 
2243 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2244 			("  Report indicated %d credits to distribute\n",
2245 			 totalCredits));
2246 
2247 	UNLOCK_HTC_TX(target);
2248 
2249 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
2250 }
2251 
2252 /* function to fetch stats from htc layer*/
2253 struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
2254 {
2255 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2256 
2257 	return &(target->htc_pkt_stats);
2258 }
2259