2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018 Chelsio Communications, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include "tcb_common.h"
32 /***:-----------------------------------------------------------------------
34 ***:-----------------------------------------------------------------------
37 extern _TCBVAR g_tcb_info4[];
38 extern _TCBVAR g_scb_info4[];
39 extern _TCBVAR g_fcb_info4[];
40 extern void t4_display_tcb_aux_0(_TCBVAR *tvp,int aux);
41 extern void t4_display_tcb_aux_1(_TCBVAR *tvp,int aux);
42 extern void t4_display_tcb_aux_2(_TCBVAR *tvp,int aux);
43 extern void t4_display_tcb_aux_3(_TCBVAR *tvp,int aux);
45 extern _TCBVAR g_tcb_info5[];
46 extern _TCBVAR g_scb_info5[];
47 extern _TCBVAR g_fcb_info5[];
48 extern void t5_display_tcb_aux_0(_TCBVAR *tvp,int aux);
49 extern void t5_display_tcb_aux_1(_TCBVAR *tvp,int aux);
50 extern void t5_display_tcb_aux_2(_TCBVAR *tvp,int aux);
51 extern void t5_display_tcb_aux_3(_TCBVAR *tvp,int aux);
53 extern _TCBVAR g_tcb_info6[];
54 extern _TCBVAR g_scb_info6[];
55 extern _TCBVAR g_fcb_info6[];
56 extern void t6_display_tcb_aux_0(_TCBVAR *tvp,int aux);
57 extern void t6_display_tcb_aux_1(_TCBVAR *tvp,int aux);
58 extern void t6_display_tcb_aux_2(_TCBVAR *tvp,int aux);
59 extern void t6_display_tcb_aux_3(_TCBVAR *tvp,int aux);
60 extern void t6_display_tcb_aux_4(_TCBVAR *tvp,int aux);
62 /***:-----------------------------------------------------------------------
64 ***:-----------------------------------------------------------------------
67 _TCBVAR *g_tcb_info=g_tcb_info5;
68 _TCBVAR *g_scb_info=g_scb_info5;
69 _TCBVAR *g_fcb_info=g_fcb_info5;
72 static int g_prntstyl=PRNTSTYL_COMP;
74 static int g_got_scb=0;
75 static int g_got_fcb=0;
78 /***:-----------------------------------------------------------------------
79 ***: error exit functions
80 ***:-----------------------------------------------------------------------
83 /**: err_exit functions
87 void tcb_prflush(void)
94 void tcb_code_err_exit(char *fmt, ...)
98 printf("Coding Error in: ");
106 /***:-----------------------------------------------------------------------
107 ***: tcb_hexdump functions
108 ***:-----------------------------------------------------------------------
112 tcb_hexdump(unsigned base, unsigned char *buf, unsigned int size)
116 for (offset = 0; offset < size; ++offset) {
117 if (!(offset % 16)) printf("\n0x%4.4x: ", base + offset);
118 else if (!(offset % 8)) printf(" ");
119 printf("%2.2x ", (unsigned char)buf[offset]);
123 int tcb_strmatch_nc(char *cs, char *ct) {
125 if (tolower(*cs++) != tolower(*ct++)) return (FALSE);
126 return (!(*ct)); /*return TRUE if *ct NULL at same time as *cs==NULL*/
130 /*: -------------------------------------------------------------------------
132 tcb_strmatch_nc: Similar to exact match, but case insensitive.
137 tcb_strncmp_nc(char *cs, char *ct, int n)
139 /*case insensitive version of the standard strncmp() function */
145 for (i = 0; i < n && 0 == ret && !(EOS == *cs && EOS == *ct); ++i) {
146 /* this is weird, but it matched GCC linux when strings don't
147 * have any upper case characters.
149 ret = tolower(*cs++) - tolower(*ct++);
155 tcb_startswith_nc(char *cs, char *ct)
156 { /* return true if cs start with ct */
157 return (0 == tcb_strncmp_nc(cs, ct, (int)strlen(ct)));
163 /***:-----------------------------------------------------------------------
164 ***: START OF WINDOWS FUNCTIONS
165 ***:-----------------------------------------------------------------------
169 /***:-----------------------------------------------------------------------
171 ***:-----------------------------------------------------------------------
174 static int g_PR_indent=1;
176 void PR(char *fmt, ...)
182 if (g_PR_indent) printf(" ");
184 fmt_len=(int) strlen(fmt);
185 if (fmt_len>0 && fmt[fmt_len-1]=='\n') g_PR_indent=1;
193 /***:-----------------------------------------------------------------------
195 ***:-----------------------------------------------------------------------
199 lu_tcbvar(char *name)
201 _TCBVAR *tvp=g_tcb_info;
203 while (tvp->name!=NULL) {
204 if (tcb_strmatch_nc(name,tvp->name)) return tvp;
205 else if (tcb_strmatch_nc(name,tvp->aka )) return tvp;
208 tcb_code_err_exit("lu_tcbvar: bad name %s\n",name);
232 /***:-----------------------------------------------------------------------
234 ***:-----------------------------------------------------------------------
239 get_tcb_bit(unsigned char *A, int bit)
246 /* prdbg(" ix: %u, shift=%u\n",ix,shift); */
247 ret=(A[ix] >> shift) & 1;
252 get_tcb_bits (unsigned char *A, int hi, int lo)
263 ret = (ret<<1) | get_tcb_bit(A,hi);
272 decompress_val(_TCBVAR *tvp,unsigned ulp_type,unsigned tx_max,
273 unsigned rcv_nxt,unsigned rx_frag0_start_idx_raw)
275 unsigned rawval=(unsigned) tvp->rawval;
278 case COMP_NONE: tvp->val=rawval; break;
279 case COMP_ULP: tvp->val=rawval; break;
281 tvp->val=(tx_max - rawval) & 0xFFFFFFFF;
284 if (tcb_startswith_nc(tvp->name,"rx_frag")) {
286 if (!tcb_strmatch_nc(tvp->name,"rx_frag0_start_idx_raw"))
288 tvp->val=(rcv_nxt+rx_frag0_start_idx_raw+fragx) & 0xFFFFFFFF;
290 tvp->val=(rcv_nxt - rawval) & 0xFFFFFFFF;
293 case COMP_PTR: tvp->val=rawval; break;
297 if (PM_MODE_RDDP==ulp_type || PM_MODE_DDP==ulp_type ||
298 PM_MODE_IANDP==ulp_type) {
299 /* TP does this internally. Not sure if I should show the
300 * unaltered value or the raw value. For now I
301 * will display the raw value. For now I've added the code
302 * mainly to stop windows compiler from warning about ulp_type
303 * being an unreferenced parameter.
306 tvp->val=rawval; /* comment this out to display altered value */
311 tcb_code_err_exit("decompress_val, bad switch: %d",tvp->comp);
321 get_tcb_field(_TCBVAR *tvp,unsigned char *buf)
323 assert(tvp->hi-tvp->lo+1<=64);
324 assert(tvp->hi>=tvp->lo);
326 tvp->rawval=get_tcb_bits(buf,tvp->lo,tvp->hi);
327 /* assume no compression and 32-bit value for now */
328 tvp->val=(unsigned) (tvp->rawval & 0xFFFFFFFF);
334 /***:-----------------------------------------------------------------------
336 ***:-----------------------------------------------------------------------
340 spr_tcp_state (unsigned state)
344 if ( 0 == state) {ret = "CLOSED";}
345 else if ( 1 == state) {ret = "LISTEN";}
346 else if ( 2 == state) {ret = "SYN_SENT";}
347 else if ( 3 == state) {ret = "SYN_RCVD";}
348 else if ( 4 == state) {ret = "ESTABLISHED";}
349 else if ( 5 == state) {ret = "CLOSE_WAIT";}
350 else if ( 6 == state) {ret = "FIN_WAIT_1";}
351 else if ( 7 == state) {ret = "CLOSING";}
352 else if ( 8 == state) {ret = "LAST_ACK";}
353 else if ( 9 == state) {ret = "FIN_WAIT_2";}
354 else if (10 == state) {ret = "TIME_WAIT";}
355 else if (11 == state) {ret = "ESTABLISHED_RX";}
356 else if (12 == state) {ret = "ESTABLISHED_TX";}
357 else if (13 == state) {ret = "SYN_PEND";}
358 else if (14 == state) {ret = "ESC_1_STATE";}
359 else if (15 == state) {ret = "ESC_2_STATE";}
365 spr_cctrl_sel(unsigned sel0,unsigned sel1)
367 unsigned sel=(sel1<<1) | sel0;
370 if ( 0 == sel) {ret = "Reno";}
371 else if ( 1 == sel) {ret = "Tahoe";}
372 else if ( 2 == sel) {ret = "NewReno";}
373 else if ( 3 == sel) {ret = "HighSpeed";}
380 spr_ulp_type(unsigned ulp_type)
384 /*The tp.h PM_MODE_XXX call 1 DDP and 5 IANDP, but external
385 * documentation (tcb.h" calls 5 ddp, and doesn't mention 1 or 3.
388 if ( PM_MODE_PASS == ulp_type) {ret = "TOE";}
389 else if ( PM_MODE_DDP == ulp_type) {ret = "DDP";}
390 else if ( PM_MODE_ISCSI == ulp_type) {ret = "ISCSI";}
391 else if ( PM_MODE_IWARP == ulp_type) {ret = "IWARP";}
392 else if ( PM_MODE_RDDP == ulp_type) {ret = "RDMA";}
393 else if ( PM_MODE_IANDP == ulp_type) {ret = "IANDP_DDP";}
394 else if ( PM_MODE_FCOE == ulp_type) {ret = "FCoE";}
395 else if ( PM_MODE_USER == ulp_type) {ret = "USER";}
396 else if ( PM_MODE_TLS == ulp_type) {ret = "TLS";}
397 else if ( PM_MODE_DTLS == ulp_type) {ret = "DTLS";}
403 spr_ip_version(unsigned ip_version)
407 if ( 0 == ip_version) {ret = "IPv4";}
408 else if ( 1 == ip_version) {ret = "IPv6";}
415 /***:-----------------------------------------------------------------------
417 ***:-----------------------------------------------------------------------
421 display_tcb_compressed(_TCBVAR *tvp,int aux)
425 t4_display_tcb_aux_0(tvp,aux);
426 if (1==aux) t4_display_tcb_aux_1(tvp,aux);
427 else if (2==aux) t4_display_tcb_aux_2(tvp,aux);
428 else if (3==aux) t4_display_tcb_aux_3(tvp,aux);
430 } else if (g_tN==5) {
431 t5_display_tcb_aux_0(tvp,aux);
432 if (1==aux) t5_display_tcb_aux_1(tvp,aux);
433 else if (2==aux) t5_display_tcb_aux_2(tvp,aux);
434 else if (3==aux) t5_display_tcb_aux_3(tvp,aux);
435 } else if (g_tN==6) {
436 t6_display_tcb_aux_0(tvp,aux);
437 if (1==aux) t6_display_tcb_aux_1(tvp,aux);
438 else if (2==aux) t6_display_tcb_aux_2(tvp,aux);
439 else if (3==aux) t6_display_tcb_aux_3(tvp,aux);
440 else if (4==aux) t6_display_tcb_aux_4(tvp,aux);
447 /***:-----------------------------------------------------------------------
448 ***: parse_n_decode_tcb
449 ***:-----------------------------------------------------------------------
454 parse_tcb( _TCBVAR *base_tvp, unsigned char *buf)
455 { /* parse the TCB */
456 _TCBVAR *tvp=base_tvp;
458 int aux=1; /* assume TOE or iSCSI */
459 unsigned tx_max=0, rcv_nxt=0, rx_frag0_start_idx_raw=0;
460 int got_tx_max=0, got_rcv_nxt=0, got_rx_frag0_start_idx_raw=0;
464 while (tvp->name!=NULL) {
465 get_tcb_field(tvp,buf);
466 if (!got_tx_max && tcb_strmatch_nc("tx_max",tvp->name)) {
470 if (!got_rcv_nxt && tcb_strmatch_nc("rcv_nxt",tvp->name)) {
474 if (!got_rx_frag0_start_idx_raw &&
475 tcb_strmatch_nc("rx_frag0_start_idx_raw",tvp->name)) {
476 rx_frag0_start_idx_raw=tvp->val;
477 got_rx_frag0_start_idx_raw=1;
483 ulp_type=tvp->val; /* ULP type is always first variable in TCB */
484 if (PM_MODE_IANDP==ulp_type || PM_MODE_FCOE==ulp_type) aux=3;
485 else if (PM_MODE_RDDP==ulp_type) aux=2;
486 else if (6==g_tN && (PM_MODE_TLS==ulp_type || PM_MODE_DTLS==ulp_type)) aux=4;
489 assert(got_tx_max && got_rcv_nxt && got_rx_frag0_start_idx_raw);
491 /* decompress the compressed values */
493 while (tvp->name!=NULL) {
494 decompress_val(tvp,ulp_type,tx_max,rcv_nxt,rx_frag0_start_idx_raw);
504 parse_scb( _TCBVAR *base_tvp, unsigned char *buf)
505 { /* parse the SCB */
506 _TCBVAR *tvp=base_tvp;
508 while (tvp->name!=NULL) {
509 if (tcb_strmatch_nc("scb_slush",tvp->name)) {
510 /* the scb_slush field is all of remaining memory */
514 get_tcb_field(tvp,buf);
522 parse_fcb( _TCBVAR *base_tvp, unsigned char *buf)
523 { /* parse the FCB */
524 _TCBVAR *tvp=base_tvp;
526 while (tvp->name!=NULL) {
527 get_tcb_field(tvp,buf);
534 display_list_tcb(_TCBVAR *base_tvp,int aux)
536 _TCBVAR *tvp=base_tvp;
537 while (tvp->name!=NULL) {
538 if (tvp->aux==0 || tvp->aux==aux) {
539 if (tvp->hi-tvp->lo+1<=32) {
540 printf(" %4d:%4d %31s: %10u (0x%1x)",tvp->lo,tvp->hi,tvp->name,
541 (unsigned) tvp->rawval,(unsigned) tvp->rawval);
542 if (COMP_TX_MAX==tvp->comp || COMP_RCV_NXT==tvp->comp)
543 printf(" -> %1u (0x%x)", tvp->val,tvp->val);
545 printf(" %4d:%4d %31s: 0x%1llx",tvp->lo,tvp->hi,tvp->name,
555 display_tcb(_TCBVAR *tvp,unsigned char *buf,int aux)
557 if (g_prntstyl==PRNTSTYL_VERBOSE ||
558 g_prntstyl==PRNTSTYL_RAW) {
559 tcb_hexdump(0,buf,128);
563 if (g_prntstyl==PRNTSTYL_VERBOSE ||
564 g_prntstyl==PRNTSTYL_LIST) {
565 display_list_tcb(tvp,aux);
568 if (g_prntstyl==PRNTSTYL_VERBOSE ||
569 g_prntstyl==PRNTSTYL_COMP) {
570 display_tcb_compressed(tvp,aux);
576 parse_n_display_tcb(unsigned char *buf)
578 _TCBVAR *tvp=g_tcb_info;
581 aux=parse_tcb(tvp,buf);
582 display_tcb(tvp,buf,aux);
586 parse_n_display_scb(unsigned char *buf)
588 _TCBVAR *tvp=g_scb_info;
591 if (g_prntstyl==PRNTSTYL_VERBOSE ||
592 g_prntstyl==PRNTSTYL_RAW) {
593 tcb_hexdump(0,buf,128);
596 if (g_prntstyl==PRNTSTYL_VERBOSE ||
597 g_prntstyl==PRNTSTYL_LIST ||
598 g_prntstyl==PRNTSTYL_COMP) {
599 display_list_tcb(tvp,0);
604 parse_n_display_fcb(unsigned char *buf)
606 _TCBVAR *tvp=g_fcb_info;
609 if (g_prntstyl==PRNTSTYL_VERBOSE ||
610 g_prntstyl==PRNTSTYL_RAW) {
611 tcb_hexdump(0,buf,128);
615 if (g_prntstyl==PRNTSTYL_VERBOSE ||
616 g_prntstyl==PRNTSTYL_LIST ||
617 g_prntstyl==PRNTSTYL_COMP) {
618 display_list_tcb(tvp,0);
623 parse_n_display_xcb(unsigned char *buf)
625 if (g_got_scb) parse_n_display_scb(buf);
626 else if (g_got_fcb) parse_n_display_fcb(buf);
627 else parse_n_display_tcb(buf);
630 /***:-----------------------------------------------------------------------
632 ***:-----------------------------------------------------------------------
636 swizzle_tcb(unsigned char *buf)
640 for (i=0, j=128-16 ; i<j ; i+=16, j-=16) {
642 for (k=0; k<16; ++k) {
651 /***:-----------------------------------------------------------------------
652 ***: END OF WINDOWS FUNCTIONS
653 ***:-----------------------------------------------------------------------
656 void set_tidtype(unsigned int tidtype)
658 if (tidtype == TIDTYPE_SCB)
662 else if (tidtype == TIDTYPE_FCB)
675 set_tcb_info(unsigned int tidtype, unsigned int cardtype)
677 set_tidtype(tidtype);
681 g_tcb_info = g_tcb_info4;
682 g_scb_info = g_scb_info4;
683 g_fcb_info = g_fcb_info4;
685 else if (5 == g_tN) {
686 g_tcb_info = g_tcb_info5;
687 g_scb_info = g_scb_info5;
688 g_fcb_info = g_fcb_info5;
690 else if (6 == g_tN) {
691 g_tcb_info = g_tcb_info6;
692 g_scb_info = g_scb_info6;
693 g_fcb_info = g_fcb_info6;
698 set_print_style(unsigned int prntstyl)