driver-avalon.c 26 KB

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