xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_send.c (revision 1397a33f48ea6455be40871470b286e535820eb8)
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 			/* reclaim credits */
650 			pEndpoint->TxCredits +=
651 				pPacket->PktInfo.AsTx.CreditsUsed;
652 			htc_packet_set_magic_cookie(pPacket, 0);
653 			/* put it back into the callers queue */
654 			HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
655 			if (!pEndpoint->async_update) {
656 				UNLOCK_HTC_TX(target);
657 			}
658 			break;
659 		}
660 		if (rt_put) {
661 			hif_pm_runtime_put(target->hif_dev);
662 			rt_put = false;
663 		}
664 	}
665 	if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
666 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
667 			("htc_issue_packets, failed pkt:0x%pK status:%d",
668 			 pPacket, status));
669 	}
670 
671 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets\n"));
672 
673 	return status;
674 }
675 
676 #ifdef FEATURE_RUNTIME_PM
677 /**
678  * extract_htc_pm_packets(): move pm packets from endpoint into queue
679  * @endpoint: which enpoint to extract packets from
680  * @queue: a queue to store extracted packets in.
681  *
682  * remove pm packets from the endpoint's tx queue.
683  * queue them into a queue
684  */
685 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
686 				HTC_PACKET_QUEUE *queue)
687 {
688 	HTC_PACKET *packet;
689 
690 	/* only WMI endpoint has power management packets */
691 	if (endpoint->service_id != WMI_CONTROL_SVC)
692 		return;
693 
694 	ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
695 			HTC_PACKET, ListLink) {
696 		if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
697 			HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
698 			HTC_PACKET_ENQUEUE(queue, packet);
699 		}
700 	} ITERATE_END
701 }
702 
703 /**
704  * queue_htc_pm_packets(): queue pm packets with priority
705  * @endpoint: enpoint to queue packets to
706  * @queue: queue of pm packets to enque
707  *
708  * suspend resume packets get special treatment & priority.
709  * need to queue them at the front of the queue.
710  */
711 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
712 				 HTC_PACKET_QUEUE *queue)
713 {
714 	if (endpoint->service_id != WMI_CONTROL_SVC)
715 		return;
716 
717 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
718 }
719 #else
720 static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
721 		HTC_PACKET_QUEUE *queue)
722 {}
723 
724 static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
725 		HTC_PACKET_QUEUE *queue)
726 {}
727 #endif
728 
729 /**
730  * get_htc_send_packets_credit_based() - get packets based on available credits
731  * @target: HTC target on which packets need to be sent
732  * @pEndpoint: logical endpoint on which packets needs to be sent
733  * @pQueue: HTC packet queue containing the list of packets to be sent
734  *
735  * Get HTC send packets from TX queue on an endpoint based on available credits.
736  * The function moves the packets from TX queue of the endpoint to pQueue.
737  *
738  * Return: None
739  */
740 static void get_htc_send_packets_credit_based(HTC_TARGET *target,
741 					      HTC_ENDPOINT *pEndpoint,
742 					      HTC_PACKET_QUEUE *pQueue)
743 {
744 	int creditsRequired;
745 	int remainder;
746 	uint8_t sendFlags;
747 	HTC_PACKET *pPacket;
748 	unsigned int transferLength;
749 	HTC_PACKET_QUEUE *tx_queue;
750 	HTC_PACKET_QUEUE pm_queue;
751 	bool do_pm_get = false;
752 
753 	/*** NOTE : the TX lock is held when this function is called ***/
754 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
755 			("+get_htc_send_packets_credit_based\n"));
756 
757 	INIT_HTC_PACKET_QUEUE(&pm_queue);
758 	extract_htc_pm_packets(pEndpoint, &pm_queue);
759 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
760 		tx_queue = &pEndpoint->TxQueue;
761 		do_pm_get = true;
762 	} else {
763 		tx_queue = &pm_queue;
764 	}
765 
766 	/* loop until we can grab as many packets out of the queue as we can */
767 	while (true) {
768 		if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
769 			/* bus suspended, runtime resume issued */
770 			QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
771 			break;
772 		}
773 
774 		sendFlags = 0;
775 		/* get packet at head, but don't remove it */
776 		pPacket = htc_get_pkt_at_head(tx_queue);
777 		if (pPacket == NULL) {
778 			if (do_pm_get)
779 				hif_pm_runtime_put(target->hif_dev);
780 			break;
781 		}
782 
783 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
784 				(" Got head packet:%pK , Queue Depth: %d\n",
785 				 pPacket,
786 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
787 
788 		transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
789 
790 		if (transferLength <= pEndpoint->TxCreditSize) {
791 			creditsRequired = 1;
792 		} else {
793 			/* figure out how many credits this message requires */
794 			creditsRequired =
795 				transferLength / pEndpoint->TxCreditSize;
796 			remainder = transferLength % pEndpoint->TxCreditSize;
797 
798 			if (remainder)
799 				creditsRequired++;
800 		}
801 
802 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
803 				(" Credits Required:%d   Got:%d\n",
804 				 creditsRequired, pEndpoint->TxCredits));
805 
806 		if (pEndpoint->Id == ENDPOINT_0) {
807 			/*
808 			 * endpoint 0 is special, it always has a credit and
809 			 * does not require credit based flow control
810 			 */
811 			creditsRequired = 0;
812 		} else {
813 
814 			if (pEndpoint->TxCredits < creditsRequired) {
815 #if DEBUG_CREDIT
816 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
817 						("EP%d,No Credit now.%d < %d\n",
818 						 pEndpoint->Id,
819 						 pEndpoint->TxCredits,
820 						 creditsRequired));
821 #endif
822 				if (do_pm_get)
823 					hif_pm_runtime_put(target->hif_dev);
824 				break;
825 			}
826 
827 			pEndpoint->TxCredits -= creditsRequired;
828 			INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
829 					creditsRequired);
830 
831 			/* check if we need credits back from the target */
832 			if (pEndpoint->TxCredits <=
833 			    pEndpoint->TxCreditsPerMaxMsg) {
834 				/* tell the target we need credits ASAP! */
835 				sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
836 				if (pEndpoint->service_id == WMI_CONTROL_SVC) {
837 					htc_credit_record(HTC_REQUEST_CREDIT,
838 							  pEndpoint->TxCredits,
839 							  HTC_PACKET_QUEUE_DEPTH
840 							  (tx_queue));
841 				}
842 				INC_HTC_EP_STAT(pEndpoint,
843 						TxCreditLowIndications, 1);
844 #if DEBUG_CREDIT
845 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
846 						(" EP%d Needs Credits\n",
847 						 pEndpoint->Id));
848 #endif
849 			}
850 		}
851 
852 		/* now we can fully dequeue */
853 		pPacket = htc_packet_dequeue(tx_queue);
854 		if (pPacket) {
855 			/* save the number of credits this packet consumed */
856 			pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
857 			/* save send flags */
858 			pPacket->PktInfo.AsTx.SendFlags = sendFlags;
859 
860 			/* queue this packet into the caller's queue */
861 			HTC_PACKET_ENQUEUE(pQueue, pPacket);
862 		}
863 	}
864 
865 	if (!HTC_QUEUE_EMPTY(&pm_queue))
866 		queue_htc_pm_packets(pEndpoint, &pm_queue);
867 
868 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
869 			("-get_htc_send_packets_credit_based\n"));
870 
871 }
872 
873 static void get_htc_send_packets(HTC_TARGET *target,
874 				 HTC_ENDPOINT *pEndpoint,
875 				 HTC_PACKET_QUEUE *pQueue, int Resources)
876 {
877 
878 	HTC_PACKET *pPacket;
879 	HTC_PACKET_QUEUE *tx_queue;
880 	HTC_PACKET_QUEUE pm_queue;
881 	bool do_pm_get = false;
882 
883 	/*** NOTE : the TX lock is held when this function is called ***/
884 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
885 			("+get_htc_send_packets %d resources\n", Resources));
886 
887 	INIT_HTC_PACKET_QUEUE(&pm_queue);
888 	extract_htc_pm_packets(pEndpoint, &pm_queue);
889 	if (HTC_QUEUE_EMPTY(&pm_queue)) {
890 		tx_queue = &pEndpoint->TxQueue;
891 		do_pm_get = true;
892 	} else {
893 		tx_queue = &pm_queue;
894 	}
895 
896 	/* loop until we can grab as many packets out of the queue as we can */
897 	while (Resources > 0) {
898 		int num_frags;
899 
900 		if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
901 			/* bus suspended, runtime resume issued */
902 			QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
903 			break;
904 		}
905 
906 		pPacket = htc_packet_dequeue(tx_queue);
907 		if (pPacket == NULL) {
908 			if (do_pm_get)
909 				hif_pm_runtime_put(target->hif_dev);
910 			break;
911 		}
912 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
913 				(" Got packet:%pK , New Queue Depth: %d\n",
914 				 pPacket,
915 				 HTC_PACKET_QUEUE_DEPTH(tx_queue)));
916 		/* For non-credit path the sequence number is already embedded
917 		 * in the constructed HTC header
918 		 */
919 		pPacket->PktInfo.AsTx.SendFlags = 0;
920 		pPacket->PktInfo.AsTx.CreditsUsed = 0;
921 		/* queue this packet into the caller's queue */
922 		HTC_PACKET_ENQUEUE(pQueue, pPacket);
923 
924 		/*
925 		 * FIX THIS:
926 		 * For now, avoid calling qdf_nbuf_get_num_frags before calling
927 		 * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
928 		 * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
929 		 * been done.
930 		 * Assume that the non-data netbufs, i.e. WMI message netbufs,
931 		 * consist of a single fragment.
932 		 */
933 		/* WMI messages are in a single-fragment network buf */
934 		num_frags =
935 			(pPacket->PktInfo.AsTx.
936 			 Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
937 			qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
938 						       (pPacket));
939 		Resources -= num_frags;
940 	}
941 
942 	if (!HTC_QUEUE_EMPTY(&pm_queue))
943 		queue_htc_pm_packets(pEndpoint, &pm_queue);
944 
945 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
946 
947 }
948 
949 /**
950  * htc_try_send() - Send packets in a queue on an endpoint
951  * @target: HTC target on which packets need to be sent
952  * @pEndpoint: logical endpoint on which packets needs to be sent
953  * @pCallersSendQueue: packet queue containing the list of packets to be sent
954  *
955  * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
956  *         be sent or the packet should be dropped by the upper layer
957  */
958 static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
959 					  HTC_ENDPOINT *pEndpoint,
960 					  HTC_PACKET_QUEUE *pCallersSendQueue)
961 {
962 	/* temp queue to hold packets at various stages */
963 	HTC_PACKET_QUEUE sendQueue;
964 	HTC_PACKET *pPacket;
965 	int tx_resources;
966 	int overflow;
967 	enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
968 
969 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
970 					 pCallersSendQueue,
971 					 (pCallersSendQueue ==
972 					  NULL) ? 0 :
973 					 HTC_PACKET_QUEUE_DEPTH
974 						 (pCallersSendQueue)));
975 
976 	/* init the local send queue */
977 	INIT_HTC_PACKET_QUEUE(&sendQueue);
978 
979 	do {
980 
981 		/* caller didn't provide a queue, just wants us to check
982 		 * queues and send
983 		 */
984 		if (pCallersSendQueue == NULL)
985 			break;
986 
987 		if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
988 			/* empty queue */
989 			OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
990 							HTC_PKT_Q_EMPTY);
991 			result = HTC_SEND_QUEUE_DROP;
992 			break;
993 		}
994 
995 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
996 		    pEndpoint->MaxTxQueueDepth) {
997 			/* we've already overflowed */
998 			overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
999 		} else {
1000 			/* figure out how much we will overflow by */
1001 			overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1002 			overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
1003 			/* get how much we will overflow the TX queue by */
1004 			overflow -= pEndpoint->MaxTxQueueDepth;
1005 		}
1006 
1007 		/* if overflow is negative or zero, we are okay */
1008 		if (overflow > 0) {
1009 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1010 					("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
1011 					 pEndpoint->Id, overflow,
1012 					 HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
1013 								TxQueue),
1014 					 pEndpoint->MaxTxQueueDepth));
1015 		}
1016 		if ((overflow <= 0)
1017 		    || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
1018 			/* all packets will fit or caller did not provide send
1019 			 * full indication handler
1020 			 * just move all of them to local sendQueue object
1021 			 */
1022 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
1023 							  pCallersSendQueue);
1024 		} else {
1025 			int i;
1026 			int goodPkts =
1027 				HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
1028 				overflow;
1029 
1030 			A_ASSERT(goodPkts >= 0);
1031 			/* we have overflowed and callback is provided. Dequeue
1032 			 * all non-overflow packets into the sendqueue
1033 			 */
1034 			for (i = 0; i < goodPkts; i++) {
1035 				/* pop off caller's queue */
1036 				pPacket = htc_packet_dequeue(pCallersSendQueue);
1037 				A_ASSERT(pPacket != NULL);
1038 				/* insert into local queue */
1039 				HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1040 			}
1041 
1042 			/* the caller's queue has all the packets that won't fit
1043 			 * walk through the caller's queue and indicate each one
1044 			 * to the send full handler
1045 			 */
1046 			ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
1047 						       QueueHead, pPacket,
1048 						       HTC_PACKET, ListLink) {
1049 
1050 				AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1051 						("Indicating overflowed TX packet: %pK\n",
1052 						 pPacket));
1053 				/*
1054 				 * Remove headroom reserved for HTC_FRAME_HDR
1055 				 * before giving the packet back to the user via
1056 				 * the EpSendFull callback.
1057 				 */
1058 				restore_tx_packet(target, pPacket);
1059 
1060 				if (pEndpoint->EpCallBacks.
1061 				    EpSendFull(pEndpoint->EpCallBacks.pContext,
1062 					       pPacket) == HTC_SEND_FULL_DROP) {
1063 					/* callback wants the packet dropped */
1064 					INC_HTC_EP_STAT(pEndpoint, TxDropped,
1065 							1);
1066 					/* leave this one in the caller's queue
1067 					 * for cleanup
1068 					 */
1069 				} else {
1070 					/* callback wants to keep this packet,
1071 					 * remove from caller's queue
1072 					 */
1073 					HTC_PACKET_REMOVE(pCallersSendQueue,
1074 							  pPacket);
1075 					/* put it in the send queue
1076 					 * add HTC_FRAME_HDR space reservation
1077 					 * again
1078 					 */
1079 					qdf_nbuf_push_head
1080 						(GET_HTC_PACKET_NET_BUF_CONTEXT
1081 							(pPacket),
1082 						sizeof(HTC_FRAME_HDR));
1083 
1084 					HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
1085 				}
1086 
1087 			}
1088 			ITERATE_END;
1089 
1090 			if (HTC_QUEUE_EMPTY(&sendQueue)) {
1091 				/* no packets made it in, caller will cleanup */
1092 				OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
1093 							HTC_SEND_Q_EMPTY);
1094 				result = HTC_SEND_QUEUE_DROP;
1095 				break;
1096 			}
1097 		}
1098 
1099 	} while (false);
1100 
1101 	if (result != HTC_SEND_QUEUE_OK) {
1102 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
1103 			result));
1104 		return result;
1105 	}
1106 
1107 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1108 		tx_resources =
1109 			hif_get_free_queue_number(target->hif_dev,
1110 						  pEndpoint->UL_PipeID);
1111 	} else {
1112 		tx_resources = 0;
1113 	}
1114 
1115 	LOCK_HTC_TX(target);
1116 
1117 	if (!HTC_QUEUE_EMPTY(&sendQueue)) {
1118 		if (target->is_nodrop_pkt) {
1119 			/*
1120 			 * nodrop pkts have higher priority than normal pkts,
1121 			 * insert nodrop pkt to head for proper
1122 			 * start/termination of test.
1123 			 */
1124 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1125 					&sendQueue);
1126 			target->is_nodrop_pkt = false;
1127 		} else {
1128 			/* transfer packets to tail */
1129 			HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
1130 					&sendQueue);
1131 			A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
1132 			INIT_HTC_PACKET_QUEUE(&sendQueue);
1133 		}
1134 	}
1135 
1136 	/* increment tx processing count on entry */
1137 	if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
1138 		/* another thread or task is draining the TX queues on this
1139 		 * endpoint that thread will reset the tx processing count when
1140 		 * the queue is drained
1141 		 */
1142 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1143 		UNLOCK_HTC_TX(target);
1144 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
1145 		return HTC_SEND_QUEUE_OK;
1146 	}
1147 
1148 	/***** beyond this point only 1 thread may enter ******/
1149 
1150 	/* now drain the endpoint TX queue for transmission as long as we have
1151 	 * enough transmit resources
1152 	 */
1153 	while (true) {
1154 
1155 		if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
1156 			break;
1157 
1158 		if (pEndpoint->async_update &&
1159 			(!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
1160 			(!tx_resources))
1161 			break;
1162 
1163 		if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1164 #if DEBUG_CREDIT
1165 			int cred = pEndpoint->TxCredits;
1166 #endif
1167 			/* credit based mechanism provides flow control based on
1168 			 * target transmit resource availability, we assume that
1169 			 * the HIF layer will always have bus resources greater
1170 			 * than target transmit resources
1171 			 */
1172 			get_htc_send_packets_credit_based(target, pEndpoint,
1173 							  &sendQueue);
1174 #if DEBUG_CREDIT
1175 			if (ep_debug_mask & (1 << pEndpoint->Id)) {
1176 				if (cred - pEndpoint->TxCredits > 0) {
1177 					AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1178 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1179 							 pEndpoint->Id, cred,
1180 							 cred -
1181 							 pEndpoint->TxCredits,
1182 							 pEndpoint->TxCredits));
1183 				}
1184 			}
1185 #endif
1186 		} else {
1187 
1188 		/*
1189 		* Header and payload belongs to the different fragments and
1190 		* consume 2 resource for one HTC package but USB combine into
1191 		* one transfer.And one WMI message only consumes one single
1192 		* resource.
1193 		*/
1194 			if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
1195 				hif_get_bus_type(target->hif_dev) ==
1196 						QDF_BUS_TYPE_USB) {
1197 				if (pEndpoint->service_id ==
1198 					WMI_CONTROL_SVC)
1199 					tx_resources =
1200 					    HTC_MAX_MSG_PER_BUNDLE_TX;
1201 				else
1202 					tx_resources =
1203 					    (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
1204 			}
1205 			/* get all the packets for this endpoint that we can for
1206 			 * this pass
1207 			 */
1208 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1209 					     tx_resources);
1210 		}
1211 
1212 		if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
1213 			/* didn't get any packets due to a lack of resources or
1214 			 * TX queue was drained
1215 			 */
1216 			break;
1217 		}
1218 
1219 		if (!pEndpoint->async_update)
1220 			UNLOCK_HTC_TX(target);
1221 
1222 		/* send what we can */
1223 		if (htc_issue_packets(target, pEndpoint, &sendQueue)) {
1224 			int i;
1225 
1226 			result = HTC_SEND_QUEUE_DROP;
1227 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1228 				("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
1229 				 result));
1230 
1231 			for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
1232 				hif_pm_runtime_put(target->hif_dev);
1233 			if (!pEndpoint->async_update) {
1234 				LOCK_HTC_TX(target);
1235 			}
1236 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1237 							  &sendQueue);
1238 			break;
1239 		}
1240 
1241 		if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1242 			tx_resources =
1243 				hif_get_free_queue_number(target->hif_dev,
1244 							  pEndpoint->UL_PipeID);
1245 		}
1246 
1247 		if (!pEndpoint->async_update) {
1248 			LOCK_HTC_TX(target);
1249 		}
1250 
1251 	}
1252 
1253 	/* done with this endpoint, we can clear the count */
1254 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1255 
1256 	UNLOCK_HTC_TX(target);
1257 
1258 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
1259 
1260 	return HTC_SEND_QUEUE_OK;
1261 }
1262 
1263 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1264 static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
1265 					  HTC_ENDPOINT_ID id)
1266 {
1267 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1268 	HTC_ENDPOINT *pEndpoint;
1269 	HTC_ENDPOINT_ID eid;
1270 	HTC_PACKET_QUEUE *pTxQueue;
1271 	uint16_t resources;
1272 	uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
1273 
1274 	if (id < ENDPOINT_2 || id > ENDPOINT_5)
1275 		return 1;
1276 
1277 	for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
1278 		pEndpoint = &target->endpoint[eid];
1279 		pTxQueue = &pEndpoint->TxQueue;
1280 
1281 		if (HTC_QUEUE_EMPTY(pTxQueue))
1282 			acQueueStatus[eid - 2] = 1;
1283 	}
1284 
1285 	switch (id) {
1286 	case ENDPOINT_2:        /* BE */
1287 		return acQueueStatus[0] && acQueueStatus[2]
1288 			&& acQueueStatus[3];
1289 	case ENDPOINT_3:        /* BK */
1290 		return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
1291 			&& acQueueStatus[3];
1292 	case ENDPOINT_4:        /* VI */
1293 		return acQueueStatus[2] && acQueueStatus[3];
1294 	case ENDPOINT_5:        /* VO */
1295 		return acQueueStatus[3];
1296 	default:
1297 		return 0;
1298 	}
1299 
1300 }
1301 
1302 static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
1303 					  HTC_PACKET_QUEUE *pPktQueue,
1304 					  HTC_ENDPOINT_ID eid)
1305 {
1306 	HTC_ENDPOINT *pEndpoint;
1307 	HTC_PACKET_QUEUE *pTxQueue;
1308 	HTC_PACKET *pPacket;
1309 	int goodPkts;
1310 
1311 	pEndpoint = &target->endpoint[eid];
1312 	pTxQueue = &pEndpoint->TxQueue;
1313 
1314 	LOCK_HTC_TX(target);
1315 
1316 	goodPkts =
1317 		pEndpoint->MaxTxQueueDepth -
1318 		HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
1319 
1320 	if (goodPkts > 0) {
1321 		while (!HTC_QUEUE_EMPTY(pPktQueue)) {
1322 			pPacket = htc_packet_dequeue(pPktQueue);
1323 			HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1324 			goodPkts--;
1325 
1326 			if (goodPkts <= 0)
1327 				break;
1328 		}
1329 	}
1330 
1331 	if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
1332 		ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
1333 					       HTC_PACKET, ListLink) {
1334 
1335 			if (pEndpoint->EpCallBacks.
1336 			    EpSendFull(pEndpoint->EpCallBacks.pContext,
1337 				       pPacket) == HTC_SEND_FULL_DROP) {
1338 				INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
1339 			} else {
1340 				HTC_PACKET_REMOVE(pPktQueue, pPacket);
1341 				HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
1342 			}
1343 		}
1344 		ITERATE_END;
1345 	}
1346 
1347 	UNLOCK_HTC_TX(target);
1348 
1349 	return A_OK;
1350 }
1351 
1352 #endif
1353 
1354 static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
1355 				HTC_PACKET *pPacket)
1356 {
1357 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1358 	HTC_ENDPOINT *pEndpoint;
1359 	HTC_PACKET_QUEUE pPktQueue;
1360 	qdf_nbuf_t netbuf;
1361 	HTC_FRAME_HDR *htc_hdr;
1362 	QDF_STATUS status;
1363 
1364 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1365 			("+__htc_send_pkt\n"));
1366 
1367 	/* get packet at head to figure out which endpoint these packets will
1368 	 * go into
1369 	 */
1370 	if (NULL == pPacket) {
1371 		OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
1372 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1373 		return QDF_STATUS_E_INVAL;
1374 	}
1375 
1376 	if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1377 	    (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1378 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1379 			("%s endpoint is invalid\n", __func__));
1380 		AR_DEBUG_ASSERT(0);
1381 		return QDF_STATUS_E_INVAL;
1382 	}
1383 	pEndpoint = &target->endpoint[pPacket->Endpoint];
1384 
1385 	if (!pEndpoint->service_id) {
1386 		AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
1387 								__func__));
1388 		return QDF_STATUS_E_INVAL;
1389 	}
1390 
1391 #ifdef HTC_EP_STAT_PROFILING
1392 	LOCK_HTC_TX(target);
1393 	INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
1394 	UNLOCK_HTC_TX(target);
1395 #endif
1396 
1397 	/* provide room in each packet's netbuf for the HTC frame header */
1398 	netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1399 	AR_DEBUG_ASSERT(netbuf);
1400 	if (!netbuf)
1401 		return QDF_STATUS_E_INVAL;
1402 
1403 	qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
1404 	/* setup HTC frame header */
1405 	htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1406 	AR_DEBUG_ASSERT(htc_hdr);
1407 	if (!htc_hdr)
1408 		return QDF_STATUS_E_INVAL;
1409 
1410 	HTC_WRITE32(htc_hdr,
1411 		    SM(pPacket->ActualLength,
1412 		       HTC_FRAME_HDR_PAYLOADLEN) |
1413 		    SM(pPacket->Endpoint,
1414 		       HTC_FRAME_HDR_ENDPOINTID));
1415 	LOCK_HTC_TX(target);
1416 
1417 	pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1418 	pEndpoint->SeqNo++;
1419 
1420 	HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
1421 		    SM(pPacket->PktInfo.AsTx.SeqNo,
1422 		       HTC_FRAME_HDR_CONTROLBYTES1));
1423 
1424 	UNLOCK_HTC_TX(target);
1425 
1426 	/*
1427 	 * For flow control enabled endpoints mapping is done in
1428 	 * htc_issue_packets and for non flow control enabled endpoints
1429 	 * its done here.
1430 	 */
1431 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1432 		pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
1433 		status = qdf_nbuf_map(target->osdev,
1434 				      GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
1435 				      QDF_DMA_TO_DEVICE);
1436 		if (status != QDF_STATUS_SUCCESS) {
1437 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1438 					("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
1439 					 __func__, pEndpoint, pEndpoint->SeqNo));
1440 			return status;
1441 		}
1442 	}
1443 
1444 	INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
1445 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1446 	if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
1447 		htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
1448 	else
1449 		htc_try_send(target, pEndpoint, &pPktQueue);
1450 #else
1451 	htc_try_send(target, pEndpoint, &pPktQueue);
1452 #endif
1453 
1454 	/* do completion on any packets that couldn't get in */
1455 	while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
1456 		pPacket = htc_packet_dequeue(&pPktQueue);
1457 
1458 		if (HTC_STOPPING(target))
1459 			pPacket->Status = QDF_STATUS_E_CANCELED;
1460 		else
1461 			pPacket->Status = QDF_STATUS_E_RESOURCES;
1462 
1463 		send_packet_completion(target, pPacket);
1464 	}
1465 
1466 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1467 
1468 	return QDF_STATUS_SUCCESS;
1469 }
1470 
1471 /* HTC API - htc_send_pkt */
1472 QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
1473 {
1474 	if (!htc_handle) {
1475 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1476 				("%s: HTCHandle is NULL \n", __func__));
1477 		return QDF_STATUS_E_FAILURE;
1478 	}
1479 
1480 	if (!htc_packet) {
1481 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1482 				("%s: pPacket is NULL \n", __func__));
1483 		return QDF_STATUS_E_FAILURE;
1484 	}
1485 
1486 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1487 			("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
1488 			 htc_packet->Endpoint, htc_packet->pBuffer,
1489 			 htc_packet->ActualLength));
1490 	return __htc_send_pkt(htc_handle, htc_packet);
1491 }
1492 qdf_export_symbol(htc_send_pkt);
1493 
1494 #ifdef ATH_11AC_TXCOMPACT
1495 /**
1496  * htc_send_data_pkt() - send single data packet on an endpoint
1497  * @HTCHandle: pointer to HTC handle
1498  * @netbuf: network buffer containing the data to be sent
1499  * @ActualLength: length of data that needs to be transmitted
1500  *
1501  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1502  */
1503 QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
1504 			     int actual_length)
1505 {
1506 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
1507 	HTC_ENDPOINT *pEndpoint;
1508 	HTC_FRAME_HDR *p_htc_hdr;
1509 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1510 	int tx_resources;
1511 	uint32_t data_attr = 0;
1512 	int htc_payload_len = actual_length;
1513 
1514 	pEndpoint = &target->endpoint[ep_id];
1515 
1516 	tx_resources = hif_get_free_queue_number(target->hif_dev,
1517 						 pEndpoint->UL_PipeID);
1518 
1519 	if (tx_resources < HTC_DATA_RESOURCE_THRS) {
1520 		if (pEndpoint->ul_is_polled) {
1521 			hif_send_complete_check(pEndpoint->target->hif_dev,
1522 						pEndpoint->UL_PipeID, 1);
1523 			tx_resources =
1524 				hif_get_free_queue_number(target->hif_dev,
1525 							  pEndpoint->UL_PipeID);
1526 		}
1527 		if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
1528 			return QDF_STATUS_E_FAILURE;
1529 	}
1530 
1531 	if (hif_pm_runtime_get(target->hif_dev))
1532 		return QDF_STATUS_E_FAILURE;
1533 
1534 	p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
1535 	AR_DEBUG_ASSERT(p_htc_hdr);
1536 
1537 	data_attr = qdf_nbuf_data_attr_get(netbuf);
1538 
1539 	if (target->htc_hdr_length_check)
1540 		htc_payload_len = actual_length - HTC_HEADER_LEN;
1541 
1542 	HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
1543 		    | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
1544 	/*
1545 	 * If the HIF pipe for the data endpoint is polled rather than
1546 	 * interrupt-driven, this is a good point to check whether any
1547 	 * data previously sent through the HIF pipe have finished being
1548 	 * sent.
1549 	 * Since this may result in callbacks to htc_tx_completion_handler,
1550 	 * which can take the HTC tx lock, make the hif_send_complete_check
1551 	 * call before acquiring the HTC tx lock.
1552 	 * Call hif_send_complete_check directly, rather than calling
1553 	 * htc_send_complete_check, and call the PollTimerStart separately
1554 	 * after calling hif_send_head, so the timer will be started to
1555 	 * check for completion of the new outstanding download (in the
1556 	 * unexpected event that other polling calls don't catch it).
1557 	 */
1558 
1559 	LOCK_HTC_TX(target);
1560 
1561 	HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
1562 		    SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
1563 
1564 	pEndpoint->SeqNo++;
1565 
1566 	QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1567 	DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1568 		QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1569 		sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1570 	status = hif_send_head(target->hif_dev,
1571 			       pEndpoint->UL_PipeID,
1572 			       pEndpoint->Id, actual_length, netbuf, data_attr);
1573 
1574 	UNLOCK_HTC_TX(target);
1575 	return status;
1576 }
1577 #else                           /*ATH_11AC_TXCOMPACT */
1578 
1579 /**
1580  * htc_send_data_pkt() - htc_send_data_pkt
1581  * @HTCHandle: pointer to HTC handle
1582  * @pPacket: pointer to HTC_PACKET
1583  * @more_data: indicates whether more data is to follow
1584  *
1585  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1586  */
1587 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
1588 			   uint8_t more_data)
1589 {
1590 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1591 	HTC_ENDPOINT *pEndpoint;
1592 	HTC_FRAME_HDR *pHtcHdr;
1593 	HTC_PACKET_QUEUE sendQueue;
1594 	qdf_nbuf_t netbuf = NULL;
1595 	int tx_resources;
1596 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1597 	uint32_t data_attr = 0;
1598 
1599 	if (pPacket) {
1600 		if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1601 		   (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1602 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1603 				("%s endpoint is invalid\n", __func__));
1604 			AR_DEBUG_ASSERT(0);
1605 			return QDF_STATUS_E_INVAL;
1606 		}
1607 		pEndpoint = &target->endpoint[pPacket->Endpoint];
1608 
1609 		/* add HTC_FRAME_HDR in the initial fragment */
1610 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1611 		pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
1612 		AR_DEBUG_ASSERT(pHtcHdr);
1613 
1614 		HTC_WRITE32(pHtcHdr,
1615 				SM(pPacket->ActualLength,
1616 				       HTC_FRAME_HDR_PAYLOADLEN) |
1617 				SM(pPacket->PktInfo.AsTx.SendFlags,
1618 					HTC_FRAME_HDR_FLAGS) |
1619 				SM(pPacket->Endpoint,
1620 					HTC_FRAME_HDR_ENDPOINTID));
1621 		/*
1622 		 * If the HIF pipe for the data endpoint is polled rather than
1623 		 * interrupt-driven, this is a good point to check whether any
1624 		 * data previously sent through the HIF pipe have finished being
1625 		 * sent. Since this may result in callbacks to
1626 		 * htc_tx_completion_handler, which can take the HTC tx lock,
1627 		 * make the hif_send_complete_check call before acquiring the
1628 		 * HTC tx lock.
1629 		 * Call hif_send_complete_check directly, rather than calling
1630 		 * htc_send_complete_check, and call the PollTimerStart
1631 		 * separately after calling hif_send_head, so the timer will be
1632 		 * started to check for completion of the new outstanding
1633 		 * download (in the unexpected event that other polling calls
1634 		 * don't catch it).
1635 		 */
1636 		if (pEndpoint->ul_is_polled) {
1637 			htc_send_complete_poll_timer_stop(pEndpoint);
1638 			hif_send_complete_check(pEndpoint->target->hif_dev,
1639 						pEndpoint->UL_PipeID, 0);
1640 		}
1641 
1642 		LOCK_HTC_TX(target);
1643 
1644 		pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1645 		pEndpoint->SeqNo++;
1646 
1647 		HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
1648 			    SM(pPacket->PktInfo.AsTx.SeqNo,
1649 			       HTC_FRAME_HDR_CONTROLBYTES1));
1650 
1651 		/* append new packet to pEndpoint->TxQueue */
1652 		HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
1653 		if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
1654 			UNLOCK_HTC_TX(target);
1655 			return QDF_STATUS_SUCCESS;
1656 		}
1657 
1658 		QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1659 		DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1660 			  QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1661 				sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1662 	} else {
1663 		LOCK_HTC_TX(target);
1664 		pEndpoint = &target->endpoint[1];
1665 	}
1666 
1667 	/* increment tx processing count on entry */
1668 	qdf_atomic_inc(&pEndpoint->TxProcessCount);
1669 	if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
1670 		/*
1671 		 * Another thread or task is draining the TX queues on this
1672 		 * endpoint. That thread will reset the tx processing count when
1673 		 * the queue is drained.
1674 		 */
1675 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1676 		UNLOCK_HTC_TX(target);
1677 		return QDF_STATUS_SUCCESS;
1678 	}
1679 
1680 	/***** beyond this point only 1 thread may enter ******/
1681 
1682 	INIT_HTC_PACKET_QUEUE(&sendQueue);
1683 	if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1684 #if DEBUG_CREDIT
1685 		int cred = pEndpoint->TxCredits;
1686 #endif
1687 		get_htc_send_packets_credit_based(target, pEndpoint,
1688 						 &sendQueue);
1689 #if DEBUG_CREDIT
1690 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
1691 			if (cred - pEndpoint->TxCredits > 0) {
1692 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1693 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1694 						 pEndpoint->Id, cred,
1695 						 cred - pEndpoint->TxCredits,
1696 						 pEndpoint->TxCredits));
1697 			}
1698 		}
1699 #endif
1700 		UNLOCK_HTC_TX(target);
1701 	}
1702 
1703 	else if (HTC_TX_BUNDLE_ENABLED(target)) {
1704 
1705 		if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) &&
1706 			hif_get_free_queue_number(target->hif_dev,
1707 						pEndpoint->UL_PipeID)) {
1708 			/*
1709 			 * Header and payload belongs to the different fragments
1710 			 * and consume 2 resource for one HTC package but USB
1711 			 * combine into one transfer.
1712 			 */
1713 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1714 				(HTC_MAX_MSG_PER_BUNDLE_TX * 2));
1715 		} else {
1716 		/* Dequeue max packets from endpoint tx queue */
1717 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1718 				     HTC_MAX_TX_BUNDLE_SEND_LIMIT);
1719 		}
1720 
1721 		UNLOCK_HTC_TX(target);
1722 	} else {
1723 		/*
1724 		 * Now drain the endpoint TX queue for transmission as long as
1725 		 * we have enough transmit resources
1726 		 */
1727 		tx_resources =
1728 			hif_get_free_queue_number(target->hif_dev,
1729 						  pEndpoint->UL_PipeID);
1730 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1731 				     tx_resources);
1732 		UNLOCK_HTC_TX(target);
1733 	}
1734 
1735 	/* send what we can */
1736 	while (true) {
1737 		if (HTC_TX_BUNDLE_ENABLED(target) &&
1738 		    (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
1739 		     HTC_MIN_MSG_PER_BUNDLE) &&
1740 		    (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
1741 		     hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
1742 			htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
1743 		}
1744 		pPacket = htc_packet_dequeue(&sendQueue);
1745 		if (pPacket == NULL)
1746 			break;
1747 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1748 
1749 		LOCK_HTC_TX(target);
1750 		/* store in look up queue to match completions */
1751 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
1752 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
1753 		pEndpoint->ul_outstanding_cnt++;
1754 		UNLOCK_HTC_TX(target);
1755 
1756 		status = hif_send_head(target->hif_dev,
1757 				       pEndpoint->UL_PipeID,
1758 				       pEndpoint->Id,
1759 				       HTC_HDR_LENGTH + pPacket->ActualLength,
1760 				       netbuf, data_attr);
1761 #if DEBUG_BUNDLE
1762 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
1763 			  pEndpoint->Id,
1764 			  pEndpoint->TxCreditSize,
1765 			  HTC_HDR_LENGTH + pPacket->ActualLength);
1766 #endif
1767 
1768 		htc_issue_tx_bundle_stats_inc(target);
1769 
1770 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
1771 			LOCK_HTC_TX(target);
1772 			pEndpoint->ul_outstanding_cnt--;
1773 			/* remove this packet from the tx completion queue */
1774 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
1775 
1776 			/*
1777 			 * Don't bother reclaiming credits - HTC flow control
1778 			 * is not applicable to tx data.
1779 			 * In LL systems, there is no download flow control,
1780 			 * since there's virtually no download delay.
1781 			 * In HL systems, the txrx SW explicitly performs the
1782 			 * tx flow control.
1783 			 */
1784 			/* pEndpoint->TxCredits +=
1785 			 * pPacket->PktInfo.AsTx.CreditsUsed;
1786 			 */
1787 
1788 			/* put this frame back at the front of the sendQueue */
1789 			HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
1790 
1791 			/* put the sendQueue back at the front of
1792 			 * pEndpoint->TxQueue
1793 			 */
1794 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1795 							  &sendQueue);
1796 			UNLOCK_HTC_TX(target);
1797 			break;  /* still need to reset TxProcessCount */
1798 		}
1799 	}
1800 	/* done with this endpoint, we can clear the count */
1801 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1802 
1803 	if (pEndpoint->ul_is_polled) {
1804 		/*
1805 		 * Start a cleanup timer to poll for download completion.
1806 		 * The download completion should be noticed promptly from
1807 		 * other polling calls, but the timer provides a safety net
1808 		 * in case other polling calls don't occur as expected.
1809 		 */
1810 		htc_send_complete_poll_timer_start(pEndpoint);
1811 	}
1812 
1813 	return status;
1814 }
1815 #endif /*ATH_11AC_TXCOMPACT */
1816 qdf_export_symbol(htc_send_data_pkt);
1817 
1818 /*
1819  * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
1820  * since upper layers expects HTC_PACKET containers we use the completed netbuf
1821  * and lookup its corresponding HTC packet buffer from a lookup list.
1822  * This is extra overhead that can be fixed by re-aligning HIF interfaces
1823  * with HTC.
1824  *
1825  */
1826 static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
1827 					HTC_ENDPOINT *pEndpoint,
1828 					qdf_nbuf_t netbuf)
1829 {
1830 	HTC_PACKET *pPacket = NULL;
1831 	HTC_PACKET *pFoundPacket = NULL;
1832 	HTC_PACKET_QUEUE lookupQueue;
1833 
1834 	INIT_HTC_PACKET_QUEUE(&lookupQueue);
1835 	LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1836 
1837 	LOCK_HTC_TX(target);
1838 
1839 	/* mark that HIF has indicated the send complete for another packet */
1840 	pEndpoint->ul_outstanding_cnt--;
1841 
1842 	/* Dequeue first packet directly because of in-order completion */
1843 	pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
1844 	if (qdf_unlikely(!pPacket)) {
1845 		UNLOCK_HTC_TX(target);
1846 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1847 		return NULL;
1848 	}
1849 	if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1850 		UNLOCK_HTC_TX(target);
1851 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1852 		return pPacket;
1853 	}
1854 	HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
1855 
1856 	/*
1857 	 * Move TX lookup queue to temp queue because most of packets that are
1858 	 * not index 0 are not top 10 packets.
1859 	 */
1860 	HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
1861 					  &pEndpoint->TxLookupQueue);
1862 
1863 	ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
1864 				       HTC_PACKET, ListLink) {
1865 
1866 		if (NULL == pPacket) {
1867 			pFoundPacket = pPacket;
1868 			break;
1869 		}
1870 		/* check for removal */
1871 		if (netbuf ==
1872 		    (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1873 			/* found it */
1874 			HTC_PACKET_REMOVE(&lookupQueue, pPacket);
1875 			pFoundPacket = pPacket;
1876 			break;
1877 		}
1878 
1879 	}
1880 	ITERATE_END;
1881 
1882 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
1883 					  &lookupQueue);
1884 	UNLOCK_HTC_TX(target);
1885 	UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1886 
1887 	return pFoundPacket;
1888 }
1889 
1890 /**
1891  * htc_tx_completion_handler() - htc tx completion handler
1892  * @Context: pointer to HTC_TARGET structure
1893  * @netbuf: pointer to netbuf for which completion handler is being called
1894  * @EpID: end point Id on which the packet was sent
1895  * @toeplitz_hash_result: toeplitz hash result
1896  *
1897  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1898  */
1899 QDF_STATUS htc_tx_completion_handler(void *Context,
1900 				   qdf_nbuf_t netbuf, unsigned int EpID,
1901 				   uint32_t toeplitz_hash_result)
1902 {
1903 	HTC_TARGET *target = (HTC_TARGET *) Context;
1904 	HTC_ENDPOINT *pEndpoint;
1905 	HTC_PACKET *pPacket;
1906 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1907 	HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
1908 		ENDPOINT_2, ENDPOINT_3 };
1909 	int epidIdx;
1910 	uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
1911 	uint16_t resources;
1912 	uint16_t resourcesMax;
1913 #endif
1914 
1915 	pEndpoint = &target->endpoint[EpID];
1916 	target->TX_comp_cnt++;
1917 
1918 	do {
1919 		pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
1920 		if (NULL == pPacket) {
1921 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1922 					("HTC TX lookup failed!\n"));
1923 			/* may have already been flushed and freed */
1924 			netbuf = NULL;
1925 			break;
1926 		}
1927 		if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
1928 			hif_pm_runtime_put(target->hif_dev);
1929 
1930 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
1931 			HTC_PACKET *pPacketTemp;
1932 			HTC_PACKET_QUEUE *pQueueSave =
1933 				(HTC_PACKET_QUEUE *) pPacket->pContext;
1934 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
1935 							      pPacketTemp) {
1936 				pPacket->Status = QDF_STATUS_SUCCESS;
1937 				send_packet_completion(target, pPacketTemp);
1938 			}
1939 			HTC_PACKET_QUEUE_ITERATE_END;
1940 			free_htc_bundle_packet(target, pPacket);
1941 
1942 			if (hif_get_bus_type(target->hif_dev) ==
1943 					     QDF_BUS_TYPE_USB) {
1944 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
1945 					htc_try_send(target, pEndpoint, NULL);
1946 			}
1947 
1948 			return QDF_STATUS_SUCCESS;
1949 		}
1950 		/* will be giving this buffer back to upper layers */
1951 		netbuf = NULL;
1952 		pPacket->Status = QDF_STATUS_SUCCESS;
1953 		send_packet_completion(target, pPacket);
1954 
1955 	} while (false);
1956 
1957 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1958 		/* note: when using TX credit flow, the re-checking of queues
1959 		 * happens when credits flow back from the target. In the non-TX
1960 		 * credit case, we recheck after the packet completes
1961 		 */
1962 		if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
1963 				(!pEndpoint->async_update)) {
1964 			htc_try_send(target, pEndpoint, NULL);
1965 		}
1966 	}
1967 
1968 	return QDF_STATUS_SUCCESS;
1969 }
1970 
1971 #ifdef WLAN_FEATURE_FASTPATH
1972 /**
1973  * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
1974  * @HTC_HANDLE : pointer to the htc target context
1975  * @htc_ep_id  : end point id
1976  *
1977  * checks HTC tx completion
1978  *
1979  * Return: none
1980  */
1981 void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
1982 {
1983 	HTC_TARGET      *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
1984 	HTC_ENDPOINT    *pendpoint = &target->endpoint[htc_ep_id];
1985 
1986 	htc_send_complete_check(pendpoint, 1);
1987 }
1988 qdf_export_symbol(htc_ctrl_msg_cmpl);
1989 #endif
1990 
1991 /* callback when TX resources become available */
1992 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
1993 {
1994 	int i;
1995 	HTC_TARGET *target = (HTC_TARGET *) context;
1996 	HTC_ENDPOINT *pEndpoint = NULL;
1997 
1998 	for (i = 0; i < ENDPOINT_MAX; i++) {
1999 		pEndpoint = &target->endpoint[i];
2000 		if (pEndpoint->service_id != 0) {
2001 			if (pEndpoint->UL_PipeID == pipeID)
2002 				break;
2003 		}
2004 	}
2005 
2006 	if (i >= ENDPOINT_MAX) {
2007 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2008 				("Invalid pipe indicated for TX resource avail : %d!\n",
2009 				 pipeID));
2010 		return;
2011 	}
2012 
2013 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2014 			("HIF indicated more resources for pipe:%d\n",
2015 			 pipeID));
2016 
2017 	htc_try_send(target, pEndpoint, NULL);
2018 }
2019 
2020 #ifdef FEATURE_RUNTIME_PM
2021 /**
2022  * htc_kick_queues(): resumes tx transactions of suspended endpoints
2023  * @context: pointer to the htc target context
2024  *
2025  * Iterates through the enpoints and provides a context to empty queues
2026  * int the hif layer when they are stalled due to runtime suspend.
2027  *
2028  * Return: none
2029  */
2030 void htc_kick_queues(void *context)
2031 {
2032 	int i;
2033 	HTC_TARGET *target = (HTC_TARGET *)context;
2034 	HTC_ENDPOINT *endpoint = NULL;
2035 
2036 	for (i = 0; i < ENDPOINT_MAX; i++) {
2037 		endpoint = &target->endpoint[i];
2038 
2039 		if (endpoint->service_id == 0)
2040 			continue;
2041 
2042 		if (endpoint->EpCallBacks.ep_resume_tx_queue)
2043 			endpoint->EpCallBacks.ep_resume_tx_queue(
2044 					endpoint->EpCallBacks.pContext);
2045 
2046 		htc_try_send(target, endpoint, NULL);
2047 	}
2048 
2049 	hif_fastpath_resume(target->hif_dev);
2050 }
2051 #endif
2052 
2053 /* flush endpoint TX queue */
2054 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
2055 			   HTC_TX_TAG Tag)
2056 {
2057 	HTC_PACKET *pPacket;
2058 
2059 	LOCK_HTC_TX(target);
2060 	while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2061 		pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
2062 
2063 		if (pPacket) {
2064 			/* let the sender know the packet was not delivered */
2065 			pPacket->Status = QDF_STATUS_E_CANCELED;
2066 			send_packet_completion(target, pPacket);
2067 		}
2068 	}
2069 	UNLOCK_HTC_TX(target);
2070 }
2071 
2072 /* flush pending entries in endpoint TX Lookup queue */
2073 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
2074 				  HTC_ENDPOINT_ID endpoint_id,
2075 				  bool call_ep_callback)
2076 {
2077 	HTC_PACKET *packet;
2078 	HTC_ENDPOINT *endpoint;
2079 
2080 	endpoint = &target->endpoint[endpoint_id];
2081 
2082 	if (!endpoint && endpoint->service_id == 0)
2083 		return;
2084 
2085 	LOCK_HTC_TX(target);
2086 	while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
2087 		packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
2088 
2089 		if (packet) {
2090 			if (call_ep_callback == true) {
2091 				packet->Status = QDF_STATUS_E_CANCELED;
2092 				send_packet_completion(target, packet);
2093 			} else {
2094 				qdf_mem_free(packet);
2095 			}
2096 		}
2097 	}
2098 	UNLOCK_HTC_TX(target);
2099 }
2100 
2101 /* HTC API to flush an endpoint's TX queue*/
2102 void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
2103 			HTC_TX_TAG Tag)
2104 {
2105 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2106 	HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
2107 
2108 	if (pEndpoint->service_id == 0) {
2109 		AR_DEBUG_ASSERT(false);
2110 		/* not in use.. */
2111 		return;
2112 	}
2113 
2114 	htc_flush_endpoint_tx(target, pEndpoint, Tag);
2115 }
2116 
2117 /* HTC API to indicate activity to the credit distribution function */
2118 void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
2119 				  HTC_ENDPOINT_ID Endpoint, bool Active)
2120 {
2121 	/*  TODO */
2122 }
2123 
2124 bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
2125 {
2126 	return true;
2127 }
2128 
2129 void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
2130 {
2131 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2132 
2133 	target->is_nodrop_pkt = isNodropPkt;
2134 }
2135 
2136 void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
2137 {
2138 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
2139 
2140 	target->htc_hdr_length_check = htc_hdr_length_check;
2141 }
2142 
2143 /**
2144  * htc_process_credit_rpt() - process credit report, call distribution function
2145  * @target: pointer to HTC_TARGET
2146  * @pRpt: pointer to HTC_CREDIT_REPORT
2147  * @NumEntries: number of entries in credit report
2148  * @FromEndpoint: endpoint for which  credit report is received
2149  *
2150  * Return: A_OK for success or an appropriate A_STATUS error
2151  */
2152 void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
2153 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
2154 {
2155 	int i;
2156 	HTC_ENDPOINT *pEndpoint;
2157 	int totalCredits = 0;
2158 	uint8_t rpt_credits, rpt_ep_id;
2159 
2160 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2161 			("+htc_process_credit_rpt, Credit Report Entries:%d\n",
2162 			 NumEntries));
2163 
2164 	/* lock out TX while we update credits */
2165 	LOCK_HTC_TX(target);
2166 
2167 	for (i = 0; i < NumEntries; i++, pRpt++) {
2168 
2169 		rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
2170 
2171 		if (rpt_ep_id >= ENDPOINT_MAX) {
2172 			AR_DEBUG_ASSERT(false);
2173 			break;
2174 		}
2175 
2176 		rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
2177 
2178 		pEndpoint = &target->endpoint[rpt_ep_id];
2179 #if DEBUG_CREDIT
2180 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2181 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2182 					(" <HTC> Increase EP%d %d + %d = %d credits\n",
2183 					 rpt_ep_id, pEndpoint->TxCredits,
2184 					 rpt_credits,
2185 					 pEndpoint->TxCredits + rpt_credits));
2186 		}
2187 #endif
2188 
2189 #ifdef HTC_EP_STAT_PROFILING
2190 
2191 		INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
2192 		INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
2193 
2194 		if (FromEndpoint == rpt_ep_id) {
2195 			/* this credit report arrived on the same endpoint
2196 			 * indicating it arrived in an RX packet
2197 			 */
2198 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
2199 					rpt_credits);
2200 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
2201 		} else if (FromEndpoint == ENDPOINT_0) {
2202 			/* this credit arrived on endpoint 0 as a NULL msg */
2203 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
2204 					rpt_credits);
2205 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
2206 		} else {
2207 			/* arrived on another endpoint */
2208 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
2209 					rpt_credits);
2210 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
2211 		}
2212 
2213 #endif
2214 
2215 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
2216 			htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
2217 					  pEndpoint->TxCredits + rpt_credits,
2218 					  HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
2219 							TxQueue));
2220 		}
2221 
2222 		pEndpoint->TxCredits += rpt_credits;
2223 
2224 		if (pEndpoint->TxCredits
2225 		    && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2226 			UNLOCK_HTC_TX(target);
2227 #ifdef ATH_11AC_TXCOMPACT
2228 			htc_try_send(target, pEndpoint, NULL);
2229 #else
2230 			if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
2231 				htc_send_data_pkt(target, NULL, 0);
2232 			else
2233 				htc_try_send(target, pEndpoint, NULL);
2234 #endif
2235 			LOCK_HTC_TX(target);
2236 		}
2237 		totalCredits += rpt_credits;
2238 	}
2239 
2240 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2241 			("  Report indicated %d credits to distribute\n",
2242 			 totalCredits));
2243 
2244 	UNLOCK_HTC_TX(target);
2245 
2246 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
2247 }
2248 
2249 /* function to fetch stats from htc layer*/
2250 struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
2251 {
2252 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2253 
2254 	return &(target->htc_pkt_stats);
2255 }
2256