driver-modminer.c 16 KB

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