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) 2019 by Chelsio Communications, Inc.
147e6ad46Vishal Kulkarni */
157e6ad46Vishal Kulkarni
167e6ad46Vishal Kulkarni#include <stdio.h>
177e6ad46Vishal Kulkarni#include <stddef.h>
187e6ad46Vishal Kulkarni#include <stdlib.h>
197e6ad46Vishal Kulkarni#include <string.h>
207e6ad46Vishal Kulkarni#include <ctype.h>
217e6ad46Vishal Kulkarni#include <fcntl.h>
227e6ad46Vishal Kulkarni#include <unistd.h>
237e6ad46Vishal Kulkarni#include <assert.h>
247e6ad46Vishal Kulkarni
257e6ad46Vishal Kulkarni#include "t4_regs.h"
267e6ad46Vishal Kulkarni#include "t4_chip_type.h"
277e6ad46Vishal Kulkarni#include "cudbg_view.h"
287e6ad46Vishal Kulkarni#include "osdep.h"
297e6ad46Vishal Kulkarni#include "t4fw_interface.h"
307e6ad46Vishal Kulkarni
317e6ad46Vishal Kulkarni#include "cudbg_view_entity.h"
327e6ad46Vishal Kulkarni#include "cudbg_entity.h"
337e6ad46Vishal Kulkarni#include "cudbg.h"
347e6ad46Vishal Kulkarni#include "cudbg_lib_common.h"
357e6ad46Vishal Kulkarni#include "fastlz.h"
367e6ad46Vishal Kulkarni
377e6ad46Vishal Kulkarniextern struct reg_info t6_sge_regs[];
387e6ad46Vishal Kulkarniextern struct reg_info t6_pcie_regs[];
397e6ad46Vishal Kulkarniextern struct reg_info t6_dbg_regs[];
407e6ad46Vishal Kulkarniextern struct reg_info t6_ma_regs[];
417e6ad46Vishal Kulkarniextern struct reg_info t6_cim_regs[];
427e6ad46Vishal Kulkarniextern struct reg_info t6_tp_regs[];
437e6ad46Vishal Kulkarniextern struct reg_info t6_ulp_tx_regs[];
447e6ad46Vishal Kulkarniextern struct reg_info t6_pm_rx_regs[];
457e6ad46Vishal Kulkarniextern struct reg_info t6_pm_tx_regs[];
467e6ad46Vishal Kulkarniextern struct reg_info t6_mps_regs[];
477e6ad46Vishal Kulkarniextern struct reg_info t6_cpl_switch_regs[];
487e6ad46Vishal Kulkarniextern struct reg_info t6_smb_regs[];
497e6ad46Vishal Kulkarniextern struct reg_info t6_i2cm_regs[];
507e6ad46Vishal Kulkarniextern struct reg_info t6_mi_regs[];
517e6ad46Vishal Kulkarniextern struct reg_info t6_uart_regs[];
527e6ad46Vishal Kulkarniextern struct reg_info t6_pmu_regs[];
537e6ad46Vishal Kulkarniextern struct reg_info t6_ulp_rx_regs[];
547e6ad46Vishal Kulkarniextern struct reg_info t6_sf_regs[];
557e6ad46Vishal Kulkarniextern struct reg_info t6_pl_regs[];
567e6ad46Vishal Kulkarniextern struct reg_info t6_le_regs[];
577e6ad46Vishal Kulkarniextern struct reg_info t6_ncsi_regs[];
587e6ad46Vishal Kulkarniextern struct reg_info t6_mac_regs[];
597e6ad46Vishal Kulkarniextern struct reg_info t6_mc_0_regs[];
607e6ad46Vishal Kulkarniextern struct reg_info t6_edc_t60_regs[];
617e6ad46Vishal Kulkarniextern struct reg_info t6_edc_t61_regs[];
627e6ad46Vishal Kulkarniextern struct reg_info t6_hma_t6_regs[];
637e6ad46Vishal Kulkarni
647e6ad46Vishal Kulkarniextern struct reg_info t5_sge_regs[];
657e6ad46Vishal Kulkarniextern struct reg_info t5_pcie_regs[];
667e6ad46Vishal Kulkarniextern struct reg_info t5_dbg_regs[];
677e6ad46Vishal Kulkarniextern struct reg_info t5_ma_regs[];
687e6ad46Vishal Kulkarniextern struct reg_info t5_cim_regs[];
697e6ad46Vishal Kulkarniextern struct reg_info t5_tp_regs[];
707e6ad46Vishal Kulkarniextern struct reg_info t5_ulp_tx_regs[];
717e6ad46Vishal Kulkarniextern struct reg_info t5_pm_rx_regs[];
727e6ad46Vishal Kulkarniextern struct reg_info t5_pm_tx_regs[];
737e6ad46Vishal Kulkarniextern struct reg_info t5_mps_regs[];
747e6ad46Vishal Kulkarniextern struct reg_info t5_cpl_switch_regs[];
757e6ad46Vishal Kulkarniextern struct reg_info t5_smb_regs[];
767e6ad46Vishal Kulkarniextern struct reg_info t5_i2cm_regs[];
777e6ad46Vishal Kulkarniextern struct reg_info t5_mi_regs[];
787e6ad46Vishal Kulkarniextern struct reg_info t5_uart_regs[];
797e6ad46Vishal Kulkarniextern struct reg_info t5_pmu_regs[];
807e6ad46Vishal Kulkarniextern struct reg_info t5_ulp_rx_regs[];
817e6ad46Vishal Kulkarniextern struct reg_info t5_sf_regs[];
827e6ad46Vishal Kulkarniextern struct reg_info t5_pl_regs[];
837e6ad46Vishal Kulkarniextern struct reg_info t5_le_regs[];
847e6ad46Vishal Kulkarniextern struct reg_info t5_ncsi_regs[];
857e6ad46Vishal Kulkarniextern struct reg_info t5_mac_regs[];
867e6ad46Vishal Kulkarniextern struct reg_info t5_mc_0_regs[];
877e6ad46Vishal Kulkarniextern struct reg_info t5_mc_1_regs[];
887e6ad46Vishal Kulkarniextern struct reg_info t5_edc_t50_regs[];
897e6ad46Vishal Kulkarniextern struct reg_info t5_edc_t51_regs[];
907e6ad46Vishal Kulkarniextern struct reg_info t5_hma_t5_regs[];
917e6ad46Vishal Kulkarni
927e6ad46Vishal Kulkarni#include "reg_defs_t5.c"
937e6ad46Vishal Kulkarni#include "reg_defs_t6.c"
947e6ad46Vishal Kulkarni
957e6ad46Vishal Kulkarni#include <time.h>
967e6ad46Vishal Kulkarni#include <stdarg.h>
977e6ad46Vishal Kulkarni
987e6ad46Vishal Kulkarniint is_t5(enum chip_type chip)
997e6ad46Vishal Kulkarni{
1007e6ad46Vishal Kulkarni	return (CHELSIO_CHIP_VERSION(chip) == CHELSIO_T5);
1017e6ad46Vishal Kulkarni}
1027e6ad46Vishal Kulkarni
1037e6ad46Vishal Kulkarniint is_t6(enum chip_type chip)
1047e6ad46Vishal Kulkarni{
1057e6ad46Vishal Kulkarni	return (CHELSIO_CHIP_VERSION(chip) == CHELSIO_T6);
1067e6ad46Vishal Kulkarni}
1077e6ad46Vishal Kulkarni
1087e6ad46Vishal Kulkarnienum {                                 /* adapter flags */
1097e6ad46Vishal Kulkarni	FULL_INIT_DONE     = (1 << 0),
1107e6ad46Vishal Kulkarni	USING_MSI          = (1 << 1),
1117e6ad46Vishal Kulkarni	USING_MSIX         = (1 << 2),
1127e6ad46Vishal Kulkarni	QUEUES_BOUND       = (1 << 3),
1137e6ad46Vishal Kulkarni	FW_OK              = (1 << 4),
1147e6ad46Vishal Kulkarni	RSS_TNLALLLOOKUP   = (1 << 5),
1157e6ad46Vishal Kulkarni	USING_SOFT_PARAMS  = (1 << 6),
1167e6ad46Vishal Kulkarni	MASTER_PF          = (1 << 7),
1177e6ad46Vishal Kulkarni	BYPASS_DROP        = (1 << 8),
1187e6ad46Vishal Kulkarni	FW_OFLD_CONN       = (1 << 9),
1197e6ad46Vishal Kulkarni};
1207e6ad46Vishal Kulkarni
1217e6ad46Vishal Kulkarnistatic struct ver_cs {
1227e6ad46Vishal Kulkarni	int major;
1237e6ad46Vishal Kulkarni	int minor;
1247e6ad46Vishal Kulkarni	int changeset;
1257e6ad46Vishal Kulkarni} ver_to_cs[] = {
1267e6ad46Vishal Kulkarni	{1, 9, 12852},
1277e6ad46Vishal Kulkarni	{1, 10, 13182},
1287e6ad46Vishal Kulkarni	{1, 11, 13257},
1297e6ad46Vishal Kulkarni	{1, 12, 13495},
1307e6ad46Vishal Kulkarni	{1, 13, 13905},
1317e6ad46Vishal Kulkarni	{1, 14, 13969},
1327e6ad46Vishal Kulkarni};
1337e6ad46Vishal Kulkarni
1347e6ad46Vishal Kulkarnistatic bool flash_info_banner = true;
1357e6ad46Vishal Kulkarni
1367e6ad46Vishal Kulkarni#include "cudbg_view_compat.c"
1377e6ad46Vishal Kulkarni
1387e6ad46Vishal Kulkarniint
1397e6ad46Vishal Kulkarnicudbg_sge_ctxt_check_valid(u32 *buf, int type)
1407e6ad46Vishal Kulkarni{
1417e6ad46Vishal Kulkarni	int index, bit, bit_pos = 0;
1427e6ad46Vishal Kulkarni
1437e6ad46Vishal Kulkarni	switch (type) {
1447e6ad46Vishal Kulkarni		case CTXT_EGRESS:
1457e6ad46Vishal Kulkarni			bit_pos = 176;
1467e6ad46Vishal Kulkarni			break;
1477e6ad46Vishal Kulkarni		case CTXT_INGRESS:
1487e6ad46Vishal Kulkarni			bit_pos = 141;
1497e6ad46Vishal Kulkarni			break;
1507e6ad46Vishal Kulkarni		case CTXT_FLM:
1517e6ad46Vishal Kulkarni			bit_pos = 89;
1527e6ad46Vishal Kulkarni			break;
1537e6ad46Vishal Kulkarni	}
1547e6ad46Vishal Kulkarni	index = bit_pos / 32;
1557e6ad46Vishal Kulkarni	bit =  bit_pos % 32;
1567e6ad46Vishal Kulkarni	return buf[index] & (1U << bit);
1577e6ad46Vishal Kulkarni}
1587e6ad46Vishal Kulkarni
1597e6ad46Vishal Kulkarniint
1607e6ad46Vishal Kulkarnicudbg_view_decompress_buff(char *pbuf,
1617e6ad46Vishal Kulkarni			   struct cudbg_entity_hdr *entity_hdr,
1627e6ad46Vishal Kulkarni			   struct cudbg_buffer *c_buff,
1637e6ad46Vishal Kulkarni			   struct cudbg_buffer *dc_buff)
1647e6ad46Vishal Kulkarni{
1657e6ad46Vishal Kulkarni	int rc = 0;
1667e6ad46Vishal Kulkarni
1677e6ad46Vishal Kulkarni	c_buff->data = pbuf + entity_hdr->start_offset;
1687e6ad46Vishal Kulkarni	/* Remove padding bytes, if any */
1697e6ad46Vishal Kulkarni	if (entity_hdr->num_pad)
1707e6ad46Vishal Kulkarni		c_buff->size = entity_hdr->size - entity_hdr->num_pad;
1717e6ad46Vishal Kulkarni	else
1727e6ad46Vishal Kulkarni		c_buff->size = entity_hdr->size;
1737e6ad46Vishal Kulkarni	c_buff->offset = 0;
1747e6ad46Vishal Kulkarni	memset(dc_buff, 0, sizeof(struct cudbg_buffer));
1757e6ad46Vishal Kulkarni
1767e6ad46Vishal Kulkarni	rc = validate_buffer(c_buff);
1777e6ad46Vishal Kulkarni	if (rc)
1787e6ad46Vishal Kulkarni		return rc;
1797e6ad46Vishal Kulkarni
1807e6ad46Vishal Kulkarni	rc = decompress_buffer_wrapper(c_buff, dc_buff);
1817e6ad46Vishal Kulkarni	if (rc) {
1827e6ad46Vishal Kulkarni		free(dc_buff->data);
1837e6ad46Vishal Kulkarni		return rc;
1847e6ad46Vishal Kulkarni	}
1857e6ad46Vishal Kulkarni	return rc;
1867e6ad46Vishal Kulkarni}
1877e6ad46Vishal Kulkarni
1887e6ad46Vishal Kulkarniint
1897e6ad46Vishal Kulkarniget_entity_rev(struct cudbg_ver_hdr *ver_hdr)
1907e6ad46Vishal Kulkarni{
1917e6ad46Vishal Kulkarni	if (ver_hdr->signature == CUDBG_ENTITY_SIGNATURE)
1927e6ad46Vishal Kulkarni		return ver_hdr->revision;
1937e6ad46Vishal Kulkarni	return 0;
1947e6ad46Vishal Kulkarni}
1957e6ad46Vishal Kulkarni
1967e6ad46Vishal Kulkarni/* Find Mercurial sw repo changeset number
1977e6ad46Vishal Kulkarni * where major or minor number set to given number
1987e6ad46Vishal Kulkarni * */
1997e6ad46Vishal Kulkarniint
2007e6ad46Vishal Kulkarnicudbg_find_changeset(int major, int minor)
2017e6ad46Vishal Kulkarni{
2027e6ad46Vishal Kulkarni	int i;
2037e6ad46Vishal Kulkarni
2047e6ad46Vishal Kulkarni	for (i = 0; i < sizeof(ver_to_cs)/sizeof(struct ver_cs); i++) {
2057e6ad46Vishal Kulkarni		if (ver_to_cs[i].major == major &&
2067e6ad46Vishal Kulkarni		    ver_to_cs[i].minor == minor)
2077e6ad46Vishal Kulkarni			return ver_to_cs[i].changeset;
2087e6ad46Vishal Kulkarni	}
2097e6ad46Vishal Kulkarni
2107e6ad46Vishal Kulkarni	return -1;
2117e6ad46Vishal Kulkarni}
2127e6ad46Vishal Kulkarni
2137e6ad46Vishal Kulkarni/* Format a value in a unit that differs from the
2147e6ad46Vishal Kulkarni * value's native unit by the
2157e6ad46Vishal Kulkarni * given factor.
2167e6ad46Vishal Kulkarni */
2177e6ad46Vishal Kulkarnistatic void
2187e6ad46Vishal Kulkarniunit_conv(char *buf, size_t len, unsigned int val,
2197e6ad46Vishal Kulkarni	  unsigned int factor)
2207e6ad46Vishal Kulkarni{
2217e6ad46Vishal Kulkarni	unsigned int rem = val % factor;
2227e6ad46Vishal Kulkarni
2237e6ad46Vishal Kulkarni	if (rem == 0)
2247e6ad46Vishal Kulkarni		(void) snprintf(buf, len, "%u", val / factor);
2257e6ad46Vishal Kulkarni	else {
2267e6ad46Vishal Kulkarni		while (rem % 10 == 0)
2277e6ad46Vishal Kulkarni			rem /= 10;
2287e6ad46Vishal Kulkarni		(void) snprintf(buf, len, "%u.%u", val / factor, rem);
2297e6ad46Vishal Kulkarni	}
2307e6ad46Vishal Kulkarni}
2317e6ad46Vishal Kulkarni
2327e6ad46Vishal Kulkarniint
2337e6ad46Vishal Kulkarnivalidate_next_rec_offset(void *pinbuf, u32 inbuf_size, u32
2347e6ad46Vishal Kulkarni			 next_rec_offset)
2357e6ad46Vishal Kulkarni{
2367e6ad46Vishal Kulkarni	struct cudbg_hdr *cudbg_hdr;
2377e6ad46Vishal Kulkarni
2387e6ad46Vishal Kulkarni	if (inbuf_size <= next_rec_offset)
2397e6ad46Vishal Kulkarni		return 0;
2407e6ad46Vishal Kulkarni
2417e6ad46Vishal Kulkarni	cudbg_hdr = (struct cudbg_hdr *)((char *)pinbuf + next_rec_offset);
2427e6ad46Vishal Kulkarni	if ((cudbg_hdr->signature != CUDBG_SIGNATURE) &&
2437e6ad46Vishal Kulkarni	    (cudbg_hdr->signature != CUDBG_LEGACY_SIGNATURE))
2447e6ad46Vishal Kulkarni		return 0; /* no next rec */
2457e6ad46Vishal Kulkarni
2467e6ad46Vishal Kulkarni	return next_rec_offset;
2477e6ad46Vishal Kulkarni}
2487e6ad46Vishal Kulkarni
2497e6ad46Vishal Kulkarniint
2507e6ad46Vishal Kulkarniview_ext_entity(char *pinbuf, struct cudbg_entity_hdr *ent_hdr,
2517e6ad46Vishal Kulkarni		struct cudbg_buffer *cudbg_poutbuf,
2527e6ad46Vishal Kulkarni		enum chip_type chip)
2537e6ad46Vishal Kulkarni{
2547e6ad46Vishal Kulkarni	struct cudbg_entity_hdr *entity_hdr = NULL;
2557e6ad46Vishal Kulkarni	u32 size, total_size = 0;
2567e6ad46Vishal Kulkarni	u32 next_ext_offset = 0;
2577e6ad46Vishal Kulkarni	u32 entity_type;
2587e6ad46Vishal Kulkarni	int rc = 0;
2597e6ad46Vishal Kulkarni
2607e6ad46Vishal Kulkarni	entity_hdr = (struct cudbg_entity_hdr *)
2617e6ad46Vishal Kulkarni		     (pinbuf + ent_hdr->start_offset);
2627e6ad46Vishal Kulkarni	/* Remove padding bytes, if any */
2637e6ad46Vishal Kulkarni	size = ent_hdr->num_pad ? ent_hdr->size - ent_hdr->num_pad :
2647e6ad46Vishal Kulkarni				  ent_hdr->size;
2657e6ad46Vishal Kulkarni	while ((entity_hdr->flag & CUDBG_EXT_DATA_VALID)
2667e6ad46Vishal Kulkarni		&& (total_size < size)) {
2677e6ad46Vishal Kulkarni		entity_type = entity_hdr->entity_type;
2687e6ad46Vishal Kulkarni		if (entity_hdr->sys_warn)
2697e6ad46Vishal Kulkarni			printf("Entity warning: Type %s , %d\n",
2707e6ad46Vishal Kulkarni			       entity_list[entity_type].name,
2717e6ad46Vishal Kulkarni			       entity_hdr->sys_warn);
2727e6ad46Vishal Kulkarni
2737e6ad46Vishal Kulkarni		if (entity_hdr->hdr_flags) {
2747e6ad46Vishal Kulkarni			printf("Entity error: Type %s, %s\n",
2757e6ad46Vishal Kulkarni			       entity_list[entity_type].name,
2767e6ad46Vishal Kulkarni			       err_msg[-entity_hdr->hdr_flags]);
2777e6ad46Vishal Kulkarni			if (entity_hdr->sys_err)
2787e6ad46Vishal Kulkarni				printf("System error  %d\n",
2797e6ad46Vishal Kulkarni				       entity_hdr->sys_err);
2807e6ad46Vishal Kulkarni
2817e6ad46Vishal Kulkarni			next_ext_offset = entity_hdr->next_ext_offset;
2827e6ad46Vishal Kulkarni			entity_hdr = (struct cudbg_entity_hdr *)
2837e6ad46Vishal Kulkarni				     (pinbuf + ent_hdr->start_offset +
2847e6ad46Vishal Kulkarni				      next_ext_offset);
2857e6ad46Vishal Kulkarni			continue;
2867e6ad46Vishal Kulkarni		}
2877e6ad46Vishal Kulkarni		if (entity_hdr->size > 0) {
2887e6ad46Vishal Kulkarni			total_size += entity_hdr->size +
2897e6ad46Vishal Kulkarni					sizeof(struct cudbg_entity_hdr);
2907e6ad46Vishal Kulkarni
2917e6ad46Vishal Kulkarni			rc = view_entity[entity_type - 1]
2927e6ad46Vishal Kulkarni				(pinbuf + ent_hdr->start_offset,
2937e6ad46Vishal Kulkarni				 entity_hdr,
2947e6ad46Vishal Kulkarni				 cudbg_poutbuf,
2957e6ad46Vishal Kulkarni				 chip);
2967e6ad46Vishal Kulkarni			if (rc < 0)
2977e6ad46Vishal Kulkarni				goto out;
2987e6ad46Vishal Kulkarni		}
2997e6ad46Vishal Kulkarni		next_ext_offset = entity_hdr->next_ext_offset;
3007e6ad46Vishal Kulkarni		entity_hdr = (struct cudbg_entity_hdr *)
3017e6ad46Vishal Kulkarni			     (pinbuf + ent_hdr->start_offset + next_ext_offset);
3027e6ad46Vishal Kulkarni	}
3037e6ad46Vishal Kulkarni
3047e6ad46Vishal Kulkarni	if (total_size != size)
3057e6ad46Vishal Kulkarni		printf("Entity warning: Extended entity size mismatch\n");
3067e6ad46Vishal Kulkarni
3077e6ad46Vishal Kulkarniout:
3087e6ad46Vishal Kulkarni	return rc;
3097e6ad46Vishal Kulkarni}
3107e6ad46Vishal Kulkarni
3117e6ad46Vishal Kulkarnistatic void
3127e6ad46Vishal Kulkarnicudbg_print_cudbg_header(struct cudbg_hdr *hdr)
3137e6ad46Vishal Kulkarni{
3147e6ad46Vishal Kulkarni	printf("\n/***************Header Information***************/\n");
3157e6ad46Vishal Kulkarni	printf("Library Version: %u.%u\n", hdr->major_ver, hdr->minor_ver);
3167e6ad46Vishal Kulkarni	printf("Compressed with: ");
3177e6ad46Vishal Kulkarni	printf("Chip Version: ");
3187e6ad46Vishal Kulkarni	switch (CHELSIO_CHIP_VERSION(hdr->chip_ver)) {
3197e6ad46Vishal Kulkarni	case CHELSIO_T4:
3207e6ad46Vishal Kulkarni		printf("T4 rev: %u\n", CHELSIO_CHIP_RELEASE(hdr->chip_ver));
3217e6ad46Vishal Kulkarni		break;
3227e6ad46Vishal Kulkarni	case CHELSIO_T5:
3237e6ad46Vishal Kulkarni		printf("T5 rev: %u\n", CHELSIO_CHIP_RELEASE(hdr->chip_ver));
3247e6ad46Vishal Kulkarni		break;
3257e6ad46Vishal Kulkarni	case CHELSIO_T6:
3267e6ad46Vishal Kulkarni		printf("T6 rev: %u\n", CHELSIO_CHIP_RELEASE(hdr->chip_ver));
3277e6ad46Vishal Kulkarni		break;
3287e6ad46Vishal Kulkarni	default:
3297e6ad46Vishal Kulkarni		printf("%u (unknown)\n", hdr->chip_ver);
3307e6ad46Vishal Kulkarni		break;
3317e6ad46Vishal Kulkarni	}
3327e6ad46Vishal Kulkarni	printf("/************************************************/\n\n");
3337e6ad46Vishal Kulkarni}
3347e6ad46Vishal Kulkarni
3357e6ad46Vishal Kulkarnivoid
3367e6ad46Vishal Kulkarnicudbg_print_flash_header(void *pinbuf)
3377e6ad46Vishal Kulkarni{
3387e6ad46Vishal Kulkarni	struct cudbg_flash_hdr *fl_hdr = (struct cudbg_flash_hdr *)pinbuf;
3397e6ad46Vishal Kulkarni
3407e6ad46Vishal Kulkarni	if (fl_hdr->signature == CUDBG_FL_SIGNATURE && flash_info_banner) {
3417e6ad46Vishal Kulkarni		printf("\n/***************Flash Header information***************/\n");
3427e6ad46Vishal Kulkarni		printf("Flash signature: %c%c%c%c\n",
3437e6ad46Vishal Kulkarni		       (fl_hdr->signature  >> 24) & 0xFF,
3447e6ad46Vishal Kulkarni		       (fl_hdr->signature  >> 16) & 0xFF,
3457e6ad46Vishal Kulkarni		       (fl_hdr->signature  >> 8) & 0xFF,
3467e6ad46Vishal Kulkarni		       fl_hdr->signature & 0xFF);
3477e6ad46Vishal Kulkarni
3487e6ad46Vishal Kulkarni		printf("Flash payload timestamp (GMT): %s",
3497e6ad46Vishal Kulkarni		       asctime(gmtime((time_t *)&fl_hdr->timestamp)));
3507e6ad46Vishal Kulkarni		printf("Flash payload size: %u bytes\n", fl_hdr->data_len);
3517e6ad46Vishal Kulkarni		printf("/******************************************************/\n");
3527e6ad46Vishal Kulkarni		flash_info_banner = false;
3537e6ad46Vishal Kulkarni	}
3547e6ad46Vishal Kulkarni}
3557e6ad46Vishal Kulkarni
3567e6ad46Vishal Kulkarniint
3577e6ad46Vishal Kulkarnicudbg_view(void *handle, void *pinbuf, u32 inbuf_size,
3587e6ad46Vishal Kulkarni	   void *poutbuf, s64 *poutbuf_size)
3597e6ad46Vishal Kulkarni{
3607e6ad46Vishal Kulkarni
3617e6ad46Vishal Kulkarni	struct cudbg_buffer cudbg_poutbuf = {0};
3627e6ad46Vishal Kulkarni	struct cudbg_entity_hdr *entity_hdr;
3637e6ad46Vishal Kulkarni	u32 info, offset, max_entities, i;
3647e6ad46Vishal Kulkarni	struct cudbg_hdr *tmp_hdr;
3657e6ad46Vishal Kulkarni	u32 next_rec_offset = 0;
3667e6ad46Vishal Kulkarni	int index, bit, all;
3677e6ad46Vishal Kulkarni	int rc = 0, cs;
3687e6ad46Vishal Kulkarni	u8 *dbg_bitmap;
3697e6ad46Vishal Kulkarni	int count = 0;
3707e6ad46Vishal Kulkarni
3717e6ad46Vishal Kulkarni	dbg_bitmap = ((struct cudbg_private *)handle)->dbg_init.dbg_bitmap;
3727e6ad46Vishal Kulkarni	info = ((struct cudbg_private *)handle)->dbg_init.info;
3737e6ad46Vishal Kulkarni
3747e6ad46Vishal Kulkarni	if (inbuf_size < (sizeof(struct cudbg_entity_hdr) +
3757e6ad46Vishal Kulkarni			  sizeof(struct cudbg_hdr))) {
3767e6ad46Vishal Kulkarni		printf("\n\tInvalid cudbg dump file\n");
3777e6ad46Vishal Kulkarni		return CUDBG_STATUS_NO_SIGNATURE;
3787e6ad46Vishal Kulkarni	}
3797e6ad46Vishal Kulkarni
3807e6ad46Vishal Kulkarni	tmp_hdr  = (struct cudbg_hdr *)pinbuf;
3817e6ad46Vishal Kulkarni	if ((tmp_hdr->signature != CUDBG_SIGNATURE) &&
3827e6ad46Vishal Kulkarni	    (tmp_hdr->signature != CUDBG_LEGACY_SIGNATURE)) {
3837e6ad46Vishal Kulkarni		printf("\n\tInvalid cudbg dump file\n");
3847e6ad46Vishal Kulkarni		return CUDBG_STATUS_NO_SIGNATURE;
3857e6ad46Vishal Kulkarni	}
3867e6ad46Vishal Kulkarni
3877e6ad46Vishal Kulkarni	if ((tmp_hdr->major_ver != CUDBG_MAJOR_VERSION) ||
3887e6ad46Vishal Kulkarni	    (tmp_hdr->minor_ver != CUDBG_MINOR_VERSION)) {
3897e6ad46Vishal Kulkarni		printf("\n\tMeta data version mismatch\n");
3907e6ad46Vishal Kulkarni		printf("\tMeta data version expected %d.%d\n",
3917e6ad46Vishal Kulkarni		       CUDBG_MAJOR_VERSION, CUDBG_MINOR_VERSION);
3927e6ad46Vishal Kulkarni		printf("\tMeta data version in dump %d.%d\n",
3937e6ad46Vishal Kulkarni		       tmp_hdr->major_ver, tmp_hdr->minor_ver);
3947e6ad46Vishal Kulkarni
3957e6ad46Vishal Kulkarni		cs = cudbg_find_changeset(tmp_hdr->major_ver,
3967e6ad46Vishal Kulkarni					  tmp_hdr->minor_ver);
3977e6ad46Vishal Kulkarni		if (cs != -1) {
3987e6ad46Vishal Kulkarni			printf("\n\tPlease use changeset %d in sw Mercurial "\
3997e6ad46Vishal Kulkarni			       "repo to build cudbg_app with version %d.%d\n",
4007e6ad46Vishal Kulkarni			       cs, tmp_hdr->major_ver, tmp_hdr->minor_ver);
4017e6ad46Vishal Kulkarni
4027e6ad46Vishal Kulkarni			printf("\n\tOr\n\n\tUse precompiled cudbg_app binary for RHEL 5.x from "\
4037e6ad46Vishal Kulkarni			       "vnc52:/home/surendra/vnc52/"\
4047e6ad46Vishal Kulkarni			       "cudbg_app/cudbg_app_<version>\"\n\n");
4057e6ad46Vishal Kulkarni
4067e6ad46Vishal Kulkarni
4077e6ad46Vishal Kulkarni		}
4087e6ad46Vishal Kulkarni		return CUDBG_METADATA_VERSION_MISMATCH;
4097e6ad46Vishal Kulkarni	}
4107e6ad46Vishal Kulkarni
4117e6ad46Vishal Kulkarni	if (info)
4127e6ad46Vishal Kulkarni		cudbg_print_cudbg_header(tmp_hdr);
4137e6ad46Vishal Kulkarni
4147e6ad46Vishal Kulkarni	next_rec_offset += tmp_hdr->data_len;
4157e6ad46Vishal Kulkarni	offset = tmp_hdr->hdr_len;
4167e6ad46Vishal Kulkarni	all = dbg_bitmap[0] & (1 << CUDBG_ALL);
4177e6ad46Vishal Kulkarni	max_entities = min(tmp_hdr->max_entities, CUDBG_MAX_ENTITY);
4187e6ad46Vishal Kulkarni
4197e6ad46Vishal Kulkarni	for (i = 1; i < max_entities; i++) {
4207e6ad46Vishal Kulkarni		index = i / 8;
4217e6ad46Vishal Kulkarni		bit = i % 8;
4227e6ad46Vishal Kulkarni
4237e6ad46Vishal Kulkarni		if (all || (dbg_bitmap[index] & (1 << bit))) {
4247e6ad46Vishal Kulkarni			entity_hdr =
4257e6ad46Vishal Kulkarni				(struct cudbg_entity_hdr *)((char *)pinbuf + offset);
4267e6ad46Vishal Kulkarni
4277e6ad46Vishal Kulkarni			if (entity_hdr->sys_warn)
4287e6ad46Vishal Kulkarni				printf("Entity warning: Type %s , %d\n",
4297e6ad46Vishal Kulkarni				       entity_list[i].name,
4307e6ad46Vishal Kulkarni				       entity_hdr->sys_warn);
4317e6ad46Vishal Kulkarni
4327e6ad46Vishal Kulkarni			if (entity_hdr->hdr_flags) {
4337e6ad46Vishal Kulkarni				offset += sizeof(struct cudbg_entity_hdr);
4347e6ad46Vishal Kulkarni				printf("Entity error: Type %s, %s\n",
4357e6ad46Vishal Kulkarni				       entity_list[i].name,
4367e6ad46Vishal Kulkarni				       err_msg[-entity_hdr->hdr_flags]);
4377e6ad46Vishal Kulkarni				if (entity_hdr->sys_err)
4387e6ad46Vishal Kulkarni					printf("System error  %d\n",
4397e6ad46Vishal Kulkarni					       entity_hdr->sys_err);
4407e6ad46Vishal Kulkarni
4417e6ad46Vishal Kulkarni				if (poutbuf)
4427e6ad46Vishal Kulkarni					*poutbuf_size = 0;
4437e6ad46Vishal Kulkarni
4447e6ad46Vishal Kulkarni				continue;
4457e6ad46Vishal Kulkarni			}
4467e6ad46Vishal Kulkarni			memset(&cudbg_poutbuf, 0, sizeof(cudbg_poutbuf));
4477e6ad46Vishal Kulkarni			if (entity_hdr->size > 0) {
4487e6ad46Vishal Kulkarni				if (poutbuf) {
4497e6ad46Vishal Kulkarni					cudbg_poutbuf.data = poutbuf;
4507e6ad46Vishal Kulkarni					/* poutbuf_size value should not be
4517e6ad46Vishal Kulkarni 					 * more than 32 bit value
4527e6ad46Vishal Kulkarni 					 */
4537e6ad46Vishal Kulkarni					assert(!((*poutbuf_size) >> 32));
4547e6ad46Vishal Kulkarni					cudbg_poutbuf.size = (u32)*poutbuf_size;
4557e6ad46Vishal Kulkarni					cudbg_poutbuf.offset = 0;
4567e6ad46Vishal Kulkarni				}
4577e6ad46Vishal Kulkarni
4587e6ad46Vishal Kulkarni				if (info)
4597e6ad46Vishal Kulkarni					printf("%-20s compressed size %u\n",
4607e6ad46Vishal Kulkarni					       entity_list[i].name,
4617e6ad46Vishal Kulkarni					       entity_hdr->size);
4627e6ad46Vishal Kulkarni				else {
4637e6ad46Vishal Kulkarni					if (entity_hdr->entity_type !=
4647e6ad46Vishal Kulkarni					    CUDBG_EXT_ENTITY)
4657e6ad46Vishal Kulkarni						printf("%s() dbg entity : %s\n",
4667e6ad46Vishal Kulkarni						       __func__,
4677e6ad46Vishal Kulkarni						       entity_list[i].name);
4687e6ad46Vishal Kulkarni
4697e6ad46Vishal Kulkarni					rc = view_entity[i - 1]
4707e6ad46Vishal Kulkarni						((char *)pinbuf,
4717e6ad46Vishal Kulkarni						 entity_hdr,
4727e6ad46Vishal Kulkarni						 &cudbg_poutbuf,
4737e6ad46Vishal Kulkarni						 tmp_hdr->chip_ver);
4747e6ad46Vishal Kulkarni
4757e6ad46Vishal Kulkarni					count++;
4767e6ad46Vishal Kulkarni				}
4777e6ad46Vishal Kulkarni			} else if (!all && i !=
4787e6ad46Vishal Kulkarni				   CUDBG_EXT_ENTITY) {
4797e6ad46Vishal Kulkarni				printf("%s() dbg entity : %s\n",
4807e6ad46Vishal Kulkarni				       __func__, entity_list[i].name);
4817e6ad46Vishal Kulkarni				printf("\t%s not available\n",
4827e6ad46Vishal Kulkarni				       entity_list[i].name);
4837e6ad46Vishal Kulkarni			}
4847e6ad46Vishal Kulkarni			if (rc < 0)
4857e6ad46Vishal Kulkarni				goto out;
4867e6ad46Vishal Kulkarni		}
4877e6ad46Vishal Kulkarni		offset += sizeof(struct cudbg_entity_hdr);
4887e6ad46Vishal Kulkarni	}
4897e6ad46Vishal Kulkarni
4907e6ad46Vishal Kulkarni	/* if max_entities in dump is less than current CUDBG_MAX_ENTITY
4917e6ad46Vishal Kulkarni	 * it means entities after tmp_hdr->max_entities does not exist
4927e6ad46Vishal Kulkarni	 * in that dump
4937e6ad46Vishal Kulkarni	 */
4947e6ad46Vishal Kulkarni	if (tmp_hdr->max_entities < CUDBG_MAX_ENTITY) {
4957e6ad46Vishal Kulkarni		for (i = tmp_hdr->max_entities; i < CUDBG_MAX_ENTITY; i++) {
4967e6ad46Vishal Kulkarni			index = i / 8;
4977e6ad46Vishal Kulkarni			bit = i % 8;
4987e6ad46Vishal Kulkarni
4997e6ad46Vishal Kulkarni			if (all || (dbg_bitmap[index] & (1 << bit))) {
5007e6ad46Vishal Kulkarni				printf("%s() dbg entity : %s\n",
5017e6ad46Vishal Kulkarni				       __func__, entity_list[i].name);
5027e6ad46Vishal Kulkarni				printf("\t%s does not Exist\n",
5037e6ad46Vishal Kulkarni				       entity_list[i].name);
5047e6ad46Vishal Kulkarni			}
5057e6ad46Vishal Kulkarni		}
5067e6ad46Vishal Kulkarni	}
5077e6ad46Vishal Kulkarni	if (poutbuf) {
5087e6ad46Vishal Kulkarni		if (!count)
5097e6ad46Vishal Kulkarni			*poutbuf_size = 0;
5107e6ad46Vishal Kulkarni		else
5117e6ad46Vishal Kulkarni			*poutbuf_size = cudbg_poutbuf.size;
5127e6ad46Vishal Kulkarni	}
5137e6ad46Vishal Kulkarni
5147e6ad46Vishal Kulkarni	return validate_next_rec_offset(pinbuf, inbuf_size, next_rec_offset);
5157e6ad46Vishal Kulkarni
5167e6ad46Vishal Kulkarniout:
5177e6ad46Vishal Kulkarni	if (poutbuf)
5187e6ad46Vishal Kulkarni		*poutbuf_size = cudbg_poutbuf.size;
5197e6ad46Vishal Kulkarni	return rc;
5207e6ad46Vishal Kulkarni}
5217e6ad46Vishal Kulkarni
5227e6ad46Vishal Kulkarniint
5237e6ad46Vishal Kulkarniview_cim_q(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
5247e6ad46Vishal Kulkarni	   struct cudbg_buffer *cudbg_poutbuf,
5257e6ad46Vishal Kulkarni	   enum chip_type chip)
5267e6ad46Vishal Kulkarni{
5277e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
5287e6ad46Vishal Kulkarni	u32 i, *pdata = NULL;
5297e6ad46Vishal Kulkarni	int rc;
5307e6ad46Vishal Kulkarni
5317e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
5327e6ad46Vishal Kulkarni	if (rc)
5337e6ad46Vishal Kulkarni		return rc;
5347e6ad46Vishal Kulkarni
5357e6ad46Vishal Kulkarni	pdata = (u32 *)dc_buff.data;
5367e6ad46Vishal Kulkarni	for (i = 0; i < dc_buff.offset / 4; i += 4)
5377e6ad46Vishal Kulkarni		printf("%#06x: %08x %08x %08x "\
5387e6ad46Vishal Kulkarni			     "%08x\n", i * 4,
5397e6ad46Vishal Kulkarni			     pdata[i + 0], pdata[i + 1],
5407e6ad46Vishal Kulkarni			     pdata[i + 2], pdata[i + 3]);
5417e6ad46Vishal Kulkarni
5427e6ad46Vishal Kulkarni	return rc;
5437e6ad46Vishal Kulkarni}
5447e6ad46Vishal Kulkarni
5457e6ad46Vishal Kulkarnistatic int
5467e6ad46Vishal Kulkarniview_cim_la_t6(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
5477e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf)
5487e6ad46Vishal Kulkarni{
5497e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
5507e6ad46Vishal Kulkarni	u32 i, *p, cfg, dc_size;
5517e6ad46Vishal Kulkarni	int rc;
5527e6ad46Vishal Kulkarni
5537e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
5547e6ad46Vishal Kulkarni	if (rc)
5557e6ad46Vishal Kulkarni		return rc;
5567e6ad46Vishal Kulkarni
5577e6ad46Vishal Kulkarni	dc_size = dc_buff.offset;
5587e6ad46Vishal Kulkarni	p = (u32 *)((char *)dc_buff.data + sizeof(cfg));
5597e6ad46Vishal Kulkarni	cfg = *((u32 *)dc_buff.data);
5607e6ad46Vishal Kulkarni	dc_size -= sizeof(cfg);
5617e6ad46Vishal Kulkarni
5627e6ad46Vishal Kulkarni	if (cfg & F_UPDBGLACAPTPCONLY) {
5637e6ad46Vishal Kulkarni		printf("Status   Inst    Data      "\
5647e6ad46Vishal Kulkarni			     "PC\r\n");
5657e6ad46Vishal Kulkarni
5667e6ad46Vishal Kulkarni		for (i = 0; i < dc_size; i += 40, p += 10) {
5677e6ad46Vishal Kulkarni			printf("  %02x   %08x %08x %08x\n",
5687e6ad46Vishal Kulkarni				p[3] & 0xff, p[2], p[1], p[0]);
5697e6ad46Vishal Kulkarni
5707e6ad46Vishal Kulkarni			printf("  %02x   %02x%06x %02x%06x %02x%06x\n",
5717e6ad46Vishal Kulkarni				(p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
5727e6ad46Vishal Kulkarni				     p[5] & 0xff, p[4] >> 8, p[4] & 0xff,
5737e6ad46Vishal Kulkarni				     p[3] >> 8);
5747e6ad46Vishal Kulkarni
5757e6ad46Vishal Kulkarni			printf("  %02x   %04x%04x %04x%04x %04x%04x\n",
5767e6ad46Vishal Kulkarni				(p[9] >> 16) & 0xff, p[9] & 0xffff,
5777e6ad46Vishal Kulkarni				p[8] >> 16, p[8] & 0xffff, p[7] >> 16,
5787e6ad46Vishal Kulkarni				p[7] & 0xffff, p[6] >> 16);
5797e6ad46Vishal Kulkarni		}
5807e6ad46Vishal Kulkarni		goto err1;
5817e6ad46Vishal Kulkarni	}
5827e6ad46Vishal Kulkarni
5837e6ad46Vishal Kulkarni	printf("Status   Inst    Data      PC     "\
5847e6ad46Vishal Kulkarni		     "LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data\n");
5857e6ad46Vishal Kulkarni
5867e6ad46Vishal Kulkarni	for (i = 0; i < dc_size; i += 40, p += 10) {
5877e6ad46Vishal Kulkarni		printf("  %02x   %04x%04x %04x%04x "\
5887e6ad46Vishal Kulkarni			     "%04x%04x %08x %08x %08x %08x %08x %08x\n",
5897e6ad46Vishal Kulkarni			     (p[9] >> 16) & 0xff,       /* Status */
5907e6ad46Vishal Kulkarni			     p[9] & 0xffff, p[8] >> 16, /* Inst */
5917e6ad46Vishal Kulkarni			     p[8] & 0xffff, p[7] >> 16, /* Data */
5927e6ad46Vishal Kulkarni			     p[7] & 0xffff, p[6] >> 16, /* PC */
5937e6ad46Vishal Kulkarni			     p[2], p[1], p[0],          /* LS0 Stat, Addr
5947e6ad46Vishal Kulkarni							   and Data */
5957e6ad46Vishal Kulkarni			     p[5], p[4], p[3]);         /* LS1 Stat, Addr
5967e6ad46Vishal Kulkarni							   and Data */
5977e6ad46Vishal Kulkarni	}
5987e6ad46Vishal Kulkarni
5997e6ad46Vishal Kulkarnierr1:
6007e6ad46Vishal Kulkarni	return rc;
6017e6ad46Vishal Kulkarni}
6027e6ad46Vishal Kulkarni
6037e6ad46Vishal Kulkarnistatic int
6047e6ad46Vishal Kulkarniview_cim_la_t5(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
6057e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf)
6067e6ad46Vishal Kulkarni{
6077e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
6087e6ad46Vishal Kulkarni	u32 i, *p, cfg, dc_size;
6097e6ad46Vishal Kulkarni	int rc;
6107e6ad46Vishal Kulkarni
6117e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
6127e6ad46Vishal Kulkarni	if (rc)
6137e6ad46Vishal Kulkarni		return rc;
6147e6ad46Vishal Kulkarni
6157e6ad46Vishal Kulkarni	dc_size = dc_buff.offset;
6167e6ad46Vishal Kulkarni	p = (u32 *)((char *)dc_buff.data + sizeof(cfg));
6177e6ad46Vishal Kulkarni	cfg = *((u32 *)dc_buff.data);
6187e6ad46Vishal Kulkarni	dc_size -= sizeof(cfg);
6197e6ad46Vishal Kulkarni
6207e6ad46Vishal Kulkarni	if (cfg & F_UPDBGLACAPTPCONLY) {
6217e6ad46Vishal Kulkarni		/* as per cim_la_show_3in1() (in
6227e6ad46Vishal Kulkarni		 * sw\dev\linux\drv\cxgb4_main.c)*/
6237e6ad46Vishal Kulkarni		printf("Status   Data      PC\r\n");
6247e6ad46Vishal Kulkarni
6257e6ad46Vishal Kulkarni		for (i = 0; i < dc_size; i += 32, p += 8) {
6267e6ad46Vishal Kulkarni			printf("  %02X   %08X %08X\r\n",
6277e6ad46Vishal Kulkarni				(p[5] & 0xFF), p[6], p[7]);
6287e6ad46Vishal Kulkarni
6297e6ad46Vishal Kulkarni			printf(
6307e6ad46Vishal Kulkarni				     "  %02X   %02X%06X %02X%06X\n",
6317e6ad46Vishal Kulkarni				     ((p[3] >> 8) & 0xFF), (p[3] & 0xFF),
6327e6ad46Vishal Kulkarni				     (p[4] >> 8), (p[4] & 0xFF), (p[5] >> 8));
6337e6ad46Vishal Kulkarni
6347e6ad46Vishal Kulkarni			printf(
6357e6ad46Vishal Kulkarni				     "  %02X   %X%07X %X%07X\r\n",
6367e6ad46Vishal Kulkarni				     ((p[0] >> 4) & 0xFF), (p[0] & 0xF),
6377e6ad46Vishal Kulkarni				     (p[1] >> 4), (p[1] & 0xF), (p[2] >> 4));
6387e6ad46Vishal Kulkarni		}
6397e6ad46Vishal Kulkarni		goto err1;
6407e6ad46Vishal Kulkarni	}
6417e6ad46Vishal Kulkarni
6427e6ad46Vishal Kulkarni	printf("Status   Data      PC     LS0Stat  "\
6437e6ad46Vishal Kulkarni		     "LS0Addr             LS0Data\n");
6447e6ad46Vishal Kulkarni
6457e6ad46Vishal Kulkarni	for (i = 0; i < dc_size; i += 32, p += 8) {
6467e6ad46Vishal Kulkarni		printf("%02x   %x%07x %x%07x %08x "\
6477e6ad46Vishal Kulkarni			     "%08x %08x%08x%08x%08x\n",
6487e6ad46Vishal Kulkarni			     ((p[0] >> 4) & 0xFF), (p[0] & 0xF), (p[1] >> 4),
6497e6ad46Vishal Kulkarni			     (p[1] & 0xF), (p[2] >> 4), (p[2] & 0xF), p[3],
6507e6ad46Vishal Kulkarni			     p[4], p[5], p[6], p[7]);
6517e6ad46Vishal Kulkarni	}
6527e6ad46Vishal Kulkarnierr1:
6537e6ad46Vishal Kulkarni	return rc;
6547e6ad46Vishal Kulkarni}
6557e6ad46Vishal Kulkarni
6567e6ad46Vishal Kulkarniint
6577e6ad46Vishal Kulkarniview_cim_la(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
6587e6ad46Vishal Kulkarni	    struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
6597e6ad46Vishal Kulkarni{
6607e6ad46Vishal Kulkarni	int rc = -1;
6617e6ad46Vishal Kulkarni
6627e6ad46Vishal Kulkarni	if (is_t5(chip))
6637e6ad46Vishal Kulkarni		rc = view_cim_la_t5(pbuf, entity_hdr, cudbg_poutbuf);
6647e6ad46Vishal Kulkarni	else if (is_t6(chip))
6657e6ad46Vishal Kulkarni		rc = view_cim_la_t6(pbuf, entity_hdr, cudbg_poutbuf);
6667e6ad46Vishal Kulkarni
6677e6ad46Vishal Kulkarni	return rc;
6687e6ad46Vishal Kulkarni}
6697e6ad46Vishal Kulkarni
6707e6ad46Vishal Kulkarniint
6717e6ad46Vishal Kulkarniview_cim_ma_la(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
6727e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
6737e6ad46Vishal Kulkarni{
6747e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
6757e6ad46Vishal Kulkarni	int rc, i, j;
6767e6ad46Vishal Kulkarni	u32 *p;
6777e6ad46Vishal Kulkarni
6787e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
6797e6ad46Vishal Kulkarni	if (rc)
6807e6ad46Vishal Kulkarni		return rc;
6817e6ad46Vishal Kulkarni
6827e6ad46Vishal Kulkarni	p = (u32 *)dc_buff.data;
6837e6ad46Vishal Kulkarni	for (i = 0; i <= CIM_MALA_SIZE; i++, p += 4) {
6847e6ad46Vishal Kulkarni		if (i < CIM_MALA_SIZE) {
6857e6ad46Vishal Kulkarni			printf(
6867e6ad46Vishal Kulkarni				     "%02x%08x%08x%08x%08x\n",
6877e6ad46Vishal Kulkarni				     p[4], p[3], p[2], p[1], p[0]);
6887e6ad46Vishal Kulkarni		} else {
6897e6ad46Vishal Kulkarni			printf("\nCnt ID Tag UE   "\
6907e6ad46Vishal Kulkarni				     "   Data       RDY VLD\n");
6917e6ad46Vishal Kulkarni			for (j = 0; j < CIM_MALA_SIZE ; j++, p += 3) {
6927e6ad46Vishal Kulkarni				printf(
6937e6ad46Vishal Kulkarni					     "%3u %2u  %x  %u %08x%08x  %u   "\
6947e6ad46Vishal Kulkarni					     "%u\n",
6957e6ad46Vishal Kulkarni					     (p[2] >> 10) & 0xff,
6967e6ad46Vishal Kulkarni					     (p[2] >> 7) & 7, (p[2] >> 3) & 0xf,
6977e6ad46Vishal Kulkarni					     (p[2] >> 2) & 1,
6987e6ad46Vishal Kulkarni					     (p[1] >> 2) | ((p[2] & 3) << 30),
6997e6ad46Vishal Kulkarni					     (p[0] >> 2) | ((p[1] & 3) << 30),
7007e6ad46Vishal Kulkarni					     (p[0] >> 1) & 1, p[0] & 1);
7017e6ad46Vishal Kulkarni			}
7027e6ad46Vishal Kulkarni		}
7037e6ad46Vishal Kulkarni	}
7047e6ad46Vishal Kulkarni
7057e6ad46Vishal Kulkarni	return rc;
7067e6ad46Vishal Kulkarni}
7077e6ad46Vishal Kulkarni
7087e6ad46Vishal Kulkarniint
7097e6ad46Vishal Kulkarniview_cim_qcfg(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
7107e6ad46Vishal Kulkarni	      struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
7117e6ad46Vishal Kulkarni{
7127e6ad46Vishal Kulkarni	static const char * const pQname[] = {
7137e6ad46Vishal Kulkarni		"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",
7147e6ad46Vishal Kulkarni		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI"
7157e6ad46Vishal Kulkarni	};
7167e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
7177e6ad46Vishal Kulkarni	struct struct_cim_qcfg *q_cfg_data;
7187e6ad46Vishal Kulkarni	u32 *p, *wr;
7197e6ad46Vishal Kulkarni	int rc, i;
7207e6ad46Vishal Kulkarni
7217e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
7227e6ad46Vishal Kulkarni	if (rc)
7237e6ad46Vishal Kulkarni		return rc;
7247e6ad46Vishal Kulkarni
7257e6ad46Vishal Kulkarni	q_cfg_data = (struct struct_cim_qcfg *) (dc_buff.data);
7267e6ad46Vishal Kulkarni	p = q_cfg_data->stat;
7277e6ad46Vishal Kulkarni	wr = q_cfg_data->obq_wr;
7287e6ad46Vishal Kulkarni
7297e6ad46Vishal Kulkarni	printf("  Queue Base Size Thres  RdPtr "\
7307e6ad46Vishal Kulkarni		     "WrPtr  SOP  EOP Avail\n");
7317e6ad46Vishal Kulkarni	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4) {
7327e6ad46Vishal Kulkarni		printf("%5s %5x %5u %4u %6x  %4x "\
7337e6ad46Vishal Kulkarni			     "%4u %4u %5u\n",
7347e6ad46Vishal Kulkarni			     pQname[i],
7357e6ad46Vishal Kulkarni			     q_cfg_data->base[i], q_cfg_data->size[i],
7367e6ad46Vishal Kulkarni			     q_cfg_data->thres[i], G_IBQRDADDR(p[0]),
7377e6ad46Vishal Kulkarni			     G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]),
7387e6ad46Vishal Kulkarni			     G_QUEEOPCNT(p[3]), G_QUEREMFLITS(p[2]) * 16);
7397e6ad46Vishal Kulkarni	}
7407e6ad46Vishal Kulkarni
7417e6ad46Vishal Kulkarni	for (; i < CIM_NUM_IBQ + CIM_NUM_OBQ; i++, p += 4, wr += 2) {
7427e6ad46Vishal Kulkarni		printf("%5s %5x %5u %11x  %4x %4u "\
7437e6ad46Vishal Kulkarni			     "%4u %5u\n",
7447e6ad46Vishal Kulkarni			     pQname[i],
7457e6ad46Vishal Kulkarni			     q_cfg_data->base[i], q_cfg_data->size[i],
7467e6ad46Vishal Kulkarni			     G_QUERDADDR(p[0]) & 0x3fff,
7477e6ad46Vishal Kulkarni			     wr[0] - q_cfg_data->base[i], G_QUESOPCNT(p[3]),
7487e6ad46Vishal Kulkarni			     G_QUEEOPCNT(p[3]), G_QUEREMFLITS(p[2]) * 16);
7497e6ad46Vishal Kulkarni	}
7507e6ad46Vishal Kulkarni
7517e6ad46Vishal Kulkarni	return rc;
7527e6ad46Vishal Kulkarni}
7537e6ad46Vishal Kulkarni
7547e6ad46Vishal Kulkarniint
7557e6ad46Vishal Kulkarnidecompress_buffer_wrapper(struct cudbg_buffer *pc_buff,
7567e6ad46Vishal Kulkarni			  struct cudbg_buffer *pdc_buff)
7577e6ad46Vishal Kulkarni{
7587e6ad46Vishal Kulkarni	int rc = 0;
7597e6ad46Vishal Kulkarni	pdc_buff->data =  malloc(2 * CUDBG_CHUNK_SIZE);
7607e6ad46Vishal Kulkarni	if (pdc_buff->data == NULL) {
7617e6ad46Vishal Kulkarni		rc = CUDBG_STATUS_NOSPACE;
7627e6ad46Vishal Kulkarni		goto err;
7637e6ad46Vishal Kulkarni	}
7647e6ad46Vishal Kulkarni	pdc_buff->size = 2 * CUDBG_CHUNK_SIZE;
7657e6ad46Vishal Kulkarni
7667e6ad46Vishal Kulkarni	rc = decompress_buffer(pc_buff, pdc_buff);
7677e6ad46Vishal Kulkarni	if (rc == CUDBG_STATUS_SMALL_BUFF) {
7687e6ad46Vishal Kulkarni		free(pdc_buff->data);
7697e6ad46Vishal Kulkarni		pdc_buff->data =  malloc(pdc_buff->size);
7707e6ad46Vishal Kulkarni
7717e6ad46Vishal Kulkarni		if (pdc_buff->data == NULL) {
7727e6ad46Vishal Kulkarni			printf("malloc failed for size %u\n", pdc_buff->size);
7737e6ad46Vishal Kulkarni			rc = CUDBG_STATUS_NOSPACE;
7747e6ad46Vishal Kulkarni			goto err;
7757e6ad46Vishal Kulkarni		}
7767e6ad46Vishal Kulkarni		rc = decompress_buffer(pc_buff, pdc_buff);
7777e6ad46Vishal Kulkarni	}
7787e6ad46Vishal Kulkarnierr:
7797e6ad46Vishal Kulkarni	return rc;
7807e6ad46Vishal Kulkarni}
7817e6ad46Vishal Kulkarni
7827e6ad46Vishal Kulkarniint
7837e6ad46Vishal Kulkarnicopy_bin_data(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
7847e6ad46Vishal Kulkarni	      const char *fname, struct cudbg_buffer *cudbg_poutbuf)
7857e6ad46Vishal Kulkarni{
7867e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
7877e6ad46Vishal Kulkarni	int rc = 0;
7887e6ad46Vishal Kulkarni
7897e6ad46Vishal Kulkarni	if (cudbg_poutbuf->data == NULL)
7907e6ad46Vishal Kulkarni		return 0;
7917e6ad46Vishal Kulkarni
7927e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
7937e6ad46Vishal Kulkarni	if (rc)
7947e6ad46Vishal Kulkarni		return rc;
7957e6ad46Vishal Kulkarni
7967e6ad46Vishal Kulkarni	if (dc_buff.size > cudbg_poutbuf->size) {
7977e6ad46Vishal Kulkarni		rc = CUDBG_STATUS_OUTBUFF_OVERFLOW;
7987e6ad46Vishal Kulkarni		cudbg_poutbuf->size = dc_buff.size;
7997e6ad46Vishal Kulkarni		goto err1;
8007e6ad46Vishal Kulkarni	}
8017e6ad46Vishal Kulkarni
8027e6ad46Vishal Kulkarni	memcpy(cudbg_poutbuf->data, dc_buff.data, dc_buff.size);
8037e6ad46Vishal Kulkarni	cudbg_poutbuf->size = dc_buff.size;
8047e6ad46Vishal Kulkarni
8057e6ad46Vishal Kulkarnierr1:
8067e6ad46Vishal Kulkarni	return rc;
8077e6ad46Vishal Kulkarni}
8087e6ad46Vishal Kulkarni
8097e6ad46Vishal Kulkarniint
8107e6ad46Vishal Kulkarniview_edc0_data(char *pbuf, struct cudbg_entity_hdr *ent_hdr,
8117e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8127e6ad46Vishal Kulkarni{
8137e6ad46Vishal Kulkarni	return copy_bin_data(pbuf, ent_hdr, "_cudbg_edc0.bin", cudbg_poutbuf);
8147e6ad46Vishal Kulkarni}
8157e6ad46Vishal Kulkarni
8167e6ad46Vishal Kulkarniint
8177e6ad46Vishal Kulkarniview_edc1_data(char *pbuf, struct cudbg_entity_hdr *ent_hdr,
8187e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8197e6ad46Vishal Kulkarni{
8207e6ad46Vishal Kulkarni	return copy_bin_data(pbuf, ent_hdr, "_cudbg_edc1.bin", cudbg_poutbuf);
8217e6ad46Vishal Kulkarni}
8227e6ad46Vishal Kulkarni
8237e6ad46Vishal Kulkarniint
8247e6ad46Vishal Kulkarniview_mc0_data(char *pbuf, struct cudbg_entity_hdr *ent_hdr,
8257e6ad46Vishal Kulkarni	      struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8267e6ad46Vishal Kulkarni{
8277e6ad46Vishal Kulkarni	return copy_bin_data(pbuf, ent_hdr, "_cudbg_mc0.bin", cudbg_poutbuf);
8287e6ad46Vishal Kulkarni}
8297e6ad46Vishal Kulkarni
8307e6ad46Vishal Kulkarniint
8317e6ad46Vishal Kulkarniview_mc1_data(char *pbuf, struct cudbg_entity_hdr *ent_hdr,
8327e6ad46Vishal Kulkarni	      struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8337e6ad46Vishal Kulkarni{
8347e6ad46Vishal Kulkarni	return copy_bin_data(pbuf, ent_hdr, "_cudbg_mc1.bin", cudbg_poutbuf);
8357e6ad46Vishal Kulkarni}
8367e6ad46Vishal Kulkarni
8377e6ad46Vishal Kulkarniint
8387e6ad46Vishal Kulkarniview_hma_data(char *pbuf, struct cudbg_entity_hdr *ent_hdr,
8397e6ad46Vishal Kulkarni	      struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8407e6ad46Vishal Kulkarni{
8417e6ad46Vishal Kulkarni	return copy_bin_data(pbuf, ent_hdr, "_cudbg_hma.bin", cudbg_poutbuf);
8427e6ad46Vishal Kulkarni}
8437e6ad46Vishal Kulkarni
8447e6ad46Vishal Kulkarniint
8457e6ad46Vishal Kulkarniview_sw_state(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
8467e6ad46Vishal Kulkarni	      struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8477e6ad46Vishal Kulkarni{
8487e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
8497e6ad46Vishal Kulkarni	u8 os_type, *caller_string;
8507e6ad46Vishal Kulkarni	struct sw_state *swstate;
8517e6ad46Vishal Kulkarni	char *os, *fwstate;
8527e6ad46Vishal Kulkarni	u32 fw_state;
8537e6ad46Vishal Kulkarni	int rc = 0;
8547e6ad46Vishal Kulkarni
8557e6ad46Vishal Kulkarni	rc = cudbg_view_decompress_buff(pbuf, entity_hdr, &c_buff, &dc_buff);
8567e6ad46Vishal Kulkarni	if (rc)
8577e6ad46Vishal Kulkarni		return rc;
8587e6ad46Vishal Kulkarni
8597e6ad46Vishal Kulkarni	swstate = (struct sw_state *) dc_buff.data;
8607e6ad46Vishal Kulkarni	fw_state = swstate->fw_state;
8617e6ad46Vishal Kulkarni	caller_string = swstate->caller_string;
8627e6ad46Vishal Kulkarni	os_type = swstate->os_type;
8637e6ad46Vishal Kulkarni
8647e6ad46Vishal Kulkarni	printf("\n");
8657e6ad46Vishal Kulkarni	if (fw_state & F_PCIE_FW_ERR && G_PCIE_FW_EVAL(fw_state) ==
8667e6ad46Vishal Kulkarni	    PCIE_FW_EVAL_CRASH)
8677e6ad46Vishal Kulkarni		fwstate = "Crashed";
8687e6ad46Vishal Kulkarni	else
8697e6ad46Vishal Kulkarni		fwstate = "Alive";
8707e6ad46Vishal Kulkarni
8717e6ad46Vishal Kulkarni	switch (os_type) {
8727e6ad46Vishal Kulkarni	case CUDBG_OS_TYPE_WINDOWS:
8737e6ad46Vishal Kulkarni		os = "Windows";
8747e6ad46Vishal Kulkarni		break;
8757e6ad46Vishal Kulkarni	case CUDBG_OS_TYPE_LINUX:
8767e6ad46Vishal Kulkarni		os = "Linux";
8777e6ad46Vishal Kulkarni		break;
8787e6ad46Vishal Kulkarni	case CUDBG_OS_TYPE_ESX:
8797e6ad46Vishal Kulkarni		os = "ESX";
8807e6ad46Vishal Kulkarni		break;
8817e6ad46Vishal Kulkarni	case CUDBG_OS_TYPE_UNKNOWN:
8827e6ad46Vishal Kulkarni	default:
8837e6ad46Vishal Kulkarni		os = "Unknown";
8847e6ad46Vishal Kulkarni	}
8857e6ad46Vishal Kulkarni
8867e6ad46Vishal Kulkarni	printf("\tFW STATE  : %s\n", fwstate);
8877e6ad46Vishal Kulkarni	printf("\tOS        : %s\n", os);
8887e6ad46Vishal Kulkarni	printf("\tCALLER    : %s\n", caller_string);
8897e6ad46Vishal Kulkarni	printf("\n");
8907e6ad46Vishal Kulkarni
8917e6ad46Vishal Kulkarni	return rc;
8927e6ad46Vishal Kulkarni}
8937e6ad46Vishal Kulkarni
8947e6ad46Vishal Kulkarniint
8957e6ad46Vishal Kulkarniview_cpl_stats(char *pbuf, struct cudbg_entity_hdr *entity_hdr,
8967e6ad46Vishal Kulkarni	       struct cudbg_buffer *cudbg_poutbuf, enum chip_type chip)
8977e6ad46Vishal Kulkarni{
8987e6ad46Vishal Kulkarni	struct struct_tp_cpl_stats *tp_cpl_stats_buff;
8997e6ad46Vishal Kulkarni	struct cudbg_buffer c_buff, dc_buff;
9007e6ad46Vishal Kulkarni	struct tp_cpl_stats stats;
9017e6ad46Vishal Kulkarni	int rc = 0;