htc.c 21 KB

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