htc_pipe.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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 = skb_push(skb, sizeof(*htc_hdr));
  194. if (!htc_hdr) {
  195. WARN_ON_ONCE(1);
  196. status = -EINVAL;
  197. break;
  198. }
  199. packet->info.tx.flags |= HTC_FLAGS_TX_FIXUP_NETBUF;
  200. /* Endianess? */
  201. put_unaligned((u16) payload_len, &htc_hdr->payld_len);
  202. htc_hdr->flags = packet->info.tx.flags;
  203. htc_hdr->eid = (u8) packet->endpoint;
  204. htc_hdr->ctrl[0] = 0;
  205. htc_hdr->ctrl[1] = (u8) packet->info.tx.seqno;
  206. spin_lock_bh(&target->tx_lock);
  207. /* store in look up queue to match completions */
  208. list_add_tail(&packet->list, &ep->pipe.tx_lookup_queue);
  209. ep->ep_st.tx_issued += 1;
  210. spin_unlock_bh(&target->tx_lock);
  211. status = ath6kl_hif_pipe_send(target->dev->ar,
  212. ep->pipe.pipeid_ul, NULL, skb);
  213. if (status != 0) {
  214. if (status != -ENOMEM) {
  215. /* TODO: if more than 1 endpoint maps to the
  216. * same PipeID, it is possible to run out of
  217. * resources in the HIF layer.
  218. * Don't emit the error
  219. */
  220. ath6kl_dbg(ATH6KL_DBG_HTC,
  221. "%s: failed status:%d\n",
  222. __func__, status);
  223. }
  224. spin_lock_bh(&target->tx_lock);
  225. list_del(&packet->list);
  226. /* reclaim credits */
  227. ep->cred_dist.credits += packet->info.tx.cred_used;
  228. spin_unlock_bh(&target->tx_lock);
  229. /* put it back into the callers queue */
  230. list_add(&packet->list, pkt_queue);
  231. break;
  232. }
  233. }
  234. if (status != 0) {
  235. while (!list_empty(pkt_queue)) {
  236. if (status != -ENOMEM) {
  237. ath6kl_dbg(ATH6KL_DBG_HTC,
  238. "%s: failed pkt:0x%p status:%d\n",
  239. __func__, packet, status);
  240. }
  241. packet = list_first_entry(pkt_queue,
  242. struct htc_packet, list);
  243. list_del(&packet->list);
  244. packet->status = status;
  245. send_packet_completion(target, packet);
  246. }
  247. }
  248. return status;
  249. }
  250. static enum htc_send_queue_result htc_try_send(struct htc_target *target,
  251. struct htc_endpoint *ep,
  252. struct list_head *txq)
  253. {
  254. struct list_head send_queue; /* temp queue to hold packets */
  255. struct htc_packet *packet, *tmp_pkt;
  256. struct ath6kl *ar = target->dev->ar;
  257. enum htc_send_full_action action;
  258. int tx_resources, overflow, txqueue_depth, i, good_pkts;
  259. u8 pipeid;
  260. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: (queue:0x%p depth:%d)\n",
  261. __func__, txq,
  262. (txq == NULL) ? 0 : get_queue_depth(txq));
  263. /* init the local send queue */
  264. INIT_LIST_HEAD(&send_queue);
  265. /*
  266. * txq equals to NULL means
  267. * caller didn't provide a queue, just wants us to
  268. * check queues and send
  269. */
  270. if (txq != NULL) {
  271. if (list_empty(txq)) {
  272. /* empty queue */
  273. return HTC_SEND_QUEUE_DROP;
  274. }
  275. spin_lock_bh(&target->tx_lock);
  276. txqueue_depth = get_queue_depth(&ep->txq);
  277. spin_unlock_bh(&target->tx_lock);
  278. if (txqueue_depth >= ep->max_txq_depth) {
  279. /* we've already overflowed */
  280. overflow = get_queue_depth(txq);
  281. } else {
  282. /* get how much we will overflow by */
  283. overflow = txqueue_depth;
  284. overflow += get_queue_depth(txq);
  285. /* get how much we will overflow the TX queue by */
  286. overflow -= ep->max_txq_depth;
  287. }
  288. /* if overflow is negative or zero, we are okay */
  289. if (overflow > 0) {
  290. ath6kl_dbg(ATH6KL_DBG_HTC,
  291. "%s: Endpoint %d, TX queue will overflow :%d, Tx Depth:%d, Max:%d\n",
  292. __func__, ep->eid, overflow, txqueue_depth,
  293. ep->max_txq_depth);
  294. }
  295. if ((overflow <= 0) ||
  296. (ep->ep_cb.tx_full == NULL)) {
  297. /*
  298. * all packets will fit or caller did not provide send
  299. * full indication handler -- just move all of them
  300. * to the local send_queue object
  301. */
  302. list_splice_tail_init(txq, &send_queue);
  303. } else {
  304. good_pkts = get_queue_depth(txq) - overflow;
  305. if (good_pkts < 0) {
  306. WARN_ON_ONCE(1);
  307. return HTC_SEND_QUEUE_DROP;
  308. }
  309. /* we have overflowed, and a callback is provided */
  310. /* dequeue all non-overflow packets to the sendqueue */
  311. for (i = 0; i < good_pkts; i++) {
  312. /* pop off caller's queue */
  313. packet = list_first_entry(txq,
  314. struct htc_packet,
  315. list);
  316. /* move to local queue */
  317. list_move_tail(&packet->list, &send_queue);
  318. }
  319. /*
  320. * the caller's queue has all the packets that won't fit
  321. * walk through the caller's queue and indicate each to
  322. * the send full handler
  323. */
  324. list_for_each_entry_safe(packet, tmp_pkt,
  325. txq, list) {
  326. ath6kl_dbg(ATH6KL_DBG_HTC,
  327. "%s: Indicate overflowed TX pkts: %p\n",
  328. __func__, packet);
  329. action = ep->ep_cb.tx_full(ep->target, packet);
  330. if (action == HTC_SEND_FULL_DROP) {
  331. /* callback wants the packet dropped */
  332. ep->ep_st.tx_dropped += 1;
  333. /* leave this one in the caller's queue
  334. * for cleanup */
  335. } else {
  336. /* callback wants to keep this packet,
  337. * move from caller's queue to the send
  338. * queue */
  339. list_move_tail(&packet->list,
  340. &send_queue);
  341. }
  342. }
  343. if (list_empty(&send_queue)) {
  344. /* no packets made it in, caller will cleanup */
  345. return HTC_SEND_QUEUE_DROP;
  346. }
  347. }
  348. }
  349. if (!ep->pipe.tx_credit_flow_enabled) {
  350. tx_resources =
  351. ath6kl_hif_pipe_get_free_queue_number(ar,
  352. ep->pipe.pipeid_ul);
  353. } else {
  354. tx_resources = 0;
  355. }
  356. spin_lock_bh(&target->tx_lock);
  357. if (!list_empty(&send_queue)) {
  358. /* transfer packets to tail */
  359. list_splice_tail_init(&send_queue, &ep->txq);
  360. if (!list_empty(&send_queue)) {
  361. WARN_ON_ONCE(1);
  362. spin_unlock_bh(&target->tx_lock);
  363. return HTC_SEND_QUEUE_DROP;
  364. }
  365. INIT_LIST_HEAD(&send_queue);
  366. }
  367. /* increment tx processing count on entry */
  368. ep->tx_proc_cnt++;
  369. if (ep->tx_proc_cnt > 1) {
  370. /*
  371. * Another thread or task is draining the TX queues on this
  372. * endpoint that thread will reset the tx processing count
  373. * when the queue is drained.
  374. */
  375. ep->tx_proc_cnt--;
  376. spin_unlock_bh(&target->tx_lock);
  377. return HTC_SEND_QUEUE_OK;
  378. }
  379. /***** beyond this point only 1 thread may enter ******/
  380. /*
  381. * Now drain the endpoint TX queue for transmission as long as we have
  382. * enough transmit resources.
  383. */
  384. while (true) {
  385. if (get_queue_depth(&ep->txq) == 0)
  386. break;
  387. if (ep->pipe.tx_credit_flow_enabled) {
  388. /*
  389. * Credit based mechanism provides flow control
  390. * based on target transmit resource availability,
  391. * we assume that the HIF layer will always have
  392. * bus resources greater than target transmit
  393. * resources.
  394. */
  395. get_htc_packet_credit_based(target, ep, &send_queue);
  396. } else {
  397. /*
  398. * Get all packets for this endpoint that we can
  399. * for this pass.
  400. */
  401. get_htc_packet(target, ep, &send_queue, tx_resources);
  402. }
  403. if (get_queue_depth(&send_queue) == 0) {
  404. /*
  405. * Didn't get packets due to out of resources or TX
  406. * queue was drained.
  407. */
  408. break;
  409. }
  410. spin_unlock_bh(&target->tx_lock);
  411. /* send what we can */
  412. htc_issue_packets(target, ep, &send_queue);
  413. if (!ep->pipe.tx_credit_flow_enabled) {
  414. pipeid = ep->pipe.pipeid_ul;
  415. tx_resources =
  416. ath6kl_hif_pipe_get_free_queue_number(ar, pipeid);
  417. }
  418. spin_lock_bh(&target->tx_lock);
  419. }
  420. /* done with this endpoint, we can clear the count */
  421. ep->tx_proc_cnt = 0;
  422. spin_unlock_bh(&target->tx_lock);
  423. return HTC_SEND_QUEUE_OK;
  424. }
  425. /* htc control packet manipulation */
  426. static void destroy_htc_txctrl_packet(struct htc_packet *packet)
  427. {
  428. struct sk_buff *skb;
  429. skb = packet->skb;
  430. dev_kfree_skb(skb);
  431. kfree(packet);
  432. }
  433. static struct htc_packet *build_htc_txctrl_packet(void)
  434. {
  435. struct htc_packet *packet = NULL;
  436. struct sk_buff *skb;
  437. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  438. if (packet == NULL)
  439. return NULL;
  440. skb = __dev_alloc_skb(HTC_CONTROL_BUFFER_SIZE, GFP_KERNEL);
  441. if (skb == NULL) {
  442. kfree(packet);
  443. return NULL;
  444. }
  445. packet->skb = skb;
  446. return packet;
  447. }
  448. static void htc_free_txctrl_packet(struct htc_target *target,
  449. struct htc_packet *packet)
  450. {
  451. destroy_htc_txctrl_packet(packet);
  452. }
  453. static struct htc_packet *htc_alloc_txctrl_packet(struct htc_target *target)
  454. {
  455. return build_htc_txctrl_packet();
  456. }
  457. static void htc_txctrl_complete(struct htc_target *target,
  458. struct htc_packet *packet)
  459. {
  460. htc_free_txctrl_packet(target, packet);
  461. }
  462. #define MAX_MESSAGE_SIZE 1536
  463. static int htc_setup_target_buffer_assignments(struct htc_target *target)
  464. {
  465. int status, credits, credit_per_maxmsg, i;
  466. struct htc_pipe_txcredit_alloc *entry;
  467. unsigned int hif_usbaudioclass = 0;
  468. credit_per_maxmsg = MAX_MESSAGE_SIZE / target->tgt_cred_sz;
  469. if (MAX_MESSAGE_SIZE % target->tgt_cred_sz)
  470. credit_per_maxmsg++;
  471. /* TODO, this should be configured by the caller! */
  472. credits = target->tgt_creds;
  473. entry = &target->pipe.txcredit_alloc[0];
  474. status = -ENOMEM;
  475. /* FIXME: hif_usbaudioclass is always zero */
  476. if (hif_usbaudioclass) {
  477. ath6kl_dbg(ATH6KL_DBG_HTC,
  478. "%s: For USB Audio Class- Total:%d\n",
  479. __func__, credits);
  480. entry++;
  481. entry++;
  482. /* Setup VO Service To have Max Credits */
  483. entry->service_id = WMI_DATA_VO_SVC;
  484. entry->credit_alloc = (credits - 6);
  485. if (entry->credit_alloc == 0)
  486. entry->credit_alloc++;
  487. credits -= (int) entry->credit_alloc;
  488. if (credits <= 0)
  489. return status;
  490. entry++;
  491. entry->service_id = WMI_CONTROL_SVC;
  492. entry->credit_alloc = credit_per_maxmsg;
  493. credits -= (int) entry->credit_alloc;
  494. if (credits <= 0)
  495. return status;
  496. /* leftovers go to best effort */
  497. entry++;
  498. entry++;
  499. entry->service_id = WMI_DATA_BE_SVC;
  500. entry->credit_alloc = (u8) credits;
  501. status = 0;
  502. } else {
  503. entry++;
  504. entry->service_id = WMI_DATA_VI_SVC;
  505. entry->credit_alloc = credits / 4;
  506. if (entry->credit_alloc == 0)
  507. entry->credit_alloc++;
  508. credits -= (int) entry->credit_alloc;
  509. if (credits <= 0)
  510. return status;
  511. entry++;
  512. entry->service_id = WMI_DATA_VO_SVC;
  513. entry->credit_alloc = credits / 4;
  514. if (entry->credit_alloc == 0)
  515. entry->credit_alloc++;
  516. credits -= (int) entry->credit_alloc;
  517. if (credits <= 0)
  518. return status;
  519. entry++;
  520. entry->service_id = WMI_CONTROL_SVC;
  521. entry->credit_alloc = credit_per_maxmsg;
  522. credits -= (int) entry->credit_alloc;
  523. if (credits <= 0)
  524. return status;
  525. entry++;
  526. entry->service_id = WMI_DATA_BK_SVC;
  527. entry->credit_alloc = credit_per_maxmsg;
  528. credits -= (int) entry->credit_alloc;
  529. if (credits <= 0)
  530. return status;
  531. /* leftovers go to best effort */
  532. entry++;
  533. entry->service_id = WMI_DATA_BE_SVC;
  534. entry->credit_alloc = (u8) credits;
  535. status = 0;
  536. }
  537. if (status == 0) {
  538. for (i = 0; i < ENDPOINT_MAX; i++) {
  539. if (target->pipe.txcredit_alloc[i].service_id != 0) {
  540. ath6kl_dbg(ATH6KL_DBG_HTC,
  541. "HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n",
  542. i,
  543. target->pipe.txcredit_alloc[i].
  544. service_id,
  545. target->pipe.txcredit_alloc[i].
  546. credit_alloc);
  547. }
  548. }
  549. }
  550. return status;
  551. }
  552. /* process credit reports and call distribution function */
  553. static void htc_process_credit_report(struct htc_target *target,
  554. struct htc_credit_report *rpt,
  555. int num_entries,
  556. enum htc_endpoint_id from_ep)
  557. {
  558. int total_credits = 0, i;
  559. struct htc_endpoint *ep;
  560. /* lock out TX while we update credits */
  561. spin_lock_bh(&target->tx_lock);
  562. for (i = 0; i < num_entries; i++, rpt++) {
  563. if (rpt->eid >= ENDPOINT_MAX) {
  564. WARN_ON_ONCE(1);
  565. spin_unlock_bh(&target->tx_lock);
  566. return;
  567. }
  568. ep = &target->endpoint[rpt->eid];
  569. ep->cred_dist.credits += rpt->credits;
  570. if (ep->cred_dist.credits && get_queue_depth(&ep->txq)) {
  571. spin_unlock_bh(&target->tx_lock);
  572. htc_try_send(target, ep, NULL);
  573. spin_lock_bh(&target->tx_lock);
  574. }
  575. total_credits += rpt->credits;
  576. }
  577. ath6kl_dbg(ATH6KL_DBG_HTC,
  578. "Report indicated %d credits to distribute\n",
  579. total_credits);
  580. spin_unlock_bh(&target->tx_lock);
  581. }
  582. /* flush endpoint TX queue */
  583. static void htc_flush_tx_endpoint(struct htc_target *target,
  584. struct htc_endpoint *ep, u16 tag)
  585. {
  586. struct htc_packet *packet;
  587. spin_lock_bh(&target->tx_lock);
  588. while (get_queue_depth(&ep->txq)) {
  589. packet = list_first_entry(&ep->txq, struct htc_packet, list);
  590. list_del(&packet->list);
  591. packet->status = 0;
  592. send_packet_completion(target, packet);
  593. }
  594. spin_unlock_bh(&target->tx_lock);
  595. }
  596. /*
  597. * In the adapted HIF layer, struct sk_buff * are passed between HIF and HTC,
  598. * since upper layers expects struct htc_packet containers we use the completed
  599. * skb and lookup it's corresponding HTC packet buffer from a lookup list.
  600. * This is extra overhead that can be fixed by re-aligning HIF interfaces with
  601. * HTC.
  602. */
  603. static struct htc_packet *htc_lookup_tx_packet(struct htc_target *target,
  604. struct htc_endpoint *ep,
  605. struct sk_buff *skb)
  606. {
  607. struct htc_packet *packet, *tmp_pkt, *found_packet = NULL;
  608. spin_lock_bh(&target->tx_lock);
  609. /*
  610. * interate from the front of tx lookup queue
  611. * this lookup should be fast since lower layers completes in-order and
  612. * so the completed packet should be at the head of the list generally
  613. */
  614. list_for_each_entry_safe(packet, tmp_pkt, &ep->pipe.tx_lookup_queue,
  615. list) {
  616. /* check for removal */
  617. if (skb == packet->skb) {
  618. /* found it */
  619. list_del(&packet->list);
  620. found_packet = packet;
  621. break;
  622. }
  623. }
  624. spin_unlock_bh(&target->tx_lock);
  625. return found_packet;
  626. }
  627. static int ath6kl_htc_pipe_tx_complete(struct ath6kl *ar, struct sk_buff *skb)
  628. {
  629. struct htc_target *target = ar->htc_target;
  630. struct htc_frame_hdr *htc_hdr;
  631. struct htc_endpoint *ep;
  632. struct htc_packet *packet;
  633. u8 ep_id, *netdata;
  634. u32 netlen;
  635. netdata = skb->data;
  636. netlen = skb->len;
  637. htc_hdr = (struct htc_frame_hdr *) netdata;
  638. ep_id = htc_hdr->eid;
  639. ep = &target->endpoint[ep_id];
  640. packet = htc_lookup_tx_packet(target, ep, skb);
  641. if (packet == NULL) {
  642. /* may have already been flushed and freed */
  643. ath6kl_err("HTC TX lookup failed!\n");
  644. } else {
  645. /* will be giving this buffer back to upper layers */
  646. packet->status = 0;
  647. send_packet_completion(target, packet);
  648. }
  649. skb = NULL;
  650. if (!ep->pipe.tx_credit_flow_enabled) {
  651. /*
  652. * note: when using TX credit flow, the re-checking of queues
  653. * happens when credits flow back from the target. in the
  654. * non-TX credit case, we recheck after the packet completes
  655. */
  656. htc_try_send(target, ep, NULL);
  657. }
  658. return 0;
  659. }
  660. static int htc_send_packets_multiple(struct htc_target *target,
  661. struct list_head *pkt_queue)
  662. {
  663. struct htc_endpoint *ep;
  664. struct htc_packet *packet, *tmp_pkt;
  665. if (list_empty(pkt_queue))
  666. return -EINVAL;
  667. /* get first packet to find out which ep the packets will go into */
  668. packet = list_first_entry(pkt_queue, struct htc_packet, list);
  669. if (packet->endpoint >= ENDPOINT_MAX) {
  670. WARN_ON_ONCE(1);
  671. return -EINVAL;
  672. }
  673. ep = &target->endpoint[packet->endpoint];
  674. htc_try_send(target, ep, pkt_queue);
  675. /* do completion on any packets that couldn't get in */
  676. if (!list_empty(pkt_queue)) {
  677. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  678. packet->status = -ENOMEM;
  679. }
  680. do_send_completion(ep, pkt_queue);
  681. }
  682. return 0;
  683. }
  684. /* htc pipe rx path */
  685. static struct htc_packet *alloc_htc_packet_container(struct htc_target *target)
  686. {
  687. struct htc_packet *packet;
  688. spin_lock_bh(&target->rx_lock);
  689. if (target->pipe.htc_packet_pool == NULL) {
  690. spin_unlock_bh(&target->rx_lock);
  691. return NULL;
  692. }
  693. packet = target->pipe.htc_packet_pool;
  694. target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next;
  695. spin_unlock_bh(&target->rx_lock);
  696. packet->list.next = NULL;
  697. return packet;
  698. }
  699. static void free_htc_packet_container(struct htc_target *target,
  700. struct htc_packet *packet)
  701. {
  702. struct list_head *lh;
  703. spin_lock_bh(&target->rx_lock);
  704. if (target->pipe.htc_packet_pool == NULL) {
  705. target->pipe.htc_packet_pool = packet;
  706. packet->list.next = NULL;
  707. } else {
  708. lh = (struct list_head *) target->pipe.htc_packet_pool;
  709. packet->list.next = lh;
  710. target->pipe.htc_packet_pool = packet;
  711. }
  712. spin_unlock_bh(&target->rx_lock);
  713. }
  714. static int htc_process_trailer(struct htc_target *target, u8 *buffer,
  715. int len, enum htc_endpoint_id from_ep)
  716. {
  717. struct htc_credit_report *report;
  718. struct htc_record_hdr *record;
  719. u8 *record_buf, *orig_buf;
  720. int orig_len, status;
  721. orig_buf = buffer;
  722. orig_len = len;
  723. status = 0;
  724. while (len > 0) {
  725. if (len < sizeof(struct htc_record_hdr)) {
  726. status = -EINVAL;
  727. break;
  728. }
  729. /* these are byte aligned structs */
  730. record = (struct htc_record_hdr *) buffer;
  731. len -= sizeof(struct htc_record_hdr);
  732. buffer += sizeof(struct htc_record_hdr);
  733. if (record->len > len) {
  734. /* no room left in buffer for record */
  735. ath6kl_dbg(ATH6KL_DBG_HTC,
  736. "invalid length: %d (id:%d) buffer has: %d bytes left\n",
  737. record->len, record->rec_id, len);
  738. status = -EINVAL;
  739. break;
  740. }
  741. /* start of record follows the header */
  742. record_buf = buffer;
  743. switch (record->rec_id) {
  744. case HTC_RECORD_CREDITS:
  745. if (record->len < sizeof(struct htc_credit_report)) {
  746. WARN_ON_ONCE(1);
  747. return -EINVAL;
  748. }
  749. report = (struct htc_credit_report *) record_buf;
  750. htc_process_credit_report(target, report,
  751. record->len / sizeof(*report),
  752. from_ep);
  753. break;
  754. default:
  755. ath6kl_dbg(ATH6KL_DBG_HTC,
  756. "unhandled record: id:%d length:%d\n",
  757. record->rec_id, record->len);
  758. break;
  759. }
  760. if (status != 0)
  761. break;
  762. /* advance buffer past this record for next time around */
  763. buffer += record->len;
  764. len -= record->len;
  765. }
  766. return status;
  767. }
  768. static void do_recv_completion(struct htc_endpoint *ep,
  769. struct list_head *queue_to_indicate)
  770. {
  771. struct htc_packet *packet;
  772. if (list_empty(queue_to_indicate)) {
  773. /* nothing to indicate */
  774. return;
  775. }
  776. /* using legacy EpRecv */
  777. while (!list_empty(queue_to_indicate)) {
  778. packet = list_first_entry(queue_to_indicate,
  779. struct htc_packet, list);
  780. list_del(&packet->list);
  781. ep->ep_cb.rx(ep->target, packet);
  782. }
  783. return;
  784. }
  785. static void recv_packet_completion(struct htc_target *target,
  786. struct htc_endpoint *ep,
  787. struct htc_packet *packet)
  788. {
  789. struct list_head container;
  790. INIT_LIST_HEAD(&container);
  791. list_add_tail(&packet->list, &container);
  792. /* do completion */
  793. do_recv_completion(ep, &container);
  794. }
  795. static int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb,
  796. u8 pipeid)
  797. {
  798. struct htc_target *target = ar->htc_target;
  799. u8 *netdata, *trailer, hdr_info;
  800. struct htc_frame_hdr *htc_hdr;
  801. u32 netlen, trailerlen = 0;
  802. struct htc_packet *packet;
  803. struct htc_endpoint *ep;
  804. u16 payload_len;
  805. int status = 0;
  806. /*
  807. * ar->htc_target can be NULL due to a race condition that can occur
  808. * during driver initialization(we do 'ath6kl_hif_power_on' before
  809. * initializing 'ar->htc_target' via 'ath6kl_htc_create').
  810. * 'ath6kl_hif_power_on' assigns 'ath6kl_recv_complete' as
  811. * usb_complete_t/callback function for 'usb_fill_bulk_urb'.
  812. * Thus the possibility of ar->htc_target being NULL
  813. * via ath6kl_recv_complete -> ath6kl_usb_io_comp_work.
  814. */
  815. if (WARN_ON_ONCE(!target)) {
  816. ath6kl_err("Target not yet initialized\n");
  817. status = -EINVAL;
  818. goto free_skb;
  819. }
  820. netdata = skb->data;
  821. netlen = skb->len;
  822. htc_hdr = (struct htc_frame_hdr *) netdata;
  823. if (htc_hdr->eid >= ENDPOINT_MAX) {
  824. ath6kl_dbg(ATH6KL_DBG_HTC,
  825. "HTC Rx: invalid EndpointID=%d\n",
  826. htc_hdr->eid);
  827. status = -EINVAL;
  828. goto free_skb;
  829. }
  830. ep = &target->endpoint[htc_hdr->eid];
  831. payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
  832. if (netlen < (payload_len + HTC_HDR_LENGTH)) {
  833. ath6kl_dbg(ATH6KL_DBG_HTC,
  834. "HTC Rx: insufficient length, got:%d expected =%zu\n",
  835. netlen, payload_len + HTC_HDR_LENGTH);
  836. status = -EINVAL;
  837. goto free_skb;
  838. }
  839. /* get flags to check for trailer */
  840. hdr_info = htc_hdr->flags;
  841. if (hdr_info & HTC_FLG_RX_TRAILER) {
  842. /* extract the trailer length */
  843. hdr_info = htc_hdr->ctrl[0];
  844. if ((hdr_info < sizeof(struct htc_record_hdr)) ||
  845. (hdr_info > payload_len)) {
  846. ath6kl_dbg(ATH6KL_DBG_HTC,
  847. "invalid header: payloadlen should be %d, CB[0]: %d\n",
  848. payload_len, hdr_info);
  849. status = -EINVAL;
  850. goto free_skb;
  851. }
  852. trailerlen = hdr_info;
  853. /* process trailer after hdr/apps payload */
  854. trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH +
  855. payload_len - hdr_info;
  856. status = htc_process_trailer(target, trailer, hdr_info,
  857. htc_hdr->eid);
  858. if (status != 0)
  859. goto free_skb;
  860. }
  861. if (((int) payload_len - (int) trailerlen) <= 0) {
  862. /* zero length packet with trailer, just drop these */
  863. goto free_skb;
  864. }
  865. if (htc_hdr->eid == ENDPOINT_0) {
  866. /* handle HTC control message */
  867. if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) {
  868. /*
  869. * fatal: target should not send unsolicited
  870. * messageson the endpoint 0
  871. */
  872. ath6kl_dbg(ATH6KL_DBG_HTC,
  873. "HTC ignores Rx Ctrl after setup complete\n");
  874. status = -EINVAL;
  875. goto free_skb;
  876. }
  877. /* remove HTC header */
  878. skb_pull(skb, HTC_HDR_LENGTH);
  879. netdata = skb->data;
  880. netlen = skb->len;
  881. spin_lock_bh(&target->rx_lock);
  882. target->pipe.ctrl_response_valid = true;
  883. target->pipe.ctrl_response_len = min_t(int, netlen,
  884. HTC_MAX_CTRL_MSG_LEN);
  885. memcpy(target->pipe.ctrl_response_buf, netdata,
  886. target->pipe.ctrl_response_len);
  887. spin_unlock_bh(&target->rx_lock);
  888. dev_kfree_skb(skb);
  889. skb = NULL;
  890. goto free_skb;
  891. }
  892. /*
  893. * TODO: the message based HIF architecture allocates net bufs
  894. * for recv packets since it bridges that HIF to upper layers,
  895. * which expects HTC packets, we form the packets here
  896. */
  897. packet = alloc_htc_packet_container(target);
  898. if (packet == NULL) {
  899. status = -ENOMEM;
  900. goto free_skb;
  901. }
  902. packet->status = 0;
  903. packet->endpoint = htc_hdr->eid;
  904. packet->pkt_cntxt = skb;
  905. /* TODO: for backwards compatibility */
  906. packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH;
  907. packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen;
  908. /*
  909. * TODO: this is a hack because the driver layer will set the
  910. * actual len of the skb again which will just double the len
  911. */
  912. skb_trim(skb, 0);
  913. recv_packet_completion(target, ep, packet);
  914. /* recover the packet container */
  915. free_htc_packet_container(target, packet);
  916. skb = NULL;
  917. free_skb:
  918. dev_kfree_skb(skb);
  919. return status;
  920. }
  921. static void htc_flush_rx_queue(struct htc_target *target,
  922. struct htc_endpoint *ep)
  923. {
  924. struct list_head container;
  925. struct htc_packet *packet;
  926. spin_lock_bh(&target->rx_lock);
  927. while (1) {
  928. if (list_empty(&ep->rx_bufq))
  929. break;
  930. packet = list_first_entry(&ep->rx_bufq,
  931. struct htc_packet, list);
  932. list_del(&packet->list);
  933. spin_unlock_bh(&target->rx_lock);
  934. packet->status = -ECANCELED;
  935. packet->act_len = 0;
  936. ath6kl_dbg(ATH6KL_DBG_HTC,
  937. "Flushing RX packet:0x%p, length:%d, ep:%d\n",
  938. packet, packet->buf_len,
  939. packet->endpoint);
  940. INIT_LIST_HEAD(&container);
  941. list_add_tail(&packet->list, &container);
  942. /* give the packet back */
  943. do_recv_completion(ep, &container);
  944. spin_lock_bh(&target->rx_lock);
  945. }
  946. spin_unlock_bh(&target->rx_lock);
  947. }
  948. /* polling routine to wait for a control packet to be received */
  949. static int htc_wait_recv_ctrl_message(struct htc_target *target)
  950. {
  951. int count = HTC_TARGET_RESPONSE_POLL_COUNT;
  952. while (count > 0) {
  953. spin_lock_bh(&target->rx_lock);
  954. if (target->pipe.ctrl_response_valid) {
  955. target->pipe.ctrl_response_valid = false;
  956. spin_unlock_bh(&target->rx_lock);
  957. break;
  958. }
  959. spin_unlock_bh(&target->rx_lock);
  960. count--;
  961. msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT);
  962. }
  963. if (count <= 0) {
  964. ath6kl_warn("htc pipe control receive timeout!\n");
  965. return -ETIMEDOUT;
  966. }
  967. return 0;
  968. }
  969. static void htc_rxctrl_complete(struct htc_target *context,
  970. struct htc_packet *packet)
  971. {
  972. struct sk_buff *skb = packet->skb;
  973. if (packet->endpoint == ENDPOINT_0 &&
  974. packet->status == -ECANCELED &&
  975. skb != NULL)
  976. dev_kfree_skb(skb);
  977. }
  978. /* htc pipe initialization */
  979. static void reset_endpoint_states(struct htc_target *target)
  980. {
  981. struct htc_endpoint *ep;
  982. int i;
  983. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  984. ep = &target->endpoint[i];
  985. ep->svc_id = 0;
  986. ep->len_max = 0;
  987. ep->max_txq_depth = 0;
  988. ep->eid = i;
  989. INIT_LIST_HEAD(&ep->txq);
  990. INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
  991. INIT_LIST_HEAD(&ep->rx_bufq);
  992. ep->target = target;
  993. ep->pipe.tx_credit_flow_enabled = true;
  994. }
  995. }
  996. /* start HTC, this is called after all services are connected */
  997. static int htc_config_target_hif_pipe(struct htc_target *target)
  998. {
  999. return 0;
  1000. }
  1001. /* htc service functions */
  1002. static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
  1003. {
  1004. u8 allocation = 0;
  1005. int i;
  1006. for (i = 0; i < ENDPOINT_MAX; i++) {
  1007. if (target->pipe.txcredit_alloc[i].service_id == service_id)
  1008. allocation =
  1009. target->pipe.txcredit_alloc[i].credit_alloc;
  1010. }
  1011. if (allocation == 0) {
  1012. ath6kl_dbg(ATH6KL_DBG_HTC,
  1013. "HTC Service TX : 0x%2.2X : allocation is zero!\n",
  1014. service_id);
  1015. }
  1016. return allocation;
  1017. }
  1018. static int ath6kl_htc_pipe_conn_service(struct htc_target *target,
  1019. struct htc_service_connect_req *conn_req,
  1020. struct htc_service_connect_resp *conn_resp)
  1021. {
  1022. struct ath6kl *ar = target->dev->ar;
  1023. struct htc_packet *packet = NULL;
  1024. struct htc_conn_service_resp *resp_msg;
  1025. struct htc_conn_service_msg *conn_msg;
  1026. enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
  1027. bool disable_credit_flowctrl = false;
  1028. unsigned int max_msg_size = 0;
  1029. struct htc_endpoint *ep;
  1030. int length, status = 0;
  1031. struct sk_buff *skb;
  1032. u8 tx_alloc;
  1033. u16 flags;
  1034. if (conn_req->svc_id == 0) {
  1035. WARN_ON_ONCE(1);
  1036. status = -EINVAL;
  1037. goto free_packet;
  1038. }
  1039. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1040. /* special case for pseudo control service */
  1041. assigned_epid = ENDPOINT_0;
  1042. max_msg_size = HTC_MAX_CTRL_MSG_LEN;
  1043. tx_alloc = 0;
  1044. } else {
  1045. tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
  1046. if (tx_alloc == 0) {
  1047. status = -ENOMEM;
  1048. goto free_packet;
  1049. }
  1050. /* allocate a packet to send to the target */
  1051. packet = htc_alloc_txctrl_packet(target);
  1052. if (packet == NULL) {
  1053. WARN_ON_ONCE(1);
  1054. status = -ENOMEM;
  1055. goto free_packet;
  1056. }
  1057. skb = packet->skb;
  1058. length = sizeof(struct htc_conn_service_msg);
  1059. /* assemble connect service message */
  1060. conn_msg = skb_put(skb, length);
  1061. if (conn_msg == NULL) {
  1062. WARN_ON_ONCE(1);
  1063. status = -EINVAL;
  1064. goto free_packet;
  1065. }
  1066. memset(conn_msg, 0,
  1067. sizeof(struct htc_conn_service_msg));
  1068. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1069. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1070. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
  1071. ~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
  1072. /* tell target desired recv alloc for this ep */
  1073. flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
  1074. conn_msg->conn_flags |= cpu_to_le16(flags);
  1075. if (conn_req->conn_flags &
  1076. HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
  1077. disable_credit_flowctrl = true;
  1078. }
  1079. set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
  1080. length,
  1081. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1082. status = ath6kl_htc_pipe_tx(target, packet);
  1083. /* we don't own it anymore */
  1084. packet = NULL;
  1085. if (status != 0)
  1086. goto free_packet;
  1087. /* wait for response */
  1088. status = htc_wait_recv_ctrl_message(target);
  1089. if (status != 0)
  1090. goto free_packet;
  1091. /* we controlled the buffer creation so it has to be
  1092. * properly aligned
  1093. */
  1094. resp_msg = (struct htc_conn_service_resp *)
  1095. target->pipe.ctrl_response_buf;
  1096. if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
  1097. (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
  1098. /* this message is not valid */
  1099. WARN_ON_ONCE(1);
  1100. status = -EINVAL;
  1101. goto free_packet;
  1102. }
  1103. ath6kl_dbg(ATH6KL_DBG_TRC,
  1104. "%s: service 0x%X conn resp: status: %d ep: %d\n",
  1105. __func__, resp_msg->svc_id, resp_msg->status,
  1106. resp_msg->eid);
  1107. conn_resp->resp_code = resp_msg->status;
  1108. /* check response status */
  1109. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1110. ath6kl_dbg(ATH6KL_DBG_HTC,
  1111. "Target failed service 0x%X connect request (status:%d)\n",
  1112. resp_msg->svc_id, resp_msg->status);
  1113. status = -EINVAL;
  1114. goto free_packet;
  1115. }
  1116. assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
  1117. max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
  1118. }
  1119. /* the rest are parameter checks so set the error status */
  1120. status = -EINVAL;
  1121. if (assigned_epid >= ENDPOINT_MAX) {
  1122. WARN_ON_ONCE(1);
  1123. goto free_packet;
  1124. }
  1125. if (max_msg_size == 0) {
  1126. WARN_ON_ONCE(1);
  1127. goto free_packet;
  1128. }
  1129. ep = &target->endpoint[assigned_epid];
  1130. ep->eid = assigned_epid;
  1131. if (ep->svc_id != 0) {
  1132. /* endpoint already in use! */
  1133. WARN_ON_ONCE(1);
  1134. goto free_packet;
  1135. }
  1136. /* return assigned endpoint to caller */
  1137. conn_resp->endpoint = assigned_epid;
  1138. conn_resp->len_max = max_msg_size;
  1139. /* setup the endpoint */
  1140. ep->svc_id = conn_req->svc_id; /* this marks ep in use */
  1141. ep->max_txq_depth = conn_req->max_txq_depth;
  1142. ep->len_max = max_msg_size;
  1143. ep->cred_dist.credits = tx_alloc;
  1144. ep->cred_dist.cred_sz = target->tgt_cred_sz;
  1145. ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
  1146. if (max_msg_size % target->tgt_cred_sz)
  1147. ep->cred_dist.cred_per_msg++;
  1148. /* copy all the callbacks */
  1149. ep->ep_cb = conn_req->ep_cb;
  1150. /* initialize tx_drop_packet_threshold */
  1151. ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
  1152. status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
  1153. &ep->pipe.pipeid_ul,
  1154. &ep->pipe.pipeid_dl);
  1155. if (status != 0)
  1156. goto free_packet;
  1157. ath6kl_dbg(ATH6KL_DBG_HTC,
  1158. "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
  1159. ep->svc_id, ep->pipe.pipeid_ul,
  1160. ep->pipe.pipeid_dl, ep->eid);
  1161. if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
  1162. ep->pipe.tx_credit_flow_enabled = false;
  1163. ath6kl_dbg(ATH6KL_DBG_HTC,
  1164. "SVC: 0x%4.4X ep:%d TX flow control off\n",
  1165. ep->svc_id, assigned_epid);
  1166. }
  1167. free_packet:
  1168. if (packet != NULL)
  1169. htc_free_txctrl_packet(target, packet);
  1170. return status;
  1171. }
  1172. /* htc export functions */
  1173. static void *ath6kl_htc_pipe_create(struct ath6kl *ar)
  1174. {
  1175. int status = 0;
  1176. struct htc_endpoint *ep = NULL;
  1177. struct htc_target *target = NULL;
  1178. struct htc_packet *packet;
  1179. int i;
  1180. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  1181. if (target == NULL) {
  1182. ath6kl_err("htc create unable to allocate memory\n");
  1183. status = -ENOMEM;
  1184. goto fail_htc_create;
  1185. }
  1186. spin_lock_init(&target->htc_lock);
  1187. spin_lock_init(&target->rx_lock);
  1188. spin_lock_init(&target->tx_lock);
  1189. reset_endpoint_states(target);
  1190. for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
  1191. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  1192. if (packet != NULL)
  1193. free_htc_packet_container(target, packet);
  1194. }
  1195. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1196. if (!target->dev) {
  1197. ath6kl_err("unable to allocate memory\n");
  1198. status = -ENOMEM;
  1199. goto fail_htc_create;
  1200. }
  1201. target->dev->ar = ar;
  1202. target->dev->htc_cnxt = target;
  1203. /* Get HIF default pipe for HTC message exchange */
  1204. ep = &target->endpoint[ENDPOINT_0];
  1205. ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
  1206. &ep->pipe.pipeid_dl);
  1207. return target;
  1208. fail_htc_create:
  1209. if (status != 0) {
  1210. if (target != NULL)
  1211. ath6kl_htc_pipe_cleanup(target);
  1212. target = NULL;
  1213. }
  1214. return target;
  1215. }
  1216. /* cleanup the HTC instance */
  1217. static void ath6kl_htc_pipe_cleanup(struct htc_target *target)
  1218. {
  1219. struct htc_packet *packet;
  1220. while (true) {
  1221. packet = alloc_htc_packet_container(target);
  1222. if (packet == NULL)
  1223. break;
  1224. kfree(packet);
  1225. }
  1226. kfree(target->dev);
  1227. /* kfree our instance */
  1228. kfree(target);
  1229. }
  1230. static int ath6kl_htc_pipe_start(struct htc_target *target)
  1231. {
  1232. struct sk_buff *skb;
  1233. struct htc_setup_comp_ext_msg *setup;
  1234. struct htc_packet *packet;
  1235. htc_config_target_hif_pipe(target);
  1236. /* allocate a buffer to send */
  1237. packet = htc_alloc_txctrl_packet(target);
  1238. if (packet == NULL) {
  1239. WARN_ON_ONCE(1);
  1240. return -ENOMEM;
  1241. }
  1242. skb = packet->skb;
  1243. /* assemble setup complete message */
  1244. setup = skb_put(skb, 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_warn("invalid htc pipe 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_warn("invalid htc pipe 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. struct htc_endpoint *endpoint;
  1378. struct htc_packet *packet, *tmp_pkt;
  1379. int i;
  1380. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1381. endpoint = &target->endpoint[i];
  1382. spin_lock_bh(&target->rx_lock);
  1383. list_for_each_entry_safe(packet, tmp_pkt,
  1384. &endpoint->rx_bufq, list) {
  1385. list_del(&packet->list);
  1386. spin_unlock_bh(&target->rx_lock);
  1387. ath6kl_dbg(ATH6KL_DBG_HTC,
  1388. "htc rx flush pkt 0x%p len %d ep %d\n",
  1389. packet, packet->buf_len,
  1390. packet->endpoint);
  1391. dev_kfree_skb(packet->pkt_cntxt);
  1392. spin_lock_bh(&target->rx_lock);
  1393. }
  1394. spin_unlock_bh(&target->rx_lock);
  1395. }
  1396. }
  1397. static int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
  1398. struct ath6kl_htc_credit_info *info)
  1399. {
  1400. return 0;
  1401. }
  1402. static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
  1403. .create = ath6kl_htc_pipe_create,
  1404. .wait_target = ath6kl_htc_pipe_wait_target,
  1405. .start = ath6kl_htc_pipe_start,
  1406. .conn_service = ath6kl_htc_pipe_conn_service,
  1407. .tx = ath6kl_htc_pipe_tx,
  1408. .stop = ath6kl_htc_pipe_stop,
  1409. .cleanup = ath6kl_htc_pipe_cleanup,
  1410. .flush_txep = ath6kl_htc_pipe_flush_txep,
  1411. .flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
  1412. .activity_changed = ath6kl_htc_pipe_activity_changed,
  1413. .get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
  1414. .add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
  1415. .credit_setup = ath6kl_htc_pipe_credit_setup,
  1416. .tx_complete = ath6kl_htc_pipe_tx_complete,
  1417. .rx_complete = ath6kl_htc_pipe_rx_complete,
  1418. };
  1419. void ath6kl_htc_pipe_attach(struct ath6kl *ar)
  1420. {
  1421. ar->htc_ops = &ath6kl_htc_pipe_ops;
  1422. }