driver-bitforce.c 64 KB

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