driver-bitforce.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 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 "config.h"
  11. #include <ctype.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <strings.h>
  19. #include <sys/time.h>
  20. #include <unistd.h>
  21. #include "compat.h"
  22. #include "deviceapi.h"
  23. #include "miner.h"
  24. #include "lowlevel.h"
  25. #include "lowl-pci.h"
  26. #include "lowl-vcom.h"
  27. #include "util.h"
  28. #define BFL_PCI_VENDOR_ID 0x1cf9
  29. #define BITFORCE_SLEEP_MS 500
  30. #define BITFORCE_VCOM_TIMEOUT_DSEC 250
  31. #define BITFORCE_VCOM_TIMEOUT_DSEC_ZCX 10
  32. #define BITFORCE_TIMEOUT_S 7
  33. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  34. #define BITFORCE_LONG_TIMEOUT_S 25
  35. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  36. #define BITFORCE_CHECK_INTERVAL_MS 10
  37. #define WORK_CHECK_INTERVAL_MS 50
  38. #define MAX_START_DELAY_MS 100
  39. #define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
  40. #define TIME_AVG_CONSTANT 8
  41. #define BITFORCE_QRESULT_LINE_LEN 165
  42. #define BITFORCE_MAX_QUEUED_MAX 40
  43. #define BITFORCE_MIN_QUEUED_MAX 10
  44. #define BITFORCE_MAX_QRESULTS 16
  45. #define BITFORCE_GOAL_QRESULTS 5
  46. #define BITFORCE_MIN_QRESULT_WAIT BITFORCE_CHECK_INTERVAL_MS
  47. #define BITFORCE_MAX_QRESULT_WAIT 1000
  48. #define BITFORCE_MAX_BQUEUE_AT_ONCE_65NM 5
  49. #define BITFORCE_MAX_BQUEUE_AT_ONCE_28NM 20
  50. enum bitforce_proto {
  51. BFP_WORK = 0,
  52. BFP_RANGE = 1,
  53. BFP_BQUEUE = 3,
  54. BFP_PQUEUE = 4,
  55. };
  56. static const char *protonames[] = {
  57. "full work",
  58. "nonce range",
  59. NULL,
  60. "bulk queue",
  61. "parallel queue",
  62. };
  63. BFG_REGISTER_DRIVER(bitforce_drv)
  64. BFG_REGISTER_DRIVER(bitforce_queue_api)
  65. static const struct bfg_set_device_definition bitforce_set_device_funcs[];
  66. enum bitforce_style {
  67. BFS_FPGA,
  68. BFS_65NM,
  69. BFS_28NM,
  70. };
  71. struct bitforce_lowl_interface {
  72. bool (*open)(struct cgpu_info *);
  73. void (*close)(struct cgpu_info *);
  74. void (*gets)(char *, size_t, struct cgpu_info *);
  75. ssize_t (*write)(struct cgpu_info *, const void *, ssize_t);
  76. bool (*set_timeout)(struct cgpu_info* , uint8_t);
  77. };
  78. struct bitforce_data {
  79. struct bitforce_lowl_interface *lowlif;
  80. bool is_open;
  81. struct lowl_pci_handle *lph;
  82. uint8_t lasttag;
  83. bytes_t getsbuf;
  84. int xlink_id;
  85. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  86. unsigned char *next_work_obs; // Start of data to send
  87. unsigned char next_work_obsz;
  88. const char *next_work_cmd;
  89. char noncebuf[14 + ((BITFORCE_MAX_QRESULTS+1) * BITFORCE_QRESULT_LINE_LEN)];
  90. int poll_func;
  91. enum bitforce_proto proto;
  92. enum bitforce_style style;
  93. int queued;
  94. int queued_max;
  95. int parallel;
  96. bool parallel_protocol;
  97. bool missing_zwx;
  98. bool already_have_results;
  99. bool just_flushed;
  100. int max_queue_at_once;
  101. int ready_to_queue;
  102. bool want_to_send_queue;
  103. unsigned result_busy_polled;
  104. unsigned sleep_ms_default;
  105. struct timeval tv_hashmeter_start;
  106. float temp[2];
  107. long *volts;
  108. int volts_count;
  109. bool probed;
  110. bool supports_fanspeed;
  111. };
  112. // Code must deal with a timeout
  113. static
  114. bool bitforce_vcom_open(struct cgpu_info * const dev)
  115. {
  116. struct bitforce_data * const devdata = dev->device_data;
  117. const char * const devpath = dev->device_path;
  118. dev->device_fd = serial_open(devpath, 0, BITFORCE_VCOM_TIMEOUT_DSEC, true);
  119. devdata->is_open = (dev->device_fd != -1);
  120. return devdata->is_open;
  121. }
  122. static
  123. void bitforce_vcom_close(struct cgpu_info * const dev)
  124. {
  125. struct bitforce_data * const devdata = dev->device_data;
  126. if (devdata->is_open)
  127. {
  128. serial_close(dev->device_fd);
  129. dev->device_fd = -1;
  130. devdata->is_open = false;
  131. }
  132. }
  133. static
  134. void bitforce_vcom_gets(char *buf, size_t bufLen, struct cgpu_info * const dev)
  135. {
  136. const int fd = dev->device_fd;
  137. while (likely(bufLen > 1 && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'))
  138. {}
  139. buf[0] = '\0';
  140. }
  141. static
  142. ssize_t bitforce_vcom_write(struct cgpu_info * const dev, const void *buf, ssize_t bufLen)
  143. {
  144. const int fd = dev->device_fd;
  145. if ((bufLen) != write(fd, buf, bufLen))
  146. return 0;
  147. else
  148. return bufLen;
  149. }
  150. static
  151. bool bitforce_vcom_set_timeout(struct cgpu_info * const dev, const uint8_t timeout)
  152. {
  153. const int fd = dev->device_fd;
  154. return vcom_set_timeout(fd, timeout);
  155. }
  156. static struct bitforce_lowl_interface bfllif_vcom = {
  157. .open = bitforce_vcom_open,
  158. .close = bitforce_vcom_close,
  159. .gets = bitforce_vcom_gets,
  160. .write = bitforce_vcom_write,
  161. .set_timeout = bitforce_vcom_set_timeout,
  162. };
  163. #ifdef NEED_BFG_LOWL_PCI
  164. static
  165. bool bitforce_pci_open(struct cgpu_info * const dev)
  166. {
  167. const char * const devpath = dev->device_path;
  168. struct bitforce_data * const devdata = dev->device_data;
  169. devdata->lph = lowl_pci_open(devpath, LP_BARINFO(
  170. LP_BAR(0, 0x1000, O_WRONLY),
  171. LP_BAR(1, 0x1000, O_RDONLY),
  172. LP_BAR(2, 0x80, O_RDWR),
  173. ));
  174. if (!devdata->lph)
  175. return false;
  176. devdata->lasttag = (lowl_pci_get_word(devdata->lph, 2, 2) >> 16) & 0xff;
  177. devdata->is_open = true;
  178. return devdata->is_open;
  179. }
  180. static
  181. void bitforce_pci_close(struct cgpu_info * const dev)
  182. {
  183. struct bitforce_data * const devdata = dev->device_data;
  184. if (devdata->is_open)
  185. {
  186. lowl_pci_close(devdata->lph);
  187. devdata->is_open = false;
  188. }
  189. }
  190. static
  191. void bitforce_pci_gets(char * const buf, size_t bufLen, struct cgpu_info * const dev)
  192. {
  193. struct bitforce_data * const devdata = dev->device_data;
  194. const uint32_t looking_for = (uint32_t)devdata->lasttag << 0x10;
  195. uint32_t resp;
  196. bytes_t *b = &devdata->getsbuf;
  197. if (!bytes_len(&devdata->getsbuf))
  198. {
  199. while (((resp = lowl_pci_get_word(devdata->lph, 2, 2)) & 0xff0000) != looking_for)
  200. cgsleep_ms(1);
  201. resp &= 0xffff;
  202. if (unlikely(resp > 0x1000))
  203. resp = 0x1000;
  204. void * const buf = bytes_preappend(b, resp + LOWL_PCI_GET_DATA_PADDING);
  205. if (lowl_pci_read_data(devdata->lph, buf, resp, 1, 0))
  206. bytes_postappend(b, resp);
  207. }
  208. ssize_t linelen = (bytes_find(b, '\n') + 1) ?: bytes_len(b);
  209. if (linelen > --bufLen)
  210. linelen = bufLen;
  211. memcpy(buf, bytes_buf(b), linelen);
  212. bytes_shift(b, linelen);
  213. buf[linelen] = '\0';
  214. }
  215. static
  216. ssize_t bitforce_pci_write(struct cgpu_info * const dev, const void * const bufp, ssize_t bufLen)
  217. {
  218. const uint8_t *buf = bufp;
  219. struct bitforce_data * const devdata = dev->device_data;
  220. if (unlikely(bufLen > 0x1000))
  221. return 0;
  222. if (!lowl_pci_set_data(devdata->lph, buf, bufLen, 0, 0))
  223. return 0;
  224. if (++devdata->lasttag == 0)
  225. ++devdata->lasttag;
  226. if (!lowl_pci_set_word(devdata->lph, 2, 0, ((uint32_t)devdata->lasttag << 0x10) | bufLen))
  227. return 0;
  228. return bufLen;
  229. }
  230. static struct bitforce_lowl_interface bfllif_pci = {
  231. .open = bitforce_pci_open,
  232. .close = bitforce_pci_close,
  233. .gets = bitforce_pci_gets,
  234. .write = bitforce_pci_write,
  235. };
  236. #endif
  237. static
  238. void bitforce_close(struct cgpu_info * const proc)
  239. {
  240. struct cgpu_info * const dev = proc->device;
  241. struct bitforce_data * const devdata = dev->device_data;
  242. if (devdata->is_open)
  243. devdata->lowlif->close(dev);
  244. }
  245. static
  246. bool bitforce_open(struct cgpu_info * const proc)
  247. {
  248. struct cgpu_info * const dev = proc->device;
  249. struct bitforce_data * const devdata = dev->device_data;
  250. bitforce_close(proc);
  251. return devdata->lowlif->open(dev);
  252. }
  253. static
  254. void bitforce_gets(char * const buf, const size_t bufLen, struct cgpu_info * const proc)
  255. {
  256. struct cgpu_info * const dev = proc->device;
  257. struct bitforce_data * const devdata = dev->device_data;
  258. if (likely(devdata->is_open))
  259. devdata->lowlif->gets(buf, bufLen, dev);
  260. else
  261. buf[0] = '\0';
  262. if (unlikely(opt_dev_protocol))
  263. applog(LOG_DEBUG, "DEVPROTO: %s: GETS: %s", dev->dev_repr, buf);
  264. }
  265. static
  266. ssize_t bitforce_write(struct cgpu_info * const proc, const void * const buf, const ssize_t bufLen)
  267. {
  268. struct cgpu_info * const dev = proc->device;
  269. struct bitforce_data * const devdata = dev->device_data;
  270. if (unlikely(!devdata->is_open))
  271. return 0;
  272. return devdata->lowlif->write(dev, buf, bufLen);
  273. }
  274. static ssize_t bitforce_send(struct cgpu_info * const proc, const void *buf, ssize_t bufLen)
  275. {
  276. struct bitforce_data * const data = proc->device_data;
  277. const int procid = data->xlink_id;
  278. if (!procid)
  279. return bitforce_write(proc, buf, bufLen);
  280. if (bufLen > 255)
  281. return -1;
  282. size_t bufLeft = bufLen + 3;
  283. char realbuf[bufLeft], *bufp;
  284. ssize_t rv;
  285. memcpy(&realbuf[3], buf, bufLen);
  286. realbuf[0] = '@';
  287. realbuf[1] = bufLen;
  288. realbuf[2] = procid;
  289. bufp = realbuf;
  290. do
  291. {
  292. rv = bitforce_write(proc, bufp, bufLeft);
  293. if (rv <= 0)
  294. return rv;
  295. bufLeft -= rv;
  296. }
  297. while (bufLeft > 0);
  298. return bufLen;
  299. }
  300. static
  301. void bitforce_cmd1b(struct cgpu_info * const proc, void *buf, size_t bufsz, const char *cmd, size_t cmdsz)
  302. {
  303. if (unlikely(opt_dev_protocol))
  304. applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD1: %s",
  305. proc->proc_repr, cmd);
  306. bitforce_send(proc, cmd, cmdsz);
  307. bitforce_gets(buf, bufsz, proc);
  308. }
  309. static
  310. void bitforce_cmd1c(struct cgpu_info * const proc, void *buf, size_t bufsz, void *cmd, size_t cmdsz)
  311. {
  312. if (unlikely(opt_dev_protocol))
  313. {
  314. char hex[(cmdsz * 2) + 1];
  315. bin2hex(hex, cmd, cmdsz);
  316. applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD1 HEX: %s",
  317. proc->proc_repr, hex);
  318. }
  319. bitforce_send(proc, cmd, cmdsz);
  320. bitforce_gets(buf, bufsz, proc);
  321. }
  322. static
  323. void bitforce_cmd2(struct cgpu_info * const proc, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  324. {
  325. bitforce_cmd1b(proc, buf, bufsz, cmd, 3);
  326. if (strncasecmp(buf, "OK", 2))
  327. return;
  328. if (unlikely(opt_dev_protocol))
  329. {
  330. char hex[(datasz * 2) + 1];
  331. bin2hex(hex, data, datasz);
  332. applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD2: %s",
  333. proc->proc_repr, hex);
  334. }
  335. bitforce_send(proc, data, datasz);
  336. bitforce_gets(buf, bufsz, proc);
  337. }
  338. static
  339. void bitforce_zgx(struct cgpu_info * const proc, void *buf, size_t bufsz)
  340. {
  341. struct cgpu_info * const dev = proc->device;
  342. struct bitforce_data * const devdata = dev->device_data;
  343. if (devdata->is_open && devdata->lowlif->set_timeout)
  344. {
  345. devdata->lowlif->set_timeout(dev, BITFORCE_VCOM_TIMEOUT_DSEC_ZCX);
  346. bitforce_cmd1b(proc, buf, bufsz, "ZGX", 3);
  347. devdata->lowlif->set_timeout(dev, BITFORCE_VCOM_TIMEOUT_DSEC);
  348. }
  349. else
  350. bitforce_cmd1b(proc, buf, bufsz, "ZGX", 3);
  351. }
  352. struct bitforce_init_data {
  353. struct bitforce_lowl_interface *lowlif;
  354. enum bitforce_style style;
  355. long devmask;
  356. int *parallels;
  357. };
  358. static
  359. int bitforce_chips_to_plan_for(int parallel, int chipcount) {
  360. if (parallel < 1)
  361. return parallel;
  362. if (chipcount > 15) return 32;
  363. if (chipcount > 7) return 16;
  364. if (chipcount > 3) return 8;
  365. if (chipcount > 1) return 4;
  366. if (chipcount ) return 2;
  367. return 1;
  368. }
  369. static
  370. bool bitforce_lowl_match(const struct lowlevel_device_info * const info)
  371. {
  372. #ifdef NEED_BFG_LOWL_PCI
  373. if (info->lowl == &lowl_pci)
  374. return info->vid == BFL_PCI_VENDOR_ID;
  375. #endif
  376. return lowlevel_match_product(info, "BitFORCE", "SHA256");
  377. }
  378. char *bitforce_copy_name(char * const pdevbuf)
  379. {
  380. char *s;
  381. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  382. {
  383. s[0] = '\0';
  384. s = strdup(&pdevbuf[7]);
  385. }
  386. else
  387. {
  388. for (s = &pdevbuf[strlen(pdevbuf)]; isspace(*--s); )
  389. *s = '\0';
  390. s = strdup(pdevbuf);
  391. }
  392. return s;
  393. }
  394. static
  395. bool bitforce_detect_oneof(const char * const devpath, struct bitforce_lowl_interface * const lowlif)
  396. {
  397. struct cgpu_info *bitforce;
  398. char pdevbuf[0x100];
  399. size_t pdevbuf_len;
  400. char *s;
  401. int procs = 1, parallel = -1;
  402. long maxchipno = 0;
  403. struct bitforce_init_data *initdata;
  404. char *manuf = NULL;
  405. struct bitforce_data dummy_bfdata = {
  406. .lowlif = lowlif,
  407. .xlink_id = 0,
  408. };
  409. struct cgpu_info dummy_cgpu = {
  410. .device = &dummy_cgpu,
  411. .dev_repr = "BFL",
  412. .proc_repr = "BFL",
  413. .device_path = devpath,
  414. .device_data = &dummy_bfdata,
  415. };
  416. dummy_cgpu.device_fd = -1;
  417. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  418. bitforce_open(&dummy_cgpu);
  419. if (unlikely(!dummy_bfdata.is_open)) {
  420. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  421. return false;
  422. }
  423. bitforce_zgx(&dummy_cgpu, pdevbuf, sizeof(pdevbuf));
  424. if (unlikely(!pdevbuf[0])) {
  425. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  426. bitforce_close(&dummy_cgpu);
  427. return 0;
  428. }
  429. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  430. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  431. bitforce_close(&dummy_cgpu);
  432. return false;
  433. }
  434. if (serial_claim_v(devpath, &bitforce_drv))
  435. {
  436. bitforce_close(&dummy_cgpu);
  437. return false;
  438. }
  439. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  440. s = bitforce_copy_name(pdevbuf);
  441. initdata = malloc(sizeof(*initdata));
  442. *initdata = (struct bitforce_init_data){
  443. .lowlif = lowlif,
  444. .style = BFS_FPGA,
  445. };
  446. bitforce_cmd1b(&dummy_cgpu, pdevbuf, sizeof(pdevbuf), "ZCX", 3);
  447. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  448. bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu);
  449. for ( ;
  450. strncasecmp(pdevbuf, "OK", 2);
  451. bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu) )
  452. {
  453. pdevbuf_len = strlen(pdevbuf);
  454. if (unlikely(!pdevbuf_len))
  455. continue;
  456. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  457. applog(LOG_DEBUG, " %s", pdevbuf);
  458. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  459. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  460. else
  461. if (!strncasecmp(pdevbuf, "CHANNEL", 7))
  462. maxchipno = max(maxchipno, atoi(&pdevbuf[7]));
  463. else
  464. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  465. procs = atoi(&pdevbuf[17]);
  466. else
  467. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  468. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  469. else
  470. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC") && initdata->style == BFS_FPGA)
  471. initdata->style = BFS_65NM;
  472. else
  473. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  474. parallel = atoi(&pdevbuf[27]);
  475. else
  476. if (!strncasecmp(pdevbuf, "ASIC CHANNELS:", 14))
  477. {
  478. parallel = atoi(&pdevbuf[14]);
  479. initdata->style = BFS_28NM;
  480. }
  481. else
  482. if (!strncasecmp(pdevbuf, "MANUFACTURER:", 13))
  483. {
  484. manuf = &pdevbuf[13];
  485. while (manuf[0] && isspace(manuf[0]))
  486. ++manuf;
  487. if (manuf[0])
  488. manuf = strdup(manuf);
  489. else
  490. manuf = NULL;
  491. }
  492. }
  493. parallel = bitforce_chips_to_plan_for(parallel, maxchipno);
  494. initdata->parallels = malloc(sizeof(initdata->parallels[0]) * procs);
  495. initdata->parallels[0] = parallel;
  496. parallel = abs(parallel);
  497. for (int proc = 1; proc < procs; ++proc)
  498. {
  499. applog(LOG_DEBUG, "Slave board %d:", proc);
  500. initdata->parallels[proc] = -1;
  501. maxchipno = 0;
  502. bitforce_cmd1b(&dummy_cgpu, pdevbuf, sizeof(pdevbuf), "ZCX", 3);
  503. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  504. bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu);
  505. for ( ;
  506. strncasecmp(pdevbuf, "OK", 2);
  507. bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu) )
  508. {
  509. pdevbuf_len = strlen(pdevbuf);
  510. if (unlikely(!pdevbuf_len))
  511. continue;
  512. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  513. applog(LOG_DEBUG, " %s", pdevbuf);
  514. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  515. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  516. else
  517. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  518. initdata->parallels[proc] = atoi(&pdevbuf[27]);
  519. }
  520. initdata->parallels[proc] = bitforce_chips_to_plan_for(initdata->parallels[proc], maxchipno);
  521. parallel += abs(initdata->parallels[proc]);
  522. }
  523. bitforce_close(&dummy_cgpu);
  524. if (unlikely((procs != 1 || parallel != 1) && initdata->style == BFS_FPGA))
  525. {
  526. // Only bitforce_queue supports parallelization and XLINK, so force SC mode and hope for the best
  527. applog(LOG_WARNING, "SC features detected with non-SC device; this is not supported!");
  528. initdata->style = BFS_65NM;
  529. }
  530. // We have a real BitForce!
  531. bitforce = calloc(1, sizeof(*bitforce));
  532. bitforce->drv = &bitforce_drv;
  533. if (initdata->style != BFS_FPGA)
  534. bitforce->drv = &bitforce_queue_api;
  535. bitforce->device_path = strdup(devpath);
  536. if (manuf)
  537. bitforce->dev_manufacturer = manuf;
  538. bitforce->deven = DEV_ENABLED;
  539. bitforce->procs = parallel;
  540. bitforce->threads = 1;
  541. if (initdata->style != BFS_FPGA)
  542. bitforce->cutofftemp = 85;
  543. if (s)
  544. bitforce->name = s;
  545. bitforce->device_data = initdata;
  546. // Skip fanspeed until we probe support for it
  547. bitforce->set_device_funcs = &bitforce_set_device_funcs[1];
  548. mutex_init(&bitforce->device_mutex);
  549. return add_cgpu(bitforce);
  550. }
  551. static
  552. bool bitforce_detect_one(const char * const devpath)
  553. {
  554. return bitforce_detect_oneof(devpath, &bfllif_vcom);
  555. }
  556. static
  557. bool bitforce_lowl_probe(const struct lowlevel_device_info * const info)
  558. {
  559. #ifdef NEED_BFG_LOWL_PCI
  560. if (info->lowl == &lowl_pci)
  561. return bitforce_detect_oneof(info->path, &bfllif_pci);
  562. #endif
  563. return vcom_lowl_probe_wrapper(info, bitforce_detect_one);
  564. }
  565. struct bitforce_proc_data {
  566. struct cgpu_info *cgpu;
  567. bool handles_board; // The first processor handles the queue for the entire board
  568. };
  569. static void bitforce_clear_buffer(struct cgpu_info *);
  570. static
  571. void bitforce_comm_error(struct thr_info *thr)
  572. {
  573. struct cgpu_info *bitforce = thr->cgpu;
  574. struct bitforce_data *data = bitforce->device_data;
  575. data->noncebuf[0] = '\0';
  576. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  577. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  578. inc_hw_errors_only(thr);
  579. if (!bitforce_open(bitforce))
  580. {
  581. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  582. return;
  583. }
  584. /* empty read buffer */
  585. bitforce_clear_buffer(bitforce);
  586. }
  587. static
  588. void __bitforce_clear_buffer(struct cgpu_info * const dev)
  589. {
  590. char pdevbuf[0x100];
  591. int count = 0;
  592. do {
  593. pdevbuf[0] = '\0';
  594. bitforce_gets(pdevbuf, sizeof(pdevbuf), dev);
  595. } while (pdevbuf[0] && (++count < 10));
  596. }
  597. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  598. {
  599. struct cgpu_info * const dev = bitforce->device;
  600. struct bitforce_data * const devdata = dev->device_data;
  601. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  602. mutex_lock(mutexp);
  603. if (devdata->is_open)
  604. {
  605. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  606. __bitforce_clear_buffer(bitforce);
  607. }
  608. mutex_unlock(mutexp);
  609. }
  610. void work_list_del(struct work **head, struct work *);
  611. void bitforce_reinit(struct cgpu_info *bitforce)
  612. {
  613. struct cgpu_info * const dev = bitforce->device;
  614. struct bitforce_data * const devdata = dev->device_data;
  615. struct bitforce_data *data = bitforce->device_data;
  616. struct thr_info *thr = bitforce->thr[0];
  617. struct bitforce_proc_data *procdata = thr->cgpu_data;
  618. const char *devpath = bitforce->device_path;
  619. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  620. int retries = 0;
  621. char pdevbuf[0x100];
  622. if (!procdata->handles_board)
  623. return;
  624. mutex_lock(mutexp);
  625. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  626. if (devdata->is_open)
  627. {
  628. bitforce_close(bitforce);
  629. cgsleep_ms(5000);
  630. }
  631. bitforce_open(bitforce);
  632. if (unlikely(!devdata->is_open)) {
  633. mutex_unlock(mutexp);
  634. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  635. return;
  636. }
  637. __bitforce_clear_buffer(bitforce);
  638. do {
  639. bitforce_zgx(bitforce, pdevbuf, sizeof(pdevbuf));
  640. if (unlikely(!pdevbuf[0])) {
  641. mutex_unlock(mutexp);
  642. bitforce_close(bitforce);
  643. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  644. return;
  645. }
  646. if (retries++)
  647. cgsleep_ms(10);
  648. } while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  649. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  650. mutex_unlock(mutexp);
  651. bitforce_close(bitforce);
  652. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  653. return;
  654. }
  655. free((void*)bitforce->name);
  656. bitforce->name = bitforce_copy_name(pdevbuf);
  657. bitforce->sleep_ms = data->sleep_ms_default;
  658. if (bitforce->drv == &bitforce_queue_api)
  659. {
  660. struct work *work, *tmp;
  661. timer_set_delay_from_now(&thr->tv_poll, 0);
  662. notifier_wake(thr->notifier);
  663. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZQX", 3);
  664. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  665. work_list_del(&thr->work_list, work);
  666. data->queued = 0;
  667. data->ready_to_queue = 0;
  668. data->already_have_results = false;
  669. data->just_flushed = true;
  670. thr->queue_full = false;
  671. }
  672. mutex_unlock(mutexp);
  673. }
  674. static void bitforce_flash_led(struct cgpu_info *bitforce)
  675. {
  676. struct cgpu_info * const dev = bitforce->device;
  677. struct bitforce_data * const devdata = dev->device_data;
  678. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  679. if (unlikely(!devdata->is_open))
  680. return;
  681. /* Do not try to flash the led if we're polling for a result to
  682. * minimise the chance of interleaved results */
  683. if (bitforce->polling)
  684. return;
  685. /* It is not critical flashing the led so don't get stuck if we
  686. * can't grab the mutex here */
  687. if (mutex_trylock(mutexp))
  688. return;
  689. char pdevbuf[0x100];
  690. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZMX", 3);
  691. /* Once we've tried - don't do it until told to again */
  692. bitforce->flash_led = false;
  693. /* However, this stops anything else getting a reply
  694. * So best to delay any other access to the BFL */
  695. cgsleep_ms(4000);
  696. mutex_unlock(mutexp);
  697. return; // nothing is returned by the BFL
  698. }
  699. static
  700. float my_strtof(const char *nptr, char **endptr)
  701. {
  702. float f = strtof(nptr, endptr);
  703. /* Cope with older software that breaks and reads nonsense
  704. * values */
  705. if (f > 100)
  706. f = strtod(nptr, endptr);
  707. return f;
  708. }
  709. static
  710. void set_float_if_gt_zero(float *var, float value)
  711. {
  712. if (value > 0)
  713. *var = value;
  714. }
  715. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  716. {
  717. struct cgpu_info * const dev = bitforce->device;
  718. struct bitforce_data * const devdata = dev->device_data;
  719. struct bitforce_data *data = bitforce->device_data;
  720. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  721. char pdevbuf[0x40];
  722. char voltbuf[0x40];
  723. char *s;
  724. struct cgpu_info *chip_cgpu;
  725. if (unlikely(!devdata->is_open))
  726. return false;
  727. /* Do not try to get the temperature if we're polling for a result to
  728. * minimise the chance of interleaved results */
  729. if (bitforce->polling)
  730. return true;
  731. // Flash instead of Temp - doing both can be too slow
  732. if (bitforce->flash_led) {
  733. bitforce_flash_led(bitforce);
  734. return true;
  735. }
  736. /* It is not critical getting temperature so don't get stuck if we
  737. * can't grab the mutex here */
  738. if (mutex_trylock(mutexp))
  739. return false;
  740. if (data->style != BFS_FPGA)
  741. {
  742. if (unlikely(!data->probed))
  743. {
  744. bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "Z9X", 3);
  745. if (strncasecmp(voltbuf, "ERR", 3))
  746. {
  747. data->supports_fanspeed = true;
  748. bitforce->set_device_funcs = bitforce_set_device_funcs;
  749. }
  750. data->probed = true;
  751. }
  752. bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "ZTX", 3);
  753. }
  754. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZLX", 3);
  755. mutex_unlock(mutexp);
  756. if (data->style != BFS_FPGA && likely(voltbuf[0]))
  757. {
  758. // Process voltage info
  759. // "NNNxxx,NNNxxx,NNNxxx"
  760. int n = 1;
  761. for (char *p = voltbuf; p[0]; ++p)
  762. if (p[0] == ',')
  763. ++n;
  764. long *out = malloc(sizeof(long) * n);
  765. if (!out)
  766. goto skipvolts;
  767. n = 0;
  768. char *saveptr, *v;
  769. for (v = strtok_r(voltbuf, ",", &saveptr); v; v = strtok_r(NULL, ",", &saveptr))
  770. out[n++] = strtol(v, NULL, 10);
  771. data->volts_count = 0;
  772. free(data->volts);
  773. data->volts = out;
  774. data->volts_count = n;
  775. }
  776. skipvolts:
  777. if (unlikely(!pdevbuf[0])) {
  778. struct thr_info *thr = bitforce->thr[0];
  779. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  780. inc_hw_errors_only(thr);
  781. return false;
  782. }
  783. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  784. float temp = my_strtof(s + 1, &s);
  785. set_float_if_gt_zero(&data->temp[0], temp);
  786. for ( ; s[0]; ++s)
  787. {
  788. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  789. {
  790. float temp2 = my_strtof(s + 1, &s);
  791. set_float_if_gt_zero(&data->temp[1], temp2);
  792. if (temp2 > temp)
  793. temp = temp2;
  794. }
  795. }
  796. if (temp > 0)
  797. {
  798. chip_cgpu = bitforce;
  799. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  800. chip_cgpu->temp = temp;
  801. }
  802. } else {
  803. struct thr_info *thr = bitforce->thr[0];
  804. /* Use the temperature monitor as a kind of watchdog for when
  805. * our responses are out of sync and flush the buffer to
  806. * hopefully recover */
  807. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  808. dev_error(bitforce, REASON_DEV_THROTTLE);
  809. /* Count throttling episodes as hardware errors */
  810. inc_hw_errors_only(thr);
  811. bitforce_clear_buffer(bitforce);
  812. return false;
  813. }
  814. return true;
  815. }
  816. static inline
  817. void dbg_block_data(struct cgpu_info *bitforce)
  818. {
  819. if (!opt_debug)
  820. return;
  821. struct bitforce_data *data = bitforce->device_data;
  822. char s[89];
  823. bin2hex(s, &data->next_work_ob[8], 44);
  824. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  825. }
  826. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  827. static
  828. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  829. {
  830. struct cgpu_info *bitforce = thr->cgpu;
  831. struct bitforce_data *data = bitforce->device_data;
  832. unsigned char *ob_ms = &data->next_work_ob[8];
  833. unsigned char *ob_dt = &ob_ms[32];
  834. // If polling job_start, cancel it
  835. if (data->poll_func == 1)
  836. {
  837. thr->tv_poll.tv_sec = -1;
  838. data->poll_func = 0;
  839. }
  840. memcpy(ob_ms, work->midstate, 32);
  841. memcpy(ob_dt, work->data + 64, 12);
  842. switch (data->proto)
  843. {
  844. case BFP_BQUEUE:
  845. quithere(1, "%"PRIpreprv": Impossible BFP_BQUEUE", bitforce->proc_repr);
  846. case BFP_PQUEUE:
  847. quithere(1, "%"PRIpreprv": Impossible BFP_PQUEUE", bitforce->proc_repr);
  848. case BFP_RANGE:
  849. {
  850. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  851. ob_nonce[0] = htobe32(work->blk.nonce);
  852. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  853. // FIXME: if nonce range fails... we didn't increment enough
  854. work->blk.nonce += bitforce->nonces + 1;
  855. break;
  856. }
  857. case BFP_WORK:
  858. work->blk.nonce = 0xffffffff;
  859. }
  860. return true;
  861. }
  862. static
  863. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  864. {
  865. struct bitforce_data *data = bitforce->device_data;
  866. if (data->proto == proto)
  867. return;
  868. if (data->proto == BFP_RANGE)
  869. {
  870. bitforce->nonces = 0xffffffff;
  871. bitforce->sleep_ms *= 5;
  872. data->sleep_ms_default *= 5;
  873. switch (proto)
  874. {
  875. case BFP_WORK:
  876. data->next_work_cmd = "ZDX";
  877. default:
  878. ;
  879. }
  880. if (data->style != BFS_FPGA)
  881. {
  882. // "S|---------- MidState ----------||-DataTail-|E"
  883. data->next_work_ob[7] = 45;
  884. data->next_work_ob[8+32+12] = '\xAA';
  885. data->next_work_obsz = 46;
  886. }
  887. else
  888. {
  889. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  890. memset(&data->next_work_ob[8+32+12], '>', 8);
  891. data->next_work_obsz = 60;
  892. }
  893. }
  894. else
  895. if (proto == BFP_RANGE)
  896. {
  897. /* Split work up into 1/5th nonce ranges */
  898. bitforce->nonces = 0x33333332;
  899. bitforce->sleep_ms /= 5;
  900. data->sleep_ms_default /= 5;
  901. data->next_work_cmd = "ZPX";
  902. if (data->style != BFS_FPGA)
  903. {
  904. data->next_work_ob[7] = 53;
  905. data->next_work_obsz = 54;
  906. }
  907. else
  908. data->next_work_obsz = 68;
  909. }
  910. data->proto = proto;
  911. bitforce->kname = protonames[proto];
  912. }
  913. static
  914. void bitforce_job_start(struct thr_info *thr)
  915. {
  916. struct cgpu_info *bitforce = thr->cgpu;
  917. struct cgpu_info * const dev = bitforce->device;
  918. struct bitforce_data * const devdata = dev->device_data;
  919. struct bitforce_data *data = bitforce->device_data;
  920. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  921. unsigned char *ob = data->next_work_obs;
  922. char pdevbuf[0x100];
  923. struct timeval tv_now;
  924. data->result_busy_polled = 0;
  925. if (data->queued)
  926. {
  927. uint32_t delay;
  928. // get_results collected more accurate job start time
  929. mt_job_transition(thr);
  930. job_start_complete(thr);
  931. data->queued = 0;
  932. delay = (uint32_t)bitforce->sleep_ms * 1000;
  933. if (unlikely(data->already_have_results))
  934. delay = 0;
  935. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  936. return;
  937. }
  938. if (unlikely(!devdata->is_open))
  939. goto commerr;
  940. re_send:
  941. mutex_lock(mutexp);
  942. bitforce_cmd2(bitforce, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  943. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  944. mutex_unlock(mutexp);
  945. cgtime(&tv_now);
  946. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  947. data->poll_func = 1;
  948. return;
  949. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  950. mutex_unlock(mutexp);
  951. switch (data->proto)
  952. {
  953. case BFP_RANGE:
  954. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  955. bitforce_change_mode(bitforce, BFP_WORK);
  956. goto re_send;
  957. default:
  958. ;
  959. }
  960. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  961. goto commerr;
  962. }
  963. mt_job_transition(thr);
  964. mutex_unlock(mutexp);
  965. dbg_block_data(bitforce);
  966. cgtime(&tv_now);
  967. bitforce->work_start_tv = tv_now;
  968. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  969. job_start_complete(thr);
  970. return;
  971. commerr:
  972. bitforce_comm_error(thr);
  973. job_start_abort(thr, true);
  974. }
  975. static char _discardedbuf[0x10];
  976. static
  977. int bitforce_zox(struct thr_info *thr, const char *cmd)
  978. {
  979. struct cgpu_info *bitforce = thr->cgpu;
  980. struct bitforce_data *data = bitforce->device_data;
  981. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  982. char *pdevbuf = &data->noncebuf[0];
  983. int count;
  984. mutex_lock(mutexp);
  985. bitforce_cmd1b(bitforce, pdevbuf, sizeof(data->noncebuf), cmd, 3);
  986. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  987. bitforce_gets(pdevbuf, sizeof(data->noncebuf), bitforce);
  988. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  989. {
  990. count = atoi(&pdevbuf[6]);
  991. size_t cls = strlen(pdevbuf);
  992. char *pmorebuf = &pdevbuf[cls];
  993. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  994. if (count && data->queued)
  995. cgtime(&bitforce->work_start_tv);
  996. while (true)
  997. {
  998. bitforce_gets(pmorebuf, szleft, bitforce);
  999. if (!strncasecmp(pmorebuf, "OK", 2))
  1000. {
  1001. pmorebuf[0] = '\0'; // process expects only results
  1002. break;
  1003. }
  1004. sz = strlen(pmorebuf);
  1005. if (!sz)
  1006. {
  1007. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  1008. break;
  1009. }
  1010. szleft -= sz;
  1011. pmorebuf += sz;
  1012. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  1013. {
  1014. // Out of buffer space somehow :(
  1015. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  1016. pmorebuf = _discardedbuf;
  1017. szleft = sizeof(_discardedbuf);
  1018. }
  1019. }
  1020. }
  1021. else
  1022. count = -1;
  1023. mutex_unlock(mutexp);
  1024. return count;
  1025. }
  1026. static inline char *next_line(char *);
  1027. static
  1028. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  1029. {
  1030. struct cgpu_info *bitforce = thr->cgpu;
  1031. struct cgpu_info * const dev = bitforce->device;
  1032. struct bitforce_data * const devdata = dev->device_data;
  1033. struct bitforce_data *data = bitforce->device_data;
  1034. unsigned int delay_time_ms;
  1035. struct timeval elapsed;
  1036. struct timeval now;
  1037. char *pdevbuf = &data->noncebuf[0];
  1038. bool stale;
  1039. int count;
  1040. cgtime(&now);
  1041. timersub(&now, &bitforce->work_start_tv, &elapsed);
  1042. bitforce->wait_ms = tv_to_ms(elapsed);
  1043. bitforce->polling = true;
  1044. if (unlikely(!devdata->is_open))
  1045. goto commerr;
  1046. stale = stale_work(work, true);
  1047. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  1048. {
  1049. // We're likely here because of a work restart
  1050. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  1051. if (!stale)
  1052. {
  1053. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  1054. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  1055. data->poll_func = 2;
  1056. return;
  1057. }
  1058. }
  1059. while (1) {
  1060. if (data->already_have_results)
  1061. {
  1062. data->already_have_results = false;
  1063. strcpy(pdevbuf, "COUNT:0");
  1064. count = 1;
  1065. break;
  1066. }
  1067. const char * const cmd = "ZFX";
  1068. count = bitforce_zox(thr, cmd);
  1069. cgtime(&now);
  1070. timersub(&now, &bitforce->work_start_tv, &elapsed);
  1071. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  1072. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  1073. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  1074. goto out;
  1075. }
  1076. if (count > 0)
  1077. {
  1078. // Check that queue results match the current work
  1079. // Also, if there are results from the next work, short-circuit this wait
  1080. unsigned char midstate[32], datatail[12];
  1081. char *p;
  1082. int i;
  1083. p = pdevbuf;
  1084. for (i = 0; i < count; ++i)
  1085. {
  1086. p = next_line(p);
  1087. hex2bin(midstate, p, 32);
  1088. hex2bin(datatail, &p[65], 12);
  1089. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  1090. break;
  1091. }
  1092. if (i == count)
  1093. {
  1094. // Didn't find the one we're waiting on
  1095. // Must be extra stuff in the queue results
  1096. char xmid[65];
  1097. char xdt[25];
  1098. bin2hex(xmid, work->midstate, 32);
  1099. bin2hex(xdt, &work->data[64], 12);
  1100. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  1101. bitforce->proc_repr, pdevbuf);
  1102. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  1103. bitforce->proc_repr, xmid, xdt);
  1104. count = 0;
  1105. }
  1106. else
  1107. if (i == count - 1)
  1108. // Last one found is what we're looking for
  1109. {}
  1110. else
  1111. // We finished the next job too!
  1112. data->already_have_results = true;
  1113. }
  1114. if (!count)
  1115. goto noqr;
  1116. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  1117. break;
  1118. data->result_busy_polled = bitforce->wait_ms;
  1119. if (stale)
  1120. {
  1121. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  1122. bitforce->proc_repr);
  1123. goto out;
  1124. }
  1125. noqr:
  1126. data->result_busy_polled = bitforce->wait_ms;
  1127. /* if BFL is throttling, no point checking so quickly */
  1128. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  1129. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  1130. data->poll_func = 2;
  1131. return;
  1132. }
  1133. if (count < 0 && pdevbuf[0] == 'N')
  1134. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  1135. // At this point, 'count' is:
  1136. // negative, in case of some kind of error
  1137. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  1138. // positive, if at least one job completed successfully
  1139. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  1140. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  1141. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  1142. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  1143. inc_hw_errors_only(thr);
  1144. /* If the device truly throttled, it didn't process the job and there
  1145. * are no results. But check first, just in case we're wrong about it
  1146. * throttling.
  1147. */
  1148. if (count > 0)
  1149. goto out;
  1150. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  1151. /* Simple timing adjustment. Allow a few polls to cope with
  1152. * OS timer delays being variably reliable. wait_ms will
  1153. * always equal sleep_ms when we've waited greater than or
  1154. * equal to the result return time.*/
  1155. delay_time_ms = bitforce->sleep_ms;
  1156. if (!data->result_busy_polled)
  1157. {
  1158. // No busy polls before results received
  1159. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  1160. // ... due to poll being rather late; ignore it as an anomaly
  1161. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  1162. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  1163. else
  1164. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  1165. {
  1166. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  1167. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  1168. bitforce->sleep_ms = data->sleep_ms_default;
  1169. }
  1170. else
  1171. {
  1172. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  1173. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  1174. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  1175. }
  1176. }
  1177. else
  1178. {
  1179. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  1180. {
  1181. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  1182. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  1183. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  1184. }
  1185. else
  1186. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  1187. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  1188. }
  1189. /* Work out the average time taken. Float for calculation, uint for display */
  1190. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  1191. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  1192. }
  1193. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  1194. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  1195. inc_hw_errors_only(thr);
  1196. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  1197. bitforce_clear_buffer(bitforce);
  1198. }
  1199. out:
  1200. bitforce->polling = false;
  1201. job_results_fetched(thr);
  1202. return;
  1203. commerr:
  1204. bitforce_comm_error(thr);
  1205. goto out;
  1206. }
  1207. static
  1208. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  1209. {
  1210. struct cgpu_info *bitforce = thr->cgpu;
  1211. struct bitforce_data *data = bitforce->device_data;
  1212. uint32_t nonce;
  1213. while (1) {
  1214. hex2bin((void*)&nonce, pnoncebuf, 4);
  1215. nonce = be32toh(nonce);
  1216. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  1217. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  1218. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  1219. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  1220. bitforce_change_mode(bitforce, BFP_WORK);
  1221. }
  1222. submit_nonce(thr, work, nonce);
  1223. if (strncmp(&pnoncebuf[8], ",", 1))
  1224. break;
  1225. pnoncebuf += 9;
  1226. }
  1227. }
  1228. static
  1229. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  1230. {
  1231. if (!work)
  1232. return false;
  1233. if (memcmp(work->midstate, midstate, 32))
  1234. return false;
  1235. if (memcmp(&work->data[64], datatail, 12))
  1236. return false;
  1237. char *end;
  1238. if (strtol(&buf[90], &end, 10))
  1239. bitforce_process_result_nonces(thr, work, &end[1]);
  1240. return true;
  1241. }
  1242. static
  1243. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  1244. {
  1245. struct cgpu_info *bitforce = thr->cgpu;
  1246. char midstate[32], datatail[12];
  1247. hex2bin((void*)midstate, buf, 32);
  1248. hex2bin((void*)datatail, &buf[65], 12);
  1249. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  1250. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  1251. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  1252. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  1253. {
  1254. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  1255. inc_hw_errors_only(thr);
  1256. }
  1257. }
  1258. static inline
  1259. char *next_line(char *in)
  1260. {
  1261. while (in[0] && (in++)[0] != '\n')
  1262. {}
  1263. return in;
  1264. }
  1265. static
  1266. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  1267. {
  1268. struct cgpu_info *bitforce = thr->cgpu;
  1269. struct bitforce_data *data = bitforce->device_data;
  1270. char *pnoncebuf = &data->noncebuf[0];
  1271. int count;
  1272. if (!strncasecmp(pnoncebuf, "NO-", 3))
  1273. return bitforce->nonces; /* No valid nonce found */
  1274. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  1275. {
  1276. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  1277. count = 1;
  1278. }
  1279. else
  1280. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  1281. {
  1282. count = 0;
  1283. pnoncebuf = next_line(pnoncebuf);
  1284. while (pnoncebuf[0])
  1285. {
  1286. bitforce_process_qresult_line(thr, pnoncebuf, work);
  1287. ++count;
  1288. pnoncebuf = next_line(pnoncebuf);
  1289. }
  1290. }
  1291. else
  1292. return 0;
  1293. // FIXME: This might have changed in the meantime (new job start, or broken)
  1294. return bitforce->nonces * count;
  1295. }
  1296. static void bitforce_shutdown(struct thr_info *thr)
  1297. {
  1298. struct cgpu_info *bitforce = thr->cgpu;
  1299. bitforce_close(bitforce);
  1300. }
  1301. static void biforce_thread_enable(struct thr_info *thr)
  1302. {
  1303. struct cgpu_info *bitforce = thr->cgpu;
  1304. bitforce_reinit(bitforce);
  1305. }
  1306. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  1307. {
  1308. struct bitforce_proc_data *procdata = bitforce->thr[0]->cgpu_data;
  1309. if (!procdata->handles_board)
  1310. return true;
  1311. return bitforce_get_temp(bitforce);
  1312. }
  1313. static bool bitforce_identify(struct cgpu_info *bitforce)
  1314. {
  1315. bitforce->flash_led = true;
  1316. return true;
  1317. }
  1318. static bool bitforce_thread_init(struct thr_info *thr)
  1319. {
  1320. struct cgpu_info *bitforce = thr->cgpu;
  1321. unsigned int wait;
  1322. struct bitforce_data *data;
  1323. struct bitforce_proc_data *procdata;
  1324. struct bitforce_init_data *initdata = bitforce->device_data;
  1325. const enum bitforce_style style = initdata->style;
  1326. int xlink_id = 0, boardno = 0;
  1327. struct bitforce_proc_data *first_on_this_board;
  1328. char buf[100];
  1329. for ( ; bitforce; bitforce = bitforce->next_proc)
  1330. {
  1331. thr = bitforce->thr[0];
  1332. if (unlikely(xlink_id > 30))
  1333. {
  1334. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  1335. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  1336. bitforce->reinit_backoff = 1e10;
  1337. continue;
  1338. }
  1339. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  1340. bitforce->device_data = data = malloc(sizeof(*data));
  1341. *data = (struct bitforce_data){
  1342. .lowlif = &bfllif_vcom,
  1343. .xlink_id = xlink_id,
  1344. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1345. .proto = BFP_RANGE,
  1346. .style = style,
  1347. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1348. .parallel = abs(initdata->parallels[boardno]),
  1349. .parallel_protocol = (initdata->parallels[boardno] != -1),
  1350. };
  1351. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1352. *procdata = (struct bitforce_proc_data){
  1353. .handles_board = true,
  1354. .cgpu = bitforce,
  1355. };
  1356. if (style != BFS_FPGA)
  1357. {
  1358. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1359. data->next_work_ob[8+32+12+8] = '\xAA';
  1360. data->next_work_obs = &data->next_work_ob[7];
  1361. switch (style)
  1362. {
  1363. case BFS_FPGA: // impossible
  1364. case BFS_65NM:
  1365. data->max_queue_at_once = BITFORCE_MAX_BQUEUE_AT_ONCE_65NM;
  1366. break;
  1367. case BFS_28NM:
  1368. data->max_queue_at_once = BITFORCE_MAX_BQUEUE_AT_ONCE_28NM;
  1369. }
  1370. if (bitforce->drv == &bitforce_queue_api)
  1371. {
  1372. bitforce_change_mode(bitforce, data->parallel_protocol ? BFP_PQUEUE : BFP_BQUEUE);
  1373. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1374. timer_set_delay_from_now(&thr->tv_poll, 0);
  1375. data->queued_max = data->parallel * 2;
  1376. if (data->queued_max < BITFORCE_MIN_QUEUED_MAX)
  1377. data->queued_max = BITFORCE_MIN_QUEUED_MAX;
  1378. if (data->queued_max > BITFORCE_MAX_QUEUED_MAX)
  1379. data->queued_max = BITFORCE_MAX_QUEUED_MAX;
  1380. }
  1381. else
  1382. bitforce_change_mode(bitforce, BFP_WORK);
  1383. // Clear job queue to start fresh; ignore response
  1384. bitforce_cmd1b(bitforce, buf, sizeof(buf), "ZQX", 3);
  1385. }
  1386. else
  1387. {
  1388. data->next_work_obs = &data->next_work_ob[0];
  1389. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1390. bitforce_change_mode(bitforce, BFP_WORK);
  1391. /* Initially enable support for nonce range and disable it later if it
  1392. * fails */
  1393. if (opt_bfl_noncerange)
  1394. bitforce_change_mode(bitforce, BFP_RANGE);
  1395. }
  1396. bitforce->status = LIFE_INIT2;
  1397. first_on_this_board = procdata;
  1398. for (int proc = 1; proc < data->parallel; ++proc)
  1399. {
  1400. bitforce = bitforce->next_proc;
  1401. assert(bitforce);
  1402. thr = bitforce->thr[0];
  1403. thr->queue_full = true;
  1404. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1405. *procdata = *first_on_this_board;
  1406. procdata->handles_board = false;
  1407. procdata->cgpu = bitforce;
  1408. bitforce->device_data = data;
  1409. bitforce->status = LIFE_INIT2;
  1410. bitforce->kname = first_on_this_board->cgpu->kname;
  1411. }
  1412. applog(LOG_DEBUG, "%s: Board %d: %"PRIpreprv"-%"PRIpreprv, bitforce->dev_repr, boardno, first_on_this_board->cgpu->proc_repr, bitforce->proc_repr);
  1413. ++boardno;
  1414. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1415. {}
  1416. }
  1417. // NOTE: This doesn't restore the first processor, but it does get us the last one; this is sufficient for the delay debug and start of the next loop below
  1418. bitforce = thr->cgpu;
  1419. free(initdata->parallels);
  1420. free(initdata);
  1421. /* Pause each new thread at least 100ms between initialising
  1422. * so the devices aren't making calls all at the same time. */
  1423. wait = thr->id * MAX_START_DELAY_MS;
  1424. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1425. cgsleep_ms(wait);
  1426. if (unlikely(!bitforce_open(bitforce)))
  1427. {
  1428. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  1429. return false;
  1430. }
  1431. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  1432. if (style != BFS_FPGA)
  1433. {
  1434. // Clear results queue last, to start fresh; ignore response
  1435. int last_xlink_id = -1;
  1436. for (bitforce = bitforce->device; bitforce; bitforce = bitforce->next_proc)
  1437. {
  1438. struct bitforce_data * const data = bitforce->device_data;
  1439. if (data->xlink_id == last_xlink_id)
  1440. continue;
  1441. last_xlink_id = data->xlink_id;
  1442. thr = bitforce->thr[0];
  1443. bitforce_zox(thr, "ZOX");
  1444. }
  1445. }
  1446. return true;
  1447. }
  1448. #ifdef HAVE_CURSES
  1449. static
  1450. void bitforce_tui_wlogprint_choices(struct cgpu_info *cgpu)
  1451. {
  1452. struct bitforce_data *data = cgpu->device_data;
  1453. if (data->supports_fanspeed)
  1454. wlogprint("[F]an control ");
  1455. }
  1456. static
  1457. const char *bitforce_tui_handle_choice(struct cgpu_info *cgpu, int input)
  1458. {
  1459. struct bitforce_data *data = cgpu->device_data;
  1460. pthread_mutex_t *mutexp;
  1461. static char replybuf[0x100];
  1462. if (!data->supports_fanspeed)
  1463. return NULL;
  1464. switch (input)
  1465. {
  1466. case 'f': case 'F':
  1467. {
  1468. int fanspeed;
  1469. char *intvar;
  1470. intvar = curses_input("Set fan speed (range 0-5 for low to fast or 9 for auto)");
  1471. if (!intvar)
  1472. return "Invalid fan speed\n";
  1473. fanspeed = atoi(intvar);
  1474. free(intvar);
  1475. if ((fanspeed < 0 || fanspeed > 5) && fanspeed != 9)
  1476. return "Invalid fan speed\n";
  1477. char cmd[4] = "Z0X";
  1478. cmd[1] += fanspeed;
  1479. mutexp = &cgpu->device->device_mutex;
  1480. mutex_lock(mutexp);
  1481. bitforce_cmd1b(cgpu, replybuf, sizeof(replybuf), cmd, 3);
  1482. mutex_unlock(mutexp);
  1483. return replybuf;
  1484. }
  1485. }
  1486. return NULL;
  1487. }
  1488. static
  1489. void bitforce_wlogprint_status(struct cgpu_info *cgpu)
  1490. {
  1491. struct bitforce_data *data = cgpu->device_data;
  1492. if (data->temp[0] > 0 && data->temp[1] > 0)
  1493. wlogprint("Temperatures: %4.1fC %4.1fC\n", data->temp[0], data->temp[1]);
  1494. if (data->volts_count)
  1495. {
  1496. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  1497. size_t sz = (data->volts_count * 10) + 1;
  1498. char buf[sz];
  1499. char *s = buf;
  1500. int rv = 0;
  1501. for (int i = 0; i < data->volts_count; ++i)
  1502. {
  1503. long v = data->volts[i];
  1504. _SNP("%ld.%03d / ", v / 1000, (int)(v % 1000));
  1505. }
  1506. if (rv >= 3 && s[-2] == '/')
  1507. s[-3] = '\0';
  1508. wlogprint("Voltages: %s\n", buf);
  1509. }
  1510. }
  1511. #endif
  1512. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1513. {
  1514. struct bitforce_data *data = cgpu->device_data;
  1515. struct api_data *root = NULL;
  1516. // Warning, access to these is not locked - but we don't really
  1517. // care since hashing performance is way more important than
  1518. // locking access to displaying API debug 'stats'
  1519. // If locking becomes an issue for any of them, use copy_data=true also
  1520. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1521. if (data->proto != BFP_BQUEUE && data->proto != BFP_PQUEUE)
  1522. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1523. if (data->temp[0] > 0 && data->temp[1] > 0)
  1524. {
  1525. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1526. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1527. }
  1528. for (int i = 0; i < data->volts_count; ++i)
  1529. {
  1530. float voltage = data->volts[i];
  1531. char key[] = "VoltageNN";
  1532. snprintf(&key[7], 3, "%d", i);
  1533. voltage /= 1e3;
  1534. root = api_add_volts(root, key, &voltage, true);
  1535. }
  1536. return root;
  1537. }
  1538. void bitforce_poll(struct thr_info *thr)
  1539. {
  1540. struct cgpu_info *bitforce = thr->cgpu;
  1541. struct bitforce_data *data = bitforce->device_data;
  1542. int poll = data->poll_func;
  1543. thr->tv_poll.tv_sec = -1;
  1544. data->poll_func = 0;
  1545. switch (poll)
  1546. {
  1547. case 1:
  1548. bitforce_job_start(thr);
  1549. break;
  1550. case 2:
  1551. bitforce_job_get_results(thr, thr->work);
  1552. break;
  1553. default:
  1554. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1555. }
  1556. }
  1557. static
  1558. const char *bitforce_set_fanmode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  1559. {
  1560. struct bitforce_data *data = proc->device_data;
  1561. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1562. {
  1563. if (!data->supports_fanspeed)
  1564. {
  1565. sprintf(replybuf, "fanmode not supported");
  1566. return replybuf;
  1567. }
  1568. if (!setting || !*setting)
  1569. {
  1570. sprintf(replybuf, "missing fanmode setting");
  1571. return replybuf;
  1572. }
  1573. if (setting[1] || ((setting[0] < '0' || setting[0] > '5') && setting[0] != '9'))
  1574. {
  1575. sprintf(replybuf, "invalid fanmode setting");
  1576. return replybuf;
  1577. }
  1578. char cmd[4] = "Z5X";
  1579. cmd[1] = setting[0];
  1580. mutex_lock(mutexp);
  1581. bitforce_cmd1b(proc, replybuf, 256, cmd, 3);
  1582. mutex_unlock(mutexp);
  1583. return replybuf;
  1584. }
  1585. }
  1586. static
  1587. const char *bitforce_rpc_send_cmd1(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  1588. {
  1589. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1590. {
  1591. mutex_lock(mutexp);
  1592. bitforce_cmd1b(proc, replybuf, 8000, setting, strlen(setting));
  1593. mutex_unlock(mutexp);
  1594. *success = SDR_OK;
  1595. return replybuf;
  1596. }
  1597. }
  1598. static const struct bfg_set_device_definition bitforce_set_device_funcs[] = {
  1599. {"fanmode", bitforce_set_fanmode, "range 0-5 (low to fast) or 9 (auto)"},
  1600. {"_cmd1", bitforce_rpc_send_cmd1, NULL},
  1601. {NULL},
  1602. };
  1603. struct device_drv bitforce_drv = {
  1604. .dname = "bitforce",
  1605. .name = "BFL",
  1606. .lowl_match = bitforce_lowl_match,
  1607. .lowl_probe = bitforce_lowl_probe,
  1608. #ifdef HAVE_CURSES
  1609. .proc_wlogprint_status = bitforce_wlogprint_status,
  1610. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1611. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1612. #endif
  1613. .get_api_stats = bitforce_drv_stats,
  1614. .minerloop = minerloop_async,
  1615. .reinit_device = bitforce_reinit,
  1616. .get_stats = bitforce_get_stats,
  1617. .identify_device = bitforce_identify,
  1618. .thread_init = bitforce_thread_init,
  1619. .job_prepare = bitforce_job_prepare,
  1620. .job_start = bitforce_job_start,
  1621. .job_get_results = bitforce_job_get_results,
  1622. .poll = bitforce_poll,
  1623. .job_process_results = bitforce_job_process_results,
  1624. .thread_shutdown = bitforce_shutdown,
  1625. .thread_enable = biforce_thread_enable
  1626. };
  1627. static inline
  1628. void bitforce_set_queue_full(struct thr_info *thr)
  1629. {
  1630. struct cgpu_info *bitforce = thr->cgpu;
  1631. struct bitforce_data *data = bitforce->device_data;
  1632. thr->queue_full = (data->queued + data->ready_to_queue >= data->queued_max) || (data->ready_to_queue >= data->max_queue_at_once);
  1633. }
  1634. static
  1635. bool bitforce_send_queue(struct thr_info *thr)
  1636. {
  1637. struct cgpu_info *bitforce = thr->cgpu;
  1638. struct cgpu_info * const dev = bitforce->device;
  1639. struct bitforce_data * const devdata = dev->device_data;
  1640. struct bitforce_data *data = bitforce->device_data;
  1641. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1642. struct work *work;
  1643. if (unlikely(!(devdata->is_open && data->ready_to_queue)))
  1644. return false;
  1645. char buf[0x100];
  1646. int queued_ok;
  1647. size_t qjs_sz = (32 + 12 + 1);
  1648. if (data->style == BFS_65NM)
  1649. ++qjs_sz;
  1650. size_t qjp_sz = 7 + (qjs_sz * data->ready_to_queue);
  1651. if (data->style == BFS_65NM)
  1652. qjp_sz -= 3;
  1653. uint8_t qjp[qjp_sz], *qjs;
  1654. qjs = &qjp[qjp_sz];
  1655. // NOTE: qjp is build backwards here
  1656. *(--qjs) = 0xfe;
  1657. work = thr->work_list->prev;
  1658. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1659. {
  1660. *(--qjs) = 0xaa;
  1661. memcpy(qjs -= 12, work->data + 64, 12);
  1662. memcpy(qjs -= 32, work->midstate, 32);
  1663. if (data->style == BFS_65NM)
  1664. *(--qjs) = 45;
  1665. }
  1666. *(--qjs) = data->ready_to_queue;
  1667. *(--qjs) = 0xc1;
  1668. if (data->style == BFS_65NM)
  1669. *(--qjs) = qjp_sz - 1;
  1670. else
  1671. {
  1672. *(--qjs) = qjp_sz >> 8;
  1673. *(--qjs) = qjp_sz & 0xff;
  1674. *(--qjs) = 'X';
  1675. *(--qjs) = 'W';
  1676. }
  1677. retry:
  1678. mutex_lock(mutexp);
  1679. if (data->style != BFS_65NM)
  1680. bitforce_cmd1c(bitforce, buf, sizeof(buf), qjp, qjp_sz);
  1681. else
  1682. if (data->missing_zwx)
  1683. bitforce_cmd2(bitforce, buf, sizeof(buf), "ZNX", &qjp[3], qjp_sz - 4);
  1684. else
  1685. bitforce_cmd2(bitforce, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1686. mutex_unlock(mutexp);
  1687. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1688. {
  1689. // Queue full :(
  1690. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1691. bitforce->proc_repr,
  1692. data->ready_to_queue, data->queued);
  1693. thr->queue_full = true;
  1694. return false;
  1695. }
  1696. if (strncasecmp(buf, "OK:QUEUED", 9))
  1697. {
  1698. if ((!strncasecmp(buf, "ERROR: UNKNOWN", 11)) && !data->missing_zwx)
  1699. {
  1700. applog(LOG_DEBUG, "%"PRIpreprv": Missing ZWX command, trying ZNX",
  1701. bitforce->proc_repr);
  1702. data->missing_zwx = true;
  1703. goto retry;
  1704. }
  1705. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1706. bitforce->proc_repr,
  1707. data->ready_to_queue, data->queued, buf);
  1708. return false;
  1709. }
  1710. if (!data->queued)
  1711. cgtime(&data->tv_hashmeter_start);
  1712. if (data->missing_zwx)
  1713. queued_ok = 1;
  1714. else
  1715. queued_ok = atoi(&buf[9]);
  1716. data->queued += queued_ok;
  1717. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1718. bitforce->proc_repr,
  1719. queued_ok, data->ready_to_queue, data->queued);
  1720. data->ready_to_queue -= queued_ok;
  1721. if (!data->missing_zwx)
  1722. thr->queue_full = data->ready_to_queue;
  1723. data->just_flushed = false;
  1724. data->want_to_send_queue = false;
  1725. return true;
  1726. }
  1727. void work_list_del(struct work **head, struct work *work)
  1728. {
  1729. DL_DELETE(*head, work);
  1730. free_work(work);
  1731. }
  1732. static
  1733. bool bitforce_queue_do_results(struct thr_info *thr)
  1734. {
  1735. struct cgpu_info *bitforce = thr->cgpu;
  1736. struct cgpu_info * const dev = bitforce->device;
  1737. struct bitforce_data * const devdata = dev->device_data;
  1738. struct bitforce_data *data = bitforce->device_data;
  1739. int count;
  1740. int fcount;
  1741. char *noncebuf, *buf, *end;
  1742. unsigned char midstate[32], datatail[12];
  1743. struct work *work, *tmpwork, *thiswork;
  1744. struct timeval tv_now, tv_elapsed;
  1745. long chipno = 0; // Initialized value is used for non-parallelized boards
  1746. struct cgpu_info *chip_cgpu;
  1747. struct thr_info *chip_thr;
  1748. int counts[data->parallel];
  1749. if (unlikely(!devdata->is_open))
  1750. return false;
  1751. again:
  1752. noncebuf = &data->noncebuf[0];
  1753. count = bitforce_zox(thr, "ZOX");
  1754. if (unlikely(count < 0))
  1755. {
  1756. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1757. inc_hw_errors_only(thr);
  1758. return false;
  1759. }
  1760. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results on poll (max=%d)", bitforce->proc_repr, count, (int)BITFORCE_MAX_QRESULTS);
  1761. if (!count)
  1762. return true;
  1763. fcount = 0;
  1764. for (int i = 0; i < data->parallel; ++i)
  1765. counts[i] = 0;
  1766. noncebuf = next_line(noncebuf);
  1767. while ((buf = noncebuf)[0])
  1768. {
  1769. if ( (noncebuf = next_line(buf)) )
  1770. noncebuf[-1] = '\0';
  1771. if (strlen(buf) <= 90)
  1772. {
  1773. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1774. continue;
  1775. }
  1776. hex2bin(midstate, buf, 32);
  1777. hex2bin(datatail, &buf[65], 12);
  1778. thiswork = NULL;
  1779. DL_FOREACH(thr->work_list, work)
  1780. {
  1781. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1782. continue;
  1783. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1784. continue;
  1785. thiswork = work;
  1786. break;
  1787. }
  1788. end = &buf[89];
  1789. chip_cgpu = bitforce;
  1790. if (data->parallel_protocol)
  1791. {
  1792. chipno = strtol(&end[1], &end, 16);
  1793. if (chipno >= data->parallel)
  1794. {
  1795. applog(LOG_ERR, "%"PRIpreprv": Chip number out of range for queue result: %s", chip_cgpu->proc_repr, buf);
  1796. chipno = 0;
  1797. }
  1798. for (int i = 0; i < chipno; ++i)
  1799. chip_cgpu = chip_cgpu->next_proc;
  1800. }
  1801. chip_thr = chip_cgpu->thr[0];
  1802. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", chip_cgpu->proc_repr, buf);
  1803. if (unlikely(!thiswork))
  1804. {
  1805. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results: %s", chip_cgpu->proc_repr, buf);
  1806. inc_hw_errors_only(chip_thr);
  1807. goto next_qline;
  1808. }
  1809. if (unlikely(!end[0]))
  1810. {
  1811. applog(LOG_ERR, "%"PRIpreprv": Missing nonce count in queue results: %s", chip_cgpu->proc_repr, buf);
  1812. goto finishresult;
  1813. }
  1814. if (strtol(&end[1], &end, 10))
  1815. {
  1816. if (unlikely(!end[0]))
  1817. {
  1818. applog(LOG_ERR, "%"PRIpreprv": Missing nonces in queue results: %s", chip_cgpu->proc_repr, buf);
  1819. goto finishresult;
  1820. }
  1821. bitforce_process_result_nonces(chip_thr, work, &end[1]);
  1822. }
  1823. ++fcount;
  1824. ++counts[chipno];
  1825. finishresult:
  1826. if (data->parallel == 1)
  1827. {
  1828. // Queue results are in order, so anything queued prior this is lost
  1829. // Delete all queued work up to, and including, this one
  1830. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1831. {
  1832. work_list_del(&thr->work_list, work);
  1833. --data->queued;
  1834. if (work == thiswork)
  1835. break;
  1836. }
  1837. }
  1838. else
  1839. {
  1840. // Parallel processors means the results might not be in order
  1841. // This could leak if jobs get lost, hence the sanity checks using "ZqX"
  1842. work_list_del(&thr->work_list, thiswork);
  1843. --data->queued;
  1844. }
  1845. next_qline: (void)0;
  1846. }
  1847. bitforce_set_queue_full(thr);
  1848. if (count >= BITFORCE_MAX_QRESULTS)
  1849. goto again;
  1850. if (data->parallel == 1 && (
  1851. (fcount < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1852. || (fcount > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT) ))
  1853. {
  1854. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1855. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (fcount ?: 1);
  1856. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1857. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1858. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1859. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1860. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1861. bitforce->proc_repr, fcount, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1862. }
  1863. else
  1864. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1865. bitforce->proc_repr, fcount, bitforce->sleep_ms, data->queued);
  1866. cgtime(&tv_now);
  1867. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1868. chip_cgpu = bitforce;
  1869. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  1870. {
  1871. chip_thr = chip_cgpu->thr[0];
  1872. hashes_done(chip_thr, (uint64_t)bitforce->nonces * counts[i], &tv_elapsed, NULL);
  1873. }
  1874. data->tv_hashmeter_start = tv_now;
  1875. return true;
  1876. }
  1877. static
  1878. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1879. {
  1880. struct cgpu_info *bitforce = thr->cgpu;
  1881. struct bitforce_data *data = bitforce->device_data;
  1882. bool rv, ndq;
  1883. bitforce_set_queue_full(thr);
  1884. rv = !thr->queue_full;
  1885. if (rv)
  1886. {
  1887. DL_APPEND(thr->work_list, work);
  1888. ++data->ready_to_queue;
  1889. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1890. bitforce->proc_repr,
  1891. (unsigned)data->queued_max, data->ready_to_queue, data->queued);
  1892. bitforce_set_queue_full(thr);
  1893. }
  1894. else
  1895. if (!data->ready_to_queue)
  1896. return rv;
  1897. ndq = !data->queued;
  1898. if ((ndq) // Device is idle
  1899. || (data->ready_to_queue >= data->max_queue_at_once) // ...or 5 items ready to go
  1900. || (thr->queue_full) // ...or done filling queue
  1901. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1902. || (data->missing_zwx) // ...or device can only queue one at a time
  1903. )
  1904. {
  1905. if (!bitforce_send_queue(thr))
  1906. {
  1907. // Problem sending queue, retry again in a few seconds
  1908. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1909. inc_hw_errors_only(thr);
  1910. data->want_to_send_queue = true;
  1911. }
  1912. }
  1913. return rv;
  1914. }
  1915. struct _jobinfo {
  1916. uint8_t key[32+12];
  1917. int instances;
  1918. UT_hash_handle hh;
  1919. };
  1920. static
  1921. void bitforce_queue_flush(struct thr_info *thr)
  1922. {
  1923. struct bitforce_proc_data *procdata = thr->cgpu_data;
  1924. if (!procdata->handles_board)
  1925. return;
  1926. struct cgpu_info *bitforce = thr->cgpu;
  1927. struct bitforce_data *data = bitforce->device_data;
  1928. char *buf = &data->noncebuf[0], *buf2 = NULL;
  1929. const char *cmd = "ZqX";
  1930. unsigned flushed;
  1931. struct _jobinfo *processing = NULL, *item, *this;
  1932. if (data->parallel == 1)
  1933. // Pre-parallelization neither needs nor supports "ZqX"
  1934. cmd = "ZQX";
  1935. // TODO: Call "ZQX" most of the time: don't need to do sanity checks so often
  1936. bitforce_zox(thr, cmd);
  1937. if (!strncasecmp(buf, "OK:FLUSHED", 10))
  1938. flushed = atoi(&buf[10]);
  1939. else
  1940. if ((!strncasecmp(buf, "COUNT:", 6)) && (buf2 = strstr(buf, "FLUSHED:")) )
  1941. {
  1942. flushed = atoi(&buf2[8]);
  1943. buf2 = next_line(buf2);
  1944. }
  1945. else
  1946. if (!strncasecmp(buf, "OK", 2))
  1947. {
  1948. applog(LOG_DEBUG, "%"PRIpreprv": Didn't report flush count", bitforce->proc_repr);
  1949. thr->queue_full = false;
  1950. flushed = 0;
  1951. }
  1952. else
  1953. {
  1954. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1955. flushed = 0;
  1956. }
  1957. if (flushed > data->queued)
  1958. {
  1959. applog(LOG_WARNING, "%"PRIpreprv": Flushed %u jobs from device, but only %u were queued",
  1960. bitforce->proc_repr, flushed, data->queued);
  1961. inc_hw_errors_only(thr);
  1962. // We need to avoid trying to delete more items than we've sent, or a segfault is upcoming...
  1963. flushed = data->queued;
  1964. }
  1965. data->queued -= flushed;
  1966. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1967. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1968. flushed += data->ready_to_queue;
  1969. data->ready_to_queue = 0;
  1970. while (flushed--)
  1971. work_list_del(&thr->work_list, thr->work_list->prev);
  1972. bitforce_set_queue_full(thr);
  1973. data->just_flushed = true;
  1974. data->want_to_send_queue = false;
  1975. // "ZqX" returns jobs in progress, allowing us to sanity check
  1976. // NOTE: Must process buffer into hash table BEFORE calling bitforce_queue_do_results, which clobbers it
  1977. // NOTE: Must do actual sanity check AFTER calling bitforce_queue_do_results, to ensure we don't delete completed jobs
  1978. if (buf2)
  1979. {
  1980. // First, turn buf2 into a hash
  1981. for ( ; buf2[0]; buf2 = next_line(buf2))
  1982. {
  1983. this = malloc(sizeof(*this));
  1984. hex2bin(&this->key[ 0], &buf2[ 0], 32);
  1985. hex2bin(&this->key[32], &buf2[65], 12);
  1986. HASH_FIND(hh, processing, &this->key[0], sizeof(this->key), item);
  1987. if (likely(!item))
  1988. {
  1989. this->instances = 1;
  1990. HASH_ADD(hh, processing, key, sizeof(this->key), this);
  1991. }
  1992. else
  1993. {
  1994. // This should really only happen in testing/benchmarking...
  1995. ++item->instances;
  1996. free(this);
  1997. }
  1998. }
  1999. }
  2000. bitforce_queue_do_results(thr);
  2001. if (buf2)
  2002. {
  2003. struct work *work, *tmp;
  2004. uint8_t key[32+12];
  2005. // Now iterate over the work_list and delete anything not in the hash
  2006. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  2007. {
  2008. memcpy(&key[ 0], work->midstate, 32);
  2009. memcpy(&key[32], &work->data[64], 12);
  2010. HASH_FIND(hh, processing, &key[0], sizeof(key), item);
  2011. if (unlikely(!item))
  2012. {
  2013. char hex[89];
  2014. bin2hex(hex, key, 32+12);
  2015. applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is missing queued job! %s", bitforce->proc_repr, hex);
  2016. work_list_del(&thr->work_list, work);
  2017. continue;
  2018. }
  2019. if (likely(!--item->instances))
  2020. {
  2021. HASH_DEL(processing, item);
  2022. free(item);
  2023. }
  2024. }
  2025. if (unlikely( (flushed = HASH_COUNT(processing)) ))
  2026. {
  2027. //applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is working on %d unknown jobs!", bitforce->proc_repr, flushed);
  2028. // FIXME: Probably these were jobs finished after ZqX, included in the result check we just did
  2029. // NOTE: We need to do that result check first to avoid deleting work_list items for things just solved
  2030. HASH_ITER(hh, processing, item, this)
  2031. {
  2032. HASH_DEL(processing, item);
  2033. free(item);
  2034. }
  2035. }
  2036. }
  2037. }
  2038. static
  2039. void bitforce_queue_poll(struct thr_info *thr)
  2040. {
  2041. struct cgpu_info *bitforce = thr->cgpu;
  2042. struct bitforce_data *data = bitforce->device_data;
  2043. unsigned long sleep_us;
  2044. if (data->queued)
  2045. bitforce_queue_do_results(thr);
  2046. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  2047. if (data->want_to_send_queue)
  2048. if (!bitforce_send_queue(thr))
  2049. if (!data->queued)
  2050. {
  2051. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  2052. inc_hw_errors_only(thr);
  2053. sleep_us = 1000000;
  2054. }
  2055. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  2056. }
  2057. static void bitforce_queue_thread_deven(struct thr_info *thr)
  2058. {
  2059. struct cgpu_info *bitforce = thr->cgpu, *thisbf;
  2060. struct bitforce_data *data = bitforce->device_data;
  2061. struct thr_info *thisthr;
  2062. for (thisbf = bitforce->device; thisbf && thisbf->device_data != data; thisbf = thisbf->next_proc)
  2063. {}
  2064. for ( ; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
  2065. {
  2066. thisthr = bitforce->thr[0];
  2067. thisthr->pause = thr->pause;
  2068. thisbf->deven = bitforce->deven;
  2069. }
  2070. }
  2071. static void bitforce_queue_thread_disable(struct thr_info *thr)
  2072. {
  2073. // Disable other threads sharing the same queue
  2074. bitforce_queue_thread_deven(thr);
  2075. }
  2076. static void bitforce_queue_thread_enable(struct thr_info *thr)
  2077. {
  2078. // TODO: Maybe reinit?
  2079. // Enable other threads sharing the same queue
  2080. bitforce_queue_thread_deven(thr);
  2081. }
  2082. struct device_drv bitforce_queue_api = {
  2083. .dname = "bitforce_queue",
  2084. .name = "BFL",
  2085. .lowl_probe_by_name_only = true,
  2086. .lowl_match = bitforce_lowl_match,
  2087. .lowl_probe = bitforce_lowl_probe,
  2088. .minerloop = minerloop_queue,
  2089. .reinit_device = bitforce_reinit,
  2090. #ifdef HAVE_CURSES
  2091. .proc_wlogprint_status = bitforce_wlogprint_status,
  2092. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  2093. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  2094. #endif
  2095. .get_api_stats = bitforce_drv_stats,
  2096. .get_stats = bitforce_get_stats,
  2097. .identify_device = bitforce_identify,
  2098. .thread_init = bitforce_thread_init,
  2099. .queue_append = bitforce_queue_append,
  2100. .queue_flush = bitforce_queue_flush,
  2101. .poll = bitforce_queue_poll,
  2102. .thread_shutdown = bitforce_shutdown,
  2103. .thread_disable = bitforce_queue_thread_disable,
  2104. .thread_enable = bitforce_queue_thread_enable,
  2105. };