hf_protocol.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // Copyright 2013 HashFast LLC
  3. //
  4. // This program is free software; you can redistribute it and/or modify it
  5. // under the terms of the GNU General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option)
  7. // any later version. See COPYING for more details.
  8. //
  9. // Useful data structures and values for interfacing with HashFast products
  10. //
  11. #ifndef _HF_PROTOCOL_H_
  12. #define _HF_PROTOCOL_H_
  13. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  14. #error "This header uses bit fields and has byte ordering assumptions suitable only for Little Endian platforms"
  15. #endif
  16. #define HF_PREAMBLE (uint8_t) 0xaa
  17. #define HF_BROADCAST_ADDRESS (uint8_t) 0xff
  18. #define HF_GWQ_ADDRESS (uint8_t) 254
  19. // Serial protocol operation codes (Second header byte)
  20. #define OP_NULL 0
  21. #define OP_ROOT 1
  22. #define OP_RESET 2
  23. #define OP_PLL_CONFIG 3
  24. #define OP_ADDRESS 4
  25. #define OP_READDRESS 5
  26. #define OP_HIGHEST 6
  27. #define OP_BAUD 7
  28. #define OP_UNROOT 8
  29. #define OP_HASH 9
  30. #define OP_NONCE 10
  31. #define OP_ABORT 11
  32. #define OP_STATUS 12
  33. #define OP_GPIO 13
  34. #define OP_CONFIG 14
  35. #define OP_STATISTICS 15
  36. #define OP_GROUP 16
  37. #define OP_CLOCKGATE 17
  38. // Generic header
  39. struct hf_header {
  40. uint8_t preamble; // Always 0xaa
  41. uint8_t operation_code;
  42. uint8_t chip_address;
  43. uint8_t core_address;
  44. uint16_t hdata; // Header specific data
  45. uint8_t data_length; // .. of data frame to follow, in 4 byte blocks, 0=no data
  46. uint8_t crc8; // Computed across bytes 1-6 inclusive
  47. } __attribute__((packed,aligned(4))); // 8 bytes total
  48. // Header specific to OP_PLL_CONFIG
  49. struct hf_pll_config {
  50. uint8_t preamble;
  51. uint8_t operation_code;
  52. uint8_t chip_address;
  53. uint8_t pll_divr:6;
  54. uint8_t pll_bypass:1;
  55. uint8_t pll_reset:1;
  56. uint16_t pll_divf:8;
  57. uint16_t spare1:1; // Must always be 0
  58. uint16_t pll_divq:3;
  59. uint16_t pll_range:3;
  60. uint16_t pll_fse:1; // Must always be 1
  61. uint8_t data_length; // Always 0
  62. uint8_t crc8; // Computed across bytes 1-6 inclusive
  63. } __attribute__((packed,aligned(4))); // 8 bytes total
  64. // OP_HASH serial data
  65. struct hf_hash_serial {
  66. uint8_t midstate[32]; // Computed from first half of block header
  67. uint8_t merkle_residual[4]; // From block header
  68. uint32_t timestamp; // From block header
  69. uint32_t bits; // Actual difficulty target for block header
  70. uint32_t starting_nonce; // Usually set to 0
  71. uint32_t nonce_loops; // How many nonces to search, or 0 for 2^32
  72. uint16_t ntime_loops:12; // How many times to roll timestamp, or 0
  73. uint16_t spare1:4;
  74. uint8_t search_difficulty; // Search difficulty to use, # of '0' digits required
  75. uint8_t option;
  76. uint32_t group:8;
  77. uint32_t spare3:24;
  78. uint32_t crc32; // Computed across all preceding data fields
  79. } __attribute__((packed,aligned(4))); // 64 bytes total, including CRC
  80. // OP_HASH usb data - squashed so header+data = 64 bytes
  81. struct hf_hash_usb {
  82. uint8_t midstate[32]; // Computed from first half of block header
  83. uint8_t merkle_residual[4]; // From block header
  84. uint32_t timestamp; // From block header
  85. uint32_t bits; // Actual difficulty target for block header
  86. uint32_t starting_nonce; // Usually set to 0
  87. uint32_t nonce_loops; // How many nonces to search, or 0 for 2^32
  88. uint16_t ntime_loops:12; // How many times to roll timestamp, or 0
  89. uint16_t spare1:4;
  90. uint8_t search_difficulty; // Search difficulty to use, # of '0' digits required
  91. uint8_t group; // Non-zero for valid group
  92. } __attribute__((packed,aligned(4)));
  93. // OP_NONCE data
  94. struct hf_candidate_nonce {
  95. uint32_t nonce; // Candidate nonce
  96. uint16_t sequence; // Sequence number from corresponding OP_HASH
  97. uint16_t ntime:12; // ntime offset, if ntime roll occurred
  98. uint16_t search:1; // Search forward next 128 nonces to find solution
  99. uint16_t spare:3;
  100. } __attribute__((packed,aligned(4)));
  101. // OP_CONFIG data
  102. struct hf_config_data {
  103. uint16_t status_period:11; // Periodic status time, msec
  104. uint16_t enable_periodic_status:1; // Send periodic status
  105. uint16_t send_status_on_core_idle:1; // Schedule status whenever core goes idle
  106. uint16_t send_status_on_pending_empty:1; // Schedule status whenever core pending goes idle
  107. uint16_t pwm_active_level:1; // Active level of PWM outputs, if used
  108. uint16_t forward_all_privileged_packets:1; // Forward priv pkts -- diagnostic
  109. uint8_t status_batch_delay; // Batching delay, time to wait before sending status
  110. uint8_t watchdog:7; // Watchdog timeout, seconds
  111. uint8_t disable_sensors:1; // Diagnostic
  112. uint8_t rx_header_timeout:7; // Header timeout in char times
  113. uint8_t rx_ignore_header_crc:1; // Ignore rx header crc's (diagnostic)
  114. uint8_t rx_data_timeout:7; // Data timeout in char times / 16
  115. uint8_t rx_ignore_data_crc:1; // Ignore rx data crc's (diagnostic)
  116. uint8_t stats_interval:7; // Minimum interval to report statistics (seconds)
  117. uint8_t stat_diagnostic:1; // Never set this
  118. uint8_t measure_interval; // Die temperature measurement interval (msec)
  119. uint32_t one_usec:12; // How many LF clocks per usec.
  120. uint32_t max_nonces_per_frame:4; // Maximum # of nonces to combine in a single frame
  121. uint32_t voltage_sample_points:8; // Bit mask for sample points (up to 5 bits set)
  122. uint32_t pwm_phases:2; // phases - 1
  123. uint32_t trim:4; // Trim value for temperature measurements
  124. uint32_t clock_diagnostic:1; // Never set this
  125. uint32_t forward_all_packets:1; // Forward everything - diagnostic.
  126. uint16_t pwm_period; // Period of PWM outputs, in reference clock cycles
  127. uint16_t pwm_pulse_period; // Initial count, phase 0
  128. } __attribute__((packed,aligned(4)));
  129. // OP_GROUP data
  130. struct hf_group_data {
  131. uint16_t nonce_msoffset; // This value << 16 added to starting nonce
  132. uint16_t ntime_offset:12; // This value added to timestamp
  133. uint16_t spare:4;
  134. } __attribute__((packed,aligned(4)));
  135. // Structure of the monitor fields for G-1, returned in OP_STATUS, core bitmap follows this
  136. struct hf_g1_monitor {
  137. uint16_t die_temperature:12; // Die temperature ADC count
  138. uint16_t spare:4; // Spare
  139. uint8_t core_voltage[6]; // Core voltage
  140. // [0] = main sensor
  141. // [1]-[5] = other positions
  142. } __attribute__((packed,aligned(4)));
  143. // Conversions for the ADC readings from GN on-chip sensors in the above structure
  144. #define GN_CORE_VOLTAGE(a) ((float)(a)/256*1.2)
  145. #define GN_DIE_TEMPERATURE(a) ((((float)(a)*240)/4096.0)-61.5)
  146. // What comes back in the body of an OP_STATISTICS frame (On die statistics)
  147. struct hf_statistics {
  148. uint8_t rx_header_crc; // Header CRC error's
  149. uint8_t rx_body_crc; // Data CRC error's
  150. uint8_t rx_header_timeouts; // Header timeouts
  151. uint8_t rx_body_timeouts; // Data timeouts
  152. uint8_t core_nonce_fifo_full; // Core nonce Q overrun events
  153. uint8_t array_nonce_fifo_full; // System nonce Q overrun events
  154. uint8_t stats_overrun; // Overrun in statistics reporting
  155. uint8_t spare;
  156. } __attribute__((packed,aligned(4)));
  157. // The sequence distance between a sent and received sequence number.
  158. #define SEQUENCE_DISTANCE(tx,rx) ((tx)>=(rx)?((tx)-(rx)):(info->num_sequence+(tx)-(rx)))
  159. ////////////////////////////////////////////////////////////////////////////////
  160. // USB protocol data structures
  161. ////////////////////////////////////////////////////////////////////////////////
  162. // Convenience header specific to OP_USB_INIT
  163. struct hf_usb_init_header {
  164. uint8_t preamble; // Always 0xaa
  165. uint8_t operation_code;
  166. uint8_t spare1;
  167. uint8_t protocol:3; // Which protocol to use
  168. uint8_t user_configuration:1; // Use the following configuration data
  169. uint8_t spare2:4;
  170. uint16_t hash_clock:12; // Requested hash clock frequency
  171. uint16_t pll_bypass:1; // Force PLL bypass, hash clock = ref clock
  172. uint16_t no_asic_initialization:1; // Do not perform automatic ASIC initialization
  173. uint16_t do_atspeed_core_tests:1; // Do core tests at speed, return second bitmap
  174. uint16_t leave_powered_down:1; // Init USB only, leave device powered down
  175. uint8_t data_length; // .. of data frame to follow, in 4 byte blocks
  176. uint8_t crc8; // Computed across bytes 1-6 inclusive
  177. } __attribute__((packed,aligned(4))); // 8 bytes total
  178. // Values the protocol field in the above structure may take
  179. #define PROTOCOL_USB_MAPPED_SERIAL 0
  180. #define PROTOCOL_GLOBAL_WORK_QUEUE 1
  181. // Options (only if present) that may be appended to the above header
  182. // Each option involving a numerical value will only be in effect if the value is non-zero
  183. // This allows the user to select only those options desired for modification. Do not
  184. // use this facility unless you are an expert - loading inconsistent settings will not work.
  185. struct hf_usb_init_options {
  186. uint16_t group_ntime_roll; // Total ntime roll amount per group
  187. uint16_t core_ntime_roll; // Total core ntime roll amount
  188. uint8_t low_operating_temp_limit; // Lowest normal operating limit
  189. uint8_t high_operating_temp_limit; // Highest normal operating limit
  190. uint16_t spare;
  191. } __attribute__((packed,aligned(4)));
  192. // Base item returned from device for OP_USB_INIT
  193. struct hf_usb_init_base {
  194. uint16_t firmware_rev; // Firmware revision #
  195. uint16_t hardware_rev; // Hardware revision #
  196. uint32_t serial_number; // Board serial number
  197. uint8_t operation_status; // Reply status for OP_USB_INIT (0 = success)
  198. uint8_t extra_status_1; // Extra reply status information, code specific
  199. uint16_t sequence_modulus; // Sequence numbers are to be modulo this
  200. uint16_t hash_clockrate; // Actual hash clock rate used (nearest Mhz)
  201. uint16_t inflight_target; // Target inflight amount for GWQ protocol
  202. } __attribute__((packed,aligned(4)));
  203. // The above base item (16 bytes) is followed by the struct hf_config_data (16 bytes) actually
  204. // used internally (so users may modify non-critical fields by doing subsequent
  205. // OP_CONFIG operations). This is followed by a device specific "core good" bitmap (unless the
  206. // user disabled initialization), and optionally by an at-speed "core good" bitmap.
  207. // Information in an OP_DIE_STATUS frame. This is for one die - there are four per ASIC.
  208. // Board level phase current and voltage sensors are likely to disappear in later production models.
  209. struct hf_g1_die_data {
  210. struct hf_g1_monitor die; // Die sensors - 8 bytes
  211. uint16_t phase_currents[4]; // Phase currents (0 if unavailable)
  212. uint16_t voltage; // Voltage at device boundary (0 if unavailable)
  213. uint16_t temperature; // Regulator temp sensor
  214. uint16_t tacho; // See documentation
  215. uint16_t spare;
  216. } __attribute__((packed,aligned(4))); // 24 bytes total
  217. // Conversions for the board/module level sensors
  218. #define M_VOLTAGE(a) ((float)(a)*19.0734e-6)
  219. #define M_PHASE_CURRENT(a) ((float)(a)*0.794728597e-3)
  220. // Information for an OP_GWQ_STATUS frame
  221. // If sequence_head == sequence_tail, then there is no active work and sequence_head is invalid
  222. struct hf_gwq_data {
  223. uint64_t hash_count; // Add this to host's cumulative hash count
  224. uint16_t sequence_head; // The latest, internal, active sequence #
  225. uint16_t sequence_tail; // The latest, internal, inactive sequence #
  226. uint16_t shed_count; // # of cores have been shedded for thermal control
  227. uint16_t spare;
  228. } __attribute__((packed,aligned(4)));
  229. // Information for an OP_USB_STATS1 frame - Communication statistics
  230. struct hf_usb_stats1 {
  231. // USB incoming
  232. uint16_t usb_rx_preambles;
  233. uint16_t usb_rx_receive_byte_errors;
  234. uint16_t usb_rx_bad_hcrc;
  235. // USB outgoing
  236. uint16_t usb_tx_attempts;
  237. uint16_t usb_tx_packets;
  238. uint16_t usb_tx_timeouts;
  239. uint16_t usb_tx_incompletes;
  240. uint16_t usb_tx_endpointstalled;
  241. uint16_t usb_tx_disconnected;
  242. uint16_t usb_tx_suspended;
  243. // Internal UART transmit
  244. uint16_t uart_tx_queue_dma;
  245. uint16_t uart_tx_interrupts;
  246. // Internal UART receive
  247. uint16_t uart_rx_preamble_ints;
  248. uint16_t uart_rx_missed_preamble_ints;
  249. uint16_t uart_rx_header_done;
  250. uint16_t uart_rx_data_done;
  251. uint16_t uart_rx_bad_hcrc;
  252. uint16_t uart_rx_bad_dma;
  253. uint16_t uart_rx_short_dma;
  254. uint16_t uart_rx_buffers_full;
  255. uint8_t max_tx_buffers; // Maximum # of send buffers ever used
  256. uint8_t max_rx_buffers; // Maximum # of receive buffers ever used
  257. } __attribute__((packed,aligned(4)));
  258. // Values info->device_type can take
  259. #define HFD_G1 1 // HashFast G-1 GN ASIC
  260. #define HFD_VC709 128
  261. #define HFD_ExpressAGX 129
  262. // USB interface specific operation codes
  263. #define OP_USB_INIT 128 // Initialize USB interface details
  264. #define OP_GET_TRACE 129 // Send back the trace buffer if present
  265. #define OP_LOOPBACK_USB 130
  266. #define OP_LOOPBACK_UART 131
  267. #define OP_DFU 132 // Jump into the boot loader
  268. #define OP_USB_SHUTDOWN 133 // Initialize USB interface details
  269. #define OP_DIE_STATUS 134 // Die status. There are 4 die per ASIC
  270. #define OP_GWQ_STATUS 135 // Global Work Queue protocol status
  271. #define OP_WORK_RESTART 136 // Stratum work restart regime
  272. #define OP_USB_STATS1 137
  273. #define OP_USB_GWQSTATS 138
  274. #define OP_USB_DEBUG 255
  275. // HashFast vendor and product ID's
  276. #define HF_USB_VENDOR_ID 0x297c
  277. #define HF_USB_PRODUCT_ID_G1 0x0001
  278. //
  279. // Fault codes that can be returned in struct hf_usb_init_base.operation_status
  280. //
  281. #define E_RESET_TIMEOUT 1
  282. #define E_ADDRESS_TIMEOUT 2
  283. #define E_CLOCKGATE_TIMEOUT 3
  284. #define E_CONFIG_TIMEOUT 4
  285. #define U32SIZE(x) (sizeof(x)/sizeof(uint32_t))
  286. #endif