htc_pipe.c 42 KB

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