driver-avalon.c 25 KB

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