driver-modminer.c 23 KB

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