driver-modminer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Copyright 2012 Andrew Smith
  3. * Copyright 2012 Luke Dashjr
  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 <stdarg.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <math.h>
  15. #include "logging.h"
  16. #include "miner.h"
  17. #include "usbutils.h"
  18. #include "fpgautils.h"
  19. #include "util.h"
  20. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  21. #define BISTREAM_USER_ID "\2\4$B"
  22. #define BITSTREAM_MAGIC_0 0
  23. #define BITSTREAM_MAGIC_1 9
  24. #define MODMINER_CUTOFF_TEMP 60.0
  25. #define MODMINER_OVERHEAT_TEMP 50.0
  26. #define MODMINER_RECOVER_TEMP 46.5
  27. #define MODMINER_TEMP_UP_LIMIT 47.0
  28. #define MODMINER_HW_ERROR_PERCENT 0.75
  29. // How many seconds of no nonces means there's something wrong
  30. // First time - drop the clock and see if it revives
  31. // Second time - (and it didn't revive) disable it
  32. #define ITS_DEAD_JIM 300
  33. // N.B. in the latest firmware the limit is 250
  34. // however the voltage/temperature risks preclude that
  35. #define MODMINER_MAX_CLOCK 230
  36. #define MODMINER_DEF_CLOCK 200
  37. #define MODMINER_MIN_CLOCK 160
  38. #define MODMINER_CLOCK_DOWN -2
  39. #define MODMINER_CLOCK_SET 0
  40. #define MODMINER_CLOCK_UP 2
  41. #define MODMINER_CLOCK_DEAD -6
  42. #define MODMINER_CLOCK_CUTOFF -10
  43. // Commands
  44. #define MODMINER_PING "\x00"
  45. #define MODMINER_GET_VERSION "\x01"
  46. #define MODMINER_FPGA_COUNT "\x02"
  47. // Commands + require FPGAid
  48. #define MODMINER_GET_IDCODE '\x03'
  49. #define MODMINER_GET_USERCODE '\x04'
  50. #define MODMINER_PROGRAM '\x05'
  51. #define MODMINER_SET_CLOCK '\x06'
  52. #define MODMINER_READ_CLOCK '\x07'
  53. #define MODMINER_SEND_WORK '\x08'
  54. #define MODMINER_CHECK_WORK '\x09'
  55. // One byte temperature reply
  56. #define MODMINER_TEMP1 '\x0a'
  57. // Two byte temperature reply
  58. #define MODMINER_TEMP2 '\x0d'
  59. // +6 bytes
  60. #define MODMINER_SET_REG '\x0b'
  61. // +2 bytes
  62. #define MODMINER_GET_REG '\x0c'
  63. #define FPGAID_ALL 4
  64. // Maximum how many good shares in a row means clock up
  65. // 96 is ~34m22s at 200MH/s
  66. #define MODMINER_TRY_UP 96
  67. // Initially how many good shares in a row means clock up
  68. // This is doubled each down clock until it reaches MODMINER_TRY_UP
  69. // 6 is ~2m9s at 200MH/s
  70. #define MODMINER_EARLY_UP 6
  71. // Limit when reducing shares_to_good
  72. #define MODMINER_MIN_BACK 12
  73. struct device_api modminer_api;
  74. // 45 noops sent when detecting, in case the device was left in "start job" reading
  75. 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";
  76. static void do_ping(struct cgpu_info *modminer)
  77. {
  78. char buf[0x100+1];
  79. int err, amount;
  80. // Don't care if it fails
  81. err = usb_write(modminer, (char *)NOOP, sizeof(NOOP)-1, &amount, C_PING);
  82. applog(LOG_DEBUG, "%s%u: flush noop got %d err %d",
  83. modminer->api->name, modminer->fpgaid, amount, err);
  84. // Clear any outstanding data
  85. while ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_CLEAR)) == 0 && amount > 0)
  86. applog(LOG_DEBUG, "%s%u: clear got %d",
  87. modminer->api->name, modminer->fpgaid, amount);
  88. applog(LOG_DEBUG, "%s%u: final clear got %d err %d",
  89. modminer->api->name, modminer->fpgaid, amount, err);
  90. }
  91. static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  92. {
  93. char buf[0x100+1];
  94. char *devname = NULL;
  95. char devpath[20];
  96. int err, i, amount;
  97. bool added = false;
  98. struct cgpu_info *modminer = NULL;
  99. modminer = calloc(1, sizeof(*modminer));
  100. modminer->api = &modminer_api;
  101. modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex)));
  102. mutex_init(modminer->modminer_mutex);
  103. modminer->fpgaid = (char)0;
  104. if (!usb_init(modminer, dev, found))
  105. goto shin;
  106. do_ping(modminer);
  107. if ((err = usb_write(modminer, MODMINER_GET_VERSION, 1, &amount, C_REQUESTVERSION)) < 0 || amount != 1) {
  108. applog(LOG_ERR, "ModMiner detect: send version request failed (%d:%d)", amount, err);
  109. goto unshin;
  110. }
  111. if ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) {
  112. if (err < 0)
  113. applog(LOG_ERR, "ModMiner detect: no version reply (%d)", err);
  114. else
  115. applog(LOG_ERR, "ModMiner detect: empty version reply (%d)", amount);
  116. applog(LOG_DEBUG, "ModMiner detect: check the firmware");
  117. goto unshin;
  118. }
  119. buf[amount] = '\0';
  120. devname = strdup(buf);
  121. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  122. if ((err = usb_write(modminer, MODMINER_FPGA_COUNT, 1, &amount, C_REQUESTFPGACOUNT) < 0 || amount != 1)) {
  123. applog(LOG_ERR, "ModMiner detect: FPGA count request failed (%d:%d)", amount, err);
  124. goto unshin;
  125. }
  126. if ((err = usb_read(modminer, buf, 1, &amount, C_GETFPGACOUNT)) < 0 || amount != 1) {
  127. applog(LOG_ERR, "ModMiner detect: no FPGA count reply (%d:%d)", amount, err);
  128. goto unshin;
  129. }
  130. // TODO: flag it use 1 byte temp if it is an old firmware
  131. // can detect with modminer->cgusb->serial ?
  132. if (buf[0] == 0) {
  133. applog(LOG_ERR, "ModMiner detect: zero FPGA count from %s", devname);
  134. goto unshin;
  135. }
  136. if (buf[0] < 1 || buf[0] > 4) {
  137. applog(LOG_ERR, "ModMiner detect: invalid FPGA count (%u) from %s", buf[0], devname);
  138. goto unshin;
  139. }
  140. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  141. modminer->name = devname;
  142. // TODO: test with 1 board missing in the middle and each end
  143. // to see how that affects the sequence numbers
  144. for (i = 0; i < buf[0]; i++) {
  145. struct cgpu_info *tmp = calloc(1, sizeof(*tmp));
  146. tmp->api = modminer->api;
  147. tmp->name = devname;
  148. sprintf(devpath, "%d:%d:%d",
  149. (int)(modminer->usbdev->bus_number),
  150. (int)(modminer->usbdev->device_address),
  151. i);
  152. tmp->device_path = strdup(devpath);
  153. tmp->usbdev = modminer->usbdev;
  154. // Only the first copy gets the already used stats
  155. if (!added)
  156. tmp->usbstat = modminer->usbstat;
  157. tmp->fpgaid = (char)i;
  158. tmp->modminer_mutex = modminer->modminer_mutex;
  159. tmp->deven = DEV_ENABLED;
  160. tmp->threads = 1;
  161. if (!add_cgpu(tmp)) {
  162. free(tmp->device_path);
  163. free(tmp);
  164. goto unshin;
  165. }
  166. update_usb_stats(tmp);
  167. added = true;
  168. }
  169. free(modminer);
  170. return true;
  171. unshin:
  172. if (!added)
  173. usb_uninit(modminer);
  174. shin:
  175. if (!added)
  176. free(modminer->modminer_mutex);
  177. free(modminer);
  178. if (added)
  179. return true;
  180. else
  181. return false;
  182. }
  183. static void modminer_detect()
  184. {
  185. usb_detect(&modminer_api, modminer_detect_one);
  186. }
  187. static bool get_expect(struct cgpu_info *modminer, FILE *f, char c)
  188. {
  189. char buf;
  190. if (fread(&buf, 1, 1, f) != 1) {
  191. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream (%c)",
  192. modminer->api->name, modminer->device_id, errno, c);
  193. return false;
  194. }
  195. if (buf != c) {
  196. applog(LOG_ERR, "%s%u: firmware code mismatch (%c)",
  197. modminer->api->name, modminer->device_id, c);
  198. return false;
  199. }
  200. return true;
  201. }
  202. static bool get_info(struct cgpu_info *modminer, FILE *f, char *buf, int bufsiz, const char *name)
  203. {
  204. unsigned char siz[2];
  205. int len;
  206. if (fread(siz, 2, 1, f) != 1) {
  207. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s' len",
  208. modminer->api->name, modminer->device_id, errno, name);
  209. return false;
  210. }
  211. len = siz[0] * 256 + siz[1];
  212. if (len >= bufsiz) {
  213. applog(LOG_ERR, "%s%u: Bitstream '%s' len too large (%d)",
  214. modminer->api->name, modminer->device_id, name, len);
  215. return false;
  216. }
  217. if (fread(buf, len, 1, f) != 1) {
  218. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s'", errno,
  219. modminer->api->name, modminer->device_id, errno, name);
  220. return false;
  221. }
  222. buf[len] = '\0';
  223. return true;
  224. }
  225. #define USE_DEFAULT_TIMEOUT 0
  226. // mutex must always be locked before calling
  227. static bool get_status_timeout(struct cgpu_info *modminer, char *msg, unsigned int timeout, enum usb_cmds cmd)
  228. {
  229. int err, amount;
  230. char buf[1];
  231. if (timeout == USE_DEFAULT_TIMEOUT)
  232. err = usb_read(modminer, buf, 1, &amount, cmd);
  233. else
  234. err = usb_read_timeout(modminer, buf, 1, &amount, timeout, cmd);
  235. if (err < 0 || amount != 1) {
  236. mutex_unlock(modminer->modminer_mutex);
  237. applog(LOG_ERR, "%s%u: Error (%d:%d) getting %s reply",
  238. modminer->api->name, modminer->device_id, amount, err, msg);
  239. return false;
  240. }
  241. if (buf[0] != 1) {
  242. mutex_unlock(modminer->modminer_mutex);
  243. applog(LOG_ERR, "%s%u: Error, invalid %s reply (was %d should be 1)",
  244. modminer->api->name, modminer->device_id, msg, buf[0]);
  245. return false;
  246. }
  247. return true;
  248. }
  249. // mutex must always be locked before calling
  250. static bool get_status(struct cgpu_info *modminer, char *msg, enum usb_cmds cmd)
  251. {
  252. return get_status_timeout(modminer, msg, USE_DEFAULT_TIMEOUT, cmd);
  253. }
  254. static bool modminer_fpga_upload_bitstream(struct cgpu_info *modminer)
  255. {
  256. const char *bsfile = BITSTREAM_FILENAME;
  257. char buf[0x100], *p;
  258. char devmsg[64];
  259. unsigned char *ubuf = (unsigned char *)buf;
  260. unsigned long totlen, len;
  261. size_t buflen, remaining;
  262. float nextmsg, upto;
  263. char fpgaid = FPGAID_ALL;
  264. int err, amount, tries;
  265. char *ptr;
  266. FILE *f = open_bitstream("modminer", bsfile);
  267. if (!f) {
  268. mutex_unlock(modminer->modminer_mutex);
  269. applog(LOG_DEBUG, "%s%u: Error (%d) opening bitstream file %s",
  270. modminer->api->name, modminer->device_id, errno, bsfile);
  271. return false;
  272. }
  273. if (fread(buf, 2, 1, f) != 1) {
  274. mutex_unlock(modminer->modminer_mutex);
  275. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream magic",
  276. modminer->api->name, modminer->device_id, errno);
  277. goto dame;
  278. }
  279. if (buf[0] != BITSTREAM_MAGIC_0 || buf[1] != BITSTREAM_MAGIC_1) {
  280. mutex_unlock(modminer->modminer_mutex);
  281. applog(LOG_ERR, "%s%u: bitstream has incorrect magic (%u,%u) instead of (%u,%u)",
  282. modminer->api->name, modminer->device_id,
  283. buf[0], buf[1],
  284. BITSTREAM_MAGIC_0, BITSTREAM_MAGIC_1);
  285. goto dame;
  286. }
  287. if (fseek(f, 11L, SEEK_CUR)) {
  288. mutex_unlock(modminer->modminer_mutex);
  289. applog(LOG_ERR, "%s%u: Error (%d) bitstream seek failed",
  290. modminer->api->name, modminer->device_id, errno);
  291. goto dame;
  292. }
  293. if (!get_expect(modminer, f, 'a'))
  294. goto undame;
  295. if (!get_info(modminer, f, buf, sizeof(buf), "Design name"))
  296. goto undame;
  297. applog(LOG_DEBUG, "%s%u: bitstream file '%s' info:",
  298. modminer->api->name, modminer->device_id, bsfile);
  299. applog(LOG_DEBUG, " Design name: '%s'", buf);
  300. p = strrchr(buf, ';') ? : buf;
  301. p = strrchr(buf, '=') ? : p;
  302. if (p[0] == '=')
  303. p++;
  304. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  305. if (p[0] != '\0') {
  306. mutex_unlock(modminer->modminer_mutex);
  307. applog(LOG_ERR, "%s%u: Bad usercode in bitstream file",
  308. modminer->api->name, modminer->device_id);
  309. goto dame;
  310. }
  311. if (fwusercode == 0xffffffff) {
  312. mutex_unlock(modminer->modminer_mutex);
  313. applog(LOG_ERR, "%s%u: bitstream doesn't support user code",
  314. modminer->api->name, modminer->device_id);
  315. goto dame;
  316. }
  317. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  318. if (!get_expect(modminer, f, 'b'))
  319. goto undame;
  320. if (!get_info(modminer, f, buf, sizeof(buf), "Part number"))
  321. goto undame;
  322. applog(LOG_DEBUG, " Part number: '%s'", buf);
  323. if (!get_expect(modminer, f, 'c'))
  324. goto undame;
  325. if (!get_info(modminer, f, buf, sizeof(buf), "Build date"))
  326. goto undame;
  327. applog(LOG_DEBUG, " Build date: '%s'", buf);
  328. if (!get_expect(modminer, f, 'd'))
  329. goto undame;
  330. if (!get_info(modminer, f, buf, sizeof(buf), "Build time"))
  331. goto undame;
  332. applog(LOG_DEBUG, " Build time: '%s'", buf);
  333. if (!get_expect(modminer, f, 'e'))
  334. goto undame;
  335. if (fread(buf, 4, 1, f) != 1) {
  336. mutex_unlock(modminer->modminer_mutex);
  337. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream data len",
  338. modminer->api->name, modminer->device_id, errno);
  339. goto dame;
  340. }
  341. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  342. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  343. strcpy(devmsg, modminer->device_path);
  344. ptr = strrchr(devmsg, ':');
  345. if (ptr)
  346. *ptr = '\0';
  347. applog(LOG_WARNING, "%s%u: Programming all FPGA on %s ... Mining will not start until complete",
  348. modminer->api->name, modminer->device_id, devmsg);
  349. buf[0] = MODMINER_PROGRAM;
  350. buf[1] = fpgaid;
  351. buf[2] = (len >> 0) & 0xff;
  352. buf[3] = (len >> 8) & 0xff;
  353. buf[4] = (len >> 16) & 0xff;
  354. buf[5] = (len >> 24) & 0xff;
  355. if ((err = usb_write(modminer, buf, 6, &amount, C_STARTPROGRAM)) < 0 || amount != 6) {
  356. mutex_unlock(modminer->modminer_mutex);
  357. applog(LOG_ERR, "%s%u: Program init failed (%d:%d)",
  358. modminer->api->name, modminer->device_id, amount, err);
  359. goto dame;
  360. }
  361. if (!get_status(modminer, "initialise", C_STARTPROGRAMSTATUS))
  362. goto undame;
  363. // It must be 32 bytes according to MCU legacy.c
  364. #define WRITE_SIZE 32
  365. totlen = len;
  366. nextmsg = 0.1;
  367. while (len > 0) {
  368. buflen = len < WRITE_SIZE ? len : WRITE_SIZE;
  369. if (fread(buf, buflen, 1, f) != 1) {
  370. mutex_unlock(modminer->modminer_mutex);
  371. applog(LOG_ERR, "%s%u: bitstream file read error %d (%d bytes left)",
  372. modminer->api->name, modminer->device_id, errno, len);
  373. goto dame;
  374. }
  375. tries = 0;
  376. ptr = buf;
  377. remaining = buflen;
  378. while ((err = usb_write(modminer, ptr, remaining, &amount, C_PROGRAM)) < 0 || amount != (int)remaining) {
  379. if (err == LIBUSB_ERROR_TIMEOUT && amount > 0 && ++tries < 4) {
  380. remaining -= amount;
  381. ptr += amount;
  382. if (opt_debug)
  383. applog(LOG_DEBUG, "%s%u: Program timeout (%d:%d) sent %d tries %d",
  384. modminer->api->name, modminer->device_id,
  385. amount, err, remaining, tries);
  386. if (!get_status(modminer, "write status", C_PROGRAMSTATUS2))
  387. goto dame;
  388. } else {
  389. mutex_unlock(modminer->modminer_mutex);
  390. applog(LOG_ERR, "%s%u: Program failed (%d:%d) sent %d",
  391. modminer->api->name, modminer->device_id, amount, err, remaining);
  392. goto dame;
  393. }
  394. }
  395. if (!get_status(modminer, "write status", C_PROGRAMSTATUS))
  396. goto dame;
  397. len -= buflen;
  398. upto = (float)(totlen - len) / (float)(totlen);
  399. if (upto >= nextmsg) {
  400. applog(LOG_WARNING,
  401. "%s%u: Programming %.1f%% (%d out of %d)",
  402. modminer->api->name, modminer->device_id, upto*100, (totlen - len), totlen);
  403. nextmsg += 0.1;
  404. }
  405. }
  406. if (!get_status(modminer, "final status", C_FINALPROGRAMSTATUS))
  407. goto undame;
  408. applog(LOG_WARNING, "%s%u: Programming completed for all FPGA on %s",
  409. modminer->api->name, modminer->device_id, devmsg);
  410. // Give it a 2/3s delay after programming
  411. nmsleep(666);
  412. return true;
  413. undame:
  414. ;
  415. mutex_unlock(modminer->modminer_mutex);
  416. ;
  417. dame:
  418. fclose(f);
  419. return false;
  420. }
  421. static bool modminer_fpga_prepare(struct thr_info *thr)
  422. {
  423. struct cgpu_info *modminer = thr->cgpu;
  424. struct timeval now;
  425. gettimeofday(&now, NULL);
  426. get_datestamp(modminer->init, &now);
  427. struct modminer_fpga_state *state;
  428. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  429. state->next_work_cmd[0] = MODMINER_SEND_WORK;
  430. state->next_work_cmd[1] = modminer->fpgaid;
  431. state->shares_to_good = MODMINER_EARLY_UP;
  432. state->overheated = false;
  433. return true;
  434. }
  435. /*
  436. * Clocking rules:
  437. * If device exceeds cutoff or overheat temp - stop sending work until it cools
  438. * decrease the clock by MODMINER_CLOCK_CUTOFF/MODMINER_CLOCK_OVERHEAT
  439. * for when it restarts
  440. *
  441. * When to clock down:
  442. * If device overheats
  443. * also halve shares_to_good
  444. * (so multiple temp drops can recover faster)
  445. * or
  446. * If device gets MODMINER_HW_ERROR_PERCENT errors since last clock up or down
  447. * if clock is <= default it requires 2 HW to do this test
  448. * if clock is > default it only requires 1 HW to do this test
  449. * also double shares_to_good
  450. *
  451. * When to clock up:
  452. * If device gets shares_to_good good shares in a row
  453. * and temp < MODMINER_TEMP_UP_LIMIT
  454. *
  455. * N.B. clock must always be a multiple of 2
  456. */
  457. static bool modminer_delta_clock(struct thr_info *thr, int delta, bool temp)
  458. {
  459. struct cgpu_info *modminer = thr->cgpu;
  460. struct modminer_fpga_state *state = thr->cgpu_data;
  461. unsigned char cmd[6], buf[1];
  462. int err, amount;
  463. // Only do once if multiple shares per work or multiple reasons
  464. // Since the temperature down clock test is first in the code this is OK
  465. if (!state->new_work)
  466. return false;
  467. state->new_work = false;
  468. state->shares = 0;
  469. state->shares_last_hw = 0;
  470. state->hw_errors = 0;
  471. // If drop requested due to temperature, clock drop is always allowed
  472. if (!temp && delta < 0 && modminer->clock <= MODMINER_MIN_CLOCK)
  473. return false;
  474. if (delta > 0 && modminer->clock >= MODMINER_MAX_CLOCK)
  475. return false;
  476. if (delta < 0) {
  477. if (temp) {
  478. if (state->shares_to_good > MODMINER_MIN_BACK)
  479. state->shares_to_good /= 2;
  480. } else {
  481. if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
  482. state->shares_to_good *= 2;
  483. else
  484. state->shares_to_good = MODMINER_TRY_UP;
  485. }
  486. }
  487. modminer->clock += delta;
  488. cmd[0] = MODMINER_SET_CLOCK;
  489. cmd[1] = modminer->fpgaid;
  490. cmd[2] = modminer->clock;
  491. cmd[3] = cmd[4] = cmd[5] = '\0';
  492. mutex_lock(modminer->modminer_mutex);
  493. if ((err = usb_write(modminer, (char *)cmd, 6, &amount, C_SETCLOCK)) < 0 || amount != 6) {
  494. mutex_unlock(modminer->modminer_mutex);
  495. applog(LOG_ERR, "%s%u: Error writing set clock speed (%d:%d)",
  496. modminer->api->name, modminer->device_id, amount, err);
  497. return false;
  498. }
  499. if ((err = usb_read(modminer, (char *)(&buf), 1, &amount, C_REPLYSETCLOCK)) < 0 || amount != 1) {
  500. mutex_unlock(modminer->modminer_mutex);
  501. applog(LOG_ERR, "%s%u: Error reading set clock speed (%d:%d)",
  502. modminer->api->name, modminer->device_id, amount, err);
  503. return false;
  504. }
  505. mutex_unlock(modminer->modminer_mutex);
  506. applog(LOG_WARNING, "%s%u: Set clock speed %sto %u",
  507. modminer->api->name, modminer->device_id,
  508. (delta < 0) ? "down " : (delta > 0 ? "up " : ""),
  509. modminer->clock);
  510. return true;
  511. }
  512. static bool modminer_fpga_init(struct thr_info *thr)
  513. {
  514. struct cgpu_info *modminer = thr->cgpu;
  515. unsigned char cmd[2], buf[4];
  516. int err, amount;
  517. mutex_lock(modminer->modminer_mutex);
  518. cmd[0] = MODMINER_GET_USERCODE;
  519. cmd[1] = modminer->fpgaid;
  520. if ((err = usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTUSERCODE)) < 0 || amount != 2) {
  521. mutex_unlock(modminer->modminer_mutex);
  522. applog(LOG_ERR, "%s%u: Error requesting USER code (%d:%d)",
  523. modminer->api->name, modminer->device_id, amount, err);
  524. return false;
  525. }
  526. if ((err = usb_read(modminer, (char *)buf, 4, &amount, C_GETUSERCODE)) < 0 || amount != 4) {
  527. mutex_unlock(modminer->modminer_mutex);
  528. applog(LOG_ERR, "%s%u: Error reading USER code (%d:%d)",
  529. modminer->api->name, modminer->device_id, amount, err);
  530. return false;
  531. }
  532. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  533. applog(LOG_ERR, "%s%u: FPGA not programmed",
  534. modminer->api->name, modminer->device_id);
  535. if (!modminer_fpga_upload_bitstream(modminer))
  536. return false;
  537. mutex_unlock(modminer->modminer_mutex);
  538. } else {
  539. mutex_unlock(modminer->modminer_mutex);
  540. applog(LOG_DEBUG, "%s%u: FPGA is already programmed :)",
  541. modminer->api->name, modminer->device_id);
  542. }
  543. modminer->clock = MODMINER_DEF_CLOCK;
  544. modminer_delta_clock(thr, MODMINER_CLOCK_SET, false);
  545. thr->primary_thread = true;
  546. return true;
  547. }
  548. static void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  549. {
  550. char info[64];
  551. sprintf(info, " %s%.1fC %3uMHz | ",
  552. (modminer->temp < 10) ? " " : "",
  553. modminer->temp,
  554. (unsigned int)(modminer->clock));
  555. strcat(buf, info);
  556. }
  557. static bool modminer_prepare_next_work(struct modminer_fpga_state *state, struct work *work)
  558. {
  559. char *midstate = state->next_work_cmd + 2;
  560. char *taildata = midstate + 32;
  561. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  562. return false;
  563. memcpy(midstate, work->midstate, 32);
  564. memcpy(taildata, work->data + 64, 12);
  565. return true;
  566. }
  567. static bool modminer_start_work(struct thr_info *thr)
  568. {
  569. struct cgpu_info *modminer = thr->cgpu;
  570. struct modminer_fpga_state *state = thr->cgpu_data;
  571. int err, amount;
  572. bool sta;
  573. if (state->first_work.tv_sec == 0)
  574. gettimeofday(&state->first_work, NULL);
  575. if (state->last_nonce.tv_sec == 0)
  576. gettimeofday(&state->last_nonce, NULL);
  577. mutex_lock(modminer->modminer_mutex);
  578. if ((err = usb_write(modminer, (char *)(state->next_work_cmd), 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
  579. // TODO: err = -4 means the MMQ disappeared - need to delete it and rescan for it? (after a delay?)
  580. // but check all (4) disappeared
  581. mutex_unlock(modminer->modminer_mutex);
  582. applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
  583. modminer->api->name, modminer->device_id, amount, err);
  584. return false;
  585. }
  586. gettimeofday(&state->tv_workstart, NULL);
  587. state->hashes = 0;
  588. sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
  589. if (sta) {
  590. mutex_unlock(modminer->modminer_mutex);
  591. state->new_work = true;
  592. }
  593. return sta;
  594. }
  595. static void check_temperature(struct thr_info *thr)
  596. {
  597. struct cgpu_info *modminer = thr->cgpu;
  598. struct modminer_fpga_state *state = thr->cgpu_data;
  599. char cmd[2], temperature[2];
  600. int tbytes, tamount;
  601. int amount;
  602. if (state->one_byte_temp) {
  603. cmd[0] = MODMINER_TEMP1;
  604. tbytes = 1;
  605. } else {
  606. cmd[0] = MODMINER_TEMP2;
  607. tbytes = 2;
  608. }
  609. cmd[1] = modminer->fpgaid;
  610. mutex_lock(modminer->modminer_mutex);
  611. if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2 &&
  612. usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes)
  613. {
  614. mutex_unlock(modminer->modminer_mutex);
  615. if (state->one_byte_temp)
  616. modminer->temp = temperature[0];
  617. else {
  618. // Only accurate to 2 and a bit places
  619. modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  620. state->tried_two_byte_temp = true;
  621. }
  622. if (state->overheated) {
  623. // Limit recovery to lower than OVERHEAT so it doesn't just go straight over again
  624. if (modminer->temp < MODMINER_RECOVER_TEMP) {
  625. state->overheated = false;
  626. applog(LOG_WARNING, "%s%u: Recovered, temp less than (%.1f) now %.3f",
  627. modminer->api->name, modminer->device_id,
  628. MODMINER_RECOVER_TEMP, modminer->temp);
  629. }
  630. }
  631. else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
  632. if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
  633. applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit! (%.1f) at %.3f",
  634. modminer->api->name, modminer->device_id,
  635. MODMINER_CUTOFF_TEMP, modminer->temp);
  636. modminer_delta_clock(thr, MODMINER_CLOCK_CUTOFF, true);
  637. state->overheated = true;
  638. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  639. } else {
  640. applog(LOG_WARNING, "%s%u: Overheat limit (%.1f) reached %.3f",
  641. modminer->api->name, modminer->device_id,
  642. MODMINER_OVERHEAT_TEMP, modminer->temp);
  643. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, true);
  644. state->overheated = true;
  645. dev_error(modminer, REASON_DEV_OVER_HEAT);
  646. }
  647. }
  648. } else {
  649. mutex_unlock(modminer->modminer_mutex);
  650. if (!state->tried_two_byte_temp) {
  651. state->tried_two_byte_temp = true;
  652. state->one_byte_temp = true;
  653. }
  654. }
  655. }
  656. #define work_restart(thr) thr->work_restart
  657. static uint64_t modminer_process_results(struct thr_info *thr)
  658. {
  659. struct cgpu_info *modminer = thr->cgpu;
  660. struct modminer_fpga_state *state = thr->cgpu_data;
  661. struct work *work = &state->running_work;
  662. struct timeval now;
  663. char cmd[2];
  664. uint32_t nonce;
  665. uint32_t curr_hw_errors;
  666. int err, amount;
  667. int timeoutloop;
  668. double processtime;
  669. int temploop;
  670. // If we are overheated it will just keep checking for results
  671. // since we can't stop the work
  672. // The next work will not start until the temp drops
  673. check_temperature(thr);
  674. cmd[0] = MODMINER_CHECK_WORK;
  675. cmd[1] = modminer->fpgaid;
  676. // 250Mhz is 17.17s
  677. processtime = 17.0;
  678. timeoutloop = 0;
  679. temploop = 0;
  680. while (1) {
  681. mutex_lock(modminer->modminer_mutex);
  682. if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
  683. // TODO: err = -4 means the MMQ disappeared - need to delete it and rescan for it? (after a delay?)
  684. // but check all (4) disappeared
  685. mutex_unlock(modminer->modminer_mutex);
  686. // timeoutloop never resets so the timeouts can't
  687. // accumulate much during a single item of work
  688. if (err == -7 && ++timeoutloop < 10)
  689. goto tryagain;
  690. applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
  691. modminer->api->name, modminer->device_id, amount, err);
  692. return -1;
  693. }
  694. err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
  695. mutex_unlock(modminer->modminer_mutex);
  696. if (err < 0 || amount != 4) {
  697. // timeoutloop never resets so the timeouts can't
  698. // accumulate much during a single item of work
  699. if (err == -7 && ++timeoutloop < 10)
  700. goto tryagain;
  701. applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
  702. modminer->api->name, modminer->device_id, amount, err);
  703. }
  704. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  705. // found 'something' ...
  706. state->shares++;
  707. curr_hw_errors = state->hw_errors;
  708. submit_nonce(thr, work, nonce);
  709. if (state->hw_errors > curr_hw_errors) {
  710. gettimeofday(&now, NULL);
  711. // Ignore initial errors that often happen
  712. if (tdiff(&now, &state->first_work) < 2.0) {
  713. state->shares = 0;
  714. state->shares_last_hw = 0;
  715. state->hw_errors = 0;
  716. } else {
  717. state->shares_last_hw = state->shares;
  718. if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  719. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  720. if (pct >= MODMINER_HW_ERROR_PERCENT)
  721. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false);
  722. }
  723. }
  724. } else {
  725. gettimeofday(&state->last_nonce, NULL);
  726. state->death_stage_one = false;
  727. // If we've reached the required good shares in a row then clock up
  728. if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
  729. modminer->temp < MODMINER_TEMP_UP_LIMIT)
  730. modminer_delta_clock(thr, MODMINER_CLOCK_UP, false);
  731. }
  732. } else {
  733. // on rare occasions - the MMQ can just stop returning valid nonces
  734. double death = ITS_DEAD_JIM * (state->death_stage_one ? 2.0 : 1.0);
  735. gettimeofday(&now, NULL);
  736. if (tdiff(&now, &state->last_nonce) >= death) {
  737. if (state->death_stage_one) {
  738. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false);
  739. applog(LOG_ERR, "%s%u: DEATH clock down",
  740. modminer->api->name, modminer->device_id);
  741. // reset the death info and DISABLE it
  742. state->last_nonce.tv_sec = 0;
  743. state->last_nonce.tv_usec = 0;
  744. state->death_stage_one = false;
  745. return -1;
  746. } else {
  747. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false);
  748. applog(LOG_ERR, "%s%u: death clock down",
  749. modminer->api->name, modminer->device_id);
  750. state->death_stage_one = true;
  751. }
  752. }
  753. }
  754. tryagain:
  755. if (work_restart(thr))
  756. break;
  757. gettimeofday(&now, NULL);
  758. if (tdiff(&now, &state->tv_workstart) > processtime)
  759. break;
  760. // don't check every time
  761. if (state->overheated == true && ++temploop > 30) {
  762. check_temperature(thr);
  763. temploop = 0;
  764. }
  765. nmsleep(10);
  766. if (work_restart(thr))
  767. break;
  768. }
  769. struct timeval tv_workend, elapsed;
  770. gettimeofday(&tv_workend, NULL);
  771. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  772. // Not exact since the clock may have changed ... but close enough I guess
  773. uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  774. if (hashes > 0xffffffff)
  775. hashes = 0xffffffff;
  776. else
  777. if (hashes <= state->hashes)
  778. hashes = 1;
  779. else
  780. hashes -= state->hashes;
  781. state->hashes += hashes;
  782. return hashes;
  783. }
  784. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  785. {
  786. struct modminer_fpga_state *state = thr->cgpu_data;
  787. int64_t hashes = 0;
  788. bool startwork;
  789. struct timeval tv1, tv2;
  790. // Don't start new work if overheated
  791. if (state->overheated == true) {
  792. gettimeofday(&tv1, NULL);
  793. if (state->work_running)
  794. state->work_running = false;
  795. while (state->overheated == true) {
  796. check_temperature(thr);
  797. if (state->overheated == true) {
  798. gettimeofday(&tv2, NULL);
  799. // give up on this work item
  800. if (work_restart(thr) || tdiff(&tv2, &tv1) > 30)
  801. return 0;
  802. // Give it 1s rest then check again
  803. nmsleep(1000);
  804. }
  805. }
  806. }
  807. startwork = modminer_prepare_next_work(state, work);
  808. if (state->work_running) {
  809. hashes = modminer_process_results(thr);
  810. if (hashes == -1)
  811. return hashes;
  812. if (work_restart(thr)) {
  813. state->work_running = false;
  814. return 0;
  815. }
  816. } else
  817. state->work_running = true;
  818. if (startwork) {
  819. if (!modminer_start_work(thr))
  820. return -1;
  821. __copy_work(&state->running_work, work);
  822. }
  823. // This is intentionally early
  824. work->blk.nonce += hashes;
  825. return hashes;
  826. }
  827. static void modminer_hw_error(struct thr_info *thr)
  828. {
  829. struct modminer_fpga_state *state = thr->cgpu_data;
  830. state->hw_errors++;
  831. }
  832. static void modminer_fpga_shutdown(struct thr_info *thr)
  833. {
  834. free(thr->cgpu_data);
  835. }
  836. struct device_api modminer_api = {
  837. .dname = "modminer",
  838. .name = "MMQ",
  839. .api_detect = modminer_detect,
  840. .get_statline_before = get_modminer_statline_before,
  841. .thread_prepare = modminer_fpga_prepare,
  842. .thread_init = modminer_fpga_init,
  843. .scanhash = modminer_scanhash,
  844. .hw_error = modminer_hw_error,
  845. .thread_shutdown = modminer_fpga_shutdown,
  846. };