[4284] | 1 | /** @file wl_transport.c |
---|
| 2 | * @brief WARPLab Framework (Transport) |
---|
| 3 | * |
---|
[4668] | 4 | * This contains the code for WARPLab Framework transport. |
---|
[4284] | 5 | * |
---|
[4668] | 6 | * @copyright Copyright 2013-2015, Mango Communications. All rights reserved. |
---|
[4284] | 7 | * Distributed under the WARP license (http://warpproject.org/license) |
---|
| 8 | * |
---|
| 9 | * @author Chris Hunter (chunter [at] mangocomm.com) |
---|
| 10 | * @author Patrick Murphy (murphpo [at] mangocomm.com) |
---|
| 11 | * @author Erik Welsh (welsh [at] mangocomm.com) |
---|
| 12 | */ |
---|
[1995] | 13 | |
---|
[4453] | 14 | |
---|
| 15 | /**********************************************************************************************************************/ |
---|
| 16 | /** |
---|
[4668] | 17 | * @brief Common Functions |
---|
[4453] | 18 | * |
---|
| 19 | **********************************************************************************************************************/ |
---|
| 20 | |
---|
[2054] | 21 | /***************************** Include Files *********************************/ |
---|
| 22 | |
---|
[4284] | 23 | // Xilinx / Standard library includes |
---|
| 24 | #include <xparameters.h> |
---|
[4514] | 25 | #include <xil_io.h> |
---|
| 26 | #include <xstatus.h> |
---|
[1915] | 27 | #include <stdlib.h> |
---|
| 28 | #include <stdio.h> |
---|
[4514] | 29 | #include <string.h> |
---|
[2054] | 30 | |
---|
[4284] | 31 | // WARPLab includes |
---|
| 32 | #include "wl_common.h" |
---|
| 33 | #include "wl_transport.h" |
---|
| 34 | #include "wl_trigger_manager.h" |
---|
| 35 | #include "wl_node.h" |
---|
| 36 | |
---|
[1915] | 37 | |
---|
[2054] | 38 | /*************************** Constant Definitions ****************************/ |
---|
| 39 | |
---|
| 40 | /*********************** Global Variable Definitions *************************/ |
---|
| 41 | |
---|
[4284] | 42 | extern u16 node; // Node ID (defined in wl_node.c) |
---|
[2054] | 43 | |
---|
| 44 | |
---|
| 45 | /*************************** Variable Definitions ****************************/ |
---|
| 46 | |
---|
[2057] | 47 | // NOTE: This structure has different member types depending on the WARP version |
---|
[4514] | 48 | wl_eth_dev_info eth_devices[WL_NUM_ETH_DEVICES]; |
---|
[2054] | 49 | |
---|
[4514] | 50 | // Callbacks |
---|
| 51 | volatile wl_function_ptr_t process_hton_msg_callback; |
---|
[2054] | 52 | |
---|
| 53 | |
---|
[4514] | 54 | /*************************** Function Prototypes *****************************/ |
---|
[2054] | 55 | |
---|
[4514] | 56 | void transport_wl_eth_dev_info_init(u32 eth_dev_num); |
---|
[2054] | 57 | |
---|
[4514] | 58 | int transport_check_device(u32 eth_dev_num); |
---|
[2873] | 59 | |
---|
[4514] | 60 | void transport_receive(u32 eth_dev_num, int socket_index, struct sockaddr * from, warp_ip_udp_buffer * recv_buffer, warp_ip_udp_buffer * send_buffer); |
---|
[4284] | 61 | |
---|
[4673] | 62 | void transport_set_eth_phy_speed(u32 eth_dev_num, u32 speed); |
---|
| 63 | void transport_set_eth_phy_auto_negotiation(u32 eth_dev_num, u32 enable); |
---|
[1915] | 64 | |
---|
[4673] | 65 | |
---|
[4514] | 66 | /******************************** Functions **********************************/ |
---|
[1915] | 67 | |
---|
[4453] | 68 | |
---|
[4514] | 69 | /*****************************************************************************/ |
---|
| 70 | /** |
---|
| 71 | * This function will poll the given Ethernet device |
---|
| 72 | * |
---|
[4668] | 73 | * @param eth_dev_num - Ethernet device number |
---|
[4514] | 74 | * |
---|
[4668] | 75 | * @return None |
---|
[4514] | 76 | * |
---|
| 77 | * @note Buffers are managed by the WARP UDP transport driver |
---|
| 78 | * |
---|
| 79 | *****************************************************************************/ |
---|
| 80 | void transport_poll(u32 eth_dev_num) { |
---|
[1915] | 81 | |
---|
[4668] | 82 | int recv_bytes; |
---|
[4514] | 83 | int socket_index; |
---|
[4668] | 84 | warp_ip_udp_buffer recv_buffer; |
---|
| 85 | warp_ip_udp_buffer * send_buffer; |
---|
[4514] | 86 | struct sockaddr from; |
---|
| 87 | |
---|
| 88 | // Check the socket to see if there is data |
---|
| 89 | recv_bytes = socket_recvfrom_eth(eth_dev_num, &socket_index, &from, &recv_buffer); |
---|
| 90 | |
---|
| 91 | // If we have received data, then we need to process it |
---|
| 92 | if (recv_bytes > 0) { |
---|
| 93 | // Allocate a send buffer from the transport driver |
---|
| 94 | send_buffer = socket_alloc_send_buffer(); |
---|
| 95 | |
---|
| 96 | // Process the received packet |
---|
| 97 | transport_receive(eth_dev_num, socket_index, &from, &recv_buffer, send_buffer); |
---|
| 98 | |
---|
| 99 | // Need to communicate to the transport driver that the buffers can now be reused |
---|
| 100 | socket_free_recv_buffer(socket_index, &recv_buffer); |
---|
| 101 | socket_free_send_buffer(send_buffer); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
[2054] | 107 | /*****************************************************************************/ |
---|
| 108 | /** |
---|
[4514] | 109 | * Process the received UDP packet by the transport |
---|
| 110 | * |
---|
| 111 | * @param eth_dev_num - Ethernet device number |
---|
| 112 | * @param socket_index - Index of the socket on which message was received |
---|
| 113 | * @param from - Pointer to socket address structure from which message was received |
---|
| 114 | * @param recv_buffer - Pointer to transport buffer with received message |
---|
| 115 | * @param send_buffer - Pointer to transport buffer for a node response to the message |
---|
| 116 | * |
---|
[4668] | 117 | * @return None |
---|
[4514] | 118 | * |
---|
| 119 | * @note If this packet is a host to node message, then the process_hton_msg_callback |
---|
| 120 | * is used to further process the packet. This method will strip off the |
---|
| 121 | * WL transport header for future packet processing. |
---|
| 122 | * |
---|
| 123 | *****************************************************************************/ |
---|
| 124 | void transport_receive(u32 eth_dev_num, int socket_index, struct sockaddr * from, warp_ip_udp_buffer * recv_buffer, warp_ip_udp_buffer * send_buffer) { |
---|
[1915] | 125 | |
---|
[4668] | 126 | int status; |
---|
| 127 | u32 trigger_ethernet_id; |
---|
| 128 | u16 dest_id; |
---|
| 129 | u16 src_id; |
---|
| 130 | u16 seq_num; |
---|
| 131 | u16 flags; |
---|
| 132 | u32 group_id; |
---|
[1915] | 133 | |
---|
[4668] | 134 | wl_transport_header * wl_header_rx = (wl_transport_header*)(recv_buffer->offset); // Contains entire Ethernet frame; offset points to UDP payload |
---|
| 135 | wl_transport_header * wl_header_tx = (wl_transport_header*)(send_buffer->offset); // New buffer for UDP payload |
---|
[1915] | 136 | |
---|
[4668] | 137 | // Get the transport headers for the send / receive buffers |
---|
| 138 | // NOTE: For the receive buffer, offset points to UDP payload of the Ethernet frame |
---|
| 139 | // NOTE: For the send buffer, the offset points to the start of the buffer but since we will use the |
---|
| 140 | // UDP header of the socket to transmit the frame, this is effectively the start of the UDP payload |
---|
| 141 | // |
---|
| 142 | wl_header_rx = (wl_transport_header*)(recv_buffer->offset); |
---|
| 143 | wl_header_tx = (wl_transport_header*)(send_buffer->offset); |
---|
[2071] | 144 | |
---|
[4514] | 145 | // Update the buffers to account for the transport headers |
---|
| 146 | recv_buffer->offset += sizeof(wl_transport_header); |
---|
| 147 | recv_buffer->length -= sizeof(wl_transport_header); // Remaining bytes in receive buffer |
---|
[4284] | 148 | |
---|
[4514] | 149 | send_buffer->offset += sizeof(wl_transport_header); |
---|
| 150 | send_buffer->length += sizeof(wl_transport_header); // Adding bytes to the send buffer |
---|
| 151 | send_buffer->size += sizeof(wl_transport_header); // Keep size in sync |
---|
[4284] | 152 | |
---|
[4668] | 153 | // Update the green LEDs for every received packet |
---|
| 154 | increment_green_leds_one_hot(); |
---|
[4314] | 155 | |
---|
[4668] | 156 | // Process the data based on the packet type |
---|
| 157 | // NOTE: The pkt_type does not need to be endian swapped because it is a u8 |
---|
| 158 | // |
---|
| 159 | switch(wl_header_rx->pkt_type){ |
---|
[4284] | 160 | |
---|
[4668] | 161 | //------------------------------- |
---|
| 162 | // Trigger Manager Trigger packet |
---|
| 163 | // |
---|
| 164 | case PKTTYPE_TRIGGER: |
---|
[4514] | 165 | |
---|
[4668] | 166 | // Get the Trigger Ethernet ID from the receive packet |
---|
| 167 | trigger_ethernet_id = Xil_Ntohl(*((u32 *)(recv_buffer->offset))); |
---|
[4514] | 168 | |
---|
[4668] | 169 | // Process the Trigger Ethernet ID |
---|
| 170 | trigmngr_trigger_in(trigger_ethernet_id, eth_dev_num); |
---|
| 171 | break; |
---|
[4284] | 172 | |
---|
[4668] | 173 | //------------------------------- |
---|
| 174 | // Message from the Host to the Node |
---|
| 175 | // |
---|
| 176 | case PKTTYPE_HTON_MSG: |
---|
| 177 | // Extract values from the received transport header |
---|
| 178 | // |
---|
[4514] | 179 | dest_id = Xil_Ntohs(wl_header_rx->dest_id); |
---|
| 180 | src_id = Xil_Ntohs(wl_header_rx->src_id); |
---|
| 181 | seq_num = Xil_Ntohs(wl_header_rx->seq_num); |
---|
| 182 | flags = Xil_Ntohs(wl_header_rx->flags); |
---|
[4668] | 183 | |
---|
[4514] | 184 | group_id = eth_devices[eth_dev_num].group_id; |
---|
[4284] | 185 | |
---|
[4668] | 186 | // If this message is not for the given node, then ignore it |
---|
| 187 | if((dest_id != node) && (dest_id != BROADCAST_DEST_ID) && ((dest_id & (0xFF00 | group_id)) == 0)) { return; } |
---|
[4514] | 188 | |
---|
[4668] | 189 | // Form outgoing WARPLab header for any outgoing packet in response to this message |
---|
| 190 | // NOTE: The u16/u32 fields here will be endian swapped in transport_send |
---|
| 191 | // NOTE: The length field of the header will be set in transport_send |
---|
[4514] | 192 | // |
---|
[4668] | 193 | wl_header_tx->dest_id = src_id; |
---|
| 194 | wl_header_tx->src_id = node; |
---|
| 195 | wl_header_tx->pkt_type = PKTTYPE_NTOH_MSG; |
---|
| 196 | wl_header_tx->seq_num = seq_num; |
---|
| 197 | wl_header_tx->flags = 0; |
---|
| 198 | wl_header_tx->reserved = 0; |
---|
[4284] | 199 | |
---|
[4668] | 200 | // Call the callback to further process the recv_buffer |
---|
| 201 | status = process_hton_msg_callback(socket_index, from, recv_buffer, send_buffer); |
---|
[4284] | 202 | |
---|
[4668] | 203 | if (send_buffer->size != send_buffer->length) { |
---|
| 204 | wl_printf(WL_PRINT_WARNING, print_type_transport, "Send buffer length (%d) does not match size (%d)\n", send_buffer->length, send_buffer->size); |
---|
| 205 | } |
---|
[4514] | 206 | |
---|
[4668] | 207 | // Based on the status, return a message to the host |
---|
| 208 | switch(status) { |
---|
[4284] | 209 | |
---|
[4668] | 210 | //------------------------------- |
---|
| 211 | // No response has been sent by the node |
---|
| 212 | // |
---|
| 213 | case NO_RESP_SENT: |
---|
| 214 | // Check if the host requires a response from the node |
---|
| 215 | if (flags & TRANSPORT_HDR_ROBUST_FLAG) { |
---|
[4284] | 216 | |
---|
[4668] | 217 | // Check that the node has something to send to the host |
---|
| 218 | if ((send_buffer->length) > sizeof(wl_transport_header)) { |
---|
| 219 | transport_send(socket_index, from, &send_buffer, 1); // Send the buffer of data |
---|
| 220 | } else { |
---|
| 221 | wl_printf(WL_PRINT_WARNING, print_type_transport, "Host requires response but node has nothing to send.\n"); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | break; |
---|
[4284] | 225 | |
---|
[4668] | 226 | //------------------------------- |
---|
| 227 | // A response has already been sent by the node |
---|
| 228 | // |
---|
| 229 | case RESP_SENT: |
---|
| 230 | // The transport does not need to do anything else |
---|
| 231 | break; |
---|
[4284] | 232 | |
---|
[4668] | 233 | //------------------------------- |
---|
| 234 | // The node is not ready to process the message; host needs to wait and send it again |
---|
| 235 | // |
---|
| 236 | case NODE_NOT_READY: |
---|
| 237 | wl_printf(WL_PRINT_NONE, NULL, "\nWARNING: Node not ready for command.\n Please add a pause() with the appropriate time to your Matlab code.\n\n"); |
---|
[4371] | 238 | |
---|
[4668] | 239 | // Set the flag to indicate the node is not ready |
---|
| 240 | wl_header_tx->flags = TRANSPORT_HDR_NODE_NOT_READY_FLAG; |
---|
[4284] | 241 | |
---|
[4668] | 242 | // Send a packet to the host |
---|
[4514] | 243 | transport_send(socket_index, from, &send_buffer, 1); // Send the buffer of data |
---|
[4668] | 244 | break; |
---|
[4284] | 245 | |
---|
[4668] | 246 | default: |
---|
| 247 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Received unknown status for message: %d\n", status); |
---|
| 248 | break; |
---|
| 249 | } |
---|
| 250 | break; |
---|
| 251 | |
---|
| 252 | default: |
---|
| 253 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Received packet with unknown packet type: %d\n", (wl_header_rx->pkt_type)); |
---|
| 254 | break; |
---|
| 255 | } |
---|
[1915] | 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | |
---|
[2054] | 260 | /*****************************************************************************/ |
---|
| 261 | /** |
---|
[4514] | 262 | * This function is used to send a message over Ethernet |
---|
| 263 | * |
---|
| 264 | * @param socket_index - Index of the socket on which to send message |
---|
| 265 | * @param to - Pointer to socket address structure to send message |
---|
| 266 | * @param buffers - Array of transport buffers to send |
---|
| 267 | * @param num_buffers - Number of transport buffers in 'buffers' array |
---|
| 268 | * |
---|
[4668] | 269 | * @return None |
---|
[4514] | 270 | * |
---|
| 271 | * @note This function requires that the first transport buffer in the 'buffers' |
---|
| 272 | * array contain the WARPLab transport header. |
---|
| 273 | * |
---|
[4668] | 274 | *****************************************************************************/ |
---|
[4514] | 275 | void transport_send(int socket_index, struct sockaddr * to, warp_ip_udp_buffer ** buffers, u32 num_buffers) { |
---|
[1915] | 276 | |
---|
[4514] | 277 | u32 i; |
---|
| 278 | int status; |
---|
[4668] | 279 | wl_transport_header * wl_header_tx; |
---|
| 280 | u16 buffer_length = 0; |
---|
[4387] | 281 | |
---|
[4668] | 282 | // Check that we have a valid socket to send a message on |
---|
| 283 | if (socket_index == SOCKET_INVALID_SOCKET) { |
---|
| 284 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Invalid socket.\n"); |
---|
| 285 | return; |
---|
| 286 | } |
---|
[4514] | 287 | |
---|
[4668] | 288 | // Initialize the header |
---|
[4514] | 289 | // NOTE: We require that the first warp_ip_udp_buffer always contain the wl_transport_header |
---|
| 290 | // |
---|
[4668] | 291 | wl_header_tx = (wl_transport_header *)(buffers[0]->data); |
---|
[4514] | 292 | |
---|
| 293 | // Compute the length |
---|
| 294 | for (i = 0; i < num_buffers; i++) { |
---|
| 295 | buffer_length += buffers[i]->size; |
---|
| 296 | } |
---|
| 297 | |
---|
[4668] | 298 | // |
---|
| 299 | // NOTE: Through performance testing, we found that it was faster to just manipulate the header |
---|
| 300 | // in place vs creating a copy, updating the header and then restoring the copy. |
---|
| 301 | // |
---|
[4514] | 302 | |
---|
[4668] | 303 | // Make the outgoing transport header endian safe for sending on the network |
---|
| 304 | // NOTE: Set the 'length' to the computed value above |
---|
| 305 | // |
---|
| 306 | wl_header_tx->dest_id = Xil_Htons(wl_header_tx->dest_id); |
---|
| 307 | wl_header_tx->src_id = Xil_Htons(wl_header_tx->src_id); |
---|
| 308 | wl_header_tx->length = Xil_Htons(buffer_length + WARP_IP_UDP_DELIM_LEN); |
---|
| 309 | wl_header_tx->seq_num = Xil_Htons(wl_header_tx->seq_num); |
---|
| 310 | wl_header_tx->flags = Xil_Htons(wl_header_tx->flags); |
---|
[4514] | 311 | |
---|
[4668] | 312 | // Update the green LEDs for every packet sent |
---|
| 313 | increment_green_leds_one_hot(); |
---|
[4514] | 314 | |
---|
[4668] | 315 | // Send the Ethernet packet |
---|
| 316 | status = socket_sendto(socket_index, to, buffers, num_buffers); |
---|
[4514] | 317 | |
---|
[4668] | 318 | // Restore wl_header_tx |
---|
| 319 | wl_header_tx->dest_id = Xil_Ntohs(wl_header_tx->dest_id); |
---|
| 320 | wl_header_tx->src_id = Xil_Ntohs(wl_header_tx->src_id); |
---|
| 321 | wl_header_tx->length = 0; |
---|
| 322 | wl_header_tx->seq_num = Xil_Ntohs(wl_header_tx->seq_num); |
---|
| 323 | wl_header_tx->flags = Xil_Ntohs(wl_header_tx->flags); |
---|
[4514] | 324 | |
---|
[4668] | 325 | // Check that the packet was sent correctly |
---|
| 326 | if (status == WARP_IP_UDP_FAILURE) { |
---|
| 327 | wl_printf(WL_PRINT_WARNING, print_type_transport, "Issue sending packet %d to host.\n", wl_header_tx->seq_num); |
---|
| 328 | } |
---|
[4514] | 329 | } |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | /*****************************************************************************/ |
---|
| 335 | /** |
---|
| 336 | * Process Transport Commands |
---|
| 337 | * |
---|
| 338 | * This function is part of the Ethernet processing system and will process the |
---|
| 339 | * various transport related commands. |
---|
| 340 | * |
---|
| 341 | * @param socket_index - Index of the socket on which to send message |
---|
| 342 | * @param from - Pointer to socket address structure (struct sockaddr *) where command is from |
---|
| 343 | * @param command - Pointer to WARPLab Command |
---|
| 344 | * @param response - Pointer to WARPLab Response |
---|
| 345 | * |
---|
[4668] | 346 | * @return int - Status of the command: |
---|
| 347 | * NO_RESP_SENT - No response has been sent |
---|
| 348 | * RESP_SENT - A response has been sent |
---|
[4514] | 349 | * |
---|
| 350 | * @note See on-line documentation for more information about the Ethernet |
---|
| 351 | * packet structure for WARPLab: www.warpproject.org |
---|
| 352 | * |
---|
[4668] | 353 | *****************************************************************************/ |
---|
[4514] | 354 | int transport_process_cmd(int socket_index, void * from, wl_cmd_resp * command, wl_cmd_resp * response) { |
---|
| 355 | |
---|
[4668] | 356 | // |
---|
[4514] | 357 | // IMPORTANT ENDIAN NOTES: |
---|
[4668] | 358 | // - command |
---|
| 359 | // - header - Already endian swapped by the framework (safe to access directly) |
---|
| 360 | // - args - Must be endian swapped as necessary by code (framework does not know the contents of the command) |
---|
| 361 | // - response |
---|
| 362 | // - header - Will be endian swapped by the framework (safe to write directly) |
---|
| 363 | // - args - Must be endian swapped as necessary by code (framework does not know the contents of the response) |
---|
| 364 | // |
---|
[4514] | 365 | |
---|
[4668] | 366 | // Standard variables |
---|
[4514] | 367 | u32 resp_sent = NO_RESP_SENT; |
---|
| 368 | |
---|
| 369 | wl_cmd_resp_hdr * cmd_hdr = command->header; |
---|
| 370 | u32 * cmd_args_32 = command->args; |
---|
| 371 | u32 cmd_id = WL_CMD_TO_CMDID(cmd_hdr->cmd); |
---|
| 372 | |
---|
| 373 | wl_cmd_resp_hdr * resp_hdr = response->header; |
---|
| 374 | u32 * resp_args_32 = response->args; |
---|
| 375 | u32 resp_index = 0; |
---|
| 376 | |
---|
| 377 | // Specific command variables |
---|
| 378 | u32 temp; |
---|
| 379 | u32 eth_dev_num; |
---|
[4668] | 380 | u32 size_index; |
---|
| 381 | u32 payload_size; |
---|
| 382 | u32 header_size; |
---|
[4514] | 383 | |
---|
[4668] | 384 | // Set up the response header |
---|
| 385 | resp_hdr->cmd = cmd_hdr->cmd; |
---|
| 386 | resp_hdr->length = 0; |
---|
| 387 | resp_hdr->num_args = 0; |
---|
[1915] | 388 | |
---|
[4668] | 389 | // Process the command |
---|
| 390 | switch(cmd_id){ |
---|
[1915] | 391 | |
---|
[4668] | 392 | //--------------------------------------------------------------------- |
---|
[4783] | 393 | case CMDID_TRANSPORT_PING: |
---|
[4668] | 394 | // |
---|
| 395 | // Nothing actually needs to be done when receiving the ping command. The framework is going |
---|
| 396 | // to respond regardless, which is all the host wants. |
---|
| 397 | // |
---|
| 398 | break; |
---|
[1915] | 399 | |
---|
[4668] | 400 | //--------------------------------------------------------------------- |
---|
[4783] | 401 | case CMDID_TRANSPORT_PAYLOAD_SIZE_TEST: |
---|
[4668] | 402 | // |
---|
| 403 | // Due to packet fragmentation, it is not safe to just return the packet length. We have seen |
---|
| 404 | // an issue where the host will send a 1514 byte fragment which results in a payload size of |
---|
| 405 | // 1472 and causes the transport to not behave correctly. Therefore, we need to find the |
---|
| 406 | // last valid command argument and check that against the packet length. |
---|
| 407 | // |
---|
[4709] | 408 | header_size = (sizeof(wl_transport_header) + sizeof(wl_cmd_resp_hdr)); // Transport / Command headers |
---|
| 409 | size_index = (((warp_ip_udp_buffer *)(command->buffer))->length - sizeof(wl_cmd_resp_hdr)) / 4; // Final index into command args (/4 truncates) |
---|
[4387] | 410 | |
---|
[4668] | 411 | // Check the value in the command args to make sure it matches the size_index |
---|
[4758] | 412 | // NOTE: This indexing step works because the payload is filled with incrementing number starting at 1. |
---|
| 413 | // |
---|
[4668] | 414 | payload_size = (Xil_Htonl(cmd_args_32[size_index - 1]) * 4) + header_size; |
---|
| 415 | temp = ((size_index * 4) + header_size); |
---|
[4387] | 416 | |
---|
[4668] | 417 | if (payload_size != temp) { |
---|
| 418 | wl_printf(WL_PRINT_WARNING, print_type_transport, "Payload size mismatch. Value in command args does not match index: %d != %d\n", payload_size, temp); |
---|
| 419 | } |
---|
[4387] | 420 | |
---|
[4668] | 421 | resp_args_32[resp_index++] = Xil_Ntohl(payload_size); |
---|
[1915] | 422 | |
---|
[4668] | 423 | resp_hdr->length += (resp_index * sizeof(resp_args_32)); |
---|
| 424 | resp_hdr->num_args = 1; |
---|
| 425 | break; |
---|
[1915] | 426 | |
---|
[4668] | 427 | //--------------------------------------------------------------------- |
---|
[4783] | 428 | case CMDID_TRANSPORT_NODE_GROUP_ID_ADD: |
---|
[4668] | 429 | eth_dev_num = socket_get_eth_dev_num(socket_index); |
---|
[2026] | 430 | |
---|
[4668] | 431 | if (eth_dev_num != WARP_IP_UDP_INVALID_ETH_DEVICE) { |
---|
| 432 | eth_devices[eth_dev_num].group_id = (eth_devices[eth_dev_num].group_id | Xil_Htonl(cmd_args_32[0])); |
---|
| 433 | } else { |
---|
| 434 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Add Group ID - Invalid socket index: %d\n", socket_index); |
---|
| 435 | } |
---|
| 436 | break; |
---|
[4514] | 437 | |
---|
[4668] | 438 | //--------------------------------------------------------------------- |
---|
[4783] | 439 | case CMDID_TRANSPORT_NODE_GROUP_ID_CLEAR: |
---|
[4668] | 440 | eth_dev_num = socket_get_eth_dev_num(socket_index); |
---|
[4514] | 441 | |
---|
[4668] | 442 | if (eth_dev_num != WARP_IP_UDP_INVALID_ETH_DEVICE) { |
---|
| 443 | eth_devices[eth_dev_num].group_id = (eth_devices[eth_dev_num].group_id & ~Xil_Htonl(cmd_args_32[0])); |
---|
| 444 | } else { |
---|
| 445 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Clear Group ID - Invalid socket index: %d\n", socket_index); |
---|
| 446 | } |
---|
| 447 | break; |
---|
[2026] | 448 | |
---|
[4668] | 449 | //--------------------------------------------------------------------- |
---|
| 450 | default: |
---|
| 451 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Unknown user command ID: %d\n", cmd_id); |
---|
| 452 | break; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | return resp_sent; |
---|
[2054] | 456 | } |
---|
[2026] | 457 | |
---|
[2054] | 458 | |
---|
| 459 | |
---|
| 460 | /*****************************************************************************/ |
---|
| 461 | /** |
---|
[4514] | 462 | * Close the unicast and broadcast sockets associated with a given Ethernet device |
---|
| 463 | * |
---|
| 464 | * @param eth_dev_num - Ethernet device number |
---|
| 465 | * |
---|
[4668] | 466 | * @return None |
---|
[4514] | 467 | * |
---|
[4668] | 468 | *****************************************************************************/ |
---|
[4284] | 469 | void transport_close(u32 eth_dev_num) { |
---|
[4514] | 470 | |
---|
[4668] | 471 | if (transport_check_device(eth_dev_num) == XST_SUCCESS) { |
---|
| 472 | socket_close(eth_devices[eth_dev_num].unicast_socket); |
---|
| 473 | socket_close(eth_devices[eth_dev_num].broadcast_socket); |
---|
| 474 | } |
---|
[2026] | 475 | } |
---|
| 476 | |
---|
| 477 | |
---|
| 478 | |
---|
[2054] | 479 | /*****************************************************************************/ |
---|
| 480 | /** |
---|
[4514] | 481 | * Create and bind a socket for the Ethernet device |
---|
[4292] | 482 | * |
---|
[4514] | 483 | * @param eth_dev_num - Ethernet device number |
---|
| 484 | * @param socket_index - Socket index (return value) |
---|
| 485 | * @param udp_port - UDP port number |
---|
[4292] | 486 | * |
---|
[4668] | 487 | * @return int - Status of the command: |
---|
| 488 | * XST_SUCCESS - Command completed successfully |
---|
| 489 | * XST_FAILURE - There was an error in the command |
---|
[4514] | 490 | * |
---|
[4668] | 491 | *****************************************************************************/ |
---|
[4514] | 492 | int transport_config_socket(u32 eth_dev_num, int * socket_index, u32 udp_port) { |
---|
[1915] | 493 | |
---|
[4514] | 494 | int status; |
---|
| 495 | int tmp_socket = *socket_index; |
---|
| 496 | |
---|
| 497 | // Release socket if it is already bound |
---|
| 498 | if (tmp_socket != SOCKET_INVALID_SOCKET) { |
---|
[4668] | 499 | socket_close(tmp_socket); |
---|
[4514] | 500 | } |
---|
| 501 | |
---|
| 502 | // Create a new socket |
---|
| 503 | tmp_socket = socket_socket(AF_INET, SOCK_DGRAM, 0); |
---|
| 504 | |
---|
| 505 | if (tmp_socket == SOCKET_INVALID_SOCKET) { |
---|
[4668] | 506 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Could not create socket\n"); |
---|
[2054] | 507 | |
---|
[4668] | 508 | * socket_index = SOCKET_INVALID_SOCKET; |
---|
[2054] | 509 | |
---|
[4668] | 510 | return XST_FAILURE; |
---|
[4514] | 511 | } |
---|
[2054] | 512 | |
---|
[4514] | 513 | // Bind the socket |
---|
| 514 | status = socket_bind_eth(tmp_socket, eth_dev_num, udp_port); |
---|
| 515 | |
---|
| 516 | if (status == WARP_IP_UDP_FAILURE) { |
---|
[4668] | 517 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Unable to bind socket on port: %d\n", udp_port); |
---|
[4514] | 518 | |
---|
[4668] | 519 | socket_close(tmp_socket); |
---|
[4514] | 520 | |
---|
[4668] | 521 | * socket_index = SOCKET_INVALID_SOCKET; |
---|
[4514] | 522 | |
---|
[4668] | 523 | return XST_FAILURE; |
---|
[4514] | 524 | } |
---|
| 525 | |
---|
[4668] | 526 | * socket_index = tmp_socket; |
---|
[4514] | 527 | |
---|
[4668] | 528 | return XST_SUCCESS; |
---|
[4292] | 529 | } |
---|
[2873] | 530 | |
---|
| 531 | |
---|
| 532 | |
---|
[4292] | 533 | /*****************************************************************************/ |
---|
| 534 | /** |
---|
| 535 | * This function will configure the unicast and broadcast sockets to be used |
---|
| 536 | * by the transport using the default transport_receive_callback() function. |
---|
| 537 | * |
---|
[4668] | 538 | * @param eth_dev_num - Ethernet device number |
---|
| 539 | * @param unicast_port - Unicast port for the node |
---|
| 540 | * @param bcast_port - Broadcast port for the node |
---|
[4292] | 541 | * |
---|
[4668] | 542 | * @return int - Status of the command: |
---|
| 543 | * XST_SUCCESS - Command completed successfully |
---|
| 544 | * XST_FAILURE - There was an error in the command |
---|
| 545 | * |
---|
| 546 | *****************************************************************************/ |
---|
[4292] | 547 | int transport_config_sockets(u32 eth_dev_num, u32 unicast_port, u32 bcast_port) { |
---|
[4668] | 548 | int status = XST_SUCCESS; |
---|
[2873] | 549 | |
---|
[4668] | 550 | status = transport_config_socket(eth_dev_num, &(eth_devices[eth_dev_num].unicast_socket), unicast_port); |
---|
[4292] | 551 | if (status == XST_FAILURE) { return status; } |
---|
[2054] | 552 | |
---|
[4668] | 553 | status = transport_config_socket(eth_dev_num, &(eth_devices[eth_dev_num].broadcast_socket), bcast_port); |
---|
[4292] | 554 | if (status == XST_FAILURE) { return status; } |
---|
[2054] | 555 | |
---|
[4668] | 556 | wl_printf(WL_PRINT_NONE, NULL, " Listening on UDP ports %d (unicast) and %d (broadcast)\n", unicast_port, bcast_port); |
---|
[4292] | 557 | |
---|
[4668] | 558 | return status; |
---|
[1915] | 559 | } |
---|
| 560 | |
---|
[2054] | 561 | |
---|
| 562 | |
---|
| 563 | /*****************************************************************************/ |
---|
| 564 | /** |
---|
[4668] | 565 | * This function is the Transport callback that allows WARPLab to process |
---|
| 566 | * a received Ethernet packet |
---|
| 567 | * |
---|
| 568 | * @param hander - Pointer to the transport receive callback function |
---|
| 569 | * |
---|
| 570 | * @return int - Status of the command: |
---|
| 571 | * XST_SUCCESS - Command completed successfully |
---|
| 572 | * |
---|
| 573 | *****************************************************************************/ |
---|
[4514] | 574 | int transport_set_process_hton_msg_callback(void(*handler)) { |
---|
[4668] | 575 | process_hton_msg_callback = handler; |
---|
| 576 | |
---|
| 577 | return XST_SUCCESS; |
---|
[1915] | 578 | } |
---|
| 579 | |
---|
| 580 | |
---|
| 581 | |
---|
[2054] | 582 | /*****************************************************************************/ |
---|
| 583 | /** |
---|
[4668] | 584 | * This function will check the link status of a Ethernet controller |
---|
| 585 | * |
---|
| 586 | * @param eth_dev_num - Ethernet device number |
---|
| 587 | * @param unicast_port - Unicast port for the node |
---|
| 588 | * @param bcast_port - Broadcast port for the node |
---|
| 589 | * |
---|
| 590 | * @return int - Status of the command: |
---|
| 591 | * LINK_READY - Ethernet controller is ready to be used |
---|
| 592 | * LINK_NOT_READY - Ethernet controller is not ready to be used |
---|
| 593 | * |
---|
| 594 | *****************************************************************************/ |
---|
[4284] | 595 | int transport_link_status(u32 eth_dev_num) { |
---|
[2054] | 596 | |
---|
[4668] | 597 | int status = LINK_READY; |
---|
| 598 | u16 reg_val = transport_get_ethernet_status(eth_dev_num); |
---|
[2054] | 599 | |
---|
[4668] | 600 | if(reg_val & ETH_PHY_REG_17_0_LINKUP) { |
---|
| 601 | status = LINK_READY; |
---|
| 602 | } else { |
---|
| 603 | status = LINK_NOT_READY; |
---|
| 604 | } |
---|
[2054] | 605 | |
---|
[4668] | 606 | return status; |
---|
[4417] | 607 | } |
---|
[4284] | 608 | |
---|
[4417] | 609 | |
---|
[4668] | 610 | |
---|
[4417] | 611 | /*****************************************************************************/ |
---|
| 612 | /** |
---|
[4668] | 613 | * This function will update the link speed of a Ethernet controller |
---|
| 614 | * |
---|
| 615 | * @param eth_dev_num - Ethernet device number |
---|
| 616 | * @param wait_for_negotiation - Flag to wait for auto-negotiation of Ethernet link speed |
---|
| 617 | * |
---|
| 618 | * @return speed - Ethernet link speed that was chosen |
---|
| 619 | * |
---|
| 620 | *****************************************************************************/ |
---|
[4417] | 621 | u32 transport_update_link_speed(u32 eth_dev_num, u32 wait_for_negotiation) { |
---|
| 622 | |
---|
[4673] | 623 | volatile u16 reg_val = 0; |
---|
| 624 | u32 negotiated = 1; |
---|
| 625 | u16 speed = 0; |
---|
[4417] | 626 | |
---|
[4673] | 627 | u32 start_time = get_usec_timestamp(); |
---|
| 628 | u32 end_time = start_time; |
---|
[4417] | 629 | |
---|
[4668] | 630 | // Make sure the Ethernet device is initialized |
---|
| 631 | if (eth_devices[eth_dev_num].initialized == WL_ETH_DEV_INITIALIZED) { |
---|
[4417] | 632 | |
---|
[4668] | 633 | xil_printf(" ETH %c speed ", warp_conv_eth_dev_num(eth_dev_num)); |
---|
[4417] | 634 | |
---|
[4668] | 635 | reg_val = transport_get_ethernet_status(eth_dev_num); |
---|
[4417] | 636 | |
---|
[4668] | 637 | if (wait_for_negotiation == ETH_WAIT_FOR_AUTO_NEGOTIATION) { |
---|
[4417] | 638 | |
---|
[4668] | 639 | while((reg_val & ETH_PHY_REG_17_0_SPEED_RESOLVED) == 0) { |
---|
| 640 | usleep(1000); |
---|
| 641 | reg_val = transport_get_ethernet_status(eth_dev_num); |
---|
| 642 | } |
---|
[4417] | 643 | |
---|
[4668] | 644 | speed = ETH_PHY_SPEED_TO_MBPS((reg_val & ETH_PHY_REG_17_0_SPEED)); |
---|
| 645 | end_time = get_usec_timestamp(); |
---|
[4417] | 646 | |
---|
[4668] | 647 | } else { |
---|
| 648 | // Check to see if the Ethernet controller has auto-negotiated a speed |
---|
| 649 | if (reg_val & ETH_PHY_REG_17_0_SPEED_RESOLVED) { |
---|
| 650 | speed = ETH_PHY_SPEED_TO_MBPS((reg_val & ETH_PHY_REG_17_0_SPEED)); |
---|
| 651 | } else { |
---|
| 652 | speed = eth_devices[eth_dev_num].default_speed; |
---|
| 653 | negotiated = 0; |
---|
| 654 | } |
---|
| 655 | } |
---|
[4417] | 656 | |
---|
[4673] | 657 | // Set the operating speed of the Ethernet controller |
---|
[4668] | 658 | eth_set_operating_speed(eth_dev_num, speed); |
---|
[4437] | 659 | |
---|
[4673] | 660 | // Set the operating speed of the Ethernet PHY |
---|
| 661 | transport_set_eth_phy_speed(eth_dev_num, speed); |
---|
| 662 | |
---|
| 663 | // Sleep for a short period of time to let everything settle |
---|
[4668] | 664 | usleep(1 * 10000); |
---|
[4417] | 665 | |
---|
[4668] | 666 | } else { |
---|
| 667 | wl_printf(WL_PRINT_NONE, NULL, " ETH %c not initialized. Link speed not updated.\n", warp_conv_eth_dev_num(eth_dev_num)); |
---|
| 668 | } |
---|
[2054] | 669 | |
---|
[4668] | 670 | if (negotiated) { |
---|
[4417] | 671 | xil_printf("%d Mbps (auto-negotiated", speed); |
---|
[2054] | 672 | |
---|
[4668] | 673 | if (start_time != end_time) { |
---|
[4417] | 674 | xil_printf(" in %d usec)\n", (end_time - start_time)); |
---|
[4668] | 675 | } else { |
---|
[4417] | 676 | xil_printf(")\n"); |
---|
[4668] | 677 | } |
---|
| 678 | } else { |
---|
[4417] | 679 | xil_printf("%d Mbps (default)\n", speed); |
---|
[4668] | 680 | } |
---|
[1915] | 681 | |
---|
[4668] | 682 | return speed; |
---|
[1915] | 683 | } |
---|
[2054] | 684 | |
---|
| 685 | |
---|
[4417] | 686 | |
---|
[4514] | 687 | /*****************************************************************************/ |
---|
| 688 | /** |
---|
[4673] | 689 | * This function set the speed of a Ethernet PHY |
---|
| 690 | * |
---|
| 691 | * @param eth_dev_num - Ethernet device number |
---|
| 692 | * @param speed - Speed |
---|
| 693 | * |
---|
| 694 | * @return None |
---|
| 695 | * |
---|
| 696 | *****************************************************************************/ |
---|
| 697 | void transport_set_eth_phy_speed(u32 eth_dev_num, u32 speed) { |
---|
| 698 | |
---|
| 699 | // See Ethernet PHY specification for documentation on the values used for PHY commands |
---|
| 700 | u16 phy_ctrl_reg_val; |
---|
| 701 | |
---|
| 702 | // Read the PHY Control register |
---|
| 703 | eth_read_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, &phy_ctrl_reg_val); |
---|
| 704 | |
---|
| 705 | // Based on the argument configure the appropriate bits to set the desired speed |
---|
| 706 | switch (speed) { |
---|
| 707 | case ETH_PHY_SPEED_1000_MBPS: |
---|
| 708 | // Set speed to 1000 Mbps (MSB = 1; LSB = 0) |
---|
| 709 | phy_ctrl_reg_val = (phy_ctrl_reg_val & ~ETH_PHY_REG_0_SPEED_LSB) | ETH_PHY_REG_0_SPEED_MSB; |
---|
| 710 | break; |
---|
| 711 | case ETH_PHY_SPEED_100_MBPS: |
---|
| 712 | // Set speed to 100 Mbps (MSB = 0; LSB = 1) |
---|
| 713 | phy_ctrl_reg_val = (phy_ctrl_reg_val & ~ETH_PHY_REG_0_SPEED_MSB) | ETH_PHY_REG_0_SPEED_LSB; |
---|
| 714 | break; |
---|
| 715 | case ETH_PHY_SPEED_10_MBPS: |
---|
| 716 | // Set speed to 10 Mbps (MSB = 0; LSB = 0) |
---|
| 717 | phy_ctrl_reg_val = phy_ctrl_reg_val & ~(ETH_PHY_REG_0_SPEED_MSB | ETH_PHY_REG_0_SPEED_LSB); |
---|
| 718 | break; |
---|
| 719 | default: |
---|
| 720 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Ethernet %c invalid speed: %d.\n", warp_conv_eth_dev_num(eth_dev_num), speed); |
---|
| 721 | break; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | // Write the value to the PHY and trigger a reset to update PHY internal state |
---|
| 725 | eth_write_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, phy_ctrl_reg_val); |
---|
| 726 | eth_write_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, (ETH_PHY_REG_0_RESET | phy_ctrl_reg_val)); |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | |
---|
| 730 | |
---|
| 731 | /*****************************************************************************/ |
---|
| 732 | /** |
---|
| 733 | * This function set the auto-negotiation state of a Ethernet controller |
---|
| 734 | * |
---|
| 735 | * @param eth_dev_num - Ethernet device number |
---|
| 736 | * @param enable - Enable / Disable auto-negotiation |
---|
| 737 | * |
---|
| 738 | * @return None |
---|
| 739 | * |
---|
| 740 | *****************************************************************************/ |
---|
| 741 | void transport_set_eth_phy_auto_negotiation(u32 eth_dev_num, u32 enable) { |
---|
| 742 | |
---|
| 743 | // See Ethernet PHY specification for documentation on the values used for PHY commands |
---|
| 744 | u16 phy_ctrl_reg_val; |
---|
| 745 | |
---|
| 746 | // Read the PHY Control register |
---|
| 747 | eth_read_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, &phy_ctrl_reg_val); |
---|
| 748 | |
---|
| 749 | // Based on the argument enable or disable auto-negotiation |
---|
| 750 | if (enable) { |
---|
| 751 | // Enable auto-negotiation |
---|
| 752 | phy_ctrl_reg_val = phy_ctrl_reg_val | ETH_PHY_REG_0_AUTO_NEGOTIATION; |
---|
| 753 | } else { |
---|
| 754 | // Disable auto-negotiation |
---|
| 755 | phy_ctrl_reg_val = phy_ctrl_reg_val & ~ETH_PHY_REG_0_AUTO_NEGOTIATION; |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | // Write the value to the PHY and trigger a reset to update PHY internal state |
---|
| 759 | eth_write_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, phy_ctrl_reg_val); |
---|
| 760 | eth_write_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_CONTROL_REG, (ETH_PHY_REG_0_RESET | phy_ctrl_reg_val)); |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | |
---|
| 764 | |
---|
| 765 | /*****************************************************************************/ |
---|
| 766 | /** |
---|
[4514] | 767 | * Check the Ethernet device of the transport |
---|
| 768 | * |
---|
[4668] | 769 | * @param eth_dev_num - Ethernet device number |
---|
[4514] | 770 | * |
---|
| 771 | * @return int - Status of the command: |
---|
| 772 | * XST_SUCCESS - Command completed successfully |
---|
| 773 | * XST_FAILURE - There was an error in the command |
---|
| 774 | * |
---|
| 775 | ******************************************************************************/ |
---|
[4417] | 776 | |
---|
[4514] | 777 | int transport_check_device(u32 eth_dev_num) { |
---|
| 778 | |
---|
[4668] | 779 | // Check that we have a valid Ethernet device for the transport |
---|
| 780 | if (eth_dev_num >= WL_NUM_ETH_DEVICES) { |
---|
| 781 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Ethernet %c is not available on WARP HW.\n", warp_conv_eth_dev_num(eth_dev_num)); |
---|
| 782 | return XST_FAILURE; |
---|
| 783 | } |
---|
[4514] | 784 | |
---|
[4668] | 785 | return XST_SUCCESS; |
---|
[4514] | 786 | } |
---|
| 787 | |
---|
| 788 | |
---|
| 789 | |
---|
[4453] | 790 | /**********************************************************************************************************************/ |
---|
| 791 | /** |
---|
| 792 | * @brief WARP v3 Specific Functions |
---|
| 793 | * |
---|
| 794 | **********************************************************************************************************************/ |
---|
| 795 | |
---|
| 796 | |
---|
| 797 | /***************************** Include Files *********************************/ |
---|
| 798 | |
---|
| 799 | #include <w3_iic_eeprom.h> |
---|
| 800 | #include <w3_userio.h> |
---|
| 801 | |
---|
| 802 | |
---|
| 803 | /*************************** Constant Definitions ****************************/ |
---|
| 804 | |
---|
| 805 | /*********************** Global Variable Definitions *************************/ |
---|
| 806 | |
---|
| 807 | /*************************** Variable Definitions ****************************/ |
---|
| 808 | |
---|
| 809 | /*************************** Functions Prototypes ****************************/ |
---|
| 810 | |
---|
[4514] | 811 | int transport_read_ip_addr(u32 eth_dev_num, u8 * ip_addr); |
---|
| 812 | |
---|
| 813 | |
---|
[4453] | 814 | /******************************** Functions **********************************/ |
---|
| 815 | |
---|
[4417] | 816 | /*****************************************************************************/ |
---|
| 817 | /** |
---|
[4514] | 818 | * Read the IP / MAC address from the node for the Ethernet device |
---|
[4453] | 819 | * |
---|
[4668] | 820 | * @param eth_dev_num - Ethernet device number |
---|
| 821 | * @param hw_addr - Pointer to an u8 array to return the MAC address |
---|
| 822 | * @param ip_addr - Pointer to an u8 array to return the IP address |
---|
[4453] | 823 | * |
---|
[4514] | 824 | * @return int - Status of the command: |
---|
| 825 | * XST_SUCCESS - Command completed successfully |
---|
| 826 | * XST_FAILURE - There was an error in the command |
---|
| 827 | * |
---|
[4453] | 828 | ******************************************************************************/ |
---|
[4514] | 829 | int transport_get_hw_info(u32 eth_dev_num, u8 * hw_addr, u8 * ip_addr) { |
---|
| 830 | |
---|
[4668] | 831 | int status = XST_SUCCESS; |
---|
[4453] | 832 | |
---|
[4668] | 833 | // Read the MAC address from the node |
---|
| 834 | w3_eeprom_readEthAddr(EEPROM_BASEADDR, eth_dev_num, hw_addr); |
---|
[4453] | 835 | |
---|
[4668] | 836 | wl_printf(WL_PRINT_NONE, NULL, " ETH %c MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n", |
---|
| 837 | warp_conv_eth_dev_num(eth_dev_num), hw_addr[0], hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]); |
---|
[4453] | 838 | |
---|
[4514] | 839 | // Read the IP address from the node |
---|
[4668] | 840 | transport_read_ip_addr(eth_dev_num, ip_addr); |
---|
[4453] | 841 | |
---|
[4668] | 842 | wl_printf(WL_PRINT_NONE, NULL, " ETH %c IP Address: %d.%d.%d.%d\n", |
---|
| 843 | warp_conv_eth_dev_num(eth_dev_num), ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]); |
---|
[4453] | 844 | |
---|
| 845 | return status; |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | |
---|
| 849 | |
---|
| 850 | /*****************************************************************************/ |
---|
| 851 | /** |
---|
[4514] | 852 | * Read the IP address from the node |
---|
[4453] | 853 | * |
---|
[4668] | 854 | * @param eth_dev_num - Ethernet device number |
---|
| 855 | * @param ip_addr - Pointer to an u8 array to return the IP address |
---|
[4453] | 856 | * |
---|
[4514] | 857 | * @return int - Status of the command: |
---|
| 858 | * XST_SUCCESS - Command completed successfully |
---|
| 859 | * XST_FAILURE - There was an error in the command |
---|
[4453] | 860 | * |
---|
| 861 | ******************************************************************************/ |
---|
[4514] | 862 | int transport_read_ip_addr(u32 eth_dev_num, u8 * ip_addr) { |
---|
[4453] | 863 | |
---|
[4673] | 864 | u32 tmp_ip_address = 0; |
---|
[4453] | 865 | |
---|
[4668] | 866 | switch (eth_dev_num) { |
---|
[4453] | 867 | case WL_ETH_A: |
---|
[4673] | 868 | tmp_ip_address = WL_ETH_A_IP_ADDR_BASE; |
---|
[4453] | 869 | break; |
---|
| 870 | case WL_ETH_B: |
---|
[4673] | 871 | tmp_ip_address = WL_ETH_B_IP_ADDR_BASE; |
---|
[4453] | 872 | break; |
---|
[4668] | 873 | } |
---|
[4453] | 874 | |
---|
[4673] | 875 | if (tmp_ip_address == 0) { |
---|
| 876 | return XST_FAILURE; |
---|
| 877 | } |
---|
| 878 | |
---|
[4668] | 879 | // Populate the output array |
---|
| 880 | ip_addr[0] = (tmp_ip_address >> 24) & 0xFF; |
---|
| 881 | ip_addr[1] = (tmp_ip_address >> 16) & 0xFF; |
---|
| 882 | ip_addr[2] = (tmp_ip_address >> 8) & 0xFF; |
---|
| 883 | ip_addr[3] = (node + 1); // IP ADDR = x.y.z.( node + 1 ) |
---|
[4453] | 884 | |
---|
[4514] | 885 | return XST_SUCCESS; |
---|
[4453] | 886 | } |
---|
| 887 | |
---|
[4417] | 888 | |
---|
[4453] | 889 | |
---|
| 890 | /*****************************************************************************/ |
---|
| 891 | /** |
---|
[4668] | 892 | * This function will read the status of a Ethernet controller |
---|
| 893 | * |
---|
| 894 | * @param eth_dev_num - Ethernet device number |
---|
| 895 | * |
---|
| 896 | * @return u16 - Bits are defined for the WARP Ethernet chip. See wl_transport.h |
---|
| 897 | * for defines for relevant fields |
---|
| 898 | * |
---|
| 899 | *****************************************************************************/ |
---|
[4453] | 900 | u16 transport_get_ethernet_status(u32 eth_dev_num) { |
---|
| 901 | |
---|
[4668] | 902 | u16 reg_val = 0; |
---|
[4453] | 903 | |
---|
[4668] | 904 | // Check that we are initializing a valid Ethernet device for the transport |
---|
| 905 | if (transport_check_device(eth_dev_num) != XST_SUCCESS) { |
---|
| 906 | return LINK_NOT_READY; |
---|
| 907 | } |
---|
[4453] | 908 | |
---|
[4668] | 909 | if (eth_devices[eth_dev_num].initialized == WL_ETH_DEV_INITIALIZED) { |
---|
[4417] | 910 | |
---|
[4668] | 911 | // Check if the Ethernet PHY reports a valid link |
---|
| 912 | eth_read_phy_reg(eth_dev_num, eth_devices[eth_dev_num].phy_addr, ETH_PHY_STATUS_REG, ®_val); |
---|
| 913 | } |
---|
[4417] | 914 | |
---|
[4668] | 915 | return reg_val; |
---|
[4417] | 916 | } |
---|
| 917 | |
---|
| 918 | |
---|
| 919 | |
---|
[4453] | 920 | /*****************************************************************************/ |
---|
| 921 | /** |
---|
[4514] | 922 | * Initialize the information about the Ethernet device |
---|
| 923 | * |
---|
[4668] | 924 | * @param eth_dev_num - Ethernet device number |
---|
[4514] | 925 | * |
---|
[4668] | 926 | * @return None |
---|
[4514] | 927 | * |
---|
| 928 | ******************************************************************************/ |
---|
| 929 | void transport_wl_eth_dev_info_init(u32 eth_dev_num) { |
---|
| 930 | |
---|
| 931 | // Initialize Ethernet device |
---|
| 932 | switch (eth_dev_num) { |
---|
| 933 | case WL_ETH_A: |
---|
[4668] | 934 | eth_devices[eth_dev_num].default_speed = WL_ETH_A_DEFAULT_SPEED; |
---|
| 935 | eth_devices[eth_dev_num].phy_addr = WL_ETH_A_MDIO_PHYADDR; |
---|
[4514] | 936 | break; |
---|
| 937 | |
---|
| 938 | case WL_ETH_B: |
---|
[4668] | 939 | eth_devices[eth_dev_num].default_speed = WL_ETH_B_DEFAULT_SPEED; |
---|
| 940 | eth_devices[eth_dev_num].phy_addr = WL_ETH_B_MDIO_PHYADDR; |
---|
[4514] | 941 | break; |
---|
| 942 | |
---|
| 943 | default: |
---|
[4668] | 944 | xil_printf(" **** ERROR: Ethernet device %d not configured in hardware.", (eth_dev_num + 1)); |
---|
| 945 | break; |
---|
[4514] | 946 | } |
---|
| 947 | |
---|
| 948 | // Common initialization |
---|
[4668] | 949 | eth_devices[eth_dev_num].type = WL_IP_UDP_TRANSPORT; |
---|
| 950 | eth_devices[eth_dev_num].hw_addr[0] = 0; |
---|
| 951 | eth_devices[eth_dev_num].hw_addr[1] = 0; |
---|
| 952 | eth_devices[eth_dev_num].ip_addr = 0; |
---|
| 953 | eth_devices[eth_dev_num].unicast_socket = SOCKET_INVALID_SOCKET; |
---|
| 954 | eth_devices[eth_dev_num].broadcast_socket = SOCKET_INVALID_SOCKET; |
---|
| 955 | eth_devices[eth_dev_num].group_id = 0; |
---|
| 956 | eth_devices[eth_dev_num].initialized = WL_ETH_DEV_INITIALIZED; |
---|
[4453] | 957 | } |
---|
[4417] | 958 | |
---|
[4453] | 959 | |
---|
| 960 | |
---|
| 961 | /*****************************************************************************/ |
---|
| 962 | /** |
---|
| 963 | * @brief Transport subsystem initialization |
---|
| 964 | * |
---|
| 965 | * Initializes the transport subsystem |
---|
| 966 | * |
---|
[4668] | 967 | * @param eth_dev_num - Ethernet device number |
---|
| 968 | * @param init_driver - Initialize the WARP IP/UDP driver (should only be done once) |
---|
[4453] | 969 | * |
---|
[4514] | 970 | * @return int - Status of the command: |
---|
| 971 | * XST_SUCCESS - Command completed successfully |
---|
| 972 | * XST_FAILURE - There was an error in the command |
---|
[4453] | 973 | ******************************************************************************/ |
---|
[4514] | 974 | int transport_init(u32 eth_dev_num, u8 init_driver) { |
---|
[4453] | 975 | |
---|
[4668] | 976 | int status = XST_SUCCESS; |
---|
[4453] | 977 | |
---|
[4668] | 978 | u8 node_ip_addr[IP_ADDR_LEN]; |
---|
[4514] | 979 | u8 node_hw_addr[ETH_MAC_ADDR_LEN]; |
---|
[4453] | 980 | |
---|
[4514] | 981 | // Print initialization message |
---|
[4453] | 982 | wl_printf(WL_PRINT_NONE, NULL, "Configuring transport ...\n"); |
---|
| 983 | |
---|
[4514] | 984 | // Initialize the User callback for processing a packet |
---|
| 985 | process_hton_msg_callback = wl_null_callback; |
---|
[4453] | 986 | |
---|
[4668] | 987 | // Check that we are initializing a valid Ethernet device for the transport |
---|
| 988 | if (transport_check_device(eth_dev_num) != XST_SUCCESS) { |
---|
| 989 | return XST_FAILURE; |
---|
| 990 | } |
---|
[4453] | 991 | |
---|
[4668] | 992 | // Initialize the WARP UDP transport global variables |
---|
| 993 | if (init_driver) { |
---|
[4514] | 994 | warp_ip_udp_init(); |
---|
[4668] | 995 | } |
---|
[4453] | 996 | |
---|
[4668] | 997 | // Get device specific MAC / IP address from the EEPROM |
---|
| 998 | status = transport_get_hw_info(eth_dev_num, (u8 *)&node_hw_addr, (u8 *)&node_ip_addr); |
---|
[4453] | 999 | |
---|
[4668] | 1000 | if (status != XST_SUCCESS) { |
---|
| 1001 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Error retrieving node specific HW info from EEPROM: %d \n", status); |
---|
| 1002 | } |
---|
[4453] | 1003 | |
---|
[4514] | 1004 | // Initialize the Ethernet device (use verbose mode) |
---|
| 1005 | status = eth_init(eth_dev_num, node_hw_addr, node_ip_addr, 0x1); |
---|
[4453] | 1006 | |
---|
[4668] | 1007 | if (status != XST_SUCCESS) { |
---|
| 1008 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Ethernet %c initialization error\n", warp_conv_eth_dev_num(eth_dev_num)); |
---|
| 1009 | } |
---|
[4453] | 1010 | |
---|
[4668] | 1011 | // Initialize the wl_eth_devices structure |
---|
| 1012 | transport_wl_eth_dev_info_init(eth_dev_num); |
---|
[4453] | 1013 | |
---|
[4673] | 1014 | // Set the Ethernet link speed |
---|
| 1015 | if (WL_NEGOTIATE_ETH_LINK_SPEED) { |
---|
| 1016 | // Enable auto-negotiation in the Ethernet PHY |
---|
| 1017 | transport_set_eth_phy_auto_negotiation(eth_dev_num, WL_ENABLE); |
---|
[4453] | 1018 | |
---|
[4673] | 1019 | // Update the link speed |
---|
| 1020 | transport_update_link_speed(eth_dev_num, ETH_WAIT_FOR_AUTO_NEGOTIATION); |
---|
| 1021 | |
---|
| 1022 | } else { |
---|
| 1023 | // Disable auto-negotiation in the Ethernet PHY |
---|
| 1024 | transport_set_eth_phy_auto_negotiation(eth_dev_num, WL_DISABLE); |
---|
| 1025 | |
---|
| 1026 | // Update the link speed |
---|
| 1027 | transport_update_link_speed(eth_dev_num, ETH_DO_NOT_WAIT_FOR_AUTO_NEGOTIATION); |
---|
| 1028 | } |
---|
| 1029 | |
---|
[4668] | 1030 | // Start Ethernet device |
---|
| 1031 | status = eth_start_device(eth_dev_num); |
---|
[4453] | 1032 | |
---|
[4668] | 1033 | if (status != XST_SUCCESS) { |
---|
| 1034 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Cannot start Ethernet %c\n", warp_conv_eth_dev_num(eth_dev_num)); |
---|
| 1035 | } |
---|
[4453] | 1036 | |
---|
[4668] | 1037 | // Configure the Sockets for each Ethernet Interface |
---|
| 1038 | status = transport_config_sockets(eth_dev_num, NODE_UDP_UNICAST_PORT_BASE, NODE_UDP_MCAST_BASE); |
---|
[4453] | 1039 | |
---|
[4668] | 1040 | if (status != XST_SUCCESS) { |
---|
| 1041 | wl_printf(WL_PRINT_ERROR, print_type_transport, "Cannot configure sockets for Ethernet %c\n", warp_conv_eth_dev_num(eth_dev_num)); |
---|
| 1042 | } |
---|
[4514] | 1043 | |
---|
[4668] | 1044 | return status; |
---|
[4453] | 1045 | } |
---|
| 1046 | |
---|