driver-modminer.c 21 KB

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