driver-modminer.c 22 KB

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