driver-bflsc.h 10 KB

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