driver-bitforce.c 21 KB

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