driver-bitforce.c 65 KB

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