driver-bitforce.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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 <limits.h>
  12. #include <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <strings.h>
  16. #include <sys/time.h>
  17. #include <unistd.h>
  18. #include "compat.h"
  19. #include "deviceapi.h"
  20. #include "miner.h"
  21. #include "fpgautils.h"
  22. #include "util.h"
  23. #define BITFORCE_SLEEP_MS 500
  24. #define BITFORCE_TIMEOUT_S 7
  25. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  26. #define BITFORCE_LONG_TIMEOUT_S 25
  27. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  28. #define BITFORCE_CHECK_INTERVAL_MS 10
  29. #define WORK_CHECK_INTERVAL_MS 50
  30. #define MAX_START_DELAY_MS 100
  31. #define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
  32. #define TIME_AVG_CONSTANT 8
  33. #define BITFORCE_QRESULT_LINE_LEN 165
  34. #define BITFORCE_MAX_QUEUED 10
  35. #define BITFORCE_MAX_QRESULTS 10
  36. #define BITFORCE_GOAL_QRESULTS (BITFORCE_MAX_QRESULTS / 2)
  37. #define BITFORCE_MIN_QRESULT_WAIT BITFORCE_CHECK_INTERVAL_MS
  38. #define BITFORCE_MAX_QRESULT_WAIT 1000
  39. #define BITFORCE_MAX_BQUEUE_AT_ONCE 5
  40. enum bitforce_proto {
  41. BFP_WORK,
  42. BFP_RANGE,
  43. BFP_QUEUE,
  44. BFP_BQUEUE,
  45. };
  46. static const char *protonames[] = {
  47. "full work",
  48. "nonce range",
  49. "work queue",
  50. "bulk queue",
  51. };
  52. struct device_drv bitforce_drv;
  53. struct device_drv bitforce_queue_api;
  54. // Code must deal with a timeout
  55. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  56. static void BFgets(char *buf, size_t bufLen, int fd)
  57. {
  58. do {
  59. buf[0] = '\0';
  60. --bufLen;
  61. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  62. buf[0] = '\0';
  63. }
  64. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  65. {
  66. if ((bufLen) != write(fd, buf, bufLen))
  67. return 0;
  68. else
  69. return bufLen;
  70. }
  71. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  72. {
  73. if (!procid)
  74. return BFwrite(fd, buf, bufLen);
  75. if (bufLen > 255)
  76. return -1;
  77. size_t bufLeft = bufLen + 3;
  78. char realbuf[bufLeft], *bufp;
  79. ssize_t rv;
  80. memcpy(&realbuf[3], buf, bufLen);
  81. realbuf[0] = '@';
  82. realbuf[1] = procid;
  83. realbuf[2] = bufLen;
  84. bufp = realbuf;
  85. while (true)
  86. {
  87. rv = BFwrite(fd, bufp, bufLeft);
  88. if (rv <= 0)
  89. return rv;
  90. bufLeft -= rv;
  91. }
  92. return bufLen;
  93. }
  94. static
  95. void bitforce_cmd1(int fd, int procid, void *buf, size_t bufsz, const char *cmd)
  96. {
  97. bitforce_send(fd, procid, cmd, 3);
  98. BFgets(buf, bufsz, fd);
  99. }
  100. static
  101. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  102. {
  103. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  104. if (strncasecmp(buf, "OK", 2))
  105. return;
  106. bitforce_send(fd, procid, data, datasz);
  107. BFgets(buf, bufsz, fd);
  108. }
  109. #define BFclose(fd) close(fd)
  110. struct bitforce_init_data {
  111. bool sc;
  112. long devmask;
  113. };
  114. static bool bitforce_detect_one(const char *devpath)
  115. {
  116. int fdDev = serial_open(devpath, 0, 10, true);
  117. struct cgpu_info *bitforce;
  118. char pdevbuf[0x100];
  119. size_t pdevbuf_len;
  120. char *s;
  121. int procs = 1;
  122. struct bitforce_init_data *initdata;
  123. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  124. if (unlikely(fdDev == -1)) {
  125. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  126. return false;
  127. }
  128. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  129. if (unlikely(!pdevbuf[0])) {
  130. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  131. return 0;
  132. }
  133. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  134. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  135. BFclose(fdDev);
  136. return false;
  137. }
  138. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  139. initdata = malloc(sizeof(*initdata));
  140. *initdata = (struct bitforce_init_data){
  141. .sc = false,
  142. };
  143. for ( bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  144. strncasecmp(pdevbuf, "OK", 2);
  145. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  146. {
  147. pdevbuf_len = strlen(pdevbuf);
  148. if (unlikely(!pdevbuf_len))
  149. continue;
  150. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  151. applog(LOG_DEBUG, " %s", pdevbuf);
  152. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  153. procs = atoi(&pdevbuf[17]);
  154. else
  155. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  156. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  157. else
  158. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  159. initdata->sc = true;
  160. }
  161. BFclose(fdDev);
  162. // We have a real BitForce!
  163. bitforce = calloc(1, sizeof(*bitforce));
  164. bitforce->drv = &bitforce_drv;
  165. if (initdata->sc)
  166. bitforce->drv = &bitforce_queue_api;
  167. bitforce->device_path = strdup(devpath);
  168. bitforce->deven = DEV_ENABLED;
  169. bitforce->procs = procs;
  170. bitforce->threads = 1;
  171. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  172. s[0] = '\0';
  173. bitforce->name = strdup(pdevbuf + 7);
  174. }
  175. bitforce->device_data = initdata;
  176. mutex_init(&bitforce->device_mutex);
  177. return add_cgpu(bitforce);
  178. }
  179. static int bitforce_detect_auto(void)
  180. {
  181. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  182. }
  183. static void bitforce_detect(void)
  184. {
  185. serial_detect_auto(&bitforce_drv, bitforce_detect_one, bitforce_detect_auto);
  186. }
  187. struct bitforce_data {
  188. int xlink_id;
  189. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  190. unsigned char *next_work_obs; // Start of data to send
  191. unsigned char next_work_obsz;
  192. const char *next_work_cmd;
  193. char noncebuf[14 + ((BITFORCE_MAX_QRESULTS+1) * BITFORCE_QRESULT_LINE_LEN)];
  194. int poll_func;
  195. enum bitforce_proto proto;
  196. bool sc;
  197. int queued;
  198. bool already_have_results;
  199. bool just_flushed;
  200. int ready_to_queue;
  201. bool want_to_send_queue;
  202. unsigned result_busy_polled;
  203. unsigned sleep_ms_default;
  204. struct timeval tv_hashmeter_start;
  205. float temp[2];
  206. };
  207. static void bitforce_clear_buffer(struct cgpu_info *);
  208. static
  209. void bitforce_comm_error(struct thr_info *thr)
  210. {
  211. struct cgpu_info *bitforce = thr->cgpu;
  212. struct bitforce_data *data = bitforce->device_data;
  213. int *p_fdDev = &bitforce->device->device_fd;
  214. data->noncebuf[0] = '\0';
  215. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  216. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  217. ++bitforce->hw_errors;
  218. ++hw_errors;
  219. BFclose(*p_fdDev);
  220. int fd = *p_fdDev = BFopen(bitforce->device_path);
  221. if (fd == -1)
  222. {
  223. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  224. return;
  225. }
  226. /* empty read buffer */
  227. bitforce_clear_buffer(bitforce);
  228. }
  229. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  230. {
  231. struct bitforce_data *data = bitforce->device_data;
  232. if (data->temp[0] > 0 && data->temp[1] > 0)
  233. tailsprintf(buf, "%5.1fC/%4.1fC | ", data->temp[0], data->temp[1]);
  234. else
  235. if (bitforce->temp > 0)
  236. tailsprintf(buf, "%5.1fC | ", bitforce->temp);
  237. else
  238. tailsprintf(buf, " | ");
  239. }
  240. static bool bitforce_thread_prepare(struct thr_info *thr)
  241. {
  242. struct cgpu_info *bitforce = thr->cgpu;
  243. int fdDev = BFopen(bitforce->device_path);
  244. struct timeval now;
  245. if (unlikely(fdDev == -1)) {
  246. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  247. return false;
  248. }
  249. bitforce->device_fd = fdDev;
  250. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  251. cgtime(&now);
  252. get_datestamp(bitforce->init, &now);
  253. return true;
  254. }
  255. static void __bitforce_clear_buffer(int fdDev)
  256. {
  257. char pdevbuf[0x100];
  258. int count = 0;
  259. do {
  260. pdevbuf[0] = '\0';
  261. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  262. } while (pdevbuf[0] && (++count < 10));
  263. }
  264. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  265. {
  266. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  267. int fdDev;
  268. mutex_lock(mutexp);
  269. fdDev = bitforce->device->device_fd;
  270. if (fdDev)
  271. {
  272. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  273. __bitforce_clear_buffer(fdDev);
  274. }
  275. mutex_unlock(mutexp);
  276. }
  277. void bitforce_reinit(struct cgpu_info *bitforce)
  278. {
  279. struct bitforce_data *data = bitforce->device_data;
  280. struct thr_info *thr = bitforce->thr[0];
  281. const char *devpath = bitforce->device_path;
  282. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  283. int *p_fdDev = &bitforce->device->device_fd;
  284. int fdDev, retries = 0;
  285. char pdevbuf[0x100];
  286. char *s;
  287. mutex_lock(mutexp);
  288. fdDev = *p_fdDev;
  289. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  290. if (fdDev) {
  291. BFclose(fdDev);
  292. nmsleep(5000);
  293. *p_fdDev = 0;
  294. }
  295. fdDev = BFopen(devpath);
  296. if (unlikely(fdDev == -1)) {
  297. mutex_unlock(mutexp);
  298. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  299. return;
  300. }
  301. __bitforce_clear_buffer(fdDev);
  302. do {
  303. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  304. if (unlikely(!pdevbuf[0])) {
  305. mutex_unlock(mutexp);
  306. BFclose(fdDev);
  307. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  308. return;
  309. }
  310. if (retries++)
  311. nmsleep(10);
  312. } while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  313. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  314. mutex_unlock(mutexp);
  315. BFclose(fdDev);
  316. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  317. return;
  318. }
  319. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  320. s[0] = '\0';
  321. free((void*)bitforce->name);
  322. bitforce->name = strdup(pdevbuf + 7);
  323. }
  324. *p_fdDev = fdDev;
  325. bitforce->sleep_ms = data->sleep_ms_default;
  326. if (bitforce->drv == &bitforce_queue_api)
  327. {
  328. struct work *work, *tmp;
  329. timer_set_delay_from_now(&thr->tv_poll, 0);
  330. notifier_wake(thr->notifier);
  331. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  332. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  333. {
  334. DL_DELETE(thr->work_list, work);
  335. free_work(work);
  336. }
  337. data->queued = 0;
  338. data->ready_to_queue = 0;
  339. data->already_have_results = false;
  340. data->just_flushed = true;
  341. thr->queue_full = false;
  342. }
  343. mutex_unlock(mutexp);
  344. }
  345. static void bitforce_flash_led(struct cgpu_info *bitforce)
  346. {
  347. struct bitforce_data *data = bitforce->device_data;
  348. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  349. int fdDev = bitforce->device->device_fd;
  350. if (!fdDev)
  351. return;
  352. /* Do not try to flash the led if we're polling for a result to
  353. * minimise the chance of interleaved results */
  354. if (bitforce->polling)
  355. return;
  356. /* It is not critical flashing the led so don't get stuck if we
  357. * can't grab the mutex here */
  358. if (mutex_trylock(mutexp))
  359. return;
  360. char pdevbuf[0x100];
  361. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  362. /* Once we've tried - don't do it until told to again */
  363. bitforce->flash_led = false;
  364. /* However, this stops anything else getting a reply
  365. * So best to delay any other access to the BFL */
  366. nmsleep(4000);
  367. mutex_unlock(mutexp);
  368. return; // nothing is returned by the BFL
  369. }
  370. static
  371. float my_strtof(const char *nptr, char **endptr)
  372. {
  373. float f = strtof(nptr, endptr);
  374. /* Cope with older software that breaks and reads nonsense
  375. * values */
  376. if (f > 100)
  377. f = strtod(nptr, endptr);
  378. return f;
  379. }
  380. static
  381. void set_float_if_gt_zero(float *var, float value)
  382. {
  383. if (value > 0)
  384. *var = value;
  385. }
  386. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  387. {
  388. struct bitforce_data *data = bitforce->device_data;
  389. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  390. int fdDev = bitforce->device->device_fd;
  391. char pdevbuf[0x100];
  392. char *s;
  393. if (!fdDev)
  394. return false;
  395. /* Do not try to get the temperature if we're polling for a result to
  396. * minimise the chance of interleaved results */
  397. if (bitforce->polling)
  398. return true;
  399. // Flash instead of Temp - doing both can be too slow
  400. if (bitforce->flash_led) {
  401. bitforce_flash_led(bitforce);
  402. return true;
  403. }
  404. /* It is not critical getting temperature so don't get stuck if we
  405. * can't grab the mutex here */
  406. if (mutex_trylock(mutexp))
  407. return false;
  408. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  409. mutex_unlock(mutexp);
  410. if (unlikely(!pdevbuf[0])) {
  411. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  412. bitforce->hw_errors++;
  413. ++hw_errors;
  414. return false;
  415. }
  416. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  417. float temp = my_strtof(s + 1, &s);
  418. set_float_if_gt_zero(&data->temp[0], temp);
  419. for ( ; s[0]; ++s)
  420. {
  421. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  422. {
  423. float temp2 = my_strtof(s + 1, &s);
  424. set_float_if_gt_zero(&data->temp[1], temp2);
  425. if (temp2 > temp)
  426. temp = temp;
  427. }
  428. }
  429. set_float_if_gt_zero(&bitforce->temp, temp);
  430. } else {
  431. /* Use the temperature monitor as a kind of watchdog for when
  432. * our responses are out of sync and flush the buffer to
  433. * hopefully recover */
  434. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  435. dev_error(bitforce, REASON_DEV_THROTTLE);
  436. /* Count throttling episodes as hardware errors */
  437. bitforce->hw_errors++;
  438. ++hw_errors;
  439. bitforce_clear_buffer(bitforce);
  440. return false;
  441. }
  442. return true;
  443. }
  444. static inline
  445. void dbg_block_data(struct cgpu_info *bitforce)
  446. {
  447. if (!opt_debug)
  448. return;
  449. struct bitforce_data *data = bitforce->device_data;
  450. char *s;
  451. s = bin2hex(&data->next_work_ob[8], 44);
  452. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  453. free(s);
  454. }
  455. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  456. static
  457. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  458. {
  459. struct cgpu_info *bitforce = thr->cgpu;
  460. struct bitforce_data *data = bitforce->device_data;
  461. int fdDev = bitforce->device->device_fd;
  462. unsigned char *ob_ms = &data->next_work_ob[8];
  463. unsigned char *ob_dt = &ob_ms[32];
  464. // If polling job_start, cancel it
  465. if (data->poll_func == 1)
  466. {
  467. thr->tv_poll.tv_sec = -1;
  468. data->poll_func = 0;
  469. }
  470. memcpy(ob_ms, work->midstate, 32);
  471. memcpy(ob_dt, work->data + 64, 12);
  472. switch (data->proto)
  473. {
  474. case BFP_BQUEUE:
  475. quit(1, "%"PRIpreprv": Impossible BFP_BQUEUE in bitforce_job_prepare", bitforce->proc_repr);
  476. case BFP_RANGE:
  477. {
  478. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  479. ob_nonce[0] = htobe32(work->blk.nonce);
  480. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  481. // FIXME: if nonce range fails... we didn't increment enough
  482. work->blk.nonce += bitforce->nonces + 1;
  483. break;
  484. }
  485. case BFP_QUEUE:
  486. if (thr->work)
  487. {
  488. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  489. char pdevbuf[0x100];
  490. if (unlikely(!fdDev))
  491. return false;
  492. mutex_lock(mutexp);
  493. if (data->queued)
  494. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  495. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  496. mutex_unlock(mutexp);
  497. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  498. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  499. bitforce_change_mode(bitforce, BFP_WORK);
  500. }
  501. else
  502. {
  503. dbg_block_data(bitforce);
  504. data->queued = 1;
  505. }
  506. }
  507. // fallthru...
  508. case BFP_WORK:
  509. work->blk.nonce = 0xffffffff;
  510. }
  511. return true;
  512. }
  513. static
  514. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  515. {
  516. struct bitforce_data *data = bitforce->device_data;
  517. if (data->proto == proto)
  518. return;
  519. if (data->proto == BFP_RANGE)
  520. {
  521. bitforce->nonces = 0xffffffff;
  522. bitforce->sleep_ms *= 5;
  523. data->sleep_ms_default *= 5;
  524. switch (proto)
  525. {
  526. case BFP_WORK:
  527. data->next_work_cmd = "ZDX";
  528. break;
  529. case BFP_QUEUE:
  530. data->next_work_cmd = "ZNX";
  531. default:
  532. ;
  533. }
  534. if (data->sc)
  535. {
  536. // "S|---------- MidState ----------||-DataTail-|E"
  537. data->next_work_ob[7] = 45;
  538. data->next_work_ob[8+32+12] = '\xAA';
  539. data->next_work_obsz = 46;
  540. }
  541. else
  542. {
  543. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  544. memset(&data->next_work_ob[8+32+12], '>', 8);
  545. data->next_work_obsz = 60;
  546. }
  547. }
  548. else
  549. if (proto == BFP_RANGE)
  550. {
  551. /* Split work up into 1/5th nonce ranges */
  552. bitforce->nonces = 0x33333332;
  553. bitforce->sleep_ms /= 5;
  554. data->sleep_ms_default /= 5;
  555. data->next_work_cmd = "ZPX";
  556. if (data->sc)
  557. {
  558. data->next_work_ob[7] = 53;
  559. data->next_work_obsz = 54;
  560. }
  561. else
  562. data->next_work_obsz = 68;
  563. }
  564. data->proto = proto;
  565. bitforce->kname = protonames[proto];
  566. }
  567. static
  568. void bitforce_job_start(struct thr_info *thr)
  569. {
  570. struct cgpu_info *bitforce = thr->cgpu;
  571. struct bitforce_data *data = bitforce->device_data;
  572. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  573. int fdDev = bitforce->device->device_fd;
  574. unsigned char *ob = data->next_work_obs;
  575. char pdevbuf[0x100];
  576. struct timeval tv_now;
  577. data->result_busy_polled = 0;
  578. if (data->queued)
  579. {
  580. uint32_t delay;
  581. // get_results collected more accurate job start time
  582. mt_job_transition(thr);
  583. job_start_complete(thr);
  584. data->queued = 0;
  585. delay = (uint32_t)bitforce->sleep_ms * 1000;
  586. if (unlikely(data->already_have_results))
  587. delay = 0;
  588. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  589. return;
  590. }
  591. if (!fdDev)
  592. goto commerr;
  593. re_send:
  594. mutex_lock(mutexp);
  595. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  596. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  597. mutex_unlock(mutexp);
  598. cgtime(&tv_now);
  599. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  600. data->poll_func = 1;
  601. return;
  602. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  603. mutex_unlock(mutexp);
  604. switch (data->proto)
  605. {
  606. case BFP_RANGE:
  607. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  608. bitforce_change_mode(bitforce, BFP_WORK);
  609. goto re_send;
  610. case BFP_QUEUE:
  611. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  612. bitforce_change_mode(bitforce, BFP_WORK);
  613. goto re_send;
  614. default:
  615. ;
  616. }
  617. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  618. goto commerr;
  619. }
  620. mt_job_transition(thr);
  621. mutex_unlock(mutexp);
  622. dbg_block_data(bitforce);
  623. cgtime(&tv_now);
  624. bitforce->work_start_tv = tv_now;
  625. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  626. job_start_complete(thr);
  627. return;
  628. commerr:
  629. bitforce_comm_error(thr);
  630. job_start_abort(thr, true);
  631. }
  632. static char _discardedbuf[0x10];
  633. static
  634. int bitforce_zox(struct thr_info *thr, const char *cmd)
  635. {
  636. struct cgpu_info *bitforce = thr->cgpu;
  637. struct bitforce_data *data = bitforce->device_data;
  638. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  639. int fd = bitforce->device->device_fd;
  640. char *pdevbuf = &data->noncebuf[0];
  641. int count;
  642. mutex_lock(mutexp);
  643. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  644. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  645. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  646. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  647. {
  648. count = atoi(&pdevbuf[6]);
  649. size_t cls = strlen(pdevbuf);
  650. char *pmorebuf = &pdevbuf[cls];
  651. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  652. if (count && data->queued)
  653. cgtime(&bitforce->work_start_tv);
  654. while (true)
  655. {
  656. BFgets(pmorebuf, szleft, fd);
  657. if (!strncasecmp(pmorebuf, "OK", 2))
  658. {
  659. pmorebuf[0] = '\0'; // process expects only results
  660. break;
  661. }
  662. sz = strlen(pmorebuf);
  663. if (!sz)
  664. {
  665. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  666. break;
  667. }
  668. szleft -= sz;
  669. pmorebuf += sz;
  670. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  671. {
  672. // Out of buffer space somehow :(
  673. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  674. pmorebuf = _discardedbuf;
  675. szleft = sizeof(_discardedbuf);
  676. }
  677. }
  678. }
  679. else
  680. count = -1;
  681. mutex_unlock(mutexp);
  682. return count;
  683. }
  684. static inline char *next_line(char *);
  685. static
  686. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  687. {
  688. struct cgpu_info *bitforce = thr->cgpu;
  689. struct bitforce_data *data = bitforce->device_data;
  690. int fdDev = bitforce->device->device_fd;
  691. unsigned int delay_time_ms;
  692. struct timeval elapsed;
  693. struct timeval now;
  694. char *pdevbuf = &data->noncebuf[0];
  695. bool stale;
  696. int count;
  697. cgtime(&now);
  698. timersub(&now, &bitforce->work_start_tv, &elapsed);
  699. bitforce->wait_ms = tv_to_ms(elapsed);
  700. bitforce->polling = true;
  701. if (!fdDev)
  702. goto commerr;
  703. stale = stale_work(work, true);
  704. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  705. {
  706. // We're likely here because of a work restart
  707. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  708. // BFP_QUEUE does not support stopping work at all
  709. if (data->proto == BFP_QUEUE || !stale)
  710. {
  711. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  712. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  713. data->poll_func = 2;
  714. return;
  715. }
  716. }
  717. while (1) {
  718. if (data->already_have_results)
  719. {
  720. data->already_have_results = false;
  721. strcpy(pdevbuf, "COUNT:0");
  722. count = 1;
  723. break;
  724. }
  725. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  726. count = bitforce_zox(thr, cmd);
  727. cgtime(&now);
  728. timersub(&now, &bitforce->work_start_tv, &elapsed);
  729. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  730. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  731. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  732. goto out;
  733. }
  734. if (count > 0)
  735. {
  736. // Check that queue results match the current work
  737. // Also, if there are results from the next work, short-circuit this wait
  738. unsigned char midstate[32], datatail[12];
  739. char *p;
  740. int i;
  741. p = pdevbuf;
  742. for (i = 0; i < count; ++i)
  743. {
  744. p = next_line(p);
  745. hex2bin(midstate, p, 32);
  746. hex2bin(datatail, &p[65], 12);
  747. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  748. break;
  749. }
  750. if (i == count)
  751. {
  752. // Didn't find the one we're waiting on
  753. // Must be extra stuff in the queue results
  754. char *xmid = bin2hex(work->midstate, 32);
  755. char *xdt = bin2hex(&work->data[64], 12);
  756. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  757. bitforce->proc_repr, pdevbuf);
  758. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  759. bitforce->proc_repr, xmid, xdt);
  760. free(xmid);
  761. free(xdt);
  762. count = 0;
  763. }
  764. else
  765. if (i == count - 1)
  766. // Last one found is what we're looking for
  767. {}
  768. else
  769. // We finished the next job too!
  770. data->already_have_results = true;
  771. }
  772. if (!count)
  773. goto noqr;
  774. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  775. break;
  776. data->result_busy_polled = bitforce->wait_ms;
  777. if (stale && data->proto != BFP_QUEUE)
  778. {
  779. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  780. bitforce->proc_repr);
  781. goto out;
  782. }
  783. noqr:
  784. data->result_busy_polled = bitforce->wait_ms;
  785. /* if BFL is throttling, no point checking so quickly */
  786. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  787. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  788. data->poll_func = 2;
  789. return;
  790. }
  791. if (count < 0 && pdevbuf[0] == 'N')
  792. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  793. // At this point, 'count' is:
  794. // negative, in case of some kind of error
  795. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  796. // positive, if at least one job completed successfully
  797. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  798. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  799. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  800. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  801. ++bitforce->hw_errors;
  802. ++hw_errors;
  803. /* If the device truly throttled, it didn't process the job and there
  804. * are no results. But check first, just in case we're wrong about it
  805. * throttling.
  806. */
  807. if (count > 0)
  808. goto out;
  809. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  810. /* Simple timing adjustment. Allow a few polls to cope with
  811. * OS timer delays being variably reliable. wait_ms will
  812. * always equal sleep_ms when we've waited greater than or
  813. * equal to the result return time.*/
  814. delay_time_ms = bitforce->sleep_ms;
  815. if (!data->result_busy_polled)
  816. {
  817. // No busy polls before results received
  818. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  819. // ... due to poll being rather late; ignore it as an anomaly
  820. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  821. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  822. else
  823. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  824. {
  825. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  826. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  827. bitforce->sleep_ms = data->sleep_ms_default;
  828. }
  829. else
  830. {
  831. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  832. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  833. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  834. }
  835. }
  836. else
  837. {
  838. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  839. {
  840. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  841. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  842. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  843. }
  844. else
  845. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  846. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  847. }
  848. /* Work out the average time taken. Float for calculation, uint for display */
  849. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  850. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  851. }
  852. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  853. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  854. bitforce->hw_errors++;
  855. ++hw_errors;
  856. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  857. bitforce_clear_buffer(bitforce);
  858. }
  859. out:
  860. bitforce->polling = false;
  861. job_results_fetched(thr);
  862. return;
  863. commerr:
  864. bitforce_comm_error(thr);
  865. goto out;
  866. }
  867. static
  868. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  869. {
  870. struct cgpu_info *bitforce = thr->cgpu;
  871. struct bitforce_data *data = bitforce->device_data;
  872. uint32_t nonce;
  873. while (1) {
  874. hex2bin((void*)&nonce, pnoncebuf, 4);
  875. nonce = be32toh(nonce);
  876. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  877. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  878. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  879. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  880. bitforce_change_mode(bitforce, BFP_WORK);
  881. }
  882. submit_nonce(thr, work, nonce);
  883. if (strncmp(&pnoncebuf[8], ",", 1))
  884. break;
  885. pnoncebuf += 9;
  886. }
  887. }
  888. static
  889. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  890. {
  891. if (!work)
  892. return false;
  893. if (memcmp(work->midstate, midstate, 32))
  894. return false;
  895. if (memcmp(&work->data[64], datatail, 12))
  896. return false;
  897. char *end;
  898. if (strtol(&buf[90], &end, 10))
  899. bitforce_process_result_nonces(thr, work, &end[1]);
  900. return true;
  901. }
  902. static
  903. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  904. {
  905. struct cgpu_info *bitforce = thr->cgpu;
  906. char midstate[32], datatail[12];
  907. hex2bin((void*)midstate, buf, 32);
  908. hex2bin((void*)datatail, &buf[65], 12);
  909. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  910. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  911. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  912. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  913. {
  914. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  915. ++bitforce->hw_errors;
  916. ++hw_errors;
  917. }
  918. }
  919. static inline
  920. char *next_line(char *in)
  921. {
  922. while (in[0] && (in++)[0] != '\n')
  923. {}
  924. return in;
  925. }
  926. static
  927. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  928. {
  929. struct cgpu_info *bitforce = thr->cgpu;
  930. struct bitforce_data *data = bitforce->device_data;
  931. char *pnoncebuf = &data->noncebuf[0];
  932. int count;
  933. if (!strncasecmp(pnoncebuf, "NO-", 3))
  934. return bitforce->nonces; /* No valid nonce found */
  935. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  936. {
  937. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  938. count = 1;
  939. }
  940. else
  941. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  942. {
  943. count = 0;
  944. pnoncebuf = next_line(pnoncebuf);
  945. while (pnoncebuf[0])
  946. {
  947. bitforce_process_qresult_line(thr, pnoncebuf, work);
  948. ++count;
  949. pnoncebuf = next_line(pnoncebuf);
  950. }
  951. }
  952. else
  953. return 0;
  954. // FIXME: This might have changed in the meantime (new job start, or broken)
  955. return bitforce->nonces * count;
  956. }
  957. static void bitforce_shutdown(struct thr_info *thr)
  958. {
  959. struct cgpu_info *bitforce = thr->cgpu;
  960. int *p_fdDev = &bitforce->device->device_fd;
  961. BFclose(*p_fdDev);
  962. *p_fdDev = 0;
  963. }
  964. static void biforce_thread_enable(struct thr_info *thr)
  965. {
  966. struct cgpu_info *bitforce = thr->cgpu;
  967. bitforce_reinit(bitforce);
  968. }
  969. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  970. {
  971. return bitforce_get_temp(bitforce);
  972. }
  973. static bool bitforce_identify(struct cgpu_info *bitforce)
  974. {
  975. bitforce->flash_led = true;
  976. return true;
  977. }
  978. static bool bitforce_thread_init(struct thr_info *thr)
  979. {
  980. struct cgpu_info *bitforce = thr->cgpu;
  981. unsigned int wait;
  982. struct bitforce_data *data;
  983. struct bitforce_init_data *initdata = bitforce->device_data;
  984. bool sc = initdata->sc;
  985. int xlink_id = 0;
  986. for ( ; bitforce; bitforce = bitforce->next_proc)
  987. {
  988. thr = bitforce->thr[0];
  989. if (unlikely(xlink_id > 30))
  990. {
  991. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  992. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  993. bitforce->reinit_backoff = 1e10;
  994. continue;
  995. }
  996. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  997. bitforce->device_data = data = malloc(sizeof(*data));
  998. *data = (struct bitforce_data){
  999. .xlink_id = xlink_id,
  1000. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1001. .proto = BFP_RANGE,
  1002. .sc = sc,
  1003. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1004. };
  1005. if (sc)
  1006. {
  1007. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1008. data->next_work_ob[8+32+12+8] = '\xAA';
  1009. data->next_work_obs = &data->next_work_ob[7];
  1010. if (bitforce->drv == &bitforce_queue_api)
  1011. {
  1012. bitforce_change_mode(bitforce, BFP_BQUEUE);
  1013. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1014. timer_set_delay_from_now(&thr->tv_poll, 0);
  1015. }
  1016. else
  1017. bitforce_change_mode(bitforce, BFP_QUEUE);
  1018. }
  1019. else
  1020. {
  1021. data->next_work_obs = &data->next_work_ob[0];
  1022. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1023. bitforce_change_mode(bitforce, BFP_WORK);
  1024. /* Initially enable support for nonce range and disable it later if it
  1025. * fails */
  1026. if (opt_bfl_noncerange)
  1027. bitforce_change_mode(bitforce, BFP_RANGE);
  1028. }
  1029. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1030. {}
  1031. }
  1032. bitforce = thr->cgpu;
  1033. free(initdata);
  1034. /* Pause each new thread at least 100ms between initialising
  1035. * so the devices aren't making calls all at the same time. */
  1036. wait = thr->id * MAX_START_DELAY_MS;
  1037. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1038. nmsleep(wait);
  1039. return true;
  1040. }
  1041. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1042. {
  1043. struct bitforce_data *data = cgpu->device_data;
  1044. struct api_data *root = NULL;
  1045. // Warning, access to these is not locked - but we don't really
  1046. // care since hashing performance is way more important than
  1047. // locking access to displaying API debug 'stats'
  1048. // If locking becomes an issue for any of them, use copy_data=true also
  1049. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1050. if (data->proto != BFP_BQUEUE)
  1051. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1052. if (data->temp[0] > 0 && data->temp[1] > 0)
  1053. {
  1054. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1055. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1056. }
  1057. return root;
  1058. }
  1059. void bitforce_poll(struct thr_info *thr)
  1060. {
  1061. struct cgpu_info *bitforce = thr->cgpu;
  1062. struct bitforce_data *data = bitforce->device_data;
  1063. int poll = data->poll_func;
  1064. thr->tv_poll.tv_sec = -1;
  1065. data->poll_func = 0;
  1066. switch (poll)
  1067. {
  1068. case 1:
  1069. bitforce_job_start(thr);
  1070. break;
  1071. case 2:
  1072. bitforce_job_get_results(thr, thr->work);
  1073. break;
  1074. default:
  1075. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1076. }
  1077. }
  1078. static
  1079. char *bitforce_set_device(struct cgpu_info *proc, char *option, char *setting, char *replybuf)
  1080. {
  1081. struct bitforce_data *data = proc->device_data;
  1082. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1083. int fd;
  1084. if (!strcasecmp(option, "help"))
  1085. {
  1086. sprintf(replybuf, "fanmode: range 0-4 (low to fast) or 5 (auto)");
  1087. return replybuf;
  1088. }
  1089. if (!strcasecmp(option, "fanmode"))
  1090. {
  1091. if (!setting || !*setting)
  1092. {
  1093. sprintf(replybuf, "missing fanmode setting");
  1094. return replybuf;
  1095. }
  1096. if (setting[1] || setting[0] < '0' || setting[0] > '5')
  1097. {
  1098. sprintf(replybuf, "invalid fanmode setting");
  1099. return replybuf;
  1100. }
  1101. char cmd[4] = "Z5X";
  1102. cmd[1] = setting[0];
  1103. mutex_lock(mutexp);
  1104. fd = proc->device->device_fd;
  1105. bitforce_cmd1(fd, data->xlink_id, replybuf, 256, cmd);
  1106. mutex_unlock(mutexp);
  1107. return replybuf;
  1108. }
  1109. sprintf(replybuf, "Unknown option: %s", option);
  1110. return replybuf;
  1111. }
  1112. struct device_drv bitforce_drv = {
  1113. .dname = "bitforce",
  1114. .name = "BFL",
  1115. .drv_detect = bitforce_detect,
  1116. .get_api_stats = bitforce_drv_stats,
  1117. .minerloop = minerloop_async,
  1118. .reinit_device = bitforce_reinit,
  1119. .get_statline_before = get_bitforce_statline_before,
  1120. .get_stats = bitforce_get_stats,
  1121. .set_device = bitforce_set_device,
  1122. .identify_device = bitforce_identify,
  1123. .thread_prepare = bitforce_thread_prepare,
  1124. .thread_init = bitforce_thread_init,
  1125. .job_prepare = bitforce_job_prepare,
  1126. .job_start = bitforce_job_start,
  1127. .job_get_results = bitforce_job_get_results,
  1128. .poll = bitforce_poll,
  1129. .job_process_results = bitforce_job_process_results,
  1130. .thread_shutdown = bitforce_shutdown,
  1131. .thread_enable = biforce_thread_enable
  1132. };
  1133. static inline
  1134. void bitforce_set_queue_full(struct thr_info *thr)
  1135. {
  1136. struct cgpu_info *bitforce = thr->cgpu;
  1137. struct bitforce_data *data = bitforce->device_data;
  1138. thr->queue_full = (data->queued + data->ready_to_queue >= BITFORCE_MAX_QUEUED) || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE);
  1139. }
  1140. static
  1141. bool bitforce_send_queue(struct thr_info *thr)
  1142. {
  1143. struct cgpu_info *bitforce = thr->cgpu;
  1144. struct bitforce_data *data = bitforce->device_data;
  1145. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1146. int fd = bitforce->device->device_fd;
  1147. struct work *work;
  1148. if (unlikely(!(fd && data->ready_to_queue)))
  1149. return false;
  1150. char buf[0x100];
  1151. int queued_ok;
  1152. size_t qjs_sz = (32 + 12 + 2);
  1153. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1154. uint8_t qjp[qjp_sz], *qjs;
  1155. qjp[0] = qjp_sz - 1;
  1156. qjp[1] = 0xc1;
  1157. qjp[2] = data->ready_to_queue;
  1158. qjp[qjp_sz - 1] = 0xfe;
  1159. qjs = &qjp[qjp_sz - 1];
  1160. work = thr->work_list->prev;
  1161. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1162. {
  1163. *(--qjs) = 0xaa;
  1164. memcpy(qjs -= 12, work->data + 64, 12);
  1165. memcpy(qjs -= 32, work->midstate, 32);
  1166. *(--qjs) = 45;
  1167. }
  1168. mutex_lock(mutexp);
  1169. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1170. mutex_unlock(mutexp);
  1171. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1172. {
  1173. // Queue full :(
  1174. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1175. bitforce->proc_repr,
  1176. data->ready_to_queue, data->queued);
  1177. thr->queue_full = true;
  1178. return false;
  1179. }
  1180. if (strncasecmp(buf, "OK:QUEUED", 9))
  1181. {
  1182. // TODO: ERR:UNKNOWN COMMAND
  1183. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1184. bitforce->proc_repr,
  1185. data->ready_to_queue, data->queued, buf);
  1186. return false;
  1187. }
  1188. if (!data->queued)
  1189. cgtime(&data->tv_hashmeter_start);
  1190. queued_ok = atoi(&buf[9]);
  1191. data->queued += queued_ok;
  1192. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1193. bitforce->proc_repr,
  1194. queued_ok, data->ready_to_queue, data->queued);
  1195. data->ready_to_queue -= queued_ok;
  1196. thr->queue_full = data->ready_to_queue;
  1197. data->just_flushed = false;
  1198. data->want_to_send_queue = false;
  1199. return true;
  1200. }
  1201. void work_list_del(struct work **head, struct work *work)
  1202. {
  1203. DL_DELETE(*head, work);
  1204. free_work(work);
  1205. }
  1206. static
  1207. bool bitforce_queue_do_results(struct thr_info *thr)
  1208. {
  1209. struct cgpu_info *bitforce = thr->cgpu;
  1210. struct bitforce_data *data = bitforce->device_data;
  1211. int fd = bitforce->device->device_fd;
  1212. int count;
  1213. char *noncebuf = &data->noncebuf[0], *buf, *end;
  1214. unsigned char midstate[32], datatail[12];
  1215. struct work *work, *tmpwork, *thiswork;
  1216. struct timeval tv_now, tv_elapsed;
  1217. if (unlikely(!fd))
  1218. return false;
  1219. count = bitforce_zox(thr, "ZOX");
  1220. if (!count)
  1221. {
  1222. applog(LOG_DEBUG, "%"PRIpreprv": Received 0 queued results on poll", bitforce->proc_repr);
  1223. return true;
  1224. }
  1225. if (unlikely(count < 0))
  1226. {
  1227. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1228. ++bitforce->hw_errors;
  1229. ++hw_errors;
  1230. return false;
  1231. }
  1232. if (unlikely(!thr->work_list))
  1233. {
  1234. applog(LOG_ERR, "%"PRIpreprv": Received %d queued results when there was no queue", bitforce->proc_repr, count);
  1235. ++bitforce->hw_errors;
  1236. ++hw_errors;
  1237. return true;
  1238. }
  1239. count = 0;
  1240. noncebuf = next_line(noncebuf);
  1241. while ((buf = noncebuf)[0])
  1242. {
  1243. if ( (noncebuf = next_line(buf)) )
  1244. noncebuf[-1] = '\0';
  1245. if (strlen(buf) <= 90)
  1246. {
  1247. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1248. continue;
  1249. }
  1250. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", bitforce->proc_repr, buf);
  1251. hex2bin(midstate, buf, 32);
  1252. hex2bin(datatail, &buf[65], 12);
  1253. thiswork = NULL;
  1254. DL_FOREACH(thr->work_list, work)
  1255. {
  1256. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1257. continue;
  1258. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1259. continue;
  1260. thiswork = work;
  1261. break;
  1262. }
  1263. if (unlikely(!thiswork))
  1264. {
  1265. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results", bitforce->proc_repr);
  1266. ++bitforce->hw_errors;
  1267. ++hw_errors;
  1268. goto next_qline;
  1269. }
  1270. ++count;
  1271. if (strtol(&buf[90], &end, 10))
  1272. bitforce_process_result_nonces(thr, work, &end[1]);
  1273. // Queue results are in order, so anything queued prior this is lost
  1274. // Delete all queued work up to, and including, this one
  1275. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1276. {
  1277. DL_DELETE(thr->work_list, work);
  1278. --data->queued;
  1279. if (work == thiswork)
  1280. break;
  1281. }
  1282. next_qline: (void)0;
  1283. }
  1284. bitforce_set_queue_full(thr);
  1285. if ((count < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1286. || (count > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT))
  1287. {
  1288. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1289. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (count ?: 1);
  1290. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1291. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1292. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1293. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1294. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1295. bitforce->proc_repr, count, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1296. }
  1297. else
  1298. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1299. bitforce->proc_repr, count, bitforce->sleep_ms, data->queued);
  1300. cgtime(&tv_now);
  1301. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1302. hashes_done(thr, (uint64_t)bitforce->nonces * count, &tv_elapsed, NULL);
  1303. data->tv_hashmeter_start = tv_now;
  1304. return true;
  1305. }
  1306. static
  1307. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1308. {
  1309. struct cgpu_info *bitforce = thr->cgpu;
  1310. struct bitforce_data *data = bitforce->device_data;
  1311. bool rv, ndq;
  1312. bitforce_set_queue_full(thr);
  1313. rv = !thr->queue_full;
  1314. if (rv)
  1315. {
  1316. DL_APPEND(thr->work_list, work);
  1317. ++data->ready_to_queue;
  1318. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1319. bitforce->proc_repr,
  1320. (unsigned)BITFORCE_MAX_QUEUED, data->ready_to_queue, data->queued);
  1321. bitforce_set_queue_full(thr);
  1322. }
  1323. else
  1324. if (!data->ready_to_queue)
  1325. return rv;
  1326. ndq = !data->queued;
  1327. if ((ndq) // Device is idle
  1328. || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE) // ...or 5 items ready to go
  1329. || (thr->queue_full) // ...or done filling queue
  1330. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1331. )
  1332. {
  1333. if (!bitforce_send_queue(thr))
  1334. {
  1335. // Problem sending queue, retry again in a few seconds
  1336. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1337. ++bitforce->hw_errors;
  1338. ++hw_errors;
  1339. data->want_to_send_queue = true;
  1340. }
  1341. }
  1342. return rv;
  1343. }
  1344. static
  1345. void bitforce_queue_flush(struct thr_info *thr)
  1346. {
  1347. struct cgpu_info *bitforce = thr->cgpu;
  1348. struct bitforce_data *data = bitforce->device_data;
  1349. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1350. int fd = bitforce->device->device_fd;
  1351. char buf[100];
  1352. unsigned flushed;
  1353. mutex_lock(mutexp);
  1354. bitforce_cmd1(fd, data->xlink_id, buf, sizeof(buf), "ZQX");
  1355. mutex_unlock(mutexp);
  1356. if (unlikely(strncasecmp(buf, "OK:FLUSHED", 10)))
  1357. {
  1358. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1359. flushed = 0;
  1360. }
  1361. else
  1362. flushed = atoi(&buf[10]);
  1363. data->queued -= flushed;
  1364. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1365. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1366. flushed += data->ready_to_queue;
  1367. data->ready_to_queue = 0;
  1368. while (flushed--)
  1369. work_list_del(&thr->work_list, thr->work_list->prev);
  1370. bitforce_set_queue_full(thr);
  1371. data->just_flushed = true;
  1372. data->want_to_send_queue = false;
  1373. bitforce_queue_do_results(thr);
  1374. }
  1375. static
  1376. void bitforce_queue_poll(struct thr_info *thr)
  1377. {
  1378. struct cgpu_info *bitforce = thr->cgpu;
  1379. struct bitforce_data *data = bitforce->device_data;
  1380. unsigned long sleep_us;
  1381. if (data->queued)
  1382. bitforce_queue_do_results(thr);
  1383. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  1384. if (data->want_to_send_queue)
  1385. if (!bitforce_send_queue(thr))
  1386. if (!data->queued)
  1387. {
  1388. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  1389. ++bitforce->hw_errors;
  1390. ++hw_errors;
  1391. sleep_us = 1000000;
  1392. }
  1393. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  1394. }
  1395. struct device_drv bitforce_queue_api = {
  1396. .dname = "bitforce_queue",
  1397. .name = "BFL",
  1398. .minerloop = minerloop_queue,
  1399. .reinit_device = bitforce_reinit,
  1400. .get_statline_before = get_bitforce_statline_before,
  1401. .get_api_stats = bitforce_drv_stats,
  1402. .get_stats = bitforce_get_stats,
  1403. .set_device = bitforce_set_device,
  1404. .identify_device = bitforce_identify,
  1405. .thread_prepare = bitforce_thread_prepare,
  1406. .thread_init = bitforce_thread_init,
  1407. .queue_append = bitforce_queue_append,
  1408. .queue_flush = bitforce_queue_flush,
  1409. .poll = bitforce_queue_poll,
  1410. .thread_shutdown = bitforce_shutdown,
  1411. .thread_enable = biforce_thread_enable
  1412. };