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