driver-modminer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright 2012 Andrew Smith
  3. * Copyright 2012 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <math.h>
  15. #include "logging.h"
  16. #include "miner.h"
  17. #include "fpgautils.h"
  18. #include "util.h"
  19. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  20. #define BISTREAM_USER_ID "\2\4$B"
  21. #define MODMINER_CUTOFF_TEMP 60.0
  22. #define MODMINER_OVERHEAT_TEMP 50.0
  23. #define MODMINER_OVERHEAT_CLOCK -10
  24. #define MODMINER_HW_ERROR_PERCENT 0.75
  25. #define MODMINER_MAX_CLOCK 220
  26. #define MODMINER_DEF_CLOCK 200
  27. #define MODMINER_MIN_CLOCK 160
  28. #define MODMINER_CLOCK_DOWN -2
  29. #define MODMINER_CLOCK_SET 0
  30. #define MODMINER_CLOCK_UP 2
  31. // Maximum how many good shares in a row means clock up
  32. // 96 is ~34m22s at 200MH/s
  33. #define MODMINER_TRY_UP 96
  34. // Initially how many good shares in a row means clock up
  35. // This is doubled each down clock until it reaches MODMINER_TRY_UP
  36. // 6 is ~2m9s at 200MH/s
  37. #define MODMINER_EARLY_UP 6
  38. struct device_api modminer_api;
  39. static inline bool _bailout(int fd, struct cgpu_info *modminer, int prio, const char *fmt, ...)
  40. {
  41. if (fd != -1)
  42. serial_close(fd);
  43. if (modminer) {
  44. modminer->device_fd = -1;
  45. mutex_unlock(&modminer->device_mutex);
  46. }
  47. va_list ap;
  48. va_start(ap, fmt);
  49. vapplog(prio, fmt, ap);
  50. va_end(ap);
  51. return false;
  52. }
  53. // 45 noops sent when detecting, in case the device was left in "start job" reading
  54. static const char NOOP[] = "\0\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
  55. static bool modminer_detect_one(const char *devpath)
  56. {
  57. char buf[0x100];
  58. char *devname;
  59. ssize_t len;
  60. int fd;
  61. #ifdef WIN32
  62. fd = serial_open(devpath, 0, 10, true);
  63. if (fd < 0) {
  64. applog(LOG_ERR, "ModMiner detect: failed to open %s", devpath);
  65. return false;
  66. }
  67. (void)(write(fd, NOOP, sizeof(NOOP)-1) ?:0);
  68. while (serial_read(fd, buf, sizeof(buf)) > 0)
  69. ;
  70. // Version
  71. if (1 != write(fd, "\x01", 1)) {
  72. applog(LOG_ERR, "ModMiner detect: version request failed on %s (%d)", devpath, errno);
  73. goto shin;
  74. }
  75. len = serial_read(fd, buf, sizeof(buf)-1);
  76. if (len < 1) {
  77. applog(LOG_ERR, "ModMiner detect: no version reply on %s (%d)", devpath, errno);
  78. goto shin;
  79. }
  80. buf[len] = '\0';
  81. devname = strdup(buf);
  82. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  83. // FPGA count
  84. if (1 != write(fd, "\x02", 1)) {
  85. applog(LOG_ERR, "ModMiner detect: FPGA count request failed on %s (%d)", devpath, errno);
  86. goto shin;
  87. }
  88. len = read(fd, buf, 1);
  89. if (len < 1) {
  90. applog(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s (%d)", devpath, errno);
  91. goto shin;
  92. }
  93. serial_close(fd);
  94. #else
  95. fd = select_open(devpath);
  96. if (fd < 0) {
  97. applog(LOG_ERR, "ModMiner detect: failed to open %s", devpath);
  98. return false;
  99. }
  100. // Don't care if they fail
  101. select_write(fd, (char *)NOOP, sizeof(NOOP)-1);
  102. // Will clear up to a max of sizeof(buf)-1 chars
  103. select_read(fd, buf, sizeof(buf)-1);
  104. // Version
  105. if (select_write(fd, "\x01", 1) < 1) {
  106. applog(LOG_ERR, "ModMiner detect: version request failed on %s (%d)", devpath, errno);
  107. goto shin;
  108. }
  109. if ((len = select_read(fd, buf, sizeof(buf)-1)) < 1) {
  110. applog(LOG_ERR, "ModMiner detect: no version reply on %s (%d)", devpath, errno);
  111. goto shin;
  112. }
  113. buf[len] = '\0';
  114. devname = strdup(buf);
  115. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  116. // FPGA count
  117. if (select_write(fd, "\x02", 1) < 1) {
  118. applog(LOG_ERR, "ModMiner detect: FPGA count request failed on %s (%d)", devpath, errno);
  119. goto shin;
  120. }
  121. if ((len = select_read(fd, buf, 1)) < 1) {
  122. applog(LOG_ERR, "ModMiner detect: no FPGA count reply on %s (%d)", devpath, errno);
  123. goto shin;
  124. }
  125. select_close(fd);
  126. #endif
  127. // TODO: check if it supports 2 byte temperatures and if not
  128. // add a flag and set it use 1 byte and code to use the flag
  129. if (buf[0] == 0) {
  130. applog(LOG_ERR, "ModMiner detect: zero FPGA count from %s", devpath);
  131. goto shin;
  132. }
  133. if (buf[0] < 1 || buf[0] > 4) {
  134. applog(LOG_ERR, "ModMiner detect: invalid FPGA count (%u) from %s", buf[0], devpath);
  135. goto shin;
  136. }
  137. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  138. struct cgpu_info *modminer;
  139. modminer = calloc(1, sizeof(*modminer));
  140. modminer->api = &modminer_api;
  141. mutex_init(&modminer->device_mutex);
  142. modminer->device_path = strdup(devpath);
  143. modminer->device_fd = -1;
  144. modminer->deven = DEV_ENABLED;
  145. modminer->threads = buf[0];
  146. modminer->name = devname;
  147. return add_cgpu(modminer);
  148. shin:
  149. #ifdef WIN32
  150. serial_close(fd);
  151. #else
  152. select_close(fd);
  153. #endif
  154. return false;
  155. }
  156. static int modminer_detect_auto()
  157. {
  158. return
  159. serial_autodetect_udev (modminer_detect_one, "*ModMiner*") ?:
  160. serial_autodetect_devserial(modminer_detect_one, "BTCFPGA_ModMiner") ?:
  161. 0;
  162. }
  163. static void modminer_detect()
  164. {
  165. serial_detect_auto(&modminer_api, modminer_detect_one, modminer_detect_auto);
  166. }
  167. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  168. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  169. #define check_magic(L) do { \
  170. if (1 != fread(buf, 1, 1, f)) \
  171. bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \
  172. if (buf[0] != L) \
  173. bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \
  174. } while(0)
  175. #define read_str(eng) do { \
  176. if (1 != fread(buf, 2, 1, f)) \
  177. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \
  178. len = (ubuf[0] << 8) | ubuf[1]; \
  179. if (len >= sizeof(buf)) \
  180. bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \
  181. if (1 != fread(buf, len, 1, f)) \
  182. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \
  183. buf[len] = '\0'; \
  184. } while(0)
  185. #define status_read(eng) do { \
  186. FD_ZERO(&fds); \
  187. FD_SET(fd, &fds); \
  188. select(fd+1, &fds, NULL, NULL, NULL); \
  189. if (1 != read(fd, buf, 1)) \
  190. bailout2(LOG_ERR, "%s %u: Error programming %s (" eng ")", modminer->api->name, modminer->device_id, modminer->device_path); \
  191. if (buf[0] != 1) \
  192. bailout2(LOG_ERR, "%s %u: Wrong " eng " programming %s", modminer->api->name, modminer->device_id, modminer->device_path); \
  193. } while(0)
  194. static bool modminer_fpga_upload_bitstream(struct cgpu_info *modminer)
  195. {
  196. fd_set fds;
  197. char buf[0x100];
  198. unsigned char *ubuf = (unsigned char *)buf;
  199. unsigned long len;
  200. char *p;
  201. const char *fwfile = BITSTREAM_FILENAME;
  202. char fpgaid = 4; // "all FPGAs"
  203. FILE *f = open_bitstream("modminer", fwfile);
  204. if (!f)
  205. bailout(LOG_ERR, "Error opening ModMiner firmware file %s", fwfile);
  206. if (1 != fread(buf, 2, 1, f))
  207. bailout(LOG_ERR, "Error reading ModMiner firmware (magic)");
  208. if (buf[0] || buf[1] != 9)
  209. bailout(LOG_ERR, "ModMiner firmware has wrong magic (9)");
  210. if (-1 == fseek(f, 11, SEEK_CUR))
  211. bailout(LOG_ERR, "ModMiner firmware seek failed");
  212. check_magic('a');
  213. read_str("design name");
  214. applog(LOG_DEBUG, "ModMiner firmware file %s info:", fwfile);
  215. applog(LOG_DEBUG, " Design name: %s", buf);
  216. p = strrchr(buf, ';') ?: buf;
  217. p = strrchr(buf, '=') ?: p;
  218. if (p[0] == '=')
  219. ++p;
  220. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  221. if (p[0] != '\0')
  222. bailout(LOG_ERR, "Bad usercode in ModMiner firmware file");
  223. if (fwusercode == 0xffffffff)
  224. bailout(LOG_ERR, "ModMiner firmware doesn't support user code");
  225. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  226. check_magic('b');
  227. read_str("part number");
  228. applog(LOG_DEBUG, " Part number: %s", buf);
  229. check_magic('c');
  230. read_str("build date");
  231. applog(LOG_DEBUG, " Build date: %s", buf);
  232. check_magic('d');
  233. read_str("build time");
  234. applog(LOG_DEBUG, " Build time: %s", buf);
  235. check_magic('e');
  236. if (1 != fread(buf, 4, 1, f))
  237. bailout(LOG_ERR, "Error reading ModMiner firmware (data len)");
  238. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  239. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  240. SOCKETTYPE fd = modminer->device_fd;
  241. applog(LOG_WARNING, "%s %u: Programming %s... DO NOT EXIT CGMINER UNTIL COMPLETE", modminer->api->name, modminer->device_id, modminer->device_path);
  242. buf[0] = '\x05'; // Program Bitstream
  243. buf[1] = fpgaid;
  244. buf[2] = (len >> 0) & 0xff;
  245. buf[3] = (len >> 8) & 0xff;
  246. buf[4] = (len >> 16) & 0xff;
  247. buf[5] = (len >> 24) & 0xff;
  248. if (6 != write(fd, buf, 6))
  249. bailout2(LOG_ERR, "%s %u: Error programming %s (cmd)", modminer->api->name, modminer->device_id, modminer->device_path);
  250. status_read("cmd reply");
  251. ssize_t buflen;
  252. while (len) {
  253. buflen = len < 32 ? len : 32;
  254. if (fread(buf, buflen, 1, f) != 1)
  255. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", modminer->api->name, modminer->device_id, modminer->device_path, len);
  256. if (write(fd, buf, buflen) != buflen)
  257. bailout2(LOG_ERR, "%s %u: Error programming %s (data)", modminer->api->name, modminer->device_id, modminer->device_path);
  258. status_read("status");
  259. len -= buflen;
  260. }
  261. status_read("final status");
  262. applog(LOG_WARNING, "%s %u: Done programming %s", modminer->api->name, modminer->device_id, modminer->device_path);
  263. return true;
  264. }
  265. static bool modminer_device_prepare(struct cgpu_info *modminer)
  266. {
  267. int fd = serial_open(modminer->device_path, 0, 10, true);
  268. if (unlikely(-1 == fd))
  269. bailout(LOG_ERR, "%s %u: Failed to open %s", modminer->api->name, modminer->device_id, modminer->device_path);
  270. modminer->device_fd = fd;
  271. applog(LOG_INFO, "%s %u: Opened %s", modminer->api->name, modminer->device_id, modminer->device_path);
  272. struct timeval now;
  273. gettimeofday(&now, NULL);
  274. get_datestamp(modminer->init, &now);
  275. return true;
  276. }
  277. #undef bailout
  278. static bool modminer_fpga_prepare(struct thr_info *thr)
  279. {
  280. struct cgpu_info *modminer = thr->cgpu;
  281. // Don't need to lock the mutex here,
  282. // since prepare runs from the main thread before the miner threads start
  283. if (modminer->device_fd == -1 && !modminer_device_prepare(modminer))
  284. return false;
  285. struct modminer_fpga_state *state;
  286. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  287. state->next_work_cmd[0] = '\x08'; // Send Job
  288. state->next_work_cmd[1] = thr->device_thread; // FPGA id
  289. state->shares_to_good = MODMINER_EARLY_UP;
  290. return true;
  291. }
  292. /*
  293. * Clocking rules:
  294. * If device exceeds cutoff temp - shut down - and decrease the clock by
  295. * MODMINER_OVERHEAT_CLOCK for when it restarts
  296. *
  297. * When to clock down:
  298. * If device overheats
  299. * or
  300. * If device gets MODMINER_HW_ERROR_PERCENT errors since last clock up or down
  301. * if clock is <= default it requires 2 HW to do this test
  302. * if clock is > default it only requires 1 HW to do this test
  303. *
  304. * When to clock up:
  305. * If device gets shares_to_good good shares in a row
  306. *
  307. * N.B. clock must always be a multiple of 2
  308. */
  309. static bool modminer_delta_clock(struct thr_info *thr, bool needlock, int delta, bool temp)
  310. {
  311. struct cgpu_info *modminer = thr->cgpu;
  312. struct modminer_fpga_state *state = thr->cgpu_data;
  313. char fpgaid = thr->device_thread;
  314. int fd = modminer->device_fd;
  315. unsigned char cmd[6], buf[1];
  316. struct timeval now;
  317. gettimeofday(&now, NULL);
  318. // Only do once if multiple shares per work or multiple reasons
  319. // Since the temperature down clock test is first in the code this is OK
  320. if (tdiff(&now, &(state->last_changed)) < 0.5)
  321. return false;
  322. // Update before possibly aborting to avoid repeating unnecessarily
  323. memcpy(&(state->last_changed), &now, sizeof(struct timeval));
  324. state->shares = 0;
  325. state->shares_last_hw = 0;
  326. state->hw_errors = 0;
  327. // If drop requested due to temperature, clock drop is always allowed
  328. if (!temp && delta < 0 && state->clock <= MODMINER_MIN_CLOCK)
  329. return false;
  330. if (delta > 0 && state->clock >= MODMINER_MAX_CLOCK)
  331. return false;
  332. if (delta < 0) {
  333. if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
  334. state->shares_to_good *= 2;
  335. else
  336. state->shares_to_good = MODMINER_TRY_UP;
  337. }
  338. state->clock += delta;
  339. cmd[0] = '\x06'; // set clock speed
  340. cmd[1] = fpgaid;
  341. cmd[2] = state->clock;
  342. cmd[3] = cmd[4] = cmd[5] = '\0';
  343. if (needlock)
  344. mutex_lock(&modminer->device_mutex);
  345. if (6 != write(fd, cmd, 6))
  346. bailout2(LOG_ERR, "%s%u.%u: Error writing (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  347. if (serial_read(fd, &buf, 1) != 1)
  348. bailout2(LOG_ERR, "%s%u.%u: Error reading (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  349. if (needlock)
  350. mutex_unlock(&modminer->device_mutex);
  351. applog(LOG_WARNING, "%s%u.%u: Set clock speed %sto %u", modminer->api->name, modminer->device_id, fpgaid, (delta < 0) ? "down " : (delta > 0 ? "up " : ""), state->clock);
  352. return true;
  353. }
  354. static bool modminer_fpga_init(struct thr_info *thr)
  355. {
  356. struct cgpu_info *modminer = thr->cgpu;
  357. struct modminer_fpga_state *state = thr->cgpu_data;
  358. int fd;
  359. char fpgaid = thr->device_thread;
  360. unsigned char cmd[2], buf[4];
  361. mutex_lock(&modminer->device_mutex);
  362. fd = modminer->device_fd;
  363. if (fd == -1) {
  364. // Died in another thread...
  365. mutex_unlock(&modminer->device_mutex);
  366. return false;
  367. }
  368. cmd[0] = '\x04'; // Read USER code (bitstream id)
  369. cmd[1] = fpgaid;
  370. if (write(fd, cmd, 2) != 2)
  371. bailout2(LOG_ERR, "%s%u.%u: Error writing (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  372. if (serial_read(fd, buf, 4) != 4)
  373. bailout2(LOG_ERR, "%s%u.%u: Error reading (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  374. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  375. applog(LOG_ERR, "%s%u.%u: FPGA not programmed", modminer->api->name, modminer->device_id, fpgaid);
  376. if (!modminer_fpga_upload_bitstream(modminer))
  377. return false;
  378. }
  379. else
  380. applog(LOG_DEBUG, "%s%u.%u: FPGA is already programmed :)", modminer->api->name, modminer->device_id, fpgaid);
  381. state->clock = MODMINER_DEF_CLOCK;
  382. modminer_delta_clock(thr, false, MODMINER_CLOCK_SET, false);
  383. mutex_unlock(&modminer->device_mutex);
  384. thr->primary_thread = true;
  385. return true;
  386. }
  387. static void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  388. {
  389. char info[18] = " | ";
  390. int tc = modminer->threads;
  391. bool havetemp = false;
  392. int i;
  393. if (tc > 4)
  394. tc = 4;
  395. for (i = tc - 1; i >= 0; --i) {
  396. struct thr_info *thr = modminer->thr[i];
  397. struct modminer_fpga_state *state = thr->cgpu_data;
  398. float temp = state->temp;
  399. info[i*3+2] = '/';
  400. if (temp) {
  401. havetemp = true;
  402. if (temp > 9)
  403. info[i*3+0] = 0x30 + (temp / 10);
  404. info[i*3+1] = 0x30 + ((int)temp % 10);
  405. }
  406. }
  407. if (havetemp) {
  408. info[tc*3-1] = ' ';
  409. info[tc*3] = 'C';
  410. strcat(buf, info);
  411. }
  412. else
  413. strcat(buf, " | ");
  414. }
  415. static bool modminer_prepare_next_work(struct modminer_fpga_state *state, struct work *work)
  416. {
  417. char *midstate = state->next_work_cmd + 2;
  418. char *taildata = midstate + 32;
  419. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  420. return false;
  421. memcpy(midstate, work->midstate, 32);
  422. memcpy(taildata, work->data + 64, 12);
  423. return true;
  424. }
  425. static bool modminer_start_work(struct thr_info *thr)
  426. {
  427. fd_set fds;
  428. struct cgpu_info *modminer = thr->cgpu;
  429. struct modminer_fpga_state *state = thr->cgpu_data;
  430. char fpgaid = thr->device_thread;
  431. SOCKETTYPE fd = modminer->device_fd;
  432. char buf[1];
  433. mutex_lock(&modminer->device_mutex);
  434. if (46 != write(fd, state->next_work_cmd, 46))
  435. bailout2(LOG_ERR, "%s%u.%u: Error writing (start work)", modminer->api->name, modminer->device_id, fpgaid);
  436. gettimeofday(&state->tv_workstart, NULL);
  437. state->hashes = 0;
  438. status_read("start work");
  439. mutex_unlock(&modminer->device_mutex);
  440. return true;
  441. }
  442. #define work_restart(thr) thr->work_restart
  443. static uint64_t modminer_process_results(struct thr_info *thr)
  444. {
  445. struct cgpu_info *modminer = thr->cgpu;
  446. struct modminer_fpga_state *state = thr->cgpu_data;
  447. char fpgaid = thr->device_thread;
  448. int fd = modminer->device_fd;
  449. struct work *work = &state->running_work;
  450. char cmd[2], temperature[2];
  451. uint32_t nonce;
  452. long iter;
  453. uint32_t curr_hw_errors;
  454. // \x0a is 1 byte temperature
  455. // \x0d is 2 byte temperature
  456. cmd[0] = '\x0d';
  457. cmd[1] = fpgaid;
  458. mutex_lock(&modminer->device_mutex);
  459. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 2) == 2)
  460. {
  461. // Only accurate to 2 and a bit places
  462. state->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  463. if (!fpgaid)
  464. modminer->temp = state->temp;
  465. if (state->temp >= MODMINER_OVERHEAT_TEMP) {
  466. if (state->temp >= MODMINER_CUTOFF_TEMP) {
  467. applog(LOG_WARNING, "%s%u.%u: Hit thermal cutoff limit (%f) at %f, disabling device!", modminer->api->name, modminer->device_id, fpgaid, MODMINER_CUTOFF_TEMP, state->temp);
  468. modminer_delta_clock(thr, true, MODMINER_OVERHEAT_CLOCK, true);
  469. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  470. modminer->deven = DEV_RECOVER;
  471. } else {
  472. applog(LOG_WARNING, "%s%u.%u Overheat limit (%f) reached %f", modminer->api->name, modminer->device_id, fpgaid, MODMINER_OVERHEAT_TEMP, state->temp);
  473. modminer_delta_clock(thr, true, MODMINER_CLOCK_DOWN, true);
  474. dev_error(modminer, REASON_DEV_OVER_HEAT);
  475. }
  476. }
  477. }
  478. cmd[0] = '\x09';
  479. iter = 200;
  480. while (1) {
  481. if (write(fd, cmd, 2) != 2)
  482. bailout2(LOG_ERR, "%s%u.%u: Error reading (get nonce)", modminer->api->name, modminer->device_id, fpgaid);
  483. serial_read(fd, &nonce, 4);
  484. mutex_unlock(&modminer->device_mutex);
  485. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  486. state->shares++;
  487. state->no_nonce_counter = 0;
  488. curr_hw_errors = state->hw_errors;
  489. submit_nonce(thr, work, nonce);
  490. if (state->hw_errors > curr_hw_errors) {
  491. state->shares_last_hw = state->shares;
  492. if (state->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  493. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  494. if (pct >= MODMINER_HW_ERROR_PERCENT)
  495. modminer_delta_clock(thr, true, MODMINER_CLOCK_DOWN, false);
  496. }
  497. } else {
  498. // If we've reached the required good shares in a row then clock up
  499. if ((state->shares - state->shares_last_hw) >= state->shares_to_good)
  500. modminer_delta_clock(thr, true, MODMINER_CLOCK_UP, false);
  501. }
  502. } else if (++state->no_nonce_counter > 18000) {
  503. // TODO: NFI what this is - but will be gone
  504. // when the threading rewrite is done
  505. state->no_nonce_counter = 0;
  506. modminer_delta_clock(thr, true, MODMINER_CLOCK_DOWN, false);
  507. }
  508. if (work_restart(thr))
  509. break;
  510. nmsleep(10);
  511. if (work_restart(thr) || !--iter)
  512. break;
  513. mutex_lock(&modminer->device_mutex);
  514. }
  515. struct timeval tv_workend, elapsed;
  516. gettimeofday(&tv_workend, NULL);
  517. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  518. uint64_t hashes = (uint64_t)state->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  519. if (hashes > 0xffffffff)
  520. hashes = 0xffffffff;
  521. else
  522. if (hashes <= state->hashes)
  523. hashes = 1;
  524. else
  525. hashes -= state->hashes;
  526. state->hashes += hashes;
  527. return hashes;
  528. }
  529. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  530. {
  531. struct modminer_fpga_state *state = thr->cgpu_data;
  532. int64_t hashes = 0;
  533. bool startwork;
  534. startwork = modminer_prepare_next_work(state, work);
  535. if (state->work_running) {
  536. hashes = modminer_process_results(thr);
  537. if (work_restart(thr)) {
  538. state->work_running = false;
  539. return 0;
  540. }
  541. } else
  542. state->work_running = true;
  543. if (startwork) {
  544. if (!modminer_start_work(thr))
  545. return -1;
  546. __copy_work(&state->running_work, work);
  547. }
  548. // This is intentionally early
  549. work->blk.nonce += hashes;
  550. return hashes;
  551. }
  552. static void modminer_hw_error(struct thr_info *thr)
  553. {
  554. struct modminer_fpga_state *state = thr->cgpu_data;
  555. state->hw_errors++;
  556. }
  557. static void modminer_fpga_shutdown(struct thr_info *thr)
  558. {
  559. free(thr->cgpu_data);
  560. }
  561. struct device_api modminer_api = {
  562. .dname = "modminer",
  563. .name = "MMQ",
  564. .api_detect = modminer_detect,
  565. .get_statline_before = get_modminer_statline_before,
  566. .thread_prepare = modminer_fpga_prepare,
  567. .thread_init = modminer_fpga_init,
  568. .scanhash = modminer_scanhash,
  569. .hw_error = modminer_hw_error,
  570. .thread_shutdown = modminer_fpga_shutdown,
  571. };