htc.c 22 KB

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