htc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. 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. free_htc_packet_container(target, pPacket);
  244. }
  245. }
  246. #ifdef TODO_FIXME
  247. for (i = 0; i < NUM_CONTROL_TX_BUFFERS; i++) {
  248. pPacket = build_htc_tx_ctrl_packet();
  249. if (NULL == pPacket) {
  250. break;
  251. }
  252. htc_free_control_tx_packet(target, pPacket);
  253. }
  254. #endif
  255. /* setup HIF layer callbacks */
  256. qdf_mem_zero(&htcCallbacks, sizeof(struct hif_msg_callbacks));
  257. htcCallbacks.Context = target;
  258. htcCallbacks.rxCompletionHandler = htc_rx_completion_handler;
  259. htcCallbacks.txCompletionHandler = htc_tx_completion_handler;
  260. htcCallbacks.txResourceAvailHandler = htc_tx_resource_avail_handler;
  261. htcCallbacks.fwEventHandler = htc_fw_event_handler;
  262. target->hif_dev = ol_sc;
  263. /* Get HIF default pipe for HTC message exchange */
  264. pEndpoint = &target->endpoint[ENDPOINT_0];
  265. hif_post_init(target->hif_dev, target, &htcCallbacks);
  266. hif_get_default_pipe(target->hif_dev, &pEndpoint->UL_PipeID,
  267. &pEndpoint->DL_PipeID);
  268. } while (false);
  269. htc_recv_init(target);
  270. HTC_TRACE("-htc_create: (0x%p)", target);
  271. return (HTC_HANDLE) target;
  272. }
  273. void htc_destroy(HTC_HANDLE HTCHandle)
  274. {
  275. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  276. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  277. ("+htc_destroy .. Destroying :0x%p\n", target));
  278. hif_stop(htc_get_hif_device(HTCHandle));
  279. if (target)
  280. htc_cleanup(target);
  281. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_destroy\n"));
  282. }
  283. /* get the low level HIF device for the caller , the caller may wish to do low level
  284. * HIF requests */
  285. void *htc_get_hif_device(HTC_HANDLE HTCHandle)
  286. {
  287. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  288. return target->hif_dev;
  289. }
  290. static void htc_control_tx_complete(void *Context, HTC_PACKET *pPacket)
  291. {
  292. HTC_TARGET *target = (HTC_TARGET *) Context;
  293. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  294. ("+-htc_control_tx_complete 0x%p (l:%d) \n", pPacket,
  295. pPacket->ActualLength));
  296. htc_free_control_tx_packet(target, pPacket);
  297. }
  298. /* TODO, this is just a temporary max packet size */
  299. #define MAX_MESSAGE_SIZE 1536
  300. /**
  301. * htc_setup_epping_credit_allocation() - allocate credits/HTC buffers to WMI
  302. * @scn: pointer to hif_opaque_softc
  303. * @pEntry: pointer to tx credit allocation entry
  304. * @credits: number of credits
  305. *
  306. * Return: None
  307. */
  308. static void
  309. htc_setup_epping_credit_allocation(struct hif_opaque_softc *scn,
  310. HTC_SERVICE_TX_CREDIT_ALLOCATION *pEntry,
  311. int credits)
  312. {
  313. switch (hif_get_bus_type(scn)) {
  314. case QDF_BUS_TYPE_PCI:
  315. pEntry++;
  316. pEntry->service_id = WMI_DATA_BE_SVC;
  317. pEntry->CreditAllocation = (credits >> 1);
  318. pEntry++;
  319. pEntry->service_id = WMI_DATA_BK_SVC;
  320. pEntry->CreditAllocation = (credits >> 1);
  321. break;
  322. case QDF_BUS_TYPE_SDIO:
  323. pEntry++;
  324. pEntry->service_id = WMI_DATA_BE_SVC;
  325. pEntry->CreditAllocation = credits;
  326. break;
  327. default:
  328. break;
  329. }
  330. return;
  331. }
  332. /**
  333. * htc_setup_target_buffer_assignments() - setup target buffer assignments
  334. * @target: HTC Target Pointer
  335. *
  336. * Return: A_STATUS
  337. */
  338. static
  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. uint8_t i = 0;
  431. HTC_PACKET *rx_bundle_packet, *temp_bundle_packet;
  432. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  433. ("htc_wait_target - Enter (target:0x%p)\n", HTCHandle));
  434. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("+HWT\n"));
  435. do {
  436. status = hif_start(target->hif_dev);
  437. if (A_FAILED(status)) {
  438. AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("hif_start failed\n"));
  439. break;
  440. }
  441. status = htc_wait_recv_ctrl_message(target);
  442. if (A_FAILED(status)) {
  443. break;
  444. }
  445. if (target->CtrlResponseLength < (sizeof(HTC_READY_EX_MSG))) {
  446. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  447. ("Invalid HTC Ready Msg Len:%d! \n",
  448. target->CtrlResponseLength));
  449. status = A_ECOMM;
  450. break;
  451. }
  452. pReadyMsg = (HTC_READY_EX_MSG *) target->CtrlResponseBuffer;
  453. rdy_msg = &pReadyMsg->Version2_0_Info;
  454. htc_rdy_msg_id =
  455. HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, MESSAGEID);
  456. if (htc_rdy_msg_id != HTC_MSG_READY_ID) {
  457. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  458. ("Invalid HTC Ready Msg : 0x%X ! \n",
  459. htc_rdy_msg_id));
  460. status = A_ECOMM;
  461. break;
  462. }
  463. target->TotalTransmitCredits =
  464. HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITCOUNT);
  465. target->TargetCreditSize =
  466. (int)HTC_GET_FIELD(rdy_msg, HTC_READY_MSG, CREDITSIZE);
  467. target->MaxMsgsPerHTCBundle =
  468. (uint8_t) pReadyMsg->MaxMsgsPerHTCBundle;
  469. /* for old fw this value is set to 0. But the minimum value should be 1,
  470. * i.e., no bundling */
  471. if (target->MaxMsgsPerHTCBundle < 1)
  472. target->MaxMsgsPerHTCBundle = 1;
  473. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  474. ("Target Ready! : transmit resources : %d size:%d, MaxMsgsPerHTCBundle = %d\n",
  475. target->TotalTransmitCredits,
  476. target->TargetCreditSize,
  477. target->MaxMsgsPerHTCBundle));
  478. if ((0 == target->TotalTransmitCredits)
  479. || (0 == target->TargetCreditSize)) {
  480. status = A_ECOMM;
  481. break;
  482. }
  483. /* Allocate expected number of RX bundle buffer allocation */
  484. if (HTC_RX_BUNDLE_ENABLED(target)) {
  485. temp_bundle_packet = NULL;
  486. for (i = 0; i < MAX_HTC_RX_BUNDLE; i++) {
  487. rx_bundle_packet =
  488. allocate_htc_bundle_packet(target);
  489. if (rx_bundle_packet != NULL)
  490. rx_bundle_packet->ListLink.pNext =
  491. (DL_LIST *)temp_bundle_packet;
  492. else
  493. break;
  494. temp_bundle_packet = rx_bundle_packet;
  495. }
  496. target->pBundleFreeList = temp_bundle_packet;
  497. }
  498. /* done processing */
  499. target->CtrlResponseProcessing = false;
  500. htc_setup_target_buffer_assignments(target);
  501. /* setup our pseudo HTC control endpoint connection */
  502. qdf_mem_zero(&connect, sizeof(connect));
  503. qdf_mem_zero(&resp, sizeof(resp));
  504. connect.EpCallbacks.pContext = target;
  505. connect.EpCallbacks.EpTxComplete = htc_control_tx_complete;
  506. connect.EpCallbacks.EpRecv = htc_control_rx_complete;
  507. connect.MaxSendQueueDepth = NUM_CONTROL_TX_BUFFERS;
  508. connect.service_id = HTC_CTRL_RSVD_SVC;
  509. /* connect fake service */
  510. status = htc_connect_service((HTC_HANDLE) target,
  511. &connect, &resp);
  512. } while (false);
  513. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_wait_target - Exit (%d)\n", status));
  514. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("-HWT\n"));
  515. return status;
  516. }
  517. /* start HTC, this is called after all services are connected */
  518. static A_STATUS htc_config_target_hif_pipe(HTC_TARGET *target)
  519. {
  520. return A_OK;
  521. }
  522. static void reset_endpoint_states(HTC_TARGET *target)
  523. {
  524. HTC_ENDPOINT *pEndpoint;
  525. int i;
  526. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  527. pEndpoint = &target->endpoint[i];
  528. pEndpoint->service_id = 0;
  529. pEndpoint->MaxMsgLength = 0;
  530. pEndpoint->MaxTxQueueDepth = 0;
  531. pEndpoint->Id = i;
  532. INIT_HTC_PACKET_QUEUE(&pEndpoint->TxQueue);
  533. INIT_HTC_PACKET_QUEUE(&pEndpoint->TxLookupQueue);
  534. INIT_HTC_PACKET_QUEUE(&pEndpoint->RxBufferHoldQueue);
  535. pEndpoint->target = target;
  536. pEndpoint->TxCreditFlowEnabled = (bool)htc_credit_flow;
  537. qdf_atomic_init(&pEndpoint->TxProcessCount);
  538. }
  539. }
  540. /**
  541. * htc_start() - Main HTC function to trigger HTC start
  542. * @HTCHandle: pointer to HTC handle
  543. *
  544. * Return: A_OK for success or an appropriate A_STATUS error
  545. */
  546. A_STATUS htc_start(HTC_HANDLE HTCHandle)
  547. {
  548. qdf_nbuf_t netbuf;
  549. A_STATUS status = A_OK;
  550. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  551. HTC_SETUP_COMPLETE_EX_MSG *pSetupComp;
  552. HTC_PACKET *pSendPacket;
  553. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Enter\n"));
  554. do {
  555. htc_config_target_hif_pipe(target);
  556. /* allocate a buffer to send */
  557. pSendPacket = htc_alloc_control_tx_packet(target);
  558. if (NULL == pSendPacket) {
  559. AR_DEBUG_ASSERT(false);
  560. qdf_print("%s: allocControlTxPacket failed\n",
  561. __func__);
  562. status = A_NO_MEMORY;
  563. break;
  564. }
  565. netbuf =
  566. (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pSendPacket);
  567. /* assemble setup complete message */
  568. qdf_nbuf_put_tail(netbuf, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
  569. pSetupComp =
  570. (HTC_SETUP_COMPLETE_EX_MSG *) qdf_nbuf_data(netbuf);
  571. qdf_mem_zero(pSetupComp, sizeof(HTC_SETUP_COMPLETE_EX_MSG));
  572. HTC_SET_FIELD(pSetupComp, HTC_SETUP_COMPLETE_EX_MSG,
  573. MESSAGEID, HTC_MSG_SETUP_COMPLETE_EX_ID);
  574. if (!htc_credit_flow) {
  575. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  576. ("HTC will not use TX credit flow control\n"));
  577. pSetupComp->SetupFlags |=
  578. HTC_SETUP_COMPLETE_FLAGS_DISABLE_TX_CREDIT_FLOW;
  579. } else {
  580. AR_DEBUG_PRINTF(ATH_DEBUG_INIT,
  581. ("HTC using TX credit flow control\n"));
  582. }
  583. if ((hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO) ||
  584. (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
  585. if (HTC_RX_BUNDLE_ENABLED(target))
  586. pSetupComp->SetupFlags |=
  587. HTC_SETUP_COMPLETE_FLAGS_ENABLE_BUNDLE_RECV;
  588. hif_set_bundle_mode(target->hif_dev, true,
  589. HTC_MAX_MSG_PER_BUNDLE_RX);
  590. }
  591. SET_HTC_PACKET_INFO_TX(pSendPacket,
  592. NULL,
  593. (uint8_t *) pSetupComp,
  594. sizeof(HTC_SETUP_COMPLETE_EX_MSG),
  595. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  596. status = htc_send_pkt((HTC_HANDLE) target, pSendPacket);
  597. if (A_FAILED(status)) {
  598. break;
  599. }
  600. } while (false);
  601. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("htc_start Exit\n"));
  602. return status;
  603. }
  604. /*flush all queued buffers for surpriseremove case*/
  605. void htc_flush_surprise_remove(HTC_HANDLE HTCHandle)
  606. {
  607. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  608. int i;
  609. HTC_ENDPOINT *pEndpoint;
  610. #ifdef RX_SG_SUPPORT
  611. qdf_nbuf_t netbuf;
  612. qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
  613. #endif
  614. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_flush_surprise_remove\n"));
  615. /* cleanup endpoints */
  616. for (i = 0; i < ENDPOINT_MAX; i++) {
  617. pEndpoint = &target->endpoint[i];
  618. htc_flush_rx_hold_queue(target, pEndpoint);
  619. htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
  620. }
  621. hif_flush_surprise_remove(target->hif_dev);
  622. #ifdef RX_SG_SUPPORT
  623. LOCK_HTC_RX(target);
  624. while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
  625. qdf_nbuf_free(netbuf);
  626. RESET_RX_SG_CONFIG(target);
  627. UNLOCK_HTC_RX(target);
  628. #endif
  629. reset_endpoint_states(target);
  630. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_flush_surprise_remove \n"));
  631. }
  632. /* stop HTC communications, i.e. stop interrupt reception, and flush all queued buffers */
  633. void htc_stop(HTC_HANDLE HTCHandle)
  634. {
  635. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  636. int i;
  637. HTC_ENDPOINT *pEndpoint;
  638. #ifdef RX_SG_SUPPORT
  639. qdf_nbuf_t netbuf;
  640. qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
  641. #endif
  642. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+htc_stop\n"));
  643. /* cleanup endpoints */
  644. for (i = 0; i < ENDPOINT_MAX; i++) {
  645. pEndpoint = &target->endpoint[i];
  646. htc_flush_rx_hold_queue(target, pEndpoint);
  647. htc_flush_endpoint_tx(target, pEndpoint, HTC_TX_PACKET_TAG_ALL);
  648. if (pEndpoint->ul_is_polled) {
  649. qdf_timer_stop(&pEndpoint->ul_poll_timer);
  650. qdf_timer_free(&pEndpoint->ul_poll_timer);
  651. }
  652. }
  653. /* Note: htc_flush_endpoint_tx for all endpoints should be called before
  654. * hif_stop - otherwise htc_tx_completion_handler called from
  655. * hif_send_buffer_cleanup_on_pipe for residual tx frames in HIF layer,
  656. * might queue the packet again to HIF Layer - which could cause tx
  657. * buffer leak
  658. */
  659. hif_stop(target->hif_dev);
  660. #ifdef RX_SG_SUPPORT
  661. LOCK_HTC_RX(target);
  662. while ((netbuf = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
  663. qdf_nbuf_free(netbuf);
  664. RESET_RX_SG_CONFIG(target);
  665. UNLOCK_HTC_RX(target);
  666. #endif
  667. reset_endpoint_states(target);
  668. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_stop\n"));
  669. }
  670. void htc_dump_credit_states(HTC_HANDLE HTCHandle)
  671. {
  672. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  673. HTC_ENDPOINT *pEndpoint;
  674. int i;
  675. for (i = 0; i < ENDPOINT_MAX; i++) {
  676. pEndpoint = &target->endpoint[i];
  677. if (0 == pEndpoint->service_id)
  678. continue;
  679. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  680. ("--- EP : %d service_id: 0x%X --------------\n",
  681. pEndpoint->Id, pEndpoint->service_id));
  682. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  683. (" TxCredits : %d\n",
  684. pEndpoint->TxCredits));
  685. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  686. (" TxCreditSize : %d\n",
  687. pEndpoint->TxCreditSize));
  688. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  689. (" TxCreditsPerMaxMsg : %d\n",
  690. pEndpoint->TxCreditsPerMaxMsg));
  691. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  692. (" TxQueueDepth : %d\n",
  693. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  694. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  695. ("----------------------------------------------------\n"));
  696. }
  697. }
  698. bool htc_get_endpoint_statistics(HTC_HANDLE HTCHandle,
  699. HTC_ENDPOINT_ID Endpoint,
  700. HTC_ENDPOINT_STAT_ACTION Action,
  701. HTC_ENDPOINT_STATS *pStats)
  702. {
  703. #ifdef HTC_EP_STAT_PROFILING
  704. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  705. bool clearStats = false;
  706. bool sample = false;
  707. switch (Action) {
  708. case HTC_EP_STAT_SAMPLE:
  709. sample = true;
  710. break;
  711. case HTC_EP_STAT_SAMPLE_AND_CLEAR:
  712. sample = true;
  713. clearStats = true;
  714. break;
  715. case HTC_EP_STAT_CLEAR:
  716. clearStats = true;
  717. break;
  718. default:
  719. break;
  720. }
  721. A_ASSERT(Endpoint < ENDPOINT_MAX);
  722. /* lock out TX and RX while we sample and/or clear */
  723. LOCK_HTC_TX(target);
  724. LOCK_HTC_RX(target);
  725. if (sample) {
  726. A_ASSERT(pStats != NULL);
  727. /* return the stats to the caller */
  728. qdf_mem_copy(pStats, &target->endpoint[Endpoint].endpoint_stats,
  729. sizeof(HTC_ENDPOINT_STATS));
  730. }
  731. if (clearStats) {
  732. /* reset stats */
  733. qdf_mem_zero(&target->endpoint[Endpoint].endpoint_stats,
  734. sizeof(HTC_ENDPOINT_STATS));
  735. }
  736. UNLOCK_HTC_RX(target);
  737. UNLOCK_HTC_TX(target);
  738. return true;
  739. #else
  740. return false;
  741. #endif
  742. }
  743. void *htc_get_targetdef(HTC_HANDLE htc_handle)
  744. {
  745. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  746. return hif_get_targetdef(target->hif_dev);
  747. }
  748. #ifdef IPA_OFFLOAD
  749. /**
  750. * htc_ipa_get_ce_resource() - get uc resource on lower layer
  751. * @htc_handle: htc context
  752. * @ce_sr_base_paddr: copyengine source ring base physical address
  753. * @ce_sr_ring_size: copyengine source ring size
  754. * @ce_reg_paddr: copyengine register physical address
  755. *
  756. * Return: None
  757. */
  758. void htc_ipa_get_ce_resource(HTC_HANDLE htc_handle,
  759. qdf_dma_addr_t *ce_sr_base_paddr,
  760. uint32_t *ce_sr_ring_size,
  761. qdf_dma_addr_t *ce_reg_paddr)
  762. {
  763. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  764. if (target->hif_dev != NULL) {
  765. hif_ipa_get_ce_resource(target->hif_dev,
  766. ce_sr_base_paddr,
  767. ce_sr_ring_size, ce_reg_paddr);
  768. }
  769. }
  770. #endif /* IPA_OFFLOAD */
  771. #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
  772. void htc_dump_bundle_stats(HTC_HANDLE HTCHandle)
  773. {
  774. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  775. int total, i;
  776. total = 0;
  777. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
  778. total += target->rx_bundle_stats[i];
  779. if (total) {
  780. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("RX Bundle stats:\n"));
  781. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total RX packets: %d\n",
  782. total));
  783. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, (
  784. "Number of bundle: Number of packets\n"));
  785. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_RX; i++)
  786. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  787. ("%10d:%10d(%2d%s)\n", (i+1),
  788. target->rx_bundle_stats[i],
  789. ((target->rx_bundle_stats[i]*100)/
  790. total), "%"));
  791. }
  792. total = 0;
  793. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
  794. total += target->tx_bundle_stats[i];
  795. if (total) {
  796. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("TX Bundle stats:\n"));
  797. AR_DEBUG_PRINTF(ATH_DEBUG_ANY, ("Total TX packets: %d\n",
  798. total));
  799. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  800. ("Number of bundle: Number of packets\n"));
  801. for (i = 0; i < HTC_MAX_MSG_PER_BUNDLE_TX; i++)
  802. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  803. ("%10d:%10d(%2d%s)\n", (i+1),
  804. target->tx_bundle_stats[i],
  805. ((target->tx_bundle_stats[i]*100)/
  806. total), "%"));
  807. }
  808. }
  809. void htc_clear_bundle_stats(HTC_HANDLE HTCHandle)
  810. {
  811. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  812. qdf_mem_zero(&target->rx_bundle_stats, sizeof(target->rx_bundle_stats));
  813. qdf_mem_zero(&target->tx_bundle_stats, sizeof(target->tx_bundle_stats));
  814. }
  815. #endif
  816. /**
  817. * htc_vote_link_down - API to vote for link down
  818. * @htc_handle: HTC handle
  819. *
  820. * API for upper layers to call HIF to vote for link down
  821. *
  822. * Return: void
  823. */
  824. void htc_vote_link_down(HTC_HANDLE htc_handle)
  825. {
  826. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  827. if (!target->hif_dev)
  828. return;
  829. hif_vote_link_down(target->hif_dev);
  830. }
  831. /**
  832. * htc_vote_link_up - API to vote for link up
  833. * @htc_handle: HTC Handle
  834. *
  835. * API for upper layers to call HIF to vote for link up
  836. *
  837. * Return: void
  838. */
  839. void htc_vote_link_up(HTC_HANDLE htc_handle)
  840. {
  841. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  842. if (!target->hif_dev)
  843. return;
  844. hif_vote_link_up(target->hif_dev);
  845. }
  846. /**
  847. * htc_can_suspend_link - API to query HIF for link status
  848. * @htc_handle: HTC Handle
  849. *
  850. * API for upper layers to call HIF to query if the link can suspend
  851. *
  852. * Return: void
  853. */
  854. bool htc_can_suspend_link(HTC_HANDLE htc_handle)
  855. {
  856. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  857. if (!target->hif_dev)
  858. return false;
  859. return hif_can_suspend_link(target->hif_dev);
  860. }
  861. #ifdef FEATURE_RUNTIME_PM
  862. int htc_pm_runtime_get(HTC_HANDLE htc_handle)
  863. {
  864. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  865. HTC_INFO("%s: %pS\n", __func__, (void *)_RET_IP_);
  866. return hif_pm_runtime_get(target->hif_dev);
  867. }
  868. int htc_pm_runtime_put(HTC_HANDLE htc_handle)
  869. {
  870. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  871. HTC_INFO("%s: %pS\n", __func__, (void *)_RET_IP_);
  872. return hif_pm_runtime_put(target->hif_dev);
  873. }
  874. #endif