htc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 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_TEST_RAW_STREAMS:
  457. return "RAW";
  458. }
  459. return "Unknown";
  460. }
  461. static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
  462. {
  463. struct ath10k_htc_ep *ep;
  464. int i;
  465. for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
  466. ep = &htc->endpoint[i];
  467. ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
  468. ep->max_ep_message_len = 0;
  469. ep->max_tx_queue_depth = 0;
  470. ep->eid = i;
  471. ep->htc = htc;
  472. ep->tx_credit_flow_enabled = true;
  473. }
  474. }
  475. static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
  476. u16 service_id)
  477. {
  478. u8 allocation = 0;
  479. /* The WMI control service is the only service with flow control.
  480. * Let it have all transmit credits.
  481. */
  482. if (service_id == ATH10K_HTC_SVC_ID_WMI_CONTROL)
  483. allocation = htc->total_transmit_credits;
  484. return allocation;
  485. }
  486. int ath10k_htc_wait_target(struct ath10k_htc *htc)
  487. {
  488. struct ath10k *ar = htc->ar;
  489. int i, status = 0;
  490. unsigned long time_left;
  491. struct ath10k_htc_msg *msg;
  492. u16 message_id;
  493. time_left = wait_for_completion_timeout(&htc->ctl_resp,
  494. ATH10K_HTC_WAIT_TIMEOUT_HZ);
  495. if (!time_left) {
  496. /* Workaround: In some cases the PCI HIF doesn't
  497. * receive interrupt for the control response message
  498. * even if the buffer was completed. It is suspected
  499. * iomap writes unmasking PCI CE irqs aren't propagated
  500. * properly in KVM PCI-passthrough sometimes.
  501. */
  502. ath10k_warn(ar, "failed to receive control response completion, polling..\n");
  503. for (i = 0; i < CE_COUNT; i++)
  504. ath10k_hif_send_complete_check(htc->ar, i, 1);
  505. time_left =
  506. wait_for_completion_timeout(&htc->ctl_resp,
  507. ATH10K_HTC_WAIT_TIMEOUT_HZ);
  508. if (!time_left)
  509. status = -ETIMEDOUT;
  510. }
  511. if (status < 0) {
  512. ath10k_err(ar, "ctl_resp never came in (%d)\n", status);
  513. return status;
  514. }
  515. if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
  516. ath10k_err(ar, "Invalid HTC ready msg len:%d\n",
  517. htc->control_resp_len);
  518. return -ECOMM;
  519. }
  520. msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
  521. message_id = __le16_to_cpu(msg->hdr.message_id);
  522. if (message_id != ATH10K_HTC_MSG_READY_ID) {
  523. ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);
  524. return -ECOMM;
  525. }
  526. htc->total_transmit_credits = __le16_to_cpu(msg->ready.credit_count);
  527. htc->target_credit_size = __le16_to_cpu(msg->ready.credit_size);
  528. ath10k_dbg(ar, ATH10K_DBG_HTC,
  529. "Target ready! transmit resources: %d size:%d\n",
  530. htc->total_transmit_credits,
  531. htc->target_credit_size);
  532. if ((htc->total_transmit_credits == 0) ||
  533. (htc->target_credit_size == 0)) {
  534. ath10k_err(ar, "Invalid credit size received\n");
  535. return -ECOMM;
  536. }
  537. /* The only way to determine if the ready message is an extended
  538. * message is from the size.
  539. */
  540. if (htc->control_resp_len >=
  541. sizeof(msg->hdr) + sizeof(msg->ready_ext)) {
  542. htc->max_msgs_per_htc_bundle =
  543. min_t(u8, msg->ready_ext.max_msgs_per_htc_bundle,
  544. HTC_HOST_MAX_MSG_PER_BUNDLE);
  545. ath10k_dbg(ar, ATH10K_DBG_HTC,
  546. "Extended ready message. RX bundle size: %d\n",
  547. htc->max_msgs_per_htc_bundle);
  548. }
  549. return 0;
  550. }
  551. int ath10k_htc_connect_service(struct ath10k_htc *htc,
  552. struct ath10k_htc_svc_conn_req *conn_req,
  553. struct ath10k_htc_svc_conn_resp *conn_resp)
  554. {
  555. struct ath10k *ar = htc->ar;
  556. struct ath10k_htc_msg *msg;
  557. struct ath10k_htc_conn_svc *req_msg;
  558. struct ath10k_htc_conn_svc_response resp_msg_dummy;
  559. struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
  560. enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
  561. struct ath10k_htc_ep *ep;
  562. struct sk_buff *skb;
  563. unsigned int max_msg_size = 0;
  564. int length, status;
  565. unsigned long time_left;
  566. bool disable_credit_flow_ctrl = false;
  567. u16 message_id, service_id, flags = 0;
  568. u8 tx_alloc = 0;
  569. /* special case for HTC pseudo control service */
  570. if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
  571. disable_credit_flow_ctrl = true;
  572. assigned_eid = ATH10K_HTC_EP_0;
  573. max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
  574. memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
  575. goto setup;
  576. }
  577. tx_alloc = ath10k_htc_get_credit_allocation(htc,
  578. conn_req->service_id);
  579. if (!tx_alloc)
  580. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  581. "boot htc service %s does not allocate target credits\n",
  582. htc_service_name(conn_req->service_id));
  583. skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
  584. if (!skb) {
  585. ath10k_err(ar, "Failed to allocate HTC packet\n");
  586. return -ENOMEM;
  587. }
  588. length = sizeof(msg->hdr) + sizeof(msg->connect_service);
  589. skb_put(skb, length);
  590. memset(skb->data, 0, length);
  591. msg = (struct ath10k_htc_msg *)skb->data;
  592. msg->hdr.message_id =
  593. __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
  594. flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
  595. /* Only enable credit flow control for WMI ctrl service */
  596. if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
  597. flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
  598. disable_credit_flow_ctrl = true;
  599. }
  600. req_msg = &msg->connect_service;
  601. req_msg->flags = __cpu_to_le16(flags);
  602. req_msg->service_id = __cpu_to_le16(conn_req->service_id);
  603. reinit_completion(&htc->ctl_resp);
  604. status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
  605. if (status) {
  606. kfree_skb(skb);
  607. return status;
  608. }
  609. /* wait for response */
  610. time_left = wait_for_completion_timeout(&htc->ctl_resp,
  611. ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
  612. if (!time_left) {
  613. ath10k_err(ar, "Service connect timeout\n");
  614. return -ETIMEDOUT;
  615. }
  616. /* we controlled the buffer creation, it's aligned */
  617. msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
  618. resp_msg = &msg->connect_service_response;
  619. message_id = __le16_to_cpu(msg->hdr.message_id);
  620. service_id = __le16_to_cpu(resp_msg->service_id);
  621. if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
  622. (htc->control_resp_len < sizeof(msg->hdr) +
  623. sizeof(msg->connect_service_response))) {
  624. ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);
  625. return -EPROTO;
  626. }
  627. ath10k_dbg(ar, ATH10K_DBG_HTC,
  628. "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
  629. htc_service_name(service_id),
  630. resp_msg->status, resp_msg->eid);
  631. conn_resp->connect_resp_code = resp_msg->status;
  632. /* check response status */
  633. if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
  634. ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",
  635. htc_service_name(service_id),
  636. resp_msg->status);
  637. return -EPROTO;
  638. }
  639. assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
  640. max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
  641. setup:
  642. if (assigned_eid >= ATH10K_HTC_EP_COUNT)
  643. return -EPROTO;
  644. if (max_msg_size == 0)
  645. return -EPROTO;
  646. ep = &htc->endpoint[assigned_eid];
  647. ep->eid = assigned_eid;
  648. if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
  649. return -EPROTO;
  650. /* return assigned endpoint to caller */
  651. conn_resp->eid = assigned_eid;
  652. conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
  653. /* setup the endpoint */
  654. ep->service_id = conn_req->service_id;
  655. ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
  656. ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
  657. ep->tx_credits = tx_alloc;
  658. /* copy all the callbacks */
  659. ep->ep_ops = conn_req->ep_ops;
  660. status = ath10k_hif_map_service_to_pipe(htc->ar,
  661. ep->service_id,
  662. &ep->ul_pipe_id,
  663. &ep->dl_pipe_id);
  664. if (status)
  665. return status;
  666. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  667. "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
  668. htc_service_name(ep->service_id), ep->ul_pipe_id,
  669. ep->dl_pipe_id, ep->eid);
  670. if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
  671. ep->tx_credit_flow_enabled = false;
  672. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  673. "boot htc service '%s' eid %d TX flow control disabled\n",
  674. htc_service_name(ep->service_id), assigned_eid);
  675. }
  676. return status;
  677. }
  678. struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)
  679. {
  680. struct sk_buff *skb;
  681. skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
  682. if (!skb)
  683. return NULL;
  684. skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
  685. /* FW/HTC requires 4-byte aligned streams */
  686. if (!IS_ALIGNED((unsigned long)skb->data, 4))
  687. ath10k_warn(ar, "Unaligned HTC tx skb\n");
  688. return skb;
  689. }
  690. int ath10k_htc_start(struct ath10k_htc *htc)
  691. {
  692. struct ath10k *ar = htc->ar;
  693. struct sk_buff *skb;
  694. int status = 0;
  695. struct ath10k_htc_msg *msg;
  696. skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
  697. if (!skb)
  698. return -ENOMEM;
  699. skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
  700. memset(skb->data, 0, skb->len);
  701. msg = (struct ath10k_htc_msg *)skb->data;
  702. msg->hdr.message_id =
  703. __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
  704. if (ar->hif.bus == ATH10K_BUS_SDIO) {
  705. /* Extra setup params used by SDIO */
  706. msg->setup_complete_ext.flags =
  707. __cpu_to_le32(ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN);
  708. msg->setup_complete_ext.max_msgs_per_bundled_recv =
  709. htc->max_msgs_per_htc_bundle;
  710. }
  711. ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
  712. status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
  713. if (status) {
  714. kfree_skb(skb);
  715. return status;
  716. }
  717. return 0;
  718. }
  719. /* registered target arrival callback from the HIF layer */
  720. int ath10k_htc_init(struct ath10k *ar)
  721. {
  722. int status;
  723. struct ath10k_htc *htc = &ar->htc;
  724. struct ath10k_htc_svc_conn_req conn_req;
  725. struct ath10k_htc_svc_conn_resp conn_resp;
  726. spin_lock_init(&htc->tx_lock);
  727. ath10k_htc_reset_endpoint_states(htc);
  728. htc->ar = ar;
  729. /* setup our pseudo HTC control endpoint connection */
  730. memset(&conn_req, 0, sizeof(conn_req));
  731. memset(&conn_resp, 0, sizeof(conn_resp));
  732. conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
  733. conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
  734. conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
  735. conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
  736. /* connect fake service */
  737. status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
  738. if (status) {
  739. ath10k_err(ar, "could not connect to htc service (%d)\n",
  740. status);
  741. return status;
  742. }
  743. init_completion(&htc->ctl_resp);
  744. return 0;
  745. }