driver-bitforce.c 65 KB

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