htc.c 21 KB

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