driver-bitforce.c 73 KB

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