epping_txrx.c 13 KB

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