driver-avalon.c 24 KB

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