driver-bitforce.c 63 KB

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