driver-bflsc.c 51 KB

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