driver-avalon.c 41 KB

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