driver-bitmain.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "util.h"
  14. //#define BITMAIN_TYPE_S1
  15. //#define BITMAIN_TYPE_S2
  16. //#define BITMAIN_TYPE_S3
  17. #define BITMAIN_TYPE_S4
  18. #define BITMAIN_RESET_FAULT_DECISECONDS 1
  19. #define BITMAIN_MINER_THREADS 1
  20. #define BITMAIN_IO_SPEED 115200
  21. #define BITMAIN_HASH_TIME_FACTOR ((float)1.67/0x32)
  22. #define BITMAIN_RESET_PITCH (300*1000*1000)
  23. #define BITMAIN_TOKEN_TYPE_TXCONFIG 0x51
  24. #define BITMAIN_TOKEN_TYPE_TXTASK 0x52
  25. #define BITMAIN_TOKEN_TYPE_RXSTATUS 0x53
  26. #define BITMAIN_DATA_TYPE_RXSTATUS 0xa1
  27. #define BITMAIN_DATA_TYPE_RXNONCE 0xa2
  28. #define BITMAIN_FAN_FACTOR 60
  29. #define BITMAIN_PWM_MAX 0xA0
  30. #define BITMAIN_DEFAULT_FAN_MIN 20
  31. #define BITMAIN_DEFAULT_FAN_MAX 100
  32. #define BITMAIN_DEFAULT_FAN_MAX_PWM 0xA0 /* 100% */
  33. #define BITMAIN_DEFAULT_FAN_MIN_PWM 0x20 /* 20% */
  34. #define BITMAIN_TEMP_TARGET 50
  35. #define BITMAIN_TEMP_HYSTERESIS 3
  36. #define BITMAIN_TEMP_OVERHEAT 60
  37. #define BITMAIN_DEFAULT_TIMEOUT 0x2D
  38. #define BITMAIN_MIN_FREQUENCY 10
  39. #define BITMAIN_MAX_FREQUENCY 1000000
  40. #define BITMAIN_TIMEOUT_FACTOR 12690
  41. #define BITMAIN_DEFAULT_FREQUENCY 282
  42. #define BITMAIN_DEFAULT_VOLTAGE_T "0725"
  43. #define BITMAIN_DEFAULT_VOLTAGE0 0x07
  44. #define BITMAIN_DEFAULT_VOLTAGE1 0x25
  45. #define BITMAIN_DEFAULT_CHAIN_NUM 8
  46. #define BITMAIN_DEFAULT_ASIC_NUM 32
  47. #define BITMAIN_DEFAULT_REG_DATA 0
  48. #define BITMAIN_AUTO_CYCLE 1024
  49. #define BITMAIN_FTDI_READSIZE 2048
  50. #define BITMAIN_USB_PACKETSIZE 512
  51. #define BITMAIN_SENDBUF_SIZE 8192
  52. #define BITMAIN_READBUF_SIZE 8192
  53. #define BITMAIN_RESET_TIMEOUT 100
  54. #define BITMAIN_READ_TIMEOUT 18 /* Enough to only half fill the buffer */
  55. #define BITMAIN_LATENCY 1
  56. #ifdef BITMAIN_TYPE_S1
  57. #define BITMAIN_MAX_WORK_NUM 8
  58. #define BITMAIN_MAX_WORK_QUEUE_NUM 64
  59. #define BITMAIN_MAX_DEAL_QUEUE_NUM 1
  60. #define BITMAIN_MAX_NONCE_NUM 8
  61. #define BITMAIN_MAX_CHAIN_NUM 8
  62. #define BITMAIN_MAX_TEMP_NUM 32
  63. #define BITMAIN_MAX_FAN_NUM 32
  64. #define BITMAIN_ARRAY_SIZE 16384
  65. #define BITMAIN_SEND_STATUS_TIME 10 //s
  66. #define BITMAIN_SEND_FULL_SPACE 128
  67. #endif
  68. #ifdef BITMAIN_TYPE_S2
  69. #define BITMAIN_MAX_WORK_NUM 64
  70. #define BITMAIN_MAX_WORK_QUEUE_NUM 4096
  71. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  72. #define BITMAIN_MAX_NONCE_NUM 128
  73. #define BITMAIN_MAX_CHAIN_NUM 16
  74. #define BITMAIN_MAX_TEMP_NUM 32
  75. #define BITMAIN_MAX_FAN_NUM 32
  76. #define BITMAIN_ARRAY_SIZE 16384
  77. #define BITMAIN_SEND_STATUS_TIME 15 //s
  78. #define BITMAIN_SEND_FULL_SPACE 512
  79. #endif
  80. #ifdef BITMAIN_TYPE_S3
  81. #define BITMAIN_MAX_WORK_NUM 8
  82. #define BITMAIN_MAX_WORK_QUEUE_NUM 1024
  83. #define BITMAIN_MAX_DEAL_QUEUE_NUM 2
  84. #define BITMAIN_MAX_NONCE_NUM 128
  85. #define BITMAIN_MAX_CHAIN_NUM 8
  86. #define BITMAIN_MAX_TEMP_NUM 32
  87. #define BITMAIN_MAX_FAN_NUM 32
  88. #define BITMAIN_ARRAY_SIZE 16384
  89. #define BITMAIN_SEND_STATUS_TIME 15 //s
  90. #define BITMAIN_SEND_FULL_SPACE 256
  91. #endif
  92. #ifdef BITMAIN_TYPE_S4
  93. #define BITMAIN_MAX_WORK_NUM 64
  94. #define BITMAIN_MAX_WORK_QUEUE_NUM 4096
  95. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  96. #define BITMAIN_MAX_NONCE_NUM 128
  97. #define BITMAIN_MAX_CHAIN_NUM 16
  98. #define BITMAIN_MAX_TEMP_NUM 32
  99. #define BITMAIN_MAX_FAN_NUM 32
  100. #define BITMAIN_ARRAY_SIZE 16384*2
  101. #define BITMAIN_SEND_STATUS_TIME 15 //s
  102. #define BITMAIN_SEND_FULL_SPACE 512
  103. #endif
  104. struct bitmain_packet_head {
  105. uint8_t token_type;
  106. uint8_t version;
  107. uint16_t length;
  108. } __attribute__((packed, aligned(4)));
  109. struct bitmain_txconfig_token {
  110. uint8_t token_type;
  111. uint8_t version;
  112. uint16_t length;
  113. uint8_t reset :1;
  114. uint8_t fan_eft :1;
  115. uint8_t timeout_eft :1;
  116. uint8_t frequency_eft :1;
  117. uint8_t voltage_eft :1;
  118. uint8_t chain_check_time_eft :1;
  119. uint8_t chip_config_eft :1;
  120. uint8_t hw_error_eft :1;
  121. uint8_t beeper_ctrl :1;
  122. uint8_t temp_over_ctrl :1;
  123. uint8_t fan_home_mode :1;
  124. uint8_t reserved1 :5;
  125. uint8_t chain_check_time;
  126. uint8_t reserved2;
  127. uint8_t chain_num;
  128. uint8_t asic_num;
  129. uint8_t fan_pwm_data;
  130. uint8_t timeout_data;
  131. uint16_t frequency;
  132. uint8_t voltage[2];
  133. uint8_t reg_data[4];
  134. uint8_t chip_address;
  135. uint8_t reg_address;
  136. uint16_t crc;
  137. } __attribute__((packed, aligned(4)));
  138. struct bitmain_txtask_work {
  139. uint32_t work_id;
  140. uint8_t midstate[32];
  141. uint8_t data2[12];
  142. } __attribute__((packed, aligned(4)));
  143. struct bitmain_txtask_token {
  144. uint8_t token_type;
  145. uint8_t version;
  146. uint16_t length;
  147. uint8_t new_block :1;
  148. uint8_t reserved1 :7;
  149. uint8_t diff;
  150. uint16_t net_diff;
  151. struct bitmain_txtask_work works[BITMAIN_MAX_WORK_NUM];
  152. uint16_t crc;
  153. } __attribute__((packed, aligned(4)));
  154. struct bitmain_rxstatus_token {
  155. uint8_t token_type;
  156. uint8_t version;
  157. uint16_t length;
  158. uint8_t chip_status_eft :1;
  159. uint8_t detect_get :1;
  160. uint8_t reserved1 :6;
  161. uint8_t reserved2[3];
  162. uint8_t chip_address;
  163. uint8_t reg_address;
  164. uint16_t crc;
  165. } __attribute__((packed, aligned(4)));
  166. struct bitmain_rxstatus_data {
  167. uint8_t data_type;
  168. uint8_t version;
  169. uint16_t length;
  170. uint8_t chip_value_eft :1;
  171. uint8_t reserved1 :3;
  172. uint8_t get_blk_num :4;
  173. uint8_t chain_num;
  174. uint16_t fifo_space;
  175. uint8_t hw_version[4];
  176. uint8_t fan_num;
  177. uint8_t temp_num;
  178. uint16_t fan_exist;
  179. uint32_t temp_exist;
  180. uint32_t nonce_error;
  181. uint32_t reg_value;
  182. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  183. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  184. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  185. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  186. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  187. uint16_t crc;
  188. } __attribute__((packed, aligned(4)));
  189. struct bitmain_rxnonce_nonce {
  190. uint32_t work_id;
  191. uint32_t nonce;
  192. } __attribute__((packed, aligned(4)));
  193. struct bitmain_rxnonce_data {
  194. uint8_t data_type;
  195. uint8_t version;
  196. uint16_t length;
  197. uint16_t fifo_space;
  198. uint16_t diff;
  199. uint64_t total_nonce_num;
  200. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_NONCE_NUM];
  201. uint16_t crc;
  202. } __attribute__((packed, aligned(4)));
  203. struct bitmain_info {
  204. int baud;
  205. int chain_num;
  206. int asic_num;
  207. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  208. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  209. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  210. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][320];
  211. int timeout;
  212. int errorcount;
  213. uint32_t nonce_error;
  214. uint32_t last_nonce_error;
  215. uint8_t reg_data[4];
  216. int fan_num;
  217. int fan[BITMAIN_MAX_FAN_NUM];
  218. int temp_num;
  219. int temp[BITMAIN_MAX_TEMP_NUM];
  220. int temp_max;
  221. int temp_avg;
  222. int temp_history_count;
  223. int temp_history_index;
  224. int temp_sum;
  225. int temp_old;
  226. int fan_pwm;
  227. uint64_t total_nonce_num;
  228. int frequency;
  229. char frequency_t[256];
  230. uint8_t voltage[2];
  231. char voltage_t[8];
  232. int diff;
  233. int no_matching_work;
  234. //int matching_work[BITMAIN_DEFAULT_CHAIN_NUM];
  235. struct thr_info *thr;
  236. pthread_t read_thr;
  237. pthread_t write_thr;
  238. pthread_mutex_t lock;
  239. pthread_mutex_t qlock;
  240. pthread_cond_t qcond;
  241. cgsem_t write_sem;
  242. int nonces;
  243. int fifo_space;
  244. int hw_version[4];
  245. unsigned int last_work_block;
  246. struct timeval last_status_time;
  247. int send_full_space;
  248. int auto_queued;
  249. int auto_nonces;
  250. int auto_hw;
  251. int idle;
  252. bool reset;
  253. bool overheat;
  254. bool optimal;
  255. };
  256. #define BITMAIN_READ_SIZE 12
  257. #define BTM_GETS_ERROR -1
  258. #define BTM_GETS_OK 0
  259. #define BTM_SEND_ERROR -1
  260. #define BTM_SEND_OK 0
  261. #define BITMAIN_READ_TIME(baud) ((double)BITMAIN_READ_SIZE * (double)8.0 / (double)(baud))
  262. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  263. ASSERT1(sizeof(uint32_t) == 4);
  264. extern struct bitmain_info **bitmain_info;
  265. extern char opt_bitmain_dev[256];
  266. extern int opt_bitmain_temp;
  267. extern int opt_bitmain_overheat;
  268. extern int opt_bitmain_fan_min;
  269. extern int opt_bitmain_fan_max;
  270. extern bool opt_bitmain_auto;
  271. extern char *set_bitmain_dev(char *arg);
  272. extern char *set_bitmain_fan(char *arg);
  273. #endif /* USE_BITMAIN */
  274. #endif /* BITMAIN_H */