driver-bitforce.c 65 KB

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