driver-avalon.c 28 KB

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