epping_txrx.c 13 KB

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