driver-bflsc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013 Con Kolivas
  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. #include <float.h>
  11. #include <limits.h>
  12. #include <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <strings.h>
  16. #include <sys/time.h>
  17. #include <unistd.h>
  18. #include "config.h"
  19. #ifdef WIN32
  20. #include <windows.h>
  21. #endif
  22. #include "compat.h"
  23. #include "miner.h"
  24. #include "usbutils.h"
  25. #define BLANK ""
  26. #define LFSTR "<LF>"
  27. /*
  28. * With Firmware 1.0.0 and a result queue of 20 the Max is:
  29. * header = 9
  30. * 64+1+32+1+1+(1+8)*8+1 per line = 172 * 20
  31. * OK = 3
  32. * Total: 3452
  33. */
  34. #define BFLSC_BUFSIZ (0x1000)
  35. #define BFLSC_DI_FIRMWARE "FIRMWARE"
  36. #define BFLSC_DI_ENGINES "ENGINES"
  37. #define BFLSC_DI_JOBSINQUE "JOBS IN QUEUE"
  38. #define BFLSC_DI_XLINKMODE "XLINK MODE"
  39. #define BFLSC_DI_XLINKPRESENT "XLINK PRESENT"
  40. #define BFLSC_DI_DEVICESINCHAIN "DEVICES IN CHAIN"
  41. #define BFLSC_DI_CHAINPRESENCE "CHAIN PRESENCE MASK"
  42. #define FULLNONCE 0x100000000ULL
  43. struct bflsc_dev {
  44. // Work
  45. unsigned int ms_work;
  46. int work_queued;
  47. int work_complete;
  48. int nonces_hw; // TODO: this - need to add a paramter to submit_nonce()
  49. // so can pass 'dev' to hw_error
  50. uint64_t hashes_unsent;
  51. uint64_t hashes_sent;
  52. uint64_t nonces_found;
  53. struct timeval last_check_result;
  54. struct timeval last_dev_result; // array > 0
  55. struct timeval last_nonce_result; // > 0 nonce
  56. // Info
  57. char getinfo[(BFLSC_BUFSIZ+4)*4];
  58. char *firmware;
  59. int engines; // each engine represents a 'thread' in a chip
  60. char *xlink_mode;
  61. char *xlink_present;
  62. // Status
  63. bool dead; // TODO: handle seperate x-link devices failing?
  64. bool overheat;
  65. // Stats
  66. float temp1;
  67. float temp2;
  68. float vcc1;
  69. float vcc2;
  70. float vmain;
  71. float temp1_max;
  72. float temp2_max;
  73. time_t temp1_max_time;
  74. time_t temp2_max_time;
  75. float temp1_5min_av; // TODO:
  76. float temp2_5min_av; // TODO:
  77. // To handle the fact that flushing the queue may not remove all work
  78. // (normally one item is still being processed)
  79. // and also that once the queue is flushed, results may still be in
  80. // the output queue - but we don't want to process them at the time of doing an LP
  81. // when result_id > flush_id+1, flushed work can be discarded since it
  82. // is no longer in the device
  83. uint64_t flush_id; // counter when results were last flushed
  84. uint64_t result_id; // counter when results were last checked
  85. bool flushed; // are any flushed?
  86. };
  87. struct bflsc_info {
  88. pthread_rwlock_t stat_lock;
  89. struct thr_info results_thr;
  90. uint64_t hashes_sent;
  91. uint32_t update_count;
  92. struct timeval last_update;
  93. int sc_count;
  94. struct bflsc_dev *sc_devs;
  95. unsigned int scan_sleep_time;
  96. unsigned int results_sleep_time;
  97. unsigned int default_ms_work;
  98. bool shutdown;
  99. bool flash_led;
  100. bool not_first_work; // allow ignoring the first nonce error
  101. };
  102. #define BFLSC_XLINKHDR '@'
  103. #define BFLSC_MAXPAYLOAD 255
  104. struct DataForwardToChain {
  105. uint8_t header;
  106. uint8_t deviceAddress;
  107. uint8_t payloadSize;
  108. uint8_t payloadData[BFLSC_MAXPAYLOAD];
  109. };
  110. #define DATAFORWARDSIZE(data) (1 + 1 + 1 + data.payloadSize)
  111. #define MIDSTATE_BYTES 32
  112. #define MERKLE_OFFSET 64
  113. #define MERKLE_BYTES 12
  114. #define BFLSC_QJOBSIZ (MIDSTATE_BYTES+MERKLE_BYTES+1)
  115. #define BFLSC_EOB 0xaa
  116. struct QueueJobStructure {
  117. uint8_t payloadSize;
  118. uint8_t midState[MIDSTATE_BYTES];
  119. uint8_t blockData[MERKLE_BYTES];
  120. uint8_t endOfBlock;
  121. };
  122. #define QUE_RES_LINES_MIN 3
  123. #define QUE_MIDSTATE 0
  124. #define QUE_BLOCKDATA 1
  125. #define QUE_NONCECOUNT 2
  126. #define QUE_FLD_MIN 3
  127. #define QUE_FLD_MAX 11
  128. #define BFLSC_SIGNATURE 0xc1
  129. #define BFLSC_EOW 0xfe
  130. // N.B. this will only work with 5 jobs
  131. // requires a different jobs[N] for each job count
  132. // but really only need to handle 5 anyway
  133. struct QueueJobPackStructure {
  134. uint8_t payloadSize;
  135. uint8_t signature;
  136. uint8_t jobsInArray;
  137. struct QueueJobStructure jobs[5];
  138. uint8_t endOfWrapper;
  139. };
  140. // TODO: Implement in API and also in usb device selection
  141. struct SaveString {
  142. uint8_t payloadSize;
  143. uint8_t payloadData[BFLSC_MAXPAYLOAD];
  144. };
  145. // Commands
  146. #define BFLSC_IDENTIFY "ZGX"
  147. #define BFLSC_IDENTIFY_LEN (sizeof(BFLSC_IDENTIFY)-1)
  148. #define BFLSC_DETAILS "ZCX"
  149. #define BFLSC_DETAILS_LEN (sizeof(BFLSC_DETAILS)-1)
  150. #define BFLSC_FIRMWARE "ZJX"
  151. #define BFLSC_FIRMWARE_LEN (sizeof(BFLSC_FIRMWARE)-1)
  152. #define BFLSC_FLASH "ZMX"
  153. #define BFLSC_FLASH_LEN (sizeof(BFLSC_FLASH)-1)
  154. #define BFLSC_VOLTAGE "ZTX"
  155. #define BFLSC_VOLTAGE_LEN (sizeof(BFLSC_VOLTAGE)-1)
  156. #define BFLSC_TEMPERATURE "ZLX"
  157. #define BFLSC_TEMPERATURE_LEN (sizeof(BFLSC_TEMPERATURE)-1)
  158. #define BFLSC_QJOB "ZNX"
  159. #define BFLSC_QJOB_LEN (sizeof(BFLSC_QJOB)-1)
  160. #define BFLSC_QJOBS "ZWX"
  161. #define BFLSC_QJOBS_LEN (sizeof(BFLSC_QJOBS)-1)
  162. #define BFLSC_QRES "ZOX"
  163. #define BFLSC_QRES_LEN (sizeof(BFLSC_QRES)-1)
  164. #define BFLSC_QFLUSH "ZQX"
  165. #define BFLSC_QFLUSH_LEN (sizeof(BFLSC_QFLUSH)-1)
  166. #define BFLSC_FANAUTO "Z5X"
  167. #define BFLSC_FANOUT_LEN (sizeof(BFLSC_FANAUTO)-1)
  168. #define BFLSC_FAN0 "Z0X"
  169. #define BFLSC_FAN0_LEN (sizeof(BFLSC_FAN0)-1)
  170. #define BFLSC_FAN1 "Z1X"
  171. #define BFLSC_FAN1_LEN (sizeof(BFLSC_FAN1)-1)
  172. #define BFLSC_FAN2 "Z2X"
  173. #define BFLSC_FAN2_LEN (sizeof(BFLSC_FAN2)-1)
  174. #define BFLSC_FAN3 "Z3X"
  175. #define BFLSC_FAN3_LEN (sizeof(BFLSC_FAN3)-1)
  176. #define BFLSC_FAN4 "Z4X"
  177. #define BFLSC_FAN4_LEN (sizeof(BFLSC_FAN4)-1)
  178. #define BFLSC_SAVESTR "ZSX"
  179. #define BFLSC_SAVESTR_LEN (sizeof(BFLSC_SAVESTR)-1)
  180. #define BFLSC_LOADSTR "ZUX"
  181. #define BFLSC_LOADSTR_LEN (sizeof(BFLSC_LOADSTR)-1)
  182. // Replies
  183. #define BFLSC_IDENTITY "BitFORCE SC"
  184. #define BFLSC_BFLSC "SHA256 SC"
  185. #define BFLSC_OK "OK\n"
  186. #define BFLSC_OK_LEN (sizeof(BFLSC_OK)-1)
  187. #define BFLSC_SUCCESS "SUCCESS\n"
  188. #define BFLSC_SUCCESS_LEN (sizeof(BFLSC_SUCCESS)-1)
  189. #define BFLSC_RESULT "COUNT:"
  190. #define BFLSC_RESULT_LEN (sizeof(BFLSC_RESULT)-1)
  191. #define BFLSC_ANERR "ERR:"
  192. #define BFLSC_ANERR_LEN (sizeof(BFLSC_ANERR)-1)
  193. #define BFLSC_TIMEOUT BFLSC_ANERR "TIMEOUT"
  194. #define BFLSC_TIMEOUT_LEN (sizeof(BFLSC_TIMEOUT)-1)
  195. #define BFLSC_INVALID BFLSC_ANERR "INVALID DATA"
  196. #define BFLSC_INVALID_LEN (sizeof(BFLSC_INVALID)-1)
  197. #define BFLSC_ERRSIG BFLSC_ANERR "SIGNATURE"
  198. #define BFLSC_ERRSIG_LEN (sizeof(BFLSC_ERRSIG)-1)
  199. #define BFLSC_OKQ "OK:QUEUED"
  200. #define BFLSC_OKQ_LEN (sizeof(BFLSC_OKQ)-1)
  201. // Followed by N=1..5
  202. #define BFLSC_OKQN "OK:QUEUED "
  203. #define BFLSC_OKQN_LEN (sizeof(BFLSC_OKQN)-1)
  204. #define BFLSC_QFULL "QUEUE FULL"
  205. #define BFLSC_QFULL_LEN (sizeof(BFLSC_QFULL)-1)
  206. #define BFLSC_HITEMP "HIGH TEMPERATURE RECOVERY"
  207. #define BFLSC_HITEMP_LEN (sizeof(BFLSC_HITEMP)-1)
  208. #define BFLSC_EMPTYSTR "MEMORY EMPTY"
  209. #define BFLSC_EMPTYSTR_LEN (sizeof(BFLSC_EMPTYSTR)-1)
  210. // Queued and non-queued are the same
  211. #define FullNonceRangeJob QueueJobStructure
  212. #define BFLSC_JOBSIZ BFLSC_QJOBSIZ
  213. // Non queued commands
  214. #define BFLSC_SENDWORK "ZDX"
  215. #define BFLSC_SENDWORK_LEN (sizeof(BFLSC_SENDWORK)-1)
  216. // Non queued commands (not used)
  217. #define BFLSC_WORKSTATUS "ZFX"
  218. #define BFLSC_WORKSTATUS_LEN (sizeof(BFLSC_WORKSTATUS)-1)
  219. #define BFLSC_SENDRANGE "ZPX"
  220. #define BFLSC_SENDRANGE_LEN (sizeof(BFLSC_SENDRANGE)-1)
  221. // Non queued work replies (not used)
  222. #define BFLSC_NONCE "NONCE-FOUND:"
  223. #define BFLSC_NONCE_LEN (sizeof(BFLSC_NONCE)-1)
  224. #define BFLSC_NO_NONCE "NO-NONCE"
  225. #define BFLSC_NO_NONCE_LEN (sizeof(BFLSC_NO_NONCE)-1)
  226. #define BFLSC_IDLE "IDLE"
  227. #define BFLSC_IDLE_LEN (sizeof(BFLSC_IDLE)-1)
  228. #define BFLSC_BUSY "BUSY"
  229. #define BFLSC_BUSY_LEN (sizeof(BFLSC_BUSY)-1)
  230. #define BFLSC_MINIRIG "BAM"
  231. #define BFLSC_SINGLE "BAS"
  232. #define BFLSC_LITTLESINGLE "BAL"
  233. #define BFLSC_JALAPENO "BAJ"
  234. // Default expected time for a nonce range
  235. // - thus no need to check until this + last time work was found
  236. // 60GH/s MiniRig (1 board) or Single
  237. #define BAM_WORK_TIME 71.58
  238. #define BAS_WORK_TIME 71.58
  239. // 30GH/s Little Single
  240. #define BAL_WORK_TIME 143.17
  241. // 4.5GH/s Jalapeno
  242. #define BAJ_WORK_TIME 954.44
  243. // Defaults (slightly over half the work time) but ensure none are above 100
  244. // SCAN_TIME - delay after sending work
  245. // RES_TIME - delay between checking for results
  246. #define BAM_SCAN_TIME 20
  247. #define BAM_RES_TIME 2
  248. #define BAS_SCAN_TIME 360
  249. #define BAS_RES_TIME 36
  250. #define BAL_SCAN_TIME 720
  251. #define BAL_RES_TIME 72
  252. #define BAJ_SCAN_TIME 1000
  253. #define BAJ_RES_TIME 100
  254. #define BFLSC_MAX_SLEEP 2000
  255. #define BFLSC_TEMP_SLEEPMS 5
  256. #define BFLSC_QUE_SIZE 20
  257. #define BFLSC_QUE_FULL_ENOUGH 13
  258. #define BFLSC_QUE_WATERMARK 6
  259. // Must drop this far below cutoff before resuming work
  260. #define BFLSC_TEMP_RECOVER 5
  261. // If initialisation fails the first time,
  262. // sleep this amount (ms) and try again
  263. #define REINIT_TIME_FIRST_MS 100
  264. // Max ms per sleep
  265. #define REINIT_TIME_MAX_MS 800
  266. // Keep trying up to this many us
  267. #define REINIT_TIME_MAX 3000000
  268. static const char *blank = "";
  269. struct device_drv bflsc_drv;
  270. static void xlinkstr(char *xlink, int dev, struct bflsc_info *sc_info)
  271. {
  272. if (dev > 0)
  273. sprintf(xlink, " x-%d", dev);
  274. else {
  275. if (sc_info->sc_count > 1)
  276. strcpy(xlink, " master");
  277. else
  278. *xlink = '\0';
  279. }
  280. }
  281. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  282. {
  283. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  284. char xlink[17];
  285. xlinkstr(xlink, dev, sc_info);
  286. usb_applog(bflsc, cmd, xlink, amount, err);
  287. }
  288. // Break an input up into lines with LFs removed
  289. // false means an error, but if *lines > 0 then data was also found
  290. // error would be no data or missing LF at the end
  291. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  292. {
  293. bool ok = true;
  294. char *ptr;
  295. #define p_lines (*lines)
  296. #define p_items (*items)
  297. p_lines = 0;
  298. p_items = NULL;
  299. if (!buf || !(*buf)) {
  300. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  301. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  302. return false;
  303. }
  304. ptr = strdup(buf);
  305. while (ptr && *ptr) {
  306. p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
  307. if (unlikely(!p_items))
  308. quit(1, "Failed to realloc p_items in tolines");
  309. p_items[p_lines-1] = ptr;
  310. ptr = strchr(ptr, '\n');
  311. if (ptr)
  312. *(ptr++) = '\0';
  313. else {
  314. if (ok) {
  315. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  316. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  317. }
  318. ok = false;
  319. }
  320. }
  321. return ok;
  322. }
  323. static void freetolines(int *lines, char ***items)
  324. {
  325. if (*lines > 0) {
  326. free(**items);
  327. free(*items);
  328. }
  329. *lines = 0;
  330. *items = NULL;
  331. }
  332. enum breakmode {
  333. NOCOLON,
  334. ONECOLON,
  335. ALLCOLON // Temperature uses this
  336. };
  337. // Break down a single line into 'fields'
  338. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  339. // firstname will be the allocated buf copy pointer which is also
  340. // the string before ':' for ONECOLON and ALLCOLON
  341. // If any string is missing the ':' when it was expected, false is returned
  342. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  343. {
  344. char *ptr, *colon, *comma;
  345. bool ok;
  346. #define p_count (*count)
  347. #define p_firstname (*firstname)
  348. #define p_fields (*fields)
  349. #define p_lf (*lf)
  350. p_count = 0;
  351. p_firstname = NULL;
  352. p_fields = NULL;
  353. p_lf = NULL;
  354. if (!buf || !(*buf))
  355. return false;
  356. ptr = p_firstname = strdup(buf);
  357. p_lf = strchr(p_firstname, '\n');
  358. if (mode == ONECOLON) {
  359. colon = strchr(ptr, ':');
  360. if (colon) {
  361. ptr = colon;
  362. *(ptr++) = '\0';
  363. } else
  364. ok = false;
  365. }
  366. while (*ptr == ' ')
  367. ptr++;
  368. ok = true;
  369. while (ptr && *ptr) {
  370. if (mode == ALLCOLON) {
  371. colon = strchr(ptr, ':');
  372. if (colon)
  373. ptr = colon + 1;
  374. else
  375. ok = false;
  376. }
  377. while (*ptr == ' ')
  378. ptr++;
  379. comma = strchr(ptr, ',');
  380. if (comma)
  381. *(comma++) = '\0';
  382. p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
  383. if (unlikely(!p_fields))
  384. quit(1, "Failed to realloc p_fields in breakdown");
  385. p_fields[p_count-1] = ptr;
  386. ptr = comma;
  387. }
  388. return ok;
  389. }
  390. static void freebreakdown(int *count, char **firstname, char ***fields)
  391. {
  392. if (*firstname)
  393. free(*firstname);
  394. if (*count > 0)
  395. free(*fields);
  396. *count = 0;
  397. *firstname = NULL;
  398. *fields = NULL;
  399. }
  400. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  401. {
  402. struct DataForwardToChain data;
  403. int len;
  404. if (dev == 0)
  405. return usb_write(bflsc, buf, buflen, amount, cmd);
  406. data.header = BFLSC_XLINKHDR;
  407. data.deviceAddress = (uint8_t)dev;
  408. data.payloadSize = buflen;
  409. memcpy(data.payloadData, buf, buflen);
  410. len = DATAFORWARDSIZE(data);
  411. // TODO: handle xlink timeout message - here or at call?
  412. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  413. }
  414. static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount)
  415. {
  416. char buf[BFLSC_BUFSIZ+1];
  417. *err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
  418. if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
  419. return false;
  420. else
  421. return true;
  422. }
  423. static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
  424. {
  425. *err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
  426. if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
  427. return false;
  428. else {
  429. if (*amount > (int)BFLSC_ANERR_LEN && strncmp(buf, BFLSC_ANERR, BFLSC_ANERR_LEN) == 0)
  430. return false;
  431. else
  432. return true;
  433. }
  434. }
  435. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  436. {
  437. int err, amount;
  438. // Device is gone
  439. if (bflsc->usbinfo.nodev)
  440. return;
  441. mutex_lock(&bflsc->device_mutex);
  442. err = write_to_dev(bflsc, dev, BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, &amount, C_QUEFLUSH);
  443. if (err < 0 || amount != BFLSC_QFLUSH_LEN) {
  444. mutex_unlock(&bflsc->device_mutex);
  445. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  446. } else {
  447. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  448. err = getok(bflsc, C_QUEFLUSHREPLY, &err, &amount);
  449. mutex_unlock(&bflsc->device_mutex);
  450. // TODO: report an error if not 'OK' ?
  451. }
  452. }
  453. /* return True = attempted usb_read_ok()
  454. * set ignore to true means no applog/ignore errors */
  455. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  456. {
  457. bool readok = false;
  458. mutex_lock(&(bflsc->device_mutex));
  459. *err = write_to_dev(bflsc, dev, BFLSC_QRES, BFLSC_QRES_LEN, amount, C_REQUESTRESULTS);
  460. if (*err < 0 || *amount != BFLSC_QRES_LEN) {
  461. mutex_unlock(&(bflsc->device_mutex));
  462. if (!ignore)
  463. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  464. // TODO: do what? flag as dead device?
  465. // count how many times it has happened and reset/fail it
  466. // or even make sure it is all x-link and that means device
  467. // has failed after some limit of this?
  468. // of course all other I/O must also be failing ...
  469. } else {
  470. readok = true;
  471. *err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
  472. mutex_unlock(&(bflsc->device_mutex));
  473. if (*err < 0 || *amount < 1) {
  474. if (!ignore)
  475. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  476. // TODO: do what? ... see above
  477. }
  478. }
  479. return readok;
  480. }
  481. static void __bflsc_initialise(struct cgpu_info *bflsc)
  482. {
  483. int err;
  484. // TODO: does x-link bypass the other device FTDI? (I think it does)
  485. // So no initialisation required except for the master device?
  486. if (bflsc->usbinfo.nodev)
  487. return;
  488. // Reset
  489. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  490. FTDI_VALUE_RESET, bflsc->usbdev->found->interface, C_RESET);
  491. applog(LOG_DEBUG, "%s%i: reset got err %d",
  492. bflsc->drv->name, bflsc->device_id, err);
  493. if (bflsc->usbinfo.nodev)
  494. return;
  495. // Set data control
  496. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  497. FTDI_VALUE_DATA_BAS, bflsc->usbdev->found->interface, C_SETDATA);
  498. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  499. bflsc->drv->name, bflsc->device_id, err);
  500. if (bflsc->usbinfo.nodev)
  501. return;
  502. // Set the baud
  503. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
  504. (FTDI_INDEX_BAUD_BAS & 0xff00) | bflsc->usbdev->found->interface,
  505. C_SETBAUD);
  506. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  507. bflsc->drv->name, bflsc->device_id, err);
  508. if (bflsc->usbinfo.nodev)
  509. return;
  510. // Set Flow Control
  511. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  512. FTDI_VALUE_FLOW, bflsc->usbdev->found->interface, C_SETFLOW);
  513. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  514. bflsc->drv->name, bflsc->device_id, err);
  515. if (bflsc->usbinfo.nodev)
  516. return;
  517. // Set Modem Control
  518. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  519. FTDI_VALUE_MODEM, bflsc->usbdev->found->interface, C_SETMODEM);
  520. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  521. bflsc->drv->name, bflsc->device_id, err);
  522. if (bflsc->usbinfo.nodev)
  523. return;
  524. // Clear any sent data
  525. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  526. FTDI_VALUE_PURGE_TX, bflsc->usbdev->found->interface, C_PURGETX);
  527. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  528. bflsc->drv->name, bflsc->device_id, err);
  529. if (bflsc->usbinfo.nodev)
  530. return;
  531. // Clear any received data
  532. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  533. FTDI_VALUE_PURGE_RX, bflsc->usbdev->found->interface, C_PURGERX);
  534. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  535. bflsc->drv->name, bflsc->device_id, err);
  536. }
  537. static void bflsc_initialise(struct cgpu_info *bflsc)
  538. {
  539. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  540. char buf[BFLSC_BUFSIZ+1];
  541. int err, amount;
  542. int dev;
  543. mutex_lock(&(bflsc->device_mutex));
  544. __bflsc_initialise(bflsc);
  545. mutex_unlock(&(bflsc->device_mutex));
  546. for (dev = 0; dev < sc_info->sc_count; dev++) {
  547. bflsc_send_flush_work(bflsc, dev);
  548. bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
  549. }
  550. }
  551. static bool getinfo(struct cgpu_info *bflsc, int dev)
  552. {
  553. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  554. struct bflsc_dev sc_dev;
  555. char buf[BFLSC_BUFSIZ+1];
  556. int err, amount;
  557. char **items, *firstname, **fields, *lf;
  558. int i, lines, count;
  559. bool res, ok;
  560. char *tmp;
  561. /*
  562. * Kano's first dev Jalapeno output:
  563. * DEVICE: BitFORCE SC<LF>
  564. * FIRMWARE: 1.0.0<LF>
  565. * ENGINES: 30<LF>
  566. * FREQUENCY: [UNKNOWN]<LF>
  567. * XLINK MODE: MASTER<LF>
  568. * XLINK PRESENT: YES<LF>
  569. * --DEVICES IN CHAIN: 0<LF>
  570. * --CHAIN PRESENCE MASK: 00000000<LF>
  571. * OK<LF>
  572. */
  573. // TODO: if dev is ever > 0 must handle xlink timeout message
  574. err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
  575. if (err < 0 || amount != BFLSC_DETAILS_LEN) {
  576. applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
  577. bflsc->drv->dname, bflsc->device_path, amount, err);
  578. return false;
  579. }
  580. err = usb_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
  581. if (err < 0 || amount < 1) {
  582. if (err < 0) {
  583. applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
  584. bflsc->drv->dname, bflsc->device_path, amount, err);
  585. } else {
  586. applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
  587. bflsc->drv->dname, bflsc->device_path, amount, err);
  588. }
  589. return false;
  590. }
  591. memset(&sc_dev, 0, sizeof(struct bflsc_dev));
  592. sc_info->sc_count = 1;
  593. res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
  594. if (!res)
  595. return false;
  596. tmp = str_text(buf);
  597. strcpy(sc_dev.getinfo, tmp);
  598. free(tmp);
  599. for (i = 0; i < lines-2; i++) {
  600. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  601. if (lf)
  602. *lf = '\0';
  603. if (!res || count != 1) {
  604. tmp = str_text(items[i]);
  605. applog(LOG_WARNING, "%s detect (%s) invalid details line: '%s' %d",
  606. bflsc->drv->dname, bflsc->device_path, tmp, count);
  607. free(tmp);
  608. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  609. goto mata;
  610. }
  611. if (strcmp(firstname, BFLSC_DI_FIRMWARE) == 0) {
  612. sc_dev.firmware = strdup(fields[0]);
  613. if (strcmp(sc_dev.firmware, "1.0.0")) {
  614. tmp = str_text(items[i]);
  615. applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s'",
  616. bflsc->drv->dname, bflsc->device_path, tmp);
  617. free(tmp);
  618. }
  619. }
  620. else if (strcmp(firstname, BFLSC_DI_ENGINES) == 0) {
  621. sc_dev.engines = atoi(fields[0]);
  622. if (sc_dev.engines < 1) {
  623. tmp = str_text(items[i]);
  624. applog(LOG_WARNING, "%s detect (%s) invalid engine count: '%s'",
  625. bflsc->drv->dname, bflsc->device_path, tmp);
  626. free(tmp);
  627. goto mata;
  628. }
  629. }
  630. else if (strcmp(firstname, BFLSC_DI_XLINKMODE) == 0)
  631. sc_dev.xlink_mode = strdup(fields[0]);
  632. else if (strcmp(firstname, BFLSC_DI_XLINKPRESENT) == 0)
  633. sc_dev.xlink_present = strdup(fields[0]);
  634. else if (strcmp(firstname, BFLSC_DI_DEVICESINCHAIN) == 0) {
  635. sc_info->sc_count = atoi(fields[0]) + 1;
  636. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  637. tmp = str_text(items[i]);
  638. applog(LOG_WARNING, "%s detect (%s) invalid s-link count: '%s'",
  639. bflsc->drv->dname, bflsc->device_path, tmp);
  640. free(tmp);
  641. goto mata;
  642. }
  643. }
  644. freebreakdown(&count, &firstname, &fields);
  645. }
  646. sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
  647. if (unlikely(!sc_info->sc_devs))
  648. quit(1, "Failed to calloc in getinfo");
  649. memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
  650. // TODO: do we care about getting this info for the rest if > 0 x-link
  651. ok = true;
  652. goto ne;
  653. mata:
  654. freebreakdown(&count, &firstname, &fields);
  655. ok = false;
  656. ne:
  657. freetolines(&lines, &items);
  658. return ok;
  659. }
  660. static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  661. {
  662. struct bflsc_info *sc_info = NULL;
  663. char buf[BFLSC_BUFSIZ+1];
  664. char devpath[20];
  665. int i, err, amount;
  666. struct timeval init_start, init_now;
  667. int init_sleep, init_count;
  668. bool ident_first;
  669. char *newname;
  670. struct cgpu_info *bflsc = calloc(1, sizeof(*bflsc));
  671. if (unlikely(!bflsc))
  672. quit(1, "Failed to calloc bflsc in bflsc_detect_one");
  673. bflsc->drv = &bflsc_drv;
  674. bflsc->deven = DEV_ENABLED;
  675. bflsc->threads = 1;
  676. sc_info = calloc(1, sizeof(*sc_info));
  677. if (unlikely(!sc_info))
  678. quit(1, "Failed to calloc sc_info in bflsc_detect_one");
  679. // TODO: fix ... everywhere ...
  680. bflsc->device_data = (FILE *)sc_info;
  681. if (!usb_init(bflsc, dev, found))
  682. goto shin;
  683. sprintf(devpath, "%d:%d",
  684. (int)(bflsc->usbinfo.bus_number),
  685. (int)(bflsc->usbinfo.device_address));
  686. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  687. ident_first = true;
  688. retry:
  689. init_count = 0;
  690. init_sleep = REINIT_TIME_FIRST_MS;
  691. cgtime(&init_start);
  692. reinit:
  693. __bflsc_initialise(bflsc);
  694. err = write_to_dev(bflsc, 0, BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY);
  695. if (err < 0 || amount != BFLSC_IDENTIFY_LEN) {
  696. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  697. bflsc->drv->dname, devpath, amount, err);
  698. goto unshin;
  699. }
  700. err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
  701. if (err < 0 || amount < 1) {
  702. init_count++;
  703. cgtime(&init_now);
  704. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  705. if (init_count == 2) {
  706. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  707. bflsc->drv->dname, devpath, amount, err);
  708. }
  709. nmsleep(init_sleep);
  710. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  711. init_sleep *= 2;
  712. goto reinit;
  713. }
  714. if (init_count > 0)
  715. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  716. bflsc->drv->dname, devpath, init_count, tdiff(&init_now, &init_start));
  717. if (err < 0) {
  718. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  719. bflsc->drv->dname, devpath, amount, err);
  720. } else {
  721. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  722. bflsc->drv->dname, devpath, amount);
  723. }
  724. goto unshin;
  725. }
  726. buf[amount] = '\0';
  727. if (unlikely(!strstr(buf, BFLSC_BFLSC))) {
  728. applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
  729. bflsc->drv->dname, devpath, buf);
  730. goto unshin;
  731. }
  732. if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
  733. if (ident_first) {
  734. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
  735. bflsc->drv->dname, devpath, buf);
  736. ident_first = false;
  737. goto retry;
  738. }
  739. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  740. bflsc->drv->dname, devpath, buf);
  741. goto unshin;
  742. }
  743. bflsc->device_path = strdup(devpath);
  744. if (!getinfo(bflsc, 0))
  745. goto unshin;
  746. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  747. sc_info->results_sleep_time = BAS_RES_TIME;
  748. sc_info->default_ms_work = BAS_WORK_TIME;
  749. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  750. * use 'freq * engines' to estimate.
  751. * Otherwise for now: */
  752. newname = NULL;
  753. if (sc_info->sc_count > 1) {
  754. newname = BFLSC_MINIRIG;
  755. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  756. sc_info->results_sleep_time = BAM_RES_TIME;
  757. sc_info->default_ms_work = BAM_WORK_TIME;
  758. bflsc->usbdev->ident = IDENT_BAM;
  759. } else {
  760. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  761. newname = BFLSC_JALAPENO;
  762. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  763. sc_info->results_sleep_time = BAJ_RES_TIME;
  764. sc_info->default_ms_work = BAJ_WORK_TIME;
  765. bflsc->usbdev->ident = IDENT_BAJ;
  766. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  767. newname = BFLSC_LITTLESINGLE;
  768. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  769. sc_info->results_sleep_time = BAL_RES_TIME;
  770. sc_info->default_ms_work = BAL_WORK_TIME;
  771. bflsc->usbdev->ident = IDENT_BAL;
  772. }
  773. }
  774. for (i = 0; i < sc_info->sc_count; i++)
  775. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  776. if (newname) {
  777. if (!bflsc->drv->copy)
  778. bflsc->drv = copy_drv(bflsc->drv);
  779. bflsc->drv->name = newname;
  780. }
  781. // We have a real BFLSC!
  782. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  783. bflsc->drv->dname, devpath, bflsc->drv->name);
  784. if (!add_cgpu(bflsc))
  785. goto unshin;
  786. update_usb_stats(bflsc);
  787. mutex_init(&bflsc->device_mutex);
  788. rwlock_init(&sc_info->stat_lock);
  789. return true;
  790. unshin:
  791. usb_uninit(bflsc);
  792. shin:
  793. free(bflsc->device_path);
  794. free(bflsc->device_data);
  795. if (bflsc->name != blank)
  796. free(bflsc->name);
  797. if (bflsc->drv->copy)
  798. free(bflsc->drv);
  799. free(bflsc);
  800. return false;
  801. }
  802. static void bflsc_detect(void)
  803. {
  804. usb_detect(&bflsc_drv, bflsc_detect_one);
  805. }
  806. static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
  807. {
  808. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  809. float temp = 0;
  810. float vcc1 = 0;
  811. int i;
  812. rd_lock(&(sc_info->stat_lock));
  813. for (i = 0; i < sc_info->sc_count; i++) {
  814. if (sc_info->sc_devs[i].temp1 > temp)
  815. temp = sc_info->sc_devs[i].temp1;
  816. if (sc_info->sc_devs[i].temp2 > temp)
  817. temp = sc_info->sc_devs[i].temp2;
  818. if (sc_info->sc_devs[i].vcc1 > vcc1)
  819. vcc1 = sc_info->sc_devs[i].vcc1;
  820. }
  821. rd_unlock(&(sc_info->stat_lock));
  822. tailsprintf(buf, " max%3.0fC %4.2fV | ", temp, vcc1);
  823. }
  824. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  825. {
  826. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  827. struct work *work, *tmp;
  828. bool did = false;
  829. bflsc_send_flush_work(bflsc, dev);
  830. rd_lock(&bflsc->qlock);
  831. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  832. if (work->queued && work->subid == dev) {
  833. // devflag is used to flag stale work
  834. work->devflag = true;
  835. did = true;
  836. }
  837. }
  838. rd_unlock(&bflsc->qlock);
  839. if (did) {
  840. wr_lock(&(sc_info->stat_lock));
  841. sc_info->sc_devs[dev].flushed = true;
  842. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  843. sc_info->sc_devs[dev].work_queued = 0;
  844. wr_unlock(&(sc_info->stat_lock));
  845. }
  846. }
  847. static void bflsc_flush_work(struct cgpu_info *bflsc)
  848. {
  849. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  850. int dev;
  851. for (dev = 0; dev < sc_info->sc_count; dev++)
  852. flush_one_dev(bflsc, dev);
  853. }
  854. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  855. {
  856. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  857. int err, amount;
  858. // Device is gone
  859. if (bflsc->usbinfo.nodev)
  860. return;
  861. // It is not critical flashing the led so don't get stuck if we
  862. // can't grab the mutex now
  863. if (mutex_trylock(&bflsc->device_mutex))
  864. return;
  865. err = write_to_dev(bflsc, dev, BFLSC_FLASH, BFLSC_FLASH_LEN, &amount, C_REQUESTFLASH);
  866. if (err < 0 || amount != BFLSC_FLASH_LEN) {
  867. mutex_unlock(&(bflsc->device_mutex));
  868. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  869. } else {
  870. getok(bflsc, C_FLASHREPLY, &err, &amount);
  871. mutex_unlock(&(bflsc->device_mutex));
  872. }
  873. // Once we've tried - don't do it until told to again
  874. // - even if it failed
  875. sc_info->flash_led = false;
  876. return;
  877. }
  878. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  879. {
  880. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  881. struct bflsc_dev *sc_dev;
  882. char temp_buf[BFLSC_BUFSIZ+1];
  883. char volt_buf[BFLSC_BUFSIZ+1];
  884. char *tmp;
  885. int err, amount;
  886. char *firstname, **fields, *lf;
  887. char xlink[17];
  888. int count;
  889. bool res;
  890. float temp, temp1, temp2;
  891. float vcc1, vcc2, vmain;
  892. // Device is gone
  893. if (bflsc->usbinfo.nodev)
  894. return false;
  895. if (dev >= sc_info->sc_count) {
  896. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  897. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  898. return false;
  899. }
  900. // Flash instead of Temp
  901. if (sc_info->flash_led) {
  902. bflsc_flash_led(bflsc, dev);
  903. return true;
  904. }
  905. /* It is not very critical getting temp so don't get stuck if we
  906. * can't grab the mutex here */
  907. if (mutex_trylock(&bflsc->device_mutex))
  908. return false;
  909. xlinkstr(&(xlink[0]), dev, sc_info);
  910. err = write_to_dev(bflsc, dev, BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE);
  911. if (err < 0 || amount != BFLSC_TEMPERATURE_LEN) {
  912. mutex_unlock(&(bflsc->device_mutex));
  913. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  914. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  915. return false;
  916. }
  917. err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
  918. if (err < 0 || amount < 1) {
  919. mutex_unlock(&(bflsc->device_mutex));
  920. if (err < 0) {
  921. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  922. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  923. } else {
  924. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  925. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  926. }
  927. return false;
  928. }
  929. // N.B. we only get the voltages if the temp succeeds - temp is the important one
  930. err = write_to_dev(bflsc, dev, BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, &amount, C_REQUESTVOLTS);
  931. if (err < 0 || amount != BFLSC_VOLTAGE_LEN) {
  932. mutex_unlock(&(bflsc->device_mutex));
  933. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  934. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  935. return false;
  936. }
  937. err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
  938. if (err < 0 || amount < 1) {
  939. mutex_unlock(&(bflsc->device_mutex));
  940. if (err < 0) {
  941. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  942. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  943. } else {
  944. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  945. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  946. }
  947. return false;
  948. }
  949. mutex_unlock(&(bflsc->device_mutex));
  950. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  951. if (lf)
  952. *lf = '\0';
  953. if (!res || count != 2 || !lf) {
  954. tmp = str_text(temp_buf);
  955. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  956. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  957. free(tmp);
  958. freebreakdown(&count, &firstname, &fields);
  959. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  960. return false;
  961. }
  962. temp = temp1 = (float)atoi(fields[0]);
  963. temp2 = (float)atoi(fields[1]);
  964. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  965. if (lf)
  966. *lf = '\0';
  967. if (!res || count != 3 || !lf) {
  968. tmp = str_text(volt_buf);
  969. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  970. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  971. free(tmp);
  972. freebreakdown(&count, &firstname, &fields);
  973. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  974. return false;
  975. }
  976. sc_dev = &sc_info->sc_devs[dev];
  977. vcc1 = (float)atoi(fields[0]) / 1000.0;
  978. vcc2 = (float)atoi(fields[1]) / 1000.0;
  979. vmain = (float)atoi(fields[2]) / 1000.0;
  980. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  981. wr_lock(&(sc_info->stat_lock));
  982. if (vcc1 > 0) {
  983. if (unlikely(sc_dev->vcc1 == 0))
  984. sc_dev->vcc1 = vcc1;
  985. else {
  986. sc_dev->vcc1 += vcc1 * 0.63;
  987. sc_dev->vcc1 /= 1.63;
  988. }
  989. }
  990. if (vcc2 > 0) {
  991. if (unlikely(sc_dev->vcc2 == 0))
  992. sc_dev->vcc2 = vcc2;
  993. else {
  994. sc_dev->vcc2 += vcc2 * 0.63;
  995. sc_dev->vcc2 /= 1.63;
  996. }
  997. }
  998. if (vmain > 0) {
  999. if (unlikely(sc_dev->vmain == 0))
  1000. sc_dev->vmain = vmain;
  1001. else {
  1002. sc_dev->vmain += vmain * 0.63;
  1003. sc_dev->vmain /= 1.63;
  1004. }
  1005. }
  1006. wr_unlock(&(sc_info->stat_lock));
  1007. }
  1008. if (temp1 > 0 || temp2 > 0) {
  1009. wr_lock(&(sc_info->stat_lock));
  1010. if (unlikely(!sc_dev->temp1))
  1011. sc_dev->temp1 = temp1;
  1012. else {
  1013. sc_dev->temp1 += temp1 * 0.63;
  1014. sc_dev->temp1 /= 1.63;
  1015. }
  1016. if (unlikely(!sc_dev->temp2))
  1017. sc_dev->temp2 = temp2;
  1018. else {
  1019. sc_dev->temp2 += temp2 * 0.63;
  1020. sc_dev->temp2 /= 1.63;
  1021. }
  1022. if (temp1 > sc_dev->temp1_max) {
  1023. sc_dev->temp1_max = temp1;
  1024. sc_dev->temp1_max_time = time(NULL);
  1025. }
  1026. if (temp2 > sc_dev->temp2_max) {
  1027. sc_dev->temp2_max = temp2;
  1028. sc_dev->temp2_max_time = time(NULL);
  1029. }
  1030. if (unlikely(sc_dev->temp1_5min_av == 0))
  1031. sc_dev->temp1_5min_av = temp1;
  1032. else {
  1033. sc_dev->temp1_5min_av += temp1 * .0042;
  1034. sc_dev->temp1_5min_av /= 1.0042;
  1035. }
  1036. if (unlikely(sc_dev->temp2_5min_av == 0))
  1037. sc_dev->temp2_5min_av = temp2;
  1038. else {
  1039. sc_dev->temp2_5min_av += temp2 * .0042;
  1040. sc_dev->temp2_5min_av /= 1.0042;
  1041. }
  1042. wr_unlock(&(sc_info->stat_lock));
  1043. if (temp < temp2)
  1044. temp = temp2;
  1045. bflsc->temp = temp;
  1046. if (bflsc->cutofftemp > 0 && temp > bflsc->cutofftemp) {
  1047. applog(LOG_WARNING, "%s%i:%s temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  1048. bflsc->drv->name, bflsc->device_id, xlink,
  1049. temp, bflsc->cutofftemp);
  1050. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  1051. sc_dev->overheat = true;
  1052. flush_one_dev(bflsc, dev);
  1053. return false;
  1054. }
  1055. if (bflsc->cutofftemp > 0 && temp < (bflsc->cutofftemp - BFLSC_TEMP_RECOVER))
  1056. sc_dev->overheat = false;
  1057. }
  1058. freebreakdown(&count, &firstname, &fields);
  1059. return true;
  1060. }
  1061. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1062. {
  1063. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1064. char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
  1065. struct work *work;
  1066. uint32_t nonce;
  1067. int i, num;
  1068. bool res;
  1069. char *tmp;
  1070. if (count < QUE_FLD_MIN) {
  1071. tmp = str_text(data);
  1072. applog(LOG_ERR, "%s%i:%s work returned too small (%d,%s)",
  1073. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1074. free(tmp);
  1075. inc_hw_errors(bflsc->thr[0]);
  1076. return;
  1077. }
  1078. if (count > QUE_FLD_MAX) {
  1079. applog(LOG_ERR, "%s%i:%s work returned too large (%d) processing %d anyway",
  1080. bflsc->drv->name, bflsc->device_id, xlink, count, QUE_FLD_MAX);
  1081. count = QUE_FLD_MAX;
  1082. inc_hw_errors(bflsc->thr[0]);
  1083. }
  1084. num = atoi(fields[QUE_NONCECOUNT]);
  1085. if (num != count - QUE_FLD_MIN) {
  1086. tmp = str_text(data);
  1087. applog(LOG_ERR, "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1088. bflsc->drv->name, bflsc->device_id, xlink, num, count - QUE_FLD_MAX, tmp);
  1089. free(tmp);
  1090. inc_hw_errors(bflsc->thr[0]);
  1091. }
  1092. memset(midstate, 0, MIDSTATE_BYTES);
  1093. memset(blockdata, 0, MERKLE_BYTES);
  1094. hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES);
  1095. hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES);
  1096. work = find_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1097. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1098. if (!work) {
  1099. if (sc_info->not_first_work) {
  1100. applog(LOG_ERR, "%s%i:%s failed to find nonce work - can't be processed - ignored",
  1101. bflsc->drv->name, bflsc->device_id, xlink);
  1102. inc_hw_errors(bflsc->thr[0]);
  1103. }
  1104. return;
  1105. }
  1106. res = false;
  1107. for (i = QUE_FLD_MIN; i < count; i++) {
  1108. if (strlen(fields[i]) != 8) {
  1109. tmp = str_text(data);
  1110. applog(LOG_ERR, "%s%i:%s invalid nonce (%s) will try to process anyway",
  1111. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1112. free(tmp);
  1113. }
  1114. hex2bin((void*)&nonce, fields[i], 4);
  1115. nonce = htobe32(nonce);
  1116. wr_lock(&(sc_info->stat_lock));
  1117. sc_info->sc_devs[dev].nonces_found++;
  1118. wr_unlock(&(sc_info->stat_lock));
  1119. submit_nonce(bflsc->thr[0], work, nonce);
  1120. (*nonces)++;
  1121. res = true;
  1122. }
  1123. wr_lock(&(sc_info->stat_lock));
  1124. if (res)
  1125. sc_info->sc_devs[dev].result_id++;
  1126. sc_info->sc_devs[dev].work_complete++;
  1127. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1128. // If not flushed (stale)
  1129. if (!(work->devflag))
  1130. sc_info->sc_devs[dev].work_queued -= 1;
  1131. wr_unlock(&(sc_info->stat_lock));
  1132. work_completed(bflsc, work);
  1133. }
  1134. static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces)
  1135. {
  1136. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1137. char **items, *firstname, **fields, *lf;
  1138. int que, i, lines, count;
  1139. char xlink[17];
  1140. char *tmp, *tmp2;
  1141. *nonces = 0;
  1142. xlinkstr(&(xlink[0]), dev, sc_info);
  1143. tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1144. if (lines < 1) {
  1145. tmp = str_text(buf);
  1146. applog(LOG_ERR, "%s%i:%s empty result (%s) ignored",
  1147. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1148. free(tmp);
  1149. que = 0;
  1150. goto arigatou;
  1151. }
  1152. if (lines < QUE_RES_LINES_MIN) {
  1153. tmp = str_text(buf);
  1154. applog(LOG_ERR, "%s%i:%s result too small (%s) ignored",
  1155. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1156. free(tmp);
  1157. que = 0;
  1158. goto arigatou;
  1159. }
  1160. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1161. if (count < 1) {
  1162. tmp = str_text(buf);
  1163. tmp2 = str_text(items[1]);
  1164. applog(LOG_ERR, "%s%i:%s empty result count (%s) in (%s) will try anyway",
  1165. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1166. free(tmp2);
  1167. free(tmp);
  1168. } else if (count != 1) {
  1169. tmp = str_text(buf);
  1170. tmp2 = str_text(items[1]);
  1171. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1172. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1173. free(tmp2);
  1174. free(tmp);
  1175. }
  1176. que = atoi(fields[0]);
  1177. if (que != (lines - QUE_RES_LINES_MIN)) {
  1178. i = que;
  1179. // 1+ In case the last line isn't 'OK' - try to process it
  1180. que = 1 + lines - QUE_RES_LINES_MIN;
  1181. tmp = str_text(buf);
  1182. tmp2 = str_text(items[0]);
  1183. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1184. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1185. free(tmp2);
  1186. free(tmp);
  1187. }
  1188. freebreakdown(&count, &firstname, &fields);
  1189. for (i = 0; i < que; i++) {
  1190. breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1191. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1192. freebreakdown(&count, &firstname, &fields);
  1193. sc_info->not_first_work = true;
  1194. }
  1195. arigatou:
  1196. freetolines(&lines, &items);
  1197. return que;
  1198. }
  1199. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1200. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1201. // Thread to simply keep looking for results
  1202. static void *bflsc_get_results(void *userdata)
  1203. {
  1204. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1205. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1206. struct timeval elapsed, now;
  1207. float oldest, f;
  1208. char buf[BFLSC_BUFSIZ+1];
  1209. int err, amount;
  1210. int i, que, dev, nonces;
  1211. bool readok;
  1212. cgtime(&now);
  1213. for (i = 0; i < sc_info->sc_count; i++) {
  1214. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1215. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1216. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1217. }
  1218. while (sc_info->shutdown == false) {
  1219. if (bflsc->usbinfo.nodev)
  1220. return NULL;
  1221. dev = -1;
  1222. oldest = FLT_MAX;
  1223. cgtime(&now);
  1224. // Find the first oldest ... that also needs checking
  1225. for (i = 0; i < sc_info->sc_count; i++) {
  1226. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1227. f = TVFMS(&elapsed);
  1228. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1229. f = oldest;
  1230. dev = i;
  1231. }
  1232. }
  1233. if (bflsc->usbinfo.nodev)
  1234. return NULL;
  1235. if (dev == -1)
  1236. goto utsura;
  1237. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1238. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1239. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1240. // TODO: do what else?
  1241. } else {
  1242. que = process_results(bflsc, dev, buf, &nonces);
  1243. sc_info->not_first_work = true; // in case it failed processing it
  1244. if (que > 0)
  1245. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1246. if (nonces > 0)
  1247. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1248. // TODO: if not getting results ... reinit?
  1249. }
  1250. utsura:
  1251. nmsleep(sc_info->results_sleep_time);
  1252. }
  1253. return NULL;
  1254. }
  1255. static bool bflsc_thread_prepare(struct thr_info *thr)
  1256. {
  1257. struct cgpu_info *bflsc = thr->cgpu;
  1258. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1259. struct timeval now;
  1260. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1261. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1262. return false;
  1263. }
  1264. pthread_detach(sc_info->results_thr.pth);
  1265. cgtime(&now);
  1266. get_datestamp(bflsc->init, &now);
  1267. return true;
  1268. }
  1269. static void bflsc_shutdown(struct thr_info *thr)
  1270. {
  1271. struct cgpu_info *bflsc = thr->cgpu;
  1272. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1273. bflsc_flush_work(bflsc);
  1274. sc_info->shutdown = true;
  1275. }
  1276. static void bflsc_thread_enable(struct thr_info *thr)
  1277. {
  1278. struct cgpu_info *bflsc = thr->cgpu;
  1279. if (bflsc->usbinfo.nodev)
  1280. return;
  1281. bflsc_initialise(bflsc);
  1282. }
  1283. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work)
  1284. {
  1285. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1286. struct FullNonceRangeJob data;
  1287. char buf[BFLSC_BUFSIZ+1];
  1288. int err, amount;
  1289. int len;
  1290. int try;
  1291. // Device is gone
  1292. if (bflsc->usbinfo.nodev)
  1293. return false;
  1294. // TODO: handle this everywhere
  1295. if (sc_info->sc_devs[dev].overheat == true)
  1296. return false;
  1297. // Initially code only deals with sending one work item
  1298. data.payloadSize = BFLSC_JOBSIZ;
  1299. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1300. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1301. data.endOfBlock = BFLSC_EOB;
  1302. try = 0;
  1303. mutex_lock(&(bflsc->device_mutex));
  1304. re_send:
  1305. err = write_to_dev(bflsc, dev, BFLSC_QJOB, BFLSC_QJOB_LEN, &amount, C_REQUESTQUEJOB);
  1306. if (err < 0 || amount != BFLSC_QJOB_LEN) {
  1307. mutex_unlock(&(bflsc->device_mutex));
  1308. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1309. return false;
  1310. }
  1311. if (!getok(bflsc, C_REQUESTQUEJOBSTATUS, &err, &amount)) {
  1312. mutex_unlock(&(bflsc->device_mutex));
  1313. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1314. return false;
  1315. }
  1316. len = sizeof(struct FullNonceRangeJob);
  1317. err = write_to_dev(bflsc, dev, (char *)&data, len, &amount, C_QUEJOB);
  1318. if (err < 0 || amount != len) {
  1319. mutex_unlock(&(bflsc->device_mutex));
  1320. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1321. return false;
  1322. }
  1323. if (!getokerr(bflsc, C_QUEJOBSTATUS, &err, &amount, buf, sizeof(buf))) {
  1324. // TODO: check for QUEUE FULL and set work_queued to BFLSC_QUE_SIZE
  1325. // and report a code bug LOG_ERR - coz it should never happen
  1326. // Try twice
  1327. if (try++ < 1 && amount > 1 &&
  1328. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1329. goto re_send;
  1330. mutex_unlock(&(bflsc->device_mutex));
  1331. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1332. return false;
  1333. }
  1334. mutex_unlock(&(bflsc->device_mutex));
  1335. wr_lock(&(sc_info->stat_lock));
  1336. sc_info->sc_devs[dev].work_queued++;
  1337. wr_unlock(&(sc_info->stat_lock));
  1338. work->subid = dev;
  1339. return true;
  1340. }
  1341. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1342. {
  1343. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1344. struct work *work = NULL;
  1345. int i, dev, tried, que;
  1346. bool ret = false;
  1347. int tries = 0;
  1348. tried = -1;
  1349. // if something is wrong with a device try the next one available
  1350. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1351. while (++tries < 3) {
  1352. // Device is gone - shouldn't normally get here
  1353. if (bflsc->usbinfo.nodev) {
  1354. ret = true;
  1355. break;
  1356. }
  1357. dev = -1;
  1358. rd_lock(&(sc_info->stat_lock));
  1359. // Anything waiting - gets the work first
  1360. for (i = 0; i < sc_info->sc_count; i++) {
  1361. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1362. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1363. !sc_info->sc_devs[i].overheat) {
  1364. dev = i;
  1365. break;
  1366. }
  1367. }
  1368. if (dev == -1) {
  1369. que = BFLSC_QUE_SIZE * 10; // 10x is certainly above the MAX it could be
  1370. // The first device with the smallest amount queued
  1371. for (i = 0; i < sc_info->sc_count; i++) {
  1372. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1373. !sc_info->sc_devs[i].overheat) {
  1374. dev = i;
  1375. que = sc_info->sc_devs[i].work_queued;
  1376. }
  1377. }
  1378. if (que > BFLSC_QUE_FULL_ENOUGH)
  1379. dev = -1;
  1380. }
  1381. rd_unlock(&(sc_info->stat_lock));
  1382. // nothing needs work yet
  1383. if (dev == -1) {
  1384. ret = true;
  1385. break;
  1386. }
  1387. if (!work)
  1388. work = get_queued(bflsc);
  1389. if (unlikely(!work))
  1390. break;
  1391. if (bflsc_send_work(bflsc, dev, work)) {
  1392. work = NULL;
  1393. break;
  1394. } else
  1395. tried = dev;
  1396. }
  1397. if (unlikely(work))
  1398. work_completed(bflsc, work);
  1399. return ret;
  1400. }
  1401. static int64_t bflsc_scanwork(struct thr_info *thr)
  1402. {
  1403. struct cgpu_info *bflsc = thr->cgpu;
  1404. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1405. int64_t ret, unsent;
  1406. bool flushed, cleanup;
  1407. struct work *work, *tmp;
  1408. int dev, waited, i;
  1409. // Device is gone
  1410. if (bflsc->usbinfo.nodev)
  1411. return -1;
  1412. flushed = false;
  1413. // Single lock check if any are flagged as flushed
  1414. rd_lock(&(sc_info->stat_lock));
  1415. for (dev = 0; dev < sc_info->sc_count; dev++)
  1416. flushed |= sc_info->sc_devs[dev].flushed;
  1417. rd_unlock(&(sc_info->stat_lock));
  1418. // > 0 flagged as flushed
  1419. if (flushed) {
  1420. // TODO: something like this ......
  1421. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1422. cleanup = false;
  1423. // Is there any flushed work that can be removed?
  1424. rd_lock(&(sc_info->stat_lock));
  1425. if (sc_info->sc_devs[dev].flushed) {
  1426. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + 1))
  1427. cleanup = true;
  1428. }
  1429. rd_unlock(&(sc_info->stat_lock));
  1430. // yes remove the flushed work that can be removed
  1431. if (cleanup) {
  1432. wr_lock(&bflsc->qlock);
  1433. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1434. if (work->devflag && work->subid == dev) {
  1435. bflsc->queued_count--;
  1436. HASH_DEL(bflsc->queued_work, work);
  1437. discard_work(work);
  1438. }
  1439. }
  1440. wr_unlock(&bflsc->qlock);
  1441. wr_lock(&(sc_info->stat_lock));
  1442. sc_info->sc_devs[dev].flushed = false;
  1443. wr_unlock(&(sc_info->stat_lock));
  1444. }
  1445. }
  1446. }
  1447. waited = restart_wait(sc_info->scan_sleep_time);
  1448. if (waited == ETIMEDOUT) {
  1449. unsigned int old_sleep_time, new_sleep_time = 0;
  1450. int min_queued = BFLSC_QUE_SIZE;
  1451. /* Only adjust the scan_sleep_time if we did not receive a
  1452. * restart message while waiting. Try to adjust sleep time
  1453. * so we drop to BFLSC_QUE_WATERMARK before getting more work.
  1454. */
  1455. rd_lock(&sc_info->stat_lock);
  1456. old_sleep_time = sc_info->scan_sleep_time;
  1457. for (i = 0; i < sc_info->sc_count; i++) {
  1458. if (sc_info->sc_devs[i].work_queued < min_queued)
  1459. min_queued = sc_info->sc_devs[i].work_queued;
  1460. }
  1461. rd_unlock(&sc_info->stat_lock);
  1462. new_sleep_time = old_sleep_time;
  1463. /* Increase slowly but decrease quickly */
  1464. if (min_queued > BFLSC_QUE_WATERMARK && old_sleep_time < BFLSC_MAX_SLEEP)
  1465. new_sleep_time = old_sleep_time * 21 / 20;
  1466. else if (min_queued < BFLSC_QUE_WATERMARK)
  1467. new_sleep_time = old_sleep_time * 2 / 3;
  1468. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1469. * report in at least 2 results per 5s log interval. */
  1470. if (new_sleep_time != old_sleep_time) {
  1471. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1472. new_sleep_time = BFLSC_MAX_SLEEP;
  1473. else if (new_sleep_time == 0)
  1474. new_sleep_time = 1;
  1475. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1476. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1477. wr_lock(&sc_info->stat_lock);
  1478. sc_info->scan_sleep_time = new_sleep_time;
  1479. wr_unlock(&sc_info->stat_lock);
  1480. }
  1481. }
  1482. // Count up the work done since we last were here
  1483. ret = 0;
  1484. wr_lock(&(sc_info->stat_lock));
  1485. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1486. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1487. sc_info->sc_devs[dev].hashes_unsent = 0;
  1488. sc_info->sc_devs[dev].hashes_sent += unsent;
  1489. sc_info->hashes_sent += unsent;
  1490. ret += unsent;
  1491. }
  1492. wr_unlock(&(sc_info->stat_lock));
  1493. return ret;
  1494. }
  1495. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1496. {
  1497. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1498. bool allok = true;
  1499. int i;
  1500. // Device is gone
  1501. if (bflsc->usbinfo.nodev)
  1502. return false;
  1503. for (i = 0; i < sc_info->sc_count; i++) {
  1504. if (!bflsc_get_temp(bflsc, i))
  1505. allok = false;
  1506. // Device is gone
  1507. if (bflsc->usbinfo.nodev)
  1508. return false;
  1509. if (i < (sc_info->sc_count - 1))
  1510. nmsleep(BFLSC_TEMP_SLEEPMS);
  1511. }
  1512. return allok;
  1513. }
  1514. static void bflsc_identify(struct cgpu_info *bflsc)
  1515. {
  1516. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1517. // TODO: handle x-link
  1518. sc_info->flash_led = true;
  1519. }
  1520. static bool bflsc_thread_init(struct thr_info *thr)
  1521. {
  1522. struct cgpu_info *bflsc = thr->cgpu;
  1523. if (bflsc->usbinfo.nodev)
  1524. return false;
  1525. bflsc_initialise(bflsc);
  1526. return true;
  1527. }
  1528. // there should be a new API function to return device info that isn't the standard stuff
  1529. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1530. // and the new one should be UNusual device stats/extra details - like the stuff below
  1531. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1532. {
  1533. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1534. struct api_data *root = NULL;
  1535. //if no x-link ... etc
  1536. rd_lock(&(sc_info->stat_lock));
  1537. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1538. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1539. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1540. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1541. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1542. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1543. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1544. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1545. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1546. rd_unlock(&(sc_info->stat_lock));
  1547. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1548. /*
  1549. else a whole lot of something like these ... etc
  1550. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1551. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1552. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1553. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1554. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1555. */
  1556. return root;
  1557. }
  1558. struct device_drv bflsc_drv = {
  1559. .drv_id = DRIVER_BFLSC,
  1560. .dname = "BitForceSC",
  1561. .name = BFLSC_SINGLE,
  1562. .drv_detect = bflsc_detect,
  1563. .get_api_stats = bflsc_api_stats,
  1564. .get_statline_before = get_bflsc_statline_before,
  1565. .get_stats = bflsc_get_stats,
  1566. .identify_device = bflsc_identify,
  1567. .thread_prepare = bflsc_thread_prepare,
  1568. .thread_init = bflsc_thread_init,
  1569. .hash_work = hash_queued_work,
  1570. .scanwork = bflsc_scanwork,
  1571. .queue_full = bflsc_queue_full,
  1572. .flush_work = bflsc_flush_work,
  1573. .thread_shutdown = bflsc_shutdown,
  1574. .thread_enable = bflsc_thread_enable
  1575. };