driver-modminer.c 23 KB

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