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