driver-modminer.c 22 KB

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