driver-bitforce.c 59 KB

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