driver-bitforce.c 64 KB

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