driver-avalon.c 26 KB

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