driver-bitforce.c 22 KB

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