driver-bitforce.c 63 KB

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