driver-bitforce.c 65 KB

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