/ driver-bflsc.h
driver-bflsc.h
  1  /*
  2   * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  3   * Copyright 2013 Andrew Smith
  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  
 11  #ifndef BFLSC_H
 12  #define BFLSC_H
 13  #define BLANK ""
 14  #define LFSTR "<LF>"
 15  
 16  /*
 17   * Firmware
 18   * DRV_V2 expects (beyond V1) the GetInfo to return the chip count
 19   * The queues are 40 instead of 20 and are *usually* consumed and filled
 20   * in bursts due to e.g. a 16 chip device doing 16 items at a time and
 21   * returning 16 results at a time
 22   * If the device has varying chip speeds, it will gradually break up the
 23   * burst of results as we progress
 24   */
 25  enum driver_version {
 26  	BFLSC_DRVUNDEF = 0,
 27  	BFLSC_DRV1,
 28  	BFLSC_DRV2
 29  };
 30  
 31  /*
 32   * With Firmware 1.0.0 and a result queue of 20 the Max is:
 33   * inprocess = 12
 34   * max count = 9
 35   * 64+1+24+1+1+(1+8)*8+1 per line = 164 * 20
 36   * OK = 3
 37   * Total: 3304
 38   *
 39   * With Firmware 1.2.* and a result queue of 40 but a limit of 15 replies:
 40   * inprocess = 12
 41   * max count = 9
 42   * 64+1+24+1+1+1+1+(1+8)*8+1 per line = 166 * 15
 43   * OK = 3
 44   * Total: 2514
 45   *
 46   */
 47  #define BFLSC_BUFSIZ (0x1000)
 48  
 49  // Should be big enough
 50  #define BFLSC_APPLOGSIZ 8192
 51  
 52  #define BFLSC_INFO_TIMEOUT 999
 53  
 54  #define BFLSC_DI_FIRMWARE "FIRMWARE"
 55  #define BFLSC_DI_ENGINES "ENGINES"
 56  #define BFLSC_DI_JOBSINQUE "JOBS IN QUEUE"
 57  #define BFLSC_DI_XLINKMODE "XLINK MODE"
 58  #define BFLSC_DI_XLINKPRESENT "XLINK PRESENT"
 59  #define BFLSC_DI_DEVICESINCHAIN "DEVICES IN CHAIN"
 60  #define BFLSC_DI_CHAINPRESENCE "CHAIN PRESENCE MASK"
 61  #define BFLSC_DI_CHIPS "CHIP PARALLELIZATION"
 62  #define BFLSC_DI_CHIPS_PARALLEL "YES"
 63  #define BFLSC28_DI_ASICS "ASIC Installed"
 64  
 65  #define FULLNONCE 0x100000000ULL
 66  
 67  struct bflsc_dev {
 68  	// Work
 69  	unsigned int ms_work;
 70  	int work_queued;
 71  	int work_complete;
 72  	int nonces_hw; // TODO: this - need to add a paramter to submit_nonce()
 73  			// so can pass 'dev' to hw_error
 74  	uint64_t hashes_unsent;
 75  	uint64_t hashes_sent;
 76  	uint64_t nonces_found;
 77  
 78  	struct timeval last_check_result;
 79  	struct timeval last_dev_result; // array > 0
 80  	struct timeval last_nonce_result; // > 0 nonce
 81  
 82  	// Info
 83  	char getinfo[(BFLSC_BUFSIZ+4)*4];
 84  	char *firmware;
 85  	int engines; // each engine represents a 'thread' in a chip
 86  	char *xlink_mode;
 87  	char *xlink_present;
 88  	char *chips;
 89  
 90  	// Status
 91  	bool dead; // TODO: handle seperate x-link devices failing?
 92  	bool overheat;
 93  
 94  	// Stats
 95  	float temp1;
 96  	float temp2;
 97  	float vcc1;
 98  	float vcc2;
 99  	float vmain;
100  	float temp1_max;
101  	float temp2_max;
102  	time_t temp1_max_time;
103  	time_t temp2_max_time;
104  	float temp1_5min_av; // TODO:
105  	float temp2_5min_av; // TODO:
106  
107  	// To handle the fact that flushing the queue may not remove all work
108  	// (normally one item is still being processed)
109  	// and also that once the queue is flushed, results may still be in
110  	// the output queue - but we don't want to process them at the time of doing an LP
111  	// when result_id > flush_id+1, flushed work can be discarded since it
112  	// is no longer in the device
113  	uint64_t flush_id; // counter when results were last flushed
114  	uint64_t result_id; // counter when results were last checked
115  	bool flushed; // are any flushed?
116  };
117  
118  #define QUE_MAX_RESULTS 8
119  
120  struct bflsc_work {
121  	UT_hash_handle hh;
122  	int id;
123  	struct work *work;
124  };
125  
126  struct bflsc_info {
127  	enum sub_ident ident;
128  	enum driver_version driver_version;
129  	pthread_rwlock_t stat_lock;
130  	struct thr_info results_thr;
131  	uint64_t hashes_sent;
132  	uint32_t update_count;
133  	struct timeval last_update;
134  	int sc_count;
135  	struct bflsc_dev *sc_devs;
136  	unsigned int scan_sleep_time;
137  	unsigned int results_sleep_time;
138  	unsigned int default_ms_work;
139  	bool shutdown;
140  	bool flash_led;
141  	bool not_first_work; // allow ignoring the first nonce error
142  	bool fanauto;
143  	int que_size;
144  	int que_full_enough;
145  	int que_watermark;
146  	int que_low;
147  	int que_noncecount;
148  	int que_fld_min;
149  	int que_fld_max;
150  	uint64_t core_nonces[17];
151  	uint64_t core_hw[17];
152  	int flush_size;
153  	// count of given size, [+2] is for any > QUE_MAX_RESULTS
154  	uint64_t result_size[QUE_MAX_RESULTS+2];
155  
156  	struct bflsc_work *bworks;
157  	uint64_t cortex_nonces[0x80];
158  	uint64_t cortex_hw[0x80];
159  
160  	int volt_next;
161  	bool volt_next_stat;
162  	int clock_next;
163  	bool clock_next_stat;
164  };
165  
166  #define BFLSC_XLINKHDR '@'
167  #define BFLSC_MAXPAYLOAD 255
168  
169  struct DataForwardToChain {
170  	uint8_t header;
171  	uint8_t payloadSize;
172  	uint8_t deviceAddress;
173  	uint8_t payloadData[BFLSC_MAXPAYLOAD];
174  };
175  
176  #define DATAFORWARDSIZE(data) (1 + 1 + 1 + data.payloadSize)
177  
178  #define MIDSTATE_BYTES 32
179  #define MERKLE_OFFSET 64
180  #define MERKLE_BYTES 12
181  #define BFLSC_QJOBSIZ (MIDSTATE_BYTES+MERKLE_BYTES+1)
182  #define BFLSC_EOB 0xaa
183  
184  struct QueueJobStructure {
185  	uint8_t payloadSize;
186  	uint8_t midState[MIDSTATE_BYTES];
187  	uint8_t blockData[MERKLE_BYTES];
188  	uint8_t endOfBlock;
189  };
190  
191  #define QUE_RES_LINES_MIN 3
192  #define QUE_MIDSTATE 0
193  #define QUE_BLOCKDATA 1
194  
195  #define QUE_UID 0
196  #define QUE_CC 1
197  
198  #define QUE_NONCECOUNT_V1 2
199  #define QUE_FLD_MIN_V1 3
200  #define QUE_FLD_MAX_V1 (QUE_MAX_RESULTS+QUE_FLD_MIN_V1)
201  
202  #define QUE_CHIP_V2 2
203  #define QUE_NONCECOUNT_V2 3
204  #define QUE_FLD_MIN_V2 4
205  #define QUE_FLD_MAX_V2 (QUE_MAX_RESULTS+QUE_FLD_MIN_V2)
206  
207  #define BFLSC_SIGNATURE 0xc1
208  #define BFLSC_EOW 0xfe
209  
210  // N.B. this will only work with 5 jobs
211  // requires a different jobs[N] for each job count
212  // but really only need to handle 5 anyway
213  struct QueueJobPackStructure {
214  	uint8_t payloadSize;
215  	uint8_t signature;
216  	uint8_t jobsInArray;
217  	struct QueueJobStructure jobs[5];
218  	uint8_t endOfWrapper;
219  };
220  
221  // TODO: Implement in API and also in usb device selection
222  struct SaveString {
223  	uint8_t payloadSize;
224  	uint8_t payloadData[BFLSC_MAXPAYLOAD];
225  };
226  
227  // Commands (Single Stage)
228  #define BFLSC_IDENTIFY "ZGX"
229  #define BFLSC_IDENTIFY_LEN (sizeof(BFLSC_IDENTIFY)-1)
230  #define BFLSC_DETAILS "ZCX"
231  #define BFLSC_DETAILS_LEN (sizeof(BFLSC_DETAILS)-1)
232  #define BFLSC_FIRMWARE "ZJX"
233  #define BFLSC_FIRMWARE_LEN (sizeof(BFLSC_FIRMWARE)-1)
234  #define BFLSC_FLASH "ZMX"
235  #define BFLSC_FLASH_LEN (sizeof(BFLSC_FLASH)-1)
236  #define BFLSC_VOLTAGE "ZTX"
237  #define BFLSC_VOLTAGE_LEN (sizeof(BFLSC_VOLTAGE)-1)
238  #define BFLSC_TEMPERATURE "ZLX"
239  #define BFLSC_TEMPERATURE_LEN (sizeof(BFLSC_TEMPERATURE)-1)
240  #define BFLSC_QRES "ZOX"
241  #define BFLSC_QRES_LEN (sizeof(BFLSC_QRES)-1)
242  #define BFLSC_QFLUSH "ZQX"
243  #define BFLSC_QFLUSH_LEN (sizeof(BFLSC_QFLUSH)-1)
244  #define BFLSC_FANAUTO "Z9X"
245  #define BFLSC_FANOUT_LEN (sizeof(BFLSC_FANAUTO)-1)
246  #define BFLSC_FAN0 "Z0X"
247  #define BFLSC_FAN0_LEN (sizeof(BFLSC_FAN0)-1)
248  #define BFLSC_FAN1 "Z1X"
249  #define BFLSC_FAN1_LEN (sizeof(BFLSC_FAN1)-1)
250  #define BFLSC_FAN2 "Z2X"
251  #define BFLSC_FAN2_LEN (sizeof(BFLSC_FAN2)-1)
252  #define BFLSC_FAN3 "Z3X"
253  #define BFLSC_FAN3_LEN (sizeof(BFLSC_FAN3)-1)
254  #define BFLSC_FAN4 "Z4X"
255  #define BFLSC_FAN4_LEN (sizeof(BFLSC_FAN4)-1)
256  #define BFLSC_LOADSTR "ZUX"
257  #define BFLSC_LOADSTR_LEN (sizeof(BFLSC_LOADSTR)-1)
258  
259  // Commands (Dual Stage)
260  #define BFLSC_QJOB "ZNX"
261  #define BFLSC_QJOB_LEN (sizeof(BFLSC_QJOB)-1)
262  #define BFLSC_QJOBS "ZWX"
263  #define BFLSC_QJOBS_LEN (sizeof(BFLSC_QJOBS)-1)
264  #define BFLSC_SAVESTR "ZSX"
265  #define BFLSC_SAVESTR_LEN (sizeof(BFLSC_SAVESTR)-1)
266  
267  // Replies
268  #define BFLSC_IDENTITY "BitFORCE SC"
269  #define BFLSC_BFLSC "SHA256 SC"
270  #define BFLSC_BFLSC28 "SC-28nm"
271  
272  #define BFLSC_OK "OK\n"
273  #define BFLSC_OK_LEN (sizeof(BFLSC_OK)-1)
274  #define BFLSC_SUCCESS "SUCCESS\n"
275  #define BFLSC_SUCCESS_LEN (sizeof(BFLSC_SUCCESS)-1)
276  
277  #define BFLSC_RESULT "COUNT:"
278  #define BFLSC_RESULT_LEN (sizeof(BFLSC_RESULT)-1)
279  
280  #define BFLSC_ANERR "ERR:"
281  #define BFLSC_ANERR_LEN (sizeof(BFLSC_ANERR)-1)
282  #define BFLSC_TIMEOUT BFLSC_ANERR "TIMEOUT"
283  #define BFLSC_TIMEOUT_LEN (sizeof(BFLSC_TIMEOUT)-1)
284  // x-link timeout has a space (a number follows)
285  #define BFLSC_XTIMEOUT BFLSC_ANERR "TIMEOUT "
286  #define BFLSC_XTIMEOUT_LEN (sizeof(BFLSC_XTIMEOUT)-1)
287  #define BFLSC_INVALID BFLSC_ANERR "INVALID DATA"
288  #define BFLSC_INVALID_LEN (sizeof(BFLSC_INVALID)-1)
289  #define BFLSC_ERRSIG BFLSC_ANERR "SIGNATURE"
290  #define BFLSC_ERRSIG_LEN (sizeof(BFLSC_ERRSIG)-1)
291  #define BFLSC_OKQ "OK:QUEUED"
292  #define BFLSC_OKQ_LEN (sizeof(BFLSC_OKQ)-1)
293  #define BFLSC_INPROCESS "INPROCESS"
294  #define BFLSC_INPROCESS_LEN (sizeof(BFLSC_INPROCESS)-1)
295  // Followed by N=1..5
296  #define BFLSC_OKQN "OK:QUEUED "
297  #define BFLSC_OKQN_LEN (sizeof(BFLSC_OKQN)-1)
298  #define BFLSC_QFULL "QUEUE FULL"
299  #define BFLSC_QFULL_LEN (sizeof(BFLSC_QFULL)-1)
300  #define BFLSC_HITEMP "HIGH TEMPERATURE RECOVERY"
301  #define BFLSC_HITEMP_LEN (sizeof(BFLSC_HITEMP)-1)
302  #define BFLSC_EMPTYSTR "MEMORY EMPTY"
303  #define BFLSC_EMPTYSTR_LEN (sizeof(BFLSC_EMPTYSTR)-1)
304  
305  // Queued and non-queued are the same
306  #define FullNonceRangeJob QueueJobStructure
307  #define BFLSC_JOBSIZ BFLSC_QJOBSIZ
308  
309  // Non queued commands (not used)
310  #define BFLSC_SENDWORK "ZDX"
311  #define BFLSC_SENDWORK_LEN (sizeof(BFLSC_SENDWORK)-1)
312  #define BFLSC_WORKSTATUS "ZFX"
313  #define BFLSC_WORKSTATUS_LEN (sizeof(BFLSC_WORKSTATUS)-1)
314  #define BFLSC_SENDRANGE "ZPX"
315  #define BFLSC_SENDRANGE_LEN (sizeof(BFLSC_SENDRANGE)-1)
316  
317  // Non queued work replies (not used)
318  #define BFLSC_NONCE "NONCE-FOUND:"
319  #define BFLSC_NONCE_LEN (sizeof(BFLSC_NONCE)-1)
320  #define BFLSC_NO_NONCE "NO-NONCE"
321  #define BFLSC_NO_NONCE_LEN (sizeof(BFLSC_NO_NONCE)-1)
322  #define BFLSC_IDLE "IDLE"
323  #define BFLSC_IDLE_LEN (sizeof(BFLSC_IDLE)-1)
324  #define BFLSC_BUSY "BUSY"
325  #define BFLSC_BUSY_LEN (sizeof(BFLSC_BUSY)-1)
326  
327  #define BFLSC_MINIRIG "BAM"
328  #define BFLSC_SINGLE "BAS"
329  #define BFLSC_LITTLESINGLE "BAL"
330  #define BFLSC_JALAPENO "BAJ"
331  #define BFLSC_MONARCH "BMA"
332  
333  // Default expected time for a nonce range
334  // - thus no need to check until this + last time work was found
335  // 60GH/s MiniRig (1 board) or Single
336  #define BAM_WORK_TIME 71.58
337  #define BAS_WORK_TIME 71.58
338  // 30GH/s Little Single
339  #define BAL_WORK_TIME 143.17
340  // 4.5GH/s Jalapeno
341  #define BAJ_WORK_TIME 954.44
342  #define BMA_WORK_TIME 35 // ???
343  
344  // Defaults (slightly over half the work time) but ensure none are above 100
345  // SCAN_TIME - delay after sending work
346  // RES_TIME - delay between checking for results
347  #define BAM_SCAN_TIME 20
348  #define BMA_SCAN_TIME 50
349  #define BAS_SCAN_TIME 360
350  #define BAL_SCAN_TIME 720
351  #define BAJ_SCAN_TIME 1000
352  #define BFLSC_RES_TIME 100
353  #define BMA_RES_TIME 50
354  #define BFLSC_MAX_SLEEP 2000
355  
356  #define BAJ_LATENCY LATENCY_STD
357  #define BAL_LATENCY 12
358  #define BAS_LATENCY 12
359  // For now a BAM doesn't really exist - it's currently 8 independent BASs
360  #define BAM_LATENCY 2
361  
362  #define BFLSC_TEMP_SLEEPMS 5
363  
364  #define BFLSC_QUE_SIZE_V1 20
365  #define BFLSC_QUE_FULL_ENOUGH_V1 13
366  #define BFLSC_QUE_WATERMARK_V1 6
367  #define BFLSC_QUE_LOW_V1 3
368  
369  // TODO: use 5 batch jobs
370  // TODO: base these numbers on the chip count?
371  #define BFLSC_QUE_SIZE_V2 40
372  #define BFLSC_QUE_FULL_ENOUGH_V2 36
373  #define BFLSC_QUE_WATERMARK_V2 32
374  #define BFLSC_QUE_LOW_V2 16
375  
376  #define BFLSC_TEMP_OVERHEAT 85
377  // Will start throttling this much below overheat
378  #define BFLSC_TEMP_THROTTLE 3
379  // Must drop this far below overheat before resuming work
380  #define BFLSC_TEMP_RECOVER 5
381  
382  // If initialisation fails the first time,
383  // sleep this amount (ms) and try again
384  #define REINIT_TIME_FIRST_MS 100
385  // Max ms per sleep
386  #define REINIT_TIME_MAX_MS 800
387  // Keep trying up to this many us
388  #define REINIT_TIME_MAX 3000000
389  
390  extern int opt_bflsc_overheat;
391  
392  #endif /* BFLSC_H */