1 /** @file fb_lib.h
2  *
3  * @brief Main libfb include file
4  */
5 #ifdef HAVE_CONFIG_H
6 # include <libfb/fb_config.h>
7 #endif
8 
9 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
10 # include <string.h>
11 #endif
12 
13 #ifdef HAVE_STDBOOL_H
14 # include <stdbool.h>
15 #endif
16 
17 #ifdef HAVE_INTTYPES_H
18 # include <inttypes.h>
19 #endif
20 
21 #if defined(TM_IN_SYS_TIME)
22 # if HAVE_SYS_TIME_H
23 #   include <sys/time.h>
24 # endif
25 #elif defined(HAVE_TIME_H)
26 # include <time.h>
27 #endif
28 
29 #if defined(HAVE_ARPA_INET_H)
30 # include <arpa/inet.h>
31 #elif defined(HAVE_NETINET_IN_H)
32 # include <netinet/in.h>
33 #endif
34 
35 #if HAVE_LIBNET_H
36 # include <libnet.h>
37 #endif
38 
39 #if HAVE_PCAP_H
40 # include <pcap.h>
41 #endif
42 
43 #if defined(HAVE_STDIO_H)
44 # include <stdio.h>
45 #endif
46 
47 #include "./libfb/fb_context.h"
48 
49 #ifdef VERSION
50 # define FBLIB_VER VERSION
51 #endif
52 
53 #define MAC_NUM 2	    /**< The max number of MAC/PHY pairs */
54 #define TDM_STREAM_COUNT 6  /**< The number of TDM streams */
55 #define IDT_LINKS 4	    /**< The maximum number of spans */
56 
57 #define CRCPOLY 0x1021	    /**< Polynomial used in CRC16 generation */
58 
59 /** @brief Skeleton 0xD00F packet */
60 typedef struct
61 {
62   /** foneBRIDGE firmware command */
63   uint8_t cmd;
64   /** paramaters to the command */
65   uint8_t param;
66   /** Length of payload in bytes */
67   uint16_t len;
68   /** CRC16 of the structure with the crc and reserved fields considered */
69   uint16_t crc;
70   /** reserved for future use */
71   unsigned char reserved[2];
72 } __attribute__ ((packed)) DOOF;
73 
74 #define DOOF_PL_OFF 8		/**< Offset from DOOF header to payload */
75 
76 /**
77  * @brief Raw Ethernet packet for 0xD00F configuration
78  *
79  * For the single unsigned char values we use a bitmask to represent
80  * each span. So if you have four E1 spans, E1 = 00001111, in
81  * binary. If you have two E1 and two T1 then E1 = 00001100 in
82  * binary. Except for LBO and MAC all the fields work this way.
83  *
84  * @deprecated used only by raw ethernet layer
85  */
86 typedef volatile struct
87 {
88   /** number of spans being configures */
89   unsigned char numSpan;
90   /** E1 bitmask */
91   unsigned char E1;
92   /** RBS bitmask */
93   unsigned char RBS;
94   /** CLK bitmask */
95   unsigned char CLK;
96   /** AMI bitmask */
97   unsigned char AMI;
98   /** ESF bitmask */
99   unsigned char ESF;
100   /** ETH bitmask */
101   unsigned char ETH;
102   /** CRCMF bitmask */
103   unsigned char CRCMF;
104   /** LBO configuration for each of 4 spans */
105   unsigned char LBO[4];
106   unsigned char mac[4][6];
107 } DOOF_CFG;
108 
109 /* DOOF Commands */
110 #define DOOF_CMD_RECONFIG 1		  /**< Reconfigure FB Mode */
111 #define DOOF_CMD_READ_BLK 2		  /**< Read a block from Flash */
112 #define DOOF_CMD_WRITE_BLK 3		  /**< Write a block to flash */
113 #define DOOF_CMD_GET_DIAG 4		  /**< Get diagnostic info */
114 #define DOOF_CMD_STOP_FLOW 5		  /**< Stop TDMOE flow */
115 #define DOOF_CMD_START_WRITE 6		  /**< Start write into EPCS device from buffer */
116 #define DOOF_CMD_GET_STATIC_INFO 7	  /**< Get static information */
117 #define DOOF_CMD_WRITE_DSP 9		  /**< Write a block of words to the DSP */
118 #define DOOF_CMD_READ_DSP 10		  /**< Read a block of words from the DSP */
119 #define DOOF_CMD_SDRAM_TEST 11		  /**< SDRAM Test? */
120 #define DOOF_CMD_RESET 12		  /**< Reset external devices */
121 #define DOOF_CMD_READ_MEM 13		  /**< Read any location in SOPC memory */
122 #define DOOF_CMD_TDM_CTL 14		  /**< Control TDM Cores individually */
123 #define DOOF_CMD_GPAK_READ_FIFO 15	  /**< Read a FIFO event from the GPAK API */
124 #define DOOF_CMD_EC_CHAN_TYPE 16	  /**< Set the EC chan type to either data/voice (0=data,1=voice0,2=voice1,3=teardown) */
125 #define DOOF_CMD_EC_COMP_TYPE 17	  /**< Set the companding type on the EC */
126 #define DOOF_CMD_SPI_READREG 18		  /**< Read a group of register from SPI device */
127 #define DOOF_CMD_SPI_WRITEREG 19	  /**< Write a group of registers to SPI device */
128 #define DOOF_CMD_TDMOE_TXCTL 20		  /**< Enable/disable TDMOE TX */
129 #define DOOF_CMD_TDMOE_DSTMAC 21	  /**< Set the dest. MAC for TDMOE */
130 #define DOOF_CMD_PRINT_PERF 22		  /**< Print perf. status to stdout */
131 #define DOOF_CMD_TDM_LB_SEL 23		  /**< Enable/disable TDM4-5 Loopback */
132 #define DOOF_CMD_DSP_INIT 24		  /**< DSP init. */
133 #define DOOF_CMD_GET_GPAK_FLASH_PARMS 25  /**< Get GPAK Flash parms structure */
134 #define DOOF_CMD_GET_DSP_USAGE 26	  /**< Retrive the CPU usage of the DSP */
135 #define DOOF_GET_STATS 27
136 #define DOOF_GET_RBS 28
137 #define DOOF_RBS_SPY_CTL 29		  /**< Control RBS spying */
138 #define DOOF_CMD_SETIP 30		  /**< Set the IP address / recalc CRC */
139 #define DOOF_CMD_LFSR_CHK 31		  /**< Enable LFSR checking for indicated TDM stream */
140 #define DOOF_CMD_TDM_CTLSTAT 32		  /**< Set/clear bits in CTLSTAT register */
141 #define DOOF_CMD_EC_SETPARM 33		  /**< Set certain parameters related to EC (sysparms) */
142 #define DOOF_CMD_SET_PRIORITY 34	  /**< Set the priority of each span */
143 #define DOOF_CMD_IDT_WRITE_REG 35	  /**< Write IDT register based on span field */
144 #define DOOF_CMD_READ_IDT_REG 36	  /**< Read IDT registers */
145 #define DOOF_CMD_TDM_FECTL 37		  /**< Set/clr bits in TDM FE register */
146 #define DOOF_CMD_TDM_REGCTL 38		  /**< Set/clr/read values in the TDM Cores */
147 #define DOOF_CMD_KEY_WRITE 41		  /**< Write customer key */
148 #define DOOF_CMD_CLKSEL_PIO 43
149 #define DOOF_CMD_TEMP 50
150 
151 #define DOOF_CMD_EC_SETPARM_TAPLEN 0
152 #define DOOF_CMD_EC_SETPARM_ADAPT_FREQ 1
153 #define DOOF_CMD_EC_SETPARM_FIR_SEGS 2
154 #define DOOF_CMD_EC_SETPARM_FIR_SEGLEN 3
155 #define DOOF_CMD_EC_SETPARM_COMP_TYPE 4
156 
157 /* CTLSTAT register bit defines */
158 #define TDM_CTLSTAT_TXEN (1<<0)		 /**< Transmission enable */
159 #define TDM_CTLSTAT_RXEN (1<<1)		 /**< Reception enable */
160 #define TDM_CTLSTAT_TXIE (1<<2)		 /**< TX interrupt enable */
161 #define TDM_CTLSTAT_RXIE (1<<3)		 /**< RX interrupt enable */
162 #define TDM_CTLSTAT_TXIF (1<<4)		 /**< TX interrupt flag */
163 #define TDM_CTLSTAT_RXIF (1<<5)		 /**< RX interrupt flag */
164 #define TDM_CTLSTAT_RLB (1<<6)		 /**< Remote loopback - loops PCM DOUT (from FE) to PCM DIN */
165 #define TDM_CTLSTAT_TX_STATIC (1<<7)	 /**< Transmit static byte flag */
166 #define TDM_CTLSTAT_INTERL (1<<8)	 /**< Data interleaved flag */
167 #define TDM_CTLSTAT_PRBS (1<<9)		 /**< Replaces a rotating PRBS value in place of msr_readdata */
168 #define TDM_CTLSTAT_PRBS_CNT (1<<10)	 /**< Turns the PRBS mode into a channel counter */
169 #define TDM_CTLSTAT_PRBS_DET (1<<11)	 /**< PRBS detect */
170 /* REGCTL commands */
171 #define DOOF_CMD_TDM_REGCTL_NOP 0
172 #define DOOF_CMD_TDM_REGCTL_SET 1
173 #define DOOF_CMD_TDM_REGCTL_CLR 2
174 #define DOOF_CMD_TDM_REGCTL_FORCE 3
175 /* TDM Core register offsets */
176 #define DOOF_TDM_OFF_CTLSTAT 0
177 #define DOOF_TDM_OFF_RXBD 1
178 #define DOOF_TDM_OFF_TXBD 2
179 #define DOOF_TDM_OFF_TXSTATIC 3
180 #define DOOF_TDM_OFF_CHAN_NUM 4
181 #define DOOF_TDM_OFF_PRBS_ERR 5
182 #define DOOF_TDM_OFF_FE_CTL 6
183 #define DOOF_TDM_OFF_MC 7
184 
185 #define DOOF_BLK_HDR_LEN 8
186 
187 /** Default chip size - 16 Mbit = 2 Mbyte */
188 #define EPCS_SPAN (2*(1<<20))
189 /** EPCS block size */
190 #define EPCS_BLK_SIZE (1<<16)
191 
192 /** Timeout period while waiting for packets, in seconds */
193 #define FBLIB_POLLTIMEOUT       10
194 
195 /** Error conditions that can be reported by the libfb library */
196 typedef enum
197 {
198   FBLIB_ESUCCESS = 0,		/**< Success */
199   FBLIB_EAGAIN,			/**< Poll failed */
200   FBLIB_ETIMEDOUT,		/**< Time out waiting for packet */
201   FBLIB_EERRNO,			/**< Check errno */
202   FBLIB_EINVAL,			/**< Invalid arguments */
203   FBLIB_EBYTECOUNT,		/**< Received incorrect packet length */
204   FBLIB_EMAXIMUMS,		/**< Exceeded maximum (threads, sockets...) */
205   FBLIB_EEXTLIB,		/**< External library error */
206   FBLIB_EHERRNO,		/**< Check h_errno */
207   FBLIB_EMAX
208 } fblib_err;
209 
210 /** Error conditions that can be reported by the device */
211 typedef enum
212 {
213   DOOF_RESP_NOERROR = 0,	/**< Success */
214   DOOF_RESP_CRCFAIL = 1,	/**< CRC Check Failed */
215   DOOF_RESP_NOMEM,		/**< No Memory Available */
216   DOOF_RESP_NOCMD,		/**< Invalid Command */
217   DOOF_RESP_OOB,		/**< Out of Bounds */
218   DOOF_RESP_NODEV,		/**< No such device */
219   DOOF_RESP_BADSIZE,		/**< Bad packet length */
220   DOOF_RESP_BADPARM,		/**< Bad parameter specified */
221   DOOF_RESP_MAXERROR
222 } doof_err;
223 
224 extern const char *fberrstr[];
225 extern const char *dooferrstr[];
226 
227 /** Convert a libfb error into a string representation */
228 #define FBERR_STR(errnum)   "libfb: %s\n", fberrstr[errnum]
229 /** Convert a device error into a string representation */
230 #define DOOFERR_STR(errnum) "libfb: %s\n", dooferrstr[errnum]
231 
232 
233 /** @brief Print a libfb error message to stderr
234  *
235  * Nothing happens if the error is not a real error code or if the
236  * error is FBLIB_ESUCCESS
237  */
238 #define FBERR_PRINT_IF_FAIL(errnum)           \
239 do {                                          \
240   if (errnum > FBLIB_ESUCCESS &&              \
241       errnum < FBLIB_EMAX )                   \
242 	 fprintf(stderr, FBERR_STR(errnum));  \
243   else if (errnum != FBLIB_ESUCCESS)          \
244    fprintf(stderr, "libfb: Unknown Error\n"); \
245 } while (0)
246 
247 /** @brief Print a device error message to stderr
248  *
249  * Nothing happens if the error is not a real error code or if the
250  * error is DOOF_RESP_NOERROR
251  */
252 #define DOOFERR_PRINT_IF_FAIL(errnum)         \
253 do {                                          \
254   if (errnum > DOOF_RESP_NOERROR &&           \
255       errnum < DOOF_RESP_MAXERROR )           \
256          fprintf(stderr, DOOFERR_STR(errnum));\
257   else if (errnum != DOOF_RESP_NOERROR)       \
258    fprintf(stderr,                            \
259      "libfb: Unknown remote device error\n"); \
260 } while (0)
261 
262 /** @brief Print an error message to stderr
263  *
264  * Negative error codes are mapped to DOOF_RESP_* (device) errors and
265  * positive errors are mapped to libfb errors. If the error code is
266  * '0' or if the error code is not a valid error then nothing is
267  * printed.
268  */
269 #define PRINT_MAPPED_ERROR_IF_FAIL(errnum)    \
270 do {                                          \
271   if (errnum < 0)                             \
272     DOOFERR_PRINT_IF_FAIL(-errnum);           \
273   else if (errnum > 0)                        \
274     FBERR_PRINT_IF_FAIL(errnum);              \
275 } while (0)
276 
277 /** @brief Header for DOOF_BLK packets */
278 typedef struct
279 {
280   /** Start address */
281   uint32_t addr;
282   /** Length of payload */
283   uint16_t len;
284 } __attribute__ ((packed)) DOOF_BLK;
285 
286 /** uLaw (mu-Law) companding type for GPAK DSP */
287 #define DSP_COMP_TYPE_ULAW 3
288 /** A-law companding type for GPAK DSP */
289 #define DSP_COMP_TYPE_ALAW 4
290 
291 /* LBO PULS[3:0] Settings */
292 #define PULS_LBO3 (0x09)	/**< -22.5 dB */
293 #define PULS_LBO2 (0x0A)	/**< -15.0 dB */
294 #define PULS_LBO1 (0x0B)	/**<  -7.5 dB */
295 #define PULS_LBO0 (0x02)	/**<   0.0 dB */
296 
297 
298 #define MAX_LONGLBO 4		/**< The number of valid longhaul LBO
299 				   settings */
300 extern const unsigned char longlbo[MAX_LONGLBO];
301 
302 /* Preset Templates */
303 #define PULS_655  (0x6)		/**< 533 ~ 655 ft */
304 #define PULS_533  (0x5)		/**< 399 ~ 533 ft */
305 #define PULS_399  (0x4)		/**< 266 ~ 399 ft */
306 #define PULS_266  (0x3)		/**< 133 ~ 266 ft */
307 #define PULS_133  (0x2)		/**<   0 ~ 133 ft */
308 #define PULS_J1   (0x2)		/**< J1: 0~655 ft */
309 
310 #define MAX_SHORTLBO 5		/**< The number of valid shorthaul LBO
311 				   settings */
312 extern const unsigned char shortlbo[MAX_SHORTLBO];
313 
314 /* deprecated span_mode mask flags */
315 #define SPAN_MODE_EQ    (1<<7) /**< equalizer on */
316 #define SPAN_MODE_RLB   (1<<6) /**< remote loopback */
317 #define SPAN_MODE_CRCMF (1<<5) /**< crcmf on */
318 #define SPAN_MODE_RBS   (1<<4) /**< rbs enabled */
319 #define SPAN_MODE_HDLC  (1<<3)
320 #define SPAN_MODE_E1    (1<<2) /**< E1 mode */
321 #define SPAN_MODE_ESF   (1<<1) /**< ESF mode */
322 #define SPAN_MODE_AMI   (1<<0) /**< AMI framing */
323 
324 #define MAC_SZ     6	       /**< MAC address length in bytes */
325 #define SERIAL_SZ 16	       /**< Serial number length in bytes */
326 
327 typedef unsigned char MAC_ADDR[MAC_SZ];	   /**< MAC address typedef */
328 typedef unsigned char SERIAL[SERIAL_SZ];   /**< Serial number typedef */
329 
330 /** @brief DSP subsystem (software) information
331  *
332  * Represents the detected number of channels, the number of active
333  * (configured) channels, and versioning information about the GPAK
334  * software.
335  *
336  * @attention <b>It is absolutely critical that these structures
337  * maintain an EIGHT BYTE alignment.</b>
338  */
339 typedef struct
340 {
341   /** The maximum number of channels the hardware DSP supports, this should be '128' if the GPAK software was loaded correctly */
342   uint8_t max_channels;
343   /** The number of 'running' (properly configured) channels  */
344   uint8_t active_channels;
345   uint8_t stream_slots[2];
346   uint8_t stream_supported_slots[2];
347   uint8_t bist;
348   uint8_t num_ec;
349   /** GPAK software version */
350   uint32_t ver;
351   /** Reserved fields */
352   uint8_t res[4];
353 } __attribute__ ((packed)) GPAK_SYS_CONFIG;
354 
355 /** @brief Persistant configuration information about the device that is stored in EPCS flash
356  * @details Sizeof EPCS_CONFIG is currently 256 bytes
357  */
358 typedef struct
359 {
360   /** The device's first MAC address */
361   unsigned char mac_addr[MAC_SZ];
362   /** Two IP addresses represented in big endian */
363   uint32_t ip_address[2];
364   /** Bitmask representing configuration flags applicable to the device.
365    * @details Documentation of valid flags is specified in firmware source code.
366    */
367   unsigned char cfg_flags;
368   /** reserved field */
369   unsigned char res;
370   /** a cfg_flag used to mark a device as an inline echo canceller
371    * @details 0 = Fonebridge mode (TDMOE), 1 = IEC mode
372    */
373 #define CFG_FLAGS_IEC_EN (1<<0)
374 
375   /** 32 bit word representing the manufacturing date
376    *  @details Number of seconds since Jan 1st 2000 in network byte order. Offset 16 .
377    */
378   uint32_t mfg_date;
379 
380   /** Serial number of the device */
381   SERIAL snumber;		/* Off. 20 */
382 
383   /** Length in bytes of the stored GPAK file (0 if none) */
384   uint32_t gpak_len;		/* Off. 36 */
385   unsigned char attempted_boots;	/* Off. 40 - Attempted boots */
386   /** Reserved fields */
387   unsigned char res2[85];	/* Off. 40 */
388   /** Reserved fields */
389   unsigned char res3[128];	/* Off. 126 */
390   /** CRC16 checksum of this structure, less the crc16 field */
391   uint16_t crc16;		/* Off. 254 */
392 } __attribute__ ((packed)) EPCS_CONFIG;
393 
394 /** @brief Static device information
395  *
396  * Current sizeof(DOOF_STATIC_INFO) is 352 bytes. The structure is
397  * 'static' in the sense that none of this information is changable by
398  * software. It is determined by the firmware and the physical
399  * hardware's configuration.
400  */
401 typedef volatile struct
402 {
403   /** software version string as reported by firmware */
404   uint8_t sw_ver[16];
405   /** software compile date string as reported by firmware */
406   uint8_t sw_compile_date[32];	/* Off. 16 */
407   /** 16 bit word representing the FB Verilog core version */
408   uint16_t fb_core_version;	/* Off. 48 */
409   /** 16 bit word representing the build number in firmware */
410   uint16_t build_num;
411   /** The number of spans found on the device */
412   uint8_t spans;		/* Off. 52 */
413   /** The number of T1/E1/J1 ICs found in the device. One IC can contain mutiple spans (transceivers.) */
414   uint8_t devices;
415   /** The number of MAC/PHYs on the device. */
416   uint8_t mac_num;
417   /** Reserved fields */
418   uint8_t res2;
419 
420   /** 16 bit word representing the number of flash blocks */
421   uint16_t epcs_blocks;		/* Off. 56 */
422   /** Reserved fields */
423   uint8_t res3[6];
424   /** 32 bit word representing the size in bytes of each EPCS block */
425   uint32_t epcs_block_size;	/* Off. 64 */
426 
427   /** 32 bit word representing the size of the entire EPCS flash,
428       i.e. region size is blocks size times the number of blocks */
429   uint32_t epcs_region_size;	/* Off. 68 */
430 
431   /** EPCS configuration structure which must be aligned to 8-byte boundary */
432   EPCS_CONFIG epcs_config;	/* Off. 72 */
433 
434   /** 32 bit word representing the FPGA system id tag */
435   uint32_t fpga_sysid;		/* Off. 328 */
436   /** 32 bit word representing the system id tag timestamp */
437   uint32_t fpga_timestamp;	/* Off. 332 */
438 
439   /** GPAK configuration data */
440   GPAK_SYS_CONFIG gpak_config;	/* Off. 336 */
441 } __attribute__ ((packed)) DOOF_STATIC_INFO;
442 
443 
444 /** @brief Convenience structure for representing flash sizes */
445 typedef struct
446 {
447   unsigned short int epcs_blocks;
448   int epcs_block_size;
449   int epcs_region_size;
450 } T_EPCS_INFO;
451 
452 /** @brief Modern transceiver configuration information
453  *
454  * Each transceiver (span) is configured with one IDT_LINK_CONFIG data
455  * structure. The values are generally all binary, although they are
456  * transmitted as 8-bit words.
457  */
458 typedef struct
459 {
460   /** Configure span as an E1, 0 = T1/J1, 1 = E1 */
461   uint8_t E1Mode;
462   /** Configure span as a J1, 0 = T1, 1 = J1 (if E1Mode = 0) */
463   uint8_t J1Mode;
464   /** Enables ESF framing if true or SF framing if false. Valid for T1s only. */
465   uint8_t framing;
466   /** Enables AMI encoding if true. When false default is B8ZS (T1) or HDB3 (E1) */
467   uint8_t encoding;
468   /** Enables RBS functions in the hardware cores, used for T1 RBS and E1 CAS */
469   uint8_t rbs_en;
470   /** Enables the CRC Multiframe if true (only valid for E1) */
471   uint8_t CRCMF;
472   /** Places span into "remote loopback mode" if true */
473   uint8_t rlb;
474   /** Enables automatic equalizer if true, used for longhaul spans */
475   uint8_t EQ;
476   /** Sets the line build out, see the PULS_XXX defines for allowed values */
477   uint8_t LBO;
478   /** Enables/Disables TX And RX HDLC */
479   uint8_t HDLC;
480   /** Reserved fields */
481   uint8_t res[6];
482 } IDT_LINK_CONFIG;
483 
484 /** @brief Device statistics */
485 typedef struct
486 {
487   /** Number of packets received on each MAC */
488   uint32_t mac_rxcnt[MAC_NUM];
489   /** Number of packets transmitted on each MAC */
490   uint32_t mac_txcnt[MAC_NUM];
491   /** Number of dropped packets on each MAC */
492   uint32_t mac_rxdrop[MAC_NUM];
493   /** System clock ticks */
494   uint8_t ticks[8];
495   /** LFSR checking errors for each TDM stream, only vaild when an LFSR check/test is running */
496   uint32_t lfsr_err[TDM_STREAM_COUNT];
497   uint32_t lfsr_cnt[TDM_STREAM_COUNT];
498   /** Reserved fields */
499   unsigned char res[32][4];
500 } __attribute__ ((packed)) DOOF_STATS;
501 
502 /** @brief DSP configuration
503  *
504  * @details Represents the properties and current configuration of the
505  * DSP and the GPAK software on a device
506  */
507 typedef struct
508 {
509   /** The DSP channel configuration for all 128 supported channels.
510    *
511    * The possibilities are:
512    * <ul>
513    *  <li>Data channel (no echo cancelling): 0</li>
514    *  <li>Cancel in direction 'A': 1</li>
515    *  <li>Cancel in direction 'B': 2</li>
516    *  <li>Disable data transmission: 3</li>
517    * </ul>
518    */
519   uint8_t dsp_chan_type[128];
520   /** The companding type used on the channel, one of DSP_COMP_TYPE_ALAW or DSP_COMP_TYPE_ULAW */
521   uint8_t dsp_companding_type;
522   uint8_t taplen;
523   uint8_t adapt_freq;
524   uint8_t fir_segs;
525   uint8_t fir_seglen;
526   /** Reserved fields */
527   uint8_t res[3];
528 } GPAK_FLASH_PARMS;
529 
530 /* Customer Key Data */
531 /** The length of the "secret" customer key */
532 #define CUSTOMER_KEY_SZ 32
533 /** Length of the hashed key */
534 #define HASH_KEY_SZ     20
535 /** Length of the salt used */
536 #define SALT_SZ          2
537 /** Length of the base random seed generated */
538 #define BASE_SEED_SZ    32
539 /** Total seed side */
540 #define SEED_SZ         (BASE_SEED_SZ + MAC_SZ + SERIAL_SZ  + SALT_SZ)
541 
542 /** @brief A single securty key */
543 typedef struct
544 {
545   /** The one-way hash of the customer key */
546   unsigned char hash_key[HASH_KEY_SZ];
547   /** The "secret" customer key */
548   unsigned char customer_key[CUSTOMER_KEY_SZ];
549   /** CRC16 of the fields, less the crc16 field */
550   uint16_t crc16;
551 } __attribute__ ((packed)) KEY_ENTRY;
552 
553 /* End Key */
554 
555 /** @brief Representation of a PMON register
556  *
557  * At a minimum first_address and the two length fields must be set
558  * correctly. Data is a pointer to a location of at least size length_bytes
559  * that may be used for storing the data held in the physical register.
560  *
561  * length_bits is used by PMON reading functions to mask off reserved
562  * fields.
563  *
564  */
565 typedef struct libfb_PMONRegister_
566 {
567   char *name; /**< Optional register name */
568   char *longname; /**< Optional description */
569 
570   /** First_address is where the first 8 bits are found. We assume
571    *  that bits found after the first 8 are found in sequentially
572    *  increasing registers. For example if register 0x12 contains the
573    *  first 8 bits ([7:0]) then we expect register 0x13 to contain the
574    *  next 8 ([15:8])
575    */
576 
577   uint8_t first_address;
578 
579   uint8_t length_bits; /**< Length of data in bits. */
580   uint8_t length_bytes;	/**< Length of data in smallest number of bytes. */
581 
582   uint8_t *data; /**< Location to store read data */
583 
584 } libfb_PMONRegister;
585 
586 
587 extern const libfb_PMONRegister libfb_regs_T1ESF[];
588 
589 extern const libfb_PMONRegister libfb_regs_T1SF[];
590 
591 extern const libfb_PMONRegister libfb_regs_E1[];
592 
593 
594 
595 
596 /***** Function prototypes ******/
597 /* get_local_mac.c: This file must be ported for each target OS */
598 u_int8_t *get_local_mac (char *nicname);
599 
600 /* ethernet.c */
601 fblib_err send_ethernet (libfb_t * f, u_int8_t * dst_mac, u_int8_t * payload,
602 			 u_int32_t len);
603 fblib_err send_doof (libfb_t * f, u_int8_t * dst_mac, u_int8_t * buf,
604 		     u_int16_t buflen, u_int8_t cmd, u_int8_t param);
605 int recv_paacket (libfb_t * f, u_int8_t * buffer);
606 uint16_t recv_doof (libfb_t * f, u_int8_t * buffer);
607 fblib_err config_fb (libfb_t * f, unsigned char *span_mode,
608 		     unsigned char *dest_mac, unsigned char fb_mac[][6]);
609 fblib_err config_fb_allspan_off (libfb_t * f, unsigned char *dest_mac);
610 int doof_txrx (libfb_t * f, u_int8_t * dstmac, u_int8_t * packet,
611 	       u_int8_t cmd, u_int8_t param, u_int16_t len,
612 	       u_int8_t * recv_buf);
613 
614 /* fb_lib.c */
615 void libfb_printver ();
616 int get_static_info (libfb_t * f, unsigned char *dest_mac,
617 		     DOOF_STATIC_INFO * doof_info);
618 int get_epcs_pointer (libfb_t * f, unsigned char *dest_mac,
619 		      DOOF_STATIC_INFO * ptr);
620 void print_static_info (libfb_t * f, DOOF_STATIC_INFO * packet_in);
621 libfb_t *libfb_init (char *device, int ethernet, char *errstr);
622 fblib_err libfb_connect (libfb_t * f, const char *host, int port);
623 fblib_err libfb_destroy (libfb_t * f);
624 inline time_t libfb_get_ctime (libfb_t * f);
625 inline time_t libfb_get_systime (libfb_t * f);
626 void libfb_setcrc_on (libfb_t * f);
627 void libfb_setcrc_off (libfb_t * f);
628 bool libfb_getcrc (libfb_t * f);
629 u_int8_t *libfb_getsrcmac (libfb_t * f);
630 void set_reftime (libfb_t * f);
631 fblib_err libfb_updat_pmon (libfb_t * f, uint8_t span);
632 fblib_err libfb_readidt_pmon (libfb_t * f, uint8_t span, uint8_t address,
633 			      uint8_t * data);
634 fblib_err writeidt (libfb_t * f, unsigned char span, uint8_t address,
635 		    uint8_t data);
636 fblib_err readidt (libfb_t * f, unsigned char span, unsigned int address,
637 		   size_t len, char *charbuf);
638 
639 
640 /* flash.c */
641 fblib_err udp_read_blk (libfb_t * f, uint32_t address, uint16_t len,
642 			uint8_t * buffer);
643 fblib_err udp_write_to_blk (libfb_t * f, uint32_t address, uint16_t len,
644 			    uint8_t * buffer);
645 fblib_err udp_start_blk_write (libfb_t * f, uint32_t address);
646 int write_to_blk (libfb_t * f, unsigned char *dest_mac, int address,
647 		  int length, unsigned char *buffer);
648 int start_blk_write (libfb_t * f, unsigned char *dest_mac, int address);
649 int read_blk (libfb_t * f, unsigned char *dest_mac, int address, int length,
650 	      unsigned char *recv_buf);
651 
652 /* utility.c */
653 unsigned short crc_16 (unsigned char *buf, int len);
654 u_int16_t grab16 (const volatile u_int8_t * src);
655 u_int32_t grab32 (const volatile u_int8_t * src);
656 void store16 (u_int16_t val, u_int8_t * dst);
657 void store32 (u_int32_t val, u_int8_t * dst);
658 void print_current_time (FILE * output);
659 void fprint_ip (FILE * stream, uint32_t ip);
660 void print_ip (uint32_t ip);
661 void fprint_mac (FILE * output, const volatile unsigned char *mac);
662 void print_mac (const volatile unsigned char *mac);
663 int parse_mac (char *src_mac, unsigned char *dst_mac);
664 void print_span_mode_idtlink (IDT_LINK_CONFIG link, FILE * output);
665 void print_span_mode (unsigned char mode, FILE * output);
666 void libfb_write_seed (uint8_t * buffer);
667 int libfb_fprint_key (FILE * stream, KEY_ENTRY * key);
668 
669 /** Build feature idenfitication macros */
670 /** @enum FEATURE
671  *
672  * @brief Possible feature sets that are available on the target device.
673  *
674  * FEATURE_PRE_2_0: Only feature sets prior to hardware version 2.0. This refers to the old priority scheme where '1' meant master and '0' meant slave.
675  *
676  * FEATURE_2_0: Only feature sets available in hardware version 2.0
677  * are supported. This refers to the priority scheme where '0' means
678  * slave to line and values '1-3' represent a priority indication.
679  *
680  */
681 typedef enum
682 { FEATURE_PRE_2_0, FEATURE_2_0, FEATURE_MAX } FEATURE;
683 
684 const uint16_t buildnum_featureset[FEATURE_MAX];
685 
686 #define IS_FEATURE_2_0(x)           ((x >= buildnum_featureset[FEATURE_2_0]) ? 1 : 0)
687 #define IS_FEATURE_PRE_2_0(x)       ((x <= buildnum_featureset[FEATURE_PRE_2_0]) ? 1 : 0)
688 
689 FEATURE libfb_feature_set (DOOF_STATIC_INFO *);
690 
691 /* unsorted so far */
692 int tx_packet (unsigned char *dest_mac, unsigned char *buffer, int len);
693 int rx_packet (unsigned char *buffer);
694 int rx_packet_sock (int socket, unsigned char *buffer);
695 void fprint_static_info (libfb_t * f, FILE * stream,
696 			 DOOF_STATIC_INFO * packet_in);
697 fblib_err udp_get_static_info (libfb_t * f, DOOF_STATIC_INFO * dsi);
698 fblib_err readdsp (libfb_t * f, unsigned int address, size_t len,
699 		   unsigned int *intbuf);
700 fblib_err writedsp (libfb_t * f, unsigned int address, size_t len,
701 		    unsigned int *intbuf);
702 fblib_err ec_set_chantype (libfb_t * f, unsigned char type, uint32_t * mask);
703 fblib_err custom_cmd (libfb_t * f, unsigned char cmd, unsigned char param,
704 		      char *buf, size_t len);
705 fblib_err custom_cmd_reply (libfb_t * f, unsigned char cmd,
706 			    unsigned char param, char *buf, size_t len,
707 			    char *rbuf, size_t rlen);
708 fblib_err readmem (libfb_t * f, unsigned int address, size_t len,
709 		   char *charbuf);
710 fblib_err readmem32 (libfb_t * f, unsigned int address, size_t len,
711 		     uint32_t * intbuf);
712 fblib_err readspi (libfb_t * f, unsigned char dev, unsigned char address,
713 		   size_t len, char *charbuf);
714 fblib_err writespi (libfb_t * f, unsigned char dev, unsigned char address,
715 		    size_t len, char *charbuf);
716 fblib_err config_fb_udp (libfb_t * f, unsigned char *span_mode);
717 fblib_err config_fb_udp_lbo (libfb_t * f, unsigned char *span_mode,
718 			     unsigned char *puls);
719 fblib_err config_fb_udp_linkconfig (libfb_t * f, IDT_LINK_CONFIG *);
720 int parseMac (unsigned char *dst, char *mac);
721 fblib_err fblib_get_gpakparms (libfb_t * f, GPAK_FLASH_PARMS * buf);
722 fblib_err configcheck_fb_udp (libfb_t * f, IDT_LINK_CONFIG * link_cfg);
723 
724 
725 /** poll.c **/
726 bool udp_ready_read (libfb_t * f);
727 bool udp_ready_write (libfb_t * f);
728 fblib_err poll_for_newpkt (libfb_t * f);
729