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