driver-avalon.c 29 KB

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