driver-modminer.c 24 KB

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