driver-avalon.c 24 KB

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