driver-avalon.c 24 KB

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