driver-avalon.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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, unsigned char *buf,
  245. size_t bufsize, int timeout, int ep)
  246. {
  247. size_t total = 0, readsize = bufsize + 2;
  248. char readbuf[AVALON_READBUF_SIZE];
  249. int err, amount, ofs = 2, cp;
  250. err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
  251. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  252. avalon->drv->name, avalon->device_id, err);
  253. if (amount < 2)
  254. goto out;
  255. /* The first 2 of every 64 bytes are status on FTDIRL */
  256. while (amount > 2) {
  257. cp = amount - 2;
  258. if (cp > 62)
  259. cp = 62;
  260. memcpy(&buf[total], &readbuf[ofs], cp);
  261. total += cp;
  262. amount -= cp + 2;
  263. ofs += 64;
  264. }
  265. out:
  266. return total;
  267. }
  268. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  269. {
  270. struct avalon_result ar;
  271. int ret, i, spare;
  272. struct avalon_task at;
  273. uint8_t *buf, *tmp;
  274. struct timespec p;
  275. struct avalon_info *info = avalon->device_data;
  276. /* Send reset, then check for result */
  277. avalon_init_task(&at, 1, 0,
  278. AVALON_DEFAULT_FAN_MAX_PWM,
  279. AVALON_DEFAULT_TIMEOUT,
  280. AVALON_DEFAULT_ASIC_NUM,
  281. AVALON_DEFAULT_MINER_NUM,
  282. 0, 0,
  283. AVALON_DEFAULT_FREQUENCY);
  284. wait_avalon_ready(avalon);
  285. ret = avalon_send_task(&at, avalon);
  286. if (unlikely(ret == AVA_SEND_ERROR))
  287. return -1;
  288. if (!initial) {
  289. applog(LOG_ERR, "%s%d reset sequence sent", avalon->drv->name, avalon->device_id);
  290. return 0;
  291. }
  292. ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
  293. AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
  294. /* What do these sleeps do?? */
  295. p.tv_sec = 0;
  296. p.tv_nsec = AVALON_RESET_PITCH;
  297. nanosleep(&p, NULL);
  298. /* Look for the first occurrence of 0xAA, the reset response should be:
  299. * AA 55 AA 55 00 00 00 00 00 00 */
  300. spare = ret - 10;
  301. buf = tmp = (uint8_t *)&ar;
  302. if (opt_debug) {
  303. applog(LOG_DEBUG, "%s%d reset: get:", avalon->drv->name, avalon->device_id);
  304. hexdump(tmp, AVALON_READ_SIZE);
  305. }
  306. for (i = 0; i <= spare; i++) {
  307. buf = &tmp[i];
  308. if (buf[0] == 0xAA)
  309. break;
  310. }
  311. i = 0;
  312. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  313. buf[2] == 0xAA && buf[3] == 0x55) {
  314. for (i = 4; i < 11; i++)
  315. if (buf[i] != 0)
  316. break;
  317. }
  318. if (i != 11) {
  319. applog(LOG_ERR, "%s%d: Reset failed! not an Avalon?"
  320. " (%d: %02x %02x %02x %02x)", avalon->drv->name, avalon->device_id,
  321. i, buf[0], buf[1], buf[2], buf[3]);
  322. /* FIXME: return 1; */
  323. } else {
  324. /* buf[44]: minor
  325. * buf[45]: day
  326. * buf[46]: year,month, d6: 201306
  327. */
  328. info->ctlr_ver = ((buf[46] >> 4) + 2000) * 1000000 +
  329. (buf[46] & 0x0f) * 10000 +
  330. buf[45] * 100 + buf[44];
  331. applog(LOG_WARNING, "%s%d: Reset succeeded (Controller version: %d)",
  332. avalon->drv->name, avalon->device_id, info->ctlr_ver);
  333. }
  334. return 0;
  335. }
  336. static int avalon_calc_timeout(int frequency)
  337. {
  338. return AVALON_TIMEOUT_FACTOR / frequency;
  339. }
  340. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  341. int *asic_count, int *timeout, int *frequency, char *options)
  342. {
  343. char buf[BUFSIZ+1];
  344. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  345. bool timeout_default;
  346. size_t max;
  347. int i, tmp;
  348. if (options == NULL)
  349. buf[0] = '\0';
  350. else {
  351. ptr = options;
  352. for (i = 0; i < this_option_offset; i++) {
  353. comma = strchr(ptr, ',');
  354. if (comma == NULL)
  355. break;
  356. ptr = comma + 1;
  357. }
  358. comma = strchr(ptr, ',');
  359. if (comma == NULL)
  360. max = strlen(ptr);
  361. else
  362. max = comma - ptr;
  363. if (max > BUFSIZ)
  364. max = BUFSIZ;
  365. strncpy(buf, ptr, max);
  366. buf[max] = '\0';
  367. }
  368. if (!(*buf))
  369. return false;
  370. colon = strchr(buf, ':');
  371. if (colon)
  372. *(colon++) = '\0';
  373. tmp = atoi(buf);
  374. switch (tmp) {
  375. case 115200:
  376. *baud = 115200;
  377. break;
  378. case 57600:
  379. *baud = 57600;
  380. break;
  381. case 38400:
  382. *baud = 38400;
  383. break;
  384. case 19200:
  385. *baud = 19200;
  386. break;
  387. default:
  388. quit(1, "Invalid avalon-options for baud (%s) "
  389. "must be 115200, 57600, 38400 or 19200", buf);
  390. }
  391. if (colon && *colon) {
  392. colon2 = strchr(colon, ':');
  393. if (colon2)
  394. *(colon2++) = '\0';
  395. if (*colon) {
  396. tmp = atoi(colon);
  397. if (tmp > 0 && tmp <= AVALON_MAX_MINER_NUM) {
  398. *miner_count = tmp;
  399. } else {
  400. quit(1, "Invalid avalon-options for "
  401. "miner_count (%s) must be 1 ~ %d",
  402. colon, AVALON_MAX_MINER_NUM);
  403. }
  404. }
  405. if (colon2 && *colon2) {
  406. colon3 = strchr(colon2, ':');
  407. if (colon3)
  408. *(colon3++) = '\0';
  409. tmp = atoi(colon2);
  410. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  411. *asic_count = tmp;
  412. else {
  413. quit(1, "Invalid avalon-options for "
  414. "asic_count (%s) must be 1 ~ %d",
  415. colon2, AVALON_DEFAULT_ASIC_NUM);
  416. }
  417. timeout_default = false;
  418. if (colon3 && *colon3) {
  419. colon4 = strchr(colon3, ':');
  420. if (colon4)
  421. *(colon4++) = '\0';
  422. if (tolower(*colon3) == 'd')
  423. timeout_default = true;
  424. else {
  425. tmp = atoi(colon3);
  426. if (tmp > 0 && tmp <= 0xff)
  427. *timeout = tmp;
  428. else {
  429. quit(1, "Invalid avalon-options for "
  430. "timeout (%s) must be 1 ~ %d",
  431. colon3, 0xff);
  432. }
  433. }
  434. if (colon4 && *colon4) {
  435. tmp = atoi(colon4);
  436. if (tmp < AVALON_MIN_FREQUENCY || tmp > AVALON_MAX_FREQUENCY) {
  437. quit(1, "Invalid avalon-options for frequency, must be %d <= frequency <= %d",
  438. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  439. }
  440. *frequency = tmp;
  441. if (timeout_default)
  442. *timeout = avalon_calc_timeout(*frequency);
  443. }
  444. }
  445. }
  446. }
  447. return true;
  448. }
  449. char *set_avalon_fan(char *arg)
  450. {
  451. int val1, val2, ret;
  452. ret = sscanf(arg, "%d-%d", &val1, &val2);
  453. if (ret < 1)
  454. return "No values passed to avalon-fan";
  455. if (ret == 1)
  456. val2 = val1;
  457. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  458. return "Invalid value passed to avalon-fan";
  459. opt_avalon_fan_min = val1 * AVALON_PWM_MAX / 100;
  460. opt_avalon_fan_max = val2 * AVALON_PWM_MAX / 100;
  461. return NULL;
  462. }
  463. char *set_avalon_freq(char *arg)
  464. {
  465. int val1, val2, ret;
  466. ret = sscanf(arg, "%d-%d", &val1, &val2);
  467. if (ret < 1)
  468. return "No values passed to avalon-freq";
  469. if (ret == 1)
  470. val2 = val1;
  471. if (val1 < AVALON_MIN_FREQUENCY || val1 > AVALON_MAX_FREQUENCY ||
  472. val2 < AVALON_MIN_FREQUENCY || val2 > AVALON_MAX_FREQUENCY ||
  473. val2 < val1)
  474. return "Invalid value passed to avalon-freq";
  475. opt_avalon_freq_min = val1;
  476. opt_avalon_freq_max = val2;
  477. return NULL;
  478. }
  479. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  480. {
  481. int i;
  482. wait_avalon_ready(avalon);
  483. /* Send idle to all miners */
  484. for (i = 0; i < info->miner_count; i++) {
  485. struct avalon_task at;
  486. if (unlikely(avalon_buffer_full(avalon)))
  487. break;
  488. info->idle++;
  489. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  490. info->asic_count, info->miner_count, 1, 1,
  491. info->frequency);
  492. avalon_send_task(&at, avalon);
  493. }
  494. applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
  495. wait_avalon_ready(avalon);
  496. }
  497. static void avalon_initialise(struct cgpu_info *avalon)
  498. {
  499. int err, interface;
  500. if (avalon->usbinfo.nodev)
  501. return;
  502. interface = usb_interface(avalon);
  503. // Reset
  504. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  505. FTDI_VALUE_RESET, interface, C_RESET);
  506. applog(LOG_DEBUG, "%s%i: reset got err %d",
  507. avalon->drv->name, avalon->device_id, err);
  508. if (avalon->usbinfo.nodev)
  509. return;
  510. // Set latency
  511. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  512. AVALON_LATENCY, interface, C_LATENCY);
  513. applog(LOG_DEBUG, "%s%i: latency got err %d",
  514. avalon->drv->name, avalon->device_id, err);
  515. if (avalon->usbinfo.nodev)
  516. return;
  517. // Set data
  518. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  519. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  520. applog(LOG_DEBUG, "%s%i: data got err %d",
  521. avalon->drv->name, avalon->device_id, err);
  522. if (avalon->usbinfo.nodev)
  523. return;
  524. // Set the baud
  525. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  526. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  527. C_SETBAUD);
  528. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  529. avalon->drv->name, avalon->device_id, err);
  530. if (avalon->usbinfo.nodev)
  531. return;
  532. // Set Modem Control
  533. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  534. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  535. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  536. avalon->drv->name, avalon->device_id, err);
  537. if (avalon->usbinfo.nodev)
  538. return;
  539. // Set Flow Control
  540. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  541. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  542. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  543. avalon->drv->name, avalon->device_id, err);
  544. if (avalon->usbinfo.nodev)
  545. return;
  546. /* Avalon repeats the following */
  547. // Set Modem Control
  548. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  549. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  550. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  551. avalon->drv->name, avalon->device_id, err);
  552. if (avalon->usbinfo.nodev)
  553. return;
  554. // Set Flow Control
  555. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  556. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  557. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  558. avalon->drv->name, avalon->device_id, err);
  559. }
  560. static bool is_bitburner(struct cgpu_info *avalon)
  561. {
  562. enum sub_ident ident;
  563. ident = usb_ident(avalon);
  564. return ident == IDENT_BTB || ident == IDENT_BBF;
  565. }
  566. static bool bitburner_set_core_voltage(struct cgpu_info *avalon, int core_voltage)
  567. {
  568. uint8_t buf[2];
  569. int err;
  570. if (is_bitburner(avalon)) {
  571. buf[0] = (uint8_t)core_voltage;
  572. buf[1] = (uint8_t)(core_voltage >> 8);
  573. err = usb_transfer_data(avalon, FTDI_TYPE_OUT, BITBURNER_REQUEST,
  574. BITBURNER_VALUE, BITBURNER_INDEX_SET_VOLTAGE,
  575. (uint32_t *)buf, sizeof(buf), C_BB_SET_VOLTAGE);
  576. if (unlikely(err < 0)) {
  577. applog(LOG_ERR, "%s%i: SetCoreVoltage failed: err = %d",
  578. avalon->drv->name, avalon->device_id, err);
  579. return false;
  580. } else {
  581. applog(LOG_WARNING, "%s%i: Core voltage set to %d millivolts",
  582. avalon->drv->name, avalon->device_id,
  583. core_voltage);
  584. }
  585. return true;
  586. }
  587. return false;
  588. }
  589. static int bitburner_get_core_voltage(struct cgpu_info *avalon)
  590. {
  591. uint8_t buf[2];
  592. int err;
  593. int amount;
  594. if (is_bitburner(avalon)) {
  595. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  596. BITBURNER_VALUE, BITBURNER_INDEX_GET_VOLTAGE,
  597. (char *)buf, sizeof(buf), &amount,
  598. C_BB_GET_VOLTAGE);
  599. if (unlikely(err != 0 || amount != 2)) {
  600. applog(LOG_ERR, "%s%i: GetCoreVoltage failed: err = %d, amount = %d",
  601. avalon->drv->name, avalon->device_id, err, amount);
  602. return 0;
  603. } else {
  604. return (int)(buf[0] + ((unsigned int)buf[1] << 8));
  605. }
  606. } else {
  607. return 0;
  608. }
  609. }
  610. static void bitburner_get_version(struct cgpu_info *avalon)
  611. {
  612. struct avalon_info *info = avalon->device_data;
  613. uint8_t buf[3];
  614. int err;
  615. int amount;
  616. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  617. BITBURNER_VALUE, BITBURNER_INDEX_GET_VERSION,
  618. (char *)buf, sizeof(buf), &amount,
  619. C_GETVERSION);
  620. if (unlikely(err != 0 || amount != sizeof(buf))) {
  621. applog(LOG_DEBUG, "%s%i: GetVersion failed: err=%d, amt=%d assuming %d.%d.%d",
  622. avalon->drv->name, avalon->device_id, err, amount,
  623. BITBURNER_VERSION1, BITBURNER_VERSION2, BITBURNER_VERSION3);
  624. info->version1 = BITBURNER_VERSION1;
  625. info->version2 = BITBURNER_VERSION2;
  626. info->version3 = BITBURNER_VERSION3;
  627. } else {
  628. info->version1 = buf[0];
  629. info->version2 = buf[1];
  630. info->version3 = buf[2];
  631. }
  632. }
  633. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  634. {
  635. int baud, miner_count, asic_count, timeout, frequency;
  636. int this_option_offset;
  637. struct avalon_info *info;
  638. struct cgpu_info *avalon;
  639. bool configured;
  640. int ret;
  641. avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
  642. baud = AVALON_IO_SPEED;
  643. miner_count = AVALON_DEFAULT_MINER_NUM;
  644. asic_count = AVALON_DEFAULT_ASIC_NUM;
  645. timeout = AVALON_DEFAULT_TIMEOUT;
  646. frequency = AVALON_DEFAULT_FREQUENCY;
  647. if (!usb_init(avalon, dev, found))
  648. goto shin;
  649. this_option_offset = usb_ident(avalon) == IDENT_BBF ? ++bbf_option_offset : ++option_offset;
  650. configured = get_options(this_option_offset, &baud, &miner_count,
  651. &asic_count, &timeout, &frequency,
  652. (usb_ident(avalon) == IDENT_BBF && opt_bitburner_fury_options != NULL) ? opt_bitburner_fury_options : opt_avalon_options);
  653. /* Even though this is an FTDI type chip, we want to do the parsing
  654. * all ourselves so set it to std usb type */
  655. avalon->usbdev->usb_type = USB_TYPE_STD;
  656. usb_set_pps(avalon, AVALON_USB_PACKETSIZE);
  657. usb_buffer_enable(avalon);
  658. /* We have a real Avalon! */
  659. avalon_initialise(avalon);
  660. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  661. if (unlikely(!(avalon->device_data)))
  662. quit(1, "Failed to calloc avalon_info data");
  663. info = avalon->device_data;
  664. if (configured) {
  665. info->baud = baud;
  666. info->miner_count = miner_count;
  667. info->asic_count = asic_count;
  668. info->timeout = timeout;
  669. info->frequency = frequency;
  670. } else {
  671. info->baud = AVALON_IO_SPEED;
  672. info->asic_count = AVALON_DEFAULT_ASIC_NUM;
  673. switch (usb_ident(avalon)) {
  674. case IDENT_BTB:
  675. info->miner_count = BITBURNER_XX_DEFAULT_MINER_NUM;
  676. info->timeout = AVALON_DEFAULT_TIMEOUT;
  677. info->frequency = AVALON_DEFAULT_FREQUENCY;
  678. break;
  679. case IDENT_BBF:
  680. info->miner_count = BITBURNER_FURY_DEFAULT_MINER_NUM;
  681. info->timeout = BITBURNER_FURY_DEFAULT_TIMEOUT;
  682. info->frequency = BITBURNER_FURY_DEFAULT_FREQUENCY;
  683. break;
  684. default:
  685. info->miner_count = AVALON_DEFAULT_MINER_NUM;
  686. info->timeout = AVALON_DEFAULT_TIMEOUT;
  687. info->frequency = AVALON_DEFAULT_FREQUENCY;
  688. }
  689. }
  690. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  691. info->temp_max = 0;
  692. /* This is for check the temp/fan every 3~4s */
  693. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  694. if (info->temp_history_count <= 0)
  695. info->temp_history_count = 1;
  696. info->temp_history_index = 0;
  697. info->temp_sum = 0;
  698. info->temp_old = 0;
  699. if (!add_cgpu(avalon))
  700. goto unshin;
  701. ret = avalon_reset(avalon, true);
  702. if (ret && !configured)
  703. goto unshin;
  704. update_usb_stats(avalon);
  705. avalon_idle(avalon, info);
  706. applog(LOG_DEBUG, "Avalon Detected: %s "
  707. "(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  708. avalon->device_path, info->miner_count, info->asic_count, info->timeout,
  709. info->frequency);
  710. if (usb_ident(avalon) == IDENT_BTB) {
  711. if (opt_bitburner_core_voltage < BITBURNER_MIN_COREMV ||
  712. opt_bitburner_core_voltage > BITBURNER_MAX_COREMV) {
  713. quit(1, "Invalid bitburner-voltage %d must be %dmv - %dmv",
  714. opt_bitburner_core_voltage,
  715. BITBURNER_MIN_COREMV,
  716. BITBURNER_MAX_COREMV);
  717. } else
  718. bitburner_set_core_voltage(avalon, opt_bitburner_core_voltage);
  719. } else if (usb_ident(avalon) == IDENT_BBF) {
  720. if (opt_bitburner_fury_core_voltage < BITBURNER_FURY_MIN_COREMV ||
  721. opt_bitburner_fury_core_voltage > BITBURNER_FURY_MAX_COREMV) {
  722. quit(1, "Invalid bitburner-fury-voltage %d must be %dmv - %dmv",
  723. opt_bitburner_fury_core_voltage,
  724. BITBURNER_FURY_MIN_COREMV,
  725. BITBURNER_FURY_MAX_COREMV);
  726. } else
  727. bitburner_set_core_voltage(avalon, opt_bitburner_fury_core_voltage);
  728. }
  729. if (is_bitburner(avalon)) {
  730. bitburner_get_version(avalon);
  731. }
  732. return true;
  733. unshin:
  734. usb_uninit(avalon);
  735. shin:
  736. free(avalon->device_data);
  737. avalon->device_data = NULL;
  738. avalon = usb_free_cgpu(avalon);
  739. return false;
  740. }
  741. static void avalon_detect(bool __maybe_unused hotplug)
  742. {
  743. usb_detect(&avalon_drv, avalon_detect_one);
  744. }
  745. static void avalon_init(struct cgpu_info *avalon)
  746. {
  747. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  748. }
  749. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  750. {
  751. return clone_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  752. (char *)ar->data, 64, 12);
  753. }
  754. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  755. struct avalon_result *ar);
  756. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  757. {
  758. applog(LOG_INFO, "%s%d: No matching work - HW error",
  759. thr->cgpu->drv->name, thr->cgpu->device_id);
  760. inc_hw_errors(thr);
  761. info->no_matching_work++;
  762. }
  763. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  764. struct thr_info *thr, char *buf, int *offset)
  765. {
  766. int i, spare = *offset - AVALON_READ_SIZE;
  767. bool found = false;
  768. for (i = 0; i <= spare; i++) {
  769. struct avalon_result *ar;
  770. struct work *work;
  771. ar = (struct avalon_result *)&buf[i];
  772. work = avalon_valid_result(avalon, ar);
  773. if (work) {
  774. bool gettemp = false;
  775. found = true;
  776. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  777. mutex_lock(&info->lock);
  778. if (!info->nonces++)
  779. gettemp = true;
  780. info->auto_nonces++;
  781. mutex_unlock(&info->lock);
  782. } else if (opt_avalon_auto) {
  783. mutex_lock(&info->lock);
  784. info->auto_hw++;
  785. mutex_unlock(&info->lock);
  786. }
  787. free_work(work);
  788. if (gettemp)
  789. avalon_update_temps(avalon, info, ar);
  790. break;
  791. }
  792. }
  793. if (!found) {
  794. spare = *offset - AVALON_READ_SIZE;
  795. /* We are buffering and haven't accumulated one more corrupt
  796. * work result. */
  797. if (spare < (int)AVALON_READ_SIZE)
  798. return;
  799. avalon_inc_nvw(info, thr);
  800. } else {
  801. spare = AVALON_READ_SIZE + i;
  802. if (i) {
  803. if (i >= (int)AVALON_READ_SIZE)
  804. avalon_inc_nvw(info, thr);
  805. else
  806. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  807. }
  808. }
  809. *offset -= spare;
  810. memmove(buf, buf + spare, *offset);
  811. }
  812. static void avalon_running_reset(struct cgpu_info *avalon,
  813. struct avalon_info *info)
  814. {
  815. avalon_reset(avalon, false);
  816. avalon_idle(avalon, info);
  817. avalon->results = 0;
  818. info->reset = false;
  819. }
  820. static void *avalon_get_results(void *userdata)
  821. {
  822. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  823. struct avalon_info *info = avalon->device_data;
  824. const int rsize = AVALON_FTDI_READSIZE;
  825. char readbuf[AVALON_READBUF_SIZE];
  826. struct thr_info *thr = info->thr;
  827. cgtimer_t ts_start;
  828. int offset = 0, ret = 0;
  829. char threadname[24];
  830. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  831. RenameThread(threadname);
  832. cgsleep_prepare_r(&ts_start);
  833. while (likely(!avalon->shutdown)) {
  834. unsigned char buf[rsize];
  835. if (offset >= (int)AVALON_READ_SIZE)
  836. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  837. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  838. /* This should never happen */
  839. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  840. offset = 0;
  841. }
  842. if (unlikely(info->reset)) {
  843. avalon_running_reset(avalon, info);
  844. /* Discard anything in the buffer */
  845. offset = 0;
  846. }
  847. /* As the usb read returns after just 1ms, sleep long enough
  848. * to leave the interface idle for writes to occur, but do not
  849. * sleep if we have been receiving data, and we do not yet have
  850. * a full result as more may be coming. */
  851. if (ret < 1 || offset == 0)
  852. cgsleep_ms_r(&ts_start, AVALON_READ_TIMEOUT);
  853. cgsleep_prepare_r(&ts_start);
  854. ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
  855. C_AVALON_READ);
  856. if (ret < 1)
  857. continue;
  858. if (opt_debug) {
  859. applog(LOG_DEBUG, "Avalon: get:");
  860. hexdump((uint8_t *)buf, ret);
  861. }
  862. memcpy(&readbuf[offset], &buf, ret);
  863. offset += ret;
  864. }
  865. return NULL;
  866. }
  867. static void avalon_rotate_array(struct cgpu_info *avalon)
  868. {
  869. avalon->queued = 0;
  870. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  871. avalon->work_array = 0;
  872. }
  873. static void bitburner_rotate_array(struct cgpu_info *avalon)
  874. {
  875. avalon->queued = 0;
  876. if (++avalon->work_array >= BITBURNER_ARRAY_SIZE)
  877. avalon->work_array = 0;
  878. }
  879. static void avalon_set_timeout(struct avalon_info *info)
  880. {
  881. info->timeout = avalon_calc_timeout(info->frequency);
  882. }
  883. static void avalon_set_freq(struct cgpu_info *avalon, int frequency)
  884. {
  885. struct avalon_info *info = avalon->device_data;
  886. info->frequency = frequency;
  887. if (info->frequency > opt_avalon_freq_max)
  888. info->frequency = opt_avalon_freq_max;
  889. if (info->frequency < opt_avalon_freq_min)
  890. info->frequency = opt_avalon_freq_min;
  891. avalon_set_timeout(info);
  892. applog(LOG_WARNING, "%s%i: Set frequency to %d, timeout %d",
  893. avalon->drv->name, avalon->device_id,
  894. info->frequency, info->timeout);
  895. }
  896. static void avalon_inc_freq(struct avalon_info *info)
  897. {
  898. info->frequency += 2;
  899. if (info->frequency > opt_avalon_freq_max)
  900. info->frequency = opt_avalon_freq_max;
  901. avalon_set_timeout(info);
  902. applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
  903. info->frequency, info->timeout);
  904. }
  905. static void avalon_dec_freq(struct avalon_info *info)
  906. {
  907. info->frequency -= 1;
  908. if (info->frequency < opt_avalon_freq_min)
  909. info->frequency = opt_avalon_freq_min;
  910. avalon_set_timeout(info);
  911. applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
  912. info->frequency, info->timeout);
  913. }
  914. static void avalon_reset_auto(struct avalon_info *info)
  915. {
  916. info->auto_queued =
  917. info->auto_nonces =
  918. info->auto_hw = 0;
  919. }
  920. static void avalon_adjust_freq(struct avalon_info *info, struct cgpu_info *avalon)
  921. {
  922. if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
  923. mutex_lock(&info->lock);
  924. if (!info->optimal) {
  925. if (info->fan_pwm >= opt_avalon_fan_max) {
  926. applog(LOG_WARNING,
  927. "%s%i: Above optimal temperature, throttling",
  928. avalon->drv->name, avalon->device_id);
  929. avalon_dec_freq(info);
  930. }
  931. } else if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
  932. info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
  933. int total = info->auto_nonces + info->auto_hw;
  934. /* Try to keep hw errors < 2% */
  935. if (info->auto_hw * 100 < total)
  936. avalon_inc_freq(info);
  937. else if (info->auto_hw * 66 > total)
  938. avalon_dec_freq(info);
  939. }
  940. avalon_reset_auto(info);
  941. mutex_unlock(&info->lock);
  942. }
  943. }
  944. static void *avalon_send_tasks(void *userdata)
  945. {
  946. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  947. struct avalon_info *info = avalon->device_data;
  948. const int avalon_get_work_count = info->miner_count;
  949. char threadname[24];
  950. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  951. RenameThread(threadname);
  952. while (likely(!avalon->shutdown)) {
  953. int start_count, end_count, i, j, ret;
  954. cgtimer_t ts_start;
  955. struct avalon_task at;
  956. bool idled = false;
  957. int64_t us_timeout;
  958. while (avalon_buffer_full(avalon))
  959. cgsleep_ms(40);
  960. avalon_adjust_freq(info, avalon);
  961. /* A full nonce range */
  962. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  963. cgsleep_prepare_r(&ts_start);
  964. mutex_lock(&info->qlock);
  965. start_count = avalon->work_array * avalon_get_work_count;
  966. end_count = start_count + avalon_get_work_count;
  967. for (i = start_count, j = 0; i < end_count; i++, j++) {
  968. if (avalon_buffer_full(avalon)) {
  969. applog(LOG_INFO,
  970. "%s%i: Buffer full after only %d of %d work queued",
  971. avalon->drv->name, avalon->device_id, j, avalon_get_work_count);
  972. break;
  973. }
  974. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  975. avalon_init_task(&at, 0, 0, info->fan_pwm,
  976. info->timeout, info->asic_count,
  977. info->miner_count, 1, 0, info->frequency);
  978. avalon_create_task(&at, avalon->works[i]);
  979. info->auto_queued++;
  980. } else {
  981. int idle_freq = info->frequency;
  982. if (!info->idle++)
  983. idled = true;
  984. if (unlikely(info->overheat && opt_avalon_auto))
  985. idle_freq = AVALON_MIN_FREQUENCY;
  986. avalon_init_task(&at, 0, 0, info->fan_pwm,
  987. info->timeout, info->asic_count,
  988. info->miner_count, 1, 1, idle_freq);
  989. /* Reset the auto_queued count if we end up
  990. * idling any miners. */
  991. avalon_reset_auto(info);
  992. }
  993. ret = avalon_send_task(&at, avalon);
  994. if (unlikely(ret == AVA_SEND_ERROR)) {
  995. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  996. avalon->drv->name, avalon->device_id);
  997. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  998. info->reset = true;
  999. break;
  1000. }
  1001. }
  1002. avalon_rotate_array(avalon);
  1003. pthread_cond_signal(&info->qcond);
  1004. mutex_unlock(&info->qlock);
  1005. if (unlikely(idled)) {
  1006. applog(LOG_WARNING, "%s%i: Idled %d miners",
  1007. avalon->drv->name, avalon->device_id, idled);
  1008. }
  1009. /* Sleep how long it would take to complete a full nonce range
  1010. * at the current frequency using the clock_nanosleep function
  1011. * timed from before we started loading new work so it will
  1012. * fall short of the full duration. */
  1013. cgsleep_us_r(&ts_start, us_timeout);
  1014. }
  1015. return NULL;
  1016. }
  1017. static void *bitburner_send_tasks(void *userdata)
  1018. {
  1019. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  1020. struct avalon_info *info = avalon->device_data;
  1021. const int avalon_get_work_count = info->miner_count;
  1022. char threadname[24];
  1023. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  1024. RenameThread(threadname);
  1025. while (likely(!avalon->shutdown)) {
  1026. int start_count, end_count, i, j, ret;
  1027. struct avalon_task at;
  1028. bool idled = false;
  1029. while (avalon_buffer_full(avalon))
  1030. cgsleep_ms(40);
  1031. avalon_adjust_freq(info, avalon);
  1032. /* Give other threads a chance to acquire qlock. */
  1033. i = 0;
  1034. do {
  1035. cgsleep_ms(40);
  1036. } while (!avalon->shutdown && i++ < 15
  1037. && avalon->queued < avalon_get_work_count);
  1038. mutex_lock(&info->qlock);
  1039. start_count = avalon->work_array * avalon_get_work_count;
  1040. end_count = start_count + avalon_get_work_count;
  1041. for (i = start_count, j = 0; i < end_count; i++, j++) {
  1042. while (avalon_buffer_full(avalon))
  1043. cgsleep_ms(40);
  1044. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  1045. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1046. info->timeout, info->asic_count,
  1047. info->miner_count, 1, 0, info->frequency);
  1048. avalon_create_task(&at, avalon->works[i]);
  1049. info->auto_queued++;
  1050. } else {
  1051. int idle_freq = info->frequency;
  1052. if (!info->idle++)
  1053. idled = true;
  1054. if (unlikely(info->overheat && opt_avalon_auto))
  1055. idle_freq = AVALON_MIN_FREQUENCY;
  1056. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1057. info->timeout, info->asic_count,
  1058. info->miner_count, 1, 1, idle_freq);
  1059. /* Reset the auto_queued count if we end up
  1060. * idling any miners. */
  1061. avalon_reset_auto(info);
  1062. }
  1063. ret = bitburner_send_task(&at, avalon);
  1064. if (unlikely(ret == AVA_SEND_ERROR)) {
  1065. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  1066. avalon->drv->name, avalon->device_id);
  1067. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  1068. info->reset = true;
  1069. break;
  1070. }
  1071. }
  1072. bitburner_rotate_array(avalon);
  1073. pthread_cond_signal(&info->qcond);
  1074. mutex_unlock(&info->qlock);
  1075. if (unlikely(idled)) {
  1076. applog(LOG_WARNING, "%s%i: Idled %d miners",
  1077. avalon->drv->name, avalon->device_id, idled);
  1078. }
  1079. }
  1080. return NULL;
  1081. }
  1082. static bool avalon_prepare(struct thr_info *thr)
  1083. {
  1084. struct cgpu_info *avalon = thr->cgpu;
  1085. struct avalon_info *info = avalon->device_data;
  1086. int array_size = AVALON_ARRAY_SIZE;
  1087. void *(*write_thread_fn)(void *) = avalon_send_tasks;
  1088. if (is_bitburner(avalon)) {
  1089. array_size = BITBURNER_ARRAY_SIZE;
  1090. write_thread_fn = bitburner_send_tasks;
  1091. }
  1092. free(avalon->works);
  1093. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  1094. array_size);
  1095. if (!avalon->works)
  1096. quit(1, "Failed to calloc avalon works in avalon_prepare");
  1097. info->thr = thr;
  1098. mutex_init(&info->lock);
  1099. mutex_init(&info->qlock);
  1100. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  1101. quit(1, "Failed to pthread_cond_init avalon qcond");
  1102. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  1103. quit(1, "Failed to create avalon read_thr");
  1104. if (pthread_create(&info->write_thr, NULL, write_thread_fn, (void *)avalon))
  1105. quit(1, "Failed to create avalon write_thr");
  1106. avalon_init(avalon);
  1107. return true;
  1108. }
  1109. static void do_avalon_close(struct thr_info *thr)
  1110. {
  1111. struct cgpu_info *avalon = thr->cgpu;
  1112. struct avalon_info *info = avalon->device_data;
  1113. pthread_join(info->read_thr, NULL);
  1114. pthread_join(info->write_thr, NULL);
  1115. avalon_running_reset(avalon, info);
  1116. info->no_matching_work = 0;
  1117. }
  1118. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  1119. {
  1120. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  1121. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  1122. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  1123. info->temp0 = ar->temp0;
  1124. info->temp1 = ar->temp1;
  1125. info->temp2 = ar->temp2;
  1126. if (ar->temp0 & 0x80) {
  1127. ar->temp0 &= 0x7f;
  1128. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  1129. }
  1130. if (ar->temp1 & 0x80) {
  1131. ar->temp1 &= 0x7f;
  1132. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  1133. }
  1134. if (ar->temp2 & 0x80) {
  1135. ar->temp2 &= 0x7f;
  1136. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  1137. }
  1138. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  1139. if (info->temp0 > info->temp_max)
  1140. info->temp_max = info->temp0;
  1141. if (info->temp1 > info->temp_max)
  1142. info->temp_max = info->temp1;
  1143. if (info->temp2 > info->temp_max)
  1144. info->temp_max = info->temp2;
  1145. }
  1146. static void temp_rise(struct avalon_info *info, int temp)
  1147. {
  1148. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
  1149. info->fan_pwm = AVALON_PWM_MAX;
  1150. return;
  1151. }
  1152. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
  1153. info->fan_pwm += 10;
  1154. else if (temp > opt_avalon_temp)
  1155. info->fan_pwm += 5;
  1156. else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1157. info->fan_pwm += 1;
  1158. else
  1159. return;
  1160. if (info->fan_pwm > opt_avalon_fan_max)
  1161. info->fan_pwm = opt_avalon_fan_max;
  1162. }
  1163. static void temp_drop(struct avalon_info *info, int temp)
  1164. {
  1165. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
  1166. info->fan_pwm = opt_avalon_fan_min;
  1167. return;
  1168. }
  1169. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
  1170. info->fan_pwm -= 10;
  1171. else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1172. info->fan_pwm -= 5;
  1173. else if (temp < opt_avalon_temp)
  1174. info->fan_pwm -= 1;
  1175. if (info->fan_pwm < opt_avalon_fan_min)
  1176. info->fan_pwm = opt_avalon_fan_min;
  1177. }
  1178. static inline void adjust_fan(struct avalon_info *info)
  1179. {
  1180. int temp_new;
  1181. temp_new = info->temp_sum / info->temp_history_count;
  1182. if (temp_new > info->temp_old)
  1183. temp_rise(info, temp_new);
  1184. else if (temp_new < info->temp_old)
  1185. temp_drop(info, temp_new);
  1186. else {
  1187. /* temp_new == info->temp_old */
  1188. if (temp_new > opt_avalon_temp)
  1189. temp_rise(info, temp_new);
  1190. else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1191. temp_drop(info, temp_new);
  1192. }
  1193. info->temp_old = temp_new;
  1194. if (info->temp_old <= opt_avalon_temp)
  1195. info->optimal = true;
  1196. else
  1197. info->optimal = false;
  1198. }
  1199. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  1200. struct avalon_result *ar)
  1201. {
  1202. record_temp_fan(info, ar, &(avalon->temp));
  1203. applog(LOG_INFO,
  1204. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  1205. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  1206. info->fan0, info->fan1, info->fan2,
  1207. info->temp0, info->temp1, info->temp2, info->temp_max);
  1208. info->temp_history_index++;
  1209. info->temp_sum += avalon->temp;
  1210. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  1211. info->temp_history_index, info->temp_history_count, info->temp_old);
  1212. if (is_bitburner(avalon)) {
  1213. info->core_voltage = bitburner_get_core_voltage(avalon);
  1214. }
  1215. if (info->temp_history_index == info->temp_history_count) {
  1216. adjust_fan(info);
  1217. info->temp_history_index = 0;
  1218. info->temp_sum = 0;
  1219. }
  1220. if (unlikely(info->temp_old >= opt_avalon_overheat)) {
  1221. applog(LOG_WARNING, "%s%d overheat! Idling", avalon->drv->name, avalon->device_id);
  1222. info->overheat = true;
  1223. } else if (info->overheat && info->temp_old <= opt_avalon_temp) {
  1224. applog(LOG_WARNING, "%s%d cooled, restarting", avalon->drv->name, avalon->device_id);
  1225. info->overheat = false;
  1226. }
  1227. }
  1228. static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon)
  1229. {
  1230. struct avalon_info *info = avalon->device_data;
  1231. int lowfan = 10000;
  1232. if (is_bitburner(avalon)) {
  1233. int temp = info->temp0;
  1234. if (info->temp2 > temp)
  1235. temp = info->temp2;
  1236. if (temp > 99)
  1237. temp = 99;
  1238. if (temp < 0)
  1239. temp = 0;
  1240. tailsprintf(buf, bufsiz, "%2dC %3d %4dmV | ", temp, info->frequency, info->core_voltage);
  1241. } else {
  1242. /* Find the lowest fan speed of the ASIC cooling fans. */
  1243. if (info->fan1 >= 0 && info->fan1 < lowfan)
  1244. lowfan = info->fan1;
  1245. if (info->fan2 >= 0 && info->fan2 < lowfan)
  1246. lowfan = info->fan2;
  1247. tailsprintf(buf, bufsiz, "%2dC/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  1248. }
  1249. }
  1250. /* We use a replacement algorithm to only remove references to work done from
  1251. * the buffer when we need the extra space for new work. */
  1252. static bool avalon_fill(struct cgpu_info *avalon)
  1253. {
  1254. struct avalon_info *info = avalon->device_data;
  1255. int subid, slot, mc;
  1256. struct work *work;
  1257. bool ret = true;
  1258. mc = info->miner_count;
  1259. mutex_lock(&info->qlock);
  1260. if (avalon->queued >= mc)
  1261. goto out_unlock;
  1262. work = get_queued(avalon);
  1263. if (unlikely(!work)) {
  1264. ret = false;
  1265. goto out_unlock;
  1266. }
  1267. subid = avalon->queued++;
  1268. work->subid = subid;
  1269. slot = avalon->work_array * mc + subid;
  1270. if (likely(avalon->works[slot]))
  1271. work_completed(avalon, avalon->works[slot]);
  1272. avalon->works[slot] = work;
  1273. if (avalon->queued < mc)
  1274. ret = false;
  1275. out_unlock:
  1276. mutex_unlock(&info->qlock);
  1277. return ret;
  1278. }
  1279. static int64_t avalon_scanhash(struct thr_info *thr)
  1280. {
  1281. struct cgpu_info *avalon = thr->cgpu;
  1282. struct avalon_info *info = avalon->device_data;
  1283. const int miner_count = info->miner_count;
  1284. struct timeval now, then, tdiff;
  1285. int64_t hash_count, us_timeout;
  1286. struct timespec abstime;
  1287. /* Half nonce range */
  1288. us_timeout = 0x80000000ll / info->asic_count / info->frequency;
  1289. us_to_timeval(&tdiff, us_timeout);
  1290. cgtime(&now);
  1291. timeradd(&now, &tdiff, &then);
  1292. timeval_to_spec(&abstime, &then);
  1293. /* Wait until avalon_send_tasks signals us that it has completed
  1294. * sending its work or a full nonce range timeout has occurred */
  1295. mutex_lock(&info->qlock);
  1296. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  1297. mutex_unlock(&info->qlock);
  1298. mutex_lock(&info->lock);
  1299. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  1300. avalon->results += info->nonces + info->idle;
  1301. if (avalon->results > miner_count)
  1302. avalon->results = miner_count;
  1303. if (!info->reset)
  1304. avalon->results--;
  1305. info->nonces = info->idle = 0;
  1306. mutex_unlock(&info->lock);
  1307. /* Check for nothing but consecutive bad results or consistently less
  1308. * results than we should be getting and reset the FPGA if necessary */
  1309. if (!is_bitburner(avalon)) {
  1310. if (avalon->results < -miner_count && !info->reset) {
  1311. applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
  1312. avalon->drv->name, avalon->device_id);
  1313. info->reset = true;
  1314. }
  1315. }
  1316. if (unlikely(avalon->usbinfo.nodev)) {
  1317. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  1318. avalon->drv->name, avalon->device_id);
  1319. avalon->shutdown = true;
  1320. }
  1321. /* This hashmeter is just a utility counter based on returned shares */
  1322. return hash_count;
  1323. }
  1324. static void avalon_flush_work(struct cgpu_info *avalon)
  1325. {
  1326. struct avalon_info *info = avalon->device_data;
  1327. mutex_lock(&info->qlock);
  1328. /* Will overwrite any work queued */
  1329. avalon->queued = 0;
  1330. pthread_cond_signal(&info->qcond);
  1331. mutex_unlock(&info->qlock);
  1332. }
  1333. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  1334. {
  1335. struct api_data *root = NULL;
  1336. struct avalon_info *info = cgpu->device_data;
  1337. char buf[64];
  1338. int i;
  1339. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1340. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1341. root = api_add_int(root, "baud", &(info->baud), false);
  1342. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  1343. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  1344. root = api_add_int(root, "timeout", &(info->timeout), false);
  1345. root = api_add_int(root, "frequency", &(info->frequency), false);
  1346. root = api_add_int(root, "fan1", &(info->fan0), false);
  1347. root = api_add_int(root, "fan2", &(info->fan1), false);
  1348. root = api_add_int(root, "fan3", &(info->fan2), false);
  1349. root = api_add_int(root, "temp1", &(info->temp0), false);
  1350. root = api_add_int(root, "temp2", &(info->temp1), false);
  1351. root = api_add_int(root, "temp3", &(info->temp2), false);
  1352. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1353. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  1354. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1355. for (i = 0; i < info->miner_count; i++) {
  1356. char mcw[24];
  1357. sprintf(mcw, "match_work_count%d", i + 1);
  1358. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1359. }
  1360. if (is_bitburner(cgpu)) {
  1361. root = api_add_int(root, "core_voltage", &(info->core_voltage), false);
  1362. snprintf(buf, sizeof(buf), "%"PRIu8".%"PRIu8".%"PRIu8,
  1363. info->version1, info->version2, info->version3);
  1364. root = api_add_string(root, "version", buf, true);
  1365. }
  1366. root = api_add_uint32(root, "Controller Version", &(info->ctlr_ver), false);
  1367. return root;
  1368. }
  1369. static void avalon_shutdown(struct thr_info *thr)
  1370. {
  1371. do_avalon_close(thr);
  1372. }
  1373. static char *avalon_set_device(struct cgpu_info *avalon, char *option, char *setting, char *replybuf)
  1374. {
  1375. int val;
  1376. if (strcasecmp(option, "help") == 0) {
  1377. sprintf(replybuf, "freq: range %d-%d millivolts: range %d-%d",
  1378. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY,
  1379. BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1380. return replybuf;
  1381. }
  1382. if (strcasecmp(option, "millivolts") == 0 || strcasecmp(option, "mv") == 0) {
  1383. if (!is_bitburner(avalon)) {
  1384. sprintf(replybuf, "%s cannot set millivolts", avalon->drv->name);
  1385. return replybuf;
  1386. }
  1387. if (!setting || !*setting) {
  1388. sprintf(replybuf, "missing millivolts setting");
  1389. return replybuf;
  1390. }
  1391. val = atoi(setting);
  1392. if (val < BITBURNER_MIN_COREMV || val > BITBURNER_MAX_COREMV) {
  1393. sprintf(replybuf, "invalid millivolts: '%s' valid range %d-%d",
  1394. setting, BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1395. return replybuf;
  1396. }
  1397. if (bitburner_set_core_voltage(avalon, val))
  1398. return NULL;
  1399. else {
  1400. sprintf(replybuf, "Set millivolts failed");
  1401. return replybuf;
  1402. }
  1403. }
  1404. if (strcasecmp(option, "freq") == 0) {
  1405. if (!setting || !*setting) {
  1406. sprintf(replybuf, "missing freq setting");
  1407. return replybuf;
  1408. }
  1409. val = atoi(setting);
  1410. if (val < AVALON_MIN_FREQUENCY || val > AVALON_MAX_FREQUENCY) {
  1411. sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
  1412. setting, AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  1413. return replybuf;
  1414. }
  1415. avalon_set_freq(avalon, val);
  1416. return NULL;
  1417. }
  1418. sprintf(replybuf, "Unknown option: %s", option);
  1419. return replybuf;
  1420. }
  1421. struct device_drv avalon_drv = {
  1422. .drv_id = DRIVER_avalon,
  1423. .dname = "avalon",
  1424. .name = "AVA",
  1425. .drv_detect = avalon_detect,
  1426. .thread_prepare = avalon_prepare,
  1427. .hash_work = hash_queued_work,
  1428. .queue_full = avalon_fill,
  1429. .scanwork = avalon_scanhash,
  1430. .flush_work = avalon_flush_work,
  1431. .get_api_stats = avalon_api_stats,
  1432. .get_statline_before = get_avalon_statline_before,
  1433. .set_device = avalon_set_device,
  1434. .reinit_device = avalon_init,
  1435. .thread_shutdown = avalon_shutdown,
  1436. };