htc_pipe.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. if (htc_hdr->eid >= ENDPOINT_MAX) {
  825. ath6kl_dbg(ATH6KL_DBG_HTC,
  826. "HTC Rx: invalid EndpointID=%d\n",
  827. htc_hdr->eid);
  828. status = -EINVAL;
  829. goto free_skb;
  830. }
  831. ep = &target->endpoint[htc_hdr->eid];
  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_warn("htc pipe control receive timeout!\n");
  966. return -ETIMEDOUT;
  967. }
  968. return 0;
  969. }
  970. static void htc_rxctrl_complete(struct htc_target *context,
  971. struct htc_packet *packet)
  972. {
  973. struct sk_buff *skb = packet->skb;
  974. if (packet->endpoint == ENDPOINT_0 &&
  975. packet->status == -ECANCELED &&
  976. skb != NULL)
  977. dev_kfree_skb(skb);
  978. }
  979. /* htc pipe initialization */
  980. static void reset_endpoint_states(struct htc_target *target)
  981. {
  982. struct htc_endpoint *ep;
  983. int i;
  984. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  985. ep = &target->endpoint[i];
  986. ep->svc_id = 0;
  987. ep->len_max = 0;
  988. ep->max_txq_depth = 0;
  989. ep->eid = i;
  990. INIT_LIST_HEAD(&ep->txq);
  991. INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
  992. INIT_LIST_HEAD(&ep->rx_bufq);
  993. ep->target = target;
  994. ep->pipe.tx_credit_flow_enabled = true;
  995. }
  996. }
  997. /* start HTC, this is called after all services are connected */
  998. static int htc_config_target_hif_pipe(struct htc_target *target)
  999. {
  1000. return 0;
  1001. }
  1002. /* htc service functions */
  1003. static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
  1004. {
  1005. u8 allocation = 0;
  1006. int i;
  1007. for (i = 0; i < ENDPOINT_MAX; i++) {
  1008. if (target->pipe.txcredit_alloc[i].service_id == service_id)
  1009. allocation =
  1010. target->pipe.txcredit_alloc[i].credit_alloc;
  1011. }
  1012. if (allocation == 0) {
  1013. ath6kl_dbg(ATH6KL_DBG_HTC,
  1014. "HTC Service TX : 0x%2.2X : allocation is zero!\n",
  1015. service_id);
  1016. }
  1017. return allocation;
  1018. }
  1019. static int ath6kl_htc_pipe_conn_service(struct htc_target *target,
  1020. struct htc_service_connect_req *conn_req,
  1021. struct htc_service_connect_resp *conn_resp)
  1022. {
  1023. struct ath6kl *ar = target->dev->ar;
  1024. struct htc_packet *packet = NULL;
  1025. struct htc_conn_service_resp *resp_msg;
  1026. struct htc_conn_service_msg *conn_msg;
  1027. enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
  1028. bool disable_credit_flowctrl = false;
  1029. unsigned int max_msg_size = 0;
  1030. struct htc_endpoint *ep;
  1031. int length, status = 0;
  1032. struct sk_buff *skb;
  1033. u8 tx_alloc;
  1034. u16 flags;
  1035. if (conn_req->svc_id == 0) {
  1036. WARN_ON_ONCE(1);
  1037. status = -EINVAL;
  1038. goto free_packet;
  1039. }
  1040. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1041. /* special case for pseudo control service */
  1042. assigned_epid = ENDPOINT_0;
  1043. max_msg_size = HTC_MAX_CTRL_MSG_LEN;
  1044. tx_alloc = 0;
  1045. } else {
  1046. tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
  1047. if (tx_alloc == 0) {
  1048. status = -ENOMEM;
  1049. goto free_packet;
  1050. }
  1051. /* allocate a packet to send to the target */
  1052. packet = htc_alloc_txctrl_packet(target);
  1053. if (packet == NULL) {
  1054. WARN_ON_ONCE(1);
  1055. status = -ENOMEM;
  1056. goto free_packet;
  1057. }
  1058. skb = packet->skb;
  1059. length = sizeof(struct htc_conn_service_msg);
  1060. /* assemble connect service message */
  1061. conn_msg = (struct htc_conn_service_msg *) skb_put(skb,
  1062. length);
  1063. if (conn_msg == NULL) {
  1064. WARN_ON_ONCE(1);
  1065. status = -EINVAL;
  1066. goto free_packet;
  1067. }
  1068. memset(conn_msg, 0,
  1069. sizeof(struct htc_conn_service_msg));
  1070. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1071. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1072. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
  1073. ~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
  1074. /* tell target desired recv alloc for this ep */
  1075. flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
  1076. conn_msg->conn_flags |= cpu_to_le16(flags);
  1077. if (conn_req->conn_flags &
  1078. HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
  1079. disable_credit_flowctrl = true;
  1080. }
  1081. set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
  1082. length,
  1083. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1084. status = ath6kl_htc_pipe_tx(target, packet);
  1085. /* we don't own it anymore */
  1086. packet = NULL;
  1087. if (status != 0)
  1088. goto free_packet;
  1089. /* wait for response */
  1090. status = htc_wait_recv_ctrl_message(target);
  1091. if (status != 0)
  1092. goto free_packet;
  1093. /* we controlled the buffer creation so it has to be
  1094. * properly aligned
  1095. */
  1096. resp_msg = (struct htc_conn_service_resp *)
  1097. target->pipe.ctrl_response_buf;
  1098. if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
  1099. (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
  1100. /* this message is not valid */
  1101. WARN_ON_ONCE(1);
  1102. status = -EINVAL;
  1103. goto free_packet;
  1104. }
  1105. ath6kl_dbg(ATH6KL_DBG_TRC,
  1106. "%s: service 0x%X conn resp: status: %d ep: %d\n",
  1107. __func__, resp_msg->svc_id, resp_msg->status,
  1108. resp_msg->eid);
  1109. conn_resp->resp_code = resp_msg->status;
  1110. /* check response status */
  1111. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1112. ath6kl_dbg(ATH6KL_DBG_HTC,
  1113. "Target failed service 0x%X connect request (status:%d)\n",
  1114. resp_msg->svc_id, resp_msg->status);
  1115. status = -EINVAL;
  1116. goto free_packet;
  1117. }
  1118. assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
  1119. max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
  1120. }
  1121. /* the rest are parameter checks so set the error status */
  1122. status = -EINVAL;
  1123. if (assigned_epid >= ENDPOINT_MAX) {
  1124. WARN_ON_ONCE(1);
  1125. goto free_packet;
  1126. }
  1127. if (max_msg_size == 0) {
  1128. WARN_ON_ONCE(1);
  1129. goto free_packet;
  1130. }
  1131. ep = &target->endpoint[assigned_epid];
  1132. ep->eid = assigned_epid;
  1133. if (ep->svc_id != 0) {
  1134. /* endpoint already in use! */
  1135. WARN_ON_ONCE(1);
  1136. goto free_packet;
  1137. }
  1138. /* return assigned endpoint to caller */
  1139. conn_resp->endpoint = assigned_epid;
  1140. conn_resp->len_max = max_msg_size;
  1141. /* setup the endpoint */
  1142. ep->svc_id = conn_req->svc_id; /* this marks ep in use */
  1143. ep->max_txq_depth = conn_req->max_txq_depth;
  1144. ep->len_max = max_msg_size;
  1145. ep->cred_dist.credits = tx_alloc;
  1146. ep->cred_dist.cred_sz = target->tgt_cred_sz;
  1147. ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
  1148. if (max_msg_size % target->tgt_cred_sz)
  1149. ep->cred_dist.cred_per_msg++;
  1150. /* copy all the callbacks */
  1151. ep->ep_cb = conn_req->ep_cb;
  1152. /* initialize tx_drop_packet_threshold */
  1153. ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
  1154. status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
  1155. &ep->pipe.pipeid_ul,
  1156. &ep->pipe.pipeid_dl);
  1157. if (status != 0)
  1158. goto free_packet;
  1159. ath6kl_dbg(ATH6KL_DBG_HTC,
  1160. "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
  1161. ep->svc_id, ep->pipe.pipeid_ul,
  1162. ep->pipe.pipeid_dl, ep->eid);
  1163. if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
  1164. ep->pipe.tx_credit_flow_enabled = false;
  1165. ath6kl_dbg(ATH6KL_DBG_HTC,
  1166. "SVC: 0x%4.4X ep:%d TX flow control off\n",
  1167. ep->svc_id, assigned_epid);
  1168. }
  1169. free_packet:
  1170. if (packet != NULL)
  1171. htc_free_txctrl_packet(target, packet);
  1172. return status;
  1173. }
  1174. /* htc export functions */
  1175. static void *ath6kl_htc_pipe_create(struct ath6kl *ar)
  1176. {
  1177. int status = 0;
  1178. struct htc_endpoint *ep = NULL;
  1179. struct htc_target *target = NULL;
  1180. struct htc_packet *packet;
  1181. int i;
  1182. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  1183. if (target == NULL) {
  1184. ath6kl_err("htc create unable to allocate memory\n");
  1185. status = -ENOMEM;
  1186. goto fail_htc_create;
  1187. }
  1188. spin_lock_init(&target->htc_lock);
  1189. spin_lock_init(&target->rx_lock);
  1190. spin_lock_init(&target->tx_lock);
  1191. reset_endpoint_states(target);
  1192. for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
  1193. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  1194. if (packet != NULL)
  1195. free_htc_packet_container(target, packet);
  1196. }
  1197. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1198. if (!target->dev) {
  1199. ath6kl_err("unable to allocate memory\n");
  1200. status = -ENOMEM;
  1201. goto fail_htc_create;
  1202. }
  1203. target->dev->ar = ar;
  1204. target->dev->htc_cnxt = target;
  1205. /* Get HIF default pipe for HTC message exchange */
  1206. ep = &target->endpoint[ENDPOINT_0];
  1207. ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
  1208. &ep->pipe.pipeid_dl);
  1209. return target;
  1210. fail_htc_create:
  1211. if (status != 0) {
  1212. if (target != NULL)
  1213. ath6kl_htc_pipe_cleanup(target);
  1214. target = NULL;
  1215. }
  1216. return target;
  1217. }
  1218. /* cleanup the HTC instance */
  1219. static void ath6kl_htc_pipe_cleanup(struct htc_target *target)
  1220. {
  1221. struct htc_packet *packet;
  1222. while (true) {
  1223. packet = alloc_htc_packet_container(target);
  1224. if (packet == NULL)
  1225. break;
  1226. kfree(packet);
  1227. }
  1228. kfree(target->dev);
  1229. /* kfree our instance */
  1230. kfree(target);
  1231. }
  1232. static int ath6kl_htc_pipe_start(struct htc_target *target)
  1233. {
  1234. struct sk_buff *skb;
  1235. struct htc_setup_comp_ext_msg *setup;
  1236. struct htc_packet *packet;
  1237. htc_config_target_hif_pipe(target);
  1238. /* allocate a buffer to send */
  1239. packet = htc_alloc_txctrl_packet(target);
  1240. if (packet == NULL) {
  1241. WARN_ON_ONCE(1);
  1242. return -ENOMEM;
  1243. }
  1244. skb = packet->skb;
  1245. /* assemble setup complete message */
  1246. setup = (struct htc_setup_comp_ext_msg *) skb_put(skb,
  1247. sizeof(*setup));
  1248. memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg));
  1249. setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
  1250. ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n");
  1251. set_htc_pkt_info(packet, NULL, (u8 *) setup,
  1252. sizeof(struct htc_setup_comp_ext_msg),
  1253. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1254. target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE;
  1255. return ath6kl_htc_pipe_tx(target, packet);
  1256. }
  1257. static void ath6kl_htc_pipe_stop(struct htc_target *target)
  1258. {
  1259. int i;
  1260. struct htc_endpoint *ep;
  1261. /* cleanup endpoints */
  1262. for (i = 0; i < ENDPOINT_MAX; i++) {
  1263. ep = &target->endpoint[i];
  1264. htc_flush_rx_queue(target, ep);
  1265. htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL);
  1266. }
  1267. reset_endpoint_states(target);
  1268. target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE;
  1269. }
  1270. static int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target,
  1271. enum htc_endpoint_id endpoint)
  1272. {
  1273. int num;
  1274. spin_lock_bh(&target->rx_lock);
  1275. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1276. spin_unlock_bh(&target->rx_lock);
  1277. return num;
  1278. }
  1279. static int ath6kl_htc_pipe_tx(struct htc_target *target,
  1280. struct htc_packet *packet)
  1281. {
  1282. struct list_head queue;
  1283. ath6kl_dbg(ATH6KL_DBG_HTC,
  1284. "%s: endPointId: %d, buffer: 0x%p, length: %d\n",
  1285. __func__, packet->endpoint, packet->buf,
  1286. packet->act_len);
  1287. INIT_LIST_HEAD(&queue);
  1288. list_add_tail(&packet->list, &queue);
  1289. return htc_send_packets_multiple(target, &queue);
  1290. }
  1291. static int ath6kl_htc_pipe_wait_target(struct htc_target *target)
  1292. {
  1293. struct htc_ready_ext_msg *ready_msg;
  1294. struct htc_service_connect_req connect;
  1295. struct htc_service_connect_resp resp;
  1296. int status = 0;
  1297. status = htc_wait_recv_ctrl_message(target);
  1298. if (status != 0)
  1299. return status;
  1300. if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) {
  1301. ath6kl_warn("invalid htc pipe ready msg len: %d\n",
  1302. target->pipe.ctrl_response_len);
  1303. return -ECOMM;
  1304. }
  1305. ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf;
  1306. if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) {
  1307. ath6kl_warn("invalid htc pipe ready msg: 0x%x\n",
  1308. ready_msg->ver2_0_info.msg_id);
  1309. return -ECOMM;
  1310. }
  1311. ath6kl_dbg(ATH6KL_DBG_HTC,
  1312. "Target Ready! : transmit resources : %d size:%d\n",
  1313. ready_msg->ver2_0_info.cred_cnt,
  1314. ready_msg->ver2_0_info.cred_sz);
  1315. target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt);
  1316. target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz);
  1317. if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0))
  1318. return -ECOMM;
  1319. htc_setup_target_buffer_assignments(target);
  1320. /* setup our pseudo HTC control endpoint connection */
  1321. memset(&connect, 0, sizeof(connect));
  1322. memset(&resp, 0, sizeof(resp));
  1323. connect.ep_cb.tx_complete = htc_txctrl_complete;
  1324. connect.ep_cb.rx = htc_rxctrl_complete;
  1325. connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS;
  1326. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1327. /* connect fake service */
  1328. status = ath6kl_htc_pipe_conn_service(target, &connect, &resp);
  1329. return status;
  1330. }
  1331. static void ath6kl_htc_pipe_flush_txep(struct htc_target *target,
  1332. enum htc_endpoint_id endpoint, u16 tag)
  1333. {
  1334. struct htc_endpoint *ep = &target->endpoint[endpoint];
  1335. if (ep->svc_id == 0) {
  1336. WARN_ON_ONCE(1);
  1337. /* not in use.. */
  1338. return;
  1339. }
  1340. htc_flush_tx_endpoint(target, ep, tag);
  1341. }
  1342. static int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target,
  1343. struct list_head *pkt_queue)
  1344. {
  1345. struct htc_packet *packet, *tmp_pkt, *first;
  1346. struct htc_endpoint *ep;
  1347. int status = 0;
  1348. if (list_empty(pkt_queue))
  1349. return -EINVAL;
  1350. first = list_first_entry(pkt_queue, struct htc_packet, list);
  1351. if (first->endpoint >= ENDPOINT_MAX) {
  1352. WARN_ON_ONCE(1);
  1353. return -EINVAL;
  1354. }
  1355. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n",
  1356. __func__, first->endpoint, get_queue_depth(pkt_queue),
  1357. first->buf_len);
  1358. ep = &target->endpoint[first->endpoint];
  1359. spin_lock_bh(&target->rx_lock);
  1360. /* store receive packets */
  1361. list_splice_tail_init(pkt_queue, &ep->rx_bufq);
  1362. spin_unlock_bh(&target->rx_lock);
  1363. if (status != 0) {
  1364. /* walk through queue and mark each one canceled */
  1365. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  1366. packet->status = -ECANCELED;
  1367. }
  1368. do_recv_completion(ep, pkt_queue);
  1369. }
  1370. return status;
  1371. }
  1372. static void ath6kl_htc_pipe_activity_changed(struct htc_target *target,
  1373. enum htc_endpoint_id ep,
  1374. bool active)
  1375. {
  1376. /* TODO */
  1377. }
  1378. static void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target)
  1379. {
  1380. struct htc_endpoint *endpoint;
  1381. struct htc_packet *packet, *tmp_pkt;
  1382. int i;
  1383. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1384. endpoint = &target->endpoint[i];
  1385. spin_lock_bh(&target->rx_lock);
  1386. list_for_each_entry_safe(packet, tmp_pkt,
  1387. &endpoint->rx_bufq, list) {
  1388. list_del(&packet->list);
  1389. spin_unlock_bh(&target->rx_lock);
  1390. ath6kl_dbg(ATH6KL_DBG_HTC,
  1391. "htc rx flush pkt 0x%p len %d ep %d\n",
  1392. packet, packet->buf_len,
  1393. packet->endpoint);
  1394. dev_kfree_skb(packet->pkt_cntxt);
  1395. spin_lock_bh(&target->rx_lock);
  1396. }
  1397. spin_unlock_bh(&target->rx_lock);
  1398. }
  1399. }
  1400. static int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
  1401. struct ath6kl_htc_credit_info *info)
  1402. {
  1403. return 0;
  1404. }
  1405. static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
  1406. .create = ath6kl_htc_pipe_create,
  1407. .wait_target = ath6kl_htc_pipe_wait_target,
  1408. .start = ath6kl_htc_pipe_start,
  1409. .conn_service = ath6kl_htc_pipe_conn_service,
  1410. .tx = ath6kl_htc_pipe_tx,
  1411. .stop = ath6kl_htc_pipe_stop,
  1412. .cleanup = ath6kl_htc_pipe_cleanup,
  1413. .flush_txep = ath6kl_htc_pipe_flush_txep,
  1414. .flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
  1415. .activity_changed = ath6kl_htc_pipe_activity_changed,
  1416. .get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
  1417. .add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
  1418. .credit_setup = ath6kl_htc_pipe_credit_setup,
  1419. .tx_complete = ath6kl_htc_pipe_tx_complete,
  1420. .rx_complete = ath6kl_htc_pipe_rx_complete,
  1421. };
  1422. void ath6kl_htc_pipe_attach(struct ath6kl *ar)
  1423. {
  1424. ar->htc_ops = &ath6kl_htc_pipe_ops;
  1425. }