2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include "tcb_common.h"
34 /***:-----------------------------------------------------------------------
36 ***:-----------------------------------------------------------------------
39 extern _TCBVAR g_tcb_info4[];
40 extern _TCBVAR g_scb_info4[];
41 extern _TCBVAR g_fcb_info4[];
42 extern void t4_display_tcb_aux_0(_TCBVAR *tvp,int aux);
43 extern void t4_display_tcb_aux_1(_TCBVAR *tvp,int aux);
44 extern void t4_display_tcb_aux_2(_TCBVAR *tvp,int aux);
45 extern void t4_display_tcb_aux_3(_TCBVAR *tvp,int aux);
47 extern _TCBVAR g_tcb_info5[];
48 extern _TCBVAR g_scb_info5[];
49 extern _TCBVAR g_fcb_info5[];
50 extern void t5_display_tcb_aux_0(_TCBVAR *tvp,int aux);
51 extern void t5_display_tcb_aux_1(_TCBVAR *tvp,int aux);
52 extern void t5_display_tcb_aux_2(_TCBVAR *tvp,int aux);
53 extern void t5_display_tcb_aux_3(_TCBVAR *tvp,int aux);
55 extern _TCBVAR g_tcb_info6[];
56 extern _TCBVAR g_scb_info6[];
57 extern _TCBVAR g_fcb_info6[];
58 extern void t6_display_tcb_aux_0(_TCBVAR *tvp,int aux);
59 extern void t6_display_tcb_aux_1(_TCBVAR *tvp,int aux);
60 extern void t6_display_tcb_aux_2(_TCBVAR *tvp,int aux);
61 extern void t6_display_tcb_aux_3(_TCBVAR *tvp,int aux);
62 extern void t6_display_tcb_aux_4(_TCBVAR *tvp,int aux);
64 /***:-----------------------------------------------------------------------
66 ***:-----------------------------------------------------------------------
69 _TCBVAR *g_tcb_info=g_tcb_info5;
70 _TCBVAR *g_scb_info=g_scb_info5;
71 _TCBVAR *g_fcb_info=g_fcb_info5;
74 static int g_prntstyl=PRNTSTYL_COMP;
76 static int g_got_scb=0;
77 static int g_got_fcb=0;
80 /***:-----------------------------------------------------------------------
81 ***: error exit functions
82 ***:-----------------------------------------------------------------------
85 /**: err_exit functions
89 void tcb_prflush(void)
96 void tcb_code_err_exit(char *fmt, ...)
100 printf("Coding Error in: ");
108 /***:-----------------------------------------------------------------------
109 ***: tcb_hexdump functions
110 ***:-----------------------------------------------------------------------
114 tcb_hexdump(unsigned base, unsigned char *buf, unsigned int size)
118 for (offset = 0; offset < size; ++offset) {
119 if (!(offset % 16)) printf("\n0x%4.4x: ", base + offset);
120 else if (!(offset % 8)) printf(" ");
121 printf("%2.2x ", (unsigned char)buf[offset]);
125 int tcb_strmatch_nc(char *cs, char *ct) {
127 if (tolower(*cs++) != tolower(*ct++)) return (FALSE);
128 return (!(*ct)); /*return TRUE if *ct NULL at same time as *cs==NULL*/
132 /*: -------------------------------------------------------------------------
134 tcb_strmatch_nc: Similar to exact match, but case insensitive.
139 tcb_strncmp_nc(char *cs, char *ct, int n)
141 /*case insensitive version of the standard strncmp() function */
147 for (i = 0; i < n && 0 == ret && !(EOS == *cs && EOS == *ct); ++i) {
148 /* this is weird, but it matched GCC linux when strings don't
149 * have any upper case characters.
151 ret = tolower(*cs++) - tolower(*ct++);
157 tcb_startswith_nc(char *cs, char *ct)
158 { /* return true if cs start with ct */
159 return (0 == tcb_strncmp_nc(cs, ct, (int)strlen(ct)));
165 /***:-----------------------------------------------------------------------
166 ***: START OF WINDOWS FUNCTIONS
167 ***:-----------------------------------------------------------------------
171 /***:-----------------------------------------------------------------------
173 ***:-----------------------------------------------------------------------
176 static int g_PR_indent=1;
178 void PR(char *fmt, ...)
184 if (g_PR_indent) printf(" ");
186 fmt_len=(int) strlen(fmt);
187 if (fmt_len>0 && fmt[fmt_len-1]=='\n') g_PR_indent=1;
195 /***:-----------------------------------------------------------------------
197 ***:-----------------------------------------------------------------------
201 lu_tcbvar(char *name)
203 _TCBVAR *tvp=g_tcb_info;
205 while (tvp->name!=NULL) {
206 if (tcb_strmatch_nc(name,tvp->name)) return tvp;
207 else if (tcb_strmatch_nc(name,tvp->aka )) return tvp;
210 tcb_code_err_exit("lu_tcbvar: bad name %s\n",name);
234 /***:-----------------------------------------------------------------------
236 ***:-----------------------------------------------------------------------
241 get_tcb_bit(unsigned char *A, int bit)
248 /* prdbg(" ix: %u, shift=%u\n",ix,shift); */
249 ret=(A[ix] >> shift) & 1;
254 get_tcb_bits (unsigned char *A, int hi, int lo)
265 ret = (ret<<1) | get_tcb_bit(A,hi);
274 decompress_val(_TCBVAR *tvp,unsigned ulp_type,unsigned tx_max,
275 unsigned rcv_nxt,unsigned rx_frag0_start_idx_raw)
277 unsigned rawval=(unsigned) tvp->rawval;
280 case COMP_NONE: tvp->val=rawval; break;
281 case COMP_ULP: tvp->val=rawval; break;
283 tvp->val=(tx_max - rawval) & 0xFFFFFFFF;
286 if (tcb_startswith_nc(tvp->name,"rx_frag")) {
288 if (!tcb_strmatch_nc(tvp->name,"rx_frag0_start_idx_raw"))
290 tvp->val=(rcv_nxt+rx_frag0_start_idx_raw+fragx) & 0xFFFFFFFF;
292 tvp->val=(rcv_nxt - rawval) & 0xFFFFFFFF;
295 case COMP_PTR: tvp->val=rawval; break;
299 if (PM_MODE_RDDP==ulp_type || PM_MODE_DDP==ulp_type ||
300 PM_MODE_IANDP==ulp_type) {
301 /* TP does this internally. Not sure if I should show the
302 * unaltered value or the raw value. For now I
303 * will diplay the raw value. For now I've added the code
304 * mainly to stop windows compiler from warning about ulp_type
305 * being an unreferenced parameter.
308 tvp->val=rawval; /* comment this out to display altered value */
313 tcb_code_err_exit("decompress_val, bad switch: %d",tvp->comp);
323 get_tcb_field(_TCBVAR *tvp,unsigned char *buf)
325 assert(tvp->hi-tvp->lo+1<=64);
326 assert(tvp->hi>=tvp->lo);
328 tvp->rawval=get_tcb_bits(buf,tvp->lo,tvp->hi);
329 /* assume no compression and 32-bit value for now */
330 tvp->val=(unsigned) (tvp->rawval & 0xFFFFFFFF);
336 /***:-----------------------------------------------------------------------
338 ***:-----------------------------------------------------------------------
342 spr_tcp_state (unsigned state)
346 if ( 0 == state) {ret = "CLOSED";}
347 else if ( 1 == state) {ret = "LISTEN";}
348 else if ( 2 == state) {ret = "SYN_SENT";}
349 else if ( 3 == state) {ret = "SYN_RCVD";}
350 else if ( 4 == state) {ret = "ESTABLISHED";}
351 else if ( 5 == state) {ret = "CLOSE_WAIT";}
352 else if ( 6 == state) {ret = "FIN_WAIT_1";}
353 else if ( 7 == state) {ret = "CLOSING";}
354 else if ( 8 == state) {ret = "LAST_ACK";}
355 else if ( 9 == state) {ret = "FIN_WAIT_2";}
356 else if (10 == state) {ret = "TIME_WAIT";}
357 else if (11 == state) {ret = "ESTABLISHED_RX";}
358 else if (12 == state) {ret = "ESTABLISHED_TX";}
359 else if (13 == state) {ret = "SYN_PEND";}
360 else if (14 == state) {ret = "ESC_1_STATE";}
361 else if (15 == state) {ret = "ESC_2_STATE";}
367 spr_cctrl_sel(unsigned sel0,unsigned sel1)
369 unsigned sel=(sel1<<1) | sel0;
372 if ( 0 == sel) {ret = "Reno";}
373 else if ( 1 == sel) {ret = "Tahoe";}
374 else if ( 2 == sel) {ret = "NewReno";}
375 else if ( 3 == sel) {ret = "HighSpeed";}
382 spr_ulp_type(unsigned ulp_type)
386 /*The tp.h PM_MODE_XXX call 1 DDP and 5 IANDP, but external
387 * documentation (tcb.h" calls 5 ddp, and doesn't mention 1 or 3.
390 if ( PM_MODE_PASS == ulp_type) {ret = "TOE";}
391 else if ( PM_MODE_DDP == ulp_type) {ret = "DDP";}
392 else if ( PM_MODE_ISCSI == ulp_type) {ret = "ISCSI";}
393 else if ( PM_MODE_IWARP == ulp_type) {ret = "IWARP";}
394 else if ( PM_MODE_RDDP == ulp_type) {ret = "RDMA";}
395 else if ( PM_MODE_IANDP == ulp_type) {ret = "IANDP_DDP";}
396 else if ( PM_MODE_FCOE == ulp_type) {ret = "FCoE";}
397 else if ( PM_MODE_USER == ulp_type) {ret = "USER";}
398 else if ( PM_MODE_TLS == ulp_type) {ret = "TLS";}
399 else if ( PM_MODE_DTLS == ulp_type) {ret = "DTLS";}
405 spr_ip_version(unsigned ip_version)
409 if ( 0 == ip_version) {ret = "IPv4";}
410 else if ( 1 == ip_version) {ret = "IPv6";}
417 /***:-----------------------------------------------------------------------
419 ***:-----------------------------------------------------------------------
423 display_tcb_compressed(_TCBVAR *tvp,int aux)
427 t4_display_tcb_aux_0(tvp,aux);
428 if (1==aux) t4_display_tcb_aux_1(tvp,aux);
429 else if (2==aux) t4_display_tcb_aux_2(tvp,aux);
430 else if (3==aux) t4_display_tcb_aux_3(tvp,aux);
432 } else if (g_tN==5) {
433 t5_display_tcb_aux_0(tvp,aux);
434 if (1==aux) t5_display_tcb_aux_1(tvp,aux);
435 else if (2==aux) t5_display_tcb_aux_2(tvp,aux);
436 else if (3==aux) t5_display_tcb_aux_3(tvp,aux);
437 } else if (g_tN==6) {
438 t6_display_tcb_aux_0(tvp,aux);
439 if (1==aux) t6_display_tcb_aux_1(tvp,aux);
440 else if (2==aux) t6_display_tcb_aux_2(tvp,aux);
441 else if (3==aux) t6_display_tcb_aux_3(tvp,aux);
442 else if (4==aux) t6_display_tcb_aux_4(tvp,aux);
449 /***:-----------------------------------------------------------------------
450 ***: parse_n_decode_tcb
451 ***:-----------------------------------------------------------------------
456 parse_tcb( _TCBVAR *base_tvp, unsigned char *buf)
457 { /* parse the TCB */
458 _TCBVAR *tvp=base_tvp;
460 int aux=1; /* assume TOE or iSCSI */
461 unsigned tx_max=0, rcv_nxt=0, rx_frag0_start_idx_raw=0;
462 int got_tx_max=0, got_rcv_nxt=0, got_rx_frag0_start_idx_raw=0;
466 while (tvp->name!=NULL) {
467 get_tcb_field(tvp,buf);
468 if (!got_tx_max && tcb_strmatch_nc("tx_max",tvp->name)) {
472 if (!got_rcv_nxt && tcb_strmatch_nc("rcv_nxt",tvp->name)) {
476 if (!got_rx_frag0_start_idx_raw &&
477 tcb_strmatch_nc("rx_frag0_start_idx_raw",tvp->name)) {
478 rx_frag0_start_idx_raw=tvp->val;
479 got_rx_frag0_start_idx_raw=1;
485 ulp_type=tvp->val; /* ULP type is always first variable in TCB */
486 if (PM_MODE_IANDP==ulp_type || PM_MODE_FCOE==ulp_type) aux=3;
487 else if (PM_MODE_RDDP==ulp_type) aux=2;
488 else if (6==g_tN && (PM_MODE_TLS==ulp_type || PM_MODE_DTLS==ulp_type)) aux=4;
491 assert(got_tx_max && got_rcv_nxt && got_rx_frag0_start_idx_raw);
493 /* decompress the compressed values */
495 while (tvp->name!=NULL) {
496 decompress_val(tvp,ulp_type,tx_max,rcv_nxt,rx_frag0_start_idx_raw);
506 parse_scb( _TCBVAR *base_tvp, unsigned char *buf)
507 { /* parse the SCB */
508 _TCBVAR *tvp=base_tvp;
510 while (tvp->name!=NULL) {
511 if (tcb_strmatch_nc("scb_slush",tvp->name)) {
512 /* the scb_slush field is all of remaining memory */
516 get_tcb_field(tvp,buf);
524 parse_fcb( _TCBVAR *base_tvp, unsigned char *buf)
525 { /* parse the FCB */
526 _TCBVAR *tvp=base_tvp;
528 while (tvp->name!=NULL) {
529 get_tcb_field(tvp,buf);
536 display_list_tcb(_TCBVAR *base_tvp,int aux)
538 _TCBVAR *tvp=base_tvp;
539 while (tvp->name!=NULL) {
540 if (tvp->aux==0 || tvp->aux==aux) {
541 if (tvp->hi-tvp->lo+1<=32) {
542 printf(" %4d:%4d %31s: %10u (0x%1x)",tvp->lo,tvp->hi,tvp->name,
543 (unsigned) tvp->rawval,(unsigned) tvp->rawval);
544 if (COMP_TX_MAX==tvp->comp || COMP_RCV_NXT==tvp->comp)
545 printf(" -> %1u (0x%x)", tvp->val,tvp->val);
547 printf(" %4d:%4d %31s: 0x%1llx",tvp->lo,tvp->hi,tvp->name,
557 display_tcb(_TCBVAR *tvp,unsigned char *buf,int aux)
559 if (g_prntstyl==PRNTSTYL_VERBOSE ||
560 g_prntstyl==PRNTSTYL_RAW) {
561 tcb_hexdump(0,buf,128);
565 if (g_prntstyl==PRNTSTYL_VERBOSE ||
566 g_prntstyl==PRNTSTYL_LIST) {
567 display_list_tcb(tvp,aux);
570 if (g_prntstyl==PRNTSTYL_VERBOSE ||
571 g_prntstyl==PRNTSTYL_COMP) {
572 display_tcb_compressed(tvp,aux);
578 parse_n_display_tcb(unsigned char *buf)
580 _TCBVAR *tvp=g_tcb_info;
583 aux=parse_tcb(tvp,buf);
584 display_tcb(tvp,buf,aux);
588 parse_n_display_scb(unsigned char *buf)
590 _TCBVAR *tvp=g_scb_info;
593 if (g_prntstyl==PRNTSTYL_VERBOSE ||
594 g_prntstyl==PRNTSTYL_RAW) {
595 tcb_hexdump(0,buf,128);
598 if (g_prntstyl==PRNTSTYL_VERBOSE ||
599 g_prntstyl==PRNTSTYL_LIST ||
600 g_prntstyl==PRNTSTYL_COMP) {
601 display_list_tcb(tvp,0);
606 parse_n_display_fcb(unsigned char *buf)
608 _TCBVAR *tvp=g_fcb_info;
611 if (g_prntstyl==PRNTSTYL_VERBOSE ||
612 g_prntstyl==PRNTSTYL_RAW) {
613 tcb_hexdump(0,buf,128);
617 if (g_prntstyl==PRNTSTYL_VERBOSE ||
618 g_prntstyl==PRNTSTYL_LIST ||
619 g_prntstyl==PRNTSTYL_COMP) {
620 display_list_tcb(tvp,0);
625 parse_n_display_xcb(unsigned char *buf)
627 if (g_got_scb) parse_n_display_scb(buf);
628 else if (g_got_fcb) parse_n_display_fcb(buf);
629 else parse_n_display_tcb(buf);
632 /***:-----------------------------------------------------------------------
634 ***:-----------------------------------------------------------------------
638 swizzle_tcb(unsigned char *buf)
642 for (i=0, j=128-16 ; i<j ; i+=16, j-=16) {
644 for (k=0; k<16; ++k) {
653 /***:-----------------------------------------------------------------------
654 ***: END OF WINDOWS FUNCTIONS
655 ***:-----------------------------------------------------------------------
658 void set_tidtype(unsigned int tidtype)
660 if (tidtype == TIDTYPE_SCB)
664 else if (tidtype == TIDTYPE_FCB)
677 set_tcb_info(unsigned int tidtype, unsigned int cardtype)
679 set_tidtype(tidtype);
683 g_tcb_info = g_tcb_info4;
684 g_scb_info = g_scb_info4;
685 g_fcb_info = g_fcb_info4;
687 else if (5 == g_tN) {
688 g_tcb_info = g_tcb_info5;
689 g_scb_info = g_scb_info5;
690 g_fcb_info = g_fcb_info5;
692 else if (6 == g_tN) {
693 g_tcb_info = g_tcb_info6;
694 g_scb_info = g_scb_info6;
695 g_fcb_info = g_fcb_info6;
700 set_print_style(unsigned int prntstyl)