driver-modminer.c 24 KB

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