driver-modminer.c 32 KB

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