driver-modminer.c 24 KB

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