driver-avalon.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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_info;
  39. struct device_api avalon_api;
  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_info[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, int read_count,
  175. struct thr_info *thr, struct timeval *tv_finish)
  176. {
  177. ssize_t ret = 0;
  178. int rc = 0;
  179. int read_amount = AVALON_READ_SIZE;
  180. bool first = true;
  181. while (true) {
  182. struct timeval timeout = {0, 100000};
  183. fd_set rd;
  184. FD_ZERO(&rd);
  185. FD_SET(fd, &rd);
  186. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  187. if (unlikely(ret < 0))
  188. return AVA_GETS_ERROR;
  189. if (ret) {
  190. ret = read(fd, buf, read_amount);
  191. if (unlikely(ret < 0))
  192. return AVA_GETS_ERROR;
  193. if (first) {
  194. if (tv_finish)
  195. gettimeofday(tv_finish, NULL);
  196. first = false;
  197. }
  198. if (ret >= read_amount)
  199. return AVA_GETS_OK;
  200. buf += ret;
  201. read_amount -= ret;
  202. continue;
  203. }
  204. rc++;
  205. if (rc >= read_count) {
  206. if (opt_debug) {
  207. applog(LOG_WARNING,
  208. "Avalon: No data in %.2f seconds",
  209. (float)rc/(float)AVALON_TIME_FACTOR);
  210. }
  211. return AVA_GETS_TIMEOUT;
  212. }
  213. if (thr && thr->work_restart) {
  214. if (opt_debug) {
  215. applog(LOG_WARNING,
  216. "Avalon: Work restart at %.2f seconds",
  217. (float)(rc)/(float)AVALON_TIME_FACTOR);
  218. }
  219. return AVA_GETS_RESTART;
  220. }
  221. }
  222. }
  223. static int avalon_get_result(int fd, struct avalon_result *ar,
  224. struct thr_info *thr, struct timeval *tv_finish)
  225. {
  226. struct cgpu_info *avalon;
  227. struct avalon_info *info;
  228. uint8_t result[AVALON_READ_SIZE];
  229. int ret, read_count = AVALON_RESET_FAULT_DECISECONDS * AVALON_TIME_FACTOR;
  230. if (likely(thr)) {
  231. avalon = thr->cgpu;
  232. info = avalon_info[avalon->device_id];
  233. read_count = info->read_count;
  234. }
  235. memset(result, 0, AVALON_READ_SIZE);
  236. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  237. if (ret == AVA_GETS_OK) {
  238. if (opt_debug) {
  239. applog(LOG_DEBUG, "Avalon: get:");
  240. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  241. }
  242. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  243. }
  244. return ret;
  245. }
  246. static int avalon_decode_nonce(struct thr_info *thr, struct work **work,
  247. struct avalon_result *ar, uint32_t *nonce)
  248. {
  249. struct cgpu_info *avalon;
  250. struct avalon_info *info;
  251. int avalon_get_work_count, i;
  252. if (unlikely(!work))
  253. return -1;
  254. avalon = thr->cgpu;
  255. info = avalon_info[avalon->device_id];
  256. avalon_get_work_count = info->miner_count;
  257. for (i = 0; i < avalon_get_work_count; i++) {
  258. if (work[i] &&
  259. !memcmp(ar->data, work[i]->data + 64, 12) &&
  260. !memcmp(ar->midstate, work[i]->midstate, 32))
  261. break;
  262. }
  263. if (i == avalon_get_work_count)
  264. return -1;
  265. info->matching_work[i]++;
  266. *nonce = htole32(ar->nonce);
  267. applog(LOG_DEBUG, "Avalon: match to work[%d](%p): %d",i, work[i],
  268. info->matching_work[i]);
  269. return i;
  270. }
  271. static int avalon_reset(int fd, struct avalon_result *ar)
  272. {
  273. struct avalon_task at;
  274. uint8_t *buf;
  275. int ret, i = 0;
  276. struct timespec p;
  277. avalon_init_task(&at, 1, 0,
  278. AVALON_DEFAULT_FAN_MAX_PWM,
  279. AVALON_DEFAULT_TIMEOUT,
  280. AVALON_DEFAULT_ASIC_NUM,
  281. AVALON_DEFAULT_MINER_NUM,
  282. 0, 0,
  283. AVALON_DEFAULT_FREQUENCY);
  284. ret = avalon_send_task(fd, &at, NULL);
  285. if (ret == AVA_SEND_ERROR)
  286. return 1;
  287. avalon_get_result(fd, ar, NULL, NULL);
  288. buf = (uint8_t *)ar;
  289. if (buf[0] == 0)
  290. buf = (uint8_t *)(ar + 1);
  291. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  292. buf[2] == 0xAA && buf[3] == 0x55) {
  293. for (i = 4; i < 11; i++)
  294. if (buf[i] != 0)
  295. break;
  296. }
  297. p.tv_sec = 0;
  298. p.tv_nsec = AVALON_RESET_PITCH;
  299. nanosleep(&p, NULL);
  300. if (i != 11) {
  301. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  302. " (%d: %02x %02x %02x %02x)",
  303. i, buf[0], buf[1], buf[2], buf[3]);
  304. /* FIXME: return 1; */
  305. } else
  306. applog(LOG_WARNING, "Avalon: Reset succeeded");
  307. return 0;
  308. }
  309. static void avalon_idle(struct cgpu_info *avalon)
  310. {
  311. int i, ret;
  312. struct avalon_task at;
  313. int fd = avalon->device_fd;
  314. struct avalon_info *info = avalon_info[avalon->device_id];
  315. int avalon_get_work_count = info->miner_count;
  316. i = 0;
  317. while (true) {
  318. avalon_init_task(&at, 0, 0, info->fan_pwm,
  319. info->timeout, info->asic_count,
  320. info->miner_count, 1, 1, info->frequency);
  321. ret = avalon_send_task(fd, &at, avalon);
  322. if (unlikely(ret == AVA_SEND_ERROR ||
  323. (ret == AVA_SEND_BUFFER_EMPTY &&
  324. (i + 1 == avalon_get_work_count * 2)))) {
  325. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  326. return;
  327. }
  328. if (i + 1 == avalon_get_work_count * 2)
  329. break;
  330. if (ret == AVA_SEND_BUFFER_FULL)
  331. break;
  332. i++;
  333. }
  334. applog(LOG_ERR, "Avalon: Goto idle mode");
  335. }
  336. static void get_options(int this_option_offset, int *baud, int *miner_count,
  337. int *asic_count, int *timeout, int *frequency)
  338. {
  339. char err_buf[BUFSIZ+1];
  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. sprintf(err_buf,
  390. "Invalid avalon-options for baud (%s) "
  391. "must be 115200, 57600, 38400 or 19200", buf);
  392. quit(1, err_buf);
  393. }
  394. if (colon && *colon) {
  395. colon2 = strchr(colon, ':');
  396. if (colon2)
  397. *(colon2++) = '\0';
  398. if (*colon) {
  399. tmp = atoi(colon);
  400. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  401. *miner_count = tmp;
  402. } else {
  403. sprintf(err_buf,
  404. "Invalid avalon-options for "
  405. "miner_count (%s) must be 1 ~ %d",
  406. colon, AVALON_DEFAULT_MINER_NUM);
  407. quit(1, err_buf);
  408. }
  409. }
  410. if (colon2 && *colon2) {
  411. colon3 = strchr(colon2, ':');
  412. if (colon3)
  413. *(colon3++) = '\0';
  414. tmp = atoi(colon2);
  415. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  416. *asic_count = tmp;
  417. else {
  418. sprintf(err_buf,
  419. "Invalid avalon-options for "
  420. "asic_count (%s) must be 1 ~ %d",
  421. colon2, AVALON_DEFAULT_ASIC_NUM);
  422. quit(1, err_buf);
  423. }
  424. if (colon3 && *colon3) {
  425. colon4 = strchr(colon3, ':');
  426. if (colon4)
  427. *(colon4++) = '\0';
  428. tmp = atoi(colon3);
  429. if (tmp > 0 && tmp <= 0xff)
  430. *timeout = tmp;
  431. else {
  432. sprintf(err_buf,
  433. "Invalid avalon-options for "
  434. "timeout (%s) must be 1 ~ %d",
  435. colon3, 0xff);
  436. quit(1, err_buf);
  437. }
  438. if (colon4 && *colon4) {
  439. tmp = atoi(colon4);
  440. switch (tmp) {
  441. case 256:
  442. case 270:
  443. case 282:
  444. case 300:
  445. *frequency = tmp;
  446. break;
  447. default:
  448. sprintf(err_buf,
  449. "Invalid avalon-options for "
  450. "frequency must be 256/270/282/300");
  451. quit(1, err_buf);
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. static bool avalon_detect_one(const char *devpath)
  459. {
  460. struct avalon_info *info;
  461. struct avalon_result ar;
  462. int fd, ret;
  463. int baud, miner_count, asic_count, timeout, frequency = 0;
  464. struct cgpu_info *avalon;
  465. int this_option_offset = ++option_offset;
  466. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  467. &timeout, &frequency);
  468. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  469. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  470. devpath, baud, miner_count, asic_count, timeout, frequency);
  471. fd = avalon_open2(devpath, baud, true);
  472. if (unlikely(fd == -1)) {
  473. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  474. return false;
  475. }
  476. /* We have a real Avalon! */
  477. avalon = calloc(1, sizeof(struct cgpu_info));
  478. avalon->api = &avalon_api;
  479. avalon->device_path = strdup(devpath);
  480. avalon->device_fd = fd;
  481. avalon->threads = AVALON_MINER_THREADS;
  482. add_cgpu(avalon);
  483. ret = avalon_reset(fd, &ar);
  484. if (ret) {
  485. ; /* FIXME: I think IT IS avalon and wait on reset;
  486. * avalon_close(fd);
  487. * return false; */
  488. }
  489. avalon_info = realloc(avalon_info,
  490. sizeof(struct avalon_info *) *
  491. (total_devices + 1));
  492. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  493. devpath, avalon->device_id);
  494. avalon_info[avalon->device_id] = (struct avalon_info *)
  495. malloc(sizeof(struct avalon_info));
  496. if (unlikely(!(avalon_info[avalon->device_id])))
  497. quit(1, "Failed to malloc avalon_info");
  498. info = avalon_info[avalon->device_id];
  499. memset(info, 0, sizeof(struct avalon_info));
  500. info->baud = baud;
  501. info->miner_count = miner_count;
  502. info->asic_count = asic_count;
  503. info->timeout = timeout;
  504. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  505. AVALON_TIME_FACTOR) / (float)info->miner_count;
  506. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  507. info->temp_max = 0;
  508. /* This is for check the temp/fan every 3~4s */
  509. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  510. if (info->temp_history_count <= 0)
  511. info->temp_history_count = 1;
  512. info->temp_history_index = 0;
  513. info->temp_sum = 0;
  514. info->temp_old = 0;
  515. info->frequency = frequency;
  516. /* Do something for failed reset ? */
  517. if (0) {
  518. /* Set asic to idle mode after detect */
  519. avalon_idle(avalon);
  520. avalon->device_fd = -1;
  521. avalon_close(fd);
  522. }
  523. return true;
  524. }
  525. static inline void avalon_detect()
  526. {
  527. serial_detect(&avalon_api, avalon_detect_one);
  528. }
  529. static void __avalon_init(struct cgpu_info *avalon)
  530. {
  531. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  532. }
  533. static void avalon_init(struct cgpu_info *avalon)
  534. {
  535. struct avalon_result ar;
  536. int fd, ret;
  537. avalon->device_fd = -1;
  538. fd = avalon_open(avalon->device_path,
  539. avalon_info[avalon->device_id]->baud);
  540. if (unlikely(fd == -1)) {
  541. applog(LOG_ERR, "Avalon: Failed to open on %s",
  542. avalon->device_path);
  543. return;
  544. }
  545. ret = avalon_reset(fd, &ar);
  546. if (ret) {
  547. avalon_close(fd);
  548. return;
  549. }
  550. avalon->device_fd = fd;
  551. __avalon_init(avalon);
  552. }
  553. static bool avalon_prepare(struct thr_info *thr)
  554. {
  555. struct cgpu_info *avalon = thr->cgpu;
  556. struct timeval now;
  557. __avalon_init(avalon);
  558. gettimeofday(&now, NULL);
  559. get_datestamp(avalon->init, &now);
  560. return true;
  561. }
  562. static void avalon_free_work(struct thr_info *thr, struct work **work)
  563. {
  564. struct cgpu_info *avalon;
  565. struct avalon_info *info;
  566. int i;
  567. if (unlikely(!work))
  568. return;
  569. avalon = thr->cgpu;
  570. info = avalon_info[avalon->device_id];
  571. for (i = 0; i < info->miner_count; i++)
  572. if (likely(work[i])) {
  573. free_work(work[i]);
  574. work[i] = NULL;
  575. }
  576. }
  577. static void do_avalon_close(struct thr_info *thr)
  578. {
  579. struct avalon_result ar;
  580. struct cgpu_info *avalon = thr->cgpu;
  581. struct avalon_info *info = avalon_info[avalon->device_id];
  582. sleep(1);
  583. avalon_reset(avalon->device_fd, &ar);
  584. avalon_idle(avalon);
  585. avalon_close(avalon->device_fd);
  586. avalon->device_fd = -1;
  587. info->no_matching_work = 0;
  588. avalon_free_work(thr, info->bulk0);
  589. avalon_free_work(thr, info->bulk1);
  590. avalon_free_work(thr, info->bulk2);
  591. avalon_free_work(thr, info->bulk3);
  592. }
  593. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  594. {
  595. int max;
  596. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  597. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  598. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  599. info->temp0 = ar->temp0;
  600. info->temp1 = ar->temp1;
  601. info->temp2 = ar->temp2;
  602. if (ar->temp0 & 0x80) {
  603. ar->temp0 &= 0x7f;
  604. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  605. }
  606. if (ar->temp1 & 0x80) {
  607. ar->temp1 &= 0x7f;
  608. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  609. }
  610. if (ar->temp2 & 0x80) {
  611. ar->temp2 &= 0x7f;
  612. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  613. }
  614. *temp_avg = info->temp2;
  615. max = info->temp_max;
  616. if (info->temp0 > max)
  617. max = info->temp0;
  618. if (info->temp1 > max)
  619. max = info->temp1;
  620. if (info->temp2 > max)
  621. max = info->temp2;
  622. if (max >= 100) { /* FIXME: fix the root cause on fpga controller firmware */
  623. if (opt_debug) {
  624. applog(LOG_DEBUG, "Avalon: temp_max: %d", max);
  625. hexdump((uint8_t *)ar, AVALON_READ_SIZE);
  626. }
  627. return;
  628. }
  629. info->temp_max = max;
  630. }
  631. static inline void adjust_fan(struct avalon_info *info)
  632. {
  633. int temp_new;
  634. temp_new = info->temp_sum / info->temp_history_count;
  635. if (temp_new < 35) {
  636. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  637. info->temp_old = temp_new;
  638. } else if (temp_new > 55) {
  639. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  640. info->temp_old = temp_new;
  641. } else if (abs(temp_new - info->temp_old) >= 2) {
  642. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  643. info->temp_old = temp_new;
  644. }
  645. }
  646. static int64_t avalon_scanhash(struct thr_info *thr, struct work **work,
  647. __maybe_unused int64_t max_nonce)
  648. {
  649. struct cgpu_info *avalon;
  650. int fd, ret, full;
  651. struct avalon_info *info;
  652. struct avalon_task at;
  653. struct avalon_result ar;
  654. int i, work_i0, work_i1, work_i2, work_i3;
  655. int avalon_get_work_count;
  656. struct timeval tv_start, tv_finish, elapsed;
  657. uint32_t nonce;
  658. int64_t hash_count;
  659. static int first_try = 0;
  660. int result_count, result_wrong;
  661. avalon = thr->cgpu;
  662. info = avalon_info[avalon->device_id];
  663. avalon_get_work_count = info->miner_count;
  664. if (unlikely(avalon->device_fd == -1))
  665. if (!avalon_prepare(thr)) {
  666. applog(LOG_ERR, "AVA%i: Comms error(open)",
  667. avalon->device_id);
  668. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  669. /* fail the device if the reopen attempt fails */
  670. return -1;
  671. }
  672. fd = avalon->device_fd;
  673. #ifndef WIN32
  674. tcflush(fd, TCOFLUSH);
  675. #endif
  676. for (i = 0; i < avalon_get_work_count; i++) {
  677. info->bulk0[i] = info->bulk1[i];
  678. info->bulk1[i] = info->bulk2[i];
  679. info->bulk2[i] = info->bulk3[i];
  680. info->bulk3[i] = work[i];
  681. applog(LOG_DEBUG, "Avalon: bulk0/1/2 buffer [%d]: %p, %p, %p, %p",
  682. i, info->bulk0[i], info->bulk1[i], info->bulk2[i], info->bulk3[i]);
  683. }
  684. i = 0;
  685. while (true) {
  686. avalon_init_task(&at, 0, 0, info->fan_pwm,
  687. info->timeout, info->asic_count,
  688. info->miner_count, 1, 0, info->frequency);
  689. avalon_create_task(&at, work[i]);
  690. ret = avalon_send_task(fd, &at, avalon);
  691. if (unlikely(ret == AVA_SEND_ERROR ||
  692. (ret == AVA_SEND_BUFFER_EMPTY &&
  693. (i + 1 == avalon_get_work_count) &&
  694. first_try))) {
  695. avalon_free_work(thr, info->bulk0);
  696. avalon_free_work(thr, info->bulk1);
  697. avalon_free_work(thr, info->bulk2);
  698. avalon_free_work(thr, info->bulk3);
  699. do_avalon_close(thr);
  700. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  701. avalon->device_id);
  702. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  703. first_try = 0;
  704. sleep(1);
  705. avalon_init(avalon);
  706. return 0; /* This should never happen */
  707. }
  708. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == avalon_get_work_count)) {
  709. first_try = 1;
  710. return 0xffffffff;
  711. }
  712. work[i]->blk.nonce = 0xffffffff;
  713. if (ret == AVA_SEND_BUFFER_FULL)
  714. break;
  715. i++;
  716. }
  717. if (unlikely(first_try))
  718. first_try = 0;
  719. elapsed.tv_sec = elapsed.tv_usec = 0;
  720. gettimeofday(&tv_start, NULL);
  721. result_count = 0;
  722. result_wrong = 0;
  723. hash_count = 0;
  724. while (true) {
  725. work_i0 = work_i1 = work_i2 = work_i3 = -1;
  726. full = avalon_buffer_full(fd);
  727. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  728. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  729. if (unlikely(full == AVA_BUFFER_EMPTY))
  730. break;
  731. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  732. if (unlikely(ret == AVA_GETS_ERROR)) {
  733. avalon_free_work(thr, info->bulk0);
  734. avalon_free_work(thr, info->bulk1);
  735. avalon_free_work(thr, info->bulk2);
  736. avalon_free_work(thr, info->bulk3);
  737. do_avalon_close(thr);
  738. applog(LOG_ERR,
  739. "AVA%i: Comms error(read)", avalon->device_id);
  740. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  741. return 0;
  742. }
  743. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  744. timersub(&tv_finish, &tv_start, &elapsed);
  745. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  746. elapsed.tv_sec, elapsed.tv_usec);
  747. continue;
  748. }
  749. if (unlikely(ret == AVA_GETS_RESTART)) {
  750. avalon_free_work(thr, info->bulk0);
  751. avalon_free_work(thr, info->bulk1);
  752. avalon_free_work(thr, info->bulk2);
  753. avalon_free_work(thr, info->bulk3);
  754. break;
  755. }
  756. result_count++;
  757. work_i0 = avalon_decode_nonce(thr, info->bulk0, &ar, &nonce);
  758. if (work_i0 < 0) {
  759. work_i1 = avalon_decode_nonce(thr, info->bulk1, &ar, &nonce);
  760. if (work_i1 < 0) {
  761. work_i2 = avalon_decode_nonce(thr, info->bulk2, &ar, &nonce);
  762. if (work_i2 < 0) {
  763. work_i3 = avalon_decode_nonce(thr, info->bulk3, &ar, &nonce);
  764. if (work_i3 < 0) {
  765. info->no_matching_work++;
  766. result_wrong++;
  767. if (opt_debug) {
  768. timersub(&tv_finish, &tv_start, &elapsed);
  769. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  770. " (%ld.%06lds)", info->no_matching_work,
  771. elapsed.tv_sec, elapsed.tv_usec);
  772. }
  773. continue;
  774. } else
  775. submit_nonce(thr, info->bulk3[work_i3], nonce);
  776. } else
  777. submit_nonce(thr, info->bulk2[work_i2], nonce);
  778. } else
  779. submit_nonce(thr, info->bulk1[work_i1], nonce);
  780. } else
  781. submit_nonce(thr, info->bulk0[work_i0], nonce);
  782. hash_count += nonce;
  783. if (opt_debug) {
  784. timersub(&tv_finish, &tv_start, &elapsed);
  785. applog(LOG_DEBUG,
  786. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  787. "(%ld.%06lds)", nonce, hash_count,
  788. elapsed.tv_sec, elapsed.tv_usec);
  789. }
  790. }
  791. if (result_wrong && result_count == result_wrong) {
  792. /* This mean FPGA controller give all wrong result
  793. * try to reset the Avalon */
  794. avalon_free_work(thr, info->bulk0);
  795. avalon_free_work(thr, info->bulk1);
  796. avalon_free_work(thr, info->bulk2);
  797. avalon_free_work(thr, info->bulk3);
  798. do_avalon_close(thr);
  799. applog(LOG_ERR,
  800. "AVA%i: FPGA controller mess up", avalon->device_id);
  801. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  802. do_avalon_close(thr);
  803. sleep(1);
  804. avalon_init(avalon);
  805. return 0;
  806. }
  807. avalon_free_work(thr, info->bulk0);
  808. record_temp_fan(info, &ar, &(avalon->temp));
  809. applog(LOG_INFO,
  810. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  811. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  812. info->fan0, info->fan1, info->fan2,
  813. info->temp0, info->temp1, info->temp2, info->temp_max);
  814. info->temp_history_index++;
  815. info->temp_sum += info->temp2;
  816. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  817. info->temp_history_index, info->temp_history_count, info->temp_old);
  818. if (info->temp_history_index == info->temp_history_count) {
  819. adjust_fan(info);
  820. info->temp_history_index = 0;
  821. info->temp_sum = 0;
  822. }
  823. /*
  824. * FIXME: Each work split to 10 pieces, each piece send to a
  825. * asic(256MHs). one work can be mulit-nonce back. it is not
  826. * easy calculate correct hash on such situation. so I simplely
  827. * add each nonce to hash_count. base on Utility/m hash_count*2
  828. * give a very good result.
  829. *
  830. * Any patch will be great.
  831. */
  832. return (hash_count * 2);
  833. }
  834. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  835. {
  836. struct api_data *root = NULL;
  837. struct avalon_info *info = avalon_info[cgpu->device_id];
  838. root = api_add_int(root, "baud", &(info->baud), false);
  839. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  840. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  841. root = api_add_int(root, "read_count", &(info->read_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_count1", &(info->matching_work[0]), false);
  853. root = api_add_int(root, "matching_work_count2", &(info->matching_work[1]), false);
  854. root = api_add_int(root, "matching_work_count3", &(info->matching_work[2]), false);
  855. root = api_add_int(root, "matching_work_count4", &(info->matching_work[3]), false);
  856. root = api_add_int(root, "matching_work_count5", &(info->matching_work[4]), false);
  857. root = api_add_int(root, "matching_work_count6", &(info->matching_work[5]), false);
  858. root = api_add_int(root, "matching_work_count7", &(info->matching_work[6]), false);
  859. root = api_add_int(root, "matching_work_count8", &(info->matching_work[7]), false);
  860. root = api_add_int(root, "matching_work_count9", &(info->matching_work[8]), false);
  861. root = api_add_int(root, "matching_work_count10", &(info->matching_work[9]), false);
  862. root = api_add_int(root, "matching_work_count11", &(info->matching_work[10]), false);
  863. root = api_add_int(root, "matching_work_count12", &(info->matching_work[11]), false);
  864. root = api_add_int(root, "matching_work_count13", &(info->matching_work[12]), false);
  865. root = api_add_int(root, "matching_work_count14", &(info->matching_work[13]), false);
  866. root = api_add_int(root, "matching_work_count15", &(info->matching_work[14]), false);
  867. root = api_add_int(root, "matching_work_count16", &(info->matching_work[15]), false);
  868. root = api_add_int(root, "matching_work_count17", &(info->matching_work[16]), false);
  869. root = api_add_int(root, "matching_work_count18", &(info->matching_work[17]), false);
  870. root = api_add_int(root, "matching_work_count19", &(info->matching_work[18]), false);
  871. root = api_add_int(root, "matching_work_count20", &(info->matching_work[19]), false);
  872. root = api_add_int(root, "matching_work_count21", &(info->matching_work[20]), false);
  873. root = api_add_int(root, "matching_work_count22", &(info->matching_work[21]), false);
  874. root = api_add_int(root, "matching_work_count23", &(info->matching_work[22]), false);
  875. root = api_add_int(root, "matching_work_count24", &(info->matching_work[23]), false);
  876. return root;
  877. }
  878. static void avalon_shutdown(struct thr_info *thr)
  879. {
  880. do_avalon_close(thr);
  881. }
  882. struct device_api avalon_api = {
  883. .dname = "avalon",
  884. .name = "AVA",
  885. .api_detect = avalon_detect,
  886. .thread_prepare = avalon_prepare,
  887. .scanhash_queue = avalon_scanhash,
  888. .get_api_stats = avalon_api_stats,
  889. .reinit_device = avalon_init,
  890. .thread_shutdown = avalon_shutdown,
  891. };