driver-avalon.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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_infos;
  39. struct device_drv avalon_drv;
  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_infos[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 inline int avalon_gets(int fd, uint8_t *buf, struct thr_info *thr,
  175. struct timeval *tv_finish)
  176. {
  177. int read_amount = AVALON_READ_SIZE;
  178. bool first = true;
  179. ssize_t ret = 0;
  180. while (true) {
  181. struct timeval timeout;
  182. fd_set rd;
  183. timeout.tv_sec = 0;
  184. /* If we get a restart message, still check if there's
  185. * anything in the buffer waiting to be parsed */
  186. if (unlikely(thr->work_restart))
  187. timeout.tv_usec = 0;
  188. else
  189. timeout.tv_usec = 100000;
  190. FD_ZERO(&rd);
  191. FD_SET(fd, &rd);
  192. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  193. if (unlikely(ret < 0)) {
  194. applog(LOG_ERR, "Avalon: Error %d on select in avalon_gets", errno);
  195. return AVA_GETS_ERROR;
  196. }
  197. if (ret) {
  198. ret = read(fd, buf, read_amount);
  199. if (unlikely(ret < 0)) {
  200. applog(LOG_ERR, "Avalon: Error %d on read in avalon_gets", errno);
  201. return AVA_GETS_ERROR;
  202. }
  203. if (likely(first)) {
  204. gettimeofday(tv_finish, NULL);
  205. first = false;
  206. }
  207. if (likely(ret >= read_amount))
  208. return AVA_GETS_OK;
  209. buf += ret;
  210. read_amount -= ret;
  211. continue;
  212. }
  213. if (unlikely(thr->work_restart)) {
  214. applog(LOG_DEBUG, "Avalon: Work restart");
  215. return AVA_GETS_RESTART;
  216. }
  217. return AVA_GETS_TIMEOUT;
  218. }
  219. }
  220. static int avalon_get_result(int fd, struct avalon_result *ar,
  221. struct thr_info *thr, struct timeval *tv_finish)
  222. {
  223. uint8_t result[AVALON_READ_SIZE];
  224. int ret;
  225. memset(result, 0, AVALON_READ_SIZE);
  226. ret = avalon_gets(fd, result, 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 bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
  237. uint32_t *nonce)
  238. {
  239. struct cgpu_info *avalon;
  240. struct avalon_info *info;
  241. struct work *work;
  242. avalon = thr->cgpu;
  243. if (unlikely(!avalon->works))
  244. return false;
  245. work = find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  246. (char *)ar->data, 64, 12);
  247. if (!work)
  248. return false;
  249. info = avalon_infos[avalon->device_id];
  250. info->matching_work++;
  251. *nonce = htole32(ar->nonce);
  252. submit_nonce(thr, work, *nonce);
  253. return true;
  254. }
  255. static void avalon_get_reset(int fd, struct avalon_result *ar)
  256. {
  257. int read_amount = AVALON_READ_SIZE;
  258. uint8_t result[AVALON_READ_SIZE];
  259. struct timeval timeout = {1, 0};
  260. ssize_t ret = 0;
  261. fd_set rd;
  262. memset(result, 0, AVALON_READ_SIZE);
  263. memset(ar, 0, AVALON_READ_SIZE);
  264. FD_ZERO(&rd);
  265. FD_SET(fd, &rd);
  266. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  267. if (unlikely(ret < 0)) {
  268. applog(LOG_WARNING, "Avalon: Error %d on select in avalon_get_reset", errno);
  269. return;
  270. }
  271. if (!ret) {
  272. applog(LOG_WARNING, "Avalon: Timeout on select in avalon_get_reset");
  273. return;
  274. }
  275. ret = read(fd, result, read_amount);
  276. if (unlikely(ret != read_amount)) {
  277. applog(LOG_WARNING, "Avalon: Error %d on read in avalon_get_reset", errno);
  278. return;
  279. }
  280. if (opt_debug) {
  281. applog(LOG_DEBUG, "Avalon: get:");
  282. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  283. }
  284. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  285. }
  286. static int avalon_reset(int fd, struct avalon_result *ar)
  287. {
  288. struct avalon_task at;
  289. uint8_t *buf;
  290. int ret, i = 0;
  291. struct timespec p;
  292. avalon_init_task(&at, 1, 0,
  293. AVALON_DEFAULT_FAN_MAX_PWM,
  294. AVALON_DEFAULT_TIMEOUT,
  295. AVALON_DEFAULT_ASIC_NUM,
  296. AVALON_DEFAULT_MINER_NUM,
  297. 0, 0,
  298. AVALON_DEFAULT_FREQUENCY);
  299. ret = avalon_send_task(fd, &at, NULL);
  300. if (ret == AVA_SEND_ERROR)
  301. return 1;
  302. avalon_get_reset(fd, ar);
  303. buf = (uint8_t *)ar;
  304. /* Sometimes there is one extra 0 byte for some reason in the buffer,
  305. * so work around it. */
  306. if (buf[0] == 0)
  307. buf = (uint8_t *)(ar + 1);
  308. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  309. buf[2] == 0xAA && buf[3] == 0x55) {
  310. for (i = 4; i < 11; i++)
  311. if (buf[i] != 0)
  312. break;
  313. }
  314. p.tv_sec = 0;
  315. p.tv_nsec = AVALON_RESET_PITCH;
  316. nanosleep(&p, NULL);
  317. if (i != 11) {
  318. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  319. " (%d: %02x %02x %02x %02x)",
  320. i, buf[0], buf[1], buf[2], buf[3]);
  321. /* FIXME: return 1; */
  322. } else
  323. applog(LOG_WARNING, "Avalon: Reset succeeded");
  324. return 0;
  325. }
  326. static void avalon_idle(struct cgpu_info *avalon)
  327. {
  328. int i, ret;
  329. struct avalon_task at;
  330. int fd = avalon->device_fd;
  331. struct avalon_info *info = avalon_infos[avalon->device_id];
  332. int avalon_get_work_count = info->miner_count;
  333. i = 0;
  334. while (true) {
  335. avalon_init_task(&at, 0, 0, info->fan_pwm,
  336. info->timeout, info->asic_count,
  337. info->miner_count, 1, 1, info->frequency);
  338. ret = avalon_send_task(fd, &at, avalon);
  339. if (unlikely(ret == AVA_SEND_ERROR ||
  340. (ret == AVA_SEND_BUFFER_EMPTY &&
  341. (i + 1 == avalon_get_work_count * 2)))) {
  342. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  343. return;
  344. }
  345. if (i + 1 == avalon_get_work_count * 2)
  346. break;
  347. if (ret == AVA_SEND_BUFFER_FULL)
  348. break;
  349. i++;
  350. }
  351. applog(LOG_ERR, "Avalon: Goto idle mode");
  352. }
  353. static void get_options(int this_option_offset, int *baud, int *miner_count,
  354. int *asic_count, int *timeout, int *frequency)
  355. {
  356. char err_buf[BUFSIZ+1];
  357. char buf[BUFSIZ+1];
  358. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  359. size_t max;
  360. int i, tmp;
  361. if (opt_avalon_options == NULL)
  362. buf[0] = '\0';
  363. else {
  364. ptr = opt_avalon_options;
  365. for (i = 0; i < this_option_offset; i++) {
  366. comma = strchr(ptr, ',');
  367. if (comma == NULL)
  368. break;
  369. ptr = comma + 1;
  370. }
  371. comma = strchr(ptr, ',');
  372. if (comma == NULL)
  373. max = strlen(ptr);
  374. else
  375. max = comma - ptr;
  376. if (max > BUFSIZ)
  377. max = BUFSIZ;
  378. strncpy(buf, ptr, max);
  379. buf[max] = '\0';
  380. }
  381. *baud = AVALON_IO_SPEED;
  382. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  383. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  384. *timeout = AVALON_DEFAULT_TIMEOUT;
  385. *frequency = AVALON_DEFAULT_FREQUENCY;
  386. if (!(*buf))
  387. return;
  388. colon = strchr(buf, ':');
  389. if (colon)
  390. *(colon++) = '\0';
  391. tmp = atoi(buf);
  392. switch (tmp) {
  393. case 115200:
  394. *baud = 115200;
  395. break;
  396. case 57600:
  397. *baud = 57600;
  398. break;
  399. case 38400:
  400. *baud = 38400;
  401. break;
  402. case 19200:
  403. *baud = 19200;
  404. break;
  405. default:
  406. sprintf(err_buf,
  407. "Invalid avalon-options for baud (%s) "
  408. "must be 115200, 57600, 38400 or 19200", buf);
  409. quit(1, err_buf);
  410. }
  411. if (colon && *colon) {
  412. colon2 = strchr(colon, ':');
  413. if (colon2)
  414. *(colon2++) = '\0';
  415. if (*colon) {
  416. tmp = atoi(colon);
  417. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  418. *miner_count = tmp;
  419. } else {
  420. sprintf(err_buf,
  421. "Invalid avalon-options for "
  422. "miner_count (%s) must be 1 ~ %d",
  423. colon, AVALON_DEFAULT_MINER_NUM);
  424. quit(1, err_buf);
  425. }
  426. }
  427. if (colon2 && *colon2) {
  428. colon3 = strchr(colon2, ':');
  429. if (colon3)
  430. *(colon3++) = '\0';
  431. tmp = atoi(colon2);
  432. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  433. *asic_count = tmp;
  434. else {
  435. sprintf(err_buf,
  436. "Invalid avalon-options for "
  437. "asic_count (%s) must be 1 ~ %d",
  438. colon2, AVALON_DEFAULT_ASIC_NUM);
  439. quit(1, err_buf);
  440. }
  441. if (colon3 && *colon3) {
  442. colon4 = strchr(colon3, ':');
  443. if (colon4)
  444. *(colon4++) = '\0';
  445. tmp = atoi(colon3);
  446. if (tmp > 0 && tmp <= 0xff)
  447. *timeout = tmp;
  448. else {
  449. sprintf(err_buf,
  450. "Invalid avalon-options for "
  451. "timeout (%s) must be 1 ~ %d",
  452. colon3, 0xff);
  453. quit(1, err_buf);
  454. }
  455. if (colon4 && *colon4) {
  456. tmp = atoi(colon4);
  457. switch (tmp) {
  458. case 256:
  459. case 270:
  460. case 282:
  461. case 300:
  462. *frequency = tmp;
  463. break;
  464. default:
  465. sprintf(err_buf,
  466. "Invalid avalon-options for "
  467. "frequency must be 256/270/282/300");
  468. quit(1, err_buf);
  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. /* Do something for failed reset ? */
  532. if (0) {
  533. /* Set asic to idle mode after detect */
  534. avalon_idle(avalon);
  535. avalon->device_fd = -1;
  536. avalon_close(fd);
  537. }
  538. return true;
  539. }
  540. static inline void avalon_detect()
  541. {
  542. serial_detect(&avalon_drv, avalon_detect_one);
  543. }
  544. static void __avalon_init(struct cgpu_info *avalon)
  545. {
  546. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  547. }
  548. static void avalon_init(struct cgpu_info *avalon)
  549. {
  550. struct avalon_result ar;
  551. int fd, ret;
  552. avalon->device_fd = -1;
  553. fd = avalon_open(avalon->device_path,
  554. avalon_infos[avalon->device_id]->baud);
  555. if (unlikely(fd == -1)) {
  556. applog(LOG_ERR, "Avalon: Failed to open on %s",
  557. avalon->device_path);
  558. return;
  559. }
  560. ret = avalon_reset(fd, &ar);
  561. if (ret) {
  562. avalon_close(fd);
  563. return;
  564. }
  565. avalon->device_fd = fd;
  566. __avalon_init(avalon);
  567. }
  568. static bool avalon_prepare(struct thr_info *thr)
  569. {
  570. struct cgpu_info *avalon = thr->cgpu;
  571. struct avalon_info *info = avalon_infos[avalon->device_id];
  572. struct timeval now;
  573. free(avalon->works);
  574. avalon->works = calloc(info->miner_count * sizeof(struct work *), 2);
  575. if (!avalon->works)
  576. quit(1, "Failed to calloc avalon works in avalon_prepare");
  577. if (avalon->device_fd == -1)
  578. avalon_init(avalon);
  579. else
  580. __avalon_init(avalon);
  581. gettimeofday(&now, NULL);
  582. get_datestamp(avalon->init, &now);
  583. return true;
  584. }
  585. static void avalon_free_work(struct thr_info *thr)
  586. {
  587. struct cgpu_info *avalon;
  588. struct avalon_info *info;
  589. struct work **works;
  590. int i;
  591. avalon = thr->cgpu;
  592. avalon->queued = 0;
  593. if (unlikely(!avalon->works))
  594. return;
  595. works = avalon->works;
  596. info = avalon_infos[avalon->device_id];
  597. for (i = 0; i < info->miner_count * 2; i++) {
  598. if (works[i]) {
  599. work_completed(avalon, works[i]);
  600. works[i] = NULL;
  601. }
  602. }
  603. }
  604. static void avalon_free_work_array(struct thr_info *thr)
  605. {
  606. struct cgpu_info *avalon;
  607. struct work **works;
  608. int i, j, mc;
  609. avalon = thr->cgpu;
  610. avalon->queued = 0;
  611. if (unlikely(!avalon->works))
  612. return;
  613. works = avalon->works;
  614. mc = avalon_infos[avalon->device_id]->miner_count;
  615. avalon->work_array ^= 1;
  616. for (i = avalon->work_array * mc, j = 0; j < mc; i++, j++) {
  617. if (likely(works[i])) {
  618. work_completed(avalon, works[i]);
  619. works[i] = NULL;
  620. }
  621. }
  622. }
  623. static void do_avalon_close(struct thr_info *thr)
  624. {
  625. struct avalon_result ar;
  626. struct cgpu_info *avalon = thr->cgpu;
  627. struct avalon_info *info = avalon_infos[avalon->device_id];
  628. avalon_free_work(thr);
  629. sleep(1);
  630. avalon_reset(avalon->device_fd, &ar);
  631. avalon_idle(avalon);
  632. avalon_close(avalon->device_fd);
  633. avalon->device_fd = -1;
  634. info->no_matching_work = 0;
  635. }
  636. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  637. {
  638. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  639. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  640. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  641. info->temp0 = ar->temp0;
  642. info->temp1 = ar->temp1;
  643. info->temp2 = ar->temp2;
  644. if (ar->temp0 & 0x80) {
  645. ar->temp0 &= 0x7f;
  646. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  647. }
  648. if (ar->temp1 & 0x80) {
  649. ar->temp1 &= 0x7f;
  650. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  651. }
  652. if (ar->temp2 & 0x80) {
  653. ar->temp2 &= 0x7f;
  654. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  655. }
  656. *temp_avg = info->temp2;
  657. if (info->temp0 > info->temp_max)
  658. info->temp_max = info->temp0;
  659. if (info->temp1 > info->temp_max)
  660. info->temp_max = info->temp1;
  661. if (info->temp2 > info->temp_max)
  662. info->temp_max = info->temp2;
  663. }
  664. static inline void adjust_fan(struct avalon_info *info)
  665. {
  666. int temp_new;
  667. temp_new = info->temp_sum / info->temp_history_count;
  668. if (temp_new < 35) {
  669. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  670. info->temp_old = temp_new;
  671. } else if (temp_new > 55) {
  672. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  673. info->temp_old = temp_new;
  674. } else if (abs(temp_new - info->temp_old) >= 2) {
  675. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  676. info->temp_old = temp_new;
  677. }
  678. }
  679. static bool avalon_fill(struct cgpu_info *avalon)
  680. {
  681. int subid, mc = avalon_infos[avalon->device_id]->miner_count;
  682. struct work *work;
  683. if (avalon->queued >= mc)
  684. return true;
  685. work = get_queued(avalon);
  686. if (unlikely(!work))
  687. return false;
  688. subid = avalon->queued++;
  689. work->subid = subid;
  690. avalon->works[avalon->work_array * mc + subid] = work;
  691. if (avalon->queued >= mc)
  692. return true;
  693. return false;
  694. }
  695. static int64_t avalon_scanhash(struct thr_info *thr)
  696. {
  697. struct cgpu_info *avalon;
  698. struct work **works;
  699. int fd, ret, full;
  700. struct avalon_info *info;
  701. struct avalon_task at;
  702. struct avalon_result ar;
  703. int i;
  704. int avalon_get_work_count;
  705. int start_count, end_count;
  706. struct timeval tv_start, tv_finish, elapsed;
  707. uint32_t nonce;
  708. int64_t hash_count;
  709. static int first_try = 0;
  710. int result_count, result_wrong;
  711. avalon = thr->cgpu;
  712. works = avalon->works;
  713. info = avalon_infos[avalon->device_id];
  714. avalon_get_work_count = info->miner_count;
  715. if (unlikely(avalon->device_fd == -1)) {
  716. if (!avalon_prepare(thr)) {
  717. applog(LOG_ERR, "AVA%i: Comms error(open)",
  718. avalon->device_id);
  719. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  720. /* fail the device if the reopen attempt fails */
  721. return -1;
  722. }
  723. }
  724. fd = avalon->device_fd;
  725. #ifndef WIN32
  726. tcflush(fd, TCOFLUSH);
  727. #endif
  728. start_count = avalon->work_array * avalon_get_work_count;
  729. end_count = start_count + avalon_get_work_count;
  730. i = start_count;
  731. while (true) {
  732. avalon_init_task(&at, 0, 0, info->fan_pwm,
  733. info->timeout, info->asic_count,
  734. info->miner_count, 1, 0, info->frequency);
  735. avalon_create_task(&at, works[i]);
  736. ret = avalon_send_task(fd, &at, avalon);
  737. if (unlikely(ret == AVA_SEND_ERROR ||
  738. (ret == AVA_SEND_BUFFER_EMPTY &&
  739. (i + 1 == end_count) &&
  740. first_try))) {
  741. do_avalon_close(thr);
  742. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  743. avalon->device_id);
  744. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  745. first_try = 0;
  746. sleep(1);
  747. avalon_init(avalon);
  748. return 0; /* This should never happen */
  749. }
  750. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == end_count)) {
  751. first_try = 1;
  752. avalon_free_work_array(thr);
  753. return 0xffffffff;
  754. }
  755. works[i]->blk.nonce = 0xffffffff;
  756. if (ret == AVA_SEND_BUFFER_FULL)
  757. break;
  758. i++;
  759. }
  760. if (unlikely(first_try))
  761. first_try = 0;
  762. elapsed.tv_sec = elapsed.tv_usec = 0;
  763. gettimeofday(&tv_start, NULL);
  764. result_count = 0;
  765. result_wrong = 0;
  766. hash_count = 0;
  767. while (true) {
  768. full = avalon_buffer_full(fd);
  769. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  770. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  771. if (unlikely(full == AVA_BUFFER_EMPTY))
  772. break;
  773. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  774. if (unlikely(ret == AVA_GETS_ERROR)) {
  775. do_avalon_close(thr);
  776. applog(LOG_ERR,
  777. "AVA%i: Comms error(read)", avalon->device_id);
  778. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  779. return 0;
  780. }
  781. if (unlikely(ret == AVA_GETS_RESTART)) {
  782. /* Reset the wrong count in case there has only been
  783. * a small number of nonces tested before the restart.
  784. */
  785. result_wrong = 0;
  786. break;
  787. }
  788. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  789. timersub(&tv_finish, &tv_start, &elapsed);
  790. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  791. elapsed.tv_sec, elapsed.tv_usec);
  792. continue;
  793. }
  794. result_count++;
  795. if (!avalon_decode_nonce(thr, &ar, &nonce)) {
  796. info->no_matching_work++;
  797. result_wrong++;
  798. if (opt_debug) {
  799. timersub(&tv_finish, &tv_start, &elapsed);
  800. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  801. " (%ld.%06lds)", info->no_matching_work,
  802. elapsed.tv_sec, elapsed.tv_usec);
  803. }
  804. continue;
  805. }
  806. hash_count += 0xffffffff;
  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. do_avalon_close(thr);
  819. applog(LOG_ERR,
  820. "AVA%i: FPGA controller mess up", avalon->device_id);
  821. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  822. sleep(1);
  823. avalon_init(avalon);
  824. return 0;
  825. }
  826. avalon_free_work_array(thr);
  827. record_temp_fan(info, &ar, &(avalon->temp));
  828. applog(LOG_INFO,
  829. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  830. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  831. info->fan0, info->fan1, info->fan2,
  832. info->temp0, info->temp1, info->temp2, info->temp_max);
  833. info->temp_history_index++;
  834. info->temp_sum += info->temp2;
  835. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  836. info->temp_history_index, info->temp_history_count, info->temp_old);
  837. if (info->temp_history_index == info->temp_history_count) {
  838. adjust_fan(info);
  839. info->temp_history_index = 0;
  840. info->temp_sum = 0;
  841. }
  842. /* This hashmeter is just a utility counter based on returned shares */
  843. return hash_count;
  844. }
  845. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  846. {
  847. struct api_data *root = NULL;
  848. struct avalon_info *info = avalon_infos[cgpu->device_id];
  849. root = api_add_int(root, "baud", &(info->baud), false);
  850. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  851. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  852. root = api_add_int(root, "timeout", &(info->timeout), false);
  853. root = api_add_int(root, "frequency", &(info->frequency), false);
  854. root = api_add_int(root, "fan1", &(info->fan0), false);
  855. root = api_add_int(root, "fan2", &(info->fan1), false);
  856. root = api_add_int(root, "fan3", &(info->fan2), false);
  857. root = api_add_int(root, "temp1", &(info->temp0), false);
  858. root = api_add_int(root, "temp2", &(info->temp1), false);
  859. root = api_add_int(root, "temp3", &(info->temp2), false);
  860. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  861. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  862. root = api_add_int(root, "matching_work_count", &(info->matching_work), false);
  863. return root;
  864. }
  865. static void avalon_shutdown(struct thr_info *thr)
  866. {
  867. do_avalon_close(thr);
  868. }
  869. struct device_drv avalon_drv = {
  870. .dname = "avalon",
  871. .name = "AVA",
  872. .drv_detect = avalon_detect,
  873. .thread_prepare = avalon_prepare,
  874. .hash_work = hash_queued_work,
  875. .queue_full = avalon_fill,
  876. .scanwork = avalon_scanhash,
  877. .get_api_stats = avalon_api_stats,
  878. .reinit_device = avalon_init,
  879. .thread_shutdown = avalon_shutdown,
  880. };