driver-bitforce.c 21 KB

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