driver-modminer.c 24 KB

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