1 /*
2  * Copyright (c) 2006-2007 The Regents of the University of California.
3  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
4  * Copyright (c) 2002-2010 Mellanox Technologies LTD. All rights reserved.
5  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6  * Copyright (c) 2009 HNR Consulting. All rights reserved.
7  * Copyright (c) 2011 Lawrence Livermore National Security. All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  *
37  */
38 
39 #ifndef _IBDIAG_COMMON_H_
40 #define _IBDIAG_COMMON_H_
41 
42 #include <stdarg.h>
43 #include <infiniband/mad.h>
44 #include <infiniband/iba/ib_types.h>
45 #include <infiniband/ibnetdisc.h>
46 
47 extern int ibverbose;
48 extern char *ibd_ca;
49 extern int ibd_ca_port;
50 extern enum MAD_DEST ibd_dest_type;
51 extern ib_portid_t *ibd_sm_id;
52 extern int ibd_timeout;
53 extern uint32_t ibd_ibnetdisc_flags;
54 extern uint64_t ibd_mkey;
55 extern uint64_t ibd_sakey;
56 extern int show_keys;
57 extern char *ibd_nd_format;
58 
59 /*========================================================*/
60 /*                External interface                      */
61 /*========================================================*/
62 
63 #undef DEBUG
64 #define DEBUG(fmt, ...) do { \
65 	if (ibdebug) IBDEBUG(fmt, ## __VA_ARGS__); \
66 } while (0)
67 #define VERBOSE(fmt, ...) do { \
68 	if (ibverbose) IBVERBOSE(fmt, ## __VA_ARGS__); \
69 } while (0)
70 #define IBEXIT(fmt, ...) ibexit(__FUNCTION__, fmt, ## __VA_ARGS__)
71 
72 #define NOT_DISPLAYED_STR "<not displayed>"
73 
74 /* not all versions of ib_types.h will have this define */
75 #ifndef IB_PM_PC_XMIT_WAIT_SUP
76 #define IB_PM_PC_XMIT_WAIT_SUP (CL_HTON16(((uint16_t)1)<<12))
77 #endif
78 
79 /* PM ClassPortInfo CapabilityMask Bits */
80 #ifndef IS_PM_RSFEC_COUNTERS_SUP
81 #define IS_PM_RSFEC_COUNTERS_SUP (CL_HTON16(((uint16_t)1)<<14))
82 #endif
83 
84 #ifndef IB_PM_IS_QP1_DROP_SUP
85 #define IB_PM_IS_QP1_DROP_SUP (CL_HTON16(((uint16_t)1)<<15))
86 #endif
87 
88 /* PM ClassPortInfo CapabilityMask2 Bits */
89 #ifndef IB_PM_IS_ADDL_PORT_CTRS_EXT_SUP
90 #define IB_PM_IS_ADDL_PORT_CTRS_EXT_SUP (CL_HTON32(((uint32_t)1)<<1))
91 #endif
92 
93 /* SM PortInfo CapabilityMask2 Bits */
94 #ifndef IB_PORT_CAP2_IS_PORT_INFO_EXT_SUPPORTED
95 #define IB_PORT_CAP2_IS_PORT_INFO_EXT_SUPPORTED (CL_HTON16(0x0002))
96 #endif
97 
98 /* SM PortInfoExtended Fec Mode Bits */
99 #ifndef IB_PORT_EXT_NO_FEC_MODE_ACTIVE
100 #define IB_PORT_EXT_NO_FEC_MODE_ACTIVE 0
101 #endif
102 
103 #ifndef IB_PORT_EXT_FIRE_CODE_FEC_MODE_ACTIVE
104 #define IB_PORT_EXT_FIRE_CODE_FEC_MODE_ACTIVE (CL_HTON16(0x0001))
105 #endif
106 
107 #ifndef IB_PORT_EXT_RS_FEC_MODE_ACTIVE
108 #define IB_PORT_EXT_RS_FEC_MODE_ACTIVE (CL_HTON16(0x0002))
109 #endif
110 
111 #ifndef IB_PORT_EXT_LOW_LATENCY_RS_FEC_MODE_ACTIVE
112 #define IB_PORT_EXT_LOW_LATENCY_RS_FEC_MODE_ACTIVE (CL_HTON16(0x0003))
113 #endif
114 
115 /* SM PortInfoExtended CapabilityMask Bits */
116 #ifndef IB_PORT_EXT_CAP_IS_FEC_MODE_SUPPORTED
117 #define IB_PORT_EXT_CAP_IS_FEC_MODE_SUPPORTED (CL_HTON32(0x00000001))
118 #endif
119 
120 struct ibdiag_opt {
121 	const char *name;
122 	char letter;
123 	unsigned has_arg;
124 	const char *arg_tmpl;
125 	const char *description;
126 };
127 
128 extern int ibdiag_process_opts(int argc, char *const argv[], void *context,
129 			       const char *exclude_common_str,
130 			       const struct ibdiag_opt custom_opts[],
131 			       int (*custom_handler) (void *cxt, int val,
132 						      char *optarg),
133 			       const char *usage_args,
134 			       const char *usage_examples[]);
135 extern void ibdiag_show_usage();
136 extern void ibexit(const char *fn, char *msg, ...);
137 
138 /* convert counter values to a float with a unit specifier returned (using
139  * binary prefix)
140  * "data" is a flag indicating this counter is a byte counter multiplied by 4
141  * as per PortCounters[Extended]
142  */
143 extern char *conv_cnt_human_readable(uint64_t val64, float *val, int data);
144 
145 int is_mlnx_ext_port_info_supported(uint32_t vendorid, uint16_t devid);
146 
147 int is_port_info_extended_supported(ib_portid_t * dest, int port,
148 				    struct ibmad_port *srcport);
149 void get_max_msg(char *width_msg, char *speed_msg, int msg_size,
150 		 ibnd_port_t * port);
151 
152 int resolve_sm_portid(char *ca_name, uint8_t portnum, ib_portid_t *sm_id);
153 int resolve_self(char *ca_name, uint8_t ca_port, ib_portid_t *portid,
154                  int *port, ibmad_gid_t *gid);
155 int resolve_portid_str(char *ca_name, uint8_t ca_port, ib_portid_t * portid,
156 		       char *addr_str, enum MAD_DEST dest_type,
157 		       ib_portid_t *sm_id, const struct ibmad_port *srcport);
158 int vsnprint_field(char *buf, size_t n, enum MAD_FIELDS f, int spacing,
159 		   const char *format, va_list va_args);
160 int snprint_field(char *buf, size_t n, enum MAD_FIELDS f, int spacing,
161 		  const char *format, ...);
162 void dump_portinfo(void *pi, int tabs);
163 
164 /**
165  * Some common command line parsing
166  */
167 typedef char *(op_fn_t) (ib_portid_t * dest, char **argv, int argc);
168 
169 typedef struct match_rec {
170 	const char *name, *alias;
171 	op_fn_t *fn;
172 	unsigned opt_portnum;
173 	char *ops_extra;
174 } match_rec_t;
175 
176 op_fn_t *match_op(const match_rec_t match_tbl[], char *name);
177 
178 #endif				/* _IBDIAG_COMMON_H_ */
179