17e6ad46Vishal Kulkarni/*
27e6ad46Vishal Kulkarni * This file and its contents are supplied under the terms of the
37e6ad46Vishal Kulkarni * Common Development and Distribution License ("CDDL"), version 1.0.
47e6ad46Vishal Kulkarni * You may only use this file in accordance with the terms of version
57e6ad46Vishal Kulkarni * 1.0 of the CDDL.
67e6ad46Vishal Kulkarni *
77e6ad46Vishal Kulkarni * A full copy of the text of the CDDL should have accompanied this
87e6ad46Vishal Kulkarni * source. A copy of the CDDL is also available via the Internet at
97e6ad46Vishal Kulkarni * http://www.illumos.org/license/CDDL.
107e6ad46Vishal Kulkarni */
117e6ad46Vishal Kulkarni
127e6ad46Vishal Kulkarni/*-
137e6ad46Vishal Kulkarni * Copyright (c) 2017 Chelsio Communications, Inc.
147e6ad46Vishal Kulkarni * All rights reserved.
157e6ad46Vishal Kulkarni *
167e6ad46Vishal Kulkarni * Redistribution and use in source and binary forms, with or without
177e6ad46Vishal Kulkarni * modification, are permitted provided that the following conditions
187e6ad46Vishal Kulkarni * are met:
197e6ad46Vishal Kulkarni * 1. Redistributions of source code must retain the above copyright
207e6ad46Vishal Kulkarni *    notice, this list of conditions and the following disclaimer.
217e6ad46Vishal Kulkarni * 2. Redistributions in binary form must reproduce the above copyright
227e6ad46Vishal Kulkarni *    notice, this list of conditions and the following disclaimer in the
237e6ad46Vishal Kulkarni *    documentation and/or other materials provided with the distribution.
247e6ad46Vishal Kulkarni *
257e6ad46Vishal Kulkarni * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
267e6ad46Vishal Kulkarni * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
277e6ad46Vishal Kulkarni * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
287e6ad46Vishal Kulkarni * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
297e6ad46Vishal Kulkarni * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
307e6ad46Vishal Kulkarni * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
317e6ad46Vishal Kulkarni * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
327e6ad46Vishal Kulkarni * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
337e6ad46Vishal Kulkarni * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
347e6ad46Vishal Kulkarni * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
357e6ad46Vishal Kulkarni * SUCH DAMAGE.
367e6ad46Vishal Kulkarni *
377e6ad46Vishal Kulkarni */
387e6ad46Vishal Kulkarni
397e6ad46Vishal Kulkarni#ifndef __CUDBG_LIB_COMMON_H__
407e6ad46Vishal Kulkarni#define __CUDBG_LIB_COMMON_H__
417e6ad46Vishal Kulkarni
427e6ad46Vishal Kulkarni#define CUDBG_EXT_DATA_BIT          0
437e6ad46Vishal Kulkarni#define CUDBG_EXT_DATA_VALID        (1 << CUDBG_EXT_DATA_BIT)
447e6ad46Vishal Kulkarni
457e6ad46Vishal Kulkarni#ifdef _KERNEL
467e6ad46Vishal Kulkarni#include "common/t4_hw.h"
477e6ad46Vishal Kulkarni
487e6ad46Vishal Kulkarni#define CUDBG_SF_MAX_SECTOR         (FLASH_CUDBG_START_SEC + FLASH_CUDBG_NSECS)
497e6ad46Vishal Kulkarni#define CUDBG_SF_SECTOR_SIZE        SF_SEC_SIZE
507e6ad46Vishal Kulkarni#define CUDBG_START_SEC             FLASH_CUDBG_START_SEC
517e6ad46Vishal Kulkarni#define CUDBG_FLASH_SIZE            FLASH_CUDBG_MAX_SIZE
527e6ad46Vishal Kulkarni
537e6ad46Vishal Kulkarnistruct cudbg_flash_sec_info {
547e6ad46Vishal Kulkarni	int par_sec;		   /* Represent partially filled sector no */
557e6ad46Vishal Kulkarni	int par_sec_offset;	   /* Offset in partially filled sector */
567e6ad46Vishal Kulkarni	int cur_seq_no;
577e6ad46Vishal Kulkarni	u32 max_seq_no;
587e6ad46Vishal Kulkarni	u32 max_seq_sec;
597e6ad46Vishal Kulkarni	u32 hdr_data_len;	   /* Total data */
607e6ad46Vishal Kulkarni	u32 skip_size;		   /* Total size of large entities. */
617e6ad46Vishal Kulkarni	u64 max_timestamp;
627e6ad46Vishal Kulkarni	char sec_data[CUDBG_SF_SECTOR_SIZE];
637e6ad46Vishal Kulkarni	u8 sec_bitmap[8];
647e6ad46Vishal Kulkarni};
657e6ad46Vishal Kulkarni
667e6ad46Vishal Kulkarnivoid update_skip_size(struct cudbg_flash_sec_info *, u32);
677e6ad46Vishal Kulkarni#endif
687e6ad46Vishal Kulkarni#include "osdep.h"
697e6ad46Vishal Kulkarni
707e6ad46Vishal Kulkarnistruct cudbg_hdr {
717e6ad46Vishal Kulkarni	u32 signature;
727e6ad46Vishal Kulkarni	u32 hdr_len;
737e6ad46Vishal Kulkarni	u16 major_ver;
747e6ad46Vishal Kulkarni	u16 minor_ver;
757e6ad46Vishal Kulkarni	u32 data_len;
767e6ad46Vishal Kulkarni	u32 hdr_flags;
777e6ad46Vishal Kulkarni	u16 max_entities;
787e6ad46Vishal Kulkarni	u8 chip_ver;
797e6ad46Vishal Kulkarni	u8 reserved1;
807e6ad46Vishal Kulkarni	u32 reserved[8];
817e6ad46Vishal Kulkarni};
827e6ad46Vishal Kulkarni
837e6ad46Vishal Kulkarnistruct cudbg_entity_hdr {
847e6ad46Vishal Kulkarni	u32 entity_type;
857e6ad46Vishal Kulkarni	u32 start_offset;
867e6ad46Vishal Kulkarni	u32 size;
877e6ad46Vishal Kulkarni	int hdr_flags;
887e6ad46Vishal Kulkarni	u32 sys_warn;
897e6ad46Vishal Kulkarni	u32 sys_err;
907e6ad46Vishal Kulkarni	u8 num_pad;
917e6ad46Vishal Kulkarni	u8 flag;		/* bit 0 is used to indicate ext data */
927e6ad46Vishal Kulkarni	u8 reserved1[2];
937e6ad46Vishal Kulkarni	u32 next_ext_offset;	/* pointer to next extended entity meta data */
947e6ad46Vishal Kulkarni	u32 reserved[5];
957e6ad46Vishal Kulkarni};
967e6ad46Vishal Kulkarni
977e6ad46Vishal Kulkarnistruct cudbg_ver_hdr {
987e6ad46Vishal Kulkarni	u32 signature;
997e6ad46Vishal Kulkarni	u16 revision;
1007e6ad46Vishal Kulkarni	u16 size;
1017e6ad46Vishal Kulkarni};
1027e6ad46Vishal Kulkarni
1037e6ad46Vishal Kulkarnistruct cudbg_buffer {
1047e6ad46Vishal Kulkarni	u32 size;
1057e6ad46Vishal Kulkarni	u32 offset;
1067e6ad46Vishal Kulkarni	char *data;
1077e6ad46Vishal Kulkarni};
1087e6ad46Vishal Kulkarni
1097e6ad46Vishal Kulkarnistruct cudbg_error {
1107e6ad46Vishal Kulkarni	int sys_err;
1117e6ad46Vishal Kulkarni	int sys_warn;
1127e6ad46Vishal Kulkarni	int app_err;
1137e6ad46Vishal Kulkarni};
1147e6ad46Vishal Kulkarni
1157e6ad46Vishal Kulkarni#define HTONL_NIBBLE(data) ( \
1167e6ad46Vishal Kulkarni			    (((uint32_t)(data) >> 28) & 0x0000000F) | \
1177e6ad46Vishal Kulkarni			    (((uint32_t)(data) >> 20)  & 0x000000F0) | \
1187e6ad46Vishal Kulkarni			    (((uint32_t)(data) >> 12)  & 0x00000F00) | \
1197e6ad46Vishal Kulkarni			    (((uint32_t)(data) >> 4) & 0x0000F000) | \
1207e6ad46Vishal Kulkarni			    (((uint32_t)(data) << 4) & 0x000F0000) | \
1217e6ad46Vishal Kulkarni			    (((uint32_t)(data) << 12)  & 0x00F00000) | \
1227e6ad46Vishal Kulkarni			    (((uint32_t)(data) << 20)  & 0x0F000000) | \
1237e6ad46Vishal Kulkarni			    (((uint32_t)(data) << 28) & 0xF0000000))
1247e6ad46Vishal Kulkarni
1257e6ad46Vishal Kulkarni#define CDUMP_MAX_COMP_BUF_SIZE    ((64 * 1024) - 1)
1267e6ad46Vishal Kulkarni#define CUDBG_CHUNK_SIZE  ((CDUMP_MAX_COMP_BUF_SIZE/1024) * 1024)
1277e6ad46Vishal Kulkarni
1287e6ad46Vishal Kulkarni#define CUDBG_LEGACY_SIGNATURE 123
1297e6ad46Vishal Kulkarni#define CUDBG_SIGNATURE 67856866 /* CUDB in ascii */
1307e6ad46Vishal Kulkarni#define CUDBG_FL_SIGNATURE 0x4355464c /* CUFL in ascii */
1317e6ad46Vishal Kulkarni
1327e6ad46Vishal Kulkarni#define CUDBG_FL_MAJOR_VERSION	    1
1337e6ad46Vishal Kulkarni#define CUDBG_FL_MINOR_VERSION	    1
1347e6ad46Vishal Kulkarni#define CUDBG_FL_BUILD_VERSION	    0
1357e6ad46Vishal Kulkarni
1367e6ad46Vishal Kulkarniint write_compression_hdr(struct cudbg_buffer *, struct cudbg_buffer *);
1377e6ad46Vishal Kulkarniint compress_buff(struct cudbg_buffer *, struct cudbg_buffer *);
1387e6ad46Vishal Kulkarniint get_scratch_buff(struct cudbg_buffer *, u32, struct cudbg_buffer *);
1397e6ad46Vishal Kulkarnivoid release_scratch_buff(struct cudbg_buffer *, struct cudbg_buffer *);
1407e6ad46Vishal Kulkarniint decompress_buffer(struct cudbg_buffer *, struct cudbg_buffer *);
1417e6ad46Vishal Kulkarniint validate_buffer(struct cudbg_buffer *compressed_buffer);
1427e6ad46Vishal Kulkarniint decompress_buffer_wrapper(struct cudbg_buffer *pc_buff,
1437e6ad46Vishal Kulkarni			      struct cudbg_buffer *pdc_buff);
1447e6ad46Vishal Kulkarniint get_entity_rev(struct cudbg_ver_hdr *ver_hdr);
1457e6ad46Vishal Kulkarnivoid sort_t(void *base, int num, int size,
1467e6ad46Vishal Kulkarni	    int (*cmp_func)(const void *, const void *),
1477e6ad46Vishal Kulkarni	    void (*swap_func)(void *, void *, int size));
1487e6ad46Vishal Kulkarniint cudbg_read_flash(void *handle, void *data, u32 size, int data_flag);
1497e6ad46Vishal Kulkarniint cudbg_write_flash(void *handle, u64 timestamp, void *data,
1507e6ad46Vishal Kulkarni		      u32 start_offset, u32 start_hdr_offset,
1517e6ad46Vishal Kulkarni		      u32 cur_entity_size,
1527e6ad46Vishal Kulkarni		      u32 ext_size);
1537e6ad46Vishal Kulkarni#endif
154