1322014Snp/*-
2322014Snp * Copyright (c) 2017 Chelsio Communications, Inc.
3322014Snp * All rights reserved.
4322014Snp *
5322014Snp * Redistribution and use in source and binary forms, with or without
6322014Snp * modification, are permitted provided that the following conditions
7322014Snp * are met:
8322014Snp * 1. Redistributions of source code must retain the above copyright
9322014Snp *    notice, this list of conditions and the following disclaimer.
10322014Snp * 2. Redistributions in binary form must reproduce the above copyright
11322014Snp *    notice, this list of conditions and the following disclaimer in the
12322014Snp *    documentation and/or other materials provided with the distribution.
13322014Snp *
14322014Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15322014Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16322014Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17322014Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18322014Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19322014Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20322014Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21322014Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22322014Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23322014Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24322014Snp * SUCH DAMAGE.
25322014Snp *
26322014Snp * $FreeBSD: stable/11/sys/dev/cxgbe/cudbg/cudbg_lib_common.h 322014 2017-08-03 14:43:30Z np $
27322014Snp *
28322014Snp */
29322014Snp
30322014Snp#ifndef __CUDBG_LIB_COMMON_H__
31322014Snp#define __CUDBG_LIB_COMMON_H__
32322014Snp
33322014Snp/* Extended entity
34322014Snp *
35322014Snp * Layout of the cudbg dump file when extended entity is present.
36322014Snp *
37322014Snp *
38322014Snp *           ----------------
39322014Snp *           | Global header |
40322014Snp *           |---------------|
41322014Snp *           |entity headers |
42322014Snp *           |---------------|
43322014Snp *           | Entity data   |
44322014Snp *           |      *        |
45322014Snp *           |      *        |
46322014Snp *           |      *        |
47322014Snp *           |---------------|
48322014Snp *           |extended entity|
49322014Snp *           |    header     |
50322014Snp *           |---------------|
51322014Snp *           |extended entity|
52322014Snp *           |     data      |
53322014Snp *           -----------------
54322014Snp *
55322014Snp *
56322014Snp * Extended entity: This comes into picture only when cudbg_collect() is called
57322014Snp * multiple times.
58322014Snp */
59322014Snp
60322014Snp#ifndef CUDBG_LITE
61322014Snp#include "common/t4_hw.h"
62322014Snp#endif
63322014Snp
64322014Snp#define CUDBG_SF_MAX_SECTOR         (FLASH_CUDBG_START_SEC + FLASH_CUDBG_NSECS)
65322014Snp#define CUDBG_SF_SECTOR_SIZE        SF_SEC_SIZE
66322014Snp#define CUDBG_START_SEC             FLASH_CUDBG_START_SEC
67322014Snp#define CUDBG_FLASH_SIZE            FLASH_CUDBG_MAX_SIZE
68322014Snp
69322014Snp#define CUDBG_EXT_DATA_BIT          0
70322014Snp#define CUDBG_EXT_DATA_VALID        (1 << CUDBG_EXT_DATA_BIT)
71322014Snp
72322014Snpstruct cudbg_hdr {
73322014Snp	u32 signature;
74322014Snp	u32 hdr_len;
75322014Snp	u16 major_ver;
76322014Snp	u16 minor_ver;
77322014Snp	u32 data_len;
78322014Snp	u32 hdr_flags;
79322014Snp	u16 max_entities;
80322014Snp	u8 chip_ver;
81322014Snp	u8 reserved1;
82322014Snp	u32 reserved[8];
83322014Snp};
84322014Snp
85322014Snpstruct cudbg_entity_hdr {
86322014Snp	u32 entity_type;
87322014Snp	u32 start_offset;
88322014Snp	u32 size;
89322014Snp	int hdr_flags;
90322014Snp	u32 sys_warn;
91322014Snp	u32 sys_err;
92322014Snp	u8 num_pad;
93322014Snp	u8 flag;		/* bit 0 is used to indicate ext data */
94322014Snp	u8 reserved1[2];
95322014Snp	u32 next_ext_offset;	/* pointer to next extended entity meta data */
96322014Snp	u32 reserved[5];
97322014Snp};
98322014Snp
99322014Snpstruct cudbg_ver_hdr {
100322014Snp	u32 signature;
101322014Snp	u16 revision;
102322014Snp	u16 size;
103322014Snp};
104322014Snp
105322014Snpstruct cudbg_buffer {
106322014Snp	u32 size;
107322014Snp	u32 offset;
108322014Snp	char *data;
109322014Snp};
110322014Snp
111322014Snpstruct cudbg_error {
112322014Snp	int sys_err;
113322014Snp	int sys_warn;
114322014Snp	int app_err;
115322014Snp};
116322014Snp
117322014Snpstruct cudbg_flash_sec_info {
118322014Snp	int par_sec;		   /* Represent partially filled sector no */
119322014Snp	int par_sec_offset;	   /* Offset in partially filled sector */
120322014Snp	int cur_seq_no;
121322014Snp	u32 max_seq_no;
122322014Snp	u32 max_seq_sec;
123322014Snp	u32 hdr_data_len;	   /* Total data */
124322014Snp	u32 skip_size;		   /* Total size of large entities. */
125322014Snp	u64 max_timestamp;
126322014Snp	char sec_data[CUDBG_SF_SECTOR_SIZE];
127322014Snp	u8 sec_bitmap[8];
128322014Snp};
129322014Snp
130322014Snpstruct cudbg_private {
131322014Snp	struct cudbg_init  dbg_init;
132322014Snp	struct cudbg_flash_sec_info  sec_info;
133322014Snp};
134322014Snp
135322014Snp#define HTONL_NIBBLE(data) ( \
136322014Snp			    (((uint32_t)(data) >> 28) & 0x0000000F) | \
137322014Snp			    (((uint32_t)(data) >> 20)  & 0x000000F0) | \
138322014Snp			    (((uint32_t)(data) >> 12)  & 0x00000F00) | \
139322014Snp			    (((uint32_t)(data) >> 4) & 0x0000F000) | \
140322014Snp			    (((uint32_t)(data) << 4) & 0x000F0000) | \
141322014Snp			    (((uint32_t)(data) << 12)  & 0x00F00000) | \
142322014Snp			    (((uint32_t)(data) << 20)  & 0x0F000000) | \
143322014Snp			    (((uint32_t)(data) << 28) & 0xF0000000))
144322014Snp
145322014Snp#define CDUMP_MAX_COMP_BUF_SIZE    ((64 * 1024) - 1)
146322014Snp#define CUDBG_CHUNK_SIZE  ((CDUMP_MAX_COMP_BUF_SIZE/1024) * 1024)
147322014Snp
148322014Snp#define CUDBG_LEGACY_SIGNATURE 123
149322014Snp#define CUDBG_SIGNATURE 67856866 /* CUDB in ascii */
150322014Snp#define CUDBG_FL_SIGNATURE 0x4355464c /* CUFL in ascii */
151322014Snp
152322014Snp#define CUDBG_FL_MAJOR_VERSION	    1
153322014Snp#define CUDBG_FL_MINOR_VERSION	    1
154322014Snp#define CUDBG_FL_BUILD_VERSION	    0
155322014Snp
156322014Snpvoid update_skip_size(struct cudbg_flash_sec_info *, u32);
157322014Snpint write_compression_hdr(struct cudbg_buffer *, struct cudbg_buffer *);
158322014Snpint compress_buff(struct cudbg_buffer *, struct cudbg_buffer *);
159322014Snpint get_scratch_buff(struct cudbg_buffer *, u32, struct cudbg_buffer *);
160322014Snpvoid release_scratch_buff(struct cudbg_buffer *, struct cudbg_buffer *);
161322014Snpint decompress_buffer(struct cudbg_buffer *, struct cudbg_buffer *);
162322014Snpint validate_buffer(struct cudbg_buffer *compressed_buffer);
163322014Snpint decompress_buffer_wrapper(struct cudbg_buffer *pc_buff,
164322014Snp			      struct cudbg_buffer *pdc_buff);
165322014Snpint get_entity_rev(struct cudbg_ver_hdr *ver_hdr);
166322014Snpvoid sort_t(void *base, int num, int size,
167322014Snp	    int (*cmp_func)(const void *, const void *),
168322014Snp	    void (*swap_func)(void *, void *, int size));
169322014Snpint cudbg_read_flash(void *handle, void *data, u32 size, int data_flag);
170322014Snpint cudbg_write_flash(void *handle, u64 timestamp, void *data,
171322014Snp		      u32 start_offset, u32 start_hdr_offset,
172322014Snp		      u32 cur_entity_size,
173322014Snp		      u32 ext_size);
174322014Snp#endif
175