driver-bflsc.h 10 KB

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