htc.c 25 KB

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