driver-bitforce.c 65 KB

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