driver-modminer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include "fpgautils.h"
  14. #include "logging.h"
  15. #include "miner.h"
  16. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  17. #define BISTREAM_USER_ID "\2\4$B"
  18. struct device_api modminer_api;
  19. struct modminer_fpga_state {
  20. bool work_running;
  21. struct work running_work;
  22. struct timeval tv_workstart;
  23. uint32_t hashes;
  24. char next_work_cmd[46];
  25. unsigned char clock;
  26. int no_nonce_counter;
  27. int good_share_counter;
  28. time_t last_cutoff_reduced;
  29. unsigned char temp;
  30. unsigned char pdone;
  31. };
  32. static inline bool
  33. _bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...)
  34. {
  35. if (fd != -1)
  36. serial_close(fd);
  37. if (modminer) {
  38. modminer->device_fd = -1;
  39. mutex_unlock(&modminer->device_mutex);
  40. }
  41. va_list ap;
  42. va_start(ap, fmt);
  43. vapplog(prio, fmt, ap);
  44. va_end(ap);
  45. return false;
  46. }
  47. #define bailout(...) return _bailout(fd, NULL, __VA_ARGS__);
  48. static bool
  49. modminer_detect_one(const char *devpath)
  50. {
  51. int fd = serial_open(devpath, 0, 10, true);
  52. if (unlikely(fd == -1))
  53. bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath);
  54. char buf[0x100];
  55. size_t len;
  56. // Sending 45 noops, just in case the device was left in "start job" reading
  57. (void)(write(fd, "\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", 45) ?:0);
  58. while (serial_read(fd, buf, sizeof(buf)) > 0)
  59. ;
  60. if (1 != write(fd, "\x01", 1)) // Get version
  61. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath);
  62. len = serial_read(fd, buf, sizeof(buf)-1);
  63. if (len < 1)
  64. bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath);
  65. buf[len] = '\0';
  66. char*devname = strdup(buf);
  67. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  68. if (1 != write(fd, "\x02", 1)) // Get FPGA count
  69. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath);
  70. len = read(fd, buf, 1);
  71. if (len < 1)
  72. bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath);
  73. if (!buf[0])
  74. bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath);
  75. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  76. serial_close(fd);
  77. struct cgpu_info *modminer;
  78. modminer = calloc(1, sizeof(*modminer));
  79. modminer->api = &modminer_api;
  80. mutex_init(&modminer->device_mutex);
  81. modminer->device_path = strdup(devpath);
  82. modminer->device_fd = -1;
  83. modminer->deven = DEV_ENABLED;
  84. modminer->threads = buf[0];
  85. modminer->name = devname;
  86. modminer->cutofftemp = 85;
  87. return add_cgpu(modminer);
  88. }
  89. #undef bailout
  90. static char
  91. modminer_detect_auto()
  92. {
  93. return
  94. serial_autodetect_udev (modminer_detect_one, "BTCFPGA*ModMiner") ?:
  95. serial_autodetect_devserial(modminer_detect_one, "BTCFPGA_ModMiner") ?:
  96. 0;
  97. }
  98. static void
  99. modminer_detect()
  100. {
  101. serial_detect_auto(modminer_api.dname, modminer_detect_one, modminer_detect_auto);
  102. }
  103. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  104. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  105. #define check_magic(L) do { \
  106. if (1 != fread(buf, 1, 1, f)) \
  107. bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \
  108. if (buf[0] != L) \
  109. bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \
  110. } while(0)
  111. #define read_str(eng) do { \
  112. if (1 != fread(buf, 2, 1, f)) \
  113. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \
  114. len = (ubuf[0] << 8) | ubuf[1]; \
  115. if (len >= sizeof(buf)) \
  116. bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \
  117. if (1 != fread(buf, len, 1, f)) \
  118. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \
  119. buf[len] = '\0'; \
  120. } while(0)
  121. #define status_read(eng) do { \
  122. FD_SET(fd, &fds); \
  123. select(fd+1, &fds, NULL, NULL, NULL); \
  124. if (1 != read(fd, buf, 1)) \
  125. bailout2(LOG_ERR, "%s %u: Error programming %s (" eng ")", modminer->api->name, modminer->device_id, modminer->device_path); \
  126. if (buf[0] != 1) \
  127. bailout2(LOG_ERR, "%s %u: Wrong " eng " programming %s", modminer->api->name, modminer->device_id, modminer->device_path); \
  128. } while(0)
  129. static bool
  130. modminer_fpga_upload_bitstream(struct cgpu_info*modminer)
  131. {
  132. struct modminer_fpga_state *state = modminer->thr[0]->cgpu_data;
  133. fd_set fds;
  134. char buf[0x100];
  135. unsigned char *ubuf = (unsigned char*)buf;
  136. unsigned long len, flen;
  137. char *p;
  138. const char *fwfile = BITSTREAM_FILENAME;
  139. char fpgaid = 4; // "all FPGAs"
  140. FILE *f = open_bitstream("modminer", fwfile);
  141. if (!f)
  142. bailout(LOG_ERR, "Error opening ModMiner firmware file %s", fwfile);
  143. if (1 != fread(buf, 2, 1, f))
  144. bailout(LOG_ERR, "Error reading ModMiner firmware (magic)");
  145. if (buf[0] || buf[1] != 9)
  146. bailout(LOG_ERR, "ModMiner firmware has wrong magic (9)");
  147. if (-1 == fseek(f, 11, SEEK_CUR))
  148. bailout(LOG_ERR, "ModMiner firmware seek failed");
  149. check_magic('a');
  150. read_str("design name");
  151. applog(LOG_DEBUG, "ModMiner firmware file %s info:", fwfile);
  152. applog(LOG_DEBUG, " Design name: %s", buf);
  153. p = strrchr(buf, ';') ?: buf;
  154. p = strrchr(buf, '=') ?: p;
  155. if (p[0] == '=')
  156. ++p;
  157. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  158. if (p[0] != '\0')
  159. bailout(LOG_ERR, "Bad usercode in ModMiner firmware file");
  160. if (fwusercode == 0xffffffff)
  161. bailout(LOG_ERR, "ModMiner firmware doesn't support user code");
  162. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  163. check_magic('b');
  164. read_str("part number");
  165. applog(LOG_DEBUG, " Part number: %s", buf);
  166. check_magic('c');
  167. read_str("build date");
  168. applog(LOG_DEBUG, " Build date: %s", buf);
  169. check_magic('d');
  170. read_str("build time");
  171. applog(LOG_DEBUG, " Build time: %s", buf);
  172. check_magic('e');
  173. if (1 != fread(buf, 4, 1, f))
  174. bailout(LOG_ERR, "Error reading ModMiner firmware (data len)");
  175. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  176. flen = len;
  177. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  178. int fd = modminer->device_fd;
  179. applog(LOG_WARNING, "%s %u: Programming %s... DO NOT EXIT CGMINER UNTIL COMPLETE", modminer->api->name, modminer->device_id, modminer->device_path);
  180. buf[0] = '\x05'; // Program Bitstream
  181. buf[1] = fpgaid;
  182. buf[2] = (len >> 0) & 0xff;
  183. buf[3] = (len >> 8) & 0xff;
  184. buf[4] = (len >> 16) & 0xff;
  185. buf[5] = (len >> 24) & 0xff;
  186. if (6 != write(fd, buf, 6))
  187. bailout2(LOG_ERR, "%s %u: Error programming %s (cmd)", modminer->api->name, modminer->device_id, modminer->device_path);
  188. status_read("cmd reply");
  189. ssize_t buflen;
  190. char nextstatus = 10;
  191. while (len) {
  192. buflen = len < 32 ? len : 32;
  193. if (fread(buf, buflen, 1, f) != 1)
  194. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", modminer->api->name, modminer->device_id, modminer->device_path, len);
  195. if (write(fd, buf, buflen) != buflen)
  196. bailout2(LOG_ERR, "%s %u: Error programming %s (data)", modminer->api->name, modminer->device_id, modminer->device_path);
  197. state->pdone = 100 - ((len * 100) / flen);
  198. if (state->pdone >= nextstatus)
  199. {
  200. nextstatus += 10;
  201. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", modminer->api->name, modminer->device_id, modminer->device_path, state->pdone);
  202. }
  203. status_read("status");
  204. len -= buflen;
  205. }
  206. status_read("final status");
  207. applog(LOG_WARNING, "%s %u: Done programming %s", modminer->api->name, modminer->device_id, modminer->device_path);
  208. return true;
  209. }
  210. static bool
  211. modminer_device_prepare(struct cgpu_info *modminer)
  212. {
  213. int fd = serial_open(modminer->device_path, 0, /*FIXME=-1*/3000, true);
  214. if (unlikely(-1 == fd))
  215. bailout(LOG_ERR, "%s %u: Failed to open %s", modminer->api->name, modminer->device_id, modminer->device_path);
  216. modminer->device_fd = fd;
  217. applog(LOG_INFO, "%s %u: Opened %s", modminer->api->name, modminer->device_id, modminer->device_path);
  218. struct timeval now;
  219. gettimeofday(&now, NULL);
  220. get_datestamp(modminer->init, &now);
  221. return true;
  222. }
  223. #undef bailout
  224. static bool
  225. modminer_fpga_prepare(struct thr_info *thr)
  226. {
  227. struct cgpu_info *modminer = thr->cgpu;
  228. // Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
  229. if (modminer->device_fd == -1 && !modminer_device_prepare(modminer))
  230. return false;
  231. struct modminer_fpga_state *state;
  232. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  233. state->next_work_cmd[0] = '\x08'; // Send Job
  234. state->next_work_cmd[1] = thr->device_thread; // FPGA id
  235. return true;
  236. }
  237. static bool
  238. modminer_reduce_clock(struct thr_info*thr, bool needlock)
  239. {
  240. struct cgpu_info*modminer = thr->cgpu;
  241. struct modminer_fpga_state *state = thr->cgpu_data;
  242. char fpgaid = thr->device_thread;
  243. int fd = modminer->device_fd;
  244. unsigned char cmd[6], buf[1];
  245. if (state->clock <= 100)
  246. return false;
  247. cmd[0] = '\x06'; // set clock speed
  248. cmd[1] = fpgaid;
  249. cmd[2] = state->clock -= 2;
  250. cmd[3] = cmd[4] = cmd[5] = '\0';
  251. if (needlock)
  252. mutex_lock(&modminer->device_mutex);
  253. if (6 != write(fd, cmd, 6))
  254. bailout2(LOG_ERR, "%s %u.%u: Error writing (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  255. if (serial_read(fd, &buf, 1) != 1)
  256. bailout2(LOG_ERR, "%s %u.%u: Error reading (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  257. if (needlock)
  258. mutex_unlock(&modminer->device_mutex);
  259. applog(LOG_WARNING, "%s %u.%u: Setting clock speed to %u", modminer->api->name, modminer->device_id, fpgaid, state->clock);
  260. return true;
  261. }
  262. static bool
  263. modminer_fpga_init(struct thr_info *thr)
  264. {
  265. struct cgpu_info *modminer = thr->cgpu;
  266. struct modminer_fpga_state *state = thr->cgpu_data;
  267. int fd;
  268. char fpgaid = thr->device_thread;
  269. unsigned char cmd[2], buf[4];
  270. mutex_lock(&modminer->device_mutex);
  271. fd = modminer->device_fd;
  272. if (fd == -1) {
  273. // Died in another thread...
  274. mutex_unlock(&modminer->device_mutex);
  275. return false;
  276. }
  277. cmd[0] = '\x04'; // Read USER code (bitstream id)
  278. cmd[1] = fpgaid;
  279. if (write(fd, cmd, 2) != 2)
  280. bailout2(LOG_ERR, "%s %u.%u: Error writing (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  281. if (serial_read(fd, buf, 4) != 4)
  282. bailout2(LOG_ERR, "%s %u.%u: Error reading (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  283. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  284. applog(LOG_ERR, "%s %u.%u: FPGA not programmed", modminer->api->name, modminer->device_id, fpgaid);
  285. if (!modminer_fpga_upload_bitstream(modminer))
  286. return false;
  287. }
  288. else
  289. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)", modminer->api->name, modminer->device_id, fpgaid);
  290. state->pdone = 101;
  291. state->clock = 212; // Will be reduced to 210 by modminer_reduce_clock
  292. modminer_reduce_clock(thr, false);
  293. mutex_unlock(&modminer->device_mutex);
  294. thr->primary_thread = true;
  295. return true;
  296. }
  297. static void
  298. get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  299. {
  300. char info[18] = " | ";
  301. int tc = modminer->threads;
  302. bool havetemp = false;
  303. int i;
  304. char pdone = ((struct modminer_fpga_state*)(modminer->thr[0]->cgpu_data))->pdone;
  305. if (pdone != 101) {
  306. sprintf(&info[1], "%3d%%", pdone);
  307. info[5] = ' ';
  308. strcat(buf, info);
  309. return;
  310. }
  311. if (tc > 4)
  312. tc = 4;
  313. for (i = tc - 1; i >= 0; --i) {
  314. struct thr_info*thr = modminer->thr[i];
  315. struct modminer_fpga_state *state = thr->cgpu_data;
  316. unsigned char temp = state->temp;
  317. info[i*3+2] = '/';
  318. if (temp) {
  319. havetemp = true;
  320. if (temp > 9)
  321. info[i*3+0] = 0x30 + (temp / 10);
  322. info[i*3+1] = 0x30 + (temp % 10);
  323. }
  324. }
  325. if (havetemp) {
  326. info[tc*3-1] = ' ';
  327. info[tc*3] = 'C';
  328. strcat(buf, info);
  329. }
  330. else
  331. strcat(buf, " | ");
  332. }
  333. static bool
  334. modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work)
  335. {
  336. char *midstate = state->next_work_cmd + 2;
  337. char *taildata = midstate + 32;
  338. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  339. return false;
  340. memcpy(midstate, work->midstate, 32);
  341. memcpy(taildata, work->data + 64, 12);
  342. return true;
  343. }
  344. static bool
  345. modminer_start_work(struct thr_info*thr)
  346. {
  347. fd_set fds;
  348. struct cgpu_info*modminer = thr->cgpu;
  349. struct modminer_fpga_state *state = thr->cgpu_data;
  350. char fpgaid = thr->device_thread;
  351. int fd = modminer->device_fd;
  352. char buf[1];
  353. mutex_lock(&modminer->device_mutex);
  354. if (46 != write(fd, state->next_work_cmd, 46))
  355. bailout2(LOG_ERR, "%s %u.%u: Error writing (start work)", modminer->api->name, modminer->device_id, fpgaid);
  356. gettimeofday(&state->tv_workstart, NULL);
  357. state->hashes = 0;
  358. status_read("start work");
  359. mutex_unlock(&modminer->device_mutex);
  360. return true;
  361. }
  362. #define work_restart(thr) thr->work_restart
  363. static uint64_t
  364. modminer_process_results(struct thr_info*thr)
  365. {
  366. struct cgpu_info*modminer = thr->cgpu;
  367. struct modminer_fpga_state *state = thr->cgpu_data;
  368. char fpgaid = thr->device_thread;
  369. int fd = modminer->device_fd;
  370. struct work *work = &state->running_work;
  371. char cmd[2], temperature;
  372. uint32_t nonce;
  373. long iter;
  374. bool bad;
  375. cmd[0] = '\x0a';
  376. cmd[1] = fpgaid;
  377. mutex_lock(&modminer->device_mutex);
  378. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1)
  379. {
  380. state->temp = temperature;
  381. if (!fpgaid)
  382. modminer->temp = (float)temperature;
  383. if (temperature > modminer->cutofftemp - 2) {
  384. if (temperature > modminer->cutofftemp) {
  385. applog(LOG_WARNING, "%s %u.%u: Hit thermal cutoff limit, disabling device!", modminer->api->name, modminer->device_id, fpgaid);
  386. modminer->deven = DEV_RECOVER;
  387. modminer->device_last_not_well = time(NULL);
  388. modminer->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  389. ++modminer->dev_thermal_cutoff_count;
  390. } else {
  391. time_t now = time(NULL);
  392. if (state->last_cutoff_reduced != now) {
  393. state->last_cutoff_reduced = now;
  394. modminer_reduce_clock(thr, false);
  395. }
  396. }
  397. }
  398. }
  399. cmd[0] = '\x09';
  400. iter = 200;
  401. while (1) {
  402. if (write(fd, cmd, 2) != 2)
  403. bailout2(LOG_ERR, "%s %u.%u: Error reading (get nonce)", modminer->api->name, modminer->device_id, fpgaid);
  404. serial_read(fd, &nonce, 4);
  405. mutex_unlock(&modminer->device_mutex);
  406. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  407. state->no_nonce_counter = 0;
  408. bad = !test_nonce(work, nonce, false);
  409. if (!bad)
  410. submit_nonce(thr, work, nonce);
  411. else {
  412. ++hw_errors;
  413. if (++modminer->hw_errors * 100 > 1000 + state->good_share_counter)
  414. // Only reduce clocks if hardware errors are more than ~1% of results
  415. modminer_reduce_clock(thr, true);
  416. }
  417. }
  418. else
  419. if (++state->no_nonce_counter > 18000) {
  420. state->no_nonce_counter = 0;
  421. modminer_reduce_clock(thr, true);
  422. }
  423. if (work_restart(thr))
  424. break;
  425. usleep(10000);
  426. if (work_restart(thr) || !--iter)
  427. break;
  428. mutex_lock(&modminer->device_mutex);
  429. }
  430. struct timeval tv_workend, elapsed;
  431. gettimeofday(&tv_workend, NULL);
  432. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  433. uint64_t hashes = (uint64_t)state->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  434. if (hashes > 0xffffffff)
  435. hashes = 0xffffffff;
  436. else
  437. if (hashes <= state->hashes)
  438. hashes = 1;
  439. else
  440. hashes -= state->hashes;
  441. state->hashes += hashes;
  442. return hashes;
  443. }
  444. static int64_t
  445. modminer_scanhash(struct thr_info*thr, struct work*work, int64_t __maybe_unused max_nonce)
  446. {
  447. struct modminer_fpga_state *state = thr->cgpu_data;
  448. int64_t hashes = 0;
  449. bool startwork;
  450. startwork = modminer_prepare_next_work(state, work);
  451. if (state->work_running) {
  452. hashes = modminer_process_results(thr);
  453. if (work_restart(thr)) {
  454. state->work_running = false;
  455. return 0;
  456. }
  457. } else
  458. state->work_running = true;
  459. if (startwork) {
  460. if (!modminer_start_work(thr))
  461. return -1;
  462. memcpy(&state->running_work, work, sizeof(state->running_work));
  463. }
  464. // This is intentionally early
  465. work->blk.nonce += hashes;
  466. return hashes;
  467. }
  468. static void
  469. modminer_fpga_shutdown(struct thr_info *thr)
  470. {
  471. free(thr->cgpu_data);
  472. }
  473. struct device_api modminer_api = {
  474. .dname = "modminer",
  475. .name = "MMQ",
  476. .api_detect = modminer_detect,
  477. .get_statline_before = get_modminer_statline_before,
  478. .thread_prepare = modminer_fpga_prepare,
  479. .thread_init = modminer_fpga_init,
  480. .scanhash = modminer_scanhash,
  481. .thread_shutdown = modminer_fpga_shutdown,
  482. };