driver-modminer.c 25 KB

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