driver-bitmain.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2013 BitMain project
  3. * Copyright 2013 BitMain <xlc1985@126.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #ifndef BITMAIN_H
  11. #define BITMAIN_H
  12. #ifdef USE_BITMAIN
  13. #include <curl/curl.h>
  14. #include "util.h"
  15. //#define BITMAIN_TYPE_S1
  16. //#define BITMAIN_TYPE_S2
  17. //#define BITMAIN_TYPE_S3
  18. #define BITMAIN_TYPE_S4
  19. #define BITMAIN_MINER_THREADS 1
  20. #define BITMAIN_RESET_PITCH (300*1000*1000)
  21. #define BITMAIN_TOKEN_TYPE_TXCONFIG 0x51
  22. #define BITMAIN_TOKEN_TYPE_TXTASK 0x52
  23. #define BITMAIN_TOKEN_TYPE_RXSTATUS 0x53
  24. #define BITMAIN_DATA_TYPE_RXSTATUS 0xa1
  25. #define BITMAIN_DATA_TYPE_RXNONCE 0xa2
  26. #define BITMAIN_FAN_FACTOR 60
  27. #define BITMAIN_PWM_MAX 0xA0
  28. #define BITMAIN_DEFAULT_FAN_MIN 20
  29. #define BITMAIN_DEFAULT_FAN_MAX 100
  30. #define BITMAIN_DEFAULT_FAN_MAX_PWM 0xA0 /* 100% */
  31. #define BITMAIN_DEFAULT_FAN_MIN_PWM 0x20 /* 20% */
  32. #define BITMAIN_DEFAULT_TIMEOUT 0x2D
  33. #define BITMAIN_MIN_FREQUENCY 10
  34. #define BITMAIN_MAX_FREQUENCY 1000000
  35. #define BITMAIN_TIMEOUT_FACTOR 12690
  36. #define BITMAIN_DEFAULT_FREQUENCY 282
  37. #define BITMAIN_DEFAULT_VOLTAGE_T "0725"
  38. #define BITMAIN_DEFAULT_VOLTAGE0 0x07
  39. #define BITMAIN_DEFAULT_VOLTAGE1 0x25
  40. #define BITMAIN_DEFAULT_CHAIN_NUM 8
  41. #define BITMAIN_DEFAULT_ASIC_NUM 32
  42. #define BITMAIN_DEFAULT_REG_DATA 0
  43. #define BITMAIN_AUTO_CYCLE 1024
  44. #define BITMAIN_FTDI_READSIZE 2048
  45. #define BITMAIN_USB_PACKETSIZE 512
  46. #define BITMAIN_SENDBUF_SIZE 8192
  47. #define BITMAIN_READBUF_SIZE 8192
  48. #define BITMAIN_RESET_TIMEOUT 100
  49. #define BITMAIN_READ_TIMEOUT 18 /* Enough to only half fill the buffer */
  50. #define BITMAIN_LATENCY 1
  51. #ifdef BITMAIN_TYPE_S1
  52. #define BITMAIN_MAX_DEAL_QUEUE_NUM 1
  53. #define BITMAIN_MAX_CHAIN_NUM 8
  54. #define BITMAIN_MAX_TEMP_NUM 32
  55. #define BITMAIN_MAX_FAN_NUM 32
  56. #define BITMAIN_ARRAY_SIZE 16384
  57. #define BITMAIN_SEND_STATUS_TIME 10 //s
  58. #define BITMAIN_SEND_FULL_SPACE 128
  59. #endif
  60. #ifdef BITMAIN_TYPE_S2
  61. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  62. #define BITMAIN_MAX_CHAIN_NUM 16
  63. #define BITMAIN_MAX_TEMP_NUM 32
  64. #define BITMAIN_MAX_FAN_NUM 32
  65. #define BITMAIN_ARRAY_SIZE 16384
  66. #define BITMAIN_SEND_STATUS_TIME 15 //s
  67. #define BITMAIN_SEND_FULL_SPACE 512
  68. #endif
  69. #ifdef BITMAIN_TYPE_S3
  70. #define BITMAIN_MAX_DEAL_QUEUE_NUM 2
  71. #define BITMAIN_MAX_CHAIN_NUM 8
  72. #define BITMAIN_MAX_TEMP_NUM 32
  73. #define BITMAIN_MAX_FAN_NUM 32
  74. #define BITMAIN_ARRAY_SIZE 16384
  75. #define BITMAIN_SEND_STATUS_TIME 15 //s
  76. #define BITMAIN_SEND_FULL_SPACE 256
  77. #endif
  78. #ifdef BITMAIN_TYPE_S4
  79. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  80. #define BITMAIN_MAX_CHAIN_NUM 16
  81. #define BITMAIN_MAX_TEMP_NUM 32
  82. #define BITMAIN_MAX_FAN_NUM 32
  83. #define BITMAIN_ARRAY_SIZE 16384*2
  84. #define BITMAIN_SEND_STATUS_TIME 15 //s
  85. #define BITMAIN_SEND_FULL_SPACE 512
  86. #endif
  87. #define BITMAIN_TASK_HEADER_SIZE 8
  88. #define BITMAIN_TASK_FOOTER_SIZE 2
  89. #define BITMAIN_WORK_SIZE 0x30
  90. #define BITMAIN_MAX_PACKET_MAX_NONCE 0x80
  91. struct bitmain_packet_head {
  92. uint8_t token_type;
  93. uint8_t version;
  94. uint16_t length;
  95. } __attribute__((packed, aligned(4)));
  96. struct bitmain_txconfig_token {
  97. uint8_t token_type;
  98. uint8_t version;
  99. uint16_t length;
  100. uint8_t reset :1;
  101. uint8_t fan_eft :1;
  102. uint8_t timeout_eft :1;
  103. uint8_t frequency_eft :1;
  104. uint8_t voltage_eft :1;
  105. uint8_t chain_check_time_eft :1;
  106. uint8_t chip_config_eft :1;
  107. uint8_t hw_error_eft :1;
  108. uint8_t beeper_ctrl :1;
  109. uint8_t temp_over_ctrl :1;
  110. uint8_t fan_home_mode :1;
  111. uint8_t reserved1 :5;
  112. uint8_t chain_check_time;
  113. uint8_t reserved2;
  114. uint8_t chain_num;
  115. uint8_t asic_num;
  116. uint8_t fan_pwm_data;
  117. uint8_t timeout_data;
  118. uint16_t frequency;
  119. uint8_t voltage[2];
  120. uint8_t reg_data[4];
  121. uint8_t chip_address;
  122. uint8_t reg_address;
  123. uint16_t crc;
  124. } __attribute__((packed, aligned(4)));
  125. struct bitmain_rxstatus_token {
  126. uint8_t token_type;
  127. uint8_t version;
  128. uint16_t length;
  129. uint8_t chip_status_eft :1;
  130. uint8_t detect_get :1;
  131. uint8_t reserved1 :6;
  132. uint8_t reserved2[3];
  133. uint8_t chip_address;
  134. uint8_t reg_address;
  135. uint16_t crc;
  136. } __attribute__((packed, aligned(4)));
  137. struct bitmain_rxstatus_data {
  138. uint8_t data_type;
  139. uint8_t version;
  140. uint16_t length;
  141. uint8_t chip_value_eft :1;
  142. uint8_t reserved1 :3;
  143. uint8_t get_blk_num :4;
  144. uint8_t chain_num;
  145. uint16_t fifo_space;
  146. uint8_t hw_version[4];
  147. uint8_t fan_num;
  148. uint8_t temp_num;
  149. uint16_t fan_exist;
  150. uint32_t temp_exist;
  151. uint32_t nonce_error;
  152. uint32_t reg_value;
  153. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  154. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  155. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  156. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  157. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  158. uint16_t crc;
  159. } __attribute__((packed, aligned(4)));
  160. struct bitmain_rxnonce_nonce {
  161. uint32_t work_id;
  162. uint32_t nonce;
  163. } __attribute__((packed, aligned(4)));
  164. struct bitmain_rxnonce_data {
  165. uint8_t data_type;
  166. uint8_t version;
  167. uint16_t length;
  168. uint16_t fifo_space;
  169. uint16_t diff;
  170. uint64_t total_nonce_num;
  171. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_PACKET_MAX_NONCE];
  172. uint16_t crc;
  173. } __attribute__((packed, aligned(4)));
  174. struct bitmain_info {
  175. void *device_curl;
  176. SOCKETTYPE curl_sock;
  177. int chain_num;
  178. int asic_num;
  179. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  180. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  181. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  182. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][320];
  183. int timeout;
  184. int errorcount;
  185. int errorcount2;
  186. uint32_t nonce_error;
  187. uint32_t last_nonce_error;
  188. uint8_t reg_data[4];
  189. unsigned packet_max_work; // BITMAIN_MAX_WORK_NUM
  190. unsigned poll_prio_threshold; // BITMAIN_MAX_WORK_QUEUE_NUM
  191. unsigned packet_max_nonce; // BITMAIN_MAX_NONCE_NUM
  192. int fan_num;
  193. int fan[BITMAIN_MAX_FAN_NUM];
  194. int temp_num;
  195. int temp[BITMAIN_MAX_TEMP_NUM];
  196. int temp_max;
  197. int temp_avg;
  198. int temp_history_count;
  199. int temp_history_index;
  200. int temp_sum;
  201. int temp_old;
  202. int fan_pwm;
  203. int frequency;
  204. char frequency_t[256];
  205. uint8_t voltage[2];
  206. char voltage_t[8];
  207. int diff;
  208. float lowest_goal_diff;
  209. uint32_t next_work_id;
  210. int no_matching_work;
  211. //int matching_work[BITMAIN_DEFAULT_CHAIN_NUM];
  212. pthread_mutex_t qlock;
  213. int fifo_space;
  214. int max_fifo_space;
  215. int hw_version[4];
  216. int ready_to_queue;
  217. uint8_t *queuebuf;
  218. bool reset;
  219. bool work_restart;
  220. char g_miner_version[256];
  221. uint8_t readbuf[BITMAIN_READBUF_SIZE];
  222. int readbuf_offset;
  223. };
  224. #define BITMAIN_READ_SIZE 12
  225. #define BTM_GETS_ERROR -1
  226. #define BTM_GETS_OK 0
  227. #define BTM_SEND_ERROR -1
  228. #define BTM_SEND_OK 0
  229. #define BITMAIN_READ_TIME(baud) ((double)BITMAIN_READ_SIZE * (double)8.0 / (double)(baud))
  230. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  231. #endif /* USE_BITMAIN */
  232. #endif /* BITMAIN_H */