htc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Copyright (c) 2013-2016 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. #if defined(WLAN_DEBUG) || defined(DEBUG)
  32. static ATH_DEBUG_MASK_DESCRIPTION g_htc_debug_description[] = {
  33. {ATH_DEBUG_SEND, "Send"},
  34. {ATH_DEBUG_RECV, "Recv"},
  35. {ATH_DEBUG_SYNC, "Sync"},
  36. {ATH_DEBUG_DUMP, "Dump Data (RX or TX)"},
  37. {ATH_DEBUG_SETUP, "Setup"},
  38. };
  39. ATH_DEBUG_INSTANTIATE_MODULE_VAR(htc,
  40. "htc",
  41. "Host Target Communications",
  42. ATH_DEBUG_MASK_DEFAULTS | ATH_DEBUG_INFO |
  43. ATH_DEBUG_SETUP,
  44. ATH_DEBUG_DESCRIPTION_COUNT
  45. (g_htc_debug_description),
  46. g_htc_debug_description);
  47. #endif
  48. extern unsigned int htc_credit_flow;
  49. static void reset_endpoint_states(HTC_TARGET *target);
  50. static void destroy_htc_tx_ctrl_packet(HTC_PACKET *pPacket)
  51. {
  52. qdf_nbuf_t netbuf;
  53. netbuf = (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  54. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("free ctrl netbuf :0x%p\n", netbuf));
  55. if (netbuf != NULL) {
  56. qdf_nbuf_free(netbuf);
  57. }
  58. qdf_mem_free(pPacket);
  59. }
  60. static HTC_PACKET *build_htc_tx_ctrl_packet(qdf_device_t osdev)
  61. {
  62. HTC_PACKET *pPacket = NULL;
  63. qdf_nbuf_t netbuf;
  64. do {
  65. pPacket = (HTC_PACKET *) qdf_mem_malloc(sizeof(HTC_PACKET));
  66. if (NULL == pPacket) {
  67. break;
  68. }
  69. qdf_mem_zero(pPacket, sizeof(HTC_PACKET));
  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. qdf_mem_zero(target, sizeof(HTC_TARGET));
  226. htc_runtime_pm_init(target);
  227. qdf_spinlock_create(&target->HTCLock);
  228. qdf_spinlock_create(&target->HTCRxLock);
  229. qdf_spinlock_create(&target->HTCTxLock);
  230. qdf_spinlock_create(&target->HTCCreditLock);
  231. do {
  232. qdf_mem_copy(&target->HTCInitInfo, pInfo,
  233. sizeof(HTC_INIT_INFO));
  234. target->host_handle = pInfo->pContext;
  235. target->osdev = osdev;
  236. target->con_mode = con_mode;
  237. reset_endpoint_states(target);
  238. INIT_HTC_PACKET_QUEUE(&target->ControlBufferTXFreeList);
  239. for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
  240. HTC_PACKET *pPacket =
  241. (HTC_PACKET *) qdf_mem_malloc(sizeof(HTC_PACKET));
  242. if (pPacket != NULL) {
  243. qdf_mem_zero(pPacket, sizeof(HTC_PACKET));
  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. 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. A_STATUS htc_setup_target_buffer_assignments(HTC_TARGET *target)
  340. {
  341. HTC_SERVICE_TX_CREDIT_ALLOCATION *pEntry;
  342. A_STATUS status;
  343. int credits;
  344. int creditsPerMaxMsg;
  345. creditsPerMaxMsg = MAX_MESSAGE_SIZE / target->TargetCreditSize;
  346. if (MAX_MESSAGE_SIZE % target->TargetCreditSize) {
  347. creditsPerMaxMsg++;
  348. }
  349. /* TODO, this should be configured by the caller! */
  350. credits = target->TotalTransmitCredits;
  351. pEntry = &target->ServiceTxAllocTable[0];
  352. /*
  353. * Allocate all credists/HTC buffers to WMI.
  354. * no buffers are used/required for data. data always
  355. * remains on host.
  356. */
  357. status = A_OK;
  358. pEntry++;
  359. pEntry->service_id = WMI_CONTROL_SVC;
  360. pEntry->CreditAllocation = credits;
  361. if (HTC_IS_EPPING_ENABLED(target->con_mode)) {
  362. /* endpoint ping is a testing tool directly on top of HTC in
  363. * both target and host sides.
  364. * In target side, the endppint ping fw has no wlan stack and the
  365. * FW mboxping app directly sits on HTC and it simply drops
  366. * or loops back TX packets. For rx perf, FW mboxping app
  367. * generates packets and passes packets to HTC to send to host.
  368. * There is no WMI mesage exchanges between host and target
  369. * in endpoint ping case.
  370. * In host side, the endpoint ping driver is a Ethernet driver
  371. * and it directly sits on HTC. Only HIF, HTC, QDF, ADF are
  372. * used by the endpoint ping driver. There is no wifi stack
  373. * at all in host side also. For tx perf use case,
  374. * the user space mboxping app sends the raw packets to endpoint
  375. * ping driver and it directly forwards to HTC for transmission
  376. * to stress the bus. For the rx perf, HTC passes the received
  377. * packets to endpoint ping driver and it is passed to the user
  378. * space through the Ethernet interface.
  379. * For credit allocation, in SDIO bus case, only BE service is
  380. * used for tx/rx perf testing so that all credits are given
  381. * to BE service. In PCIe and USB bus case, endpoint ping uses both
  382. * BE and BK services to stress the bus so that the total credits
  383. * are equally distributed to BE and BK services.
  384. */
  385. htc_setup_epping_credit_allocation(target->hif_dev,
  386. pEntry, credits);
  387. }
  388. if (A_SUCCESS(status)) {
  389. int i;
  390. for (i = 0; i < HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
  391. if (target->ServiceTxAllocTable[i].service_id != 0) {
  392. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  393. ("HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n",
  394. i,
  395. target->ServiceTxAllocTable[i].
  396. service_id,
  397. target->ServiceTxAllocTable[i].
  398. CreditAllocation));
  399. }
  400. }
  401. }
  402. return status;
  403. }
  404. uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id)
  405. {
  406. uint8_t allocation = 0;
  407. int i;
  408. for (i = 0; i < HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
  409. if (target->ServiceTxAllocTable[i].service_id == service_id) {
  410. allocation =
  411. target->ServiceTxAllocTable[i].CreditAllocation;
  412. }
  413. }
  414. if (0 == allocation) {
  415. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  416. ("HTC Service TX : 0x%2.2X : allocation is zero!\n",
  417. service_id));
  418. }
  419. return allocation;
  420. }
  421. A_STATUS htc_wait_target(HTC_HANDLE HTCHandle)
  422. {
  423. A_STATUS status = A_OK;
  424. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  425. HTC_READY_EX_MSG *pReadyMsg;
  426. HTC_SERVICE_CONNECT_REQ connect;
  427. HTC_SERVICE_CONNECT_RESP resp;
  428. HTC_READY_MSG *rdy_msg;
  429. uint16_t htc_rdy_msg_id;
  430. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  431. ("htc_wait_target - Enter (target:0x%p) \n", HTCHandle));
  432. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("+HWT\n"));
  433. do {
  434. status = hif_start(target->hif_dev);
  435. if (A_FAILED(status)) {
  436. AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("hif_start failed\n"));
  437. break;
  438. }
  439. status = htc_wait_recv_ctrl_message(target);
  440. if (A_FAILED(status)) {
  441. break;
  442. }
  443. if (target->CtrlResponseLength < (sizeof(HTC_READY_EX_MSG))) {
  444. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  445. ("Invalid HTC Ready Msg Len:%d! \n",
  446. target->CtrlResponseLength));
  447. status = A_ECOMM;
  448. break;
  449. }
  450. pReadyMsg = (HTC_READY_EX_MSG *) target->CtrlResponseBuffer;
  451. rdy_msg = &pReadyMsg->Version2_0_Info;
  452. htc_rdy_msg_id =
  453. HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, MESSAGEID);
  454. if (htc_rdy_msg_id != HTC_MSG_READY_ID) {
  455. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  456. ("Invalid HTC Ready Msg : 0x%X ! \n",
  457. htc_rdy_msg_id));
  458. status = A_ECOMM;
  459. break;
  460. }
  461. target->TotalTransmitCredits =
  462. HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITCOUNT);
  463. target->TargetCreditSize =
  464. (int)HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITSIZE);
  465. target->MaxMsgsPerHTCBundle =
  466. (uint8_t) pReadyMsg->MaxMsgsPerHTCBundle;
  467. /* for old fw this value is set to 0. But the minimum value should be 1,
  468. * i.e., no bundling */
  469. if (target->MaxMsgsPerHTCBundle < 1)
  470. target->MaxMsgsPerHTCBundle = 1;
  471. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  472. ("Target Ready! : transmit resources : %d size:%d, MaxMsgsPerHTCBundle = %d\n",
  473. target->TotalTransmitCredits,
  474. target->TargetCreditSize,
  475. target->MaxMsgsPerHTCBundle));
  476. if ((0 == target->TotalTransmitCredits)
  477. || (0 == target->TargetCreditSize)) {
  478. status = A_ECOMM;
  479. break;
  480. }
  481. /* done processing */
  482. target->CtrlResponseProcessing = false;
  483. htc_setup_target_buffer_assignments(target);
  484. /* setup our pseudo HTC control endpoint connection */
  485. qdf_mem_zero(&connect, sizeof(connect));
  486. qdf_mem_zero(&resp, sizeof(resp));
  487. connect.EpCallbacks.pContext = target;
  488. connect.EpCallbacks.EpTxComplete = htc_control_tx_complete;
  489. connect.EpCallbacks.EpRecv = htc_control_rx_complete;
  490. connect.MaxSendQueueDepth = NUM_CONTROL_TX_BUFFERS;
  491. connect.service_id = HTC_CTRL_RSVD_SVC;
  492. /* connect fake service */
  493. status = htc_connect_service((HTC_HANDLE) target,
  494. &connect, &resp);
  495. } while (false);
  496. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_wait_target - Exit (%d)\n", status));
  497. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("-HWT\n"));
  498. return status;
  499. }
  500. /* start HTC, this is called after all services are connected */
  501. static A_STATUS htc_config_target_hif_pipe(HTC_TARGET *target)
  502. {
  503. return A_OK;
  504. }
  505. static void reset_endpoint_states(HTC_TARGET *target)
  506. {
  507. HTC_ENDPOINT *pEndpoint;
  508. int i;
  509. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  510. pEndpoint = &target->endpoint[i];
  511. pEndpoint->service_id = 0;
  512. pEndpoint->MaxMsgLength = 0;
  513. pEndpoint->MaxTxQueueDepth = 0;
  514. pEndpoint->Id = i;
  515. INIT_HTC_PACKET_QUEUE(&pEndpoint->TxQueue);
  516. INIT_HTC_PACKET_QUEUE(&pEndpoint->TxLookupQueue);
  517. INIT_HTC_PACKET_QUEUE(&pEndpoint->RxBufferHoldQueue);
  518. pEndpoint->target = target;
  519. pEndpoint->TxCreditFlowEnabled = (bool)htc_credit_flow;
  520. qdf_atomic_init(&pEndpoint->TxProcessCount);
  521. }
  522. }
  523. /**
  524. * htc_start() - Main HTC function to trigger HTC start
  525. * @HTCHandle: pointer to HTC handle
  526. *
  527. * Return: A_OK for success or an appropriate A_STATUS error
  528. */
  529. A_STATUS htc_start(HTC_HANDLE HTCHandle)
  530. {
  531. qdf_nbuf_t netbuf;
  532. A_STATUS status = A_OK;
  533. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  534. HTC_SETUP_COMPLETE_EX_MSG *pSetupComp;
  535. HTC_PACKET *pSendPacket;
  536. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Enter\n"));
  537. do {
  538. htc_config_target_hif_pipe(target);
  539. /* allocate a buffer to send */
  540. pSendPacket = htc_alloc_control_tx_packet(target);
  541. if (NULL == pSendPacket) {
  542. AR_DEBUG_ASSERT(false);
  543. qdf_print("%s: allocControlTxPacket failed\n",
  544. __func__);
  545. status = A_NO_MEMORY;
  546. break;
  547. }
  548. netbuf =
  549. (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pSendPacket);
  550. /* assemble setup complete message */
  551. qdf_nbuf_put_tail(netbuf, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
  552. pSetupComp =
  553. (HTC_SETUP_COMPLETE_EX_MSG *) qdf_nbuf_data(netbuf);
  554. qdf_mem_zero(pSetupComp, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
  555. HTC_SET_FIELD(pSetupComp, HTC_SETUP_COMPLETE_EX_MSG,
  556. MESSAGEID, HTC_MSG_SETUP_COMPLETE_EX_ID);
  557. if (!htc_credit_flow) {
  558. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  559. ("HTC will not use TX credit flow control\n"));
  560. pSetupComp->SetupFlags |=
  561. HTC_SETUP_COMPLETE_FLAGS_DISABLE_TX_CREDIT_FLOW;
  562. } else {
  563. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  564. ("HTC using TX credit flow control\n"));
  565. }
  566. if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO) ||
  567. (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
  568. if (HTC_RX_BUNDLE_ENABLED(target))
  569. pSetupComp->SetupFlags |=
  570. HTC_SETUP_COMPLETE_FLAGS_ENABLE_BUNDLE_RECV;
  571. hif_set_bundle_mode(target->hif_dev, true,
  572. HTC_MAX_MSG_PER_BUNDLE_RX);
  573. }
  574. SET_HTC_PACKET_INFO_TX(pSendPacket,
  575. NULL,
  576. (uint8_t *) pSetupComp,
  577. sizeof(HTC_SETUP_COMPLETE_EX_MSG),
  578. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  579. status = htc_send_pkt((HTC_HANDLE) target, pSendPacket);
  580. if (A_FAILED(status)) {
  581. break;
  582. }
  583. } while (false);
  584. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Exit\n"));
  585. return status;
  586. }
  587. /*flush all queued buffers for surpriseremove case*/
  588. void htc_flush_surprise_remove(HTC_HANDLE HTCHandle)
  589. {
  590. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  591. int i;
  592. HTC_ENDPOINT *pEndpoint;
  593. #ifdef RX_SG_SUPPORT
  594. qdf_nbuf_t netbuf;
  595. qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
  596. #endif
  597. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_flush_surprise_remove\n"));
  598. /* cleanup endpoints */
  599. for (i = 0; i < ENDPOINT_MAX; i++) {
  600. pEndpoint = &target->endpoint[i];
  601. htc_flush_rx_hold_queue(target, pEndpoint);
  602. htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
  603. }
  604. hif_flush_surprise_remove(target->hif_dev);
  605. #ifdef RX_SG_SUPPORT
  606. LOCK_HTC_RX(target);
  607. while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
  608. qdf_nbuf_free(netbuf);
  609. RESET_RX_SG_CONFIG(target);
  610. UNLOCK_HTC_RX(target);
  611. #endif
  612. reset_endpoint_states(target);
  613. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_flush_surprise_remove \n"));
  614. }
  615. /* stop HTC communications, i.e. stop interrupt reception, and flush all queued buffers */
  616. void htc_stop(HTC_HANDLE HTCHandle)
  617. {
  618. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  619. int i;
  620. HTC_ENDPOINT *pEndpoint;
  621. #ifdef RX_SG_SUPPORT
  622. qdf_nbuf_t netbuf;
  623. qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
  624. #endif
  625. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_stop\n"));
  626. /* cleanup endpoints */
  627. for (i = 0; i < ENDPOINT_MAX; i++) {
  628. pEndpoint = &target->endpoint[i];
  629. htc_flush_rx_hold_queue(target, pEndpoint);
  630. htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
  631. if (pEndpoint->ul_is_polled) {
  632. qdf_timer_stop(&pEndpoint->ul_poll_timer);
  633. qdf_timer_free(&pEndpoint->ul_poll_timer);
  634. }
  635. }
  636. /* Note: htc_flush_endpoint_tx for all endpoints should be called before
  637. * hif_stop - otherwise htc_tx_completion_handler called from
  638. * hif_send_buffer_cleanup_on_pipe for residual tx frames in HIF layer,
  639. * might queue the packet again to HIF Layer - which could cause tx
  640. * buffer leak
  641. */
  642. hif_stop(target->hif_dev);
  643. #ifdef RX_SG_SUPPORT
  644. LOCK_HTC_RX(target);
  645. while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
  646. qdf_nbuf_free(netbuf);
  647. RESET_RX_SG_CONFIG(target);
  648. UNLOCK_HTC_RX(target);
  649. #endif
  650. reset_endpoint_states(target);
  651. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_stop\n"));
  652. }
  653. void htc_dump_credit_states(HTC_HANDLE HTCHandle)
  654. {
  655. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  656. HTC_ENDPOINT *pEndpoint;
  657. int i;
  658. for (i = 0; i < ENDPOINT_MAX; i++) {
  659. pEndpoint = &target->endpoint[i];
  660. if (0 == pEndpoint->service_id)
  661. continue;
  662. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  663. ("--- EP : %d service_id: 0x%X --------------\n",
  664. pEndpoint->Id, pEndpoint->service_id));
  665. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  666. (" TxCredits : %d\n",
  667. pEndpoint->TxCredits));
  668. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  669. (" TxCreditSize : %d\n",
  670. pEndpoint->TxCreditSize));
  671. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  672. (" TxCreditsPerMaxMsg : %d\n",
  673. pEndpoint->TxCreditsPerMaxMsg));
  674. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  675. (" TxQueueDepth : %d\n",
  676. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  677. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  678. ("----------------------------------------------------\n"));
  679. }
  680. }
  681. bool htc_get_endpoint_statistics(HTC_HANDLE HTCHandle,
  682. HTC_ENDPOINT_ID Endpoint,
  683. HTC_ENDPOINT_STAT_ACTION Action,
  684. HTC_ENDPOINT_STATS *pStats)
  685. {
  686. #ifdef HTC_EP_STAT_PROFILING
  687. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  688. bool clearStats = false;
  689. bool sample = false;
  690. switch (Action) {
  691. case HTC_EP_STAT_SAMPLE:
  692. sample = true;
  693. break;
  694. case HTC_EP_STAT_SAMPLE_AND_CLEAR:
  695. sample = true;
  696. clearStats = true;
  697. break;
  698. case HTC_EP_STAT_CLEAR:
  699. clearStats = true;
  700. break;
  701. default:
  702. break;
  703. }
  704. A_ASSERT(Endpoint < ENDPOINT_MAX);
  705. /* lock out TX and RX while we sample and/or clear */
  706. LOCK_HTC_TX(target);
  707. LOCK_HTC_RX(target);
  708. if (sample) {
  709. A_ASSERT(pStats != NULL);
  710. /* return the stats to the caller */
  711. qdf_mem_copy(pStats, &target->endpoint[Endpoint].endpoint_stats,
  712. sizeof(HTC_ENDPOINT_STATS));
  713. }
  714. if (clearStats) {
  715. /* reset stats */
  716. qdf_mem_zero(&target->endpoint[Endpoint].endpoint_stats,
  717. sizeof(HTC_ENDPOINT_STATS));
  718. }
  719. UNLOCK_HTC_RX(target);
  720. UNLOCK_HTC_TX(target);
  721. return true;
  722. #else
  723. return false;
  724. #endif
  725. }
  726. void *htc_get_targetdef(HTC_HANDLE htc_handle)
  727. {
  728. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  729. return hif_get_targetdef(target->hif_dev);
  730. }
  731. #ifdef IPA_OFFLOAD
  732. /**
  733. * htc_ipa_get_ce_resource() - get uc resource on lower layer
  734. * @htc_handle: htc context
  735. * @ce_sr_base_paddr: copyengine source ring base physical address
  736. * @ce_sr_ring_size: copyengine source ring size
  737. * @ce_reg_paddr: copyengine register physical address
  738. *
  739. * Return: None
  740. */
  741. void htc_ipa_get_ce_resource(HTC_HANDLE htc_handle,
  742. qdf_dma_addr_t *ce_sr_base_paddr,
  743. uint32_t *ce_sr_ring_size,
  744. qdf_dma_addr_t *ce_reg_paddr)
  745. {
  746. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  747. if (target->hif_dev != NULL) {
  748. hif_ipa_get_ce_resource(target->hif_dev,
  749. ce_sr_base_paddr,
  750. ce_sr_ring_size, ce_reg_paddr);
  751. }
  752. }
  753. #endif /* IPA_OFFLOAD */
  754. #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
  755. void htc_dump_bundle_stats(HTC_HANDLE HTCHandle)
  756. {
  757. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  758. int total, i;
  759. total = 0;
  760. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
  761. total += target->rx_bundle_stats[i];
  762. if (total) {
  763. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("RX Bundle stats:\n"));
  764. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total RX packets: %d\n",
  765. total));
  766. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, (
  767. "Number of bundle: Number of packets\n"));
  768. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
  769. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  770. ("%10d:%10d(%2d%s)\n", (i+1),
  771. target->rx_bundle_stats[i],
  772. ((target->rx_bundle_stats[i]*100)/
  773. total), "%"));
  774. }
  775. total = 0;
  776. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
  777. total += target->tx_bundle_stats[i];
  778. if (total) {
  779. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("TX Bundle stats:\n"));
  780. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total TX packets: %d\n",
  781. total));
  782. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  783. ("Number of bundle: Number of packets\n"));
  784. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
  785. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  786. ("%10d:%10d(%2d%s)\n", (i+1),
  787. target->tx_bundle_stats[i],
  788. ((target->tx_bundle_stats[i]*100)/
  789. total), "%"));
  790. }
  791. }
  792. void htc_clear_bundle_stats(HTC_HANDLE HTCHandle)
  793. {
  794. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  795. qdf_mem_zero(&target->rx_bundle_stats, sizeof(target->rx_bundle_stats));
  796. qdf_mem_zero(&target->tx_bundle_stats, sizeof(target->tx_bundle_stats));
  797. }
  798. #endif
  799. /**
  800. * htc_vote_link_down - API to vote for link down
  801. * @htc_handle: HTC handle
  802. *
  803. * API for upper layers to call HIF to vote for link down
  804. *
  805. * Return: void
  806. */
  807. void htc_vote_link_down(HTC_HANDLE htc_handle)
  808. {
  809. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  810. if (!target->hif_dev)
  811. return;
  812. hif_vote_link_down(target->hif_dev);
  813. }
  814. /**
  815. * htc_vote_link_up - API to vote for link up
  816. * @htc_handle: HTC Handle
  817. *
  818. * API for upper layers to call HIF to vote for link up
  819. *
  820. * Return: void
  821. */
  822. void htc_vote_link_up(HTC_HANDLE htc_handle)
  823. {
  824. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  825. if (!target->hif_dev)
  826. return;
  827. hif_vote_link_up(target->hif_dev);
  828. }
  829. /**
  830. * htc_can_suspend_link - API to query HIF for link status
  831. * @htc_handle: HTC Handle
  832. *
  833. * API for upper layers to call HIF to query if the link can suspend
  834. *
  835. * Return: void
  836. */
  837. bool htc_can_suspend_link(HTC_HANDLE htc_handle)
  838. {
  839. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  840. if (!target->hif_dev)
  841. return false;
  842. return hif_can_suspend_link(target->hif_dev);
  843. }
  844. #ifdef FEATURE_RUNTIME_PM
  845. int htc_pm_runtime_get(HTC_HANDLE htc_handle)
  846. {
  847. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  848. HTC_INFO("%s: %pS\n", __func__, (void *)_RET_IP_);
  849. return hif_pm_runtime_get(target->hif_dev);
  850. }
  851. int htc_pm_runtime_put(HTC_HANDLE htc_handle)
  852. {
  853. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  854. HTC_INFO("%s: %pS\n", __func__, (void *)_RET_IP_);
  855. return hif_pm_runtime_put(target->hif_dev);
  856. }
  857. #endif