driver-bitmain.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright 2013 BitMain project
  3. * Copyright 2013 BitMain <xlc1985@126.com>
  4. * Copyright 2014-2015 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. // S3 is a USB S2 with other minor changes (search for USE_ANT_S3)
  14. #if defined(USE_ANT_S3)
  15. #define USE_ANT_S2 1
  16. #endif
  17. #if (defined(USE_ANT_S1) || defined(USE_ANT_S2))
  18. #include "util.h"
  19. #include "klist.h"
  20. #define BITMAIN_RESET_FAULT_DECISECONDS 1
  21. #define BITMAIN_MINER_THREADS 1
  22. #define BITMAIN_IO_SPEED 115200
  23. #define BITMAIN_HASH_TIME_FACTOR ((float)1.67/0x32)
  24. #define BITMAIN_RESET_PITCH (300*1000*1000)
  25. #define BITMAIN_TOKEN_TYPE_TXCONFIG 0x51
  26. #define BITMAIN_TOKEN_TYPE_TXTASK 0x52
  27. #define BITMAIN_TOKEN_TYPE_RXSTATUS 0x53
  28. #define BITMAIN_DATA_TYPE_RXSTATUS 0xa1
  29. #define BITMAIN_DATA_TYPE_RXNONCE 0xa2
  30. #define BITMAIN_FAN_FACTOR 60
  31. #define BITMAIN_PWM_MAX 0xA0
  32. #define BITMAIN_DEFAULT_FAN_MIN 20
  33. #define BITMAIN_DEFAULT_FAN_MAX 100
  34. #define BITMAIN_DEFAULT_FAN_MAX_PWM 0xA0 /* 100% */
  35. #define BITMAIN_DEFAULT_FAN_MIN_PWM 0x20 /* 20% */
  36. #define BITMAIN_TEMP_TARGET 50
  37. #define BITMAIN_TEMP_HYSTERESIS 3
  38. #define BITMAIN_TEMP_OVERHEAT 75
  39. #define BITMAIN_DEFAULT_TIMEOUT 0x2D
  40. #define BITMAIN_MIN_FREQUENCY 10
  41. #define BITMAIN_MAX_FREQUENCY 1000000
  42. #define BITMAIN_TIMEOUT_FACTOR 12690
  43. #define BITMAIN_DEFAULT_FREQUENCY 282
  44. #define BITMAIN_DEFAULT_CHAIN_NUM 8
  45. #define BITMAIN_DEFAULT_ASIC_NUM 32
  46. #define BITMAIN_DEFAULT_REG_DATA 0
  47. #define BITMAIN_AUTO_CYCLE 1024
  48. #ifdef USE_ANT_S1
  49. #define BITMAIN_FTDI_READSIZE 510
  50. #define BITMAIN_DEFAULT_VOLTAGE 5
  51. #define BITMAIN_WORK_DELAY 1
  52. #else // S2 or S3
  53. #ifdef USE_ANT_S3 // S3
  54. #define BITMAIN_FTDI_READSIZE 510
  55. #define BITMAIN_VOLTAGE_DEF "0000"
  56. #define BITMAIN_VOLTAGE0_DEF 0x00
  57. #define BITMAIN_VOLTAGE1_DEF 0x00
  58. #define BITMAIN_WORK_DELAY 1
  59. #else // S2
  60. #define BITMAIN_FTDI_READSIZE 2048
  61. #define BITMAIN_VOLTAGE_DEF "0725"
  62. #define BITMAIN_VOLTAGE0_DEF 0x07
  63. #define BITMAIN_VOLTAGE1_DEF 0x25
  64. #define BITMAIN_WORK_DELAY 1
  65. #endif
  66. #endif
  67. #define BITMAIN_USB_PACKETSIZE 512
  68. #define BITMAIN_SENDBUF_SIZE 8192
  69. #define BITMAIN_READBUF_SIZE 8192
  70. #define BITMAIN_RESET_TIMEOUT 100
  71. #define BITMAIN_LATENCY 1
  72. #ifdef USE_ANT_S1
  73. #define BITMAIN_READ_TIMEOUT 18 /* Enough to only half fill the buffer */
  74. #define BITMAIN_MAX_WORK_NUM 8
  75. #define BITMAIN_MAX_WORK_QUEUE_NUM 64
  76. #define BITMAIN_MAX_DEAL_QUEUE_NUM 1
  77. #define BITMAIN_MAX_NONCE_NUM 8
  78. #define BITMAIN_MAX_CHAIN_NUM 8
  79. #else // S2 or S3
  80. #ifdef USE_ANT_S3
  81. #define BITMAIN_READ_TIMEOUT 100
  82. #define BITMAIN_MAX_WORK_NUM 8
  83. #define BITMAIN_MAX_WORK_QUEUE_NUM 1024
  84. #define BITMAIN_MAX_DEAL_QUEUE_NUM 2
  85. #define BITMAIN_MAX_NONCE_NUM 128
  86. #define BITMAIN_MAX_CHAIN_NUM 8
  87. #else // S2
  88. #define BITMAIN_READ_TIMEOUT 0 // Ignored
  89. #define BITMAIN_MAX_WORK_NUM 64
  90. #define BITMAIN_MAX_WORK_QUEUE_NUM 4096
  91. #define BITMAIN_MAX_DEAL_QUEUE_NUM 32
  92. #define BITMAIN_MAX_NONCE_NUM 128
  93. #define BITMAIN_MAX_CHAIN_NUM 16
  94. #endif
  95. #endif
  96. #define BITMAIN_MAX_TEMP_NUM 32
  97. #define BITMAIN_MAX_FAN_NUM 32
  98. #ifdef USE_ANT_S1
  99. #define BITMAIN_SEND_STATUS_TIME 10 //s
  100. #define BITMAIN_SEND_FULL_SPACE 128
  101. #else // S2 or S3
  102. #define BITMAIN_SEND_STATUS_TIME 15 //s
  103. #ifdef USE_ANT_S3
  104. #define BITMAIN_SEND_FULL_SPACE 256
  105. #else
  106. #define BITMAIN_SEND_FULL_SPACE 512
  107. #endif
  108. #endif
  109. #define BITMAIN_OVERHEAT_SLEEP_MS_MAX 10000
  110. #define BITMAIN_OVERHEAT_SLEEP_MS_MIN 200
  111. #define BITMAIN_OVERHEAT_SLEEP_MS_DEF 600
  112. #define BITMAIN_OVERHEAT_SLEEP_MS_STEP 200
  113. #ifdef USE_ANT_S2
  114. struct bitmain_packet_head {
  115. uint8_t token_type;
  116. uint8_t version;
  117. uint16_t length;
  118. } __attribute__((packed, aligned(4)));
  119. #endif
  120. struct bitmain_txconfig_token {
  121. uint8_t token_type;
  122. #ifdef USE_ANT_S1
  123. uint8_t length;
  124. #else // S2
  125. uint8_t version;
  126. uint16_t length;
  127. #endif
  128. uint8_t reset :1;
  129. uint8_t fan_eft :1;
  130. uint8_t timeout_eft :1;
  131. uint8_t frequency_eft :1;
  132. uint8_t voltage_eft :1;
  133. uint8_t chain_check_time_eft :1;
  134. uint8_t chip_config_eft :1;
  135. uint8_t hw_error_eft :1;
  136. #ifdef USE_ANT_S1
  137. uint8_t reserved1;
  138. #else // S2
  139. uint8_t beeper_ctrl :1;
  140. uint8_t temp_over_ctrl :1;
  141. uint8_t reserved1 :6;
  142. uint8_t chain_check_time;
  143. uint8_t reserved2;
  144. #endif
  145. uint8_t chain_num;
  146. uint8_t asic_num;
  147. uint8_t fan_pwm_data;
  148. uint8_t timeout_data;
  149. uint16_t frequency;
  150. #ifdef USE_ANT_S1
  151. uint8_t voltage;
  152. uint8_t chain_check_time;
  153. #else
  154. uint8_t voltage[2];
  155. #endif
  156. uint8_t reg_data[4];
  157. uint8_t chip_address;
  158. uint8_t reg_address;
  159. uint16_t crc;
  160. } __attribute__((packed, aligned(4)));
  161. struct bitmain_txtask_work {
  162. uint32_t work_id;
  163. uint8_t midstate[32];
  164. uint8_t data2[12];
  165. } __attribute__((packed, aligned(4)));
  166. struct bitmain_txtask_token {
  167. #ifdef USE_ANT_S1
  168. uint8_t token_type;
  169. uint8_t reserved1;
  170. uint16_t length;
  171. uint8_t new_block :1;
  172. uint8_t reserved2 :7;
  173. uint8_t reserved3[3];
  174. struct bitmain_txtask_work works[BITMAIN_MAX_WORK_NUM];
  175. uint16_t crc;
  176. #else // S2
  177. uint8_t token_type;
  178. uint8_t version;
  179. uint16_t length;
  180. uint8_t new_block :1;
  181. uint8_t reserved1 :7;
  182. uint8_t diff;
  183. uint8_t reserved2[2];
  184. struct bitmain_txtask_work works[BITMAIN_MAX_WORK_NUM];
  185. uint16_t crc;
  186. #endif
  187. } __attribute__((packed, aligned(4)));
  188. struct bitmain_rxstatus_token {
  189. #ifdef USE_ANT_S1
  190. uint8_t token_type;
  191. uint8_t length;
  192. uint8_t chip_status_eft :1;
  193. uint8_t detect_get :1;
  194. uint8_t reserved1 :6;
  195. uint8_t reserved2;
  196. uint8_t chip_address;
  197. uint8_t reg_address;
  198. uint16_t crc;
  199. #else // S2
  200. uint8_t token_type;
  201. uint8_t version;
  202. uint16_t length;
  203. uint8_t chip_status_eft :1;
  204. uint8_t detect_get :1;
  205. uint8_t reserved1 :6;
  206. uint8_t reserved2[3];
  207. uint8_t chip_address;
  208. uint8_t reg_address;
  209. uint16_t crc;
  210. #endif
  211. } __attribute__((packed, aligned(4)));
  212. struct bitmain_rxstatus_data {
  213. #ifdef USE_ANT_S1
  214. uint8_t data_type;
  215. uint8_t length;
  216. uint8_t chip_value_eft :1;
  217. uint8_t reserved1 :7;
  218. uint8_t version;
  219. uint32_t fifo_space;
  220. uint32_t reg_value;
  221. uint32_t nonce_error;
  222. uint8_t chain_num;
  223. uint8_t temp_num;
  224. uint8_t fan_num;
  225. uint8_t reserved2;
  226. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM];
  227. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  228. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  229. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  230. uint16_t crc;
  231. #else // S2
  232. uint8_t data_type;
  233. uint8_t version;
  234. uint16_t length;
  235. uint8_t chip_value_eft :1;
  236. uint8_t reserved1 :7;
  237. uint8_t chain_num;
  238. uint16_t fifo_space;
  239. uint8_t hw_version[4];
  240. uint8_t fan_num;
  241. uint8_t temp_num;
  242. uint16_t fan_exist;
  243. uint32_t temp_exist;
  244. uint32_t nonce_error;
  245. uint32_t reg_value;
  246. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  247. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  248. uint8_t chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  249. uint8_t temp[BITMAIN_MAX_TEMP_NUM];
  250. uint8_t fan[BITMAIN_MAX_FAN_NUM];
  251. uint16_t crc;
  252. #endif
  253. } __attribute__((packed, aligned(4)));
  254. struct bitmain_rxnonce_nonce {
  255. uint32_t work_id;
  256. uint32_t nonce;
  257. } __attribute__((packed, aligned(4)));
  258. struct bitmain_rxnonce_data {
  259. #ifdef USE_ANT_S1
  260. uint8_t data_type;
  261. uint8_t length;
  262. uint8_t fifo_space;
  263. uint8_t nonce_num;
  264. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_NONCE_NUM];
  265. uint16_t crc;
  266. #else
  267. uint8_t data_type;
  268. uint8_t version;
  269. uint16_t length;
  270. uint16_t fifo_space;
  271. uint16_t diff;
  272. uint64_t total_nonce_num;
  273. struct bitmain_rxnonce_nonce nonces[BITMAIN_MAX_NONCE_NUM];
  274. uint16_t crc;
  275. #endif
  276. } __attribute__((packed, aligned(4)));
  277. struct bitmain_info {
  278. int queued;
  279. int results;
  280. #ifdef USE_ANT_S1
  281. int baud;
  282. int chain_num;
  283. int asic_num;
  284. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  285. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM];
  286. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][40];
  287. #else // S2 or S3
  288. #ifndef USE_ANT_S3
  289. int device_fd;
  290. #endif
  291. int baud;
  292. int chain_num;
  293. int asic_num;
  294. int chain_asic_num[BITMAIN_MAX_CHAIN_NUM];
  295. uint32_t chain_asic_exist[BITMAIN_MAX_CHAIN_NUM*8];
  296. uint32_t chain_asic_status[BITMAIN_MAX_CHAIN_NUM*8];
  297. char chain_asic_status_t[BITMAIN_MAX_CHAIN_NUM][320];
  298. #endif
  299. int timeout;
  300. int errorcount;
  301. uint32_t nonce_error;
  302. uint32_t last_nonce_error;
  303. uint8_t reg_data[4];
  304. int fan_num;
  305. int fan[BITMAIN_MAX_FAN_NUM];
  306. int temp_num;
  307. int temp[BITMAIN_MAX_TEMP_NUM];
  308. int temp_max;
  309. int temp_avg;
  310. int temp_history_count;
  311. int temp_history_index;
  312. int temp_sum;
  313. int fan_pwm;
  314. int frequency;
  315. int temp_hi;
  316. #ifdef USE_ANT_S1
  317. int voltage;
  318. #else
  319. uint8_t voltage[2];
  320. uint64_t total_nonce_num;
  321. int diff;
  322. #endif
  323. int no_matching_work;
  324. //int matching_work[BITMAIN_DEFAULT_CHAIN_NUM];
  325. struct thr_info *thr;
  326. pthread_t read_thr;
  327. pthread_t write_thr;
  328. pthread_mutex_t lock;
  329. pthread_mutex_t qlock;
  330. pthread_cond_t qcond;
  331. cgsem_t write_sem;
  332. int nonces;
  333. int fifo_space;
  334. unsigned int last_work_block;
  335. struct timeval last_status_time;
  336. int send_full_space;
  337. #ifdef USE_ANT_S2
  338. int hw_version[4];
  339. #endif
  340. int auto_queued;
  341. int auto_hw;
  342. int idle;
  343. bool reset;
  344. bool optimal;
  345. #ifdef USE_ANT_S1
  346. bool overheat;
  347. int overheat_temp;
  348. uint32_t overheat_count;
  349. uint32_t overheat_sleep_ms;
  350. uint32_t overheat_sleeps;
  351. uint32_t overheat_slept;
  352. uint64_t overheat_total_sleep;
  353. uint32_t overheat_recovers;
  354. #endif
  355. // Work
  356. K_LIST *work_list;
  357. K_STORE *work_ready;
  358. #ifdef USE_ANT_S2
  359. K_STORE *wbuild;
  360. #endif
  361. uint32_t last_wid;
  362. uint64_t work_search;
  363. uint64_t tot_search;
  364. uint64_t min_search;
  365. uint64_t max_search;
  366. uint64_t failed_search;
  367. uint64_t tot_failed;
  368. uint64_t min_failed;
  369. uint64_t max_failed;
  370. uint64_t fill_calls;
  371. uint64_t fill_nospace;
  372. uint64_t fifo_checks;
  373. uint64_t fill_neededless;
  374. uint64_t fill_totalneeded;
  375. uint64_t fill_need[BITMAIN_MAX_WORK_NUM+1];
  376. uint64_t fill_want;
  377. uint64_t fill_nowork;
  378. uint64_t fill_roll;
  379. int fill_rollmin;
  380. int fill_rollmax;
  381. uint64_t fill_rolltot;
  382. uint64_t fill_toosmall;
  383. uint64_t fill_less;
  384. uint64_t fill_sends;
  385. uint64_t fill_totalsend;
  386. uint64_t fill_send[BITMAIN_MAX_WORK_NUM+1];
  387. uint64_t fill_sendless[BITMAIN_MAX_WORK_NUM+1];
  388. uint64_t fill_seterr;
  389. uint64_t fill_senderr;
  390. uint64_t need_over;
  391. uint64_t need_nowork[BITMAIN_MAX_WORK_NUM+1];
  392. uint64_t fill_sendstatus;
  393. uint64_t read_good;
  394. uint64_t read_size;
  395. uint64_t read_18s;
  396. int read_sizemin;
  397. int read_sizemax;
  398. uint64_t read_bad;
  399. uint64_t readbuf_over;
  400. };
  401. // Work
  402. typedef struct witem {
  403. struct work *work;
  404. uint32_t wid;
  405. } WITEM;
  406. #ifdef USE_ANT_S1
  407. #define ALLOC_WITEMS 1024
  408. #else
  409. #ifdef USE_ANT_S3
  410. #define ALLOC_WITEMS 4096
  411. #else // S2
  412. #define ALLOC_WITEMS 32768
  413. #endif
  414. #endif
  415. /*
  416. * The limit doesn't matter since we simply take the tail item
  417. * every time, optionally free it, and then put it on the head
  418. */
  419. #ifdef USE_ANT_S1
  420. #define LIMIT_WITEMS 1024
  421. #else
  422. #ifdef USE_ANT_S3
  423. #define LIMIT_WITEMS 4096
  424. #else // S2
  425. #define LIMIT_WITEMS 32768
  426. #endif
  427. #endif
  428. #define DATAW(_item) ((WITEM *)(_item->data))
  429. #define BITMAIN_READ_SIZE 12
  430. #ifdef USE_ANT_S2
  431. #define BITMAIN_ARRAY_SIZE 16384
  432. #endif
  433. #define BTM_GETS_ERROR -1
  434. #define BTM_GETS_OK 0
  435. #define BTM_SEND_ERROR -1
  436. #define BTM_SEND_OK 0
  437. #define BITMAIN_READ_TIME(baud) ((double)BITMAIN_READ_SIZE * (double)8.0 / (double)(baud))
  438. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  439. ASSERT1(sizeof(uint32_t) == 4);
  440. extern struct bitmain_info **bitmain_info;
  441. extern int opt_bitmain_temp;
  442. extern int opt_bitmain_workdelay;
  443. extern int opt_bitmain_overheat;
  444. extern int opt_bitmain_fan_min;
  445. extern int opt_bitmain_fan_max;
  446. extern int opt_bitmain_freq_min;
  447. extern int opt_bitmain_freq_max;
  448. extern bool opt_bitmain_auto;
  449. extern char *set_bitmain_fan(char *arg);
  450. #endif /* USE_ANT_S1 || USE_ANT_S2 */
  451. #endif /* BITMAIN_H */