driver-bflsc.c 43 KB

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