driver-modminer.c 25 KB

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