driver-bflsc.c 50 KB

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