driver-modminer.c 25 KB

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