driver-modminer.c 31 KB

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