htc.c 22 KB

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