htc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "core.h"
  18. #include "hif.h"
  19. #include "debug.h"
  20. /********/
  21. /* Send */
  22. /********/
  23. static inline void ath10k_htc_send_complete_check(struct ath10k_htc_ep *ep,
  24. int force)
  25. {
  26. /*
  27. * Check whether HIF has any prior sends that have finished,
  28. * have not had the post-processing done.
  29. */
  30. ath10k_hif_send_complete_check(ep->htc->ar, ep->ul_pipe_id, force);
  31. }
  32. static void ath10k_htc_control_tx_complete(struct ath10k *ar,
  33. struct sk_buff *skb)
  34. {
  35. kfree_skb(skb);
  36. }
  37. static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)
  38. {
  39. struct sk_buff *skb;
  40. struct ath10k_skb_cb *skb_cb;
  41. skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);
  42. if (!skb)
  43. return NULL;
  44. skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
  45. WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
  46. skb_cb = ATH10K_SKB_CB(skb);
  47. memset(skb_cb, 0, sizeof(*skb_cb));
  48. ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);
  49. return skb;
  50. }
  51. static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
  52. struct sk_buff *skb)
  53. {
  54. struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
  55. dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
  56. skb_pull(skb, sizeof(struct ath10k_htc_hdr));
  57. }
  58. static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
  59. struct sk_buff *skb)
  60. {
  61. struct ath10k *ar = ep->htc->ar;
  62. ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
  63. ep->eid, skb);
  64. ath10k_htc_restore_tx_skb(ep->htc, skb);
  65. if (!ep->ep_ops.ep_tx_complete) {
  66. ath10k_warn(ar, "no tx handler for eid %d\n", ep->eid);
  67. dev_kfree_skb_any(skb);
  68. return;
  69. }
  70. ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
  71. }
  72. static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
  73. struct sk_buff *skb)
  74. {
  75. struct ath10k_htc_hdr *hdr;
  76. hdr = (struct ath10k_htc_hdr *)skb->data;
  77. hdr->eid = ep->eid;
  78. hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
  79. hdr->flags = 0;
  80. hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
  81. spin_lock_bh(&ep->htc->tx_lock);
  82. hdr->seq_no = ep->seq_no++;
  83. spin_unlock_bh(&ep->htc->tx_lock);
  84. }
  85. int ath10k_htc_send(struct ath10k_htc *htc,
  86. enum ath10k_htc_ep_id eid,
  87. struct sk_buff *skb)
  88. {
  89. struct ath10k *ar = htc->ar;
  90. struct ath10k_htc_ep *ep = &htc->endpoint[eid];
  91. struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
  92. struct ath10k_hif_sg_item sg_item;
  93. struct device *dev = htc->ar->dev;
  94. int credits = 0;
  95. int ret;
  96. if (htc->ar->state == ATH10K_STATE_WEDGED)
  97. return -ECOMM;
  98. if (eid >= ATH10K_HTC_EP_COUNT) {
  99. ath10k_warn(ar, "Invalid endpoint id: %d\n", eid);
  100. return -ENOENT;
  101. }
  102. skb_push(skb, sizeof(struct ath10k_htc_hdr));
  103. if (ep->tx_credit_flow_enabled) {
  104. credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
  105. spin_lock_bh(&htc->tx_lock);
  106. if (ep->tx_credits < credits) {
  107. spin_unlock_bh(&htc->tx_lock);
  108. ret = -EAGAIN;
  109. goto err_pull;
  110. }
  111. ep->tx_credits -= credits;
  112. ath10k_dbg(ar, ATH10K_DBG_HTC,
  113. "htc ep %d consumed %d credits (total %d)\n",
  114. eid, credits, ep->tx_credits);
  115. spin_unlock_bh(&htc->tx_lock);
  116. }
  117. ath10k_htc_prepare_tx_skb(ep, skb);
  118. skb_cb->eid = eid;
  119. skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
  120. ret = dma_mapping_error(dev, skb_cb->paddr);
  121. if (ret) {
  122. ret = -EIO;
  123. goto err_credits;
  124. }
  125. sg_item.transfer_id = ep->eid;
  126. sg_item.transfer_context = skb;
  127. sg_item.vaddr = skb->data;
  128. sg_item.paddr = skb_cb->paddr;
  129. sg_item.len = skb->len;
  130. ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
  131. if (ret)
  132. goto err_unmap;
  133. return 0;
  134. err_unmap:
  135. dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
  136. err_credits:
  137. if (ep->tx_credit_flow_enabled) {
  138. spin_lock_bh(&htc->tx_lock);
  139. ep->tx_credits += credits;
  140. ath10k_dbg(ar, ATH10K_DBG_HTC,
  141. "htc ep %d reverted %d credits back (total %d)\n",
  142. eid, credits, ep->tx_credits);
  143. spin_unlock_bh(&htc->tx_lock);
  144. if (ep->ep_ops.ep_tx_credits)
  145. ep->ep_ops.ep_tx_credits(htc->ar);
  146. }
  147. err_pull:
  148. skb_pull(skb, sizeof(struct ath10k_htc_hdr));
  149. return ret;
  150. }
  151. static int ath10k_htc_tx_completion_handler(struct ath10k *ar,
  152. struct sk_buff *skb)
  153. {
  154. struct ath10k_htc *htc = &ar->htc;
  155. struct ath10k_skb_cb *skb_cb;
  156. struct ath10k_htc_ep *ep;
  157. if (WARN_ON_ONCE(!skb))
  158. return 0;
  159. skb_cb = ATH10K_SKB_CB(skb);
  160. ep = &htc->endpoint[skb_cb->eid];
  161. ath10k_htc_notify_tx_completion(ep, skb);
  162. /* the skb now belongs to the completion handler */
  163. return 0;
  164. }
  165. /***********/
  166. /* Receive */
  167. /***********/
  168. static void
  169. ath10k_htc_process_credit_report(struct ath10k_htc *htc,
  170. const struct ath10k_htc_credit_report *report,
  171. int len,
  172. enum ath10k_htc_ep_id eid)
  173. {
  174. struct ath10k *ar = htc->ar;
  175. struct ath10k_htc_ep *ep;
  176. int i, n_reports;
  177. if (len % sizeof(*report))
  178. ath10k_warn(ar, "Uneven credit report len %d", len);
  179. n_reports = len / sizeof(*report);
  180. spin_lock_bh(&htc->tx_lock);
  181. for (i = 0; i < n_reports; i++, report++) {
  182. if (report->eid >= ATH10K_HTC_EP_COUNT)
  183. break;
  184. ep = &htc->endpoint[report->eid];
  185. ep->tx_credits += report->credits;
  186. ath10k_dbg(ar, ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
  187. report->eid, report->credits, ep->tx_credits);
  188. if (ep->ep_ops.ep_tx_credits) {
  189. spin_unlock_bh(&htc->tx_lock);
  190. ep->ep_ops.ep_tx_credits(htc->ar);
  191. spin_lock_bh(&htc->tx_lock);
  192. }
  193. }
  194. spin_unlock_bh(&htc->tx_lock);
  195. }
  196. static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
  197. u8 *buffer,
  198. int length,
  199. enum ath10k_htc_ep_id src_eid)
  200. {
  201. struct ath10k *ar = htc->ar;
  202. int status = 0;
  203. struct ath10k_htc_record *record;
  204. u8 *orig_buffer;
  205. int orig_length;
  206. size_t len;
  207. orig_buffer = buffer;
  208. orig_length = length;
  209. while (length > 0) {
  210. record = (struct ath10k_htc_record *)buffer;
  211. if (length < sizeof(record->hdr)) {
  212. status = -EINVAL;
  213. break;
  214. }
  215. if (record->hdr.len > length) {
  216. /* no room left in buffer for record */
  217. ath10k_warn(ar, "Invalid record length: %d\n",
  218. record->hdr.len);
  219. status = -EINVAL;
  220. break;
  221. }
  222. switch (record->hdr.id) {
  223. case ATH10K_HTC_RECORD_CREDITS:
  224. len = sizeof(struct ath10k_htc_credit_report);
  225. if (record->hdr.len < len) {
  226. ath10k_warn(ar, "Credit report too long\n");
  227. status = -EINVAL;
  228. break;
  229. }
  230. ath10k_htc_process_credit_report(htc,
  231. record->credit_report,
  232. record->hdr.len,
  233. src_eid);
  234. break;
  235. default:
  236. ath10k_warn(ar, "Unhandled record: id:%d length:%d\n",
  237. record->hdr.id, record->hdr.len);
  238. break;
  239. }
  240. if (status)
  241. break;
  242. /* multiple records may be present in a trailer */
  243. buffer += sizeof(record->hdr) + record->hdr.len;
  244. length -= sizeof(record->hdr) + record->hdr.len;
  245. }
  246. if (status)
  247. ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc rx bad trailer", "",
  248. orig_buffer, orig_length);
  249. return status;
  250. }
  251. static int ath10k_htc_rx_completion_handler(struct ath10k *ar,
  252. struct sk_buff *skb)
  253. {
  254. int status = 0;
  255. struct ath10k_htc *htc = &ar->htc;
  256. struct ath10k_htc_hdr *hdr;
  257. struct ath10k_htc_ep *ep;
  258. u16 payload_len;
  259. u32 trailer_len = 0;
  260. size_t min_len;
  261. u8 eid;
  262. bool trailer_present;
  263. hdr = (struct ath10k_htc_hdr *)skb->data;
  264. skb_pull(skb, sizeof(*hdr));
  265. eid = hdr->eid;
  266. if (eid >= ATH10K_HTC_EP_COUNT) {
  267. ath10k_warn(ar, "HTC Rx: invalid eid %d\n", eid);
  268. ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad header", "",
  269. hdr, sizeof(*hdr));
  270. status = -EINVAL;
  271. goto out;
  272. }
  273. ep = &htc->endpoint[eid];
  274. /*
  275. * If this endpoint that received a message from the target has
  276. * a to-target HIF pipe whose send completions are polled rather
  277. * than interrupt-driven, this is a good point to ask HIF to check
  278. * whether it has any completed sends to handle.
  279. */
  280. if (ep->ul_is_polled)
  281. ath10k_htc_send_complete_check(ep, 1);
  282. payload_len = __le16_to_cpu(hdr->len);
  283. if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
  284. ath10k_warn(ar, "HTC rx frame too long, len: %zu\n",
  285. payload_len + sizeof(*hdr));
  286. ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len", "",
  287. hdr, sizeof(*hdr));
  288. status = -EINVAL;
  289. goto out;
  290. }
  291. if (skb->len < payload_len) {
  292. ath10k_dbg(ar, ATH10K_DBG_HTC,
  293. "HTC Rx: insufficient length, got %d, expected %d\n",
  294. skb->len, payload_len);
  295. ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len",
  296. "", hdr, sizeof(*hdr));
  297. status = -EINVAL;
  298. goto out;
  299. }
  300. /* get flags to check for trailer */
  301. trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
  302. if (trailer_present) {
  303. u8 *trailer;
  304. trailer_len = hdr->trailer_len;
  305. min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
  306. if ((trailer_len < min_len) ||
  307. (trailer_len > payload_len)) {
  308. ath10k_warn(ar, "Invalid trailer length: %d\n",
  309. trailer_len);
  310. status = -EPROTO;
  311. goto out;
  312. }
  313. trailer = (u8 *)hdr;
  314. trailer += sizeof(*hdr);
  315. trailer += payload_len;
  316. trailer -= trailer_len;
  317. status = ath10k_htc_process_trailer(htc, trailer,
  318. trailer_len, hdr->eid);
  319. if (status)
  320. goto out;
  321. skb_trim(skb, skb->len - trailer_len);
  322. }
  323. if (((int)payload_len - (int)trailer_len) <= 0)
  324. /* zero length packet with trailer data, just drop these */
  325. goto out;
  326. if (eid == ATH10K_HTC_EP_0) {
  327. struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
  328. switch (__le16_to_cpu(msg->hdr.message_id)) {
  329. case ATH10K_HTC_MSG_READY_ID:
  330. case ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
  331. /* handle HTC control message */
  332. if (completion_done(&htc->ctl_resp)) {
  333. /*
  334. * this is a fatal error, target should not be
  335. * sending unsolicited messages on the ep 0
  336. */
  337. ath10k_warn(ar, "HTC rx ctrl still processing\n");
  338. status = -EINVAL;
  339. complete(&htc->ctl_resp);
  340. goto out;
  341. }
  342. htc->control_resp_len =
  343. min_t(int, skb->len,
  344. ATH10K_HTC_MAX_CTRL_MSG_LEN);
  345. memcpy(htc->control_resp_buffer, skb->data,
  346. htc->control_resp_len);
  347. complete(&htc->ctl_resp);
  348. break;
  349. case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
  350. htc->htc_ops.target_send_suspend_complete(ar);
  351. break;
  352. default:
  353. ath10k_warn(ar, "ignoring unsolicited htc ep0 event\n");
  354. break;
  355. }
  356. goto out;
  357. }
  358. ath10k_dbg(ar, ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
  359. eid, skb);
  360. ep->ep_ops.ep_rx_complete(ar, skb);
  361. /* skb is now owned by the rx completion handler */
  362. skb = NULL;
  363. out:
  364. kfree_skb(skb);
  365. return status;
  366. }
  367. static void ath10k_htc_control_rx_complete(struct ath10k *ar,
  368. struct sk_buff *skb)
  369. {
  370. /* This is unexpected. FW is not supposed to send regular rx on this
  371. * endpoint. */
  372. ath10k_warn(ar, "unexpected htc rx\n");
  373. kfree_skb(skb);
  374. }
  375. /***************/
  376. /* Init/Deinit */
  377. /***************/
  378. static const char *htc_service_name(enum ath10k_htc_svc_id id)
  379. {
  380. switch (id) {
  381. case ATH10K_HTC_SVC_ID_RESERVED:
  382. return "Reserved";
  383. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  384. return "Control";
  385. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  386. return "WMI";
  387. case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
  388. return "DATA BE";
  389. case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
  390. return "DATA BK";
  391. case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
  392. return "DATA VI";
  393. case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
  394. return "DATA VO";
  395. case ATH10K_HTC_SVC_ID_NMI_CONTROL:
  396. return "NMI Control";
  397. case ATH10K_HTC_SVC_ID_NMI_DATA:
  398. return "NMI Data";
  399. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  400. return "HTT Data";
  401. case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
  402. return "RAW";
  403. }
  404. return "Unknown";
  405. }
  406. static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
  407. {
  408. struct ath10k_htc_ep *ep;
  409. int i;
  410. for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
  411. ep = &htc->endpoint[i];
  412. ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
  413. ep->max_ep_message_len = 0;
  414. ep->max_tx_queue_depth = 0;
  415. ep->eid = i;
  416. ep->htc = htc;
  417. ep->tx_credit_flow_enabled = true;
  418. }
  419. }
  420. static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
  421. {
  422. struct ath10k_htc_svc_tx_credits *entry;
  423. entry = &htc->service_tx_alloc[0];
  424. /*
  425. * for PCIE allocate all credists/HTC buffers to WMI.
  426. * no buffers are used/required for data. data always
  427. * remains on host.
  428. */
  429. entry++;
  430. entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
  431. entry->credit_allocation = htc->total_transmit_credits;
  432. }
  433. static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
  434. u16 service_id)
  435. {
  436. u8 allocation = 0;
  437. int i;
  438. for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
  439. if (htc->service_tx_alloc[i].service_id == service_id)
  440. allocation =
  441. htc->service_tx_alloc[i].credit_allocation;
  442. }
  443. return allocation;
  444. }
  445. int ath10k_htc_wait_target(struct ath10k_htc *htc)
  446. {
  447. struct ath10k *ar = htc->ar;
  448. int i, status = 0;
  449. unsigned long time_left;
  450. struct ath10k_htc_svc_conn_req conn_req;
  451. struct ath10k_htc_svc_conn_resp conn_resp;
  452. struct ath10k_htc_msg *msg;
  453. u16 message_id;
  454. u16 credit_count;
  455. u16 credit_size;
  456. time_left = wait_for_completion_timeout(&htc->ctl_resp,
  457. ATH10K_HTC_WAIT_TIMEOUT_HZ);
  458. if (!time_left) {
  459. /* Workaround: In some cases the PCI HIF doesn't
  460. * receive interrupt for the control response message
  461. * even if the buffer was completed. It is suspected
  462. * iomap writes unmasking PCI CE irqs aren't propagated
  463. * properly in KVM PCI-passthrough sometimes.
  464. */
  465. ath10k_warn(ar, "failed to receive control response completion, polling..\n");
  466. for (i = 0; i < CE_COUNT; i++)
  467. ath10k_hif_send_complete_check(htc->ar, i, 1);
  468. time_left =
  469. wait_for_completion_timeout(&htc->ctl_resp,
  470. ATH10K_HTC_WAIT_TIMEOUT_HZ);
  471. if (!time_left)
  472. status = -ETIMEDOUT;
  473. }
  474. if (status < 0) {
  475. ath10k_err(ar, "ctl_resp never came in (%d)\n", status);
  476. return status;
  477. }
  478. if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
  479. ath10k_err(ar, "Invalid HTC ready msg len:%d\n",
  480. htc->control_resp_len);
  481. return -ECOMM;
  482. }
  483. msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
  484. message_id = __le16_to_cpu(msg->hdr.message_id);
  485. credit_count = __le16_to_cpu(msg->ready.credit_count);
  486. credit_size = __le16_to_cpu(msg->ready.credit_size);
  487. if (message_id != ATH10K_HTC_MSG_READY_ID) {
  488. ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);
  489. return -ECOMM;
  490. }
  491. htc->total_transmit_credits = credit_count;
  492. htc->target_credit_size = credit_size;
  493. ath10k_dbg(ar, ATH10K_DBG_HTC,
  494. "Target ready! transmit resources: %d size:%d\n",
  495. htc->total_transmit_credits,
  496. htc->target_credit_size);
  497. if ((htc->total_transmit_credits == 0) ||
  498. (htc->target_credit_size == 0)) {
  499. ath10k_err(ar, "Invalid credit size received\n");
  500. return -ECOMM;
  501. }
  502. ath10k_htc_setup_target_buffer_assignments(htc);
  503. /* setup our pseudo HTC control endpoint connection */
  504. memset(&conn_req, 0, sizeof(conn_req));
  505. memset(&conn_resp, 0, sizeof(conn_resp));
  506. conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
  507. conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
  508. conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
  509. conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
  510. /* connect fake service */
  511. status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
  512. if (status) {
  513. ath10k_err(ar, "could not connect to htc service (%d)\n",
  514. status);
  515. return status;
  516. }
  517. return 0;
  518. }
  519. int ath10k_htc_connect_service(struct ath10k_htc *htc,
  520. struct ath10k_htc_svc_conn_req *conn_req,
  521. struct ath10k_htc_svc_conn_resp *conn_resp)
  522. {
  523. struct ath10k *ar = htc->ar;
  524. struct ath10k_htc_msg *msg;
  525. struct ath10k_htc_conn_svc *req_msg;
  526. struct ath10k_htc_conn_svc_response resp_msg_dummy;
  527. struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
  528. enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
  529. struct ath10k_htc_ep *ep;
  530. struct sk_buff *skb;
  531. unsigned int max_msg_size = 0;
  532. int length, status;
  533. unsigned long time_left;
  534. bool disable_credit_flow_ctrl = false;
  535. u16 message_id, service_id, flags = 0;
  536. u8 tx_alloc = 0;
  537. /* special case for HTC pseudo control service */
  538. if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
  539. disable_credit_flow_ctrl = true;
  540. assigned_eid = ATH10K_HTC_EP_0;
  541. max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
  542. memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
  543. goto setup;
  544. }
  545. tx_alloc = ath10k_htc_get_credit_allocation(htc,
  546. conn_req->service_id);
  547. if (!tx_alloc)
  548. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  549. "boot htc service %s does not allocate target credits\n",
  550. htc_service_name(conn_req->service_id));
  551. skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
  552. if (!skb) {
  553. ath10k_err(ar, "Failed to allocate HTC packet\n");
  554. return -ENOMEM;
  555. }
  556. length = sizeof(msg->hdr) + sizeof(msg->connect_service);
  557. skb_put(skb, length);
  558. memset(skb->data, 0, length);
  559. msg = (struct ath10k_htc_msg *)skb->data;
  560. msg->hdr.message_id =
  561. __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
  562. flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
  563. /* Only enable credit flow control for WMI ctrl service */
  564. if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
  565. flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
  566. disable_credit_flow_ctrl = true;
  567. }
  568. req_msg = &msg->connect_service;
  569. req_msg->flags = __cpu_to_le16(flags);
  570. req_msg->service_id = __cpu_to_le16(conn_req->service_id);
  571. reinit_completion(&htc->ctl_resp);
  572. status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
  573. if (status) {
  574. kfree_skb(skb);
  575. return status;
  576. }
  577. /* wait for response */
  578. time_left = wait_for_completion_timeout(&htc->ctl_resp,
  579. ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
  580. if (!time_left) {
  581. ath10k_err(ar, "Service connect timeout\n");
  582. return -ETIMEDOUT;
  583. }
  584. /* we controlled the buffer creation, it's aligned */
  585. msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
  586. resp_msg = &msg->connect_service_response;
  587. message_id = __le16_to_cpu(msg->hdr.message_id);
  588. service_id = __le16_to_cpu(resp_msg->service_id);
  589. if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
  590. (htc->control_resp_len < sizeof(msg->hdr) +
  591. sizeof(msg->connect_service_response))) {
  592. ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);
  593. return -EPROTO;
  594. }
  595. ath10k_dbg(ar, ATH10K_DBG_HTC,
  596. "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
  597. htc_service_name(service_id),
  598. resp_msg->status, resp_msg->eid);
  599. conn_resp->connect_resp_code = resp_msg->status;
  600. /* check response status */
  601. if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
  602. ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",
  603. htc_service_name(service_id),
  604. resp_msg->status);
  605. return -EPROTO;
  606. }
  607. assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
  608. max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
  609. setup:
  610. if (assigned_eid >= ATH10K_HTC_EP_COUNT)
  611. return -EPROTO;
  612. if (max_msg_size == 0)
  613. return -EPROTO;
  614. ep = &htc->endpoint[assigned_eid];
  615. ep->eid = assigned_eid;
  616. if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
  617. return -EPROTO;
  618. /* return assigned endpoint to caller */
  619. conn_resp->eid = assigned_eid;
  620. conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
  621. /* setup the endpoint */
  622. ep->service_id = conn_req->service_id;
  623. ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
  624. ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
  625. ep->tx_credits = tx_alloc;
  626. ep->tx_credit_size = htc->target_credit_size;
  627. ep->tx_credits_per_max_message = ep->max_ep_message_len /
  628. htc->target_credit_size;
  629. if (ep->max_ep_message_len % htc->target_credit_size)
  630. ep->tx_credits_per_max_message++;
  631. /* copy all the callbacks */
  632. ep->ep_ops = conn_req->ep_ops;
  633. status = ath10k_hif_map_service_to_pipe(htc->ar,
  634. ep->service_id,
  635. &ep->ul_pipe_id,
  636. &ep->dl_pipe_id,
  637. &ep->ul_is_polled,
  638. &ep->dl_is_polled);
  639. if (status)
  640. return status;
  641. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  642. "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
  643. htc_service_name(ep->service_id), ep->ul_pipe_id,
  644. ep->dl_pipe_id, ep->eid);
  645. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  646. "boot htc ep %d ul polled %d dl polled %d\n",
  647. ep->eid, ep->ul_is_polled, ep->dl_is_polled);
  648. if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
  649. ep->tx_credit_flow_enabled = false;
  650. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  651. "boot htc service '%s' eid %d TX flow control disabled\n",
  652. htc_service_name(ep->service_id), assigned_eid);
  653. }
  654. return status;
  655. }
  656. struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)
  657. {
  658. struct sk_buff *skb;
  659. skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
  660. if (!skb)
  661. return NULL;
  662. skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
  663. /* FW/HTC requires 4-byte aligned streams */
  664. if (!IS_ALIGNED((unsigned long)skb->data, 4))
  665. ath10k_warn(ar, "Unaligned HTC tx skb\n");
  666. return skb;
  667. }
  668. int ath10k_htc_start(struct ath10k_htc *htc)
  669. {
  670. struct ath10k *ar = htc->ar;
  671. struct sk_buff *skb;
  672. int status = 0;
  673. struct ath10k_htc_msg *msg;
  674. skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
  675. if (!skb)
  676. return -ENOMEM;
  677. skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
  678. memset(skb->data, 0, skb->len);
  679. msg = (struct ath10k_htc_msg *)skb->data;
  680. msg->hdr.message_id =
  681. __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
  682. ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
  683. status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
  684. if (status) {
  685. kfree_skb(skb);
  686. return status;
  687. }
  688. return 0;
  689. }
  690. /* registered target arrival callback from the HIF layer */
  691. int ath10k_htc_init(struct ath10k *ar)
  692. {
  693. struct ath10k_hif_cb htc_callbacks;
  694. struct ath10k_htc_ep *ep = NULL;
  695. struct ath10k_htc *htc = &ar->htc;
  696. spin_lock_init(&htc->tx_lock);
  697. ath10k_htc_reset_endpoint_states(htc);
  698. /* setup HIF layer callbacks */
  699. htc_callbacks.rx_completion = ath10k_htc_rx_completion_handler;
  700. htc_callbacks.tx_completion = ath10k_htc_tx_completion_handler;
  701. htc->ar = ar;
  702. /* Get HIF default pipe for HTC message exchange */
  703. ep = &htc->endpoint[ATH10K_HTC_EP_0];
  704. ath10k_hif_set_callbacks(ar, &htc_callbacks);
  705. ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
  706. init_completion(&htc->ctl_resp);
  707. return 0;
  708. }