driver-bitforce.c 72 KB

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