driver-avalon.c 25 KB

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