driver-bitforce.c 45 KB

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