driver-modminer.c 25 KB

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