drv_eth_mac.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * Copyright (C) 2016 CSI Project. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _CSI_ETH_H_
  17. #define _CSI_ETH_H_
  18. #include "drv_eth.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef void *eth_mac_handle_t;
  23. #define MAX_FRAMELEN 1518 /* (note: maximum ethernet frame length would be 1518) */
  24. #define CSI_ETH_MAC_API_VERSION CSI_DRIVER_VERSION_MAJOR_MINOR(2,1) /* API version */
  25. #define _CSI_Driver_ETH_MAC_(n) Driver_ETH_MAC##n
  26. #define CSI_Driver_ETH_MAC_(n) _CSI_Driver_ETH_MAC_(n)
  27. /****** Ethernet MAC Control Codes *****/
  28. #define CSI_ETH_MAC_CONFIGURE (0x01) ///< Configure MAC; arg = configuration
  29. #define CSI_ETH_MAC_CONTROL_TX (0x02) ///< Transmitter; arg: 0=disabled (default), 1=enabled
  30. #define CSI_ETH_MAC_CONTROL_RX (0x03) ///< Receiver; arg: 0=disabled (default), 1=enabled
  31. #define CSI_ETH_MAC_FLUSH (0x04) ///< Flush buffer; arg = CSI_ETH_MAC_FLUSH_...
  32. #define CSI_ETH_MAC_SLEEP (0x05) ///< Sleep mode; arg: 1=enter and wait for Magic packet, 0=exit
  33. #define CSI_ETH_MAC_VLAN_FILTER (0x06) ///< VLAN Filter for received frames; arg15..0: VLAN Tag; arg16: optional CSI_ETH_MAC_VLAN_FILTER_ID_ONLY; 0=disabled (default)
  34. /*----- Ethernet MAC Configuration -----*/
  35. #define CSI_ETH_MAC_SPEED_Pos 0
  36. #define CSI_ETH_MAC_SPEED_Msk (3UL << CSI_ETH_MAC_SPEED_Pos)
  37. #define CSI_ETH_MAC_SPEED_10M (CSI_ETH_SPEED_10M << CSI_ETH_MAC_SPEED_Pos) ///< 10 Mbps link speed
  38. #define CSI_ETH_MAC_SPEED_100M (CSI_ETH_SPEED_100M << CSI_ETH_MAC_SPEED_Pos) ///< 100 Mbps link speed
  39. #define CSI_ETH_MAC_SPEED_1G (CSI_ETH_SPEED_1G << CSI_ETH_MAC_SPEED_Pos) ///< 1 Gpbs link speed
  40. #define CSI_ETH_MAC_DUPLEX_Pos 2
  41. #define CSI_ETH_MAC_DUPLEX_Msk (1UL << CSI_ETH_MAC_DUPLEX_Pos)
  42. #define CSI_ETH_MAC_DUPLEX_HALF (CSI_ETH_DUPLEX_HALF << CSI_ETH_MAC_DUPLEX_Pos) ///< Half duplex link
  43. #define CSI_ETH_MAC_DUPLEX_FULL (CSI_ETH_DUPLEX_FULL << CSI_ETH_MAC_DUPLEX_Pos) ///< Full duplex link
  44. #define CSI_ETH_MAC_LOOPBACK (1UL << 4) ///< Loop-back test mode
  45. #define CSI_ETH_MAC_CHECKSUM_OFFLOAD_RX (1UL << 5) ///< Receiver Checksum offload
  46. #define CSI_ETH_MAC_CHECKSUM_OFFLOAD_TX (1UL << 6) ///< Transmitter Checksum offload
  47. #define CSI_ETH_MAC_ADDRESS_BROADCAST (1UL << 7) ///< Accept frames with Broadcast address
  48. #define CSI_ETH_MAC_ADDRESS_MULTICAST (1UL << 8) ///< Accept frames with any Multicast address
  49. #define CSI_ETH_MAC_ADDRESS_ALL (1UL << 9) ///< Accept frames with any address (Promiscuous Mode)
  50. /*----- Ethernet MAC Flush Flags -----*/
  51. #define CSI_ETH_MAC_FLUSH_RX (1UL << 0) ///< Flush Receive buffer
  52. #define CSI_ETH_MAC_FLUSH_TX (1UL << 1) ///< Flush Transmit buffer
  53. /*----- Ethernet MAC VLAN Filter Flag -----*/
  54. #define CSI_ETH_MAC_VLAN_FILTER_ID_ONLY (1UL << 16) ///< Compare only the VLAN Identifier (12-bit)
  55. /****** Ethernet MAC Frame Transmit Flags *****/
  56. #define CSI_ETH_MAC_TX_FRAME_FRAGMENT (1UL << 0) ///< Indicate frame fragment
  57. #define CSI_ETH_MAC_TX_FRAME_EVENT (1UL << 1) ///< Generate event when frame is transmitted
  58. #define CSI_ETH_MAC_TX_FRAME_TIMESTAMP (1UL << 2) ///< Capture frame time stamp
  59. /****** Ethernet MAC Timer Control Codes *****/
  60. #define CSI_ETH_MAC_TIMER_GET_TIME (0x01) ///< Get current time
  61. #define CSI_ETH_MAC_TIMER_SET_TIME (0x02) ///< Set new time
  62. #define CSI_ETH_MAC_TIMER_INC_TIME (0x03) ///< Increment current time
  63. #define CSI_ETH_MAC_TIMER_DEC_TIME (0x04) ///< Decrement current time
  64. #define CSI_ETH_MAC_TIMER_SET_ALCSI (0x05) ///< Set alarm time
  65. #define CSI_ETH_MAC_TIMER_ADJUST_CLOCK (0x06) ///< Adjust clock frequency; time->ns: correction factor * 2^31
  66. /**
  67. \brief Ethernet MAC Time
  68. */
  69. typedef struct eth_mac_time {
  70. uint32_t ns; ///< Nano seconds
  71. uint32_t sec; ///< Seconds
  72. } eth_mac_time_t;
  73. /****** Ethernet MAC Event *****/
  74. #define CSI_ETH_MAC_EVENT_RX_FRAME (1UL << 0) ///< Frame Received
  75. #define CSI_ETH_MAC_EVENT_TX_FRAME (1UL << 1) ///< Frame Transmitted
  76. #define CSI_ETH_MAC_EVENT_WAKEUP (1UL << 2) ///< Wake-up (on Magic Packet)
  77. #define CSI_ETH_MAC_EVENT_TIMER_ALCSI (1UL << 3) ///< Timer Alarm
  78. #define CSI_ETH_MAC_EVENT_LINK_CHANGE (1UL << 4) ///< Link state
  79. typedef void (*eth_event_cb_t)(eth_mac_handle_t handle, uint32_t event); ///< Pointer to \ref eth_event_cb_t : Signal Ethernet Event.
  80. typedef enum
  81. {
  82. FRAME_FILTER_RULE_POSITIVE_MATCHING = 0, /*!< Specifies that a filter should match a given pattern */
  83. FRAME_FILTER_RULE_NEGATIVE_MATCHING = 1, /*!< Specifies that a filter should NOT match a given pattern */
  84. } frame_filter_rule_t;
  85. /**
  86. \brief Ethernet MAC Capabilities
  87. */
  88. typedef struct eth_capabilities {
  89. uint32_t checksum_offload_rx_ip4 : 1; ///< 1 = IPv4 header checksum verified on receive
  90. uint32_t checksum_offload_rx_ip6 : 1; ///< 1 = IPv6 checksum verification supported on receive
  91. uint32_t checksum_offload_rx_udp : 1; ///< 1 = UDP payload checksum verified on receive
  92. uint32_t checksum_offload_rx_tcp : 1; ///< 1 = TCP payload checksum verified on receive
  93. uint32_t checksum_offload_rx_icmp : 1; ///< 1 = ICMP payload checksum verified on receive
  94. uint32_t checksum_offload_tx_ip4 : 1; ///< 1 = IPv4 header checksum generated on transmit
  95. uint32_t checksum_offload_tx_ip6 : 1; ///< 1 = IPv6 checksum generation supported on transmit
  96. uint32_t checksum_offload_tx_udp : 1; ///< 1 = UDP payload checksum generated on transmit
  97. uint32_t checksum_offload_tx_tcp : 1; ///< 1 = TCP payload checksum generated on transmit
  98. uint32_t checksum_offload_tx_icmp : 1; ///< 1 = ICMP payload checksum generated on transmit
  99. uint32_t media_interface : 2; ///< Ethernet Media Interface type
  100. uint32_t mac_address : 1; ///< 1 = driver provides initial valid MAC address
  101. uint32_t event_rx_frame : 1; ///< 1 = callback event generated
  102. uint32_t event_tx_frame : 1; ///< 1 = callback event generated
  103. uint32_t event_wakeup : 1; ///< 1 = wakeup event generated
  104. uint32_t precision_timer : 1; ///< 1 = Precision Timer supported
  105. uint32_t reserved : 15; ///< Reserved (must be zero)
  106. } eth_capabilities_t;
  107. #if 0
  108. /**
  109. \brief Ethernet Frame filter
  110. */
  111. typedef struct eth_frame_filter {
  112. struct {
  113. uint32_t and_or : 1; ///< 1 = AND: Packets will be rejected unless all enabled filters accept the packet; 0 = OR: Packets will be accepted unless all enabled filters reject the packet
  114. uint32_t unicast_en : 1; ///< 1 = Packets with a destination address matching the local MAC address will be accepted
  115. uint32_t multicast_en : 1; ///< 1 = Packets which have the Least Significant bit set in the destination address will be accepted
  116. uint32_t broadcast_en : 1; ///< 1 = Packets which have a destination address of FF-FF-FF-FF-FF-FF will be accepted
  117. uint32_t crc_en : 1; ///< 1 = All packets with an invalid CRC will be discarded
  118. uint32_t patten_match_en : 1; ///< 1 = Packets which meet the Pattern Match criteria will be accepted
  119. uint32_t magic_packet_en : 1; ///< 1 = Magic Packets for the local MAC address will be accepted
  120. uint32_t hash_table_en : 1; ///< 1 = Packets which meet the Hash Table criteria will be accepted
  121. } sum; ///< summary
  122. uint32_t patten_match; ///< patten match filter
  123. uint32_t magic_packet; ///< patten match filter
  124. uint32_t hash_table; ///< hash table filter
  125. } eth_frame_filter_t;
  126. #else
  127. /**
  128. * Structure describing a frame filter list item
  129. */
  130. typedef struct
  131. {
  132. uint32_t id; /*!< Unique identifier for a packet filter item */
  133. frame_filter_rule_t rule; /*!< Filter matches are either POSITIVE or NEGATIVE matching */
  134. uint16_t offset; /*!< Offset in bytes to start filtering (referenced to the start of the ethernet packet) */
  135. uint16_t mask_size; /*!< Size of the mask in bytes */
  136. uint8_t* mask; /*!< Pattern mask bytes to be ANDed with the pattern eg. "\xff00" (must be in network byte order) */
  137. uint8_t* pattern; /*!< Pattern bytes used to filter eg. "\x0800" (must be in network byte order) */
  138. bool enabled_status; /*!< When returned from mhd_get_packet_filters, indicates if the filter is enabled */
  139. } eth_frame_filter_t;
  140. struct eth_frame_filter_list
  141. {
  142. struct eth_frame_filter_list* next;
  143. };
  144. typedef struct eth_frame_filter_list eth_frame_filter_list_t;
  145. #endif
  146. typedef struct {
  147. eth_event_cb_t cb_event;
  148. eth_capabilities_t capabilities;
  149. }eth_mac_priv_t;
  150. /**
  151. \brief Get driver version.
  152. \param[in] handle ethernet handle
  153. \return ethernet version including chip version and driver version
  154. */
  155. csi_drv_version_t csi_eth_mac_get_version(eth_mac_handle_t handle);
  156. /**
  157. \brief Get driver capabilities.
  158. \param[in] handle ethernet handle
  159. \return ethernet capabilities
  160. */
  161. eth_capabilities_t csi_eth_mac_get_capabilities(eth_mac_handle_t handle);
  162. /**
  163. \brief This function is used to initialize Ethernet device and related resource, an event callback is registered. It is called when the middleware component like TCPIP starts operation.
  164. \param[in] cb callback to handle ethernet event
  165. \return return ethernet handle if success
  166. */
  167. eth_mac_handle_t csi_eth_mac_initialize(eth_event_cb_t cb);
  168. /**
  169. \brief This function is used to de-initialize Ethernet device. It is called when the middleware component stops operation and releases the software resources used by the interface.
  170. \param[in] handle ethernet handle
  171. \return error code
  172. */
  173. int32_t csi_eth_mac_uninitialize(eth_mac_handle_t handle);
  174. /**
  175. \brief Control Ethernet MAC Device Power.
  176. \param[in] handle ethernet handle
  177. \param[in] state Power state
  178. \return error code
  179. */
  180. int32_t csi_eth_mac_power_control(eth_mac_handle_t handle, eth_power_state_t state);
  181. /**
  182. \brief Get Ethernet MAC Address.
  183. \param[in] handle ethernet handle
  184. \param[in] mac Pointer to address
  185. \return error code
  186. */
  187. int32_t csi_eth_mac_get_macaddr(eth_mac_handle_t handle, eth_mac_addr_t *mac);
  188. /**
  189. \brief Set Ethernet MAC Address.
  190. \param[in] handle ethernet handle
  191. \param[in] mac Pointer to address
  192. \return error code
  193. */
  194. int32_t csi_eth_mac_set_macaddr(eth_mac_handle_t handle, const eth_mac_addr_t *mac);
  195. /**
  196. \brief Configure Address Filter.
  197. \param[in] handle ethernet handle
  198. \param[in] addr Pointer to addresses
  199. \param[in] num_addr Number of addresses to configure
  200. \return error code
  201. */
  202. int32_t csi_eth_mac_set_addrfilter(eth_mac_handle_t handle, const eth_mac_addr_t *addr, uint32_t num_addr);
  203. /**
  204. \brief Send Ethernet frame.
  205. \param[in] handle ethernet handle
  206. \param[in] frame Pointer to frame buffer with data to send
  207. \param[in] len Frame buffer length in bytes
  208. \param[in] flags Frame transmit flags (see CSI_ETH_MAC_TX_FRAME_...)
  209. \return error code
  210. */
  211. int32_t csi_eth_mac_send_frame(eth_mac_handle_t handle, const uint8_t *frame, uint32_t len, uint32_t flags);
  212. /**
  213. \brief Read data of received Ethernet frame.
  214. \param[in] handle ethernet handle
  215. \param[in] frame Pointer to frame buffer for data to read into
  216. \param[in] len Frame buffer length in bytes
  217. \return number of data bytes read or execution status
  218. - value >= 0: number of data bytes read
  219. - value < 0: error occurred, value is execution status as defined with execution_status
  220. */
  221. int32_t csi_eth_mac_read_frame(eth_mac_handle_t handle, uint8_t *frame, uint32_t len);
  222. /**
  223. \brief Get size of received Ethernet frame.
  224. \param[in] handle ethernet handle
  225. \return number of bytes in received frame
  226. */
  227. int32_t csi_eth_mac_get_rx_framesize(eth_mac_handle_t handle);
  228. /**
  229. \brief Get time of received Ethernet frame.
  230. \param[in] handle ethernet handle
  231. \param[in] time Pointer to time structure for data to read into
  232. \return error code
  233. */
  234. int32_t csi_eth_mac_get_rx_frametime(eth_mac_handle_t handle, eth_mac_time_t *time);
  235. /**
  236. \brief Get time of transmitted Ethernet frame.
  237. \param[in] handle ethernet handle
  238. \param[in] time Pointer to time structure for data to read into
  239. \return error code
  240. */
  241. int32_t csi_eth_mac_get_tx_frametime(eth_mac_handle_t handle, eth_mac_time_t *time);
  242. /**
  243. \brief Control Ethernet Interface.
  244. \param[in] handle ethernet handle
  245. \param[in] control Operation
  246. \param[in] arg Argument of operation (optional)
  247. \return error code
  248. */
  249. int32_t csi_eth_mac_control(eth_mac_handle_t handle, uint32_t control, uint32_t arg);
  250. /**
  251. \brief Control Precision Timer.
  252. \param[in] handle ethernet handle
  253. \param[in] control Operation
  254. \param[in] time Pointer to time structure
  255. \return error code
  256. */
  257. int32_t csi_eth_mac_control_time(eth_mac_handle_t handle, uint32_t control, eth_mac_time_t *time);
  258. /**
  259. \brief Read Ethernet PHY Register through Management Interface.
  260. \param[in] handle ethernet handle
  261. \param[in] phy_addr 5-bit device address
  262. \param[in] reg_addr 5-bit register address
  263. \param[out] data Pointer where the result is written to
  264. \return error code
  265. */
  266. int32_t csi_eth_mac_phy_read(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data);
  267. /**
  268. \brief Write Ethernet PHY Register through Management Interface.
  269. \param[in] handle ethernet handle
  270. \param[in] phy_addr 5-bit device address
  271. \param[in] reg_addr 5-bit register address
  272. \param[in] data 16-bit data to write
  273. \return error code
  274. */
  275. int32_t csi_eth_mac_phy_write(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t data);
  276. /**
  277. \brief Callback function that signals a Ethernet Event.
  278. \param[in] handle ethernet handle
  279. \param[in] event event notification mask
  280. \return none
  281. */
  282. void csi_eth_mac_signal_event(eth_mac_handle_t handle, uint32_t event);
  283. /**
  284. \brief Add Frame Filter Setting with Filter ID.
  285. \param[in] handle ethernet handle
  286. \param[in] filter Pointer to filter setting
  287. \return error code
  288. */
  289. int32_t csi_eth_mac_add_framefilter(eth_mac_handle_t handle, const eth_frame_filter_t *filter);
  290. /**
  291. \brief Remove Frame Filter Setting.
  292. \param[in] handle ethernet handle
  293. \param[in] filter_id Frame Filter ID
  294. \return error code
  295. */
  296. int32_t csi_eth_mac_remove_framefilter(eth_mac_handle_t handle, uint32_t filter_id);
  297. /**
  298. \brief Enable/Disable Specified Frame Filter ID.
  299. \param[in] handle ethernet handle
  300. \param[in] filter_id Frame Filter ID
  301. \param[in] en Enable or disable
  302. \return error code
  303. */
  304. int32_t csi_eth_mac_en_framefilter(eth_mac_handle_t handle, uint32_t filter_id, bool en);
  305. /**
  306. \brief Get frame filter table list.
  307. \param[in] handle ethernet handle
  308. \param[in] list frame filter table list
  309. \param[in] count_out the count of filter setting added
  310. \param[in] max_count max filter setting can be supported
  311. \return error code
  312. */
  313. int32_t csi_eth_mac_get_framefilter(eth_mac_handle_t handle, eth_frame_filter_list_t* list, uint32_t* count_out, uint32_t max_count);
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif