driver-bitforce.c 73 KB

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