driver-avalon.c 25 KB

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