driver-bflsc.h 10 KB

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