driver-avalon.c 27 KB

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