driver-modminer.c 23 KB

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