driver-modminer.c 25 KB

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