driver-avalon.c 28 KB

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