driver-bitforce.c 72 KB

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