driver-avalon.c 37 KB

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