xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_send.c (revision a175314c51a4ce5cec2835cc8a8c7dc0c1810915)
1 /*
2  * Copyright (c) 2013-2018 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.\n",
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).\n", __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.\n",
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 *pHtcHdr;
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 
1401 	qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
1402 	/* setup HTC frame header */
1403 	pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
1404 	AR_DEBUG_ASSERT(pHtcHdr);
1405 	HTC_WRITE32(pHtcHdr,
1406 		    SM(pPacket->ActualLength,
1407 		       HTC_FRAME_HDR_PAYLOADLEN) |
1408 		    SM(pPacket->Endpoint,
1409 		       HTC_FRAME_HDR_ENDPOINTID));
1410 	LOCK_HTC_TX(target);
1411 
1412 	pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1413 	pEndpoint->SeqNo++;
1414 
1415 	HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
1416 		    SM(pPacket->PktInfo.AsTx.SeqNo,
1417 		       HTC_FRAME_HDR_CONTROLBYTES1));
1418 
1419 	UNLOCK_HTC_TX(target);
1420 
1421 	/*
1422 	 * For flow control enabled endpoints mapping is done in
1423 	 * htc_issue_packets and for non flow control enabled endpoints
1424 	 * its done here.
1425 	 */
1426 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1427 		pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
1428 		status = qdf_nbuf_map(target->osdev,
1429 				      GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
1430 				      QDF_DMA_TO_DEVICE);
1431 		if (status != QDF_STATUS_SUCCESS) {
1432 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1433 					("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
1434 					 __func__, pEndpoint, pEndpoint->SeqNo));
1435 			return status;
1436 		}
1437 	}
1438 
1439 	INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
1440 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1441 	if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
1442 		htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
1443 	else
1444 		htc_try_send(target, pEndpoint, &pPktQueue);
1445 #else
1446 	htc_try_send(target, pEndpoint, &pPktQueue);
1447 #endif
1448 
1449 	/* do completion on any packets that couldn't get in */
1450 	while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
1451 		pPacket = htc_packet_dequeue(&pPktQueue);
1452 
1453 		if (HTC_STOPPING(target))
1454 			pPacket->Status = QDF_STATUS_E_CANCELED;
1455 		else
1456 			pPacket->Status = QDF_STATUS_E_RESOURCES;
1457 
1458 		send_packet_completion(target, pPacket);
1459 	}
1460 
1461 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
1462 
1463 	return QDF_STATUS_SUCCESS;
1464 }
1465 
1466 /* HTC API - htc_send_pkt */
1467 QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
1468 {
1469 	if (!htc_handle) {
1470 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1471 				("%s: HTCHandle is NULL \n", __func__));
1472 		return QDF_STATUS_E_FAILURE;
1473 	}
1474 
1475 	if (!htc_packet) {
1476 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1477 				("%s: pPacket is NULL \n", __func__));
1478 		return QDF_STATUS_E_FAILURE;
1479 	}
1480 
1481 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1482 			("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
1483 			 htc_packet->Endpoint, htc_packet->pBuffer,
1484 			 htc_packet->ActualLength));
1485 	return __htc_send_pkt(htc_handle, htc_packet);
1486 }
1487 qdf_export_symbol(htc_send_pkt);
1488 
1489 #ifdef ATH_11AC_TXCOMPACT
1490 /**
1491  * htc_send_data_pkt() - send single data packet on an endpoint
1492  * @HTCHandle: pointer to HTC handle
1493  * @netbuf: network buffer containing the data to be sent
1494  * @ActualLength: length of data that needs to be transmitted
1495  *
1496  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1497  */
1498 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, qdf_nbuf_t netbuf, int Epid,
1499 			   int ActualLength)
1500 {
1501 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1502 	HTC_ENDPOINT *pEndpoint;
1503 	HTC_FRAME_HDR *pHtcHdr;
1504 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1505 	int tx_resources;
1506 	uint32_t data_attr = 0;
1507 
1508 	pEndpoint = &target->endpoint[Epid];
1509 
1510 	tx_resources = hif_get_free_queue_number(target->hif_dev,
1511 						 pEndpoint->UL_PipeID);
1512 
1513 	if (tx_resources < HTC_DATA_RESOURCE_THRS) {
1514 		if (pEndpoint->ul_is_polled) {
1515 			hif_send_complete_check(pEndpoint->target->hif_dev,
1516 						pEndpoint->UL_PipeID, 1);
1517 			tx_resources =
1518 				hif_get_free_queue_number(target->hif_dev,
1519 							  pEndpoint->UL_PipeID);
1520 		}
1521 		if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
1522 			return QDF_STATUS_E_FAILURE;
1523 	}
1524 
1525 	if (hif_pm_runtime_get(target->hif_dev))
1526 		return QDF_STATUS_E_FAILURE;
1527 
1528 	pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
1529 	AR_DEBUG_ASSERT(pHtcHdr);
1530 
1531 	data_attr = qdf_nbuf_data_attr_get(netbuf);
1532 
1533 	HTC_WRITE32(pHtcHdr, SM(ActualLength, HTC_FRAME_HDR_PAYLOADLEN) |
1534 		    SM(Epid, HTC_FRAME_HDR_ENDPOINTID));
1535 	/*
1536 	 * If the HIF pipe for the data endpoint is polled rather than
1537 	 * interrupt-driven, this is a good point to check whether any
1538 	 * data previously sent through the HIF pipe have finished being
1539 	 * sent.
1540 	 * Since this may result in callbacks to htc_tx_completion_handler,
1541 	 * which can take the HTC tx lock, make the hif_send_complete_check
1542 	 * call before acquiring the HTC tx lock.
1543 	 * Call hif_send_complete_check directly, rather than calling
1544 	 * htc_send_complete_check, and call the PollTimerStart separately
1545 	 * after calling hif_send_head, so the timer will be started to
1546 	 * check for completion of the new outstanding download (in the
1547 	 * unexpected event that other polling calls don't catch it).
1548 	 */
1549 
1550 	LOCK_HTC_TX(target);
1551 
1552 	HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
1553 		    SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
1554 
1555 	pEndpoint->SeqNo++;
1556 
1557 	QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1558 	DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1559 		QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1560 		sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1561 	status = hif_send_head(target->hif_dev,
1562 			       pEndpoint->UL_PipeID,
1563 			       pEndpoint->Id, ActualLength, netbuf, data_attr);
1564 
1565 	UNLOCK_HTC_TX(target);
1566 	return status;
1567 }
1568 #else                           /*ATH_11AC_TXCOMPACT */
1569 
1570 /**
1571  * htc_send_data_pkt() - htc_send_data_pkt
1572  * @HTCHandle: pointer to HTC handle
1573  * @pPacket: pointer to HTC_PACKET
1574  * @more_data: indicates whether more data is to follow
1575  *
1576  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1577  */
1578 QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
1579 			   uint8_t more_data)
1580 {
1581 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1582 	HTC_ENDPOINT *pEndpoint;
1583 	HTC_FRAME_HDR *pHtcHdr;
1584 	HTC_PACKET_QUEUE sendQueue;
1585 	qdf_nbuf_t netbuf = NULL;
1586 	int tx_resources;
1587 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1588 	uint32_t data_attr = 0;
1589 
1590 	if (pPacket) {
1591 		if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
1592 		   (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
1593 			AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
1594 				("%s endpoint is invalid\n", __func__));
1595 			AR_DEBUG_ASSERT(0);
1596 			return QDF_STATUS_E_INVAL;
1597 		}
1598 		pEndpoint = &target->endpoint[pPacket->Endpoint];
1599 
1600 		/* add HTC_FRAME_HDR in the initial fragment */
1601 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1602 		pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
1603 		AR_DEBUG_ASSERT(pHtcHdr);
1604 
1605 		HTC_WRITE32(pHtcHdr,
1606 				SM(pPacket->ActualLength,
1607 				       HTC_FRAME_HDR_PAYLOADLEN) |
1608 				SM(pPacket->PktInfo.AsTx.SendFlags,
1609 					HTC_FRAME_HDR_FLAGS) |
1610 				SM(pPacket->Endpoint,
1611 					HTC_FRAME_HDR_ENDPOINTID));
1612 		/*
1613 		 * If the HIF pipe for the data endpoint is polled rather than
1614 		 * interrupt-driven, this is a good point to check whether any
1615 		 * data previously sent through the HIF pipe have finished being
1616 		 * sent. Since this may result in callbacks to
1617 		 * htc_tx_completion_handler, which can take the HTC tx lock,
1618 		 * make the hif_send_complete_check call before acquiring the
1619 		 * HTC tx lock.
1620 		 * Call hif_send_complete_check directly, rather than calling
1621 		 * htc_send_complete_check, and call the PollTimerStart
1622 		 * separately after calling hif_send_head, so the timer will be
1623 		 * started to check for completion of the new outstanding
1624 		 * download (in the unexpected event that other polling calls
1625 		 * don't catch it).
1626 		 */
1627 		if (pEndpoint->ul_is_polled) {
1628 			htc_send_complete_poll_timer_stop(pEndpoint);
1629 			hif_send_complete_check(pEndpoint->target->hif_dev,
1630 						pEndpoint->UL_PipeID, 0);
1631 		}
1632 
1633 		LOCK_HTC_TX(target);
1634 
1635 		pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
1636 		pEndpoint->SeqNo++;
1637 
1638 		HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
1639 			    SM(pPacket->PktInfo.AsTx.SeqNo,
1640 			       HTC_FRAME_HDR_CONTROLBYTES1));
1641 
1642 		/* append new packet to pEndpoint->TxQueue */
1643 		HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
1644 		if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
1645 			UNLOCK_HTC_TX(target);
1646 			return QDF_STATUS_SUCCESS;
1647 		}
1648 
1649 		QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
1650 		DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
1651 			  QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
1652 				sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
1653 	} else {
1654 		LOCK_HTC_TX(target);
1655 		pEndpoint = &target->endpoint[1];
1656 	}
1657 
1658 	/* increment tx processing count on entry */
1659 	qdf_atomic_inc(&pEndpoint->TxProcessCount);
1660 	if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
1661 		/*
1662 		 * Another thread or task is draining the TX queues on this
1663 		 * endpoint. That thread will reset the tx processing count when
1664 		 * the queue is drained.
1665 		 */
1666 		qdf_atomic_dec(&pEndpoint->TxProcessCount);
1667 		UNLOCK_HTC_TX(target);
1668 		return QDF_STATUS_SUCCESS;
1669 	}
1670 
1671 	/***** beyond this point only 1 thread may enter ******/
1672 
1673 	INIT_HTC_PACKET_QUEUE(&sendQueue);
1674 	if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1675 #if DEBUG_CREDIT
1676 		int cred = pEndpoint->TxCredits;
1677 #endif
1678 		get_htc_send_packets_credit_based(target, pEndpoint,
1679 						 &sendQueue);
1680 #if DEBUG_CREDIT
1681 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
1682 			if (cred - pEndpoint->TxCredits > 0) {
1683 				AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1684 						(" <HTC> Decrease EP%d %d - %d = %d credits.\n",
1685 						 pEndpoint->Id, cred,
1686 						 cred - pEndpoint->TxCredits,
1687 						 pEndpoint->TxCredits));
1688 			}
1689 		}
1690 #endif
1691 		UNLOCK_HTC_TX(target);
1692 	}
1693 
1694 	else if (HTC_TX_BUNDLE_ENABLED(target)) {
1695 
1696 		if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) &&
1697 			hif_get_free_queue_number(target->hif_dev,
1698 						pEndpoint->UL_PipeID)) {
1699 			/*
1700 			 * Header and payload belongs to the different fragments
1701 			 * and consume 2 resource for one HTC package but USB
1702 			 * combine into one transfer.
1703 			 */
1704 			get_htc_send_packets(target, pEndpoint, &sendQueue,
1705 				(HTC_MAX_MSG_PER_BUNDLE_TX * 2));
1706 		} else {
1707 		/* Dequeue max packets from endpoint tx queue */
1708 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1709 				     HTC_MAX_TX_BUNDLE_SEND_LIMIT);
1710 		}
1711 
1712 		UNLOCK_HTC_TX(target);
1713 	} else {
1714 		/*
1715 		 * Now drain the endpoint TX queue for transmission as long as
1716 		 * we have enough transmit resources
1717 		 */
1718 		tx_resources =
1719 			hif_get_free_queue_number(target->hif_dev,
1720 						  pEndpoint->UL_PipeID);
1721 		get_htc_send_packets(target, pEndpoint, &sendQueue,
1722 				     tx_resources);
1723 		UNLOCK_HTC_TX(target);
1724 	}
1725 
1726 	/* send what we can */
1727 	while (true) {
1728 		if (HTC_TX_BUNDLE_ENABLED(target) &&
1729 		    (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
1730 		     HTC_MIN_MSG_PER_BUNDLE) &&
1731 		    (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
1732 		     hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
1733 			htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
1734 		}
1735 		pPacket = htc_packet_dequeue(&sendQueue);
1736 		if (pPacket == NULL)
1737 			break;
1738 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
1739 
1740 		LOCK_HTC_TX(target);
1741 		/* store in look up queue to match completions */
1742 		HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
1743 		INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
1744 		pEndpoint->ul_outstanding_cnt++;
1745 		UNLOCK_HTC_TX(target);
1746 
1747 		status = hif_send_head(target->hif_dev,
1748 				       pEndpoint->UL_PipeID,
1749 				       pEndpoint->Id,
1750 				       HTC_HDR_LENGTH + pPacket->ActualLength,
1751 				       netbuf, data_attr);
1752 #if DEBUG_BUNDLE
1753 		qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
1754 			  pEndpoint->Id,
1755 			  pEndpoint->TxCreditSize,
1756 			  HTC_HDR_LENGTH + pPacket->ActualLength);
1757 #endif
1758 
1759 		htc_issue_tx_bundle_stats_inc(target);
1760 
1761 		if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
1762 			LOCK_HTC_TX(target);
1763 			pEndpoint->ul_outstanding_cnt--;
1764 			/* remove this packet from the tx completion queue */
1765 			HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
1766 
1767 			/*
1768 			 * Don't bother reclaiming credits - HTC flow control
1769 			 * is not applicable to tx data.
1770 			 * In LL systems, there is no download flow control,
1771 			 * since there's virtually no download delay.
1772 			 * In HL systems, the txrx SW explicitly performs the
1773 			 * tx flow control.
1774 			 */
1775 			/* pEndpoint->TxCredits +=
1776 			 * pPacket->PktInfo.AsTx.CreditsUsed;
1777 			 */
1778 
1779 			/* put this frame back at the front of the sendQueue */
1780 			HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
1781 
1782 			/* put the sendQueue back at the front of
1783 			 * pEndpoint->TxQueue
1784 			 */
1785 			HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
1786 							  &sendQueue);
1787 			UNLOCK_HTC_TX(target);
1788 			break;  /* still need to reset TxProcessCount */
1789 		}
1790 	}
1791 	/* done with this endpoint, we can clear the count */
1792 	qdf_atomic_init(&pEndpoint->TxProcessCount);
1793 
1794 	if (pEndpoint->ul_is_polled) {
1795 		/*
1796 		 * Start a cleanup timer to poll for download completion.
1797 		 * The download completion should be noticed promptly from
1798 		 * other polling calls, but the timer provides a safety net
1799 		 * in case other polling calls don't occur as expected.
1800 		 */
1801 		htc_send_complete_poll_timer_start(pEndpoint);
1802 	}
1803 
1804 	return status;
1805 }
1806 #endif /*ATH_11AC_TXCOMPACT */
1807 qdf_export_symbol(htc_send_data_pkt);
1808 
1809 /*
1810  * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
1811  * since upper layers expects HTC_PACKET containers we use the completed netbuf
1812  * and lookup its corresponding HTC packet buffer from a lookup list.
1813  * This is extra overhead that can be fixed by re-aligning HIF interfaces
1814  * with HTC.
1815  *
1816  */
1817 static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
1818 					HTC_ENDPOINT *pEndpoint,
1819 					qdf_nbuf_t netbuf)
1820 {
1821 	HTC_PACKET *pPacket = NULL;
1822 	HTC_PACKET *pFoundPacket = NULL;
1823 	HTC_PACKET_QUEUE lookupQueue;
1824 
1825 	INIT_HTC_PACKET_QUEUE(&lookupQueue);
1826 	LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1827 
1828 	LOCK_HTC_TX(target);
1829 
1830 	/* mark that HIF has indicated the send complete for another packet */
1831 	pEndpoint->ul_outstanding_cnt--;
1832 
1833 	/* Dequeue first packet directly because of in-order completion */
1834 	pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
1835 	if (qdf_unlikely(!pPacket)) {
1836 		UNLOCK_HTC_TX(target);
1837 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1838 		return NULL;
1839 	}
1840 	if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1841 		UNLOCK_HTC_TX(target);
1842 		UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1843 		return pPacket;
1844 	}
1845 	HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
1846 
1847 	/*
1848 	 * Move TX lookup queue to temp queue because most of packets that are
1849 	 * not index 0 are not top 10 packets.
1850 	 */
1851 	HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
1852 					  &pEndpoint->TxLookupQueue);
1853 	UNLOCK_HTC_TX(target);
1854 
1855 	ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
1856 				       HTC_PACKET, ListLink) {
1857 
1858 		if (NULL == pPacket) {
1859 			pFoundPacket = pPacket;
1860 			break;
1861 		}
1862 		/* check for removal */
1863 		if (netbuf ==
1864 		    (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
1865 			/* found it */
1866 			HTC_PACKET_REMOVE(&lookupQueue, pPacket);
1867 			pFoundPacket = pPacket;
1868 			break;
1869 		}
1870 
1871 	}
1872 	ITERATE_END;
1873 
1874 	LOCK_HTC_TX(target);
1875 	HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
1876 					  &lookupQueue);
1877 	UNLOCK_HTC_TX(target);
1878 	UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
1879 
1880 	return pFoundPacket;
1881 }
1882 
1883 /**
1884  * htc_tx_completion_handler() - htc tx completion handler
1885  * @Context: pointer to HTC_TARGET structure
1886  * @netbuf: pointer to netbuf for which completion handler is being called
1887  * @EpID: end point Id on which the packet was sent
1888  * @toeplitz_hash_result: toeplitz hash result
1889  *
1890  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
1891  */
1892 QDF_STATUS htc_tx_completion_handler(void *Context,
1893 				   qdf_nbuf_t netbuf, unsigned int EpID,
1894 				   uint32_t toeplitz_hash_result)
1895 {
1896 	HTC_TARGET *target = (HTC_TARGET *) Context;
1897 	HTC_ENDPOINT *pEndpoint;
1898 	HTC_PACKET *pPacket;
1899 #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
1900 	HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
1901 		ENDPOINT_2, ENDPOINT_3 };
1902 	int epidIdx;
1903 	uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
1904 	uint16_t resources;
1905 	uint16_t resourcesMax;
1906 #endif
1907 
1908 	pEndpoint = &target->endpoint[EpID];
1909 	target->TX_comp_cnt++;
1910 
1911 	do {
1912 		pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
1913 		if (NULL == pPacket) {
1914 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1915 					("HTC TX lookup failed!\n"));
1916 			/* may have already been flushed and freed */
1917 			netbuf = NULL;
1918 			break;
1919 		}
1920 		if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
1921 			hif_pm_runtime_put(target->hif_dev);
1922 
1923 		if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
1924 			HTC_PACKET *pPacketTemp;
1925 			HTC_PACKET_QUEUE *pQueueSave =
1926 				(HTC_PACKET_QUEUE *) pPacket->pContext;
1927 			HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
1928 							      pPacketTemp) {
1929 				pPacket->Status = QDF_STATUS_SUCCESS;
1930 				send_packet_completion(target, pPacketTemp);
1931 			}
1932 			HTC_PACKET_QUEUE_ITERATE_END;
1933 			free_htc_bundle_packet(target, pPacket);
1934 
1935 			if (hif_get_bus_type(target->hif_dev) ==
1936 					     QDF_BUS_TYPE_USB) {
1937 				if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
1938 					htc_try_send(target, pEndpoint, NULL);
1939 			}
1940 
1941 			return QDF_STATUS_SUCCESS;
1942 		}
1943 		/* will be giving this buffer back to upper layers */
1944 		netbuf = NULL;
1945 		pPacket->Status = QDF_STATUS_SUCCESS;
1946 		send_packet_completion(target, pPacket);
1947 
1948 	} while (false);
1949 
1950 	if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
1951 		/* note: when using TX credit flow, the re-checking of queues
1952 		 * happens when credits flow back from the target. In the non-TX
1953 		 * credit case, we recheck after the packet completes
1954 		 */
1955 		if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
1956 				(!pEndpoint->async_update)) {
1957 			htc_try_send(target, pEndpoint, NULL);
1958 		}
1959 	}
1960 
1961 	return QDF_STATUS_SUCCESS;
1962 }
1963 
1964 #ifdef WLAN_FEATURE_FASTPATH
1965 /**
1966  * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
1967  * @HTC_HANDLE : pointer to the htc target context
1968  * @htc_ep_id  : end point id
1969  *
1970  * checks HTC tx completion
1971  *
1972  * Return: none
1973  */
1974 void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
1975 {
1976 	HTC_TARGET      *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
1977 	HTC_ENDPOINT    *pendpoint = &target->endpoint[htc_ep_id];
1978 
1979 	htc_send_complete_check(pendpoint, 1);
1980 }
1981 qdf_export_symbol(htc_ctrl_msg_cmpl);
1982 #endif
1983 
1984 /* callback when TX resources become available */
1985 void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
1986 {
1987 	int i;
1988 	HTC_TARGET *target = (HTC_TARGET *) context;
1989 	HTC_ENDPOINT *pEndpoint = NULL;
1990 
1991 	for (i = 0; i < ENDPOINT_MAX; i++) {
1992 		pEndpoint = &target->endpoint[i];
1993 		if (pEndpoint->service_id != 0) {
1994 			if (pEndpoint->UL_PipeID == pipeID)
1995 				break;
1996 		}
1997 	}
1998 
1999 	if (i >= ENDPOINT_MAX) {
2000 		AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2001 				("Invalid pipe indicated for TX resource avail : %d!\n",
2002 				 pipeID));
2003 		return;
2004 	}
2005 
2006 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2007 			("HIF indicated more resources for pipe:%d\n",
2008 			 pipeID));
2009 
2010 	htc_try_send(target, pEndpoint, NULL);
2011 }
2012 
2013 #ifdef FEATURE_RUNTIME_PM
2014 /**
2015  * htc_kick_queues(): resumes tx transactions of suspended endpoints
2016  * @context: pointer to the htc target context
2017  *
2018  * Iterates through the enpoints and provides a context to empty queues
2019  * int the hif layer when they are stalled due to runtime suspend.
2020  *
2021  * Return: none
2022  */
2023 void htc_kick_queues(void *context)
2024 {
2025 	int i;
2026 	HTC_TARGET *target = (HTC_TARGET *)context;
2027 	HTC_ENDPOINT *endpoint = NULL;
2028 
2029 	for (i = 0; i < ENDPOINT_MAX; i++) {
2030 		endpoint = &target->endpoint[i];
2031 
2032 		if (endpoint->service_id == 0)
2033 			continue;
2034 
2035 		if (endpoint->EpCallBacks.ep_resume_tx_queue)
2036 			endpoint->EpCallBacks.ep_resume_tx_queue(
2037 					endpoint->EpCallBacks.pContext);
2038 
2039 		htc_try_send(target, endpoint, NULL);
2040 	}
2041 
2042 	hif_fastpath_resume(target->hif_dev);
2043 }
2044 #endif
2045 
2046 /* flush endpoint TX queue */
2047 void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
2048 			   HTC_TX_TAG Tag)
2049 {
2050 	HTC_PACKET *pPacket;
2051 
2052 	LOCK_HTC_TX(target);
2053 	while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2054 		pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
2055 
2056 		if (pPacket) {
2057 			/* let the sender know the packet was not delivered */
2058 			pPacket->Status = QDF_STATUS_E_CANCELED;
2059 			send_packet_completion(target, pPacket);
2060 		}
2061 	}
2062 	UNLOCK_HTC_TX(target);
2063 }
2064 
2065 /* flush pending entries in endpoint TX Lookup queue */
2066 void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
2067 				  HTC_ENDPOINT_ID endpoint_id,
2068 				  bool call_ep_callback)
2069 {
2070 	HTC_PACKET *packet;
2071 	HTC_ENDPOINT *endpoint;
2072 
2073 	endpoint = &target->endpoint[endpoint_id];
2074 
2075 	if (!endpoint && endpoint->service_id == 0)
2076 		return;
2077 
2078 	while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
2079 		packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
2080 
2081 		if (packet) {
2082 			if (call_ep_callback == true) {
2083 				packet->Status = QDF_STATUS_E_CANCELED;
2084 				send_packet_completion(target, packet);
2085 			} else {
2086 				qdf_mem_free(packet);
2087 			}
2088 		}
2089 	}
2090 }
2091 
2092 /* HTC API to flush an endpoint's TX queue*/
2093 void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
2094 			HTC_TX_TAG Tag)
2095 {
2096 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2097 	HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
2098 
2099 	if (pEndpoint->service_id == 0) {
2100 		AR_DEBUG_ASSERT(false);
2101 		/* not in use.. */
2102 		return;
2103 	}
2104 
2105 	htc_flush_endpoint_tx(target, pEndpoint, Tag);
2106 }
2107 
2108 /* HTC API to indicate activity to the credit distribution function */
2109 void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
2110 				  HTC_ENDPOINT_ID Endpoint, bool Active)
2111 {
2112 	/*  TODO */
2113 }
2114 
2115 bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
2116 {
2117 	return true;
2118 }
2119 
2120 void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
2121 {
2122 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2123 
2124 	target->is_nodrop_pkt = isNodropPkt;
2125 }
2126 
2127 /**
2128  * htc_process_credit_rpt() - process credit report, call distribution function
2129  * @target: pointer to HTC_TARGET
2130  * @pRpt: pointer to HTC_CREDIT_REPORT
2131  * @NumEntries: number of entries in credit report
2132  * @FromEndpoint: endpoint for which  credit report is received
2133  *
2134  * Return: A_OK for success or an appropriate A_STATUS error
2135  */
2136 void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
2137 			    int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
2138 {
2139 	int i;
2140 	HTC_ENDPOINT *pEndpoint;
2141 	int totalCredits = 0;
2142 	uint8_t rpt_credits, rpt_ep_id;
2143 
2144 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2145 			("+htc_process_credit_rpt, Credit Report Entries:%d\n",
2146 			 NumEntries));
2147 
2148 	/* lock out TX while we update credits */
2149 	LOCK_HTC_TX(target);
2150 
2151 	for (i = 0; i < NumEntries; i++, pRpt++) {
2152 
2153 		rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
2154 
2155 		if (rpt_ep_id >= ENDPOINT_MAX) {
2156 			AR_DEBUG_ASSERT(false);
2157 			break;
2158 		}
2159 
2160 		rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
2161 
2162 		pEndpoint = &target->endpoint[rpt_ep_id];
2163 #if DEBUG_CREDIT
2164 		if (ep_debug_mask & (1 << pEndpoint->Id)) {
2165 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
2166 					(" <HTC> Increase EP%d %d + %d = %d credits\n",
2167 					 rpt_ep_id, pEndpoint->TxCredits,
2168 					 rpt_credits,
2169 					 pEndpoint->TxCredits + rpt_credits));
2170 		}
2171 #endif
2172 
2173 #ifdef HTC_EP_STAT_PROFILING
2174 
2175 		INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
2176 		INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
2177 
2178 		if (FromEndpoint == rpt_ep_id) {
2179 			/* this credit report arrived on the same endpoint
2180 			 * indicating it arrived in an RX packet
2181 			 */
2182 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
2183 					rpt_credits);
2184 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
2185 		} else if (FromEndpoint == ENDPOINT_0) {
2186 			/* this credit arrived on endpoint 0 as a NULL msg */
2187 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
2188 					rpt_credits);
2189 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
2190 		} else {
2191 			/* arrived on another endpoint */
2192 			INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
2193 					rpt_credits);
2194 			INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
2195 		}
2196 
2197 #endif
2198 
2199 		if (pEndpoint->service_id == WMI_CONTROL_SVC) {
2200 			htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
2201 					  pEndpoint->TxCredits + rpt_credits,
2202 					  HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
2203 							TxQueue));
2204 		}
2205 
2206 		pEndpoint->TxCredits += rpt_credits;
2207 
2208 		if (pEndpoint->TxCredits
2209 		    && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
2210 			UNLOCK_HTC_TX(target);
2211 #ifdef ATH_11AC_TXCOMPACT
2212 			htc_try_send(target, pEndpoint, NULL);
2213 #else
2214 			if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
2215 				htc_send_data_pkt(target, NULL, 0);
2216 			else
2217 				htc_try_send(target, pEndpoint, NULL);
2218 #endif
2219 			LOCK_HTC_TX(target);
2220 		}
2221 		totalCredits += rpt_credits;
2222 	}
2223 
2224 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
2225 			("  Report indicated %d credits to distribute\n",
2226 			 totalCredits));
2227 
2228 	UNLOCK_HTC_TX(target);
2229 
2230 	AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
2231 }
2232 
2233 /* function to fetch stats from htc layer*/
2234 struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
2235 {
2236 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
2237 
2238 	return &(target->htc_pkt_stats);
2239 }
2240