driver-modminer.c 18 KB

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