driver-modminer.c 22 KB

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