driver-modminer.c 24 KB

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