driver-bitforce.c 72 KB

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