xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/dhd_csi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Broadcom Dongle Host Driver (DHD)
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 1999-2018, Broadcom.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
7*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
8*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
9*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10*4882a593Smuzhiyun  * following added to such license:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
13*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
14*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
15*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
16*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
17*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
18*4882a593Smuzhiyun  * modifications of the software.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
21*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
22*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * $Id: dhd_csi.c 606280 2015-12-15 05:28:25Z $
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun #include <osl.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include <bcmutils.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <bcmendian.h>
31*4882a593Smuzhiyun #include <linuxver.h>
32*4882a593Smuzhiyun #include <linux/list.h>
33*4882a593Smuzhiyun #include <linux/sort.h>
34*4882a593Smuzhiyun #include <dngl_stats.h>
35*4882a593Smuzhiyun #include <wlioctl.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <bcmevent.h>
38*4882a593Smuzhiyun #include <dhd.h>
39*4882a593Smuzhiyun #include <dhd_dbg.h>
40*4882a593Smuzhiyun #include <dhd_csi.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define NULL_CHECK(p, s, err)  \
43*4882a593Smuzhiyun 	do { \
44*4882a593Smuzhiyun 		if (!(p)) { \
45*4882a593Smuzhiyun 			printf("NULL POINTER (%s) : %s\n", __FUNCTION__, (s)); \
46*4882a593Smuzhiyun 			err = BCME_ERROR; \
47*4882a593Smuzhiyun 			return err; \
48*4882a593Smuzhiyun 		} \
49*4882a593Smuzhiyun 	} while (0)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define TIMESPEC_TO_US(ts)  (((uint64)(ts).tv_sec * USEC_PER_SEC) + \
52*4882a593Smuzhiyun 						(ts).tv_nsec / NSEC_PER_USEC)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define NULL_ADDR	"\x00\x00\x00\x00\x00\x00"
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun int
dhd_csi_event_handler(dhd_pub_t * dhd,wl_event_msg_t * event,void * event_data)57*4882a593Smuzhiyun dhd_csi_event_handler(dhd_pub_t *dhd, wl_event_msg_t *event, void *event_data)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	int ret = BCME_OK;
60*4882a593Smuzhiyun 	bool is_new = TRUE;
61*4882a593Smuzhiyun 	cfr_dump_data_t *p_event;
62*4882a593Smuzhiyun 	cfr_dump_list_t *ptr, *next, *new;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	NULL_CHECK(dhd, "dhd is NULL", ret);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	DHD_TRACE(("Enter %s\n", __FUNCTION__));
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (!event_data) {
69*4882a593Smuzhiyun 		DHD_ERROR(("%s: event_data is NULL\n", __FUNCTION__));
70*4882a593Smuzhiyun 		return -EINVAL;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 	p_event = (cfr_dump_data_t *)event_data;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/* check if this addr exist */
75*4882a593Smuzhiyun 	if (!list_empty(&dhd->csi_list)) {
76*4882a593Smuzhiyun 		list_for_each_entry_safe(ptr, next, &dhd->csi_list, list) {
77*4882a593Smuzhiyun 			if (bcmp(&ptr->entry.header.peer_macaddr, &p_event->header.peer_macaddr,
78*4882a593Smuzhiyun 					ETHER_ADDR_LEN) == 0) {
79*4882a593Smuzhiyun 				int pos = 0, dump_len = 0, remain = 0;
80*4882a593Smuzhiyun 				is_new = FALSE;
81*4882a593Smuzhiyun 				DHD_INFO(("CSI data exist\n"));
82*4882a593Smuzhiyun 				if (p_event->header.status == 0) {
83*4882a593Smuzhiyun 					bcopy(&p_event->header, &ptr->entry.header, sizeof(cfr_dump_header_t));
84*4882a593Smuzhiyun 					dump_len = p_event->header.cfr_dump_length;
85*4882a593Smuzhiyun 					if (dump_len < MAX_EVENT_SIZE) {
86*4882a593Smuzhiyun 						bcopy(&p_event->data, &ptr->entry.data, dump_len);
87*4882a593Smuzhiyun 					} else {
88*4882a593Smuzhiyun 						/* for big csi data */
89*4882a593Smuzhiyun 						uint8 *p = (uint8 *)&ptr->entry.data;
90*4882a593Smuzhiyun 						remain = p_event->header.remain_length;
91*4882a593Smuzhiyun 						if (remain) {
92*4882a593Smuzhiyun 							pos = dump_len - remain - MAX_EVENT_SIZE;
93*4882a593Smuzhiyun 							p += pos;
94*4882a593Smuzhiyun 							bcopy(&p_event->data, p, MAX_EVENT_SIZE);
95*4882a593Smuzhiyun 						}
96*4882a593Smuzhiyun 						/* copy rest of csi data */
97*4882a593Smuzhiyun 						else {
98*4882a593Smuzhiyun 							pos = dump_len - (dump_len % MAX_EVENT_SIZE);
99*4882a593Smuzhiyun 							p += pos;
100*4882a593Smuzhiyun 							bcopy(&p_event->data, p, (dump_len % MAX_EVENT_SIZE));
101*4882a593Smuzhiyun 						}
102*4882a593Smuzhiyun 					}
103*4882a593Smuzhiyun 					return BCME_OK;
104*4882a593Smuzhiyun 				}
105*4882a593Smuzhiyun 			}
106*4882a593Smuzhiyun 		}
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 	if (is_new) {
109*4882a593Smuzhiyun 		if (dhd->csi_count < MAX_CSI_NUM) {
110*4882a593Smuzhiyun 			new = (cfr_dump_list_t *)MALLOCZ(dhd->osh, sizeof(cfr_dump_list_t));
111*4882a593Smuzhiyun 			if (!new){
112*4882a593Smuzhiyun 				DHD_ERROR(("Malloc cfr dump list error\n"));
113*4882a593Smuzhiyun 				return BCME_NOMEM;
114*4882a593Smuzhiyun 			}
115*4882a593Smuzhiyun 			bcopy(&p_event->header, &new->entry.header, sizeof(cfr_dump_header_t));
116*4882a593Smuzhiyun 			DHD_INFO(("New entry data size %d\n", p_event->header.cfr_dump_length));
117*4882a593Smuzhiyun 			/* for big csi data */
118*4882a593Smuzhiyun 			if (p_event->header.remain_length) {
119*4882a593Smuzhiyun 				DHD_TRACE(("remain %d\n", p_event->header.remain_length));
120*4882a593Smuzhiyun 				bcopy(&p_event->data, &new->entry.data, MAX_EVENT_SIZE);
121*4882a593Smuzhiyun 			}
122*4882a593Smuzhiyun 			else
123*4882a593Smuzhiyun 				bcopy(&p_event->data, &new->entry.data, p_event->header.cfr_dump_length);
124*4882a593Smuzhiyun 			INIT_LIST_HEAD(&(new->list));
125*4882a593Smuzhiyun 			list_add_tail(&(new->list), &dhd->csi_list);
126*4882a593Smuzhiyun 			dhd->csi_count++;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 		else {
129*4882a593Smuzhiyun 			DHD_TRACE(("Over maximum CSI Number 8. SKIP it.\n"));
130*4882a593Smuzhiyun 		}
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun 	return ret;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun int
dhd_csi_init(dhd_pub_t * dhd)136*4882a593Smuzhiyun dhd_csi_init(dhd_pub_t *dhd)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	int err = BCME_OK;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	NULL_CHECK(dhd, "dhd is NULL", err);
141*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dhd->csi_list);
142*4882a593Smuzhiyun 	dhd->csi_count = 0;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	return err;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun int
dhd_csi_deinit(dhd_pub_t * dhd)148*4882a593Smuzhiyun dhd_csi_deinit(dhd_pub_t *dhd)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	int err = BCME_OK;
151*4882a593Smuzhiyun 	cfr_dump_list_t *ptr, *next;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	NULL_CHECK(dhd, "dhd is NULL", err);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (!list_empty(&dhd->csi_list)) {
156*4882a593Smuzhiyun 		list_for_each_entry_safe(ptr, next, &dhd->csi_list, list) {
157*4882a593Smuzhiyun 			list_del(&ptr->list);
158*4882a593Smuzhiyun 			MFREE(dhd->osh, ptr, sizeof(cfr_dump_list_t));
159*4882a593Smuzhiyun 		}
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 	return err;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun void
dhd_csi_clean_list(dhd_pub_t * dhd)165*4882a593Smuzhiyun dhd_csi_clean_list(dhd_pub_t *dhd)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	cfr_dump_list_t *ptr, *next;
168*4882a593Smuzhiyun 	int num = 0;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (!dhd) {
171*4882a593Smuzhiyun 		DHD_ERROR(("NULL POINTER: %s\n", __FUNCTION__));
172*4882a593Smuzhiyun 		return;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (!list_empty(&dhd->csi_list)) {
176*4882a593Smuzhiyun 		list_for_each_entry_safe(ptr, next, &dhd->csi_list, list) {
177*4882a593Smuzhiyun 			if (0 == ptr->entry.header.remain_length) {
178*4882a593Smuzhiyun 				list_del(&ptr->list);
179*4882a593Smuzhiyun 				num++;
180*4882a593Smuzhiyun 				MFREE(dhd->osh, ptr, sizeof(cfr_dump_list_t));
181*4882a593Smuzhiyun 			}
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 	dhd->csi_count = 0;
185*4882a593Smuzhiyun 	DHD_TRACE(("Clean up %d record\n", num));
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun int
dhd_csi_dump_list(dhd_pub_t * dhd,char * buf)189*4882a593Smuzhiyun dhd_csi_dump_list(dhd_pub_t *dhd, char *buf)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	int ret = BCME_OK;
192*4882a593Smuzhiyun 	cfr_dump_list_t *ptr, *next;
193*4882a593Smuzhiyun 	uint8 * pbuf = buf;
194*4882a593Smuzhiyun 	int num = 0;
195*4882a593Smuzhiyun 	int length = 0;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	NULL_CHECK(dhd, "dhd is NULL", ret);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/* check if this addr exist */
200*4882a593Smuzhiyun 	if (!list_empty(&dhd->csi_list)) {
201*4882a593Smuzhiyun 		list_for_each_entry_safe(ptr, next, &dhd->csi_list, list) {
202*4882a593Smuzhiyun 			if (ptr->entry.header.remain_length) {
203*4882a593Smuzhiyun 				DHD_ERROR(("data not ready %d\n", ptr->entry.header.remain_length));
204*4882a593Smuzhiyun 				continue;
205*4882a593Smuzhiyun 			}
206*4882a593Smuzhiyun 			bcopy(&ptr->entry.header, pbuf, sizeof(cfr_dump_header_t));
207*4882a593Smuzhiyun 			length += sizeof(cfr_dump_header_t);
208*4882a593Smuzhiyun 			pbuf += sizeof(cfr_dump_header_t);
209*4882a593Smuzhiyun 			DHD_TRACE(("Copy data size %d\n", ptr->entry.header.cfr_dump_length));
210*4882a593Smuzhiyun 			bcopy(&ptr->entry.data, pbuf, ptr->entry.header.cfr_dump_length);
211*4882a593Smuzhiyun 			length += ptr->entry.header.cfr_dump_length;
212*4882a593Smuzhiyun 			pbuf += ptr->entry.header.cfr_dump_length;
213*4882a593Smuzhiyun 			num++;
214*4882a593Smuzhiyun 		}
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 	DHD_TRACE(("dump %d record %d bytes\n", num, length));
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return length;
219*4882a593Smuzhiyun }
220