driver-avalon.c 38 KB

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