htc.c 25 KB

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