xref: /openbmc/u-boot/cmd/aspeed/nettest/ncsi.c (revision 3ba98ed8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) ASPEED Technology Inc.
4  */
5 
6 #define NCSI_C
7 
8 #include "swfunc.h"
9 
10 #include "comminf.h"
11 //#include "io.h"
12 #include "ncsi.h"
13 #include <command.h>
14 #include <common.h>
15 #include "mac_api.h"
16 
17 //------------------------------------------------------------
18 int ncsi_set_error_flag(MAC_ENGINE *eng, int eflag)
19 {
20 	eng->flg.ncsi = eng->flg.ncsi | eflag;
21 	eng->flg.error = eng->flg.error | ERR_FLAG_NCSI_LINKFAIL;
22 	if (DBG_PRINT_ERR_FLAG)
23 		printf("\nflags: error = %08x, ncsi = %08x\n",
24 		       eng->flg.error, eng->flg.ncsi);
25 
26 	return (1);
27 }
28 
29 //------------------------------------------------------------
30 // PHY IC(NC-SI)
31 //------------------------------------------------------------
32 void ncsi_reqdump(MAC_ENGINE *eng, NCSI_Command_Packet *in)
33 {
34 	int     i;
35 	PRINTF( FP_LOG, "[NCSI-Request] DA             : %02x %02x %02x %02x %02x %02x\n", in->DA[ 0 ], in->DA[ 1 ], in->DA[ 2 ], in->DA[ 3 ], in->DA[ 4 ] , in->DA[ 5 ]);
36 	PRINTF( FP_LOG, "[NCSI-Request] SA             : %02x %02x %02x %02x %02x %02x\n", in->SA[ 0 ], in->SA[ 1 ], in->SA[ 2 ], in->SA[ 3 ], in->SA[ 4 ] , in->SA[ 5 ]);
37 	PRINTF( FP_LOG, "[NCSI-Request] EtherType      : %04x\n", SWAP_2B_BEDN( in->EtherType )             );//DMTF NC-SI
38 	PRINTF( FP_LOG, "[NCSI-Request] MC_ID          : %02x\n", in->MC_ID                                 );//Management Controller should set this field to 0x00
39 	PRINTF( FP_LOG, "[NCSI-Request] Header_Revision: %02x\n", in->Header_Revision                       );//For NC-SI 1.0 spec, this field has to set 0x01
40 //	PRINTF( FP_LOG, "[NCSI-Request] Reserved_1     : %02x\n", in->Reserved_1                            ); //Reserved has to set to 0x00
41 	PRINTF( FP_LOG, "[NCSI-Request] IID            : %02x\n", in->IID                                   );//Instance ID
42 	PRINTF( FP_LOG, "[NCSI-Request] Command        : %02x\n", in->Command                               );
43 	PRINTF( FP_LOG, "[NCSI-Request] ChID           : %02x\n", in->ChID                                  );
44 	PRINTF( FP_LOG, "[NCSI-Request] Payload_Length : %04x\n", SWAP_2B_BEDN( in->Payload_Length )        );//Payload Length = 12 bits, 4 bits are reserved
45 //	PRINTF( FP_LOG, "[NCSI-Request] Reserved_2     : %04x\n", in->Reserved_2                            );
46 //	PRINTF( FP_LOG, "[NCSI-Request] Reserved_3     : %04x\n", in->Reserved_3                            );
47 //	PRINTF( FP_LOG, "[NCSI-Request] Reserved_4     : %04x\n", in->Reserved_4                            );
48 //	PRINTF( FP_LOG, "[NCSI-Request] Reserved_5     : %04x\n", in->Reserved_5                            );
49 	PRINTF( FP_LOG, "[NCSI-Request] Response_Code  : %04x\n", SWAP_2B_BEDN( in->Response_Code )         );
50 	PRINTF( FP_LOG, "[NCSI-Request] Reason_Code    : %04x\n", SWAP_2B_BEDN( in->Reason_Code )           );
51 	for ( i = 0; i < SWAP_2B_BEDN( in->Payload_Length ); i++ ) {
52 		switch ( i % 4 ) {
53 			case 0	: PRINTF( FP_LOG, "[NCSI-Request] Payload_Data   : %02x", in->Payload_Data[ i ]); break;
54 			case 3	: PRINTF( FP_LOG, " %02x\n", in->Payload_Data[ i ]); break;
55 			default	: PRINTF( FP_LOG, " %02x", in->Payload_Data[ i ]); break;
56 		}
57 	}
58 	if ( ( i % 4 ) != 3 )
59 		PRINTF( FP_LOG, "\n");
60 }
61 void ncsi_respdump (MAC_ENGINE *eng, NCSI_Response_Packet *in) {
62 	int     i;
63 //	PRINTF( FP_LOG, "[NCSI-Respond] DA             : %02x %02x %02x %02x %02x %02x\n", in->DA[ 5 ], in->DA[ 4 ], in->DA[ 3 ], in->DA[ 2 ], in->DA[ 1] , in->DA[ 0 ]);
64 //	PRINTF( FP_LOG, "[NCSI-Respond] SA             : %02x %02x %02x %02x %02x %02x\n", in->SA[ 5 ], in->SA[ 4 ], in->SA[ 3 ], in->SA[ 2 ], in->SA[ 1] , in->SA[ 0 ]);
65 	PRINTF( FP_LOG, "[NCSI-Respond] DA             : %02x %02x %02x %02x %02x %02x\n", in->DA[ 0 ], in->DA[ 1 ], in->DA[ 2 ], in->DA[ 3 ], in->DA[ 4 ] , in->DA[ 5 ]);
66 	PRINTF( FP_LOG, "[NCSI-Respond] SA             : %02x %02x %02x %02x %02x %02x\n", in->SA[ 0 ], in->SA[ 1 ], in->SA[ 2 ], in->SA[ 3 ], in->SA[ 4 ] , in->SA[ 5 ]);
67 	PRINTF( FP_LOG, "[NCSI-Respond] EtherType      : %04x\n", SWAP_2B_BEDN( in->EtherType )             );//DMTF NC-SI
68 	PRINTF( FP_LOG, "[NCSI-Respond] MC_ID          : %02x\n", in->MC_ID                                 );//Management Controller should set this field to 0x00
69 	PRINTF( FP_LOG, "[NCSI-Respond] Header_Revision: %02x\n", in->Header_Revision                       );//For NC-SI 1.0 spec, this field has to set 0x01
70 //	PRINTF( FP_LOG, "[NCSI-Respond] Reserved_1     : %02x\n", in->Reserved_1                            ); //Reserved has to set to 0x00
71 	PRINTF( FP_LOG, "[NCSI-Respond] IID            : %02x\n", in->IID                                   );//Instance ID
72 	PRINTF( FP_LOG, "[NCSI-Respond] Command        : %02x\n", in->Command                               );
73 	PRINTF( FP_LOG, "[NCSI-Respond] ChID           : %02x\n", in->ChID                                  );
74 	PRINTF( FP_LOG, "[NCSI-Respond] Payload_Length : %04x\n", SWAP_2B_BEDN( in->Payload_Length )        );//Payload Length = 12 bits, 4 bits are reserved
75 //	PRINTF( FP_LOG, "[NCSI-Respond] Reserved_2     : %04x\n", in->Reserved_2                            );
76 //	PRINTF( FP_LOG, "[NCSI-Respond] Reserved_3     : %04x\n", in->Reserved_3                            );
77 //	PRINTF( FP_LOG, "[NCSI-Respond] Reserved_4     : %04x\n", in->Reserved_4                            );
78 //	PRINTF( FP_LOG, "[NCSI-Respond] Reserved_5     : %04x\n", in->Reserved_5                            );
79 	PRINTF( FP_LOG, "[NCSI-Respond] Response_Code  : %04x\n", SWAP_2B_BEDN( in->Response_Code )         );
80 	PRINTF( FP_LOG, "[NCSI-Respond] Reason_Code    : %04x\n", SWAP_2B_BEDN( in->Reason_Code )           );
81 	for ( i = 0; i < SWAP_2B_BEDN( in->Payload_Length ); i++ ) {
82 		switch ( i % 4 ) {
83 			case 0	: PRINTF( FP_LOG, "[NCSI-Respond] Payload_Data   : %02x", in->Payload_Data[ i ]); break;
84 			case 3	: PRINTF( FP_LOG, " %02x\n", in->Payload_Data[ i ]); break;
85 			default	: PRINTF( FP_LOG, " %02x", in->Payload_Data[ i ]); break;
86 		}
87 	}
88 	if ( ( i % 4 ) != 3 )
89 		PRINTF( FP_LOG, "\n");
90 }
91 
92 //------------------------------------------------------------
93 void NCSI_PrintCommandStr (MAC_ENGINE *eng, unsigned char command, unsigned iid) {
94 	switch ( command & 0x80 ) {
95 		case 0x80   : sprintf(eng->dat.NCSI_CommandStr, "IID:%3d [%02x:Respond]", iid, command); break;
96 		default     : sprintf(eng->dat.NCSI_CommandStr, "IID:%3d [%02x:Request]", iid, command); break;
97 	}
98 	switch ( command & 0x7f ) {
99 		case 0x00   : sprintf(eng->dat.NCSI_CommandStr, "%s[CLEAR_INITIAL_STATE                ]", eng->dat.NCSI_CommandStr); break;
100 		case 0x01   : sprintf(eng->dat.NCSI_CommandStr, "%s[SELECT_PACKAGE                     ]", eng->dat.NCSI_CommandStr); break;
101 		case 0x02   : sprintf(eng->dat.NCSI_CommandStr, "%s[DESELECT_PACKAGE                   ]", eng->dat.NCSI_CommandStr); break;
102 		case 0x03   : sprintf(eng->dat.NCSI_CommandStr, "%s[ENABLE_CHANNEL                     ]", eng->dat.NCSI_CommandStr); break;
103 		case 0x04   : sprintf(eng->dat.NCSI_CommandStr, "%s[DISABLE_CHANNEL                    ]", eng->dat.NCSI_CommandStr); break;
104 		case 0x05   : sprintf(eng->dat.NCSI_CommandStr, "%s[RESET_CHANNEL                      ]", eng->dat.NCSI_CommandStr); break;
105 		case 0x06   : sprintf(eng->dat.NCSI_CommandStr, "%s[ENABLE_CHANNEL_NETWORK_TX          ]", eng->dat.NCSI_CommandStr); break;
106 		case 0x07   : sprintf(eng->dat.NCSI_CommandStr, "%s[DISABLE_CHANNEL_NETWORK_TX         ]", eng->dat.NCSI_CommandStr); break;
107 		case 0x08   : sprintf(eng->dat.NCSI_CommandStr, "%s[AEN_ENABLE                         ]", eng->dat.NCSI_CommandStr); break;
108 		case 0x09   : sprintf(eng->dat.NCSI_CommandStr, "%s[SET_LINK                           ]", eng->dat.NCSI_CommandStr); break;
109 		case 0x0A   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_LINK_STATUS                    ]", eng->dat.NCSI_CommandStr); break;
110 		case 0x0B   : sprintf(eng->dat.NCSI_CommandStr, "%s[SET_VLAN_FILTER                    ]", eng->dat.NCSI_CommandStr); break;
111 		case 0x0C   : sprintf(eng->dat.NCSI_CommandStr, "%s[ENABLE_VLAN                        ]", eng->dat.NCSI_CommandStr); break;
112 		case 0x0D   : sprintf(eng->dat.NCSI_CommandStr, "%s[DISABLE_VLAN                       ]", eng->dat.NCSI_CommandStr); break;
113 		case 0x0E   : sprintf(eng->dat.NCSI_CommandStr, "%s[SET_MAC_ADDRESS                    ]", eng->dat.NCSI_CommandStr); break;
114 		case 0x10   : sprintf(eng->dat.NCSI_CommandStr, "%s[ENABLE_BROADCAST_FILTERING         ]", eng->dat.NCSI_CommandStr); break;
115 		case 0x11   : sprintf(eng->dat.NCSI_CommandStr, "%s[DISABLE_BROADCAST_FILTERING        ]", eng->dat.NCSI_CommandStr); break;
116 		case 0x12   : sprintf(eng->dat.NCSI_CommandStr, "%s[ENABLE_GLOBAL_MULTICAST_FILTERING  ]", eng->dat.NCSI_CommandStr); break;
117 		case 0x13   : sprintf(eng->dat.NCSI_CommandStr, "%s[DISABLE_GLOBAL_MULTICAST_FILTERING ]", eng->dat.NCSI_CommandStr); break;
118 		case 0x14   : sprintf(eng->dat.NCSI_CommandStr, "%s[SET_NCSI_FLOW_CONTROL              ]", eng->dat.NCSI_CommandStr); break;
119 		case 0x15   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_VERSION_ID                     ]", eng->dat.NCSI_CommandStr); break;
120 		case 0x16   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_CAPABILITIES                   ]", eng->dat.NCSI_CommandStr); break;
121 		case 0x17   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_PARAMETERS                     ]", eng->dat.NCSI_CommandStr); break;
122 		case 0x18   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_CONTROLLER_PACKET_STATISTICS   ]", eng->dat.NCSI_CommandStr); break;
123 		case 0x19   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_NCSI_STATISTICS                ]", eng->dat.NCSI_CommandStr); break;
124 		case 0x1A   : sprintf(eng->dat.NCSI_CommandStr, "%s[GET_NCSI_PASS_THROUGH_STATISTICS   ]", eng->dat.NCSI_CommandStr); break;
125 		case 0x50   : sprintf(eng->dat.NCSI_CommandStr, "%s[OEM_COMMAND                        ]", eng->dat.NCSI_CommandStr); break;
126 		default     : sprintf(eng->dat.NCSI_CommandStr, "%s Not Support Command", eng->dat.NCSI_CommandStr); break ;
127 	}
128 } // End void NCSI_PrintCommandStr (MAC_ENGINE *eng, unsigned char command, unsigned iid)
129 
130 //------------------------------------------------------------
131 void NCSI_PrintCommandType (MAC_ENGINE *eng, unsigned char command, unsigned iid) {
132 	NCSI_PrintCommandStr( eng, command, iid );
133 	printf("[NCSI-commd]%s\n", eng->dat.NCSI_CommandStr);
134 }
135 
136 //------------------------------------------------------------
137 void NCSI_PrintCommandType2File (MAC_ENGINE *eng, unsigned char command, unsigned iid) {
138 	NCSI_PrintCommandStr( eng, command, iid );
139 	PRINTF( FP_LOG, "[NCSI-commd]%s\n", eng->dat.NCSI_CommandStr );
140 }
141 
142 //------------------------------------------------------------
143 void NCSI_Struct_Initialize_SLT (MAC_ENGINE *eng)
144 {
145 	int i;
146 	uint32_t NCSI_RxDatBase;
147 
148 	eng->run.NCSI_RxTimeOutScale = 1;
149 
150 	for (i = 0; i < 6; i++) {
151 		eng->ncsi_req.DA[i] = 0xFF;
152 		eng->ncsi_req.SA[i] = eng->inf.SA[i];
153 	}
154 
155 	/* EtherType = 0x88F8 (DMTF DSP0222 NC-SI spec)
156 	ethernet frame header format, table 8 */
157 	eng->ncsi_req.EtherType = SWAP_2B_BEDN(0x88F8);
158 
159 	eng->ncsi_req.MC_ID           = 0;
160 	eng->ncsi_req.Header_Revision = 0x01;
161 	eng->ncsi_req.Reserved_1      = 0;
162 	eng->ncsi_req.IID             = 0;
163 //	eng->ncsi_req.Command         = 0;
164 //	eng->ncsi_req.ChID            = 0;
165 //	eng->ncsi_req.Payload_Length  = 0;
166 
167 	eng->ncsi_req.Response_Code   = 0;
168 	eng->ncsi_req.Reason_Code     = 0;
169 	eng->ncsi_req.Reserved_2      = 0;
170 	eng->ncsi_req.Reserved_3      = 0;
171 
172 	eng->dat.NCSI_TxByteBUF = (unsigned char *) &eng->dat.NCSI_TxDWBUF[0];
173 	eng->dat.NCSI_RxByteBUF = (unsigned char *) &eng->dat.NCSI_RxDWBUF[0];
174 
175 	eng->run.ncsi_tdes_base = eng->run.tdes_base;//base for read/write
176 	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x04, 0                        );
177 	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x08, 0                        );
178 	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x0C, DMA_BASE - ASPEED_DRAM_BASE);
179 
180 	eng->run.ncsi_rdes_base = eng->run.rdes_base;//base for read/write
181 	NCSI_RxDatBase = AT_MEMRW_BUF( NCSI_RxDMA_BASE );//base of the descriptor
182 
183 	for (i = 0; i < NCSI_RxDESNum - 1; i++) {
184 		Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base        ), 0x00000000     );
185 		Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x04 ), 0x00000000     );
186 		Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x08 ), 0x00000000     );
187 		Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x0C ), NCSI_RxDatBase );
188 		eng->run.ncsi_rdes_base += 16;
189 		NCSI_RxDatBase += NCSI_RxDMA_PakSize;
190 	}
191 	Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base        ), EOR_IniVal     );
192 	Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x04 ), 0x00000000     );
193 	Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x08 ), 0x00000000     );
194 //	Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x0C ), (NCSI_RxDatBase + CPU_BUS_ADDR_SDRAM_OFFSET) ); // 20130730
195 	Write_Mem_Des_NCSI_DD( ( eng->run.ncsi_rdes_base + 0x0C ), NCSI_RxDatBase ); // 20130730
196 
197 	eng->run.ncsi_rdes_base = eng->run.rdes_base;//base for read/write
198 }
199 
200 //------------------------------------------------------------
201 void Calculate_Checksum_NCSI (MAC_ENGINE *eng, unsigned char *buffer_base, int Length) {
202 	uint32_t      CheckSum = 0;
203 	uint32_t      Data;
204 	uint32_t      Data1;
205 	int        i;
206 
207 	// Calculate checksum is from byte 14 of ethernet Haeder and Control packet header
208 	// Page 50, NC-SI spec. ver. 1.0.0 form DMTF
209 	for (i = 14; i < Length; i += 2 ) {
210 		Data      = buffer_base[i];
211 		Data1     = buffer_base[i + 1];
212 		CheckSum += ((Data << 8) + Data1);
213 	}
214 	eng->dat.Payload_Checksum_NCSI = SWAP_4B_BEDN(~(CheckSum) + 1); //2's complement
215 }
216 
217 /**
218  * @brief	check error mask in RX descriptor
219  * @param	rx_desc0	RX descript[0]
220 */
221 static int check_rx_desc_err(MAC_ENGINE *p_eng, uint32_t rx_desc0)
222 {
223 	uint8_t prefix[8] = "[RxDes]";
224 
225 	if (rx_desc0 & RXDES_EM_ALL) {
226 #ifdef CheckRxErr
227 		if (rx_desc0 & RXDES_EM_RXERR) {
228 			PRINTF(STD_OUT, "%s Error RxErr        %08x\n", prefix,
229 			       rx_desc0);
230 			p_eng->dat.NCSI_RxEr = 1;
231 		}
232 #endif
233 
234 #ifdef CheckCRC
235 		if (rx_desc0 & RXDES_EM_CRC) {
236 			PRINTF(STD_OUT, "%s Error CRC          %08x\n", prefix,
237 			       rx_desc0);
238 			FindErr_Des(p_eng, Des_Flag_CRC);
239 		}
240 #endif
241 
242 #ifdef CheckFTL
243 		if (rx_desc0 & RXDES_EM_FTL) {
244 			PRINTF(STD_OUT, "%s Error FTL          %08x\n", prefix,
245 			       rx_desc0);
246 			FindErr_Des(p_eng, Des_Flag_FTL);
247 		}
248 #endif
249 
250 #ifdef CheckRunt
251 		if (rx_desc0 & RXDES_EM_RUNT) {
252 			PRINTF(STD_OUT, "%s Error Runt         %08x\n", prefix,
253 			       rx_desc0);
254 			FindErr_Des(p_eng, Des_Flag_Runt);
255 		}
256 #endif
257 
258 #ifdef CheckOddNibble
259 		if (rx_desc0 & RXDES_EM_ODD_NB) {
260 			PRINTF(STD_OUT, "%s Odd Nibble         %08x\n", prefix,
261 			       rx_desc0);
262 			FindErr_Des(p_eng, Des_Flag_OddNibble);
263 		}
264 #endif
265 
266 #ifdef CheckRxFIFOFull
267 		if (rx_desc0 & RXDES_EM_FIFO_FULL) {
268 			PRINTF(STD_OUT, "%s Error Rx FIFO Full %08x\n", prefix,
269 			       rx_desc0);
270 			FindErr_Des(p_eng, Des_Flag_RxFIFOFull);
271 		}
272 #endif
273 	}
274 
275 	return 0;
276 }
277 
278 //------------------------------------------------------------
279 // return 0: it is PASS
280 // return 1: it is FAIL
281 //------------------------------------------------------------
282 char NCSI_Rx_SLT (MAC_ENGINE *eng) {
283 	int        timeout = 0;
284 	int        bytesize;
285 	int        dwsize;
286 	int        i;
287 	int        retry   = 0;
288 	char       ret     = 1;
289 
290 	uint32_t      NCSI_RxDatBase;
291 	uint32_t      NCSI_RxDesDat;
292 	uint32_t      NCSI_RxData;
293 	uint32_t      NCSI_BufData;
294 
295 	do {
296 		mac_reg_write( eng, 0x1C, 0x00000000 );//Rx Poll
297 
298 		timeout = 0;
299 		do {
300 			NCSI_RxDesDat = Read_Mem_Des_NCSI_DD( eng->run.ncsi_rdes_base );
301 			if ( ++timeout > TIME_OUT_NCSI * eng->run.NCSI_RxTimeOutScale ) {
302 				PRINTF( FP_LOG, "[RxDes] DesOwn timeout     %08x\n", NCSI_RxDesDat );
303 				return( FindErr( eng, Err_Flag_NCSI_Check_RxOwnTimeOut ) );
304 			}
305 		} while( HWOwnRx( NCSI_RxDesDat ) );
306 
307 		check_rx_desc_err(eng, NCSI_RxDesDat);
308 
309 		// Get point of RX DMA buffer
310 		NCSI_RxDatBase = AT_BUF_MEMRW( Read_Mem_Des_NCSI_DD( eng->run.ncsi_rdes_base + 0x0C ) );//base for read/write
311 		NCSI_RxData    = SWAP_4B_LEDN_NCSI( SWAP_4B_LEDN( Read_Mem_Dat_NCSI_DD( NCSI_RxDatBase + 0x0C ) ) );
312 
313 		// Get RX valid data in offset 00h of RXDS#0
314 #ifdef NCSI_Skip_RxCRCData
315 		bytesize  = (NCSI_RxDesDat & 0x3fff) - 4;
316 #else
317 		bytesize  = (NCSI_RxDesDat & 0x3fff);
318 #endif
319 		// Fill up to multiple of 4
320 		if ( ( bytesize % 4 ) != 0 )
321 			dwsize = ( bytesize >> 2 ) + 1;
322 		else
323 			dwsize = bytesize >> 2;
324 
325 		if ( eng->arg.ctrl.b.print_ncsi ) {
326 #ifdef NCSI_Skip_RxCRCData
327 			PRINTF( FP_LOG ,"----->[Rx] %d bytes(%xh) [Remove CRC data]\n", bytesize, bytesize );
328 #else
329 			PRINTF( FP_LOG ,"----->[Rx] %d bytes(%xh)\n", bytesize, bytesize );
330 #endif
331 			for (i = 0; i < dwsize - 1; i++) {
332 				NCSI_BufData = SWAP_4B_LEDN_NCSI( Read_Mem_Dat_NCSI_DD( NCSI_RxDatBase + ( i << 2 ) ) );
333 				PRINTF( FP_LOG ,"      [Rx]%02d:%08x %08x\n", i, NCSI_BufData, SWAP_4B( NCSI_BufData ) );
334 			}
335 
336 			i = ( dwsize - 1 );
337 			NCSI_BufData = SWAP_4B_LEDN_NCSI( Read_Mem_Dat_NCSI_DD( NCSI_RxDatBase + ( i << 2 ) ) );
338 			switch ( bytesize % 4 ) {
339 				case 0  : PRINTF( FP_LOG ,"      [Rx]%02d:%08x %08x\n",                          i, NCSI_BufData & SWAP_4B_LEDN_NCSI( 0xffffffff ), SWAP_4B( NCSI_BufData ) & SWAP_4B_BEDN_NCSI( 0xffffffff ) ); break;
340 				case 3  : PRINTF( FP_LOG ,"      [Rx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, NCSI_BufData & SWAP_4B_LEDN_NCSI( 0x00ffffff ), SWAP_4B( NCSI_BufData ) & SWAP_4B_BEDN_NCSI( 0x00ffffff ), NCSI_BufData, SWAP_4B( NCSI_BufData ), SWAP_4B_LEDN_NCSI( 0x00ffffff ), SWAP_4B_BEDN_NCSI( 0x00ffffff ) ); break;
341 				case 2  : PRINTF( FP_LOG ,"      [Rx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, NCSI_BufData & SWAP_4B_LEDN_NCSI( 0x0000ffff ), SWAP_4B( NCSI_BufData ) & SWAP_4B_BEDN_NCSI( 0x0000ffff ), NCSI_BufData, SWAP_4B( NCSI_BufData ), SWAP_4B_LEDN_NCSI( 0x0000ffff ), SWAP_4B_BEDN_NCSI( 0x0000ffff ) ); break;
342 				case 1  : PRINTF( FP_LOG ,"      [Rx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, NCSI_BufData & SWAP_4B_LEDN_NCSI( 0x000000ff ), SWAP_4B( NCSI_BufData ) & SWAP_4B_BEDN_NCSI( 0x000000ff ), NCSI_BufData, SWAP_4B( NCSI_BufData ), SWAP_4B_LEDN_NCSI( 0x000000ff ), SWAP_4B_BEDN_NCSI( 0x000000ff ) ); break;
343 				default : PRINTF( FP_LOG ,"      [Rx]%02d:error", i ); break;
344 			}
345 		}
346 
347 		// EtherType field of the response packet should be 0x88F8
348 //
349 		if ( ( NCSI_RxData & 0xffff ) == 0xf888 ) {
350 			for (i = 0; i < dwsize; i++)
351 				eng->dat.NCSI_RxDWBUF[i] = SWAP_4B_LEDN_NCSI( Read_Mem_Dat_NCSI_DD( NCSI_RxDatBase + ( i << 2 ) ) );
352 
353 			memcpy ( &eng->ncsi_rsp, eng->dat.NCSI_RxByteBUF, bytesize );
354 
355 			if ( eng->arg.ctrl.b.print_ncsi )
356 				PRINTF( FP_LOG ,"[Frm-NCSI][Rx IID:%2d]\n", eng->ncsi_rsp.IID );
357 
358 			if ( ( eng->ncsi_rsp.IID == 0x0 ) && ( eng->ncsi_rsp.Command == 0xff ) ) { // AEN Packet
359 				if ( eng->arg.ctrl.b.print_ncsi )
360 					PRINTF( FP_LOG ,"[Frm-NCSI][AEN Packet]Type:%2d\n", SWAP_2B_BEDN( eng->ncsi_rsp.Reason_Code ) & 0xff );
361 			}
362 			else {
363 				ret = 0;
364 			}
365 		}
366 		else {
367 			if ( eng->arg.ctrl.b.print_ncsi )
368 				PRINTF( FP_LOG, "[Frm-Skip] Not NCSI Response: [%08x & %08x = %08x]!=[%08x]\n", NCSI_RxData, 0xffff, NCSI_RxData & 0xffff, 0xf888 );
369 		} // End if ( ( NCSI_RxData & 0xffff ) == 0xf888 )
370 
371 		if ( HWEOR( NCSI_RxDesDat ) ) {
372 			// it is last the descriptor in the receive Ring
373 			Write_Mem_Des_NCSI_DD( eng->run.ncsi_rdes_base     , EOR_IniVal    );
374 			eng->run.ncsi_rdes_base = eng->run.rdes_base;//base for read/write
375 		}
376 		else {
377 			Write_Mem_Des_NCSI_DD( eng->run.ncsi_rdes_base     , 0x00000000    );
378 			eng->run.ncsi_rdes_base += 16;
379 		}
380 
381 		if ( ret == 0 )
382 			break;
383 		retry++;
384 	} while ( retry < NCSI_RxDESNum );
385 
386 	if ( ( ret == 0 ) && eng->arg.ctrl.b.print_ncsi ) {
387 #ifdef Print_DetailFrame
388 		ncsi_respdump ( eng, &eng->ncsi_rsp );
389 #else
390 		PRINTF( FP_LOG, "[NCSI-Respond] ETyp:%04x MC_ID:%02x HeadVer:%02x IID:%02x Comm:%02x ChlID:%02x PayLen:%04x ResCd:%02x ReaCd:%02x\n",
391 		SWAP_2B_BEDN( eng->ncsi_rsp.EtherType ),
392 		eng->ncsi_rsp.MC_ID,
393 		eng->ncsi_rsp.Header_Revision,
394 		eng->ncsi_rsp.IID,
395 		eng->ncsi_rsp.Command,
396 		eng->ncsi_rsp.ChID,
397 		SWAP_2B_BEDN( eng->ncsi_rsp.Payload_Length ),
398 		SWAP_2B_BEDN( eng->ncsi_rsp.Response_Code ),
399 		SWAP_2B_BEDN( eng->ncsi_rsp.Reason_Code ));
400 #endif
401 
402 		NCSI_PrintCommandType2File( eng, eng->ncsi_rsp.Command, eng->ncsi_rsp.IID );
403 	}
404 
405 	return( ret );
406 } // End char NCSI_Rx_SLT (MAC_ENGINE *eng)
407 
408 //------------------------------------------------------------
409 char NCSI_Tx (MAC_ENGINE *eng, unsigned char command, unsigned char allid, uint16_t length) {
410 	int        bytesize;
411 	int        dwsize;
412 	int        i;
413 	int        timeout = 0;
414 	uint32_t      NCSI_TxDesDat;
415 
416 	eng->ncsi_req.IID++;
417 	eng->ncsi_req.Command        = command;
418 	eng->ncsi_req.ChID           = allid;
419 	eng->ncsi_req.Payload_Length = SWAP_2B_BEDN( length );
420 
421 	memcpy (  eng->dat.NCSI_TxByteBUF               , &eng->ncsi_req                 , 30     );
422 	memcpy ( (eng->dat.NCSI_TxByteBUF + 30         ), &eng->dat.NCSI_Payload_Data    , length );
423 	Calculate_Checksum_NCSI( eng, eng->dat.NCSI_TxByteBUF, 30 + length );
424 	memcpy ( (eng->dat.NCSI_TxByteBUF + 30 + length), &eng->dat.Payload_Checksum_NCSI, 4      );
425 
426 	// Header of NC-SI command format is 34 bytes. page 58, NC-SI spec. ver 1.0.0 from DMTF
427 	// The minimum size of a NC-SI package is 64 bytes.
428 	bytesize = 34 + length;
429 	if ( bytesize < 60 ) {
430 		memset ( eng->dat.NCSI_TxByteBUF + bytesize, 0, 60 - bytesize );
431 		bytesize = 60;
432 	}
433 
434 	// Fill up to multiple of 4
435 	//    dwsize = (bytesize + 3) >> 2;
436 	if ( ( bytesize % 4 ) != 0 )
437 		dwsize = ( bytesize >> 2 ) + 1;
438 	else
439 		dwsize = bytesize >> 2;
440 
441 	if ( eng->arg.ctrl.b.print_ncsi ) {
442 		if ( bytesize % 4 )
443 			memset ( eng->dat.NCSI_TxByteBUF + bytesize, 0, (dwsize << 2) - bytesize );
444 
445 		PRINTF( FP_LOG ,"----->[Tx] %d bytes(%xh)\n", bytesize, bytesize );
446 		for ( i = 0; i < dwsize-1; i++ )
447 			PRINTF( FP_LOG, "      [Tx]%02d:%08x %08x\n", i, eng->dat.NCSI_TxDWBUF[i], SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ) );
448 
449 		i = dwsize - 1;
450 		switch ( bytesize % 4 ) {
451 			case 0  : PRINTF( FP_LOG ,"      [Tx]%02d:%08x %08x\n",                          i, eng->dat.NCSI_TxDWBUF[i] & SWAP_4B_LEDN_NCSI( 0xffffffff ), SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ) & SWAP_4B_BEDN_NCSI( 0xffffffff ) ); break;
452 			case 3  : PRINTF( FP_LOG ,"      [Tx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, eng->dat.NCSI_TxDWBUF[i] & SWAP_4B_LEDN_NCSI( 0x00ffffff ), SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ) & SWAP_4B_BEDN_NCSI( 0x00ffffff ), eng->dat.NCSI_TxDWBUF[i], SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ), SWAP_4B_LEDN_NCSI( 0x00ffffff ), SWAP_4B_BEDN_NCSI( 0x00ffffff ) ); break;
453 			case 2  : PRINTF( FP_LOG ,"      [Tx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, eng->dat.NCSI_TxDWBUF[i] & SWAP_4B_LEDN_NCSI( 0x0000ffff ), SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ) & SWAP_4B_BEDN_NCSI( 0x0000ffff ), eng->dat.NCSI_TxDWBUF[i], SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ), SWAP_4B_LEDN_NCSI( 0x0000ffff ), SWAP_4B_BEDN_NCSI( 0x0000ffff ) ); break;
454 			case 1  : PRINTF( FP_LOG ,"      [Tx]%02d:%08x %08x [%08x %08x][%08x %08x]\n", i, eng->dat.NCSI_TxDWBUF[i] & SWAP_4B_LEDN_NCSI( 0x000000ff ), SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ) & SWAP_4B_BEDN_NCSI( 0x000000ff ), eng->dat.NCSI_TxDWBUF[i], SWAP_4B( eng->dat.NCSI_TxDWBUF[i] ), SWAP_4B_LEDN_NCSI( 0x000000ff ), SWAP_4B_BEDN_NCSI( 0x000000ff ) ); break;
455 			default : PRINTF( FP_LOG ,"      [Tx]%02d:error", i ); break;
456 		}
457 		PRINTF( FP_LOG ,"[Frm-NCSI][Tx IID:%2d]\n", eng->ncsi_req.IID );
458 	}
459 
460 #if 1
461 	// Copy data to DMA buffer
462 	for ( i = 0; i < dwsize; i++ )
463 		Write_Mem_Dat_NCSI_DD( DMA_BASE + ( i << 2 ), SWAP_4B_LEDN_NCSI( eng->dat.NCSI_TxDWBUF[i] ) );
464 #endif
465 
466 	// Setting one TX descriptor
467 	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base       , 0xf0008000 + bytesize );
468 
469 //	mac_reg_write( eng, 0x40, eng->reg.MAC_040 ); // 20170505
470 
471 	// Fire
472 	mac_reg_write( eng, 0x18, 0x00000000 );//Tx Poll
473 
474 	do {
475 		NCSI_TxDesDat = Read_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base );
476 		if ( ++timeout > TIME_OUT_NCSI ) {
477 			PRINTF( FP_LOG, "[TxDes] DesOwn timeout     %08X\n", NCSI_TxDesDat );
478 			return( FindErr( eng, Err_Flag_NCSI_Check_TxOwnTimeOut  ));
479 		}
480 	} while ( HWOwnTx( NCSI_TxDesDat ) );
481 
482 	if ( eng->arg.ctrl.b.print_ncsi ) {
483 #ifdef Print_DetailFrame
484 		ncsi_reqdump ( eng, &eng->ncsi_req );
485 #else
486 		PRINTF( FP_LOG, "[NCSI-Request] ETyp:%04x MC_ID:%02x HeadVer:%02x IID:%02x Comm:%02x ChlID:%02x PayLen:%04x\n",
487 		SWAP_2B_BEDN( eng->ncsi_req.EtherType ),
488 		eng->ncsi_req.MC_ID,
489 		eng->ncsi_req.Header_Revision,
490 		eng->ncsi_req.IID,
491 		eng->ncsi_req.Command,
492 		eng->ncsi_req.ChID,
493 		SWAP_2B_BEDN( eng->ncsi_req.Payload_Length ) );
494 #endif
495 
496 		NCSI_PrintCommandType2File( eng, eng->ncsi_req.Command, eng->ncsi_req.IID );
497 	}
498 #ifdef Print_PackageName
499 	NCSI_PrintCommandType( eng, eng->ncsi_req.Command, eng->ncsi_req.IID );
500 #endif
501 
502 	return(0);
503 } // End char NCSI_Tx (MAC_ENGINE *eng, unsigned char command, unsigned char allid, uint16_t length)
504 
505 //------------------------------------------------------------
506 char NCSI_ARP (MAC_ENGINE *eng) {
507 	int        i;
508 	int        timeout = 0;
509 	uint32_t      NCSI_TxDesDat;
510 
511 	if ( eng->arg.ctrl.b.print_ncsi )
512 		PRINTF( FP_LOG ,"----->[ARP] 60 bytes x%d\n", eng->arg.GARPNumCnt );
513 
514 	for (i = 0; i < 15; i++) {
515 		if ( eng->arg.ctrl.b.print_ncsi )
516 			PRINTF( FP_LOG, "      [Tx%02d] %08x %08x\n", i, eng->dat.ARP_data[i], SWAP_4B( eng->dat.ARP_data[i] ) );
517 
518 		Write_Mem_Dat_NCSI_DD(((uint32_t)&dma_buf) + ( i << 2 ), eng->dat.ARP_data[i] );
519 	}
520 
521 //	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x04, 0                        );
522 //	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x08, 0                        );
523 //	Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base + 0x0C, AT_MEMRW_BUF( DMA_BASE ) );
524 	for (i = 0; i < eng->arg.GARPNumCnt; i++) {
525 		Write_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base      , 0xf0008000 + 60);
526 		mac_reg_write( eng, 0x18, 0x00000000 );//Tx Poll
527 
528 		do {
529 			NCSI_TxDesDat = Read_Mem_Des_NCSI_DD( eng->run.ncsi_tdes_base );
530 			if ( ++timeout > TIME_OUT_NCSI ) {
531 				PRINTF( FP_LOG, "[TxDes-ARP] DesOwn timeout %08x\n", NCSI_TxDesDat );
532 				return( FindErr( eng, Err_Flag_NCSI_Check_ARPOwnTimeOut ) );
533 			}
534 		} while ( HWOwnTx( NCSI_TxDesDat ) );
535 	}
536 	return(0);
537 } // End char NCSI_ARP (MAC_ENGINE *eng)
538 
539 //------------------------------------------------------------
540 char NCSI_SentWaitPacket (MAC_ENGINE *eng, unsigned char command, unsigned char allid, uint16_t length) {
541 	int        Retry = 0;
542 
543 	do {
544 		if ( NCSI_Tx( eng, command, allid, length ) )
545 			return( 1 );
546 
547 #ifdef NCSI_EnableDelay_EachPackage
548 		DELAY( Delay_EachPackage );
549 #endif
550 		if ( NCSI_Rx_SLT( eng ) )
551 			return( 2 );
552 
553 		if (    ( eng->ncsi_rsp.IID           != eng->ncsi_req.IID                        )
554 		     || ( eng->ncsi_rsp.Command       != ( command | 0x80 )                       )
555 		     || ( eng->ncsi_rsp.Response_Code != SWAP_2B_BEDN( COMMAND_COMPLETED ) ) ) {
556 			if ( eng->arg.ctrl.b.print_ncsi ) {
557 				PRINTF( FP_LOG, "Retry: Command = %x, Response_Code = %x", eng->ncsi_req.Command, SWAP_2B_BEDN( eng->ncsi_rsp.Response_Code ) );
558 				switch ( SWAP_2B_BEDN( eng->ncsi_rsp.Response_Code ) ) {
559 					case COMMAND_COMPLETED  	: PRINTF( FP_LOG, "(completed  )\n" ); break;
560 					case COMMAND_FAILED     	: PRINTF( FP_LOG, "(failed     )\n" ); break;
561 					case COMMAND_UNAVAILABLE	: PRINTF( FP_LOG, "(unavailable)\n" ); break;
562 					case COMMAND_UNSUPPORTED	: PRINTF( FP_LOG, "(unsupported)\n" ); break;
563 					default                 	: PRINTF( FP_LOG, "(-----------)\n" ); break;
564 				}
565 			}
566 			Retry++;
567 		}
568 		else {
569 			return( 0 );
570 		}
571 	} while (Retry <= SENT_RETRY_COUNT);
572 
573 	return( 3 );
574 } // End char NCSI_SentWaitPacket (unsigned char command, unsigned char id, uint16_t length)
575 
576 //------------------------------------------------------------
577 char Clear_Initial_State_SLT (MAC_ENGINE *eng) {//Command:0x00
578 	char       return_value;
579 
580 	eng->flg.error_backup  = eng->flg.error;
581 	eng->flg.ncsi_backup = eng->flg.ncsi;
582 
583 	return_value = NCSI_SentWaitPacket( eng, CLEAR_INITIAL_STATE, eng->ncsi_cap.All_ID, 0 );//Internal Channel ID = 0
584 
585 	eng->flg.error  = eng->flg.error_backup;
586 	eng->flg.ncsi = eng->flg.ncsi_backup;
587 	return( return_value );//Internal Channel ID = 0
588 }
589 
590 //------------------------------------------------------------
591 char Select_Package_SLT (MAC_ENGINE *eng, char skipflag) {//Command:0x01
592 	char       return_value;
593 
594 	if ( skipflag ) {
595 		eng->flg.error_backup  = eng->flg.error;
596 		eng->flg.ncsi_backup = eng->flg.ncsi;
597 	}
598 
599 	memset ((void *)eng->dat.NCSI_Payload_Data, 0, 4);
600 	eng->dat.NCSI_Payload_Data[ 3 ] = 1; //Arbitration Disable
601 	return_value = NCSI_SentWaitPacket( eng, SELECT_PACKAGE, ( eng->ncsi_cap.Package_ID << 5 ) + 0x1F, 4 );//Internal Channel ID = 0x1F, 0x1F means all channel
602 	if ( return_value )
603 		ncsi_set_error_flag( eng, NCSI_Flag_Select_Package );
604 
605 	if ( skipflag ) {
606 		eng->flg.error  = eng->flg.error_backup;
607 		eng->flg.ncsi = eng->flg.ncsi_backup;
608 	}
609 	return( return_value );
610 }
611 
612 //------------------------------------------------------------
613 void Select_Active_Package_SLT (MAC_ENGINE *eng) {//Command:0x01
614 	memset ((void *)eng->dat.NCSI_Payload_Data, 0, 4);
615 	eng->dat.NCSI_Payload_Data[ 3 ] = 1; //Arbitration Disable
616 
617 	if ( NCSI_SentWaitPacket( eng, SELECT_PACKAGE, ( eng->ncsi_cap.Package_ID << 5 ) + 0x1F, 4 ) ) //Internal Channel ID = 0x1F, 0x1F means all channel
618 		ncsi_set_error_flag( eng, NCSI_Flag_Select_Active_Package );
619 }
620 
621 //------------------------------------------------------------
622 void DeSelect_Package_SLT (MAC_ENGINE *eng) {//Command:0x02
623 	if ( NCSI_SentWaitPacket( eng, DESELECT_PACKAGE, ( eng->ncsi_cap.Package_ID << 5 ) + 0x1F, 0 ) ) //Internal Channel ID = 0x1F, 0x1F means all channel
624 		ncsi_set_error_flag( eng, NCSI_Flag_Deselect_Package );
625 
626 #ifdef NCSI_EnableDelay_DeSelectPackage
627 	DELAY( Delay_DeSelectPackage );
628 #endif
629 }
630 
631 //------------------------------------------------------------
632 void Enable_Channel_SLT (MAC_ENGINE *eng) {//Command:0x03
633 	if ( NCSI_SentWaitPacket( eng, ENABLE_CHANNEL, eng->ncsi_cap.All_ID, 0 ) )
634 		ncsi_set_error_flag( eng, NCSI_Flag_Enable_Channel );
635 }
636 
637 //------------------------------------------------------------
638 void Disable_Channel_SLT (MAC_ENGINE *eng, char skipflag) {//Command:0x04
639 	if ( skipflag ) {
640 		eng->flg.error_backup  = eng->flg.error;
641 		eng->flg.ncsi_backup = eng->flg.ncsi;
642 	}
643 
644 	memset ((void *)eng->dat.NCSI_Payload_Data, 0, 4);
645 	eng->dat.NCSI_Payload_Data[ 3 ] = 0x1; //ALD
646 	if ( NCSI_SentWaitPacket( eng, DISABLE_CHANNEL, eng->ncsi_cap.All_ID, 4 ) )
647 		ncsi_set_error_flag( eng, NCSI_Flag_Disable_Channel );
648 
649 	if ( skipflag ) {
650 		eng->flg.error  = eng->flg.error_backup;
651 		eng->flg.ncsi = eng->flg.ncsi_backup;
652 	}
653 }
654 
655 //------------------------------------------------------------
656 void Enable_Network_TX_SLT (MAC_ENGINE *eng) {//Command:0x06
657 	if ( NCSI_SentWaitPacket( eng, ENABLE_CHANNEL_NETWORK_TX, eng->ncsi_cap.All_ID, 0 ) )
658 		ncsi_set_error_flag( eng, NCSI_Flag_Enable_Network_TX );
659 }
660 
661 //------------------------------------------------------------
662 void Disable_Network_TX_SLT (MAC_ENGINE *eng) {//Command:0x07
663 	if ( NCSI_SentWaitPacket( eng, DISABLE_CHANNEL_NETWORK_TX, eng->ncsi_cap.All_ID, 0 ) )
664 		ncsi_set_error_flag( eng, NCSI_Flag_Disable_Network_TX );
665 }
666 
667 //------------------------------------------------------------
668 void Set_Link_SLT (MAC_ENGINE *eng) {//Command:0x09
669 	memset ((void *)eng->dat.NCSI_Payload_Data, 0, 8);
670 	eng->dat.NCSI_Payload_Data[ 2 ] = 0x02; //full duplex
671 //	eng->dat.NCSI_Payload_Data[ 3 ] = 0x04; //100M, auto-disable
672 	eng->dat.NCSI_Payload_Data[ 3 ] = 0x05; //100M, auto-enable
673 
674 	if ( NCSI_SentWaitPacket( eng, SET_LINK, eng->ncsi_cap.All_ID, 8 ) )
675 		ncsi_set_error_flag( eng, NCSI_Flag_Set_Link );
676 }
677 
678 //------------------------------------------------------------
679 char Get_Link_Status_SLT (MAC_ENGINE *eng) {//Command:0x0a
680 	if ( NCSI_SentWaitPacket( eng, GET_LINK_STATUS, eng->ncsi_cap.All_ID, 0 ) )
681 		return(0);
682 	else {
683 		if ( eng->ncsi_rsp.Payload_Data[ 3 ] & 0x20 ) {
684 			if ( eng->ncsi_rsp.Payload_Data[ 3 ] & 0x40 ) {
685 				if ( eng->ncsi_rsp.Payload_Data[ 3 ] & 0x01 )
686 					return(1); //Link Up or Not
687 				else
688 					return(0);
689 			}
690 			else
691 				return(0); //Auto Negotiate did not finish
692 		}
693 		else {
694 			if ( eng->ncsi_rsp.Payload_Data[ 3 ] & 0x01 )
695 				return(1); //Link Up or Not
696 			else
697 				return(0);
698 		}
699 	}
700 } // End char Get_Link_Status_SLT (MAC_ENGINE *eng)
701 
702 //------------------------------------------------------------
703 void Enable_Set_MAC_Address_SLT (MAC_ENGINE *eng)
704 {
705 	//Command:0x0e
706 
707 	int        i;
708 
709 	for ( i = 0; i < 6; i++ )
710 		eng->dat.NCSI_Payload_Data[ i ] = eng->ncsi_req.SA[ i ];
711 	eng->dat.NCSI_Payload_Data[ 6 ] = 1; //MAC Address Num = 1 --> address filter 1, fixed in sample code
712 
713 	if ( eng->ncsi_req.SA[ 0 ] & 0x1 )
714 		eng->dat.NCSI_Payload_Data[ 7 ] = MULTICAST + ENABLE_MAC_ADDRESS_FILTER; //AT + E
715 	else
716 		eng->dat.NCSI_Payload_Data[ 7 ] = UNICAST   + ENABLE_MAC_ADDRESS_FILTER; //AT + E
717 
718 	if ( NCSI_SentWaitPacket( eng, SET_MAC_ADDRESS, eng->ncsi_cap.All_ID, 8 ) )
719 		ncsi_set_error_flag( eng, NCSI_Flag_Enable_Set_MAC_Address );
720 }
721 
722 //------------------------------------------------------------
723 void Enable_Broadcast_Filter_SLT (MAC_ENGINE *eng) {//Command:0x10
724 	memset ((void *)eng->dat.NCSI_Payload_Data, 0, 4);
725 	eng->dat.NCSI_Payload_Data[ 3 ] = 0xF; //ARP, DHCP, NetBIOS
726 
727 	if ( NCSI_SentWaitPacket( eng, ENABLE_BROADCAST_FILTERING, eng->ncsi_cap.All_ID, 4 ) )
728 		ncsi_set_error_flag( eng, NCSI_Flag_Enable_Broadcast_Filter );
729 }
730 
731 //------------------------------------------------------------
732 void Get_Version_ID_SLT (MAC_ENGINE *eng) {//Command:0x15
733 	if ( NCSI_SentWaitPacket( eng, GET_VERSION_ID, eng->ncsi_cap.All_ID, 0 ) )
734 		ncsi_set_error_flag( eng, NCSI_Flag_Get_Version_ID );
735 	else {
736 #ifdef Print_Version_ID
737 		printf("NCSI Version        : %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[  0 ], eng->ncsi_rsp.Payload_Data[  1 ], eng->ncsi_rsp.Payload_Data[  2 ], eng->ncsi_rsp.Payload_Data[  3 ]);
738 		printf("NCSI Version        : %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[  4 ], eng->ncsi_rsp.Payload_Data[  5 ], eng->ncsi_rsp.Payload_Data[  6 ], eng->ncsi_rsp.Payload_Data[  7 ]);
739 		printf("Firmware Name String: %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[  8 ], eng->ncsi_rsp.Payload_Data[  9 ], eng->ncsi_rsp.Payload_Data[ 10 ], eng->ncsi_rsp.Payload_Data[ 11 ]);
740 		printf("Firmware Name String: %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[ 12 ], eng->ncsi_rsp.Payload_Data[ 13 ], eng->ncsi_rsp.Payload_Data[ 14 ], eng->ncsi_rsp.Payload_Data[ 15 ]);
741 		printf("Firmware Name String: %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[ 16 ], eng->ncsi_rsp.Payload_Data[ 17 ], eng->ncsi_rsp.Payload_Data[ 18 ], eng->ncsi_rsp.Payload_Data[ 19 ]);
742 		printf("Firmware Version    : %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[ 20 ], eng->ncsi_rsp.Payload_Data[ 21 ], eng->ncsi_rsp.Payload_Data[ 22 ], eng->ncsi_rsp.Payload_Data[ 23 ]);
743 		printf("PCI DID/VID         : %02x %02x/%02x %02x\n", eng->ncsi_rsp.Payload_Data[ 24 ], eng->ncsi_rsp.Payload_Data[ 25 ], eng->ncsi_rsp.Payload_Data[ 26 ], eng->ncsi_rsp.Payload_Data[ 27 ]);
744 		printf("PCI SSID/SVID       : %02x %02x/%02x %02x\n", eng->ncsi_rsp.Payload_Data[ 28 ], eng->ncsi_rsp.Payload_Data[ 29 ], eng->ncsi_rsp.Payload_Data[ 30 ], eng->ncsi_rsp.Payload_Data[ 31 ]);
745 		printf("Manufacturer ID     : %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[ 32 ], eng->ncsi_rsp.Payload_Data[ 33 ], eng->ncsi_rsp.Payload_Data[ 34 ], eng->ncsi_rsp.Payload_Data[ 35 ]);
746 		printf("Checksum            : %02x %02x %02x %02x\n", eng->ncsi_rsp.Payload_Data[ 36 ], eng->ncsi_rsp.Payload_Data[ 37 ], eng->ncsi_rsp.Payload_Data[ 38 ], eng->ncsi_rsp.Payload_Data[ 39 ]);
747 #endif
748 		eng->ncsi_cap.PCI_DID_VID    = (eng->ncsi_rsp.Payload_Data[ 24 ]<<24)
749 		                             | (eng->ncsi_rsp.Payload_Data[ 25 ]<<16)
750 		                             | (eng->ncsi_rsp.Payload_Data[ 26 ]<< 8)
751 		                             | (eng->ncsi_rsp.Payload_Data[ 27 ]    );
752 		eng->ncsi_cap.manufacturer_id = (eng->ncsi_rsp.Payload_Data[ 32 ]<<24)
753 		                             | (eng->ncsi_rsp.Payload_Data[ 33 ]<<16)
754 		                             | (eng->ncsi_rsp.Payload_Data[ 34 ]<< 8)
755 		                             | (eng->ncsi_rsp.Payload_Data[ 35 ]    );
756 	}
757 } // End void Get_Version_ID_SLT (MAC_ENGINE *eng)
758 
759 //------------------------------------------------------------
760 void Get_Capabilities_SLT (MAC_ENGINE *eng) {//Command:0x16
761 	if ( NCSI_SentWaitPacket( eng, GET_CAPABILITIES, eng->ncsi_cap.All_ID, 0 ) )
762 		ncsi_set_error_flag( eng, NCSI_Flag_Get_Capabilities );
763 	else {
764 //		eng->ncsi_cap.Capabilities_Flags                   = (eng->ncsi_rsp.Payload_Data[  0 ]<<24)
765 //		                                                   | (eng->ncsi_rsp.Payload_Data[  1 ]<<16)
766 //		                                                   | (eng->ncsi_rsp.Payload_Data[  2 ]<< 8)
767 //		                                                   | (eng->ncsi_rsp.Payload_Data[  3 ]    );
768 //		eng->ncsi_cap.Broadcast_Packet_Filter_Capabilities = (eng->ncsi_rsp.Payload_Data[  4 ]<<24)
769 //		                                                   | (eng->ncsi_rsp.Payload_Data[  5 ]<<16)
770 //		                                                   | (eng->ncsi_rsp.Payload_Data[  6 ]<< 8)
771 //		                                                   | (eng->ncsi_rsp.Payload_Data[  7 ]    );
772 //		eng->ncsi_cap.Multicast_Packet_Filter_Capabilities = (eng->ncsi_rsp.Payload_Data[  8 ]<<24)
773 //		                                                   | (eng->ncsi_rsp.Payload_Data[  9 ]<<16)
774 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 10 ]<< 8)
775 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 11 ]    );
776 //		eng->ncsi_cap.Buffering_Capabilities               = (eng->ncsi_rsp.Payload_Data[ 12 ]<<24)
777 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 13 ]<<16)
778 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 14 ]<< 8)
779 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 15 ]    );
780 //		eng->ncsi_cap.AEN_Control_Support                  = (eng->ncsi_rsp.Payload_Data[ 16 ]<<24)
781 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 17 ]<<16)
782 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 18 ]<< 8)
783 //		                                                   | (eng->ncsi_rsp.Payload_Data[ 19 ]    );
784 //		eng->ncsi_cap.VLAN_Filter_Count                    =  eng->ncsi_rsp.Payload_Data[ 20 ];
785 		eng->ncsi_cap.Mixed_Filter_Count                   =  eng->ncsi_rsp.Payload_Data[ 21 ];
786 //		eng->ncsi_cap.Multicast_Filter_Count               =  eng->ncsi_rsp.Payload_Data[ 22 ];
787 		eng->ncsi_cap.Unicast_Filter_Count                 =  eng->ncsi_rsp.Payload_Data[ 23 ];
788 //		eng->ncsi_cap.VLAN_Mode_Support                    =  eng->ncsi_rsp.Payload_Data[ 26 ]
789 		eng->ncsi_cap.Channel_Count                        =  eng->ncsi_rsp.Payload_Data[ 27 ];
790 	}
791 }
792 
793 //------------------------------------------------------------
794 void Get_Controller_Packet_Statistics_SLT (MAC_ENGINE *eng) {//Command:0x18
795 	if ( NCSI_SentWaitPacket( eng, GET_CONTROLLER_PACKET_STATISTICS, eng->ncsi_cap.All_ID, 0 ) )
796 		ncsi_set_error_flag( eng, NCSI_Flag_Get_Controller_Packet_Statistics );
797 }
798 
799 //------------------------------------------------------------
800 char phy_ncsi (MAC_ENGINE *eng)
801 {
802 	uint32_t pkg_idx;
803 	uint32_t chl_idx;
804 	uint32_t select_flag[MAX_PACKAGE_NUM];
805 #ifdef NCSI_VERBOSE_TEST
806 	uint32_t Re_Send;
807 	uint32_t Link_Status;
808 #endif
809 	eng->dat.NCSI_RxEr = 0;
810 	eng->dat.number_chl = 0;
811 	eng->dat.number_pak = 0;
812 	eng->ncsi_cap.Package_ID = 0;
813 	eng->ncsi_cap.Channel_ID = 0x1F;
814 	eng->ncsi_cap.All_ID = 0x1F;
815 	PRINTF(FP_LOG, "\n\n======> Start:\n");
816 
817 	NCSI_Struct_Initialize_SLT(eng);
818 
819 #ifdef NCSI_Skip_Phase1_DeSelectPackage
820 #else
821 	//NCSI Start
822 	//Disable Channel then DeSelect Package
823 	for (pkg_idx = 0; pkg_idx < MAX_PACKAGE_NUM; pkg_idx++) {
824 		eng->ncsi_cap.Package_ID = pkg_idx;
825 		eng->ncsi_cap.Channel_ID = 0x1F;
826 		eng->ncsi_cap.All_ID     = ( eng->ncsi_cap.Package_ID << 5) + eng->ncsi_cap.Channel_ID;
827 
828 		select_flag[ pkg_idx ] = Select_Package_SLT ( eng, 1 ); //skipflag// Command:0x01
829 
830 		if ( select_flag[ pkg_idx ] == 0 ) {
831 			if ( !eng->run.IO_MrgChk ) {
832 				printf("----Find Package ID: %d\n", eng->ncsi_cap.Package_ID);
833 				PRINTF(FP_LOG, "----Find Package ID: %d\n", eng->ncsi_cap.Package_ID );
834 			}
835 			for ( chl_idx = 0; chl_idx < MAX_CHANNEL_NUM; chl_idx++ ) {
836 				eng->ncsi_cap.Channel_ID = chl_idx;
837 				eng->ncsi_cap.All_ID     = ( eng->ncsi_cap.Package_ID << 5) + eng->ncsi_cap.Channel_ID;
838 
839 				Disable_Channel_SLT( eng, 1 );//skipflag // Command: 0x04
840 			}
841   #ifdef NCSI_Skip_DeSelectPackage
842   #else
843 			DeSelect_Package_SLT ( eng ); // Command:0x02
844   #endif
845 		} else {
846 			if ( !eng->run.IO_MrgChk ) {
847 				printf("----Absence of Package ID: %d\n", pkg_idx);
848 				PRINTF( FP_LOG, "----Absence of Package ID: %d\n", pkg_idx );
849 			}
850 		} // End if ( select_flag[ pkg_idx ] == 0 )
851 	} // End for (pkg_idx = 0; pkg_idx < MAX_PACKAGE_NUM; pkg_idx++)
852 #endif
853 
854 	//Select Package
855 	for ( pkg_idx = 0; pkg_idx < MAX_PACKAGE_NUM; pkg_idx++ ) {
856 		eng->ncsi_cap.Package_ID = pkg_idx;
857 		eng->ncsi_cap.Channel_ID = 0x1F;
858 		eng->ncsi_cap.All_ID     = ( eng->ncsi_cap.Package_ID << 5) + eng->ncsi_cap.Channel_ID;
859 
860 #ifdef NCSI_Skip_Phase1_DeSelectPackage
861 		select_flag[ pkg_idx ] = Select_Package_SLT ( eng, 1 ); //skipflag//Command:0x01
862 #endif
863 
864 		if ( select_flag[ pkg_idx ] == 0 ) {
865 			//eng->run.NCSI_RxTimeOutScale = 1000;
866 			eng->run.NCSI_RxTimeOutScale = 10;
867 
868 #ifdef NCSI_Skip_Phase1_DeSelectPackage
869 #else
870 			Select_Package_SLT ( eng, 0 );//Command:0x01
871 #endif
872 			eng->dat.number_pak++;
873 			if ( !eng->run.IO_MrgChk ) {
874 				printf("====Find Package ID: %d\n", eng->ncsi_cap.Package_ID);
875 				PRINTF(FP_LOG, "====Find Package ID: %d\n", eng->ncsi_cap.Package_ID );
876 			}
877 
878 			// Scan all channel in the package
879 			for ( chl_idx = 0; chl_idx < MAX_CHANNEL_NUM; chl_idx++ ) {
880 				eng->ncsi_cap.Channel_ID = chl_idx;
881 				eng->ncsi_cap.All_ID     = ( eng->ncsi_cap.Package_ID << 5) + eng->ncsi_cap.Channel_ID;
882 
883 				if ( Clear_Initial_State_SLT( eng ) == 0 ) { //Command:0x00
884 					eng->dat.number_chl++;
885 					if ( !eng->run.IO_MrgChk ) {
886 						printf("--------Find Channel ID: %d\n", eng->ncsi_cap.Channel_ID);
887 						PRINTF( FP_LOG, "--------Find Channel ID: %d\n", eng->ncsi_cap.Channel_ID );
888 					}
889 
890 					// Get Version and Capabilities
891 					Get_Version_ID_SLT( eng );          //Command:0x15
892 #ifdef NCSI_VERBOSE_TEST
893 					Get_Capabilities_SLT( eng );        //Command:0x16
894 					Select_Active_Package_SLT( eng );   //Command:0x01
895 					Enable_Set_MAC_Address_SLT( eng );  //Command:0x0e
896 					Enable_Broadcast_Filter_SLT( eng ); //Command:0x10
897 
898 					// Enable TX
899 					Enable_Network_TX_SLT( eng );       //Command:0x06
900 
901 					// Enable Channel
902 					Enable_Channel_SLT( eng );          //Command:0x03
903 
904 					// Get Link Status
905 					Re_Send = 0;
906 					do {
907 #ifdef NCSI_EnableDelay_GetLinkStatus
908 						if ( Re_Send )
909 							DELAY( Delay_GetLinkStatus );
910 #endif
911 
912 						Link_Status = Get_Link_Status_SLT( eng );//Command:0x0a
913 						if ( Link_Status == LINK_UP ) {
914 							if ( eng->arg.GARPNumCnt )
915 								NCSI_ARP ( eng );
916 							break;
917 						} // End if ( Link_Status == LINK_UP )
918 					} while ( Re_Send++ <= 2 );
919 
920 					if ( !eng->run.IO_MrgChk ) {
921 						if ( Link_Status == LINK_UP ) {
922 							printf("        This Channel is LINK_UP (MFC:%d, UFC:%d, CC:%d)\n", eng->ncsi_cap.Mixed_Filter_Count, eng->ncsi_cap.Unicast_Filter_Count, eng->ncsi_cap.Channel_Count);
923 							PRINTF( FP_LOG, "        This Channel is LINK_UP (MFC:%d, UFC:%d, CC:%d)\n", eng->ncsi_cap.Mixed_Filter_Count, eng->ncsi_cap.Unicast_Filter_Count, eng->ncsi_cap.Channel_Count);
924 						}
925 						else {
926 							printf("        This Channel is LINK_DOWN (MFC:%d, UFC:%d, CC:%d)\n", eng->ncsi_cap.Mixed_Filter_Count, eng->ncsi_cap.Unicast_Filter_Count, eng->ncsi_cap.Channel_Count);
927 							PRINTF( FP_LOG, "        This Channel is LINK_DOWN (MFC:%d, UFC:%d, CC:%d)\n", eng->ncsi_cap.Mixed_Filter_Count, eng->ncsi_cap.Unicast_Filter_Count, eng->ncsi_cap.Channel_Count);
928 						}
929 					}
930 #endif	/* endof "#ifdef NCSI_VERBOSE_TEST" */
931 
932 #ifdef NCSI_Skip_DiSChannel
933 #else
934 					if ( eng->run.TM_NCSI_DiSChannel ) {
935 						// Disable TX
936 						Disable_Network_TX_SLT( eng ); //Command:0x07
937 						// Disable Channel
938 						Disable_Channel_SLT( eng, 0 );    //Command:0x04
939 					}
940 #endif
941 				} // End if ( Clear_Initial_State_SLT( eng, chl_idx ) == 0 )
942 			} // End for ( chl_idx = 0; chl_idx < MAX_CHANNEL_NUM; chl_idx++ )
943 
944 #ifdef NCSI_Skip_DeSelectPackage
945 #else
946 			DeSelect_Package_SLT ( eng );//Command:0x02
947 #endif
948 			eng->run.NCSI_RxTimeOutScale = 1;
949 		}
950 		else {
951 			if ( !eng->run.IO_MrgChk ) {
952 				printf("====Absence of Package ID: %d\n", pkg_idx);
953 				PRINTF( FP_LOG, "====Absence of Package ID: %d\n", pkg_idx );
954 			}
955 		} // End if ( select_flag[pkg_idx] == 0 )
956 	} // End for ( pkg_idx = 0; pkg_idx < MAX_PACKAGE_NUM; pkg_idx++ )
957 
958 	if ( eng->dat.number_pak == 0                       ) FindErr( eng, Err_Flag_NCSI_No_PHY      );
959 	if ( eng->dat.number_pak != eng->arg.GPackageTolNum ) FindErr( eng, Err_Flag_NCSI_Package_Num );
960 	if ( eng->dat.number_chl != eng->arg.GChannelTolNum ) FindErr( eng, Err_Flag_NCSI_Channel_Num );
961 //	if ( eng->dat.number_chl == 0                       ) FindErr( eng );
962 
963 	if ( eng->flg.error ) {
964 		if ( eng->dat.NCSI_RxEr )
965 			FindErr_Des( eng, Des_Flag_RxErr );
966 		return(1);
967 	}
968 	else {
969 		if ( eng->dat.NCSI_RxEr ) {
970 			eng->flg.warn = eng->flg.warn | Wrn_Flag_RxErFloatting;
971 			if ( eng->arg.ctrl.b.skip_rx_err ) {
972 				eng->flg.all_fail = 0;
973 				return(0);
974 			}
975 			else {
976 				FindErr_Des( eng, Des_Flag_RxErr );
977 				return(1);
978 			}
979 		}
980 		else {
981 			eng->flg.all_fail = 0;
982 			return(0);
983 		}
984 	}
985 }
986