driver-modminer.c 19 KB

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