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