driver-modminer.c 24 KB

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