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