driver-avalon.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * Copyright 2012-2013 Xiangfu
  3. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  4. * Copyright 2012-2014 Luke Dashjr
  5. * Copyright 2012-2013 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 <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <dirent.h>
  21. #include <unistd.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 "deviceapi.h"
  36. #include "miner.h"
  37. #include "driver-avalon.h"
  38. #include "logging.h"
  39. #include "lowlevel.h"
  40. #include "lowl-vcom.h"
  41. #include "util.h"
  42. BFG_REGISTER_DRIVER(avalon_drv)
  43. static int avalon_init_task(struct avalon_task *at,
  44. uint8_t reset, uint8_t ff, uint8_t fan,
  45. uint8_t timeout, uint8_t asic_num,
  46. uint8_t miner_num, uint8_t nonce_elf,
  47. uint8_t gate_miner, int frequency)
  48. {
  49. uint8_t *buf;
  50. static bool first = true;
  51. if (unlikely(!at))
  52. return -1;
  53. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  54. return -1;
  55. memset(at, 0, sizeof(struct avalon_task));
  56. if (unlikely(reset)) {
  57. at->reset = 1;
  58. at->fan_eft = 1;
  59. at->timer_eft = 1;
  60. first = true;
  61. }
  62. at->flush_fifo = (ff ? 1 : 0);
  63. at->fan_eft = (fan ? 1 : 0);
  64. if (unlikely(first && !at->reset)) {
  65. at->fan_eft = 1;
  66. at->timer_eft = 1;
  67. first = false;
  68. }
  69. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  70. at->timeout_data = timeout;
  71. at->asic_num = asic_num;
  72. at->miner_num = miner_num;
  73. at->nonce_elf = nonce_elf;
  74. at->gate_miner_elf = 1;
  75. at->asic_pll = 1;
  76. if (unlikely(gate_miner)) {
  77. at-> gate_miner = 1;
  78. at->asic_pll = 0;
  79. }
  80. buf = (uint8_t *)at;
  81. buf[5] = 0x00;
  82. buf[8] = 0x74;
  83. buf[9] = 0x01;
  84. buf[10] = 0x00;
  85. buf[11] = 0x00;
  86. switch (frequency) {
  87. case 256:
  88. buf[6] = 0x03;
  89. buf[7] = 0x08;
  90. break;
  91. default:
  92. case 270:
  93. buf[6] = 0x73;
  94. buf[7] = 0x08;
  95. break;
  96. case 282:
  97. buf[6] = 0xd3;
  98. buf[7] = 0x08;
  99. break;
  100. case 300:
  101. buf[6] = 0x63;
  102. buf[7] = 0x09;
  103. break;
  104. case 325:
  105. buf[6] = 0x28;
  106. buf[7] = 0x0a;
  107. break;
  108. case 350:
  109. buf[6] = 0xf0;
  110. buf[7] = 0x0a;
  111. break;
  112. case 375:
  113. buf[6] = 0xb8;
  114. buf[7] = 0x0b;
  115. break;
  116. }
  117. return 0;
  118. }
  119. static inline void avalon_create_task(struct avalon_task *at,
  120. struct work *work)
  121. {
  122. memcpy(at->midstate, work->midstate, 32);
  123. memcpy(at->data, work->data + 64, 12);
  124. }
  125. static int avalon_send_task(int fd, const struct avalon_task *at,
  126. struct cgpu_info *avalon)
  127. {
  128. size_t ret;
  129. int full;
  130. struct timespec p;
  131. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  132. size_t nr_len;
  133. struct avalon_info *info;
  134. uint64_t delay = 32000000; /* Default 32ms for B19200 */
  135. uint32_t nonce_range;
  136. int i;
  137. if (at->nonce_elf)
  138. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  139. else
  140. nr_len = AVALON_WRITE_SIZE;
  141. memcpy(buf, at, AVALON_WRITE_SIZE);
  142. if (at->nonce_elf) {
  143. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  144. for (i = 0; i < at->asic_num; i++) {
  145. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  146. (i * nonce_range & 0xff000000) >> 24;
  147. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  148. (i * nonce_range & 0x00ff0000) >> 16;
  149. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  150. (i * nonce_range & 0x0000ff00) >> 8;
  151. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  152. (i * nonce_range & 0x000000ff) >> 0;
  153. }
  154. }
  155. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  156. uint8_t tt = 0;
  157. tt = (buf[0] & 0x0f) << 4;
  158. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  159. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  160. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  161. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  162. buf[0] = tt;
  163. tt = (buf[4] & 0x0f) << 4;
  164. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  165. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  166. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  167. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  168. buf[4] = tt;
  169. #endif
  170. if (likely(avalon)) {
  171. info = avalon->device_data;
  172. delay = nr_len * 10 * 1000000000ULL;
  173. delay = delay / info->baud;
  174. }
  175. if (at->reset)
  176. nr_len = 1;
  177. if (opt_debug) {
  178. char x[(nr_len * 2) + 1];
  179. bin2hex(x, buf, nr_len);
  180. applog(LOG_DEBUG, "Avalon: Sent(%u): %s", (unsigned int)nr_len, x);
  181. }
  182. ret = write(fd, buf, nr_len);
  183. if (unlikely(ret != nr_len))
  184. return AVA_SEND_ERROR;
  185. p.tv_sec = 0;
  186. p.tv_nsec = (long)delay + 4000000;
  187. nanosleep(&p, NULL);
  188. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld", p.tv_nsec);
  189. full = avalon_buffer_full(fd);
  190. applog(LOG_DEBUG, "Avalon: Sent: Buffer full: %s",
  191. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  192. if (unlikely(full == AVA_BUFFER_FULL))
  193. return AVA_SEND_BUFFER_FULL;
  194. return AVA_SEND_BUFFER_EMPTY;
  195. }
  196. static inline int avalon_gets(int fd, uint8_t *buf, int read_count,
  197. struct thr_info *thr, struct timeval *tv_finish)
  198. {
  199. ssize_t ret = 0;
  200. int rc = 0;
  201. int read_amount = AVALON_READ_SIZE;
  202. bool first = true;
  203. /* Read reply 1 byte at a time to get earliest tv_finish */
  204. while (true) {
  205. ret = read(fd, buf, 1);
  206. if (ret < 0)
  207. {
  208. applog(LOG_ERR, "Avalon: Error on read in avalon_gets: %s", bfg_strerror(errno, BST_ERRNO));
  209. return AVA_GETS_ERROR;
  210. }
  211. if (first && likely(tv_finish))
  212. cgtime(tv_finish);
  213. if (ret >= read_amount)
  214. return AVA_GETS_OK;
  215. if (ret > 0) {
  216. buf += ret;
  217. read_amount -= ret;
  218. first = false;
  219. continue;
  220. }
  221. if (thr && thr->work_restart) {
  222. if (opt_debug) {
  223. applog(LOG_WARNING,
  224. "Avalon: Work restart at %.2f seconds",
  225. (float)(rc)/(float)AVALON_TIME_FACTOR);
  226. }
  227. return AVA_GETS_RESTART;
  228. }
  229. rc++;
  230. if (rc >= read_count) {
  231. if (opt_debug) {
  232. applog(LOG_WARNING,
  233. "Avalon: No data in %.2f seconds",
  234. (float)rc/(float)AVALON_TIME_FACTOR);
  235. }
  236. return AVA_GETS_TIMEOUT;
  237. }
  238. }
  239. }
  240. static int avalon_get_result(int fd, struct avalon_result *ar,
  241. struct thr_info *thr, struct timeval *tv_finish)
  242. {
  243. struct cgpu_info *avalon;
  244. struct avalon_info *info;
  245. uint8_t result[AVALON_READ_SIZE];
  246. int ret, read_count;
  247. avalon = thr->cgpu;
  248. info = avalon->device_data;
  249. read_count = info->read_count;
  250. memset(result, 0, AVALON_READ_SIZE);
  251. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  252. if (ret == AVA_GETS_OK) {
  253. if (opt_debug) {
  254. char x[(AVALON_READ_SIZE * 2) + 1];
  255. bin2hex(x, result, AVALON_READ_SIZE);
  256. applog(LOG_DEBUG, "Avalon: get: %s", x);
  257. }
  258. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  259. }
  260. return ret;
  261. }
  262. static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
  263. uint32_t *nonce)
  264. {
  265. struct cgpu_info *avalon;
  266. struct avalon_info *info;
  267. struct work *work;
  268. avalon = thr->cgpu;
  269. if (unlikely(!avalon->works))
  270. return false;
  271. work = clone_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  272. (char *)ar->data, 64, 12);
  273. if (!work)
  274. return false;
  275. info = avalon->device_data;
  276. info->matching_work[work->subid]++;
  277. *nonce = htole32(ar->nonce);
  278. submit_nonce(thr, work, *nonce);
  279. free_work(work);
  280. return true;
  281. }
  282. static void avalon_get_reset(int fd, struct avalon_result *ar)
  283. {
  284. int ret;
  285. const int read_count = AVALON_RESET_FAULT_DECISECONDS * AVALON_TIME_FACTOR;
  286. memset(ar, 0, AVALON_READ_SIZE);
  287. ret = avalon_gets(fd, (uint8_t*)ar, read_count, NULL, NULL);
  288. if (ret == AVA_GETS_OK && opt_debug) {
  289. char x[(AVALON_READ_SIZE * 2) + 1];
  290. bin2hex(x, ar, AVALON_READ_SIZE);
  291. applog(LOG_DEBUG, "Avalon: get: %s", x);
  292. }
  293. }
  294. static int avalon_reset(int fd, struct avalon_result *ar)
  295. {
  296. struct avalon_task at;
  297. uint8_t *buf;
  298. int ret, i = 0;
  299. struct timespec p;
  300. avalon_init_task(&at, 1, 0,
  301. AVALON_DEFAULT_FAN_MAX_PWM,
  302. AVALON_DEFAULT_TIMEOUT,
  303. AVALON_DEFAULT_ASIC_NUM,
  304. AVALON_DEFAULT_MINER_NUM,
  305. 0, 0,
  306. AVALON_DEFAULT_FREQUENCY);
  307. ret = avalon_send_task(fd, &at, NULL);
  308. if (ret == AVA_SEND_ERROR)
  309. return 1;
  310. avalon_get_reset(fd, ar);
  311. buf = (uint8_t *)ar;
  312. /* Sometimes there is one extra 0 byte for some reason in the buffer,
  313. * so work around it. */
  314. if (buf[0] == 0)
  315. buf = (uint8_t *)(ar + 1);
  316. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  317. buf[2] == 0xAA && buf[3] == 0x55) {
  318. for (i = 4; i < 11; i++)
  319. if (buf[i] != 0)
  320. break;
  321. }
  322. p.tv_sec = 0;
  323. p.tv_nsec = AVALON_RESET_PITCH;
  324. nanosleep(&p, NULL);
  325. if (i != 11) {
  326. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  327. " (%d: %02x %02x %02x %02x)",
  328. i, buf[0], buf[1], buf[2], buf[3]);
  329. /* FIXME: return 1; */
  330. } else
  331. applog(LOG_WARNING, "Avalon: Reset succeeded");
  332. return 0;
  333. }
  334. static void avalon_idle(struct cgpu_info *avalon)
  335. {
  336. int i, ret;
  337. struct avalon_task at;
  338. int fd = avalon->device_fd;
  339. struct avalon_info *info = avalon->device_data;
  340. int avalon_get_work_count = info->miner_count;
  341. i = 0;
  342. while (true) {
  343. avalon_init_task(&at, 0, 0, info->fan_pwm,
  344. info->timeout, info->asic_count,
  345. info->miner_count, 1, 1, info->frequency);
  346. ret = avalon_send_task(fd, &at, avalon);
  347. if (unlikely(ret == AVA_SEND_ERROR ||
  348. (ret == AVA_SEND_BUFFER_EMPTY &&
  349. (i + 1 == avalon_get_work_count * 2)))) {
  350. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  351. return;
  352. }
  353. if (i + 1 == avalon_get_work_count * 2)
  354. break;
  355. if (ret == AVA_SEND_BUFFER_FULL)
  356. break;
  357. i++;
  358. }
  359. applog(LOG_ERR, "Avalon: Goto idle mode");
  360. }
  361. static
  362. const char *avalon_set_baud(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  363. {
  364. struct avalon_info * const info = proc->device_data;
  365. const int baud = atoi(newvalue);
  366. if (!valid_baud(baud))
  367. return "Invalid baud setting";
  368. info->baud = baud;
  369. return NULL;
  370. }
  371. static
  372. const char *avalon_set_miner_count(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  373. {
  374. struct avalon_info * const info = proc->device_data;
  375. const int miner_count = atoi(newvalue);
  376. if (miner_count <= 0 || miner_count > AVALON_DEFAULT_MINER_NUM)
  377. return "Invalid miner_count: must be 1 ~ " AVALON_DEFAULT_MINER_NUM_S;
  378. info->miner_count = miner_count;
  379. return NULL;
  380. }
  381. static
  382. const char *avalon_set_asic_count(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  383. {
  384. struct avalon_info * const info = proc->device_data;
  385. const int asic_count = atoi(newvalue);
  386. if (asic_count <= 0 || asic_count > AVALON_DEFAULT_ASIC_NUM)
  387. return "Invalid asic_count: must be 1 ~ " AVALON_DEFAULT_ASIC_NUM_S;
  388. info->asic_count = asic_count;
  389. return NULL;
  390. }
  391. static
  392. const char *avalon_set_timeout(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  393. {
  394. struct avalon_info * const info = proc->device_data;
  395. const int timeout = atoi(newvalue);
  396. if (timeout <= 0 || timeout > 0xff)
  397. return "Invalid timeout: must be 1 ~ 255";
  398. info->timeout = timeout;
  399. return NULL;
  400. }
  401. static
  402. const char *avalon_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  403. {
  404. struct avalon_info * const info = proc->device_data;
  405. const int clock = atoi(newvalue);
  406. switch (clock) {
  407. default:
  408. return "Invalid clock: must be 256/270/282/300/325/350/375";
  409. case 256:
  410. case 270:
  411. case 282:
  412. case 300:
  413. case 325:
  414. case 350:
  415. case 375:
  416. info->frequency = clock;
  417. }
  418. return NULL;
  419. }
  420. const struct bfg_set_device_definition avalon_set_device_funcs[] = {
  421. // NOTE: Order of parameters below is important for --avalon-options
  422. {"baud" , avalon_set_baud , "serial baud rate"},
  423. {"miner_count", avalon_set_miner_count, ""},
  424. {"asic_count" , avalon_set_asic_count , ""},
  425. {"timeout" , avalon_set_timeout , "how long the device will work on a work item before accepting new work"},
  426. {"clock" , avalon_set_clock , "clock speed: 256, 270, 282, 300, 325, 350, or 375"},
  427. {NULL},
  428. };
  429. #ifdef HAVE_CURSES
  430. static
  431. void avalon_wlogprint_status(struct cgpu_info * const proc)
  432. {
  433. struct avalon_info *info = proc->device_data;
  434. if (((info->temp0?1:0) + (info->temp1?1:0) + (info->temp2?1:0)) > 1)
  435. {
  436. wlogprint("Temperatures:");
  437. if (info->temp0) wlogprint(" %uC", (unsigned)info->temp0);
  438. if (info->temp1) wlogprint(" %uC", (unsigned)info->temp1);
  439. if (info->temp2) wlogprint(" %uC", (unsigned)info->temp2);
  440. }
  441. wlogprint("\n");
  442. wlogprint("Clock speed: %d\n", info->frequency);
  443. }
  444. static
  445. void avalon_tui_wlogprint_choices(struct cgpu_info * const proc)
  446. {
  447. wlogprint("[C]lock speed ");
  448. }
  449. static
  450. const char *avalon_tui_handle_choice(struct cgpu_info * const proc, const int input)
  451. {
  452. switch (input)
  453. {
  454. case 'c': case 'C':
  455. return proc_set_device_tui_wrapper(proc, NULL, avalon_set_clock, "Set clock speed (256, 270, 282, 300, 325, 350, or 375)", NULL);
  456. }
  457. return NULL;
  458. }
  459. #endif
  460. /* Non blocking clearing of anything in the buffer */
  461. static void avalon_clear_readbuf(int fd)
  462. {
  463. ssize_t ret;
  464. do {
  465. char buf[AVALON_FTDI_READSIZE];
  466. #ifndef WIN32
  467. struct timeval timeout;
  468. fd_set rd;
  469. timeout.tv_sec = timeout.tv_usec = 0;
  470. FD_ZERO(&rd);
  471. FD_SET((SOCKETTYPE)fd, &rd);
  472. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  473. if (ret > 0)
  474. #endif
  475. // Relies on serial timeout for Windows
  476. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  477. } while (ret > 0);
  478. }
  479. static
  480. void avalon_zero_stats(struct cgpu_info * const cgpu)
  481. {
  482. struct avalon_info *info = cgpu->device_data;
  483. info->temp_max = 0;
  484. info->no_matching_work = 0;
  485. for (int i = 0; i < info->miner_count; ++i)
  486. info->matching_work[i] = 0;
  487. }
  488. static bool avalon_detect_one(const char *devpath)
  489. {
  490. struct avalon_info *info;
  491. struct avalon_result ar;
  492. int fd, ret;
  493. struct cgpu_info *avalon;
  494. if (serial_claim(devpath, &avalon_drv))
  495. return false;
  496. info = malloc(sizeof(*info));
  497. if (unlikely(!info))
  498. applogr(false, LOG_ERR, "Failed to malloc avalon_info data");
  499. *info = (struct avalon_info){
  500. .baud = AVALON_IO_SPEED,
  501. .miner_count = AVALON_DEFAULT_MINER_NUM - 8,
  502. .asic_count = AVALON_DEFAULT_ASIC_NUM,
  503. .timeout = AVALON_DEFAULT_TIMEOUT,
  504. .frequency = AVALON_DEFAULT_FREQUENCY,
  505. };
  506. drv_set_defaults(&avalon_drv, avalon_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  507. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  508. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  509. devpath, info->baud, info->miner_count, info->asic_count, info->timeout, info->frequency);
  510. fd = avalon_open2(devpath, info->baud, true);
  511. if (unlikely(fd == -1)) {
  512. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  513. free(info);
  514. return false;
  515. }
  516. avalon_clear_readbuf(fd);
  517. /* We have a real Avalon! */
  518. avalon = calloc(1, sizeof(struct cgpu_info));
  519. avalon->drv = &avalon_drv;
  520. avalon->device_path = strdup(devpath);
  521. avalon->device_fd = fd;
  522. avalon->threads = AVALON_MINER_THREADS;
  523. avalon->set_device_funcs = avalon_set_device_funcs;
  524. add_cgpu(avalon);
  525. ret = avalon_reset(fd, &ar);
  526. if (ret) {
  527. ; /* FIXME: I think IT IS avalon and wait on reset;
  528. * avalon_close(fd);
  529. * free(info);
  530. * return false; */
  531. }
  532. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  533. devpath, avalon->device_id);
  534. avalon->device_data = info;
  535. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  536. AVALON_TIME_FACTOR) / (float)info->miner_count;
  537. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  538. avalon_zero_stats(avalon);
  539. /* This is for check the temp/fan every 3~4s */
  540. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  541. if (info->temp_history_count <= 0)
  542. info->temp_history_count = 1;
  543. info->temp_history_index = 0;
  544. info->temp_sum = 0;
  545. info->temp_old = 0;
  546. /* Set asic to idle mode after detect */
  547. avalon_idle(avalon);
  548. avalon->device_fd = -1;
  549. avalon_close(fd);
  550. return true;
  551. }
  552. static
  553. bool avalon_lowl_probe(const struct lowlevel_device_info * const info)
  554. {
  555. return vcom_lowl_probe_wrapper(info, avalon_detect_one);
  556. }
  557. static void __avalon_init(struct cgpu_info *avalon)
  558. {
  559. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  560. }
  561. static void avalon_init(struct cgpu_info *avalon)
  562. {
  563. struct avalon_info *info = avalon->device_data;
  564. struct avalon_result ar;
  565. int fd, ret;
  566. cgpu_set_defaults(avalon);
  567. avalon->set_device_funcs = NULL;
  568. avalon->device_fd = -1;
  569. fd = avalon_open(avalon->device_path, info->baud);
  570. if (unlikely(fd == -1)) {
  571. applog(LOG_ERR, "Avalon: Failed to open on %s",
  572. avalon->device_path);
  573. return;
  574. }
  575. ret = avalon_reset(fd, &ar);
  576. if (ret) {
  577. avalon_close(fd);
  578. return;
  579. }
  580. avalon->device_fd = fd;
  581. __avalon_init(avalon);
  582. }
  583. static bool avalon_prepare(struct thr_info *thr)
  584. {
  585. struct cgpu_info *avalon = thr->cgpu;
  586. struct avalon_info *info = avalon->device_data;
  587. free(avalon->works);
  588. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  589. AVALON_ARRAY_SIZE);
  590. if (!avalon->works)
  591. quithere(1, "Failed to calloc avalon works");
  592. if (avalon->device_fd == -1)
  593. avalon_init(avalon);
  594. else
  595. __avalon_init(avalon);
  596. avalon->status = LIFE_INIT2;
  597. return true;
  598. }
  599. static void avalon_free_work(struct thr_info *thr)
  600. {
  601. struct cgpu_info *avalon;
  602. struct avalon_info *info;
  603. struct work **works;
  604. int i;
  605. avalon = thr->cgpu;
  606. avalon->queued = 0;
  607. if (unlikely(!avalon->works))
  608. return;
  609. works = avalon->works;
  610. info = avalon->device_data;
  611. for (i = 0; i < info->miner_count * 4; i++) {
  612. if (works[i]) {
  613. work_completed(avalon, works[i]);
  614. works[i] = NULL;
  615. }
  616. }
  617. }
  618. static void do_avalon_close(struct thr_info *thr)
  619. {
  620. struct avalon_result ar;
  621. struct cgpu_info *avalon = thr->cgpu;
  622. struct avalon_info *info = avalon->device_data;
  623. avalon_free_work(thr);
  624. cgsleep_ms(1000);
  625. avalon_reset(avalon->device_fd, &ar);
  626. avalon_idle(avalon);
  627. avalon_close(avalon->device_fd);
  628. avalon->device_fd = -1;
  629. info->no_matching_work = 0;
  630. }
  631. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  632. {
  633. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  634. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  635. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  636. info->temp0 = ar->temp0;
  637. info->temp1 = ar->temp1;
  638. info->temp2 = ar->temp2;
  639. if (ar->temp0 & 0x80) {
  640. ar->temp0 &= 0x7f;
  641. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  642. }
  643. if (ar->temp1 & 0x80) {
  644. ar->temp1 &= 0x7f;
  645. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  646. }
  647. if (ar->temp2 & 0x80) {
  648. ar->temp2 &= 0x7f;
  649. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  650. }
  651. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  652. if (info->temp0 > info->temp_max)
  653. info->temp_max = info->temp0;
  654. if (info->temp1 > info->temp_max)
  655. info->temp_max = info->temp1;
  656. if (info->temp2 > info->temp_max)
  657. info->temp_max = info->temp2;
  658. }
  659. static inline void adjust_fan(struct avalon_info *info)
  660. {
  661. int temp_new;
  662. temp_new = info->temp_sum / info->temp_history_count;
  663. if (temp_new < 35) {
  664. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  665. info->temp_old = temp_new;
  666. } else if (temp_new > 55) {
  667. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  668. info->temp_old = temp_new;
  669. } else if (abs(temp_new - info->temp_old) >= 2) {
  670. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  671. info->temp_old = temp_new;
  672. }
  673. }
  674. /* We use a replacement algorithm to only remove references to work done from
  675. * the buffer when we need the extra space for new work. */
  676. static bool avalon_fill(struct cgpu_info *avalon)
  677. {
  678. struct avalon_info *info = avalon->device_data;
  679. int subid, slot, mc;
  680. struct work *work;
  681. mc = info->miner_count;
  682. if (avalon->queued >= mc)
  683. return true;
  684. work = get_queued(avalon);
  685. if (unlikely(!work))
  686. return false;
  687. subid = avalon->queued++;
  688. work->subid = subid;
  689. slot = avalon->work_array * mc + subid;
  690. if (likely(avalon->works[slot]))
  691. work_completed(avalon, avalon->works[slot]);
  692. avalon->works[slot] = work;
  693. if (avalon->queued >= mc)
  694. return true;
  695. return false;
  696. }
  697. static void avalon_rotate_array(struct cgpu_info *avalon)
  698. {
  699. avalon->queued = 0;
  700. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  701. avalon->work_array = 0;
  702. }
  703. static int64_t avalon_scanhash(struct thr_info *thr)
  704. {
  705. struct cgpu_info *avalon;
  706. struct work **works;
  707. int fd, ret = AVA_GETS_OK, full;
  708. struct avalon_info *info;
  709. struct avalon_task at;
  710. struct avalon_result ar;
  711. int i;
  712. int avalon_get_work_count;
  713. int start_count, end_count;
  714. struct timeval tv_start, tv_finish, elapsed;
  715. uint32_t nonce;
  716. int64_t hash_count;
  717. static int first_try = 0;
  718. int result_wrong;
  719. avalon = thr->cgpu;
  720. works = avalon->works;
  721. info = avalon->device_data;
  722. avalon_get_work_count = info->miner_count;
  723. if (unlikely(avalon->device_fd == -1)) {
  724. if (!avalon_prepare(thr)) {
  725. applog(LOG_ERR, "AVA%i: Comms error(open)",
  726. avalon->device_id);
  727. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  728. /* fail the device if the reopen attempt fails */
  729. return -1;
  730. }
  731. }
  732. fd = avalon->device_fd;
  733. #ifndef WIN32
  734. tcflush(fd, TCOFLUSH);
  735. #endif
  736. start_count = avalon->work_array * avalon_get_work_count;
  737. end_count = start_count + avalon_get_work_count;
  738. i = start_count;
  739. while (true) {
  740. avalon_init_task(&at, 0, 0, info->fan_pwm,
  741. info->timeout, info->asic_count,
  742. info->miner_count, 1, 0, info->frequency);
  743. avalon_create_task(&at, works[i]);
  744. ret = avalon_send_task(fd, &at, avalon);
  745. if (unlikely(ret == AVA_SEND_ERROR ||
  746. (ret == AVA_SEND_BUFFER_EMPTY &&
  747. (i + 1 == end_count) &&
  748. first_try))) {
  749. do_avalon_close(thr);
  750. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  751. avalon->device_id);
  752. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  753. first_try = 0;
  754. cgsleep_ms(1000);
  755. avalon_init(avalon);
  756. return 0; /* This should never happen */
  757. }
  758. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == end_count)) {
  759. first_try = 1;
  760. avalon_rotate_array(avalon);
  761. return 0xffffffff;
  762. }
  763. works[i]->blk.nonce = 0xffffffff;
  764. if (ret == AVA_SEND_BUFFER_FULL)
  765. break;
  766. i++;
  767. }
  768. if (unlikely(first_try))
  769. first_try = 0;
  770. elapsed.tv_sec = elapsed.tv_usec = 0;
  771. cgtime(&tv_start);
  772. result_wrong = 0;
  773. hash_count = 0;
  774. while (true) {
  775. full = avalon_buffer_full(fd);
  776. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  777. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  778. if (unlikely(full == AVA_BUFFER_EMPTY))
  779. break;
  780. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  781. if (unlikely(ret == AVA_GETS_ERROR)) {
  782. do_avalon_close(thr);
  783. applog(LOG_ERR,
  784. "AVA%i: Comms error(read)", avalon->device_id);
  785. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  786. return 0;
  787. }
  788. if (unlikely(ret == AVA_GETS_RESTART))
  789. break;
  790. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  791. timersub(&tv_finish, &tv_start, &elapsed);
  792. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  793. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  794. continue;
  795. }
  796. if (!avalon_decode_nonce(thr, &ar, &nonce)) {
  797. info->no_matching_work++;
  798. result_wrong++;
  799. if (unlikely(result_wrong >= avalon_get_work_count))
  800. break;
  801. if (opt_debug) {
  802. timersub(&tv_finish, &tv_start, &elapsed);
  803. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  804. " (%ld.%06lds)", info->no_matching_work,
  805. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  806. }
  807. continue;
  808. }
  809. hash_count += 0xffffffff;
  810. if (opt_debug) {
  811. timersub(&tv_finish, &tv_start, &elapsed);
  812. applog(LOG_DEBUG,
  813. "Avalon: nonce = 0x%08"PRIx32" = 0x%08"PRIx64" hashes "
  814. "(%ld.%06lds)", nonce, (uint64_t)hash_count,
  815. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  816. }
  817. }
  818. if (hash_count && avalon->results < AVALON_ARRAY_SIZE)
  819. avalon->results++;
  820. if (unlikely((result_wrong >= avalon_get_work_count) ||
  821. (!hash_count && ret != AVA_GETS_RESTART && --avalon->results < 0))) {
  822. /* Look for all invalid results, or consecutive failure
  823. * to generate any results suggesting the FPGA
  824. * controller has screwed up. */
  825. do_avalon_close(thr);
  826. applog(LOG_ERR,
  827. "AVA%i: FPGA controller messed up, %d wrong results",
  828. avalon->device_id, result_wrong);
  829. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  830. cgsleep_ms(1000);
  831. avalon_init(avalon);
  832. return 0;
  833. }
  834. avalon_rotate_array(avalon);
  835. if (hash_count) {
  836. record_temp_fan(info, &ar, &(avalon->temp));
  837. avalon->temp = info->temp_max;
  838. applog(LOG_INFO,
  839. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  840. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  841. info->fan0, info->fan1, info->fan2,
  842. info->temp0, info->temp1, info->temp2, info->temp_max);
  843. info->temp_history_index++;
  844. info->temp_sum += avalon->temp;
  845. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  846. info->temp_history_index, info->temp_history_count, info->temp_old);
  847. if (info->temp_history_index == info->temp_history_count) {
  848. adjust_fan(info);
  849. info->temp_history_index = 0;
  850. info->temp_sum = 0;
  851. }
  852. }
  853. /* This hashmeter is just a utility counter based on returned shares */
  854. return hash_count;
  855. }
  856. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  857. {
  858. struct api_data *root = NULL;
  859. struct avalon_info *info = cgpu->device_data;
  860. int i;
  861. root = api_add_int(root, "baud", &(info->baud), false);
  862. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  863. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  864. root = api_add_int(root, "read_count", &(info->read_count), false);
  865. root = api_add_int(root, "timeout", &(info->timeout), false);
  866. root = api_add_int(root, "frequency", &(info->frequency), false);
  867. root = api_add_int(root, "fan1", &(info->fan0), false);
  868. root = api_add_int(root, "fan2", &(info->fan1), false);
  869. root = api_add_int(root, "fan3", &(info->fan2), false);
  870. root = api_add_int(root, "temp1", &(info->temp0), false);
  871. root = api_add_int(root, "temp2", &(info->temp1), false);
  872. root = api_add_int(root, "temp3", &(info->temp2), false);
  873. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  874. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  875. for (i = 0; i < info->miner_count; i++) {
  876. char mcw[24];
  877. sprintf(mcw, "match_work_count%d", i + 1);
  878. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  879. }
  880. return root;
  881. }
  882. static void avalon_shutdown(struct thr_info *thr)
  883. {
  884. do_avalon_close(thr);
  885. }
  886. struct device_drv avalon_drv = {
  887. .dname = "avalon",
  888. .name = "AVA",
  889. .lowl_probe_by_name_only = true,
  890. .lowl_probe = avalon_lowl_probe,
  891. .thread_prepare = avalon_prepare,
  892. .minerloop = hash_queued_work,
  893. .queue_full = avalon_fill,
  894. .scanwork = avalon_scanhash,
  895. .zero_stats = avalon_zero_stats,
  896. .get_api_stats = avalon_api_stats,
  897. .reinit_device = avalon_init,
  898. .thread_shutdown = avalon_shutdown,
  899. #ifdef HAVE_CURSES
  900. .proc_wlogprint_status = avalon_wlogprint_status,
  901. .proc_tui_wlogprint_choices = avalon_tui_wlogprint_choices,
  902. .proc_tui_handle_choice = avalon_tui_handle_choice,
  903. #endif
  904. };