driver-bitforce.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  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
  547. void __bitforce_clear_buffer(struct cgpu_info * const dev)
  548. {
  549. char pdevbuf[0x100];
  550. int count = 0;
  551. do {
  552. pdevbuf[0] = '\0';
  553. bitforce_gets(pdevbuf, sizeof(pdevbuf), dev);
  554. } while (pdevbuf[0] && (++count < 10));
  555. }
  556. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  557. {
  558. struct cgpu_info * const dev = bitforce->device;
  559. struct bitforce_data * const devdata = dev->device_data;
  560. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  561. mutex_lock(mutexp);
  562. if (devdata->is_open)
  563. {
  564. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  565. __bitforce_clear_buffer(bitforce);
  566. }
  567. mutex_unlock(mutexp);
  568. }
  569. void work_list_del(struct work **head, struct work *);
  570. void bitforce_reinit(struct cgpu_info *bitforce)
  571. {
  572. struct cgpu_info * const dev = bitforce->device;
  573. struct bitforce_data * const devdata = dev->device_data;
  574. struct bitforce_data *data = bitforce->device_data;
  575. struct thr_info *thr = bitforce->thr[0];
  576. struct bitforce_proc_data *procdata = thr->cgpu_data;
  577. const char *devpath = bitforce->device_path;
  578. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  579. int retries = 0;
  580. char pdevbuf[0x100];
  581. char *s;
  582. if (!procdata->handles_board)
  583. return;
  584. mutex_lock(mutexp);
  585. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  586. if (devdata->is_open)
  587. {
  588. bitforce_close(bitforce);
  589. cgsleep_ms(5000);
  590. }
  591. bitforce_open(bitforce);
  592. if (unlikely(!devdata->is_open)) {
  593. mutex_unlock(mutexp);
  594. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  595. return;
  596. }
  597. __bitforce_clear_buffer(bitforce);
  598. do {
  599. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZGX", 3);
  600. if (unlikely(!pdevbuf[0])) {
  601. mutex_unlock(mutexp);
  602. bitforce_close(bitforce);
  603. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  604. return;
  605. }
  606. if (retries++)
  607. cgsleep_ms(10);
  608. } while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  609. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  610. mutex_unlock(mutexp);
  611. bitforce_close(bitforce);
  612. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  613. return;
  614. }
  615. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  616. s[0] = '\0';
  617. free((void*)bitforce->name);
  618. bitforce->name = strdup(pdevbuf + 7);
  619. }
  620. bitforce->sleep_ms = data->sleep_ms_default;
  621. if (bitforce->drv == &bitforce_queue_api)
  622. {
  623. struct work *work, *tmp;
  624. timer_set_delay_from_now(&thr->tv_poll, 0);
  625. notifier_wake(thr->notifier);
  626. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZQX", 3);
  627. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  628. work_list_del(&thr->work_list, work);
  629. data->queued = 0;
  630. data->ready_to_queue = 0;
  631. data->already_have_results = false;
  632. data->just_flushed = true;
  633. thr->queue_full = false;
  634. }
  635. mutex_unlock(mutexp);
  636. }
  637. static void bitforce_flash_led(struct cgpu_info *bitforce)
  638. {
  639. struct cgpu_info * const dev = bitforce->device;
  640. struct bitforce_data * const devdata = dev->device_data;
  641. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  642. if (unlikely(!devdata->is_open))
  643. return;
  644. /* Do not try to flash the led if we're polling for a result to
  645. * minimise the chance of interleaved results */
  646. if (bitforce->polling)
  647. return;
  648. /* It is not critical flashing the led so don't get stuck if we
  649. * can't grab the mutex here */
  650. if (mutex_trylock(mutexp))
  651. return;
  652. char pdevbuf[0x100];
  653. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZMX", 3);
  654. /* Once we've tried - don't do it until told to again */
  655. bitforce->flash_led = false;
  656. /* However, this stops anything else getting a reply
  657. * So best to delay any other access to the BFL */
  658. cgsleep_ms(4000);
  659. mutex_unlock(mutexp);
  660. return; // nothing is returned by the BFL
  661. }
  662. static
  663. float my_strtof(const char *nptr, char **endptr)
  664. {
  665. float f = strtof(nptr, endptr);
  666. /* Cope with older software that breaks and reads nonsense
  667. * values */
  668. if (f > 100)
  669. f = strtod(nptr, endptr);
  670. return f;
  671. }
  672. static
  673. void set_float_if_gt_zero(float *var, float value)
  674. {
  675. if (value > 0)
  676. *var = value;
  677. }
  678. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  679. {
  680. struct cgpu_info * const dev = bitforce->device;
  681. struct bitforce_data * const devdata = dev->device_data;
  682. struct bitforce_data *data = bitforce->device_data;
  683. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  684. char pdevbuf[0x40];
  685. char voltbuf[0x40];
  686. char *s;
  687. struct cgpu_info *chip_cgpu;
  688. if (unlikely(!devdata->is_open))
  689. return false;
  690. /* Do not try to get the temperature if we're polling for a result to
  691. * minimise the chance of interleaved results */
  692. if (bitforce->polling)
  693. return true;
  694. // Flash instead of Temp - doing both can be too slow
  695. if (bitforce->flash_led) {
  696. bitforce_flash_led(bitforce);
  697. return true;
  698. }
  699. /* It is not critical getting temperature so don't get stuck if we
  700. * can't grab the mutex here */
  701. if (mutex_trylock(mutexp))
  702. return false;
  703. if (data->style != BFS_FPGA)
  704. {
  705. if (unlikely(!data->probed))
  706. {
  707. bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "Z9X", 3);
  708. if (strncasecmp(voltbuf, "ERR", 3))
  709. {
  710. data->supports_fanspeed = true;
  711. bitforce->set_device_funcs = bitforce_set_device_funcs;
  712. }
  713. data->probed = true;
  714. }
  715. bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "ZTX", 3);
  716. }
  717. bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZLX", 3);
  718. mutex_unlock(mutexp);
  719. if (data->style != BFS_FPGA && likely(voltbuf[0]))
  720. {
  721. // Process voltage info
  722. // "NNNxxx,NNNxxx,NNNxxx"
  723. int n = 1;
  724. for (char *p = voltbuf; p[0]; ++p)
  725. if (p[0] == ',')
  726. ++n;
  727. long *out = malloc(sizeof(long) * n);
  728. if (!out)
  729. goto skipvolts;
  730. n = 0;
  731. char *saveptr, *v;
  732. for (v = strtok_r(voltbuf, ",", &saveptr); v; v = strtok_r(NULL, ",", &saveptr))
  733. out[n++] = strtol(v, NULL, 10);
  734. data->volts_count = 0;
  735. free(data->volts);
  736. data->volts = out;
  737. data->volts_count = n;
  738. }
  739. skipvolts:
  740. if (unlikely(!pdevbuf[0])) {
  741. struct thr_info *thr = bitforce->thr[0];
  742. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  743. inc_hw_errors_only(thr);
  744. return false;
  745. }
  746. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  747. float temp = my_strtof(s + 1, &s);
  748. set_float_if_gt_zero(&data->temp[0], temp);
  749. for ( ; s[0]; ++s)
  750. {
  751. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  752. {
  753. float temp2 = my_strtof(s + 1, &s);
  754. set_float_if_gt_zero(&data->temp[1], temp2);
  755. if (temp2 > temp)
  756. temp = temp2;
  757. }
  758. }
  759. if (temp > 0)
  760. {
  761. chip_cgpu = bitforce;
  762. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  763. chip_cgpu->temp = temp;
  764. }
  765. } else {
  766. struct thr_info *thr = bitforce->thr[0];
  767. /* Use the temperature monitor as a kind of watchdog for when
  768. * our responses are out of sync and flush the buffer to
  769. * hopefully recover */
  770. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  771. dev_error(bitforce, REASON_DEV_THROTTLE);
  772. /* Count throttling episodes as hardware errors */
  773. inc_hw_errors_only(thr);
  774. bitforce_clear_buffer(bitforce);
  775. return false;
  776. }
  777. return true;
  778. }
  779. static inline
  780. void dbg_block_data(struct cgpu_info *bitforce)
  781. {
  782. if (!opt_debug)
  783. return;
  784. struct bitforce_data *data = bitforce->device_data;
  785. char s[89];
  786. bin2hex(s, &data->next_work_ob[8], 44);
  787. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  788. }
  789. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  790. static
  791. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  792. {
  793. struct cgpu_info *bitforce = thr->cgpu;
  794. struct bitforce_data *data = bitforce->device_data;
  795. unsigned char *ob_ms = &data->next_work_ob[8];
  796. unsigned char *ob_dt = &ob_ms[32];
  797. // If polling job_start, cancel it
  798. if (data->poll_func == 1)
  799. {
  800. thr->tv_poll.tv_sec = -1;
  801. data->poll_func = 0;
  802. }
  803. memcpy(ob_ms, work->midstate, 32);
  804. memcpy(ob_dt, work->data + 64, 12);
  805. switch (data->proto)
  806. {
  807. case BFP_BQUEUE:
  808. quithere(1, "%"PRIpreprv": Impossible BFP_BQUEUE", bitforce->proc_repr);
  809. case BFP_PQUEUE:
  810. quithere(1, "%"PRIpreprv": Impossible BFP_PQUEUE", bitforce->proc_repr);
  811. case BFP_RANGE:
  812. {
  813. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  814. ob_nonce[0] = htobe32(work->blk.nonce);
  815. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  816. // FIXME: if nonce range fails... we didn't increment enough
  817. work->blk.nonce += bitforce->nonces + 1;
  818. break;
  819. }
  820. case BFP_WORK:
  821. work->blk.nonce = 0xffffffff;
  822. }
  823. return true;
  824. }
  825. static
  826. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  827. {
  828. struct bitforce_data *data = bitforce->device_data;
  829. if (data->proto == proto)
  830. return;
  831. if (data->proto == BFP_RANGE)
  832. {
  833. bitforce->nonces = 0xffffffff;
  834. bitforce->sleep_ms *= 5;
  835. data->sleep_ms_default *= 5;
  836. switch (proto)
  837. {
  838. case BFP_WORK:
  839. data->next_work_cmd = "ZDX";
  840. default:
  841. ;
  842. }
  843. if (data->style != BFS_FPGA)
  844. {
  845. // "S|---------- MidState ----------||-DataTail-|E"
  846. data->next_work_ob[7] = 45;
  847. data->next_work_ob[8+32+12] = '\xAA';
  848. data->next_work_obsz = 46;
  849. }
  850. else
  851. {
  852. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  853. memset(&data->next_work_ob[8+32+12], '>', 8);
  854. data->next_work_obsz = 60;
  855. }
  856. }
  857. else
  858. if (proto == BFP_RANGE)
  859. {
  860. /* Split work up into 1/5th nonce ranges */
  861. bitforce->nonces = 0x33333332;
  862. bitforce->sleep_ms /= 5;
  863. data->sleep_ms_default /= 5;
  864. data->next_work_cmd = "ZPX";
  865. if (data->style != BFS_FPGA)
  866. {
  867. data->next_work_ob[7] = 53;
  868. data->next_work_obsz = 54;
  869. }
  870. else
  871. data->next_work_obsz = 68;
  872. }
  873. data->proto = proto;
  874. bitforce->kname = protonames[proto];
  875. }
  876. static
  877. void bitforce_job_start(struct thr_info *thr)
  878. {
  879. struct cgpu_info *bitforce = thr->cgpu;
  880. struct cgpu_info * const dev = bitforce->device;
  881. struct bitforce_data * const devdata = dev->device_data;
  882. struct bitforce_data *data = bitforce->device_data;
  883. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  884. unsigned char *ob = data->next_work_obs;
  885. char pdevbuf[0x100];
  886. struct timeval tv_now;
  887. data->result_busy_polled = 0;
  888. if (data->queued)
  889. {
  890. uint32_t delay;
  891. // get_results collected more accurate job start time
  892. mt_job_transition(thr);
  893. job_start_complete(thr);
  894. data->queued = 0;
  895. delay = (uint32_t)bitforce->sleep_ms * 1000;
  896. if (unlikely(data->already_have_results))
  897. delay = 0;
  898. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  899. return;
  900. }
  901. if (unlikely(!devdata->is_open))
  902. goto commerr;
  903. re_send:
  904. mutex_lock(mutexp);
  905. bitforce_cmd2(bitforce, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  906. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  907. mutex_unlock(mutexp);
  908. cgtime(&tv_now);
  909. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  910. data->poll_func = 1;
  911. return;
  912. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  913. mutex_unlock(mutexp);
  914. switch (data->proto)
  915. {
  916. case BFP_RANGE:
  917. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  918. bitforce_change_mode(bitforce, BFP_WORK);
  919. goto re_send;
  920. default:
  921. ;
  922. }
  923. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  924. goto commerr;
  925. }
  926. mt_job_transition(thr);
  927. mutex_unlock(mutexp);
  928. dbg_block_data(bitforce);
  929. cgtime(&tv_now);
  930. bitforce->work_start_tv = tv_now;
  931. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  932. job_start_complete(thr);
  933. return;
  934. commerr:
  935. bitforce_comm_error(thr);
  936. job_start_abort(thr, true);
  937. }
  938. static char _discardedbuf[0x10];
  939. static
  940. int bitforce_zox(struct thr_info *thr, const char *cmd)
  941. {
  942. struct cgpu_info *bitforce = thr->cgpu;
  943. struct bitforce_data *data = bitforce->device_data;
  944. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  945. char *pdevbuf = &data->noncebuf[0];
  946. int count;
  947. mutex_lock(mutexp);
  948. bitforce_cmd1b(bitforce, pdevbuf, sizeof(data->noncebuf), cmd, 3);
  949. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  950. bitforce_gets(pdevbuf, sizeof(data->noncebuf), bitforce);
  951. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  952. {
  953. count = atoi(&pdevbuf[6]);
  954. size_t cls = strlen(pdevbuf);
  955. char *pmorebuf = &pdevbuf[cls];
  956. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  957. if (count && data->queued)
  958. cgtime(&bitforce->work_start_tv);
  959. while (true)
  960. {
  961. bitforce_gets(pmorebuf, szleft, bitforce);
  962. if (!strncasecmp(pmorebuf, "OK", 2))
  963. {
  964. pmorebuf[0] = '\0'; // process expects only results
  965. break;
  966. }
  967. sz = strlen(pmorebuf);
  968. if (!sz)
  969. {
  970. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  971. break;
  972. }
  973. szleft -= sz;
  974. pmorebuf += sz;
  975. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  976. {
  977. // Out of buffer space somehow :(
  978. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  979. pmorebuf = _discardedbuf;
  980. szleft = sizeof(_discardedbuf);
  981. }
  982. }
  983. }
  984. else
  985. count = -1;
  986. mutex_unlock(mutexp);
  987. return count;
  988. }
  989. static inline char *next_line(char *);
  990. static
  991. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  992. {
  993. struct cgpu_info *bitforce = thr->cgpu;
  994. struct cgpu_info * const dev = bitforce->device;
  995. struct bitforce_data * const devdata = dev->device_data;
  996. struct bitforce_data *data = bitforce->device_data;
  997. unsigned int delay_time_ms;
  998. struct timeval elapsed;
  999. struct timeval now;
  1000. char *pdevbuf = &data->noncebuf[0];
  1001. bool stale;
  1002. int count;
  1003. cgtime(&now);
  1004. timersub(&now, &bitforce->work_start_tv, &elapsed);
  1005. bitforce->wait_ms = tv_to_ms(elapsed);
  1006. bitforce->polling = true;
  1007. if (unlikely(!devdata->is_open))
  1008. goto commerr;
  1009. stale = stale_work(work, true);
  1010. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  1011. {
  1012. // We're likely here because of a work restart
  1013. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  1014. if (!stale)
  1015. {
  1016. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  1017. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  1018. data->poll_func = 2;
  1019. return;
  1020. }
  1021. }
  1022. while (1) {
  1023. if (data->already_have_results)
  1024. {
  1025. data->already_have_results = false;
  1026. strcpy(pdevbuf, "COUNT:0");
  1027. count = 1;
  1028. break;
  1029. }
  1030. const char * const cmd = "ZFX";
  1031. count = bitforce_zox(thr, cmd);
  1032. cgtime(&now);
  1033. timersub(&now, &bitforce->work_start_tv, &elapsed);
  1034. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  1035. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  1036. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  1037. goto out;
  1038. }
  1039. if (count > 0)
  1040. {
  1041. // Check that queue results match the current work
  1042. // Also, if there are results from the next work, short-circuit this wait
  1043. unsigned char midstate[32], datatail[12];
  1044. char *p;
  1045. int i;
  1046. p = pdevbuf;
  1047. for (i = 0; i < count; ++i)
  1048. {
  1049. p = next_line(p);
  1050. hex2bin(midstate, p, 32);
  1051. hex2bin(datatail, &p[65], 12);
  1052. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  1053. break;
  1054. }
  1055. if (i == count)
  1056. {
  1057. // Didn't find the one we're waiting on
  1058. // Must be extra stuff in the queue results
  1059. char xmid[65];
  1060. char xdt[25];
  1061. bin2hex(xmid, work->midstate, 32);
  1062. bin2hex(xdt, &work->data[64], 12);
  1063. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  1064. bitforce->proc_repr, pdevbuf);
  1065. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  1066. bitforce->proc_repr, xmid, xdt);
  1067. count = 0;
  1068. }
  1069. else
  1070. if (i == count - 1)
  1071. // Last one found is what we're looking for
  1072. {}
  1073. else
  1074. // We finished the next job too!
  1075. data->already_have_results = true;
  1076. }
  1077. if (!count)
  1078. goto noqr;
  1079. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  1080. break;
  1081. data->result_busy_polled = bitforce->wait_ms;
  1082. if (stale)
  1083. {
  1084. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  1085. bitforce->proc_repr);
  1086. goto out;
  1087. }
  1088. noqr:
  1089. data->result_busy_polled = bitforce->wait_ms;
  1090. /* if BFL is throttling, no point checking so quickly */
  1091. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  1092. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  1093. data->poll_func = 2;
  1094. return;
  1095. }
  1096. if (count < 0 && pdevbuf[0] == 'N')
  1097. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  1098. // At this point, 'count' is:
  1099. // negative, in case of some kind of error
  1100. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  1101. // positive, if at least one job completed successfully
  1102. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  1103. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  1104. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  1105. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  1106. inc_hw_errors_only(thr);
  1107. /* If the device truly throttled, it didn't process the job and there
  1108. * are no results. But check first, just in case we're wrong about it
  1109. * throttling.
  1110. */
  1111. if (count > 0)
  1112. goto out;
  1113. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  1114. /* Simple timing adjustment. Allow a few polls to cope with
  1115. * OS timer delays being variably reliable. wait_ms will
  1116. * always equal sleep_ms when we've waited greater than or
  1117. * equal to the result return time.*/
  1118. delay_time_ms = bitforce->sleep_ms;
  1119. if (!data->result_busy_polled)
  1120. {
  1121. // No busy polls before results received
  1122. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  1123. // ... due to poll being rather late; ignore it as an anomaly
  1124. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  1125. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  1126. else
  1127. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  1128. {
  1129. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  1130. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  1131. bitforce->sleep_ms = data->sleep_ms_default;
  1132. }
  1133. else
  1134. {
  1135. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  1136. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  1137. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  1138. }
  1139. }
  1140. else
  1141. {
  1142. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  1143. {
  1144. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  1145. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  1146. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  1147. }
  1148. else
  1149. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  1150. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  1151. }
  1152. /* Work out the average time taken. Float for calculation, uint for display */
  1153. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  1154. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  1155. }
  1156. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  1157. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  1158. inc_hw_errors_only(thr);
  1159. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  1160. bitforce_clear_buffer(bitforce);
  1161. }
  1162. out:
  1163. bitforce->polling = false;
  1164. job_results_fetched(thr);
  1165. return;
  1166. commerr:
  1167. bitforce_comm_error(thr);
  1168. goto out;
  1169. }
  1170. static
  1171. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  1172. {
  1173. struct cgpu_info *bitforce = thr->cgpu;
  1174. struct bitforce_data *data = bitforce->device_data;
  1175. uint32_t nonce;
  1176. while (1) {
  1177. hex2bin((void*)&nonce, pnoncebuf, 4);
  1178. nonce = be32toh(nonce);
  1179. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  1180. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  1181. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  1182. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  1183. bitforce_change_mode(bitforce, BFP_WORK);
  1184. }
  1185. submit_nonce(thr, work, nonce);
  1186. if (strncmp(&pnoncebuf[8], ",", 1))
  1187. break;
  1188. pnoncebuf += 9;
  1189. }
  1190. }
  1191. static
  1192. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  1193. {
  1194. if (!work)
  1195. return false;
  1196. if (memcmp(work->midstate, midstate, 32))
  1197. return false;
  1198. if (memcmp(&work->data[64], datatail, 12))
  1199. return false;
  1200. char *end;
  1201. if (strtol(&buf[90], &end, 10))
  1202. bitforce_process_result_nonces(thr, work, &end[1]);
  1203. return true;
  1204. }
  1205. static
  1206. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  1207. {
  1208. struct cgpu_info *bitforce = thr->cgpu;
  1209. char midstate[32], datatail[12];
  1210. hex2bin((void*)midstate, buf, 32);
  1211. hex2bin((void*)datatail, &buf[65], 12);
  1212. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  1213. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  1214. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  1215. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  1216. {
  1217. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  1218. inc_hw_errors_only(thr);
  1219. }
  1220. }
  1221. static inline
  1222. char *next_line(char *in)
  1223. {
  1224. while (in[0] && (in++)[0] != '\n')
  1225. {}
  1226. return in;
  1227. }
  1228. static
  1229. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  1230. {
  1231. struct cgpu_info *bitforce = thr->cgpu;
  1232. struct bitforce_data *data = bitforce->device_data;
  1233. char *pnoncebuf = &data->noncebuf[0];
  1234. int count;
  1235. if (!strncasecmp(pnoncebuf, "NO-", 3))
  1236. return bitforce->nonces; /* No valid nonce found */
  1237. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  1238. {
  1239. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  1240. count = 1;
  1241. }
  1242. else
  1243. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  1244. {
  1245. count = 0;
  1246. pnoncebuf = next_line(pnoncebuf);
  1247. while (pnoncebuf[0])
  1248. {
  1249. bitforce_process_qresult_line(thr, pnoncebuf, work);
  1250. ++count;
  1251. pnoncebuf = next_line(pnoncebuf);
  1252. }
  1253. }
  1254. else
  1255. return 0;
  1256. // FIXME: This might have changed in the meantime (new job start, or broken)
  1257. return bitforce->nonces * count;
  1258. }
  1259. static void bitforce_shutdown(struct thr_info *thr)
  1260. {
  1261. struct cgpu_info *bitforce = thr->cgpu;
  1262. bitforce_close(bitforce);
  1263. }
  1264. static void biforce_thread_enable(struct thr_info *thr)
  1265. {
  1266. struct cgpu_info *bitforce = thr->cgpu;
  1267. bitforce_reinit(bitforce);
  1268. }
  1269. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  1270. {
  1271. struct bitforce_proc_data *procdata = bitforce->thr[0]->cgpu_data;
  1272. if (!procdata->handles_board)
  1273. return true;
  1274. return bitforce_get_temp(bitforce);
  1275. }
  1276. static bool bitforce_identify(struct cgpu_info *bitforce)
  1277. {
  1278. bitforce->flash_led = true;
  1279. return true;
  1280. }
  1281. static bool bitforce_thread_init(struct thr_info *thr)
  1282. {
  1283. struct cgpu_info *bitforce = thr->cgpu;
  1284. unsigned int wait;
  1285. struct bitforce_data *data;
  1286. struct bitforce_proc_data *procdata;
  1287. struct bitforce_init_data *initdata = bitforce->device_data;
  1288. const enum bitforce_style style = initdata->style;
  1289. int xlink_id = 0, boardno = 0;
  1290. struct bitforce_proc_data *first_on_this_board;
  1291. char buf[100];
  1292. for ( ; bitforce; bitforce = bitforce->next_proc)
  1293. {
  1294. thr = bitforce->thr[0];
  1295. if (unlikely(xlink_id > 30))
  1296. {
  1297. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  1298. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  1299. bitforce->reinit_backoff = 1e10;
  1300. continue;
  1301. }
  1302. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  1303. bitforce->device_data = data = malloc(sizeof(*data));
  1304. *data = (struct bitforce_data){
  1305. .lowlif = &bfllif_vcom,
  1306. .xlink_id = xlink_id,
  1307. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1308. .proto = BFP_RANGE,
  1309. .style = style,
  1310. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1311. .parallel = abs(initdata->parallels[boardno]),
  1312. .parallel_protocol = (initdata->parallels[boardno] != -1),
  1313. };
  1314. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1315. *procdata = (struct bitforce_proc_data){
  1316. .handles_board = true,
  1317. .cgpu = bitforce,
  1318. };
  1319. if (style != BFS_FPGA)
  1320. {
  1321. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1322. data->next_work_ob[8+32+12+8] = '\xAA';
  1323. data->next_work_obs = &data->next_work_ob[7];
  1324. switch (style)
  1325. {
  1326. case BFS_FPGA: // impossible
  1327. case BFS_65NM:
  1328. data->max_queue_at_once = BITFORCE_MAX_BQUEUE_AT_ONCE_65NM;
  1329. break;
  1330. case BFS_28NM:
  1331. data->max_queue_at_once = BITFORCE_MAX_BQUEUE_AT_ONCE_28NM;
  1332. }
  1333. if (bitforce->drv == &bitforce_queue_api)
  1334. {
  1335. bitforce_change_mode(bitforce, data->parallel_protocol ? BFP_PQUEUE : BFP_BQUEUE);
  1336. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1337. timer_set_delay_from_now(&thr->tv_poll, 0);
  1338. data->queued_max = data->parallel * 2;
  1339. if (data->queued_max < BITFORCE_MIN_QUEUED_MAX)
  1340. data->queued_max = BITFORCE_MIN_QUEUED_MAX;
  1341. if (data->queued_max > BITFORCE_MAX_QUEUED_MAX)
  1342. data->queued_max = BITFORCE_MAX_QUEUED_MAX;
  1343. }
  1344. else
  1345. bitforce_change_mode(bitforce, BFP_WORK);
  1346. // Clear job queue to start fresh; ignore response
  1347. bitforce_cmd1b(bitforce, buf, sizeof(buf), "ZQX", 3);
  1348. }
  1349. else
  1350. {
  1351. data->next_work_obs = &data->next_work_ob[0];
  1352. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1353. bitforce_change_mode(bitforce, BFP_WORK);
  1354. /* Initially enable support for nonce range and disable it later if it
  1355. * fails */
  1356. if (opt_bfl_noncerange)
  1357. bitforce_change_mode(bitforce, BFP_RANGE);
  1358. }
  1359. bitforce->status = LIFE_INIT2;
  1360. first_on_this_board = procdata;
  1361. for (int proc = 1; proc < data->parallel; ++proc)
  1362. {
  1363. bitforce = bitforce->next_proc;
  1364. assert(bitforce);
  1365. thr = bitforce->thr[0];
  1366. thr->queue_full = true;
  1367. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1368. *procdata = *first_on_this_board;
  1369. procdata->handles_board = false;
  1370. procdata->cgpu = bitforce;
  1371. bitforce->device_data = data;
  1372. bitforce->status = LIFE_INIT2;
  1373. bitforce->kname = first_on_this_board->cgpu->kname;
  1374. }
  1375. applog(LOG_DEBUG, "%s: Board %d: %"PRIpreprv"-%"PRIpreprv, bitforce->dev_repr, boardno, first_on_this_board->cgpu->proc_repr, bitforce->proc_repr);
  1376. ++boardno;
  1377. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1378. {}
  1379. }
  1380. // 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
  1381. bitforce = thr->cgpu;
  1382. free(initdata->parallels);
  1383. free(initdata);
  1384. /* Pause each new thread at least 100ms between initialising
  1385. * so the devices aren't making calls all at the same time. */
  1386. wait = thr->id * MAX_START_DELAY_MS;
  1387. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1388. cgsleep_ms(wait);
  1389. if (unlikely(!bitforce_open(bitforce)))
  1390. {
  1391. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  1392. return false;
  1393. }
  1394. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  1395. if (style != BFS_FPGA)
  1396. {
  1397. // Clear results queue last, to start fresh; ignore response
  1398. for (bitforce = bitforce->device; bitforce; bitforce = bitforce->next_proc)
  1399. bitforce_zox(thr, "ZOX");
  1400. }
  1401. return true;
  1402. }
  1403. #ifdef HAVE_CURSES
  1404. static
  1405. void bitforce_tui_wlogprint_choices(struct cgpu_info *cgpu)
  1406. {
  1407. struct bitforce_data *data = cgpu->device_data;
  1408. if (data->supports_fanspeed)
  1409. wlogprint("[F]an control ");
  1410. }
  1411. static
  1412. const char *bitforce_tui_handle_choice(struct cgpu_info *cgpu, int input)
  1413. {
  1414. struct bitforce_data *data = cgpu->device_data;
  1415. pthread_mutex_t *mutexp;
  1416. static char replybuf[0x100];
  1417. if (!data->supports_fanspeed)
  1418. return NULL;
  1419. switch (input)
  1420. {
  1421. case 'f': case 'F':
  1422. {
  1423. int fanspeed;
  1424. char *intvar;
  1425. intvar = curses_input("Set fan speed (range 0-5 for low to fast or 9 for auto)");
  1426. if (!intvar)
  1427. return "Invalid fan speed\n";
  1428. fanspeed = atoi(intvar);
  1429. free(intvar);
  1430. if ((fanspeed < 0 || fanspeed > 5) && fanspeed != 9)
  1431. return "Invalid fan speed\n";
  1432. char cmd[4] = "Z0X";
  1433. cmd[1] += fanspeed;
  1434. mutexp = &cgpu->device->device_mutex;
  1435. mutex_lock(mutexp);
  1436. bitforce_cmd1b(cgpu, replybuf, sizeof(replybuf), cmd, 3);
  1437. mutex_unlock(mutexp);
  1438. return replybuf;
  1439. }
  1440. }
  1441. return NULL;
  1442. }
  1443. static
  1444. void bitforce_wlogprint_status(struct cgpu_info *cgpu)
  1445. {
  1446. struct bitforce_data *data = cgpu->device_data;
  1447. if (data->temp[0] > 0 && data->temp[1] > 0)
  1448. wlogprint("Temperatures: %4.1fC %4.1fC\n", data->temp[0], data->temp[1]);
  1449. if (data->volts_count)
  1450. {
  1451. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  1452. size_t sz = (data->volts_count * 10) + 1;
  1453. char buf[sz];
  1454. char *s = buf;
  1455. int rv = 0;
  1456. for (int i = 0; i < data->volts_count; ++i)
  1457. {
  1458. long v = data->volts[i];
  1459. _SNP("%ld.%03d / ", v / 1000, (int)(v % 1000));
  1460. }
  1461. if (rv >= 3 && s[-2] == '/')
  1462. s[-3] = '\0';
  1463. wlogprint("Voltages: %s\n", buf);
  1464. }
  1465. }
  1466. #endif
  1467. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1468. {
  1469. struct bitforce_data *data = cgpu->device_data;
  1470. struct api_data *root = NULL;
  1471. // Warning, access to these is not locked - but we don't really
  1472. // care since hashing performance is way more important than
  1473. // locking access to displaying API debug 'stats'
  1474. // If locking becomes an issue for any of them, use copy_data=true also
  1475. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1476. if (data->proto != BFP_BQUEUE && data->proto != BFP_PQUEUE)
  1477. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1478. if (data->temp[0] > 0 && data->temp[1] > 0)
  1479. {
  1480. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1481. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1482. }
  1483. for (int i = 0; i < data->volts_count; ++i)
  1484. {
  1485. float voltage = data->volts[i];
  1486. char key[] = "VoltageNN";
  1487. snprintf(&key[7], 3, "%d", i);
  1488. voltage /= 1e3;
  1489. root = api_add_volts(root, key, &voltage, true);
  1490. }
  1491. return root;
  1492. }
  1493. void bitforce_poll(struct thr_info *thr)
  1494. {
  1495. struct cgpu_info *bitforce = thr->cgpu;
  1496. struct bitforce_data *data = bitforce->device_data;
  1497. int poll = data->poll_func;
  1498. thr->tv_poll.tv_sec = -1;
  1499. data->poll_func = 0;
  1500. switch (poll)
  1501. {
  1502. case 1:
  1503. bitforce_job_start(thr);
  1504. break;
  1505. case 2:
  1506. bitforce_job_get_results(thr, thr->work);
  1507. break;
  1508. default:
  1509. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1510. }
  1511. }
  1512. static
  1513. 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)
  1514. {
  1515. struct bitforce_data *data = proc->device_data;
  1516. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1517. {
  1518. if (!data->supports_fanspeed)
  1519. {
  1520. sprintf(replybuf, "fanmode not supported");
  1521. return replybuf;
  1522. }
  1523. if (!setting || !*setting)
  1524. {
  1525. sprintf(replybuf, "missing fanmode setting");
  1526. return replybuf;
  1527. }
  1528. if (setting[1] || ((setting[0] < '0' || setting[0] > '5') && setting[0] != '9'))
  1529. {
  1530. sprintf(replybuf, "invalid fanmode setting");
  1531. return replybuf;
  1532. }
  1533. char cmd[4] = "Z5X";
  1534. cmd[1] = setting[0];
  1535. mutex_lock(mutexp);
  1536. bitforce_cmd1b(proc, replybuf, 256, cmd, 3);
  1537. mutex_unlock(mutexp);
  1538. return replybuf;
  1539. }
  1540. }
  1541. static
  1542. 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)
  1543. {
  1544. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1545. {
  1546. mutex_lock(mutexp);
  1547. bitforce_cmd1b(proc, replybuf, 8000, setting, strlen(setting));
  1548. mutex_unlock(mutexp);
  1549. *success = SDR_OK;
  1550. return replybuf;
  1551. }
  1552. }
  1553. static const struct bfg_set_device_definition bitforce_set_device_funcs[] = {
  1554. {"fanmode", bitforce_set_fanmode, "range 0-5 (low to fast) or 9 (auto)"},
  1555. {"_cmd1", bitforce_rpc_send_cmd1, NULL},
  1556. {NULL},
  1557. };
  1558. struct device_drv bitforce_drv = {
  1559. .dname = "bitforce",
  1560. .name = "BFL",
  1561. .lowl_match = bitforce_lowl_match,
  1562. .lowl_probe = bitforce_lowl_probe,
  1563. #ifdef HAVE_CURSES
  1564. .proc_wlogprint_status = bitforce_wlogprint_status,
  1565. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1566. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1567. #endif
  1568. .get_api_stats = bitforce_drv_stats,
  1569. .minerloop = minerloop_async,
  1570. .reinit_device = bitforce_reinit,
  1571. .get_stats = bitforce_get_stats,
  1572. .identify_device = bitforce_identify,
  1573. .thread_init = bitforce_thread_init,
  1574. .job_prepare = bitforce_job_prepare,
  1575. .job_start = bitforce_job_start,
  1576. .job_get_results = bitforce_job_get_results,
  1577. .poll = bitforce_poll,
  1578. .job_process_results = bitforce_job_process_results,
  1579. .thread_shutdown = bitforce_shutdown,
  1580. .thread_enable = biforce_thread_enable
  1581. };
  1582. static inline
  1583. void bitforce_set_queue_full(struct thr_info *thr)
  1584. {
  1585. struct cgpu_info *bitforce = thr->cgpu;
  1586. struct bitforce_data *data = bitforce->device_data;
  1587. thr->queue_full = (data->queued + data->ready_to_queue >= data->queued_max) || (data->ready_to_queue >= data->max_queue_at_once);
  1588. }
  1589. static
  1590. bool bitforce_send_queue(struct thr_info *thr)
  1591. {
  1592. struct cgpu_info *bitforce = thr->cgpu;
  1593. struct cgpu_info * const dev = bitforce->device;
  1594. struct bitforce_data * const devdata = dev->device_data;
  1595. struct bitforce_data *data = bitforce->device_data;
  1596. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1597. struct work *work;
  1598. if (unlikely(!(devdata->is_open && data->ready_to_queue)))
  1599. return false;
  1600. char buf[0x100];
  1601. int queued_ok;
  1602. size_t qjs_sz = (32 + 12 + 1);
  1603. if (data->style == BFS_65NM)
  1604. ++qjs_sz;
  1605. size_t qjp_sz = 7 + (qjs_sz * data->ready_to_queue);
  1606. if (data->style == BFS_65NM)
  1607. qjp_sz -= 3;
  1608. uint8_t qjp[qjp_sz], *qjs;
  1609. qjs = &qjp[qjp_sz];
  1610. // NOTE: qjp is build backwards here
  1611. *(--qjs) = 0xfe;
  1612. work = thr->work_list->prev;
  1613. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1614. {
  1615. *(--qjs) = 0xaa;
  1616. memcpy(qjs -= 12, work->data + 64, 12);
  1617. memcpy(qjs -= 32, work->midstate, 32);
  1618. if (data->style == BFS_65NM)
  1619. *(--qjs) = 45;
  1620. }
  1621. *(--qjs) = data->ready_to_queue;
  1622. *(--qjs) = 0xc1;
  1623. if (data->style == BFS_65NM)
  1624. *(--qjs) = qjp_sz - 1;
  1625. else
  1626. {
  1627. *(--qjs) = qjp_sz >> 8;
  1628. *(--qjs) = qjp_sz & 0xff;
  1629. *(--qjs) = 'X';
  1630. *(--qjs) = 'W';
  1631. }
  1632. retry:
  1633. mutex_lock(mutexp);
  1634. if (data->style != BFS_65NM)
  1635. bitforce_cmd1c(bitforce, buf, sizeof(buf), qjp, qjp_sz);
  1636. else
  1637. if (data->missing_zwx)
  1638. bitforce_cmd2(bitforce, buf, sizeof(buf), "ZNX", &qjp[3], qjp_sz - 4);
  1639. else
  1640. bitforce_cmd2(bitforce, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1641. mutex_unlock(mutexp);
  1642. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1643. {
  1644. // Queue full :(
  1645. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1646. bitforce->proc_repr,
  1647. data->ready_to_queue, data->queued);
  1648. thr->queue_full = true;
  1649. return false;
  1650. }
  1651. if (strncasecmp(buf, "OK:QUEUED", 9))
  1652. {
  1653. if ((!strncasecmp(buf, "ERROR: UNKNOWN", 11)) && !data->missing_zwx)
  1654. {
  1655. applog(LOG_DEBUG, "%"PRIpreprv": Missing ZWX command, trying ZNX",
  1656. bitforce->proc_repr);
  1657. data->missing_zwx = true;
  1658. goto retry;
  1659. }
  1660. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1661. bitforce->proc_repr,
  1662. data->ready_to_queue, data->queued, buf);
  1663. return false;
  1664. }
  1665. if (!data->queued)
  1666. cgtime(&data->tv_hashmeter_start);
  1667. if (data->missing_zwx)
  1668. queued_ok = 1;
  1669. else
  1670. queued_ok = atoi(&buf[9]);
  1671. data->queued += queued_ok;
  1672. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1673. bitforce->proc_repr,
  1674. queued_ok, data->ready_to_queue, data->queued);
  1675. data->ready_to_queue -= queued_ok;
  1676. if (!data->missing_zwx)
  1677. thr->queue_full = data->ready_to_queue;
  1678. data->just_flushed = false;
  1679. data->want_to_send_queue = false;
  1680. return true;
  1681. }
  1682. void work_list_del(struct work **head, struct work *work)
  1683. {
  1684. DL_DELETE(*head, work);
  1685. free_work(work);
  1686. }
  1687. static
  1688. bool bitforce_queue_do_results(struct thr_info *thr)
  1689. {
  1690. struct cgpu_info *bitforce = thr->cgpu;
  1691. struct cgpu_info * const dev = bitforce->device;
  1692. struct bitforce_data * const devdata = dev->device_data;
  1693. struct bitforce_data *data = bitforce->device_data;
  1694. int count;
  1695. int fcount;
  1696. char *noncebuf, *buf, *end;
  1697. unsigned char midstate[32], datatail[12];
  1698. struct work *work, *tmpwork, *thiswork;
  1699. struct timeval tv_now, tv_elapsed;
  1700. long chipno = 0; // Initialized value is used for non-parallelized boards
  1701. struct cgpu_info *chip_cgpu;
  1702. struct thr_info *chip_thr;
  1703. int counts[data->parallel];
  1704. if (unlikely(!devdata->is_open))
  1705. return false;
  1706. again:
  1707. noncebuf = &data->noncebuf[0];
  1708. count = bitforce_zox(thr, "ZOX");
  1709. if (unlikely(count < 0))
  1710. {
  1711. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1712. inc_hw_errors_only(thr);
  1713. return false;
  1714. }
  1715. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results on poll (max=%d)", bitforce->proc_repr, count, (int)BITFORCE_MAX_QRESULTS);
  1716. if (!count)
  1717. return true;
  1718. fcount = 0;
  1719. for (int i = 0; i < data->parallel; ++i)
  1720. counts[i] = 0;
  1721. noncebuf = next_line(noncebuf);
  1722. while ((buf = noncebuf)[0])
  1723. {
  1724. if ( (noncebuf = next_line(buf)) )
  1725. noncebuf[-1] = '\0';
  1726. if (strlen(buf) <= 90)
  1727. {
  1728. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1729. continue;
  1730. }
  1731. hex2bin(midstate, buf, 32);
  1732. hex2bin(datatail, &buf[65], 12);
  1733. thiswork = NULL;
  1734. DL_FOREACH(thr->work_list, work)
  1735. {
  1736. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1737. continue;
  1738. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1739. continue;
  1740. thiswork = work;
  1741. break;
  1742. }
  1743. end = &buf[89];
  1744. chip_cgpu = bitforce;
  1745. if (data->parallel_protocol)
  1746. {
  1747. chipno = strtol(&end[1], &end, 16);
  1748. if (chipno >= data->parallel)
  1749. {
  1750. applog(LOG_ERR, "%"PRIpreprv": Chip number out of range for queue result: %s", chip_cgpu->proc_repr, buf);
  1751. chipno = 0;
  1752. }
  1753. for (int i = 0; i < chipno; ++i)
  1754. chip_cgpu = chip_cgpu->next_proc;
  1755. }
  1756. chip_thr = chip_cgpu->thr[0];
  1757. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", chip_cgpu->proc_repr, buf);
  1758. if (unlikely(!thiswork))
  1759. {
  1760. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results: %s", chip_cgpu->proc_repr, buf);
  1761. inc_hw_errors_only(chip_thr);
  1762. goto next_qline;
  1763. }
  1764. if (unlikely(!end[0]))
  1765. {
  1766. applog(LOG_ERR, "%"PRIpreprv": Missing nonce count in queue results: %s", chip_cgpu->proc_repr, buf);
  1767. goto finishresult;
  1768. }
  1769. if (strtol(&end[1], &end, 10))
  1770. {
  1771. if (unlikely(!end[0]))
  1772. {
  1773. applog(LOG_ERR, "%"PRIpreprv": Missing nonces in queue results: %s", chip_cgpu->proc_repr, buf);
  1774. goto finishresult;
  1775. }
  1776. bitforce_process_result_nonces(chip_thr, work, &end[1]);
  1777. }
  1778. ++fcount;
  1779. ++counts[chipno];
  1780. finishresult:
  1781. if (data->parallel == 1)
  1782. {
  1783. // Queue results are in order, so anything queued prior this is lost
  1784. // Delete all queued work up to, and including, this one
  1785. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1786. {
  1787. work_list_del(&thr->work_list, work);
  1788. --data->queued;
  1789. if (work == thiswork)
  1790. break;
  1791. }
  1792. }
  1793. else
  1794. {
  1795. // Parallel processors means the results might not be in order
  1796. // This could leak if jobs get lost, hence the sanity checks using "ZqX"
  1797. work_list_del(&thr->work_list, thiswork);
  1798. --data->queued;
  1799. }
  1800. next_qline: (void)0;
  1801. }
  1802. bitforce_set_queue_full(thr);
  1803. if (count >= BITFORCE_MAX_QRESULTS)
  1804. goto again;
  1805. if (data->parallel == 1 && (
  1806. (fcount < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1807. || (fcount > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT) ))
  1808. {
  1809. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1810. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (fcount ?: 1);
  1811. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1812. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1813. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1814. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1815. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1816. bitforce->proc_repr, fcount, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1817. }
  1818. else
  1819. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1820. bitforce->proc_repr, fcount, bitforce->sleep_ms, data->queued);
  1821. cgtime(&tv_now);
  1822. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1823. chip_cgpu = bitforce;
  1824. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  1825. {
  1826. chip_thr = chip_cgpu->thr[0];
  1827. hashes_done(chip_thr, (uint64_t)bitforce->nonces * counts[i], &tv_elapsed, NULL);
  1828. }
  1829. data->tv_hashmeter_start = tv_now;
  1830. return true;
  1831. }
  1832. static
  1833. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1834. {
  1835. struct cgpu_info *bitforce = thr->cgpu;
  1836. struct bitforce_data *data = bitforce->device_data;
  1837. bool rv, ndq;
  1838. bitforce_set_queue_full(thr);
  1839. rv = !thr->queue_full;
  1840. if (rv)
  1841. {
  1842. DL_APPEND(thr->work_list, work);
  1843. ++data->ready_to_queue;
  1844. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1845. bitforce->proc_repr,
  1846. (unsigned)data->queued_max, data->ready_to_queue, data->queued);
  1847. bitforce_set_queue_full(thr);
  1848. }
  1849. else
  1850. if (!data->ready_to_queue)
  1851. return rv;
  1852. ndq = !data->queued;
  1853. if ((ndq) // Device is idle
  1854. || (data->ready_to_queue >= data->max_queue_at_once) // ...or 5 items ready to go
  1855. || (thr->queue_full) // ...or done filling queue
  1856. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1857. || (data->missing_zwx) // ...or device can only queue one at a time
  1858. )
  1859. {
  1860. if (!bitforce_send_queue(thr))
  1861. {
  1862. // Problem sending queue, retry again in a few seconds
  1863. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1864. inc_hw_errors_only(thr);
  1865. data->want_to_send_queue = true;
  1866. }
  1867. }
  1868. return rv;
  1869. }
  1870. struct _jobinfo {
  1871. uint8_t key[32+12];
  1872. int instances;
  1873. UT_hash_handle hh;
  1874. };
  1875. static
  1876. void bitforce_queue_flush(struct thr_info *thr)
  1877. {
  1878. struct bitforce_proc_data *procdata = thr->cgpu_data;
  1879. if (!procdata->handles_board)
  1880. return;
  1881. struct cgpu_info *bitforce = thr->cgpu;
  1882. struct bitforce_data *data = bitforce->device_data;
  1883. char *buf = &data->noncebuf[0], *buf2 = NULL;
  1884. const char *cmd = "ZqX";
  1885. unsigned flushed;
  1886. struct _jobinfo *processing = NULL, *item, *this;
  1887. if (data->parallel == 1)
  1888. // Pre-parallelization neither needs nor supports "ZqX"
  1889. cmd = "ZQX";
  1890. // TODO: Call "ZQX" most of the time: don't need to do sanity checks so often
  1891. bitforce_zox(thr, cmd);
  1892. if (!strncasecmp(buf, "OK:FLUSHED", 10))
  1893. flushed = atoi(&buf[10]);
  1894. else
  1895. if ((!strncasecmp(buf, "COUNT:", 6)) && (buf2 = strstr(buf, "FLUSHED:")) )
  1896. {
  1897. flushed = atoi(&buf2[8]);
  1898. buf2 = next_line(buf2);
  1899. }
  1900. else
  1901. if (!strncasecmp(buf, "OK", 2))
  1902. {
  1903. applog(LOG_DEBUG, "%"PRIpreprv": Didn't report flush count", bitforce->proc_repr);
  1904. thr->queue_full = false;
  1905. flushed = 0;
  1906. }
  1907. else
  1908. {
  1909. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1910. flushed = 0;
  1911. }
  1912. data->queued -= flushed;
  1913. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1914. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1915. flushed += data->ready_to_queue;
  1916. data->ready_to_queue = 0;
  1917. while (flushed--)
  1918. work_list_del(&thr->work_list, thr->work_list->prev);
  1919. bitforce_set_queue_full(thr);
  1920. data->just_flushed = true;
  1921. data->want_to_send_queue = false;
  1922. // "ZqX" returns jobs in progress, allowing us to sanity check
  1923. // NOTE: Must process buffer into hash table BEFORE calling bitforce_queue_do_results, which clobbers it
  1924. // NOTE: Must do actual sanity check AFTER calling bitforce_queue_do_results, to ensure we don't delete completed jobs
  1925. if (buf2)
  1926. {
  1927. // First, turn buf2 into a hash
  1928. for ( ; buf2[0]; buf2 = next_line(buf2))
  1929. {
  1930. this = malloc(sizeof(*this));
  1931. hex2bin(&this->key[ 0], &buf2[ 0], 32);
  1932. hex2bin(&this->key[32], &buf2[65], 12);
  1933. HASH_FIND(hh, processing, &this->key[0], sizeof(this->key), item);
  1934. if (likely(!item))
  1935. {
  1936. this->instances = 1;
  1937. HASH_ADD(hh, processing, key, sizeof(this->key), this);
  1938. }
  1939. else
  1940. {
  1941. // This should really only happen in testing/benchmarking...
  1942. ++item->instances;
  1943. free(this);
  1944. }
  1945. }
  1946. }
  1947. bitforce_queue_do_results(thr);
  1948. if (buf2)
  1949. {
  1950. struct work *work, *tmp;
  1951. uint8_t key[32+12];
  1952. // Now iterate over the work_list and delete anything not in the hash
  1953. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  1954. {
  1955. memcpy(&key[ 0], work->midstate, 32);
  1956. memcpy(&key[32], &work->data[64], 12);
  1957. HASH_FIND(hh, processing, &key[0], sizeof(key), item);
  1958. if (unlikely(!item))
  1959. {
  1960. char hex[89];
  1961. bin2hex(hex, key, 32+12);
  1962. applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is missing queued job! %s", bitforce->proc_repr, hex);
  1963. work_list_del(&thr->work_list, work);
  1964. continue;
  1965. }
  1966. if (likely(!--item->instances))
  1967. {
  1968. HASH_DEL(processing, item);
  1969. free(item);
  1970. }
  1971. }
  1972. if (unlikely( (flushed = HASH_COUNT(processing)) ))
  1973. {
  1974. //applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is working on %d unknown jobs!", bitforce->proc_repr, flushed);
  1975. // FIXME: Probably these were jobs finished after ZqX, included in the result check we just did
  1976. // NOTE: We need to do that result check first to avoid deleting work_list items for things just solved
  1977. HASH_ITER(hh, processing, item, this)
  1978. {
  1979. HASH_DEL(processing, item);
  1980. free(item);
  1981. }
  1982. }
  1983. }
  1984. }
  1985. static
  1986. void bitforce_queue_poll(struct thr_info *thr)
  1987. {
  1988. struct cgpu_info *bitforce = thr->cgpu;
  1989. struct bitforce_data *data = bitforce->device_data;
  1990. unsigned long sleep_us;
  1991. if (data->queued)
  1992. bitforce_queue_do_results(thr);
  1993. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  1994. if (data->want_to_send_queue)
  1995. if (!bitforce_send_queue(thr))
  1996. if (!data->queued)
  1997. {
  1998. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  1999. inc_hw_errors_only(thr);
  2000. sleep_us = 1000000;
  2001. }
  2002. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  2003. }
  2004. static void bitforce_queue_thread_deven(struct thr_info *thr)
  2005. {
  2006. struct cgpu_info *bitforce = thr->cgpu, *thisbf;
  2007. struct bitforce_data *data = bitforce->device_data;
  2008. struct thr_info *thisthr;
  2009. for (thisbf = bitforce->device; thisbf && thisbf->device_data != data; thisbf = thisbf->next_proc)
  2010. {}
  2011. for ( ; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
  2012. {
  2013. thisthr = bitforce->thr[0];
  2014. thisthr->pause = thr->pause;
  2015. thisbf->deven = bitforce->deven;
  2016. }
  2017. }
  2018. static void bitforce_queue_thread_disable(struct thr_info *thr)
  2019. {
  2020. // Disable other threads sharing the same queue
  2021. bitforce_queue_thread_deven(thr);
  2022. }
  2023. static void bitforce_queue_thread_enable(struct thr_info *thr)
  2024. {
  2025. // TODO: Maybe reinit?
  2026. // Enable other threads sharing the same queue
  2027. bitforce_queue_thread_deven(thr);
  2028. }
  2029. struct device_drv bitforce_queue_api = {
  2030. .dname = "bitforce_queue",
  2031. .name = "BFL",
  2032. .lowl_probe_by_name_only = true,
  2033. .lowl_match = bitforce_lowl_match,
  2034. .lowl_probe = bitforce_lowl_probe,
  2035. .minerloop = minerloop_queue,
  2036. .reinit_device = bitforce_reinit,
  2037. #ifdef HAVE_CURSES
  2038. .proc_wlogprint_status = bitforce_wlogprint_status,
  2039. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  2040. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  2041. #endif
  2042. .get_api_stats = bitforce_drv_stats,
  2043. .get_stats = bitforce_get_stats,
  2044. .identify_device = bitforce_identify,
  2045. .thread_init = bitforce_thread_init,
  2046. .queue_append = bitforce_queue_append,
  2047. .queue_flush = bitforce_queue_flush,
  2048. .poll = bitforce_queue_poll,
  2049. .thread_shutdown = bitforce_shutdown,
  2050. .thread_disable = bitforce_queue_thread_disable,
  2051. .thread_enable = bitforce_queue_thread_enable,
  2052. };