driver-bflsc.h 11 KB

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