driver-bflsc.c 43 KB

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