driver-modminer.c 23 KB

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