driver-bitforce.c 73 KB

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