driver-avalon.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. if (ret == AVA_GETS_OK) {
  239. if (opt_debug) {
  240. applog(LOG_DEBUG, "Avalon: get:");
  241. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  242. }
  243. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  244. }
  245. return ret;
  246. }
  247. static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
  248. uint32_t *nonce)
  249. {
  250. struct cgpu_info *avalon;
  251. struct avalon_info *info;
  252. struct work *work;
  253. avalon = thr->cgpu;
  254. if (unlikely(!avalon->works))
  255. return false;
  256. work = find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  257. (char *)ar->data, 64, 12);
  258. if (!work)
  259. return false;
  260. info = avalon_infos[avalon->device_id];
  261. info->matching_work++;
  262. *nonce = htole32(ar->nonce);
  263. submit_nonce(thr, work, *nonce);
  264. return true;
  265. }
  266. static int avalon_reset(int fd, struct avalon_result *ar)
  267. {
  268. struct avalon_task at;
  269. uint8_t *buf;
  270. int ret, i = 0;
  271. struct timespec p;
  272. avalon_init_task(&at, 1, 0,
  273. AVALON_DEFAULT_FAN_MAX_PWM,
  274. AVALON_DEFAULT_TIMEOUT,
  275. AVALON_DEFAULT_ASIC_NUM,
  276. AVALON_DEFAULT_MINER_NUM,
  277. 0, 0,
  278. AVALON_DEFAULT_FREQUENCY);
  279. ret = avalon_send_task(fd, &at, NULL);
  280. if (ret == AVA_SEND_ERROR)
  281. return 1;
  282. avalon_get_result(fd, ar, NULL, NULL);
  283. buf = (uint8_t *)ar;
  284. /* Sometimes there is one extra 0 byte for some reason in the buffer,
  285. * so work around it. */
  286. if (buf[0] == 0)
  287. buf = (uint8_t *)(ar + 1);
  288. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  289. buf[2] == 0xAA && buf[3] == 0x55) {
  290. for (i = 4; i < 11; i++)
  291. if (buf[i] != 0)
  292. break;
  293. }
  294. p.tv_sec = 0;
  295. p.tv_nsec = AVALON_RESET_PITCH;
  296. nanosleep(&p, NULL);
  297. if (i != 11) {
  298. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  299. " (%d: %02x %02x %02x %02x)",
  300. i, buf[0], buf[1], buf[2], buf[3]);
  301. /* FIXME: return 1; */
  302. } else
  303. applog(LOG_WARNING, "Avalon: Reset succeeded");
  304. return 0;
  305. }
  306. static void avalon_idle(struct cgpu_info *avalon)
  307. {
  308. int i, ret;
  309. struct avalon_task at;
  310. int fd = avalon->device_fd;
  311. struct avalon_info *info = avalon_infos[avalon->device_id];
  312. int avalon_get_work_count = info->miner_count;
  313. i = 0;
  314. while (true) {
  315. avalon_init_task(&at, 0, 0, info->fan_pwm,
  316. info->timeout, info->asic_count,
  317. info->miner_count, 1, 1, info->frequency);
  318. ret = avalon_send_task(fd, &at, avalon);
  319. if (unlikely(ret == AVA_SEND_ERROR ||
  320. (ret == AVA_SEND_BUFFER_EMPTY &&
  321. (i + 1 == avalon_get_work_count * 2)))) {
  322. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  323. return;
  324. }
  325. if (i + 1 == avalon_get_work_count * 2)
  326. break;
  327. if (ret == AVA_SEND_BUFFER_FULL)
  328. break;
  329. i++;
  330. }
  331. applog(LOG_ERR, "Avalon: Goto idle mode");
  332. }
  333. static void get_options(int this_option_offset, int *baud, int *miner_count,
  334. int *asic_count, int *timeout, int *frequency)
  335. {
  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. quit(1,
  386. "Invalid avalon-options for baud (%s) "
  387. "must be 115200, 57600, 38400 or 19200", buf);
  388. }
  389. if (colon && *colon) {
  390. colon2 = strchr(colon, ':');
  391. if (colon2)
  392. *(colon2++) = '\0';
  393. if (*colon) {
  394. tmp = atoi(colon);
  395. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  396. *miner_count = tmp;
  397. } else {
  398. quit(1,
  399. "Invalid avalon-options for "
  400. "miner_count (%s) must be 1 ~ %d",
  401. colon, AVALON_DEFAULT_MINER_NUM);
  402. }
  403. }
  404. if (colon2 && *colon2) {
  405. colon3 = strchr(colon2, ':');
  406. if (colon3)
  407. *(colon3++) = '\0';
  408. tmp = atoi(colon2);
  409. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  410. *asic_count = tmp;
  411. else {
  412. quit(1,
  413. "Invalid avalon-options for "
  414. "asic_count (%s) must be 1 ~ %d",
  415. colon2, AVALON_DEFAULT_ASIC_NUM);
  416. }
  417. if (colon3 && *colon3) {
  418. colon4 = strchr(colon3, ':');
  419. if (colon4)
  420. *(colon4++) = '\0';
  421. tmp = atoi(colon3);
  422. if (tmp > 0 && tmp <= 0xff)
  423. *timeout = tmp;
  424. else {
  425. quit(1,
  426. "Invalid avalon-options for "
  427. "timeout (%s) must be 1 ~ %d",
  428. colon3, 0xff);
  429. }
  430. if (colon4 && *colon4) {
  431. tmp = atoi(colon4);
  432. switch (tmp) {
  433. case 256:
  434. case 270:
  435. case 282:
  436. case 300:
  437. *frequency = tmp;
  438. break;
  439. default:
  440. quit(1,
  441. "Invalid avalon-options for "
  442. "frequency must be 256/270/282/300");
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }
  449. static bool avalon_detect_one(const char *devpath)
  450. {
  451. struct avalon_info *info;
  452. struct avalon_result ar;
  453. int fd, ret;
  454. int baud, miner_count, asic_count, timeout, frequency = 0;
  455. struct cgpu_info *avalon;
  456. int this_option_offset = ++option_offset;
  457. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  458. &timeout, &frequency);
  459. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  460. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  461. devpath, baud, miner_count, asic_count, timeout, frequency);
  462. fd = avalon_open2(devpath, baud, true);
  463. if (unlikely(fd == -1)) {
  464. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  465. return false;
  466. }
  467. /* We have a real Avalon! */
  468. avalon = calloc(1, sizeof(struct cgpu_info));
  469. avalon->drv = &avalon_drv;
  470. avalon->device_path = strdup(devpath);
  471. avalon->device_fd = fd;
  472. avalon->threads = AVALON_MINER_THREADS;
  473. add_cgpu(avalon);
  474. ret = avalon_reset(fd, &ar);
  475. if (ret) {
  476. ; /* FIXME: I think IT IS avalon and wait on reset;
  477. * avalon_close(fd);
  478. * return false; */
  479. }
  480. avalon_infos = realloc(avalon_infos,
  481. sizeof(struct avalon_info *) *
  482. (total_devices + 1));
  483. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  484. devpath, avalon->device_id);
  485. avalon_infos[avalon->device_id] = (struct avalon_info *)
  486. malloc(sizeof(struct avalon_info));
  487. if (unlikely(!(avalon_infos[avalon->device_id])))
  488. quit(1, "Failed to malloc avalon_infos");
  489. info = avalon_infos[avalon->device_id];
  490. memset(info, 0, sizeof(struct avalon_info));
  491. info->baud = baud;
  492. info->miner_count = miner_count;
  493. info->asic_count = asic_count;
  494. info->timeout = timeout;
  495. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  496. AVALON_TIME_FACTOR) / (float)info->miner_count;
  497. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  498. info->temp_max = 0;
  499. /* This is for check the temp/fan every 3~4s */
  500. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  501. if (info->temp_history_count <= 0)
  502. info->temp_history_count = 1;
  503. info->temp_history_index = 0;
  504. info->temp_sum = 0;
  505. info->temp_old = 0;
  506. info->frequency = frequency;
  507. /* Do something for failed reset ? */
  508. if (0) {
  509. /* Set asic to idle mode after detect */
  510. avalon_idle(avalon);
  511. avalon->device_fd = -1;
  512. avalon_close(fd);
  513. }
  514. return true;
  515. }
  516. static inline void avalon_detect()
  517. {
  518. serial_detect(&avalon_drv, avalon_detect_one);
  519. }
  520. static void __avalon_init(struct cgpu_info *avalon)
  521. {
  522. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  523. }
  524. static void avalon_init(struct cgpu_info *avalon)
  525. {
  526. struct avalon_result ar;
  527. int fd, ret;
  528. avalon->device_fd = -1;
  529. fd = avalon_open(avalon->device_path,
  530. avalon_infos[avalon->device_id]->baud);
  531. if (unlikely(fd == -1)) {
  532. applog(LOG_ERR, "Avalon: Failed to open on %s",
  533. avalon->device_path);
  534. return;
  535. }
  536. ret = avalon_reset(fd, &ar);
  537. if (ret) {
  538. avalon_close(fd);
  539. return;
  540. }
  541. avalon->device_fd = fd;
  542. __avalon_init(avalon);
  543. }
  544. static bool avalon_prepare(struct thr_info *thr)
  545. {
  546. struct cgpu_info *avalon = thr->cgpu;
  547. struct avalon_info *info = avalon_infos[avalon->device_id];
  548. struct timeval now;
  549. free(avalon->works);
  550. avalon->works = calloc(info->miner_count * sizeof(struct work *), 4);
  551. if (!avalon->works)
  552. quit(1, "Failed to calloc avalon works in avalon_prepare");
  553. if (avalon->device_fd == -1)
  554. avalon_init(avalon);
  555. else
  556. __avalon_init(avalon);
  557. gettimeofday(&now, NULL);
  558. get_datestamp(avalon->init, &now);
  559. return true;
  560. }
  561. static void avalon_free_work(struct thr_info *thr)
  562. {
  563. struct cgpu_info *avalon;
  564. struct avalon_info *info;
  565. struct work **works;
  566. int i;
  567. avalon = thr->cgpu;
  568. avalon->queued = 0;
  569. if (unlikely(!avalon->works))
  570. return;
  571. works = avalon->works;
  572. info = avalon_infos[avalon->device_id];
  573. for (i = 0; i < info->miner_count * 4; i++) {
  574. if (works[i]) {
  575. work_completed(avalon, works[i]);
  576. works[i] = NULL;
  577. }
  578. }
  579. }
  580. static void avalon_free_work_array(struct thr_info *thr)
  581. {
  582. struct cgpu_info *avalon;
  583. struct work **works;
  584. int i, j, mc;
  585. avalon = thr->cgpu;
  586. avalon->queued = 0;
  587. if (unlikely(!avalon->works))
  588. return;
  589. works = avalon->works;
  590. mc = avalon_infos[avalon->device_id]->miner_count;
  591. if (++avalon->work_array > 3)
  592. avalon->work_array = 0;
  593. for (i = avalon->work_array * mc, j = 0; j < mc; i++, j++) {
  594. if (likely(works[i])) {
  595. work_completed(avalon, works[i]);
  596. works[i] = NULL;
  597. }
  598. }
  599. }
  600. static void do_avalon_close(struct thr_info *thr)
  601. {
  602. struct avalon_result ar;
  603. struct cgpu_info *avalon = thr->cgpu;
  604. struct avalon_info *info = avalon_infos[avalon->device_id];
  605. avalon_free_work(thr);
  606. nmsleep(1000);
  607. avalon_reset(avalon->device_fd, &ar);
  608. avalon_idle(avalon);
  609. avalon_close(avalon->device_fd);
  610. avalon->device_fd = -1;
  611. info->no_matching_work = 0;
  612. }
  613. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  614. {
  615. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  616. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  617. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  618. info->temp0 = ar->temp0;
  619. info->temp1 = ar->temp1;
  620. info->temp2 = ar->temp2;
  621. if (ar->temp0 & 0x80) {
  622. ar->temp0 &= 0x7f;
  623. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  624. }
  625. if (ar->temp1 & 0x80) {
  626. ar->temp1 &= 0x7f;
  627. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  628. }
  629. if (ar->temp2 & 0x80) {
  630. ar->temp2 &= 0x7f;
  631. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  632. }
  633. *temp_avg = info->temp2;
  634. if (info->temp0 > info->temp_max)
  635. info->temp_max = info->temp0;
  636. if (info->temp1 > info->temp_max)
  637. info->temp_max = info->temp1;
  638. if (info->temp2 > info->temp_max)
  639. info->temp_max = info->temp2;
  640. }
  641. static inline void adjust_fan(struct avalon_info *info)
  642. {
  643. int temp_new;
  644. temp_new = info->temp_sum / info->temp_history_count;
  645. if (temp_new < 35) {
  646. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  647. info->temp_old = temp_new;
  648. } else if (temp_new > 55) {
  649. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  650. info->temp_old = temp_new;
  651. } else if (abs(temp_new - info->temp_old) >= 2) {
  652. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  653. info->temp_old = temp_new;
  654. }
  655. }
  656. static bool avalon_fill(struct cgpu_info *avalon)
  657. {
  658. int subid, mc = avalon_infos[avalon->device_id]->miner_count;
  659. struct work *work;
  660. if (avalon->queued >= mc)
  661. return true;
  662. work = get_queued(avalon);
  663. if (unlikely(!work))
  664. return false;
  665. subid = avalon->queued++;
  666. work->subid = subid;
  667. avalon->works[avalon->work_array * mc + subid] = work;
  668. if (avalon->queued >= mc)
  669. return true;
  670. return false;
  671. }
  672. static int64_t avalon_scanhash(struct thr_info *thr)
  673. {
  674. struct cgpu_info *avalon;
  675. struct work **works;
  676. int fd, ret, full;
  677. struct avalon_info *info;
  678. struct avalon_task at;
  679. struct avalon_result ar;
  680. int i;
  681. int avalon_get_work_count;
  682. int start_count, end_count;
  683. struct timeval tv_start, tv_finish, elapsed;
  684. uint32_t nonce;
  685. int64_t hash_count;
  686. static int first_try = 0;
  687. int result_count, result_wrong;
  688. avalon = thr->cgpu;
  689. works = avalon->works;
  690. info = avalon_infos[avalon->device_id];
  691. avalon_get_work_count = info->miner_count;
  692. if (unlikely(avalon->device_fd == -1)) {
  693. if (!avalon_prepare(thr)) {
  694. applog(LOG_ERR, "AVA%i: Comms error(open)",
  695. avalon->device_id);
  696. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  697. /* fail the device if the reopen attempt fails */
  698. return -1;
  699. }
  700. }
  701. fd = avalon->device_fd;
  702. #ifndef WIN32
  703. tcflush(fd, TCOFLUSH);
  704. #endif
  705. start_count = avalon->work_array * avalon_get_work_count;
  706. end_count = start_count + avalon_get_work_count;
  707. i = start_count;
  708. while (true) {
  709. avalon_init_task(&at, 0, 0, info->fan_pwm,
  710. info->timeout, info->asic_count,
  711. info->miner_count, 1, 0, info->frequency);
  712. avalon_create_task(&at, works[i]);
  713. ret = avalon_send_task(fd, &at, avalon);
  714. if (unlikely(ret == AVA_SEND_ERROR ||
  715. (ret == AVA_SEND_BUFFER_EMPTY &&
  716. (i + 1 == end_count) &&
  717. first_try))) {
  718. do_avalon_close(thr);
  719. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  720. avalon->device_id);
  721. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  722. first_try = 0;
  723. nmsleep(1000);
  724. avalon_init(avalon);
  725. return 0; /* This should never happen */
  726. }
  727. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == end_count)) {
  728. first_try = 1;
  729. avalon_free_work_array(thr);
  730. return 0xffffffff;
  731. }
  732. works[i]->blk.nonce = 0xffffffff;
  733. if (ret == AVA_SEND_BUFFER_FULL)
  734. break;
  735. i++;
  736. }
  737. if (unlikely(first_try))
  738. first_try = 0;
  739. elapsed.tv_sec = elapsed.tv_usec = 0;
  740. gettimeofday(&tv_start, NULL);
  741. result_count = 0;
  742. result_wrong = 0;
  743. hash_count = 0;
  744. while (true) {
  745. full = avalon_buffer_full(fd);
  746. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  747. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  748. if (unlikely(full == AVA_BUFFER_EMPTY))
  749. break;
  750. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  751. if (unlikely(ret == AVA_GETS_ERROR)) {
  752. do_avalon_close(thr);
  753. applog(LOG_ERR,
  754. "AVA%i: Comms error(read)", avalon->device_id);
  755. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  756. return 0;
  757. }
  758. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  759. timersub(&tv_finish, &tv_start, &elapsed);
  760. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  761. elapsed.tv_sec, elapsed.tv_usec);
  762. continue;
  763. }
  764. if (unlikely(ret == AVA_GETS_RESTART)) {
  765. /* Reset the wrong count in case there has only been
  766. * a small number of nonces tested before the restart.
  767. */
  768. result_wrong = 0;
  769. break;
  770. }
  771. result_count++;
  772. if (!avalon_decode_nonce(thr, &ar, &nonce)) {
  773. info->no_matching_work++;
  774. result_wrong++;
  775. if (opt_debug) {
  776. timersub(&tv_finish, &tv_start, &elapsed);
  777. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  778. " (%ld.%06lds)", info->no_matching_work,
  779. elapsed.tv_sec, elapsed.tv_usec);
  780. }
  781. continue;
  782. }
  783. hash_count += nonce;
  784. if (opt_debug) {
  785. timersub(&tv_finish, &tv_start, &elapsed);
  786. applog(LOG_DEBUG,
  787. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  788. "(%ld.%06lds)", nonce, hash_count,
  789. elapsed.tv_sec, elapsed.tv_usec);
  790. }
  791. }
  792. if (result_wrong && result_count == result_wrong) {
  793. /* This mean FPGA controller give all wrong result
  794. * try to reset the Avalon */
  795. do_avalon_close(thr);
  796. applog(LOG_ERR,
  797. "AVA%i: FPGA controller mess up", avalon->device_id);
  798. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  799. nmsleep(1000);
  800. avalon_init(avalon);
  801. return 0;
  802. }
  803. avalon_free_work_array(thr);
  804. record_temp_fan(info, &ar, &(avalon->temp));
  805. applog(LOG_INFO,
  806. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  807. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  808. info->fan0, info->fan1, info->fan2,
  809. info->temp0, info->temp1, info->temp2, info->temp_max);
  810. info->temp_history_index++;
  811. info->temp_sum += info->temp2;
  812. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  813. info->temp_history_index, info->temp_history_count, info->temp_old);
  814. if (info->temp_history_index == info->temp_history_count) {
  815. adjust_fan(info);
  816. info->temp_history_index = 0;
  817. info->temp_sum = 0;
  818. }
  819. /*
  820. * FIXME: Each work split to 10 pieces, each piece send to a
  821. * asic(256MHs). one work can be mulit-nonce back. it is not
  822. * easy calculate correct hash on such situation. so I simplely
  823. * add each nonce to hash_count. base on Utility/m hash_count*2
  824. * give a very good result.
  825. *
  826. * Any patch will be great.
  827. */
  828. return hash_count * 2;
  829. }
  830. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  831. {
  832. struct api_data *root = NULL;
  833. struct avalon_info *info = avalon_infos[cgpu->device_id];
  834. root = api_add_int(root, "baud", &(info->baud), false);
  835. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  836. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  837. root = api_add_int(root, "read_count", &(info->read_count), false);
  838. root = api_add_int(root, "timeout", &(info->timeout), false);
  839. root = api_add_int(root, "frequency", &(info->frequency), false);
  840. root = api_add_int(root, "fan1", &(info->fan0), false);
  841. root = api_add_int(root, "fan2", &(info->fan1), false);
  842. root = api_add_int(root, "fan3", &(info->fan2), false);
  843. root = api_add_int(root, "temp1", &(info->temp0), false);
  844. root = api_add_int(root, "temp2", &(info->temp1), false);
  845. root = api_add_int(root, "temp3", &(info->temp2), false);
  846. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  847. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  848. root = api_add_int(root, "matching_work_count", &(info->matching_work), false);
  849. return root;
  850. }
  851. static void avalon_shutdown(struct thr_info *thr)
  852. {
  853. do_avalon_close(thr);
  854. }
  855. struct device_drv avalon_drv = {
  856. .dname = "avalon",
  857. .name = "AVA",
  858. .drv_detect = avalon_detect,
  859. .thread_prepare = avalon_prepare,
  860. .minerloop = hash_queued_work,
  861. .queue_full = avalon_fill,
  862. .scanwork = avalon_scanhash,
  863. .get_api_stats = avalon_api_stats,
  864. .reinit_device = avalon_init,
  865. .thread_shutdown = avalon_shutdown,
  866. };