driver-avalon.c 24 KB

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