driver-bitmain.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright 2013 BitMain project
  3. * Copyright 2013 BitMain <xlc1985@126.com>
  4. * Copyright 2014 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #ifndef BITMAIN_H
  12. #define BITMAIN_H
  13. #if defined(USE_ANT_S1) || defined(USE_ANT_S2)
  14. #include "util.h"
  15. #include "klist.h"
  16. #define BITMAIN_RESET_FAULT_DECISECONDS 1
  17. #define BITMAIN_MINER_THREADS 1
  18. #define BITMAIN_IO_SPEED 115200
  19. #define BITMAIN_HASH_TIME_FACTOR ((float)1.67/0x32)
  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_TEMP_TARGET 50
  33. #define BITMAIN_TEMP_HYSTERESIS 3
  34. #define BITMAIN_TEMP_OVERHEAT 75
  35. #define BITMAIN_DEFAULT_TIMEOUT 0x2D
  36. #define BITMAIN_MIN_FREQUENCY 10
  37. #define BITMAIN_MAX_FREQUENCY 1000000
  38. #define BITMAIN_TIMEOUT_FACTOR 12690
  39. #define BITMAIN_DEFAULT_FREQUENCY 282
  40. #define BITMAIN_DEFAULT_VOLTAGE 5
  41. #define BITMAIN_DEFAULT_CHAIN_NUM 8
  42. #define BITMAIN_DEFAULT_ASIC_NUM 32
  43. #define BITMAIN_DEFAULT_REG_DATA 0
  44. #define BITMAIN_AUTO_CYCLE 1024
  45. #ifdef USE_ANT_S1
  46. #define BITMAIN_FTDI_READSIZE 510
  47. #else // S2
  48. #define BITMAIN_FTDI_READSIZE 2048
  49. #endif
  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 USE_ANT_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. #else // S2
  63. #define BITMAIN_MAX_WORK_NUM 64
  64. #define BITMAIN_MAX_WORK_QUEUE_NUM 4096
  65. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  66. #define BITMAIN_MAX_NONCE_NUM 128
  67. #define BITMAIN_MAX_CHAIN_NUM 16
  68. #endif
  69. #define BITMAIN_MAX_TEMP_NUM 32
  70. #define BITMAIN_MAX_FAN_NUM 32
  71. #ifdef USE_ANT_S1
  72. #define BITMAIN_SEND_STATUS_TIME 10 //s
  73. #define BITMAIN_SEND_FULL_SPACE 128
  74. #else // S2
  75. #define BITMAIN_SEND_STATUS_TIME 15 //s
  76. #define BITMAIN_SEND_FULL_SPACE 512
  77. #endif
  78. #define BITMAIN_OVERHEAT_SLEEP_MS_MAX 10000
  79. #define BITMAIN_OVERHEAT_SLEEP_MS_MIN 200
  80. #define BITMAIN_OVERHEAT_SLEEP_MS_DEF 600
  81. #define BITMAIN_OVERHEAT_SLEEP_MS_STEP 200
  82. #ifdef USE_ANT_S2
  83. struct bitmain_packet_head {
  84. uint8_t token_type;
  85. uint8_t version;
  86. uint16_t length;
  87. } __attribute__((packed, aligned(4)));
  88. #endif
  89. struct bitmain_txconfig_token {
  90. uint8_t token_type;
  91. #ifdef USE_ANT_S1
  92. uint8_t length;
  93. #else // S2
  94. uint8_t version;
  95. uint16_t length;
  96. #endif
  97. uint8_t reset :1;
  98. uint8_t fan_eft :1;
  99. uint8_t timeout_eft :1;
  100. uint8_t frequency_eft :1;
  101. uint8_t voltage_eft :1;
  102. uint8_t chain_check_time_eft :1;
  103. uint8_t chip_config_eft :1;
  104. uint8_t hw_error_eft :1;
  105. #ifdef USE_ANT_S1
  106. uint8_t reserved1;
  107. #else // S2
  108. uint8_t beeper_ctrl :1;
  109. uint8_t temp_over_ctrl :1;
  110. uint8_t reserved1 :6;
  111. uint8_t reserved[2];
  112. #endif
  113. uint8_t chain_num;
  114. uint8_t asic_num;
  115. uint8_t fan_pwm_data;
  116. uint8_t timeout_data;
  117. uint16_t frequency;
  118. uint8_t voltage;
  119. uint8_t chain_check_time;
  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_txtask_work {
  126. uint32_t work_id;
  127. uint8_t midstate[32];
  128. uint8_t data2[12];
  129. } __attribute__((packed, aligned(4)));
  130. struct bitmain_txtask_token {
  131. #ifdef USE_ANT_S1
  132. uint8_t token_type;
  133. uint8_t reserved1;
  134. uint16_t length;
  135. uint8_t new_block :1;
  136. uint8_t reserved2 :7;
  137. uint8_t reserved3[3];
  138. struct bitmain_txtask_work works[BITMAIN_MAX_WORK_NUM];
  139. uint16_t crc;
  140. #else // S2
  141. uint8_t token_type;
  142. uint8_t version;
  143. uint16_t length;
  144. uint8_t new_block :1;
  145. uint8_t reserved1 :7;
  146. uint8_t diff;
  147. uint8_t reserved2[2];
  148. struct bitmain_txtask_work works[BITMAIN_MAX_WORK_NUM];
  149. uint16_t crc;
  150. #endif
  151. } __attribute__((packed, aligned(4)));
  152. struct bitmain_rxstatus_token {
  153. #ifdef USE_ANT_S1
  154. uint8_t token_type;
  155. uint8_t length;
  156. uint8_t chip_status_eft :1;
  157. uint8_t detect_get :1;
  158. uint8_t reserved1 :6;
  159. uint8_t reserved2;
  160. uint8_t chip_address;
  161. uint8_t reg_address;
  162. uint16_t crc;
  163. #else // S2
  164. uint8_t token_type;
  165. uint8_t version;
  166. uint16_t length;
  167. uint8_t chip_status_eft :1;
  168. uint8_t detect_get :1;
  169. uint8_t reserved1 :6;
  170. uint8_t reserved2[3];
  171. uint8_t chip_address;
  172. uint8_t reg_address;
  173. uint16_t crc;
  174. #endif
  175. } __attribute__((packed, aligned(4)));
  176. struct bitmain_rxstatus_data {
  177. #ifdef USE_ANT_S1
  178. uint8_t data_type;
  179. uint8_t length;
  180. uint8_t chip_value_eft :1;
  181. uint8_t reserved1 :7;
  182. uint8_t version;
  183. uint32_t fifo_space;
  184. uint32_t reg_value;
  185. uint32_t nonce_error;
  186. uint8_t chain_num;
  187. uint8_t temp_num;
  188. uint8_t fan_num;
  189. uint8_t reserved2;
  190. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM];
  191. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  192. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  193. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  194. uint16_t crc;
  195. #else // S2
  196. uint8_t data_type;
  197. uint8_t version;
  198. uint16_t length;
  199. uint8_t chip_value_eft :1;
  200. uint8_t reserved1 :7;
  201. uint8_t chain_num;
  202. uint16_t fifo_space;
  203. uint8_t hw_version[4];
  204. uint8_t fan_num;
  205. uint8_t temp_num;
  206. uint16_t fan_exist;
  207. uint32_t temp_exist;
  208. uint32_t nonce_error;
  209. uint32_t reg_value;
  210. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  211. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  212. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  213. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  214. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  215. uint16_t crc;
  216. #endif
  217. } __attribute__((packed, aligned(4)));
  218. struct bitmain_rxnonce_nonce {
  219. uint32_t work_id;
  220. uint32_t nonce;
  221. } __attribute__((packed, aligned(4)));
  222. struct bitmain_rxnonce_data {
  223. #ifdef USE_ANT_S1
  224. uint8_t data_type;
  225. uint8_t length;
  226. uint8_t fifo_space;
  227. uint8_t nonce_num;
  228. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_NONCE_NUM];
  229. uint16_t crc;
  230. #else
  231. uint8_t data_type;
  232. uint8_t version;
  233. uint16_t length;
  234. uint16_t fifo_space;
  235. uint16_t diff;
  236. uint64_t total_nonce_num;
  237. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_NONCE_NUM];
  238. uint16_t crc;
  239. #endif
  240. } __attribute__((packed, aligned(4)));
  241. struct bitmain_info {
  242. int queued;
  243. int results;
  244. #ifdef USE_ANT_S1
  245. int baud;
  246. int chain_num;
  247. int asic_num;
  248. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  249. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM];
  250. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][40];
  251. #else // S2
  252. int device_fd;
  253. int baud;
  254. int chain_num;
  255. int asic_num;
  256. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  257. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  258. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  259. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][320];
  260. #endif
  261. int timeout;
  262. int errorcount;
  263. uint32_t nonce_error;
  264. uint32_t last_nonce_error;
  265. uint8_t reg_data[4];
  266. int fan_num;
  267. int fan[BITMAIN_MAX_FAN_NUM];
  268. int temp_num;
  269. int temp[BITMAIN_MAX_TEMP_NUM];
  270. int temp_max;
  271. int temp_avg;
  272. int temp_history_count;
  273. int temp_history_index;
  274. int temp_sum;
  275. int fan_pwm;
  276. int frequency;
  277. int voltage;
  278. int temp_hi;
  279. #ifdef USE_ANT_S2
  280. uint64_t total_nonce_num;
  281. int diff;
  282. #endif
  283. int no_matching_work;
  284. //int matching_work[BITMAIN_DEFAULT_CHAIN_NUM];
  285. struct thr_info *thr;
  286. pthread_t read_thr;
  287. pthread_t write_thr;
  288. pthread_mutex_t lock;
  289. pthread_mutex_t qlock;
  290. pthread_cond_t qcond;
  291. cgsem_t write_sem;
  292. int nonces;
  293. int fifo_space;
  294. unsigned int last_work_block;
  295. struct timeval last_status_time;
  296. int send_full_space;
  297. #ifdef USE_ANT_S2
  298. int hw_version[4];
  299. #endif
  300. int auto_queued;
  301. int auto_hw;
  302. int idle;
  303. bool reset;
  304. bool optimal;
  305. #ifdef USE_ANT_S1
  306. bool overheat;
  307. int overheat_temp;
  308. uint32_t overheat_count;
  309. uint32_t overheat_sleep_ms;
  310. uint32_t overheat_sleeps;
  311. uint32_t overheat_slept;
  312. uint64_t overheat_total_sleep;
  313. uint32_t overheat_recovers;
  314. #endif
  315. // Work
  316. K_LIST *work_list;
  317. K_STORE *work_ready;
  318. #ifdef USE_ANT_S2
  319. K_STORE *wbuild;
  320. #endif
  321. uint32_t last_wid;
  322. uint64_t work_search;
  323. uint64_t tot_search;
  324. uint64_t min_search;
  325. uint64_t max_search;
  326. uint64_t failed_search;
  327. uint64_t tot_failed;
  328. uint64_t min_failed;
  329. uint64_t max_failed;
  330. };
  331. // Work
  332. typedef struct witem {
  333. struct work *work;
  334. uint32_t wid;
  335. } WITEM;
  336. #ifdef USE_ANT_S1
  337. #define ALLOC_WITEMS 1024
  338. #else
  339. #define ALLOC_WITEMS 32768
  340. #endif
  341. /*
  342. * The limit doesn't matter since we simply take the tail item
  343. * every time, optionally free it, and then put it on the head
  344. */
  345. #ifdef USE_ANT_S1
  346. #define LIMIT_WITEMS 1024
  347. #else
  348. #define LIMIT_WITEMS 32768
  349. #endif
  350. #define DATAW(_item) ((WITEM *)(_item->data))
  351. #define BITMAIN_READ_SIZE 12
  352. #ifdef USE_ANT_S2
  353. #define BITMAIN_ARRAY_SIZE 16384
  354. #endif
  355. #define BTM_GETS_ERROR -1
  356. #define BTM_GETS_OK 0
  357. #define BTM_SEND_ERROR -1
  358. #define BTM_SEND_OK 0
  359. #define BITMAIN_READ_TIME(baud) ((double)BITMAIN_READ_SIZE * (double)8.0 / (double)(baud))
  360. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  361. ASSERT1(sizeof(uint32_t) == 4);
  362. extern struct bitmain_info **bitmain_info;
  363. extern int opt_bitmain_temp;
  364. extern int opt_bitmain_overheat;
  365. extern int opt_bitmain_fan_min;
  366. extern int opt_bitmain_fan_max;
  367. extern int opt_bitmain_freq_min;
  368. extern int opt_bitmain_freq_max;
  369. extern bool opt_bitmain_auto;
  370. extern char *set_bitmain_fan(char *arg);
  371. extern char *set_bitmain_freq(char *arg);
  372. #endif /* USE_ANT_S1 || USE_ANT_S2 */
  373. #endif /* BITMAIN_H */