xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc.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 <hif.h>
23 #include <qdf_nbuf.h>           /* qdf_nbuf_t */
24 #include <qdf_types.h>          /* qdf_print */
25 
26 #define MAX_HTC_RX_BUNDLE  2
27 
28 #if defined(WLAN_DEBUG) || defined(DEBUG)
29 static ATH_DEBUG_MASK_DESCRIPTION g_htc_debug_description[] = {
30 	{ATH_DEBUG_SEND, "Send"},
31 	{ATH_DEBUG_RECV, "Recv"},
32 	{ATH_DEBUG_SYNC, "Sync"},
33 	{ATH_DEBUG_DUMP, "Dump Data (RX or TX)"},
34 	{ATH_DEBUG_SETUP, "Setup"},
35 };
36 
37 ATH_DEBUG_INSTANTIATE_MODULE_VAR(htc,
38 				 "htc",
39 				 "Host Target Communications",
40 				 ATH_DEBUG_MASK_DEFAULTS | ATH_DEBUG_INFO |
41 				 ATH_DEBUG_SETUP,
42 				 ATH_DEBUG_DESCRIPTION_COUNT
43 					 (g_htc_debug_description),
44 				 g_htc_debug_description);
45 
46 #endif
47 
48 #if (defined(CONFIG_MCL) || (QCA_WIFI_QCA8074))
49 static const uint32_t svc_id[] = {WMI_CONTROL_SVC, WMI_CONTROL_SVC_WMAC1,
50 						WMI_CONTROL_SVC_WMAC2};
51 #else
52 static const uint32_t svc_id[] = {WMI_CONTROL_SVC};
53 #endif
54 
55 extern unsigned int htc_credit_flow;
56 
57 static void reset_endpoint_states(HTC_TARGET *target);
58 
59 static void destroy_htc_tx_ctrl_packet(HTC_PACKET *pPacket)
60 {
61 	qdf_nbuf_t netbuf;
62 
63 	netbuf = (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
64 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("free ctrl netbuf :0x%pK\n", netbuf));
65 	if (netbuf != NULL)
66 		qdf_nbuf_free(netbuf);
67 	qdf_mem_free(pPacket);
68 }
69 
70 static HTC_PACKET *build_htc_tx_ctrl_packet(qdf_device_t osdev)
71 {
72 	HTC_PACKET *pPacket = NULL;
73 	qdf_nbuf_t netbuf;
74 
75 	do {
76 		pPacket = (HTC_PACKET *) qdf_mem_malloc(sizeof(HTC_PACKET));
77 		if (pPacket == NULL)
78 			break;
79 		netbuf = qdf_nbuf_alloc(osdev, HTC_CONTROL_BUFFER_SIZE,
80 					20, 4, true);
81 		if (NULL == netbuf) {
82 			qdf_mem_free(pPacket);
83 			pPacket = NULL;
84 			qdf_print("%s: nbuf alloc failed", __func__);
85 			break;
86 		}
87 		AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
88 				("alloc ctrl netbuf :0x%pK\n", netbuf));
89 		SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
90 	} while (false);
91 
92 	return pPacket;
93 }
94 
95 void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
96 {
97 
98 #ifdef TODO_FIXME
99 	LOCK_HTC(target);
100 	HTC_PACKET_ENQUEUE(&target->ControlBufferTXFreeList, pPacket);
101 	UNLOCK_HTC(target);
102 	/* TODO_FIXME netbufs cannot be RESET! */
103 #else
104 	destroy_htc_tx_ctrl_packet(pPacket);
105 #endif
106 
107 }
108 
109 HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target)
110 {
111 #ifdef TODO_FIXME
112 	HTC_PACKET *pPacket;
113 
114 	LOCK_HTC(target);
115 	pPacket = htc_packet_dequeue(&target->ControlBufferTXFreeList);
116 	UNLOCK_HTC(target);
117 
118 	return pPacket;
119 #else
120 	return build_htc_tx_ctrl_packet(target->osdev);
121 #endif
122 }
123 
124 /* Set the target failure handling callback */
125 void htc_set_target_failure_callback(HTC_HANDLE HTCHandle,
126 				     HTC_TARGET_FAILURE Callback)
127 {
128 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
129 
130 	target->HTCInitInfo.TargetFailure = Callback;
131 }
132 
133 void htc_dump(HTC_HANDLE HTCHandle, uint8_t CmdId, bool start)
134 {
135 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
136 
137 	hif_dump(target->hif_dev, CmdId, start);
138 }
139 
140 /* cleanup the HTC instance */
141 static void htc_cleanup(HTC_TARGET *target)
142 {
143 	HTC_PACKET *pPacket;
144 	int i;
145 	HTC_ENDPOINT *endpoint;
146 	HTC_PACKET_QUEUE *pkt_queue;
147 	qdf_nbuf_t netbuf;
148 
149 	if (target->hif_dev != NULL) {
150 		hif_detach_htc(target->hif_dev);
151 		hif_mask_interrupt_call(target->hif_dev);
152 		target->hif_dev = NULL;
153 	}
154 
155 	while (true) {
156 		pPacket = allocate_htc_packet_container(target);
157 		if (pPacket == NULL)
158 			break;
159 		qdf_mem_free(pPacket);
160 	}
161 
162 	LOCK_HTC_TX(target);
163 	pPacket = target->pBundleFreeList;
164 	target->pBundleFreeList = NULL;
165 	UNLOCK_HTC_TX(target);
166 	while (pPacket) {
167 		HTC_PACKET *pPacketTmp = (HTC_PACKET *) pPacket->ListLink.pNext;
168 		netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
169 		if (netbuf)
170 			qdf_nbuf_free(netbuf);
171 		pkt_queue = pPacket->pContext;
172 		if (pkt_queue)
173 			qdf_mem_free(pkt_queue);
174 		qdf_mem_free(pPacket);
175 		pPacket = pPacketTmp;
176 	}
177 
178 #ifdef TODO_FIXME
179 	while (true) {
180 		pPacket = htc_alloc_control_tx_packet(target);
181 		if (pPacket == NULL)
182 			break;
183 		netbuf = (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
184 		if (netbuf != NULL)
185 			qdf_nbuf_free(netbuf);
186 		qdf_mem_free(pPacket);
187 	}
188 #endif
189 
190 	htc_flush_endpoint_txlookupQ(target, ENDPOINT_0, true);
191 
192 	qdf_spinlock_destroy(&target->HTCLock);
193 	qdf_spinlock_destroy(&target->HTCRxLock);
194 	qdf_spinlock_destroy(&target->HTCTxLock);
195 	qdf_spinlock_destroy(&target->HTCCreditLock);
196 	for (i = 0; i < ENDPOINT_MAX; i++) {
197 		endpoint = &target->endpoint[i];
198 		qdf_spinlock_destroy(&endpoint->lookup_queue_lock);
199 	}
200 
201 	/* free our instance */
202 	qdf_mem_free(target);
203 }
204 
205 #ifdef FEATURE_RUNTIME_PM
206 /**
207  * htc_runtime_pm_init(): runtime pm related intialization
208  *
209  * need to initialize a work item.
210  */
211 static void htc_runtime_pm_init(HTC_TARGET *target)
212 {
213 	qdf_create_work(0, &target->queue_kicker, htc_kick_queues, target);
214 }
215 
216 /**
217  * htc_runtime_suspend() - runtime suspend HTC
218  *
219  * @htc_ctx: HTC context pointer
220  *
221  * This is a dummy function for symmetry.
222  *
223  * Return: 0 for success
224  */
225 int htc_runtime_suspend(HTC_HANDLE htc_ctx)
226 {
227 	return 0;
228 }
229 
230 /**
231  * htc_runtime_resume(): resume htc
232  *
233  * The htc message queue needs to be kicked off after
234  * a runtime resume.  Otherwise messages would get stuck.
235  *
236  * @htc_ctx: HTC context pointer
237  *
238  * Return: 0 for success;
239  */
240 int htc_runtime_resume(HTC_HANDLE htc_ctx)
241 {
242 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_ctx);
243 
244 	if (target == NULL)
245 		return 0;
246 
247 	qdf_sched_work(0, &target->queue_kicker);
248 	return 0;
249 }
250 #else
251 static inline void htc_runtime_pm_init(HTC_TARGET *target) { }
252 #endif
253 
254 /* registered target arrival callback from the HIF layer */
255 HTC_HANDLE htc_create(void *ol_sc, struct htc_init_info *pInfo,
256 			qdf_device_t osdev, uint32_t con_mode)
257 {
258 	struct hif_msg_callbacks htcCallbacks;
259 	HTC_ENDPOINT *pEndpoint = NULL;
260 	HTC_TARGET *target = NULL;
261 	int i;
262 
263 	if (ol_sc == NULL) {
264 		HTC_ERROR("%s: ol_sc = NULL", __func__);
265 		return NULL;
266 	}
267 	HTC_TRACE("+htc_create ..  HIF :%pK", ol_sc);
268 
269 	A_REGISTER_MODULE_DEBUG_INFO(htc);
270 
271 	target = (HTC_TARGET *) qdf_mem_malloc(sizeof(HTC_TARGET));
272 	if (target == NULL) {
273 		HTC_ERROR("%s: Unable to allocate memory", __func__);
274 		return NULL;
275 	}
276 
277 	htc_runtime_pm_init(target);
278 	htc_credit_history_init();
279 	qdf_spinlock_create(&target->HTCLock);
280 	qdf_spinlock_create(&target->HTCRxLock);
281 	qdf_spinlock_create(&target->HTCTxLock);
282 	for (i = 0; i < ENDPOINT_MAX; i++) {
283 		pEndpoint = &target->endpoint[i];
284 		qdf_spinlock_create(&pEndpoint->lookup_queue_lock);
285 	}
286 	target->is_nodrop_pkt = false;
287 	target->wmi_ep_count = 1;
288 
289 	do {
290 		qdf_mem_copy(&target->HTCInitInfo, pInfo,
291 			     sizeof(struct htc_init_info));
292 		target->host_handle = pInfo->pContext;
293 		target->osdev = osdev;
294 		target->con_mode = con_mode;
295 
296 		reset_endpoint_states(target);
297 
298 		INIT_HTC_PACKET_QUEUE(&target->ControlBufferTXFreeList);
299 
300 		for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
301 			HTC_PACKET *pPacket = (HTC_PACKET *)
302 					qdf_mem_malloc(sizeof(HTC_PACKET));
303 			if (pPacket != NULL)
304 				free_htc_packet_container(target, pPacket);
305 		}
306 
307 #ifdef TODO_FIXME
308 		for (i = 0; i < NUM_CONTROL_TX_BUFFERS; i++) {
309 			pPacket = build_htc_tx_ctrl_packet();
310 			if (pPacket == NULL)
311 				break;
312 			htc_free_control_tx_packet(target, pPacket);
313 		}
314 #endif
315 
316 		/* setup HIF layer callbacks */
317 		qdf_mem_zero(&htcCallbacks, sizeof(struct hif_msg_callbacks));
318 		htcCallbacks.Context = target;
319 		htcCallbacks.rxCompletionHandler = htc_rx_completion_handler;
320 		htcCallbacks.txCompletionHandler = htc_tx_completion_handler;
321 		htcCallbacks.txResourceAvailHandler =
322 						 htc_tx_resource_avail_handler;
323 		htcCallbacks.fwEventHandler = htc_fw_event_handler;
324 		target->hif_dev = ol_sc;
325 
326 		/* Get HIF default pipe for HTC message exchange */
327 		pEndpoint = &target->endpoint[ENDPOINT_0];
328 
329 		hif_post_init(target->hif_dev, target, &htcCallbacks);
330 		hif_get_default_pipe(target->hif_dev, &pEndpoint->UL_PipeID,
331 				     &pEndpoint->DL_PipeID);
332 		hif_set_initial_wakeup_cb(target->hif_dev,
333 					  pInfo->target_initial_wakeup_cb,
334 					  pInfo->target_psoc);
335 
336 	} while (false);
337 
338 	htc_recv_init(target);
339 
340 	HTC_TRACE("-htc_create: (0x%pK)", target);
341 
342 	return (HTC_HANDLE) target;
343 }
344 
345 void htc_destroy(HTC_HANDLE HTCHandle)
346 {
347 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
348 
349 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
350 			("+htc_destroy ..  Destroying :0x%pK\n", target));
351 	hif_stop(htc_get_hif_device(HTCHandle));
352 	if (target)
353 		htc_cleanup(target);
354 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_destroy\n"));
355 }
356 
357 /* get the low level HIF device for the caller , the caller may wish to do low
358  * level HIF requests
359  */
360 void *htc_get_hif_device(HTC_HANDLE HTCHandle)
361 {
362 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
363 
364 	return target->hif_dev;
365 }
366 
367 static void htc_control_tx_complete(void *Context, HTC_PACKET *pPacket)
368 {
369 	HTC_TARGET *target = (HTC_TARGET *) Context;
370 
371 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
372 			("+-htc_control_tx_complete 0x%pK (l:%d)\n", pPacket,
373 			 pPacket->ActualLength));
374 	htc_free_control_tx_packet(target, pPacket);
375 }
376 
377 /* TODO, this is just a temporary max packet size */
378 #define MAX_MESSAGE_SIZE 1536
379 
380 /**
381  * htc_setup_epping_credit_allocation() - allocate credits/HTC buffers to WMI
382  * @scn: pointer to hif_opaque_softc
383  * @pEntry: pointer to tx credit allocation entry
384  * @credits: number of credits
385  *
386  * Return: None
387  */
388 static void
389 htc_setup_epping_credit_allocation(struct hif_opaque_softc *scn,
390 			   struct htc_service_tx_credit_allocation *pEntry,
391 			   int credits)
392 {
393 	switch (hif_get_bus_type(scn)) {
394 	case QDF_BUS_TYPE_PCI:
395 		pEntry++;
396 		pEntry->service_id = WMI_DATA_BE_SVC;
397 		pEntry->CreditAllocation = (credits >> 1);
398 
399 		pEntry++;
400 		pEntry->service_id = WMI_DATA_BK_SVC;
401 		pEntry->CreditAllocation = (credits >> 1);
402 		break;
403 	case QDF_BUS_TYPE_SDIO:
404 		pEntry++;
405 		pEntry->service_id = WMI_DATA_BE_SVC;
406 		pEntry->CreditAllocation = credits;
407 		break;
408 	default:
409 		break;
410 	}
411 }
412 
413 /**
414  * htc_setup_target_buffer_assignments() - setup target buffer assignments
415  * @target: HTC Target Pointer
416  *
417  * Return: A_STATUS
418  */
419 static
420 A_STATUS htc_setup_target_buffer_assignments(HTC_TARGET *target)
421 {
422 	struct htc_service_tx_credit_allocation *pEntry;
423 	A_STATUS status;
424 	int credits;
425 	int creditsPerMaxMsg;
426 
427 	creditsPerMaxMsg = MAX_MESSAGE_SIZE / target->TargetCreditSize;
428 	if (MAX_MESSAGE_SIZE % target->TargetCreditSize)
429 		creditsPerMaxMsg++;
430 
431 	/* TODO, this should be configured by the caller! */
432 
433 	credits = target->TotalTransmitCredits;
434 	pEntry = &target->ServiceTxAllocTable[0];
435 
436 	status = A_OK;
437 	/*
438 	 * Allocate all credists/HTC buffers to WMI.
439 	 * no buffers are used/required for data. data always
440 	 * remains on host.
441 	 */
442 	if (HTC_IS_EPPING_ENABLED(target->con_mode)) {
443 		pEntry++;
444 		pEntry->service_id = WMI_CONTROL_SVC;
445 		pEntry->CreditAllocation = credits;
446 		/* endpoint ping is a testing tool directly on top of HTC in
447 		 * both target and host sides.
448 		 * In target side, the endppint ping fw has no wlan stack and
449 		 * FW mboxping app directly sits on HTC and it simply drops
450 		 * or loops back TX packets. For rx perf, FW mboxping app
451 		 * generates packets and passes packets to HTC to send to host.
452 		 * There is no WMI message exchanges between host and target
453 		 * in endpoint ping case.
454 		 * In host side, the endpoint ping driver is a Ethernet driver
455 		 * and it directly sits on HTC. Only HIF, HTC, QDF, ADF are
456 		 * used by the endpoint ping driver. There is no wifi stack
457 		 * at all in host side also. For tx perf use case,
458 		 * the user space mboxping app sends the raw packets to endpoint
459 		 * ping driver and it directly forwards to HTC for transmission
460 		 * to stress the bus. For the rx perf, HTC passes the received
461 		 * packets to endpoint ping driver and it is passed to the user
462 		 * space through the Ethernet interface.
463 		 * For credit allocation, in SDIO bus case, only BE service is
464 		 * used for tx/rx perf testing so that all credits are given
465 		 * to BE service. In PCIe and USB bus case, endpoint ping uses
466 		 * both BE and BK services to stress the bus so that the total
467 		 * credits are equally distributed to BE and BK services.
468 		 */
469 
470 		htc_setup_epping_credit_allocation(target->hif_dev,
471 						   pEntry, credits);
472 	} else {
473 		int i;
474 		uint32_t max_wmi_svc = (sizeof(svc_id) / sizeof(uint32_t));
475 
476 		if ((target->wmi_ep_count == 0) ||
477 				(target->wmi_ep_count > max_wmi_svc))
478 			return A_ERROR;
479 
480 		/*
481 		 * Divide credit among number of endpoints for WMI
482 		 */
483 		credits = credits / target->wmi_ep_count;
484 		for (i = 0; i < target->wmi_ep_count; i++) {
485 			status = A_OK;
486 			pEntry++;
487 			pEntry->service_id = svc_id[i];
488 			pEntry->CreditAllocation = credits;
489 		}
490 	}
491 
492 	if (A_SUCCESS(status)) {
493 		int i;
494 
495 		for (i = 0; i < HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
496 			if (target->ServiceTxAllocTable[i].service_id != 0) {
497 				AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
498 						("SVS Index : %d TX : 0x%2.2X : alloc:%d",
499 						 i,
500 						 target->ServiceTxAllocTable[i].
501 						 service_id,
502 						 target->ServiceTxAllocTable[i].
503 						 CreditAllocation));
504 			}
505 		}
506 	}
507 
508 	return status;
509 }
510 
511 uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id)
512 {
513 	uint8_t allocation = 0;
514 	int i;
515 
516 	for (i = 0; i < HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
517 		if (target->ServiceTxAllocTable[i].service_id == service_id) {
518 			allocation =
519 				target->ServiceTxAllocTable[i].CreditAllocation;
520 		}
521 	}
522 
523 	if (0 == allocation) {
524 		AR_DEBUG_PRINTF(ATH_DEBUG_RSVD1,
525 			("HTC Service TX : 0x%2.2X : allocation is zero!\n",
526 				 service_id));
527 	}
528 
529 	return allocation;
530 }
531 
532 QDF_STATUS htc_wait_target(HTC_HANDLE HTCHandle)
533 {
534 	QDF_STATUS status = QDF_STATUS_SUCCESS;
535 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
536 	HTC_READY_EX_MSG *pReadyMsg;
537 	struct htc_service_connect_req connect;
538 	struct htc_service_connect_resp resp;
539 	HTC_READY_MSG *rdy_msg;
540 	uint16_t htc_rdy_msg_id;
541 	uint8_t i = 0;
542 	HTC_PACKET *rx_bundle_packet, *temp_bundle_packet;
543 
544 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
545 			("htc_wait_target - Enter (target:0x%pK)\n", HTCHandle));
546 	AR_DEBUG_PRINTF(ATH_DEBUG_RSVD1, ("+HWT\n"));
547 
548 	do {
549 
550 		status = hif_start(target->hif_dev);
551 		if (QDF_IS_STATUS_ERROR(status)) {
552 			AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
553 					("hif_start failed\n"));
554 			break;
555 		}
556 
557 		status = htc_wait_recv_ctrl_message(target);
558 
559 		if (QDF_IS_STATUS_ERROR(status))
560 			break;
561 
562 		if (target->CtrlResponseLength < (sizeof(HTC_READY_EX_MSG))) {
563 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
564 					("Invalid HTC Ready Msg Len:%d!\n",
565 					 target->CtrlResponseLength));
566 			status = QDF_STATUS_E_BADMSG;
567 			break;
568 		}
569 
570 		pReadyMsg = (HTC_READY_EX_MSG *) target->CtrlResponseBuffer;
571 
572 		rdy_msg = &pReadyMsg->Version2_0_Info;
573 		htc_rdy_msg_id =
574 			HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, MESSAGEID);
575 		if (htc_rdy_msg_id != HTC_MSG_READY_ID) {
576 			AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
577 					("Invalid HTC Ready Msg : 0x%X!\n",
578 					 htc_rdy_msg_id));
579 			status = QDF_STATUS_E_BADMSG;
580 			break;
581 		}
582 
583 		target->TotalTransmitCredits =
584 			HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITCOUNT);
585 		target->TargetCreditSize =
586 			(int)HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITSIZE);
587 		target->MaxMsgsPerHTCBundle =
588 			(uint8_t) pReadyMsg->MaxMsgsPerHTCBundle;
589 		UPDATE_ALT_CREDIT(target, pReadyMsg->AltDataCreditSize);
590 		/* for old fw this value is set to 0. But the minimum value
591 		 * should be 1, i.e., no bundling
592 		 */
593 		if (target->MaxMsgsPerHTCBundle < 1)
594 			target->MaxMsgsPerHTCBundle = 1;
595 
596 		AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
597 				("Target Ready! TX resource : %d size:%d, MaxMsgsPerHTCBundle = %d",
598 				 target->TotalTransmitCredits,
599 				 target->TargetCreditSize,
600 				 target->MaxMsgsPerHTCBundle));
601 
602 		if ((0 == target->TotalTransmitCredits)
603 		    || (0 == target->TargetCreditSize)) {
604 			status = QDF_STATUS_E_ABORTED;
605 			break;
606 		}
607 
608 		/* Allocate expected number of RX bundle buffer allocation */
609 		if (HTC_RX_BUNDLE_ENABLED(target)) {
610 			temp_bundle_packet = NULL;
611 			for (i = 0; i < MAX_HTC_RX_BUNDLE; i++) {
612 				rx_bundle_packet =
613 					allocate_htc_bundle_packet(target);
614 				if (rx_bundle_packet != NULL)
615 					rx_bundle_packet->ListLink.pNext =
616 						(DL_LIST *)temp_bundle_packet;
617 				else
618 					break;
619 
620 				temp_bundle_packet = rx_bundle_packet;
621 			}
622 			LOCK_HTC_TX(target);
623 			target->pBundleFreeList = temp_bundle_packet;
624 			UNLOCK_HTC_TX(target);
625 		}
626 
627 		/* done processing */
628 		target->CtrlResponseProcessing = false;
629 
630 		htc_setup_target_buffer_assignments(target);
631 
632 		/* setup our pseudo HTC control endpoint connection */
633 		qdf_mem_zero(&connect, sizeof(connect));
634 		qdf_mem_zero(&resp, sizeof(resp));
635 		connect.EpCallbacks.pContext = target;
636 		connect.EpCallbacks.EpTxComplete = htc_control_tx_complete;
637 		connect.EpCallbacks.EpRecv = htc_control_rx_complete;
638 		connect.MaxSendQueueDepth = NUM_CONTROL_TX_BUFFERS;
639 		connect.service_id = HTC_CTRL_RSVD_SVC;
640 
641 		/* connect fake service */
642 		status = htc_connect_service((HTC_HANDLE) target,
643 					     &connect, &resp);
644 
645 	} while (false);
646 
647 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_wait_target - Exit (%d)\n",
648 			status));
649 	AR_DEBUG_PRINTF(ATH_DEBUG_RSVD1, ("-HWT\n"));
650 	return status;
651 }
652 
653 /* start HTC, this is called after all services are connected */
654 static A_STATUS htc_config_target_hif_pipe(HTC_TARGET *target)
655 {
656 
657 	return A_OK;
658 }
659 
660 static void reset_endpoint_states(HTC_TARGET *target)
661 {
662 	HTC_ENDPOINT *pEndpoint;
663 	int i;
664 
665 	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
666 		pEndpoint = &target->endpoint[i];
667 		pEndpoint->service_id = 0;
668 		pEndpoint->MaxMsgLength = 0;
669 		pEndpoint->MaxTxQueueDepth = 0;
670 		pEndpoint->Id = i;
671 		INIT_HTC_PACKET_QUEUE(&pEndpoint->TxQueue);
672 		INIT_HTC_PACKET_QUEUE(&pEndpoint->TxLookupQueue);
673 		INIT_HTC_PACKET_QUEUE(&pEndpoint->RxBufferHoldQueue);
674 		pEndpoint->target = target;
675 		pEndpoint->TxCreditFlowEnabled = (bool)htc_credit_flow;
676 		qdf_atomic_init(&pEndpoint->TxProcessCount);
677 	}
678 }
679 
680 /**
681  * htc_start() - Main HTC function to trigger HTC start
682  * @HTCHandle: pointer to HTC handle
683  *
684  * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
685  */
686 QDF_STATUS htc_start(HTC_HANDLE HTCHandle)
687 {
688 	qdf_nbuf_t netbuf;
689 	QDF_STATUS status = QDF_STATUS_SUCCESS;
690 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
691 	HTC_SETUP_COMPLETE_EX_MSG *pSetupComp;
692 	HTC_PACKET *pSendPacket;
693 
694 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Enter\n"));
695 
696 	do {
697 
698 		htc_config_target_hif_pipe(target);
699 
700 		/* allocate a buffer to send */
701 		pSendPacket = htc_alloc_control_tx_packet(target);
702 		if (NULL == pSendPacket) {
703 			AR_DEBUG_ASSERT(false);
704 			qdf_print("%s: allocControlTxPacket failed",
705 				  __func__);
706 			status = QDF_STATUS_E_NOMEM;
707 			break;
708 		}
709 
710 		netbuf =
711 		   (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pSendPacket);
712 		/* assemble setup complete message */
713 		qdf_nbuf_put_tail(netbuf, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
714 		pSetupComp =
715 			(HTC_SETUP_COMPLETE_EX_MSG *) qdf_nbuf_data(netbuf);
716 		qdf_mem_zero(pSetupComp, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
717 
718 		HTC_SET_FIELD(pSetupComp, HTC_SETUP_COMPLETE_EX_MSG,
719 			      MESSAGEID, HTC_MSG_SETUP_COMPLETE_EX_ID);
720 
721 		if (!htc_credit_flow) {
722 			AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
723 					("HTC will not use TX credit flow control"));
724 			pSetupComp->SetupFlags |=
725 				HTC_SETUP_COMPLETE_FLAGS_DISABLE_TX_CREDIT_FLOW;
726 		} else {
727 			AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
728 					("HTC using TX credit flow control"));
729 		}
730 
731 		if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO) ||
732 					(hif_get_bus_type(target->hif_dev) ==
733 							 QDF_BUS_TYPE_USB)) {
734 			if (HTC_RX_BUNDLE_ENABLED(target))
735 			pSetupComp->SetupFlags |=
736 				HTC_SETUP_COMPLETE_FLAGS_ENABLE_BUNDLE_RECV;
737 			hif_set_bundle_mode(target->hif_dev, true,
738 				HTC_MAX_MSG_PER_BUNDLE_RX);
739 		}
740 
741 		SET_HTC_PACKET_INFO_TX(pSendPacket,
742 				       NULL,
743 				       (uint8_t *) pSetupComp,
744 				       sizeof(HTC_SETUP_COMPLETE_EX_MSG),
745 				       ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
746 
747 		status = htc_send_pkt((HTC_HANDLE) target, pSendPacket);
748 		if (QDF_IS_STATUS_ERROR(status))
749 			break;
750 	} while (false);
751 
752 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Exit\n"));
753 	return status;
754 }
755 
756 /*flush all queued buffers for surpriseremove case*/
757 void htc_flush_surprise_remove(HTC_HANDLE HTCHandle)
758 {
759 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
760 	int i;
761 	HTC_ENDPOINT *pEndpoint;
762 #ifdef RX_SG_SUPPORT
763 	qdf_nbuf_t netbuf;
764 	qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
765 #endif
766 
767 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_flush_surprise_remove\n"));
768 
769 	/* cleanup endpoints */
770 	for (i = 0; i < ENDPOINT_MAX; i++) {
771 		pEndpoint = &target->endpoint[i];
772 		htc_flush_rx_hold_queue(target, pEndpoint);
773 		htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
774 	}
775 
776 	hif_flush_surprise_remove(target->hif_dev);
777 
778 #ifdef RX_SG_SUPPORT
779 	LOCK_HTC_RX(target);
780 	while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
781 		qdf_nbuf_free(netbuf);
782 	RESET_RX_SG_CONFIG(target);
783 	UNLOCK_HTC_RX(target);
784 #endif
785 
786 	reset_endpoint_states(target);
787 
788 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_flush_surprise_remove\n"));
789 }
790 
791 /* stop HTC communications, i.e. stop interrupt reception, and flush all queued
792  * buffers
793  */
794 void htc_stop(HTC_HANDLE HTCHandle)
795 {
796 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
797 	int i;
798 	HTC_ENDPOINT *pEndpoint;
799 #ifdef RX_SG_SUPPORT
800 	qdf_nbuf_t netbuf;
801 	qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
802 #endif
803 
804 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_stop\n"));
805 
806 	/* cleanup endpoints */
807 	for (i = 0; i < ENDPOINT_MAX; i++) {
808 		pEndpoint = &target->endpoint[i];
809 		htc_flush_rx_hold_queue(target, pEndpoint);
810 		htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
811 		if (pEndpoint->ul_is_polled) {
812 			qdf_timer_stop(&pEndpoint->ul_poll_timer);
813 			qdf_timer_free(&pEndpoint->ul_poll_timer);
814 		}
815 	}
816 
817 	/* Note: htc_flush_endpoint_tx for all endpoints should be called before
818 	 * hif_stop - otherwise htc_tx_completion_handler called from
819 	 * hif_send_buffer_cleanup_on_pipe for residual tx frames in HIF layer,
820 	 * might queue the packet again to HIF Layer - which could cause tx
821 	 * buffer leak
822 	 */
823 
824 	hif_stop(target->hif_dev);
825 
826 #ifdef RX_SG_SUPPORT
827 	LOCK_HTC_RX(target);
828 	while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
829 		qdf_nbuf_free(netbuf);
830 	RESET_RX_SG_CONFIG(target);
831 	UNLOCK_HTC_RX(target);
832 #endif
833 
834 	/**
835 	 * In SSR case, HTC tx completion callback for wmi will be blocked
836 	 * by TARGET_STATUS_RESET and HTC packets will be left unfreed on
837 	 * lookup queue.
838 	 */
839 	for (i = 0; i < ENDPOINT_MAX; i++) {
840 		pEndpoint = &target->endpoint[i];
841 		if (pEndpoint->service_id == WMI_CONTROL_SVC)
842 			htc_flush_endpoint_txlookupQ(target, i, false);
843 	}
844 
845 	reset_endpoint_states(target);
846 
847 	AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_stop\n"));
848 }
849 
850 void htc_dump_credit_states(HTC_HANDLE HTCHandle)
851 {
852 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
853 	HTC_ENDPOINT *pEndpoint;
854 	int i;
855 
856 	for (i = 0; i < ENDPOINT_MAX; i++) {
857 		pEndpoint = &target->endpoint[i];
858 		if (0 == pEndpoint->service_id)
859 			continue;
860 
861 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
862 			("--- EP : %d  service_id: 0x%X    --------------\n",
863 				 pEndpoint->Id, pEndpoint->service_id));
864 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
865 				(" TxCredits          : %d\n",
866 				 pEndpoint->TxCredits));
867 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
868 				(" TxCreditSize       : %d\n",
869 				 pEndpoint->TxCreditSize));
870 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
871 				(" TxCreditsPerMaxMsg : %d\n",
872 				 pEndpoint->TxCreditsPerMaxMsg));
873 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
874 				(" TxQueueDepth       : %d\n",
875 				 HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
876 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
877 				("----------------------------------------\n"));
878 	}
879 }
880 
881 bool htc_get_endpoint_statistics(HTC_HANDLE HTCHandle,
882 				   HTC_ENDPOINT_ID Endpoint,
883 				   enum htc_endpoint_stat_action Action,
884 				   struct htc_endpoint_stats *pStats)
885 {
886 #ifdef HTC_EP_STAT_PROFILING
887 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
888 	bool clearStats = false;
889 	bool sample = false;
890 
891 	switch (Action) {
892 	case HTC_EP_STAT_SAMPLE:
893 		sample = true;
894 		break;
895 	case HTC_EP_STAT_SAMPLE_AND_CLEAR:
896 		sample = true;
897 		clearStats = true;
898 		break;
899 	case HTC_EP_STAT_CLEAR:
900 		clearStats = true;
901 		break;
902 	default:
903 		break;
904 	}
905 
906 	A_ASSERT(Endpoint < ENDPOINT_MAX);
907 
908 	/* lock out TX and RX while we sample and/or clear */
909 	LOCK_HTC_TX(target);
910 	LOCK_HTC_RX(target);
911 
912 	if (sample) {
913 		A_ASSERT(pStats != NULL);
914 		/* return the stats to the caller */
915 		qdf_mem_copy(pStats, &target->endpoint[Endpoint].endpoint_stats,
916 			 sizeof(struct htc_endpoint_stats));
917 	}
918 
919 	if (clearStats) {
920 		/* reset stats */
921 		qdf_mem_zero(&target->endpoint[Endpoint].endpoint_stats,
922 			  sizeof(struct htc_endpoint_stats));
923 	}
924 
925 	UNLOCK_HTC_RX(target);
926 	UNLOCK_HTC_TX(target);
927 
928 	return true;
929 #else
930 	return false;
931 #endif
932 }
933 
934 void *htc_get_targetdef(HTC_HANDLE htc_handle)
935 {
936 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
937 
938 	return hif_get_targetdef(target->hif_dev);
939 }
940 
941 #ifdef IPA_OFFLOAD
942 /**
943  * htc_ipa_get_ce_resource() - get uc resource on lower layer
944  * @htc_handle: htc context
945  * @ce_sr_base_paddr: copyengine source ring base physical address
946  * @ce_sr_ring_size: copyengine source ring size
947  * @ce_reg_paddr: copyengine register physical address
948  *
949  * Return: None
950  */
951 void htc_ipa_get_ce_resource(HTC_HANDLE htc_handle,
952 			     qdf_shared_mem_t **ce_sr,
953 			     uint32_t *ce_sr_ring_size,
954 			     qdf_dma_addr_t *ce_reg_paddr)
955 {
956 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
957 
958 	if (target->hif_dev)
959 		hif_ipa_get_ce_resource(target->hif_dev,
960 					ce_sr, ce_sr_ring_size, ce_reg_paddr);
961 }
962 #endif /* IPA_OFFLOAD */
963 
964 #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
965 
966 void htc_dump_bundle_stats(HTC_HANDLE HTCHandle)
967 {
968 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
969 	int total, i;
970 
971 	total = 0;
972 	for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
973 		total += target->rx_bundle_stats[i];
974 
975 	if (total) {
976 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("RX Bundle stats:\n"));
977 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total RX packets: %d\n",
978 						total));
979 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY, (
980 				"Number of bundle: Number of packets\n"));
981 		for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
982 			AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
983 					("%10d:%10d(%2d%s)\n", (i+1),
984 					 target->rx_bundle_stats[i],
985 					 ((target->rx_bundle_stats[i]*100)/
986 					  total), "%"));
987 	}
988 
989 
990 	total = 0;
991 	for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
992 		total += target->tx_bundle_stats[i];
993 
994 	if (total) {
995 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("TX Bundle stats:\n"));
996 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total TX packets: %d\n",
997 						total));
998 		AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
999 				("Number of bundle: Number of packets\n"));
1000 		for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
1001 			AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
1002 					("%10d:%10d(%2d%s)\n", (i+1),
1003 					 target->tx_bundle_stats[i],
1004 					 ((target->tx_bundle_stats[i]*100)/
1005 					  total), "%"));
1006 	}
1007 }
1008 
1009 void htc_clear_bundle_stats(HTC_HANDLE HTCHandle)
1010 {
1011 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
1012 
1013 	qdf_mem_zero(&target->rx_bundle_stats, sizeof(target->rx_bundle_stats));
1014 	qdf_mem_zero(&target->tx_bundle_stats, sizeof(target->tx_bundle_stats));
1015 }
1016 #endif
1017 
1018 /**
1019  * htc_vote_link_down - API to vote for link down
1020  * @htc_handle: HTC handle
1021  *
1022  * API for upper layers to call HIF to vote for link down
1023  *
1024  * Return: void
1025  */
1026 void htc_vote_link_down(HTC_HANDLE htc_handle)
1027 {
1028 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1029 
1030 	if (!target->hif_dev)
1031 		return;
1032 
1033 	hif_vote_link_down(target->hif_dev);
1034 }
1035 
1036 /**
1037  * htc_vote_link_up - API to vote for link up
1038  * @htc_handle: HTC Handle
1039  *
1040  * API for upper layers to call HIF to vote for link up
1041  *
1042  * Return: void
1043  */
1044 void htc_vote_link_up(HTC_HANDLE htc_handle)
1045 {
1046 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1047 
1048 	if (!target->hif_dev)
1049 		return;
1050 
1051 	hif_vote_link_up(target->hif_dev);
1052 }
1053 
1054 /**
1055  * htc_can_suspend_link - API to query HIF for link status
1056  * @htc_handle: HTC Handle
1057  *
1058  * API for upper layers to call HIF to query if the link can suspend
1059  *
1060  * Return: void
1061  */
1062 bool htc_can_suspend_link(HTC_HANDLE htc_handle)
1063 {
1064 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1065 
1066 	if (!target->hif_dev)
1067 		return false;
1068 
1069 	return hif_can_suspend_link(target->hif_dev);
1070 }
1071 
1072 #ifdef FEATURE_RUNTIME_PM
1073 int htc_pm_runtime_get(HTC_HANDLE htc_handle)
1074 {
1075 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1076 
1077 	return hif_pm_runtime_get(target->hif_dev);
1078 }
1079 
1080 int htc_pm_runtime_put(HTC_HANDLE htc_handle)
1081 {
1082 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1083 
1084 	return hif_pm_runtime_put(target->hif_dev);
1085 }
1086 #endif
1087 
1088 /**
1089  * htc_set_wmi_endpoint_count: Set number of WMI endpoint
1090  * @htc_handle: HTC handle
1091  * @wmi_ep_count: WMI enpoint count
1092  *
1093  * return: None
1094  */
1095 void htc_set_wmi_endpoint_count(HTC_HANDLE htc_handle, uint8_t wmi_ep_count)
1096 {
1097 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1098 
1099 	target->wmi_ep_count = wmi_ep_count;
1100 }
1101 
1102 /**
1103  * htc_get_wmi_endpoint_count: Get number of WMI endpoint
1104  * @htc_handle: HTC handle
1105  *
1106  * return: WMI enpoint count
1107  */
1108 uint8_t htc_get_wmi_endpoint_count(HTC_HANDLE htc_handle)
1109 {
1110 	HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
1111 
1112 	return target->wmi_ep_count;
1113 }
1114