epping_tx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2014-2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*========================================================================
  19. \file epping_tx.c
  20. \brief WLAN End Point Ping test tool implementation
  21. ========================================================================*/
  22. /*--------------------------------------------------------------------------
  23. Include Files
  24. ------------------------------------------------------------------------*/
  25. #include <cds_api.h>
  26. #include <cds_sched.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/firmware.h>
  29. #include <wni_api.h>
  30. #include <wlan_ptt_sock_svc.h>
  31. #include <linux/wireless.h>
  32. #include <net/cfg80211.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/semaphore.h>
  35. #include <linux/ctype.h>
  36. #include "epping_main.h"
  37. #include "epping_internal.h"
  38. #include "epping_test.h"
  39. #define TX_RETRY_TIMEOUT_IN_MS 1
  40. static bool enb_tx_dump;
  41. void epping_tx_dup_pkt(epping_adapter_t *adapter,
  42. HTC_ENDPOINT_ID eid, qdf_nbuf_t skb)
  43. {
  44. struct epping_cookie *cookie = NULL;
  45. int skb_len, ret;
  46. qdf_nbuf_t new_skb;
  47. cookie = epping_alloc_cookie(adapter->pEpping_ctx);
  48. if (!cookie) {
  49. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  50. "%s: epping_alloc_cookie returns no resource\n",
  51. __func__);
  52. return;
  53. }
  54. new_skb = qdf_nbuf_copy(skb);
  55. if (!new_skb) {
  56. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  57. "%s: qdf_nbuf_copy returns no resource\n", __func__);
  58. epping_free_cookie(adapter->pEpping_ctx, cookie);
  59. return;
  60. }
  61. SET_HTC_PACKET_INFO_TX(&cookie->HtcPkt,
  62. cookie, qdf_nbuf_data(skb),
  63. qdf_nbuf_len(new_skb), eid, 0);
  64. SET_HTC_PACKET_NET_BUF_CONTEXT(&cookie->HtcPkt, new_skb);
  65. skb_len = (int)qdf_nbuf_len(new_skb);
  66. /* send the packet */
  67. ret = htc_send_pkt(adapter->pEpping_ctx->HTCHandle, &cookie->HtcPkt);
  68. if (ret != QDF_STATUS_SUCCESS) {
  69. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  70. "%s: htc_send_pkt failed, ret = %d\n", __func__, ret);
  71. epping_free_cookie(adapter->pEpping_ctx, cookie);
  72. qdf_nbuf_free(new_skb);
  73. return;
  74. }
  75. adapter->stats.tx_bytes += skb_len;
  76. ++adapter->stats.tx_packets;
  77. if (((adapter->stats.tx_packets +
  78. adapter->stats.tx_dropped) % EPPING_STATS_LOG_COUNT) == 0 &&
  79. (adapter->stats.tx_packets || adapter->stats.tx_dropped)) {
  80. epping_log_stats(adapter, __func__);
  81. }
  82. }
  83. static int epping_tx_send_int(qdf_nbuf_t skb, epping_adapter_t *adapter)
  84. {
  85. EPPING_HEADER *eppingHdr = (EPPING_HEADER *) qdf_nbuf_data(skb);
  86. HTC_ENDPOINT_ID eid = ENDPOINT_UNUSED;
  87. struct epping_cookie *cookie = NULL;
  88. uint8_t ac = 0;
  89. QDF_STATUS ret = QDF_STATUS_SUCCESS;
  90. int skb_len;
  91. EPPING_HEADER tmpHdr = *eppingHdr;
  92. /* allocate resource for this packet */
  93. cookie = epping_alloc_cookie(adapter->pEpping_ctx);
  94. /* no resource */
  95. if (!cookie) {
  96. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  97. "%s: epping_alloc_cookie returns no resource\n",
  98. __func__);
  99. return A_ERROR;
  100. }
  101. if (enb_tx_dump)
  102. epping_hex_dump((void *)eppingHdr, skb->len, __func__);
  103. /*
  104. * a quirk of linux, the payload of the frame is 32-bit aligned and thus
  105. * the addition of the HTC header will mis-align the start of the HTC
  106. * frame, so we add some padding which will be stripped off in the target
  107. */
  108. if (EPPING_ALIGNMENT_PAD > 0) {
  109. A_NETBUF_PUSH(skb, EPPING_ALIGNMENT_PAD);
  110. }
  111. /* prepare ep/HTC information */
  112. ac = eppingHdr->StreamNo_h;
  113. eid = adapter->pEpping_ctx->EppingEndpoint[ac];
  114. if (eid < 0 || eid >= EPPING_MAX_NUM_EPIDS) {
  115. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  116. "%s: invalid eid = %d, ac = %d\n", __func__, eid,
  117. ac);
  118. return A_ERROR;
  119. }
  120. if (tmpHdr.Cmd_h == EPPING_CMD_RESET_RECV_CNT ||
  121. tmpHdr.Cmd_h == EPPING_CMD_CONT_RX_START) {
  122. epping_set_kperf_flag(adapter, eid, tmpHdr.CmdBuffer_t[0]);
  123. }
  124. SET_HTC_PACKET_INFO_TX(&cookie->HtcPkt,
  125. cookie, qdf_nbuf_data(skb), qdf_nbuf_len(skb),
  126. eid, 0);
  127. SET_HTC_PACKET_NET_BUF_CONTEXT(&cookie->HtcPkt, skb);
  128. skb_len = skb->len;
  129. /* send the packet */
  130. ret = htc_send_pkt(adapter->pEpping_ctx->HTCHandle, &cookie->HtcPkt);
  131. epping_log_packet(adapter, &tmpHdr, ret, __func__);
  132. if (ret != QDF_STATUS_SUCCESS) {
  133. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  134. "%s: htc_send_pkt failed, status = %d\n", __func__,
  135. ret);
  136. epping_free_cookie(adapter->pEpping_ctx, cookie);
  137. return A_ERROR;
  138. }
  139. adapter->stats.tx_bytes += skb_len;
  140. ++adapter->stats.tx_packets;
  141. if (((adapter->stats.tx_packets +
  142. adapter->stats.tx_dropped) % EPPING_STATS_LOG_COUNT) == 0 &&
  143. (adapter->stats.tx_packets || adapter->stats.tx_dropped)) {
  144. epping_log_stats(adapter, __func__);
  145. }
  146. return 0;
  147. }
  148. void epping_tx_timer_expire(epping_adapter_t *adapter)
  149. {
  150. qdf_nbuf_t nodrop_skb;
  151. EPPING_LOG(QDF_TRACE_LEVEL_INFO, "%s: queue len: %d\n", __func__,
  152. qdf_nbuf_queue_len(&adapter->nodrop_queue));
  153. if (!qdf_nbuf_queue_len(&adapter->nodrop_queue)) {
  154. /* nodrop queue is empty so no need to arm timer */
  155. adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
  156. return;
  157. }
  158. /* try to flush nodrop queue */
  159. while ((nodrop_skb = qdf_nbuf_queue_remove(&adapter->nodrop_queue))) {
  160. htc_set_nodrop_pkt(adapter->pEpping_ctx->HTCHandle, true);
  161. if (epping_tx_send_int(nodrop_skb, adapter)) {
  162. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  163. "%s: nodrop: %pK xmit fail in timer\n",
  164. __func__, nodrop_skb);
  165. /* fail to xmit so put the nodrop packet to the nodrop queue */
  166. qdf_nbuf_queue_insert_head(&adapter->nodrop_queue,
  167. nodrop_skb);
  168. break;
  169. } else {
  170. htc_set_nodrop_pkt(adapter->pEpping_ctx->HTCHandle, false);
  171. EPPING_LOG(QDF_TRACE_LEVEL_INFO,
  172. "%s: nodrop: %pK xmit ok in timer\n",
  173. __func__, nodrop_skb);
  174. }
  175. }
  176. /* if nodrop queue is not empty, continue to arm timer */
  177. if (nodrop_skb) {
  178. qdf_spin_lock_bh(&adapter->data_lock);
  179. /* if nodrop queue is not empty, continue to arm timer */
  180. if (adapter->epping_timer_state != EPPING_TX_TIMER_RUNNING) {
  181. adapter->epping_timer_state = EPPING_TX_TIMER_RUNNING;
  182. qdf_timer_mod(&adapter->epping_timer,
  183. TX_RETRY_TIMEOUT_IN_MS);
  184. }
  185. qdf_spin_unlock_bh(&adapter->data_lock);
  186. } else {
  187. adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
  188. }
  189. }
  190. int epping_tx_send(qdf_nbuf_t skb, epping_adapter_t *adapter)
  191. {
  192. qdf_nbuf_t nodrop_skb;
  193. EPPING_HEADER *eppingHdr;
  194. uint8_t ac = 0;
  195. eppingHdr = (EPPING_HEADER *) qdf_nbuf_data(skb);
  196. if (!IS_EPPING_PACKET(eppingHdr)) {
  197. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  198. "%s: Recived non endpoint ping packets\n", __func__);
  199. /* no packet to send, cleanup */
  200. qdf_nbuf_free(skb);
  201. return -ENOMEM;
  202. }
  203. /* the stream ID is mapped to an access class */
  204. ac = eppingHdr->StreamNo_h;
  205. /* hard coded two ep ids */
  206. if (ac != 0 && ac != 1) {
  207. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  208. "%s: ac %d is not mapped to mboxping service\n",
  209. __func__, ac);
  210. qdf_nbuf_free(skb);
  211. return -ENOMEM;
  212. }
  213. /*
  214. * some EPPING packets cannot be dropped no matter what access class
  215. * it was sent on. A special care has been taken:
  216. * 1. when there is no TX resource, queue the control packets to
  217. * a special queue
  218. * 2. when there is TX resource, send the queued control packets first
  219. * and then other packets
  220. * 3. a timer launches to check if there is queued control packets and
  221. * flush them
  222. */
  223. /* check the nodrop queue first */
  224. while ((nodrop_skb = qdf_nbuf_queue_remove(&adapter->nodrop_queue))) {
  225. htc_set_nodrop_pkt(adapter->pEpping_ctx->HTCHandle, true);
  226. if (epping_tx_send_int(nodrop_skb, adapter)) {
  227. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  228. "%s: nodrop: %pK xmit fail\n", __func__,
  229. nodrop_skb);
  230. /* fail to xmit so put the nodrop packet to the nodrop queue */
  231. qdf_nbuf_queue_insert_head(&adapter->nodrop_queue,
  232. nodrop_skb);
  233. /* no cookie so free the current skb */
  234. goto tx_fail;
  235. } else {
  236. htc_set_nodrop_pkt(adapter->pEpping_ctx->HTCHandle, false);
  237. EPPING_LOG(QDF_TRACE_LEVEL_INFO,
  238. "%s: nodrop: %pK xmit ok\n", __func__,
  239. nodrop_skb);
  240. }
  241. }
  242. /* send the original packet */
  243. if (epping_tx_send_int(skb, adapter))
  244. goto tx_fail;
  245. return 0;
  246. tx_fail:
  247. if (!IS_EPING_PACKET_NO_DROP(eppingHdr)) {
  248. /* allow to drop the skb so drop it */
  249. qdf_nbuf_free(skb);
  250. ++adapter->stats.tx_dropped;
  251. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  252. "%s: Tx skb %pK dropped, stats.tx_dropped = %ld\n",
  253. __func__, skb, adapter->stats.tx_dropped);
  254. return -ENOMEM;
  255. } else {
  256. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  257. "%s: nodrop: %pK queued\n", __func__, skb);
  258. qdf_nbuf_queue_add(&adapter->nodrop_queue, skb);
  259. qdf_spin_lock_bh(&adapter->data_lock);
  260. if (adapter->epping_timer_state != EPPING_TX_TIMER_RUNNING) {
  261. adapter->epping_timer_state = EPPING_TX_TIMER_RUNNING;
  262. qdf_timer_mod(&adapter->epping_timer,
  263. TX_RETRY_TIMEOUT_IN_MS);
  264. }
  265. qdf_spin_unlock_bh(&adapter->data_lock);
  266. }
  267. return 0;
  268. }
  269. #ifdef HIF_SDIO
  270. enum htc_send_full_action epping_tx_queue_full(void *Context,
  271. HTC_PACKET *pPacket)
  272. {
  273. /*
  274. * Call netif_stop_queue frequently will impact the mboxping tx t-put.
  275. * Return HTC_SEND_FULL_KEEP directly in epping_tx_queue_full to avoid.
  276. */
  277. return HTC_SEND_FULL_KEEP;
  278. }
  279. #endif /* HIF_SDIO */
  280. void epping_tx_complete(void *ctx, HTC_PACKET *htc_pkt)
  281. {
  282. epping_context_t *pEpping_ctx = (epping_context_t *) ctx;
  283. epping_adapter_t *adapter = pEpping_ctx->epping_adapter;
  284. struct net_device *dev = adapter->dev;
  285. QDF_STATUS status;
  286. HTC_ENDPOINT_ID eid;
  287. qdf_nbuf_t pktSkb;
  288. struct epping_cookie *cookie;
  289. A_BOOL flushing = false;
  290. qdf_nbuf_queue_t skb_queue;
  291. if (!htc_pkt)
  292. return;
  293. qdf_nbuf_queue_init(&skb_queue);
  294. qdf_spin_lock_bh(&adapter->data_lock);
  295. status = htc_pkt->Status;
  296. eid = htc_pkt->Endpoint;
  297. pktSkb = GET_HTC_PACKET_NET_BUF_CONTEXT(htc_pkt);
  298. cookie = htc_pkt->pPktContext;
  299. if (!pktSkb) {
  300. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  301. "%s: NULL skb from hc packet", __func__);
  302. QDF_BUG(0);
  303. } else {
  304. if (htc_pkt->pBuffer != qdf_nbuf_data(pktSkb)) {
  305. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  306. "%s: htc_pkt buffer not equal to skb->data",
  307. __func__);
  308. QDF_BUG(0);
  309. }
  310. /* add this to the list, use faster non-lock API */
  311. qdf_nbuf_queue_add(&skb_queue, pktSkb);
  312. if (QDF_IS_STATUS_SUCCESS(status)) {
  313. if (htc_pkt->ActualLength !=
  314. qdf_nbuf_len(pktSkb)) {
  315. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  316. "%s: htc_pkt length not equal to skb->len",
  317. __func__);
  318. QDF_BUG(0);
  319. }
  320. }
  321. }
  322. EPPING_LOG(QDF_TRACE_LEVEL_INFO,
  323. "%s skb=%pK data=%pK len=0x%x eid=%d ",
  324. __func__, pktSkb, htc_pkt->pBuffer,
  325. htc_pkt->ActualLength, eid);
  326. if (QDF_IS_STATUS_ERROR(status)) {
  327. if (status == QDF_STATUS_E_CANCELED) {
  328. /* a packet was flushed */
  329. flushing = true;
  330. }
  331. if (status != QDF_STATUS_E_RESOURCES) {
  332. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  333. "%s() -TX ERROR, status: 0x%x",
  334. __func__, status);
  335. }
  336. } else {
  337. EPPING_LOG(QDF_TRACE_LEVEL_INFO, "%s: OK\n", __func__);
  338. flushing = false;
  339. }
  340. epping_free_cookie(adapter->pEpping_ctx, cookie);
  341. qdf_spin_unlock_bh(&adapter->data_lock);
  342. /* free all skbs in our local list */
  343. while (qdf_nbuf_queue_len(&skb_queue)) {
  344. /* use non-lock version */
  345. pktSkb = qdf_nbuf_queue_remove(&skb_queue);
  346. if (!pktSkb)
  347. break;
  348. qdf_nbuf_tx_free(pktSkb, QDF_NBUF_PKT_ERROR);
  349. pEpping_ctx->total_tx_acks++;
  350. }
  351. if (!flushing) {
  352. netif_wake_queue(dev);
  353. }
  354. }