1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*-
13  * Copyright (c) 2017 Chelsio Communications, Inc.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38 
39 #ifndef __CUDBG_LIB_COMMON_H__
40 #define __CUDBG_LIB_COMMON_H__
41 
42 #define CUDBG_EXT_DATA_BIT          0
43 #define CUDBG_EXT_DATA_VALID        (1 << CUDBG_EXT_DATA_BIT)
44 
45 #ifdef _KERNEL
46 #include "common/t4_hw.h"
47 
48 #define CUDBG_SF_MAX_SECTOR         (FLASH_CUDBG_START_SEC + FLASH_CUDBG_NSECS)
49 #define CUDBG_SF_SECTOR_SIZE        SF_SEC_SIZE
50 #define CUDBG_START_SEC             FLASH_CUDBG_START_SEC
51 #define CUDBG_FLASH_SIZE            FLASH_CUDBG_MAX_SIZE
52 
53 struct cudbg_flash_sec_info {
54 	int par_sec;		   /* Represent partially filled sector no */
55 	int par_sec_offset;	   /* Offset in partially filled sector */
56 	int cur_seq_no;
57 	u32 max_seq_no;
58 	u32 max_seq_sec;
59 	u32 hdr_data_len;	   /* Total data */
60 	u32 skip_size;		   /* Total size of large entities. */
61 	u64 max_timestamp;
62 	char sec_data[CUDBG_SF_SECTOR_SIZE];
63 	u8 sec_bitmap[8];
64 };
65 
66 void update_skip_size(struct cudbg_flash_sec_info *, u32);
67 #endif
68 #include "osdep.h"
69 
70 struct cudbg_hdr {
71 	u32 signature;
72 	u32 hdr_len;
73 	u16 major_ver;
74 	u16 minor_ver;
75 	u32 data_len;
76 	u32 hdr_flags;
77 	u16 max_entities;
78 	u8 chip_ver;
79 	u8 reserved1;
80 	u32 reserved[8];
81 };
82 
83 struct cudbg_entity_hdr {
84 	u32 entity_type;
85 	u32 start_offset;
86 	u32 size;
87 	int hdr_flags;
88 	u32 sys_warn;
89 	u32 sys_err;
90 	u8 num_pad;
91 	u8 flag;		/* bit 0 is used to indicate ext data */
92 	u8 reserved1[2];
93 	u32 next_ext_offset;	/* pointer to next extended entity meta data */
94 	u32 reserved[5];
95 };
96 
97 struct cudbg_ver_hdr {
98 	u32 signature;
99 	u16 revision;
100 	u16 size;
101 };
102 
103 struct cudbg_buffer {
104 	u32 size;
105 	u32 offset;
106 	char *data;
107 };
108 
109 struct cudbg_error {
110 	int sys_err;
111 	int sys_warn;
112 	int app_err;
113 };
114 
115 #define HTONL_NIBBLE(data) ( \
116 			    (((uint32_t)(data) >> 28) & 0x0000000F) | \
117 			    (((uint32_t)(data) >> 20)  & 0x000000F0) | \
118 			    (((uint32_t)(data) >> 12)  & 0x00000F00) | \
119 			    (((uint32_t)(data) >> 4) & 0x0000F000) | \
120 			    (((uint32_t)(data) << 4) & 0x000F0000) | \
121 			    (((uint32_t)(data) << 12)  & 0x00F00000) | \
122 			    (((uint32_t)(data) << 20)  & 0x0F000000) | \
123 			    (((uint32_t)(data) << 28) & 0xF0000000))
124 
125 #define CDUMP_MAX_COMP_BUF_SIZE    ((64 * 1024) - 1)
126 #define CUDBG_CHUNK_SIZE  ((CDUMP_MAX_COMP_BUF_SIZE/1024) * 1024)
127 
128 #define CUDBG_LEGACY_SIGNATURE 123
129 #define CUDBG_SIGNATURE 67856866 /* CUDB in ascii */
130 #define CUDBG_FL_SIGNATURE 0x4355464c /* CUFL in ascii */
131 
132 #define CUDBG_FL_MAJOR_VERSION	    1
133 #define CUDBG_FL_MINOR_VERSION	    1
134 #define CUDBG_FL_BUILD_VERSION	    0
135 
136 int write_compression_hdr(struct cudbg_buffer *, struct cudbg_buffer *);
137 int compress_buff(struct cudbg_buffer *, struct cudbg_buffer *);
138 int get_scratch_buff(struct cudbg_buffer *, u32, struct cudbg_buffer *);
139 void release_scratch_buff(struct cudbg_buffer *, struct cudbg_buffer *);
140 int decompress_buffer(struct cudbg_buffer *, struct cudbg_buffer *);
141 int validate_buffer(struct cudbg_buffer *compressed_buffer);
142 int decompress_buffer_wrapper(struct cudbg_buffer *pc_buff,
143 			      struct cudbg_buffer *pdc_buff);
144 int get_entity_rev(struct cudbg_ver_hdr *ver_hdr);
145 void sort_t(void *base, int num, int size,
146 	    int (*cmp_func)(const void *, const void *),
147 	    void (*swap_func)(void *, void *, int size));
148 int cudbg_read_flash(void *handle, void *data, u32 size, int data_flag);
149 int cudbg_write_flash(void *handle, u64 timestamp, void *data,
150 		      u32 start_offset, u32 start_hdr_offset,
151 		      u32 cur_entity_size,
152 		      u32 ext_size);
153 #endif
154