driver-bitforce.c 22 KB

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