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