driver-avalon.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2013 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Luke Dashjr
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <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->device_data;
  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(%u):", (unsigned int)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 void avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  177. struct avalon_info *info, struct avalon_result *ar,
  178. struct work *work)
  179. {
  180. uint32_t nonce;
  181. info = avalon->device_data;
  182. info->matching_work[work->subid]++;
  183. nonce = htole32(ar->nonce);
  184. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  185. submit_nonce(thr, work, nonce);
  186. }
  187. static int avalon_write(int fd, char *buf, ssize_t len)
  188. {
  189. ssize_t wrote = 0;
  190. while (len > 0) {
  191. struct timeval timeout;
  192. ssize_t ret;
  193. fd_set wd;
  194. timeout.tv_sec = 0;
  195. timeout.tv_usec = 100000;
  196. FD_ZERO(&wd);
  197. FD_SET((SOCKETTYPE)fd, &wd);
  198. ret = select(fd + 1, NULL, &wd, NULL, &timeout);
  199. if (unlikely(ret < 1)) {
  200. applog(LOG_WARNING, "Select error on avalon_write");
  201. return AVA_SEND_ERROR;
  202. }
  203. ret = write(fd, buf + wrote, len);
  204. if (unlikely(ret < 1)) {
  205. applog(LOG_WARNING, "Write error on avalon_write");
  206. return AVA_SEND_ERROR;
  207. }
  208. wrote += ret;
  209. len -= ret;
  210. }
  211. return 0;
  212. }
  213. static int avalon_read(int fd, char *buf, ssize_t len)
  214. {
  215. ssize_t aread = 0;
  216. while (len > 0) {
  217. struct timeval timeout;
  218. ssize_t ret;
  219. fd_set rd;
  220. timeout.tv_sec = 0;
  221. timeout.tv_usec = 100000;
  222. FD_ZERO(&rd);
  223. FD_SET((SOCKETTYPE)fd, &rd);
  224. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  225. if (unlikely(ret < 1)) {
  226. applog(LOG_WARNING, "Select error on avalon_read");
  227. return AVA_GETS_ERROR;
  228. }
  229. ret = read(fd, buf + aread, len);
  230. if (unlikely(ret < 1)) {
  231. applog(LOG_WARNING, "Read error on avalon_read");
  232. return AVA_GETS_ERROR;
  233. }
  234. aread += ret;
  235. len -= ret;
  236. }
  237. return 0;
  238. }
  239. /* Non blocking clearing of anything in the buffer */
  240. static void avalon_clear_readbuf(int fd)
  241. {
  242. ssize_t ret;
  243. do {
  244. struct timeval timeout;
  245. char buf[AVALON_FTDI_READSIZE];
  246. fd_set rd;
  247. timeout.tv_sec = timeout.tv_usec = 0;
  248. FD_ZERO(&rd);
  249. FD_SET((SOCKETTYPE)fd, &rd);
  250. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  251. if (ret > 0)
  252. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  253. } while (ret > 0);
  254. }
  255. static int avalon_reset(struct cgpu_info *avalon, int fd)
  256. {
  257. struct avalon_result ar;
  258. char reset = 0xad;
  259. uint8_t *buf;
  260. int ret, i = 0;
  261. struct timespec p;
  262. /* There's usually a one byte response after opening */
  263. avalon_clear_readbuf(fd);
  264. /* Reset once, then send command to go idle */
  265. ret = avalon_write(fd, &reset, 1);
  266. if (unlikely(ret == AVA_SEND_ERROR))
  267. return -1;
  268. ret = avalon_read(fd, (char *)&ar, AVALON_READ_SIZE);
  269. if (unlikely(ret == AVA_GETS_ERROR))
  270. return -1;
  271. nanosleep(&p, NULL);
  272. buf = (uint8_t *)&ar;
  273. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  274. buf[2] == 0xAA && buf[3] == 0x55) {
  275. for (i = 4; i < 11; i++)
  276. if (buf[i] != 0)
  277. break;
  278. }
  279. if (i != 11) {
  280. applog(LOG_ERR, "AVA%d: Reset failed! not an Avalon?"
  281. " (%d: %02x %02x %02x %02x)", avalon->device_id,
  282. i, buf[0], buf[1], buf[2], buf[3]);
  283. /* FIXME: return 1; */
  284. } else
  285. applog(LOG_WARNING, "AVA%d: Reset succeeded",
  286. avalon->device_id);
  287. /* What do these sleeps do?? */
  288. p.tv_sec = 0;
  289. p.tv_nsec = AVALON_RESET_PITCH;
  290. nanosleep(&p, NULL);
  291. return 0;
  292. }
  293. static void get_options(int this_option_offset, int *baud, int *miner_count,
  294. int *asic_count, int *timeout, int *frequency)
  295. {
  296. char err_buf[BUFSIZ+1];
  297. char buf[BUFSIZ+1];
  298. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  299. size_t max;
  300. int i, tmp;
  301. if (opt_avalon_options == NULL)
  302. buf[0] = '\0';
  303. else {
  304. ptr = opt_avalon_options;
  305. for (i = 0; i < this_option_offset; i++) {
  306. comma = strchr(ptr, ',');
  307. if (comma == NULL)
  308. break;
  309. ptr = comma + 1;
  310. }
  311. comma = strchr(ptr, ',');
  312. if (comma == NULL)
  313. max = strlen(ptr);
  314. else
  315. max = comma - ptr;
  316. if (max > BUFSIZ)
  317. max = BUFSIZ;
  318. strncpy(buf, ptr, max);
  319. buf[max] = '\0';
  320. }
  321. *baud = AVALON_IO_SPEED;
  322. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  323. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  324. *timeout = AVALON_DEFAULT_TIMEOUT;
  325. *frequency = AVALON_DEFAULT_FREQUENCY;
  326. if (!(*buf))
  327. return;
  328. colon = strchr(buf, ':');
  329. if (colon)
  330. *(colon++) = '\0';
  331. tmp = atoi(buf);
  332. switch (tmp) {
  333. case 115200:
  334. *baud = 115200;
  335. break;
  336. case 57600:
  337. *baud = 57600;
  338. break;
  339. case 38400:
  340. *baud = 38400;
  341. break;
  342. case 19200:
  343. *baud = 19200;
  344. break;
  345. default:
  346. sprintf(err_buf,
  347. "Invalid avalon-options for baud (%s) "
  348. "must be 115200, 57600, 38400 or 19200", buf);
  349. quit(1, err_buf);
  350. }
  351. if (colon && *colon) {
  352. colon2 = strchr(colon, ':');
  353. if (colon2)
  354. *(colon2++) = '\0';
  355. if (*colon) {
  356. tmp = atoi(colon);
  357. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  358. *miner_count = tmp;
  359. } else {
  360. sprintf(err_buf,
  361. "Invalid avalon-options for "
  362. "miner_count (%s) must be 1 ~ %d",
  363. colon, AVALON_DEFAULT_MINER_NUM);
  364. quit(1, err_buf);
  365. }
  366. }
  367. if (colon2 && *colon2) {
  368. colon3 = strchr(colon2, ':');
  369. if (colon3)
  370. *(colon3++) = '\0';
  371. tmp = atoi(colon2);
  372. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  373. *asic_count = tmp;
  374. else {
  375. sprintf(err_buf,
  376. "Invalid avalon-options for "
  377. "asic_count (%s) must be 1 ~ %d",
  378. colon2, AVALON_DEFAULT_ASIC_NUM);
  379. quit(1, err_buf);
  380. }
  381. if (colon3 && *colon3) {
  382. colon4 = strchr(colon3, ':');
  383. if (colon4)
  384. *(colon4++) = '\0';
  385. tmp = atoi(colon3);
  386. if (tmp > 0 && tmp <= 0xff)
  387. *timeout = tmp;
  388. else {
  389. sprintf(err_buf,
  390. "Invalid avalon-options for "
  391. "timeout (%s) must be 1 ~ %d",
  392. colon3, 0xff);
  393. quit(1, err_buf);
  394. }
  395. if (colon4 && *colon4) {
  396. tmp = atoi(colon4);
  397. switch (tmp) {
  398. case 256:
  399. case 270:
  400. case 282:
  401. case 300:
  402. *frequency = tmp;
  403. break;
  404. default:
  405. sprintf(err_buf,
  406. "Invalid avalon-options for "
  407. "frequency must be 256/270/282/300");
  408. quit(1, err_buf);
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. static bool avalon_detect_one(const char *devpath)
  416. {
  417. struct avalon_info *info;
  418. int fd, ret;
  419. int baud, miner_count, asic_count, timeout, frequency = 0;
  420. struct cgpu_info *avalon;
  421. int this_option_offset = ++option_offset;
  422. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  423. &timeout, &frequency);
  424. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  425. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  426. devpath, baud, miner_count, asic_count, timeout, frequency);
  427. fd = avalon_open2(devpath, baud, true);
  428. if (unlikely(fd == -1)) {
  429. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  430. return false;
  431. }
  432. avalon_clear_readbuf(fd);
  433. /* We have a real Avalon! */
  434. avalon = calloc(1, sizeof(struct cgpu_info));
  435. avalon->drv = &avalon_drv;
  436. avalon->device_path = strdup(devpath);
  437. avalon->device_fd = fd;
  438. avalon->threads = AVALON_MINER_THREADS;
  439. add_cgpu(avalon);
  440. avalon_infos = realloc(avalon_infos,
  441. sizeof(struct avalon_info *) *
  442. (total_devices + 1));
  443. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  444. devpath, avalon->device_id);
  445. avalon_infos[avalon->device_id] = calloc(sizeof(struct avalon_info), 1);
  446. if (unlikely(!(avalon_infos[avalon->device_id])))
  447. quit(1, "Failed to calloc avalon_infos");
  448. avalon->device_data = avalon_infos[avalon->device_id];
  449. info = avalon->device_data;
  450. info->baud = baud;
  451. info->miner_count = miner_count;
  452. info->asic_count = asic_count;
  453. info->timeout = timeout;
  454. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  455. info->temp_max = 0;
  456. /* This is for check the temp/fan every 3~4s */
  457. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  458. if (info->temp_history_count <= 0)
  459. info->temp_history_count = 1;
  460. info->temp_history_index = 0;
  461. info->temp_sum = 0;
  462. info->temp_old = 0;
  463. info->frequency = frequency;
  464. ret = avalon_reset(avalon, fd);
  465. if (ret) {
  466. ; /* FIXME: I think IT IS avalon and wait on reset;
  467. * avalon_close(fd);
  468. * return false; */
  469. }
  470. return true;
  471. }
  472. static inline void avalon_detect()
  473. {
  474. serial_detect(&avalon_drv, avalon_detect_one);
  475. }
  476. static void avalon_init(struct cgpu_info *avalon)
  477. {
  478. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  479. }
  480. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  481. {
  482. return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  483. (char *)ar->data, 64, 12);
  484. }
  485. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  486. struct avalon_result *ar);
  487. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  488. {
  489. applog(LOG_WARNING, "%s%d: No valid work - HW error",
  490. thr->cgpu->drv->name, thr->cgpu->device_id);
  491. inc_hw_errors(thr);
  492. mutex_lock(&info->lock);
  493. info->no_matching_work++;
  494. mutex_unlock(&info->lock);
  495. }
  496. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  497. struct thr_info *thr, char *buf, int *offset)
  498. {
  499. int i, spare = *offset - AVALON_READ_SIZE;
  500. bool found = false;
  501. for (i = 0; i <= spare; i++) {
  502. struct avalon_result *ar;
  503. struct work *work;
  504. ar = (struct avalon_result *)&buf[i];
  505. work = avalon_valid_result(avalon, ar);
  506. if (work) {
  507. bool gettemp = false;
  508. found = true;
  509. mutex_lock(&info->lock);
  510. if (!(++avalon->results % info->miner_count)) {
  511. gettemp = true;
  512. avalon->results = 0;
  513. }
  514. info->nonces++;
  515. mutex_unlock(&info->lock);
  516. avalon_decode_nonce(thr, avalon, info, ar, work);
  517. if (gettemp)
  518. avalon_update_temps(avalon, info, ar);
  519. break;
  520. }
  521. }
  522. if (!found) {
  523. spare = *offset - AVALON_READ_SIZE;
  524. /* We are buffering and haven't accumulated one more corrupt
  525. * work result. */
  526. if (spare < (int)AVALON_READ_SIZE)
  527. return;
  528. avalon_inc_nvw(info, thr);
  529. } else {
  530. spare = AVALON_READ_SIZE + i;
  531. if (i) {
  532. if (i >= (int)AVALON_READ_SIZE)
  533. avalon_inc_nvw(info, thr);
  534. else
  535. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  536. }
  537. }
  538. *offset -= spare;
  539. memmove(buf, buf + spare, *offset);
  540. }
  541. static void *avalon_get_results(void *userdata)
  542. {
  543. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  544. struct avalon_info *info = avalon->device_data;
  545. const int rsize = AVALON_FTDI_READSIZE;
  546. char readbuf[AVALON_READBUF_SIZE];
  547. struct thr_info *thr = info->thr;
  548. int fd = avalon->device_fd;
  549. char threadname[24];
  550. int offset = 0;
  551. pthread_detach(pthread_self());
  552. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  553. RenameThread(threadname);
  554. while (42) {
  555. struct timeval timeout;
  556. char buf[rsize];
  557. ssize_t ret;
  558. fd_set rd;
  559. if (offset >= (int)AVALON_READ_SIZE)
  560. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  561. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  562. /* This should never happen */
  563. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  564. offset = 0;
  565. }
  566. timeout.tv_sec = 0;
  567. timeout.tv_usec = AVALON_READ_TIMEOUT * 1000;
  568. FD_ZERO(&rd);
  569. FD_SET((SOCKETTYPE)fd, &rd);
  570. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  571. if (ret < 1) {
  572. if (unlikely(ret < 0))
  573. applog(LOG_WARNING, "Select error in avalon_get_results");
  574. continue;
  575. }
  576. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  577. if (unlikely(ret < 1)) {
  578. if (unlikely(ret < 0))
  579. applog(LOG_WARNING, "Read error in avalon_get_results");
  580. continue;
  581. }
  582. if (opt_debug) {
  583. applog(LOG_DEBUG, "Avalon: get:");
  584. hexdump((uint8_t *)buf, ret);
  585. }
  586. memcpy(&readbuf[offset], buf, ret);
  587. offset += ret;
  588. }
  589. return NULL;
  590. }
  591. static void avalon_rotate_array(struct cgpu_info *avalon)
  592. {
  593. avalon->queued = 0;
  594. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  595. avalon->work_array = 0;
  596. }
  597. static void *avalon_send_tasks(void *userdata)
  598. {
  599. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  600. struct avalon_info *info = avalon->device_data;
  601. const int avalon_get_work_count = info->miner_count;
  602. int fd = avalon->device_fd;
  603. char threadname[24];
  604. bool idle = false;
  605. pthread_detach(pthread_self());
  606. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  607. RenameThread(threadname);
  608. while (42) {
  609. int start_count, end_count, i, j, ret;
  610. struct avalon_task at;
  611. int idled = 0;
  612. while (avalon_buffer_full(fd)) {
  613. nmsleep(40);
  614. }
  615. mutex_lock(&info->qlock);
  616. start_count = avalon->work_array * avalon_get_work_count;
  617. end_count = start_count + avalon_get_work_count;
  618. for (i = start_count, j = 0; i < end_count; i++, j++) {
  619. if (unlikely(avalon_buffer_full(fd))) {
  620. applog(LOG_WARNING,
  621. "AVA%i: Buffer full before all work queued",
  622. avalon->device_id);
  623. break;
  624. }
  625. if (likely(j < avalon->queued)) {
  626. idle = false;
  627. avalon_init_task(&at, 0, 0, info->fan_pwm,
  628. info->timeout, info->asic_count,
  629. info->miner_count, 1, 0, info->frequency);
  630. avalon_create_task(&at, avalon->works[i]);
  631. } else {
  632. idled++;
  633. avalon_init_task(&at, 0, 0, info->fan_pwm,
  634. info->timeout, info->asic_count,
  635. info->miner_count, 1, 1, info->frequency);
  636. }
  637. ret = avalon_send_task(fd, &at, avalon);
  638. if (unlikely(ret == AVA_SEND_ERROR)) {
  639. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  640. avalon->device_id);
  641. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  642. avalon_reset(avalon, fd);
  643. }
  644. }
  645. pthread_cond_signal(&info->qcond);
  646. mutex_unlock(&info->qlock);
  647. if (unlikely(idled && !idle)) {
  648. idle = true;
  649. applog(LOG_WARNING, "AVA%i: Idled %d miners",
  650. avalon->device_id, idled);
  651. }
  652. avalon_rotate_array(avalon);
  653. }
  654. return NULL;
  655. }
  656. static bool avalon_prepare(struct thr_info *thr)
  657. {
  658. struct cgpu_info *avalon = thr->cgpu;
  659. struct avalon_info *info = avalon->device_data;
  660. struct timeval now;
  661. free(avalon->works);
  662. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  663. AVALON_ARRAY_SIZE);
  664. if (!avalon->works)
  665. quit(1, "Failed to calloc avalon works in avalon_prepare");
  666. info->thr = thr;
  667. mutex_init(&info->lock);
  668. mutex_init(&info->qlock);
  669. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  670. quit(1, "Failed to pthread_cond_init avalon qcond");
  671. avalon_clear_readbuf(avalon->device_fd);
  672. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  673. quit(1, "Failed to create avalon write_thr");
  674. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  675. quit(1, "Failed to create avalon read_thr");
  676. avalon_init(avalon);
  677. cgtime(&now);
  678. get_datestamp(avalon->init, &now);
  679. return true;
  680. }
  681. static void avalon_free_work(struct thr_info *thr)
  682. {
  683. struct cgpu_info *avalon;
  684. struct avalon_info *info;
  685. struct work **works;
  686. int i;
  687. avalon = thr->cgpu;
  688. avalon->queued = 0;
  689. if (unlikely(!avalon->works))
  690. return;
  691. works = avalon->works;
  692. info = avalon->device_data;
  693. for (i = 0; i < info->miner_count * 4; i++) {
  694. if (works[i]) {
  695. work_completed(avalon, works[i]);
  696. works[i] = NULL;
  697. }
  698. }
  699. }
  700. static void do_avalon_close(struct thr_info *thr)
  701. {
  702. struct cgpu_info *avalon = thr->cgpu;
  703. struct avalon_info *info = avalon->device_data;
  704. avalon_free_work(thr);
  705. avalon_reset(avalon, avalon->device_fd);
  706. avalon_close(avalon->device_fd);
  707. avalon->device_fd = -1;
  708. info->no_matching_work = 0;
  709. }
  710. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  711. {
  712. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  713. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  714. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  715. info->temp0 = ar->temp0;
  716. info->temp1 = ar->temp1;
  717. info->temp2 = ar->temp2;
  718. if (ar->temp0 & 0x80) {
  719. ar->temp0 &= 0x7f;
  720. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  721. }
  722. if (ar->temp1 & 0x80) {
  723. ar->temp1 &= 0x7f;
  724. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  725. }
  726. if (ar->temp2 & 0x80) {
  727. ar->temp2 &= 0x7f;
  728. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  729. }
  730. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  731. if (info->temp0 > info->temp_max)
  732. info->temp_max = info->temp0;
  733. if (info->temp1 > info->temp_max)
  734. info->temp_max = info->temp1;
  735. if (info->temp2 > info->temp_max)
  736. info->temp_max = info->temp2;
  737. }
  738. static inline void adjust_fan(struct avalon_info *info)
  739. {
  740. int temp_new;
  741. temp_new = info->temp_sum / info->temp_history_count;
  742. if (temp_new < 35) {
  743. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  744. info->temp_old = temp_new;
  745. } else if (temp_new > 55) {
  746. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  747. info->temp_old = temp_new;
  748. } else if (abs(temp_new - info->temp_old) >= 2) {
  749. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  750. info->temp_old = temp_new;
  751. }
  752. }
  753. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  754. struct avalon_result *ar)
  755. {
  756. record_temp_fan(info, ar, &(avalon->temp));
  757. applog(LOG_INFO,
  758. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  759. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  760. info->fan0, info->fan1, info->fan2,
  761. info->temp0, info->temp1, info->temp2, info->temp_max);
  762. info->temp_history_index++;
  763. info->temp_sum += avalon->temp;
  764. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  765. info->temp_history_index, info->temp_history_count, info->temp_old);
  766. if (info->temp_history_index == info->temp_history_count) {
  767. adjust_fan(info);
  768. info->temp_history_index = 0;
  769. info->temp_sum = 0;
  770. }
  771. }
  772. /* We use a replacement algorithm to only remove references to work done from
  773. * the buffer when we need the extra space for new work. */
  774. static bool avalon_fill(struct cgpu_info *avalon)
  775. {
  776. int subid, slot, mc = avalon_infos[avalon->device_id]->miner_count;
  777. struct avalon_info *info = avalon->device_data;
  778. struct work *work;
  779. bool ret = true;
  780. mutex_lock(&info->qlock);
  781. if (avalon->queued >= mc)
  782. goto out_unlock;
  783. work = get_queued(avalon);
  784. if (unlikely(!work)) {
  785. ret = false;
  786. goto out_unlock;
  787. }
  788. subid = avalon->queued++;
  789. work->subid = subid;
  790. slot = avalon->work_array * mc + subid;
  791. if (likely(avalon->works[slot]))
  792. work_completed(avalon, avalon->works[slot]);
  793. avalon->works[slot] = work;
  794. if (avalon->queued < mc)
  795. ret = false;
  796. out_unlock:
  797. mutex_unlock(&info->qlock);
  798. return ret;
  799. }
  800. static int64_t avalon_scanhash(struct thr_info *thr)
  801. {
  802. struct cgpu_info *avalon = thr->cgpu;
  803. struct avalon_info *info = avalon->device_data;
  804. struct timeval now, then, tdiff;
  805. int64_t hash_count, us_timeout;
  806. struct timespec abstime;
  807. /* Full nonce range */
  808. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  809. tdiff.tv_sec = us_timeout / 1000000;
  810. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  811. cgtime(&now);
  812. timeradd(&now, &tdiff, &then);
  813. abstime.tv_sec = then.tv_sec;
  814. abstime.tv_nsec = then.tv_usec * 1000;
  815. /* Wait until avalon_send_tasks signals us that it has completed
  816. * sending its work or a full nonce range timeout has occurred */
  817. mutex_lock(&info->qlock);
  818. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  819. mutex_unlock(&info->qlock);
  820. mutex_lock(&info->lock);
  821. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  822. info->nonces = 0;
  823. mutex_unlock(&info->lock);
  824. /* This hashmeter is just a utility counter based on returned shares */
  825. return hash_count;
  826. }
  827. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  828. {
  829. struct api_data *root = NULL;
  830. struct avalon_info *info = cgpu->device_data;
  831. int i;
  832. root = api_add_int(root, "baud", &(info->baud), false);
  833. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  834. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  835. root = api_add_int(root, "timeout", &(info->timeout), false);
  836. root = api_add_int(root, "frequency", &(info->frequency), false);
  837. root = api_add_int(root, "fan1", &(info->fan0), false);
  838. root = api_add_int(root, "fan2", &(info->fan1), false);
  839. root = api_add_int(root, "fan3", &(info->fan2), false);
  840. root = api_add_int(root, "temp1", &(info->temp0), false);
  841. root = api_add_int(root, "temp2", &(info->temp1), false);
  842. root = api_add_int(root, "temp3", &(info->temp2), false);
  843. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  844. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  845. for (i = 0; i < info->miner_count; i++) {
  846. char mcw[24];
  847. sprintf(mcw, "match_work_count%d", i + 1);
  848. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  849. }
  850. return root;
  851. }
  852. static void avalon_shutdown(struct thr_info *thr)
  853. {
  854. do_avalon_close(thr);
  855. }
  856. struct device_drv avalon_drv = {
  857. .drv_id = DRIVER_AVALON,
  858. .dname = "avalon",
  859. .name = "AVA",
  860. .drv_detect = avalon_detect,
  861. .thread_prepare = avalon_prepare,
  862. .hash_work = hash_queued_work,
  863. .queue_full = avalon_fill,
  864. .scanwork = avalon_scanhash,
  865. .get_api_stats = avalon_api_stats,
  866. .reinit_device = avalon_init,
  867. .thread_shutdown = avalon_shutdown,
  868. };