htc.c 29 KB

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