driver-bitforce.c 64 KB

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