htc_pipe.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /*
  2. * Copyright (c) 2007-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "core.h"
  17. #include "debug.h"
  18. #include "hif-ops.h"
  19. #define HTC_PACKET_CONTAINER_ALLOCATION 32
  20. #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH)
  21. static int ath6kl_htc_pipe_tx(struct htc_target *handle,
  22. struct htc_packet *packet);
  23. static void ath6kl_htc_pipe_cleanup(struct htc_target *handle);
  24. /* htc pipe tx path */
  25. static inline void restore_tx_packet(struct htc_packet *packet)
  26. {
  27. if (packet->info.tx.flags & HTC_FLAGS_TX_FIXUP_NETBUF) {
  28. skb_pull(packet->skb, sizeof(struct htc_frame_hdr));
  29. packet->info.tx.flags &= ~HTC_FLAGS_TX_FIXUP_NETBUF;
  30. }
  31. }
  32. static void do_send_completion(struct htc_endpoint *ep,
  33. struct list_head *queue_to_indicate)
  34. {
  35. struct htc_packet *packet;
  36. if (list_empty(queue_to_indicate)) {
  37. /* nothing to indicate */
  38. return;
  39. }
  40. if (ep->ep_cb.tx_comp_multi != NULL) {
  41. ath6kl_dbg(ATH6KL_DBG_HTC,
  42. "%s: calling ep %d, send complete multiple callback (%d pkts)\n",
  43. __func__, ep->eid,
  44. get_queue_depth(queue_to_indicate));
  45. /*
  46. * a multiple send complete handler is being used,
  47. * pass the queue to the handler
  48. */
  49. ep->ep_cb.tx_comp_multi(ep->target, queue_to_indicate);
  50. /*
  51. * all packets are now owned by the callback,
  52. * reset queue to be safe
  53. */
  54. INIT_LIST_HEAD(queue_to_indicate);
  55. } else {
  56. /* using legacy EpTxComplete */
  57. do {
  58. packet = list_first_entry(queue_to_indicate,
  59. struct htc_packet, list);
  60. list_del(&packet->list);
  61. ath6kl_dbg(ATH6KL_DBG_HTC,
  62. "%s: calling ep %d send complete callback on packet 0x%p\n",
  63. __func__, ep->eid, packet);
  64. ep->ep_cb.tx_complete(ep->target, packet);
  65. } while (!list_empty(queue_to_indicate));
  66. }
  67. }
  68. static void send_packet_completion(struct htc_target *target,
  69. struct htc_packet *packet)
  70. {
  71. struct htc_endpoint *ep = &target->endpoint[packet->endpoint];
  72. struct list_head container;
  73. restore_tx_packet(packet);
  74. INIT_LIST_HEAD(&container);
  75. list_add_tail(&packet->list, &container);
  76. /* do completion */
  77. do_send_completion(ep, &container);
  78. }
  79. static void get_htc_packet_credit_based(struct htc_target *target,
  80. struct htc_endpoint *ep,
  81. struct list_head *queue)
  82. {
  83. int credits_required;
  84. int remainder;
  85. u8 send_flags;
  86. struct htc_packet *packet;
  87. unsigned int transfer_len;
  88. /* NOTE : the TX lock is held when this function is called */
  89. /* loop until we can grab as many packets out of the queue as we can */
  90. while (true) {
  91. send_flags = 0;
  92. if (list_empty(&ep->txq))
  93. break;
  94. /* get packet at head, but don't remove it */
  95. packet = list_first_entry(&ep->txq, struct htc_packet, list);
  96. ath6kl_dbg(ATH6KL_DBG_HTC,
  97. "%s: got head packet:0x%p , queue depth: %d\n",
  98. __func__, packet, get_queue_depth(&ep->txq));
  99. transfer_len = packet->act_len + HTC_HDR_LENGTH;
  100. if (transfer_len <= target->tgt_cred_sz) {
  101. credits_required = 1;
  102. } else {
  103. /* figure out how many credits this message requires */
  104. credits_required = transfer_len / target->tgt_cred_sz;
  105. remainder = transfer_len % target->tgt_cred_sz;
  106. if (remainder)
  107. credits_required++;
  108. }
  109. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: creds required:%d got:%d\n",
  110. __func__, credits_required, ep->cred_dist.credits);
  111. if (ep->eid == ENDPOINT_0) {
  112. /*
  113. * endpoint 0 is special, it always has a credit and
  114. * does not require credit based flow control
  115. */
  116. credits_required = 0;
  117. } else {
  118. if (ep->cred_dist.credits < credits_required)
  119. break;
  120. ep->cred_dist.credits -= credits_required;
  121. ep->ep_st.cred_cosumd += credits_required;
  122. /* check if we need credits back from the target */
  123. if (ep->cred_dist.credits <
  124. ep->cred_dist.cred_per_msg) {
  125. /* tell the target we need credits ASAP! */
  126. send_flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  127. ep->ep_st.cred_low_indicate += 1;
  128. ath6kl_dbg(ATH6KL_DBG_HTC,
  129. "%s: host needs credits\n",
  130. __func__);
  131. }
  132. }
  133. /* now we can fully dequeue */
  134. packet = list_first_entry(&ep->txq, struct htc_packet, list);
  135. list_del(&packet->list);
  136. /* save the number of credits this packet consumed */
  137. packet->info.tx.cred_used = credits_required;
  138. /* save send flags */
  139. packet->info.tx.flags = send_flags;
  140. packet->info.tx.seqno = ep->seqno;
  141. ep->seqno++;
  142. /* queue this packet into the caller's queue */
  143. list_add_tail(&packet->list, queue);
  144. }
  145. }
  146. static void get_htc_packet(struct htc_target *target,
  147. struct htc_endpoint *ep,
  148. struct list_head *queue, int resources)
  149. {
  150. struct htc_packet *packet;
  151. /* NOTE : the TX lock is held when this function is called */
  152. /* loop until we can grab as many packets out of the queue as we can */
  153. while (resources) {
  154. if (list_empty(&ep->txq))
  155. break;
  156. packet = list_first_entry(&ep->txq, struct htc_packet, list);
  157. list_del(&packet->list);
  158. ath6kl_dbg(ATH6KL_DBG_HTC,
  159. "%s: got packet:0x%p , new queue depth: %d\n",
  160. __func__, packet, get_queue_depth(&ep->txq));
  161. packet->info.tx.seqno = ep->seqno;
  162. packet->info.tx.flags = 0;
  163. packet->info.tx.cred_used = 0;
  164. ep->seqno++;
  165. /* queue this packet into the caller's queue */
  166. list_add_tail(&packet->list, queue);
  167. resources--;
  168. }
  169. }
  170. static int htc_issue_packets(struct htc_target *target,
  171. struct htc_endpoint *ep,
  172. struct list_head *pkt_queue)
  173. {
  174. int status = 0;
  175. u16 payload_len;
  176. struct sk_buff *skb;
  177. struct htc_frame_hdr *htc_hdr;
  178. struct htc_packet *packet;
  179. ath6kl_dbg(ATH6KL_DBG_HTC,
  180. "%s: queue: 0x%p, pkts %d\n", __func__,
  181. pkt_queue, get_queue_depth(pkt_queue));
  182. while (!list_empty(pkt_queue)) {
  183. packet = list_first_entry(pkt_queue, struct htc_packet, list);
  184. list_del(&packet->list);
  185. skb = packet->skb;
  186. if (!skb) {
  187. WARN_ON_ONCE(1);
  188. status = -EINVAL;
  189. break;
  190. }
  191. payload_len = packet->act_len;
  192. /* setup HTC frame header */
  193. htc_hdr = (struct htc_frame_hdr *) skb_push(skb,
  194. sizeof(*htc_hdr));
  195. if (!htc_hdr) {
  196. WARN_ON_ONCE(1);
  197. status = -EINVAL;
  198. break;
  199. }
  200. packet->info.tx.flags |= HTC_FLAGS_TX_FIXUP_NETBUF;
  201. /* Endianess? */
  202. put_unaligned((u16) payload_len, &htc_hdr->payld_len);
  203. htc_hdr->flags = packet->info.tx.flags;
  204. htc_hdr->eid = (u8) packet->endpoint;
  205. htc_hdr->ctrl[0] = 0;
  206. htc_hdr->ctrl[1] = (u8) packet->info.tx.seqno;
  207. spin_lock_bh(&target->tx_lock);
  208. /* store in look up queue to match completions */
  209. list_add_tail(&packet->list, &ep->pipe.tx_lookup_queue);
  210. ep->ep_st.tx_issued += 1;
  211. spin_unlock_bh(&target->tx_lock);
  212. status = ath6kl_hif_pipe_send(target->dev->ar,
  213. ep->pipe.pipeid_ul, NULL, skb);
  214. if (status != 0) {
  215. if (status != -ENOMEM) {
  216. /* TODO: if more than 1 endpoint maps to the
  217. * same PipeID, it is possible to run out of
  218. * resources in the HIF layer.
  219. * Don't emit the error
  220. */
  221. ath6kl_dbg(ATH6KL_DBG_HTC,
  222. "%s: failed status:%d\n",
  223. __func__, status);
  224. }
  225. spin_lock_bh(&target->tx_lock);
  226. list_del(&packet->list);
  227. /* reclaim credits */
  228. ep->cred_dist.credits += packet->info.tx.cred_used;
  229. spin_unlock_bh(&target->tx_lock);
  230. /* put it back into the callers queue */
  231. list_add(&packet->list, pkt_queue);
  232. break;
  233. }
  234. }
  235. if (status != 0) {
  236. while (!list_empty(pkt_queue)) {
  237. if (status != -ENOMEM) {
  238. ath6kl_dbg(ATH6KL_DBG_HTC,
  239. "%s: failed pkt:0x%p status:%d\n",
  240. __func__, packet, status);
  241. }
  242. packet = list_first_entry(pkt_queue,
  243. struct htc_packet, list);
  244. list_del(&packet->list);
  245. packet->status = status;
  246. send_packet_completion(target, packet);
  247. }
  248. }
  249. return status;
  250. }
  251. static enum htc_send_queue_result htc_try_send(struct htc_target *target,
  252. struct htc_endpoint *ep,
  253. struct list_head *txq)
  254. {
  255. struct list_head send_queue; /* temp queue to hold packets */
  256. struct htc_packet *packet, *tmp_pkt;
  257. struct ath6kl *ar = target->dev->ar;
  258. enum htc_send_full_action action;
  259. int tx_resources, overflow, txqueue_depth, i, good_pkts;
  260. u8 pipeid;
  261. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: (queue:0x%p depth:%d)\n",
  262. __func__, txq,
  263. (txq == NULL) ? 0 : get_queue_depth(txq));
  264. /* init the local send queue */
  265. INIT_LIST_HEAD(&send_queue);
  266. /*
  267. * txq equals to NULL means
  268. * caller didn't provide a queue, just wants us to
  269. * check queues and send
  270. */
  271. if (txq != NULL) {
  272. if (list_empty(txq)) {
  273. /* empty queue */
  274. return HTC_SEND_QUEUE_DROP;
  275. }
  276. spin_lock_bh(&target->tx_lock);
  277. txqueue_depth = get_queue_depth(&ep->txq);
  278. spin_unlock_bh(&target->tx_lock);
  279. if (txqueue_depth >= ep->max_txq_depth) {
  280. /* we've already overflowed */
  281. overflow = get_queue_depth(txq);
  282. } else {
  283. /* get how much we will overflow by */
  284. overflow = txqueue_depth;
  285. overflow += get_queue_depth(txq);
  286. /* get how much we will overflow the TX queue by */
  287. overflow -= ep->max_txq_depth;
  288. }
  289. /* if overflow is negative or zero, we are okay */
  290. if (overflow > 0) {
  291. ath6kl_dbg(ATH6KL_DBG_HTC,
  292. "%s: Endpoint %d, TX queue will overflow :%d, Tx Depth:%d, Max:%d\n",
  293. __func__, ep->eid, overflow, txqueue_depth,
  294. ep->max_txq_depth);
  295. }
  296. if ((overflow <= 0) ||
  297. (ep->ep_cb.tx_full == NULL)) {
  298. /*
  299. * all packets will fit or caller did not provide send
  300. * full indication handler -- just move all of them
  301. * to the local send_queue object
  302. */
  303. list_splice_tail_init(txq, &send_queue);
  304. } else {
  305. good_pkts = get_queue_depth(txq) - overflow;
  306. if (good_pkts < 0) {
  307. WARN_ON_ONCE(1);
  308. return HTC_SEND_QUEUE_DROP;
  309. }
  310. /* we have overflowed, and a callback is provided */
  311. /* dequeue all non-overflow packets to the sendqueue */
  312. for (i = 0; i < good_pkts; i++) {
  313. /* pop off caller's queue */
  314. packet = list_first_entry(txq,
  315. struct htc_packet,
  316. list);
  317. /* move to local queue */
  318. list_move_tail(&packet->list, &send_queue);
  319. }
  320. /*
  321. * the caller's queue has all the packets that won't fit
  322. * walk through the caller's queue and indicate each to
  323. * the send full handler
  324. */
  325. list_for_each_entry_safe(packet, tmp_pkt,
  326. txq, list) {
  327. ath6kl_dbg(ATH6KL_DBG_HTC,
  328. "%s: Indicat overflowed TX pkts: %p\n",
  329. __func__, packet);
  330. action = ep->ep_cb.tx_full(ep->target, packet);
  331. if (action == HTC_SEND_FULL_DROP) {
  332. /* callback wants the packet dropped */
  333. ep->ep_st.tx_dropped += 1;
  334. /* leave this one in the caller's queue
  335. * for cleanup */
  336. } else {
  337. /* callback wants to keep this packet,
  338. * move from caller's queue to the send
  339. * queue */
  340. list_move_tail(&packet->list,
  341. &send_queue);
  342. }
  343. }
  344. if (list_empty(&send_queue)) {
  345. /* no packets made it in, caller will cleanup */
  346. return HTC_SEND_QUEUE_DROP;
  347. }
  348. }
  349. }
  350. if (!ep->pipe.tx_credit_flow_enabled) {
  351. tx_resources =
  352. ath6kl_hif_pipe_get_free_queue_number(ar,
  353. ep->pipe.pipeid_ul);
  354. } else {
  355. tx_resources = 0;
  356. }
  357. spin_lock_bh(&target->tx_lock);
  358. if (!list_empty(&send_queue)) {
  359. /* transfer packets to tail */
  360. list_splice_tail_init(&send_queue, &ep->txq);
  361. if (!list_empty(&send_queue)) {
  362. WARN_ON_ONCE(1);
  363. spin_unlock_bh(&target->tx_lock);
  364. return HTC_SEND_QUEUE_DROP;
  365. }
  366. INIT_LIST_HEAD(&send_queue);
  367. }
  368. /* increment tx processing count on entry */
  369. ep->tx_proc_cnt++;
  370. if (ep->tx_proc_cnt > 1) {
  371. /*
  372. * Another thread or task is draining the TX queues on this
  373. * endpoint that thread will reset the tx processing count
  374. * when the queue is drained.
  375. */
  376. ep->tx_proc_cnt--;
  377. spin_unlock_bh(&target->tx_lock);
  378. return HTC_SEND_QUEUE_OK;
  379. }
  380. /***** beyond this point only 1 thread may enter ******/
  381. /*
  382. * Now drain the endpoint TX queue for transmission as long as we have
  383. * enough transmit resources.
  384. */
  385. while (true) {
  386. if (get_queue_depth(&ep->txq) == 0)
  387. break;
  388. if (ep->pipe.tx_credit_flow_enabled) {
  389. /*
  390. * Credit based mechanism provides flow control
  391. * based on target transmit resource availability,
  392. * we assume that the HIF layer will always have
  393. * bus resources greater than target transmit
  394. * resources.
  395. */
  396. get_htc_packet_credit_based(target, ep, &send_queue);
  397. } else {
  398. /*
  399. * Get all packets for this endpoint that we can
  400. * for this pass.
  401. */
  402. get_htc_packet(target, ep, &send_queue, tx_resources);
  403. }
  404. if (get_queue_depth(&send_queue) == 0) {
  405. /*
  406. * Didn't get packets due to out of resources or TX
  407. * queue was drained.
  408. */
  409. break;
  410. }
  411. spin_unlock_bh(&target->tx_lock);
  412. /* send what we can */
  413. htc_issue_packets(target, ep, &send_queue);
  414. if (!ep->pipe.tx_credit_flow_enabled) {
  415. pipeid = ep->pipe.pipeid_ul;
  416. tx_resources =
  417. ath6kl_hif_pipe_get_free_queue_number(ar, pipeid);
  418. }
  419. spin_lock_bh(&target->tx_lock);
  420. }
  421. /* done with this endpoint, we can clear the count */
  422. ep->tx_proc_cnt = 0;
  423. spin_unlock_bh(&target->tx_lock);
  424. return HTC_SEND_QUEUE_OK;
  425. }
  426. /* htc control packet manipulation */
  427. static void destroy_htc_txctrl_packet(struct htc_packet *packet)
  428. {
  429. struct sk_buff *skb;
  430. skb = packet->skb;
  431. dev_kfree_skb(skb);
  432. kfree(packet);
  433. }
  434. static struct htc_packet *build_htc_txctrl_packet(void)
  435. {
  436. struct htc_packet *packet = NULL;
  437. struct sk_buff *skb;
  438. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  439. if (packet == NULL)
  440. return NULL;
  441. skb = __dev_alloc_skb(HTC_CONTROL_BUFFER_SIZE, GFP_KERNEL);
  442. if (skb == NULL) {
  443. kfree(packet);
  444. return NULL;
  445. }
  446. packet->skb = skb;
  447. return packet;
  448. }
  449. static void htc_free_txctrl_packet(struct htc_target *target,
  450. struct htc_packet *packet)
  451. {
  452. destroy_htc_txctrl_packet(packet);
  453. }
  454. static struct htc_packet *htc_alloc_txctrl_packet(struct htc_target *target)
  455. {
  456. return build_htc_txctrl_packet();
  457. }
  458. static void htc_txctrl_complete(struct htc_target *target,
  459. struct htc_packet *packet)
  460. {
  461. htc_free_txctrl_packet(target, packet);
  462. }
  463. #define MAX_MESSAGE_SIZE 1536
  464. static int htc_setup_target_buffer_assignments(struct htc_target *target)
  465. {
  466. int status, credits, credit_per_maxmsg, i;
  467. struct htc_pipe_txcredit_alloc *entry;
  468. unsigned int hif_usbaudioclass = 0;
  469. credit_per_maxmsg = MAX_MESSAGE_SIZE / target->tgt_cred_sz;
  470. if (MAX_MESSAGE_SIZE % target->tgt_cred_sz)
  471. credit_per_maxmsg++;
  472. /* TODO, this should be configured by the caller! */
  473. credits = target->tgt_creds;
  474. entry = &target->pipe.txcredit_alloc[0];
  475. status = -ENOMEM;
  476. /* FIXME: hif_usbaudioclass is always zero */
  477. if (hif_usbaudioclass) {
  478. ath6kl_dbg(ATH6KL_DBG_HTC,
  479. "%s: For USB Audio Class- Total:%d\n",
  480. __func__, credits);
  481. entry++;
  482. entry++;
  483. /* Setup VO Service To have Max Credits */
  484. entry->service_id = WMI_DATA_VO_SVC;
  485. entry->credit_alloc = (credits - 6);
  486. if (entry->credit_alloc == 0)
  487. entry->credit_alloc++;
  488. credits -= (int) entry->credit_alloc;
  489. if (credits <= 0)
  490. return status;
  491. entry++;
  492. entry->service_id = WMI_CONTROL_SVC;
  493. entry->credit_alloc = credit_per_maxmsg;
  494. credits -= (int) entry->credit_alloc;
  495. if (credits <= 0)
  496. return status;
  497. /* leftovers go to best effort */
  498. entry++;
  499. entry++;
  500. entry->service_id = WMI_DATA_BE_SVC;
  501. entry->credit_alloc = (u8) credits;
  502. status = 0;
  503. } else {
  504. entry++;
  505. entry->service_id = WMI_DATA_VI_SVC;
  506. entry->credit_alloc = credits / 4;
  507. if (entry->credit_alloc == 0)
  508. entry->credit_alloc++;
  509. credits -= (int) entry->credit_alloc;
  510. if (credits <= 0)
  511. return status;
  512. entry++;
  513. entry->service_id = WMI_DATA_VO_SVC;
  514. entry->credit_alloc = credits / 4;
  515. if (entry->credit_alloc == 0)
  516. entry->credit_alloc++;
  517. credits -= (int) entry->credit_alloc;
  518. if (credits <= 0)
  519. return status;
  520. entry++;
  521. entry->service_id = WMI_CONTROL_SVC;
  522. entry->credit_alloc = credit_per_maxmsg;
  523. credits -= (int) entry->credit_alloc;
  524. if (credits <= 0)
  525. return status;
  526. entry++;
  527. entry->service_id = WMI_DATA_BK_SVC;
  528. entry->credit_alloc = credit_per_maxmsg;
  529. credits -= (int) entry->credit_alloc;
  530. if (credits <= 0)
  531. return status;
  532. /* leftovers go to best effort */
  533. entry++;
  534. entry->service_id = WMI_DATA_BE_SVC;
  535. entry->credit_alloc = (u8) credits;
  536. status = 0;
  537. }
  538. if (status == 0) {
  539. for (i = 0; i < ENDPOINT_MAX; i++) {
  540. if (target->pipe.txcredit_alloc[i].service_id != 0) {
  541. ath6kl_dbg(ATH6KL_DBG_HTC,
  542. "HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n",
  543. i,
  544. target->pipe.txcredit_alloc[i].
  545. service_id,
  546. target->pipe.txcredit_alloc[i].
  547. credit_alloc);
  548. }
  549. }
  550. }
  551. return status;
  552. }
  553. /* process credit reports and call distribution function */
  554. static void htc_process_credit_report(struct htc_target *target,
  555. struct htc_credit_report *rpt,
  556. int num_entries,
  557. enum htc_endpoint_id from_ep)
  558. {
  559. int total_credits = 0, i;
  560. struct htc_endpoint *ep;
  561. /* lock out TX while we update credits */
  562. spin_lock_bh(&target->tx_lock);
  563. for (i = 0; i < num_entries; i++, rpt++) {
  564. if (rpt->eid >= ENDPOINT_MAX) {
  565. WARN_ON_ONCE(1);
  566. spin_unlock_bh(&target->tx_lock);
  567. return;
  568. }
  569. ep = &target->endpoint[rpt->eid];
  570. ep->cred_dist.credits += rpt->credits;
  571. if (ep->cred_dist.credits && get_queue_depth(&ep->txq)) {
  572. spin_unlock_bh(&target->tx_lock);
  573. htc_try_send(target, ep, NULL);
  574. spin_lock_bh(&target->tx_lock);
  575. }
  576. total_credits += rpt->credits;
  577. }
  578. ath6kl_dbg(ATH6KL_DBG_HTC,
  579. "Report indicated %d credits to distribute\n",
  580. total_credits);
  581. spin_unlock_bh(&target->tx_lock);
  582. }
  583. /* flush endpoint TX queue */
  584. static void htc_flush_tx_endpoint(struct htc_target *target,
  585. struct htc_endpoint *ep, u16 tag)
  586. {
  587. struct htc_packet *packet;
  588. spin_lock_bh(&target->tx_lock);
  589. while (get_queue_depth(&ep->txq)) {
  590. packet = list_first_entry(&ep->txq, struct htc_packet, list);
  591. list_del(&packet->list);
  592. packet->status = 0;
  593. send_packet_completion(target, packet);
  594. }
  595. spin_unlock_bh(&target->tx_lock);
  596. }
  597. /*
  598. * In the adapted HIF layer, struct sk_buff * are passed between HIF and HTC,
  599. * since upper layers expects struct htc_packet containers we use the completed
  600. * skb and lookup it's corresponding HTC packet buffer from a lookup list.
  601. * This is extra overhead that can be fixed by re-aligning HIF interfaces with
  602. * HTC.
  603. */
  604. static struct htc_packet *htc_lookup_tx_packet(struct htc_target *target,
  605. struct htc_endpoint *ep,
  606. struct sk_buff *skb)
  607. {
  608. struct htc_packet *packet, *tmp_pkt, *found_packet = NULL;
  609. spin_lock_bh(&target->tx_lock);
  610. /*
  611. * interate from the front of tx lookup queue
  612. * this lookup should be fast since lower layers completes in-order and
  613. * so the completed packet should be at the head of the list generally
  614. */
  615. list_for_each_entry_safe(packet, tmp_pkt, &ep->pipe.tx_lookup_queue,
  616. list) {
  617. /* check for removal */
  618. if (skb == packet->skb) {
  619. /* found it */
  620. list_del(&packet->list);
  621. found_packet = packet;
  622. break;
  623. }
  624. }
  625. spin_unlock_bh(&target->tx_lock);
  626. return found_packet;
  627. }
  628. static int ath6kl_htc_pipe_tx_complete(struct ath6kl *ar, struct sk_buff *skb)
  629. {
  630. struct htc_target *target = ar->htc_target;
  631. struct htc_frame_hdr *htc_hdr;
  632. struct htc_endpoint *ep;
  633. struct htc_packet *packet;
  634. u8 ep_id, *netdata;
  635. u32 netlen;
  636. netdata = skb->data;
  637. netlen = skb->len;
  638. htc_hdr = (struct htc_frame_hdr *) netdata;
  639. ep_id = htc_hdr->eid;
  640. ep = &target->endpoint[ep_id];
  641. packet = htc_lookup_tx_packet(target, ep, skb);
  642. if (packet == NULL) {
  643. /* may have already been flushed and freed */
  644. ath6kl_err("HTC TX lookup failed!\n");
  645. } else {
  646. /* will be giving this buffer back to upper layers */
  647. packet->status = 0;
  648. send_packet_completion(target, packet);
  649. }
  650. skb = NULL;
  651. if (!ep->pipe.tx_credit_flow_enabled) {
  652. /*
  653. * note: when using TX credit flow, the re-checking of queues
  654. * happens when credits flow back from the target. in the
  655. * non-TX credit case, we recheck after the packet completes
  656. */
  657. htc_try_send(target, ep, NULL);
  658. }
  659. return 0;
  660. }
  661. static int htc_send_packets_multiple(struct htc_target *target,
  662. struct list_head *pkt_queue)
  663. {
  664. struct htc_endpoint *ep;
  665. struct htc_packet *packet, *tmp_pkt;
  666. if (list_empty(pkt_queue))
  667. return -EINVAL;
  668. /* get first packet to find out which ep the packets will go into */
  669. packet = list_first_entry(pkt_queue, struct htc_packet, list);
  670. if (packet->endpoint >= ENDPOINT_MAX) {
  671. WARN_ON_ONCE(1);
  672. return -EINVAL;
  673. }
  674. ep = &target->endpoint[packet->endpoint];
  675. htc_try_send(target, ep, pkt_queue);
  676. /* do completion on any packets that couldn't get in */
  677. if (!list_empty(pkt_queue)) {
  678. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  679. packet->status = -ENOMEM;
  680. }
  681. do_send_completion(ep, pkt_queue);
  682. }
  683. return 0;
  684. }
  685. /* htc pipe rx path */
  686. static struct htc_packet *alloc_htc_packet_container(struct htc_target *target)
  687. {
  688. struct htc_packet *packet;
  689. spin_lock_bh(&target->rx_lock);
  690. if (target->pipe.htc_packet_pool == NULL) {
  691. spin_unlock_bh(&target->rx_lock);
  692. return NULL;
  693. }
  694. packet = target->pipe.htc_packet_pool;
  695. target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next;
  696. spin_unlock_bh(&target->rx_lock);
  697. packet->list.next = NULL;
  698. return packet;
  699. }
  700. static void free_htc_packet_container(struct htc_target *target,
  701. struct htc_packet *packet)
  702. {
  703. struct list_head *lh;
  704. spin_lock_bh(&target->rx_lock);
  705. if (target->pipe.htc_packet_pool == NULL) {
  706. target->pipe.htc_packet_pool = packet;
  707. packet->list.next = NULL;
  708. } else {
  709. lh = (struct list_head *) target->pipe.htc_packet_pool;
  710. packet->list.next = lh;
  711. target->pipe.htc_packet_pool = packet;
  712. }
  713. spin_unlock_bh(&target->rx_lock);
  714. }
  715. static int htc_process_trailer(struct htc_target *target, u8 *buffer,
  716. int len, enum htc_endpoint_id from_ep)
  717. {
  718. struct htc_credit_report *report;
  719. struct htc_record_hdr *record;
  720. u8 *record_buf, *orig_buf;
  721. int orig_len, status;
  722. orig_buf = buffer;
  723. orig_len = len;
  724. status = 0;
  725. while (len > 0) {
  726. if (len < sizeof(struct htc_record_hdr)) {
  727. status = -EINVAL;
  728. break;
  729. }
  730. /* these are byte aligned structs */
  731. record = (struct htc_record_hdr *) buffer;
  732. len -= sizeof(struct htc_record_hdr);
  733. buffer += sizeof(struct htc_record_hdr);
  734. if (record->len > len) {
  735. /* no room left in buffer for record */
  736. ath6kl_dbg(ATH6KL_DBG_HTC,
  737. "invalid length: %d (id:%d) buffer has: %d bytes left\n",
  738. record->len, record->rec_id, len);
  739. status = -EINVAL;
  740. break;
  741. }
  742. /* start of record follows the header */
  743. record_buf = buffer;
  744. switch (record->rec_id) {
  745. case HTC_RECORD_CREDITS:
  746. if (record->len < sizeof(struct htc_credit_report)) {
  747. WARN_ON_ONCE(1);
  748. return -EINVAL;
  749. }
  750. report = (struct htc_credit_report *) record_buf;
  751. htc_process_credit_report(target, report,
  752. record->len / sizeof(*report),
  753. from_ep);
  754. break;
  755. default:
  756. ath6kl_dbg(ATH6KL_DBG_HTC,
  757. "unhandled record: id:%d length:%d\n",
  758. record->rec_id, record->len);
  759. break;
  760. }
  761. if (status != 0)
  762. break;
  763. /* advance buffer past this record for next time around */
  764. buffer += record->len;
  765. len -= record->len;
  766. }
  767. return status;
  768. }
  769. static void do_recv_completion(struct htc_endpoint *ep,
  770. struct list_head *queue_to_indicate)
  771. {
  772. struct htc_packet *packet;
  773. if (list_empty(queue_to_indicate)) {
  774. /* nothing to indicate */
  775. return;
  776. }
  777. /* using legacy EpRecv */
  778. while (!list_empty(queue_to_indicate)) {
  779. packet = list_first_entry(queue_to_indicate,
  780. struct htc_packet, list);
  781. list_del(&packet->list);
  782. ep->ep_cb.rx(ep->target, packet);
  783. }
  784. return;
  785. }
  786. static void recv_packet_completion(struct htc_target *target,
  787. struct htc_endpoint *ep,
  788. struct htc_packet *packet)
  789. {
  790. struct list_head container;
  791. INIT_LIST_HEAD(&container);
  792. list_add_tail(&packet->list, &container);
  793. /* do completion */
  794. do_recv_completion(ep, &container);
  795. }
  796. static int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb,
  797. u8 pipeid)
  798. {
  799. struct htc_target *target = ar->htc_target;
  800. u8 *netdata, *trailer, hdr_info;
  801. struct htc_frame_hdr *htc_hdr;
  802. u32 netlen, trailerlen = 0;
  803. struct htc_packet *packet;
  804. struct htc_endpoint *ep;
  805. u16 payload_len;
  806. int status = 0;
  807. /*
  808. * ar->htc_target can be NULL due to a race condition that can occur
  809. * during driver initialization(we do 'ath6kl_hif_power_on' before
  810. * initializing 'ar->htc_target' via 'ath6kl_htc_create').
  811. * 'ath6kl_hif_power_on' assigns 'ath6kl_recv_complete' as
  812. * usb_complete_t/callback function for 'usb_fill_bulk_urb'.
  813. * Thus the possibility of ar->htc_target being NULL
  814. * via ath6kl_recv_complete -> ath6kl_usb_io_comp_work.
  815. */
  816. if (WARN_ON_ONCE(!target)) {
  817. ath6kl_err("Target not yet initialized\n");
  818. status = -EINVAL;
  819. goto free_skb;
  820. }
  821. netdata = skb->data;
  822. netlen = skb->len;
  823. htc_hdr = (struct htc_frame_hdr *) netdata;
  824. ep = &target->endpoint[htc_hdr->eid];
  825. if (htc_hdr->eid >= ENDPOINT_MAX) {
  826. ath6kl_dbg(ATH6KL_DBG_HTC,
  827. "HTC Rx: invalid EndpointID=%d\n",
  828. htc_hdr->eid);
  829. status = -EINVAL;
  830. goto free_skb;
  831. }
  832. payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
  833. if (netlen < (payload_len + HTC_HDR_LENGTH)) {
  834. ath6kl_dbg(ATH6KL_DBG_HTC,
  835. "HTC Rx: insufficient length, got:%d expected =%u\n",
  836. netlen, payload_len + HTC_HDR_LENGTH);
  837. status = -EINVAL;
  838. goto free_skb;
  839. }
  840. /* get flags to check for trailer */
  841. hdr_info = htc_hdr->flags;
  842. if (hdr_info & HTC_FLG_RX_TRAILER) {
  843. /* extract the trailer length */
  844. hdr_info = htc_hdr->ctrl[0];
  845. if ((hdr_info < sizeof(struct htc_record_hdr)) ||
  846. (hdr_info > payload_len)) {
  847. ath6kl_dbg(ATH6KL_DBG_HTC,
  848. "invalid header: payloadlen should be %d, CB[0]: %d\n",
  849. payload_len, hdr_info);
  850. status = -EINVAL;
  851. goto free_skb;
  852. }
  853. trailerlen = hdr_info;
  854. /* process trailer after hdr/apps payload */
  855. trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH +
  856. payload_len - hdr_info;
  857. status = htc_process_trailer(target, trailer, hdr_info,
  858. htc_hdr->eid);
  859. if (status != 0)
  860. goto free_skb;
  861. }
  862. if (((int) payload_len - (int) trailerlen) <= 0) {
  863. /* zero length packet with trailer, just drop these */
  864. goto free_skb;
  865. }
  866. if (htc_hdr->eid == ENDPOINT_0) {
  867. /* handle HTC control message */
  868. if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) {
  869. /*
  870. * fatal: target should not send unsolicited
  871. * messageson the endpoint 0
  872. */
  873. ath6kl_dbg(ATH6KL_DBG_HTC,
  874. "HTC ignores Rx Ctrl after setup complete\n");
  875. status = -EINVAL;
  876. goto free_skb;
  877. }
  878. /* remove HTC header */
  879. skb_pull(skb, HTC_HDR_LENGTH);
  880. netdata = skb->data;
  881. netlen = skb->len;
  882. spin_lock_bh(&target->rx_lock);
  883. target->pipe.ctrl_response_valid = true;
  884. target->pipe.ctrl_response_len = min_t(int, netlen,
  885. HTC_MAX_CTRL_MSG_LEN);
  886. memcpy(target->pipe.ctrl_response_buf, netdata,
  887. target->pipe.ctrl_response_len);
  888. spin_unlock_bh(&target->rx_lock);
  889. dev_kfree_skb(skb);
  890. skb = NULL;
  891. goto free_skb;
  892. }
  893. /*
  894. * TODO: the message based HIF architecture allocates net bufs
  895. * for recv packets since it bridges that HIF to upper layers,
  896. * which expects HTC packets, we form the packets here
  897. */
  898. packet = alloc_htc_packet_container(target);
  899. if (packet == NULL) {
  900. status = -ENOMEM;
  901. goto free_skb;
  902. }
  903. packet->status = 0;
  904. packet->endpoint = htc_hdr->eid;
  905. packet->pkt_cntxt = skb;
  906. /* TODO: for backwards compatibility */
  907. packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH;
  908. packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen;
  909. /*
  910. * TODO: this is a hack because the driver layer will set the
  911. * actual len of the skb again which will just double the len
  912. */
  913. skb_trim(skb, 0);
  914. recv_packet_completion(target, ep, packet);
  915. /* recover the packet container */
  916. free_htc_packet_container(target, packet);
  917. skb = NULL;
  918. free_skb:
  919. dev_kfree_skb(skb);
  920. return status;
  921. }
  922. static void htc_flush_rx_queue(struct htc_target *target,
  923. struct htc_endpoint *ep)
  924. {
  925. struct list_head container;
  926. struct htc_packet *packet;
  927. spin_lock_bh(&target->rx_lock);
  928. while (1) {
  929. if (list_empty(&ep->rx_bufq))
  930. break;
  931. packet = list_first_entry(&ep->rx_bufq,
  932. struct htc_packet, list);
  933. list_del(&packet->list);
  934. spin_unlock_bh(&target->rx_lock);
  935. packet->status = -ECANCELED;
  936. packet->act_len = 0;
  937. ath6kl_dbg(ATH6KL_DBG_HTC,
  938. "Flushing RX packet:0x%p, length:%d, ep:%d\n",
  939. packet, packet->buf_len,
  940. packet->endpoint);
  941. INIT_LIST_HEAD(&container);
  942. list_add_tail(&packet->list, &container);
  943. /* give the packet back */
  944. do_recv_completion(ep, &container);
  945. spin_lock_bh(&target->rx_lock);
  946. }
  947. spin_unlock_bh(&target->rx_lock);
  948. }
  949. /* polling routine to wait for a control packet to be received */
  950. static int htc_wait_recv_ctrl_message(struct htc_target *target)
  951. {
  952. int count = HTC_TARGET_RESPONSE_POLL_COUNT;
  953. while (count > 0) {
  954. spin_lock_bh(&target->rx_lock);
  955. if (target->pipe.ctrl_response_valid) {
  956. target->pipe.ctrl_response_valid = false;
  957. spin_unlock_bh(&target->rx_lock);
  958. break;
  959. }
  960. spin_unlock_bh(&target->rx_lock);
  961. count--;
  962. msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT);
  963. }
  964. if (count <= 0) {
  965. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: Timeout!\n", __func__);
  966. return -ECOMM;
  967. }
  968. return 0;
  969. }
  970. static void htc_rxctrl_complete(struct htc_target *context,
  971. struct htc_packet *packet)
  972. {
  973. /* TODO, can't really receive HTC control messages yet.... */
  974. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: invalid call function\n", __func__);
  975. }
  976. /* htc pipe initialization */
  977. static void reset_endpoint_states(struct htc_target *target)
  978. {
  979. struct htc_endpoint *ep;
  980. int i;
  981. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  982. ep = &target->endpoint[i];
  983. ep->svc_id = 0;
  984. ep->len_max = 0;
  985. ep->max_txq_depth = 0;
  986. ep->eid = i;
  987. INIT_LIST_HEAD(&ep->txq);
  988. INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
  989. INIT_LIST_HEAD(&ep->rx_bufq);
  990. ep->target = target;
  991. ep->pipe.tx_credit_flow_enabled = true;
  992. }
  993. }
  994. /* start HTC, this is called after all services are connected */
  995. static int htc_config_target_hif_pipe(struct htc_target *target)
  996. {
  997. return 0;
  998. }
  999. /* htc service functions */
  1000. static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
  1001. {
  1002. u8 allocation = 0;
  1003. int i;
  1004. for (i = 0; i < ENDPOINT_MAX; i++) {
  1005. if (target->pipe.txcredit_alloc[i].service_id == service_id)
  1006. allocation =
  1007. target->pipe.txcredit_alloc[i].credit_alloc;
  1008. }
  1009. if (allocation == 0) {
  1010. ath6kl_dbg(ATH6KL_DBG_HTC,
  1011. "HTC Service TX : 0x%2.2X : allocation is zero!\n",
  1012. service_id);
  1013. }
  1014. return allocation;
  1015. }
  1016. static int ath6kl_htc_pipe_conn_service(struct htc_target *target,
  1017. struct htc_service_connect_req *conn_req,
  1018. struct htc_service_connect_resp *conn_resp)
  1019. {
  1020. struct ath6kl *ar = target->dev->ar;
  1021. struct htc_packet *packet = NULL;
  1022. struct htc_conn_service_resp *resp_msg;
  1023. struct htc_conn_service_msg *conn_msg;
  1024. enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
  1025. bool disable_credit_flowctrl = false;
  1026. unsigned int max_msg_size = 0;
  1027. struct htc_endpoint *ep;
  1028. int length, status = 0;
  1029. struct sk_buff *skb;
  1030. u8 tx_alloc;
  1031. u16 flags;
  1032. if (conn_req->svc_id == 0) {
  1033. WARN_ON_ONCE(1);
  1034. status = -EINVAL;
  1035. goto free_packet;
  1036. }
  1037. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1038. /* special case for pseudo control service */
  1039. assigned_epid = ENDPOINT_0;
  1040. max_msg_size = HTC_MAX_CTRL_MSG_LEN;
  1041. tx_alloc = 0;
  1042. } else {
  1043. tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
  1044. if (tx_alloc == 0) {
  1045. status = -ENOMEM;
  1046. goto free_packet;
  1047. }
  1048. /* allocate a packet to send to the target */
  1049. packet = htc_alloc_txctrl_packet(target);
  1050. if (packet == NULL) {
  1051. WARN_ON_ONCE(1);
  1052. status = -ENOMEM;
  1053. goto free_packet;
  1054. }
  1055. skb = packet->skb;
  1056. length = sizeof(struct htc_conn_service_msg);
  1057. /* assemble connect service message */
  1058. conn_msg = (struct htc_conn_service_msg *) skb_put(skb,
  1059. length);
  1060. if (conn_msg == NULL) {
  1061. WARN_ON_ONCE(1);
  1062. status = -EINVAL;
  1063. goto free_packet;
  1064. }
  1065. memset(conn_msg, 0,
  1066. sizeof(struct htc_conn_service_msg));
  1067. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1068. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1069. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
  1070. ~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
  1071. /* tell target desired recv alloc for this ep */
  1072. flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
  1073. conn_msg->conn_flags |= cpu_to_le16(flags);
  1074. if (conn_req->conn_flags &
  1075. HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
  1076. disable_credit_flowctrl = true;
  1077. }
  1078. set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
  1079. length,
  1080. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1081. status = ath6kl_htc_pipe_tx(target, packet);
  1082. /* we don't own it anymore */
  1083. packet = NULL;
  1084. if (status != 0)
  1085. goto free_packet;
  1086. /* wait for response */
  1087. status = htc_wait_recv_ctrl_message(target);
  1088. if (status != 0)
  1089. goto free_packet;
  1090. /* we controlled the buffer creation so it has to be
  1091. * properly aligned
  1092. */
  1093. resp_msg = (struct htc_conn_service_resp *)
  1094. target->pipe.ctrl_response_buf;
  1095. if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
  1096. (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
  1097. /* this message is not valid */
  1098. WARN_ON_ONCE(1);
  1099. status = -EINVAL;
  1100. goto free_packet;
  1101. }
  1102. ath6kl_dbg(ATH6KL_DBG_TRC,
  1103. "%s: service 0x%X conn resp: status: %d ep: %d\n",
  1104. __func__, resp_msg->svc_id, resp_msg->status,
  1105. resp_msg->eid);
  1106. conn_resp->resp_code = resp_msg->status;
  1107. /* check response status */
  1108. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1109. ath6kl_dbg(ATH6KL_DBG_HTC,
  1110. "Target failed service 0x%X connect request (status:%d)\n",
  1111. resp_msg->svc_id, resp_msg->status);
  1112. status = -EINVAL;
  1113. goto free_packet;
  1114. }
  1115. assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
  1116. max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
  1117. }
  1118. /* the rest are parameter checks so set the error status */
  1119. status = -EINVAL;
  1120. if (assigned_epid >= ENDPOINT_MAX) {
  1121. WARN_ON_ONCE(1);
  1122. goto free_packet;
  1123. }
  1124. if (max_msg_size == 0) {
  1125. WARN_ON_ONCE(1);
  1126. goto free_packet;
  1127. }
  1128. ep = &target->endpoint[assigned_epid];
  1129. ep->eid = assigned_epid;
  1130. if (ep->svc_id != 0) {
  1131. /* endpoint already in use! */
  1132. WARN_ON_ONCE(1);
  1133. goto free_packet;
  1134. }
  1135. /* return assigned endpoint to caller */
  1136. conn_resp->endpoint = assigned_epid;
  1137. conn_resp->len_max = max_msg_size;
  1138. /* setup the endpoint */
  1139. ep->svc_id = conn_req->svc_id; /* this marks ep in use */
  1140. ep->max_txq_depth = conn_req->max_txq_depth;
  1141. ep->len_max = max_msg_size;
  1142. ep->cred_dist.credits = tx_alloc;
  1143. ep->cred_dist.cred_sz = target->tgt_cred_sz;
  1144. ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
  1145. if (max_msg_size % target->tgt_cred_sz)
  1146. ep->cred_dist.cred_per_msg++;
  1147. /* copy all the callbacks */
  1148. ep->ep_cb = conn_req->ep_cb;
  1149. /* initialize tx_drop_packet_threshold */
  1150. ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
  1151. status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
  1152. &ep->pipe.pipeid_ul,
  1153. &ep->pipe.pipeid_dl);
  1154. if (status != 0)
  1155. goto free_packet;
  1156. ath6kl_dbg(ATH6KL_DBG_HTC,
  1157. "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
  1158. ep->svc_id, ep->pipe.pipeid_ul,
  1159. ep->pipe.pipeid_dl, ep->eid);
  1160. if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
  1161. ep->pipe.tx_credit_flow_enabled = false;
  1162. ath6kl_dbg(ATH6KL_DBG_HTC,
  1163. "SVC: 0x%4.4X ep:%d TX flow control off\n",
  1164. ep->svc_id, assigned_epid);
  1165. }
  1166. free_packet:
  1167. if (packet != NULL)
  1168. htc_free_txctrl_packet(target, packet);
  1169. return status;
  1170. }
  1171. /* htc export functions */
  1172. static void *ath6kl_htc_pipe_create(struct ath6kl *ar)
  1173. {
  1174. int status = 0;
  1175. struct htc_endpoint *ep = NULL;
  1176. struct htc_target *target = NULL;
  1177. struct htc_packet *packet;
  1178. int i;
  1179. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  1180. if (target == NULL) {
  1181. ath6kl_err("htc create unable to allocate memory\n");
  1182. status = -ENOMEM;
  1183. goto fail_htc_create;
  1184. }
  1185. spin_lock_init(&target->htc_lock);
  1186. spin_lock_init(&target->rx_lock);
  1187. spin_lock_init(&target->tx_lock);
  1188. reset_endpoint_states(target);
  1189. for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
  1190. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  1191. if (packet != NULL)
  1192. free_htc_packet_container(target, packet);
  1193. }
  1194. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1195. if (!target->dev) {
  1196. ath6kl_err("unable to allocate memory\n");
  1197. status = -ENOMEM;
  1198. goto fail_htc_create;
  1199. }
  1200. target->dev->ar = ar;
  1201. target->dev->htc_cnxt = target;
  1202. /* Get HIF default pipe for HTC message exchange */
  1203. ep = &target->endpoint[ENDPOINT_0];
  1204. ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
  1205. &ep->pipe.pipeid_dl);
  1206. return target;
  1207. fail_htc_create:
  1208. if (status != 0) {
  1209. if (target != NULL)
  1210. ath6kl_htc_pipe_cleanup(target);
  1211. target = NULL;
  1212. }
  1213. return target;
  1214. }
  1215. /* cleanup the HTC instance */
  1216. static void ath6kl_htc_pipe_cleanup(struct htc_target *target)
  1217. {
  1218. struct htc_packet *packet;
  1219. while (true) {
  1220. packet = alloc_htc_packet_container(target);
  1221. if (packet == NULL)
  1222. break;
  1223. kfree(packet);
  1224. }
  1225. kfree(target->dev);
  1226. /* kfree our instance */
  1227. kfree(target);
  1228. }
  1229. static int ath6kl_htc_pipe_start(struct htc_target *target)
  1230. {
  1231. struct sk_buff *skb;
  1232. struct htc_setup_comp_ext_msg *setup;
  1233. struct htc_packet *packet;
  1234. htc_config_target_hif_pipe(target);
  1235. /* allocate a buffer to send */
  1236. packet = htc_alloc_txctrl_packet(target);
  1237. if (packet == NULL) {
  1238. WARN_ON_ONCE(1);
  1239. return -ENOMEM;
  1240. }
  1241. skb = packet->skb;
  1242. /* assemble setup complete message */
  1243. setup = (struct htc_setup_comp_ext_msg *) skb_put(skb,
  1244. sizeof(*setup));
  1245. memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg));
  1246. setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
  1247. ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n");
  1248. set_htc_pkt_info(packet, NULL, (u8 *) setup,
  1249. sizeof(struct htc_setup_comp_ext_msg),
  1250. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1251. target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE;
  1252. return ath6kl_htc_pipe_tx(target, packet);
  1253. }
  1254. static void ath6kl_htc_pipe_stop(struct htc_target *target)
  1255. {
  1256. int i;
  1257. struct htc_endpoint *ep;
  1258. /* cleanup endpoints */
  1259. for (i = 0; i < ENDPOINT_MAX; i++) {
  1260. ep = &target->endpoint[i];
  1261. htc_flush_rx_queue(target, ep);
  1262. htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL);
  1263. }
  1264. reset_endpoint_states(target);
  1265. target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE;
  1266. }
  1267. static int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target,
  1268. enum htc_endpoint_id endpoint)
  1269. {
  1270. int num;
  1271. spin_lock_bh(&target->rx_lock);
  1272. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1273. spin_unlock_bh(&target->rx_lock);
  1274. return num;
  1275. }
  1276. static int ath6kl_htc_pipe_tx(struct htc_target *target,
  1277. struct htc_packet *packet)
  1278. {
  1279. struct list_head queue;
  1280. ath6kl_dbg(ATH6KL_DBG_HTC,
  1281. "%s: endPointId: %d, buffer: 0x%p, length: %d\n",
  1282. __func__, packet->endpoint, packet->buf,
  1283. packet->act_len);
  1284. INIT_LIST_HEAD(&queue);
  1285. list_add_tail(&packet->list, &queue);
  1286. return htc_send_packets_multiple(target, &queue);
  1287. }
  1288. static int ath6kl_htc_pipe_wait_target(struct htc_target *target)
  1289. {
  1290. struct htc_ready_ext_msg *ready_msg;
  1291. struct htc_service_connect_req connect;
  1292. struct htc_service_connect_resp resp;
  1293. int status = 0;
  1294. status = htc_wait_recv_ctrl_message(target);
  1295. if (status != 0)
  1296. return status;
  1297. if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) {
  1298. ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg len:%d!\n",
  1299. target->pipe.ctrl_response_len);
  1300. return -ECOMM;
  1301. }
  1302. ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf;
  1303. if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) {
  1304. ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg : 0x%X !\n",
  1305. ready_msg->ver2_0_info.msg_id);
  1306. return -ECOMM;
  1307. }
  1308. ath6kl_dbg(ATH6KL_DBG_HTC,
  1309. "Target Ready! : transmit resources : %d size:%d\n",
  1310. ready_msg->ver2_0_info.cred_cnt,
  1311. ready_msg->ver2_0_info.cred_sz);
  1312. target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt);
  1313. target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz);
  1314. if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0))
  1315. return -ECOMM;
  1316. htc_setup_target_buffer_assignments(target);
  1317. /* setup our pseudo HTC control endpoint connection */
  1318. memset(&connect, 0, sizeof(connect));
  1319. memset(&resp, 0, sizeof(resp));
  1320. connect.ep_cb.tx_complete = htc_txctrl_complete;
  1321. connect.ep_cb.rx = htc_rxctrl_complete;
  1322. connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS;
  1323. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1324. /* connect fake service */
  1325. status = ath6kl_htc_pipe_conn_service(target, &connect, &resp);
  1326. return status;
  1327. }
  1328. static void ath6kl_htc_pipe_flush_txep(struct htc_target *target,
  1329. enum htc_endpoint_id endpoint, u16 tag)
  1330. {
  1331. struct htc_endpoint *ep = &target->endpoint[endpoint];
  1332. if (ep->svc_id == 0) {
  1333. WARN_ON_ONCE(1);
  1334. /* not in use.. */
  1335. return;
  1336. }
  1337. htc_flush_tx_endpoint(target, ep, tag);
  1338. }
  1339. static int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target,
  1340. struct list_head *pkt_queue)
  1341. {
  1342. struct htc_packet *packet, *tmp_pkt, *first;
  1343. struct htc_endpoint *ep;
  1344. int status = 0;
  1345. if (list_empty(pkt_queue))
  1346. return -EINVAL;
  1347. first = list_first_entry(pkt_queue, struct htc_packet, list);
  1348. if (first->endpoint >= ENDPOINT_MAX) {
  1349. WARN_ON_ONCE(1);
  1350. return -EINVAL;
  1351. }
  1352. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n",
  1353. __func__, first->endpoint, get_queue_depth(pkt_queue),
  1354. first->buf_len);
  1355. ep = &target->endpoint[first->endpoint];
  1356. spin_lock_bh(&target->rx_lock);
  1357. /* store receive packets */
  1358. list_splice_tail_init(pkt_queue, &ep->rx_bufq);
  1359. spin_unlock_bh(&target->rx_lock);
  1360. if (status != 0) {
  1361. /* walk through queue and mark each one canceled */
  1362. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  1363. packet->status = -ECANCELED;
  1364. }
  1365. do_recv_completion(ep, pkt_queue);
  1366. }
  1367. return status;
  1368. }
  1369. static void ath6kl_htc_pipe_activity_changed(struct htc_target *target,
  1370. enum htc_endpoint_id ep,
  1371. bool active)
  1372. {
  1373. /* TODO */
  1374. }
  1375. static void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target)
  1376. {
  1377. /* TODO */
  1378. }
  1379. static int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
  1380. struct ath6kl_htc_credit_info *info)
  1381. {
  1382. return 0;
  1383. }
  1384. static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
  1385. .create = ath6kl_htc_pipe_create,
  1386. .wait_target = ath6kl_htc_pipe_wait_target,
  1387. .start = ath6kl_htc_pipe_start,
  1388. .conn_service = ath6kl_htc_pipe_conn_service,
  1389. .tx = ath6kl_htc_pipe_tx,
  1390. .stop = ath6kl_htc_pipe_stop,
  1391. .cleanup = ath6kl_htc_pipe_cleanup,
  1392. .flush_txep = ath6kl_htc_pipe_flush_txep,
  1393. .flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
  1394. .activity_changed = ath6kl_htc_pipe_activity_changed,
  1395. .get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
  1396. .add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
  1397. .credit_setup = ath6kl_htc_pipe_credit_setup,
  1398. .tx_complete = ath6kl_htc_pipe_tx_complete,
  1399. .rx_complete = ath6kl_htc_pipe_rx_complete,
  1400. };
  1401. void ath6kl_htc_pipe_attach(struct ath6kl *ar)
  1402. {
  1403. ar->htc_ops = &ath6kl_htc_pipe_ops;
  1404. }