driver-modminer.c 23 KB

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