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