htc.c 28 KB

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