htc_pipe.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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. netdata = skb->data;
  635. htc_hdr = (struct htc_frame_hdr *) netdata;
  636. ep_id = htc_hdr->eid;
  637. ep = &target->endpoint[ep_id];
  638. packet = htc_lookup_tx_packet(target, ep, skb);
  639. if (packet == NULL) {
  640. /* may have already been flushed and freed */
  641. ath6kl_err("HTC TX lookup failed!\n");
  642. } else {
  643. /* will be giving this buffer back to upper layers */
  644. packet->status = 0;
  645. send_packet_completion(target, packet);
  646. }
  647. skb = NULL;
  648. if (!ep->pipe.tx_credit_flow_enabled) {
  649. /*
  650. * note: when using TX credit flow, the re-checking of queues
  651. * happens when credits flow back from the target. in the
  652. * non-TX credit case, we recheck after the packet completes
  653. */
  654. htc_try_send(target, ep, NULL);
  655. }
  656. return 0;
  657. }
  658. static int htc_send_packets_multiple(struct htc_target *target,
  659. struct list_head *pkt_queue)
  660. {
  661. struct htc_endpoint *ep;
  662. struct htc_packet *packet, *tmp_pkt;
  663. if (list_empty(pkt_queue))
  664. return -EINVAL;
  665. /* get first packet to find out which ep the packets will go into */
  666. packet = list_first_entry(pkt_queue, struct htc_packet, list);
  667. if (packet->endpoint >= ENDPOINT_MAX) {
  668. WARN_ON_ONCE(1);
  669. return -EINVAL;
  670. }
  671. ep = &target->endpoint[packet->endpoint];
  672. htc_try_send(target, ep, pkt_queue);
  673. /* do completion on any packets that couldn't get in */
  674. if (!list_empty(pkt_queue)) {
  675. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  676. packet->status = -ENOMEM;
  677. }
  678. do_send_completion(ep, pkt_queue);
  679. }
  680. return 0;
  681. }
  682. /* htc pipe rx path */
  683. static struct htc_packet *alloc_htc_packet_container(struct htc_target *target)
  684. {
  685. struct htc_packet *packet;
  686. spin_lock_bh(&target->rx_lock);
  687. if (target->pipe.htc_packet_pool == NULL) {
  688. spin_unlock_bh(&target->rx_lock);
  689. return NULL;
  690. }
  691. packet = target->pipe.htc_packet_pool;
  692. target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next;
  693. spin_unlock_bh(&target->rx_lock);
  694. packet->list.next = NULL;
  695. return packet;
  696. }
  697. static void free_htc_packet_container(struct htc_target *target,
  698. struct htc_packet *packet)
  699. {
  700. struct list_head *lh;
  701. spin_lock_bh(&target->rx_lock);
  702. if (target->pipe.htc_packet_pool == NULL) {
  703. target->pipe.htc_packet_pool = packet;
  704. packet->list.next = NULL;
  705. } else {
  706. lh = (struct list_head *) target->pipe.htc_packet_pool;
  707. packet->list.next = lh;
  708. target->pipe.htc_packet_pool = packet;
  709. }
  710. spin_unlock_bh(&target->rx_lock);
  711. }
  712. static int htc_process_trailer(struct htc_target *target, u8 *buffer,
  713. int len, enum htc_endpoint_id from_ep)
  714. {
  715. struct htc_credit_report *report;
  716. struct htc_record_hdr *record;
  717. u8 *record_buf;
  718. int status = 0;
  719. while (len > 0) {
  720. if (len < sizeof(struct htc_record_hdr)) {
  721. status = -EINVAL;
  722. break;
  723. }
  724. /* these are byte aligned structs */
  725. record = (struct htc_record_hdr *) buffer;
  726. len -= sizeof(struct htc_record_hdr);
  727. buffer += sizeof(struct htc_record_hdr);
  728. if (record->len > len) {
  729. /* no room left in buffer for record */
  730. ath6kl_dbg(ATH6KL_DBG_HTC,
  731. "invalid length: %d (id:%d) buffer has: %d bytes left\n",
  732. record->len, record->rec_id, len);
  733. status = -EINVAL;
  734. break;
  735. }
  736. /* start of record follows the header */
  737. record_buf = buffer;
  738. switch (record->rec_id) {
  739. case HTC_RECORD_CREDITS:
  740. if (record->len < sizeof(struct htc_credit_report)) {
  741. WARN_ON_ONCE(1);
  742. return -EINVAL;
  743. }
  744. report = (struct htc_credit_report *) record_buf;
  745. htc_process_credit_report(target, report,
  746. record->len / sizeof(*report),
  747. from_ep);
  748. break;
  749. default:
  750. ath6kl_dbg(ATH6KL_DBG_HTC,
  751. "unhandled record: id:%d length:%d\n",
  752. record->rec_id, record->len);
  753. break;
  754. }
  755. if (status != 0)
  756. break;
  757. /* advance buffer past this record for next time around */
  758. buffer += record->len;
  759. len -= record->len;
  760. }
  761. return status;
  762. }
  763. static void do_recv_completion(struct htc_endpoint *ep,
  764. struct list_head *queue_to_indicate)
  765. {
  766. struct htc_packet *packet;
  767. if (list_empty(queue_to_indicate)) {
  768. /* nothing to indicate */
  769. return;
  770. }
  771. /* using legacy EpRecv */
  772. while (!list_empty(queue_to_indicate)) {
  773. packet = list_first_entry(queue_to_indicate,
  774. struct htc_packet, list);
  775. list_del(&packet->list);
  776. ep->ep_cb.rx(ep->target, packet);
  777. }
  778. return;
  779. }
  780. static void recv_packet_completion(struct htc_target *target,
  781. struct htc_endpoint *ep,
  782. struct htc_packet *packet)
  783. {
  784. struct list_head container;
  785. INIT_LIST_HEAD(&container);
  786. list_add_tail(&packet->list, &container);
  787. /* do completion */
  788. do_recv_completion(ep, &container);
  789. }
  790. static int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb,
  791. u8 pipeid)
  792. {
  793. struct htc_target *target = ar->htc_target;
  794. u8 *netdata, *trailer, hdr_info;
  795. struct htc_frame_hdr *htc_hdr;
  796. u32 netlen, trailerlen = 0;
  797. struct htc_packet *packet;
  798. struct htc_endpoint *ep;
  799. u16 payload_len;
  800. int status = 0;
  801. /*
  802. * ar->htc_target can be NULL due to a race condition that can occur
  803. * during driver initialization(we do 'ath6kl_hif_power_on' before
  804. * initializing 'ar->htc_target' via 'ath6kl_htc_create').
  805. * 'ath6kl_hif_power_on' assigns 'ath6kl_recv_complete' as
  806. * usb_complete_t/callback function for 'usb_fill_bulk_urb'.
  807. * Thus the possibility of ar->htc_target being NULL
  808. * via ath6kl_recv_complete -> ath6kl_usb_io_comp_work.
  809. */
  810. if (WARN_ON_ONCE(!target)) {
  811. ath6kl_err("Target not yet initialized\n");
  812. status = -EINVAL;
  813. goto free_skb;
  814. }
  815. netdata = skb->data;
  816. netlen = skb->len;
  817. htc_hdr = (struct htc_frame_hdr *) netdata;
  818. if (htc_hdr->eid >= ENDPOINT_MAX) {
  819. ath6kl_dbg(ATH6KL_DBG_HTC,
  820. "HTC Rx: invalid EndpointID=%d\n",
  821. htc_hdr->eid);
  822. status = -EINVAL;
  823. goto free_skb;
  824. }
  825. ep = &target->endpoint[htc_hdr->eid];
  826. payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
  827. if (netlen < (payload_len + HTC_HDR_LENGTH)) {
  828. ath6kl_dbg(ATH6KL_DBG_HTC,
  829. "HTC Rx: insufficient length, got:%d expected =%zu\n",
  830. netlen, payload_len + HTC_HDR_LENGTH);
  831. status = -EINVAL;
  832. goto free_skb;
  833. }
  834. /* get flags to check for trailer */
  835. hdr_info = htc_hdr->flags;
  836. if (hdr_info & HTC_FLG_RX_TRAILER) {
  837. /* extract the trailer length */
  838. hdr_info = htc_hdr->ctrl[0];
  839. if ((hdr_info < sizeof(struct htc_record_hdr)) ||
  840. (hdr_info > payload_len)) {
  841. ath6kl_dbg(ATH6KL_DBG_HTC,
  842. "invalid header: payloadlen should be %d, CB[0]: %d\n",
  843. payload_len, hdr_info);
  844. status = -EINVAL;
  845. goto free_skb;
  846. }
  847. trailerlen = hdr_info;
  848. /* process trailer after hdr/apps payload */
  849. trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH +
  850. payload_len - hdr_info;
  851. status = htc_process_trailer(target, trailer, hdr_info,
  852. htc_hdr->eid);
  853. if (status != 0)
  854. goto free_skb;
  855. }
  856. if (((int) payload_len - (int) trailerlen) <= 0) {
  857. /* zero length packet with trailer, just drop these */
  858. goto free_skb;
  859. }
  860. if (htc_hdr->eid == ENDPOINT_0) {
  861. /* handle HTC control message */
  862. if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) {
  863. /*
  864. * fatal: target should not send unsolicited
  865. * messageson the endpoint 0
  866. */
  867. ath6kl_dbg(ATH6KL_DBG_HTC,
  868. "HTC ignores Rx Ctrl after setup complete\n");
  869. status = -EINVAL;
  870. goto free_skb;
  871. }
  872. /* remove HTC header */
  873. skb_pull(skb, HTC_HDR_LENGTH);
  874. netdata = skb->data;
  875. netlen = skb->len;
  876. spin_lock_bh(&target->rx_lock);
  877. target->pipe.ctrl_response_valid = true;
  878. target->pipe.ctrl_response_len = min_t(int, netlen,
  879. HTC_MAX_CTRL_MSG_LEN);
  880. memcpy(target->pipe.ctrl_response_buf, netdata,
  881. target->pipe.ctrl_response_len);
  882. spin_unlock_bh(&target->rx_lock);
  883. dev_kfree_skb(skb);
  884. skb = NULL;
  885. goto free_skb;
  886. }
  887. /*
  888. * TODO: the message based HIF architecture allocates net bufs
  889. * for recv packets since it bridges that HIF to upper layers,
  890. * which expects HTC packets, we form the packets here
  891. */
  892. packet = alloc_htc_packet_container(target);
  893. if (packet == NULL) {
  894. status = -ENOMEM;
  895. goto free_skb;
  896. }
  897. packet->status = 0;
  898. packet->endpoint = htc_hdr->eid;
  899. packet->pkt_cntxt = skb;
  900. /* TODO: for backwards compatibility */
  901. packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH;
  902. packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen;
  903. /*
  904. * TODO: this is a hack because the driver layer will set the
  905. * actual len of the skb again which will just double the len
  906. */
  907. skb_trim(skb, 0);
  908. recv_packet_completion(target, ep, packet);
  909. /* recover the packet container */
  910. free_htc_packet_container(target, packet);
  911. skb = NULL;
  912. free_skb:
  913. dev_kfree_skb(skb);
  914. return status;
  915. }
  916. static void htc_flush_rx_queue(struct htc_target *target,
  917. struct htc_endpoint *ep)
  918. {
  919. struct list_head container;
  920. struct htc_packet *packet;
  921. spin_lock_bh(&target->rx_lock);
  922. while (1) {
  923. if (list_empty(&ep->rx_bufq))
  924. break;
  925. packet = list_first_entry(&ep->rx_bufq,
  926. struct htc_packet, list);
  927. list_del(&packet->list);
  928. spin_unlock_bh(&target->rx_lock);
  929. packet->status = -ECANCELED;
  930. packet->act_len = 0;
  931. ath6kl_dbg(ATH6KL_DBG_HTC,
  932. "Flushing RX packet:0x%p, length:%d, ep:%d\n",
  933. packet, packet->buf_len,
  934. packet->endpoint);
  935. INIT_LIST_HEAD(&container);
  936. list_add_tail(&packet->list, &container);
  937. /* give the packet back */
  938. do_recv_completion(ep, &container);
  939. spin_lock_bh(&target->rx_lock);
  940. }
  941. spin_unlock_bh(&target->rx_lock);
  942. }
  943. /* polling routine to wait for a control packet to be received */
  944. static int htc_wait_recv_ctrl_message(struct htc_target *target)
  945. {
  946. int count = HTC_TARGET_RESPONSE_POLL_COUNT;
  947. while (count > 0) {
  948. spin_lock_bh(&target->rx_lock);
  949. if (target->pipe.ctrl_response_valid) {
  950. target->pipe.ctrl_response_valid = false;
  951. spin_unlock_bh(&target->rx_lock);
  952. break;
  953. }
  954. spin_unlock_bh(&target->rx_lock);
  955. count--;
  956. msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT);
  957. }
  958. if (count <= 0) {
  959. ath6kl_warn("htc pipe control receive timeout!\n");
  960. return -ETIMEDOUT;
  961. }
  962. return 0;
  963. }
  964. static void htc_rxctrl_complete(struct htc_target *context,
  965. struct htc_packet *packet)
  966. {
  967. struct sk_buff *skb = packet->skb;
  968. if (packet->endpoint == ENDPOINT_0 &&
  969. packet->status == -ECANCELED &&
  970. skb != NULL)
  971. dev_kfree_skb(skb);
  972. }
  973. /* htc pipe initialization */
  974. static void reset_endpoint_states(struct htc_target *target)
  975. {
  976. struct htc_endpoint *ep;
  977. int i;
  978. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  979. ep = &target->endpoint[i];
  980. ep->svc_id = 0;
  981. ep->len_max = 0;
  982. ep->max_txq_depth = 0;
  983. ep->eid = i;
  984. INIT_LIST_HEAD(&ep->txq);
  985. INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
  986. INIT_LIST_HEAD(&ep->rx_bufq);
  987. ep->target = target;
  988. ep->pipe.tx_credit_flow_enabled = true;
  989. }
  990. }
  991. /* start HTC, this is called after all services are connected */
  992. static int htc_config_target_hif_pipe(struct htc_target *target)
  993. {
  994. return 0;
  995. }
  996. /* htc service functions */
  997. static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
  998. {
  999. u8 allocation = 0;
  1000. int i;
  1001. for (i = 0; i < ENDPOINT_MAX; i++) {
  1002. if (target->pipe.txcredit_alloc[i].service_id == service_id)
  1003. allocation =
  1004. target->pipe.txcredit_alloc[i].credit_alloc;
  1005. }
  1006. if (allocation == 0) {
  1007. ath6kl_dbg(ATH6KL_DBG_HTC,
  1008. "HTC Service TX : 0x%2.2X : allocation is zero!\n",
  1009. service_id);
  1010. }
  1011. return allocation;
  1012. }
  1013. static int ath6kl_htc_pipe_conn_service(struct htc_target *target,
  1014. struct htc_service_connect_req *conn_req,
  1015. struct htc_service_connect_resp *conn_resp)
  1016. {
  1017. struct ath6kl *ar = target->dev->ar;
  1018. struct htc_packet *packet = NULL;
  1019. struct htc_conn_service_resp *resp_msg;
  1020. struct htc_conn_service_msg *conn_msg;
  1021. enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
  1022. bool disable_credit_flowctrl = false;
  1023. unsigned int max_msg_size = 0;
  1024. struct htc_endpoint *ep;
  1025. int length, status = 0;
  1026. struct sk_buff *skb;
  1027. u8 tx_alloc;
  1028. u16 flags;
  1029. if (conn_req->svc_id == 0) {
  1030. WARN_ON_ONCE(1);
  1031. status = -EINVAL;
  1032. goto free_packet;
  1033. }
  1034. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1035. /* special case for pseudo control service */
  1036. assigned_epid = ENDPOINT_0;
  1037. max_msg_size = HTC_MAX_CTRL_MSG_LEN;
  1038. tx_alloc = 0;
  1039. } else {
  1040. tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
  1041. if (tx_alloc == 0) {
  1042. status = -ENOMEM;
  1043. goto free_packet;
  1044. }
  1045. /* allocate a packet to send to the target */
  1046. packet = htc_alloc_txctrl_packet(target);
  1047. if (packet == NULL) {
  1048. WARN_ON_ONCE(1);
  1049. status = -ENOMEM;
  1050. goto free_packet;
  1051. }
  1052. skb = packet->skb;
  1053. length = sizeof(struct htc_conn_service_msg);
  1054. /* assemble connect service message */
  1055. conn_msg = skb_put(skb, length);
  1056. if (conn_msg == NULL) {
  1057. WARN_ON_ONCE(1);
  1058. status = -EINVAL;
  1059. goto free_packet;
  1060. }
  1061. memset(conn_msg, 0,
  1062. sizeof(struct htc_conn_service_msg));
  1063. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1064. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1065. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
  1066. ~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
  1067. /* tell target desired recv alloc for this ep */
  1068. flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
  1069. conn_msg->conn_flags |= cpu_to_le16(flags);
  1070. if (conn_req->conn_flags &
  1071. HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
  1072. disable_credit_flowctrl = true;
  1073. }
  1074. set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
  1075. length,
  1076. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1077. status = ath6kl_htc_pipe_tx(target, packet);
  1078. /* we don't own it anymore */
  1079. packet = NULL;
  1080. if (status != 0)
  1081. goto free_packet;
  1082. /* wait for response */
  1083. status = htc_wait_recv_ctrl_message(target);
  1084. if (status != 0)
  1085. goto free_packet;
  1086. /* we controlled the buffer creation so it has to be
  1087. * properly aligned
  1088. */
  1089. resp_msg = (struct htc_conn_service_resp *)
  1090. target->pipe.ctrl_response_buf;
  1091. if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
  1092. (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
  1093. /* this message is not valid */
  1094. WARN_ON_ONCE(1);
  1095. status = -EINVAL;
  1096. goto free_packet;
  1097. }
  1098. ath6kl_dbg(ATH6KL_DBG_TRC,
  1099. "%s: service 0x%X conn resp: status: %d ep: %d\n",
  1100. __func__, resp_msg->svc_id, resp_msg->status,
  1101. resp_msg->eid);
  1102. conn_resp->resp_code = resp_msg->status;
  1103. /* check response status */
  1104. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1105. ath6kl_dbg(ATH6KL_DBG_HTC,
  1106. "Target failed service 0x%X connect request (status:%d)\n",
  1107. resp_msg->svc_id, resp_msg->status);
  1108. status = -EINVAL;
  1109. goto free_packet;
  1110. }
  1111. assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
  1112. max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
  1113. }
  1114. /* the rest are parameter checks so set the error status */
  1115. status = -EINVAL;
  1116. if (assigned_epid >= ENDPOINT_MAX) {
  1117. WARN_ON_ONCE(1);
  1118. goto free_packet;
  1119. }
  1120. if (max_msg_size == 0) {
  1121. WARN_ON_ONCE(1);
  1122. goto free_packet;
  1123. }
  1124. ep = &target->endpoint[assigned_epid];
  1125. ep->eid = assigned_epid;
  1126. if (ep->svc_id != 0) {
  1127. /* endpoint already in use! */
  1128. WARN_ON_ONCE(1);
  1129. goto free_packet;
  1130. }
  1131. /* return assigned endpoint to caller */
  1132. conn_resp->endpoint = assigned_epid;
  1133. conn_resp->len_max = max_msg_size;
  1134. /* setup the endpoint */
  1135. ep->svc_id = conn_req->svc_id; /* this marks ep in use */
  1136. ep->max_txq_depth = conn_req->max_txq_depth;
  1137. ep->len_max = max_msg_size;
  1138. ep->cred_dist.credits = tx_alloc;
  1139. ep->cred_dist.cred_sz = target->tgt_cred_sz;
  1140. ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
  1141. if (max_msg_size % target->tgt_cred_sz)
  1142. ep->cred_dist.cred_per_msg++;
  1143. /* copy all the callbacks */
  1144. ep->ep_cb = conn_req->ep_cb;
  1145. /* initialize tx_drop_packet_threshold */
  1146. ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
  1147. status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
  1148. &ep->pipe.pipeid_ul,
  1149. &ep->pipe.pipeid_dl);
  1150. if (status != 0)
  1151. goto free_packet;
  1152. ath6kl_dbg(ATH6KL_DBG_HTC,
  1153. "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
  1154. ep->svc_id, ep->pipe.pipeid_ul,
  1155. ep->pipe.pipeid_dl, ep->eid);
  1156. if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
  1157. ep->pipe.tx_credit_flow_enabled = false;
  1158. ath6kl_dbg(ATH6KL_DBG_HTC,
  1159. "SVC: 0x%4.4X ep:%d TX flow control off\n",
  1160. ep->svc_id, assigned_epid);
  1161. }
  1162. free_packet:
  1163. if (packet != NULL)
  1164. htc_free_txctrl_packet(target, packet);
  1165. return status;
  1166. }
  1167. /* htc export functions */
  1168. static void *ath6kl_htc_pipe_create(struct ath6kl *ar)
  1169. {
  1170. int status = 0;
  1171. struct htc_endpoint *ep = NULL;
  1172. struct htc_target *target = NULL;
  1173. struct htc_packet *packet;
  1174. int i;
  1175. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  1176. if (target == NULL) {
  1177. ath6kl_err("htc create unable to allocate memory\n");
  1178. status = -ENOMEM;
  1179. goto fail_htc_create;
  1180. }
  1181. spin_lock_init(&target->htc_lock);
  1182. spin_lock_init(&target->rx_lock);
  1183. spin_lock_init(&target->tx_lock);
  1184. reset_endpoint_states(target);
  1185. for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
  1186. packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
  1187. if (packet != NULL)
  1188. free_htc_packet_container(target, packet);
  1189. }
  1190. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1191. if (!target->dev) {
  1192. ath6kl_err("unable to allocate memory\n");
  1193. status = -ENOMEM;
  1194. goto fail_htc_create;
  1195. }
  1196. target->dev->ar = ar;
  1197. target->dev->htc_cnxt = target;
  1198. /* Get HIF default pipe for HTC message exchange */
  1199. ep = &target->endpoint[ENDPOINT_0];
  1200. ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
  1201. &ep->pipe.pipeid_dl);
  1202. return target;
  1203. fail_htc_create:
  1204. if (status != 0) {
  1205. if (target != NULL)
  1206. ath6kl_htc_pipe_cleanup(target);
  1207. target = NULL;
  1208. }
  1209. return target;
  1210. }
  1211. /* cleanup the HTC instance */
  1212. static void ath6kl_htc_pipe_cleanup(struct htc_target *target)
  1213. {
  1214. struct htc_packet *packet;
  1215. while (true) {
  1216. packet = alloc_htc_packet_container(target);
  1217. if (packet == NULL)
  1218. break;
  1219. kfree(packet);
  1220. }
  1221. kfree(target->dev);
  1222. /* kfree our instance */
  1223. kfree(target);
  1224. }
  1225. static int ath6kl_htc_pipe_start(struct htc_target *target)
  1226. {
  1227. struct sk_buff *skb;
  1228. struct htc_setup_comp_ext_msg *setup;
  1229. struct htc_packet *packet;
  1230. htc_config_target_hif_pipe(target);
  1231. /* allocate a buffer to send */
  1232. packet = htc_alloc_txctrl_packet(target);
  1233. if (packet == NULL) {
  1234. WARN_ON_ONCE(1);
  1235. return -ENOMEM;
  1236. }
  1237. skb = packet->skb;
  1238. /* assemble setup complete message */
  1239. setup = skb_put(skb, sizeof(*setup));
  1240. memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg));
  1241. setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
  1242. ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n");
  1243. set_htc_pkt_info(packet, NULL, (u8 *) setup,
  1244. sizeof(struct htc_setup_comp_ext_msg),
  1245. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1246. target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE;
  1247. return ath6kl_htc_pipe_tx(target, packet);
  1248. }
  1249. static void ath6kl_htc_pipe_stop(struct htc_target *target)
  1250. {
  1251. int i;
  1252. struct htc_endpoint *ep;
  1253. /* cleanup endpoints */
  1254. for (i = 0; i < ENDPOINT_MAX; i++) {
  1255. ep = &target->endpoint[i];
  1256. htc_flush_rx_queue(target, ep);
  1257. htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL);
  1258. }
  1259. reset_endpoint_states(target);
  1260. target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE;
  1261. }
  1262. static int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target,
  1263. enum htc_endpoint_id endpoint)
  1264. {
  1265. int num;
  1266. spin_lock_bh(&target->rx_lock);
  1267. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1268. spin_unlock_bh(&target->rx_lock);
  1269. return num;
  1270. }
  1271. static int ath6kl_htc_pipe_tx(struct htc_target *target,
  1272. struct htc_packet *packet)
  1273. {
  1274. struct list_head queue;
  1275. ath6kl_dbg(ATH6KL_DBG_HTC,
  1276. "%s: endPointId: %d, buffer: 0x%p, length: %d\n",
  1277. __func__, packet->endpoint, packet->buf,
  1278. packet->act_len);
  1279. INIT_LIST_HEAD(&queue);
  1280. list_add_tail(&packet->list, &queue);
  1281. return htc_send_packets_multiple(target, &queue);
  1282. }
  1283. static int ath6kl_htc_pipe_wait_target(struct htc_target *target)
  1284. {
  1285. struct htc_ready_ext_msg *ready_msg;
  1286. struct htc_service_connect_req connect;
  1287. struct htc_service_connect_resp resp;
  1288. int status = 0;
  1289. status = htc_wait_recv_ctrl_message(target);
  1290. if (status != 0)
  1291. return status;
  1292. if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) {
  1293. ath6kl_warn("invalid htc pipe ready msg len: %d\n",
  1294. target->pipe.ctrl_response_len);
  1295. return -ECOMM;
  1296. }
  1297. ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf;
  1298. if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) {
  1299. ath6kl_warn("invalid htc pipe ready msg: 0x%x\n",
  1300. ready_msg->ver2_0_info.msg_id);
  1301. return -ECOMM;
  1302. }
  1303. ath6kl_dbg(ATH6KL_DBG_HTC,
  1304. "Target Ready! : transmit resources : %d size:%d\n",
  1305. ready_msg->ver2_0_info.cred_cnt,
  1306. ready_msg->ver2_0_info.cred_sz);
  1307. target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt);
  1308. target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz);
  1309. if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0))
  1310. return -ECOMM;
  1311. htc_setup_target_buffer_assignments(target);
  1312. /* setup our pseudo HTC control endpoint connection */
  1313. memset(&connect, 0, sizeof(connect));
  1314. memset(&resp, 0, sizeof(resp));
  1315. connect.ep_cb.tx_complete = htc_txctrl_complete;
  1316. connect.ep_cb.rx = htc_rxctrl_complete;
  1317. connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS;
  1318. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1319. /* connect fake service */
  1320. status = ath6kl_htc_pipe_conn_service(target, &connect, &resp);
  1321. return status;
  1322. }
  1323. static void ath6kl_htc_pipe_flush_txep(struct htc_target *target,
  1324. enum htc_endpoint_id endpoint, u16 tag)
  1325. {
  1326. struct htc_endpoint *ep = &target->endpoint[endpoint];
  1327. if (ep->svc_id == 0) {
  1328. WARN_ON_ONCE(1);
  1329. /* not in use.. */
  1330. return;
  1331. }
  1332. htc_flush_tx_endpoint(target, ep, tag);
  1333. }
  1334. static int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target,
  1335. struct list_head *pkt_queue)
  1336. {
  1337. struct htc_packet *packet, *tmp_pkt, *first;
  1338. struct htc_endpoint *ep;
  1339. int status = 0;
  1340. if (list_empty(pkt_queue))
  1341. return -EINVAL;
  1342. first = list_first_entry(pkt_queue, struct htc_packet, list);
  1343. if (first->endpoint >= ENDPOINT_MAX) {
  1344. WARN_ON_ONCE(1);
  1345. return -EINVAL;
  1346. }
  1347. ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n",
  1348. __func__, first->endpoint, get_queue_depth(pkt_queue),
  1349. first->buf_len);
  1350. ep = &target->endpoint[first->endpoint];
  1351. spin_lock_bh(&target->rx_lock);
  1352. /* store receive packets */
  1353. list_splice_tail_init(pkt_queue, &ep->rx_bufq);
  1354. spin_unlock_bh(&target->rx_lock);
  1355. if (status != 0) {
  1356. /* walk through queue and mark each one canceled */
  1357. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  1358. packet->status = -ECANCELED;
  1359. }
  1360. do_recv_completion(ep, pkt_queue);
  1361. }
  1362. return status;
  1363. }
  1364. static void ath6kl_htc_pipe_activity_changed(struct htc_target *target,
  1365. enum htc_endpoint_id ep,
  1366. bool active)
  1367. {
  1368. /* TODO */
  1369. }
  1370. static void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target)
  1371. {
  1372. struct htc_endpoint *endpoint;
  1373. struct htc_packet *packet, *tmp_pkt;
  1374. int i;
  1375. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1376. endpoint = &target->endpoint[i];
  1377. spin_lock_bh(&target->rx_lock);
  1378. list_for_each_entry_safe(packet, tmp_pkt,
  1379. &endpoint->rx_bufq, list) {
  1380. list_del(&packet->list);
  1381. spin_unlock_bh(&target->rx_lock);
  1382. ath6kl_dbg(ATH6KL_DBG_HTC,
  1383. "htc rx flush pkt 0x%p len %d ep %d\n",
  1384. packet, packet->buf_len,
  1385. packet->endpoint);
  1386. dev_kfree_skb(packet->pkt_cntxt);
  1387. spin_lock_bh(&target->rx_lock);
  1388. }
  1389. spin_unlock_bh(&target->rx_lock);
  1390. }
  1391. }
  1392. static int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
  1393. struct ath6kl_htc_credit_info *info)
  1394. {
  1395. return 0;
  1396. }
  1397. static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
  1398. .create = ath6kl_htc_pipe_create,
  1399. .wait_target = ath6kl_htc_pipe_wait_target,
  1400. .start = ath6kl_htc_pipe_start,
  1401. .conn_service = ath6kl_htc_pipe_conn_service,
  1402. .tx = ath6kl_htc_pipe_tx,
  1403. .stop = ath6kl_htc_pipe_stop,
  1404. .cleanup = ath6kl_htc_pipe_cleanup,
  1405. .flush_txep = ath6kl_htc_pipe_flush_txep,
  1406. .flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
  1407. .activity_changed = ath6kl_htc_pipe_activity_changed,
  1408. .get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
  1409. .add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
  1410. .credit_setup = ath6kl_htc_pipe_credit_setup,
  1411. .tx_complete = ath6kl_htc_pipe_tx_complete,
  1412. .rx_complete = ath6kl_htc_pipe_rx_complete,
  1413. };
  1414. void ath6kl_htc_pipe_attach(struct ath6kl *ar)
  1415. {
  1416. ar->htc_ops = &ath6kl_htc_pipe_ops;
  1417. }