driver-bitforce.c 22 KB

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