driver-bitforce.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2012 Luke Dashjr
  4. * Copyright 2012 Con Kolivas
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <strings.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include "compat.h"
  20. #include "miner.h"
  21. #include "usbutils.h"
  22. #include "util.h"
  23. #ifdef WIN32
  24. #include <windows.h>
  25. #endif /* WIN32 */
  26. #define BITFORCE_IDENTIFY "ZGX"
  27. #define BITFORCE_IDENTIFY_LEN (sizeof(BITFORCE_IDENTIFY)-1)
  28. #define BITFORCE_FLASH "ZMX"
  29. #define BITFORCE_FLASH_LEN (sizeof(BITFORCE_FLASH)-1)
  30. #define BITFORCE_TEMPERATURE "ZLX"
  31. #define BITFORCE_TEMPERATURE_LEN (sizeof(BITFORCE_TEMPERATURE)-1)
  32. #define BITFORCE_SENDRANGE "ZPX"
  33. #define BITFORCE_SENDRANGE_LEN (sizeof(BITFORCE_SENDRANGE)-1)
  34. #define BITFORCE_SENDWORK "ZDX"
  35. #define BITFORCE_SENDWORK_LEN (sizeof(BITFORCE_SENDWORK)-1)
  36. #define BITFORCE_WORKSTATUS "ZFX"
  37. #define BITFORCE_WORKSTATUS_LEN (sizeof(BITFORCE_WORKSTATUS)-1)
  38. // Either of Nonce or No-nonce start with:
  39. #define BITFORCE_EITHER "N"
  40. #define BITFORCE_EITHER_LEN 1
  41. #define BITFORCE_NONCE "NONCE-FOUND"
  42. #define BITFORCE_NONCE_LEN (sizeof(BITFORCE_NONCE)-1)
  43. #define BITFORCE_NO_NONCE "NO-NONCE"
  44. #define BITFORCE_NO_NONCE_MATCH 3
  45. #define BITFORCE_IDLE "IDLE"
  46. #define BITFORCE_IDLE_MATCH 1
  47. #define BITFORCE_SLEEP_MS 500
  48. #define BITFORCE_TIMEOUT_S 7
  49. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  50. #define BITFORCE_LONG_TIMEOUT_S 30
  51. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  52. #define BITFORCE_CHECK_INTERVAL_MS 10
  53. #define WORK_CHECK_INTERVAL_MS 50
  54. #define MAX_START_DELAY_MS 100
  55. #define tv_to_ms(tval) (tval.tv_sec * 1000 + tval.tv_usec / 1000)
  56. #define TIME_AVG_CONSTANT 8
  57. #define KNAME_WORK "full work"
  58. #define KNAME_RANGE "nonce range"
  59. #define BITFORCE_BUFSIZ (0x200)
  60. // If initialisation fails the first time,
  61. // sleep this amount (ms) and try again
  62. #define REINIT_TIME_FIRST_MS 100
  63. // Max ms per sleep
  64. #define REINIT_TIME_MAX_MS 800
  65. // Keep trying up to this many us
  66. #define REINIT_TIME_MAX 3000000
  67. static const char *blank = "";
  68. struct device_drv bitforce_drv;
  69. static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
  70. {
  71. int err, interface;
  72. if (lock)
  73. mutex_lock(&bitforce->device_mutex);
  74. if (bitforce->usbinfo.nodev)
  75. goto failed;
  76. interface = usb_interface(bitforce);
  77. // Reset
  78. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  79. FTDI_VALUE_RESET, interface, C_RESET);
  80. if (opt_debug)
  81. applog(LOG_DEBUG, "%s%i: reset got err %d",
  82. bitforce->drv->name, bitforce->device_id, err);
  83. if (bitforce->usbinfo.nodev)
  84. goto failed;
  85. // Set data control
  86. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  87. FTDI_VALUE_DATA_BFL, interface, C_SETDATA);
  88. if (opt_debug)
  89. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  90. bitforce->drv->name, bitforce->device_id, err);
  91. if (bitforce->usbinfo.nodev)
  92. goto failed;
  93. // Set the baud
  94. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BFL,
  95. (FTDI_INDEX_BAUD_BFL & 0xff00) | interface,
  96. C_SETBAUD);
  97. if (opt_debug)
  98. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  99. bitforce->drv->name, bitforce->device_id, err);
  100. if (bitforce->usbinfo.nodev)
  101. goto failed;
  102. // Set Flow Control
  103. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  104. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  105. if (opt_debug)
  106. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  107. bitforce->drv->name, bitforce->device_id, err);
  108. if (bitforce->usbinfo.nodev)
  109. goto failed;
  110. // Set Modem Control
  111. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  112. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  113. if (opt_debug)
  114. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  115. bitforce->drv->name, bitforce->device_id, err);
  116. if (bitforce->usbinfo.nodev)
  117. goto failed;
  118. // Clear any sent data
  119. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  120. FTDI_VALUE_PURGE_TX, interface, C_PURGETX);
  121. if (opt_debug)
  122. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  123. bitforce->drv->name, bitforce->device_id, err);
  124. if (bitforce->usbinfo.nodev)
  125. goto failed;
  126. // Clear any received data
  127. err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  128. FTDI_VALUE_PURGE_RX, interface, C_PURGERX);
  129. if (opt_debug)
  130. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  131. bitforce->drv->name, bitforce->device_id, err);
  132. failed:
  133. if (lock)
  134. mutex_unlock(&bitforce->device_mutex);
  135. }
  136. static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  137. {
  138. char buf[BITFORCE_BUFSIZ+1];
  139. int err, amount;
  140. char *s;
  141. struct timeval init_start, init_now;
  142. int init_sleep, init_count;
  143. bool ident_first;
  144. struct cgpu_info *bitforce = usb_alloc_cgpu(&bitforce_drv, 1);
  145. if (!usb_init(bitforce, dev, found))
  146. goto shin;
  147. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  148. ident_first = true;
  149. retry:
  150. init_count = 0;
  151. init_sleep = REINIT_TIME_FIRST_MS;
  152. cgtime(&init_start);
  153. reinit:
  154. bitforce_initialise(bitforce, false);
  155. if ((err = usb_write(bitforce, BITFORCE_IDENTIFY, BITFORCE_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY)) < 0 || amount != BITFORCE_IDENTIFY_LEN) {
  156. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  157. bitforce->drv->dname, bitforce->device_path, amount, err);
  158. goto unshin;
  159. }
  160. if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
  161. init_count++;
  162. cgtime(&init_now);
  163. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  164. if (init_count == 2) {
  165. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  166. bitforce->drv->dname, bitforce->device_path, amount, err);
  167. }
  168. cgsleep_ms(init_sleep);
  169. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  170. init_sleep *= 2;
  171. goto reinit;
  172. }
  173. if (init_count > 0)
  174. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  175. bitforce->drv->dname, bitforce->device_path, init_count, tdiff(&init_now, &init_start));
  176. if (err < 0) {
  177. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  178. bitforce->drv->dname, bitforce->device_path, amount, err);
  179. } else {
  180. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  181. bitforce->drv->dname, bitforce->device_path, amount);
  182. }
  183. goto unshin;
  184. }
  185. buf[amount] = '\0';
  186. if (unlikely(!strstr(buf, "SHA256"))) {
  187. if (ident_first) {
  188. applog(LOG_WARNING, "%s detect (%s) didn't recognise '%s' trying again ...",
  189. bitforce->drv->dname, bitforce->device_path, buf);
  190. ident_first = false;
  191. goto retry;
  192. }
  193. applog(LOG_ERR, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  194. bitforce->drv->dname, bitforce->device_path, buf);
  195. goto unshin;
  196. }
  197. if (strstr(buf, "SHA256 SC")) {
  198. #ifdef USE_BFLSC
  199. applog(LOG_DEBUG, "SC device detected, will defer to BFLSC driver");
  200. #else
  201. applog(LOG_WARNING, "SC device detected but no BFLSC support compiled in!");
  202. #endif
  203. goto unshin;
  204. }
  205. if (likely((!memcmp(buf, ">>>ID: ", 7)) && (s = strstr(buf + 3, ">>>")))) {
  206. s[0] = '\0';
  207. bitforce->name = strdup(buf + 7);
  208. } else {
  209. bitforce->name = (char *)blank;
  210. }
  211. // We have a real BitForce!
  212. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  213. bitforce->drv->dname, bitforce->device_path, bitforce->name);
  214. /* Initially enable support for nonce range and disable it later if it
  215. * fails */
  216. if (opt_bfl_noncerange) {
  217. bitforce->nonce_range = true;
  218. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  219. bitforce->kname = KNAME_RANGE;
  220. } else {
  221. bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
  222. bitforce->kname = KNAME_WORK;
  223. }
  224. if (!add_cgpu(bitforce))
  225. goto unshin;
  226. update_usb_stats(bitforce);
  227. mutex_init(&bitforce->device_mutex);
  228. return true;
  229. unshin:
  230. usb_uninit(bitforce);
  231. shin:
  232. if (bitforce->name != blank) {
  233. free(bitforce->name);
  234. bitforce->name = NULL;
  235. }
  236. bitforce = usb_free_cgpu(bitforce);
  237. return false;
  238. }
  239. static void bitforce_detect(void)
  240. {
  241. usb_detect(&bitforce_drv, bitforce_detect_one);
  242. }
  243. static void get_bitforce_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitforce)
  244. {
  245. float gt = bitforce->temp;
  246. if (gt > 0)
  247. tailsprintf(buf, bufsiz, "%5.1fC ", gt);
  248. else
  249. tailsprintf(buf, bufsiz, " ");
  250. tailsprintf(buf, bufsiz, " | ");
  251. }
  252. static bool bitforce_thread_prepare(__maybe_unused struct thr_info *thr)
  253. {
  254. // struct cgpu_info *bitforce = thr->cgpu;
  255. return true;
  256. }
  257. static void bitforce_flash_led(struct cgpu_info *bitforce)
  258. {
  259. int err, amount;
  260. /* Do not try to flash the led if we're polling for a result to
  261. * minimise the chance of interleaved results */
  262. if (bitforce->polling)
  263. return;
  264. /* It is not critical flashing the led so don't get stuck if we
  265. * can't grab the mutex now */
  266. if (mutex_trylock(&bitforce->device_mutex))
  267. return;
  268. if ((err = usb_write(bitforce, BITFORCE_FLASH, BITFORCE_FLASH_LEN, &amount, C_REQUESTFLASH)) < 0 || amount != BITFORCE_FLASH_LEN) {
  269. applog(LOG_ERR, "%s%i: flash request failed (%d:%d)",
  270. bitforce->drv->name, bitforce->device_id, amount, err);
  271. } else {
  272. /* However, this stops anything else getting a reply
  273. * So best to delay any other access to the BFL */
  274. cgsleep_ms(4000);
  275. }
  276. /* Once we've tried - don't do it until told to again */
  277. bitforce->flash_led = false;
  278. mutex_unlock(&bitforce->device_mutex);
  279. return; // nothing is returned by the BFL
  280. }
  281. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  282. {
  283. char buf[BITFORCE_BUFSIZ+1];
  284. int err, amount;
  285. char *s;
  286. // Device is gone
  287. if (bitforce->usbinfo.nodev)
  288. return false;
  289. /* Do not try to get the temperature if we're polling for a result to
  290. * minimise the chance of interleaved results */
  291. if (bitforce->polling)
  292. return true;
  293. // Flash instead of Temp - doing both can be too slow
  294. if (bitforce->flash_led) {
  295. bitforce_flash_led(bitforce);
  296. return true;
  297. }
  298. /* It is not critical getting temperature so don't get stuck if we
  299. * can't grab the mutex here */
  300. if (mutex_trylock(&bitforce->device_mutex))
  301. return false;
  302. if ((err = usb_write(bitforce, BITFORCE_TEMPERATURE, BITFORCE_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE)) < 0 || amount != BITFORCE_TEMPERATURE_LEN) {
  303. mutex_unlock(&bitforce->device_mutex);
  304. applog(LOG_ERR, "%s%i: Error: Request temp invalid/timed out (%d:%d)",
  305. bitforce->drv->name, bitforce->device_id, amount, err);
  306. bitforce->hw_errors++;
  307. return false;
  308. }
  309. if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
  310. mutex_unlock(&bitforce->device_mutex);
  311. if (err < 0) {
  312. applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
  313. bitforce->drv->name, bitforce->device_id, amount, err);
  314. } else {
  315. applog(LOG_ERR, "%s%i: Error: Get temp returned nothing (%d:%d)",
  316. bitforce->drv->name, bitforce->device_id, amount, err);
  317. }
  318. bitforce->hw_errors++;
  319. return false;
  320. }
  321. mutex_unlock(&bitforce->device_mutex);
  322. if ((!strncasecmp(buf, "TEMP", 4)) && (s = strchr(buf + 4, ':'))) {
  323. float temp = strtof(s + 1, NULL);
  324. /* Cope with older software that breaks and reads nonsense
  325. * values */
  326. if (temp > 100)
  327. temp = strtod(s + 1, NULL);
  328. if (temp > 0) {
  329. bitforce->temp = temp;
  330. if (unlikely(bitforce->cutofftemp > 0 && temp > bitforce->cutofftemp)) {
  331. applog(LOG_WARNING, "%s%i: Hit thermal cutoff limit, disabling!",
  332. bitforce->drv->name, bitforce->device_id);
  333. bitforce->deven = DEV_RECOVER;
  334. dev_error(bitforce, REASON_DEV_THERMAL_CUTOFF);
  335. }
  336. }
  337. } else {
  338. /* Use the temperature monitor as a kind of watchdog for when
  339. * our responses are out of sync and flush the buffer to
  340. * hopefully recover */
  341. applog(LOG_WARNING, "%s%i: Garbled response probably throttling, clearing buffer",
  342. bitforce->drv->name, bitforce->device_id);
  343. dev_error(bitforce, REASON_DEV_THROTTLE);
  344. /* Count throttling episodes as hardware errors */
  345. bitforce->hw_errors++;
  346. bitforce_initialise(bitforce, true);
  347. return false;
  348. }
  349. return true;
  350. }
  351. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  352. {
  353. struct cgpu_info *bitforce = thr->cgpu;
  354. unsigned char ob[70];
  355. char buf[BITFORCE_BUFSIZ+1];
  356. int err, amount;
  357. char *s;
  358. char *cmd;
  359. int len;
  360. re_send:
  361. if (bitforce->nonce_range) {
  362. cmd = BITFORCE_SENDRANGE;
  363. len = BITFORCE_SENDRANGE_LEN;
  364. } else {
  365. cmd = BITFORCE_SENDWORK;
  366. len = BITFORCE_SENDWORK_LEN;
  367. }
  368. mutex_lock(&bitforce->device_mutex);
  369. if ((err = usb_write(bitforce, cmd, len, &amount, C_REQUESTSENDWORK)) < 0 || amount != len) {
  370. mutex_unlock(&bitforce->device_mutex);
  371. applog(LOG_ERR, "%s%i: request send work failed (%d:%d)",
  372. bitforce->drv->name, bitforce->device_id, amount, err);
  373. return false;
  374. }
  375. if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
  376. mutex_unlock(&bitforce->device_mutex);
  377. applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
  378. bitforce->drv->name, bitforce->device_id, amount, err);
  379. return false;
  380. }
  381. if (amount == 0 || !buf[0] || !strncasecmp(buf, "B", 1)) {
  382. mutex_unlock(&bitforce->device_mutex);
  383. cgsleep_ms(WORK_CHECK_INTERVAL_MS);
  384. goto re_send;
  385. } else if (unlikely(strncasecmp(buf, "OK", 2))) {
  386. mutex_unlock(&bitforce->device_mutex);
  387. if (bitforce->nonce_range) {
  388. applog(LOG_WARNING, "%s%i: Does not support nonce range, disabling",
  389. bitforce->drv->name, bitforce->device_id);
  390. bitforce->nonce_range = false;
  391. bitforce->sleep_ms *= 5;
  392. bitforce->kname = KNAME_WORK;
  393. goto re_send;
  394. }
  395. applog(LOG_ERR, "%s%i: Error: Send work reports: %s",
  396. bitforce->drv->name, bitforce->device_id, buf);
  397. return false;
  398. }
  399. sprintf((char *)ob, ">>>>>>>>");
  400. memcpy(ob + 8, work->midstate, 32);
  401. memcpy(ob + 8 + 32, work->data + 64, 12);
  402. if (!bitforce->nonce_range) {
  403. sprintf((char *)ob + 8 + 32 + 12, ">>>>>>>>");
  404. work->blk.nonce = bitforce->nonces = 0xffffffff;
  405. len = 60;
  406. } else {
  407. uint32_t *nonce;
  408. nonce = (uint32_t *)(ob + 8 + 32 + 12);
  409. *nonce = htobe32(work->blk.nonce);
  410. nonce = (uint32_t *)(ob + 8 + 32 + 12 + 4);
  411. /* Split work up into 1/5th nonce ranges */
  412. bitforce->nonces = 0x33333332;
  413. *nonce = htobe32(work->blk.nonce + bitforce->nonces);
  414. work->blk.nonce += bitforce->nonces + 1;
  415. sprintf((char *)ob + 8 + 32 + 12 + 8, ">>>>>>>>");
  416. len = 68;
  417. }
  418. if ((err = usb_write(bitforce, (char *)ob, len, &amount, C_SENDWORK)) < 0 || amount != len) {
  419. mutex_unlock(&bitforce->device_mutex);
  420. applog(LOG_ERR, "%s%i: send work failed (%d:%d)",
  421. bitforce->drv->name, bitforce->device_id, amount, err);
  422. return false;
  423. }
  424. if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
  425. mutex_unlock(&bitforce->device_mutex);
  426. applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
  427. bitforce->drv->name, bitforce->device_id, amount, err);
  428. return false;
  429. }
  430. mutex_unlock(&bitforce->device_mutex);
  431. if (opt_debug) {
  432. s = bin2hex(ob + 8, 44);
  433. applog(LOG_DEBUG, "%s%i: block data: %s",
  434. bitforce->drv->name, bitforce->device_id, s);
  435. free(s);
  436. }
  437. if (amount == 0 || !buf[0]) {
  438. applog(LOG_ERR, "%s%i: Error: Send block data returned empty string/timed out",
  439. bitforce->drv->name, bitforce->device_id);
  440. return false;
  441. }
  442. if (unlikely(strncasecmp(buf, "OK", 2))) {
  443. applog(LOG_ERR, "%s%i: Error: Send block data reports: %s",
  444. bitforce->drv->name, bitforce->device_id, buf);
  445. return false;
  446. }
  447. cgtime(&bitforce->work_start_tv);
  448. return true;
  449. }
  450. static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  451. {
  452. struct cgpu_info *bitforce = thr->cgpu;
  453. unsigned int delay_time_ms;
  454. struct timeval elapsed;
  455. struct timeval now;
  456. char buf[BITFORCE_BUFSIZ+1];
  457. int amount;
  458. char *pnoncebuf;
  459. uint32_t nonce;
  460. while (1) {
  461. if (unlikely(thr->work_restart))
  462. return 0;
  463. mutex_lock(&bitforce->device_mutex);
  464. usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
  465. usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
  466. mutex_unlock(&bitforce->device_mutex);
  467. cgtime(&now);
  468. timersub(&now, &bitforce->work_start_tv, &elapsed);
  469. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  470. applog(LOG_ERR, "%s%i: took %ldms - longer than %dms",
  471. bitforce->drv->name, bitforce->device_id,
  472. tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
  473. return 0;
  474. }
  475. if (amount > 0 && buf[0] && strncasecmp(buf, "B", 1)) /* BFL does not respond during throttling */
  476. break;
  477. /* if BFL is throttling, no point checking so quickly */
  478. delay_time_ms = (buf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  479. cgsleep_ms(delay_time_ms);
  480. bitforce->wait_ms += delay_time_ms;
  481. }
  482. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  483. applog(LOG_ERR, "%s%i: took %ldms - longer than %dms",
  484. bitforce->drv->name, bitforce->device_id,
  485. tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
  486. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  487. /* Only return if we got nothing after timeout - there still may be results */
  488. if (amount == 0)
  489. return 0;
  490. } else if (!strncasecmp(buf, BITFORCE_EITHER, BITFORCE_EITHER_LEN)) {
  491. /* Simple timing adjustment. Allow a few polls to cope with
  492. * OS timer delays being variably reliable. wait_ms will
  493. * always equal sleep_ms when we've waited greater than or
  494. * equal to the result return time.*/
  495. delay_time_ms = bitforce->sleep_ms;
  496. if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
  497. bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
  498. else if (bitforce->wait_ms == bitforce->sleep_ms) {
  499. if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  500. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  501. else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
  502. bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
  503. }
  504. if (delay_time_ms != bitforce->sleep_ms)
  505. applog(LOG_DEBUG, "%s%i: Wait time changed to: %d, waited %u",
  506. bitforce->drv->name, bitforce->device_id,
  507. bitforce->sleep_ms, bitforce->wait_ms);
  508. /* Work out the average time taken. Float for calculation, uint for display */
  509. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  510. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  511. }
  512. applog(LOG_DEBUG, "%s%i: waited %dms until %s",
  513. bitforce->drv->name, bitforce->device_id,
  514. bitforce->wait_ms, buf);
  515. if (!strncasecmp(buf, BITFORCE_NO_NONCE, BITFORCE_NO_NONCE_MATCH))
  516. return bitforce->nonces; /* No valid nonce found */
  517. else if (!strncasecmp(buf, BITFORCE_IDLE, BITFORCE_IDLE_MATCH))
  518. return 0; /* Device idle */
  519. else if (strncasecmp(buf, BITFORCE_NONCE, BITFORCE_NONCE_LEN)) {
  520. bitforce->hw_errors++;
  521. applog(LOG_WARNING, "%s%i: Error: Get result reports: %s",
  522. bitforce->drv->name, bitforce->device_id, buf);
  523. bitforce_initialise(bitforce, true);
  524. return 0;
  525. }
  526. pnoncebuf = &buf[12];
  527. while (1) {
  528. hex2bin((void*)&nonce, pnoncebuf, 4);
  529. #ifndef __BIG_ENDIAN__
  530. nonce = swab32(nonce);
  531. #endif
  532. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  533. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  534. applog(LOG_WARNING, "%s%i: Disabling broken nonce range support",
  535. bitforce->drv->name, bitforce->device_id);
  536. bitforce->nonce_range = false;
  537. work->blk.nonce = 0xffffffff;
  538. bitforce->sleep_ms *= 5;
  539. bitforce->kname = KNAME_WORK;
  540. }
  541. submit_nonce(thr, work, nonce);
  542. if (strncmp(&pnoncebuf[8], ",", 1))
  543. break;
  544. pnoncebuf += 9;
  545. }
  546. return bitforce->nonces;
  547. }
  548. static void bitforce_shutdown(__maybe_unused struct thr_info *thr)
  549. {
  550. // struct cgpu_info *bitforce = thr->cgpu;
  551. }
  552. static void biforce_thread_enable(struct thr_info *thr)
  553. {
  554. struct cgpu_info *bitforce = thr->cgpu;
  555. bitforce_initialise(bitforce, true);
  556. }
  557. static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  558. {
  559. struct cgpu_info *bitforce = thr->cgpu;
  560. bool send_ret;
  561. int64_t ret;
  562. // Device is gone
  563. if (bitforce->usbinfo.nodev)
  564. return -1;
  565. send_ret = bitforce_send_work(thr, work);
  566. if (!restart_wait(thr, bitforce->sleep_ms))
  567. return 0;
  568. bitforce->wait_ms = bitforce->sleep_ms;
  569. if (send_ret) {
  570. bitforce->polling = true;
  571. ret = bitforce_get_result(thr, work);
  572. bitforce->polling = false;
  573. } else
  574. ret = -1;
  575. if (ret == -1) {
  576. ret = 0;
  577. applog(LOG_ERR, "%s%i: Comms error", bitforce->drv->name, bitforce->device_id);
  578. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  579. bitforce->hw_errors++;
  580. /* empty read buffer */
  581. bitforce_initialise(bitforce, true);
  582. }
  583. return ret;
  584. }
  585. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  586. {
  587. return bitforce_get_temp(bitforce);
  588. }
  589. static void bitforce_identify(struct cgpu_info *bitforce)
  590. {
  591. bitforce->flash_led = true;
  592. }
  593. static bool bitforce_thread_init(struct thr_info *thr)
  594. {
  595. struct cgpu_info *bitforce = thr->cgpu;
  596. unsigned int wait;
  597. /* Pause each new thread at least 100ms between initialising
  598. * so the devices aren't making calls all at the same time. */
  599. wait = thr->id * MAX_START_DELAY_MS;
  600. applog(LOG_DEBUG, "%s%d: Delaying start by %dms",
  601. bitforce->drv->name, bitforce->device_id, wait / 1000);
  602. cgsleep_ms(wait);
  603. return true;
  604. }
  605. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  606. {
  607. struct api_data *root = NULL;
  608. // Warning, access to these is not locked - but we don't really
  609. // care since hashing performance is way more important than
  610. // locking access to displaying API debug 'stats'
  611. // If locking becomes an issue for any of them, use copy_data=true also
  612. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  613. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  614. return root;
  615. }
  616. struct device_drv bitforce_drv = {
  617. .drv_id = DRIVER_bitforce,
  618. .dname = "BitForce",
  619. .name = "BFL",
  620. .drv_detect = bitforce_detect,
  621. .get_api_stats = bitforce_api_stats,
  622. .get_statline_before = get_bitforce_statline_before,
  623. .get_stats = bitforce_get_stats,
  624. .identify_device = bitforce_identify,
  625. .thread_prepare = bitforce_thread_prepare,
  626. .thread_init = bitforce_thread_init,
  627. .scanhash = bitforce_scanhash,
  628. .thread_shutdown = bitforce_shutdown,
  629. .thread_enable = biforce_thread_enable
  630. };