driver-modminer.c 22 KB

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