driver-modminer.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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_drv modminer_drv;
  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. char buf[LOGBUFSIZ];
  73. va_start(ap, fmt);
  74. vsnprintf(buf, sizeof(buf), fmt, ap);
  75. va_end(ap);
  76. _applog(prio, buf);
  77. return false;
  78. }
  79. #define bailout(...) return _bailout(fd, NULL, __VA_ARGS__);
  80. // 45 noops sent when detecting, in case the device was left in "start job" reading
  81. 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";
  82. static bool
  83. modminer_detect_one(const char *devpath)
  84. {
  85. int fd = serial_open(devpath, 0, 10, true);
  86. if (unlikely(fd == -1))
  87. bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath);
  88. char buf[0x100];
  89. ssize_t len;
  90. // Sending a "ping" first, to workaround bug in new firmware betas (see issue #62)
  91. // Sending 45 noops, just in case the device was left in "start job" reading
  92. (void)(write(fd, NOOP, sizeof(NOOP)) ?:0);
  93. while (serial_read(fd, buf, sizeof(buf)) > 0)
  94. ;
  95. if (1 != write(fd, MODMINER_GET_VERSION, 1))
  96. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath);
  97. len = serial_read(fd, buf, sizeof(buf)-1);
  98. if (len < 1)
  99. bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath);
  100. buf[len] = '\0';
  101. char*devname = strdup(buf);
  102. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  103. if (serial_claim_v(devpath, &modminer_drv))
  104. return false;
  105. if (1 != write(fd, MODMINER_FPGA_COUNT, 1))
  106. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath);
  107. len = read(fd, buf, 1);
  108. if (len < 1)
  109. bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath);
  110. if (!buf[0])
  111. bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath);
  112. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  113. serial_close(fd);
  114. struct cgpu_info *modminer;
  115. modminer = calloc(1, sizeof(*modminer));
  116. modminer->drv = &modminer_drv;
  117. mutex_init(&modminer->device_mutex);
  118. modminer->device_path = strdup(devpath);
  119. modminer->device_fd = -1;
  120. modminer->deven = DEV_ENABLED;
  121. modminer->procs = buf[0];
  122. modminer->threads = buf[0];
  123. modminer->name = devname;
  124. modminer->cutofftemp = 85;
  125. return add_cgpu(modminer);
  126. }
  127. #undef bailout
  128. static int
  129. modminer_detect_auto()
  130. {
  131. return serial_autodetect(modminer_detect_one, "BTCFPGA", "ModMiner");
  132. }
  133. static void
  134. modminer_detect()
  135. {
  136. serial_detect_auto(&modminer_drv, modminer_detect_one, modminer_detect_auto);
  137. }
  138. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  139. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  140. #define bailout3(...) _bailout(fd, modminer, __VA_ARGS__);
  141. static bool
  142. modminer_reopen(struct cgpu_info*modminer)
  143. {
  144. close(modminer->device->device_fd);
  145. int fd = serial_open(modminer->device_path, 0, 10, true);
  146. if (unlikely(-1 == fd)) {
  147. applog(LOG_ERR, "%s: Failed to reopen %s", modminer->dev_repr, modminer->device_path);
  148. return false;
  149. }
  150. modminer->device->device_fd = fd;
  151. return true;
  152. }
  153. #define safebailout() do { \
  154. bool _safebailoutrv; \
  155. state->work_running = false; \
  156. _safebailoutrv = modminer_reopen(modminer); \
  157. mutex_unlock(mutexp); \
  158. return _safebailoutrv ? 0 : -1; \
  159. } while(0)
  160. #define check_magic(L) do { \
  161. if (1 != fread(buf, 1, 1, f)) \
  162. bailout(LOG_ERR, "Error reading ModMiner bitstream ('%c')", L); \
  163. if (buf[0] != L) \
  164. bailout(LOG_ERR, "ModMiner bitstream has wrong magic ('%c')", L); \
  165. } while(0)
  166. #define read_str(eng) do { \
  167. if (1 != fread(buf, 2, 1, f)) \
  168. bailout(LOG_ERR, "Error reading ModMiner bitstream (" eng " len)"); \
  169. len = (ubuf[0] << 8) | ubuf[1]; \
  170. if (len >= sizeof(buf)) \
  171. bailout(LOG_ERR, "ModMiner bitstream " eng " too long"); \
  172. if (1 != fread(buf, len, 1, f)) \
  173. bailout(LOG_ERR, "Error reading ModMiner bitstream (" eng ")"); \
  174. buf[len] = '\0'; \
  175. } while(0)
  176. #define status_read(eng) do { \
  177. FD_ZERO(&fds); \
  178. FD_SET(fd, &fds); \
  179. select(fd+1, &fds, NULL, NULL, NULL); \
  180. if (1 != read(fd, buf, 1)) \
  181. bailout2(LOG_ERR, "%s: Error programming %s (" eng ")", modminer->dev_repr, modminer->device_path); \
  182. if (buf[0] != 1) \
  183. bailout2(LOG_ERR, "%s: Wrong " eng " programming %s", modminer->dev_repr, modminer->device_path); \
  184. } while(0)
  185. static bool
  186. modminer_fpga_upload_bitstream(struct cgpu_info*modminer)
  187. {
  188. struct modminer_fpga_state *state = modminer->thr[0]->cgpu_data;
  189. fd_set fds;
  190. char buf[0x100];
  191. unsigned long len, flen;
  192. char fpgaid = FPGAID_ALL;
  193. FILE *f = open_xilinx_bitstream(modminer->drv->dname, modminer->dev_repr, BITSTREAM_FILENAME, &len);
  194. if (!f)
  195. return false;
  196. flen = len;
  197. int fd = modminer->device->device_fd;
  198. applog(LOG_WARNING, "%s: Programming %s... DO NOT EXIT UNTIL COMPLETE", modminer->dev_repr, modminer->device_path);
  199. buf[0] = MODMINER_PROGRAM;
  200. buf[1] = fpgaid;
  201. buf[2] = (len >> 0) & 0xff;
  202. buf[3] = (len >> 8) & 0xff;
  203. buf[4] = (len >> 16) & 0xff;
  204. buf[5] = (len >> 24) & 0xff;
  205. if (6 != write(fd, buf, 6))
  206. bailout2(LOG_ERR, "%s: Error programming %s (cmd)", modminer->dev_repr, modminer->device_path);
  207. status_read("cmd reply");
  208. ssize_t buflen;
  209. char nextstatus = 10;
  210. while (len) {
  211. buflen = len < 32 ? len : 32;
  212. if (fread(buf, buflen, 1, f) != 1)
  213. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", modminer->dev_repr, modminer->device_path, len);
  214. if (write(fd, buf, buflen) != buflen)
  215. bailout2(LOG_ERR, "%s: Error programming %s (data)", modminer->dev_repr, modminer->device_path);
  216. state->pdone = 100 - ((len * 100) / flen);
  217. if (state->pdone >= nextstatus)
  218. {
  219. nextstatus += 10;
  220. applog(LOG_WARNING, "%s: Programming %s... %d%% complete...", modminer->dev_repr, modminer->device_path, state->pdone);
  221. }
  222. status_read("status");
  223. len -= buflen;
  224. }
  225. status_read("final status");
  226. applog(LOG_WARNING, "%s: Done programming %s", modminer->dev_repr, modminer->device_path);
  227. return true;
  228. }
  229. static bool
  230. modminer_device_prepare(struct cgpu_info *modminer)
  231. {
  232. int fd = serial_open(modminer->device_path, 0, 10, true);
  233. if (unlikely(-1 == fd))
  234. bailout(LOG_ERR, "%s: Failed to open %s", modminer->dev_repr, modminer->device_path);
  235. modminer->device->device_fd = fd;
  236. applog(LOG_INFO, "%s: Opened %s", modminer->dev_repr, modminer->device_path);
  237. struct timeval now;
  238. gettimeofday(&now, NULL);
  239. get_datestamp(modminer->init, &now);
  240. return true;
  241. }
  242. #undef bailout
  243. static bool
  244. modminer_fpga_prepare(struct thr_info *thr)
  245. {
  246. struct cgpu_info *proc = thr->cgpu;
  247. struct cgpu_info *modminer = proc->device;
  248. // Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
  249. if (modminer->device->device_fd == -1 && !modminer_device_prepare(modminer))
  250. return false;
  251. struct modminer_fpga_state *state;
  252. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  253. dclk_prepare(&state->dclk);
  254. state->next_work_cmd[0] = MODMINER_SEND_WORK;
  255. state->next_work_cmd[1] = proc->proc_id; // FPGA id
  256. return true;
  257. }
  258. static bool
  259. modminer_change_clock(struct thr_info*thr, bool needlock, signed char delta)
  260. {
  261. struct cgpu_info*modminer = thr->cgpu;
  262. struct modminer_fpga_state *state = thr->cgpu_data;
  263. char fpgaid = modminer->proc_id;
  264. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  265. int fd;
  266. unsigned char cmd[6], buf[1];
  267. unsigned char clk;
  268. clk = (state->dclk.freqM * 2) + delta;
  269. cmd[0] = MODMINER_SET_CLOCK;
  270. cmd[1] = fpgaid;
  271. cmd[2] = clk;
  272. cmd[3] = cmd[4] = cmd[5] = '\0';
  273. if (needlock)
  274. mutex_lock(mutexp);
  275. fd = modminer->device->device_fd;
  276. if (6 != write(fd, cmd, 6))
  277. bailout2(LOG_ERR, "%s: Error writing (set frequency)", modminer->proc_repr);
  278. if (serial_read(fd, &buf, 1) != 1)
  279. bailout2(LOG_ERR, "%s: Error reading (set frequency)", modminer->proc_repr);
  280. if (needlock)
  281. mutex_unlock(mutexp);
  282. if (buf[0])
  283. state->dclk.freqM = clk / 2;
  284. else
  285. return false;
  286. return true;
  287. }
  288. static bool modminer_dclk_change_clock(struct thr_info*thr, int multiplier)
  289. {
  290. struct cgpu_info *modminer = thr->cgpu;
  291. struct modminer_fpga_state *state = thr->cgpu_data;
  292. uint8_t oldFreq = state->dclk.freqM;
  293. signed char delta = (multiplier - oldFreq) * 2;
  294. if (unlikely(!modminer_change_clock(thr, true, delta)))
  295. return false;
  296. dclk_msg_freqchange(modminer->proc_repr, oldFreq * 2, state->dclk.freqM * 2, NULL);
  297. return true;
  298. }
  299. static bool
  300. modminer_reduce_clock(struct thr_info*thr, bool needlock)
  301. {
  302. struct modminer_fpga_state *state = thr->cgpu_data;
  303. if (state->dclk.freqM <= MODMINER_MIN_CLOCK / 2)
  304. return false;
  305. return modminer_change_clock(thr, needlock, -2);
  306. }
  307. static bool _modminer_get_nonce(struct cgpu_info*modminer, char fpgaid, uint32_t*nonce)
  308. {
  309. int fd = modminer->device->device_fd;
  310. char cmd[2] = {MODMINER_CHECK_WORK, fpgaid};
  311. if (write(fd, cmd, 2) != 2) {
  312. applog(LOG_ERR, "%s: Error writing (get nonce)", modminer->proc_repr);
  313. return false;
  314. }
  315. if (4 != serial_read(fd, nonce, 4)) {
  316. applog(LOG_ERR, "%s: Short read (get nonce)", modminer->proc_repr);
  317. return false;
  318. }
  319. return true;
  320. }
  321. static bool
  322. modminer_fpga_init(struct thr_info *thr)
  323. {
  324. struct cgpu_info *modminer = thr->cgpu;
  325. struct modminer_fpga_state *state = thr->cgpu_data;
  326. int fd;
  327. char fpgaid = modminer->proc_id;
  328. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  329. uint32_t nonce;
  330. unsigned char cmd[2], buf[4];
  331. mutex_lock(mutexp);
  332. fd = modminer->device->device_fd;
  333. if (fd == -1) {
  334. // Died in another thread...
  335. mutex_unlock(mutexp);
  336. return false;
  337. }
  338. cmd[0] = MODMINER_GET_USERCODE;
  339. cmd[1] = fpgaid;
  340. if (write(fd, cmd, 2) != 2)
  341. bailout2(LOG_ERR, "%s: Error writing (read USER code)", modminer->proc_repr);
  342. if (serial_read(fd, buf, 4) != 4)
  343. bailout2(LOG_ERR, "%s: Error reading (read USER code)", modminer->proc_repr);
  344. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  345. applog(LOG_ERR, "%s: FPGA not programmed", modminer->proc_repr);
  346. if (!modminer_fpga_upload_bitstream(modminer))
  347. return false;
  348. } else if (opt_force_dev_init && modminer->status == LIFE_INIT) {
  349. applog(LOG_DEBUG, "%s: FPGA is already programmed, but --force-dev-init is set",
  350. modminer->proc_repr);
  351. if (!modminer_fpga_upload_bitstream(modminer))
  352. return false;
  353. }
  354. else
  355. applog(LOG_DEBUG, "%s: FPGA is already programmed :)", modminer->proc_repr);
  356. state->pdone = 101;
  357. state->dclk.freqM = MODMINER_MAX_CLOCK / 2 + 1; // Will be reduced immediately
  358. while (1) {
  359. if (state->dclk.freqM <= MODMINER_MIN_CLOCK / 2)
  360. bailout2(LOG_ERR, "%s: Hit minimum trying to find acceptable frequencies", modminer->proc_repr);
  361. --state->dclk.freqM;
  362. if (!modminer_change_clock(thr, false, 0))
  363. // MCU rejected assignment
  364. continue;
  365. if (!_modminer_get_nonce(modminer, fpgaid, &nonce))
  366. bailout2(LOG_ERR, "%s: Error detecting acceptable frequencies", modminer->proc_repr);
  367. if (!memcmp(&nonce, "\x00\xff\xff\xff", 4))
  368. // MCU took assignment, but disabled FPGA
  369. continue;
  370. break;
  371. }
  372. state->freqMaxMaxM =
  373. state->dclk.freqMaxM = state->dclk.freqM;
  374. if (MODMINER_DEF_CLOCK / 2 < state->dclk.freqM) {
  375. if (!modminer_change_clock(thr, false, -(state->dclk.freqM * 2 - MODMINER_DEF_CLOCK)))
  376. applog(LOG_WARNING, "%s: Failed to set desired initial frequency of %u", modminer->proc_repr, MODMINER_DEF_CLOCK);
  377. }
  378. state->dclk.freqMDefault = state->dclk.freqM;
  379. 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);
  380. mutex_unlock(mutexp);
  381. thr->primary_thread = true;
  382. return true;
  383. }
  384. static
  385. bool get_modminer_upload_percent(char *buf, struct cgpu_info *modminer)
  386. {
  387. char info[18] = " | ";
  388. char pdone = ((struct modminer_fpga_state*)(modminer->device->thr[0]->cgpu_data))->pdone;
  389. if (pdone != 101) {
  390. sprintf(&info[1], "%3d%%", pdone);
  391. info[5] = ' ';
  392. strcat(buf, info);
  393. return true;
  394. }
  395. return false;
  396. }
  397. static
  398. void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  399. {
  400. if (get_modminer_upload_percent(buf, modminer))
  401. return;
  402. struct thr_info*thr = modminer->thr[0];
  403. struct modminer_fpga_state *state = thr->cgpu_data;
  404. float gt = state->temp;
  405. if (gt > 0)
  406. tailsprintf(buf, "%5.1fC ", gt);
  407. else
  408. tailsprintf(buf, " ");
  409. tailsprintf(buf, " | ");
  410. }
  411. static
  412. void get_modminer_dev_statline_before(char *buf, struct cgpu_info *modminer)
  413. {
  414. if (get_modminer_upload_percent(buf, modminer))
  415. return;
  416. char info[18] = " | ";
  417. int tc = modminer->procs;
  418. bool havetemp = false;
  419. int i;
  420. if (tc > 4)
  421. tc = 4;
  422. for (i = 0; i < tc; ++i, modminer = modminer->next_proc) {
  423. struct thr_info*thr = modminer->thr[0];
  424. struct modminer_fpga_state *state = thr->cgpu_data;
  425. unsigned char temp = state->temp;
  426. info[i*3+2] = '/';
  427. if (temp) {
  428. havetemp = true;
  429. if (temp > 9)
  430. info[i*3+0] = 0x30 + (temp / 10);
  431. info[i*3+1] = 0x30 + (temp % 10);
  432. }
  433. }
  434. if (havetemp) {
  435. info[tc*3-1] = ' ';
  436. info[tc*3] = 'C';
  437. strcat(buf, info);
  438. }
  439. else
  440. strcat(buf, " | ");
  441. }
  442. static void modminer_get_temperature(struct cgpu_info *modminer, struct thr_info *thr)
  443. {
  444. struct modminer_fpga_state *state = thr->cgpu_data;
  445. #ifdef WIN32
  446. /* Workaround for bug in Windows driver */
  447. if (!modminer_reopen(modminer))
  448. return;
  449. #endif
  450. int fd = modminer->device->device_fd;
  451. int fpgaid = modminer->proc_id;
  452. char cmd[2] = {MODMINER_TEMP1, fpgaid};
  453. char temperature;
  454. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1)
  455. {
  456. state->temp = temperature;
  457. if (temperature > modminer->targettemp + opt_hysteresis) {
  458. {
  459. time_t now = time(NULL);
  460. if (state->last_cutoff_reduced != now) {
  461. state->last_cutoff_reduced = now;
  462. int oldFreq = state->dclk.freqM;
  463. if (modminer_reduce_clock(thr, false))
  464. applog(LOG_NOTICE, "%s: Frequency %s from %u to %u MHz (temp: %d)",
  465. modminer->proc_repr,
  466. (oldFreq > state->dclk.freqM ? "dropped" : "raised "),
  467. oldFreq * 2, state->dclk.freqM * 2,
  468. temperature
  469. );
  470. state->dclk.freqMaxM = state->dclk.freqM;
  471. }
  472. }
  473. }
  474. else
  475. if (state->dclk.freqMaxM < state->freqMaxMaxM && temperature < modminer->targettemp) {
  476. if (temperature < modminer->targettemp - opt_hysteresis) {
  477. state->dclk.freqMaxM = state->freqMaxMaxM;
  478. } else {
  479. ++state->dclk.freqMaxM;
  480. }
  481. }
  482. }
  483. }
  484. static bool modminer_get_stats(struct cgpu_info *modminer)
  485. {
  486. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  487. int hottest = 0;
  488. bool get_temp = (modminer->deven != DEV_ENABLED);
  489. // Getting temperature more efficiently while enabled
  490. for (int i = modminer->threads; i--; ) {
  491. struct thr_info*thr = modminer->thr[i];
  492. struct modminer_fpga_state *state = thr->cgpu_data;
  493. if (get_temp)
  494. {
  495. mutex_lock(mutexp);
  496. modminer_get_temperature(modminer, thr);
  497. mutex_unlock(mutexp);
  498. }
  499. int temp = state->temp;
  500. if (temp > hottest)
  501. hottest = temp;
  502. }
  503. modminer->temp = (float)hottest;
  504. return true;
  505. }
  506. static struct api_data*
  507. get_modminer_drv_extra_device_status(struct cgpu_info*modminer)
  508. {
  509. struct api_data*root = NULL;
  510. struct thr_info*thr = modminer->thr[0];
  511. struct modminer_fpga_state *state = thr->cgpu_data;
  512. double d;
  513. d = (double)state->dclk.freqM * 2;
  514. root = api_add_freq(root, "Frequency", &d, true);
  515. d = (double)state->dclk.freqMaxM * 2;
  516. root = api_add_freq(root, "Cool Max Frequency", &d, true);
  517. d = (double)state->freqMaxMaxM * 2;
  518. root = api_add_freq(root, "Max Frequency", &d, true);
  519. root = api_add_int(root, "Hardware Errors", &state->bad_share_counter, true);
  520. root = api_add_int(root, "Valid Nonces", &state->good_share_counter, true);
  521. return root;
  522. }
  523. static bool
  524. modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work)
  525. {
  526. char *midstate = state->next_work_cmd + 2;
  527. char *taildata = midstate + 32;
  528. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  529. return false;
  530. memcpy(midstate, work->midstate, 32);
  531. memcpy(taildata, work->data + 64, 12);
  532. return true;
  533. }
  534. static bool
  535. modminer_start_work(struct thr_info*thr)
  536. {
  537. fd_set fds;
  538. struct cgpu_info*modminer = thr->cgpu;
  539. struct modminer_fpga_state *state = thr->cgpu_data;
  540. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  541. int fd;
  542. char buf[1];
  543. mutex_lock(mutexp);
  544. fd = modminer->device->device_fd;
  545. if (unlikely(fd == -1)) {
  546. if (!modminer_reopen(modminer)) {
  547. mutex_unlock(mutexp);
  548. return false;
  549. }
  550. fd = modminer->device->device_fd;
  551. }
  552. if (46 != write(fd, state->next_work_cmd, 46))
  553. bailout2(LOG_ERR, "%s: Error writing (start work)", modminer->proc_repr);
  554. gettimeofday(&state->tv_workstart, NULL);
  555. state->hashes = 0;
  556. status_read("start work");
  557. mutex_unlock(mutexp);
  558. if (opt_debug) {
  559. char *xdata = bin2hex(state->running_work.data, 80);
  560. applog(LOG_DEBUG, "%s: Started work: %s",
  561. modminer->proc_repr, xdata);
  562. free(xdata);
  563. }
  564. return true;
  565. }
  566. #define work_restart(thr) thr->work_restart
  567. #define NONCE_CHARS(nonce) \
  568. (int)((unsigned char*)&nonce)[3], \
  569. (int)((unsigned char*)&nonce)[2], \
  570. (int)((unsigned char*)&nonce)[1], \
  571. (int)((unsigned char*)&nonce)[0]
  572. static int64_t
  573. modminer_process_results(struct thr_info*thr)
  574. {
  575. struct cgpu_info*modminer = thr->cgpu;
  576. struct modminer_fpga_state *state = thr->cgpu_data;
  577. char fpgaid = modminer->proc_id;
  578. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  579. struct work *work = &state->running_work;
  580. uint32_t nonce;
  581. long iter;
  582. int immediate_bad_nonces = 0, immediate_nonces = 0;
  583. bool bad;
  584. mutex_lock(mutexp);
  585. modminer_get_temperature(modminer, thr);
  586. iter = 200;
  587. while (1) {
  588. if (!_modminer_get_nonce(modminer, fpgaid, &nonce))
  589. safebailout();
  590. mutex_unlock(mutexp);
  591. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  592. nonce = le32toh(nonce);
  593. bad = !test_nonce(work, nonce, false);
  594. ++immediate_nonces;
  595. if (!bad)
  596. applog(LOG_DEBUG, "%s: Nonce for current work: %02x%02x%02x%02x",
  597. modminer->proc_repr,
  598. NONCE_CHARS(nonce));
  599. else
  600. if (test_nonce(&state->last_work, nonce, false))
  601. {
  602. applog(LOG_DEBUG, "%s: Nonce for previous work: %02x%02x%02x%02x",
  603. modminer->proc_repr,
  604. NONCE_CHARS(nonce));
  605. work = &state->last_work;
  606. bad = false;
  607. }
  608. if (!bad)
  609. {
  610. ++state->good_share_counter;
  611. submit_nonce(thr, work, nonce);
  612. }
  613. else {
  614. inc_hw_errors(thr, work, nonce);
  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. thr->cgpu_data = NULL;
  685. }
  686. static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf)
  687. {
  688. int val;
  689. if (strcasecmp(option, "help") == 0) {
  690. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  691. MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  692. return replybuf;
  693. }
  694. if (strcasecmp(option, "clock") == 0) {
  695. int multiplier;
  696. if (!setting || !*setting) {
  697. sprintf(replybuf, "missing clock setting");
  698. return replybuf;
  699. }
  700. val = atoi(setting);
  701. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
  702. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  703. setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  704. return replybuf;
  705. }
  706. multiplier = val / 2;
  707. struct thr_info *thr = modminer->thr[0];
  708. struct modminer_fpga_state *state = thr->cgpu_data;
  709. uint8_t oldFreqM = state->dclk.freqM;
  710. signed char delta = (multiplier - oldFreqM) * 2;
  711. state->dclk.freqMDefault = multiplier;
  712. if (unlikely(!modminer_change_clock(thr, true, delta))) {
  713. sprintf(replybuf, "Set clock failed: %s",
  714. modminer->proc_repr);
  715. return replybuf;
  716. }
  717. dclk_msg_freqchange(modminer->proc_repr, oldFreqM * 2, state->dclk.freqM * 2, " on user request");
  718. return NULL;
  719. }
  720. sprintf(replybuf, "Unknown option: %s", option);
  721. return replybuf;
  722. }
  723. struct device_drv modminer_drv = {
  724. .dname = "modminer",
  725. .name = "MMQ",
  726. .drv_detect = modminer_detect,
  727. .get_dev_statline_before = get_modminer_dev_statline_before,
  728. .get_statline_before = get_modminer_statline_before,
  729. .get_stats = modminer_get_stats,
  730. .get_api_extra_device_status = get_modminer_drv_extra_device_status,
  731. .set_device = modminer_set_device,
  732. .thread_prepare = modminer_fpga_prepare,
  733. .thread_init = modminer_fpga_init,
  734. .scanhash = modminer_scanhash,
  735. .thread_shutdown = modminer_fpga_shutdown,
  736. };