driver-modminer.c 22 KB

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