driver-bitforce.c 74 KB

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