epping_txrx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2014-2020 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_txrx.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 <pld_common.h>
  34. #include <linux/rtnetlink.h>
  35. #include <linux/semaphore.h>
  36. #include <linux/ctype.h>
  37. #include "epping_main.h"
  38. #include "epping_internal.h"
  39. static int epping_start_adapter(epping_adapter_t *adapter);
  40. static void epping_stop_adapter(epping_adapter_t *adapter);
  41. static void epping_timer_expire(void *data)
  42. {
  43. struct net_device *dev = (struct net_device *)data;
  44. epping_adapter_t *adapter;
  45. if (!dev) {
  46. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  47. "%s: netdev = NULL", __func__);
  48. return;
  49. }
  50. adapter = netdev_priv(dev);
  51. if (!adapter) {
  52. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  53. "%s: adapter = NULL", __func__);
  54. return;
  55. }
  56. adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
  57. epping_tx_timer_expire(adapter);
  58. }
  59. static int epping_ndev_open(struct net_device *dev)
  60. {
  61. epping_adapter_t *adapter;
  62. int ret = 0;
  63. adapter = netdev_priv(dev);
  64. epping_start_adapter(adapter);
  65. return ret;
  66. }
  67. static int epping_ndev_stop(struct net_device *dev)
  68. {
  69. epping_adapter_t *adapter;
  70. int ret = 0;
  71. adapter = netdev_priv(dev);
  72. if (!adapter) {
  73. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  74. "%s: EPPING adapter context is Null", __func__);
  75. ret = -ENODEV;
  76. goto end;
  77. }
  78. epping_stop_adapter(adapter);
  79. end:
  80. return ret;
  81. }
  82. static void epping_ndev_uninit(struct net_device *dev)
  83. {
  84. epping_adapter_t *adapter;
  85. adapter = netdev_priv(dev);
  86. if (!adapter) {
  87. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  88. "%s: EPPING adapter context is Null", __func__);
  89. goto end;
  90. }
  91. epping_stop_adapter(adapter);
  92. end:
  93. return;
  94. }
  95. static void epping_tx_queue_timeout(struct net_device *dev)
  96. {
  97. epping_adapter_t *adapter;
  98. adapter = netdev_priv(dev);
  99. if (!adapter) {
  100. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  101. "%s: EPPING adapter context is Null", __func__);
  102. goto end;
  103. }
  104. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  105. "%s: Transmission timeout occurred, adapter->started= %d",
  106. __func__, adapter->started);
  107. /* Getting here implies we disabled the TX queues
  108. * for too long. Since this is epping
  109. * (not because of disassociation or low resource scenarios),
  110. * try to restart the queue
  111. */
  112. if (adapter->started)
  113. netif_wake_queue(dev);
  114. end:
  115. return;
  116. }
  117. static netdev_tx_t epping_hard_start_xmit(struct sk_buff *skb,
  118. struct net_device *dev)
  119. {
  120. epping_adapter_t *adapter;
  121. int ret = 0;
  122. adapter = netdev_priv(dev);
  123. if (!adapter) {
  124. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  125. "%s: EPPING adapter context is Null", __func__);
  126. kfree_skb(skb);
  127. ret = -ENODEV;
  128. goto end;
  129. }
  130. qdf_net_buf_debug_acquire_skb(skb, __FILE__, __LINE__);
  131. ret = epping_tx_send(skb, adapter);
  132. end:
  133. return NETDEV_TX_OK;
  134. }
  135. static struct net_device_stats *epping_get_stats(struct net_device *dev)
  136. {
  137. epping_adapter_t *adapter = netdev_priv(dev);
  138. if (!adapter) {
  139. EPPING_LOG(QDF_TRACE_LEVEL_ERROR, "%s: adapter = NULL",
  140. __func__);
  141. return NULL;
  142. }
  143. return &adapter->stats;
  144. }
  145. static int epping_ndev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  146. {
  147. epping_adapter_t *adapter;
  148. int ret = 0;
  149. adapter = netdev_priv(dev);
  150. if (!adapter) {
  151. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  152. "%s: EPPING adapter context is Null", __func__);
  153. ret = -ENODEV;
  154. goto end;
  155. }
  156. if (dev != adapter->dev) {
  157. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  158. "%s: HDD adapter/dev inconsistency", __func__);
  159. ret = -ENODEV;
  160. goto end;
  161. }
  162. if ((!ifr) || (!ifr->ifr_data)) {
  163. ret = -EINVAL;
  164. goto end;
  165. }
  166. switch (cmd) {
  167. case (SIOCDEVPRIVATE + 1):
  168. EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
  169. "%s: do not support ioctl %d (SIOCDEVPRIVATE + 1)",
  170. __func__, cmd);
  171. break;
  172. default:
  173. EPPING_LOG(QDF_TRACE_LEVEL_ERROR, "%s: unknown ioctl %d",
  174. __func__, cmd);
  175. ret = -EINVAL;
  176. break;
  177. }
  178. end:
  179. return ret;
  180. }
  181. static int epping_set_mac_address(struct net_device *dev, void *addr)
  182. {
  183. epping_adapter_t *adapter = netdev_priv(dev);
  184. struct sockaddr *psta_mac_addr = addr;
  185. qdf_mem_copy(&adapter->macAddressCurrent,
  186. psta_mac_addr->sa_data, ETH_ALEN);
  187. qdf_mem_copy(dev->dev_addr, psta_mac_addr->sa_data, ETH_ALEN);
  188. return 0;
  189. }
  190. static void epping_stop_adapter(epping_adapter_t *adapter)
  191. {
  192. qdf_device_t qdf_ctx = cds_get_context(QDF_MODULE_ID_QDF_DEVICE);
  193. if (!qdf_ctx) {
  194. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  195. "%s: qdf_ctx is NULL\n", __func__);
  196. return;
  197. }
  198. if (adapter && adapter->started) {
  199. EPPING_LOG(LOG1, FL("Disabling queues"));
  200. netif_tx_disable(adapter->dev);
  201. netif_carrier_off(adapter->dev);
  202. adapter->started = false;
  203. pld_request_bus_bandwidth(qdf_ctx->dev,
  204. PLD_BUS_WIDTH_LOW);
  205. }
  206. }
  207. static int epping_start_adapter(epping_adapter_t *adapter)
  208. {
  209. qdf_device_t qdf_ctx = cds_get_context(QDF_MODULE_ID_QDF_DEVICE);
  210. if (!qdf_ctx) {
  211. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  212. "%s: qdf_ctx is NULL", __func__);
  213. return -EINVAL;
  214. }
  215. if (!adapter) {
  216. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  217. "%s: adapter= NULL\n", __func__);
  218. return -EINVAL;
  219. }
  220. if (!adapter->started) {
  221. pld_request_bus_bandwidth(qdf_ctx->dev,
  222. PLD_BUS_WIDTH_HIGH);
  223. netif_carrier_on(adapter->dev);
  224. EPPING_LOG(LOG1, FL("Enabling queues"));
  225. netif_tx_start_all_queues(adapter->dev);
  226. adapter->started = true;
  227. } else {
  228. EPPING_LOG(QDF_TRACE_LEVEL_WARN,
  229. "%s: adapter %pK already started\n", __func__,
  230. adapter);
  231. }
  232. return 0;
  233. }
  234. static int epping_register_adapter(epping_adapter_t *adapter, bool rtnl_held)
  235. {
  236. int ret = 0;
  237. if (!rtnl_held)
  238. ret = register_netdev(adapter->dev);
  239. else
  240. ret = register_netdevice(adapter->dev);
  241. if (ret != 0) {
  242. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  243. "%s: unable to register device\n",
  244. adapter->dev->name);
  245. } else {
  246. adapter->registered = true;
  247. }
  248. return ret;
  249. }
  250. static void epping_unregister_adapter(epping_adapter_t *adapter)
  251. {
  252. if (adapter) {
  253. epping_stop_adapter(adapter);
  254. if (adapter->registered) {
  255. unregister_netdev(adapter->dev);
  256. adapter->registered = false;
  257. }
  258. } else {
  259. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  260. "%s: adapter = NULL, unable to unregister device\n",
  261. __func__);
  262. }
  263. }
  264. void epping_destroy_adapter(epping_adapter_t *adapter)
  265. {
  266. struct net_device *dev = NULL;
  267. epping_context_t *pEpping_ctx;
  268. if (!adapter || !adapter->pEpping_ctx) {
  269. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  270. "%s: adapter = NULL\n", __func__);
  271. return;
  272. }
  273. dev = adapter->dev;
  274. pEpping_ctx = adapter->pEpping_ctx;
  275. epping_unregister_adapter(adapter);
  276. qdf_spinlock_destroy(&adapter->data_lock);
  277. qdf_timer_free(&adapter->epping_timer);
  278. adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
  279. while (qdf_nbuf_queue_len(&adapter->nodrop_queue)) {
  280. qdf_nbuf_t tmp_nbuf = NULL;
  281. tmp_nbuf = qdf_nbuf_queue_remove(&adapter->nodrop_queue);
  282. if (tmp_nbuf)
  283. qdf_nbuf_free(tmp_nbuf);
  284. }
  285. free_netdev(dev);
  286. if (!pEpping_ctx)
  287. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  288. "%s: pEpping_ctx = NULL\n", __func__);
  289. else
  290. pEpping_ctx->epping_adapter = NULL;
  291. }
  292. static struct net_device_ops epping_drv_ops = {
  293. .ndo_open = epping_ndev_open,
  294. .ndo_stop = epping_ndev_stop,
  295. .ndo_uninit = epping_ndev_uninit,
  296. .ndo_start_xmit = epping_hard_start_xmit,
  297. .ndo_tx_timeout = epping_tx_queue_timeout,
  298. .ndo_get_stats = epping_get_stats,
  299. .ndo_do_ioctl = epping_ndev_ioctl,
  300. .ndo_set_mac_address = epping_set_mac_address,
  301. .ndo_select_queue = NULL,
  302. };
  303. #define EPPING_TX_QUEUE_MAX_LEN 128 /* need to be power of 2 */
  304. epping_adapter_t *epping_add_adapter(epping_context_t *pEpping_ctx,
  305. tSirMacAddr macAddr,
  306. enum QDF_OPMODE device_mode,
  307. bool rtnl_held)
  308. {
  309. struct net_device *dev;
  310. epping_adapter_t *adapter;
  311. dev = alloc_netdev(sizeof(epping_adapter_t), "wifi%d",
  312. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0))
  313. NET_NAME_UNKNOWN,
  314. #endif
  315. ether_setup);
  316. if (!dev) {
  317. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  318. "%s: Cannot allocate epping_adapter_t\n", __func__);
  319. return NULL;
  320. }
  321. adapter = netdev_priv(dev);
  322. qdf_mem_zero(adapter, sizeof(*adapter));
  323. adapter->dev = dev;
  324. adapter->pEpping_ctx = pEpping_ctx;
  325. adapter->device_mode = device_mode; /* station, SAP, etc */
  326. qdf_mem_copy(dev->dev_addr, (void *)macAddr, sizeof(tSirMacAddr));
  327. qdf_mem_copy(adapter->macAddressCurrent.bytes,
  328. macAddr, sizeof(tSirMacAddr));
  329. qdf_spinlock_create(&adapter->data_lock);
  330. qdf_nbuf_queue_init(&adapter->nodrop_queue);
  331. adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
  332. qdf_timer_init(epping_get_qdf_ctx(), &adapter->epping_timer,
  333. epping_timer_expire, dev, QDF_TIMER_TYPE_SW);
  334. dev->type = ARPHRD_IEEE80211;
  335. dev->needed_headroom += 24;
  336. dev->netdev_ops = &epping_drv_ops;
  337. dev->watchdog_timeo = 5 * HZ; /* XXX */
  338. dev->tx_queue_len = EPPING_TXBUF - 1; /* 1 for mgmt frame */
  339. if (epping_register_adapter(adapter, rtnl_held) == 0) {
  340. EPPING_LOG(LOG1, FL("Disabling queues"));
  341. netif_tx_disable(dev);
  342. netif_carrier_off(dev);
  343. return adapter;
  344. } else {
  345. epping_destroy_adapter(adapter);
  346. return NULL;
  347. }
  348. }
  349. int epping_connect_service(epping_context_t *pEpping_ctx)
  350. {
  351. int status, i;
  352. struct htc_service_connect_req connect;
  353. struct htc_service_connect_resp response;
  354. qdf_mem_zero(&connect, sizeof(connect));
  355. qdf_mem_zero(&response, sizeof(response));
  356. /* these fields are the same for all service endpoints */
  357. connect.EpCallbacks.pContext = pEpping_ctx;
  358. connect.EpCallbacks.EpTxCompleteMultiple = NULL;
  359. connect.EpCallbacks.EpRecv = epping_rx;
  360. /* epping_tx_complete use Multiple version */
  361. connect.EpCallbacks.EpTxComplete = epping_tx_complete;
  362. connect.MaxSendQueueDepth = 64;
  363. #ifdef HIF_SDIO
  364. connect.EpCallbacks.EpRecvRefill = epping_refill;
  365. connect.EpCallbacks.EpSendFull =
  366. epping_tx_queue_full /* ar6000_tx_queue_full */;
  367. #elif defined(HIF_USB) || defined(HIF_PCI) || defined(HIF_SNOC) || \
  368. defined(HIF_IPCI)
  369. connect.EpCallbacks.EpRecvRefill = NULL /* provided by HIF */;
  370. connect.EpCallbacks.EpSendFull = NULL /* provided by HIF */;
  371. /* disable flow control for hw flow control */
  372. connect.ConnectionFlags |= HTC_CONNECT_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
  373. #endif
  374. /* connect to service */
  375. connect.service_id = WMI_DATA_BE_SVC;
  376. status = htc_connect_service(pEpping_ctx->HTCHandle, &connect, &response);
  377. if (QDF_IS_STATUS_ERROR(status)) {
  378. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  379. "Failed to connect to Endpoint Ping BE service status:%d\n",
  380. status);
  381. return status;
  382. } else {
  383. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  384. "eppingtest BE endpoint:%d\n", response.Endpoint);
  385. }
  386. pEpping_ctx->EppingEndpoint[0] = response.Endpoint;
  387. #if defined(HIF_PCI) || defined(HIF_USB) || defined(HIF_SNOC) || \
  388. defined(HIF_IPCI)
  389. connect.service_id = WMI_DATA_BK_SVC;
  390. status = htc_connect_service(pEpping_ctx->HTCHandle, &connect, &response);
  391. if (QDF_IS_STATUS_ERROR(status)) {
  392. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  393. "Failed to connect to Endpoint Ping BK service status:%d\n",
  394. status);
  395. return status;
  396. } else {
  397. EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
  398. "eppingtest BK endpoint:%d\n", response.Endpoint);
  399. }
  400. pEpping_ctx->EppingEndpoint[1] = response.Endpoint;
  401. /* Since we do not create other two SVC use BK endpoint
  402. * for rest ACs (2, 3) */
  403. for (i = 2; i < EPPING_MAX_NUM_EPIDS; i++) {
  404. pEpping_ctx->EppingEndpoint[i] = response.Endpoint;
  405. }
  406. #else
  407. /* we only use one endpoint for high latenance bus.
  408. * Map all AC's EPIDs to the same endpoint ID returned by HTC */
  409. for (i = 0; i < EPPING_MAX_NUM_EPIDS; i++) {
  410. pEpping_ctx->EppingEndpoint[i] = response.Endpoint;
  411. }
  412. #endif
  413. return 0;
  414. }