xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_dbg_ring.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * DHD debug ring API and structures
3  *
4  * <<Broadcom-WL-IPTag/Open:>>
5  *
6  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
7  *
8  * Copyright (C) 1999-2017, Broadcom Corporation
9  *
10  *      Unless you and Broadcom execute a separate written software license
11  * agreement governing use of this software, this software is licensed to you
12  * under the terms of the GNU General Public License version 2 (the "GPL"),
13  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
14  * following added to such license:
15  *
16  *      As a special exception, the copyright holders of this software give you
17  * permission to link this software with independent modules, and to copy and
18  * distribute the resulting executable under terms of your choice, provided that
19  * you also meet, for each linked independent module, the terms and conditions of
20  * the license of that module.  An independent module is a module which is not
21  * derived from this software.  The special exception does not apply to any
22  * modifications of the software.
23  *
24  *      Notwithstanding the above, under no circumstances may you combine this
25  * software in any way with any other Broadcom software provided under a license
26  * other than the GPL, without Broadcom's express prior written consent.
27  *
28  * $Id$
29  */
30 #include <typedefs.h>
31 #include <osl.h>
32 #include <bcmutils.h>
33 #include <bcmendian.h>
34 #include <dngl_stats.h>
35 #include <dhd.h>
36 #include <dhd_dbg.h>
37 #include <dhd_dbg_ring.h>
38 
39 int
dhd_dbg_ring_init(dhd_pub_t * dhdp,dhd_dbg_ring_t * ring,uint16 id,uint8 * name,uint32 ring_sz,void * allocd_buf,bool pull_inactive)40 dhd_dbg_ring_init(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring, uint16 id, uint8 *name,
41 		uint32 ring_sz, void *allocd_buf, bool pull_inactive)
42 {
43 	void *buf;
44 	unsigned long flags = 0;
45 
46 	if (allocd_buf == NULL) {
47 			return BCME_NOMEM;
48 	} else {
49 		buf = allocd_buf;
50 	}
51 
52 	ring->lock = DHD_DBG_RING_LOCK_INIT(dhdp->osh);
53 	if (!ring->lock)
54 		return BCME_NOMEM;
55 
56 	DHD_DBG_RING_LOCK(ring->lock, flags);
57 	ring->id = id;
58 	strncpy(ring->name, name, DBGRING_NAME_MAX);
59 	ring->name[DBGRING_NAME_MAX - 1] = 0;
60 	ring->ring_size = ring_sz;
61 	ring->wp = ring->rp = 0;
62 	ring->ring_buf = buf;
63 	ring->threshold = DBGRING_FLUSH_THRESHOLD(ring);
64 	ring->state = RING_SUSPEND;
65 	ring->rem_len = 0;
66 	ring->sched_pull = TRUE;
67 	ring->pull_inactive = pull_inactive;
68 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
69 
70 	return BCME_OK;
71 }
72 
73 void
dhd_dbg_ring_deinit(dhd_pub_t * dhdp,dhd_dbg_ring_t * ring)74 dhd_dbg_ring_deinit(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring)
75 {
76 	unsigned long flags = 0;
77 	DHD_DBG_RING_LOCK(ring->lock, flags);
78 	ring->id = 0;
79 	ring->name[0] = 0;
80 	ring->wp = ring->rp = 0;
81 	memset(&ring->stat, 0, sizeof(ring->stat));
82 	ring->threshold = 0;
83 	ring->state = RING_STOP;
84 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
85 
86 	DHD_DBG_RING_LOCK_DEINIT(dhdp->osh, ring->lock);
87 }
88 
89 void
dhd_dbg_ring_sched_pull(dhd_dbg_ring_t * ring,uint32 pending_len,os_pullreq_t pull_fn,void * os_pvt,const int id)90 dhd_dbg_ring_sched_pull(dhd_dbg_ring_t *ring, uint32 pending_len,
91 		os_pullreq_t pull_fn, void *os_pvt, const int id)
92 {
93 	unsigned long flags = 0;
94 	DHD_DBG_RING_LOCK(ring->lock, flags);
95 	/* if the current pending size is bigger than threshold and
96 	 * threshold is set
97 	 */
98 	if (ring->threshold > 0 &&
99 	   (pending_len >= ring->threshold) && ring->sched_pull) {
100 		/*
101 		 * Update the state and release the lock before calling
102 		 * the pull_fn. Do not transfer control to other layers
103 		 * with locks held. If the call back again calls into
104 		 * the same layer fro this context, can lead to deadlock.
105 		 */
106 		ring->sched_pull = FALSE;
107 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
108 		pull_fn(os_pvt, id);
109 	} else {
110 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
111 	}
112 }
113 
114 uint32
dhd_dbg_ring_get_pending_len(dhd_dbg_ring_t * ring)115 dhd_dbg_ring_get_pending_len(dhd_dbg_ring_t *ring)
116 {
117 	uint32 pending_len = 0;
118 	unsigned long flags = 0;
119 	DHD_DBG_RING_LOCK(ring->lock, flags);
120 	if (ring->stat.written_bytes > ring->stat.read_bytes) {
121 		pending_len = ring->stat.written_bytes - ring->stat.read_bytes;
122 	} else if (ring->stat.written_bytes < ring->stat.read_bytes) {
123 		pending_len = PENDING_LEN_MAX - ring->stat.read_bytes + ring->stat.written_bytes;
124 	} else {
125 		pending_len = 0;
126 	}
127 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
128 
129 	return pending_len;
130 }
131 
132 int
dhd_dbg_ring_push(dhd_dbg_ring_t * ring,dhd_dbg_ring_entry_t * hdr,void * data)133 dhd_dbg_ring_push(dhd_dbg_ring_t *ring, dhd_dbg_ring_entry_t *hdr, void *data)
134 {
135 	unsigned long flags;
136 	uint32 w_len;
137 	uint32 avail_size;
138 	dhd_dbg_ring_entry_t *w_entry, *r_entry;
139 
140 	if (!ring || !hdr || !data) {
141 		return BCME_BADARG;
142 	}
143 
144 	DHD_DBG_RING_LOCK(ring->lock, flags);
145 
146 	if (ring->state != RING_ACTIVE) {
147 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
148 		return BCME_OK;
149 	}
150 
151 	w_len = ENTRY_LENGTH(hdr);
152 
153 	DHD_DBGIF(("%s: RING%d[%s] hdr->len=%u, w_len=%u, wp=%d, rp=%d, ring_start=0x%p;"
154 		" ring_size=%u\n",
155 		__FUNCTION__, ring->id, ring->name, hdr->len, w_len, ring->wp, ring->rp,
156 		ring->ring_buf, ring->ring_size));
157 
158 	if (w_len > ring->ring_size) {
159 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
160 		DHD_ERROR(("%s: RING%d[%s] w_len=%u, ring_size=%u,"
161 			" write size exceeds ring size !\n",
162 			__FUNCTION__, ring->id, ring->name, w_len, ring->ring_size));
163 		return BCME_BUFTOOLONG;
164 	}
165 	/* Claim the space */
166 	do {
167 		avail_size = DBG_RING_CHECK_WRITE_SPACE(ring->rp, ring->wp, ring->ring_size);
168 		if (avail_size <= w_len) {
169 			/* Prepare the space */
170 			if (ring->rp <= ring->wp) {
171 				ring->tail_padded = TRUE;
172 				ring->rem_len = ring->ring_size - ring->wp;
173 				DHD_DBGIF(("%s: RING%d[%s] Insuffient tail space,"
174 					" rp=%d, wp=%d, rem_len=%d, ring_size=%d,"
175 					" avail_size=%d, w_len=%d\n", __FUNCTION__,
176 					ring->id, ring->name, ring->rp, ring->wp,
177 					ring->rem_len, ring->ring_size, avail_size,
178 					w_len));
179 
180 				/* 0 pad insufficient tail space */
181 				memset((uint8 *)ring->ring_buf + ring->wp, 0, ring->rem_len);
182 				/* If read pointer is still at the beginning, make some room */
183 				if (ring->rp == 0) {
184 					r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf +
185 						ring->rp);
186 					ring->rp += ENTRY_LENGTH(r_entry);
187 					ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
188 					DHD_DBGIF(("%s: rp at 0, move by one entry length"
189 						" (%u bytes)\n",
190 						__FUNCTION__, (uint32)ENTRY_LENGTH(r_entry)));
191 				}
192 				if (ring->rp == ring->wp) {
193 					ring->rp = 0;
194 				}
195 				ring->wp = 0;
196 				DHD_DBGIF(("%s: new rp=%u, wp=%u\n",
197 					__FUNCTION__, ring->rp, ring->wp));
198 			} else {
199 				/* Not enough space for new entry, free some up */
200 				r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf +
201 					ring->rp);
202 				/* check bounds before incrementing read ptr */
203 				if (ring->rp + ENTRY_LENGTH(r_entry) >= ring->ring_size) {
204 					DHD_ERROR(("%s: RING%d[%s] rp points out of boundary, "
205 						"ring->wp=%u, ring->rp=%u, ring->ring_size=%d\n",
206 						__FUNCTION__, ring->id, ring->name, ring->wp,
207 						ring->rp, ring->ring_size));
208 					ASSERT(0);
209 					DHD_DBG_RING_UNLOCK(ring->lock, flags);
210 					return BCME_BUFTOOSHORT;
211 				}
212 				ring->rp += ENTRY_LENGTH(r_entry);
213 				/* skip padding if there is one */
214 				if (ring->tail_padded &&
215 					((ring->rp + ring->rem_len) == ring->ring_size)) {
216 					DHD_DBGIF(("%s: RING%d[%s] Found padding,"
217 						" avail_size=%d, w_len=%d, set rp=0\n",
218 						__FUNCTION__, ring->id, ring->name,
219 						avail_size, w_len));
220 					ring->rp = 0;
221 					ring->tail_padded = FALSE;
222 					ring->rem_len = 0;
223 				}
224 				ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
225 				DHD_DBGIF(("%s: RING%d[%s] read_bytes=%d, wp=%d, rp=%d\n",
226 					__FUNCTION__, ring->id, ring->name, ring->stat.read_bytes,
227 					ring->wp, ring->rp));
228 			}
229 		} else {
230 			break;
231 		}
232 	} while (TRUE);
233 
234 	/* check before writing to the ring */
235 	if (ring->wp + w_len >= ring->ring_size) {
236 		DHD_ERROR(("%s: RING%d[%s] wp pointed out of ring boundary, "
237 			"wp=%d, ring_size=%d, w_len=%u\n", __FUNCTION__, ring->id,
238 			ring->name, ring->wp, ring->ring_size, w_len));
239 		ASSERT(0);
240 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
241 		return BCME_BUFTOOLONG;
242 	}
243 
244 	w_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf + ring->wp);
245 	/* header */
246 	memcpy(w_entry, hdr, DBG_RING_ENTRY_SIZE);
247 	w_entry->len = hdr->len;
248 	/* payload */
249 	memcpy((char *)w_entry + DBG_RING_ENTRY_SIZE, data, w_entry->len);
250 	/* update write pointer */
251 	ring->wp += w_len;
252 
253 	/* update statistics */
254 	ring->stat.written_records++;
255 	ring->stat.written_bytes += w_len;
256 	DHD_DBGIF(("%s : RING%d[%s] written_records %d, written_bytes %d, read_bytes=%d,"
257 		" ring->threshold=%d, wp=%d, rp=%d\n", __FUNCTION__, ring->id, ring->name,
258 		ring->stat.written_records, ring->stat.written_bytes, ring->stat.read_bytes,
259 		ring->threshold, ring->wp, ring->rp));
260 
261 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
262 	return BCME_OK;
263 }
264 
265 /*
266  * This function folds ring->lock, so callers of this function
267  * should not hold ring->lock.
268  */
269 int
dhd_dbg_ring_pull_single(dhd_dbg_ring_t * ring,void * data,uint32 buf_len,bool strip_header)270 dhd_dbg_ring_pull_single(dhd_dbg_ring_t *ring, void *data, uint32 buf_len, bool strip_header)
271 {
272 	dhd_dbg_ring_entry_t *r_entry = NULL;
273 	uint32 rlen = 0;
274 	char *buf = NULL;
275 	unsigned long flags;
276 
277 	if (!ring || !data || buf_len <= 0) {
278 		return 0;
279 	}
280 
281 	DHD_DBG_RING_LOCK(ring->lock, flags);
282 
283 	/* pull from ring is allowed for inactive (suspended) ring
284 	 * in case of ecounters only, this is because, for ecounters
285 	 * when a trap occurs the ring is suspended and data is then
286 	 * pulled to dump it to a file. For other rings if ring is
287 	 * not in active state return without processing (as before)
288 	 */
289 	if (!ring->pull_inactive && (ring->state != RING_ACTIVE)) {
290 		goto exit;
291 	}
292 
293 	if (ring->rp == ring->wp) {
294 		goto exit;
295 	}
296 
297 	DHD_DBGIF(("%s: RING%d[%s] buf_len=%u, wp=%d, rp=%d, ring_start=0x%p; ring_size=%u\n",
298 		__FUNCTION__, ring->id, ring->name, buf_len, ring->wp, ring->rp,
299 		ring->ring_buf, ring->ring_size));
300 
301 	r_entry = (dhd_dbg_ring_entry_t *)((uint8 *)ring->ring_buf + ring->rp);
302 
303 	/* Boundary Check */
304 	rlen = ENTRY_LENGTH(r_entry);
305 	if ((ring->rp + rlen) > ring->ring_size) {
306 		DHD_ERROR(("%s: entry len %d is out of boundary of ring size %d,"
307 			" current ring %d[%s] - rp=%d\n", __FUNCTION__, rlen,
308 			ring->ring_size, ring->id, ring->name, ring->rp));
309 		rlen = 0;
310 		goto exit;
311 	}
312 
313 	if (strip_header) {
314 		rlen = r_entry->len;
315 		buf = (char *)r_entry + DBG_RING_ENTRY_SIZE;
316 	} else {
317 		rlen = ENTRY_LENGTH(r_entry);
318 		buf = (char *)r_entry;
319 	}
320 	if (rlen > buf_len) {
321 		DHD_ERROR(("%s: buf len %d is too small for entry len %d\n",
322 			__FUNCTION__, buf_len, rlen));
323 		DHD_ERROR(("%s: ring %d[%s] - ring size=%d, wp=%d, rp=%d\n",
324 			__FUNCTION__, ring->id, ring->name, ring->ring_size,
325 			ring->wp, ring->rp));
326 		ASSERT(0);
327 		rlen = 0;
328 		goto exit;
329 	}
330 
331 	memcpy(data, buf, rlen);
332 	/* update ring context */
333 	ring->rp += ENTRY_LENGTH(r_entry);
334 	/* don't pass wp but skip padding if there is one */
335 	if (ring->rp != ring->wp &&
336 	    ring->tail_padded && ((ring->rp + ring->rem_len) >= ring->ring_size)) {
337 		DHD_DBGIF(("%s: RING%d[%s] Found padding, rp=%d, wp=%d\n",
338 			__FUNCTION__, ring->id, ring->name, ring->rp, ring->wp));
339 		ring->rp = 0;
340 		ring->tail_padded = FALSE;
341 		ring->rem_len = 0;
342 	}
343 	if (ring->rp >= ring->ring_size) {
344 		DHD_ERROR(("%s: RING%d[%s] rp pointed out of ring boundary,"
345 			" rp=%d, ring_size=%d\n", __FUNCTION__, ring->id,
346 			ring->name, ring->rp, ring->ring_size));
347 		ASSERT(0);
348 		rlen = 0;
349 		goto exit;
350 	}
351 	ring->stat.read_bytes += ENTRY_LENGTH(r_entry);
352 	DHD_DBGIF(("%s RING%d[%s]read_bytes %d, wp=%d, rp=%d\n", __FUNCTION__,
353 		ring->id, ring->name, ring->stat.read_bytes, ring->wp, ring->rp));
354 
355 exit:
356 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
357 
358 	return rlen;
359 }
360 
361 int
dhd_dbg_ring_pull(dhd_dbg_ring_t * ring,void * data,uint32 buf_len,bool strip_hdr)362 dhd_dbg_ring_pull(dhd_dbg_ring_t *ring, void *data, uint32 buf_len, bool strip_hdr)
363 {
364 	int32 r_len, total_r_len = 0;
365 	unsigned long flags;
366 
367 	if (!ring || !data)
368 		return 0;
369 
370 	DHD_DBG_RING_LOCK(ring->lock, flags);
371 	if (!ring->pull_inactive && (ring->state != RING_ACTIVE)) {
372 		DHD_DBG_RING_UNLOCK(ring->lock, flags);
373 		return 0;
374 	}
375 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
376 
377 	while (buf_len > 0) {
378 		r_len = dhd_dbg_ring_pull_single(ring, data, buf_len, strip_hdr);
379 		if (r_len == 0)
380 			break;
381 		data = (uint8 *)data + r_len;
382 		buf_len -= r_len;
383 		total_r_len += r_len;
384 	}
385 
386 	return total_r_len;
387 }
388 
389 int
dhd_dbg_ring_config(dhd_dbg_ring_t * ring,int log_level,uint32 threshold)390 dhd_dbg_ring_config(dhd_dbg_ring_t *ring, int log_level, uint32 threshold)
391 {
392 	unsigned long flags = 0;
393 	if (!ring)
394 		return BCME_BADADDR;
395 
396 	if (ring->state == RING_STOP)
397 		return BCME_UNSUPPORTED;
398 
399 	DHD_DBG_RING_LOCK(ring->lock, flags);
400 
401 	if (log_level == 0)
402 		ring->state = RING_SUSPEND;
403 	else
404 		ring->state = RING_ACTIVE;
405 
406 	ring->log_level = log_level;
407 	ring->threshold = MIN(threshold, DBGRING_FLUSH_THRESHOLD(ring));
408 
409 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
410 
411 	return BCME_OK;
412 }
413 
414 void
dhd_dbg_ring_start(dhd_dbg_ring_t * ring)415 dhd_dbg_ring_start(dhd_dbg_ring_t *ring)
416 {
417 	if (!ring)
418 		return;
419 
420 	/* Initialize the information for the ring */
421 	ring->state = RING_SUSPEND;
422 	ring->log_level = 0;
423 	ring->rp = ring->wp = 0;
424 	ring->threshold = 0;
425 	memset(&ring->stat, 0, sizeof(struct ring_statistics));
426 	memset(ring->ring_buf, 0, ring->ring_size);
427 }
428