driver-bitforce.c 67 KB

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