driver-bitforce.c 74 KB

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