driver-bitforce.c 73 KB

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