driver-avalon.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2013 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Luke Dashjr
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <ctype.h>
  19. #include <dirent.h>
  20. #include <unistd.h>
  21. #include <time.h>
  22. #ifndef WIN32
  23. #include <sys/select.h>
  24. #include <termios.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #ifndef O_CLOEXEC
  28. #define O_CLOEXEC 0
  29. #endif
  30. #else
  31. #include "compat.h"
  32. #include <windows.h>
  33. #include <io.h>
  34. #endif
  35. #include "elist.h"
  36. #include "miner.h"
  37. #include "usbutils.h"
  38. #include "driver-avalon.h"
  39. #include "hexdump.c"
  40. #include "util.h"
  41. int opt_avalon_temp = AVALON_TEMP_TARGET;
  42. int opt_avalon_overheat = AVALON_TEMP_OVERHEAT;
  43. int opt_avalon_fan_min = AVALON_DEFAULT_FAN_MIN_PWM;
  44. int opt_avalon_fan_max = AVALON_DEFAULT_FAN_MAX_PWM;
  45. int opt_avalon_freq_min = AVALON_MIN_FREQUENCY;
  46. int opt_avalon_freq_max = AVALON_MAX_FREQUENCY;
  47. int opt_bitburner_core_voltage = BITBURNER_DEFAULT_CORE_VOLTAGE;
  48. int opt_bitburner_fury_core_voltage = BITBURNER_FURY_DEFAULT_CORE_VOLTAGE;
  49. bool opt_avalon_auto;
  50. static int option_offset = -1;
  51. static int bbf_option_offset = -1;
  52. static int avalon_init_task(struct avalon_task *at,
  53. uint8_t reset, uint8_t ff, uint8_t fan,
  54. uint8_t timeout, uint8_t asic_num,
  55. uint8_t miner_num, uint8_t nonce_elf,
  56. uint8_t gate_miner, int frequency)
  57. {
  58. uint16_t *lefreq16;
  59. uint8_t *buf;
  60. static bool first = true;
  61. if (unlikely(!at))
  62. return -1;
  63. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  64. return -1;
  65. memset(at, 0, sizeof(struct avalon_task));
  66. if (unlikely(reset)) {
  67. at->reset = 1;
  68. at->fan_eft = 1;
  69. at->timer_eft = 1;
  70. first = true;
  71. }
  72. at->flush_fifo = (ff ? 1 : 0);
  73. at->fan_eft = (fan ? 1 : 0);
  74. if (unlikely(first && !at->reset)) {
  75. at->fan_eft = 1;
  76. at->timer_eft = 1;
  77. first = false;
  78. }
  79. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  80. at->timeout_data = timeout;
  81. at->asic_num = asic_num;
  82. at->miner_num = miner_num;
  83. at->nonce_elf = nonce_elf;
  84. at->gate_miner_elf = 1;
  85. at->asic_pll = 1;
  86. if (unlikely(gate_miner)) {
  87. at-> gate_miner = 1;
  88. at->asic_pll = 0;
  89. }
  90. buf = (uint8_t *)at;
  91. buf[5] = 0x00;
  92. buf[8] = 0x74;
  93. buf[9] = 0x01;
  94. buf[10] = 0x00;
  95. buf[11] = 0x00;
  96. lefreq16 = (uint16_t *)&buf[6];
  97. *lefreq16 = htole16(frequency * 8);
  98. return 0;
  99. }
  100. static inline void avalon_create_task(struct avalon_task *at,
  101. struct work *work)
  102. {
  103. memcpy(at->midstate, work->midstate, 32);
  104. memcpy(at->data, work->data + 64, 12);
  105. }
  106. static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
  107. {
  108. int err, amount;
  109. err = usb_write(avalon, buf, len, &amount, ep);
  110. applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
  111. avalon->device_id, err);
  112. if (unlikely(err != 0)) {
  113. applog(LOG_WARNING, "usb_write error on avalon_write");
  114. return AVA_SEND_ERROR;
  115. }
  116. if (amount != len) {
  117. applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
  118. return AVA_SEND_ERROR;
  119. }
  120. return AVA_SEND_OK;
  121. }
  122. static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  123. {
  124. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  125. int delay, ret, i, ep = C_AVALON_TASK;
  126. struct avalon_info *info;
  127. cgtimer_t ts_start;
  128. uint32_t nonce_range;
  129. size_t nr_len;
  130. if (at->nonce_elf)
  131. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  132. else
  133. nr_len = AVALON_WRITE_SIZE;
  134. memcpy(buf, at, AVALON_WRITE_SIZE);
  135. if (at->nonce_elf) {
  136. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  137. for (i = 0; i < at->asic_num; i++) {
  138. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  139. (i * nonce_range & 0xff000000) >> 24;
  140. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  141. (i * nonce_range & 0x00ff0000) >> 16;
  142. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  143. (i * nonce_range & 0x0000ff00) >> 8;
  144. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  145. (i * nonce_range & 0x000000ff) >> 0;
  146. }
  147. }
  148. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  149. uint8_t tt = 0;
  150. tt = (buf[0] & 0x0f) << 4;
  151. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  152. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  153. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  154. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  155. buf[0] = tt;
  156. tt = (buf[4] & 0x0f) << 4;
  157. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  158. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  159. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  160. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  161. buf[4] = tt;
  162. #endif
  163. info = avalon->device_data;
  164. delay = nr_len * 10 * 1000000;
  165. delay = delay / info->baud;
  166. delay += 4000;
  167. if (at->reset) {
  168. ep = C_AVALON_RESET;
  169. nr_len = 1;
  170. }
  171. if (opt_debug) {
  172. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  173. hexdump(buf, nr_len);
  174. }
  175. cgsleep_prepare_r(&ts_start);
  176. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  177. cgsleep_us_r(&ts_start, delay);
  178. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %dus", delay);
  179. return ret;
  180. }
  181. static int bitburner_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  182. {
  183. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  184. int ret, ep = C_AVALON_TASK;
  185. cgtimer_t ts_start;
  186. size_t nr_len;
  187. if (at->nonce_elf)
  188. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  189. else
  190. nr_len = AVALON_WRITE_SIZE;
  191. memset(buf, 0, nr_len);
  192. memcpy(buf, at, AVALON_WRITE_SIZE);
  193. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  194. uint8_t tt = 0;
  195. tt = (buf[0] & 0x0f) << 4;
  196. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  197. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  198. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  199. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  200. buf[0] = tt;
  201. tt = (buf[4] & 0x0f) << 4;
  202. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  203. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  204. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  205. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  206. buf[4] = tt;
  207. #endif
  208. if (at->reset) {
  209. ep = C_AVALON_RESET;
  210. nr_len = 1;
  211. }
  212. if (opt_debug) {
  213. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  214. hexdump(buf, nr_len);
  215. }
  216. cgsleep_prepare_r(&ts_start);
  217. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  218. cgsleep_us_r(&ts_start, 3000); // 3 ms = 333 tasks per second, or 1.4 TH/s
  219. return ret;
  220. }
  221. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  222. struct avalon_info *info, struct avalon_result *ar,
  223. struct work *work)
  224. {
  225. uint32_t nonce;
  226. info = avalon->device_data;
  227. info->matching_work[work->subid]++;
  228. nonce = htole32(ar->nonce);
  229. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  230. return submit_nonce(thr, work, nonce);
  231. }
  232. /* Wait until the ftdi chip returns a CTS saying we can send more data. */
  233. static void wait_avalon_ready(struct cgpu_info *avalon)
  234. {
  235. while (avalon_buffer_full(avalon)) {
  236. cgsleep_ms(40);
  237. }
  238. }
  239. #define AVALON_CTS (1 << 4)
  240. static inline bool avalon_cts(char c)
  241. {
  242. return (c & AVALON_CTS);
  243. }
  244. static int avalon_read(struct cgpu_info *avalon, char *buf, size_t bufsize, int ep)
  245. {
  246. int err, amount;
  247. err = usb_read_once(avalon, buf, bufsize, &amount, ep);
  248. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  249. avalon->drv->name, avalon->device_id, err);
  250. return amount;
  251. }
  252. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  253. {
  254. struct avalon_result ar;
  255. int ret, i, spare;
  256. struct avalon_task at;
  257. uint8_t *buf, *tmp;
  258. struct timespec p;
  259. struct avalon_info *info = avalon->device_data;
  260. /* Send reset, then check for result */
  261. avalon_init_task(&at, 1, 0,
  262. AVALON_DEFAULT_FAN_MAX_PWM,
  263. AVALON_DEFAULT_TIMEOUT,
  264. AVALON_DEFAULT_ASIC_NUM,
  265. AVALON_DEFAULT_MINER_NUM,
  266. 0, 0,
  267. AVALON_DEFAULT_FREQUENCY);
  268. wait_avalon_ready(avalon);
  269. ret = avalon_send_task(&at, avalon);
  270. if (unlikely(ret == AVA_SEND_ERROR))
  271. return -1;
  272. if (!initial) {
  273. applog(LOG_ERR, "%s%d reset sequence sent", avalon->drv->name, avalon->device_id);
  274. return 0;
  275. }
  276. ret = avalon_read(avalon, (char *)&ar, AVALON_READ_SIZE, C_GET_AVALON_RESET);
  277. /* What do these sleeps do?? */
  278. p.tv_sec = 0;
  279. p.tv_nsec = AVALON_RESET_PITCH;
  280. nanosleep(&p, NULL);
  281. /* Look for the first occurrence of 0xAA, the reset response should be:
  282. * AA 55 AA 55 00 00 00 00 00 00 */
  283. spare = ret - 10;
  284. buf = tmp = (uint8_t *)&ar;
  285. if (opt_debug) {
  286. applog(LOG_DEBUG, "%s%d reset: get:", avalon->drv->name, avalon->device_id);
  287. hexdump(tmp, AVALON_READ_SIZE);
  288. }
  289. for (i = 0; i <= spare; i++) {
  290. buf = &tmp[i];
  291. if (buf[0] == 0xAA)
  292. break;
  293. }
  294. i = 0;
  295. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  296. buf[2] == 0xAA && buf[3] == 0x55) {
  297. for (i = 4; i < 11; i++)
  298. if (buf[i] != 0)
  299. break;
  300. }
  301. if (i != 11) {
  302. applog(LOG_ERR, "%s%d: Reset failed! not an Avalon?"
  303. " (%d: %02x %02x %02x %02x)", avalon->drv->name, avalon->device_id,
  304. i, buf[0], buf[1], buf[2], buf[3]);
  305. /* FIXME: return 1; */
  306. } else {
  307. /* buf[44]: minor
  308. * buf[45]: day
  309. * buf[46]: year,month, d6: 201306
  310. */
  311. info->ctlr_ver = ((buf[46] >> 4) + 2000) * 1000000 +
  312. (buf[46] & 0x0f) * 10000 +
  313. buf[45] * 100 + buf[44];
  314. applog(LOG_WARNING, "%s%d: Reset succeeded (Controller version: %d)",
  315. avalon->drv->name, avalon->device_id, info->ctlr_ver);
  316. }
  317. return 0;
  318. }
  319. static int avalon_calc_timeout(int frequency)
  320. {
  321. return AVALON_TIMEOUT_FACTOR / frequency;
  322. }
  323. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  324. int *asic_count, int *timeout, int *frequency, char *options)
  325. {
  326. char buf[BUFSIZ+1];
  327. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  328. bool timeout_default;
  329. size_t max;
  330. int i, tmp;
  331. if (options == NULL)
  332. buf[0] = '\0';
  333. else {
  334. ptr = options;
  335. for (i = 0; i < this_option_offset; i++) {
  336. comma = strchr(ptr, ',');
  337. if (comma == NULL)
  338. break;
  339. ptr = comma + 1;
  340. }
  341. comma = strchr(ptr, ',');
  342. if (comma == NULL)
  343. max = strlen(ptr);
  344. else
  345. max = comma - ptr;
  346. if (max > BUFSIZ)
  347. max = BUFSIZ;
  348. strncpy(buf, ptr, max);
  349. buf[max] = '\0';
  350. }
  351. if (!(*buf))
  352. return false;
  353. colon = strchr(buf, ':');
  354. if (colon)
  355. *(colon++) = '\0';
  356. tmp = atoi(buf);
  357. switch (tmp) {
  358. case 115200:
  359. *baud = 115200;
  360. break;
  361. case 57600:
  362. *baud = 57600;
  363. break;
  364. case 38400:
  365. *baud = 38400;
  366. break;
  367. case 19200:
  368. *baud = 19200;
  369. break;
  370. default:
  371. quit(1, "Invalid avalon-options for baud (%s) "
  372. "must be 115200, 57600, 38400 or 19200", buf);
  373. }
  374. if (colon && *colon) {
  375. colon2 = strchr(colon, ':');
  376. if (colon2)
  377. *(colon2++) = '\0';
  378. if (*colon) {
  379. tmp = atoi(colon);
  380. if (tmp > 0 && tmp <= AVALON_MAX_MINER_NUM) {
  381. *miner_count = tmp;
  382. } else {
  383. quit(1, "Invalid avalon-options for "
  384. "miner_count (%s) must be 1 ~ %d",
  385. colon, AVALON_MAX_MINER_NUM);
  386. }
  387. }
  388. if (colon2 && *colon2) {
  389. colon3 = strchr(colon2, ':');
  390. if (colon3)
  391. *(colon3++) = '\0';
  392. tmp = atoi(colon2);
  393. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  394. *asic_count = tmp;
  395. else {
  396. quit(1, "Invalid avalon-options for "
  397. "asic_count (%s) must be 1 ~ %d",
  398. colon2, AVALON_DEFAULT_ASIC_NUM);
  399. }
  400. timeout_default = false;
  401. if (colon3 && *colon3) {
  402. colon4 = strchr(colon3, ':');
  403. if (colon4)
  404. *(colon4++) = '\0';
  405. if (tolower(*colon3) == 'd')
  406. timeout_default = true;
  407. else {
  408. tmp = atoi(colon3);
  409. if (tmp > 0 && tmp <= 0xff)
  410. *timeout = tmp;
  411. else {
  412. quit(1, "Invalid avalon-options for "
  413. "timeout (%s) must be 1 ~ %d",
  414. colon3, 0xff);
  415. }
  416. }
  417. if (colon4 && *colon4) {
  418. tmp = atoi(colon4);
  419. if (tmp < AVALON_MIN_FREQUENCY || tmp > AVALON_MAX_FREQUENCY) {
  420. quit(1, "Invalid avalon-options for frequency, must be %d <= frequency <= %d",
  421. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  422. }
  423. *frequency = tmp;
  424. if (timeout_default)
  425. *timeout = avalon_calc_timeout(*frequency);
  426. }
  427. }
  428. }
  429. }
  430. return true;
  431. }
  432. char *set_avalon_fan(char *arg)
  433. {
  434. int val1, val2, ret;
  435. ret = sscanf(arg, "%d-%d", &val1, &val2);
  436. if (ret < 1)
  437. return "No values passed to avalon-fan";
  438. if (ret == 1)
  439. val2 = val1;
  440. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  441. return "Invalid value passed to avalon-fan";
  442. opt_avalon_fan_min = val1 * AVALON_PWM_MAX / 100;
  443. opt_avalon_fan_max = val2 * AVALON_PWM_MAX / 100;
  444. return NULL;
  445. }
  446. char *set_avalon_freq(char *arg)
  447. {
  448. int val1, val2, ret;
  449. ret = sscanf(arg, "%d-%d", &val1, &val2);
  450. if (ret < 1)
  451. return "No values passed to avalon-freq";
  452. if (ret == 1)
  453. val2 = val1;
  454. if (val1 < AVALON_MIN_FREQUENCY || val1 > AVALON_MAX_FREQUENCY ||
  455. val2 < AVALON_MIN_FREQUENCY || val2 > AVALON_MAX_FREQUENCY ||
  456. val2 < val1)
  457. return "Invalid value passed to avalon-freq";
  458. opt_avalon_freq_min = val1;
  459. opt_avalon_freq_max = val2;
  460. return NULL;
  461. }
  462. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  463. {
  464. int i;
  465. wait_avalon_ready(avalon);
  466. /* Send idle to all miners */
  467. for (i = 0; i < info->miner_count; i++) {
  468. struct avalon_task at;
  469. if (unlikely(avalon_buffer_full(avalon)))
  470. break;
  471. info->idle++;
  472. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  473. info->asic_count, info->miner_count, 1, 1,
  474. info->frequency);
  475. avalon_send_task(&at, avalon);
  476. }
  477. applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
  478. wait_avalon_ready(avalon);
  479. }
  480. static void avalon_initialise(struct cgpu_info *avalon)
  481. {
  482. int err, interface;
  483. if (avalon->usbinfo.nodev)
  484. return;
  485. interface = usb_interface(avalon);
  486. // Reset
  487. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  488. FTDI_VALUE_RESET, interface, C_RESET);
  489. applog(LOG_DEBUG, "%s%i: reset got err %d",
  490. avalon->drv->name, avalon->device_id, err);
  491. if (avalon->usbinfo.nodev)
  492. return;
  493. // Set latency
  494. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  495. AVALON_LATENCY, interface, C_LATENCY);
  496. applog(LOG_DEBUG, "%s%i: latency got err %d",
  497. avalon->drv->name, avalon->device_id, err);
  498. if (avalon->usbinfo.nodev)
  499. return;
  500. // Set data
  501. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  502. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  503. applog(LOG_DEBUG, "%s%i: data got err %d",
  504. avalon->drv->name, avalon->device_id, err);
  505. if (avalon->usbinfo.nodev)
  506. return;
  507. // Set the baud
  508. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  509. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  510. C_SETBAUD);
  511. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  512. avalon->drv->name, avalon->device_id, err);
  513. if (avalon->usbinfo.nodev)
  514. return;
  515. // Set Modem Control
  516. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  517. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  518. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  519. avalon->drv->name, avalon->device_id, err);
  520. if (avalon->usbinfo.nodev)
  521. return;
  522. // Set Flow Control
  523. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  524. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  525. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  526. avalon->drv->name, avalon->device_id, err);
  527. if (avalon->usbinfo.nodev)
  528. return;
  529. /* Avalon repeats the following */
  530. // Set Modem Control
  531. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  532. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  533. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  534. avalon->drv->name, avalon->device_id, err);
  535. if (avalon->usbinfo.nodev)
  536. return;
  537. // Set Flow Control
  538. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  539. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  540. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  541. avalon->drv->name, avalon->device_id, err);
  542. }
  543. static bool is_bitburner(struct cgpu_info *avalon)
  544. {
  545. enum sub_ident ident;
  546. ident = usb_ident(avalon);
  547. return ident == IDENT_BTB || ident == IDENT_BBF;
  548. }
  549. static bool bitburner_set_core_voltage(struct cgpu_info *avalon, int core_voltage)
  550. {
  551. uint8_t buf[2];
  552. int err;
  553. if (is_bitburner(avalon)) {
  554. buf[0] = (uint8_t)core_voltage;
  555. buf[1] = (uint8_t)(core_voltage >> 8);
  556. err = usb_transfer_data(avalon, FTDI_TYPE_OUT, BITBURNER_REQUEST,
  557. BITBURNER_VALUE, BITBURNER_INDEX_SET_VOLTAGE,
  558. (uint32_t *)buf, sizeof(buf), C_BB_SET_VOLTAGE);
  559. if (unlikely(err < 0)) {
  560. applog(LOG_ERR, "%s%i: SetCoreVoltage failed: err = %d",
  561. avalon->drv->name, avalon->device_id, err);
  562. return false;
  563. } else {
  564. applog(LOG_WARNING, "%s%i: Core voltage set to %d millivolts",
  565. avalon->drv->name, avalon->device_id,
  566. core_voltage);
  567. }
  568. return true;
  569. }
  570. return false;
  571. }
  572. static int bitburner_get_core_voltage(struct cgpu_info *avalon)
  573. {
  574. uint8_t buf[2];
  575. int err;
  576. int amount;
  577. if (is_bitburner(avalon)) {
  578. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  579. BITBURNER_VALUE, BITBURNER_INDEX_GET_VOLTAGE,
  580. (char *)buf, sizeof(buf), &amount,
  581. C_BB_GET_VOLTAGE);
  582. if (unlikely(err != 0 || amount != 2)) {
  583. applog(LOG_ERR, "%s%i: GetCoreVoltage failed: err = %d, amount = %d",
  584. avalon->drv->name, avalon->device_id, err, amount);
  585. return 0;
  586. } else {
  587. return (int)(buf[0] + ((unsigned int)buf[1] << 8));
  588. }
  589. } else {
  590. return 0;
  591. }
  592. }
  593. static void bitburner_get_version(struct cgpu_info *avalon)
  594. {
  595. struct avalon_info *info = avalon->device_data;
  596. uint8_t buf[3];
  597. int err;
  598. int amount;
  599. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  600. BITBURNER_VALUE, BITBURNER_INDEX_GET_VERSION,
  601. (char *)buf, sizeof(buf), &amount,
  602. C_GETVERSION);
  603. if (unlikely(err != 0 || amount != sizeof(buf))) {
  604. applog(LOG_DEBUG, "%s%i: GetVersion failed: err=%d, amt=%d assuming %d.%d.%d",
  605. avalon->drv->name, avalon->device_id, err, amount,
  606. BITBURNER_VERSION1, BITBURNER_VERSION2, BITBURNER_VERSION3);
  607. info->version1 = BITBURNER_VERSION1;
  608. info->version2 = BITBURNER_VERSION2;
  609. info->version3 = BITBURNER_VERSION3;
  610. } else {
  611. info->version1 = buf[0];
  612. info->version2 = buf[1];
  613. info->version3 = buf[2];
  614. }
  615. }
  616. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  617. {
  618. int baud, miner_count, asic_count, timeout, frequency;
  619. int this_option_offset;
  620. struct avalon_info *info;
  621. struct cgpu_info *avalon;
  622. bool configured;
  623. int ret;
  624. avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
  625. baud = AVALON_IO_SPEED;
  626. miner_count = AVALON_DEFAULT_MINER_NUM;
  627. asic_count = AVALON_DEFAULT_ASIC_NUM;
  628. timeout = AVALON_DEFAULT_TIMEOUT;
  629. frequency = AVALON_DEFAULT_FREQUENCY;
  630. if (!usb_init(avalon, dev, found))
  631. goto shin;
  632. this_option_offset = usb_ident(avalon) == IDENT_BBF ? ++bbf_option_offset : ++option_offset;
  633. configured = get_options(this_option_offset, &baud, &miner_count,
  634. &asic_count, &timeout, &frequency,
  635. (usb_ident(avalon) == IDENT_BBF && opt_bitburner_fury_options != NULL) ? opt_bitburner_fury_options : opt_avalon_options);
  636. avalon->usbdev->usb_type = USB_TYPE_FTDI;
  637. /* We have a real Avalon! */
  638. avalon_initialise(avalon);
  639. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  640. if (unlikely(!(avalon->device_data)))
  641. quit(1, "Failed to calloc avalon_info data");
  642. info = avalon->device_data;
  643. if (configured) {
  644. info->baud = baud;
  645. info->miner_count = miner_count;
  646. info->asic_count = asic_count;
  647. info->timeout = timeout;
  648. info->frequency = frequency;
  649. } else {
  650. info->baud = AVALON_IO_SPEED;
  651. info->asic_count = AVALON_DEFAULT_ASIC_NUM;
  652. switch (usb_ident(avalon)) {
  653. case IDENT_BBF:
  654. info->miner_count = BITBURNER_FURY_DEFAULT_MINER_NUM;
  655. info->timeout = BITBURNER_FURY_DEFAULT_TIMEOUT;
  656. info->frequency = BITBURNER_FURY_DEFAULT_FREQUENCY;
  657. break;
  658. default:
  659. info->miner_count = AVALON_DEFAULT_MINER_NUM;
  660. info->timeout = AVALON_DEFAULT_TIMEOUT;
  661. info->frequency = AVALON_DEFAULT_FREQUENCY;
  662. }
  663. }
  664. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  665. info->temp_max = 0;
  666. /* This is for check the temp/fan every 3~4s */
  667. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  668. if (info->temp_history_count <= 0)
  669. info->temp_history_count = 1;
  670. info->temp_history_index = 0;
  671. info->temp_sum = 0;
  672. info->temp_old = 0;
  673. if (!add_cgpu(avalon))
  674. goto unshin;
  675. ret = avalon_reset(avalon, true);
  676. if (ret && !configured)
  677. goto unshin;
  678. update_usb_stats(avalon);
  679. avalon_idle(avalon, info);
  680. applog(LOG_DEBUG, "Avalon Detected: %s "
  681. "(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  682. avalon->device_path, info->miner_count, info->asic_count, info->timeout,
  683. info->frequency);
  684. if (usb_ident(avalon) == IDENT_BTB) {
  685. if (opt_bitburner_core_voltage < BITBURNER_MIN_COREMV ||
  686. opt_bitburner_core_voltage > BITBURNER_MAX_COREMV) {
  687. quit(1, "Invalid bitburner-voltage %d must be %dmv - %dmv",
  688. opt_bitburner_core_voltage,
  689. BITBURNER_MIN_COREMV,
  690. BITBURNER_MAX_COREMV);
  691. } else
  692. bitburner_set_core_voltage(avalon, opt_bitburner_core_voltage);
  693. } else if (usb_ident(avalon) == IDENT_BBF) {
  694. if (opt_bitburner_fury_core_voltage < BITBURNER_FURY_MIN_COREMV ||
  695. opt_bitburner_fury_core_voltage > BITBURNER_FURY_MAX_COREMV) {
  696. quit(1, "Invalid bitburner-fury-voltage %d must be %dmv - %dmv",
  697. opt_bitburner_fury_core_voltage,
  698. BITBURNER_FURY_MIN_COREMV,
  699. BITBURNER_FURY_MAX_COREMV);
  700. } else
  701. bitburner_set_core_voltage(avalon, opt_bitburner_fury_core_voltage);
  702. }
  703. if (is_bitburner(avalon)) {
  704. bitburner_get_version(avalon);
  705. }
  706. return true;
  707. unshin:
  708. usb_uninit(avalon);
  709. shin:
  710. free(avalon->device_data);
  711. avalon->device_data = NULL;
  712. avalon = usb_free_cgpu(avalon);
  713. return false;
  714. }
  715. static void avalon_detect(bool __maybe_unused hotplug)
  716. {
  717. usb_detect(&avalon_drv, avalon_detect_one);
  718. }
  719. static void avalon_init(struct cgpu_info *avalon)
  720. {
  721. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  722. }
  723. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  724. {
  725. return clone_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  726. (char *)ar->data, 64, 12);
  727. }
  728. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  729. struct avalon_result *ar);
  730. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  731. {
  732. applog(LOG_INFO, "%s%d: No matching work - HW error",
  733. thr->cgpu->drv->name, thr->cgpu->device_id);
  734. inc_hw_errors(thr);
  735. info->no_matching_work++;
  736. }
  737. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  738. struct thr_info *thr, char *buf, int *offset)
  739. {
  740. int i, spare = *offset - AVALON_READ_SIZE;
  741. bool found = false;
  742. for (i = 0; i <= spare; i++) {
  743. struct avalon_result *ar;
  744. struct work *work;
  745. ar = (struct avalon_result *)&buf[i];
  746. work = avalon_valid_result(avalon, ar);
  747. if (work) {
  748. bool gettemp = false;
  749. found = true;
  750. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  751. mutex_lock(&info->lock);
  752. if (!info->nonces++)
  753. gettemp = true;
  754. info->auto_nonces++;
  755. mutex_unlock(&info->lock);
  756. } else if (opt_avalon_auto) {
  757. mutex_lock(&info->lock);
  758. info->auto_hw++;
  759. mutex_unlock(&info->lock);
  760. }
  761. free_work(work);
  762. if (gettemp)
  763. avalon_update_temps(avalon, info, ar);
  764. break;
  765. }
  766. }
  767. if (!found) {
  768. spare = *offset - AVALON_READ_SIZE;
  769. /* We are buffering and haven't accumulated one more corrupt
  770. * work result. */
  771. if (spare < (int)AVALON_READ_SIZE)
  772. return;
  773. avalon_inc_nvw(info, thr);
  774. } else {
  775. spare = AVALON_READ_SIZE + i;
  776. if (i) {
  777. if (i >= (int)AVALON_READ_SIZE)
  778. avalon_inc_nvw(info, thr);
  779. else
  780. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  781. }
  782. }
  783. *offset -= spare;
  784. memmove(buf, buf + spare, *offset);
  785. }
  786. static void avalon_running_reset(struct cgpu_info *avalon,
  787. struct avalon_info *info)
  788. {
  789. avalon_reset(avalon, false);
  790. avalon_idle(avalon, info);
  791. avalon->results = 0;
  792. info->reset = false;
  793. }
  794. static void *avalon_get_results(void *userdata)
  795. {
  796. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  797. struct avalon_info *info = avalon->device_data;
  798. const int rsize = AVALON_FTDI_READSIZE;
  799. char readbuf[AVALON_READBUF_SIZE];
  800. struct thr_info *thr = info->thr;
  801. int offset = 0, ret = 0;
  802. char threadname[24];
  803. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  804. RenameThread(threadname);
  805. while (likely(!avalon->shutdown)) {
  806. char buf[rsize];
  807. if (offset >= (int)AVALON_READ_SIZE)
  808. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  809. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  810. /* This should never happen */
  811. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  812. offset = 0;
  813. }
  814. if (unlikely(info->reset)) {
  815. avalon_running_reset(avalon, info);
  816. /* Discard anything in the buffer */
  817. offset = 0;
  818. }
  819. ret = avalon_read(avalon, buf, rsize, C_AVALON_READ);
  820. if (ret < 1)
  821. continue;
  822. if (opt_debug) {
  823. applog(LOG_DEBUG, "Avalon: get:");
  824. hexdump((uint8_t *)buf, ret);
  825. }
  826. memcpy(&readbuf[offset], &buf, ret);
  827. offset += ret;
  828. }
  829. return NULL;
  830. }
  831. static void avalon_rotate_array(struct cgpu_info *avalon)
  832. {
  833. avalon->queued = 0;
  834. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  835. avalon->work_array = 0;
  836. }
  837. static void bitburner_rotate_array(struct cgpu_info *avalon)
  838. {
  839. avalon->queued = 0;
  840. if (++avalon->work_array >= BITBURNER_ARRAY_SIZE)
  841. avalon->work_array = 0;
  842. }
  843. static void avalon_set_timeout(struct avalon_info *info)
  844. {
  845. info->timeout = avalon_calc_timeout(info->frequency);
  846. }
  847. static void avalon_set_freq(struct cgpu_info *avalon, int frequency)
  848. {
  849. struct avalon_info *info = avalon->device_data;
  850. info->frequency = frequency;
  851. if (info->frequency > opt_avalon_freq_max)
  852. info->frequency = opt_avalon_freq_max;
  853. if (info->frequency < opt_avalon_freq_min)
  854. info->frequency = opt_avalon_freq_min;
  855. avalon_set_timeout(info);
  856. applog(LOG_WARNING, "%s%i: Set frequency to %d, timeout %d",
  857. avalon->drv->name, avalon->device_id,
  858. info->frequency, info->timeout);
  859. }
  860. static void avalon_inc_freq(struct avalon_info *info)
  861. {
  862. info->frequency += 2;
  863. if (info->frequency > opt_avalon_freq_max)
  864. info->frequency = opt_avalon_freq_max;
  865. avalon_set_timeout(info);
  866. applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
  867. info->frequency, info->timeout);
  868. }
  869. static void avalon_dec_freq(struct avalon_info *info)
  870. {
  871. info->frequency -= 1;
  872. if (info->frequency < opt_avalon_freq_min)
  873. info->frequency = opt_avalon_freq_min;
  874. avalon_set_timeout(info);
  875. applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
  876. info->frequency, info->timeout);
  877. }
  878. static void avalon_reset_auto(struct avalon_info *info)
  879. {
  880. info->auto_queued =
  881. info->auto_nonces =
  882. info->auto_hw = 0;
  883. }
  884. static void avalon_adjust_freq(struct avalon_info *info, struct cgpu_info *avalon)
  885. {
  886. if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
  887. mutex_lock(&info->lock);
  888. if (!info->optimal) {
  889. if (info->fan_pwm >= opt_avalon_fan_max) {
  890. applog(LOG_WARNING,
  891. "%s%i: Above optimal temperature, throttling",
  892. avalon->drv->name, avalon->device_id);
  893. avalon_dec_freq(info);
  894. }
  895. } else if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
  896. info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
  897. int total = info->auto_nonces + info->auto_hw;
  898. /* Try to keep hw errors < 2% */
  899. if (info->auto_hw * 100 < total)
  900. avalon_inc_freq(info);
  901. else if (info->auto_hw * 66 > total)
  902. avalon_dec_freq(info);
  903. }
  904. avalon_reset_auto(info);
  905. mutex_unlock(&info->lock);
  906. }
  907. }
  908. static void *avalon_send_tasks(void *userdata)
  909. {
  910. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  911. struct avalon_info *info = avalon->device_data;
  912. const int avalon_get_work_count = info->miner_count;
  913. char threadname[24];
  914. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  915. RenameThread(threadname);
  916. while (likely(!avalon->shutdown)) {
  917. int start_count, end_count, i, j, ret;
  918. cgtimer_t ts_start;
  919. struct avalon_task at;
  920. bool idled = false;
  921. int64_t us_timeout;
  922. while (avalon_buffer_full(avalon))
  923. cgsleep_ms(40);
  924. avalon_adjust_freq(info, avalon);
  925. /* A full nonce range */
  926. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  927. cgsleep_prepare_r(&ts_start);
  928. mutex_lock(&info->qlock);
  929. start_count = avalon->work_array * avalon_get_work_count;
  930. end_count = start_count + avalon_get_work_count;
  931. for (i = start_count, j = 0; i < end_count; i++, j++) {
  932. if (avalon_buffer_full(avalon)) {
  933. applog(LOG_INFO,
  934. "%s%i: Buffer full after only %d of %d work queued",
  935. avalon->drv->name, avalon->device_id, j, avalon_get_work_count);
  936. break;
  937. }
  938. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  939. avalon_init_task(&at, 0, 0, info->fan_pwm,
  940. info->timeout, info->asic_count,
  941. info->miner_count, 1, 0, info->frequency);
  942. avalon_create_task(&at, avalon->works[i]);
  943. info->auto_queued++;
  944. } else {
  945. int idle_freq = info->frequency;
  946. if (!info->idle++)
  947. idled = true;
  948. if (unlikely(info->overheat && opt_avalon_auto))
  949. idle_freq = AVALON_MIN_FREQUENCY;
  950. avalon_init_task(&at, 0, 0, info->fan_pwm,
  951. info->timeout, info->asic_count,
  952. info->miner_count, 1, 1, idle_freq);
  953. /* Reset the auto_queued count if we end up
  954. * idling any miners. */
  955. avalon_reset_auto(info);
  956. }
  957. ret = avalon_send_task(&at, avalon);
  958. if (unlikely(ret == AVA_SEND_ERROR)) {
  959. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  960. avalon->drv->name, avalon->device_id);
  961. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  962. info->reset = true;
  963. break;
  964. }
  965. }
  966. avalon_rotate_array(avalon);
  967. mutex_unlock(&info->qlock);
  968. cgsem_post(&info->qsem);
  969. if (unlikely(idled)) {
  970. applog(LOG_WARNING, "%s%i: Idled %d miners",
  971. avalon->drv->name, avalon->device_id, idled);
  972. }
  973. /* Sleep how long it would take to complete a full nonce range
  974. * at the current frequency using the clock_nanosleep function
  975. * timed from before we started loading new work so it will
  976. * fall short of the full duration. */
  977. cgsleep_us_r(&ts_start, us_timeout);
  978. }
  979. return NULL;
  980. }
  981. static void *bitburner_send_tasks(void *userdata)
  982. {
  983. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  984. struct avalon_info *info = avalon->device_data;
  985. const int avalon_get_work_count = info->miner_count;
  986. char threadname[24];
  987. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  988. RenameThread(threadname);
  989. while (likely(!avalon->shutdown)) {
  990. int start_count, end_count, i, j, ret;
  991. struct avalon_task at;
  992. bool idled = false;
  993. while (avalon_buffer_full(avalon))
  994. cgsleep_ms(40);
  995. avalon_adjust_freq(info, avalon);
  996. /* Give other threads a chance to acquire qlock. */
  997. i = 0;
  998. do {
  999. cgsleep_ms(40);
  1000. } while (!avalon->shutdown && i++ < 15
  1001. && avalon->queued < avalon_get_work_count);
  1002. mutex_lock(&info->qlock);
  1003. start_count = avalon->work_array * avalon_get_work_count;
  1004. end_count = start_count + avalon_get_work_count;
  1005. for (i = start_count, j = 0; i < end_count; i++, j++) {
  1006. while (avalon_buffer_full(avalon))
  1007. cgsleep_ms(40);
  1008. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  1009. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1010. info->timeout, info->asic_count,
  1011. info->miner_count, 1, 0, info->frequency);
  1012. avalon_create_task(&at, avalon->works[i]);
  1013. info->auto_queued++;
  1014. } else {
  1015. int idle_freq = info->frequency;
  1016. if (!info->idle++)
  1017. idled = true;
  1018. if (unlikely(info->overheat && opt_avalon_auto))
  1019. idle_freq = AVALON_MIN_FREQUENCY;
  1020. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1021. info->timeout, info->asic_count,
  1022. info->miner_count, 1, 1, idle_freq);
  1023. /* Reset the auto_queued count if we end up
  1024. * idling any miners. */
  1025. avalon_reset_auto(info);
  1026. }
  1027. ret = bitburner_send_task(&at, avalon);
  1028. if (unlikely(ret == AVA_SEND_ERROR)) {
  1029. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  1030. avalon->drv->name, avalon->device_id);
  1031. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  1032. info->reset = true;
  1033. break;
  1034. }
  1035. }
  1036. bitburner_rotate_array(avalon);
  1037. mutex_unlock(&info->qlock);
  1038. cgsem_post(&info->qsem);
  1039. if (unlikely(idled)) {
  1040. applog(LOG_WARNING, "%s%i: Idled %d miners",
  1041. avalon->drv->name, avalon->device_id, idled);
  1042. }
  1043. }
  1044. return NULL;
  1045. }
  1046. static bool avalon_prepare(struct thr_info *thr)
  1047. {
  1048. struct cgpu_info *avalon = thr->cgpu;
  1049. struct avalon_info *info = avalon->device_data;
  1050. int array_size = AVALON_ARRAY_SIZE;
  1051. void *(*write_thread_fn)(void *) = avalon_send_tasks;
  1052. if (is_bitburner(avalon)) {
  1053. array_size = BITBURNER_ARRAY_SIZE;
  1054. write_thread_fn = bitburner_send_tasks;
  1055. }
  1056. free(avalon->works);
  1057. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  1058. array_size);
  1059. if (!avalon->works)
  1060. quit(1, "Failed to calloc avalon works in avalon_prepare");
  1061. info->thr = thr;
  1062. mutex_init(&info->lock);
  1063. mutex_init(&info->qlock);
  1064. cgsem_init(&info->qsem);
  1065. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  1066. quit(1, "Failed to create avalon read_thr");
  1067. if (pthread_create(&info->write_thr, NULL, write_thread_fn, (void *)avalon))
  1068. quit(1, "Failed to create avalon write_thr");
  1069. avalon_init(avalon);
  1070. return true;
  1071. }
  1072. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  1073. {
  1074. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  1075. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  1076. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  1077. info->temp0 = ar->temp0;
  1078. info->temp1 = ar->temp1;
  1079. info->temp2 = ar->temp2;
  1080. if (ar->temp0 & 0x80) {
  1081. ar->temp0 &= 0x7f;
  1082. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  1083. }
  1084. if (ar->temp1 & 0x80) {
  1085. ar->temp1 &= 0x7f;
  1086. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  1087. }
  1088. if (ar->temp2 & 0x80) {
  1089. ar->temp2 &= 0x7f;
  1090. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  1091. }
  1092. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  1093. if (info->temp0 > info->temp_max)
  1094. info->temp_max = info->temp0;
  1095. if (info->temp1 > info->temp_max)
  1096. info->temp_max = info->temp1;
  1097. if (info->temp2 > info->temp_max)
  1098. info->temp_max = info->temp2;
  1099. }
  1100. static void temp_rise(struct avalon_info *info, int temp)
  1101. {
  1102. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
  1103. info->fan_pwm = AVALON_PWM_MAX;
  1104. return;
  1105. }
  1106. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
  1107. info->fan_pwm += 10;
  1108. else if (temp > opt_avalon_temp)
  1109. info->fan_pwm += 5;
  1110. else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1111. info->fan_pwm += 1;
  1112. else
  1113. return;
  1114. if (info->fan_pwm > opt_avalon_fan_max)
  1115. info->fan_pwm = opt_avalon_fan_max;
  1116. }
  1117. static void temp_drop(struct avalon_info *info, int temp)
  1118. {
  1119. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
  1120. info->fan_pwm = opt_avalon_fan_min;
  1121. return;
  1122. }
  1123. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
  1124. info->fan_pwm -= 10;
  1125. else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1126. info->fan_pwm -= 5;
  1127. else if (temp < opt_avalon_temp)
  1128. info->fan_pwm -= 1;
  1129. if (info->fan_pwm < opt_avalon_fan_min)
  1130. info->fan_pwm = opt_avalon_fan_min;
  1131. }
  1132. static inline void adjust_fan(struct avalon_info *info)
  1133. {
  1134. int temp_new;
  1135. temp_new = info->temp_sum / info->temp_history_count;
  1136. if (temp_new > info->temp_old)
  1137. temp_rise(info, temp_new);
  1138. else if (temp_new < info->temp_old)
  1139. temp_drop(info, temp_new);
  1140. else {
  1141. /* temp_new == info->temp_old */
  1142. if (temp_new > opt_avalon_temp)
  1143. temp_rise(info, temp_new);
  1144. else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1145. temp_drop(info, temp_new);
  1146. }
  1147. info->temp_old = temp_new;
  1148. if (info->temp_old <= opt_avalon_temp)
  1149. info->optimal = true;
  1150. else
  1151. info->optimal = false;
  1152. }
  1153. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  1154. struct avalon_result *ar)
  1155. {
  1156. record_temp_fan(info, ar, &(avalon->temp));
  1157. applog(LOG_INFO,
  1158. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  1159. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  1160. info->fan0, info->fan1, info->fan2,
  1161. info->temp0, info->temp1, info->temp2, info->temp_max);
  1162. info->temp_history_index++;
  1163. info->temp_sum += avalon->temp;
  1164. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  1165. info->temp_history_index, info->temp_history_count, info->temp_old);
  1166. if (is_bitburner(avalon)) {
  1167. info->core_voltage = bitburner_get_core_voltage(avalon);
  1168. }
  1169. if (info->temp_history_index == info->temp_history_count) {
  1170. adjust_fan(info);
  1171. info->temp_history_index = 0;
  1172. info->temp_sum = 0;
  1173. }
  1174. if (unlikely(info->temp_old >= opt_avalon_overheat)) {
  1175. applog(LOG_WARNING, "%s%d overheat! Idling", avalon->drv->name, avalon->device_id);
  1176. info->overheat = true;
  1177. } else if (info->overheat && info->temp_old <= opt_avalon_temp) {
  1178. applog(LOG_WARNING, "%s%d cooled, restarting", avalon->drv->name, avalon->device_id);
  1179. info->overheat = false;
  1180. }
  1181. }
  1182. static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon)
  1183. {
  1184. struct avalon_info *info = avalon->device_data;
  1185. int lowfan = 10000;
  1186. if (is_bitburner(avalon)) {
  1187. int temp = info->temp0;
  1188. if (info->temp2 > temp)
  1189. temp = info->temp2;
  1190. if (temp > 99)
  1191. temp = 99;
  1192. if (temp < 0)
  1193. temp = 0;
  1194. tailsprintf(buf, bufsiz, "%2dC %3d %4dmV | ", temp, info->frequency, info->core_voltage);
  1195. } else {
  1196. /* Find the lowest fan speed of the ASIC cooling fans. */
  1197. if (info->fan1 >= 0 && info->fan1 < lowfan)
  1198. lowfan = info->fan1;
  1199. if (info->fan2 >= 0 && info->fan2 < lowfan)
  1200. lowfan = info->fan2;
  1201. tailsprintf(buf, bufsiz, "%2dC/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  1202. }
  1203. }
  1204. /* We use a replacement algorithm to only remove references to work done from
  1205. * the buffer when we need the extra space for new work. */
  1206. static bool avalon_fill(struct cgpu_info *avalon)
  1207. {
  1208. struct avalon_info *info = avalon->device_data;
  1209. int subid, slot, mc;
  1210. struct work *work;
  1211. bool ret = true;
  1212. mc = info->miner_count;
  1213. mutex_lock(&info->qlock);
  1214. if (avalon->queued >= mc)
  1215. goto out_unlock;
  1216. work = get_queued(avalon);
  1217. if (unlikely(!work)) {
  1218. ret = false;
  1219. goto out_unlock;
  1220. }
  1221. subid = avalon->queued++;
  1222. work->subid = subid;
  1223. slot = avalon->work_array * mc + subid;
  1224. if (likely(avalon->works[slot]))
  1225. work_completed(avalon, avalon->works[slot]);
  1226. avalon->works[slot] = work;
  1227. if (avalon->queued < mc)
  1228. ret = false;
  1229. out_unlock:
  1230. mutex_unlock(&info->qlock);
  1231. return ret;
  1232. }
  1233. static int64_t avalon_scanhash(struct thr_info *thr)
  1234. {
  1235. struct cgpu_info *avalon = thr->cgpu;
  1236. struct avalon_info *info = avalon->device_data;
  1237. const int miner_count = info->miner_count;
  1238. int64_t hash_count, ms_timeout;
  1239. /* Half nonce range */
  1240. ms_timeout = 0x80000000ll / info->asic_count / info->frequency / 1000;
  1241. /* Wait until avalon_send_tasks signals us that it has completed
  1242. * sending its work or a full nonce range timeout has occurred. We use
  1243. * cgsems to never miss a wakeup. */
  1244. cgsem_mswait(&info->qsem, ms_timeout);
  1245. mutex_lock(&info->lock);
  1246. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  1247. avalon->results += info->nonces + info->idle;
  1248. if (avalon->results > miner_count)
  1249. avalon->results = miner_count;
  1250. if (!info->reset)
  1251. avalon->results--;
  1252. info->nonces = info->idle = 0;
  1253. mutex_unlock(&info->lock);
  1254. /* Check for nothing but consecutive bad results or consistently less
  1255. * results than we should be getting and reset the FPGA if necessary */
  1256. if (!is_bitburner(avalon)) {
  1257. if (avalon->results < -miner_count && !info->reset) {
  1258. applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
  1259. avalon->drv->name, avalon->device_id);
  1260. info->reset = true;
  1261. }
  1262. }
  1263. if (unlikely(avalon->usbinfo.nodev)) {
  1264. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  1265. avalon->drv->name, avalon->device_id);
  1266. avalon->shutdown = true;
  1267. }
  1268. /* This hashmeter is just a utility counter based on returned shares */
  1269. return hash_count;
  1270. }
  1271. static void avalon_flush_work(struct cgpu_info *avalon)
  1272. {
  1273. struct avalon_info *info = avalon->device_data;
  1274. mutex_lock(&info->qlock);
  1275. /* Will overwrite any work queued */
  1276. avalon->queued = 0;
  1277. mutex_unlock(&info->qlock);
  1278. /* Signal main loop we need more work */
  1279. cgsem_post(&info->qsem);
  1280. }
  1281. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  1282. {
  1283. struct api_data *root = NULL;
  1284. struct avalon_info *info = cgpu->device_data;
  1285. char buf[64];
  1286. int i;
  1287. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1288. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1289. root = api_add_int(root, "baud", &(info->baud), false);
  1290. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  1291. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  1292. root = api_add_int(root, "timeout", &(info->timeout), false);
  1293. root = api_add_int(root, "frequency", &(info->frequency), false);
  1294. root = api_add_int(root, "fan1", &(info->fan0), false);
  1295. root = api_add_int(root, "fan2", &(info->fan1), false);
  1296. root = api_add_int(root, "fan3", &(info->fan2), false);
  1297. root = api_add_int(root, "temp1", &(info->temp0), false);
  1298. root = api_add_int(root, "temp2", &(info->temp1), false);
  1299. root = api_add_int(root, "temp3", &(info->temp2), false);
  1300. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1301. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  1302. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1303. for (i = 0; i < info->miner_count; i++) {
  1304. char mcw[24];
  1305. sprintf(mcw, "match_work_count%d", i + 1);
  1306. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1307. }
  1308. if (is_bitburner(cgpu)) {
  1309. root = api_add_int(root, "core_voltage", &(info->core_voltage), false);
  1310. snprintf(buf, sizeof(buf), "%"PRIu8".%"PRIu8".%"PRIu8,
  1311. info->version1, info->version2, info->version3);
  1312. root = api_add_string(root, "version", buf, true);
  1313. }
  1314. root = api_add_uint32(root, "Controller Version", &(info->ctlr_ver), false);
  1315. return root;
  1316. }
  1317. static void avalon_shutdown(struct thr_info *thr)
  1318. {
  1319. struct cgpu_info *avalon = thr->cgpu;
  1320. struct avalon_info *info = avalon->device_data;
  1321. pthread_join(info->read_thr, NULL);
  1322. pthread_join(info->write_thr, NULL);
  1323. avalon_running_reset(avalon, info);
  1324. cgsem_destroy(&info->qsem);
  1325. mutex_destroy(&info->qlock);
  1326. mutex_destroy(&info->lock);
  1327. free(avalon->works);
  1328. avalon->works = NULL;
  1329. }
  1330. static char *avalon_set_device(struct cgpu_info *avalon, char *option, char *setting, char *replybuf)
  1331. {
  1332. int val;
  1333. if (strcasecmp(option, "help") == 0) {
  1334. sprintf(replybuf, "freq: range %d-%d millivolts: range %d-%d",
  1335. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY,
  1336. BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1337. return replybuf;
  1338. }
  1339. if (strcasecmp(option, "millivolts") == 0 || strcasecmp(option, "mv") == 0) {
  1340. if (!is_bitburner(avalon)) {
  1341. sprintf(replybuf, "%s cannot set millivolts", avalon->drv->name);
  1342. return replybuf;
  1343. }
  1344. if (!setting || !*setting) {
  1345. sprintf(replybuf, "missing millivolts setting");
  1346. return replybuf;
  1347. }
  1348. val = atoi(setting);
  1349. if (val < BITBURNER_MIN_COREMV || val > BITBURNER_MAX_COREMV) {
  1350. sprintf(replybuf, "invalid millivolts: '%s' valid range %d-%d",
  1351. setting, BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1352. return replybuf;
  1353. }
  1354. if (bitburner_set_core_voltage(avalon, val))
  1355. return NULL;
  1356. else {
  1357. sprintf(replybuf, "Set millivolts failed");
  1358. return replybuf;
  1359. }
  1360. }
  1361. if (strcasecmp(option, "freq") == 0) {
  1362. if (!setting || !*setting) {
  1363. sprintf(replybuf, "missing freq setting");
  1364. return replybuf;
  1365. }
  1366. val = atoi(setting);
  1367. if (val < AVALON_MIN_FREQUENCY || val > AVALON_MAX_FREQUENCY) {
  1368. sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
  1369. setting, AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  1370. return replybuf;
  1371. }
  1372. avalon_set_freq(avalon, val);
  1373. return NULL;
  1374. }
  1375. sprintf(replybuf, "Unknown option: %s", option);
  1376. return replybuf;
  1377. }
  1378. struct device_drv avalon_drv = {
  1379. .drv_id = DRIVER_avalon,
  1380. .dname = "avalon",
  1381. .name = "AVA",
  1382. .drv_detect = avalon_detect,
  1383. .thread_prepare = avalon_prepare,
  1384. .hash_work = hash_queued_work,
  1385. .queue_full = avalon_fill,
  1386. .scanwork = avalon_scanhash,
  1387. .flush_work = avalon_flush_work,
  1388. .get_api_stats = avalon_api_stats,
  1389. .get_statline_before = get_avalon_statline_before,
  1390. .set_device = avalon_set_device,
  1391. .reinit_device = avalon_init,
  1392. .thread_shutdown = avalon_shutdown,
  1393. };