xref: /wlan-dirver/qca-wifi-host-cmn/utils/epping/src/epping_helper.c (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
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_main.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 <linux/delay.h>
35 #include <wni_api.h>
36 #include <wlan_ptt_sock_svc.h>
37 #include <linux/wireless.h>
38 #include <net/cfg80211.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/semaphore.h>
41 #include <linux/delay.h>
42 #include <linux/ctype.h>
43 #include "epping_main.h"
44 #include "epping_internal.h"
45 
46 int epping_cookie_init(epping_context_t *pEpping_ctx)
47 {
48 	uint32_t i, j;
49 
50 	pEpping_ctx->cookie_list = NULL;
51 	pEpping_ctx->cookie_count = 0;
52 	for (i = 0; i < MAX_COOKIE_SLOTS_NUM; i++) {
53 		pEpping_ctx->s_cookie_mem[i] =
54 			qdf_mem_malloc(sizeof(struct epping_cookie) *
55 				       MAX_COOKIE_SLOT_SIZE);
56 		if (!pEpping_ctx->s_cookie_mem[i])
57 			goto error;
58 	}
59 	qdf_spinlock_create(&pEpping_ctx->cookie_lock);
60 
61 	for (i = 0; i < MAX_COOKIE_SLOTS_NUM; i++) {
62 		struct epping_cookie *cookie_mem = pEpping_ctx->s_cookie_mem[i];
63 		for (j = 0; j < MAX_COOKIE_SLOT_SIZE; j++) {
64 			epping_free_cookie(pEpping_ctx, &cookie_mem[j]);
65 		}
66 	}
67 	return 0;
68 error:
69 	for (i = 0; i < MAX_COOKIE_SLOTS_NUM; i++) {
70 		if (pEpping_ctx->s_cookie_mem[i]) {
71 			qdf_mem_free(pEpping_ctx->s_cookie_mem[i]);
72 			pEpping_ctx->s_cookie_mem[i] = NULL;
73 		}
74 	}
75 	return -ENOMEM;
76 }
77 
78 /* cleanup cookie queue */
79 void epping_cookie_cleanup(epping_context_t *pEpping_ctx)
80 {
81 	int i;
82 	qdf_spin_lock_bh(&pEpping_ctx->cookie_lock);
83 	pEpping_ctx->cookie_list = NULL;
84 	pEpping_ctx->cookie_count = 0;
85 	qdf_spin_unlock_bh(&pEpping_ctx->cookie_lock);
86 	for (i = 0; i < MAX_COOKIE_SLOTS_NUM; i++) {
87 		if (pEpping_ctx->s_cookie_mem[i]) {
88 			qdf_mem_free(pEpping_ctx->s_cookie_mem[i]);
89 			pEpping_ctx->s_cookie_mem[i] = NULL;
90 		}
91 	}
92 }
93 
94 void epping_free_cookie(epping_context_t *pEpping_ctx,
95 			struct epping_cookie *cookie)
96 {
97 	qdf_spin_lock_bh(&pEpping_ctx->cookie_lock);
98 	cookie->next = pEpping_ctx->cookie_list;
99 	pEpping_ctx->cookie_list = cookie;
100 	pEpping_ctx->cookie_count++;
101 	qdf_spin_unlock_bh(&pEpping_ctx->cookie_lock);
102 }
103 
104 struct epping_cookie *epping_alloc_cookie(epping_context_t *pEpping_ctx)
105 {
106 	struct epping_cookie *cookie;
107 
108 	qdf_spin_lock_bh(&pEpping_ctx->cookie_lock);
109 	cookie = pEpping_ctx->cookie_list;
110 	if (cookie != NULL) {
111 		pEpping_ctx->cookie_list = cookie->next;
112 		pEpping_ctx->cookie_count--;
113 	}
114 	qdf_spin_unlock_bh(&pEpping_ctx->cookie_lock);
115 	return cookie;
116 }
117 
118 void epping_get_dummy_mac_addr(tSirMacAddr macAddr)
119 {
120 	macAddr[0] = 69;        /* E */
121 	macAddr[1] = 80;        /* P */
122 	macAddr[2] = 80;        /* P */
123 	macAddr[3] = 73;        /* I */
124 	macAddr[4] = 78;        /* N */
125 	macAddr[5] = 71;        /* G */
126 }
127 
128 void epping_hex_dump(void *data, int buf_len, const char *str)
129 {
130 	char *buf = (char *)data;
131 	int i;
132 
133 	printk("%s: E, %s\n", __func__, str);
134 	for (i = 0; (i + 7) < buf_len; i += 8) {
135 		printk("%02x %02x %02x %02x %02x %02x %02x %02x\n",
136 		       buf[i],
137 		       buf[i + 1],
138 		       buf[i + 2],
139 		       buf[i + 3],
140 		       buf[i + 4], buf[i + 5], buf[i + 6], buf[i + 7]);
141 	}
142 
143 	/* Dump the bytes in the last line */
144 	for (; i < buf_len; i++) {
145 		printk("%02x ", buf[i]);
146 	}
147 	printk("\n%s: X %s\n", __func__, str);
148 }
149 
150 void *epping_get_qdf_ctx(void)
151 {
152 	qdf_device_t *qdf_ctx;
153 
154 	qdf_ctx = cds_get_context(QDF_MODULE_ID_QDF_DEVICE);
155 	return qdf_ctx;
156 }
157 
158 void epping_log_packet(epping_adapter_t *adapter,
159 		       EPPING_HEADER *eppingHdr, int ret, const char *str)
160 {
161 	if (eppingHdr->Cmd_h & EPPING_LOG_MASK) {
162 		EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
163 			   "%s: cmd = %d, seqNo = %u, flag = 0x%x, ret = %d, "
164 			   "txCount = %lu, txDrop =  %lu, txBytes = %lu,"
165 			   "rxCount = %lu, rxDrop = %lu, rxBytes = %lu\n",
166 			   str, eppingHdr->Cmd_h, eppingHdr->SeqNo,
167 			   eppingHdr->CmdFlags_h, ret,
168 			   adapter->stats.tx_packets,
169 			   adapter->stats.tx_dropped,
170 			   adapter->stats.tx_bytes,
171 			   adapter->stats.rx_packets,
172 			   adapter->stats.rx_dropped,
173 			   adapter->stats.rx_bytes);
174 	}
175 }
176 
177 void epping_log_stats(epping_adapter_t *adapter, const char *str)
178 {
179 	EPPING_LOG(QDF_TRACE_LEVEL_FATAL,
180 		   "%s: txCount = %lu, txDrop = %lu, tx_bytes = %lu, "
181 		   "rxCount = %lu, rxDrop = %lu, rx_bytes = %lu, tx_acks = %u\n",
182 		   str,
183 		   adapter->stats.tx_packets,
184 		   adapter->stats.tx_dropped,
185 		   adapter->stats.tx_bytes,
186 		   adapter->stats.rx_packets,
187 		   adapter->stats.rx_dropped,
188 		   adapter->stats.rx_bytes,
189 		   adapter->pEpping_ctx->total_tx_acks);
190 }
191 
192 void epping_set_kperf_flag(epping_adapter_t *adapter,
193 			   HTC_ENDPOINT_ID eid, uint8_t kperf_flag)
194 {
195 	adapter->pEpping_ctx->kperf_num_rx_recv[eid] = 0;
196 	adapter->pEpping_ctx->kperf_num_tx_acks[eid] = 0;
197 }
198