htc.c 24 KB

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