htc.c 27 KB

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