xref: /wlan-dirver/qca-wifi-host-cmn/utils/epping/src/epping_txrx.c (revision 6ecd284e5a94a1c96e26d571dd47419ac305990d)
1 /*
2  * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
3  *
4  * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5  *
6  *
7  * Permission to use, copy, modify, and/or distribute this software for
8  * any purpose with or without fee is hereby granted, provided that the
9  * above copyright notice and this permission notice appear in all
10  * copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19  * PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 /*
23  * This file was originally distributed by Qualcomm Atheros, Inc.
24  * under proprietary terms before Copyright ownership was assigned
25  * to the Linux Foundation.
26  */
27 
28 /*========================================================================
29 
30    \file  epping_txrx.c
31 
32    \brief WLAN End Point Ping test tool implementation
33 
34    ========================================================================*/
35 
36 /*--------------------------------------------------------------------------
37    Include Files
38    ------------------------------------------------------------------------*/
39 #include <cds_api.h>
40 #include <cds_sched.h>
41 #include <linux/etherdevice.h>
42 #include <linux/firmware.h>
43 #include <wni_api.h>
44 #include <wlan_ptt_sock_svc.h>
45 #include <linux/wireless.h>
46 #include <net/cfg80211.h>
47 #include <pld_common.h>
48 #include <linux/rtnetlink.h>
49 #include <linux/semaphore.h>
50 #include <linux/ctype.h>
51 #include "epping_main.h"
52 #include "epping_internal.h"
53 
54 static int epping_start_adapter(epping_adapter_t *adapter);
55 static void epping_stop_adapter(epping_adapter_t *adapter);
56 
57 static void epping_timer_expire(void *data)
58 {
59 	struct net_device *dev = (struct net_device *)data;
60 	epping_adapter_t *adapter;
61 
62 	if (dev == NULL) {
63 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
64 			   "%s: netdev = NULL", __func__);
65 		return;
66 	}
67 
68 	adapter = netdev_priv(dev);
69 	if (adapter == NULL) {
70 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
71 			   "%s: adapter = NULL", __func__);
72 		return;
73 	}
74 	adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
75 	epping_tx_timer_expire(adapter);
76 }
77 
78 static int epping_ndev_open(struct net_device *dev)
79 {
80 	epping_adapter_t *adapter;
81 	int ret = 0;
82 
83 	adapter = netdev_priv(dev);
84 	epping_start_adapter(adapter);
85 	return ret;
86 }
87 
88 static int epping_ndev_stop(struct net_device *dev)
89 {
90 	epping_adapter_t *adapter;
91 	int ret = 0;
92 
93 	adapter = netdev_priv(dev);
94 	if (NULL == adapter) {
95 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
96 			   "%s: EPPING adapter context is Null", __func__);
97 		ret = -ENODEV;
98 		goto end;
99 	}
100 	epping_stop_adapter(adapter);
101 end:
102 	return ret;
103 }
104 
105 static void epping_ndev_uninit(struct net_device *dev)
106 {
107 	epping_adapter_t *adapter;
108 
109 	adapter = netdev_priv(dev);
110 	if (NULL == adapter) {
111 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
112 			   "%s: EPPING adapter context is Null", __func__);
113 		goto end;
114 	}
115 	epping_stop_adapter(adapter);
116 end:
117 	return;
118 }
119 
120 static void epping_tx_queue_timeout(struct net_device *dev)
121 {
122 	epping_adapter_t *adapter;
123 
124 	adapter = netdev_priv(dev);
125 	if (NULL == adapter) {
126 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
127 			   "%s: EPPING adapter context is Null", __func__);
128 		goto end;
129 	}
130 
131 	EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
132 		   "%s: Transmission timeout occurred, adapter->started= %d",
133 		   __func__, adapter->started);
134 
135 	/* Getting here implies we disabled the TX queues
136 	 * for too long. Since this is epping
137 	 * (not because of disassociation or low resource scenarios),
138 	 * try to restart the queue
139 	 */
140 	if (adapter->started)
141 		netif_wake_queue(dev);
142 end:
143 	return;
144 
145 }
146 
147 static netdev_tx_t epping_hard_start_xmit(struct sk_buff *skb,
148 					  struct net_device *dev)
149 {
150 	epping_adapter_t *adapter;
151 	int ret = 0;
152 
153 	adapter = netdev_priv(dev);
154 	if (NULL == adapter) {
155 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
156 			   "%s: EPPING adapter context is Null", __func__);
157 		kfree_skb(skb);
158 		ret = -ENODEV;
159 		goto end;
160 	}
161 	ret = epping_tx_send(skb, adapter);
162 end:
163 	return NETDEV_TX_OK;
164 }
165 
166 static struct net_device_stats *epping_get_stats(struct net_device *dev)
167 {
168 	epping_adapter_t *adapter = netdev_priv(dev);
169 
170 	if (NULL == adapter) {
171 		EPPING_LOG(QDF_TRACE_LEVEL_ERROR, "%s: adapter = NULL",
172 			   __func__);
173 		return NULL;
174 	}
175 
176 	return &adapter->stats;
177 }
178 
179 static int epping_ndev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
180 {
181 	epping_adapter_t *adapter;
182 	int ret = 0;
183 
184 	adapter = netdev_priv(dev);
185 	if (NULL == adapter) {
186 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
187 			   "%s: EPPING adapter context is Null", __func__);
188 		ret = -ENODEV;
189 		goto end;
190 	}
191 	if (dev != adapter->dev) {
192 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
193 			   "%s: HDD adapter/dev inconsistency", __func__);
194 		ret = -ENODEV;
195 		goto end;
196 	}
197 
198 	if ((!ifr) || (!ifr->ifr_data)) {
199 		ret = -EINVAL;
200 		goto end;
201 	}
202 
203 	switch (cmd) {
204 	case (SIOCDEVPRIVATE + 1):
205 		EPPING_LOG(QDF_TRACE_LEVEL_ERROR,
206 			   "%s: do not support ioctl %d (SIOCDEVPRIVATE + 1)",
207 			   __func__, cmd);
208 		break;
209 	default:
210 		EPPING_LOG(QDF_TRACE_LEVEL_ERROR, "%s: unknown ioctl %d",
211 			   __func__, cmd);
212 		ret = -EINVAL;
213 		break;
214 	}
215 
216 end:
217 	return ret;
218 }
219 
220 static int epping_set_mac_address(struct net_device *dev, void *addr)
221 {
222 	epping_adapter_t *adapter = netdev_priv(dev);
223 	struct sockaddr *psta_mac_addr = addr;
224 	qdf_mem_copy(&adapter->macAddressCurrent,
225 		     psta_mac_addr->sa_data, ETH_ALEN);
226 	qdf_mem_copy(dev->dev_addr, psta_mac_addr->sa_data, ETH_ALEN);
227 	return 0;
228 }
229 
230 static void epping_stop_adapter(epping_adapter_t *adapter)
231 {
232 	qdf_device_t qdf_ctx = cds_get_context(QDF_MODULE_ID_QDF_DEVICE);
233 
234 	if (!qdf_ctx) {
235 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
236 			   "%s: qdf_ctx is NULL\n", __func__);
237 		return;
238 	}
239 
240 	if (adapter && adapter->started) {
241 		EPPING_LOG(LOG1, FL("Disabling queues"));
242 		netif_tx_disable(adapter->dev);
243 		netif_carrier_off(adapter->dev);
244 		adapter->started = false;
245 		pld_request_bus_bandwidth(qdf_ctx->dev,
246 					  PLD_BUS_WIDTH_LOW);
247 	}
248 }
249 
250 static int epping_start_adapter(epping_adapter_t *adapter)
251 {
252 	qdf_device_t qdf_ctx = cds_get_context(QDF_MODULE_ID_QDF_DEVICE);
253 
254 	if (!qdf_ctx) {
255 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
256 			   "%s: qdf_ctx is NULL", __func__);
257 		return -EINVAL;
258 	}
259 
260 	if (!adapter) {
261 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
262 			   "%s: adapter= NULL\n", __func__);
263 		return -EINVAL;
264 	}
265 	if (!adapter->started) {
266 		pld_request_bus_bandwidth(qdf_ctx->dev,
267 					  PLD_BUS_WIDTH_HIGH);
268 		netif_carrier_on(adapter->dev);
269 		EPPING_LOG(LOG1, FL("Enabling queues"));
270 		netif_tx_start_all_queues(adapter->dev);
271 		adapter->started = true;
272 	} else {
273 		EPPING_LOG(QDF_TRACE_LEVEL_WARN,
274 			   "%s: adapter %pK already started\n", __func__,
275 			   adapter);
276 	}
277 	return 0;
278 }
279 
280 static int epping_register_adapter(epping_adapter_t *adapter)
281 {
282 	int ret = 0;
283 
284 	ret = register_netdev(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 {
362 	struct net_device *dev;
363 	epping_adapter_t *adapter;
364 
365 	dev = alloc_netdev(sizeof(epping_adapter_t), "wifi%d",
366 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0))
367 			   NET_NAME_UNKNOWN,
368 #endif
369 			   ether_setup);
370 	if (dev == NULL) {
371 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
372 			   "%s: Cannot allocate epping_adapter_t\n", __func__);
373 		return NULL;
374 	}
375 
376 	adapter = netdev_priv(dev);
377 	qdf_mem_zero(adapter, sizeof(*adapter));
378 	adapter->dev = dev;
379 	adapter->pEpping_ctx = pEpping_ctx;
380 	adapter->device_mode = device_mode;    /* station, SAP, etc */
381 	qdf_mem_copy(dev->dev_addr, (void *)macAddr, sizeof(tSirMacAddr));
382 	qdf_mem_copy(adapter->macAddressCurrent.bytes,
383 		     macAddr, sizeof(tSirMacAddr));
384 	qdf_spinlock_create(&adapter->data_lock);
385 	qdf_nbuf_queue_init(&adapter->nodrop_queue);
386 	adapter->epping_timer_state = EPPING_TX_TIMER_STOPPED;
387 	qdf_timer_init(epping_get_qdf_ctx(), &adapter->epping_timer,
388 		epping_timer_expire, dev, QDF_TIMER_TYPE_SW);
389 	dev->type = ARPHRD_IEEE80211;
390 	dev->netdev_ops = &epping_drv_ops;
391 	dev->watchdog_timeo = 5 * HZ;   /* XXX */
392 	dev->tx_queue_len = EPPING_TXBUF - 1;      /* 1 for mgmt frame */
393 	if (epping_register_adapter(adapter) == 0) {
394 		EPPING_LOG(LOG1, FL("Disabling queues"));
395 		netif_tx_disable(dev);
396 		netif_carrier_off(dev);
397 		return adapter;
398 	} else {
399 		epping_destroy_adapter(adapter);
400 		return NULL;
401 	}
402 }
403 
404 int epping_connect_service(epping_context_t *pEpping_ctx)
405 {
406 	int status, i;
407 	struct htc_service_connect_req connect;
408 	struct htc_service_connect_resp response;
409 
410 	qdf_mem_zero(&connect, sizeof(connect));
411 	qdf_mem_zero(&response, sizeof(response));
412 
413 	/* these fields are the same for all service endpoints */
414 	connect.EpCallbacks.pContext = pEpping_ctx;
415 	connect.EpCallbacks.EpTxCompleteMultiple = NULL;
416 	connect.EpCallbacks.EpRecv = epping_rx;
417 	/* epping_tx_complete use Multiple version */
418 	connect.EpCallbacks.EpTxComplete  = epping_tx_complete;
419 	connect.MaxSendQueueDepth = 64;
420 
421 #ifdef HIF_SDIO
422 	connect.EpCallbacks.EpRecvRefill = epping_refill;
423 	connect.EpCallbacks.EpSendFull =
424 		epping_tx_queue_full /* ar6000_tx_queue_full */;
425 #elif defined(HIF_USB) || defined(HIF_PCI)
426 	connect.EpCallbacks.EpRecvRefill = NULL /* provided by HIF */;
427 	connect.EpCallbacks.EpSendFull = NULL /* provided by HIF */;
428 	/* disable flow control for hw flow control */
429 	connect.ConnectionFlags |= HTC_CONNECT_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
430 #endif
431 
432 	/* connect to service */
433 	connect.service_id = WMI_DATA_BE_SVC;
434 	status = htc_connect_service(pEpping_ctx->HTCHandle, &connect, &response);
435 	if (status != EOK) {
436 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
437 			   "Failed to connect to Endpoint Ping BE service status:%d\n",
438 			   status);
439 		return status;
440 	} else {
441 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
442 			   "eppingtest BE endpoint:%d\n", response.Endpoint);
443 	}
444 	pEpping_ctx->EppingEndpoint[0] = response.Endpoint;
445 
446 #if defined(HIF_PCI) || defined(HIF_USB)
447 	connect.service_id = WMI_DATA_BK_SVC;
448 	status = htc_connect_service(pEpping_ctx->HTCHandle, &connect, &response);
449 	if (status != EOK) {
450 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
451 			   "Failed to connect to Endpoint Ping BK service status:%d\n",
452 			   status);
453 		return status;
454 	} else {
455 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
456 			   "eppingtest BK endpoint:%d\n", response.Endpoint);
457 	}
458 	pEpping_ctx->EppingEndpoint[1] = response.Endpoint;
459 	/* Since we do not create other two SVC use BK endpoint
460 	 * for rest ACs (2, 3) */
461 	for (i = 2; i < EPPING_MAX_NUM_EPIDS; i++) {
462 		pEpping_ctx->EppingEndpoint[i] = response.Endpoint;
463 	}
464 #else
465 	/* we only use one endpoint for high latenance bus.
466 	 * Map all AC's EPIDs to the same endpoint ID returned by HTC */
467 	for (i = 0; i < EPPING_MAX_NUM_EPIDS; i++) {
468 		pEpping_ctx->EppingEndpoint[i] = response.Endpoint;
469 	}
470 #endif
471 	return 0;
472 }
473