driver-avalon.c 26 KB

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