xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_dbg_ring.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * DHD debug ring header file
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
11*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
12*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
13*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
14*4882a593Smuzhiyun  * following added to such license:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
17*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
18*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
19*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
20*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
21*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
22*4882a593Smuzhiyun  * modifications of the software.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
25*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
26*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * $Id$
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifndef __DHD_DBG_RING_H__
32*4882a593Smuzhiyun #define __DHD_DBG_RING_H__
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <bcmutils.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define PACKED_STRUCT __attribute__ ((packed))
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define DBGRING_NAME_MAX 32
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun enum dbg_ring_state {
41*4882a593Smuzhiyun 	RING_STOP = 0,  /* ring is not initialized */
42*4882a593Smuzhiyun 	RING_ACTIVE,    /* ring is live and logging */
43*4882a593Smuzhiyun 	RING_SUSPEND    /* ring is initialized but not logging */
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* each entry in dbg ring has below header, to handle
47*4882a593Smuzhiyun  * variable length records in ring
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun typedef struct dhd_dbg_ring_entry {
50*4882a593Smuzhiyun 	uint16 len; /* payload length excluding the header */
51*4882a593Smuzhiyun 	uint8 flags;
52*4882a593Smuzhiyun 	uint8 type; /* Per ring specific */
53*4882a593Smuzhiyun 	uint64 timestamp; /* present if has_timestamp bit is set. */
54*4882a593Smuzhiyun } PACKED_STRUCT dhd_dbg_ring_entry_t;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun struct ring_statistics {
57*4882a593Smuzhiyun 	/* number of bytes that was written to the buffer by driver */
58*4882a593Smuzhiyun 	uint32 written_bytes;
59*4882a593Smuzhiyun 	/* number of bytes that was read from the buffer by user land */
60*4882a593Smuzhiyun 	uint32 read_bytes;
61*4882a593Smuzhiyun 	/* number of records that was written to the buffer by driver */
62*4882a593Smuzhiyun 	uint32 written_records;
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun typedef struct dhd_dbg_ring_status {
66*4882a593Smuzhiyun 	uint8 name[DBGRING_NAME_MAX];
67*4882a593Smuzhiyun 	uint32 flags;
68*4882a593Smuzhiyun 	int ring_id; /* unique integer representing the ring */
69*4882a593Smuzhiyun 	/* total memory size allocated for the buffer */
70*4882a593Smuzhiyun 	uint32 ring_buffer_byte_size;
71*4882a593Smuzhiyun 	uint32 verbose_level;
72*4882a593Smuzhiyun 	/* number of bytes that was written to the buffer by driver */
73*4882a593Smuzhiyun 	uint32 written_bytes;
74*4882a593Smuzhiyun 	/* number of bytes that was read from the buffer by user land */
75*4882a593Smuzhiyun 	uint32 read_bytes;
76*4882a593Smuzhiyun 	/* number of records that was read from the buffer by user land */
77*4882a593Smuzhiyun 	uint32 written_records;
78*4882a593Smuzhiyun } dhd_dbg_ring_status_t;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun typedef struct dhd_dbg_ring {
81*4882a593Smuzhiyun 	int     id;		/* ring id */
82*4882a593Smuzhiyun 	uint8   name[DBGRING_NAME_MAX]; /* name string */
83*4882a593Smuzhiyun 	uint32  ring_size;	/* numbers of item in ring */
84*4882a593Smuzhiyun 	uint32  wp;		/* write pointer */
85*4882a593Smuzhiyun 	uint32  rp;		/* read pointer */
86*4882a593Smuzhiyun 	uint32  rp_tmp;		/* tmp read pointer */
87*4882a593Smuzhiyun 	uint32  log_level;	/* log_level */
88*4882a593Smuzhiyun 	uint32  threshold;	/* threshold bytes */
89*4882a593Smuzhiyun 	void *  ring_buf;	/* pointer of actually ring buffer */
90*4882a593Smuzhiyun 	void *  lock;		/* lock for ring access */
91*4882a593Smuzhiyun 	struct ring_statistics stat;	/* statistics */
92*4882a593Smuzhiyun 	enum dbg_ring_state state;	/* ring state enum */
93*4882a593Smuzhiyun 	bool tail_padded;	/* writer does not have enough space */
94*4882a593Smuzhiyun 	uint32 rem_len;		/* number of bytes from wp_pad to end */
95*4882a593Smuzhiyun 	bool sched_pull;	/* schedule reader immediately */
96*4882a593Smuzhiyun 	bool pull_inactive;	/* pull contents from ring even if it is inactive */
97*4882a593Smuzhiyun } dhd_dbg_ring_t;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #define DBGRING_FLUSH_THRESHOLD(ring)		(ring->ring_size / 3)
100*4882a593Smuzhiyun #define RING_STAT_TO_STATUS(ring, status) \
101*4882a593Smuzhiyun 	do {               \
102*4882a593Smuzhiyun 		strncpy(status.name, ring->name, \
103*4882a593Smuzhiyun 			sizeof(status.name) - 1);  \
104*4882a593Smuzhiyun 		status.ring_id = ring->id;     \
105*4882a593Smuzhiyun 		status.ring_buffer_byte_size = ring->ring_size;  \
106*4882a593Smuzhiyun 		status.written_bytes = ring->stat.written_bytes; \
107*4882a593Smuzhiyun 		status.written_records = ring->stat.written_records; \
108*4882a593Smuzhiyun 		status.read_bytes = ring->stat.read_bytes; \
109*4882a593Smuzhiyun 		status.verbose_level = ring->log_level; \
110*4882a593Smuzhiyun 	} while (0)
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun #define DBG_RING_ENTRY_SIZE (sizeof(dhd_dbg_ring_entry_t))
113*4882a593Smuzhiyun #define ENTRY_LENGTH(hdr) ((hdr)->len + DBG_RING_ENTRY_SIZE)
114*4882a593Smuzhiyun #define PAYLOAD_MAX_LEN 65535
115*4882a593Smuzhiyun #define PAYLOAD_ECNTR_MAX_LEN 1648u
116*4882a593Smuzhiyun #define PAYLOAD_RTT_MAX_LEN 1648u
117*4882a593Smuzhiyun #define PENDING_LEN_MAX 0xFFFFFFFF
118*4882a593Smuzhiyun #define DBG_RING_STATUS_SIZE (sizeof(dhd_dbg_ring_status_t))
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #define TXACTIVESZ(r, w, d)			(((r) <= (w)) ? ((w) - (r)) : ((d) - (r) + (w)))
121*4882a593Smuzhiyun #define DBG_RING_READ_AVAIL_SPACE(w, r, d)		(((w) >= (r)) ? ((w) - (r)) : ((d) - (r)))
122*4882a593Smuzhiyun #define DBG_RING_WRITE_SPACE_AVAIL_CONT(r, w, d)	(((w) >= (r)) ? ((d) - (w)) : ((r) - (w)))
123*4882a593Smuzhiyun #define DBG_RING_WRITE_SPACE_AVAIL(r, w, d)		(d - (TXACTIVESZ(r, w, d)))
124*4882a593Smuzhiyun #define DBG_RING_CHECK_WRITE_SPACE(r, w, d)		\
125*4882a593Smuzhiyun 		MIN(DBG_RING_WRITE_SPACE_AVAIL(r, w, d), DBG_RING_WRITE_SPACE_AVAIL_CONT(r, w, d))
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun typedef void (*os_pullreq_t)(void *os_priv, const int ring_id);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun int dhd_dbg_ring_init(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring, uint16 id, uint8 *name,
130*4882a593Smuzhiyun 		uint32 ring_sz, void *allocd_buf, bool pull_inactive);
131*4882a593Smuzhiyun void dhd_dbg_ring_deinit(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring);
132*4882a593Smuzhiyun int dhd_dbg_ring_push(dhd_dbg_ring_t *ring, dhd_dbg_ring_entry_t *hdr, void *data);
133*4882a593Smuzhiyun int dhd_dbg_ring_pull(dhd_dbg_ring_t *ring, void *data, uint32 buf_len,
134*4882a593Smuzhiyun 		bool strip_hdr);
135*4882a593Smuzhiyun int dhd_dbg_ring_pull_single(dhd_dbg_ring_t *ring, void *data, uint32 buf_len,
136*4882a593Smuzhiyun 	bool strip_header);
137*4882a593Smuzhiyun uint32 dhd_dbg_ring_get_pending_len(dhd_dbg_ring_t *ring);
138*4882a593Smuzhiyun void dhd_dbg_ring_sched_pull(dhd_dbg_ring_t *ring, uint32 pending_len,
139*4882a593Smuzhiyun 		os_pullreq_t pull_fn, void *os_pvt, const int id);
140*4882a593Smuzhiyun int dhd_dbg_ring_config(dhd_dbg_ring_t *ring, int log_level, uint32 threshold);
141*4882a593Smuzhiyun void dhd_dbg_ring_start(dhd_dbg_ring_t *ring);
142*4882a593Smuzhiyun #endif /* __DHD_DBG_RING_H__ */
143