driver-avalon.c 25 KB

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