driver-bitforce.c 65 KB

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