driver-modminer.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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->next_work_cmd[0] = MODMINER_SEND_WORK;
  457. state->next_work_cmd[1] = modminer->fpgaid;
  458. state->shares_to_good = MODMINER_EARLY_UP;
  459. state->overheated = false;
  460. return true;
  461. }
  462. /*
  463. * Clocking rules:
  464. * If device exceeds cutoff or overheat temp - stop sending work until it cools
  465. * decrease the clock by MODMINER_CLOCK_CUTOFF/MODMINER_CLOCK_OVERHEAT
  466. * for when it restarts
  467. * with MODMINER_CLOCK_OVERHEAT=0 basically says that temp shouldn't
  468. * affect the clock unless we reach CUTOFF
  469. *
  470. * If device overheats
  471. * set shares_to_good back to MODMINER_MIN_BACK
  472. * to speed up clock recovery if temp drop doesnt help
  473. *
  474. * When to clock down:
  475. * If device gets MODMINER_HW_ERROR_PERCENT errors since last clock up or down
  476. * if clock is <= default it requires 2 HW to do this test
  477. * if clock is > default it only requires 1 HW to do this test
  478. * also double shares_to_good
  479. *
  480. * When to clock up:
  481. * If device gets shares_to_good good shares in a row
  482. * and temp < MODMINER_TEMP_UP_LIMIT
  483. *
  484. * N.B. clock must always be a multiple of 2
  485. */
  486. static const char *clocknodev = "clock failed - no device";
  487. static const char *clockoldwork = "clock already changed for this work";
  488. static const char *clocktoolow = "clock too low";
  489. static const char *clocktoohi = "clock too high";
  490. static const char *clocksetfail = "clock set command failed";
  491. static const char *clockreplyfail = "clock reply failed";
  492. static const char *modminer_delta_clock(struct thr_info *thr, int delta, bool temp, bool force)
  493. {
  494. struct cgpu_info *modminer = thr->cgpu;
  495. struct modminer_fpga_state *state = thr->cgpu_data;
  496. unsigned char cmd[6], buf[1];
  497. int err, amount;
  498. // Device is gone
  499. if (modminer->usbinfo.nodev)
  500. return clocknodev;
  501. // Only do once if multiple shares per work or multiple reasons
  502. if (!state->new_work && !force)
  503. return clockoldwork;
  504. state->new_work = false;
  505. state->shares = 0;
  506. state->shares_last_hw = 0;
  507. state->hw_errors = 0;
  508. // FYI clock drop has little effect on temp
  509. if (delta < 0 && (modminer->clock + delta) < MODMINER_MIN_CLOCK)
  510. return clocktoolow;
  511. if (delta > 0 && (modminer->clock + delta) > MODMINER_MAX_CLOCK)
  512. return clocktoohi;
  513. if (delta < 0) {
  514. if (temp)
  515. state->shares_to_good = MODMINER_MIN_BACK;
  516. else {
  517. if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
  518. state->shares_to_good *= 2;
  519. else
  520. state->shares_to_good = MODMINER_TRY_UP;
  521. }
  522. }
  523. modminer->clock += delta;
  524. cmd[0] = MODMINER_SET_CLOCK;
  525. cmd[1] = modminer->fpgaid;
  526. cmd[2] = modminer->clock;
  527. cmd[3] = cmd[4] = cmd[5] = '\0';
  528. mutex_lock(modminer->modminer_mutex);
  529. if ((err = usb_write(modminer, (char *)cmd, 6, &amount, C_SETCLOCK)) < 0 || amount != 6) {
  530. mutex_unlock(modminer->modminer_mutex);
  531. applog(LOG_ERR, "%s%u: Error writing set clock speed (%d:%d)",
  532. modminer->drv->name, modminer->device_id, amount, err);
  533. return clocksetfail;
  534. }
  535. if ((err = usb_read(modminer, (char *)(&buf), 1, &amount, C_REPLYSETCLOCK)) < 0 || amount != 1) {
  536. mutex_unlock(modminer->modminer_mutex);
  537. applog(LOG_ERR, "%s%u: Error reading set clock speed (%d:%d)",
  538. modminer->drv->name, modminer->device_id, amount, err);
  539. return clockreplyfail;
  540. }
  541. mutex_unlock(modminer->modminer_mutex);
  542. applog(LOG_WARNING, "%s%u: Set clock speed %sto %u",
  543. modminer->drv->name, modminer->device_id,
  544. (delta < 0) ? "down " : (delta > 0 ? "up " : ""),
  545. modminer->clock);
  546. return NULL;
  547. }
  548. static bool modminer_fpga_init(struct thr_info *thr)
  549. {
  550. struct cgpu_info *modminer = thr->cgpu;
  551. unsigned char cmd[2], buf[4];
  552. int err, amount;
  553. mutex_lock(modminer->modminer_mutex);
  554. cmd[0] = MODMINER_GET_USERCODE;
  555. cmd[1] = modminer->fpgaid;
  556. if ((err = usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTUSERCODE)) < 0 || amount != 2) {
  557. mutex_unlock(modminer->modminer_mutex);
  558. applog(LOG_ERR, "%s%u: Error requesting USER code (%d:%d)",
  559. modminer->drv->name, modminer->device_id, amount, err);
  560. return false;
  561. }
  562. if ((err = usb_read(modminer, (char *)buf, 4, &amount, C_GETUSERCODE)) < 0 || amount != 4) {
  563. mutex_unlock(modminer->modminer_mutex);
  564. applog(LOG_ERR, "%s%u: Error reading USER code (%d:%d)",
  565. modminer->drv->name, modminer->device_id, amount, err);
  566. return false;
  567. }
  568. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  569. applog(LOG_ERR, "%s%u: FPGA not programmed",
  570. modminer->drv->name, modminer->device_id);
  571. if (!modminer_fpga_upload_bitstream(modminer))
  572. return false;
  573. mutex_unlock(modminer->modminer_mutex);
  574. } else {
  575. mutex_unlock(modminer->modminer_mutex);
  576. applog(LOG_DEBUG, "%s%u: FPGA is already programmed :)",
  577. modminer->drv->name, modminer->device_id);
  578. }
  579. modminer->clock = MODMINER_DEF_CLOCK;
  580. modminer_delta_clock(thr, MODMINER_CLOCK_SET, false, false);
  581. thr->primary_thread = true;
  582. return true;
  583. }
  584. static void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  585. {
  586. char info[64];
  587. sprintf(info, " %s%.1fC %3uMHz | ",
  588. (modminer->temp < 10) ? " " : "",
  589. modminer->temp,
  590. (unsigned int)(modminer->clock));
  591. strcat(buf, info);
  592. }
  593. static bool modminer_prepare_next_work(struct modminer_fpga_state *state, struct work *work)
  594. {
  595. char *midstate = state->next_work_cmd + 2;
  596. char *taildata = midstate + 32;
  597. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  598. return false;
  599. memcpy(midstate, work->midstate, 32);
  600. memcpy(taildata, work->data + 64, 12);
  601. return true;
  602. }
  603. static bool modminer_start_work(struct thr_info *thr)
  604. {
  605. struct cgpu_info *modminer = thr->cgpu;
  606. struct modminer_fpga_state *state = thr->cgpu_data;
  607. int err, amount;
  608. bool sta;
  609. if (state->first_work.tv_sec == 0)
  610. gettimeofday(&state->first_work, NULL);
  611. if (state->last_nonce.tv_sec == 0)
  612. gettimeofday(&state->last_nonce, NULL);
  613. mutex_lock(modminer->modminer_mutex);
  614. if ((err = usb_write(modminer, (char *)(state->next_work_cmd), 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
  615. mutex_unlock(modminer->modminer_mutex);
  616. applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
  617. modminer->drv->name, modminer->device_id, amount, err);
  618. return false;
  619. }
  620. gettimeofday(&state->tv_workstart, NULL);
  621. state->hashes = 0;
  622. sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
  623. if (sta) {
  624. mutex_unlock(modminer->modminer_mutex);
  625. state->new_work = true;
  626. }
  627. return sta;
  628. }
  629. static void check_temperature(struct thr_info *thr)
  630. {
  631. struct cgpu_info *modminer = thr->cgpu;
  632. struct modminer_fpga_state *state = thr->cgpu_data;
  633. char cmd[2], temperature[2];
  634. int tbytes, tamount;
  635. int amount;
  636. // Device is gone
  637. if (modminer->usbinfo.nodev)
  638. return;
  639. if (state->one_byte_temp) {
  640. cmd[0] = MODMINER_TEMP1;
  641. tbytes = 1;
  642. } else {
  643. cmd[0] = MODMINER_TEMP2;
  644. tbytes = 2;
  645. }
  646. cmd[1] = modminer->fpgaid;
  647. mutex_lock(modminer->modminer_mutex);
  648. if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2 &&
  649. usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes) {
  650. mutex_unlock(modminer->modminer_mutex);
  651. if (state->one_byte_temp)
  652. modminer->temp = temperature[0];
  653. else {
  654. // Only accurate to 2 and a bit places
  655. modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  656. state->tried_two_byte_temp = true;
  657. }
  658. if (state->overheated) {
  659. // Limit recovery to lower than OVERHEAT so it doesn't just go straight over again
  660. if (modminer->temp < MODMINER_RECOVER_TEMP) {
  661. state->overheated = false;
  662. applog(LOG_WARNING, "%s%u: Recovered, temp less than (%.1f) now %.3f",
  663. modminer->drv->name, modminer->device_id,
  664. MODMINER_RECOVER_TEMP, modminer->temp);
  665. }
  666. }
  667. else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
  668. if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
  669. applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit! (%.1f) at %.3f",
  670. modminer->drv->name, modminer->device_id,
  671. MODMINER_CUTOFF_TEMP, modminer->temp);
  672. modminer_delta_clock(thr, MODMINER_CLOCK_CUTOFF, true, false);
  673. state->overheated = true;
  674. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  675. } else {
  676. applog(LOG_WARNING, "%s%u: Overheat limit (%.1f) reached %.3f",
  677. modminer->drv->name, modminer->device_id,
  678. MODMINER_OVERHEAT_TEMP, modminer->temp);
  679. // If it's defined to be 0 then don't call modminer_delta_clock()
  680. if (MODMINER_CLOCK_OVERHEAT != 0)
  681. modminer_delta_clock(thr, MODMINER_CLOCK_OVERHEAT, true, false);
  682. state->overheated = true;
  683. dev_error(modminer, REASON_DEV_OVER_HEAT);
  684. }
  685. }
  686. } else {
  687. mutex_unlock(modminer->modminer_mutex);
  688. if (!state->tried_two_byte_temp) {
  689. state->tried_two_byte_temp = true;
  690. state->one_byte_temp = true;
  691. }
  692. }
  693. }
  694. #define work_restart(thr) thr->work_restart
  695. // 250Mhz is 17.17s - ensure we don't go idle
  696. static const double processtime = 17.0;
  697. // 160Mhz is 26.84 - when overheated ensure we don't throw away shares
  698. static const double overheattime = 26.9;
  699. static uint64_t modminer_process_results(struct thr_info *thr)
  700. {
  701. struct cgpu_info *modminer = thr->cgpu;
  702. struct modminer_fpga_state *state = thr->cgpu_data;
  703. struct work *work = &state->running_work;
  704. struct timeval now;
  705. char cmd[2];
  706. uint32_t nonce;
  707. uint32_t curr_hw_errors;
  708. int err, amount, amount2;
  709. int timeoutloop;
  710. double timeout;
  711. int temploop;
  712. // Device is gone
  713. if (modminer->usbinfo.nodev)
  714. return -1;
  715. // If we are overheated it will just keep checking for results
  716. // since we can't stop the work
  717. // The next work will not start until the temp drops
  718. check_temperature(thr);
  719. cmd[0] = MODMINER_CHECK_WORK;
  720. cmd[1] = modminer->fpgaid;
  721. timeoutloop = 0;
  722. temploop = 0;
  723. while (1) {
  724. mutex_lock(modminer->modminer_mutex);
  725. if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
  726. mutex_unlock(modminer->modminer_mutex);
  727. // timeoutloop never resets so the timeouts can't
  728. // accumulate much during a single item of work
  729. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 10) {
  730. state->timeout_fail++;
  731. goto tryagain;
  732. }
  733. applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
  734. modminer->drv->name, modminer->device_id, amount, err);
  735. return -1;
  736. }
  737. err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
  738. while (err == LIBUSB_SUCCESS && amount < 4) {
  739. size_t remain = 4 - amount;
  740. char *pos = ((char *)(&nonce)) + amount;
  741. state->success_more++;
  742. err = usb_read(modminer, pos, remain, &amount2, C_GETWORKSTATUS);
  743. amount += amount2;
  744. }
  745. mutex_unlock(modminer->modminer_mutex);
  746. if (err < 0 || amount < 4) {
  747. // timeoutloop never resets so the timeouts can't
  748. // accumulate much during a single item of work
  749. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 10) {
  750. state->timeout_fail++;
  751. goto tryagain;
  752. }
  753. applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
  754. modminer->drv->name, modminer->device_id, amount+amount2, err);
  755. }
  756. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  757. // found 'something' ...
  758. state->shares++;
  759. curr_hw_errors = state->hw_errors;
  760. submit_nonce(thr, work, nonce);
  761. if (state->hw_errors > curr_hw_errors) {
  762. gettimeofday(&now, NULL);
  763. // Ignore initial errors that often happen
  764. if (tdiff(&now, &state->first_work) < 2.0) {
  765. state->shares = 0;
  766. state->shares_last_hw = 0;
  767. state->hw_errors = 0;
  768. } else {
  769. state->shares_last_hw = state->shares;
  770. if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  771. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  772. if (pct >= MODMINER_HW_ERROR_PERCENT)
  773. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false, false);
  774. }
  775. }
  776. } else {
  777. gettimeofday(&state->last_nonce, NULL);
  778. state->death_stage_one = false;
  779. // If we've reached the required good shares in a row then clock up
  780. if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
  781. modminer->temp < MODMINER_TEMP_UP_LIMIT)
  782. modminer_delta_clock(thr, MODMINER_CLOCK_UP, false, false);
  783. }
  784. } else {
  785. // on rare occasions - the MMQ can just stop returning valid nonces
  786. double death = ITS_DEAD_JIM * (state->death_stage_one ? 2.0 : 1.0);
  787. gettimeofday(&now, NULL);
  788. if (tdiff(&now, &state->last_nonce) >= death) {
  789. if (state->death_stage_one) {
  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. // reset the death info and DISABLE it
  794. state->last_nonce.tv_sec = 0;
  795. state->last_nonce.tv_usec = 0;
  796. state->death_stage_one = false;
  797. return -1;
  798. } else {
  799. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
  800. applog(LOG_ERR, "%s%u: death clock down",
  801. modminer->drv->name, modminer->device_id);
  802. state->death_stage_one = true;
  803. }
  804. }
  805. }
  806. tryagain:
  807. if (work_restart(thr))
  808. break;
  809. if (state->overheated == true) {
  810. // don't check every time
  811. if (++temploop > 30) {
  812. check_temperature(thr);
  813. temploop = 0;
  814. }
  815. }
  816. if (state->overheated == true)
  817. timeout = overheattime;
  818. else
  819. timeout = processtime;
  820. gettimeofday(&now, NULL);
  821. if (tdiff(&now, &state->tv_workstart) > timeout)
  822. break;
  823. nmsleep(10);
  824. if (work_restart(thr))
  825. break;
  826. }
  827. struct timeval tv_workend, elapsed;
  828. gettimeofday(&tv_workend, NULL);
  829. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  830. // Not exact since the clock may have changed ... but close enough I guess
  831. uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  832. // Overheat will complete the nonce range
  833. if (hashes > 0xffffffff)
  834. hashes = 0xffffffff;
  835. else
  836. if (hashes <= state->hashes)
  837. hashes = 1;
  838. else
  839. hashes -= state->hashes;
  840. state->hashes += hashes;
  841. return hashes;
  842. }
  843. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  844. {
  845. struct modminer_fpga_state *state = thr->cgpu_data;
  846. int64_t hashes = 0;
  847. bool startwork;
  848. struct timeval tv1, tv2;
  849. // Device is gone
  850. if (thr->cgpu->usbinfo.nodev)
  851. return -1;
  852. // Don't start new work if overheated
  853. if (state->overheated == true) {
  854. gettimeofday(&tv1, NULL);
  855. if (state->work_running)
  856. state->work_running = false;
  857. while (state->overheated == true) {
  858. check_temperature(thr);
  859. // Device is gone
  860. if (thr->cgpu->usbinfo.nodev)
  861. return -1;
  862. if (state->overheated == true) {
  863. gettimeofday(&tv2, NULL);
  864. // give up on this work item
  865. if (work_restart(thr) || tdiff(&tv2, &tv1) > 30)
  866. return 0;
  867. // Give it 1s rest then check again
  868. nmsleep(1000);
  869. }
  870. }
  871. }
  872. startwork = modminer_prepare_next_work(state, work);
  873. if (state->work_running) {
  874. hashes = modminer_process_results(thr);
  875. if (hashes == -1)
  876. return hashes;
  877. if (work_restart(thr)) {
  878. state->work_running = false;
  879. return 0;
  880. }
  881. } else
  882. state->work_running = true;
  883. if (startwork) {
  884. if (!modminer_start_work(thr))
  885. return -1;
  886. __copy_work(&state->running_work, work);
  887. }
  888. // This is intentionally early
  889. work->blk.nonce += hashes;
  890. return hashes;
  891. }
  892. static void modminer_hw_error(struct thr_info *thr)
  893. {
  894. struct modminer_fpga_state *state = thr->cgpu_data;
  895. state->hw_errors++;
  896. }
  897. static void modminer_fpga_shutdown(struct thr_info *thr)
  898. {
  899. free(thr->cgpu_data);
  900. }
  901. static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf)
  902. {
  903. const char *ret;
  904. int val;
  905. if (strcasecmp(option, "help") == 0) {
  906. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  907. MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  908. return replybuf;
  909. }
  910. if (strcasecmp(option, "clock") == 0) {
  911. if (!setting || !*setting) {
  912. sprintf(replybuf, "missing clock setting");
  913. return replybuf;
  914. }
  915. val = atoi(setting);
  916. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
  917. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  918. setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  919. return replybuf;
  920. }
  921. val -= (int)(modminer->clock);
  922. ret = modminer_delta_clock(modminer->thr[0], val, false, true);
  923. if (ret) {
  924. sprintf(replybuf, "Set clock failed: %s", ret);
  925. return replybuf;
  926. } else
  927. return NULL;
  928. }
  929. sprintf(replybuf, "Unknown option: %s", option);
  930. return replybuf;
  931. }
  932. struct device_drv modminer_drv = {
  933. .drv_id = DRIVER_MODMINER,
  934. .dname = "ModMiner",
  935. .name = "MMQ",
  936. .drv_detect = modminer_detect,
  937. .get_statline_before = get_modminer_statline_before,
  938. .set_device = modminer_set_device,
  939. .thread_prepare = modminer_fpga_prepare,
  940. .thread_init = modminer_fpga_init,
  941. .scanhash = modminer_scanhash,
  942. .hw_error = modminer_hw_error,
  943. .thread_shutdown = modminer_fpga_shutdown,
  944. };