htc.c 23 KB

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