driver-modminer.c 23 KB

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