driver-avalon.c.bak 45 KB

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