driver-modminer.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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)(modminer->usbinfo.bus_number),
  110. (int)(modminer->usbinfo.device_address));
  111. goto shin;
  112. }
  113. sprintf(devpath, "%d:%d",
  114. (int)(modminer->usbinfo.bus_number),
  115. (int)(modminer->usbinfo.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->usbinfo.bus_number),
  169. (int)(modminer->usbinfo.device_address),
  170. i);
  171. tmp->device_path = strdup(devpath);
  172. tmp->usbdev = modminer->usbdev;
  173. tmp->usbinfo.bus_number = modminer->usbinfo.bus_number;
  174. tmp->usbinfo.device_address = modminer->usbinfo.device_address;
  175. // Only the first copy gets the already used stats
  176. if (!added)
  177. tmp->usbinfo.usbstat = modminer->usbinfo.usbstat;
  178. tmp->fpgaid = (char)i;
  179. tmp->modminer_mutex = modminer->modminer_mutex;
  180. tmp->deven = DEV_ENABLED;
  181. tmp->threads = 1;
  182. if (!add_cgpu(tmp)) {
  183. free(tmp->device_path);
  184. if (tmp->drv->copy)
  185. free(tmp->drv);
  186. free(tmp);
  187. goto unshin;
  188. }
  189. update_usb_stats(tmp);
  190. added = true;
  191. }
  192. if (modminer->drv->copy)
  193. free(modminer->drv);
  194. free(modminer);
  195. return true;
  196. unshin:
  197. if (!added)
  198. usb_uninit(modminer);
  199. shin:
  200. if (!added)
  201. free(modminer->modminer_mutex);
  202. if (modminer->drv->copy)
  203. free(modminer->drv);
  204. free(modminer);
  205. if (added)
  206. return true;
  207. else
  208. return false;
  209. }
  210. static void modminer_detect()
  211. {
  212. usb_detect(&modminer_drv, modminer_detect_one);
  213. }
  214. static bool get_expect(struct cgpu_info *modminer, FILE *f, char c)
  215. {
  216. char buf;
  217. if (fread(&buf, 1, 1, f) != 1) {
  218. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream (%c)",
  219. modminer->drv->name, modminer->device_id, errno, c);
  220. return false;
  221. }
  222. if (buf != c) {
  223. applog(LOG_ERR, "%s%u: bitstream code mismatch (%c)",
  224. modminer->drv->name, modminer->device_id, c);
  225. return false;
  226. }
  227. return true;
  228. }
  229. static bool get_info(struct cgpu_info *modminer, FILE *f, char *buf, int bufsiz, const char *name)
  230. {
  231. unsigned char siz[2];
  232. int len;
  233. if (fread(siz, 2, 1, f) != 1) {
  234. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s' len",
  235. modminer->drv->name, modminer->device_id, errno, name);
  236. return false;
  237. }
  238. len = siz[0] * 256 + siz[1];
  239. if (len >= bufsiz) {
  240. applog(LOG_ERR, "%s%u: Bitstream '%s' len too large (%d)",
  241. modminer->drv->name, modminer->device_id, name, len);
  242. return false;
  243. }
  244. if (fread(buf, len, 1, f) != 1) {
  245. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s'", errno,
  246. modminer->drv->name, modminer->device_id, errno, name);
  247. return false;
  248. }
  249. buf[len] = '\0';
  250. return true;
  251. }
  252. #define USE_DEFAULT_TIMEOUT 0
  253. // mutex must always be locked before calling
  254. static bool get_status_timeout(struct cgpu_info *modminer, char *msg, unsigned int timeout, enum usb_cmds cmd)
  255. {
  256. int err, amount;
  257. char buf[1];
  258. if (timeout == USE_DEFAULT_TIMEOUT)
  259. err = usb_read(modminer, buf, 1, &amount, cmd);
  260. else
  261. err = usb_read_timeout(modminer, buf, 1, &amount, timeout, cmd);
  262. if (err < 0 || amount != 1) {
  263. mutex_unlock(modminer->modminer_mutex);
  264. applog(LOG_ERR, "%s%u: Error (%d:%d) getting %s reply",
  265. modminer->drv->name, modminer->device_id, amount, err, msg);
  266. return false;
  267. }
  268. if (buf[0] != 1) {
  269. mutex_unlock(modminer->modminer_mutex);
  270. applog(LOG_ERR, "%s%u: Error, invalid %s reply (was %d should be 1)",
  271. modminer->drv->name, modminer->device_id, msg, buf[0]);
  272. return false;
  273. }
  274. return true;
  275. }
  276. // mutex must always be locked before calling
  277. static bool get_status(struct cgpu_info *modminer, char *msg, enum usb_cmds cmd)
  278. {
  279. return get_status_timeout(modminer, msg, USE_DEFAULT_TIMEOUT, cmd);
  280. }
  281. static bool modminer_fpga_upload_bitstream(struct cgpu_info *modminer)
  282. {
  283. const char *bsfile = BITSTREAM_FILENAME;
  284. char buf[0x100], *p;
  285. char devmsg[64];
  286. unsigned char *ubuf = (unsigned char *)buf;
  287. unsigned long totlen, len;
  288. size_t buflen, remaining;
  289. float nextmsg, upto;
  290. char fpgaid = FPGAID_ALL;
  291. int err, amount, tries;
  292. char *ptr;
  293. FILE *f = open_bitstream("modminer", bsfile);
  294. if (!f) {
  295. mutex_unlock(modminer->modminer_mutex);
  296. applog(LOG_ERR, "%s%u: Error (%d) opening bitstream file %s",
  297. modminer->drv->name, modminer->device_id, errno, bsfile);
  298. return false;
  299. }
  300. if (fread(buf, 2, 1, f) != 1) {
  301. mutex_unlock(modminer->modminer_mutex);
  302. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream magic",
  303. modminer->drv->name, modminer->device_id, errno);
  304. goto dame;
  305. }
  306. if (buf[0] != BITSTREAM_MAGIC_0 || buf[1] != BITSTREAM_MAGIC_1) {
  307. mutex_unlock(modminer->modminer_mutex);
  308. applog(LOG_ERR, "%s%u: bitstream has incorrect magic (%u,%u) instead of (%u,%u)",
  309. modminer->drv->name, modminer->device_id,
  310. buf[0], buf[1],
  311. BITSTREAM_MAGIC_0, BITSTREAM_MAGIC_1);
  312. goto dame;
  313. }
  314. if (fseek(f, 11L, SEEK_CUR)) {
  315. mutex_unlock(modminer->modminer_mutex);
  316. applog(LOG_ERR, "%s%u: Error (%d) bitstream seek failed",
  317. modminer->drv->name, modminer->device_id, errno);
  318. goto dame;
  319. }
  320. if (!get_expect(modminer, f, 'a'))
  321. goto undame;
  322. if (!get_info(modminer, f, buf, sizeof(buf), "Design name"))
  323. goto undame;
  324. applog(LOG_DEBUG, "%s%u: bitstream file '%s' info:",
  325. modminer->drv->name, modminer->device_id, bsfile);
  326. applog(LOG_DEBUG, " Design name: '%s'", buf);
  327. p = strrchr(buf, ';') ? : buf;
  328. p = strrchr(buf, '=') ? : p;
  329. if (p[0] == '=')
  330. p++;
  331. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  332. if (p[0] != '\0') {
  333. mutex_unlock(modminer->modminer_mutex);
  334. applog(LOG_ERR, "%s%u: Bad usercode in bitstream file",
  335. modminer->drv->name, modminer->device_id);
  336. goto dame;
  337. }
  338. if (fwusercode == 0xffffffff) {
  339. mutex_unlock(modminer->modminer_mutex);
  340. applog(LOG_ERR, "%s%u: bitstream doesn't support user code",
  341. modminer->drv->name, modminer->device_id);
  342. goto dame;
  343. }
  344. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  345. if (!get_expect(modminer, f, 'b'))
  346. goto undame;
  347. if (!get_info(modminer, f, buf, sizeof(buf), "Part number"))
  348. goto undame;
  349. applog(LOG_DEBUG, " Part number: '%s'", buf);
  350. if (!get_expect(modminer, f, 'c'))
  351. goto undame;
  352. if (!get_info(modminer, f, buf, sizeof(buf), "Build date"))
  353. goto undame;
  354. applog(LOG_DEBUG, " Build date: '%s'", buf);
  355. if (!get_expect(modminer, f, 'd'))
  356. goto undame;
  357. if (!get_info(modminer, f, buf, sizeof(buf), "Build time"))
  358. goto undame;
  359. applog(LOG_DEBUG, " Build time: '%s'", buf);
  360. if (!get_expect(modminer, f, 'e'))
  361. goto undame;
  362. if (fread(buf, 4, 1, f) != 1) {
  363. mutex_unlock(modminer->modminer_mutex);
  364. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream data len",
  365. modminer->drv->name, modminer->device_id, errno);
  366. goto dame;
  367. }
  368. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  369. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  370. strcpy(devmsg, modminer->device_path);
  371. ptr = strrchr(devmsg, ':');
  372. if (ptr)
  373. *ptr = '\0';
  374. applog(LOG_WARNING, "%s%u: Programming all FPGA on %s ... Mining will not start until complete",
  375. modminer->drv->name, modminer->device_id, devmsg);
  376. buf[0] = MODMINER_PROGRAM;
  377. buf[1] = fpgaid;
  378. buf[2] = (len >> 0) & 0xff;
  379. buf[3] = (len >> 8) & 0xff;
  380. buf[4] = (len >> 16) & 0xff;
  381. buf[5] = (len >> 24) & 0xff;
  382. if ((err = usb_write(modminer, buf, 6, &amount, C_STARTPROGRAM)) < 0 || amount != 6) {
  383. mutex_unlock(modminer->modminer_mutex);
  384. applog(LOG_ERR, "%s%u: Program init failed (%d:%d)",
  385. modminer->drv->name, modminer->device_id, amount, err);
  386. goto dame;
  387. }
  388. if (!get_status(modminer, "initialise", C_STARTPROGRAMSTATUS))
  389. goto undame;
  390. // It must be 32 bytes according to MCU legacy.c
  391. #define WRITE_SIZE 32
  392. totlen = len;
  393. nextmsg = 0.1;
  394. while (len > 0) {
  395. buflen = len < WRITE_SIZE ? len : WRITE_SIZE;
  396. if (fread(buf, buflen, 1, f) != 1) {
  397. mutex_unlock(modminer->modminer_mutex);
  398. applog(LOG_ERR, "%s%u: bitstream file read error %d (%d bytes left)",
  399. modminer->drv->name, modminer->device_id, errno, len);
  400. goto dame;
  401. }
  402. tries = 0;
  403. ptr = buf;
  404. remaining = buflen;
  405. while ((err = usb_write(modminer, ptr, remaining, &amount, C_PROGRAM)) < 0 || amount != (int)remaining) {
  406. if (err == LIBUSB_ERROR_TIMEOUT && amount > 0 && ++tries < 4) {
  407. remaining -= amount;
  408. ptr += amount;
  409. if (opt_debug)
  410. applog(LOG_DEBUG, "%s%u: Program timeout (%d:%d) sent %d tries %d",
  411. modminer->drv->name, modminer->device_id,
  412. amount, err, remaining, tries);
  413. if (!get_status(modminer, "write status", C_PROGRAMSTATUS2))
  414. goto dame;
  415. } else {
  416. mutex_unlock(modminer->modminer_mutex);
  417. applog(LOG_ERR, "%s%u: Program failed (%d:%d) sent %d",
  418. modminer->drv->name, modminer->device_id, amount, err, remaining);
  419. goto dame;
  420. }
  421. }
  422. if (!get_status(modminer, "write status", C_PROGRAMSTATUS))
  423. goto dame;
  424. len -= buflen;
  425. upto = (float)(totlen - len) / (float)(totlen);
  426. if (upto >= nextmsg) {
  427. applog(LOG_WARNING,
  428. "%s%u: Programming %.1f%% (%d out of %d)",
  429. modminer->drv->name, modminer->device_id, upto*100, (totlen - len), totlen);
  430. nextmsg += 0.1;
  431. }
  432. }
  433. if (!get_status(modminer, "final status", C_FINALPROGRAMSTATUS))
  434. goto undame;
  435. applog(LOG_WARNING, "%s%u: Programming completed for all FPGA on %s",
  436. modminer->drv->name, modminer->device_id, devmsg);
  437. // Give it a 2/3s delay after programming
  438. nmsleep(666);
  439. return true;
  440. undame:
  441. ;
  442. mutex_unlock(modminer->modminer_mutex);
  443. ;
  444. dame:
  445. fclose(f);
  446. return false;
  447. }
  448. static bool modminer_fpga_prepare(struct thr_info *thr)
  449. {
  450. struct cgpu_info *modminer = thr->cgpu;
  451. struct timeval now;
  452. gettimeofday(&now, NULL);
  453. get_datestamp(modminer->init, &now);
  454. struct modminer_fpga_state *state;
  455. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  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->usbinfo.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_start_work(struct thr_info *thr, struct work *work)
  592. {
  593. struct cgpu_info *modminer = thr->cgpu;
  594. struct modminer_fpga_state *state = thr->cgpu_data;
  595. int err, amount;
  596. char cmd[48];
  597. bool sta;
  598. cmd[0] = MODMINER_SEND_WORK;
  599. cmd[1] = modminer->fpgaid;
  600. memcpy(&cmd[2], work->midstate, 32);
  601. memcpy(&cmd[34], work->data + 64, 12);
  602. if (state->first_work.tv_sec == 0)
  603. gettimeofday(&state->first_work, NULL);
  604. if (state->last_nonce.tv_sec == 0)
  605. gettimeofday(&state->last_nonce, NULL);
  606. mutex_lock(modminer->modminer_mutex);
  607. if ((err = usb_write(modminer, cmd, 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
  608. mutex_unlock(modminer->modminer_mutex);
  609. applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
  610. modminer->drv->name, modminer->device_id, amount, err);
  611. return false;
  612. }
  613. gettimeofday(&state->tv_workstart, NULL);
  614. sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
  615. if (sta) {
  616. mutex_unlock(modminer->modminer_mutex);
  617. state->new_work = true;
  618. }
  619. return sta;
  620. }
  621. static void check_temperature(struct thr_info *thr)
  622. {
  623. struct cgpu_info *modminer = thr->cgpu;
  624. struct modminer_fpga_state *state = thr->cgpu_data;
  625. char cmd[2], temperature[2];
  626. int tbytes, tamount;
  627. int amount;
  628. // Device is gone
  629. if (modminer->usbinfo.nodev)
  630. return;
  631. if (state->one_byte_temp) {
  632. cmd[0] = MODMINER_TEMP1;
  633. tbytes = 1;
  634. } else {
  635. cmd[0] = MODMINER_TEMP2;
  636. tbytes = 2;
  637. }
  638. cmd[1] = modminer->fpgaid;
  639. mutex_lock(modminer->modminer_mutex);
  640. if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2 &&
  641. usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes) {
  642. mutex_unlock(modminer->modminer_mutex);
  643. if (state->one_byte_temp)
  644. modminer->temp = temperature[0];
  645. else {
  646. // Only accurate to 2 and a bit places
  647. modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  648. state->tried_two_byte_temp = true;
  649. }
  650. if (state->overheated) {
  651. // Limit recovery to lower than OVERHEAT so it doesn't just go straight over again
  652. if (modminer->temp < MODMINER_RECOVER_TEMP) {
  653. state->overheated = false;
  654. applog(LOG_WARNING, "%s%u: Recovered, temp less than (%.1f) now %.3f",
  655. modminer->drv->name, modminer->device_id,
  656. MODMINER_RECOVER_TEMP, modminer->temp);
  657. }
  658. }
  659. else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
  660. if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
  661. applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit! (%.1f) at %.3f",
  662. modminer->drv->name, modminer->device_id,
  663. MODMINER_CUTOFF_TEMP, modminer->temp);
  664. modminer_delta_clock(thr, MODMINER_CLOCK_CUTOFF, true, false);
  665. state->overheated = true;
  666. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  667. } else {
  668. applog(LOG_WARNING, "%s%u: Overheat limit (%.1f) reached %.3f",
  669. modminer->drv->name, modminer->device_id,
  670. MODMINER_OVERHEAT_TEMP, modminer->temp);
  671. // If it's defined to be 0 then don't call modminer_delta_clock()
  672. if (MODMINER_CLOCK_OVERHEAT != 0)
  673. modminer_delta_clock(thr, MODMINER_CLOCK_OVERHEAT, true, false);
  674. state->overheated = true;
  675. dev_error(modminer, REASON_DEV_OVER_HEAT);
  676. }
  677. }
  678. } else {
  679. mutex_unlock(modminer->modminer_mutex);
  680. if (!state->tried_two_byte_temp) {
  681. state->tried_two_byte_temp = true;
  682. state->one_byte_temp = true;
  683. }
  684. }
  685. }
  686. #define work_restart(thr) thr->work_restart
  687. // 250Mhz is 17.17s - ensure we don't go idle
  688. static const double processtime = 17.0;
  689. // 160Mhz is 26.84 - when overheated ensure we don't throw away shares
  690. static const double overheattime = 26.9;
  691. static uint64_t modminer_process_results(struct thr_info *thr, struct work *work)
  692. {
  693. struct cgpu_info *modminer = thr->cgpu;
  694. struct modminer_fpga_state *state = thr->cgpu_data;
  695. struct timeval now;
  696. char cmd[2];
  697. uint32_t nonce;
  698. uint32_t curr_hw_errors;
  699. int err, amount, amount2;
  700. int timeoutloop;
  701. double timeout;
  702. int temploop;
  703. // Device is gone
  704. if (modminer->usbinfo.nodev)
  705. return -1;
  706. // If we are overheated it will just keep checking for results
  707. // since we can't stop the work
  708. // The next work will not start until the temp drops
  709. check_temperature(thr);
  710. cmd[0] = MODMINER_CHECK_WORK;
  711. cmd[1] = modminer->fpgaid;
  712. timeoutloop = 0;
  713. temploop = 0;
  714. while (0x80085) {
  715. mutex_lock(modminer->modminer_mutex);
  716. if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
  717. mutex_unlock(modminer->modminer_mutex);
  718. // timeoutloop never resets so the timeouts can't
  719. // accumulate much during a single item of work
  720. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 5) {
  721. state->timeout_fail++;
  722. goto tryagain;
  723. }
  724. applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
  725. modminer->drv->name, modminer->device_id, amount, err);
  726. return -1;
  727. }
  728. err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
  729. while (err == LIBUSB_SUCCESS && amount < 4) {
  730. size_t remain = 4 - amount;
  731. char *pos = ((char *)(&nonce)) + amount;
  732. state->success_more++;
  733. err = usb_read(modminer, pos, remain, &amount2, C_GETWORKSTATUS);
  734. amount += amount2;
  735. }
  736. mutex_unlock(modminer->modminer_mutex);
  737. if (err < 0 || amount < 4) {
  738. // timeoutloop never resets so the timeouts can't
  739. // accumulate much during a single item of work
  740. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 10) {
  741. state->timeout_fail++;
  742. goto tryagain;
  743. }
  744. applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
  745. modminer->drv->name, modminer->device_id, amount+amount2, err);
  746. }
  747. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  748. // found 'something' ...
  749. state->shares++;
  750. curr_hw_errors = state->hw_errors;
  751. submit_nonce(thr, work, nonce);
  752. if (state->hw_errors > curr_hw_errors) {
  753. gettimeofday(&now, NULL);
  754. // Ignore initial errors that often happen
  755. if (tdiff(&now, &state->first_work) < 2.0) {
  756. state->shares = 0;
  757. state->shares_last_hw = 0;
  758. state->hw_errors = 0;
  759. } else {
  760. state->shares_last_hw = state->shares;
  761. if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  762. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  763. if (pct >= MODMINER_HW_ERROR_PERCENT)
  764. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false, false);
  765. }
  766. }
  767. } else {
  768. gettimeofday(&state->last_nonce, NULL);
  769. state->death_stage_one = false;
  770. // If we've reached the required good shares in a row then clock up
  771. if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
  772. modminer->temp < MODMINER_TEMP_UP_LIMIT)
  773. modminer_delta_clock(thr, MODMINER_CLOCK_UP, false, false);
  774. }
  775. } else {
  776. // on rare occasions - the MMQ can just stop returning valid nonces
  777. double death = ITS_DEAD_JIM * (state->death_stage_one ? 2.0 : 1.0);
  778. gettimeofday(&now, NULL);
  779. if (tdiff(&now, &state->last_nonce) >= death) {
  780. if (state->death_stage_one) {
  781. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
  782. applog(LOG_ERR, "%s%u: DEATH clock down",
  783. modminer->drv->name, modminer->device_id);
  784. // reset the death info and DISABLE it
  785. state->last_nonce.tv_sec = 0;
  786. state->last_nonce.tv_usec = 0;
  787. state->death_stage_one = false;
  788. return -1;
  789. } else {
  790. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
  791. applog(LOG_ERR, "%s%u: death clock down",
  792. modminer->drv->name, modminer->device_id);
  793. state->death_stage_one = true;
  794. }
  795. }
  796. }
  797. tryagain:
  798. if (work_restart(thr))
  799. break;
  800. if (state->overheated == true) {
  801. // don't check every time (every ~1/2 sec)
  802. if (++temploop > 4) {
  803. check_temperature(thr);
  804. temploop = 0;
  805. }
  806. }
  807. if (state->overheated == true)
  808. timeout = overheattime;
  809. else
  810. timeout = processtime;
  811. gettimeofday(&now, NULL);
  812. if (tdiff(&now, &state->tv_workstart) > timeout)
  813. break;
  814. // 1/10th sec to lower CPU usage
  815. nmsleep(100);
  816. if (work_restart(thr))
  817. break;
  818. }
  819. struct timeval tv_workend, elapsed;
  820. gettimeofday(&tv_workend, NULL);
  821. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  822. // Not exact since the clock may have changed ... but close enough I guess
  823. uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  824. // Overheat will complete the nonce range
  825. if (hashes > 0xffffffff)
  826. hashes = 0xffffffff;
  827. work->blk.nonce = 0xffffffff;
  828. return hashes;
  829. }
  830. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  831. {
  832. struct modminer_fpga_state *state = thr->cgpu_data;
  833. struct timeval tv1, tv2;
  834. int64_t hashes;
  835. // Device is gone
  836. if (thr->cgpu->usbinfo.nodev)
  837. return -1;
  838. // Don't start new work if overheated
  839. if (state->overheated == true) {
  840. gettimeofday(&tv1, NULL);
  841. while (state->overheated == true) {
  842. check_temperature(thr);
  843. // Device is gone
  844. if (thr->cgpu->usbinfo.nodev)
  845. return -1;
  846. if (state->overheated == true) {
  847. gettimeofday(&tv2, NULL);
  848. // give up on this work item after 30s
  849. if (work_restart(thr) || tdiff(&tv2, &tv1) > 30)
  850. return 0;
  851. // Give it 1s rest then check again
  852. nmsleep(1000);
  853. }
  854. }
  855. }
  856. if (!modminer_start_work(thr, work))
  857. return -1;
  858. hashes = modminer_process_results(thr, work);
  859. if (hashes == -1)
  860. return hashes;
  861. return hashes;
  862. }
  863. static void modminer_hw_error(struct thr_info *thr)
  864. {
  865. struct modminer_fpga_state *state = thr->cgpu_data;
  866. state->hw_errors++;
  867. }
  868. static void modminer_fpga_shutdown(struct thr_info *thr)
  869. {
  870. free(thr->cgpu_data);
  871. }
  872. static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf)
  873. {
  874. const char *ret;
  875. int val;
  876. if (strcasecmp(option, "help") == 0) {
  877. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  878. MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  879. return replybuf;
  880. }
  881. if (strcasecmp(option, "clock") == 0) {
  882. if (!setting || !*setting) {
  883. sprintf(replybuf, "missing clock setting");
  884. return replybuf;
  885. }
  886. val = atoi(setting);
  887. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
  888. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  889. setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  890. return replybuf;
  891. }
  892. val -= (int)(modminer->clock);
  893. ret = modminer_delta_clock(modminer->thr[0], val, false, true);
  894. if (ret) {
  895. sprintf(replybuf, "Set clock failed: %s", ret);
  896. return replybuf;
  897. } else
  898. return NULL;
  899. }
  900. sprintf(replybuf, "Unknown option: %s", option);
  901. return replybuf;
  902. }
  903. struct device_drv modminer_drv = {
  904. .drv_id = DRIVER_MODMINER,
  905. .dname = "ModMiner",
  906. .name = "MMQ",
  907. .drv_detect = modminer_detect,
  908. .get_statline_before = get_modminer_statline_before,
  909. .set_device = modminer_set_device,
  910. .thread_prepare = modminer_fpga_prepare,
  911. .thread_init = modminer_fpga_init,
  912. .scanhash = modminer_scanhash,
  913. .hw_error = modminer_hw_error,
  914. .thread_shutdown = modminer_fpga_shutdown,
  915. };