xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_dbg_ring.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * DHD debug ring header file
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 
31 #ifndef __DHD_DBG_RING_H__
32 #define __DHD_DBG_RING_H__
33 
34 #include <bcmutils.h>
35 
36 #define PACKED_STRUCT __attribute__ ((packed))
37 
38 #define DBGRING_NAME_MAX 32
39 
40 enum dbg_ring_state {
41 	RING_STOP = 0,  /* ring is not initialized */
42 	RING_ACTIVE,    /* ring is live and logging */
43 	RING_SUSPEND    /* ring is initialized but not logging */
44 };
45 
46 /* each entry in dbg ring has below header, to handle
47  * variable length records in ring
48  */
49 typedef struct dhd_dbg_ring_entry {
50 	uint16 len; /* payload length excluding the header */
51 	uint8 flags;
52 	uint8 type; /* Per ring specific */
53 	uint64 timestamp; /* present if has_timestamp bit is set. */
54 } PACKED_STRUCT dhd_dbg_ring_entry_t;
55 
56 struct ring_statistics {
57 	/* number of bytes that was written to the buffer by driver */
58 	uint32 written_bytes;
59 	/* number of bytes that was read from the buffer by user land */
60 	uint32 read_bytes;
61 	/* number of records that was written to the buffer by driver */
62 	uint32 written_records;
63 };
64 
65 typedef struct dhd_dbg_ring_status {
66 	uint8 name[DBGRING_NAME_MAX];
67 	uint32 flags;
68 	int ring_id; /* unique integer representing the ring */
69 	/* total memory size allocated for the buffer */
70 	uint32 ring_buffer_byte_size;
71 	uint32 verbose_level;
72 	/* number of bytes that was written to the buffer by driver */
73 	uint32 written_bytes;
74 	/* number of bytes that was read from the buffer by user land */
75 	uint32 read_bytes;
76 	/* number of records that was read from the buffer by user land */
77 	uint32 written_records;
78 } dhd_dbg_ring_status_t;
79 
80 typedef struct dhd_dbg_ring {
81 	int     id;		/* ring id */
82 	uint8   name[DBGRING_NAME_MAX]; /* name string */
83 	uint32  ring_size;	/* numbers of item in ring */
84 	uint32  wp;		/* write pointer */
85 	uint32  rp;		/* read pointer */
86 	uint32  rp_tmp;		/* tmp read pointer */
87 	uint32  log_level;	/* log_level */
88 	uint32  threshold;	/* threshold bytes */
89 	void *  ring_buf;	/* pointer of actually ring buffer */
90 	void *  lock;		/* lock for ring access */
91 	struct ring_statistics stat;	/* statistics */
92 	enum dbg_ring_state state;	/* ring state enum */
93 	bool tail_padded;	/* writer does not have enough space */
94 	uint32 rem_len;		/* number of bytes from wp_pad to end */
95 	bool sched_pull;	/* schedule reader immediately */
96 	bool pull_inactive;	/* pull contents from ring even if it is inactive */
97 } dhd_dbg_ring_t;
98 
99 #define DBGRING_FLUSH_THRESHOLD(ring)		(ring->ring_size / 3)
100 #define RING_STAT_TO_STATUS(ring, status) \
101 	do {               \
102 		strncpy(status.name, ring->name, \
103 			sizeof(status.name) - 1);  \
104 		status.ring_id = ring->id;     \
105 		status.ring_buffer_byte_size = ring->ring_size;  \
106 		status.written_bytes = ring->stat.written_bytes; \
107 		status.written_records = ring->stat.written_records; \
108 		status.read_bytes = ring->stat.read_bytes; \
109 		status.verbose_level = ring->log_level; \
110 	} while (0)
111 
112 #define DBG_RING_ENTRY_SIZE (sizeof(dhd_dbg_ring_entry_t))
113 #define ENTRY_LENGTH(hdr) ((hdr)->len + DBG_RING_ENTRY_SIZE)
114 #define PAYLOAD_MAX_LEN 65535
115 #define PAYLOAD_ECNTR_MAX_LEN 1648u
116 #define PAYLOAD_RTT_MAX_LEN 1648u
117 #define PENDING_LEN_MAX 0xFFFFFFFF
118 #define DBG_RING_STATUS_SIZE (sizeof(dhd_dbg_ring_status_t))
119 
120 #define TXACTIVESZ(r, w, d)			(((r) <= (w)) ? ((w) - (r)) : ((d) - (r) + (w)))
121 #define DBG_RING_READ_AVAIL_SPACE(w, r, d)		(((w) >= (r)) ? ((w) - (r)) : ((d) - (r)))
122 #define DBG_RING_WRITE_SPACE_AVAIL_CONT(r, w, d)	(((w) >= (r)) ? ((d) - (w)) : ((r) - (w)))
123 #define DBG_RING_WRITE_SPACE_AVAIL(r, w, d)		(d - (TXACTIVESZ(r, w, d)))
124 #define DBG_RING_CHECK_WRITE_SPACE(r, w, d)		\
125 		MIN(DBG_RING_WRITE_SPACE_AVAIL(r, w, d), DBG_RING_WRITE_SPACE_AVAIL_CONT(r, w, d))
126 
127 typedef void (*os_pullreq_t)(void *os_priv, const int ring_id);
128 
129 int dhd_dbg_ring_init(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring, uint16 id, uint8 *name,
130 		uint32 ring_sz, void *allocd_buf, bool pull_inactive);
131 void dhd_dbg_ring_deinit(dhd_pub_t *dhdp, dhd_dbg_ring_t *ring);
132 int dhd_dbg_ring_push(dhd_dbg_ring_t *ring, dhd_dbg_ring_entry_t *hdr, void *data);
133 int dhd_dbg_ring_pull(dhd_dbg_ring_t *ring, void *data, uint32 buf_len,
134 		bool strip_hdr);
135 int dhd_dbg_ring_pull_single(dhd_dbg_ring_t *ring, void *data, uint32 buf_len,
136 	bool strip_header);
137 uint32 dhd_dbg_ring_get_pending_len(dhd_dbg_ring_t *ring);
138 void dhd_dbg_ring_sched_pull(dhd_dbg_ring_t *ring, uint32 pending_len,
139 		os_pullreq_t pull_fn, void *os_pvt, const int id);
140 int dhd_dbg_ring_config(dhd_dbg_ring_t *ring, int log_level, uint32 threshold);
141 void dhd_dbg_ring_start(dhd_dbg_ring_t *ring);
142 #endif /* __DHD_DBG_RING_H__ */
143