driver-bitforce.c 20 KB

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