driver-modminer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include "fpgautils.h"
  14. #include "logging.h"
  15. #include "miner.h"
  16. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  17. #define BISTREAM_USER_ID "\2\4$B"
  18. struct device_api modminer_api;
  19. struct modminer_fpga_state {
  20. bool work_running;
  21. struct work running_work;
  22. struct timeval tv_workstart;
  23. uint32_t hashes;
  24. char next_work_cmd[46];
  25. unsigned char clock;
  26. int no_nonce_counter;
  27. int bad_share_counter;
  28. int good_share_counter;
  29. time_t last_cutoff_reduced;
  30. unsigned char temp;
  31. unsigned char pdone;
  32. };
  33. static inline bool
  34. _bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...)
  35. {
  36. if (fd != -1)
  37. serial_close(fd);
  38. if (modminer) {
  39. modminer->device_fd = -1;
  40. mutex_unlock(&modminer->device_mutex);
  41. }
  42. va_list ap;
  43. va_start(ap, fmt);
  44. vapplog(prio, fmt, ap);
  45. va_end(ap);
  46. return false;
  47. }
  48. #define bailout(...) return _bailout(fd, NULL, __VA_ARGS__);
  49. static bool
  50. modminer_detect_one(const char *devpath)
  51. {
  52. int fd = serial_open(devpath, 0, 10, true);
  53. if (unlikely(fd == -1))
  54. bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath);
  55. char buf[0x100];
  56. size_t len;
  57. // Sending a "ping" first, to workaround bug in new firmware betas (see issue #62)
  58. // Sending 45 noops, just in case the device was left in "start job" reading
  59. (void)(write(fd, "\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", 46) ?:0);
  60. while (serial_read(fd, buf, sizeof(buf)) > 0)
  61. ;
  62. if (1 != write(fd, "\x01", 1)) // Get version
  63. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath);
  64. len = serial_read(fd, buf, sizeof(buf)-1);
  65. if (len < 1)
  66. bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath);
  67. buf[len] = '\0';
  68. char*devname = strdup(buf);
  69. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  70. if (1 != write(fd, "\x02", 1)) // Get FPGA count
  71. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath);
  72. len = read(fd, buf, 1);
  73. if (len < 1)
  74. bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath);
  75. if (!buf[0])
  76. bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath);
  77. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  78. serial_close(fd);
  79. struct cgpu_info *modminer;
  80. modminer = calloc(1, sizeof(*modminer));
  81. modminer->api = &modminer_api;
  82. mutex_init(&modminer->device_mutex);
  83. modminer->device_path = strdup(devpath);
  84. modminer->device_fd = -1;
  85. modminer->deven = DEV_ENABLED;
  86. modminer->threads = buf[0];
  87. modminer->name = devname;
  88. modminer->cutofftemp = 85;
  89. return add_cgpu(modminer);
  90. }
  91. #undef bailout
  92. static int
  93. modminer_detect_auto()
  94. {
  95. return
  96. serial_autodetect_udev (modminer_detect_one, "*ModMiner*") ?:
  97. serial_autodetect_devserial(modminer_detect_one, "BTCFPGA_ModMiner") ?:
  98. 0;
  99. }
  100. static void
  101. modminer_detect()
  102. {
  103. serial_detect_auto(modminer_api.dname, modminer_detect_one, modminer_detect_auto);
  104. }
  105. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  106. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  107. #define bailout3(...) _bailout(fd, modminer, __VA_ARGS__);
  108. static bool
  109. modminer_reopen(struct cgpu_info*modminer)
  110. {
  111. close(modminer->device_fd);
  112. int fd = serial_open(modminer->device_path, 0, 10, true);
  113. if (unlikely(-1 == fd)) {
  114. applog(LOG_ERR, "%s %u: Failed to reopen %s", modminer->api->name, modminer->device_id, modminer->device_path);
  115. return false;
  116. }
  117. modminer->device_fd = fd;
  118. return true;
  119. }
  120. #define safebailout(...) do { \
  121. bool _safebailoutrv; \
  122. applog(__VA_ARGS__); \
  123. state->work_running = false; \
  124. _safebailoutrv = modminer_reopen(modminer); \
  125. mutex_unlock(&modminer->device_mutex); \
  126. return _safebailoutrv ? 0 : -1; \
  127. } while(0)
  128. #define check_magic(L) do { \
  129. if (1 != fread(buf, 1, 1, f)) \
  130. bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \
  131. if (buf[0] != L) \
  132. bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \
  133. } while(0)
  134. #define read_str(eng) do { \
  135. if (1 != fread(buf, 2, 1, f)) \
  136. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \
  137. len = (ubuf[0] << 8) | ubuf[1]; \
  138. if (len >= sizeof(buf)) \
  139. bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \
  140. if (1 != fread(buf, len, 1, f)) \
  141. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \
  142. buf[len] = '\0'; \
  143. } while(0)
  144. #define status_read(eng) do { \
  145. FD_SET(fd, &fds); \
  146. select(fd+1, &fds, NULL, NULL, NULL); \
  147. if (1 != read(fd, buf, 1)) \
  148. bailout2(LOG_ERR, "%s %u: Error programming %s (" eng ")", modminer->api->name, modminer->device_id, modminer->device_path); \
  149. if (buf[0] != 1) \
  150. bailout2(LOG_ERR, "%s %u: Wrong " eng " programming %s", modminer->api->name, modminer->device_id, modminer->device_path); \
  151. } while(0)
  152. static bool
  153. modminer_fpga_upload_bitstream(struct cgpu_info*modminer)
  154. {
  155. struct modminer_fpga_state *state = modminer->thr[0]->cgpu_data;
  156. fd_set fds;
  157. char buf[0x100];
  158. unsigned char *ubuf = (unsigned char*)buf;
  159. unsigned long len, flen;
  160. char *p;
  161. const char *fwfile = BITSTREAM_FILENAME;
  162. char fpgaid = 4; // "all FPGAs"
  163. FILE *f = open_bitstream("modminer", fwfile);
  164. if (!f)
  165. bailout(LOG_ERR, "Error opening ModMiner firmware file %s", fwfile);
  166. if (1 != fread(buf, 2, 1, f))
  167. bailout(LOG_ERR, "Error reading ModMiner firmware (magic)");
  168. if (buf[0] || buf[1] != 9)
  169. bailout(LOG_ERR, "ModMiner firmware has wrong magic (9)");
  170. if (-1 == fseek(f, 11, SEEK_CUR))
  171. bailout(LOG_ERR, "ModMiner firmware seek failed");
  172. check_magic('a');
  173. read_str("design name");
  174. applog(LOG_DEBUG, "ModMiner firmware file %s info:", fwfile);
  175. applog(LOG_DEBUG, " Design name: %s", buf);
  176. p = strrchr(buf, ';') ?: buf;
  177. p = strrchr(buf, '=') ?: p;
  178. if (p[0] == '=')
  179. ++p;
  180. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  181. if (p[0] != '\0')
  182. bailout(LOG_ERR, "Bad usercode in ModMiner firmware file");
  183. if (fwusercode == 0xffffffff)
  184. bailout(LOG_ERR, "ModMiner firmware doesn't support user code");
  185. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  186. check_magic('b');
  187. read_str("part number");
  188. applog(LOG_DEBUG, " Part number: %s", buf);
  189. check_magic('c');
  190. read_str("build date");
  191. applog(LOG_DEBUG, " Build date: %s", buf);
  192. check_magic('d');
  193. read_str("build time");
  194. applog(LOG_DEBUG, " Build time: %s", buf);
  195. check_magic('e');
  196. if (1 != fread(buf, 4, 1, f))
  197. bailout(LOG_ERR, "Error reading ModMiner firmware (data len)");
  198. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  199. flen = len;
  200. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  201. int fd = modminer->device_fd;
  202. applog(LOG_WARNING, "%s %u: Programming %s... DO NOT EXIT UNTIL COMPLETE", modminer->api->name, modminer->device_id, modminer->device_path);
  203. buf[0] = '\x05'; // Program Bitstream
  204. buf[1] = fpgaid;
  205. buf[2] = (len >> 0) & 0xff;
  206. buf[3] = (len >> 8) & 0xff;
  207. buf[4] = (len >> 16) & 0xff;
  208. buf[5] = (len >> 24) & 0xff;
  209. if (6 != write(fd, buf, 6))
  210. bailout2(LOG_ERR, "%s %u: Error programming %s (cmd)", modminer->api->name, modminer->device_id, modminer->device_path);
  211. status_read("cmd reply");
  212. ssize_t buflen;
  213. char nextstatus = 10;
  214. while (len) {
  215. buflen = len < 32 ? len : 32;
  216. if (fread(buf, buflen, 1, f) != 1)
  217. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", modminer->api->name, modminer->device_id, modminer->device_path, len);
  218. if (write(fd, buf, buflen) != buflen)
  219. bailout2(LOG_ERR, "%s %u: Error programming %s (data)", modminer->api->name, modminer->device_id, modminer->device_path);
  220. state->pdone = 100 - ((len * 100) / flen);
  221. if (state->pdone >= nextstatus)
  222. {
  223. nextstatus += 10;
  224. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", modminer->api->name, modminer->device_id, modminer->device_path, state->pdone);
  225. }
  226. status_read("status");
  227. len -= buflen;
  228. }
  229. status_read("final status");
  230. applog(LOG_WARNING, "%s %u: Done programming %s", modminer->api->name, modminer->device_id, modminer->device_path);
  231. return true;
  232. }
  233. static bool
  234. modminer_device_prepare(struct cgpu_info *modminer)
  235. {
  236. int fd = serial_open(modminer->device_path, 0, 10, true);
  237. if (unlikely(-1 == fd))
  238. bailout(LOG_ERR, "%s %u: Failed to open %s", modminer->api->name, modminer->device_id, modminer->device_path);
  239. modminer->device_fd = fd;
  240. applog(LOG_INFO, "%s %u: Opened %s", modminer->api->name, modminer->device_id, modminer->device_path);
  241. struct timeval now;
  242. gettimeofday(&now, NULL);
  243. get_datestamp(modminer->init, &now);
  244. return true;
  245. }
  246. #undef bailout
  247. static bool
  248. modminer_fpga_prepare(struct thr_info *thr)
  249. {
  250. struct cgpu_info *modminer = thr->cgpu;
  251. // Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
  252. if (modminer->device_fd == -1 && !modminer_device_prepare(modminer))
  253. return false;
  254. struct modminer_fpga_state *state;
  255. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  256. state->next_work_cmd[0] = '\x08'; // Send Job
  257. state->next_work_cmd[1] = thr->device_thread; // FPGA id
  258. return true;
  259. }
  260. static bool
  261. modminer_reduce_clock(struct thr_info*thr, bool needlock)
  262. {
  263. struct cgpu_info*modminer = thr->cgpu;
  264. struct modminer_fpga_state *state = thr->cgpu_data;
  265. char fpgaid = thr->device_thread;
  266. int fd;
  267. unsigned char cmd[6], buf[1];
  268. if (state->clock <= 100)
  269. return false;
  270. cmd[0] = '\x06'; // set clock speed
  271. cmd[1] = fpgaid;
  272. cmd[2] = state->clock -= 2;
  273. cmd[3] = cmd[4] = cmd[5] = '\0';
  274. if (needlock)
  275. mutex_lock(&modminer->device_mutex);
  276. fd = modminer->device_fd;
  277. if (6 != write(fd, cmd, 6))
  278. bailout2(LOG_ERR, "%s %u.%u: Error writing (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  279. if (serial_read(fd, &buf, 1) != 1)
  280. bailout2(LOG_ERR, "%s %u.%u: Error reading (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  281. if (needlock)
  282. mutex_unlock(&modminer->device_mutex);
  283. return true;
  284. }
  285. static bool
  286. modminer_fpga_init(struct thr_info *thr)
  287. {
  288. struct cgpu_info *modminer = thr->cgpu;
  289. struct modminer_fpga_state *state = thr->cgpu_data;
  290. int fd;
  291. char fpgaid = thr->device_thread;
  292. unsigned char cmd[2], buf[4];
  293. mutex_lock(&modminer->device_mutex);
  294. fd = modminer->device_fd;
  295. if (fd == -1) {
  296. // Died in another thread...
  297. mutex_unlock(&modminer->device_mutex);
  298. return false;
  299. }
  300. cmd[0] = '\x04'; // Read USER code (bitstream id)
  301. cmd[1] = fpgaid;
  302. if (write(fd, cmd, 2) != 2)
  303. bailout2(LOG_ERR, "%s %u.%u: Error writing (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  304. if (serial_read(fd, buf, 4) != 4)
  305. bailout2(LOG_ERR, "%s %u.%u: Error reading (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  306. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  307. applog(LOG_ERR, "%s %u.%u: FPGA not programmed", modminer->api->name, modminer->device_id, fpgaid);
  308. if (!modminer_fpga_upload_bitstream(modminer))
  309. return false;
  310. }
  311. else
  312. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)", modminer->api->name, modminer->device_id, fpgaid);
  313. state->pdone = 101;
  314. state->clock = 212; // Will be reduced to 210 by modminer_reduce_clock
  315. modminer_reduce_clock(thr, false);
  316. applog(LOG_WARNING, "%s %u.%u: Setting clock speed to %u", modminer->api->name, modminer->device_id, fpgaid, state->clock);
  317. mutex_unlock(&modminer->device_mutex);
  318. thr->primary_thread = true;
  319. return true;
  320. }
  321. static void
  322. get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  323. {
  324. char info[18] = " | ";
  325. int tc = modminer->threads;
  326. bool havetemp = false;
  327. int i;
  328. char pdone = ((struct modminer_fpga_state*)(modminer->thr[0]->cgpu_data))->pdone;
  329. if (pdone != 101) {
  330. sprintf(&info[1], "%3d%%", pdone);
  331. info[5] = ' ';
  332. strcat(buf, info);
  333. return;
  334. }
  335. if (tc > 4)
  336. tc = 4;
  337. for (i = tc - 1; i >= 0; --i) {
  338. struct thr_info*thr = modminer->thr[i];
  339. struct modminer_fpga_state *state = thr->cgpu_data;
  340. unsigned char temp = state->temp;
  341. info[i*3+2] = '/';
  342. if (temp) {
  343. havetemp = true;
  344. if (temp > 9)
  345. info[i*3+0] = 0x30 + (temp / 10);
  346. info[i*3+1] = 0x30 + (temp % 10);
  347. }
  348. }
  349. if (havetemp) {
  350. info[tc*3-1] = ' ';
  351. info[tc*3] = 'C';
  352. strcat(buf, info);
  353. }
  354. else
  355. strcat(buf, " | ");
  356. }
  357. static struct api_data*
  358. get_modminer_api_extra_device_status(struct cgpu_info*modminer)
  359. {
  360. struct api_data*root = NULL;
  361. static char *k[4] = {"Board0", "Board1", "Board2", "Board3"};
  362. int i;
  363. for (i = modminer->threads - 1; i >= 0; --i) {
  364. struct thr_info*thr = modminer->thr[i];
  365. struct modminer_fpga_state *state = thr->cgpu_data;
  366. json_t *o = json_object();
  367. if (state->temp)
  368. json_object_set(o, "Temperature", json_integer(state->temp));
  369. json_object_set(o, "Frequency", json_real((double)state->clock * 1000000.));
  370. json_object_set(o, "Hardware Errors", json_integer(state->bad_share_counter));
  371. json_object_set(o, "Valid Nonces", json_integer(state->good_share_counter));
  372. root = api_add_json(root, k[i], o, false);
  373. json_decref(o);
  374. }
  375. return root;
  376. }
  377. static bool
  378. modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work)
  379. {
  380. char *midstate = state->next_work_cmd + 2;
  381. char *taildata = midstate + 32;
  382. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  383. return false;
  384. memcpy(midstate, work->midstate, 32);
  385. memcpy(taildata, work->data + 64, 12);
  386. return true;
  387. }
  388. static bool
  389. modminer_start_work(struct thr_info*thr)
  390. {
  391. fd_set fds;
  392. struct cgpu_info*modminer = thr->cgpu;
  393. struct modminer_fpga_state *state = thr->cgpu_data;
  394. char fpgaid = thr->device_thread;
  395. int fd;
  396. char buf[1];
  397. mutex_lock(&modminer->device_mutex);
  398. fd = modminer->device_fd;
  399. if (46 != write(fd, state->next_work_cmd, 46))
  400. bailout2(LOG_ERR, "%s %u.%u: Error writing (start work)", modminer->api->name, modminer->device_id, fpgaid);
  401. gettimeofday(&state->tv_workstart, NULL);
  402. state->hashes = 0;
  403. status_read("start work");
  404. mutex_unlock(&modminer->device_mutex);
  405. return true;
  406. }
  407. #define work_restart(thr) thr->work_restart
  408. static int64_t
  409. modminer_process_results(struct thr_info*thr)
  410. {
  411. struct cgpu_info*modminer = thr->cgpu;
  412. struct modminer_fpga_state *state = thr->cgpu_data;
  413. char fpgaid = thr->device_thread;
  414. int fd;
  415. struct work *work = &state->running_work;
  416. char cmd[2], temperature;
  417. uint32_t nonce;
  418. long iter;
  419. bool bad;
  420. cmd[0] = '\x0a';
  421. cmd[1] = fpgaid;
  422. mutex_lock(&modminer->device_mutex);
  423. #ifdef WIN32
  424. /* Workaround for bug in Windows driver */
  425. if (!modminer_reopen(modminer))
  426. return -1;
  427. #endif
  428. fd = modminer->device_fd;
  429. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1)
  430. {
  431. state->temp = temperature;
  432. if (!fpgaid)
  433. modminer->temp = (float)temperature;
  434. if (temperature > modminer->cutofftemp - 2) {
  435. if (temperature > modminer->cutofftemp) {
  436. applog(LOG_WARNING, "%s %u.%u: Hit thermal cutoff limit, disabling device!", modminer->api->name, modminer->device_id, fpgaid);
  437. modminer->deven = DEV_RECOVER;
  438. modminer->device_last_not_well = time(NULL);
  439. modminer->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  440. ++modminer->dev_thermal_cutoff_count;
  441. } else {
  442. time_t now = time(NULL);
  443. if (state->last_cutoff_reduced != now) {
  444. state->last_cutoff_reduced = now;
  445. modminer_reduce_clock(thr, false);
  446. applog(LOG_WARNING, "%s %u.%u: Drop clock speed to %u (temp: %d)", modminer->api->name, modminer->device_id, fpgaid, state->clock, temperature);
  447. }
  448. }
  449. }
  450. }
  451. cmd[0] = '\x09';
  452. iter = 200;
  453. while (1) {
  454. if (write(fd, cmd, 2) != 2)
  455. safebailout(LOG_ERR, "%s %u: Error writing (get nonce %u)", modminer->api->name, modminer->device_id, fpgaid);
  456. if (4 != serial_read(fd, &nonce, 4))
  457. safebailout(LOG_ERR, "%s %u: Short read (get nonce %u)", modminer->api->name, modminer->device_id, fpgaid);
  458. mutex_unlock(&modminer->device_mutex);
  459. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  460. state->no_nonce_counter = 0;
  461. bad = !test_nonce(work, nonce, false);
  462. if (!bad)
  463. {
  464. ++state->good_share_counter;
  465. submit_nonce(thr, work, nonce);
  466. }
  467. else
  468. if (unlikely((!state->good_share_counter) && nonce == 0xffffff00))
  469. {
  470. // Firmware returns 0xffffff00 immediately if we set clockspeed too high; but it's not a hw error and shouldn't affect future downclocking
  471. modminer_reduce_clock(thr, true);
  472. applog(LOG_WARNING, "%s %u.%u: Drop clock speed to %u (init)", modminer->api->name, modminer->device_id, fpgaid, state->clock);
  473. }
  474. else {
  475. ++hw_errors;
  476. ++modminer->hw_errors;
  477. ++state->bad_share_counter;
  478. if (state->bad_share_counter * 100 > 1000 + state->bad_share_counter + state->good_share_counter)
  479. {
  480. // Only reduce clocks if hardware errors are more than ~1% of results
  481. modminer_reduce_clock(thr, true);
  482. applog(LOG_WARNING, "%s %u.%u: Drop clock speed to %u (%d%% hw err)", modminer->api->name, modminer->device_id, fpgaid, state->clock, state->bad_share_counter * 100 / (state->bad_share_counter + state->good_share_counter));
  483. }
  484. }
  485. }
  486. else
  487. if (++state->no_nonce_counter > 0x20000) {
  488. state->no_nonce_counter = 0;
  489. modminer_reduce_clock(thr, true);
  490. applog(LOG_WARNING, "%s %u.%u: Drop clock speed to %u (no nonces)", modminer->api->name, modminer->device_id, fpgaid, state->clock);
  491. }
  492. if (work_restart(thr) || !--iter)
  493. break;
  494. usleep(1000);
  495. if (work_restart(thr))
  496. break;
  497. mutex_lock(&modminer->device_mutex);
  498. fd = modminer->device_fd;
  499. }
  500. struct timeval tv_workend, elapsed;
  501. gettimeofday(&tv_workend, NULL);
  502. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  503. uint64_t hashes = (uint64_t)state->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  504. if (hashes > 0xffffffff)
  505. hashes = 0xffffffff;
  506. else
  507. if (hashes <= state->hashes)
  508. hashes = 1;
  509. else
  510. hashes -= state->hashes;
  511. state->hashes += hashes;
  512. return hashes;
  513. }
  514. static int64_t
  515. modminer_scanhash(struct thr_info*thr, struct work*work, int64_t __maybe_unused max_nonce)
  516. {
  517. struct modminer_fpga_state *state = thr->cgpu_data;
  518. int64_t hashes = 0;
  519. bool startwork;
  520. startwork = modminer_prepare_next_work(state, work);
  521. if (state->work_running) {
  522. hashes = modminer_process_results(thr);
  523. if (work_restart(thr)) {
  524. state->work_running = false;
  525. return hashes;
  526. }
  527. } else
  528. state->work_running = true;
  529. if (startwork) {
  530. if (!modminer_start_work(thr))
  531. return -1;
  532. memcpy(&state->running_work, work, sizeof(state->running_work));
  533. }
  534. // This is intentionally early
  535. work->blk.nonce += hashes;
  536. return hashes;
  537. }
  538. static void
  539. modminer_fpga_shutdown(struct thr_info *thr)
  540. {
  541. free(thr->cgpu_data);
  542. }
  543. struct device_api modminer_api = {
  544. .dname = "modminer",
  545. .name = "MMQ",
  546. .api_detect = modminer_detect,
  547. .get_statline_before = get_modminer_statline_before,
  548. .get_api_extra_device_status = get_modminer_api_extra_device_status,
  549. .thread_prepare = modminer_fpga_prepare,
  550. .thread_init = modminer_fpga_init,
  551. .scanhash = modminer_scanhash,
  552. .thread_shutdown = modminer_fpga_shutdown,
  553. };