driver-avalon.c 45 KB

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