driver-bitforce.c 72 KB

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